xref: /titanic_52/usr/src/lib/abi/apptrace/common/apptrace.c (revision 004388ebfdfe2ed7dfd2d153a876dfcc22d2c006)
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
5*004388ebScasper  * Common Development and Distribution License (the "License").
6*004388ebScasper  * 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  */
217c478bd9Sstevel@tonic-gate /*
22*004388ebScasper  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <link.h>
297c478bd9Sstevel@tonic-gate #include <dlfcn.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/resource.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate #include <regex.h>
397c478bd9Sstevel@tonic-gate #include <signal.h>
407c478bd9Sstevel@tonic-gate #include <synch.h>
417c478bd9Sstevel@tonic-gate #include <fcntl.h>
427c478bd9Sstevel@tonic-gate #include <sys/stat.h>
437c478bd9Sstevel@tonic-gate #include <apptrace.h>
447c478bd9Sstevel@tonic-gate #include <libintl.h>
457c478bd9Sstevel@tonic-gate #include <locale.h>
467c478bd9Sstevel@tonic-gate #include <limits.h>
477c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
487c478bd9Sstevel@tonic-gate #include "abienv.h"
497c478bd9Sstevel@tonic-gate #include "mach.h"
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #include <libproc.h>
527c478bd9Sstevel@tonic-gate #include <libctf.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #define	NUM_ARGS 40
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate extern const char	*type_name(ctf_file_t *, ctf_id_t, char *, size_t);
577c478bd9Sstevel@tonic-gate extern void		print_value(ctf_file_t *, ctf_id_t, ulong_t);
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static struct ps_prochandle	*proc_hdl = NULL;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate static Liblist	*bindto_list;
627c478bd9Sstevel@tonic-gate static Liblist	*bindto_excl;
637c478bd9Sstevel@tonic-gate static Liblist	*bindfrom_list;
647c478bd9Sstevel@tonic-gate static Liblist	*bindfrom_excl;
657c478bd9Sstevel@tonic-gate static Liblist	*intlib_list;
667c478bd9Sstevel@tonic-gate static uint_t	pidout;
677c478bd9Sstevel@tonic-gate static Intlist	*trace_list;
687c478bd9Sstevel@tonic-gate static Intlist	*trace_excl;
697c478bd9Sstevel@tonic-gate static Intlist	*verbose_list;
707c478bd9Sstevel@tonic-gate static Intlist	*verbose_excl;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate  * Required for calls to build_env_list1 where
747c478bd9Sstevel@tonic-gate  * things are added to the end of the list (preserving
757c478bd9Sstevel@tonic-gate  * search order implied by the setting of env variables
767c478bd9Sstevel@tonic-gate  * in apptracecmd.c)
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate static Liblist	*intlib_listend;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate  * These globals are sought and used by interceptlib.c
827c478bd9Sstevel@tonic-gate  * which goes into all interceptor objects.
837c478bd9Sstevel@tonic-gate  */
847c478bd9Sstevel@tonic-gate FILE		*ABISTREAM = stderr;
857c478bd9Sstevel@tonic-gate sigset_t	abisigset;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate  * Strings are printed with "%.*s", abi_strpsz, string
897c478bd9Sstevel@tonic-gate  */
907c478bd9Sstevel@tonic-gate int		abi_strpsz = 20;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate  * Special function pointers that'll be set up to point at the
947c478bd9Sstevel@tonic-gate  * libc/libthread versions in the _application's_ link map (as opposed
957c478bd9Sstevel@tonic-gate  * to our own).
967c478bd9Sstevel@tonic-gate  *
977c478bd9Sstevel@tonic-gate  * Additionally, it is impossible to generalize the programmatic
987c478bd9Sstevel@tonic-gate  * creation of interceptor functions for variable argument list
997c478bd9Sstevel@tonic-gate  * functions.  However, in the case of the printf family, there is a
1007c478bd9Sstevel@tonic-gate  * vprintf equivalent.  The interceptors for the printf family live in
1017c478bd9Sstevel@tonic-gate  * interceptor.c and they call the appropriate vprintf interface
1027c478bd9Sstevel@tonic-gate  * instead of the printf interface that they're intercepting.  The
1037c478bd9Sstevel@tonic-gate  * link map issue remains, however, so function pointers for the
1047c478bd9Sstevel@tonic-gate  * vprintf family in the application's link map are set up here.
1057c478bd9Sstevel@tonic-gate  *
1067c478bd9Sstevel@tonic-gate  * The interceptors also need to examine errno which also needs to be
1077c478bd9Sstevel@tonic-gate  * extracted from the base link map.
1087c478bd9Sstevel@tonic-gate  *
1097c478bd9Sstevel@tonic-gate  * All of these pointers are initialized in la_preinit().
1107c478bd9Sstevel@tonic-gate  */
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate thread_t (*abi_thr_self)(void);
1137c478bd9Sstevel@tonic-gate int (*abi_thr_main)(void);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate int (*ABI_VFPRINTF)(FILE *, char const *, va_list);
1167c478bd9Sstevel@tonic-gate int (*ABI_VFWPRINTF)(FILE *, const wchar_t *, va_list);
1177c478bd9Sstevel@tonic-gate int (*ABI_VPRINTF)(char const *, va_list);
1187c478bd9Sstevel@tonic-gate int (*ABI_VSNPRINTF)(char *, size_t, char const *, va_list);
1197c478bd9Sstevel@tonic-gate int (*ABI_VSPRINTF)(char *, char const *, va_list);
1207c478bd9Sstevel@tonic-gate int (*ABI_VSWPRINTF)(wchar_t *, size_t, const wchar_t *, va_list);
1217c478bd9Sstevel@tonic-gate int (*ABI_VWPRINTF)(const wchar_t *, va_list);
1227c478bd9Sstevel@tonic-gate int *(*__abi_real_errno)(void);
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate #if defined(__sparcv9)
1257c478bd9Sstevel@tonic-gate static char const *libcpath		= "/lib/sparcv9/libc.so.1";
1267c478bd9Sstevel@tonic-gate #elif defined(__amd64)
1277c478bd9Sstevel@tonic-gate static char const *libcpath		= "/lib/amd64/libc.so.1";
1287c478bd9Sstevel@tonic-gate #else
1297c478bd9Sstevel@tonic-gate static char const *libcpath		= "/lib/libc.so.1";
1307c478bd9Sstevel@tonic-gate #endif
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate /* Used as arguments later to dlsym */
1337c478bd9Sstevel@tonic-gate static char const *thr_main_sym		= "thr_main";
1347c478bd9Sstevel@tonic-gate static char const *thr_self_sym		= "thr_self";
1357c478bd9Sstevel@tonic-gate static char const *vfprintf_sym		= "vfprintf";
1367c478bd9Sstevel@tonic-gate static char const *vfwprintf_sym	= "vfwprintf";
1377c478bd9Sstevel@tonic-gate static char const *vprintf_sym		= "vprintf";
1387c478bd9Sstevel@tonic-gate static char const *vsnprintf_sym	= "vsnprintf";
1397c478bd9Sstevel@tonic-gate static char const *vsprintf_sym		= "vsprintf";
1407c478bd9Sstevel@tonic-gate static char const *vswprintf_sym	= "vswprintf";
1417c478bd9Sstevel@tonic-gate static char const *vwprintf_sym		= "vwprintf";
1427c478bd9Sstevel@tonic-gate static char const *errno_sym		= "___errno";
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate /*
1457c478bd9Sstevel@tonic-gate  * The list of functions below are functions for which
1467c478bd9Sstevel@tonic-gate  * apptrace.so will not perform any tracing.
1477c478bd9Sstevel@tonic-gate  *
1487c478bd9Sstevel@tonic-gate  * The user visible failure of tracing these functions
1497c478bd9Sstevel@tonic-gate  * is a core dump of the application under observation.
1507c478bd9Sstevel@tonic-gate  *
1517c478bd9Sstevel@tonic-gate  * This list was originally discovered during sotruss
1527c478bd9Sstevel@tonic-gate  * development.  Attempts lacking sufficient determination
1537c478bd9Sstevel@tonic-gate  * to shrink this list have failed.
1547c478bd9Sstevel@tonic-gate  *
1557c478bd9Sstevel@tonic-gate  * There are a number of different kinds of issues here.
1567c478bd9Sstevel@tonic-gate  *
1577c478bd9Sstevel@tonic-gate  * The .stretX functions have to do with the relationship
1587c478bd9Sstevel@tonic-gate  * that the caller and callee has with functions that
1597c478bd9Sstevel@tonic-gate  * return structures and the altered calling convention
1607c478bd9Sstevel@tonic-gate  * that results.
1617c478bd9Sstevel@tonic-gate  *
1627c478bd9Sstevel@tonic-gate  * We cannot trace *setjmp because the caller of these routines
1637c478bd9Sstevel@tonic-gate  * is not allow to return which is exactly what an interceptor
1647c478bd9Sstevel@tonic-gate  * function is going to do.
1657c478bd9Sstevel@tonic-gate  *
1667c478bd9Sstevel@tonic-gate  * The *context functions are on the list because we cannot trace
1677c478bd9Sstevel@tonic-gate  * netscape without them on the list, but the exact mechanics of the
1687c478bd9Sstevel@tonic-gate  * failure are not known at this time.
1697c478bd9Sstevel@tonic-gate  *
1707c478bd9Sstevel@tonic-gate  * The leaf functions *getsp can probably be removed given the
1717c478bd9Sstevel@tonic-gate  * presence of an interceptor but that experiment has not been
1727c478bd9Sstevel@tonic-gate  * conducted.
1737c478bd9Sstevel@tonic-gate  *
1747c478bd9Sstevel@tonic-gate  * NOTE: this list *must* be maintained in alphabetical order.
1757c478bd9Sstevel@tonic-gate  *	 if this list ever became too long a faster search mechanism
1767c478bd9Sstevel@tonic-gate  *	 should be considered.
1777c478bd9Sstevel@tonic-gate  */
1787c478bd9Sstevel@tonic-gate static char *spec_sym[] = {
1797c478bd9Sstevel@tonic-gate #if defined(sparc)
1807c478bd9Sstevel@tonic-gate 	".stret1",
1817c478bd9Sstevel@tonic-gate 	".stret2",
1827c478bd9Sstevel@tonic-gate 	".stret4",
1837c478bd9Sstevel@tonic-gate 	".stret8",
1847c478bd9Sstevel@tonic-gate #endif
1857c478bd9Sstevel@tonic-gate 	"__getcontext",
1867c478bd9Sstevel@tonic-gate 	"_getcontext",
1877c478bd9Sstevel@tonic-gate 	"_getsp",
1887c478bd9Sstevel@tonic-gate 	"_longjmp",
1897c478bd9Sstevel@tonic-gate 	"_setcontext",
1907c478bd9Sstevel@tonic-gate 	"_setjmp",
1917c478bd9Sstevel@tonic-gate 	"_siglongjmp",
1927c478bd9Sstevel@tonic-gate 	"_sigsetjmp",
1937c478bd9Sstevel@tonic-gate 	"_vfork",
1947c478bd9Sstevel@tonic-gate 	"getcontext",
1957c478bd9Sstevel@tonic-gate 	"getsp",
1967c478bd9Sstevel@tonic-gate 	"longjmp",
1977c478bd9Sstevel@tonic-gate 	"setcontext",
1987c478bd9Sstevel@tonic-gate 	"setjmp",
1997c478bd9Sstevel@tonic-gate 	"siglongjmp",
2007c478bd9Sstevel@tonic-gate 	"sigsetjmp",
2017c478bd9Sstevel@tonic-gate 	"vfork",
2027c478bd9Sstevel@tonic-gate 	NULL
2037c478bd9Sstevel@tonic-gate };
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate uint_t
2067c478bd9Sstevel@tonic-gate la_version(uint_t version)
2077c478bd9Sstevel@tonic-gate {
2087c478bd9Sstevel@tonic-gate 	char		*str;
2097c478bd9Sstevel@tonic-gate 	FILE		*fp;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	if (version > LAV_CURRENT)
2127c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2137c478bd9Sstevel@tonic-gate 				dgettext(TEXT_DOMAIN,
2147c478bd9Sstevel@tonic-gate 					"apptrace: unexpected version: %u\n"),
2157c478bd9Sstevel@tonic-gate 				version);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	build_env_list(&bindto_list, "APPTRACE_BINDTO");
2187c478bd9Sstevel@tonic-gate 	build_env_list(&bindto_excl, "APPTRACE_BINDTO_EXCLUDE");
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	build_env_list(&bindfrom_list, "APPTRACE_BINDFROM");
2217c478bd9Sstevel@tonic-gate 	build_env_list(&bindfrom_excl, "APPTRACE_BINDFROM_EXCLUDE");
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	if (checkenv("APPTRACE_PID") != NULL) {
2247c478bd9Sstevel@tonic-gate 		pidout = 1;
2257c478bd9Sstevel@tonic-gate 	} else {
2267c478bd9Sstevel@tonic-gate 		char *str = "LD_AUDIT=";
2277c478bd9Sstevel@tonic-gate 		char *str2 = "LD_AUDIT64=";
2287c478bd9Sstevel@tonic-gate 		/*
2297c478bd9Sstevel@tonic-gate 		 * This disables apptrace output in subsequent exec'ed
2307c478bd9Sstevel@tonic-gate 		 * processes.
2317c478bd9Sstevel@tonic-gate 		 */
2327c478bd9Sstevel@tonic-gate 		(void) putenv(str);
2337c478bd9Sstevel@tonic-gate 		(void) putenv(str2);
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	if ((str = checkenv("APPTRACE_OUTPUT")) != NULL) {
2377c478bd9Sstevel@tonic-gate 		int fd, newfd, targetfd, lowerlimit;
2387c478bd9Sstevel@tonic-gate 		struct rlimit rl;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 		if (getrlimit(RLIMIT_NOFILE, &rl) == -1) {
2417c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2427c478bd9Sstevel@tonic-gate 					dgettext(TEXT_DOMAIN,
2437c478bd9Sstevel@tonic-gate 						"apptrace: getrlimit: %s\n"),
2447c478bd9Sstevel@tonic-gate 					strerror(errno));
2457c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
2467c478bd9Sstevel@tonic-gate 		}
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 		fd = open(str, O_WRONLY|O_CREAT|O_TRUNC, 0666);
2497c478bd9Sstevel@tonic-gate 		if (fd == -1) {
2507c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2517c478bd9Sstevel@tonic-gate 					dgettext(TEXT_DOMAIN,
2527c478bd9Sstevel@tonic-gate 						"apptrace: %s: %s\n"),
2537c478bd9Sstevel@tonic-gate 					str,
2547c478bd9Sstevel@tonic-gate 					strerror(errno));
2557c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
2567c478bd9Sstevel@tonic-gate 		}
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 		/*
2597c478bd9Sstevel@tonic-gate 		 * Those fans of dup2 should note that dup2 cannot
2607c478bd9Sstevel@tonic-gate 		 * be used below because dup2 closes the target file
2617c478bd9Sstevel@tonic-gate 		 * descriptor.  Thus, if we're apptracing say, ksh
2627c478bd9Sstevel@tonic-gate 		 * we'd have closed the fd it uses for the history
2637c478bd9Sstevel@tonic-gate 		 * file (63 on my box).
2647c478bd9Sstevel@tonic-gate 		 *
2657c478bd9Sstevel@tonic-gate 		 * fcntl with F_DUPFD returns first available >= arg3
2667c478bd9Sstevel@tonic-gate 		 * so we iterate from the top until we find a available
2677c478bd9Sstevel@tonic-gate 		 * fd.
2687c478bd9Sstevel@tonic-gate 		 *
2697c478bd9Sstevel@tonic-gate 		 * Not finding an fd after 10 tries is a failure.
2707c478bd9Sstevel@tonic-gate 		 *
2717c478bd9Sstevel@tonic-gate 		 * Since the _file member of the FILE structure is an
2727c478bd9Sstevel@tonic-gate 		 * unsigned char, we must clamp our fd request to
2737c478bd9Sstevel@tonic-gate 		 * UCHAR_MAX
2747c478bd9Sstevel@tonic-gate 		 */
2757c478bd9Sstevel@tonic-gate 		lowerlimit = ((rl.rlim_cur >
2767c478bd9Sstevel@tonic-gate 		    UCHAR_MAX) ? UCHAR_MAX : rl.rlim_cur) - 10;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 		for (targetfd = lowerlimit + 10;
2797c478bd9Sstevel@tonic-gate 		    targetfd > lowerlimit; targetfd--) {
2807c478bd9Sstevel@tonic-gate 			if ((newfd = fcntl(fd, F_DUPFD, targetfd)) != -1)
2817c478bd9Sstevel@tonic-gate 				break;
2827c478bd9Sstevel@tonic-gate 		}
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 		if (newfd == -1) {
2857c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2867c478bd9Sstevel@tonic-gate 					dgettext(TEXT_DOMAIN,
2877c478bd9Sstevel@tonic-gate 						"apptrace: F_DUPFD: %s\n"),
2887c478bd9Sstevel@tonic-gate 					strerror(errno));
2897c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
2907c478bd9Sstevel@tonic-gate 		}
2917c478bd9Sstevel@tonic-gate 		(void) close(fd);
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 		if (fcntl(newfd, F_SETFD, FD_CLOEXEC) == -1) {
2947c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2957c478bd9Sstevel@tonic-gate 					dgettext(TEXT_DOMAIN,
2967c478bd9Sstevel@tonic-gate 					"apptrace: fcntl FD_CLOEXEC: %s\n"),
2977c478bd9Sstevel@tonic-gate 					strerror(errno));
2987c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
2997c478bd9Sstevel@tonic-gate 		}
3007c478bd9Sstevel@tonic-gate 
301*004388ebScasper 		if ((fp = fdopen(newfd, "wF")) != NULL) {
3027c478bd9Sstevel@tonic-gate 			ABISTREAM = fp;
3037c478bd9Sstevel@tonic-gate 		} else {
3047c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3057c478bd9Sstevel@tonic-gate 					dgettext(TEXT_DOMAIN,
3067c478bd9Sstevel@tonic-gate 						"apptrace: fdopen: %s\n"),
3077c478bd9Sstevel@tonic-gate 					strerror(errno));
3087c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
3097c478bd9Sstevel@tonic-gate 		}
3107c478bd9Sstevel@tonic-gate 	}
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate #if defined(_LP64)
3137c478bd9Sstevel@tonic-gate 	build_env_list1(&intlib_list, &intlib_listend,
3147c478bd9Sstevel@tonic-gate 	    "APPTRACE_INTERCEPTORS64");
3157c478bd9Sstevel@tonic-gate #else
3167c478bd9Sstevel@tonic-gate 	build_env_list1(&intlib_list, &intlib_listend,
3177c478bd9Sstevel@tonic-gate 	    "APPTRACE_INTERCEPTORS");
3187c478bd9Sstevel@tonic-gate #endif
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	/* Set up lists interfaces to trace or ignore */
3217c478bd9Sstevel@tonic-gate 	env_to_intlist(&trace_list, "APPTRACE_INTERFACES");
3227c478bd9Sstevel@tonic-gate 	env_to_intlist(&trace_excl, "APPTRACE_INTERFACES_EXCLUDE");
3237c478bd9Sstevel@tonic-gate 	env_to_intlist(&verbose_list, "APPTRACE_VERBOSE");
3247c478bd9Sstevel@tonic-gate 	env_to_intlist(&verbose_excl, "APPTRACE_VERBOSE_EXCLUDE");
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	return (LAV_CURRENT);
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate /* ARGSUSED1 */
3307c478bd9Sstevel@tonic-gate uint_t
3317c478bd9Sstevel@tonic-gate la_objopen(Link_map *lmp, Lmid_t lmid, uintptr_t *cookie)
3327c478bd9Sstevel@tonic-gate {
3337c478bd9Sstevel@tonic-gate 	uint_t		flags;
3347c478bd9Sstevel@tonic-gate 	static int	first = 1;
3357c478bd9Sstevel@tonic-gate 	int		perr;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	/*
3387c478bd9Sstevel@tonic-gate 	 * If this is the first time in, then l_name is the app
3397c478bd9Sstevel@tonic-gate 	 * and unless the user gave an explict from list
3407c478bd9Sstevel@tonic-gate 	 * we will trace calls from it.
3417c478bd9Sstevel@tonic-gate 	 */
3427c478bd9Sstevel@tonic-gate 	if (first && bindfrom_list == NULL) {
3437c478bd9Sstevel@tonic-gate 		flags = LA_FLG_BINDFROM | LA_FLG_BINDTO;
3447c478bd9Sstevel@tonic-gate 		first = 0;
3457c478bd9Sstevel@tonic-gate 		goto work;
3467c478bd9Sstevel@tonic-gate 	}
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	/*
3497c478bd9Sstevel@tonic-gate 	 * If we have no bindto_list, then we assume that we
3507c478bd9Sstevel@tonic-gate 	 * bindto everything (apptrace -T \*)
3517c478bd9Sstevel@tonic-gate 	 *
3527c478bd9Sstevel@tonic-gate 	 * Otherwise we make sure that l_name is on the list.
3537c478bd9Sstevel@tonic-gate 	 */
3547c478bd9Sstevel@tonic-gate 	flags = 0;
3557c478bd9Sstevel@tonic-gate 	if (bindto_list == NULL) {
3567c478bd9Sstevel@tonic-gate 		flags = LA_FLG_BINDTO;
3577c478bd9Sstevel@tonic-gate 	} else if (check_list(bindto_list, lmp->l_name) != NULL) {
3587c478bd9Sstevel@tonic-gate 		flags |= LA_FLG_BINDTO;
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	/*
3627c478bd9Sstevel@tonic-gate 	 * If l_name is on the exclusion list, zero the bit.
3637c478bd9Sstevel@tonic-gate 	 */
3647c478bd9Sstevel@tonic-gate 	if ((bindto_excl != NULL) &&
3657c478bd9Sstevel@tonic-gate 	    check_list(bindto_excl, lmp->l_name) != NULL) {
3667c478bd9Sstevel@tonic-gate 		flags &= ~LA_FLG_BINDTO;
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	/*
3707c478bd9Sstevel@tonic-gate 	 * If l_name is on the bindfrom list then trace
3717c478bd9Sstevel@tonic-gate 	 */
3727c478bd9Sstevel@tonic-gate 	if (check_list(bindfrom_list, lmp->l_name) != NULL) {
3737c478bd9Sstevel@tonic-gate 		flags |= LA_FLG_BINDFROM;
3747c478bd9Sstevel@tonic-gate 	}
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	/*
3777c478bd9Sstevel@tonic-gate 	 * If l_name is on the exclusion list, zero the bit
3787c478bd9Sstevel@tonic-gate 	 * else trace, (this allows "-F !foo" to imply
3797c478bd9Sstevel@tonic-gate 	 * "-F '*' -F !foo")
3807c478bd9Sstevel@tonic-gate 	 */
3817c478bd9Sstevel@tonic-gate 	if (check_list(bindfrom_excl, lmp->l_name) != NULL) {
3827c478bd9Sstevel@tonic-gate 		flags &= ~LA_FLG_BINDFROM;
3837c478bd9Sstevel@tonic-gate 	} else if (bindfrom_excl != NULL && bindfrom_list == NULL) {
3847c478bd9Sstevel@tonic-gate 		flags |= LA_FLG_BINDFROM;
3857c478bd9Sstevel@tonic-gate 	}
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate work:
3887c478bd9Sstevel@tonic-gate 	if (flags) {
3897c478bd9Sstevel@tonic-gate 		*cookie = (uintptr_t)abibasename(lmp->l_name);
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 		/*
3927c478bd9Sstevel@tonic-gate 		 * only call Pgrab() once to get the ps_prochandle
3937c478bd9Sstevel@tonic-gate 		 */
3947c478bd9Sstevel@tonic-gate 		if (proc_hdl == NULL)
3957c478bd9Sstevel@tonic-gate 			proc_hdl = Pgrab(getpid(), PGRAB_RDONLY, &perr);
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	return (flags);
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate static void
4027c478bd9Sstevel@tonic-gate apptrace_preinit_fail(void)
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
4057c478bd9Sstevel@tonic-gate 			dgettext(TEXT_DOMAIN, "apptrace: la_preinit: %s\n"),
4067c478bd9Sstevel@tonic-gate 			dlerror());
4077c478bd9Sstevel@tonic-gate 	exit(EXIT_FAILURE);
4087c478bd9Sstevel@tonic-gate }
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate /* ARGSUSED */
4117c478bd9Sstevel@tonic-gate void
4127c478bd9Sstevel@tonic-gate la_preinit(uintptr_t *cookie)
4137c478bd9Sstevel@tonic-gate {
4147c478bd9Sstevel@tonic-gate 	void	*h = NULL;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	(void) sigfillset(&abisigset);
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	h = dlmopen(LM_ID_BASE, libcpath, RTLD_LAZY | RTLD_NOLOAD);
4197c478bd9Sstevel@tonic-gate 	if (h == NULL)
4207c478bd9Sstevel@tonic-gate 		apptrace_preinit_fail();
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	if ((abi_thr_self =
4237c478bd9Sstevel@tonic-gate 	    (thread_t (*)(void)) dlsym(h, thr_self_sym)) == NULL)
4247c478bd9Sstevel@tonic-gate 		apptrace_preinit_fail();
4257c478bd9Sstevel@tonic-gate 	if ((abi_thr_main =
4267c478bd9Sstevel@tonic-gate 	    (int (*)(void)) dlsym(h, thr_main_sym)) == NULL)
4277c478bd9Sstevel@tonic-gate 		apptrace_preinit_fail();
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	/* Do printf style pointers */
4307c478bd9Sstevel@tonic-gate 	if ((ABI_VFPRINTF =
4317c478bd9Sstevel@tonic-gate 	    (int (*)(FILE *, char const *, va_list))
4327c478bd9Sstevel@tonic-gate 	    dlsym(h, vfprintf_sym)) == NULL)
4337c478bd9Sstevel@tonic-gate 		apptrace_preinit_fail();
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	if ((ABI_VFWPRINTF =
4367c478bd9Sstevel@tonic-gate 	    (int (*)(FILE *, const wchar_t *, va_list))
4377c478bd9Sstevel@tonic-gate 	    dlsym(h, vfwprintf_sym)) == NULL)
4387c478bd9Sstevel@tonic-gate 		apptrace_preinit_fail();
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	if ((ABI_VPRINTF =
4417c478bd9Sstevel@tonic-gate 	    (int (*)(char const *, va_list))
4427c478bd9Sstevel@tonic-gate 	    dlsym(h, vprintf_sym)) == NULL)
4437c478bd9Sstevel@tonic-gate 		apptrace_preinit_fail();
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	if ((ABI_VSNPRINTF =
4467c478bd9Sstevel@tonic-gate 	    (int (*)(char *, size_t, char const *, va_list))
4477c478bd9Sstevel@tonic-gate 	    dlsym(h, vsnprintf_sym)) == NULL)
4487c478bd9Sstevel@tonic-gate 		apptrace_preinit_fail();
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	if ((ABI_VSPRINTF =
4517c478bd9Sstevel@tonic-gate 	    (int (*)(char *, char const *, va_list))
4527c478bd9Sstevel@tonic-gate 	    dlsym(h, vsprintf_sym)) == NULL)
4537c478bd9Sstevel@tonic-gate 		apptrace_preinit_fail();
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	if ((ABI_VSWPRINTF =
4567c478bd9Sstevel@tonic-gate 	    (int (*)(wchar_t *, size_t, const wchar_t *, va_list))
4577c478bd9Sstevel@tonic-gate 	    dlsym(h, vswprintf_sym)) == NULL)
4587c478bd9Sstevel@tonic-gate 		apptrace_preinit_fail();
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	if ((ABI_VWPRINTF =
4617c478bd9Sstevel@tonic-gate 	    (int (*)(const wchar_t *, va_list))
4627c478bd9Sstevel@tonic-gate 	    dlsym(h, vwprintf_sym)) == NULL)
4637c478bd9Sstevel@tonic-gate 		apptrace_preinit_fail();
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	if ((__abi_real_errno =
4667c478bd9Sstevel@tonic-gate 	    (int *(*)(void))
4677c478bd9Sstevel@tonic-gate 	    dlsym(h, errno_sym)) == NULL)
4687c478bd9Sstevel@tonic-gate 		apptrace_preinit_fail();
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	(void) dlclose(h);
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate /* ARGSUSED1 */
4747c478bd9Sstevel@tonic-gate #if defined(_LP64)
4757c478bd9Sstevel@tonic-gate uintptr_t
4767c478bd9Sstevel@tonic-gate la_symbind64(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcook,
4777c478bd9Sstevel@tonic-gate     uintptr_t *defcook, uint_t *sb_flags, char const *sym_name)
4787c478bd9Sstevel@tonic-gate #else
4797c478bd9Sstevel@tonic-gate uintptr_t
4807c478bd9Sstevel@tonic-gate la_symbind32(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcook,
4817c478bd9Sstevel@tonic-gate     uintptr_t *defcook, uint_t *sb_flags)
4827c478bd9Sstevel@tonic-gate #endif
4837c478bd9Sstevel@tonic-gate {
4847c478bd9Sstevel@tonic-gate #if !defined(_LP64)
4857c478bd9Sstevel@tonic-gate 	char const *sym_name = (char const *) symp->st_name;
4867c478bd9Sstevel@tonic-gate #endif
4877c478bd9Sstevel@tonic-gate 	int intercept = 0, verbose = 0;
4887c478bd9Sstevel@tonic-gate 	uintptr_t ret = symp->st_value;
4897c478bd9Sstevel@tonic-gate 	uint_t ndx;
4907c478bd9Sstevel@tonic-gate 	char *str;
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate #if defined(_LP64)
4937c478bd9Sstevel@tonic-gate 	if (ELF64_ST_TYPE(symp->st_info) != STT_FUNC)
4947c478bd9Sstevel@tonic-gate 		goto end;
4957c478bd9Sstevel@tonic-gate #else
4967c478bd9Sstevel@tonic-gate 	/* If we're not looking at a function, bug out */
4977c478bd9Sstevel@tonic-gate 	if (ELF32_ST_TYPE(symp->st_info) != STT_FUNC)
4987c478bd9Sstevel@tonic-gate 		goto end;
4997c478bd9Sstevel@tonic-gate #endif
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	if (verbose_list != NULL) {
5027c478bd9Sstevel@tonic-gate 		/* apptrace ... -v verbose_list ... cmd */
5037c478bd9Sstevel@tonic-gate 		if (check_intlist(verbose_list, sym_name))
5047c478bd9Sstevel@tonic-gate 			verbose = 1;
5057c478bd9Sstevel@tonic-gate 	}
5067c478bd9Sstevel@tonic-gate 	if (verbose_excl != NULL) {
5077c478bd9Sstevel@tonic-gate 		/* apptrace ... -v !verbose_excl ... cmd */
5087c478bd9Sstevel@tonic-gate 		if (check_intlist(verbose_excl, sym_name))
5097c478bd9Sstevel@tonic-gate 			verbose = 0;
5107c478bd9Sstevel@tonic-gate 		else if (verbose_list == NULL && trace_list == NULL &&
5117c478bd9Sstevel@tonic-gate 		    trace_excl == NULL)
5127c478bd9Sstevel@tonic-gate 			/* apptrace -v !verbose_excl cmd */
5137c478bd9Sstevel@tonic-gate 			intercept = 1;
5147c478bd9Sstevel@tonic-gate 	}
5157c478bd9Sstevel@tonic-gate 	if (trace_list != NULL) {
5167c478bd9Sstevel@tonic-gate 		/* apptrace ... -t trace_list ... cmd */
5177c478bd9Sstevel@tonic-gate 		if (check_intlist(trace_list, sym_name))
5187c478bd9Sstevel@tonic-gate 			intercept = 1;
5197c478bd9Sstevel@tonic-gate 	} else if (verbose_list == NULL && verbose_excl == NULL)
5207c478bd9Sstevel@tonic-gate 		/* default (implies -t '*'):  apptrace cmd */
5217c478bd9Sstevel@tonic-gate 		intercept = 1;
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	if (trace_excl != NULL) {
5247c478bd9Sstevel@tonic-gate 		/* apptrace ... -t !trace_excl ... cmd */
5257c478bd9Sstevel@tonic-gate 		if (check_intlist(trace_excl, sym_name))
5267c478bd9Sstevel@tonic-gate 			intercept = 0;
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	if (verbose == 0 && intercept == 0) {
5307c478bd9Sstevel@tonic-gate 		*sb_flags |= (LA_SYMB_NOPLTEXIT | LA_SYMB_NOPLTENTER);
5317c478bd9Sstevel@tonic-gate 		goto end;
5327c478bd9Sstevel@tonic-gate 	}
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	/*
5357c478bd9Sstevel@tonic-gate 	 * Check to see if this symbol is one of the 'special' symbols.
5367c478bd9Sstevel@tonic-gate 	 * If so we disable calls for that symbol.
5377c478bd9Sstevel@tonic-gate 	 */
5387c478bd9Sstevel@tonic-gate 	for (ndx = 0; (str = spec_sym[ndx]) != NULL; ndx++) {
5397c478bd9Sstevel@tonic-gate 		int	cmpval;
5407c478bd9Sstevel@tonic-gate 		cmpval = strcmp(sym_name, str);
5417c478bd9Sstevel@tonic-gate 		if (cmpval < 0)
5427c478bd9Sstevel@tonic-gate 			break;
5437c478bd9Sstevel@tonic-gate 		if (cmpval == 0) {
5447c478bd9Sstevel@tonic-gate 			intercept = verbose = 0;
5457c478bd9Sstevel@tonic-gate 			*sb_flags |= (LA_SYMB_NOPLTEXIT | LA_SYMB_NOPLTENTER);
5467c478bd9Sstevel@tonic-gate 			break;
5477c478bd9Sstevel@tonic-gate 		}
5487c478bd9Sstevel@tonic-gate 	}
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate end:
5517c478bd9Sstevel@tonic-gate 	return (ret);
5527c478bd9Sstevel@tonic-gate }
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate /* ARGSUSED1 */
5557c478bd9Sstevel@tonic-gate #if	defined(__sparcv9)
5567c478bd9Sstevel@tonic-gate uintptr_t
5577c478bd9Sstevel@tonic-gate la_sparcv9_pltenter(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie,
5587c478bd9Sstevel@tonic-gate 	uintptr_t *defcookie, La_sparcv9_regs *regset, uint_t *sb_flags,
5597c478bd9Sstevel@tonic-gate 	char const *sym_name)
5607c478bd9Sstevel@tonic-gate #elif	defined(__sparc)
5617c478bd9Sstevel@tonic-gate uintptr_t
5627c478bd9Sstevel@tonic-gate la_sparcv8_pltenter(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcookie,
5637c478bd9Sstevel@tonic-gate 	uintptr_t *defcookie, La_sparcv8_regs *regset, uint_t *sb_flags)
5647c478bd9Sstevel@tonic-gate #elif   defined(__amd64)
5657c478bd9Sstevel@tonic-gate uintptr_t
5667c478bd9Sstevel@tonic-gate la_amd64_pltenter(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie,
5677c478bd9Sstevel@tonic-gate 	uintptr_t *defcookie, La_amd64_regs *regset, uint_t *sb_flags,
5687c478bd9Sstevel@tonic-gate 	char const *sym_name)
5697c478bd9Sstevel@tonic-gate #elif   defined(__i386)
5707c478bd9Sstevel@tonic-gate uintptr_t
5717c478bd9Sstevel@tonic-gate la_i86_pltenter(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcookie,
5727c478bd9Sstevel@tonic-gate 	uintptr_t *defcookie, La_i86_regs *regset, uint_t *sb_flags)
5737c478bd9Sstevel@tonic-gate #endif
5747c478bd9Sstevel@tonic-gate {
5757c478bd9Sstevel@tonic-gate 	char		*defname = (char *)(*defcookie);
5767c478bd9Sstevel@tonic-gate 	char		*refname = (char *)(*refcookie);
5777c478bd9Sstevel@tonic-gate 	sigset_t	omask;
5787c478bd9Sstevel@tonic-gate #if	!defined(_LP64)
5797c478bd9Sstevel@tonic-gate 	char const	*sym_name = (char const *)symp->st_name;
5807c478bd9Sstevel@tonic-gate #endif
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	char		buf[256];
5837c478bd9Sstevel@tonic-gate 	GElf_Sym	sym;
5847c478bd9Sstevel@tonic-gate 	prsyminfo_t	si;
5857c478bd9Sstevel@tonic-gate 	ctf_file_t	*ctfp;
5867c478bd9Sstevel@tonic-gate 	ctf_funcinfo_t	finfo;
5877c478bd9Sstevel@tonic-gate 	int		argc;
5887c478bd9Sstevel@tonic-gate 	ctf_id_t	argt[NUM_ARGS];
5897c478bd9Sstevel@tonic-gate 	ulong_t		argv[NUM_ARGS];
5907c478bd9Sstevel@tonic-gate 	int		i;
5917c478bd9Sstevel@tonic-gate 	char		*sep = "";
5927c478bd9Sstevel@tonic-gate 	ctf_id_t	type, rtype;
5937c478bd9Sstevel@tonic-gate 	int		kind;
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	abilock(&omask);
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	if (pidout)
5987c478bd9Sstevel@tonic-gate 		(void) fprintf(ABISTREAM, "%7u:", (unsigned int)getpid());
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	if ((ctfp = Pname_to_ctf(proc_hdl, defname)) == NULL)
6017c478bd9Sstevel@tonic-gate 		goto fail;
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	if (Pxlookup_by_name(proc_hdl, PR_LMID_EVERY, defname, sym_name,
6047c478bd9Sstevel@tonic-gate 	    &sym, &si) != 0)
6057c478bd9Sstevel@tonic-gate 		goto fail;
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	if (ctf_func_info(ctfp, si.prs_id, &finfo) == CTF_ERR)
6087c478bd9Sstevel@tonic-gate 		goto fail;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	(void) type_name(ctfp, finfo.ctc_return, buf, sizeof (buf));
6117c478bd9Sstevel@tonic-gate 	(void) fprintf(ABISTREAM, "-> %-8s -> %8s:%s %s(",
6127c478bd9Sstevel@tonic-gate 	    refname, defname, buf, sym_name);
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	/*
6157c478bd9Sstevel@tonic-gate 	 * According to bug in la_pltexit(), it can't return
6167c478bd9Sstevel@tonic-gate 	 * if the type is just a struct/union.  So, if the return
6177c478bd9Sstevel@tonic-gate 	 * type is a struct/union, la_pltexit() should be off.
6187c478bd9Sstevel@tonic-gate 	 */
6197c478bd9Sstevel@tonic-gate 	rtype = ctf_type_resolve(ctfp, finfo.ctc_return);
6207c478bd9Sstevel@tonic-gate 	type = ctf_type_reference(ctfp, rtype);
6217c478bd9Sstevel@tonic-gate 	rtype = ctf_type_resolve(ctfp, type);
6227c478bd9Sstevel@tonic-gate 	kind = ctf_type_kind(ctfp, rtype);
6237c478bd9Sstevel@tonic-gate 	if ((kind == CTF_K_STRUCT || kind == CTF_K_UNION) &&
6247c478bd9Sstevel@tonic-gate 	    strpbrk(buf, "*") == NULL)
6257c478bd9Sstevel@tonic-gate 		*sb_flags |= LA_SYMB_NOPLTEXIT;
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	argc = MIN(sizeof (argt) / sizeof (argt[0]), finfo.ctc_argc);
6287c478bd9Sstevel@tonic-gate 	(void) ctf_func_args(ctfp, si.prs_id, argc, argt);
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	argv[0] = GETARG0(regset);
6317c478bd9Sstevel@tonic-gate 	if (argc > 1)
6327c478bd9Sstevel@tonic-gate 		argv[1] = GETARG1(regset);
6337c478bd9Sstevel@tonic-gate 	if (argc > 2)
6347c478bd9Sstevel@tonic-gate 		argv[2] = GETARG2(regset);
6357c478bd9Sstevel@tonic-gate 	if (argc > 3)
6367c478bd9Sstevel@tonic-gate 		argv[3] = GETARG3(regset);
6377c478bd9Sstevel@tonic-gate 	if (argc > 4)
6387c478bd9Sstevel@tonic-gate 		argv[4] = GETARG4(regset);
6397c478bd9Sstevel@tonic-gate 	if (argc > 5)
6407c478bd9Sstevel@tonic-gate 		argv[5] = GETARG5(regset);
6417c478bd9Sstevel@tonic-gate 	if (argc > 6) {
6427c478bd9Sstevel@tonic-gate 		for (i = 6; i < argc; i++)
6437c478bd9Sstevel@tonic-gate 			argv[i] = GETARG_6NUP(i, regset);
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
6477c478bd9Sstevel@tonic-gate 		(void) type_name(ctfp, argt[i], buf, sizeof (buf));
6487c478bd9Sstevel@tonic-gate 		(void) fprintf(ABISTREAM, "%s%s = ", sep, buf);
6497c478bd9Sstevel@tonic-gate 		rtype = ctf_type_resolve(ctfp, argt[i]);
6507c478bd9Sstevel@tonic-gate 		type = ctf_type_reference(ctfp, rtype);
6517c478bd9Sstevel@tonic-gate 		rtype = ctf_type_resolve(ctfp, type);
6527c478bd9Sstevel@tonic-gate 		kind = ctf_type_kind(ctfp, rtype);
6537c478bd9Sstevel@tonic-gate 		if (kind == CTF_K_STRUCT || kind == CTF_K_UNION)
6547c478bd9Sstevel@tonic-gate 			(void) fprintf(ABISTREAM, "0x%p", (void *)argv[i]);
6557c478bd9Sstevel@tonic-gate 		else
6567c478bd9Sstevel@tonic-gate 			print_value(ctfp, argt[i], argv[i]);
6577c478bd9Sstevel@tonic-gate 		sep = ", ";
6587c478bd9Sstevel@tonic-gate 	}
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	if (finfo.ctc_flags & CTF_FUNC_VARARG)
6617c478bd9Sstevel@tonic-gate 		(void) fprintf(ABISTREAM, "%s...", sep);
6627c478bd9Sstevel@tonic-gate 	else if (argc == 0)
6637c478bd9Sstevel@tonic-gate 		(void) fprintf(ABISTREAM, "void");
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	if ((*sb_flags & LA_SYMB_NOPLTEXIT) != 0)
6667c478bd9Sstevel@tonic-gate 		(void) fprintf(ABISTREAM, ") ** ST\n");
6677c478bd9Sstevel@tonic-gate 	else
6687c478bd9Sstevel@tonic-gate 		(void) fprintf(ABISTREAM, ")\n");
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	if (verbose_list != NULL &&
6717c478bd9Sstevel@tonic-gate 	    check_intlist(verbose_list, sym_name) != 0) {
6727c478bd9Sstevel@tonic-gate 		for (i = 0; i < argc; i++) {
6737c478bd9Sstevel@tonic-gate 			(void) type_name(ctfp, argt[i], buf, sizeof (buf));
6747c478bd9Sstevel@tonic-gate 			(void) fprintf(ABISTREAM, "\targ%d = (%s) ", i, buf);
6757c478bd9Sstevel@tonic-gate 			print_value(ctfp, argt[i], argv[i]);
6767c478bd9Sstevel@tonic-gate 			(void) fprintf(ABISTREAM, "\n");
6777c478bd9Sstevel@tonic-gate 		}
6787c478bd9Sstevel@tonic-gate 		if ((*sb_flags & LA_SYMB_NOPLTEXIT) != 0) {
6797c478bd9Sstevel@tonic-gate 			if (kind == CTF_K_STRUCT)
6807c478bd9Sstevel@tonic-gate 				(void) fprintf(ABISTREAM,
6817c478bd9Sstevel@tonic-gate 				    "\treturn = (struct), apptrace "
6827c478bd9Sstevel@tonic-gate 				    "will not trace the return\n");
6837c478bd9Sstevel@tonic-gate 			else
6847c478bd9Sstevel@tonic-gate 				(void) fprintf(ABISTREAM,
6857c478bd9Sstevel@tonic-gate 				    "\treturn = (union), apptrace "
6867c478bd9Sstevel@tonic-gate 				    "will not trace the return\n");
6877c478bd9Sstevel@tonic-gate 		}
6887c478bd9Sstevel@tonic-gate 	}
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	(void) fflush(ABISTREAM);
6917c478bd9Sstevel@tonic-gate 	abiunlock(&omask);
6927c478bd9Sstevel@tonic-gate 	return (symp->st_value);
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate fail:
6957c478bd9Sstevel@tonic-gate 	(void) fprintf(ABISTREAM,
6967c478bd9Sstevel@tonic-gate 	    "-> %-8s -> %8s:%s(0x%lx, 0x%lx, 0x%lx) ** NR\n",
6977c478bd9Sstevel@tonic-gate 	    refname, defname, sym_name,
6987c478bd9Sstevel@tonic-gate 	    (ulong_t)GETARG0(regset),
6997c478bd9Sstevel@tonic-gate 	    (ulong_t)GETARG1(regset),
7007c478bd9Sstevel@tonic-gate 	    (ulong_t)GETARG2(regset));
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	*sb_flags |= LA_SYMB_NOPLTEXIT;
7037c478bd9Sstevel@tonic-gate 	(void) fflush(ABISTREAM);
7047c478bd9Sstevel@tonic-gate 	abiunlock(&omask);
7057c478bd9Sstevel@tonic-gate 	return (symp->st_value);
7067c478bd9Sstevel@tonic-gate }
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate /* ARGSUSED */
7097c478bd9Sstevel@tonic-gate #if	defined(_LP64)
7107c478bd9Sstevel@tonic-gate uintptr_t
7117c478bd9Sstevel@tonic-gate la_pltexit64(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie,
7127c478bd9Sstevel@tonic-gate 	uintptr_t *defcookie, uintptr_t retval, const char *sym_name)
7137c478bd9Sstevel@tonic-gate #else
7147c478bd9Sstevel@tonic-gate uintptr_t
7157c478bd9Sstevel@tonic-gate la_pltexit(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcookie,
7167c478bd9Sstevel@tonic-gate 	uintptr_t *defcookie, uintptr_t retval)
7177c478bd9Sstevel@tonic-gate #endif
7187c478bd9Sstevel@tonic-gate {
7197c478bd9Sstevel@tonic-gate #if	!defined(_LP64)
7207c478bd9Sstevel@tonic-gate 	const char	*sym_name = (const char *)symp->st_name;
7217c478bd9Sstevel@tonic-gate #endif
7227c478bd9Sstevel@tonic-gate 	sigset_t	omask;
7237c478bd9Sstevel@tonic-gate 	char		buf[256];
7247c478bd9Sstevel@tonic-gate 	GElf_Sym	sym;
7257c478bd9Sstevel@tonic-gate 	prsyminfo_t	si;
7267c478bd9Sstevel@tonic-gate 	ctf_file_t	*ctfp;
7277c478bd9Sstevel@tonic-gate 	ctf_funcinfo_t	finfo;
7287c478bd9Sstevel@tonic-gate 	char		*defname = (char *)(*defcookie);
7297c478bd9Sstevel@tonic-gate 	char		*refname = (char *)(*refcookie);
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	abilock(&omask);
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	if (pidout)
7347c478bd9Sstevel@tonic-gate 		(void) fprintf(ABISTREAM, "%7u:", (unsigned int)getpid());
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	if (retval == 0) {
7377c478bd9Sstevel@tonic-gate 		if (verbose_list == NULL) {
7387c478bd9Sstevel@tonic-gate 			(void) fprintf(ABISTREAM, "<- %-8s -> %8s:%s()\n",
7397c478bd9Sstevel@tonic-gate 			    refname, defname, sym_name);
7407c478bd9Sstevel@tonic-gate 			(void) fflush(ABISTREAM);
7417c478bd9Sstevel@tonic-gate 		}
7427c478bd9Sstevel@tonic-gate 		abiunlock(&omask);
7437c478bd9Sstevel@tonic-gate 		return (retval);
7447c478bd9Sstevel@tonic-gate 	}
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	if ((ctfp = Pname_to_ctf(proc_hdl, defname)) == NULL)
7477c478bd9Sstevel@tonic-gate 		goto fail;
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	if (Pxlookup_by_name(proc_hdl, PR_LMID_EVERY, defname,
7507c478bd9Sstevel@tonic-gate 	    sym_name, &sym, &si) != 0)
7517c478bd9Sstevel@tonic-gate 		goto fail;
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	if (ctf_func_info(ctfp, si.prs_id, &finfo) == CTF_ERR)
7547c478bd9Sstevel@tonic-gate 		goto fail;
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	if (verbose_list != NULL) {
7577c478bd9Sstevel@tonic-gate 		if (check_intlist(verbose_list, sym_name) != 0) {
7587c478bd9Sstevel@tonic-gate 			(void) type_name(ctfp, finfo.ctc_return, buf,
7597c478bd9Sstevel@tonic-gate 			    sizeof (buf));
7607c478bd9Sstevel@tonic-gate 			(void) fprintf(ABISTREAM, "\treturn = (%s) ", buf);
7617c478bd9Sstevel@tonic-gate 			print_value(ctfp, finfo.ctc_return, retval);
7627c478bd9Sstevel@tonic-gate 			(void) fprintf(ABISTREAM, "\n");
7637c478bd9Sstevel@tonic-gate 			(void) fprintf(ABISTREAM, "<- %-8s -> %8s:%s()",
7647c478bd9Sstevel@tonic-gate 			    refname, defname, sym_name);
7657c478bd9Sstevel@tonic-gate 			(void) fprintf(ABISTREAM, " = 0x%p\n", (void *)retval);
7667c478bd9Sstevel@tonic-gate 		}
7677c478bd9Sstevel@tonic-gate 	} else {
7687c478bd9Sstevel@tonic-gate 		(void) fprintf(ABISTREAM, "<- %-8s -> %8s:%s()",
7697c478bd9Sstevel@tonic-gate 		    refname, defname, sym_name);
7707c478bd9Sstevel@tonic-gate 		(void) fprintf(ABISTREAM, " = 0x%p\n", (void *)retval);
7717c478bd9Sstevel@tonic-gate 	}
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	(void) fflush(ABISTREAM);
7747c478bd9Sstevel@tonic-gate 	abiunlock(&omask);
7757c478bd9Sstevel@tonic-gate 	return (retval);
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate fail:
7787c478bd9Sstevel@tonic-gate 	if (verbose_list != NULL) {
7797c478bd9Sstevel@tonic-gate 		if (check_intlist(verbose_list, sym_name) != 0) {
7807c478bd9Sstevel@tonic-gate 			(void) fprintf(ABISTREAM,
7817c478bd9Sstevel@tonic-gate 			    "\treturn = 0x%p\n", (void *)retval);
7827c478bd9Sstevel@tonic-gate 			(void) fprintf(ABISTREAM, "<- %-8s -> %8s:%s()",
7837c478bd9Sstevel@tonic-gate 			    refname, defname, sym_name);
7847c478bd9Sstevel@tonic-gate 			(void) fprintf(ABISTREAM, " = 0x%p\n", (void *)retval);
7857c478bd9Sstevel@tonic-gate 		}
7867c478bd9Sstevel@tonic-gate 	} else {
7877c478bd9Sstevel@tonic-gate 		(void) fprintf(ABISTREAM, "<- %-8s -> %8s:%s()",
7887c478bd9Sstevel@tonic-gate 		    refname, defname, sym_name);
7897c478bd9Sstevel@tonic-gate 		(void) fprintf(ABISTREAM, " = 0x%p\n", (void *)retval);
7907c478bd9Sstevel@tonic-gate 	}
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	(void) fflush(ABISTREAM);
7937c478bd9Sstevel@tonic-gate 	abiunlock(&omask);
7947c478bd9Sstevel@tonic-gate 	return (retval);
7957c478bd9Sstevel@tonic-gate }
796