PATCH: backtraces for error messages - Mailing list pgsql-hackers

From Craig Ringer
Subject PATCH: backtraces for error messages
Date
Msg-id CAMsr+YGL+yfWE=JvbUbnpWtrRZNey7hJ07+zT4bYJdVp4Szdrg@mail.gmail.com
Whole thread Raw
Responses Re: PATCH: backtraces for error messages
Re: PATCH: backtraces for error messages
Re: PATCH: backtraces for error messages
List pgsql-hackers
Hi folks

I recently needed a way to get backtraces from errors in a convenient, non-interactive and indescriminate way. The attached patch is the result. It teaches Pg to use libunwind to self-backtrace in elog/ereport. 

Anyone think this is useful/interesting/worth pursuing?

(Patch is currently against pg10, so this is a PoC only).

As written it emits a backtrace when log_error_verbosity=verbose or, unconditionally, on PANIC. A bt can be hidden by errhidestack() but is otherwise shown. That's ridiculously, excessively spammy, so it's not viable for core as-is. Before playing with it too much I thought I'd ask for ideas on if anyone thinks it's useful, and if so, how it'd work best.

My goal is to allow capture of extra diagnostic info from key locations in production without needing to attach gdb. It's imperfect since sometimes there's no convenient message, and other times you can't afford to set up logging with enough detail. So this would be most useful when combined with one of the occasionally discussed patches to allow for selective logging verbosity on a module- or file- level. But I think it's still handy without that. 

I briefly looked into Windows too. Roughly the same approach could be used to plug in dbghelp.dll support for Windows self-backtracing, it's just rather uglier; see https://jpassing.com/2008/03/12/walking-the-stack-of-the-current-thread/ .

BTW, Álvaro posted a simpler patch at https://www.postgresql.org/message-id/20180410213203.nl645o5vj5ap66sl@alvherre.pgsql. It uses backtrace() from glibc, and requires each site you want to bt to be annotated explicitly. I forgot about backtrace() completely when I wrote mine, and I prefer the control libunwind gives me anyway, but the reduced dependency would be nice. Especially since backtrace() is in FreeBSD too.

--
 Craig Ringer                   http://www.2ndQuadrant.com/
 PostgreSQL Development, 24x7 Support, Training & Services
Attachment

pgsql-hackers by date:

Previous
From: Euler Taveira
Date:
Subject: Re: New function pg_stat_statements_reset_query() to reset statisticsof a specific query
Next
From: Bruce Momjian
Date:
Subject: Re: add default parallel query to v10 release notes? (Re: [PERFORM]performance drop after upgrade (9.6 > 10))