]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: sample: store location for fetch/conv via initcalls
authorWilly Tarreau <w@1wt.eu>
Fri, 6 Mar 2026 09:49:46 +0000 (10:49 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 12 Mar 2026 17:06:38 +0000 (18:06 +0100)
Now keywords are registered with an exec_ctx and this one is passed
when calling ->process. The ctx is of type INITCALL when passed via
an initcall where we know the file name and line number.

This was tested with and extra "malloc(15)" added in smp_fetch_path()
which shows that it works:

  $ socat /tmp/sock1 - <<< "show profiling memory"|grep via
           Calls         |         Tot Bytes           |       Caller and method  [via]
      1893399           0       60592592              0|         0x78b2ec task_run_applet+0x3339c malloc(32) [via initcall @http_fetch.c:2416]

include/haproxy/sample-t.h
src/hlua.c
src/sample.c

index 27cf4bab820a3dd5d9ca14aa7352344ade73cc90..1ad14522b4ba9411e77bad89a7a2253115d90bb4 100644 (file)
@@ -24,6 +24,7 @@
 #define _HAPROXY_SAMPLE_T_H
 
 #include <haproxy/api-t.h>
+#include <haproxy/tinfo-t.h>
 #include <haproxy/sample_data-t.h>
 
 /* input and output sample types
@@ -265,6 +266,7 @@ struct sample_conv {
        unsigned int in_type;                     /* expected input sample type */
        unsigned int out_type;                    /* output sample type */
        void *private;                            /* private values. only used by maps and Lua */
+       struct thread_exec_ctx exec_ctx;          /* execution context */
 };
 
 /* sample conversion expression */
@@ -288,6 +290,7 @@ struct sample_fetch {
        unsigned int use;                         /* fetch source (SMP_USE_*) */
        unsigned int val;                         /* fetch validity (SMP_VAL_*) */
        void *private;                            /* private values. only used by Lua */
+       struct thread_exec_ctx exec_ctx;          /* execution context */
 };
 
 /* sample expression */
index 604e2d142a8fcc8b44b398269c47ef7a00c7bc04..6e56583df21ae4312d4eff89e09a8c915d9a1706 100644 (file)
@@ -4926,7 +4926,7 @@ __LJMP static int hlua_run_sample_fetch(lua_State *L)
 
        /* Run the sample fetch process. */
        smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
-       if (!f->process(args, &smp, f->kw, f->private)) {
+       if (!EXEC_CTX_WITH_RET(f->exec_ctx, f->process(args, &smp, f->kw, f->private))) {
                if (hsmp->flags & HLUA_F_AS_STRING)
                        lua_pushstring(L, "");
                else
@@ -5059,7 +5059,7 @@ __LJMP static int hlua_run_sample_conv(lua_State *L)
        }
 
        /* Run the sample conversion process. */
-       if (!conv->process(args, &smp, conv->private)) {
+       if (!EXEC_CTX_WITH_RET(conv->exec_ctx, conv->process(args, &smp, conv->private))) {
                if (hsmp->flags & HLUA_F_AS_STRING)
                        lua_pushstring(L, "");
                else
index b29f6f3d3763b11ff38c803b271d2c881a75dbbf..4a79285957eba29b59ce8761af914e22c984824b 100644 (file)
@@ -451,6 +451,14 @@ void sample_register_fetches(struct sample_fetch_kw_list *kwl)
                for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
                        if (sf->use & (1 << bit))
                                sf->val |= fetch_cap[bit];
+               /* store declaration file/line if known */
+               if (sf->exec_ctx.type)
+                       continue;
+
+               if (caller_initcall) {
+                       sf->exec_ctx.type = TH_EX_CTX_INITCALL;
+                       sf->exec_ctx.initcall = caller_initcall;
+               }
        }
        LIST_APPEND(&sample_fetches.list, &kwl->list);
 }
@@ -461,6 +469,18 @@ void sample_register_fetches(struct sample_fetch_kw_list *kwl)
  */
 void sample_register_convs(struct sample_conv_kw_list *pckl)
 {
+       struct sample_conv *sc;
+
+       /* store declaration file/line if known */
+       for (sc = pckl->kw; sc->kw != NULL; sc++) {
+               if (sc->exec_ctx.type)
+                       continue;
+
+               if (caller_initcall) {
+                       sc->exec_ctx.type = TH_EX_CTX_INITCALL;
+                       sc->exec_ctx.initcall = caller_initcall;
+               }
+       }
        LIST_APPEND(&sample_convs.list, &pckl->list);
 }
 
@@ -1337,8 +1357,8 @@ int sample_process_cnv(struct sample_expr *expr, struct sample *p)
                }
 
                /* OK cast succeeded */
-
-               if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
+               if (!EXEC_CTX_WITH_RET(conv_expr->conv->exec_ctx,
+                                      conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private)))
                        return 0;
        }
        return 1;
@@ -1378,7 +1398,8 @@ struct sample *sample_process(struct proxy *px, struct session *sess,
        }
 
        smp_set_owner(p, px, sess, strm, opt);
-       if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
+       if (!EXEC_CTX_WITH_RET(expr->fetch->exec_ctx,
+                              expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private)))
                return NULL;
 
        if (!sample_process_cnv(expr, p))