Add backtrace support for Windows using DbgHelp API

Previously, backtrace generation on Windows would return an "unsupported"
message.  With this commit, we rely on CaptureStackBackTrace() to capture
the call stack and the DbgHelp API (SymFromAddrW, SymGetLineFromAddrW64)
for symbol resolution.

Symbol handler initialization (SymInitialize) is performed once per
process and cached.  If initialization fails, the report for it is
returned as the backtrace output.  The symbol handler is cleaned up via
on_proc_exit() to release DbgHelp resources.

The implementation provides symbol names, offsets, and addresses.  When
PDB files are available, it also includes source file names and line
numbers.  Symbol names and file paths are converted from UTF-16 to the
database encoding using wchar2char(), which properly handles both UTF-8
and non-UTF-8 databases on Windows.  When symbol information is
unavailable or encoding conversion fails, it falls back to displaying raw
addresses.

The implementation uses the explicit UTF16 versions of the DbgHelp
functions (SYMBOL_INFOW, SymFromAddrW, IMAGEHLP_LINEW64,
SymGetLineFromAddrW64) rather than the generic versions.  This allows us
to rely on predictable encoding conversion, rather than using the
haphazard ANSI codepage that we'd get otherwise.

DbgHelp is apparently available on all Windows platforms we support, so
there are no version number checks.

Author: Bryan Green <[email protected]>
Reviewed-by: Euler Taveira <[email protected]>
Reviewed-by: Jakub Wartak <[email protected]>
Reviewed-by: Greg Burd <[email protected]>
Discussion: https://postgr.es/m/[email protected]

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/65707ed9afc924e0eec19a706b24af5bae1f9574

Modified Files
--------------
src/backend/meson.build        |   6 ++
src/backend/utils/error/elog.c | 181 ++++++++++++++++++++++++++++++++++++++++-
2 files changed, 184 insertions(+), 3 deletions(-)

Reply via email to