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
5*7257d1b4Sraf * Common Development and Distribution License (the "License").
6*7257d1b4Sraf * 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 */
21*7257d1b4Sraf
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24*7257d1b4Sraf * 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 /*
307c478bd9Sstevel@tonic-gate * Includes
317c478bd9Sstevel@tonic-gate */
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate #ifndef DEBUG
347c478bd9Sstevel@tonic-gate #define NDEBUG 1
357c478bd9Sstevel@tonic-gate #endif
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate #include <assert.h>
387c478bd9Sstevel@tonic-gate #include <limits.h>
397c478bd9Sstevel@tonic-gate #include <values.h>
407c478bd9Sstevel@tonic-gate #include <stdio.h>
417c478bd9Sstevel@tonic-gate #include <string.h>
427c478bd9Sstevel@tonic-gate #include <unistd.h>
437c478bd9Sstevel@tonic-gate #include <stdlib.h>
447c478bd9Sstevel@tonic-gate #include <sys/types.h>
457c478bd9Sstevel@tonic-gate #include <sys/stat.h>
467c478bd9Sstevel@tonic-gate #include <fcntl.h>
477c478bd9Sstevel@tonic-gate #include <dlfcn.h>
487c478bd9Sstevel@tonic-gate #include <sys/mman.h>
497c478bd9Sstevel@tonic-gate #include <sys/param.h>
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate #include <thread.h>
527c478bd9Sstevel@tonic-gate #include <sys/lwp.h>
537c478bd9Sstevel@tonic-gate #include <errno.h>
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate #include "tnf_trace.h"
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate * Defines
607c478bd9Sstevel@tonic-gate */
617c478bd9Sstevel@tonic-gate #define TNF_FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate * Declarations
657c478bd9Sstevel@tonic-gate */
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate extern void thr_probe_setup(void *);
687c478bd9Sstevel@tonic-gate #pragma weak thr_probe_setup
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate * Globals
727c478bd9Sstevel@tonic-gate */
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate static TNFW_B_CONTROL __tnfw_b_control_local = {
757c478bd9Sstevel@tonic-gate TNFW_B_NOBUFFER,
767c478bd9Sstevel@tonic-gate NULL,
777c478bd9Sstevel@tonic-gate _tnf_trace_initialize,
787c478bd9Sstevel@tonic-gate _tnf_fork_thread_setup,
797c478bd9Sstevel@tonic-gate NULL
807c478bd9Sstevel@tonic-gate };
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate TNFW_B_CONTROL *_tnfw_b_control = &__tnfw_b_control_local;
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate static char *file_start;
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate * Two Project Private Interfaces between prex and libtnfprobe -
887c478bd9Sstevel@tonic-gate * tnf_trace_file_name and tnf_trace_file_size (three now ...)
897c478bd9Sstevel@tonic-gate */
907c478bd9Sstevel@tonic-gate char tnf_trace_file_name[MAXPATHLEN] = "";
917c478bd9Sstevel@tonic-gate uint_t tnf_trace_file_size = 4194304; /* 4 Meg */
927c478bd9Sstevel@tonic-gate uint_t tnf_trace_file_min = (128 * 1024);
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate tnf_ops_t tnf_trace_initial_tpd = {
957c478bd9Sstevel@tonic-gate TNF_ALLOC_REUSABLE, /* mode */
967c478bd9Sstevel@tonic-gate tnfw_b_alloc, /* alloc */
977c478bd9Sstevel@tonic-gate tnfw_b_xcommit, /* commit */
987c478bd9Sstevel@tonic-gate tnfw_b_xabort, /* rollback */
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate B_FALSE /* tnfw_w_initialized */
1017c478bd9Sstevel@tonic-gate /* rest of struct is null */
1027c478bd9Sstevel@tonic-gate },
1037c478bd9Sstevel@tonic-gate 0 /* busy */
1047c478bd9Sstevel@tonic-gate };
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate * tnf_process_enable: exported API to turn on tracing for the process
1087c478bd9Sstevel@tonic-gate * (on by default).
1097c478bd9Sstevel@tonic-gate */
1107c478bd9Sstevel@tonic-gate void
tnf_process_enable(void)1117c478bd9Sstevel@tonic-gate tnf_process_enable(void)
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate TNFW_B_UNSET_STOPPED(_tnfw_b_control->tnf_state);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate * tnf_process_disable: exported API to turn off tracing for the process.
1187c478bd9Sstevel@tonic-gate */
1197c478bd9Sstevel@tonic-gate void
tnf_process_disable(void)1207c478bd9Sstevel@tonic-gate tnf_process_disable(void)
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate TNFW_B_SET_STOPPED(_tnfw_b_control->tnf_state);
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate /*
1267c478bd9Sstevel@tonic-gate * _tnf_trace_initialize
1277c478bd9Sstevel@tonic-gate * prex is responsible for creating and zeroing the trace file. So,
1287c478bd9Sstevel@tonic-gate * this routine expects the file to be there. It does try to handle
1297c478bd9Sstevel@tonic-gate * the case where prex (run as root) for probing a setuid root program
1307c478bd9Sstevel@tonic-gate * created the trace file as root. But, by the time the first probe is
1317c478bd9Sstevel@tonic-gate * hit (and this function is called), the program has reduced it's
1327c478bd9Sstevel@tonic-gate * privilege to its real user id - so the open fails. In this case,
1337c478bd9Sstevel@tonic-gate * this function unlinks the trace file and creates it again with its
1347c478bd9Sstevel@tonic-gate * current user id. The unlink can fail if the user does not have
1357c478bd9Sstevel@tonic-gate * write permission in the directory where the trace file is - if so,
1367c478bd9Sstevel@tonic-gate * tracing is set to broken.
1377c478bd9Sstevel@tonic-gate */
1387c478bd9Sstevel@tonic-gate int
_tnf_trace_initialize(void)1397c478bd9Sstevel@tonic-gate _tnf_trace_initialize(void)
1407c478bd9Sstevel@tonic-gate {
1417c478bd9Sstevel@tonic-gate int fd;
1427c478bd9Sstevel@tonic-gate int created_file = 0;
1437c478bd9Sstevel@tonic-gate static mutex_t init_mutex = DEFAULTMUTEX;
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate /*
1467c478bd9Sstevel@tonic-gate * if this is a MT program and the primordial thread hasn't been
1477c478bd9Sstevel@tonic-gate * setup yet, can't start tracing yet - THREAD_REG hasn't been
1487c478bd9Sstevel@tonic-gate * initialized, so we can't call open() in libthread.
1497c478bd9Sstevel@tonic-gate */
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate /*
1527c478bd9Sstevel@tonic-gate * Use dlsym to check for the present of thr_probe_setup.
1537c478bd9Sstevel@tonic-gate */
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate if ((((int(*)())dlsym(RTLD_DEFAULT, "thr_probe_setup")) != NULL) &&
156*7257d1b4Sraf (thr_main() == -1)) {
1577c478bd9Sstevel@tonic-gate return (0);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate * lock is needed to to prevent multiple threads from
1627c478bd9Sstevel@tonic-gate * mmapping the file.
1637c478bd9Sstevel@tonic-gate */
1647c478bd9Sstevel@tonic-gate mutex_lock(&init_mutex);
1657c478bd9Sstevel@tonic-gate if (_tnfw_b_control->tnf_state != TNFW_B_NOBUFFER) {
1667c478bd9Sstevel@tonic-gate mutex_unlock(&init_mutex);
1677c478bd9Sstevel@tonic-gate return (1);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate _tnfw_b_control->tnf_pid = getpid();
1717c478bd9Sstevel@tonic-gate assert(tnf_trace_file_name[0] != '\0');
1727c478bd9Sstevel@tonic-gate fd = open(tnf_trace_file_name, O_RDWR, TNF_FILE_MODE);
1737c478bd9Sstevel@tonic-gate if (fd < 0) {
1747c478bd9Sstevel@tonic-gate if (errno == EACCES) {
1757c478bd9Sstevel@tonic-gate /*
1767c478bd9Sstevel@tonic-gate * fix for bug 1197494: permission denied when
1777c478bd9Sstevel@tonic-gate * trying to open the file - happens for setuid root
1787c478bd9Sstevel@tonic-gate * programs - prex creates the file with root ownership
1797c478bd9Sstevel@tonic-gate */
1807c478bd9Sstevel@tonic-gate if (unlink(tnf_trace_file_name) == -1) {
1817c478bd9Sstevel@tonic-gate goto SetBroken;
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate /* try creating it rather than opening it */
1847c478bd9Sstevel@tonic-gate fd = open(tnf_trace_file_name,
1857c478bd9Sstevel@tonic-gate O_CREAT | O_RDWR | O_TRUNC, TNF_FILE_MODE);
1867c478bd9Sstevel@tonic-gate if (fd < 0) {
1877c478bd9Sstevel@tonic-gate goto SetBroken;
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate /*
1907c478bd9Sstevel@tonic-gate * expand file to needed size - ftruncate is not
1917c478bd9Sstevel@tonic-gate * portable, hence using lseek + write.
1927c478bd9Sstevel@tonic-gate */
1937c478bd9Sstevel@tonic-gate if (lseek(fd, tnf_trace_file_size-1, SEEK_SET) == -1) {
1947c478bd9Sstevel@tonic-gate goto SetBroken;
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate if (write(fd, "", 1) != 1) {
1977c478bd9Sstevel@tonic-gate goto SetBroken;
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate created_file = 1;
2007c478bd9Sstevel@tonic-gate } else {
2017c478bd9Sstevel@tonic-gate goto SetBroken;
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate
2057c478bd9Sstevel@tonic-gate /* mmap the file */
2067c478bd9Sstevel@tonic-gate if ((file_start = mmap(0, tnf_trace_file_size,
207*7257d1b4Sraf PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == (caddr_t)-1) {
2087c478bd9Sstevel@tonic-gate goto SetBroken;
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate if (created_file == 1) {
2117c478bd9Sstevel@tonic-gate /* explicitly zero the file XXX - performance problem */
2127c478bd9Sstevel@tonic-gate (void) memset(file_start, 0, tnf_trace_file_size);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate _tnfw_b_control->tnf_buffer = file_start;
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate if (tnfw_b_init_buffer(file_start, tnf_trace_file_size / TNF_BLOCK_SIZE,
2177c478bd9Sstevel@tonic-gate TNF_BLOCK_SIZE, B_TRUE) != TNFW_B_OK) {
2187c478bd9Sstevel@tonic-gate goto SetBroken;
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate /* successful return */
2227c478bd9Sstevel@tonic-gate _tnfw_b_control->tnf_state = TNFW_B_RUNNING;
2237c478bd9Sstevel@tonic-gate mutex_unlock(&init_mutex);
2247c478bd9Sstevel@tonic-gate return (1);
2257c478bd9Sstevel@tonic-gate
2267c478bd9Sstevel@tonic-gate SetBroken:
2277c478bd9Sstevel@tonic-gate _tnfw_b_control->tnf_state = TNFW_B_BROKEN;
2287c478bd9Sstevel@tonic-gate mutex_unlock(&init_mutex);
2297c478bd9Sstevel@tonic-gate return (0);
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate /*
2347c478bd9Sstevel@tonic-gate * _tnf_sched_init
2357c478bd9Sstevel@tonic-gate */
2367c478bd9Sstevel@tonic-gate
2377c478bd9Sstevel@tonic-gate void
_tnf_sched_init(tnf_schedule_t * sched,hrtime_t t)2387c478bd9Sstevel@tonic-gate _tnf_sched_init(tnf_schedule_t *sched, hrtime_t t)
2397c478bd9Sstevel@tonic-gate {
2407c478bd9Sstevel@tonic-gate thread_t tid = 0;
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gate sched->time_base = t;
2437c478bd9Sstevel@tonic-gate /* thr_self() is stubbed out by libc for a non-threaded pgm */
2447c478bd9Sstevel@tonic-gate tid = thr_self();
2457c478bd9Sstevel@tonic-gate sched->tid = tid;
2467c478bd9Sstevel@tonic-gate sched->lwpid = _lwp_self();
2477c478bd9Sstevel@tonic-gate sched->pid = getpid();
2487c478bd9Sstevel@tonic-gate }
249