xref: /titanic_52/usr/src/cmd/lockstat/sym.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1997-1999 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
31*7c478bd9Sstevel@tonic-gate #include <ctype.h>
32*7c478bd9Sstevel@tonic-gate #include <string.h>
33*7c478bd9Sstevel@tonic-gate #include <signal.h>
34*7c478bd9Sstevel@tonic-gate #include <errno.h>
35*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
36*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
37*7c478bd9Sstevel@tonic-gate #include <unistd.h>
38*7c478bd9Sstevel@tonic-gate #include <limits.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #include <libelf.h>
43*7c478bd9Sstevel@tonic-gate #include <link.h>
44*7c478bd9Sstevel@tonic-gate #include <elf.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/machelf.h>
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate #include <kstat.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate typedef struct syment {
51*7c478bd9Sstevel@tonic-gate 	uintptr_t	addr;
52*7c478bd9Sstevel@tonic-gate 	char		*name;
53*7c478bd9Sstevel@tonic-gate 	size_t		size;
54*7c478bd9Sstevel@tonic-gate } syment_t;
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate static syment_t *symbol_table;
57*7c478bd9Sstevel@tonic-gate static int nsyms, maxsyms;
58*7c478bd9Sstevel@tonic-gate static char maxsymname[64];
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate #ifdef _ELF64
61*7c478bd9Sstevel@tonic-gate #define	elf_getshdr elf64_getshdr
62*7c478bd9Sstevel@tonic-gate #else
63*7c478bd9Sstevel@tonic-gate #define	elf_getshdr elf32_getshdr
64*7c478bd9Sstevel@tonic-gate #endif
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate static void
67*7c478bd9Sstevel@tonic-gate add_symbol(char *name, uintptr_t addr, size_t size)
68*7c478bd9Sstevel@tonic-gate {
69*7c478bd9Sstevel@tonic-gate 	syment_t *sep;
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 	if (nsyms >= maxsyms) {
72*7c478bd9Sstevel@tonic-gate 		maxsyms += 10000;
73*7c478bd9Sstevel@tonic-gate 		symbol_table = realloc(symbol_table, maxsyms * sizeof (*sep));
74*7c478bd9Sstevel@tonic-gate 		if (symbol_table == NULL) {
75*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "can't allocate symbol table\n");
76*7c478bd9Sstevel@tonic-gate 			exit(3);
77*7c478bd9Sstevel@tonic-gate 		}
78*7c478bd9Sstevel@tonic-gate 	}
79*7c478bd9Sstevel@tonic-gate 	sep = &symbol_table[nsyms++];
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 	sep->name = name;
82*7c478bd9Sstevel@tonic-gate 	sep->addr = addr;
83*7c478bd9Sstevel@tonic-gate 	sep->size = size;
84*7c478bd9Sstevel@tonic-gate }
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate static void
87*7c478bd9Sstevel@tonic-gate remove_symbol(uintptr_t addr)
88*7c478bd9Sstevel@tonic-gate {
89*7c478bd9Sstevel@tonic-gate 	int i;
90*7c478bd9Sstevel@tonic-gate 	syment_t *sep = symbol_table;
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nsyms; i++, sep++)
93*7c478bd9Sstevel@tonic-gate 		if (sep->addr == addr)
94*7c478bd9Sstevel@tonic-gate 			sep->addr = 0;
95*7c478bd9Sstevel@tonic-gate }
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate static void
98*7c478bd9Sstevel@tonic-gate fake_up_certain_popular_kernel_symbols(void)
99*7c478bd9Sstevel@tonic-gate {
100*7c478bd9Sstevel@tonic-gate 	kstat_ctl_t *kc;
101*7c478bd9Sstevel@tonic-gate 	kstat_t *ksp;
102*7c478bd9Sstevel@tonic-gate 	char *name;
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	if ((kc = kstat_open()) == NULL)
105*7c478bd9Sstevel@tonic-gate 		return;
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
108*7c478bd9Sstevel@tonic-gate 		if (strcmp(ksp->ks_module, "cpu_info") == 0) {
109*7c478bd9Sstevel@tonic-gate 			if ((name = malloc(20)) == NULL)
110*7c478bd9Sstevel@tonic-gate 				break;
111*7c478bd9Sstevel@tonic-gate 			/*
112*7c478bd9Sstevel@tonic-gate 			 * For consistency, keep cpu[0] and toss cpu0
113*7c478bd9Sstevel@tonic-gate 			 * or any other such symbols.
114*7c478bd9Sstevel@tonic-gate 			 */
115*7c478bd9Sstevel@tonic-gate 			if (ksp->ks_instance == 0)
116*7c478bd9Sstevel@tonic-gate 				remove_symbol((uintptr_t)ksp->ks_private);
117*7c478bd9Sstevel@tonic-gate 			(void) sprintf(name, "cpu[%d]", ksp->ks_instance);
118*7c478bd9Sstevel@tonic-gate 			add_symbol(name, (uintptr_t)ksp->ks_private,
119*7c478bd9Sstevel@tonic-gate 			    sizeof (struct cpu));
120*7c478bd9Sstevel@tonic-gate 		}
121*7c478bd9Sstevel@tonic-gate 	}
122*7c478bd9Sstevel@tonic-gate 	(void) kstat_close(kc);
123*7c478bd9Sstevel@tonic-gate }
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate static int
126*7c478bd9Sstevel@tonic-gate symcmp(const void *p1, const void *p2)
127*7c478bd9Sstevel@tonic-gate {
128*7c478bd9Sstevel@tonic-gate 	uintptr_t a1 = ((syment_t *)p1)->addr;
129*7c478bd9Sstevel@tonic-gate 	uintptr_t a2 = ((syment_t *)p2)->addr;
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	if (a1 < a2)
132*7c478bd9Sstevel@tonic-gate 		return (-1);
133*7c478bd9Sstevel@tonic-gate 	if (a1 > a2)
134*7c478bd9Sstevel@tonic-gate 		return (1);
135*7c478bd9Sstevel@tonic-gate 	return (0);
136*7c478bd9Sstevel@tonic-gate }
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate int
139*7c478bd9Sstevel@tonic-gate symtab_init(void)
140*7c478bd9Sstevel@tonic-gate {
141*7c478bd9Sstevel@tonic-gate 	Elf		*elf;
142*7c478bd9Sstevel@tonic-gate 	Elf_Scn		*scn = NULL;
143*7c478bd9Sstevel@tonic-gate 	Sym		*symtab, *symp, *lastsym;
144*7c478bd9Sstevel@tonic-gate 	char		*strtab;
145*7c478bd9Sstevel@tonic-gate 	uint_t		cnt;
146*7c478bd9Sstevel@tonic-gate 	int		fd;
147*7c478bd9Sstevel@tonic-gate 	int		i;
148*7c478bd9Sstevel@tonic-gate 	int		strindex = -1;
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 	if ((fd = open("/dev/ksyms", O_RDONLY)) == -1)
151*7c478bd9Sstevel@tonic-gate 		return (-1);
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 	(void) elf_version(EV_CURRENT);
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	elf = elf_begin(fd, ELF_C_READ, NULL);
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 	for (cnt = 1; (scn = elf_nextscn(elf, scn)) != NULL; cnt++) {
158*7c478bd9Sstevel@tonic-gate 		Shdr *shdr = elf_getshdr(scn);
159*7c478bd9Sstevel@tonic-gate 		if (shdr->sh_type == SHT_SYMTAB) {
160*7c478bd9Sstevel@tonic-gate 			symtab = (Sym *)elf_getdata(scn, NULL)->d_buf;
161*7c478bd9Sstevel@tonic-gate 			nsyms = shdr->sh_size / shdr->sh_entsize;
162*7c478bd9Sstevel@tonic-gate 			strindex = shdr->sh_link;
163*7c478bd9Sstevel@tonic-gate 		}
164*7c478bd9Sstevel@tonic-gate 	}
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 	for (cnt = 1; (scn = elf_nextscn(elf, scn)) != NULL; cnt++) {
167*7c478bd9Sstevel@tonic-gate 		if (cnt == strindex)
168*7c478bd9Sstevel@tonic-gate 			strtab = (char *)elf_getdata(scn, NULL)->d_buf;
169*7c478bd9Sstevel@tonic-gate 	}
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate 	lastsym = symtab + nsyms;
172*7c478bd9Sstevel@tonic-gate 	nsyms = 0;
173*7c478bd9Sstevel@tonic-gate 	for (symp = symtab; symp < lastsym; symp++)
174*7c478bd9Sstevel@tonic-gate 		if ((uint_t)ELF32_ST_TYPE(symp->st_info) <= STT_FUNC &&
175*7c478bd9Sstevel@tonic-gate 		    symp->st_size != 0)
176*7c478bd9Sstevel@tonic-gate 			add_symbol(symp->st_name + strtab,
177*7c478bd9Sstevel@tonic-gate 			    (uintptr_t)symp->st_value, (size_t)symp->st_size);
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 	fake_up_certain_popular_kernel_symbols();
180*7c478bd9Sstevel@tonic-gate 	(void) sprintf(maxsymname, "0x%lx", ULONG_MAX);
181*7c478bd9Sstevel@tonic-gate 	add_symbol(maxsymname, ULONG_MAX, 1);
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate 	qsort(symbol_table, nsyms, sizeof (syment_t), symcmp);
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 	/*
186*7c478bd9Sstevel@tonic-gate 	 * Destroy all duplicate symbols, then sort it again.
187*7c478bd9Sstevel@tonic-gate 	 */
188*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nsyms - 1; i++)
189*7c478bd9Sstevel@tonic-gate 		if (symbol_table[i].addr == symbol_table[i + 1].addr)
190*7c478bd9Sstevel@tonic-gate 			symbol_table[i].addr = 0;
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 	qsort(symbol_table, nsyms, sizeof (syment_t), symcmp);
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 	while (symbol_table[1].addr == 0) {
195*7c478bd9Sstevel@tonic-gate 		symbol_table++;
196*7c478bd9Sstevel@tonic-gate 		nsyms--;
197*7c478bd9Sstevel@tonic-gate 	}
198*7c478bd9Sstevel@tonic-gate 	symbol_table[0].name = "(usermode)";
199*7c478bd9Sstevel@tonic-gate 	symbol_table[0].addr = 0;
200*7c478bd9Sstevel@tonic-gate 	symbol_table[0].size = 1;
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 	return (0);
203*7c478bd9Sstevel@tonic-gate }
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate char *
206*7c478bd9Sstevel@tonic-gate addr_to_sym(uintptr_t addr, uintptr_t *offset, size_t *sizep)
207*7c478bd9Sstevel@tonic-gate {
208*7c478bd9Sstevel@tonic-gate 	int lo = 0;
209*7c478bd9Sstevel@tonic-gate 	int hi = nsyms - 1;
210*7c478bd9Sstevel@tonic-gate 	int mid;
211*7c478bd9Sstevel@tonic-gate 	syment_t *sep;
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 	while (hi - lo > 1) {
214*7c478bd9Sstevel@tonic-gate 		mid = (lo + hi) / 2;
215*7c478bd9Sstevel@tonic-gate 		if (addr >= symbol_table[mid].addr) {
216*7c478bd9Sstevel@tonic-gate 			lo = mid;
217*7c478bd9Sstevel@tonic-gate 		} else {
218*7c478bd9Sstevel@tonic-gate 			hi = mid;
219*7c478bd9Sstevel@tonic-gate 		}
220*7c478bd9Sstevel@tonic-gate 	}
221*7c478bd9Sstevel@tonic-gate 	sep = &symbol_table[lo];
222*7c478bd9Sstevel@tonic-gate 	*offset = addr - sep->addr;
223*7c478bd9Sstevel@tonic-gate 	*sizep = sep->size;
224*7c478bd9Sstevel@tonic-gate 	return (sep->name);
225*7c478bd9Sstevel@tonic-gate }
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate uintptr_t
228*7c478bd9Sstevel@tonic-gate sym_to_addr(char *name)
229*7c478bd9Sstevel@tonic-gate {
230*7c478bd9Sstevel@tonic-gate 	int i;
231*7c478bd9Sstevel@tonic-gate 	syment_t *sep = symbol_table;
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nsyms; i++) {
234*7c478bd9Sstevel@tonic-gate 		if (strcmp(name, sep->name) == 0)
235*7c478bd9Sstevel@tonic-gate 			return (sep->addr);
236*7c478bd9Sstevel@tonic-gate 		sep++;
237*7c478bd9Sstevel@tonic-gate 	}
238*7c478bd9Sstevel@tonic-gate 	return (NULL);
239*7c478bd9Sstevel@tonic-gate }
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate size_t
242*7c478bd9Sstevel@tonic-gate sym_size(char *name)
243*7c478bd9Sstevel@tonic-gate {
244*7c478bd9Sstevel@tonic-gate 	int i;
245*7c478bd9Sstevel@tonic-gate 	syment_t *sep = symbol_table;
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nsyms; i++) {
248*7c478bd9Sstevel@tonic-gate 		if (strcmp(name, sep->name) == 0)
249*7c478bd9Sstevel@tonic-gate 			return (sep->size);
250*7c478bd9Sstevel@tonic-gate 		sep++;
251*7c478bd9Sstevel@tonic-gate 	}
252*7c478bd9Sstevel@tonic-gate 	return (0);
253*7c478bd9Sstevel@tonic-gate }
254