blob: 0b20d238636febaec94eb9bbe96bb9cedeb48dc0 [file] [log] [blame]
Austin Schuh906616c2019-01-21 20:25:11 -08001// Copyright (c) 2005 - 2007, 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// Author: Arun Sharma
31//
32// Produce stack trace using libunwind
33
34#include "utilities.h"
35
36extern "C" {
37#define UNW_LOCAL_ONLY
38#include <libunwind.h>
39}
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070040#include <glog/raw_logging.h>
Austin Schuh906616c2019-01-21 20:25:11 -080041#include "stacktrace.h"
42
43_START_GOOGLE_NAMESPACE_
44
45// Sometimes, we can try to get a stack trace from within a stack
46// trace, because libunwind can call mmap (maybe indirectly via an
47// internal mmap based memory allocator), and that mmap gets trapped
48// and causes a stack-trace request. If were to try to honor that
49// recursive request, we'd end up with infinite recursion or deadlock.
50// Luckily, it's safe to ignore those subsequent traces. In such
51// cases, we return 0 to indicate the situation.
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070052// We can use the GCC __thread syntax here since libunwind is not supported on
53// Windows.
54static __thread bool g_tl_entered; // Initialized to false.
Austin Schuh906616c2019-01-21 20:25:11 -080055
56// If you change this function, also change GetStackFrames below.
57int GetStackTrace(void** result, int max_depth, int skip_count) {
58 void *ip;
59 int n = 0;
60 unw_cursor_t cursor;
61 unw_context_t uc;
62
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070063 if (g_tl_entered) {
Austin Schuh906616c2019-01-21 20:25:11 -080064 return 0;
65 }
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070066 g_tl_entered = true;
Austin Schuh906616c2019-01-21 20:25:11 -080067
68 unw_getcontext(&uc);
69 RAW_CHECK(unw_init_local(&cursor, &uc) >= 0, "unw_init_local failed");
70 skip_count++; // Do not include the "GetStackTrace" frame
71
72 while (n < max_depth) {
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070073 int ret =
74 unw_get_reg(&cursor, UNW_REG_IP, reinterpret_cast<unw_word_t *>(&ip));
75 if (ret < 0) {
Austin Schuh906616c2019-01-21 20:25:11 -080076 break;
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070077 }
Austin Schuh906616c2019-01-21 20:25:11 -080078 if (skip_count > 0) {
79 skip_count--;
80 } else {
81 result[n++] = ip;
82 }
83 ret = unw_step(&cursor);
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070084 if (ret <= 0) {
Austin Schuh906616c2019-01-21 20:25:11 -080085 break;
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070086 }
Austin Schuh906616c2019-01-21 20:25:11 -080087 }
88
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070089 g_tl_entered = false;
Austin Schuh906616c2019-01-21 20:25:11 -080090 return n;
91}
92
93_END_GOOGLE_NAMESPACE_