Remove whitespace at end of lines, step 1.
[openocd.git] / src / target / breakpoints.c
blobc8cd1c3bbe6518404591338b7b679251e4be32b2
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 int breakpoint_add(target_t *target, uint32_t address, uint32_t length, enum breakpoint_type type)
44 breakpoint_t *breakpoint = target->breakpoints;
45 breakpoint_t **breakpoint_p = &target->breakpoints;
46 int retval;
48 while (breakpoint)
50 if (breakpoint->address == address)
51 return ERROR_OK;
52 breakpoint_p = &breakpoint->next;
53 breakpoint = breakpoint->next;
56 (*breakpoint_p) = malloc(sizeof(breakpoint_t));
57 (*breakpoint_p)->address = address;
58 (*breakpoint_p)->length = length;
59 (*breakpoint_p)->type = type;
60 (*breakpoint_p)->set = 0;
61 (*breakpoint_p)->orig_instr = malloc(length);
62 (*breakpoint_p)->next = NULL;
64 if ((retval = target_add_breakpoint(target, *breakpoint_p)) != ERROR_OK)
66 switch (retval)
68 case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
69 LOG_INFO("can't add %s breakpoint, resource not available", breakpoint_type_strings[(*breakpoint_p)->type]);
70 free((*breakpoint_p)->orig_instr);
71 free(*breakpoint_p);
72 *breakpoint_p = NULL;
73 return retval;
74 break;
75 case ERROR_TARGET_NOT_HALTED:
76 LOG_INFO("can't add breakpoint while target is running");
77 free((*breakpoint_p)->orig_instr);
78 free(*breakpoint_p);
79 *breakpoint_p = NULL;
80 return retval;
81 break;
82 default:
83 break;
87 LOG_DEBUG("added %s breakpoint at 0x%8.8" PRIx32 " of length 0x%8.8x",
88 breakpoint_type_strings[(*breakpoint_p)->type],
89 (*breakpoint_p)->address, (*breakpoint_p)->length);
91 return ERROR_OK;
94 /* free up a breakpoint */
95 static void breakpoint_free(target_t *target, breakpoint_t *breakpoint_remove)
97 breakpoint_t *breakpoint = target->breakpoints;
98 breakpoint_t **breakpoint_p = &target->breakpoints;
100 while (breakpoint)
102 if (breakpoint == breakpoint_remove)
103 break;
104 breakpoint_p = &breakpoint->next;
105 breakpoint = breakpoint->next;
108 if (breakpoint == NULL)
109 return;
111 target_remove_breakpoint(target, breakpoint);
113 (*breakpoint_p) = breakpoint->next;
114 free(breakpoint->orig_instr);
115 free(breakpoint);
118 void breakpoint_remove(target_t *target, uint32_t address)
120 breakpoint_t *breakpoint = target->breakpoints;
121 breakpoint_t **breakpoint_p = &target->breakpoints;
123 while (breakpoint)
125 if (breakpoint->address == address)
126 break;
127 breakpoint_p = &breakpoint->next;
128 breakpoint = breakpoint->next;
131 if (breakpoint)
133 breakpoint_free(target, breakpoint);
135 else
137 LOG_ERROR("no breakpoint at address 0x%8.8" PRIx32 " found", address);
141 void breakpoint_clear_target(target_t *target)
143 breakpoint_t *breakpoint;
144 while ((breakpoint = target->breakpoints) != NULL)
146 breakpoint_free(target, breakpoint);
150 breakpoint_t* breakpoint_find(target_t *target, uint32_t address)
152 breakpoint_t *breakpoint = target->breakpoints;
154 while (breakpoint)
156 if (breakpoint->address == address)
157 return breakpoint;
158 breakpoint = breakpoint->next;
161 return NULL;
164 int watchpoint_add(target_t *target, uint32_t address, uint32_t length, enum watchpoint_rw rw, uint32_t value, uint32_t mask)
166 watchpoint_t *watchpoint = target->watchpoints;
167 watchpoint_t **watchpoint_p = &target->watchpoints;
168 int retval;
170 while (watchpoint)
172 if (watchpoint->address == address)
173 return ERROR_OK;
174 watchpoint_p = &watchpoint->next;
175 watchpoint = watchpoint->next;
178 (*watchpoint_p) = malloc(sizeof(watchpoint_t));
179 (*watchpoint_p)->address = address;
180 (*watchpoint_p)->length = length;
181 (*watchpoint_p)->value = value;
182 (*watchpoint_p)->mask = mask;
183 (*watchpoint_p)->rw = rw;
184 (*watchpoint_p)->set = 0;
185 (*watchpoint_p)->next = NULL;
187 if ((retval = target_add_watchpoint(target, *watchpoint_p)) != ERROR_OK)
189 switch (retval)
191 case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
192 LOG_INFO("can't add %s watchpoint, resource not available", watchpoint_rw_strings[(*watchpoint_p)->rw]);
193 free (*watchpoint_p);
194 *watchpoint_p = NULL;
195 return retval;
196 break;
197 case ERROR_TARGET_NOT_HALTED:
198 LOG_INFO("can't add watchpoint while target is running");
199 free (*watchpoint_p);
200 *watchpoint_p = NULL;
201 return retval;
202 break;
203 default:
204 LOG_ERROR("unknown error");
205 exit(-1);
206 break;
210 LOG_DEBUG("added %s watchpoint at 0x%8.8" PRIx32 " of length 0x%8.8x",
211 watchpoint_rw_strings[(*watchpoint_p)->rw],
212 (*watchpoint_p)->address, (*watchpoint_p)->length);
214 return ERROR_OK;
217 static void watchpoint_free(target_t *target, watchpoint_t *watchpoint_remove)
219 watchpoint_t *watchpoint = target->watchpoints;
220 watchpoint_t **watchpoint_p = &target->watchpoints;
222 while (watchpoint)
224 if (watchpoint == watchpoint_remove)
225 break;
226 watchpoint_p = &watchpoint->next;
227 watchpoint = watchpoint->next;
230 if (watchpoint == NULL)
231 return;
232 target_remove_watchpoint(target, watchpoint);
233 (*watchpoint_p) = watchpoint->next;
234 free(watchpoint);
237 void watchpoint_remove(target_t *target, uint32_t address)
239 watchpoint_t *watchpoint = target->watchpoints;
240 watchpoint_t **watchpoint_p = &target->watchpoints;
242 while (watchpoint)
244 if (watchpoint->address == address)
245 break;
246 watchpoint_p = &watchpoint->next;
247 watchpoint = watchpoint->next;
250 if (watchpoint)
252 watchpoint_free(target, watchpoint);
254 else
256 LOG_ERROR("no watchpoint at address 0x%8.8" PRIx32 " found", address);
260 void watchpoint_clear_target(target_t *target)
262 watchpoint_t *watchpoint;
263 while ((watchpoint = target->watchpoints) != NULL)
265 watchpoint_free(target, watchpoint);