1// Copyright (c) 2005, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8//     * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10//     * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14//     * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30// ---
31// Author: Arun Sharma
32//
33// Produce stack trace using libunwind
34
35#ifndef BASE_STACKTRACE_LIBINWIND_INL_H_
36#define BASE_STACKTRACE_LIBINWIND_INL_H_
37// Note: this file is included into stacktrace.cc more than once.
38// Anything that should only be defined once should be here:
39
40// We only need local unwinder.
41#define UNW_LOCAL_ONLY
42
43extern "C" {
44#include <assert.h>
45#include <string.h>   // for memset()
46#include <libunwind.h>
47}
48#include "gperftools/stacktrace.h"
49#include "base/logging.h"
50
51// Sometimes, we can try to get a stack trace from within a stack
52// trace, because libunwind can call mmap (maybe indirectly via an
53// internal mmap based memory allocator), and that mmap gets trapped
54// and causes a stack-trace request.  If were to try to honor that
55// recursive request, we'd end up with infinite recursion or deadlock.
56// Luckily, it's safe to ignore those subsequent traces.  In such
57// cases, we return 0 to indicate the situation.
58static __thread int recursive;
59
60#endif  // BASE_STACKTRACE_LIBINWIND_INL_H_
61
62// Note: this part of the file is included several times.
63// Do not put globals below.
64
65// The following 4 functions are generated from the code below:
66//   GetStack{Trace,Frames}()
67//   GetStack{Trace,Frames}WithContext()
68//
69// These functions take the following args:
70//   void** result: the stack-trace, as an array
71//   int* sizes: the size of each stack frame, as an array
72//               (GetStackFrames* only)
73//   int max_depth: the size of the result (and sizes) array(s)
74//   int skip_count: how many stack pointers to skip before storing in result
75//   void* ucp: a ucontext_t* (GetStack{Trace,Frames}WithContext only)
76int GET_STACK_TRACE_OR_FRAMES {
77  void *ip;
78  int n = 0;
79  unw_cursor_t cursor;
80  unw_context_t uc;
81#if IS_STACK_FRAMES
82  unw_word_t sp = 0, next_sp = 0;
83#endif
84
85  if (recursive) {
86    return 0;
87  }
88  ++recursive;
89
90  unw_getcontext(&uc);
91  int ret = unw_init_local(&cursor, &uc);
92  assert(ret >= 0);
93  skip_count++;         // Do not include current frame
94
95  while (skip_count--) {
96    if (unw_step(&cursor) <= 0) {
97      goto out;
98    }
99#if IS_STACK_FRAMES
100    if (unw_get_reg(&cursor, UNW_REG_SP, &next_sp)) {
101      goto out;
102    }
103#endif
104  }
105
106  while (n < max_depth) {
107    if (unw_get_reg(&cursor, UNW_REG_IP, (unw_word_t *) &ip) < 0) {
108      break;
109    }
110#if IS_STACK_FRAMES
111    sizes[n] = 0;
112#endif
113    result[n++] = ip;
114    if (unw_step(&cursor) <= 0) {
115      break;
116    }
117#if IS_STACK_FRAMES
118    sp = next_sp;
119    if (unw_get_reg(&cursor, UNW_REG_SP, &next_sp) , 0) {
120      break;
121    }
122    sizes[n - 1] = next_sp - sp;
123#endif
124  }
125out:
126  --recursive;
127  return n;
128}
129