-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlibrary.c
318 lines (237 loc) · 7.5 KB
/
library.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* This file is part of mtrace-ng.
* Copyright (C) 2018 Stefani Seibold <[email protected]>
*
* This work was sponsored by Rohde & Schwarz GmbH & Co. KG, Munich/Germany.
*
* 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 <errno.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <sys/mman.h>
#include "backend.h"
#include "breakpoint.h"
#include "debug.h"
#include "dict.h"
#include "library.h"
#include "options.h"
#include "report.h"
#include "server.h"
#include "task.h"
struct libref *libref_new(unsigned int type)
{
struct libref *libref = malloc(sizeof(*libref));
if (!libref)
return NULL;
memset(libref, 0, sizeof(*libref));
libref->refcnt = 0;
libref->type = type;
INIT_LIST_HEAD(&libref->sym_list);
return libref;
}
void libref_delete(struct libref *libref)
{
struct list_head *it, *next;
list_for_each_safe(it, next, &libref->sym_list) {
struct library_symbol *sym = container_of(it, struct library_symbol, list);
list_del(&sym->list);
free(sym);
}
if (libref->mmap_addr)
munmap(libref->mmap_addr, libref->txt_size);
free((void *)libref->filename);
free(libref);
}
static void libref_put(struct libref *libref)
{
assert(libref->refcnt != 0);
if (!--libref->refcnt)
libref_delete(libref);
}
static struct libref *libref_get(struct libref *libref)
{
assert(libref);
++libref->refcnt;
return libref;
}
void libref_set_filename(struct libref *libref, const char *new_name)
{
free((void *)libref->filename);
libref->filename = new_name ? strdup(new_name) : NULL;
}
struct library_symbol *library_symbol_new(struct libref *libref, arch_addr_t addr, const struct function *func)
{
struct library_symbol *libsym = malloc(sizeof(*libsym));
if (!libsym)
return NULL;
libsym->libref = libref;
libsym->func = func;
libsym->addr = addr;
list_add_tail(&libsym->list, &libref->sym_list);
return libsym;
}
static void library_delete(struct task *task, struct library *lib)
{
if (lib == NULL)
return;
struct list_head *it, *next;
struct libref *libref = lib->libref;
struct task *leader = task->leader;
list_for_each_safe(it, next, &libref->sym_list) {
struct breakpoint *bp = breakpoint_find(leader, container_of(it, struct library_symbol, list)->addr);
if (bp)
breakpoint_delete(leader, bp);
}
list_del(&lib->list);
rb_erase(&lib->rb_node, &leader->libraries_tree);
free(lib);
libref_put(libref);
}
struct library_symbol *library_find_symbol(struct libref *libref, arch_addr_t addr)
{
struct list_head *it;
list_for_each(it, &libref->sym_list) {
struct library_symbol *sym = container_of(it, struct library_symbol, list);
if (sym->addr == addr)
return sym;
}
return NULL;
}
struct library *library_find_by_dyn(struct list_head *list, arch_addr_t dyn)
{
struct list_head *it;
list_for_each(it, list) {
struct library *lib = container_of(it, struct library, list);
if (lib->libref->dyn == dyn)
return lib;
}
return NULL;
}
void library_delete_list(struct task *leader, struct list_head *list)
{
struct list_head *it, *next;
list_for_each_safe(it, next, list) {
struct library *lib = container_of(it, struct library, list);
struct libref *libref = lib->libref;
debug(DEBUG_FUNCTION, "%s@%#lx pid=%d ", libref->filename, libref->dyn, leader->pid);
if (unlikely(options.verbose > 1))
fprintf(stderr, "+++ library del pid=%d %s@%#lx %#lx-%#lx\n", leader->pid, libref->filename, libref->dyn, libref->txt_vaddr, libref->txt_vaddr + libref->txt_size);
library_delete(leader, lib);
}
}
static void cb_breakpoint_for_symbol(struct library_symbol *libsym, void *data)
{
struct task *task = data;
arch_addr_t addr = libsym->addr;
struct breakpoint *bp = breakpoint_find(task, addr);
if (bp) {
assert(bp->libsym == NULL);
bp->libsym = libsym;
return;
}
bp = breakpoint_new(task, addr, libsym, BP_AUTO);
if (!bp)
fprintf(stderr, "Couldn't insert breakpoint for %s to %d: %s", libsym->func->name, task->pid, strerror(errno));
if (server_connected())
breakpoint_enable(task, bp);
}
static void library_each_symbol(struct libref *libref, void (*cb)(struct library_symbol *, void *), void *data)
{
struct list_head *it, *next;
list_for_each_safe(it, next, &libref->sym_list) {
struct library_symbol *sym = container_of(it, struct library_symbol, list);
(*cb) (sym, data);
}
}
struct libref *addr2libref(struct task *leader, arch_addr_t addr)
{
struct rb_node **new = &(leader->libraries_tree.rb_node);
/* Figure out where to put new node */
while (*new) {
struct libref *this = container_of(*new, struct library, rb_node)->libref;
if (addr >= this->txt_vaddr && addr < this->txt_vaddr + this->txt_size)
return this;
if (this->txt_vaddr < addr)
new = &((*new)->rb_left);
else
new = &((*new)->rb_right);
}
return NULL;
}
static void insert_lib(struct task *leader, struct library *lib)
{
struct rb_node **new = &(leader->libraries_tree.rb_node), *parent = NULL;
/* Figure out where to put new node */
while (*new) {
struct library *this = container_of(*new, struct library, rb_node);
parent = *new;
if (this->libref->txt_vaddr < lib->libref->txt_vaddr)
new = &((*new)->rb_left);
else
new = &((*new)->rb_right);
}
/* Add new node and rebalance tree. */
rb_link_node(&lib->rb_node, parent, new);
rb_insert_color(&lib->rb_node, &leader->libraries_tree);
}
static struct library *_library_add(struct task *leader, struct libref *libref)
{
debug(DEBUG_PROCESS, "%s@%#lx to pid=%d", libref->filename, libref->dyn, leader->pid);
assert(leader->leader == leader);
struct library *lib = malloc(sizeof(*lib));
memset(lib, 0, sizeof(*lib));
lib->libref = libref_get(libref);
list_add_tail(&lib->list, &leader->libraries_list);
insert_lib(leader, lib);
if (unlikely(options.verbose > 1))
fprintf(stderr, "+++ library add pid=%d %s@%#lx %#lx-%#lx\n", leader->pid, libref->filename, libref->dyn, libref->txt_vaddr, libref->txt_vaddr + libref->txt_size);
return lib;
}
struct library *library_add(struct task *leader, struct libref *libref)
{
struct library *lib = _library_add(leader, libref);
/* Insert breakpoints for all active symbols. */
library_each_symbol(libref, cb_breakpoint_for_symbol, leader);
report_add_map(leader, lib);
return lib;
}
void library_clear_all(struct task *leader)
{
library_delete_list(leader, &leader->libraries_list);
}
int library_clone_all(struct task *clone, struct task *leader)
{
struct list_head *it;
list_for_each(it, &leader->libraries_list) {
struct library *lib = container_of(it, struct library, list);
_library_add(clone, lib->libref);
}
return 0;
}
void library_setup(struct task *leader)
{
INIT_LIST_HEAD(&leader->libraries_list);
leader->libraries_tree = RB_ROOT;
}
const char *library_execname(struct task *leader)
{
if (list_empty(&leader->libraries_list))
return NULL;
return container_of(leader->libraries_list.next, struct library, list)->libref->filename;
}