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
  static char name[1024];

  char link[1024];
  snprintf(link, sizeof(link), "/proc/%d/exe", getpid());
36
  auto read = readlink(link, name, sizeof(name) - 1);
37 38 39 40 41 42 43 44
  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
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) {
I
Igor Canadi 已提交
73 74 75 76
  static int pid = getpid();
  // out source to atos, for the address translation
  const int kLineMax = 256;
  char cmd[kLineMax];
I
Igor Canadi 已提交
77
  snprintf(cmd, kLineMax, "xcrun atos -d %p -p %d  2>&1", frame, pid);
I
Igor Canadi 已提交
78 79 80 81 82 83
  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);
84
    }
I
Igor Canadi 已提交
85 86 87
    pclose(f);
  } else if (symbol) {
    fprintf(stderr, "%s ", symbol);
I
Igor Canadi 已提交
88
  }
I
Igor Canadi 已提交
89

I
Igor Canadi 已提交
90 91 92 93 94 95 96
  fprintf(stderr, "\n");
}

#endif

}  // namespace

97
void PrintStack(int first_frames_to_skip) {
98
  const int kMaxFrames = 100;
I
Igor Canadi 已提交
99
  void* frames[kMaxFrames];
100 101 102 103

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

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

110 111 112 113 114 115
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);
116 117 118 119 120 121 122 123 124 125 126 127 128
  // 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 已提交
129
#endif
130

I
Igor Canadi 已提交
131 132
}  // namespace port
}  // namespace rocksdb