Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Separate the dummy implementation of the backtraces into its own file (backtrace_dumm...
[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 extern char **environ;          /* the environment, as specified by the opengroup */
13
14 /* Module creation/destruction: nothing to do on linux */
15 void xbt_backtrace_init(void) { }
16 void xbt_backtrace_exit(void) { }
17
18 void xbt_ex_setup_backtrace(xbt_ex_t * e)
19 {
20   int i;
21
22   /* to get the backtrace from the libc */
23   char **backtrace_syms = backtrace_symbols(e->bt, e->used);
24
25   /* To build the commandline of addr2line */
26   char *cmd, *curr;
27
28   /* to extract the addresses from the backtrace */
29   char **addrs = xbt_new(char *, e->used);
30   char buff[256], *p;
31
32   /* To read the output of addr2line */
33   FILE *pipe;
34   char line_func[1024], line_pos[1024];
35
36   /* size (in char) of pointers on this arch */
37   int addr_len = 0;
38
39   /* To search for the right executable path when not trivial */
40   struct stat stat_buf;
41   char *binary_name = NULL;
42
43   e->used = backtrace((void **) e->bt, XBT_BACKTRACE_SIZE);
44   e->bt_strings = NULL;
45
46   /* Some arches only have stubs of backtrace, no implementation (hppa comes to mind) */
47   if (!e->used)
48     return;
49
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 */
53     int i;
54
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, ":");
58         unsigned int cpt;
59         char *data;
60
61         xbt_dynar_foreach(path, cpt, data) {
62           if (binary_name)
63             free(binary_name);
64           binary_name = bprintf("%s/%s", data, xbt_binary_name);
65           if (!stat(binary_name, &stat_buf)) {
66             /* Found. */
67             DEBUG1("Looked in the PATH for the binary. Found %s",
68                    binary_name);
69             xbt_dynar_free(&path);
70             break;
71           }
72         }
73         if (stat(binary_name, &stat_buf)) {
74           /* not found */
75           e->used = 1;
76           e->bt_strings = xbt_new(char *, 1);
77
78           e->bt_strings[0] =
79             bprintf("(binary '%s' not found the path)", xbt_binary_name);
80           return;
81         }
82         xbt_dynar_free(&path);
83         break;
84       }
85     }
86   } else {
87     binary_name = xbt_strdup(xbt_binary_name);
88   }
89   cmd = curr =
90     xbt_new(char,
91             strlen(ADDR2LINE) + 25 + strlen(binary_name) + 32 * e->used);
92
93   curr += sprintf(curr, "%s -f -e %s ", ADDR2LINE, binary_name);
94   free(binary_name);
95
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, ']');
101     *p = '\0';
102     if (strcmp(buff, "(nil)"))
103       addrs[i] = bprintf("%s", buff);
104     else
105       addrs[i] = bprintf("0x0");
106     DEBUG3("Set up a new address: %d, '%s'(%p)", i, addrs[i], addrs[i]);
107
108     /* Add it to the command line args */
109     curr += sprintf(curr, "%s ", addrs[i]);
110   }
111   addr_len = strlen(addrs[0]);
112
113   /* parse the output and build a new backtrace */
114   e->bt_strings = xbt_new(char *, e->used);
115
116   VERB1("Fire a first command: '%s'", cmd);
117   pipe = popen(cmd, "r");
118   if (!pipe) {
119     CRITICAL0("Cannot fork addr2line to display the backtrace");
120     abort();
121   }
122
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';
129
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);
133     } else {
134       /* Damn. The symbol is in a dynamic library. Let's get wild */
135       char *maps_name;
136       FILE *maps;
137       char maps_buff[512];
138
139       long int addr, offset = 0;
140       char *p, *p2;
141
142       char *subcmd;
143       FILE *subpipe;
144       int found = 0;
145
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");
149
150       sscanf(addrs[i], "%lx", &addr);
151       sprintf(maps_buff, "%#lx", addr);
152
153       if (strcmp(addrs[i], maps_buff)) {
154         CRITICAL2("Cannot parse backtrace address '%s' (addr=%#lx)",
155                   addrs[i], addr);
156       }
157       DEBUG2("addr=%s (as string) =%#lx (as number)", addrs[i], addr);
158
159       while (!found) {
160         long int first, last;
161
162         if (fgets(maps_buff, 512, maps) == NULL)
163           break;
164         if (i == 0) {
165           maps_buff[strlen(maps_buff) - 1] = '\0';
166           DEBUG1("map line: %s", maps_buff);
167         }
168         sscanf(maps_buff, "%lx", &first);
169         p = strchr(maps_buff, '-') + 1;
170         sscanf(p, "%lx", &last);
171         if (first < addr && addr < last) {
172           offset = first;
173           found = 1;
174         }
175         if (found) {
176           DEBUG3("%#lx in [%#lx-%#lx]", addr, first, last);
177           DEBUG0
178             ("Symbol found, map lines not further displayed (even if looking for next ones)");
179         }
180       }
181       fclose(maps);
182       free(maps_name);
183
184       if (!found) {
185         VERB0
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]);
189         continue;
190       }
191
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.
194        */
195
196       free(addrs[i]);
197       addrs[i] = bprintf("0x%0*lx", addr_len - 2, addr - offset);
198       DEBUG2("offset=%#lx new addr=%s", offset, addrs[i]);
199
200       /* Got it. We have our new address. Let's get the library path and we 
201          are set */
202       p = xbt_strdup(backtrace_syms[i]);
203       if (p[0] == '[') {
204         /* library path not displayed in the map file either... */
205         free(p);
206         sprintf(line_func, "??");
207       } else {
208         p2 = strrchr(p, '(');
209         if (p2)
210           *p2 = '\0';
211         p2 = strrchr(p, ' ');
212         if (p2)
213           *p2 = '\0';
214
215         /* Here we go, fire an addr2line up */
216         subcmd = bprintf("%s -f -e %s %s", ADDR2LINE, p, addrs[i]);
217         free(p);
218         VERB1("Fire a new command: '%s'", subcmd);
219         subpipe = popen(subcmd, "r");
220         if (!subpipe) {
221           CRITICAL0("Cannot fork addr2line to display the backtrace");
222           abort();
223         }
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';
228         pclose(subpipe);
229         free(subcmd);
230       }
231
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);
236       } else {
237         /* damn, nothing to do here. Let's print the raw address */
238         DEBUG1("Dynamic symbol not found. Raw address = %s",
239                backtrace_syms[i]);
240         e->bt_strings[i] = bprintf("**   In ?? at %s", backtrace_syms[i]);
241       }
242
243     }
244     free(addrs[i]);
245
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"))) {
250       int j;
251
252       for (j = i + 1; j < e->used; j++)
253         free(addrs[j]);
254       e->used = i;
255
256       if (!strncmp
257           ("xbt_thread_context_wrapper", line_func,
258            strlen("xbt_thread_context_wrapper"))) {
259         e->used++;
260         e->bt_strings[i] = bprintf("**   (in a separate thread)");
261       }
262     }
263
264
265   }
266   pclose(pipe);
267   free(addrs);
268   free(backtrace_syms);
269   free(cmd);
270 }