gdbµ÷ÊÔÆ÷µÄʹÓÃÒªÁìºÍ±àÒë°ì·¨
ʹÓÃgdbµ÷ÊÔÆ÷µÄÒªÁ죺Æô¶¯gdb£ºÊäÈëgdbºó¸ú¿ÉÖ´ÐÐÎļþ¡£ÉèÖöϵ㣺ʹÓÃbreakÏÂÁîÔÚÌض¨Ðд¦ÉèÖöϵ㡣ÔËÐгÌÐò£ºÊ¹ÓÃrunÏÂÁîÔËÐгÌÐò£¬ÔÚÓöµ½¶ÏµãʱÔÝÍ£¡£¼ì²é±äÁ¿£ºÊ¹ÓÃprintÏÂÁî´òÓ¡±äÁ¿Öµ¡£µ¥²½Ö´ÐУºÊ¹ÓÃstepÏÂÁîÖðÐÐÖ´ÐгÌÐò¡£Éó²éŲÓÃÕ»£ºÊ¹ÓÃbacktraceÏÂÁîÉó²éÄ¿½ñŲÓÃÕ»¡£¼ÌÐøÖ´ÐУºÊ¹ÓÃcontinueÏÂÁî¼ÌÐø³ÌÐòÖ´ÐС£
gdbµ÷ÊÔÆ÷µÄʹÓÃÒªÁìºÍ±àÒë°ì·¨
gdbµ÷ÊÔÆ÷ʹÓÃÒªÁì
- Æô¶¯gdb£ºÔÚÏÂÁîÐÐÖÐÊäÈëgdbºó¸úÒªµ÷ÊԵĿÉÖ´ÐÐÎļþ¡£
- ¶ÏµãÉèÖãºÊ¹ÓÃbreakÏÂÁîÔÚÒª×èÖ¹Ö´ÐеÄÐд¦ÉèÖöϵ㡣
- ÔËÐгÌÐò£ºÊ¹ÓÃrunÏÂÁîÔËÐгÌÐò£¬³ÌÐò½«ÔÚÓöµ½¶Ïµã´¦ÔÝÍ£¡£
- ¼ì²é±äÁ¿£ºÊ¹ÓÃprintÏÂÁî´òÓ¡±äÁ¿Öµ¡£
- µ¥²½Ö´ÐУºÊ¹ÓÃstepÏÂÁîÖðÐÐÖ´ÐгÌÐò¡£
- Éó²éŲÓÃÕ»£ºÊ¹ÓÃbacktraceÏÂÁîÉó²éÄ¿½ñŲÓÃÕ»¡£
- ¼ÌÐøÖ´ÐУºÊ¹ÓÃcontinueÏÂÁî¼ÌÐø³ÌÐòÖ´ÐС£
±àÒë°ì·¨
- ±àдԴ´úÂ룺ʹÓÃÈκÎÎı¾±à¼Æ÷£¨ÈçVim»òVisual Studio Code£©±àдC»òC++Ô´´úÂëÎļþ£¨ÀýÈçmain.c£©¡£
- ±àÒëÔ´´úÂ룺ʹÓÃgcc»òclang±àÒëÆ÷£¨ÀýÈ磺gcc main.c -o main£©¡£
- Á´½ÓÄ¿µÄÎļþ£ºÈôÊÇÔ´´úÂëÎļþÒÀÀµÓÚÍⲿ¿â»òÆäËûÄ¿µÄÎļþ£¬ÔòÐèҪʹÓÃÁ´½ÓÆ÷£¨Èçld£©Á´½ÓËüÃÇ£¨ÀýÈ磺ld -o main main.o lib.a£©¡£
ʾÀý
ÒÔÏÂÊÇgdbµ÷ÊԻỰµÄʾÀý£º
$ gdb main (gdb) break 12 Breakpoint 1 at 0x100001234: file main.c, line 12. (gdb) run Breakpoint 1, main () at main.c:12 12 int main(int argc, char *argv[]) { (gdb) print argc $1 = 1 (gdb) step 13 printf("Hello, world!\n"); (gdb) continue Continuing. Program exited with code 0.
µÇ¼ºó¸´ÖÆ
ÒÔÉϾÍÊÇgdbµ÷ÊÔÆ÷µÄʹÓÃÒªÁìºÍ±àÒë°ì·¨µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡
ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£