xref: /freebsd/usr.bin/gprof/kernel.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1e026a48cSDavid E. O'Brien #include <sys/cdefs.h>
23fc980b1SBrian Feldman #include <sys/param.h>
33fc980b1SBrian Feldman #include <sys/linker.h>
43fc980b1SBrian Feldman #include <sys/sysctl.h>
53fc980b1SBrian Feldman #include <sys/errno.h>
63fc980b1SBrian Feldman 
73fc980b1SBrian Feldman #include <err.h>
83fc980b1SBrian Feldman #include <stdlib.h>
98b061e14SStefan Farfeleder #include <string.h>
103fc980b1SBrian Feldman 
113fc980b1SBrian Feldman #include "gprof.h"
123fc980b1SBrian Feldman 
133fc980b1SBrian Feldman /* Things which get -E excluded by default. */
143fc980b1SBrian Feldman static char	*excludes[] = { ".mcount", "_mcleanup", NULL };
153fc980b1SBrian Feldman 
163fc980b1SBrian Feldman int
kernel_getnfile(const char * unused __unused,char *** defaultEs)17*af2637dbSPhilippe Charnier kernel_getnfile(const char *unused __unused, char ***defaultEs)
183fc980b1SBrian Feldman {
193fc980b1SBrian Feldman 	char *namelist;
203fc980b1SBrian Feldman 	size_t len;
213fc980b1SBrian Feldman 	char *name;
223fc980b1SBrian Feldman 
233fc980b1SBrian Feldman 	if (sysctlbyname("kern.function_list", NULL, &len, NULL, 0) == -1)
243fc980b1SBrian Feldman 		err(1, "sysctlbyname: function_list size");
253fc980b1SBrian Feldman 	for (;;) {
263fc980b1SBrian Feldman 		namelist = malloc(len);
273fc980b1SBrian Feldman 		if (namelist == NULL)
283fc980b1SBrian Feldman 			err(1, "malloc");
293fc980b1SBrian Feldman 		if (sysctlbyname("kern.function_list", namelist, &len, NULL,
303fc980b1SBrian Feldman 		   0) == 0)
313fc980b1SBrian Feldman 			break;
323fc980b1SBrian Feldman 		if (errno == ENOMEM)
333fc980b1SBrian Feldman 			free(namelist);
343fc980b1SBrian Feldman 		else
353fc980b1SBrian Feldman 			err(1, "sysctlbyname: function_list");
363fc980b1SBrian Feldman 	}
373fc980b1SBrian Feldman 	nname = 0;
383fc980b1SBrian Feldman 	for (name = namelist; *name != '\0'; name += strlen(name) + 1)
393fc980b1SBrian Feldman 		nname++;
403fc980b1SBrian Feldman 	/* Allocate memory for them, plus a terminating entry. */
413fc980b1SBrian Feldman 	if ((nl = (nltype *)calloc(nname + 1, sizeof(nltype))) == NULL)
423fc980b1SBrian Feldman 		errx(1, "Insufficient memory for symbol table");
433fc980b1SBrian Feldman 	npe = nl;
443fc980b1SBrian Feldman 	for (name = namelist; *name != '\0'; name += strlen(name) + 1) {
453fc980b1SBrian Feldman 		struct kld_sym_lookup ksl;
463fc980b1SBrian Feldman 
473fc980b1SBrian Feldman 		ksl.version = sizeof(ksl);
483fc980b1SBrian Feldman 		ksl.symname = name;
493fc980b1SBrian Feldman 		if (kldsym(0, KLDSYM_LOOKUP, &ksl))
503fc980b1SBrian Feldman 			err(1, "kldsym(%s)", name);
513fc980b1SBrian Feldman 		/* aflag not supported */
523fc980b1SBrian Feldman 		if (uflag && strchr(name, '.') != NULL)
533fc980b1SBrian Feldman 			continue;
543fc980b1SBrian Feldman 		npe->value = ksl.symvalue;
553fc980b1SBrian Feldman 		npe->name = name;
563fc980b1SBrian Feldman 		npe++;
573fc980b1SBrian Feldman 	}
583fc980b1SBrian Feldman 	npe->value = -1;
593fc980b1SBrian Feldman 
603fc980b1SBrian Feldman 	*defaultEs = excludes;
6158f45f54SBrian Feldman 	return (0);
623fc980b1SBrian Feldman }
63