Debugging with GDB

2016-11-08  本文已影响173人  iMark

To debug, you must generate debugging info when compiling, and this requires you to pass the -g option to the compiler. If you don't know why, just think of how you debug with visual studio. (FAQ之 Debug单步调试)

I'll illustrate how to debug with GDB with a simple C program.

#include <stdio.h>

void main () 
{
    /*inteded to transfer temprature format
    from Fahrenheit to Celsius*/

    float far, cel, inp;
    const int up=300, low=0, step=20;

    far=low;

    puts("This is a list for temprature conversion:\n\n");
    printf("%s\t%s\n\n", "Fahrenheit", "Celsius");

    while(far <= up)
    {
        cel = 5 * (far-32) / 9.;
        printf("%5.0f F \t%9.2f C\n", far, cel);
        far += step;
    }

    //Now, we'll read input
    printf("Please enter the value of Fahrenheit\n");
    scanf("%f", &inp);
    printf("You have entered Fahrenheit temprature %f F\n", inp);
    printf("The Celsius temprature is %f C\n", 5 * (inp-32) / 9.);
}

1. The debug mode and release mode

compile the C source with/out debug info

I run the compiler with release mode and debug mode, respectively. Accordingly, executable file are named with release and debug. As it shown in the picture above, the release version is smaller than the debug one, which is resulted from the lack of debug info.


2. debug with GDB

To start the debugging, type gdb exe_name. Note the name here is the debugged executable file, not the source code.

break to set breakpoints, e.g., break 20 to set at the 20th line.

the command is run

You can type continue to let the program go on.


3.Below is the full info in the terminal for debugging.

mark@ASUS:~/Desktop$ gdb debug
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from debug...done.
(gdb) break 20
Breakpoint 1 at 0x4006ec: file temprt.c, line 20.
(gdb) continue
The program is not being run.
(gdb) run
Starting program: /home/mark/Desktop/debug 
This is a list for temprature conversion:


Fahrenheit  Celsius

    0 F        -17.78 C

Breakpoint 1, main () at temprt.c:20
warning: Source file is more recent than executable.
20          far += step;
(gdb) continue
Continuing.
   20 F         -6.67 C

Breakpoint 1, main () at temprt.c:20
20          far += step;
(gdb) continue
Continuing.
   40 F          4.44 C

Breakpoint 1, main () at temprt.c:20
20          far += step;
(gdb) continue
Continuing.
   60 F         15.56 C

Breakpoint 1, main () at temprt.c:20
20          far += step;
(gdb) continue
Continuing.
   80 F         26.67 C

Breakpoint 1, main () at temprt.c:20
20          far += step;
(gdb) continue
Continuing.
  100 F         37.78 C

Breakpoint 1, main () at temprt.c:20
20          far += step;
(gdb) continue
Continuing.
  120 F         48.89 C

Breakpoint 1, main () at temprt.c:20
20          far += step;
(gdb) continue
Continuing.
  140 F         60.00 C

Breakpoint 1, main () at temprt.c:20
20          far += step;
(gdb) continue
Continuing.
  160 F         71.11 C

Breakpoint 1, main () at temprt.c:20
20          far += step;
(gdb) continue
Continuing.
  180 F         82.22 C

Breakpoint 1, main () at temprt.c:20
20          far += step;
(gdb) continue
Continuing.
  200 F         93.33 C

Breakpoint 1, main () at temprt.c:20
20          far += step;
(gdb) continue
Continuing.
  220 F        104.44 C

Breakpoint 1, main () at temprt.c:20
20          far += step;
(gdb) continue
Continuing.
  240 F        115.56 C

Breakpoint 1, main () at temprt.c:20
20          far += step;
(gdb) continue
Continuing.
  260 F        126.67 C

Breakpoint 1, main () at temprt.c:20
20          far += step;
(gdb) continue
Continuing.
  280 F        137.78 C

Breakpoint 1, main () at temprt.c:20
20          far += step;
(gdb) continue
Continuing.
  300 F        148.89 C

Breakpoint 1, main () at temprt.c:20
20          far += step;
(gdb) continue
Continuing.
Please enter the value of Celsius
3
You have entered Fahrenheit temprature 3.000000 F
The Celsius temprature is -16.111111 C
[Inferior 1 (process 23884) exited normally]
(gdb) 
上一篇下一篇

猜你喜欢

热点阅读