Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix: correct trace mask checking
[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 {
24 }
25
26 void xbt_backtrace_exit(void)
27 {
28 }
29
30 void xbt_backtrace_current(xbt_ex_t * e)
31 {
32   e->used = backtrace((void **) e->bt, XBT_BACKTRACE_SIZE);
33 }
34
35
36 void xbt_ex_setup_backtrace(xbt_ex_t * e)
37 {
38   int i;
39
40   /* to get the backtrace from the libc */
41   char **backtrace_syms;
42
43   /* To build the commandline of addr2line */
44   char *cmd, *curr;
45
46   /* to extract the addresses from the backtrace */
47   char **addrs;
48   char buff[256], *p;
49
50   /* To read the output of addr2line */
51   FILE *pipe;
52   char line_func[1024], line_pos[1024];
53
54   /* size (in char) of pointers on this arch */
55   int addr_len = 0;
56
57   /* To search for the right executable path when not trivial */
58   struct stat stat_buf;
59   char *binary_name = NULL;
60
61   xbt_assert0(e
62               && e->used,
63               "Backtrace not setup yet, cannot set it up for display");
64
65   backtrace_syms = backtrace_symbols(e->bt, e->used);
66   /* ignore first one, which is this xbt_backtrace_current() */
67   e->used--;
68   memmove(backtrace_syms, backtrace_syms + 1, sizeof(char *) * e->used);
69   addrs = xbt_new(char *, e->used);
70
71   e->bt_strings = NULL;
72
73   /* Some arches only have stubs of backtrace, no implementation (hppa comes to mind) */
74   if (!e->used)
75     return;
76
77   /* build the commandline */
78   if (stat(xbt_binary_name, &stat_buf)) {
79     /* Damn. binary not in current dir. We'll have to dig the PATH to find it */
80     int i;
81
82     for (i = 0; environ[i]; i++) {
83       if (!strncmp("PATH=", environ[i], 5)) {
84         xbt_dynar_t path = xbt_str_split(environ[i] + 5, ":");
85         unsigned int cpt;
86         char *data;
87
88         xbt_dynar_foreach(path, cpt, data) {
89           if (binary_name)
90             free(binary_name);
91           binary_name = bprintf("%s/%s", data, xbt_binary_name);
92           if (!stat(binary_name, &stat_buf)) {
93             /* Found. */
94             DEBUG1("Looked in the PATH for the binary. Found %s",
95                    binary_name);
96             xbt_dynar_free(&path);
97             break;
98           }
99         }
100         if (stat(binary_name, &stat_buf)) {
101           /* not found */
102           e->used = 1;
103           e->bt_strings = xbt_new(char *, 1);
104
105           e->bt_strings[0] =
106             bprintf("(binary '%s' not found the path)", xbt_binary_name);
107           return;
108         }
109         xbt_dynar_free(&path);
110         break;
111       }
112     }
113   } else {
114     binary_name = xbt_strdup(xbt_binary_name);
115   }
116   cmd = curr =
117     xbt_new(char,
118             strlen(ADDR2LINE) + 25 + strlen(binary_name) + 32 * e->used);
119
120   curr += sprintf(curr, "%s -f -e %s ", ADDR2LINE, binary_name);
121   free(binary_name);
122
123   for (i = 0; i < e->used; i++) {
124     /* retrieve this address */
125     DEBUG2("Retrieving address number %d from '%s'", i, backtrace_syms[i]);
126     snprintf(buff, 256, "%s", strchr(backtrace_syms[i], '[') + 1);
127     p = strchr(buff, ']');
128     *p = '\0';
129     if (strcmp(buff, "(nil)"))
130       addrs[i] = bprintf("%s", buff);
131     else
132       addrs[i] = bprintf("0x0");
133     DEBUG3("Set up a new address: %d, '%s'(%p)", i, addrs[i], addrs[i]);
134
135     /* Add it to the command line args */
136     curr += sprintf(curr, "%s ", addrs[i]);
137   }
138   addr_len = strlen(addrs[0]);
139
140   /* parse the output and build a new backtrace */
141   e->bt_strings = xbt_new(char *, e->used);
142
143   VERB1("Fire a first command: '%s'", cmd);
144   pipe = popen(cmd, "r");
145   if (!pipe) {
146     CRITICAL0("Cannot fork addr2line to display the backtrace");
147     abort();
148   }
149
150   for (i = 0; i < e->used; i++) {
151     char *fgets_res;
152     DEBUG2("Looking for symbol %d, addr = '%s'", i, addrs[i]);
153     fgets_res = fgets(line_func, 1024, pipe);
154     if (fgets_res == NULL)
155       THROW2(system_error, 0,
156              "Cannot run fgets to look for symbol %d, addr %s", i, addrs[i]);
157     line_func[strlen(line_func) - 1] = '\0';
158     fgets_res = fgets(line_pos, 1024, pipe);
159     if (fgets_res == NULL)
160       THROW2(system_error, 0,
161              "Cannot run fgets to look for symbol %d, addr %s", i, addrs[i]);
162     line_pos[strlen(line_pos) - 1] = '\0';
163
164     if (strcmp("??", line_func)) {
165       DEBUG2("Found static symbol %s() at %s", line_func, line_pos);
166       e->bt_strings[i] = bprintf("**   In %s() at %s", line_func, line_pos);
167     } else {
168       /* Damn. The symbol is in a dynamic library. Let's get wild */
169       char *maps_name;
170       FILE *maps;
171       char maps_buff[512];
172
173       long int addr, offset = 0;
174       char *p, *p2;
175
176       char *subcmd;
177       FILE *subpipe;
178       int found = 0;
179
180       /* let's look for the offset of this library in our addressing space */
181       maps_name = bprintf("/proc/%d/maps", (int) getpid());
182       maps = fopen(maps_name, "r");
183
184       sscanf(addrs[i], "%lx", &addr);
185       sprintf(maps_buff, "%#lx", addr);
186
187       if (strcmp(addrs[i], maps_buff)) {
188         CRITICAL2("Cannot parse backtrace address '%s' (addr=%#lx)",
189                   addrs[i], addr);
190       }
191       DEBUG2("addr=%s (as string) =%#lx (as number)", addrs[i], addr);
192
193       while (!found) {
194         long int first, last;
195
196         if (fgets(maps_buff, 512, maps) == NULL)
197           break;
198         if (i == 0) {
199           maps_buff[strlen(maps_buff) - 1] = '\0';
200           DEBUG1("map line: %s", maps_buff);
201         }
202         sscanf(maps_buff, "%lx", &first);
203         p = strchr(maps_buff, '-') + 1;
204         sscanf(p, "%lx", &last);
205         if (first < addr && addr < last) {
206           offset = first;
207           found = 1;
208         }
209         if (found) {
210           DEBUG3("%#lx in [%#lx-%#lx]", addr, first, last);
211           DEBUG0
212             ("Symbol found, map lines not further displayed (even if looking for next ones)");
213         }
214       }
215       fclose(maps);
216       free(maps_name);
217
218       if (!found) {
219         VERB0
220           ("Problem while reading the maps file. Following backtrace will be mangled.");
221         DEBUG1("No dynamic. Static symbol: %s", backtrace_syms[i]);
222         e->bt_strings[i] = bprintf("**   In ?? (%s)", backtrace_syms[i]);
223         continue;
224       }
225
226       /* Ok, Found the offset of the maps line containing the searched symbol.
227          We now need to substract this from the address we got from backtrace.
228        */
229
230       free(addrs[i]);
231       addrs[i] = bprintf("0x%0*lx", addr_len - 2, addr - offset);
232       DEBUG2("offset=%#lx new addr=%s", offset, addrs[i]);
233
234       /* Got it. We have our new address. Let's get the library path and we
235          are set */
236       p = xbt_strdup(backtrace_syms[i]);
237       if (p[0] == '[') {
238         /* library path not displayed in the map file either... */
239         free(p);
240         sprintf(line_func, "??");
241       } else {
242         p2 = strrchr(p, '(');
243         if (p2)
244           *p2 = '\0';
245         p2 = strrchr(p, ' ');
246         if (p2)
247           *p2 = '\0';
248
249         /* Here we go, fire an addr2line up */
250         subcmd = bprintf("%s -f -e %s %s", ADDR2LINE, p, addrs[i]);
251         free(p);
252         VERB1("Fire a new command: '%s'", subcmd);
253         subpipe = popen(subcmd, "r");
254         if (!subpipe) {
255           CRITICAL0("Cannot fork addr2line to display the backtrace");
256           abort();
257         }
258         fgets_res = fgets(line_func, 1024, subpipe);
259         if (fgets_res == NULL)
260           THROW1(system_error, 0, "Cannot read result of subcommand %s",
261                  subcmd);
262         line_func[strlen(line_func) - 1] = '\0';
263         fgets_res = fgets(line_pos, 1024, subpipe);
264         if (fgets_res == NULL)
265           THROW1(system_error, 0, "Cannot read result of subcommand %s",
266                  subcmd);
267         line_pos[strlen(line_pos) - 1] = '\0';
268         pclose(subpipe);
269         free(subcmd);
270       }
271
272       /* check whether the trick worked */
273       if (strcmp("??", line_func)) {
274         DEBUG2("Found dynamic symbol %s() at %s", line_func, line_pos);
275         e->bt_strings[i] = bprintf("**   In %s() at %s", line_func, line_pos);
276       } else {
277         /* damn, nothing to do here. Let's print the raw address */
278         DEBUG1("Dynamic symbol not found. Raw address = %s",
279                backtrace_syms[i]);
280         e->bt_strings[i] = bprintf("**   In ?? at %s", backtrace_syms[i]);
281       }
282
283     }
284     free(addrs[i]);
285
286     /* Mask the bottom of the stack */
287     if (!strncmp("main", line_func, strlen("main")) ||
288         !strncmp("xbt_thread_context_wrapper", line_func,
289                  strlen("xbt_thread_context_wrapper"))) {
290       int j;
291
292       for (j = i + 1; j < e->used; j++)
293         free(addrs[j]);
294       e->used = i;
295
296       if (!strncmp
297           ("xbt_thread_context_wrapper", line_func,
298            strlen("xbt_thread_context_wrapper"))) {
299         e->used++;
300         e->bt_strings[i] = bprintf("**   (in a separate thread)");
301       }
302     }
303
304
305   }
306   pclose(pipe);
307   free(addrs);
308   free(backtrace_syms);
309   free(cmd);
310 }