CPN
Computational Process Networks
Functions
StackTrace.cc File Reference

functino for printing out a stack trace lookup the backtrace etc. in the man pages. More...

#include "common_priv.h"
#include <cpn/utils/StackTrace.h>
#include <execinfo.h>
#include <cstdlib>
#include <cstdio>
#include <sstream>
+ Include dependency graph for StackTrace.cc:

Go to the source code of this file.

Functions

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

Detailed Description

functino for printing out a stack trace lookup the backtrace etc. in the man pages.

Author
John Bridgman

Definition in file StackTrace.cc.

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