| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 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 |
|
| 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 |
|
| 77 |
|
| 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 |
|
| 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 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
min_delta = LLONG_MAX; |
| 113 |
sym_base_addr = 0; |
| 114 |
while (!feof(pf)) { |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 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 |
|
| 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 |
}; |