From 043178bd25fdf32aaac53ee32ff8c90925621e1c Mon Sep 17 00:00:00 2001 From: Felix Brendel Date: Sat, 15 May 2021 00:11:19 +0200 Subject: [PATCH] better stracktrace on linux (using gdb if wanted) --- macros.hpp | 7 +++---- stacktrace.hpp | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/macros.hpp b/macros.hpp index 692afea..21947f4 100644 --- a/macros.hpp +++ b/macros.hpp @@ -11,10 +11,9 @@ #define proc auto #define concat(x, y) x ## y #define label(x, y) concat(x, y) -#define line_label(x) label(x, __LINE__) #define MPI_LABEL(id1,id2) \ - concat(MPI_LABEL_ ## id1 ## _ ## id2 ## _, __LINE__) + label(MPI_LABEL_ ## id1 ## _ ## id2 ## _, __LINE__) #define MPP_DECLARE(labid, declaration) \ if (0) \ @@ -51,10 +50,10 @@ // #ifndef min -// #define min(a, b) ((a) < (b)) ? (a) : (b) +#define min(a, b) ((a) < (b)) ? (a) : (b) // #endif // #ifndef max -// #define max(a, b) ((a) > (b)) ? (a) : (b) +#define max(a, b) ((a) > (b)) ? (a) : (b) // #endif /** diff --git a/stacktrace.hpp b/stacktrace.hpp index 81be6b4..838f79a 100644 --- a/stacktrace.hpp +++ b/stacktrace.hpp @@ -1,14 +1,23 @@ #pragma once #include "platform.hpp" +#include "allocation_stats.hpp" #if defined FTB_WINDOWS # include #else -# include -# include -# include "stdio.h" -# include "stdlib.h" +# ifdef FTB_LINUX_STACK_TRACE_USE_GDB +# include +# include +# include +# include +# include +# else +# include +# include +# include "stdio.h" +# include "stdlib.h" +# endif #endif auto print_stacktrace() -> void { @@ -33,6 +42,22 @@ auto print_stacktrace() -> void { printf( " %3i: %s\n", frames - i - 1, symbol->Name); } fflush(stdout); +#else +#ifdef FTB_LINUX_STACK_TRACE_USE_GDB + printf("Stacktrace: \n"); + char pid_buf[30]; + sprintf(pid_buf, "%d", getpid()); + char name_buf[512]; + name_buf[readlink("/proc/self/exe", name_buf, 511)]=0; + prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, 0, 0, 0); + int child_pid = fork(); + if (!child_pid) { + dup2(2,1); // redirect output to stderr - edit: unnecessary? + execl("/usr/bin/gdb", "gdb", "--batch", "-n", "-ex", "thread", "-ex", "bt", name_buf, pid_buf, NULL); + abort(); /* If gdb failed to start */ + } else { + waitpid(child_pid,NULL,0); + } #else // NOTE(Felix): Don't forget to compile with "-rdynamic" printf("Stacktrace (this is unmagled -- sorry): \n"); @@ -47,4 +72,5 @@ auto print_stacktrace() -> void { puts(""); ftb_free(strings); #endif +#endif }