µ±Ç°Î»ÖãºLinux½Ì³Ì - Linux×ÛºÏ - ʹÓàGDB µ÷ÊÔ Linux Èí¼þ

ʹÓàGDB µ÷ÊÔ Linux Èí¼þ

ÄÚÈÝ£º ±àÒë ÔËÐÐ gdb µ÷ÊԻỰʾÀý ʹÓöϵ㠸ü¶à¶ÏµãºÍ¹Û²ìµã Core Îļþ ¶ÑÕ»¸ú×Ù Á¬½Óµ½ÆäËü½ø³Ì ÆäËüС¼¼ÇÉ ½áÊøÓï ²Î¿¼×ÊÁÏ ¹ØÓÚ×÷Õß GNU µ÷ÊÔÆ÷¼ò½é ×÷Õß:David Seager Linux µÄ´ó²¿·ÖÌØÉ«Ô´×ÔÓÚ shell µÄ GNU µ÷ÊÔÆ÷£¬Ò²³Æ×÷ gdb¡£gdb ¿ÉÒÔÈÃÄú²é¿´³ÌÐòµÄÄÚ²¿½á¹¹¡¢´òÓ¡±äÁ¿Öµ¡¢ÉèÖöϵ㣬ÒÔ¼°µ¥²½µ÷ÊÔÔ´´úÂë¡£ËüÊǹ¦Äܼ«ÆäÇ¿´óµÄ¹¤¾ß£¬ÊÊÓÃÓÚÐÞ¸´³ÌÐò´úÂëÖеÄÎÊÌâ¡£ÔÚ±¾ÎÄÖУ¬ÎÒ½«³¢ÊÔ˵Ã÷ gdb Óжà°ô£¬¶àʵÓᣠ±àÒë ¿ªÊ¼µ÷ÊÔ֮ǰ£¬±ØÐëÓóÌÐòÖеĵ÷ÊÔÐÅÏ¢±àÒëÒªµ÷ÊԵijÌÐò¡£ÕâÑù£¬gdb ²ÅÄܹ»µ÷ÊÔËùʹÓõıäÁ¿¡¢´úÂëÐкͺ¯Êý¡£Èç¹ûÒª½øÐбàÒ룬ÇëÔÚ gcc£¨»ò g++£©ÏÂʹÓöîÍâµÄ '-g' Ñ¡ÏîÀ´±àÒë³ÌÐò£º gcc -g eg.c -o eg ÔËÐÐ gdb ÔÚ shell ÖУ¬¿ÉÒÔʹÓà 'gdb' ÃüÁî²¢Ö¸¶¨³ÌÐòÃû×÷Ϊ²ÎÊýÀ´ÔËÐÐ gdb£¬ÀýÈç 'gdb eg'£»»òÕßÔÚ gdb ÖУ¬¿ÉÒÔʹÓà file ÃüÁîÀ´×°ÈëÒªµ÷ÊԵijÌÐò£¬ÀýÈç 'file eg'¡£ÕâÁ½ÖÖ·½Ê½¶¼¼ÙÉèÄúÊÇÔÚ°üº¬³ÌÐòµÄĿ¼ÖÐÖ´ÐÐÃüÁî¡£×°Èë³ÌÐòÖ®ºó£¬¿ÉÒÔÓà gdb ÃüÁî 'run' À´Æô¶¯³ÌÐò¡£ µ÷ÊԻỰʾÀý Èç¹ûÒ»ÇÐÕý³££¬³ÌÐò½«Ö´Ðе½½áÊø£¬´Ëʱ gdb ½«ÖØлñµÃ¿ØÖÆ¡£µ«Èç¹ûÓдíÎ󽫻áÔõôÑù£¿ÕâÖÖÇé¿öÏ£¬gdb »á»ñµÃ¿ØÖƲ¢ÖжϳÌÐò£¬´Ó¶ø¿ÉÒÔÈÃÄú¼ì²éËùÓÐÊÂÎïµÄ״̬£¬Èç¹ûÔËÆøºÃµÄ»°£¬¿ÉÒÔÕÒ³öÔ­Òò¡£ÎªÁËÒý·¢ÕâÖÖÇé¿ö£¬ÎÒÃǽ«Ê¹ÓÃÒ»¸öʾÀý³ÌÐò£º ´úÂëʾÀý eg1.c #include int wib(int no1, int no2) { int result, diff; diff = no1 - no2; result = no1 / diff; return result; } int main(int argc, char *argv[]) { int value, div, result, i, total; value = 10; div = 6; total = 0; for(i = 0; i < 10; i++) { result = wib(value, div); total += result; div++; value--; } printf("%d wibed by %d equals %dn", value, div, total); return 0; } Õâ¸ö³ÌÐò½«ÔËÐÐ 10 ´Î for Ñ­»·£¬Ê¹Óà 'wib()' º¯Êý¼ÆËã³öÀÛ»ýÖµ£¬×îºó´òÓ¡³ö½á¹û¡£ ÔÚÄúϲ»¶µÄÎı¾±à¼­Æ÷ÖÐÊäÈëÕâ¸ö³ÌÐò£¨Òª±£³ÖÏàͬµÄÐоࣩ£¬±£´æΪ 'eg1.c'£¬Ê¹Óà 'gcc -g eg1.c -o eg1' ½øÐбàÒ룬²¢Óà 'gdb eg1' Æô¶¯ gdb¡£Ê¹Óà 'run' ÔËÐгÌÐò¿ÉÄÜ»á²úÉúÒÔÏÂÏûÏ¢£º Program received signal SIGFPE, Arithmetic exception. 0x80483ea in wib (no1=8, no2=8) at eg1.c:7 7 result = no1 / diff; (gdb) gdb Ö¸³öÔÚ³ÌÐòµÚ 7 Ðз¢ÉúÒ»¸öËãÊõÒì³££¬Í¨³£Ëü»á´òÓ¡ÕâÒ»ÐÐÒÔ¼° wib() º¯ÊýµÄ×Ô±äÁ¿Öµ¡£Òª²é¿´µÚ 7 ÐÐÇ°ºóµÄÔ´´úÂ룬ÇëʹÓà 'list' ÃüÁËüͨ³£»á´òÓ¡ 10 ÐС£ÔÙ´ÎÊäÈë 'list'£¨»òÕß°´»Ø³µÖظ´ÉÏÒ»ÌõÃüÁ½«Áгö³ÌÐòµÄÏ 10 ÐС£´Ó gdb ÏûÏ¢ÖпÉÒÔ¿´³ö£¬µÚ 7 ÐÐÖеijý·¨ÔËËã³öÁË´í£¬³ÌÐòÔÚÕâÒ»ÐÐÖн«±äÁ¿ "no1" ³ýÒÔ "diff"¡£ Òª²é¿´±äÁ¿µÄÖµ£¬Ê¹Óà gdb 'print' ÃüÁî²¢Ö¸¶¨±äÁ¿Ãû¡£ÊäÈë 'print no1' ºÍ 'print diff'£¬¿ÉÒÔÏàÓ¦¿´µ½ "no1" ºÍ "diff" µÄÖµ£¬½á¹ûÈçÏ£º (gdb) print no1 $5 = 8 (gdb) print diff $2 = 0 gdb Ö¸³ö "no1" µÈÓÚ 8£¬"diff" µÈÓÚ 0¡£¸ù¾ÝÕâЩֵºÍµÚ 7 ÐÐÖеÄÓï¾ä£¬ÎÒÃÇ¿ÉÒÔÍƶϳöËãÊõÒì³£ÊÇÓɳýÊýΪ 0 µÄ³ý·¨ÔËËãÔì³ÉµÄ¡£Çåµ¥ÏÔʾÁËµÚ 6 ÐмÆËãµÄ±äÁ¿ "diff"£¬ÎÒÃÇ¿ÉÒÔ´òÓ¡ "diff" ±í´ïʽ£¨Ê¹Óà 'print no1 - no2' ÃüÁ£¬À´ÖØйÀ¼ÆÕâ¸ö±äÁ¿¡£gdb ¸æËßÎÒÃÇ wib º¯ÊýµÄÕâÁ½¸ö×Ô±äÁ¿¶¼µÈÓÚ 8£¬ÓÚÊÇÎÒÃÇÒª¼ì²éµ÷Óà wib() º¯ÊýµÄ main() º¯Êý£¬ÒԲ鿴ÕâÊÇÔÚʲôʱºò·¢ÉúµÄ¡£ÔÚÔÊÐí³ÌÐò×ÔÈ»ÖÕÖ¹µÄͬʱ£¬ÎÒÃÇʹÓà 'continue' ÃüÁî¸æËß gdb ¼ÌÐøÖ´ÐС£ (gdb) continue Continuing. Program terminated with signal SIGFPE, Arithmetic exception. The program no longer exists. ʹÓöϵã ΪÁ˲鿴ÔÚ main() Öз¢ÉúÁËʲôÇé¿ö£¬¿ÉÒÔÔÚ³ÌÐò´úÂëÖеÄijһÌض¨Ðлòº¯ÊýÖÐÉèÖöϵ㣬ÕâÑù gdb »áÔÚÓöµ½¶ÏµãʱÖжÏÖ´ÐС£¿ÉÒÔʹÓÃÃüÁî 'break main' ÔÚ½øÈë main() º¯ÊýʱÉèÖöϵ㣬»òÕß¿ÉÒÔÖ¸¶¨ÆäËüÈκθÐÐËȤµÄº¯ÊýÃûÀ´ÉèÖöϵ㡣Ȼ¶ø£¬ÎÒÃÇֻϣÍûÔÚµ÷Óà wib() º¯Êý֮ǰÖжÏÖ´ÐС£ÊäÈë 'list main' ½«´òÓ¡´Ó main() º¯Êý¿ªÊ¼µÄÔ´ÂëÇåµ¥£¬Ôٴΰ´»Ø³µ½«ÏÔʾµÚ 21 ÐÐÉ쵀 wib() º¯Êýµ÷Óá£ÒªÔÚÄÇÒ»ÐÐÉÏÉèÖöϵ㣬ֻÐèÊäÈë 'break 21'¡£gdb ½«·¢³öÒÔÏÂÏìÓ¦£º (gdb) break 21 Breakpoint 1 at 0x8048428: file eg1.c, line 21. ÒÔÏÔʾËüÒÑÔÚÎÒÃÇÇëÇóµÄÐÐÉÏÉèÖÃÁË 1 ºÅ¶Ïµã¡£'run' ÃüÁ´ÓÍ·ÖØÐÂÔËÐгÌÐò£¬Ö±µ½ gdb ÖжÏΪֹ¡£·¢ÉúÕâÖÖÇé¿öʱ£¬gdb »áÉú³ÉÒ»ÌõÏûÏ¢£¬Ö¸³öËüÔÚÄĸö¶ÏµãÉÏÖжϣ¬ÒÔ¼°³ÌÐòÔËÐе½ºÎ´¦£º Breakpoint 1, main (argc=1, argv=0xbffff954) at eg1.c:21 21 result = wib(value, div); ·¢³ö 'print value' ºÍ 'print div' ½«»áÏÔʾÔÚµÚÒ»´Îµ÷Óà wib() ʱ£¬±äÁ¿·Ö±ðµÈÓÚ 10 ºÍ 6£¬¶ø 'print i' ½«»áÏÔʾ 0¡£ÐҺã¬gdb ½«ÏÔʾËùÓоֲ¿±äÁ¿µÄÖµ£¬²¢Ê¹Óà 'info locals' ÃüÁî±£´æ´óÁ¿ÊäÈëÐÅÏ¢¡£ ´ÓÒÔÉϵĵ÷²éÖпÉÒÔ¿´³ö£¬µ± "value" ºÍ "div" ÏàµÈʱ¾Í»á³öÏÖÎÊÌ⣬Òò´ËÊäÈë 'continue' ¼ÌÐøÖ´ÐУ¬Ö±µ½ÏÂÒ»´ÎÓöµ½ 1 ºÅ¶Ïµã¡£¶ÔÓÚÕâ´Îµü´ú£¬'info locals' ÏÔʾÁË value=9 ºÍ div=7¡£ ÓëÆäÔٴμÌÐø£¬»¹²»ÈçʹÓà 'next' ÃüÁîµ¥²½µ÷ÊÔ³ÌÐò£¬ÒԲ鿴 "value" ºÍ "div" ÊÇÈçºÎ¸Ä±äµÄ¡£gdb ½«ÏìÓ¦£º (gdb) next 22 total += result; ÔÙ°´Á½´Î»Ø³µ½«ÏÔʾ¼Ó·¨ºÍ¼õ·¨±í´ïʽ£º (gdb) 23 div++; (gdb) 24 value--; ÔÙ°´Á½´Î»Ø³µ½«ÏÔʾµÚ 21 ÐУ¬wib() µ÷Óá£'info locals' ½«ÏÔʾĿǰ "div" µÈÓÚ "value"£¬Õâ¾ÍÒâζ׎«·¢ÉúÎÊÌâ¡£Èç¹ûÓÐÐËȤ£¬¿ÉÒÔʹÓà 'step' ÃüÁÓë 'next' ÐγɶԱȣ¬'next' ½«Ìø¹ýº¯Êýµ÷Óã©À´¼ÌÐøÖ´ÐÐ wib() º¯Êý£¬ÒÔÔٴβ鿴³ý·¨´íÎó£¬È»ºóʹÓà 'next' À´¼ÆËã "result"¡£ ÏÖÔÚÒÑÍê³ÉÁ˵÷ÊÔ£¬¿ÉÒÔʹÓà 'quit' ÃüÁîÍ˳ö gdb¡£ÓÉÓÚ³ÌÐòÈÔÔÚÔËÐУ¬Õâ¸ö²Ù×÷»áÖÕÖ¹Ëü£¬gdb ½«ÌáʾÄúÈ·ÈÏ¡£


[1] [2] [3] ÏÂÒ»Ò³ 

¸ü¶à¶ÏµãºÍ¹Û²ìµã ÓÉÓÚÎÒÃÇÏëÒªÖªµÀÔÚµ÷Óà wib() º¯Êý֮ǰ "value" ʲôʱºòµÈÓÚ "div"£¬Òò´ËÔÚÉÏһʾÀýÖÐÎÒÃÇÔÚµÚ 21 ÐÐÖÐÉèÖöϵ㡣ÎÒÃDZØÐë¼ÌÐøÖ´ÐÐÁ½´Î³ÌÐò²Å»á·¢ÉúÕâÖÖÇé¿ö£¬µ«ÊÇÖ»ÒªÔڶϵãÉÏÉèÖÃÒ»¸öÌõ¼þ¾Í¿ÉÒÔʹ gdb Ö»ÔÚ "value" Óë "div" ÕæÕýÏàµÈʱÔÝÍ£¡£ÒªÉèÖÃÌõ¼þ£¬¿ÉÒÔÔÚ¶¨Òå¶Ïµãʱָ¶¨ "break if "¡£½« eg1 ÔÙ´Î×°Èë gdb£¬²¢ÊäÈ룺 (gdb) break 21 if value==div Breakpoint 1 at 0x8048428: file eg1.c, line 21. Èç¹ûÒѾ­ÔÚµÚ 21 ÐÐÖÐÉèÖÃÁ˶ϵ㣬Èç 1 ºÅ¶Ïµã£¬Ôò¿ÉÒÔʹÓà 'condition' ÃüÁîÀ´´úÌæÔڶϵãÉÏÉèÖÃÌõ¼þ£º (gdb) condition 1 value==div ʹÓà 'run' ÔËÐÐ eg1.c ʱ£¬Èç¹û "value" µÈÓÚ "div"£¬gdb ½«Öжϣ¬´Ó¶ø±ÜÃâÁËÔÚËüÃÇÏàµÈ֮ǰ±ØÐëÊÖ¹¤Ö´ÐÐ 'continue'¡£µ÷ÊÔ C ³ÌÐòʱ£¬¶ÏµãÌõ¼þ¿ÉÒÔÊÇÈκÎÓÐЧµÄ C ±í´ïʽ£¬Ò»¶¨ÒªÊdzÌÐòËùʹÓÃÓïÑÔµÄÈÎÒâÓÐЧ±í´ïʽ¡£Ìõ¼þÖÐÖ¸¶¨µÄ±äÁ¿±ØÐëÔÚÉèÖÃÁ˶ϵãµÄÐÐÖУ¬·ñÔò±í´ïʽ¾ÍûÓÐʲôÒâÒ壡 ʹÓà 'condition' ÃüÁîʱ£¬Èç¹ûÖ¸¶¨¶Ïµã±àºÅµ«ÓÖ²»Ö¸¶¨±í´ïʽ£¬¿ÉÒÔ½«¶ÏµãÉèÖóÉÎÞÌõ¼þ¶Ïµã£¬ÀýÈ磬'condition 1' ¾Í½« 1 ºÅ¶ÏµãÉèÖóÉÎÞÌõ¼þ¶Ïµã¡£ Òª²é¿´µ±Ç°¶¨ÒåÁËʲô¶Ïµã¼°ÆäÌõ¼þ£¬Çë·¢³öÃüÁî 'info break'£º (gdb) info break Num Type Disp Enb Address What 1 breakpoint keep y 0x08048428 in main at eg1.c:21 stop only if value == div breakpoint already hit 1 time ³ýÁËËùÓÐÌõ¼þºÍÒѾ­Óöµ½¶Ïµã¶àÉÙ´ÎÖ®Í⣬¶ÏµãÐÅÏ¢»¹ÔÚ 'Enb' ÁÐÖÐÖ¸¶¨ÁËÊÇ·ñÆôÓøöϵ㡣¿ÉÒÔʹÓÃÃüÁî 'disable '¡¢'enable ' »ò 'delete ' À´½ûÓá¢ÆôÓúͳ¹µ×ɾ³ý¶Ïµã£¬ÀýÈç 'disable 1' ½«×èÖ¹ÔÚ 1 ºÅ¶Ïµã´¦Öжϡ£ Èç¹ûÎÒÃÇ¶Ô "value" ʲôʱºò±äµÃÓë "div" ÏàµÈ¸ü¸ÐÐËȤ£¬ÄÇô¿ÉÒÔʹÓÃÁíÒ»Öֶϵ㣬³Æ×÷¼àÊÓ¡£µ±Ö¸¶¨±í´ïʽµÄÖµ¸Ä±äʱ£¬¼àÊӵ㽫ÖжϳÌÐòÖ´ÐУ¬µ«±ØÐëÔÚ±í´ïʽÖÐËùʹÓõıäÁ¿ÔÚ×÷ÓÃÓòÖÐʱÉèÖüàÊӵ㡣Ҫ»ñÈ¡×÷ÓÃÓòÖÐµÄ "value" ºÍ "div"£¬¿ÉÒÔÔÚ main º¯ÊýÉÏÉèÖöϵ㣬ȻºóÔËÐгÌÐò£¬µ±Óöµ½ main() ¶ÏµãʱÉèÖüàÊӵ㡣ÖØÐÂÆô¶¯ gdb£¬²¢×°Èë eg1£¬È»ºóÊäÈ룺 (gdb) break main Breakpoint 1 at 0x8048402: file eg1.c, line 15. (gdb) run ... Breakpoint 1, main (argc=1, argv=0xbffff954) at eg1.c:15 15 value = 10; ÒªÁ˽â "div" ºÎʱ¸ü¸Ä£¬¿ÉÒÔʹÓà 'watch div'£¬µ«ÓÉÓÚÒªÔÚ "div" µÈÓÚ "value" ʱÖжϣ¬ÄÇôӦÊäÈ룺 (gdb) watch div==value Hardware watchpoint 2: div == value Èç¹û¼ÌÐøÖ´ÐУ¬ÄÇôµ±±í´ïʽ "div==value" µÄÖµ´Ó 0£¨¼Ù£©±ä³É 1£¨Õ棩ʱ£¬gdb ½«Öжϣº (gdb) continue Continuing. Hardware watchpoint 2: div == value Old value = 0 New value = 1 main (argc=1, argv=0xbffff954) at eg1.c:19 19 for(i = 0; i < 10; i++) 'info locals' ÃüÁÑéÖ¤ "value" ÊÇ·ñȷʵµÈÓÚ "div"£¨ÔÙ´ÎÉùÃ÷£¬ÊÇ 8£©¡£ 'info watch' ÃüÁÁгöÒѶ¨ÒåµÄ¼àÊÓµãºÍ¶Ïµã£¨´ËÃüÁîµÈ¼ÛÓÚ 'info break'£©£¬¶øÇÒ¿ÉÒÔʹÓÃÓë¶ÏµãÏàͬµÄÓï·¨À´ÆôÓᢽûÓúÍɾ³ý¼àÊӵ㡣 core Îļþ ÔÚ gdb ÏÂÔËÐгÌÐò¿ÉÒÔʹ·ý»ñ´íÎó±äµÃ¸üÈÝÒ×£¬µ«ÔÚµ÷ÊÔÆ÷ÍâÔËÐеijÌÐòͨ³£»áÖÐÖ¹¶øÖ»ÁôÏÂÒ»¸ö core Îļþ¡£gdb ¿ÉÒÔ×°Èë core Îļþ£¬²¢ÈÃÄú¼ì²é³ÌÐòÖÐֹ֮ǰµÄ״̬¡£ ÔÚ gdb ÍâÔËÐÐʾÀý³ÌÐò eg1 ½«»áµ¼ÖºËÐÄÐÅϢת´¢£º $ ./eg1 Floating point exception (core dumped) ҪʹÓà core ÎļþÆô¶¯ gdb£¬ÔÚ shell Öз¢³öÃüÁî 'gdb eg1 core' »ò 'gdb eg1 -c core'¡£gdb ½«×°Èë core Îļþ£¬eg1 µÄ³ÌÐòÇåµ¥£¬ÏÔʾ³ÌÐòÊÇÈçºÎÖÕÖ¹µÄ£¬²¢ÏÔʾ·Ç³£ÀàËÆÓÚÎÒÃǸղÅÔÚ gdb ÏÂÔËÐгÌÐòʱ¿´µ½µÄÏûÏ¢£º ... Core was generated by `./eg1'. Program terminated with signal 8, Floating point exception. ... #0 0x80483ea in wib (no1=8, no2=8) at eg1.c:7 7 result = no1 / diff; ´Ëʱ£¬¿ÉÒÔ·¢³ö 'info locals'¡¢'print'¡¢'info args' ºÍ 'list' ÃüÁîÀ´²é¿´ÒýÆð³ýÊýΪÁãµÄÖµ¡£'info variables' ÃüÁ´òÓ¡³öËùÓгÌÐò±äÁ¿µÄÖµ£¬µ«ÕâÒª½øÐкܳ¤Ê±¼ä£¬ÒòΪ gdb ½«´òÓ¡ C ¿âºÍ³ÌÐò´úÂëÖеıäÁ¿¡£ÎªÁ˸üÈÝÒ׵زéÃ÷ÔÚµ÷Óà wib() µÄº¯ÊýÖз¢ÉúÁËʲôÇé¿ö£¬¿ÉÒÔʹÓà gdb µÄ¶ÑÕ»ÃüÁî¡£ ¶ÑÕ»¸ú×Ù ³ÌÐò¡°µ÷ÓöÑÕ»¡±Êǵ±Ç°º¯Êý֮ǰµÄËùÓÐÒѵ÷Óú¯ÊýµÄÁÐ±í£¨°üÀ¨µ±Ç°º¯Êý£©¡£Ã¿¸öº¯Êý¼°Æä±äÁ¿¶¼±»·ÖÅäÁËÒ»¸ö¡°Ö¡¡±£¬×î½üµ÷Óõĺ¯ÊýÔÚ 0 ºÅÖ¡ÖУ¨¡°µ×²¿¡±Ö¡£©¡£Òª´òÓ¡¶ÑÕ»£¬·¢³öÃüÁî 'BT'£¨'backtrace' [»ØËÝ] µÄËõд£©£º (gdb) bt #0 0x80483ea in wib (no1=8, no2=8) at eg1.c:7 #1 0x8048435 in main (argc=1, argv=0xbffff9c4) at eg1.c:21 ´Ë½á¹ûÏÔʾÁËÔÚ main() µÄµÚ 21 ÐÐÖе÷ÓÃÁ˺¯Êý wib()£¨Ö»ÒªÊ¹Óà 'list 21' ¾ÍÄÜ֤ʵÕâÒ»µã£©£¬¶øÇÒ wib() ÔÚ 0 ºÅÖ¡ÖУ¬main() ÔÚ 1 ºÅÖ¡ÖС£ÓÉÓÚ wib() ÔÚ 0 ºÅÖ¡ÖУ¬ÄÇôËü¾ÍÊÇÖ´ÐгÌÐòʱ·¢ÉúËãÊõ´íÎóµÄº¯Êý¡£ ʵ¼ÊÉÏ£¬·¢³ö 'info locals' ÃüÁîʱ£¬gdb »á´òÓ¡³öµ±Ç°Ö¡Öеľֲ¿±äÁ¿£¬È±Ê¡Çé¿öÏ£¬Õâ¸öÖ¡Öеĺ¯Êý¾ÍÊDZ»Öжϵĺ¯Êý£¨0 ºÅÖ¡£©¡£¿ÉÒÔʹÓÃÃüÁî 'frame' ´òÓ¡µ±Ç°Ö¡¡£Òª²é¿´ main º¯Êý£¨ÔÚ 1 ºÅÖ¡ÖУ©ÖеıäÁ¿£¬¿ÉÒÔ·¢³ö 'frame 1' Çл»µ½ 1 ºÅÖ¡£¬È»ºó·¢³ö 'info locals' ÃüÁ (gdb) frame 1 #1 0x8048435 in main (argc=1, argv=0xbffff9c4) at eg1.c:21 21 result = wib(value, div); (gdb) info locals value = 8 div = 8 result = 4 i = 2 total = 6 ´ËÐÅÏ¢ÏÔʾÁËÔÚµÚÈý´ÎÖ´ÐÐ "for" Ñ­»·Ê±£¨i µÈÓÚ 2£©·¢ÉúÁË´íÎ󣬴Ëʱ "value" µÈÓÚ "div"¡£ ¿ÉÒÔͨ¹ýÈçÉÏËùʾÔÚ 'frame' ÃüÁîÖÐÃ÷È·Ö¸¶¨ºÅÂ룬»òÕßʹÓà 'up' ÃüÁîÔÚ¶ÑÕ»ÖÐÉÏÒÆÒÔ¼° 'down' ÃüÁîÔÚ¶ÑÕ»ÖÐÏÂÒÆÀ´Çл»Ö¡¡£Òª»ñÈ¡ÓйØÖ¡µÄ½øÒ»²½ÐÅÏ¢£¬ÈçËüµÄµØÖ·ºÍ³ÌÐòÓïÑÔ£¬¿ÉÒÔʹÓÃÃüÁî 'info frame'¡£ gdb ¶ÑÕ»ÃüÁî¿ÉÒÔÔÚ³ÌÐòÖ´ÐÐÆÚ¼äʹÓã¬Ò²¿ÉÒÔÔÚ core ÎļþÖÐʹÓã¬Òò´Ë¶ÔÓÚ¸´ÔӵijÌÐò£¬¿ÉÒÔÔÚ³ÌÐòÔËÐÐʱ¸ú×ÙËüÊÇÈçºÎתµ½º¯ÊýµÄ¡£ Á¬½Óµ½ÆäËü½ø³Ì ³ýÁ˵÷ÊÔ core Îļþ»ò³ÌÐòÖ®Í⣬gdb »¹¿ÉÒÔÁ¬½Óµ½ÒѾ­ÔËÐеĽø³Ì£¨ËüµÄ³ÌÐòÒѾ­¹ý±àÒ룬²¢¼ÓÈëÁ˵÷ÊÔÐÅÏ¢£©£¬²¢Öжϸýø³Ì¡£Ö»ÐèÓÃÏ£Íû gdb Á¬½ÓµÄ½ø³Ì±êʶÌæ»» core ÎļþÃû¾Í¿ÉÒÔÖ´Ðд˲Ù×÷¡£ÒÔÏÂÊÇÒ»¸öÖ´ÐÐÑ­»·²¢Ë¯ÃßµÄʾÀý³ÌÐò£º eg2 ʾÀý´úÂë #include int main(int argc, char *argv[]) { int i; for(i = 0; i < 60; i++) { sleep(1); } return 0; } ʹÓà 'gcc -g eg2.c -o eg2' ±àÒë¸Ã³ÌÐò²¢Ê¹Óà './eg2 &' ÔËÐиóÌÐò¡£ÇëÁôÒâÔÚÆô¶¯¸Ã³ÌÐòʱÔÚ±³¾°ÉÏ´òÓ¡µÄ½ø³Ì±êʶ£¬ÔÚ±¾ÀýÖÐÊÇ 1283£º ./eg2 & [3] 1283 Æô¶¯ gdb ²¢Ö¸¶¨½ø³Ì±êʶ£¬ÔÚÎÒ¾ÙµÄÕâ¸öÀý×ÓÖÐÊÇ 'gdb eg2 1283'¡£gdb »á²éÕÒÒ»¸ö½Ð×÷ "1283" µÄ core Îļþ¡£Èç¹ûûÓÐÕÒµ½£¬ÄÇôֻҪ½ø³Ì 1283 ÕýÔÚÔËÐУ¨ÔÚ±¾ÀýÖпÉÄÜÔÚ sleep() ÖУ©£¬gdb ¾Í»áÁ¬½Ó²¢Öжϸýø³Ì£º ... /home/seager/gdb/1283: No sUCh file or Directory. Attaching to program: /home/seager/gdb/eg2, Pid 1283 ... 0x400a87f1 in __libc_nanosleep () from /lib/libc.so.6 (gdb) ´Ëʱ£¬¿ÉÒÔ·¢³öËùÓг£Óà gdb ÃüÁî¡£¿ÉÒÔʹÓà 'backtrace' À´²é¿´µ±Ç°Î»ÖÃÓë main() µÄÏà¶Ô¹Øϵ£¬ÒÔ¼° mian() µÄÖ¡ºÅÊÇʲô£¬È»ºóÇл»µ½ main() ËùÔÚµÄÖ¡£¬²é¿´ÒѾ­ÔÚ "for" Ñ­»·ÖÐÔËÐÐÁ˶àÉٴΣº (gdb) backtrace #0 0x400a87f1 in __libc_nanosleep () from /lib/libc.so.6 #1 0x400a877d in __sleep (seconds=1) at ../sysdeps/unix/sysv/linux/sleep.c:78 #2 0x80483ef in main (argc=1, argv=0xbffff9c4) at eg2.c:7 (gdb) frame 2 #2 0x80483ef in main (argc=1, argv=0xbffff9c4) at eg2.c:7 7 sleep(1); (gdb) print i $1 = 50 Èç¹ûÒѾ­Íê³ÉÁ˶ԳÌÐòµÄÐ޸ģ¬¿ÉÒÔ 'detach' ÃüÁî¼ÌÐøÖ´ÐгÌÐò£¬»òÕß 'kill' ÃüÁîɱËÀ½ø³Ì¡£»¹¿ÉÒÔÊ×ÏÈʹÓà 'file eg2' ×°ÈëÎļþ£¬È»ºó·¢³ö 'attach 1283' ÃüÁîÁ¬½Óµ½½ø³Ì±êʶ 1283 Ï嵀 eg2¡£ ÆäËüС¼¼ÇÉ gdb ¿ÉÒÔÈÃÄúͨ¹ýʹÓà shell ÃüÁîÔÚ²»Í˳öµ÷ÊÔ»·¾³µÄÇé¿öÏÂÔËÐÐ shell ÃüÁµ÷ÓÃÐÎʽÊÇ 'shell [commandline]'£¬ÕâÓÐÖúÓÚÔÚµ÷ÊÔʱ¸ü¸ÄÔ´´úÂë¡£ ×îºó£¬ÔÚ³ÌÐòÔËÐÐʱ£¬¿ÉÒÔʹÓà 'set ' ÃüÁîÐ޸ıäÁ¿µÄÖµ¡£ÔÚ gdb ÏÂÔÙ´ÎÔËÐÐ eg1£¬Ê¹ÓÃÃüÁî 'break 7 if diff==0' ÔÚµÚ 7 ÐУ¨½«ÔÚ´Ë´¦¼ÆËã½á¹û£©ÉèÖÃÌõ¼þ¶Ïµã£¬È»ºóÔËÐгÌÐò¡£µ± gdb ÖжÏÖ´ÐÐʱ£¬¿ÉÒÔ½« "diff" ÉèÖóɷÇÁãÖµ£¬Ê¹³ÌÐò¼ÌÐøÔËÐÐÖ±ÖÁ½áÊø£º Breakpoint 1, wib (no1=8, no2=8) at eg1.c:7 7 result = no1 / diff; (gdb) print diff $1 = 0 (gdb) set diff=1 (gdb) continue Continuing. 0 wibed by 16 equals 10 Program exited normally. ½áÊøÓï GNU µ÷ÊÔÆ÷ÊÇËùÓгÌÐòÔ±¹¤¾ß¿âÖеÄÒ»¸ö¹¦Äܷdz£Ç¿´óµÄ¹¤¾ß¡£ÔÚ±¾ÎÄÖУ¬ÎÒÖ»½éÉÜÁË gdb µÄһС²¿·Ö¹¦ÄÜ¡£ÒªÁ˽â¸ü¶à֪ʶ£¬½¨ÒéÄúÔĶÁ GNU µ÷ÊÔÆ÷Êֲᡣ


ÉÏÒ»Ò³ [1] [2] [3] ÏÂÒ»Ò³ 

£¨³ö´¦£ºhttp://www.sheup.com£©


ÉÏÒ»Ò³ [1] [2] [3]