pgsql: Add backtrace support for Windows using DbgHelp API - Mailing list pgsql-committers

From Álvaro Herrera
Subject pgsql: Add backtrace support for Windows using DbgHelp API
Date
Msg-id E1vuvTg-000zDz-0I@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
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 <dbryan.green@gmail.com>
Reviewed-by: Euler Taveira <euler@eulerto.com>
Reviewed-by: Jakub Wartak <jakub.wartak@enterprisedb.com>
Reviewed-by: Greg Burd <greg@burd.me>
Discussion: https://postgr.es/m/a692c0fe-caca-4c08-9c5d-debfd0ef2504@gmail.com

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(-)


pgsql-committers by date:

Previous
From: Peter Eisentraut
Date:
Subject: pgsql: doc: Add link targets to CREATE/ALTER FOREIGN TABLE reference pa
Next
From: noreply@postgresql.org
Date:
Subject: pgsql: Tag refs/tags/REL_18_3 was created