1/* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 * 21 * $FreeBSD: src/sys/cddl/contrib/opensolaris/uts/common/sys/dtrace_impl.h,v 1.3.4.1 2009/08/03 08:13:06 kensmith Exp $ 22 */ 23 24/* 25 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 26 * Use is subject to license terms. 27 */ 28 29#ifndef _SYS_DTRACE_IMPL_H 30#define _SYS_DTRACE_IMPL_H 31 32/* #pragma ident "%Z%%M% %I% %E% SMI" */ 33 34#ifdef __cplusplus 35extern "C" { 36#endif 37 38/* 39 * DTrace Dynamic Tracing Software: Kernel Implementation Interfaces 40 * 41 * Note: The contents of this file are private to the implementation of the 42 * Solaris system and DTrace subsystem and are subject to change at any time 43 * without notice. Applications and drivers using these interfaces will fail 44 * to run on future releases. These interfaces should not be used for any 45 * purpose except those expressly outlined in dtrace(7D) and libdtrace(3LIB). 46 * Please refer to the "Solaris Dynamic Tracing Guide" for more information. 47 */ 48 49#include <sys/dtrace.h> 50#if !defined(sun) 51#ifdef __sparcv9 52typedef uint32_t pc_t; 53#else 54typedef uintptr_t pc_t; 55#endif 56typedef u_long greg_t; 57#endif 58 59/* 60 * DTrace Implementation Constants and Typedefs 61 */ 62#define DTRACE_MAXPROPLEN 128 63#define DTRACE_DYNVAR_CHUNKSIZE 256 64 65struct dtrace_probe; 66struct dtrace_ecb; 67struct dtrace_predicate; 68struct dtrace_action; 69struct dtrace_provider; 70struct dtrace_state; 71 72typedef struct dtrace_probe dtrace_probe_t; 73typedef struct dtrace_ecb dtrace_ecb_t; 74typedef struct dtrace_predicate dtrace_predicate_t; 75typedef struct dtrace_action dtrace_action_t; 76typedef struct dtrace_provider dtrace_provider_t; 77typedef struct dtrace_meta dtrace_meta_t; 78typedef struct dtrace_state dtrace_state_t; 79typedef uint32_t dtrace_optid_t; 80typedef uint32_t dtrace_specid_t; 81typedef uint64_t dtrace_genid_t; 82 83/* 84 * DTrace Probes 85 * 86 * The probe is the fundamental unit of the DTrace architecture. Probes are 87 * created by DTrace providers, and managed by the DTrace framework. A probe 88 * is identified by a unique <provider, module, function, name> tuple, and has 89 * a unique probe identifier assigned to it. (Some probes are not associated 90 * with a specific point in text; these are called _unanchored probes_ and have 91 * no module or function associated with them.) Probes are represented as a 92 * dtrace_probe structure. To allow quick lookups based on each element of the 93 * probe tuple, probes are hashed by each of provider, module, function and 94 * name. (If a lookup is performed based on a regular expression, a 95 * dtrace_probekey is prepared, and a linear search is performed.) Each probe 96 * is additionally pointed to by a linear array indexed by its identifier. The 97 * identifier is the provider's mechanism for indicating to the DTrace 98 * framework that a probe has fired: the identifier is passed as the first 99 * argument to dtrace_probe(), where it is then mapped into the corresponding 100 * dtrace_probe structure. From the dtrace_probe structure, dtrace_probe() can 101 * iterate over the probe's list of enabling control blocks; see "DTrace 102 * Enabling Control Blocks", below.) 103 */ 104struct dtrace_probe { 105 dtrace_id_t dtpr_id; /* probe identifier */ 106 dtrace_ecb_t *dtpr_ecb; /* ECB list; see below */ 107 dtrace_ecb_t *dtpr_ecb_last; /* last ECB in list */ 108 void *dtpr_arg; /* provider argument */ 109 dtrace_cacheid_t dtpr_predcache; /* predicate cache ID */ 110 int dtpr_aframes; /* artificial frames */ 111 dtrace_provider_t *dtpr_provider; /* pointer to provider */ 112 char *dtpr_mod; /* probe's module name */ 113 char *dtpr_func; /* probe's function name */ 114 char *dtpr_name; /* probe's name */ 115 dtrace_probe_t *dtpr_nextmod; /* next in module hash */ 116 dtrace_probe_t *dtpr_prevmod; /* previous in module hash */ 117 dtrace_probe_t *dtpr_nextfunc; /* next in function hash */ 118 dtrace_probe_t *dtpr_prevfunc; /* previous in function hash */ 119 dtrace_probe_t *dtpr_nextname; /* next in name hash */ 120 dtrace_probe_t *dtpr_prevname; /* previous in name hash */ 121 dtrace_genid_t dtpr_gen; /* probe generation ID */ 122}; 123 124typedef int dtrace_probekey_f(const char *, const char *, int); 125 126typedef struct dtrace_probekey { 127 char *dtpk_prov; /* provider name to match */ 128 dtrace_probekey_f *dtpk_pmatch; /* provider matching function */ 129 char *dtpk_mod; /* module name to match */ 130 dtrace_probekey_f *dtpk_mmatch; /* module matching function */ 131 char *dtpk_func; /* func name to match */ 132 dtrace_probekey_f *dtpk_fmatch; /* func matching function */ 133 char *dtpk_name; /* name to match */ 134 dtrace_probekey_f *dtpk_nmatch; /* name matching function */ 135 dtrace_id_t dtpk_id; /* identifier to match */ 136} dtrace_probekey_t; 137 138typedef struct dtrace_hashbucket { 139 struct dtrace_hashbucket *dthb_next; /* next on hash chain */ 140 dtrace_probe_t *dthb_chain; /* chain of probes */ 141 int dthb_len; /* number of probes here */ 142} dtrace_hashbucket_t; 143 144typedef struct dtrace_hash { 145 dtrace_hashbucket_t **dth_tab; /* hash table */ 146 int dth_size; /* size of hash table */ 147 int dth_mask; /* mask to index into table */ 148 int dth_nbuckets; /* total number of buckets */ 149 uintptr_t dth_nextoffs; /* offset of next in probe */ 150 uintptr_t dth_prevoffs; /* offset of prev in probe */ 151 uintptr_t dth_stroffs; /* offset of str in probe */ 152} dtrace_hash_t; 153 154/* 155 * DTrace Enabling Control Blocks 156 * 157 * When a provider wishes to fire a probe, it calls into dtrace_probe(), 158 * passing the probe identifier as the first argument. As described above, 159 * dtrace_probe() maps the identifier into a pointer to a dtrace_probe_t 160 * structure. This structure contains information about the probe, and a 161 * pointer to the list of Enabling Control Blocks (ECBs). Each ECB points to 162 * DTrace consumer state, and contains an optional predicate, and a list of 163 * actions. (Shown schematically below.) The ECB abstraction allows a single 164 * probe to be multiplexed across disjoint consumers, or across disjoint 165 * enablings of a single probe within one consumer. 166 * 167 * Enabling Control Block 168 * dtrace_ecb_t 169 * +------------------------+ 170 * | dtrace_epid_t ---------+--------------> Enabled Probe ID (EPID) 171 * | dtrace_state_t * ------+--------------> State associated with this ECB 172 * | dtrace_predicate_t * --+---------+ 173 * | dtrace_action_t * -----+----+ | 174 * | dtrace_ecb_t * ---+ | | | Predicate (if any) 175 * +-------------------+----+ | | dtrace_predicate_t 176 * | | +---> +--------------------+ 177 * | | | dtrace_difo_t * ---+----> DIFO 178 * | | +--------------------+ 179 * | | 180 * Next ECB | | Action 181 * (if any) | | dtrace_action_t 182 * : +--> +-------------------+ 183 * : | dtrace_actkind_t -+------> kind 184 * v | dtrace_difo_t * --+------> DIFO (if any) 185 * | dtrace_recdesc_t -+------> record descr. 186 * | dtrace_action_t * +------+ 187 * +-------------------+ | 188 * | Next action 189 * +-------------------------------+ (if any) 190 * | 191 * | Action 192 * | dtrace_action_t 193 * +--> +-------------------+ 194 * | dtrace_actkind_t -+------> kind 195 * | dtrace_difo_t * --+------> DIFO (if any) 196 * | dtrace_action_t * +------+ 197 * +-------------------+ | 198 * | Next action 199 * +-------------------------------+ (if any) 200 * | 201 * : 202 * v 203 * 204 * 205 * dtrace_probe() iterates over the ECB list. If the ECB needs less space 206 * than is available in the principal buffer, the ECB is processed: if the 207 * predicate is non-NULL, the DIF object is executed. If the result is 208 * non-zero, the action list is processed, with each action being executed 209 * accordingly. When the action list has been completely executed, processing 210 * advances to the next ECB. processing advances to the next ECB. If the 211 * result is non-zero; For each ECB, it first determines the The ECB 212 * abstraction allows disjoint consumers to multiplex on single probes. 213 */ 214struct dtrace_ecb { 215 dtrace_epid_t dte_epid; /* enabled probe ID */ 216 uint32_t dte_alignment; /* required alignment */ 217 size_t dte_needed; /* bytes needed */ 218 size_t dte_size; /* total size of payload */ 219 dtrace_predicate_t *dte_predicate; /* predicate, if any */ 220 dtrace_action_t *dte_action; /* actions, if any */ 221 dtrace_ecb_t *dte_next; /* next ECB on probe */ 222 dtrace_state_t *dte_state; /* pointer to state */ 223 uint32_t dte_cond; /* security condition */ 224 dtrace_probe_t *dte_probe; /* pointer to probe */ 225 dtrace_action_t *dte_action_last; /* last action on ECB */ 226 uint64_t dte_uarg; /* library argument */ 227}; 228 229struct dtrace_predicate { 230 dtrace_difo_t *dtp_difo; /* DIF object */ 231 dtrace_cacheid_t dtp_cacheid; /* cache identifier */ 232 int dtp_refcnt; /* reference count */ 233}; 234 235struct dtrace_action { 236 dtrace_actkind_t dta_kind; /* kind of action */ 237 uint16_t dta_intuple; /* boolean: in aggregation */ 238 uint32_t dta_refcnt; /* reference count */ 239 dtrace_difo_t *dta_difo; /* pointer to DIFO */ 240 dtrace_recdesc_t dta_rec; /* record description */ 241 dtrace_action_t *dta_prev; /* previous action */ 242 dtrace_action_t *dta_next; /* next action */ 243}; 244 245typedef struct dtrace_aggregation { 246 dtrace_action_t dtag_action; /* action; must be first */ 247 dtrace_aggid_t dtag_id; /* identifier */ 248 dtrace_ecb_t *dtag_ecb; /* corresponding ECB */ 249 dtrace_action_t *dtag_first; /* first action in tuple */ 250 uint32_t dtag_base; /* base of aggregation */ 251 uint8_t dtag_hasarg; /* boolean: has argument */ 252 uint64_t dtag_initial; /* initial value */ 253 void (*dtag_aggregate)(uint64_t *, uint64_t, uint64_t); 254} dtrace_aggregation_t; 255 256/* 257 * DTrace Buffers 258 * 259 * Principal buffers, aggregation buffers, and speculative buffers are all 260 * managed with the dtrace_buffer structure. By default, this structure 261 * includes twin data buffers -- dtb_tomax and dtb_xamot -- that serve as the 262 * active and passive buffers, respectively. For speculative buffers, 263 * dtb_xamot will be NULL; for "ring" and "fill" buffers, dtb_xamot will point 264 * to a scratch buffer. For all buffer types, the dtrace_buffer structure is 265 * always allocated on a per-CPU basis; a single dtrace_buffer structure is 266 * never shared among CPUs. (That is, there is never true sharing of the 267 * dtrace_buffer structure; to prevent false sharing of the structure, it must 268 * always be aligned to the coherence granularity -- generally 64 bytes.) 269 * 270 * One of the critical design decisions of DTrace is that a given ECB always 271 * stores the same quantity and type of data. This is done to assure that the 272 * only metadata required for an ECB's traced data is the EPID. That is, from 273 * the EPID, the consumer can determine the data layout. (The data buffer 274 * layout is shown schematically below.) By assuring that one can determine 275 * data layout from the EPID, the metadata stream can be separated from the 276 * data stream -- simplifying the data stream enormously. 277 * 278 * base of data buffer ---> +------+--------------------+------+ 279 * | EPID | data | EPID | 280 * +------+--------+------+----+------+ 281 * | data | EPID | data | 282 * +---------------+------+-----------+ 283 * | data, cont. | 284 * +------+--------------------+------+ 285 * | EPID | data | | 286 * +------+--------------------+ | 287 * | || | 288 * | || | 289 * | \/ | 290 * : : 291 * . . 292 * . . 293 * . . 294 * : : 295 * | | 296 * limit of data buffer ---> +----------------------------------+ 297 * 298 * When evaluating an ECB, dtrace_probe() determines if the ECB's needs of the 299 * principal buffer (both scratch and payload) exceed the available space. If 300 * the ECB's needs exceed available space (and if the principal buffer policy 301 * is the default "switch" policy), the ECB is dropped, the buffer's drop count 302 * is incremented, and processing advances to the next ECB. If the ECB's needs 303 * can be met with the available space, the ECB is processed, but the offset in 304 * the principal buffer is only advanced if the ECB completes processing 305 * without error. 306 * 307 * When a buffer is to be switched (either because the buffer is the principal 308 * buffer with a "switch" policy or because it is an aggregation buffer), a 309 * cross call is issued to the CPU associated with the buffer. In the cross 310 * call context, interrupts are disabled, and the active and the inactive 311 * buffers are atomically switched. This involves switching the data pointers, 312 * copying the various state fields (offset, drops, errors, etc.) into their 313 * inactive equivalents, and clearing the state fields. Because interrupts are 314 * disabled during this procedure, the switch is guaranteed to appear atomic to 315 * dtrace_probe(). 316 * 317 * DTrace Ring Buffering 318 * 319 * To process a ring buffer correctly, one must know the oldest valid record. 320 * Processing starts at the oldest record in the buffer and continues until 321 * the end of the buffer is reached. Processing then resumes starting with 322 * the record stored at offset 0 in the buffer, and continues until the 323 * youngest record is processed. If trace records are of a fixed-length, 324 * determining the oldest record is trivial: 325 * 326 * - If the ring buffer has not wrapped, the oldest record is the record 327 * stored at offset 0. 328 * 329 * - If the ring buffer has wrapped, the oldest record is the record stored 330 * at the current offset. 331 * 332 * With variable length records, however, just knowing the current offset 333 * doesn't suffice for determining the oldest valid record: assuming that one 334 * allows for arbitrary data, one has no way of searching forward from the 335 * current offset to find the oldest valid record. (That is, one has no way 336 * of separating data from metadata.) It would be possible to simply refuse to 337 * process any data in the ring buffer between the current offset and the 338 * limit, but this leaves (potentially) an enormous amount of otherwise valid 339 * data unprocessed. 340 * 341 * To effect ring buffering, we track two offsets in the buffer: the current 342 * offset and the _wrapped_ offset. If a request is made to reserve some 343 * amount of data, and the buffer has wrapped, the wrapped offset is 344 * incremented until the wrapped offset minus the current offset is greater 345 * than or equal to the reserve request. This is done by repeatedly looking 346 * up the ECB corresponding to the EPID at the current wrapped offset, and 347 * incrementing the wrapped offset by the size of the data payload 348 * corresponding to that ECB. If this offset is greater than or equal to the 349 * limit of the data buffer, the wrapped offset is set to 0. Thus, the 350 * current offset effectively "chases" the wrapped offset around the buffer. 351 * Schematically: 352 * 353 * base of data buffer ---> +------+--------------------+------+ 354 * | EPID | data | EPID | 355 * +------+--------+------+----+------+ 356 * | data | EPID | data | 357 * +---------------+------+-----------+ 358 * | data, cont. | 359 * +------+---------------------------+ 360 * | EPID | data | 361 * current offset ---> +------+---------------------------+ 362 * | invalid data | 363 * wrapped offset ---> +------+--------------------+------+ 364 * | EPID | data | EPID | 365 * +------+--------+------+----+------+ 366 * | data | EPID | data | 367 * +---------------+------+-----------+ 368 * : : 369 * . . 370 * . ... valid data ... . 371 * . . 372 * : : 373 * +------+-------------+------+------+ 374 * | EPID | data | EPID | data | 375 * +------+------------++------+------+ 376 * | data, cont. | leftover | 377 * limit of data buffer ---> +-------------------+--------------+ 378 * 379 * If the amount of requested buffer space exceeds the amount of space 380 * available between the current offset and the end of the buffer: 381 * 382 * (1) all words in the data buffer between the current offset and the limit 383 * of the data buffer (marked "leftover", above) are set to 384 * DTRACE_EPIDNONE 385 * 386 * (2) the wrapped offset is set to zero 387 * 388 * (3) the iteration process described above occurs until the wrapped offset 389 * is greater than the amount of desired space. 390 * 391 * The wrapped offset is implemented by (re-)using the inactive offset. 392 * In a "switch" buffer policy, the inactive offset stores the offset in 393 * the inactive buffer; in a "ring" buffer policy, it stores the wrapped 394 * offset. 395 * 396 * DTrace Scratch Buffering 397 * 398 * Some ECBs may wish to allocate dynamically-sized temporary scratch memory. 399 * To accommodate such requests easily, scratch memory may be allocated in 400 * the buffer beyond the current offset plus the needed memory of the current 401 * ECB. If there isn't sufficient room in the buffer for the requested amount 402 * of scratch space, the allocation fails and an error is generated. Scratch 403 * memory is tracked in the dtrace_mstate_t and is automatically freed when 404 * the ECB ceases processing. Note that ring buffers cannot allocate their 405 * scratch from the principal buffer -- lest they needlessly overwrite older, 406 * valid data. Ring buffers therefore have their own dedicated scratch buffer 407 * from which scratch is allocated. 408 */ 409#define DTRACEBUF_RING 0x0001 /* bufpolicy set to "ring" */ 410#define DTRACEBUF_FILL 0x0002 /* bufpolicy set to "fill" */ 411#define DTRACEBUF_NOSWITCH 0x0004 /* do not switch buffer */ 412#define DTRACEBUF_WRAPPED 0x0008 /* ring buffer has wrapped */ 413#define DTRACEBUF_DROPPED 0x0010 /* drops occurred */ 414#define DTRACEBUF_ERROR 0x0020 /* errors occurred */ 415#define DTRACEBUF_FULL 0x0040 /* "fill" buffer is full */ 416#define DTRACEBUF_CONSUMED 0x0080 /* buffer has been consumed */ 417#define DTRACEBUF_INACTIVE 0x0100 /* buffer is not yet active */ 418 419typedef struct dtrace_buffer { 420 uint64_t dtb_offset; /* current offset in buffer */ 421 uint64_t dtb_size; /* size of buffer */ 422 uint32_t dtb_flags; /* flags */ 423 uint32_t dtb_drops; /* number of drops */ 424 caddr_t dtb_tomax; /* active buffer */ 425 caddr_t dtb_xamot; /* inactive buffer */ 426 uint32_t dtb_xamot_flags; /* inactive flags */ 427 uint32_t dtb_xamot_drops; /* drops in inactive buffer */ 428 uint64_t dtb_xamot_offset; /* offset in inactive buffer */ 429 uint32_t dtb_errors; /* number of errors */ 430 uint32_t dtb_xamot_errors; /* errors in inactive buffer */ 431#ifndef _LP64 432 uint64_t dtb_pad1; 433#endif 434} dtrace_buffer_t; 435 436/* 437 * DTrace Aggregation Buffers 438 * 439 * Aggregation buffers use much of the same mechanism as described above 440 * ("DTrace Buffers"). However, because an aggregation is fundamentally a 441 * hash, there exists dynamic metadata associated with an aggregation buffer 442 * that is not associated with other kinds of buffers. This aggregation 443 * metadata is _only_ relevant for the in-kernel implementation of 444 * aggregations; it is not actually relevant to user-level consumers. To do 445 * this, we allocate dynamic aggregation data (hash keys and hash buckets) 446 * starting below the _limit_ of the buffer, and we allocate data from the 447 * _base_ of the buffer. When the aggregation buffer is copied out, _only_ the 448 * data is copied out; the metadata is simply discarded. Schematically, 449 * aggregation buffers look like: 450 * 451 * base of data buffer ---> +-------+------+-----------+-------+ 452 * | aggid | key | value | aggid | 453 * +-------+------+-----------+-------+ 454 * | key | 455 * +-------+-------+-----+------------+ 456 * | value | aggid | key | value | 457 * +-------+------++-----+------+-----+ 458 * | aggid | key | value | | 459 * +-------+------+-------------+ | 460 * | || | 461 * | || | 462 * | \/ | 463 * : : 464 * . . 465 * . . 466 * . . 467 * : : 468 * | /\ | 469 * | || +------------+ 470 * | || | | 471 * +---------------------+ | 472 * | hash keys | 473 * | (dtrace_aggkey structures) | 474 * | | 475 * +----------------------------------+ 476 * | hash buckets | 477 * | (dtrace_aggbuffer structure) | 478 * | | 479 * limit of data buffer ---> +----------------------------------+ 480 * 481 * 482 * As implied above, just as we assure that ECBs always store a constant 483 * amount of data, we assure that a given aggregation -- identified by its 484 * aggregation ID -- always stores data of a constant quantity and type. 485 * As with EPIDs, this allows the aggregation ID to serve as the metadata for a 486 * given record. 487 * 488 * Note that the size of the dtrace_aggkey structure must be sizeof (uintptr_t) 489 * aligned. (If this the structure changes such that this becomes false, an 490 * assertion will fail in dtrace_aggregate().) 491 */ 492typedef struct dtrace_aggkey { 493 uint32_t dtak_hashval; /* hash value */ 494 uint32_t dtak_action:4; /* action -- 4 bits */ 495 uint32_t dtak_size:28; /* size -- 28 bits */ 496 caddr_t dtak_data; /* data pointer */ 497 struct dtrace_aggkey *dtak_next; /* next in hash chain */ 498} dtrace_aggkey_t; 499 500typedef struct dtrace_aggbuffer { 501 uintptr_t dtagb_hashsize; /* number of buckets */ 502 uintptr_t dtagb_free; /* free list of keys */ 503 dtrace_aggkey_t **dtagb_hash; /* hash table */ 504} dtrace_aggbuffer_t; 505 506/* 507 * DTrace Speculations 508 * 509 * Speculations have a per-CPU buffer and a global state. Once a speculation 510 * buffer has been comitted or discarded, it cannot be reused until all CPUs 511 * have taken the same action (commit or discard) on their respective 512 * speculative buffer. However, because DTrace probes may execute in arbitrary 513 * context, other CPUs cannot simply be cross-called at probe firing time to 514 * perform the necessary commit or discard. The speculation states thus 515 * optimize for the case that a speculative buffer is only active on one CPU at 516 * the time of a commit() or discard() -- for if this is the case, other CPUs 517 * need not take action, and the speculation is immediately available for 518 * reuse. If the speculation is active on multiple CPUs, it must be 519 * asynchronously cleaned -- potentially leading to a higher rate of dirty 520 * speculative drops. The speculation states are as follows: 521 * 522 * DTRACESPEC_INACTIVE <= Initial state; inactive speculation 523 * DTRACESPEC_ACTIVE <= Allocated, but not yet speculatively traced to 524 * DTRACESPEC_ACTIVEONE <= Speculatively traced to on one CPU 525 * DTRACESPEC_ACTIVEMANY <= Speculatively traced to on more than one CPU 526 * DTRACESPEC_COMMITTING <= Currently being commited on one CPU 527 * DTRACESPEC_COMMITTINGMANY <= Currently being commited on many CPUs 528 * DTRACESPEC_DISCARDING <= Currently being discarded on many CPUs 529 * 530 * The state transition diagram is as follows: 531 * 532 * +----------------------------------------------------------+ 533 * | | 534 * | +------------+ | 535 * | +-------------------| COMMITTING |<-----------------+ | 536 * | | +------------+ | | 537 * | | copied spec. ^ commit() on | | discard() on 538 * | | into principal | active CPU | | active CPU 539 * | | | commit() | | 540 * V V | | | 541 * +----------+ +--------+ +-----------+ 542 * | INACTIVE |---------------->| ACTIVE |--------------->| ACTIVEONE | 543 * +----------+ speculation() +--------+ speculate() +-----------+ 544 * ^ ^ | | | 545 * | | | discard() | | 546 * | | asynchronously | discard() on | | speculate() 547 * | | cleaned V inactive CPU | | on inactive 548 * | | +------------+ | | CPU 549 * | +-------------------| DISCARDING |<-----------------+ | 550 * | +------------+ | 551 * | asynchronously ^ | 552 * | copied spec. | discard() | 553 * | into principal +------------------------+ | 554 * | | V 555 * +----------------+ commit() +------------+ 556 * | COMMITTINGMANY |<----------------------------------| ACTIVEMANY | 557 * +----------------+ +------------+ 558 */ 559typedef enum dtrace_speculation_state { 560 DTRACESPEC_INACTIVE = 0, 561 DTRACESPEC_ACTIVE, 562 DTRACESPEC_ACTIVEONE, 563 DTRACESPEC_ACTIVEMANY, 564 DTRACESPEC_COMMITTING, 565 DTRACESPEC_COMMITTINGMANY, 566 DTRACESPEC_DISCARDING 567} dtrace_speculation_state_t; 568 569typedef struct dtrace_speculation { 570 dtrace_speculation_state_t dtsp_state; /* current speculation state */ 571 int dtsp_cleaning; /* non-zero if being cleaned */ 572 dtrace_buffer_t *dtsp_buffer; /* speculative buffer */ 573} dtrace_speculation_t; 574 575/* 576 * DTrace Dynamic Variables 577 * 578 * The dynamic variable problem is obviously decomposed into two subproblems: 579 * allocating new dynamic storage, and freeing old dynamic storage. The 580 * presence of the second problem makes the first much more complicated -- or 581 * rather, the absence of the second renders the first trivial. This is the 582 * case with aggregations, for which there is effectively no deallocation of 583 * dynamic storage. (Or more accurately, all dynamic storage is deallocated 584 * when a snapshot is taken of the aggregation.) As DTrace dynamic variables 585 * allow for both dynamic allocation and dynamic deallocation, the 586 * implementation of dynamic variables is quite a bit more complicated than 587 * that of their aggregation kin. 588 * 589 * We observe that allocating new dynamic storage is tricky only because the 590 * size can vary -- the allocation problem is much easier if allocation sizes 591 * are uniform. We further observe that in D, the size of dynamic variables is 592 * actually _not_ dynamic -- dynamic variable sizes may be determined by static 593 * analysis of DIF text. (This is true even of putatively dynamically-sized 594 * objects like strings and stacks, the sizes of which are dictated by the 595 * "stringsize" and "stackframes" variables, respectively.) We exploit this by 596 * performing this analysis on all DIF before enabling any probes. For each 597 * dynamic load or store, we calculate the dynamically-allocated size plus the 598 * size of the dtrace_dynvar structure plus the storage required to key the 599 * data. For all DIF, we take the largest value and dub it the _chunksize_. 600 * We then divide dynamic memory into two parts: a hash table that is wide 601 * enough to have every chunk in its own bucket, and a larger region of equal 602 * chunksize units. Whenever we wish to dynamically allocate a variable, we 603 * always allocate a single chunk of memory. Depending on the uniformity of 604 * allocation, this will waste some amount of memory -- but it eliminates the 605 * non-determinism inherent in traditional heap fragmentation. 606 * 607 * Dynamic objects are allocated by storing a non-zero value to them; they are 608 * deallocated by storing a zero value to them. Dynamic variables are 609 * complicated enormously by being shared between CPUs. In particular, 610 * consider the following scenario: 611 * 612 * CPU A CPU B 613 * +---------------------------------+ +---------------------------------+ 614 * | | | | 615 * | allocates dynamic object a[123] | | | 616 * | by storing the value 345 to it | | | 617 * | ---------> | 618 * | | | wishing to load from object | 619 * | | | a[123], performs lookup in | 620 * | | | dynamic variable space | 621 * | <--------- | 622 * | deallocates object a[123] by | | | 623 * | storing 0 to it | | | 624 * | | | | 625 * | allocates dynamic object b[567] | | performs load from a[123] | 626 * | by storing the value 789 to it | | | 627 * : : : : 628 * . . . . 629 * 630 * This is obviously a race in the D program, but there are nonetheless only 631 * two valid values for CPU B's load from a[123]: 345 or 0. Most importantly, 632 * CPU B may _not_ see the value 789 for a[123]. 633 * 634 * There are essentially two ways to deal with this: 635 * 636 * (1) Explicitly spin-lock variables. That is, if CPU B wishes to load 637 * from a[123], it needs to lock a[123] and hold the lock for the 638 * duration that it wishes to manipulate it. 639 * 640 * (2) Avoid reusing freed chunks until it is known that no CPU is referring 641 * to them. 642 * 643 * The implementation of (1) is rife with complexity, because it requires the 644 * user of a dynamic variable to explicitly decree when they are done using it. 645 * Were all variables by value, this perhaps wouldn't be debilitating -- but 646 * dynamic variables of non-scalar types are tracked by reference. That is, if 647 * a dynamic variable is, say, a string, and that variable is to be traced to, 648 * say, the principal buffer, the DIF emulation code returns to the main 649 * dtrace_probe() loop a pointer to the underlying storage, not the contents of 650 * the storage. Further, code calling on DIF emulation would have to be aware 651 * that the DIF emulation has returned a reference to a dynamic variable that 652 * has been potentially locked. The variable would have to be unlocked after 653 * the main dtrace_probe() loop is finished with the variable, and the main 654 * dtrace_probe() loop would have to be careful to not call any further DIF 655 * emulation while the variable is locked to avoid deadlock. More generally, 656 * if one were to implement (1), DIF emulation code dealing with dynamic 657 * variables could only deal with one dynamic variable at a time (lest deadlock 658 * result). To sum, (1) exports too much subtlety to the users of dynamic 659 * variables -- increasing maintenance burden and imposing serious constraints 660 * on future DTrace development. 661 * 662 * The implementation of (2) is also complex, but the complexity is more 663 * manageable. We need to be sure that when a variable is deallocated, it is 664 * not placed on a traditional free list, but rather on a _dirty_ list. Once a 665 * variable is on a dirty list, it cannot be found by CPUs performing a 666 * subsequent lookup of the variable -- but it may still be in use by other 667 * CPUs. To assure that all CPUs that may be seeing the old variable have 668 * cleared out of probe context, a dtrace_sync() can be issued. Once the 669 * dtrace_sync() has completed, it can be known that all CPUs are done 670 * manipulating the dynamic variable -- the dirty list can be atomically 671 * appended to the free list. Unfortunately, there's a slight hiccup in this 672 * mechanism: dtrace_sync() may not be issued from probe context. The 673 * dtrace_sync() must be therefore issued asynchronously from non-probe 674 * context. For this we rely on the DTrace cleaner, a cyclic that runs at the 675 * "cleanrate" frequency. To ease this implementation, we define several chunk 676 * lists: 677 * 678 * - Dirty. Deallocated chunks, not yet cleaned. Not available. 679 * 680 * - Rinsing. Formerly dirty chunks that are currently being asynchronously 681 * cleaned. Not available, but will be shortly. Dynamic variable 682 * allocation may not spin or block for availability, however. 683 * 684 * - Clean. Clean chunks, ready for allocation -- but not on the free list. 685 * 686 * - Free. Available for allocation. 687 * 688 * Moreover, to avoid absurd contention, _each_ of these lists is implemented 689 * on a per-CPU basis. This is only for performance, not correctness; chunks 690 * may be allocated from another CPU's free list. The algorithm for allocation 691 * then is this: 692 * 693 * (1) Attempt to atomically allocate from current CPU's free list. If list 694 * is non-empty and allocation is successful, allocation is complete. 695 * 696 * (2) If the clean list is non-empty, atomically move it to the free list, 697 * and reattempt (1). 698 * 699 * (3) If the dynamic variable space is in the CLEAN state, look for free 700 * and clean lists on other CPUs by setting the current CPU to the next 701 * CPU, and reattempting (1). If the next CPU is the current CPU (that 702 * is, if all CPUs have been checked), atomically switch the state of 703 * the dynamic variable space based on the following: 704 * 705 * - If no free chunks were found and no dirty chunks were found, 706 * atomically set the state to EMPTY. 707 * 708 * - If dirty chunks were found, atomically set the state to DIRTY. 709 * 710 * - If rinsing chunks were found, atomically set the state to RINSING. 711 * 712 * (4) Based on state of dynamic variable space state, increment appropriate 713 * counter to indicate dynamic drops (if in EMPTY state) vs. dynamic 714 * dirty drops (if in DIRTY state) vs. dynamic rinsing drops (if in 715 * RINSING state). Fail the allocation. 716 * 717 * The cleaning cyclic operates with the following algorithm: for all CPUs 718 * with a non-empty dirty list, atomically move the dirty list to the rinsing 719 * list. Perform a dtrace_sync(). For all CPUs with a non-empty rinsing list, 720 * atomically move the rinsing list to the clean list. Perform another 721 * dtrace_sync(). By this point, all CPUs have seen the new clean list; the 722 * state of the dynamic variable space can be restored to CLEAN. 723 * 724 * There exist two final races that merit explanation. The first is a simple 725 * allocation race: 726 * 727 * CPU A CPU B 728 * +---------------------------------+ +---------------------------------+ 729 * | | | | 730 * | allocates dynamic object a[123] | | allocates dynamic object a[123] | 731 * | by storing the value 345 to it | | by storing the value 567 to it | 732 * | | | | 733 * : : : : 734 * . . . . 735 * 736 * Again, this is a race in the D program. It can be resolved by having a[123] 737 * hold the value 345 or a[123] hold the value 567 -- but it must be true that 738 * a[123] have only _one_ of these values. (That is, the racing CPUs may not 739 * put the same element twice on the same hash chain.) This is resolved 740 * simply: before the allocation is undertaken, the start of the new chunk's 741 * hash chain is noted. Later, after the allocation is complete, the hash 742 * chain is atomically switched to point to the new element. If this fails 743 * (because of either concurrent allocations or an allocation concurrent with a 744 * deletion), the newly allocated chunk is deallocated to the dirty list, and 745 * the whole process of looking up (and potentially allocating) the dynamic 746 * variable is reattempted. 747 * 748 * The final race is a simple deallocation race: 749 * 750 * CPU A CPU B 751 * +---------------------------------+ +---------------------------------+ 752 * | | | | 753 * | deallocates dynamic object | | deallocates dynamic object | 754 * | a[123] by storing the value 0 | | a[123] by storing the value 0 | 755 * | to it | | to it | 756 * | | | | 757 * : : : : 758 * . . . . 759 * 760 * Once again, this is a race in the D program, but it is one that we must 761 * handle without corrupting the underlying data structures. Because 762 * deallocations require the deletion of a chunk from the middle of a hash 763 * chain, we cannot use a single-word atomic operation to remove it. For this, 764 * we add a spin lock to the hash buckets that is _only_ used for deallocations 765 * (allocation races are handled as above). Further, this spin lock is _only_ 766 * held for the duration of the delete; before control is returned to the DIF 767 * emulation code, the hash bucket is unlocked. 768 */ 769typedef struct dtrace_key { 770 uint64_t dttk_value; /* data value or data pointer */ 771 uint64_t dttk_size; /* 0 if by-val, >0 if by-ref */ 772} dtrace_key_t; 773 774typedef struct dtrace_tuple { 775 uint32_t dtt_nkeys; /* number of keys in tuple */ 776 uint32_t dtt_pad; /* padding */ 777 dtrace_key_t dtt_key[1]; /* array of tuple keys */ 778} dtrace_tuple_t; 779 780typedef struct dtrace_dynvar { 781 uint64_t dtdv_hashval; /* hash value -- 0 if free */ 782 struct dtrace_dynvar *dtdv_next; /* next on list or hash chain */ 783 void *dtdv_data; /* pointer to data */ 784 dtrace_tuple_t dtdv_tuple; /* tuple key */ 785} dtrace_dynvar_t; 786 787typedef enum dtrace_dynvar_op { 788 DTRACE_DYNVAR_ALLOC, 789 DTRACE_DYNVAR_NOALLOC, 790 DTRACE_DYNVAR_DEALLOC 791} dtrace_dynvar_op_t; 792 793typedef struct dtrace_dynhash { 794 dtrace_dynvar_t *dtdh_chain; /* hash chain for this bucket */ 795 uintptr_t dtdh_lock; /* deallocation lock */ 796#ifdef _LP64 797 uintptr_t dtdh_pad[6]; /* pad to avoid false sharing */ 798#else 799 uintptr_t dtdh_pad[14]; /* pad to avoid false sharing */ 800#endif 801} dtrace_dynhash_t; 802 803typedef struct dtrace_dstate_percpu { 804 dtrace_dynvar_t *dtdsc_free; /* free list for this CPU */ 805 dtrace_dynvar_t *dtdsc_dirty; /* dirty list for this CPU */ 806 dtrace_dynvar_t *dtdsc_rinsing; /* rinsing list for this CPU */ 807 dtrace_dynvar_t *dtdsc_clean; /* clean list for this CPU */ 808 uint64_t dtdsc_drops; /* number of capacity drops */ 809 uint64_t dtdsc_dirty_drops; /* number of dirty drops */ 810 uint64_t dtdsc_rinsing_drops; /* number of rinsing drops */ 811#ifdef _LP64 812 uint64_t dtdsc_pad; /* pad to avoid false sharing */ 813#else 814 uint64_t dtdsc_pad[2]; /* pad to avoid false sharing */ 815#endif 816} dtrace_dstate_percpu_t; 817 818typedef enum dtrace_dstate_state { 819 DTRACE_DSTATE_CLEAN = 0, 820 DTRACE_DSTATE_EMPTY, 821 DTRACE_DSTATE_DIRTY, 822 DTRACE_DSTATE_RINSING 823} dtrace_dstate_state_t; 824 825typedef struct dtrace_dstate { 826 void *dtds_base; /* base of dynamic var. space */ 827 size_t dtds_size; /* size of dynamic var. space */ 828 size_t dtds_hashsize; /* number of buckets in hash */ 829 size_t dtds_chunksize; /* size of each chunk */ 830 dtrace_dynhash_t *dtds_hash; /* pointer to hash table */ 831 dtrace_dstate_state_t dtds_state; /* current dynamic var. state */ 832 dtrace_dstate_percpu_t *dtds_percpu; /* per-CPU dyn. var. state */ 833} dtrace_dstate_t; 834 835/* 836 * DTrace Variable State 837 * 838 * The DTrace variable state tracks user-defined variables in its dtrace_vstate 839 * structure. Each DTrace consumer has exactly one dtrace_vstate structure, 840 * but some dtrace_vstate structures may exist without a corresponding DTrace 841 * consumer (see "DTrace Helpers", below). As described in <sys/dtrace.h>, 842 * user-defined variables can have one of three scopes: 843 * 844 * DIFV_SCOPE_GLOBAL => global scope 845 * DIFV_SCOPE_THREAD => thread-local scope (i.e. "self->" variables) 846 * DIFV_SCOPE_LOCAL => clause-local scope (i.e. "this->" variables) 847 * 848 * The variable state tracks variables by both their scope and their allocation 849 * type: 850 * 851 * - The dtvs_globals and dtvs_locals members each point to an array of 852 * dtrace_statvar structures. These structures contain both the variable 853 * metadata (dtrace_difv structures) and the underlying storage for all 854 * statically allocated variables, including statically allocated 855 * DIFV_SCOPE_GLOBAL variables and all DIFV_SCOPE_LOCAL variables. 856 * 857 * - The dtvs_tlocals member points to an array of dtrace_difv structures for 858 * DIFV_SCOPE_THREAD variables. As such, this array tracks _only_ the 859 * variable metadata for DIFV_SCOPE_THREAD variables; the underlying storage 860 * is allocated out of the dynamic variable space. 861 * 862 * - The dtvs_dynvars member is the dynamic variable state associated with the 863 * variable state. The dynamic variable state (described in "DTrace Dynamic 864 * Variables", above) tracks all DIFV_SCOPE_THREAD variables and all 865 * dynamically-allocated DIFV_SCOPE_GLOBAL variables. 866 */ 867typedef struct dtrace_statvar { 868 uint64_t dtsv_data; /* data or pointer to it */ 869 size_t dtsv_size; /* size of pointed-to data */ 870 int dtsv_refcnt; /* reference count */ 871 dtrace_difv_t dtsv_var; /* variable metadata */ 872} dtrace_statvar_t; 873 874typedef struct dtrace_vstate { 875 dtrace_state_t *dtvs_state; /* back pointer to state */ 876 dtrace_statvar_t **dtvs_globals; /* statically-allocated glbls */ 877 int dtvs_nglobals; /* number of globals */ 878 dtrace_difv_t *dtvs_tlocals; /* thread-local metadata */ 879 int dtvs_ntlocals; /* number of thread-locals */ 880 dtrace_statvar_t **dtvs_locals; /* clause-local data */ 881 int dtvs_nlocals; /* number of clause-locals */ 882 dtrace_dstate_t dtvs_dynvars; /* dynamic variable state */ 883} dtrace_vstate_t; 884 885/* 886 * DTrace Machine State 887 * 888 * In the process of processing a fired probe, DTrace needs to track and/or 889 * cache some per-CPU state associated with that particular firing. This is 890 * state that is always discarded after the probe firing has completed, and 891 * much of it is not specific to any DTrace consumer, remaining valid across 892 * all ECBs. This state is tracked in the dtrace_mstate structure. 893 */ 894#define DTRACE_MSTATE_ARGS 0x00000001 895#define DTRACE_MSTATE_PROBE 0x00000002 896#define DTRACE_MSTATE_EPID 0x00000004 897#define DTRACE_MSTATE_TIMESTAMP 0x00000008 898#define DTRACE_MSTATE_STACKDEPTH 0x00000010 899#define DTRACE_MSTATE_CALLER 0x00000020 900#define DTRACE_MSTATE_IPL 0x00000040 901#define DTRACE_MSTATE_FLTOFFS 0x00000080 902#define DTRACE_MSTATE_WALLTIMESTAMP 0x00000100 903#define DTRACE_MSTATE_USTACKDEPTH 0x00000200 904#define DTRACE_MSTATE_UCALLER 0x00000400 905 906typedef struct dtrace_mstate { 907 uintptr_t dtms_scratch_base; /* base of scratch space */ 908 uintptr_t dtms_scratch_ptr; /* current scratch pointer */ 909 size_t dtms_scratch_size; /* scratch size */ 910 uint32_t dtms_present; /* variables that are present */ 911 uint64_t dtms_arg[5]; /* cached arguments */ 912 dtrace_epid_t dtms_epid; /* current EPID */ 913 uint64_t dtms_timestamp; /* cached timestamp */ 914 hrtime_t dtms_walltimestamp; /* cached wall timestamp */ 915 int dtms_stackdepth; /* cached stackdepth */ 916 int dtms_ustackdepth; /* cached ustackdepth */ 917 struct dtrace_probe *dtms_probe; /* current probe */ 918 uintptr_t dtms_caller; /* cached caller */ 919 uint64_t dtms_ucaller; /* cached user-level caller */ 920 int dtms_ipl; /* cached interrupt pri lev */ 921 int dtms_fltoffs; /* faulting DIFO offset */ 922 uintptr_t dtms_strtok; /* saved strtok() pointer */ 923 uint32_t dtms_access; /* memory access rights */ 924 dtrace_difo_t *dtms_difo; /* current dif object */ 925} dtrace_mstate_t; 926 927#define DTRACE_COND_OWNER 0x1 928#define DTRACE_COND_USERMODE 0x2 929#define DTRACE_COND_ZONEOWNER 0x4 930 931#define DTRACE_PROBEKEY_MAXDEPTH 8 /* max glob recursion depth */ 932 933/* 934 * Access flag used by dtrace_mstate.dtms_access. 935 */ 936#define DTRACE_ACCESS_KERNEL 0x1 /* the priv to read kmem */ 937 938 939/* 940 * DTrace Activity 941 * 942 * Each DTrace consumer is in one of several states, which (for purposes of 943 * avoiding yet-another overloading of the noun "state") we call the current 944 * _activity_. The activity transitions on dtrace_go() (from DTRACIOCGO), on 945 * dtrace_stop() (from DTRACIOCSTOP) and on the exit() action. Activities may 946 * only transition in one direction; the activity transition diagram is a 947 * directed acyclic graph. The activity transition diagram is as follows: 948 * 949 * 950 * +----------+ +--------+ +--------+ 951 * | INACTIVE |------------------>| WARMUP |------------------>| ACTIVE | 952 * +----------+ dtrace_go(), +--------+ dtrace_go(), +--------+ 953 * before BEGIN | after BEGIN | | | 954 * | | | | 955 * exit() action | | | | 956 * from BEGIN ECB | | | | 957 * | | | | 958 * v | | | 959 * +----------+ exit() action | | | 960 * +-----------------------------| DRAINING |<-------------------+ | | 961 * | +----------+ | | 962 * | | | | 963 * | dtrace_stop(), | | | 964 * | before END | | | 965 * | | | | 966 * | v | | 967 * | +---------+ +----------+ | | 968 * | | STOPPED |<----------------| COOLDOWN |<----------------------+ | 969 * | +---------+ dtrace_stop(), +----------+ dtrace_stop(), | 970 * | after END before END | 971 * | | 972 * | +--------+ | 973 * +----------------------------->| KILLED |<--------------------------+ 974 * deadman timeout or +--------+ deadman timeout or 975 * killed consumer killed consumer 976 * 977 * Note that once a DTrace consumer has stopped tracing, there is no way to 978 * restart it; if a DTrace consumer wishes to restart tracing, it must reopen 979 * the DTrace pseudodevice. 980 */ 981typedef enum dtrace_activity { 982 DTRACE_ACTIVITY_INACTIVE = 0, /* not yet running */ 983 DTRACE_ACTIVITY_WARMUP, /* while starting */ 984 DTRACE_ACTIVITY_ACTIVE, /* running */ 985 DTRACE_ACTIVITY_DRAINING, /* before stopping */ 986 DTRACE_ACTIVITY_COOLDOWN, /* while stopping */ 987 DTRACE_ACTIVITY_STOPPED, /* after stopping */ 988 DTRACE_ACTIVITY_KILLED /* killed */ 989} dtrace_activity_t; 990 991/* 992 * DTrace Helper Implementation 993 * 994 * A description of the helper architecture may be found in <sys/dtrace.h>. 995 * Each process contains a pointer to its helpers in its p_dtrace_helpers 996 * member. This is a pointer to a dtrace_helpers structure, which contains an 997 * array of pointers to dtrace_helper structures, helper variable state (shared 998 * among a process's helpers) and a generation count. (The generation count is 999 * used to provide an identifier when a helper is added so that it may be 1000 * subsequently removed.) The dtrace_helper structure is self-explanatory, 1001 * containing pointers to the objects needed to execute the helper. Note that 1002 * helpers are _duplicated_ across fork(2), and destroyed on exec(2). No more 1003 * than dtrace_helpers_max are allowed per-process. 1004 */ 1005#define DTRACE_HELPER_ACTION_USTACK 0 1006#define DTRACE_NHELPER_ACTIONS 1 1007 1008typedef struct dtrace_helper_action { 1009 int dtha_generation; /* helper action generation */ 1010 int dtha_nactions; /* number of actions */ 1011 dtrace_difo_t *dtha_predicate; /* helper action predicate */ 1012 dtrace_difo_t **dtha_actions; /* array of actions */ 1013 struct dtrace_helper_action *dtha_next; /* next helper action */ 1014} dtrace_helper_action_t; 1015 1016typedef struct dtrace_helper_provider { 1017 int dthp_generation; /* helper provider generation */ 1018 uint32_t dthp_ref; /* reference count */ 1019 dof_helper_t dthp_prov; /* DOF w/ provider and probes */ 1020} dtrace_helper_provider_t; 1021 1022typedef struct dtrace_helpers { 1023 dtrace_helper_action_t **dthps_actions; /* array of helper actions */ 1024 dtrace_vstate_t dthps_vstate; /* helper action var. state */ 1025 dtrace_helper_provider_t **dthps_provs; /* array of providers */ 1026 uint_t dthps_nprovs; /* count of providers */ 1027 uint_t dthps_maxprovs; /* provider array size */ 1028 int dthps_generation; /* current generation */ 1029 pid_t dthps_pid; /* pid of associated proc */ 1030 int dthps_deferred; /* helper in deferred list */ 1031 struct dtrace_helpers *dthps_next; /* next pointer */ 1032 struct dtrace_helpers *dthps_prev; /* prev pointer */ 1033} dtrace_helpers_t; 1034 1035/* 1036 * DTrace Helper Action Tracing 1037 * 1038 * Debugging helper actions can be arduous. To ease the development and 1039 * debugging of helpers, DTrace contains a tracing-framework-within-a-tracing- 1040 * framework: helper tracing. If dtrace_helptrace_enabled is non-zero (which 1041 * it is by default on DEBUG kernels), all helper activity will be traced to a 1042 * global, in-kernel ring buffer. Each entry includes a pointer to the specific 1043 * helper, the location within the helper, and a trace of all local variables. 1044 * The ring buffer may be displayed in a human-readable format with the 1045 * ::dtrace_helptrace mdb(1) dcmd. 1046 */ 1047#define DTRACE_HELPTRACE_NEXT (-1) 1048#define DTRACE_HELPTRACE_DONE (-2) 1049#define DTRACE_HELPTRACE_ERR (-3) 1050 1051typedef struct dtrace_helptrace { 1052 dtrace_helper_action_t *dtht_helper; /* helper action */ 1053 int dtht_where; /* where in helper action */ 1054 int dtht_nlocals; /* number of locals */ 1055 int dtht_fault; /* type of fault (if any) */ 1056 int dtht_fltoffs; /* DIF offset */ 1057 uint64_t dtht_illval; /* faulting value */ 1058 uint64_t dtht_locals[1]; /* local variables */ 1059} dtrace_helptrace_t; 1060 1061/* 1062 * DTrace Credentials 1063 * 1064 * In probe context, we have limited flexibility to examine the credentials 1065 * of the DTrace consumer that created a particular enabling. We use 1066 * the Least Privilege interfaces to cache the consumer's cred pointer and 1067 * some facts about that credential in a dtrace_cred_t structure. These 1068 * can limit the consumer's breadth of visibility and what actions the 1069 * consumer may take. 1070 */ 1071#define DTRACE_CRV_ALLPROC 0x01 1072#define DTRACE_CRV_KERNEL 0x02 1073#define DTRACE_CRV_ALLZONE 0x04 1074 1075#define DTRACE_CRV_ALL (DTRACE_CRV_ALLPROC | DTRACE_CRV_KERNEL | \ 1076 DTRACE_CRV_ALLZONE) 1077 1078#define DTRACE_CRA_PROC 0x0001 1079#define DTRACE_CRA_PROC_CONTROL 0x0002 1080#define DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER 0x0004 1081#define DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE 0x0008 1082#define DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG 0x0010 1083#define DTRACE_CRA_KERNEL 0x0020 1084#define DTRACE_CRA_KERNEL_DESTRUCTIVE 0x0040 1085 1086#define DTRACE_CRA_ALL (DTRACE_CRA_PROC | \ 1087 DTRACE_CRA_PROC_CONTROL | \ 1088 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER | \ 1089 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE | \ 1090 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG | \ 1091 DTRACE_CRA_KERNEL | \ 1092 DTRACE_CRA_KERNEL_DESTRUCTIVE) 1093 1094typedef struct dtrace_cred { 1095 cred_t *dcr_cred; 1096 uint8_t dcr_destructive; 1097 uint8_t dcr_visible; 1098 uint16_t dcr_action; 1099} dtrace_cred_t; 1100 1101/* 1102 * DTrace Consumer State 1103 * 1104 * Each DTrace consumer has an associated dtrace_state structure that contains 1105 * its in-kernel DTrace state -- including options, credentials, statistics and 1106 * pointers to ECBs, buffers, speculations and formats. A dtrace_state 1107 * structure is also allocated for anonymous enablings. When anonymous state 1108 * is grabbed, the grabbing consumers dts_anon pointer is set to the grabbed 1109 * dtrace_state structure. 1110 */ 1111struct dtrace_state { 1112 dev_t dts_dev; /* device */ 1113 int dts_necbs; /* total number of ECBs */ 1114 dtrace_ecb_t **dts_ecbs; /* array of ECBs */ 1115 dtrace_epid_t dts_epid; /* next EPID to allocate */ 1116 size_t dts_needed; /* greatest needed space */ 1117 struct dtrace_state *dts_anon; /* anon. state, if grabbed */ 1118 dtrace_activity_t dts_activity; /* current activity */ 1119 dtrace_vstate_t dts_vstate; /* variable state */ 1120 dtrace_buffer_t *dts_buffer; /* principal buffer */ 1121 dtrace_buffer_t *dts_aggbuffer; /* aggregation buffer */ 1122 dtrace_speculation_t *dts_speculations; /* speculation array */ 1123 int dts_nspeculations; /* number of speculations */ 1124 int dts_naggregations; /* number of aggregations */ 1125 dtrace_aggregation_t **dts_aggregations; /* aggregation array */ 1126 vmem_t *dts_aggid_arena; /* arena for aggregation IDs */ 1127 uint64_t dts_errors; /* total number of errors */ 1128 uint32_t dts_speculations_busy; /* number of spec. busy */ 1129 uint32_t dts_speculations_unavail; /* number of spec unavail */ 1130 uint32_t dts_stkstroverflows; /* stack string tab overflows */ 1131 uint32_t dts_dblerrors; /* errors in ERROR probes */ 1132 uint32_t dts_reserve; /* space reserved for END */ 1133 hrtime_t dts_laststatus; /* time of last status */ 1134#if defined(sun) 1135 cyclic_id_t dts_cleaner; /* cleaning cyclic */ 1136 cyclic_id_t dts_deadman; /* deadman cyclic */ 1137#else 1138 struct dtrace_state_worker *dts_cleaner;/* cleaning cyclic */ 1139 struct dtrace_state_worker *dts_deadman;/* deadman cyclic */ 1140#endif 1141 hrtime_t dts_alive; /* time last alive */ 1142 char dts_speculates; /* boolean: has speculations */ 1143 char dts_destructive; /* boolean: has dest. actions */ 1144 int dts_nformats; /* number of formats */ 1145 char **dts_formats; /* format string array */ 1146 dtrace_optval_t dts_options[DTRACEOPT_MAX]; /* options */ 1147 dtrace_cred_t dts_cred; /* credentials */ 1148 size_t dts_nretained; /* number of retained enabs */ 1149}; 1150 1151struct dtrace_provider { 1152 dtrace_pattr_t dtpv_attr; /* provider attributes */ 1153 dtrace_ppriv_t dtpv_priv; /* provider privileges */ 1154 dtrace_pops_t dtpv_pops; /* provider operations */ 1155 char *dtpv_name; /* provider name */ 1156 void *dtpv_arg; /* provider argument */ 1157 uint_t dtpv_defunct; /* boolean: defunct provider */ 1158 struct dtrace_provider *dtpv_next; /* next provider */ 1159}; 1160 1161struct dtrace_meta { 1162 dtrace_mops_t dtm_mops; /* meta provider operations */ 1163 char *dtm_name; /* meta provider name */ 1164 void *dtm_arg; /* meta provider user arg */ 1165 uint64_t dtm_count; /* no. of associated provs. */ 1166}; 1167 1168/* 1169 * DTrace Enablings 1170 * 1171 * A dtrace_enabling structure is used to track a collection of ECB 1172 * descriptions -- before they have been turned into actual ECBs. This is 1173 * created as a result of DOF processing, and is generally used to generate 1174 * ECBs immediately thereafter. However, enablings are also generally 1175 * retained should the probes they describe be created at a later time; as 1176 * each new module or provider registers with the framework, the retained 1177 * enablings are reevaluated, with any new match resulting in new ECBs. To 1178 * prevent probes from being matched more than once, the enabling tracks the 1179 * last probe generation matched, and only matches probes from subsequent 1180 * generations. 1181 */ 1182typedef struct dtrace_enabling { 1183 dtrace_ecbdesc_t **dten_desc; /* all ECB descriptions */ 1184 int dten_ndesc; /* number of ECB descriptions */ 1185 int dten_maxdesc; /* size of ECB array */ 1186 dtrace_vstate_t *dten_vstate; /* associated variable state */ 1187 dtrace_genid_t dten_probegen; /* matched probe generation */ 1188 dtrace_ecbdesc_t *dten_current; /* current ECB description */ 1189 int dten_error; /* current error value */ 1190 int dten_primed; /* boolean: set if primed */ 1191 struct dtrace_enabling *dten_prev; /* previous enabling */ 1192 struct dtrace_enabling *dten_next; /* next enabling */ 1193} dtrace_enabling_t; 1194 1195/* 1196 * DTrace Anonymous Enablings 1197 * 1198 * Anonymous enablings are DTrace enablings that are not associated with a 1199 * controlling process, but rather derive their enabling from DOF stored as 1200 * properties in the dtrace.conf file. If there is an anonymous enabling, a 1201 * DTrace consumer state and enabling are created on attach. The state may be 1202 * subsequently grabbed by the first consumer specifying the "grabanon" 1203 * option. As long as an anonymous DTrace enabling exists, dtrace(7D) will 1204 * refuse to unload. 1205 */ 1206typedef struct dtrace_anon { 1207 dtrace_state_t *dta_state; /* DTrace consumer state */ 1208 dtrace_enabling_t *dta_enabling; /* pointer to enabling */ 1209 processorid_t dta_beganon; /* which CPU BEGIN ran on */ 1210} dtrace_anon_t; 1211 1212/* 1213 * DTrace Error Debugging 1214 */ 1215#ifdef DEBUG 1216#define DTRACE_ERRDEBUG 1217#endif 1218 1219#ifdef DTRACE_ERRDEBUG 1220 1221typedef struct dtrace_errhash { 1222 const char *dter_msg; /* error message */ 1223 int dter_count; /* number of times seen */ 1224} dtrace_errhash_t; 1225 1226#define DTRACE_ERRHASHSZ 256 /* must be > number of err msgs */ 1227 1228#endif /* DTRACE_ERRDEBUG */ 1229 1230/* 1231 * DTrace Toxic Ranges 1232 * 1233 * DTrace supports safe loads from probe context; if the address turns out to 1234 * be invalid, a bit will be set by the kernel indicating that DTrace 1235 * encountered a memory error, and DTrace will propagate the error to the user 1236 * accordingly. However, there may exist some regions of memory in which an 1237 * arbitrary load can change system state, and from which it is impossible to 1238 * recover from such a load after it has been attempted. Examples of this may 1239 * include memory in which programmable I/O registers are mapped (for which a 1240 * read may have some implications for the device) or (in the specific case of 1241 * UltraSPARC-I and -II) the virtual address hole. The platform is required 1242 * to make DTrace aware of these toxic ranges; DTrace will then check that 1243 * target addresses are not in a toxic range before attempting to issue a 1244 * safe load. 1245 */ 1246typedef struct dtrace_toxrange { 1247 uintptr_t dtt_base; /* base of toxic range */ 1248 uintptr_t dtt_limit; /* limit of toxic range */ 1249} dtrace_toxrange_t; 1250 1251extern uint64_t dtrace_getarg(int, int); 1252extern greg_t dtrace_getfp(void); 1253extern int dtrace_getipl(void); 1254extern uintptr_t dtrace_caller(int); 1255extern uint32_t dtrace_cas32(uint32_t *, uint32_t, uint32_t); 1256extern void *dtrace_casptr(volatile void *, volatile void *, volatile void *); 1257extern void dtrace_copyin(uintptr_t, uintptr_t, size_t, volatile uint16_t *); 1258extern void dtrace_copyinstr(uintptr_t, uintptr_t, size_t, volatile uint16_t *); 1259extern void dtrace_copyout(uintptr_t, uintptr_t, size_t, volatile uint16_t *); 1260extern void dtrace_copyoutstr(uintptr_t, uintptr_t, size_t, 1261 volatile uint16_t *); 1262extern void dtrace_getpcstack(pc_t *, int, int, uint32_t *); 1263extern ulong_t dtrace_getreg(struct regs *, uint_t); 1264extern int dtrace_getstackdepth(int); 1265extern void dtrace_getupcstack(uint64_t *, int); 1266extern void dtrace_getufpstack(uint64_t *, uint64_t *, int); 1267extern int dtrace_getustackdepth(void); 1268extern uintptr_t dtrace_fulword(void *); 1269extern uint8_t dtrace_fuword8(void *); 1270extern uint16_t dtrace_fuword16(void *); 1271extern uint32_t dtrace_fuword32(void *); 1272extern uint64_t dtrace_fuword64(void *); 1273extern void dtrace_probe_error(dtrace_state_t *, dtrace_epid_t, int, int, 1274 int, uintptr_t); 1275extern int dtrace_assfail(const char *, const char *, int); 1276extern int dtrace_attached(void); 1277#if defined(sun) 1278extern hrtime_t dtrace_gethrestime(void); 1279#endif 1280 1281#ifdef __sparc 1282extern void dtrace_flush_windows(void); 1283extern void dtrace_flush_user_windows(void); 1284extern uint_t dtrace_getotherwin(void); 1285extern uint_t dtrace_getfprs(void); 1286#else 1287extern void dtrace_copy(uintptr_t, uintptr_t, size_t); 1288extern void dtrace_copystr(uintptr_t, uintptr_t, size_t, volatile uint16_t *); 1289#endif 1290 1291/* 1292 * DTrace Assertions 1293 * 1294 * DTrace calls ASSERT from probe context. To assure that a failed ASSERT 1295 * does not induce a markedly more catastrophic failure (e.g., one from which 1296 * a dump cannot be gleaned), DTrace must define its own ASSERT to be one that 1297 * may safely be called from probe context. This header file must thus be 1298 * included by any DTrace component that calls ASSERT from probe context, and 1299 * _only_ by those components. (The only exception to this is kernel 1300 * debugging infrastructure at user-level that doesn't depend on calling 1301 * ASSERT.) 1302 */ 1303#undef ASSERT 1304#ifdef DEBUG 1305#define ASSERT(EX) ((void)((EX) || \ 1306 dtrace_assfail(#EX, __FILE__, __LINE__))) 1307#else 1308#define ASSERT(X) ((void)0) 1309#endif 1310 1311#ifdef __cplusplus 1312} 1313#endif 1314 1315#endif /* _SYS_DTRACE_IMPL_H */ 1316