9 /* this contains the code to prepare the kernel data structures and to parse records */
15 /* we could just rely on the types in the expression tree but i'm paranoid */
16 typedef struct Val Val;
37 case VALINT: r.v = va_arg(va, uvlong); break;
38 case VALSTR: r.s = va_arg(va, char*); break;
47 clause = emalloc(sizeof(Clause));
48 clause->id = nclauses;
54 clause->probs = erealloc(clause->probs, sizeof(char *) * (clause->nprob + 1));
55 clause->probs[clause->nprob++] = strdup(s);
58 static char *aggtypes[] = {
72 for(i = 0; i < nelem(aggtypes); i++)
73 if(strcmp(s->name, aggtypes[i]) == 0)
75 error("%s unknown aggregation type", s->name);
80 addstat(int type, ...)
85 clause->stats = erealloc(clause->stats, sizeof(Stat) * (clause->nstats + 1));
86 s = &clause->stats[clause->nstats++];
87 memset(s, 0, sizeof(Stat));
92 s->n = va_arg(va, Node *);
98 s->agg.name = va_arg(va, Symbol *);
99 s->agg.key = va_arg(va, Node *);
100 s->agg.type = aggtype(va_arg(va, Symbol *));
101 s->agg.value = va_arg(va, Node *);
102 if(s->agg.type == AGGCNT){
103 if(s->agg.value != nil)
104 error("too many arguments for count()");
106 if(s->agg.value == nil)
107 error("need argument for %s()", aggtypes[s->agg.type]);
111 sysfatal("addstat: unknown type %d", type);
121 assert(clause->nstats > 0);
122 s = &clause->stats[clause->nstats - 1];
123 s->arg = erealloc(s->arg, sizeof(Node *) * (s->narg + 1));
124 s->arg[s->narg++] = n;
130 clauses = erealloc(clauses, sizeof(Clause) * (nclauses + 1));
131 clauses[nclauses++] = clause;
135 actgradd(DTActGr *a, DTAct b)
137 a->acts = erealloc(a->acts, sizeof(DTAct) * (a->nact + 1));
138 a->acts[a->nact++] = b;
148 prepprintf(Node **arg, int narg, DTActGr *g, int *recoff)
154 if(narg <= 0) sysfatal("printf() needs an argument");
155 if((*arg)->type != OSTR) sysfatal("printf() format string must be a literal");
159 for(; *fmt != 0; fmt++){
166 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
167 case 'u': case '+': case '-': case ',': case '#': case ' ': case '.':
171 case 'x': case 'X': case 'o': case 'b': case 'd':
172 if(n >= narg) sysfatal("printf() too few arguments");
173 if(arg[n]->typ->type != TYPINT) sysfatal("print() %%%c with non-integer", *fmt);
174 arg[n] = tracegen(arg[n], g, recoff);
181 if(n >= narg) sysfatal("printf() too few arguments");
182 if(arg[n]->typ->type != TYPSTRING) sysfatal("print() %%s with non-string");
183 arg[n] = tracegen(arg[n], g, recoff);
187 case 0: sysfatal("printf() missing verb");
188 default: sysfatal("printf() unknown verb %%%c", *fmt);
191 if(n < narg) sysfatal("printf() too many arguments");
192 (*arg)->str = fmtstrflush(&f);
202 for(s = c->stats; s < c->stats + c->nstats; s++)
203 if(s->type != STATAGG)
209 mkdtclause(Clause *c)
216 d = emalloc(sizeof(DTClause));
219 d->gr = emalloc(sizeof(DTActGr));
220 d->gr->pred = c->pred;
223 for(s = c->stats; s < c->stats + c->nstats; s++)
226 actgradd(d->gr, (DTAct){ACTTRACE, codegen(s->n), 0, noagg});
229 for(i = 0; i < s->narg; i++)
230 s->arg[i] = tracegen(s->arg[i], d->gr, &recoff);
233 prepprintf(s->arg, s->narg, d->gr, &recoff);
236 DTAgg agg = {.id = s->agg.type << 28 | 1 << 16 | aggid++};
237 assert(dtaunpackid(&agg) >= 0);
238 aggs = realloc(aggs, sizeof(Agg) * aggid);
239 memset(&aggs[aggid-1], 0, sizeof(Agg));
240 aggs[aggid-1].DTAgg = agg;
241 aggs[aggid-1].name = strdup(s->agg.name == nil ? "" : s->agg.name->name);
242 actgradd(d->gr, (DTAct){ACTAGGKEY, codegen(s->agg.key), 8, agg});
244 if(n == nil) n = node(ONUM, 0ULL);
245 actgradd(d->gr, (DTAct){ACTAGGVAL, codegen(n), 8, agg});
250 actgradd(d->gr, (DTAct){ACTCANCEL, codegen(node(ONUM, 0)), 0, noagg});
260 for(i = 0; i < nclauses; i++){
261 d = mkdtclause(clauses[i]);
266 /* epid lookup table, filled with info from the kernel */
270 addepid(u32int epid, u32int cid, int reclen, char *p)
274 assert(cid < nclauses);
275 assert((uint)reclen >= 12);
276 e = emalloc(sizeof(Enab));
278 e->cl = clauses[cid];
280 e->probe = strdup(p);
281 ep = &enabtab[epid % nelem(enabtab)];
287 epidlookup(u32int epid)
291 for(e = enabtab[epid % nelem(enabtab)]; e != nil; e = e->next)
298 unpack(uchar *p, uchar *e, char *fmt, ...)
307 if(p + 1 > e) return nil;
308 *va_arg(va, u8int *) = p[0];
312 if(p + 2 > e) return nil;
313 *va_arg(va, u16int *) = p[0] | p[1] << 8;
317 if(p + 4 > e) return nil;
318 *va_arg(va, u32int *) = p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
322 if(p + 8 > e) return nil;
323 vl = p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
324 vl |= (uvlong)p[4] << 32 | (uvlong)p[5] << 40 | (uvlong)p[6] << 48 | (uvlong)p[7] << 56;
325 *va_arg(va, u64int *) = vl;
336 receval(Node *n, uchar *p, uchar *e, Enab *en)
348 switch(n->sym->type){
352 q = unpack(p + 4, e, "v", &v);
354 return mkval(VALINT, v);
356 return mkval(VALSTR, en->probe);
357 default: sysfatal("receval: unknown variable %d", n->type); return mkval(VALINT, 0LL);
360 default: sysfatal("receval: unknown symbol type %d", n->type); return mkval(VALINT, 0LL);
362 case ONUM: return mkval(VALINT, n->num);
364 a = receval(n->n1, p, e, en);
365 b = receval(n->n2, p, e, en);
366 assert(a.type == VALINT);
367 assert(b.type == VALINT);
368 return mkval(VALINT, evalop(n->op, n->typ->sign, a.v, b.v));
370 a = receval(n->n1, p, e, en);
371 assert(a.type == VALINT);
372 return mkval(VALINT, (uvlong) !a.v);
374 a = receval(n->n1, p, e, en);
375 assert(a.type == VALINT);
376 return a.v ? receval(n->n2, p, e, en) : receval(n->n3, p, e, en);
378 switch(n->typ->type){
380 switch(n->typ->size){
381 case 1: q = unpack(p + n->num, e, "c", &c); v = n->typ->sign ? (s8int)c : (u8int)c; break;
382 case 2: q = unpack(p + n->num, e, "s", &s); v = n->typ->sign ? (s16int)s : (u16int)s; break;
383 case 4: q = unpack(p + n->num, e, "i", &i); v = n->typ->sign ? (s32int)i : (u32int)i; break;
384 case 8: q = unpack(p + n->num, e, "v", &v); break;
388 return mkval(VALINT, v);
390 assert(p + n->num + n->typ->size <= e);
391 sp = emalloc(n->typ->size + 1);
392 memcpy(sp, p + n->num, n->typ->size);
393 return mkval(VALSTR, sp); /* TODO: fix leak */
395 sysfatal("receval: don't know how to parse record for %τ", n->typ);
398 sysfatal("receval: unknown type %α", n->type);
399 return mkval(VALINT, 0LL);
404 execprintf(Node **arg, int narg, uchar *p, uchar *e, Enab *en)
410 x = emalloc(sizeof(uvlong) * (narg - 1));
412 for(i = 0; i < narg - 1; i++){
413 v = receval(arg[i + 1], p, e, en);
417 xp += sizeof(uvlong);
426 vfprint(1, (*arg)->str, (va_list) x);
431 parseclause(Clause *cl, uchar *p, uchar *e, Enab *en, Biobuf *bp)
437 for(s = cl->stats; s < cl->stats + cl->nstats; s++)
439 case STATEXPR: break;
441 for(i = 0; i < s->narg; i++){
442 v = receval(s->arg[i], p, e, en);
445 Bprint(bp, "%lld", v.v);
448 Bprint(bp, "%s", v.s);
450 default: sysfatal("parseclause: unknown val type %d", s->type);
452 Bprint(bp, "%c", i == s->narg - 1 ? '\n' : ' ');
456 execprintf(s->arg, s->narg, p, e, en);
460 sysfatal("parseclause: unknown type %d", s->type);
466 parsefault(uchar *p0, uchar *e)
474 p = unpack(p0, e, "csci", &type, &n, &dummy, &epid);
475 if(p == nil) return nil;
476 en = epidlookup(epid);
482 p = unpack(p, e, "iv", &pid, &addr);
483 if(p == nil) return nil;
484 fprint(2, "dtracy: illegal access: probe=%s, pid=%d, addr=%#llx\n", en != nil ? en->probe : nil, pid, addr);
488 fprint(2, "dtracy: unknown fault type %#.2ux\n", type);
494 parsebuf(uchar *p, int n, Biobuf *bp)
503 p = unpack(p, e, "iv", &epid, &ts);
504 if(p == nil) goto err;
505 if(epid == (u32int)-1){
506 p = parsefault(p, e);
507 if(p == nil) goto err;
510 en = epidlookup(epid);
511 if(en == nil) goto err;
512 if(parseclause(en->cl, p - 12, p + en->reclen - 12, en, bp) < 0) return -1;
513 p += en->reclen - 12;
517 werrstr("buffer invalid");
522 dumpexpr(DTExpr *e, char *prefix)
526 for(i = 0; i < e->n; i++)
527 print("%s%.8ux %I\n", prefix, e->b[i], e->b[i]);
530 #pragma varargck type "ε" Node*
533 fmtstring(Fmt *f, char *s)
538 case '\n': fmtprint(f, "\\n"); break;
539 case '\r': fmtprint(f, "\\r"); break;
540 case '\t': fmtprint(f, "\\t"); break;
541 case '\v': fmtprint(f, "\\v"); break;
542 case '\b': fmtprint(f, "\\b"); break;
543 case '\a': fmtprint(f, "\\a"); break;
544 case '"': fmtprint(f, "\""); break;
545 case '\\': fmtprint(f, "\\"); break;
547 if(*s < 0x20 || *s >= 0x7f)
548 fmtprint(f, "\\%.3o", (uchar)*s);
555 typedef struct Op Op;
559 enum { PRECRIGHT = 1 } flags;
561 static Op optab[] = {
562 [OPLOR] {"||", 3, 0},
563 [OPLAND] {"&&", 4, 0},
565 [OPXNOR] {"~^", 6, 0},
572 [OPLSH] {"<<", 10, 0},
573 [OPRSH] {">>", 10, 0},
574 [OPADD] {"+", 11, 0},
575 [OPSUB] {"-", 11, 0},
576 [OPDIV] {"/", 12, 0},
577 [OPMOD] {"%", 12, 0},
578 [OPMUL] {"*", 12, 0},
580 enum { PREDUNARY = 14 };
590 n = va_arg(f->args, Node *);
592 case OSYM: fmtprint(f, "%s", n->sym->name); break;
593 case ONUM: fmtprint(f, "%lld", n->num); break;
594 case OSTR: fmtstring(f, n->str); break;
596 if(n->op >= nelem(optab) || optab[n->op].name == nil)
597 fmtprint(f, "(%*ε ??op%d %*ε)", PREDUNARY, n->n1, n->op, PREDUNARY, n->n2);
600 if(op->pred < p) fmtrune(f, '(');
601 fmtprint(f, "%*ε %s %*ε", op->pred + (op->flags & PRECRIGHT), n->n1, op->name, op->pred + (~op->flags & PRECRIGHT), n->n2);
602 if(op->pred < p) fmtrune(f, ')');
605 case OLNOT: fmtprint(f, "!%*ε", PREDUNARY, n->n1); break;
606 case OTERN: fmtprint(f, "%2ε ? %1ε : %1ε", n->n1, n->n2, n->n3); break;
607 case ORECORD: fmtprint(f, "record(%ε, %τ, %d)", n->n1, n->typ, (int)n->num); break;
608 case OCAST: fmtprint(f, "(%τ) %*ε", n->typ, PREDUNARY, n->n1); break;
609 default: fmtprint(f, "??? %α", n->type);
623 for(i = 0; i < nclauses; i++){
626 print("clause %d:\n", c->id);
627 for(j = 0; j < c->nprob; j++)
628 print("\tprobe '%s'\n", c->probs[j]);
629 print("\tkernel code:\n");
631 print("\t\tno predicate\n");
633 print("\t\tpredicate\n");
634 dumpexpr(c->pred, "\t\t\t");
636 for(a = d->gr->acts; a < d->gr->acts + d->gr->nact; a++)
639 print("\t\ttrace (%d bytes)\n", a->size);
640 dumpexpr(a->p, "\t\t\t");
643 print("\t\ttrace string (%d bytes)\n", a->size);
644 dumpexpr(a->p, "\t\t\t");
647 print("\t\taggregation key (%s,%d,%d)\n", a->agg.type >= nelem(aggtypes) ? "???" : aggtypes[a->agg.type], a->agg.keysize, (u16int)a->agg.id);
648 dumpexpr(a->p, "\t\t\t");
651 print("\t\taggregation value (%s,%d,%d)\n", a->agg.type >= nelem(aggtypes) ? "???" : aggtypes[a->agg.type], a->agg.keysize, (u16int)a->agg.id);
652 dumpexpr(a->p, "\t\t\t");
655 print("\t\tcancel record\n");
658 print("\t\t??? %d\n", a->type);
660 print("\trecord formatting:\n");
661 for(s = c->stats; s < c->stats + c->nstats; s++)
666 print("\t\tprint\n");
667 for(j = 0; j < s->narg; j++)
668 print("\t\t\targ %ε\n", s->arg[j]);
671 print("\t\tprintf\n");
672 for(j = 0; j < s->narg; j++)
673 print("\t\t\targ %ε\n", s->arg[j]);
678 print("\t\t??? %d\n", s->type);