1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) ST-Ericsson SA 2011 *
6 * michel.jaouen@stericsson.com : smp minimum support *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
28 #include <helper/log.h>
29 #include "breakpoints.h"
32 static char *breakpoint_type_strings
[] =
38 static char *watchpoint_rw_strings
[] =
45 // monotonic counter/id-number for breakpoints and watch points
46 static int bpwp_unique_id
;
48 int breakpoint_add_internal(struct target
*target
, uint32_t address
, uint32_t length
, enum breakpoint_type type
)
50 struct breakpoint
*breakpoint
= target
->breakpoints
;
51 struct breakpoint
**breakpoint_p
= &target
->breakpoints
;
60 if (breakpoint
->address
== address
) {
61 /* FIXME don't assume "same address" means "same
62 * breakpoint" ... check all the parameters before
65 LOG_DEBUG("Duplicate Breakpoint address: 0x%08" PRIx32
" (BP %d)",
66 address
, breakpoint
->unique_id
);
69 breakpoint_p
= &breakpoint
->next
;
70 breakpoint
= breakpoint
->next
;
73 (*breakpoint_p
) = malloc(sizeof(struct breakpoint
));
74 (*breakpoint_p
)->address
= address
;
75 (*breakpoint_p
)->length
= length
;
76 (*breakpoint_p
)->type
= type
;
77 (*breakpoint_p
)->set
= 0;
78 (*breakpoint_p
)->orig_instr
= malloc(length
);
79 (*breakpoint_p
)->next
= NULL
;
80 (*breakpoint_p
)->unique_id
= bpwp_unique_id
++;
82 retval
= target_add_breakpoint(target
, *breakpoint_p
);
86 case ERROR_TARGET_RESOURCE_NOT_AVAILABLE
:
87 reason
= "resource not available";
89 case ERROR_TARGET_NOT_HALTED
:
90 reason
= "target running";
93 reason
= "unknown reason";
95 LOG_ERROR("can't add breakpoint: %s", reason
);
96 free((*breakpoint_p
)->orig_instr
);
102 LOG_DEBUG("added %s breakpoint at 0x%8.8" PRIx32
" of length 0x%8.8x, (BPID: %d)",
103 breakpoint_type_strings
[(*breakpoint_p
)->type
],
104 (*breakpoint_p
)->address
, (*breakpoint_p
)->length
,
105 (*breakpoint_p
)->unique_id
);
110 int breakpoint_add(struct target
*target
, uint32_t address
, uint32_t length
, enum breakpoint_type type
)
113 int retval
= ERROR_OK
;
116 struct target_list
*head
;
119 while(head
!= (struct target_list
*)NULL
)
122 retval
= breakpoint_add_internal(curr
, address
,length
, type
);
123 if (retval
!= ERROR_OK
) return retval
;
129 return(breakpoint_add_internal(target
, address
, length
, type
));
133 /* free up a breakpoint */
134 static void breakpoint_free(struct target
*target
, struct breakpoint
*breakpoint_to_remove
)
136 struct breakpoint
*breakpoint
= target
->breakpoints
;
137 struct breakpoint
**breakpoint_p
= &target
->breakpoints
;
142 if (breakpoint
== breakpoint_to_remove
)
144 breakpoint_p
= &breakpoint
->next
;
145 breakpoint
= breakpoint
->next
;
148 if (breakpoint
== NULL
)
151 retval
= target_remove_breakpoint(target
, breakpoint
);
153 LOG_DEBUG("free BPID: %d --> %d", breakpoint
->unique_id
, retval
);
154 (*breakpoint_p
) = breakpoint
->next
;
155 free(breakpoint
->orig_instr
);
159 void breakpoint_remove_internal(struct target
*target
, uint32_t address
)
161 struct breakpoint
*breakpoint
= target
->breakpoints
;
165 if (breakpoint
->address
== address
)
167 breakpoint
= breakpoint
->next
;
172 breakpoint_free(target
, breakpoint
);
176 LOG_ERROR("no breakpoint at address 0x%8.8" PRIx32
" found", address
);
179 void breakpoint_remove(struct target
*target
, uint32_t address
)
183 struct target_list
*head
;
186 while(head
!= (struct target_list
*)NULL
)
189 breakpoint_remove_internal(curr
, address
);
193 else breakpoint_remove_internal(target
, address
);
196 void breakpoint_clear_target_internal(struct target
*target
)
198 struct breakpoint
*breakpoint
;
200 LOG_DEBUG("Delete all breakpoints for target: %s",
201 target_name(target
));
202 while ((breakpoint
= target
->breakpoints
) != NULL
)
204 breakpoint_free(target
, breakpoint
);
208 void breakpoint_clear_target(struct target
*target
)
212 struct target_list
*head
;
215 while(head
!= (struct target_list
*)NULL
)
218 breakpoint_clear_target_internal(curr
);
222 else breakpoint_clear_target_internal(target
);
227 struct breakpoint
* breakpoint_find(struct target
*target
, uint32_t address
)
229 struct breakpoint
*breakpoint
= target
->breakpoints
;
233 if (breakpoint
->address
== address
)
235 breakpoint
= breakpoint
->next
;
241 int watchpoint_add(struct target
*target
, uint32_t address
, uint32_t length
,
242 enum watchpoint_rw rw
, uint32_t value
, uint32_t mask
)
244 struct watchpoint
*watchpoint
= target
->watchpoints
;
245 struct watchpoint
**watchpoint_p
= &target
->watchpoints
;
251 if (watchpoint
->address
== address
) {
252 if (watchpoint
->length
!= length
253 || watchpoint
->value
!= value
254 || watchpoint
->mask
!= mask
255 || watchpoint
->rw
!= rw
) {
256 LOG_ERROR("address 0x%8.8" PRIx32
257 "already has watchpoint %d",
258 address
, watchpoint
->unique_id
);
262 /* ignore duplicate watchpoint */
265 watchpoint_p
= &watchpoint
->next
;
266 watchpoint
= watchpoint
->next
;
269 (*watchpoint_p
) = calloc(1, sizeof(struct watchpoint
));
270 (*watchpoint_p
)->address
= address
;
271 (*watchpoint_p
)->length
= length
;
272 (*watchpoint_p
)->value
= value
;
273 (*watchpoint_p
)->mask
= mask
;
274 (*watchpoint_p
)->rw
= rw
;
275 (*watchpoint_p
)->unique_id
= bpwp_unique_id
++;
277 retval
= target_add_watchpoint(target
, *watchpoint_p
);
281 case ERROR_TARGET_RESOURCE_NOT_AVAILABLE
:
282 reason
= "resource not available";
284 case ERROR_TARGET_NOT_HALTED
:
285 reason
= "target running";
288 reason
= "unrecognized error";
290 LOG_ERROR("can't add %s watchpoint at 0x%8.8" PRIx32
", %s",
291 watchpoint_rw_strings
[(*watchpoint_p
)->rw
],
293 free (*watchpoint_p
);
294 *watchpoint_p
= NULL
;
298 LOG_DEBUG("added %s watchpoint at 0x%8.8" PRIx32
299 " of length 0x%8.8" PRIx32
" (WPID: %d)",
300 watchpoint_rw_strings
[(*watchpoint_p
)->rw
],
301 (*watchpoint_p
)->address
,
302 (*watchpoint_p
)->length
,
303 (*watchpoint_p
)->unique_id
);
308 static void watchpoint_free(struct target
*target
, struct watchpoint
*watchpoint_to_remove
)
310 struct watchpoint
*watchpoint
= target
->watchpoints
;
311 struct watchpoint
**watchpoint_p
= &target
->watchpoints
;
316 if (watchpoint
== watchpoint_to_remove
)
318 watchpoint_p
= &watchpoint
->next
;
319 watchpoint
= watchpoint
->next
;
322 if (watchpoint
== NULL
)
324 retval
= target_remove_watchpoint(target
, watchpoint
);
325 LOG_DEBUG("free WPID: %d --> %d", watchpoint
->unique_id
, retval
);
326 (*watchpoint_p
) = watchpoint
->next
;
330 void watchpoint_remove(struct target
*target
, uint32_t address
)
332 struct watchpoint
*watchpoint
= target
->watchpoints
;
336 if (watchpoint
->address
== address
)
338 watchpoint
= watchpoint
->next
;
343 watchpoint_free(target
, watchpoint
);
347 LOG_ERROR("no watchpoint at address 0x%8.8" PRIx32
" found", address
);
351 void watchpoint_clear_target(struct target
*target
)
353 struct watchpoint
*watchpoint
;
355 LOG_DEBUG("Delete all watchpoints for target: %s",
356 target_name(target
));
357 while ((watchpoint
= target
->watchpoints
) != NULL
)
359 watchpoint_free(target
, watchpoint
);