1 /* $Id: ex.c 5173 2008-01-07 22:10:52Z mquinson $ */
3 /* backtrace_linux - backtrace displaying on linux platform */
4 /* This file is included by ex.c on need (have execinfo.h, popen & addrline)*/
6 /* Copyright (c) 2007 The SimGrid team */
7 /* All rights reserved. */
9 /* This program is free software; you can redistribute it and/or modify it
10 * under the terms of the license (GNU LGPL) which comes with this package. */
12 /* 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 */
15 #include "xbt/module.h" /* xbt_binary_name */
16 #include "xbt_modinter.h" /* backtrace initialization headers */
17 /* end of "useless" inclusions */
19 extern char **environ; /* the environment, as specified by the opengroup */
21 /* Module creation/destruction: nothing to do on linux */
22 void xbt_backtrace_init(void)
26 void xbt_backtrace_exit(void)
30 void xbt_backtrace_current(xbt_ex_t * e)
32 e->used = backtrace((void **) e->bt, XBT_BACKTRACE_SIZE);
36 void xbt_ex_setup_backtrace(xbt_ex_t * e)
40 /* to get the backtrace from the libc */
41 char **backtrace_syms;
43 /* To build the commandline of addr2line */
46 /* to extract the addresses from the backtrace */
50 /* To read the output of addr2line */
52 char line_func[1024], line_pos[1024];
54 /* size (in char) of pointers on this arch */
57 /* To search for the right executable path when not trivial */
59 char *binary_name = NULL;
63 "Backtrace not setup yet, cannot set it up for display");
65 backtrace_syms = backtrace_symbols(e->bt, e->used);
66 /* ignore first one, which is this xbt_backtrace_current() */
68 memmove(backtrace_syms, backtrace_syms + 1, sizeof(char *) * e->used);
69 addrs = xbt_new(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 DEBUG1("Looked in the PATH for the binary. Found %s",
96 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);
109 xbt_dynar_free(&path);
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 for (i = 0; i < e->used; i++) {
124 /* retrieve this address */
125 DEBUG2("Retrieving address number %d from '%s'", i, backtrace_syms[i]);
126 snprintf(buff, 256, "%s", strchr(backtrace_syms[i], '[') + 1);
127 p = strchr(buff, ']');
129 if (strcmp(buff, "(nil)"))
130 addrs[i] = bprintf("%s", buff);
132 addrs[i] = bprintf("0x0");
133 DEBUG3("Set up a new address: %d, '%s'(%p)", i, addrs[i], addrs[i]);
135 /* Add it to the command line args */
136 curr += sprintf(curr, "%s ", addrs[i]);
138 addr_len = strlen(addrs[0]);
140 /* parse the output and build a new backtrace */
141 e->bt_strings = xbt_new(char *, e->used);
143 VERB1("Fire a first command: '%s'", cmd);
144 pipe = popen(cmd, "r");
146 CRITICAL0("Cannot fork addr2line to display the backtrace");
150 for (i = 0; i < e->used; i++) {
152 DEBUG2("Looking for symbol %d, addr = '%s'", i, addrs[i]);
153 fgets_res = fgets(line_func, 1024, pipe);
154 if (fgets_res == NULL)
155 THROW2(system_error, 0,
156 "Cannot run fgets to look for symbol %d, addr %s", i, addrs[i]);
157 line_func[strlen(line_func) - 1] = '\0';
158 fgets_res = fgets(line_pos, 1024, pipe);
159 if (fgets_res == NULL)
160 THROW2(system_error, 0,
161 "Cannot run fgets to look for symbol %d, addr %s", i, addrs[i]);
162 line_pos[strlen(line_pos) - 1] = '\0';
164 if (strcmp("??", line_func)) {
165 DEBUG2("Found static symbol %s() at %s", line_func, line_pos);
166 e->bt_strings[i] = bprintf("** In %s() at %s", line_func, line_pos);
168 /* Damn. The symbol is in a dynamic library. Let's get wild */
173 long int addr, offset = 0;
180 /* let's look for the offset of this library in our addressing space */
181 maps_name = bprintf("/proc/%d/maps", (int) getpid());
182 maps = fopen(maps_name, "r");
184 sscanf(addrs[i], "%lx", &addr);
185 sprintf(maps_buff, "%#lx", addr);
187 if (strcmp(addrs[i], maps_buff)) {
188 CRITICAL2("Cannot parse backtrace address '%s' (addr=%#lx)",
191 DEBUG2("addr=%s (as string) =%#lx (as number)", addrs[i], addr);
194 long int first, last;
196 if (fgets(maps_buff, 512, maps) == NULL)
199 maps_buff[strlen(maps_buff) - 1] = '\0';
200 DEBUG1("map line: %s", maps_buff);
202 sscanf(maps_buff, "%lx", &first);
203 p = strchr(maps_buff, '-') + 1;
204 sscanf(p, "%lx", &last);
205 if (first < addr && addr < last) {
210 DEBUG3("%#lx in [%#lx-%#lx]", addr, first, last);
212 ("Symbol found, map lines not further displayed (even if looking for next ones)");
220 ("Problem while reading the maps file. Following backtrace will be mangled.");
221 DEBUG1("No dynamic. Static symbol: %s", backtrace_syms[i]);
222 e->bt_strings[i] = bprintf("** In ?? (%s)", backtrace_syms[i]);
226 /* Ok, Found the offset of the maps line containing the searched symbol.
227 We now need to substract this from the address we got from backtrace.
231 addrs[i] = bprintf("0x%0*lx", addr_len - 2, addr - offset);
232 DEBUG2("offset=%#lx new addr=%s", offset, addrs[i]);
234 /* Got it. We have our new address. Let's get the library path and we
236 p = xbt_strdup(backtrace_syms[i]);
238 /* library path not displayed in the map file either... */
240 sprintf(line_func, "??");
242 p2 = strrchr(p, '(');
245 p2 = strrchr(p, ' ');
249 /* Here we go, fire an addr2line up */
250 subcmd = bprintf("%s -f -e %s %s", ADDR2LINE, p, addrs[i]);
252 VERB1("Fire a new command: '%s'", subcmd);
253 subpipe = popen(subcmd, "r");
255 CRITICAL0("Cannot fork addr2line to display the backtrace");
258 fgets_res = fgets(line_func, 1024, subpipe);
259 if (fgets_res == NULL)
260 THROW1(system_error, 0, "Cannot read result of subcommand %s",
262 line_func[strlen(line_func) - 1] = '\0';
263 fgets_res = fgets(line_pos, 1024, subpipe);
264 if (fgets_res == NULL)
265 THROW1(system_error, 0, "Cannot read result of subcommand %s",
267 line_pos[strlen(line_pos) - 1] = '\0';
272 /* check whether the trick worked */
273 if (strcmp("??", line_func)) {
274 DEBUG2("Found dynamic symbol %s() at %s", line_func, line_pos);
275 e->bt_strings[i] = bprintf("** In %s() at %s", line_func, line_pos);
277 /* damn, nothing to do here. Let's print the raw address */
278 DEBUG1("Dynamic symbol not found. Raw address = %s",
280 e->bt_strings[i] = bprintf("** In ?? at %s", backtrace_syms[i]);
286 /* Mask the bottom of the stack */
287 if (!strncmp("main", line_func, strlen("main")) ||
288 !strncmp("xbt_thread_context_wrapper", line_func,
289 strlen("xbt_thread_context_wrapper"))) {
292 for (j = i + 1; j < e->used; j++)
297 ("xbt_thread_context_wrapper", line_func,
298 strlen("xbt_thread_context_wrapper"))) {
300 e->bt_strings[i] = bprintf("** (in a separate thread)");
308 free(backtrace_syms);