1 /* backtrace_linux - backtrace displaying on linux platform */
2 /* This file is included by ex.c on need (have execinfo.h, popen & addrline)*/
4 /* Copyright (c) 2008, 2009, 2010. The SimGrid Team.
5 * All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
10 /* This file is to be included in ex.c, so the following headers are not mandatory, but it's to make sure that eclipse see them too */
13 #include "xbt/module.h" /* xbt_binary_name */
14 #include "xbt_modinter.h" /* backtrace initialization headers */
15 /* end of "useless" inclusions */
17 extern char **environ; /* the environment, as specified by the opengroup */
19 /* Module creation/destruction: nothing to do on linux */
20 void xbt_backtrace_preinit(void)
24 void xbt_backtrace_postexit(void)
34 static _Unwind_Reason_Code
35 backtrace_helper (struct _Unwind_Context *ctx, void *a)
37 struct trace_arg *arg = a;
39 /* We are first called with address in the __backtrace function.
43 arg->array[arg->cnt] = (void *) _Unwind_GetIP(ctx);
45 /* Check whether we make any progress. */
46 if (arg->cnt > 0 && arg->array[arg->cnt - 1] == arg->array[arg->cnt])
47 return _URC_END_OF_STACK;
49 if (++arg->cnt == arg->size)
50 return _URC_END_OF_STACK;
51 return _URC_NO_REASON;
54 /** @brief reimplementation of glibc backtrace based directly on gcc library, without implicit malloc
56 * See http://webloria.loria.fr/~quinson/blog/2012/0208/system_programming_fun_in_SimGrid/
57 * for the motivation behind this function
60 int xbt_backtrace_no_malloc(void **array, int size) {
63 for(i=0; i < size; i++)
66 struct trace_arg arg = { .array = array, .size = size, .cnt = -1 };
69 _Unwind_Backtrace(backtrace_helper, &arg);
71 /* _Unwind_Backtrace on IA-64 seems to put NULL address above
72 _start. Fix it up here. */
73 if (arg.cnt > 1 && arg.array[arg.cnt - 1] == NULL)
75 return arg.cnt != -1 ? arg.cnt : 0;
78 void xbt_backtrace_current(xbt_ex_t * e)
80 e->used = backtrace((void **) e->bt, XBT_BACKTRACE_SIZE);
82 fprintf(stderr, "The backtrace() function failed, which probably means that the memory is exhausted. Here is a crude dump of the exception that I was trying to build:");
83 fprintf(stderr, "%s(%d) [%s:%d] %s",
84 e->procname, e->pid, e->file, e->line, e->msg);
85 fprintf(stderr, "Bailing out now since there is nothing I can do without a decent amount of memory. Please go fix the memleaks\n");
91 void xbt_ex_setup_backtrace(xbt_ex_t * e) //FIXME: This code could be greatly improved/simplifyied with http://cairo.sourcearchive.com/documentation/1.9.4/backtrace-symbols_8c-source.html
95 /* to get the backtrace from the libc */
96 char **backtrace_syms;
98 /* To build the commandline of addr2line */
101 /* to extract the addresses from the backtrace */
105 /* To read the output of addr2line */
107 char line_func[1024], line_pos[1024];
109 /* size (in char) of pointers on this arch */
112 /* To search for the right executable path when not trivial */
113 struct stat stat_buf;
114 char *binary_name = NULL;
116 xbt_assert(e, "Backtrace not setup yet, cannot set it up for display");
118 e->bt_strings = NULL;
120 if (xbt_binary_name == NULL) /* no binary name, nothing to do */
126 /* ignore first one, which is xbt_backtrace_current() */
128 memmove(e->bt, e->bt + 1, (sizeof *e->bt) * e->used);
130 backtrace_syms = backtrace_symbols(e->bt, e->used);
132 /* build the commandline */
133 if (stat(xbt_binary_name, &stat_buf)) {
134 /* Damn. binary not in current dir. We'll have to dig the PATH to find it */
135 for (i = 0; environ[i]; i++) {
136 if (!strncmp("PATH=", environ[i], 5)) {
137 xbt_dynar_t path = xbt_str_split(environ[i] + 5, ":");
141 xbt_dynar_foreach(path, cpt, data) {
143 binary_name = bprintf("%s/%s", data, xbt_binary_name);
144 if (!stat(binary_name, &stat_buf)) {
146 XBT_DEBUG("Looked in the PATH for the binary. Found %s",
151 xbt_dynar_free(&path);
152 if (stat(binary_name, &stat_buf)) {
155 e->bt_strings = xbt_new(char *, 1);
158 bprintf("(binary '%s' not found in the PATH)", xbt_binary_name);
159 free(backtrace_syms);
166 binary_name = xbt_strdup(xbt_binary_name);
170 strlen(ADDR2LINE) + 25 + strlen(binary_name) + 32 * e->used);
172 curr += sprintf(curr, "%s -f -e %s ", ADDR2LINE, binary_name);
175 addrs = xbt_new(char *, e->used);
176 for (i = 0; i < e->used; i++) {
178 /* retrieve this address */
179 XBT_DEBUG("Retrieving address number %d from '%s'", i, backtrace_syms[i]);
180 snprintf(buff, 256, "%s", strchr(backtrace_syms[i], '[') + 1);
181 p = strchr(buff, ']');
183 if (strcmp(buff, "(nil)"))
184 addrs[i] = xbt_strdup(buff);
186 addrs[i] = xbt_strdup("0x0");
187 XBT_DEBUG("Set up a new address: %d, '%s'(%p)", i, addrs[i], addrs[i]);
189 /* Add it to the command line args */
190 curr += sprintf(curr, "%s ", addrs[i]);
192 addr_len = strlen(addrs[0]);
194 /* parse the output and build a new backtrace */
195 e->bt_strings = xbt_new(char *, e->used);
197 XBT_VERB("Fire a first command: '%s'", cmd);
198 pipe = popen(cmd, "r");
200 xbt_die("Cannot fork addr2line to display the backtrace");
203 for (i = 0; i < e->used; i++) {
204 XBT_DEBUG("Looking for symbol %d, addr = '%s'", i, addrs[i]);
205 if (fgets(line_func, 1024, pipe)) {
206 line_func[strlen(line_func) - 1] = '\0';
208 XBT_VERB("Cannot run fgets to look for symbol %d, addr %s", i, addrs[i]);
209 strcpy(line_func, "???");
211 if (fgets(line_pos, 1024, pipe)) {
212 line_pos[strlen(line_pos) - 1] = '\0';
214 XBT_VERB("Cannot run fgets to look for symbol %d, addr %s", i, addrs[i]);
215 strcpy(line_pos, backtrace_syms[i]);
218 if (strcmp("??", line_func) != 0) {
219 XBT_DEBUG("Found static symbol %s() at %s", line_func, line_pos);
221 bprintf("** In %s() at %s", line_func, line_pos);
223 /* Damn. The symbol is in a dynamic library. Let's get wild */
228 long int addr, offset = 0;
235 /* let's look for the offset of this library in our addressing space */
236 maps_name = bprintf("/proc/%d/maps", (int) getpid());
237 maps = fopen(maps_name, "r");
239 addr = strtol(addrs[i], &p, 16);
241 XBT_CRITICAL("Cannot parse backtrace address '%s' (addr=%#lx)",
244 XBT_DEBUG("addr=%s (as string) =%#lx (as number)", addrs[i], addr);
247 long int first, last;
249 if (fgets(maps_buff, 512, maps) == NULL)
252 maps_buff[strlen(maps_buff) - 1] = '\0';
253 XBT_DEBUG("map line: %s", maps_buff);
255 sscanf(maps_buff, "%lx", &first);
256 p = strchr(maps_buff, '-') + 1;
257 sscanf(p, "%lx", &last);
258 if (first < addr && addr < last) {
263 XBT_DEBUG("%#lx in [%#lx-%#lx]", addr, first, last);
265 ("Symbol found, map lines not further displayed (even if looking for next ones)");
274 ("Problem while reading the maps file. Following backtrace will be mangled.");
275 XBT_DEBUG("No dynamic. Static symbol: %s", backtrace_syms[i]);
276 e->bt_strings[i] = bprintf("** In ?? (%s)", backtrace_syms[i]);
280 /* Ok, Found the offset of the maps line containing the searched symbol.
281 We now need to substract this from the address we got from backtrace.
284 addrs[i] = bprintf("0x%0*lx", addr_len - 2, addr - offset);
285 XBT_DEBUG("offset=%#lx new addr=%s", offset, addrs[i]);
287 /* Got it. We have our new address. Let's get the library path and we
289 p = xbt_strdup(backtrace_syms[i]);
291 /* library path not displayed in the map file either... */
293 sprintf(line_func, "??");
295 p2 = strrchr(p, '(');
298 p2 = strrchr(p, ' ');
302 /* Here we go, fire an addr2line up */
303 subcmd = bprintf("%s -f -e %s %s", ADDR2LINE, p, addrs[i]);
305 XBT_VERB("Fire a new command: '%s'", subcmd);
306 subpipe = popen(subcmd, "r");
308 xbt_die("Cannot fork addr2line to display the backtrace");
310 if (fgets(line_func, 1024, subpipe)) {
311 line_func[strlen(line_func) - 1] = '\0';
313 XBT_VERB("Cannot read result of subcommand %s", subcmd);
314 strcpy(line_func, "???");
316 if (fgets(line_pos, 1024, subpipe)) {
317 line_pos[strlen(line_pos) - 1] = '\0';
319 XBT_VERB("Cannot read result of subcommand %s", subcmd);
320 strcpy(line_pos, backtrace_syms[i]);
326 /* check whether the trick worked */
327 if (strcmp("??", line_func)) {
328 XBT_DEBUG("Found dynamic symbol %s() at %s", line_func, line_pos);
330 bprintf("** In %s() at %s", line_func, line_pos);
332 /* damn, nothing to do here. Let's print the raw address */
333 XBT_DEBUG("Dynamic symbol not found. Raw address = %s",
335 e->bt_strings[i] = bprintf("** In ?? at %s", backtrace_syms[i]);
341 /* Mask the bottom of the stack */
342 if (!strncmp("main", line_func, strlen("main")) ||
343 !strncmp("xbt_thread_context_wrapper", line_func,
344 strlen("xbt_thread_context_wrapper"))
345 || !strncmp("smx_ctx_sysv_wrapper", line_func,
346 strlen("smx_ctx_sysv_wrapper"))) {
349 for (j = i + 1; j < e->used; j++)
354 ("xbt_thread_context_wrapper", line_func,
355 strlen("xbt_thread_context_wrapper"))) {
356 free(e->bt_strings[i]);
357 e->bt_strings[i] = xbt_strdup("** (in a separate thread)");
365 free(backtrace_syms);