2 * Copyright (C) 2006-2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
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>.
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
20 #endif /* HAVE_DLADDR */
23 # include <execinfo.h>
24 #endif /* HAVE_BACKTRACE */
28 #include "backtrace.h"
30 typedef struct private_backtrace_t private_backtrace_t
;
33 * Private data of an backtrace_t object.
35 struct private_backtrace_t
{
38 * Public backtrace_t interface.
43 * Number of stacks frames obtained in stack_frames
48 * Recorded stack frames.
53 METHOD(backtrace_t
, log_
, void,
54 private_backtrace_t
*this, FILE *file
, bool detailed
)
60 strings
= backtrace_symbols(this->frames
, this->frame_count
);
62 fprintf(file
, " dumping %d stack frame addresses:\n", this->frame_count
);
63 for (i
= 0; i
< this->frame_count
; i
++)
68 if (dladdr(this->frames
[i
], &info
))
73 void *ptr
= this->frames
[i
];
75 if (strstr(info
.dli_fname
, ".so"))
77 ptr
= (void*)(this->frames
[i
] - info
.dli_fbase
);
81 fprintf(file
, " \e[33m%s\e[0m @ %p (\e[31m%s\e[0m+0x%tx) [%p]\n",
82 info
.dli_fname
, info
.dli_fbase
, info
.dli_sname
,
83 this->frames
[i
] - info
.dli_saddr
, this->frames
[i
]);
87 fprintf(file
, " \e[33m%s\e[0m @ %p [%p]\n", info
.dli_fname
,
88 info
.dli_fbase
, this->frames
[i
]);
92 fprintf(file
, " -> \e[32m");
93 snprintf(cmd
, sizeof(cmd
), "addr2line -e %s %p",
95 output
= popen(cmd
, "r");
101 if (c
== '\n' || c
== EOF
)
111 #endif /* HAVE_DLADDR */
112 fprintf(file
, " %s\n", strings
[i
]);
115 fprintf(file
, "\n\e[0m");
120 fprintf(file
, " %s\n", strings
[i
]);
122 #endif /* HAVE_DLADDR */
125 #else /* !HAVE_BACKTRACE */
126 fprintf(file
, "C library does not support backtrace().\n");
127 #endif /* HAVE_BACKTRACE */
130 METHOD(backtrace_t
, contains_function
, bool,
131 private_backtrace_t
*this, char *function
[], int count
)
136 for (i
= 0; i
< this->frame_count
; i
++)
140 if (dladdr(this->frames
[i
], &info
) && info
.dli_sname
)
142 for (j
= 0; j
< count
; j
++)
144 if (streq(info
.dli_sname
, function
[j
]))
151 #endif /* HAVE_DLADDR */
155 METHOD(backtrace_t
, equals
, bool,
156 private_backtrace_t
*this, backtrace_t
*other_public
)
158 private_backtrace_t
*other
= (private_backtrace_t
*)other_public
;
165 if (this->frame_count
!= other
->frame_count
)
169 for (i
= 0; i
< this->frame_count
; i
++)
171 if (this->frames
[i
] != other
->frames
[i
])
183 /** implements enumerator_t */
185 /** reference to backtrace */
186 private_backtrace_t
*bt
;
187 /** current position */
189 } frame_enumerator_t
;
191 METHOD(enumerator_t
, frame_enumerate
, bool,
192 frame_enumerator_t
*this, void **addr
)
194 if (this->i
< this->bt
->frame_count
)
196 *addr
= this->bt
->frames
[this->i
++];
202 METHOD(backtrace_t
, create_frame_enumerator
, enumerator_t
*,
203 private_backtrace_t
*this)
205 frame_enumerator_t
*enumerator
;
209 .enumerate
= (void*)_frame_enumerate
,
210 .destroy
= (void*)free
,
214 return &enumerator
->public;
217 METHOD(backtrace_t
, destroy
, void,
218 private_backtrace_t
*this)
226 backtrace_t
*backtrace_create(int skip
)
228 private_backtrace_t
*this;
232 #ifdef HAVE_BACKTRACE
233 frame_count
= backtrace(frames
, countof(frames
));
234 #endif /* HAVE_BACKTRACE */
235 frame_count
= max(frame_count
- skip
, 0);
236 this = malloc(sizeof(private_backtrace_t
) + frame_count
* sizeof(void*));
237 memcpy(this->frames
, frames
+ skip
, frame_count
* sizeof(void*));
238 this->frame_count
= frame_count
;
240 this->public = (backtrace_t
) {
242 .contains_function
= _contains_function
,
244 .create_frame_enumerator
= _create_frame_enumerator
,
248 return &this->public;