Electric Fence

Know How

https://elinux.org/Electric_Fence


Download Web site

https://launchpad.net/ubuntu/zesty/amd64/electric-fence/2.2.5

https://launchpad.net/ubuntu/+source/electric-fence


Summary

 Electric Fence is a debugger that uses virtual memory hardware to detect
 illegal memory accesses. It can detect two common programming bugs: software
 that overruns or underruns the boundaries of a malloc() memory allocation,
 and software that touches a memory allocation that has been released by
 free().
 
 Unlike other malloc() debuggers, Electric Fence will detect read accesses as
 well as writes, and it will stop and pinpoint the exact instruction that
 causes an error. It is not as thorough as Purify, however.
 
 In order to debug a program it needs to be linked with Electric Fence's
 library or dynamic linking needs to be used; README.Debian explains that in
 detail.


Install

(just use the pre-build package should be ok)

$ sudo dpkg -i electric-fence_2.2.5_amd64.deb 


then, libefence.a & libefence.so.0.0 will lay in /usr/lib

Usage

compile with libefence.so,

$ gcc -g -Wall badmem.c -o badmem -lefence

execute the ELF, (no need to generate core file, so no need to set with cmd `$ ulimit -c unlimited`)

$ ./badmem 

  Electric Fence 2.2 Copyright (C) 1987-1999 Bruce Perens <bruce@perens.com>
LITTLE: abcde
Segmentation fault (core dumped)

debug with gdb,

$ gdb badmem 
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from badmem...done.
(gdb) r
Starting program: /home/rescure/any_Xplore/linux_driver/gnu_linux_program/badmem 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

  Electric Fence 2.2 Copyright (C) 1987-1999 Bruce Perens <bruce@perens.com>
LITTLE: abcde

Program received signal SIGSEGV, Segmentation fault.
__strcpy_sse2_unaligned () at ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S:650
650    ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S: No such file or directory.   // (don't care this error)
(gdb) where 
#0  __strcpy_sse2_unaligned () at ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S:650
#1  0x0000000000400853 in main () at badmem.c:27
(gdb) quit
A debugging session is active.

    Inferior 1 [process 32374] will be killed.

Quit anyway? (y or n) y
$ 


(over)

原文地址:https://www.cnblogs.com/gaowengang/p/13063881.html