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:%s(%d) [%s:%d] %s", e->host,e->procname,e->pid, e->file,e->line,e->msg);
84 fprintf(stderr, "Bailing out now since there is nothing I can do without a decent amount of memory. Please go fix the memleaks\n");
90 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
94 /* to get the backtrace from the libc */
95 char **backtrace_syms;
97 /* To build the commandline of addr2line */
100 /* to extract the addresses from the backtrace */
104 /* To read the output of addr2line */
106 char line_func[1024], line_pos[1024];
108 /* size (in char) of pointers on this arch */
111 /* To search for the right executable path when not trivial */
112 struct stat stat_buf;
113 char *binary_name = NULL;
115 xbt_assert(e, "Backtrace not setup yet, cannot set it up for display");
117 e->bt_strings = NULL;
119 if (!xbt_binary_name) /* no binary name, nothing to do */
125 /* ignore first one, which is xbt_backtrace_current() */
127 memmove(e->bt, e->bt + 1, (sizeof *e->bt) * e->used);
129 backtrace_syms = backtrace_symbols(e->bt, e->used);
131 /* build the commandline */
132 if (stat(xbt_binary_name, &stat_buf)) {
133 /* Damn. binary not in current dir. We'll have to dig the PATH to find it */
134 for (i = 0; environ[i]; i++) {
135 if (!strncmp("PATH=", environ[i], 5)) {
136 xbt_dynar_t path = xbt_str_split(environ[i] + 5, ":");
140 xbt_dynar_foreach(path, cpt, data) {
142 binary_name = bprintf("%s/%s", data, xbt_binary_name);
143 if (!stat(binary_name, &stat_buf)) {
145 XBT_DEBUG("Looked in the PATH for the binary. Found %s",
150 xbt_dynar_free(&path);
151 if (stat(binary_name, &stat_buf)) {
154 e->bt_strings = xbt_new(char *, 1);
157 bprintf("(binary '%s' not found in the PATH)", xbt_binary_name);
158 free(backtrace_syms);
165 binary_name = xbt_strdup(xbt_binary_name);
169 strlen(ADDR2LINE) + 25 + strlen(binary_name) + 32 * e->used);
171 curr += sprintf(curr, "%s -f -e %s ", ADDR2LINE, binary_name);
174 addrs = xbt_new(char *, e->used);
175 for (i = 0; i < e->used; i++) {
177 /* retrieve this address */
178 XBT_DEBUG("Retrieving address number %d from '%s'", i, backtrace_syms[i]);
179 snprintf(buff, 256, "%s", strchr(backtrace_syms[i], '[') + 1);
180 p = strchr(buff, ']');
182 if (strcmp(buff, "(nil)"))
183 addrs[i] = xbt_strdup(buff);
185 addrs[i] = xbt_strdup("0x0");
186 XBT_DEBUG("Set up a new address: %d, '%s'(%p)", i, addrs[i], addrs[i]);
188 /* Add it to the command line args */
189 curr += sprintf(curr, "%s ", addrs[i]);
191 addr_len = strlen(addrs[0]);
193 /* parse the output and build a new backtrace */
194 e->bt_strings = xbt_new(char *, e->used);
196 XBT_VERB("Fire a first command: '%s'", cmd);
197 pipe = popen(cmd, "r");
199 xbt_die("Cannot fork addr2line to display the backtrace");
202 for (i = 0; i < e->used; i++) {
203 XBT_DEBUG("Looking for symbol %d, addr = '%s'", i, addrs[i]);
204 if (fgets(line_func, 1024, pipe)) {
205 line_func[strlen(line_func) - 1] = '\0';
207 XBT_VERB("Cannot run fgets to look for symbol %d, addr %s", i, addrs[i]);
208 strcpy(line_func, "???");
210 if (fgets(line_pos, 1024, pipe)) {
211 line_pos[strlen(line_pos) - 1] = '\0';
213 XBT_VERB("Cannot run fgets to look for symbol %d, addr %s", i, addrs[i]);
214 strcpy(line_pos, backtrace_syms[i]);
217 if (strcmp("??", line_func) != 0) {
218 XBT_DEBUG("Found static symbol %s() at %s", line_func, line_pos);
220 bprintf("** In %s() at %s", line_func, line_pos);
222 /* Damn. The symbol is in a dynamic library. Let's get wild */
227 long int addr, offset = 0;
234 /* let's look for the offset of this library in our addressing space */
235 maps_name = bprintf("/proc/%d/maps", (int) getpid());
236 maps = fopen(maps_name, "r");
238 addr = strtol(addrs[i], &p, 16);
240 XBT_CRITICAL("Cannot parse backtrace address '%s' (addr=%#lx)",
243 XBT_DEBUG("addr=%s (as string) =%#lx (as number)", addrs[i], addr);
246 long int first, last;
248 if (fgets(maps_buff, 512, maps) == NULL)
251 maps_buff[strlen(maps_buff) - 1] = '\0';
252 XBT_DEBUG("map line: %s", maps_buff);
254 sscanf(maps_buff, "%lx", &first);
255 p = strchr(maps_buff, '-') + 1;
256 sscanf(p, "%lx", &last);
257 if (first < addr && addr < last) {
262 XBT_DEBUG("%#lx in [%#lx-%#lx]", addr, first, last);
264 ("Symbol found, map lines not further displayed (even if looking for next ones)");
273 ("Problem while reading the maps file. Following backtrace will be mangled.");
274 XBT_DEBUG("No dynamic. Static symbol: %s", backtrace_syms[i]);
275 e->bt_strings[i] = bprintf("** In ?? (%s)", backtrace_syms[i]);
279 /* Ok, Found the offset of the maps line containing the searched symbol.
280 We now need to substract this from the address we got from backtrace.
283 addrs[i] = bprintf("0x%0*lx", addr_len - 2, addr - offset);
284 XBT_DEBUG("offset=%#lx new addr=%s", offset, addrs[i]);
286 /* Got it. We have our new address. Let's get the library path and we
288 p = xbt_strdup(backtrace_syms[i]);
290 /* library path not displayed in the map file either... */
292 sprintf(line_func, "??");
294 p2 = strrchr(p, '(');
297 p2 = strrchr(p, ' ');
301 /* Here we go, fire an addr2line up */
302 subcmd = bprintf("%s -f -e %s %s", ADDR2LINE, p, addrs[i]);
304 XBT_VERB("Fire a new command: '%s'", subcmd);
305 subpipe = popen(subcmd, "r");
307 xbt_die("Cannot fork addr2line to display the backtrace");
309 if (fgets(line_func, 1024, subpipe)) {
310 line_func[strlen(line_func) - 1] = '\0';
312 XBT_VERB("Cannot read result of subcommand %s", subcmd);
313 strcpy(line_func, "???");
315 if (fgets(line_pos, 1024, subpipe)) {
316 line_pos[strlen(line_pos) - 1] = '\0';
318 XBT_VERB("Cannot read result of subcommand %s", subcmd);
319 strcpy(line_pos, backtrace_syms[i]);
325 /* check whether the trick worked */
326 if (strcmp("??", line_func)) {
327 XBT_DEBUG("Found dynamic symbol %s() at %s", line_func, line_pos);
329 bprintf("** In %s() at %s", line_func, line_pos);
331 /* damn, nothing to do here. Let's print the raw address */
332 XBT_DEBUG("Dynamic symbol not found. Raw address = %s",
334 e->bt_strings[i] = bprintf("** In ?? at %s", backtrace_syms[i]);
340 /* Mask the bottom of the stack */
341 if (!strncmp("main", line_func, strlen("main")) ||
342 !strncmp("xbt_thread_context_wrapper", line_func,
343 strlen("xbt_thread_context_wrapper"))
344 || !strncmp("smx_ctx_sysv_wrapper", line_func,
345 strlen("smx_ctx_sysv_wrapper"))) {
348 for (j = i + 1; j < e->used; j++)
353 ("xbt_thread_context_wrapper", line_func,
354 strlen("xbt_thread_context_wrapper"))) {
355 free(e->bt_strings[i]);
356 e->bt_strings[i] = xbt_strdup("** (in a separate thread)");
364 free(backtrace_syms);