Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

report: reuse triggerReport() for exceptions and signals #26386

Merged
merged 5 commits into from
Mar 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/api/report.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ is provided below for reference.
{
"header": {
"event": "exception",
"location": "OnUncaughtException",
"trigger": "Exception",
"filename": "report.20181221.005011.8974.001.json",
"dumpEventTime": "2018-12-21T00:50:11Z",
"dumpEventTimeStamp": "1545371411331",
Expand Down
5 changes: 4 additions & 1 deletion lib/internal/process/execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ function createFatalException() {
report.syncConfig(config, false);
if (Array.isArray(config.events) &&
config.events.includes('exception')) {
report.onUnCaughtException(er ? er.stack : undefined);
report.triggerReport(er ? er.message : 'Exception',
'Exception',
null,
er ? er.stack : undefined);
}
}
} catch {} // Ignore the exception. Diagnostic reporting is unavailable.
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/process/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const report = {
throw new ERR_INVALID_ARG_TYPE('err', 'Object', err);
}

return nr.triggerReport(file, err.stack);
return nr.triggerReport('JavaScript API', 'API', file, err.stack);
},
getReport(err) {
emitExperimentalWarning('report');
Expand All @@ -104,7 +104,7 @@ const report = {
function handleSignal(signo) {
if (typeof signo !== 'string')
signo = config.signal;
nr.onUserSignal(signo);
nr.triggerReport(signo, 'Signal', null, '');
}

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion src/node_errors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ void OnFatalError(const char* location, const char* message) {
Environment* env = Environment::GetCurrent(isolate);
if (env == nullptr || env->isolate_data()->options()->report_on_fatalerror) {
report::TriggerNodeReport(
isolate, env, message, __func__, "", Local<String>());
isolate, env, message, "FatalError", "", Local<String>());
}
#endif // NODE_REPORT
fflush(stderr);
Expand Down
24 changes: 12 additions & 12 deletions src/node_report.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ using v8::Value;
static void WriteNodeReport(Isolate* isolate,
Environment* env,
const char* message,
const char* location,
const char* trigger,
const std::string& filename,
std::ostream& out,
Local<String> stackstr,
Expand All @@ -79,7 +79,7 @@ static void PrintVersionInformation(JSONWriter* writer);
static void PrintJavaScriptStack(JSONWriter* writer,
Isolate* isolate,
Local<String> stackstr,
const char* location);
const char* trigger);
static void PrintNativeStack(JSONWriter* writer);
#ifndef _WIN32
static void PrintResourceUsage(JSONWriter* writer);
Expand All @@ -100,7 +100,7 @@ static std::atomic_int seq = {0}; // sequence number for report filenames
std::string TriggerNodeReport(Isolate* isolate,
Environment* env,
const char* message,
const char* location,
const char* trigger,
std::string name,
Local<String> stackstr) {
std::ostringstream oss;
Expand Down Expand Up @@ -178,7 +178,7 @@ std::string TriggerNodeReport(Isolate* isolate,
<< "Writing Node.js report to file: " << filename << std::endl;
}

WriteNodeReport(isolate, env, message, location, filename, *outstream,
WriteNodeReport(isolate, env, message, trigger, filename, *outstream,
stackstr, &tm_struct);

// Do not close stdout/stderr, only close files we opened.
Expand All @@ -194,22 +194,22 @@ std::string TriggerNodeReport(Isolate* isolate,
void GetNodeReport(Isolate* isolate,
Environment* env,
const char* message,
const char* location,
const char* trigger,
Local<String> stackstr,
std::ostream& out) {
// Obtain the current time and the pid (platform dependent)
TIME_TYPE tm_struct;
LocalTime(&tm_struct);
WriteNodeReport(
isolate, env, message, location, "", out, stackstr, &tm_struct);
isolate, env, message, trigger, "", out, stackstr, &tm_struct);
}

// Internal function to coordinate and write the various
// sections of the report to the supplied stream
static void WriteNodeReport(Isolate* isolate,
Environment* env,
const char* message,
const char* location,
const char* trigger,
const std::string& filename,
std::ostream& out,
Local<String> stackstr,
Expand All @@ -228,7 +228,7 @@ static void WriteNodeReport(Isolate* isolate,
writer.json_objectstart("header");

writer.json_keyvalue("event", message);
writer.json_keyvalue("location", location);
writer.json_keyvalue("trigger", trigger);
if (!filename.empty())
writer.json_keyvalue("filename", filename);
else
Expand Down Expand Up @@ -280,7 +280,7 @@ static void WriteNodeReport(Isolate* isolate,
writer.json_objectend();

// Report summary JavaScript stack backtrace
PrintJavaScriptStack(&writer, isolate, stackstr, location);
PrintJavaScriptStack(&writer, isolate, stackstr, trigger);

// Report native stack backtrace
PrintNativeStack(&writer);
Expand Down Expand Up @@ -372,12 +372,12 @@ static void PrintVersionInformation(JSONWriter* writer) {
static void PrintJavaScriptStack(JSONWriter* writer,
Isolate* isolate,
Local<String> stackstr,
const char* location) {
const char* trigger) {
writer->json_objectstart("javascriptStack");

std::string ss;
if ((!strcmp(location, "OnFatalError")) ||
(!strcmp(location, "OnUserSignal"))) {
if ((!strcmp(trigger, "FatalError")) ||
(!strcmp(trigger, "Signal"))) {
ss = "No stack.\nUnavailable.\n";
} else {
String::Utf8Value sv(isolate, stackstr);
Expand Down
4 changes: 2 additions & 2 deletions src/node_report.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ typedef struct tm TIME_TYPE;
std::string TriggerNodeReport(v8::Isolate* isolate,
node::Environment* env,
const char* message,
const char* location,
const char* trigger,
std::string name,
v8::Local<v8::String> stackstr);
void GetNodeReport(v8::Isolate* isolate,
node::Environment* env,
const char* message,
const char* location,
const char* trigger,
v8::Local<v8::String> stackstr,
std::ostream& out);

Expand Down
50 changes: 8 additions & 42 deletions src/node_report_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ using v8::String;
using v8::V8;
using v8::Value;

// Internal/static function declarations
void OnUncaughtException(const FunctionCallbackInfo<Value>& info);
static void Initialize(Local<Object> exports,
Local<Value> unused,
Local<Context> context);

// External JavaScript API for triggering a report
void TriggerReport(const FunctionCallbackInfo<Value>& info) {
Environment* env = Environment::GetCurrent(info);
Expand All @@ -48,14 +42,16 @@ void TriggerReport(const FunctionCallbackInfo<Value>& info) {
std::string filename;
Local<String> stackstr;

CHECK_EQ(info.Length(), 2);
stackstr = info[1].As<String>();
CHECK_EQ(info.Length(), 4);
String::Utf8Value message(isolate, info[0].As<String>());
String::Utf8Value trigger(isolate, info[1].As<String>());
stackstr = info[3].As<String>();

if (info[0]->IsString())
filename = *String::Utf8Value(isolate, info[0]);
if (info[2]->IsString())
filename = *String::Utf8Value(isolate, info[2]);

filename = TriggerNodeReport(
isolate, env, "JavaScript API", __func__, filename, stackstr);
isolate, env, *message, *trigger, filename, stackstr);
// Return value is the report filename
info.GetReturnValue().Set(
String::NewFromUtf8(isolate, filename.c_str(), v8::NewStringType::kNormal)
Expand All @@ -79,34 +75,6 @@ void GetReport(const FunctionCallbackInfo<Value>& info) {
.ToLocalChecked());
}

// Callbacks for triggering report on uncaught exception.
// Calls triggered from JS land.
void OnUncaughtException(const FunctionCallbackInfo<Value>& info) {
Environment* env = Environment::GetCurrent(info);
Isolate* isolate = env->isolate();
HandleScope scope(isolate);
std::string filename;
std::shared_ptr<PerIsolateOptions> options = env->isolate_data()->options();

// Trigger report if requested
if (options->report_uncaught_exception) {
TriggerNodeReport(
isolate, env, "exception", __func__, filename, info[0].As<String>());
}
}

// Signal handler for report action, called from JS land (util.js)
void OnUserSignal(const FunctionCallbackInfo<Value>& info) {
Environment* env = Environment::GetCurrent(info);
Isolate* isolate = env->isolate();
CHECK(info[0]->IsString());
Local<String> str = info[0].As<String>();
String::Utf8Value value(isolate, str);
std::string filename;
TriggerNodeReport(
isolate, env, *value, __func__, filename, info[0].As<String>());
}

// A method to sync up data elements in the JS land with its
// corresponding elements in the C++ world. Required because
// (i) the tunables are first intercepted through the CLI but
Expand Down Expand Up @@ -236,11 +204,9 @@ static void Initialize(Local<Object> exports,
Local<Value> unused,
Local<Context> context) {
Environment* env = Environment::GetCurrent(context);
std::shared_ptr<PerIsolateOptions> options = env->isolate_data()->options();

env->SetMethod(exports, "triggerReport", TriggerReport);
env->SetMethod(exports, "getReport", GetReport);
env->SetMethod(exports, "onUnCaughtException", OnUncaughtException);
env->SetMethod(exports, "onUserSignal", OnUserSignal);
env->SetMethod(exports, "syncConfig", SyncConfig);
}

Expand Down
4 changes: 2 additions & 2 deletions test/common/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ function _validateContent(data) {

// Verify the format of the header section.
const header = report.header;
const headerFields = ['event', 'location', 'filename', 'dumpEventTime',
const headerFields = ['event', 'trigger', 'filename', 'dumpEventTime',
'dumpEventTimeStamp', 'processId', 'commandLine',
'nodejsVersion', 'wordSize', 'arch', 'platform',
'componentVersions', 'release', 'osName', 'osRelease',
'osVersion', 'osMachine', 'host', 'glibcVersionRuntime',
'glibcVersionCompiler'];
checkForUnknownFields(header, headerFields);
assert.strictEqual(typeof header.event, 'string');
assert.strictEqual(typeof header.location, 'string');
assert.strictEqual(typeof header.trigger, 'string');
assert(typeof header.filename === 'string' || header.filename === null);
assert.notStrictEqual(new Date(header.dumpEventTime).toString(),
'Invalid Date');
Expand Down