1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
/*-
* Copyright (c) 2013-2021 Varnish Software AS
* All rights reserved.
*
* Author: Poul-Henning Kamp <phk@FreeBSD.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include "cache/cache.h"
#include "vcl.h"
#include "vcc_debug_if.h"
// vmod_debug.c
extern void mylog(struct vsl_log *vsl, enum VSL_tag_e tag,
const char *fmt, ...) v_printflike_(3,4);
struct xyzzy_debug_obj {
unsigned magic;
#define VMOD_DEBUG_OBJ_MAGIC 0xccbd9b77
int foobar;
const char *string;
const char *number;
VCL_STRING vcl_name;
};
VCL_VOID
xyzzy_obj__init(VRT_CTX, struct xyzzy_debug_obj **op,
const char *vcl_name, VCL_STRING s, VCL_ENUM e)
{
struct xyzzy_debug_obj *o;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
AN(op);
AZ(*op);
if (! strcmp(s, "fail")) {
VRT_fail(ctx, "failing as requested");
}
ALLOC_OBJ(o, VMOD_DEBUG_OBJ_MAGIC);
AN(o);
*op = o;
o->foobar = 42;
o->string = s;
o->number = e;
o->vcl_name = vcl_name;
AN(*op);
}
VCL_VOID v_matchproto_(td_xyzzy_obj__fini)
xyzzy_obj__fini(struct xyzzy_debug_obj **op)
{
struct xyzzy_debug_obj *o;
TAKE_OBJ_NOTNULL(o, op, VMOD_DEBUG_OBJ_MAGIC);
FREE_OBJ(o);
}
VCL_VOID v_matchproto_(td_xyzzy_obj_enum)
xyzzy_obj_enum(VRT_CTX, struct xyzzy_debug_obj *o, VCL_ENUM e)
{
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(o, VMOD_DEBUG_OBJ_MAGIC);
assert(!strcmp(e, "martin"));
}
VCL_VOID v_matchproto_(td_xyzzy_obj_enum)
xyzzy_obj_obj(VRT_CTX, struct xyzzy_debug_obj *o)
{
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(o, VMOD_DEBUG_OBJ_MAGIC);
}
VCL_STRING v_matchproto_()
xyzzy_obj_foo(VRT_CTX, struct xyzzy_debug_obj *o, VCL_STRING s)
{
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
(void)s;
CHECK_OBJ_NOTNULL(o, VMOD_DEBUG_OBJ_MAGIC);
assert(o->foobar == 42);
return ("BOO");
}
VCL_TIME v_matchproto_()
xyzzy_obj_date(VRT_CTX, struct xyzzy_debug_obj *o)
{
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(o, VMOD_DEBUG_OBJ_MAGIC);
assert(o->foobar == 42);
return (21.4);
}
VCL_STRING v_matchproto_()
xyzzy_obj_string(VRT_CTX, struct xyzzy_debug_obj *o)
{
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(o, VMOD_DEBUG_OBJ_MAGIC);
assert(o->foobar == 42);
return (o->string);
}
VCL_STRING v_matchproto_()
xyzzy_obj_number(VRT_CTX, struct xyzzy_debug_obj *o)
{
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(o, VMOD_DEBUG_OBJ_MAGIC);
assert(o->foobar == 42);
return (o->number);
}
VCL_VOID v_matchproto_()
xyzzy_obj_test_priv_call(VRT_CTX,
struct xyzzy_debug_obj *o, struct vmod_priv *priv)
{
(void)o;
xyzzy_test_priv_call(ctx, priv);
}
VCL_VOID v_matchproto_()
xyzzy_obj_test_priv_vcl(VRT_CTX,
struct xyzzy_debug_obj *o, struct vmod_priv *priv)
{
(void)o;
xyzzy_test_priv_vcl(ctx, priv);
}
#define PRIV_FINI(name) \
static void v_matchproto_(vmod_priv_fini_f) \
obj_priv_ ## name ## _fini(VRT_CTX, void *ptr) \
{ \
const char * const fmt = "obj_priv_" #name "_fini(%p)"; \
\
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); \
AN(ptr); \
mylog(ctx->vsl, SLT_Debug, fmt, ptr); \
} \
\
static const struct vmod_priv_methods \
xyzzy_obj_test_priv_ ## name ## _methods[1] = {{ \
.magic = VMOD_PRIV_METHODS_MAGIC, \
.type = "debug_obj_test_priv_" #name, \
.fini = obj_priv_ ## name ## _fini \
}};
PRIV_FINI(task)
PRIV_FINI(top)
#undef PRIV_FINI
VCL_STRING v_matchproto_()
xyzzy_obj_test_priv_task(VRT_CTX, struct xyzzy_debug_obj *o, VCL_STRING s)
{
struct vmod_priv *p;
struct vsl_log *vsl;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
if (ctx->method & VCL_MET_TASK_H)
vsl = NULL;
else
vsl = ctx->vsl;
if (s == NULL || *s == '\0') {
p = VRT_priv_task_get(ctx, o);
if (p == NULL) {
mylog(vsl, SLT_Debug, "%s.priv_task() = NULL",
o->vcl_name);
return ("");
}
assert(p->methods == xyzzy_obj_test_priv_task_methods);
mylog(vsl, SLT_Debug,
"%s.priv_task() = %p .priv = %p (\"%s\")",
o->vcl_name, p, p->priv, (char *)p->priv);
return (p->priv);
}
p = VRT_priv_task(ctx, o);
if (p == NULL) {
mylog(vsl, SLT_Debug, "%s.priv_task() = NULL [err]",
o->vcl_name);
VRT_fail(ctx, "no priv task - out of ws?");
return ("");
}
mylog(vsl, SLT_Debug,
"%s.priv_task() = %p .priv = %p (\"%s\") [%s]",
o->vcl_name, p, s, s, p->priv ? "update" : "new");
if (p->priv == NULL)
p->methods = xyzzy_obj_test_priv_task_methods;
else
assert(p->methods == xyzzy_obj_test_priv_task_methods);
/* minimum scope of s and priv is the task - no need to copy */
p->priv = TRUST_ME(s);
return (p->priv);
}
VCL_STRING v_matchproto_()
xyzzy_obj_test_priv_top(VRT_CTX, struct xyzzy_debug_obj *o, VCL_STRING s)
{
struct vmod_priv *p;
struct req *req;
struct ws *ws;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
req = ctx->req;
if (req == NULL) {
VRT_fail(ctx, "%s.priv_top() can only be used "
"in client VCL context", o->vcl_name);
return ("");
}
CHECK_OBJ(req, REQ_MAGIC);
if (s == NULL || *s == '\0') {
p = VRT_priv_top_get(ctx, o);
if (p == NULL) {
VSLb(ctx->vsl, SLT_Debug, "%s.priv_top() = NULL",
o->vcl_name);
return ("");
}
assert(p->methods == xyzzy_obj_test_priv_top_methods);
VSLb(ctx->vsl, SLT_Debug,
"%s.priv_top() = %p .priv = %p (\"%s\")",
o->vcl_name, p, p->priv, (char *)p->priv);
return (p->priv);
}
p = VRT_priv_top(ctx, o);
if (p == NULL)
VSLb(ctx->vsl, SLT_Debug, "%s.priv_top() = NULL [err]",
o->vcl_name);
CHECK_OBJ_NOTNULL(req->top, REQTOP_MAGIC);
req = req->top->topreq;
CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
ws = req->ws;
/* copy to top req's workspace if need to */
if (ctx->ws != ws && WS_Allocated(ctx->ws, s, -1))
s = WS_Copy(ws, s, -1);
if (p == NULL || s == NULL) {
VRT_fail(ctx, "out of ws?");
return ("");
}
VSLb(ctx->vsl, SLT_Debug,
"%s.priv_top() = %p .priv = %p (\"%s\") [%s]",
o->vcl_name, p, s, s, p->priv ? "update" : "new");
if (p->priv == NULL)
p->methods = xyzzy_obj_test_priv_top_methods;
else
assert(p->methods == xyzzy_obj_test_priv_top_methods);
p->priv = TRUST_ME(s);
return (p->priv);
}
/* ----------------------------------------------------------------------------
* obj_opt (optional arguments and privs)
*/
struct xyzzy_debug_obj_opt {
unsigned magic;
#define VMOD_DEBUG_OBJ_OPT_MAGIC 0xccbd9b78
char *name;
struct VARGS(obj_opt_meth_opt) args;
void *freeptr;
};
VCL_VOID v_matchproto_()
xyzzy_obj_opt__init(VRT_CTX,
struct xyzzy_debug_obj_opt **op, const char *vcl_name,
struct VARGS(obj_opt__init) *args)
{
struct xyzzy_debug_obj_opt *o;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
AN(args);
AN(args->arg1); // priv_call
AN(args->arg2); // priv_vcl
AN(args->arg3); // priv_task
assert(args->arg1 != args->arg2);
assert(args->arg2 != args->arg3);
if (args->valid_s)
AN(args->s);
(void)args->valid_b;
(void)args->b;
AN(op);
AZ(*op);
ALLOC_OBJ(o, VMOD_DEBUG_OBJ_OPT_MAGIC);
AN(o);
*op = o;
REPLACE(o->name, vcl_name);
memcpy(&o->args, args, sizeof o->args);
if (args->valid_s) {
REPLACE(o->freeptr, args->s);
o->args.s = o->freeptr;
}
}
VCL_VOID v_matchproto_()
xyzzy_obj_opt__fini(struct xyzzy_debug_obj_opt **op)
{
struct xyzzy_debug_obj_opt *o;
TAKE_OBJ_NOTNULL(o, op, VMOD_DEBUG_OBJ_OPT_MAGIC);
REPLACE(o->name, NULL);
if (o->freeptr) {
AN(o->args.valid_s);
REPLACE(o->freeptr, NULL);
}
FREE_OBJ(o);
}
VCL_STRING v_matchproto_()
xyzzy_obj_opt_meth_opt(VRT_CTX,
struct xyzzy_debug_obj_opt *o,
struct VARGS(obj_opt_meth_opt) *args)
{
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(o, VMOD_DEBUG_OBJ_OPT_MAGIC);
AN(args);
AN(args->arg1); // priv_call
AN(args->arg2); // priv_vcl
AN(args->arg3); // priv_task
assert(args->arg1 != args->arg2);
assert(args->arg2 != args->arg3);
return (WS_Printf(ctx->ws,
"obj %s obj_s %s obj_b %s met_s %s met_b %s",
o->name,
(o->args.valid_s ? o->args.s : "*undef*"),
(o->args.valid_b
? (o->args.b ? "true" : "false" )
: "*undef*"),
(args->valid_s ? args->s : "*undef*"),
(args->valid_b
? (args->b ? "true" : "false" )
: "*undef*")
));
}
|