diff options
author | Noel Grandin <noel@peralex.com> | 2022-03-24 12:46:44 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2022-05-06 11:04:33 +0200 |
commit | 92f93f300371d2011b53d48d3491870b24cff0b4 (patch) | |
tree | bdf3a3951f5701749d32c95830796bb219c15cf5 /sal | |
parent | 7aa0730d6cdf1b2e74e1d8c9adb666cfb51d8fc6 (diff) |
output error code when SymFromAddr fails
instead of random data
Change-Id: Iba020ea3a2386e3ad940cc85aac18047c3773967
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/132066
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'sal')
-rw-r--r-- | sal/cppunittester/cppunittester.cxx | 112 |
1 files changed, 43 insertions, 69 deletions
diff --git a/sal/cppunittester/cppunittester.cxx b/sal/cppunittester/cppunittester.cxx index fec62c261260..4a1c050a4662 100644 --- a/sal/cppunittester/cppunittester.cxx +++ b/sal/cppunittester/cppunittester.cxx @@ -488,116 +488,90 @@ static bool main2() #if defined(_WIN32) && defined(_DEBUG) //Prints stack trace based on exception context record -static void printStack( CONTEXT* ctx ) +static void printStack( PCONTEXT ctx ) { constexpr int MaxNameLen = 256; - bool result; - HANDLE process; - HANDLE thread; - HMODULE hModule; -#ifdef _M_AMD64 - STACKFRAME64 stack; -#else - STACKFRAME stack; -#endif - DWORD64 displacement; - DWORD disp; - char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)]; - char module[MaxNameLen]; - PSYMBOL_INFO pSymbol = reinterpret_cast<PSYMBOL_INFO>(buffer); + HANDLE process = GetCurrentProcess(); + HANDLE thread = GetCurrentThread(); -#ifdef _M_AMD64 - memset( &stack, 0, sizeof( STACKFRAME64 ) ); -#else - memset( &stack, 0, sizeof( STACKFRAME ) ); -#endif - - process = GetCurrentProcess(); - thread = GetCurrentThread(); - displacement = 0; -#if !defined(_M_AMD64) - stack.AddrPC.Offset = (*ctx).Eip; + STACKFRAME64 stack {}; stack.AddrPC.Mode = AddrModeFlat; - stack.AddrStack.Offset = (*ctx).Esp; stack.AddrStack.Mode = AddrModeFlat; - stack.AddrFrame.Offset = (*ctx).Ebp; stack.AddrFrame.Mode = AddrModeFlat; +#ifdef _M_AMD64 + stack.AddrPC.Offset = ctx->Rip; + stack.AddrStack.Offset = ctx->Rsp; + stack.AddrFrame.Offset = ctx->Rsp; +#else + stack.AddrPC.Offset = ctx->Eip; + stack.AddrStack.Offset = ctx->Esp; + stack.AddrFrame.Offset = ctx->Ebp; #endif + DWORD symOptions = SymGetOptions(); + symOptions |= SYMOPT_LOAD_LINES; + symOptions |= SYMOPT_FAIL_CRITICAL_ERRORS; + symOptions = SymSetOptions(symOptions); + SymInitialize( process, nullptr, TRUE ); //load symbols -#ifdef _M_AMD64 - std::unique_ptr<IMAGEHLP_LINE64> line(new IMAGEHLP_LINE64); - line->SizeOfStruct = sizeof(IMAGEHLP_LINE64); -#else - std::unique_ptr<IMAGEHLP_LINE> line(new IMAGEHLP_LINE); - line->SizeOfStruct = sizeof(IMAGEHLP_LINE); -#endif + IMAGEHLP_LINE64 line{}; + line.SizeOfStruct = sizeof(line); + + char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)]; + PSYMBOL_INFO pSymbol = reinterpret_cast<PSYMBOL_INFO>(buffer); - for( ; ; ) + for (;;) { //get next call from stack -#ifdef _M_AMD64 - result = StackWalk64 + bool result = StackWalk64 ( +#ifdef _M_AMD64 IMAGE_FILE_MACHINE_AMD64, - process, - thread, - &stack, - ctx, - nullptr, - SymFunctionTableAccess64, - SymGetModuleBase64, - nullptr - ); #else - result = StackWalk - ( IMAGE_FILE_MACHINE_I386, +#endif process, thread, &stack, ctx, nullptr, - SymFunctionTableAccess, - SymGetModuleBase, + SymFunctionTableAccess64, + SymGetModuleBase64, nullptr ); -#endif if( !result ) break; //get symbol name for address pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO); - pSymbol->MaxNameLen = MAX_SYM_NAME; -#ifdef _M_AMD64 - SymFromAddr(process, static_cast< ULONG64 >(stack.AddrPC.Offset), &displacement, pSymbol); -#else - SymFromAddr(process, static_cast< ULONG >(stack.AddrPC.Offset), &displacement, pSymbol); -#endif + pSymbol->MaxNameLen = MAX_SYM_NAME + 1; + if (SymFromAddr(process, stack.AddrPC.Offset, nullptr, pSymbol)) + printf("\tat %s", pSymbol->Name); + else + printf("\tat unknown (Error in SymFromAddr=%#08x)", GetLastError()); + + DWORD disp; //try to get line -#ifdef _M_AMD64 - if (SymGetLineFromAddr64(process, stack.AddrPC.Offset, &disp, line.get())) -#else - if (SymGetLineFromAddr(process, stack.AddrPC.Offset, &disp, line.get())) -#endif + if (SymGetLineFromAddr64(process, stack.AddrPC.Offset, &disp, &line)) { - printf("\tat %s in %s: line: %lu: address: 0x%0I64X\n", pSymbol->Name, line->FileName, line->LineNumber, pSymbol->Address); + printf(" in %s: line: %lu:\n", line.FileName, line.LineNumber); } else { //failed to get line - printf("\tat %s, address 0x%0I64X.\n", pSymbol->Name, pSymbol->Address); - hModule = nullptr; - lstrcpyA(module,""); + printf(", address 0x%0I64X", stack.AddrPC.Offset); + HMODULE hModule = nullptr; GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, reinterpret_cast<LPCTSTR>(stack.AddrPC.Offset), &hModule); + char sModule[256]; //at least print module name - if(hModule != nullptr)GetModuleFileNameA(hModule,module,MaxNameLen); + if (hModule != nullptr) + GetModuleFileNameA(hModule, sModule, std::size(sModule)); - printf ("in %s\n",module); + printf (" in %s\n", sModule); } } } |