OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

printStack.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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         // Read line number of lines
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 // use popen to call addr2line (using bfd.h directly would have
00078 // meant relinking everything)
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         // Convert offset into .so into offset into executable.
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         // Reconstruct hex word from address
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     // Reads the starting addresses for the dynamically linked libraries
00175     // from the /proc/pid/maps-file
00176     // I'm afraid this works only for Linux 2.6-Kernels (may work on 2.4)
00177     // Note2: the filenames in here will have softlinks resolved so will
00178     // go wrong when having e.g. OpenFOAM installed under a softlink.
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     // Get raw stack symbols
00202     void *array[100];
00203     size_t size = backtrace(array, 100);
00204     char **strings = backtrace_symbols(array, size);
00205 
00206     // See if they contain function between () e.g. "(__libc_start_main+0xd0)"
00207     // and see if cplus_demangle can make sense of part before +
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         //os << "Raw   : " << msg << "\n\t";
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                 // not an absolute path
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,                   // have it malloc itself
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                     // Print raw message
00288                     getSymbolForRaw(os, msg, programFile, address);
00289                 }
00290             }
00291         }
00292         else
00293         {
00294             // Print raw message
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 } // End namespace Foam
00310 
00311 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd