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