利用efence调试内存泄漏

1.What is efence

      It attempts to use the virtual memory facilities of Linux to protect the memory used by malloc and free to halt the program at the point of memory corruption.


2. How to install

    a). Get the source from the address: http://perens.com/FreeSoftware/ElectricFence/

    b). use command tar to decomprss  the file

    c). just make >> make install >> make clean


3. How to use

    a). Get a test file, which can be like below:

#include <stdio.h>
#include <stdlib.h>
int main()
{
        char *ptr = (char *) malloc(1024);
        ptr[0] = 0;
        /* Now write beyond the block */
        ptr[1024] = 0;
        exit(0);
}

    b). Use different way to compile it and check the effect

          >>normal way without efence:

          test way 1:

               gcc -o test test.c

          run command ./test and the result is just fine, no error return.

          test way 2:

           use gdb to run test, it need to be recompilled

                gcc -g -o test test.c 

           run command gdb ./test  then input commandrun the result as below :

         Starting program: /home/test
         [Inferior 1 (process 23732) exited normally]
         Missing separate debuginfos, use: debuginfo-install glibc-2.14.90-24.fc16.9.i686



        >>use the efence library

            test way 1:

            gcc -o test test.c -lefence -lpthread

            run command ./test and the result is below:

            Electric Fence 2.1 Copyright (C) 1987-1998 Bruce Perens.
            段错误(吐核)

            test way 2:

            gcc -g -o test test.c -lefence -lpthread


            run command gdb ./test  then input commandrun the result as below :


            Starting program: /home/test
            [Thread debugging using libthread_db enabled]
            Using host libthread_db library "/lib/libthread_db.so.1".

              Electric Fence 2.1 Copyright (C) 1987-1998 Bruce Perens.

            Program received signal SIGSEGV, Segmentation fault.
            0x0804889d in main () at test.c:8
            8           ptr[1024] = 0;
            Missing separate debuginfos, use: debuginfo-install glibc-2.14.90-24.fc16.9.i686

4.summary

    Use the efence can easily to find out the risk of memory .

          

               



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值