Äú¿ÉÒÔÓø÷ÖÖ·½·¨À´¼à¿ØÔËÐÐ×ŵÄÓû§¿Õ¼ä³ÌÐò£º¿ÉÒÔΪÆäÔËÐе÷ÊÔÆ÷²¢µ¥²½µ÷ÊԸóÌÐò£¬Ìí¼Ó´òÓ¡Óï¾ä£¬»òÕßÌí¼Ó¹¤¾ßÀ´·ÖÎö³ÌÐò¡£±¾ÎÄÃèÊöÁ˼¸ÖÖ¿ÉÒÔÓÃÀ´µ÷ÊÔÔÚ Linux ÉÏÔËÐеijÌÐòµÄ·½·¨¡£ÎÒÃǽ«»Ø¹ËËÄÖÖµ÷ÊÔÎÊÌâµÄÇé¿ö£¬ÕâЩÎÊÌâ°üÀ¨¶Î´íÎó£¬ÄÚ´æÒç³öºÍй©£¬»¹ÓйÒÆð¡£
±¾ÎÄÌÖÂÛÁËËÄÖÖµ÷ÊÔ Linux ³ÌÐòµÄÇé¿ö¡£ÔÚµÚ 1 ÖÖÇé¿öÖУ¬ÎÒÃÇʹÓÃÁËÁ½¸öÓÐÄÚ´æ·ÖÅäÎÊÌâµÄÑù±¾³ÌÐò£¬Ê¹Óà MEMWATCH ºÍ Yet Another Malloc Debugger£¨YAMD£©¹¤¾ßÀ´µ÷ÊÔËüÃÇ¡£ÔÚµÚ 2 ÖÖÇé¿öÖУ¬ÎÒÃÇʹÓÃÁË Linux ÖÐµÄ strace ʵÓóÌÐò£¬ËüÄܹ»¸ú×Ùϵͳµ÷ÓúÍÐźţ¬´Ó¶øÕÒ³ö³ÌÐò·¢Éú´íÎóµÄµØ·½¡£ÔÚµÚ 3 ÖÖÇé¿öÖУ¬ÎÒÃÇʹÓà Linux ÄÚºËµÄ Oops ¹¦ÄÜÀ´½â¾ö³ÌÐòµÄ¶Î´íÎ󣬲¢ÏòÄúչʾÈçºÎÉèÖÃÄÚºËÔ´´úÂ뼶µ÷ÊÔÆ÷£¨kernel source level debugger£¬kgdb£©£¬ÒÔʹÓà GNU µ÷ÊÔÆ÷£¨GNU debugger£¬gdb£©À´½â¾öÏàͬµÄÎÊÌ⣻kgdb ³ÌÐòÊÇʹÓô®ÐÐÁ¬½ÓµÄ Linux ÄÚºËÔ¶³Ì gdb¡£ÔÚµÚ 4 ÖÖÇé¿öÖУ¬ÎÒÃÇʹÓà Linux ÉÏÌṩµÄħÊõ¼ü¿Ø˳Ðò£¨magic key sequence£©À´ÏÔʾÒý·¢¹ÒÆðÎÊÌâµÄ×é¼þµÄÐÅÏ¢¡£
³£¼ûµ÷ÊÔ·½·¨
µ±ÄúµÄ³ÌÐòÖаüº¬´íÎóʱ£¬ºÜ¿ÉÄÜÔÚ´úÂëÖÐij´¦ÓÐÒ»¸öÌõ¼þ£¬ÄúÈÏΪËüΪÕ棨true£©£¬µ«Êµ¼ÊÉÏÊǼ٣¨false£©¡£ÕÒ³ö´íÎóµÄ¹ý³ÌÒ²¾ÍÊÇÔÚÕÒ³ö´íÎóºóÍÆ·ÒÔǰһֱȷÐÅΪÕæµÄij¸öÌõ¼þ¹ý³Ì¡£
ÒÔϼ¸¸öʾÀýÊÇÄú¿ÉÄÜÈ·ÐųÉÁ¢µÄÌõ¼þµÄһЩÀàÐÍ£º
ÔÚÔ´´úÂëÖеÄij´¦£¬Ä³±äÁ¿ÓÐÌض¨µÄÖµ¡£
ÔÚ¸ø¶¨µÄµØ·½£¬Ä³¸ö½á¹¹Òѱ»ÕýÈ·ÉèÖá£
¶ÔÓÚ¸ø¶¨µÄ if-then-else Óï¾ä£¬if ²¿·Ö¾ÍÊDZ»Ö´ÐеÄ·¾¶¡£
µ±×ÓÀý³Ì±»µ÷ÓÃʱ£¬¸ÃÀý³ÌÕýÈ·µØ½ÓÊÕµ½ÁËËüµÄ²ÎÊý¡£
ÕÒ³ö´íÎóÒ²¾ÍÊÇҪȷ¶¨ÉÏÊöËùÓÐÇé¿öÊÇ·ñ´æÔÚ¡£Èç¹ûÄúÈ·ÐÅÔÚ×ÓÀý³Ì±»µ÷ÓÃʱij±äÁ¿Ó¦¸ÃÓÐÌض¨µÄÖµ£¬ÄÇô¾Í¼ì²éÒ»ÏÂÇé¿öÊÇ·ñÈç´Ë¡£Èç¹ûÄúÏàÐÅ if ½á¹¹»á±»Ö´ÐУ¬ÄÇôҲ¼ì²éÒ»ÏÂÇé¿öÊÇ·ñÈç´Ë¡£Í¨³££¬ÄúµÄ¼ÙÉ趼»áÊÇÕýÈ·µÄ£¬µ«×îÖÕÄú»áÕÒµ½Óë¼ÙÉè²»·ûµÄÇé¿ö¡£½á¹û£¬Äú¾Í»áÕÒ³ö·¢Éú´íÎóµÄµØ·½¡£
µ÷ÊÔÊÇÄúÎÞ·¨ÌӱܵÄÈÎÎñ¡£½øÐе÷ÊÔÓкܶàÖÖ·½·¨£¬±ÈÈ罫ÏûÏ¢´òÓ¡µ½ÆÁÄ»ÉÏ¡¢Ê¹Óõ÷ÊÔÆ÷£¬»òÖ»ÊÇ¿¼ÂdzÌÐòÖ´ÐеÄÇé¿ö²¢×ÐϸµØ´§Ä¦ÎÊÌâËùÔÚ¡£
ÔÚÐÞÕýÎÊÌâ֮ǰ£¬Äú±ØÐëÕÒ³öËüµÄÔ´Í·¡£¾ÙÀýÀ´Ëµ£¬¶ÔÓڶδíÎó£¬ÄúÐèÒªÁ˽â¶Î´íÎó·¢ÉúÔÚ´úÂëµÄÄÄÒ»ÐС£Ò»µ©Äú·¢ÏÖÁË´úÂëÖгö´íµÄÐУ¬ÇëÈ·¶¨¸Ã·½·¨ÖбäÁ¿µÄÖµ¡¢·½·¨±»µ÷Óõķ½Ê½ÒÔ¼°¹ØÓÚ´íÎóÈçºÎ·¢ÉúµÄÏêϸÇé¿ö¡£Ê¹Óõ÷ÊÔÆ÷½«Ê¹ÕÒ³öËùÓÐÕâЩÐÅÏ¢±äµÃºÜ¼òµ¥¡£Èç¹ûûÓе÷ÊÔÆ÷¿ÉÓã¬Äú»¹¿ÉÒÔʹÓÃÆäËüµÄ¹¤¾ß¡££¨Çë×¢Ò⣬²úÆ·»·¾³ÖпÉÄܲ¢²»Ìṩµ÷ÊÔÆ÷£¬¶øÇÒ Linux ÄÚºËûÓÐÄÚ½¨µÄµ÷ÊÔÆ÷¡££©
ʵÓõÄÄÚ´æºÍÄں˹¤¾ß
Äú¿ÉÒÔʹÓà Linux Éϵĵ÷ÊÔ¹¤¾ß£¬Í¨¹ý¸÷ÖÖ·½Ê½¸ú×ÙÓû§¿Õ¼äºÍÄÚºËÎÊÌâ¡£ÇëʹÓÃÏÂÃæµÄ¹¤¾ßºÍ¼¼ÊõÀ´¹¹½¨ºÍµ÷ÊÔÄúµÄÔ´´úÂ룺
Óû§¿Õ¼ä¹¤¾ß£º
Äڴ湤¾ß£ºMEMWATCH ºÍ YAMD
strace
GNU µ÷ÊÔÆ÷£¨gdb£©
ħÊõ¼ü¿Ø˳Ðò
Äں˹¤¾ß£º
ÄÚºËÔ´´úÂ뼶µ÷ÊÔÆ÷£¨kgdb£©
ÄÚ½¨Äں˵÷ÊÔÆ÷£¨kdb£©
Oops
±¾ÎĽ«ÌÖÂÛÒ»Ààͨ¹ýÈ˹¤¼ì²é´úÂë²»ÈÝÒ×ÕÒµ½µÄÎÊÌ⣬¶øÇÒ´ËÀàÎÊÌâÖ»ÔÚºÜÉÙ¼ûµÄÇé¿öÏ´æÔÚ¡£ÄÚ´æ´íÎóͨ³£ÔÚ¶àÖÖÇé¿öͬʱ´æÔÚʱ³öÏÖ£¬¶øÇÒÄúÓÐʱֻÄÜÔÚ²¿Êð³ÌÐòÖ®ºó²ÅÄÜ·¢ÏÖÄÚ´æ´íÎó¡£
yinzelei ÓÚ 2006-11-10 09:43:01·¢±í:
YAMD
YAMD Èí¼þ°üÓÉ Nate Eldredge ±àд£¬¿ÉÒÔ²éÕÒ C ºÍ C++ Öж¯Ì¬µÄ¡¢ÓëÄÚ´æ·ÖÅäÓйصÄÎÊÌâ¡£ÔÚ׫д±¾ÎÄʱ£¬YAMD µÄ×îа汾Ϊ 0.32¡£ÇëÏÂÔØ yamd-0.32.tar.gz£¨Çë²ÎÔIJο¼×ÊÁÏ£©¡£Ö´ÐÐ make ÃüÁîÀ´¹¹½¨³ÌÐò£»È»ºóÖ´ÐÐ make install ÃüÁî°²×°³ÌÐò²¢ÉèÖù¤¾ß¡£
Ò»µ©ÄúÏÂÔØÁË YAMD Ö®ºó£¬ÇëÔÚ test1.c ÉÏʹÓÃËü¡£Çëɾ³ý #include memwatch.h ²¢¶Ô makefile ½øÐÐÈçÏÂССµÄÐ޸ģºÊ¹Óà YAMD µÄ test1¡£
Çåµ¥ 3 չʾÁËÀ´×Ô test1 É쵀 YAMD µÄÊä³ö¡£
Çåµ¥ 3. ʹÓà YAMD µÄ test1 Êä³ö
YAMD ÏÔʾÎÒÃÇÒѾÊÍ·ÅÁËÄڴ棬¶øÇÒ´æÔÚÄÚ´æй©¡£ÈÃÎÒÃÇÔÚÇåµ¥ 4 ÖÐÁíÒ»¸öÑù±¾³ÌÐòÉÏÊÔÊÔ YAMD¡£
Çåµ¥ 4. ÄÚ´æ´úÂ루test2.c£©
Äú¿ÉÒÔʹÓÃÏÂÃæµÄÃüÁîÀ´Æô¶¯ YAMD£º
Çåµ¥ 5 ÏÔʾÁËÔÚÑù±¾³ÌÐò test2 ÉÏʹÓà YAMD µÃµ½µÄÊä³ö¡£YAMD ¸æËßÎÒÃÇÔÚ for Ñ»·ÖÐÓС°Ô½½ç£¨out-of-bounds£©¡±µÄÇé¿ö¡£
Çåµ¥ 5. ʹÓà YAMD µÄ test2 Êä³ö
MEMWATCH ºÍ YAMD ¶¼ÊǺÜÓÐÓõĵ÷ÊÔ¹¤¾ß£¬ËüÃǵÄʹÓ÷½·¨ÓÐËù²»Í¬¡£¶ÔÓÚ MEMWATCH£¬ÄúÐèÒªÌí¼Ó°üº¬Îļþ memwatch.h ²¢´ò¿ªÁ½¸ö±àÒëʱ¼ä±ê¼Ç¡£¶ÔÓÚÁ´½Ó£¨link£©Óï¾ä£¬YAMD Ö»ÐèÒª -g Ñ¡Ïî¡£
Electric Fence
¶àÊý Linux ·Ö·¢°æ°üº¬Ò»¸ö Electric Fence °ü£¬²»¹ýÄúÒ²¿ÉÒÔÑ¡ÔñÏÂÔØËü¡£Electric Fence ÊÇÒ»¸öÓÉ Bruce Perens ±àдµÄ malloc() µ÷ÊÔ¿â¡£Ëü¾ÍÔÚÄú·ÖÅäÄÚ´æºó·ÖÅäÊܱ£»¤µÄÄÚ´æ¡£Èç¹û´æÔÚ fencepost ´íÎ󣨳¬¹ýÊý×éĩβÔËÐУ©£¬³ÌÐò¾Í»á²úÉú±£»¤´íÎ󣬲¢Á¢¼´½áÊø¡£Í¨¹ý½áºÏ Electric Fence ºÍ gdb£¬Äú¿ÉÒÔ¾«È·µØ¸ú×Ùµ½ÄÄÒ»ÐÐÊÔͼ·ÃÎÊÊܱ£»¤ÄÚ´æ¡£Electric Fence µÄÁíÒ»¸ö¹¦ÄܾÍÊÇÄܹ»¼ì²âÄÚ´æй©¡£
yinzelei ÓÚ 2006-11-10 09:41:20·¢±í:
µÚ 1 ÖÖÇé¿ö£ºÄÚ´æµ÷ÊÔ¹¤¾ß
C ÓïÑÔ×÷Ϊ Linux ϵͳÉϱê×¼µÄ±à³ÌÓïÑÔ¸øÓèÁËÎÒÃǶԶ¯Ì¬ÄÚ´æ·ÖÅäºÜ´óµÄ¿ØÖÆȨ¡£È»¶ø£¬ÕâÖÖ×ÔÓÉ¿ÉÄܻᵼÖÂÑÏÖصÄÄÚ´æ¹ÜÀíÎÊÌ⣬¶øÕâЩÎÊÌâ¿ÉÄܵ¼Ö³ÌÐò±ÀÀ£»òËæʱ¼äµÄÍÆÒƵ¼ÖÂÐÔÄܽµ¼¶¡£
ÄÚ´æй©£¨¼´ malloc() ÄÚ´æÔÚ¶ÔÓ¦µÄ free() µ÷ÓÃÖ´ÐкóÓÀ²»±»ÊÍ·Å£©ºÍ»º³åÇøÒç³ö£¨ÀýÈç¶ÔÒÔÇ°·ÖÅ䵽ijÊý×éµÄÄÚ´æ½øÐÐд²Ù×÷£©ÊÇһЩ³£¼ûµÄÎÊÌ⣬ËüÃÇ¿ÉÄܺÜÄѼì²âµ½¡£ÕâÒ»²¿·Ö½«ÌÖÂÛ¼¸¸öµ÷ÊÔ¹¤¾ß£¬ËüÃǼ«´óµØ¼ò»¯Á˼ì²âºÍÕÒ³öÄÚ´æÎÊÌâµÄ¹ý³Ì¡£
MEMWATCH
MEMWATCH ÓÉ Johan Lindh ±àд£¬ÊÇÒ»¸ö¿ª·ÅÔ´´úÂë C ÓïÑÔÄÚ´æ´íÎó¼ì²â¹¤¾ß£¬Äú¿ÉÒÔ×Ô¼ºÏÂÔØËü£¨Çë²ÎÔı¾ÎĺóÃ沿·ÖµÄ²Î¿¼×ÊÁÏ£©¡£Ö»ÒªÔÚ´úÂëÖÐÌí¼ÓÒ»¸öÍ·Îļþ²¢ÔÚ gcc Óï¾äÖж¨ÒåÁË MEMWATCH Ö®ºó£¬Äú¾Í¿ÉÒÔ¸ú×Ù³ÌÐòÖеÄÄÚ´æй©ºÍ´íÎóÁË¡£MEMWATCH Ö§³Ö ANSI C£¬ËüÌṩ½á¹ûÈÕÖ¾¼Í¼£¬Äܼì²âË«ÖØÊÍ·Å£¨double-free£©¡¢´íÎóÊÍ·Å£¨erroneous free£©¡¢Ã»ÓÐÊͷŵÄÄڴ棨unfreed memory£©¡¢Òç³öºÍÏÂÒçµÈµÈ¡£
Çåµ¥ 1. ÄÚ´æÑù±¾£¨test1.c£©
Çåµ¥ 1 ÖеĴúÂ뽫·ÖÅäÁ½¸ö 512 ×Ö½ÚµÄÄÚ´æ¿é£¬È»ºóÖ¸ÏòµÚÒ»¸öÄÚ´æ¿éµÄÖ¸Õë±»É趨ΪָÏòµÚ¶þ¸öÄÚ´æ¿é¡£½á¹û£¬µÚ¶þ¸öÄÚ´æ¿éµÄµØÖ·¶ªÊ§£¬´Ó¶ø²úÉúÁËÄÚ´æй©¡£
ÏÖÔÚÎÒÃDZàÒëÇåµ¥ 1 µÄ memwatch.c¡£ÏÂÃæÊÇÒ»¸ö makefile ʾÀý£º
µ±ÄúÔËÐÐ test1 ³ÌÐòºó£¬Ëü»áÉú³ÉÒ»¸ö¹ØÓÚй©µÄÄÚ´æµÄ±¨¸æ¡£Çåµ¥ 2 չʾÁËʾÀý memwatch.log Êä³öÎļþ¡£
Çåµ¥ 2. test1 memwatch.log Îļþ
MEMWATCH ΪÄúÏÔʾÕæÕýµ¼ÖÂÎÊÌâµÄÐС£Èç¹ûÄúÊÍ·ÅÒ»¸öÒѾÊͷŹýµÄÖ¸Õ룬Ëü»á¸æËßÄú¡£¶ÔÓÚûÓÐÊͷŵÄÄÚ´æÒ²Ò»Ñù¡£ÈÕÖ¾½áβ²¿·ÖÏÔʾͳ¼ÆÐÅÏ¢£¬°üÀ¨Ð¹Â©Á˶àÉÙÄڴ棬ʹÓÃÁ˶àÉÙÄڴ棬ÒÔ¼°×ܹ²·ÖÅäÁ˶àÉÙÄÚ´æ¡£