aboutsummaryrefslogtreecommitdiff
path: root/breakpoints.c
blob: 85d228fc3af01b3c208d00fdf2f316bc2a43e066 (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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#include "config.h"

#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <error.h>
#include <errno.h>

#ifdef __powerpc__
#include <sys/ptrace.h>
#endif

#include "breakpoint.h"
#include "common.h"
#include "proc.h"
#include "library.h"

#ifndef ARCH_HAVE_TRANSLATE_ADDRESS
int
arch_translate_address(struct Process *proc,
		       target_address_t addr, target_address_t *ret)
{
	*ret = addr;
	return 0;
}
#endif

void
breakpoint_on_hit(struct breakpoint *bp, struct Process *proc)
{
	assert(bp != NULL);
	if (bp->cbs != NULL && bp->cbs->on_hit != NULL)
		(bp->cbs->on_hit)(bp, proc);
}

void
breakpoint_on_continue(struct breakpoint *bp, struct Process *proc)
{
	assert(bp != NULL);
	if (bp->cbs != NULL && bp->cbs->on_continue != NULL)
		(bp->cbs->on_continue)(bp, proc);
	else
		continue_after_breakpoint(proc, bp);
}

void
breakpoint_on_retract(struct breakpoint *bp, struct Process *proc)
{
	assert(bp != NULL);
	if (bp->cbs != NULL && bp->cbs->on_retract != NULL)
		(bp->cbs->on_retract)(bp, proc);
}

/*****************************************************************************/

struct breakpoint *
address2bpstruct(Process *proc, void *addr)
{
	assert(proc != NULL);
	assert(proc->breakpoints != NULL);
	assert(proc->leader == proc);
	debug(DEBUG_FUNCTION, "address2bpstruct(pid=%d, addr=%p)", proc->pid, addr);
	return dict_find_entry(proc->breakpoints, addr);
}

#ifndef ARCH_HAVE_BREAKPOINT_DATA
int
arch_breakpoint_init(struct Process *proc, struct breakpoint *sbp)
{
	return 0;
}

void
arch_breakpoint_destroy(struct breakpoint *sbp)
{
}

int
arch_breakpoint_clone(struct breakpoint *retp, struct breakpoint *sbp)
{
	return 0;
}
#endif

static void
breakpoint_init_base(struct breakpoint *bp, struct Process *proc,
		     target_address_t addr, struct library_symbol *libsym)
{
	bp->cbs = NULL;
	bp->addr = addr;
	memset(bp->orig_value, 0, sizeof(bp->orig_value));
	bp->enabled = 0;
	bp->libsym = libsym;
}

/* On second thought, I don't think we need PROC.  All the translation
 * (arch_translate_address in particular) should be doable using
 * static lookups of various sections in the ELF file.  We shouldn't
 * need process for anything.  */
int
breakpoint_init(struct breakpoint *bp, struct Process *proc,
		target_address_t addr, struct library_symbol *libsym)
{
	breakpoint_init_base(bp, proc, addr, libsym);
	return arch_breakpoint_init(proc, bp);
}

void
breakpoint_set_callbacks(struct breakpoint *bp, struct bp_callbacks *cbs)
{
	if (bp->cbs != NULL)
		assert(bp->cbs == NULL);
	bp->cbs = cbs;
}

void
breakpoint_destroy(struct breakpoint *bp)
{
	if (bp == NULL)
		return;
	arch_breakpoint_destroy(bp);
}

struct find_symbol_data {
	struct library_symbol *old_libsym;
	struct library_symbol *found_libsym;
};

static enum callback_status
find_sym_in_lib(struct Process *proc, struct library *lib, void *u)
{
	struct find_symbol_data *fs = u;
	fs->found_libsym
		= library_each_symbol(lib, NULL, library_symbol_equal_cb,
				      fs->old_libsym);
	return fs->found_libsym != NULL ? CBS_STOP : CBS_CONT;
}

int
breakpoint_clone(struct breakpoint *retp, struct Process *new_proc,
		 struct breakpoint *bp, struct Process *old_proc)
{
	/* Find library and symbol that this breakpoint was linked to.  */
	struct library_symbol *libsym = bp->libsym;
	struct library *lib = NULL;
	if (libsym != NULL) {
		struct find_symbol_data f_data = {
			.old_libsym = libsym,
		};
		lib = proc_each_library(old_proc, NULL,
					find_sym_in_lib, &f_data);
		assert(lib != NULL);
		libsym = f_data.found_libsym;
	}

	/* LIB and LIBSYM now hold the new library and symbol that
	 * correspond to the original breakpoint.  Now we can do the
	 * clone itself.  */
	breakpoint_init_base(retp, new_proc, bp->addr, libsym);
	memcpy(retp->orig_value, bp->orig_value, sizeof(bp->orig_value));
	retp->enabled = bp->enabled;
	if (arch_breakpoint_clone(retp, bp) < 0)
		return -1;
	breakpoint_set_callbacks(retp, bp->cbs);
	return 0;
}

int
breakpoint_turn_on(struct breakpoint *bp, struct Process *proc)
{
	bp->enabled++;
	if (bp->enabled == 1) {
		assert(proc->pid != 0);
		enable_breakpoint(proc, bp);
	}
	return 0;
}

int
breakpoint_turn_off(struct breakpoint *bp, struct Process *proc)
{
	bp->enabled--;
	if (bp->enabled == 0)
		disable_breakpoint(proc, bp);
	assert(bp->enabled >= 0);
	return 0;
}

struct breakpoint *
insert_breakpoint(struct Process *proc, void *addr,
		  struct library_symbol *libsym)
{
	Process *leader = proc->leader;

	/* Only the group leader should be getting the breakpoints and
	 * thus have ->breakpoint initialized.  */
	assert(leader != NULL);
	assert(leader->breakpoints != NULL);

	debug(DEBUG_FUNCTION, "insert_breakpoint(pid=%d, addr=%p, symbol=%s)",
	      proc->pid, addr, libsym ? libsym->name : "NULL");

	assert(addr != 0);

	/* XXX what we need to do instead is have a list of
	 * breakpoints that are enabled at this address.  The
	 * following works if every breakpoint is the same and there's
	 * no extra data, but that doesn't hold anymore.  For now it
	 * will suffice, about the only realistic case where we need
	 * to have more than one breakpoint per address is return from
	 * a recursive library call.  */
	struct breakpoint *sbp = dict_find_entry(leader->breakpoints, addr);
	if (sbp == NULL) {
		sbp = malloc(sizeof(*sbp));
		if (sbp == NULL
		    || breakpoint_init(sbp, proc, addr, libsym) < 0) {
			free(sbp);
			return NULL;
		}
		if (proc_add_breakpoint(leader, sbp) < 0) {
		fail:
			breakpoint_destroy(sbp);
			free(sbp);
			return NULL;
		}
	}

	if (breakpoint_turn_on(sbp, proc) < 0) {
		proc_remove_breakpoint(leader, sbp);
		goto fail;
	}

	return sbp;
}

void
delete_breakpoint(Process *proc, void *addr)
{
	debug(DEBUG_FUNCTION, "delete_breakpoint(pid=%d, addr=%p)", proc->pid, addr);

	Process * leader = proc->leader;
	assert(leader != NULL);

	struct breakpoint *sbp = dict_find_entry(leader->breakpoints, addr);
	assert(sbp != NULL);
	/* This should only happen on out-of-memory conditions. */
	if (sbp == NULL)
		return;

	if (breakpoint_turn_off(sbp, proc) < 0) {
		fprintf(stderr, "Couldn't turn off the breakpoint %s@%p\n",
			breakpoint_name(sbp), sbp->addr);
		return;
	}
	if (sbp->enabled == 0) {
		proc_remove_breakpoint(leader, sbp);
		breakpoint_destroy(sbp);
		free(sbp);
	}
}

const char *
breakpoint_name(const struct breakpoint *bp)
{
	assert(bp != NULL);
	return bp->libsym != NULL ? bp->libsym->name : NULL;
}

struct library *
breakpoint_library(const struct breakpoint *bp)
{
	assert(bp != NULL);
	return bp->libsym != NULL ? bp->libsym->lib : NULL;
}

static void
enable_bp_cb(void *addr, void *sbp, void *proc)
{
	debug(DEBUG_FUNCTION, "enable_bp_cb(pid=%d)", ((Process *)proc)->pid);
	if (((struct breakpoint *)sbp)->enabled)
		enable_breakpoint(proc, sbp);
}

void
enable_all_breakpoints(Process *proc)
{
	debug(DEBUG_FUNCTION, "enable_all_breakpoints(pid=%d)", proc->pid);

	debug(1, "Enabling breakpoints for pid %u...", proc->pid);
	if (proc->breakpoints) {
		dict_apply_to_all(proc->breakpoints, enable_bp_cb,
				  proc);
	}
#ifdef __mips__
	{
		/*
		 * I'm sure there is a nicer way to do this. We need to
		 * insert breakpoints _after_ the child has been started.
		 */
		struct library_symbol *sym;
		struct library_symbol *new_sym;
		sym=proc->list_of_symbols;
		while(sym){
			void *addr= sym2addr(proc,sym);
			if(!addr){
				sym=sym->next;
				continue;
			}
			if(dict_find_entry(proc->breakpoints,addr)){
				sym=sym->next;
				continue;
			}
			debug(2,"inserting bp %p %s",addr,sym->name);
			new_sym=malloc(sizeof(*new_sym) + strlen(sym->name) + 1);
			memcpy(new_sym,sym,sizeof(*new_sym) + strlen(sym->name) + 1);
			new_sym->next=proc->list_of_symbols;
			proc->list_of_symbols=new_sym;
			insert_breakpoint(proc, addr, new_sym);
			sym=sym->next;
		}
	}
#endif
}

static void
disable_bp_cb(void *addr, void *sbp, void *proc)
{
	debug(DEBUG_FUNCTION, "disable_bp_cb(pid=%d)", ((Process *)proc)->pid);
	if (((struct breakpoint *)sbp)->enabled)
		disable_breakpoint(proc, sbp);
}

void
disable_all_breakpoints(Process *proc) {
	debug(DEBUG_FUNCTION, "disable_all_breakpoints(pid=%d)", proc->pid);
	assert(proc->leader == proc);
	dict_apply_to_all(proc->breakpoints, disable_bp_cb, proc);
}

/* XXX This is not currently properly supported.  On clone, this is
 * just sliced.  Hopefully at the point that clone is done, this
 * breakpoint is not necessary anymore.  If this use case ends up
 * being important, we need to add a clone and destroy callbacks to
 * breakpoints, and we should also probably drop arch_breakpoint_data
 * so that we don't end up with two different customization mechanisms
 * for one structure.  */
struct entry_breakpoint {
	struct breakpoint super;
	target_address_t dyn_addr;
};

static void
entry_breakpoint_on_hit(struct breakpoint *a, struct Process *proc)
{
	struct entry_breakpoint *bp = (void *)a;
	if (proc == NULL || proc->leader == NULL)
		return;
	target_address_t dyn_addr = bp->dyn_addr;
	delete_breakpoint(proc, bp->super.addr);
	linkmap_init(proc, dyn_addr);
	arch_dynlink_done(proc);
}

int
entry_breakpoint_init(struct Process *proc,
		      struct entry_breakpoint *bp, target_address_t addr,
		      struct library *lib)
{
	int err;
	if ((err = breakpoint_init(&bp->super, proc, addr, NULL)) < 0)
		return err;

	static struct bp_callbacks entry_callbacks = {
		.on_hit = entry_breakpoint_on_hit,
	};
	bp->super.cbs = &entry_callbacks;
	bp->dyn_addr = lib->dyn_addr;
	return 0;
}

int
breakpoints_init(Process *proc)
{
	debug(DEBUG_FUNCTION, "breakpoints_init(pid=%d)", proc->pid);

	/* XXX breakpoint dictionary should be initialized
	 * outside.  Here we just put in breakpoints.  */
	assert(proc->breakpoints != NULL);

	/* Only the thread group leader should hold the breakpoints.  */
	assert(proc->leader == proc);

	/* N.B. the following used to be conditional on this, and
	 * maybe it still needs to be.  */
	assert(proc->filename != NULL);

	struct library *lib = ltelf_read_main_binary(proc, proc->filename);
	struct entry_breakpoint *entry_bp = NULL;
	int bp_state = 0;
	int result = -1;
	switch (lib != NULL) {
	fail:
		switch (bp_state) {
		case 2:
			proc_remove_library(proc, lib);
			proc_remove_breakpoint(proc, &entry_bp->super);
		case 1:
			breakpoint_destroy(&entry_bp->super);
		}
		library_destroy(lib);
		free(entry_bp);
	case 0:
		return result;
	}

	entry_bp = malloc(sizeof(*entry_bp));
	if (entry_bp == NULL
	    || (result = entry_breakpoint_init(proc, entry_bp,
					       lib->entry, lib)) < 0)
		goto fail;
	++bp_state;

	if ((result = proc_add_breakpoint(proc, &entry_bp->super)) < 0)
		goto fail;
	++bp_state;

	if ((result = breakpoint_turn_on(&entry_bp->super, proc)) < 0)
		goto fail;
	proc_add_library(proc, lib);

	proc->callstack_depth = 0;
	return 0;
}