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_ident.c
+++ new/usr/src/lib/libdtrace/common/dt_ident.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) 2013 by Delphix. All rights reserved.
25 + * Copyright (c) 2013 Joyent, Inc. All rights reserved.
24 26 */
25 27
26 28 #include <sys/sysmacros.h>
27 29 #include <strings.h>
28 30 #include <stdlib.h>
29 31 #include <alloca.h>
30 32 #include <assert.h>
31 33 #include <errno.h>
32 34 #include <ctype.h>
33 35 #include <sys/procfs_isa.h>
34 36 #include <limits.h>
35 37
36 38 #include <dt_ident.h>
37 39 #include <dt_parser.h>
38 40 #include <dt_provider.h>
39 41 #include <dt_strtab.h>
40 42 #include <dt_impl.h>
41 43
42 44 /*
43 45 * Common code for cooking an identifier that uses a typed signature list (we
44 46 * use this for associative arrays and functions). If the argument list is
45 47 * of the same length and types, then return the return type. Otherwise
46 48 * print an appropriate compiler error message and abort the compile.
47 49 */
48 50 static void
49 51 dt_idcook_sign(dt_node_t *dnp, dt_ident_t *idp,
50 52 int argc, dt_node_t *args, const char *prefix, const char *suffix)
51 53 {
52 54 dt_idsig_t *isp = idp->di_data;
53 55 int i, compat, mismatch, arglimit, iskey;
54 56
55 57 char n1[DT_TYPE_NAMELEN];
56 58 char n2[DT_TYPE_NAMELEN];
57 59
58 60 iskey = idp->di_kind == DT_IDENT_ARRAY || idp->di_kind == DT_IDENT_AGG;
59 61
60 62 if (isp->dis_varargs >= 0) {
61 63 mismatch = argc < isp->dis_varargs;
62 64 arglimit = isp->dis_varargs;
63 65 } else if (isp->dis_optargs >= 0) {
64 66 mismatch = (argc < isp->dis_optargs || argc > isp->dis_argc);
65 67 arglimit = argc;
66 68 } else {
67 69 mismatch = argc != isp->dis_argc;
68 70 arglimit = isp->dis_argc;
69 71 }
70 72
71 73 if (mismatch) {
72 74 xyerror(D_PROTO_LEN, "%s%s%s prototype mismatch: %d %s%s"
73 75 "passed, %s%d expected\n", prefix, idp->di_name, suffix,
74 76 argc, iskey ? "key" : "arg", argc == 1 ? " " : "s ",
75 77 isp->dis_optargs >= 0 ? "at least " : "",
76 78 isp->dis_optargs >= 0 ? isp->dis_optargs : arglimit);
77 79 }
78 80
79 81 for (i = 0; i < arglimit; i++, args = args->dn_list) {
80 82 if (isp->dis_args[i].dn_ctfp != NULL)
81 83 compat = dt_node_is_argcompat(&isp->dis_args[i], args);
82 84 else
83 85 compat = 1; /* "@" matches any type */
84 86
85 87 if (!compat) {
86 88 xyerror(D_PROTO_ARG,
87 89 "%s%s%s %s #%d is incompatible with "
↓ open down ↓ |
54 lines elided |
↑ open up ↑ |
88 90 "prototype:\n\tprototype: %s\n\t%9s: %s\n",
89 91 prefix, idp->di_name, suffix,
90 92 iskey ? "key" : "argument", i + 1,
91 93 dt_node_type_name(&isp->dis_args[i], n1,
92 94 sizeof (n1)),
93 95 iskey ? "key" : "argument",
94 96 dt_node_type_name(args, n2, sizeof (n2)));
95 97 }
96 98 }
97 99
98 - dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type);
100 + dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type, B_FALSE);
99 101 }
100 102
101 103 /*
102 104 * Cook an associative array identifier. If this is the first time we are
103 105 * cooking this array, create its signature based on the argument list.
104 106 * Otherwise validate the argument list against the existing signature.
105 107 */
106 108 static void
107 109 dt_idcook_assc(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args)
108 110 {
109 111 if (idp->di_data == NULL) {
110 112 dt_idsig_t *isp = idp->di_data = malloc(sizeof (dt_idsig_t));
111 113 char n[DT_TYPE_NAMELEN];
112 114 int i;
113 115
114 116 if (isp == NULL)
115 117 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
116 118
117 119 isp->dis_varargs = -1;
118 120 isp->dis_optargs = -1;
119 121 isp->dis_argc = argc;
120 122 isp->dis_args = NULL;
121 123 isp->dis_auxinfo = 0;
122 124
123 125 if (argc != 0 && (isp->dis_args = calloc(argc,
124 126 sizeof (dt_node_t))) == NULL) {
125 127 idp->di_data = NULL;
126 128 free(isp);
127 129 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
128 130 }
129 131
130 132 /*
131 133 * If this identifier has not been explicitly declared earlier,
132 134 * set the identifier's base type to be our special type <DYN>.
133 135 * If this ident is an aggregation, it will remain as is. If
134 136 * this ident is an associative array, it will be reassigned
135 137 * based on the result type of the first assignment statement.
136 138 */
137 139 if (!(idp->di_flags & DT_IDFLG_DECL)) {
138 140 idp->di_ctfp = DT_DYN_CTFP(yypcb->pcb_hdl);
139 141 idp->di_type = DT_DYN_TYPE(yypcb->pcb_hdl);
140 142 }
141 143
142 144 for (i = 0; i < argc; i++, args = args->dn_list) {
143 145 if (dt_node_is_dynamic(args) || dt_node_is_void(args)) {
144 146 xyerror(D_KEY_TYPE, "%s expression may not be "
145 147 "used as %s index: key #%d\n",
146 148 dt_node_type_name(args, n, sizeof (n)),
↓ open down ↓ |
38 lines elided |
↑ open up ↑ |
147 149 dt_idkind_name(idp->di_kind), i + 1);
148 150 }
149 151
150 152 dt_node_type_propagate(args, &isp->dis_args[i]);
151 153 isp->dis_args[i].dn_list = &isp->dis_args[i + 1];
152 154 }
153 155
154 156 if (argc != 0)
155 157 isp->dis_args[argc - 1].dn_list = NULL;
156 158
157 - dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type);
159 + dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type, B_FALSE);
158 160
159 161 } else {
160 162 dt_idcook_sign(dnp, idp, argc, args,
161 163 idp->di_kind == DT_IDENT_AGG ? "@" : "", "[ ]");
162 164 }
163 165 }
164 166
165 167 /*
166 168 * Cook a function call. If this is the first time we are cooking this
167 169 * identifier, create its type signature based on predefined prototype stored
168 170 * in di_iarg. We then validate the argument list against this signature.
169 171 */
170 172 static void
171 173 dt_idcook_func(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args)
172 174 {
173 175 if (idp->di_data == NULL) {
174 176 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
175 177 dtrace_typeinfo_t dtt;
176 178 dt_idsig_t *isp;
177 179 char *s, *p1, *p2;
178 180 int i = 0;
179 181
180 182 assert(idp->di_iarg != NULL);
181 183 s = strdupa(idp->di_iarg);
182 184
183 185 if ((p2 = strrchr(s, ')')) != NULL)
184 186 *p2 = '\0'; /* mark end of parameter list string */
185 187
186 188 if ((p1 = strchr(s, '(')) != NULL)
187 189 *p1++ = '\0'; /* mark end of return type string */
188 190
189 191 if (p1 == NULL || p2 == NULL) {
190 192 xyerror(D_UNKNOWN, "internal error: malformed entry "
191 193 "for built-in function %s\n", idp->di_name);
192 194 }
193 195
194 196 for (p2 = p1; *p2 != '\0'; p2++) {
195 197 if (!isspace(*p2)) {
196 198 i++;
197 199 break;
198 200 }
199 201 }
200 202
201 203 for (p2 = strchr(p2, ','); p2++ != NULL; i++)
202 204 p2 = strchr(p2, ',');
203 205
204 206 /*
205 207 * We first allocate a new ident signature structure with the
206 208 * appropriate number of argument entries, and then look up
207 209 * the return type and store its CTF data in di_ctfp/type.
208 210 */
209 211 if ((isp = idp->di_data = malloc(sizeof (dt_idsig_t))) == NULL)
210 212 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
211 213
212 214 isp->dis_varargs = -1;
213 215 isp->dis_optargs = -1;
214 216 isp->dis_argc = i;
215 217 isp->dis_args = NULL;
216 218 isp->dis_auxinfo = 0;
217 219
218 220 if (i != 0 && (isp->dis_args = calloc(i,
219 221 sizeof (dt_node_t))) == NULL) {
220 222 idp->di_data = NULL;
221 223 free(isp);
222 224 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
223 225 }
224 226
225 227 if (dt_type_lookup(s, &dtt) == -1) {
226 228 xyerror(D_UNKNOWN, "failed to resolve type of %s (%s):"
227 229 " %s\n", idp->di_name, s,
228 230 dtrace_errmsg(dtp, dtrace_errno(dtp)));
229 231 }
230 232
231 233 if (idp->di_kind == DT_IDENT_AGGFUNC) {
232 234 idp->di_ctfp = DT_DYN_CTFP(dtp);
233 235 idp->di_type = DT_DYN_TYPE(dtp);
234 236 } else {
235 237 idp->di_ctfp = dtt.dtt_ctfp;
236 238 idp->di_type = dtt.dtt_type;
237 239 }
238 240
239 241 /*
240 242 * For each comma-delimited parameter in the prototype string,
241 243 * we look up the corresponding type and store its CTF data in
242 244 * the corresponding location in dis_args[]. We also recognize
243 245 * the special type string "@" to indicate that the specified
244 246 * parameter may be a D expression of *any* type (represented
245 247 * as a dis_args[] element with ctfp = NULL, type == CTF_ERR).
246 248 * If a varargs "..." is present, we record the argument index
247 249 * in dis_varargs for the benefit of dt_idcook_sign(), above.
248 250 * If the type of an argument is enclosed in square brackets
249 251 * (e.g. "[int]"), the argument is considered optional: the
250 252 * argument may be absent, but if it is present, it must be of
251 253 * the specified type. Note that varargs may not optional,
252 254 * optional arguments may not follow varargs, and non-optional
253 255 * arguments may not follow optional arguments.
254 256 */
255 257 for (i = 0; i < isp->dis_argc; i++, p1 = p2) {
256 258 while (isspace(*p1))
257 259 p1++; /* skip leading whitespace */
258 260
259 261 if ((p2 = strchr(p1, ',')) == NULL)
260 262 p2 = p1 + strlen(p1);
261 263 else
262 264 *p2++ = '\0';
263 265
264 266 if (strcmp(p1, "@") == 0 || strcmp(p1, "...") == 0) {
265 267 isp->dis_args[i].dn_ctfp = NULL;
266 268 isp->dis_args[i].dn_type = CTF_ERR;
267 269 if (*p1 == '.')
268 270 isp->dis_varargs = i;
269 271 continue;
270 272 }
271 273
272 274 if (*p1 == '[' && p1[strlen(p1) - 1] == ']') {
273 275 if (isp->dis_varargs != -1) {
274 276 xyerror(D_UNKNOWN, "optional arg#%d "
275 277 "may not follow variable arg#%d\n",
276 278 i + 1, isp->dis_varargs + 1);
277 279 }
278 280
279 281 if (isp->dis_optargs == -1)
280 282 isp->dis_optargs = i;
281 283
282 284 p1[strlen(p1) - 1] = '\0';
283 285 p1++;
284 286 } else if (isp->dis_optargs != -1) {
285 287 xyerror(D_UNKNOWN, "required arg#%d may not "
286 288 "follow optional arg#%d\n", i + 1,
↓ open down ↓ |
119 lines elided |
↑ open up ↑ |
287 289 isp->dis_optargs + 1);
288 290 }
289 291
290 292 if (dt_type_lookup(p1, &dtt) == -1) {
291 293 xyerror(D_UNKNOWN, "failed to resolve type of "
292 294 "%s arg#%d (%s): %s\n", idp->di_name, i + 1,
293 295 p1, dtrace_errmsg(dtp, dtrace_errno(dtp)));
294 296 }
295 297
296 298 dt_node_type_assign(&isp->dis_args[i],
297 - dtt.dtt_ctfp, dtt.dtt_type);
299 + dtt.dtt_ctfp, dtt.dtt_type, B_FALSE);
298 300 }
299 301 }
300 302
301 303 dt_idcook_sign(dnp, idp, argc, args, "", "( )");
302 304 }
303 305
304 306 /*
305 307 * Cook a reference to the dynamically typed args[] array. We verify that the
306 308 * reference is using a single integer constant, and then construct a new ident
307 309 * representing the appropriate type or translation specifically for this node.
308 310 */
309 311 static void
310 312 dt_idcook_args(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *ap)
311 313 {
312 314 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
313 315 dt_probe_t *prp = yypcb->pcb_probe;
314 316
315 317 dt_node_t tag, *nnp, *xnp;
316 318 dt_xlator_t *dxp;
317 319 dt_ident_t *xidp;
318 320
319 321 char n1[DT_TYPE_NAMELEN];
320 322 char n2[DT_TYPE_NAMELEN];
321 323
322 324 if (argc != 1) {
323 325 xyerror(D_PROTO_LEN, "%s[ ] prototype mismatch: %d arg%s"
324 326 "passed, 1 expected\n", idp->di_name, argc,
325 327 argc == 1 ? " " : "s ");
326 328 }
327 329
328 330 if (ap->dn_kind != DT_NODE_INT) {
329 331 xyerror(D_PROTO_ARG, "%s[ ] argument #1 is incompatible with "
330 332 "prototype:\n\tprototype: %s\n\t argument: %s\n",
331 333 idp->di_name, "integer constant",
332 334 dt_type_name(ap->dn_ctfp, ap->dn_type, n1, sizeof (n1)));
333 335 }
334 336
335 337 if (yypcb->pcb_pdesc == NULL) {
336 338 xyerror(D_ARGS_NONE, "%s[ ] may not be referenced outside "
337 339 "of a probe clause\n", idp->di_name);
338 340 }
339 341
340 342 if (prp == NULL) {
341 343 xyerror(D_ARGS_MULTI,
342 344 "%s[ ] may not be referenced because probe description %s "
343 345 "matches an unstable set of probes\n", idp->di_name,
344 346 dtrace_desc2str(yypcb->pcb_pdesc, n1, sizeof (n1)));
345 347 }
346 348
347 349 if (ap->dn_value >= prp->pr_argc) {
348 350 xyerror(D_ARGS_IDX, "index %lld is out of range for %s %s[ ]\n",
349 351 (longlong_t)ap->dn_value, dtrace_desc2str(yypcb->pcb_pdesc,
350 352 n1, sizeof (n1)), idp->di_name);
351 353 }
352 354
353 355 /*
354 356 * Look up the native and translated argument types for the probe.
355 357 * If no translation is needed, these will be the same underlying node.
356 358 * If translation is needed, look up the appropriate translator. Once
357 359 * we have the appropriate node, create a new dt_ident_t for this node,
358 360 * assign it the appropriate attributes, and set the type of 'dnp'.
359 361 */
360 362 xnp = prp->pr_xargv[ap->dn_value];
361 363 nnp = prp->pr_nargv[prp->pr_mapping[ap->dn_value]];
362 364
363 365 if (xnp->dn_type == CTF_ERR) {
364 366 xyerror(D_ARGS_TYPE, "failed to resolve translated type for "
365 367 "%s[%lld]\n", idp->di_name, (longlong_t)ap->dn_value);
366 368 }
367 369
368 370 if (nnp->dn_type == CTF_ERR) {
369 371 xyerror(D_ARGS_TYPE, "failed to resolve native type for "
370 372 "%s[%lld]\n", idp->di_name, (longlong_t)ap->dn_value);
371 373 }
372 374
373 375 if (dtp->dt_xlatemode == DT_XL_STATIC && (
↓ open down ↓ |
66 lines elided |
↑ open up ↑ |
374 376 nnp == xnp || dt_node_is_argcompat(nnp, xnp))) {
375 377 dnp->dn_ident = dt_ident_create(idp->di_name, idp->di_kind,
376 378 idp->di_flags | DT_IDFLG_ORPHAN, idp->di_id, idp->di_attr,
377 379 idp->di_vers, idp->di_ops, idp->di_iarg, idp->di_gen);
378 380
379 381 if (dnp->dn_ident == NULL)
380 382 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
381 383
382 384 dt_node_type_assign(dnp,
383 385 prp->pr_argv[ap->dn_value].dtt_ctfp,
384 - prp->pr_argv[ap->dn_value].dtt_type);
386 + prp->pr_argv[ap->dn_value].dtt_type,
387 + prp->pr_argv[ap->dn_value].dtt_flags & DTT_FL_USER ?
388 + B_TRUE : B_FALSE);
385 389
386 390 } else if ((dxp = dt_xlator_lookup(dtp,
387 391 nnp, xnp, DT_XLATE_FUZZY)) != NULL || (
388 392 dxp = dt_xlator_lookup(dtp, dt_probe_tag(prp, ap->dn_value, &tag),
389 393 xnp, DT_XLATE_EXACT | DT_XLATE_EXTERN)) != NULL) {
390 394
391 395 xidp = dt_xlator_ident(dxp, xnp->dn_ctfp, xnp->dn_type);
392 396
393 397 dnp->dn_ident = dt_ident_create(idp->di_name, xidp->di_kind,
394 398 xidp->di_flags | DT_IDFLG_ORPHAN, idp->di_id, idp->di_attr,
395 399 idp->di_vers, idp->di_ops, idp->di_iarg, idp->di_gen);
396 400
397 401 if (dnp->dn_ident == NULL)
398 402 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
399 403
400 404 if (dt_xlator_dynamic(dxp))
401 405 dxp->dx_arg = (int)ap->dn_value;
↓ open down ↓ |
7 lines elided |
↑ open up ↑ |
402 406
403 407 /*
404 408 * Propagate relevant members from the translator's internal
405 409 * dt_ident_t. This code must be kept in sync with the state
406 410 * that is initialized for idents in dt_xlator_create().
407 411 */
408 412 dnp->dn_ident->di_data = xidp->di_data;
409 413 dnp->dn_ident->di_ctfp = xidp->di_ctfp;
410 414 dnp->dn_ident->di_type = xidp->di_type;
411 415
412 - dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp));
416 + dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp),
417 + B_FALSE);
413 418
414 419 } else {
415 420 xyerror(D_ARGS_XLATOR, "translator for %s[%lld] from %s to %s "
416 421 "is not defined\n", idp->di_name, (longlong_t)ap->dn_value,
417 422 dt_node_type_name(nnp, n1, sizeof (n1)),
418 423 dt_node_type_name(xnp, n2, sizeof (n2)));
419 424 }
420 425
421 426 assert(dnp->dn_ident->di_flags & DT_IDFLG_ORPHAN);
422 427 assert(dnp->dn_ident->di_id == idp->di_id);
423 428 }
424 429
425 430 static void
426 431 dt_idcook_regs(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *ap)
427 432 {
428 433 dtrace_typeinfo_t dtt;
429 434 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
430 435 char n[DT_TYPE_NAMELEN];
431 436
432 437 if (argc != 1) {
433 438 xyerror(D_PROTO_LEN, "%s[ ] prototype mismatch: %d arg%s"
434 439 "passed, 1 expected\n", idp->di_name,
435 440 argc, argc == 1 ? " " : "s ");
436 441 }
437 442
438 443 if (ap->dn_kind != DT_NODE_INT) {
439 444 xyerror(D_PROTO_ARG, "%s[ ] argument #1 is incompatible with "
440 445 "prototype:\n\tprototype: %s\n\t argument: %s\n",
441 446 idp->di_name, "integer constant",
442 447 dt_type_name(ap->dn_ctfp, ap->dn_type, n, sizeof (n)));
443 448 }
444 449
445 450 if ((ap->dn_flags & DT_NF_SIGNED) && (int64_t)ap->dn_value < 0) {
446 451 xyerror(D_REGS_IDX, "index %lld is out of range for array %s\n",
447 452 (longlong_t)ap->dn_value, idp->di_name);
↓ open down ↓ |
25 lines elided |
↑ open up ↑ |
448 453 }
449 454
450 455 if (dt_type_lookup("uint64_t", &dtt) == -1) {
451 456 xyerror(D_UNKNOWN, "failed to resolve type of %s: %s\n",
452 457 idp->di_name, dtrace_errmsg(dtp, dtrace_errno(dtp)));
453 458 }
454 459
455 460 idp->di_ctfp = dtt.dtt_ctfp;
456 461 idp->di_type = dtt.dtt_type;
457 462
458 - dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type);
463 + dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type, B_FALSE);
459 464 }
460 465
461 466 /*ARGSUSED*/
462 467 static void
463 468 dt_idcook_type(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args)
464 469 {
465 470 if (idp->di_type == CTF_ERR) {
466 471 dtrace_hdl_t *dtp = yypcb->pcb_hdl;
467 472 dtrace_typeinfo_t dtt;
468 473
469 474 if (dt_type_lookup(idp->di_iarg, &dtt) == -1) {
↓ open down ↓ |
1 lines elided |
↑ open up ↑ |
470 475 xyerror(D_UNKNOWN,
471 476 "failed to resolve type %s for identifier %s: %s\n",
472 477 (const char *)idp->di_iarg, idp->di_name,
473 478 dtrace_errmsg(dtp, dtrace_errno(dtp)));
474 479 }
475 480
476 481 idp->di_ctfp = dtt.dtt_ctfp;
477 482 idp->di_type = dtt.dtt_type;
478 483 }
479 484
480 - dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type);
485 + dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type, B_FALSE);
481 486 }
482 487
483 488 /*ARGSUSED*/
484 489 static void
485 490 dt_idcook_thaw(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args)
486 491 {
487 492 if (idp->di_ctfp != NULL && idp->di_type != CTF_ERR)
488 - dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type);
493 + dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type, B_FALSE);
489 494 }
490 495
491 496 static void
492 497 dt_idcook_inline(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args)
493 498 {
494 499 if (idp->di_kind == DT_IDENT_ARRAY)
495 500 dt_idcook_assc(dnp, idp, argc, args);
496 501 else
497 502 dt_idcook_thaw(dnp, idp, argc, args);
498 503 }
499 504
500 505 static void
501 506 dt_iddtor_sign(dt_ident_t *idp)
502 507 {
503 508 if (idp->di_data != NULL)
504 509 free(((dt_idsig_t *)idp->di_data)->dis_args);
505 510 free(idp->di_data);
506 511 }
507 512
508 513 static void
509 514 dt_iddtor_free(dt_ident_t *idp)
510 515 {
511 516 free(idp->di_data);
512 517 }
513 518
514 519 static void
515 520 dt_iddtor_inline(dt_ident_t *idp)
516 521 {
517 522 dt_idnode_t *inp = idp->di_iarg;
518 523
519 524 if (inp != NULL) {
520 525 dt_node_link_free(&inp->din_list);
521 526
522 527 if (inp->din_hash != NULL)
523 528 dt_idhash_destroy(inp->din_hash);
524 529
525 530 free(inp->din_argv);
526 531 free(inp);
527 532 }
528 533
529 534 if (idp->di_kind == DT_IDENT_ARRAY)
530 535 dt_iddtor_sign(idp);
531 536 else
532 537 dt_iddtor_free(idp);
533 538 }
534 539
535 540 /*ARGSUSED*/
536 541 static void
537 542 dt_iddtor_none(dt_ident_t *idp)
538 543 {
539 544 /* do nothing */
540 545 }
541 546
542 547 static void
543 548 dt_iddtor_probe(dt_ident_t *idp)
544 549 {
545 550 if (idp->di_data != NULL)
546 551 dt_probe_destroy(idp->di_data);
547 552 }
548 553
549 554 static size_t
550 555 dt_idsize_type(dt_ident_t *idp)
551 556 {
552 557 return (ctf_type_size(idp->di_ctfp, idp->di_type));
553 558 }
554 559
555 560 /*ARGSUSED*/
556 561 static size_t
557 562 dt_idsize_none(dt_ident_t *idp)
558 563 {
559 564 return (0);
560 565 }
561 566
562 567 const dt_idops_t dt_idops_assc = {
563 568 dt_idcook_assc,
564 569 dt_iddtor_sign,
565 570 dt_idsize_none,
566 571 };
567 572
568 573 const dt_idops_t dt_idops_func = {
569 574 dt_idcook_func,
570 575 dt_iddtor_sign,
571 576 dt_idsize_none,
572 577 };
573 578
574 579 const dt_idops_t dt_idops_args = {
575 580 dt_idcook_args,
576 581 dt_iddtor_none,
577 582 dt_idsize_none,
578 583 };
579 584
580 585 const dt_idops_t dt_idops_regs = {
581 586 dt_idcook_regs,
582 587 dt_iddtor_free,
583 588 dt_idsize_none,
584 589 };
585 590
586 591 const dt_idops_t dt_idops_type = {
587 592 dt_idcook_type,
588 593 dt_iddtor_free,
589 594 dt_idsize_type,
590 595 };
591 596
592 597 const dt_idops_t dt_idops_thaw = {
593 598 dt_idcook_thaw,
594 599 dt_iddtor_free,
595 600 dt_idsize_type,
596 601 };
597 602
598 603 const dt_idops_t dt_idops_inline = {
599 604 dt_idcook_inline,
600 605 dt_iddtor_inline,
601 606 dt_idsize_type,
602 607 };
603 608
604 609 const dt_idops_t dt_idops_probe = {
605 610 dt_idcook_thaw,
606 611 dt_iddtor_probe,
607 612 dt_idsize_none,
608 613 };
609 614
610 615 static void
611 616 dt_idhash_populate(dt_idhash_t *dhp)
612 617 {
613 618 const dt_ident_t *idp = dhp->dh_tmpl;
614 619
615 620 dhp->dh_tmpl = NULL; /* clear dh_tmpl first to avoid recursion */
616 621 dt_dprintf("populating %s idhash from %p\n", dhp->dh_name, (void *)idp);
617 622
618 623 for (; idp->di_name != NULL; idp++) {
619 624 if (dt_idhash_insert(dhp, idp->di_name,
620 625 idp->di_kind, idp->di_flags, idp->di_id, idp->di_attr,
621 626 idp->di_vers, idp->di_ops ? idp->di_ops : &dt_idops_thaw,
622 627 idp->di_iarg, 0) == NULL)
623 628 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
624 629 }
625 630 }
626 631
627 632 dt_idhash_t *
628 633 dt_idhash_create(const char *name, const dt_ident_t *tmpl,
629 634 uint_t min, uint_t max)
630 635 {
631 636 dt_idhash_t *dhp;
632 637 size_t size;
633 638
634 639 assert(min <= max);
635 640
636 641 size = sizeof (dt_idhash_t) +
637 642 sizeof (dt_ident_t *) * (_dtrace_strbuckets - 1);
638 643
639 644 if ((dhp = malloc(size)) == NULL)
640 645 return (NULL);
641 646
642 647 bzero(dhp, size);
643 648 dhp->dh_name = name;
644 649 dhp->dh_tmpl = tmpl;
645 650 dhp->dh_nextid = min;
646 651 dhp->dh_minid = min;
647 652 dhp->dh_maxid = max;
648 653 dhp->dh_hashsz = _dtrace_strbuckets;
649 654
650 655 return (dhp);
651 656 }
652 657
653 658 /*
654 659 * Destroy an entire identifier hash. This must be done using two passes with
655 660 * an inlined version of dt_ident_destroy() to avoid referencing freed memory.
656 661 * In the first pass di_dtor() is called for all identifiers; then the second
657 662 * pass frees the actual dt_ident_t's. These must be done separately because
658 663 * a di_dtor() may operate on data structures which contain references to other
659 664 * identifiers inside of this hash itself (e.g. a global inline definition
660 665 * which contains a parse tree that refers to another global variable).
661 666 */
662 667 void
663 668 dt_idhash_destroy(dt_idhash_t *dhp)
664 669 {
665 670 dt_ident_t *idp, *next;
666 671 ulong_t i;
667 672
668 673 for (i = 0; i < dhp->dh_hashsz; i++) {
669 674 for (idp = dhp->dh_hash[i]; idp != NULL; idp = next) {
670 675 next = idp->di_next;
671 676 idp->di_ops->di_dtor(idp);
672 677 }
673 678 }
674 679
675 680 for (i = 0; i < dhp->dh_hashsz; i++) {
676 681 for (idp = dhp->dh_hash[i]; idp != NULL; idp = next) {
677 682 next = idp->di_next;
678 683 free(idp->di_name);
679 684 free(idp);
680 685 }
681 686 }
682 687
683 688 free(dhp);
684 689 }
685 690
686 691 void
687 692 dt_idhash_update(dt_idhash_t *dhp)
688 693 {
689 694 uint_t nextid = dhp->dh_minid;
690 695 dt_ident_t *idp;
691 696 ulong_t i;
692 697
693 698 for (i = 0; i < dhp->dh_hashsz; i++) {
694 699 for (idp = dhp->dh_hash[i]; idp != NULL; idp = idp->di_next) {
695 700 /*
696 701 * Right now we're hard coding which types need to be
697 702 * reset, but ideally this would be done dynamically.
698 703 */
699 704 if (idp->di_kind == DT_IDENT_ARRAY ||
700 705 idp->di_kind == DT_IDENT_SCALAR ||
701 706 idp->di_kind == DT_IDENT_AGG)
702 707 nextid = MAX(nextid, idp->di_id + 1);
703 708 }
704 709 }
705 710
706 711 dhp->dh_nextid = nextid;
707 712 }
708 713
709 714 dt_ident_t *
710 715 dt_idhash_lookup(dt_idhash_t *dhp, const char *name)
711 716 {
712 717 size_t len;
713 718 ulong_t h = dt_strtab_hash(name, &len) % dhp->dh_hashsz;
714 719 dt_ident_t *idp;
715 720
716 721 if (dhp->dh_tmpl != NULL)
717 722 dt_idhash_populate(dhp); /* fill hash w/ initial population */
718 723
719 724 for (idp = dhp->dh_hash[h]; idp != NULL; idp = idp->di_next) {
720 725 if (strcmp(idp->di_name, name) == 0)
721 726 return (idp);
722 727 }
723 728
724 729 return (NULL);
725 730 }
726 731
727 732 int
728 733 dt_idhash_nextid(dt_idhash_t *dhp, uint_t *p)
729 734 {
730 735 if (dhp->dh_nextid >= dhp->dh_maxid)
731 736 return (-1); /* no more id's are free to allocate */
732 737
733 738 *p = dhp->dh_nextid++;
734 739 return (0);
735 740 }
736 741
737 742 ulong_t
738 743 dt_idhash_size(const dt_idhash_t *dhp)
739 744 {
740 745 return (dhp->dh_nelems);
741 746 }
742 747
743 748 const char *
744 749 dt_idhash_name(const dt_idhash_t *dhp)
745 750 {
746 751 return (dhp->dh_name);
747 752 }
748 753
749 754 dt_ident_t *
750 755 dt_idhash_insert(dt_idhash_t *dhp, const char *name, ushort_t kind,
751 756 ushort_t flags, uint_t id, dtrace_attribute_t attr, uint_t vers,
752 757 const dt_idops_t *ops, void *iarg, ulong_t gen)
753 758 {
754 759 dt_ident_t *idp;
755 760 ulong_t h;
756 761
757 762 if (dhp->dh_tmpl != NULL)
758 763 dt_idhash_populate(dhp); /* fill hash w/ initial population */
759 764
760 765 idp = dt_ident_create(name, kind, flags, id,
761 766 attr, vers, ops, iarg, gen);
762 767
763 768 if (idp == NULL)
764 769 return (NULL);
765 770
766 771 h = dt_strtab_hash(name, NULL) % dhp->dh_hashsz;
767 772 idp->di_next = dhp->dh_hash[h];
768 773
769 774 dhp->dh_hash[h] = idp;
770 775 dhp->dh_nelems++;
771 776
772 777 if (dhp->dh_defer != NULL)
773 778 dhp->dh_defer(dhp, idp);
774 779
775 780 return (idp);
776 781 }
777 782
778 783 void
779 784 dt_idhash_xinsert(dt_idhash_t *dhp, dt_ident_t *idp)
780 785 {
781 786 ulong_t h;
782 787
783 788 if (dhp->dh_tmpl != NULL)
784 789 dt_idhash_populate(dhp); /* fill hash w/ initial population */
785 790
786 791 h = dt_strtab_hash(idp->di_name, NULL) % dhp->dh_hashsz;
787 792 idp->di_next = dhp->dh_hash[h];
788 793 idp->di_flags &= ~DT_IDFLG_ORPHAN;
789 794
790 795 dhp->dh_hash[h] = idp;
791 796 dhp->dh_nelems++;
792 797
793 798 if (dhp->dh_defer != NULL)
794 799 dhp->dh_defer(dhp, idp);
795 800 }
796 801
797 802 void
798 803 dt_idhash_delete(dt_idhash_t *dhp, dt_ident_t *key)
799 804 {
800 805 size_t len;
801 806 ulong_t h = dt_strtab_hash(key->di_name, &len) % dhp->dh_hashsz;
802 807 dt_ident_t **pp = &dhp->dh_hash[h];
803 808 dt_ident_t *idp;
804 809
805 810 for (idp = dhp->dh_hash[h]; idp != NULL; idp = idp->di_next) {
806 811 if (idp == key)
807 812 break;
808 813 else
809 814 pp = &idp->di_next;
810 815 }
811 816
812 817 assert(idp == key);
813 818 *pp = idp->di_next;
814 819
815 820 assert(dhp->dh_nelems != 0);
816 821 dhp->dh_nelems--;
817 822
818 823 if (!(idp->di_flags & DT_IDFLG_ORPHAN))
819 824 dt_ident_destroy(idp);
820 825 }
821 826
822 827 static int
823 828 dt_idhash_comp(const void *lp, const void *rp)
824 829 {
825 830 const dt_ident_t *lhs = *((const dt_ident_t **)lp);
826 831 const dt_ident_t *rhs = *((const dt_ident_t **)rp);
827 832
828 833 if (lhs->di_id != rhs->di_id)
829 834 return ((int)(lhs->di_id - rhs->di_id));
830 835 else
831 836 return (strcmp(lhs->di_name, rhs->di_name));
832 837 }
833 838
834 839 int
835 840 dt_idhash_iter(dt_idhash_t *dhp, dt_idhash_f *func, void *data)
836 841 {
837 842 dt_ident_t **ids;
838 843 dt_ident_t *idp;
839 844 ulong_t i, j, n;
840 845 int rv;
841 846
842 847 if (dhp->dh_tmpl != NULL)
843 848 dt_idhash_populate(dhp); /* fill hash w/ initial population */
844 849
845 850 n = dhp->dh_nelems;
846 851 ids = alloca(sizeof (dt_ident_t *) * n);
847 852
848 853 for (i = 0, j = 0; i < dhp->dh_hashsz; i++) {
849 854 for (idp = dhp->dh_hash[i]; idp != NULL; idp = idp->di_next)
850 855 ids[j++] = idp;
851 856 }
852 857
853 858 qsort(ids, dhp->dh_nelems, sizeof (dt_ident_t *), dt_idhash_comp);
854 859
855 860 for (i = 0; i < n; i++) {
856 861 if ((rv = func(dhp, ids[i], data)) != 0)
857 862 return (rv);
858 863 }
859 864
860 865 return (0);
861 866 }
862 867
863 868 dt_ident_t *
864 869 dt_idstack_lookup(dt_idstack_t *sp, const char *name)
865 870 {
866 871 dt_idhash_t *dhp;
867 872 dt_ident_t *idp;
868 873
869 874 for (dhp = dt_list_prev(&sp->dids_list);
870 875 dhp != NULL; dhp = dt_list_prev(dhp)) {
871 876 if ((idp = dt_idhash_lookup(dhp, name)) != NULL)
872 877 return (idp);
873 878 }
874 879
875 880 return (NULL);
876 881 }
877 882
878 883 void
879 884 dt_idstack_push(dt_idstack_t *sp, dt_idhash_t *dhp)
880 885 {
881 886 dt_list_append(&sp->dids_list, dhp);
882 887 }
883 888
884 889 void
885 890 dt_idstack_pop(dt_idstack_t *sp, dt_idhash_t *dhp)
886 891 {
887 892 assert(dt_list_prev(&sp->dids_list) == dhp);
888 893 dt_list_delete(&sp->dids_list, dhp);
889 894 }
890 895
891 896 dt_ident_t *
892 897 dt_ident_create(const char *name, ushort_t kind, ushort_t flags, uint_t id,
893 898 dtrace_attribute_t attr, uint_t vers,
894 899 const dt_idops_t *ops, void *iarg, ulong_t gen)
895 900 {
896 901 dt_ident_t *idp;
897 902 char *s = NULL;
898 903
899 904 if ((name != NULL && (s = strdup(name)) == NULL) ||
900 905 (idp = malloc(sizeof (dt_ident_t))) == NULL) {
901 906 free(s);
902 907 return (NULL);
903 908 }
904 909
905 910 idp->di_name = s;
906 911 idp->di_kind = kind;
907 912 idp->di_flags = flags;
908 913 idp->di_id = id;
909 914 idp->di_attr = attr;
910 915 idp->di_vers = vers;
911 916 idp->di_ops = ops;
912 917 idp->di_iarg = iarg;
913 918 idp->di_data = NULL;
914 919 idp->di_ctfp = NULL;
915 920 idp->di_type = CTF_ERR;
916 921 idp->di_next = NULL;
917 922 idp->di_gen = gen;
918 923 idp->di_lineno = yylineno;
919 924
920 925 return (idp);
921 926 }
922 927
923 928 /*
924 929 * Destroy an individual identifier. This code must be kept in sync with the
925 930 * dt_idhash_destroy() function below, which separates out the call to di_dtor.
926 931 */
927 932 void
928 933 dt_ident_destroy(dt_ident_t *idp)
929 934 {
930 935 idp->di_ops->di_dtor(idp);
931 936 free(idp->di_name);
932 937 free(idp);
933 938 }
934 939
935 940 void
936 941 dt_ident_morph(dt_ident_t *idp, ushort_t kind,
937 942 const dt_idops_t *ops, void *iarg)
938 943 {
939 944 idp->di_ops->di_dtor(idp);
940 945 idp->di_kind = kind;
941 946 idp->di_ops = ops;
942 947 idp->di_iarg = iarg;
943 948 idp->di_data = NULL;
944 949 }
945 950
946 951 dtrace_attribute_t
947 952 dt_ident_cook(dt_node_t *dnp, dt_ident_t *idp, dt_node_t **pargp)
948 953 {
949 954 dtrace_attribute_t attr;
950 955 dt_node_t *args, *argp;
951 956 int argc = 0;
952 957
953 958 attr = dt_node_list_cook(pargp, DT_IDFLG_REF);
954 959 args = pargp ? *pargp : NULL;
955 960
956 961 for (argp = args; argp != NULL; argp = argp->dn_list)
957 962 argc++;
958 963
959 964 idp->di_ops->di_cook(dnp, idp, argc, args);
960 965
961 966 if (idp->di_flags & DT_IDFLG_USER)
962 967 dnp->dn_flags |= DT_NF_USERLAND;
963 968
964 969 return (dt_attr_min(attr, idp->di_attr));
965 970 }
966 971
967 972 void
968 973 dt_ident_type_assign(dt_ident_t *idp, ctf_file_t *fp, ctf_id_t type)
969 974 {
970 975 idp->di_ctfp = fp;
971 976 idp->di_type = type;
972 977 }
973 978
974 979 dt_ident_t *
975 980 dt_ident_resolve(dt_ident_t *idp)
976 981 {
977 982 while (idp->di_flags & DT_IDFLG_INLINE) {
978 983 const dt_node_t *dnp = ((dt_idnode_t *)idp->di_iarg)->din_root;
979 984
980 985 if (dnp == NULL)
981 986 break; /* can't resolve any further yet */
982 987
983 988 switch (dnp->dn_kind) {
984 989 case DT_NODE_VAR:
985 990 case DT_NODE_SYM:
986 991 case DT_NODE_FUNC:
987 992 case DT_NODE_AGG:
988 993 case DT_NODE_INLINE:
989 994 case DT_NODE_PROBE:
990 995 idp = dnp->dn_ident;
991 996 continue;
992 997 }
993 998
994 999 if (dt_node_is_dynamic(dnp))
995 1000 idp = dnp->dn_ident;
996 1001 else
997 1002 break;
998 1003 }
999 1004
1000 1005 return (idp);
1001 1006 }
1002 1007
1003 1008 size_t
1004 1009 dt_ident_size(dt_ident_t *idp)
1005 1010 {
1006 1011 idp = dt_ident_resolve(idp);
1007 1012 return (idp->di_ops->di_size(idp));
1008 1013 }
1009 1014
1010 1015 int
1011 1016 dt_ident_unref(const dt_ident_t *idp)
1012 1017 {
1013 1018 return (idp->di_gen == yypcb->pcb_hdl->dt_gen &&
1014 1019 (idp->di_flags & (DT_IDFLG_REF|DT_IDFLG_MOD|DT_IDFLG_DECL)) == 0);
1015 1020 }
1016 1021
1017 1022 const char *
1018 1023 dt_idkind_name(uint_t kind)
1019 1024 {
1020 1025 switch (kind) {
1021 1026 case DT_IDENT_ARRAY: return ("associative array");
1022 1027 case DT_IDENT_SCALAR: return ("scalar");
1023 1028 case DT_IDENT_PTR: return ("pointer");
1024 1029 case DT_IDENT_FUNC: return ("function");
1025 1030 case DT_IDENT_AGG: return ("aggregation");
1026 1031 case DT_IDENT_AGGFUNC: return ("aggregating function");
1027 1032 case DT_IDENT_ACTFUNC: return ("tracing function");
1028 1033 case DT_IDENT_XLSOU: return ("translated data");
1029 1034 case DT_IDENT_XLPTR: return ("pointer to translated data");
1030 1035 case DT_IDENT_SYMBOL: return ("external symbol reference");
1031 1036 case DT_IDENT_ENUM: return ("enumerator");
1032 1037 case DT_IDENT_PRAGAT: return ("#pragma attributes");
1033 1038 case DT_IDENT_PRAGBN: return ("#pragma binding");
1034 1039 case DT_IDENT_PROBE: return ("probe definition");
1035 1040 default: return ("<?>");
1036 1041 }
1037 1042 }
↓ open down ↓ |
539 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX