gdbµ÷ÊÔ¹¤¾ßµÄʹÓÃ
gdbÊÇÒ»¿îÓÃÓÚµ÷ÊÔc¡¢c++µÈÓïÑÔ³ÌÐòµÄÇ¿Ê¢ÏÂÁîÐй¤¾ß£¬¿ÉÈóÌÐòÔ±ÔÚÖ´ÐÐÀú³ÌÖмàÊӺͿØÖƳÌÐòÁ÷£¬ÒÔ±ãʶ±ð²¢ÐÞ¸´¹ýʧ¡£³£¼ûÏÂÁî°üÀ¨£ºÉèÖöϵ㣺break [ÐкÅ]×îÏȵ÷ÊÔ£ºrun´òÓ¡±äÁ¿Öµ£ºp [±äÁ¿Ãû]ÖðÐÐÖ´ÐдúÂ룺nµ¥²½½øÈ뺯Êý£ºs´òӡŲÓÿÍÕ»£ºbt
gdbµ÷ÊÔ¹¤¾ßµÄʹÓÃ
ʲôÊÇgdb£¿
gdb£¨GNUµ÷ÊÔÆ÷£©ÊÇÒ»¸öÇ¿Ê¢µÄÏÂÁîÐй¤¾ß£¬ÓÃÓÚµ÷ÊÔC¡¢C++¡¢FortranµÈ¶àÖÖ±à³ÌÓïÑԵijÌÐò¡£ËüÊÇLinuxºÍUnixϵͳÖÐÆÕ±éʹÓõĵ÷ÊÔÇéÐΡ£
gdbµÄÓÃ;
gdbÔÊÐí³ÌÐòÔ±ÔÚ³ÌÐòÖ´ÐÐÀú³ÌÖмàÊӺͿØÖƳÌÐòµÄÖ´ÐÐÁ÷£¬ÒÔ±ãʶ±ðºÍÐÞ¸´¹ýʧ¡£ËüÌṩÁËһϵÁÐÏÂÁÓÃÓÚ£º
- ÉèÖöϵãÒÔÔÚÌض¨µãÔÝÍ£³ÌÐò
- ¼ì²é±äÁ¿µÄÖµºÍÊý¾Ý½á¹¹
- ÖðÐÐÖ´ÐдúÂë²¢ÉèÖÃÊÓ²ìµã
- ´òӡŲÓÿÍÕ»ºÍ¼Ä´æÆ÷Öµ
- ÐÞ¸ÄÄÚ´æºÍ¼Ä´æÆ÷Öµ
ʹÓÃgdb
-
Æô¶¯gdb
gdb [program_name]
µÇ¼ºó¸´ÖÆ
-
ÉèÖöϵã
break [line_number]
µÇ¼ºó¸´ÖÆ
-
×îÏȵ÷ÊÔ
run
µÇ¼ºó¸´ÖÆ
-
Ö´ÐÐÏÂÁî
ÔÚgdb¿ØÖÆ̨ÖУ¬Äú¿ÉÒÔʹÓÃÏÂÁîÀ´¼ì²é±äÁ¿¡¢ÉèÖÃÊÓ²ìµã¡¢Ð޸Ĵ洢Æ÷µÈ¡£ÒÔÏÂÊÇһЩ³£¼ûÏÂÁ
- n£ºÖðÐÐÖ´ÐдúÂë
- p£º´òÓ¡±äÁ¿µÄÖµ
- w£ºÉèÖÃÊÓ²ìµã
- x£º¼ì²é´æ´¢Æ÷
- s£ºµ¥²½½øÈ뺯Êý
- bt£º´òӡŲÓÿÍÕ»
-
Í˳ögdb
quit
µÇ¼ºó¸´ÖÆ
ʾÀý
¼ÙÉèÎÒÃÇÓÐÒ»¸öÃûΪmy_program.cµÄ³ÌÐò£¬ÆäÖаüÀ¨Ò»¸ö°üÀ¨¹ýʧµÄº¯Êý£º
#include <stdio.h> int divide_by_zero(int a) { return a / 0; } int main() { int x = 5; printf("%d\n", divide_by_zero(x)); return 0; }</stdio.h>
µÇ¼ºó¸´ÖÆ
ÎÒÃÇ¿ÉÒÔʹÓÃgdbÀ´µ÷ÊÔÕâ¸ö³ÌÐò£º
gdb my_program (gdb) break 11 (gdb) run (gdb) list 10,15 10 #include <stdio.h> 11 int divide_by_zero(int a) { 12 return a / 0; 13 } 14 15 int main() { (gdb) p divide_by_zero(x) Can't read variable x in this context. (gdb) p x $1 = 5 (gdb) n Program received signal SIGFPE, Arithmetic exception. 0x0000000000400595 in divide_by_zero (a=5) at my_program.c:12 12 return a / 0; (gdb) list 11 int divide_by_zero(int a) { 12 return a / 0; 13 } 14 15 int main() { (gdb) print a $2 = 5 (gdb) quit</stdio.h>
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃÇÔÚµÚ11ÐÐÉèÖÃÁËÒ»¸ö¶Ïµã£¬È»ºóÔËÐгÌÐò¡£µ±³ÌÐòÔڶϵ㴦ÔÝͣʱ£¬ÎÒÃÇ´òÓ¡Á˱äÁ¿xµÄÖµ£¬²¢ÊµÑé´òÓ¡divide_by_zero(x)µÄÖµ¡£ÓÉÓÚxÔÚdivide_by_zeroº¯ÊýÖÐδ½ç˵£¬Òò´ËÎÒÃÇ¿´µ½ÁËÒ»¸ö¹ýʧ¡£½ÓÏÂÀ´£¬ÎÒÃÇÖðÐÐÖ´ÐдúÂ룬ֱµ½³ÌÐòÍ߽⣬Ȼºó´òÓ¡Á˹ýʧÐÅÏ¢ºÍŲÓÿÍÕ»¡£
ÒÔÉϾÍÊÇgdbµ÷ÊÔ¹¤¾ßµÄʹÓõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡