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 extern char **environ; /* the environment, as specified by the opengroup */
14 /* Module creation/destruction: nothing to do on linux */
15 void xbt_backtrace_init(void) { }
16 void xbt_backtrace_exit(void) { }
18 void xbt_ex_setup_backtrace(xbt_ex_t * e)
22 /* to get the backtrace from the libc */
23 char **backtrace_syms = backtrace_symbols(e->bt, e->used);
25 /* To build the commandline of addr2line */
28 /* to extract the addresses from the backtrace */
29 char **addrs = xbt_new(char *, e->used);
32 /* To read the output of addr2line */
34 char line_func[1024], line_pos[1024];
36 /* size (in char) of pointers on this arch */
39 /* To search for the right executable path when not trivial */
41 char *binary_name = NULL;
43 e->used = backtrace((void **) e->bt, XBT_BACKTRACE_SIZE);
46 /* Some arches only have stubs of backtrace, no implementation (hppa comes to mind) */
50 /* build the commandline */
51 if (stat(xbt_binary_name, &stat_buf)) {
52 /* Damn. binary not in current dir. We'll have to dig the PATH to find it */
55 for (i = 0; environ[i]; i++) {
56 if (!strncmp("PATH=", environ[i], 5)) {
57 xbt_dynar_t path = xbt_str_split(environ[i] + 5, ":");
61 xbt_dynar_foreach(path, cpt, data) {
64 binary_name = bprintf("%s/%s", data, xbt_binary_name);
65 if (!stat(binary_name, &stat_buf)) {
67 DEBUG1("Looked in the PATH for the binary. Found %s",
69 xbt_dynar_free(&path);
73 if (stat(binary_name, &stat_buf)) {
76 e->bt_strings = xbt_new(char *, 1);
79 bprintf("(binary '%s' not found the path)", xbt_binary_name);
82 xbt_dynar_free(&path);
87 binary_name = xbt_strdup(xbt_binary_name);
91 strlen(ADDR2LINE) + 25 + strlen(binary_name) + 32 * e->used);
93 curr += sprintf(curr, "%s -f -e %s ", ADDR2LINE, binary_name);
96 for (i = 0; i < e->used; i++) {
97 /* retrieve this address */
98 DEBUG2("Retrieving address number %d from '%s'", i, backtrace_syms[i]);
99 snprintf(buff, 256, "%s", strchr(backtrace_syms[i], '[') + 1);
100 p = strchr(buff, ']');
102 if (strcmp(buff, "(nil)"))
103 addrs[i] = bprintf("%s", buff);
105 addrs[i] = bprintf("0x0");
106 DEBUG3("Set up a new address: %d, '%s'(%p)", i, addrs[i], addrs[i]);
108 /* Add it to the command line args */
109 curr += sprintf(curr, "%s ", addrs[i]);
111 addr_len = strlen(addrs[0]);
113 /* parse the output and build a new backtrace */
114 e->bt_strings = xbt_new(char *, e->used);
116 VERB1("Fire a first command: '%s'", cmd);
117 pipe = popen(cmd, "r");
119 CRITICAL0("Cannot fork addr2line to display the backtrace");
123 for (i = 0; i < e->used; i++) {
124 DEBUG2("Looking for symbol %d, addr = '%s'", i, addrs[i]);
125 fgets(line_func, 1024, pipe);
126 line_func[strlen(line_func) - 1] = '\0';
127 fgets(line_pos, 1024, pipe);
128 line_pos[strlen(line_pos) - 1] = '\0';
130 if (strcmp("??", line_func)) {
131 DEBUG2("Found static symbol %s() at %s", line_func, line_pos);
132 e->bt_strings[i] = bprintf("** In %s() at %s", line_func, line_pos);
134 /* Damn. The symbol is in a dynamic library. Let's get wild */
139 long int addr, offset = 0;
146 /* let's look for the offset of this library in our addressing space */
147 maps_name = bprintf("/proc/%d/maps", (int) getpid());
148 maps = fopen(maps_name, "r");
150 sscanf(addrs[i], "%lx", &addr);
151 sprintf(maps_buff, "%#lx", addr);
153 if (strcmp(addrs[i], maps_buff)) {
154 CRITICAL2("Cannot parse backtrace address '%s' (addr=%#lx)",
157 DEBUG2("addr=%s (as string) =%#lx (as number)", addrs[i], addr);
160 long int first, last;
162 if (fgets(maps_buff, 512, maps) == NULL)
165 maps_buff[strlen(maps_buff) - 1] = '\0';
166 DEBUG1("map line: %s", maps_buff);
168 sscanf(maps_buff, "%lx", &first);
169 p = strchr(maps_buff, '-') + 1;
170 sscanf(p, "%lx", &last);
171 if (first < addr && addr < last) {
176 DEBUG3("%#lx in [%#lx-%#lx]", addr, first, last);
178 ("Symbol found, map lines not further displayed (even if looking for next ones)");
186 ("Problem while reading the maps file. Following backtrace will be mangled.");
187 DEBUG1("No dynamic. Static symbol: %s", backtrace_syms[i]);
188 e->bt_strings[i] = bprintf("** In ?? (%s)", backtrace_syms[i]);
192 /* Ok, Found the offset of the maps line containing the searched symbol.
193 We now need to substract this from the address we got from backtrace.
197 addrs[i] = bprintf("0x%0*lx", addr_len - 2, addr - offset);
198 DEBUG2("offset=%#lx new addr=%s", offset, addrs[i]);
200 /* Got it. We have our new address. Let's get the library path and we
202 p = xbt_strdup(backtrace_syms[i]);
204 /* library path not displayed in the map file either... */
206 sprintf(line_func, "??");
208 p2 = strrchr(p, '(');
211 p2 = strrchr(p, ' ');
215 /* Here we go, fire an addr2line up */
216 subcmd = bprintf("%s -f -e %s %s", ADDR2LINE, p, addrs[i]);
218 VERB1("Fire a new command: '%s'", subcmd);
219 subpipe = popen(subcmd, "r");
221 CRITICAL0("Cannot fork addr2line to display the backtrace");
224 fgets(line_func, 1024, subpipe);
225 line_func[strlen(line_func) - 1] = '\0';
226 fgets(line_pos, 1024, subpipe);
227 line_pos[strlen(line_pos) - 1] = '\0';
232 /* check whether the trick worked */
233 if (strcmp("??", line_func)) {
234 DEBUG2("Found dynamic symbol %s() at %s", line_func, line_pos);
235 e->bt_strings[i] = bprintf("** In %s() at %s", line_func, line_pos);
237 /* damn, nothing to do here. Let's print the raw address */
238 DEBUG1("Dynamic symbol not found. Raw address = %s",
240 e->bt_strings[i] = bprintf("** In ?? at %s", backtrace_syms[i]);
246 /* Mask the bottom of the stack */
247 if (!strncmp("main", line_func, strlen("main")) ||
248 !strncmp("xbt_thread_context_wrapper", line_func,
249 strlen("xbt_thread_context_wrapper"))) {
252 for (j = i + 1; j < e->used; j++)
257 ("xbt_thread_context_wrapper", line_func,
258 strlen("xbt_thread_context_wrapper"))) {
260 e->bt_strings[i] = bprintf("** (in a separate thread)");
268 free(backtrace_syms);