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) { }
23 void xbt_backtrace_exit(void) { }
25 void xbt_backtrace_current(xbt_ex_t * e) {
26 e->used = backtrace((void **) e->bt, XBT_BACKTRACE_SIZE);
30 void xbt_ex_setup_backtrace(xbt_ex_t * e)
34 /* to get the backtrace from the libc */
35 char **backtrace_syms;
37 /* To build the commandline of addr2line */
40 /* to extract the addresses from the backtrace */
44 /* To read the output of addr2line */
46 char line_func[1024], line_pos[1024];
48 /* size (in char) of pointers on this arch */
51 /* To search for the right executable path when not trivial */
53 char *binary_name = NULL;
55 xbt_assert0(e && e->used,"Backtrace not setup yet, cannot set it up for display");
57 backtrace_syms = backtrace_symbols(e->bt, e->used);
58 /* ignore first one, which is this xbt_backtrace_current() */
60 memmove(backtrace_syms,backtrace_syms+1,sizeof(char*)*e->used);
61 addrs = xbt_new(char *, e->used);
65 /* Some arches only have stubs of backtrace, no implementation (hppa comes to mind) */
69 /* build the commandline */
70 if (stat(xbt_binary_name, &stat_buf)) {
71 /* Damn. binary not in current dir. We'll have to dig the PATH to find it */
74 for (i = 0; environ[i]; i++) {
75 if (!strncmp("PATH=", environ[i], 5)) {
76 xbt_dynar_t path = xbt_str_split(environ[i] + 5, ":");
80 xbt_dynar_foreach(path, cpt, data) {
83 binary_name = bprintf("%s/%s", data, xbt_binary_name);
84 if (!stat(binary_name, &stat_buf)) {
86 DEBUG1("Looked in the PATH for the binary. Found %s",
88 xbt_dynar_free(&path);
92 if (stat(binary_name, &stat_buf)) {
95 e->bt_strings = xbt_new(char *, 1);
98 bprintf("(binary '%s' not found the path)", xbt_binary_name);
101 xbt_dynar_free(&path);
106 binary_name = xbt_strdup(xbt_binary_name);
110 strlen(ADDR2LINE) + 25 + strlen(binary_name) + 32 * e->used);
112 curr += sprintf(curr, "%s -f -e %s ", ADDR2LINE, binary_name);
115 for (i = 0; i < e->used; i++) {
116 /* retrieve this address */
117 DEBUG2("Retrieving address number %d from '%s'", i, backtrace_syms[i]);
118 snprintf(buff, 256, "%s", strchr(backtrace_syms[i], '[') + 1);
119 p = strchr(buff, ']');
121 if (strcmp(buff, "(nil)"))
122 addrs[i] = bprintf("%s", buff);
124 addrs[i] = bprintf("0x0");
125 DEBUG3("Set up a new address: %d, '%s'(%p)", i, addrs[i], addrs[i]);
127 /* Add it to the command line args */
128 curr += sprintf(curr, "%s ", addrs[i]);
130 addr_len = strlen(addrs[0]);
132 /* parse the output and build a new backtrace */
133 e->bt_strings = xbt_new(char *, e->used);
135 VERB1("Fire a first command: '%s'", cmd);
136 pipe = popen(cmd, "r");
138 CRITICAL0("Cannot fork addr2line to display the backtrace");
142 for (i = 0; i < e->used; i++) {
144 DEBUG2("Looking for symbol %d, addr = '%s'", i, addrs[i]);
145 fgets_res = fgets(line_func, 1024, pipe);
146 if (fgets_res == NULL)
147 THROW2(system_error,0, "Cannot run fgets to look for symbol %d, addr %s",i, addrs[i]);
148 line_func[strlen(line_func) - 1] = '\0';
149 fgets_res = fgets(line_pos, 1024, pipe);
150 if (fgets_res == NULL)
151 THROW2(system_error,0, "Cannot run fgets to look for symbol %d, addr %s",i, addrs[i]);
152 line_pos[strlen(line_pos) - 1] = '\0';
154 if (strcmp("??", line_func)) {
155 DEBUG2("Found static symbol %s() at %s", line_func, line_pos);
156 e->bt_strings[i] = bprintf("** In %s() at %s", line_func, line_pos);
158 /* Damn. The symbol is in a dynamic library. Let's get wild */
163 long int addr, offset = 0;
170 /* let's look for the offset of this library in our addressing space */
171 maps_name = bprintf("/proc/%d/maps", (int) getpid());
172 maps = fopen(maps_name, "r");
174 sscanf(addrs[i], "%lx", &addr);
175 sprintf(maps_buff, "%#lx", addr);
177 if (strcmp(addrs[i], maps_buff)) {
178 CRITICAL2("Cannot parse backtrace address '%s' (addr=%#lx)",
181 DEBUG2("addr=%s (as string) =%#lx (as number)", addrs[i], addr);
184 long int first, last;
186 if (fgets(maps_buff, 512, maps) == NULL)
189 maps_buff[strlen(maps_buff) - 1] = '\0';
190 DEBUG1("map line: %s", maps_buff);
192 sscanf(maps_buff, "%lx", &first);
193 p = strchr(maps_buff, '-') + 1;
194 sscanf(p, "%lx", &last);
195 if (first < addr && addr < last) {
200 DEBUG3("%#lx in [%#lx-%#lx]", addr, first, last);
202 ("Symbol found, map lines not further displayed (even if looking for next ones)");
210 ("Problem while reading the maps file. Following backtrace will be mangled.");
211 DEBUG1("No dynamic. Static symbol: %s", backtrace_syms[i]);
212 e->bt_strings[i] = bprintf("** In ?? (%s)", backtrace_syms[i]);
216 /* Ok, Found the offset of the maps line containing the searched symbol.
217 We now need to substract this from the address we got from backtrace.
221 addrs[i] = bprintf("0x%0*lx", addr_len - 2, addr - offset);
222 DEBUG2("offset=%#lx new addr=%s", offset, addrs[i]);
224 /* Got it. We have our new address. Let's get the library path and we
226 p = xbt_strdup(backtrace_syms[i]);
228 /* library path not displayed in the map file either... */
230 sprintf(line_func, "??");
232 p2 = strrchr(p, '(');
235 p2 = strrchr(p, ' ');
239 /* Here we go, fire an addr2line up */
240 subcmd = bprintf("%s -f -e %s %s", ADDR2LINE, p, addrs[i]);
242 VERB1("Fire a new command: '%s'", subcmd);
243 subpipe = popen(subcmd, "r");
245 CRITICAL0("Cannot fork addr2line to display the backtrace");
248 fgets_res=fgets(line_func, 1024, subpipe);
249 if (fgets_res == NULL)
250 THROW1(system_error,0, "Cannot read result of subcommand %s",subcmd);
251 line_func[strlen(line_func) - 1] = '\0';
252 fgets_res = fgets(line_pos, 1024, subpipe);
253 if (fgets_res == NULL)
254 THROW1(system_error,0, "Cannot read result of subcommand %s",subcmd);
255 line_pos[strlen(line_pos) - 1] = '\0';
260 /* check whether the trick worked */
261 if (strcmp("??", line_func)) {
262 DEBUG2("Found dynamic symbol %s() at %s", line_func, line_pos);
263 e->bt_strings[i] = bprintf("** In %s() at %s", line_func, line_pos);
265 /* damn, nothing to do here. Let's print the raw address */
266 DEBUG1("Dynamic symbol not found. Raw address = %s",
268 e->bt_strings[i] = bprintf("** In ?? at %s", backtrace_syms[i]);
274 /* Mask the bottom of the stack */
275 if (!strncmp("main", line_func, strlen("main")) ||
276 !strncmp("xbt_thread_context_wrapper", line_func,
277 strlen("xbt_thread_context_wrapper"))) {
280 for (j = i + 1; j < e->used; j++)
285 ("xbt_thread_context_wrapper", line_func,
286 strlen("xbt_thread_context_wrapper"))) {
288 e->bt_strings[i] = bprintf("** (in a separate thread)");
296 free(backtrace_syms);