varnish-cache/vmod/vmod_debug.c
0
/*-
1
 * Copyright (c) 2012-2019 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Poul-Henning Kamp <phk@FreeBSD.org>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 */
29
30
#include "config.h"
31
32
#include <stdlib.h>
33
#include <stdio.h>
34
#include <string.h>
35
#include <sys/socket.h>
36
#include <unistd.h>
37
38
#include "cache/cache_varnishd.h"
39
#include "cache/cache_filter.h"
40
41
#include "vsa.h"
42
#include "vss.h"
43
#include "vtcp.h"
44
#include "vtim.h"
45
#include "vcc_debug_if.h"
46
#include "VSC_debug.h"
47
48
#include "vmod_debug.h"
49
50
struct priv_vcl {
51
        unsigned                magic;
52
#define PRIV_VCL_MAGIC          0x8E62FA9D
53
        char                    *foo;
54
        uintptr_t               obj_cb;
55
        struct vclref           *vclref_discard;
56
        struct vclref           *vclref_cold;
57
        VCL_DURATION            vcl_discard_delay;
58
        VCL_BACKEND             be;
59
        unsigned                cold_be;
60
        unsigned                cooling_be;
61
        int                     tmpf;
62
};
63
64
65
static pthread_mutex_t vsc_mtx = PTHREAD_MUTEX_INITIALIZER;
66
static struct vsc_seg *vsc_seg = NULL;
67
static struct VSC_debug *vsc = NULL;
68
static int loads;
69
static const int store_ip_token = 0;
70
static const int fail_task_fini_token = 0;
71
extern void mylog(struct vsl_log *vsl, enum VSL_tag_e tag,
72
    const char *fmt, ...) v_printflike_(3,4);
73
74
/**********************************************************************/
75
76
VCL_STRING v_matchproto_(td_debug_author)
77 5
xyzzy_author(VRT_CTX, VCL_ENUM person, VCL_ENUM someone)
78
{
79 5
        (void)someone;
80
81 5
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
82 5
        if (person == VENUM(phk))
83 2
                return ("Poul-Henning");
84 3
        assert(strcmp(person, "phk"));
85 3
        if (person == VENUM(des))
86 1
                return ("Dag-Erling");
87 2
        assert(strcmp(person, "des"));
88 2
        if (person == VENUM(kristian))
89 1
                return ("Kristian");
90 1
        assert(strcmp(person, "kristian"));
91 1
        if (person == VENUM(mithrandir))
92 1
                return ("Tollef");
93 0
        assert(strcmp(person, "mithrandir"));
94 0
        WRONG("Illegal VMOD enum");
95 5
}
96
97
#define AN0(x) (void) 0
98
#define AN1(x) AN(x)
99
#define PRIV_FINI(name, assert)                                         \
100
static void v_matchproto_(vmod_priv_fini_f)                             \
101
priv_ ## name ## _fini(VRT_CTX, void *ptr)                              \
102
{                                                                       \
103
        const char * const fmt = "priv_" #name "_fini(%p)";             \
104
                                                                        \
105
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);                          \
106
        AN ## assert (ptr);                                             \
107
        mylog(ctx->vsl, SLT_Debug, fmt, (char *)ptr);                   \
108
        free(ptr);                                                      \
109
}                                                                       \
110
                                                                        \
111
static const struct vmod_priv_methods                                   \
112
xyzzy_test_priv_ ## name ## _methods[1] = {{                            \
113
        .magic = VMOD_PRIV_METHODS_MAGIC,                               \
114
        .type = "debug_test_priv_" #name,                               \
115
        .fini = priv_ ## name ## _fini                                  \
116
}};
117 2
PRIV_FINI(call, 0)
118 15
PRIV_FINI(task, 1)
119 3
PRIV_FINI(top, 1)
120
#undef PRIV_FINI
121
#undef AN0
122
#undef AN1
123
124
VCL_VOID v_matchproto_(td_debug_test_priv_call)
125 2
xyzzy_test_priv_call(VRT_CTX, struct vmod_priv *priv)
126
{
127
128 2
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
129 2
        if (priv->priv == NULL) {
130 2
                priv->priv = strdup("BAR");
131 2
                priv->methods = xyzzy_test_priv_call_methods;
132 2
        } else {
133 0
                assert(priv->methods == xyzzy_test_priv_call_methods);
134 0
                assert(!strcmp(priv->priv, "BAR"));
135
        }
136 2
}
137
138
VCL_VOID v_matchproto_(td_debug_test_priv_task_get)
139 1
xyzzy_test_priv_task_get(VRT_CTX)
140
{
141
142 1
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
143 1
        AZ(VRT_priv_task_get(ctx, NULL));
144 1
}
145
146
VCL_STRING v_matchproto_(td_debug_test_priv_task)
147 46
xyzzy_test_priv_task(VRT_CTX, struct vmod_priv *priv, VCL_STRING s)
148
{
149
150 46
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
151 46
        if (s == NULL || *s == '\0') {
152 42
                mylog(ctx->vsl, SLT_Debug, "test_priv_task(%p) = %p (exists)",
153 21
                    priv, priv->priv);
154 46
        } else if (priv->priv == NULL) {
155 15
                priv->priv = strdup(s);
156 15
                priv->methods = xyzzy_test_priv_task_methods;
157 30
                mylog(ctx->vsl, SLT_Debug, "test_priv_task(%p) = %p (new)",
158 15
                    priv, priv->priv);
159 15
        } else {
160 20
                char *n = realloc(priv->priv,
161 10
                    strlen(priv->priv) + strlen(s) + 2);
162 10
                if (n == NULL)
163 0
                        return (NULL);
164 10
                strcat(n, " ");
165 10
                strcat(n, s);
166 10
                priv->priv = n;
167 20
                mylog(ctx->vsl, SLT_Debug, "test_priv_task(%p) = %p (update)",
168 10
                    priv, priv->priv);
169
        }
170 46
        if (priv->priv != NULL)
171 45
                assert(priv->methods == xyzzy_test_priv_task_methods);
172 46
        return (priv->priv);
173 46
}
174
175
VCL_STRING v_matchproto_(td_debug_test_priv_top)
176 24
xyzzy_test_priv_top(VRT_CTX, struct vmod_priv *priv, VCL_STRING s)
177
{
178
179 24
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
180 24
        if (priv->priv == NULL) {
181 3
                priv->priv = strdup(s);
182 3
                priv->methods = xyzzy_test_priv_top_methods;
183 3
        } else {
184 21
                assert(priv->methods == xyzzy_test_priv_top_methods);
185
        }
186 24
        return (priv->priv);
187
}
188
189
VCL_VOID v_matchproto_(td_debug_test_priv_vcl)
190 2
xyzzy_test_priv_vcl(VRT_CTX, struct vmod_priv *priv)
191
{
192
        struct priv_vcl *priv_vcl;
193
        char t[PATH_MAX];
194
        ssize_t l;
195
196 2
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
197 2
        AN(priv);
198 2
        CAST_OBJ_NOTNULL(priv_vcl, priv->priv, PRIV_VCL_MAGIC);
199
200 2
        l = pread(priv_vcl->tmpf, t, sizeof t, 0);
201 2
        assert(l > 0);
202
203 2
        AN(priv_vcl->foo);
204 2
        assert(!strncmp(priv_vcl->foo, t, l));
205 2
}
206
207
VCL_VOID v_matchproto_(td_debug_rot52)
208 2
xyzzy_rot52(VRT_CTX, VCL_HTTP hp)
209
{
210
211 2
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
212 2
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
213
214 2
        http_PrintfHeader(hp, "Encrypted: ROT52");
215 2
}
216
217
VCL_STRING v_matchproto_(td_debug_argtest)
218 7
xyzzy_argtest(VRT_CTX, struct VARGS(argtest) *arg)
219
{
220
        char buf[100];
221
222 7
        AN(arg);
223 7
        bprintf(buf, "%s %g %s %s %jd %d %s",
224
            arg->one, arg->two, arg->three, arg->comma, (intmax_t)arg->four,
225
            arg->valid_string, arg->valid_string ? arg->string : "<undef>");
226 7
        return (WS_Copy(ctx->ws, buf, -1));
227
}
228
229
VCL_INT v_matchproto_(td_debug_vre_limit)
230 2
xyzzy_vre_limit(VRT_CTX)
231
{
232 2
        (void)ctx;
233 2
        return (cache_param->vre_limits.match);
234
}
235
236
static void v_matchproto_(obj_event_f)
237 2
obj_cb(struct worker *wrk, void *priv, struct objcore *oc, unsigned event)
238
{
239
        const struct priv_vcl *priv_vcl;
240
        const char *what;
241
242 2
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
243 2
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
244 2
        CAST_OBJ_NOTNULL(priv_vcl, priv, PRIV_VCL_MAGIC);
245 2
        switch (event) {
246 1
        case OEV_INSERT: what = "insert"; break;
247 1
        case OEV_EXPIRE: what = "expire"; break;
248 0
        default: WRONG("Wrong object event");
249 0
        }
250
251
        /* We cannot trust %p to be 0x... format as expected by m00021.vtc */
252 4
        VSL(SLT_Debug, NO_VXID, "Object Event: %s 0x%jx", what,
253 2
            (intmax_t)(uintptr_t)oc);
254 2
}
255
256
VCL_VOID v_matchproto_(td_debug_register_obj_events)
257 1
xyzzy_register_obj_events(VRT_CTX, struct vmod_priv *priv)
258
{
259
        struct priv_vcl *priv_vcl;
260
261 1
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
262 1
        CAST_OBJ_NOTNULL(priv_vcl, priv->priv, PRIV_VCL_MAGIC);
263 1
        AZ(priv_vcl->obj_cb);
264 1
        priv_vcl->obj_cb = ObjSubscribeEvents(obj_cb, priv_vcl,
265
                OEV_INSERT|OEV_EXPIRE);
266 1
        VSL(SLT_Debug, NO_VXID, "Subscribed to Object Events");
267 1
}
268
269
VCL_VOID v_matchproto_(td_debug_fail)
270 16
xyzzy_fail(VRT_CTX)
271
{
272
273 16
        VRT_fail(ctx, "Forced failure");
274 16
}
275
276
VCL_BOOL v_matchproto_(td_debug_fail2)
277 1
xyzzy_fail2(VRT_CTX)
278
{
279
280 1
        VRT_fail(ctx, "Forced failure");
281 1
        return (1);
282
}
283
284
static void v_matchproto_(vmod_priv_fini_f)
285 23
priv_vcl_fini(VRT_CTX, void *priv)
286
{
287
        struct priv_vcl *priv_vcl;
288
289 23
        CAST_OBJ_NOTNULL(priv_vcl, priv, PRIV_VCL_MAGIC);
290 23
        AZ(close(priv_vcl->tmpf));
291 23
        AN(priv_vcl->foo);
292 23
        AZ(unlink(priv_vcl->foo));
293 23
        free(priv_vcl->foo);
294 23
        if (priv_vcl->obj_cb != 0) {
295 1
                ObjUnsubscribeEvents(&priv_vcl->obj_cb);
296 1
                VSLb(ctx->vsl, SLT_Debug, "Unsubscribed from Object Events");
297 1
        }
298 23
        AZ(priv_vcl->vclref_discard);
299 23
        AZ(priv_vcl->vclref_cold);
300 23
        FREE_OBJ(priv_vcl);
301 23
}
302
303
static const struct vmod_priv_methods priv_vcl_methods[1] = {{
304
        .magic = VMOD_PRIV_METHODS_MAGIC,
305
        .type = "debug_priv_vcl_fini",
306
        .fini = priv_vcl_fini
307
}};
308
309
static int
310 101
event_load(VRT_CTX, struct vmod_priv *priv)
311
{
312
        struct priv_vcl *priv_vcl;
313
314 101
        AN(ctx->msg);
315
316 101
        loads++;
317
318 101
        if (cache_param->nuke_limit == 42) {
319 1
                VSB_cat(ctx->msg, "nuke_limit is not the answer.");
320 1
                return (-1);
321
        }
322
323 100
        ALLOC_OBJ(priv_vcl, PRIV_VCL_MAGIC);
324 100
        AN(priv_vcl);
325 100
        priv_vcl->foo = strdup("worker_tmpdir/vmod_debug.XXXXXX");
326 100
        AN(priv_vcl->foo);
327 100
        priv_vcl->tmpf = mkstemp(priv_vcl->foo);
328 100
        assert(priv_vcl->tmpf >= 0);
329 100
        AN(write(priv_vcl->tmpf, priv_vcl->foo, strlen(priv_vcl->foo)));
330 100
        priv->priv = priv_vcl;
331 100
        priv->methods = priv_vcl_methods;
332
333 100
        debug_add_filters(ctx);
334 100
        debug_transport_reembarking_http1_init();
335 100
        debug_transport_vai_init();
336 100
        return (0);
337 101
}
338
339
VCL_VOID
340 1
xyzzy_vcl_prevent_cold(VRT_CTX, struct vmod_priv *priv)
341
{
342
        struct priv_vcl *priv_vcl;
343
        char buf[32];
344
345 1
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
346 1
        AN(priv);
347 1
        CAST_OBJ_NOTNULL(priv_vcl, priv->priv, PRIV_VCL_MAGIC);
348 1
        AZ(priv_vcl->vclref_cold);
349
350 1
        bprintf(buf, "vmod-debug ref on %s", VCL_Name(ctx->vcl));
351 1
        priv_vcl->vclref_cold = VRT_VCL_Prevent_Cold(ctx, buf);
352 1
}
353
354
VCL_VOID
355 1
xyzzy_vcl_allow_cold(VRT_CTX, struct vmod_priv *priv)
356
{
357
        struct priv_vcl *priv_vcl;
358
359 1
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
360 1
        AN(priv);
361 1
        CAST_OBJ_NOTNULL(priv_vcl, priv->priv, PRIV_VCL_MAGIC);
362 1
        AN(priv_vcl->vclref_cold);
363 1
        VRT_VCL_Allow_Cold(&priv_vcl->vclref_cold);
364 1
}
365
366
VCL_VOID
367 0
xyzzy_cold_backend(VRT_CTX, struct vmod_priv *priv)
368
{
369
        struct priv_vcl *priv_vcl;
370
371 0
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
372 0
        AN(priv);
373 0
        CAST_OBJ_NOTNULL(priv_vcl, priv->priv, PRIV_VCL_MAGIC);
374 0
        priv_vcl->cold_be = 1;
375 0
}
376
377
VCL_VOID
378 0
xyzzy_cooling_backend(VRT_CTX, struct vmod_priv *priv)
379
{
380
        struct priv_vcl *priv_vcl;
381
382 0
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
383 0
        AN(priv);
384 0
        CAST_OBJ_NOTNULL(priv_vcl, priv->priv, PRIV_VCL_MAGIC);
385 0
        priv_vcl->cooling_be = 1;
386 0
}
387
388
static const struct vdi_methods empty_methods[1] = {{
389
        .magic =        VDI_METHODS_MAGIC,
390
        .type = "debug.dummy"
391
}};
392
393
static int
394 91
event_warm(VRT_CTX, const struct vmod_priv *priv)
395
{
396
        struct priv_vcl *priv_vcl;
397
        char buf[32];
398 91
        const char *vcl_name = VCL_Name(ctx->vcl);
399
400
        // Using VSLs for coverage
401 91
        VSLs(SLT_Debug, NO_VXID, TOSTRANDS(2, vcl_name, ": VCL_EVENT_WARM"));
402
403 91
        AN(ctx->msg);
404 91
        if (cache_param->max_esi_depth == 42) {
405 3
                VSB_cat(ctx->msg, "max_esi_depth is not the answer.");
406 3
                return (-1);
407
        }
408
409 88
        CAST_OBJ_NOTNULL(priv_vcl, priv->priv, PRIV_VCL_MAGIC);
410 88
        AZ(priv_vcl->vclref_discard);
411
412 88
        if (!priv_vcl->cold_be) {
413
                /* NB: set up a COOLING step unless we want a COLD backend. */
414 88
                bprintf(buf, "vmod-debug ref on %s", VCL_Name(ctx->vcl));
415 88
                priv_vcl->vclref_discard = VRT_VCL_Prevent_Discard(ctx, buf);
416 88
        }
417
418 88
        AZ(priv_vcl->be);
419 88
        priv_vcl->be = VRT_AddDirector(ctx, empty_methods,
420
            NULL, "%s", "dir_warmcold");
421
422 88
        return (0);
423 91
}
424
425
static void*
426 1
cooldown_thread(void *priv)
427
{
428
        struct priv_vcl *priv_vcl;
429
430 1
        CAST_OBJ_NOTNULL(priv_vcl, priv, PRIV_VCL_MAGIC);
431 1
        AN(priv_vcl->vclref_discard);
432
433 1
        VTIM_sleep(priv_vcl->vcl_discard_delay);
434 1
        VRT_VCL_Allow_Discard(&priv_vcl->vclref_discard);
435 1
        return (NULL);
436
}
437
438
static VCL_BACKEND
439 0
create_cold_backend(VRT_CTX)
440
{
441
        struct vrt_endpoint vep[1];
442
        struct vrt_backend be[1];
443
444 0
        INIT_OBJ(vep, VRT_ENDPOINT_MAGIC);
445 0
        vep->uds_path = "/";
446 0
        INIT_OBJ(be, VRT_BACKEND_MAGIC);
447 0
        be->endpoint = vep;
448 0
        be->vcl_name = "doomed";
449 0
        return (VRT_new_backend(ctx, be, NULL));
450
}
451
452
static int
453 14
event_cold(VRT_CTX, const struct vmod_priv *priv)
454
{
455
        pthread_t thread;
456
        struct priv_vcl *priv_vcl;
457
458 14
        AZ(ctx->msg);
459
460 14
        CAST_OBJ_NOTNULL(priv_vcl, priv->priv, PRIV_VCL_MAGIC);
461
462 14
        VSL(SLT_Debug, NO_VXID, "%s: VCL_EVENT_COLD", VCL_Name(ctx->vcl));
463
464 14
        VRT_DelDirector(&priv_vcl->be);
465
466 14
        if (priv_vcl->cold_be) {
467 0
                AZ(priv_vcl->vclref_discard);
468 0
                priv_vcl->be = create_cold_backend(ctx);
469 0
                WRONG("unreachable");
470 0
        }
471
472 14
        if (priv_vcl->cooling_be) {
473 0
                AN(priv_vcl->vclref_discard);
474 0
                priv_vcl->be = create_cold_backend(ctx);
475 0
                AZ(priv_vcl->be);
476 0
        }
477
478 14
        if (priv_vcl->vcl_discard_delay == 0.0) {
479 13
                AN(priv_vcl->vclref_discard);
480 13
                VRT_VCL_Allow_Discard(&priv_vcl->vclref_discard);
481 13
                return (0);
482
        }
483
484 1
        PTOK(pthread_create(&thread, NULL, cooldown_thread, priv_vcl));
485 1
        PTOK(pthread_detach(thread));
486 1
        return (0);
487 14
}
488
489
static int
490 23
event_discard(VRT_CTX, void *priv)
491
{
492
493 23
        (void)priv;
494
495 23
        AZ(ctx->msg);
496
497 23
        debug_remove_filters(ctx);
498
499 23
        if (--loads)
500 15
                return (0);
501
502
        /*
503
         * The vsc and vsc_seg variables are not per-VCL, they are
504
         * the same in all VCL's which import the same binary version
505
         * of this VMOD, so we should only carry out cleanup on the
506
         * last discard event.
507
         */
508 8
        PTOK(pthread_mutex_lock(&vsc_mtx));
509 8
        if (vsc != NULL) {
510 1
                VSC_debug_Destroy(&vsc_seg);
511 1
                vsc = NULL;
512 1
        }
513 8
        PTOK(pthread_mutex_unlock(&vsc_mtx));
514
515 8
        return (0);
516 23
}
517
518
int v_matchproto_(vmod_event_f)
519 229
xyzzy_event_function(VRT_CTX, struct vmod_priv *priv, enum vcl_event_e e)
520
{
521
522 229
        switch (e) {
523 101
        case VCL_EVENT_LOAD:    return (event_load(ctx, priv));
524 91
        case VCL_EVENT_WARM:    return (event_warm(ctx, priv));
525 14
        case VCL_EVENT_COLD:    return (event_cold(ctx, priv));
526 23
        case VCL_EVENT_DISCARD: return (event_discard(ctx, priv));
527 0
        default: WRONG("we should test all possible events");
528 0
        }
529 229
}
530
531
VCL_VOID v_matchproto_(td_debug_vcl_discard_delay)
532 1
xyzzy_vcl_discard_delay(VRT_CTX, struct vmod_priv *priv, VCL_DURATION delay)
533
{
534
        struct priv_vcl *priv_vcl;
535
536 1
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
537 1
        CAST_OBJ_NOTNULL(priv_vcl, priv->priv, PRIV_VCL_MAGIC);
538 1
        assert(delay > 0.0);
539 1
        priv_vcl->vcl_discard_delay = delay;
540 1
}
541
542
VCL_VOID v_matchproto_(td_debug_test_probe)
543 0
xyzzy_test_probe(VRT_CTX, VCL_PROBE probe, VCL_PROBE same)
544
{
545
546 0
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
547 0
        CHECK_OBJ_NOTNULL(probe, VRT_BACKEND_PROBE_MAGIC);
548 0
        CHECK_OBJ_ORNULL(same, VRT_BACKEND_PROBE_MAGIC);
549 0
        AZ(same == NULL || probe == same);
550 0
}
551
552
VCL_VOID
553 1
xyzzy_vsc_new(VRT_CTX)
554
{
555 1
        (void)ctx;
556 1
        PTOK(pthread_mutex_lock(&vsc_mtx));
557 1
        if (vsc == NULL) {
558 1
                AZ(vsc_seg);
559 1
                vsc = VSC_debug_New(NULL, &vsc_seg, "");
560 1
        }
561 1
        AN(vsc);
562 1
        AN(vsc_seg);
563 1
        PTOK(pthread_mutex_unlock(&vsc_mtx));
564 1
}
565
566
VCL_VOID
567 1
xyzzy_vsc_count(VRT_CTX, VCL_INT cnt)
568
{
569 1
        (void)ctx;
570 1
        PTOK(pthread_mutex_lock(&vsc_mtx));
571 1
        AN(vsc);
572 1
        vsc->count += cnt;
573 1
        PTOK(pthread_mutex_unlock(&vsc_mtx));
574 1
}
575
576
VCL_VOID
577 0
xyzzy_vsc_destroy(VRT_CTX)
578
{
579 0
        (void)ctx;
580 0
        PTOK(pthread_mutex_lock(&vsc_mtx));
581 0
        if (vsc != NULL) {
582 0
                AN(vsc_seg);
583 0
                VSC_debug_Destroy(&vsc_seg);
584 0
        }
585 0
        AZ(vsc_seg);
586 0
        vsc = NULL;
587 0
        PTOK(pthread_mutex_unlock(&vsc_mtx));
588 0
}
589
590
struct xyzzy_debug_concat {
591
        unsigned        magic;
592
#define CONCAT_MAGIC 0x6b746493
593
        VCL_STRING      s;
594
};
595
596
VCL_VOID
597 4
xyzzy_concat__init(VRT_CTX, struct xyzzy_debug_concat **concatp,
598
                   const char *vcl_name, VCL_STRANDS s)
599
{
600
        struct xyzzy_debug_concat *concat;
601 4
        size_t sz = 0;
602
        char *p;
603
604 4
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
605 4
        AN(concatp);
606 4
        AZ(*concatp);
607 4
        AN(vcl_name);
608
609 4
        ALLOC_OBJ(concat, CONCAT_MAGIC);
610 4
        AN(concat);
611 4
        *concatp = concat;
612
613 12
        for (int i = 0; i < s->n; i++)
614 16
                if (s->p[i] != NULL)
615 8
                        sz += strlen(s->p[i]);
616 4
        p = malloc(sz + 1);
617 4
        AN(p);
618 4
        (void)VRT_Strands(p, sz + 1, s);
619 4
        concat->s = p;
620 4
}
621
622
VCL_VOID
623 0
xyzzy_concat__fini(struct xyzzy_debug_concat **concatp)
624
{
625
        struct xyzzy_debug_concat *concat;
626
627
628 0
        TAKE_OBJ_NOTNULL(concat, concatp, CONCAT_MAGIC);
629 0
        free(TRUST_ME(concat->s));
630 0
        FREE_OBJ(concat);
631 0
}
632
633
VCL_STRING
634 2
xyzzy_concat_get(VRT_CTX, struct xyzzy_debug_concat *concat)
635
{
636 2
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
637 2
        CHECK_OBJ_NOTNULL(concat, CONCAT_MAGIC);
638 2
        return (concat->s);
639
}
640
641
VCL_STRING
642 8
xyzzy_concatenate(VRT_CTX, VCL_STRANDS s)
643
{
644
        VCL_STRING r;
645
646 8
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
647 8
        r = VRT_StrandsWS(ctx->ws, NULL, s);
648 8
        if (r != NULL && *r != '\0')
649 6
                AN(WS_Allocated(ctx->ws, r, strlen(r) + 1));
650 8
        return (r);
651
}
652
653
VCL_STRING
654 8
xyzzy_collect(VRT_CTX, VCL_STRANDS s)
655
{
656
        VCL_STRING r;
657
658 8
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
659 8
        r = VRT_STRANDS_string(ctx, s);
660 8
        if (r != NULL && *r != '\0')
661 6
                AN(WS_Allocated(ctx->ws, r, strlen(r) + 1));
662 8
        return (r);
663
}
664
665
/* cf. VRT_SetHdr() */
666
VCL_VOID
667 8
xyzzy_sethdr(VRT_CTX, VCL_HEADER hdr, VCL_STRANDS s)
668
{
669
        struct http *hp;
670
        const char *b;
671
672 8
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
673 8
        if (hdr == NULL) {
674 0
                VRT_fail(ctx, "debug.sethdr(): header argument is NULL");
675 0
                return;
676
        }
677 8
        hp = VRT_selecthttp(ctx, hdr->where);
678 8
        if (hp == NULL) {
679 0
                VRT_fail(ctx, "debug.sethdr(): header argument "
680
                    "cannot be used here");
681 0
                return;
682
        }
683 8
        AN(hdr->what);
684 8
        CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
685 8
        if (s->n == 0) {
686 0
                http_Unset(hp, hdr->what);
687 0
        } else {
688 8
                b = VRT_StrandsWS(hp->ws, hdr->what->str, s);
689 8
                if (b == NULL) {
690 0
                        VSLbs(ctx->vsl, SLT_LostHeader,
691 0
                            TOSTRAND(hdr->what->str));
692 0
                } else {
693 8
                        if (*b != '\0')
694 8
                                AN(WS_Allocated(hp->ws, b, strlen(b) + 1));
695 8
                        http_Unset(hp, hdr->what);
696 8
                        http_SetHeader(hp, b);
697
                }
698
        }
699 8
}
700
701
void
702 145
mylog(struct vsl_log *vsl, enum VSL_tag_e tag,  const char *fmt, ...)
703
{
704
        va_list ap;
705
706 145
        va_start(ap, fmt);
707 145
        if (vsl != NULL)
708 126
                VSLbv(vsl, tag, fmt, ap);
709
        else
710 19
                VSLv(tag, NO_VXID, fmt, ap);
711 145
        va_end(ap);
712 145
}
713
714
VCL_DURATION
715 4
xyzzy_priv_perf(VRT_CTX, VCL_INT size, VCL_INT rounds)
716
{
717
        vtim_mono t0, t1;
718
        vtim_dur d;
719
        struct vmod_priv *p;
720
        VCL_INT s, r;
721 4
        uintptr_t check = 0;
722
723 4
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
724
725 1115
        for (s = 1; s <= size; s++) {
726 1111
                p = VRT_priv_task(ctx, (void *)(uintptr_t)s);
727 1111
                if (p == NULL) {
728 0
                        VRT_fail(ctx, "no priv task - out of ws?");
729 0
                        return (-1.0);
730
                }
731 1111
                p->priv = NULL;
732 1111
        }
733
734 4
        t0 = VTIM_mono();
735 40004
        for (r = 0; r < rounds; r++) {
736 11150000
                for (s = 1; s <= size; s++) {
737 11110000
                        p = VRT_priv_task_get(ctx, (void *)(uintptr_t)s);
738 11110000
                        AN(p);
739 11110000
                        check += (uintptr_t)p->priv;
740 11110000
                        p->priv = (void *)(uintptr_t)(s * rounds + r);
741 11110000
                }
742 40000
        }
743 4
        t1 = VTIM_mono();
744
745 4
        d = (t1 - t0) * 1e9 / ((double)size * (double)rounds);
746
747 8
        mylog(ctx->vsl, SLT_Debug,
748
             "perf size %jd rounds %jd time %.1fns check %jd",
749 4
             (intmax_t)size, (intmax_t)rounds, d, (uintmax_t)check);
750
751 4
        return (d);
752 4
}
753
754
VCL_STRANDS
755 10
xyzzy_return_strands(VRT_CTX, VCL_STRANDS strand)
756
{
757
758 10
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
759 10
        if (ctx->vsl)
760 5
                VSLbs(ctx->vsl, SLT_Debug, strand);
761
        else
762 5
                VSLs(SLT_Debug, NO_VXID, strand);
763 10
        return (strand);
764
}
765
766
VCL_STRANDS
767 1
xyzzy_return_null_strands(VRT_CTX)
768
{
769 1
        (void)ctx;
770 1
        return (vrt_null_strands);
771
}
772
773
VCL_BOOL
774 1
xyzzy_is_null_strands(VRT_CTX, VCL_STRANDS s)
775
{
776 1
        (void)ctx;
777 1
        return (s == vrt_null_strands);
778
}
779
780
VCL_VOID
781 2
xyzzy_vsl_flush(VRT_CTX)
782
{
783 2
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
784 2
        VSL_Flush(ctx->vsl, 0);
785 2
}
786
787
/*---------------------------------------------------------------------*/
788
789
static const struct vcf_return * v_matchproto_(vcf_func_f)
790 14
xyzzy_catflap_simple(struct req *req, struct objcore **oc,
791
    struct objcore **oc_exp, int state)
792
{
793
794 14
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
795 14
        CHECK_OBJ_NOTNULL(req->vcf, VCF_MAGIC);
796 14
        assert(req->vcf->func == xyzzy_catflap_simple);
797
798 14
        (void)oc;
799 14
        (void)oc_exp;
800 14
        if (state == 0) {
801 4
                if (req->vcf->priv == VENUM(first))
802 1
                        return (VCF_HIT);
803 3
                if (req->vcf->priv == VENUM(miss))
804 3
                        return (VCF_MISS);
805 0
                WRONG("Shouldn't get here");
806 0
        }
807 10
        return (VCF_DEFAULT);
808 14
}
809
810
static const struct vcf_return * v_matchproto_(vcf_func_f)
811 6
xyzzy_catflap_last(struct req *req, struct objcore **oc,
812
    struct objcore **oc_exp, int state)
813
{
814
815 6
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
816 6
        CHECK_OBJ_NOTNULL(req->vcf, VCF_MAGIC);
817 6
        assert(req->vcf->func == xyzzy_catflap_last);
818
819 6
        (void)oc_exp;
820 6
        if (state == 0) {
821 4
                AN(oc);
822 4
                CHECK_OBJ_NOTNULL(*oc, OBJCORE_MAGIC);
823 4
                req->vcf->priv = *oc;
824 4
                return (VCF_CONTINUE);
825
        }
826 2
        if (state == 1) {
827 1
                AN(oc);
828 1
                if (req->vcf->priv != NULL)
829 1
                        CAST_OBJ_NOTNULL(*oc, req->vcf->priv, OBJCORE_MAGIC);
830 1
                return (VCF_CONTINUE);
831
        }
832 1
        return (VCF_DEFAULT);
833 6
}
834
835
VCL_VOID
836 8
xyzzy_catflap(VRT_CTX, VCL_ENUM type)
837
{
838
        struct req *req;
839
840 8
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
841 8
        req = ctx->req;
842 8
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
843 8
        XXXAZ(req->vcf);
844 16
        WS_TASK_ALLOC_OBJ(ctx, req->vcf, VCF_MAGIC);
845 8
        if (req->vcf == NULL)
846 0
                return;
847 8
        if (type == VENUM(first) || type == VENUM(miss)) {
848 7
                req->vcf->func = xyzzy_catflap_simple;
849 7
                req->vcf->priv = TRUST_ME(type);
850 8
        } else if (type == VENUM(last)) {
851 1
                req->vcf->func = xyzzy_catflap_last;
852 1
        } else {
853 0
                WRONG("Wrong VENUM");
854
        }
855 8
}
856
857
VCL_BYTES
858 0
xyzzy_stk(VRT_CTX)
859
{
860 0
        const VCL_BYTES max = 100 * 1024 * 1024;
861
        const char *a, *b;
862
        VCL_BYTES r;
863
864 0
        a = TRUST_ME(&b);
865 0
        b = TRUST_ME(ctx->req->wrk);
866 0
        b += sizeof(*ctx->req->wrk);
867
868 0
        if (b > a && (r = b - a) < max)
869 0
                return (r);
870 0
        if (a > b && (r = a - b) < max)
871 0
                return (r);
872
873 0
        return (0);
874 0
}
875
876
VCL_VOID
877 8
xyzzy_sndbuf(VRT_CTX, VCL_BYTES arg)
878
{
879 8
        int fd = -1, oldbuf, newbuf, buflen;
880 8
        socklen_t intlen = sizeof(int);
881
882 8
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
883
884 8
        if (ctx->bo) {
885 0
                CHECK_OBJ(ctx->bo, BUSYOBJ_MAGIC);
886 0
                INCOMPL();
887 0
        }
888 8
        else if (ctx->req) {
889 8
                CHECK_OBJ(ctx->req, REQ_MAGIC);
890 8
                CHECK_OBJ(ctx->req->sp, SESS_MAGIC);
891 8
                fd = ctx->req->sp->fd;
892 8
        }
893
        else {
894 0
                VRT_fail(ctx, "debug.sndbuf() called outside a transaction.");
895 0
                return;
896
        }
897
898 8
        xxxassert(fd >= 0);
899
900 8
        if (arg > INT_MAX)
901 0
                buflen = INT_MAX;
902 8
        else if (arg <= 0)
903 0
                buflen = 0;
904
        else
905 8
                buflen = (int)arg;
906
907 8
        oldbuf = 0;
908 8
        AZ(getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &oldbuf, &intlen));
909
910 8
        newbuf = buflen;
911 8
        AZ(setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &newbuf, intlen));
912 8
        AZ(getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &newbuf, &intlen));
913
914 8
        AN(ctx->vsl);
915 16
        VSLb(ctx->vsl, SLT_Debug, "SO_SNDBUF fd=%d old=%d new=%d actual=%d",
916 8
            fd, oldbuf, buflen, newbuf);
917 8
}
918
919
VCL_VOID
920 14
xyzzy_store_ip(VRT_CTX, VCL_IP ip)
921
{
922
        struct vmod_priv *priv;
923
924 14
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
925
926 14
        priv = VRT_priv_task(ctx, &store_ip_token);
927 14
        if (priv == NULL) {
928 0
                VRT_fail(ctx, "no priv task - out of ws?");
929 0
                return;
930
        }
931
932 14
        AZ(priv->methods);
933 14
        assert(VSA_Sane(ip));
934 14
        priv->priv = TRUST_ME(ip);
935 14
}
936
937
VCL_IP
938 28
xyzzy_get_ip(VRT_CTX)
939
{
940
        struct vmod_priv *priv;
941
        VCL_IP ip;
942
943 28
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
944
945 28
        priv = VRT_priv_task_get(ctx, &store_ip_token);
946 28
        AN(priv);
947 28
        AZ(priv->methods);
948
949 28
        ip = priv->priv;
950 28
        assert(VSA_Sane(ip));
951 28
        return (ip);
952
}
953
954
/*---------------------------------------------------------------------*/
955
956
struct VPFX(debug_director) {
957
        unsigned                        magic;
958
#define VMOD_DEBUG_DIRECTOR_MAGIC       0x66b9ff3d
959
        VCL_BACKEND                     dir;
960
};
961
962
/* XXX more callbacks ? */
963
static vdi_healthy_f vmod_debug_director_healthy;
964
static vdi_resolve_f vmod_debug_director_resolve;
965
966
static const struct vdi_methods vmod_debug_director_methods[1] = {{
967
        .magic =        VDI_METHODS_MAGIC,
968
        .type =         "debug.director",
969
        .resolve =      vmod_debug_director_resolve,
970
        .healthy =      vmod_debug_director_healthy
971
}};
972
973
VCL_VOID v_matchproto_(td_xyzzy_debug_director__init)
974 1
xyzzy_director__init(VRT_CTX, struct VPFX(debug_director) **dp,
975
    const char *vcl_name)
976
{
977
        struct VPFX(debug_director) *d;
978
979 1
        AN(dp);
980 1
        AZ(*dp);
981 1
        ALLOC_OBJ(d, VMOD_DEBUG_DIRECTOR_MAGIC);
982 1
        AN(d);
983
984 1
        *dp = d;
985 2
        d->dir = VRT_AddDirector(ctx, vmod_debug_director_methods, d,
986 1
            "%s", vcl_name);
987 1
}
988
989
VCL_VOID v_matchproto_(td_xyzzy_debug_director__fini)
990 0
xyzzy_director__fini(struct VPFX(debug_director) **dp)
991
{
992
        struct VPFX(debug_director) *d;
993
994 0
        TAKE_OBJ_NOTNULL(d, dp, VMOD_DEBUG_DIRECTOR_MAGIC);
995 0
        VRT_DelDirector(&d->dir);
996 0
        FREE_OBJ(d);
997 0
}
998
999
VCL_BACKEND v_matchproto_(td_xyzzy_debug_director_fail)
1000 2
xyzzy_director_fail(VRT_CTX, struct VPFX(debug_director) *d)
1001
{
1002 2
        CHECK_OBJ_NOTNULL(d, VMOD_DEBUG_DIRECTOR_MAGIC);
1003 2
        (void) ctx;
1004
1005 2
        return (d->dir);
1006
}
1007
1008
static VCL_BOOL v_matchproto_(vdi_healthy_f)
1009 2
vmod_debug_director_healthy(VRT_CTX, VCL_BACKEND dir, VCL_TIME *changed)
1010
{
1011 2
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
1012
1013 2
        (void) dir;
1014 2
        (void) changed;
1015
1016 2
        VRT_fail(ctx, "fail");
1017 2
        return (1);
1018
}
1019
1020
static VCL_BACKEND v_matchproto_(vdi_resolve_f)
1021 1
vmod_debug_director_resolve(VRT_CTX, VCL_BACKEND dir)
1022
{
1023 1
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
1024
1025 1
        (void) dir;
1026
1027 1
        VRT_fail(ctx, "fail");
1028 1
        return (NULL);
1029
}
1030
1031
VCL_STRING v_matchproto_(td_xyzzy_debug_client_ip)
1032 1
xyzzy_client_ip(VRT_CTX)
1033
{
1034 1
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
1035
1036 1
        return (SES_Get_String_Attr(ctx->sp, SA_CLIENT_IP));
1037
}
1038
1039
VCL_STRING v_matchproto_(td_xyzzy_debug_client_port)
1040 1
xyzzy_client_port(VRT_CTX)
1041
{
1042 1
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
1043
1044 1
        return (SES_Get_String_Attr(ctx->sp, SA_CLIENT_PORT));
1045
}
1046
1047
static void * fail_magic = &fail_magic;
1048
1049
static void
1050 3
fail_f(VRT_CTX, void *priv)
1051
{
1052
1053 3
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
1054 3
        assert(priv == fail_magic);
1055
1056 3
        VRT_fail(ctx, "thou shalt not fini");
1057 3
}
1058
1059
static const struct vmod_priv_methods xyzzy_fail_task_fini_methods[1] = {{
1060
        .magic = VMOD_PRIV_METHODS_MAGIC,
1061
        .type = "debug_fail_task_fini",
1062
        .fini = fail_f
1063
}};
1064
1065
VCL_VOID v_matchproto_(td_xyzzy_debug_fail_task_fini)
1066 4
xyzzy_fail_task_fini(VRT_CTX)
1067
{
1068
        struct vmod_priv *p;
1069
1070 4
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
1071
1072 4
        p = VRT_priv_task(ctx, &fail_task_fini_token);
1073 4
        if (p == NULL) {
1074 0
                VRT_fail(ctx, "no priv task - out of ws?");
1075 0
                return;
1076
        }
1077
1078 4
        if (p->priv != NULL) {
1079 0
                assert(p->priv == fail_magic);
1080 0
                assert(p->methods == xyzzy_fail_task_fini_methods);
1081 0
                return;
1082
        }
1083
1084 4
        p->priv = fail_magic;
1085 4
        p->methods = xyzzy_fail_task_fini_methods;
1086 4
}
1087
1088
VCL_VOID v_matchproto_(td_xyzzy_debug_ok_task_fini)
1089 1
xyzzy_ok_task_fini(VRT_CTX)
1090
{
1091
        struct vmod_priv *p;
1092
1093 1
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
1094
1095 1
        p = VRT_priv_task(ctx, &fail_task_fini_token);
1096 1
        if (p == NULL) {
1097 0
                VRT_fail(ctx, "no priv task - out of ws?");
1098 0
                return;
1099
        }
1100
1101 1
        p->priv = NULL;
1102 1
        p->methods = NULL;
1103 1
}
1104
1105
VCL_STRING v_matchproto_(td_xyzzy_debug_re_quote)
1106 6
xyzzy_re_quote(VRT_CTX, VCL_STRING s)
1107
{
1108
        struct vsb vsb[1];
1109
        char *q;
1110
1111 6
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
1112 6
        CHECK_OBJ_NOTNULL(ctx->ws, WS_MAGIC);
1113 6
        WS_VSB_new(vsb, ctx->ws);
1114 6
        VRE_quote(vsb, s);
1115 6
        q = WS_VSB_finish(vsb, ctx->ws, NULL);
1116 6
        if (q == NULL)
1117 0
                WS_MarkOverflow(ctx->ws);
1118 6
        return (q);
1119
}
1120
1121
VCL_STRING v_matchproto_(td_xyzzy_priv_task_with_option)
1122 3
xyzzy_priv_task_with_option(VRT_CTX, struct VARGS(priv_task_with_option) *args)
1123
{
1124
1125 3
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
1126 3
        AN(args->priv);
1127 3
        if (args->priv->priv == NULL && args->valid_opt)
1128 1
                args->priv->priv = WS_Copy(ctx->ws, args->opt, -1);
1129 3
        return (args->priv->priv);
1130
}
1131
1132
VCL_BOOL v_matchproto_(td_xyzzy_validhdr)
1133 2
xyzzy_validhdr(VRT_CTX, VCL_STRANDS s)
1134
{
1135 2
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
1136 2
        return (VRT_ValidHdr(ctx, s));
1137
}
1138
1139
VCL_REGEX v_matchproto_(td_xyzzy_regex)
1140 6
xyzzy_just_return_regex(VRT_CTX, VCL_REGEX r)
1141
{
1142
1143 6
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
1144 6
        AN(r);
1145 6
        return (r);
1146
}
1147
1148
/*---------------------------------------------------------------------*/
1149
1150
VCL_VOID v_matchproto_(td_xyzzy_call)
1151 12
xyzzy_call(VRT_CTX, VCL_SUB sub)
1152
{
1153 12
        VRT_call(ctx, sub);
1154 12
}
1155
1156
VCL_STRING v_matchproto_(td_xyzzy_check_call)
1157 1
xyzzy_check_call(VRT_CTX, VCL_SUB sub)
1158
{
1159 1
        return (VRT_check_call(ctx, sub));
1160
}
1161
1162
/* the next two are to test WRONG vmod behavior:
1163
 * holding a VCL_SUB reference across vcls
1164
 */
1165
1166
static VCL_SUB wrong = NULL;
1167
1168
VCL_VOID v_matchproto_(td_xyzzy_bad_memory)
1169 0
xyzzy_bad_memory(VRT_CTX, VCL_SUB sub)
1170
{
1171 0
        (void) ctx;
1172
1173 0
        wrong = sub;
1174 0
}
1175
1176
VCL_SUB v_matchproto_(td_xyzzy_total_recall)
1177 0
xyzzy_total_recall(VRT_CTX)
1178
{
1179 0
        (void) ctx;
1180
1181 0
        return (wrong);
1182
}
1183
1184
/*---------------------------------------------------------------------*/
1185
1186
struct VPFX(debug_caller) {
1187
       unsigned        magic;
1188
#define DEBUG_CALLER_MAGIC 0xb47f3449
1189
       VCL_SUB         sub;
1190
};
1191
1192
VCL_VOID v_matchproto_(td_xyzzy_debug_caller__init)
1193 2
xyzzy_caller__init(VRT_CTX, struct VPFX(debug_caller) **callerp,
1194
    const char *name, VCL_SUB sub)
1195
{
1196
        struct VPFX(debug_caller) *caller;
1197
1198 2
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
1199 2
        AN(callerp);
1200 2
        AZ(*callerp);
1201 2
        AN(name);
1202 2
        AN(sub);
1203
1204 2
        ALLOC_OBJ(caller, DEBUG_CALLER_MAGIC);
1205 2
        AN(caller);
1206 2
        *callerp = caller;
1207 2
        caller->sub = sub;
1208 2
}
1209
1210
VCL_VOID v_matchproto_(td_xyzzy_debug_caller__fini)
1211 0
xyzzy_caller__fini(struct VPFX(debug_caller) **callerp)
1212
{
1213
        struct VPFX(debug_caller) *caller;
1214
1215 0
        if (callerp == NULL || *callerp == NULL)
1216 0
                return;
1217 0
        TAKE_OBJ_NOTNULL(caller, callerp, DEBUG_CALLER_MAGIC);
1218 0
        FREE_OBJ(caller);
1219 0
}
1220
1221
VCL_VOID v_matchproto_(td_xyzzy_debug_caller_call)
1222 1
xyzzy_caller_call(VRT_CTX, struct VPFX(debug_caller) *caller)
1223
{
1224 1
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
1225 1
        CHECK_OBJ_NOTNULL(caller, DEBUG_CALLER_MAGIC);
1226 1
        AN(caller->sub);
1227
1228 1
        VRT_call(ctx, caller->sub);
1229 1
}
1230
1231
VCL_SUB v_matchproto_(td_xyzzy_debug_caller_sub)
1232 1
xyzzy_caller_xsub(VRT_CTX, struct VPFX(debug_caller) *caller)
1233
{
1234 1
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
1235 1
        CHECK_OBJ_NOTNULL(caller, DEBUG_CALLER_MAGIC);
1236 1
        AN(caller->sub);
1237
1238 1
        return (caller->sub);
1239
}
1240
1241
struct resolve_priv {
1242
        struct vsb vsb[1];
1243
        const char *fail_port;
1244
        const char *errp[1];
1245
};
1246
1247
static int v_matchproto_(vus_resolved_f)
1248 46
resolve_cb(void *priv, const struct suckaddr *sa)
1249
{
1250
        struct resolve_priv *p;
1251
        char abuf[VTCP_ADDRBUFSIZE], pbuf[VTCP_PORTBUFSIZE];
1252
1253 46
        p = (struct resolve_priv *)priv;
1254 46
        CHECK_OBJ_NOTNULL(p->vsb, VSB_MAGIC);
1255 46
        AN(sa);
1256 46
        VTCP_name(sa, abuf, sizeof abuf, pbuf, sizeof pbuf);
1257 46
        if (p->fail_port != NULL && !strcmp(p->fail_port, pbuf)) {
1258 5
                *(p->errp) = "bad port";
1259 5
                return (-1);
1260
        }
1261 41
        VSB_printf(p->vsb, "%s%s:%s", VSB_len(p->vsb) ? ", " : "", abuf, pbuf);
1262 41
        return (0);
1263 46
}
1264
1265
VCL_STRING
1266 48
xyzzy_resolve_range(VRT_CTX, struct VARGS(resolve_range) *args)
1267
{
1268
        struct resolve_priv p;
1269
        const char *def_port;
1270
        int ret;
1271
1272 48
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
1273 48
        if (args->addr == NULL)
1274 1
                return ("vmod-debug: s was NULL");
1275 47
        memset(&p, 0, sizeof p);
1276 47
        WS_VSB_new(p.vsb, ctx->ws);
1277 47
        p.fail_port = args->valid_fail_port ? args->fail_port : NULL;
1278 47
        def_port = args->valid_def_port ? args->def_port : NULL;
1279 47
        ret = VSS_resolver_range(args->addr, def_port, resolve_cb, &p, p.errp);
1280 47
        if (ret)
1281 54
                VSB_printf(p.vsb, "%s%s", VSB_len(p.vsb) ? ", " : "Failed: ",
1282 27
                    *(p.errp));
1283 47
        return (WS_VSB_finish(p.vsb, ctx->ws, NULL));
1284 48
}
1285
1286
VCL_VOID
1287 32
xyzzy_use_reembarking_http1(VRT_CTX)
1288
{
1289 32
        debug_transport_reembarking_http1_use(ctx);
1290 32
}
1291
1292
VCL_VOID
1293 48
xyzzy_use_vai_http1(VRT_CTX)
1294
{
1295 48
        debug_transport_vai_use(ctx);
1296 48
}
1297
1298
static int
1299 4
in_oc(struct worker *wrk, struct objcore *oc, const char *p)
1300
{
1301
        const char *hdrs;
1302 4
        ssize_t len = 0;
1303
1304 4
        if (oc == NULL)
1305 2
                return (0);
1306 2
        hdrs = ObjGetAttr(wrk, oc, OA_HEADERS, &len);
1307 2
        if (hdrs == NULL)
1308 0
                return (0);
1309 2
        if (p < hdrs)
1310 0
                return (0);
1311 2
        if (p > hdrs + len)
1312 0
                return (0);
1313 2
        return (1);
1314 4
}
1315
1316
static const char *
1317 7
ptr_where(VRT_CTX, const char *p)
1318
{
1319 7
        struct ws *ws = ctx->ws;
1320
        struct ws *aws;
1321
        struct worker *wrk;
1322
        struct objcore *oc, *stale_oc;
1323
1324 7
        if (ctx->req != NULL) {
1325 7
                wrk = ctx->req->wrk;
1326 7
                oc = ctx->req->objcore;
1327 7
                stale_oc = ctx->req->stale_oc;
1328 7
        }
1329 0
        else if (ctx->bo != NULL) {
1330 0
                wrk = ctx->bo->wrk;
1331 0
                oc = ctx->bo->fetch_objcore;
1332 0
                stale_oc = ctx->bo->stale_oc;
1333 0
        }
1334
        else
1335 0
                WRONG("ctx");
1336
1337 7
        AN(wrk);
1338 7
        aws = wrk->aws;
1339
1340 7
        if (WS_Allocated(ws, p, -1))
1341 4
                return ("ws");
1342 3
        if (WS_Allocated(aws, p, -1))
1343 0
                return ("aws");
1344 3
        if (in_oc(wrk, oc, p))
1345 2
                return ("oc");
1346 1
        if (in_oc(wrk, stale_oc, p))
1347 0
                return ("stale_oc");
1348 1
        return ("?");
1349 7
}
1350
1351
VCL_VOID
1352 5
xyzzy_log_strands(VRT_CTX, VCL_STRING prefix, VCL_STRANDS subject, VCL_INT nn)
1353
{
1354
        int i, n;
1355
1356 5
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
1357 5
        if (prefix == NULL)
1358 0
                prefix = "";
1359 5
        AN(subject);
1360 5
        if (nn > INT_MAX)
1361 0
                n = INT_MAX;
1362 5
        else if (nn < 0)
1363 0
                n = 0;
1364
        else
1365 5
                n = nn;
1366
1367 12
        for (i = 0; i < subject->n; i++) {
1368 7
                const char *p = subject->p[i];
1369 14
                mylog(ctx->vsl, SLT_Debug, "%s[%d]: (%s) %p %.*s%s", prefix, i,
1370 7
                    ptr_where(ctx, p), p, n, p, strlen(p) > (unsigned)n ? "..." : "");
1371 7
        }
1372 5
}