]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Include the function name when reporting unexpected errors
authorTony Finch <dot@dotat.at>
Fri, 14 Oct 2022 16:37:47 +0000 (17:37 +0100)
committerTony Finch <dot@dotat.at>
Mon, 17 Oct 2022 15:00:27 +0000 (16:00 +0100)
I.e. print the name of the function in BIND that called the system
function that returned an error. Since it was useful for pthreads
code, it seems worthwhile doing so everywhere.

(cherry picked from commit 26ed03a61e0cb6359a985784ffad93630cb2aeff)

bin/named/main.c
bin/tests/system/dyndb/driver/db.c
bin/tests/system/dyndb/driver/log.h
bin/tests/system/dyndb/driver/syncptr.c
lib/isc/error.c
lib/isc/include/isc/error.h
lib/isc/include/isc/util.h
lib/isc/mem.c

index 4fdfa98fa3dc0f51426f36f61135a43f650eaeb1..c496bfb58774c4c19ba1f5176014a64c9de98873 100644 (file)
@@ -240,12 +240,12 @@ assertion_failed(const char *file, int line, isc_assertiontype_t type,
 }
 
 noreturn static void
-library_fatal_error(const char *file, int line, const char *format,
-                   va_list args) ISC_FORMAT_PRINTF(3, 0);
+library_fatal_error(const char *file, int line, const char *func,
+                   const char *format, va_list args) ISC_FORMAT_PRINTF(3, 0);
 
 static void
-library_fatal_error(const char *file, int line, const char *format,
-                   va_list args) {
+library_fatal_error(const char *file, int line, const char *func,
+                   const char *format, va_list args) {
        /*
         * Handle isc_error_fatal() calls from our libraries.
         */
@@ -259,7 +259,7 @@ library_fatal_error(const char *file, int line, const char *format,
 
                isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
                              NAMED_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
-                             "%s:%d: fatal error:", file, line);
+                             "%s:%d:%s(): fatal error: ", file, line, func);
                isc_log_vwrite(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
                               NAMED_LOGMODULE_MAIN, ISC_LOG_CRITICAL, format,
                               args);
@@ -267,7 +267,7 @@ library_fatal_error(const char *file, int line, const char *format,
                              NAMED_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
                              "exiting (due to fatal error in library)");
        } else {
-               fprintf(stderr, "%s:%d: fatal error: ", file, line);
+               fprintf(stderr, "%s:%d:%s(): fatal error: ", file, line, func);
                vfprintf(stderr, format, args);
                fprintf(stderr, "\n");
                fflush(stderr);
@@ -280,12 +280,13 @@ library_fatal_error(const char *file, int line, const char *format,
 }
 
 static void
-library_unexpected_error(const char *file, int line, const char *format,
-                        va_list args) ISC_FORMAT_PRINTF(3, 0);
+library_unexpected_error(const char *file, int line, const char *func,
+                        const char *format, va_list args)
+       ISC_FORMAT_PRINTF(3, 0);
 
 static void
-library_unexpected_error(const char *file, int line, const char *format,
-                        va_list args) {
+library_unexpected_error(const char *file, int line, const char *func,
+                        const char *format, va_list args) {
        /*
         * Handle isc_error_unexpected() calls from our libraries.
         */
@@ -293,12 +294,13 @@ library_unexpected_error(const char *file, int line, const char *format,
        if (named_g_lctx != NULL) {
                isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
                              NAMED_LOGMODULE_MAIN, ISC_LOG_ERROR,
-                             "%s:%d: unexpected error:", file, line);
+                             "%s:%d:%s(): unexpected error: ", file, line,
+                             func);
                isc_log_vwrite(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
                               NAMED_LOGMODULE_MAIN, ISC_LOG_ERROR, format,
                               args);
        } else {
-               fprintf(stderr, "%s:%d: fatal error: ", file, line);
+               fprintf(stderr, "%s:%d:%s(): fatal error: ", file, line, func);
                vfprintf(stderr, format, args);
                fprintf(stderr, "\n");
                fflush(stderr);
index 05485bd6314503030703b20cefd3aaedae6b61e0..a2bdb6916b8fa21c020a345e1686ad59d26f71a1 100644 (file)
@@ -133,7 +133,7 @@ beginload(dns_db_t *db, dns_rdatacallbacks_t *callbacks) {
        UNUSED(db);
        UNUSED(callbacks);
 
-       fatal_error("current implementation should never call beginload()");
+       FATAL_ERROR("current implementation should never call beginload()");
 
        /* Not reached */
        return (ISC_R_SUCCESS);
@@ -149,7 +149,7 @@ endload(dns_db_t *db, dns_rdatacallbacks_t *callbacks) {
        UNUSED(db);
        UNUSED(callbacks);
 
-       fatal_error("current implementation should never call endload()");
+       FATAL_ERROR("current implementation should never call endload()");
 
        /* Not reached */
        return (ISC_R_SUCCESS);
@@ -163,7 +163,7 @@ dump(dns_db_t *db, dns_dbversion_t *version, const char *filename,
        UNUSED(filename);
        UNUSED(masterformat);
 
-       fatal_error("current implementation should never call dump()");
+       FATAL_ERROR("current implementation should never call dump()");
 
        /* Not reached */
        return (ISC_R_SUCCESS);
index 2cb968bf81a6770399a6e06342e9ab1767693488..375db2b46ad48bf4ab5573fd14e0952608c53bf4 100644 (file)
@@ -34,8 +34,6 @@
 
 #include <dns/log.h>
 
-#define fatal_error(...) isc_error_fatal(__FILE__, __LINE__, __VA_ARGS__)
-
 #define log_error_r(fmt, ...) \
        log_error(fmt ": %s", ##__VA_ARGS__, isc_result_totext(result))
 
index 81d98e5d43c58b86128e3be071114f98fc3d59a9..5124df32b4d235c7978effc05fb154605e04c331 100644 (file)
@@ -157,7 +157,7 @@ syncptr_find_zone(sample_instance_t *inst, dns_rdata_t *rdata, dns_name_t *name,
                break;
 
        default:
-               fatal_error("unsupported address type 0x%x", rdata->type);
+               FATAL_ERROR("unsupported address type 0x%x", rdata->type);
                break;
        }
 
index 336be8432ee6f6621a2448d3b52fe8f2d24705ee..051a6c48aae954d28191f35661a73f736ed0d50d 100644 (file)
 
 /*% Default unexpected callback. */
 static void
-default_unexpected_callback(const char *, int, const char *, va_list)
-       ISC_FORMAT_PRINTF(3, 0);
+default_unexpected_callback(const char *, int, const char *, const char *,
+                           va_list) ISC_FORMAT_PRINTF(4, 0);
 
 /*% Default fatal callback. */
 static void
-default_fatal_callback(const char *, int, const char *, va_list)
+default_fatal_callback(const char *, int, const char *, const char *, va_list)
        ISC_FORMAT_PRINTF(3, 0);
 
 /*% unexpected_callback */
@@ -52,42 +52,39 @@ isc_error_setfatal(isc_errorcallback_t cb) {
 }
 
 void
-isc_error_unexpected(const char *file, int line, const char *format, ...) {
+isc_error_unexpected(const char *file, int line, const char *func,
+                    const char *format, ...) {
        va_list args;
 
        va_start(args, format);
-       (unexpected_callback)(file, line, format, args);
+       (unexpected_callback)(file, line, func, format, args);
        va_end(args);
 }
 
 void
-isc_error_fatal(const char *file, int line, const char *format, ...) {
+isc_error_fatal(const char *file, int line, const char *func,
+               const char *format, ...) {
        va_list args;
 
        va_start(args, format);
-       (fatal_callback)(file, line, format, args);
+       (fatal_callback)(file, line, func, format, args);
        va_end(args);
        abort();
 }
 
-void
-isc_error_runtimecheck(const char *file, int line, const char *expression) {
-       isc_error_fatal(file, line, "RUNTIME_CHECK(%s) failed", expression);
-}
-
 static void
-default_unexpected_callback(const char *file, int line, const char *format,
-                           va_list args) {
-       fprintf(stderr, "%s:%d: ", file, line);
+default_unexpected_callback(const char *file, int line, const char *func,
+                           const char *format, va_list args) {
+       fprintf(stderr, "%s:%d:%s(): ", file, line, func);
        vfprintf(stderr, format, args);
        fprintf(stderr, "\n");
        fflush(stderr);
 }
 
 static void
-default_fatal_callback(const char *file, int line, const char *format,
-                      va_list args) {
-       fprintf(stderr, "%s:%d: fatal error: ", file, line);
+default_fatal_callback(const char *file, int line, const char *func,
+                      const char *format, va_list args) {
+       fprintf(stderr, "%s:%d:%s(): fatal error: ", file, line, func);
        vfprintf(stderr, format, args);
        fprintf(stderr, "\n");
        fflush(stderr);
index 96552bcb78d33070dffdfb745a024df3439867f3..f4f668203f3a024b7e6f379cdd58ca85dd0233dd 100644 (file)
@@ -23,7 +23,8 @@
 
 ISC_LANG_BEGINDECLS
 
-typedef void (*isc_errorcallback_t)(const char *, int, const char *, va_list);
+typedef void (*isc_errorcallback_t)(const char *, int, const char *,
+                                   const char *, va_list);
 
 /*% set unexpected error */
 void isc_error_setunexpected(isc_errorcallback_t);
@@ -33,15 +34,12 @@ void isc_error_setfatal(isc_errorcallback_t);
 
 /*% unexpected error */
 void
-isc_error_unexpected(const char *, int, const char *, ...)
-       ISC_FORMAT_PRINTF(3, 4);
+isc_error_unexpected(const char *, int, const char *, const char *, ...)
+       ISC_FORMAT_PRINTF(4, 5);
 
 /*% fatal error */
 noreturn void
-isc_error_fatal(const char *, int, const char *, ...) ISC_FORMAT_PRINTF(3, 4);
-
-/*% runtimecheck error */
-noreturn void
-isc_error_runtimecheck(const char *, int, const char *);
+isc_error_fatal(const char *, int, const char *, const char *, ...)
+       ISC_FORMAT_PRINTF(4, 5);
 
 ISC_LANG_ENDDECLS
index 66b82eb1ad06dd1c9f12fceaf745368f639afffb..6b8e581450420047c402e240199fc2f569fd032e 100644 (file)
@@ -313,16 +313,17 @@ mock_assert(const int result, const char *const expression,
 #include <isc/strerr.h> /* for ISC_STRERRORSIZE */
 
 #define UNEXPECTED_ERROR(...) \
-       isc_error_unexpected(__FILE__, __LINE__, __VA_ARGS__)
+       isc_error_unexpected(__FILE__, __LINE__, __func__, __VA_ARGS__)
 
-#define FATAL_ERROR(...) isc_error_fatal(__FILE__, __LINE__, __VA_ARGS__)
+#define FATAL_ERROR(...) \
+       isc_error_fatal(__FILE__, __LINE__, __func__, __VA_ARGS__)
 
-#define REPORT_SYSERROR(report, err, fmt, ...)                             \
-       {                                                                  \
-               char _strerr[ISC_STRERRORSIZE];                            \
-               strerror_r(err, _strerr, sizeof(_strerr));                 \
-               report(__FILE__, __LINE__, fmt ": %s (%d)", ##__VA_ARGS__, \
-                      _strerr, err);                                      \
+#define REPORT_SYSERROR(report, err, fmt, ...)                        \
+       {                                                             \
+               char strerr[ISC_STRERRORSIZE];                        \
+               strerror_r(err, strerr, sizeof(strerr));              \
+               report(__FILE__, __LINE__, __func__, fmt ": %s (%d)", \
+                      ##__VA_ARGS__, strerr, err);                   \
        }
 
 #define UNEXPECTED_SYSERROR(err, ...) \
@@ -340,7 +341,7 @@ mock_assert(const int result, const char *const expression,
 #else /* UNIT_TESTING */
 
 #define RUNTIME_CHECK(cond) \
-       ((cond) ? (void)0 : isc_error_runtimecheck(__FILE__, __LINE__, #cond))
+       ((cond) ? (void)0 : FATAL_ERROR("RUNTIME_CHECK(%s) failed", #cond))
 
 #endif /* UNIT_TESTING */
 
index 9af25e600a87841dd579c7ac04a61074d42d932e..3ca55b6bb6bec60d214b4a5eced0bd4dcee5b0f2 100644 (file)
@@ -1236,9 +1236,7 @@ isc__mempool_destroy(isc_mempool_t **restrict mpctxp FLARG) {
 #endif
 
        if (mpctx->allocated > 0) {
-               UNEXPECTED_ERROR(
-                       "isc_mempool_destroy(): mempool %s leaked memory",
-                       mpctx->name);
+               UNEXPECTED_ERROR("mempool %s leaked memory", mpctx->name);
        }
        REQUIRE(mpctx->allocated == 0);