Since we introduced the API to set signal handler and print a stacktrace, we should avoid glibc's backtrace, which may call malloc. Basically, we choose the way to produce a stacktrace as same as perftools. Also, I removed GetStackFrames, which is not used and not implemented with glibc. git-svn-id: https://google-glog.googlecode.com/svn/trunk@16 eb4d4688-79bd-11dd-afb4-1d65580434c0
34 lines
907 B
C
34 lines
907 B
C
// Copyright 2000 - 2007 Google Inc.
|
|
// All rights reserved.
|
|
//
|
|
// Portable implementation - just use glibc
|
|
//
|
|
// Note: The glibc implementation may cause a call to malloc.
|
|
// This can cause a deadlock in HeapProfiler.
|
|
#include <execinfo.h>
|
|
#include <string.h>
|
|
#include "stacktrace.h"
|
|
|
|
_START_GOOGLE_NAMESPACE_
|
|
|
|
// If you change this function, also change GetStackFrames below.
|
|
int GetStackTrace(void** result, int max_depth, int skip_count) {
|
|
static const int kStackLength = 64;
|
|
void * stack[kStackLength];
|
|
int size;
|
|
|
|
size = backtrace(stack, kStackLength);
|
|
skip_count++; // we want to skip the current frame as well
|
|
int result_count = size - skip_count;
|
|
if (result_count < 0)
|
|
result_count = 0;
|
|
if (result_count > max_depth)
|
|
result_count = max_depth;
|
|
for (int i = 0; i < result_count; i++)
|
|
result[i] = stack[i + skip_count];
|
|
|
|
return result_count;
|
|
}
|
|
|
|
_END_GOOGLE_NAMESPACE_
|