2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02: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 23:59:46 +02:00
|
|
|
//
|
2014-04-23 15:11:35 +02:00
|
|
|
#include "port/stack_trace.h"
|
2013-04-11 19:54:35 +02:00
|
|
|
|
2020-09-23 21:54:29 +02:00
|
|
|
#if defined(ROCKSDB_LITE) || \
|
|
|
|
!(defined(ROCKSDB_BACKTRACE) || defined(OS_MACOSX)) || defined(CYGWIN) || \
|
2021-04-06 09:27:59 +02:00
|
|
|
defined(OS_SOLARIS) || defined(OS_WIN)
|
2014-04-23 15:11:35 +02:00
|
|
|
|
|
|
|
// noop
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2014-11-06 21:01:02 +01:00
|
|
|
namespace port {
|
2014-04-23 15:11:35 +02:00
|
|
|
void InstallStackTraceHandler() {}
|
2018-03-05 22:08:17 +01:00
|
|
|
void PrintStack(int /*first_frames_to_skip*/) {}
|
2020-04-11 02:18:56 +02:00
|
|
|
void PrintAndFreeStack(void* /*callstack*/, int /*num_frames*/) {}
|
|
|
|
void* SaveStack(int* /*num_frames*/, int /*first_frames_to_skip*/) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-11-06 21:01:02 +01:00
|
|
|
} // namespace port
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2014-04-23 15:11:35 +02:00
|
|
|
|
|
|
|
#else
|
2013-04-11 19:54:35 +02:00
|
|
|
|
|
|
|
#include <execinfo.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2014-04-24 19:52:20 +02:00
|
|
|
#include <cxxabi.h>
|
2013-04-11 19:54:35 +02:00
|
|
|
|
2021-04-06 09:27:59 +02:00
|
|
|
#if defined(OS_FREEBSD)
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#endif
|
|
|
|
|
2021-07-16 08:49:12 +02:00
|
|
|
#include "port/lang.h"
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2014-11-06 21:01:02 +01:00
|
|
|
namespace port {
|
|
|
|
|
2014-04-23 15:11:35 +02:00
|
|
|
namespace {
|
2013-04-11 19:54:35 +02:00
|
|
|
|
2020-06-18 18:50:05 +02:00
|
|
|
#if defined(OS_LINUX) || defined(OS_FREEBSD) || defined(OS_GNU_KFREEBSD)
|
2014-04-23 15:11:35 +02:00
|
|
|
const char* GetExecutableName() {
|
2013-04-11 19:54:35 +02:00
|
|
|
static char name[1024];
|
|
|
|
|
2021-04-06 09:27:59 +02:00
|
|
|
#if !defined(OS_FREEBSD)
|
2013-04-11 19:54:35 +02:00
|
|
|
char link[1024];
|
|
|
|
snprintf(link, sizeof(link), "/proc/%d/exe", getpid());
|
2014-09-05 08:11:28 +02:00
|
|
|
auto read = readlink(link, name, sizeof(name) - 1);
|
2013-04-11 19:54:35 +02:00
|
|
|
if (-1 == read) {
|
|
|
|
return nullptr;
|
|
|
|
} else {
|
|
|
|
name[read] = 0;
|
|
|
|
return name;
|
|
|
|
}
|
2021-04-06 09:27:59 +02:00
|
|
|
#else
|
|
|
|
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) {
|
|
|
|
return nullptr;
|
|
|
|
} else {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
#endif
|
2013-04-11 19:54:35 +02:00
|
|
|
}
|
|
|
|
|
2014-04-23 15:11:35 +02: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 12:00:55 +01:00
|
|
|
#elif defined(OS_MACOSX)
|
2014-04-23 15:11:35 +02: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 15:50:51 +02:00
|
|
|
static int pid = getpid();
|
|
|
|
// out source to atos, for the address translation
|
|
|
|
const int kLineMax = 256;
|
|
|
|
char cmd[kLineMax];
|
2015-02-05 01:24:02 +01: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 15:50:51 +02: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 19:52:20 +02: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 15:50:51 +02:00
|
|
|
pclose(f);
|
|
|
|
} else if (symbol) {
|
|
|
|
fprintf(stderr, "%s ", symbol);
|
2014-04-23 15:11:35 +02: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 15:50:51 +02:00
|
|
|
|
2014-04-23 15:11:35 +02:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-04-11 02:18:56 +02: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 02:11:09 +01:00
|
|
|
void PrintStack(int first_frames_to_skip) {
|
2013-04-11 19:54:35 +02:00
|
|
|
const int kMaxFrames = 100;
|
2014-04-23 15:11:35 +02:00
|
|
|
void* frames[kMaxFrames];
|
2013-04-11 19:54:35 +02:00
|
|
|
|
|
|
|
auto num_frames = backtrace(frames, kMaxFrames);
|
2020-04-11 02:18:56 +02:00
|
|
|
PrintStack(&frames[first_frames_to_skip], num_frames - first_frames_to_skip);
|
|
|
|
}
|
2013-04-11 19:54:35 +02:00
|
|
|
|
2020-04-11 02:18:56 +02: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];
|
|
|
|
|
|
|
|
auto count = backtrace(frames, kMaxFrames);
|
|
|
|
*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 02:11:09 +01:00
|
|
|
}
|
2013-04-11 19:54:35 +02:00
|
|
|
|
2013-12-07 02:11:09 +01: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 19:15:36 +01: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 08:49:12 +02:00
|
|
|
#ifdef __SANITIZE_THREAD__
|
2020-12-01 19:15:36 +01: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 19:54:35 +02: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);
|
|
|
|
}
|
|
|
|
|
2014-04-23 15:11:35 +02:00
|
|
|
} // namespace port
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2014-11-06 21:01:02 +01:00
|
|
|
|
|
|
|
#endif
|