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. */
14 /* 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 */
18 #include "xbt/module.h" /* xbt_binary_name */
19 #include "src/xbt_modinter.h" /* backtrace initialization headers */
21 #define UNW_LOCAL_ONLY
22 #include <libunwind.h>
24 /* end of "useless" inclusions */
26 extern char **environ; /* the environment, as specified by the opengroup */
28 /* Module creation/destruction: nothing to do on linux */
29 void xbt_backtrace_preinit(void)
33 void xbt_backtrace_postexit(void)
43 static _Unwind_Reason_Code
44 backtrace_helper (struct _Unwind_Context *ctx, void *a)
46 struct trace_arg *arg = a;
48 /* We are first called with address in the __backtrace function.
52 arg->array[arg->cnt] = (void *) _Unwind_GetIP(ctx);
54 /* Check whether we make any progress. */
55 if (arg->cnt > 0 && arg->array[arg->cnt - 1] == arg->array[arg->cnt])
56 return _URC_END_OF_STACK;
58 if (++arg->cnt == arg->size)
59 return _URC_END_OF_STACK;
60 return _URC_NO_REASON;
63 /** @brief reimplementation of glibc backtrace based directly on gcc library, without implicit malloc
65 * See http://webloria.loria.fr/~quinson/blog/2012/0208/system_programming_fun_in_SimGrid/
66 * for the motivation behind this function
69 int xbt_backtrace_no_malloc(void **array, int size) {
72 for(i=0; i < size; i++)
75 struct trace_arg arg = { .array = array, .size = size, .cnt = -1 };
78 _Unwind_Backtrace(backtrace_helper, &arg);
80 /* _Unwind_Backtrace on IA-64 seems to put NULL address above
81 _start. Fix it up here. */
82 if (arg.cnt > 1 && arg.array[arg.cnt - 1] == NULL)
84 return arg.cnt != -1 ? arg.cnt : 0;
87 void xbt_backtrace_current(xbt_ex_t * e)
89 e->used = backtrace((void **) e->bt, XBT_BACKTRACE_SIZE);
91 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:");
92 fprintf(stderr, "%s(%d) [%s:%d] %s",
93 e->procname, e->pid, e->file, e->line, e->msg);
94 fprintf(stderr, "Bailing out now since there is nothing I can do without a decent amount of memory. Please go fix the memleaks\n");
100 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
104 /* to get the backtrace from the libc */
105 char **backtrace_syms;
107 /* To build the commandline of addr2line */
110 /* to extract the addresses from the backtrace */
114 /* To read the output of addr2line */
116 char line_func[1024], line_pos[1024];
118 /* size (in char) of pointers on this arch */
121 /* To search for the right executable path when not trivial */
122 struct stat stat_buf;
123 char *binary_name = NULL;
125 xbt_assert(e, "Backtrace not setup yet, cannot set it up for display");
127 e->bt_strings = NULL;
129 if (xbt_binary_name == NULL) /* no binary name, nothing to do */
135 /* ignore first one, which is xbt_backtrace_current() */
137 memmove(e->bt, e->bt + 1, (sizeof *e->bt) * e->used);
139 backtrace_syms = backtrace_symbols(e->bt, e->used);
141 /* build the commandline */
142 if (stat(xbt_binary_name, &stat_buf)) {
143 /* Damn. binary not in current dir. We'll have to dig the PATH to find it */
144 for (i = 0; environ[i]; i++) {
145 if (!strncmp("PATH=", environ[i], 5)) {
146 xbt_dynar_t path = xbt_str_split(environ[i] + 5, ":");
150 xbt_dynar_foreach(path, cpt, data) {
152 binary_name = bprintf("%s/%s", data, xbt_binary_name);
153 if (!stat(binary_name, &stat_buf)) {
155 XBT_DEBUG("Looked in the PATH for the binary. Found %s",
160 xbt_dynar_free(&path);
161 if (stat(binary_name, &stat_buf)) {
164 e->bt_strings = xbt_new(char *, 1);
167 bprintf("(binary '%s' not found in the PATH)", xbt_binary_name);
168 free(backtrace_syms);
175 binary_name = xbt_strdup(xbt_binary_name);
179 strlen(ADDR2LINE) + 25 + strlen(binary_name) + 32 * e->used);
181 curr += sprintf(curr, "%s -f -e %s ", ADDR2LINE, binary_name);
184 addrs = xbt_new(char *, e->used);
185 for (i = 0; i < e->used; i++) {
187 /* retrieve this address */
188 XBT_DEBUG("Retrieving address number %d from '%s'", i, backtrace_syms[i]);
189 snprintf(buff, 256, "%s", strchr(backtrace_syms[i], '[') + 1);
190 p = strchr(buff, ']');
192 if (strcmp(buff, "(nil)"))
193 addrs[i] = xbt_strdup(buff);
195 addrs[i] = xbt_strdup("0x0");
196 XBT_DEBUG("Set up a new address: %d, '%s'(%p)", i, addrs[i], addrs[i]);
198 /* Add it to the command line args */
199 curr += sprintf(curr, "%s ", addrs[i]);
201 addr_len = strlen(addrs[0]);
203 /* parse the output and build a new backtrace */
204 e->bt_strings = xbt_new(char *, e->used);
206 XBT_VERB("Fire a first command: '%s'", cmd);
207 pipe = popen(cmd, "r");
209 xbt_die("Cannot fork addr2line to display the backtrace");
212 for (i = 0; i < e->used; i++) {
213 XBT_DEBUG("Looking for symbol %d, addr = '%s'", i, addrs[i]);
214 if (fgets(line_func, 1024, pipe)) {
215 line_func[strlen(line_func) - 1] = '\0';
217 XBT_VERB("Cannot run fgets to look for symbol %d, addr %s", i, addrs[i]);
218 strcpy(line_func, "???");
220 if (fgets(line_pos, 1024, pipe)) {
221 line_pos[strlen(line_pos) - 1] = '\0';
223 XBT_VERB("Cannot run fgets to look for symbol %d, addr %s", i, addrs[i]);
224 strcpy(line_pos, backtrace_syms[i]);
227 if (strcmp("??", line_func) != 0) {
228 XBT_DEBUG("Found static symbol %s() at %s", line_func, line_pos);
230 bprintf("** In %s() at %s", line_func, line_pos);
232 /* Damn. The symbol is in a dynamic library. Let's get wild */
237 long int addr, offset = 0;
244 /* let's look for the offset of this library in our addressing space */
245 maps_name = bprintf("/proc/%d/maps", (int) getpid());
246 maps = fopen(maps_name, "r");
248 addr = strtol(addrs[i], &p, 16);
250 XBT_CRITICAL("Cannot parse backtrace address '%s' (addr=%#lx)",
253 XBT_DEBUG("addr=%s (as string) =%#lx (as number)", addrs[i], addr);
256 long int first, last;
258 if (fgets(maps_buff, 512, maps) == NULL)
261 maps_buff[strlen(maps_buff) - 1] = '\0';
262 XBT_DEBUG("map line: %s", maps_buff);
264 sscanf(maps_buff, "%lx", &first);
265 p = strchr(maps_buff, '-') + 1;
266 sscanf(p, "%lx", &last);
267 if (first < addr && addr < last) {
272 XBT_DEBUG("%#lx in [%#lx-%#lx]", addr, first, last);
274 ("Symbol found, map lines not further displayed (even if looking for next ones)");
283 ("Problem while reading the maps file. Following backtrace will be mangled.");
284 XBT_DEBUG("No dynamic. Static symbol: %s", backtrace_syms[i]);
285 e->bt_strings[i] = bprintf("** In ?? (%s)", backtrace_syms[i]);
289 /* Ok, Found the offset of the maps line containing the searched symbol.
290 We now need to substract this from the address we got from backtrace.
293 addrs[i] = bprintf("0x%0*lx", addr_len - 2, addr - offset);
294 XBT_DEBUG("offset=%#lx new addr=%s", offset, addrs[i]);
296 /* Got it. We have our new address. Let's get the library path and we
298 p = xbt_strdup(backtrace_syms[i]);
300 /* library path not displayed in the map file either... */
302 sprintf(line_func, "??");
304 p2 = strrchr(p, '(');
307 p2 = strrchr(p, ' ');
311 /* Here we go, fire an addr2line up */
312 subcmd = bprintf("%s -f -e %s %s", ADDR2LINE, p, addrs[i]);
314 XBT_VERB("Fire a new command: '%s'", subcmd);
315 subpipe = popen(subcmd, "r");
317 xbt_die("Cannot fork addr2line to display the backtrace");
319 if (fgets(line_func, 1024, subpipe)) {
320 line_func[strlen(line_func) - 1] = '\0';
322 XBT_VERB("Cannot read result of subcommand %s", subcmd);
323 strcpy(line_func, "???");
325 if (fgets(line_pos, 1024, subpipe)) {
326 line_pos[strlen(line_pos) - 1] = '\0';
328 XBT_VERB("Cannot read result of subcommand %s", subcmd);
329 strcpy(line_pos, backtrace_syms[i]);
335 /* check whether the trick worked */
336 if (strcmp("??", line_func)) {
337 XBT_DEBUG("Found dynamic symbol %s() at %s", line_func, line_pos);
339 bprintf("** In %s() at %s", line_func, line_pos);
341 /* damn, nothing to do here. Let's print the raw address */
342 XBT_DEBUG("Dynamic symbol not found. Raw address = %s",
344 e->bt_strings[i] = bprintf("** In ?? at %s", backtrace_syms[i]);
350 /* Mask the bottom of the stack */
351 if (!strncmp("main", line_func, strlen("main")) ||
352 !strncmp("xbt_thread_context_wrapper", line_func,
353 strlen("xbt_thread_context_wrapper"))
354 || !strncmp("smx_ctx_sysv_wrapper", line_func,
355 strlen("smx_ctx_sysv_wrapper"))) {
358 for (j = i + 1; j < e->used; j++)
363 ("xbt_thread_context_wrapper", line_func,
364 strlen("xbt_thread_context_wrapper"))) {
365 free(e->bt_strings[i]);
366 e->bt_strings[i] = xbt_strdup("** (in a separate thread)");
374 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;