How to debug a program with GDB – Part 1

This article describes how to run gdb with a simple program example. gdb expands to GNU Project Debugger. It is a very powerful tool to debug programs and to investigate Core dumps generated by different programs. The Article tries to setup a reader to start experimenting with gdb via a sample program.

Sample Program

compile the program with the below command

Run the GDB tool on the output file generated.  Noteworthy in the above command is to check the compiler options that are passed to “gcc” to compile the program

  1. The -g flag will enable a core to be created. The option informs the compiler to retain the source level debugging and symbol information in the executable itself.
  2. The -O0 flag indicates to the compiler to turn off all compiler optimizations

The gdb command and the load of the gdb_test symbols by the gdb tool is provided below.

A simple way to place a break-point in the program is to run the below gdb command.

A break-point is placed at start of the main program.  We expect the break-point to be hit and program execution to stop at the first line of the main function when the program is executed. With the break-point in place – run the program

it is seen from the above execution that the break-point is hit at line 6 which is the starting C instruction in the sample program. To Single step the program – run the below command

if you desire the program to continue execution – type the “continue” command

The main program completes and exits. The above message “[Inferior 1 (process 3329) exited with code 01]  only indicates that the main function invoked exit(1) at the end. 

To quit gdb – run the “quit” command

In the next article – we will understand how to look at variables and data from the program and also look at debugging an actual error.

How to debug a program with gdb – Part 2

Comments

  1. Pingback: An understanding of memory layout in C – Part 2 | Hitch Hiker's Guide to Learning

  2. Pingback: How to debug a Program with gdb – Part 2 | Hitch Hiker's Guide to Learning

Leave a Reply

Your email address will not be published. Required fields are marked *