xref: /illumos-gate/usr/src/cmd/lockstat/sym.c (revision 5328fc53d11d7151861fa272e4fb0248b8f0e145)
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 (c) 1997-1999 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #include <stdio.h>
28 #include <fcntl.h>
29 #include <ctype.h>
30 #include <string.h>
31 #include <signal.h>
32 #include <errno.h>
33 #include <stdlib.h>
34 #include <stdarg.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 
40 #include <libelf.h>
41 #include <link.h>
42 #include <elf.h>
43 #include <sys/machelf.h>
44 
45 #include <kstat.h>
46 #include <sys/cpuvar.h>
47 
48 typedef struct syment {
49 	uintptr_t	addr;
50 	char		*name;
51 	size_t		size;
52 } syment_t;
53 
54 static syment_t *symbol_table;
55 static int nsyms, maxsyms;
56 static char maxsymname[64];
57 
58 #ifdef _ELF64
59 #define	elf_getshdr elf64_getshdr
60 #else
61 #define	elf_getshdr elf32_getshdr
62 #endif
63 
64 static void
65 add_symbol(char *name, uintptr_t addr, size_t size)
66 {
67 	syment_t *sep;
68 
69 	if (nsyms >= maxsyms) {
70 		maxsyms += 10000;
71 		symbol_table = realloc(symbol_table, maxsyms * sizeof (*sep));
72 		if (symbol_table == NULL) {
73 			(void) fprintf(stderr, "can't allocate symbol table\n");
74 			exit(3);
75 		}
76 	}
77 	sep = &symbol_table[nsyms++];
78 
79 	sep->name = name;
80 	sep->addr = addr;
81 	sep->size = size;
82 }
83 
84 static void
85 remove_symbol(uintptr_t addr)
86 {
87 	int i;
88 	syment_t *sep = symbol_table;
89 
90 	for (i = 0; i < nsyms; i++, sep++)
91 		if (sep->addr == addr)
92 			sep->addr = 0;
93 }
94 
95 static void
96 fake_up_certain_popular_kernel_symbols(void)
97 {
98 	kstat_ctl_t *kc;
99 	kstat_t *ksp;
100 	char *name;
101 
102 	if ((kc = kstat_open()) == NULL)
103 		return;
104 
105 	for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
106 		if (strcmp(ksp->ks_module, "cpu_info") == 0) {
107 			if ((name = malloc(20)) == NULL)
108 				break;
109 			/*
110 			 * For consistency, keep cpu[0] and toss cpu0
111 			 * or any other such symbols.
112 			 */
113 			if (ksp->ks_instance == 0)
114 				remove_symbol((uintptr_t)ksp->ks_private);
115 			(void) sprintf(name, "cpu[%d]", ksp->ks_instance);
116 			add_symbol(name, (uintptr_t)ksp->ks_private,
117 			    sizeof (struct cpu));
118 		}
119 	}
120 	(void) kstat_close(kc);
121 }
122 
123 static int
124 symcmp(const void *p1, const void *p2)
125 {
126 	uintptr_t a1 = ((syment_t *)p1)->addr;
127 	uintptr_t a2 = ((syment_t *)p2)->addr;
128 
129 	if (a1 < a2)
130 		return (-1);
131 	if (a1 > a2)
132 		return (1);
133 	return (0);
134 }
135 
136 int
137 symtab_init(void)
138 {
139 	Elf		*elf;
140 	Elf_Scn		*scn = NULL;
141 	Sym		*symtab, *symp, *lastsym;
142 	char		*strtab;
143 	uint_t		cnt;
144 	int		fd;
145 	int		i;
146 	int		strindex = -1;
147 
148 	if ((fd = open("/dev/ksyms", O_RDONLY)) == -1)
149 		return (-1);
150 
151 	(void) elf_version(EV_CURRENT);
152 
153 	elf = elf_begin(fd, ELF_C_READ, NULL);
154 
155 	for (cnt = 1; (scn = elf_nextscn(elf, scn)) != NULL; cnt++) {
156 		Shdr *shdr = elf_getshdr(scn);
157 		if (shdr->sh_type == SHT_SYMTAB) {
158 			symtab = (Sym *)elf_getdata(scn, NULL)->d_buf;
159 			nsyms = shdr->sh_size / shdr->sh_entsize;
160 			strindex = shdr->sh_link;
161 		}
162 	}
163 
164 	for (cnt = 1; (scn = elf_nextscn(elf, scn)) != NULL; cnt++) {
165 		if (cnt == strindex)
166 			strtab = (char *)elf_getdata(scn, NULL)->d_buf;
167 	}
168 
169 	lastsym = symtab + nsyms;
170 	nsyms = 0;
171 	for (symp = symtab; symp < lastsym; symp++)
172 		if ((uint_t)ELF32_ST_TYPE(symp->st_info) <= STT_FUNC &&
173 		    symp->st_size != 0)
174 			add_symbol(symp->st_name + strtab,
175 			    (uintptr_t)symp->st_value, (size_t)symp->st_size);
176 
177 	fake_up_certain_popular_kernel_symbols();
178 	(void) sprintf(maxsymname, "0x%lx", ULONG_MAX);
179 	add_symbol(maxsymname, ULONG_MAX, 1);
180 
181 	qsort(symbol_table, nsyms, sizeof (syment_t), symcmp);
182 
183 	/*
184 	 * Destroy all duplicate symbols, then sort it again.
185 	 */
186 	for (i = 0; i < nsyms - 1; i++)
187 		if (symbol_table[i].addr == symbol_table[i + 1].addr)
188 			symbol_table[i].addr = 0;
189 
190 	qsort(symbol_table, nsyms, sizeof (syment_t), symcmp);
191 
192 	while (symbol_table[1].addr == 0) {
193 		symbol_table++;
194 		nsyms--;
195 	}
196 	symbol_table[0].name = "(usermode)";
197 	symbol_table[0].addr = 0;
198 	symbol_table[0].size = 1;
199 
200 	return (0);
201 }
202 
203 char *
204 addr_to_sym(uintptr_t addr, uintptr_t *offset, size_t *sizep)
205 {
206 	int lo = 0;
207 	int hi = nsyms - 1;
208 	int mid;
209 	syment_t *sep;
210 
211 	while (hi - lo > 1) {
212 		mid = (lo + hi) / 2;
213 		if (addr >= symbol_table[mid].addr) {
214 			lo = mid;
215 		} else {
216 			hi = mid;
217 		}
218 	}
219 	sep = &symbol_table[lo];
220 	*offset = addr - sep->addr;
221 	*sizep = sep->size;
222 	return (sep->name);
223 }
224 
225 uintptr_t
226 sym_to_addr(char *name)
227 {
228 	int i;
229 	syment_t *sep = symbol_table;
230 
231 	for (i = 0; i < nsyms; i++) {
232 		if (strcmp(name, sep->name) == 0)
233 			return (sep->addr);
234 		sep++;
235 	}
236 	return ((uintptr_t)NULL);
237 }
238 
239 size_t
240 sym_size(char *name)
241 {
242 	int i;
243 	syment_t *sep = symbol_table;
244 
245 	for (i = 0; i < nsyms; i++) {
246 		if (strcmp(name, sep->name) == 0)
247 			return (sep->size);
248 		sep++;
249 	}
250 	return (0);
251 }
252