1 /* $Id: ex.c 5173 2008-01-07 22:10:52Z mquinson $ */
3 /* backtrace_linux - backtrace displaying on linux platform */
4 /* This file is included by ex.c on need (have execinfo.h, popen & addrline)*/
6 /* Copyright (c) 2007 The SimGrid team */
7 /* All rights reserved. */
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. */
12 extern char **environ; /* the environment, as specified by the opengroup */
14 void xbt_ex_setup_backtrace(xbt_ex_t *e) {
16 /* to get the backtrace from the libc */
17 char **backtrace = backtrace_symbols (e->bt, e->used);
19 /* To build the commandline of addr2line */
22 /* to extract the addresses from the backtrace */
23 char **addrs=xbt_new(char*,e->used);
26 /* To read the output of addr2line */
28 char line_func[1024],line_pos[1024];
30 /* size (in char) of pointers on this arch */
33 /* To search for the right executable path when not trivial */
35 char *binary_name = NULL;
37 /* Some arches only have stubs of backtrace, no implementation (hppa comes to mind) */
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 */
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, ":");
50 xbt_dynar_foreach(path, cpt, data) {
53 binary_name = bprintf("%s/%s",data,xbt_binary_name);
54 if (!stat(binary_name,&stat_buf)) {
56 DEBUG1("Looked in the PATH for the binary. Found %s",binary_name);
57 xbt_dynar_free(&path);
61 if (stat(binary_name,&stat_buf)) {
64 e->bt_strings = xbt_new(char*,1);
65 e->bt_strings[0] = bprintf("(binary '%s' not found the path)",xbt_binary_name);
68 xbt_dynar_free(&path);
73 binary_name = xbt_strdup(xbt_binary_name);
75 cmd = curr = xbt_new(char,strlen(ADDR2LINE)+25+strlen(binary_name)+32*e->used);
77 curr += sprintf(curr,"%s -f -e %s ",ADDR2LINE,binary_name);
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);
86 if (strcmp(buff,"(nil)"))
87 addrs[i]=bprintf("%s", buff);
89 addrs[i]=bprintf("0x0");
90 DEBUG3("Set up a new address: %d, '%s'(%p)", i, addrs[i], addrs[i]);
92 /* Add it to the command line args */
93 curr+=sprintf(curr,"%s ",addrs[i]);
95 addr_len = strlen(addrs[0]);
97 /* parse the output and build a new backtrace */
98 e->bt_strings = xbt_new(char*,e->used);
100 VERB1("Fire a first command: '%s'", cmd);
101 pipe = popen(cmd, "r");
103 CRITICAL0("Cannot fork addr2line to display the backtrace");
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';
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);
118 /* Damn. The symbol is in a dynamic library. Let's get wild */
123 long int addr,offset=0;
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");
134 sscanf(addrs[i],"%lx",&addr);
135 sprintf(maps_buff,"%#lx",addr);
137 if (strcmp(addrs[i],maps_buff)) {
138 CRITICAL2("Cannot parse backtrace address '%s' (addr=%#lx)",
141 DEBUG2("addr=%s (as string) =%#lx (as number)",addrs[i],addr);
144 long int first, last;
145 if (fgets(maps_buff,512,maps) == NULL)
148 maps_buff[strlen(maps_buff) -1]='\0';
149 DEBUG1("map line: %s", maps_buff);
151 sscanf(maps_buff,"%lx",&first);
152 p=strchr(maps_buff,'-')+1;
153 sscanf(p,"%lx",&last);
154 if (first < addr && addr < last) {
159 DEBUG3("%#lx in [%#lx-%#lx]", addr, first,last);
160 DEBUG0("Symbol found, map lines not further displayed (even if looking for next ones)");
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]);
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.
178 addrs[i] = bprintf("0x%0*lx",addr_len-2,addr-offset);
179 DEBUG2("offset=%#lx new addr=%s",offset,addrs[i]);
181 /* Got it. We have our new address. Let's get the library path and we
183 p = xbt_strdup(backtrace[i]);
185 /* library path not displayed in the map file either... */
187 sprintf(line_func,"??");
194 /* Here we go, fire an addr2line up */
195 subcmd = bprintf("%s -f -e %s %s",ADDR2LINE,p, addrs[i]);
197 VERB1("Fire a new command: '%s'",subcmd);
198 subpipe = popen(subcmd,"r");
200 CRITICAL0("Cannot fork addr2line to display the backtrace");
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';
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);
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]);
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"))) {
228 for (j=i+1; j<e->used; j++)
232 if (!strncmp("xbt_thread_context_wrapper",line_func,strlen("xbt_thread_context_wrapper"))) {
234 e->bt_strings[i] = bprintf("** (in a separate thread)");