现在的位置: 首页 > 综合 > 正文

安装efence库

2019年06月07日 ⁄ 综合 ⁄ 共 1130字 ⁄ 字号 评论关闭

Electric Fence (efence) stops your program on theexact instruction that overruns (or underruns) a malloc() memorybuffer. GDB will then display the source-code line that causes thebug. It works by using the virtual-memory hardware to create ared-zone at the
border of each buffer - touch that, and your programstops. Catch all of those formerly impossible-to-catch overrun bugsthat have been bothering you for years.

efence对于调试动态内存的错误很有效。下面是efence库的安装:

首先,下载源文件 :
点击打开链接

然后,用命令 tar -zxvf electric-fence-2.1.13.tar.gz 解压文件包;

然后,进入electric-fence-2.1.13文件夹,编辑Makefile第九行,根据自己系统man目录的位置,修改为MAN_INSTALL_DIR= /usr/share/man/man3

注:1.Makefile应该不改也可以

        2. 我的系统是openSUSE, 如果在你的系统上可以直接找到/usr/man/man3,那就不用改Makefile了。

最后是普通的安装源代码过程:

make all

sudo make install

make clean

efence库的链接

常用的格式就是在命令行的最后加上 -lefence -lpthread,比如:

  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. int main(void)  
  4. {  
  5.     int *a = (int*)malloc(2*sizeof(int));  
  6.   
  7.     for (int i=0;i<=2;i++) {  
  8.         a[i] = i;  
  9.         printf("%d\n", a[i]);  
  10.     }  
  11.   
  12.     free(a);  
  13.     return 0;  
  14. }  

编译: cc -g3 -Wall -o out_of_bound out_of_bound.c -lefence -lpthread

运行: ./out_of_bound ,看输出结果就知道efence的威力了。

:efence库依赖于pthread(多线程)库

参考资料: 软件调试的艺术P188, man-page,stackoverflow

抱歉!评论已关闭.