aboutsummaryrefslogtreecommitdiff
path: root/breakpoints.c
blob: 28bdb4bf817a3e2cf19bb96a80da1d554a6f2aad (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
#if HAVE_CONFIG_H
#include "config.h"
#endif

#include "ltrace.h"
#include "options.h"
#include "output.h"

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

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

/*
  Dictionary code done by Morten Eriksen <mortene@sim.no>.

  FIXME: should we merge with dictionary code in demangle.c? 19990704 mortene.
*/

struct dict_entry {
	struct process * proc;
	struct breakpoint brk; /* addr field of struct is the hash key. */
	struct dict_entry * next;
};

#define DICTTABLESIZE 997 /* Semi-randomly selected prime number. */
static struct dict_entry * dict_buckets[DICTTABLESIZE];
static int dict_initialized = 0;

static void dict_init(void);
static void dict_clear(void);
static struct breakpoint * dict_enter(struct process * proc, void * brkaddr);
static struct breakpoint * dict_find_entry(struct process * proc, void * brkaddr);
static void dict_apply_to_all(void (* func)(struct process *, struct breakpoint *, void * data), void * data);


static void
dict_init(void) {
	int i;
	/* FIXME: is this necessary? Check with ANSI C spec. 19990702 mortene. */
	for (i = 0; i < DICTTABLESIZE; i++) dict_buckets[i] = NULL;
	dict_initialized = 1;
}

static void
dict_clear(void) {
	int i;
	struct dict_entry * entry, * nextentry;

	for (i = 0; i < DICTTABLESIZE; i++) {
		for (entry = dict_buckets[i]; entry != NULL; entry = nextentry) {
			nextentry = entry->next;
			free(entry);
		}
		dict_buckets[i] = NULL;
	}
}

static struct breakpoint *
dict_enter(struct process * proc, void * brkaddr) {
	struct dict_entry * entry, * newentry;
	unsigned int bucketpos = ((unsigned long int)brkaddr) % DICTTABLESIZE;

	newentry = malloc(sizeof(struct dict_entry));
	if (!newentry) {
		perror("malloc");
		return NULL;
	}

	newentry->proc = proc;
	newentry->brk.addr = brkaddr;
	newentry->brk.enabled = 0;
	newentry->next = NULL;

	entry = dict_buckets[bucketpos];
	while (entry && entry->next) entry = entry->next;

	if (entry) entry->next = newentry;
	else dict_buckets[bucketpos] = newentry;

	if (opt_d > 2)
		output_line(0, "new brk dict entry at %p\n", brkaddr);

	return &(newentry->brk);
}

static struct breakpoint *
dict_find_entry(struct process * proc, void * brkaddr) {
	unsigned int bucketpos = ((unsigned long int)brkaddr) % DICTTABLESIZE;
	struct dict_entry * entry = dict_buckets[bucketpos];
	while (entry) {
		if ((entry->brk.addr == brkaddr) && (entry->proc == proc)) break;
		entry = entry->next;
	}
	return entry ? &(entry->brk) : NULL;
}

static void
dict_apply_to_all(void (* func)(struct process *, struct breakpoint *, void * data), void * data) {
	int i;

	for (i = 0; i < DICTTABLESIZE; i++) {
		struct dict_entry * entry = dict_buckets[i];
		while (entry) {
			func(entry->proc, &(entry->brk), data);
			entry = entry->next;
		}
	}
}

#undef DICTTABLESIZE

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

struct breakpoint *
address2bpstruct(struct process * proc, void * addr) {
	return dict_find_entry(proc, addr);
}

void
insert_breakpoint(struct process * proc, void * addr) {
	struct breakpoint * sbp;

	if (!dict_initialized) {
		dict_init();
		atexit(dict_clear);
	}

	sbp = dict_find_entry(proc, addr);
	if (!sbp) sbp = dict_enter(proc, addr);
	if (!sbp) return;

	sbp->enabled++;
	if (sbp->enabled==1 && proc->pid) enable_breakpoint(proc->pid, sbp);
}

void
delete_breakpoint(struct process * proc, void * addr) {
	struct breakpoint * sbp = dict_find_entry(proc, 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->pid, sbp);
	assert(sbp->enabled >= 0);
}

static void
enable_bp_cb(struct process * proc, struct breakpoint * sbp, void * data) {
	struct process * myproc = (struct process *)data;
	if (myproc == proc && sbp->enabled) enable_breakpoint(proc->pid, sbp);
}

void
enable_all_breakpoints(struct process * proc) {
	if (proc->breakpoints_enabled <= 0) {
		if (opt_d>0) {
			output_line(0, "Enabling breakpoints for pid %u...", proc->pid);
		}
		dict_apply_to_all(enable_bp_cb, proc);
	}
	proc->breakpoints_enabled = 1;
}

static void
disable_bp_cb(struct process * proc, struct breakpoint * sbp, void * data) {
	struct process * myproc = (struct process *)data;
	if (myproc == proc && sbp->enabled) disable_breakpoint(proc->pid, sbp);
}

void
disable_all_breakpoints(struct process * proc) {
	if (proc->breakpoints_enabled) {
		if (opt_d>0) {
			output_line(0, "Disabling breakpoints for pid %u...", proc->pid);
		}
		dict_apply_to_all(disable_bp_cb, proc);
	}
	proc->breakpoints_enabled = 0;
}