1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
25 * Copyright (c) 2013, Joyent Inc. All rights reserved.
26 * Copyright 2015 Gary Mills
27 */
28
29 /*
30 * DTrace D Language Compiler
31 *
32 * The code in this source file implements the main engine for the D language
33 * compiler. The driver routine for the compiler is dt_compile(), below. The
34 * compiler operates on either stdio FILEs or in-memory strings as its input
35 * and can produce either dtrace_prog_t structures from a D program or a single
36 * dtrace_difo_t structure from a D expression. Multiple entry points are
37 * provided as wrappers around dt_compile() for the various input/output pairs.
38 * The compiler itself is implemented across the following source files:
39 *
40 * dt_lex.l - lex scanner
41 * dt_grammar.y - yacc grammar
42 * dt_parser.c - parse tree creation and semantic checking
43 * dt_decl.c - declaration stack processing
44 * dt_xlator.c - D translator lookup and creation
45 * dt_ident.c - identifier and symbol table routines
46 * dt_pragma.c - #pragma processing and D pragmas
47 * dt_printf.c - D printf() and printa() argument checking and processing
48 * dt_cc.c - compiler driver and dtrace_prog_t construction
49 * dt_cg.c - DIF code generator
50 * dt_as.c - DIF assembler
51 * dt_dof.c - dtrace_prog_t -> DOF conversion
52 *
53 * Several other source files provide collections of utility routines used by
54 * these major files. The compiler itself is implemented in multiple passes:
55 *
56 * (1) The input program is scanned and parsed by dt_lex.l and dt_grammar.y
57 * and parse tree nodes are constructed using the routines in dt_parser.c.
58 * This node construction pass is described further in dt_parser.c.
59 *
60 * (2) The parse tree is "cooked" by assigning each clause a context (see the
61 * routine dt_setcontext(), below) based on its probe description and then
62 * recursively descending the tree performing semantic checking. The cook
63 * routines are also implemented in dt_parser.c and described there.
64 *
65 * (3) For actions that are DIF expression statements, the DIF code generator
66 * and assembler are invoked to create a finished DIFO for the statement.
67 *
68 * (4) The dtrace_prog_t data structures for the program clauses and actions
69 * are built, containing pointers to any DIFOs created in step (3).
70 *
71 * (5) The caller invokes a routine in dt_dof.c to convert the finished program
72 * into DOF format for use in anonymous tracing or enabling in the kernel.
73 *
74 * In the implementation, steps 2-4 are intertwined in that they are performed
75 * in order for each clause as part of a loop that executes over the clauses.
76 *
77 * The D compiler currently implements nearly no optimization. The compiler
78 * implements integer constant folding as part of pass (1), and a set of very
79 * simple peephole optimizations as part of pass (3). As with any C compiler,
80 * a large number of optimizations are possible on both the intermediate data
81 * structures and the generated DIF code. These possibilities should be
82 * investigated in the context of whether they will have any substantive effect
83 * on the overall DTrace probe effect before they are undertaken.
84 */
85
86 #include <sys/types.h>
87 #include <sys/wait.h>
88 #include <sys/sysmacros.h>
89
90 #include <assert.h>
91 #include <strings.h>
92 #include <signal.h>
93 #include <unistd.h>
94 #include <stdlib.h>
95 #include <stdio.h>
96 #include <errno.h>
97 #include <ucontext.h>
98 #include <limits.h>
99 #include <ctype.h>
100 #include <dirent.h>
101 #include <dt_module.h>
102 #include <dt_program.h>
103 #include <dt_provider.h>
104 #include <dt_printf.h>
105 #include <dt_pid.h>
106 #include <dt_grammar.h>
107 #include <dt_ident.h>
108 #include <dt_string.h>
109 #include <dt_impl.h>
110
111 static const dtrace_diftype_t dt_void_rtype = {
112 DIF_TYPE_CTF, CTF_K_INTEGER, 0, 0, 0
113 };
114
115 static const dtrace_diftype_t dt_int_rtype = {
116 DIF_TYPE_CTF, CTF_K_INTEGER, 0, 0, sizeof (uint64_t)
117 };
118
119 static void *dt_compile(dtrace_hdl_t *, int, dtrace_probespec_t, void *,
120 uint_t, int, char *const[], FILE *, const char *);
121
122 /*ARGSUSED*/
123 static int
dt_idreset(dt_idhash_t * dhp,dt_ident_t * idp,void * ignored)124 dt_idreset(dt_idhash_t *dhp, dt_ident_t *idp, void *ignored)
125 {
126 idp->di_flags &= ~(DT_IDFLG_REF | DT_IDFLG_MOD |
127 DT_IDFLG_DIFR | DT_IDFLG_DIFW);
128 return (0);
129 }
130
131 /*ARGSUSED*/
132 static int
dt_idpragma(dt_idhash_t * dhp,dt_ident_t * idp,void * ignored)133 dt_idpragma(dt_idhash_t *dhp, dt_ident_t *idp, void *ignored)
134 {
135 yylineno = idp->di_lineno;
136 xyerror(D_PRAGMA_UNUSED, "unused #pragma %s\n", (char *)idp->di_iarg);
137 return (0);
138 }
139
140 static dtrace_stmtdesc_t *
dt_stmt_create(dtrace_hdl_t * dtp,dtrace_ecbdesc_t * edp,dtrace_attribute_t descattr,dtrace_attribute_t stmtattr)141 dt_stmt_create(dtrace_hdl_t *dtp, dtrace_ecbdesc_t *edp,
142 dtrace_attribute_t descattr, dtrace_attribute_t stmtattr)
143 {
144 dtrace_stmtdesc_t *sdp = dtrace_stmt_create(dtp, edp);
145
146 if (sdp == NULL)
147 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
148
149 assert(yypcb->pcb_stmt == NULL);
150 yypcb->pcb_stmt = sdp;
151
152 sdp->dtsd_descattr = descattr;
153 sdp->dtsd_stmtattr = stmtattr;
154
155 return (sdp);
156 }
157
158 static dtrace_actdesc_t *
dt_stmt_action(dtrace_hdl_t * dtp,dtrace_stmtdesc_t * sdp)159 dt_stmt_action(dtrace_hdl_t *dtp, dtrace_stmtdesc_t *sdp)
160 {
161 dtrace_actdesc_t *new;
162
163 if ((new = dtrace_stmt_action(dtp, sdp)) == NULL)
164 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
165
166 return (new);
167 }
168
169 /*
170 * Utility function to determine if a given action description is destructive.
171 * The dtdo_destructive bit is set for us by the DIF assembler (see dt_as.c).
172 */
173 static int
dt_action_destructive(const dtrace_actdesc_t * ap)174 dt_action_destructive(const dtrace_actdesc_t *ap)
175 {
176 return (DTRACEACT_ISDESTRUCTIVE(ap->dtad_kind) || (ap->dtad_kind ==
177 DTRACEACT_DIFEXPR && ap->dtad_difo->dtdo_destructive));
178 }
179
180 static void
dt_stmt_append(dtrace_stmtdesc_t * sdp,const dt_node_t * dnp)181 dt_stmt_append(dtrace_stmtdesc_t *sdp, const dt_node_t *dnp)
182 {
183 dtrace_ecbdesc_t *edp = sdp->dtsd_ecbdesc;
184 dtrace_actdesc_t *ap, *tap;
185 int commit = 0;
186 int speculate = 0;
187 int datarec = 0;
188
189 /*
190 * Make sure that the new statement jibes with the rest of the ECB.
191 */
192 for (ap = edp->dted_action; ap != NULL; ap = ap->dtad_next) {
193 if (ap->dtad_kind == DTRACEACT_COMMIT) {
194 if (commit) {
195 dnerror(dnp, D_COMM_COMM, "commit( ) may "
196 "not follow commit( )\n");
197 }
198
199 if (datarec) {
200 dnerror(dnp, D_COMM_DREC, "commit( ) may "
201 "not follow data-recording action(s)\n");
202 }
203
204 for (tap = ap; tap != NULL; tap = tap->dtad_next) {
205 if (!DTRACEACT_ISAGG(tap->dtad_kind))
206 continue;
207
208 dnerror(dnp, D_AGG_COMM, "aggregating actions "
209 "may not follow commit( )\n");
210 }
211
212 commit = 1;
213 continue;
214 }
215
216 if (ap->dtad_kind == DTRACEACT_SPECULATE) {
217 if (speculate) {
218 dnerror(dnp, D_SPEC_SPEC, "speculate( ) may "
219 "not follow speculate( )\n");
220 }
221
222 if (commit) {
223 dnerror(dnp, D_SPEC_COMM, "speculate( ) may "
224 "not follow commit( )\n");
225 }
226
227 if (datarec) {
228 dnerror(dnp, D_SPEC_DREC, "speculate( ) may "
229 "not follow data-recording action(s)\n");
230 }
231
232 speculate = 1;
233 continue;
234 }
235
236 if (DTRACEACT_ISAGG(ap->dtad_kind)) {
237 if (speculate) {
238 dnerror(dnp, D_AGG_SPEC, "aggregating actions "
239 "may not follow speculate( )\n");
240 }
241
242 datarec = 1;
243 continue;
244 }
245
246 if (speculate) {
247 if (dt_action_destructive(ap)) {
248 dnerror(dnp, D_ACT_SPEC, "destructive actions "
249 "may not follow speculate( )\n");
250 }
251
252 if (ap->dtad_kind == DTRACEACT_EXIT) {
253 dnerror(dnp, D_EXIT_SPEC, "exit( ) may not "
254 "follow speculate( )\n");
255 }
256 }
257
258 /*
259 * Exclude all non data-recording actions.
260 */
261 if (dt_action_destructive(ap) ||
262 ap->dtad_kind == DTRACEACT_DISCARD)
263 continue;
264
265 if (ap->dtad_kind == DTRACEACT_DIFEXPR &&
266 ap->dtad_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_CTF &&
267 ap->dtad_difo->dtdo_rtype.dtdt_size == 0)
268 continue;
269
270 if (commit) {
271 dnerror(dnp, D_DREC_COMM, "data-recording actions "
272 "may not follow commit( )\n");
273 }
274
275 if (!speculate)
276 datarec = 1;
277 }
278
279 if (dtrace_stmt_add(yypcb->pcb_hdl, yypcb->pcb_prog, sdp) != 0)
280 longjmp(yypcb->pcb_jmpbuf, dtrace_errno(yypcb->pcb_hdl));
281
282 if (yypcb->pcb_stmt == sdp)
283 yypcb->pcb_stmt = NULL;
284 }
285
286 /*
287 * For the first element of an aggregation tuple or for printa(), we create a
288 * simple DIF program that simply returns the immediate value that is the ID
289 * of the aggregation itself. This could be optimized in the future by
290 * creating a new in-kernel dtad_kind that just returns an integer.
291 */
292 static void
dt_action_difconst(dtrace_actdesc_t * ap,uint_t id,dtrace_actkind_t kind)293 dt_action_difconst(dtrace_actdesc_t *ap, uint_t id, dtrace_actkind_t kind)
294 {
295 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
296 dtrace_difo_t *dp = dt_zalloc(dtp, sizeof (dtrace_difo_t));
297
298 if (dp == NULL)
299 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
300
301 dp->dtdo_buf = dt_alloc(dtp, sizeof (dif_instr_t) * 2);
302 dp->dtdo_inttab = dt_alloc(dtp, sizeof (uint64_t));
303
304 if (dp->dtdo_buf == NULL || dp->dtdo_inttab == NULL) {
305 dt_difo_free(dtp, dp);
306 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
307 }
308
309 dp->dtdo_buf[0] = DIF_INSTR_SETX(0, 1); /* setx DIF_INTEGER[0], %r1 */
310 dp->dtdo_buf[1] = DIF_INSTR_RET(1); /* ret %r1 */
311 dp->dtdo_len = 2;
312 dp->dtdo_inttab[0] = id;
313 dp->dtdo_intlen = 1;
314 dp->dtdo_rtype = dt_int_rtype;
315
316 ap->dtad_difo = dp;
317 ap->dtad_kind = kind;
318 }
319
320 static void
dt_action_clear(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)321 dt_action_clear(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
322 {
323 dt_ident_t *aid;
324 dtrace_actdesc_t *ap;
325 dt_node_t *anp;
326
327 char n[DT_TYPE_NAMELEN];
328 int argc = 0;
329
330 for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
331 argc++; /* count up arguments for error messages below */
332
333 if (argc != 1) {
334 dnerror(dnp, D_CLEAR_PROTO,
335 "%s( ) prototype mismatch: %d args passed, 1 expected\n",
336 dnp->dn_ident->di_name, argc);
337 }
338
339 anp = dnp->dn_args;
340 assert(anp != NULL);
341
342 if (anp->dn_kind != DT_NODE_AGG) {
343 dnerror(dnp, D_CLEAR_AGGARG,
344 "%s( ) argument #1 is incompatible with prototype:\n"
345 "\tprototype: aggregation\n\t argument: %s\n",
346 dnp->dn_ident->di_name,
347 dt_node_type_name(anp, n, sizeof (n)));
348 }
349
350 aid = anp->dn_ident;
351
352 if (aid->di_gen == dtp->dt_gen && !(aid->di_flags & DT_IDFLG_MOD)) {
353 dnerror(dnp, D_CLEAR_AGGBAD,
354 "undefined aggregation: @%s\n", aid->di_name);
355 }
356
357 ap = dt_stmt_action(dtp, sdp);
358 dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_LIBACT);
359 ap->dtad_arg = DT_ACT_CLEAR;
360 }
361
362 static void
dt_action_normalize(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)363 dt_action_normalize(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
364 {
365 dt_ident_t *aid;
366 dtrace_actdesc_t *ap;
367 dt_node_t *anp, *normal;
368 int denormal = (strcmp(dnp->dn_ident->di_name, "denormalize") == 0);
369
370 char n[DT_TYPE_NAMELEN];
371 int argc = 0;
372
373 for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
374 argc++; /* count up arguments for error messages below */
375
376 if ((denormal && argc != 1) || (!denormal && argc != 2)) {
377 dnerror(dnp, D_NORMALIZE_PROTO,
378 "%s( ) prototype mismatch: %d args passed, %d expected\n",
379 dnp->dn_ident->di_name, argc, denormal ? 1 : 2);
380 }
381
382 anp = dnp->dn_args;
383 assert(anp != NULL);
384
385 if (anp->dn_kind != DT_NODE_AGG) {
386 dnerror(dnp, D_NORMALIZE_AGGARG,
387 "%s( ) argument #1 is incompatible with prototype:\n"
388 "\tprototype: aggregation\n\t argument: %s\n",
389 dnp->dn_ident->di_name,
390 dt_node_type_name(anp, n, sizeof (n)));
391 }
392
393 if ((normal = anp->dn_list) != NULL && !dt_node_is_scalar(normal)) {
394 dnerror(dnp, D_NORMALIZE_SCALAR,
395 "%s( ) argument #2 must be of scalar type\n",
396 dnp->dn_ident->di_name);
397 }
398
399 aid = anp->dn_ident;
400
401 if (aid->di_gen == dtp->dt_gen && !(aid->di_flags & DT_IDFLG_MOD)) {
402 dnerror(dnp, D_NORMALIZE_AGGBAD,
403 "undefined aggregation: @%s\n", aid->di_name);
404 }
405
406 ap = dt_stmt_action(dtp, sdp);
407 dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_LIBACT);
408
409 if (denormal) {
410 ap->dtad_arg = DT_ACT_DENORMALIZE;
411 return;
412 }
413
414 ap->dtad_arg = DT_ACT_NORMALIZE;
415
416 assert(normal != NULL);
417 ap = dt_stmt_action(dtp, sdp);
418 dt_cg(yypcb, normal);
419
420 ap->dtad_difo = dt_as(yypcb);
421 ap->dtad_kind = DTRACEACT_LIBACT;
422 ap->dtad_arg = DT_ACT_NORMALIZE;
423 }
424
425 static void
dt_action_trunc(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)426 dt_action_trunc(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
427 {
428 dt_ident_t *aid;
429 dtrace_actdesc_t *ap;
430 dt_node_t *anp, *trunc;
431
432 char n[DT_TYPE_NAMELEN];
433 int argc = 0;
434
435 for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
436 argc++; /* count up arguments for error messages below */
437
438 if (argc > 2 || argc < 1) {
439 dnerror(dnp, D_TRUNC_PROTO,
440 "%s( ) prototype mismatch: %d args passed, %s expected\n",
441 dnp->dn_ident->di_name, argc,
442 argc < 1 ? "at least 1" : "no more than 2");
443 }
444
445 anp = dnp->dn_args;
446 assert(anp != NULL);
447 trunc = anp->dn_list;
448
449 if (anp->dn_kind != DT_NODE_AGG) {
450 dnerror(dnp, D_TRUNC_AGGARG,
451 "%s( ) argument #1 is incompatible with prototype:\n"
452 "\tprototype: aggregation\n\t argument: %s\n",
453 dnp->dn_ident->di_name,
454 dt_node_type_name(anp, n, sizeof (n)));
455 }
456
457 if (argc == 2) {
458 assert(trunc != NULL);
459 if (!dt_node_is_scalar(trunc)) {
460 dnerror(dnp, D_TRUNC_SCALAR,
461 "%s( ) argument #2 must be of scalar type\n",
462 dnp->dn_ident->di_name);
463 }
464 }
465
466 aid = anp->dn_ident;
467
468 if (aid->di_gen == dtp->dt_gen && !(aid->di_flags & DT_IDFLG_MOD)) {
469 dnerror(dnp, D_TRUNC_AGGBAD,
470 "undefined aggregation: @%s\n", aid->di_name);
471 }
472
473 ap = dt_stmt_action(dtp, sdp);
474 dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_LIBACT);
475 ap->dtad_arg = DT_ACT_TRUNC;
476
477 ap = dt_stmt_action(dtp, sdp);
478
479 if (argc == 1) {
480 dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
481 } else {
482 assert(trunc != NULL);
483 dt_cg(yypcb, trunc);
484 ap->dtad_difo = dt_as(yypcb);
485 ap->dtad_kind = DTRACEACT_LIBACT;
486 }
487
488 ap->dtad_arg = DT_ACT_TRUNC;
489 }
490
491 static void
dt_action_printa(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)492 dt_action_printa(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
493 {
494 dt_ident_t *aid, *fid;
495 dtrace_actdesc_t *ap;
496 const char *format;
497 dt_node_t *anp, *proto = NULL;
498
499 char n[DT_TYPE_NAMELEN];
500 int argc = 0, argr = 0;
501
502 for (anp = dnp->dn_args; anp != NULL; anp = anp->dn_list)
503 argc++; /* count up arguments for error messages below */
504
505 if (dnp->dn_args != NULL) {
506 switch (dnp->dn_args->dn_kind) {
507 case DT_NODE_STRING:
508 format = dnp->dn_args->dn_string;
509 anp = dnp->dn_args->dn_list;
510 argr = 2;
511 break;
512 case DT_NODE_AGG:
513 format = NULL;
514 anp = dnp->dn_args;
515 argr = 1;
516 break;
517 default:
518 format = NULL;
519 anp = dnp->dn_args;
520 argr = 1;
521 }
522 }
523
524 if (argc < argr) {
525 dnerror(dnp, D_PRINTA_PROTO,
526 "%s( ) prototype mismatch: %d args passed, %d expected\n",
527 dnp->dn_ident->di_name, argc, argr);
528 }
529
530 assert(anp != NULL);
531
532 while (anp != NULL) {
533 if (anp->dn_kind != DT_NODE_AGG) {
534 dnerror(dnp, D_PRINTA_AGGARG,
535 "%s( ) argument #%d is incompatible with "
536 "prototype:\n\tprototype: aggregation\n"
537 "\t argument: %s\n", dnp->dn_ident->di_name, argr,
538 dt_node_type_name(anp, n, sizeof (n)));
539 }
540
541 aid = anp->dn_ident;
542 fid = aid->di_iarg;
543
544 if (aid->di_gen == dtp->dt_gen &&
545 !(aid->di_flags & DT_IDFLG_MOD)) {
546 dnerror(dnp, D_PRINTA_AGGBAD,
547 "undefined aggregation: @%s\n", aid->di_name);
548 }
549
550 /*
551 * If we have multiple aggregations, we must be sure that
552 * their key signatures match.
553 */
554 if (proto != NULL) {
555 dt_printa_validate(proto, anp);
556 } else {
557 proto = anp;
558 }
559
560 if (format != NULL) {
561 yylineno = dnp->dn_line;
562
563 sdp->dtsd_fmtdata =
564 dt_printf_create(yypcb->pcb_hdl, format);
565 dt_printf_validate(sdp->dtsd_fmtdata,
566 DT_PRINTF_AGGREGATION, dnp->dn_ident, 1,
567 fid->di_id, ((dt_idsig_t *)aid->di_data)->dis_args);
568 format = NULL;
569 }
570
571 ap = dt_stmt_action(dtp, sdp);
572 dt_action_difconst(ap, anp->dn_ident->di_id, DTRACEACT_PRINTA);
573
574 anp = anp->dn_list;
575 argr++;
576 }
577 }
578
579 static void
dt_action_printflike(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp,dtrace_actkind_t kind)580 dt_action_printflike(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp,
581 dtrace_actkind_t kind)
582 {
583 dt_node_t *anp, *arg1;
584 dtrace_actdesc_t *ap = NULL;
585 char n[DT_TYPE_NAMELEN], *str;
586
587 assert(DTRACEACT_ISPRINTFLIKE(kind));
588
589 if (dnp->dn_args->dn_kind != DT_NODE_STRING) {
590 dnerror(dnp, D_PRINTF_ARG_FMT,
591 "%s( ) argument #1 is incompatible with prototype:\n"
592 "\tprototype: string constant\n\t argument: %s\n",
593 dnp->dn_ident->di_name,
594 dt_node_type_name(dnp->dn_args, n, sizeof (n)));
595 }
596
597 arg1 = dnp->dn_args->dn_list;
598 yylineno = dnp->dn_line;
599 str = dnp->dn_args->dn_string;
600
601
602 /*
603 * If this is an freopen(), we use an empty string to denote that
604 * stdout should be restored. For other printf()-like actions, an
605 * empty format string is illegal: an empty format string would
606 * result in malformed DOF, and the compiler thus flags an empty
607 * format string as a compile-time error. To avoid propagating the
608 * freopen() special case throughout the system, we simply transpose
609 * an empty string into a sentinel string (DT_FREOPEN_RESTORE) that
610 * denotes that stdout should be restored.
611 */
612 if (kind == DTRACEACT_FREOPEN) {
613 if (strcmp(str, DT_FREOPEN_RESTORE) == 0) {
614 /*
615 * Our sentinel is always an invalid argument to
616 * freopen(), but if it's been manually specified, we
617 * must fail now instead of when the freopen() is
618 * actually evaluated.
619 */
620 dnerror(dnp, D_FREOPEN_INVALID,
621 "%s( ) argument #1 cannot be \"%s\"\n",
622 dnp->dn_ident->di_name, DT_FREOPEN_RESTORE);
623 }
624
625 if (str[0] == '\0')
626 str = DT_FREOPEN_RESTORE;
627 }
628
629 sdp->dtsd_fmtdata = dt_printf_create(dtp, str);
630
631 dt_printf_validate(sdp->dtsd_fmtdata, DT_PRINTF_EXACTLEN,
632 dnp->dn_ident, 1, DTRACEACT_AGGREGATION, arg1);
633
634 if (arg1 == NULL) {
635 dif_instr_t *dbuf;
636 dtrace_difo_t *dp;
637
638 if ((dbuf = dt_alloc(dtp, sizeof (dif_instr_t))) == NULL ||
639 (dp = dt_zalloc(dtp, sizeof (dtrace_difo_t))) == NULL) {
640 dt_free(dtp, dbuf);
641 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
642 }
643
644 dbuf[0] = DIF_INSTR_RET(DIF_REG_R0); /* ret %r0 */
645
646 dp->dtdo_buf = dbuf;
647 dp->dtdo_len = 1;
648 dp->dtdo_rtype = dt_int_rtype;
649
650 ap = dt_stmt_action(dtp, sdp);
651 ap->dtad_difo = dp;
652 ap->dtad_kind = kind;
653 return;
654 }
655
656 for (anp = arg1; anp != NULL; anp = anp->dn_list) {
657 ap = dt_stmt_action(dtp, sdp);
658 dt_cg(yypcb, anp);
659 ap->dtad_difo = dt_as(yypcb);
660 ap->dtad_kind = kind;
661 }
662 }
663
664 static void
dt_action_trace(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)665 dt_action_trace(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
666 {
667 int ctflib;
668
669 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
670 boolean_t istrace = (dnp->dn_ident->di_id == DT_ACT_TRACE);
671 const char *act = istrace ? "trace" : "print";
672
673 if (dt_node_is_void(dnp->dn_args)) {
674 dnerror(dnp->dn_args, istrace ? D_TRACE_VOID : D_PRINT_VOID,
675 "%s( ) may not be applied to a void expression\n", act);
676 }
677
678 if (dt_node_resolve(dnp->dn_args, DT_IDENT_XLPTR) != NULL) {
679 dnerror(dnp->dn_args, istrace ? D_TRACE_DYN : D_PRINT_DYN,
680 "%s( ) may not be applied to a translated pointer\n", act);
681 }
682
683 if (dnp->dn_args->dn_kind == DT_NODE_AGG) {
684 dnerror(dnp->dn_args, istrace ? D_TRACE_AGG : D_PRINT_AGG,
685 "%s( ) may not be applied to an aggregation%s\n", act,
686 istrace ? "" : " -- did you mean printa()?");
687 }
688
689 dt_cg(yypcb, dnp->dn_args);
690
691 /*
692 * The print() action behaves identically to trace(), except that it
693 * stores the CTF type of the argument (if present) within the DOF for
694 * the DIFEXPR action. To do this, we set the 'dtsd_strdata' to point
695 * to the fully-qualified CTF type ID for the result of the DIF
696 * action. We use the ID instead of the name to handles complex types
697 * like arrays and function pointers that can't be resolved by
698 * ctf_type_lookup(). This is later processed by dtrace_dof_create()
699 * and turned into a reference into the string table so that we can
700 * get the type information when we process the data after the fact. In
701 * the case where we are referring to userland CTF data, we also need to
702 * to identify which ctf container in question we care about and encode
703 * that within the name.
704 */
705 if (dnp->dn_ident->di_id == DT_ACT_PRINT) {
706 dt_node_t *dret;
707 size_t n;
708 dt_module_t *dmp;
709
710 dret = yypcb->pcb_dret;
711 dmp = dt_module_lookup_by_ctf(dtp, dret->dn_ctfp);
712
713 if (dmp->dm_pid != 0) {
714 ctflib = dt_module_getlibid(dtp, dmp, dret->dn_ctfp);
715 assert(ctflib >= 0);
716 n = snprintf(NULL, 0, "%s`%d`%d", dmp->dm_name,
717 ctflib, dret->dn_type) + 1;
718 } else {
719 n = snprintf(NULL, 0, "%s`%d", dmp->dm_name,
720 dret->dn_type) + 1;
721 }
722 sdp->dtsd_strdata = dt_alloc(dtp, n);
723 if (sdp->dtsd_strdata == NULL)
724 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
725 if (dmp->dm_pid != 0) {
726 (void) snprintf(sdp->dtsd_strdata, n, "%s`%d`%d",
727 dmp->dm_name, ctflib, dret->dn_type);
728 } else {
729 (void) snprintf(sdp->dtsd_strdata, n, "%s`%d",
730 dmp->dm_name, dret->dn_type);
731 }
732 }
733
734 ap->dtad_difo = dt_as(yypcb);
735 ap->dtad_kind = DTRACEACT_DIFEXPR;
736 }
737
738 static void
dt_action_tracemem(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)739 dt_action_tracemem(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
740 {
741 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
742
743 dt_node_t *addr = dnp->dn_args;
744 dt_node_t *max = dnp->dn_args->dn_list;
745 dt_node_t *size;
746
747 char n[DT_TYPE_NAMELEN];
748
749 if (dt_node_is_integer(addr) == 0 && dt_node_is_pointer(addr) == 0) {
750 dnerror(addr, D_TRACEMEM_ADDR,
751 "tracemem( ) argument #1 is incompatible with "
752 "prototype:\n\tprototype: pointer or integer\n"
753 "\t argument: %s\n",
754 dt_node_type_name(addr, n, sizeof (n)));
755 }
756
757 if (dt_node_is_posconst(max) == 0) {
758 dnerror(max, D_TRACEMEM_SIZE, "tracemem( ) argument #2 must "
759 "be a non-zero positive integral constant expression\n");
760 }
761
762 if ((size = max->dn_list) != NULL) {
763 if (size->dn_list != NULL) {
764 dnerror(size, D_TRACEMEM_ARGS, "tracemem ( ) prototype "
765 "mismatch: expected at most 3 args\n");
766 }
767
768 if (!dt_node_is_scalar(size)) {
769 dnerror(size, D_TRACEMEM_DYNSIZE, "tracemem ( ) "
770 "dynamic size (argument #3) must be of "
771 "scalar type\n");
772 }
773
774 dt_cg(yypcb, size);
775 ap->dtad_difo = dt_as(yypcb);
776 ap->dtad_difo->dtdo_rtype = dt_int_rtype;
777 ap->dtad_kind = DTRACEACT_TRACEMEM_DYNSIZE;
778
779 ap = dt_stmt_action(dtp, sdp);
780 }
781
782 dt_cg(yypcb, addr);
783 ap->dtad_difo = dt_as(yypcb);
784 ap->dtad_kind = DTRACEACT_TRACEMEM;
785
786 ap->dtad_difo->dtdo_rtype.dtdt_flags |= DIF_TF_BYREF;
787 ap->dtad_difo->dtdo_rtype.dtdt_size = max->dn_value;
788 }
789
790 static void
dt_action_stack_args(dtrace_hdl_t * dtp,dtrace_actdesc_t * ap,dt_node_t * arg0)791 dt_action_stack_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap, dt_node_t *arg0)
792 {
793 ap->dtad_kind = DTRACEACT_STACK;
794
795 if (dtp->dt_options[DTRACEOPT_STACKFRAMES] != DTRACEOPT_UNSET) {
796 ap->dtad_arg = dtp->dt_options[DTRACEOPT_STACKFRAMES];
797 } else {
798 ap->dtad_arg = 0;
799 }
800
801 if (arg0 != NULL) {
802 if (arg0->dn_list != NULL) {
803 dnerror(arg0, D_STACK_PROTO, "stack( ) prototype "
804 "mismatch: too many arguments\n");
805 }
806
807 if (dt_node_is_posconst(arg0) == 0) {
808 dnerror(arg0, D_STACK_SIZE, "stack( ) size must be a "
809 "non-zero positive integral constant expression\n");
810 }
811
812 ap->dtad_arg = arg0->dn_value;
813 }
814 }
815
816 static void
dt_action_stack(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)817 dt_action_stack(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
818 {
819 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
820 dt_action_stack_args(dtp, ap, dnp->dn_args);
821 }
822
823 static void
dt_action_ustack_args(dtrace_hdl_t * dtp,dtrace_actdesc_t * ap,dt_node_t * dnp)824 dt_action_ustack_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap, dt_node_t *dnp)
825 {
826 uint32_t nframes = 0;
827 uint32_t strsize = 0; /* default string table size */
828 dt_node_t *arg0 = dnp->dn_args;
829 dt_node_t *arg1 = arg0 != NULL ? arg0->dn_list : NULL;
830
831 assert(dnp->dn_ident->di_id == DT_ACT_JSTACK ||
832 dnp->dn_ident->di_id == DT_ACT_USTACK);
833
834 if (dnp->dn_ident->di_id == DT_ACT_JSTACK) {
835 if (dtp->dt_options[DTRACEOPT_JSTACKFRAMES] != DTRACEOPT_UNSET)
836 nframes = dtp->dt_options[DTRACEOPT_JSTACKFRAMES];
837
838 if (dtp->dt_options[DTRACEOPT_JSTACKSTRSIZE] != DTRACEOPT_UNSET)
839 strsize = dtp->dt_options[DTRACEOPT_JSTACKSTRSIZE];
840
841 ap->dtad_kind = DTRACEACT_JSTACK;
842 } else {
843 assert(dnp->dn_ident->di_id == DT_ACT_USTACK);
844
845 if (dtp->dt_options[DTRACEOPT_USTACKFRAMES] != DTRACEOPT_UNSET)
846 nframes = dtp->dt_options[DTRACEOPT_USTACKFRAMES];
847
848 ap->dtad_kind = DTRACEACT_USTACK;
849 }
850
851 if (arg0 != NULL) {
852 if (!dt_node_is_posconst(arg0)) {
853 dnerror(arg0, D_USTACK_FRAMES, "ustack( ) argument #1 "
854 "must be a non-zero positive integer constant\n");
855 }
856 nframes = (uint32_t)arg0->dn_value;
857 }
858
859 if (arg1 != NULL) {
860 if (arg1->dn_kind != DT_NODE_INT ||
861 ((arg1->dn_flags & DT_NF_SIGNED) &&
862 (int64_t)arg1->dn_value < 0)) {
863 dnerror(arg1, D_USTACK_STRSIZE, "ustack( ) argument #2 "
864 "must be a positive integer constant\n");
865 }
866
867 if (arg1->dn_list != NULL) {
868 dnerror(arg1, D_USTACK_PROTO, "ustack( ) prototype "
869 "mismatch: too many arguments\n");
870 }
871
872 strsize = (uint32_t)arg1->dn_value;
873 }
874
875 ap->dtad_arg = DTRACE_USTACK_ARG(nframes, strsize);
876 }
877
878 static void
dt_action_ustack(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)879 dt_action_ustack(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
880 {
881 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
882 dt_action_ustack_args(dtp, ap, dnp);
883 }
884
885 static void
dt_action_setopt(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)886 dt_action_setopt(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
887 {
888 dtrace_actdesc_t *ap;
889 dt_node_t *arg0, *arg1;
890
891 /*
892 * The prototype guarantees that we are called with either one or
893 * two arguments, and that any arguments that are present are strings.
894 */
895 arg0 = dnp->dn_args;
896 arg1 = arg0->dn_list;
897
898 ap = dt_stmt_action(dtp, sdp);
899 dt_cg(yypcb, arg0);
900 ap->dtad_difo = dt_as(yypcb);
901 ap->dtad_kind = DTRACEACT_LIBACT;
902 ap->dtad_arg = DT_ACT_SETOPT;
903
904 ap = dt_stmt_action(dtp, sdp);
905
906 if (arg1 == NULL) {
907 dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
908 } else {
909 dt_cg(yypcb, arg1);
910 ap->dtad_difo = dt_as(yypcb);
911 ap->dtad_kind = DTRACEACT_LIBACT;
912 }
913
914 ap->dtad_arg = DT_ACT_SETOPT;
915 }
916
917 /*ARGSUSED*/
918 static void
dt_action_symmod_args(dtrace_hdl_t * dtp,dtrace_actdesc_t * ap,dt_node_t * dnp,dtrace_actkind_t kind)919 dt_action_symmod_args(dtrace_hdl_t *dtp, dtrace_actdesc_t *ap,
920 dt_node_t *dnp, dtrace_actkind_t kind)
921 {
922 assert(kind == DTRACEACT_SYM || kind == DTRACEACT_MOD ||
923 kind == DTRACEACT_USYM || kind == DTRACEACT_UMOD ||
924 kind == DTRACEACT_UADDR);
925
926 dt_cg(yypcb, dnp);
927 ap->dtad_difo = dt_as(yypcb);
928 ap->dtad_kind = kind;
929 ap->dtad_difo->dtdo_rtype.dtdt_size = sizeof (uint64_t);
930 }
931
932 static void
dt_action_symmod(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp,dtrace_actkind_t kind)933 dt_action_symmod(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp,
934 dtrace_actkind_t kind)
935 {
936 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
937 dt_action_symmod_args(dtp, ap, dnp->dn_args, kind);
938 }
939
940 /*ARGSUSED*/
941 static void
dt_action_ftruncate(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)942 dt_action_ftruncate(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
943 {
944 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
945
946 /*
947 * Library actions need a DIFO that serves as an argument. As
948 * ftruncate() doesn't take an argument, we generate the constant 0
949 * in a DIFO; this constant will be ignored when the ftruncate() is
950 * processed.
951 */
952 dt_action_difconst(ap, 0, DTRACEACT_LIBACT);
953 ap->dtad_arg = DT_ACT_FTRUNCATE;
954 }
955
956 /*ARGSUSED*/
957 static void
dt_action_stop(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)958 dt_action_stop(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
959 {
960 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
961
962 ap->dtad_kind = DTRACEACT_STOP;
963 ap->dtad_arg = 0;
964 }
965
966 /*ARGSUSED*/
967 static void
dt_action_breakpoint(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)968 dt_action_breakpoint(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
969 {
970 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
971
972 ap->dtad_kind = DTRACEACT_BREAKPOINT;
973 ap->dtad_arg = 0;
974 }
975
976 /*ARGSUSED*/
977 static void
dt_action_panic(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)978 dt_action_panic(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
979 {
980 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
981
982 ap->dtad_kind = DTRACEACT_PANIC;
983 ap->dtad_arg = 0;
984 }
985
986 static void
dt_action_chill(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)987 dt_action_chill(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
988 {
989 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
990
991 dt_cg(yypcb, dnp->dn_args);
992 ap->dtad_difo = dt_as(yypcb);
993 ap->dtad_kind = DTRACEACT_CHILL;
994 }
995
996 static void
dt_action_raise(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)997 dt_action_raise(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
998 {
999 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1000
1001 dt_cg(yypcb, dnp->dn_args);
1002 ap->dtad_difo = dt_as(yypcb);
1003 ap->dtad_kind = DTRACEACT_RAISE;
1004 }
1005
1006 static void
dt_action_exit(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)1007 dt_action_exit(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1008 {
1009 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1010
1011 dt_cg(yypcb, dnp->dn_args);
1012 ap->dtad_difo = dt_as(yypcb);
1013 ap->dtad_kind = DTRACEACT_EXIT;
1014 ap->dtad_difo->dtdo_rtype.dtdt_size = sizeof (int);
1015 }
1016
1017 static void
dt_action_speculate(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)1018 dt_action_speculate(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1019 {
1020 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1021
1022 dt_cg(yypcb, dnp->dn_args);
1023 ap->dtad_difo = dt_as(yypcb);
1024 ap->dtad_kind = DTRACEACT_SPECULATE;
1025 }
1026
1027 static void
dt_action_commit(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)1028 dt_action_commit(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1029 {
1030 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1031
1032 dt_cg(yypcb, dnp->dn_args);
1033 ap->dtad_difo = dt_as(yypcb);
1034 ap->dtad_kind = DTRACEACT_COMMIT;
1035 }
1036
1037 static void
dt_action_discard(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)1038 dt_action_discard(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1039 {
1040 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1041
1042 dt_cg(yypcb, dnp->dn_args);
1043 ap->dtad_difo = dt_as(yypcb);
1044 ap->dtad_kind = DTRACEACT_DISCARD;
1045 }
1046
1047 static void
dt_compile_fun(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)1048 dt_compile_fun(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1049 {
1050 switch (dnp->dn_expr->dn_ident->di_id) {
1051 case DT_ACT_BREAKPOINT:
1052 dt_action_breakpoint(dtp, dnp->dn_expr, sdp);
1053 break;
1054 case DT_ACT_CHILL:
1055 dt_action_chill(dtp, dnp->dn_expr, sdp);
1056 break;
1057 case DT_ACT_CLEAR:
1058 dt_action_clear(dtp, dnp->dn_expr, sdp);
1059 break;
1060 case DT_ACT_COMMIT:
1061 dt_action_commit(dtp, dnp->dn_expr, sdp);
1062 break;
1063 case DT_ACT_DENORMALIZE:
1064 dt_action_normalize(dtp, dnp->dn_expr, sdp);
1065 break;
1066 case DT_ACT_DISCARD:
1067 dt_action_discard(dtp, dnp->dn_expr, sdp);
1068 break;
1069 case DT_ACT_EXIT:
1070 dt_action_exit(dtp, dnp->dn_expr, sdp);
1071 break;
1072 case DT_ACT_FREOPEN:
1073 dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_FREOPEN);
1074 break;
1075 case DT_ACT_FTRUNCATE:
1076 dt_action_ftruncate(dtp, dnp->dn_expr, sdp);
1077 break;
1078 case DT_ACT_MOD:
1079 dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_MOD);
1080 break;
1081 case DT_ACT_NORMALIZE:
1082 dt_action_normalize(dtp, dnp->dn_expr, sdp);
1083 break;
1084 case DT_ACT_PANIC:
1085 dt_action_panic(dtp, dnp->dn_expr, sdp);
1086 break;
1087 case DT_ACT_PRINT:
1088 dt_action_trace(dtp, dnp->dn_expr, sdp);
1089 break;
1090 case DT_ACT_PRINTA:
1091 dt_action_printa(dtp, dnp->dn_expr, sdp);
1092 break;
1093 case DT_ACT_PRINTF:
1094 dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_PRINTF);
1095 break;
1096 case DT_ACT_RAISE:
1097 dt_action_raise(dtp, dnp->dn_expr, sdp);
1098 break;
1099 case DT_ACT_SETOPT:
1100 dt_action_setopt(dtp, dnp->dn_expr, sdp);
1101 break;
1102 case DT_ACT_SPECULATE:
1103 dt_action_speculate(dtp, dnp->dn_expr, sdp);
1104 break;
1105 case DT_ACT_STACK:
1106 dt_action_stack(dtp, dnp->dn_expr, sdp);
1107 break;
1108 case DT_ACT_STOP:
1109 dt_action_stop(dtp, dnp->dn_expr, sdp);
1110 break;
1111 case DT_ACT_SYM:
1112 dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_SYM);
1113 break;
1114 case DT_ACT_SYSTEM:
1115 dt_action_printflike(dtp, dnp->dn_expr, sdp, DTRACEACT_SYSTEM);
1116 break;
1117 case DT_ACT_TRACE:
1118 dt_action_trace(dtp, dnp->dn_expr, sdp);
1119 break;
1120 case DT_ACT_TRACEMEM:
1121 dt_action_tracemem(dtp, dnp->dn_expr, sdp);
1122 break;
1123 case DT_ACT_TRUNC:
1124 dt_action_trunc(dtp, dnp->dn_expr, sdp);
1125 break;
1126 case DT_ACT_UADDR:
1127 dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_UADDR);
1128 break;
1129 case DT_ACT_UMOD:
1130 dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_UMOD);
1131 break;
1132 case DT_ACT_USYM:
1133 dt_action_symmod(dtp, dnp->dn_expr, sdp, DTRACEACT_USYM);
1134 break;
1135 case DT_ACT_USTACK:
1136 case DT_ACT_JSTACK:
1137 dt_action_ustack(dtp, dnp->dn_expr, sdp);
1138 break;
1139 default:
1140 dnerror(dnp->dn_expr, D_UNKNOWN, "tracing function %s( ) is "
1141 "not yet supported\n", dnp->dn_expr->dn_ident->di_name);
1142 }
1143 }
1144
1145 static void
dt_compile_exp(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)1146 dt_compile_exp(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1147 {
1148 dtrace_actdesc_t *ap = dt_stmt_action(dtp, sdp);
1149
1150 dt_cg(yypcb, dnp->dn_expr);
1151 ap->dtad_difo = dt_as(yypcb);
1152 ap->dtad_difo->dtdo_rtype = dt_void_rtype;
1153 ap->dtad_kind = DTRACEACT_DIFEXPR;
1154 }
1155
1156 static void
dt_compile_agg(dtrace_hdl_t * dtp,dt_node_t * dnp,dtrace_stmtdesc_t * sdp)1157 dt_compile_agg(dtrace_hdl_t *dtp, dt_node_t *dnp, dtrace_stmtdesc_t *sdp)
1158 {
1159 dt_ident_t *aid, *fid;
1160 dt_node_t *anp, *incr = NULL;
1161 dtrace_actdesc_t *ap;
1162 uint_t n = 1, argmax;
1163 uint64_t arg = 0;
1164
1165 /*
1166 * If the aggregation has no aggregating function applied to it, then
1167 * this statement has no effect. Flag this as a programming error.
1168 */
1169 if (dnp->dn_aggfun == NULL) {
1170 dnerror(dnp, D_AGG_NULL, "expression has null effect: @%s\n",
1171 dnp->dn_ident->di_name);
1172 }
1173
1174 aid = dnp->dn_ident;
1175 fid = dnp->dn_aggfun->dn_ident;
1176
1177 if (dnp->dn_aggfun->dn_args != NULL &&
1178 dt_node_is_scalar(dnp->dn_aggfun->dn_args) == 0) {
1179 dnerror(dnp->dn_aggfun, D_AGG_SCALAR, "%s( ) argument #1 must "
1180 "be of scalar type\n", fid->di_name);
1181 }
1182
1183 /*
1184 * The ID of the aggregation itself is implicitly recorded as the first
1185 * member of each aggregation tuple so we can distinguish them later.
1186 */
1187 ap = dt_stmt_action(dtp, sdp);
1188 dt_action_difconst(ap, aid->di_id, DTRACEACT_DIFEXPR);
1189
1190 for (anp = dnp->dn_aggtup; anp != NULL; anp = anp->dn_list) {
1191 ap = dt_stmt_action(dtp, sdp);
1192 n++;
1193
1194 if (anp->dn_kind == DT_NODE_FUNC) {
1195 if (anp->dn_ident->di_id == DT_ACT_STACK) {
1196 dt_action_stack_args(dtp, ap, anp->dn_args);
1197 continue;
1198 }
1199
1200 if (anp->dn_ident->di_id == DT_ACT_USTACK ||
1201 anp->dn_ident->di_id == DT_ACT_JSTACK) {
1202 dt_action_ustack_args(dtp, ap, anp);
1203 continue;
1204 }
1205
1206 switch (anp->dn_ident->di_id) {
1207 case DT_ACT_UADDR:
1208 dt_action_symmod_args(dtp, ap,
1209 anp->dn_args, DTRACEACT_UADDR);
1210 continue;
1211
1212 case DT_ACT_USYM:
1213 dt_action_symmod_args(dtp, ap,
1214 anp->dn_args, DTRACEACT_USYM);
1215 continue;
1216
1217 case DT_ACT_UMOD:
1218 dt_action_symmod_args(dtp, ap,
1219 anp->dn_args, DTRACEACT_UMOD);
1220 continue;
1221
1222 case DT_ACT_SYM:
1223 dt_action_symmod_args(dtp, ap,
1224 anp->dn_args, DTRACEACT_SYM);
1225 continue;
1226
1227 case DT_ACT_MOD:
1228 dt_action_symmod_args(dtp, ap,
1229 anp->dn_args, DTRACEACT_MOD);
1230 continue;
1231
1232 default:
1233 break;
1234 }
1235 }
1236
1237 dt_cg(yypcb, anp);
1238 ap->dtad_difo = dt_as(yypcb);
1239 ap->dtad_kind = DTRACEACT_DIFEXPR;
1240 }
1241
1242 if (fid->di_id == DTRACEAGG_LQUANTIZE) {
1243 /*
1244 * For linear quantization, we have between two and four
1245 * arguments in addition to the expression:
1246 *
1247 * arg1 => Base value
1248 * arg2 => Limit value
1249 * arg3 => Quantization level step size (defaults to 1)
1250 * arg4 => Quantization increment value (defaults to 1)
1251 */
1252 dt_node_t *arg1 = dnp->dn_aggfun->dn_args->dn_list;
1253 dt_node_t *arg2 = arg1->dn_list;
1254 dt_node_t *arg3 = arg2->dn_list;
1255 dt_idsig_t *isp;
1256 uint64_t nlevels, step = 1, oarg;
1257 int64_t baseval, limitval;
1258
1259 if (arg1->dn_kind != DT_NODE_INT) {
1260 dnerror(arg1, D_LQUANT_BASETYPE, "lquantize( ) "
1261 "argument #1 must be an integer constant\n");
1262 }
1263
1264 baseval = (int64_t)arg1->dn_value;
1265
1266 if (baseval < INT32_MIN || baseval > INT32_MAX) {
1267 dnerror(arg1, D_LQUANT_BASEVAL, "lquantize( ) "
1268 "argument #1 must be a 32-bit quantity\n");
1269 }
1270
1271 if (arg2->dn_kind != DT_NODE_INT) {
1272 dnerror(arg2, D_LQUANT_LIMTYPE, "lquantize( ) "
1273 "argument #2 must be an integer constant\n");
1274 }
1275
1276 limitval = (int64_t)arg2->dn_value;
1277
1278 if (limitval < INT32_MIN || limitval > INT32_MAX) {
1279 dnerror(arg2, D_LQUANT_LIMVAL, "lquantize( ) "
1280 "argument #2 must be a 32-bit quantity\n");
1281 }
1282
1283 if (limitval < baseval) {
1284 dnerror(dnp, D_LQUANT_MISMATCH,
1285 "lquantize( ) base (argument #1) must be less "
1286 "than limit (argument #2)\n");
1287 }
1288
1289 if (arg3 != NULL) {
1290 if (!dt_node_is_posconst(arg3)) {
1291 dnerror(arg3, D_LQUANT_STEPTYPE, "lquantize( ) "
1292 "argument #3 must be a non-zero positive "
1293 "integer constant\n");
1294 }
1295
1296 if ((step = arg3->dn_value) > UINT16_MAX) {
1297 dnerror(arg3, D_LQUANT_STEPVAL, "lquantize( ) "
1298 "argument #3 must be a 16-bit quantity\n");
1299 }
1300 }
1301
1302 nlevels = (limitval - baseval) / step;
1303
1304 if (nlevels == 0) {
1305 dnerror(dnp, D_LQUANT_STEPLARGE,
1306 "lquantize( ) step (argument #3) too large: must "
1307 "have at least one quantization level\n");
1308 }
1309
1310 if (nlevels > UINT16_MAX) {
1311 dnerror(dnp, D_LQUANT_STEPSMALL, "lquantize( ) step "
1312 "(argument #3) too small: number of quantization "
1313 "levels must be a 16-bit quantity\n");
1314 }
1315
1316 arg = (step << DTRACE_LQUANTIZE_STEPSHIFT) |
1317 (nlevels << DTRACE_LQUANTIZE_LEVELSHIFT) |
1318 ((baseval << DTRACE_LQUANTIZE_BASESHIFT) &
1319 DTRACE_LQUANTIZE_BASEMASK);
1320
1321 assert(arg != 0);
1322
1323 isp = (dt_idsig_t *)aid->di_data;
1324
1325 if (isp->dis_auxinfo == 0) {
1326 /*
1327 * This is the first time we've seen an lquantize()
1328 * for this aggregation; we'll store our argument
1329 * as the auxiliary signature information.
1330 */
1331 isp->dis_auxinfo = arg;
1332 } else if ((oarg = isp->dis_auxinfo) != arg) {
1333 /*
1334 * If we have seen this lquantize() before and the
1335 * argument doesn't match the original argument, pick
1336 * the original argument apart to concisely report the
1337 * mismatch.
1338 */
1339 int obaseval = DTRACE_LQUANTIZE_BASE(oarg);
1340 int onlevels = DTRACE_LQUANTIZE_LEVELS(oarg);
1341 int ostep = DTRACE_LQUANTIZE_STEP(oarg);
1342
1343 if (obaseval != baseval) {
1344 dnerror(dnp, D_LQUANT_MATCHBASE, "lquantize( ) "
1345 "base (argument #1) doesn't match previous "
1346 "declaration: expected %d, found %d\n",
1347 obaseval, (int)baseval);
1348 }
1349
1350 if (onlevels * ostep != nlevels * step) {
1351 dnerror(dnp, D_LQUANT_MATCHLIM, "lquantize( ) "
1352 "limit (argument #2) doesn't match previous"
1353 " declaration: expected %d, found %d\n",
1354 obaseval + onlevels * ostep,
1355 (int)baseval + (int)nlevels * (int)step);
1356 }
1357
1358 if (ostep != step) {
1359 dnerror(dnp, D_LQUANT_MATCHSTEP, "lquantize( ) "
1360 "step (argument #3) doesn't match previous "
1361 "declaration: expected %d, found %d\n",
1362 ostep, (int)step);
1363 }
1364
1365 /*
1366 * We shouldn't be able to get here -- one of the
1367 * parameters must be mismatched if the arguments
1368 * didn't match.
1369 */
1370 assert(0);
1371 }
1372
1373 incr = arg3 != NULL ? arg3->dn_list : NULL;
1374 argmax = 5;
1375 }
1376
1377 if (fid->di_id == DTRACEAGG_LLQUANTIZE) {
1378 /*
1379 * For log/linear quantizations, we have between one and five
1380 * arguments in addition to the expression:
1381 *
1382 * arg1 => Factor
1383 * arg2 => Low magnitude
1384 * arg3 => High magnitude
1385 * arg4 => Number of steps per magnitude
1386 * arg5 => Quantization increment value (defaults to 1)
1387 */
1388 dt_node_t *llarg = dnp->dn_aggfun->dn_args->dn_list;
1389 uint64_t oarg, order, v;
1390 dt_idsig_t *isp;
1391 int i;
1392
1393 struct {
1394 char *str; /* string identifier */
1395 int badtype; /* error on bad type */
1396 int badval; /* error on bad value */
1397 int mismatch; /* error on bad match */
1398 int shift; /* shift value */
1399 uint16_t value; /* value itself */
1400 } args[] = {
1401 { "factor", D_LLQUANT_FACTORTYPE,
1402 D_LLQUANT_FACTORVAL, D_LLQUANT_FACTORMATCH,
1403 DTRACE_LLQUANTIZE_FACTORSHIFT },
1404 { "low magnitude", D_LLQUANT_LOWTYPE,
1405 D_LLQUANT_LOWVAL, D_LLQUANT_LOWMATCH,
1406 DTRACE_LLQUANTIZE_LOWSHIFT },
1407 { "high magnitude", D_LLQUANT_HIGHTYPE,
1408 D_LLQUANT_HIGHVAL, D_LLQUANT_HIGHMATCH,
1409 DTRACE_LLQUANTIZE_HIGHSHIFT },
1410 { "linear steps per magnitude", D_LLQUANT_NSTEPTYPE,
1411 D_LLQUANT_NSTEPVAL, D_LLQUANT_NSTEPMATCH,
1412 DTRACE_LLQUANTIZE_NSTEPSHIFT },
1413 { NULL }
1414 };
1415
1416 assert(arg == 0);
1417
1418 for (i = 0; args[i].str != NULL; i++) {
1419 if (llarg->dn_kind != DT_NODE_INT) {
1420 dnerror(llarg, args[i].badtype, "llquantize( ) "
1421 "argument #%d (%s) must be an "
1422 "integer constant\n", i + 1, args[i].str);
1423 }
1424
1425 if ((uint64_t)llarg->dn_value > UINT16_MAX) {
1426 dnerror(llarg, args[i].badval, "llquantize( ) "
1427 "argument #%d (%s) must be an unsigned "
1428 "16-bit quantity\n", i + 1, args[i].str);
1429 }
1430
1431 args[i].value = (uint16_t)llarg->dn_value;
1432
1433 assert(!(arg & (UINT16_MAX << args[i].shift)));
1434 arg |= ((uint64_t)args[i].value << args[i].shift);
1435 llarg = llarg->dn_list;
1436 }
1437
1438 assert(arg != 0);
1439
1440 if (args[0].value < 2) {
1441 dnerror(dnp, D_LLQUANT_FACTORSMALL, "llquantize( ) "
1442 "factor (argument #1) must be two or more\n");
1443 }
1444
1445 if (args[1].value >= args[2].value) {
1446 dnerror(dnp, D_LLQUANT_MAGRANGE, "llquantize( ) "
1447 "high magnitude (argument #3) must be greater "
1448 "than low magnitude (argument #2)\n");
1449 }
1450
1451 if (args[3].value < args[0].value) {
1452 dnerror(dnp, D_LLQUANT_FACTORNSTEPS, "llquantize( ) "
1453 "factor (argument #1) must be less than or "
1454 "equal to the number of linear steps per "
1455 "magnitude (argument #4)\n");
1456 }
1457
1458 for (v = args[0].value; v < args[3].value; v *= args[0].value)
1459 continue;
1460
1461 if ((args[3].value % args[0].value) || (v % args[3].value)) {
1462 dnerror(dnp, D_LLQUANT_FACTOREVEN, "llquantize( ) "
1463 "factor (argument #1) must evenly divide the "
1464 "number of steps per magnitude (argument #4), "
1465 "and the number of steps per magnitude must evenly "
1466 "divide a power of the factor\n");
1467 }
1468
1469 for (i = 0, order = 1; i < args[2].value; i++) {
1470 if (order * args[0].value > order) {
1471 order *= args[0].value;
1472 continue;
1473 }
1474
1475 dnerror(dnp, D_LLQUANT_MAGTOOBIG, "llquantize( ) "
1476 "factor (%d) raised to power of high magnitude "
1477 "(%d) overflows 64-bits\n", args[0].value,
1478 args[2].value);
1479 }
1480
1481 isp = (dt_idsig_t *)aid->di_data;
1482
1483 if (isp->dis_auxinfo == 0) {
1484 /*
1485 * This is the first time we've seen an llquantize()
1486 * for this aggregation; we'll store our argument
1487 * as the auxiliary signature information.
1488 */
1489 isp->dis_auxinfo = arg;
1490 } else if ((oarg = isp->dis_auxinfo) != arg) {
1491 /*
1492 * If we have seen this llquantize() before and the
1493 * argument doesn't match the original argument, pick
1494 * the original argument apart to concisely report the
1495 * mismatch.
1496 */
1497 int expected = 0, found = 0;
1498
1499 for (i = 0; expected == found; i++) {
1500 assert(args[i].str != NULL);
1501
1502 expected = (oarg >> args[i].shift) & UINT16_MAX;
1503 found = (arg >> args[i].shift) & UINT16_MAX;
1504 }
1505
1506 dnerror(dnp, args[i - 1].mismatch, "llquantize( ) "
1507 "%s (argument #%d) doesn't match previous "
1508 "declaration: expected %d, found %d\n",
1509 args[i - 1].str, i, expected, found);
1510 }
1511
1512 incr = llarg;
1513 argmax = 6;
1514 }
1515
1516 if (fid->di_id == DTRACEAGG_QUANTIZE) {
1517 incr = dnp->dn_aggfun->dn_args->dn_list;
1518 argmax = 2;
1519 }
1520
1521 if (incr != NULL) {
1522 if (!dt_node_is_scalar(incr)) {
1523 dnerror(dnp, D_PROTO_ARG, "%s( ) increment value "
1524 "(argument #%d) must be of scalar type\n",
1525 fid->di_name, argmax);
1526 }
1527
1528 if ((anp = incr->dn_list) != NULL) {
1529 int argc = argmax;
1530
1531 for (; anp != NULL; anp = anp->dn_list)
1532 argc++;
1533
1534 dnerror(incr, D_PROTO_LEN, "%s( ) prototype "
1535 "mismatch: %d args passed, at most %d expected",
1536 fid->di_name, argc, argmax);
1537 }
1538
1539 ap = dt_stmt_action(dtp, sdp);
1540 n++;
1541
1542 dt_cg(yypcb, incr);
1543 ap->dtad_difo = dt_as(yypcb);
1544 ap->dtad_difo->dtdo_rtype = dt_void_rtype;
1545 ap->dtad_kind = DTRACEACT_DIFEXPR;
1546 }
1547
1548 assert(sdp->dtsd_aggdata == NULL);
1549 sdp->dtsd_aggdata = aid;
1550
1551 ap = dt_stmt_action(dtp, sdp);
1552 assert(fid->di_kind == DT_IDENT_AGGFUNC);
1553 assert(DTRACEACT_ISAGG(fid->di_id));
1554 ap->dtad_kind = fid->di_id;
1555 ap->dtad_ntuple = n;
1556 ap->dtad_arg = arg;
1557
1558 if (dnp->dn_aggfun->dn_args != NULL) {
1559 dt_cg(yypcb, dnp->dn_aggfun->dn_args);
1560 ap->dtad_difo = dt_as(yypcb);
1561 }
1562 }
1563
1564 static void
dt_compile_one_clause(dtrace_hdl_t * dtp,dt_node_t * cnp,dt_node_t * pnp)1565 dt_compile_one_clause(dtrace_hdl_t *dtp, dt_node_t *cnp, dt_node_t *pnp)
1566 {
1567 dtrace_ecbdesc_t *edp;
1568 dtrace_stmtdesc_t *sdp;
1569 dt_node_t *dnp;
1570
1571 yylineno = pnp->dn_line;
1572 dt_setcontext(dtp, pnp->dn_desc);
1573 (void) dt_node_cook(cnp, DT_IDFLG_REF);
1574
1575 if (DT_TREEDUMP_PASS(dtp, 2))
1576 dt_node_printr(cnp, stderr, 0);
1577
1578 if ((edp = dt_ecbdesc_create(dtp, pnp->dn_desc)) == NULL)
1579 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
1580
1581 assert(yypcb->pcb_ecbdesc == NULL);
1582 yypcb->pcb_ecbdesc = edp;
1583
1584 if (cnp->dn_pred != NULL) {
1585 dt_cg(yypcb, cnp->dn_pred);
1586 edp->dted_pred.dtpdd_difo = dt_as(yypcb);
1587 }
1588
1589 if (cnp->dn_acts == NULL) {
1590 dt_stmt_append(dt_stmt_create(dtp, edp,
1591 cnp->dn_ctxattr, _dtrace_defattr), cnp);
1592 }
1593
1594 for (dnp = cnp->dn_acts; dnp != NULL; dnp = dnp->dn_list) {
1595 assert(yypcb->pcb_stmt == NULL);
1596 sdp = dt_stmt_create(dtp, edp, cnp->dn_ctxattr, cnp->dn_attr);
1597
1598 switch (dnp->dn_kind) {
1599 case DT_NODE_DEXPR:
1600 if (dnp->dn_expr->dn_kind == DT_NODE_AGG)
1601 dt_compile_agg(dtp, dnp->dn_expr, sdp);
1602 else
1603 dt_compile_exp(dtp, dnp, sdp);
1604 break;
1605 case DT_NODE_DFUNC:
1606 dt_compile_fun(dtp, dnp, sdp);
1607 break;
1608 case DT_NODE_AGG:
1609 dt_compile_agg(dtp, dnp, sdp);
1610 break;
1611 default:
1612 dnerror(dnp, D_UNKNOWN, "internal error -- node kind "
1613 "%u is not a valid statement\n", dnp->dn_kind);
1614 }
1615
1616 assert(yypcb->pcb_stmt == sdp);
1617 dt_stmt_append(sdp, dnp);
1618 }
1619
1620 assert(yypcb->pcb_ecbdesc == edp);
1621 dt_ecbdesc_release(dtp, edp);
1622 dt_endcontext(dtp);
1623 yypcb->pcb_ecbdesc = NULL;
1624 }
1625
1626 static void
dt_compile_clause(dtrace_hdl_t * dtp,dt_node_t * cnp)1627 dt_compile_clause(dtrace_hdl_t *dtp, dt_node_t *cnp)
1628 {
1629 dt_node_t *pnp;
1630
1631 for (pnp = cnp->dn_pdescs; pnp != NULL; pnp = pnp->dn_list)
1632 dt_compile_one_clause(dtp, cnp, pnp);
1633 }
1634
1635 static void
dt_compile_xlator(dt_node_t * dnp)1636 dt_compile_xlator(dt_node_t *dnp)
1637 {
1638 dt_xlator_t *dxp = dnp->dn_xlator;
1639 dt_node_t *mnp;
1640
1641 for (mnp = dnp->dn_members; mnp != NULL; mnp = mnp->dn_list) {
1642 assert(dxp->dx_membdif[mnp->dn_membid] == NULL);
1643 dt_cg(yypcb, mnp);
1644 dxp->dx_membdif[mnp->dn_membid] = dt_as(yypcb);
1645 }
1646 }
1647
1648 void
dt_setcontext(dtrace_hdl_t * dtp,dtrace_probedesc_t * pdp)1649 dt_setcontext(dtrace_hdl_t *dtp, dtrace_probedesc_t *pdp)
1650 {
1651 const dtrace_pattr_t *pap;
1652 dt_probe_t *prp;
1653 dt_provider_t *pvp;
1654 dt_ident_t *idp;
1655 char attrstr[8];
1656 int err;
1657
1658 /*
1659 * Both kernel and pid based providers are allowed to have names
1660 * ending with what could be interpreted as a number. We assume it's
1661 * a pid and that we may need to dynamically create probes for
1662 * that process if:
1663 *
1664 * (1) The provider doesn't exist, or,
1665 * (2) The provider exists and has DTRACE_PRIV_PROC privilege.
1666 *
1667 * On an error, dt_pid_create_probes() will set the error message
1668 * and tag -- we just have to longjmp() out of here.
1669 */
1670 if (isdigit(pdp->dtpd_provider[strlen(pdp->dtpd_provider) - 1]) &&
1671 ((pvp = dt_provider_lookup(dtp, pdp->dtpd_provider)) == NULL ||
1672 pvp->pv_desc.dtvd_priv.dtpp_flags & DTRACE_PRIV_PROC) &&
1673 dt_pid_create_probes(pdp, dtp, yypcb) != 0) {
1674 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
1675 }
1676
1677 /*
1678 * Call dt_probe_info() to get the probe arguments and attributes. If
1679 * a representative probe is found, set 'pap' to the probe provider's
1680 * attributes. Otherwise set 'pap' to default Unstable attributes.
1681 */
1682 if ((prp = dt_probe_info(dtp, pdp, &yypcb->pcb_pinfo)) == NULL) {
1683 pap = &_dtrace_prvdesc;
1684 err = dtrace_errno(dtp);
1685 bzero(&yypcb->pcb_pinfo, sizeof (dtrace_probeinfo_t));
1686 yypcb->pcb_pinfo.dtp_attr = pap->dtpa_provider;
1687 yypcb->pcb_pinfo.dtp_arga = pap->dtpa_args;
1688 } else {
1689 pap = &prp->pr_pvp->pv_desc.dtvd_attr;
1690 err = 0;
1691 }
1692
1693 if (err == EDT_NOPROBE && !(yypcb->pcb_cflags & DTRACE_C_ZDEFS)) {
1694 xyerror(D_PDESC_ZERO, "probe description %s:%s:%s:%s does not "
1695 "match any probes\n", pdp->dtpd_provider, pdp->dtpd_mod,
1696 pdp->dtpd_func, pdp->dtpd_name);
1697 }
1698
1699 if (err != EDT_NOPROBE && err != EDT_UNSTABLE && err != 0)
1700 xyerror(D_PDESC_INVAL, "%s\n", dtrace_errmsg(dtp, err));
1701
1702 dt_dprintf("set context to %s:%s:%s:%s [%u] prp=%p attr=%s argc=%d\n",
1703 pdp->dtpd_provider, pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name,
1704 pdp->dtpd_id, (void *)prp, dt_attr_str(yypcb->pcb_pinfo.dtp_attr,
1705 attrstr, sizeof (attrstr)), yypcb->pcb_pinfo.dtp_argc);
1706
1707 /*
1708 * Reset the stability attributes of D global variables that vary
1709 * based on the attributes of the provider and context itself.
1710 */
1711 if ((idp = dt_idhash_lookup(dtp->dt_globals, "probeprov")) != NULL)
1712 idp->di_attr = pap->dtpa_provider;
1713 if ((idp = dt_idhash_lookup(dtp->dt_globals, "probemod")) != NULL)
1714 idp->di_attr = pap->dtpa_mod;
1715 if ((idp = dt_idhash_lookup(dtp->dt_globals, "probefunc")) != NULL)
1716 idp->di_attr = pap->dtpa_func;
1717 if ((idp = dt_idhash_lookup(dtp->dt_globals, "probename")) != NULL)
1718 idp->di_attr = pap->dtpa_name;
1719 if ((idp = dt_idhash_lookup(dtp->dt_globals, "args")) != NULL)
1720 idp->di_attr = pap->dtpa_args;
1721
1722 yypcb->pcb_pdesc = pdp;
1723 yypcb->pcb_probe = prp;
1724 }
1725
1726 /*
1727 * Reset context-dependent variables and state at the end of cooking a D probe
1728 * definition clause. This ensures that external declarations between clauses
1729 * do not reference any stale context-dependent data from the previous clause.
1730 */
1731 void
dt_endcontext(dtrace_hdl_t * dtp)1732 dt_endcontext(dtrace_hdl_t *dtp)
1733 {
1734 static const char *const cvars[] = {
1735 "probeprov", "probemod", "probefunc", "probename", "args", NULL
1736 };
1737
1738 dt_ident_t *idp;
1739 int i;
1740
1741 for (i = 0; cvars[i] != NULL; i++) {
1742 if ((idp = dt_idhash_lookup(dtp->dt_globals, cvars[i])) != NULL)
1743 idp->di_attr = _dtrace_defattr;
1744 }
1745
1746 yypcb->pcb_pdesc = NULL;
1747 yypcb->pcb_probe = NULL;
1748 }
1749
1750 static int
dt_reduceid(dt_idhash_t * dhp,dt_ident_t * idp,dtrace_hdl_t * dtp)1751 dt_reduceid(dt_idhash_t *dhp, dt_ident_t *idp, dtrace_hdl_t *dtp)
1752 {
1753 if (idp->di_vers != 0 && idp->di_vers > dtp->dt_vmax)
1754 dt_idhash_delete(dhp, idp);
1755
1756 return (0);
1757 }
1758
1759 /*
1760 * When dtrace_setopt() is called for "version", it calls dt_reduce() to remove
1761 * any identifiers or translators that have been previously defined as bound to
1762 * a version greater than the specified version. Therefore, in our current
1763 * version implementation, establishing a binding is a one-way transformation.
1764 * In addition, no versioning is currently provided for types as our .d library
1765 * files do not define any types and we reserve prefixes DTRACE_ and dtrace_
1766 * for our exclusive use. If required, type versioning will require more work.
1767 */
1768 int
dt_reduce(dtrace_hdl_t * dtp,dt_version_t v)1769 dt_reduce(dtrace_hdl_t *dtp, dt_version_t v)
1770 {
1771 char s[DT_VERSION_STRMAX];
1772 dt_xlator_t *dxp, *nxp;
1773
1774 if (v > dtp->dt_vmax)
1775 return (dt_set_errno(dtp, EDT_VERSREDUCED));
1776 else if (v == dtp->dt_vmax)
1777 return (0); /* no reduction necessary */
1778
1779 dt_dprintf("reducing api version to %s\n",
1780 dt_version_num2str(v, s, sizeof (s)));
1781
1782 dtp->dt_vmax = v;
1783
1784 for (dxp = dt_list_next(&dtp->dt_xlators); dxp != NULL; dxp = nxp) {
1785 nxp = dt_list_next(dxp);
1786 if ((dxp->dx_souid.di_vers != 0 && dxp->dx_souid.di_vers > v) ||
1787 (dxp->dx_ptrid.di_vers != 0 && dxp->dx_ptrid.di_vers > v))
1788 dt_list_delete(&dtp->dt_xlators, dxp);
1789 }
1790
1791 (void) dt_idhash_iter(dtp->dt_macros, (dt_idhash_f *)dt_reduceid, dtp);
1792 (void) dt_idhash_iter(dtp->dt_aggs, (dt_idhash_f *)dt_reduceid, dtp);
1793 (void) dt_idhash_iter(dtp->dt_globals, (dt_idhash_f *)dt_reduceid, dtp);
1794 (void) dt_idhash_iter(dtp->dt_tls, (dt_idhash_f *)dt_reduceid, dtp);
1795
1796 return (0);
1797 }
1798
1799 /*
1800 * Fork and exec the cpp(1) preprocessor to run over the specified input file,
1801 * and return a FILE handle for the cpp output. We use the /dev/fd filesystem
1802 * here to simplify the code by leveraging file descriptor inheritance.
1803 */
1804 static FILE *
dt_preproc(dtrace_hdl_t * dtp,FILE * ifp)1805 dt_preproc(dtrace_hdl_t *dtp, FILE *ifp)
1806 {
1807 int argc = dtp->dt_cpp_argc;
1808 char **argv = malloc(sizeof (char *) * (argc + 5));
1809 FILE *ofp = tmpfile();
1810
1811 char ipath[20], opath[20]; /* big enough for /dev/fd/ + INT_MAX + \0 */
1812 char verdef[32]; /* big enough for -D__SUNW_D_VERSION=0x%08x + \0 */
1813
1814 struct sigaction act, oact;
1815 sigset_t mask, omask;
1816
1817 int wstat, estat;
1818 pid_t pid;
1819 off64_t off;
1820 int c;
1821
1822 if (argv == NULL || ofp == NULL) {
1823 (void) dt_set_errno(dtp, errno);
1824 goto err;
1825 }
1826
1827 /*
1828 * If the input is a seekable file, see if it is an interpreter file.
1829 * If we see #!, seek past the first line because cpp will choke on it.
1830 * We start cpp just prior to the \n at the end of this line so that
1831 * it still sees the newline, ensuring that #line values are correct.
1832 */
1833 if (isatty(fileno(ifp)) == 0 && (off = ftello64(ifp)) != -1) {
1834 if ((c = fgetc(ifp)) == '#' && (c = fgetc(ifp)) == '!') {
1835 for (off += 2; c != '\n'; off++) {
1836 if ((c = fgetc(ifp)) == EOF)
1837 break;
1838 }
1839 if (c == '\n')
1840 off--; /* start cpp just prior to \n */
1841 }
1842 (void) fflush(ifp);
1843 (void) fseeko64(ifp, off, SEEK_SET);
1844 }
1845
1846 (void) snprintf(ipath, sizeof (ipath), "/dev/fd/%d", fileno(ifp));
1847 (void) snprintf(opath, sizeof (opath), "/dev/fd/%d", fileno(ofp));
1848
1849 bcopy(dtp->dt_cpp_argv, argv, sizeof (char *) * argc);
1850
1851 (void) snprintf(verdef, sizeof (verdef),
1852 "-D__SUNW_D_VERSION=0x%08x", dtp->dt_vmax);
1853 argv[argc++] = verdef;
1854
1855 switch (dtp->dt_stdcmode) {
1856 case DT_STDC_XA:
1857 case DT_STDC_XT:
1858 argv[argc++] = "-D__STDC__=0";
1859 break;
1860 case DT_STDC_XC:
1861 argv[argc++] = "-D__STDC__=1";
1862 break;
1863 }
1864
1865 argv[argc++] = ipath;
1866 argv[argc++] = opath;
1867 argv[argc] = NULL;
1868
1869 /*
1870 * libdtrace must be able to be embedded in other programs that may
1871 * include application-specific signal handlers. Therefore, if we
1872 * need to fork to run cpp(1), we must avoid generating a SIGCHLD
1873 * that could confuse the containing application. To do this,
1874 * we block SIGCHLD and reset its disposition to SIG_DFL.
1875 * We restore our signal state once we are done.
1876 */
1877 (void) sigemptyset(&mask);
1878 (void) sigaddset(&mask, SIGCHLD);
1879 (void) sigprocmask(SIG_BLOCK, &mask, &omask);
1880
1881 bzero(&act, sizeof (act));
1882 act.sa_handler = SIG_DFL;
1883 (void) sigaction(SIGCHLD, &act, &oact);
1884
1885 if ((pid = fork1()) == -1) {
1886 (void) sigaction(SIGCHLD, &oact, NULL);
1887 (void) sigprocmask(SIG_SETMASK, &omask, NULL);
1888 (void) dt_set_errno(dtp, EDT_CPPFORK);
1889 goto err;
1890 }
1891
1892 if (pid == 0) {
1893 (void) execvp(dtp->dt_cpp_path, argv);
1894 _exit(errno == ENOENT ? 127 : 126);
1895 }
1896
1897 do {
1898 dt_dprintf("waiting for %s (PID %d)\n", dtp->dt_cpp_path,
1899 (int)pid);
1900 } while (waitpid(pid, &wstat, 0) == -1 && errno == EINTR);
1901
1902 (void) sigaction(SIGCHLD, &oact, NULL);
1903 (void) sigprocmask(SIG_SETMASK, &omask, NULL);
1904
1905 dt_dprintf("%s returned exit status 0x%x\n", dtp->dt_cpp_path, wstat);
1906 estat = WIFEXITED(wstat) ? WEXITSTATUS(wstat) : -1;
1907
1908 if (estat != 0) {
1909 switch (estat) {
1910 case 126:
1911 (void) dt_set_errno(dtp, EDT_CPPEXEC);
1912 break;
1913 case 127:
1914 (void) dt_set_errno(dtp, EDT_CPPENT);
1915 break;
1916 default:
1917 (void) dt_set_errno(dtp, EDT_CPPERR);
1918 }
1919 goto err;
1920 }
1921
1922 free(argv);
1923 (void) fflush(ofp);
1924 (void) fseek(ofp, 0, SEEK_SET);
1925 return (ofp);
1926
1927 err:
1928 free(argv);
1929 (void) fclose(ofp);
1930 return (NULL);
1931 }
1932
1933 static void
dt_lib_depend_error(dtrace_hdl_t * dtp,const char * format,...)1934 dt_lib_depend_error(dtrace_hdl_t *dtp, const char *format, ...)
1935 {
1936 va_list ap;
1937
1938 va_start(ap, format);
1939 dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap);
1940 va_end(ap);
1941 }
1942
1943 int
dt_lib_depend_add(dtrace_hdl_t * dtp,dt_list_t * dlp,const char * arg)1944 dt_lib_depend_add(dtrace_hdl_t *dtp, dt_list_t *dlp, const char *arg)
1945 {
1946 dt_lib_depend_t *dld;
1947 const char *end;
1948
1949 assert(arg != NULL);
1950
1951 if ((end = strrchr(arg, '/')) == NULL)
1952 return (dt_set_errno(dtp, EINVAL));
1953
1954 if ((dld = dt_zalloc(dtp, sizeof (dt_lib_depend_t))) == NULL)
1955 return (-1);
1956
1957 if ((dld->dtld_libpath = dt_alloc(dtp, MAXPATHLEN)) == NULL) {
1958 dt_free(dtp, dld);
1959 return (-1);
1960 }
1961
1962 (void) strlcpy(dld->dtld_libpath, arg, end - arg + 2);
1963 if ((dld->dtld_library = strdup(arg)) == NULL) {
1964 dt_free(dtp, dld->dtld_libpath);
1965 dt_free(dtp, dld);
1966 return (dt_set_errno(dtp, EDT_NOMEM));
1967 }
1968
1969 dt_list_append(dlp, dld);
1970 return (0);
1971 }
1972
1973 dt_lib_depend_t *
dt_lib_depend_lookup(dt_list_t * dld,const char * arg)1974 dt_lib_depend_lookup(dt_list_t *dld, const char *arg)
1975 {
1976 dt_lib_depend_t *dldn;
1977
1978 for (dldn = dt_list_next(dld); dldn != NULL;
1979 dldn = dt_list_next(dldn)) {
1980 if (strcmp(dldn->dtld_library, arg) == 0)
1981 return (dldn);
1982 }
1983
1984 return (NULL);
1985 }
1986
1987 /*
1988 * Go through all the library files, and, if any library dependencies exist for
1989 * that file, add it to that node's list of dependents. The result of this
1990 * will be a graph which can then be topologically sorted to produce a
1991 * compilation order.
1992 */
1993 static int
dt_lib_build_graph(dtrace_hdl_t * dtp)1994 dt_lib_build_graph(dtrace_hdl_t *dtp)
1995 {
1996 dt_lib_depend_t *dld, *dpld;
1997
1998 for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
1999 dld = dt_list_next(dld)) {
2000 char *library = dld->dtld_library;
2001
2002 for (dpld = dt_list_next(&dld->dtld_dependencies); dpld != NULL;
2003 dpld = dt_list_next(dpld)) {
2004 dt_lib_depend_t *dlda;
2005
2006 if ((dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep,
2007 dpld->dtld_library)) == NULL) {
2008 dt_lib_depend_error(dtp,
2009 "Invalid library dependency in %s: %s\n",
2010 dld->dtld_library, dpld->dtld_library);
2011
2012 return (dt_set_errno(dtp, EDT_COMPILER));
2013 }
2014
2015 if ((dt_lib_depend_add(dtp, &dlda->dtld_dependents,
2016 library)) != 0) {
2017 return (-1); /* preserve dt_errno */
2018 }
2019 }
2020 }
2021 return (0);
2022 }
2023
2024 static int
dt_topo_sort(dtrace_hdl_t * dtp,dt_lib_depend_t * dld,int * count)2025 dt_topo_sort(dtrace_hdl_t *dtp, dt_lib_depend_t *dld, int *count)
2026 {
2027 dt_lib_depend_t *dpld, *dlda, *new;
2028
2029 dld->dtld_start = ++(*count);
2030
2031 for (dpld = dt_list_next(&dld->dtld_dependents); dpld != NULL;
2032 dpld = dt_list_next(dpld)) {
2033 dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep,
2034 dpld->dtld_library);
2035 assert(dlda != NULL);
2036
2037 if (dlda->dtld_start == 0 &&
2038 dt_topo_sort(dtp, dlda, count) == -1)
2039 return (-1);
2040 }
2041
2042 if ((new = dt_zalloc(dtp, sizeof (dt_lib_depend_t))) == NULL)
2043 return (-1);
2044
2045 if ((new->dtld_library = strdup(dld->dtld_library)) == NULL) {
2046 dt_free(dtp, new);
2047 return (dt_set_errno(dtp, EDT_NOMEM));
2048 }
2049
2050 new->dtld_start = dld->dtld_start;
2051 new->dtld_finish = dld->dtld_finish = ++(*count);
2052 dt_list_prepend(&dtp->dt_lib_dep_sorted, new);
2053
2054 dt_dprintf("library %s sorted (%d/%d)\n", new->dtld_library,
2055 new->dtld_start, new->dtld_finish);
2056
2057 return (0);
2058 }
2059
2060 static int
dt_lib_depend_sort(dtrace_hdl_t * dtp)2061 dt_lib_depend_sort(dtrace_hdl_t *dtp)
2062 {
2063 dt_lib_depend_t *dld, *dpld, *dlda;
2064 int count = 0;
2065
2066 if (dt_lib_build_graph(dtp) == -1)
2067 return (-1); /* preserve dt_errno */
2068
2069 /*
2070 * Perform a topological sort of the graph that hangs off
2071 * dtp->dt_lib_dep. The result of this process will be a
2072 * dependency ordered list located at dtp->dt_lib_dep_sorted.
2073 */
2074 for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
2075 dld = dt_list_next(dld)) {
2076 if (dld->dtld_start == 0 &&
2077 dt_topo_sort(dtp, dld, &count) == -1)
2078 return (-1); /* preserve dt_errno */;
2079 }
2080
2081 /*
2082 * Check the graph for cycles. If an ancestor's finishing time is
2083 * less than any of its dependent's finishing times then a back edge
2084 * exists in the graph and this is a cycle.
2085 */
2086 for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
2087 dld = dt_list_next(dld)) {
2088 for (dpld = dt_list_next(&dld->dtld_dependents); dpld != NULL;
2089 dpld = dt_list_next(dpld)) {
2090 dlda = dt_lib_depend_lookup(&dtp->dt_lib_dep_sorted,
2091 dpld->dtld_library);
2092 assert(dlda != NULL);
2093
2094 if (dlda->dtld_finish > dld->dtld_finish) {
2095 dt_lib_depend_error(dtp,
2096 "Cyclic dependency detected: %s => %s\n",
2097 dld->dtld_library, dpld->dtld_library);
2098
2099 return (dt_set_errno(dtp, EDT_COMPILER));
2100 }
2101 }
2102 }
2103
2104 return (0);
2105 }
2106
2107 static void
dt_lib_depend_free(dtrace_hdl_t * dtp)2108 dt_lib_depend_free(dtrace_hdl_t *dtp)
2109 {
2110 dt_lib_depend_t *dld, *dlda;
2111
2112 while ((dld = dt_list_next(&dtp->dt_lib_dep)) != NULL) {
2113 while ((dlda = dt_list_next(&dld->dtld_dependencies)) != NULL) {
2114 dt_list_delete(&dld->dtld_dependencies, dlda);
2115 dt_free(dtp, dlda->dtld_library);
2116 dt_free(dtp, dlda->dtld_libpath);
2117 dt_free(dtp, dlda);
2118 }
2119 while ((dlda = dt_list_next(&dld->dtld_dependents)) != NULL) {
2120 dt_list_delete(&dld->dtld_dependents, dlda);
2121 dt_free(dtp, dlda->dtld_library);
2122 dt_free(dtp, dlda->dtld_libpath);
2123 dt_free(dtp, dlda);
2124 }
2125 dt_list_delete(&dtp->dt_lib_dep, dld);
2126 dt_free(dtp, dld->dtld_library);
2127 dt_free(dtp, dld->dtld_libpath);
2128 dt_free(dtp, dld);
2129 }
2130
2131 while ((dld = dt_list_next(&dtp->dt_lib_dep_sorted)) != NULL) {
2132 dt_list_delete(&dtp->dt_lib_dep_sorted, dld);
2133 dt_free(dtp, dld->dtld_library);
2134 dt_free(dtp, dld);
2135 }
2136 }
2137
2138 /*
2139 * Open all the .d library files found in the specified directory and
2140 * compile each one of them. We silently ignore any missing directories and
2141 * other files found therein. We only fail (and thereby fail dt_load_libs()) if
2142 * we fail to compile a library and the error is something other than #pragma D
2143 * depends_on. Dependency errors are silently ignored to permit a library
2144 * directory to contain libraries which may not be accessible depending on our
2145 * privileges.
2146 */
2147 static int
dt_load_libs_dir(dtrace_hdl_t * dtp,const char * path)2148 dt_load_libs_dir(dtrace_hdl_t *dtp, const char *path)
2149 {
2150 struct dirent *dp;
2151 const char *p, *end;
2152 DIR *dirp;
2153
2154 char fname[PATH_MAX];
2155 FILE *fp;
2156 void *rv;
2157 dt_lib_depend_t *dld;
2158
2159 if ((dirp = opendir(path)) == NULL) {
2160 dt_dprintf("skipping lib dir %s: %s\n", path, strerror(errno));
2161 return (0);
2162 }
2163
2164 /* First, parse each file for library dependencies. */
2165 while ((dp = readdir(dirp)) != NULL) {
2166 if ((p = strrchr(dp->d_name, '.')) == NULL || strcmp(p, ".d"))
2167 continue; /* skip any filename not ending in .d */
2168
2169 (void) snprintf(fname, sizeof (fname),
2170 "%s/%s", path, dp->d_name);
2171
2172 if ((fp = fopen(fname, "rF")) == NULL) {
2173 dt_dprintf("skipping library %s: %s\n",
2174 fname, strerror(errno));
2175 continue;
2176 }
2177
2178 /*
2179 * Skip files whose name match an already processed library
2180 */
2181 for (dld = dt_list_next(&dtp->dt_lib_dep); dld != NULL;
2182 dld = dt_list_next(dld)) {
2183 end = strrchr(dld->dtld_library, '/');
2184 /* dt_lib_depend_add ensures this */
2185 assert(end != NULL);
2186 if (strcmp(end + 1, dp->d_name) == 0)
2187 break;
2188 }
2189
2190 if (dld != NULL) {
2191 dt_dprintf("skipping library %s, already processed "
2192 "library with the same name: %s", dp->d_name,
2193 dld->dtld_library);
2194 (void) fclose(fp);
2195 continue;
2196 }
2197
2198 dtp->dt_filetag = fname;
2199 if (dt_lib_depend_add(dtp, &dtp->dt_lib_dep, fname) != 0) {
2200 (void) fclose(fp);
2201 return (-1); /* preserve dt_errno */
2202 }
2203
2204 rv = dt_compile(dtp, DT_CTX_DPROG,
2205 DTRACE_PROBESPEC_NAME, NULL,
2206 DTRACE_C_EMPTY | DTRACE_C_CTL, 0, NULL, fp, NULL);
2207
2208 if (rv != NULL && dtp->dt_errno &&
2209 (dtp->dt_errno != EDT_COMPILER ||
2210 dtp->dt_errtag != dt_errtag(D_PRAGMA_DEPEND))) {
2211 (void) fclose(fp);
2212 return (-1); /* preserve dt_errno */
2213 }
2214
2215 if (dtp->dt_errno)
2216 dt_dprintf("error parsing library %s: %s\n",
2217 fname, dtrace_errmsg(dtp, dtrace_errno(dtp)));
2218
2219 (void) fclose(fp);
2220 dtp->dt_filetag = NULL;
2221 }
2222
2223 (void) closedir(dirp);
2224
2225 return (0);
2226 }
2227
2228 /*
2229 * Perform a topological sorting of all the libraries found across the entire
2230 * dt_lib_path. Once sorted, compile each one in topological order to cache its
2231 * inlines and translators, etc. We silently ignore any missing directories and
2232 * other files found therein. We only fail (and thereby fail dt_load_libs()) if
2233 * we fail to compile a library and the error is something other than #pragma D
2234 * depends_on. Dependency errors are silently ignored to permit a library
2235 * directory to contain libraries which may not be accessible depending on our
2236 * privileges.
2237 */
2238 static int
dt_load_libs_sort(dtrace_hdl_t * dtp)2239 dt_load_libs_sort(dtrace_hdl_t *dtp)
2240 {
2241 dtrace_prog_t *pgp;
2242 FILE *fp;
2243 dt_lib_depend_t *dld;
2244
2245 /*
2246 * Finish building the graph containing the library dependencies
2247 * and perform a topological sort to generate an ordered list
2248 * for compilation.
2249 */
2250 if (dt_lib_depend_sort(dtp) == -1)
2251 goto err;
2252
2253 for (dld = dt_list_next(&dtp->dt_lib_dep_sorted); dld != NULL;
2254 dld = dt_list_next(dld)) {
2255
2256 if ((fp = fopen(dld->dtld_library, "r")) == NULL) {
2257 dt_dprintf("skipping library %s: %s\n",
2258 dld->dtld_library, strerror(errno));
2259 continue;
2260 }
2261
2262 dtp->dt_filetag = dld->dtld_library;
2263 pgp = dtrace_program_fcompile(dtp, fp, DTRACE_C_EMPTY, 0, NULL);
2264 (void) fclose(fp);
2265 dtp->dt_filetag = NULL;
2266
2267 if (pgp == NULL && (dtp->dt_errno != EDT_COMPILER ||
2268 dtp->dt_errtag != dt_errtag(D_PRAGMA_DEPEND)))
2269 goto err;
2270
2271 if (pgp == NULL) {
2272 dt_dprintf("skipping library %s: %s\n",
2273 dld->dtld_library,
2274 dtrace_errmsg(dtp, dtrace_errno(dtp)));
2275 } else {
2276 dld->dtld_loaded = B_TRUE;
2277 dt_program_destroy(dtp, pgp);
2278 }
2279 }
2280
2281 dt_lib_depend_free(dtp);
2282 return (0);
2283
2284 err:
2285 dt_lib_depend_free(dtp);
2286 return (-1); /* preserve dt_errno */
2287 }
2288
2289 /*
2290 * Load the contents of any appropriate DTrace .d library files. These files
2291 * contain inlines and translators that will be cached by the compiler. We
2292 * defer this activity until the first compile to permit libdtrace clients to
2293 * add their own library directories and so that we can properly report errors.
2294 */
2295 static int
dt_load_libs(dtrace_hdl_t * dtp)2296 dt_load_libs(dtrace_hdl_t *dtp)
2297 {
2298 dt_dirpath_t *dirp;
2299
2300 if (dtp->dt_cflags & DTRACE_C_NOLIBS)
2301 return (0); /* libraries already processed */
2302
2303 dtp->dt_cflags |= DTRACE_C_NOLIBS;
2304
2305 /*
2306 * /usr/lib/dtrace is always at the head of the list. The rest of the
2307 * list is specified in the precedence order the user requested. Process
2308 * everything other than the head first. DTRACE_C_NOLIBS has already
2309 * been spcified so dt_vopen will ensure that there is always one entry
2310 * in dt_lib_path.
2311 */
2312 for (dirp = dt_list_next(dt_list_next(&dtp->dt_lib_path));
2313 dirp != NULL; dirp = dt_list_next(dirp)) {
2314 if (dt_load_libs_dir(dtp, dirp->dir_path) != 0) {
2315 dtp->dt_cflags &= ~DTRACE_C_NOLIBS;
2316 return (-1); /* errno is set for us */
2317 }
2318 }
2319
2320 /* Handle /usr/lib/dtrace */
2321 dirp = dt_list_next(&dtp->dt_lib_path);
2322 if (dt_load_libs_dir(dtp, dirp->dir_path) != 0) {
2323 dtp->dt_cflags &= ~DTRACE_C_NOLIBS;
2324 return (-1); /* errno is set for us */
2325 }
2326
2327 if (dt_load_libs_sort(dtp) < 0)
2328 return (-1); /* errno is set for us */
2329
2330 return (0);
2331 }
2332
2333 static void *
dt_compile(dtrace_hdl_t * dtp,int context,dtrace_probespec_t pspec,void * arg,uint_t cflags,int argc,char * const argv[],FILE * fp,const char * s)2334 dt_compile(dtrace_hdl_t *dtp, int context, dtrace_probespec_t pspec, void *arg,
2335 uint_t cflags, int argc, char *const argv[], FILE *fp, const char *s)
2336 {
2337 dt_node_t *dnp;
2338 dt_decl_t *ddp;
2339 dt_pcb_t pcb;
2340 void *volatile rv;
2341 int err;
2342
2343 if ((fp == NULL && s == NULL) || (cflags & ~DTRACE_C_MASK) != 0) {
2344 (void) dt_set_errno(dtp, EINVAL);
2345 return (NULL);
2346 }
2347
2348 if (dt_list_next(&dtp->dt_lib_path) != NULL && dt_load_libs(dtp) != 0)
2349 return (NULL); /* errno is set for us */
2350
2351 if (dtp->dt_globals->dh_nelems != 0)
2352 (void) dt_idhash_iter(dtp->dt_globals, dt_idreset, NULL);
2353
2354 if (dtp->dt_tls->dh_nelems != 0)
2355 (void) dt_idhash_iter(dtp->dt_tls, dt_idreset, NULL);
2356
2357 if (fp && (cflags & DTRACE_C_CPP) && (fp = dt_preproc(dtp, fp)) == NULL)
2358 return (NULL); /* errno is set for us */
2359
2360 dt_pcb_push(dtp, &pcb);
2361
2362 pcb.pcb_fileptr = fp;
2363 pcb.pcb_string = s;
2364 pcb.pcb_strptr = s;
2365 pcb.pcb_strlen = s ? strlen(s) : 0;
2366 pcb.pcb_sargc = argc;
2367 pcb.pcb_sargv = argv;
2368 pcb.pcb_sflagv = argc ? calloc(argc, sizeof (ushort_t)) : NULL;
2369 pcb.pcb_pspec = pspec;
2370 pcb.pcb_cflags = dtp->dt_cflags | cflags;
2371 pcb.pcb_amin = dtp->dt_amin;
2372 pcb.pcb_yystate = -1;
2373 pcb.pcb_context = context;
2374 pcb.pcb_token = context;
2375
2376 if (context != DT_CTX_DPROG)
2377 yybegin(YYS_EXPR);
2378 else if (cflags & DTRACE_C_CTL)
2379 yybegin(YYS_CONTROL);
2380 else
2381 yybegin(YYS_CLAUSE);
2382
2383 if ((err = setjmp(yypcb->pcb_jmpbuf)) != 0)
2384 goto out;
2385
2386 if (yypcb->pcb_sargc != 0 && yypcb->pcb_sflagv == NULL)
2387 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2388
2389 yypcb->pcb_idents = dt_idhash_create("ambiguous", NULL, 0, 0);
2390 yypcb->pcb_locals = dt_idhash_create("clause local", NULL,
2391 DIF_VAR_OTHER_UBASE, DIF_VAR_OTHER_MAX);
2392
2393 if (yypcb->pcb_idents == NULL || yypcb->pcb_locals == NULL)
2394 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
2395
2396 /*
2397 * Invoke the parser to evaluate the D source code. If any errors
2398 * occur during parsing, an error function will be called and we
2399 * will longjmp back to pcb_jmpbuf to abort. If parsing succeeds,
2400 * we optionally display the parse tree if debugging is enabled.
2401 */
2402 if (yyparse() != 0 || yypcb->pcb_root == NULL)
2403 xyerror(D_EMPTY, "empty D program translation unit\n");
2404
2405 yybegin(YYS_DONE);
2406
2407 if (cflags & DTRACE_C_CTL)
2408 goto out;
2409
2410 if (context != DT_CTX_DTYPE && DT_TREEDUMP_PASS(dtp, 1))
2411 dt_node_printr(yypcb->pcb_root, stderr, 0);
2412
2413 if (yypcb->pcb_pragmas != NULL)
2414 (void) dt_idhash_iter(yypcb->pcb_pragmas, dt_idpragma, NULL);
2415
2416 if (argc > 1 && !(yypcb->pcb_cflags & DTRACE_C_ARGREF) &&
2417 !(yypcb->pcb_sflagv[argc - 1] & DT_IDFLG_REF)) {
2418 xyerror(D_MACRO_UNUSED, "extraneous argument '%s' ($%d is "
2419 "not referenced)\n", yypcb->pcb_sargv[argc - 1], argc - 1);
2420 }
2421
2422 /*
2423 * Perform sugar transformations (for "if" / "else") and replace the
2424 * existing clause chain with the new one.
2425 */
2426 if (context == DT_CTX_DPROG) {
2427 dt_node_t *dnp, *next_dnp;
2428 dt_node_t *new_list = NULL;
2429
2430 for (dnp = yypcb->pcb_root->dn_list;
2431 dnp != NULL; dnp = next_dnp) {
2432 /* remove this node from the list */
2433 next_dnp = dnp->dn_list;
2434 dnp->dn_list = NULL;
2435
2436 if (dnp->dn_kind == DT_NODE_CLAUSE)
2437 dnp = dt_compile_sugar(dtp, dnp);
2438 /* append node to the new list */
2439 new_list = dt_node_link(new_list, dnp);
2440 }
2441 yypcb->pcb_root->dn_list = new_list;
2442 }
2443
2444 /*
2445 * If we have successfully created a parse tree for a D program, loop
2446 * over the clauses and actions and instantiate the corresponding
2447 * libdtrace program. If we are parsing a D expression, then we
2448 * simply run the code generator and assembler on the resulting tree.
2449 */
2450 switch (context) {
2451 case DT_CTX_DPROG:
2452 assert(yypcb->pcb_root->dn_kind == DT_NODE_PROG);
2453
2454 if ((dnp = yypcb->pcb_root->dn_list) == NULL &&
2455 !(yypcb->pcb_cflags & DTRACE_C_EMPTY))
2456 xyerror(D_EMPTY, "empty D program translation unit\n");
2457
2458 if ((yypcb->pcb_prog = dt_program_create(dtp)) == NULL)
2459 longjmp(yypcb->pcb_jmpbuf, dtrace_errno(dtp));
2460
2461 for (; dnp != NULL; dnp = dnp->dn_list) {
2462 switch (dnp->dn_kind) {
2463 case DT_NODE_CLAUSE:
2464 if (DT_TREEDUMP_PASS(dtp, 4))
2465 dt_printd(dnp, stderr, 0);
2466 dt_compile_clause(dtp, dnp);
2467 break;
2468 case DT_NODE_XLATOR:
2469 if (dtp->dt_xlatemode == DT_XL_DYNAMIC)
2470 dt_compile_xlator(dnp);
2471 break;
2472 case DT_NODE_PROVIDER:
2473 (void) dt_node_cook(dnp, DT_IDFLG_REF);
2474 break;
2475 }
2476 }
2477
2478 yypcb->pcb_prog->dp_xrefs = yypcb->pcb_asxrefs;
2479 yypcb->pcb_prog->dp_xrefslen = yypcb->pcb_asxreflen;
2480 yypcb->pcb_asxrefs = NULL;
2481 yypcb->pcb_asxreflen = 0;
2482
2483 rv = yypcb->pcb_prog;
2484 break;
2485
2486 case DT_CTX_DEXPR:
2487 (void) dt_node_cook(yypcb->pcb_root, DT_IDFLG_REF);
2488 dt_cg(yypcb, yypcb->pcb_root);
2489 rv = dt_as(yypcb);
2490 break;
2491
2492 case DT_CTX_DTYPE:
2493 ddp = (dt_decl_t *)yypcb->pcb_root; /* root is really a decl */
2494 err = dt_decl_type(ddp, arg);
2495 dt_decl_free(ddp);
2496
2497 if (err != 0)
2498 longjmp(yypcb->pcb_jmpbuf, EDT_COMPILER);
2499
2500 rv = NULL;
2501 break;
2502 }
2503
2504 out:
2505 if (context != DT_CTX_DTYPE && yypcb->pcb_root != NULL &&
2506 DT_TREEDUMP_PASS(dtp, 3))
2507 dt_node_printr(yypcb->pcb_root, stderr, 0);
2508
2509 if (dtp->dt_cdefs_fd != -1 && (ftruncate64(dtp->dt_cdefs_fd, 0) == -1 ||
2510 lseek64(dtp->dt_cdefs_fd, 0, SEEK_SET) == -1 ||
2511 ctf_write(dtp->dt_cdefs->dm_ctfp, dtp->dt_cdefs_fd) == CTF_ERR))
2512 dt_dprintf("failed to update CTF cache: %s\n", strerror(errno));
2513
2514 if (dtp->dt_ddefs_fd != -1 && (ftruncate64(dtp->dt_ddefs_fd, 0) == -1 ||
2515 lseek64(dtp->dt_ddefs_fd, 0, SEEK_SET) == -1 ||
2516 ctf_write(dtp->dt_ddefs->dm_ctfp, dtp->dt_ddefs_fd) == CTF_ERR))
2517 dt_dprintf("failed to update CTF cache: %s\n", strerror(errno));
2518
2519 if (yypcb->pcb_fileptr && (cflags & DTRACE_C_CPP))
2520 (void) fclose(yypcb->pcb_fileptr); /* close dt_preproc() file */
2521
2522 dt_pcb_pop(dtp, err);
2523 (void) dt_set_errno(dtp, err);
2524 return (err ? NULL : rv);
2525 }
2526
2527 dtrace_prog_t *
dtrace_program_strcompile(dtrace_hdl_t * dtp,const char * s,dtrace_probespec_t spec,uint_t cflags,int argc,char * const argv[])2528 dtrace_program_strcompile(dtrace_hdl_t *dtp, const char *s,
2529 dtrace_probespec_t spec, uint_t cflags, int argc, char *const argv[])
2530 {
2531 return (dt_compile(dtp, DT_CTX_DPROG,
2532 spec, NULL, cflags, argc, argv, NULL, s));
2533 }
2534
2535 dtrace_prog_t *
dtrace_program_fcompile(dtrace_hdl_t * dtp,FILE * fp,uint_t cflags,int argc,char * const argv[])2536 dtrace_program_fcompile(dtrace_hdl_t *dtp, FILE *fp,
2537 uint_t cflags, int argc, char *const argv[])
2538 {
2539 return (dt_compile(dtp, DT_CTX_DPROG,
2540 DTRACE_PROBESPEC_NAME, NULL, cflags, argc, argv, fp, NULL));
2541 }
2542
2543 int
dtrace_type_strcompile(dtrace_hdl_t * dtp,const char * s,dtrace_typeinfo_t * dtt)2544 dtrace_type_strcompile(dtrace_hdl_t *dtp, const char *s, dtrace_typeinfo_t *dtt)
2545 {
2546 (void) dt_compile(dtp, DT_CTX_DTYPE,
2547 DTRACE_PROBESPEC_NONE, dtt, 0, 0, NULL, NULL, s);
2548 return (dtp->dt_errno ? -1 : 0);
2549 }
2550
2551 int
dtrace_type_fcompile(dtrace_hdl_t * dtp,FILE * fp,dtrace_typeinfo_t * dtt)2552 dtrace_type_fcompile(dtrace_hdl_t *dtp, FILE *fp, dtrace_typeinfo_t *dtt)
2553 {
2554 (void) dt_compile(dtp, DT_CTX_DTYPE,
2555 DTRACE_PROBESPEC_NONE, dtt, 0, 0, NULL, fp, NULL);
2556 return (dtp->dt_errno ? -1 : 0);
2557 }
2558