Преглед изворни кода

better stracktrace on linux (using gdb if wanted)

master
Felix Brendel пре 5 година
родитељ
комит
043178bd25
2 измењених фајлова са 33 додато и 8 уклоњено
  1. +3
    -4
      macros.hpp
  2. +30
    -4
      stacktrace.hpp

+ 3
- 4
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

/**


+ 30
- 4
stacktrace.hpp Прегледај датотеку

@@ -1,14 +1,23 @@
#pragma once

#include "platform.hpp"
#include "allocation_stats.hpp"

#if defined FTB_WINDOWS
# include <dbghelp.h>
#else
# include <execinfo.h>
# include <unistd.h>
# include "stdio.h"
# include "stdlib.h"
# ifdef FTB_LINUX_STACK_TRACE_USE_GDB
# include <stdio.h>
# include <stdlib.h>
# include <sys/wait.h>
# include <unistd.h>
# include <sys/prctl.h>
# else
# include <execinfo.h>
# include <unistd.h>
# 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
}

Loading…
Откажи
Сачувај