-
Notifications
You must be signed in to change notification settings - Fork 2
/
symbol_lookup.c
152 lines (124 loc) · 3.36 KB
/
symbol_lookup.c
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
/*
* Copyright (C) 2017 Sergey Senozhatsky
*
* This program 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; either version 2 of the
* License, or (at your option) any later version.
*
* 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; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <string.h>
#include <pthread.h>
#include <symbol_lookup.h>
#include <output.h>
static long max_idx = 0;
static long symbols_sz = 400;
static struct resovled_sym *symbols;
static unsigned long symbol_nr = 0;
static pthread_rwlock_t lock;
static void __init(void)
{
if (!symbols)
symbols = malloc(sizeof(struct resovled_sym) * symbols_sz);
if (!symbols)
abort();
}
static int sym_compare(const void *a, const void *b)
{
const struct resovled_sym *sa = (const struct resovled_sym *) a;
const struct resovled_sym *sb = (const struct resovled_sym *) b;
if (sa->start_ip > sb->start_ip)
return 1;
if (sa->start_ip == sb->start_ip)
return 0;
if (sa->start_ip < sb->start_ip)
return -1;
}
static long __lookup(unsigned long ip)
{
long lo = 0;
long hi = max_idx - 1;
long mid;
while (lo <= hi) {
mid = lo + (hi - lo) / 2;
if (symbols[mid].start_ip <= ip &&
ip <= symbols[mid].end_ip)
return mid;
if (symbols[mid].start_ip > ip)
hi = mid - 1;
if (symbols[mid].start_ip < ip)
lo = mid + 1;
}
return -1;
}
struct resovled_sym add_resolved_symbol(struct options *opts,
unsigned long start_ip,
unsigned long end_ip,
char *fn_name)
{
long idx;
struct resovled_sym s = {0, 0, 0, UNRESOLVED_SYM_NAME};
if (pthread_rwlock_wrlock(&lock) != 0)
abort();
__init();
symbols[max_idx].start_ip = start_ip;
symbols[max_idx].end_ip = end_ip;
symbols[max_idx].nr = symbol_nr;
if (fn_name == UNRESOLVED_SYM_NAME)
symbols[max_idx].fn_name = UNRESOLVED_SYM_NAME;
else
symbols[max_idx].fn_name = strdup(fn_name);
/* report a new resolved symbol and its seq nr */
if (!(opts->flags & OPTS_HUMAN_READABLE)) {
output("[f:%ld][%x-%x][%s]\n",
symbol_nr, start_ip, end_ip, fn_name);
}
s = symbols[max_idx];
symbol_nr++;
max_idx++;
if (max_idx >= symbols_sz - 1) {
void *new_table;
symbols_sz += symbols_sz / 2;
new_table = realloc(symbols,
symbols_sz * sizeof(struct resovled_sym));
if (!new_table)
abort();
symbols = new_table;
}
qsort(symbols, max_idx - 1, sizeof(struct resovled_sym), sym_compare);
pthread_rwlock_unlock(&lock);
return s;
}
struct resovled_sym lookup_resolved_symbol(unsigned long ip)
{
long idx;
struct resovled_sym s = {0, 0, 0, UNRESOLVED_SYM_NAME};
if (pthread_rwlock_rdlock(&lock) != 0)
abort();
idx = __lookup(ip);
if (idx != -1)
s = symbols[idx];
pthread_rwlock_unlock(&lock);
return s;
}
/*
* This is early init. Do not allocate dynamic buffers here, since
* we are still in __init mode.
*/
void early_lookup_init(void)
{
if (pthread_rwlock_init(&lock, NULL) != 0)
abort();
}