[master] 0da959260 add a workspace dump utility function

Nils Goroll nils.goroll at uplex.de
Wed Apr 24 15:03:07 UTC 2019


commit 0da959260561d14293f31ae4c83d46955cccb324
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Wed Apr 24 16:42:45 2019 +0200

    add a workspace dump utility function

diff --git a/lib/libvmod_vtc/vmod.vcc b/lib/libvmod_vtc/vmod.vcc
index b6a8e4354..ccff7b654 100644
--- a/lib/libvmod_vtc/vmod.vcc
+++ b/lib/libvmod_vtc/vmod.vcc
@@ -119,6 +119,18 @@ $Function VOID workspace_overflow(ENUM { client, backend, session, thread })
 
 Mark a workspace as overflowed.
 
+$Function BLOB workspace_dump(
+	ENUM { client, backend, session, thread },
+	ENUM { s, f, r },
+	BYTES off=0,
+	BYTES len=64)
+
+Return data from a workspace's ``s``, ``f``, or ``r`` pointer as a
+blob. Data is copied onto the primary workspace to avoid it being
+subsequently overwritten.
+
+The maximum *len* is 1KB.
+
 $Function INT typesize(STRING)
 
 Returns the size in bytes of a collection of C-datatypes:
diff --git a/lib/libvmod_vtc/vmod_vtc.c b/lib/libvmod_vtc/vmod_vtc.c
index c0620bd72..ceaf76ac7 100644
--- a/lib/libvmod_vtc/vmod_vtc.c
+++ b/lib/libvmod_vtc/vmod_vtc.c
@@ -209,6 +209,62 @@ VTC_WS_OP(VOID, , overflow, WS_MarkOverflow(ws))
 VTC_WS_OP(BOOL, (0), overflowed, return (WS_Overflowed(ws)))
 #undef VTC_WS_OP
 
+VCL_BLOB v_matchproto_(td_vtc_workspace_dump)
+vmod_workspace_dump(VRT_CTX, VCL_ENUM which, VCL_ENUM where,
+    VCL_BYTES off, VCL_BYTES len)
+{
+	struct ws *ws;
+	const size_t maxlen = 1024;
+	unsigned char buf[maxlen];
+	const char *p;
+	unsigned l;
+
+	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+
+	ws = vtc_ws_find(ctx, which);
+	if (ws == NULL)
+		return (NULL);
+	WS_Assert(ws);
+
+	if (len > maxlen) {
+		VRT_fail(ctx, "workspace_dump: max length is %zd", maxlen);
+		return (NULL);
+	}
+
+	if (where == VENUM(s))
+		p = ws->s;
+	else if (where == VENUM(f))
+		p = ws->f;
+	else if (where == VENUM(r))
+		p = ws->r;
+	else
+		INCOMPL();
+
+	if (p == NULL) {
+		VSLb(ctx->vsl, SLT_Error, "workspace_dump: NULL");
+		return (NULL);
+	}
+
+	p += off;
+	if (p >= ws->e) {
+		VSLb(ctx->vsl, SLT_Error, "workspace_dump: off limit");
+		return (NULL);
+	}
+
+	l = pdiff(p, ws->e);
+	if (len < l)
+		l = len;
+
+	assert(l < maxlen);
+	memcpy(buf, p, l);
+	p = WS_Copy(ctx->ws, buf, l);
+	if (p == NULL) {
+		VRT_fail(ctx, "workspace_dump: copy failed");
+		return (NULL);
+	}
+	return (VRT_blob(ctx, "workspace_dump", p, l, 0xd000d000));
+}
+
 /*--------------------------------------------------------------------*/
 
 VCL_INT v_matchproto_(td_vtc_typesize)


More information about the varnish-commit mailing list