The use of GDB

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

mickole@test:~/ctest/05gdb$ gdb simple //Start the GDB debugging
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
Copyright (C) 2012 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 "i686-linux-gnu".
For bug reporting instructions, please see:
<>...
Reading symbols from /home/mickole/ctest/05gdb/simple...done.
(GDB) list // display the source file can also be used L -
1    #include <stdio.h>
2    #include <stdlib.h>
3   
4    long func(int n);
5   
6    int main(int argc, char *argv[])
7    {
8        printf("Entering main ...\n");
9        int i;
10        for (i=0; i<argc; ++i)
(GDB) // enter, go to a command
11        {
12            printf("%s ", argv[i]);
13        }
14        printf("\n");
15        long result = 0;
16        for (i=1; i<=100; ++i)
17        {
18            result += i;
19        }
20        printf("result[1-100] = %ld\n", result);
(gdb)
21        printf("result[1-10] = %ld\n", func(10));
22   
23        printf("Exiting main ...\n");
24        return 0;
25    }
26   
27    long func(int n)
28    {
29        long sum = 0;
30        int i;
(gdb)
31        for (i=1; i<=n; ++i)
32        {
33            sum += i;
34        }
35   
36        return sum;
37    }
(gdb)
Line number 38 out of range; simple.c has 37 lines.

(gdb) break 10 //Set a breakpoint is also used B
Breakpoint 1 at 0x8048459: file simple.c, line 10
(GDB) info B // view all breakpoint information can also be used instead of I B
Num     Type           Disp Enb Address    What
1       breakpoint     keep y   0x08048459 in main at simple.c:10

(gdb) b func //Set breakpoint entrance location can also be a function of the
Breakpoint 2 at 0x804850b: file simple.c, line 29.
(gdb) i b
Num     Type           Disp Enb Address    What
1       breakpoint     keep y   0x08048459 in main at simple.c:10
2       breakpoint     keep y   0x0804850b in func at simple.c:29

(gdb) l 10 //Look at the specified source lines of code
5   
6    int main(int argc, char *argv[])
7    {
8        printf("Entering main ...\n");
9        int i;
10        for (i=0; i<argc; ++i)
11        {
12            printf("%s ", argv[i]);
13        }
14        printf("\n");
(gdb) l 29
24        return 0;
25    }
26   
27    long func(int n)
28    {
29        long sum = 0;
30        int i;
31        for (i=1; i<=n; ++i)
32        {
33            sum += i;

(gdb) r //Start debugging run, stop at the first breakpoint
Starting program: /home/mickole/ctest/05gdb/simple
Entering main ...

Breakpoint 1, main (argc=1, argv=0xbffff3f4) at simple.c:10
10        for (i=0; i<argc; ++i)
(gdb) step //Single step debugging
12            printf("%s ", argv[i]);
(gdb) s
10        for (i=0; i<argc; ++i)
(gdb)
14        printf("\n");
(gdb)
/home/mickole/ctest/05gdb/simple
15        long result = 0;
(gdb)
16        for (i=1; i<=100; ++i)
(gdb)
18            result += i;
(gdb)
16        for (i=1; i<=100; ++i)
(GDB) p I // print I variable value print I
$1 = 1
(gdb) s
18            result += i;
(gdb)
16        for (i=1; i<=100; ++i)
(gdb)
18            result += i;
(gdb) p i
$2 = 3

(gdb) until //If you do not want to continue the cycle available until out of the loop, here to exit the for loop
20        printf("result[1-100] = %ld\n", result);
(gdb) l 20
15        long result = 0;
16        for (i=1; i<=100; ++i)
17        {
18            result += i;
19        }
20        printf("result[1-100] = %ld\n", result);
21        printf("result[1-10] = %ld\n", func(10));
22   
23        printf("Exiting main ...\n");
24        return 0;

(gdb) p result //Out of print the value of result cycle
$4 = 5050
(GDB) s // continue to step tracking
result[1-100] = 5050
21        printf("result[1-10] = %ld\n", func(10));
(GDB) c//continue to continue tracking, jump directly to the next breakpoint
Continuing.

Breakpoint 2, func (n=10) at simple.c:29
29        long sum = 0;
(gdb) l 29
24        return 0;
25    }
26   
27    long func(int n)
28    {
29        long sum = 0;
30        int i;
31        for (i=1; i<=n; ++i)
32        {
33            sum += i;
(gdb) s //Step tracking
31        for (i=1; i<=n; ++i)
(gdb)
33            sum += i;
(gdb)
31        for (i=1; i<=n; ++i)
(gdb)
33            sum += i;
(gdb)
31        for (i=1; i<=n; ++i)
(gdb)
33            sum += i;
(gdb)
31        for (i=1; i<=n; ++i)
(gdb) p sum
$5 = 6
(gdb) s
33            sum += i;
(gdb)
31        for (i=1; i<=n; ++i)
(GDB) finish // if you want to jump out the function, use finish
Run till exit from #0  func (n=10) at simple.c:31
0x080484e1 in main (argc=1, argv=0xbffff3f4) at simple.c:21
21        printf("result[1-10] = %ld\n", func(10));
Value returned is $6 = 55
(gdb) s
result[1-10] = 55
23        printf("Exiting main ...\n");
(gdb) s
Exiting main ...
24        return 0;
(GDB) C // continue tracking, until the normal end
Continuing.
[Inferior 1 (process 4450) exited normally]

(gdb) quit //Exit debug
mickole@test:~/ctest/05gdb$

The following is a single command parsing:

run: Start debugging, we can run the required parameters, such as:

mickole@test:~/ctest/05gdb$ gdb simple
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
Copyright (C) 2012 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 "i686-linux-gnu".
For bug reporting instructions, please see:
<>...
Reading symbols from /home/mickole/ctest/05gdb/simple...done.
(gdb) r a b c
Starting program: /home/mickole/ctest/05gdb/simple a b c
Entering main ...
/home/mickole/ctest/05gdb/simple a b c
result[1-100] = 5050
result[1-10] = 55
Exiting main ...
[Inferior 1 (process 4476) exited normally]
(gdb)

 

list: Lists the source code:

The list default lists the top ten lines

The list 10 lists the specified row

list 2,10 list 2 to 10 lines

list ,15 lists the fifteenth line contains fifteenth rows in front of ten elements, such as:

(gdb) l ,15
6    int main(int argc, char *argv[])
7    {
8        printf("Entering main ...\n");
9        int i;
10        for (i=0; i<argc; ++i)
11        {
12            printf("%s ", argv[i]);
13        }
14        printf("\n");
15        long result = 0;

 

List Main: list specifies the first ten lines of the function, such as

(gdb) l main
2    #include <stdlib.h>
3   
4    long func(int n);
5   
6    int main(int argc, char *argv[])
7    {
8        printf("Entering main ...\n");
9        int i;
10        for (i=0; i<argc; ++i)
11        {

list hello.c:func  Function: Specifies the source code to check in the file specified

 

Set a breakpoint and view the breakpoint:

Break line

break fun

Break file: line

break file:fun

break if <condition> - the conditions established procedures to stop.

info break(i b) View breakpoints

Watch expr - a expr value changes, the program stops.

Delete n - deletes the breakpoint, such as:

(gdb) i b
Num     Type           Disp Enb Address    What
1       breakpoint     keep y   0x08048459 in main at simple.c:10
2       breakpoint     keep y   0x0804850b in func at simple.c:29
(gdb) d 1
(gdb) d 2

 

Single step debugging:

continue(c) Run to the next breakpoint

step(s) - step tracking, into function, similar to the VC step in

next(n) - step tracking, do not enter the function, similar to the VC step out

- finish program, until the current function returns. And print function returns when the stack address and return values and parameters and other information.

Until - when tired in a loop body step tracking, the command to run the program until exit the loop body.

 

GDB commands commonly used summary:

QQ截图20130929161119

 

Now use the GDB view the runtime data:

Print - view the values of variables

Ptype - view type

Print array - view array

print arr1  - to view the entire array element

print arr[2] - look at the specified array element

print &arr1[2] - see specified subscript elements.

print &Arr1 - view array first address

print *array@Len - view dynamic memory

Print x=5 - change the runtime data

 

GDB debugging is wrong:

Duan Cuowu is due to visit illegal address generated an error.

Example:

mickole@test:~/ctest/06gdb$ gdb bugging
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
Copyright (C) 2012 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 "i686-linux-gnu".
For bug reporting instructions, please see:
<>...
Reading symbols from /home/mickole/ctest/06gdb/bugging...done.
(gdb) r //If some error program, the best way is to directly run position, it will automatically stop at the wrong place
Starting program: /home/mickole/ctest/06gdb/bugging

Program received signal SIGSEGV, Segmentation fault.
0x080483c4 in segfault () at bugging.c:7
7        *p = 100;
(gdb) bt  //The stack trace of
#0  0x080483c4 in segfault () at bugging.c:7
#1  0x080483d4 in main () at bugging.c:12
(GDB) L 7 to seventh line / location
2    #include <stdlib.h>
3   
4    void segfault()
5    {
6        int *p = NULL;
7        *p = 100;
8    }
9   
10    int main(void)
11    {
(gdb)

 

Debugging using core file:

1,The core file

When the program crashes, will generally produce a file called core file. The core file is recorded when a program crashes the memory mapping, and joined the debugging information.

Process generated core file is called core dump

2,Generate core file

Ulimit -c core-dump state that will generate the core file, such as:

mickole@test:~/ctest/06gdb$ ulimit -a
core file size          (blocks, -c) 0 //The block size is 0, said it would not generate core files
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 7895
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 7895
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
mickole@test:~/ctest/06gdb$ ulimit -c
0

Mickole@test:~/ctest/06gdb$ulimit -c 1024 // core file block size
mickole@test:~/ctest/06gdb$ ulimit -c
1024

Ulimit -c digital (e.g.: ulimit -c 1024)

ulimit -c unlimited

3,GDB files with the core debugging

The GDB file core file

bt

 

Example:

mickole@test:~/ctest/06gdb$ ./bugging
Segmentation fault (core dumped)
mickole@test:~/ctest/06gdb$ ls
bugging  bugging.c  core  Makefile  reverse  reverse.c  simple  simple.c

The above error occurred paragraph, create the core files, are now using core debug files:

mickole@test:~/ctest/06gdb$ gdb bugging core
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
Copyright (C) 2012 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 "i686-linux-gnu".
For bug reporting instructions, please see:
<>...
Reading symbols from /home/mickole/ctest/06gdb/bugging...done.
[New LWP 4582]

warning: Can't read pathname for load map: Input/output error.
Core was generated by `./bugging'.
Program terminated with signal 11, Segmentation fault.
#0  0x080483c4 in segfault () at bugging.c:7
7        *p = 100;
(gdb) bt
#0  0x080483c4 in segfault () at bugging.c:7
#1  0x080483d4 in main () at bugging.c:12
(gdb) l 7
2    #include <stdlib.h>
3   
4    void segfault()
5    {
6        int *p = NULL;
7        *p = 100;
8    }
9   
10    int main(void)
11    {
(gdb)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Parker at November 09, 2013 - 12:40 AM