xref: /titanic_44/usr/src/cmd/sgs/link_audit/common/truss.c (revision 87308b749adb04ab3f564c765b089962af3edb4d)
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 	if (str = checkenv((const char *)"TRUSS_OUTPUT")) {
124 		FILE	*fp;
125 		char	fname[MAXPATHLEN];
126 
127 		if (pidout)
128 			(void) snprintf(fname, MAXPATHLEN, "%s.%d", str,
129 			    (int)pid);
130 		else
131 			(void) strncpy(fname, str, MAXPATHLEN);
132 
133 		if (fp = fopen(fname, (const char *)"w")) {
134 			outfile = fp;
135 		} else
136 			(void) fprintf(stderr,
137 			    "truss.so: unable to open file=`%s': %s\n",
138 			    fname, strerror(errno));
139 	}
140 
141 	/*
142 	 * Initalize iset to the full set of signals to be masked durring
143 	 * pltenter/pltexit
144 	 */
145 	(void) sigfillset(&iset);
146 
147 	return (LAV_CURRENT);
148 }
149 
150 /* ARGSUSED1 */
151 uint_t
152 la_objopen(Link_map *lmp, Lmid_t lmid, uintptr_t *cookie)
153 {
154 	uint_t	flags;
155 	char	*basename;
156 	static int	first = 1;
157 
158 	if ((bindto_list == NULL) || (trussall))
159 		flags = LA_FLG_BINDTO;
160 	else if (check_list(bindto_list, lmp->l_name))
161 		flags = LA_FLG_BINDTO;
162 	else
163 		flags = 0;
164 
165 	if (((bindfrom_list == NULL) && first) || trussall ||
166 	    (check_list(bindfrom_list, lmp->l_name)))
167 		flags |= LA_FLG_BINDFROM;
168 
169 	first = 0;
170 
171 	if (flags) {
172 		if ((basename = strrchr(lmp->l_name, '/')) != NULL)
173 			basename++;
174 		else
175 			basename = lmp->l_name;
176 		*cookie = (uintptr_t)basename;
177 	}
178 
179 	return (flags);
180 }
181 
182 /* ARGSUSED1 */
183 #if	defined(_LP64)
184 uintptr_t
185 la_symbind64(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcook,
186 	uintptr_t *defcook, uint_t *sb_flags, const char *sym_name)
187 #else
188 uintptr_t
189 la_symbind32(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcook,
190 	uintptr_t *defcook, uint_t *sb_flags)
191 #endif
192 {
193 #if	!defined(_LP64)
194 	const char	*sym_name = (const char *)symp->st_name;
195 #endif
196 
197 
198 	if (noexit)
199 		*sb_flags |= LA_SYMB_NOPLTEXIT;
200 
201 	/*
202 	 * Check to see if this symbol is one of the 'special' symbols.
203 	 * If so we disable PLTEXIT calls for that symbol.
204 	 */
205 	if ((*sb_flags & LA_SYMB_NOPLTEXIT) == 0) {
206 		uint_t	ndx;
207 		char	*str;
208 		/* LINTED */
209 		for (ndx = 0; str = spec_sym[ndx]; ndx++) {
210 			int	cmpval;
211 			cmpval = strcmp(sym_name, str);
212 			if (cmpval < 0)
213 				break;
214 			if (cmpval == 0) {
215 				*sb_flags |= LA_SYMB_NOPLTEXIT;
216 				break;
217 			}
218 		}
219 	}
220 	return (symp->st_value);
221 }
222 
223 /* ARGSUSED1 */
224 #if	defined(__sparcv9)
225 uintptr_t
226 la_sparcv9_pltenter(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie,
227 	uintptr_t *defcookie, La_sparcv9_regs *regset, uint_t *sb_flags,
228 	const char *sym_name)
229 #elif	defined(__sparc)
230 uintptr_t
231 la_sparcv8_pltenter(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcookie,
232 	uintptr_t *defcookie, La_sparcv8_regs *regset, uint_t *sb_flags)
233 #elif   defined(__amd64)
234 uintptr_t
235 la_amd64_pltenter(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie,
236 	uintptr_t *defcookie, La_amd64_regs *regset, uint_t *sb_flags,
237 	const char *sym_name)
238 #elif   defined(__i386)
239 uintptr_t
240 la_i86_pltenter(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcookie,
241 	uintptr_t *defcookie, La_i86_regs *regset, uint_t *sb_flags)
242 #endif
243 {
244 	char		*istr;
245 	char		*defname = (char *)(*defcookie);
246 	char		*refname = (char *)(*refcookie);
247 #if	!defined(_LP64)
248 	const char	*sym_name = (const char *)symp->st_name;
249 #endif
250 	sigset_t	oset;
251 
252 	(void) sigprocmask(SIG_BLOCK, &iset, &oset);
253 
254 	if (pidout)
255 		(void) fprintf(outfile, "%5d:", (int)getpid());
256 
257 	if ((*sb_flags & LA_SYMB_NOPLTEXIT) == 0)
258 		istr = "";
259 	else
260 		istr = "*";
261 
262 	(void) fprintf(outfile, "%-15s -> %15s:%-*s%s(0x%lx, 0x%lx, 0x%lx)\n",
263 		refname, defname, indent_level, istr, sym_name,
264 		(long)GETARG0(regset), (long)GETARG1(regset),
265 		(long)GETARG2(regset));
266 
267 	(void) fflush(outfile);
268 	if (indent && ((*sb_flags & LA_SYMB_NOPLTEXIT) == 0))
269 		indent_level++;
270 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
271 	return (symp->st_value);
272 }
273 
274 /* ARGSUSED1 */
275 #if	defined(_LP64)
276 /* ARGSUSED */
277 uintptr_t
278 la_pltexit64(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie,
279 	uintptr_t *defcookie, uintptr_t retval, const char *sym_name)
280 #else
281 uintptr_t
282 la_pltexit(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcookie,
283 	uintptr_t *defcookie, uintptr_t retval)
284 #endif
285 {
286 	char		*defname = (char *)(*defcookie);
287 	char		*refname = (char *)(*refcookie);
288 	sigset_t	oset;
289 #if	!defined(_LP64)
290 	const char	*sym_name = (const char *)symp->st_name;
291 #endif
292 
293 	(void) sigprocmask(SIG_BLOCK, &iset, &oset);
294 
295 	if (pidout)
296 		(void) fprintf(outfile, "%5d:", (int)pid);
297 	if (indent)
298 		indent_level--;
299 	(void) fprintf(outfile, "%-15s -> %15s:%*s%s - 0x%lx\n", refname,
300 		defname, indent_level, "", sym_name, (ulong_t)retval);
301 	(void) fflush(outfile);
302 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
303 	return (retval);
304 }
305