From 8f46505a50b86e85240bf43bab67d0005cb5d50f Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 30 Apr 2022 09:28:05 +0530 Subject: [PATCH] Header to print stack traces in C --- kitty/backtrace.h | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 kitty/backtrace.h diff --git a/kitty/backtrace.h b/kitty/backtrace.h new file mode 100644 index 000000000..e3497bba5 --- /dev/null +++ b/kitty/backtrace.h @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2022 Kovid Goyal + * + * Distributed under terms of the GPL3 license. + */ + +#pragma once + +#include +#include +#include +#include + +#if __has_include() +#include + +static inline void +print_stack_trace(void) { + void *array[256]; + size_t size; + + // get void*'s for all entries on the stack + size = backtrace(array, 256); + + // print out all the frames to stderr + backtrace_symbols_fd(array, size, STDERR_FILENO); +} +#else +static inline void +print_stack_trace(void) { + fprintf(stderr, "stack trace functionality not available"); +} +#endif