Debugging with gdb David Khosid Sept 6, 2009

Slides:



Advertisements
Similar presentations
Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
Advertisements

R4 Dynamically loading processes. Overview R4 is closely related to R3, much of what you have written for R3 applies to R4 In R3, we executed procedures.
Introduction to the Omega Server CSE Overview Intro to Omega Basic Unix Command Files Directories Printing C and C++ compilers GNU Debugger.
Gnu Debugger (GDB) Topics Overview Quick Reference Card Readings: Quick Reference Card February 7, 2012 CSCE 212Honors Computer Organization.
Utilizing the GDB debugger to analyze programs Background and application.
Debugging What can debuggers do? Run programs Make the program stops on specified places or on specified conditions Give information about current variables’
Gdb: GNU Debugger Lecturer: Prof. Andrzej (AJ) Bieszczad Phone: “UNIX for Programmers and Users” Third Edition, Prentice-Hall,
CSE 303 Lecture 13a Debugging C programs
CS 225 Lab #2 - Pointers, Copy Constructors, Destructors, and DDD.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
OllyDbg Debuger.
Introduction Purpose Objectives Content Learning Time
Embedded Systems Principle of Debugger. Reference Materials kl.de/avr_projects/arm_projects/#winarmhttp://
Spring 2014 SILICON VALLEY UNIVERSITY CONFIDENTIAL 1 Introduction to Embedded Systems Dr. Jerry Shiao, Silicon Valley University.
Debugging techniques in Linux Debugging Techniques in Linux Chetan Kumar S Wipro Technologies.
F13 Forensic tool analysis Dr. John P. Abraham Professor UTPA.
Gdb is the GNU debugger on our CS machines. gdb is most effective when it is debugging a program that has debugging symbols linked in to it. With gcc and.
Chocolate Bar! luqili. Milestone 3 Speed 11% of final mark 7%: path quality and speed –Some cleverness required for full marks –Implement some A* techniques.
Homework Reading Programming Assignments
Debugging Cluster Programs using symbolic debuggers.
Computer System Laboratory
Memory & Storage Architecture Seoul National University GDB commands Hyeon-gyu School of Computer Science and Engineering.
Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.
Compiling & Debugging Quick tutorial. What is gcc? Gcc is the GNU Project C compiler A command-line program Gcc takes C source files as input Outputs.
CSE 332: C++ debugging in Eclipse C++ Debugging in Eclipse We’ve now covered several key program features –Variable declarations, expressions and statements.
Introduction Purpose This training course covers debugging an application on an SH target in the Renesas HEW (High-performance Embedded Workshop) development.
Debugging in Java. Common Bugs Compilation or syntactical errors are the first that you will encounter and the easiest to debug They are usually the result.
CS 241 Section Week #2 9/9/10. 2 Topics This Section MP1 issues MP2 overview Process creation using fork()‏ Debugging tools: valgrind, gdb.
Debugging and Profiling With some help from Software Carpentry resources.
A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.
CSE 332: C++ debugging Why Debug a Program? When your program crashes –Finding out where it crashed –Examining program memory at that point When a bug.
Bit-DSP-MicrocontrollerTMS320F2812 Texas Instruments Incorporated European Customer Training Center University of Applied Sciences Zwickau (FH)
CSE 351 GDB Introduction. Lab 1 Status? How is Lab 1 going? I’ll be available at the end of class to answer questions There are office hours later today.
Data Display Debugger (DDD)
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 11 – gdb and Debugging.
Debugging 1/6/2016. Debugging 1/6/2016 Debugging  Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a program.
Dale Roberts Debugger Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School.
Unit - V. Debugging GNU Debugger helps you in getting information about the following: 1.If a core dump happened, then what statement or expression did.
CSI605 Introduction to ddd. ddd ddd stands for the Data Display Debugger ddd is a graphical environment that resides on top of gdb We recall that gdb.
Debugging tools in Flash CIS 126. Debugging Flash provides several tools for testing ActionScript in your SWF files. –The Debugger, lets you find errors.
17/02/2016S. Ponce / EP-LBC1 Debugging Under Linux Sebastien Ponce Friday, 8 March 2002.
Lab 9 Department of Computer Science and Information Engineering National Taiwan University Lab9 - Debugging I 2014/11/4/ 28 1.
CSE 332: C++ expressions Expressions: Operators and Operands Operators obey arity, associativity, and precedence int result = 2 * 3 + 5; // assigns 11.
HP-SEE Debugging with GDB Vladimir Slavnic Research Assistant SCL, Institute of Physics Belgrade The HP-SEE initiative.
 Wind River Systems, Inc Chapter - 4 CrossWind.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Gnu Debugger (GDB) Topics Overview Quick Reference Card Readings: Quick Reference Card February 4, 2010 CSCE 212Honors Computer Organization.
CSE 332: Scientific debugging in C++ Scientific Debugging in C++ (with Eclipse & gdb) By now we’ve covered several key C++ features –Variable declarations.
Dale Roberts Debugger Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School.
Using the GNU Debugger (GDB)‏ Techzemplary Pvt.Ltd February 24 th 2008 Pranav Peshwe.
Institute of Radio Physics and Electronics ILug-Cal Introduction to GDB Institute of Radio Physics and Electronics and Indian GNU/Linux Users Group Kolkata.
Gnu Debugger (gdb) Debuggers are used to: Find semantic errors Locate seg faults and bus errors Prepared by Dr. Spiegel.
DEBUG.
Computer System Laboratory
Gnu Debugger (gdb) Debuggers are used to: Find semantic errors
Dynamic Analysis ddaa.
CSE 374 Programming Concepts & Tools
CSCE 212Honors Computer Organization
Debugging with gdb gdb is the GNU debugger on our CS machines.
gdb gdb is the GNU debugger on our CS machines.
Debugging with Eclipse
DEBUGGING JAVA PROGRAMS USING ECLIPSE DEBUGGER
CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science
GNU DEBUGGER TOOL. What is the GDB ? GNU Debugger It Works for several languages – including C/C++ [Assembly, Fortran,Go,Objective-C,Pascal]
When your program crashes
CSE 303 Concepts and Tools for Software Development
CSCE 212Honors Computer Organization
Debugging.
Makefiles, GDB, Valgrind
Debugging with Eclipse
Presentation transcript:

Debugging with gdb David Khosid Sept 6, 2009

Agenda Techniques for debugging big, modern software: – STL containers and algorithms, Boost (ex: how to see containers) – Multi-threaded (ex.: how to follow a thread?) – Signals – Repetitive tasks on the almost unchanging code base Program structure and running – Memory layout – Stack and heap – 64bit vs. 32 bit Examples 2

GDB was first written by Richard Stallman in 1986 as part of his GNU system Richard Stallman, “Debugging with gdb” Help: $gdb –h (gdb) h (gdb) apropos Command names may be truncated if the abbreviation is unambiguous. TAB completion. Command Cheat Sheet Last GDB version is Sources of information

What can debuggers do? Start your program for you, specifying anything that might affect it's behavior. Make your program stop under specified conditions. Examine what happened when the program stopped.(current variables’ values, the memory and the stack) Allow you to experiment with changes to see what effect they have on the program. Let you examine the program execution step by step Let you examine the change of program variables’ values - tracing must compile your program with the -g option (creates the symbol table) ! 4

Getting In and Out of GDB 1. gdb my_prog –silent 2. gdb my_prog core_files –si 3. gdb my_prog pid –si Loading the symbol table: (gdb) file my_prog Exit GDB: q or Ctrl-D shell commands:(gdb) shell command args short form: (gdb) make make-args (gdb) pwd; (gdb) cd 5

Debugging an already-running process From inside GDB: attach process-id From outside GDB: gdb my_prog process-id ! The first thing GDB does after arranging to debug the specified process is to stop it. detach – detaches the currently attached process from the GDB control. A detached process continues its own execution. 6

Program’s Arguments Specifying arguments for your program 1. As arguments to run : run arg1 arg2 2. With set args command: set args arg1 arg2 3. With –-args option. Ex: # sudo gdb -silent --args /bin/ping google.com run without arguments uses the same arguments used by the previous run. set args without arguments – removes all arguments. show args command shows the arguments your program has been started with 7

Program’s environment (gdb) show environment (gdb)path dir – add the directory dir at the beginning of the PATH variable. (gdb)show paths – displays the search paths for executables. The working directory: pwd cd dir – to change the working directory 8

Breakpoints and watchpoints Allow you to specify the places or the conditions where you want your program to stop. (gdb) b location if cond (gdb) watch expr – stops whenever the value of the expression changes (gdb) i b (gdb) clear [arg] (gdb) delete [bnum] Without arguments deletes all breakpoints. 9

Examining variables The variable type: ptype var Current value: p var Automatic display: display var - adds var to the automatic display list. undisplay dnum Specifying the output format ( x, o, d, u, t, a, f, and c ) : print /t var - prints the value of var in binary format 10

GDB – Examining memory The x command (for “examine”): x/nfu addr – specify the number of units ( n ), the display format ( f ) and the unit size ( u ) of the memory you want to examine, starting from the address addr. Unit size can be – b, h (half), w and g (giant). x addr – start printing from the address addr, others default x – all default Registers Registers names are different for each machine. Use info registers to see the names used on your machine. GDB has four “standard” registers names that are available on most machines: program counter, stack pointer, frame pointer and processor status. 11

C++ and STL - Containers How to see container’s content? Commands file.gdbinit Limitations: a little, ex., heterogeneous std::map libstdc++ compiled in debug mode Auxiliary functions typedef map MapStringFloat; void testPrint(const MapStringFloat& m){ for(MapStringFloat::const_iterator pos = m.begin(); pos != m.end(); ++pos){ cout first second << "\n"; } Pretty-printing of STL containers in future versions of GDB 12

C++ and STL - continue Overloaded functions rbreak regex - is useful for setting breakpoints on overloaded functions that are not members of any special classes. The rbreak command can be used to set breakpoints in all the functions in a program, like this: (gdb) rbreak. Exceptions: catchpoints (gdb) catch throw, (gdb) catch catch Templates It is possible that a breakpoint corresponds to several locations in your program. 13

The stack frame Stack frames are identified by their addresses, which are kept in the frame pointer register  Selecting a frame: f n up n down n  Information about the current frame f or frame – brief description i args – shows function arguments i locals – shows local variables  Stack: (gdb)bt or (gdb)bt full

Stepping through the program step [count] – program execution continue to next source line going into function calls. ‘n’ or ‘next’ [count] – program execution continue to the next source line omitting function calls. ‘c’ or ‘continue’ – resume program execution until – continue until the next source line in the current stack frame is reached. /useful to exit from loops/ 15

Altering execution Returning from a function finish – Continue running until just after function in the selected stack frame returns. return [ret_value] – pops the current stack frame Continuing at different address jump line_num|*address Altering the value of a variable set i=256 16

Convenience variables Convenience variables are used to store values that you may want to refer later. Any string preceded by $ is regarded as a convenience variable. Ex.: set $table = *table_ptr (gdb) show conv There are several automatically created convenience variables: $pc – program counter $sp – stack pointer $fp – frame pointer 17

Extending GDB - init files What GDB Does During Startup 1. Executes all commands from system init file 2. Executes all the commands from ~/.gdbinit 3. Process command line options and operands 4. Executes all the commands from./.gdbinit 5. reads command files specified by the `-x' option 6. … 18

Extending GDB - History, recording What GDB Does During Startup … 6. Reads the command history recorded in the history file. (gdb) set history filename fname (gdb) set history save on/off (gdb) show history (gdb) show commands 19

Extending GDB – User-defined commands (gdb) show user commandname Example: (gdb)define adder print $arg0 + $arg1 + $arg2 end (gdb) adder

Editing files during debugging Example: (gdb) run Program received signal SIGSEGV, Segmentation fault. (gdb) edit or (gdb) shell vi crash.cpp (gdb) shell gcc crash.cpp -o crash –lstdc++ (gdb) run Program exited normally. (gdb) quit 21

Signals ‘i handle’ or ‘i signals’ Print a table of all the signals and how gdb has been told to handle each one. handle signal [keywords...] keywords: nostop|stop, print|noprint and pass|nopass Ex: handle SIG35 nostop print pass handle SIG36 stop (implies the ‘print’ as well) handle SIG37 nostop print nopass handle SIG38 nostop noprint nopass 22

Multi-threads Use case: debugging specific thread, while controlling behavior of others. facilities for debugging multi-thread programs: automatic notification of new threads ‘thread threadno’, to switch among threads ‘info threads’, to inquire about existing threads thread-specific breakpoints set mode for locking scheduler during execution (gdb) set scheduler-locking step/on/off others: Interrupted System Calls Example: (gdb) i threads break foo.cpp:13 thread 28 if x > lim 23

Checkpoint A snapshot of a program’s state (gdb) checkpoint (gdb) i checkpoint (gdb) restart checkpoint-id 24

64 bit.vs. 32bit -m32 flag On 64-bit machine, install another 32-bit version of GDB $ ls -l `which gdb32` /usr/bin/gdb32 -> ‘/your/install/path’ 25

Additional process information i proc – summarize available information about the current process. i proc mappings – address range accessible in the program. 26

Remote debugging Use case: - GDB runs on one machine (host) and the program being debugged (exe.verXYZ.stripped ) runs on another (target). - GDB communicates via Serial or TCP/IP. - Host and target: exactly match between the executables and libraries, with one exception: stripped on the target. - Complication: compiling on one machine (CC view), keeping code in different place (ex. /your/path/verXYZ) Solution: - Connect gdb to source in the given place: (gdb) set substitute-path /usr/src /mnt/cross (gdb) dir /your/path/verXYZ 27

Remote debugging - example Using gdbserver through TCP connection: remote ( )> gdbserver :9999 program_stripped or remote>./gdbserver :9999 –attach host> gdb program host>(gdb) handle SIGTRAP nostop noprint pass to avoid pausing when launching the threads host> (gdb) target remote :

Various issues DDD and Eclipse Working with Shared Libraries How to see macros 29

DDD and Eclipse - GUI Advantages DDD is a GUI debugger that work with GDB. GDB commands can be typed in the console window. Frequently used commands are on the toolbars, have assigned shortcut keys or can be done just with a mouse click. Easy browsing through the source Examining current variables values directly – by placing the mouse pointer over them. Possibility to graphically display the program data. IMHO, stability is the main issue of debugging in DDD and especially Eclipse. 30

Summary 1.Start from thinking of Use Case, then look in the manual, use ‘apropos’ and ‘help’ 2.Productivity: Stepping through a program is less productive than thinking harder and adding output statements and self-checking code at critical places. 3.When to use GDB? - core file, - when a problem can be reproduced, repeating errors - self-educating 4.When not? Other tools, traces 5.Questions? 31

Questions and how-to's 1.How to create a symbol table? How to remove it? 2.How to load up your program in GDB? 3.How to know where you are (file, next execution line)? 4.How to find out the crash file executable? 5.How to find out why a program stopped? 6.Which command(s) can be used to exit from loops? 7.Why ‘print’, ‘info’, ‘show’? 32

Problem Determination Tools for Linux -Wall Code review Program’s traces, syslog, profilers Static Source Code Analysis: – scan.coverity.com – free for FOSS scan.coverity.com – Flexelint Dynamic analysis: Valgrind, strace, /proc filesystem, lsof, ldd, nm, objdump, wireshark 33