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