[Assorted-commits] SF.net SVN: assorted:[1062] sandbox/trunk/src/nix
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-11-06 09:16:57
|
Revision: 1062 http://assorted.svn.sourceforge.net/assorted/?rev=1062&view=rev Author: yangzhang Date: 2008-11-06 09:16:47 +0000 (Thu, 06 Nov 2008) Log Message: ----------- added the btmalloc debugging tool Added Paths: ----------- sandbox/trunk/src/nix/btmalloc/ sandbox/trunk/src/nix/btmalloc/backtrace.h sandbox/trunk/src/nix/btmalloc/btmalloc.c sandbox/trunk/src/nix/btmalloc/run.bash sandbox/trunk/src/nix/btmalloc/test.cc Added: sandbox/trunk/src/nix/btmalloc/backtrace.h =================================================================== --- sandbox/trunk/src/nix/btmalloc/backtrace.h (rev 0) +++ sandbox/trunk/src/nix/btmalloc/backtrace.h 2008-11-06 09:16:47 UTC (rev 1062) @@ -0,0 +1,65 @@ +#define _GNU_SOURCE +#include <stdio.h> +#include <stdlib.h> +#include <dlfcn.h> +#include <execinfo.h> +#include <signal.h> +#include <bfd.h> +#include <unistd.h> + +#define MAX_FRAMES (20) + +/* globals retained across calls to resolve. */ +static bfd* abfd = 0; +static asymbol **syms = 0; +static asection *text = 0; + +static void resolve(char *address) { + if (!abfd) { + char ename[1024]; + int l = readlink("/proc/self/exe",ename,sizeof(ename)); + if (l == -1) { + perror("failed to find executable\n"); + return; + } + ename[l] = 0; + + bfd_init(); + + abfd = bfd_openr(ename, 0); + if (!abfd) { + perror("bfd_openr failed: "); + return; + } + /* oddly, this is required for it to work... */ + bfd_check_format(abfd,bfd_object); + + unsigned storage_needed = bfd_get_symtab_upper_bound(abfd); + syms = (asymbol **) malloc(storage_needed); + + text = bfd_get_section_by_name(abfd, ".text"); + } + long offset = ((long)address) - text->vma; + if (offset > 0) { + const char *file; + const char *func; + unsigned line; + if (bfd_find_nearest_line(abfd, text, syms, offset, &file, &func, &line) && file) + printf("file: %s, line: %u, func %s\n",file,line,func); + } +} + +static void print_trace() { + void *array[MAX_FRAMES]; + size_t size; + size_t i; + void *approx_text_end = (void*) ((128+100) * 2<<20); + + size = backtrace (array, MAX_FRAMES); + printf ("Obtained %zd stack frames.\n", size); + for (i = 0; i < size; i++) { + if (array[i] < approx_text_end) { + resolve(array[i]); + } + } +} Copied: sandbox/trunk/src/nix/btmalloc/btmalloc.c (from rev 1061, sandbox/trunk/src/nix/preload/interposer.c) =================================================================== --- sandbox/trunk/src/nix/btmalloc/btmalloc.c (rev 0) +++ sandbox/trunk/src/nix/btmalloc/btmalloc.c 2008-11-06 09:16:47 UTC (rev 1062) @@ -0,0 +1,24 @@ +#include "backtrace.c" + +#define _GNU_SOURCE +#include <stdio.h> +#include <dlfcn.h> + +void * +malloc(size_t sz) +{ + // Find and cache the next malloc (in our example it should be the "real" + // malloc). + static void * (*func)(); + if (!func) + func = (void *(*)()) dlsym(RTLD_NEXT, "malloc"); + + void *p = func(sz); + + if (!p) { + printf("malloc failed!\n"); + print_trace(); + } + + return p; +} Added: sandbox/trunk/src/nix/btmalloc/run.bash =================================================================== --- sandbox/trunk/src/nix/btmalloc/run.bash (rev 0) +++ sandbox/trunk/src/nix/btmalloc/run.bash 2008-11-06 09:16:47 UTC (rev 1062) @@ -0,0 +1,3 @@ +g++ -Wall -g3 -o test test.cc +gcc -Wall -lbfd -ldl -fPIC -shared -o btmalloc.so btmalloc.c #backtrace.c +LD_PRELOAD=./btmalloc.so ./test Property changes on: sandbox/trunk/src/nix/btmalloc/run.bash ___________________________________________________________________ Added: svn:executable + * Added: sandbox/trunk/src/nix/btmalloc/test.cc =================================================================== --- sandbox/trunk/src/nix/btmalloc/test.cc (rev 0) +++ sandbox/trunk/src/nix/btmalloc/test.cc 2008-11-06 09:16:47 UTC (rev 1062) @@ -0,0 +1,3 @@ +void foo() { operator new(-1); } +void bar() { foo(); } +int main() { bar(); return 0; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |