[master] 4c86b8941 vmod_debug: Add a utility function to log the origin of strands
Nils Goroll
nils.goroll at uplex.de
Sun Feb 9 14:14:06 UTC 2025
commit 4c86b89419142c8c2ae1bdf8399348d1b5272df6
Author: Nils Goroll <nils.goroll at uplex.de>
Date: Sun Feb 9 14:58:11 2025 +0100
vmod_debug: Add a utility function to log the origin of strands
This is implemented as a logging function to not use workspace itself.
diff --git a/bin/varnishtest/tests/v00058.vtc b/bin/varnishtest/tests/v00058.vtc
index 2aa5ee2b2..c69fcce4d 100644
--- a/bin/varnishtest/tests/v00058.vtc
+++ b/bin/varnishtest/tests/v00058.vtc
@@ -1,4 +1,4 @@
-varnishtest "Test VRT STRANDS functions"
+varnishtest "Test VRT STRANDS related functions"
varnish v1 -arg "-i foobar" -vcl {
import debug;
@@ -85,6 +85,11 @@ varnish v1 -arg "-i foobar" -vcl {
req.url + "<-->" + req.url
)
);
+ debug.log_strands("return_strands",
+ debug.return_strands(
+ req.url + "<-->" + req.url
+ )
+ );
}
} -start
diff --git a/vmod/vmod_debug.c b/vmod/vmod_debug.c
index 847477d0a..299b8461d 100644
--- a/vmod/vmod_debug.c
+++ b/vmod/vmod_debug.c
@@ -1288,3 +1288,77 @@ xyzzy_use_reembarking_http1(VRT_CTX)
{
debug_transport_reembarking_http1_use(ctx);
}
+
+static int
+in_oc(struct worker *wrk, struct objcore *oc, const char *p)
+{
+ const char *hdrs;
+ ssize_t len = 0;
+
+ if (oc == NULL)
+ return (0);
+ hdrs = ObjGetAttr(wrk, oc, OA_HEADERS, &len);
+ if (hdrs == NULL)
+ return (0);
+ if (p < hdrs)
+ return (0);
+ if (p > hdrs + len)
+ return (0);
+ return (1);
+}
+
+static const char *
+ptr_where(VRT_CTX, const char *p)
+{
+ struct ws *ws = ctx->ws;
+ struct ws *aws;
+ struct worker *wrk;
+ struct objcore *oc, *stale_oc;
+
+ if (ctx->req != NULL) {
+ wrk = ctx->req->wrk;
+ oc = ctx->req->objcore;
+ stale_oc = ctx->req->stale_oc;
+ }
+ else if (ctx->bo != NULL) {
+ wrk = ctx->bo->wrk;
+ oc = ctx->bo->fetch_objcore;
+ stale_oc = ctx->bo->stale_oc;
+ }
+ else
+ WRONG("ctx");
+
+ AN(wrk);
+ aws = ctx->req->wrk->aws;
+
+ if (WS_Allocated(ws, p, -1))
+ return ("ws");
+ if (WS_Allocated(aws, p, -1))
+ return ("aws");
+ if (in_oc(wrk, oc, p))
+ return ("oc");
+ if (in_oc(wrk, stale_oc, p))
+ return ("stale_oc");
+ return ("?");
+}
+
+VCL_VOID
+xyzzy_log_strands(VRT_CTX, VCL_STRING prefix, VCL_STRANDS subject, VCL_INT nn)
+{
+ int i, n;
+
+ CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+ if (prefix == NULL)
+ prefix = "";
+ AN(subject);
+ if (nn > INT_MAX)
+ n = INT_MAX;
+ else
+ n = nn;
+
+ for (i = 0; i < subject->n; i++) {
+ const char *p = subject->p[i];
+ mylog(ctx->vsl, SLT_Debug, "%s[%d]: (%s) %p %.*s%s", prefix, i,
+ ptr_where(ctx, p), p, n, p, strlen(p) > n ? "..." : "");
+ }
+}
diff --git a/vmod/vmod_debug.vcc b/vmod/vmod_debug.vcc
index 3d791a6c8..1d531cc8c 100644
--- a/vmod/vmod_debug.vcc
+++ b/vmod/vmod_debug.vcc
@@ -447,6 +447,25 @@ $Restrict vcl_deliver
Switch to the reembarking http1 debug transport. Calling it from any other
transport than http1 results in VCL failure.
+$Function VOID log_strands(STRING prefix, STRANDS subject, INT n=4)
+
+For each strands member, emit one ``Debug`` log line as formatted using
+``printf()`` with prefix as ``%s``, the strands member index as ``[%d]:``, the
+*origin* as ``(%s)``, addresses of each strands as ``%p`` and at most the first
+*n* characters of each strand, with truncations marked by "...".
+
+*origin* can be one of:
+
+* ``ws`` if from ``ctx->ws``
+* ``aws`` if from ``wrk->aws``
+* ``oc`` if from ``OA_HEADERS`` of ``req->objcore`` or ``bo->fetch_objcore``
+* ``stale_oc`` if from ``OA_HEADERS`` of ``req->stale_oc`` or bo->stale_oc``
+* ``?`` otherwise
+
+Example::
+
+ Debug c prefix[0]: (ws) 0x7fe69ef80420 abcd...
+
DEPRECATED
==========
More information about the varnish-commit
mailing list