diff --git a/include/haproxy/trace.h b/include/haproxy/trace.h index 75cde3b21..56c58f109 100644 --- a/include/haproxy/trace.h +++ b/include/haproxy/trace.h @@ -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) diff --git a/src/haproxy.c b/src/haproxy.c index 205c19a06..b9b220f31 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -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); } diff --git a/src/trace.c b/src/trace.c index 43956681f..303b3e690 100644 --- a/src/trace.c +++ b/src/trace.c @@ -35,6 +35,14 @@ 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,