Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
do not display backtrace capturing stuff in the displayed backtrace (plus, be eclipse...
[simgrid.git] / src / xbt / backtrace_linux.c
1 /* $Id: ex.c 5173 2008-01-07 22:10:52Z mquinson $ */
2
3 /* backtrace_linux - backtrace displaying on linux platform                 */
4 /* This file is included by ex.c on need (have execinfo.h, popen & addrline)*/
5
6 /*  Copyright (c) 2007 The SimGrid team                                     */
7 /*  All rights reserved.                                                    */
8
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. */
11
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 */
13 #include "xbt/ex.h"
14 #include "xbt/str.h"
15 #include "xbt/module.h" /* xbt_binary_name */
16 #include "xbt_modinter.h" /* backtrace initialization headers */
17 /* end of "useless" inclusions */
18
19 extern char **environ;          /* the environment, as specified by the opengroup */
20
21 /* Module creation/destruction: nothing to do on linux */
22 void xbt_backtrace_init(void) { }
23 void xbt_backtrace_exit(void) { }
24
25 void xbt_backtrace_current(xbt_ex_t * e) {
26   e->used = backtrace((void **) e->bt, XBT_BACKTRACE_SIZE);
27 }
28
29
30 void xbt_ex_setup_backtrace(xbt_ex_t * e)
31 {
32   int i;
33
34   /* to get the backtrace from the libc */
35   char **backtrace_syms;
36
37   /* To build the commandline of addr2line */
38   char *cmd, *curr;
39
40   /* to extract the addresses from the backtrace */
41   char **addrs;
42   char buff[256], *p;
43
44   /* To read the output of addr2line */
45   FILE *pipe;
46   char line_func[1024], line_pos[1024];
47
48   /* size (in char) of pointers on this arch */
49   int addr_len = 0;
50
51   /* To search for the right executable path when not trivial */
52   struct stat stat_buf;
53   char *binary_name = NULL;
54
55   xbt_assert0(e && e->used,"Backtrace not setup yet, cannot set it up for display");
56
57   backtrace_syms = backtrace_symbols(e->bt, e->used);
58   /* ignore first one, which is this xbt_backtrace_current() */
59   e->used--;
60   memmove(backtrace_syms,backtrace_syms+1,sizeof(char*)*e->used);
61   addrs = xbt_new(char *, e->used);
62
63   e->bt_strings = NULL;
64
65   /* Some arches only have stubs of backtrace, no implementation (hppa comes to mind) */
66   if (!e->used)
67     return;
68
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 */
72     int i;
73
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, ":");
77         unsigned int cpt;
78         char *data;
79
80         xbt_dynar_foreach(path, cpt, data) {
81           if (binary_name)
82             free(binary_name);
83           binary_name = bprintf("%s/%s", data, xbt_binary_name);
84           if (!stat(binary_name, &stat_buf)) {
85             /* Found. */
86             DEBUG1("Looked in the PATH for the binary. Found %s",
87                    binary_name);
88             xbt_dynar_free(&path);
89             break;
90           }
91         }
92         if (stat(binary_name, &stat_buf)) {
93           /* not found */
94           e->used = 1;
95           e->bt_strings = xbt_new(char *, 1);
96
97           e->bt_strings[0] =
98             bprintf("(binary '%s' not found the path)", xbt_binary_name);
99           return;
100         }
101         xbt_dynar_free(&path);
102         break;
103       }
104     }
105   } else {
106     binary_name = xbt_strdup(xbt_binary_name);
107   }
108   cmd = curr =
109     xbt_new(char,
110             strlen(ADDR2LINE) + 25 + strlen(binary_name) + 32 * e->used);
111
112   curr += sprintf(curr, "%s -f -e %s ", ADDR2LINE, binary_name);
113   free(binary_name);
114
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, ']');
120     *p = '\0';
121     if (strcmp(buff, "(nil)"))
122       addrs[i] = bprintf("%s", buff);
123     else
124       addrs[i] = bprintf("0x0");
125     DEBUG3("Set up a new address: %d, '%s'(%p)", i, addrs[i], addrs[i]);
126
127     /* Add it to the command line args */
128     curr += sprintf(curr, "%s ", addrs[i]);
129   }
130   addr_len = strlen(addrs[0]);
131
132   /* parse the output and build a new backtrace */
133   e->bt_strings = xbt_new(char *, e->used);
134
135   VERB1("Fire a first command: '%s'", cmd);
136   pipe = popen(cmd, "r");
137   if (!pipe) {
138     CRITICAL0("Cannot fork addr2line to display the backtrace");
139     abort();
140   }
141
142   for (i = 0; i < e->used; i++) {
143     char *fgets_res;
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';
153
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);
157     } else {
158       /* Damn. The symbol is in a dynamic library. Let's get wild */
159       char *maps_name;
160       FILE *maps;
161       char maps_buff[512];
162
163       long int addr, offset = 0;
164       char *p, *p2;
165
166       char *subcmd;
167       FILE *subpipe;
168       int found = 0;
169
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");
173
174       sscanf(addrs[i], "%lx", &addr);
175       sprintf(maps_buff, "%#lx", addr);
176
177       if (strcmp(addrs[i], maps_buff)) {
178         CRITICAL2("Cannot parse backtrace address '%s' (addr=%#lx)",
179                   addrs[i], addr);
180       }
181       DEBUG2("addr=%s (as string) =%#lx (as number)", addrs[i], addr);
182
183       while (!found) {
184         long int first, last;
185
186         if (fgets(maps_buff, 512, maps) == NULL)
187           break;
188         if (i == 0) {
189           maps_buff[strlen(maps_buff) - 1] = '\0';
190           DEBUG1("map line: %s", maps_buff);
191         }
192         sscanf(maps_buff, "%lx", &first);
193         p = strchr(maps_buff, '-') + 1;
194         sscanf(p, "%lx", &last);
195         if (first < addr && addr < last) {
196           offset = first;
197           found = 1;
198         }
199         if (found) {
200           DEBUG3("%#lx in [%#lx-%#lx]", addr, first, last);
201           DEBUG0
202           ("Symbol found, map lines not further displayed (even if looking for next ones)");
203         }
204       }
205       fclose(maps);
206       free(maps_name);
207
208       if (!found) {
209         VERB0
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]);
213         continue;
214       }
215
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.
218        */
219
220       free(addrs[i]);
221       addrs[i] = bprintf("0x%0*lx", addr_len - 2, addr - offset);
222       DEBUG2("offset=%#lx new addr=%s", offset, addrs[i]);
223
224       /* Got it. We have our new address. Let's get the library path and we
225          are set */
226       p = xbt_strdup(backtrace_syms[i]);
227       if (p[0] == '[') {
228         /* library path not displayed in the map file either... */
229         free(p);
230         sprintf(line_func, "??");
231       } else {
232         p2 = strrchr(p, '(');
233         if (p2)
234           *p2 = '\0';
235         p2 = strrchr(p, ' ');
236         if (p2)
237           *p2 = '\0';
238
239         /* Here we go, fire an addr2line up */
240         subcmd = bprintf("%s -f -e %s %s", ADDR2LINE, p, addrs[i]);
241         free(p);
242         VERB1("Fire a new command: '%s'", subcmd);
243         subpipe = popen(subcmd, "r");
244         if (!subpipe) {
245           CRITICAL0("Cannot fork addr2line to display the backtrace");
246           abort();
247         }
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';
256         pclose(subpipe);
257         free(subcmd);
258       }
259
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);
264       } else {
265         /* damn, nothing to do here. Let's print the raw address */
266         DEBUG1("Dynamic symbol not found. Raw address = %s",
267                backtrace_syms[i]);
268         e->bt_strings[i] = bprintf("**   In ?? at %s", backtrace_syms[i]);
269       }
270
271     }
272     free(addrs[i]);
273
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"))) {
278       int j;
279
280       for (j = i + 1; j < e->used; j++)
281         free(addrs[j]);
282       e->used = i;
283
284       if (!strncmp
285           ("xbt_thread_context_wrapper", line_func,
286            strlen("xbt_thread_context_wrapper"))) {
287         e->used++;
288         e->bt_strings[i] = bprintf("**   (in a separate thread)");
289       }
290     }
291
292
293   }
294   pclose(pipe);
295   free(addrs);
296   free(backtrace_syms);
297   free(cmd);
298 }