Here is a extended version of Álvaro's patch that adds an errbacktrace() function. You can do two things with this:
- Manually attach it to an ereport() call site that you want to debug.
- Set a configuration parameter like backtrace_function = 'int8in' to debug ereport()/elog() calls in a specific function.
There was also mention of settings that would automatically produce backtraces for PANICs etc. Those could surely be added if there is enough interest.
For the implementation, I support both backtrace() provided by the OS as well as using libunwind. The former seems to be supported by a number of platforms, including glibc, macOS, and FreeBSD, so maybe we don't need the libunwind suport. I haven't found any difference in quality in the backtraces between the two approaches, but surely that is highly dependent on the exact configuration.
I would welcome testing in all direction with this, to see how well it works in different circumstances.
Hi Peter,
This is certainly a very useful thing. Sadly, it doesn't seem to compile when trying to use libunwind.
I tried it in a Debian 9 machine with gcc 6.3.0 and debian says i installed libunwind8 (1.1)
utils/error/elog.o: En la función `set_backtrace': /home/jcasanov/Documentos/pgdg/projects/postgresql/src/backend/utils/error/elog.c:847: referencia a `_Ux86_64_getcontext' sin definir /home/jcasanov/Documentos/pgdg/projects/postgresql/src/backend/utils/error/elog.c:848: referencia a `_Ux86_64_init_local' sin definir /home/jcasanov/Documentos/pgdg/projects/postgresql/src/backend/utils/error/elog.c:850: referencia a `_Ux86_64_step' sin definir /home/jcasanov/Documentos/pgdg/projects/postgresql/src/backend/utils/error/elog.c:861: referencia a `_Ux86_64_get_reg' sin definir /home/jcasanov/Documentos/pgdg/projects/postgresql/src/backend/utils/error/elog.c:862: referencia a `_Ux86_64_get_proc_name' sin definir collect2: error: ld returned 1 exit status make[2]: *** [postgres] Error 1 make[1]: *** [all-backend-recurse] Error 2 make: *** [all-src-recurse] Error 2