GDBÊÇGNU¿ªÔ´×éÖ¯·¢²¼µÄÒ»¸öÇ¿´óµÄUNIXϵijÌÐòµ÷ÊÔ¹¤¾ß¡£»òÐí£¬¸÷λ±È½Ïϲ»¶ÄÇÖÖͼÐνçÃ淽ʽµÄ£¬ÏñVC¡¢BCBµÈIDEµÄµ÷ÊÔ£¬
µ«Èç¹ûÄãÊÇÔÚUNIXƽ̨ÏÂ×öÈí¼þ£¬Äã»á·¢ÏÖGDBÕâ¸öµ÷ÊÔ¹¤¾ßÓбÈVC¡¢BCBµÄͼÐλ¯µ÷ÊÔÆ÷¸üÇ¿´óµÄ¹¦ÄÜ¡£
Ëùν¡°´çÓÐËù³¤£¬³ßÓÐËù¶Ì¡±¾ÍÊÇÕâ¸öµÀÀí¡£
Ò»°ãÀ´Ëµ£¬GDBÖ÷Òª°ïæÄãÍê³ÉÏÂÃæËĸö·½ÃæµÄ¹¦ÄÜ£º
1¡¢Æô¶¯ÄãµÄ³ÌÐò£¬¿ÉÒÔ°´ÕÕÄãµÄ×Ô¶¨ÒåµÄÒªÇóËæÐÄËùÓûµÄÔËÐгÌÐò¡£
2¡¢¿ÉÈñ»µ÷ÊԵijÌÐòÔÚÄãËùÖ¸¶¨µÄµ÷ÖõĶϵ㴦ͣס¡££¨¶Ïµã¿ÉÒÔÊÇÌõ¼þ±í´ïʽ£©
3¡¢µ±³ÌÐò±»Í£×¡Ê±£¬¿ÉÒÔ¼ì²é´ËʱÄãµÄ³ÌÐòÖÐËù·¢ÉúµÄÊ¡£
4¡¢¶¯Ì¬µÄ¸Ä±äÄã³ÌÐòµÄÖ´Ðл·¾³¡£
´ÓÉÏÃæ¿´À´£¬GDBºÍÒ»°ãµÄµ÷ÊÔ¹¤¾ßûÓÐʲôÁ½Ñù£¬»ù±¾ÉÏÒ²ÊÇÍê³ÉÕâЩ¹¦ÄÜ£¬²»¹ýÔÚϸ½ÚÉÏ£¬Äã»á·¢ÏÖGDBÕâ¸öµ÷ÊÔ¹¤¾ßµÄÇ¿´ó£¬
´ó¼Ò¿ÉÄܱȽÏÏ°¹ßÁËͼÐλ¯µÄµ÷ÊÔ¹¤¾ß£¬µ«ÓÐʱºò£¬ÃüÁîÐеĵ÷ÊÔ¹¤¾ßÈ´ÓÐ×ÅͼÐλ¯¹¤¾ßËù²»ÄÜÍê³ÉµÄ¹¦ÄÜ¡£ÈÃÎÒÃÇÒ»Ò»¿´À´¡£
Ò»¸öµ÷ÊÔʾÀý
Ô´³ÌÐò£ºtst.c
1 #include
2
3 int func(int n)
4 {
5 int sum=0,i;
6 for(i=0; i
8 sum+=i;
9 }
10 return sum;
11 }
12
13
14 main()
15 {
16 int i;
17 long result = 0;
18 for(i=1; i<=100; i++)
19 {
20 result += i;
21 }
22
23 printf("result[1-100] = %d \n", result );
24 printf("result[1-250] = %d \n", func(250) );
25 }
±àÒëÉú³ÉÖ´ÐÐÎļþ£º£¨LinuxÏ£©
hchen/test> gcc -g tst.c -o tst #×¢Òâ±ØÐ븽´ø-g²ÎÊý£¬Çë²Î¿¼gccʹÓÃÊÖ²áÀ´Àí½âÕâ¸ö²ÎÊý
ʹÓÃGDBµ÷ÊÔ£º
hchen/test> gdb tst <---------- Æô¶¯GDB
GNU gdb 5.1.1
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-suse-linux"...
(gdb) l 1 <-------------------- lÃüÁîÏ൱ÓÚlist£¬´ÓµÚÒ»ÐпªÊ¼Àý³öÔÂë¡£
1 #include
2
3 int func(int n)
4 {
5 int sum=0,i;
6 for(i=0; i
8 sum+=i;
9 }
10 return sum;
(gdb) <-------------------- Ö±½Ó»Ø³µ±íʾ£¬Öظ´ÉÏÒ»´ÎÃüÁî
11 }
12
13
14 main()
15 {
16 int i;
17 long result = 0;
18 for(i=1; i<=100; i++)
19 {
20 result += i;
(gdb) break 16 <-------------------- ÉèÖöϵ㣬ÔÚÔ´³ÌÐòµÚ16Ðд¦¡£
Breakpoint 1 at 0x8048496: file tst.c, line 16.
(gdb) break func <-------------------- ÉèÖöϵ㣬ÔÚº¯Êýfunc()Èë¿Ú´¦¡£
Breakpoint 2 at 0x8048456: file tst.c, line 5.
(gdb) info break <-------------------- ²é¿´¶ÏµãÐÅÏ¢¡£
Num Type Disp Enb Address What
1 breakpoint keep y 0x08048496 in main at tst.c:16
2 breakpoint keep y 0x08048456 in func at tst.c:5
(gdb) r <--------------------- ÔËÐгÌÐò£¬runÃüÁî¼òд
Starting program: /home/hchen/test/tst
Breakpoint 1, main () at tst.c:17 <---------- Ôڶϵ㴦ͣס¡£
17 long result = 0;
(gdb) n <--------------------- µ¥ÌõÓï¾äÖ´ÐУ¬nextÃüÁî¼òд¡£
18 for(i=1; i<=100; i++)
(gdb) n
20 result += i;
(gdb) n
18 for(i=1; i<=100; i++)
(gdb) n
20 result += i;
(gdb) c <--------------------- ¼ÌÐøÔËÐгÌÐò£¬continueÃüÁî¼òд¡£
Continuing.
result[1-100] = 5050 <----------³ÌÐòÊä³ö¡£
Breakpoint 2, func (n=250) at tst.c:5
5 int sum=0,i;
(gdb) n
6 for(i=1; i<=n; i++)
(gdb) p i <--------------------- ´òÓ¡±äÁ¿iµÄÖµ£¬printÃüÁî¼òд¡£
$1 = 134513808
(gdb) n
8 sum+=i;
(gdb) n
6 for(i=1; i<=n; i++)
(gdb) p sum
$2 = 1
(gdb) n
8 sum+=i;
(gdb) p i
$3 = 2
(gdb) n
6 for(i=1; i<=n; i++)
(gdb) p sum
$4 = 3
(gdb) bt <--------------------- ²é¿´º¯Êý¶ÑÕ»¡£
#0 func (n=250) at tst.c:5
#1 0x080484e4 in main () at tst.c:24
#2 0x400409ed in __libc_start_main () from /lib/libc.so.6
(gdb) finish <--------------------- Í˳öº¯Êý¡£
Run till exit from #0 func (n=250) at tst.c:5
0x080484e4 in main () at tst.c:24
24 printf("result[1-250] = %d \n", func(250) );
Value returned is $6 = 31375
(gdb) c <--------------------- ¼ÌÐøÔËÐС£
Continuing.
result[1-250] = 31375 <----------³ÌÐòÊä³ö¡£
Program exited with code 027. <--------³ÌÐòÍ˳ö£¬µ÷ÊÔ½áÊø¡£
(gdb) q <--------------------- Í˳ögdb¡£
hchen/test>
ºÃÁË£¬ÓÐÁËÒÔÉϵĸÐÐÔÈÏʶ£¬»¹ÊÇÈÃÎÒÃÇÀ´ÏµÍ³µØÈÏʶһÏÂgdb°É¡£
<¶þ>ʹÓÃGDB
Ò»°ãÀ´ËµGDBÖ÷Òªµ÷ÊÔµÄÊÇC/C++µÄ³ÌÐò¡£Òªµ÷ÊÔC/C++µÄ³ÌÐò£¬Ê×ÏÈÔÚ±àÒëʱ£¬ÎÒÃDZØÐëÒª°Ñµ÷ÊÔÐÅÏ¢¼Óµ½¿ÉÖ´ÐÐÎļþÖС£
ʹÓñàÒëÆ÷£¨cc/gcc/g++£©µÄ -g ²ÎÊý¿ÉÒÔ×öµ½ÕâÒ»µã¡£È磺
> gcc -g hello.c -o hello
> g++ -g hello.cpp -o hello
Èç¹ûûÓÐ-g£¬Ä㽫¿´²»¼û³ÌÐòµÄº¯ÊýÃû¡¢±äÁ¿Ãû£¬Ëù´úÌæµÄÈ«ÊÇÔËÐÐʱµÄÄÚ´æµØÖ·¡£µ±ÄãÓÃ-g°Ñµ÷ÊÔÐÅÏ¢¼ÓÈëÖ®ºó£¬
²¢³É¹¦±àÒëÄ¿±ê´úÂëÒÔºó£¬ÈÃÎÒÃÇÀ´¿´¿´ÈçºÎÓÃgdbÀ´µ÷ÊÔËû¡£
Æô¶¯GDBµÄ·½·¨ÓÐÒÔϼ¸ÖÖ£º
1¡¢gdb
programÒ²¾ÍÊÇÄãµÄÖ´ÐÐÎļþ£¬Ò»°ãÔÚµ±È»Ä¿Â¼Ï¡£
2¡¢gdb
ÓÃgdbͬʱµ÷ÊÔÒ»¸öÔËÐгÌÐòºÍcoreÎļþ£¬coreÊdzÌÐò·Ç·¨Ö´Ðкócore dumpºó²úÉúµÄÎļþ¡£
3¡¢gdb
Èç¹ûÄãµÄ³ÌÐòÊÇÒ»¸ö·þÎñ³ÌÐò£¬ÄÇôÄã¿ÉÒÔÖ¸¶¨Õâ¸ö·þÎñ³ÌÐòÔËÐÐʱµÄ½ø³ÌID¡£gdb»á×Ô¶¯attachÉÏÈ¥£¬²¢µ÷ÊÔËû¡£
programÓ¦¸ÃÔÚPATH»·¾³±äÁ¿ÖÐËÑË÷µÃµ½¡£
GDBÆô¶¯Ê±£¬¿ÉÒÔ¼ÓÉÏһЩGDBµÄÆô¶¯¿ª¹Ø£¬ÏêϸµÄ¿ª¹Ø¿ÉÒÔÓÃgdb -help²é¿´¡£ÎÒÔÚÏÂÃæÖ»Àý¾ÙһЩ±È½Ï³£ÓõIJÎÊý£º
-symbols
-s
´ÓÖ¸¶¨ÎļþÖжÁÈ¡·ûºÅ±í¡£
-se file
´ÓÖ¸¶¨ÎļþÖжÁÈ¡·ûºÅ±íÐÅÏ¢£¬²¢°ÑËûÓÃÔÚ¿ÉÖ´ÐÐÎļþÖС£
-core
-c
µ÷ÊÔʱcore dumpµÄcoreÎļþ¡£
-directory
-d
¼ÓÈëÒ»¸öÔ´ÎļþµÄËÑË÷·¾¶¡£Ä¬ÈÏËÑË÷·¾¶ÊÇ»·¾³±äÁ¿ÖÐPATHËù¶¨ÒåµÄ·¾¶¡£
Æô¶¯gdbºó£¬¾ÍÄã±»´øÈëgdbµÄµ÷ÊÔ»·¾³ÖУ¬¾Í¿ÉÒÔʹÓÃgdbµÄÃüÁʼµ÷ÊÔ³ÌÐòÁË£¬gdbµÄÃüÁî¿ÉÒÔʹÓÃhelpÃüÁîÀ´²é¿´£¬ÈçÏÂËùʾ£º
/home/hchen> gdb
GNU gdb 5.1.1
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-suse-linux".
(gdb) help
List of classes of commands:
aliases -- Aliases of other commands
breakpoints -- Making program stop at certain points
data -- Examining data
files -- Specifying and examining files
internals -- Maintenance commands
obscure -- Obscure features
running -- Running the program
stack -- Examining the stack
status -- Status inquiries
support -- Support facilities
tracepoints -- Tracing of program execution without stopping the program
user-defined -- User-defined commands
Type "help" followed by a class name for a list of commands in that class.
Type "help" followed by command name for full documentation.
Command name abbreviations are allowed if unambiguous.
(gdb)
gdbµÄÃüÁîºÜ¶à£¬gdb°ÑÖ®·Ö³ÉÐí¶à¸öÖÖÀà¡£helpÃüÁîÖ»ÊÇÀý³ögdbµÄÃüÁîÖÖÀ࣬Èç¹ûÒª¿´ÖÖÀàÖеÄÃüÁ
¿ÉÒÔʹÓÃhelp < class> ÃüÁÈ磺help breakpoints£¬²é¿´ÉèÖöϵãµÄËùÓÐÃüÁî¡£Ò²¿ÉÒÔÖ±½Óhelp
gdbÖУ¬ÊäÈëÃüÁîʱ£¬¿ÉÒÔ²»ÓôòÈ«ÃüÁֻÓôòÃüÁîµÄÇ°¼¸¸ö×Ö·û¾Í¿ÉÒÔÁË£¬µ±È»£¬
ÃüÁîµÄÇ°¼¸¸ö×Ö·ûÓ¦¸ÃÒª±êÖ¾×ÅÒ»¸öΨһµÄÃüÁÔÚLinuxÏ£¬Äã¿ÉÒÔÇû÷Á½´ÎTAB¼üÀ´²¹ÆëÃüÁîµÄÈ«³Æ£¬
Èç¹ûÓÐÖظ´µÄ£¬ÄÇôgdb»á°ÑÆäÀý³öÀ´¡£
ʾÀýÒ»£ºÔÚ½øÈ뺯Êýfuncʱ£¬ÉèÖÃÒ»¸ö¶Ïµã¡£¿ÉÒÔÇÃÈëbreak func£¬»òÊÇÖ±½Ó¾ÍÊÇb func
(gdb) b func
Breakpoint 1 at 0x8048458: file hello.c, line 10.
ʾÀý¶þ£ºÇÃÈëb°´Á½´ÎTAB¼ü£¬Äã»á¿´µ½ËùÓÐb´òÍ·µÄÃüÁ
(gdb) b
backtrace break bt
(gdb)
ʾÀýÈý£ºÖ»¼ÇµÃº¯ÊýµÄǰ׺£¬¿ÉÒÔÕâÑù£º
(gdb) b make_ <°´TAB¼ü>
£¨ÔÙ°´ÏÂÒ»´ÎTAB¼ü£¬Äã»á¿´µ½:£©
make_a_section_from_file make_environ
make_abs_section make_function_type
make_blockvector make_pointer_type
make_cleanup make_reference_type
make_command make_symbol_completion_list
(gdb) b make_
GDB°ÑËùÓÐmake¿ªÍ·µÄº¯ÊýÈ«²¿Àý³öÀ´¸øÄã²é¿´¡£
ʾÀýËÄ£ºµ÷ÊÔC++µÄ³ÌÐòʱ£¬ÓпÉÒÔº¯ÊýÃûÒ»Ñù¡£È磺
(gdb) b 'bubble( M-?
bubble(double,double) bubble(int,int)
(gdb) b 'bubble(
Äã¿ÉÒԲ鿴µ½C++ÖеÄËùÓеÄÖØÔغ¯Êý¼°²ÎÊý¡££¨×¢£ºM-?ºÍ¡°°´Á½´ÎTAB¼ü¡±ÊÇÒ»¸öÒâ˼£©
ÒªÍ˳ögdbʱ£¬Ö»Ó÷¢quit»òÃüÁî¼ò³Æq¾ÍÐÐÁË¡£
GDBÖÐÔËÐÐUNIXµÄshell³ÌÐò
ÔÚgdb»·¾³ÖУ¬Äã¿ÉÒÔÖ´ÐÐUNIXµÄshellµÄÃüÁʹÓÃgdbµÄshellÃüÁîÀ´Íê³É£º
shell
µ÷ÓÃUNIXµÄshellÀ´Ö´ÐÐ
»¹ÓÐÒ»¸ögdbÃüÁîÊÇmake£º
make
¿ÉÒÔÔÚgdbÖÐÖ´ÐÐmakeÃüÁîÀ´ÖØÐÂbuild×Ô¼ºµÄ³ÌÐò¡£Õâ¸öÃüÁîµÈ¼ÛÓÚ¡°shell make
ÔÚGDBÖÐÔËÐгÌÐò
µ±ÒÔgdb
ÔÚgdbÖУ¬ÔËÐгÌÐòʹÓÃr»òÊÇrunÃüÁî¡£³ÌÐòµÄÔËÐУ¬ÄãÓпÉÄÜÐèÒªÉèÖÃÏÂÃæËÄ·½ÃæµÄÊ¡£
1¡¢³ÌÐòÔËÐвÎÊý¡£
set args ¿ÉÖ¸¶¨ÔËÐÐʱ²ÎÊý¡££¨È磺set args 10 20 30 40 50£©
show args ÃüÁî¿ÉÒԲ鿴ÉèÖúõÄÔËÐвÎÊý¡£
2¡¢ÔËÐл·¾³¡£
path
show paths ²é¿´³ÌÐòµÄÔËÐз¾¶¡£
set environment varname [=value] ÉèÖû·¾³±äÁ¿¡£È磺set env USER=hchen
show environment [varname] ²é¿´»·¾³±äÁ¿¡£
3¡¢¹¤×÷Ŀ¼¡£
cd
pwd ÏÔʾµ±Ç°µÄËùÔÚĿ¼¡£
4¡¢³ÌÐòµÄÊäÈëÊä³ö¡£
info terminal ÏÔʾÄã³ÌÐòÓõ½µÄÖն˵Äģʽ¡£
ʹÓÃÖض¨Ïò¿ØÖƳÌÐòÊä³ö¡£È磺run > outfile
ttyÃüÁî¿ÉÒÔָдÊäÈëÊä³öµÄÖÕ¶ËÉ豸¡£È磺tty /dev/ttyb
µ÷ÊÔÒÑÔËÐеijÌÐò Á½ÖÖ·½·¨£º
1¡¢ÔÚUNIXÏÂÓÃps²é¿´ÕýÔÚÔËÐеijÌÐòµÄPID£¨½ø³ÌID£©£¬È»ºóÓÃgdb
2¡¢ÏÈÓÃgdb
ÔÝÍ£ / »Ö¸´³ÌÐòÔËÐÐ
µ÷ÊÔ³ÌÐòÖУ¬ÔÝÍ£³ÌÐòÔËÐÐÊDZØÐëµÄ£¬GDB¿ÉÒÔ·½±ãµØÔÝÍ£³ÌÐòµÄÔËÐС£Äã¿ÉÒÔÉèÖóÌÐòµÄÔÚÄÄÐÐͣס£¬ÔÚʲôÌõ¼þÏÂͣס£¬
ÔÚÊÕµ½Ê²Ã´ÐźÅʱͣÍùµÈµÈ¡£ÒÔ±ãÓÚÄã²é¿´ÔËÐÐʱµÄ±äÁ¿£¬ÒÔ¼°ÔËÐÐʱµÄÁ÷³Ì¡£
µ±½ø³Ì±»gdbͣסʱ£¬Äã¿ÉÒÔʹÓÃinfo program À´²é¿´³ÌÐòµÄÊÇ·ñÔÚÔËÐУ¬½ø³ÌºÅ£¬±»ÔÝÍ£µÄÔÒò¡£
ÔÚgdbÖУ¬ÎÒÃÇ¿ÉÒÔÓÐÒÔϼ¸ÖÖÔÝÍ£·½Ê½£º¶Ïµã£¨BreakPoint£©¡¢¹Û²ìµã£¨WatchPoint£©¡¢²¶×½µã£¨CatchPoint£©¡¢
Ðźţ¨Signals£©¡¢Ïß³ÌÍ£Ö¹£¨Thread Stops£©¡£Èç¹ûÒª»Ö¸´³ÌÐòÔËÐУ¬¿ÉÒÔʹÓÃc»òÊÇcontinueÃüÁî¡£
Ò»¡¢ÉèÖöϵ㣨BreakPoint£©
ÎÒÃÇÓÃbreakÃüÁîÀ´ÉèÖöϵ㡣ÕýÃæÓм¸µãÉèÖöϵãµÄ·½·¨£º
break
ÔÚ½øÈëÖ¸¶¨º¯Êýʱͣס¡£C++ÖпÉÒÔʹÓÃclass::function»òfunction(type,type)¸ñʽÀ´Ö¸¶¨º¯ÊýÃû¡£
break
ÔÚÖ¸¶¨ÐкÅͣס¡£
break +offset
break -offset
ÔÚµ±Ç°ÐкŵÄÇ°Ãæ»òºóÃæµÄoffsetÐÐͣס¡£offisetΪ×ÔÈ»Êý¡£
break filename:linenum
ÔÚÔ´ÎļþfilenameµÄlinenumÐд¦Í£×¡¡£
break filename:function
ÔÚÔ´ÎļþfilenameµÄfunctionº¯ÊýµÄÈë¿Ú´¦Í£×¡¡£
break *address
ÔÚ³ÌÐòÔËÐеÄÄÚ´æµØÖ·´¦Í£×¡¡£
break
breakÃüÁîûÓвÎÊýʱ£¬±íʾÔÚÏÂÒ»ÌõÖ¸Áͣס¡£
break ... if
...¿ÉÒÔÊÇÉÏÊöµÄ²ÎÊý£¬condition±íʾÌõ¼þ£¬ÔÚÌõ¼þ³ÉÁ¢Ê±Í£×¡¡£±ÈÈçÔÚÑ»·¾³ÌåÖУ¬¿ÉÒÔÉèÖÃbreak if i=100£¬±íʾµ±iΪ100ʱͣס³ÌÐò¡£
²é¿´¶Ïµãʱ£¬¿ÉʹÓÃinfoÃüÁÈçÏÂËùʾ£º£¨×¢£ºn±íʾ¶ÏµãºÅ£©
info breakpoints [n]
info break [n]
¶þ¡¢ÉèÖù۲ìµã£¨WatchPoint£©
¹Û²ìµãÒ»°ãÀ´¹Û²ìij¸ö±í´ïʽ£¨±äÁ¿Ò²ÊÇÒ»ÖÖ±í´ïʽ£©µÄÖµÊÇ·ñÓб仯ÁË£¬Èç¹ûÓб仯£¬ÂíÉÏͣס³ÌÐò¡£ÎÒÃÇÓÐÏÂÃæµÄ¼¸ÖÖ·½·¨À´ÉèÖù۲ìµã£º
watch
Ϊ±í´ïʽ£¨±äÁ¿£©exprÉèÖÃÒ»¸ö¹Û²ìµã¡£Ò»Á¿±í´ïʽֵÓб仯ʱ£¬ÂíÉÏͣס³ÌÐò¡£
rwatch
µ±±í´ïʽ£¨±äÁ¿£©expr±»¶Áʱ£¬Í£×¡³ÌÐò¡£
awatch
µ±±í´ïʽ£¨±äÁ¿£©µÄÖµ±»¶Á»ò±»Ð´Ê±£¬Í£×¡³ÌÐò¡£
info watchpoints
Áгöµ±Ç°ËùÉèÖÃÁ˵ÄËùÓй۲ìµã¡£
Èý¡¢ÉèÖò¶×½µã£¨CatchPoint£©
Äã¿ÉÉèÖò¶×½µãÀ´²¹×½³ÌÐòÔËÐÐʱµÄһЩʼþ¡£È磺ÔØÈë¹²Ïí¿â£¨¶¯Ì¬Á´½Ó¿â£©»òÊÇC++µÄÒì³£¡£ÉèÖò¶×½µãµÄ¸ñʽΪ£º
catch
µ±event·¢Éúʱ£¬Í£×¡³ÌÐò¡£event¿ÉÒÔÊÇÏÂÃæµÄÄÚÈÝ£º
1¡¢throw Ò»¸öC++Å׳öµÄÒì³£¡££¨throwΪ¹Ø¼ü×Ö£©
2¡¢catch Ò»¸öC++²¶×½µ½µÄÒì³£¡££¨catchΪ¹Ø¼ü×Ö£©
3¡¢exec µ÷ÓÃϵͳµ÷ÓÃexecʱ¡££¨execΪ¹Ø¼ü×Ö£¬Ä¿Ç°´Ë¹¦ÄÜÖ»ÔÚHP-UXÏÂÓÐÓã©
4¡¢fork µ÷ÓÃϵͳµ÷ÓÃforkʱ¡££¨forkΪ¹Ø¼ü×Ö£¬Ä¿Ç°´Ë¹¦ÄÜÖ»ÔÚHP-UXÏÂÓÐÓã©
5¡¢vfork µ÷ÓÃϵͳµ÷ÓÃvforkʱ¡££¨vforkΪ¹Ø¼ü×Ö£¬Ä¿Ç°´Ë¹¦ÄÜÖ»ÔÚHP-UXÏÂÓÐÓã©
6¡¢load »ò load
7¡¢unload »ò unload
tcatch
Ö»ÉèÖÃÒ»´Î²¶×½µã£¬µ±³ÌÐòͣסÒÔºó£¬Ó¦µã±»×Ô¶¯É¾³ý¡£
ËÄ¡¢Î¬»¤Í£Ö¹µã
ÉÏÃæ˵ÁËÈçºÎÉèÖóÌÐòµÄÍ£Ö¹µã£¬GDBÖеÄÍ£Ö¹µãÒ²¾ÍÊÇÉÏÊöµÄÈýÀà¡£ÔÚGDBÖУ¬Èç¹ûÄã¾õµÃÒѶ¨ÒåºÃµÄÍ£Ö¹µãûÓÐÓÃÁË£¬
Äã¿ÉÒÔʹÓÃdelete¡¢clear¡¢disable¡¢enableÕ⼸¸öÃüÁîÀ´½øÐÐά»¤¡£
clear
Çå³ýËùÓеÄÒѶ¨ÒåµÄÍ£Ö¹µã¡£
clear
clear
Çå³ýËùÓÐÉèÖÃÔÚº¯ÊýÉϵÄÍ£Ö¹µã¡£
clear
clear
Çå³ýËùÓÐÉèÖÃÔÚÖ¸¶¨ÐÐÉϵÄÍ£Ö¹µã¡£
delete [breakpoints] [range...]
ɾ³ýÖ¸¶¨µÄ¶Ïµã£¬breakpointsΪ¶ÏµãºÅ¡£Èç¹û²»Ö¸¶¨¶ÏµãºÅ£¬Ôò±íʾɾ³ýËùÓеĶϵ㡣range ±íʾ¶ÏµãºÅµÄ·¶Î§£¨È磺3-7£©¡£Æä¼òдÃüÁîΪd¡£
±Èɾ³ý¸üºÃµÄÒ»ÖÖ·½·¨ÊÇdisableÍ£Ö¹µã£¬disableÁ˵ÄÍ£Ö¹µã£¬GDB²»»áɾ³ý£¬µ±Ä㻹ÐèҪʱ£¬enable¼´¿É£¬¾ÍºÃÏñ»ØÊÕÕ¾Ò»Ñù¡£
disable [breakpoints] [range...]
disableËùÖ¸¶¨µÄÍ£Ö¹µã£¬breakpointsΪֹͣµãºÅ¡£Èç¹ûʲô¶¼²»Ö¸¶¨£¬±íʾdisableËùÓеÄÍ£Ö¹µã¡£¼òдÃüÁîÊÇdis.
enable [breakpoints] [range...]
enableËùÖ¸¶¨µÄÍ£Ö¹µã£¬breakpointsΪֹͣµãºÅ¡£
enable [breakpoints] once range...
enableËùÖ¸¶¨µÄÍ£Ö¹µãÒ»´Î£¬µ±³ÌÐòÍ£Ö¹ºó£¬¸ÃÍ£Ö¹µãÂíÉϱ»GDB×Ô¶¯disable¡£
enable [breakpoints] delete range...
enableËùÖ¸¶¨µÄÍ£Ö¹µãÒ»´Î£¬µ±³ÌÐòÍ£Ö¹ºó£¬¸ÃÍ£Ö¹µãÂíÉϱ»GDB×Ô¶¯É¾³ý¡£
Î塢ֹͣÌõ¼þά»¤
Ç°ÃæÔÚ˵µ½ÉèÖöϵãʱ£¬ÎÒÃÇÌáµ½¹ý¿ÉÒÔÉèÖÃÒ»¸öÌõ¼þ£¬µ±Ìõ¼þ³ÉÁ¢Ê±£¬³ÌÐò×Ô¶¯Í£Ö¹£¬ÕâÊÇÒ»¸ö·Ç³£Ç¿´óµÄ¹¦ÄÜ£¬
ÕâÀÎÒÏëרÃÅ˵˵Õâ¸öÌõ¼þµÄÏà¹Øά»¤ÃüÁî¡£Ò»°ãÀ´Ëµ£¬Îª¶ÏµãÉèÖÃÒ»¸öÌõ¼þ£¬ÎÒÃÇʹÓÃif¹Ø¼ü´Ê£¬ºóÃæ¸úÆä¶ÏµãÌõ¼þ¡£
²¢ÇÒ£¬Ìõ¼þÉèÖúúó£¬ÎÒÃÇ¿ÉÒÔÓÃconditionÃüÁîÀ´Ð޸ĶϵãµÄÌõ¼þ¡££¨Ö»ÓÐbreakºÍwatchÃüÁîÖ§³Öif£¬catchÄ¿Ç°Ôݲ»Ö§³Öif£©
condition
Ð޸ĶϵãºÅΪbnumµÄÍ£Ö¹Ìõ¼þΪexpression¡£
condition
Çå³ý¶ÏµãºÅΪbnumµÄÍ£Ö¹Ìõ¼þ¡£
»¹ÓÐÒ»¸ö±È½ÏÌØÊâµÄά»¤ÃüÁîignore£¬Äã¿ÉÒÔÖ¸¶¨³ÌÐòÔËÐÐʱ£¬ºöÂÔÍ£Ö¹Ìõ¼þ¼¸´Î¡£
ignore
±íʾºöÂԶϵãºÅΪbnumµÄÍ£Ö¹Ìõ¼þcount´Î¡£
Áù¡¢ÎªÍ£Ö¹µãÉ趨ÔËÐÐÃüÁî
ÎÒÃÇ¿ÉÒÔʹÓÃGDBÌṩµÄcommandÃüÁîÀ´ÉèÖÃÍ£Ö¹µãµÄÔËÐÐÃüÁî¡£Ò²¾ÍÊÇ˵£¬µ±ÔËÐеijÌÐòÔÚ±»Í£Ö¹×¡Ê±£¬
ÎÒÃÇ¿ÉÒÔÈÃÆä×Ô¶¯ÔËÐÐһЩ±ðµÄÃüÁÕâºÜÓÐÀûÐÐ×Ô¶¯»¯µ÷ÊÔ¡£¶Ô»ùÓÚGDBµÄ×Ô¶¯»¯µ÷ÊÔÊÇÒ»¸öÇ¿´óµÄÖ§³Ö¡£
commands [bnum]
... command-list ...
end
Ϊ¶ÏµãºÅbnumָдһ¸öÃüÁîÁÐ±í¡£µ±³ÌÐò±»¸Ã¶Ïµãͣסʱ£¬gdb»áÒÀ´ÎÔËÐÐÃüÁîÁбíÖеÄÃüÁî¡£
ÀýÈ磺
break foo if x>0
commands
printf "x is %d\n",x
continue
end
¶ÏµãÉèÖÃÔÚº¯ÊýfooÖУ¬¶ÏµãÌõ¼þÊÇx>0£¬Èç¹û³ÌÐò±»¶Ïסºó£¬Ò²¾ÍÊÇ£¬Ò»µ©xµÄÖµÔÚfooº¯ÊýÖдóÓÚ0£¬GDB»á×Ô¶¯´òÓ¡³öxµÄÖµ£¬²¢¼ÌÐøÔËÐгÌÐò¡£
Èç¹ûÄãÒªÇå³ý¶ÏµãÉϵÄÃüÁîÐòÁУ¬ÄÇôֻҪ¼òµ¥µÄÖ´ÐÐÒ»ÏÂcommandsÃüÁ²¢Ö±½ÓÔÚ´ò¸öend¾ÍÐÐÁË¡£
Æß¡¢¶Ïµã²Ëµ¥
ÔÚC++ÖУ¬¿ÉÄÜ»áÖظ´³öÏÖͬһ¸öÃû×ֵĺ¯ÊýÈô¸É´Î£¨º¯ÊýÖØÔØ£©£¬ÔÚÕâÖÖÇé¿öÏ£¬
break
°Ñº¯ÊýµÄ²ÎÊýÀàÐ͸æËßGDB£¬ÒÔÖ¸¶¨Ò»¸öº¯Êý¡£·ñÔòµÄ»°£¬GDB»á¸øÄãÁгöÒ»¸ö¶Ïµã²Ëµ¥¹©ÄãÑ¡ÔñÄãËùÐèÒªµÄ¶Ïµã¡£
ÄãÖ»ÒªÊäÈëÄã²Ëµ¥ÁбíÖеıàºÅ¾Í¿ÉÒÔÁË¡£È磺
(gdb) b String::after
[0] cancel
[1] all
[2] file:String.cc; line number:867
[3] file:String.cc; line number:860
[4] file:String.cc; line number:875
[5] file:String.cc; line number:853
[6] file:String.cc; line number:846
[7] file:String.cc; line number:735
> 2 4 6
Breakpoint 1 at 0xb26c: file String.cc, line 867.
Breakpoint 2 at 0xb344: file String.cc, line 875.
Breakpoint 3 at 0xafcc: file String.cc, line 846.
Multiple breakpoints were set.
Use the "delete" command to delete unwanted
breakpoints.
(gdb)
¿É¼û£¬GDBÁгöÁËËùÓÐafterµÄÖØÔغ¯Êý£¬Äã¿ÉÒÔÑ¡Ò»ÏÂÁбí±àºÅ¾ÍÐÐÁË¡£0±íʾ·ÅÆúÉèÖöϵ㣬1±íʾËùÓк¯Êý¶¼ÉèÖöϵ㡣
°Ë¡¢»Ö¸´³ÌÐòÔËÐк͵¥²½µ÷ÊÔ
µ±³ÌÐò±»Í£×¡ÁË£¬Äã¿ÉÒÔÓÃcontinueÃüÁî»Ö¸´³ÌÐòµÄÔËÐÐÖ±µ½³ÌÐò½áÊø£¬»òÏÂÒ»¸ö¶Ïµãµ½À´¡£
Ò²¿ÉÒÔʹÓÃstep»ònextÃüÁîµ¥²½¸ú×Ù³ÌÐò¡£
continue [ignore-count]
c [ignore-count]
fg [ignore-count]
»Ö¸´³ÌÐòÔËÐУ¬Ö±µ½³ÌÐò½áÊø£¬»òÊÇÏÂÒ»¸ö¶Ïµãµ½À´¡£ignore-count±íʾºöÂÔÆäºóµÄ¶Ïµã´ÎÊý¡£continue£¬c£¬fgÈý¸öÃüÁÊÇÒ»ÑùµÄÒâ˼¡£
step
µ¥²½¸ú×Ù£¬Èç¹ûÓк¯Êýµ÷Óã¬Ëû»á½øÈë¸Ãº¯Êý¡£½øÈ뺯ÊýµÄÇ°ÌáÊÇ£¬´Ëº¯Êý±»±àÒëÓÐdebugÐÅÏ¢¡£ºÜÏñVCµÈ¹¤¾ßÖеÄstep in¡£ºóÃæ¿ÉÒÔ¼ÓcountÒ²¿ÉÒÔ²»¼Ó£¬²»¼Ó±íʾһÌõÌõµØÖ´ÐУ¬¼Ó±íʾִÐкóÃæµÄcountÌõÖ¸ÁȻºóÔÙͣס¡£
next
ͬÑùµ¥²½¸ú×Ù£¬Èç¹ûÓк¯Êýµ÷Óã¬Ëû²»»á½øÈë¸Ãº¯Êý¡£ºÜÏñVCµÈ¹¤¾ßÖеÄstep over¡£ºóÃæ¿ÉÒÔ¼ÓcountÒ²¿ÉÒÔ²»¼Ó£¬²»¼Ó±íʾһÌõÌõµØÖ´ÐУ¬¼Ó±íʾִÐкóÃæµÄcountÌõÖ¸ÁȻºóÔÙͣס¡£
set step-mode
set step-mode on
´ò¿ªstep-modeģʽ£¬ÓÚÊÇ£¬ÔÚ½øÐе¥²½¸ú×Ùʱ£¬³ÌÐò²»»áÒòΪûÓÐdebugÐÅÏ¢¶ø²»Í£×¡¡£Õâ¸ö²ÎÊýÓкÜÀûÓڲ鿴»úÆ÷Âë¡£
set step-mod off
¹Ø±Õstep-modeģʽ¡£
finish
ÔËÐгÌÐò£¬Ö±µ½µ±Ç°º¯ÊýÍê³É·µ»Ø¡£²¢´òÓ¡º¯Êý·µ»ØʱµÄ¶ÑÕ»µØÖ·ºÍ·µ»ØÖµ¼°²ÎÊýÖµµÈÐÅÏ¢¡£
until »ò u
µ±ÄãÑá¾ëÁËÔÚÒ»¸öÑ»·ÌåÄÚµ¥²½¸ú×Ùʱ£¬Õâ¸öÃüÁî¿ÉÒÔÔËÐгÌÐòÖ±µ½Í˳öÑ»·Ìå¡£
stepi »ò si
nexti »ò ni
µ¥²½¸ú×ÙÒ»Ìõ»úÆ÷Ö¸ÁһÌõ³ÌÐò´úÂëÓпÉÄÜÓÉÊýÌõ»úÆ÷Ö¸ÁîÍê³É£¬stepiºÍnexti¿ÉÒÔµ¥²½Ö´ÐлúÆ÷Ö¸Áî¡£ÓëÖ®Ò»ÑùÓÐÏàͬ¹¦ÄܵÄÃüÁîÊÇ ¡°display/i $pc¡± £¬µ±ÔËÐÐÍêÕâ¸öÃüÁîºó£¬µ¥²½¸ú×Ù»áÔÚ´ò³ö³ÌÐò´úÂëµÄͬʱ´ò³ö»úÆ÷Ö¸ÁҲ¾ÍÊÇ»ã±à´úÂ룩
¾Å¡¢Ðźţ¨Signals£©
ÐźÅÊÇÒ»ÖÖÈíÖжϣ¬ÊÇÒ»ÖÖ´¦ÀíÒ첽ʼþµÄ·½·¨¡£Ò»°ãÀ´Ëµ£¬²Ù×÷ϵͳ¶¼Ö§³ÖÐí¶àÐźš£ÓÈÆäÊÇUNIX£¬
±È½ÏÖØÒªÓ¦ÓóÌÐòÒ»°ã¶¼»á´¦ÀíÐźš£UNIX ¶¨ÒåÁËÐí¶àÐźţ¬±ÈÈçSIGINT±íʾÖжÏ×Ö·ûÐźţ¬Ò²¾ÍÊÇCtrl+CµÄÐźţ¬
SIGBUS±íʾӲ¼þ¹ÊÕϵÄÐźţ»SIGCHLD±íʾ×Ó½ø³Ì״̬¸Ä±äÐźţ»SIGKILL±íʾÖÕÖ¹³ÌÐòÔËÐеÄÐźţ¬µÈµÈ¡£
ÐźÅÁ¿±à³ÌÊÇUNIXÏ·dz£ÖØÒªµÄÒ»ÖÖ¼¼Êõ¡£
GDBÓÐÄÜÁ¦ÔÚÄãµ÷ÊÔ³ÌÐòµÄʱºò´¦ÀíÈκÎÒ»ÖÖÐźţ¬Äã¿ÉÒÔ¸æËßGDBÐèÒª´¦ÀíÄÄÒ»ÖÖÐźš£
Äã¿ÉÒÔÒªÇóGDBÊÕµ½ÄãËùÖ¸¶¨µÄÐźÅʱ£¬ÂíÉÏͣסÕýÔÚÔËÐеijÌÐò£¬ÒÔ¹©Äã½øÐе÷ÊÔ¡£
Äã¿ÉÒÔÓÃGDBµÄhandleÃüÁîÀ´Íê³ÉÕâÒ»¹¦ÄÜ¡£
handle
ÔÚGDBÖж¨ÒåÒ»¸öÐźŴ¦Àí¡£ÐźÅ
nostop
µ±±»µ÷ÊԵijÌÐòÊÕµ½ÐźÅʱ£¬GDB²»»áͣס³ÌÐòµÄÔËÐУ¬µ«»á´ò³öÏûÏ¢¸æËßÄãÊÕµ½ÕâÖÖÐźš£
stop
µ±±»µ÷ÊԵijÌÐòÊÕµ½ÐźÅʱ£¬GDB»áͣסÄãµÄ³ÌÐò¡£
µ±±»µ÷ÊԵijÌÐòÊÕµ½ÐźÅʱ£¬GDB»áÏÔʾ³öÒ»ÌõÐÅÏ¢¡£
noprint
µ±±»µ÷ÊԵijÌÐòÊÕµ½ÐźÅʱ£¬GDB²»»á¸æËßÄãÊÕµ½ÐźŵÄÐÅÏ¢¡£
pass
noignore
µ±±»µ÷ÊԵijÌÐòÊÕµ½ÐźÅʱ£¬GDB²»´¦ÀíÐźš£Õâ±íʾ£¬GDB»á°ÑÕâ¸öÐźŽ»¸ø±»µ÷ÊÔ³ÌÐò»á´¦Àí¡£
nopass
ignore
µ±±»µ÷ÊԵijÌÐòÊÕµ½ÐźÅʱ£¬GDB²»»áÈñ»µ÷ÊÔ³ÌÐòÀ´´¦ÀíÕâ¸öÐźš£
info signals
info handle
²é¿´ÓÐÄÄЩÐźÅÔÚ±»GDB¼ì²âÖС£
Ê®¡¢Ị̈߳¨Thread Stops£©
Èç¹ûÄã³ÌÐòÊǶàÏ̵߳Ļ°£¬Äã¿ÉÒÔ¶¨ÒåÄãµÄ¶ÏµãÊÇ·ñÔÚËùÓеÄÏß³ÌÉÏ£¬»òÊÇÔÚij¸öÌض¨µÄÏ̡߳£GDBºÜÈÝÒ×°ïÄãÍê³ÉÕâÒ»¹¤×÷¡£
break
break
linespecÖ¸¶¨Á˶ϵãÉèÖÃÔÚµÄÔ´³ÌÐòµÄÐкš£threadnoÖ¸¶¨ÁËÏ̵߳ÄID£¬×¢Ò⣬Õâ¸öIDÊÇGDB·ÖÅäµÄ£¬Äã¿ÉÒÔͨ¹ý¡°info threads¡±ÃüÁîÀ´²é¿´ÕýÔÚÔËÐгÌÐòÖеÄÏß³ÌÐÅÏ¢¡£Èç¹ûÄã²»Ö¸¶¨thread
(gdb) break frik.c:13 thread 28 if bartab > lim
µ±ÄãµÄ³ÌÐò±»GDBͣסʱ£¬ËùÓеÄÔËÐÐÏ̶߳¼»á±»Í£×¡¡£Õâ·½±ãÄãÄã²é¿´ÔËÐгÌÐòµÄ×ÜÌåÇé¿ö¡£
¶øÔÚÄã»Ö¸´³ÌÐòÔËÐÐʱ£¬ËùÓеÄÏß³ÌÒ²»á±»»Ö¸´ÔËÐС£ÄÇÅÂÊÇÖ÷½ø³ÌÔÚ±»µ¥²½µ÷ÊÔʱ¡£
zcc1984 ÓÚ 2009-07-24 11:22:26·¢±í:
²»´í
kxajd001 ÓÚ 2009-07-22 14:55:31·¢±í:
²»´í ÎÒÓб¾ÔÚLinuxϵÄCÓïÑÔ µ«Ì«´ó ¹þ¹þ »¹Ã»´ò°üºÃ µÈÎÒ´òºÃ°ü´ó¼ÒÔÚ·ÖÏí:0)1
hice0000 ÓÚ 2009-07-22 14:49:57·¢±í:
ѧϰ°¡£¡£¡
balini ÓÚ 2009-07-22 13:08:19·¢±í:
¿´²»¶®Ò®...
wusicon ÓÚ 2009-07-14 20:32:06·¢±í:
(()):sGood!!
ÔÞ:0wmjh(1
ldmiao ÓÚ 2009-07-10 16:22:37·¢±í:
ºÜºÃ£¬ºÜÇ¿´ó
¶¥¶¥¶¥
ldmiao ÓÚ 2009-07-10 16:22:34·¢±í:
ºÜºÃ£¬ºÜÇ¿´ó
¶¥¶¥¶¥
ldmiao ÓÚ 2009-07-10 16:22:26·¢±í:
ºÜºÃ£¬ºÜÇ¿´ó
¶¥¶¥¶¥
ldmiao ÓÚ 2009-07-10 16:22:18·¢±í:
ºÜºÃ£¬ºÜÇ¿´ó
¶¥¶¥¶¥
ldmiao ÓÚ 2009-07-10 16:22:15·¢±í:
ºÜºÃ£¬ºÜÇ¿´ó
¶¥¶¥¶¥
ldmiao ÓÚ 2009-07-10 16:22:04·¢±í:
ºÜºÃ£¬ºÜÇ¿´ó
xlylinux ÓÚ 2009-07-10 15:51:00·¢±í:
лл
wfq1128 ÓÚ 2009-07-01 13:47:26·¢±í:
very good!!!
zaifeng ÓÚ 2009-06-15 13:26:23·¢±í:
¿´²»Ã÷°×
fankyxu ÓÚ 2009-06-11 09:52:28·¢±í:
ºÃ³¤Å¶¡«¡«¡«