xref: /titanic_51/usr/src/cmd/sgs/link_audit/common/who.c (revision 5a7763bf3e9db4cfe6cb523b096cb74af71e3793)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include <fcntl.h>
31 #include <link.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <strings.h>
35 #include <sys/regset.h>
36 #include <sys/frame.h>
37 #include <sys/stack.h>
38 #include <signal.h>
39 
40 #include "env.h"
41 #include "mach.h"
42 #include "who.h"
43 
44 
45 static int		detail_syms = 0; /* display detail symbol information */
46 static Objinfo *	objhead = 0;	/* head of object list */
47 static Elist *		funclist = 0;
48 static sigset_t		iset;
49 
50 
51 static void
52 add_object(Objinfo ** objlist, Link_map * lmp)
53 {
54 	Objinfo *	op, * cur, * prev;
55 	Elf_Ehdr *	ehdr;
56 	Elf_Phdr *	phdr;
57 	caddr_t		lpc, hpc;
58 	int		i;
59 
60 	if ((op = calloc(1, sizeof (Objinfo))) == 0) {
61 		(void) fprintf(stderr, "who.so.1: calloc failed\n");
62 		exit(1);
63 	}
64 
65 	lpc = hpc = (caddr_t)lmp->l_addr;
66 	/* LINTED */
67 	ehdr = (Elf_Ehdr *)lpc;
68 
69 	/* LINTED */
70 	for (i = 0, phdr = (Elf_Phdr *)(ehdr->e_phoff + lpc);
71 	    i < ehdr->e_phnum; i++, phdr++) {
72 		caddr_t		_hpc;
73 		if ((phdr->p_type == PT_LOAD) &&
74 		    ((_hpc = phdr->p_vaddr + phdr->p_memsz + lpc) > hpc))
75 			hpc = _hpc;
76 	}
77 	op->o_lpc = lpc;
78 	op->o_hpc = hpc;
79 	op->o_lmp = lmp;
80 
81 
82 	if (ehdr->e_type == ET_EXEC)
83 		op->o_flags |= FLG_OB_FIXED;
84 
85 	if (*objlist == 0) {
86 		*objlist = op;
87 		return;
88 	}
89 	/*
90 	 * Do an insertion sort to maintain the list
91 	 * in order.
92 	 */
93 	if ((*objlist)->o_lmp->l_addr > lmp->l_addr) {
94 		op->o_next = *objlist;
95 		*objlist = op;
96 		return;
97 	}
98 
99 	for (prev = 0, cur = *objlist; cur; prev = cur, cur = cur->o_next) {
100 		if (lpc < cur->o_lpc)
101 			break;
102 	}
103 	if (prev == 0) {
104 		op->o_next = *objlist;
105 		*objlist = op;
106 		return;
107 	}
108 	prev->o_next = op;
109 	op->o_next = cur;
110 }
111 
112 static void
113 remove_object(Objinfo ** objlist, Link_map * lmp)
114 {
115 	Objinfo *	cur, * prev;
116 
117 	for (prev = 0, cur = *objlist; cur; prev = cur, cur = cur->o_next) {
118 		if (cur->o_lmp == lmp)
119 			break;
120 	}
121 	/*
122 	 * Did we find it?
123 	 */
124 	if (!cur)
125 		return;
126 
127 	if (!prev)
128 		*objlist = cur->o_next;
129 	else
130 		prev->o_next = cur->o_next;
131 
132 	if (cur->o_elf) {
133 		(void) elf_end(cur->o_elf);
134 		(void) close(cur->o_fd);
135 	}
136 	free(cur);
137 }
138 
139 static void
140 print_simple_address(void * pc)
141 {
142 	Dl_info		info;
143 
144 	if (dladdr(pc, &info) == 0) {
145 		(void) fprintf(stderr,
146 			"\t<unknown>: 0x%lx\n", (unsigned long)pc);
147 		return;
148 	}
149 
150 	(void) fprintf(stderr, "\t%s:%s+0x%lx\n",
151 		info.dli_fname, info.dli_sname,
152 		(ulong_t)((uintptr_t)pc - (uintptr_t)info.dli_saddr));
153 }
154 
155 static void
156 load_syms(Objinfo * op)
157 {
158 	int		fd;
159 	Elf *		elf;
160 	Elf_Scn *	scn;
161 
162 	if (elf_version(EV_CURRENT) == EV_NONE) {
163 		op->o_flags |= FLG_OB_NOSYMS;
164 		return;
165 	}
166 
167 	if ((fd = open(op->o_lmp->l_name, O_RDONLY)) == -1) {
168 		op->o_flags |= FLG_OB_NOSYMS;
169 		return;
170 	}
171 
172 	if ((elf = elf_begin(fd, ELF_C_READ, 0)) == 0) {
173 		op->o_flags |= FLG_OB_NOSYMS;
174 		(void) close(fd);
175 		return;
176 	}
177 	scn = 0;
178 	while ((scn = elf_nextscn(elf, scn)) != 0) {
179 		Elf_Shdr *	shdr;
180 		Elf_Data *	data;
181 		shdr = elf_getshdr(scn);
182 		if (shdr->sh_type != SHT_SYMTAB)
183 			continue;
184 		data = elf_getdata(scn, 0);
185 		op->o_syms = (Elf_Sym *)data->d_buf;
186 		/* LINTED */
187 		op->o_symcnt = (uint_t)(shdr->sh_size / shdr->sh_entsize);
188 		scn = elf_getscn(elf, shdr->sh_link);
189 		data = elf_getdata(scn, 0);
190 		op->o_strs = (const char *)data->d_buf;
191 	}
192 	if (!op->o_syms) {
193 		(void) elf_end(elf);
194 		(void) close(fd);
195 		op->o_flags |= FLG_OB_NOSYMS;
196 	}
197 }
198 
199 
200 static void
201 print_address(caddr_t pc)
202 {
203 	Elf_Sym *	sym, * _sym;
204 	Objinfo *	op;
205 	int		i;
206 
207 	if (!detail_syms) {
208 		print_simple_address(pc);
209 		return;
210 	}
211 	for (op = objhead; op; op = op->o_next) {
212 		if ((pc >= op->o_lpc) && (pc <= op->o_hpc))
213 			break;
214 	}
215 	if (op && (op->o_syms == 0))
216 		load_syms(op);
217 
218 	if (!op || (op->o_flags & FLG_OB_NOSYMS)) {
219 		print_simple_address(pc);
220 		return;
221 	}
222 
223 	sym = op->o_syms;
224 	if ((op->o_flags & FLG_OB_FIXED) == 0)
225 		pc = (caddr_t)((uintptr_t)pc - (uintptr_t)op->o_lpc);
226 	for (i = 0, _sym = op->o_syms; i < op->o_symcnt; i++, _sym++) {
227 		if (((uintptr_t)_sym->st_value < (uintptr_t)pc) &&
228 		    (_sym->st_value > sym->st_value))
229 			sym = _sym;
230 	}
231 	(void) fprintf(stderr, "\t%s:%s+0x%lx\n",
232 		op->o_lmp->l_name, sym->st_name + op->o_strs,
233 		(ulong_t)((uintptr_t)pc - (uintptr_t)sym->st_value));
234 }
235 
236 static void
237 print_stack(struct frame *sp)
238 {
239 	FLUSHWIN();
240 
241 	while (sp && sp->fr_savpc) {
242 		print_address((caddr_t)sp->fr_savpc);
243 		sp = (struct frame *)((ulong_t)sp->fr_savfp + STACK_BIAS);
244 	}
245 }
246 
247 uint_t
248 la_version(uint_t version)
249 {
250 	if (version > LAV_CURRENT)
251 		(void) fprintf(stderr, "who.so: unexpected version: %d\n",
252 			version);
253 
254 	if (checkenv((const char *)"WHO_DETAIL"))
255 		detail_syms++;
256 
257 	build_env_list(&funclist, (const char *)"WHOCALLS");
258 
259 	/*
260 	 * Initalize iset to the full set of signals to be masked durring
261 	 * pltenter/pltexit
262 	 */
263 	(void) sigfillset(&iset);
264 
265 	return (LAV_CURRENT);
266 }
267 
268 /* ARGSUSED1 */
269 uint_t
270 la_objopen(Link_map *lmp, Lmid_t lmid, uintptr_t *cookie)
271 {
272 	add_object(&objhead, lmp);
273 	return (LA_FLG_BINDTO | LA_FLG_BINDFROM);
274 }
275 
276 
277 uint_t
278 la_objclose(uintptr_t *cookie)
279 {
280 	remove_object(&objhead, (Link_map *)(*cookie));
281 	return (1);
282 }
283 
284 
285 /* ARGSUSED1 */
286 #if	defined(__sparcv9)
287 uintptr_t
288 la_sparcv9_pltenter(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie,
289 	uintptr_t *defcookie, La_sparcv9_regs *regset, uint_t *sb_flags,
290 	const char *sym_name)
291 #elif	defined(__sparc)
292 uintptr_t
293 la_sparcv8_pltenter(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcookie,
294 	uintptr_t *defcookie, La_sparcv8_regs *regset, uint_t *sb_flags)
295 #elif   defined(__amd64)
296 uintptr_t
297 la_amd64_pltenter(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie,
298 	uintptr_t *defcookie, La_amd64_regs *regset, uint_t *sb_flags,
299 	const char *sym_name)
300 #elif   defined(__i386)
301 uintptr_t
302 la_i86_pltenter(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcooke,
303 	uintptr_t *defcook, La_i86_regs *regset, uint_t *sb_flags)
304 #endif
305 {
306 	sigset_t	oset;
307 #if	!defined(_LP64)
308 	const char	*sym_name = (const char *)symp->st_name;
309 #endif
310 
311 	(void) sigprocmask(SIG_BLOCK, &iset, &oset);
312 	if (check_list(funclist, sym_name)) {
313 		struct frame	*frame_p;
314 
315 		(void) fprintf(stderr, "%s(0x%lx, 0x%lx, 0x%lx)\n", sym_name,
316 			(long)GETARG0(regset), (long)GETARG1(regset),
317 			(long)GETARG2(regset));
318 
319 		print_address((caddr_t)GETPREVPC(regset));
320 
321 		frame_p = (struct frame *)((ulong_t)GETFRAME(regset)
322 		    + STACK_BIAS);
323 
324 		print_stack(frame_p);
325 		(void) fflush(stdout);
326 	}
327 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
328 	return (symp->st_value);
329 }
330