Print this page
4159 dtrace ignores all ustack helper frames from dead processes
Reviewed by: Bryan Cantrill <bryan@joyent.com>
Reviewed by: Robert Mustacchi <rm@joyent.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libdtrace/common/dt_consume.c
+++ new/usr/src/lib/libdtrace/common/dt_consume.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
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 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25
26 26 /*
27 27 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
28 28 * Copyright (c) 2012 by Delphix. All rights reserved.
29 29 */
30 30
31 31 #include <stdlib.h>
32 32 #include <strings.h>
33 33 #include <errno.h>
34 34 #include <unistd.h>
35 35 #include <limits.h>
36 36 #include <assert.h>
37 37 #include <ctype.h>
38 38 #include <alloca.h>
39 39 #include <dt_impl.h>
40 40 #include <dt_pq.h>
41 41
42 42 #define DT_MASK_LO 0x00000000FFFFFFFFULL
43 43
44 44 /*
45 45 * We declare this here because (1) we need it and (2) we want to avoid a
46 46 * dependency on libm in libdtrace.
47 47 */
48 48 static long double
49 49 dt_fabsl(long double x)
50 50 {
51 51 if (x < 0)
52 52 return (-x);
53 53
54 54 return (x);
55 55 }
56 56
57 57 static int
58 58 dt_ndigits(long long val)
59 59 {
60 60 int rval = 1;
61 61 long long cmp = 10;
62 62
63 63 if (val < 0) {
64 64 val = val == INT64_MIN ? INT64_MAX : -val;
65 65 rval++;
66 66 }
67 67
68 68 while (val > cmp && cmp > 0) {
69 69 rval++;
70 70 cmp *= 10;
71 71 }
72 72
73 73 return (rval < 4 ? 4 : rval);
74 74 }
75 75
76 76 /*
77 77 * 128-bit arithmetic functions needed to support the stddev() aggregating
78 78 * action.
79 79 */
80 80 static int
81 81 dt_gt_128(uint64_t *a, uint64_t *b)
82 82 {
83 83 return (a[1] > b[1] || (a[1] == b[1] && a[0] > b[0]));
84 84 }
85 85
86 86 static int
87 87 dt_ge_128(uint64_t *a, uint64_t *b)
88 88 {
89 89 return (a[1] > b[1] || (a[1] == b[1] && a[0] >= b[0]));
90 90 }
91 91
92 92 static int
93 93 dt_le_128(uint64_t *a, uint64_t *b)
94 94 {
95 95 return (a[1] < b[1] || (a[1] == b[1] && a[0] <= b[0]));
96 96 }
97 97
98 98 /*
99 99 * Shift the 128-bit value in a by b. If b is positive, shift left.
100 100 * If b is negative, shift right.
101 101 */
102 102 static void
103 103 dt_shift_128(uint64_t *a, int b)
104 104 {
105 105 uint64_t mask;
106 106
107 107 if (b == 0)
108 108 return;
109 109
110 110 if (b < 0) {
111 111 b = -b;
112 112 if (b >= 64) {
113 113 a[0] = a[1] >> (b - 64);
114 114 a[1] = 0;
115 115 } else {
116 116 a[0] >>= b;
117 117 mask = 1LL << (64 - b);
118 118 mask -= 1;
119 119 a[0] |= ((a[1] & mask) << (64 - b));
120 120 a[1] >>= b;
121 121 }
122 122 } else {
123 123 if (b >= 64) {
124 124 a[1] = a[0] << (b - 64);
125 125 a[0] = 0;
126 126 } else {
127 127 a[1] <<= b;
128 128 mask = a[0] >> (64 - b);
129 129 a[1] |= mask;
130 130 a[0] <<= b;
131 131 }
132 132 }
133 133 }
134 134
135 135 static int
136 136 dt_nbits_128(uint64_t *a)
137 137 {
138 138 int nbits = 0;
139 139 uint64_t tmp[2];
140 140 uint64_t zero[2] = { 0, 0 };
141 141
142 142 tmp[0] = a[0];
143 143 tmp[1] = a[1];
144 144
145 145 dt_shift_128(tmp, -1);
146 146 while (dt_gt_128(tmp, zero)) {
147 147 dt_shift_128(tmp, -1);
148 148 nbits++;
149 149 }
150 150
151 151 return (nbits);
152 152 }
153 153
154 154 static void
155 155 dt_subtract_128(uint64_t *minuend, uint64_t *subtrahend, uint64_t *difference)
156 156 {
157 157 uint64_t result[2];
158 158
159 159 result[0] = minuend[0] - subtrahend[0];
160 160 result[1] = minuend[1] - subtrahend[1] -
161 161 (minuend[0] < subtrahend[0] ? 1 : 0);
162 162
163 163 difference[0] = result[0];
164 164 difference[1] = result[1];
165 165 }
166 166
167 167 static void
168 168 dt_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum)
169 169 {
170 170 uint64_t result[2];
171 171
172 172 result[0] = addend1[0] + addend2[0];
173 173 result[1] = addend1[1] + addend2[1] +
174 174 (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0);
175 175
176 176 sum[0] = result[0];
177 177 sum[1] = result[1];
178 178 }
179 179
180 180 /*
181 181 * The basic idea is to break the 2 64-bit values into 4 32-bit values,
182 182 * use native multiplication on those, and then re-combine into the
183 183 * resulting 128-bit value.
184 184 *
185 185 * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) =
186 186 * hi1 * hi2 << 64 +
187 187 * hi1 * lo2 << 32 +
188 188 * hi2 * lo1 << 32 +
189 189 * lo1 * lo2
190 190 */
191 191 static void
192 192 dt_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product)
193 193 {
194 194 uint64_t hi1, hi2, lo1, lo2;
195 195 uint64_t tmp[2];
196 196
197 197 hi1 = factor1 >> 32;
198 198 hi2 = factor2 >> 32;
199 199
200 200 lo1 = factor1 & DT_MASK_LO;
201 201 lo2 = factor2 & DT_MASK_LO;
202 202
203 203 product[0] = lo1 * lo2;
204 204 product[1] = hi1 * hi2;
205 205
206 206 tmp[0] = hi1 * lo2;
207 207 tmp[1] = 0;
208 208 dt_shift_128(tmp, 32);
209 209 dt_add_128(product, tmp, product);
210 210
211 211 tmp[0] = hi2 * lo1;
212 212 tmp[1] = 0;
213 213 dt_shift_128(tmp, 32);
214 214 dt_add_128(product, tmp, product);
215 215 }
216 216
217 217 /*
218 218 * This is long-hand division.
219 219 *
220 220 * We initialize subtrahend by shifting divisor left as far as possible. We
221 221 * loop, comparing subtrahend to dividend: if subtrahend is smaller, we
222 222 * subtract and set the appropriate bit in the result. We then shift
223 223 * subtrahend right by one bit for the next comparison.
224 224 */
225 225 static void
226 226 dt_divide_128(uint64_t *dividend, uint64_t divisor, uint64_t *quotient)
227 227 {
228 228 uint64_t result[2] = { 0, 0 };
229 229 uint64_t remainder[2];
230 230 uint64_t subtrahend[2];
231 231 uint64_t divisor_128[2];
232 232 uint64_t mask[2] = { 1, 0 };
233 233 int log = 0;
234 234
235 235 assert(divisor != 0);
236 236
237 237 divisor_128[0] = divisor;
238 238 divisor_128[1] = 0;
239 239
240 240 remainder[0] = dividend[0];
241 241 remainder[1] = dividend[1];
242 242
243 243 subtrahend[0] = divisor;
244 244 subtrahend[1] = 0;
245 245
246 246 while (divisor > 0) {
247 247 log++;
248 248 divisor >>= 1;
249 249 }
250 250
251 251 dt_shift_128(subtrahend, 128 - log);
252 252 dt_shift_128(mask, 128 - log);
253 253
254 254 while (dt_ge_128(remainder, divisor_128)) {
255 255 if (dt_ge_128(remainder, subtrahend)) {
256 256 dt_subtract_128(remainder, subtrahend, remainder);
257 257 result[0] |= mask[0];
258 258 result[1] |= mask[1];
259 259 }
260 260
261 261 dt_shift_128(subtrahend, -1);
262 262 dt_shift_128(mask, -1);
263 263 }
264 264
265 265 quotient[0] = result[0];
266 266 quotient[1] = result[1];
267 267 }
268 268
269 269 /*
270 270 * This is the long-hand method of calculating a square root.
271 271 * The algorithm is as follows:
272 272 *
273 273 * 1. Group the digits by 2 from the right.
274 274 * 2. Over the leftmost group, find the largest single-digit number
275 275 * whose square is less than that group.
276 276 * 3. Subtract the result of the previous step (2 or 4, depending) and
277 277 * bring down the next two-digit group.
278 278 * 4. For the result R we have so far, find the largest single-digit number
279 279 * x such that 2 * R * 10 * x + x^2 is less than the result from step 3.
280 280 * (Note that this is doubling R and performing a decimal left-shift by 1
281 281 * and searching for the appropriate decimal to fill the one's place.)
282 282 * The value x is the next digit in the square root.
283 283 * Repeat steps 3 and 4 until the desired precision is reached. (We're
284 284 * dealing with integers, so the above is sufficient.)
285 285 *
286 286 * In decimal, the square root of 582,734 would be calculated as so:
287 287 *
288 288 * __7__6__3
289 289 * | 58 27 34
290 290 * -49 (7^2 == 49 => 7 is the first digit in the square root)
291 291 * --
292 292 * 9 27 (Subtract and bring down the next group.)
293 293 * 146 8 76 (2 * 7 * 10 * 6 + 6^2 == 876 => 6 is the next digit in
294 294 * ----- the square root)
295 295 * 51 34 (Subtract and bring down the next group.)
296 296 * 1523 45 69 (2 * 76 * 10 * 3 + 3^2 == 4569 => 3 is the next digit in
297 297 * ----- the square root)
298 298 * 5 65 (remainder)
299 299 *
300 300 * The above algorithm applies similarly in binary, but note that the
301 301 * only possible non-zero value for x in step 4 is 1, so step 4 becomes a
302 302 * simple decision: is 2 * R * 2 * 1 + 1^2 (aka R << 2 + 1) less than the
303 303 * preceding difference?
304 304 *
305 305 * In binary, the square root of 11011011 would be calculated as so:
306 306 *
307 307 * __1__1__1__0
308 308 * | 11 01 10 11
309 309 * 01 (0 << 2 + 1 == 1 < 11 => this bit is 1)
310 310 * --
311 311 * 10 01 10 11
312 312 * 101 1 01 (1 << 2 + 1 == 101 < 1001 => next bit is 1)
313 313 * -----
314 314 * 1 00 10 11
315 315 * 1101 11 01 (11 << 2 + 1 == 1101 < 10010 => next bit is 1)
316 316 * -------
317 317 * 1 01 11
318 318 * 11101 1 11 01 (111 << 2 + 1 == 11101 > 10111 => last bit is 0)
319 319 *
320 320 */
321 321 static uint64_t
322 322 dt_sqrt_128(uint64_t *square)
323 323 {
324 324 uint64_t result[2] = { 0, 0 };
325 325 uint64_t diff[2] = { 0, 0 };
326 326 uint64_t one[2] = { 1, 0 };
327 327 uint64_t next_pair[2];
328 328 uint64_t next_try[2];
329 329 uint64_t bit_pairs, pair_shift;
330 330 int i;
331 331
332 332 bit_pairs = dt_nbits_128(square) / 2;
333 333 pair_shift = bit_pairs * 2;
334 334
335 335 for (i = 0; i <= bit_pairs; i++) {
336 336 /*
337 337 * Bring down the next pair of bits.
338 338 */
339 339 next_pair[0] = square[0];
340 340 next_pair[1] = square[1];
341 341 dt_shift_128(next_pair, -pair_shift);
342 342 next_pair[0] &= 0x3;
343 343 next_pair[1] = 0;
344 344
345 345 dt_shift_128(diff, 2);
346 346 dt_add_128(diff, next_pair, diff);
347 347
348 348 /*
349 349 * next_try = R << 2 + 1
350 350 */
351 351 next_try[0] = result[0];
352 352 next_try[1] = result[1];
353 353 dt_shift_128(next_try, 2);
354 354 dt_add_128(next_try, one, next_try);
355 355
356 356 if (dt_le_128(next_try, diff)) {
357 357 dt_subtract_128(diff, next_try, diff);
358 358 dt_shift_128(result, 1);
359 359 dt_add_128(result, one, result);
360 360 } else {
361 361 dt_shift_128(result, 1);
362 362 }
363 363
364 364 pair_shift -= 2;
365 365 }
366 366
367 367 assert(result[1] == 0);
368 368
369 369 return (result[0]);
370 370 }
371 371
372 372 uint64_t
373 373 dt_stddev(uint64_t *data, uint64_t normal)
374 374 {
375 375 uint64_t avg_of_squares[2];
376 376 uint64_t square_of_avg[2];
377 377 int64_t norm_avg;
378 378 uint64_t diff[2];
379 379
380 380 /*
381 381 * The standard approximation for standard deviation is
382 382 * sqrt(average(x**2) - average(x)**2), i.e. the square root
383 383 * of the average of the squares minus the square of the average.
384 384 */
385 385 dt_divide_128(data + 2, normal, avg_of_squares);
386 386 dt_divide_128(avg_of_squares, data[0], avg_of_squares);
387 387
388 388 norm_avg = (int64_t)data[1] / (int64_t)normal / (int64_t)data[0];
389 389
390 390 if (norm_avg < 0)
391 391 norm_avg = -norm_avg;
392 392
393 393 dt_multiply_128((uint64_t)norm_avg, (uint64_t)norm_avg, square_of_avg);
394 394
395 395 dt_subtract_128(avg_of_squares, square_of_avg, diff);
396 396
397 397 return (dt_sqrt_128(diff));
398 398 }
399 399
400 400 static int
401 401 dt_flowindent(dtrace_hdl_t *dtp, dtrace_probedata_t *data, dtrace_epid_t last,
402 402 dtrace_bufdesc_t *buf, size_t offs)
403 403 {
404 404 dtrace_probedesc_t *pd = data->dtpda_pdesc, *npd;
405 405 dtrace_eprobedesc_t *epd = data->dtpda_edesc, *nepd;
406 406 char *p = pd->dtpd_provider, *n = pd->dtpd_name, *sub;
407 407 dtrace_flowkind_t flow = DTRACEFLOW_NONE;
408 408 const char *str = NULL;
409 409 static const char *e_str[2] = { " -> ", " => " };
410 410 static const char *r_str[2] = { " <- ", " <= " };
411 411 static const char *ent = "entry", *ret = "return";
412 412 static int entlen = 0, retlen = 0;
413 413 dtrace_epid_t next, id = epd->dtepd_epid;
414 414 int rval;
415 415
416 416 if (entlen == 0) {
417 417 assert(retlen == 0);
418 418 entlen = strlen(ent);
419 419 retlen = strlen(ret);
420 420 }
421 421
422 422 /*
423 423 * If the name of the probe is "entry" or ends with "-entry", we
424 424 * treat it as an entry; if it is "return" or ends with "-return",
425 425 * we treat it as a return. (This allows application-provided probes
426 426 * like "method-entry" or "function-entry" to participate in flow
427 427 * indentation -- without accidentally misinterpreting popular probe
428 428 * names like "carpentry", "gentry" or "Coventry".)
429 429 */
430 430 if ((sub = strstr(n, ent)) != NULL && sub[entlen] == '\0' &&
431 431 (sub == n || sub[-1] == '-')) {
432 432 flow = DTRACEFLOW_ENTRY;
433 433 str = e_str[strcmp(p, "syscall") == 0];
434 434 } else if ((sub = strstr(n, ret)) != NULL && sub[retlen] == '\0' &&
435 435 (sub == n || sub[-1] == '-')) {
436 436 flow = DTRACEFLOW_RETURN;
437 437 str = r_str[strcmp(p, "syscall") == 0];
438 438 }
439 439
440 440 /*
441 441 * If we're going to indent this, we need to check the ID of our last
442 442 * call. If we're looking at the same probe ID but a different EPID,
443 443 * we _don't_ want to indent. (Yes, there are some minor holes in
444 444 * this scheme -- it's a heuristic.)
445 445 */
446 446 if (flow == DTRACEFLOW_ENTRY) {
447 447 if ((last != DTRACE_EPIDNONE && id != last &&
448 448 pd->dtpd_id == dtp->dt_pdesc[last]->dtpd_id))
449 449 flow = DTRACEFLOW_NONE;
450 450 }
451 451
452 452 /*
453 453 * If we're going to unindent this, it's more difficult to see if
454 454 * we don't actually want to unindent it -- we need to look at the
455 455 * _next_ EPID.
456 456 */
457 457 if (flow == DTRACEFLOW_RETURN) {
458 458 offs += epd->dtepd_size;
459 459
460 460 do {
461 461 if (offs >= buf->dtbd_size)
462 462 goto out;
463 463
464 464 next = *(uint32_t *)((uintptr_t)buf->dtbd_data + offs);
465 465
466 466 if (next == DTRACE_EPIDNONE)
467 467 offs += sizeof (id);
468 468 } while (next == DTRACE_EPIDNONE);
469 469
470 470 if ((rval = dt_epid_lookup(dtp, next, &nepd, &npd)) != 0)
471 471 return (rval);
472 472
473 473 if (next != id && npd->dtpd_id == pd->dtpd_id)
474 474 flow = DTRACEFLOW_NONE;
475 475 }
476 476
477 477 out:
478 478 if (flow == DTRACEFLOW_ENTRY || flow == DTRACEFLOW_RETURN) {
479 479 data->dtpda_prefix = str;
480 480 } else {
481 481 data->dtpda_prefix = "| ";
482 482 }
483 483
484 484 if (flow == DTRACEFLOW_RETURN && data->dtpda_indent > 0)
485 485 data->dtpda_indent -= 2;
486 486
487 487 data->dtpda_flow = flow;
488 488
489 489 return (0);
490 490 }
491 491
492 492 static int
493 493 dt_nullprobe()
494 494 {
495 495 return (DTRACE_CONSUME_THIS);
496 496 }
497 497
498 498 static int
499 499 dt_nullrec()
500 500 {
501 501 return (DTRACE_CONSUME_NEXT);
502 502 }
503 503
504 504 static void
505 505 dt_quantize_total(dtrace_hdl_t *dtp, int64_t datum, long double *total)
506 506 {
507 507 long double val = dt_fabsl((long double)datum);
508 508
509 509 if (dtp->dt_options[DTRACEOPT_AGGZOOM] == DTRACEOPT_UNSET) {
510 510 *total += val;
511 511 return;
512 512 }
513 513
514 514 /*
515 515 * If we're zooming in on an aggregation, we want the height of the
516 516 * highest value to be approximately 95% of total bar height -- so we
517 517 * adjust up by the reciprocal of DTRACE_AGGZOOM_MAX when comparing to
518 518 * our highest value.
519 519 */
520 520 val *= 1 / DTRACE_AGGZOOM_MAX;
521 521
522 522 if (*total < val)
523 523 *total = val;
524 524 }
525 525
526 526 static int
527 527 dt_print_quanthdr(dtrace_hdl_t *dtp, FILE *fp, int width)
528 528 {
529 529 return (dt_printf(dtp, fp, "\n%*s %41s %-9s\n",
530 530 width ? width : 16, width ? "key" : "value",
531 531 "------------- Distribution -------------", "count"));
532 532 }
533 533
534 534 static int
535 535 dt_print_quanthdr_packed(dtrace_hdl_t *dtp, FILE *fp, int width,
536 536 const dtrace_aggdata_t *aggdata, dtrace_actkind_t action)
537 537 {
538 538 int min = aggdata->dtada_minbin, max = aggdata->dtada_maxbin;
539 539 int minwidth, maxwidth, i;
540 540
541 541 assert(action == DTRACEAGG_QUANTIZE || action == DTRACEAGG_LQUANTIZE);
542 542
543 543 if (action == DTRACEAGG_QUANTIZE) {
544 544 if (min != 0 && min != DTRACE_QUANTIZE_ZEROBUCKET)
545 545 min--;
546 546
547 547 if (max < DTRACE_QUANTIZE_NBUCKETS - 1)
548 548 max++;
549 549
550 550 minwidth = dt_ndigits(DTRACE_QUANTIZE_BUCKETVAL(min));
551 551 maxwidth = dt_ndigits(DTRACE_QUANTIZE_BUCKETVAL(max));
552 552 } else {
553 553 maxwidth = 8;
554 554 minwidth = maxwidth - 1;
555 555 max++;
556 556 }
557 557
558 558 if (dt_printf(dtp, fp, "\n%*s %*s .",
559 559 width, width > 0 ? "key" : "", minwidth, "min") < 0)
560 560 return (-1);
561 561
562 562 for (i = min; i <= max; i++) {
563 563 if (dt_printf(dtp, fp, "-") < 0)
564 564 return (-1);
565 565 }
566 566
567 567 return (dt_printf(dtp, fp, ". %*s | count\n", -maxwidth, "max"));
568 568 }
569 569
570 570 /*
571 571 * We use a subset of the Unicode Block Elements (U+2588 through U+258F,
572 572 * inclusive) to represent aggregations via UTF-8 -- which are expressed via
573 573 * 3-byte UTF-8 sequences.
574 574 */
575 575 #define DTRACE_AGGUTF8_FULL 0x2588
576 576 #define DTRACE_AGGUTF8_BASE 0x258f
577 577 #define DTRACE_AGGUTF8_LEVELS 8
578 578
579 579 #define DTRACE_AGGUTF8_BYTE0(val) (0xe0 | ((val) >> 12))
580 580 #define DTRACE_AGGUTF8_BYTE1(val) (0x80 | (((val) >> 6) & 0x3f))
581 581 #define DTRACE_AGGUTF8_BYTE2(val) (0x80 | ((val) & 0x3f))
582 582
583 583 static int
584 584 dt_print_quantline_utf8(dtrace_hdl_t *dtp, FILE *fp, int64_t val,
585 585 uint64_t normal, long double total)
586 586 {
587 587 uint_t len = 40, i, whole, partial;
588 588 long double f = (dt_fabsl((long double)val) * len) / total;
589 589 const char *spaces = " ";
590 590
591 591 whole = (uint_t)f;
592 592 partial = (uint_t)((f - (long double)(uint_t)f) *
593 593 (long double)DTRACE_AGGUTF8_LEVELS);
594 594
595 595 if (dt_printf(dtp, fp, "|") < 0)
596 596 return (-1);
597 597
598 598 for (i = 0; i < whole; i++) {
599 599 if (dt_printf(dtp, fp, "%c%c%c",
600 600 DTRACE_AGGUTF8_BYTE0(DTRACE_AGGUTF8_FULL),
601 601 DTRACE_AGGUTF8_BYTE1(DTRACE_AGGUTF8_FULL),
602 602 DTRACE_AGGUTF8_BYTE2(DTRACE_AGGUTF8_FULL)) < 0)
603 603 return (-1);
604 604 }
605 605
606 606 if (partial != 0) {
607 607 partial = DTRACE_AGGUTF8_BASE - (partial - 1);
608 608
609 609 if (dt_printf(dtp, fp, "%c%c%c",
610 610 DTRACE_AGGUTF8_BYTE0(partial),
611 611 DTRACE_AGGUTF8_BYTE1(partial),
612 612 DTRACE_AGGUTF8_BYTE2(partial)) < 0)
613 613 return (-1);
614 614
615 615 i++;
616 616 }
617 617
618 618 return (dt_printf(dtp, fp, "%s %-9lld\n", spaces + i,
619 619 (long long)val / normal));
620 620 }
621 621
622 622 static int
623 623 dt_print_quantline(dtrace_hdl_t *dtp, FILE *fp, int64_t val,
624 624 uint64_t normal, long double total, char positives, char negatives)
625 625 {
626 626 long double f;
627 627 uint_t depth, len = 40;
628 628
629 629 const char *ats = "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@";
630 630 const char *spaces = " ";
631 631
632 632 assert(strlen(ats) == len && strlen(spaces) == len);
633 633 assert(!(total == 0 && (positives || negatives)));
634 634 assert(!(val < 0 && !negatives));
635 635 assert(!(val > 0 && !positives));
636 636 assert(!(val != 0 && total == 0));
637 637
638 638 if (!negatives) {
639 639 if (positives) {
640 640 if (dtp->dt_encoding == DT_ENCODING_UTF8) {
641 641 return (dt_print_quantline_utf8(dtp, fp, val,
642 642 normal, total));
643 643 }
644 644
645 645 f = (dt_fabsl((long double)val) * len) / total;
646 646 depth = (uint_t)(f + 0.5);
647 647 } else {
648 648 depth = 0;
649 649 }
650 650
651 651 return (dt_printf(dtp, fp, "|%s%s %-9lld\n", ats + len - depth,
652 652 spaces + depth, (long long)val / normal));
653 653 }
654 654
655 655 if (!positives) {
656 656 f = (dt_fabsl((long double)val) * len) / total;
657 657 depth = (uint_t)(f + 0.5);
658 658
659 659 return (dt_printf(dtp, fp, "%s%s| %-9lld\n", spaces + depth,
660 660 ats + len - depth, (long long)val / normal));
661 661 }
662 662
663 663 /*
664 664 * If we're here, we have both positive and negative bucket values.
665 665 * To express this graphically, we're going to generate both positive
666 666 * and negative bars separated by a centerline. These bars are half
667 667 * the size of normal quantize()/lquantize() bars, so we divide the
668 668 * length in half before calculating the bar length.
669 669 */
670 670 len /= 2;
671 671 ats = &ats[len];
672 672 spaces = &spaces[len];
673 673
674 674 f = (dt_fabsl((long double)val) * len) / total;
675 675 depth = (uint_t)(f + 0.5);
676 676
677 677 if (val <= 0) {
678 678 return (dt_printf(dtp, fp, "%s%s|%*s %-9lld\n", spaces + depth,
679 679 ats + len - depth, len, "", (long long)val / normal));
680 680 } else {
681 681 return (dt_printf(dtp, fp, "%20s|%s%s %-9lld\n", "",
682 682 ats + len - depth, spaces + depth,
683 683 (long long)val / normal));
684 684 }
685 685 }
686 686
687 687 /*
688 688 * As with UTF-8 printing of aggregations, we use a subset of the Unicode
689 689 * Block Elements (U+2581 through U+2588, inclusive) to represent our packed
690 690 * aggregation.
691 691 */
692 692 #define DTRACE_AGGPACK_BASE 0x2581
693 693 #define DTRACE_AGGPACK_LEVELS 8
694 694
695 695 static int
696 696 dt_print_packed(dtrace_hdl_t *dtp, FILE *fp,
697 697 long double datum, long double total)
698 698 {
699 699 static boolean_t utf8_checked = B_FALSE;
700 700 static boolean_t utf8;
701 701 char *ascii = "__xxxxXX";
702 702 char *neg = "vvvvVV";
703 703 unsigned int len;
704 704 long double val;
705 705
706 706 if (!utf8_checked) {
707 707 char *term;
708 708
709 709 /*
710 710 * We want to determine if we can reasonably emit UTF-8 for our
711 711 * packed aggregation. To do this, we will check for terminals
712 712 * that are known to be primitive to emit UTF-8 on these.
713 713 */
714 714 utf8_checked = B_TRUE;
715 715
716 716 if (dtp->dt_encoding == DT_ENCODING_ASCII)
717 717 utf8 = B_FALSE;
718 718 else if (dtp->dt_encoding == DT_ENCODING_UTF8)
719 719 utf8 = B_TRUE;
720 720 else if ((term = getenv("TERM")) != NULL &&
721 721 (strcmp(term, "sun") == 0 ||
722 722 strcmp(term, "sun-color") == 0) ||
723 723 strcmp(term, "dumb") == 0)
724 724 utf8 = B_FALSE;
725 725 else
726 726 utf8 = B_TRUE;
727 727 }
728 728
729 729 if (datum == 0)
730 730 return (dt_printf(dtp, fp, " "));
731 731
732 732 if (datum < 0) {
733 733 len = strlen(neg);
734 734 val = dt_fabsl(datum * (len - 1)) / total;
735 735 return (dt_printf(dtp, fp, "%c", neg[(uint_t)(val + 0.5)]));
736 736 }
737 737
738 738 if (utf8) {
739 739 int block = DTRACE_AGGPACK_BASE + (unsigned int)(((datum *
740 740 (DTRACE_AGGPACK_LEVELS - 1)) / total) + 0.5);
741 741
742 742 return (dt_printf(dtp, fp, "%c%c%c",
743 743 DTRACE_AGGUTF8_BYTE0(block),
744 744 DTRACE_AGGUTF8_BYTE1(block),
745 745 DTRACE_AGGUTF8_BYTE2(block)));
746 746 }
747 747
748 748 len = strlen(ascii);
749 749 val = (datum * (len - 1)) / total;
750 750 return (dt_printf(dtp, fp, "%c", ascii[(uint_t)(val + 0.5)]));
751 751 }
752 752
753 753 int
754 754 dt_print_quantize(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
755 755 size_t size, uint64_t normal)
756 756 {
757 757 const int64_t *data = addr;
758 758 int i, first_bin = 0, last_bin = DTRACE_QUANTIZE_NBUCKETS - 1;
759 759 long double total = 0;
760 760 char positives = 0, negatives = 0;
761 761
762 762 if (size != DTRACE_QUANTIZE_NBUCKETS * sizeof (uint64_t))
763 763 return (dt_set_errno(dtp, EDT_DMISMATCH));
764 764
765 765 while (first_bin < DTRACE_QUANTIZE_NBUCKETS - 1 && data[first_bin] == 0)
766 766 first_bin++;
767 767
768 768 if (first_bin == DTRACE_QUANTIZE_NBUCKETS - 1) {
769 769 /*
770 770 * There isn't any data. This is possible if the aggregation
771 771 * has been clear()'d or if negative increment values have been
772 772 * used. Regardless, we'll print the buckets around 0.
773 773 */
774 774 first_bin = DTRACE_QUANTIZE_ZEROBUCKET - 1;
775 775 last_bin = DTRACE_QUANTIZE_ZEROBUCKET + 1;
776 776 } else {
777 777 if (first_bin > 0)
778 778 first_bin--;
779 779
780 780 while (last_bin > 0 && data[last_bin] == 0)
781 781 last_bin--;
782 782
783 783 if (last_bin < DTRACE_QUANTIZE_NBUCKETS - 1)
784 784 last_bin++;
785 785 }
786 786
787 787 for (i = first_bin; i <= last_bin; i++) {
788 788 positives |= (data[i] > 0);
789 789 negatives |= (data[i] < 0);
790 790 dt_quantize_total(dtp, data[i], &total);
791 791 }
792 792
793 793 if (dt_print_quanthdr(dtp, fp, 0) < 0)
794 794 return (-1);
795 795
796 796 for (i = first_bin; i <= last_bin; i++) {
797 797 if (dt_printf(dtp, fp, "%16lld ",
798 798 (long long)DTRACE_QUANTIZE_BUCKETVAL(i)) < 0)
799 799 return (-1);
800 800
801 801 if (dt_print_quantline(dtp, fp, data[i], normal, total,
802 802 positives, negatives) < 0)
803 803 return (-1);
804 804 }
805 805
806 806 return (0);
807 807 }
808 808
809 809 int
810 810 dt_print_quantize_packed(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
811 811 size_t size, const dtrace_aggdata_t *aggdata)
812 812 {
813 813 const int64_t *data = addr;
814 814 long double total = 0, count = 0;
815 815 int min = aggdata->dtada_minbin, max = aggdata->dtada_maxbin, i;
816 816 int64_t minval, maxval;
817 817
818 818 if (size != DTRACE_QUANTIZE_NBUCKETS * sizeof (uint64_t))
819 819 return (dt_set_errno(dtp, EDT_DMISMATCH));
820 820
821 821 if (min != 0 && min != DTRACE_QUANTIZE_ZEROBUCKET)
822 822 min--;
823 823
824 824 if (max < DTRACE_QUANTIZE_NBUCKETS - 1)
825 825 max++;
826 826
827 827 minval = DTRACE_QUANTIZE_BUCKETVAL(min);
828 828 maxval = DTRACE_QUANTIZE_BUCKETVAL(max);
829 829
830 830 if (dt_printf(dtp, fp, " %*lld :", dt_ndigits(minval),
831 831 (long long)minval) < 0)
832 832 return (-1);
833 833
834 834 for (i = min; i <= max; i++) {
835 835 dt_quantize_total(dtp, data[i], &total);
836 836 count += data[i];
837 837 }
838 838
839 839 for (i = min; i <= max; i++) {
840 840 if (dt_print_packed(dtp, fp, data[i], total) < 0)
841 841 return (-1);
842 842 }
843 843
844 844 if (dt_printf(dtp, fp, ": %*lld | %lld\n",
845 845 -dt_ndigits(maxval), (long long)maxval, (long long)count) < 0)
846 846 return (-1);
847 847
848 848 return (0);
849 849 }
850 850
851 851 int
852 852 dt_print_lquantize(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
853 853 size_t size, uint64_t normal)
854 854 {
855 855 const int64_t *data = addr;
856 856 int i, first_bin, last_bin, base;
857 857 uint64_t arg;
858 858 long double total = 0;
859 859 uint16_t step, levels;
860 860 char positives = 0, negatives = 0;
861 861
862 862 if (size < sizeof (uint64_t))
863 863 return (dt_set_errno(dtp, EDT_DMISMATCH));
864 864
865 865 arg = *data++;
866 866 size -= sizeof (uint64_t);
867 867
868 868 base = DTRACE_LQUANTIZE_BASE(arg);
869 869 step = DTRACE_LQUANTIZE_STEP(arg);
870 870 levels = DTRACE_LQUANTIZE_LEVELS(arg);
871 871
872 872 first_bin = 0;
873 873 last_bin = levels + 1;
874 874
875 875 if (size != sizeof (uint64_t) * (levels + 2))
876 876 return (dt_set_errno(dtp, EDT_DMISMATCH));
877 877
878 878 while (first_bin <= levels + 1 && data[first_bin] == 0)
879 879 first_bin++;
880 880
881 881 if (first_bin > levels + 1) {
882 882 first_bin = 0;
883 883 last_bin = 2;
884 884 } else {
885 885 if (first_bin > 0)
886 886 first_bin--;
887 887
888 888 while (last_bin > 0 && data[last_bin] == 0)
889 889 last_bin--;
890 890
891 891 if (last_bin < levels + 1)
892 892 last_bin++;
893 893 }
894 894
895 895 for (i = first_bin; i <= last_bin; i++) {
896 896 positives |= (data[i] > 0);
897 897 negatives |= (data[i] < 0);
898 898 dt_quantize_total(dtp, data[i], &total);
899 899 }
900 900
901 901 if (dt_printf(dtp, fp, "\n%16s %41s %-9s\n", "value",
902 902 "------------- Distribution -------------", "count") < 0)
903 903 return (-1);
904 904
905 905 for (i = first_bin; i <= last_bin; i++) {
906 906 char c[32];
907 907 int err;
908 908
909 909 if (i == 0) {
910 910 (void) snprintf(c, sizeof (c), "< %d", base);
911 911 err = dt_printf(dtp, fp, "%16s ", c);
912 912 } else if (i == levels + 1) {
913 913 (void) snprintf(c, sizeof (c), ">= %d",
914 914 base + (levels * step));
915 915 err = dt_printf(dtp, fp, "%16s ", c);
916 916 } else {
917 917 err = dt_printf(dtp, fp, "%16d ",
918 918 base + (i - 1) * step);
919 919 }
920 920
921 921 if (err < 0 || dt_print_quantline(dtp, fp, data[i], normal,
922 922 total, positives, negatives) < 0)
923 923 return (-1);
924 924 }
925 925
926 926 return (0);
927 927 }
928 928
929 929 /*ARGSUSED*/
930 930 int
931 931 dt_print_lquantize_packed(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
932 932 size_t size, const dtrace_aggdata_t *aggdata)
933 933 {
934 934 const int64_t *data = addr;
935 935 long double total = 0, count = 0;
936 936 int min, max, base, err;
937 937 uint64_t arg;
938 938 uint16_t step, levels;
939 939 char c[32];
940 940 unsigned int i;
941 941
942 942 if (size < sizeof (uint64_t))
943 943 return (dt_set_errno(dtp, EDT_DMISMATCH));
944 944
945 945 arg = *data++;
946 946 size -= sizeof (uint64_t);
947 947
948 948 base = DTRACE_LQUANTIZE_BASE(arg);
949 949 step = DTRACE_LQUANTIZE_STEP(arg);
950 950 levels = DTRACE_LQUANTIZE_LEVELS(arg);
951 951
952 952 if (size != sizeof (uint64_t) * (levels + 2))
953 953 return (dt_set_errno(dtp, EDT_DMISMATCH));
954 954
955 955 min = 0;
956 956 max = levels + 1;
957 957
958 958 if (min == 0) {
959 959 (void) snprintf(c, sizeof (c), "< %d", base);
960 960 err = dt_printf(dtp, fp, "%8s :", c);
961 961 } else {
962 962 err = dt_printf(dtp, fp, "%8d :", base + (min - 1) * step);
963 963 }
964 964
965 965 if (err < 0)
966 966 return (-1);
967 967
968 968 for (i = min; i <= max; i++) {
969 969 dt_quantize_total(dtp, data[i], &total);
970 970 count += data[i];
971 971 }
972 972
973 973 for (i = min; i <= max; i++) {
974 974 if (dt_print_packed(dtp, fp, data[i], total) < 0)
975 975 return (-1);
976 976 }
977 977
978 978 (void) snprintf(c, sizeof (c), ">= %d", base + (levels * step));
979 979 return (dt_printf(dtp, fp, ": %-8s | %lld\n", c, (long long)count));
980 980 }
981 981
982 982 int
983 983 dt_print_llquantize(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
984 984 size_t size, uint64_t normal)
985 985 {
986 986 int i, first_bin, last_bin, bin = 1, order, levels;
987 987 uint16_t factor, low, high, nsteps;
988 988 const int64_t *data = addr;
989 989 int64_t value = 1, next, step;
990 990 char positives = 0, negatives = 0;
991 991 long double total = 0;
992 992 uint64_t arg;
993 993 char c[32];
994 994
995 995 if (size < sizeof (uint64_t))
996 996 return (dt_set_errno(dtp, EDT_DMISMATCH));
997 997
998 998 arg = *data++;
999 999 size -= sizeof (uint64_t);
1000 1000
1001 1001 factor = DTRACE_LLQUANTIZE_FACTOR(arg);
1002 1002 low = DTRACE_LLQUANTIZE_LOW(arg);
1003 1003 high = DTRACE_LLQUANTIZE_HIGH(arg);
1004 1004 nsteps = DTRACE_LLQUANTIZE_NSTEP(arg);
1005 1005
1006 1006 /*
1007 1007 * We don't expect to be handed invalid llquantize() parameters here,
1008 1008 * but sanity check them (to a degree) nonetheless.
1009 1009 */
1010 1010 if (size > INT32_MAX || factor < 2 || low >= high ||
1011 1011 nsteps == 0 || factor > nsteps)
1012 1012 return (dt_set_errno(dtp, EDT_DMISMATCH));
1013 1013
1014 1014 levels = (int)size / sizeof (uint64_t);
1015 1015
1016 1016 first_bin = 0;
1017 1017 last_bin = levels - 1;
1018 1018
1019 1019 while (first_bin < levels && data[first_bin] == 0)
1020 1020 first_bin++;
1021 1021
1022 1022 if (first_bin == levels) {
1023 1023 first_bin = 0;
1024 1024 last_bin = 1;
1025 1025 } else {
1026 1026 if (first_bin > 0)
1027 1027 first_bin--;
1028 1028
1029 1029 while (last_bin > 0 && data[last_bin] == 0)
1030 1030 last_bin--;
1031 1031
1032 1032 if (last_bin < levels - 1)
1033 1033 last_bin++;
1034 1034 }
1035 1035
1036 1036 for (i = first_bin; i <= last_bin; i++) {
1037 1037 positives |= (data[i] > 0);
1038 1038 negatives |= (data[i] < 0);
1039 1039 dt_quantize_total(dtp, data[i], &total);
1040 1040 }
1041 1041
1042 1042 if (dt_printf(dtp, fp, "\n%16s %41s %-9s\n", "value",
1043 1043 "------------- Distribution -------------", "count") < 0)
1044 1044 return (-1);
1045 1045
1046 1046 for (order = 0; order < low; order++)
1047 1047 value *= factor;
1048 1048
1049 1049 next = value * factor;
1050 1050 step = next > nsteps ? next / nsteps : 1;
1051 1051
1052 1052 if (first_bin == 0) {
1053 1053 (void) snprintf(c, sizeof (c), "< %lld", value);
1054 1054
1055 1055 if (dt_printf(dtp, fp, "%16s ", c) < 0)
1056 1056 return (-1);
1057 1057
1058 1058 if (dt_print_quantline(dtp, fp, data[0], normal,
1059 1059 total, positives, negatives) < 0)
1060 1060 return (-1);
1061 1061 }
1062 1062
1063 1063 while (order <= high) {
1064 1064 if (bin >= first_bin && bin <= last_bin) {
1065 1065 if (dt_printf(dtp, fp, "%16lld ", (long long)value) < 0)
1066 1066 return (-1);
1067 1067
1068 1068 if (dt_print_quantline(dtp, fp, data[bin],
1069 1069 normal, total, positives, negatives) < 0)
1070 1070 return (-1);
1071 1071 }
1072 1072
1073 1073 assert(value < next);
1074 1074 bin++;
1075 1075
1076 1076 if ((value += step) != next)
1077 1077 continue;
1078 1078
1079 1079 next = value * factor;
1080 1080 step = next > nsteps ? next / nsteps : 1;
1081 1081 order++;
1082 1082 }
1083 1083
1084 1084 if (last_bin < bin)
1085 1085 return (0);
1086 1086
1087 1087 assert(last_bin == bin);
1088 1088 (void) snprintf(c, sizeof (c), ">= %lld", value);
1089 1089
1090 1090 if (dt_printf(dtp, fp, "%16s ", c) < 0)
1091 1091 return (-1);
1092 1092
1093 1093 return (dt_print_quantline(dtp, fp, data[bin], normal,
1094 1094 total, positives, negatives));
1095 1095 }
1096 1096
1097 1097 /*ARGSUSED*/
1098 1098 static int
1099 1099 dt_print_average(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr,
1100 1100 size_t size, uint64_t normal)
1101 1101 {
1102 1102 /* LINTED - alignment */
1103 1103 int64_t *data = (int64_t *)addr;
1104 1104
1105 1105 return (dt_printf(dtp, fp, " %16lld", data[0] ?
1106 1106 (long long)(data[1] / (int64_t)normal / data[0]) : 0));
1107 1107 }
1108 1108
1109 1109 /*ARGSUSED*/
1110 1110 static int
1111 1111 dt_print_stddev(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr,
1112 1112 size_t size, uint64_t normal)
1113 1113 {
1114 1114 /* LINTED - alignment */
1115 1115 uint64_t *data = (uint64_t *)addr;
1116 1116
1117 1117 return (dt_printf(dtp, fp, " %16llu", data[0] ?
1118 1118 (unsigned long long) dt_stddev(data, normal) : 0));
1119 1119 }
1120 1120
1121 1121 /*ARGSUSED*/
1122 1122 static int
1123 1123 dt_print_bytes(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr,
1124 1124 size_t nbytes, int width, int quiet, int forceraw)
1125 1125 {
1126 1126 /*
1127 1127 * If the byte stream is a series of printable characters, followed by
1128 1128 * a terminating byte, we print it out as a string. Otherwise, we
1129 1129 * assume that it's something else and just print the bytes.
1130 1130 */
1131 1131 int i, j, margin = 5;
1132 1132 char *c = (char *)addr;
1133 1133
1134 1134 if (nbytes == 0)
1135 1135 return (0);
1136 1136
1137 1137 if (forceraw)
1138 1138 goto raw;
1139 1139
1140 1140 if (dtp->dt_options[DTRACEOPT_RAWBYTES] != DTRACEOPT_UNSET)
1141 1141 goto raw;
1142 1142
1143 1143 for (i = 0; i < nbytes; i++) {
1144 1144 /*
1145 1145 * We define a "printable character" to be one for which
1146 1146 * isprint(3C) returns non-zero, isspace(3C) returns non-zero,
1147 1147 * or a character which is either backspace or the bell.
1148 1148 * Backspace and the bell are regrettably special because
1149 1149 * they fail the first two tests -- and yet they are entirely
1150 1150 * printable. These are the only two control characters that
1151 1151 * have meaning for the terminal and for which isprint(3C) and
1152 1152 * isspace(3C) return 0.
1153 1153 */
1154 1154 if (isprint(c[i]) || isspace(c[i]) ||
1155 1155 c[i] == '\b' || c[i] == '\a')
1156 1156 continue;
1157 1157
1158 1158 if (c[i] == '\0' && i > 0) {
1159 1159 /*
1160 1160 * This looks like it might be a string. Before we
1161 1161 * assume that it is indeed a string, check the
1162 1162 * remainder of the byte range; if it contains
1163 1163 * additional non-nul characters, we'll assume that
1164 1164 * it's a binary stream that just happens to look like
1165 1165 * a string, and we'll print out the individual bytes.
1166 1166 */
1167 1167 for (j = i + 1; j < nbytes; j++) {
1168 1168 if (c[j] != '\0')
1169 1169 break;
1170 1170 }
1171 1171
1172 1172 if (j != nbytes)
1173 1173 break;
1174 1174
1175 1175 if (quiet) {
1176 1176 return (dt_printf(dtp, fp, "%s", c));
1177 1177 } else {
1178 1178 return (dt_printf(dtp, fp, " %s%*s",
1179 1179 width < 0 ? " " : "", width, c));
1180 1180 }
1181 1181 }
1182 1182
1183 1183 break;
1184 1184 }
1185 1185
1186 1186 if (i == nbytes) {
1187 1187 /*
1188 1188 * The byte range is all printable characters, but there is
1189 1189 * no trailing nul byte. We'll assume that it's a string and
1190 1190 * print it as such.
1191 1191 */
1192 1192 char *s = alloca(nbytes + 1);
1193 1193 bcopy(c, s, nbytes);
1194 1194 s[nbytes] = '\0';
1195 1195 return (dt_printf(dtp, fp, " %-*s", width, s));
1196 1196 }
1197 1197
1198 1198 raw:
1199 1199 if (dt_printf(dtp, fp, "\n%*s ", margin, "") < 0)
1200 1200 return (-1);
1201 1201
1202 1202 for (i = 0; i < 16; i++)
1203 1203 if (dt_printf(dtp, fp, " %c", "0123456789abcdef"[i]) < 0)
1204 1204 return (-1);
1205 1205
1206 1206 if (dt_printf(dtp, fp, " 0123456789abcdef\n") < 0)
1207 1207 return (-1);
1208 1208
1209 1209
1210 1210 for (i = 0; i < nbytes; i += 16) {
1211 1211 if (dt_printf(dtp, fp, "%*s%5x:", margin, "", i) < 0)
1212 1212 return (-1);
1213 1213
1214 1214 for (j = i; j < i + 16 && j < nbytes; j++) {
1215 1215 if (dt_printf(dtp, fp, " %02x", (uchar_t)c[j]) < 0)
1216 1216 return (-1);
1217 1217 }
1218 1218
1219 1219 while (j++ % 16) {
1220 1220 if (dt_printf(dtp, fp, " ") < 0)
1221 1221 return (-1);
1222 1222 }
1223 1223
1224 1224 if (dt_printf(dtp, fp, " ") < 0)
1225 1225 return (-1);
1226 1226
1227 1227 for (j = i; j < i + 16 && j < nbytes; j++) {
1228 1228 if (dt_printf(dtp, fp, "%c",
1229 1229 c[j] < ' ' || c[j] > '~' ? '.' : c[j]) < 0)
1230 1230 return (-1);
1231 1231 }
1232 1232
1233 1233 if (dt_printf(dtp, fp, "\n") < 0)
1234 1234 return (-1);
1235 1235 }
1236 1236
1237 1237 return (0);
1238 1238 }
1239 1239
1240 1240 int
1241 1241 dt_print_stack(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1242 1242 caddr_t addr, int depth, int size)
1243 1243 {
1244 1244 dtrace_syminfo_t dts;
1245 1245 GElf_Sym sym;
1246 1246 int i, indent;
1247 1247 char c[PATH_MAX * 2];
1248 1248 uint64_t pc;
1249 1249
1250 1250 if (dt_printf(dtp, fp, "\n") < 0)
1251 1251 return (-1);
1252 1252
1253 1253 if (format == NULL)
1254 1254 format = "%s";
1255 1255
1256 1256 if (dtp->dt_options[DTRACEOPT_STACKINDENT] != DTRACEOPT_UNSET)
1257 1257 indent = (int)dtp->dt_options[DTRACEOPT_STACKINDENT];
1258 1258 else
1259 1259 indent = _dtrace_stkindent;
1260 1260
1261 1261 for (i = 0; i < depth; i++) {
1262 1262 switch (size) {
1263 1263 case sizeof (uint32_t):
1264 1264 /* LINTED - alignment */
1265 1265 pc = *((uint32_t *)addr);
1266 1266 break;
1267 1267
1268 1268 case sizeof (uint64_t):
1269 1269 /* LINTED - alignment */
1270 1270 pc = *((uint64_t *)addr);
1271 1271 break;
1272 1272
1273 1273 default:
1274 1274 return (dt_set_errno(dtp, EDT_BADSTACKPC));
1275 1275 }
1276 1276
1277 1277 if (pc == NULL)
1278 1278 break;
1279 1279
1280 1280 addr += size;
1281 1281
1282 1282 if (dt_printf(dtp, fp, "%*s", indent, "") < 0)
1283 1283 return (-1);
1284 1284
1285 1285 if (dtrace_lookup_by_addr(dtp, pc, &sym, &dts) == 0) {
1286 1286 if (pc > sym.st_value) {
1287 1287 (void) snprintf(c, sizeof (c), "%s`%s+0x%llx",
1288 1288 dts.dts_object, dts.dts_name,
1289 1289 pc - sym.st_value);
1290 1290 } else {
1291 1291 (void) snprintf(c, sizeof (c), "%s`%s",
1292 1292 dts.dts_object, dts.dts_name);
1293 1293 }
1294 1294 } else {
1295 1295 /*
1296 1296 * We'll repeat the lookup, but this time we'll specify
1297 1297 * a NULL GElf_Sym -- indicating that we're only
1298 1298 * interested in the containing module.
1299 1299 */
1300 1300 if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0) {
1301 1301 (void) snprintf(c, sizeof (c), "%s`0x%llx",
1302 1302 dts.dts_object, pc);
1303 1303 } else {
1304 1304 (void) snprintf(c, sizeof (c), "0x%llx", pc);
1305 1305 }
1306 1306 }
1307 1307
1308 1308 if (dt_printf(dtp, fp, format, c) < 0)
1309 1309 return (-1);
1310 1310
1311 1311 if (dt_printf(dtp, fp, "\n") < 0)
1312 1312 return (-1);
1313 1313 }
1314 1314
1315 1315 return (0);
1316 1316 }
1317 1317
1318 1318 int
1319 1319 dt_print_ustack(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1320 1320 caddr_t addr, uint64_t arg)
1321 1321 {
1322 1322 /* LINTED - alignment */
1323 1323 uint64_t *pc = (uint64_t *)addr;
1324 1324 uint32_t depth = DTRACE_USTACK_NFRAMES(arg);
1325 1325 uint32_t strsize = DTRACE_USTACK_STRSIZE(arg);
1326 1326 const char *strbase = addr + (depth + 1) * sizeof (uint64_t);
1327 1327 const char *str = strsize ? strbase : NULL;
1328 1328 int err = 0;
1329 1329
1330 1330 char name[PATH_MAX], objname[PATH_MAX], c[PATH_MAX * 2];
1331 1331 struct ps_prochandle *P;
1332 1332 GElf_Sym sym;
1333 1333 int i, indent;
1334 1334 pid_t pid;
1335 1335
1336 1336 if (depth == 0)
1337 1337 return (0);
1338 1338
1339 1339 pid = (pid_t)*pc++;
1340 1340
1341 1341 if (dt_printf(dtp, fp, "\n") < 0)
1342 1342 return (-1);
1343 1343
1344 1344 if (format == NULL)
1345 1345 format = "%s";
1346 1346
1347 1347 if (dtp->dt_options[DTRACEOPT_STACKINDENT] != DTRACEOPT_UNSET)
1348 1348 indent = (int)dtp->dt_options[DTRACEOPT_STACKINDENT];
1349 1349 else
1350 1350 indent = _dtrace_stkindent;
1351 1351
1352 1352 /*
1353 1353 * Ultimately, we need to add an entry point in the library vector for
1354 1354 * determining <symbol, offset> from <pid, address>. For now, if
1355 1355 * this is a vector open, we just print the raw address or string.
1356 1356 */
1357 1357 if (dtp->dt_vector == NULL)
1358 1358 P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0);
1359 1359 else
1360 1360 P = NULL;
1361 1361
1362 1362 if (P != NULL)
1363 1363 dt_proc_lock(dtp, P); /* lock handle while we perform lookups */
1364 1364
1365 1365 for (i = 0; i < depth && pc[i] != NULL; i++) {
1366 1366 const prmap_t *map;
1367 1367
1368 1368 if ((err = dt_printf(dtp, fp, "%*s", indent, "")) < 0)
1369 1369 break;
1370 1370
1371 1371 if (P != NULL && Plookup_by_addr(P, pc[i],
1372 1372 name, sizeof (name), &sym) == 0) {
1373 1373 (void) Pobjname(P, pc[i], objname, sizeof (objname));
↓ open down ↓ |
1373 lines elided |
↑ open up ↑ |
1374 1374
1375 1375 if (pc[i] > sym.st_value) {
1376 1376 (void) snprintf(c, sizeof (c),
1377 1377 "%s`%s+0x%llx", dt_basename(objname), name,
1378 1378 (u_longlong_t)(pc[i] - sym.st_value));
1379 1379 } else {
1380 1380 (void) snprintf(c, sizeof (c),
1381 1381 "%s`%s", dt_basename(objname), name);
1382 1382 }
1383 1383 } else if (str != NULL && str[0] != '\0' && str[0] != '@' &&
1384 - (P != NULL && ((map = Paddr_to_map(P, pc[i])) == NULL ||
1385 - (map->pr_mflags & MA_WRITE)))) {
1384 + (P == NULL || (map = Paddr_to_map(P, pc[i])) == NULL ||
1385 + map->pr_mflags & MA_WRITE)) {
1386 1386 /*
1387 1387 * If the current string pointer in the string table
1388 1388 * does not point to an empty string _and_ the program
1389 1389 * counter falls in a writable region, we'll use the
1390 1390 * string from the string table instead of the raw
1391 1391 * address. This last condition is necessary because
1392 1392 * some (broken) ustack helpers will return a string
1393 1393 * even for a program counter that they can't
1394 1394 * identify. If we have a string for a program
1395 1395 * counter that falls in a segment that isn't
1396 1396 * writable, we assume that we have fallen into this
1397 - * case and we refuse to use the string.
1397 + * case and we refuse to use the string. Finally,
1398 + * note that if we could not grab the process (e.g.,
1399 + * because it exited), the information from the helper
1400 + * is better than nothing.
1398 1401 */
1399 1402 (void) snprintf(c, sizeof (c), "%s", str);
1400 1403 } else {
1401 1404 if (P != NULL && Pobjname(P, pc[i], objname,
1402 1405 sizeof (objname)) != NULL) {
1403 1406 (void) snprintf(c, sizeof (c), "%s`0x%llx",
1404 1407 dt_basename(objname), (u_longlong_t)pc[i]);
1405 1408 } else {
1406 1409 (void) snprintf(c, sizeof (c), "0x%llx",
1407 1410 (u_longlong_t)pc[i]);
1408 1411 }
1409 1412 }
1410 1413
1411 1414 if ((err = dt_printf(dtp, fp, format, c)) < 0)
1412 1415 break;
1413 1416
1414 1417 if ((err = dt_printf(dtp, fp, "\n")) < 0)
1415 1418 break;
1416 1419
1417 1420 if (str != NULL && str[0] == '@') {
1418 1421 /*
1419 1422 * If the first character of the string is an "at" sign,
1420 1423 * then the string is inferred to be an annotation --
1421 1424 * and it is printed out beneath the frame and offset
1422 1425 * with brackets.
1423 1426 */
1424 1427 if ((err = dt_printf(dtp, fp, "%*s", indent, "")) < 0)
1425 1428 break;
1426 1429
1427 1430 (void) snprintf(c, sizeof (c), " [ %s ]", &str[1]);
1428 1431
1429 1432 if ((err = dt_printf(dtp, fp, format, c)) < 0)
1430 1433 break;
1431 1434
1432 1435 if ((err = dt_printf(dtp, fp, "\n")) < 0)
1433 1436 break;
1434 1437 }
1435 1438
1436 1439 if (str != NULL) {
1437 1440 str += strlen(str) + 1;
1438 1441 if (str - strbase >= strsize)
1439 1442 str = NULL;
1440 1443 }
1441 1444 }
1442 1445
1443 1446 if (P != NULL) {
1444 1447 dt_proc_unlock(dtp, P);
1445 1448 dt_proc_release(dtp, P);
1446 1449 }
1447 1450
1448 1451 return (err);
1449 1452 }
1450 1453
1451 1454 static int
1452 1455 dt_print_usym(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr, dtrace_actkind_t act)
1453 1456 {
1454 1457 /* LINTED - alignment */
1455 1458 uint64_t pid = ((uint64_t *)addr)[0];
1456 1459 /* LINTED - alignment */
1457 1460 uint64_t pc = ((uint64_t *)addr)[1];
1458 1461 const char *format = " %-50s";
1459 1462 char *s;
1460 1463 int n, len = 256;
1461 1464
1462 1465 if (act == DTRACEACT_USYM && dtp->dt_vector == NULL) {
1463 1466 struct ps_prochandle *P;
1464 1467
1465 1468 if ((P = dt_proc_grab(dtp, pid,
1466 1469 PGRAB_RDONLY | PGRAB_FORCE, 0)) != NULL) {
1467 1470 GElf_Sym sym;
1468 1471
1469 1472 dt_proc_lock(dtp, P);
1470 1473
1471 1474 if (Plookup_by_addr(P, pc, NULL, 0, &sym) == 0)
1472 1475 pc = sym.st_value;
1473 1476
1474 1477 dt_proc_unlock(dtp, P);
1475 1478 dt_proc_release(dtp, P);
1476 1479 }
1477 1480 }
1478 1481
1479 1482 do {
1480 1483 n = len;
1481 1484 s = alloca(n);
1482 1485 } while ((len = dtrace_uaddr2str(dtp, pid, pc, s, n)) > n);
1483 1486
1484 1487 return (dt_printf(dtp, fp, format, s));
1485 1488 }
1486 1489
1487 1490 int
1488 1491 dt_print_umod(dtrace_hdl_t *dtp, FILE *fp, const char *format, caddr_t addr)
1489 1492 {
1490 1493 /* LINTED - alignment */
1491 1494 uint64_t pid = ((uint64_t *)addr)[0];
1492 1495 /* LINTED - alignment */
1493 1496 uint64_t pc = ((uint64_t *)addr)[1];
1494 1497 int err = 0;
1495 1498
1496 1499 char objname[PATH_MAX], c[PATH_MAX * 2];
1497 1500 struct ps_prochandle *P;
1498 1501
1499 1502 if (format == NULL)
1500 1503 format = " %-50s";
1501 1504
1502 1505 /*
1503 1506 * See the comment in dt_print_ustack() for the rationale for
1504 1507 * printing raw addresses in the vectored case.
1505 1508 */
1506 1509 if (dtp->dt_vector == NULL)
1507 1510 P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0);
1508 1511 else
1509 1512 P = NULL;
1510 1513
1511 1514 if (P != NULL)
1512 1515 dt_proc_lock(dtp, P); /* lock handle while we perform lookups */
1513 1516
1514 1517 if (P != NULL && Pobjname(P, pc, objname, sizeof (objname)) != NULL) {
1515 1518 (void) snprintf(c, sizeof (c), "%s", dt_basename(objname));
1516 1519 } else {
1517 1520 (void) snprintf(c, sizeof (c), "0x%llx", (u_longlong_t)pc);
1518 1521 }
1519 1522
1520 1523 err = dt_printf(dtp, fp, format, c);
1521 1524
1522 1525 if (P != NULL) {
1523 1526 dt_proc_unlock(dtp, P);
1524 1527 dt_proc_release(dtp, P);
1525 1528 }
1526 1529
1527 1530 return (err);
1528 1531 }
1529 1532
1530 1533 static int
1531 1534 dt_print_sym(dtrace_hdl_t *dtp, FILE *fp, const char *format, caddr_t addr)
1532 1535 {
1533 1536 /* LINTED - alignment */
1534 1537 uint64_t pc = *((uint64_t *)addr);
1535 1538 dtrace_syminfo_t dts;
1536 1539 GElf_Sym sym;
1537 1540 char c[PATH_MAX * 2];
1538 1541
1539 1542 if (format == NULL)
1540 1543 format = " %-50s";
1541 1544
1542 1545 if (dtrace_lookup_by_addr(dtp, pc, &sym, &dts) == 0) {
1543 1546 (void) snprintf(c, sizeof (c), "%s`%s",
1544 1547 dts.dts_object, dts.dts_name);
1545 1548 } else {
1546 1549 /*
1547 1550 * We'll repeat the lookup, but this time we'll specify a
1548 1551 * NULL GElf_Sym -- indicating that we're only interested in
1549 1552 * the containing module.
1550 1553 */
1551 1554 if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0) {
1552 1555 (void) snprintf(c, sizeof (c), "%s`0x%llx",
1553 1556 dts.dts_object, (u_longlong_t)pc);
1554 1557 } else {
1555 1558 (void) snprintf(c, sizeof (c), "0x%llx",
1556 1559 (u_longlong_t)pc);
1557 1560 }
1558 1561 }
1559 1562
1560 1563 if (dt_printf(dtp, fp, format, c) < 0)
1561 1564 return (-1);
1562 1565
1563 1566 return (0);
1564 1567 }
1565 1568
1566 1569 int
1567 1570 dt_print_mod(dtrace_hdl_t *dtp, FILE *fp, const char *format, caddr_t addr)
1568 1571 {
1569 1572 /* LINTED - alignment */
1570 1573 uint64_t pc = *((uint64_t *)addr);
1571 1574 dtrace_syminfo_t dts;
1572 1575 char c[PATH_MAX * 2];
1573 1576
1574 1577 if (format == NULL)
1575 1578 format = " %-50s";
1576 1579
1577 1580 if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0) {
1578 1581 (void) snprintf(c, sizeof (c), "%s", dts.dts_object);
1579 1582 } else {
1580 1583 (void) snprintf(c, sizeof (c), "0x%llx", (u_longlong_t)pc);
1581 1584 }
1582 1585
1583 1586 if (dt_printf(dtp, fp, format, c) < 0)
1584 1587 return (-1);
1585 1588
1586 1589 return (0);
1587 1590 }
1588 1591
1589 1592 typedef struct dt_normal {
1590 1593 dtrace_aggvarid_t dtnd_id;
1591 1594 uint64_t dtnd_normal;
1592 1595 } dt_normal_t;
1593 1596
1594 1597 static int
1595 1598 dt_normalize_agg(const dtrace_aggdata_t *aggdata, void *arg)
1596 1599 {
1597 1600 dt_normal_t *normal = arg;
1598 1601 dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1599 1602 dtrace_aggvarid_t id = normal->dtnd_id;
1600 1603
1601 1604 if (agg->dtagd_nrecs == 0)
1602 1605 return (DTRACE_AGGWALK_NEXT);
1603 1606
1604 1607 if (agg->dtagd_varid != id)
1605 1608 return (DTRACE_AGGWALK_NEXT);
1606 1609
1607 1610 ((dtrace_aggdata_t *)aggdata)->dtada_normal = normal->dtnd_normal;
1608 1611 return (DTRACE_AGGWALK_NORMALIZE);
1609 1612 }
1610 1613
1611 1614 static int
1612 1615 dt_normalize(dtrace_hdl_t *dtp, caddr_t base, dtrace_recdesc_t *rec)
1613 1616 {
1614 1617 dt_normal_t normal;
1615 1618 caddr_t addr;
1616 1619
1617 1620 /*
1618 1621 * We (should) have two records: the aggregation ID followed by the
1619 1622 * normalization value.
1620 1623 */
1621 1624 addr = base + rec->dtrd_offset;
1622 1625
1623 1626 if (rec->dtrd_size != sizeof (dtrace_aggvarid_t))
1624 1627 return (dt_set_errno(dtp, EDT_BADNORMAL));
1625 1628
1626 1629 /* LINTED - alignment */
1627 1630 normal.dtnd_id = *((dtrace_aggvarid_t *)addr);
1628 1631 rec++;
1629 1632
1630 1633 if (rec->dtrd_action != DTRACEACT_LIBACT)
1631 1634 return (dt_set_errno(dtp, EDT_BADNORMAL));
1632 1635
1633 1636 if (rec->dtrd_arg != DT_ACT_NORMALIZE)
1634 1637 return (dt_set_errno(dtp, EDT_BADNORMAL));
1635 1638
1636 1639 addr = base + rec->dtrd_offset;
1637 1640
1638 1641 switch (rec->dtrd_size) {
1639 1642 case sizeof (uint64_t):
1640 1643 /* LINTED - alignment */
1641 1644 normal.dtnd_normal = *((uint64_t *)addr);
1642 1645 break;
1643 1646 case sizeof (uint32_t):
1644 1647 /* LINTED - alignment */
1645 1648 normal.dtnd_normal = *((uint32_t *)addr);
1646 1649 break;
1647 1650 case sizeof (uint16_t):
1648 1651 /* LINTED - alignment */
1649 1652 normal.dtnd_normal = *((uint16_t *)addr);
1650 1653 break;
1651 1654 case sizeof (uint8_t):
1652 1655 normal.dtnd_normal = *((uint8_t *)addr);
1653 1656 break;
1654 1657 default:
1655 1658 return (dt_set_errno(dtp, EDT_BADNORMAL));
1656 1659 }
1657 1660
1658 1661 (void) dtrace_aggregate_walk(dtp, dt_normalize_agg, &normal);
1659 1662
1660 1663 return (0);
1661 1664 }
1662 1665
1663 1666 static int
1664 1667 dt_denormalize_agg(const dtrace_aggdata_t *aggdata, void *arg)
1665 1668 {
1666 1669 dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1667 1670 dtrace_aggvarid_t id = *((dtrace_aggvarid_t *)arg);
1668 1671
1669 1672 if (agg->dtagd_nrecs == 0)
1670 1673 return (DTRACE_AGGWALK_NEXT);
1671 1674
1672 1675 if (agg->dtagd_varid != id)
1673 1676 return (DTRACE_AGGWALK_NEXT);
1674 1677
1675 1678 return (DTRACE_AGGWALK_DENORMALIZE);
1676 1679 }
1677 1680
1678 1681 static int
1679 1682 dt_clear_agg(const dtrace_aggdata_t *aggdata, void *arg)
1680 1683 {
1681 1684 dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1682 1685 dtrace_aggvarid_t id = *((dtrace_aggvarid_t *)arg);
1683 1686
1684 1687 if (agg->dtagd_nrecs == 0)
1685 1688 return (DTRACE_AGGWALK_NEXT);
1686 1689
1687 1690 if (agg->dtagd_varid != id)
1688 1691 return (DTRACE_AGGWALK_NEXT);
1689 1692
1690 1693 return (DTRACE_AGGWALK_CLEAR);
1691 1694 }
1692 1695
1693 1696 typedef struct dt_trunc {
1694 1697 dtrace_aggvarid_t dttd_id;
1695 1698 uint64_t dttd_remaining;
1696 1699 } dt_trunc_t;
1697 1700
1698 1701 static int
1699 1702 dt_trunc_agg(const dtrace_aggdata_t *aggdata, void *arg)
1700 1703 {
1701 1704 dt_trunc_t *trunc = arg;
1702 1705 dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1703 1706 dtrace_aggvarid_t id = trunc->dttd_id;
1704 1707
1705 1708 if (agg->dtagd_nrecs == 0)
1706 1709 return (DTRACE_AGGWALK_NEXT);
1707 1710
1708 1711 if (agg->dtagd_varid != id)
1709 1712 return (DTRACE_AGGWALK_NEXT);
1710 1713
1711 1714 if (trunc->dttd_remaining == 0)
1712 1715 return (DTRACE_AGGWALK_REMOVE);
1713 1716
1714 1717 trunc->dttd_remaining--;
1715 1718 return (DTRACE_AGGWALK_NEXT);
1716 1719 }
1717 1720
1718 1721 static int
1719 1722 dt_trunc(dtrace_hdl_t *dtp, caddr_t base, dtrace_recdesc_t *rec)
1720 1723 {
1721 1724 dt_trunc_t trunc;
1722 1725 caddr_t addr;
1723 1726 int64_t remaining;
1724 1727 int (*func)(dtrace_hdl_t *, dtrace_aggregate_f *, void *);
1725 1728
1726 1729 /*
1727 1730 * We (should) have two records: the aggregation ID followed by the
1728 1731 * number of aggregation entries after which the aggregation is to be
1729 1732 * truncated.
1730 1733 */
1731 1734 addr = base + rec->dtrd_offset;
1732 1735
1733 1736 if (rec->dtrd_size != sizeof (dtrace_aggvarid_t))
1734 1737 return (dt_set_errno(dtp, EDT_BADTRUNC));
1735 1738
1736 1739 /* LINTED - alignment */
1737 1740 trunc.dttd_id = *((dtrace_aggvarid_t *)addr);
1738 1741 rec++;
1739 1742
1740 1743 if (rec->dtrd_action != DTRACEACT_LIBACT)
1741 1744 return (dt_set_errno(dtp, EDT_BADTRUNC));
1742 1745
1743 1746 if (rec->dtrd_arg != DT_ACT_TRUNC)
1744 1747 return (dt_set_errno(dtp, EDT_BADTRUNC));
1745 1748
1746 1749 addr = base + rec->dtrd_offset;
1747 1750
1748 1751 switch (rec->dtrd_size) {
1749 1752 case sizeof (uint64_t):
1750 1753 /* LINTED - alignment */
1751 1754 remaining = *((int64_t *)addr);
1752 1755 break;
1753 1756 case sizeof (uint32_t):
1754 1757 /* LINTED - alignment */
1755 1758 remaining = *((int32_t *)addr);
1756 1759 break;
1757 1760 case sizeof (uint16_t):
1758 1761 /* LINTED - alignment */
1759 1762 remaining = *((int16_t *)addr);
1760 1763 break;
1761 1764 case sizeof (uint8_t):
1762 1765 remaining = *((int8_t *)addr);
1763 1766 break;
1764 1767 default:
1765 1768 return (dt_set_errno(dtp, EDT_BADNORMAL));
1766 1769 }
1767 1770
1768 1771 if (remaining < 0) {
1769 1772 func = dtrace_aggregate_walk_valsorted;
1770 1773 remaining = -remaining;
1771 1774 } else {
1772 1775 func = dtrace_aggregate_walk_valrevsorted;
1773 1776 }
1774 1777
1775 1778 assert(remaining >= 0);
1776 1779 trunc.dttd_remaining = remaining;
1777 1780
1778 1781 (void) func(dtp, dt_trunc_agg, &trunc);
1779 1782
1780 1783 return (0);
1781 1784 }
1782 1785
1783 1786 static int
1784 1787 dt_print_datum(dtrace_hdl_t *dtp, FILE *fp, dtrace_recdesc_t *rec,
1785 1788 caddr_t addr, size_t size, const dtrace_aggdata_t *aggdata,
1786 1789 uint64_t normal, dt_print_aggdata_t *pd)
1787 1790 {
1788 1791 int err, width;
1789 1792 dtrace_actkind_t act = rec->dtrd_action;
1790 1793 boolean_t packed = pd->dtpa_agghist || pd->dtpa_aggpack;
1791 1794 dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1792 1795
1793 1796 static struct {
1794 1797 size_t size;
1795 1798 int width;
1796 1799 int packedwidth;
1797 1800 } *fmt, fmttab[] = {
1798 1801 { sizeof (uint8_t), 3, 3 },
1799 1802 { sizeof (uint16_t), 5, 5 },
1800 1803 { sizeof (uint32_t), 8, 8 },
1801 1804 { sizeof (uint64_t), 16, 16 },
1802 1805 { 0, -50, 16 }
1803 1806 };
1804 1807
1805 1808 if (packed && pd->dtpa_agghisthdr != agg->dtagd_varid) {
1806 1809 dtrace_recdesc_t *r;
1807 1810
1808 1811 width = 0;
1809 1812
1810 1813 /*
1811 1814 * To print our quantization header for either an agghist or
1812 1815 * aggpack aggregation, we need to iterate through all of our
1813 1816 * of our records to determine their width.
1814 1817 */
1815 1818 for (r = rec; !DTRACEACT_ISAGG(r->dtrd_action); r++) {
1816 1819 for (fmt = fmttab; fmt->size &&
1817 1820 fmt->size != r->dtrd_size; fmt++)
1818 1821 continue;
1819 1822
1820 1823 width += fmt->packedwidth + 1;
1821 1824 }
1822 1825
1823 1826 if (pd->dtpa_agghist) {
1824 1827 if (dt_print_quanthdr(dtp, fp, width) < 0)
1825 1828 return (-1);
1826 1829 } else {
1827 1830 if (dt_print_quanthdr_packed(dtp, fp,
1828 1831 width, aggdata, r->dtrd_action) < 0)
1829 1832 return (-1);
1830 1833 }
1831 1834
1832 1835 pd->dtpa_agghisthdr = agg->dtagd_varid;
1833 1836 }
1834 1837
1835 1838 if (pd->dtpa_agghist && DTRACEACT_ISAGG(act)) {
1836 1839 char positives = aggdata->dtada_flags & DTRACE_A_HASPOSITIVES;
1837 1840 char negatives = aggdata->dtada_flags & DTRACE_A_HASNEGATIVES;
1838 1841 int64_t val;
1839 1842
1840 1843 assert(act == DTRACEAGG_SUM || act == DTRACEAGG_COUNT);
1841 1844 val = (long long)*((uint64_t *)addr);
1842 1845
1843 1846 if (dt_printf(dtp, fp, " ") < 0)
1844 1847 return (-1);
1845 1848
1846 1849 return (dt_print_quantline(dtp, fp, val, normal,
1847 1850 aggdata->dtada_total, positives, negatives));
1848 1851 }
1849 1852
1850 1853 if (pd->dtpa_aggpack && DTRACEACT_ISAGG(act)) {
1851 1854 switch (act) {
1852 1855 case DTRACEAGG_QUANTIZE:
1853 1856 return (dt_print_quantize_packed(dtp,
1854 1857 fp, addr, size, aggdata));
1855 1858 case DTRACEAGG_LQUANTIZE:
1856 1859 return (dt_print_lquantize_packed(dtp,
1857 1860 fp, addr, size, aggdata));
1858 1861 default:
1859 1862 break;
1860 1863 }
1861 1864 }
1862 1865
1863 1866 switch (act) {
1864 1867 case DTRACEACT_STACK:
1865 1868 return (dt_print_stack(dtp, fp, NULL, addr,
1866 1869 rec->dtrd_arg, rec->dtrd_size / rec->dtrd_arg));
1867 1870
1868 1871 case DTRACEACT_USTACK:
1869 1872 case DTRACEACT_JSTACK:
1870 1873 return (dt_print_ustack(dtp, fp, NULL, addr, rec->dtrd_arg));
1871 1874
1872 1875 case DTRACEACT_USYM:
1873 1876 case DTRACEACT_UADDR:
1874 1877 return (dt_print_usym(dtp, fp, addr, act));
1875 1878
1876 1879 case DTRACEACT_UMOD:
1877 1880 return (dt_print_umod(dtp, fp, NULL, addr));
1878 1881
1879 1882 case DTRACEACT_SYM:
1880 1883 return (dt_print_sym(dtp, fp, NULL, addr));
1881 1884
1882 1885 case DTRACEACT_MOD:
1883 1886 return (dt_print_mod(dtp, fp, NULL, addr));
1884 1887
1885 1888 case DTRACEAGG_QUANTIZE:
1886 1889 return (dt_print_quantize(dtp, fp, addr, size, normal));
1887 1890
1888 1891 case DTRACEAGG_LQUANTIZE:
1889 1892 return (dt_print_lquantize(dtp, fp, addr, size, normal));
1890 1893
1891 1894 case DTRACEAGG_LLQUANTIZE:
1892 1895 return (dt_print_llquantize(dtp, fp, addr, size, normal));
1893 1896
1894 1897 case DTRACEAGG_AVG:
1895 1898 return (dt_print_average(dtp, fp, addr, size, normal));
1896 1899
1897 1900 case DTRACEAGG_STDDEV:
1898 1901 return (dt_print_stddev(dtp, fp, addr, size, normal));
1899 1902
1900 1903 default:
1901 1904 break;
1902 1905 }
1903 1906
1904 1907 for (fmt = fmttab; fmt->size && fmt->size != size; fmt++)
1905 1908 continue;
1906 1909
1907 1910 width = packed ? fmt->packedwidth : fmt->width;
1908 1911
1909 1912 switch (size) {
1910 1913 case sizeof (uint64_t):
1911 1914 err = dt_printf(dtp, fp, " %*lld", width,
1912 1915 /* LINTED - alignment */
1913 1916 (long long)*((uint64_t *)addr) / normal);
1914 1917 break;
1915 1918 case sizeof (uint32_t):
1916 1919 /* LINTED - alignment */
1917 1920 err = dt_printf(dtp, fp, " %*d", width, *((uint32_t *)addr) /
1918 1921 (uint32_t)normal);
1919 1922 break;
1920 1923 case sizeof (uint16_t):
1921 1924 /* LINTED - alignment */
1922 1925 err = dt_printf(dtp, fp, " %*d", width, *((uint16_t *)addr) /
1923 1926 (uint32_t)normal);
1924 1927 break;
1925 1928 case sizeof (uint8_t):
1926 1929 err = dt_printf(dtp, fp, " %*d", width, *((uint8_t *)addr) /
1927 1930 (uint32_t)normal);
1928 1931 break;
1929 1932 default:
1930 1933 err = dt_print_bytes(dtp, fp, addr, size, width, 0, 0);
1931 1934 break;
1932 1935 }
1933 1936
1934 1937 return (err);
1935 1938 }
1936 1939
1937 1940 int
1938 1941 dt_print_aggs(const dtrace_aggdata_t **aggsdata, int naggvars, void *arg)
1939 1942 {
1940 1943 int i, aggact = 0;
1941 1944 dt_print_aggdata_t *pd = arg;
1942 1945 const dtrace_aggdata_t *aggdata = aggsdata[0];
1943 1946 dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1944 1947 FILE *fp = pd->dtpa_fp;
1945 1948 dtrace_hdl_t *dtp = pd->dtpa_dtp;
1946 1949 dtrace_recdesc_t *rec;
1947 1950 dtrace_actkind_t act;
1948 1951 caddr_t addr;
1949 1952 size_t size;
1950 1953
1951 1954 pd->dtpa_agghist = (aggdata->dtada_flags & DTRACE_A_TOTAL);
1952 1955 pd->dtpa_aggpack = (aggdata->dtada_flags & DTRACE_A_MINMAXBIN);
1953 1956
1954 1957 /*
1955 1958 * Iterate over each record description in the key, printing the traced
1956 1959 * data, skipping the first datum (the tuple member created by the
1957 1960 * compiler).
1958 1961 */
1959 1962 for (i = 1; i < agg->dtagd_nrecs; i++) {
1960 1963 rec = &agg->dtagd_rec[i];
1961 1964 act = rec->dtrd_action;
1962 1965 addr = aggdata->dtada_data + rec->dtrd_offset;
1963 1966 size = rec->dtrd_size;
1964 1967
1965 1968 if (DTRACEACT_ISAGG(act)) {
1966 1969 aggact = i;
1967 1970 break;
1968 1971 }
1969 1972
1970 1973 if (dt_print_datum(dtp, fp, rec, addr,
1971 1974 size, aggdata, 1, pd) < 0)
1972 1975 return (-1);
1973 1976
1974 1977 if (dt_buffered_flush(dtp, NULL, rec, aggdata,
1975 1978 DTRACE_BUFDATA_AGGKEY) < 0)
1976 1979 return (-1);
1977 1980 }
1978 1981
1979 1982 assert(aggact != 0);
1980 1983
1981 1984 for (i = (naggvars == 1 ? 0 : 1); i < naggvars; i++) {
1982 1985 uint64_t normal;
1983 1986
1984 1987 aggdata = aggsdata[i];
1985 1988 agg = aggdata->dtada_desc;
1986 1989 rec = &agg->dtagd_rec[aggact];
1987 1990 act = rec->dtrd_action;
1988 1991 addr = aggdata->dtada_data + rec->dtrd_offset;
1989 1992 size = rec->dtrd_size;
1990 1993
1991 1994 assert(DTRACEACT_ISAGG(act));
1992 1995 normal = aggdata->dtada_normal;
1993 1996
1994 1997 if (dt_print_datum(dtp, fp, rec, addr,
1995 1998 size, aggdata, normal, pd) < 0)
1996 1999 return (-1);
1997 2000
1998 2001 if (dt_buffered_flush(dtp, NULL, rec, aggdata,
1999 2002 DTRACE_BUFDATA_AGGVAL) < 0)
2000 2003 return (-1);
2001 2004
2002 2005 if (!pd->dtpa_allunprint)
2003 2006 agg->dtagd_flags |= DTRACE_AGD_PRINTED;
2004 2007 }
2005 2008
2006 2009 if (!pd->dtpa_agghist && !pd->dtpa_aggpack) {
2007 2010 if (dt_printf(dtp, fp, "\n") < 0)
2008 2011 return (-1);
2009 2012 }
2010 2013
2011 2014 if (dt_buffered_flush(dtp, NULL, NULL, aggdata,
2012 2015 DTRACE_BUFDATA_AGGFORMAT | DTRACE_BUFDATA_AGGLAST) < 0)
2013 2016 return (-1);
2014 2017
2015 2018 return (0);
2016 2019 }
2017 2020
2018 2021 int
2019 2022 dt_print_agg(const dtrace_aggdata_t *aggdata, void *arg)
2020 2023 {
2021 2024 dt_print_aggdata_t *pd = arg;
2022 2025 dtrace_aggdesc_t *agg = aggdata->dtada_desc;
2023 2026 dtrace_aggvarid_t aggvarid = pd->dtpa_id;
2024 2027
2025 2028 if (pd->dtpa_allunprint) {
2026 2029 if (agg->dtagd_flags & DTRACE_AGD_PRINTED)
2027 2030 return (0);
2028 2031 } else {
2029 2032 /*
2030 2033 * If we're not printing all unprinted aggregations, then the
2031 2034 * aggregation variable ID denotes a specific aggregation
2032 2035 * variable that we should print -- skip any other aggregations
2033 2036 * that we encounter.
2034 2037 */
2035 2038 if (agg->dtagd_nrecs == 0)
2036 2039 return (0);
2037 2040
2038 2041 if (aggvarid != agg->dtagd_varid)
2039 2042 return (0);
2040 2043 }
2041 2044
2042 2045 return (dt_print_aggs(&aggdata, 1, arg));
2043 2046 }
2044 2047
2045 2048 int
2046 2049 dt_setopt(dtrace_hdl_t *dtp, const dtrace_probedata_t *data,
2047 2050 const char *option, const char *value)
2048 2051 {
2049 2052 int len, rval;
2050 2053 char *msg;
2051 2054 const char *errstr;
2052 2055 dtrace_setoptdata_t optdata;
2053 2056
2054 2057 bzero(&optdata, sizeof (optdata));
2055 2058 (void) dtrace_getopt(dtp, option, &optdata.dtsda_oldval);
2056 2059
2057 2060 if (dtrace_setopt(dtp, option, value) == 0) {
2058 2061 (void) dtrace_getopt(dtp, option, &optdata.dtsda_newval);
2059 2062 optdata.dtsda_probe = data;
2060 2063 optdata.dtsda_option = option;
2061 2064 optdata.dtsda_handle = dtp;
2062 2065
2063 2066 if ((rval = dt_handle_setopt(dtp, &optdata)) != 0)
2064 2067 return (rval);
2065 2068
2066 2069 return (0);
2067 2070 }
2068 2071
2069 2072 errstr = dtrace_errmsg(dtp, dtrace_errno(dtp));
2070 2073 len = strlen(option) + strlen(value) + strlen(errstr) + 80;
2071 2074 msg = alloca(len);
2072 2075
2073 2076 (void) snprintf(msg, len, "couldn't set option \"%s\" to \"%s\": %s\n",
2074 2077 option, value, errstr);
2075 2078
2076 2079 if ((rval = dt_handle_liberr(dtp, data, msg)) == 0)
2077 2080 return (0);
2078 2081
2079 2082 return (rval);
2080 2083 }
2081 2084
2082 2085 static int
2083 2086 dt_consume_cpu(dtrace_hdl_t *dtp, FILE *fp, int cpu,
2084 2087 dtrace_bufdesc_t *buf, boolean_t just_one,
2085 2088 dtrace_consume_probe_f *efunc, dtrace_consume_rec_f *rfunc, void *arg)
2086 2089 {
2087 2090 dtrace_epid_t id;
2088 2091 size_t offs;
2089 2092 int flow = (dtp->dt_options[DTRACEOPT_FLOWINDENT] != DTRACEOPT_UNSET);
2090 2093 int quiet = (dtp->dt_options[DTRACEOPT_QUIET] != DTRACEOPT_UNSET);
2091 2094 int rval, i, n;
2092 2095 uint64_t tracememsize = 0;
2093 2096 dtrace_probedata_t data;
2094 2097 uint64_t drops;
2095 2098
2096 2099 bzero(&data, sizeof (data));
2097 2100 data.dtpda_handle = dtp;
2098 2101 data.dtpda_cpu = cpu;
2099 2102 data.dtpda_flow = dtp->dt_flow;
2100 2103 data.dtpda_indent = dtp->dt_indent;
2101 2104 data.dtpda_prefix = dtp->dt_prefix;
2102 2105
2103 2106 for (offs = buf->dtbd_oldest; offs < buf->dtbd_size; ) {
2104 2107 dtrace_eprobedesc_t *epd;
2105 2108
2106 2109 /*
2107 2110 * We're guaranteed to have an ID.
2108 2111 */
2109 2112 id = *(uint32_t *)((uintptr_t)buf->dtbd_data + offs);
2110 2113
2111 2114 if (id == DTRACE_EPIDNONE) {
2112 2115 /*
2113 2116 * This is filler to assure proper alignment of the
2114 2117 * next record; we simply ignore it.
2115 2118 */
2116 2119 offs += sizeof (id);
2117 2120 continue;
2118 2121 }
2119 2122
2120 2123 if ((rval = dt_epid_lookup(dtp, id, &data.dtpda_edesc,
2121 2124 &data.dtpda_pdesc)) != 0)
2122 2125 return (rval);
2123 2126
2124 2127 epd = data.dtpda_edesc;
2125 2128 data.dtpda_data = buf->dtbd_data + offs;
2126 2129
2127 2130 if (data.dtpda_edesc->dtepd_uarg != DT_ECB_DEFAULT) {
2128 2131 rval = dt_handle(dtp, &data);
2129 2132
2130 2133 if (rval == DTRACE_CONSUME_NEXT)
2131 2134 goto nextepid;
2132 2135
2133 2136 if (rval == DTRACE_CONSUME_ERROR)
2134 2137 return (-1);
2135 2138 }
2136 2139
2137 2140 if (flow)
2138 2141 (void) dt_flowindent(dtp, &data, dtp->dt_last_epid,
2139 2142 buf, offs);
2140 2143
2141 2144 rval = (*efunc)(&data, arg);
2142 2145
2143 2146 if (flow) {
2144 2147 if (data.dtpda_flow == DTRACEFLOW_ENTRY)
2145 2148 data.dtpda_indent += 2;
2146 2149 }
2147 2150
2148 2151 if (rval == DTRACE_CONSUME_NEXT)
2149 2152 goto nextepid;
2150 2153
2151 2154 if (rval == DTRACE_CONSUME_ABORT)
2152 2155 return (dt_set_errno(dtp, EDT_DIRABORT));
2153 2156
2154 2157 if (rval != DTRACE_CONSUME_THIS)
2155 2158 return (dt_set_errno(dtp, EDT_BADRVAL));
2156 2159
2157 2160 for (i = 0; i < epd->dtepd_nrecs; i++) {
2158 2161 caddr_t addr;
2159 2162 dtrace_recdesc_t *rec = &epd->dtepd_rec[i];
2160 2163 dtrace_actkind_t act = rec->dtrd_action;
2161 2164
2162 2165 data.dtpda_data = buf->dtbd_data + offs +
2163 2166 rec->dtrd_offset;
2164 2167 addr = data.dtpda_data;
2165 2168
2166 2169 if (act == DTRACEACT_LIBACT) {
2167 2170 uint64_t arg = rec->dtrd_arg;
2168 2171 dtrace_aggvarid_t id;
2169 2172
2170 2173 switch (arg) {
2171 2174 case DT_ACT_CLEAR:
2172 2175 /* LINTED - alignment */
2173 2176 id = *((dtrace_aggvarid_t *)addr);
2174 2177 (void) dtrace_aggregate_walk(dtp,
2175 2178 dt_clear_agg, &id);
2176 2179 continue;
2177 2180
2178 2181 case DT_ACT_DENORMALIZE:
2179 2182 /* LINTED - alignment */
2180 2183 id = *((dtrace_aggvarid_t *)addr);
2181 2184 (void) dtrace_aggregate_walk(dtp,
2182 2185 dt_denormalize_agg, &id);
2183 2186 continue;
2184 2187
2185 2188 case DT_ACT_FTRUNCATE:
2186 2189 if (fp == NULL)
2187 2190 continue;
2188 2191
2189 2192 (void) fflush(fp);
2190 2193 (void) ftruncate(fileno(fp), 0);
2191 2194 (void) fseeko(fp, 0, SEEK_SET);
2192 2195 continue;
2193 2196
2194 2197 case DT_ACT_NORMALIZE:
2195 2198 if (i == epd->dtepd_nrecs - 1)
2196 2199 return (dt_set_errno(dtp,
2197 2200 EDT_BADNORMAL));
2198 2201
2199 2202 if (dt_normalize(dtp,
2200 2203 buf->dtbd_data + offs, rec) != 0)
2201 2204 return (-1);
2202 2205
2203 2206 i++;
2204 2207 continue;
2205 2208
2206 2209 case DT_ACT_SETOPT: {
2207 2210 uint64_t *opts = dtp->dt_options;
2208 2211 dtrace_recdesc_t *valrec;
2209 2212 uint32_t valsize;
2210 2213 caddr_t val;
2211 2214 int rv;
2212 2215
2213 2216 if (i == epd->dtepd_nrecs - 1) {
2214 2217 return (dt_set_errno(dtp,
2215 2218 EDT_BADSETOPT));
2216 2219 }
2217 2220
2218 2221 valrec = &epd->dtepd_rec[++i];
2219 2222 valsize = valrec->dtrd_size;
2220 2223
2221 2224 if (valrec->dtrd_action != act ||
2222 2225 valrec->dtrd_arg != arg) {
2223 2226 return (dt_set_errno(dtp,
2224 2227 EDT_BADSETOPT));
2225 2228 }
2226 2229
2227 2230 if (valsize > sizeof (uint64_t)) {
2228 2231 val = buf->dtbd_data + offs +
2229 2232 valrec->dtrd_offset;
2230 2233 } else {
2231 2234 val = "1";
2232 2235 }
2233 2236
2234 2237 rv = dt_setopt(dtp, &data, addr, val);
2235 2238
2236 2239 if (rv != 0)
2237 2240 return (-1);
2238 2241
2239 2242 flow = (opts[DTRACEOPT_FLOWINDENT] !=
2240 2243 DTRACEOPT_UNSET);
2241 2244 quiet = (opts[DTRACEOPT_QUIET] !=
2242 2245 DTRACEOPT_UNSET);
2243 2246
2244 2247 continue;
2245 2248 }
2246 2249
2247 2250 case DT_ACT_TRUNC:
2248 2251 if (i == epd->dtepd_nrecs - 1)
2249 2252 return (dt_set_errno(dtp,
2250 2253 EDT_BADTRUNC));
2251 2254
2252 2255 if (dt_trunc(dtp,
2253 2256 buf->dtbd_data + offs, rec) != 0)
2254 2257 return (-1);
2255 2258
2256 2259 i++;
2257 2260 continue;
2258 2261
2259 2262 default:
2260 2263 continue;
2261 2264 }
2262 2265 }
2263 2266
2264 2267 if (act == DTRACEACT_TRACEMEM_DYNSIZE &&
2265 2268 rec->dtrd_size == sizeof (uint64_t)) {
2266 2269 /* LINTED - alignment */
2267 2270 tracememsize = *((unsigned long long *)addr);
2268 2271 continue;
2269 2272 }
2270 2273
2271 2274 rval = (*rfunc)(&data, rec, arg);
2272 2275
2273 2276 if (rval == DTRACE_CONSUME_NEXT)
2274 2277 continue;
2275 2278
2276 2279 if (rval == DTRACE_CONSUME_ABORT)
2277 2280 return (dt_set_errno(dtp, EDT_DIRABORT));
2278 2281
2279 2282 if (rval != DTRACE_CONSUME_THIS)
2280 2283 return (dt_set_errno(dtp, EDT_BADRVAL));
2281 2284
2282 2285 if (act == DTRACEACT_STACK) {
2283 2286 int depth = rec->dtrd_arg;
2284 2287
2285 2288 if (dt_print_stack(dtp, fp, NULL, addr, depth,
2286 2289 rec->dtrd_size / depth) < 0)
2287 2290 return (-1);
2288 2291 goto nextrec;
2289 2292 }
2290 2293
2291 2294 if (act == DTRACEACT_USTACK ||
2292 2295 act == DTRACEACT_JSTACK) {
2293 2296 if (dt_print_ustack(dtp, fp, NULL,
2294 2297 addr, rec->dtrd_arg) < 0)
2295 2298 return (-1);
2296 2299 goto nextrec;
2297 2300 }
2298 2301
2299 2302 if (act == DTRACEACT_SYM) {
2300 2303 if (dt_print_sym(dtp, fp, NULL, addr) < 0)
2301 2304 return (-1);
2302 2305 goto nextrec;
2303 2306 }
2304 2307
2305 2308 if (act == DTRACEACT_MOD) {
2306 2309 if (dt_print_mod(dtp, fp, NULL, addr) < 0)
2307 2310 return (-1);
2308 2311 goto nextrec;
2309 2312 }
2310 2313
2311 2314 if (act == DTRACEACT_USYM || act == DTRACEACT_UADDR) {
2312 2315 if (dt_print_usym(dtp, fp, addr, act) < 0)
2313 2316 return (-1);
2314 2317 goto nextrec;
2315 2318 }
2316 2319
2317 2320 if (act == DTRACEACT_UMOD) {
2318 2321 if (dt_print_umod(dtp, fp, NULL, addr) < 0)
2319 2322 return (-1);
2320 2323 goto nextrec;
2321 2324 }
2322 2325
2323 2326 if (DTRACEACT_ISPRINTFLIKE(act)) {
2324 2327 void *fmtdata;
2325 2328 int (*func)(dtrace_hdl_t *, FILE *, void *,
2326 2329 const dtrace_probedata_t *,
2327 2330 const dtrace_recdesc_t *, uint_t,
2328 2331 const void *buf, size_t);
2329 2332
2330 2333 if ((fmtdata = dt_format_lookup(dtp,
2331 2334 rec->dtrd_format)) == NULL)
2332 2335 goto nofmt;
2333 2336
2334 2337 switch (act) {
2335 2338 case DTRACEACT_PRINTF:
2336 2339 func = dtrace_fprintf;
2337 2340 break;
2338 2341 case DTRACEACT_PRINTA:
2339 2342 func = dtrace_fprinta;
2340 2343 break;
2341 2344 case DTRACEACT_SYSTEM:
2342 2345 func = dtrace_system;
2343 2346 break;
2344 2347 case DTRACEACT_FREOPEN:
2345 2348 func = dtrace_freopen;
2346 2349 break;
2347 2350 }
2348 2351
2349 2352 n = (*func)(dtp, fp, fmtdata, &data,
2350 2353 rec, epd->dtepd_nrecs - i,
2351 2354 (uchar_t *)buf->dtbd_data + offs,
2352 2355 buf->dtbd_size - offs);
2353 2356
2354 2357 if (n < 0)
2355 2358 return (-1); /* errno is set for us */
2356 2359
2357 2360 if (n > 0)
2358 2361 i += n - 1;
2359 2362 goto nextrec;
2360 2363 }
2361 2364
2362 2365 /*
2363 2366 * If this is a DIF expression, and the record has a
2364 2367 * format set, this indicates we have a CTF type name
2365 2368 * associated with the data and we should try to print
2366 2369 * it out by type.
2367 2370 */
2368 2371 if (act == DTRACEACT_DIFEXPR) {
2369 2372 const char *strdata = dt_strdata_lookup(dtp,
2370 2373 rec->dtrd_format);
2371 2374 if (strdata != NULL) {
2372 2375 n = dtrace_print(dtp, fp, strdata,
2373 2376 addr, rec->dtrd_size);
2374 2377
2375 2378 /*
2376 2379 * dtrace_print() will return -1 on
2377 2380 * error, or return the number of bytes
2378 2381 * consumed. It will return 0 if the
2379 2382 * type couldn't be determined, and we
2380 2383 * should fall through to the normal
2381 2384 * trace method.
2382 2385 */
2383 2386 if (n < 0)
2384 2387 return (-1);
2385 2388
2386 2389 if (n > 0)
2387 2390 goto nextrec;
2388 2391 }
2389 2392 }
2390 2393
2391 2394 nofmt:
2392 2395 if (act == DTRACEACT_PRINTA) {
2393 2396 dt_print_aggdata_t pd;
2394 2397 dtrace_aggvarid_t *aggvars;
2395 2398 int j, naggvars = 0;
2396 2399 size_t size = ((epd->dtepd_nrecs - i) *
2397 2400 sizeof (dtrace_aggvarid_t));
2398 2401
2399 2402 if ((aggvars = dt_alloc(dtp, size)) == NULL)
2400 2403 return (-1);
2401 2404
2402 2405 /*
2403 2406 * This might be a printa() with multiple
2404 2407 * aggregation variables. We need to scan
2405 2408 * forward through the records until we find
2406 2409 * a record from a different statement.
2407 2410 */
2408 2411 for (j = i; j < epd->dtepd_nrecs; j++) {
2409 2412 dtrace_recdesc_t *nrec;
2410 2413 caddr_t naddr;
2411 2414
2412 2415 nrec = &epd->dtepd_rec[j];
2413 2416
2414 2417 if (nrec->dtrd_uarg != rec->dtrd_uarg)
2415 2418 break;
2416 2419
2417 2420 if (nrec->dtrd_action != act) {
2418 2421 return (dt_set_errno(dtp,
2419 2422 EDT_BADAGG));
2420 2423 }
2421 2424
2422 2425 naddr = buf->dtbd_data + offs +
2423 2426 nrec->dtrd_offset;
2424 2427
2425 2428 aggvars[naggvars++] =
2426 2429 /* LINTED - alignment */
2427 2430 *((dtrace_aggvarid_t *)naddr);
2428 2431 }
2429 2432
2430 2433 i = j - 1;
2431 2434 bzero(&pd, sizeof (pd));
2432 2435 pd.dtpa_dtp = dtp;
2433 2436 pd.dtpa_fp = fp;
2434 2437
2435 2438 assert(naggvars >= 1);
2436 2439
2437 2440 if (naggvars == 1) {
2438 2441 pd.dtpa_id = aggvars[0];
2439 2442 dt_free(dtp, aggvars);
2440 2443
2441 2444 if (dt_printf(dtp, fp, "\n") < 0 ||
2442 2445 dtrace_aggregate_walk_sorted(dtp,
2443 2446 dt_print_agg, &pd) < 0)
2444 2447 return (-1);
2445 2448 goto nextrec;
2446 2449 }
2447 2450
2448 2451 if (dt_printf(dtp, fp, "\n") < 0 ||
2449 2452 dtrace_aggregate_walk_joined(dtp, aggvars,
2450 2453 naggvars, dt_print_aggs, &pd) < 0) {
2451 2454 dt_free(dtp, aggvars);
2452 2455 return (-1);
2453 2456 }
2454 2457
2455 2458 dt_free(dtp, aggvars);
2456 2459 goto nextrec;
2457 2460 }
2458 2461
2459 2462 if (act == DTRACEACT_TRACEMEM) {
2460 2463 if (tracememsize == 0 ||
2461 2464 tracememsize > rec->dtrd_size) {
2462 2465 tracememsize = rec->dtrd_size;
2463 2466 }
2464 2467
2465 2468 n = dt_print_bytes(dtp, fp, addr,
2466 2469 tracememsize, -33, quiet, 1);
2467 2470
2468 2471 tracememsize = 0;
2469 2472
2470 2473 if (n < 0)
2471 2474 return (-1);
2472 2475
2473 2476 goto nextrec;
2474 2477 }
2475 2478
2476 2479 switch (rec->dtrd_size) {
2477 2480 case sizeof (uint64_t):
2478 2481 n = dt_printf(dtp, fp,
2479 2482 quiet ? "%lld" : " %16lld",
2480 2483 /* LINTED - alignment */
2481 2484 *((unsigned long long *)addr));
2482 2485 break;
2483 2486 case sizeof (uint32_t):
2484 2487 n = dt_printf(dtp, fp, quiet ? "%d" : " %8d",
2485 2488 /* LINTED - alignment */
2486 2489 *((uint32_t *)addr));
2487 2490 break;
2488 2491 case sizeof (uint16_t):
2489 2492 n = dt_printf(dtp, fp, quiet ? "%d" : " %5d",
2490 2493 /* LINTED - alignment */
2491 2494 *((uint16_t *)addr));
2492 2495 break;
2493 2496 case sizeof (uint8_t):
2494 2497 n = dt_printf(dtp, fp, quiet ? "%d" : " %3d",
2495 2498 *((uint8_t *)addr));
2496 2499 break;
2497 2500 default:
2498 2501 n = dt_print_bytes(dtp, fp, addr,
2499 2502 rec->dtrd_size, -33, quiet, 0);
2500 2503 break;
2501 2504 }
2502 2505
2503 2506 if (n < 0)
2504 2507 return (-1); /* errno is set for us */
2505 2508
2506 2509 nextrec:
2507 2510 if (dt_buffered_flush(dtp, &data, rec, NULL, 0) < 0)
2508 2511 return (-1); /* errno is set for us */
2509 2512 }
2510 2513
2511 2514 /*
2512 2515 * Call the record callback with a NULL record to indicate
2513 2516 * that we're done processing this EPID.
2514 2517 */
2515 2518 rval = (*rfunc)(&data, NULL, arg);
2516 2519 nextepid:
2517 2520 offs += epd->dtepd_size;
2518 2521 dtp->dt_last_epid = id;
2519 2522 if (just_one) {
2520 2523 buf->dtbd_oldest = offs;
2521 2524 break;
2522 2525 }
2523 2526 }
2524 2527
2525 2528 dtp->dt_flow = data.dtpda_flow;
2526 2529 dtp->dt_indent = data.dtpda_indent;
2527 2530 dtp->dt_prefix = data.dtpda_prefix;
2528 2531
2529 2532 if ((drops = buf->dtbd_drops) == 0)
2530 2533 return (0);
2531 2534
2532 2535 /*
2533 2536 * Explicitly zero the drops to prevent us from processing them again.
2534 2537 */
2535 2538 buf->dtbd_drops = 0;
2536 2539
2537 2540 return (dt_handle_cpudrop(dtp, cpu, DTRACEDROP_PRINCIPAL, drops));
2538 2541 }
2539 2542
2540 2543 /*
2541 2544 * Reduce memory usage by shrinking the buffer if it's no more than half full.
2542 2545 * Note, we need to preserve the alignment of the data at dtbd_oldest, which is
2543 2546 * only 4-byte aligned.
2544 2547 */
2545 2548 static void
2546 2549 dt_realloc_buf(dtrace_hdl_t *dtp, dtrace_bufdesc_t *buf, int cursize)
2547 2550 {
2548 2551 uint64_t used = buf->dtbd_size - buf->dtbd_oldest;
2549 2552 if (used < cursize / 2) {
2550 2553 int misalign = buf->dtbd_oldest & (sizeof (uint64_t) - 1);
2551 2554 char *newdata = dt_alloc(dtp, used + misalign);
2552 2555 if (newdata == NULL)
2553 2556 return;
2554 2557 bzero(newdata, misalign);
2555 2558 bcopy(buf->dtbd_data + buf->dtbd_oldest,
2556 2559 newdata + misalign, used);
2557 2560 dt_free(dtp, buf->dtbd_data);
2558 2561 buf->dtbd_oldest = misalign;
2559 2562 buf->dtbd_size = used + misalign;
2560 2563 buf->dtbd_data = newdata;
2561 2564 }
2562 2565 }
2563 2566
2564 2567 /*
2565 2568 * If the ring buffer has wrapped, the data is not in order. Rearrange it
2566 2569 * so that it is. Note, we need to preserve the alignment of the data at
2567 2570 * dtbd_oldest, which is only 4-byte aligned.
2568 2571 */
2569 2572 static int
2570 2573 dt_unring_buf(dtrace_hdl_t *dtp, dtrace_bufdesc_t *buf)
2571 2574 {
2572 2575 int misalign;
2573 2576 char *newdata, *ndp;
2574 2577
2575 2578 if (buf->dtbd_oldest == 0)
2576 2579 return (0);
2577 2580
2578 2581 misalign = buf->dtbd_oldest & (sizeof (uint64_t) - 1);
2579 2582 newdata = ndp = dt_alloc(dtp, buf->dtbd_size + misalign);
2580 2583
2581 2584 if (newdata == NULL)
2582 2585 return (-1);
2583 2586
2584 2587 assert(0 == (buf->dtbd_size & (sizeof (uint64_t) - 1)));
2585 2588
2586 2589 bzero(ndp, misalign);
2587 2590 ndp += misalign;
2588 2591
2589 2592 bcopy(buf->dtbd_data + buf->dtbd_oldest, ndp,
2590 2593 buf->dtbd_size - buf->dtbd_oldest);
2591 2594 ndp += buf->dtbd_size - buf->dtbd_oldest;
2592 2595
2593 2596 bcopy(buf->dtbd_data, ndp, buf->dtbd_oldest);
2594 2597
2595 2598 dt_free(dtp, buf->dtbd_data);
2596 2599 buf->dtbd_oldest = 0;
2597 2600 buf->dtbd_data = newdata;
2598 2601 buf->dtbd_size += misalign;
2599 2602
2600 2603 return (0);
2601 2604 }
2602 2605
2603 2606 static void
2604 2607 dt_put_buf(dtrace_hdl_t *dtp, dtrace_bufdesc_t *buf)
2605 2608 {
2606 2609 dt_free(dtp, buf->dtbd_data);
2607 2610 dt_free(dtp, buf);
2608 2611 }
2609 2612
2610 2613 /*
2611 2614 * Returns 0 on success, in which case *cbp will be filled in if we retrieved
2612 2615 * data, or NULL if there is no data for this CPU.
2613 2616 * Returns -1 on failure and sets dt_errno.
2614 2617 */
2615 2618 static int
2616 2619 dt_get_buf(dtrace_hdl_t *dtp, int cpu, dtrace_bufdesc_t **bufp)
2617 2620 {
2618 2621 dtrace_optval_t size;
2619 2622 dtrace_bufdesc_t *buf = dt_zalloc(dtp, sizeof (*buf));
2620 2623 int error;
2621 2624
2622 2625 if (buf == NULL)
2623 2626 return (-1);
2624 2627
2625 2628 (void) dtrace_getopt(dtp, "bufsize", &size);
2626 2629 buf->dtbd_data = dt_alloc(dtp, size);
2627 2630 if (buf->dtbd_data == NULL) {
2628 2631 dt_free(dtp, buf);
2629 2632 return (-1);
2630 2633 }
2631 2634 buf->dtbd_size = size;
2632 2635 buf->dtbd_cpu = cpu;
2633 2636
2634 2637 if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, buf) == -1) {
2635 2638 dt_put_buf(dtp, buf);
2636 2639 /*
2637 2640 * If we failed with ENOENT, it may be because the
2638 2641 * CPU was unconfigured -- this is okay. Any other
2639 2642 * error, however, is unexpected.
2640 2643 */
2641 2644 if (errno == ENOENT) {
2642 2645 *bufp = NULL;
2643 2646 return (0);
2644 2647 }
2645 2648
2646 2649 return (dt_set_errno(dtp, errno));
2647 2650 }
2648 2651
2649 2652 error = dt_unring_buf(dtp, buf);
2650 2653 if (error != 0) {
2651 2654 dt_put_buf(dtp, buf);
2652 2655 return (error);
2653 2656 }
2654 2657 dt_realloc_buf(dtp, buf, size);
2655 2658
2656 2659 *bufp = buf;
2657 2660 return (0);
2658 2661 }
2659 2662
2660 2663 typedef struct dt_begin {
2661 2664 dtrace_consume_probe_f *dtbgn_probefunc;
2662 2665 dtrace_consume_rec_f *dtbgn_recfunc;
2663 2666 void *dtbgn_arg;
2664 2667 dtrace_handle_err_f *dtbgn_errhdlr;
2665 2668 void *dtbgn_errarg;
2666 2669 int dtbgn_beginonly;
2667 2670 } dt_begin_t;
2668 2671
2669 2672 static int
2670 2673 dt_consume_begin_probe(const dtrace_probedata_t *data, void *arg)
2671 2674 {
2672 2675 dt_begin_t *begin = arg;
2673 2676 dtrace_probedesc_t *pd = data->dtpda_pdesc;
2674 2677
2675 2678 int r1 = (strcmp(pd->dtpd_provider, "dtrace") == 0);
2676 2679 int r2 = (strcmp(pd->dtpd_name, "BEGIN") == 0);
2677 2680
2678 2681 if (begin->dtbgn_beginonly) {
2679 2682 if (!(r1 && r2))
2680 2683 return (DTRACE_CONSUME_NEXT);
2681 2684 } else {
2682 2685 if (r1 && r2)
2683 2686 return (DTRACE_CONSUME_NEXT);
2684 2687 }
2685 2688
2686 2689 /*
2687 2690 * We have a record that we're interested in. Now call the underlying
2688 2691 * probe function...
2689 2692 */
2690 2693 return (begin->dtbgn_probefunc(data, begin->dtbgn_arg));
2691 2694 }
2692 2695
2693 2696 static int
2694 2697 dt_consume_begin_record(const dtrace_probedata_t *data,
2695 2698 const dtrace_recdesc_t *rec, void *arg)
2696 2699 {
2697 2700 dt_begin_t *begin = arg;
2698 2701
2699 2702 return (begin->dtbgn_recfunc(data, rec, begin->dtbgn_arg));
2700 2703 }
2701 2704
2702 2705 static int
2703 2706 dt_consume_begin_error(const dtrace_errdata_t *data, void *arg)
2704 2707 {
2705 2708 dt_begin_t *begin = (dt_begin_t *)arg;
2706 2709 dtrace_probedesc_t *pd = data->dteda_pdesc;
2707 2710
2708 2711 int r1 = (strcmp(pd->dtpd_provider, "dtrace") == 0);
2709 2712 int r2 = (strcmp(pd->dtpd_name, "BEGIN") == 0);
2710 2713
2711 2714 if (begin->dtbgn_beginonly) {
2712 2715 if (!(r1 && r2))
2713 2716 return (DTRACE_HANDLE_OK);
2714 2717 } else {
2715 2718 if (r1 && r2)
2716 2719 return (DTRACE_HANDLE_OK);
2717 2720 }
2718 2721
2719 2722 return (begin->dtbgn_errhdlr(data, begin->dtbgn_errarg));
2720 2723 }
2721 2724
2722 2725 static int
2723 2726 dt_consume_begin(dtrace_hdl_t *dtp, FILE *fp,
2724 2727 dtrace_consume_probe_f *pf, dtrace_consume_rec_f *rf, void *arg)
2725 2728 {
2726 2729 /*
2727 2730 * There's this idea that the BEGIN probe should be processed before
2728 2731 * everything else, and that the END probe should be processed after
2729 2732 * anything else. In the common case, this is pretty easy to deal
2730 2733 * with. However, a situation may arise where the BEGIN enabling and
2731 2734 * END enabling are on the same CPU, and some enabling in the middle
2732 2735 * occurred on a different CPU. To deal with this (blech!) we need to
2733 2736 * consume the BEGIN buffer up until the end of the BEGIN probe, and
2734 2737 * then set it aside. We will then process every other CPU, and then
2735 2738 * we'll return to the BEGIN CPU and process the rest of the data
2736 2739 * (which will inevitably include the END probe, if any). Making this
2737 2740 * even more complicated (!) is the library's ERROR enabling. Because
2738 2741 * this enabling is processed before we even get into the consume call
2739 2742 * back, any ERROR firing would result in the library's ERROR enabling
2740 2743 * being processed twice -- once in our first pass (for BEGIN probes),
2741 2744 * and again in our second pass (for everything but BEGIN probes). To
2742 2745 * deal with this, we interpose on the ERROR handler to assure that we
2743 2746 * only process ERROR enablings induced by BEGIN enablings in the
2744 2747 * first pass, and that we only process ERROR enablings _not_ induced
2745 2748 * by BEGIN enablings in the second pass.
2746 2749 */
2747 2750
2748 2751 dt_begin_t begin;
2749 2752 processorid_t cpu = dtp->dt_beganon;
2750 2753 int rval, i;
2751 2754 static int max_ncpus;
2752 2755 dtrace_bufdesc_t *buf;
2753 2756
2754 2757 dtp->dt_beganon = -1;
2755 2758
2756 2759 if (dt_get_buf(dtp, cpu, &buf) != 0)
2757 2760 return (-1);
2758 2761 if (buf == NULL)
2759 2762 return (0);
2760 2763
2761 2764 if (!dtp->dt_stopped || buf->dtbd_cpu != dtp->dt_endedon) {
2762 2765 /*
2763 2766 * This is the simple case. We're either not stopped, or if
2764 2767 * we are, we actually processed any END probes on another
2765 2768 * CPU. We can simply consume this buffer and return.
2766 2769 */
2767 2770 rval = dt_consume_cpu(dtp, fp, cpu, buf, B_FALSE,
2768 2771 pf, rf, arg);
2769 2772 dt_put_buf(dtp, buf);
2770 2773 return (rval);
2771 2774 }
2772 2775
2773 2776 begin.dtbgn_probefunc = pf;
2774 2777 begin.dtbgn_recfunc = rf;
2775 2778 begin.dtbgn_arg = arg;
2776 2779 begin.dtbgn_beginonly = 1;
2777 2780
2778 2781 /*
2779 2782 * We need to interpose on the ERROR handler to be sure that we
2780 2783 * only process ERRORs induced by BEGIN.
2781 2784 */
2782 2785 begin.dtbgn_errhdlr = dtp->dt_errhdlr;
2783 2786 begin.dtbgn_errarg = dtp->dt_errarg;
2784 2787 dtp->dt_errhdlr = dt_consume_begin_error;
2785 2788 dtp->dt_errarg = &begin;
2786 2789
2787 2790 rval = dt_consume_cpu(dtp, fp, cpu, buf, B_FALSE,
2788 2791 dt_consume_begin_probe, dt_consume_begin_record, &begin);
2789 2792
2790 2793 dtp->dt_errhdlr = begin.dtbgn_errhdlr;
2791 2794 dtp->dt_errarg = begin.dtbgn_errarg;
2792 2795
2793 2796 if (rval != 0) {
2794 2797 dt_put_buf(dtp, buf);
2795 2798 return (rval);
2796 2799 }
2797 2800
2798 2801 if (max_ncpus == 0)
2799 2802 max_ncpus = dt_sysconf(dtp, _SC_CPUID_MAX) + 1;
2800 2803
2801 2804 for (i = 0; i < max_ncpus; i++) {
2802 2805 dtrace_bufdesc_t *nbuf;
2803 2806 if (i == cpu)
2804 2807 continue;
2805 2808
2806 2809 if (dt_get_buf(dtp, i, &nbuf) != 0) {
2807 2810 dt_put_buf(dtp, buf);
2808 2811 return (-1);
2809 2812 }
2810 2813 if (nbuf == NULL)
2811 2814 continue;
2812 2815
2813 2816 rval = dt_consume_cpu(dtp, fp, i, nbuf, B_FALSE,
2814 2817 pf, rf, arg);
2815 2818 dt_put_buf(dtp, nbuf);
2816 2819 if (rval != 0) {
2817 2820 dt_put_buf(dtp, buf);
2818 2821 return (rval);
2819 2822 }
2820 2823 }
2821 2824
2822 2825 /*
2823 2826 * Okay -- we're done with the other buffers. Now we want to
2824 2827 * reconsume the first buffer -- but this time we're looking for
2825 2828 * everything _but_ BEGIN. And of course, in order to only consume
2826 2829 * those ERRORs _not_ associated with BEGIN, we need to reinstall our
2827 2830 * ERROR interposition function...
2828 2831 */
2829 2832 begin.dtbgn_beginonly = 0;
2830 2833
2831 2834 assert(begin.dtbgn_errhdlr == dtp->dt_errhdlr);
2832 2835 assert(begin.dtbgn_errarg == dtp->dt_errarg);
2833 2836 dtp->dt_errhdlr = dt_consume_begin_error;
2834 2837 dtp->dt_errarg = &begin;
2835 2838
2836 2839 rval = dt_consume_cpu(dtp, fp, cpu, buf, B_FALSE,
2837 2840 dt_consume_begin_probe, dt_consume_begin_record, &begin);
2838 2841
2839 2842 dtp->dt_errhdlr = begin.dtbgn_errhdlr;
2840 2843 dtp->dt_errarg = begin.dtbgn_errarg;
2841 2844
2842 2845 return (rval);
2843 2846 }
2844 2847
2845 2848 /* ARGSUSED */
2846 2849 static uint64_t
2847 2850 dt_buf_oldest(void *elem, void *arg)
2848 2851 {
2849 2852 dtrace_bufdesc_t *buf = elem;
2850 2853 size_t offs = buf->dtbd_oldest;
2851 2854
2852 2855 while (offs < buf->dtbd_size) {
2853 2856 dtrace_rechdr_t *dtrh =
2854 2857 /* LINTED - alignment */
2855 2858 (dtrace_rechdr_t *)(buf->dtbd_data + offs);
2856 2859 if (dtrh->dtrh_epid == DTRACE_EPIDNONE) {
2857 2860 offs += sizeof (dtrace_epid_t);
2858 2861 } else {
2859 2862 return (DTRACE_RECORD_LOAD_TIMESTAMP(dtrh));
2860 2863 }
2861 2864 }
2862 2865
2863 2866 /* There are no records left; use the time the buffer was retrieved. */
2864 2867 return (buf->dtbd_timestamp);
2865 2868 }
2866 2869
2867 2870 int
2868 2871 dtrace_consume(dtrace_hdl_t *dtp, FILE *fp,
2869 2872 dtrace_consume_probe_f *pf, dtrace_consume_rec_f *rf, void *arg)
2870 2873 {
2871 2874 dtrace_optval_t size;
2872 2875 static int max_ncpus;
2873 2876 int i, rval;
2874 2877 dtrace_optval_t interval = dtp->dt_options[DTRACEOPT_SWITCHRATE];
2875 2878 hrtime_t now = gethrtime();
2876 2879
2877 2880 if (dtp->dt_lastswitch != 0) {
2878 2881 if (now - dtp->dt_lastswitch < interval)
2879 2882 return (0);
2880 2883
2881 2884 dtp->dt_lastswitch += interval;
2882 2885 } else {
2883 2886 dtp->dt_lastswitch = now;
2884 2887 }
2885 2888
2886 2889 if (!dtp->dt_active)
2887 2890 return (dt_set_errno(dtp, EINVAL));
2888 2891
2889 2892 if (max_ncpus == 0)
2890 2893 max_ncpus = dt_sysconf(dtp, _SC_CPUID_MAX) + 1;
2891 2894
2892 2895 if (pf == NULL)
2893 2896 pf = (dtrace_consume_probe_f *)dt_nullprobe;
2894 2897
2895 2898 if (rf == NULL)
2896 2899 rf = (dtrace_consume_rec_f *)dt_nullrec;
2897 2900
2898 2901 if (dtp->dt_options[DTRACEOPT_TEMPORAL] == DTRACEOPT_UNSET) {
2899 2902 /*
2900 2903 * The output will not be in the order it was traced. Rather,
2901 2904 * we will consume all of the data from each CPU's buffer in
2902 2905 * turn. We apply special handling for the records from BEGIN
2903 2906 * and END probes so that they are consumed first and last,
2904 2907 * respectively.
2905 2908 *
2906 2909 * If we have just begun, we want to first process the CPU that
2907 2910 * executed the BEGIN probe (if any).
2908 2911 */
2909 2912 if (dtp->dt_active && dtp->dt_beganon != -1 &&
2910 2913 (rval = dt_consume_begin(dtp, fp, pf, rf, arg)) != 0)
2911 2914 return (rval);
2912 2915
2913 2916 for (i = 0; i < max_ncpus; i++) {
2914 2917 dtrace_bufdesc_t *buf;
2915 2918
2916 2919 /*
2917 2920 * If we have stopped, we want to process the CPU on
2918 2921 * which the END probe was processed only _after_ we
2919 2922 * have processed everything else.
2920 2923 */
2921 2924 if (dtp->dt_stopped && (i == dtp->dt_endedon))
2922 2925 continue;
2923 2926
2924 2927 if (dt_get_buf(dtp, i, &buf) != 0)
2925 2928 return (-1);
2926 2929 if (buf == NULL)
2927 2930 continue;
2928 2931
2929 2932 dtp->dt_flow = 0;
2930 2933 dtp->dt_indent = 0;
2931 2934 dtp->dt_prefix = NULL;
2932 2935 rval = dt_consume_cpu(dtp, fp, i,
2933 2936 buf, B_FALSE, pf, rf, arg);
2934 2937 dt_put_buf(dtp, buf);
2935 2938 if (rval != 0)
2936 2939 return (rval);
2937 2940 }
2938 2941 if (dtp->dt_stopped) {
2939 2942 dtrace_bufdesc_t *buf;
2940 2943
2941 2944 if (dt_get_buf(dtp, dtp->dt_endedon, &buf) != 0)
2942 2945 return (-1);
2943 2946 if (buf == NULL)
2944 2947 return (0);
2945 2948
2946 2949 rval = dt_consume_cpu(dtp, fp, dtp->dt_endedon,
2947 2950 buf, B_FALSE, pf, rf, arg);
2948 2951 dt_put_buf(dtp, buf);
2949 2952 return (rval);
2950 2953 }
2951 2954 } else {
2952 2955 /*
2953 2956 * The output will be in the order it was traced (or for
2954 2957 * speculations, when it was committed). We retrieve a buffer
2955 2958 * from each CPU and put it into a priority queue, which sorts
2956 2959 * based on the first entry in the buffer. This is sufficient
2957 2960 * because entries within a buffer are already sorted.
2958 2961 *
2959 2962 * We then consume records one at a time, always consuming the
2960 2963 * oldest record, as determined by the priority queue. When
2961 2964 * we reach the end of the time covered by these buffers,
2962 2965 * we need to stop and retrieve more records on the next pass.
2963 2966 * The kernel tells us the time covered by each buffer, in
2964 2967 * dtbd_timestamp. The first buffer's timestamp tells us the
2965 2968 * time covered by all buffers, as subsequently retrieved
2966 2969 * buffers will cover to a more recent time.
2967 2970 */
2968 2971
2969 2972 uint64_t *drops = alloca(max_ncpus * sizeof (uint64_t));
2970 2973 uint64_t first_timestamp = 0;
2971 2974 uint_t cookie = 0;
2972 2975 dtrace_bufdesc_t *buf;
2973 2976
2974 2977 bzero(drops, max_ncpus * sizeof (uint64_t));
2975 2978
2976 2979 if (dtp->dt_bufq == NULL) {
2977 2980 dtp->dt_bufq = dt_pq_init(dtp, max_ncpus * 2,
2978 2981 dt_buf_oldest, NULL);
2979 2982 if (dtp->dt_bufq == NULL) /* ENOMEM */
2980 2983 return (-1);
2981 2984 }
2982 2985
2983 2986 /* Retrieve data from each CPU. */
2984 2987 (void) dtrace_getopt(dtp, "bufsize", &size);
2985 2988 for (i = 0; i < max_ncpus; i++) {
2986 2989 dtrace_bufdesc_t *buf;
2987 2990
2988 2991 if (dt_get_buf(dtp, i, &buf) != 0)
2989 2992 return (-1);
2990 2993 if (buf != NULL) {
2991 2994 if (first_timestamp == 0)
2992 2995 first_timestamp = buf->dtbd_timestamp;
2993 2996 assert(buf->dtbd_timestamp >= first_timestamp);
2994 2997
2995 2998 dt_pq_insert(dtp->dt_bufq, buf);
2996 2999 drops[i] = buf->dtbd_drops;
2997 3000 buf->dtbd_drops = 0;
2998 3001 }
2999 3002 }
3000 3003
3001 3004 /* Consume records. */
3002 3005 for (;;) {
3003 3006 dtrace_bufdesc_t *buf = dt_pq_pop(dtp->dt_bufq);
3004 3007 uint64_t timestamp;
3005 3008
3006 3009 if (buf == NULL)
3007 3010 break;
3008 3011
3009 3012 timestamp = dt_buf_oldest(buf, dtp);
3010 3013 assert(timestamp >= dtp->dt_last_timestamp);
3011 3014 dtp->dt_last_timestamp = timestamp;
3012 3015
3013 3016 if (timestamp == buf->dtbd_timestamp) {
3014 3017 /*
3015 3018 * We've reached the end of the time covered
3016 3019 * by this buffer. If this is the oldest
3017 3020 * buffer, we must do another pass
3018 3021 * to retrieve more data.
3019 3022 */
3020 3023 dt_put_buf(dtp, buf);
3021 3024 if (timestamp == first_timestamp &&
3022 3025 !dtp->dt_stopped)
3023 3026 break;
3024 3027 continue;
3025 3028 }
3026 3029
3027 3030 if ((rval = dt_consume_cpu(dtp, fp,
3028 3031 buf->dtbd_cpu, buf, B_TRUE, pf, rf, arg)) != 0)
3029 3032 return (rval);
3030 3033 dt_pq_insert(dtp->dt_bufq, buf);
3031 3034 }
3032 3035
3033 3036 /* Consume drops. */
3034 3037 for (i = 0; i < max_ncpus; i++) {
3035 3038 if (drops[i] != 0) {
3036 3039 int error = dt_handle_cpudrop(dtp, i,
3037 3040 DTRACEDROP_PRINCIPAL, drops[i]);
3038 3041 if (error != 0)
3039 3042 return (error);
3040 3043 }
3041 3044 }
3042 3045
3043 3046 /*
3044 3047 * Reduce memory usage by re-allocating smaller buffers
3045 3048 * for the "remnants".
3046 3049 */
3047 3050 while (buf = dt_pq_walk(dtp->dt_bufq, &cookie))
3048 3051 dt_realloc_buf(dtp, buf, buf->dtbd_size);
3049 3052 }
3050 3053
3051 3054 return (0);
3052 3055 }
↓ open down ↓ |
1645 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX