diff --git a/src/debug_utils.cc b/src/debug_utils.cc index 2166e54aba3b..75beaa3a9288 100644 --- a/src/debug_utils.cc +++ b/src/debug_utils.cc @@ -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__ @@ -155,6 +156,7 @@ class Win32SymbolDebuggingContext final : public NativeSymbolDebuggingContext { Win32SymbolDebuggingContext() { current_process_ = GetCurrentProcess(); USE(SymInitialize(current_process_, nullptr, true)); + DetectMissingNodeSymbols(); } ~Win32SymbolDebuggingContext() override { @@ -259,6 +261,19 @@ class Win32SymbolDebuggingContext final : public NativeSymbolDebuggingContext { SymbolInfo LookupSymbol(void* address) override { const DWORD64 dw_address = reinterpret_cast(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; + } SymbolInfo ret = WrappedGetLine(dw_address); std::tie(ret.name, ret.dis) = WrappedSymFromAddr(dw_address); if (!ret.name.empty()) { @@ -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&) @@ -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(&DumpNativeBacktrace), + &module)) { + return; + } + IMAGEHLP_MODULE64 info{}; + info.SizeOfStruct = sizeof(info); + if (!SymGetModuleInfo64( + current_process_, reinterpret_cast(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 @@ -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) { diff --git a/src/debug_utils.h b/src/debug_utils.h index f381e48547c1..4ba116ca8ff8 100644 --- a/src/debug_utils.h +++ b/src/debug_utils.h @@ -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; diff --git a/test/abort/test-abort-backtrace.js b/test/abort/test-abort-backtrace.js index 455bbf2361cf..f0ab7aec8a33 100644 --- a/test/abort/test-abort-backtrace.js +++ b/test/abort/test-abort-backtrace.js @@ -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); }