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

bootstrap: print stack trace during environment creation failure #46533

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/api/embed_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ using v8::Maybe;
using v8::Nothing;
using v8::SealHandleScope;
using v8::SnapshotCreator;
using v8::TryCatch;

namespace node {

Expand Down Expand Up @@ -129,12 +130,21 @@ CommonEnvironmentSetup::CommonEnvironmentSetup(
{
Locker locker(isolate);
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);

TryCatch bootstrapCatch(isolate);
auto print_Exception = OnScopeLeave([&]() {
if (bootstrapCatch.HasCaught()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this check CanContinue() also?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the code below should return early when there is an exception, so it's not necessary to check here because what we do should not result in JS execution anyway, just communicating the stack trace back to the caller.

errors->push_back(FormatCaughtException(
isolate, isolate->GetCurrentContext(), bootstrapCatch));
}
});

impl_->isolate_data.reset(CreateIsolateData(
isolate, loop, platform, impl_->allocator.get(), snapshot_data));
impl_->isolate_data->options()->build_snapshot =
impl_->snapshot_creator.has_value();

HandleScope handle_scope(isolate);
if (snapshot_data) {
impl_->env.reset(make_env(this));
if (impl_->env) {
Expand Down
79 changes: 55 additions & 24 deletions src/node_errors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ static std::string GetErrorSource(Isolate* isolate,
return buf + std::string(underline_buf, off);
}

void PrintStackTrace(Isolate* isolate, Local<StackTrace> stack) {
static std::string FormatStackTrace(Isolate* isolate, Local<StackTrace> stack) {
std::string result;
for (int i = 0; i < stack->GetFrameCount(); i++) {
Local<StackFrame> stack_frame = stack->GetFrame(isolate, i);
node::Utf8Value fn_name_s(isolate, stack_frame->GetFunctionName());
Expand All @@ -195,53 +196,82 @@ void PrintStackTrace(Isolate* isolate, Local<StackTrace> stack) {

if (stack_frame->IsEval()) {
if (stack_frame->GetScriptId() == Message::kNoScriptIdInfo) {
FPrintF(stderr, " at [eval]:%i:%i\n", line_number, column);
result += SPrintF(" at [eval]:%i:%i\n", line_number, column);
} else {
FPrintF(stderr,
" at [eval] (%s:%i:%i)\n",
*script_name,
line_number,
column);
std::vector<char> buf(script_name.length() + 64);
snprintf(buf.data(),
buf.size(),
" at [eval] (%s:%i:%i)\n",
*script_name,
line_number,
column);
result += std::string(buf.data());
}
break;
}

if (fn_name_s.length() == 0) {
FPrintF(stderr, " at %s:%i:%i\n", script_name, line_number, column);
std::vector<char> buf(script_name.length() + 64);
snprintf(buf.data(),
buf.size(),
" at %s:%i:%i\n",
*script_name,
line_number,
column);
result += std::string(buf.data());
} else {
FPrintF(stderr,
" at %s (%s:%i:%i)\n",
fn_name_s,
script_name,
line_number,
column);
std::vector<char> buf(fn_name_s.length() + script_name.length() + 64);
snprintf(buf.data(),
buf.size(),
" at %s (%s:%i:%i)\n",
*fn_name_s,
*script_name,
line_number,
column);
result += std::string(buf.data());
}
}
return result;
}

static void PrintToStderrAndFlush(const std::string& str) {
FPrintF(stderr, "%s\n", str);
fflush(stderr);
}

void PrintException(Isolate* isolate,
Local<Context> context,
Local<Value> err,
Local<Message> message) {
void PrintStackTrace(Isolate* isolate, Local<StackTrace> stack) {
PrintToStderrAndFlush(FormatStackTrace(isolate, stack));
}

std::string FormatCaughtException(Isolate* isolate,
Local<Context> context,
Local<Value> err,
Local<Message> message) {
node::Utf8Value reason(isolate,
err->ToDetailString(context)
.FromMaybe(Local<String>()));
bool added_exception_line = false;
std::string source =
GetErrorSource(isolate, context, message, &added_exception_line);
FPrintF(stderr, "%s\n", source);
FPrintF(stderr, "%s\n", reason);
std::string result = source + '\n' + reason.ToString() + '\n';

Local<v8::StackTrace> stack = message->GetStackTrace();
if (!stack.IsEmpty()) PrintStackTrace(isolate, stack);
if (!stack.IsEmpty()) result += FormatStackTrace(isolate, stack);
return result;
}

std::string FormatCaughtException(Isolate* isolate,
Local<Context> context,
const v8::TryCatch& try_catch) {
CHECK(try_catch.HasCaught());
return FormatCaughtException(
isolate, context, try_catch.Exception(), try_catch.Message());
}

void PrintCaughtException(Isolate* isolate,
Local<Context> context,
const v8::TryCatch& try_catch) {
CHECK(try_catch.HasCaught());
PrintException(isolate, context, try_catch.Exception(), try_catch.Message());
PrintToStderrAndFlush(FormatCaughtException(isolate, context, try_catch));
}

void AppendExceptionLine(Environment* env,
Expand Down Expand Up @@ -1087,7 +1117,8 @@ void TriggerUncaughtException(Isolate* isolate,
// error is supposed to be thrown at this point.
// Since we don't have access to Environment here, there is not
// much we can do, so we just print whatever is useful and crash.
PrintException(isolate, context, error, message);
PrintToStderrAndFlush(
FormatCaughtException(isolate, context, error, message));
Abort();
}

Expand Down
3 changes: 3 additions & 0 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ void PrintStackTrace(v8::Isolate* isolate, v8::Local<v8::StackTrace> stack);
void PrintCaughtException(v8::Isolate* isolate,
v8::Local<v8::Context> context,
const v8::TryCatch& try_catch);
std::string FormatCaughtException(v8::Isolate* isolate,
v8::Local<v8::Context> context,
const v8::TryCatch& try_catch);

void ResetStdio(); // Safe to call more than once and from signal handlers.
#ifdef __POSIX__
Expand Down