Skip to content

Commit

Permalink
feat: print location and message on fatal error
Browse files Browse the repository at this point in the history
PR-URL: #137
Reviewed-BY: hyj1991 <[email protected]>
  • Loading branch information
legendecas authored Feb 12, 2022
1 parent 95ed727 commit eb5fbc5
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 26 deletions.
3 changes: 3 additions & 0 deletions src/commands/report/heap_statistics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ using v8::HeapStatistics;
using v8::Isolate;

void NodeReport::SetHeapStatistics(JSONWriter* writer) {
if (isolate_ == nullptr) {
return;
}
HeapStatistics v8_heap_stats;
isolate_->GetHeapStatistics(&v8_heap_stats);

Expand Down
3 changes: 3 additions & 0 deletions src/commands/report/javascript_stack.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ using v8::StackFrame;
using v8::StackTrace;

void NodeReport::SetJavaScriptStack(JSONWriter* writer, bool fatal_error) {
if (isolate_ == nullptr) {
return;
}
HandleScope scope(isolate_);
RegisterState state;
SampleInfo info;
Expand Down
30 changes: 20 additions & 10 deletions src/hooks/fatal_error.cc
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
#include "../commands/report/node_report.h"
#include "../library/utils.h"
#include "../logger.h"
#include "../platform/platform.h"
#include "commands/report/node_report.h"
#include "configure-inl.h"
#include "library/utils.h"
#include "logger.h"
#include "nan.h"
#include "platform/platform.h"
#include "util.h"
#include "xpf_v8.h"

namespace xprofiler {
using std::string;
using std::to_string;
using v8::Isolate;

static const char module_type[] = "fatal_error";
constexpr char module_type[] = "fatal_error";

[[noreturn]] void OnFatalError(const char* location, const char* message) {
if (location) {
fprintf(stderr, "xprofiler: %s %s\n", location, message);
} else {
fprintf(stderr, "xprofiler: %s\n", message);
}
fflush(stderr);

static void OnFatalError(const char* location, const char* message) {
Isolate* isolate = Isolate::GetCurrent();
string filepath = GetLogDir() + GetSep() + "x-fatal-error-" +
to_string(GetPid()) + "-" + ConvertTime("%Y%m%d") + "-" +
RandNum() + ".diag";

Info(module_type, "dump report to %s.", filepath.c_str());
Isolate* isolate = TryGetCurrentIsolate();
NodeReport::GetNodeReport(isolate, filepath, location, message, true);
Info(module_type, "report dumped.");
raise(SIGABRT);

Abort();
}

void SetFatalErrorHandler() {
Isolate* isolate = Isolate::GetCurrent();
void SetFatalErrorHandler(v8::Isolate* isolate) {
isolate->SetFatalErrorHandler(OnFatalError);
}
} // namespace xprofiler
9 changes: 5 additions & 4 deletions src/hooks/fatal_error.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#ifndef _SRC_HOOKS_FATAL_ERROR_H
#define _SRC_HOOKS_FATAL_ERROR_H
#ifndef XPROFILER_SRC_HOOKS_FATAL_ERROR_H
#define XPROFILER_SRC_HOOKS_FATAL_ERROR_H

namespace xprofiler {
void SetFatalErrorHandler();
[[noreturn]] void OnFatalError(const char* location, const char* message);
void SetFatalErrorHandler(v8::Isolate* isolate);
}

#endif
#endif /* XPROFILER_SRC_HOOKS_FATAL_ERROR_H */
2 changes: 1 addition & 1 deletion src/hooks/set_hooks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace xprofiler {
void SetHooks(const FunctionCallbackInfo<Value>& info) {
// set fatal error hook
if (GetEnableFatalErrorHook()) {
SetFatalErrorHandler();
SetFatalErrorHandler(info.GetIsolate());
}
}
} // namespace xprofiler
10 changes: 4 additions & 6 deletions src/hooks/set_hooks.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#ifndef _SRC_HOOKS_SET_HOOKS_H
#define _SRC_HOOKS_SET_HOOKS_H
#ifndef XPROFILER_SRC_HOOKS_SET_HOOKS_H
#define XPROFILER_SRC_HOOKS_SET_HOOKS_H

#include "nan.h"

namespace xprofiler {
using Nan::FunctionCallbackInfo;
using v8::Value;
void SetHooks(const FunctionCallbackInfo<Value>& info);
void SetHooks(const Nan::FunctionCallbackInfo<v8::Value>& info);
} // namespace xprofiler

#endif
#endif /* XPROFILER_SRC_HOOKS_SET_HOOKS_H */
13 changes: 8 additions & 5 deletions src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
#include <cstdio>
#include <cstdlib>

#include "hooks/fatal_error.h"
#include "platform/platform.h"

namespace xprofiler {
[[noreturn]] void Abort() {
std::fflush(stderr);
std::abort();
}

[[noreturn]] void Assert(const AssertionInfo& info) {
fprintf(stderr, "xprofiler: %s:%s%s Assertion `%s' failed.\n", info.file_line,
info.function, *info.function ? ":" : "", info.message);
fflush(stderr);

Abort();
std::string location =
std::string(info.file_line) + ":" + std::string(info.function);
std::string message =
"Assertion `" + std::string(info.message) + "' failed.\n";
OnFatalError(location.c_str(), message.c_str());
}

} // namespace xprofiler
9 changes: 9 additions & 0 deletions src/xpf_v8.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ class HandleScope {
void* operator new(size_t size) = delete;
void operator delete(void*, size_t) = delete;
};

inline v8::Isolate* TryGetCurrentIsolate() {
#if NODE_MODULE_VERSION > NODE_16_0_MODULE_VERSION
return v8::Isolate::TryGetCurrent();
#else
return v8::Isolate::GetCurrent();
#endif
}

} // namespace xprofiler

#endif /* XPROFILER_SRC_XPF_V8_H */

0 comments on commit eb5fbc5

Please sign in to comment.