17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5351346dbSahl * Common Development and Distribution License (the "License"). 6351346dbSahl * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21351346dbSahl 227c478bd9Sstevel@tonic-gate /* 23351346dbSahl * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <assert.h> 307c478bd9Sstevel@tonic-gate #include <dtrace.h> 317c478bd9Sstevel@tonic-gate #include <limits.h> 327c478bd9Sstevel@tonic-gate #include <link.h> 337c478bd9Sstevel@tonic-gate #include <priv.h> 347c478bd9Sstevel@tonic-gate #include <signal.h> 357c478bd9Sstevel@tonic-gate #include <stdlib.h> 367c478bd9Sstevel@tonic-gate #include <stdarg.h> 377c478bd9Sstevel@tonic-gate #include <stdio.h> 387c478bd9Sstevel@tonic-gate #include <string.h> 397c478bd9Sstevel@tonic-gate #include <strings.h> 407c478bd9Sstevel@tonic-gate #include <errno.h> 417c478bd9Sstevel@tonic-gate #include <sys/wait.h> 42351346dbSahl #include <libgen.h> 437c478bd9Sstevel@tonic-gate #include <libproc.h> 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate static char *g_pname; 467c478bd9Sstevel@tonic-gate static dtrace_hdl_t *g_dtp; 477c478bd9Sstevel@tonic-gate struct ps_prochandle *g_pr; 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate #define E_SUCCESS 0 507c478bd9Sstevel@tonic-gate #define E_ERROR 1 517c478bd9Sstevel@tonic-gate #define E_USAGE 2 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate /* 547c478bd9Sstevel@tonic-gate * For hold times we use a global associative array since for mutexes, in 557c478bd9Sstevel@tonic-gate * user-land, it's not invalid to release a sychonization primitive that 567c478bd9Sstevel@tonic-gate * another thread acquired; rwlocks require a thread-local associative array 577c478bd9Sstevel@tonic-gate * since multiple thread can hold the same lock for reading. Note that we 587c478bd9Sstevel@tonic-gate * ignore recursive mutex acquisitions and releases as they don't truly 597c478bd9Sstevel@tonic-gate * affect lock contention. 607c478bd9Sstevel@tonic-gate */ 617c478bd9Sstevel@tonic-gate static const char *g_hold_init = 627c478bd9Sstevel@tonic-gate "plockstat$target:::rw-acquire\n" 637c478bd9Sstevel@tonic-gate "{\n" 647c478bd9Sstevel@tonic-gate " self->rwhold[arg0] = timestamp;\n" 657c478bd9Sstevel@tonic-gate "}\n" 667c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-acquire\n" 677c478bd9Sstevel@tonic-gate "/arg1 == 0/\n" 687c478bd9Sstevel@tonic-gate "{\n" 697c478bd9Sstevel@tonic-gate " mtxhold[arg0] = timestamp;\n" 707c478bd9Sstevel@tonic-gate "}\n"; 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate static const char *g_hold_histogram = 737c478bd9Sstevel@tonic-gate "plockstat$target:::rw-release\n" 747c478bd9Sstevel@tonic-gate "/self->rwhold[arg0] && arg1 == 1/\n" 757c478bd9Sstevel@tonic-gate "{\n" 767c478bd9Sstevel@tonic-gate " @rw_w_hold[arg0, ustack()] =\n" 777c478bd9Sstevel@tonic-gate " quantize(timestamp - self->rwhold[arg0]);\n" 787c478bd9Sstevel@tonic-gate " self->rwhold[arg0] = 0;\n" 79351346dbSahl " rw_w_hold_found = 1;\n" 807c478bd9Sstevel@tonic-gate "}\n" 817c478bd9Sstevel@tonic-gate "plockstat$target:::rw-release\n" 827c478bd9Sstevel@tonic-gate "/self->rwhold[arg0]/\n" 837c478bd9Sstevel@tonic-gate "{\n" 847c478bd9Sstevel@tonic-gate " @rw_r_hold[arg0, ustack()] =\n" 857c478bd9Sstevel@tonic-gate " quantize(timestamp - self->rwhold[arg0]);\n" 867c478bd9Sstevel@tonic-gate " self->rwhold[arg0] = 0;\n" 87351346dbSahl " rw_r_hold_found = 1;\n" 887c478bd9Sstevel@tonic-gate "}\n" 897c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-release\n" 907c478bd9Sstevel@tonic-gate "/mtxhold[arg0] && arg1 == 0/\n" 917c478bd9Sstevel@tonic-gate "{\n" 927c478bd9Sstevel@tonic-gate " @mtx_hold[arg0, ustack()] = quantize(timestamp - mtxhold[arg0]);\n" 937c478bd9Sstevel@tonic-gate " mtxhold[arg0] = 0;\n" 94351346dbSahl " mtx_hold_found = 1;\n" 95351346dbSahl "}\n" 96351346dbSahl "\n" 97351346dbSahl "END\n" 98351346dbSahl "/mtx_hold_found/\n" 99351346dbSahl "{\n" 100351346dbSahl " trace(\"Mutex hold\");\n" 101351346dbSahl " printa(@mtx_hold);\n" 102351346dbSahl "}\n" 103351346dbSahl "END\n" 104351346dbSahl "/rw_r_hold_found/\n" 105351346dbSahl "{\n" 106351346dbSahl " trace(\"R/W reader hold\");\n" 107351346dbSahl " printa(@rw_r_hold);\n" 108351346dbSahl "}\n" 109351346dbSahl "END\n" 110351346dbSahl "/rw_w_hold_found/\n" 111351346dbSahl "{\n" 112351346dbSahl " trace(\"R/W writer hold\");\n" 113351346dbSahl " printa(@rw_w_hold);\n" 1147c478bd9Sstevel@tonic-gate "}\n"; 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate static const char *g_hold_times = 1177c478bd9Sstevel@tonic-gate "plockstat$target:::rw-release\n" 1187c478bd9Sstevel@tonic-gate "/self->rwhold[arg0] && arg1 == 1/\n" 1197c478bd9Sstevel@tonic-gate "{\n" 120351346dbSahl " @rw_w_hold[arg0, ustack(5)] = sum(timestamp - self->rwhold[arg0]);\n" 121351346dbSahl " @rw_w_hold_count[arg0, ustack(5)] = count();\n" 1227c478bd9Sstevel@tonic-gate " self->rwhold[arg0] = 0;\n" 123351346dbSahl " rw_w_hold_found = 1;\n" 1247c478bd9Sstevel@tonic-gate "}\n" 1257c478bd9Sstevel@tonic-gate "plockstat$target:::rw-release\n" 1267c478bd9Sstevel@tonic-gate "/self->rwhold[arg0]/\n" 1277c478bd9Sstevel@tonic-gate "{\n" 128351346dbSahl " @rw_r_hold[arg0, ustack(5)] = sum(timestamp - self->rwhold[arg0]);\n" 129351346dbSahl " @rw_r_hold_count[arg0, ustack(5)] = count();\n" 1307c478bd9Sstevel@tonic-gate " self->rwhold[arg0] = 0;\n" 131351346dbSahl " rw_r_hold_found = 1;\n" 1327c478bd9Sstevel@tonic-gate "}\n" 1337c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-release\n" 1347c478bd9Sstevel@tonic-gate "/mtxhold[arg0] && arg1 == 0/\n" 1357c478bd9Sstevel@tonic-gate "{\n" 136351346dbSahl " @mtx_hold[arg0, ustack(5)] = sum(timestamp - mtxhold[arg0]);\n" 137351346dbSahl " @mtx_hold_count[arg0, ustack(5)] = count();\n" 1387c478bd9Sstevel@tonic-gate " mtxhold[arg0] = 0;\n" 139351346dbSahl " mtx_hold_found = 1;\n" 140351346dbSahl "}\n" 141351346dbSahl "\n" 142351346dbSahl "END\n" 143351346dbSahl "/mtx_hold_found/\n" 144351346dbSahl "{\n" 145351346dbSahl " trace(\"Mutex hold\");\n" 146351346dbSahl " printa(@mtx_hold, @mtx_hold_count);\n" 147351346dbSahl "}\n" 148351346dbSahl "END\n" 149351346dbSahl "/rw_r_hold_found/\n" 150351346dbSahl "{\n" 151351346dbSahl " trace(\"R/W reader hold\");\n" 152351346dbSahl " printa(@rw_r_hold, @rw_r_hold_count);\n" 153351346dbSahl "}\n" 154351346dbSahl "END\n" 155351346dbSahl "/rw_w_hold_found/\n" 156351346dbSahl "{\n" 157351346dbSahl " trace(\"R/W writer hold\");\n" 158351346dbSahl " printa(@rw_w_hold, @rw_w_hold_count);\n" 1597c478bd9Sstevel@tonic-gate "}\n"; 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate /* 1637c478bd9Sstevel@tonic-gate * For contention, we use thread-local associative arrays since we're tracing 1647c478bd9Sstevel@tonic-gate * a single thread's activity in libc and multiple threads can be blocking or 1657c478bd9Sstevel@tonic-gate * spinning on the same sychonization primitive. 1667c478bd9Sstevel@tonic-gate */ 1677c478bd9Sstevel@tonic-gate static const char *g_ctnd_init = 1687c478bd9Sstevel@tonic-gate "plockstat$target:::rw-block\n" 1697c478bd9Sstevel@tonic-gate "{\n" 1707c478bd9Sstevel@tonic-gate " self->rwblock[arg0] = timestamp;\n" 1717c478bd9Sstevel@tonic-gate "}\n" 1727c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-block\n" 1737c478bd9Sstevel@tonic-gate "{\n" 1747c478bd9Sstevel@tonic-gate " self->mtxblock[arg0] = timestamp;\n" 1757c478bd9Sstevel@tonic-gate "}\n" 1767c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-spin\n" 1777c478bd9Sstevel@tonic-gate "{\n" 1787c478bd9Sstevel@tonic-gate " self->mtxspin[arg0] = timestamp;\n" 1797c478bd9Sstevel@tonic-gate "}\n"; 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate static const char *g_ctnd_histogram = 1827c478bd9Sstevel@tonic-gate "plockstat$target:::rw-blocked\n" 1837c478bd9Sstevel@tonic-gate "/self->rwblock[arg0] && arg1 == 1 && arg2 != 0/\n" 1847c478bd9Sstevel@tonic-gate "{\n" 1857c478bd9Sstevel@tonic-gate " @rw_w_block[arg0, ustack()] =\n" 1867c478bd9Sstevel@tonic-gate " quantize(timestamp - self->rwblock[arg0]);\n" 1877c478bd9Sstevel@tonic-gate " self->rwblock[arg0] = 0;\n" 188351346dbSahl " rw_w_block_found = 1;\n" 1897c478bd9Sstevel@tonic-gate "}\n" 1907c478bd9Sstevel@tonic-gate "plockstat$target:::rw-blocked\n" 1917c478bd9Sstevel@tonic-gate "/self->rwblock[arg0] && arg2 != 0/\n" 1927c478bd9Sstevel@tonic-gate "{\n" 1937c478bd9Sstevel@tonic-gate " @rw_r_block[arg0, ustack()] =\n" 1947c478bd9Sstevel@tonic-gate " quantize(timestamp - self->rwblock[arg0]);\n" 1957c478bd9Sstevel@tonic-gate " self->rwblock[arg0] = 0;\n" 196351346dbSahl " rw_r_block_found = 1;\n" 1977c478bd9Sstevel@tonic-gate "}\n" 1987c478bd9Sstevel@tonic-gate "plockstat$target:::rw-blocked\n" 1997c478bd9Sstevel@tonic-gate "/self->rwblock[arg0]/\n" 2007c478bd9Sstevel@tonic-gate "{\n" 2017c478bd9Sstevel@tonic-gate " self->rwblock[arg0] = 0;\n" 2027c478bd9Sstevel@tonic-gate "}\n" 2037c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-spun\n" 2047c478bd9Sstevel@tonic-gate "/self->mtxspin[arg0] && arg1 != 0/\n" 2057c478bd9Sstevel@tonic-gate "{\n" 2067c478bd9Sstevel@tonic-gate " @mtx_spin[arg0, ustack()] =\n" 2077c478bd9Sstevel@tonic-gate " quantize(timestamp - self->mtxspin[arg0]);\n" 2087c478bd9Sstevel@tonic-gate " self->mtxspin[arg0] = 0;\n" 209351346dbSahl " mtx_spin_found = 1;\n" 2107c478bd9Sstevel@tonic-gate "}\n" 2117c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-spun\n" 2127c478bd9Sstevel@tonic-gate "/self->mtxspin[arg0]/\n" 2137c478bd9Sstevel@tonic-gate "{\n" 2147c478bd9Sstevel@tonic-gate " @mtx_vain_spin[arg0, ustack()] =\n" 2157c478bd9Sstevel@tonic-gate " quantize(timestamp - self->mtxspin[arg0]);\n" 2167c478bd9Sstevel@tonic-gate " self->mtxspin[arg0] = 0;\n" 217351346dbSahl " mtx_vain_spin_found = 1;\n" 2187c478bd9Sstevel@tonic-gate "}\n" 2197c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-blocked\n" 2207c478bd9Sstevel@tonic-gate "/self->mtxblock[arg0] && arg1 != 0/\n" 2217c478bd9Sstevel@tonic-gate "{\n" 2227c478bd9Sstevel@tonic-gate " @mtx_block[arg0, ustack()] =\n" 2237c478bd9Sstevel@tonic-gate " quantize(timestamp - self->mtxblock[arg0]);\n" 2247c478bd9Sstevel@tonic-gate " self->mtxblock[arg0] = 0;\n" 225351346dbSahl " mtx_block_found = 1;\n" 2267c478bd9Sstevel@tonic-gate "}\n" 2277c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-blocked\n" 2287c478bd9Sstevel@tonic-gate "/self->mtxblock[arg0]/\n" 2297c478bd9Sstevel@tonic-gate "{\n" 2307c478bd9Sstevel@tonic-gate " self->mtxblock[arg0] = 0;\n" 231351346dbSahl "}\n" 232351346dbSahl "\n" 233351346dbSahl "END\n" 234351346dbSahl "/mtx_block_found/\n" 235351346dbSahl "{\n" 236351346dbSahl " trace(\"Mutex block\");\n" 237351346dbSahl " printa(@mtx_block);\n" 238351346dbSahl "}\n" 239351346dbSahl "END\n" 240351346dbSahl "/mtx_spin_found/\n" 241351346dbSahl "{\n" 242351346dbSahl " trace(\"Mutex spin\");\n" 243351346dbSahl " printa(@mtx_spin);\n" 244351346dbSahl "}\n" 245351346dbSahl "END\n" 246351346dbSahl "/mtx_vain_spin_found/\n" 247351346dbSahl "{\n" 248351346dbSahl " trace(\"Mutex unsuccessful spin\");\n" 249351346dbSahl " printa(@mtx_vain_spin);\n" 250351346dbSahl "}\n" 251351346dbSahl "END\n" 252351346dbSahl "/rw_r_block_found/\n" 253351346dbSahl "{\n" 254351346dbSahl " trace(\"R/W reader block\");\n" 255351346dbSahl " printa(@rw_r_block);\n" 256351346dbSahl "}\n" 257351346dbSahl "END\n" 258351346dbSahl "/rw_w_block_found/\n" 259351346dbSahl "{\n" 260351346dbSahl " trace(\"R/W writer block\");\n" 261351346dbSahl " printa(@rw_w_block);\n" 2627c478bd9Sstevel@tonic-gate "}\n"; 2637c478bd9Sstevel@tonic-gate 2647c478bd9Sstevel@tonic-gate 2657c478bd9Sstevel@tonic-gate static const char *g_ctnd_times = 2667c478bd9Sstevel@tonic-gate "plockstat$target:::rw-blocked\n" 2677c478bd9Sstevel@tonic-gate "/self->rwblock[arg0] && arg1 == 1 && arg2 != 0/\n" 2687c478bd9Sstevel@tonic-gate "{\n" 2697c478bd9Sstevel@tonic-gate " @rw_w_block[arg0, ustack(5)] =\n" 270351346dbSahl " sum(timestamp - self->rwblock[arg0]);\n" 271351346dbSahl " @rw_w_block_count[arg0, ustack(5)] = count();\n" 2727c478bd9Sstevel@tonic-gate " self->rwblock[arg0] = 0;\n" 273351346dbSahl " rw_w_block_found = 1;\n" 2747c478bd9Sstevel@tonic-gate "}\n" 2757c478bd9Sstevel@tonic-gate "plockstat$target:::rw-blocked\n" 2767c478bd9Sstevel@tonic-gate "/self->rwblock[arg0] && arg2 != 0/\n" 2777c478bd9Sstevel@tonic-gate "{\n" 2787c478bd9Sstevel@tonic-gate " @rw_r_block[arg0, ustack(5)] =\n" 279351346dbSahl " sum(timestamp - self->rwblock[arg0]);\n" 280351346dbSahl " @rw_r_block_count[arg0, ustack(5)] = count();\n" 2817c478bd9Sstevel@tonic-gate " self->rwblock[arg0] = 0;\n" 282351346dbSahl " rw_r_block_found = 1;\n" 2837c478bd9Sstevel@tonic-gate "}\n" 2847c478bd9Sstevel@tonic-gate "plockstat$target:::rw-blocked\n" 2857c478bd9Sstevel@tonic-gate "/self->rwblock[arg0]/\n" 2867c478bd9Sstevel@tonic-gate "{\n" 2877c478bd9Sstevel@tonic-gate " self->rwblock[arg0] = 0;\n" 2887c478bd9Sstevel@tonic-gate "}\n" 2897c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-spun\n" 2907c478bd9Sstevel@tonic-gate "/self->mtxspin[arg0] && arg1 != 0/\n" 2917c478bd9Sstevel@tonic-gate "{\n" 2927c478bd9Sstevel@tonic-gate " @mtx_spin[arg0, ustack(5)] =\n" 293351346dbSahl " sum(timestamp - self->mtxspin[arg0]);\n" 294351346dbSahl " @mtx_spin_count[arg0, ustack(5)] = count();\n" 2957c478bd9Sstevel@tonic-gate " self->mtxspin[arg0] = 0;\n" 296351346dbSahl " mtx_spin_found = 1;\n" 2977c478bd9Sstevel@tonic-gate "}\n" 2987c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-spun\n" 2997c478bd9Sstevel@tonic-gate "/self->mtxspin[arg0]/\n" 3007c478bd9Sstevel@tonic-gate "{\n" 3017c478bd9Sstevel@tonic-gate " @mtx_vain_spin[arg0, ustack(5)] =\n" 302351346dbSahl " sum(timestamp - self->mtxspin[arg0]);\n" 303351346dbSahl " @mtx_vain_spin_count[arg0, ustack(5)] = count();\n" 3047c478bd9Sstevel@tonic-gate " self->mtxspin[arg0] = 0;\n" 305351346dbSahl " mtx_vain_spin_found = 1;\n" 3067c478bd9Sstevel@tonic-gate "}\n" 3077c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-blocked\n" 3087c478bd9Sstevel@tonic-gate "/self->mtxblock[arg0] && arg1 != 0/\n" 3097c478bd9Sstevel@tonic-gate "{\n" 3107c478bd9Sstevel@tonic-gate " @mtx_block[arg0, ustack(5)] =\n" 311351346dbSahl " sum(timestamp - self->mtxblock[arg0]);\n" 312351346dbSahl " @mtx_block_count[arg0, ustack(5)] = count();\n" 3137c478bd9Sstevel@tonic-gate " self->mtxblock[arg0] = 0;\n" 314351346dbSahl " mtx_block_found = 1;\n" 3157c478bd9Sstevel@tonic-gate "}\n" 3167c478bd9Sstevel@tonic-gate "plockstat$target:::mutex-blocked\n" 3177c478bd9Sstevel@tonic-gate "/self->mtxblock[arg0]/\n" 3187c478bd9Sstevel@tonic-gate "{\n" 3197c478bd9Sstevel@tonic-gate " self->mtxblock[arg0] = 0;\n" 320351346dbSahl "}\n" 321351346dbSahl "\n" 322351346dbSahl "END\n" 323351346dbSahl "/mtx_block_found/\n" 324351346dbSahl "{\n" 325351346dbSahl " trace(\"Mutex block\");\n" 326351346dbSahl " printa(@mtx_block, @mtx_block_count);\n" 327351346dbSahl "}\n" 328351346dbSahl "END\n" 329351346dbSahl "/mtx_spin_found/\n" 330351346dbSahl "{\n" 331351346dbSahl " trace(\"Mutex spin\");\n" 332351346dbSahl " printa(@mtx_spin, @mtx_spin_count);\n" 333351346dbSahl "}\n" 334351346dbSahl "END\n" 335351346dbSahl "/mtx_vain_spin_found/\n" 336351346dbSahl "{\n" 337351346dbSahl " trace(\"Mutex unsuccessful spin\");\n" 338351346dbSahl " printa(@mtx_vain_spin, @mtx_vain_spin_count);\n" 339351346dbSahl "}\n" 340351346dbSahl "END\n" 341351346dbSahl "/rw_r_block_found/\n" 342351346dbSahl "{\n" 343351346dbSahl " trace(\"R/W reader block\");\n" 344351346dbSahl " printa(@rw_r_block, @rw_r_block_count);\n" 345351346dbSahl "}\n" 346351346dbSahl "END\n" 347351346dbSahl "/rw_w_block_found/\n" 348351346dbSahl "{\n" 349351346dbSahl " trace(\"R/W writer block\");\n" 350351346dbSahl " printa(@rw_w_block, @rw_w_block_count);\n" 3517c478bd9Sstevel@tonic-gate "}\n"; 3527c478bd9Sstevel@tonic-gate 353351346dbSahl static char g_prog[4096]; 3547c478bd9Sstevel@tonic-gate static size_t g_proglen; 3557c478bd9Sstevel@tonic-gate static int g_opt_V, g_opt_s; 3567c478bd9Sstevel@tonic-gate static int g_intr; 357351346dbSahl static int g_exited; 3587c478bd9Sstevel@tonic-gate static dtrace_optval_t g_nframes; 3597c478bd9Sstevel@tonic-gate static ulong_t g_nent = ULONG_MAX; 3607c478bd9Sstevel@tonic-gate 3617c478bd9Sstevel@tonic-gate #define PLOCKSTAT_OPTSTR "n:ps:e:vx:ACHV" 3627c478bd9Sstevel@tonic-gate 3637c478bd9Sstevel@tonic-gate static void 3647c478bd9Sstevel@tonic-gate usage(void) 3657c478bd9Sstevel@tonic-gate { 3667c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Usage:\n" 3677c478bd9Sstevel@tonic-gate "\t%s [-vACHV] [-n count] [-s depth] [-e secs] [-x opt[=val]]\n" 3687c478bd9Sstevel@tonic-gate "\t command [arg...]\n" 3697c478bd9Sstevel@tonic-gate "\t%s [-vACHV] [-n count] [-s depth] [-e secs] [-x opt[=val]]\n" 3707c478bd9Sstevel@tonic-gate "\t -p pid\n", g_pname, g_pname); 3717c478bd9Sstevel@tonic-gate 3727c478bd9Sstevel@tonic-gate exit(E_USAGE); 3737c478bd9Sstevel@tonic-gate } 3747c478bd9Sstevel@tonic-gate 3757c478bd9Sstevel@tonic-gate static void 3767c478bd9Sstevel@tonic-gate verror(const char *fmt, va_list ap) 3777c478bd9Sstevel@tonic-gate { 3787c478bd9Sstevel@tonic-gate int error = errno; 3797c478bd9Sstevel@tonic-gate 3807c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", g_pname); 3817c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, fmt, ap); 3827c478bd9Sstevel@tonic-gate 3837c478bd9Sstevel@tonic-gate if (fmt[strlen(fmt) - 1] != '\n') 3847c478bd9Sstevel@tonic-gate (void) fprintf(stderr, ": %s\n", strerror(error)); 3857c478bd9Sstevel@tonic-gate } 3867c478bd9Sstevel@tonic-gate 3877c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 3887c478bd9Sstevel@tonic-gate static void 3897c478bd9Sstevel@tonic-gate fatal(const char *fmt, ...) 3907c478bd9Sstevel@tonic-gate { 3917c478bd9Sstevel@tonic-gate va_list ap; 3927c478bd9Sstevel@tonic-gate 3937c478bd9Sstevel@tonic-gate va_start(ap, fmt); 3947c478bd9Sstevel@tonic-gate verror(fmt, ap); 3957c478bd9Sstevel@tonic-gate va_end(ap); 3967c478bd9Sstevel@tonic-gate 3977c478bd9Sstevel@tonic-gate if (g_pr != NULL && g_dtp != NULL) 3987c478bd9Sstevel@tonic-gate dtrace_proc_release(g_dtp, g_pr); 3997c478bd9Sstevel@tonic-gate 4007c478bd9Sstevel@tonic-gate exit(E_ERROR); 4017c478bd9Sstevel@tonic-gate } 4027c478bd9Sstevel@tonic-gate 4037c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 4047c478bd9Sstevel@tonic-gate static void 4057c478bd9Sstevel@tonic-gate dfatal(const char *fmt, ...) 4067c478bd9Sstevel@tonic-gate { 4077c478bd9Sstevel@tonic-gate va_list ap; 4087c478bd9Sstevel@tonic-gate 4097c478bd9Sstevel@tonic-gate va_start(ap, fmt); 4107c478bd9Sstevel@tonic-gate 4117c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", g_pname); 4127c478bd9Sstevel@tonic-gate if (fmt != NULL) 4137c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, fmt, ap); 4147c478bd9Sstevel@tonic-gate 4157c478bd9Sstevel@tonic-gate va_end(ap); 4167c478bd9Sstevel@tonic-gate 4177c478bd9Sstevel@tonic-gate if (fmt != NULL && fmt[strlen(fmt) - 1] != '\n') { 4187c478bd9Sstevel@tonic-gate (void) fprintf(stderr, ": %s\n", 4197c478bd9Sstevel@tonic-gate dtrace_errmsg(g_dtp, dtrace_errno(g_dtp))); 4207c478bd9Sstevel@tonic-gate } else if (fmt == NULL) { 4217c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s\n", 4227c478bd9Sstevel@tonic-gate dtrace_errmsg(g_dtp, dtrace_errno(g_dtp))); 4237c478bd9Sstevel@tonic-gate } 4247c478bd9Sstevel@tonic-gate 4257c478bd9Sstevel@tonic-gate if (g_pr != NULL) { 4267c478bd9Sstevel@tonic-gate dtrace_proc_continue(g_dtp, g_pr); 4277c478bd9Sstevel@tonic-gate dtrace_proc_release(g_dtp, g_pr); 4287c478bd9Sstevel@tonic-gate } 4297c478bd9Sstevel@tonic-gate 4307c478bd9Sstevel@tonic-gate exit(E_ERROR); 4317c478bd9Sstevel@tonic-gate } 4327c478bd9Sstevel@tonic-gate 4337c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 4347c478bd9Sstevel@tonic-gate static void 4357c478bd9Sstevel@tonic-gate notice(const char *fmt, ...) 4367c478bd9Sstevel@tonic-gate { 4377c478bd9Sstevel@tonic-gate va_list ap; 4387c478bd9Sstevel@tonic-gate 4397c478bd9Sstevel@tonic-gate va_start(ap, fmt); 4407c478bd9Sstevel@tonic-gate verror(fmt, ap); 4417c478bd9Sstevel@tonic-gate va_end(ap); 4427c478bd9Sstevel@tonic-gate } 4437c478bd9Sstevel@tonic-gate 4447c478bd9Sstevel@tonic-gate static void 4457c478bd9Sstevel@tonic-gate dprog_add(const char *prog) 4467c478bd9Sstevel@tonic-gate { 4477c478bd9Sstevel@tonic-gate size_t len = strlen(prog); 4487c478bd9Sstevel@tonic-gate bcopy(prog, g_prog + g_proglen, len + 1); 4497c478bd9Sstevel@tonic-gate g_proglen += len; 450351346dbSahl assert(g_proglen < sizeof (g_prog)); 4517c478bd9Sstevel@tonic-gate } 4527c478bd9Sstevel@tonic-gate 4537c478bd9Sstevel@tonic-gate static void 4547c478bd9Sstevel@tonic-gate dprog_compile(void) 4557c478bd9Sstevel@tonic-gate { 4567c478bd9Sstevel@tonic-gate dtrace_prog_t *prog; 4577c478bd9Sstevel@tonic-gate dtrace_proginfo_t info; 4587c478bd9Sstevel@tonic-gate 4597c478bd9Sstevel@tonic-gate if (g_opt_V) { 4607c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: vvvv D program vvvv\n", g_pname); 4617c478bd9Sstevel@tonic-gate (void) fputs(g_prog, stderr); 4627c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ^^^^ D program ^^^^\n", g_pname); 4637c478bd9Sstevel@tonic-gate } 4647c478bd9Sstevel@tonic-gate 4657c478bd9Sstevel@tonic-gate if ((prog = dtrace_program_strcompile(g_dtp, g_prog, 4667c478bd9Sstevel@tonic-gate DTRACE_PROBESPEC_NAME, 0, 0, NULL)) == NULL) 4677c478bd9Sstevel@tonic-gate dfatal("failed to compile program"); 4687c478bd9Sstevel@tonic-gate 4697c478bd9Sstevel@tonic-gate if (dtrace_program_exec(g_dtp, prog, &info) == -1) 4707c478bd9Sstevel@tonic-gate dfatal("failed to enable probes"); 4717c478bd9Sstevel@tonic-gate } 4727c478bd9Sstevel@tonic-gate 4737c478bd9Sstevel@tonic-gate void 4747c478bd9Sstevel@tonic-gate print_legend(void) 4757c478bd9Sstevel@tonic-gate { 4767c478bd9Sstevel@tonic-gate (void) printf("%5s %8s %-28s %s\n", "Count", "nsec", "Lock", "Caller"); 4777c478bd9Sstevel@tonic-gate } 4787c478bd9Sstevel@tonic-gate 4797c478bd9Sstevel@tonic-gate void 4807c478bd9Sstevel@tonic-gate print_bar(void) 4817c478bd9Sstevel@tonic-gate { 4827c478bd9Sstevel@tonic-gate (void) printf("---------------------------------------" 4837c478bd9Sstevel@tonic-gate "----------------------------------------\n"); 4847c478bd9Sstevel@tonic-gate } 4857c478bd9Sstevel@tonic-gate 4867c478bd9Sstevel@tonic-gate void 4877c478bd9Sstevel@tonic-gate print_histogram_header(void) 4887c478bd9Sstevel@tonic-gate { 4897c478bd9Sstevel@tonic-gate (void) printf("\n%10s ---- Time Distribution --- %5s %s\n", 4907c478bd9Sstevel@tonic-gate "nsec", "count", "Stack"); 4917c478bd9Sstevel@tonic-gate } 4927c478bd9Sstevel@tonic-gate 4937c478bd9Sstevel@tonic-gate /* 4947c478bd9Sstevel@tonic-gate * Convert an address to a symbolic string or a numeric string. If nolocks 4957c478bd9Sstevel@tonic-gate * is set, we return an error code if this symbol appears to be a mutex- or 4967c478bd9Sstevel@tonic-gate * rwlock-related symbol in libc so the caller has a chance to find a more 4977c478bd9Sstevel@tonic-gate * helpful symbol. 4987c478bd9Sstevel@tonic-gate */ 4997c478bd9Sstevel@tonic-gate static int 5007c478bd9Sstevel@tonic-gate getsym(struct ps_prochandle *P, uintptr_t addr, char *buf, size_t size, 5017c478bd9Sstevel@tonic-gate int nolocks) 5027c478bd9Sstevel@tonic-gate { 5037c478bd9Sstevel@tonic-gate char name[256]; 5047c478bd9Sstevel@tonic-gate GElf_Sym sym; 5057c478bd9Sstevel@tonic-gate prsyminfo_t info; 5067c478bd9Sstevel@tonic-gate size_t len; 5077c478bd9Sstevel@tonic-gate 5087c478bd9Sstevel@tonic-gate if (P == NULL || Pxlookup_by_addr(P, addr, name, sizeof (name), 5097c478bd9Sstevel@tonic-gate &sym, &info) != 0) { 5107c478bd9Sstevel@tonic-gate (void) snprintf(buf, size, "%#lx", addr); 5117c478bd9Sstevel@tonic-gate return (0); 5127c478bd9Sstevel@tonic-gate } 513*12e72dbbSrh87107 if (info.prs_object == NULL) 514*12e72dbbSrh87107 info.prs_object = "<unknown>"; 5157c478bd9Sstevel@tonic-gate 5167c478bd9Sstevel@tonic-gate if (info.prs_lmid != LM_ID_BASE) { 5177c478bd9Sstevel@tonic-gate len = snprintf(buf, size, "LM%lu`", info.prs_lmid); 5187c478bd9Sstevel@tonic-gate buf += len; 5197c478bd9Sstevel@tonic-gate size -= len; 5207c478bd9Sstevel@tonic-gate } 5217c478bd9Sstevel@tonic-gate 5227c478bd9Sstevel@tonic-gate len = snprintf(buf, size, "%s`%s", info.prs_object, info.prs_name); 5237c478bd9Sstevel@tonic-gate buf += len; 5247c478bd9Sstevel@tonic-gate size -= len; 5257c478bd9Sstevel@tonic-gate 5267c478bd9Sstevel@tonic-gate if (sym.st_value != addr) 5277c478bd9Sstevel@tonic-gate len = snprintf(buf, size, "+%#lx", addr - sym.st_value); 5287c478bd9Sstevel@tonic-gate 5297c478bd9Sstevel@tonic-gate if (nolocks && strcmp("libc.so.1", info.prs_object) == 0 && 5307c478bd9Sstevel@tonic-gate (strstr("mutex", info.prs_name) == 0 || 5317c478bd9Sstevel@tonic-gate strstr("rw", info.prs_name) == 0)) 5327c478bd9Sstevel@tonic-gate return (-1); 5337c478bd9Sstevel@tonic-gate 5347c478bd9Sstevel@tonic-gate return (0); 5357c478bd9Sstevel@tonic-gate } 5367c478bd9Sstevel@tonic-gate 5377c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 5387c478bd9Sstevel@tonic-gate static int 539351346dbSahl process_aggregate(const dtrace_aggdata_t **aggsdata, int naggvars, void *arg) 5407c478bd9Sstevel@tonic-gate { 541a1b5e537Sbmc const dtrace_recdesc_t *rec; 5427c478bd9Sstevel@tonic-gate uintptr_t lock; 5437c478bd9Sstevel@tonic-gate uint64_t *stack; 544351346dbSahl caddr_t data; 545351346dbSahl pid_t pid; 5467c478bd9Sstevel@tonic-gate struct ps_prochandle *P; 547351346dbSahl char buf[256]; 5487c478bd9Sstevel@tonic-gate int i, j; 549351346dbSahl uint64_t sum, count, avg; 5507c478bd9Sstevel@tonic-gate 551351346dbSahl if ((*(uint_t *)arg)++ >= g_nent) 552351346dbSahl return (DTRACE_AGGWALK_NEXT); 5537c478bd9Sstevel@tonic-gate 554351346dbSahl rec = aggsdata[0]->dtada_desc->dtagd_rec; 555351346dbSahl data = aggsdata[0]->dtada_data; 5567c478bd9Sstevel@tonic-gate 5577c478bd9Sstevel@tonic-gate /*LINTED - alignment*/ 5587c478bd9Sstevel@tonic-gate lock = (uintptr_t)*(uint64_t *)(data + rec[1].dtrd_offset); 5597c478bd9Sstevel@tonic-gate /*LINTED - alignment*/ 5607c478bd9Sstevel@tonic-gate stack = (uint64_t *)(data + rec[2].dtrd_offset); 5617c478bd9Sstevel@tonic-gate 5627c478bd9Sstevel@tonic-gate if (!g_opt_s) { 563351346dbSahl /*LINTED - alignment*/ 564351346dbSahl sum = *(uint64_t *)(aggsdata[1]->dtada_data + 565351346dbSahl aggsdata[1]->dtada_desc->dtagd_rec[3].dtrd_offset); 566351346dbSahl /*LINTED - alignment*/ 567351346dbSahl count = *(uint64_t *)(aggsdata[2]->dtada_data + 568351346dbSahl aggsdata[2]->dtada_desc->dtagd_rec[3].dtrd_offset); 5697c478bd9Sstevel@tonic-gate } else { 570351346dbSahl uint64_t *a; 571351346dbSahl 572351346dbSahl /*LINTED - alignment*/ 573351346dbSahl a = (uint64_t *)(aggsdata[1]->dtada_data + 574351346dbSahl aggsdata[1]->dtada_desc->dtagd_rec[3].dtrd_offset); 575351346dbSahl 5767c478bd9Sstevel@tonic-gate print_bar(); 5777c478bd9Sstevel@tonic-gate print_legend(); 5787c478bd9Sstevel@tonic-gate 579351346dbSahl for (count = sum = 0, i = DTRACE_QUANTIZE_ZEROBUCKET, j = 0; 5807c478bd9Sstevel@tonic-gate i < DTRACE_QUANTIZE_NBUCKETS; i++, j++) { 5817c478bd9Sstevel@tonic-gate count += a[i]; 582351346dbSahl sum += a[i] << (j - 64); 583351346dbSahl } 5847c478bd9Sstevel@tonic-gate } 5857c478bd9Sstevel@tonic-gate 586351346dbSahl avg = sum / count; 5877c478bd9Sstevel@tonic-gate (void) printf("%5llu %8llu ", (u_longlong_t)count, (u_longlong_t)avg); 5887c478bd9Sstevel@tonic-gate 5897c478bd9Sstevel@tonic-gate pid = stack[0]; 5907c478bd9Sstevel@tonic-gate P = dtrace_proc_grab(g_dtp, pid, PGRAB_RDONLY); 5917c478bd9Sstevel@tonic-gate 5927c478bd9Sstevel@tonic-gate (void) getsym(P, lock, buf, sizeof (buf), 0); 5937c478bd9Sstevel@tonic-gate (void) printf("%-28s ", buf); 5947c478bd9Sstevel@tonic-gate 5957c478bd9Sstevel@tonic-gate for (i = 2; i <= 5; i++) { 5967c478bd9Sstevel@tonic-gate if (getsym(P, stack[i], buf, sizeof (buf), 1) == 0) 5977c478bd9Sstevel@tonic-gate break; 5987c478bd9Sstevel@tonic-gate } 5997c478bd9Sstevel@tonic-gate (void) printf("%s\n", buf); 6007c478bd9Sstevel@tonic-gate 6017c478bd9Sstevel@tonic-gate if (g_opt_s) { 6027c478bd9Sstevel@tonic-gate int stack_done = 0; 6037c478bd9Sstevel@tonic-gate int quant_done = 0; 6047c478bd9Sstevel@tonic-gate int first_bin, last_bin; 605351346dbSahl uint64_t bin_size, *a; 606351346dbSahl 607351346dbSahl /*LINTED - alignment*/ 608351346dbSahl a = (uint64_t *)(aggsdata[1]->dtada_data + 609351346dbSahl aggsdata[1]->dtada_desc->dtagd_rec[3].dtrd_offset); 6107c478bd9Sstevel@tonic-gate 6117c478bd9Sstevel@tonic-gate print_histogram_header(); 6127c478bd9Sstevel@tonic-gate 6137c478bd9Sstevel@tonic-gate for (first_bin = DTRACE_QUANTIZE_ZEROBUCKET; 6147c478bd9Sstevel@tonic-gate a[first_bin] == 0; first_bin++) 6157c478bd9Sstevel@tonic-gate continue; 6167c478bd9Sstevel@tonic-gate for (last_bin = DTRACE_QUANTIZE_ZEROBUCKET + 63; 6177c478bd9Sstevel@tonic-gate a[last_bin] == 0; last_bin--) 6187c478bd9Sstevel@tonic-gate continue; 6197c478bd9Sstevel@tonic-gate 6207c478bd9Sstevel@tonic-gate for (i = 0; !stack_done || !quant_done; i++) { 6217c478bd9Sstevel@tonic-gate if (!stack_done) { 6227c478bd9Sstevel@tonic-gate (void) getsym(P, stack[i + 2], buf, 6237c478bd9Sstevel@tonic-gate sizeof (buf), 0); 6247c478bd9Sstevel@tonic-gate } else { 6257c478bd9Sstevel@tonic-gate buf[0] = '\0'; 6267c478bd9Sstevel@tonic-gate } 6277c478bd9Sstevel@tonic-gate 6287c478bd9Sstevel@tonic-gate if (!quant_done) { 6297c478bd9Sstevel@tonic-gate bin_size = a[first_bin]; 6307c478bd9Sstevel@tonic-gate 6317c478bd9Sstevel@tonic-gate (void) printf("%10llu |%-24.*s| %5llu %s\n", 6327c478bd9Sstevel@tonic-gate 1ULL << 6337c478bd9Sstevel@tonic-gate (first_bin - DTRACE_QUANTIZE_ZEROBUCKET), 6347c478bd9Sstevel@tonic-gate (int)(24.0 * bin_size / count), 6357c478bd9Sstevel@tonic-gate "@@@@@@@@@@@@@@@@@@@@@@@@@@", 6367c478bd9Sstevel@tonic-gate (u_longlong_t)bin_size, buf); 6377c478bd9Sstevel@tonic-gate } else { 6387c478bd9Sstevel@tonic-gate (void) printf("%43s %s\n", "", buf); 6397c478bd9Sstevel@tonic-gate } 6407c478bd9Sstevel@tonic-gate 6417c478bd9Sstevel@tonic-gate if (i + 1 >= g_nframes || stack[i + 3] == 0) 6427c478bd9Sstevel@tonic-gate stack_done = 1; 6437c478bd9Sstevel@tonic-gate 6447c478bd9Sstevel@tonic-gate if (first_bin++ == last_bin) 6457c478bd9Sstevel@tonic-gate quant_done = 1; 6467c478bd9Sstevel@tonic-gate } 6477c478bd9Sstevel@tonic-gate } 6487c478bd9Sstevel@tonic-gate 6497c478bd9Sstevel@tonic-gate dtrace_proc_release(g_dtp, P); 6507c478bd9Sstevel@tonic-gate 6517c478bd9Sstevel@tonic-gate return (DTRACE_AGGWALK_NEXT); 6527c478bd9Sstevel@tonic-gate } 6537c478bd9Sstevel@tonic-gate 654351346dbSahl /*ARGSUSED*/ 655351346dbSahl static void 656351346dbSahl prochandler(struct ps_prochandle *P, const char *msg, void *arg) 6577c478bd9Sstevel@tonic-gate { 6587c478bd9Sstevel@tonic-gate const psinfo_t *prp = Ppsinfo(P); 6597c478bd9Sstevel@tonic-gate int pid = Pstatus(P)->pr_pid; 6607c478bd9Sstevel@tonic-gate char name[SIG2STR_MAX]; 6617c478bd9Sstevel@tonic-gate 662351346dbSahl if (msg != NULL) { 663351346dbSahl notice("pid %d: %s\n", pid, msg); 664351346dbSahl return; 665351346dbSahl } 666351346dbSahl 6677c478bd9Sstevel@tonic-gate switch (Pstate(P)) { 6687c478bd9Sstevel@tonic-gate case PS_UNDEAD: 6697c478bd9Sstevel@tonic-gate /* 6707c478bd9Sstevel@tonic-gate * Ideally we would like to always report pr_wstat here, but it 6717c478bd9Sstevel@tonic-gate * isn't possible given current /proc semantics. If we grabbed 6727c478bd9Sstevel@tonic-gate * the process, Ppsinfo() will either fail or return a zeroed 6737c478bd9Sstevel@tonic-gate * psinfo_t depending on how far the parent is in reaping it. 6747c478bd9Sstevel@tonic-gate * When /proc provides a stable pr_wstat in the status file, 6757c478bd9Sstevel@tonic-gate * this code can be improved by examining this new pr_wstat. 6767c478bd9Sstevel@tonic-gate */ 6777c478bd9Sstevel@tonic-gate if (prp != NULL && WIFSIGNALED(prp->pr_wstat)) { 6787c478bd9Sstevel@tonic-gate notice("pid %d terminated by %s\n", pid, 6797c478bd9Sstevel@tonic-gate proc_signame(WTERMSIG(prp->pr_wstat), 6807c478bd9Sstevel@tonic-gate name, sizeof (name))); 6817c478bd9Sstevel@tonic-gate } else if (prp != NULL && WEXITSTATUS(prp->pr_wstat) != 0) { 6827c478bd9Sstevel@tonic-gate notice("pid %d exited with status %d\n", 6837c478bd9Sstevel@tonic-gate pid, WEXITSTATUS(prp->pr_wstat)); 6847c478bd9Sstevel@tonic-gate } else { 6857c478bd9Sstevel@tonic-gate notice("pid %d has exited\n", pid); 6867c478bd9Sstevel@tonic-gate } 687351346dbSahl g_exited = 1; 688351346dbSahl break; 6897c478bd9Sstevel@tonic-gate 6907c478bd9Sstevel@tonic-gate case PS_LOST: 6917c478bd9Sstevel@tonic-gate notice("pid %d exec'd a set-id or unobservable program\n", pid); 692351346dbSahl g_exited = 1; 693351346dbSahl break; 694351346dbSahl } 6957c478bd9Sstevel@tonic-gate } 6967c478bd9Sstevel@tonic-gate 697351346dbSahl /*ARGSUSED*/ 698351346dbSahl static int 699351346dbSahl chewrec(const dtrace_probedata_t *data, const dtrace_recdesc_t *rec, void *arg) 700351346dbSahl { 701351346dbSahl dtrace_eprobedesc_t *epd = data->dtpda_edesc; 702351346dbSahl dtrace_aggvarid_t aggvars[2]; 703351346dbSahl const void *buf; 704351346dbSahl int i, nagv; 705351346dbSahl 706351346dbSahl /* 707351346dbSahl * A NULL rec indicates that we've processed the last record. 708351346dbSahl */ 709351346dbSahl if (rec == NULL) 710351346dbSahl return (DTRACE_CONSUME_NEXT); 711351346dbSahl 712351346dbSahl buf = data->dtpda_data - rec->dtrd_offset; 713351346dbSahl 714351346dbSahl switch (rec->dtrd_action) { 715351346dbSahl case DTRACEACT_DIFEXPR: 716351346dbSahl (void) printf("\n%s\n\n", (char *)buf + rec->dtrd_offset); 717351346dbSahl if (!g_opt_s) { 718351346dbSahl print_legend(); 719351346dbSahl print_bar(); 720351346dbSahl } 721351346dbSahl return (DTRACE_CONSUME_NEXT); 722351346dbSahl 723351346dbSahl case DTRACEACT_PRINTA: 724351346dbSahl for (nagv = 0, i = 0; i < epd->dtepd_nrecs - 1; i++) { 725351346dbSahl const dtrace_recdesc_t *nrec = &rec[i]; 726351346dbSahl 727351346dbSahl if (nrec->dtrd_uarg != rec->dtrd_uarg) 728351346dbSahl break; 729351346dbSahl 730351346dbSahl /*LINTED - alignment*/ 731351346dbSahl aggvars[nagv++] = *(dtrace_aggvarid_t *)((caddr_t)buf + 732351346dbSahl nrec->dtrd_offset); 733351346dbSahl } 734351346dbSahl 735351346dbSahl if (nagv == (g_opt_s ? 1 : 2)) { 736351346dbSahl uint_t nent = 0; 737351346dbSahl if (dtrace_aggregate_walk_joined(g_dtp, aggvars, nagv, 738351346dbSahl process_aggregate, &nent) != 0) 739351346dbSahl dfatal("failed to walk aggregate"); 740351346dbSahl } 741351346dbSahl 742351346dbSahl return (DTRACE_CONSUME_NEXT); 743351346dbSahl } 744351346dbSahl 745351346dbSahl return (DTRACE_CONSUME_THIS); 7467c478bd9Sstevel@tonic-gate } 7477c478bd9Sstevel@tonic-gate 7487c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 7497c478bd9Sstevel@tonic-gate static void 7507c478bd9Sstevel@tonic-gate intr(int signo) 7517c478bd9Sstevel@tonic-gate { 7527c478bd9Sstevel@tonic-gate g_intr = 1; 7537c478bd9Sstevel@tonic-gate } 7547c478bd9Sstevel@tonic-gate 7557c478bd9Sstevel@tonic-gate int 7567c478bd9Sstevel@tonic-gate main(int argc, char **argv) 7577c478bd9Sstevel@tonic-gate { 7587c478bd9Sstevel@tonic-gate ucred_t *ucp; 7597c478bd9Sstevel@tonic-gate int err; 7607c478bd9Sstevel@tonic-gate int opt_C = 0, opt_H = 0, opt_p = 0, opt_v = 0; 7617c478bd9Sstevel@tonic-gate char c, *p, *end; 7627c478bd9Sstevel@tonic-gate struct sigaction act; 7637c478bd9Sstevel@tonic-gate int done = 0; 7647c478bd9Sstevel@tonic-gate 765351346dbSahl g_pname = basename(argv[0]); 766351346dbSahl argv[0] = g_pname; /* rewrite argv[0] for getopt errors */ 7677c478bd9Sstevel@tonic-gate 7687c478bd9Sstevel@tonic-gate /* 7697c478bd9Sstevel@tonic-gate * Make sure we have the required dtrace_proc privilege. 7707c478bd9Sstevel@tonic-gate */ 7717c478bd9Sstevel@tonic-gate if ((ucp = ucred_get(getpid())) != NULL) { 7727c478bd9Sstevel@tonic-gate const priv_set_t *psp; 7737c478bd9Sstevel@tonic-gate if ((psp = ucred_getprivset(ucp, PRIV_EFFECTIVE)) != NULL && 7747c478bd9Sstevel@tonic-gate !priv_ismember(psp, PRIV_DTRACE_PROC)) { 7757c478bd9Sstevel@tonic-gate fatal("dtrace_proc privilege required\n"); 7767c478bd9Sstevel@tonic-gate } 7777c478bd9Sstevel@tonic-gate 7787c478bd9Sstevel@tonic-gate ucred_free(ucp); 7797c478bd9Sstevel@tonic-gate } 7807c478bd9Sstevel@tonic-gate 7817c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, PLOCKSTAT_OPTSTR)) != EOF) { 7827c478bd9Sstevel@tonic-gate switch (c) { 7837c478bd9Sstevel@tonic-gate case 'n': 7847c478bd9Sstevel@tonic-gate errno = 0; 7857c478bd9Sstevel@tonic-gate g_nent = strtoul(optarg, &end, 10); 7867c478bd9Sstevel@tonic-gate if (*end != '\0' || errno != 0) { 7877c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: invalid count " 7887c478bd9Sstevel@tonic-gate "'%s'\n", g_pname, optarg); 7897c478bd9Sstevel@tonic-gate usage(); 7907c478bd9Sstevel@tonic-gate } 7917c478bd9Sstevel@tonic-gate break; 7927c478bd9Sstevel@tonic-gate 7937c478bd9Sstevel@tonic-gate case 'p': 7947c478bd9Sstevel@tonic-gate opt_p = 1; 7957c478bd9Sstevel@tonic-gate break; 7967c478bd9Sstevel@tonic-gate 7977c478bd9Sstevel@tonic-gate case 'v': 7987c478bd9Sstevel@tonic-gate opt_v = 1; 7997c478bd9Sstevel@tonic-gate break; 8007c478bd9Sstevel@tonic-gate 8017c478bd9Sstevel@tonic-gate case 'A': 8027c478bd9Sstevel@tonic-gate opt_C = opt_H = 1; 8037c478bd9Sstevel@tonic-gate break; 8047c478bd9Sstevel@tonic-gate 8057c478bd9Sstevel@tonic-gate case 'C': 8067c478bd9Sstevel@tonic-gate opt_C = 1; 8077c478bd9Sstevel@tonic-gate break; 8087c478bd9Sstevel@tonic-gate 8097c478bd9Sstevel@tonic-gate case 'H': 8107c478bd9Sstevel@tonic-gate opt_H = 1; 8117c478bd9Sstevel@tonic-gate break; 8127c478bd9Sstevel@tonic-gate 8137c478bd9Sstevel@tonic-gate case 'V': 8147c478bd9Sstevel@tonic-gate g_opt_V = 1; 8157c478bd9Sstevel@tonic-gate break; 8167c478bd9Sstevel@tonic-gate 8177c478bd9Sstevel@tonic-gate default: 8187c478bd9Sstevel@tonic-gate if (strchr(PLOCKSTAT_OPTSTR, c) == NULL) 8197c478bd9Sstevel@tonic-gate usage(); 8207c478bd9Sstevel@tonic-gate } 8217c478bd9Sstevel@tonic-gate } 8227c478bd9Sstevel@tonic-gate 8237c478bd9Sstevel@tonic-gate /* 8247c478bd9Sstevel@tonic-gate * We need a command or at least one pid. 8257c478bd9Sstevel@tonic-gate */ 8267c478bd9Sstevel@tonic-gate if (argc == optind) 8277c478bd9Sstevel@tonic-gate usage(); 8287c478bd9Sstevel@tonic-gate 8297c478bd9Sstevel@tonic-gate if (opt_C == 0 && opt_H == 0) 8307c478bd9Sstevel@tonic-gate opt_C = 1; 8317c478bd9Sstevel@tonic-gate 8327c478bd9Sstevel@tonic-gate if ((g_dtp = dtrace_open(DTRACE_VERSION, 0, &err)) == NULL) 8337c478bd9Sstevel@tonic-gate fatal("failed to initialize dtrace: %s\n", 8347c478bd9Sstevel@tonic-gate dtrace_errmsg(NULL, err)); 8357c478bd9Sstevel@tonic-gate 836351346dbSahl /* 837351346dbSahl * The longest string we trace is 23 bytes long -- so 32 is plenty. 838351346dbSahl */ 839351346dbSahl if (dtrace_setopt(g_dtp, "strsize", "32") == -1) 840351346dbSahl dfatal("failed to set 'strsize'"); 841351346dbSahl 842351346dbSahl /* 843351346dbSahl * 1k should be more than enough for all trace() and printa() actions. 844351346dbSahl */ 845351346dbSahl if (dtrace_setopt(g_dtp, "bufsize", "1k") == -1) 846351346dbSahl dfatal("failed to set 'bufsize'"); 847351346dbSahl 848351346dbSahl /* 849351346dbSahl * The table we produce has the hottest locks at the top. 850351346dbSahl */ 851351346dbSahl if (dtrace_setopt(g_dtp, "aggsortrev", NULL) == -1) 852351346dbSahl dfatal("failed to set 'aggsortrev'"); 853351346dbSahl 854351346dbSahl /* 855351346dbSahl * These are two reasonable defaults which should suffice. 856351346dbSahl */ 8577c478bd9Sstevel@tonic-gate if (dtrace_setopt(g_dtp, "aggsize", "256k") == -1) 8587c478bd9Sstevel@tonic-gate dfatal("failed to set 'aggsize'"); 8597c478bd9Sstevel@tonic-gate if (dtrace_setopt(g_dtp, "aggrate", "1sec") == -1) 8607c478bd9Sstevel@tonic-gate dfatal("failed to set 'aggrate'"); 8617c478bd9Sstevel@tonic-gate 8627c478bd9Sstevel@tonic-gate /* 8637c478bd9Sstevel@tonic-gate * Take a second pass through to look for options that set options now 8647c478bd9Sstevel@tonic-gate * that we have an open dtrace handle. 8657c478bd9Sstevel@tonic-gate */ 8667c478bd9Sstevel@tonic-gate optind = 1; 8677c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, PLOCKSTAT_OPTSTR)) != EOF) { 8687c478bd9Sstevel@tonic-gate switch (c) { 8697c478bd9Sstevel@tonic-gate case 's': 8707c478bd9Sstevel@tonic-gate g_opt_s = 1; 8717c478bd9Sstevel@tonic-gate if (dtrace_setopt(g_dtp, "ustackframes", optarg) == -1) 8727c478bd9Sstevel@tonic-gate dfatal("failed to set 'ustackframes'"); 8737c478bd9Sstevel@tonic-gate break; 8747c478bd9Sstevel@tonic-gate 8757c478bd9Sstevel@tonic-gate case 'x': 8767c478bd9Sstevel@tonic-gate if ((p = strchr(optarg, '=')) != NULL) 8777c478bd9Sstevel@tonic-gate *p++ = '\0'; 8787c478bd9Sstevel@tonic-gate 8797c478bd9Sstevel@tonic-gate if (dtrace_setopt(g_dtp, optarg, p) != 0) 8807c478bd9Sstevel@tonic-gate dfatal("failed to set -x %s", optarg); 8817c478bd9Sstevel@tonic-gate break; 8827c478bd9Sstevel@tonic-gate 8837c478bd9Sstevel@tonic-gate case 'e': 8847c478bd9Sstevel@tonic-gate errno = 0; 8857c478bd9Sstevel@tonic-gate (void) strtoul(optarg, &end, 10); 8867c478bd9Sstevel@tonic-gate if (*optarg == '-' || *end != '\0' || errno != 0) { 8877c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: invalid timeout " 8887c478bd9Sstevel@tonic-gate "'%s'\n", g_pname, optarg); 8897c478bd9Sstevel@tonic-gate usage(); 8907c478bd9Sstevel@tonic-gate } 8917c478bd9Sstevel@tonic-gate 8927c478bd9Sstevel@tonic-gate /* 8937c478bd9Sstevel@tonic-gate * Construct a DTrace enabling that will exit after 8947c478bd9Sstevel@tonic-gate * the specified number of seconds. 8957c478bd9Sstevel@tonic-gate */ 8967c478bd9Sstevel@tonic-gate dprog_add("BEGIN\n{\n\tend = timestamp + "); 8977c478bd9Sstevel@tonic-gate dprog_add(optarg); 8987c478bd9Sstevel@tonic-gate dprog_add(" * 1000000000;\n}\n"); 8997c478bd9Sstevel@tonic-gate dprog_add("tick-10hz\n/timestamp >= end/\n"); 9007c478bd9Sstevel@tonic-gate dprog_add("{\n\texit(0);\n}\n"); 9017c478bd9Sstevel@tonic-gate break; 9027c478bd9Sstevel@tonic-gate } 9037c478bd9Sstevel@tonic-gate } 9047c478bd9Sstevel@tonic-gate 9057c478bd9Sstevel@tonic-gate argc -= optind; 9067c478bd9Sstevel@tonic-gate argv += optind; 9077c478bd9Sstevel@tonic-gate 9087c478bd9Sstevel@tonic-gate if (opt_H) { 9097c478bd9Sstevel@tonic-gate dprog_add(g_hold_init); 9107c478bd9Sstevel@tonic-gate if (g_opt_s == NULL) 9117c478bd9Sstevel@tonic-gate dprog_add(g_hold_times); 9127c478bd9Sstevel@tonic-gate else 9137c478bd9Sstevel@tonic-gate dprog_add(g_hold_histogram); 9147c478bd9Sstevel@tonic-gate } 9157c478bd9Sstevel@tonic-gate 9167c478bd9Sstevel@tonic-gate if (opt_C) { 9177c478bd9Sstevel@tonic-gate dprog_add(g_ctnd_init); 9187c478bd9Sstevel@tonic-gate if (g_opt_s == NULL) 9197c478bd9Sstevel@tonic-gate dprog_add(g_ctnd_times); 9207c478bd9Sstevel@tonic-gate else 9217c478bd9Sstevel@tonic-gate dprog_add(g_ctnd_histogram); 9227c478bd9Sstevel@tonic-gate } 9237c478bd9Sstevel@tonic-gate 9247c478bd9Sstevel@tonic-gate if (opt_p) { 9257c478bd9Sstevel@tonic-gate ulong_t pid; 9267c478bd9Sstevel@tonic-gate 9277c478bd9Sstevel@tonic-gate if (argc > 1) { 9287c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: only one pid is allowed\n", 9297c478bd9Sstevel@tonic-gate g_pname); 9307c478bd9Sstevel@tonic-gate usage(); 9317c478bd9Sstevel@tonic-gate } 9327c478bd9Sstevel@tonic-gate 9337c478bd9Sstevel@tonic-gate errno = 0; 9347c478bd9Sstevel@tonic-gate pid = strtoul(argv[0], &end, 10); 9357c478bd9Sstevel@tonic-gate if (*end != '\0' || errno != 0 || (pid_t)pid != pid) { 9367c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: invalid pid '%s'\n", 9377c478bd9Sstevel@tonic-gate g_pname, argv[0]); 9387c478bd9Sstevel@tonic-gate usage(); 9397c478bd9Sstevel@tonic-gate } 9407c478bd9Sstevel@tonic-gate 9417c478bd9Sstevel@tonic-gate if ((g_pr = dtrace_proc_grab(g_dtp, (pid_t)pid, 0)) == NULL) 9427c478bd9Sstevel@tonic-gate dfatal(NULL); 9437c478bd9Sstevel@tonic-gate } else { 9447c478bd9Sstevel@tonic-gate if ((g_pr = dtrace_proc_create(g_dtp, argv[0], argv)) == NULL) 9457c478bd9Sstevel@tonic-gate dfatal(NULL); 9467c478bd9Sstevel@tonic-gate } 9477c478bd9Sstevel@tonic-gate 9487c478bd9Sstevel@tonic-gate dprog_compile(); 9497c478bd9Sstevel@tonic-gate 950351346dbSahl if (dtrace_handle_proc(g_dtp, &prochandler, NULL) == -1) 951351346dbSahl dfatal("failed to establish proc handler"); 952351346dbSahl 9537c478bd9Sstevel@tonic-gate (void) sigemptyset(&act.sa_mask); 9547c478bd9Sstevel@tonic-gate act.sa_flags = 0; 9557c478bd9Sstevel@tonic-gate act.sa_handler = intr; 9567c478bd9Sstevel@tonic-gate (void) sigaction(SIGINT, &act, NULL); 9577c478bd9Sstevel@tonic-gate (void) sigaction(SIGTERM, &act, NULL); 9587c478bd9Sstevel@tonic-gate 9597c478bd9Sstevel@tonic-gate if (dtrace_go(g_dtp) != 0) 9607c478bd9Sstevel@tonic-gate dfatal("dtrace_go()"); 9617c478bd9Sstevel@tonic-gate 9627c478bd9Sstevel@tonic-gate if (dtrace_getopt(g_dtp, "ustackframes", &g_nframes) != 0) 9637c478bd9Sstevel@tonic-gate dfatal("failed to get 'ustackframes'"); 9647c478bd9Sstevel@tonic-gate 9657c478bd9Sstevel@tonic-gate dtrace_proc_continue(g_dtp, g_pr); 9667c478bd9Sstevel@tonic-gate 9677c478bd9Sstevel@tonic-gate if (opt_v) 9687c478bd9Sstevel@tonic-gate (void) printf("%s: tracing enabled for pid %d\n", g_pname, 9697c478bd9Sstevel@tonic-gate (int)Pstatus(g_pr)->pr_pid); 9707c478bd9Sstevel@tonic-gate 971351346dbSahl do { 972351346dbSahl if (!g_intr && !done) 973351346dbSahl dtrace_sleep(g_dtp); 9747c478bd9Sstevel@tonic-gate 975351346dbSahl if (done || g_intr || g_exited) { 9767c478bd9Sstevel@tonic-gate done = 1; 977351346dbSahl if (dtrace_stop(g_dtp) == -1) 978351346dbSahl dfatal("couldn't stop tracing"); 9797c478bd9Sstevel@tonic-gate } 9807c478bd9Sstevel@tonic-gate 981351346dbSahl switch (dtrace_work(g_dtp, stdout, NULL, chewrec, NULL)) { 982351346dbSahl case DTRACE_WORKSTATUS_DONE: 983351346dbSahl done = 1; 984351346dbSahl break; 985351346dbSahl case DTRACE_WORKSTATUS_OKAY: 986351346dbSahl break; 987351346dbSahl default: 988351346dbSahl dfatal("processing aborted"); 989351346dbSahl } 9907c478bd9Sstevel@tonic-gate 991351346dbSahl } while (!done); 9927c478bd9Sstevel@tonic-gate 9937c478bd9Sstevel@tonic-gate dtrace_close(g_dtp); 9947c478bd9Sstevel@tonic-gate 9957c478bd9Sstevel@tonic-gate return (0); 9967c478bd9Sstevel@tonic-gate } 997