summaryrefslogtreecommitdiff
path: root/src/lookup_kas.c
blob: 09fad980c3ea5ce1663ceb32954f64324be58158 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * Copyright (C) 2009, Neil Horman <nhorman@redhat.com>
 * 
 * This program file is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program in a file named COPYING; if not, write to the
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 */

/*
 * This is a translator.  given an input address, this will convert it into a
 * symbolic name using /proc/kallsyms
 */

#include <stdlib.h>
#include <stdio.h>
#include <sys/utsname.h>
#include <bfd.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/queue.h>

#include "lookup.h"

struct symbol_entry {
	char *sym_name;
	__u64 start;
	__u64 end;
	LIST_ENTRY(symbol_entry) list;
};

LIST_HEAD(sym_list, symbol_entry);

/*
 * This is our cache of symbols that we've previously looked up
 */ 
static struct sym_list sym_list_head = {NULL}; 


static int lookup_kas_cache( __u64 pc, struct loc_result *location)
{
	struct symbol_entry *sym;

	LIST_FOREACH(sym, &sym_list_head, list) {
		if ((pc >= sym->start) &&
		    (pc <= sym->end)) {
			location->symbol = sym->sym_name;
			location->offset = (pc - sym->start);
			return 0;
		}
	}

	return 1;
}

static void kas_add_cache(__u64 start, __u64 end, char *name)
{
	struct symbol_entry *sym = NULL;

	sym = malloc(sizeof(struct symbol_entry));
	if (!sym)
		return;

	sym->start = start;
	sym->end = end;
	sym->sym_name = name;

	LIST_INSERT_HEAD(&sym_list_head, sym, list);
	return;
}

static int lookup_kas_proc(__u64 pc, struct loc_result *location)
{
	FILE *pf;
	__u64 ppc;
	__u64 uppc, ulpc, uipc;
	char *name, *last_name;

	pf = fopen("/proc/kallsyms", "r");

	if (!pf)
		return 1;

	last_name = NULL;
	uipc = pc;
	ulpc = 0;
	while (!feof(pf)) {
		/* 
		 * Each line of /proc/kallsyms is formatteded as:
		 *  - "%pK %c %s\n" (for kernel internal symbols), or
		 *  - "%pK %c %s\t[%s]\n" (for module-provided symbols)
		 */
		if (fscanf(pf, "%llx %*s %as [ %*[^]] ]", &ppc, &name) < 0) {
			perror("Error Scanning File: ");
			break;
		}

		uppc = (__u64)ppc;
		if ((uipc >= ulpc) &&
		    (uipc < uppc)) {
			/*
 			 * The last symbol we looked at
 			 * was a hit, record and return it
 			 * Note that we don't free last_name
 			 * here, because the cache is using it
 			 */
			kas_add_cache(ulpc, uppc-1, last_name);
			fclose(pf);
			free(name);
			return lookup_kas_cache(pc, location);
		} 

		/*
 		 * Advance all our state holders
 		 */
		free(last_name);
		last_name = name;
		ulpc = uppc;
	}

	fclose(pf);
	return 1;
}

static int lookup_kas_init(void)
{
	printf("Initalizing kallsyms db\n");
	
	return 0;
}


static int lookup_kas_sym(void *pc, struct loc_result *location)
{
	__u64 pcv;

	memset(&pcv, 0, sizeof(__u64));

	memcpy(&pcv, &pc, sizeof(void *));

	if (!lookup_kas_cache(pcv, location))
		return 0;

	return lookup_kas_proc(pcv, location);
}

struct lookup_methods kallsym_methods = {
	lookup_kas_init,
	lookup_kas_sym,
};