xref: /freebsd-src/cddl/contrib/opensolaris/cmd/lockstat/sym.c (revision 49e3972afc1112f95863a04cea7a4401b78dfd1f)
17e1e3d94SStacey Son /*
27e1e3d94SStacey Son  * CDDL HEADER START
37e1e3d94SStacey Son  *
47e1e3d94SStacey Son  * The contents of this file are subject to the terms of the
57e1e3d94SStacey Son  * Common Development and Distribution License, Version 1.0 only
67e1e3d94SStacey Son  * (the "License").  You may not use this file except in compliance
77e1e3d94SStacey Son  * with the License.
87e1e3d94SStacey Son  *
97e1e3d94SStacey Son  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107e1e3d94SStacey Son  * or http://www.opensolaris.org/os/licensing.
117e1e3d94SStacey Son  * See the License for the specific language governing permissions
127e1e3d94SStacey Son  * and limitations under the License.
137e1e3d94SStacey Son  *
147e1e3d94SStacey Son  * When distributing Covered Code, include this CDDL HEADER in each
157e1e3d94SStacey Son  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167e1e3d94SStacey Son  * If applicable, add the following below this CDDL HEADER, with the
177e1e3d94SStacey Son  * fields enclosed by brackets "[]" replaced with your own identifying
187e1e3d94SStacey Son  * information: Portions Copyright [yyyy] [name of copyright owner]
197e1e3d94SStacey Son  *
207e1e3d94SStacey Son  * CDDL HEADER END
217e1e3d94SStacey Son  */
227e1e3d94SStacey Son /*
237e1e3d94SStacey Son  * Copyright (c) 1997-1999 by Sun Microsystems, Inc.
247e1e3d94SStacey Son  * All rights reserved.
257e1e3d94SStacey Son  */
267e1e3d94SStacey Son 
277e1e3d94SStacey Son #pragma ident	"%Z%%M%	%I%	%E% SMI"
287e1e3d94SStacey Son 
297e1e3d94SStacey Son #include <stdio.h>
307e1e3d94SStacey Son #include <fcntl.h>
317e1e3d94SStacey Son #include <ctype.h>
327e1e3d94SStacey Son #include <string.h>
337e1e3d94SStacey Son #include <signal.h>
347e1e3d94SStacey Son #include <errno.h>
357e1e3d94SStacey Son #include <stdlib.h>
367e1e3d94SStacey Son #include <stdarg.h>
377e1e3d94SStacey Son #include <unistd.h>
387e1e3d94SStacey Son #include <limits.h>
397e1e3d94SStacey Son #include <sys/types.h>
407e1e3d94SStacey Son #include <sys/stat.h>
417e1e3d94SStacey Son 
427e1e3d94SStacey Son #include <libelf.h>
437e1e3d94SStacey Son #include <link.h>
447e1e3d94SStacey Son #include <elf.h>
45*49e3972aSMark Johnston #include <gelf.h>
46bc96366cSSteven Hartland #ifdef illumos
477e1e3d94SStacey Son #include <sys/machelf.h>
487e1e3d94SStacey Son 
497e1e3d94SStacey Son #include <kstat.h>
507e1e3d94SStacey Son #else
517e1e3d94SStacey Son #include <sys/elf.h>
52600080d8SXin LI #include <sys/param.h>
53600080d8SXin LI #include <sys/module.h>
54600080d8SXin LI #include <sys/linker.h>
557e1e3d94SStacey Son #endif
567e1e3d94SStacey Son #include <sys/cpuvar.h>
577e1e3d94SStacey Son 
587e1e3d94SStacey Son typedef struct syment {
597e1e3d94SStacey Son 	uintptr_t	addr;
607e1e3d94SStacey Son 	char		*name;
617e1e3d94SStacey Son 	size_t		size;
627e1e3d94SStacey Son } syment_t;
637e1e3d94SStacey Son 
647e1e3d94SStacey Son static syment_t *symbol_table;
657e1e3d94SStacey Son static int nsyms, maxsyms;
667e1e3d94SStacey Son static char maxsymname[64];
677e1e3d94SStacey Son 
68bc96366cSSteven Hartland #ifdef illumos
697e1e3d94SStacey Son #ifdef _ELF64
707e1e3d94SStacey Son #define	elf_getshdr elf64_getshdr
717e1e3d94SStacey Son #else
727e1e3d94SStacey Son #define	elf_getshdr elf32_getshdr
737e1e3d94SStacey Son #endif
747e1e3d94SStacey Son #endif
757e1e3d94SStacey Son 
767e1e3d94SStacey Son static void
add_symbol(char * name,uintptr_t addr,size_t size)777e1e3d94SStacey Son add_symbol(char *name, uintptr_t addr, size_t size)
787e1e3d94SStacey Son {
797e1e3d94SStacey Son 	syment_t *sep;
807e1e3d94SStacey Son 
817e1e3d94SStacey Son 	if (nsyms >= maxsyms) {
827e1e3d94SStacey Son 		maxsyms += 10000;
837e1e3d94SStacey Son 		symbol_table = realloc(symbol_table, maxsyms * sizeof (*sep));
847e1e3d94SStacey Son 		if (symbol_table == NULL) {
857e1e3d94SStacey Son 			(void) fprintf(stderr, "can't allocate symbol table\n");
867e1e3d94SStacey Son 			exit(3);
877e1e3d94SStacey Son 		}
887e1e3d94SStacey Son 	}
897e1e3d94SStacey Son 	sep = &symbol_table[nsyms++];
907e1e3d94SStacey Son 
917e1e3d94SStacey Son 	sep->name = name;
927e1e3d94SStacey Son 	sep->addr = addr;
937e1e3d94SStacey Son 	sep->size = size;
947e1e3d94SStacey Son }
957e1e3d94SStacey Son 
967e1e3d94SStacey Son static void
remove_symbol(uintptr_t addr)977e1e3d94SStacey Son remove_symbol(uintptr_t addr)
987e1e3d94SStacey Son {
997e1e3d94SStacey Son 	int i;
1007e1e3d94SStacey Son 	syment_t *sep = symbol_table;
1017e1e3d94SStacey Son 
1027e1e3d94SStacey Son 	for (i = 0; i < nsyms; i++, sep++)
1037e1e3d94SStacey Son 		if (sep->addr == addr)
1047e1e3d94SStacey Son 			sep->addr = 0;
1057e1e3d94SStacey Son }
1067e1e3d94SStacey Son 
107bc96366cSSteven Hartland #ifdef illumos
1087e1e3d94SStacey Son static void
fake_up_certain_popular_kernel_symbols(void)1097e1e3d94SStacey Son fake_up_certain_popular_kernel_symbols(void)
1107e1e3d94SStacey Son {
1117e1e3d94SStacey Son 	kstat_ctl_t *kc;
1127e1e3d94SStacey Son 	kstat_t *ksp;
1137e1e3d94SStacey Son 	char *name;
1147e1e3d94SStacey Son 
1157e1e3d94SStacey Son 	if ((kc = kstat_open()) == NULL)
1167e1e3d94SStacey Son 		return;
1177e1e3d94SStacey Son 
1187e1e3d94SStacey Son 	for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
1197e1e3d94SStacey Son 		if (strcmp(ksp->ks_module, "cpu_info") == 0) {
1207e1e3d94SStacey Son 			if ((name = malloc(20)) == NULL)
1217e1e3d94SStacey Son 				break;
1227e1e3d94SStacey Son 			/*
1237e1e3d94SStacey Son 			 * For consistency, keep cpu[0] and toss cpu0
1247e1e3d94SStacey Son 			 * or any other such symbols.
1257e1e3d94SStacey Son 			 */
1267e1e3d94SStacey Son 			if (ksp->ks_instance == 0)
1277e1e3d94SStacey Son 				remove_symbol((uintptr_t)ksp->ks_private);
1287e1e3d94SStacey Son 			(void) sprintf(name, "cpu[%d]", ksp->ks_instance);
1297e1e3d94SStacey Son 			add_symbol(name, (uintptr_t)ksp->ks_private,
1307e1e3d94SStacey Son 			    sizeof (struct cpu));
1317e1e3d94SStacey Son 		}
1327e1e3d94SStacey Son 	}
1337e1e3d94SStacey Son 	(void) kstat_close(kc);
1347e1e3d94SStacey Son }
135bc96366cSSteven Hartland #else /* !illumos */
1367e1e3d94SStacey Son static void
fake_up_certain_popular_kernel_symbols(void)1377e1e3d94SStacey Son fake_up_certain_popular_kernel_symbols(void)
1387e1e3d94SStacey Son {
1397e1e3d94SStacey Son 	char *name;
1407e1e3d94SStacey Son 	uintptr_t addr;
1417e1e3d94SStacey Son 	int i;
1427e1e3d94SStacey Son 
1437e1e3d94SStacey Son 	/* Good for up to 256 CPUs */
1447e1e3d94SStacey Son 	for(i=0; i < 256;  i++) {
1457e1e3d94SStacey Son 		if ((name = malloc(20)) == NULL)
1467e1e3d94SStacey Son 			break;
1477e1e3d94SStacey Son 		(void) sprintf(name, "cpu[%d]", i);
1487e1e3d94SStacey Son 		addr = 0x01000000 + (i << 16);
1497e1e3d94SStacey Son 		add_symbol(name, addr, sizeof (uintptr_t));
1507e1e3d94SStacey Son 	}
1517e1e3d94SStacey Son }
152bc96366cSSteven Hartland #endif /* illumos */
1537e1e3d94SStacey Son 
1547e1e3d94SStacey Son static int
symcmp(const void * p1,const void * p2)1557e1e3d94SStacey Son symcmp(const void *p1, const void *p2)
1567e1e3d94SStacey Son {
1577e1e3d94SStacey Son 	uintptr_t a1 = ((syment_t *)p1)->addr;
1587e1e3d94SStacey Son 	uintptr_t a2 = ((syment_t *)p2)->addr;
1597e1e3d94SStacey Son 
1607e1e3d94SStacey Son 	if (a1 < a2)
1617e1e3d94SStacey Son 		return (-1);
1627e1e3d94SStacey Son 	if (a1 > a2)
1637e1e3d94SStacey Son 		return (1);
1647e1e3d94SStacey Son 	return (0);
1657e1e3d94SStacey Son }
1667e1e3d94SStacey Son 
1677e1e3d94SStacey Son int
symtab_init(void)1687e1e3d94SStacey Son symtab_init(void)
1697e1e3d94SStacey Son {
1707e1e3d94SStacey Son 	Elf		*elf;
1717e1e3d94SStacey Son 	Elf_Scn		*scn = NULL;
172*49e3972aSMark Johnston 	GElf_Sym	*symtab, *symp, *lastsym;
1737e1e3d94SStacey Son 	char		*strtab;
1747e1e3d94SStacey Son 	uint_t		cnt;
1757e1e3d94SStacey Son 	int		fd;
1767e1e3d94SStacey Son 	int		i;
1777e1e3d94SStacey Son 	int		strindex = -1;
1787e1e3d94SStacey Son 
179bc96366cSSteven Hartland #ifndef illumos
1805d766041SMark Johnston 	if ((fd = open("/dev/ksyms", O_RDONLY)) == -1) {
1815d766041SMark Johnston 		if (errno == ENOENT && modfind("ksyms") == -1) {
1825d766041SMark Johnston 			kldload("ksyms");
1835d766041SMark Johnston 			fd = open("/dev/ksyms", O_RDONLY);
1845d766041SMark Johnston 		}
1855d766041SMark Johnston 		if (fd == -1)
1865d766041SMark Johnston 			return (-1);
1875d766041SMark Johnston 	}
1885d766041SMark Johnston #else
1897e1e3d94SStacey Son 	if ((fd = open("/dev/ksyms", O_RDONLY)) == -1)
1907e1e3d94SStacey Son 		return (-1);
1915d766041SMark Johnston #endif
1927e1e3d94SStacey Son 
1937e1e3d94SStacey Son 	(void) elf_version(EV_CURRENT);
1947e1e3d94SStacey Son 
1957e1e3d94SStacey Son 	elf = elf_begin(fd, ELF_C_READ, NULL);
1967e1e3d94SStacey Son 	for (cnt = 1; (scn = elf_nextscn(elf, scn)) != NULL; cnt++) {
197*49e3972aSMark Johnston 		GElf_Shdr shdr;
198*49e3972aSMark Johnston 		(void) gelf_getshdr(scn, &shdr);
199*49e3972aSMark Johnston 		if (shdr.sh_type == SHT_SYMTAB) {
200*49e3972aSMark Johnston 			symtab = (GElf_Sym *)elf_getdata(scn, NULL)->d_buf;
201*49e3972aSMark Johnston 			nsyms = shdr.sh_size / shdr.sh_entsize;
202*49e3972aSMark Johnston 			strindex = shdr.sh_link;
2037e1e3d94SStacey Son 		}
2047e1e3d94SStacey Son 	}
2057e1e3d94SStacey Son 
2067e1e3d94SStacey Son 	for (cnt = 1; (scn = elf_nextscn(elf, scn)) != NULL; cnt++) {
2077e1e3d94SStacey Son 		if (cnt == strindex)
2087e1e3d94SStacey Son 			strtab = (char *)elf_getdata(scn, NULL)->d_buf;
2097e1e3d94SStacey Son 	}
2107e1e3d94SStacey Son 
2117e1e3d94SStacey Son 	lastsym = symtab + nsyms;
2127e1e3d94SStacey Son 	nsyms = 0;
2137e1e3d94SStacey Son 	for (symp = symtab; symp < lastsym; symp++)
2147e1e3d94SStacey Son 		if ((uint_t)ELF32_ST_TYPE(symp->st_info) <= STT_FUNC &&
2157e1e3d94SStacey Son 		    symp->st_size != 0)
2167e1e3d94SStacey Son 			add_symbol(symp->st_name + strtab,
2177e1e3d94SStacey Son 			    (uintptr_t)symp->st_value, (size_t)symp->st_size);
2187e1e3d94SStacey Son 
2197e1e3d94SStacey Son 	fake_up_certain_popular_kernel_symbols();
2207e1e3d94SStacey Son 	(void) sprintf(maxsymname, "0x%lx", ULONG_MAX);
2217e1e3d94SStacey Son 	add_symbol(maxsymname, ULONG_MAX, 1);
2227e1e3d94SStacey Son 
2237e1e3d94SStacey Son 	qsort(symbol_table, nsyms, sizeof (syment_t), symcmp);
2247e1e3d94SStacey Son 
2257e1e3d94SStacey Son 	/*
2267e1e3d94SStacey Son 	 * Destroy all duplicate symbols, then sort it again.
2277e1e3d94SStacey Son 	 */
2287e1e3d94SStacey Son 	for (i = 0; i < nsyms - 1; i++)
2297e1e3d94SStacey Son 		if (symbol_table[i].addr == symbol_table[i + 1].addr)
2307e1e3d94SStacey Son 			symbol_table[i].addr = 0;
2317e1e3d94SStacey Son 
2327e1e3d94SStacey Son 	qsort(symbol_table, nsyms, sizeof (syment_t), symcmp);
2337e1e3d94SStacey Son 
2347e1e3d94SStacey Son 	while (symbol_table[1].addr == 0) {
2357e1e3d94SStacey Son 		symbol_table++;
2367e1e3d94SStacey Son 		nsyms--;
2377e1e3d94SStacey Son 	}
2387e1e3d94SStacey Son 	symbol_table[0].name = "(usermode)";
2397e1e3d94SStacey Son 	symbol_table[0].addr = 0;
2407e1e3d94SStacey Son 	symbol_table[0].size = 1;
2417e1e3d94SStacey Son 
2427e1e3d94SStacey Son 	close(fd);
2437e1e3d94SStacey Son 	return (0);
2447e1e3d94SStacey Son }
2457e1e3d94SStacey Son 
2467e1e3d94SStacey Son char *
addr_to_sym(uintptr_t addr,uintptr_t * offset,size_t * sizep)2477e1e3d94SStacey Son addr_to_sym(uintptr_t addr, uintptr_t *offset, size_t *sizep)
2487e1e3d94SStacey Son {
2497e1e3d94SStacey Son 	int lo = 0;
2507e1e3d94SStacey Son 	int hi = nsyms - 1;
2517e1e3d94SStacey Son 	int mid;
2527e1e3d94SStacey Son 	syment_t *sep;
2537e1e3d94SStacey Son 
2547e1e3d94SStacey Son 	while (hi - lo > 1) {
2557e1e3d94SStacey Son 		mid = (lo + hi) / 2;
2567e1e3d94SStacey Son 		if (addr >= symbol_table[mid].addr) {
2577e1e3d94SStacey Son 			lo = mid;
2587e1e3d94SStacey Son 		} else {
2597e1e3d94SStacey Son 			hi = mid;
2607e1e3d94SStacey Son 		}
2617e1e3d94SStacey Son 	}
2627e1e3d94SStacey Son 	sep = &symbol_table[lo];
2637e1e3d94SStacey Son 	*offset = addr - sep->addr;
2647e1e3d94SStacey Son 	*sizep = sep->size;
2657e1e3d94SStacey Son 	return (sep->name);
2667e1e3d94SStacey Son }
2677e1e3d94SStacey Son 
2687e1e3d94SStacey Son uintptr_t
sym_to_addr(char * name)2697e1e3d94SStacey Son sym_to_addr(char *name)
2707e1e3d94SStacey Son {
2717e1e3d94SStacey Son 	int i;
2727e1e3d94SStacey Son 	syment_t *sep = symbol_table;
2737e1e3d94SStacey Son 
2747e1e3d94SStacey Son 	for (i = 0; i < nsyms; i++) {
2757e1e3d94SStacey Son 		if (strcmp(name, sep->name) == 0)
2767e1e3d94SStacey Son 			return (sep->addr);
2777e1e3d94SStacey Son 		sep++;
2787e1e3d94SStacey Son 	}
2797e1e3d94SStacey Son 	return (0);
2807e1e3d94SStacey Son }
2817e1e3d94SStacey Son 
2827e1e3d94SStacey Son size_t
sym_size(char * name)2837e1e3d94SStacey Son sym_size(char *name)
2847e1e3d94SStacey Son {
2857e1e3d94SStacey Son 	int i;
2867e1e3d94SStacey Son 	syment_t *sep = symbol_table;
2877e1e3d94SStacey Son 
2887e1e3d94SStacey Son 	for (i = 0; i < nsyms; i++) {
2897e1e3d94SStacey Son 		if (strcmp(name, sep->name) == 0)
2907e1e3d94SStacey Son 			return (sep->size);
2917e1e3d94SStacey Son 		sep++;
2927e1e3d94SStacey Son 	}
2937e1e3d94SStacey Son 	return (0);
2947e1e3d94SStacey Son }
295