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(new_size: 0);
23}
24
25void VarSizeStackTrace::ResizeBuffer(uptr new_size) {
26 Free(p&: trace_buffer);
27 trace_buffer = (new_size > 0)
28 ? (uptr *)Alloc(sz: new_size * sizeof(trace_buffer[0]))
29 : nullptr;
30 trace = trace_buffer;
31 size = new_size;
32}
33
34void VarSizeStackTrace::Init(const uptr *pcs, uptr cnt, uptr extra_top_pc) {
35 ResizeBuffer(new_size: cnt + !!extra_top_pc);
36 internal_memcpy(dest: trace_buffer, src: pcs, n: cnt * sizeof(trace_buffer[0]));
37 if (extra_top_pc)
38 trace_buffer[cnt] = extra_top_pc;
39}
40
41void VarSizeStackTrace::ReverseOrder() {
42 for (u32 i = 0; i < (size >> 1); i++)
43 Swap(a&: trace_buffer[i], b&: trace_buffer[size - 1 - i]);
44}
45
46} // namespace __tsan
47
48#if !SANITIZER_GO
49void __sanitizer::BufferedStackTrace::UnwindImpl(
50 uptr pc, uptr bp, void *context, bool request_fast, u32 max_depth) {
51 uptr top = 0;
52 uptr bottom = 0;
53 GetThreadStackTopAndBottom(at_initialization: false, stack_top: &top, stack_bottom: &bottom);
54 bool fast = StackTrace::WillUseFastUnwind(request_fast_unwind: request_fast);
55 Unwind(max_depth, pc, bp, context, stack_top: top, stack_bottom: bottom, request_fast_unwind: fast);
56}
57#endif // SANITIZER_GO
58

source code of compiler-rt/lib/tsan/rtl/tsan_stack_trace.cpp