Debian Codegraph @

src/track/trace_unwind_tables.cpp

raw symbols SCIP
1 /*
2 SPDX-FileCopyrightText: 2019 Volodymyr Nikolaichuk <[email protected]>
3
4 SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6
7 /**
8 * @brief A unwind-tables based backtrace.
9 */
10
11 #include "trace.h"
12
13 #include <cstdint>
14 #include <cstdio>
15 #include <unwind.h>
16
17 namespace {
18
19 struct backtrace
20 {
21 void** data = nullptr;
22 int ctr = 0;
23 int max_size = 0;
24 };
25
26 _Unwind_Reason_Code unwind_backtrace_callback(struct _Unwind_Context* context, void* arg)
27 {
28 backtrace* trace = static_cast<backtrace*>(arg);
29
30 uintptr_t pc = _Unwind_GetIP(context);
31 if (pc && trace->ctr < trace->max_size - 1) {
32 trace->data[trace->ctr++] = (void*)(pc);
33 }
34
35 return _URC_NO_REASON;
36 }
37
38 }
39
40 void Trace::setup()
41 {
42 }
43
44 void Trace::print()
45 {
46 Trace trace;
47 trace.fill(1);
48 for (auto ip : trace) {
49 fprintf(stderr, "%p\n", ip);
50 }
51 }
52
53 int Trace::unwind(void** data)
54 {
55 backtrace trace;
56 trace.data = data;
57 trace.max_size = MAX_SIZE;
58
59 _Unwind_Backtrace(unwind_backtrace_callback, &trace);
60 return trace.ctr;
61 }