Logo AND Algorithmique Numérique Distribuée

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