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++) {
134 DEBUG2("Looking for symbol %d, addr = '%s'", i, addrs[i]);
135 fgets_res = fgets(line_func, 1024, pipe);
136 if (fgets_res == NULL)
137 THROW2(system_error,0, "Cannot run fgets to look for symbol %d, addr %s",i, addrs[i]);
138 line_func[strlen(line_func) - 1] = '\0';
139 fgets_res = fgets(line_pos, 1024, pipe);
140 if (fgets_res == NULL)
141 THROW2(system_error,0, "Cannot run fgets to look for symbol %d, addr %s",i, addrs[i]);
142 line_pos[strlen(line_pos) - 1] = '\0';
144 if (strcmp("??", line_func)) {
145 DEBUG2("Found static symbol %s() at %s", line_func, line_pos);
146 e->bt_strings[i] = bprintf("** In %s() at %s", line_func, line_pos);
148 /* Damn. The symbol is in a dynamic library. Let's get wild */
153 long int addr, offset = 0;
160 /* let's look for the offset of this library in our addressing space */
161 maps_name = bprintf("/proc/%d/maps", (int) getpid());
162 maps = fopen(maps_name, "r");
164 sscanf(addrs[i], "%lx", &addr);
165 sprintf(maps_buff, "%#lx", addr);
167 if (strcmp(addrs[i], maps_buff)) {
168 CRITICAL2("Cannot parse backtrace address '%s' (addr=%#lx)",
171 DEBUG2("addr=%s (as string) =%#lx (as number)", addrs[i], addr);
174 long int first, last;
176 if (fgets(maps_buff, 512, maps) == NULL)
179 maps_buff[strlen(maps_buff) - 1] = '\0';
180 DEBUG1("map line: %s", maps_buff);
182 sscanf(maps_buff, "%lx", &first);
183 p = strchr(maps_buff, '-') + 1;
184 sscanf(p, "%lx", &last);
185 if (first < addr && addr < last) {
190 DEBUG3("%#lx in [%#lx-%#lx]", addr, first, last);
192 ("Symbol found, map lines not further displayed (even if looking for next ones)");
200 ("Problem while reading the maps file. Following backtrace will be mangled.");
201 DEBUG1("No dynamic. Static symbol: %s", backtrace_syms[i]);
202 e->bt_strings[i] = bprintf("** In ?? (%s)", backtrace_syms[i]);
206 /* Ok, Found the offset of the maps line containing the searched symbol.
207 We now need to substract this from the address we got from backtrace.
211 addrs[i] = bprintf("0x%0*lx", addr_len - 2, addr - offset);
212 DEBUG2("offset=%#lx new addr=%s", offset, addrs[i]);
214 /* Got it. We have our new address. Let's get the library path and we
216 p = xbt_strdup(backtrace_syms[i]);
218 /* library path not displayed in the map file either... */
220 sprintf(line_func, "??");
222 p2 = strrchr(p, '(');
225 p2 = strrchr(p, ' ');
229 /* Here we go, fire an addr2line up */
230 subcmd = bprintf("%s -f -e %s %s", ADDR2LINE, p, addrs[i]);
232 VERB1("Fire a new command: '%s'", subcmd);
233 subpipe = popen(subcmd, "r");
235 CRITICAL0("Cannot fork addr2line to display the backtrace");
238 fgets_res=fgets(line_func, 1024, subpipe);
239 if (fgets_res == NULL)
240 THROW1(system_error,0, "Cannot read result of subcommand %s",subcmd);
241 line_func[strlen(line_func) - 1] = '\0';
242 fgets_res = fgets(line_pos, 1024, subpipe);
243 if (fgets_res == NULL)
244 THROW1(system_error,0, "Cannot read result of subcommand %s",subcmd);
245 line_pos[strlen(line_pos) - 1] = '\0';
250 /* check whether the trick worked */
251 if (strcmp("??", line_func)) {
252 DEBUG2("Found dynamic symbol %s() at %s", line_func, line_pos);
253 e->bt_strings[i] = bprintf("** In %s() at %s", line_func, line_pos);
255 /* damn, nothing to do here. Let's print the raw address */
256 DEBUG1("Dynamic symbol not found. Raw address = %s",
258 e->bt_strings[i] = bprintf("** In ?? at %s", backtrace_syms[i]);
264 /* Mask the bottom of the stack */
265 if (!strncmp("main", line_func, strlen("main")) ||
266 !strncmp("xbt_thread_context_wrapper", line_func,
267 strlen("xbt_thread_context_wrapper"))) {
270 for (j = i + 1; j < e->used; j++)
275 ("xbt_thread_context_wrapper", line_func,
276 strlen("xbt_thread_context_wrapper"))) {
278 e->bt_strings[i] = bprintf("** (in a separate thread)");
286 free(backtrace_syms);