CPN
Computational Process Networks
Macros | Functions
StackTrace.h File Reference

function for printing out a stack trace Note that we need to pass -rdynamic to the linker to be able to see the function names with these functios. More...

#include <cpn/common.h>
#include <string>
+ Include dependency graph for StackTrace.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define STACKTRACE_MAXTRACE   100
 

Functions

void PrintStack ()
 
std::string GetStack (unsigned ignore=0)
 

Detailed Description

function for printing out a stack trace Note that we need to pass -rdynamic to the linker to be able to see the function names with these functios.

Author
John Bridgman

Definition in file StackTrace.h.

Macro Definition Documentation

#define STACKTRACE_MAXTRACE   100

Definition at line 33 of file StackTrace.h.

Referenced by GetStack(), and PrintStack().

Function Documentation

std::string GetStack ( unsigned  ignore = 0)

Returns a std::string to the stack trace. May fail if out of memory.

Parameters
ignorethe number of top stacks to ignore. This is useful because this function is often used in error messages and it is useless garbage to report the stack from the error handler.

Definition at line 39 of file StackTrace.cc.

References STACKTRACE_MAXTRACE.

Referenced by __ASSERT().

39  {
40  void* stack[STACKTRACE_MAXTRACE];
41  size_t size = backtrace(stack, STACKTRACE_MAXTRACE);
42  char **bt = backtrace_symbols(stack, size);
43  if (!bt) {
44  perror("backtrace_symbols");
45  return "";
46  }
47  std::ostringstream oss;
48  for (unsigned i = ignore; i < size; ++i) {
49  oss << bt[i] << '\n';
50  }
51  free(bt);
52  return oss.str();
53 }
#define STACKTRACE_MAXTRACE
Definition: StackTrace.h:33

+ Here is the caller graph for this function:

void PrintStack ( )

Prints the stack trace to stderr Should work even if we are out of memory

Definition at line 33 of file StackTrace.cc.

References STACKTRACE_MAXTRACE.

33  {
34  void* stack[STACKTRACE_MAXTRACE];
35  size_t size = backtrace(stack, STACKTRACE_MAXTRACE);
36  backtrace_symbols_fd(stack, size, 2);
37 }
#define STACKTRACE_MAXTRACE
Definition: StackTrace.h:33