1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright (c) 1994, by Sun Microsytems, Inc. 24*0Sstevel@tonic-gate */ 25*0Sstevel@tonic-gate 26*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 27*0Sstevel@tonic-gate 28*0Sstevel@tonic-gate /* 29*0Sstevel@tonic-gate * Includes 30*0Sstevel@tonic-gate */ 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gate #ifndef DEBUG 33*0Sstevel@tonic-gate #define NDEBUG 1 34*0Sstevel@tonic-gate #endif 35*0Sstevel@tonic-gate 36*0Sstevel@tonic-gate #include <assert.h> 37*0Sstevel@tonic-gate #include <limits.h> 38*0Sstevel@tonic-gate #include <values.h> 39*0Sstevel@tonic-gate #include <stdio.h> 40*0Sstevel@tonic-gate #include <string.h> 41*0Sstevel@tonic-gate #include <unistd.h> 42*0Sstevel@tonic-gate #include <stdlib.h> 43*0Sstevel@tonic-gate #include <sys/types.h> 44*0Sstevel@tonic-gate #include <sys/stat.h> 45*0Sstevel@tonic-gate #include <fcntl.h> 46*0Sstevel@tonic-gate #include <dlfcn.h> 47*0Sstevel@tonic-gate #include <sys/mman.h> 48*0Sstevel@tonic-gate #include <sys/param.h> 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate #include <thread.h> 51*0Sstevel@tonic-gate #include <sys/lwp.h> 52*0Sstevel@tonic-gate #include <errno.h> 53*0Sstevel@tonic-gate 54*0Sstevel@tonic-gate #include "tnf_trace.h" 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gate /* 58*0Sstevel@tonic-gate * Defines 59*0Sstevel@tonic-gate */ 60*0Sstevel@tonic-gate #define TNF_FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gate /* 63*0Sstevel@tonic-gate * Declarations 64*0Sstevel@tonic-gate */ 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate extern void thr_probe_setup(void *); 67*0Sstevel@tonic-gate #pragma weak thr_probe_setup 68*0Sstevel@tonic-gate extern int _thr_main(void); 69*0Sstevel@tonic-gate 70*0Sstevel@tonic-gate /* 71*0Sstevel@tonic-gate * Globals 72*0Sstevel@tonic-gate */ 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gate static TNFW_B_CONTROL __tnfw_b_control_local = { 75*0Sstevel@tonic-gate TNFW_B_NOBUFFER, 76*0Sstevel@tonic-gate NULL, 77*0Sstevel@tonic-gate _tnf_trace_initialize, 78*0Sstevel@tonic-gate _tnf_fork_thread_setup, 79*0Sstevel@tonic-gate NULL 80*0Sstevel@tonic-gate }; 81*0Sstevel@tonic-gate 82*0Sstevel@tonic-gate TNFW_B_CONTROL *_tnfw_b_control = &__tnfw_b_control_local; 83*0Sstevel@tonic-gate 84*0Sstevel@tonic-gate static char *file_start; 85*0Sstevel@tonic-gate 86*0Sstevel@tonic-gate /* 87*0Sstevel@tonic-gate * Two Project Private Interfaces between prex and libtnfprobe - 88*0Sstevel@tonic-gate * tnf_trace_file_name and tnf_trace_file_size (three now ...) 89*0Sstevel@tonic-gate */ 90*0Sstevel@tonic-gate char tnf_trace_file_name[MAXPATHLEN] = ""; 91*0Sstevel@tonic-gate uint_t tnf_trace_file_size = 4194304; /* 4 Meg */ 92*0Sstevel@tonic-gate uint_t tnf_trace_file_min = (128 * 1024); 93*0Sstevel@tonic-gate 94*0Sstevel@tonic-gate tnf_ops_t tnf_trace_initial_tpd = { 95*0Sstevel@tonic-gate TNF_ALLOC_REUSABLE, /* mode */ 96*0Sstevel@tonic-gate tnfw_b_alloc, /* alloc */ 97*0Sstevel@tonic-gate tnfw_b_xcommit, /* commit */ 98*0Sstevel@tonic-gate tnfw_b_xabort, /* rollback */ 99*0Sstevel@tonic-gate { 100*0Sstevel@tonic-gate B_FALSE /* tnfw_w_initialized */ 101*0Sstevel@tonic-gate /* rest of struct is null */ 102*0Sstevel@tonic-gate }, 103*0Sstevel@tonic-gate 0 /* busy */ 104*0Sstevel@tonic-gate }; 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate /* 107*0Sstevel@tonic-gate * tnf_process_enable: exported API to turn on tracing for the process 108*0Sstevel@tonic-gate * (on by default). 109*0Sstevel@tonic-gate */ 110*0Sstevel@tonic-gate void 111*0Sstevel@tonic-gate tnf_process_enable(void) 112*0Sstevel@tonic-gate { 113*0Sstevel@tonic-gate TNFW_B_UNSET_STOPPED(_tnfw_b_control->tnf_state); 114*0Sstevel@tonic-gate } 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gate /* 117*0Sstevel@tonic-gate * tnf_process_disable: exported API to turn off tracing for the process. 118*0Sstevel@tonic-gate */ 119*0Sstevel@tonic-gate void 120*0Sstevel@tonic-gate tnf_process_disable(void) 121*0Sstevel@tonic-gate { 122*0Sstevel@tonic-gate TNFW_B_SET_STOPPED(_tnfw_b_control->tnf_state); 123*0Sstevel@tonic-gate } 124*0Sstevel@tonic-gate 125*0Sstevel@tonic-gate /* 126*0Sstevel@tonic-gate * _tnf_trace_initialize 127*0Sstevel@tonic-gate * prex is responsible for creating and zeroing the trace file. So, 128*0Sstevel@tonic-gate * this routine expects the file to be there. It does try to handle 129*0Sstevel@tonic-gate * the case where prex (run as root) for probing a setuid root program 130*0Sstevel@tonic-gate * created the trace file as root. But, by the time the first probe is 131*0Sstevel@tonic-gate * hit (and this function is called), the program has reduced it's 132*0Sstevel@tonic-gate * privilege to its real user id - so the open fails. In this case, 133*0Sstevel@tonic-gate * this function unlinks the trace file and creates it again with its 134*0Sstevel@tonic-gate * current user id. The unlink can fail if the user does not have 135*0Sstevel@tonic-gate * write permission in the directory where the trace file is - if so, 136*0Sstevel@tonic-gate * tracing is set to broken. 137*0Sstevel@tonic-gate */ 138*0Sstevel@tonic-gate int 139*0Sstevel@tonic-gate _tnf_trace_initialize(void) 140*0Sstevel@tonic-gate { 141*0Sstevel@tonic-gate int fd; 142*0Sstevel@tonic-gate int created_file = 0; 143*0Sstevel@tonic-gate static mutex_t init_mutex = DEFAULTMUTEX; 144*0Sstevel@tonic-gate 145*0Sstevel@tonic-gate /* 146*0Sstevel@tonic-gate * if this is a MT program and the primordial thread hasn't been 147*0Sstevel@tonic-gate * setup yet, can't start tracing yet - THREAD_REG hasn't been 148*0Sstevel@tonic-gate * initialized, so we can't call open() in libthread. 149*0Sstevel@tonic-gate */ 150*0Sstevel@tonic-gate 151*0Sstevel@tonic-gate /* 152*0Sstevel@tonic-gate * Use dlsym to check for the present of thr_probe_setup. 153*0Sstevel@tonic-gate */ 154*0Sstevel@tonic-gate 155*0Sstevel@tonic-gate if ((((int(*)())dlsym(RTLD_DEFAULT, "thr_probe_setup")) != NULL) && 156*0Sstevel@tonic-gate (_thr_main() == -1)) { 157*0Sstevel@tonic-gate return (0); 158*0Sstevel@tonic-gate } 159*0Sstevel@tonic-gate 160*0Sstevel@tonic-gate /* 161*0Sstevel@tonic-gate * lock is needed to to prevent multiple threads from 162*0Sstevel@tonic-gate * mmapping the file. 163*0Sstevel@tonic-gate */ 164*0Sstevel@tonic-gate mutex_lock(&init_mutex); 165*0Sstevel@tonic-gate if (_tnfw_b_control->tnf_state != TNFW_B_NOBUFFER) { 166*0Sstevel@tonic-gate mutex_unlock(&init_mutex); 167*0Sstevel@tonic-gate return (1); 168*0Sstevel@tonic-gate } 169*0Sstevel@tonic-gate 170*0Sstevel@tonic-gate _tnfw_b_control->tnf_pid = getpid(); 171*0Sstevel@tonic-gate assert(tnf_trace_file_name[0] != '\0'); 172*0Sstevel@tonic-gate fd = open(tnf_trace_file_name, O_RDWR, TNF_FILE_MODE); 173*0Sstevel@tonic-gate if (fd < 0) { 174*0Sstevel@tonic-gate if (errno == EACCES) { 175*0Sstevel@tonic-gate /* 176*0Sstevel@tonic-gate * fix for bug 1197494: permission denied when 177*0Sstevel@tonic-gate * trying to open the file - happens for setuid root 178*0Sstevel@tonic-gate * programs - prex creates the file with root ownership 179*0Sstevel@tonic-gate */ 180*0Sstevel@tonic-gate if (unlink(tnf_trace_file_name) == -1) { 181*0Sstevel@tonic-gate goto SetBroken; 182*0Sstevel@tonic-gate } 183*0Sstevel@tonic-gate /* try creating it rather than opening it */ 184*0Sstevel@tonic-gate fd = open(tnf_trace_file_name, 185*0Sstevel@tonic-gate O_CREAT | O_RDWR | O_TRUNC, TNF_FILE_MODE); 186*0Sstevel@tonic-gate if (fd < 0) { 187*0Sstevel@tonic-gate goto SetBroken; 188*0Sstevel@tonic-gate } 189*0Sstevel@tonic-gate /* 190*0Sstevel@tonic-gate * expand file to needed size - ftruncate is not 191*0Sstevel@tonic-gate * portable, hence using lseek + write. 192*0Sstevel@tonic-gate */ 193*0Sstevel@tonic-gate if (lseek(fd, tnf_trace_file_size-1, SEEK_SET) == -1) { 194*0Sstevel@tonic-gate goto SetBroken; 195*0Sstevel@tonic-gate } 196*0Sstevel@tonic-gate if (write(fd, "", 1) != 1) { 197*0Sstevel@tonic-gate goto SetBroken; 198*0Sstevel@tonic-gate } 199*0Sstevel@tonic-gate created_file = 1; 200*0Sstevel@tonic-gate } else { 201*0Sstevel@tonic-gate goto SetBroken; 202*0Sstevel@tonic-gate } 203*0Sstevel@tonic-gate } 204*0Sstevel@tonic-gate 205*0Sstevel@tonic-gate /* mmap the file */ 206*0Sstevel@tonic-gate if ((file_start = mmap(0, tnf_trace_file_size, 207*0Sstevel@tonic-gate PROT_READ | PROT_WRITE, MAP_SHARED, 208*0Sstevel@tonic-gate fd, 0)) == (caddr_t) - 1) { 209*0Sstevel@tonic-gate goto SetBroken; 210*0Sstevel@tonic-gate } 211*0Sstevel@tonic-gate if (created_file == 1) { 212*0Sstevel@tonic-gate /* explicitly zero the file XXX - performance problem */ 213*0Sstevel@tonic-gate (void) memset(file_start, 0, tnf_trace_file_size); 214*0Sstevel@tonic-gate } 215*0Sstevel@tonic-gate _tnfw_b_control->tnf_buffer = file_start; 216*0Sstevel@tonic-gate 217*0Sstevel@tonic-gate if (tnfw_b_init_buffer(file_start, tnf_trace_file_size / TNF_BLOCK_SIZE, 218*0Sstevel@tonic-gate TNF_BLOCK_SIZE, B_TRUE) != TNFW_B_OK) { 219*0Sstevel@tonic-gate goto SetBroken; 220*0Sstevel@tonic-gate } 221*0Sstevel@tonic-gate 222*0Sstevel@tonic-gate /* successful return */ 223*0Sstevel@tonic-gate _tnfw_b_control->tnf_state = TNFW_B_RUNNING; 224*0Sstevel@tonic-gate mutex_unlock(&init_mutex); 225*0Sstevel@tonic-gate return (1); 226*0Sstevel@tonic-gate 227*0Sstevel@tonic-gate SetBroken: 228*0Sstevel@tonic-gate _tnfw_b_control->tnf_state = TNFW_B_BROKEN; 229*0Sstevel@tonic-gate mutex_unlock(&init_mutex); 230*0Sstevel@tonic-gate return (0); 231*0Sstevel@tonic-gate 232*0Sstevel@tonic-gate } 233*0Sstevel@tonic-gate 234*0Sstevel@tonic-gate /* 235*0Sstevel@tonic-gate * _tnf_sched_init 236*0Sstevel@tonic-gate */ 237*0Sstevel@tonic-gate 238*0Sstevel@tonic-gate void 239*0Sstevel@tonic-gate _tnf_sched_init(tnf_schedule_t * sched, hrtime_t t) 240*0Sstevel@tonic-gate { 241*0Sstevel@tonic-gate thread_t tid = 0; 242*0Sstevel@tonic-gate 243*0Sstevel@tonic-gate sched->time_base = t; 244*0Sstevel@tonic-gate /* thr_self() is stubbed out by libc for a non-threaded pgm */ 245*0Sstevel@tonic-gate tid = thr_self(); 246*0Sstevel@tonic-gate sched->tid = tid; 247*0Sstevel@tonic-gate sched->lwpid = _lwp_self(); 248*0Sstevel@tonic-gate sched->pid = getpid(); 249*0Sstevel@tonic-gate } 250