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 */ 21*20c1c355SRod Evans 227c478bd9Sstevel@tonic-gate /* 23*20c1c355SRod 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 37*20c1c355SRod Evans static Elist *bindto_list = NULL; 38*20c1c355SRod 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 897c478bd9Sstevel@tonic-gate la_version(uint_t version) 907c478bd9Sstevel@tonic-gate { 917c478bd9Sstevel@tonic-gate char *str; 92*20c1c355SRod Evans 937c478bd9Sstevel@tonic-gate if (version > LAV_CURRENT) 947c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "truss.so: unexpected version: %d\n", 957c478bd9Sstevel@tonic-gate 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 1237c478bd9Sstevel@tonic-gate if (str = checkenv((const char *)"TRUSS_OUTPUT")) { 1247c478bd9Sstevel@tonic-gate FILE *fp; 1257c478bd9Sstevel@tonic-gate char fname[MAXPATHLEN]; 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate if (pidout) 1287c478bd9Sstevel@tonic-gate (void) snprintf(fname, MAXPATHLEN, "%s.%d", str, 1297c478bd9Sstevel@tonic-gate (int)pid); 1307c478bd9Sstevel@tonic-gate else 1317c478bd9Sstevel@tonic-gate (void) strncpy(fname, str, MAXPATHLEN); 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate if (fp = fopen(fname, (const char *)"w")) { 1347c478bd9Sstevel@tonic-gate outfile = fp; 1357c478bd9Sstevel@tonic-gate } else 1367c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1377c478bd9Sstevel@tonic-gate "truss.so: unable to open file=`%s': %s\n", 1387c478bd9Sstevel@tonic-gate fname, strerror(errno)); 1397c478bd9Sstevel@tonic-gate } 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate /* 1427c478bd9Sstevel@tonic-gate * Initalize iset to the full set of signals to be masked durring 1437c478bd9Sstevel@tonic-gate * pltenter/pltexit 1447c478bd9Sstevel@tonic-gate */ 1457c478bd9Sstevel@tonic-gate (void) sigfillset(&iset); 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate return (LAV_CURRENT); 1487c478bd9Sstevel@tonic-gate } 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate /* ARGSUSED1 */ 1517c478bd9Sstevel@tonic-gate uint_t 1527c478bd9Sstevel@tonic-gate la_objopen(Link_map *lmp, Lmid_t lmid, uintptr_t *cookie) 1537c478bd9Sstevel@tonic-gate { 1547c478bd9Sstevel@tonic-gate uint_t flags; 1557c478bd9Sstevel@tonic-gate char *basename; 1567c478bd9Sstevel@tonic-gate static int first = 1; 1577c478bd9Sstevel@tonic-gate 158*20c1c355SRod Evans if ((bindto_list == NULL) || (trussall)) 1597c478bd9Sstevel@tonic-gate flags = LA_FLG_BINDTO; 1607c478bd9Sstevel@tonic-gate else if (check_list(bindto_list, lmp->l_name)) 1617c478bd9Sstevel@tonic-gate flags = LA_FLG_BINDTO; 1627c478bd9Sstevel@tonic-gate else 1637c478bd9Sstevel@tonic-gate flags = 0; 1647c478bd9Sstevel@tonic-gate 165*20c1c355SRod Evans if (((bindfrom_list == NULL) && first) || trussall || 1667c478bd9Sstevel@tonic-gate (check_list(bindfrom_list, lmp->l_name))) 1677c478bd9Sstevel@tonic-gate flags |= LA_FLG_BINDFROM; 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate first = 0; 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate if (flags) { 172*20c1c355SRod Evans if ((basename = strrchr(lmp->l_name, '/')) != NULL) 1737c478bd9Sstevel@tonic-gate basename++; 1747c478bd9Sstevel@tonic-gate else 1757c478bd9Sstevel@tonic-gate basename = lmp->l_name; 1767c478bd9Sstevel@tonic-gate *cookie = (uintptr_t)basename; 1777c478bd9Sstevel@tonic-gate } 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate return (flags); 1807c478bd9Sstevel@tonic-gate } 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate /* ARGSUSED1 */ 1837c478bd9Sstevel@tonic-gate #if defined(_LP64) 1847c478bd9Sstevel@tonic-gate uintptr_t 1857c478bd9Sstevel@tonic-gate la_symbind64(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcook, 1867c478bd9Sstevel@tonic-gate uintptr_t *defcook, uint_t *sb_flags, const char *sym_name) 1877c478bd9Sstevel@tonic-gate #else 1887c478bd9Sstevel@tonic-gate uintptr_t 1897c478bd9Sstevel@tonic-gate la_symbind32(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcook, 1907c478bd9Sstevel@tonic-gate uintptr_t *defcook, uint_t *sb_flags) 1917c478bd9Sstevel@tonic-gate #endif 1927c478bd9Sstevel@tonic-gate { 1937c478bd9Sstevel@tonic-gate #if !defined(_LP64) 1947c478bd9Sstevel@tonic-gate const char *sym_name = (const char *)symp->st_name; 1957c478bd9Sstevel@tonic-gate #endif 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate if (noexit) 1997c478bd9Sstevel@tonic-gate *sb_flags |= LA_SYMB_NOPLTEXIT; 2007c478bd9Sstevel@tonic-gate 2017c478bd9Sstevel@tonic-gate /* 2027c478bd9Sstevel@tonic-gate * Check to see if this symbol is one of the 'special' symbols. 2037c478bd9Sstevel@tonic-gate * If so we disable PLTEXIT calls for that symbol. 2047c478bd9Sstevel@tonic-gate */ 2057c478bd9Sstevel@tonic-gate if ((*sb_flags & LA_SYMB_NOPLTEXIT) == 0) { 2067c478bd9Sstevel@tonic-gate uint_t ndx; 2077c478bd9Sstevel@tonic-gate char *str; 2087c478bd9Sstevel@tonic-gate /* LINTED */ 2097c478bd9Sstevel@tonic-gate for (ndx = 0; str = spec_sym[ndx]; ndx++) { 2107c478bd9Sstevel@tonic-gate int cmpval; 2117c478bd9Sstevel@tonic-gate cmpval = strcmp(sym_name, str); 2127c478bd9Sstevel@tonic-gate if (cmpval < 0) 2137c478bd9Sstevel@tonic-gate break; 2147c478bd9Sstevel@tonic-gate if (cmpval == 0) { 2157c478bd9Sstevel@tonic-gate *sb_flags |= LA_SYMB_NOPLTEXIT; 2167c478bd9Sstevel@tonic-gate break; 2177c478bd9Sstevel@tonic-gate } 2187c478bd9Sstevel@tonic-gate } 2197c478bd9Sstevel@tonic-gate } 2207c478bd9Sstevel@tonic-gate return (symp->st_value); 2217c478bd9Sstevel@tonic-gate } 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate /* ARGSUSED1 */ 2247c478bd9Sstevel@tonic-gate #if defined(__sparcv9) 2257c478bd9Sstevel@tonic-gate uintptr_t 2267c478bd9Sstevel@tonic-gate la_sparcv9_pltenter(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie, 2277c478bd9Sstevel@tonic-gate uintptr_t *defcookie, La_sparcv9_regs *regset, uint_t *sb_flags, 2287c478bd9Sstevel@tonic-gate const char *sym_name) 2297c478bd9Sstevel@tonic-gate #elif defined(__sparc) 2307c478bd9Sstevel@tonic-gate uintptr_t 2317c478bd9Sstevel@tonic-gate la_sparcv8_pltenter(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcookie, 2327c478bd9Sstevel@tonic-gate uintptr_t *defcookie, La_sparcv8_regs *regset, uint_t *sb_flags) 2337c478bd9Sstevel@tonic-gate #elif defined(__amd64) 2347c478bd9Sstevel@tonic-gate uintptr_t 2357c478bd9Sstevel@tonic-gate la_amd64_pltenter(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie, 2367c478bd9Sstevel@tonic-gate uintptr_t *defcookie, La_amd64_regs *regset, uint_t *sb_flags, 2377c478bd9Sstevel@tonic-gate const char *sym_name) 2387c478bd9Sstevel@tonic-gate #elif defined(__i386) 2397c478bd9Sstevel@tonic-gate uintptr_t 2407c478bd9Sstevel@tonic-gate la_i86_pltenter(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcookie, 2417c478bd9Sstevel@tonic-gate uintptr_t *defcookie, La_i86_regs *regset, uint_t *sb_flags) 2427c478bd9Sstevel@tonic-gate #endif 2437c478bd9Sstevel@tonic-gate { 2447c478bd9Sstevel@tonic-gate char *istr; 2457c478bd9Sstevel@tonic-gate char *defname = (char *)(*defcookie); 2467c478bd9Sstevel@tonic-gate char *refname = (char *)(*refcookie); 2477c478bd9Sstevel@tonic-gate #if !defined(_LP64) 2487c478bd9Sstevel@tonic-gate const char *sym_name = (const char *)symp->st_name; 2497c478bd9Sstevel@tonic-gate #endif 2507c478bd9Sstevel@tonic-gate sigset_t oset; 2517c478bd9Sstevel@tonic-gate 2527c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &iset, &oset); 2537c478bd9Sstevel@tonic-gate 2547c478bd9Sstevel@tonic-gate if (pidout) 2557c478bd9Sstevel@tonic-gate (void) fprintf(outfile, "%5d:", (int)getpid()); 2567c478bd9Sstevel@tonic-gate 2577c478bd9Sstevel@tonic-gate if ((*sb_flags & LA_SYMB_NOPLTEXIT) == 0) 2587c478bd9Sstevel@tonic-gate istr = ""; 2597c478bd9Sstevel@tonic-gate else 2607c478bd9Sstevel@tonic-gate istr = "*"; 2617c478bd9Sstevel@tonic-gate 2627c478bd9Sstevel@tonic-gate (void) fprintf(outfile, "%-15s -> %15s:%-*s%s(0x%lx, 0x%lx, 0x%lx)\n", 2637c478bd9Sstevel@tonic-gate refname, defname, indent_level, istr, sym_name, 2647c478bd9Sstevel@tonic-gate (long)GETARG0(regset), (long)GETARG1(regset), 2657c478bd9Sstevel@tonic-gate (long)GETARG2(regset)); 2667c478bd9Sstevel@tonic-gate 2677c478bd9Sstevel@tonic-gate (void) fflush(outfile); 2687c478bd9Sstevel@tonic-gate if (indent && ((*sb_flags & LA_SYMB_NOPLTEXIT) == 0)) 2697c478bd9Sstevel@tonic-gate indent_level++; 2707c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, &oset, NULL); 2717c478bd9Sstevel@tonic-gate return (symp->st_value); 2727c478bd9Sstevel@tonic-gate } 2737c478bd9Sstevel@tonic-gate 2747c478bd9Sstevel@tonic-gate /* ARGSUSED1 */ 2757c478bd9Sstevel@tonic-gate #if defined(_LP64) 2767c478bd9Sstevel@tonic-gate /* ARGSUSED */ 2777c478bd9Sstevel@tonic-gate uintptr_t 2787c478bd9Sstevel@tonic-gate la_pltexit64(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie, 2797c478bd9Sstevel@tonic-gate uintptr_t *defcookie, uintptr_t retval, const char *sym_name) 2807c478bd9Sstevel@tonic-gate #else 2817c478bd9Sstevel@tonic-gate uintptr_t 2827c478bd9Sstevel@tonic-gate la_pltexit(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcookie, 2837c478bd9Sstevel@tonic-gate uintptr_t *defcookie, uintptr_t retval) 2847c478bd9Sstevel@tonic-gate #endif 2857c478bd9Sstevel@tonic-gate { 2867c478bd9Sstevel@tonic-gate char *defname = (char *)(*defcookie); 2877c478bd9Sstevel@tonic-gate char *refname = (char *)(*refcookie); 2887c478bd9Sstevel@tonic-gate sigset_t oset; 2897c478bd9Sstevel@tonic-gate #if !defined(_LP64) 2907c478bd9Sstevel@tonic-gate const char *sym_name = (const char *)symp->st_name; 2917c478bd9Sstevel@tonic-gate #endif 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &iset, &oset); 2947c478bd9Sstevel@tonic-gate 2957c478bd9Sstevel@tonic-gate if (pidout) 2967c478bd9Sstevel@tonic-gate (void) fprintf(outfile, "%5d:", (int)pid); 2977c478bd9Sstevel@tonic-gate if (indent) 2987c478bd9Sstevel@tonic-gate indent_level--; 2997c478bd9Sstevel@tonic-gate (void) fprintf(outfile, "%-15s -> %15s:%*s%s - 0x%lx\n", refname, 3007c478bd9Sstevel@tonic-gate defname, indent_level, "", sym_name, (ulong_t)retval); 3017c478bd9Sstevel@tonic-gate (void) fflush(outfile); 3027c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, &oset, NULL); 3037c478bd9Sstevel@tonic-gate return (retval); 3047c478bd9Sstevel@tonic-gate } 305