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_backtrace_current(xbt_ex_t * e) {
19 e->used = backtrace((void **) e->bt, XBT_BACKTRACE_SIZE);
23 void xbt_ex_setup_backtrace(xbt_ex_t * e)
27 /* to get the backtrace from the libc */
28 char **backtrace_syms;
30 /* To build the commandline of addr2line */
33 /* to extract the addresses from the backtrace */
37 /* To read the output of addr2line */
39 char line_func[1024], line_pos[1024];
41 /* size (in char) of pointers on this arch */
44 /* To search for the right executable path when not trivial */
46 char *binary_name = NULL;
48 xbt_assert0(e && e->used,"Backtrace not setup yet, cannot set it up for display");
50 backtrace_syms = backtrace_symbols(e->bt, e->used);
51 addrs = xbt_new(char *, e->used);
55 /* Some arches only have stubs of backtrace, no implementation (hppa comes to mind) */
59 /* build the commandline */
60 if (stat(xbt_binary_name, &stat_buf)) {
61 /* Damn. binary not in current dir. We'll have to dig the PATH to find it */
64 for (i = 0; environ[i]; i++) {
65 if (!strncmp("PATH=", environ[i], 5)) {
66 xbt_dynar_t path = xbt_str_split(environ[i] + 5, ":");
70 xbt_dynar_foreach(path, cpt, data) {
73 binary_name = bprintf("%s/%s", data, xbt_binary_name);
74 if (!stat(binary_name, &stat_buf)) {
76 DEBUG1("Looked in the PATH for the binary. Found %s",
78 xbt_dynar_free(&path);
82 if (stat(binary_name, &stat_buf)) {
85 e->bt_strings = xbt_new(char *, 1);
88 bprintf("(binary '%s' not found the path)", xbt_binary_name);
91 xbt_dynar_free(&path);
96 binary_name = xbt_strdup(xbt_binary_name);
100 strlen(ADDR2LINE) + 25 + strlen(binary_name) + 32 * e->used);
102 curr += sprintf(curr, "%s -f -e %s ", ADDR2LINE, binary_name);
105 for (i = 0; i < e->used; i++) {
106 /* retrieve this address */
107 DEBUG2("Retrieving address number %d from '%s'", i, backtrace_syms[i]);
108 snprintf(buff, 256, "%s", strchr(backtrace_syms[i], '[') + 1);
109 p = strchr(buff, ']');
111 if (strcmp(buff, "(nil)"))
112 addrs[i] = bprintf("%s", buff);
114 addrs[i] = bprintf("0x0");
115 DEBUG3("Set up a new address: %d, '%s'(%p)", i, addrs[i], addrs[i]);
117 /* Add it to the command line args */
118 curr += sprintf(curr, "%s ", addrs[i]);
120 addr_len = strlen(addrs[0]);
122 /* parse the output and build a new backtrace */
123 e->bt_strings = xbt_new(char *, e->used);
125 VERB1("Fire a first command: '%s'", cmd);
126 pipe = popen(cmd, "r");
128 CRITICAL0("Cannot fork addr2line to display the backtrace");
132 for (i = 0; i < e->used; i++) {
133 DEBUG2("Looking for symbol %d, addr = '%s'", i, addrs[i]);
134 fgets(line_func, 1024, pipe);
135 line_func[strlen(line_func) - 1] = '\0';
136 fgets(line_pos, 1024, pipe);
137 line_pos[strlen(line_pos) - 1] = '\0';
139 if (strcmp("??", line_func)) {
140 DEBUG2("Found static symbol %s() at %s", line_func, line_pos);
141 e->bt_strings[i] = bprintf("** In %s() at %s", line_func, line_pos);
143 /* Damn. The symbol is in a dynamic library. Let's get wild */
148 long int addr, offset = 0;
155 /* let's look for the offset of this library in our addressing space */
156 maps_name = bprintf("/proc/%d/maps", (int) getpid());
157 maps = fopen(maps_name, "r");
159 sscanf(addrs[i], "%lx", &addr);
160 sprintf(maps_buff, "%#lx", addr);
162 if (strcmp(addrs[i], maps_buff)) {
163 CRITICAL2("Cannot parse backtrace address '%s' (addr=%#lx)",
166 DEBUG2("addr=%s (as string) =%#lx (as number)", addrs[i], addr);
169 long int first, last;
171 if (fgets(maps_buff, 512, maps) == NULL)
174 maps_buff[strlen(maps_buff) - 1] = '\0';
175 DEBUG1("map line: %s", maps_buff);
177 sscanf(maps_buff, "%lx", &first);
178 p = strchr(maps_buff, '-') + 1;
179 sscanf(p, "%lx", &last);
180 if (first < addr && addr < last) {
185 DEBUG3("%#lx in [%#lx-%#lx]", addr, first, last);
187 ("Symbol found, map lines not further displayed (even if looking for next ones)");
195 ("Problem while reading the maps file. Following backtrace will be mangled.");
196 DEBUG1("No dynamic. Static symbol: %s", backtrace_syms[i]);
197 e->bt_strings[i] = bprintf("** In ?? (%s)", backtrace_syms[i]);
201 /* Ok, Found the offset of the maps line containing the searched symbol.
202 We now need to substract this from the address we got from backtrace.
206 addrs[i] = bprintf("0x%0*lx", addr_len - 2, addr - offset);
207 DEBUG2("offset=%#lx new addr=%s", offset, addrs[i]);
209 /* Got it. We have our new address. Let's get the library path and we
211 p = xbt_strdup(backtrace_syms[i]);
213 /* library path not displayed in the map file either... */
215 sprintf(line_func, "??");
217 p2 = strrchr(p, '(');
220 p2 = strrchr(p, ' ');
224 /* Here we go, fire an addr2line up */
225 subcmd = bprintf("%s -f -e %s %s", ADDR2LINE, p, addrs[i]);
227 VERB1("Fire a new command: '%s'", subcmd);
228 subpipe = popen(subcmd, "r");
230 CRITICAL0("Cannot fork addr2line to display the backtrace");
233 fgets(line_func, 1024, subpipe);
234 line_func[strlen(line_func) - 1] = '\0';
235 fgets(line_pos, 1024, subpipe);
236 line_pos[strlen(line_pos) - 1] = '\0';
241 /* check whether the trick worked */
242 if (strcmp("??", line_func)) {
243 DEBUG2("Found dynamic symbol %s() at %s", line_func, line_pos);
244 e->bt_strings[i] = bprintf("** In %s() at %s", line_func, line_pos);
246 /* damn, nothing to do here. Let's print the raw address */
247 DEBUG1("Dynamic symbol not found. Raw address = %s",
249 e->bt_strings[i] = bprintf("** In ?? at %s", backtrace_syms[i]);
255 /* Mask the bottom of the stack */
256 if (!strncmp("main", line_func, strlen("main")) ||
257 !strncmp("xbt_thread_context_wrapper", line_func,
258 strlen("xbt_thread_context_wrapper"))) {
261 for (j = i + 1; j < e->used; j++)
266 ("xbt_thread_context_wrapper", line_func,
267 strlen("xbt_thread_context_wrapper"))) {
269 e->bt_strings[i] = bprintf("** (in a separate thread)");
277 free(backtrace_syms);