2016-02-09 23:12:00 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2013-10-16 21:59:46 +00:00
|
|
|
//
|
2014-04-23 13:11:35 +00:00
|
|
|
#include "port/stack_trace.h"
|
2013-04-11 17:54:35 +00:00
|
|
|
|
2023-01-27 21:14:19 +00:00
|
|
|
#if !(defined(ROCKSDB_BACKTRACE) || defined(OS_MACOSX)) || defined(CYGWIN) || \
|
2021-04-06 07:27:59 +00:00
|
|
|
defined(OS_SOLARIS) || defined(OS_WIN)
|
2014-04-23 13:11:35 +00:00
|
|
|
|
|
|
|
// noop
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2014-11-06 20:01:02 +00:00
|
|
|
namespace port {
|
2014-04-23 13:11:35 +00:00
|
|
|
void InstallStackTraceHandler() {}
|
2018-03-05 21:08:17 +00:00
|
|
|
void PrintStack(int /*first_frames_to_skip*/) {}
|
2020-04-11 00:18:56 +00:00
|
|
|
void PrintAndFreeStack(void* /*callstack*/, int /*num_frames*/) {}
|
|
|
|
void* SaveStack(int* /*num_frames*/, int /*first_frames_to_skip*/) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-11-06 20:01:02 +00:00
|
|
|
} // namespace port
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2014-04-23 13:11:35 +00:00
|
|
|
|
|
|
|
#else
|
2013-04-11 17:54:35 +00:00
|
|
|
|
2022-10-24 23:56:01 +00:00
|
|
|
#include <cxxabi.h>
|
2013-04-11 17:54:35 +00:00
|
|
|
#include <execinfo.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-06-27 18:58:29 +00:00
|
|
|
#ifdef OS_OPENBSD
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#endif // OS_OPENBSD
|
2023-02-04 00:49:54 +00:00
|
|
|
#ifdef OS_FREEBSD
|
2021-04-06 07:27:59 +00:00
|
|
|
#include <sys/sysctl.h>
|
2023-02-04 00:49:54 +00:00
|
|
|
#endif // OS_FREEBSD
|
2022-10-18 07:35:35 +00:00
|
|
|
#ifdef OS_LINUX
|
|
|
|
#include <sys/prctl.h>
|
2023-02-03 21:21:03 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2023-02-04 00:49:54 +00:00
|
|
|
#if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 30)
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#define gettid() syscall(SYS_gettid)
|
|
|
|
#endif // GLIBC version
|
|
|
|
#endif // OS_LINUX
|
2021-04-06 07:27:59 +00:00
|
|
|
|
2021-07-16 06:49:12 +00:00
|
|
|
#include "port/lang.h"
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2014-11-06 20:01:02 +00:00
|
|
|
namespace port {
|
|
|
|
|
2014-04-23 13:11:35 +00:00
|
|
|
namespace {
|
2013-04-11 17:54:35 +00:00
|
|
|
|
2023-06-27 18:58:29 +00:00
|
|
|
#if defined(OS_LINUX) || defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_GNU_KFREEBSD)
|
2014-04-23 13:11:35 +00:00
|
|
|
const char* GetExecutableName() {
|
2013-04-11 17:54:35 +00:00
|
|
|
static char name[1024];
|
|
|
|
|
2023-06-27 18:58:29 +00:00
|
|
|
#if defined(OS_FREEBSD)
|
|
|
|
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
|
|
|
|
size_t namesz = sizeof(name);
|
|
|
|
|
|
|
|
auto ret = sysctl(mib, 4, name, &namesz, nullptr, 0);
|
|
|
|
if (-1 == ret) {
|
2013-04-11 17:54:35 +00:00
|
|
|
return nullptr;
|
|
|
|
} else {
|
|
|
|
return name;
|
|
|
|
}
|
2023-06-27 18:58:29 +00:00
|
|
|
#elif defined(OS_OPENBSD)
|
|
|
|
int mib[4] = {CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV};
|
2021-04-06 07:27:59 +00:00
|
|
|
size_t namesz = sizeof(name);
|
2023-06-27 18:58:29 +00:00
|
|
|
char* bin[namesz];
|
2021-04-06 07:27:59 +00:00
|
|
|
|
2023-06-27 18:58:29 +00:00
|
|
|
auto ret = sysctl(mib, 4, bin, &namesz, nullptr, 0);
|
2021-04-06 07:27:59 +00:00
|
|
|
if (-1 == ret) {
|
|
|
|
return nullptr;
|
|
|
|
} else {
|
2023-06-27 18:58:29 +00:00
|
|
|
return bin[0];
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
char link[1024];
|
|
|
|
snprintf(link, sizeof(link), "/proc/%d/exe", getpid());
|
|
|
|
auto read = readlink(link, name, sizeof(name) - 1);
|
|
|
|
if (-1 == read) {
|
|
|
|
return nullptr;
|
|
|
|
} else {
|
|
|
|
name[read] = 0;
|
2021-04-06 07:27:59 +00:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
#endif
|
2013-04-11 17:54:35 +00:00
|
|
|
}
|
|
|
|
|
2014-04-23 13:11:35 +00:00
|
|
|
void PrintStackTraceLine(const char* symbol, void* frame) {
|
|
|
|
static const char* executable = GetExecutableName();
|
|
|
|
if (symbol) {
|
|
|
|
fprintf(stderr, "%s ", symbol);
|
|
|
|
}
|
|
|
|
if (executable) {
|
|
|
|
// out source to addr2line, for the address translation
|
|
|
|
const int kLineMax = 256;
|
|
|
|
char cmd[kLineMax];
|
|
|
|
snprintf(cmd, kLineMax, "addr2line %p -e %s -f -C 2>&1", frame, executable);
|
|
|
|
auto f = popen(cmd, "r");
|
|
|
|
if (f) {
|
|
|
|
char line[kLineMax];
|
|
|
|
while (fgets(line, sizeof(line), f)) {
|
|
|
|
line[strlen(line) - 1] = 0; // remove newline
|
|
|
|
fprintf(stderr, "%s\t", line);
|
|
|
|
}
|
|
|
|
pclose(f);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, " %p", frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
2015-03-17 11:00:55 +00:00
|
|
|
#elif defined(OS_MACOSX)
|
2014-04-23 13:11:35 +00:00
|
|
|
|
|
|
|
void PrintStackTraceLine(const char* symbol, void* frame) {
|
Better stack trace in MAC
Summary:
Now this gives us the real deal stack trace:
Assertion failed: (false), function GetProperty, file db/db_impl.cc, line 4072.
Received signal 6 (Abort trap: 6)
#0 0x7fff57ce39b9
#1 abort (in libsystem_c.dylib) + 125
#2 basename (in libsystem_c.dylib) + 0
#3 rocksdb::DBImpl::GetProperty(rocksdb::ColumnFamilyHandle*, rocksdb::Slice const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*) (in db_test) (db_impl.cc:4072)
#4 rocksdb::_Test_Empty::_Run() (in db_test) (testharness.h:68)
#5 rocksdb::_Test_Empty::_RunIt() (in db_test) (db_test.cc:1005)
#6 rocksdb::test::RunAllTests() (in db_test) (testharness.cc:60)
#7 main (in db_test) (db_test.cc:6697)
#8 start (in libdyld.dylib) + 1
Test Plan: added artificial assert, saw great stack trace
Reviewers: haobo, dhruba, ljin
Reviewed By: haobo
CC: leveldb
Differential Revision: https://reviews.facebook.net/D18309
2014-04-25 13:50:51 +00:00
|
|
|
static int pid = getpid();
|
|
|
|
// out source to atos, for the address translation
|
|
|
|
const int kLineMax = 256;
|
|
|
|
char cmd[kLineMax];
|
2015-02-05 00:24:02 +00:00
|
|
|
snprintf(cmd, kLineMax, "xcrun atos %p -p %d 2>&1", frame, pid);
|
Better stack trace in MAC
Summary:
Now this gives us the real deal stack trace:
Assertion failed: (false), function GetProperty, file db/db_impl.cc, line 4072.
Received signal 6 (Abort trap: 6)
#0 0x7fff57ce39b9
#1 abort (in libsystem_c.dylib) + 125
#2 basename (in libsystem_c.dylib) + 0
#3 rocksdb::DBImpl::GetProperty(rocksdb::ColumnFamilyHandle*, rocksdb::Slice const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*) (in db_test) (db_impl.cc:4072)
#4 rocksdb::_Test_Empty::_Run() (in db_test) (testharness.h:68)
#5 rocksdb::_Test_Empty::_RunIt() (in db_test) (db_test.cc:1005)
#6 rocksdb::test::RunAllTests() (in db_test) (testharness.cc:60)
#7 main (in db_test) (db_test.cc:6697)
#8 start (in libdyld.dylib) + 1
Test Plan: added artificial assert, saw great stack trace
Reviewers: haobo, dhruba, ljin
Reviewed By: haobo
CC: leveldb
Differential Revision: https://reviews.facebook.net/D18309
2014-04-25 13:50:51 +00:00
|
|
|
auto f = popen(cmd, "r");
|
|
|
|
if (f) {
|
|
|
|
char line[kLineMax];
|
|
|
|
while (fgets(line, sizeof(line), f)) {
|
|
|
|
line[strlen(line) - 1] = 0; // remove newline
|
|
|
|
fprintf(stderr, "%s\t", line);
|
2014-04-24 17:52:20 +00:00
|
|
|
}
|
Better stack trace in MAC
Summary:
Now this gives us the real deal stack trace:
Assertion failed: (false), function GetProperty, file db/db_impl.cc, line 4072.
Received signal 6 (Abort trap: 6)
#0 0x7fff57ce39b9
#1 abort (in libsystem_c.dylib) + 125
#2 basename (in libsystem_c.dylib) + 0
#3 rocksdb::DBImpl::GetProperty(rocksdb::ColumnFamilyHandle*, rocksdb::Slice const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*) (in db_test) (db_impl.cc:4072)
#4 rocksdb::_Test_Empty::_Run() (in db_test) (testharness.h:68)
#5 rocksdb::_Test_Empty::_RunIt() (in db_test) (db_test.cc:1005)
#6 rocksdb::test::RunAllTests() (in db_test) (testharness.cc:60)
#7 main (in db_test) (db_test.cc:6697)
#8 start (in libdyld.dylib) + 1
Test Plan: added artificial assert, saw great stack trace
Reviewers: haobo, dhruba, ljin
Reviewed By: haobo
CC: leveldb
Differential Revision: https://reviews.facebook.net/D18309
2014-04-25 13:50:51 +00:00
|
|
|
pclose(f);
|
|
|
|
} else if (symbol) {
|
|
|
|
fprintf(stderr, "%s ", symbol);
|
2014-04-23 13:11:35 +00:00
|
|
|
}
|
Better stack trace in MAC
Summary:
Now this gives us the real deal stack trace:
Assertion failed: (false), function GetProperty, file db/db_impl.cc, line 4072.
Received signal 6 (Abort trap: 6)
#0 0x7fff57ce39b9
#1 abort (in libsystem_c.dylib) + 125
#2 basename (in libsystem_c.dylib) + 0
#3 rocksdb::DBImpl::GetProperty(rocksdb::ColumnFamilyHandle*, rocksdb::Slice const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*) (in db_test) (db_impl.cc:4072)
#4 rocksdb::_Test_Empty::_Run() (in db_test) (testharness.h:68)
#5 rocksdb::_Test_Empty::_RunIt() (in db_test) (db_test.cc:1005)
#6 rocksdb::test::RunAllTests() (in db_test) (testharness.cc:60)
#7 main (in db_test) (db_test.cc:6697)
#8 start (in libdyld.dylib) + 1
Test Plan: added artificial assert, saw great stack trace
Reviewers: haobo, dhruba, ljin
Reviewed By: haobo
CC: leveldb
Differential Revision: https://reviews.facebook.net/D18309
2014-04-25 13:50:51 +00:00
|
|
|
|
2014-04-23 13:11:35 +00:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2023-05-01 21:12:40 +00:00
|
|
|
const char* GetLldbScriptSelectThread(long long tid) {
|
|
|
|
// NOTE: called from a signal handler, so no heap allocation
|
|
|
|
static char script[80];
|
|
|
|
snprintf(script, sizeof(script),
|
|
|
|
"script -l python -- lldb.process.SetSelectedThreadByID(%lld)", tid);
|
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
2014-04-23 13:11:35 +00:00
|
|
|
} // namespace
|
|
|
|
|
2020-04-11 00:18:56 +00:00
|
|
|
void PrintStack(void* frames[], int num_frames) {
|
|
|
|
auto symbols = backtrace_symbols(frames, num_frames);
|
|
|
|
|
|
|
|
for (int i = 0; i < num_frames; ++i) {
|
|
|
|
fprintf(stderr, "#%-2d ", i);
|
|
|
|
PrintStackTraceLine((symbols != nullptr) ? symbols[i] : nullptr, frames[i]);
|
|
|
|
}
|
|
|
|
free(symbols);
|
|
|
|
}
|
|
|
|
|
2013-12-07 01:11:09 +00:00
|
|
|
void PrintStack(int first_frames_to_skip) {
|
2023-03-05 16:21:57 +00:00
|
|
|
// Default to getting stack traces with GDB, at least on Linux where we
|
|
|
|
// know how to attach to a particular thread.
|
|
|
|
//
|
|
|
|
// * Address space layout randomization (ASLR) interferes with getting good
|
|
|
|
// stack information from backtrace+addr2line. This is more likely to show
|
|
|
|
// up with LIB_MODE=shared builds (when kernel.randomize_va_space >= 1)
|
|
|
|
// but can also show up with LIB_MODE=static builds ((when
|
|
|
|
// kernel.randomize_va_space == 2).
|
|
|
|
// * It doesn't appear easy to detect when ASLR is in use.
|
|
|
|
// * With DEBUG_LEVEL < 2, backtrace() can skip frames that are not skipped
|
|
|
|
// in GDB.
|
2023-05-01 21:12:40 +00:00
|
|
|
//
|
|
|
|
// LLDB also available as an option
|
|
|
|
bool lldb_stack_trace = getenv("ROCKSDB_LLDB_STACK") != nullptr;
|
2023-03-05 16:21:57 +00:00
|
|
|
#if defined(OS_LINUX)
|
|
|
|
// Default true, override with ROCKSDB_BACKTRACE_STACK=1
|
2023-05-01 21:12:40 +00:00
|
|
|
bool gdb_stack_trace =
|
|
|
|
!lldb_stack_trace && getenv("ROCKSDB_BACKTRACE_STACK") == nullptr;
|
2023-02-03 21:21:03 +00:00
|
|
|
#else
|
2023-03-05 16:21:57 +00:00
|
|
|
// Default false, override with ROCKSDB_GDB_STACK=1
|
|
|
|
bool gdb_stack_trace = getenv("ROCKSDB_GDB_STACK") != nullptr;
|
2023-02-03 21:21:03 +00:00
|
|
|
#endif
|
|
|
|
// Also support invoking interactive debugger on stack trace, with this
|
|
|
|
// envvar set to non-empty
|
|
|
|
char* debug_env = getenv("ROCKSDB_DEBUG");
|
|
|
|
bool debug = debug_env != nullptr && strlen(debug_env) > 0;
|
|
|
|
|
2023-10-02 23:19:05 +00:00
|
|
|
if (!debug && getenv("ROCKSDB_NO_STACK") != nullptr) {
|
|
|
|
// Skip stack trace
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-01 21:12:40 +00:00
|
|
|
if (lldb_stack_trace || gdb_stack_trace || debug) {
|
2023-02-03 21:21:03 +00:00
|
|
|
// Allow ouside debugger to attach, even with Yama security restrictions
|
|
|
|
#ifdef PR_SET_PTRACER_ANY
|
|
|
|
(void)prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, 0, 0, 0);
|
|
|
|
#endif
|
|
|
|
// Try to invoke GDB, either for stack trace or debugging.
|
2023-05-01 21:12:40 +00:00
|
|
|
long long attach_pid = getpid();
|
|
|
|
// NOTE: we're in a signal handler, so no heap allocation
|
|
|
|
char attach_pid_str[20];
|
|
|
|
snprintf(attach_pid_str, sizeof(attach_pid_str), "%lld", attach_pid);
|
2023-02-03 21:21:03 +00:00
|
|
|
|
|
|
|
// `gdb -p PID` seems to always attach to main thread, but `gdb -p TID`
|
|
|
|
// seems to be able to attach to a particular thread in a process, which
|
|
|
|
// makes sense as the main thread TID == PID of the process.
|
|
|
|
// But I haven't found that gdb capability documented anywhere, so leave
|
|
|
|
// a back door to attach to main thread.
|
2023-05-01 21:12:40 +00:00
|
|
|
long long gdb_attach_id = attach_pid;
|
|
|
|
// Save current thread id before fork
|
|
|
|
long long attach_tid = 0;
|
2023-02-03 21:21:03 +00:00
|
|
|
#ifdef OS_LINUX
|
2023-05-01 21:12:40 +00:00
|
|
|
attach_tid = gettid();
|
2023-02-03 21:21:03 +00:00
|
|
|
if (getenv("ROCKSDB_DEBUG_USE_PID") == nullptr) {
|
2023-05-01 21:12:40 +00:00
|
|
|
gdb_attach_id = attach_tid;
|
2023-02-03 21:21:03 +00:00
|
|
|
}
|
|
|
|
#endif
|
2023-05-01 21:12:40 +00:00
|
|
|
|
|
|
|
char gdb_attach_id_str[20];
|
|
|
|
snprintf(gdb_attach_id_str, sizeof(gdb_attach_id_str), "%lld",
|
|
|
|
gdb_attach_id);
|
|
|
|
|
2023-02-03 21:21:03 +00:00
|
|
|
pid_t child_pid = fork();
|
|
|
|
if (child_pid == 0) {
|
|
|
|
// child process
|
|
|
|
if (debug) {
|
2023-05-01 21:12:40 +00:00
|
|
|
if (strcmp(debug_env, "lldb") == 0) {
|
|
|
|
fprintf(stderr, "Invoking LLDB for debugging (ROCKSDB_DEBUG=%s)...\n",
|
|
|
|
debug_env);
|
|
|
|
execlp(/*cmd in PATH*/ "lldb", /*arg0*/ "lldb", "-p", attach_pid_str,
|
|
|
|
/*"-Q",*/ "-o", GetLldbScriptSelectThread(attach_tid),
|
|
|
|
(char*)nullptr);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Invoking GDB for debugging (ROCKSDB_DEBUG=%s)...\n",
|
|
|
|
debug_env);
|
|
|
|
execlp(/*cmd in PATH*/ "gdb", /*arg0*/ "gdb", "-p", gdb_attach_id_str,
|
|
|
|
(char*)nullptr);
|
|
|
|
return;
|
|
|
|
}
|
2023-02-03 21:21:03 +00:00
|
|
|
} else {
|
|
|
|
// Redirect child stdout to original stderr
|
|
|
|
dup2(2, 1);
|
|
|
|
// No child stdin (don't use pager)
|
|
|
|
close(0);
|
2023-05-01 21:12:40 +00:00
|
|
|
if (lldb_stack_trace) {
|
|
|
|
fprintf(stderr, "Invoking LLDB for stack trace...\n");
|
|
|
|
|
|
|
|
// Skip top ~8 frames here in PrintStack
|
|
|
|
auto bt_in_lldb =
|
|
|
|
"script -l python -- for f in lldb.thread.frames[8:]: print(f)";
|
|
|
|
execlp(/*cmd in PATH*/ "lldb", /*arg0*/ "lldb", "-p", attach_pid_str,
|
|
|
|
"-b", "-Q", "-o", GetLldbScriptSelectThread(attach_tid), "-o",
|
|
|
|
bt_in_lldb, (char*)nullptr);
|
|
|
|
} else {
|
|
|
|
// gdb_stack_trace
|
|
|
|
fprintf(stderr, "Invoking GDB for stack trace...\n");
|
|
|
|
|
|
|
|
// Skip top ~4 frames here in PrintStack
|
|
|
|
// See https://stackoverflow.com/q/40991943/454544
|
|
|
|
auto bt_in_gdb =
|
|
|
|
"frame apply level 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 "
|
|
|
|
"21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 "
|
|
|
|
"42 43 44 -q frame";
|
|
|
|
// -n : Loading config files can apparently cause failures with the
|
|
|
|
// other options here.
|
|
|
|
// -batch : non-interactive; suppress banners as much as possible
|
|
|
|
execlp(/*cmd in PATH*/ "gdb", /*arg0*/ "gdb", "-n", "-batch", "-p",
|
|
|
|
gdb_attach_id_str, "-ex", bt_in_gdb, (char*)nullptr);
|
|
|
|
}
|
2023-02-03 21:21:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// parent process; wait for child
|
|
|
|
int wstatus;
|
|
|
|
waitpid(child_pid, &wstatus, 0);
|
|
|
|
if (WIFEXITED(wstatus) && WEXITSTATUS(wstatus) == EXIT_SUCCESS) {
|
|
|
|
// Good
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(stderr, "GDB failed; falling back on backtrace+addr2line...\n");
|
|
|
|
}
|
|
|
|
|
2013-04-11 17:54:35 +00:00
|
|
|
const int kMaxFrames = 100;
|
2014-04-23 13:11:35 +00:00
|
|
|
void* frames[kMaxFrames];
|
2013-04-11 17:54:35 +00:00
|
|
|
|
2023-06-27 18:58:29 +00:00
|
|
|
int num_frames = (int) backtrace(frames, kMaxFrames);
|
2020-04-11 00:18:56 +00:00
|
|
|
PrintStack(&frames[first_frames_to_skip], num_frames - first_frames_to_skip);
|
|
|
|
}
|
2013-04-11 17:54:35 +00:00
|
|
|
|
2020-04-11 00:18:56 +00:00
|
|
|
void PrintAndFreeStack(void* callstack, int num_frames) {
|
|
|
|
PrintStack(static_cast<void**>(callstack), num_frames);
|
|
|
|
free(callstack);
|
|
|
|
}
|
|
|
|
|
|
|
|
void* SaveStack(int* num_frames, int first_frames_to_skip) {
|
|
|
|
const int kMaxFrames = 100;
|
|
|
|
void* frames[kMaxFrames];
|
|
|
|
|
2023-06-27 18:58:29 +00:00
|
|
|
int count = (int) backtrace(frames, kMaxFrames);
|
2020-04-11 00:18:56 +00:00
|
|
|
*num_frames = count - first_frames_to_skip;
|
|
|
|
void* callstack = malloc(sizeof(void*) * *num_frames);
|
|
|
|
memcpy(callstack, &frames[first_frames_to_skip], sizeof(void*) * *num_frames);
|
|
|
|
return callstack;
|
2013-12-07 01:11:09 +00:00
|
|
|
}
|
2013-04-11 17:54:35 +00:00
|
|
|
|
2013-12-07 01:11:09 +00:00
|
|
|
static void StackTraceHandler(int sig) {
|
|
|
|
// reset to default handler
|
|
|
|
signal(sig, SIG_DFL);
|
|
|
|
fprintf(stderr, "Received signal %d (%s)\n", sig, strsignal(sig));
|
|
|
|
// skip the top three signal handler related frames
|
|
|
|
PrintStack(3);
|
2020-12-01 18:15:36 +00:00
|
|
|
|
|
|
|
// Efforts to fix or suppress TSAN warnings "signal-unsafe call inside of
|
|
|
|
// a signal" have failed, so just warn the user about them.
|
2021-07-16 06:49:12 +00:00
|
|
|
#ifdef __SANITIZE_THREAD__
|
2020-12-01 18:15:36 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"==> NOTE: any above warnings about \"signal-unsafe call\" are\n"
|
|
|
|
"==> ignorable, as they are expected when generating a stack\n"
|
|
|
|
"==> trace because of a signal under TSAN. Consider why the\n"
|
|
|
|
"==> signal was generated to begin with, and the stack trace\n"
|
|
|
|
"==> in the TSAN warning can be useful for that. (The stack\n"
|
|
|
|
"==> trace printed by the signal handler is likely obscured\n"
|
|
|
|
"==> by TSAN output.)\n");
|
|
|
|
#endif
|
|
|
|
|
2013-04-11 17:54:35 +00:00
|
|
|
// re-signal to default handler (so we still get core dump if needed...)
|
|
|
|
raise(sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstallStackTraceHandler() {
|
|
|
|
// just use the plain old signal as it's simple and sufficient
|
|
|
|
// for this use case
|
|
|
|
signal(SIGILL, StackTraceHandler);
|
|
|
|
signal(SIGSEGV, StackTraceHandler);
|
|
|
|
signal(SIGBUS, StackTraceHandler);
|
|
|
|
signal(SIGABRT, StackTraceHandler);
|
2023-02-03 21:21:03 +00:00
|
|
|
// Allow ouside debugger to attach, even with Yama security restrictions.
|
|
|
|
// This is needed even outside of PrintStack() so that external mechanisms
|
|
|
|
// can dump stacks if they suspect that a test has hung.
|
2022-10-18 07:35:35 +00:00
|
|
|
#ifdef PR_SET_PTRACER_ANY
|
|
|
|
(void)prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, 0, 0, 0);
|
|
|
|
#endif
|
2013-04-11 17:54:35 +00:00
|
|
|
}
|
|
|
|
|
2014-04-23 13:11:35 +00:00
|
|
|
} // namespace port
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2014-11-06 20:01:02 +00:00
|
|
|
|
|
|
|
#endif
|