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
502ca3e02Srie  * Common Development and Distribution License (the "License").
602ca3e02Srie  * 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  */
2120c1c355SRod Evans 
227c478bd9Sstevel@tonic-gate /*
2320c1c355SRod Evans  * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate #include <link.h>
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/param.h>
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <unistd.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #include <signal.h>
347c478bd9Sstevel@tonic-gate #include "env.h"
357c478bd9Sstevel@tonic-gate #include "mach.h"
367c478bd9Sstevel@tonic-gate 
3720c1c355SRod Evans static Elist		*bindto_list = NULL;
3820c1c355SRod Evans static Elist		*bindfrom_list = NULL;
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate static uint_t		pidout = 0;
417c478bd9Sstevel@tonic-gate static pid_t		pid;
427c478bd9Sstevel@tonic-gate static FILE		*outfile = stderr;
437c478bd9Sstevel@tonic-gate static uint_t		indent = 1;
447c478bd9Sstevel@tonic-gate static uint_t		indent_level = 1;
457c478bd9Sstevel@tonic-gate static uint_t		trussall = 0;
467c478bd9Sstevel@tonic-gate static uint_t		noexit = 0;
477c478bd9Sstevel@tonic-gate static sigset_t		iset;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * It's not possible to gather the return code on routines
517c478bd9Sstevel@tonic-gate  * which actually have a dependence on the 'stack frame structure'.
527c478bd9Sstevel@tonic-gate  * Below is a list of known symbols which have this dependency,
537c478bd9Sstevel@tonic-gate  * truss.so will disable the la_pltexit() entry point for these
547c478bd9Sstevel@tonic-gate  * routines, which will remove the requirement for the extra
557c478bd9Sstevel@tonic-gate  * stackframe that the link_auditing interface creates.
567c478bd9Sstevel@tonic-gate  *
577c478bd9Sstevel@tonic-gate  * NOTE: this list *must* be mainted in alphabetical order.
587c478bd9Sstevel@tonic-gate  *	 if this list ever became to long a faster search mechanism
597c478bd9Sstevel@tonic-gate  *	 should be considered.
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate static char	*spec_sym[] = {
6202ca3e02Srie #if	defined(__sparc)
637c478bd9Sstevel@tonic-gate 	".stret1",
647c478bd9Sstevel@tonic-gate 	".stret2",
657c478bd9Sstevel@tonic-gate 	".stret4",
667c478bd9Sstevel@tonic-gate 	".stret8",
677c478bd9Sstevel@tonic-gate #endif
687c478bd9Sstevel@tonic-gate 	"__getcontext",
697c478bd9Sstevel@tonic-gate 	"_getcontext",
707c478bd9Sstevel@tonic-gate 	"_getsp",
717c478bd9Sstevel@tonic-gate 	"_longjmp",
727c478bd9Sstevel@tonic-gate 	"_setcontext",
737c478bd9Sstevel@tonic-gate 	"_setjmp",
747c478bd9Sstevel@tonic-gate 	"_siglongjmp",
757c478bd9Sstevel@tonic-gate 	"_sigsetjmp",
767c478bd9Sstevel@tonic-gate 	"_vfork",
777c478bd9Sstevel@tonic-gate 	"getcontext",
787c478bd9Sstevel@tonic-gate 	"getsp",
797c478bd9Sstevel@tonic-gate 	"longjmp",
807c478bd9Sstevel@tonic-gate 	"setcontext",
817c478bd9Sstevel@tonic-gate 	"setjmp",
827c478bd9Sstevel@tonic-gate 	"siglongjmp",
837c478bd9Sstevel@tonic-gate 	"sigsetjmp",
847c478bd9Sstevel@tonic-gate 	"vfork",
857c478bd9Sstevel@tonic-gate 	(char *)0
867c478bd9Sstevel@tonic-gate };
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate uint_t
la_version(uint_t version)897c478bd9Sstevel@tonic-gate la_version(uint_t version)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	char	*str;
9220c1c355SRod Evans 
937c478bd9Sstevel@tonic-gate 	if (version > LAV_CURRENT)
947c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "truss.so: unexpected version: %d\n",
9520c1c355SRod Evans 		    version);
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	build_env_list(&bindto_list, (const char *)"TRUSS_BINDTO");
987c478bd9Sstevel@tonic-gate 	build_env_list(&bindfrom_list, (const char *)"TRUSS_BINDFROM");
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	if (checkenv((const char *)"TRUSS_PID")) {
1017c478bd9Sstevel@tonic-gate 		pidout = 1;
1027c478bd9Sstevel@tonic-gate 		pid = getpid();
1037c478bd9Sstevel@tonic-gate 	} else {
1047c478bd9Sstevel@tonic-gate 		char	*str = "LD_AUDIT=";
1057c478bd9Sstevel@tonic-gate 		/*
1067c478bd9Sstevel@tonic-gate 		 * This disables truss output in subsequent fork()/exec
1077c478bd9Sstevel@tonic-gate 		 * processes.
1087c478bd9Sstevel@tonic-gate 		 */
1097c478bd9Sstevel@tonic-gate 		(void) putenv(str);
1107c478bd9Sstevel@tonic-gate 	}
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	if (checkenv((const char *)"TRUSS_NOEXIT")) {
1137c478bd9Sstevel@tonic-gate 		noexit++;
1147c478bd9Sstevel@tonic-gate 		indent = 0;
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if (checkenv((const char *)"TRUSS_NOINDENT"))
1187c478bd9Sstevel@tonic-gate 		indent = 0;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	if (checkenv((const char *)"TRUSS_ALL"))
1217c478bd9Sstevel@tonic-gate 		trussall++;
1227c478bd9Sstevel@tonic-gate 
123*78f67f4eSToomas Soome 	str = checkenv((const char *)"TRUSS_OUTPUT");
124*78f67f4eSToomas Soome 	if (str != NULL) {
1257c478bd9Sstevel@tonic-gate 		FILE	*fp;
1267c478bd9Sstevel@tonic-gate 		char	fname[MAXPATHLEN];
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 		if (pidout)
1297c478bd9Sstevel@tonic-gate 			(void) snprintf(fname, MAXPATHLEN, "%s.%d", str,
1307c478bd9Sstevel@tonic-gate 			    (int)pid);
1317c478bd9Sstevel@tonic-gate 		else
1327c478bd9Sstevel@tonic-gate 			(void) strncpy(fname, str, MAXPATHLEN);
1337c478bd9Sstevel@tonic-gate 
134*78f67f4eSToomas Soome 		fp = fopen(fname, (const char *)"w");
135*78f67f4eSToomas Soome 		if (fp != NULL) {
1367c478bd9Sstevel@tonic-gate 			outfile = fp;
137*78f67f4eSToomas Soome 		} else {
1387c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1397c478bd9Sstevel@tonic-gate 			    "truss.so: unable to open file=`%s': %s\n",
1407c478bd9Sstevel@tonic-gate 			    fname, strerror(errno));
141*78f67f4eSToomas Soome 		}
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	/*
1457c478bd9Sstevel@tonic-gate 	 * Initalize iset to the full set of signals to be masked durring
1467c478bd9Sstevel@tonic-gate 	 * pltenter/pltexit
1477c478bd9Sstevel@tonic-gate 	 */
1487c478bd9Sstevel@tonic-gate 	(void) sigfillset(&iset);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	return (LAV_CURRENT);
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /* ARGSUSED1 */
1547c478bd9Sstevel@tonic-gate uint_t
la_objopen(Link_map * lmp,Lmid_t lmid,uintptr_t * cookie)1557c478bd9Sstevel@tonic-gate la_objopen(Link_map *lmp, Lmid_t lmid, uintptr_t *cookie)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate 	uint_t	flags;
1587c478bd9Sstevel@tonic-gate 	char	*basename;
1597c478bd9Sstevel@tonic-gate 	static int	first = 1;
1607c478bd9Sstevel@tonic-gate 
16120c1c355SRod Evans 	if ((bindto_list == NULL) || (trussall))
1627c478bd9Sstevel@tonic-gate 		flags = LA_FLG_BINDTO;
1637c478bd9Sstevel@tonic-gate 	else if (check_list(bindto_list, lmp->l_name))
1647c478bd9Sstevel@tonic-gate 		flags = LA_FLG_BINDTO;
1657c478bd9Sstevel@tonic-gate 	else
1667c478bd9Sstevel@tonic-gate 		flags = 0;
1677c478bd9Sstevel@tonic-gate 
16820c1c355SRod Evans 	if (((bindfrom_list == NULL) && first) || trussall ||
1697c478bd9Sstevel@tonic-gate 	    (check_list(bindfrom_list, lmp->l_name)))
1707c478bd9Sstevel@tonic-gate 		flags |= LA_FLG_BINDFROM;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	first = 0;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	if (flags) {
17520c1c355SRod Evans 		if ((basename = strrchr(lmp->l_name, '/')) != NULL)
1767c478bd9Sstevel@tonic-gate 			basename++;
1777c478bd9Sstevel@tonic-gate 		else
1787c478bd9Sstevel@tonic-gate 			basename = lmp->l_name;
1797c478bd9Sstevel@tonic-gate 		*cookie = (uintptr_t)basename;
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	return (flags);
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate /* ARGSUSED1 */
1867c478bd9Sstevel@tonic-gate #if	defined(_LP64)
1877c478bd9Sstevel@tonic-gate uintptr_t
la_symbind64(Elf64_Sym * symp,uint_t symndx,uintptr_t * refcook,uintptr_t * defcook,uint_t * sb_flags,const char * sym_name)1887c478bd9Sstevel@tonic-gate la_symbind64(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcook,
189*78f67f4eSToomas Soome     uintptr_t *defcook, uint_t *sb_flags, const char *sym_name)
1907c478bd9Sstevel@tonic-gate #else
1917c478bd9Sstevel@tonic-gate uintptr_t
1927c478bd9Sstevel@tonic-gate la_symbind32(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcook,
193*78f67f4eSToomas Soome     uintptr_t *defcook, uint_t *sb_flags)
1947c478bd9Sstevel@tonic-gate #endif
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate #if	!defined(_LP64)
1977c478bd9Sstevel@tonic-gate 	const char	*sym_name = (const char *)symp->st_name;
1987c478bd9Sstevel@tonic-gate #endif
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (noexit)
2027c478bd9Sstevel@tonic-gate 		*sb_flags |= LA_SYMB_NOPLTEXIT;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	/*
2057c478bd9Sstevel@tonic-gate 	 * Check to see if this symbol is one of the 'special' symbols.
2067c478bd9Sstevel@tonic-gate 	 * If so we disable PLTEXIT calls for that symbol.
2077c478bd9Sstevel@tonic-gate 	 */
2087c478bd9Sstevel@tonic-gate 	if ((*sb_flags & LA_SYMB_NOPLTEXIT) == 0) {
2097c478bd9Sstevel@tonic-gate 		uint_t	ndx;
2107c478bd9Sstevel@tonic-gate 		char	*str;
211*78f67f4eSToomas Soome 		for (ndx = 0; (str = spec_sym[ndx]) != NULL; ndx++) {
2127c478bd9Sstevel@tonic-gate 			int	cmpval;
2137c478bd9Sstevel@tonic-gate 			cmpval = strcmp(sym_name, str);
2147c478bd9Sstevel@tonic-gate 			if (cmpval < 0)
2157c478bd9Sstevel@tonic-gate 				break;
2167c478bd9Sstevel@tonic-gate 			if (cmpval == 0) {
2177c478bd9Sstevel@tonic-gate 				*sb_flags |= LA_SYMB_NOPLTEXIT;
2187c478bd9Sstevel@tonic-gate 				break;
2197c478bd9Sstevel@tonic-gate 			}
2207c478bd9Sstevel@tonic-gate 		}
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate 	return (symp->st_value);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /* ARGSUSED1 */
2267c478bd9Sstevel@tonic-gate #if	defined(__sparcv9)
2277c478bd9Sstevel@tonic-gate uintptr_t
la_sparcv9_pltenter(Elf64_Sym * symp,uint_t symndx,uintptr_t * refcookie,uintptr_t * defcookie,La_sparcv9_regs * regset,uint_t * sb_flags,const char * sym_name)2287c478bd9Sstevel@tonic-gate la_sparcv9_pltenter(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie,
229*78f67f4eSToomas Soome     uintptr_t *defcookie, La_sparcv9_regs *regset, uint_t *sb_flags,
230*78f67f4eSToomas Soome     const char *sym_name)
2317c478bd9Sstevel@tonic-gate #elif	defined(__sparc)
2327c478bd9Sstevel@tonic-gate uintptr_t
2337c478bd9Sstevel@tonic-gate la_sparcv8_pltenter(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcookie,
234*78f67f4eSToomas Soome     uintptr_t *defcookie, La_sparcv8_regs *regset, uint_t *sb_flags)
2357c478bd9Sstevel@tonic-gate #elif   defined(__amd64)
2367c478bd9Sstevel@tonic-gate uintptr_t
2377c478bd9Sstevel@tonic-gate la_amd64_pltenter(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie,
238*78f67f4eSToomas Soome     uintptr_t *defcookie, La_amd64_regs *regset, uint_t *sb_flags,
239*78f67f4eSToomas Soome     const char *sym_name)
2407c478bd9Sstevel@tonic-gate #elif   defined(__i386)
2417c478bd9Sstevel@tonic-gate uintptr_t
2427c478bd9Sstevel@tonic-gate la_i86_pltenter(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcookie,
243*78f67f4eSToomas Soome     uintptr_t *defcookie, La_i86_regs *regset, uint_t *sb_flags)
2447c478bd9Sstevel@tonic-gate #endif
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate 	char		*istr;
2477c478bd9Sstevel@tonic-gate 	char		*defname = (char *)(*defcookie);
2487c478bd9Sstevel@tonic-gate 	char		*refname = (char *)(*refcookie);
2497c478bd9Sstevel@tonic-gate #if	!defined(_LP64)
2507c478bd9Sstevel@tonic-gate 	const char	*sym_name = (const char *)symp->st_name;
2517c478bd9Sstevel@tonic-gate #endif
2527c478bd9Sstevel@tonic-gate 	sigset_t	oset;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &iset, &oset);
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	if (pidout)
2577c478bd9Sstevel@tonic-gate 		(void) fprintf(outfile, "%5d:", (int)getpid());
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	if ((*sb_flags & LA_SYMB_NOPLTEXIT) == 0)
2607c478bd9Sstevel@tonic-gate 		istr = "";
2617c478bd9Sstevel@tonic-gate 	else
2627c478bd9Sstevel@tonic-gate 		istr = "*";
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	(void) fprintf(outfile, "%-15s -> %15s:%-*s%s(0x%lx, 0x%lx, 0x%lx)\n",
265*78f67f4eSToomas Soome 	    refname, defname, indent_level, istr, sym_name,
266*78f67f4eSToomas Soome 	    (long)GETARG0(regset), (long)GETARG1(regset),
267*78f67f4eSToomas Soome 	    (long)GETARG2(regset));
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	(void) fflush(outfile);
2707c478bd9Sstevel@tonic-gate 	if (indent && ((*sb_flags & LA_SYMB_NOPLTEXIT) == 0))
2717c478bd9Sstevel@tonic-gate 		indent_level++;
2727c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
2737c478bd9Sstevel@tonic-gate 	return (symp->st_value);
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate /* ARGSUSED1 */
2777c478bd9Sstevel@tonic-gate #if	defined(_LP64)
2787c478bd9Sstevel@tonic-gate /* ARGSUSED */
2797c478bd9Sstevel@tonic-gate uintptr_t
la_pltexit64(Elf64_Sym * symp,uint_t symndx,uintptr_t * refcookie,uintptr_t * defcookie,uintptr_t retval,const char * sym_name)2807c478bd9Sstevel@tonic-gate la_pltexit64(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie,
281*78f67f4eSToomas Soome     uintptr_t *defcookie, uintptr_t retval, const char *sym_name)
2827c478bd9Sstevel@tonic-gate #else
2837c478bd9Sstevel@tonic-gate uintptr_t
2847c478bd9Sstevel@tonic-gate la_pltexit(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcookie,
285*78f67f4eSToomas Soome     uintptr_t *defcookie, uintptr_t retval)
2867c478bd9Sstevel@tonic-gate #endif
2877c478bd9Sstevel@tonic-gate {
2887c478bd9Sstevel@tonic-gate 	char		*defname = (char *)(*defcookie);
2897c478bd9Sstevel@tonic-gate 	char		*refname = (char *)(*refcookie);
2907c478bd9Sstevel@tonic-gate 	sigset_t	oset;
2917c478bd9Sstevel@tonic-gate #if	!defined(_LP64)
2927c478bd9Sstevel@tonic-gate 	const char	*sym_name = (const char *)symp->st_name;
2937c478bd9Sstevel@tonic-gate #endif
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &iset, &oset);
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	if (pidout)
2987c478bd9Sstevel@tonic-gate 		(void) fprintf(outfile, "%5d:", (int)pid);
2997c478bd9Sstevel@tonic-gate 	if (indent)
3007c478bd9Sstevel@tonic-gate 		indent_level--;
3017c478bd9Sstevel@tonic-gate 	(void) fprintf(outfile, "%-15s -> %15s:%*s%s - 0x%lx\n", refname,
302*78f67f4eSToomas Soome 	    defname, indent_level, "", sym_name, (ulong_t)retval);
3037c478bd9Sstevel@tonic-gate 	(void) fflush(outfile);
3047c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
3057c478bd9Sstevel@tonic-gate 	return (retval);
3067c478bd9Sstevel@tonic-gate }
307