a4b240dbf7f69dc7e4088a8214b9a35bbf633b0b
[platform/upstream/glog.git] / src / stacktrace_generic-inl.h
1 // Copyright 2000 - 2007 Google Inc.
2 // All rights reserved.
3 //
4 // Portable implementation - just use glibc
5 //
6 // Note:  The glibc implementation may cause a call to malloc.
7 // This can cause a deadlock in HeapProfiler.
8 #include <execinfo.h>
9 #include <string.h>
10 #include "stacktrace.h"
11
12 _START_GOOGLE_NAMESPACE_
13
14 // If you change this function, also change GetStackFrames below.
15 int GetStackTrace(void** result, int max_depth, int skip_count) {
16   static const int kStackLength = 64;
17   void * stack[kStackLength];
18   int size;
19
20   size = backtrace(stack, kStackLength);
21   skip_count++;  // we want to skip the current frame as well
22   int result_count = size - skip_count;
23   if (result_count < 0)
24     result_count = 0;
25   if (result_count > max_depth)
26     result_count = max_depth;
27   for (int i = 0; i < result_count; i++)
28     result[i] = stack[i + skip_count];
29
30   return result_count;
31 }
32
33 _END_GOOGLE_NAMESPACE_