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)
28 void xbt_backtrace_current(xbt_ex_t * e)
30 e->used = backtrace((void **) e->bt, XBT_BACKTRACE_SIZE);
34 void xbt_ex_setup_backtrace(xbt_ex_t * e)
38 /* to get the backtrace from the libc */
39 char **backtrace_syms;
41 /* To build the commandline of addr2line */
44 /* to extract the addresses from the backtrace */
48 /* To read the output of addr2line */
50 char line_func[1024], line_pos[1024];
52 /* size (in char) of pointers on this arch */
55 /* To search for the right executable path when not trivial */
57 char *binary_name = NULL;
61 "Backtrace not setup yet, cannot set it up for display");
63 if (!xbt_binary_name) /* no binary name, nothing to do */
66 backtrace_syms = backtrace_symbols(e->bt, e->used);
67 /* ignore first one, which is this xbt_backtrace_current() */
69 memmove(backtrace_syms, backtrace_syms + 1, sizeof(char *) * e->used);
73 /* Some arches only have stubs of backtrace, no implementation (hppa comes to mind) */
77 /* build the commandline */
78 if (stat(xbt_binary_name, &stat_buf)) {
79 /* Damn. binary not in current dir. We'll have to dig the PATH to find it */
82 for (i = 0; environ[i]; i++) {
83 if (!strncmp("PATH=", environ[i], 5)) {
84 xbt_dynar_t path = xbt_str_split(environ[i] + 5, ":");
88 xbt_dynar_foreach(path, cpt, data) {
91 binary_name = bprintf("%s/%s", data, xbt_binary_name);
92 if (!stat(binary_name, &stat_buf)) {
94 XBT_DEBUG("Looked in the PATH for the binary. Found %s",
99 xbt_dynar_free(&path);
100 if (stat(binary_name, &stat_buf)) {
103 e->bt_strings = xbt_new(char *, 1);
106 bprintf("(binary '%s' not found the path)", xbt_binary_name);
107 free(backtrace_syms);
114 binary_name = xbt_strdup(xbt_binary_name);
118 strlen(ADDR2LINE) + 25 + strlen(binary_name) + 32 * e->used);
120 curr += sprintf(curr, "%s -f -e %s ", ADDR2LINE, binary_name);
123 addrs = xbt_new(char *, e->used);
124 for (i = 0; i < e->used; i++) {
125 /* retrieve this address */
126 XBT_DEBUG("Retrieving address number %d from '%s'", i, backtrace_syms[i]);
127 snprintf(buff, 256, "%s", strchr(backtrace_syms[i], '[') + 1);
128 p = strchr(buff, ']');
130 if (strcmp(buff, "(nil)"))
131 addrs[i] = xbt_strdup(buff);
133 addrs[i] = xbt_strdup("0x0");
134 XBT_DEBUG("Set up a new address: %d, '%s'(%p)", i, addrs[i], addrs[i]);
136 /* Add it to the command line args */
137 curr += sprintf(curr, "%s ", addrs[i]);
139 addr_len = strlen(addrs[0]);
141 /* parse the output and build a new backtrace */
142 e->bt_strings = xbt_new(char *, e->used);
144 XBT_VERB("Fire a first command: '%s'", cmd);
145 pipe = popen(cmd, "r");
147 XBT_CRITICAL("Cannot fork addr2line to display the backtrace");
151 for (i = 0; i < e->used; i++) {
153 XBT_DEBUG("Looking for symbol %d, addr = '%s'", i, addrs[i]);
154 fgets_res = fgets(line_func, 1024, pipe);
155 if (fgets_res == NULL)
156 THROWF(system_error, 0,
157 "Cannot run fgets to look for symbol %d, addr %s", i,
159 line_func[strlen(line_func) - 1] = '\0';
160 fgets_res = fgets(line_pos, 1024, pipe);
161 if (fgets_res == NULL)
162 THROWF(system_error, 0,
163 "Cannot run fgets to look for symbol %d, addr %s", i,
165 line_pos[strlen(line_pos) - 1] = '\0';
167 if (strcmp("??", line_func)) {
168 XBT_DEBUG("Found static symbol %s() at %s", line_func, line_pos);
170 bprintf("** In %s() at %s", line_func, line_pos);
172 /* Damn. The symbol is in a dynamic library. Let's get wild */
177 long int addr, offset = 0;
184 /* let's look for the offset of this library in our addressing space */
185 maps_name = bprintf("/proc/%d/maps", (int) getpid());
186 maps = fopen(maps_name, "r");
188 sscanf(addrs[i], "%lx", &addr);
189 sprintf(maps_buff, "%#lx", addr);
191 if (strcmp(addrs[i], maps_buff)) {
192 XBT_CRITICAL("Cannot parse backtrace address '%s' (addr=%#lx)",
195 XBT_DEBUG("addr=%s (as string) =%#lx (as number)", addrs[i], addr);
198 long int first, last;
200 if (fgets(maps_buff, 512, maps) == NULL)
203 maps_buff[strlen(maps_buff) - 1] = '\0';
204 XBT_DEBUG("map line: %s", maps_buff);
206 sscanf(maps_buff, "%lx", &first);
207 p = strchr(maps_buff, '-') + 1;
208 sscanf(p, "%lx", &last);
209 if (first < addr && addr < last) {
214 XBT_DEBUG("%#lx in [%#lx-%#lx]", addr, first, last);
216 ("Symbol found, map lines not further displayed (even if looking for next ones)");
225 ("Problem while reading the maps file. Following backtrace will be mangled.");
226 XBT_DEBUG("No dynamic. Static symbol: %s", backtrace_syms[i]);
227 e->bt_strings[i] = bprintf("** In ?? (%s)", backtrace_syms[i]);
231 /* Ok, Found the offset of the maps line containing the searched symbol.
232 We now need to substract this from the address we got from backtrace.
235 addrs[i] = bprintf("0x%0*lx", addr_len - 2, addr - offset);
236 XBT_DEBUG("offset=%#lx new addr=%s", offset, addrs[i]);
238 /* Got it. We have our new address. Let's get the library path and we
240 p = xbt_strdup(backtrace_syms[i]);
242 /* library path not displayed in the map file either... */
244 sprintf(line_func, "??");
246 p2 = strrchr(p, '(');
249 p2 = strrchr(p, ' ');
253 /* Here we go, fire an addr2line up */
254 subcmd = bprintf("%s -f -e %s %s", ADDR2LINE, p, addrs[i]);
256 XBT_VERB("Fire a new command: '%s'", subcmd);
257 subpipe = popen(subcmd, "r");
259 XBT_CRITICAL("Cannot fork addr2line to display the backtrace");
262 fgets_res = fgets(line_func, 1024, subpipe);
263 if (fgets_res == NULL)
264 THROWF(system_error, 0, "Cannot read result of subcommand %s",
266 line_func[strlen(line_func) - 1] = '\0';
267 fgets_res = fgets(line_pos, 1024, subpipe);
268 if (fgets_res == NULL)
269 THROWF(system_error, 0, "Cannot read result of subcommand %s",
271 line_pos[strlen(line_pos) - 1] = '\0';
276 /* check whether the trick worked */
277 if (strcmp("??", line_func)) {
278 XBT_DEBUG("Found dynamic symbol %s() at %s", line_func, line_pos);
280 bprintf("** In %s() at %s", line_func, line_pos);
282 /* damn, nothing to do here. Let's print the raw address */
283 XBT_DEBUG("Dynamic symbol not found. Raw address = %s",
285 e->bt_strings[i] = bprintf("** In ?? at %s", backtrace_syms[i]);
291 /* Mask the bottom of the stack */
292 if (!strncmp("main", line_func, strlen("main")) ||
293 !strncmp("xbt_thread_context_wrapper", line_func,
294 strlen("xbt_thread_context_wrapper"))
295 || !strncmp("smx_ctx_sysv_wrapper", line_func,
296 strlen("smx_ctx_sysv_wrapper"))) {
299 for (j = i + 1; j < e->used; j++)
304 ("xbt_thread_context_wrapper", line_func,
305 strlen("xbt_thread_context_wrapper"))) {
306 free(e->bt_strings[i]);
307 e->bt_strings[i] = xbt_strdup("** (in a separate thread)");
315 free(backtrace_syms);