Skip to content

Commit 8198997

Browse files
committed
Handle no frames returned.
1 parent 0c84f8a commit 8198997

1 file changed

Lines changed: 29 additions & 24 deletions

File tree

Modules/faulthandler.c

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -220,39 +220,44 @@ faulthandler_dump_traceback(int fd, int all_threads,
220220
static void
221221
faulthandler_stack_dump_impl(int fd)
222222
{
223-
#define BACKTRACE_SIZE 16
223+
#define BACKTRACE_SIZE 32
224224
#define TRACEBACK_ENTRY_MAX_SIZE 256
225225
void *callstack[BACKTRACE_SIZE];
226226
int frames = backtrace(callstack, BACKTRACE_SIZE);
227+
if (frames == 0) {
228+
// Some systems won't return anything for the stack trace
229+
PUTS(fd, " <system returned no stack trace>\n");
230+
return;
231+
}
232+
227233
char **strings = backtrace_symbols(callstack, BACKTRACE_SIZE);
228234
if (strings == NULL) {
229-
PUTS(fd, " <failed to get stack trace>\n");
235+
PUTS(fd, " <not enough memory to get stack trace>\n");
236+
return;
230237
}
231-
else {
232-
for (int i = 0; i < frames; ++i) {
233-
char entry_str[TRACEBACK_ENTRY_MAX_SIZE];
234-
snprintf(entry_str, TRACEBACK_ENTRY_MAX_SIZE, " %s\n", strings[i]);
235-
size_t length = strlen(entry_str) + 1;
236-
if (length == TRACEBACK_ENTRY_MAX_SIZE) {
237-
/* We exceeded the size, make it look prettier */
238-
// Add ellipsis to last 3 characters
239-
entry_str[TRACEBACK_ENTRY_MAX_SIZE - 5] = '.';
240-
entry_str[TRACEBACK_ENTRY_MAX_SIZE - 4] = '.';
241-
entry_str[TRACEBACK_ENTRY_MAX_SIZE - 3] = '.';
242-
// Ensure trailing newline
243-
entry_str[TRACEBACK_ENTRY_MAX_SIZE - 2] = '\n';
244-
// Ensure that it's null-terminated
245-
entry_str[TRACEBACK_ENTRY_MAX_SIZE - 1] = '\0';
246-
}
247-
_Py_write_noraise(fd, entry_str, length);
248-
}
249-
250-
if (frames == BACKTRACE_SIZE) {
251-
PUTS(fd, " <truncated rest of calls>\n");
238+
for (int i = 0; i < frames; ++i) {
239+
char entry_str[TRACEBACK_ENTRY_MAX_SIZE];
240+
snprintf(entry_str, TRACEBACK_ENTRY_MAX_SIZE, " %s\n", strings[i]);
241+
size_t length = strlen(entry_str) + 1;
242+
if (length == TRACEBACK_ENTRY_MAX_SIZE) {
243+
/* We exceeded the size, make it look prettier */
244+
// Add ellipsis to last 3 characters
245+
entry_str[TRACEBACK_ENTRY_MAX_SIZE - 5] = '.';
246+
entry_str[TRACEBACK_ENTRY_MAX_SIZE - 4] = '.';
247+
entry_str[TRACEBACK_ENTRY_MAX_SIZE - 3] = '.';
248+
// Ensure trailing newline
249+
entry_str[TRACEBACK_ENTRY_MAX_SIZE - 2] = '\n';
250+
// Ensure that it's null-terminated
251+
entry_str[TRACEBACK_ENTRY_MAX_SIZE - 1] = '\0';
252252
}
253+
_Py_write_noraise(fd, entry_str, length);
254+
}
253255

254-
free(strings);
256+
if (frames == BACKTRACE_SIZE) {
257+
PUTS(fd, " <truncated rest of calls>\n");
255258
}
259+
260+
free(strings);
256261
#undef BACKTRACE_SIZE
257262
#undef TRACEBACK_ENTRY_MAX_SIZE
258263
}

0 commit comments

Comments
 (0)