tsan_stack_trace.cpp revision 360784
1//===-- tsan_stack_trace.cpp ----------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file is a part of ThreadSanitizer (TSan), a race detector.
10//
11//===----------------------------------------------------------------------===//
12#include "tsan_stack_trace.h"
13#include "tsan_rtl.h"
14#include "tsan_mman.h"
15
16namespace __tsan {
17
18VarSizeStackTrace::VarSizeStackTrace()
19    : StackTrace(nullptr, 0), trace_buffer(nullptr) {}
20
21VarSizeStackTrace::~VarSizeStackTrace() {
22  ResizeBuffer(0);
23}
24
25void VarSizeStackTrace::ResizeBuffer(uptr new_size) {
26  if (trace_buffer) {
27    internal_free(trace_buffer);
28  }
29  trace_buffer =
30      (new_size > 0)
31          ? (uptr *)internal_alloc(MBlockStackTrace,
32                                   new_size * sizeof(trace_buffer[0]))
33          : nullptr;
34  trace = trace_buffer;
35  size = new_size;
36}
37
38void VarSizeStackTrace::Init(const uptr *pcs, uptr cnt, uptr extra_top_pc) {
39  ResizeBuffer(cnt + !!extra_top_pc);
40  internal_memcpy(trace_buffer, pcs, cnt * sizeof(trace_buffer[0]));
41  if (extra_top_pc)
42    trace_buffer[cnt] = extra_top_pc;
43}
44
45void VarSizeStackTrace::ReverseOrder() {
46  for (u32 i = 0; i < (size >> 1); i++)
47    Swap(trace_buffer[i], trace_buffer[size - 1 - i]);
48}
49
50}  // namespace __tsan
51
52#if !SANITIZER_GO
53void __sanitizer::BufferedStackTrace::UnwindImpl(
54    uptr pc, uptr bp, void *context, bool request_fast, u32 max_depth) {
55  uptr top = 0;
56  uptr bottom = 0;
57  if (StackTrace::WillUseFastUnwind(request_fast)) {
58    GetThreadStackTopAndBottom(false, &top, &bottom);
59    Unwind(max_depth, pc, bp, nullptr, top, bottom, true);
60  } else
61    Unwind(max_depth, pc, 0, context, 0, 0, false);
62}
63#endif  // SANITIZER_GO
64