separated backtrace functionality from leak_detective, used in
[strongswan.git] / src / libstrongswan / utils / backtrace.c
1 /*
2 * Copyright (C) 2006-2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * $Id$
16 */
17
18 #define _GNU_SOURCE
19
20 #ifdef HAVE_DLADDR
21 # include <dlfcn.h>
22 #endif /* HAVE_DLADDR */
23
24 #ifdef HAVE_BACKTRACE
25 # include <execinfo.h>
26 #endif /* HAVE_BACKTRACE */
27
28 #include "backtrace.h"
29
30 typedef struct private_backtrace_t private_backtrace_t;
31
32 /**
33 * Private data of an backtrace_t object.
34 */
35 struct private_backtrace_t {
36
37 /**
38 * Public backtrace_t interface.
39 */
40 backtrace_t public;
41
42 /**
43 * Number of stacks frames obtained in stack_frames
44 */
45 int frame_count;
46
47 /**
48 * Recorded stack frames.
49 */
50 void *frames[];
51 };
52
53 /**
54 * Implementation of backtrace_t.log
55 */
56 static void log_(private_backtrace_t *this, FILE *file)
57 {
58 #ifdef HAVE_BACKTRACE
59 size_t i;
60 char **strings;
61
62 strings = backtrace_symbols(this->frames, this->frame_count);
63
64 fprintf(file, " dumping %d stack frame addresses:\n", this->frame_count);
65 for (i = 0; i < this->frame_count; i++)
66 {
67 #ifdef HAVE_DLADDR
68 Dl_info info;
69
70 if (dladdr(this->frames[i], &info))
71 {
72 char cmd[1024];
73 FILE *output;
74 char c;
75 void *ptr = this->frames[i];
76
77 if (strstr(info.dli_fname, ".so"))
78 {
79 ptr = (void*)(this->frames[i] - info.dli_fbase);
80 }
81 snprintf(cmd, sizeof(cmd), "addr2line -e %s %p", info.dli_fname, ptr);
82 if (info.dli_sname)
83 {
84 fprintf(file, " \e[33m%s\e[0m @ %p (\e[31m%s\e[0m+0x%x) [%p]\n",
85 info.dli_fname, info.dli_fbase, info.dli_sname,
86 this->frames[i] - info.dli_saddr, this->frames[i]);
87 }
88 else
89 {
90 fprintf(file, " \e[33m%s\e[0m @ %p [%p]\n", info.dli_fname,
91 info.dli_fbase, this->frames[i]);
92 }
93 fprintf(file, " -> \e[32m");
94 output = popen(cmd, "r");
95 if (output)
96 {
97 while (TRUE)
98 {
99 c = getc(output);
100 if (c == '\n' || c == EOF)
101 {
102 break;
103 }
104 fputc(c, file);
105 }
106 }
107 else
108 {
109 #endif /* HAVE_DLADDR */
110 fprintf(file, " %s\n", strings[i]);
111 #ifdef HAVE_DLADDR
112 }
113 fprintf(file, "\n\e[0m");
114 }
115 else
116 {
117 fprintf(file, " %s\n", strings[i]);
118 }
119 #endif /* HAVE_DLADDR */
120 }
121 free (strings);
122 #else /* !HAVE_BACKTRACE */
123 fprintf(file, "C library does not support backtrace().\n");
124 #endif /* HAVE_BACKTRACE */
125 }
126
127 /**
128 * Implementation of backtrace_t.contains_function
129 */
130 static bool contains_function(private_backtrace_t *this, char *function)
131 {
132 #ifdef HAVE_DLADDR
133 int i;
134
135 for (i = 0; i< this->frame_count; i++)
136 {
137 Dl_info info;
138
139 if (dladdr(this->frames[i], &info) && info.dli_sname)
140 {
141 if (streq(info.dli_sname, function))
142 {
143 return TRUE;
144 }
145 }
146 }
147 #endif /* HAVE_DLADDR */
148 return FALSE;
149 }
150
151 /**
152 * Implementation of backtrace_t.destroy.
153 */
154 static void destroy(private_backtrace_t *this)
155 {
156 free(this);
157 }
158
159 /**
160 * See header
161 */
162 backtrace_t *backtrace_create(int skip)
163 {
164 private_backtrace_t *this;
165 void *frames[50];
166 int frame_count;
167
168 frame_count = backtrace(frames, countof(frames));
169 this = malloc(sizeof(private_backtrace_t) + frame_count * sizeof(void*));
170 this->frame_count = frame_count - skip;
171 memcpy(this->frames, frames + skip, this->frame_count * sizeof(void*));
172
173 this->public.log = (void(*)(backtrace_t*,FILE*))log_;
174 this->public.contains_function = (bool(*)(backtrace_t*, char *function))contains_function;
175 this->public.destroy = (void(*)(backtrace_t*))destroy;
176
177 return &this->public;
178 }
179