xref: /dragonfly/usr.bin/systat/symbols.c (revision 01bedb5a)
1 /*
2  * Copyright (c) 2010 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/types.h>
36 #include <sys/sysctl.h>
37 
38 #include <ctype.h>
39 #include <devinfo.h>
40 #include <err.h>
41 #include <fcntl.h>
42 #include <kvm.h>
43 #include <limits.h>
44 #include <nlist.h>
45 #include <stdint.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <evtr.h>
51 #include <stdarg.h>
52 #include "symbols.h"
53 
54 struct symdata {
55 	RB_ENTRY(symdata) node;
56 	const char *symname;
57 	char *symaddr_beg;
58 	char *symaddr_end;
59 	char symtype;
60 };
61 
62 static int symdata_cmp(struct symdata *s1, struct symdata *s2);
63 
64 RB_HEAD(symdata_rbtree, symdata);
65 RB_PROTOTYPE3(symdata_rbtree, symdata, node, symdata_cmp, char *);
66 RB_GENERATE3(symdata_rbtree, symdata, node, symdata_cmp, char *,
67 		symaddr_beg, symaddr_end);
68 
69 static struct symdata_rbtree symroot = RB_INITIALIZER(symroot);
70 static char *symbegin = (void *)(intptr_t)0;
71 static char *symend = (void *)(intptr_t)-1;
72 
73 
74 void
75 read_symbols(const char *file)
76 {
77 	char buf[256];
78 	char cmd[256];
79 	size_t buflen = sizeof(buf);
80 	FILE *fp;
81 	struct symdata *lsym;
82 	struct symdata *sym;
83 	char *s1;
84 	char *s2;
85 	char *s3;
86 
87 	RB_INIT(&symroot);
88 
89 	if (file == NULL) {
90 		if (sysctlbyname("kern.bootfile", buf, &buflen, NULL, 0) < 0)
91 			file = "/boot/kernel";
92 		else
93 			file = buf;
94 	}
95 	snprintf(cmd, sizeof(cmd), "nm -n %s", file);
96 	if ((fp = popen(cmd, "r")) != NULL) {
97 		lsym = NULL;
98 		while (fgets(buf, sizeof(buf), fp) != NULL) {
99 		    s1 = strtok(buf, " \t\n");
100 		    s2 = strtok(NULL, " \t\n");
101 		    s3 = strtok(NULL, " \t\n");
102 		    if (s1 && s2 && s3) {
103 			sym = malloc(sizeof(struct symdata));
104 			sym->symaddr_beg = (char *)strtoul(s1, NULL, 16);
105 			sym->symaddr_end = sym->symaddr_beg;
106 			if (lsym)
107 				lsym->symaddr_end = sym->symaddr_beg - 1;
108 			sym->symtype = s2[0];
109 			sym->symname = strdup(s3);
110 			if (strcmp(s3, "kernbase") == 0)
111 				symbegin = sym->symaddr_beg;
112 			if (strcmp(s3, "end") == 0)
113 				symend = sym->symaddr_beg;
114 			RB_INSERT(symdata_rbtree, &symroot, sym);
115 			lsym = sym;
116 		    }
117 		}
118 		pclose(fp);
119 	}
120 }
121 
122 const char *
123 address_to_symbol(void *kptr, struct save_ctx *ctx)
124 {
125 	static struct symdata *sym;
126 	char *buf = ctx->save_buf;
127 	int size = sizeof(ctx->save_buf);
128 
129 	sym = RB_RLOOKUP(symdata_rbtree, &symroot, (char *)kptr);
130 	if (sym) {
131 		snprintf(buf, size, "%s+%d", sym->symname,
132 			(int)((char *)kptr - sym->symaddr_beg));
133 	} else {
134 		snprintf(buf, size, "%p", kptr);
135 	}
136 	return(buf);
137 }
138 
139 static int
140 symdata_cmp(struct symdata *s1, struct symdata *s2)
141 {
142 	if (s1->symaddr_beg < s2->symaddr_beg)
143 		return -1;
144 	if (s1->symaddr_beg > s2->symaddr_beg)
145 		return 1;
146 	return 0;
147 }
148