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_as.c
+++ new/usr/src/lib/libdtrace/common/dt_as.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, Version 1.0 only
6 6 * (the "License"). You may not use this file except in compliance
7 7 * with the License.
8 8 *
9 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 10 * or http://www.opensolaris.org/os/licensing.
11 11 * See the License for the specific language governing permissions
12 12 * and limitations under the License.
13 13 *
14 14 * When distributing Covered Code, include this CDDL HEADER in each
15 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
16 16 * If applicable, add the following below this CDDL HEADER, with the
17 17 * fields enclosed by brackets "[]" replaced with your own identifying
18 18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 19 *
20 20 * CDDL HEADER END
21 21 */
22 22 /*
23 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 -
27 -#pragma ident "%Z%%M% %I% %E% SMI"
26 +/*
27 + * Copyright (c) 2013 by Delphix. All rights reserved.
28 + * Copyright (c) 2013 Joyent, Inc. All rights reserved.
29 + */
28 30
29 31 #include <sys/types.h>
30 32 #include <strings.h>
31 33 #include <stdlib.h>
32 34 #include <assert.h>
33 35
34 36 #include <dt_impl.h>
35 37 #include <dt_parser.h>
36 38 #include <dt_as.h>
37 39
38 40 void
39 41 dt_irlist_create(dt_irlist_t *dlp)
40 42 {
41 43 bzero(dlp, sizeof (dt_irlist_t));
42 44 dlp->dl_label = 1;
43 45 }
44 46
45 47 void
46 48 dt_irlist_destroy(dt_irlist_t *dlp)
47 49 {
48 50 dt_irnode_t *dip, *nip;
49 51
50 52 for (dip = dlp->dl_list; dip != NULL; dip = nip) {
51 53 nip = dip->di_next;
52 54 free(dip);
53 55 }
54 56 }
55 57
56 58 void
57 59 dt_irlist_append(dt_irlist_t *dlp, dt_irnode_t *dip)
58 60 {
59 61 if (dlp->dl_last != NULL)
60 62 dlp->dl_last->di_next = dip;
61 63 else
62 64 dlp->dl_list = dip;
63 65
64 66 dlp->dl_last = dip;
65 67
66 68 if (dip->di_label == DT_LBL_NONE || dip->di_instr != DIF_INSTR_NOP)
67 69 dlp->dl_len++; /* don't count forward refs in instr count */
68 70 }
69 71
70 72 uint_t
71 73 dt_irlist_label(dt_irlist_t *dlp)
72 74 {
73 75 return (dlp->dl_label++);
74 76 }
75 77
76 78 /*ARGSUSED*/
77 79 static int
78 80 dt_countvar(dt_idhash_t *dhp, dt_ident_t *idp, void *data)
79 81 {
80 82 size_t *np = data;
81 83
82 84 if (idp->di_flags & (DT_IDFLG_DIFR | DT_IDFLG_DIFW))
83 85 (*np)++; /* include variable in vartab */
84 86
85 87 return (0);
86 88 }
87 89
88 90 /*ARGSUSED*/
89 91 static int
90 92 dt_copyvar(dt_idhash_t *dhp, dt_ident_t *idp, void *data)
91 93 {
92 94 dt_pcb_t *pcb = data;
93 95 dtrace_difv_t *dvp;
94 96 ssize_t stroff;
95 97 dt_node_t dn;
96 98
97 99 if (!(idp->di_flags & (DT_IDFLG_DIFR | DT_IDFLG_DIFW)))
98 100 return (0); /* omit variable from vartab */
99 101
100 102 dvp = &pcb->pcb_difo->dtdo_vartab[pcb->pcb_asvidx++];
101 103 stroff = dt_strtab_insert(pcb->pcb_strtab, idp->di_name);
102 104
103 105 if (stroff == -1L)
104 106 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
105 107 if (stroff > DIF_STROFF_MAX)
106 108 longjmp(pcb->pcb_jmpbuf, EDT_STR2BIG);
107 109
108 110 dvp->dtdv_name = (uint_t)stroff;
109 111 dvp->dtdv_id = idp->di_id;
110 112 dvp->dtdv_flags = 0;
111 113
112 114 dvp->dtdv_kind = (idp->di_kind == DT_IDENT_ARRAY) ?
113 115 DIFV_KIND_ARRAY : DIFV_KIND_SCALAR;
114 116
115 117 if (idp->di_flags & DT_IDFLG_LOCAL)
116 118 dvp->dtdv_scope = DIFV_SCOPE_LOCAL;
117 119 else if (idp->di_flags & DT_IDFLG_TLS)
↓ open down ↓ |
80 lines elided |
↑ open up ↑ |
118 120 dvp->dtdv_scope = DIFV_SCOPE_THREAD;
119 121 else
120 122 dvp->dtdv_scope = DIFV_SCOPE_GLOBAL;
121 123
122 124 if (idp->di_flags & DT_IDFLG_DIFR)
123 125 dvp->dtdv_flags |= DIFV_F_REF;
124 126 if (idp->di_flags & DT_IDFLG_DIFW)
125 127 dvp->dtdv_flags |= DIFV_F_MOD;
126 128
127 129 bzero(&dn, sizeof (dn));
128 - dt_node_type_assign(&dn, idp->di_ctfp, idp->di_type);
130 + dt_node_type_assign(&dn, idp->di_ctfp, idp->di_type, B_FALSE);
129 131 dt_node_diftype(pcb->pcb_hdl, &dn, &dvp->dtdv_type);
130 132
131 133 idp->di_flags &= ~(DT_IDFLG_DIFR | DT_IDFLG_DIFW);
132 134 return (0);
133 135 }
134 136
135 137 static ssize_t
136 138 dt_copystr(const char *s, size_t n, size_t off, dt_pcb_t *pcb)
137 139 {
138 140 bcopy(s, pcb->pcb_difo->dtdo_strtab + off, n);
139 141 return (n);
140 142 }
141 143
142 144 /*
143 145 * Rewrite the xlate/xlarg instruction at dtdo_buf[i] so that the instruction's
144 146 * xltab index reflects the offset 'xi' of the assigned dtdo_xlmtab[] location.
145 147 * We track the cumulative references to translators and members in the pcb's
146 148 * pcb_asxrefs[] array, a two-dimensional array of bitmaps indexed by the
147 149 * global translator id and then by the corresponding translator member id.
148 150 */
149 151 static void
150 152 dt_as_xlate(dt_pcb_t *pcb, dtrace_difo_t *dp,
151 153 uint_t i, uint_t xi, dt_node_t *dnp)
152 154 {
153 155 dtrace_hdl_t *dtp = pcb->pcb_hdl;
154 156 dt_xlator_t *dxp = dnp->dn_membexpr->dn_xlator;
155 157
156 158 assert(i < dp->dtdo_len);
157 159 assert(xi < dp->dtdo_xlmlen);
158 160
159 161 assert(dnp->dn_kind == DT_NODE_MEMBER);
160 162 assert(dnp->dn_membexpr->dn_kind == DT_NODE_XLATOR);
161 163
162 164 assert(dxp->dx_id < dtp->dt_xlatorid);
163 165 assert(dnp->dn_membid < dxp->dx_nmembers);
164 166
165 167 if (pcb->pcb_asxrefs == NULL) {
166 168 pcb->pcb_asxreflen = dtp->dt_xlatorid;
167 169 pcb->pcb_asxrefs =
168 170 dt_zalloc(dtp, sizeof (ulong_t *) * pcb->pcb_asxreflen);
169 171 if (pcb->pcb_asxrefs == NULL)
170 172 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
171 173 }
172 174
173 175 if (pcb->pcb_asxrefs[dxp->dx_id] == NULL) {
174 176 pcb->pcb_asxrefs[dxp->dx_id] =
175 177 dt_zalloc(dtp, BT_SIZEOFMAP(dxp->dx_nmembers));
176 178 if (pcb->pcb_asxrefs[dxp->dx_id] == NULL)
177 179 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
178 180 }
179 181
180 182 dp->dtdo_buf[i] = DIF_INSTR_XLATE(
181 183 DIF_INSTR_OP(dp->dtdo_buf[i]), xi, DIF_INSTR_RD(dp->dtdo_buf[i]));
182 184
183 185 BT_SET(pcb->pcb_asxrefs[dxp->dx_id], dnp->dn_membid);
184 186 dp->dtdo_xlmtab[xi] = dnp;
185 187 }
186 188
187 189 static void
188 190 dt_as_undef(const dt_ident_t *idp, uint_t offset)
189 191 {
190 192 const char *kind, *mark = (idp->di_flags & DT_IDFLG_USER) ? "``" : "`";
191 193 const dtrace_syminfo_t *dts = idp->di_data;
192 194
193 195 if (idp->di_flags & DT_IDFLG_USER)
194 196 kind = "user";
195 197 else if (idp->di_flags & DT_IDFLG_PRIM)
196 198 kind = "primary kernel";
197 199 else
198 200 kind = "loadable kernel";
199 201
200 202 yylineno = idp->di_lineno;
201 203
202 204 xyerror(D_ASRELO, "relocation remains against %s symbol %s%s%s (offset "
203 205 "0x%x)\n", kind, dts->dts_object, mark, dts->dts_name, offset);
204 206 }
205 207
206 208 dtrace_difo_t *
207 209 dt_as(dt_pcb_t *pcb)
208 210 {
209 211 dtrace_hdl_t *dtp = pcb->pcb_hdl;
210 212 dt_irlist_t *dlp = &pcb->pcb_ir;
211 213 uint_t *labels = NULL;
212 214 dt_irnode_t *dip;
213 215 dtrace_difo_t *dp;
214 216 dt_ident_t *idp;
215 217
216 218 size_t n = 0;
217 219 uint_t i;
218 220
219 221 uint_t kmask, kbits, umask, ubits;
220 222 uint_t krel = 0, urel = 0, xlrefs = 0;
221 223
222 224 /*
223 225 * Select bitmasks based upon the desired symbol linking policy. We
224 226 * test (di_extern->di_flags & xmask) == xbits to determine if the
225 227 * symbol should have a relocation entry generated in the loop below.
226 228 *
227 229 * DT_LINK_KERNEL = kernel symbols static, user symbols dynamic
228 230 * DT_LINK_PRIMARY = primary kernel symbols static, others dynamic
229 231 * DT_LINK_DYNAMIC = all symbols dynamic
230 232 * DT_LINK_STATIC = all symbols static
231 233 *
232 234 * By 'static' we mean that we use the symbol's value at compile-time
233 235 * in the final DIF. By 'dynamic' we mean that we create a relocation
234 236 * table entry for the symbol's value so it can be relocated later.
235 237 */
236 238 switch (dtp->dt_linkmode) {
237 239 case DT_LINK_KERNEL:
238 240 kmask = 0;
239 241 kbits = -1u;
240 242 umask = DT_IDFLG_USER;
241 243 ubits = DT_IDFLG_USER;
242 244 break;
243 245 case DT_LINK_PRIMARY:
244 246 kmask = DT_IDFLG_USER | DT_IDFLG_PRIM;
245 247 kbits = 0;
246 248 umask = DT_IDFLG_USER;
247 249 ubits = DT_IDFLG_USER;
248 250 break;
249 251 case DT_LINK_DYNAMIC:
250 252 kmask = DT_IDFLG_USER;
251 253 kbits = 0;
252 254 umask = DT_IDFLG_USER;
253 255 ubits = DT_IDFLG_USER;
254 256 break;
255 257 case DT_LINK_STATIC:
256 258 kmask = umask = 0;
257 259 kbits = ubits = -1u;
258 260 break;
259 261 default:
260 262 xyerror(D_UNKNOWN, "internal error -- invalid link mode %u\n",
261 263 dtp->dt_linkmode);
262 264 }
263 265
264 266 assert(pcb->pcb_difo == NULL);
265 267 pcb->pcb_difo = dt_zalloc(dtp, sizeof (dtrace_difo_t));
266 268
267 269 if ((dp = pcb->pcb_difo) == NULL)
268 270 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
269 271
270 272 dp->dtdo_buf = dt_alloc(dtp, sizeof (dif_instr_t) * dlp->dl_len);
271 273
272 274 if (dp->dtdo_buf == NULL)
273 275 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
274 276
275 277 if ((labels = dt_alloc(dtp, sizeof (uint_t) * dlp->dl_label)) == NULL)
276 278 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
277 279
278 280 /*
279 281 * Make an initial pass through the instruction list, filling in the
280 282 * instruction buffer with valid instructions and skipping labeled nops.
281 283 * While doing this, we also fill in our labels[] translation table
282 284 * and we count up the number of relocation table entries we will need.
283 285 */
284 286 for (i = 0, dip = dlp->dl_list; dip != NULL; dip = dip->di_next) {
285 287 if (dip->di_label != DT_LBL_NONE)
286 288 labels[dip->di_label] = i;
287 289
288 290 if (dip->di_label == DT_LBL_NONE ||
289 291 dip->di_instr != DIF_INSTR_NOP)
290 292 dp->dtdo_buf[i++] = dip->di_instr;
291 293
292 294 if (dip->di_extern == NULL)
293 295 continue; /* no external references needed */
294 296
295 297 switch (DIF_INSTR_OP(dip->di_instr)) {
296 298 case DIF_OP_SETX:
297 299 idp = dip->di_extern;
298 300 if ((idp->di_flags & kmask) == kbits)
299 301 krel++;
300 302 else if ((idp->di_flags & umask) == ubits)
301 303 urel++;
302 304 break;
303 305 case DIF_OP_XLATE:
304 306 case DIF_OP_XLARG:
305 307 xlrefs++;
306 308 break;
307 309 default:
308 310 xyerror(D_UNKNOWN, "unexpected assembler relocation "
309 311 "for opcode 0x%x\n", DIF_INSTR_OP(dip->di_instr));
310 312 }
311 313 }
312 314
313 315 assert(i == dlp->dl_len);
314 316 dp->dtdo_len = dlp->dl_len;
315 317
316 318 /*
317 319 * Make a second pass through the instructions, relocating each branch
318 320 * label to the index of the final instruction in the buffer and noting
319 321 * any other instruction-specific DIFO flags such as dtdo_destructive.
320 322 */
321 323 for (i = 0; i < dp->dtdo_len; i++) {
322 324 dif_instr_t instr = dp->dtdo_buf[i];
323 325 uint_t op = DIF_INSTR_OP(instr);
324 326
325 327 if (op == DIF_OP_CALL) {
326 328 if (DIF_INSTR_SUBR(instr) == DIF_SUBR_COPYOUT ||
327 329 DIF_INSTR_SUBR(instr) == DIF_SUBR_COPYOUTSTR)
328 330 dp->dtdo_destructive = 1;
329 331 continue;
330 332 }
331 333
332 334 if (op >= DIF_OP_BA && op <= DIF_OP_BLEU) {
333 335 assert(DIF_INSTR_LABEL(instr) < dlp->dl_label);
334 336 dp->dtdo_buf[i] = DIF_INSTR_BRANCH(op,
335 337 labels[DIF_INSTR_LABEL(instr)]);
336 338 }
337 339 }
338 340
339 341 dt_free(dtp, labels);
340 342 pcb->pcb_asvidx = 0;
341 343
342 344 /*
343 345 * Allocate memory for the appropriate number of variable records and
344 346 * then fill in each variable record. As we populate the variable
345 347 * table we insert the corresponding variable names into the strtab.
346 348 */
347 349 (void) dt_idhash_iter(dtp->dt_tls, dt_countvar, &n);
348 350 (void) dt_idhash_iter(dtp->dt_globals, dt_countvar, &n);
349 351 (void) dt_idhash_iter(pcb->pcb_locals, dt_countvar, &n);
350 352
351 353 if (n != 0) {
352 354 dp->dtdo_vartab = dt_alloc(dtp, n * sizeof (dtrace_difv_t));
353 355 dp->dtdo_varlen = (uint32_t)n;
354 356
355 357 if (dp->dtdo_vartab == NULL)
356 358 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
357 359
358 360 (void) dt_idhash_iter(dtp->dt_tls, dt_copyvar, pcb);
359 361 (void) dt_idhash_iter(dtp->dt_globals, dt_copyvar, pcb);
360 362 (void) dt_idhash_iter(pcb->pcb_locals, dt_copyvar, pcb);
361 363 }
362 364
363 365 /*
364 366 * Allocate memory for the appropriate number of relocation table
365 367 * entries based upon our kernel and user counts from the first pass.
366 368 */
367 369 if (krel != 0) {
368 370 dp->dtdo_kreltab = dt_alloc(dtp,
369 371 krel * sizeof (dof_relodesc_t));
370 372 dp->dtdo_krelen = krel;
371 373
372 374 if (dp->dtdo_kreltab == NULL)
373 375 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
374 376 }
375 377
376 378 if (urel != 0) {
377 379 dp->dtdo_ureltab = dt_alloc(dtp,
378 380 urel * sizeof (dof_relodesc_t));
379 381 dp->dtdo_urelen = urel;
380 382
381 383 if (dp->dtdo_ureltab == NULL)
382 384 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
383 385 }
384 386
385 387 if (xlrefs != 0) {
386 388 dp->dtdo_xlmtab = dt_zalloc(dtp, sizeof (dt_node_t *) * xlrefs);
387 389 dp->dtdo_xlmlen = xlrefs;
388 390
389 391 if (dp->dtdo_xlmtab == NULL)
390 392 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
391 393 }
392 394
393 395 /*
394 396 * If any relocations are needed, make another pass through the
395 397 * instruction list and fill in the relocation table entries.
396 398 */
397 399 if (krel + urel + xlrefs != 0) {
398 400 uint_t knodef = pcb->pcb_cflags & DTRACE_C_KNODEF;
399 401 uint_t unodef = pcb->pcb_cflags & DTRACE_C_UNODEF;
400 402
401 403 dof_relodesc_t *krp = dp->dtdo_kreltab;
402 404 dof_relodesc_t *urp = dp->dtdo_ureltab;
403 405 dt_node_t **xlp = dp->dtdo_xlmtab;
404 406
405 407 i = 0; /* dtdo_buf[] index */
406 408
407 409 for (dip = dlp->dl_list; dip != NULL; dip = dip->di_next) {
408 410 dof_relodesc_t *rp;
409 411 ssize_t soff;
410 412 uint_t nodef;
411 413
412 414 if (dip->di_label != DT_LBL_NONE &&
413 415 dip->di_instr == DIF_INSTR_NOP)
414 416 continue; /* skip label declarations */
415 417
416 418 i++; /* advance dtdo_buf[] index */
417 419
418 420 if (DIF_INSTR_OP(dip->di_instr) == DIF_OP_XLATE ||
419 421 DIF_INSTR_OP(dip->di_instr) == DIF_OP_XLARG) {
420 422 assert(dp->dtdo_buf[i - 1] == dip->di_instr);
421 423 dt_as_xlate(pcb, dp, i - 1, (uint_t)
422 424 (xlp++ - dp->dtdo_xlmtab), dip->di_extern);
423 425 continue;
424 426 }
425 427
426 428 if ((idp = dip->di_extern) == NULL)
427 429 continue; /* no relocation entry needed */
428 430
429 431 if ((idp->di_flags & kmask) == kbits) {
430 432 nodef = knodef;
431 433 rp = krp++;
432 434 } else if ((idp->di_flags & umask) == ubits) {
433 435 nodef = unodef;
434 436 rp = urp++;
435 437 } else
436 438 continue;
437 439
438 440 if (!nodef)
439 441 dt_as_undef(idp, i);
440 442
441 443 assert(DIF_INSTR_OP(dip->di_instr) == DIF_OP_SETX);
442 444 soff = dt_strtab_insert(pcb->pcb_strtab, idp->di_name);
443 445
444 446 if (soff == -1L)
445 447 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
446 448 if (soff > DIF_STROFF_MAX)
447 449 longjmp(pcb->pcb_jmpbuf, EDT_STR2BIG);
448 450
449 451 rp->dofr_name = (dof_stridx_t)soff;
450 452 rp->dofr_type = DOF_RELO_SETX;
451 453 rp->dofr_offset = DIF_INSTR_INTEGER(dip->di_instr) *
452 454 sizeof (uint64_t);
453 455 rp->dofr_data = 0;
454 456 }
455 457
456 458 assert(krp == dp->dtdo_kreltab + dp->dtdo_krelen);
457 459 assert(urp == dp->dtdo_ureltab + dp->dtdo_urelen);
458 460 assert(xlp == dp->dtdo_xlmtab + dp->dtdo_xlmlen);
459 461 assert(i == dp->dtdo_len);
460 462 }
461 463
462 464 /*
463 465 * Allocate memory for the compiled string table and then copy the
464 466 * chunks from the string table into the final string buffer.
465 467 */
466 468 if ((n = dt_strtab_size(pcb->pcb_strtab)) != 0) {
467 469 if ((dp->dtdo_strtab = dt_alloc(dtp, n)) == NULL)
468 470 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
469 471
470 472 (void) dt_strtab_write(pcb->pcb_strtab,
471 473 (dt_strtab_write_f *)dt_copystr, pcb);
472 474 dp->dtdo_strlen = (uint32_t)n;
473 475 }
474 476
475 477 /*
476 478 * Allocate memory for the compiled integer table and then copy the
477 479 * integer constants from the table into the final integer buffer.
478 480 */
479 481 if ((n = dt_inttab_size(pcb->pcb_inttab)) != 0) {
480 482 if ((dp->dtdo_inttab = dt_alloc(dtp,
481 483 n * sizeof (uint64_t))) == NULL)
482 484 longjmp(pcb->pcb_jmpbuf, EDT_NOMEM);
483 485
484 486 dt_inttab_write(pcb->pcb_inttab, dp->dtdo_inttab);
485 487 dp->dtdo_intlen = (uint32_t)n;
486 488 }
487 489
488 490 /*
489 491 * Fill in the DIFO return type from the type associated with the
490 492 * node saved in pcb_dret, and then clear pcb_difo and pcb_dret
491 493 * now that the assembler has completed successfully.
492 494 */
493 495 dt_node_diftype(dtp, pcb->pcb_dret, &dp->dtdo_rtype);
494 496 pcb->pcb_difo = NULL;
495 497 pcb->pcb_dret = NULL;
496 498
497 499 if (pcb->pcb_cflags & DTRACE_C_DIFV)
498 500 dt_dis(dp, stderr);
499 501
500 502 return (dp);
501 503 }
↓ open down ↓ |
363 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX