Presentation is loading. Please wait.

Presentation is loading. Please wait.

A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.

Similar presentations


Presentation on theme: "A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology."— Presentation transcript:

1 A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology

2 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

3 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

4 Part 1 Introduction

5 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.

6 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.

7 Part 2 Why do you need to learn GDB?

8 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

9 Advantages continued Can debug optimized code Can debug macros Multiple processor support Capability to debug multi threaded applications Remote debugging

10 Major Features Set Breakpoints Examine Data Specify Files Run Program Examine Stack Report Status Trace Execution

11 Part 3: Working

12 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

13 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

14 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

15 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.

16 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

17 Break Points break function break +/-offset break linenum break filename:linenum break filename:function break *address break break... if cond

18 Watch Points watch expr rwatch expr awatch expr info watchpoints Hardware & Software watchpoints –set can-use-hw-watchpoints 0

19 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

20 Break point commands info break list ignore Remove break points –“delete” –“clear” or

21 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

22 Print command Formats –o – octal –x – hexadecimal –d – decimal –u - unsigned decimal –t – binary –f – float –a – address –i – instruction –c – char –s - string

23 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

24 Most useful command help

25 Examining Data list linenum list function list list - list + set listsize count show listsize

26 Edit Data edit edit number edit function edit *address edit filename:number edit filename:function

27 Editors Vi Emacs XEmacs Choose an editor –EDITOR=/usr/bin/vi –export EDITOR –gdb …….

28 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.

29 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

30 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

31 Info command for frames info frame addr info args info locals info catch SELECT A STACK FRAME –frame –frame addr –up n –down n

32 Back Trace bt (backtrace) –Stop by hitting control-C backtrace n backtrace full

33 D E M O Debugging Session Getting our hands dirty

34 Q & A 10 Minutes


Download ppt "A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology."

Similar presentations


Ads by Google