breakpoints.c 7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/***************************************************************************
 *   Copyright (C) 2005 by Dominic Rath                                    *
 *   Dominic.Rath@gmx.de                                                   *
 *                                                                         *
 *   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.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
20
#ifdef HAVE_CONFIG_H
21
#include "config.h"
22
#endif
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

#include <stdlib.h>

#include "binarybuffer.h"
#include "target.h"
#include "log.h"
#include "types.h"

#include "breakpoints.h"

char *breakpoint_type_strings[] =
{
	"hardware",
	"software"
};

char *watchpoint_rw_strings[] =
{
	"read",
	"write",
	"access"
};

int breakpoint_add(target_t *target, u32 address, u32 length, enum breakpoint_type type)
{
	breakpoint_t *breakpoint = target->breakpoints;
	breakpoint_t **breakpoint_p = &target->breakpoints;
	int retval;
51
	
52
53
54
55
56
57
58
	while (breakpoint)
	{
		if (breakpoint->address == address)
			return ERROR_OK;
		breakpoint_p = &breakpoint->next;
		breakpoint = breakpoint->next;
	}
59
60
61
62
63
64
		
	(*breakpoint_p) = malloc(sizeof(breakpoint_t));
	(*breakpoint_p)->address = address;
	(*breakpoint_p)->length = length;
	(*breakpoint_p)->type = type;
	(*breakpoint_p)->set = 0;
65
	(*breakpoint_p)->orig_instr = malloc(length);
66
	(*breakpoint_p)->next = NULL;
67
	
68
	if ((retval = target->type->add_breakpoint(target, *breakpoint_p)) != ERROR_OK)
69
70
71
72
	{
		switch (retval)
		{
			case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
73
				LOG_INFO("can't add %s breakpoint, resource not available", breakpoint_type_strings[(*breakpoint_p)->type]);
ntfreak's avatar
ntfreak committed
74
75
				free((*breakpoint_p)->orig_instr);
				free(*breakpoint_p);
76
				*breakpoint_p = NULL;
77
78
79
				return retval;
				break;
			case ERROR_TARGET_NOT_HALTED:
80
				LOG_INFO("can't add breakpoint while target is running");
ntfreak's avatar
ntfreak committed
81
82
				free((*breakpoint_p)->orig_instr);
				free(*breakpoint_p);
83
				*breakpoint_p = NULL;
84
85
86
87
88
89
90
				return retval;
				break;
			default:
				break;
		}
	}
	
91
	LOG_DEBUG("added %s breakpoint at 0x%8.8x of length 0x%8.8x", 
92
93
		breakpoint_type_strings[(*breakpoint_p)->type],
		(*breakpoint_p)->address, (*breakpoint_p)->length);
94
95
96
97
	
	return ERROR_OK;
}

98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* free up a breakpoint */
static void breakpoint_free(target_t *target, breakpoint_t *breakpoint_remove)
{
	breakpoint_t *breakpoint = target->breakpoints;
	breakpoint_t **breakpoint_p = &target->breakpoints;
	
	while (breakpoint)
	{
		if (breakpoint==breakpoint_remove)
			break;
		breakpoint_p = &breakpoint->next;
		breakpoint = breakpoint->next;
	}
	
	if (breakpoint==NULL)
		return;
	
	target->type->remove_breakpoint(target, breakpoint);
	
	(*breakpoint_p) = breakpoint->next;
	free(breakpoint->orig_instr);
	free(breakpoint);
}

122
void breakpoint_remove(target_t *target, u32 address)
123
124
125
126
127
128
129
130
131
132
133
134
135
136
{
	breakpoint_t *breakpoint = target->breakpoints;
	breakpoint_t **breakpoint_p = &target->breakpoints;
	
	while (breakpoint)
	{
		if (breakpoint->address == address)
			break;
		breakpoint_p = &breakpoint->next;
		breakpoint = breakpoint->next;
	}
	
	if (breakpoint)
	{
137
		breakpoint_free(target, breakpoint);
138
139
140
	}
	else
	{
141
		LOG_ERROR("no breakpoint at address 0x%8.8x found", address);
142
143
144
	}
}

145
146
147
148
149
150
151
152
153
void breakpoint_clear_target(target_t *target)
{
	breakpoint_t *breakpoint;
	while ((breakpoint = target->breakpoints)!=NULL)
	{
		breakpoint_free(target, breakpoint);
	}
}

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
breakpoint_t* breakpoint_find(target_t *target, u32 address)
{
	breakpoint_t *breakpoint = target->breakpoints;
	
	while (breakpoint)
	{
		if (breakpoint->address == address)
			return breakpoint;
		breakpoint = breakpoint->next;
	}
	
	return NULL;
}

int watchpoint_add(target_t *target, u32 address, u32 length, enum watchpoint_rw rw, u32 value, u32 mask)
{
	watchpoint_t *watchpoint = target->watchpoints;
	watchpoint_t **watchpoint_p = &target->watchpoints;
	int retval;
		
	while (watchpoint)
	{
		if (watchpoint->address == address)
			return ERROR_OK;
		watchpoint_p = &watchpoint->next;
		watchpoint = watchpoint->next;
	}
	
182
183
184
185
186
187
188
189
190
191
	(*watchpoint_p) = malloc(sizeof(watchpoint_t));
	(*watchpoint_p)->address = address;
	(*watchpoint_p)->length = length;
	(*watchpoint_p)->value = value;
	(*watchpoint_p)->mask = mask;
	(*watchpoint_p)->rw = rw;
	(*watchpoint_p)->set = 0;
	(*watchpoint_p)->next = NULL;
		
	if ((retval = target->type->add_watchpoint(target, *watchpoint_p)) != ERROR_OK)
192
193
194
195
	{
		switch (retval)
		{
			case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
196
				LOG_INFO("can't add %s watchpoint, resource not available", watchpoint_rw_strings[(*watchpoint_p)->rw]);
197
198
				free (*watchpoint_p);
				*watchpoint_p = NULL;
199
200
				return retval;
				break;
201
			case ERROR_TARGET_NOT_HALTED:
202
				LOG_INFO("can't add watchpoint while target is running");
203
204
				free (*watchpoint_p);
				*watchpoint_p = NULL;
205
206
				return retval;
				break;
207
			default:
208
				LOG_ERROR("unknown error");
209
210
211
212
213
				exit(-1);
				break;
		}
	}
	
214
	LOG_DEBUG("added %s watchpoint at 0x%8.8x of length 0x%8.8x",
215
216
		watchpoint_rw_strings[(*watchpoint_p)->rw],
		(*watchpoint_p)->address, (*watchpoint_p)->length);
217
218
219
220
	
	return ERROR_OK;
}

221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
static void watchpoint_free(target_t *target, watchpoint_t *watchpoint_remove)
{
	watchpoint_t *watchpoint = target->watchpoints;
	watchpoint_t **watchpoint_p = &target->watchpoints;

	while (watchpoint)
	{
		if (watchpoint == watchpoint_remove)
			break;
		watchpoint_p = &watchpoint->next;
		watchpoint = watchpoint->next;
	}
	
	if (watchpoint==NULL)
		return;
	target->type->remove_watchpoint(target, watchpoint);
	(*watchpoint_p) = watchpoint->next;
	free(watchpoint);
}



243
void watchpoint_remove(target_t *target, u32 address)
244
245
246
247
248
249
250
251
252
253
254
255
256
257
{
	watchpoint_t *watchpoint = target->watchpoints;
	watchpoint_t **watchpoint_p = &target->watchpoints;
	
	while (watchpoint)
	{
		if (watchpoint->address == address)
			break;
		watchpoint_p = &watchpoint->next;
		watchpoint = watchpoint->next;
	}
	
	if (watchpoint)
	{
258
		watchpoint_free(target, watchpoint);
259
260
261
	}
	else
	{
262
		LOG_ERROR("no watchpoint at address 0x%8.8x found", address);
263
264
	}
}
265
266
267
268
269
270
271
272
273
274


void watchpoint_clear_target(target_t *target)
{
	watchpoint_t *watchpoint;
	while ((watchpoint = target->watchpoints)!=NULL)
	{
		watchpoint_free(target, watchpoint);
	}
}