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;
63 return fmtstrflush(&f);
69 clause = emalloc(sizeof(Clause));
70 clause->id = nclauses;
76 clause->probs = erealloc(clause->probs, sizeof(char *) * (clause->nprob + 1));
77 clause->probs[clause->nprob++] = insertstars(s);
80 static char *aggtypes[] = {
94 for(i = 0; i < nelem(aggtypes); i++)
95 if(strcmp(s->name, aggtypes[i]) == 0)
97 error("%s unknown aggregation type", s->name);
102 addstat(int type, ...)
107 clause->stats = erealloc(clause->stats, sizeof(Stat) * (clause->nstats + 1));
108 s = &clause->stats[clause->nstats++];
109 memset(s, 0, sizeof(Stat));
114 s->n = va_arg(va, Node *);
120 s->agg.name = va_arg(va, Symbol *);
121 s->agg.key = va_arg(va, Node *);
122 s->agg.type = aggtype(va_arg(va, Symbol *));
123 s->agg.value = va_arg(va, Node *);
124 if(s->agg.type == AGGCNT){
125 if(s->agg.value != nil)
126 error("too many arguments for count()");
128 if(s->agg.value == nil)
129 error("need argument for %s()", aggtypes[s->agg.type]);
133 sysfatal("addstat: unknown type %d", type);
143 assert(clause->nstats > 0);
144 s = &clause->stats[clause->nstats - 1];
145 s->arg = erealloc(s->arg, sizeof(Node *) * (s->narg + 1));
146 s->arg[s->narg++] = n;
152 clauses = erealloc(clauses, sizeof(Clause) * (nclauses + 1));
153 clauses[nclauses++] = clause;
157 actgradd(DTActGr *a, DTAct b)
159 a->acts = erealloc(a->acts, sizeof(DTAct) * (a->nact + 1));
160 a->acts[a->nact++] = b;
170 prepprintf(Node **arg, int narg, DTActGr *g, int *recoff)
176 if(narg <= 0) sysfatal("printf() needs an argument");
177 if((*arg)->type != OSTR) sysfatal("printf() format string must be a literal");
181 for(; *fmt != 0; fmt++){
188 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
189 case 'u': case '+': case '-': case ',': case '#': case ' ': case '.':
193 case 'x': case 'X': case 'o': case 'b': case 'd':
194 if(n >= narg) sysfatal("printf() too few arguments");
195 if(arg[n]->typ->type != TYPINT) sysfatal("print() %%%c with non-integer", *fmt);
196 arg[n] = tracegen(arg[n], g, recoff);
203 if(n >= narg) sysfatal("printf() too few arguments");
204 if(arg[n]->typ->type != TYPSTRING) sysfatal("print() %%s with non-string");
205 arg[n] = tracegen(arg[n], g, recoff);
209 case 0: sysfatal("printf() missing verb");
210 default: sysfatal("printf() unknown verb %%%c", *fmt);
213 if(n < narg) sysfatal("printf() too many arguments");
214 (*arg)->str = fmtstrflush(&f);
224 for(s = c->stats; s < c->stats + c->nstats; s++)
225 if(s->type != STATAGG)
231 mkdtclause(Clause *c)
238 d = emalloc(sizeof(DTClause));
241 d->gr = emalloc(sizeof(DTActGr));
242 d->gr->pred = c->pred;
245 for(s = c->stats; s < c->stats + c->nstats; s++)
248 actgradd(d->gr, (DTAct){ACTTRACE, codegen(s->n), 0, noagg});
251 for(i = 0; i < s->narg; i++)
252 s->arg[i] = tracegen(s->arg[i], d->gr, &recoff);
255 prepprintf(s->arg, s->narg, d->gr, &recoff);
258 DTAgg agg = {.id = s->agg.type << 28 | 1 << 16 | aggid++};
259 assert(dtaunpackid(&agg) >= 0);
260 aggs = realloc(aggs, sizeof(Agg) * aggid);
261 memset(&aggs[aggid-1], 0, sizeof(Agg));
262 aggs[aggid-1].DTAgg = agg;
263 aggs[aggid-1].name = strdup(s->agg.name == nil ? "" : s->agg.name->name);
264 actgradd(d->gr, (DTAct){ACTAGGKEY, codegen(s->agg.key), 8, agg});
266 if(n == nil) n = node(ONUM, 0ULL);
267 actgradd(d->gr, (DTAct){ACTAGGVAL, codegen(n), 8, agg});
272 actgradd(d->gr, (DTAct){ACTCANCEL, codegen(node(ONUM, 0)), 0, noagg});
282 for(i = 0; i < nclauses; i++){
283 d = mkdtclause(clauses[i]);
288 /* epid lookup table, filled with info from the kernel */
292 addepid(u32int epid, u32int cid, int reclen, char *p)
296 assert(cid < nclauses);
297 assert((uint)reclen >= 12);
298 e = emalloc(sizeof(Enab));
300 e->cl = clauses[cid];
302 e->probe = strdup(p);
303 ep = &enabtab[epid % nelem(enabtab)];
309 epidlookup(u32int epid)
313 for(e = enabtab[epid % nelem(enabtab)]; e != nil; e = e->next)
320 unpack(uchar *p, uchar *e, char *fmt, ...)
329 if(p + 1 > e) return nil;
330 *va_arg(va, u8int *) = p[0];
334 if(p + 2 > e) return nil;
335 *va_arg(va, u16int *) = p[0] | p[1] << 8;
339 if(p + 4 > e) return nil;
340 *va_arg(va, u32int *) = p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
344 if(p + 8 > e) return nil;
345 vl = p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
346 vl |= (uvlong)p[4] << 32 | (uvlong)p[5] << 40 | (uvlong)p[6] << 48 | (uvlong)p[7] << 56;
347 *va_arg(va, u64int *) = vl;
358 receval(Node *n, uchar *p, uchar *e, Enab *en)
370 switch(n->sym->type){
374 q = unpack(p + 4, e, "v", &v);
376 return mkval(VALINT, v);
378 return mkval(VALSTR, en->probe);
379 default: sysfatal("receval: unknown variable %d", n->type); return mkval(VALINT, 0LL);
382 default: sysfatal("receval: unknown symbol type %d", n->type); return mkval(VALINT, 0LL);
384 case ONUM: return mkval(VALINT, n->num);
386 a = receval(n->n1, p, e, en);
387 b = receval(n->n2, p, e, en);
388 assert(a.type == VALINT);
389 assert(b.type == VALINT);
390 return mkval(VALINT, evalop(n->op, n->typ->sign, a.v, b.v));
392 a = receval(n->n1, p, e, en);
393 assert(a.type == VALINT);
394 return mkval(VALINT, (uvlong) !a.v);
396 a = receval(n->n1, p, e, en);
397 assert(a.type == VALINT);
398 return a.v ? receval(n->n2, p, e, en) : receval(n->n3, p, e, en);
400 switch(n->typ->type){
402 switch(n->typ->size){
403 case 1: q = unpack(p + n->num, e, "c", &c); v = n->typ->sign ? (s8int)c : (u8int)c; break;
404 case 2: q = unpack(p + n->num, e, "s", &s); v = n->typ->sign ? (s16int)s : (u16int)s; break;
405 case 4: q = unpack(p + n->num, e, "i", &i); v = n->typ->sign ? (s32int)i : (u32int)i; break;
406 case 8: q = unpack(p + n->num, e, "v", &v); break;
410 return mkval(VALINT, v);
412 assert(p + n->num + n->typ->size <= e);
413 sp = emalloc(n->typ->size + 1);
414 memcpy(sp, p + n->num, n->typ->size);
415 return mkval(VALSTR, sp); /* TODO: fix leak */
417 sysfatal("receval: don't know how to parse record for %τ", n->typ);
420 sysfatal("receval: unknown type %α", n->type);
421 return mkval(VALINT, 0LL);
426 execprintf(Node **arg, int narg, uchar *p, uchar *e, Enab *en)
432 x = emalloc(sizeof(uvlong) * (narg - 1));
434 for(i = 0; i < narg - 1; i++){
435 v = receval(arg[i + 1], p, e, en);
439 xp += sizeof(uvlong);
448 vfprint(1, (*arg)->str, (va_list) x);
453 parseclause(Clause *cl, uchar *p, uchar *e, Enab *en, Biobuf *bp)
459 for(s = cl->stats; s < cl->stats + cl->nstats; s++)
461 case STATEXPR: break;
463 for(i = 0; i < s->narg; i++){
464 v = receval(s->arg[i], p, e, en);
467 Bprint(bp, "%lld", v.v);
470 Bprint(bp, "%s", v.s);
472 default: sysfatal("parseclause: unknown val type %d", s->type);
474 Bprint(bp, "%c", i == s->narg - 1 ? '\n' : ' ');
478 execprintf(s->arg, s->narg, p, e, en);
482 sysfatal("parseclause: unknown type %d", s->type);
488 parsefault(uchar *p0, uchar *e)
496 p = unpack(p0, e, "csci", &type, &n, &dummy, &epid);
497 if(p == nil) return nil;
498 en = epidlookup(epid);
504 p = unpack(p, e, "iv", &pid, &addr);
505 if(p == nil) return nil;
506 fprint(2, "dtracy: illegal access: probe=%s, pid=%d, addr=%#llx\n", en != nil ? en->probe : nil, pid, addr);
510 fprint(2, "dtracy: unknown fault type %#.2ux\n", type);
516 parsebuf(uchar *p, int n, Biobuf *bp)
525 p = unpack(p, e, "iv", &epid, &ts);
526 if(p == nil) goto err;
527 if(epid == (u32int)-1){
528 p = parsefault(p, e);
529 if(p == nil) goto err;
532 en = epidlookup(epid);
533 if(en == nil) goto err;
534 if(parseclause(en->cl, p - 12, p + en->reclen - 12, en, bp) < 0) return -1;
535 p += en->reclen - 12;
539 werrstr("buffer invalid");
544 dumpexpr(DTExpr *e, char *prefix)
548 for(i = 0; i < e->n; i++)
549 print("%s%.8ux %I\n", prefix, e->b[i], e->b[i]);
552 #pragma varargck type "ε" Node*
555 fmtstring(Fmt *f, char *s)
560 case '\n': fmtprint(f, "\\n"); break;
561 case '\r': fmtprint(f, "\\r"); break;
562 case '\t': fmtprint(f, "\\t"); break;
563 case '\v': fmtprint(f, "\\v"); break;
564 case '\b': fmtprint(f, "\\b"); break;
565 case '\a': fmtprint(f, "\\a"); break;
566 case '"': fmtprint(f, "\""); break;
567 case '\\': fmtprint(f, "\\"); break;
569 if(*s < 0x20 || *s >= 0x7f)
570 fmtprint(f, "\\%.3o", (uchar)*s);
577 typedef struct Op Op;
581 enum { PRECRIGHT = 1 } flags;
583 static Op optab[] = {
584 [OPLOR] {"||", 3, 0},
585 [OPLAND] {"&&", 4, 0},
587 [OPXNOR] {"~^", 6, 0},
594 [OPLSH] {"<<", 10, 0},
595 [OPRSH] {">>", 10, 0},
596 [OPADD] {"+", 11, 0},
597 [OPSUB] {"-", 11, 0},
598 [OPDIV] {"/", 12, 0},
599 [OPMOD] {"%", 12, 0},
600 [OPMUL] {"*", 12, 0},
602 enum { PREDUNARY = 14 };
612 n = va_arg(f->args, Node *);
614 case OSYM: fmtprint(f, "%s", n->sym->name); break;
615 case ONUM: fmtprint(f, "%lld", n->num); break;
616 case OSTR: fmtstring(f, n->str); break;
618 if(n->op >= nelem(optab) || optab[n->op].name == nil)
619 fmtprint(f, "(%*ε ??op%d %*ε)", PREDUNARY, n->n1, n->op, PREDUNARY, n->n2);
622 if(op->pred < p) fmtrune(f, '(');
623 fmtprint(f, "%*ε %s %*ε", op->pred + (op->flags & PRECRIGHT), n->n1, op->name, op->pred + (~op->flags & PRECRIGHT), n->n2);
624 if(op->pred < p) fmtrune(f, ')');
627 case OLNOT: fmtprint(f, "!%*ε", PREDUNARY, n->n1); break;
628 case OTERN: fmtprint(f, "%2ε ? %1ε : %1ε", n->n1, n->n2, n->n3); break;
629 case ORECORD: fmtprint(f, "record(%ε, %τ, %d)", n->n1, n->typ, (int)n->num); break;
630 case OCAST: fmtprint(f, "(%τ) %*ε", n->typ, PREDUNARY, n->n1); break;
631 default: fmtprint(f, "??? %α", n->type);
645 for(i = 0; i < nclauses; i++){
648 print("clause %d:\n", c->id);
649 for(j = 0; j < c->nprob; j++)
650 print("\tprobe '%s'\n", c->probs[j]);
651 print("\tkernel code:\n");
653 print("\t\tno predicate\n");
655 print("\t\tpredicate\n");
656 dumpexpr(c->pred, "\t\t\t");
658 for(a = d->gr->acts; a < d->gr->acts + d->gr->nact; a++)
661 print("\t\ttrace (%d bytes)\n", a->size);
662 dumpexpr(a->p, "\t\t\t");
665 print("\t\ttrace string (%d bytes)\n", a->size);
666 dumpexpr(a->p, "\t\t\t");
669 print("\t\taggregation key (%s,%d,%d)\n", a->agg.type >= nelem(aggtypes) ? "???" : aggtypes[a->agg.type], a->agg.keysize, (u16int)a->agg.id);
670 dumpexpr(a->p, "\t\t\t");
673 print("\t\taggregation value (%s,%d,%d)\n", a->agg.type >= nelem(aggtypes) ? "???" : aggtypes[a->agg.type], a->agg.keysize, (u16int)a->agg.id);
674 dumpexpr(a->p, "\t\t\t");
677 print("\t\tcancel record\n");
680 print("\t\t??? %d\n", a->type);
682 print("\trecord formatting:\n");
683 for(s = c->stats; s < c->stats + c->nstats; s++)
688 print("\t\tprint\n");
689 for(j = 0; j < s->narg; j++)
690 print("\t\t\targ %ε\n", s->arg[j]);
693 print("\t\tprintf\n");
694 for(j = 0; j < s->narg; j++)
695 print("\t\t\targ %ε\n", s->arg[j]);
700 print("\t\t??? %d\n", s->type);