]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: traces: defer processing of "-dt" options
authorMaxime Henrion <mhenrion@haproxy.com>
Thu, 12 Mar 2026 15:29:38 +0000 (11:29 -0400)
committerWilliam Lallemand <wlallemand@haproxy.com>
Fri, 13 Mar 2026 08:13:24 +0000 (09:13 +0100)
We defer processing of the "-dt" options until after the configuration
file has been read. This will be useful if we ever allow trace sources
to be registered later, for instance with LUA.

No backport needed.

include/haproxy/trace.h
src/haproxy.c
src/trace.c

index 75cde3b213a37001f5567c5edb80264871e201b3..56c58f1093457060896a253b9edb4b8a9234d3e6 100644 (file)
@@ -190,7 +190,8 @@ void trace_no_cb(enum trace_level level, uint64_t mask, const struct trace_sourc
 
 void trace_register_source(struct trace_source *source);
 
-int trace_parse_cmd(const char *arg_src, char **errmsg);
+int trace_add_cmd(const char *arg_src, char **errmsg);
+void trace_parse_cmds(void);
 
 /* return a single char to describe a trace state */
 static inline char trace_state_char(enum trace_state st)
index 205c19a06c438743b71d5b78f3a3b395925cdc9c..b9b220f31d1f0f1aca79ac2f38437433fd9d4822 100644 (file)
@@ -1635,18 +1635,11 @@ void haproxy_init_args(int argc, char **argv)
                                        argc--; argv++;
                                }
 
-                               ret = trace_parse_cmd(arg, &err_msg);
-                               if (ret <= -1) {
-                                       if (ret < -1) {
-                                               ha_alert("-dt: %s.\n", err_msg);
-                                               ha_free(&err_msg);
-                                               exit(EXIT_FAILURE);
-                                       }
-                                       else {
-                                               printf("%s\n", err_msg);
-                                               ha_free(&err_msg);
-                                               exit(0);
-                                       }
+                               ret = trace_add_cmd(arg, &err_msg);
+                               if (ret) {
+                                       ha_alert("-dt: %s.\n", err_msg);
+                                       ha_free(&err_msg);
+                                       exit(EXIT_FAILURE);
                                }
                        }
 #ifdef HA_USE_KTLS
@@ -3478,6 +3471,7 @@ int main(int argc, char **argv)
                list_for_each_entry_safe(cfg, cfg_tmp, &cfg_cfgfiles, list)
                        ha_free(&cfg->content);
 
+               trace_parse_cmds();
                usermsgs_clr(NULL);
        }
 
index 43956681fc35d0dad56ec196e4891505dacd70ca..303b3e690802963a6d32ce556ca9f7f5acb50f4e 100644 (file)
 struct list trace_sources = LIST_HEAD_INIT(trace_sources);
 THREAD_LOCAL struct buffer trace_buf = { };
 
+struct trace_cmd {
+       struct list next;
+       const char *arg;
+};
+
+/* List of arguments to "-dt" options for deferred processing. */
+static struct list trace_cmds = LIST_HEAD_INIT(trace_cmds);
+
 /* allocates the trace buffers. Returns 0 in case of failure. It is safe to
  * call to call this function multiple times if the size changes.
  */
@@ -990,6 +998,21 @@ static int trace_parse_statement(char **args, char **msg)
        return _trace_parse_statement(args, msg, NULL, 0);
 }
 
+int trace_add_cmd(const char *arg_src, char **errmsg)
+{
+       struct trace_cmd *cmd;
+
+       cmd = malloc(sizeof(*cmd));
+       if (!cmd) {
+               memprintf(errmsg, "Can't allocate trace cmd!");
+               return -1;
+       }
+
+       cmd->arg = arg_src;
+       LIST_APPEND(&trace_cmds, &cmd->next);
+       return 0;
+}
+
 void _trace_parse_cmd(struct trace_source *src, int level, int verbosity)
 {
        trace_source_reset(src);
@@ -1004,7 +1027,7 @@ void _trace_parse_cmd(struct trace_source *src, int level, int verbosity)
  *
  * Returns 0 on success else non-zero.
  */
-int trace_parse_cmd(const char *arg_src, char **errmsg)
+static int trace_parse_cmd(const char *arg_src, char **errmsg)
 {
        char *str;
        char *arg, *oarg;
@@ -1137,6 +1160,34 @@ int trace_parse_cmd(const char *arg_src, char **errmsg)
        return 0;
 }
 
+void trace_parse_cmds(void)
+{
+       struct trace_cmd *cmd;
+       char *errmsg = NULL;
+       int ret = 0;
+
+       while (!LIST_ISEMPTY(&trace_cmds)) {
+               cmd = LIST_ELEM(trace_cmds.n, struct trace_cmd *, next);
+               if (!ret)
+                       ret = trace_parse_cmd(cmd->arg, &errmsg);
+               LIST_DELETE(&cmd->next);
+               free(cmd);
+       }
+
+       if (ret <= -1) {
+               if (ret < -1) {
+                       ha_alert("-dt: %s.\n", errmsg);
+                       ha_free(&errmsg);
+                       exit(EXIT_FAILURE);
+               }
+               else {
+                       printf("%s\n", errmsg);
+                       ha_free(&errmsg);
+                       exit(0);
+               }
+       }
+}
+
 /* parse a "trace" statement in the "global" section, returns 1 if a message is returned, otherwise zero */
 static int cfg_parse_trace(char **args, int section_type, struct proxy *curpx,
                           const struct proxy *defpx, const char *file, int line,