2019-07-07 02:05:30 +02:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
2019-07-07 02:05:30 +02:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/utils/port/stacktrace.h"
|
2019-07-21 20:07:07 +02:00
|
|
|
|
2019-07-07 02:05:30 +02:00
|
|
|
#include "td/utils/port/signals.h"
|
|
|
|
|
2019-07-21 20:07:07 +02:00
|
|
|
#if __GLIBC__
|
2019-07-07 02:05:30 +02:00
|
|
|
#include <execinfo.h>
|
2019-07-21 20:07:07 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if TD_LINUX || TD_FREEBSD
|
|
|
|
#include <sys/types.h>
|
2019-07-07 02:05:30 +02:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#if TD_LINUX
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace td {
|
2019-07-21 20:07:07 +02:00
|
|
|
|
2019-07-07 02:05:30 +02:00
|
|
|
namespace {
|
2019-07-21 20:07:07 +02:00
|
|
|
|
2019-07-07 02:05:30 +02:00
|
|
|
void print_backtrace(void) {
|
|
|
|
void *buffer[128];
|
2021-05-11 18:26:35 +02:00
|
|
|
#if TD_PORT_WINDOWS
|
|
|
|
USHORT nptrs = CaptureStackBackTrace(0, 128, buffer, nullptr);
|
|
|
|
#elif __GLIBC__
|
2019-07-07 02:05:30 +02:00
|
|
|
int nptrs = backtrace(buffer, 128);
|
2021-05-11 18:26:35 +02:00
|
|
|
#else
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2019-07-21 20:07:07 +02:00
|
|
|
signal_safe_write("------- Stack Backtrace -------\n", false);
|
2021-05-11 18:26:35 +02:00
|
|
|
#if TD_PORT_WINDOWS
|
|
|
|
for (USHORT i = 0; i < nptrs; i++) {
|
|
|
|
signal_safe_write_pointer(buffer[i], false);
|
|
|
|
}
|
|
|
|
#elif __GLIBC__
|
2019-07-07 02:05:30 +02:00
|
|
|
backtrace_symbols_fd(buffer, nptrs, 2);
|
|
|
|
#endif
|
2021-05-11 18:26:35 +02:00
|
|
|
signal_safe_write("-------------------------------\n", false);
|
2019-07-07 02:05:30 +02:00
|
|
|
}
|
2019-07-21 20:07:07 +02:00
|
|
|
|
2019-07-07 02:05:30 +02:00
|
|
|
void print_backtrace_gdb(void) {
|
2019-07-21 20:07:07 +02:00
|
|
|
#if TD_LINUX || TD_FREEBSD
|
|
|
|
char pid_buf[30];
|
|
|
|
char *pid_buf_begin = pid_buf + sizeof(pid_buf);
|
2019-07-07 02:05:30 +02:00
|
|
|
pid_t pid = getpid();
|
|
|
|
*--pid_buf_begin = '\0';
|
|
|
|
do {
|
|
|
|
*--pid_buf_begin = static_cast<char>(pid % 10 + '0');
|
|
|
|
pid /= 10;
|
|
|
|
} while (pid > 0);
|
|
|
|
|
|
|
|
char name_buf[512];
|
|
|
|
ssize_t res = readlink("/proc/self/exe", name_buf, 511); // TODO works only under Linux
|
|
|
|
if (res >= 0) {
|
|
|
|
name_buf[res] = 0;
|
|
|
|
|
|
|
|
#if TD_LINUX
|
2019-12-20 18:12:52 +01:00
|
|
|
#if defined(PR_SET_DUMPABLE)
|
2019-07-07 02:05:30 +02:00
|
|
|
if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) < 0) {
|
2019-07-21 20:07:07 +02:00
|
|
|
signal_safe_write("Can't set dumpable\n");
|
2019-07-07 02:05:30 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-12-20 18:12:52 +01:00
|
|
|
#endif
|
2019-07-07 02:05:30 +02:00
|
|
|
#if defined(PR_SET_PTRACER)
|
2019-07-23 00:50:12 +02:00
|
|
|
// We can't use event fd because we are in a signal handler
|
2019-07-07 02:05:30 +02:00
|
|
|
int fds[2];
|
|
|
|
bool need_set_ptracer = true;
|
|
|
|
if (pipe(fds) < 0) {
|
|
|
|
need_set_ptracer = false;
|
2019-07-21 20:07:07 +02:00
|
|
|
signal_safe_write("Can't create a pipe\n");
|
2019-07-07 02:05:30 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int child_pid = fork();
|
|
|
|
if (child_pid < 0) {
|
2019-07-21 20:07:07 +02:00
|
|
|
signal_safe_write("Can't fork() to run gdb\n");
|
2019-07-07 02:05:30 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!child_pid) {
|
|
|
|
#if TD_LINUX && defined(PR_SET_PTRACER)
|
|
|
|
if (need_set_ptracer) {
|
|
|
|
char c;
|
2019-07-22 06:05:50 +02:00
|
|
|
if (read(fds[0], &c, 1) < 0) {
|
|
|
|
signal_safe_write("Failed to read from pipe\n");
|
|
|
|
}
|
2019-07-07 02:05:30 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
dup2(2, 1); // redirect output to stderr
|
|
|
|
execlp("gdb", "gdb", "--batch", "-n", "-ex", "thread", "-ex", "thread apply all bt full", name_buf, pid_buf_begin,
|
2019-07-22 06:01:51 +02:00
|
|
|
nullptr);
|
2019-07-07 02:05:30 +02:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
#if TD_LINUX && defined(PR_SET_PTRACER)
|
|
|
|
if (need_set_ptracer) {
|
|
|
|
if (prctl(PR_SET_PTRACER, child_pid, 0, 0, 0) < 0) {
|
2019-07-21 20:07:07 +02:00
|
|
|
signal_safe_write("Can't set ptracer\n");
|
2019-07-07 02:05:30 +02:00
|
|
|
}
|
|
|
|
if (write(fds[1], "a", 1) != 1) {
|
2019-07-21 20:07:07 +02:00
|
|
|
signal_safe_write("Can't write to pipe\n");
|
2019-07-07 02:05:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
waitpid(child_pid, nullptr, 0);
|
|
|
|
}
|
|
|
|
} else {
|
2019-07-21 20:07:07 +02:00
|
|
|
signal_safe_write("Can't get name of executable file to pass to gdb\n");
|
2019-07-07 02:05:30 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2019-07-21 20:07:07 +02:00
|
|
|
|
2019-07-07 02:05:30 +02:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void Stacktrace::print_to_stderr(const PrintOptions &options) {
|
2019-12-20 18:12:52 +01:00
|
|
|
print_backtrace();
|
2019-07-07 02:05:30 +02:00
|
|
|
if (options.use_gdb) {
|
|
|
|
print_backtrace_gdb();
|
|
|
|
}
|
|
|
|
}
|
2019-07-21 20:07:07 +02:00
|
|
|
|
2019-08-14 10:31:25 +02:00
|
|
|
void Stacktrace::init() {
|
|
|
|
#if __GLIBC__
|
2019-12-20 18:12:52 +01:00
|
|
|
// backtrace needs to be called once to ensure that next calls are async-signal-safe
|
2019-08-14 10:31:25 +02:00
|
|
|
void *buffer[1];
|
|
|
|
backtrace(buffer, 1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-07-07 02:05:30 +02:00
|
|
|
} // namespace td
|