]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Ad-hoc backtrace logging with isc_backtrace_log()
authorTony Finch <fanf@isc.org>
Fri, 3 Mar 2023 12:01:03 +0000 (12:01 +0000)
committerTony Finch <fanf@isc.org>
Wed, 29 Mar 2023 10:47:53 +0000 (10:47 +0000)
It's sometimes helpful to get a quick idea of the call stack when
debugging. This change factors out the backtrace logging from named's
fatal error handler so that it's easy to use in other places too.

bin/named/main.c
lib/isc/assertions.c
lib/isc/backtrace.c
lib/isc/include/isc/backtrace.h

index 752745d7fe4cd918558bac52cf1ad12bf228285b..9ae8832a40da96cc294491d6daf7af6860ef2cea 100644 (file)
  */
 /* #include "xxdb.h" */
 
-/*
- * The maximum number of stack frames to dump on assertion failure.
- */
-#ifndef BACKTRACE_MAXFRAME
-#define BACKTRACE_MAXFRAME 128
-#endif /* ifndef BACKTRACE_MAXFRAME */
-
 extern unsigned int dns_zone_mkey_hour;
 extern unsigned int dns_zone_mkey_day;
 extern unsigned int dns_zone_mkey_month;
@@ -189,9 +182,6 @@ assertion_failed(const char *file, int line, isc_assertiontype_t type,
 static void
 assertion_failed(const char *file, int line, isc_assertiontype_t type,
                 const char *cond) {
-       void *tracebuf[BACKTRACE_MAXFRAME];
-       int nframes;
-
        /*
         * Handle assertion failures.
         */
@@ -203,24 +193,12 @@ assertion_failed(const char *file, int line, isc_assertiontype_t type,
                 */
                isc_assertion_setcallback(NULL);
 
-               nframes = isc_backtrace(tracebuf, BACKTRACE_MAXFRAME);
                isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
                              NAMED_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
-                             "%s:%d: %s(%s) failed%s", file, line,
-                             isc_assertion_typetotext(type), cond,
-                             (nframes > 0) ? ", back trace" : "");
-               if (nframes > 0) {
-                       char **strs = isc_backtrace_symbols(tracebuf, nframes);
-                       if (strs != NULL) {
-                               for (int i = 0; i < nframes; i++) {
-                                       isc_log_write(named_g_lctx,
-                                                     NAMED_LOGCATEGORY_GENERAL,
-                                                     NAMED_LOGMODULE_MAIN,
-                                                     ISC_LOG_CRITICAL, "%s",
-                                                     strs[i]);
-                               }
-                       }
-               }
+                             "%s:%d: %s(%s) failed", file, line,
+                             isc_assertion_typetotext(type), cond);
+               isc_backtrace_log(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
+                                 NAMED_LOGMODULE_MAIN, ISC_LOG_CRITICAL);
                isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
                              NAMED_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
                              "exiting (due to assertion failure)");
index a5cc0a2ac87ba3db46045fe68d700ba37d61f3cf..3dfccdcb07b08e31f35fb8734415a5ec59623cfa 100644 (file)
@@ -95,8 +95,8 @@ isc_assertion_typetotext(isc_assertiontype_t type) {
 static void
 default_callback(const char *file, int line, isc_assertiontype_t type,
                 const char *cond) {
-       void *tracebuf[BACKTRACE_MAXFRAME];
-       int nframes = isc_backtrace(tracebuf, BACKTRACE_MAXFRAME);
+       void *tracebuf[ISC_BACKTRACE_MAXFRAME];
+       int nframes = isc_backtrace(tracebuf, ISC_BACKTRACE_MAXFRAME);
 
        fprintf(stderr, "%s:%d: %s(%s) failed%s\n", file, line,
                isc_assertion_typetotext(type), cond,
index fc15acf3eb305bbc5fd4f87be88409cb698afa4c..23369417fcc1558d39149aa7b32ca4adec43370e 100644 (file)
@@ -20,6 +20,7 @@
 #endif /* HAVE_BACKTRACE_SYMBOLS */
 
 #include <isc/backtrace.h>
+#include <isc/log.h>
 #include <isc/result.h>
 #include <isc/util.h>
 
@@ -60,6 +61,26 @@ isc_backtrace_symbols_fd(void *const *buffer, int size, int fd) {
        backtrace_symbols_fd(buffer, size, fd);
 }
 
+void
+isc_backtrace_log(isc_log_t *lctx, isc_logcategory_t *category,
+                 isc_logmodule_t *module, int level) {
+       void *tracebuf[ISC_BACKTRACE_MAXFRAME];
+       int nframes;
+       char **strs;
+
+       nframes = isc_backtrace(tracebuf, ISC_BACKTRACE_MAXFRAME);
+       if (nframes <= 0) {
+               return;
+       }
+       strs = isc_backtrace_symbols(tracebuf, nframes);
+       if (strs == NULL) {
+               return;
+       }
+       for (int i = 0; i < nframes; i++) {
+               isc_log_write(lctx, category, module, level, "%s", strs[i]);
+       }
+}
+
 #else /* HAVE_BACKTRACE_SYMBOLS */
 
 int
@@ -85,4 +106,13 @@ isc_backtrace_symbols_fd(void *const *buffer, int size, int fd) {
        UNUSED(fd);
 }
 
+void
+isc_backtrace_log(isc_log_t *lctx, isc_logcategory_t *category,
+                 isc_logmodule_t *module, int level) {
+       UNUSED(lctx);
+       UNUSED(category);
+       UNUSED(module);
+       UNUSED(level);
+}
+
 #endif /* HAVE_BACKTRACE_SYMBOLS */
index f818c3b54ab5546fb3ce0a3a631f918fcaf16c14..c8fa3242b00efd524425562765ce9bb7549734f1 100644 (file)
  ***/
 #include <isc/types.h>
 
+/*
+ * The maximum number of stack frames to dump on assertion failure.
+ */
+#ifndef ISC_BACKTRACE_MAXFRAME
+#define ISC_BACKTRACE_MAXFRAME 128
+#endif /* ifndef ISC_BACKTRACE_MAXFRAME */
+
 /***
  *** Functions
  ***/
@@ -98,4 +105,11 @@ isc_backtrace_symbols_fd(void *const *buffer, int size, int fd);
  *\li  See platform NOTES for backtrace_symbols_fd for caveats
  */
 
+void
+isc_backtrace_log(isc_log_t *lctx, isc_logcategory_t *category,
+                 isc_logmodule_t *module, int level);
+/*
+ * Write a backtrace to the log.
+ */
+
 ISC_LANG_ENDDECLS