一、gdb常用命令
二、GDB與多進程
#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
int main()
{
pid_t id = fork();
if(id < 0 )
{
perror("fork");
exit(-1);
}else if(id == 0)
{
printf("child id is %d, my father id is %d\n", getpid(), getppid());
}else
{
sleep(1);
printf("father id id %d\n", getpid());
wait(NULL);
}
return 0;
}
默認設置下,在調試多進程程序時GDB只會調用主進程,但是GDB(>V7.0)支持多進程的分別與同步調試。即GDB支持同時調試多個進程。只需要設置follow-fork-mode(默認爲 parent)和detach-on-fork(默認爲:on)即可。
follow-fork-mode detach-on-fork 說明
parent on 只調試主進程(GDB默認)
child on 只調試子進程
parent off 同時調試兩個進程,gdb跟主進程,子進程block(阻塞)在fork位置child off 同時調試兩個進程,gdb跟子進程,主進程block在fork位置
設置方法: set follow-fork-mode[parent|child] set detach-on-fork[on|off]
顯示:show follow-fork-mode show detach-on-fork
查詢正在調試的進程:info inferiors
顯示GDB調試的所有inferior,GDB爲他們分配ID。其中帶*的進程是正在調試的進程。
(GDB將每一個被調試程序的執行狀態記錄在一個名爲inferior的結構中。一般情況下一個inferior對應一個進程,每一個inferior都有自己的地址空間。inferior有時候會在進程沒有啓動時就存在)
切換調試的進程:inferior <inferior number>
通過該指令可以切換到ID爲number的inferior進行調試。
調加新的調試進程:add-inferior[-copies n][-exec executable]
可以用file+executable來分配給inferior可執行文件。+增加n個inferior並執行程序爲executable。如果不指定n只增加一個inferior。如果不指定executable,則執行程序留空,增加後可使用file命令重新指定執行程序。這時候創建的inferior其關聯的進程並沒啓動。刪除一個infnum 的inferior:remove-inferior infnum (如果inferior正在運行,則不能刪除,刪除之間需先kill或detach掉該inferior)
detach掉infnum的inferior:detach inferior infnum注意(inferior仍然存在,可以用run等命令執行)
kill 掉infnum的inferior:kill inferior infnum 注意(inferior仍然存在,可以用run等命令執行)
cfc:/app/tmp>gcc some.c -o some -g
cfc:/app/tmp>gdb some
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-90.el6)
Copyright (C) 2010 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-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /app/tmp/some...done.
(gdb) set follow-fork-mode
Requires an argument. Valid arguments are child, parent.
(gdb) set follow-fork-mode child
(gdb) set detach-on-fork off
(gdb) show follow-fork-mode
Debugger response to a program call of fork or vfork is "child".
(gdb) show detach-on-fork
Whether gdb will detach the child of a fork is off.
(gdb) info inferiors
Num Description Executable
* 1 <null> /app/tmp/some
(gdb) break some.c:14
Breakpoint 1 at 0x4006e5: file some.c, line 14.
(gdb) add-inferiors -copies 3 -exec /app/tmp/some
Undefined command: "add-inferiors". Try "help".
(gdb) add-inferior -copies 3 -exec /app/tmp/some
Added inferior 2
Reading symbols from /app/tmp/some...done.
Added inferior 3
Reading symbols from /app/tmp/some...done.
Added inferior 4
Reading symbols from /app/tmp/some...done.
(gdb) info inferiors
Num Description Executable
4 <null> /app/tmp/some
3 <null> /app/tmp/some
2 <null> /app/tmp/some
* 1 <null> /app/tmp/some
(gdb) run
Starting program: /app/tmp/some
[New process 16964]
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.192.el6.x86_64
[Switching to process 16964]
Breakpoint 1, main () at some.c:14
14 printf("child id is %d, my father id is %d\n", getpid(), getppid());
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.192.el6.x86_64
(gdb) n
child id is 16964, my father id is 16961
21 return 0;
(gdb) info inferiors
Num Description Executable
* 5 process 16964 /app/tmp/some
4 <null> /app/tmp/some
3 <null> /app/tmp/some
2 <null> /app/tmp/some
1 process 16961 /app/tmp/some
(gdb) remove inferiors 5
No symbol "inferiors" in current context.
(gdb) remove-inferiors 5
Undefined command: "remove-inferiors". Try "help".
(gdb) remove-inferiors infnum
Undefined command: "remove-inferiors". Try "help".
(gdb) detach inferior 5
Detaching from program: /app/tmp/some, process 16964
(gdb) info inferiors
Num Description Executable
* 5 <null> /app/tmp/some
4 <null> /app/tmp/some
3 <null> /app/tmp/some
2 <null> /app/tmp/some
1 process 16961 /app/tmp/some
(gdb) kill inferior 5
Inferior has no threads.
(gdb) inferior 2
[Switching to inferior 2 [process 0] (/app/tmp/some)]
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y <MULTIPLE>
breakpoint already hit 1 time
1.1 y 0x00000000004006e5 in main at some.c:14 inf 5
1.2 y 0x00000000004006e5 in main at some.c:14 inf 4
1.3 y 0x00000000004006e5 in main at some.c:14 inf 3
1.4 y 0x00000000004006e5 in main at some.c:14 inf 2
1.5 y 0x00000000004006e5 in main at some.c:14 inf 1
(gdb) info inferiors
Num Description Executable
5 <null> /app/tmp/some
4 <null> /app/tmp/some
3 <null> /app/tmp/some
* 2 <null> /app/tmp/some
1 process 16961 /app/tmp/some
(gdb) run
Starting program: /app/tmp/some
[New process 17010]
[Switching to process 17010]
Breakpoint 1, main () at some.c:14
14 printf("child id is %d, my father id is %d\n", getpid(), getppid());
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.192.el6.x86_64
(gdb) info inferiors
Num Description Executable
* 6 process 17010 /app/tmp/some
4 <null> /app/tmp/some
3 <null> /app/tmp/some
2 process 17009 /app/tmp/some
1 process 16961 /app/tmp/some
(gdb) kill inferior 6
(gdb) info inferiors
Num Description Executable
* 6 <null> /app/tmp/some
4 <null> /app/tmp/some
3 <null> /app/tmp/some
2 process 17009 /app/tmp/some
1 process 16961 /app/tmp/some
(gdb) run
Starting program: /app/tmp/some
[New process 17012]
[Switching to process 17012]
Breakpoint 1, main () at some.c:14
14 printf("child id is %d, my father id is %d\n", getpid(), getppid());
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.192.el6.x86_64
(gdb) info inferiors
Num Description Executable
* 7 process 17012 /app/tmp/some
6 process 17011 /app/tmp/some
4 <null> /app/tmp/some
3 <null> /app/tmp/some
2 process 17009 /app/tmp/some
1 process 16961 /app/tmp/some
(gdb) run
The program being debugged has been started already.
Start it from the beginning? (y or n) n
Program not restarted.
(gdb) n
child id is 17012, my father id is 17011
21 return 0;
(gdb) n
22 }
(gdb) n
0x0000003d8a21ed1d in __libc_start_main () from /lib64/libc.so.6
(gdb) n
Single stepping until exit from function __libc_start_main,
which has no line number information.
Program exited normally.
(gdb)
The program is not being run.
(gdb)
The program is not being run.
(gdb) info inferiors
Num Description Executable
* 7 <null> /app/tmp/some
6 process 17011 /app/tmp/some
4 <null> /app/tmp/some
3 <null> /app/tmp/some
2 process 17009 /app/tmp/some
1 process 16961 /app/tmp/some
(gdb) inferior 2
[Switching to inferior 2 [process 17009] (/app/tmp/some)]
[Switching to thread 3 (process 17009)]
#0 0x0000003d8a2accbd in fork () from /lib64/libc.so.6
(gdb) inferior 3
[Switching to inferior 3 [process 0] (/app/tmp/some)]
(gdb) info inferiors
Num Description Executable
7 <null> /app/tmp/some
6 process 17011 /app/tmp/some
4 <null> /app/tmp/some
* 3 <null> /app/tmp/some
2 process 17009 /app/tmp/some
1 process 16961 /app/tmp/some
(gdb) run
Starting program: /app/tmp/some
[New process 17036]
[Switching to process 17036]
Breakpoint 1, main () at some.c:14
14 printf("child id is %d, my father id is %d\n", getpid(), getppid());
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.192.el6.x86_64
(gdb) info inferiors
Num Description Executable
* 8 process 17036 /app/tmp/some
6 process 17011 /app/tmp/some
4 <null> /app/tmp/some
3 process 17035 /app/tmp/some
2 process 17009 /app/tmp/some
1 process 16961 /app/tmp/some
(gdb) s
child id is 17036, my father id is 17035
21 return 0;
(gdb) s
22 }
(gdb) s
0x0000003d8a21ed1d in __libc_start_main () from /lib64/libc.so.6
(gdb) s
Single stepping until exit from function __libc_start_main,
which has no line number information.
Program exited normally.
(gdb) info inferiors
Num Description Executable
* 8 <null> /app/tmp/some
6 process 17011 /app/tmp/some
4 <null> /app/tmp/some
3 process 17035 /app/tmp/some
2 process 17009 /app/tmp/some
1 process 16961 /app/tmp/some
(gdb) kill inferior 2
(gdb) info inferiors
Num Description Executable
8 <null> /app/tmp/some
6 process 17011 /app/tmp/some
4 <null> /app/tmp/some
3 process 17035 /app/tmp/some
* 2 <null> /app/tmp/some
1 process 16961 /app/tmp/some
(gdb) kill inferior 3
(gdb) kill inferior 1
(gdb) info inferiors
Num Description Executable
8 <null> /app/tmp/some
6 process 17011 /app/tmp/some
4 <null> /app/tmp/some
3 <null> /app/tmp/some
2 <null> /app/tmp/some
* 1 <null> /app/tmp/some
(gdb) remove-inferior 2
(gdb) info inferiors
Num Description Executable
8 <null> /app/tmp/some
6 process 17011 /app/tmp/some
4 <null> /app/tmp/some
3 <null> /app/tmp/some
* 1 <null> /app/tmp/some
(gdb)