ÀÌ °­Á´ ³ª¿ì´©¸® ¸®´ª½º µ¿È£È¸ÀÇ »ýüÁ¹º´(À¯¼öÇà)´ÔÀÇ gdb °­Á¸¦ Âü°íÇؼ­ Á¦(oprix@hanmail.net)°¡ À籸¼ºÇÑ °ÍÀÔ´Ï´Ù. ÀúÀÚ¿Í Ãâó¸¦ ¹àÈ÷½Ã°í »ç¿ëÇϽñ⠹ٶø´Ï´Ù. - http://security.xmecca.com - [¸»¸Ó¸® : µð¹ö°Å¶õ ¹«¾ùÀΰ¡?] µð¹ö°Å(Debugger)¶õ ÇÁ·Î±×·¥ °³¹ß µµ±¸·Î½á, ÇÁ·Î±×·¥À» °³¹ßÇÏ´Ù°¡ ¿¡·¯°¡ ¹ß»ýÇÏ¸é ¹ß»ý À§Ä¡ ¹× ¹ß»ýÀÌÀ¯¸¦ ½±°Ô ãÀ» ¼ö ÀÖµµ·Ï µµ¿Í ÁÝ´Ï´Ù. [gdb] ¸í·É ¿ä¾à ÇÁ·Î±×·¥ ½ÇÇà°ú ÃßÀû(trace)¿¡ °ü·ÃµÈ ¸í·Éµé --------------------------------------------------------- run ÇöÀçÀÇ Àμö¸¦ »ç¿ëÇÏ¿© ÇÁ·Î±×·¥À» ½ÇÇà run »õ·Î¿î <Àμö>¸¦ °¡Áö°í ÇÁ·Î±×·¥À» ½ÇÇà continue ÇöÀç À§Ä¡¿¡¼­ ÇÁ·Î±×·¥À» °è¼Ó ½ÇÇà½Ãŵ´Ï´Ù. break ¸í·ÉÀÌ ÀÛµ¿ µÈ ´ÙÀ½¿¡ »ç¿ëÇÕ´Ï´Ù. (¾àÀÚ) c, cont next ÇÑ ÁÙ¾¿ ½ÇÇà ½Ãŵ´Ï´Ù. ÀÌ ¶§ ÇÔ¼ö¸¦ Æ÷ÇÔÇÏ°í ÀÖÀ¸¸é ÇÔ¼ö¸¦ ¼öÇà½Ãŵ´Ï´Ù. (¾àÀÚ) n next ÁÙÀ» ½ÇÇà ½Ãŵ´Ï´Ù. step ÇÑ ÁÙ¾¿ ½ÇÇà ½Ãŵ´Ï´Ù. ÀÌ ¶§ ÇÔ¼ö¸¦ Æ÷ÇÔÇÏ°í ÀÖÀ¸¸é ÇÔ¼ö ³»ºÎ·Î µé¾î°¡¼­ ÇÑ ÁÙ¾¿ ½ÇÇàÇÕ´Ï´Ù. (¾àÀÚ) s step ÁÙÀ» ½ÇÇà½Ãŵ´Ï´Ù. break ¶óÀÎ ¹øÈ£¿¡¼­ ÇÁ·Î±×·¥ ½ÇÇàÀ» ¸ØÃß°Ô ÇÕ´Ï´Ù. (dbx) stop at (¾àÀÚ) b break <ÇÔ¼ö ¸í> ÇÔ¼ö ³»ºÎÀÇ Ã¹¹ø° ¶óÀο¡¼­ ÇÁ·Î±×·¥ÀÇ ½ÇÇàÀ» ¸ØÃß°Ô ÇÕ´Ï´Ù. (dbx) stop in <ÇÔ¼ö¸í> quit gdb¸¦ Á¾·á ½Ãŵ´Ï´Ù. ------------------------------------------------------------ µ¥ÀÌŸ¿¡ °ü·ÃµÈ ¸í·Éµé ----------------------------------------------------------- whatis ÁöÁ¤ÇÑ <º¯¼ö>¿¡ °ü·ÃµÈ Á¤º¸¸¦ º¸¿©ÁÝ´Ï´Ù. print ÁöÁ¤µÈ ½ÄÀÇ °ªÀ» º¸¿©ÁÝ´Ï´Ù. (¾àÀÚ) p display ÇöÀç ÁöÁ¤µÈ display ¸í·ÉÀÇ ¸ñ·ÏÀ» º¸¿©ÁÝ´Ï´Ù. list ÇöÀç À§Ä¡¿¡¼­ ¼Ò½º ÆÄÀÏÀÇ ³»¿ëÀ» 10ÁÙ º¸¿©ÁÝ´Ï´Ù. list , <½ÃÀÛÁÙ>°ú <³¡ÁÙ>»çÀÌÀÇ ¼Ò½ºÆÄÀÏ ³»¿ëÀ» º¸¿©ÁÝ´Ï´Ù. ----------------------------------------------------------- gdb »ç¿ë¹ýÀ» ¾Ë±â À§Çؼ­ ¿ì¼± bug°¡ ÀÖ´Â ÇÁ·Î±×·¥À» ÀÛ¼ºÇغ¸ÁÒ. $ vi bugprogram1.c ---------------< bugprogram1.c ³»¿ë>-------------- 1 #include < stdio.h > 2 3 int main(void) 4 { 5 int i; 6 double j; 7 char *bug = NULL; 8 9 10 /* ´ÙÀ½Àº i/2 + i ÀÇ °ªÀ» Ãâ·Â ½ÃÅ°´Â ¹®ÀÌ´Ù. */ 11 /* i °¡ 1 À̸é, j ´Â 1.5 °¡ µÇµµ·Ï § °ÍÀÌ´Ù. */ 12 /* ±×·¯³ª ½ÇÁ¦·Î ±×·¸Áö ¾Ê´Ù. */ 13 14 for( i = 0; i < 5; i++) { 15 j = i/2 + i; 16 printf(" j is %lf \n", j ); 17 } 18 19/* ´ÙÀ½Àº bug º¯¼ö¿¡ hi¸¦ copyÇÏ·Á´Â °ÍÀÌ´Ù. */ 20/* º¯¼ö¸í bug¿¡¼­ ´À³¢°ÚÁö¸¸, ÀϺηÁ bug¸¦ ¸¸µé¾ú´Ù. */ 21/* ¹«¾ùÀϱî ? */ 22 23 strcpy(bug,"hi"); 24 printf("bug is %s \n", bug); 25 26 return 0; 27 } --------------------------------------------- À§ÀÇ ³»¿ëÀ» ÀúÀåÇÏ°í ³ª¼­, $ gcc bugprogram1.c -g -o bugprogram1 $ gcc bugprogram1.c -o bugprogram1_g $ ls -l total 32 -rwxr-xr-x 1 oprix staff 16375 Apr 5 15:53 bugprogram1* -rw-r--r-- 1 oprix staff 578 Apr 5 15:52 bugprogram1.c -rwxr-xr-x 1 oprix staff 11927 Apr 5 15:53 bugprogram1_g* <¼³¸í>------------------ -g option Àº Çü¼ºµÈ ½ÇÇàÈ­ÀÏÀ» °¡Áö°í debugµÉ ¼ö ÀÖ°Ô compile ÇØ ´Þ¶ó´Â ÀÏÁ¾ÀÇ ºÎŹÇÏ´Â optionÀÔ´Ï´Ù. gdb¸¦ ÀÛµ¿½ÃÅ°·Á¸é ÀÌ·¸°Ô compileÀ» ÇØ¾ß ÇÕ´Ï´Ù. -g ¿É¼ÇÀ» ÁÖ°í ÇÑ °Í°ú ¾È ÇÑ °ÍÀ» ºñ±³Çϸé -g ¿É¼ÇÀ» ÁØ°Ô ÆÄÀÏ Å©±â°¡ Å®´Ï´Ù. debug¸¦ À§Çؼ­ ¿©·¯ Äڵ尡 »ðÀԵǰí, ½ÇÁ¦ ¼Ò½ºµµ µé¾î°¡ ÀÖ½À´Ï´Ù. -o optionÀº -o µÚÀÇ È­ÀÏ À̸§À» °¡Áø ½ÇÇàÈ­ÀÏÀ» ¸¸µé¾î ´Þ¶ó¶ó´Â °ÍÀ¸·Î ÀÌ ¿É¼ÇÀ» »ý·«ÇÒ °æ¿ì¿¡ a.out À̶ó´Â ½ÇÇàÆÄÀÏÀÌ »ý¼ºµË´Ï´Ù. À§ÀÇ bugprogram1.c¸¦ compileÇϸé error ¸Þ¼¼Áö°¡ ¾ø½À´Ï´Ù.. ------------------------------------------------------------ $ ./bugprogram1 j is 0.000000 j is 1.000000 j is 3.000000 j is 4.000000 j is 6.000000 Segmentation fault $ <¼³¸í>----------------------------------------------------- bugprogram1 ½ÇÇàÈ­ÀÏÀ» ½ÇÇà½ÃÄ×´õ´Ï ÀÛµ¿µÇ´Ù°¡ Segmentation fault¸¦ ÀÏÀ¸Å°´Â ±º¿ä. ÇÁ·Î±×·¥Àº ¿¡·¯¾øÀÌ ÄÄÆÄÀÏÀÌ Àß µÇ¾ú´Âµ¥... ¾îµð¼­ ¹®Á¦°¡ ÀϾ °É±î¿ä? ----------------------------------------------------------- $ gdb ./bugprogram1 GNU gdb 4.18 Copyright 1998 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-redhat-linux"... (gdb) --<¼³¸í>------------------------------------------------------ ÇÁ·Î±×·¥ À̸§ÀÌ bugprogram1ÀÌ°í ÇöÀç µð·ºÅ丮¿¡ ÀÖÀ¸´Ï ÀÌ·¸°Ô ¼³Á¤ÇÕ´Ï´Ù. ±×³É ½©¿¡¼­ gdb¸¦ Ä¡½Ã°í (gdb) file ./bugprogram1 ÀÌ·¸°Ô ÇÏ´Â ¹æ¹ýµµ ÀÖ½À´Ï´Ù. ÆíÇÑ´ë·Î »ç¿ëÇϼ¼¿ä. ----------------------------------------------------------------- (gdb) list 1 #include < stdio.h > 2 3 int main(void) 4 { 5 int i; 6 double j; 7 char *bug = NULL; 8 9 10 /* ´ÙÀ½Àº i/2 + i ÀÇ °ªÀ» Ãâ·Â ½ÃÅ°´Â ¹®ÀÌ´Ù. */ (gdb) --< ¼³¸í > ---------------------------------------------------- list´Â ¼Ò½º ³»¿ëÀ» º¸¿©ÁÝ´Ï´Ù. l À̶ó°í °£´ÜÇÏ°Ô Ãĵµ ÀÛµ¿ÀÌ µË´Ï´Ù. -------------------------------------------------------------- (gdb) l 4,16 4 { 5 int i; 6 double j; 7 char *bug = NULL; 8 9 10 /* ´ÙÀ½Àº i/2 + i ÀÇ °ªÀ» Ãâ·Â ½ÃÅ°´Â ¹®ÀÌ´Ù. */ 11 /* i °¡ 1 À̸é, j ´Â 1.5 °¡ µÇµµ·Ï § °ÍÀÌ´Ù. */ 12 /* ±×·¯³ª ½ÇÁ¦·Î ±×·¸Áö ¾Ê´Ù. */ 13 14 for( i = 0; i < 5; i++) { 15 j = i/2 + i; 16 printf(" j is %lf \n", j ); --<¼³¸í> -------------------------------------------------------- list <ù¹ø° ÁÙ¹øÈ£>, <³¡ÁÙ¹øÈ£>¸¦ Ä¡¸é À§Ã³·³ º¸ÀÔ´Ï´Ù. --------------------------------------------------------------- (gdb) break 14 Breakpoint 1 at 0x804840d: file bugprogram1.c, line 14. (gdb) run Starting program: /tmp/gdbproject/bugprogram1 Breakpoint 1, main () at bugprogram1.c:14 14 for( i = 0; i < 5; i++) { --<¼³¸í>----------------------------------------------------- ¸ÕÀú °¡Àå ÀǽɵǴ °÷ ºÎÅÍ Ã£±â·Î Çß½À´Ï´Ù. for ¹®ÀÌ Àǽɽº·´±º¿ä. ±×·¡¼­ for ¹®ÀÇ ÁÙ¹øÈ£ÀÎ 14¿¡¼­ break¸¦ °É¾îµÎ¾ú½À´Ï´Ù. break´Â b¶ó´Â ¸í·ÉÀ¸·Î »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù. runÀ¸·Î ½ÇÇàÀ» ½ÃÅ°´Ï ÀÛµ¿µÇ´Ù°¡ 14 ÁÙ¿¡¼­ ¸ØÃß¾ú½À´Ï´Ù. --------------------------------------------------------------- (gdb) step 15 j = i/2 + i; (gdb) step 16 printf(" j is %lf \n", j ); (gdb) step j is 0.000000 j is 1.000000 j is 3.000000 j is 4.000000 j is 6.000000 Program received signal SIGSEGV, Segmentation fault. 0x400787a4 in strcpy () at ../sysdeps/generic/strcpy.c:43 43 ../sysdeps/generic/strcpy.c: ±×·± ÆÄÀÏÀ̳ª µð·ºÅ丮°¡ ¾øÀ½. ----<¼³¸í>---------------------------------------- ÀÌ·± °©ÀÚ±â ÇÁ·Î±×·¥ÀÌ Á¾·á°¡ µÇ¾ú±º¿ä. 16 ¹ø° ÁÙ ´ÙÀ½¿¡ stepÀ» ÇÏ¸é ¾È µÇ°Ú±º¿ä. stepÀº s¸í·ÉÀ¸·Îµµ ¾µ ¼ö ÀÖ½À´Ï´Ù. ´Ù½Ã ½ÃÀÛÇؼ­ Çغ¸ÁÒ. À̹ø¿¡´Â 15¹ø° ÁÙ¿¡ break¸¦ ³Ö¾î º¸ÁÒ. -------------------------------------------------- (gdb) quit The program is running. Exit anyway? (y or n) y $ gdb ./bugprogram1 GNU gdb 4.18 Copyright 1998 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-redhat-linux"... (gdb) b 15 Breakpoint 1 at 0x8048420: file bugprogram1.c, line 15. (gdb) print i $1 = 0 (gdb) print j $2 = 4.8699524093964861e-270 ----<¼³¸í>---------------------------------------- ³»¿ëÀ» º¼¶§ print ¶ó´Â ¸í·ÉÀ» »ç¿ëÇÕ´Ï´Ù. ¾ÆÁ÷ j´Â ¾²·¹±â °ªÀ» °¡Áö°í ÀÖ±º¿ä. print´Â p¶ó´Â ¸í·ÉÀ¸·Îµµ ¾²¼Åµµ µË´Ï´Ù. °è¼ÓÇØ º¸Áö¿ä. -------------------------------------------------- (gdb) s 16 printf(" j is %lf \n", j ); (gdb) s j is 0.000000 Breakpoint 1, main () at bugprogram1.c:15 15 j = i/2 + i; (gdb) print i $3 = 1 (gdb) print j $4 = 0 (gdb) s 16 printf(" j is %lf \n", j ); (gdb) print i $5 = 1 (gdb) print j $6 = 1 (gdb) s j is 1.000000 Breakpoint 1, main () at bugprogram1.c:15 15 j = i/2 + i; (gdb) print i $7 = 2 (gdb) print j $8 = 1 (gdb) s 16 printf(" j is %lf \n", j ); (gdb) print i $9 = 2 (gdb) print j $10 = 3 ----<¼³¸í>---------------------------------------- ÀÚ¼¼È÷ º¸¸é ½ÇÁ¦·Î °ªÀÌ Àû¿ëµÇ´Â °Ç ±× ¹®ÀåÀÌ ½ÇÇàµÈ ´ÙÀ½¿¡ °ªÀÌ Àû¿ëµÇ°í ÀÖÁö¿ä. Áï 15¹ø° ¹®Àå¿¡¼­ ¸ØÃß¾úÀ¸¸é 15¹®ÀåÀº ½ÇÇàÀÌ ¾È µÈ »óÅÂÀÔ´Ï´Ù. ±× ´ÙÀ½¿¡ step ¸í·ÉÀÌ ÀÛµ¿µÇ¾î¾ß °ªÀÌ ¹Ù²îÁö¿ä. ±×·±µ¥ °ªÀ» Àß °üÂûÇØ º¸¸é j´Â 1.0000 ÀÌ ¾Æ´Ï¶ó 1.50000°¡ µÉ ¶§µµ ÀÖ¾î¾ß µÇ´Âµ¥ ¾ø±º¿ä. °è¼Ó Á¤¼ö°ªÀ» °¡Áö°í ÀÖ±º¿ä. j = i/2 + i ; ÀÌ ºÎºÐÀÌ ¹®Á¦°¡ ÀÖ±º¿ä. ÀÌ ºÎºÐÀ» ÀÌ·¸°Ô ¼öÁ¤ÇØ º¸Áö¿ä. j = (double)/2 + (double)i; -------------------------------------------------- $ gcc bugprogram1.c -g -o bugprogram1 $ gdb ./bugprogram1 GNU gdb 4.18 Copyright 1998 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-redhat-linux"... (gdb) b 15 Breakpoint 1 at 0x8048420: file bugprogram1.c, line 15. (gdb) r Starting program: /tmp/gdbproject/./bugprogram1 Breakpoint 1, main () at bugprogram1.c:15 15 j = (double)i/2 + (double)i; (gdb) s 16 printf(" j is %lf \n", j ); (gdb) s j is 0.000000 Breakpoint 1, main () at bugprogram1.c:15 15 j = (double)i/2 + (double)i; (gdb) s 16 printf(" j is %lf \n", j ); (gdb) s j is 1.500000 ----<¼³¸í>---------------------------------------- ÀÚ! ¿øÇÏ´Â °á°ú°¡ ³ª¿ÔÁö¿ä. °ªÀÇ º¯È­¸¦ õõÈ÷ ÃßÀûÇؼ­ ¹®Á¦Á¡À» ÆľÇÇÏ´Â ¹æ¹ýÀÔ´Ï´Ù. ÇϳªÀÇ ¹®Á¦´Â ÇØ°á µÇ¾ú°í continue ¹®À» ÀÌ¿ëÇؼ­ break¸¦ ³ª¿Í º¸Áö¿ä. break °¡ 15¹ø° ÁÙ¿¡ ÀÖ¾úÀ¸´Ï continue 15¶ó°í ÀÔ·ÂÇÕ´Ï´Ù. -------------------------------------------------- (gdb) continue 15 Will ignore next 14 crossings of breakpoint 1. Continuing. j is 0.000000 j is 1.500000 j is 3.000000 j is 4.500000 j is 6.000000 Program received signal SIGSEGV, Segmentation fault. 0x400787a4 in strcpy () at ../sysdeps/generic/strcpy.c:43 43 ../sysdeps/generic/strcpy.c: ±×·± ÆÄÀÏÀ̳ª µð·ºÅ丮°¡ ¾øÀ½. ----<¼³¸í>---------------------------------------- Àü¿¡ ºÃ´ø ¿¡·¯°¡ ³ª¿Ô±º¿ä. ÀÚ ÀÌ°Ç ¾î¶»°Ô ÇÒ±î¿ä? ¿¡·¯ ¸Þ½ÃÁö°¡ strcpy¸¦ °¡¸®Å°°í ÀÖÀ¸´Ï strcpy¶ó°í ´ë·« ¿¹ÃøÀ» Çغ¸Áö¿ä. 23¹ø° ÁÙ¿¡ break¸¦ °É¾îº¸Áö¿ä. ¿¡·¯°¡ ³ª¹Ç·Î gdb¸¦ ´Ù½Ã ½ÃÀÛÇؼ­ break¸¦ °Ì´Ï´Ù. -------------------------------------------------- $ gdb ./bugprogram1 GNU gdb 4.18 Copyright 1998 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-redhat-linux"... (gdb) b 23 Breakpoint 1 at 0x8048456: file bugprogram1.c, line 23. (gdb) r Starting program: /tmp/gdbproject/./bugprogram1 j is 0.000000 j is 1.500000 j is 3.000000 j is 4.500000 j is 6.000000 Breakpoint 1, main () at bugprogram1.c:23 23 strcpy(bug,"hi"); (gdb) p bug $1 = 0x0 (gdb) p *bug Cannot access memory at address 0x0. ---<¼³¸í>--------------------------------------- À½ ¹ö±×¸¦ ãÀº °Å °°±º¿ä. bugÀÇ ÁÖ¼Ò°¡ 0x0Àε¥ ¿©±â¿¡ °ªÀ» º¹»çÇÏ·Á°í ÇßÀ¸´Ï ÀÛµ¿ÀÌ ¾È µÇ´Â °ÅÁö¿ä. 0x0ÁÖ¼Ò´Â access ÇÒ ¼ö ¾ø´Â ÁÖ¼ÒÀε¥.. ±×·¡¼­ ÇÁ·Î±×·¥ÀÌ Á¦´ë·Î ÀÛµ¿ÇÏ·Á¸é bug¿¡ ¸Þ¸ð¸®ÁÖ¼Ò¸¦ ÇÒ´çÇØÁÖ°í »ç¿ëÇÏ¸é µË´Ï´Ù. bug = (char *)calloc(3, sizeof(char)); bug ¼±¾ð ´ÙÀ½¿¡ ÀÌ·¸°Ô ¼³Á¤ÇØÁÖ¸é µÇ°ÚÁö¿ä. -------------------------------------------------------- $ ./bugprogram1 j is 0.000000 j is 1.500000 j is 3.000000 j is 4.500000 j is 6.000000 bug is hi ÀÚ ¹è¿î °É º¹½ÀÇØ º¸¼¼¿ä. break, continue, step, file, print, list ,run ÀúÀÚ¿Í Ãâó¸¦ ¹àÈ÷½Ã°í »ç¿ëÇϽñ⠹ٶø´Ï´Ù.