-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathstacktrace-backtrace.c
More file actions
159 lines (122 loc) · 4.66 KB
/
Copy pathstacktrace-backtrace.c
File metadata and controls
159 lines (122 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// SPDX-License-Identifier: GPL-3.0-or-later
#include "stacktrace-common.h"
#if defined(USE_BACKTRACE)
#include <execinfo.h>
const char *stacktrace_backend(void) {
return "backtrace";
}
bool stacktrace_available(void) {
return true;
}
void impl_stacktrace_init(void) {
// Nothing to initialize for backtrace backend
}
void stacktrace_flush(void) {
// Nothing to flush
}
bool stacktrace_capture_is_async_signal_safe(void) {
return false;
}
NEVER_INLINE
void stacktrace_capture(BUFFER *wb) {
void *array[50];
char **messages;
int size, i;
root_cause_function[0] = '\0';
size = backtrace(array, _countof(array));
messages = backtrace_symbols(array, size);
if (!messages) {
buffer_strcat(wb, NO_STACK_TRACE_PREFIX "backtrace() reports no symbols");
return;
}
size_t added = 0;
bool found_signal_handler = false;
// Format the stack trace (removing the address part)
// Skip the first frame (stacktrace_capture itself)
for (i = 1; i < size; i++) {
if(messages[i] && *messages[i]) {
// Check if we found the signal handler frame
if (!found_signal_handler && stacktrace_contains_signal_handler_function(messages[i])) {
// We found the signal handler, reset the buffer
buffer_flush(wb);
added = 0;
found_signal_handler = true;
continue; // Skip adding the signal handler itself
}
// Check for logging functions, but only if we haven't found a signal handler yet
if (!found_signal_handler && stacktrace_contains_logging_function(messages[i])) {
// Found a logging function, reset the buffer
buffer_flush(wb);
added = 0;
// continue to add the function to the stack trace
}
if(added)
buffer_putc(wb, '\n');
buffer_putc(wb, '#');
buffer_print_uint64(wb, added);
buffer_putc(wb, ' ');
buffer_strcat(wb, messages[i]);
added++;
}
}
if(!added)
buffer_strcat(wb, NO_STACK_TRACE_PREFIX "backtrace() reports no frames");
free(messages);
}
// Implementation-specific function to collect stack trace frames
int impl_stacktrace_get_frames(void **frames, int max_frames, int skip_frames) {
if (!frames || max_frames <= 0)
return 0;
// Add 1 to skip_frames to also skip this function itself
skip_frames += 1;
// Collect all stack frames without skipping at this level
void *array[100 + 50]; // Use a larger array to account for skipped frames (100) plus the max we need (50)
int size = backtrace(array, _countof(array));
if (size <= skip_frames) // Not enough frames after skipping
return 0;
// Apply skip_frames here, in the aftermath of backtrace, not during capture
// This ensures we're not skipping inlined functions
int available_frames = size - skip_frames;
int frames_to_copy = available_frames < max_frames ? available_frames : max_frames;
// Copy the frames, skipping the requested number of frames
memcpy(frames, array + skip_frames, frames_to_copy * sizeof(void *));
return frames_to_copy;
}
// Implementation-specific function to convert a stacktrace to a buffer
void impl_stacktrace_to_buffer(STACKTRACE trace, BUFFER *wb) {
struct stacktrace *st = (struct stacktrace *)trace;
// Convert to text representation
char **messages = backtrace_symbols(st->frames, st->frame_count);
if (!messages) {
buffer_strcat(wb, NO_STACK_TRACE_PREFIX "backtrace_symbols() failed");
return;
}
bool found_signal_handler = false;
int added = 0;
for (int i = 0; i < st->frame_count; i++) {
if (!messages[i] || !*messages[i])
continue;
// Handle filtering
if (!found_signal_handler && stacktrace_contains_signal_handler_function(messages[i])) {
buffer_flush(wb);
added = 0;
found_signal_handler = true;
continue;
}
if (!found_signal_handler && stacktrace_contains_logging_function(messages[i])) {
buffer_flush(wb);
added = 0;
}
if (added > 0)
buffer_putc(wb, '\n');
buffer_putc(wb, '#');
buffer_print_uint64(wb, added);
buffer_putc(wb, ' ');
buffer_strcat(wb, messages[i]);
added++;
}
free(messages);
if (added == 0)
buffer_strcat(wb, NO_STACK_TRACE_PREFIX "no valid frames");
}
#endif // USE_BACKTRACE