fixed leak
[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 pclose(output);
107 }
108 else
109 {
110 #endif /* HAVE_DLADDR */
111 fprintf(file, " %s\n", strings[i]);
112 #ifdef HAVE_DLADDR
113 }
114 fprintf(file, "\n\e[0m");
115 }
116 else
117 {
118 fprintf(file, " %s\n", strings[i]);
119 }
120 #endif /* HAVE_DLADDR */
121 }
122 free (strings);
123 #else /* !HAVE_BACKTRACE */
124 fprintf(file, "C library does not support backtrace().\n");
125 #endif /* HAVE_BACKTRACE */
126 }
127
128 /**
129 * Implementation of backtrace_t.contains_function
130 */
131 static bool contains_function(private_backtrace_t *this, char *function)
132 {
133 #ifdef HAVE_DLADDR
134 int i;
135
136 for (i = 0; i< this->frame_count; i++)
137 {
138 Dl_info info;
139
140 if (dladdr(this->frames[i], &info) && info.dli_sname)
141 {
142 if (streq(info.dli_sname, function))
143 {
144 return TRUE;
145 }
146 }
147 }
148 #endif /* HAVE_DLADDR */
149 return FALSE;
150 }
151
152 /**
153 * Implementation of backtrace_t.destroy.
154 */
155 static void destroy(private_backtrace_t *this)
156 {
157 free(this);
158 }
159
160 /**
161 * See header
162 */
163 backtrace_t *backtrace_create(int skip)
164 {
165 private_backtrace_t *this;
166 void *frames[50];
167 int frame_count = 0;
168
169 #ifdef HAVE_BACKTRACE
170 frame_count = backtrace(frames, countof(frames));
171 #endif /* HAVE_BACKTRACE */
172 frame_count = max(frame_count - skip, 0);
173 this = malloc(sizeof(private_backtrace_t) + frame_count * sizeof(void*));
174 memcpy(this->frames, frames + skip, frame_count * sizeof(void*));
175 this->frame_count = frame_count;
176
177 this->public.log = (void(*)(backtrace_t*,FILE*))log_;
178 this->public.contains_function = (bool(*)(backtrace_t*, char *function))contains_function;
179 this->public.destroy = (void(*)(backtrace_t*))destroy;
180
181 return &this->public;
182 }
183