Browse code

Added backtrace

git-svn-id: file:///var/lib/svn/clamav-devel/trunk/clamav-devel@625 77e5149b-7576-45b1-b177-96237e5ba77b

Nigel Horne authored on 2004/06/22 18:36:28
Showing 1 changed files
... ...
@@ -1,5 +1,7 @@
1 1
 /*
2
- * $CC $CFLAGS debugm.c -lclamav -lefence (or what ever memory debugger)
2
+ * $CC $CFLAGS -I../.. debugm.c -lclamav -lefence (or what ever memory debugger)
3
+ * If you're going to use HAVE_BACKTRACE, ensure CFLAGS includes -g and doesn't
4
+ * include -fomit-frame-pointer
3 5
  *
4 6
  * njh@bandsman.co.uk
5 7
  */
... ...
@@ -11,6 +13,48 @@
11 11
 #include <malloc.h>
12 12
 #include <clamav.h>
13 13
 #include <sys/resource.h>
14
+#include <signal.h>
15
+#include <features.h>
16
+#include "clamav-config.h"
17
+
18
+#if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 1
19
+/*#define HAVE_BACKTRACE	/* Only tested on Linux... */
20
+#endif
21
+
22
+#ifdef HAVE_BACKTRACE
23
+#include <execinfo.h>
24
+#endif
25
+
26
+static	void	print_trace(void);
27
+static	void	sigsegv(int sig);
28
+
29
+static void
30
+sigsegv(int sig)
31
+{
32
+	signal(SIGSEGV, SIG_DFL);
33
+	print_trace();
34
+	_exit(SIGSEGV);
35
+}
36
+
37
+static void
38
+print_trace(void)
39
+{
40
+#ifdef HAVE_BACKTRACE
41
+	void *array[10];
42
+	size_t size, i;
43
+	char **strings;
44
+
45
+	puts("Segfault caught, backtrace:");
46
+
47
+	size = backtrace(array, 10);
48
+	strings = backtrace_symbols(array, size);
49
+
50
+	for(i = 0; i < size; i++)
51
+		printf("\t%s\n", strings[i]);
52
+
53
+	free(strings);
54
+#endif
55
+}
14 56
 
15 57
 int
16 58
 main(int argc, char **argv)
... ...
@@ -29,6 +73,7 @@ main(int argc, char **argv)
29 29
 		perror("/tmp/mboxtest");
30 30
 		return errno;
31 31
 	}
32
+	signal(SIGSEGV, sigsegv);
32 33
 	while(*++argv) {
33 34
 		int fd = open(*argv, 0);
34 35
 
... ...
@@ -42,5 +87,5 @@ main(int argc, char **argv)
42 42
 	}
43 43
 	puts("Finished - don't forget to rm -rf /tmp/mboxtest");
44 44
 
45
-	exit(0);
45
+	return 0;
46 46
 }