Debian Codegraph @

src/lookup_kas.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2009, Neil Horman <[email protected]>
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
6 /*
7 * This is a translator. given an input address, this will convert it into a
8 * symbolic name using /proc/kallsyms
9 */
10
11 #include "config.h"
12
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <stdint.h>
16 #include <sys/utsname.h>
17 #include <string.h>
18 #include <limits.h>
19 #include <unistd.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/queue.h>
23
24 #include "lookup.h"
25
26 struct symbol_entry {
27 char *sym_name;
28 uint64_t start;
29 uint64_t end;
30 LIST_ENTRY(symbol_entry) list;
31 };
32
33 LIST_HEAD(sym_list, symbol_entry);
34
35 /*
36 * This is our cache of symbols that we've previously looked up
37 */
38 static struct sym_list sym_list_head = {NULL};
39
40
41 static int lookup_kas_cache(uint64_t pc, struct loc_result *location)
42 {
43 struct symbol_entry *sym;
44
45 LIST_FOREACH(sym, &sym_list_head, list) {
46 if ((pc >= sym->start) &&
47 (pc <= sym->end)) {
48 location->symbol = sym->sym_name;
49 location->offset = (pc - sym->start);
50 return 0;
51 }
52 }
53
54 return 1;
55 }
56
57 static void kas_add_cache(__u64 start, __u64 end, char *name)
58 {
59 struct symbol_entry *sym = NULL;
60
61 sym = malloc(sizeof(struct symbol_entry));
62 if (!sym)
63 return;
64
65 sym->start = start;
66 sym->end = end;
67 sym->sym_name = name;
68
69 LIST_INSERT_HEAD(&sym_list_head, sym, list);
70 return;
71 }
72
73 static void kas_update_cache(__u64 start, __u64 end, char *name)
74 {
75 struct symbol_entry *sym;
76 /* look for any symbol that matches our start
77 * if the new end is longer than the current end, extend it
78 */
79 LIST_FOREACH(sym, &sym_list_head, list) {
80 if (start == sym->start) {
81 if (end > sym->end) {
82 sym->end = end;
83 }
84 return;
85 }
86 }
87
88 /* if we get here, we didn't find a symbol, and should add it */
89 kas_add_cache(start, end, name);
90 }
91
92 static int lookup_kas_proc(uint64_t pc, struct loc_result *location)
93 {
94 FILE *pf;
95 uint64_t sppc;
96 uint64_t min_delta, sdelta;
97 uint64_t sym_base_addr;
98 char *tgt_sym = NULL;
99 char *name;
100
101 pf = fopen("/proc/kallsyms", "r");
102
103 if (!pf)
104 return 1;
105
106
107 /*
108 * We need to conduct a reverse price is right search here, we need to find the symbol that is less than
109 * pc, but by the least amount. i.e. address 0xffffffff00010 is 10 more than symbol A, at 0xffffffff00000000,
110 * but is only 8 more than symbol B at 0xffffffff00000002, therefore this drop occurs at symbol B+8
111 */
112 min_delta = LLONG_MAX;
113 sym_base_addr = 0;
114 while (!feof(pf)) {
115 /*
116 * Each line of /proc/kallsyms is formatteded as:
117 * - "%pK %c %s\n" (for kernel internal symbols), or
118 * - "%pK %c %s\t[%s]\n" (for module-provided symbols)
119 */
120 if (fscanf(pf, "%llx %*s %ms [ %*[^]] ]", (unsigned long long *)&sppc, &name) < 0) {
121 if (ferror(pf)) {
122 perror("Error Scanning File: ");
123 break;
124 }
125 if (feof(pf)) {
126 continue;
127 }
128 }
129
130 /* don't bother with symbols that are above our target */
131 if (sppc > pc) {
132 continue;
133 }
134
135 sdelta = pc - sppc;
136 if (sdelta < min_delta) {
137 min_delta = sdelta;
138 if (tgt_sym)
139 free(tgt_sym);
140 tgt_sym = strdup(name);
141 sym_base_addr = sppc;
142 }
143 free(name);
144 }
145
146 fclose(pf);
147
148 if (sym_base_addr) {
149 kas_update_cache(sym_base_addr, sym_base_addr + min_delta, tgt_sym);
150 location->symbol = tgt_sym;
151 location->offset = min_delta;
152 return 0;
153 }
154 return 1;
155 }
156
157 static int lookup_kas_init(void)
158 {
159 printf("Initializing kallsyms db\n");
160
161 return 0;
162 }
163
164 static int lookup_kas_sym(void *pc, struct loc_result *location)
165 {
166 __u64 pcv;
167
168 pcv = (uintptr_t)pc;
169
170 if (!lookup_kas_cache(pcv, location)) {
171 return 0;
172 }
173
174 return lookup_kas_proc(pcv, location);
175 }
176
177 struct lookup_methods kallsym_methods = {
178 lookup_kas_init,
179 lookup_kas_sym,
180 };