printStack.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "error.H"
00028 #include "IStringStream.H"
00029 #include "OStringStream.H"
00030 #include "OSspecific.H"
00031 #include "IFstream.H"
00032 #include "readHexLabel.H"
00033
00034 #include <cxxabi.h>
00035 #include <execinfo.h>
00036 #include <dlfcn.h>
00037
00038
00039
00040 namespace Foam
00041 {
00042
00043
00044
00045 string pOpen(const string &cmd, label line=0)
00046 {
00047 const int MAX = 1000;
00048
00049 FILE *cmdPipe = popen(cmd.c_str(), "r");
00050
00051 if (cmdPipe)
00052 {
00053
00054 for (label cnt = 0; cnt <= line; cnt++)
00055 {
00056 char buffer[MAX];
00057 char* s = fgets(buffer, MAX-1, cmdPipe);
00058
00059 if (s == NULL)
00060 {
00061 return "";
00062 }
00063
00064 if (cnt == line)
00065 {
00066 string str(buffer);
00067 return str.substr(0, str.size()-1);
00068 }
00069 }
00070 pclose(cmdPipe);
00071 }
00072
00073 return "";
00074 }
00075
00076
00077
00078
00079
00080 void printSourceFileAndLine
00081 (
00082 Ostream& os,
00083 const HashTable<label, fileName>& addressMap,
00084 const fileName& filename,
00085 const word& address
00086 )
00087 {
00088 word myAddress = address;
00089
00090 if (filename.ext() == "so")
00091 {
00092
00093
00094 void *addr;
00095 sscanf(myAddress.c_str(), "%p",&addr);
00096
00097 Dl_info info;
00098
00099 dladdr(addr, &info);
00100
00101 unsigned long offset = ulong(info.dli_fbase);
00102
00103 IStringStream addressStr(address.substr(2));
00104 label addressValue = readHexLabel(addressStr);
00105 label relativeAddress = addressValue-offset;
00106
00107
00108 OStringStream nStream;
00109 nStream << "0x" << hex << relativeAddress;
00110 myAddress = nStream.str();
00111 }
00112
00113 if (filename[0] == '/')
00114 {
00115 string line = pOpen
00116 (
00117 "addr2line -f --demangle=auto --exe "
00118 + filename
00119 + " "
00120 + myAddress,
00121 1
00122 );
00123
00124 if (line == "")
00125 {
00126 os << " addr2line failed";
00127 }
00128 else if (line == "??:0")
00129 {
00130 os << " in " << filename;
00131 }
00132 else
00133 {
00134 string cwdLine(line.replaceAll(cwd() + '/', ""));
00135
00136 string homeLine(cwdLine.replaceAll(home(), '~'));
00137
00138 os << " at " << homeLine.c_str();
00139 }
00140 }
00141 }
00142
00143
00144 void getSymbolForRaw
00145 (
00146 Ostream& os,
00147 const string& raw,
00148 const fileName& filename,
00149 const word& address
00150 )
00151 {
00152 if (filename.size() && filename[0] == '/')
00153 {
00154 string fcnt = pOpen
00155 (
00156 "addr2line -f --demangle=auto --exe "
00157 + filename
00158 + " "
00159 + address
00160 );
00161
00162 if (fcnt != "")
00163 {
00164 os << fcnt.c_str();
00165 return;
00166 }
00167 }
00168 os << "Uninterpreted: " << raw.c_str();
00169 }
00170
00171
00172 void error::printStack(Ostream& os)
00173 {
00174
00175
00176
00177
00178
00179
00180 HashTable<label, fileName> addressMap;
00181 {
00182 IFstream is("/proc/" + name(pid()) + "/maps");
00183
00184 while(is.good())
00185 {
00186 string line;
00187 is.getLine(line);
00188
00189 string::size_type space = line.rfind(' ') + 1;
00190 fileName libPath = line.substr(space, line.size()-space);
00191
00192 if (libPath.size() && libPath[0] == '/')
00193 {
00194 string offsetString(line.substr(0, line.find('-')));
00195 IStringStream offsetStr(offsetString);
00196 addressMap.insert(libPath, readHexLabel(offsetStr));
00197 }
00198 }
00199 }
00200
00201
00202 void *array[100];
00203 size_t size = backtrace(array, 100);
00204 char **strings = backtrace_symbols(array, size);
00205
00206
00207
00208 for (size_t i = 0; i < size; i++)
00209 {
00210 string msg(strings[i]);
00211 fileName programFile;
00212 word address;
00213
00214 os << '#' << label(i) << " ";
00215
00216 {
00217 string::size_type lPos = msg.find('[');
00218 string::size_type rPos = msg.find(']');
00219
00220 if (lPos != string::npos && rPos != string::npos && lPos < rPos)
00221 {
00222 address = msg.substr(lPos+1, rPos-lPos-1);
00223 msg = msg.substr(0, lPos);
00224 }
00225
00226 string::size_type bracketPos = msg.find('(');
00227 string::size_type spacePos = msg.find(' ');
00228 if (bracketPos != string::npos || spacePos != string::npos)
00229 {
00230 programFile = msg.substr(0, min(spacePos, bracketPos));
00231
00232
00233 if (programFile[0] != '/')
00234 {
00235 string tmp = pOpen("which " + programFile);
00236 if (tmp[0] == '/' || tmp[0] == '~')
00237 {
00238 programFile = tmp;
00239 }
00240 }
00241 }
00242 }
00243
00244 string::size_type bracketPos = msg.find('(');
00245
00246 if (bracketPos != string::npos)
00247 {
00248 string::size_type start = bracketPos+1;
00249
00250 string::size_type plusPos = msg.find('+', start);
00251
00252 if (plusPos != string::npos)
00253 {
00254 string cName(msg.substr(start, plusPos-start));
00255
00256 int status;
00257 char* cplusNamePtr = abi::__cxa_demangle
00258 (
00259 cName.c_str(),
00260 NULL,
00261 0,
00262 &status
00263 );
00264
00265 if (status == 0 && cplusNamePtr)
00266 {
00267 os << cplusNamePtr;
00268 free(cplusNamePtr);
00269 }
00270 else
00271 {
00272 os << cName.c_str();
00273 }
00274 }
00275 else
00276 {
00277 string::size_type endBracketPos = msg.find(')', start);
00278
00279 if (endBracketPos != string::npos)
00280 {
00281 string fullName(msg.substr(start, endBracketPos-start));
00282
00283 os << fullName.c_str() << nl;
00284 }
00285 else
00286 {
00287
00288 getSymbolForRaw(os, msg, programFile, address);
00289 }
00290 }
00291 }
00292 else
00293 {
00294
00295 getSymbolForRaw(os, msg, programFile, address);
00296 }
00297
00298 printSourceFileAndLine(os, addressMap, programFile, address);
00299
00300 os << nl;
00301 }
00302
00303 free(strings);
00304 }
00305
00306
00307
00308
00309 }
00310
00311
Copyright © 2000-2009 OpenCFD Ltd