ARM: add arm_mode_name()
[openocd/ztw.git] / src / target / breakpoints.c
blobf657ddce546c2a106aceef3cb8e46c041a7c73c0
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include "target.h"
25 #include "log.h"
26 #include "breakpoints.h"
29 static char *breakpoint_type_strings[] =
31 "hardware",
32 "software"
35 static char *watchpoint_rw_strings[] =
37 "read",
38 "write",
39 "access"
42 // monotonic counter/id-number for breakpoints and watch points
43 static int bpwp_unique_id;
45 int breakpoint_add(struct target *target, uint32_t address, uint32_t length, enum breakpoint_type type)
47 struct breakpoint *breakpoint = target->breakpoints;
48 struct breakpoint **breakpoint_p = &target->breakpoints;
49 int retval;
50 int n;
52 n = 0;
53 while (breakpoint)
55 n++;
56 if (breakpoint->address == address){
57 LOG_DEBUG("Duplicate Breakpoint address: 0x%08" PRIx32 " (BP %d)",
58 address, breakpoint->unique_id );
59 return ERROR_OK;
61 breakpoint_p = &breakpoint->next;
62 breakpoint = breakpoint->next;
65 (*breakpoint_p) = malloc(sizeof(struct breakpoint));
66 (*breakpoint_p)->address = address;
67 (*breakpoint_p)->length = length;
68 (*breakpoint_p)->type = type;
69 (*breakpoint_p)->set = 0;
70 (*breakpoint_p)->orig_instr = malloc(length);
71 (*breakpoint_p)->next = NULL;
72 (*breakpoint_p)->unique_id = bpwp_unique_id++;
74 if ((retval = target_add_breakpoint(target, *breakpoint_p)) != ERROR_OK)
76 switch (retval)
78 case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
79 LOG_INFO("can't add %s breakpoint, resource not available (BPID=%d)",
80 breakpoint_type_strings[(*breakpoint_p)->type],
81 (*breakpoint_p)->unique_id );
83 free((*breakpoint_p)->orig_instr);
84 free(*breakpoint_p);
85 *breakpoint_p = NULL;
86 return retval;
87 break;
88 case ERROR_TARGET_NOT_HALTED:
89 LOG_INFO("can't add breakpoint while target is running (BPID: %d)",
90 (*breakpoint_p)->unique_id );
91 free((*breakpoint_p)->orig_instr);
92 free(*breakpoint_p);
93 *breakpoint_p = NULL;
94 return retval;
95 break;
96 default:
97 break;
101 LOG_DEBUG("added %s breakpoint at 0x%8.8" PRIx32 " of length 0x%8.8x, (BPID: %d)",
102 breakpoint_type_strings[(*breakpoint_p)->type],
103 (*breakpoint_p)->address, (*breakpoint_p)->length,
104 (*breakpoint_p)->unique_id );
106 return ERROR_OK;
109 /* free up a breakpoint */
110 static void breakpoint_free(struct target *target, struct breakpoint *breakpoint_remove)
112 struct breakpoint *breakpoint = target->breakpoints;
113 struct breakpoint **breakpoint_p = &target->breakpoints;
115 while (breakpoint)
117 if (breakpoint == breakpoint_remove)
118 break;
119 breakpoint_p = &breakpoint->next;
120 breakpoint = breakpoint->next;
123 if (breakpoint == NULL)
124 return;
126 target_remove_breakpoint(target, breakpoint);
128 LOG_DEBUG("BPID: %d", breakpoint->unique_id );
129 (*breakpoint_p) = breakpoint->next;
130 free(breakpoint->orig_instr);
131 free(breakpoint);
134 void breakpoint_remove(struct target *target, uint32_t address)
136 struct breakpoint *breakpoint = target->breakpoints;
137 struct breakpoint **breakpoint_p = &target->breakpoints;
139 while (breakpoint)
141 if (breakpoint->address == address)
142 break;
143 breakpoint_p = &breakpoint->next;
144 breakpoint = breakpoint->next;
147 if (breakpoint)
149 breakpoint_free(target, breakpoint);
151 else
153 LOG_ERROR("no breakpoint at address 0x%8.8" PRIx32 " found", address);
157 void breakpoint_clear_target(struct target *target)
159 struct breakpoint *breakpoint;
160 LOG_DEBUG("Delete all breakpoints for target: %s", target_get_name( target ));
161 while ((breakpoint = target->breakpoints) != NULL)
163 breakpoint_free(target, breakpoint);
167 struct breakpoint* breakpoint_find(struct target *target, uint32_t address)
169 struct breakpoint *breakpoint = target->breakpoints;
171 while (breakpoint)
173 if (breakpoint->address == address)
174 return breakpoint;
175 breakpoint = breakpoint->next;
178 return NULL;
181 int watchpoint_add(struct target *target, uint32_t address, uint32_t length,
182 enum watchpoint_rw rw, uint32_t value, uint32_t mask)
184 struct watchpoint *watchpoint = target->watchpoints;
185 struct watchpoint **watchpoint_p = &target->watchpoints;
186 int retval;
187 char *reason;
189 while (watchpoint)
191 if (watchpoint->address == address) {
192 if (watchpoint->length != length
193 || watchpoint->value != value
194 || watchpoint->mask != mask
195 || watchpoint->rw != rw) {
196 LOG_ERROR("address 0x%8.8" PRIx32
197 "already has watchpoint %d",
198 address, watchpoint->unique_id);
199 return ERROR_FAIL;
202 /* ignore duplicate watchpoint */
203 return ERROR_OK;
205 watchpoint_p = &watchpoint->next;
206 watchpoint = watchpoint->next;
209 (*watchpoint_p) = calloc(1, sizeof(struct watchpoint));
210 (*watchpoint_p)->address = address;
211 (*watchpoint_p)->length = length;
212 (*watchpoint_p)->value = value;
213 (*watchpoint_p)->mask = mask;
214 (*watchpoint_p)->rw = rw;
215 (*watchpoint_p)->unique_id = bpwp_unique_id++;
217 retval = target_add_watchpoint(target, *watchpoint_p);
218 switch (retval) {
219 case ERROR_OK:
220 break;
221 case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
222 reason = "resource not available";
223 goto bye;
224 case ERROR_TARGET_NOT_HALTED:
225 reason = "target running";
226 goto bye;
227 default:
228 reason = "unrecognized error";
229 bye:
230 LOG_ERROR("can't add %s watchpoint at 0x%8.8" PRIx32 ", %s",
231 watchpoint_rw_strings[(*watchpoint_p)->rw],
232 address, reason);
233 free (*watchpoint_p);
234 *watchpoint_p = NULL;
235 return retval;
238 LOG_DEBUG("added %s watchpoint at 0x%8.8" PRIx32
239 " of length 0x%8.8" PRIx32 " (WPID: %d)",
240 watchpoint_rw_strings[(*watchpoint_p)->rw],
241 (*watchpoint_p)->address,
242 (*watchpoint_p)->length,
243 (*watchpoint_p)->unique_id );
245 return ERROR_OK;
248 static void watchpoint_free(struct target *target, struct watchpoint *watchpoint_remove)
250 struct watchpoint *watchpoint = target->watchpoints;
251 struct watchpoint **watchpoint_p = &target->watchpoints;
253 while (watchpoint)
255 if (watchpoint == watchpoint_remove)
256 break;
257 watchpoint_p = &watchpoint->next;
258 watchpoint = watchpoint->next;
261 if (watchpoint == NULL)
262 return;
263 target_remove_watchpoint(target, watchpoint);
264 LOG_DEBUG("WPID: %d", watchpoint->unique_id );
265 (*watchpoint_p) = watchpoint->next;
266 free(watchpoint);
269 void watchpoint_remove(struct target *target, uint32_t address)
271 struct watchpoint *watchpoint = target->watchpoints;
272 struct watchpoint **watchpoint_p = &target->watchpoints;
274 while (watchpoint)
276 if (watchpoint->address == address)
277 break;
278 watchpoint_p = &watchpoint->next;
279 watchpoint = watchpoint->next;
282 if (watchpoint)
284 watchpoint_free(target, watchpoint);
286 else
288 LOG_ERROR("no watchpoint at address 0x%8.8" PRIx32 " found", address);
292 void watchpoint_clear_target(struct target *target)
294 struct watchpoint *watchpoint;
295 LOG_DEBUG("Delete all watchpoints for target: %s", target_get_name( target ));
296 while ((watchpoint = target->watchpoints) != NULL)
298 watchpoint_free(target, watchpoint);