libcsdbg  1.28
C++ exception (and generic) stack trace debug library
string & csdbg::tracer::addr2line ( string dst,
const i8 path,
mem_addr_t  addr 
)
staticprotected

Given an address in an objective code file, extract from the gdb-related debug information, the equivalent source code file name and line and append it to a string buffer.

Parameters
[in,out]dstthe destination string
[in]paththe path of the objective code file
[in]addrthe address
Returns
the first argument
Note
If the addr2line program fails to retreive the debug information, or if any other error or exception occurs, nothing is appended to the destination string
See Also
man addr2line
man g++ (-g family options)

Definition at line 398 of file tracer.cpp.

References __D_ASSERT, csdbg::string::append(), csdbg::string::cmp(), csdbg::string::cstr(), csdbg::util::dbg_error(), likely, csdbg::exception::msg(), and unlikely.

Referenced by trace().

399 {
400  __D_ASSERT(path != NULL);
401  if ( unlikely(path == NULL) )
402  return dst;
403 
404  FILE *pipe = NULL;
405  try {
406  /* Command to be executed by the child process */
407  string cmd("addr2line -se %s 0x%x", path, addr);
408 
409  /* Open a readonly pipe to the child process */
410  pipe = popen(cmd.cstr(), "r");
411  if ( unlikely(pipe == NULL) )
412  throw exception(
413  "failed to open pipe for command '%s' (errno %d - %s)",
414  cmd.cstr(),
415  errno,
416  strerror(errno)
417  );
418 
419  /* Read a line of output from the pipe to a buffer */
420  string buf;
421  i8 ch = fgetc(pipe);
422  while ( likely(ch != '\n' && ch != EOF) ) {
423  buf.append(ch);
424  ch = fgetc(pipe);
425  }
426 
427  if ( unlikely(ferror(pipe) != 0) )
428  throw exception("failed to read pipe for command '%s'", cmd.cstr());
429 
430  if ( likely(buf.cmp("??:0") != 0) )
431  dst.append(" (%s)", buf.cstr());
432  }
433 
434  catch (exception &x) {
435  util::dbg_error("in tracer::%s(): %s", __FUNCTION__, x.msg());
436  }
437 
438  catch (std::exception &x) {
439  util::dbg_error("in tracer::%s(): %s", __FUNCTION__, x.what());
440  }
441 
442  if ( likely(pipe != NULL) )
443  pclose(pipe);
444 
445  return dst;
446 }
char i8
8-bit signed integer
Definition: config.hpp:72
#define likely(expr)
Offer a hint (positive) to the pipeline branch predictor.
Definition: config.hpp:344
static void dbg_error(const i8 *,...)
Print an error debug message on the standard error stream.
Definition: util.cpp:726
#define unlikely(expr)
Offer a hint (negative) to the pipeline branch predictor.
Definition: config.hpp:349
#define __D_ASSERT(x)
Assertion macro.
Definition: config.hpp:268

+ Here is the call graph for this function:

+ Here is the caller graph for this function: