xref: /onnv-gate/usr/src/cmd/sgs/link_audit/common/truss.c (revision 0:68f95e015346)
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 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <link.h>
30*0Sstevel@tonic-gate #include <sys/types.h>
31*0Sstevel@tonic-gate #include <sys/param.h>
32*0Sstevel@tonic-gate #include <stdio.h>
33*0Sstevel@tonic-gate #include <stdlib.h>
34*0Sstevel@tonic-gate #include <unistd.h>
35*0Sstevel@tonic-gate #include <string.h>
36*0Sstevel@tonic-gate #include <errno.h>
37*0Sstevel@tonic-gate #include <signal.h>
38*0Sstevel@tonic-gate #include "env.h"
39*0Sstevel@tonic-gate #include "mach.h"
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate static Elist		*bindto_list = 0;
42*0Sstevel@tonic-gate static Elist		*bindfrom_list = 0;
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate static uint_t		pidout = 0;
45*0Sstevel@tonic-gate static pid_t		pid;
46*0Sstevel@tonic-gate static FILE		*outfile = stderr;
47*0Sstevel@tonic-gate static uint_t		indent = 1;
48*0Sstevel@tonic-gate static uint_t		indent_level = 1;
49*0Sstevel@tonic-gate static uint_t		trussall = 0;
50*0Sstevel@tonic-gate static uint_t		noexit = 0;
51*0Sstevel@tonic-gate static sigset_t		iset;
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate /*
55*0Sstevel@tonic-gate  * It's not possible to gather the return code on routines
56*0Sstevel@tonic-gate  * which actually have a dependence on the 'stack frame structure'.
57*0Sstevel@tonic-gate  * Below is a list of known symbols which have this dependency,
58*0Sstevel@tonic-gate  * truss.so will disable the la_pltexit() entry point for these
59*0Sstevel@tonic-gate  * routines, which will remove the requirement for the extra
60*0Sstevel@tonic-gate  * stackframe that the link_auditing interface creates.
61*0Sstevel@tonic-gate  *
62*0Sstevel@tonic-gate  * NOTE: this list *must* be mainted in alphabetical order.
63*0Sstevel@tonic-gate  *	 if this list ever became to long a faster search mechanism
64*0Sstevel@tonic-gate  *	 should be considered.
65*0Sstevel@tonic-gate  */
66*0Sstevel@tonic-gate static char	*spec_sym[] = {
67*0Sstevel@tonic-gate #if defined(sparc)
68*0Sstevel@tonic-gate 	".stret1",
69*0Sstevel@tonic-gate 	".stret2",
70*0Sstevel@tonic-gate 	".stret4",
71*0Sstevel@tonic-gate 	".stret8",
72*0Sstevel@tonic-gate #endif
73*0Sstevel@tonic-gate 	"__getcontext",
74*0Sstevel@tonic-gate 	"_getcontext",
75*0Sstevel@tonic-gate 	"_getsp",
76*0Sstevel@tonic-gate 	"_longjmp",
77*0Sstevel@tonic-gate 	"_setcontext",
78*0Sstevel@tonic-gate 	"_setjmp",
79*0Sstevel@tonic-gate 	"_siglongjmp",
80*0Sstevel@tonic-gate 	"_sigsetjmp",
81*0Sstevel@tonic-gate 	"_vfork",
82*0Sstevel@tonic-gate 	"getcontext",
83*0Sstevel@tonic-gate 	"getsp",
84*0Sstevel@tonic-gate 	"longjmp",
85*0Sstevel@tonic-gate 	"setcontext",
86*0Sstevel@tonic-gate 	"setjmp",
87*0Sstevel@tonic-gate 	"siglongjmp",
88*0Sstevel@tonic-gate 	"sigsetjmp",
89*0Sstevel@tonic-gate 	"vfork",
90*0Sstevel@tonic-gate 	(char *)0
91*0Sstevel@tonic-gate };
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate uint_t
95*0Sstevel@tonic-gate la_version(uint_t version)
96*0Sstevel@tonic-gate {
97*0Sstevel@tonic-gate 	char	*str;
98*0Sstevel@tonic-gate 	if (version > LAV_CURRENT)
99*0Sstevel@tonic-gate 		(void) fprintf(stderr, "truss.so: unexpected version: %d\n",
100*0Sstevel@tonic-gate 			version);
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	build_env_list(&bindto_list, (const char *)"TRUSS_BINDTO");
103*0Sstevel@tonic-gate 	build_env_list(&bindfrom_list, (const char *)"TRUSS_BINDFROM");
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 	if (checkenv((const char *)"TRUSS_PID")) {
106*0Sstevel@tonic-gate 		pidout = 1;
107*0Sstevel@tonic-gate 		pid = getpid();
108*0Sstevel@tonic-gate 	} else {
109*0Sstevel@tonic-gate 		char	*str = "LD_AUDIT=";
110*0Sstevel@tonic-gate 		/*
111*0Sstevel@tonic-gate 		 * This disables truss output in subsequent fork()/exec
112*0Sstevel@tonic-gate 		 * processes.
113*0Sstevel@tonic-gate 		 */
114*0Sstevel@tonic-gate 		(void) putenv(str);
115*0Sstevel@tonic-gate 	}
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	if (checkenv((const char *)"TRUSS_NOEXIT")) {
118*0Sstevel@tonic-gate 		noexit++;
119*0Sstevel@tonic-gate 		indent = 0;
120*0Sstevel@tonic-gate 	}
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	if (checkenv((const char *)"TRUSS_NOINDENT"))
123*0Sstevel@tonic-gate 		indent = 0;
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 	if (checkenv((const char *)"TRUSS_ALL"))
126*0Sstevel@tonic-gate 		trussall++;
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 	if (str = checkenv((const char *)"TRUSS_OUTPUT")) {
129*0Sstevel@tonic-gate 		FILE	*fp;
130*0Sstevel@tonic-gate 		char	fname[MAXPATHLEN];
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 		if (pidout)
133*0Sstevel@tonic-gate 			(void) snprintf(fname, MAXPATHLEN, "%s.%d", str,
134*0Sstevel@tonic-gate 			    (int)pid);
135*0Sstevel@tonic-gate 		else
136*0Sstevel@tonic-gate 			(void) strncpy(fname, str, MAXPATHLEN);
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 		if (fp = fopen(fname, (const char *)"w")) {
139*0Sstevel@tonic-gate 			outfile = fp;
140*0Sstevel@tonic-gate 		} else
141*0Sstevel@tonic-gate 			(void) fprintf(stderr,
142*0Sstevel@tonic-gate 			    "truss.so: unable to open file=`%s': %s\n",
143*0Sstevel@tonic-gate 			    fname, strerror(errno));
144*0Sstevel@tonic-gate 	}
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 	/*
147*0Sstevel@tonic-gate 	 * Initalize iset to the full set of signals to be masked durring
148*0Sstevel@tonic-gate 	 * pltenter/pltexit
149*0Sstevel@tonic-gate 	 */
150*0Sstevel@tonic-gate 	(void) sigfillset(&iset);
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	return (LAV_CURRENT);
153*0Sstevel@tonic-gate }
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate /* ARGSUSED1 */
157*0Sstevel@tonic-gate uint_t
158*0Sstevel@tonic-gate la_objopen(Link_map *lmp, Lmid_t lmid, uintptr_t *cookie)
159*0Sstevel@tonic-gate {
160*0Sstevel@tonic-gate 	uint_t	flags;
161*0Sstevel@tonic-gate 	char	*basename;
162*0Sstevel@tonic-gate 	static int	first = 1;
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 	if ((bindto_list == 0) || (trussall))
165*0Sstevel@tonic-gate 		flags = LA_FLG_BINDTO;
166*0Sstevel@tonic-gate 	else if (check_list(bindto_list, lmp->l_name))
167*0Sstevel@tonic-gate 		flags = LA_FLG_BINDTO;
168*0Sstevel@tonic-gate 	else
169*0Sstevel@tonic-gate 		flags = 0;
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 	if (((bindfrom_list == 0) && first) || trussall ||
172*0Sstevel@tonic-gate 	    (check_list(bindfrom_list, lmp->l_name)))
173*0Sstevel@tonic-gate 		flags |= LA_FLG_BINDFROM;
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 	first = 0;
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	if (flags) {
178*0Sstevel@tonic-gate 		if ((basename = strrchr(lmp->l_name, '/')) != 0)
179*0Sstevel@tonic-gate 			basename++;
180*0Sstevel@tonic-gate 		else
181*0Sstevel@tonic-gate 			basename = lmp->l_name;
182*0Sstevel@tonic-gate 		*cookie = (uintptr_t)basename;
183*0Sstevel@tonic-gate 	}
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 	return (flags);
186*0Sstevel@tonic-gate }
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate /* ARGSUSED1 */
189*0Sstevel@tonic-gate #if	defined(_LP64)
190*0Sstevel@tonic-gate uintptr_t
191*0Sstevel@tonic-gate la_symbind64(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcook,
192*0Sstevel@tonic-gate 	uintptr_t *defcook, uint_t *sb_flags, const char *sym_name)
193*0Sstevel@tonic-gate #else
194*0Sstevel@tonic-gate uintptr_t
195*0Sstevel@tonic-gate la_symbind32(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcook,
196*0Sstevel@tonic-gate 	uintptr_t *defcook, uint_t *sb_flags)
197*0Sstevel@tonic-gate #endif
198*0Sstevel@tonic-gate {
199*0Sstevel@tonic-gate #if	!defined(_LP64)
200*0Sstevel@tonic-gate 	const char	*sym_name = (const char *)symp->st_name;
201*0Sstevel@tonic-gate #endif
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 	if (noexit)
205*0Sstevel@tonic-gate 		*sb_flags |= LA_SYMB_NOPLTEXIT;
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate 	/*
208*0Sstevel@tonic-gate 	 * Check to see if this symbol is one of the 'special' symbols.
209*0Sstevel@tonic-gate 	 * If so we disable PLTEXIT calls for that symbol.
210*0Sstevel@tonic-gate 	 */
211*0Sstevel@tonic-gate 	if ((*sb_flags & LA_SYMB_NOPLTEXIT) == 0) {
212*0Sstevel@tonic-gate 		uint_t	ndx;
213*0Sstevel@tonic-gate 		char	*str;
214*0Sstevel@tonic-gate 		/* LINTED */
215*0Sstevel@tonic-gate 		for (ndx = 0; str = spec_sym[ndx]; ndx++) {
216*0Sstevel@tonic-gate 			int	cmpval;
217*0Sstevel@tonic-gate 			cmpval = strcmp(sym_name, str);
218*0Sstevel@tonic-gate 			if (cmpval < 0)
219*0Sstevel@tonic-gate 				break;
220*0Sstevel@tonic-gate 			if (cmpval == 0) {
221*0Sstevel@tonic-gate 				*sb_flags |= LA_SYMB_NOPLTEXIT;
222*0Sstevel@tonic-gate 				break;
223*0Sstevel@tonic-gate 			}
224*0Sstevel@tonic-gate 		}
225*0Sstevel@tonic-gate 	}
226*0Sstevel@tonic-gate 	return (symp->st_value);
227*0Sstevel@tonic-gate }
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate /* ARGSUSED1 */
232*0Sstevel@tonic-gate #if	defined(__sparcv9)
233*0Sstevel@tonic-gate uintptr_t
234*0Sstevel@tonic-gate la_sparcv9_pltenter(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie,
235*0Sstevel@tonic-gate 	uintptr_t *defcookie, La_sparcv9_regs *regset, uint_t *sb_flags,
236*0Sstevel@tonic-gate 	const char *sym_name)
237*0Sstevel@tonic-gate #elif	defined(__sparc)
238*0Sstevel@tonic-gate uintptr_t
239*0Sstevel@tonic-gate la_sparcv8_pltenter(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcookie,
240*0Sstevel@tonic-gate 	uintptr_t *defcookie, La_sparcv8_regs *regset, uint_t *sb_flags)
241*0Sstevel@tonic-gate #elif   defined(__amd64)
242*0Sstevel@tonic-gate uintptr_t
243*0Sstevel@tonic-gate la_amd64_pltenter(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie,
244*0Sstevel@tonic-gate 	uintptr_t *defcookie, La_amd64_regs *regset, uint_t *sb_flags,
245*0Sstevel@tonic-gate 	const char *sym_name)
246*0Sstevel@tonic-gate #elif   defined(__i386)
247*0Sstevel@tonic-gate uintptr_t
248*0Sstevel@tonic-gate la_i86_pltenter(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcookie,
249*0Sstevel@tonic-gate 	uintptr_t *defcookie, La_i86_regs *regset, uint_t *sb_flags)
250*0Sstevel@tonic-gate #endif
251*0Sstevel@tonic-gate {
252*0Sstevel@tonic-gate 	char		*istr;
253*0Sstevel@tonic-gate 	char		*defname = (char *)(*defcookie);
254*0Sstevel@tonic-gate 	char		*refname = (char *)(*refcookie);
255*0Sstevel@tonic-gate #if	!defined(_LP64)
256*0Sstevel@tonic-gate 	const char	*sym_name = (const char *)symp->st_name;
257*0Sstevel@tonic-gate #endif
258*0Sstevel@tonic-gate 	sigset_t	oset;
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &iset, &oset);
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate 	if (pidout)
263*0Sstevel@tonic-gate 		(void) fprintf(outfile, "%5d:", (int)getpid());
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate 	if ((*sb_flags & LA_SYMB_NOPLTEXIT) == 0)
266*0Sstevel@tonic-gate 		istr = "";
267*0Sstevel@tonic-gate 	else
268*0Sstevel@tonic-gate 		istr = "*";
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 	(void) fprintf(outfile, "%-15s -> %15s:%-*s%s(0x%lx, 0x%lx, 0x%lx)\n",
271*0Sstevel@tonic-gate 		refname, defname, indent_level, istr, sym_name,
272*0Sstevel@tonic-gate 		(long)GETARG0(regset), (long)GETARG1(regset),
273*0Sstevel@tonic-gate 		(long)GETARG2(regset));
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate 	(void) fflush(outfile);
276*0Sstevel@tonic-gate 	if (indent && ((*sb_flags & LA_SYMB_NOPLTEXIT) == 0))
277*0Sstevel@tonic-gate 		indent_level++;
278*0Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
279*0Sstevel@tonic-gate 	return (symp->st_value);
280*0Sstevel@tonic-gate }
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate /* ARGSUSED1 */
284*0Sstevel@tonic-gate #if	defined(_LP64)
285*0Sstevel@tonic-gate /* ARGSUSED */
286*0Sstevel@tonic-gate uintptr_t
287*0Sstevel@tonic-gate la_pltexit64(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie,
288*0Sstevel@tonic-gate 	uintptr_t *defcookie, uintptr_t retval, const char *sym_name)
289*0Sstevel@tonic-gate #else
290*0Sstevel@tonic-gate uintptr_t
291*0Sstevel@tonic-gate la_pltexit(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcookie,
292*0Sstevel@tonic-gate 	uintptr_t *defcookie, uintptr_t retval)
293*0Sstevel@tonic-gate #endif
294*0Sstevel@tonic-gate {
295*0Sstevel@tonic-gate 	char		*defname = (char *)(*defcookie);
296*0Sstevel@tonic-gate 	char		*refname = (char *)(*refcookie);
297*0Sstevel@tonic-gate 	sigset_t	oset;
298*0Sstevel@tonic-gate #if	!defined(_LP64)
299*0Sstevel@tonic-gate 	const char	*sym_name = (const char *)symp->st_name;
300*0Sstevel@tonic-gate #endif
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &iset, &oset);
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 	if (pidout)
305*0Sstevel@tonic-gate 		(void) fprintf(outfile, "%5d:", (int)pid);
306*0Sstevel@tonic-gate 	if (indent)
307*0Sstevel@tonic-gate 		indent_level--;
308*0Sstevel@tonic-gate 	(void) fprintf(outfile, "%-15s -> %15s:%*s%s - 0x%lx\n", refname,
309*0Sstevel@tonic-gate 		defname, indent_level, "", sym_name, (ulong_t)retval);
310*0Sstevel@tonic-gate 	(void) fflush(outfile);
311*0Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
312*0Sstevel@tonic-gate 	return (retval);
313*0Sstevel@tonic-gate }
314