glog 0.1
[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 // If you change this function, also change GetStackTrace above:
34 //
35 // This GetStackFrames routine shares a lot of code with GetStackTrace
36 // above. This code could have been refactored into a common routine,
37 // and then both GetStackTrace/GetStackFrames could call that routine.
38 // There are two problems with that:
39 //
40 // (1) The performance of the refactored-code suffers substantially - the
41 //     refactored needs to be able to record the stack trace when called
42 //     from GetStackTrace, and both the stack trace and stack frame sizes,
43 //     when called from GetStackFrames - this introduces enough new
44 //     conditionals that GetStackTrace performance can degrade by as much
45 //     as 50%.
46 //
47 // (2) Whether the refactored routine gets inlined into GetStackTrace and
48 //     GetStackFrames depends on the compiler, and we can't guarantee the
49 //     behavior either-way, even with "__attribute__ ((always_inline))"
50 //     or "__attribute__ ((noinline))". But we need this guarantee or the
51 //     frame counts may be off by one.
52 //
53 // Both (1) and (2) can be addressed without this code duplication, by
54 // clever use of template functions, and by defining GetStackTrace and
55 // GetStackFrames as macros that expand to these template functions.
56 // However, this approach comes with its own set of problems - namely,
57 // macros and  preprocessor trouble - for example,  if GetStackTrace
58 // and/or GetStackFrames is ever defined as a member functions in some
59 // class, we are in trouble.
60 int GetStackFrames(void** pcs, int* sizes, int max_depth, int skip_count) {
61   static const int kStackLength = 64;
62   void * stack[kStackLength];
63   int size;
64
65   size = backtrace(stack, kStackLength);
66   skip_count++;  // we want to skip the current frame as well
67   int result_count = size - skip_count;
68   if (result_count < 0)
69     result_count = 0;
70   if (result_count > max_depth)
71     result_count = max_depth;
72   for (int i = 0; i < result_count; i++)
73     pcs[i] = stack[i + skip_count];
74
75   // No implementation for finding out the stack frame sizes yet.
76   memset(sizes, 0, sizeof(*sizes) * result_count);
77
78   return result_count;
79 }
80
81 _END_GOOGLE_NAMESPACE_