ºìÁªLinuxÃÅ»§
Linux°ïÖú

LinuxÉÏÕÒ³ö²¢½â¾ö³ÌÐò´íÎó·½·¨£¨ÉÏ£©

·¢²¼Ê±¼ä:2006-11-10 09:40:26À´Ô´:ºìÁª×÷Õß:yinzelei
Äú¿ÉÒÔÓø÷ÖÖ·½·¨À´¼à¿ØÔËÐÐ×ŵÄÓû§¿Õ¼ä³ÌÐò£º¿ÉÒÔΪÆäÔËÐе÷ÊÔÆ÷²¢µ¥²½µ÷ÊԸóÌÐò£¬Ìí¼Ó´òÓ¡Óï¾ä£¬»òÕßÌí¼Ó¹¤¾ßÀ´·ÖÎö³ÌÐò¡£±¾ÎÄÃèÊöÁ˼¸ÖÖ¿ÉÒÔÓÃÀ´µ÷ÊÔÔÚ 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

±¾ÎĽ«ÌÖÂÛÒ»Ààͨ¹ýÈ˹¤¼ì²é´úÂë²»ÈÝÒ×ÕÒµ½µÄÎÊÌ⣬¶øÇÒ´ËÀàÎÊÌâÖ»ÔÚºÜÉÙ¼ûµÄÇé¿öÏ´æÔÚ¡£ÄÚ´æ´íÎóͨ³£ÔÚ¶àÖÖÇé¿öͬʱ´æÔÚʱ³öÏÖ£¬¶øÇÒÄúÓÐʱֻÄÜÔÚ²¿Êð³ÌÐòÖ®ºó²ÅÄÜ·¢ÏÖÄÚ´æ´íÎó¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 2 ÌõÆÀÂÛ

  1. 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¡£

    ÒýÓÃ:
    gcc -g test1.c -o test1


    Çåµ¥ 3 չʾÁËÀ´×Ô test1 É쵀 YAMD µÄÊä³ö¡£

    ÒýÓÃ:
    YAMD version 0.32
    Executable: /usr/src/test/yamd-0.32/test1
    ...
    INFO: Normal allocation of this block
    Address 0x40025e00, size 512
    ...
    INFO: Normal allocation of this block
    Address 0x40028e00, size 512
    ...
    INFO: Normal deallocation of this block
    Address 0x40025e00, size 512
    ...
    ERROR: Multiple freeing At
    free of pointer already freed
    Address 0x40025e00, size 512
    ...
    WARNING: Memory leak
    Address 0x40028e00, size 512
    WARNING: Total memory leaks:
    1 unfreed allocations totaling 512 bytes

    *** Finished at Tue ... 10:07:15 2002
    Allocated a grand total of 1024 bytes 2 allocations
    Average of 512 bytes per allocation
    Max bytes allocated at one time: 1024
    24 K alloced internally / 12 K mapped now / 8 K max
    Virtual program size is 1416 K
    End.


    Çåµ¥ 3. ʹÓà YAMD µÄ test1 Êä³ö

    YAMD ÏÔʾÎÒÃÇÒѾ­ÊÍ·ÅÁËÄڴ棬¶øÇÒ´æÔÚÄÚ´æй©¡£ÈÃÎÒÃÇÔÚÇåµ¥ 4 ÖÐÁíÒ»¸öÑù±¾³ÌÐòÉÏÊÔÊÔ YAMD¡£

    ÒýÓÃ:
    #include "STDLIB.H"
    #include "STDIO.H"

    int main(void)
    {
    char *ptr1;
    char *ptr2;
    char *chptr;
    int i = 1;
    ptr1 = malloc(512);
    ptr2 = malloc(512);
    chptr = (char *)malloc(512);
    for (i; i <= 512; i++) {
    chptr = 'S';
    }
    ptr2 = ptr1;
    free(ptr2);
    free(ptr1);
    free(chptr);
    }


    Çåµ¥ 4. ÄÚ´æ´úÂ루test2.c£©

    Äú¿ÉÒÔʹÓÃÏÂÃæµÄÃüÁîÀ´Æô¶¯ YAMD£º

    ÒýÓÃ:
    ./run-yamd /usr/src/test/test2/test2


    Çåµ¥ 5 ÏÔʾÁËÔÚÑù±¾³ÌÐò test2 ÉÏʹÓà YAMD µÃµ½µÄÊä³ö¡£YAMD ¸æËßÎÒÃÇÔÚ for Ñ­»·ÖÐÓС°Ô½½ç£¨out-of-bounds£©¡±µÄÇé¿ö¡£

    ÒýÓÃ:
    Running /usr/src/test/test2/test2
    Temp output to /tmp/yamd-out.1243
    *********
    ./run-yamd: line 101: 1248 Segmentation fault (core dumped)
    YAMD version 0.32
    Starting run: /usr/src/test/test2/test2
    Executable: /usr/src/test/test2/test2
    Virtual program size is 1380 K
    ...
    INFO: Normal allocation of this block
    Address 0x40025e00, size 512
    ...
    INFO: Normal allocation of this block
    Address 0x40028e00, size 512
    ...
    INFO: Normal allocation of this block
    Address 0x4002be00, size 512
    ERROR: Crash
    ...
    Tried to write address 0x4002c000
    Seems to be part of this block:
    Address 0x4002be00, size 512
    ...
    Address in question is at offset 512 (out of bounds)
    Will dump core after checking heap.
    Done.


    Çåµ¥ 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 µÄÁíÒ»¸ö¹¦ÄܾÍÊÇÄܹ»¼ì²âÄÚ´æй©¡£

  2. 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£©¡¢Òç³öºÍÏÂÒçµÈµÈ¡£

    ÒýÓÃ:
    #include "STDLIB.H"
    #include "STDIO.H"
    #include "memwatch.h"

    int main(void)
    {
    char *ptr1;
    char *ptr2;

    ptr1 = malloc(512);
    ptr2 = malloc(512);

    ptr2 = ptr1;
    free(ptr2);
    free(ptr1);
    }


    Çåµ¥ 1. ÄÚ´æÑù±¾£¨test1.c£©

    Çåµ¥ 1 ÖеĴúÂ뽫·ÖÅäÁ½¸ö 512 ×Ö½ÚµÄÄÚ´æ¿é£¬È»ºóÖ¸ÏòµÚÒ»¸öÄÚ´æ¿éµÄÖ¸Õë±»É趨ΪָÏòµÚ¶þ¸öÄÚ´æ¿é¡£½á¹û£¬µÚ¶þ¸öÄÚ´æ¿éµÄµØÖ·¶ªÊ§£¬´Ó¶ø²úÉúÁËÄÚ´æй©¡£

    ÏÖÔÚÎÒÃDZàÒëÇåµ¥ 1 µÄ memwatch.c¡£ÏÂÃæÊÇÒ»¸ö makefile ʾÀý£º

    ÒýÓÃ:
    test1
    gcc -DMEMWATCH -DMW_STDIO test1.c memwatch
    c -o test1


    µ±ÄúÔËÐÐ test1 ³ÌÐòºó£¬Ëü»áÉú³ÉÒ»¸ö¹ØÓÚй©µÄÄÚ´æµÄ±¨¸æ¡£Çåµ¥ 2 չʾÁËʾÀý memwatch.log Êä³öÎļþ¡£

    ÒýÓÃ:
    MEMWATCH 2.67 Copyright (C) 1992-1999 Johan Lindh

    ...
    double-free: <4> test1.c(15), 0x80517b4 was freed from test1.c(14)
    ...
    unfreed: <2> test1.c(11), 512 bytes at 0x80519e4
    {FE FE FE FE FE FE FE FE FE FE FE FE ..............}

    Memory usage statistics (global):
    N)umber of allocations made: 2
    L)argest memory usage : 1024
    T)otal of all alloc() calls: 1024
    U)nfreed bytes totals : 512


    Çåµ¥ 2. test1 memwatch.log Îļþ

    MEMWATCH ΪÄúÏÔʾÕæÕýµ¼ÖÂÎÊÌâµÄÐС£Èç¹ûÄúÊÍ·ÅÒ»¸öÒѾ­ÊͷŹýµÄÖ¸Õ룬Ëü»á¸æËßÄú¡£¶ÔÓÚûÓÐÊͷŵÄÄÚ´æÒ²Ò»Ñù¡£ÈÕÖ¾½áβ²¿·ÖÏÔʾͳ¼ÆÐÅÏ¢£¬°üÀ¨Ð¹Â©Á˶àÉÙÄڴ棬ʹÓÃÁ˶àÉÙÄڴ棬ÒÔ¼°×ܹ²·ÖÅäÁ˶àÉÙÄÚ´æ¡£