Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix a god damn race condition: make sure nobody changes the dynar->used value before...
[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_backtrace_current(xbt_ex_t * e) {
19   e->used = backtrace((void **) e->bt, XBT_BACKTRACE_SIZE);
20 }
21
22   
23 void xbt_ex_setup_backtrace(xbt_ex_t * e)
24 {
25   int i;
26
27   /* to get the backtrace from the libc */
28   char **backtrace_syms;
29
30   /* To build the commandline of addr2line */
31   char *cmd, *curr;
32
33   /* to extract the addresses from the backtrace */
34   char **addrs;
35   char buff[256], *p;
36
37   /* To read the output of addr2line */
38   FILE *pipe;
39   char line_func[1024], line_pos[1024];
40
41   /* size (in char) of pointers on this arch */
42   int addr_len = 0;
43
44   /* To search for the right executable path when not trivial */
45   struct stat stat_buf;
46   char *binary_name = NULL;
47    
48   xbt_assert0(e && e->used,"Backtrace not setup yet, cannot set it up for display");
49    
50   backtrace_syms = backtrace_symbols(e->bt, e->used);
51   addrs = xbt_new(char *, e->used);
52    
53   e->bt_strings = NULL;
54
55   /* Some arches only have stubs of backtrace, no implementation (hppa comes to mind) */
56   if (!e->used)
57     return;
58
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 */
62     int i;
63
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, ":");
67         unsigned int cpt;
68         char *data;
69
70         xbt_dynar_foreach(path, cpt, data) {
71           if (binary_name)
72             free(binary_name);
73           binary_name = bprintf("%s/%s", data, xbt_binary_name);
74           if (!stat(binary_name, &stat_buf)) {
75             /* Found. */
76             DEBUG1("Looked in the PATH for the binary. Found %s",
77                    binary_name);
78             xbt_dynar_free(&path);
79             break;
80           }
81         }
82         if (stat(binary_name, &stat_buf)) {
83           /* not found */
84           e->used = 1;
85           e->bt_strings = xbt_new(char *, 1);
86
87           e->bt_strings[0] =
88             bprintf("(binary '%s' not found the path)", xbt_binary_name);
89           return;
90         }
91         xbt_dynar_free(&path);
92         break;
93       }
94     }
95   } else {
96     binary_name = xbt_strdup(xbt_binary_name);
97   }
98   cmd = curr =
99     xbt_new(char,
100             strlen(ADDR2LINE) + 25 + strlen(binary_name) + 32 * e->used);
101
102   curr += sprintf(curr, "%s -f -e %s ", ADDR2LINE, binary_name);
103   free(binary_name);
104
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, ']');
110     *p = '\0';
111     if (strcmp(buff, "(nil)"))
112       addrs[i] = bprintf("%s", buff);
113     else
114       addrs[i] = bprintf("0x0");
115     DEBUG3("Set up a new address: %d, '%s'(%p)", i, addrs[i], addrs[i]);
116
117     /* Add it to the command line args */
118     curr += sprintf(curr, "%s ", addrs[i]);
119   }
120   addr_len = strlen(addrs[0]);
121
122   /* parse the output and build a new backtrace */
123   e->bt_strings = xbt_new(char *, e->used);
124
125   VERB1("Fire a first command: '%s'", cmd);
126   pipe = popen(cmd, "r");
127   if (!pipe) {
128     CRITICAL0("Cannot fork addr2line to display the backtrace");
129     abort();
130   }
131
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';
138
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);
142     } else {
143       /* Damn. The symbol is in a dynamic library. Let's get wild */
144       char *maps_name;
145       FILE *maps;
146       char maps_buff[512];
147
148       long int addr, offset = 0;
149       char *p, *p2;
150
151       char *subcmd;
152       FILE *subpipe;
153       int found = 0;
154
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");
158
159       sscanf(addrs[i], "%lx", &addr);
160       sprintf(maps_buff, "%#lx", addr);
161
162       if (strcmp(addrs[i], maps_buff)) {
163         CRITICAL2("Cannot parse backtrace address '%s' (addr=%#lx)",
164                   addrs[i], addr);
165       }
166       DEBUG2("addr=%s (as string) =%#lx (as number)", addrs[i], addr);
167
168       while (!found) {
169         long int first, last;
170
171         if (fgets(maps_buff, 512, maps) == NULL)
172           break;
173         if (i == 0) {
174           maps_buff[strlen(maps_buff) - 1] = '\0';
175           DEBUG1("map line: %s", maps_buff);
176         }
177         sscanf(maps_buff, "%lx", &first);
178         p = strchr(maps_buff, '-') + 1;
179         sscanf(p, "%lx", &last);
180         if (first < addr && addr < last) {
181           offset = first;
182           found = 1;
183         }
184         if (found) {
185           DEBUG3("%#lx in [%#lx-%#lx]", addr, first, last);
186           DEBUG0
187             ("Symbol found, map lines not further displayed (even if looking for next ones)");
188         }
189       }
190       fclose(maps);
191       free(maps_name);
192
193       if (!found) {
194         VERB0
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]);
198         continue;
199       }
200
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.
203        */
204
205       free(addrs[i]);
206       addrs[i] = bprintf("0x%0*lx", addr_len - 2, addr - offset);
207       DEBUG2("offset=%#lx new addr=%s", offset, addrs[i]);
208
209       /* Got it. We have our new address. Let's get the library path and we 
210          are set */
211       p = xbt_strdup(backtrace_syms[i]);
212       if (p[0] == '[') {
213         /* library path not displayed in the map file either... */
214         free(p);
215         sprintf(line_func, "??");
216       } else {
217         p2 = strrchr(p, '(');
218         if (p2)
219           *p2 = '\0';
220         p2 = strrchr(p, ' ');
221         if (p2)
222           *p2 = '\0';
223
224         /* Here we go, fire an addr2line up */
225         subcmd = bprintf("%s -f -e %s %s", ADDR2LINE, p, addrs[i]);
226         free(p);
227         VERB1("Fire a new command: '%s'", subcmd);
228         subpipe = popen(subcmd, "r");
229         if (!subpipe) {
230           CRITICAL0("Cannot fork addr2line to display the backtrace");
231           abort();
232         }
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';
237         pclose(subpipe);
238         free(subcmd);
239       }
240
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);
245       } else {
246         /* damn, nothing to do here. Let's print the raw address */
247         DEBUG1("Dynamic symbol not found. Raw address = %s",
248                backtrace_syms[i]);
249         e->bt_strings[i] = bprintf("**   In ?? at %s", backtrace_syms[i]);
250       }
251
252     }
253     free(addrs[i]);
254
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"))) {
259       int j;
260
261       for (j = i + 1; j < e->used; j++)
262         free(addrs[j]);
263       e->used = i;
264
265       if (!strncmp
266           ("xbt_thread_context_wrapper", line_func,
267            strlen("xbt_thread_context_wrapper"))) {
268         e->used++;
269         e->bt_strings[i] = bprintf("**   (in a separate thread)");
270       }
271     }
272
273
274   }
275   pclose(pipe);
276   free(addrs);
277   free(backtrace_syms);
278   free(cmd);
279 }