stack_trace.cc 3.3 KB
Newer Older
1 2 3 4 5
//  Copyright (c) 2013, Facebook, Inc.  All rights reserved.
//  This source code is licensed under the BSD-style license found in the
//  LICENSE file in the root directory of this source tree. An additional grant
//  of patent rights can be found in the PATENTS file in the same directory.
//
I
Igor Canadi 已提交
6
#include "port/stack_trace.h"
7

I
Igor Canadi 已提交
8 9 10 11 12 13 14 15 16 17 18
namespace rocksdb {
namespace port {

#if defined(ROCKSDB_LITE) || !(defined(OS_LINUX) || defined(OS_MACOSX))

// noop

void InstallStackTraceHandler() {}
void PrintStack(int first_frames_to_skip) {}

#else
19 20 21 22 23 24 25

#include <execinfo.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
26
#include <cxxabi.h>
27

I
Igor Canadi 已提交
28
namespace {
29

I
Igor Canadi 已提交
30 31
#ifdef OS_LINUX
const char* GetExecutableName() {
32 33 34 35 36 37 38 39 40 41 42 43 44
  static char name[1024];

  char link[1024];
  snprintf(link, sizeof(link), "/proc/%d/exe", getpid());
  auto read = readlink(link, name, sizeof(name));
  if (-1 == read) {
    return nullptr;
  } else {
    name[read] = 0;
    return name;
  }
}

I
Igor Canadi 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
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");
}
#elif OS_MACOSX

void PrintStackTraceLine(const char* symbol, void* frame) {
  if (symbol) {
74 75 76 77 78 79 80 81 82 83
    char filename[64], function[512], plus[2], line[10];
    sscanf(symbol, "%*s %64s %*s %512s %2s %10s", filename, function, plus,
           line);
    int status;
    char* demangled = abi::__cxa_demangle(function, 0, 0, &status);
    fprintf(stderr, "%s   %s %s %s", filename,
            (status == 0) ? demangled : function, plus, line);
    if (demangled) {
      free(demangled);
    }
I
Igor Canadi 已提交
84 85 86 87 88 89 90 91 92
  }
  fprintf(stderr, " %p", frame);
  fprintf(stderr, "\n");
}

#endif

}  // namespace

93
void PrintStack(int first_frames_to_skip) {
94
  const int kMaxFrames = 100;
I
Igor Canadi 已提交
95
  void* frames[kMaxFrames];
96 97 98 99

  auto num_frames = backtrace(frames, kMaxFrames);
  auto symbols = backtrace_symbols(frames, num_frames);

100 101
  for (int i = first_frames_to_skip; i < num_frames; ++i) {
    fprintf(stderr, "#%-2d  ", i - first_frames_to_skip);
I
Igor Canadi 已提交
102
    PrintStackTraceLine((symbols != nullptr) ? symbols[i] : nullptr, frames[i]);
103
  }
104
}
105

106 107 108 109 110 111
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);
112 113 114 115 116 117 118 119 120 121 122 123 124
  // 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);
}

I
Igor Canadi 已提交
125
#endif
126

I
Igor Canadi 已提交
127 128
}  // namespace port
}  // namespace rocksdb