xref: /onnv-gate/usr/src/cmd/sgs/link_audit/common/who.c (revision 12927:a27c46eb192b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*12927SRod.Evans@Sun.COM  * Common Development and Distribution License (the "License").
6*12927SRod.Evans@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*12927SRod.Evans@Sun.COM 
220Sstevel@tonic-gate /*
23*12927SRod.Evans@Sun.COM  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate #include <stdio.h>
260Sstevel@tonic-gate #include <fcntl.h>
270Sstevel@tonic-gate #include <link.h>
280Sstevel@tonic-gate #include <stdlib.h>
290Sstevel@tonic-gate #include <unistd.h>
300Sstevel@tonic-gate #include <strings.h>
310Sstevel@tonic-gate #include <sys/regset.h>
320Sstevel@tonic-gate #include <sys/frame.h>
330Sstevel@tonic-gate #include <sys/stack.h>
340Sstevel@tonic-gate #include <signal.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include "env.h"
370Sstevel@tonic-gate #include "mach.h"
380Sstevel@tonic-gate #include "who.h"
390Sstevel@tonic-gate 
400Sstevel@tonic-gate 
41*12927SRod.Evans@Sun.COM static int	detail_syms = 0;	/* display detail symbol information */
42*12927SRod.Evans@Sun.COM static Objinfo	*objhead = NULL;	/* head of object list */
43*12927SRod.Evans@Sun.COM static Elist	*funclist = NULL;
44*12927SRod.Evans@Sun.COM static sigset_t	iset;
450Sstevel@tonic-gate 
460Sstevel@tonic-gate static void
add_object(Objinfo ** objlist,Link_map * lmp)47*12927SRod.Evans@Sun.COM add_object(Objinfo **objlist, Link_map *lmp)
480Sstevel@tonic-gate {
49*12927SRod.Evans@Sun.COM 	Objinfo		*op, *cur, *prev;
50*12927SRod.Evans@Sun.COM 	Elf_Ehdr	*ehdr;
51*12927SRod.Evans@Sun.COM 	Elf_Phdr	*phdr;
520Sstevel@tonic-gate 	caddr_t		lpc, hpc;
530Sstevel@tonic-gate 	int		i;
540Sstevel@tonic-gate 
55*12927SRod.Evans@Sun.COM 	if ((op = calloc(1, sizeof (Objinfo))) == NULL) {
560Sstevel@tonic-gate 		(void) fprintf(stderr, "who.so.1: calloc failed\n");
570Sstevel@tonic-gate 		exit(1);
580Sstevel@tonic-gate 	}
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 	lpc = hpc = (caddr_t)lmp->l_addr;
610Sstevel@tonic-gate 	/* LINTED */
620Sstevel@tonic-gate 	ehdr = (Elf_Ehdr *)lpc;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	/* LINTED */
650Sstevel@tonic-gate 	for (i = 0, phdr = (Elf_Phdr *)(ehdr->e_phoff + lpc);
660Sstevel@tonic-gate 	    i < ehdr->e_phnum; i++, phdr++) {
670Sstevel@tonic-gate 		caddr_t		_hpc;
680Sstevel@tonic-gate 		if ((phdr->p_type == PT_LOAD) &&
690Sstevel@tonic-gate 		    ((_hpc = phdr->p_vaddr + phdr->p_memsz + lpc) > hpc))
700Sstevel@tonic-gate 			hpc = _hpc;
710Sstevel@tonic-gate 	}
720Sstevel@tonic-gate 	op->o_lpc = lpc;
730Sstevel@tonic-gate 	op->o_hpc = hpc;
740Sstevel@tonic-gate 	op->o_lmp = lmp;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	if (ehdr->e_type == ET_EXEC)
770Sstevel@tonic-gate 		op->o_flags |= FLG_OB_FIXED;
780Sstevel@tonic-gate 
79*12927SRod.Evans@Sun.COM 	if (*objlist == NULL) {
800Sstevel@tonic-gate 		*objlist = op;
810Sstevel@tonic-gate 		return;
820Sstevel@tonic-gate 	}
830Sstevel@tonic-gate 	/*
840Sstevel@tonic-gate 	 * Do an insertion sort to maintain the list
850Sstevel@tonic-gate 	 * in order.
860Sstevel@tonic-gate 	 */
870Sstevel@tonic-gate 	if ((*objlist)->o_lmp->l_addr > lmp->l_addr) {
880Sstevel@tonic-gate 		op->o_next = *objlist;
890Sstevel@tonic-gate 		*objlist = op;
900Sstevel@tonic-gate 		return;
910Sstevel@tonic-gate 	}
920Sstevel@tonic-gate 
93*12927SRod.Evans@Sun.COM 	for (prev = NULL, cur = *objlist; cur; prev = cur, cur = cur->o_next) {
940Sstevel@tonic-gate 		if (lpc < cur->o_lpc)
950Sstevel@tonic-gate 			break;
960Sstevel@tonic-gate 	}
97*12927SRod.Evans@Sun.COM 	if (prev == NULL) {
980Sstevel@tonic-gate 		op->o_next = *objlist;
990Sstevel@tonic-gate 		*objlist = op;
1000Sstevel@tonic-gate 		return;
1010Sstevel@tonic-gate 	}
1020Sstevel@tonic-gate 	prev->o_next = op;
1030Sstevel@tonic-gate 	op->o_next = cur;
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate static void
remove_object(Objinfo ** objlist,Link_map * lmp)107*12927SRod.Evans@Sun.COM remove_object(Objinfo **objlist, Link_map *lmp)
1080Sstevel@tonic-gate {
109*12927SRod.Evans@Sun.COM 	Objinfo	*cur, *prev;
1100Sstevel@tonic-gate 
111*12927SRod.Evans@Sun.COM 	for (prev = NULL, cur = *objlist; cur; prev = cur, cur = cur->o_next) {
1120Sstevel@tonic-gate 		if (cur->o_lmp == lmp)
1130Sstevel@tonic-gate 			break;
1140Sstevel@tonic-gate 	}
115*12927SRod.Evans@Sun.COM 
1160Sstevel@tonic-gate 	/*
1170Sstevel@tonic-gate 	 * Did we find it?
1180Sstevel@tonic-gate 	 */
1190Sstevel@tonic-gate 	if (!cur)
1200Sstevel@tonic-gate 		return;
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	if (!prev)
1230Sstevel@tonic-gate 		*objlist = cur->o_next;
1240Sstevel@tonic-gate 	else
1250Sstevel@tonic-gate 		prev->o_next = cur->o_next;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	if (cur->o_elf) {
1280Sstevel@tonic-gate 		(void) elf_end(cur->o_elf);
1290Sstevel@tonic-gate 		(void) close(cur->o_fd);
1300Sstevel@tonic-gate 	}
1310Sstevel@tonic-gate 	free(cur);
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate static void
print_simple_address(void * pc)135*12927SRod.Evans@Sun.COM print_simple_address(void *pc)
1360Sstevel@tonic-gate {
1370Sstevel@tonic-gate 	Dl_info		info;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	if (dladdr(pc, &info) == 0) {
1400Sstevel@tonic-gate 		(void) fprintf(stderr,
141*12927SRod.Evans@Sun.COM 		    "\t<unknown>: 0x%lx\n", (unsigned long)pc);
1420Sstevel@tonic-gate 		return;
1430Sstevel@tonic-gate 	}
1440Sstevel@tonic-gate 
145*12927SRod.Evans@Sun.COM 	(void) fprintf(stderr, "\t%s:%s+0x%lx\n", info.dli_fname,
146*12927SRod.Evans@Sun.COM 	    info.dli_sname,
147*12927SRod.Evans@Sun.COM 	    (ulong_t)((uintptr_t)pc - (uintptr_t)info.dli_saddr));
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate static void
load_syms(Objinfo * op)151*12927SRod.Evans@Sun.COM load_syms(Objinfo *op)
1520Sstevel@tonic-gate {
153*12927SRod.Evans@Sun.COM 	int	fd;
154*12927SRod.Evans@Sun.COM 	Elf	*elf;
155*12927SRod.Evans@Sun.COM 	Elf_Scn	*scn;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	if (elf_version(EV_CURRENT) == EV_NONE) {
1580Sstevel@tonic-gate 		op->o_flags |= FLG_OB_NOSYMS;
1590Sstevel@tonic-gate 		return;
1600Sstevel@tonic-gate 	}
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	if ((fd = open(op->o_lmp->l_name, O_RDONLY)) == -1) {
1630Sstevel@tonic-gate 		op->o_flags |= FLG_OB_NOSYMS;
1640Sstevel@tonic-gate 		return;
1650Sstevel@tonic-gate 	}
1660Sstevel@tonic-gate 
167*12927SRod.Evans@Sun.COM 	if ((elf = elf_begin(fd, ELF_C_READ, 0)) == NULL) {
1680Sstevel@tonic-gate 		op->o_flags |= FLG_OB_NOSYMS;
1690Sstevel@tonic-gate 		(void) close(fd);
1700Sstevel@tonic-gate 		return;
1710Sstevel@tonic-gate 	}
172*12927SRod.Evans@Sun.COM 	scn = NULL;
173*12927SRod.Evans@Sun.COM 	while ((scn = elf_nextscn(elf, scn)) != NULL) {
174*12927SRod.Evans@Sun.COM 		Elf_Shdr	*shdr;
175*12927SRod.Evans@Sun.COM 		Elf_Data	*data;
176*12927SRod.Evans@Sun.COM 
1770Sstevel@tonic-gate 		shdr = elf_getshdr(scn);
1780Sstevel@tonic-gate 		if (shdr->sh_type != SHT_SYMTAB)
1790Sstevel@tonic-gate 			continue;
1800Sstevel@tonic-gate 		data = elf_getdata(scn, 0);
1810Sstevel@tonic-gate 		op->o_syms = (Elf_Sym *)data->d_buf;
1820Sstevel@tonic-gate 		/* LINTED */
1830Sstevel@tonic-gate 		op->o_symcnt = (uint_t)(shdr->sh_size / shdr->sh_entsize);
1840Sstevel@tonic-gate 		scn = elf_getscn(elf, shdr->sh_link);
1850Sstevel@tonic-gate 		data = elf_getdata(scn, 0);
1860Sstevel@tonic-gate 		op->o_strs = (const char *)data->d_buf;
1870Sstevel@tonic-gate 	}
1880Sstevel@tonic-gate 	if (!op->o_syms) {
1890Sstevel@tonic-gate 		(void) elf_end(elf);
1900Sstevel@tonic-gate 		(void) close(fd);
1910Sstevel@tonic-gate 		op->o_flags |= FLG_OB_NOSYMS;
1920Sstevel@tonic-gate 	}
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate static void
print_address(caddr_t pc)1970Sstevel@tonic-gate print_address(caddr_t pc)
1980Sstevel@tonic-gate {
199*12927SRod.Evans@Sun.COM 	Elf_Sym	*sym, *_sym;
200*12927SRod.Evans@Sun.COM 	Objinfo	*op;
201*12927SRod.Evans@Sun.COM 	int	i;
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	if (!detail_syms) {
2040Sstevel@tonic-gate 		print_simple_address(pc);
2050Sstevel@tonic-gate 		return;
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate 	for (op = objhead; op; op = op->o_next) {
2080Sstevel@tonic-gate 		if ((pc >= op->o_lpc) && (pc <= op->o_hpc))
2090Sstevel@tonic-gate 			break;
2100Sstevel@tonic-gate 	}
211*12927SRod.Evans@Sun.COM 	if (op && (op->o_syms == NULL))
2120Sstevel@tonic-gate 		load_syms(op);
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	if (!op || (op->o_flags & FLG_OB_NOSYMS)) {
2150Sstevel@tonic-gate 		print_simple_address(pc);
2160Sstevel@tonic-gate 		return;
2170Sstevel@tonic-gate 	}
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	sym = op->o_syms;
2200Sstevel@tonic-gate 	if ((op->o_flags & FLG_OB_FIXED) == 0)
2210Sstevel@tonic-gate 		pc = (caddr_t)((uintptr_t)pc - (uintptr_t)op->o_lpc);
2220Sstevel@tonic-gate 	for (i = 0, _sym = op->o_syms; i < op->o_symcnt; i++, _sym++) {
2230Sstevel@tonic-gate 		if (((uintptr_t)_sym->st_value < (uintptr_t)pc) &&
2240Sstevel@tonic-gate 		    (_sym->st_value > sym->st_value))
2250Sstevel@tonic-gate 			sym = _sym;
2260Sstevel@tonic-gate 	}
227*12927SRod.Evans@Sun.COM 	(void) fprintf(stderr, "\t%s:%s+0x%lx\n", op->o_lmp->l_name,
228*12927SRod.Evans@Sun.COM 	    sym->st_name + op->o_strs,
229*12927SRod.Evans@Sun.COM 	    (ulong_t)((uintptr_t)pc - (uintptr_t)sym->st_value));
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate static void
print_stack(struct frame * sp)2330Sstevel@tonic-gate print_stack(struct frame *sp)
2340Sstevel@tonic-gate {
2350Sstevel@tonic-gate 	FLUSHWIN();
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	while (sp && sp->fr_savpc) {
2380Sstevel@tonic-gate 		print_address((caddr_t)sp->fr_savpc);
2390Sstevel@tonic-gate 		sp = (struct frame *)((ulong_t)sp->fr_savfp + STACK_BIAS);
2400Sstevel@tonic-gate 	}
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate uint_t
la_version(uint_t version)2440Sstevel@tonic-gate la_version(uint_t version)
2450Sstevel@tonic-gate {
2460Sstevel@tonic-gate 	if (version > LAV_CURRENT)
2470Sstevel@tonic-gate 		(void) fprintf(stderr, "who.so: unexpected version: %d\n",
248*12927SRod.Evans@Sun.COM 		    version);
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	if (checkenv((const char *)"WHO_DETAIL"))
2510Sstevel@tonic-gate 		detail_syms++;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	build_env_list(&funclist, (const char *)"WHOCALLS");
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	/*
2560Sstevel@tonic-gate 	 * Initalize iset to the full set of signals to be masked durring
2570Sstevel@tonic-gate 	 * pltenter/pltexit
2580Sstevel@tonic-gate 	 */
2590Sstevel@tonic-gate 	(void) sigfillset(&iset);
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	return (LAV_CURRENT);
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate /* ARGSUSED1 */
2650Sstevel@tonic-gate uint_t
la_objopen(Link_map * lmp,Lmid_t lmid,uintptr_t * cookie)2660Sstevel@tonic-gate la_objopen(Link_map *lmp, Lmid_t lmid, uintptr_t *cookie)
2670Sstevel@tonic-gate {
2680Sstevel@tonic-gate 	add_object(&objhead, lmp);
2690Sstevel@tonic-gate 	return (LA_FLG_BINDTO | LA_FLG_BINDFROM);
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate uint_t
la_objclose(uintptr_t * cookie)2730Sstevel@tonic-gate la_objclose(uintptr_t *cookie)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate 	remove_object(&objhead, (Link_map *)(*cookie));
2760Sstevel@tonic-gate 	return (1);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate /* ARGSUSED1 */
2800Sstevel@tonic-gate #if	defined(__sparcv9)
2810Sstevel@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)2820Sstevel@tonic-gate la_sparcv9_pltenter(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie,
2830Sstevel@tonic-gate 	uintptr_t *defcookie, La_sparcv9_regs *regset, uint_t *sb_flags,
2840Sstevel@tonic-gate 	const char *sym_name)
2850Sstevel@tonic-gate #elif	defined(__sparc)
2860Sstevel@tonic-gate uintptr_t
2870Sstevel@tonic-gate la_sparcv8_pltenter(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcookie,
2880Sstevel@tonic-gate 	uintptr_t *defcookie, La_sparcv8_regs *regset, uint_t *sb_flags)
2890Sstevel@tonic-gate #elif   defined(__amd64)
2900Sstevel@tonic-gate uintptr_t
2910Sstevel@tonic-gate la_amd64_pltenter(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie,
2920Sstevel@tonic-gate 	uintptr_t *defcookie, La_amd64_regs *regset, uint_t *sb_flags,
2930Sstevel@tonic-gate 	const char *sym_name)
2940Sstevel@tonic-gate #elif   defined(__i386)
2950Sstevel@tonic-gate uintptr_t
2960Sstevel@tonic-gate la_i86_pltenter(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcooke,
2970Sstevel@tonic-gate 	uintptr_t *defcook, La_i86_regs *regset, uint_t *sb_flags)
2980Sstevel@tonic-gate #endif
2990Sstevel@tonic-gate {
3000Sstevel@tonic-gate 	sigset_t	oset;
3010Sstevel@tonic-gate #if	!defined(_LP64)
3020Sstevel@tonic-gate 	const char	*sym_name = (const char *)symp->st_name;
3030Sstevel@tonic-gate #endif
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &iset, &oset);
3060Sstevel@tonic-gate 	if (check_list(funclist, sym_name)) {
3070Sstevel@tonic-gate 		struct frame	*frame_p;
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 		(void) fprintf(stderr, "%s(0x%lx, 0x%lx, 0x%lx)\n", sym_name,
310*12927SRod.Evans@Sun.COM 		    (long)GETARG0(regset), (long)GETARG1(regset),
311*12927SRod.Evans@Sun.COM 		    (long)GETARG2(regset));
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 		print_address((caddr_t)GETPREVPC(regset));
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 		frame_p = (struct frame *)((ulong_t)GETFRAME(regset)
3160Sstevel@tonic-gate 		    + STACK_BIAS);
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 		print_stack(frame_p);
3190Sstevel@tonic-gate 		(void) fflush(stdout);
3200Sstevel@tonic-gate 	}
3210Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
3220Sstevel@tonic-gate 	return (symp->st_value);
3230Sstevel@tonic-gate }
324