Skip to content
Open
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
63 changes: 63 additions & 0 deletions src/debug_utils.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "debug_utils-inl.h" // NOLINT(build/include)
#include "env-inl.h"
#include "node_internals.h"
#include "node_metadata.h"
#include "util.h"

#ifdef __POSIX__
Expand Down Expand Up @@ -155,6 +156,7 @@ class Win32SymbolDebuggingContext final : public NativeSymbolDebuggingContext {
Win32SymbolDebuggingContext() {
current_process_ = GetCurrentProcess();
USE(SymInitialize(current_process_, nullptr, true));
DetectMissingNodeSymbols();
}

~Win32SymbolDebuggingContext() override {
Expand Down Expand Up @@ -259,6 +261,19 @@ class Win32SymbolDebuggingContext final : public NativeSymbolDebuggingContext {

SymbolInfo LookupSymbol(void* address) override {
const DWORD64 dw_address = reinterpret_cast<DWORD64>(address);
// An export-table name is right only when the frame is inside an exported
// function, which can't be told apart here, so show the image offset.
// It can be resolved later against the matching .pdb.
// Unsigned wraparound makes this one comparison a check that the address
// is in [base, base + size), and it is false while size is 0.
if (dw_address - export_only_image_base_ < export_only_image_size_) {
std::ostringstream oss;
oss << export_only_image_name_ << "+0x" << std::hex
<< (dw_address - export_only_image_base_);
SymbolInfo ret;
ret.name = oss.str();
return ret;
}
Comment thread
addaleax marked this conversation as resolved.
Resolved
SymbolInfo ret = WrappedGetLine(dw_address);
std::tie(ret.name, ret.dis) = WrappedSymFromAddr(dw_address);
if (!ret.name.empty()) {
Expand All @@ -280,6 +295,19 @@ class Win32SymbolDebuggingContext final : public NativeSymbolDebuggingContext {
return CaptureStackBackTrace(0, count, frames, nullptr);
}

std::string MissingSymbolsHint() override {
if (export_only_image_size_ == 0) return {};
std::string hint = "Symbols for " + export_only_image_file_ +
" were not found, so its frames are shown as offsets. "
"Put the matching .pdb file next to it or on "
"_NT_SYMBOL_PATH to get function names.";
#ifdef NODE_HAS_RELEASE_URLS
hint += " For this release, the .pdb file is in " NODE_RELEASE_URLBASE
"v" NODE_VERSION_STRING "/win-" NODE_ARCH "/node_pdb.zip";
#endif // NODE_HAS_RELEASE_URLS
return hint;
}

Win32SymbolDebuggingContext(const Win32SymbolDebuggingContext&) = delete;
Win32SymbolDebuggingContext(Win32SymbolDebuggingContext&&) = delete;
Win32SymbolDebuggingContext operator=(const Win32SymbolDebuggingContext&)
Expand All @@ -288,7 +316,38 @@ class Win32SymbolDebuggingContext final : public NativeSymbolDebuggingContext {
= delete;

private:
// Records the image containing node's code (node.exe, or an embedder's DLL)
// when dbghelp built its symbols from the export table, which it does when
// no .pdb was found.
void DetectMissingNodeSymbols() {
HMODULE module;
if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
reinterpret_cast<LPCWSTR>(&DumpNativeBacktrace),
&module)) {
return;
}
IMAGEHLP_MODULE64 info{};
info.SizeOfStruct = sizeof(info);
if (!SymGetModuleInfo64(
current_process_, reinterpret_cast<DWORD64>(module), &info) ||
info.SymType != SymExport) {
return;
}
export_only_image_base_ = info.BaseOfImage;
export_only_image_size_ = info.ImageSize;
export_only_image_name_ = info.ModuleName;
const char* image = strrchr(info.ImageName, '\\');
export_only_image_file_ = image != nullptr ? image + 1 : info.ImageName;
}

HANDLE current_process_;
// Set only when the image containing node's code has export-table symbols
// alone.
DWORD64 export_only_image_base_ = 0;
DWORD export_only_image_size_ = 0;
std::string export_only_image_name_;
std::string export_only_image_file_;
};

std::unique_ptr<NativeSymbolDebuggingContext>
Expand Down Expand Up @@ -324,6 +383,10 @@ void DumpNativeBacktrace(FILE* fp) {
NativeSymbolDebuggingContext::SymbolInfo s = sym_ctx->LookupSymbol(frame);
fprintf(fp, "%2d: %p %s\n", i, frame, s.Display().c_str());
}
const std::string hint = sym_ctx->MissingSymbolsHint();
if (!hint.empty()) {
fprintf(fp, "\n%s\n", hint.c_str());
}
}

void DumpJavaScriptBacktrace(FILE* fp) {
Expand Down
3 changes: 3 additions & 0 deletions src/debug_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ class NativeSymbolDebuggingContext {
virtual SymbolInfo LookupSymbol(void* address) { return {}; }
virtual bool IsMapped(void* address) { return false; }
virtual int GetStackTrace(void** frames, int count) { return 0; }
// Returns advice for getting symbolized frames when node's own symbols
// could not be loaded, or an empty string when they were.
virtual std::string MissingSymbolsHint() { return {}; }

NativeSymbolDebuggingContext(const NativeSymbolDebuggingContext&) = delete;
NativeSymbolDebuggingContext(NativeSymbolDebuggingContext&&) = delete;
Expand Down
3 changes: 3 additions & 0 deletions test/abort/test-abort-backtrace.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ function getPrintedStackTrace(stderr) {
case 'native-stack':
if (line.includes('JavaScript stack trace')) {
state = 'js-stack';
} else if (line.startsWith('Symbols for ')) {
// Printed after the frames on Windows when node's .pdb is missing.
result.missingSymbolsHint = line;
} else {
result.nativeStack.push(line);
}
Expand Down
Loading