dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / lockstat / sym.c
blob2898b95affc78610ca5c1a806d287e84a3dbaeb1
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright (c) 1997-1999 by Sun Microsystems, Inc.
24 * All rights reserved.
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>
40 #include <libelf.h>
41 #include <link.h>
42 #include <elf.h>
43 #include <sys/machelf.h>
45 #include <kstat.h>
46 #include <sys/cpuvar.h>
48 typedef struct syment {
49 uintptr_t addr;
50 char *name;
51 size_t size;
52 } syment_t;
54 static syment_t *symbol_table;
55 static int nsyms, maxsyms;
56 static char maxsymname[64];
58 #ifdef _ELF64
59 #define elf_getshdr elf64_getshdr
60 #else
61 #define elf_getshdr elf32_getshdr
62 #endif
64 static void
65 add_symbol(char *name, uintptr_t addr, size_t size)
67 syment_t *sep;
69 if (nsyms >= maxsyms) {
70 maxsyms += 10000;
71 symbol_table = reallocarray(symbol_table, maxsyms,
72 sizeof (*sep));
73 if (symbol_table == NULL) {
74 (void) fprintf(stderr, "can't allocate symbol table\n");
75 exit(3);
78 sep = &symbol_table[nsyms++];
80 sep->name = name;
81 sep->addr = addr;
82 sep->size = size;
85 static void
86 remove_symbol(uintptr_t addr)
88 int i;
89 syment_t *sep = symbol_table;
91 for (i = 0; i < nsyms; i++, sep++)
92 if (sep->addr == addr)
93 sep->addr = 0;
96 static void
97 fake_up_certain_popular_kernel_symbols(void)
99 kstat_ctl_t *kc;
100 kstat_t *ksp;
101 char *name;
103 if ((kc = kstat_open()) == NULL)
104 return;
106 for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
107 if (strcmp(ksp->ks_module, "cpu_info") == 0) {
108 if ((name = malloc(20)) == NULL)
109 break;
111 * For consistency, keep cpu[0] and toss cpu0
112 * or any other such symbols.
114 if (ksp->ks_instance == 0)
115 remove_symbol((uintptr_t)ksp->ks_private);
116 (void) sprintf(name, "cpu[%d]", ksp->ks_instance);
117 add_symbol(name, (uintptr_t)ksp->ks_private,
118 sizeof (struct cpu));
121 (void) kstat_close(kc);
124 static int
125 symcmp(const void *p1, const void *p2)
127 uintptr_t a1 = ((syment_t *)p1)->addr;
128 uintptr_t a2 = ((syment_t *)p2)->addr;
130 if (a1 < a2)
131 return (-1);
132 if (a1 > a2)
133 return (1);
134 return (0);
138 symtab_init(void)
140 Elf *elf;
141 Elf_Scn *scn = NULL;
142 Sym *symtab, *symp, *lastsym;
143 char *strtab;
144 uint_t cnt;
145 int fd;
146 int i;
147 int strindex = -1;
149 if ((fd = open("/dev/ksyms", O_RDONLY)) == -1)
150 return (-1);
152 (void) elf_version(EV_CURRENT);
154 elf = elf_begin(fd, ELF_C_READ, NULL);
156 for (cnt = 1; (scn = elf_nextscn(elf, scn)) != NULL; cnt++) {
157 Shdr *shdr = elf_getshdr(scn);
158 if (shdr->sh_type == SHT_SYMTAB) {
159 symtab = (Sym *)elf_getdata(scn, NULL)->d_buf;
160 nsyms = shdr->sh_size / shdr->sh_entsize;
161 strindex = shdr->sh_link;
165 for (cnt = 1; (scn = elf_nextscn(elf, scn)) != NULL; cnt++) {
166 if (cnt == strindex)
167 strtab = (char *)elf_getdata(scn, NULL)->d_buf;
170 lastsym = symtab + nsyms;
171 nsyms = 0;
172 for (symp = symtab; symp < lastsym; symp++)
173 if ((uint_t)ELF32_ST_TYPE(symp->st_info) <= STT_FUNC &&
174 symp->st_size != 0)
175 add_symbol(symp->st_name + strtab,
176 (uintptr_t)symp->st_value, (size_t)symp->st_size);
178 fake_up_certain_popular_kernel_symbols();
179 (void) sprintf(maxsymname, "0x%lx", ULONG_MAX);
180 add_symbol(maxsymname, ULONG_MAX, 1);
182 qsort(symbol_table, nsyms, sizeof (syment_t), symcmp);
185 * Destroy all duplicate symbols, then sort it again.
187 for (i = 0; i < nsyms - 1; i++)
188 if (symbol_table[i].addr == symbol_table[i + 1].addr)
189 symbol_table[i].addr = 0;
191 qsort(symbol_table, nsyms, sizeof (syment_t), symcmp);
193 while (symbol_table[1].addr == 0) {
194 symbol_table++;
195 nsyms--;
197 symbol_table[0].name = "(usermode)";
198 symbol_table[0].addr = 0;
199 symbol_table[0].size = 1;
201 return (0);
204 char *
205 addr_to_sym(uintptr_t addr, uintptr_t *offset, size_t *sizep)
207 int lo = 0;
208 int hi = nsyms - 1;
209 int mid;
210 syment_t *sep;
212 while (hi - lo > 1) {
213 mid = (lo + hi) / 2;
214 if (addr >= symbol_table[mid].addr) {
215 lo = mid;
216 } else {
217 hi = mid;
220 sep = &symbol_table[lo];
221 *offset = addr - sep->addr;
222 *sizep = sep->size;
223 return (sep->name);
226 uintptr_t
227 sym_to_addr(char *name)
229 int i;
230 syment_t *sep = symbol_table;
232 for (i = 0; i < nsyms; i++) {
233 if (strcmp(name, sep->name) == 0)
234 return (sep->addr);
235 sep++;
237 return (0);
240 size_t
241 sym_size(char *name)
243 int i;
244 syment_t *sep = symbol_table;
246 for (i = 0; i < nsyms; i++) {
247 if (strcmp(name, sep->name) == 0)
248 return (sep->size);
249 sep++;
251 return (0);