45 std::string pipeOpen(
const std::string& cmd,
const int lineNum = 0)
49 FILE *handle = popen(cmd.c_str(),
"r");
50 if (!handle)
return str;
60 cnt <= lineNum && (nread = ::getline(&buf, &len, handle)) >= 0;
71 str.resize(str.size()-1);
83 inline std::string addressToWord(
const uintptr_t addr)
85 std::ostringstream buf;
91 buf << uint64_t(addr);
101 inline std::string demangleSymbol(
const char* sn)
105 char* cxx_sname = abi::__cxa_demangle(sn,
nullptr,
nullptr, &st);
107 if (st == 0 && cxx_sname)
109 std::string demangled(cxx_sname);
127 void printSourceFileAndLine
135 uintptr_t address = uintptr_t(addr);
136 std::string myAddress = addressToWord(address);
147 uintptr_t offset = uintptr_t(info.dli_fbase);
148 intptr_t relativeAddress = address - offset;
149 myAddress = addressToWord(relativeAddress);
152 if (filename[0] ==
'/')
156 "addr2line -f --demangle=auto --exe " 165 os <<
" addr2line failed";
167 else if (line ==
"??:0" || line ==
"??:?" )
170 os <<
" in " << shorterPath(line).c_str();
174 os <<
" at " << shorterPath(line).c_str();
186 if (!fname.empty() && fname[0] !=
'/' && fname[0] !=
'~')
188 std::string
s = pipeOpen(
"which " + fname);
190 if (
s[0] ==
'/' ||
s[0] ==
'~')
207 void *callstack[100];
208 size = backtrace(callstack, (size > 0 && size < 100) ? size + 1 : 100);
210 char **strings = backtrace_symbols(callstack, size);
217 for (
int i = 1; i < size; ++i)
219 std::string str(strings[i]);
221 os <<
'#' << i <<
'\t';
229 auto ldelim = str.find(
'(');
230 auto beg = str.find(
"/platforms/");
232 if (beg == std::string::npos || !beg || beg > ldelim)
243 (ldelim != std::string::npos)
244 && (rdelim = str.find(
'+', ldelim+1)) != std::string::npos
245 && (rdelim > ldelim+1)
252 os << str.substr(beg, ldelim-beg)
256 str.substr(ldelim+1, rdelim-ldelim-1).c_str()
259 if ((rdelim = str.find(
'[', rdelim)) != std::string::npos)
261 os <<
' ' << str.substr(rdelim);
267 os << str.substr(beg);
286 void *callstack[100];
287 size = backtrace(callstack, (size > 0 && size < 100) ? size + 1 : 100);
293 os <<
"[stack trace]" <<
nl 294 <<
"=============" <<
nl;
296 for (
int i = 1; i < size; ++i)
298 int st = dladdr(callstack[i], &info);
300 os <<
'#' << i <<
" ";
301 if (st != 0 && info.dli_fname !=
nullptr && *(info.dli_fname))
303 fname = whichPath(info.dli_fname);
307 os << demangleSymbol(info.dli_sname).c_str();
320 printSourceFileAndLine(
os, fname, info, callstack[i]);
324 os <<
"=============" <<
nl;
A class for handling file names.
IOstream & hex(IOstream &io)
constexpr char nl
The newline '\n' character (0x0a)
Ostream & endl(Ostream &os)
Add newline and flush stream.
fileName home()
Return home directory path name for the current user.
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
static void safePrintStack(std::ostream &os, int size=-1)
Helper function to print a stack, with optional upper limit. Used when OpenFOAM IO not yet initialise...
An Ostream is an abstract base class for all output systems (streams, files, token lists...
OBJstream os(runTime.globalPath()/outputName)
fileName cwd()
The physical or logical current working directory path name.
bool has_ext() const
Various checks for extensions.
static void printStack(Ostream &os, int size=-1)
Helper function to print a stack, with optional upper limit.
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
A class for handling character strings derived from std::string.