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/* 27267942Srpaulo * Copyright (c) 2013, Joyent, Inc. All rights reserved. 28250574Smarkj * Copyright (c) 2012 by Delphix. All rights reserved. 29237624Spfg */ 30237624Spfg 31178479Sjb#include <stdlib.h> 32178479Sjb#include <strings.h> 33178479Sjb#include <errno.h> 34178479Sjb#include <unistd.h> 35178479Sjb#include <limits.h> 36178479Sjb#include <assert.h> 37178479Sjb#include <ctype.h> 38277300Ssmh#ifdef illumos 39178479Sjb#include <alloca.h> 40178576Sjb#endif 41178479Sjb#include <dt_impl.h> 42250574Smarkj#include <dt_pq.h> 43277300Ssmh#ifndef illumos 44211554Srpaulo#include <libproc_compat.h> 45211554Srpaulo#endif 46178479Sjb 47178479Sjb#define DT_MASK_LO 0x00000000FFFFFFFFULL 48178479Sjb 49178479Sjb/* 50178479Sjb * We declare this here because (1) we need it and (2) we want to avoid a 51178479Sjb * dependency on libm in libdtrace. 52178479Sjb */ 53178479Sjbstatic long double 54178479Sjbdt_fabsl(long double x) 55178479Sjb{ 56178479Sjb if (x < 0) 57178479Sjb return (-x); 58178479Sjb 59178479Sjb return (x); 60178479Sjb} 61178479Sjb 62267942Srpaulostatic int 63267942Srpaulodt_ndigits(long long val) 64267942Srpaulo{ 65267942Srpaulo int rval = 1; 66267942Srpaulo long long cmp = 10; 67267942Srpaulo 68267942Srpaulo if (val < 0) { 69267942Srpaulo val = val == INT64_MIN ? INT64_MAX : -val; 70267942Srpaulo rval++; 71267942Srpaulo } 72267942Srpaulo 73267942Srpaulo while (val > cmp && cmp > 0) { 74267942Srpaulo rval++; 75267942Srpaulo cmp *= 10; 76267942Srpaulo } 77267942Srpaulo 78267942Srpaulo return (rval < 4 ? 4 : rval); 79267942Srpaulo} 80267942Srpaulo 81178479Sjb/* 82178479Sjb * 128-bit arithmetic functions needed to support the stddev() aggregating 83178479Sjb * action. 84178479Sjb */ 85178479Sjbstatic int 86178479Sjbdt_gt_128(uint64_t *a, uint64_t *b) 87178479Sjb{ 88178479Sjb return (a[1] > b[1] || (a[1] == b[1] && a[0] > b[0])); 89178479Sjb} 90178479Sjb 91178479Sjbstatic int 92178479Sjbdt_ge_128(uint64_t *a, uint64_t *b) 93178479Sjb{ 94178479Sjb return (a[1] > b[1] || (a[1] == b[1] && a[0] >= b[0])); 95178479Sjb} 96178479Sjb 97178479Sjbstatic int 98178479Sjbdt_le_128(uint64_t *a, uint64_t *b) 99178479Sjb{ 100178479Sjb return (a[1] < b[1] || (a[1] == b[1] && a[0] <= b[0])); 101178479Sjb} 102178479Sjb 103178479Sjb/* 104178479Sjb * Shift the 128-bit value in a by b. If b is positive, shift left. 105178479Sjb * If b is negative, shift right. 106178479Sjb */ 107178479Sjbstatic void 108178479Sjbdt_shift_128(uint64_t *a, int b) 109178479Sjb{ 110178479Sjb uint64_t mask; 111178479Sjb 112178479Sjb if (b == 0) 113178479Sjb return; 114178479Sjb 115178479Sjb if (b < 0) { 116178479Sjb b = -b; 117178479Sjb if (b >= 64) { 118178479Sjb a[0] = a[1] >> (b - 64); 119178479Sjb a[1] = 0; 120178479Sjb } else { 121178479Sjb a[0] >>= b; 122178479Sjb mask = 1LL << (64 - b); 123178479Sjb mask -= 1; 124178479Sjb a[0] |= ((a[1] & mask) << (64 - b)); 125178479Sjb a[1] >>= b; 126178479Sjb } 127178479Sjb } else { 128178479Sjb if (b >= 64) { 129178479Sjb a[1] = a[0] << (b - 64); 130178479Sjb a[0] = 0; 131178479Sjb } else { 132178479Sjb a[1] <<= b; 133178479Sjb mask = a[0] >> (64 - b); 134178479Sjb a[1] |= mask; 135178479Sjb a[0] <<= b; 136178479Sjb } 137178479Sjb } 138178479Sjb} 139178479Sjb 140178479Sjbstatic int 141178479Sjbdt_nbits_128(uint64_t *a) 142178479Sjb{ 143178479Sjb int nbits = 0; 144178479Sjb uint64_t tmp[2]; 145178479Sjb uint64_t zero[2] = { 0, 0 }; 146178479Sjb 147178479Sjb tmp[0] = a[0]; 148178479Sjb tmp[1] = a[1]; 149178479Sjb 150178479Sjb dt_shift_128(tmp, -1); 151178479Sjb while (dt_gt_128(tmp, zero)) { 152178479Sjb dt_shift_128(tmp, -1); 153178479Sjb nbits++; 154178479Sjb } 155178479Sjb 156178479Sjb return (nbits); 157178479Sjb} 158178479Sjb 159178479Sjbstatic void 160178479Sjbdt_subtract_128(uint64_t *minuend, uint64_t *subtrahend, uint64_t *difference) 161178479Sjb{ 162178479Sjb uint64_t result[2]; 163178479Sjb 164178479Sjb result[0] = minuend[0] - subtrahend[0]; 165178479Sjb result[1] = minuend[1] - subtrahend[1] - 166178479Sjb (minuend[0] < subtrahend[0] ? 1 : 0); 167178479Sjb 168178479Sjb difference[0] = result[0]; 169178479Sjb difference[1] = result[1]; 170178479Sjb} 171178479Sjb 172178479Sjbstatic void 173178479Sjbdt_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum) 174178479Sjb{ 175178479Sjb uint64_t result[2]; 176178479Sjb 177178479Sjb result[0] = addend1[0] + addend2[0]; 178178479Sjb result[1] = addend1[1] + addend2[1] + 179178479Sjb (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0); 180178479Sjb 181178479Sjb sum[0] = result[0]; 182178479Sjb sum[1] = result[1]; 183178479Sjb} 184178479Sjb 185178479Sjb/* 186178479Sjb * The basic idea is to break the 2 64-bit values into 4 32-bit values, 187178479Sjb * use native multiplication on those, and then re-combine into the 188178479Sjb * resulting 128-bit value. 189178479Sjb * 190178479Sjb * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) = 191178479Sjb * hi1 * hi2 << 64 + 192178479Sjb * hi1 * lo2 << 32 + 193178479Sjb * hi2 * lo1 << 32 + 194178479Sjb * lo1 * lo2 195178479Sjb */ 196178479Sjbstatic void 197178479Sjbdt_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product) 198178479Sjb{ 199178479Sjb uint64_t hi1, hi2, lo1, lo2; 200178479Sjb uint64_t tmp[2]; 201178479Sjb 202178479Sjb hi1 = factor1 >> 32; 203178479Sjb hi2 = factor2 >> 32; 204178479Sjb 205178479Sjb lo1 = factor1 & DT_MASK_LO; 206178479Sjb lo2 = factor2 & DT_MASK_LO; 207178479Sjb 208178479Sjb product[0] = lo1 * lo2; 209178479Sjb product[1] = hi1 * hi2; 210178479Sjb 211178479Sjb tmp[0] = hi1 * lo2; 212178479Sjb tmp[1] = 0; 213178479Sjb dt_shift_128(tmp, 32); 214178479Sjb dt_add_128(product, tmp, product); 215178479Sjb 216178479Sjb tmp[0] = hi2 * lo1; 217178479Sjb tmp[1] = 0; 218178479Sjb dt_shift_128(tmp, 32); 219178479Sjb dt_add_128(product, tmp, product); 220178479Sjb} 221178479Sjb 222178479Sjb/* 223178479Sjb * This is long-hand division. 224178479Sjb * 225178479Sjb * We initialize subtrahend by shifting divisor left as far as possible. We 226178479Sjb * loop, comparing subtrahend to dividend: if subtrahend is smaller, we 227178479Sjb * subtract and set the appropriate bit in the result. We then shift 228178479Sjb * subtrahend right by one bit for the next comparison. 229178479Sjb */ 230178479Sjbstatic void 231178479Sjbdt_divide_128(uint64_t *dividend, uint64_t divisor, uint64_t *quotient) 232178479Sjb{ 233178479Sjb uint64_t result[2] = { 0, 0 }; 234178479Sjb uint64_t remainder[2]; 235178479Sjb uint64_t subtrahend[2]; 236178479Sjb uint64_t divisor_128[2]; 237178479Sjb uint64_t mask[2] = { 1, 0 }; 238178479Sjb int log = 0; 239178479Sjb 240178479Sjb assert(divisor != 0); 241178479Sjb 242178479Sjb divisor_128[0] = divisor; 243178479Sjb divisor_128[1] = 0; 244178479Sjb 245178479Sjb remainder[0] = dividend[0]; 246178479Sjb remainder[1] = dividend[1]; 247178479Sjb 248178479Sjb subtrahend[0] = divisor; 249178479Sjb subtrahend[1] = 0; 250178479Sjb 251178479Sjb while (divisor > 0) { 252178479Sjb log++; 253178479Sjb divisor >>= 1; 254178479Sjb } 255178479Sjb 256178479Sjb dt_shift_128(subtrahend, 128 - log); 257178479Sjb dt_shift_128(mask, 128 - log); 258178479Sjb 259178479Sjb while (dt_ge_128(remainder, divisor_128)) { 260178479Sjb if (dt_ge_128(remainder, subtrahend)) { 261178479Sjb dt_subtract_128(remainder, subtrahend, remainder); 262178479Sjb result[0] |= mask[0]; 263178479Sjb result[1] |= mask[1]; 264178479Sjb } 265178479Sjb 266178479Sjb dt_shift_128(subtrahend, -1); 267178479Sjb dt_shift_128(mask, -1); 268178479Sjb } 269178479Sjb 270178479Sjb quotient[0] = result[0]; 271178479Sjb quotient[1] = result[1]; 272178479Sjb} 273178479Sjb 274178479Sjb/* 275178479Sjb * This is the long-hand method of calculating a square root. 276178479Sjb * The algorithm is as follows: 277178479Sjb * 278178479Sjb * 1. Group the digits by 2 from the right. 279178479Sjb * 2. Over the leftmost group, find the largest single-digit number 280178479Sjb * whose square is less than that group. 281178479Sjb * 3. Subtract the result of the previous step (2 or 4, depending) and 282178479Sjb * bring down the next two-digit group. 283178479Sjb * 4. For the result R we have so far, find the largest single-digit number 284178479Sjb * x such that 2 * R * 10 * x + x^2 is less than the result from step 3. 285178479Sjb * (Note that this is doubling R and performing a decimal left-shift by 1 286178479Sjb * and searching for the appropriate decimal to fill the one's place.) 287178479Sjb * The value x is the next digit in the square root. 288178479Sjb * Repeat steps 3 and 4 until the desired precision is reached. (We're 289178479Sjb * dealing with integers, so the above is sufficient.) 290178479Sjb * 291178479Sjb * In decimal, the square root of 582,734 would be calculated as so: 292178479Sjb * 293178479Sjb * __7__6__3 294178479Sjb * | 58 27 34 295178479Sjb * -49 (7^2 == 49 => 7 is the first digit in the square root) 296178479Sjb * -- 297178479Sjb * 9 27 (Subtract and bring down the next group.) 298178479Sjb * 146 8 76 (2 * 7 * 10 * 6 + 6^2 == 876 => 6 is the next digit in 299178479Sjb * ----- the square root) 300178479Sjb * 51 34 (Subtract and bring down the next group.) 301178479Sjb * 1523 45 69 (2 * 76 * 10 * 3 + 3^2 == 4569 => 3 is the next digit in 302178479Sjb * ----- the square root) 303178479Sjb * 5 65 (remainder) 304178479Sjb * 305178479Sjb * The above algorithm applies similarly in binary, but note that the 306178479Sjb * only possible non-zero value for x in step 4 is 1, so step 4 becomes a 307178479Sjb * simple decision: is 2 * R * 2 * 1 + 1^2 (aka R << 2 + 1) less than the 308178479Sjb * preceding difference? 309178479Sjb * 310178479Sjb * In binary, the square root of 11011011 would be calculated as so: 311178479Sjb * 312178479Sjb * __1__1__1__0 313178479Sjb * | 11 01 10 11 314178479Sjb * 01 (0 << 2 + 1 == 1 < 11 => this bit is 1) 315178479Sjb * -- 316178479Sjb * 10 01 10 11 317178479Sjb * 101 1 01 (1 << 2 + 1 == 101 < 1001 => next bit is 1) 318178479Sjb * ----- 319178479Sjb * 1 00 10 11 320178479Sjb * 1101 11 01 (11 << 2 + 1 == 1101 < 10010 => next bit is 1) 321178479Sjb * ------- 322178479Sjb * 1 01 11 323178479Sjb * 11101 1 11 01 (111 << 2 + 1 == 11101 > 10111 => last bit is 0) 324178479Sjb * 325178479Sjb */ 326178479Sjbstatic uint64_t 327178479Sjbdt_sqrt_128(uint64_t *square) 328178479Sjb{ 329178479Sjb uint64_t result[2] = { 0, 0 }; 330178479Sjb uint64_t diff[2] = { 0, 0 }; 331178479Sjb uint64_t one[2] = { 1, 0 }; 332178479Sjb uint64_t next_pair[2]; 333178479Sjb uint64_t next_try[2]; 334178479Sjb uint64_t bit_pairs, pair_shift; 335178479Sjb int i; 336178479Sjb 337178479Sjb bit_pairs = dt_nbits_128(square) / 2; 338178479Sjb pair_shift = bit_pairs * 2; 339178479Sjb 340178479Sjb for (i = 0; i <= bit_pairs; i++) { 341178479Sjb /* 342178479Sjb * Bring down the next pair of bits. 343178479Sjb */ 344178479Sjb next_pair[0] = square[0]; 345178479Sjb next_pair[1] = square[1]; 346178479Sjb dt_shift_128(next_pair, -pair_shift); 347178479Sjb next_pair[0] &= 0x3; 348178479Sjb next_pair[1] = 0; 349178479Sjb 350178479Sjb dt_shift_128(diff, 2); 351178479Sjb dt_add_128(diff, next_pair, diff); 352178479Sjb 353178479Sjb /* 354178479Sjb * next_try = R << 2 + 1 355178479Sjb */ 356178479Sjb next_try[0] = result[0]; 357178479Sjb next_try[1] = result[1]; 358178479Sjb dt_shift_128(next_try, 2); 359178479Sjb dt_add_128(next_try, one, next_try); 360178479Sjb 361178479Sjb if (dt_le_128(next_try, diff)) { 362178479Sjb dt_subtract_128(diff, next_try, diff); 363178479Sjb dt_shift_128(result, 1); 364178479Sjb dt_add_128(result, one, result); 365178479Sjb } else { 366178479Sjb dt_shift_128(result, 1); 367178479Sjb } 368178479Sjb 369178479Sjb pair_shift -= 2; 370178479Sjb } 371178479Sjb 372178479Sjb assert(result[1] == 0); 373178479Sjb 374178479Sjb return (result[0]); 375178479Sjb} 376178479Sjb 377178479Sjbuint64_t 378178479Sjbdt_stddev(uint64_t *data, uint64_t normal) 379178479Sjb{ 380178479Sjb uint64_t avg_of_squares[2]; 381178479Sjb uint64_t square_of_avg[2]; 382178479Sjb int64_t norm_avg; 383178479Sjb uint64_t diff[2]; 384178479Sjb 385278114Smarkj if (data[0] == 0) 386278114Smarkj return (0); 387278114Smarkj 388178479Sjb /* 389178479Sjb * The standard approximation for standard deviation is 390178479Sjb * sqrt(average(x**2) - average(x)**2), i.e. the square root 391178479Sjb * of the average of the squares minus the square of the average. 392318783Savg * When normalizing, we should divide the sum of x**2 by normal**2. 393178479Sjb */ 394178479Sjb dt_divide_128(data + 2, normal, avg_of_squares); 395318783Savg dt_divide_128(avg_of_squares, normal, avg_of_squares); 396178479Sjb dt_divide_128(avg_of_squares, data[0], avg_of_squares); 397178479Sjb 398178479Sjb norm_avg = (int64_t)data[1] / (int64_t)normal / (int64_t)data[0]; 399178479Sjb 400178479Sjb if (norm_avg < 0) 401178479Sjb norm_avg = -norm_avg; 402178479Sjb 403178479Sjb dt_multiply_128((uint64_t)norm_avg, (uint64_t)norm_avg, square_of_avg); 404178479Sjb 405178479Sjb dt_subtract_128(avg_of_squares, square_of_avg, diff); 406178479Sjb 407178479Sjb return (dt_sqrt_128(diff)); 408178479Sjb} 409178479Sjb 410178479Sjbstatic int 411178479Sjbdt_flowindent(dtrace_hdl_t *dtp, dtrace_probedata_t *data, dtrace_epid_t last, 412178479Sjb dtrace_bufdesc_t *buf, size_t offs) 413178479Sjb{ 414178479Sjb dtrace_probedesc_t *pd = data->dtpda_pdesc, *npd; 415178479Sjb dtrace_eprobedesc_t *epd = data->dtpda_edesc, *nepd; 416178479Sjb char *p = pd->dtpd_provider, *n = pd->dtpd_name, *sub; 417178479Sjb dtrace_flowkind_t flow = DTRACEFLOW_NONE; 418178479Sjb const char *str = NULL; 419178479Sjb static const char *e_str[2] = { " -> ", " => " }; 420178479Sjb static const char *r_str[2] = { " <- ", " <= " }; 421178479Sjb static const char *ent = "entry", *ret = "return"; 422178479Sjb static int entlen = 0, retlen = 0; 423178479Sjb dtrace_epid_t next, id = epd->dtepd_epid; 424178479Sjb int rval; 425178479Sjb 426178479Sjb if (entlen == 0) { 427178479Sjb assert(retlen == 0); 428178479Sjb entlen = strlen(ent); 429178479Sjb retlen = strlen(ret); 430178479Sjb } 431178479Sjb 432178479Sjb /* 433178479Sjb * If the name of the probe is "entry" or ends with "-entry", we 434178479Sjb * treat it as an entry; if it is "return" or ends with "-return", 435178479Sjb * we treat it as a return. (This allows application-provided probes 436178479Sjb * like "method-entry" or "function-entry" to participate in flow 437178479Sjb * indentation -- without accidentally misinterpreting popular probe 438178479Sjb * names like "carpentry", "gentry" or "Coventry".) 439178479Sjb */ 440178479Sjb if ((sub = strstr(n, ent)) != NULL && sub[entlen] == '\0' && 441178479Sjb (sub == n || sub[-1] == '-')) { 442178479Sjb flow = DTRACEFLOW_ENTRY; 443178479Sjb str = e_str[strcmp(p, "syscall") == 0]; 444178479Sjb } else if ((sub = strstr(n, ret)) != NULL && sub[retlen] == '\0' && 445178479Sjb (sub == n || sub[-1] == '-')) { 446178479Sjb flow = DTRACEFLOW_RETURN; 447178479Sjb str = r_str[strcmp(p, "syscall") == 0]; 448178479Sjb } 449178479Sjb 450178479Sjb /* 451178479Sjb * If we're going to indent this, we need to check the ID of our last 452178479Sjb * call. If we're looking at the same probe ID but a different EPID, 453178479Sjb * we _don't_ want to indent. (Yes, there are some minor holes in 454178479Sjb * this scheme -- it's a heuristic.) 455178479Sjb */ 456178479Sjb if (flow == DTRACEFLOW_ENTRY) { 457178479Sjb if ((last != DTRACE_EPIDNONE && id != last && 458178479Sjb pd->dtpd_id == dtp->dt_pdesc[last]->dtpd_id)) 459178479Sjb flow = DTRACEFLOW_NONE; 460178479Sjb } 461178479Sjb 462178479Sjb /* 463178479Sjb * If we're going to unindent this, it's more difficult to see if 464178479Sjb * we don't actually want to unindent it -- we need to look at the 465178479Sjb * _next_ EPID. 466178479Sjb */ 467178479Sjb if (flow == DTRACEFLOW_RETURN) { 468178479Sjb offs += epd->dtepd_size; 469178479Sjb 470178479Sjb do { 471250574Smarkj if (offs >= buf->dtbd_size) 472250574Smarkj goto out; 473178479Sjb 474178479Sjb next = *(uint32_t *)((uintptr_t)buf->dtbd_data + offs); 475178479Sjb 476178479Sjb if (next == DTRACE_EPIDNONE) 477178479Sjb offs += sizeof (id); 478178479Sjb } while (next == DTRACE_EPIDNONE); 479178479Sjb 480178479Sjb if ((rval = dt_epid_lookup(dtp, next, &nepd, &npd)) != 0) 481178479Sjb return (rval); 482178479Sjb 483178479Sjb if (next != id && npd->dtpd_id == pd->dtpd_id) 484178479Sjb flow = DTRACEFLOW_NONE; 485178479Sjb } 486178479Sjb 487178479Sjbout: 488178479Sjb if (flow == DTRACEFLOW_ENTRY || flow == DTRACEFLOW_RETURN) { 489178479Sjb data->dtpda_prefix = str; 490178479Sjb } else { 491178479Sjb data->dtpda_prefix = "| "; 492178479Sjb } 493178479Sjb 494178479Sjb if (flow == DTRACEFLOW_RETURN && data->dtpda_indent > 0) 495178479Sjb data->dtpda_indent -= 2; 496178479Sjb 497178479Sjb data->dtpda_flow = flow; 498178479Sjb 499178479Sjb return (0); 500178479Sjb} 501178479Sjb 502178479Sjbstatic int 503178479Sjbdt_nullprobe() 504178479Sjb{ 505178479Sjb return (DTRACE_CONSUME_THIS); 506178479Sjb} 507178479Sjb 508178479Sjbstatic int 509178479Sjbdt_nullrec() 510178479Sjb{ 511178479Sjb return (DTRACE_CONSUME_NEXT); 512178479Sjb} 513178479Sjb 514267942Srpaulostatic void 515267942Srpaulodt_quantize_total(dtrace_hdl_t *dtp, int64_t datum, long double *total) 516267942Srpaulo{ 517267942Srpaulo long double val = dt_fabsl((long double)datum); 518267942Srpaulo 519267942Srpaulo if (dtp->dt_options[DTRACEOPT_AGGZOOM] == DTRACEOPT_UNSET) { 520267942Srpaulo *total += val; 521267942Srpaulo return; 522267942Srpaulo } 523267942Srpaulo 524267942Srpaulo /* 525267942Srpaulo * If we're zooming in on an aggregation, we want the height of the 526267942Srpaulo * highest value to be approximately 95% of total bar height -- so we 527267942Srpaulo * adjust up by the reciprocal of DTRACE_AGGZOOM_MAX when comparing to 528267942Srpaulo * our highest value. 529267942Srpaulo */ 530267942Srpaulo val *= 1 / DTRACE_AGGZOOM_MAX; 531267942Srpaulo 532267942Srpaulo if (*total < val) 533267942Srpaulo *total = val; 534267942Srpaulo} 535267942Srpaulo 536267942Srpaulostatic int 537267942Srpaulodt_print_quanthdr(dtrace_hdl_t *dtp, FILE *fp, int width) 538267942Srpaulo{ 539267942Srpaulo return (dt_printf(dtp, fp, "\n%*s %41s %-9s\n", 540267942Srpaulo width ? width : 16, width ? "key" : "value", 541267942Srpaulo "------------- Distribution -------------", "count")); 542267942Srpaulo} 543267942Srpaulo 544267942Srpaulostatic int 545267942Srpaulodt_print_quanthdr_packed(dtrace_hdl_t *dtp, FILE *fp, int width, 546267942Srpaulo const dtrace_aggdata_t *aggdata, dtrace_actkind_t action) 547267942Srpaulo{ 548267942Srpaulo int min = aggdata->dtada_minbin, max = aggdata->dtada_maxbin; 549267942Srpaulo int minwidth, maxwidth, i; 550267942Srpaulo 551267942Srpaulo assert(action == DTRACEAGG_QUANTIZE || action == DTRACEAGG_LQUANTIZE); 552267942Srpaulo 553267942Srpaulo if (action == DTRACEAGG_QUANTIZE) { 554267942Srpaulo if (min != 0 && min != DTRACE_QUANTIZE_ZEROBUCKET) 555267942Srpaulo min--; 556267942Srpaulo 557267942Srpaulo if (max < DTRACE_QUANTIZE_NBUCKETS - 1) 558267942Srpaulo max++; 559267942Srpaulo 560267942Srpaulo minwidth = dt_ndigits(DTRACE_QUANTIZE_BUCKETVAL(min)); 561267942Srpaulo maxwidth = dt_ndigits(DTRACE_QUANTIZE_BUCKETVAL(max)); 562267942Srpaulo } else { 563267942Srpaulo maxwidth = 8; 564267942Srpaulo minwidth = maxwidth - 1; 565267942Srpaulo max++; 566267942Srpaulo } 567267942Srpaulo 568267942Srpaulo if (dt_printf(dtp, fp, "\n%*s %*s .", 569267942Srpaulo width, width > 0 ? "key" : "", minwidth, "min") < 0) 570267942Srpaulo return (-1); 571267942Srpaulo 572267942Srpaulo for (i = min; i <= max; i++) { 573267942Srpaulo if (dt_printf(dtp, fp, "-") < 0) 574267942Srpaulo return (-1); 575267942Srpaulo } 576267942Srpaulo 577267942Srpaulo return (dt_printf(dtp, fp, ". %*s | count\n", -maxwidth, "max")); 578267942Srpaulo} 579267942Srpaulo 580267942Srpaulo/* 581267942Srpaulo * We use a subset of the Unicode Block Elements (U+2588 through U+258F, 582267942Srpaulo * inclusive) to represent aggregations via UTF-8 -- which are expressed via 583267942Srpaulo * 3-byte UTF-8 sequences. 584267942Srpaulo */ 585267942Srpaulo#define DTRACE_AGGUTF8_FULL 0x2588 586267942Srpaulo#define DTRACE_AGGUTF8_BASE 0x258f 587267942Srpaulo#define DTRACE_AGGUTF8_LEVELS 8 588267942Srpaulo 589267942Srpaulo#define DTRACE_AGGUTF8_BYTE0(val) (0xe0 | ((val) >> 12)) 590267942Srpaulo#define DTRACE_AGGUTF8_BYTE1(val) (0x80 | (((val) >> 6) & 0x3f)) 591267942Srpaulo#define DTRACE_AGGUTF8_BYTE2(val) (0x80 | ((val) & 0x3f)) 592267942Srpaulo 593267942Srpaulostatic int 594267942Srpaulodt_print_quantline_utf8(dtrace_hdl_t *dtp, FILE *fp, int64_t val, 595267942Srpaulo uint64_t normal, long double total) 596267942Srpaulo{ 597267942Srpaulo uint_t len = 40, i, whole, partial; 598267942Srpaulo long double f = (dt_fabsl((long double)val) * len) / total; 599267942Srpaulo const char *spaces = " "; 600267942Srpaulo 601267942Srpaulo whole = (uint_t)f; 602267942Srpaulo partial = (uint_t)((f - (long double)(uint_t)f) * 603267942Srpaulo (long double)DTRACE_AGGUTF8_LEVELS); 604267942Srpaulo 605267942Srpaulo if (dt_printf(dtp, fp, "|") < 0) 606267942Srpaulo return (-1); 607267942Srpaulo 608267942Srpaulo for (i = 0; i < whole; i++) { 609267942Srpaulo if (dt_printf(dtp, fp, "%c%c%c", 610267942Srpaulo DTRACE_AGGUTF8_BYTE0(DTRACE_AGGUTF8_FULL), 611267942Srpaulo DTRACE_AGGUTF8_BYTE1(DTRACE_AGGUTF8_FULL), 612267942Srpaulo DTRACE_AGGUTF8_BYTE2(DTRACE_AGGUTF8_FULL)) < 0) 613267942Srpaulo return (-1); 614267942Srpaulo } 615267942Srpaulo 616267942Srpaulo if (partial != 0) { 617267942Srpaulo partial = DTRACE_AGGUTF8_BASE - (partial - 1); 618267942Srpaulo 619267942Srpaulo if (dt_printf(dtp, fp, "%c%c%c", 620267942Srpaulo DTRACE_AGGUTF8_BYTE0(partial), 621267942Srpaulo DTRACE_AGGUTF8_BYTE1(partial), 622267942Srpaulo DTRACE_AGGUTF8_BYTE2(partial)) < 0) 623267942Srpaulo return (-1); 624267942Srpaulo 625267942Srpaulo i++; 626267942Srpaulo } 627267942Srpaulo 628267942Srpaulo return (dt_printf(dtp, fp, "%s %-9lld\n", spaces + i, 629267942Srpaulo (long long)val / normal)); 630267942Srpaulo} 631267942Srpaulo 632267942Srpaulostatic int 633178479Sjbdt_print_quantline(dtrace_hdl_t *dtp, FILE *fp, int64_t val, 634178479Sjb uint64_t normal, long double total, char positives, char negatives) 635178479Sjb{ 636178479Sjb long double f; 637178479Sjb uint_t depth, len = 40; 638178479Sjb 639178479Sjb const char *ats = "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"; 640178479Sjb const char *spaces = " "; 641178479Sjb 642178479Sjb assert(strlen(ats) == len && strlen(spaces) == len); 643178479Sjb assert(!(total == 0 && (positives || negatives))); 644178479Sjb assert(!(val < 0 && !negatives)); 645178479Sjb assert(!(val > 0 && !positives)); 646178479Sjb assert(!(val != 0 && total == 0)); 647178479Sjb 648178479Sjb if (!negatives) { 649178479Sjb if (positives) { 650267942Srpaulo if (dtp->dt_encoding == DT_ENCODING_UTF8) { 651267942Srpaulo return (dt_print_quantline_utf8(dtp, fp, val, 652267942Srpaulo normal, total)); 653267942Srpaulo } 654267942Srpaulo 655178479Sjb f = (dt_fabsl((long double)val) * len) / total; 656178479Sjb depth = (uint_t)(f + 0.5); 657178479Sjb } else { 658178479Sjb depth = 0; 659178479Sjb } 660178479Sjb 661178479Sjb return (dt_printf(dtp, fp, "|%s%s %-9lld\n", ats + len - depth, 662178479Sjb spaces + depth, (long long)val / normal)); 663178479Sjb } 664178479Sjb 665178479Sjb if (!positives) { 666178479Sjb f = (dt_fabsl((long double)val) * len) / total; 667178479Sjb depth = (uint_t)(f + 0.5); 668178479Sjb 669178479Sjb return (dt_printf(dtp, fp, "%s%s| %-9lld\n", spaces + depth, 670178479Sjb ats + len - depth, (long long)val / normal)); 671178479Sjb } 672178479Sjb 673178479Sjb /* 674178479Sjb * If we're here, we have both positive and negative bucket values. 675178479Sjb * To express this graphically, we're going to generate both positive 676178479Sjb * and negative bars separated by a centerline. These bars are half 677178479Sjb * the size of normal quantize()/lquantize() bars, so we divide the 678178479Sjb * length in half before calculating the bar length. 679178479Sjb */ 680178479Sjb len /= 2; 681178479Sjb ats = &ats[len]; 682178479Sjb spaces = &spaces[len]; 683178479Sjb 684178479Sjb f = (dt_fabsl((long double)val) * len) / total; 685178479Sjb depth = (uint_t)(f + 0.5); 686178479Sjb 687178479Sjb if (val <= 0) { 688178479Sjb return (dt_printf(dtp, fp, "%s%s|%*s %-9lld\n", spaces + depth, 689178479Sjb ats + len - depth, len, "", (long long)val / normal)); 690178479Sjb } else { 691178479Sjb return (dt_printf(dtp, fp, "%20s|%s%s %-9lld\n", "", 692178479Sjb ats + len - depth, spaces + depth, 693178479Sjb (long long)val / normal)); 694178479Sjb } 695178479Sjb} 696178479Sjb 697267942Srpaulo/* 698267942Srpaulo * As with UTF-8 printing of aggregations, we use a subset of the Unicode 699267942Srpaulo * Block Elements (U+2581 through U+2588, inclusive) to represent our packed 700267942Srpaulo * aggregation. 701267942Srpaulo */ 702267942Srpaulo#define DTRACE_AGGPACK_BASE 0x2581 703267942Srpaulo#define DTRACE_AGGPACK_LEVELS 8 704267942Srpaulo 705267942Srpaulostatic int 706267942Srpaulodt_print_packed(dtrace_hdl_t *dtp, FILE *fp, 707267942Srpaulo long double datum, long double total) 708267942Srpaulo{ 709267942Srpaulo static boolean_t utf8_checked = B_FALSE; 710267942Srpaulo static boolean_t utf8; 711267942Srpaulo char *ascii = "__xxxxXX"; 712267942Srpaulo char *neg = "vvvvVV"; 713267942Srpaulo unsigned int len; 714267942Srpaulo long double val; 715267942Srpaulo 716267942Srpaulo if (!utf8_checked) { 717267942Srpaulo char *term; 718267942Srpaulo 719267942Srpaulo /* 720267942Srpaulo * We want to determine if we can reasonably emit UTF-8 for our 721267942Srpaulo * packed aggregation. To do this, we will check for terminals 722267942Srpaulo * that are known to be primitive to emit UTF-8 on these. 723267942Srpaulo */ 724267942Srpaulo utf8_checked = B_TRUE; 725267942Srpaulo 726267942Srpaulo if (dtp->dt_encoding == DT_ENCODING_ASCII) { 727267942Srpaulo utf8 = B_FALSE; 728267942Srpaulo } else if (dtp->dt_encoding == DT_ENCODING_UTF8) { 729267942Srpaulo utf8 = B_TRUE; 730267942Srpaulo } else if ((term = getenv("TERM")) != NULL && 731267942Srpaulo (strcmp(term, "sun") == 0 || 732280882Smarkj strcmp(term, "sun-color") == 0 || 733280882Smarkj strcmp(term, "dumb") == 0)) { 734267942Srpaulo utf8 = B_FALSE; 735267942Srpaulo } else { 736267942Srpaulo utf8 = B_TRUE; 737267942Srpaulo } 738267942Srpaulo } 739267942Srpaulo 740267942Srpaulo if (datum == 0) 741267942Srpaulo return (dt_printf(dtp, fp, " ")); 742267942Srpaulo 743267942Srpaulo if (datum < 0) { 744267942Srpaulo len = strlen(neg); 745267942Srpaulo val = dt_fabsl(datum * (len - 1)) / total; 746267942Srpaulo return (dt_printf(dtp, fp, "%c", neg[(uint_t)(val + 0.5)])); 747267942Srpaulo } 748267942Srpaulo 749267942Srpaulo if (utf8) { 750267942Srpaulo int block = DTRACE_AGGPACK_BASE + (unsigned int)(((datum * 751267942Srpaulo (DTRACE_AGGPACK_LEVELS - 1)) / total) + 0.5); 752267942Srpaulo 753267942Srpaulo return (dt_printf(dtp, fp, "%c%c%c", 754267942Srpaulo DTRACE_AGGUTF8_BYTE0(block), 755267942Srpaulo DTRACE_AGGUTF8_BYTE1(block), 756267942Srpaulo DTRACE_AGGUTF8_BYTE2(block))); 757267942Srpaulo } 758267942Srpaulo 759267942Srpaulo len = strlen(ascii); 760267942Srpaulo val = (datum * (len - 1)) / total; 761267942Srpaulo return (dt_printf(dtp, fp, "%c", ascii[(uint_t)(val + 0.5)])); 762267942Srpaulo} 763267942Srpaulo 764178479Sjbint 765178479Sjbdt_print_quantize(dtrace_hdl_t *dtp, FILE *fp, const void *addr, 766178479Sjb size_t size, uint64_t normal) 767178479Sjb{ 768178479Sjb const int64_t *data = addr; 769178479Sjb int i, first_bin = 0, last_bin = DTRACE_QUANTIZE_NBUCKETS - 1; 770178479Sjb long double total = 0; 771178479Sjb char positives = 0, negatives = 0; 772178479Sjb 773178479Sjb if (size != DTRACE_QUANTIZE_NBUCKETS * sizeof (uint64_t)) 774178479Sjb return (dt_set_errno(dtp, EDT_DMISMATCH)); 775178479Sjb 776178479Sjb while (first_bin < DTRACE_QUANTIZE_NBUCKETS - 1 && data[first_bin] == 0) 777178479Sjb first_bin++; 778178479Sjb 779178479Sjb if (first_bin == DTRACE_QUANTIZE_NBUCKETS - 1) { 780178479Sjb /* 781267942Srpaulo * There isn't any data. This is possible if the aggregation 782267942Srpaulo * has been clear()'d or if negative increment values have been 783267942Srpaulo * used. Regardless, we'll print the buckets around 0. 784178479Sjb */ 785178479Sjb first_bin = DTRACE_QUANTIZE_ZEROBUCKET - 1; 786178479Sjb last_bin = DTRACE_QUANTIZE_ZEROBUCKET + 1; 787178479Sjb } else { 788178479Sjb if (first_bin > 0) 789178479Sjb first_bin--; 790178479Sjb 791178479Sjb while (last_bin > 0 && data[last_bin] == 0) 792178479Sjb last_bin--; 793178479Sjb 794178479Sjb if (last_bin < DTRACE_QUANTIZE_NBUCKETS - 1) 795178479Sjb last_bin++; 796178479Sjb } 797178479Sjb 798178479Sjb for (i = first_bin; i <= last_bin; i++) { 799178479Sjb positives |= (data[i] > 0); 800178479Sjb negatives |= (data[i] < 0); 801267942Srpaulo dt_quantize_total(dtp, data[i], &total); 802178479Sjb } 803178479Sjb 804267942Srpaulo if (dt_print_quanthdr(dtp, fp, 0) < 0) 805178479Sjb return (-1); 806178479Sjb 807178479Sjb for (i = first_bin; i <= last_bin; i++) { 808178479Sjb if (dt_printf(dtp, fp, "%16lld ", 809178479Sjb (long long)DTRACE_QUANTIZE_BUCKETVAL(i)) < 0) 810178479Sjb return (-1); 811178479Sjb 812178479Sjb if (dt_print_quantline(dtp, fp, data[i], normal, total, 813178479Sjb positives, negatives) < 0) 814178479Sjb return (-1); 815178479Sjb } 816178479Sjb 817178479Sjb return (0); 818178479Sjb} 819178479Sjb 820178479Sjbint 821267942Srpaulodt_print_quantize_packed(dtrace_hdl_t *dtp, FILE *fp, const void *addr, 822267942Srpaulo size_t size, const dtrace_aggdata_t *aggdata) 823267942Srpaulo{ 824267942Srpaulo const int64_t *data = addr; 825267942Srpaulo long double total = 0, count = 0; 826267942Srpaulo int min = aggdata->dtada_minbin, max = aggdata->dtada_maxbin, i; 827267942Srpaulo int64_t minval, maxval; 828267942Srpaulo 829267942Srpaulo if (size != DTRACE_QUANTIZE_NBUCKETS * sizeof (uint64_t)) 830267942Srpaulo return (dt_set_errno(dtp, EDT_DMISMATCH)); 831267942Srpaulo 832267942Srpaulo if (min != 0 && min != DTRACE_QUANTIZE_ZEROBUCKET) 833267942Srpaulo min--; 834267942Srpaulo 835267942Srpaulo if (max < DTRACE_QUANTIZE_NBUCKETS - 1) 836267942Srpaulo max++; 837267942Srpaulo 838267942Srpaulo minval = DTRACE_QUANTIZE_BUCKETVAL(min); 839267942Srpaulo maxval = DTRACE_QUANTIZE_BUCKETVAL(max); 840267942Srpaulo 841267942Srpaulo if (dt_printf(dtp, fp, " %*lld :", dt_ndigits(minval), 842267942Srpaulo (long long)minval) < 0) 843267942Srpaulo return (-1); 844267942Srpaulo 845267942Srpaulo for (i = min; i <= max; i++) { 846267942Srpaulo dt_quantize_total(dtp, data[i], &total); 847267942Srpaulo count += data[i]; 848267942Srpaulo } 849267942Srpaulo 850267942Srpaulo for (i = min; i <= max; i++) { 851267942Srpaulo if (dt_print_packed(dtp, fp, data[i], total) < 0) 852267942Srpaulo return (-1); 853267942Srpaulo } 854267942Srpaulo 855267942Srpaulo if (dt_printf(dtp, fp, ": %*lld | %lld\n", 856267942Srpaulo -dt_ndigits(maxval), (long long)maxval, (long long)count) < 0) 857267942Srpaulo return (-1); 858267942Srpaulo 859267942Srpaulo return (0); 860267942Srpaulo} 861267942Srpaulo 862267942Srpauloint 863178479Sjbdt_print_lquantize(dtrace_hdl_t *dtp, FILE *fp, const void *addr, 864178479Sjb size_t size, uint64_t normal) 865178479Sjb{ 866178479Sjb const int64_t *data = addr; 867178479Sjb int i, first_bin, last_bin, base; 868178479Sjb uint64_t arg; 869178479Sjb long double total = 0; 870178479Sjb uint16_t step, levels; 871178479Sjb char positives = 0, negatives = 0; 872178479Sjb 873178479Sjb if (size < sizeof (uint64_t)) 874178479Sjb return (dt_set_errno(dtp, EDT_DMISMATCH)); 875178479Sjb 876178479Sjb arg = *data++; 877178479Sjb size -= sizeof (uint64_t); 878178479Sjb 879178479Sjb base = DTRACE_LQUANTIZE_BASE(arg); 880178479Sjb step = DTRACE_LQUANTIZE_STEP(arg); 881178479Sjb levels = DTRACE_LQUANTIZE_LEVELS(arg); 882178479Sjb 883178479Sjb first_bin = 0; 884178479Sjb last_bin = levels + 1; 885178479Sjb 886178479Sjb if (size != sizeof (uint64_t) * (levels + 2)) 887178479Sjb return (dt_set_errno(dtp, EDT_DMISMATCH)); 888178479Sjb 889178479Sjb while (first_bin <= levels + 1 && data[first_bin] == 0) 890178479Sjb first_bin++; 891178479Sjb 892178479Sjb if (first_bin > levels + 1) { 893178479Sjb first_bin = 0; 894178479Sjb last_bin = 2; 895178479Sjb } else { 896178479Sjb if (first_bin > 0) 897178479Sjb first_bin--; 898178479Sjb 899178479Sjb while (last_bin > 0 && data[last_bin] == 0) 900178479Sjb last_bin--; 901178479Sjb 902178479Sjb if (last_bin < levels + 1) 903178479Sjb last_bin++; 904178479Sjb } 905178479Sjb 906178479Sjb for (i = first_bin; i <= last_bin; i++) { 907178479Sjb positives |= (data[i] > 0); 908178479Sjb negatives |= (data[i] < 0); 909267942Srpaulo dt_quantize_total(dtp, data[i], &total); 910178479Sjb } 911178479Sjb 912178479Sjb if (dt_printf(dtp, fp, "\n%16s %41s %-9s\n", "value", 913178479Sjb "------------- Distribution -------------", "count") < 0) 914178479Sjb return (-1); 915178479Sjb 916178479Sjb for (i = first_bin; i <= last_bin; i++) { 917178479Sjb char c[32]; 918178479Sjb int err; 919178479Sjb 920178479Sjb if (i == 0) { 921267942Srpaulo (void) snprintf(c, sizeof (c), "< %d", base); 922178479Sjb err = dt_printf(dtp, fp, "%16s ", c); 923178479Sjb } else if (i == levels + 1) { 924178479Sjb (void) snprintf(c, sizeof (c), ">= %d", 925178479Sjb base + (levels * step)); 926178479Sjb err = dt_printf(dtp, fp, "%16s ", c); 927178479Sjb } else { 928178479Sjb err = dt_printf(dtp, fp, "%16d ", 929178479Sjb base + (i - 1) * step); 930178479Sjb } 931178479Sjb 932178479Sjb if (err < 0 || dt_print_quantline(dtp, fp, data[i], normal, 933178479Sjb total, positives, negatives) < 0) 934178479Sjb return (-1); 935178479Sjb } 936178479Sjb 937178479Sjb return (0); 938178479Sjb} 939178479Sjb 940267942Srpaulo/*ARGSUSED*/ 941237624Spfgint 942267942Srpaulodt_print_lquantize_packed(dtrace_hdl_t *dtp, FILE *fp, const void *addr, 943267942Srpaulo size_t size, const dtrace_aggdata_t *aggdata) 944267942Srpaulo{ 945267942Srpaulo const int64_t *data = addr; 946267942Srpaulo long double total = 0, count = 0; 947267942Srpaulo int min, max, base, err; 948267942Srpaulo uint64_t arg; 949267942Srpaulo uint16_t step, levels; 950267942Srpaulo char c[32]; 951267942Srpaulo unsigned int i; 952267942Srpaulo 953267942Srpaulo if (size < sizeof (uint64_t)) 954267942Srpaulo return (dt_set_errno(dtp, EDT_DMISMATCH)); 955267942Srpaulo 956267942Srpaulo arg = *data++; 957267942Srpaulo size -= sizeof (uint64_t); 958267942Srpaulo 959267942Srpaulo base = DTRACE_LQUANTIZE_BASE(arg); 960267942Srpaulo step = DTRACE_LQUANTIZE_STEP(arg); 961267942Srpaulo levels = DTRACE_LQUANTIZE_LEVELS(arg); 962267942Srpaulo 963267942Srpaulo if (size != sizeof (uint64_t) * (levels + 2)) 964267942Srpaulo return (dt_set_errno(dtp, EDT_DMISMATCH)); 965267942Srpaulo 966267942Srpaulo min = 0; 967267942Srpaulo max = levels + 1; 968267942Srpaulo 969267942Srpaulo if (min == 0) { 970267942Srpaulo (void) snprintf(c, sizeof (c), "< %d", base); 971267942Srpaulo err = dt_printf(dtp, fp, "%8s :", c); 972267942Srpaulo } else { 973267942Srpaulo err = dt_printf(dtp, fp, "%8d :", base + (min - 1) * step); 974267942Srpaulo } 975267942Srpaulo 976267942Srpaulo if (err < 0) 977267942Srpaulo return (-1); 978267942Srpaulo 979267942Srpaulo for (i = min; i <= max; i++) { 980267942Srpaulo dt_quantize_total(dtp, data[i], &total); 981267942Srpaulo count += data[i]; 982267942Srpaulo } 983267942Srpaulo 984267942Srpaulo for (i = min; i <= max; i++) { 985267942Srpaulo if (dt_print_packed(dtp, fp, data[i], total) < 0) 986267942Srpaulo return (-1); 987267942Srpaulo } 988267942Srpaulo 989267942Srpaulo (void) snprintf(c, sizeof (c), ">= %d", base + (levels * step)); 990267942Srpaulo return (dt_printf(dtp, fp, ": %-8s | %lld\n", c, (long long)count)); 991267942Srpaulo} 992267942Srpaulo 993267942Srpauloint 994237624Spfgdt_print_llquantize(dtrace_hdl_t *dtp, FILE *fp, const void *addr, 995237624Spfg size_t size, uint64_t normal) 996237624Spfg{ 997237624Spfg int i, first_bin, last_bin, bin = 1, order, levels; 998237624Spfg uint16_t factor, low, high, nsteps; 999237624Spfg const int64_t *data = addr; 1000237624Spfg int64_t value = 1, next, step; 1001237624Spfg char positives = 0, negatives = 0; 1002237624Spfg long double total = 0; 1003237624Spfg uint64_t arg; 1004237624Spfg char c[32]; 1005237624Spfg 1006237624Spfg if (size < sizeof (uint64_t)) 1007237624Spfg return (dt_set_errno(dtp, EDT_DMISMATCH)); 1008237624Spfg 1009237624Spfg arg = *data++; 1010237624Spfg size -= sizeof (uint64_t); 1011237624Spfg 1012237624Spfg factor = DTRACE_LLQUANTIZE_FACTOR(arg); 1013237624Spfg low = DTRACE_LLQUANTIZE_LOW(arg); 1014237624Spfg high = DTRACE_LLQUANTIZE_HIGH(arg); 1015237624Spfg nsteps = DTRACE_LLQUANTIZE_NSTEP(arg); 1016237624Spfg 1017237624Spfg /* 1018237624Spfg * We don't expect to be handed invalid llquantize() parameters here, 1019237624Spfg * but sanity check them (to a degree) nonetheless. 1020237624Spfg */ 1021237624Spfg if (size > INT32_MAX || factor < 2 || low >= high || 1022237624Spfg nsteps == 0 || factor > nsteps) 1023237624Spfg return (dt_set_errno(dtp, EDT_DMISMATCH)); 1024237624Spfg 1025237624Spfg levels = (int)size / sizeof (uint64_t); 1026237624Spfg 1027237624Spfg first_bin = 0; 1028237624Spfg last_bin = levels - 1; 1029237624Spfg 1030237624Spfg while (first_bin < levels && data[first_bin] == 0) 1031237624Spfg first_bin++; 1032237624Spfg 1033237624Spfg if (first_bin == levels) { 1034237624Spfg first_bin = 0; 1035237624Spfg last_bin = 1; 1036237624Spfg } else { 1037237624Spfg if (first_bin > 0) 1038237624Spfg first_bin--; 1039237624Spfg 1040237624Spfg while (last_bin > 0 && data[last_bin] == 0) 1041237624Spfg last_bin--; 1042237624Spfg 1043237624Spfg if (last_bin < levels - 1) 1044237624Spfg last_bin++; 1045237624Spfg } 1046237624Spfg 1047237624Spfg for (i = first_bin; i <= last_bin; i++) { 1048237624Spfg positives |= (data[i] > 0); 1049237624Spfg negatives |= (data[i] < 0); 1050267942Srpaulo dt_quantize_total(dtp, data[i], &total); 1051237624Spfg } 1052237624Spfg 1053237624Spfg if (dt_printf(dtp, fp, "\n%16s %41s %-9s\n", "value", 1054237624Spfg "------------- Distribution -------------", "count") < 0) 1055237624Spfg return (-1); 1056237624Spfg 1057237624Spfg for (order = 0; order < low; order++) 1058237624Spfg value *= factor; 1059237624Spfg 1060237624Spfg next = value * factor; 1061237624Spfg step = next > nsteps ? next / nsteps : 1; 1062237624Spfg 1063237624Spfg if (first_bin == 0) { 1064237716Spfg (void) snprintf(c, sizeof (c), "< %lld", (long long)value); 1065237624Spfg 1066237624Spfg if (dt_printf(dtp, fp, "%16s ", c) < 0) 1067237624Spfg return (-1); 1068237624Spfg 1069237624Spfg if (dt_print_quantline(dtp, fp, data[0], normal, 1070237624Spfg total, positives, negatives) < 0) 1071237624Spfg return (-1); 1072237624Spfg } 1073237624Spfg 1074237624Spfg while (order <= high) { 1075237624Spfg if (bin >= first_bin && bin <= last_bin) { 1076237624Spfg if (dt_printf(dtp, fp, "%16lld ", (long long)value) < 0) 1077237624Spfg return (-1); 1078237624Spfg 1079237624Spfg if (dt_print_quantline(dtp, fp, data[bin], 1080237624Spfg normal, total, positives, negatives) < 0) 1081237624Spfg return (-1); 1082237624Spfg } 1083237624Spfg 1084237624Spfg assert(value < next); 1085237624Spfg bin++; 1086237624Spfg 1087237624Spfg if ((value += step) != next) 1088237624Spfg continue; 1089237624Spfg 1090237624Spfg next = value * factor; 1091237624Spfg step = next > nsteps ? next / nsteps : 1; 1092237624Spfg order++; 1093237624Spfg } 1094237624Spfg 1095237624Spfg if (last_bin < bin) 1096237624Spfg return (0); 1097237624Spfg 1098237624Spfg assert(last_bin == bin); 1099238071Sdim (void) snprintf(c, sizeof (c), ">= %lld", (long long)value); 1100237624Spfg 1101237624Spfg if (dt_printf(dtp, fp, "%16s ", c) < 0) 1102237624Spfg return (-1); 1103237624Spfg 1104237624Spfg return (dt_print_quantline(dtp, fp, data[bin], normal, 1105237624Spfg total, positives, negatives)); 1106237624Spfg} 1107237624Spfg 1108178479Sjb/*ARGSUSED*/ 1109178479Sjbstatic int 1110178479Sjbdt_print_average(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr, 1111178479Sjb size_t size, uint64_t normal) 1112178479Sjb{ 1113178479Sjb /* LINTED - alignment */ 1114178479Sjb int64_t *data = (int64_t *)addr; 1115178479Sjb 1116178479Sjb return (dt_printf(dtp, fp, " %16lld", data[0] ? 1117178479Sjb (long long)(data[1] / (int64_t)normal / data[0]) : 0)); 1118178479Sjb} 1119178479Sjb 1120178479Sjb/*ARGSUSED*/ 1121178479Sjbstatic int 1122178479Sjbdt_print_stddev(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr, 1123178479Sjb size_t size, uint64_t normal) 1124178479Sjb{ 1125178479Sjb /* LINTED - alignment */ 1126178479Sjb uint64_t *data = (uint64_t *)addr; 1127178479Sjb 1128178479Sjb return (dt_printf(dtp, fp, " %16llu", data[0] ? 1129178479Sjb (unsigned long long) dt_stddev(data, normal) : 0)); 1130178479Sjb} 1131178479Sjb 1132178479Sjb/*ARGSUSED*/ 1133267942Srpaulostatic int 1134178479Sjbdt_print_bytes(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr, 1135248690Spfg size_t nbytes, int width, int quiet, int forceraw) 1136178479Sjb{ 1137178479Sjb /* 1138178479Sjb * If the byte stream is a series of printable characters, followed by 1139178479Sjb * a terminating byte, we print it out as a string. Otherwise, we 1140178479Sjb * assume that it's something else and just print the bytes. 1141178479Sjb */ 1142178479Sjb int i, j, margin = 5; 1143178479Sjb char *c = (char *)addr; 1144178479Sjb 1145178479Sjb if (nbytes == 0) 1146178479Sjb return (0); 1147178479Sjb 1148248690Spfg if (forceraw) 1149178479Sjb goto raw; 1150178479Sjb 1151248690Spfg if (dtp->dt_options[DTRACEOPT_RAWBYTES] != DTRACEOPT_UNSET) 1152248690Spfg goto raw; 1153248690Spfg 1154178479Sjb for (i = 0; i < nbytes; i++) { 1155178479Sjb /* 1156178479Sjb * We define a "printable character" to be one for which 1157178479Sjb * isprint(3C) returns non-zero, isspace(3C) returns non-zero, 1158178479Sjb * or a character which is either backspace or the bell. 1159178479Sjb * Backspace and the bell are regrettably special because 1160178479Sjb * they fail the first two tests -- and yet they are entirely 1161178479Sjb * printable. These are the only two control characters that 1162178479Sjb * have meaning for the terminal and for which isprint(3C) and 1163178479Sjb * isspace(3C) return 0. 1164178479Sjb */ 1165178479Sjb if (isprint(c[i]) || isspace(c[i]) || 1166178479Sjb c[i] == '\b' || c[i] == '\a') 1167178479Sjb continue; 1168178479Sjb 1169178479Sjb if (c[i] == '\0' && i > 0) { 1170178479Sjb /* 1171178479Sjb * This looks like it might be a string. Before we 1172178479Sjb * assume that it is indeed a string, check the 1173178479Sjb * remainder of the byte range; if it contains 1174178479Sjb * additional non-nul characters, we'll assume that 1175178479Sjb * it's a binary stream that just happens to look like 1176178479Sjb * a string, and we'll print out the individual bytes. 1177178479Sjb */ 1178178479Sjb for (j = i + 1; j < nbytes; j++) { 1179178479Sjb if (c[j] != '\0') 1180178479Sjb break; 1181178479Sjb } 1182178479Sjb 1183178479Sjb if (j != nbytes) 1184178479Sjb break; 1185178479Sjb 1186267942Srpaulo if (quiet) { 1187178479Sjb return (dt_printf(dtp, fp, "%s", c)); 1188267942Srpaulo } else { 1189267942Srpaulo return (dt_printf(dtp, fp, " %s%*s", 1190267942Srpaulo width < 0 ? " " : "", width, c)); 1191267942Srpaulo } 1192178479Sjb } 1193178479Sjb 1194178479Sjb break; 1195178479Sjb } 1196178479Sjb 1197178479Sjb if (i == nbytes) { 1198178479Sjb /* 1199178479Sjb * The byte range is all printable characters, but there is 1200178479Sjb * no trailing nul byte. We'll assume that it's a string and 1201178479Sjb * print it as such. 1202178479Sjb */ 1203178479Sjb char *s = alloca(nbytes + 1); 1204178479Sjb bcopy(c, s, nbytes); 1205178479Sjb s[nbytes] = '\0'; 1206178479Sjb return (dt_printf(dtp, fp, " %-*s", width, s)); 1207178479Sjb } 1208178479Sjb 1209178479Sjbraw: 1210178479Sjb if (dt_printf(dtp, fp, "\n%*s ", margin, "") < 0) 1211178479Sjb return (-1); 1212178479Sjb 1213178479Sjb for (i = 0; i < 16; i++) 1214178479Sjb if (dt_printf(dtp, fp, " %c", "0123456789abcdef"[i]) < 0) 1215178479Sjb return (-1); 1216178479Sjb 1217178479Sjb if (dt_printf(dtp, fp, " 0123456789abcdef\n") < 0) 1218178479Sjb return (-1); 1219178479Sjb 1220178479Sjb 1221178479Sjb for (i = 0; i < nbytes; i += 16) { 1222178479Sjb if (dt_printf(dtp, fp, "%*s%5x:", margin, "", i) < 0) 1223178479Sjb return (-1); 1224178479Sjb 1225178479Sjb for (j = i; j < i + 16 && j < nbytes; j++) { 1226178479Sjb if (dt_printf(dtp, fp, " %02x", (uchar_t)c[j]) < 0) 1227178479Sjb return (-1); 1228178479Sjb } 1229178479Sjb 1230178479Sjb while (j++ % 16) { 1231178479Sjb if (dt_printf(dtp, fp, " ") < 0) 1232178479Sjb return (-1); 1233178479Sjb } 1234178479Sjb 1235178479Sjb if (dt_printf(dtp, fp, " ") < 0) 1236178479Sjb return (-1); 1237178479Sjb 1238178479Sjb for (j = i; j < i + 16 && j < nbytes; j++) { 1239178479Sjb if (dt_printf(dtp, fp, "%c", 1240178479Sjb c[j] < ' ' || c[j] > '~' ? '.' : c[j]) < 0) 1241178479Sjb return (-1); 1242178479Sjb } 1243178479Sjb 1244178479Sjb if (dt_printf(dtp, fp, "\n") < 0) 1245178479Sjb return (-1); 1246178479Sjb } 1247178479Sjb 1248178479Sjb return (0); 1249178479Sjb} 1250178479Sjb 1251178479Sjbint 1252178479Sjbdt_print_stack(dtrace_hdl_t *dtp, FILE *fp, const char *format, 1253178479Sjb caddr_t addr, int depth, int size) 1254178479Sjb{ 1255178479Sjb dtrace_syminfo_t dts; 1256178479Sjb GElf_Sym sym; 1257178479Sjb int i, indent; 1258178479Sjb char c[PATH_MAX * 2]; 1259178479Sjb uint64_t pc; 1260178479Sjb 1261178479Sjb if (dt_printf(dtp, fp, "\n") < 0) 1262178479Sjb return (-1); 1263178479Sjb 1264178479Sjb if (format == NULL) 1265178479Sjb format = "%s"; 1266178479Sjb 1267178479Sjb if (dtp->dt_options[DTRACEOPT_STACKINDENT] != DTRACEOPT_UNSET) 1268178479Sjb indent = (int)dtp->dt_options[DTRACEOPT_STACKINDENT]; 1269178479Sjb else 1270178479Sjb indent = _dtrace_stkindent; 1271178479Sjb 1272178479Sjb for (i = 0; i < depth; i++) { 1273178479Sjb switch (size) { 1274178479Sjb case sizeof (uint32_t): 1275178479Sjb /* LINTED - alignment */ 1276178479Sjb pc = *((uint32_t *)addr); 1277178479Sjb break; 1278178479Sjb 1279178479Sjb case sizeof (uint64_t): 1280178479Sjb /* LINTED - alignment */ 1281178479Sjb pc = *((uint64_t *)addr); 1282178479Sjb break; 1283178479Sjb 1284178479Sjb default: 1285178479Sjb return (dt_set_errno(dtp, EDT_BADSTACKPC)); 1286178479Sjb } 1287178479Sjb 1288178576Sjb if (pc == 0) 1289178479Sjb break; 1290178479Sjb 1291178479Sjb addr += size; 1292178479Sjb 1293178479Sjb if (dt_printf(dtp, fp, "%*s", indent, "") < 0) 1294178479Sjb return (-1); 1295178479Sjb 1296178479Sjb if (dtrace_lookup_by_addr(dtp, pc, &sym, &dts) == 0) { 1297178479Sjb if (pc > sym.st_value) { 1298178479Sjb (void) snprintf(c, sizeof (c), "%s`%s+0x%llx", 1299178479Sjb dts.dts_object, dts.dts_name, 1300228579Sdim (u_longlong_t)(pc - sym.st_value)); 1301178479Sjb } else { 1302178479Sjb (void) snprintf(c, sizeof (c), "%s`%s", 1303178479Sjb dts.dts_object, dts.dts_name); 1304178479Sjb } 1305178479Sjb } else { 1306178479Sjb /* 1307178479Sjb * We'll repeat the lookup, but this time we'll specify 1308178479Sjb * a NULL GElf_Sym -- indicating that we're only 1309178479Sjb * interested in the containing module. 1310178479Sjb */ 1311178479Sjb if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0) { 1312178479Sjb (void) snprintf(c, sizeof (c), "%s`0x%llx", 1313228579Sdim dts.dts_object, (u_longlong_t)pc); 1314178479Sjb } else { 1315228579Sdim (void) snprintf(c, sizeof (c), "0x%llx", 1316228579Sdim (u_longlong_t)pc); 1317178479Sjb } 1318178479Sjb } 1319178479Sjb 1320178479Sjb if (dt_printf(dtp, fp, format, c) < 0) 1321178479Sjb return (-1); 1322178479Sjb 1323178479Sjb if (dt_printf(dtp, fp, "\n") < 0) 1324178479Sjb return (-1); 1325178479Sjb } 1326178479Sjb 1327178479Sjb return (0); 1328178479Sjb} 1329178479Sjb 1330178479Sjbint 1331178479Sjbdt_print_ustack(dtrace_hdl_t *dtp, FILE *fp, const char *format, 1332178479Sjb caddr_t addr, uint64_t arg) 1333178479Sjb{ 1334178479Sjb /* LINTED - alignment */ 1335178479Sjb uint64_t *pc = (uint64_t *)addr; 1336178479Sjb uint32_t depth = DTRACE_USTACK_NFRAMES(arg); 1337178479Sjb uint32_t strsize = DTRACE_USTACK_STRSIZE(arg); 1338178479Sjb const char *strbase = addr + (depth + 1) * sizeof (uint64_t); 1339178479Sjb const char *str = strsize ? strbase : NULL; 1340178479Sjb int err = 0; 1341178479Sjb 1342178479Sjb char name[PATH_MAX], objname[PATH_MAX], c[PATH_MAX * 2]; 1343178479Sjb struct ps_prochandle *P; 1344178479Sjb GElf_Sym sym; 1345178479Sjb int i, indent; 1346178479Sjb pid_t pid; 1347178479Sjb 1348178479Sjb if (depth == 0) 1349178479Sjb return (0); 1350178479Sjb 1351178479Sjb pid = (pid_t)*pc++; 1352178479Sjb 1353178479Sjb if (dt_printf(dtp, fp, "\n") < 0) 1354178479Sjb return (-1); 1355178479Sjb 1356178479Sjb if (format == NULL) 1357178479Sjb format = "%s"; 1358178479Sjb 1359178479Sjb if (dtp->dt_options[DTRACEOPT_STACKINDENT] != DTRACEOPT_UNSET) 1360178479Sjb indent = (int)dtp->dt_options[DTRACEOPT_STACKINDENT]; 1361178479Sjb else 1362178479Sjb indent = _dtrace_stkindent; 1363178479Sjb 1364178479Sjb /* 1365178479Sjb * Ultimately, we need to add an entry point in the library vector for 1366178479Sjb * determining <symbol, offset> from <pid, address>. For now, if 1367178479Sjb * this is a vector open, we just print the raw address or string. 1368178479Sjb */ 1369178479Sjb if (dtp->dt_vector == NULL) 1370178479Sjb P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0); 1371178479Sjb else 1372178479Sjb P = NULL; 1373178479Sjb 1374178479Sjb if (P != NULL) 1375178479Sjb dt_proc_lock(dtp, P); /* lock handle while we perform lookups */ 1376178479Sjb 1377178576Sjb for (i = 0; i < depth && pc[i] != 0; i++) { 1378178479Sjb const prmap_t *map; 1379178479Sjb 1380178479Sjb if ((err = dt_printf(dtp, fp, "%*s", indent, "")) < 0) 1381178479Sjb break; 1382178479Sjb 1383178479Sjb if (P != NULL && Plookup_by_addr(P, pc[i], 1384178479Sjb name, sizeof (name), &sym) == 0) { 1385178479Sjb (void) Pobjname(P, pc[i], objname, sizeof (objname)); 1386178479Sjb 1387178479Sjb if (pc[i] > sym.st_value) { 1388178479Sjb (void) snprintf(c, sizeof (c), 1389178479Sjb "%s`%s+0x%llx", dt_basename(objname), name, 1390178479Sjb (u_longlong_t)(pc[i] - sym.st_value)); 1391178479Sjb } else { 1392178479Sjb (void) snprintf(c, sizeof (c), 1393178479Sjb "%s`%s", dt_basename(objname), name); 1394178479Sjb } 1395178479Sjb } else if (str != NULL && str[0] != '\0' && str[0] != '@' && 1396178479Sjb (P != NULL && ((map = Paddr_to_map(P, pc[i])) == NULL || 1397178479Sjb (map->pr_mflags & MA_WRITE)))) { 1398178479Sjb /* 1399178479Sjb * If the current string pointer in the string table 1400178479Sjb * does not point to an empty string _and_ the program 1401178479Sjb * counter falls in a writable region, we'll use the 1402178479Sjb * string from the string table instead of the raw 1403178479Sjb * address. This last condition is necessary because 1404178479Sjb * some (broken) ustack helpers will return a string 1405178479Sjb * even for a program counter that they can't 1406178479Sjb * identify. If we have a string for a program 1407178479Sjb * counter that falls in a segment that isn't 1408178479Sjb * writable, we assume that we have fallen into this 1409178479Sjb * case and we refuse to use the string. 1410178479Sjb */ 1411178479Sjb (void) snprintf(c, sizeof (c), "%s", str); 1412178479Sjb } else { 1413178479Sjb if (P != NULL && Pobjname(P, pc[i], objname, 1414178576Sjb sizeof (objname)) != 0) { 1415178479Sjb (void) snprintf(c, sizeof (c), "%s`0x%llx", 1416178479Sjb dt_basename(objname), (u_longlong_t)pc[i]); 1417178479Sjb } else { 1418178479Sjb (void) snprintf(c, sizeof (c), "0x%llx", 1419178479Sjb (u_longlong_t)pc[i]); 1420178479Sjb } 1421178479Sjb } 1422178479Sjb 1423178479Sjb if ((err = dt_printf(dtp, fp, format, c)) < 0) 1424178479Sjb break; 1425178479Sjb 1426178479Sjb if ((err = dt_printf(dtp, fp, "\n")) < 0) 1427178479Sjb break; 1428178479Sjb 1429178479Sjb if (str != NULL && str[0] == '@') { 1430178479Sjb /* 1431178479Sjb * If the first character of the string is an "at" sign, 1432178479Sjb * then the string is inferred to be an annotation -- 1433178479Sjb * and it is printed out beneath the frame and offset 1434178479Sjb * with brackets. 1435178479Sjb */ 1436178479Sjb if ((err = dt_printf(dtp, fp, "%*s", indent, "")) < 0) 1437178479Sjb break; 1438178479Sjb 1439178479Sjb (void) snprintf(c, sizeof (c), " [ %s ]", &str[1]); 1440178479Sjb 1441178479Sjb if ((err = dt_printf(dtp, fp, format, c)) < 0) 1442178479Sjb break; 1443178479Sjb 1444178479Sjb if ((err = dt_printf(dtp, fp, "\n")) < 0) 1445178479Sjb break; 1446178479Sjb } 1447178479Sjb 1448178479Sjb if (str != NULL) { 1449178479Sjb str += strlen(str) + 1; 1450178479Sjb if (str - strbase >= strsize) 1451178479Sjb str = NULL; 1452178479Sjb } 1453178479Sjb } 1454178479Sjb 1455178479Sjb if (P != NULL) { 1456178479Sjb dt_proc_unlock(dtp, P); 1457178479Sjb dt_proc_release(dtp, P); 1458178479Sjb } 1459178479Sjb 1460178479Sjb return (err); 1461178479Sjb} 1462178479Sjb 1463178479Sjbstatic int 1464178479Sjbdt_print_usym(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr, dtrace_actkind_t act) 1465178479Sjb{ 1466178479Sjb /* LINTED - alignment */ 1467178479Sjb uint64_t pid = ((uint64_t *)addr)[0]; 1468178479Sjb /* LINTED - alignment */ 1469178479Sjb uint64_t pc = ((uint64_t *)addr)[1]; 1470178479Sjb const char *format = " %-50s"; 1471178479Sjb char *s; 1472178479Sjb int n, len = 256; 1473178479Sjb 1474178479Sjb if (act == DTRACEACT_USYM && dtp->dt_vector == NULL) { 1475178479Sjb struct ps_prochandle *P; 1476178479Sjb 1477178479Sjb if ((P = dt_proc_grab(dtp, pid, 1478178479Sjb PGRAB_RDONLY | PGRAB_FORCE, 0)) != NULL) { 1479178479Sjb GElf_Sym sym; 1480178479Sjb 1481178479Sjb dt_proc_lock(dtp, P); 1482178479Sjb 1483178479Sjb if (Plookup_by_addr(P, pc, NULL, 0, &sym) == 0) 1484178479Sjb pc = sym.st_value; 1485178479Sjb 1486178479Sjb dt_proc_unlock(dtp, P); 1487178479Sjb dt_proc_release(dtp, P); 1488178479Sjb } 1489178479Sjb } 1490178479Sjb 1491178479Sjb do { 1492178479Sjb n = len; 1493178479Sjb s = alloca(n); 1494210767Srpaulo } while ((len = dtrace_uaddr2str(dtp, pid, pc, s, n)) > n); 1495178479Sjb 1496178479Sjb return (dt_printf(dtp, fp, format, s)); 1497178479Sjb} 1498178479Sjb 1499178479Sjbint 1500178479Sjbdt_print_umod(dtrace_hdl_t *dtp, FILE *fp, const char *format, caddr_t addr) 1501178479Sjb{ 1502178479Sjb /* LINTED - alignment */ 1503178479Sjb uint64_t pid = ((uint64_t *)addr)[0]; 1504178479Sjb /* LINTED - alignment */ 1505178479Sjb uint64_t pc = ((uint64_t *)addr)[1]; 1506178479Sjb int err = 0; 1507178479Sjb 1508178479Sjb char objname[PATH_MAX], c[PATH_MAX * 2]; 1509178479Sjb struct ps_prochandle *P; 1510178479Sjb 1511178479Sjb if (format == NULL) 1512178479Sjb format = " %-50s"; 1513178479Sjb 1514178479Sjb /* 1515178479Sjb * See the comment in dt_print_ustack() for the rationale for 1516178479Sjb * printing raw addresses in the vectored case. 1517178479Sjb */ 1518178479Sjb if (dtp->dt_vector == NULL) 1519178479Sjb P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0); 1520178479Sjb else 1521178479Sjb P = NULL; 1522178479Sjb 1523178479Sjb if (P != NULL) 1524178479Sjb dt_proc_lock(dtp, P); /* lock handle while we perform lookups */ 1525178479Sjb 1526178576Sjb if (P != NULL && Pobjname(P, pc, objname, sizeof (objname)) != 0) { 1527178479Sjb (void) snprintf(c, sizeof (c), "%s", dt_basename(objname)); 1528178479Sjb } else { 1529178479Sjb (void) snprintf(c, sizeof (c), "0x%llx", (u_longlong_t)pc); 1530178479Sjb } 1531178479Sjb 1532178479Sjb err = dt_printf(dtp, fp, format, c); 1533178479Sjb 1534178479Sjb if (P != NULL) { 1535178479Sjb dt_proc_unlock(dtp, P); 1536178479Sjb dt_proc_release(dtp, P); 1537178479Sjb } 1538178479Sjb 1539178479Sjb return (err); 1540178479Sjb} 1541178479Sjb 1542178576Sjbint 1543178576Sjbdt_print_memory(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr) 1544178576Sjb{ 1545178576Sjb int quiet = (dtp->dt_options[DTRACEOPT_QUIET] != DTRACEOPT_UNSET); 1546178576Sjb size_t nbytes = *((uintptr_t *) addr); 1547178576Sjb 1548178576Sjb return (dt_print_bytes(dtp, fp, addr + sizeof(uintptr_t), 1549178576Sjb nbytes, 50, quiet, 1)); 1550178576Sjb} 1551178576Sjb 1552178576Sjbtypedef struct dt_type_cbdata { 1553178576Sjb dtrace_hdl_t *dtp; 1554178576Sjb dtrace_typeinfo_t dtt; 1555178576Sjb caddr_t addr; 1556178576Sjb caddr_t addrend; 1557178576Sjb const char *name; 1558178576Sjb int f_type; 1559178576Sjb int indent; 1560178576Sjb int type_width; 1561178576Sjb int name_width; 1562178576Sjb FILE *fp; 1563178576Sjb} dt_type_cbdata_t; 1564178576Sjb 1565178576Sjbstatic int dt_print_type_data(dt_type_cbdata_t *, ctf_id_t); 1566178576Sjb 1567178479Sjbstatic int 1568178576Sjbdt_print_type_member(const char *name, ctf_id_t type, ulong_t off, void *arg) 1569178576Sjb{ 1570178576Sjb dt_type_cbdata_t cbdata; 1571178576Sjb dt_type_cbdata_t *cbdatap = arg; 1572178576Sjb ssize_t ssz; 1573178576Sjb 1574178576Sjb if ((ssz = ctf_type_size(cbdatap->dtt.dtt_ctfp, type)) <= 0) 1575178576Sjb return (0); 1576178576Sjb 1577178576Sjb off /= 8; 1578178576Sjb 1579178576Sjb cbdata = *cbdatap; 1580178576Sjb cbdata.name = name; 1581178576Sjb cbdata.addr += off; 1582178576Sjb cbdata.addrend = cbdata.addr + ssz; 1583178576Sjb 1584178576Sjb return (dt_print_type_data(&cbdata, type)); 1585178576Sjb} 1586178576Sjb 1587178576Sjbstatic int 1588178576Sjbdt_print_type_width(const char *name, ctf_id_t type, ulong_t off, void *arg) 1589178576Sjb{ 1590178576Sjb char buf[DT_TYPE_NAMELEN]; 1591178576Sjb char *p; 1592178576Sjb dt_type_cbdata_t *cbdatap = arg; 1593178576Sjb size_t sz = strlen(name); 1594178576Sjb 1595178576Sjb ctf_type_name(cbdatap->dtt.dtt_ctfp, type, buf, sizeof (buf)); 1596178576Sjb 1597178576Sjb if ((p = strchr(buf, '[')) != NULL) 1598178576Sjb p[-1] = '\0'; 1599178576Sjb else 1600178576Sjb p = ""; 1601178576Sjb 1602178576Sjb sz += strlen(p); 1603178576Sjb 1604178576Sjb if (sz > cbdatap->name_width) 1605178576Sjb cbdatap->name_width = sz; 1606178576Sjb 1607178576Sjb sz = strlen(buf); 1608178576Sjb 1609178576Sjb if (sz > cbdatap->type_width) 1610178576Sjb cbdatap->type_width = sz; 1611178576Sjb 1612178576Sjb return (0); 1613178576Sjb} 1614178576Sjb 1615178576Sjbstatic int 1616178576Sjbdt_print_type_data(dt_type_cbdata_t *cbdatap, ctf_id_t type) 1617178576Sjb{ 1618178576Sjb caddr_t addr = cbdatap->addr; 1619178576Sjb caddr_t addrend = cbdatap->addrend; 1620178576Sjb char buf[DT_TYPE_NAMELEN]; 1621178576Sjb char *p; 1622178576Sjb int cnt = 0; 1623178576Sjb uint_t kind = ctf_type_kind(cbdatap->dtt.dtt_ctfp, type); 1624178576Sjb ssize_t ssz = ctf_type_size(cbdatap->dtt.dtt_ctfp, type); 1625178576Sjb 1626178576Sjb ctf_type_name(cbdatap->dtt.dtt_ctfp, type, buf, sizeof (buf)); 1627178576Sjb 1628178576Sjb if ((p = strchr(buf, '[')) != NULL) 1629178576Sjb p[-1] = '\0'; 1630178576Sjb else 1631178576Sjb p = ""; 1632178576Sjb 1633178576Sjb if (cbdatap->f_type) { 1634178576Sjb int type_width = roundup(cbdatap->type_width + 1, 4); 1635178576Sjb int name_width = roundup(cbdatap->name_width + 1, 4); 1636178576Sjb 1637178576Sjb name_width -= strlen(cbdatap->name); 1638178576Sjb 1639178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "%*s%-*s%s%-*s = ",cbdatap->indent * 4,"",type_width,buf,cbdatap->name,name_width,p); 1640178576Sjb } 1641178576Sjb 1642178576Sjb while (addr < addrend) { 1643178576Sjb dt_type_cbdata_t cbdata; 1644178576Sjb ctf_arinfo_t arinfo; 1645178576Sjb ctf_encoding_t cte; 1646178576Sjb uintptr_t *up; 1647178576Sjb void *vp = addr; 1648178576Sjb cbdata = *cbdatap; 1649178576Sjb cbdata.name = ""; 1650178576Sjb cbdata.addr = addr; 1651178576Sjb cbdata.addrend = addr + ssz; 1652178576Sjb cbdata.f_type = 0; 1653178576Sjb cbdata.indent++; 1654178576Sjb cbdata.type_width = 0; 1655178576Sjb cbdata.name_width = 0; 1656178576Sjb 1657178576Sjb if (cnt > 0) 1658178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "%*s", cbdatap->indent * 4,""); 1659178576Sjb 1660178576Sjb switch (kind) { 1661178576Sjb case CTF_K_INTEGER: 1662178576Sjb if (ctf_type_encoding(cbdatap->dtt.dtt_ctfp, type, &cte) != 0) 1663178576Sjb return (-1); 1664178576Sjb if ((cte.cte_format & CTF_INT_SIGNED) != 0) 1665178576Sjb switch (cte.cte_bits) { 1666178576Sjb case 8: 1667178576Sjb if (isprint(*((char *) vp))) 1668178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "'%c', ", *((char *) vp)); 1669178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "%d (0x%x);\n", *((char *) vp), *((char *) vp)); 1670178576Sjb break; 1671178576Sjb case 16: 1672178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "%hd (0x%hx);\n", *((short *) vp), *((u_short *) vp)); 1673178576Sjb break; 1674178576Sjb case 32: 1675178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "%d (0x%x);\n", *((int *) vp), *((u_int *) vp)); 1676178576Sjb break; 1677178576Sjb case 64: 1678178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "%jd (0x%jx);\n", *((long long *) vp), *((unsigned long long *) vp)); 1679178576Sjb break; 1680178576Sjb default: 1681178576Sjb 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); 1682178576Sjb break; 1683178576Sjb } 1684178576Sjb else 1685178576Sjb switch (cte.cte_bits) { 1686178576Sjb case 8: 1687178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "%u (0x%x);\n", *((uint8_t *) vp) & 0xff, *((uint8_t *) vp) & 0xff); 1688178576Sjb break; 1689178576Sjb case 16: 1690178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "%hu (0x%hx);\n", *((u_short *) vp), *((u_short *) vp)); 1691178576Sjb break; 1692178576Sjb case 32: 1693178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "%u (0x%x);\n", *((u_int *) vp), *((u_int *) vp)); 1694178576Sjb break; 1695178576Sjb case 64: 1696178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "%ju (0x%jx);\n", *((unsigned long long *) vp), *((unsigned long long *) vp)); 1697178576Sjb break; 1698178576Sjb default: 1699178576Sjb 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); 1700178576Sjb break; 1701178576Sjb } 1702178576Sjb break; 1703178576Sjb case CTF_K_FLOAT: 1704178576Sjb 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); 1705178576Sjb break; 1706178576Sjb case CTF_K_POINTER: 1707178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "%p;\n", *((void **) addr)); 1708178576Sjb break; 1709178576Sjb case CTF_K_ARRAY: 1710178576Sjb if (ctf_array_info(cbdatap->dtt.dtt_ctfp, type, &arinfo) != 0) 1711178576Sjb return (-1); 1712178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "{\n%*s",cbdata.indent * 4,""); 1713178576Sjb dt_print_type_data(&cbdata, arinfo.ctr_contents); 1714178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "%*s};\n",cbdatap->indent * 4,""); 1715178576Sjb break; 1716178576Sjb case CTF_K_FUNCTION: 1717178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "CTF_K_FUNCTION:\n"); 1718178576Sjb break; 1719178576Sjb case CTF_K_STRUCT: 1720178576Sjb cbdata.f_type = 1; 1721178576Sjb if (ctf_member_iter(cbdatap->dtt.dtt_ctfp, type, 1722178576Sjb dt_print_type_width, &cbdata) != 0) 1723178576Sjb return (-1); 1724178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "{\n"); 1725178576Sjb if (ctf_member_iter(cbdatap->dtt.dtt_ctfp, type, 1726178576Sjb dt_print_type_member, &cbdata) != 0) 1727178576Sjb return (-1); 1728178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "%*s};\n",cbdatap->indent * 4,""); 1729178576Sjb break; 1730178576Sjb case CTF_K_UNION: 1731178576Sjb cbdata.f_type = 1; 1732178576Sjb if (ctf_member_iter(cbdatap->dtt.dtt_ctfp, type, 1733178576Sjb dt_print_type_width, &cbdata) != 0) 1734178576Sjb return (-1); 1735178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "{\n"); 1736178576Sjb if (ctf_member_iter(cbdatap->dtt.dtt_ctfp, type, 1737178576Sjb dt_print_type_member, &cbdata) != 0) 1738178576Sjb return (-1); 1739178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "%*s};\n",cbdatap->indent * 4,""); 1740178576Sjb break; 1741178576Sjb case CTF_K_ENUM: 1742178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "%s;\n", ctf_enum_name(cbdatap->dtt.dtt_ctfp, type, *((int *) vp))); 1743178576Sjb break; 1744178576Sjb case CTF_K_TYPEDEF: 1745178576Sjb dt_print_type_data(&cbdata, ctf_type_reference(cbdatap->dtt.dtt_ctfp,type)); 1746178576Sjb break; 1747178576Sjb case CTF_K_VOLATILE: 1748178576Sjb if (cbdatap->f_type) 1749178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "volatile "); 1750178576Sjb dt_print_type_data(&cbdata, ctf_type_reference(cbdatap->dtt.dtt_ctfp,type)); 1751178576Sjb break; 1752178576Sjb case CTF_K_CONST: 1753178576Sjb if (cbdatap->f_type) 1754178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "const "); 1755178576Sjb dt_print_type_data(&cbdata, ctf_type_reference(cbdatap->dtt.dtt_ctfp,type)); 1756178576Sjb break; 1757178576Sjb case CTF_K_RESTRICT: 1758178576Sjb if (cbdatap->f_type) 1759178576Sjb dt_printf(cbdatap->dtp, cbdatap->fp, "restrict "); 1760178576Sjb dt_print_type_data(&cbdata, ctf_type_reference(cbdatap->dtt.dtt_ctfp,type)); 1761178576Sjb break; 1762178576Sjb default: 1763178576Sjb break; 1764178576Sjb } 1765178576Sjb 1766178576Sjb addr += ssz; 1767178576Sjb cnt++; 1768178576Sjb } 1769178576Sjb 1770178576Sjb return (0); 1771178576Sjb} 1772178576Sjb 1773178576Sjbstatic int 1774178576Sjbdt_print_type(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr) 1775178576Sjb{ 1776178576Sjb caddr_t addrend; 1777178576Sjb char *p; 1778178576Sjb dtrace_typeinfo_t dtt; 1779178576Sjb dt_type_cbdata_t cbdata; 1780178576Sjb int num = 0; 1781178576Sjb int quiet = (dtp->dt_options[DTRACEOPT_QUIET] != DTRACEOPT_UNSET); 1782178576Sjb ssize_t ssz; 1783178576Sjb 1784178576Sjb if (!quiet) 1785178576Sjb dt_printf(dtp, fp, "\n"); 1786178576Sjb 1787178576Sjb /* Get the total number of bytes of data buffered. */ 1788178576Sjb size_t nbytes = *((uintptr_t *) addr); 1789178576Sjb addr += sizeof(uintptr_t); 1790178576Sjb 1791178576Sjb /* 1792178576Sjb * Get the size of the type so that we can check that it matches 1793178576Sjb * the CTF data we look up and so that we can figure out how many 1794178576Sjb * type elements are buffered. 1795178576Sjb */ 1796178576Sjb size_t typs = *((uintptr_t *) addr); 1797178576Sjb addr += sizeof(uintptr_t); 1798178576Sjb 1799178576Sjb /* 1800178576Sjb * Point to the type string in the buffer. Get it's string 1801178576Sjb * length and round it up to become the offset to the start 1802178576Sjb * of the buffered type data which we would like to be aligned 1803178576Sjb * for easy access. 1804178576Sjb */ 1805178576Sjb char *strp = (char *) addr; 1806178576Sjb int offset = roundup(strlen(strp) + 1, sizeof(uintptr_t)); 1807178576Sjb 1808178576Sjb /* 1809178576Sjb * The type string might have a format such as 'int [20]'. 1810178576Sjb * Check if there is an array dimension present. 1811178576Sjb */ 1812178576Sjb if ((p = strchr(strp, '[')) != NULL) { 1813178576Sjb /* Strip off the array dimension. */ 1814178576Sjb *p++ = '\0'; 1815178576Sjb 1816178576Sjb for (; *p != '\0' && *p != ']'; p++) 1817178576Sjb num = num * 10 + *p - '0'; 1818178576Sjb } else 1819178576Sjb /* No array dimension, so default. */ 1820178576Sjb num = 1; 1821178576Sjb 1822178576Sjb /* Lookup the CTF type from the type string. */ 1823178576Sjb if (dtrace_lookup_by_type(dtp, DTRACE_OBJ_EVERY, strp, &dtt) < 0) 1824178576Sjb return (-1); 1825178576Sjb 1826178576Sjb /* Offset the buffer address to the start of the data... */ 1827178576Sjb addr += offset; 1828178576Sjb 1829178576Sjb ssz = ctf_type_size(dtt.dtt_ctfp, dtt.dtt_type); 1830178576Sjb 1831178576Sjb if (typs != ssz) { 1832178576Sjb printf("Expected type size from buffer (%lu) to match type size looked up now (%ld)\n", (u_long) typs, (long) ssz); 1833178576Sjb return (-1); 1834178576Sjb } 1835178576Sjb 1836178576Sjb cbdata.dtp = dtp; 1837178576Sjb cbdata.dtt = dtt; 1838178576Sjb cbdata.name = ""; 1839178576Sjb cbdata.addr = addr; 1840178576Sjb cbdata.addrend = addr + nbytes; 1841178576Sjb cbdata.indent = 1; 1842178576Sjb cbdata.f_type = 1; 1843178576Sjb cbdata.type_width = 0; 1844178576Sjb cbdata.name_width = 0; 1845178576Sjb cbdata.fp = fp; 1846178576Sjb 1847178576Sjb return (dt_print_type_data(&cbdata, dtt.dtt_type)); 1848178576Sjb} 1849178576Sjb 1850178576Sjbstatic int 1851178479Sjbdt_print_sym(dtrace_hdl_t *dtp, FILE *fp, const char *format, caddr_t addr) 1852178479Sjb{ 1853178479Sjb /* LINTED - alignment */ 1854178479Sjb uint64_t pc = *((uint64_t *)addr); 1855178479Sjb dtrace_syminfo_t dts; 1856178479Sjb GElf_Sym sym; 1857178479Sjb char c[PATH_MAX * 2]; 1858178479Sjb 1859178479Sjb if (format == NULL) 1860178479Sjb format = " %-50s"; 1861178479Sjb 1862178479Sjb if (dtrace_lookup_by_addr(dtp, pc, &sym, &dts) == 0) { 1863178479Sjb (void) snprintf(c, sizeof (c), "%s`%s", 1864178479Sjb dts.dts_object, dts.dts_name); 1865178479Sjb } else { 1866178479Sjb /* 1867178479Sjb * We'll repeat the lookup, but this time we'll specify a 1868178479Sjb * NULL GElf_Sym -- indicating that we're only interested in 1869178479Sjb * the containing module. 1870178479Sjb */ 1871178479Sjb if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0) { 1872178479Sjb (void) snprintf(c, sizeof (c), "%s`0x%llx", 1873178479Sjb dts.dts_object, (u_longlong_t)pc); 1874178479Sjb } else { 1875178479Sjb (void) snprintf(c, sizeof (c), "0x%llx", 1876178479Sjb (u_longlong_t)pc); 1877178479Sjb } 1878178479Sjb } 1879178479Sjb 1880178479Sjb if (dt_printf(dtp, fp, format, c) < 0) 1881178479Sjb return (-1); 1882178479Sjb 1883178479Sjb return (0); 1884178479Sjb} 1885178479Sjb 1886178479Sjbint 1887178479Sjbdt_print_mod(dtrace_hdl_t *dtp, FILE *fp, const char *format, caddr_t addr) 1888178479Sjb{ 1889178479Sjb /* LINTED - alignment */ 1890178479Sjb uint64_t pc = *((uint64_t *)addr); 1891178479Sjb dtrace_syminfo_t dts; 1892178479Sjb char c[PATH_MAX * 2]; 1893178479Sjb 1894178479Sjb if (format == NULL) 1895178479Sjb format = " %-50s"; 1896178479Sjb 1897178479Sjb if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0) { 1898178479Sjb (void) snprintf(c, sizeof (c), "%s", dts.dts_object); 1899178479Sjb } else { 1900178479Sjb (void) snprintf(c, sizeof (c), "0x%llx", (u_longlong_t)pc); 1901178479Sjb } 1902178479Sjb 1903178479Sjb if (dt_printf(dtp, fp, format, c) < 0) 1904178479Sjb return (-1); 1905178479Sjb 1906178479Sjb return (0); 1907178479Sjb} 1908178479Sjb 1909178479Sjbtypedef struct dt_normal { 1910178479Sjb dtrace_aggvarid_t dtnd_id; 1911178479Sjb uint64_t dtnd_normal; 1912178479Sjb} dt_normal_t; 1913178479Sjb 1914178479Sjbstatic int 1915178479Sjbdt_normalize_agg(const dtrace_aggdata_t *aggdata, void *arg) 1916178479Sjb{ 1917178479Sjb dt_normal_t *normal = arg; 1918178479Sjb dtrace_aggdesc_t *agg = aggdata->dtada_desc; 1919178479Sjb dtrace_aggvarid_t id = normal->dtnd_id; 1920178479Sjb 1921178479Sjb if (agg->dtagd_nrecs == 0) 1922178479Sjb return (DTRACE_AGGWALK_NEXT); 1923178479Sjb 1924178479Sjb if (agg->dtagd_varid != id) 1925178479Sjb return (DTRACE_AGGWALK_NEXT); 1926178479Sjb 1927178479Sjb ((dtrace_aggdata_t *)aggdata)->dtada_normal = normal->dtnd_normal; 1928178479Sjb return (DTRACE_AGGWALK_NORMALIZE); 1929178479Sjb} 1930178479Sjb 1931178479Sjbstatic int 1932178479Sjbdt_normalize(dtrace_hdl_t *dtp, caddr_t base, dtrace_recdesc_t *rec) 1933178479Sjb{ 1934178479Sjb dt_normal_t normal; 1935178479Sjb caddr_t addr; 1936178479Sjb 1937178479Sjb /* 1938178479Sjb * We (should) have two records: the aggregation ID followed by the 1939178479Sjb * normalization value. 1940178479Sjb */ 1941178479Sjb addr = base + rec->dtrd_offset; 1942178479Sjb 1943178479Sjb if (rec->dtrd_size != sizeof (dtrace_aggvarid_t)) 1944178479Sjb return (dt_set_errno(dtp, EDT_BADNORMAL)); 1945178479Sjb 1946178479Sjb /* LINTED - alignment */ 1947178479Sjb normal.dtnd_id = *((dtrace_aggvarid_t *)addr); 1948178479Sjb rec++; 1949178479Sjb 1950178479Sjb if (rec->dtrd_action != DTRACEACT_LIBACT) 1951178479Sjb return (dt_set_errno(dtp, EDT_BADNORMAL)); 1952178479Sjb 1953178479Sjb if (rec->dtrd_arg != DT_ACT_NORMALIZE) 1954178479Sjb return (dt_set_errno(dtp, EDT_BADNORMAL)); 1955178479Sjb 1956178479Sjb addr = base + rec->dtrd_offset; 1957178479Sjb 1958178479Sjb switch (rec->dtrd_size) { 1959178479Sjb case sizeof (uint64_t): 1960178479Sjb /* LINTED - alignment */ 1961178479Sjb normal.dtnd_normal = *((uint64_t *)addr); 1962178479Sjb break; 1963178479Sjb case sizeof (uint32_t): 1964178479Sjb /* LINTED - alignment */ 1965178479Sjb normal.dtnd_normal = *((uint32_t *)addr); 1966178479Sjb break; 1967178479Sjb case sizeof (uint16_t): 1968178479Sjb /* LINTED - alignment */ 1969178479Sjb normal.dtnd_normal = *((uint16_t *)addr); 1970178479Sjb break; 1971178479Sjb case sizeof (uint8_t): 1972178479Sjb normal.dtnd_normal = *((uint8_t *)addr); 1973178479Sjb break; 1974178479Sjb default: 1975178479Sjb return (dt_set_errno(dtp, EDT_BADNORMAL)); 1976178479Sjb } 1977178479Sjb 1978178479Sjb (void) dtrace_aggregate_walk(dtp, dt_normalize_agg, &normal); 1979178479Sjb 1980178479Sjb return (0); 1981178479Sjb} 1982178479Sjb 1983178479Sjbstatic int 1984178479Sjbdt_denormalize_agg(const dtrace_aggdata_t *aggdata, void *arg) 1985178479Sjb{ 1986178479Sjb dtrace_aggdesc_t *agg = aggdata->dtada_desc; 1987178479Sjb dtrace_aggvarid_t id = *((dtrace_aggvarid_t *)arg); 1988178479Sjb 1989178479Sjb if (agg->dtagd_nrecs == 0) 1990178479Sjb return (DTRACE_AGGWALK_NEXT); 1991178479Sjb 1992178479Sjb if (agg->dtagd_varid != id) 1993178479Sjb return (DTRACE_AGGWALK_NEXT); 1994178479Sjb 1995178479Sjb return (DTRACE_AGGWALK_DENORMALIZE); 1996178479Sjb} 1997178479Sjb 1998178479Sjbstatic int 1999178479Sjbdt_clear_agg(const dtrace_aggdata_t *aggdata, void *arg) 2000178479Sjb{ 2001178479Sjb dtrace_aggdesc_t *agg = aggdata->dtada_desc; 2002178479Sjb dtrace_aggvarid_t id = *((dtrace_aggvarid_t *)arg); 2003178479Sjb 2004178479Sjb if (agg->dtagd_nrecs == 0) 2005178479Sjb return (DTRACE_AGGWALK_NEXT); 2006178479Sjb 2007178479Sjb if (agg->dtagd_varid != id) 2008178479Sjb return (DTRACE_AGGWALK_NEXT); 2009178479Sjb 2010178479Sjb return (DTRACE_AGGWALK_CLEAR); 2011178479Sjb} 2012178479Sjb 2013178479Sjbtypedef struct dt_trunc { 2014178479Sjb dtrace_aggvarid_t dttd_id; 2015178479Sjb uint64_t dttd_remaining; 2016178479Sjb} dt_trunc_t; 2017178479Sjb 2018178479Sjbstatic int 2019178479Sjbdt_trunc_agg(const dtrace_aggdata_t *aggdata, void *arg) 2020178479Sjb{ 2021178479Sjb dt_trunc_t *trunc = arg; 2022178479Sjb dtrace_aggdesc_t *agg = aggdata->dtada_desc; 2023178479Sjb dtrace_aggvarid_t id = trunc->dttd_id; 2024178479Sjb 2025178479Sjb if (agg->dtagd_nrecs == 0) 2026178479Sjb return (DTRACE_AGGWALK_NEXT); 2027178479Sjb 2028178479Sjb if (agg->dtagd_varid != id) 2029178479Sjb return (DTRACE_AGGWALK_NEXT); 2030178479Sjb 2031178479Sjb if (trunc->dttd_remaining == 0) 2032178479Sjb return (DTRACE_AGGWALK_REMOVE); 2033178479Sjb 2034178479Sjb trunc->dttd_remaining--; 2035178479Sjb return (DTRACE_AGGWALK_NEXT); 2036178479Sjb} 2037178479Sjb 2038178479Sjbstatic int 2039178479Sjbdt_trunc(dtrace_hdl_t *dtp, caddr_t base, dtrace_recdesc_t *rec) 2040178479Sjb{ 2041178479Sjb dt_trunc_t trunc; 2042178479Sjb caddr_t addr; 2043178479Sjb int64_t remaining; 2044178479Sjb int (*func)(dtrace_hdl_t *, dtrace_aggregate_f *, void *); 2045178479Sjb 2046178479Sjb /* 2047178479Sjb * We (should) have two records: the aggregation ID followed by the 2048178479Sjb * number of aggregation entries after which the aggregation is to be 2049178479Sjb * truncated. 2050178479Sjb */ 2051178479Sjb addr = base + rec->dtrd_offset; 2052178479Sjb 2053178479Sjb if (rec->dtrd_size != sizeof (dtrace_aggvarid_t)) 2054178479Sjb return (dt_set_errno(dtp, EDT_BADTRUNC)); 2055178479Sjb 2056178479Sjb /* LINTED - alignment */ 2057178479Sjb trunc.dttd_id = *((dtrace_aggvarid_t *)addr); 2058178479Sjb rec++; 2059178479Sjb 2060178479Sjb if (rec->dtrd_action != DTRACEACT_LIBACT) 2061178479Sjb return (dt_set_errno(dtp, EDT_BADTRUNC)); 2062178479Sjb 2063178479Sjb if (rec->dtrd_arg != DT_ACT_TRUNC) 2064178479Sjb return (dt_set_errno(dtp, EDT_BADTRUNC)); 2065178479Sjb 2066178479Sjb addr = base + rec->dtrd_offset; 2067178479Sjb 2068178479Sjb switch (rec->dtrd_size) { 2069178479Sjb case sizeof (uint64_t): 2070178479Sjb /* LINTED - alignment */ 2071178479Sjb remaining = *((int64_t *)addr); 2072178479Sjb break; 2073178479Sjb case sizeof (uint32_t): 2074178479Sjb /* LINTED - alignment */ 2075178479Sjb remaining = *((int32_t *)addr); 2076178479Sjb break; 2077178479Sjb case sizeof (uint16_t): 2078178479Sjb /* LINTED - alignment */ 2079178479Sjb remaining = *((int16_t *)addr); 2080178479Sjb break; 2081178479Sjb case sizeof (uint8_t): 2082178479Sjb remaining = *((int8_t *)addr); 2083178479Sjb break; 2084178479Sjb default: 2085178479Sjb return (dt_set_errno(dtp, EDT_BADNORMAL)); 2086178479Sjb } 2087178479Sjb 2088178479Sjb if (remaining < 0) { 2089178479Sjb func = dtrace_aggregate_walk_valsorted; 2090178479Sjb remaining = -remaining; 2091178479Sjb } else { 2092178479Sjb func = dtrace_aggregate_walk_valrevsorted; 2093178479Sjb } 2094178479Sjb 2095178479Sjb assert(remaining >= 0); 2096178479Sjb trunc.dttd_remaining = remaining; 2097178479Sjb 2098178479Sjb (void) func(dtp, dt_trunc_agg, &trunc); 2099178479Sjb 2100178479Sjb return (0); 2101178479Sjb} 2102178479Sjb 2103178479Sjbstatic int 2104178479Sjbdt_print_datum(dtrace_hdl_t *dtp, FILE *fp, dtrace_recdesc_t *rec, 2105267942Srpaulo caddr_t addr, size_t size, const dtrace_aggdata_t *aggdata, 2106267942Srpaulo uint64_t normal, dt_print_aggdata_t *pd) 2107178479Sjb{ 2108267942Srpaulo int err, width; 2109178479Sjb dtrace_actkind_t act = rec->dtrd_action; 2110267942Srpaulo boolean_t packed = pd->dtpa_agghist || pd->dtpa_aggpack; 2111267942Srpaulo dtrace_aggdesc_t *agg = aggdata->dtada_desc; 2112178479Sjb 2113267942Srpaulo static struct { 2114267942Srpaulo size_t size; 2115267942Srpaulo int width; 2116267942Srpaulo int packedwidth; 2117267942Srpaulo } *fmt, fmttab[] = { 2118267942Srpaulo { sizeof (uint8_t), 3, 3 }, 2119267942Srpaulo { sizeof (uint16_t), 5, 5 }, 2120267942Srpaulo { sizeof (uint32_t), 8, 8 }, 2121267942Srpaulo { sizeof (uint64_t), 16, 16 }, 2122267942Srpaulo { 0, -50, 16 } 2123267942Srpaulo }; 2124267942Srpaulo 2125267942Srpaulo if (packed && pd->dtpa_agghisthdr != agg->dtagd_varid) { 2126267942Srpaulo dtrace_recdesc_t *r; 2127267942Srpaulo 2128267942Srpaulo width = 0; 2129267942Srpaulo 2130267942Srpaulo /* 2131267942Srpaulo * To print our quantization header for either an agghist or 2132267942Srpaulo * aggpack aggregation, we need to iterate through all of our 2133267942Srpaulo * of our records to determine their width. 2134267942Srpaulo */ 2135267942Srpaulo for (r = rec; !DTRACEACT_ISAGG(r->dtrd_action); r++) { 2136267942Srpaulo for (fmt = fmttab; fmt->size && 2137267942Srpaulo fmt->size != r->dtrd_size; fmt++) 2138267942Srpaulo continue; 2139267942Srpaulo 2140267942Srpaulo width += fmt->packedwidth + 1; 2141267942Srpaulo } 2142267942Srpaulo 2143267942Srpaulo if (pd->dtpa_agghist) { 2144267942Srpaulo if (dt_print_quanthdr(dtp, fp, width) < 0) 2145267942Srpaulo return (-1); 2146267942Srpaulo } else { 2147267942Srpaulo if (dt_print_quanthdr_packed(dtp, fp, 2148267942Srpaulo width, aggdata, r->dtrd_action) < 0) 2149267942Srpaulo return (-1); 2150267942Srpaulo } 2151267942Srpaulo 2152267942Srpaulo pd->dtpa_agghisthdr = agg->dtagd_varid; 2153267942Srpaulo } 2154267942Srpaulo 2155267942Srpaulo if (pd->dtpa_agghist && DTRACEACT_ISAGG(act)) { 2156267942Srpaulo char positives = aggdata->dtada_flags & DTRACE_A_HASPOSITIVES; 2157267942Srpaulo char negatives = aggdata->dtada_flags & DTRACE_A_HASNEGATIVES; 2158267942Srpaulo int64_t val; 2159267942Srpaulo 2160267942Srpaulo assert(act == DTRACEAGG_SUM || act == DTRACEAGG_COUNT); 2161267942Srpaulo val = (long long)*((uint64_t *)addr); 2162267942Srpaulo 2163267942Srpaulo if (dt_printf(dtp, fp, " ") < 0) 2164267942Srpaulo return (-1); 2165267942Srpaulo 2166267942Srpaulo return (dt_print_quantline(dtp, fp, val, normal, 2167267942Srpaulo aggdata->dtada_total, positives, negatives)); 2168267942Srpaulo } 2169267942Srpaulo 2170267942Srpaulo if (pd->dtpa_aggpack && DTRACEACT_ISAGG(act)) { 2171267942Srpaulo switch (act) { 2172267942Srpaulo case DTRACEAGG_QUANTIZE: 2173267942Srpaulo return (dt_print_quantize_packed(dtp, 2174267942Srpaulo fp, addr, size, aggdata)); 2175267942Srpaulo case DTRACEAGG_LQUANTIZE: 2176267942Srpaulo return (dt_print_lquantize_packed(dtp, 2177267942Srpaulo fp, addr, size, aggdata)); 2178267942Srpaulo default: 2179267942Srpaulo break; 2180267942Srpaulo } 2181267942Srpaulo } 2182267942Srpaulo 2183178479Sjb switch (act) { 2184178479Sjb case DTRACEACT_STACK: 2185178479Sjb return (dt_print_stack(dtp, fp, NULL, addr, 2186178479Sjb rec->dtrd_arg, rec->dtrd_size / rec->dtrd_arg)); 2187178479Sjb 2188178479Sjb case DTRACEACT_USTACK: 2189178479Sjb case DTRACEACT_JSTACK: 2190178479Sjb return (dt_print_ustack(dtp, fp, NULL, addr, rec->dtrd_arg)); 2191178479Sjb 2192178479Sjb case DTRACEACT_USYM: 2193178479Sjb case DTRACEACT_UADDR: 2194178479Sjb return (dt_print_usym(dtp, fp, addr, act)); 2195178479Sjb 2196178479Sjb case DTRACEACT_UMOD: 2197178479Sjb return (dt_print_umod(dtp, fp, NULL, addr)); 2198178479Sjb 2199178479Sjb case DTRACEACT_SYM: 2200178479Sjb return (dt_print_sym(dtp, fp, NULL, addr)); 2201178479Sjb 2202178479Sjb case DTRACEACT_MOD: 2203178479Sjb return (dt_print_mod(dtp, fp, NULL, addr)); 2204178479Sjb 2205178479Sjb case DTRACEAGG_QUANTIZE: 2206178479Sjb return (dt_print_quantize(dtp, fp, addr, size, normal)); 2207178479Sjb 2208178479Sjb case DTRACEAGG_LQUANTIZE: 2209178479Sjb return (dt_print_lquantize(dtp, fp, addr, size, normal)); 2210178479Sjb 2211237624Spfg case DTRACEAGG_LLQUANTIZE: 2212237624Spfg return (dt_print_llquantize(dtp, fp, addr, size, normal)); 2213237624Spfg 2214178479Sjb case DTRACEAGG_AVG: 2215178479Sjb return (dt_print_average(dtp, fp, addr, size, normal)); 2216178479Sjb 2217178479Sjb case DTRACEAGG_STDDEV: 2218178479Sjb return (dt_print_stddev(dtp, fp, addr, size, normal)); 2219178479Sjb 2220178479Sjb default: 2221178479Sjb break; 2222178479Sjb } 2223178479Sjb 2224267942Srpaulo for (fmt = fmttab; fmt->size && fmt->size != size; fmt++) 2225267942Srpaulo continue; 2226267942Srpaulo 2227267942Srpaulo width = packed ? fmt->packedwidth : fmt->width; 2228267942Srpaulo 2229178479Sjb switch (size) { 2230178479Sjb case sizeof (uint64_t): 2231267942Srpaulo err = dt_printf(dtp, fp, " %*lld", width, 2232178479Sjb /* LINTED - alignment */ 2233178479Sjb (long long)*((uint64_t *)addr) / normal); 2234178479Sjb break; 2235178479Sjb case sizeof (uint32_t): 2236178479Sjb /* LINTED - alignment */ 2237267942Srpaulo err = dt_printf(dtp, fp, " %*d", width, *((uint32_t *)addr) / 2238178479Sjb (uint32_t)normal); 2239178479Sjb break; 2240178479Sjb case sizeof (uint16_t): 2241178479Sjb /* LINTED - alignment */ 2242267942Srpaulo err = dt_printf(dtp, fp, " %*d", width, *((uint16_t *)addr) / 2243178479Sjb (uint32_t)normal); 2244178479Sjb break; 2245178479Sjb case sizeof (uint8_t): 2246267942Srpaulo err = dt_printf(dtp, fp, " %*d", width, *((uint8_t *)addr) / 2247178479Sjb (uint32_t)normal); 2248178479Sjb break; 2249178479Sjb default: 2250267942Srpaulo err = dt_print_bytes(dtp, fp, addr, size, width, 0, 0); 2251178479Sjb break; 2252178479Sjb } 2253178479Sjb 2254178479Sjb return (err); 2255178479Sjb} 2256178479Sjb 2257178479Sjbint 2258178479Sjbdt_print_aggs(const dtrace_aggdata_t **aggsdata, int naggvars, void *arg) 2259178479Sjb{ 2260178479Sjb int i, aggact = 0; 2261178479Sjb dt_print_aggdata_t *pd = arg; 2262178479Sjb const dtrace_aggdata_t *aggdata = aggsdata[0]; 2263178479Sjb dtrace_aggdesc_t *agg = aggdata->dtada_desc; 2264178479Sjb FILE *fp = pd->dtpa_fp; 2265178479Sjb dtrace_hdl_t *dtp = pd->dtpa_dtp; 2266178479Sjb dtrace_recdesc_t *rec; 2267178479Sjb dtrace_actkind_t act; 2268178479Sjb caddr_t addr; 2269178479Sjb size_t size; 2270178479Sjb 2271267942Srpaulo pd->dtpa_agghist = (aggdata->dtada_flags & DTRACE_A_TOTAL); 2272267942Srpaulo pd->dtpa_aggpack = (aggdata->dtada_flags & DTRACE_A_MINMAXBIN); 2273267942Srpaulo 2274178479Sjb /* 2275178479Sjb * Iterate over each record description in the key, printing the traced 2276178479Sjb * data, skipping the first datum (the tuple member created by the 2277178479Sjb * compiler). 2278178479Sjb */ 2279178479Sjb for (i = 1; i < agg->dtagd_nrecs; i++) { 2280178479Sjb rec = &agg->dtagd_rec[i]; 2281178479Sjb act = rec->dtrd_action; 2282178479Sjb addr = aggdata->dtada_data + rec->dtrd_offset; 2283178479Sjb size = rec->dtrd_size; 2284178479Sjb 2285178479Sjb if (DTRACEACT_ISAGG(act)) { 2286178479Sjb aggact = i; 2287178479Sjb break; 2288178479Sjb } 2289178479Sjb 2290267942Srpaulo if (dt_print_datum(dtp, fp, rec, addr, 2291267942Srpaulo size, aggdata, 1, pd) < 0) 2292178479Sjb return (-1); 2293178479Sjb 2294178479Sjb if (dt_buffered_flush(dtp, NULL, rec, aggdata, 2295178479Sjb DTRACE_BUFDATA_AGGKEY) < 0) 2296178479Sjb return (-1); 2297178479Sjb } 2298178479Sjb 2299178479Sjb assert(aggact != 0); 2300178479Sjb 2301178479Sjb for (i = (naggvars == 1 ? 0 : 1); i < naggvars; i++) { 2302178479Sjb uint64_t normal; 2303178479Sjb 2304178479Sjb aggdata = aggsdata[i]; 2305178479Sjb agg = aggdata->dtada_desc; 2306178479Sjb rec = &agg->dtagd_rec[aggact]; 2307178479Sjb act = rec->dtrd_action; 2308178479Sjb addr = aggdata->dtada_data + rec->dtrd_offset; 2309178479Sjb size = rec->dtrd_size; 2310178479Sjb 2311178479Sjb assert(DTRACEACT_ISAGG(act)); 2312178479Sjb normal = aggdata->dtada_normal; 2313178479Sjb 2314267942Srpaulo if (dt_print_datum(dtp, fp, rec, addr, 2315267942Srpaulo size, aggdata, normal, pd) < 0) 2316178479Sjb return (-1); 2317178479Sjb 2318178479Sjb if (dt_buffered_flush(dtp, NULL, rec, aggdata, 2319178479Sjb DTRACE_BUFDATA_AGGVAL) < 0) 2320178479Sjb return (-1); 2321178479Sjb 2322178479Sjb if (!pd->dtpa_allunprint) 2323178479Sjb agg->dtagd_flags |= DTRACE_AGD_PRINTED; 2324178479Sjb } 2325178479Sjb 2326267942Srpaulo if (!pd->dtpa_agghist && !pd->dtpa_aggpack) { 2327267942Srpaulo if (dt_printf(dtp, fp, "\n") < 0) 2328267942Srpaulo return (-1); 2329267942Srpaulo } 2330178479Sjb 2331178479Sjb if (dt_buffered_flush(dtp, NULL, NULL, aggdata, 2332178479Sjb DTRACE_BUFDATA_AGGFORMAT | DTRACE_BUFDATA_AGGLAST) < 0) 2333178479Sjb return (-1); 2334178479Sjb 2335178479Sjb return (0); 2336178479Sjb} 2337178479Sjb 2338178479Sjbint 2339178479Sjbdt_print_agg(const dtrace_aggdata_t *aggdata, void *arg) 2340178479Sjb{ 2341178479Sjb dt_print_aggdata_t *pd = arg; 2342178479Sjb dtrace_aggdesc_t *agg = aggdata->dtada_desc; 2343178479Sjb dtrace_aggvarid_t aggvarid = pd->dtpa_id; 2344178479Sjb 2345178479Sjb if (pd->dtpa_allunprint) { 2346178479Sjb if (agg->dtagd_flags & DTRACE_AGD_PRINTED) 2347178479Sjb return (0); 2348178479Sjb } else { 2349178479Sjb /* 2350178479Sjb * If we're not printing all unprinted aggregations, then the 2351178479Sjb * aggregation variable ID denotes a specific aggregation 2352178479Sjb * variable that we should print -- skip any other aggregations 2353178479Sjb * that we encounter. 2354178479Sjb */ 2355178479Sjb if (agg->dtagd_nrecs == 0) 2356178479Sjb return (0); 2357178479Sjb 2358178479Sjb if (aggvarid != agg->dtagd_varid) 2359178479Sjb return (0); 2360178479Sjb } 2361178479Sjb 2362178479Sjb return (dt_print_aggs(&aggdata, 1, arg)); 2363178479Sjb} 2364178479Sjb 2365178479Sjbint 2366178479Sjbdt_setopt(dtrace_hdl_t *dtp, const dtrace_probedata_t *data, 2367178479Sjb const char *option, const char *value) 2368178479Sjb{ 2369178479Sjb int len, rval; 2370178479Sjb char *msg; 2371178479Sjb const char *errstr; 2372178479Sjb dtrace_setoptdata_t optdata; 2373178479Sjb 2374178479Sjb bzero(&optdata, sizeof (optdata)); 2375178479Sjb (void) dtrace_getopt(dtp, option, &optdata.dtsda_oldval); 2376178479Sjb 2377178479Sjb if (dtrace_setopt(dtp, option, value) == 0) { 2378178479Sjb (void) dtrace_getopt(dtp, option, &optdata.dtsda_newval); 2379178479Sjb optdata.dtsda_probe = data; 2380178479Sjb optdata.dtsda_option = option; 2381178479Sjb optdata.dtsda_handle = dtp; 2382178479Sjb 2383178479Sjb if ((rval = dt_handle_setopt(dtp, &optdata)) != 0) 2384178479Sjb return (rval); 2385178479Sjb 2386178479Sjb return (0); 2387178479Sjb } 2388178479Sjb 2389178479Sjb errstr = dtrace_errmsg(dtp, dtrace_errno(dtp)); 2390178479Sjb len = strlen(option) + strlen(value) + strlen(errstr) + 80; 2391178479Sjb msg = alloca(len); 2392178479Sjb 2393178479Sjb (void) snprintf(msg, len, "couldn't set option \"%s\" to \"%s\": %s\n", 2394178479Sjb option, value, errstr); 2395178479Sjb 2396178479Sjb if ((rval = dt_handle_liberr(dtp, data, msg)) == 0) 2397178479Sjb return (0); 2398178479Sjb 2399178479Sjb return (rval); 2400178479Sjb} 2401178479Sjb 2402178479Sjbstatic int 2403250574Smarkjdt_consume_cpu(dtrace_hdl_t *dtp, FILE *fp, int cpu, 2404250574Smarkj dtrace_bufdesc_t *buf, boolean_t just_one, 2405178479Sjb dtrace_consume_probe_f *efunc, dtrace_consume_rec_f *rfunc, void *arg) 2406178479Sjb{ 2407178479Sjb dtrace_epid_t id; 2408250574Smarkj size_t offs; 2409178479Sjb int flow = (dtp->dt_options[DTRACEOPT_FLOWINDENT] != DTRACEOPT_UNSET); 2410178479Sjb int quiet = (dtp->dt_options[DTRACEOPT_QUIET] != DTRACEOPT_UNSET); 2411178479Sjb int rval, i, n; 2412248690Spfg uint64_t tracememsize = 0; 2413178479Sjb dtrace_probedata_t data; 2414178479Sjb uint64_t drops; 2415178479Sjb 2416178479Sjb bzero(&data, sizeof (data)); 2417178479Sjb data.dtpda_handle = dtp; 2418178479Sjb data.dtpda_cpu = cpu; 2419253725Spfg data.dtpda_flow = dtp->dt_flow; 2420253725Spfg data.dtpda_indent = dtp->dt_indent; 2421253725Spfg data.dtpda_prefix = dtp->dt_prefix; 2422178479Sjb 2423250574Smarkj for (offs = buf->dtbd_oldest; offs < buf->dtbd_size; ) { 2424178479Sjb dtrace_eprobedesc_t *epd; 2425178479Sjb 2426178479Sjb /* 2427178479Sjb * We're guaranteed to have an ID. 2428178479Sjb */ 2429178479Sjb id = *(uint32_t *)((uintptr_t)buf->dtbd_data + offs); 2430178479Sjb 2431178479Sjb if (id == DTRACE_EPIDNONE) { 2432178479Sjb /* 2433178479Sjb * This is filler to assure proper alignment of the 2434178479Sjb * next record; we simply ignore it. 2435178479Sjb */ 2436178479Sjb offs += sizeof (id); 2437178479Sjb continue; 2438178479Sjb } 2439178479Sjb 2440178479Sjb if ((rval = dt_epid_lookup(dtp, id, &data.dtpda_edesc, 2441178479Sjb &data.dtpda_pdesc)) != 0) 2442178479Sjb return (rval); 2443178479Sjb 2444178479Sjb epd = data.dtpda_edesc; 2445178479Sjb data.dtpda_data = buf->dtbd_data + offs; 2446178479Sjb 2447178479Sjb if (data.dtpda_edesc->dtepd_uarg != DT_ECB_DEFAULT) { 2448178479Sjb rval = dt_handle(dtp, &data); 2449178479Sjb 2450178479Sjb if (rval == DTRACE_CONSUME_NEXT) 2451178479Sjb goto nextepid; 2452178479Sjb 2453178479Sjb if (rval == DTRACE_CONSUME_ERROR) 2454178479Sjb return (-1); 2455178479Sjb } 2456178479Sjb 2457178479Sjb if (flow) 2458250574Smarkj (void) dt_flowindent(dtp, &data, dtp->dt_last_epid, 2459250574Smarkj buf, offs); 2460178479Sjb 2461178479Sjb rval = (*efunc)(&data, arg); 2462178479Sjb 2463178479Sjb if (flow) { 2464178479Sjb if (data.dtpda_flow == DTRACEFLOW_ENTRY) 2465178479Sjb data.dtpda_indent += 2; 2466178479Sjb } 2467178479Sjb 2468178479Sjb if (rval == DTRACE_CONSUME_NEXT) 2469178479Sjb goto nextepid; 2470178479Sjb 2471178479Sjb if (rval == DTRACE_CONSUME_ABORT) 2472178479Sjb return (dt_set_errno(dtp, EDT_DIRABORT)); 2473178479Sjb 2474178479Sjb if (rval != DTRACE_CONSUME_THIS) 2475178479Sjb return (dt_set_errno(dtp, EDT_BADRVAL)); 2476178479Sjb 2477178479Sjb for (i = 0; i < epd->dtepd_nrecs; i++) { 2478250574Smarkj caddr_t addr; 2479178479Sjb dtrace_recdesc_t *rec = &epd->dtepd_rec[i]; 2480178479Sjb dtrace_actkind_t act = rec->dtrd_action; 2481178479Sjb 2482178479Sjb data.dtpda_data = buf->dtbd_data + offs + 2483178479Sjb rec->dtrd_offset; 2484178479Sjb addr = data.dtpda_data; 2485178479Sjb 2486178479Sjb if (act == DTRACEACT_LIBACT) { 2487178479Sjb uint64_t arg = rec->dtrd_arg; 2488178479Sjb dtrace_aggvarid_t id; 2489178479Sjb 2490178479Sjb switch (arg) { 2491178479Sjb case DT_ACT_CLEAR: 2492178479Sjb /* LINTED - alignment */ 2493178479Sjb id = *((dtrace_aggvarid_t *)addr); 2494178479Sjb (void) dtrace_aggregate_walk(dtp, 2495178479Sjb dt_clear_agg, &id); 2496178479Sjb continue; 2497178479Sjb 2498178479Sjb case DT_ACT_DENORMALIZE: 2499178479Sjb /* LINTED - alignment */ 2500178479Sjb id = *((dtrace_aggvarid_t *)addr); 2501178479Sjb (void) dtrace_aggregate_walk(dtp, 2502178479Sjb dt_denormalize_agg, &id); 2503178479Sjb continue; 2504178479Sjb 2505178479Sjb case DT_ACT_FTRUNCATE: 2506178479Sjb if (fp == NULL) 2507178479Sjb continue; 2508178479Sjb 2509178479Sjb (void) fflush(fp); 2510178479Sjb (void) ftruncate(fileno(fp), 0); 2511178479Sjb (void) fseeko(fp, 0, SEEK_SET); 2512178479Sjb continue; 2513178479Sjb 2514178479Sjb case DT_ACT_NORMALIZE: 2515178479Sjb if (i == epd->dtepd_nrecs - 1) 2516178479Sjb return (dt_set_errno(dtp, 2517178479Sjb EDT_BADNORMAL)); 2518178479Sjb 2519178479Sjb if (dt_normalize(dtp, 2520178479Sjb buf->dtbd_data + offs, rec) != 0) 2521178479Sjb return (-1); 2522178479Sjb 2523178479Sjb i++; 2524178479Sjb continue; 2525178479Sjb 2526178479Sjb case DT_ACT_SETOPT: { 2527178479Sjb uint64_t *opts = dtp->dt_options; 2528178479Sjb dtrace_recdesc_t *valrec; 2529178479Sjb uint32_t valsize; 2530178479Sjb caddr_t val; 2531178479Sjb int rv; 2532178479Sjb 2533178479Sjb if (i == epd->dtepd_nrecs - 1) { 2534178479Sjb return (dt_set_errno(dtp, 2535178479Sjb EDT_BADSETOPT)); 2536178479Sjb } 2537178479Sjb 2538178479Sjb valrec = &epd->dtepd_rec[++i]; 2539178479Sjb valsize = valrec->dtrd_size; 2540178479Sjb 2541178479Sjb if (valrec->dtrd_action != act || 2542178479Sjb valrec->dtrd_arg != arg) { 2543178479Sjb return (dt_set_errno(dtp, 2544178479Sjb EDT_BADSETOPT)); 2545178479Sjb } 2546178479Sjb 2547178479Sjb if (valsize > sizeof (uint64_t)) { 2548178479Sjb val = buf->dtbd_data + offs + 2549178479Sjb valrec->dtrd_offset; 2550178479Sjb } else { 2551178479Sjb val = "1"; 2552178479Sjb } 2553178479Sjb 2554178479Sjb rv = dt_setopt(dtp, &data, addr, val); 2555178479Sjb 2556178479Sjb if (rv != 0) 2557178479Sjb return (-1); 2558178479Sjb 2559178479Sjb flow = (opts[DTRACEOPT_FLOWINDENT] != 2560178479Sjb DTRACEOPT_UNSET); 2561178479Sjb quiet = (opts[DTRACEOPT_QUIET] != 2562178479Sjb DTRACEOPT_UNSET); 2563178479Sjb 2564178479Sjb continue; 2565178479Sjb } 2566178479Sjb 2567178479Sjb case DT_ACT_TRUNC: 2568178479Sjb if (i == epd->dtepd_nrecs - 1) 2569178479Sjb return (dt_set_errno(dtp, 2570178479Sjb EDT_BADTRUNC)); 2571178479Sjb 2572178479Sjb if (dt_trunc(dtp, 2573178479Sjb buf->dtbd_data + offs, rec) != 0) 2574178479Sjb return (-1); 2575178479Sjb 2576178479Sjb i++; 2577178479Sjb continue; 2578178479Sjb 2579178479Sjb default: 2580178479Sjb continue; 2581178479Sjb } 2582178479Sjb } 2583178479Sjb 2584248690Spfg if (act == DTRACEACT_TRACEMEM_DYNSIZE && 2585248690Spfg rec->dtrd_size == sizeof (uint64_t)) { 2586248708Spfg /* LINTED - alignment */ 2587248690Spfg tracememsize = *((unsigned long long *)addr); 2588248690Spfg continue; 2589248690Spfg } 2590248690Spfg 2591178479Sjb rval = (*rfunc)(&data, rec, arg); 2592178479Sjb 2593178479Sjb if (rval == DTRACE_CONSUME_NEXT) 2594178479Sjb continue; 2595178479Sjb 2596178479Sjb if (rval == DTRACE_CONSUME_ABORT) 2597178479Sjb return (dt_set_errno(dtp, EDT_DIRABORT)); 2598178479Sjb 2599178479Sjb if (rval != DTRACE_CONSUME_THIS) 2600178479Sjb return (dt_set_errno(dtp, EDT_BADRVAL)); 2601178479Sjb 2602178479Sjb if (act == DTRACEACT_STACK) { 2603178479Sjb int depth = rec->dtrd_arg; 2604178479Sjb 2605178479Sjb if (dt_print_stack(dtp, fp, NULL, addr, depth, 2606178479Sjb rec->dtrd_size / depth) < 0) 2607178479Sjb return (-1); 2608178479Sjb goto nextrec; 2609178479Sjb } 2610178479Sjb 2611178479Sjb if (act == DTRACEACT_USTACK || 2612178479Sjb act == DTRACEACT_JSTACK) { 2613178479Sjb if (dt_print_ustack(dtp, fp, NULL, 2614178479Sjb addr, rec->dtrd_arg) < 0) 2615178479Sjb return (-1); 2616178479Sjb goto nextrec; 2617178479Sjb } 2618178479Sjb 2619178479Sjb if (act == DTRACEACT_SYM) { 2620178479Sjb if (dt_print_sym(dtp, fp, NULL, addr) < 0) 2621178479Sjb return (-1); 2622178479Sjb goto nextrec; 2623178479Sjb } 2624178479Sjb 2625178479Sjb if (act == DTRACEACT_MOD) { 2626178479Sjb if (dt_print_mod(dtp, fp, NULL, addr) < 0) 2627178479Sjb return (-1); 2628178479Sjb goto nextrec; 2629178479Sjb } 2630178479Sjb 2631178479Sjb if (act == DTRACEACT_USYM || act == DTRACEACT_UADDR) { 2632178479Sjb if (dt_print_usym(dtp, fp, addr, act) < 0) 2633178479Sjb return (-1); 2634178479Sjb goto nextrec; 2635178479Sjb } 2636178479Sjb 2637178479Sjb if (act == DTRACEACT_UMOD) { 2638178479Sjb if (dt_print_umod(dtp, fp, NULL, addr) < 0) 2639178479Sjb return (-1); 2640178479Sjb goto nextrec; 2641178479Sjb } 2642178479Sjb 2643178576Sjb if (act == DTRACEACT_PRINTM) { 2644178576Sjb if (dt_print_memory(dtp, fp, addr) < 0) 2645178576Sjb return (-1); 2646178576Sjb goto nextrec; 2647178576Sjb } 2648178576Sjb 2649178576Sjb if (act == DTRACEACT_PRINTT) { 2650178576Sjb if (dt_print_type(dtp, fp, addr) < 0) 2651178576Sjb return (-1); 2652178576Sjb goto nextrec; 2653178576Sjb } 2654178576Sjb 2655178479Sjb if (DTRACEACT_ISPRINTFLIKE(act)) { 2656178479Sjb void *fmtdata; 2657178479Sjb int (*func)(dtrace_hdl_t *, FILE *, void *, 2658178479Sjb const dtrace_probedata_t *, 2659178479Sjb const dtrace_recdesc_t *, uint_t, 2660178479Sjb const void *buf, size_t); 2661178479Sjb 2662178479Sjb if ((fmtdata = dt_format_lookup(dtp, 2663178479Sjb rec->dtrd_format)) == NULL) 2664178479Sjb goto nofmt; 2665178479Sjb 2666178479Sjb switch (act) { 2667178479Sjb case DTRACEACT_PRINTF: 2668178479Sjb func = dtrace_fprintf; 2669178479Sjb break; 2670178479Sjb case DTRACEACT_PRINTA: 2671178479Sjb func = dtrace_fprinta; 2672178479Sjb break; 2673178479Sjb case DTRACEACT_SYSTEM: 2674178479Sjb func = dtrace_system; 2675178479Sjb break; 2676178479Sjb case DTRACEACT_FREOPEN: 2677178479Sjb func = dtrace_freopen; 2678178479Sjb break; 2679178479Sjb } 2680178479Sjb 2681178479Sjb n = (*func)(dtp, fp, fmtdata, &data, 2682178479Sjb rec, epd->dtepd_nrecs - i, 2683178479Sjb (uchar_t *)buf->dtbd_data + offs, 2684178479Sjb buf->dtbd_size - offs); 2685178479Sjb 2686178479Sjb if (n < 0) 2687178479Sjb return (-1); /* errno is set for us */ 2688178479Sjb 2689178479Sjb if (n > 0) 2690178479Sjb i += n - 1; 2691178479Sjb goto nextrec; 2692178479Sjb } 2693178479Sjb 2694248708Spfg /* 2695248708Spfg * If this is a DIF expression, and the record has a 2696248708Spfg * format set, this indicates we have a CTF type name 2697248708Spfg * associated with the data and we should try to print 2698248708Spfg * it out by type. 2699248708Spfg */ 2700248708Spfg if (act == DTRACEACT_DIFEXPR) { 2701248708Spfg const char *strdata = dt_strdata_lookup(dtp, 2702248708Spfg rec->dtrd_format); 2703248708Spfg if (strdata != NULL) { 2704248708Spfg n = dtrace_print(dtp, fp, strdata, 2705248708Spfg addr, rec->dtrd_size); 2706248708Spfg 2707248708Spfg /* 2708248708Spfg * dtrace_print() will return -1 on 2709248708Spfg * error, or return the number of bytes 2710248708Spfg * consumed. It will return 0 if the 2711248708Spfg * type couldn't be determined, and we 2712248708Spfg * should fall through to the normal 2713248708Spfg * trace method. 2714248708Spfg */ 2715248708Spfg if (n < 0) 2716248708Spfg return (-1); 2717248708Spfg 2718248708Spfg if (n > 0) 2719248708Spfg goto nextrec; 2720248708Spfg } 2721248708Spfg } 2722248708Spfg 2723178479Sjbnofmt: 2724178479Sjb if (act == DTRACEACT_PRINTA) { 2725178479Sjb dt_print_aggdata_t pd; 2726178479Sjb dtrace_aggvarid_t *aggvars; 2727178479Sjb int j, naggvars = 0; 2728178479Sjb size_t size = ((epd->dtepd_nrecs - i) * 2729178479Sjb sizeof (dtrace_aggvarid_t)); 2730178479Sjb 2731178479Sjb if ((aggvars = dt_alloc(dtp, size)) == NULL) 2732178479Sjb return (-1); 2733178479Sjb 2734178479Sjb /* 2735178479Sjb * This might be a printa() with multiple 2736178479Sjb * aggregation variables. We need to scan 2737178479Sjb * forward through the records until we find 2738178479Sjb * a record from a different statement. 2739178479Sjb */ 2740178479Sjb for (j = i; j < epd->dtepd_nrecs; j++) { 2741178479Sjb dtrace_recdesc_t *nrec; 2742178479Sjb caddr_t naddr; 2743178479Sjb 2744178479Sjb nrec = &epd->dtepd_rec[j]; 2745178479Sjb 2746178479Sjb if (nrec->dtrd_uarg != rec->dtrd_uarg) 2747178479Sjb break; 2748178479Sjb 2749178479Sjb if (nrec->dtrd_action != act) { 2750178479Sjb return (dt_set_errno(dtp, 2751178479Sjb EDT_BADAGG)); 2752178479Sjb } 2753178479Sjb 2754178479Sjb naddr = buf->dtbd_data + offs + 2755178479Sjb nrec->dtrd_offset; 2756178479Sjb 2757178479Sjb aggvars[naggvars++] = 2758178479Sjb /* LINTED - alignment */ 2759178479Sjb *((dtrace_aggvarid_t *)naddr); 2760178479Sjb } 2761178479Sjb 2762178479Sjb i = j - 1; 2763178479Sjb bzero(&pd, sizeof (pd)); 2764178479Sjb pd.dtpa_dtp = dtp; 2765178479Sjb pd.dtpa_fp = fp; 2766178479Sjb 2767178479Sjb assert(naggvars >= 1); 2768178479Sjb 2769178479Sjb if (naggvars == 1) { 2770178479Sjb pd.dtpa_id = aggvars[0]; 2771178479Sjb dt_free(dtp, aggvars); 2772178479Sjb 2773178479Sjb if (dt_printf(dtp, fp, "\n") < 0 || 2774178479Sjb dtrace_aggregate_walk_sorted(dtp, 2775178479Sjb dt_print_agg, &pd) < 0) 2776178479Sjb return (-1); 2777178479Sjb goto nextrec; 2778178479Sjb } 2779178479Sjb 2780178479Sjb if (dt_printf(dtp, fp, "\n") < 0 || 2781178479Sjb dtrace_aggregate_walk_joined(dtp, aggvars, 2782178479Sjb naggvars, dt_print_aggs, &pd) < 0) { 2783178479Sjb dt_free(dtp, aggvars); 2784178479Sjb return (-1); 2785178479Sjb } 2786178479Sjb 2787178479Sjb dt_free(dtp, aggvars); 2788178479Sjb goto nextrec; 2789178479Sjb } 2790178479Sjb 2791248690Spfg if (act == DTRACEACT_TRACEMEM) { 2792248690Spfg if (tracememsize == 0 || 2793248690Spfg tracememsize > rec->dtrd_size) { 2794248690Spfg tracememsize = rec->dtrd_size; 2795248690Spfg } 2796248690Spfg 2797248690Spfg n = dt_print_bytes(dtp, fp, addr, 2798267942Srpaulo tracememsize, -33, quiet, 1); 2799248690Spfg 2800248690Spfg tracememsize = 0; 2801248690Spfg 2802248690Spfg if (n < 0) 2803248690Spfg return (-1); 2804248690Spfg 2805248690Spfg goto nextrec; 2806248690Spfg } 2807248690Spfg 2808178479Sjb switch (rec->dtrd_size) { 2809178479Sjb case sizeof (uint64_t): 2810178479Sjb n = dt_printf(dtp, fp, 2811178479Sjb quiet ? "%lld" : " %16lld", 2812178479Sjb /* LINTED - alignment */ 2813178479Sjb *((unsigned long long *)addr)); 2814178479Sjb break; 2815178479Sjb case sizeof (uint32_t): 2816178479Sjb n = dt_printf(dtp, fp, quiet ? "%d" : " %8d", 2817178479Sjb /* LINTED - alignment */ 2818178479Sjb *((uint32_t *)addr)); 2819178479Sjb break; 2820178479Sjb case sizeof (uint16_t): 2821178479Sjb n = dt_printf(dtp, fp, quiet ? "%d" : " %5d", 2822178479Sjb /* LINTED - alignment */ 2823178479Sjb *((uint16_t *)addr)); 2824178479Sjb break; 2825178479Sjb case sizeof (uint8_t): 2826178479Sjb n = dt_printf(dtp, fp, quiet ? "%d" : " %3d", 2827178479Sjb *((uint8_t *)addr)); 2828178479Sjb break; 2829178479Sjb default: 2830178479Sjb n = dt_print_bytes(dtp, fp, addr, 2831267942Srpaulo rec->dtrd_size, -33, quiet, 0); 2832178479Sjb break; 2833178479Sjb } 2834178479Sjb 2835178479Sjb if (n < 0) 2836178479Sjb return (-1); /* errno is set for us */ 2837178479Sjb 2838178479Sjbnextrec: 2839178479Sjb if (dt_buffered_flush(dtp, &data, rec, NULL, 0) < 0) 2840178479Sjb return (-1); /* errno is set for us */ 2841178479Sjb } 2842178479Sjb 2843178479Sjb /* 2844178479Sjb * Call the record callback with a NULL record to indicate 2845178479Sjb * that we're done processing this EPID. 2846178479Sjb */ 2847178479Sjb rval = (*rfunc)(&data, NULL, arg); 2848178479Sjbnextepid: 2849178479Sjb offs += epd->dtepd_size; 2850250574Smarkj dtp->dt_last_epid = id; 2851250574Smarkj if (just_one) { 2852250574Smarkj buf->dtbd_oldest = offs; 2853250574Smarkj break; 2854250574Smarkj } 2855178479Sjb } 2856178479Sjb 2857250574Smarkj dtp->dt_flow = data.dtpda_flow; 2858250574Smarkj dtp->dt_indent = data.dtpda_indent; 2859250574Smarkj dtp->dt_prefix = data.dtpda_prefix; 2860178479Sjb 2861178479Sjb if ((drops = buf->dtbd_drops) == 0) 2862178479Sjb return (0); 2863178479Sjb 2864178479Sjb /* 2865178479Sjb * Explicitly zero the drops to prevent us from processing them again. 2866178479Sjb */ 2867178479Sjb buf->dtbd_drops = 0; 2868178479Sjb 2869178479Sjb return (dt_handle_cpudrop(dtp, cpu, DTRACEDROP_PRINCIPAL, drops)); 2870178479Sjb} 2871178479Sjb 2872250574Smarkj/* 2873250574Smarkj * Reduce memory usage by shrinking the buffer if it's no more than half full. 2874250574Smarkj * Note, we need to preserve the alignment of the data at dtbd_oldest, which is 2875250574Smarkj * only 4-byte aligned. 2876250574Smarkj */ 2877250574Smarkjstatic void 2878250574Smarkjdt_realloc_buf(dtrace_hdl_t *dtp, dtrace_bufdesc_t *buf, int cursize) 2879250574Smarkj{ 2880250574Smarkj uint64_t used = buf->dtbd_size - buf->dtbd_oldest; 2881250574Smarkj if (used < cursize / 2) { 2882250574Smarkj int misalign = buf->dtbd_oldest & (sizeof (uint64_t) - 1); 2883250574Smarkj char *newdata = dt_alloc(dtp, used + misalign); 2884250574Smarkj if (newdata == NULL) 2885250574Smarkj return; 2886250574Smarkj bzero(newdata, misalign); 2887250574Smarkj bcopy(buf->dtbd_data + buf->dtbd_oldest, 2888250574Smarkj newdata + misalign, used); 2889250574Smarkj dt_free(dtp, buf->dtbd_data); 2890250574Smarkj buf->dtbd_oldest = misalign; 2891250574Smarkj buf->dtbd_size = used + misalign; 2892250574Smarkj buf->dtbd_data = newdata; 2893250574Smarkj } 2894250574Smarkj} 2895250574Smarkj 2896250574Smarkj/* 2897250574Smarkj * If the ring buffer has wrapped, the data is not in order. Rearrange it 2898250574Smarkj * so that it is. Note, we need to preserve the alignment of the data at 2899250574Smarkj * dtbd_oldest, which is only 4-byte aligned. 2900250574Smarkj */ 2901250574Smarkjstatic int 2902250574Smarkjdt_unring_buf(dtrace_hdl_t *dtp, dtrace_bufdesc_t *buf) 2903250574Smarkj{ 2904250574Smarkj int misalign; 2905250574Smarkj char *newdata, *ndp; 2906250574Smarkj 2907250574Smarkj if (buf->dtbd_oldest == 0) 2908250574Smarkj return (0); 2909250574Smarkj 2910250574Smarkj misalign = buf->dtbd_oldest & (sizeof (uint64_t) - 1); 2911250574Smarkj newdata = ndp = dt_alloc(dtp, buf->dtbd_size + misalign); 2912250574Smarkj 2913250574Smarkj if (newdata == NULL) 2914250574Smarkj return (-1); 2915250574Smarkj 2916250574Smarkj assert(0 == (buf->dtbd_size & (sizeof (uint64_t) - 1))); 2917250574Smarkj 2918250574Smarkj bzero(ndp, misalign); 2919250574Smarkj ndp += misalign; 2920250574Smarkj 2921250574Smarkj bcopy(buf->dtbd_data + buf->dtbd_oldest, ndp, 2922250574Smarkj buf->dtbd_size - buf->dtbd_oldest); 2923250574Smarkj ndp += buf->dtbd_size - buf->dtbd_oldest; 2924250574Smarkj 2925250574Smarkj bcopy(buf->dtbd_data, ndp, buf->dtbd_oldest); 2926250574Smarkj 2927250574Smarkj dt_free(dtp, buf->dtbd_data); 2928250574Smarkj buf->dtbd_oldest = 0; 2929250574Smarkj buf->dtbd_data = newdata; 2930250574Smarkj buf->dtbd_size += misalign; 2931250574Smarkj 2932250574Smarkj return (0); 2933250574Smarkj} 2934250574Smarkj 2935250574Smarkjstatic void 2936250574Smarkjdt_put_buf(dtrace_hdl_t *dtp, dtrace_bufdesc_t *buf) 2937250574Smarkj{ 2938250574Smarkj dt_free(dtp, buf->dtbd_data); 2939250574Smarkj dt_free(dtp, buf); 2940250574Smarkj} 2941250574Smarkj 2942250574Smarkj/* 2943250574Smarkj * Returns 0 on success, in which case *cbp will be filled in if we retrieved 2944250574Smarkj * data, or NULL if there is no data for this CPU. 2945250574Smarkj * Returns -1 on failure and sets dt_errno. 2946250574Smarkj */ 2947250574Smarkjstatic int 2948250574Smarkjdt_get_buf(dtrace_hdl_t *dtp, int cpu, dtrace_bufdesc_t **bufp) 2949250574Smarkj{ 2950250574Smarkj dtrace_optval_t size; 2951250574Smarkj dtrace_bufdesc_t *buf = dt_zalloc(dtp, sizeof (*buf)); 2952269524Smarkj int error, rval; 2953250574Smarkj 2954250574Smarkj if (buf == NULL) 2955250574Smarkj return (-1); 2956250574Smarkj 2957250574Smarkj (void) dtrace_getopt(dtp, "bufsize", &size); 2958250574Smarkj buf->dtbd_data = dt_alloc(dtp, size); 2959250574Smarkj if (buf->dtbd_data == NULL) { 2960250574Smarkj dt_free(dtp, buf); 2961250574Smarkj return (-1); 2962250574Smarkj } 2963250574Smarkj buf->dtbd_size = size; 2964250574Smarkj buf->dtbd_cpu = cpu; 2965250574Smarkj 2966277300Ssmh#ifdef illumos 2967250574Smarkj if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, buf) == -1) { 2968250574Smarkj#else 2969250574Smarkj if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, &buf) == -1) { 2970250574Smarkj#endif 2971250574Smarkj /* 2972250574Smarkj * If we failed with ENOENT, it may be because the 2973250574Smarkj * CPU was unconfigured -- this is okay. Any other 2974250574Smarkj * error, however, is unexpected. 2975250574Smarkj */ 2976250574Smarkj if (errno == ENOENT) { 2977250574Smarkj *bufp = NULL; 2978269524Smarkj rval = 0; 2979269524Smarkj } else 2980269524Smarkj rval = dt_set_errno(dtp, errno); 2981250574Smarkj 2982269524Smarkj dt_put_buf(dtp, buf); 2983269524Smarkj return (rval); 2984250574Smarkj } 2985250574Smarkj 2986250574Smarkj error = dt_unring_buf(dtp, buf); 2987250574Smarkj if (error != 0) { 2988250574Smarkj dt_put_buf(dtp, buf); 2989250574Smarkj return (error); 2990250574Smarkj } 2991250574Smarkj dt_realloc_buf(dtp, buf, size); 2992250574Smarkj 2993250574Smarkj *bufp = buf; 2994250574Smarkj return (0); 2995250574Smarkj} 2996250574Smarkj 2997178479Sjbtypedef struct dt_begin { 2998178479Sjb dtrace_consume_probe_f *dtbgn_probefunc; 2999178479Sjb dtrace_consume_rec_f *dtbgn_recfunc; 3000178479Sjb void *dtbgn_arg; 3001178479Sjb dtrace_handle_err_f *dtbgn_errhdlr; 3002178479Sjb void *dtbgn_errarg; 3003178479Sjb int dtbgn_beginonly; 3004178479Sjb} dt_begin_t; 3005178479Sjb 3006178479Sjbstatic int 3007178479Sjbdt_consume_begin_probe(const dtrace_probedata_t *data, void *arg) 3008178479Sjb{ 3009253725Spfg dt_begin_t *begin = arg; 3010178479Sjb dtrace_probedesc_t *pd = data->dtpda_pdesc; 3011178479Sjb 3012178479Sjb int r1 = (strcmp(pd->dtpd_provider, "dtrace") == 0); 3013178479Sjb int r2 = (strcmp(pd->dtpd_name, "BEGIN") == 0); 3014178479Sjb 3015178479Sjb if (begin->dtbgn_beginonly) { 3016178479Sjb if (!(r1 && r2)) 3017178479Sjb return (DTRACE_CONSUME_NEXT); 3018178479Sjb } else { 3019178479Sjb if (r1 && r2) 3020178479Sjb return (DTRACE_CONSUME_NEXT); 3021178479Sjb } 3022178479Sjb 3023178479Sjb /* 3024178479Sjb * We have a record that we're interested in. Now call the underlying 3025178479Sjb * probe function... 3026178479Sjb */ 3027178479Sjb return (begin->dtbgn_probefunc(data, begin->dtbgn_arg)); 3028178479Sjb} 3029178479Sjb 3030178479Sjbstatic int 3031178479Sjbdt_consume_begin_record(const dtrace_probedata_t *data, 3032178479Sjb const dtrace_recdesc_t *rec, void *arg) 3033178479Sjb{ 3034253725Spfg dt_begin_t *begin = arg; 3035178479Sjb 3036178479Sjb return (begin->dtbgn_recfunc(data, rec, begin->dtbgn_arg)); 3037178479Sjb} 3038178479Sjb 3039178479Sjbstatic int 3040178479Sjbdt_consume_begin_error(const dtrace_errdata_t *data, void *arg) 3041178479Sjb{ 3042178479Sjb dt_begin_t *begin = (dt_begin_t *)arg; 3043178479Sjb dtrace_probedesc_t *pd = data->dteda_pdesc; 3044178479Sjb 3045178479Sjb int r1 = (strcmp(pd->dtpd_provider, "dtrace") == 0); 3046178479Sjb int r2 = (strcmp(pd->dtpd_name, "BEGIN") == 0); 3047178479Sjb 3048178479Sjb if (begin->dtbgn_beginonly) { 3049178479Sjb if (!(r1 && r2)) 3050178479Sjb return (DTRACE_HANDLE_OK); 3051178479Sjb } else { 3052178479Sjb if (r1 && r2) 3053178479Sjb return (DTRACE_HANDLE_OK); 3054178479Sjb } 3055178479Sjb 3056178479Sjb return (begin->dtbgn_errhdlr(data, begin->dtbgn_errarg)); 3057178479Sjb} 3058178479Sjb 3059178479Sjbstatic int 3060250574Smarkjdt_consume_begin(dtrace_hdl_t *dtp, FILE *fp, 3061178479Sjb dtrace_consume_probe_f *pf, dtrace_consume_rec_f *rf, void *arg) 3062178479Sjb{ 3063178479Sjb /* 3064178479Sjb * There's this idea that the BEGIN probe should be processed before 3065178479Sjb * everything else, and that the END probe should be processed after 3066178479Sjb * anything else. In the common case, this is pretty easy to deal 3067178479Sjb * with. However, a situation may arise where the BEGIN enabling and 3068178479Sjb * END enabling are on the same CPU, and some enabling in the middle 3069178479Sjb * occurred on a different CPU. To deal with this (blech!) we need to 3070178479Sjb * consume the BEGIN buffer up until the end of the BEGIN probe, and 3071178479Sjb * then set it aside. We will then process every other CPU, and then 3072178479Sjb * we'll return to the BEGIN CPU and process the rest of the data 3073178479Sjb * (which will inevitably include the END probe, if any). Making this 3074178479Sjb * even more complicated (!) is the library's ERROR enabling. Because 3075178479Sjb * this enabling is processed before we even get into the consume call 3076178479Sjb * back, any ERROR firing would result in the library's ERROR enabling 3077178479Sjb * being processed twice -- once in our first pass (for BEGIN probes), 3078178479Sjb * and again in our second pass (for everything but BEGIN probes). To 3079178479Sjb * deal with this, we interpose on the ERROR handler to assure that we 3080178479Sjb * only process ERROR enablings induced by BEGIN enablings in the 3081178479Sjb * first pass, and that we only process ERROR enablings _not_ induced 3082178479Sjb * by BEGIN enablings in the second pass. 3083178479Sjb */ 3084250574Smarkj 3085178479Sjb dt_begin_t begin; 3086178479Sjb processorid_t cpu = dtp->dt_beganon; 3087178479Sjb int rval, i; 3088178479Sjb static int max_ncpus; 3089250574Smarkj dtrace_bufdesc_t *buf; 3090178479Sjb 3091178479Sjb dtp->dt_beganon = -1; 3092178479Sjb 3093250574Smarkj if (dt_get_buf(dtp, cpu, &buf) != 0) 3094250574Smarkj return (-1); 3095250574Smarkj if (buf == NULL) 3096250574Smarkj return (0); 3097178479Sjb 3098178479Sjb if (!dtp->dt_stopped || buf->dtbd_cpu != dtp->dt_endedon) { 3099178479Sjb /* 3100178479Sjb * This is the simple case. We're either not stopped, or if 3101178479Sjb * we are, we actually processed any END probes on another 3102178479Sjb * CPU. We can simply consume this buffer and return. 3103178479Sjb */ 3104250574Smarkj rval = dt_consume_cpu(dtp, fp, cpu, buf, B_FALSE, 3105250574Smarkj pf, rf, arg); 3106250574Smarkj dt_put_buf(dtp, buf); 3107250574Smarkj return (rval); 3108178479Sjb } 3109178479Sjb 3110178479Sjb begin.dtbgn_probefunc = pf; 3111178479Sjb begin.dtbgn_recfunc = rf; 3112178479Sjb begin.dtbgn_arg = arg; 3113178479Sjb begin.dtbgn_beginonly = 1; 3114178479Sjb 3115178479Sjb /* 3116178479Sjb * We need to interpose on the ERROR handler to be sure that we 3117178479Sjb * only process ERRORs induced by BEGIN. 3118178479Sjb */ 3119178479Sjb begin.dtbgn_errhdlr = dtp->dt_errhdlr; 3120178479Sjb begin.dtbgn_errarg = dtp->dt_errarg; 3121178479Sjb dtp->dt_errhdlr = dt_consume_begin_error; 3122178479Sjb dtp->dt_errarg = &begin; 3123178479Sjb 3124250574Smarkj rval = dt_consume_cpu(dtp, fp, cpu, buf, B_FALSE, 3125250574Smarkj dt_consume_begin_probe, dt_consume_begin_record, &begin); 3126178479Sjb 3127178479Sjb dtp->dt_errhdlr = begin.dtbgn_errhdlr; 3128178479Sjb dtp->dt_errarg = begin.dtbgn_errarg; 3129178479Sjb 3130250574Smarkj if (rval != 0) { 3131250574Smarkj dt_put_buf(dtp, buf); 3132178479Sjb return (rval); 3133250574Smarkj } 3134178479Sjb 3135178479Sjb if (max_ncpus == 0) 3136178479Sjb max_ncpus = dt_sysconf(dtp, _SC_CPUID_MAX) + 1; 3137178479Sjb 3138178479Sjb for (i = 0; i < max_ncpus; i++) { 3139250574Smarkj dtrace_bufdesc_t *nbuf; 3140178479Sjb if (i == cpu) 3141178479Sjb continue; 3142178479Sjb 3143250574Smarkj if (dt_get_buf(dtp, i, &nbuf) != 0) { 3144250574Smarkj dt_put_buf(dtp, buf); 3145250574Smarkj return (-1); 3146178479Sjb } 3147250574Smarkj if (nbuf == NULL) 3148250574Smarkj continue; 3149178479Sjb 3150250574Smarkj rval = dt_consume_cpu(dtp, fp, i, nbuf, B_FALSE, 3151250574Smarkj pf, rf, arg); 3152250574Smarkj dt_put_buf(dtp, nbuf); 3153250574Smarkj if (rval != 0) { 3154250574Smarkj dt_put_buf(dtp, buf); 3155178479Sjb return (rval); 3156178479Sjb } 3157178479Sjb } 3158178479Sjb 3159178479Sjb /* 3160178479Sjb * Okay -- we're done with the other buffers. Now we want to 3161178479Sjb * reconsume the first buffer -- but this time we're looking for 3162178479Sjb * everything _but_ BEGIN. And of course, in order to only consume 3163178479Sjb * those ERRORs _not_ associated with BEGIN, we need to reinstall our 3164178479Sjb * ERROR interposition function... 3165178479Sjb */ 3166178479Sjb begin.dtbgn_beginonly = 0; 3167178479Sjb 3168178479Sjb assert(begin.dtbgn_errhdlr == dtp->dt_errhdlr); 3169178479Sjb assert(begin.dtbgn_errarg == dtp->dt_errarg); 3170178479Sjb dtp->dt_errhdlr = dt_consume_begin_error; 3171178479Sjb dtp->dt_errarg = &begin; 3172178479Sjb 3173250574Smarkj rval = dt_consume_cpu(dtp, fp, cpu, buf, B_FALSE, 3174250574Smarkj dt_consume_begin_probe, dt_consume_begin_record, &begin); 3175178479Sjb 3176178479Sjb dtp->dt_errhdlr = begin.dtbgn_errhdlr; 3177178479Sjb dtp->dt_errarg = begin.dtbgn_errarg; 3178178479Sjb 3179178479Sjb return (rval); 3180178479Sjb} 3181178479Sjb 3182250574Smarkj/* ARGSUSED */ 3183250574Smarkjstatic uint64_t 3184250574Smarkjdt_buf_oldest(void *elem, void *arg) 3185250574Smarkj{ 3186250574Smarkj dtrace_bufdesc_t *buf = elem; 3187250574Smarkj size_t offs = buf->dtbd_oldest; 3188250574Smarkj 3189250574Smarkj while (offs < buf->dtbd_size) { 3190250574Smarkj dtrace_rechdr_t *dtrh = 3191250574Smarkj /* LINTED - alignment */ 3192250574Smarkj (dtrace_rechdr_t *)(buf->dtbd_data + offs); 3193250574Smarkj if (dtrh->dtrh_epid == DTRACE_EPIDNONE) { 3194250574Smarkj offs += sizeof (dtrace_epid_t); 3195250574Smarkj } else { 3196250574Smarkj return (DTRACE_RECORD_LOAD_TIMESTAMP(dtrh)); 3197250574Smarkj } 3198250574Smarkj } 3199250574Smarkj 3200250574Smarkj /* There are no records left; use the time the buffer was retrieved. */ 3201250574Smarkj return (buf->dtbd_timestamp); 3202250574Smarkj} 3203250574Smarkj 3204178479Sjbint 3205178479Sjbdtrace_consume(dtrace_hdl_t *dtp, FILE *fp, 3206178479Sjb dtrace_consume_probe_f *pf, dtrace_consume_rec_f *rf, void *arg) 3207178479Sjb{ 3208178479Sjb dtrace_optval_t size; 3209178479Sjb static int max_ncpus; 3210178479Sjb int i, rval; 3211178479Sjb dtrace_optval_t interval = dtp->dt_options[DTRACEOPT_SWITCHRATE]; 3212178479Sjb hrtime_t now = gethrtime(); 3213178479Sjb 3214178479Sjb if (dtp->dt_lastswitch != 0) { 3215178479Sjb if (now - dtp->dt_lastswitch < interval) 3216178479Sjb return (0); 3217178479Sjb 3218178479Sjb dtp->dt_lastswitch += interval; 3219178479Sjb } else { 3220178479Sjb dtp->dt_lastswitch = now; 3221178479Sjb } 3222178479Sjb 3223178479Sjb if (!dtp->dt_active) 3224178479Sjb return (dt_set_errno(dtp, EINVAL)); 3225178479Sjb 3226178479Sjb if (max_ncpus == 0) 3227178479Sjb max_ncpus = dt_sysconf(dtp, _SC_CPUID_MAX) + 1; 3228178479Sjb 3229178479Sjb if (pf == NULL) 3230178479Sjb pf = (dtrace_consume_probe_f *)dt_nullprobe; 3231178479Sjb 3232178479Sjb if (rf == NULL) 3233178479Sjb rf = (dtrace_consume_rec_f *)dt_nullrec; 3234178479Sjb 3235250574Smarkj if (dtp->dt_options[DTRACEOPT_TEMPORAL] == DTRACEOPT_UNSET) { 3236250574Smarkj /* 3237250574Smarkj * The output will not be in the order it was traced. Rather, 3238250574Smarkj * we will consume all of the data from each CPU's buffer in 3239250574Smarkj * turn. We apply special handling for the records from BEGIN 3240250574Smarkj * and END probes so that they are consumed first and last, 3241250574Smarkj * respectively. 3242250574Smarkj * 3243250574Smarkj * If we have just begun, we want to first process the CPU that 3244250574Smarkj * executed the BEGIN probe (if any). 3245250574Smarkj */ 3246250574Smarkj if (dtp->dt_active && dtp->dt_beganon != -1 && 3247250574Smarkj (rval = dt_consume_begin(dtp, fp, pf, rf, arg)) != 0) 3248249573Spfg return (rval); 3249249573Spfg 3250250574Smarkj for (i = 0; i < max_ncpus; i++) { 3251250574Smarkj dtrace_bufdesc_t *buf; 3252249573Spfg 3253178479Sjb /* 3254250574Smarkj * If we have stopped, we want to process the CPU on 3255250574Smarkj * which the END probe was processed only _after_ we 3256250574Smarkj * have processed everything else. 3257178479Sjb */ 3258250574Smarkj if (dtp->dt_stopped && (i == dtp->dt_endedon)) 3259178479Sjb continue; 3260178479Sjb 3261250574Smarkj if (dt_get_buf(dtp, i, &buf) != 0) 3262250574Smarkj return (-1); 3263250574Smarkj if (buf == NULL) 3264250574Smarkj continue; 3265250574Smarkj 3266250574Smarkj dtp->dt_flow = 0; 3267250574Smarkj dtp->dt_indent = 0; 3268250574Smarkj dtp->dt_prefix = NULL; 3269250574Smarkj rval = dt_consume_cpu(dtp, fp, i, 3270250574Smarkj buf, B_FALSE, pf, rf, arg); 3271250574Smarkj dt_put_buf(dtp, buf); 3272250574Smarkj if (rval != 0) 3273250574Smarkj return (rval); 3274178479Sjb } 3275250574Smarkj if (dtp->dt_stopped) { 3276250574Smarkj dtrace_bufdesc_t *buf; 3277178479Sjb 3278250574Smarkj if (dt_get_buf(dtp, dtp->dt_endedon, &buf) != 0) 3279250574Smarkj return (-1); 3280250574Smarkj if (buf == NULL) 3281250574Smarkj return (0); 3282250574Smarkj 3283250574Smarkj rval = dt_consume_cpu(dtp, fp, dtp->dt_endedon, 3284250574Smarkj buf, B_FALSE, pf, rf, arg); 3285250574Smarkj dt_put_buf(dtp, buf); 3286178479Sjb return (rval); 3287250574Smarkj } 3288250574Smarkj } else { 3289250574Smarkj /* 3290250574Smarkj * The output will be in the order it was traced (or for 3291250574Smarkj * speculations, when it was committed). We retrieve a buffer 3292250574Smarkj * from each CPU and put it into a priority queue, which sorts 3293250574Smarkj * based on the first entry in the buffer. This is sufficient 3294250574Smarkj * because entries within a buffer are already sorted. 3295250574Smarkj * 3296250574Smarkj * We then consume records one at a time, always consuming the 3297250574Smarkj * oldest record, as determined by the priority queue. When 3298250574Smarkj * we reach the end of the time covered by these buffers, 3299250574Smarkj * we need to stop and retrieve more records on the next pass. 3300250574Smarkj * The kernel tells us the time covered by each buffer, in 3301250574Smarkj * dtbd_timestamp. The first buffer's timestamp tells us the 3302250574Smarkj * time covered by all buffers, as subsequently retrieved 3303250574Smarkj * buffers will cover to a more recent time. 3304250574Smarkj */ 3305178479Sjb 3306250574Smarkj uint64_t *drops = alloca(max_ncpus * sizeof (uint64_t)); 3307250574Smarkj uint64_t first_timestamp = 0; 3308250574Smarkj uint_t cookie = 0; 3309250574Smarkj dtrace_bufdesc_t *buf; 3310178479Sjb 3311250574Smarkj bzero(drops, max_ncpus * sizeof (uint64_t)); 3312178479Sjb 3313250574Smarkj if (dtp->dt_bufq == NULL) { 3314250574Smarkj dtp->dt_bufq = dt_pq_init(dtp, max_ncpus * 2, 3315250574Smarkj dt_buf_oldest, NULL); 3316250574Smarkj if (dtp->dt_bufq == NULL) /* ENOMEM */ 3317250574Smarkj return (-1); 3318250574Smarkj } 3319250574Smarkj 3320250574Smarkj /* Retrieve data from each CPU. */ 3321250574Smarkj (void) dtrace_getopt(dtp, "bufsize", &size); 3322250574Smarkj for (i = 0; i < max_ncpus; i++) { 3323250574Smarkj dtrace_bufdesc_t *buf; 3324250574Smarkj 3325250574Smarkj if (dt_get_buf(dtp, i, &buf) != 0) 3326250574Smarkj return (-1); 3327250574Smarkj if (buf != NULL) { 3328250574Smarkj if (first_timestamp == 0) 3329250574Smarkj first_timestamp = buf->dtbd_timestamp; 3330250574Smarkj assert(buf->dtbd_timestamp >= first_timestamp); 3331250574Smarkj 3332250574Smarkj dt_pq_insert(dtp->dt_bufq, buf); 3333250574Smarkj drops[i] = buf->dtbd_drops; 3334250574Smarkj buf->dtbd_drops = 0; 3335250574Smarkj } 3336250574Smarkj } 3337250574Smarkj 3338250574Smarkj /* Consume records. */ 3339250574Smarkj for (;;) { 3340250574Smarkj dtrace_bufdesc_t *buf = dt_pq_pop(dtp->dt_bufq); 3341250574Smarkj uint64_t timestamp; 3342250574Smarkj 3343250574Smarkj if (buf == NULL) 3344250574Smarkj break; 3345250574Smarkj 3346250574Smarkj timestamp = dt_buf_oldest(buf, dtp); 3347250574Smarkj assert(timestamp >= dtp->dt_last_timestamp); 3348250574Smarkj dtp->dt_last_timestamp = timestamp; 3349250574Smarkj 3350250574Smarkj if (timestamp == buf->dtbd_timestamp) { 3351250574Smarkj /* 3352250574Smarkj * We've reached the end of the time covered 3353250574Smarkj * by this buffer. If this is the oldest 3354250574Smarkj * buffer, we must do another pass 3355250574Smarkj * to retrieve more data. 3356250574Smarkj */ 3357250574Smarkj dt_put_buf(dtp, buf); 3358250574Smarkj if (timestamp == first_timestamp && 3359250574Smarkj !dtp->dt_stopped) 3360250574Smarkj break; 3361250574Smarkj continue; 3362250574Smarkj } 3363250574Smarkj 3364250574Smarkj if ((rval = dt_consume_cpu(dtp, fp, 3365250574Smarkj buf->dtbd_cpu, buf, B_TRUE, pf, rf, arg)) != 0) 3366250574Smarkj return (rval); 3367250574Smarkj dt_pq_insert(dtp->dt_bufq, buf); 3368250574Smarkj } 3369250574Smarkj 3370250574Smarkj /* Consume drops. */ 3371250574Smarkj for (i = 0; i < max_ncpus; i++) { 3372250574Smarkj if (drops[i] != 0) { 3373250574Smarkj int error = dt_handle_cpudrop(dtp, i, 3374250574Smarkj DTRACEDROP_PRINCIPAL, drops[i]); 3375250574Smarkj if (error != 0) 3376250574Smarkj return (error); 3377250574Smarkj } 3378250574Smarkj } 3379250574Smarkj 3380178479Sjb /* 3381250574Smarkj * Reduce memory usage by re-allocating smaller buffers 3382250574Smarkj * for the "remnants". 3383178479Sjb */ 3384250574Smarkj while (buf = dt_pq_walk(dtp->dt_bufq, &cookie)) 3385250574Smarkj dt_realloc_buf(dtp, buf, buf->dtbd_size); 3386178479Sjb } 3387178479Sjb 3388250574Smarkj return (0); 3389178479Sjb} 3390