aboutsummaryrefslogtreecommitdiff
path: root/breakpoints.c
blob: 6016975be8629e26d3ebb00028b82ff4e0a17b47 (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
#include "config.h"

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

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

#include "breakpoint.h"
#include "common.h"

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_destroy(struct breakpoint *bp)
{
	assert(bp != NULL);
	if (bp->cbs != NULL && bp->cbs->on_destroy != NULL)
		(bp->cbs->on_destroy) (bp);
}

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

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);
}

struct breakpoint *
insert_breakpoint(Process *proc, void *addr,
		  struct library_symbol *libsym, int enable)
{
	struct breakpoint *sbp;

	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);

#ifdef __arm__
	int thumb_mode = (int)addr & 1;
	if (thumb_mode)
		addr = (void *)((int)addr & ~1);
#endif

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

	if (!addr)
		return NULL;

	if (libsym)
		libsym->needs_init = 0;

	sbp = dict_find_entry(leader->breakpoints, addr);
	if (sbp == NULL) {
		sbp = calloc(1, sizeof(*sbp));
		if (sbp == NULL) {
			return NULL;	/* TODO FIXME XXX: error_mem */
		}
		dict_enter(leader->breakpoints, addr, sbp);
		sbp->addr = addr;
		sbp->libsym = libsym;
	}
#ifdef __arm__
	sbp->thumb_mode = thumb_mode | proc->thumb_mode;
	proc->thumb_mode = 0;
#endif
	sbp->enabled++;
	if (sbp->enabled == 1 && enable) {
		assert(proc->pid != 0);
		enable_breakpoint(proc, sbp);
	}

	return sbp;
}

void
delete_breakpoint(Process *proc, void *addr)
{
	struct breakpoint *sbp;

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

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

	sbp = dict_find_entry(leader->breakpoints, addr);
	assert(sbp);		/* FIXME: remove after debugging has been done. */
	/* This should only happen on out-of-memory conditions. */
	if (sbp == NULL)
		return;

	sbp->enabled--;
	if (sbp->enabled == 0)
		disable_breakpoint(proc, sbp);
	assert(sbp->enabled >= 0);
}

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);
#ifdef __powerpc__
	unsigned long a;

	/*
	 * PPC HACK! (XXX FIXME TODO)
	 * If the dynamic linker hasn't populated the PLT then
	 * dont enable the breakpoints
	 */
	if (options.libcalls) {
		a = ptrace(PTRACE_PEEKTEXT, proc->pid,
			   sym2addr(proc, proc->list_of_symbols),
			   0);
		if (a == 0x0)
			return;
	}
#endif

	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);
}

static void
free_bp_cb(void *addr, void *sbp, void *data) {
	debug(DEBUG_FUNCTION, "free_bp_cb(sbp=%p)", sbp);
	assert(sbp);
	free(sbp);
}

static void
entry_callback_hit(struct breakpoint *bp, struct Process *proc)
{
	if (proc == NULL || proc->leader == NULL)
		return;
	delete_breakpoint(proc, bp->addr); // xxx
	reinitialize_breakpoints(proc->leader);
}

int
breakpoints_init(Process *proc, int enable)
{
	debug(DEBUG_FUNCTION, "breakpoints_init(pid=%d)", proc->pid);
	if (proc->breakpoints) {	/* let's remove that struct */
		dict_apply_to_all(proc->breakpoints, free_bp_cb, NULL);
		dict_clear(proc->breakpoints);
		proc->breakpoints = NULL;
	}

	/* Only the thread group leader should hold the breakpoints.
	 * (N.B. PID may be set to 0 temporarily when called by
	 * handle_exec).  */
	assert(proc->leader == proc);

	proc->breakpoints = dict_init(dict_key2hash_int,
				      dict_key_cmp_int);

	destroy_library_symbol_chain(proc->list_of_symbols);
	proc->list_of_symbols = NULL;

	GElf_Addr entry;
	if (options.libcalls && proc->filename) {
		proc->list_of_symbols = read_elf(proc, &entry);
		if (proc->list_of_symbols == NULL) {
		fail:
			/* XXX leak breakpoints */
			return -1;
		}

		if (opt_e) {
			struct library_symbol **tmp1 = &proc->list_of_symbols;
			while (*tmp1) {
				struct opt_e_t *tmp2 = opt_e;
				int keep = !opt_e_enable;

				while (tmp2) {
					if (!strcmp((*tmp1)->name,
						    tmp2->name)) {
						keep = opt_e_enable;
					}
					tmp2 = tmp2->next;
				}
				if (!keep) {
					*tmp1 = (*tmp1)->next;
				} else {
					tmp1 = &((*tmp1)->next);
				}
			}
		}
	}

	struct breakpoint *entry_bp
		= insert_breakpoint(proc, (void *)(uintptr_t)entry, NULL, 1);
	if (entry_bp == NULL) {
		fprintf(stderr, "fail!\n");
		goto fail;
	}

	static struct bp_callbacks entry_callbacks = {
		.on_hit = entry_callback_hit,
	};
	entry_bp->cbs = &entry_callbacks;

	proc->callstack_depth = 0;
	return 0;
}

void
reinitialize_breakpoints(Process *proc) {
	struct library_symbol *sym;

	debug(DEBUG_FUNCTION, "reinitialize_breakpoints(pid=%d)", proc->pid);

	sym = proc->list_of_symbols;

	while (sym) {
		if (sym->needs_init) {
			insert_breakpoint(proc, sym2addr(proc, sym), sym, 1);
			if (sym->needs_init && !sym->is_weak) {
				fprintf(stderr,
					"could not re-initialize breakpoint for \"%s\" in file \"%s\"\n",
					sym->name, proc->filename);
				exit(1);
			}
		}
		sym = sym->next;
	}
}