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