A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology
WISDOM W-What do you learn from this I-What is your Interest S-Your level of Sophistication D-Level of Detail O-Ownership-“The Pragmatic Programmer” »By Andy Hunt and David Thomas M-My Motivation
Topics discussed Part 1: Introduction Part 2: THE WHY: gdb? –Features Part 3: THE HOW –Commands Part 4: PRACTICE: Hands on debugging session Part 4: Advanced concepts Part 5: Conclusion –Q & A –References
Part 1 Introduction
What is a debugger? Why do we need one? gdb is NOT –A shell –A text editor –A programming environment Other types of debuggers –ddd, GDBTk, Insight, dbx, softice, etc.
What is a BUG? Bug- program does not generate the expected output BUG or Fault? Errors or bugs –syntactic –Logical Syntactic errors - caught by compiler Logical errors - difficult to find and remove.
Part 2 Why do you need to learn GDB?
Advantages of gdb Interactive Responsive Permissive Capability to work with very large programs –Reported cases: file size close to 1 GB Extremely portable –No other debugger works on so many configurations
Advantages continued Can debug optimized code Can debug macros Multiple processor support Capability to debug multi threaded applications Remote debugging
Major Features Set Breakpoints Examine Data Specify Files Run Program Examine Stack Report Status Trace Execution
Part 3: Working
Compiling the program C-program - compiled using gcc compiler Compilation command: –Example: “gcc -o testout -g test.c” “-o” generates the output “-g” enables gdb C++ program - compiled using g++ Compilation command: –Example: “g++ -o testout -g test.cpp” “-o” generates the output “-g” enables gdb
How to get in and get out Command: gdb Alternate Formats –gdb –gdb program pid –gdb program core –gdb a.out (not recommended) –gdb --args./exe/ To quit gdb use the “quit” or “q” command
Modes of Operation Has many modes some of which are: –Silent –Batch useful for running GDB as a filter. –Example: Download and run a program on another computer –Shell –No windows –Set baud rate (for remote debugging) –Output statistics about time and memory
Logging output set logging on –Enable logging. set logging off –Disable logging. set logging file file –Change the name of the current logfile. The default logfile is `gdb.txt'. set logging overwrite [on|off] –By default, GDB will append to the logfile. Set overwrite if you want set logging on to overwrite the logfile instead. set logging redirect [on|off] –By default, GDB output will go to both the terminal and the logfile. Set redirect if you want output to go only to the log file. show logging –Show the current values of the logging settings.
Halting program execution Break Points –Predefined points where program stops Watch Points –Special breakpoint that stops the program when the value of an expression changes Catch Points –Special breakpoint that stops the program on an event Throwing of a C++ exception Loading of a library
Break Points break function break +/-offset break linenum break filename:linenum break filename:function break *address break break... if cond
Watch Points watch expr rwatch expr awatch expr info watchpoints Hardware & Software watchpoints –set can-use-hw-watchpoints 0
Catch Points Syntax: –catch event Event can be any of the following –throw The throwing of a C++ exception –catch The catching of a C++ exception –exec A call to exec. This is currently only available for HP-UX –fork A call to fork. This is currently only available for HP-UX –vfork A call to vfork. This is currently only available for HP-UX
Break point commands info break list ignore Remove break points –“delete” –“clear” or
Stepping Stepping is the process of executing the program line by line Commands –next – step over –step – step through –continue – jump between break points To step over a few lines of code –step –next
Print command Formats –o – octal –x – hexadecimal –d – decimal –u - unsigned decimal –t – binary –f – float –a – address –i – instruction –c – char –s - string
Useful commands set whatis ptype Call Return gdb has command completion –Hit TAB to complete a command complete r –Lists all commands starting with r. aprops
Most useful command help
Examining Data list linenum list function list list - list + set listsize count show listsize
Edit Data edit edit number edit function edit *address edit filename:number edit filename:function
Editors Vi Emacs XEmacs Choose an editor –EDITOR=/usr/bin/vi –export EDITOR –gdb …….
Search source files forward-search regexp search regexp reverse-search regexp SPECIFY SOURCE DIRECTORY –dir dirname... Adds the name of the directory to the beginning of the source directory path –show directories Shows the source path –show directories Resets the source path to empty.
Examine Stack Very useful in debugging Each function is placed on a stack Function “main” is the only function – one stack frame –Each additional function has a stack frame frame args: Move from one stack to another select-frame: silent version of frame command
Frame Information info f –This command prints a verbose description of the selected stack frame, including: the address of the frame the address of the next frame down (called by this frame) the address of the next frame up (caller of this frame) the language in which the source code corresponding to this frame is written the address of the frame's arguments the address of the frame's local variables the program counter saved in it (the address of execution in the caller frame) which registers were saved in the frame
Info command for frames info frame addr info args info locals info catch SELECT A STACK FRAME –frame –frame addr –up n –down n
Back Trace bt (backtrace) –Stop by hitting control-C backtrace n backtrace full
D E M O Debugging Session Getting our hands dirty
Q & A 10 Minutes