]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: initcall: record the file and line declaration of an INITCALL
authorWilly Tarreau <w@1wt.eu>
Tue, 3 Mar 2026 09:08:19 +0000 (10:08 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 12 Mar 2026 17:06:38 +0000 (18:06 +0100)
The INITCALL macros will now store the file and line number where they
are declared into the initcall struct, and RUN_INITCALLS() will assign
them to the global caller_file and caller_line variables, and will even
set caller_initcall to the current initall so that at any instant such
functions know where their caller declared them. This will help with
error messages and traces where a bit of context will be welcome.

include/haproxy/init.h
include/haproxy/initcall.h
src/init.c

index 47333ffe4df6919647889e1b39fe8e627c530e7f..91b46b0f9c6c1714b6b71f36bbfe0e48ddc5d4fe 100644 (file)
@@ -20,6 +20,11 @@ extern struct list server_deinit_list;
 extern struct list per_thread_free_list;
 extern struct list per_thread_deinit_list;
 
+/* initcall caller location */
+extern const struct initcall *caller_initcall;
+extern const char *caller_file;
+extern int caller_line;
+
 void hap_register_pre_check(int (*fct)());
 void hap_register_post_check(int (*fct)());
 void hap_register_post_proxy_check(int (*fct)(struct proxy *));
index a5f2bd2a8a6808f107e9de4bb5c147f260769153..39c43d7dfeece252e21e730b99afe5ebdc990371 100644 (file)
@@ -77,6 +77,8 @@ struct initcall {
        void *arg1;
        void *arg2;
        void *arg3;
+       const char *loc_file; /* file where the call is declared, or NULL */
+       int loc_line;         /* line where the call is declared, or NULL */
 #if defined(USE_OBSOLETE_LINKER)
        void *next;
 #endif
@@ -107,6 +109,8 @@ struct initcall {
                .arg1 = (void *)(a1),                              \
                .arg2 = (void *)(a2),                              \
                .arg3 = (void *)(a3),                              \
+               .loc_file = __FILE__,                              \
+               .loc_line = linenum,                               \
        } : NULL
 
 
@@ -131,6 +135,8 @@ __attribute__((constructor)) static void __initcb_##linenum()      \
                .arg1 = (void *)(a1),                              \
                .arg2 = (void *)(a2),                              \
                .arg3 = (void *)(a3),                              \
+               .loc_file = __FILE__,                              \
+               .loc_line = linenum,                               \
        };                                                         \
        if (stg < STG_SIZE) {                                      \
                entry.next = __initstg[stg];                       \
@@ -229,8 +235,15 @@ extern struct initcall *__initstg[STG_SIZE];
                const struct initcall **ptr;                                   \
                if (stg >= STG_SIZE)                                           \
                        break;                                                 \
-               FOREACH_INITCALL(ptr, stg)                                     \
+               FOREACH_INITCALL(ptr, stg) {                                   \
+                       caller_initcall = *ptr;                                \
+                       caller_file = (*ptr)->loc_file;                        \
+                       caller_line = (*ptr)->loc_line;                        \
                        (*ptr)->fct((*ptr)->arg1, (*ptr)->arg2, (*ptr)->arg3); \
+                       caller_initcall = NULL;                                \
+                       caller_file = NULL;                                    \
+                       caller_line = 0;                                       \
+               }                                                              \
        } while (0)
 
 #else // USE_OBSOLETE_LINKER
@@ -243,8 +256,15 @@ extern struct initcall *__initstg[STG_SIZE];
                const struct initcall *ptr;                                    \
                if (stg >= STG_SIZE)                                           \
                        break;                                                 \
-               FOREACH_INITCALL(ptr, stg)                                     \
+               FOREACH_INITCALL(ptr, stg) {                                   \
+                       caller_initcall = ptr;                                 \
+                       caller_file = (ptr)->loc_file;                         \
+                       caller_line = (ptr)->loc_line;                         \
                        (ptr)->fct((ptr)->arg1, (ptr)->arg2, (ptr)->arg3);     \
+                       caller_initcall = NULL;                                \
+                       caller_file = NULL;                                    \
+                       caller_line = 0;                                       \
+               }                                                              \
        } while (0)
 
 #endif // USE_OBSOLETE_LINKER
index ac5ac7a2e9faf6f4c124b091e9f6acaa62fb2078..de8b48da2e0afba0f91d3a3bfeabe9d7b296aacd 100644 (file)
@@ -89,6 +89,11 @@ struct list per_thread_free_list = LIST_HEAD_INIT(per_thread_free_list);
  * valgrind mostly happy. */
 struct list per_thread_deinit_list = LIST_HEAD_INIT(per_thread_deinit_list);
 
+/* location of current INITCALL declaration being processed, or NULL */
+const struct initcall *caller_initcall = NULL;
+const char *caller_file = NULL;
+int caller_line = 0;
+
 /* used to register some initialization functions to call before the checks. */
 void hap_register_pre_check(int (*fct)())
 {