Fix NULL pointer constant issues in cmd
[unleashed.git] / usr / src / cmd / lockstat / sym.c
blob03edb9e566a2a7e8b8d187c323a0a049d823c375
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 = realloc(symbol_table, maxsyms * sizeof (*sep));
72 if (symbol_table == NULL) {
73 (void) fprintf(stderr, "can't allocate symbol table\n");
74 exit(3);
77 sep = &symbol_table[nsyms++];
79 sep->name = name;
80 sep->addr = addr;
81 sep->size = size;
84 static void
85 remove_symbol(uintptr_t addr)
87 int i;
88 syment_t *sep = symbol_table;
90 for (i = 0; i < nsyms; i++, sep++)
91 if (sep->addr == addr)
92 sep->addr = 0;
95 static void
96 fake_up_certain_popular_kernel_symbols(void)
98 kstat_ctl_t *kc;
99 kstat_t *ksp;
100 char *name;
102 if ((kc = kstat_open()) == NULL)
103 return;
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;
110 * For consistency, keep cpu[0] and toss cpu0
111 * or any other such symbols.
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));
120 (void) kstat_close(kc);
123 static int
124 symcmp(const void *p1, const void *p2)
126 uintptr_t a1 = ((syment_t *)p1)->addr;
127 uintptr_t a2 = ((syment_t *)p2)->addr;
129 if (a1 < a2)
130 return (-1);
131 if (a1 > a2)
132 return (1);
133 return (0);
137 symtab_init(void)
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;
148 if ((fd = open("/dev/ksyms", O_RDONLY)) == -1)
149 return (-1);
151 (void) elf_version(EV_CURRENT);
153 elf = elf_begin(fd, ELF_C_READ, NULL);
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;
164 for (cnt = 1; (scn = elf_nextscn(elf, scn)) != NULL; cnt++) {
165 if (cnt == strindex)
166 strtab = (char *)elf_getdata(scn, NULL)->d_buf;
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);
177 fake_up_certain_popular_kernel_symbols();
178 (void) sprintf(maxsymname, "0x%lx", ULONG_MAX);
179 add_symbol(maxsymname, ULONG_MAX, 1);
181 qsort(symbol_table, nsyms, sizeof (syment_t), symcmp);
184 * Destroy all duplicate symbols, then sort it again.
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;
190 qsort(symbol_table, nsyms, sizeof (syment_t), symcmp);
192 while (symbol_table[1].addr == 0) {
193 symbol_table++;
194 nsyms--;
196 symbol_table[0].name = "(usermode)";
197 symbol_table[0].addr = 0;
198 symbol_table[0].size = 1;
200 return (0);
203 char *
204 addr_to_sym(uintptr_t addr, uintptr_t *offset, size_t *sizep)
206 int lo = 0;
207 int hi = nsyms - 1;
208 int mid;
209 syment_t *sep;
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;
219 sep = &symbol_table[lo];
220 *offset = addr - sep->addr;
221 *sizep = sep->size;
222 return (sep->name);
225 uintptr_t
226 sym_to_addr(char *name)
228 int i;
229 syment_t *sep = symbol_table;
231 for (i = 0; i < nsyms; i++) {
232 if (strcmp(name, sep->name) == 0)
233 return (sep->addr);
234 sep++;
236 return (0);
239 size_t
240 sym_size(char *name)
242 int i;
243 syment_t *sep = symbol_table;
245 for (i = 0; i < nsyms; i++) {
246 if (strcmp(name, sep->name) == 0)
247 return (sep->size);
248 sep++;
250 return (0);