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-2015. 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. */
13 /* 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 */
17 #include "xbt/module.h" /* xbt_binary_name */
18 #include "xbt_modinter.h" /* backtrace initialization headers */
20 #define UNW_LOCAL_ONLY
21 #include <libunwind.h>
23 /* end of "useless" inclusions */
25 extern char **environ; /* the environment, as specified by the opengroup */
27 /* Module creation/destruction: nothing to do on linux */
28 void xbt_backtrace_preinit(void)
32 void xbt_backtrace_postexit(void)
42 static _Unwind_Reason_Code
43 backtrace_helper (struct _Unwind_Context *ctx, void *a)
45 struct trace_arg *arg = a;
47 /* We are first called with address in the __backtrace function.
51 arg->array[arg->cnt] = (void *) _Unwind_GetIP(ctx);
53 /* Check whether we make any progress. */
54 if (arg->cnt > 0 && arg->array[arg->cnt - 1] == arg->array[arg->cnt])
55 return _URC_END_OF_STACK;
57 if (++arg->cnt == arg->size)
58 return _URC_END_OF_STACK;
59 return _URC_NO_REASON;
62 /** @brief reimplementation of glibc backtrace based directly on gcc library, without implicit malloc
64 * See http://webloria.loria.fr/~quinson/blog/2012/0208/system_programming_fun_in_SimGrid/
65 * for the motivation behind this function
68 int xbt_backtrace_no_malloc(void **array, int size) {
71 for(i=0; i < size; i++)
74 struct trace_arg arg = { .array = array, .size = size, .cnt = -1 };
77 _Unwind_Backtrace(backtrace_helper, &arg);
79 /* _Unwind_Backtrace on IA-64 seems to put NULL address above
80 _start. Fix it up here. */
81 if (arg.cnt > 1 && arg.array[arg.cnt - 1] == NULL)
83 return arg.cnt != -1 ? arg.cnt : 0;
86 void xbt_backtrace_current(xbt_ex_t * e)
88 e->used = backtrace((void **) e->bt, XBT_BACKTRACE_SIZE);
90 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:");
91 fprintf(stderr, "%s(%d) [%s:%d] %s",
92 e->procname, e->pid, e->file, e->line, e->msg);
93 fprintf(stderr, "Bailing out now since there is nothing I can do without a decent amount of memory. Please go fix the memleaks\n");
99 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
103 /* to get the backtrace from the libc */
104 char **backtrace_syms;
106 /* To build the commandline of addr2line */
109 /* to extract the addresses from the backtrace */
113 /* To read the output of addr2line */
115 char line_func[1024], line_pos[1024];
117 /* size (in char) of pointers on this arch */
120 /* To search for the right executable path when not trivial */
121 struct stat stat_buf;
122 char *binary_name = NULL;
124 xbt_assert(e, "Backtrace not setup yet, cannot set it up for display");
126 e->bt_strings = NULL;
128 if (xbt_binary_name == NULL) /* no binary name, nothing to do */
134 /* ignore first one, which is xbt_backtrace_current() */
136 memmove(e->bt, e->bt + 1, (sizeof *e->bt) * e->used);
138 backtrace_syms = backtrace_symbols(e->bt, e->used);
140 /* build the commandline */
141 if (stat(xbt_binary_name, &stat_buf)) {
142 /* Damn. binary not in current dir. We'll have to dig the PATH to find it */
143 for (i = 0; environ[i]; i++) {
144 if (!strncmp("PATH=", environ[i], 5)) {
145 xbt_dynar_t path = xbt_str_split(environ[i] + 5, ":");
149 xbt_dynar_foreach(path, cpt, data) {
151 binary_name = bprintf("%s/%s", data, xbt_binary_name);
152 if (!stat(binary_name, &stat_buf)) {
154 XBT_DEBUG("Looked in the PATH for the binary. Found %s",
159 xbt_dynar_free(&path);
160 if (stat(binary_name, &stat_buf)) {
163 e->bt_strings = xbt_new(char *, 1);
166 bprintf("(binary '%s' not found in the PATH)", xbt_binary_name);
167 free(backtrace_syms);
174 binary_name = xbt_strdup(xbt_binary_name);
178 strlen(ADDR2LINE) + 25 + strlen(binary_name) + 32 * e->used);
180 curr += sprintf(curr, "%s -f -e %s ", ADDR2LINE, binary_name);
183 addrs = xbt_new(char *, e->used);
184 for (i = 0; i < e->used; i++) {
186 /* retrieve this address */
187 XBT_DEBUG("Retrieving address number %d from '%s'", i, backtrace_syms[i]);
188 snprintf(buff, 256, "%s", strchr(backtrace_syms[i], '[') + 1);
189 p = strchr(buff, ']');
191 if (strcmp(buff, "(nil)"))
192 addrs[i] = xbt_strdup(buff);
194 addrs[i] = xbt_strdup("0x0");
195 XBT_DEBUG("Set up a new address: %d, '%s'(%p)", i, addrs[i], addrs[i]);
197 /* Add it to the command line args */
198 curr += sprintf(curr, "%s ", addrs[i]);
200 addr_len = strlen(addrs[0]);
202 /* parse the output and build a new backtrace */
203 e->bt_strings = xbt_new(char *, e->used);
205 XBT_VERB("Fire a first command: '%s'", cmd);
206 pipe = popen(cmd, "r");
208 xbt_die("Cannot fork addr2line to display the backtrace");
211 for (i = 0; i < e->used; i++) {
212 XBT_DEBUG("Looking for symbol %d, addr = '%s'", i, addrs[i]);
213 if (fgets(line_func, 1024, pipe)) {
214 line_func[strlen(line_func) - 1] = '\0';
216 XBT_VERB("Cannot run fgets to look for symbol %d, addr %s", i, addrs[i]);
217 strcpy(line_func, "???");
219 if (fgets(line_pos, 1024, pipe)) {
220 line_pos[strlen(line_pos) - 1] = '\0';
222 XBT_VERB("Cannot run fgets to look for symbol %d, addr %s", i, addrs[i]);
223 strcpy(line_pos, backtrace_syms[i]);
226 if (strcmp("??", line_func) != 0) {
227 XBT_DEBUG("Found static symbol %s() at %s", line_func, line_pos);
229 bprintf("** In %s() at %s", line_func, line_pos);
231 /* Damn. The symbol is in a dynamic library. Let's get wild */
236 long int addr, offset = 0;
243 /* let's look for the offset of this library in our addressing space */
244 maps_name = bprintf("/proc/%d/maps", (int) getpid());
245 maps = fopen(maps_name, "r");
247 addr = strtol(addrs[i], &p, 16);
249 XBT_CRITICAL("Cannot parse backtrace address '%s' (addr=%#lx)",
252 XBT_DEBUG("addr=%s (as string) =%#lx (as number)", addrs[i], addr);
255 long int first, last;
257 if (fgets(maps_buff, 512, maps) == NULL)
260 maps_buff[strlen(maps_buff) - 1] = '\0';
261 XBT_DEBUG("map line: %s", maps_buff);
263 sscanf(maps_buff, "%lx", &first);
264 p = strchr(maps_buff, '-') + 1;
265 sscanf(p, "%lx", &last);
266 if (first < addr && addr < last) {
271 XBT_DEBUG("%#lx in [%#lx-%#lx]", addr, first, last);
273 ("Symbol found, map lines not further displayed (even if looking for next ones)");
282 ("Problem while reading the maps file. Following backtrace will be mangled.");
283 XBT_DEBUG("No dynamic. Static symbol: %s", backtrace_syms[i]);
284 e->bt_strings[i] = bprintf("** In ?? (%s)", backtrace_syms[i]);
288 /* Ok, Found the offset of the maps line containing the searched symbol.
289 We now need to substract this from the address we got from backtrace.
292 addrs[i] = bprintf("0x%0*lx", addr_len - 2, addr - offset);
293 XBT_DEBUG("offset=%#lx new addr=%s", offset, addrs[i]);
295 /* Got it. We have our new address. Let's get the library path and we
297 p = xbt_strdup(backtrace_syms[i]);
299 /* library path not displayed in the map file either... */
301 sprintf(line_func, "??");
303 p2 = strrchr(p, '(');
306 p2 = strrchr(p, ' ');
310 /* Here we go, fire an addr2line up */
311 subcmd = bprintf("%s -f -e %s %s", ADDR2LINE, p, addrs[i]);
313 XBT_VERB("Fire a new command: '%s'", subcmd);
314 subpipe = popen(subcmd, "r");
316 xbt_die("Cannot fork addr2line to display the backtrace");
318 if (fgets(line_func, 1024, subpipe)) {
319 line_func[strlen(line_func) - 1] = '\0';
321 XBT_VERB("Cannot read result of subcommand %s", subcmd);
322 strcpy(line_func, "???");
324 if (fgets(line_pos, 1024, subpipe)) {
325 line_pos[strlen(line_pos) - 1] = '\0';
327 XBT_VERB("Cannot read result of subcommand %s", subcmd);
328 strcpy(line_pos, backtrace_syms[i]);
334 /* check whether the trick worked */
335 if (strcmp("??", line_func)) {
336 XBT_DEBUG("Found dynamic symbol %s() at %s", line_func, line_pos);
338 bprintf("** In %s() at %s", line_func, line_pos);
340 /* damn, nothing to do here. Let's print the raw address */
341 XBT_DEBUG("Dynamic symbol not found. Raw address = %s",
343 e->bt_strings[i] = bprintf("** In ?? at %s", backtrace_syms[i]);
349 /* Mask the bottom of the stack */
350 if (!strncmp("main", line_func, strlen("main")) ||
351 !strncmp("xbt_thread_context_wrapper", line_func,
352 strlen("xbt_thread_context_wrapper"))
353 || !strncmp("smx_ctx_sysv_wrapper", line_func,
354 strlen("smx_ctx_sysv_wrapper"))) {
357 for (j = i + 1; j < e->used; j++)
362 ("xbt_thread_context_wrapper", line_func,
363 strlen("xbt_thread_context_wrapper"))) {
364 free(e->bt_strings[i]);
365 e->bt_strings[i] = xbt_strdup("** (in a separate thread)");
373 free(backtrace_syms);
379 int xbt_libunwind_backtrace(void* bt[XBT_BACKTRACE_SIZE], int size){
382 for(i=0; i < size; i++)
390 unw_getcontext (&uc);
391 unw_init_local (&c, &uc);
397 while(unw_step(&c) >= 0 && i < size){
399 unw_get_reg(&c, UNW_REG_IP, &ip);
400 bt[i] = (void*)(long)ip;