dt_consume.c revision 237624
1178479Sjb/* 2178479Sjb * CDDL HEADER START 3178479Sjb * 4178479Sjb * The contents of this file are subject to the terms of the 5178479Sjb * Common Development and Distribution License (the "License"). 6178479Sjb * You may not use this file except in compliance with the License. 7178479Sjb * 8178479Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9178479Sjb * or http://www.opensolaris.org/os/licensing. 10178479Sjb * See the License for the specific language governing permissions 11178479Sjb * and limitations under the License. 12178479Sjb * 13178479Sjb * When distributing Covered Code, include this CDDL HEADER in each 14178479Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15178479Sjb * If applicable, add the following below this CDDL HEADER, with the 16178479Sjb * fields enclosed by brackets "[]" replaced with your own identifying 17178479Sjb * information: Portions Copyright [yyyy] [name of copyright owner] 18178479Sjb * 19178479Sjb * CDDL HEADER END 20178479Sjb */ 21178479Sjb/* 22210767Srpaulo * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23178479Sjb * Use is subject to license terms. 24178479Sjb */ 25178479Sjb 26237624Spfg/* 27237624Spfg * Copyright (c) 2011, Joyent, Inc. All rights reserved. 28237624Spfg */ 29237624Spfg 30178479Sjb#include <stdlib.h> 31178479Sjb#include <strings.h> 32178479Sjb#include <errno.h> 33178479Sjb#include <unistd.h> 34178479Sjb#include <limits.h> 35178479Sjb#include <assert.h> 36178479Sjb#include <ctype.h> 37178576Sjb#if defined(sun) 38178479Sjb#include <alloca.h> 39178576Sjb#endif 40178479Sjb#include <dt_impl.h> 41211554Srpaulo#if !defined(sun) 42211554Srpaulo#include <libproc_compat.h> 43211554Srpaulo#endif 44178479Sjb 45178479Sjb#define DT_MASK_LO 0x00000000FFFFFFFFULL 46178479Sjb 47178479Sjb/* 48178479Sjb * We declare this here because (1) we need it and (2) we want to avoid a 49178479Sjb * dependency on libm in libdtrace. 50178479Sjb */ 51178479Sjbstatic long double 52178479Sjbdt_fabsl(long double x) 53178479Sjb{ 54178479Sjb if (x < 0) 55178479Sjb return (-x); 56178479Sjb 57178479Sjb return (x); 58178479Sjb} 59178479Sjb 60178479Sjb/* 61178479Sjb * 128-bit arithmetic functions needed to support the stddev() aggregating 62178479Sjb * action. 63178479Sjb */ 64178479Sjbstatic int 65178479Sjbdt_gt_128(uint64_t *a, uint64_t *b) 66178479Sjb{ 67178479Sjb return (a[1] > b[1] || (a[1] == b[1] && a[0] > b[0])); 68178479Sjb} 69178479Sjb 70178479Sjbstatic int 71178479Sjbdt_ge_128(uint64_t *a, uint64_t *b) 72178479Sjb{ 73178479Sjb return (a[1] > b[1] || (a[1] == b[1] && a[0] >= b[0])); 74178479Sjb} 75178479Sjb 76178479Sjbstatic int 77178479Sjbdt_le_128(uint64_t *a, uint64_t *b) 78178479Sjb{ 79178479Sjb return (a[1] < b[1] || (a[1] == b[1] && a[0] <= b[0])); 80178479Sjb} 81178479Sjb 82178479Sjb/* 83178479Sjb * Shift the 128-bit value in a by b. If b is positive, shift left. 84178479Sjb * If b is negative, shift right. 85178479Sjb */ 86178479Sjbstatic void 87178479Sjbdt_shift_128(uint64_t *a, int b) 88178479Sjb{ 89178479Sjb uint64_t mask; 90178479Sjb 91178479Sjb if (b == 0) 92178479Sjb return; 93178479Sjb 94178479Sjb if (b < 0) { 95178479Sjb b = -b; 96178479Sjb if (b >= 64) { 97178479Sjb a[0] = a[1] >> (b - 64); 98178479Sjb a[1] = 0; 99178479Sjb } else { 100178479Sjb a[0] >>= b; 101178479Sjb mask = 1LL << (64 - b); 102178479Sjb mask -= 1; 103178479Sjb a[0] |= ((a[1] & mask) << (64 - b)); 104178479Sjb a[1] >>= b; 105178479Sjb } 106178479Sjb } else { 107178479Sjb if (b >= 64) { 108178479Sjb a[1] = a[0] << (b - 64); 109178479Sjb a[0] = 0; 110178479Sjb } else { 111178479Sjb a[1] <<= b; 112178479Sjb mask = a[0] >> (64 - b); 113178479Sjb a[1] |= mask; 114178479Sjb a[0] <<= b; 115178479Sjb } 116178479Sjb } 117178479Sjb} 118178479Sjb 119178479Sjbstatic int 120178479Sjbdt_nbits_128(uint64_t *a) 121178479Sjb{ 122178479Sjb int nbits = 0; 123178479Sjb uint64_t tmp[2]; 124178479Sjb uint64_t zero[2] = { 0, 0 }; 125178479Sjb 126178479Sjb tmp[0] = a[0]; 127178479Sjb tmp[1] = a[1]; 128178479Sjb 129178479Sjb dt_shift_128(tmp, -1); 130178479Sjb while (dt_gt_128(tmp, zero)) { 131178479Sjb dt_shift_128(tmp, -1); 132178479Sjb nbits++; 133178479Sjb } 134178479Sjb 135178479Sjb return (nbits); 136178479Sjb} 137178479Sjb 138178479Sjbstatic void 139178479Sjbdt_subtract_128(uint64_t *minuend, uint64_t *subtrahend, uint64_t *difference) 140178479Sjb{ 141178479Sjb uint64_t result[2]; 142178479Sjb 143178479Sjb result[0] = minuend[0] - subtrahend[0]; 144178479Sjb result[1] = minuend[1] - subtrahend[1] - 145178479Sjb (minuend[0] < subtrahend[0] ? 1 : 0); 146178479Sjb 147178479Sjb difference[0] = result[0]; 148178479Sjb difference[1] = result[1]; 149178479Sjb} 150178479Sjb 151178479Sjbstatic void 152178479Sjbdt_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum) 153178479Sjb{ 154178479Sjb uint64_t result[2]; 155178479Sjb 156178479Sjb result[0] = addend1[0] + addend2[0]; 157178479Sjb result[1] = addend1[1] + addend2[1] + 158178479Sjb (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0); 159178479Sjb 160178479Sjb sum[0] = result[0]; 161178479Sjb sum[1] = result[1]; 162178479Sjb} 163178479Sjb 164178479Sjb/* 165178479Sjb * The basic idea is to break the 2 64-bit values into 4 32-bit values, 166178479Sjb * use native multiplication on those, and then re-combine into the 167178479Sjb * resulting 128-bit value. 168178479Sjb * 169178479Sjb * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) = 170178479Sjb * hi1 * hi2 << 64 + 171178479Sjb * hi1 * lo2 << 32 + 172178479Sjb * hi2 * lo1 << 32 + 173178479Sjb * lo1 * lo2 174178479Sjb */ 175178479Sjbstatic void 176178479Sjbdt_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product) 177178479Sjb{ 178178479Sjb uint64_t hi1, hi2, lo1, lo2; 179178479Sjb uint64_t tmp[2]; 180178479Sjb 181178479Sjb hi1 = factor1 >> 32; 182178479Sjb hi2 = factor2 >> 32; 183178479Sjb 184178479Sjb lo1 = factor1 & DT_MASK_LO; 185178479Sjb lo2 = factor2 & DT_MASK_LO; 186178479Sjb 187178479Sjb product[0] = lo1 * lo2; 188178479Sjb product[1] = hi1 * hi2; 189178479Sjb 190178479Sjb tmp[0] = hi1 * lo2; 191178479Sjb tmp[1] = 0; 192178479Sjb dt_shift_128(tmp, 32); 193178479Sjb dt_add_128(product, tmp, product); 194178479Sjb 195178479Sjb tmp[0] = hi2 * lo1; 196178479Sjb tmp[1] = 0; 197178479Sjb dt_shift_128(tmp, 32); 198178479Sjb dt_add_128(product, tmp, product); 199178479Sjb} 200178479Sjb 201178479Sjb/* 202178479Sjb * This is long-hand division. 203178479Sjb * 204178479Sjb * We initialize subtrahend by shifting divisor left as far as possible. We 205178479Sjb * loop, comparing subtrahend to dividend: if subtrahend is smaller, we 206178479Sjb * subtract and set the appropriate bit in the result. We then shift 207178479Sjb * subtrahend right by one bit for the next comparison. 208178479Sjb */ 209178479Sjbstatic void 210178479Sjbdt_divide_128(uint64_t *dividend, uint64_t divisor, uint64_t *quotient) 211178479Sjb{ 212178479Sjb uint64_t result[2] = { 0, 0 }; 213178479Sjb uint64_t remainder[2]; 214178479Sjb uint64_t subtrahend[2]; 215178479Sjb uint64_t divisor_128[2]; 216178479Sjb uint64_t mask[2] = { 1, 0 }; 217178479Sjb int log = 0; 218178479Sjb 219178479Sjb assert(divisor != 0); 220178479Sjb 221178479Sjb divisor_128[0] = divisor; 222178479Sjb divisor_128[1] = 0; 223178479Sjb 224178479Sjb remainder[0] = dividend[0]; 225178479Sjb remainder[1] = dividend[1]; 226178479Sjb 227178479Sjb subtrahend[0] = divisor; 228178479Sjb subtrahend[1] = 0; 229178479Sjb 230178479Sjb while (divisor > 0) { 231178479Sjb log++; 232178479Sjb divisor >>= 1; 233178479Sjb } 234178479Sjb 235178479Sjb dt_shift_128(subtrahend, 128 - log); 236178479Sjb dt_shift_128(mask, 128 - log); 237178479Sjb 238178479Sjb while (dt_ge_128(remainder, divisor_128)) { 239178479Sjb if (dt_ge_128(remainder, subtrahend)) { 240178479Sjb dt_subtract_128(remainder, subtrahend, remainder); 241178479Sjb result[0] |= mask[0]; 242178479Sjb result[1] |= mask[1]; 243178479Sjb } 244178479Sjb 245178479Sjb dt_shift_128(subtrahend, -1); 246178479Sjb dt_shift_128(mask, -1); 247178479Sjb } 248178479Sjb 249178479Sjb quotient[0] = result[0]; 250178479Sjb quotient[1] = result[1]; 251178479Sjb} 252178479Sjb 253178479Sjb/* 254178479Sjb * This is the long-hand method of calculating a square root. 255178479Sjb * The algorithm is as follows: 256178479Sjb * 257178479Sjb * 1. Group the digits by 2 from the right. 258178479Sjb * 2. Over the leftmost group, find the largest single-digit number 259178479Sjb * whose square is less than that group. 260178479Sjb * 3. Subtract the result of the previous step (2 or 4, depending) and 261178479Sjb * bring down the next two-digit group. 262178479Sjb * 4. For the result R we have so far, find the largest single-digit number 263178479Sjb * x such that 2 * R * 10 * x + x^2 is less than the result from step 3. 264178479Sjb * (Note that this is doubling R and performing a decimal left-shift by 1 265178479Sjb * and searching for the appropriate decimal to fill the one's place.) 266178479Sjb * The value x is the next digit in the square root. 267178479Sjb * Repeat steps 3 and 4 until the desired precision is reached. (We're 268178479Sjb * dealing with integers, so the above is sufficient.) 269178479Sjb * 270178479Sjb * In decimal, the square root of 582,734 would be calculated as so: 271178479Sjb * 272178479Sjb * __7__6__3 273178479Sjb * | 58 27 34 274178479Sjb * -49 (7^2 == 49 => 7 is the first digit in the square root) 275178479Sjb * -- 276178479Sjb * 9 27 (Subtract and bring down the next group.) 277178479Sjb * 146 8 76 (2 * 7 * 10 * 6 + 6^2 == 876 => 6 is the next digit in 278178479Sjb * ----- the square root) 279178479Sjb * 51 34 (Subtract and bring down the next group.) 280178479Sjb * 1523 45 69 (2 * 76 * 10 * 3 + 3^2 == 4569 => 3 is the next digit in 281178479Sjb * ----- the square root) 282178479Sjb * 5 65 (remainder) 283178479Sjb * 284178479Sjb * The above algorithm applies similarly in binary, but note that the 285178479Sjb * only possible non-zero value for x in step 4 is 1, so step 4 becomes a 286178479Sjb * simple decision: is 2 * R * 2 * 1 + 1^2 (aka R << 2 + 1) less than the 287178479Sjb * preceding difference? 288178479Sjb * 289178479Sjb * In binary, the square root of 11011011 would be calculated as so: 290178479Sjb * 291178479Sjb * __1__1__1__0 292178479Sjb * | 11 01 10 11 293178479Sjb * 01 (0 << 2 + 1 == 1 < 11 => this bit is 1) 294178479Sjb * -- 295178479Sjb * 10 01 10 11 296178479Sjb * 101 1 01 (1 << 2 + 1 == 101 < 1001 => next bit is 1) 297178479Sjb * ----- 298178479Sjb * 1 00 10 11 299178479Sjb * 1101 11 01 (11 << 2 + 1 == 1101 < 10010 => next bit is 1) 300178479Sjb * ------- 301178479Sjb * 1 01 11 302178479Sjb * 11101 1 11 01 (111 << 2 + 1 == 11101 > 10111 => last bit is 0) 303178479Sjb * 304178479Sjb */ 305178479Sjbstatic uint64_t 306178479Sjbdt_sqrt_128(uint64_t *square) 307178479Sjb{ 308178479Sjb uint64_t result[2] = { 0, 0 }; 309178479Sjb uint64_t diff[2] = { 0, 0 }; 310178479Sjb uint64_t one[2] = { 1, 0 }; 311178479Sjb uint64_t next_pair[2]; 312178479Sjb uint64_t next_try[2]; 313178479Sjb uint64_t bit_pairs, pair_shift; 314178479Sjb int i; 315178479Sjb 316178479Sjb bit_pairs = dt_nbits_128(square) / 2; 317178479Sjb pair_shift = bit_pairs * 2; 318178479Sjb 319178479Sjb for (i = 0; i <= bit_pairs; i++) { 320178479Sjb /* 321178479Sjb * Bring down the next pair of bits. 322178479Sjb */ 323178479Sjb next_pair[0] = square[0]; 324178479Sjb next_pair[1] = square[1]; 325178479Sjb dt_shift_128(next_pair, -pair_shift); 326178479Sjb next_pair[0] &= 0x3; 327178479Sjb next_pair[1] = 0; 328178479Sjb 329178479Sjb dt_shift_128(diff, 2); 330178479Sjb dt_add_128(diff, next_pair, diff); 331178479Sjb 332178479Sjb /* 333178479Sjb * next_try = R << 2 + 1 334178479Sjb */ 335178479Sjb next_try[0] = result[0]; 336178479Sjb next_try[1] = result[1]; 337178479Sjb dt_shift_128(next_try, 2); 338178479Sjb dt_add_128(next_try, one, next_try); 339178479Sjb 340178479Sjb if (dt_le_128(next_try, diff)) { 341178479Sjb dt_subtract_128(diff, next_try, diff); 342178479Sjb dt_shift_128(result, 1); 343178479Sjb dt_add_128(result, one, result); 344178479Sjb } else { 345178479Sjb dt_shift_128(result, 1); 346178479Sjb } 347178479Sjb 348178479Sjb pair_shift -= 2; 349178479Sjb } 350178479Sjb 351178479Sjb assert(result[1] == 0); 352178479Sjb 353178479Sjb return (result[0]); 354178479Sjb} 355178479Sjb 356178479Sjbuint64_t 357178479Sjbdt_stddev(uint64_t *data, uint64_t normal) 358178479Sjb{ 359178479Sjb uint64_t avg_of_squares[2]; 360178479Sjb uint64_t square_of_avg[2]; 361178479Sjb int64_t norm_avg; 362178479Sjb uint64_t diff[2]; 363178479Sjb 364178479Sjb /* 365178479Sjb * The standard approximation for standard deviation is 366178479Sjb * sqrt(average(x**2) - average(x)**2), i.e. the square root 367178479Sjb * of the average of the squares minus the square of the average. 368178479Sjb */ 369178479Sjb dt_divide_128(data + 2, normal, avg_of_squares); 370178479Sjb dt_divide_128(avg_of_squares, data[0], avg_of_squares); 371178479Sjb 372178479Sjb norm_avg = (int64_t)data[1] / (int64_t)normal / (int64_t)data[0]; 373178479Sjb 374178479Sjb if (norm_avg < 0) 375178479Sjb norm_avg = -norm_avg; 376178479Sjb 377178479Sjb dt_multiply_128((uint64_t)norm_avg, (uint64_t)norm_avg, square_of_avg); 378178479Sjb 379178479Sjb dt_subtract_128(avg_of_squares, square_of_avg, diff); 380178479Sjb 381178479Sjb return (dt_sqrt_128(diff)); 382178479Sjb} 383178479Sjb 384178479Sjbstatic int 385178479Sjbdt_flowindent(dtrace_hdl_t *dtp, dtrace_probedata_t *data, dtrace_epid_t last, 386178479Sjb dtrace_bufdesc_t *buf, size_t offs) 387178479Sjb{ 388178479Sjb dtrace_probedesc_t *pd = data->dtpda_pdesc, *npd; 389178479Sjb dtrace_eprobedesc_t *epd = data->dtpda_edesc, *nepd; 390178479Sjb char *p = pd->dtpd_provider, *n = pd->dtpd_name, *sub; 391178479Sjb dtrace_flowkind_t flow = DTRACEFLOW_NONE; 392178479Sjb const char *str = NULL; 393178479Sjb static const char *e_str[2] = { " -> ", " => " }; 394178479Sjb static const char *r_str[2] = { " <- ", " <= " }; 395178479Sjb static const char *ent = "entry", *ret = "return"; 396178479Sjb static int entlen = 0, retlen = 0; 397178479Sjb dtrace_epid_t next, id = epd->dtepd_epid; 398178479Sjb int rval; 399178479Sjb 400178479Sjb if (entlen == 0) { 401178479Sjb assert(retlen == 0); 402178479Sjb entlen = strlen(ent); 403178479Sjb retlen = strlen(ret); 404178479Sjb } 405178479Sjb 406178479Sjb /* 407178479Sjb * If the name of the probe is "entry" or ends with "-entry", we 408178479Sjb * treat it as an entry; if it is "return" or ends with "-return", 409178479Sjb * we treat it as a return. (This allows application-provided probes 410178479Sjb * like "method-entry" or "function-entry" to participate in flow 411178479Sjb * indentation -- without accidentally misinterpreting popular probe 412178479Sjb * names like "carpentry", "gentry" or "Coventry".) 413178479Sjb */ 414178479Sjb if ((sub = strstr(n, ent)) != NULL && sub[entlen] == '\0' && 415178479Sjb (sub == n || sub[-1] == '-')) { 416178479Sjb flow = DTRACEFLOW_ENTRY; 417178479Sjb str = e_str[strcmp(p, "syscall") == 0]; 418178479Sjb } else if ((sub = strstr(n, ret)) != NULL && sub[retlen] == '\0' && 419178479Sjb (sub == n || sub[-1] == '-')) { 420178479Sjb flow = DTRACEFLOW_RETURN; 421178479Sjb str = r_str[strcmp(p, "syscall") == 0]; 422178479Sjb } 423178479Sjb 424178479Sjb /* 425178479Sjb * If we're going to indent this, we need to check the ID of our last 426178479Sjb * call. If we're looking at the same probe ID but a different EPID, 427178479Sjb * we _don't_ want to indent. (Yes, there are some minor holes in 428178479Sjb * this scheme -- it's a heuristic.) 429178479Sjb */ 430178479Sjb if (flow == DTRACEFLOW_ENTRY) { 431178479Sjb if ((last != DTRACE_EPIDNONE && id != last && 432178479Sjb pd->dtpd_id == dtp->dt_pdesc[last]->dtpd_id)) 433178479Sjb flow = DTRACEFLOW_NONE; 434178479Sjb } 435178479Sjb 436178479Sjb /* 437178479Sjb * If we're going to unindent this, it's more difficult to see if 438178479Sjb * we don't actually want to unindent it -- we need to look at the 439178479Sjb * _next_ EPID. 440178479Sjb */ 441178479Sjb if (flow == DTRACEFLOW_RETURN) { 442178479Sjb offs += epd->dtepd_size; 443178479Sjb 444178479Sjb do { 445178479Sjb if (offs >= buf->dtbd_size) { 446178479Sjb /* 447178479Sjb * We're at the end -- maybe. If the oldest 448178479Sjb * record is non-zero, we need to wrap. 449178479Sjb */ 450178479Sjb if (buf->dtbd_oldest != 0) { 451178479Sjb offs = 0; 452178479Sjb } else { 453178479Sjb goto out; 454178479Sjb } 455178479Sjb } 456178479Sjb 457178479Sjb next = *(uint32_t *)((uintptr_t)buf->dtbd_data + offs); 458178479Sjb 459178479Sjb if (next == DTRACE_EPIDNONE) 460178479Sjb offs += sizeof (id); 461178479Sjb } while (next == DTRACE_EPIDNONE); 462178479Sjb 463178479Sjb if ((rval = dt_epid_lookup(dtp, next, &nepd, &npd)) != 0) 464178479Sjb return (rval); 465178479Sjb 466178479Sjb if (next != id && npd->dtpd_id == pd->dtpd_id) 467178479Sjb flow = DTRACEFLOW_NONE; 468178479Sjb } 469178479Sjb 470178479Sjbout: 471178479Sjb if (flow == DTRACEFLOW_ENTRY || flow == DTRACEFLOW_RETURN) { 472178479Sjb data->dtpda_prefix = str; 473178479Sjb } else { 474178479Sjb data->dtpda_prefix = "| "; 475178479Sjb } 476178479Sjb 477178479Sjb if (flow == DTRACEFLOW_RETURN && data->dtpda_indent > 0) 478178479Sjb data->dtpda_indent -= 2; 479178479Sjb 480178479Sjb data->dtpda_flow = flow; 481178479Sjb 482178479Sjb return (0); 483178479Sjb} 484178479Sjb 485178479Sjbstatic int 486178479Sjbdt_nullprobe() 487178479Sjb{ 488178479Sjb return (DTRACE_CONSUME_THIS); 489178479Sjb} 490178479Sjb 491178479Sjbstatic int 492178479Sjbdt_nullrec() 493178479Sjb{ 494178479Sjb return (DTRACE_CONSUME_NEXT); 495178479Sjb} 496178479Sjb 497178479Sjbint 498178479Sjbdt_print_quantline(dtrace_hdl_t *dtp, FILE *fp, int64_t val, 499178479Sjb uint64_t normal, long double total, char positives, char negatives) 500178479Sjb{ 501178479Sjb long double f; 502178479Sjb uint_t depth, len = 40; 503178479Sjb 504178479Sjb const char *ats = "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"; 505178479Sjb const char *spaces = " "; 506178479Sjb 507178479Sjb assert(strlen(ats) == len && strlen(spaces) == len); 508178479Sjb assert(!(total == 0 && (positives || negatives))); 509178479Sjb assert(!(val < 0 && !negatives)); 510178479Sjb assert(!(val > 0 && !positives)); 511178479Sjb assert(!(val != 0 && total == 0)); 512178479Sjb 513178479Sjb if (!negatives) { 514178479Sjb if (positives) { 515178479Sjb f = (dt_fabsl((long double)val) * len) / total; 516178479Sjb depth = (uint_t)(f + 0.5); 517178479Sjb } else { 518178479Sjb depth = 0; 519178479Sjb } 520178479Sjb 521178479Sjb return (dt_printf(dtp, fp, "|%s%s %-9lld\n", ats + len - depth, 522178479Sjb spaces + depth, (long long)val / normal)); 523178479Sjb } 524178479Sjb 525178479Sjb if (!positives) { 526178479Sjb f = (dt_fabsl((long double)val) * len) / total; 527178479Sjb depth = (uint_t)(f + 0.5); 528178479Sjb 529178479Sjb return (dt_printf(dtp, fp, "%s%s| %-9lld\n", spaces + depth, 530178479Sjb ats + len - depth, (long long)val / normal)); 531178479Sjb } 532178479Sjb 533178479Sjb /* 534178479Sjb * If we're here, we have both positive and negative bucket values. 535178479Sjb * To express this graphically, we're going to generate both positive 536178479Sjb * and negative bars separated by a centerline. These bars are half 537178479Sjb * the size of normal quantize()/lquantize() bars, so we divide the 538178479Sjb * length in half before calculating the bar length. 539178479Sjb */ 540178479Sjb len /= 2; 541178479Sjb ats = &ats[len]; 542178479Sjb spaces = &spaces[len]; 543178479Sjb 544178479Sjb f = (dt_fabsl((long double)val) * len) / total; 545178479Sjb depth = (uint_t)(f + 0.5); 546178479Sjb 547178479Sjb if (val <= 0) { 548178479Sjb return (dt_printf(dtp, fp, "%s%s|%*s %-9lld\n", spaces + depth, 549178479Sjb ats + len - depth, len, "", (long long)val / normal)); 550178479Sjb } else { 551178479Sjb return (dt_printf(dtp, fp, "%20s|%s%s %-9lld\n", "", 552178479Sjb ats + len - depth, spaces + depth, 553178479Sjb (long long)val / normal)); 554178479Sjb } 555178479Sjb} 556178479Sjb 557178479Sjbint 558178479Sjbdt_print_quantize(dtrace_hdl_t *dtp, FILE *fp, const void *addr, 559178479Sjb size_t size, uint64_t normal) 560178479Sjb{ 561178479Sjb const int64_t *data = addr; 562178479Sjb int i, first_bin = 0, last_bin = DTRACE_QUANTIZE_NBUCKETS - 1; 563178479Sjb long double total = 0; 564178479Sjb char positives = 0, negatives = 0; 565178479Sjb 566178479Sjb if (size != DTRACE_QUANTIZE_NBUCKETS * sizeof (uint64_t)) 567178479Sjb return (dt_set_errno(dtp, EDT_DMISMATCH)); 568178479Sjb 569178479Sjb while (first_bin < DTRACE_QUANTIZE_NBUCKETS - 1 && data[first_bin] == 0) 570178479Sjb first_bin++; 571178479Sjb 572178479Sjb if (first_bin == DTRACE_QUANTIZE_NBUCKETS - 1) { 573178479Sjb /* 574178479Sjb * There isn't any data. This is possible if (and only if) 575178479Sjb * negative increment values have been used. In this case, 576178479Sjb * we'll print the buckets around 0. 577178479Sjb */ 578178479Sjb first_bin = DTRACE_QUANTIZE_ZEROBUCKET - 1; 579178479Sjb last_bin = DTRACE_QUANTIZE_ZEROBUCKET + 1; 580178479Sjb } else { 581178479Sjb if (first_bin > 0) 582178479Sjb first_bin--; 583178479Sjb 584178479Sjb while (last_bin > 0 && data[last_bin] == 0) 585178479Sjb last_bin--; 586178479Sjb 587178479Sjb if (last_bin < DTRACE_QUANTIZE_NBUCKETS - 1) 588178479Sjb last_bin++; 589178479Sjb } 590178479Sjb 591178479Sjb for (i = first_bin; i <= last_bin; i++) { 592178479Sjb positives |= (data[i] > 0); 593178479Sjb negatives |= (data[i] < 0); 594178479Sjb total += dt_fabsl((long double)data[i]); 595178479Sjb } 596178479Sjb 597178479Sjb if (dt_printf(dtp, fp, "\n%16s %41s %-9s\n", "value", 598178479Sjb "------------- Distribution -------------", "count") < 0) 599178479Sjb return (-1); 600178479Sjb 601178479Sjb for (i = first_bin; i <= last_bin; i++) { 602178479Sjb if (dt_printf(dtp, fp, "%16lld ", 603178479Sjb (long long)DTRACE_QUANTIZE_BUCKETVAL(i)) < 0) 604178479Sjb return (-1); 605178479Sjb 606178479Sjb if (dt_print_quantline(dtp, fp, data[i], normal, total, 607178479Sjb positives, negatives) < 0) 608178479Sjb return (-1); 609178479Sjb } 610178479Sjb 611178479Sjb return (0); 612178479Sjb} 613178479Sjb 614178479Sjbint 615178479Sjbdt_print_lquantize(dtrace_hdl_t *dtp, FILE *fp, const void *addr, 616178479Sjb size_t size, uint64_t normal) 617178479Sjb{ 618178479Sjb const int64_t *data = addr; 619178479Sjb int i, first_bin, last_bin, base; 620178479Sjb uint64_t arg; 621178479Sjb long double total = 0; 622178479Sjb uint16_t step, levels; 623178479Sjb char positives = 0, negatives = 0; 624178479Sjb 625178479Sjb if (size < sizeof (uint64_t)) 626178479Sjb return (dt_set_errno(dtp, EDT_DMISMATCH)); 627178479Sjb 628178479Sjb arg = *data++; 629178479Sjb size -= sizeof (uint64_t); 630178479Sjb 631178479Sjb base = DTRACE_LQUANTIZE_BASE(arg); 632178479Sjb step = DTRACE_LQUANTIZE_STEP(arg); 633178479Sjb levels = DTRACE_LQUANTIZE_LEVELS(arg); 634178479Sjb 635178479Sjb first_bin = 0; 636178479Sjb last_bin = levels + 1; 637178479Sjb 638178479Sjb if (size != sizeof (uint64_t) * (levels + 2)) 639178479Sjb return (dt_set_errno(dtp, EDT_DMISMATCH)); 640178479Sjb 641178479Sjb while (first_bin <= levels + 1 && data[first_bin] == 0) 642178479Sjb first_bin++; 643178479Sjb 644178479Sjb if (first_bin > levels + 1) { 645178479Sjb first_bin = 0; 646178479Sjb last_bin = 2; 647178479Sjb } else { 648178479Sjb if (first_bin > 0) 649178479Sjb first_bin--; 650178479Sjb 651178479Sjb while (last_bin > 0 && data[last_bin] == 0) 652178479Sjb last_bin--; 653178479Sjb 654178479Sjb if (last_bin < levels + 1) 655178479Sjb last_bin++; 656178479Sjb } 657178479Sjb 658178479Sjb for (i = first_bin; i <= last_bin; i++) { 659178479Sjb positives |= (data[i] > 0); 660178479Sjb negatives |= (data[i] < 0); 661178479Sjb total += dt_fabsl((long double)data[i]); 662178479Sjb } 663178479Sjb 664178479Sjb if (dt_printf(dtp, fp, "\n%16s %41s %-9s\n", "value", 665178479Sjb "------------- Distribution -------------", "count") < 0) 666178479Sjb return (-1); 667178479Sjb 668178479Sjb for (i = first_bin; i <= last_bin; i++) { 669178479Sjb char c[32]; 670178479Sjb int err; 671178479Sjb 672178479Sjb if (i == 0) { 673178479Sjb (void) snprintf(c, sizeof (c), "< %d", 674178479Sjb base / (uint32_t)normal); 675178479Sjb err = dt_printf(dtp, fp, "%16s ", c); 676178479Sjb } else if (i == levels + 1) { 677178479Sjb (void) snprintf(c, sizeof (c), ">= %d", 678178479Sjb base + (levels * step)); 679178479Sjb err = dt_printf(dtp, fp, "%16s ", c); 680178479Sjb } else { 681178479Sjb err = dt_printf(dtp, fp, "%16d ", 682178479Sjb base + (i - 1) * step); 683178479Sjb } 684178479Sjb 685178479Sjb if (err < 0 || dt_print_quantline(dtp, fp, data[i], normal, 686178479Sjb total, positives, negatives) < 0) 687178479Sjb return (-1); 688178479Sjb } 689178479Sjb 690178479Sjb return (0); 691178479Sjb} 692178479Sjb 693237624Spfgint 694237624Spfgdt_print_llquantize(dtrace_hdl_t *dtp, FILE *fp, const void *addr, 695237624Spfg size_t size, uint64_t normal) 696237624Spfg{ 697237624Spfg int i, first_bin, last_bin, bin = 1, order, levels; 698237624Spfg uint16_t factor, low, high, nsteps; 699237624Spfg const int64_t *data = addr; 700237624Spfg int64_t value = 1, next, step; 701237624Spfg char positives = 0, negatives = 0; 702237624Spfg long double total = 0; 703237624Spfg uint64_t arg; 704237624Spfg char c[32]; 705237624Spfg 706237624Spfg if (size < sizeof (uint64_t)) 707237624Spfg return (dt_set_errno(dtp, EDT_DMISMATCH)); 708237624Spfg 709237624Spfg arg = *data++; 710237624Spfg size -= sizeof (uint64_t); 711237624Spfg 712237624Spfg factor = DTRACE_LLQUANTIZE_FACTOR(arg); 713237624Spfg low = DTRACE_LLQUANTIZE_LOW(arg); 714237624Spfg high = DTRACE_LLQUANTIZE_HIGH(arg); 715237624Spfg nsteps = DTRACE_LLQUANTIZE_NSTEP(arg); 716237624Spfg 717237624Spfg /* 718237624Spfg * We don't expect to be handed invalid llquantize() parameters here, 719237624Spfg * but sanity check them (to a degree) nonetheless. 720237624Spfg */ 721237624Spfg if (size > INT32_MAX || factor < 2 || low >= high || 722237624Spfg nsteps == 0 || factor > nsteps) 723237624Spfg return (dt_set_errno(dtp, EDT_DMISMATCH)); 724237624Spfg 725237624Spfg levels = (int)size / sizeof (uint64_t); 726237624Spfg 727237624Spfg first_bin = 0; 728237624Spfg last_bin = levels - 1; 729237624Spfg 730237624Spfg while (first_bin < levels && data[first_bin] == 0) 731237624Spfg first_bin++; 732237624Spfg 733237624Spfg if (first_bin == levels) { 734237624Spfg first_bin = 0; 735237624Spfg last_bin = 1; 736237624Spfg } else { 737237624Spfg if (first_bin > 0) 738237624Spfg first_bin--; 739237624Spfg 740237624Spfg while (last_bin > 0 && data[last_bin] == 0) 741237624Spfg last_bin--; 742237624Spfg 743237624Spfg if (last_bin < levels - 1) 744237624Spfg last_bin++; 745237624Spfg } 746237624Spfg 747237624Spfg for (i = first_bin; i <= last_bin; i++) { 748237624Spfg positives |= (data[i] > 0); 749237624Spfg negatives |= (data[i] < 0); 750237624Spfg total += dt_fabsl((long double)data[i]); 751237624Spfg } 752237624Spfg 753237624Spfg if (dt_printf(dtp, fp, "\n%16s %41s %-9s\n", "value", 754237624Spfg "------------- Distribution -------------", "count") < 0) 755237624Spfg return (-1); 756237624Spfg 757237624Spfg for (order = 0; order < low; order++) 758237624Spfg value *= factor; 759237624Spfg 760237624Spfg next = value * factor; 761237624Spfg step = next > nsteps ? next / nsteps : 1; 762237624Spfg 763237624Spfg if (first_bin == 0) { 764237624Spfg (void) snprintf(c, sizeof (c), "< %lld", value); 765237624Spfg 766237624Spfg if (dt_printf(dtp, fp, "%16s ", c) < 0) 767237624Spfg return (-1); 768237624Spfg 769237624Spfg if (dt_print_quantline(dtp, fp, data[0], normal, 770237624Spfg total, positives, negatives) < 0) 771237624Spfg return (-1); 772237624Spfg } 773237624Spfg 774237624Spfg while (order <= high) { 775237624Spfg if (bin >= first_bin && bin <= last_bin) { 776237624Spfg if (dt_printf(dtp, fp, "%16lld ", (long long)value) < 0) 777237624Spfg return (-1); 778237624Spfg 779237624Spfg if (dt_print_quantline(dtp, fp, data[bin], 780237624Spfg normal, total, positives, negatives) < 0) 781237624Spfg return (-1); 782237624Spfg } 783237624Spfg 784237624Spfg assert(value < next); 785237624Spfg bin++; 786237624Spfg 787237624Spfg if ((value += step) != next) 788237624Spfg continue; 789237624Spfg 790237624Spfg next = value * factor; 791237624Spfg step = next > nsteps ? next / nsteps : 1; 792237624Spfg order++; 793237624Spfg } 794237624Spfg 795237624Spfg if (last_bin < bin) 796237624Spfg return (0); 797237624Spfg 798237624Spfg assert(last_bin == bin); 799237624Spfg (void) snprintf(c, sizeof (c), ">= %lld", value); 800237624Spfg 801237624Spfg if (dt_printf(dtp, fp, "%16s ", c) < 0) 802237624Spfg return (-1); 803237624Spfg 804237624Spfg return (dt_print_quantline(dtp, fp, data[bin], normal, 805237624Spfg total, positives, negatives)); 806237624Spfg} 807237624Spfg 808178479Sjb/*ARGSUSED*/ 809178479Sjbstatic int 810178479Sjbdt_print_average(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr, 811178479Sjb size_t size, uint64_t normal) 812178479Sjb{ 813178479Sjb /* LINTED - alignment */ 814178479Sjb int64_t *data = (int64_t *)addr; 815178479Sjb 816178479Sjb return (dt_printf(dtp, fp, " %16lld", data[0] ? 817178479Sjb (long long)(data[1] / (int64_t)normal / data[0]) : 0)); 818178479Sjb} 819178479Sjb 820178479Sjb/*ARGSUSED*/ 821178479Sjbstatic int 822178479Sjbdt_print_stddev(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr, 823178479Sjb size_t size, uint64_t normal) 824178479Sjb{ 825178479Sjb /* LINTED - alignment */ 826178479Sjb uint64_t *data = (uint64_t *)addr; 827178479Sjb 828178479Sjb return (dt_printf(dtp, fp, " %16llu", data[0] ? 829178479Sjb (unsigned long long) dt_stddev(data, normal) : 0)); 830178479Sjb} 831178479Sjb 832178479Sjb/*ARGSUSED*/ 833178479Sjbint 834178479Sjbdt_print_bytes(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr, 835178576Sjb size_t nbytes, int width, int quiet, int raw) 836178479Sjb{ 837178479Sjb /* 838178479Sjb * If the byte stream is a series of printable characters, followed by 839178479Sjb * a terminating byte, we print it out as a string. Otherwise, we 840178479Sjb * assume that it's something else and just print the bytes. 841178479Sjb */ 842178479Sjb int i, j, margin = 5; 843178479Sjb char *c = (char *)addr; 844178479Sjb 845178479Sjb if (nbytes == 0) 846178479Sjb return (0); 847178479Sjb 848178576Sjb if (raw || dtp->dt_options[DTRACEOPT_RAWBYTES] != DTRACEOPT_UNSET) 849178479Sjb goto raw; 850178479Sjb 851178479Sjb for (i = 0; i < nbytes; i++) { 852178479Sjb /* 853178479Sjb * We define a "printable character" to be one for which 854178479Sjb * isprint(3C) returns non-zero, isspace(3C) returns non-zero, 855178479Sjb * or a character which is either backspace or the bell. 856178479Sjb * Backspace and the bell are regrettably special because 857178479Sjb * they fail the first two tests -- and yet they are entirely 858178479Sjb * printable. These are the only two control characters that 859178479Sjb * have meaning for the terminal and for which isprint(3C) and 860178479Sjb * isspace(3C) return 0. 861178479Sjb */ 862178479Sjb if (isprint(c[i]) || isspace(c[i]) || 863178479Sjb c[i] == '\b' || c[i] == '\a') 864178479Sjb continue; 865178479Sjb 866178479Sjb if (c[i] == '\0' && i > 0) { 867178479Sjb /* 868178479Sjb * This looks like it might be a string. Before we 869178479Sjb * assume that it is indeed a string, check the 870178479Sjb * remainder of the byte range; if it contains 871178479Sjb * additional non-nul characters, we'll assume that 872178479Sjb * it's a binary stream that just happens to look like 873178479Sjb * a string, and we'll print out the individual bytes. 874178479Sjb */ 875178479Sjb for (j = i + 1; j < nbytes; j++) { 876178479Sjb if (c[j] != '\0') 877178479Sjb break; 878178479Sjb } 879178479Sjb 880178479Sjb if (j != nbytes) 881178479Sjb break; 882178479Sjb 883178479Sjb if (quiet) 884178479Sjb return (dt_printf(dtp, fp, "%s", c)); 885178479Sjb else 886178479Sjb return (dt_printf(dtp, fp, " %-*s", width, c)); 887178479Sjb } 888178479Sjb 889178479Sjb break; 890178479Sjb } 891178479Sjb 892178479Sjb if (i == nbytes) { 893178479Sjb /* 894178479Sjb * The byte range is all printable characters, but there is 895178479Sjb * no trailing nul byte. We'll assume that it's a string and 896178479Sjb * print it as such. 897178479Sjb */ 898178479Sjb char *s = alloca(nbytes + 1); 899178479Sjb bcopy(c, s, nbytes); 900178479Sjb s[nbytes] = '\0'; 901178479Sjb return (dt_printf(dtp, fp, " %-*s", width, s)); 902178479Sjb } 903178479Sjb 904178479Sjbraw: 905178479Sjb if (dt_printf(dtp, fp, "\n%*s ", margin, "") < 0) 906178479Sjb return (-1); 907178479Sjb 908178479Sjb for (i = 0; i < 16; i++) 909178479Sjb if (dt_printf(dtp, fp, " %c", "0123456789abcdef"[i]) < 0) 910178479Sjb return (-1); 911178479Sjb 912178479Sjb if (dt_printf(dtp, fp, " 0123456789abcdef\n") < 0) 913178479Sjb return (-1); 914178479Sjb 915178479Sjb 916178479Sjb for (i = 0; i < nbytes; i += 16) { 917178479Sjb if (dt_printf(dtp, fp, "%*s%5x:", margin, "", i) < 0) 918178479Sjb return (-1); 919178479Sjb 920178479Sjb for (j = i; j < i + 16 && j < nbytes; j++) { 921178479Sjb if (dt_printf(dtp, fp, " %02x", (uchar_t)c[j]) < 0) 922178479Sjb return (-1); 923178479Sjb } 924178479Sjb 925178479Sjb while (j++ % 16) { 926178479Sjb if (dt_printf(dtp, fp, " ") < 0) 927178479Sjb return (-1); 928178479Sjb } 929178479Sjb 930178479Sjb if (dt_printf(dtp, fp, " ") < 0) 931178479Sjb return (-1); 932178479Sjb 933178479Sjb for (j = i; j < i + 16 && j < nbytes; j++) { 934178479Sjb if (dt_printf(dtp, fp, "%c", 935178479Sjb c[j] < ' ' || c[j] > '~' ? '.' : c[j]) < 0) 936178479Sjb return (-1); 937178479Sjb } 938178479Sjb 939178479Sjb if (dt_printf(dtp, fp, "\n") < 0) 940178479Sjb return (-1); 941178479Sjb } 942178479Sjb 943178479Sjb return (0); 944178479Sjb} 945178479Sjb 946178479Sjbint 947178479Sjbdt_print_stack(dtrace_hdl_t *dtp, FILE *fp, const char *format, 948178479Sjb caddr_t addr, int depth, int size) 949178479Sjb{ 950178479Sjb dtrace_syminfo_t dts; 951178479Sjb GElf_Sym sym; 952178479Sjb int i, indent; 953178479Sjb char c[PATH_MAX * 2]; 954178479Sjb uint64_t pc; 955178479Sjb 956178479Sjb if (dt_printf(dtp, fp, "\n") < 0) 957178479Sjb return (-1); 958178479Sjb 959178479Sjb if (format == NULL) 960178479Sjb format = "%s"; 961178479Sjb 962178479Sjb if (dtp->dt_options[DTRACEOPT_STACKINDENT] != DTRACEOPT_UNSET) 963178479Sjb indent = (int)dtp->dt_options[DTRACEOPT_STACKINDENT]; 964178479Sjb else 965178479Sjb indent = _dtrace_stkindent; 966178479Sjb 967178479Sjb for (i = 0; i < depth; i++) { 968178479Sjb switch (size) { 969178479Sjb case sizeof (uint32_t): 970178479Sjb /* LINTED - alignment */ 971178479Sjb pc = *((uint32_t *)addr); 972178479Sjb break; 973178479Sjb 974178479Sjb case sizeof (uint64_t): 975178479Sjb /* LINTED - alignment */ 976178479Sjb pc = *((uint64_t *)addr); 977178479Sjb break; 978178479Sjb 979178479Sjb default: 980178479Sjb return (dt_set_errno(dtp, EDT_BADSTACKPC)); 981178479Sjb } 982178479Sjb 983178576Sjb if (pc == 0) 984178479Sjb break; 985178479Sjb 986178479Sjb addr += size; 987178479Sjb 988178479Sjb if (dt_printf(dtp, fp, "%*s", indent, "") < 0) 989178479Sjb return (-1); 990178479Sjb 991178479Sjb if (dtrace_lookup_by_addr(dtp, pc, &sym, &dts) == 0) { 992178479Sjb if (pc > sym.st_value) { 993178479Sjb (void) snprintf(c, sizeof (c), "%s`%s+0x%llx", 994178479Sjb dts.dts_object, dts.dts_name, 995228579Sdim (u_longlong_t)(pc - sym.st_value)); 996178479Sjb } else { 997178479Sjb (void) snprintf(c, sizeof (c), "%s`%s", 998178479Sjb dts.dts_object, dts.dts_name); 999178479Sjb } 1000178479Sjb } else { 1001178479Sjb /* 1002178479Sjb * We'll repeat the lookup, but this time we'll specify 1003178479Sjb * a NULL GElf_Sym -- indicating that we're only 1004178479Sjb * interested in the containing module. 1005178479Sjb */ 1006178479Sjb if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0) { 1007178479Sjb (void) snprintf(c, sizeof (c), "%s`0x%llx", 1008228579Sdim dts.dts_object, (u_longlong_t)pc); 1009178479Sjb } else { 1010228579Sdim (void) snprintf(c, sizeof (c), "0x%llx", 1011228579Sdim (u_longlong_t)pc); 1012178479Sjb } 1013178479Sjb } 1014178479Sjb 1015178479Sjb if (dt_printf(dtp, fp, format, c) < 0) 1016178479Sjb return (-1); 1017178479Sjb 1018178479Sjb if (dt_printf(dtp, fp, "\n") < 0) 1019178479Sjb return (-1); 1020178479Sjb } 1021178479Sjb 1022178479Sjb return (0); 1023178479Sjb} 1024178479Sjb 1025178479Sjbint 1026178479Sjbdt_print_ustack(dtrace_hdl_t *dtp, FILE *fp, const char *format, 1027178479Sjb caddr_t addr, uint64_t arg) 1028178479Sjb{ 1029178479Sjb /* LINTED - alignment */ 1030178479Sjb uint64_t *pc = (uint64_t *)addr; 1031178479Sjb uint32_t depth = DTRACE_USTACK_NFRAMES(arg); 1032178479Sjb uint32_t strsize = DTRACE_USTACK_STRSIZE(arg); 1033178479Sjb const char *strbase = addr + (depth + 1) * sizeof (uint64_t); 1034178479Sjb const char *str = strsize ? strbase : NULL; 1035178479Sjb int err = 0; 1036178479Sjb 1037178479Sjb char name[PATH_MAX], objname[PATH_MAX], c[PATH_MAX * 2]; 1038178479Sjb struct ps_prochandle *P; 1039178479Sjb GElf_Sym sym; 1040178479Sjb int i, indent; 1041178479Sjb pid_t pid; 1042178479Sjb 1043178479Sjb if (depth == 0) 1044178479Sjb return (0); 1045178479Sjb 1046178479Sjb pid = (pid_t)*pc++; 1047178479Sjb 1048178479Sjb if (dt_printf(dtp, fp, "\n") < 0) 1049178479Sjb return (-1); 1050178479Sjb 1051178479Sjb if (format == NULL) 1052178479Sjb format = "%s"; 1053178479Sjb 1054178479Sjb if (dtp->dt_options[DTRACEOPT_STACKINDENT] != DTRACEOPT_UNSET) 1055178479Sjb indent = (int)dtp->dt_options[DTRACEOPT_STACKINDENT]; 1056178479Sjb else 1057178479Sjb indent = _dtrace_stkindent; 1058178479Sjb 1059178479Sjb /* 1060178479Sjb * Ultimately, we need to add an entry point in the library vector for 1061178479Sjb * determining <symbol, offset> from <pid, address>. For now, if 1062178479Sjb * this is a vector open, we just print the raw address or string. 1063178479Sjb */ 1064178479Sjb if (dtp->dt_vector == NULL) 1065178479Sjb P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0); 1066178479Sjb else 1067178479Sjb P = NULL; 1068178479Sjb 1069178479Sjb if (P != NULL) 1070178479Sjb dt_proc_lock(dtp, P); /* lock handle while we perform lookups */ 1071178479Sjb 1072178576Sjb for (i = 0; i < depth && pc[i] != 0; i++) { 1073178479Sjb const prmap_t *map; 1074178479Sjb 1075178479Sjb if ((err = dt_printf(dtp, fp, "%*s", indent, "")) < 0) 1076178479Sjb break; 1077178479Sjb 1078178479Sjb if (P != NULL && Plookup_by_addr(P, pc[i], 1079178479Sjb name, sizeof (name), &sym) == 0) { 1080178479Sjb (void) Pobjname(P, pc[i], objname, sizeof (objname)); 1081178479Sjb 1082178479Sjb if (pc[i] > sym.st_value) { 1083178479Sjb (void) snprintf(c, sizeof (c), 1084178479Sjb "%s`%s+0x%llx", dt_basename(objname), name, 1085178479Sjb (u_longlong_t)(pc[i] - sym.st_value)); 1086178479Sjb } else { 1087178479Sjb (void) snprintf(c, sizeof (c), 1088178479Sjb "%s`%s", dt_basename(objname), name); 1089178479Sjb } 1090178479Sjb } else if (str != NULL && str[0] != '\0' && str[0] != '@' && 1091178479Sjb (P != NULL && ((map = Paddr_to_map(P, pc[i])) == NULL || 1092178479Sjb (map->pr_mflags & MA_WRITE)))) { 1093178479Sjb /* 1094178479Sjb * If the current string pointer in the string table 1095178479Sjb * does not point to an empty string _and_ the program 1096178479Sjb * counter falls in a writable region, we'll use the 1097178479Sjb * string from the string table instead of the raw 1098178479Sjb * address. This last condition is necessary because 1099178479Sjb * some (broken) ustack helpers will return a string 1100178479Sjb * even for a program counter that they can't 1101178479Sjb * identify. If we have a string for a program 1102178479Sjb * counter that falls in a segment that isn't 1103178479Sjb * writable, we assume that we have fallen into this 1104178479Sjb * case and we refuse to use the string. 1105178479Sjb */ 1106178479Sjb (void) snprintf(c, sizeof (c), "%s", str); 1107178479Sjb } else { 1108178479Sjb if (P != NULL && Pobjname(P, pc[i], objname, 1109178576Sjb sizeof (objname)) != 0) { 1110178479Sjb (void) snprintf(c, sizeof (c), "%s`0x%llx", 1111178479Sjb dt_basename(objname), (u_longlong_t)pc[i]); 1112178479Sjb } else { 1113178479Sjb (void) snprintf(c, sizeof (c), "0x%llx", 1114178479Sjb (u_longlong_t)pc[i]); 1115178479Sjb } 1116178479Sjb } 1117178479Sjb 1118178479Sjb if ((err = dt_printf(dtp, fp, format, c)) < 0) 1119178479Sjb break; 1120178479Sjb 1121178479Sjb if ((err = dt_printf(dtp, fp, "\n")) < 0) 1122178479Sjb break; 1123178479Sjb 1124178479Sjb if (str != NULL && str[0] == '@') { 1125178479Sjb /* 1126178479Sjb * If the first character of the string is an "at" sign, 1127178479Sjb * then the string is inferred to be an annotation -- 1128178479Sjb * and it is printed out beneath the frame and offset 1129178479Sjb * with brackets. 1130178479Sjb */ 1131178479Sjb if ((err = dt_printf(dtp, fp, "%*s", indent, "")) < 0) 1132178479Sjb break; 1133178479Sjb 1134178479Sjb (void) snprintf(c, sizeof (c), " [ %s ]", &str[1]); 1135178479Sjb 1136178479Sjb if ((err = dt_printf(dtp, fp, format, c)) < 0) 1137178479Sjb break; 1138178479Sjb 1139178479Sjb if ((err = dt_printf(dtp, fp, "\n")) < 0) 1140178479Sjb break; 1141178479Sjb } 1142178479Sjb 1143178479Sjb if (str != NULL) { 1144178479Sjb str += strlen(str) + 1; 1145178479Sjb if (str - strbase >= strsize) 1146178479Sjb str = NULL; 1147178479Sjb } 1148178479Sjb } 1149178479Sjb 1150178479Sjb if (P != NULL) { 1151178479Sjb dt_proc_unlock(dtp, P); 1152178479Sjb dt_proc_release(dtp, P); 1153178479Sjb } 1154178479Sjb 1155178479Sjb return (err); 1156178479Sjb} 1157178479Sjb 1158178479Sjbstatic int 1159178479Sjbdt_print_usym(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr, dtrace_actkind_t act) 1160178479Sjb{ 1161178479Sjb /* LINTED - alignment */ 1162178479Sjb uint64_t pid = ((uint64_t *)addr)[0]; 1163178479Sjb /* LINTED - alignment */ 1164178479Sjb uint64_t pc = ((uint64_t *)addr)[1]; 1165178479Sjb const char *format = " %-50s"; 1166178479Sjb char *s; 1167178479Sjb int n, len = 256; 1168178479Sjb 1169178479Sjb if (act == DTRACEACT_USYM && dtp->dt_vector == NULL) { 1170178479Sjb struct ps_prochandle *P; 1171178479Sjb 1172178479Sjb if ((P = dt_proc_grab(dtp, pid, 1173178479Sjb PGRAB_RDONLY | PGRAB_FORCE, 0)) != NULL) { 1174178479Sjb GElf_Sym sym; 1175178479Sjb 1176178479Sjb dt_proc_lock(dtp, P); 1177178479Sjb 1178178479Sjb if (Plookup_by_addr(P, pc, NULL, 0, &sym) == 0) 1179178479Sjb pc = sym.st_value; 1180178479Sjb 1181178479Sjb dt_proc_unlock(dtp, P); 1182178479Sjb dt_proc_release(dtp, P); 1183178479Sjb } 1184178479Sjb } 1185178479Sjb 1186178479Sjb do { 1187178479Sjb n = len; 1188178479Sjb s = alloca(n); 1189210767Srpaulo } while ((len = dtrace_uaddr2str(dtp, pid, pc, s, n)) > n); 1190178479Sjb 1191178479Sjb return (dt_printf(dtp, fp, format, s)); 1192178479Sjb} 1193178479Sjb 1194178479Sjbint 1195178479Sjbdt_print_umod(dtrace_hdl_t *dtp, FILE *fp, const char *format, caddr_t addr) 1196178479Sjb{ 1197178479Sjb /* LINTED - alignment */ 1198178479Sjb uint64_t pid = ((uint64_t *)addr)[0]; 1199178479Sjb /* LINTED - alignment */ 1200178479Sjb uint64_t pc = ((uint64_t *)addr)[1]; 1201178479Sjb int err = 0; 1202178479Sjb 1203178479Sjb char objname[PATH_MAX], c[PATH_MAX * 2]; 1204178479Sjb struct ps_prochandle *P; 1205178479Sjb 1206178479Sjb if (format == NULL) 1207178479Sjb format = " %-50s"; 1208178479Sjb 1209178479Sjb /* 1210178479Sjb * See the comment in dt_print_ustack() for the rationale for 1211178479Sjb * printing raw addresses in the vectored case. 1212178479Sjb */ 1213178479Sjb if (dtp->dt_vector == NULL) 1214178479Sjb P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0); 1215178479Sjb else 1216178479Sjb P = NULL; 1217178479Sjb 1218178479Sjb if (P != NULL) 1219178479Sjb dt_proc_lock(dtp, P); /* lock handle while we perform lookups */ 1220178479Sjb 1221178576Sjb if (P != NULL && Pobjname(P, pc, objname, sizeof (objname)) != 0) { 1222178479Sjb (void) snprintf(c, sizeof (c), "%s", dt_basename(objname)); 1223178479Sjb } else { 1224178479Sjb (void) snprintf(c, sizeof (c), "0x%llx", (u_longlong_t)pc); 1225178479Sjb } 1226178479Sjb 1227178479Sjb err = dt_printf(dtp, fp, format, c); 1228178479Sjb 1229178479Sjb if (P != NULL) { 1230178479Sjb dt_proc_unlock(dtp, P); 1231178479Sjb dt_proc_release(dtp, P); 1232178479Sjb } 1233178479Sjb 1234178479Sjb return (err); 1235178479Sjb} 1236178479Sjb 1237178576Sjbint 1238178576Sjbdt_print_memory(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr) 1239178576Sjb{ 1240178576Sjb int quiet = (dtp->dt_options[DTRACEOPT_QUIET] != DTRACEOPT_UNSET); 1241178576Sjb size_t nbytes = *((uintptr_t *) addr); 1242178576Sjb 1243178576Sjb return (dt_print_bytes(dtp, fp, addr + sizeof(uintptr_t), 1244178576Sjb nbytes, 50, quiet, 1)); 1245178576Sjb} 1246178576Sjb 1247178576Sjbtypedef struct dt_type_cbdata { 1248178576Sjb dtrace_hdl_t *dtp; 1249178576Sjb dtrace_typeinfo_t dtt; 1250178576Sjb caddr_t addr; 1251178576Sjb caddr_t addrend; 1252178576Sjb const char *name; 1253178576Sjb int f_type; 1254178576Sjb int indent; 1255178576Sjb int type_width; 1256178576Sjb int name_width; 1257178576Sjb FILE *fp; 1258178576Sjb} dt_type_cbdata_t; 1259178576Sjb 1260178576Sjbstatic int dt_print_type_data(dt_type_cbdata_t *, ctf_id_t); 1261178576Sjb 1262178479Sjbstatic int 1263178576Sjbdt_print_type_member(const char *name, ctf_id_t type, ulong_t off, void *arg) 1264178576Sjb{ 1265178576Sjb dt_type_cbdata_t cbdata; 1266178576Sjb dt_type_cbdata_t *cbdatap = arg; 1267178576Sjb ssize_t ssz; 1268178576Sjb 1269178576Sjb if ((ssz = ctf_type_size(cbdatap->dtt.dtt_ctfp, type)) <= 0) 1270178576Sjb return (0); 1271178576Sjb 1272178576Sjb off /= 8; 1273178576Sjb 1274178576Sjb cbdata = *cbdatap; 1275178576Sjb cbdata.name = name; 1276178576Sjb cbdata.addr += off; 1277178576Sjb cbdata.addrend = cbdata.addr + ssz; 1278178576Sjb 1279178576Sjb return (dt_print_type_data(&cbdata, type)); 1280178576Sjb} 1281178576Sjb 1282178576Sjbstatic int 1283178576Sjbdt_print_type_width(const char *name, ctf_id_t type, ulong_t off, void *arg) 1284178576Sjb{ 1285178576Sjb char buf[DT_TYPE_NAMELEN]; 1286178576Sjb char *p; 1287178576Sjb dt_type_cbdata_t *cbdatap = arg; 1288178576Sjb size_t sz = strlen(name); 1289178576Sjb 1290178576Sjb ctf_type_name(cbdatap->dtt.dtt_ctfp, type, buf, sizeof (buf)); 1291178576Sjb 1292178576Sjb if ((p = strchr(buf, '[')) != NULL) 1293178576Sjb p[-1] = '\0'; 1294178576Sjb else 1295178576Sjb p = ""; 1296178576Sjb 1297178576Sjb sz += strlen(p); 1298178576Sjb 1299178576Sjb if (sz > cbdatap->name_width) 1300178576Sjb cbdatap->name_width = sz; 1301178576Sjb 1302178576Sjb sz = strlen(buf); 1303178576Sjb 1304178576Sjb if (sz > cbdatap->type_width) 1305178576Sjb cbdatap->type_width = sz; 1306178576Sjb 1307178576Sjb return (0); 1308178576Sjb} 1309178576Sjb 1310178576Sjbstatic int 1311178576Sjbdt_print_type_data(dt_type_cbdata_t *cbdatap, ctf_id_t type) 1312178576Sjb{ 1313178576Sjb caddr_t addr = cbdatap->addr; 1314178576Sjb caddr_t addrend = cbdatap->addrend; 1315178576Sjb char buf[DT_TYPE_NAMELEN]; 1316178576Sjb char *p; 1317178576Sjb int cnt = 0; 1318178576Sjb uint_t kind = ctf_type_kind(cbdatap->dtt.dtt_ctfp, type); 1319178576Sjb ssize_t ssz = ctf_type_size(cbdatap->dtt.dtt_ctfp, type); 1320178576Sjb 1321178576Sjb ctf_type_name(cbdatap->dtt.dtt_ctfp, type, buf, sizeof (buf)); 1322178576Sjb 1323178576Sjb if ((p = strchr(buf, '[')) != NULL) 1324178576Sjb p[-1] = '\0'; 1325178576Sjb else 1326178576Sjb p = ""; 1327178576Sjb 1328178576Sjb if (cbdatap->f_type) { 1329178576Sjb int type_width = roundup(cbdatap->type_width + 1, 4); 1330178576Sjb int name_width = roundup(cbdatap->name_width + 1, 4); 1331178576Sjb 1332178576Sjb name_width -= strlen(cbdatap->name); 1333178576Sjb 1334178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "%*s%-*s%s%-*s = ",cbdatap->indent * 4,"",type_width,buf,cbdatap->name,name_width,p); 1335178576Sjb } 1336178576Sjb 1337178576Sjb while (addr < addrend) { 1338178576Sjb dt_type_cbdata_t cbdata; 1339178576Sjb ctf_arinfo_t arinfo; 1340178576Sjb ctf_encoding_t cte; 1341178576Sjb uintptr_t *up; 1342178576Sjb void *vp = addr; 1343178576Sjb cbdata = *cbdatap; 1344178576Sjb cbdata.name = ""; 1345178576Sjb cbdata.addr = addr; 1346178576Sjb cbdata.addrend = addr + ssz; 1347178576Sjb cbdata.f_type = 0; 1348178576Sjb cbdata.indent++; 1349178576Sjb cbdata.type_width = 0; 1350178576Sjb cbdata.name_width = 0; 1351178576Sjb 1352178576Sjb if (cnt > 0) 1353178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "%*s", cbdatap->indent * 4,""); 1354178576Sjb 1355178576Sjb switch (kind) { 1356178576Sjb case CTF_K_INTEGER: 1357178576Sjb if (ctf_type_encoding(cbdatap->dtt.dtt_ctfp, type, &cte) != 0) 1358178576Sjb return (-1); 1359178576Sjb if ((cte.cte_format & CTF_INT_SIGNED) != 0) 1360178576Sjb switch (cte.cte_bits) { 1361178576Sjb case 8: 1362178576Sjb if (isprint(*((char *) vp))) 1363178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "'%c', ", *((char *) vp)); 1364178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "%d (0x%x);\n", *((char *) vp), *((char *) vp)); 1365178576Sjb break; 1366178576Sjb case 16: 1367178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "%hd (0x%hx);\n", *((short *) vp), *((u_short *) vp)); 1368178576Sjb break; 1369178576Sjb case 32: 1370178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "%d (0x%x);\n", *((int *) vp), *((u_int *) vp)); 1371178576Sjb break; 1372178576Sjb case 64: 1373178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "%jd (0x%jx);\n", *((long long *) vp), *((unsigned long long *) vp)); 1374178576Sjb break; 1375178576Sjb default: 1376178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "CTF_K_INTEGER: format %x offset %u bits %u\n",cte.cte_format,cte.cte_offset,cte.cte_bits); 1377178576Sjb break; 1378178576Sjb } 1379178576Sjb else 1380178576Sjb switch (cte.cte_bits) { 1381178576Sjb case 8: 1382178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "%u (0x%x);\n", *((uint8_t *) vp) & 0xff, *((uint8_t *) vp) & 0xff); 1383178576Sjb break; 1384178576Sjb case 16: 1385178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "%hu (0x%hx);\n", *((u_short *) vp), *((u_short *) vp)); 1386178576Sjb break; 1387178576Sjb case 32: 1388178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "%u (0x%x);\n", *((u_int *) vp), *((u_int *) vp)); 1389178576Sjb break; 1390178576Sjb case 64: 1391178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "%ju (0x%jx);\n", *((unsigned long long *) vp), *((unsigned long long *) vp)); 1392178576Sjb break; 1393178576Sjb default: 1394178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "CTF_K_INTEGER: format %x offset %u bits %u\n",cte.cte_format,cte.cte_offset,cte.cte_bits); 1395178576Sjb break; 1396178576Sjb } 1397178576Sjb break; 1398178576Sjb case CTF_K_FLOAT: 1399178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "CTF_K_FLOAT: format %x offset %u bits %u\n",cte.cte_format,cte.cte_offset,cte.cte_bits); 1400178576Sjb break; 1401178576Sjb case CTF_K_POINTER: 1402178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "%p;\n", *((void **) addr)); 1403178576Sjb break; 1404178576Sjb case CTF_K_ARRAY: 1405178576Sjb if (ctf_array_info(cbdatap->dtt.dtt_ctfp, type, &arinfo) != 0) 1406178576Sjb return (-1); 1407178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "{\n%*s",cbdata.indent * 4,""); 1408178576Sjb dt_print_type_data(&cbdata, arinfo.ctr_contents); 1409178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "%*s};\n",cbdatap->indent * 4,""); 1410178576Sjb break; 1411178576Sjb case CTF_K_FUNCTION: 1412178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "CTF_K_FUNCTION:\n"); 1413178576Sjb break; 1414178576Sjb case CTF_K_STRUCT: 1415178576Sjb cbdata.f_type = 1; 1416178576Sjb if (ctf_member_iter(cbdatap->dtt.dtt_ctfp, type, 1417178576Sjb dt_print_type_width, &cbdata) != 0) 1418178576Sjb return (-1); 1419178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "{\n"); 1420178576Sjb if (ctf_member_iter(cbdatap->dtt.dtt_ctfp, type, 1421178576Sjb dt_print_type_member, &cbdata) != 0) 1422178576Sjb return (-1); 1423178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "%*s};\n",cbdatap->indent * 4,""); 1424178576Sjb break; 1425178576Sjb case CTF_K_UNION: 1426178576Sjb cbdata.f_type = 1; 1427178576Sjb if (ctf_member_iter(cbdatap->dtt.dtt_ctfp, type, 1428178576Sjb dt_print_type_width, &cbdata) != 0) 1429178576Sjb return (-1); 1430178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "{\n"); 1431178576Sjb if (ctf_member_iter(cbdatap->dtt.dtt_ctfp, type, 1432178576Sjb dt_print_type_member, &cbdata) != 0) 1433178576Sjb return (-1); 1434178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "%*s};\n",cbdatap->indent * 4,""); 1435178576Sjb break; 1436178576Sjb case CTF_K_ENUM: 1437178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "%s;\n", ctf_enum_name(cbdatap->dtt.dtt_ctfp, type, *((int *) vp))); 1438178576Sjb break; 1439178576Sjb case CTF_K_TYPEDEF: 1440178576Sjb dt_print_type_data(&cbdata, ctf_type_reference(cbdatap->dtt.dtt_ctfp,type)); 1441178576Sjb break; 1442178576Sjb case CTF_K_VOLATILE: 1443178576Sjb if (cbdatap->f_type) 1444178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "volatile "); 1445178576Sjb dt_print_type_data(&cbdata, ctf_type_reference(cbdatap->dtt.dtt_ctfp,type)); 1446178576Sjb break; 1447178576Sjb case CTF_K_CONST: 1448178576Sjb if (cbdatap->f_type) 1449178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "const "); 1450178576Sjb dt_print_type_data(&cbdata, ctf_type_reference(cbdatap->dtt.dtt_ctfp,type)); 1451178576Sjb break; 1452178576Sjb case CTF_K_RESTRICT: 1453178576Sjb if (cbdatap->f_type) 1454178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "restrict "); 1455178576Sjb dt_print_type_data(&cbdata, ctf_type_reference(cbdatap->dtt.dtt_ctfp,type)); 1456178576Sjb break; 1457178576Sjb default: 1458178576Sjb break; 1459178576Sjb } 1460178576Sjb 1461178576Sjb addr += ssz; 1462178576Sjb cnt++; 1463178576Sjb } 1464178576Sjb 1465178576Sjb return (0); 1466178576Sjb} 1467178576Sjb 1468178576Sjbstatic int 1469178576Sjbdt_print_type(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr) 1470178576Sjb{ 1471178576Sjb caddr_t addrend; 1472178576Sjb char *p; 1473178576Sjb dtrace_typeinfo_t dtt; 1474178576Sjb dt_type_cbdata_t cbdata; 1475178576Sjb int num = 0; 1476178576Sjb int quiet = (dtp->dt_options[DTRACEOPT_QUIET] != DTRACEOPT_UNSET); 1477178576Sjb ssize_t ssz; 1478178576Sjb 1479178576Sjb if (!quiet) 1480178576Sjb dt_printf(dtp, fp, "\n"); 1481178576Sjb 1482178576Sjb /* Get the total number of bytes of data buffered. */ 1483178576Sjb size_t nbytes = *((uintptr_t *) addr); 1484178576Sjb addr += sizeof(uintptr_t); 1485178576Sjb 1486178576Sjb /* 1487178576Sjb * Get the size of the type so that we can check that it matches 1488178576Sjb * the CTF data we look up and so that we can figure out how many 1489178576Sjb * type elements are buffered. 1490178576Sjb */ 1491178576Sjb size_t typs = *((uintptr_t *) addr); 1492178576Sjb addr += sizeof(uintptr_t); 1493178576Sjb 1494178576Sjb /* 1495178576Sjb * Point to the type string in the buffer. Get it's string 1496178576Sjb * length and round it up to become the offset to the start 1497178576Sjb * of the buffered type data which we would like to be aligned 1498178576Sjb * for easy access. 1499178576Sjb */ 1500178576Sjb char *strp = (char *) addr; 1501178576Sjb int offset = roundup(strlen(strp) + 1, sizeof(uintptr_t)); 1502178576Sjb 1503178576Sjb /* 1504178576Sjb * The type string might have a format such as 'int [20]'. 1505178576Sjb * Check if there is an array dimension present. 1506178576Sjb */ 1507178576Sjb if ((p = strchr(strp, '[')) != NULL) { 1508178576Sjb /* Strip off the array dimension. */ 1509178576Sjb *p++ = '\0'; 1510178576Sjb 1511178576Sjb for (; *p != '\0' && *p != ']'; p++) 1512178576Sjb num = num * 10 + *p - '0'; 1513178576Sjb } else 1514178576Sjb /* No array dimension, so default. */ 1515178576Sjb num = 1; 1516178576Sjb 1517178576Sjb /* Lookup the CTF type from the type string. */ 1518178576Sjb if (dtrace_lookup_by_type(dtp, DTRACE_OBJ_EVERY, strp, &dtt) < 0) 1519178576Sjb return (-1); 1520178576Sjb 1521178576Sjb /* Offset the buffer address to the start of the data... */ 1522178576Sjb addr += offset; 1523178576Sjb 1524178576Sjb ssz = ctf_type_size(dtt.dtt_ctfp, dtt.dtt_type); 1525178576Sjb 1526178576Sjb if (typs != ssz) { 1527178576Sjb printf("Expected type size from buffer (%lu) to match type size looked up now (%ld)\n", (u_long) typs, (long) ssz); 1528178576Sjb return (-1); 1529178576Sjb } 1530178576Sjb 1531178576Sjb cbdata.dtp = dtp; 1532178576Sjb cbdata.dtt = dtt; 1533178576Sjb cbdata.name = ""; 1534178576Sjb cbdata.addr = addr; 1535178576Sjb cbdata.addrend = addr + nbytes; 1536178576Sjb cbdata.indent = 1; 1537178576Sjb cbdata.f_type = 1; 1538178576Sjb cbdata.type_width = 0; 1539178576Sjb cbdata.name_width = 0; 1540178576Sjb cbdata.fp = fp; 1541178576Sjb 1542178576Sjb return (dt_print_type_data(&cbdata, dtt.dtt_type)); 1543178576Sjb} 1544178576Sjb 1545178576Sjbstatic int 1546178479Sjbdt_print_sym(dtrace_hdl_t *dtp, FILE *fp, const char *format, caddr_t addr) 1547178479Sjb{ 1548178479Sjb /* LINTED - alignment */ 1549178479Sjb uint64_t pc = *((uint64_t *)addr); 1550178479Sjb dtrace_syminfo_t dts; 1551178479Sjb GElf_Sym sym; 1552178479Sjb char c[PATH_MAX * 2]; 1553178479Sjb 1554178479Sjb if (format == NULL) 1555178479Sjb format = " %-50s"; 1556178479Sjb 1557178479Sjb if (dtrace_lookup_by_addr(dtp, pc, &sym, &dts) == 0) { 1558178479Sjb (void) snprintf(c, sizeof (c), "%s`%s", 1559178479Sjb dts.dts_object, dts.dts_name); 1560178479Sjb } else { 1561178479Sjb /* 1562178479Sjb * We'll repeat the lookup, but this time we'll specify a 1563178479Sjb * NULL GElf_Sym -- indicating that we're only interested in 1564178479Sjb * the containing module. 1565178479Sjb */ 1566178479Sjb if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0) { 1567178479Sjb (void) snprintf(c, sizeof (c), "%s`0x%llx", 1568178479Sjb dts.dts_object, (u_longlong_t)pc); 1569178479Sjb } else { 1570178479Sjb (void) snprintf(c, sizeof (c), "0x%llx", 1571178479Sjb (u_longlong_t)pc); 1572178479Sjb } 1573178479Sjb } 1574178479Sjb 1575178479Sjb if (dt_printf(dtp, fp, format, c) < 0) 1576178479Sjb return (-1); 1577178479Sjb 1578178479Sjb return (0); 1579178479Sjb} 1580178479Sjb 1581178479Sjbint 1582178479Sjbdt_print_mod(dtrace_hdl_t *dtp, FILE *fp, const char *format, caddr_t addr) 1583178479Sjb{ 1584178479Sjb /* LINTED - alignment */ 1585178479Sjb uint64_t pc = *((uint64_t *)addr); 1586178479Sjb dtrace_syminfo_t dts; 1587178479Sjb char c[PATH_MAX * 2]; 1588178479Sjb 1589178479Sjb if (format == NULL) 1590178479Sjb format = " %-50s"; 1591178479Sjb 1592178479Sjb if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0) { 1593178479Sjb (void) snprintf(c, sizeof (c), "%s", dts.dts_object); 1594178479Sjb } else { 1595178479Sjb (void) snprintf(c, sizeof (c), "0x%llx", (u_longlong_t)pc); 1596178479Sjb } 1597178479Sjb 1598178479Sjb if (dt_printf(dtp, fp, format, c) < 0) 1599178479Sjb return (-1); 1600178479Sjb 1601178479Sjb return (0); 1602178479Sjb} 1603178479Sjb 1604178479Sjbtypedef struct dt_normal { 1605178479Sjb dtrace_aggvarid_t dtnd_id; 1606178479Sjb uint64_t dtnd_normal; 1607178479Sjb} dt_normal_t; 1608178479Sjb 1609178479Sjbstatic int 1610178479Sjbdt_normalize_agg(const dtrace_aggdata_t *aggdata, void *arg) 1611178479Sjb{ 1612178479Sjb dt_normal_t *normal = arg; 1613178479Sjb dtrace_aggdesc_t *agg = aggdata->dtada_desc; 1614178479Sjb dtrace_aggvarid_t id = normal->dtnd_id; 1615178479Sjb 1616178479Sjb if (agg->dtagd_nrecs == 0) 1617178479Sjb return (DTRACE_AGGWALK_NEXT); 1618178479Sjb 1619178479Sjb if (agg->dtagd_varid != id) 1620178479Sjb return (DTRACE_AGGWALK_NEXT); 1621178479Sjb 1622178479Sjb ((dtrace_aggdata_t *)aggdata)->dtada_normal = normal->dtnd_normal; 1623178479Sjb return (DTRACE_AGGWALK_NORMALIZE); 1624178479Sjb} 1625178479Sjb 1626178479Sjbstatic int 1627178479Sjbdt_normalize(dtrace_hdl_t *dtp, caddr_t base, dtrace_recdesc_t *rec) 1628178479Sjb{ 1629178479Sjb dt_normal_t normal; 1630178479Sjb caddr_t addr; 1631178479Sjb 1632178479Sjb /* 1633178479Sjb * We (should) have two records: the aggregation ID followed by the 1634178479Sjb * normalization value. 1635178479Sjb */ 1636178479Sjb addr = base + rec->dtrd_offset; 1637178479Sjb 1638178479Sjb if (rec->dtrd_size != sizeof (dtrace_aggvarid_t)) 1639178479Sjb return (dt_set_errno(dtp, EDT_BADNORMAL)); 1640178479Sjb 1641178479Sjb /* LINTED - alignment */ 1642178479Sjb normal.dtnd_id = *((dtrace_aggvarid_t *)addr); 1643178479Sjb rec++; 1644178479Sjb 1645178479Sjb if (rec->dtrd_action != DTRACEACT_LIBACT) 1646178479Sjb return (dt_set_errno(dtp, EDT_BADNORMAL)); 1647178479Sjb 1648178479Sjb if (rec->dtrd_arg != DT_ACT_NORMALIZE) 1649178479Sjb return (dt_set_errno(dtp, EDT_BADNORMAL)); 1650178479Sjb 1651178479Sjb addr = base + rec->dtrd_offset; 1652178479Sjb 1653178479Sjb switch (rec->dtrd_size) { 1654178479Sjb case sizeof (uint64_t): 1655178479Sjb /* LINTED - alignment */ 1656178479Sjb normal.dtnd_normal = *((uint64_t *)addr); 1657178479Sjb break; 1658178479Sjb case sizeof (uint32_t): 1659178479Sjb /* LINTED - alignment */ 1660178479Sjb normal.dtnd_normal = *((uint32_t *)addr); 1661178479Sjb break; 1662178479Sjb case sizeof (uint16_t): 1663178479Sjb /* LINTED - alignment */ 1664178479Sjb normal.dtnd_normal = *((uint16_t *)addr); 1665178479Sjb break; 1666178479Sjb case sizeof (uint8_t): 1667178479Sjb normal.dtnd_normal = *((uint8_t *)addr); 1668178479Sjb break; 1669178479Sjb default: 1670178479Sjb return (dt_set_errno(dtp, EDT_BADNORMAL)); 1671178479Sjb } 1672178479Sjb 1673178479Sjb (void) dtrace_aggregate_walk(dtp, dt_normalize_agg, &normal); 1674178479Sjb 1675178479Sjb return (0); 1676178479Sjb} 1677178479Sjb 1678178479Sjbstatic int 1679178479Sjbdt_denormalize_agg(const dtrace_aggdata_t *aggdata, void *arg) 1680178479Sjb{ 1681178479Sjb dtrace_aggdesc_t *agg = aggdata->dtada_desc; 1682178479Sjb dtrace_aggvarid_t id = *((dtrace_aggvarid_t *)arg); 1683178479Sjb 1684178479Sjb if (agg->dtagd_nrecs == 0) 1685178479Sjb return (DTRACE_AGGWALK_NEXT); 1686178479Sjb 1687178479Sjb if (agg->dtagd_varid != id) 1688178479Sjb return (DTRACE_AGGWALK_NEXT); 1689178479Sjb 1690178479Sjb return (DTRACE_AGGWALK_DENORMALIZE); 1691178479Sjb} 1692178479Sjb 1693178479Sjbstatic int 1694178479Sjbdt_clear_agg(const dtrace_aggdata_t *aggdata, void *arg) 1695178479Sjb{ 1696178479Sjb dtrace_aggdesc_t *agg = aggdata->dtada_desc; 1697178479Sjb dtrace_aggvarid_t id = *((dtrace_aggvarid_t *)arg); 1698178479Sjb 1699178479Sjb if (agg->dtagd_nrecs == 0) 1700178479Sjb return (DTRACE_AGGWALK_NEXT); 1701178479Sjb 1702178479Sjb if (agg->dtagd_varid != id) 1703178479Sjb return (DTRACE_AGGWALK_NEXT); 1704178479Sjb 1705178479Sjb return (DTRACE_AGGWALK_CLEAR); 1706178479Sjb} 1707178479Sjb 1708178479Sjbtypedef struct dt_trunc { 1709178479Sjb dtrace_aggvarid_t dttd_id; 1710178479Sjb uint64_t dttd_remaining; 1711178479Sjb} dt_trunc_t; 1712178479Sjb 1713178479Sjbstatic int 1714178479Sjbdt_trunc_agg(const dtrace_aggdata_t *aggdata, void *arg) 1715178479Sjb{ 1716178479Sjb dt_trunc_t *trunc = arg; 1717178479Sjb dtrace_aggdesc_t *agg = aggdata->dtada_desc; 1718178479Sjb dtrace_aggvarid_t id = trunc->dttd_id; 1719178479Sjb 1720178479Sjb if (agg->dtagd_nrecs == 0) 1721178479Sjb return (DTRACE_AGGWALK_NEXT); 1722178479Sjb 1723178479Sjb if (agg->dtagd_varid != id) 1724178479Sjb return (DTRACE_AGGWALK_NEXT); 1725178479Sjb 1726178479Sjb if (trunc->dttd_remaining == 0) 1727178479Sjb return (DTRACE_AGGWALK_REMOVE); 1728178479Sjb 1729178479Sjb trunc->dttd_remaining--; 1730178479Sjb return (DTRACE_AGGWALK_NEXT); 1731178479Sjb} 1732178479Sjb 1733178479Sjbstatic int 1734178479Sjbdt_trunc(dtrace_hdl_t *dtp, caddr_t base, dtrace_recdesc_t *rec) 1735178479Sjb{ 1736178479Sjb dt_trunc_t trunc; 1737178479Sjb caddr_t addr; 1738178479Sjb int64_t remaining; 1739178479Sjb int (*func)(dtrace_hdl_t *, dtrace_aggregate_f *, void *); 1740178479Sjb 1741178479Sjb /* 1742178479Sjb * We (should) have two records: the aggregation ID followed by the 1743178479Sjb * number of aggregation entries after which the aggregation is to be 1744178479Sjb * truncated. 1745178479Sjb */ 1746178479Sjb addr = base + rec->dtrd_offset; 1747178479Sjb 1748178479Sjb if (rec->dtrd_size != sizeof (dtrace_aggvarid_t)) 1749178479Sjb return (dt_set_errno(dtp, EDT_BADTRUNC)); 1750178479Sjb 1751178479Sjb /* LINTED - alignment */ 1752178479Sjb trunc.dttd_id = *((dtrace_aggvarid_t *)addr); 1753178479Sjb rec++; 1754178479Sjb 1755178479Sjb if (rec->dtrd_action != DTRACEACT_LIBACT) 1756178479Sjb return (dt_set_errno(dtp, EDT_BADTRUNC)); 1757178479Sjb 1758178479Sjb if (rec->dtrd_arg != DT_ACT_TRUNC) 1759178479Sjb return (dt_set_errno(dtp, EDT_BADTRUNC)); 1760178479Sjb 1761178479Sjb addr = base + rec->dtrd_offset; 1762178479Sjb 1763178479Sjb switch (rec->dtrd_size) { 1764178479Sjb case sizeof (uint64_t): 1765178479Sjb /* LINTED - alignment */ 1766178479Sjb remaining = *((int64_t *)addr); 1767178479Sjb break; 1768178479Sjb case sizeof (uint32_t): 1769178479Sjb /* LINTED - alignment */ 1770178479Sjb remaining = *((int32_t *)addr); 1771178479Sjb break; 1772178479Sjb case sizeof (uint16_t): 1773178479Sjb /* LINTED - alignment */ 1774178479Sjb remaining = *((int16_t *)addr); 1775178479Sjb break; 1776178479Sjb case sizeof (uint8_t): 1777178479Sjb remaining = *((int8_t *)addr); 1778178479Sjb break; 1779178479Sjb default: 1780178479Sjb return (dt_set_errno(dtp, EDT_BADNORMAL)); 1781178479Sjb } 1782178479Sjb 1783178479Sjb if (remaining < 0) { 1784178479Sjb func = dtrace_aggregate_walk_valsorted; 1785178479Sjb remaining = -remaining; 1786178479Sjb } else { 1787178479Sjb func = dtrace_aggregate_walk_valrevsorted; 1788178479Sjb } 1789178479Sjb 1790178479Sjb assert(remaining >= 0); 1791178479Sjb trunc.dttd_remaining = remaining; 1792178479Sjb 1793178479Sjb (void) func(dtp, dt_trunc_agg, &trunc); 1794178479Sjb 1795178479Sjb return (0); 1796178479Sjb} 1797178479Sjb 1798178479Sjbstatic int 1799178479Sjbdt_print_datum(dtrace_hdl_t *dtp, FILE *fp, dtrace_recdesc_t *rec, 1800178479Sjb caddr_t addr, size_t size, uint64_t normal) 1801178479Sjb{ 1802178479Sjb int err; 1803178479Sjb dtrace_actkind_t act = rec->dtrd_action; 1804178479Sjb 1805178479Sjb switch (act) { 1806178479Sjb case DTRACEACT_STACK: 1807178479Sjb return (dt_print_stack(dtp, fp, NULL, addr, 1808178479Sjb rec->dtrd_arg, rec->dtrd_size / rec->dtrd_arg)); 1809178479Sjb 1810178479Sjb case DTRACEACT_USTACK: 1811178479Sjb case DTRACEACT_JSTACK: 1812178479Sjb return (dt_print_ustack(dtp, fp, NULL, addr, rec->dtrd_arg)); 1813178479Sjb 1814178479Sjb case DTRACEACT_USYM: 1815178479Sjb case DTRACEACT_UADDR: 1816178479Sjb return (dt_print_usym(dtp, fp, addr, act)); 1817178479Sjb 1818178479Sjb case DTRACEACT_UMOD: 1819178479Sjb return (dt_print_umod(dtp, fp, NULL, addr)); 1820178479Sjb 1821178479Sjb case DTRACEACT_SYM: 1822178479Sjb return (dt_print_sym(dtp, fp, NULL, addr)); 1823178479Sjb 1824178479Sjb case DTRACEACT_MOD: 1825178479Sjb return (dt_print_mod(dtp, fp, NULL, addr)); 1826178479Sjb 1827178479Sjb case DTRACEAGG_QUANTIZE: 1828178479Sjb return (dt_print_quantize(dtp, fp, addr, size, normal)); 1829178479Sjb 1830178479Sjb case DTRACEAGG_LQUANTIZE: 1831178479Sjb return (dt_print_lquantize(dtp, fp, addr, size, normal)); 1832178479Sjb 1833237624Spfg case DTRACEAGG_LLQUANTIZE: 1834237624Spfg return (dt_print_llquantize(dtp, fp, addr, size, normal)); 1835237624Spfg 1836178479Sjb case DTRACEAGG_AVG: 1837178479Sjb return (dt_print_average(dtp, fp, addr, size, normal)); 1838178479Sjb 1839178479Sjb case DTRACEAGG_STDDEV: 1840178479Sjb return (dt_print_stddev(dtp, fp, addr, size, normal)); 1841178479Sjb 1842178479Sjb default: 1843178479Sjb break; 1844178479Sjb } 1845178479Sjb 1846178479Sjb switch (size) { 1847178479Sjb case sizeof (uint64_t): 1848178479Sjb err = dt_printf(dtp, fp, " %16lld", 1849178479Sjb /* LINTED - alignment */ 1850178479Sjb (long long)*((uint64_t *)addr) / normal); 1851178479Sjb break; 1852178479Sjb case sizeof (uint32_t): 1853178479Sjb /* LINTED - alignment */ 1854178479Sjb err = dt_printf(dtp, fp, " %8d", *((uint32_t *)addr) / 1855178479Sjb (uint32_t)normal); 1856178479Sjb break; 1857178479Sjb case sizeof (uint16_t): 1858178479Sjb /* LINTED - alignment */ 1859178479Sjb err = dt_printf(dtp, fp, " %5d", *((uint16_t *)addr) / 1860178479Sjb (uint32_t)normal); 1861178479Sjb break; 1862178479Sjb case sizeof (uint8_t): 1863178479Sjb err = dt_printf(dtp, fp, " %3d", *((uint8_t *)addr) / 1864178479Sjb (uint32_t)normal); 1865178479Sjb break; 1866178479Sjb default: 1867178576Sjb err = dt_print_bytes(dtp, fp, addr, size, 50, 0, 0); 1868178479Sjb break; 1869178479Sjb } 1870178479Sjb 1871178479Sjb return (err); 1872178479Sjb} 1873178479Sjb 1874178479Sjbint 1875178479Sjbdt_print_aggs(const dtrace_aggdata_t **aggsdata, int naggvars, void *arg) 1876178479Sjb{ 1877178479Sjb int i, aggact = 0; 1878178479Sjb dt_print_aggdata_t *pd = arg; 1879178479Sjb const dtrace_aggdata_t *aggdata = aggsdata[0]; 1880178479Sjb dtrace_aggdesc_t *agg = aggdata->dtada_desc; 1881178479Sjb FILE *fp = pd->dtpa_fp; 1882178479Sjb dtrace_hdl_t *dtp = pd->dtpa_dtp; 1883178479Sjb dtrace_recdesc_t *rec; 1884178479Sjb dtrace_actkind_t act; 1885178479Sjb caddr_t addr; 1886178479Sjb size_t size; 1887178479Sjb 1888178479Sjb /* 1889178479Sjb * Iterate over each record description in the key, printing the traced 1890178479Sjb * data, skipping the first datum (the tuple member created by the 1891178479Sjb * compiler). 1892178479Sjb */ 1893178479Sjb for (i = 1; i < agg->dtagd_nrecs; i++) { 1894178479Sjb rec = &agg->dtagd_rec[i]; 1895178479Sjb act = rec->dtrd_action; 1896178479Sjb addr = aggdata->dtada_data + rec->dtrd_offset; 1897178479Sjb size = rec->dtrd_size; 1898178479Sjb 1899178479Sjb if (DTRACEACT_ISAGG(act)) { 1900178479Sjb aggact = i; 1901178479Sjb break; 1902178479Sjb } 1903178479Sjb 1904178479Sjb if (dt_print_datum(dtp, fp, rec, addr, size, 1) < 0) 1905178479Sjb return (-1); 1906178479Sjb 1907178479Sjb if (dt_buffered_flush(dtp, NULL, rec, aggdata, 1908178479Sjb DTRACE_BUFDATA_AGGKEY) < 0) 1909178479Sjb return (-1); 1910178479Sjb } 1911178479Sjb 1912178479Sjb assert(aggact != 0); 1913178479Sjb 1914178479Sjb for (i = (naggvars == 1 ? 0 : 1); i < naggvars; i++) { 1915178479Sjb uint64_t normal; 1916178479Sjb 1917178479Sjb aggdata = aggsdata[i]; 1918178479Sjb agg = aggdata->dtada_desc; 1919178479Sjb rec = &agg->dtagd_rec[aggact]; 1920178479Sjb act = rec->dtrd_action; 1921178479Sjb addr = aggdata->dtada_data + rec->dtrd_offset; 1922178479Sjb size = rec->dtrd_size; 1923178479Sjb 1924178479Sjb assert(DTRACEACT_ISAGG(act)); 1925178479Sjb normal = aggdata->dtada_normal; 1926178479Sjb 1927178479Sjb if (dt_print_datum(dtp, fp, rec, addr, size, normal) < 0) 1928178479Sjb return (-1); 1929178479Sjb 1930178479Sjb if (dt_buffered_flush(dtp, NULL, rec, aggdata, 1931178479Sjb DTRACE_BUFDATA_AGGVAL) < 0) 1932178479Sjb return (-1); 1933178479Sjb 1934178479Sjb if (!pd->dtpa_allunprint) 1935178479Sjb agg->dtagd_flags |= DTRACE_AGD_PRINTED; 1936178479Sjb } 1937178479Sjb 1938178479Sjb if (dt_printf(dtp, fp, "\n") < 0) 1939178479Sjb return (-1); 1940178479Sjb 1941178479Sjb if (dt_buffered_flush(dtp, NULL, NULL, aggdata, 1942178479Sjb DTRACE_BUFDATA_AGGFORMAT | DTRACE_BUFDATA_AGGLAST) < 0) 1943178479Sjb return (-1); 1944178479Sjb 1945178479Sjb return (0); 1946178479Sjb} 1947178479Sjb 1948178479Sjbint 1949178479Sjbdt_print_agg(const dtrace_aggdata_t *aggdata, void *arg) 1950178479Sjb{ 1951178479Sjb dt_print_aggdata_t *pd = arg; 1952178479Sjb dtrace_aggdesc_t *agg = aggdata->dtada_desc; 1953178479Sjb dtrace_aggvarid_t aggvarid = pd->dtpa_id; 1954178479Sjb 1955178479Sjb if (pd->dtpa_allunprint) { 1956178479Sjb if (agg->dtagd_flags & DTRACE_AGD_PRINTED) 1957178479Sjb return (0); 1958178479Sjb } else { 1959178479Sjb /* 1960178479Sjb * If we're not printing all unprinted aggregations, then the 1961178479Sjb * aggregation variable ID denotes a specific aggregation 1962178479Sjb * variable that we should print -- skip any other aggregations 1963178479Sjb * that we encounter. 1964178479Sjb */ 1965178479Sjb if (agg->dtagd_nrecs == 0) 1966178479Sjb return (0); 1967178479Sjb 1968178479Sjb if (aggvarid != agg->dtagd_varid) 1969178479Sjb return (0); 1970178479Sjb } 1971178479Sjb 1972178479Sjb return (dt_print_aggs(&aggdata, 1, arg)); 1973178479Sjb} 1974178479Sjb 1975178479Sjbint 1976178479Sjbdt_setopt(dtrace_hdl_t *dtp, const dtrace_probedata_t *data, 1977178479Sjb const char *option, const char *value) 1978178479Sjb{ 1979178479Sjb int len, rval; 1980178479Sjb char *msg; 1981178479Sjb const char *errstr; 1982178479Sjb dtrace_setoptdata_t optdata; 1983178479Sjb 1984178479Sjb bzero(&optdata, sizeof (optdata)); 1985178479Sjb (void) dtrace_getopt(dtp, option, &optdata.dtsda_oldval); 1986178479Sjb 1987178479Sjb if (dtrace_setopt(dtp, option, value) == 0) { 1988178479Sjb (void) dtrace_getopt(dtp, option, &optdata.dtsda_newval); 1989178479Sjb optdata.dtsda_probe = data; 1990178479Sjb optdata.dtsda_option = option; 1991178479Sjb optdata.dtsda_handle = dtp; 1992178479Sjb 1993178479Sjb if ((rval = dt_handle_setopt(dtp, &optdata)) != 0) 1994178479Sjb return (rval); 1995178479Sjb 1996178479Sjb return (0); 1997178479Sjb } 1998178479Sjb 1999178479Sjb errstr = dtrace_errmsg(dtp, dtrace_errno(dtp)); 2000178479Sjb len = strlen(option) + strlen(value) + strlen(errstr) + 80; 2001178479Sjb msg = alloca(len); 2002178479Sjb 2003178479Sjb (void) snprintf(msg, len, "couldn't set option \"%s\" to \"%s\": %s\n", 2004178479Sjb option, value, errstr); 2005178479Sjb 2006178479Sjb if ((rval = dt_handle_liberr(dtp, data, msg)) == 0) 2007178479Sjb return (0); 2008178479Sjb 2009178479Sjb return (rval); 2010178479Sjb} 2011178479Sjb 2012178479Sjbstatic int 2013178479Sjbdt_consume_cpu(dtrace_hdl_t *dtp, FILE *fp, int cpu, dtrace_bufdesc_t *buf, 2014178479Sjb dtrace_consume_probe_f *efunc, dtrace_consume_rec_f *rfunc, void *arg) 2015178479Sjb{ 2016178479Sjb dtrace_epid_t id; 2017178479Sjb size_t offs, start = buf->dtbd_oldest, end = buf->dtbd_size; 2018178479Sjb int flow = (dtp->dt_options[DTRACEOPT_FLOWINDENT] != DTRACEOPT_UNSET); 2019178479Sjb int quiet = (dtp->dt_options[DTRACEOPT_QUIET] != DTRACEOPT_UNSET); 2020178479Sjb int rval, i, n; 2021178479Sjb dtrace_epid_t last = DTRACE_EPIDNONE; 2022178479Sjb dtrace_probedata_t data; 2023178479Sjb uint64_t drops; 2024178479Sjb caddr_t addr; 2025178479Sjb 2026178479Sjb bzero(&data, sizeof (data)); 2027178479Sjb data.dtpda_handle = dtp; 2028178479Sjb data.dtpda_cpu = cpu; 2029178479Sjb 2030178479Sjbagain: 2031178479Sjb for (offs = start; offs < end; ) { 2032178479Sjb dtrace_eprobedesc_t *epd; 2033178479Sjb 2034178479Sjb /* 2035178479Sjb * We're guaranteed to have an ID. 2036178479Sjb */ 2037178479Sjb id = *(uint32_t *)((uintptr_t)buf->dtbd_data + offs); 2038178479Sjb 2039178479Sjb if (id == DTRACE_EPIDNONE) { 2040178479Sjb /* 2041178479Sjb * This is filler to assure proper alignment of the 2042178479Sjb * next record; we simply ignore it. 2043178479Sjb */ 2044178479Sjb offs += sizeof (id); 2045178479Sjb continue; 2046178479Sjb } 2047178479Sjb 2048178479Sjb if ((rval = dt_epid_lookup(dtp, id, &data.dtpda_edesc, 2049178479Sjb &data.dtpda_pdesc)) != 0) 2050178479Sjb return (rval); 2051178479Sjb 2052178479Sjb epd = data.dtpda_edesc; 2053178479Sjb data.dtpda_data = buf->dtbd_data + offs; 2054178479Sjb 2055178479Sjb if (data.dtpda_edesc->dtepd_uarg != DT_ECB_DEFAULT) { 2056178479Sjb rval = dt_handle(dtp, &data); 2057178479Sjb 2058178479Sjb if (rval == DTRACE_CONSUME_NEXT) 2059178479Sjb goto nextepid; 2060178479Sjb 2061178479Sjb if (rval == DTRACE_CONSUME_ERROR) 2062178479Sjb return (-1); 2063178479Sjb } 2064178479Sjb 2065178479Sjb if (flow) 2066178479Sjb (void) dt_flowindent(dtp, &data, last, buf, offs); 2067178479Sjb 2068178479Sjb rval = (*efunc)(&data, arg); 2069178479Sjb 2070178479Sjb if (flow) { 2071178479Sjb if (data.dtpda_flow == DTRACEFLOW_ENTRY) 2072178479Sjb data.dtpda_indent += 2; 2073178479Sjb } 2074178479Sjb 2075178479Sjb if (rval == DTRACE_CONSUME_NEXT) 2076178479Sjb goto nextepid; 2077178479Sjb 2078178479Sjb if (rval == DTRACE_CONSUME_ABORT) 2079178479Sjb return (dt_set_errno(dtp, EDT_DIRABORT)); 2080178479Sjb 2081178479Sjb if (rval != DTRACE_CONSUME_THIS) 2082178479Sjb return (dt_set_errno(dtp, EDT_BADRVAL)); 2083178479Sjb 2084178479Sjb for (i = 0; i < epd->dtepd_nrecs; i++) { 2085178479Sjb dtrace_recdesc_t *rec = &epd->dtepd_rec[i]; 2086178479Sjb dtrace_actkind_t act = rec->dtrd_action; 2087178479Sjb 2088178479Sjb data.dtpda_data = buf->dtbd_data + offs + 2089178479Sjb rec->dtrd_offset; 2090178479Sjb addr = data.dtpda_data; 2091178479Sjb 2092178479Sjb if (act == DTRACEACT_LIBACT) { 2093178479Sjb uint64_t arg = rec->dtrd_arg; 2094178479Sjb dtrace_aggvarid_t id; 2095178479Sjb 2096178479Sjb switch (arg) { 2097178479Sjb case DT_ACT_CLEAR: 2098178479Sjb /* LINTED - alignment */ 2099178479Sjb id = *((dtrace_aggvarid_t *)addr); 2100178479Sjb (void) dtrace_aggregate_walk(dtp, 2101178479Sjb dt_clear_agg, &id); 2102178479Sjb continue; 2103178479Sjb 2104178479Sjb case DT_ACT_DENORMALIZE: 2105178479Sjb /* LINTED - alignment */ 2106178479Sjb id = *((dtrace_aggvarid_t *)addr); 2107178479Sjb (void) dtrace_aggregate_walk(dtp, 2108178479Sjb dt_denormalize_agg, &id); 2109178479Sjb continue; 2110178479Sjb 2111178479Sjb case DT_ACT_FTRUNCATE: 2112178479Sjb if (fp == NULL) 2113178479Sjb continue; 2114178479Sjb 2115178479Sjb (void) fflush(fp); 2116178479Sjb (void) ftruncate(fileno(fp), 0); 2117178479Sjb (void) fseeko(fp, 0, SEEK_SET); 2118178479Sjb continue; 2119178479Sjb 2120178479Sjb case DT_ACT_NORMALIZE: 2121178479Sjb if (i == epd->dtepd_nrecs - 1) 2122178479Sjb return (dt_set_errno(dtp, 2123178479Sjb EDT_BADNORMAL)); 2124178479Sjb 2125178479Sjb if (dt_normalize(dtp, 2126178479Sjb buf->dtbd_data + offs, rec) != 0) 2127178479Sjb return (-1); 2128178479Sjb 2129178479Sjb i++; 2130178479Sjb continue; 2131178479Sjb 2132178479Sjb case DT_ACT_SETOPT: { 2133178479Sjb uint64_t *opts = dtp->dt_options; 2134178479Sjb dtrace_recdesc_t *valrec; 2135178479Sjb uint32_t valsize; 2136178479Sjb caddr_t val; 2137178479Sjb int rv; 2138178479Sjb 2139178479Sjb if (i == epd->dtepd_nrecs - 1) { 2140178479Sjb return (dt_set_errno(dtp, 2141178479Sjb EDT_BADSETOPT)); 2142178479Sjb } 2143178479Sjb 2144178479Sjb valrec = &epd->dtepd_rec[++i]; 2145178479Sjb valsize = valrec->dtrd_size; 2146178479Sjb 2147178479Sjb if (valrec->dtrd_action != act || 2148178479Sjb valrec->dtrd_arg != arg) { 2149178479Sjb return (dt_set_errno(dtp, 2150178479Sjb EDT_BADSETOPT)); 2151178479Sjb } 2152178479Sjb 2153178479Sjb if (valsize > sizeof (uint64_t)) { 2154178479Sjb val = buf->dtbd_data + offs + 2155178479Sjb valrec->dtrd_offset; 2156178479Sjb } else { 2157178479Sjb val = "1"; 2158178479Sjb } 2159178479Sjb 2160178479Sjb rv = dt_setopt(dtp, &data, addr, val); 2161178479Sjb 2162178479Sjb if (rv != 0) 2163178479Sjb return (-1); 2164178479Sjb 2165178479Sjb flow = (opts[DTRACEOPT_FLOWINDENT] != 2166178479Sjb DTRACEOPT_UNSET); 2167178479Sjb quiet = (opts[DTRACEOPT_QUIET] != 2168178479Sjb DTRACEOPT_UNSET); 2169178479Sjb 2170178479Sjb continue; 2171178479Sjb } 2172178479Sjb 2173178479Sjb case DT_ACT_TRUNC: 2174178479Sjb if (i == epd->dtepd_nrecs - 1) 2175178479Sjb return (dt_set_errno(dtp, 2176178479Sjb EDT_BADTRUNC)); 2177178479Sjb 2178178479Sjb if (dt_trunc(dtp, 2179178479Sjb buf->dtbd_data + offs, rec) != 0) 2180178479Sjb return (-1); 2181178479Sjb 2182178479Sjb i++; 2183178479Sjb continue; 2184178479Sjb 2185178479Sjb default: 2186178479Sjb continue; 2187178479Sjb } 2188178479Sjb } 2189178479Sjb 2190178479Sjb rval = (*rfunc)(&data, rec, arg); 2191178479Sjb 2192178479Sjb if (rval == DTRACE_CONSUME_NEXT) 2193178479Sjb continue; 2194178479Sjb 2195178479Sjb if (rval == DTRACE_CONSUME_ABORT) 2196178479Sjb return (dt_set_errno(dtp, EDT_DIRABORT)); 2197178479Sjb 2198178479Sjb if (rval != DTRACE_CONSUME_THIS) 2199178479Sjb return (dt_set_errno(dtp, EDT_BADRVAL)); 2200178479Sjb 2201178479Sjb if (act == DTRACEACT_STACK) { 2202178479Sjb int depth = rec->dtrd_arg; 2203178479Sjb 2204178479Sjb if (dt_print_stack(dtp, fp, NULL, addr, depth, 2205178479Sjb rec->dtrd_size / depth) < 0) 2206178479Sjb return (-1); 2207178479Sjb goto nextrec; 2208178479Sjb } 2209178479Sjb 2210178479Sjb if (act == DTRACEACT_USTACK || 2211178479Sjb act == DTRACEACT_JSTACK) { 2212178479Sjb if (dt_print_ustack(dtp, fp, NULL, 2213178479Sjb addr, rec->dtrd_arg) < 0) 2214178479Sjb return (-1); 2215178479Sjb goto nextrec; 2216178479Sjb } 2217178479Sjb 2218178479Sjb if (act == DTRACEACT_SYM) { 2219178479Sjb if (dt_print_sym(dtp, fp, NULL, addr) < 0) 2220178479Sjb return (-1); 2221178479Sjb goto nextrec; 2222178479Sjb } 2223178479Sjb 2224178479Sjb if (act == DTRACEACT_MOD) { 2225178479Sjb if (dt_print_mod(dtp, fp, NULL, addr) < 0) 2226178479Sjb return (-1); 2227178479Sjb goto nextrec; 2228178479Sjb } 2229178479Sjb 2230178479Sjb if (act == DTRACEACT_USYM || act == DTRACEACT_UADDR) { 2231178479Sjb if (dt_print_usym(dtp, fp, addr, act) < 0) 2232178479Sjb return (-1); 2233178479Sjb goto nextrec; 2234178479Sjb } 2235178479Sjb 2236178479Sjb if (act == DTRACEACT_UMOD) { 2237178479Sjb if (dt_print_umod(dtp, fp, NULL, addr) < 0) 2238178479Sjb return (-1); 2239178479Sjb goto nextrec; 2240178479Sjb } 2241178479Sjb 2242178576Sjb if (act == DTRACEACT_PRINTM) { 2243178576Sjb if (dt_print_memory(dtp, fp, addr) < 0) 2244178576Sjb return (-1); 2245178576Sjb goto nextrec; 2246178576Sjb } 2247178576Sjb 2248178576Sjb if (act == DTRACEACT_PRINTT) { 2249178576Sjb if (dt_print_type(dtp, fp, addr) < 0) 2250178576Sjb return (-1); 2251178576Sjb goto nextrec; 2252178576Sjb } 2253178576Sjb 2254178479Sjb if (DTRACEACT_ISPRINTFLIKE(act)) { 2255178479Sjb void *fmtdata; 2256178479Sjb int (*func)(dtrace_hdl_t *, FILE *, void *, 2257178479Sjb const dtrace_probedata_t *, 2258178479Sjb const dtrace_recdesc_t *, uint_t, 2259178479Sjb const void *buf, size_t); 2260178479Sjb 2261178479Sjb if ((fmtdata = dt_format_lookup(dtp, 2262178479Sjb rec->dtrd_format)) == NULL) 2263178479Sjb goto nofmt; 2264178479Sjb 2265178479Sjb switch (act) { 2266178479Sjb case DTRACEACT_PRINTF: 2267178479Sjb func = dtrace_fprintf; 2268178479Sjb break; 2269178479Sjb case DTRACEACT_PRINTA: 2270178479Sjb func = dtrace_fprinta; 2271178479Sjb break; 2272178479Sjb case DTRACEACT_SYSTEM: 2273178479Sjb func = dtrace_system; 2274178479Sjb break; 2275178479Sjb case DTRACEACT_FREOPEN: 2276178479Sjb func = dtrace_freopen; 2277178479Sjb break; 2278178479Sjb } 2279178479Sjb 2280178479Sjb n = (*func)(dtp, fp, fmtdata, &data, 2281178479Sjb rec, epd->dtepd_nrecs - i, 2282178479Sjb (uchar_t *)buf->dtbd_data + offs, 2283178479Sjb buf->dtbd_size - offs); 2284178479Sjb 2285178479Sjb if (n < 0) 2286178479Sjb return (-1); /* errno is set for us */ 2287178479Sjb 2288178479Sjb if (n > 0) 2289178479Sjb i += n - 1; 2290178479Sjb goto nextrec; 2291178479Sjb } 2292178479Sjb 2293178479Sjbnofmt: 2294178479Sjb if (act == DTRACEACT_PRINTA) { 2295178479Sjb dt_print_aggdata_t pd; 2296178479Sjb dtrace_aggvarid_t *aggvars; 2297178479Sjb int j, naggvars = 0; 2298178479Sjb size_t size = ((epd->dtepd_nrecs - i) * 2299178479Sjb sizeof (dtrace_aggvarid_t)); 2300178479Sjb 2301178479Sjb if ((aggvars = dt_alloc(dtp, size)) == NULL) 2302178479Sjb return (-1); 2303178479Sjb 2304178479Sjb /* 2305178479Sjb * This might be a printa() with multiple 2306178479Sjb * aggregation variables. We need to scan 2307178479Sjb * forward through the records until we find 2308178479Sjb * a record from a different statement. 2309178479Sjb */ 2310178479Sjb for (j = i; j < epd->dtepd_nrecs; j++) { 2311178479Sjb dtrace_recdesc_t *nrec; 2312178479Sjb caddr_t naddr; 2313178479Sjb 2314178479Sjb nrec = &epd->dtepd_rec[j]; 2315178479Sjb 2316178479Sjb if (nrec->dtrd_uarg != rec->dtrd_uarg) 2317178479Sjb break; 2318178479Sjb 2319178479Sjb if (nrec->dtrd_action != act) { 2320178479Sjb return (dt_set_errno(dtp, 2321178479Sjb EDT_BADAGG)); 2322178479Sjb } 2323178479Sjb 2324178479Sjb naddr = buf->dtbd_data + offs + 2325178479Sjb nrec->dtrd_offset; 2326178479Sjb 2327178479Sjb aggvars[naggvars++] = 2328178479Sjb /* LINTED - alignment */ 2329178479Sjb *((dtrace_aggvarid_t *)naddr); 2330178479Sjb } 2331178479Sjb 2332178479Sjb i = j - 1; 2333178479Sjb bzero(&pd, sizeof (pd)); 2334178479Sjb pd.dtpa_dtp = dtp; 2335178479Sjb pd.dtpa_fp = fp; 2336178479Sjb 2337178479Sjb assert(naggvars >= 1); 2338178479Sjb 2339178479Sjb if (naggvars == 1) { 2340178479Sjb pd.dtpa_id = aggvars[0]; 2341178479Sjb dt_free(dtp, aggvars); 2342178479Sjb 2343178479Sjb if (dt_printf(dtp, fp, "\n") < 0 || 2344178479Sjb dtrace_aggregate_walk_sorted(dtp, 2345178479Sjb dt_print_agg, &pd) < 0) 2346178479Sjb return (-1); 2347178479Sjb goto nextrec; 2348178479Sjb } 2349178479Sjb 2350178479Sjb if (dt_printf(dtp, fp, "\n") < 0 || 2351178479Sjb dtrace_aggregate_walk_joined(dtp, aggvars, 2352178479Sjb naggvars, dt_print_aggs, &pd) < 0) { 2353178479Sjb dt_free(dtp, aggvars); 2354178479Sjb return (-1); 2355178479Sjb } 2356178479Sjb 2357178479Sjb dt_free(dtp, aggvars); 2358178479Sjb goto nextrec; 2359178479Sjb } 2360178479Sjb 2361178479Sjb switch (rec->dtrd_size) { 2362178479Sjb case sizeof (uint64_t): 2363178479Sjb n = dt_printf(dtp, fp, 2364178479Sjb quiet ? "%lld" : " %16lld", 2365178479Sjb /* LINTED - alignment */ 2366178479Sjb *((unsigned long long *)addr)); 2367178479Sjb break; 2368178479Sjb case sizeof (uint32_t): 2369178479Sjb n = dt_printf(dtp, fp, quiet ? "%d" : " %8d", 2370178479Sjb /* LINTED - alignment */ 2371178479Sjb *((uint32_t *)addr)); 2372178479Sjb break; 2373178479Sjb case sizeof (uint16_t): 2374178479Sjb n = dt_printf(dtp, fp, quiet ? "%d" : " %5d", 2375178479Sjb /* LINTED - alignment */ 2376178479Sjb *((uint16_t *)addr)); 2377178479Sjb break; 2378178479Sjb case sizeof (uint8_t): 2379178479Sjb n = dt_printf(dtp, fp, quiet ? "%d" : " %3d", 2380178479Sjb *((uint8_t *)addr)); 2381178479Sjb break; 2382178479Sjb default: 2383178479Sjb n = dt_print_bytes(dtp, fp, addr, 2384178576Sjb rec->dtrd_size, 33, quiet, 0); 2385178479Sjb break; 2386178479Sjb } 2387178479Sjb 2388178479Sjb if (n < 0) 2389178479Sjb return (-1); /* errno is set for us */ 2390178479Sjb 2391178479Sjbnextrec: 2392178479Sjb if (dt_buffered_flush(dtp, &data, rec, NULL, 0) < 0) 2393178479Sjb return (-1); /* errno is set for us */ 2394178479Sjb } 2395178479Sjb 2396178479Sjb /* 2397178479Sjb * Call the record callback with a NULL record to indicate 2398178479Sjb * that we're done processing this EPID. 2399178479Sjb */ 2400178479Sjb rval = (*rfunc)(&data, NULL, arg); 2401178479Sjbnextepid: 2402178479Sjb offs += epd->dtepd_size; 2403178479Sjb last = id; 2404178479Sjb } 2405178479Sjb 2406178479Sjb if (buf->dtbd_oldest != 0 && start == buf->dtbd_oldest) { 2407178479Sjb end = buf->dtbd_oldest; 2408178479Sjb start = 0; 2409178479Sjb goto again; 2410178479Sjb } 2411178479Sjb 2412178479Sjb if ((drops = buf->dtbd_drops) == 0) 2413178479Sjb return (0); 2414178479Sjb 2415178479Sjb /* 2416178479Sjb * Explicitly zero the drops to prevent us from processing them again. 2417178479Sjb */ 2418178479Sjb buf->dtbd_drops = 0; 2419178479Sjb 2420178479Sjb return (dt_handle_cpudrop(dtp, cpu, DTRACEDROP_PRINCIPAL, drops)); 2421178479Sjb} 2422178479Sjb 2423178479Sjbtypedef struct dt_begin { 2424178479Sjb dtrace_consume_probe_f *dtbgn_probefunc; 2425178479Sjb dtrace_consume_rec_f *dtbgn_recfunc; 2426178479Sjb void *dtbgn_arg; 2427178479Sjb dtrace_handle_err_f *dtbgn_errhdlr; 2428178479Sjb void *dtbgn_errarg; 2429178479Sjb int dtbgn_beginonly; 2430178479Sjb} dt_begin_t; 2431178479Sjb 2432178479Sjbstatic int 2433178479Sjbdt_consume_begin_probe(const dtrace_probedata_t *data, void *arg) 2434178479Sjb{ 2435178479Sjb dt_begin_t *begin = (dt_begin_t *)arg; 2436178479Sjb dtrace_probedesc_t *pd = data->dtpda_pdesc; 2437178479Sjb 2438178479Sjb int r1 = (strcmp(pd->dtpd_provider, "dtrace") == 0); 2439178479Sjb int r2 = (strcmp(pd->dtpd_name, "BEGIN") == 0); 2440178479Sjb 2441178479Sjb if (begin->dtbgn_beginonly) { 2442178479Sjb if (!(r1 && r2)) 2443178479Sjb return (DTRACE_CONSUME_NEXT); 2444178479Sjb } else { 2445178479Sjb if (r1 && r2) 2446178479Sjb return (DTRACE_CONSUME_NEXT); 2447178479Sjb } 2448178479Sjb 2449178479Sjb /* 2450178479Sjb * We have a record that we're interested in. Now call the underlying 2451178479Sjb * probe function... 2452178479Sjb */ 2453178479Sjb return (begin->dtbgn_probefunc(data, begin->dtbgn_arg)); 2454178479Sjb} 2455178479Sjb 2456178479Sjbstatic int 2457178479Sjbdt_consume_begin_record(const dtrace_probedata_t *data, 2458178479Sjb const dtrace_recdesc_t *rec, void *arg) 2459178479Sjb{ 2460178479Sjb dt_begin_t *begin = (dt_begin_t *)arg; 2461178479Sjb 2462178479Sjb return (begin->dtbgn_recfunc(data, rec, begin->dtbgn_arg)); 2463178479Sjb} 2464178479Sjb 2465178479Sjbstatic int 2466178479Sjbdt_consume_begin_error(const dtrace_errdata_t *data, void *arg) 2467178479Sjb{ 2468178479Sjb dt_begin_t *begin = (dt_begin_t *)arg; 2469178479Sjb dtrace_probedesc_t *pd = data->dteda_pdesc; 2470178479Sjb 2471178479Sjb int r1 = (strcmp(pd->dtpd_provider, "dtrace") == 0); 2472178479Sjb int r2 = (strcmp(pd->dtpd_name, "BEGIN") == 0); 2473178479Sjb 2474178479Sjb if (begin->dtbgn_beginonly) { 2475178479Sjb if (!(r1 && r2)) 2476178479Sjb return (DTRACE_HANDLE_OK); 2477178479Sjb } else { 2478178479Sjb if (r1 && r2) 2479178479Sjb return (DTRACE_HANDLE_OK); 2480178479Sjb } 2481178479Sjb 2482178479Sjb return (begin->dtbgn_errhdlr(data, begin->dtbgn_errarg)); 2483178479Sjb} 2484178479Sjb 2485178479Sjbstatic int 2486178479Sjbdt_consume_begin(dtrace_hdl_t *dtp, FILE *fp, dtrace_bufdesc_t *buf, 2487178479Sjb dtrace_consume_probe_f *pf, dtrace_consume_rec_f *rf, void *arg) 2488178479Sjb{ 2489178479Sjb /* 2490178479Sjb * There's this idea that the BEGIN probe should be processed before 2491178479Sjb * everything else, and that the END probe should be processed after 2492178479Sjb * anything else. In the common case, this is pretty easy to deal 2493178479Sjb * with. However, a situation may arise where the BEGIN enabling and 2494178479Sjb * END enabling are on the same CPU, and some enabling in the middle 2495178479Sjb * occurred on a different CPU. To deal with this (blech!) we need to 2496178479Sjb * consume the BEGIN buffer up until the end of the BEGIN probe, and 2497178479Sjb * then set it aside. We will then process every other CPU, and then 2498178479Sjb * we'll return to the BEGIN CPU and process the rest of the data 2499178479Sjb * (which will inevitably include the END probe, if any). Making this 2500178479Sjb * even more complicated (!) is the library's ERROR enabling. Because 2501178479Sjb * this enabling is processed before we even get into the consume call 2502178479Sjb * back, any ERROR firing would result in the library's ERROR enabling 2503178479Sjb * being processed twice -- once in our first pass (for BEGIN probes), 2504178479Sjb * and again in our second pass (for everything but BEGIN probes). To 2505178479Sjb * deal with this, we interpose on the ERROR handler to assure that we 2506178479Sjb * only process ERROR enablings induced by BEGIN enablings in the 2507178479Sjb * first pass, and that we only process ERROR enablings _not_ induced 2508178479Sjb * by BEGIN enablings in the second pass. 2509178479Sjb */ 2510178479Sjb dt_begin_t begin; 2511178479Sjb processorid_t cpu = dtp->dt_beganon; 2512178479Sjb dtrace_bufdesc_t nbuf; 2513178576Sjb#if !defined(sun) 2514178576Sjb dtrace_bufdesc_t *pbuf; 2515178576Sjb#endif 2516178479Sjb int rval, i; 2517178479Sjb static int max_ncpus; 2518178479Sjb dtrace_optval_t size; 2519178479Sjb 2520178479Sjb dtp->dt_beganon = -1; 2521178479Sjb 2522178576Sjb#if defined(sun) 2523178479Sjb if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, buf) == -1) { 2524178576Sjb#else 2525178576Sjb if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, &buf) == -1) { 2526178576Sjb#endif 2527178479Sjb /* 2528178479Sjb * We really don't expect this to fail, but it is at least 2529178479Sjb * technically possible for this to fail with ENOENT. In this 2530178479Sjb * case, we just drive on... 2531178479Sjb */ 2532178479Sjb if (errno == ENOENT) 2533178479Sjb return (0); 2534178479Sjb 2535178479Sjb return (dt_set_errno(dtp, errno)); 2536178479Sjb } 2537178479Sjb 2538178479Sjb if (!dtp->dt_stopped || buf->dtbd_cpu != dtp->dt_endedon) { 2539178479Sjb /* 2540178479Sjb * This is the simple case. We're either not stopped, or if 2541178479Sjb * we are, we actually processed any END probes on another 2542178479Sjb * CPU. We can simply consume this buffer and return. 2543178479Sjb */ 2544178479Sjb return (dt_consume_cpu(dtp, fp, cpu, buf, pf, rf, arg)); 2545178479Sjb } 2546178479Sjb 2547178479Sjb begin.dtbgn_probefunc = pf; 2548178479Sjb begin.dtbgn_recfunc = rf; 2549178479Sjb begin.dtbgn_arg = arg; 2550178479Sjb begin.dtbgn_beginonly = 1; 2551178479Sjb 2552178479Sjb /* 2553178479Sjb * We need to interpose on the ERROR handler to be sure that we 2554178479Sjb * only process ERRORs induced by BEGIN. 2555178479Sjb */ 2556178479Sjb begin.dtbgn_errhdlr = dtp->dt_errhdlr; 2557178479Sjb begin.dtbgn_errarg = dtp->dt_errarg; 2558178479Sjb dtp->dt_errhdlr = dt_consume_begin_error; 2559178479Sjb dtp->dt_errarg = &begin; 2560178479Sjb 2561178479Sjb rval = dt_consume_cpu(dtp, fp, cpu, buf, dt_consume_begin_probe, 2562178479Sjb dt_consume_begin_record, &begin); 2563178479Sjb 2564178479Sjb dtp->dt_errhdlr = begin.dtbgn_errhdlr; 2565178479Sjb dtp->dt_errarg = begin.dtbgn_errarg; 2566178479Sjb 2567178479Sjb if (rval != 0) 2568178479Sjb return (rval); 2569178479Sjb 2570178479Sjb /* 2571178479Sjb * Now allocate a new buffer. We'll use this to deal with every other 2572178479Sjb * CPU. 2573178479Sjb */ 2574178479Sjb bzero(&nbuf, sizeof (dtrace_bufdesc_t)); 2575178479Sjb (void) dtrace_getopt(dtp, "bufsize", &size); 2576178479Sjb if ((nbuf.dtbd_data = malloc(size)) == NULL) 2577178479Sjb return (dt_set_errno(dtp, EDT_NOMEM)); 2578178479Sjb 2579178479Sjb if (max_ncpus == 0) 2580178479Sjb max_ncpus = dt_sysconf(dtp, _SC_CPUID_MAX) + 1; 2581178479Sjb 2582178479Sjb for (i = 0; i < max_ncpus; i++) { 2583178479Sjb nbuf.dtbd_cpu = i; 2584178479Sjb 2585178479Sjb if (i == cpu) 2586178479Sjb continue; 2587178479Sjb 2588178576Sjb#if defined(sun) 2589178479Sjb if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, &nbuf) == -1) { 2590178576Sjb#else 2591178576Sjb pbuf = &nbuf; 2592178576Sjb if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, &pbuf) == -1) { 2593178576Sjb#endif 2594178479Sjb /* 2595178479Sjb * If we failed with ENOENT, it may be because the 2596178479Sjb * CPU was unconfigured -- this is okay. Any other 2597178479Sjb * error, however, is unexpected. 2598178479Sjb */ 2599178479Sjb if (errno == ENOENT) 2600178479Sjb continue; 2601178479Sjb 2602178479Sjb free(nbuf.dtbd_data); 2603178479Sjb 2604178479Sjb return (dt_set_errno(dtp, errno)); 2605178479Sjb } 2606178479Sjb 2607178479Sjb if ((rval = dt_consume_cpu(dtp, fp, 2608178479Sjb i, &nbuf, pf, rf, arg)) != 0) { 2609178479Sjb free(nbuf.dtbd_data); 2610178479Sjb return (rval); 2611178479Sjb } 2612178479Sjb } 2613178479Sjb 2614178479Sjb free(nbuf.dtbd_data); 2615178479Sjb 2616178479Sjb /* 2617178479Sjb * Okay -- we're done with the other buffers. Now we want to 2618178479Sjb * reconsume the first buffer -- but this time we're looking for 2619178479Sjb * everything _but_ BEGIN. And of course, in order to only consume 2620178479Sjb * those ERRORs _not_ associated with BEGIN, we need to reinstall our 2621178479Sjb * ERROR interposition function... 2622178479Sjb */ 2623178479Sjb begin.dtbgn_beginonly = 0; 2624178479Sjb 2625178479Sjb assert(begin.dtbgn_errhdlr == dtp->dt_errhdlr); 2626178479Sjb assert(begin.dtbgn_errarg == dtp->dt_errarg); 2627178479Sjb dtp->dt_errhdlr = dt_consume_begin_error; 2628178479Sjb dtp->dt_errarg = &begin; 2629178479Sjb 2630178479Sjb rval = dt_consume_cpu(dtp, fp, cpu, buf, dt_consume_begin_probe, 2631178479Sjb dt_consume_begin_record, &begin); 2632178479Sjb 2633178479Sjb dtp->dt_errhdlr = begin.dtbgn_errhdlr; 2634178479Sjb dtp->dt_errarg = begin.dtbgn_errarg; 2635178479Sjb 2636178479Sjb return (rval); 2637178479Sjb} 2638178479Sjb 2639178479Sjbint 2640178479Sjbdtrace_consume(dtrace_hdl_t *dtp, FILE *fp, 2641178479Sjb dtrace_consume_probe_f *pf, dtrace_consume_rec_f *rf, void *arg) 2642178479Sjb{ 2643178479Sjb dtrace_bufdesc_t *buf = &dtp->dt_buf; 2644178479Sjb dtrace_optval_t size; 2645178479Sjb static int max_ncpus; 2646178479Sjb int i, rval; 2647178479Sjb dtrace_optval_t interval = dtp->dt_options[DTRACEOPT_SWITCHRATE]; 2648178479Sjb hrtime_t now = gethrtime(); 2649178479Sjb 2650178479Sjb if (dtp->dt_lastswitch != 0) { 2651178479Sjb if (now - dtp->dt_lastswitch < interval) 2652178479Sjb return (0); 2653178479Sjb 2654178479Sjb dtp->dt_lastswitch += interval; 2655178479Sjb } else { 2656178479Sjb dtp->dt_lastswitch = now; 2657178479Sjb } 2658178479Sjb 2659178479Sjb if (!dtp->dt_active) 2660178479Sjb return (dt_set_errno(dtp, EINVAL)); 2661178479Sjb 2662178479Sjb if (max_ncpus == 0) 2663178479Sjb max_ncpus = dt_sysconf(dtp, _SC_CPUID_MAX) + 1; 2664178479Sjb 2665178479Sjb if (pf == NULL) 2666178479Sjb pf = (dtrace_consume_probe_f *)dt_nullprobe; 2667178479Sjb 2668178479Sjb if (rf == NULL) 2669178479Sjb rf = (dtrace_consume_rec_f *)dt_nullrec; 2670178479Sjb 2671178479Sjb if (buf->dtbd_data == NULL) { 2672178479Sjb (void) dtrace_getopt(dtp, "bufsize", &size); 2673178479Sjb if ((buf->dtbd_data = malloc(size)) == NULL) 2674178479Sjb return (dt_set_errno(dtp, EDT_NOMEM)); 2675178479Sjb 2676178479Sjb buf->dtbd_size = size; 2677178479Sjb } 2678178479Sjb 2679178479Sjb /* 2680178479Sjb * If we have just begun, we want to first process the CPU that 2681178479Sjb * executed the BEGIN probe (if any). 2682178479Sjb */ 2683178479Sjb if (dtp->dt_active && dtp->dt_beganon != -1) { 2684178479Sjb buf->dtbd_cpu = dtp->dt_beganon; 2685178479Sjb if ((rval = dt_consume_begin(dtp, fp, buf, pf, rf, arg)) != 0) 2686178479Sjb return (rval); 2687178479Sjb } 2688178479Sjb 2689178479Sjb for (i = 0; i < max_ncpus; i++) { 2690178479Sjb buf->dtbd_cpu = i; 2691178479Sjb 2692178479Sjb /* 2693178479Sjb * If we have stopped, we want to process the CPU on which the 2694178479Sjb * END probe was processed only _after_ we have processed 2695178479Sjb * everything else. 2696178479Sjb */ 2697178479Sjb if (dtp->dt_stopped && (i == dtp->dt_endedon)) 2698178479Sjb continue; 2699178479Sjb 2700178576Sjb#if defined(sun) 2701178479Sjb if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, buf) == -1) { 2702178576Sjb#else 2703178576Sjb if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, &buf) == -1) { 2704178576Sjb#endif 2705178479Sjb /* 2706178479Sjb * If we failed with ENOENT, it may be because the 2707178479Sjb * CPU was unconfigured -- this is okay. Any other 2708178479Sjb * error, however, is unexpected. 2709178479Sjb */ 2710178479Sjb if (errno == ENOENT) 2711178479Sjb continue; 2712178479Sjb 2713178479Sjb return (dt_set_errno(dtp, errno)); 2714178479Sjb } 2715178479Sjb 2716178479Sjb if ((rval = dt_consume_cpu(dtp, fp, i, buf, pf, rf, arg)) != 0) 2717178479Sjb return (rval); 2718178479Sjb } 2719178479Sjb 2720178479Sjb if (!dtp->dt_stopped) 2721178479Sjb return (0); 2722178479Sjb 2723178479Sjb buf->dtbd_cpu = dtp->dt_endedon; 2724178479Sjb 2725178576Sjb#if defined(sun) 2726178479Sjb if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, buf) == -1) { 2727178576Sjb#else 2728178576Sjb if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, &buf) == -1) { 2729178576Sjb#endif 2730178479Sjb /* 2731178479Sjb * This _really_ shouldn't fail, but it is strictly speaking 2732178479Sjb * possible for this to return ENOENT if the CPU that called 2733178479Sjb * the END enabling somehow managed to become unconfigured. 2734178479Sjb * It's unclear how the user can possibly expect anything 2735178479Sjb * rational to happen in this case -- the state has been thrown 2736178479Sjb * out along with the unconfigured CPU -- so we'll just drive 2737178479Sjb * on... 2738178479Sjb */ 2739178479Sjb if (errno == ENOENT) 2740178479Sjb return (0); 2741178479Sjb 2742178479Sjb return (dt_set_errno(dtp, errno)); 2743178479Sjb } 2744178479Sjb 2745178479Sjb return (dt_consume_cpu(dtp, fp, dtp->dt_endedon, buf, pf, rf, arg)); 2746178479Sjb} 2747