libcsdbg  1.28
C++ exception (and generic) stack trace debug library
tracer & csdbg::tracer::trace ( string dst,
pthread_t  id 
) const
virtual

Create the stack trace of a thread indexed by its ID and append it to a string.

Parameters
[in]dstthe destination string
[in]idthe thread ID
Returns
*this
Exceptions
std::bad_alloc
csdbg::exception

Definition at line 716 of file tracer.cpp.

References csdbg::symbol::addr(), addr2line(), csdbg::string::append(), csdbg::thread::backtrace(), csdbg::thread::call_depth(), csdbg::process::get_thread(), csdbg::thread::handle(), csdbg::process::ilookup(), likely, csdbg::util::lock(), m_proc, csdbg::symbol::name(), csdbg::thread::name(), csdbg::call::site(), unlikely, and csdbg::util::unlock().

717 {
718  /* If an exception occurs, unlock and rethrow it */
719  try {
720  util::lock();
721  thread *thr = m_proc->get_thread(id);
722  if ( unlikely(thr == NULL) ) {
723  util::unlock();
724  return const_cast<tracer&> (*this);
725  }
726 
727  const i8 *nm = thr->name();
728  if ( likely(nm == NULL) )
729  nm = "anonymous";
730  dst.append("at %s thread (0x%lx) {\r\n", nm, thr->handle());
731 
732  /* For each function call */
733  for (i32 i = thr->call_depth() - 1; likely(i >= 0); i--) {
734  const call *cur = thr->backtrace(i);
735  dst.append(" at %s", cur->name());
736 
737  /* Append addr2line debug information */
738  u32 prev = i + 1;
739  if ( likely (prev < thr->call_depth()) ) {
740  const call *caller = thr->backtrace(prev);
741  mem_addr_t base = 0;
742 
743  const i8 *path = m_proc->ilookup(caller->addr(), base);
744  addr2line(dst, path, cur->site() - base);
745  }
746 
747  dst.append("\r\n");
748  }
749 
750  dst.append("}\r\n");
751  util::unlock();
752  return const_cast<tracer&> (*this);
753  }
754 
755  catch (...) {
756  util::unlock();
757  throw;
758  }
759 }
virtual string & append(const string &)
Append a string.
Definition: string.cpp:404
char i8
8-bit signed integer
Definition: config.hpp:72
virtual thread * get_thread(pthread_t) const
Get a thread by ID.
Definition: process.cpp:376
process * m_proc
Process handle.
Definition: tracer.hpp:45
#define likely(expr)
Offer a hint (positive) to the pipeline branch predictor.
Definition: config.hpp:344
static void lock()
Lock the global access mutex.
Definition: util.cpp:397
static void unlock()
Unlock the global access mutex.
Definition: util.cpp:406
tracer()
Object default constructor.
Definition: tracer.cpp:454
unsigned int u32
32-bit unsigned integer
Definition: config.hpp:102
static string & addr2line(string &, const i8 *, mem_addr_t)
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.
Definition: tracer.cpp:398
int i32
32-bit signed integer
Definition: config.hpp:82
virtual const i8 * ilookup(mem_addr_t, mem_addr_t &) const
Inverse lookup. Find the module (executable or DSO library) that defines a symbol and return its path...
Definition: process.cpp:300
unsigned long long mem_addr_t
64-bit memory address
Definition: config.hpp:120
#define unlikely(expr)
Offer a hint (negative) to the pipeline branch predictor.
Definition: config.hpp:349

+ Here is the call graph for this function: