Docsity

GDB Debugger Tutorial for CS240 at Purdue University - Prof. Gustavo Rodriguez-Rivera, Study notes of Computer Science

Partial preview of the text

Download GDB Debugger Tutorial for CS240 at Purdue University - Prof. Gustavo Rodriguez-Rivera and more Study notes Computer Science in PDF only on Docsity!

GDB Tutorial

CS

Purdue University

What is gdb

GDB is a debugger that helps you debug your program.

The time you spend now learning gdb will save you days of debugging time.

A debugger will make a good programmer a better programmer.

Running a Program with gdb

 (^) To run a program with gdb type gdb progname (gdb)  (^) Then set a breakpoint in the main function. (gdb) break main  (^) A breakpoint is a marker in your program that will make the program stop and return control back to gdb.  (^) Now run your program. (gdb) run  (^) If your program has arguments, you can pass them after run.

Stepping Through your Program

 Your program will start running and when it reaches

“main()” it will stop.

gdb>

 Now you have the following commands to run your

program step by step:

(gdb) step It will run the next line of code and stop. If it is a function call, it will enter into it (gdb) next It will run the next line of code and stop. If it is a function call, it will not enter the function and it will go through it. Example: (gdb) step (gdb) next

Regaining the Control

When you type

(gdb) run

the program will start running and it will stop at a break

point.

If the program is running without stopping, you can regain control again typing ctrl-c.

Where is your Program

The command

(gdb)where

Will print the current function being executed and the

chain of functions that are calling that fuction.

This is also called the backtrace.

Example:

(gdb) where #0 main () at test_mystring.c: (gdb)

Exiting gdb

The command “quit” exits gdb. (gdb) quit The program is running. Exit anyway? (y or n) y

Debugging a Crashed Program

This is also called “postmortem debugging”

It has nothing to do with CSI 

When a program crashes, it writes a core file.

bash-4.1$ ./hello

Segmentation Fault (core dumped)

bash-4.1$

The core is a file that contains a snapshot of the program at the time of the crash. That includes what function the program was running.

Now Try gdb in Your Own Program

Make sure that your program is compiled with the –g option.

Remember:  One hour you spend learning gdb will save you days of debugging.  Less stress, better grades.