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