- prepare OpenOCD for branching, created ./trunk/
[fw/openocd] / src / target / breakpoints.c
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 #include "config.h"
21
22 #include <stdlib.h>
23
24 #include "binarybuffer.h"
25 #include "target.h"
26 #include "log.h"
27 #include "types.h"
28
29 #include "breakpoints.h"
30
31 char *breakpoint_type_strings[] =
32 {
33         "hardware",
34         "software"
35 };
36
37 char *watchpoint_rw_strings[] =
38 {
39         "read",
40         "write",
41         "access"
42 };
43
44 int breakpoint_add(target_t *target, u32 address, u32 length, enum breakpoint_type type)
45 {
46         breakpoint_t *breakpoint = target->breakpoints;
47         breakpoint_t **breakpoint_p = &target->breakpoints;
48         int retval;
49                 
50         while (breakpoint)
51         {
52                 if (breakpoint->address == address)
53                         return ERROR_OK;
54                 breakpoint_p = &breakpoint->next;
55                 breakpoint = breakpoint->next;
56         }
57         
58         if ((retval = target->type->add_breakpoint(target, address, length, type)) != ERROR_OK)
59         {
60                 switch (retval)
61                 {
62                         case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
63                                 INFO("can't add %s breakpoint, resource not available", breakpoint_type_strings[type]);
64                                 return retval;
65                                 break;
66                         case ERROR_TARGET_NOT_HALTED:
67                                 INFO("can't add breakpoint while target is running");
68                                 return retval;
69                                 break;
70                         default:
71                                 ERROR("unknown error");
72                                 exit(-1);
73                                 break;
74                 }
75         }
76         
77         (*breakpoint_p) = malloc(sizeof(breakpoint_t));
78         (*breakpoint_p)->address = address;
79         (*breakpoint_p)->length = length;
80         (*breakpoint_p)->type = type;
81         (*breakpoint_p)->set = 0;
82         (*breakpoint_p)->orig_instr = malloc(CEIL(length, 8));
83         (*breakpoint_p)->next = NULL;
84         
85         DEBUG("added %s breakpoint at 0x%8.8x of length 0x%8.8x", breakpoint_type_strings[type], address, length);
86         
87         return ERROR_OK;
88 }
89
90 int breakpoint_remove(target_t *target, u32 address)
91 {
92         breakpoint_t *breakpoint = target->breakpoints;
93         breakpoint_t **breakpoint_p = &target->breakpoints;
94         int retval;
95         
96         while (breakpoint)
97         {
98                 if (breakpoint->address == address)
99                         break;
100                 breakpoint_p = &breakpoint->next;
101                 breakpoint = breakpoint->next;
102         }
103         
104         if (breakpoint)
105         {
106                 if ((retval = target->type->remove_breakpoint(target, breakpoint)) != ERROR_OK)
107                 {
108                         switch (retval)
109                         {
110                                 case ERROR_TARGET_NOT_HALTED:
111                                         INFO("can't remove breakpoint while target is running");
112                                         return retval;
113                                         break;
114                                 default:
115                                         ERROR("unknown error");
116                                         exit(-1);
117                                         break;
118                         }
119                 }
120                 (*breakpoint_p) = breakpoint->next;
121                 free(breakpoint->orig_instr);
122                 free(breakpoint);
123         }
124         else
125         {
126                 ERROR("no breakpoint at address 0x%8.8x found", address);
127         }
128         
129         return ERROR_OK;
130 }
131
132 breakpoint_t* breakpoint_find(target_t *target, u32 address)
133 {
134         breakpoint_t *breakpoint = target->breakpoints;
135         
136         while (breakpoint)
137         {
138                 if (breakpoint->address == address)
139                         return breakpoint;
140                 breakpoint = breakpoint->next;
141         }
142         
143         return NULL;
144 }
145
146 int watchpoint_add(target_t *target, u32 address, u32 length, enum watchpoint_rw rw, u32 value, u32 mask)
147 {
148         watchpoint_t *watchpoint = target->watchpoints;
149         watchpoint_t **watchpoint_p = &target->watchpoints;
150         int retval;
151                 
152         while (watchpoint)
153         {
154                 if (watchpoint->address == address)
155                         return ERROR_OK;
156                 watchpoint_p = &watchpoint->next;
157                 watchpoint = watchpoint->next;
158         }
159         
160         if ((retval = target->type->add_watchpoint(target, address, length, rw)) != ERROR_OK)
161         {
162                 switch (retval)
163                 {
164                         case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
165                                 INFO("can't add %s watchpoint, resource not available", watchpoint_rw_strings[rw]);
166                                 return retval;
167                                 break;
168                         default:
169                                 ERROR("unknown error");
170                                 exit(-1);
171                                 break;
172                 }
173         }
174         
175         (*watchpoint_p) = malloc(sizeof(watchpoint_t));
176         (*watchpoint_p)->address = address;
177         (*watchpoint_p)->length = length;
178         (*watchpoint_p)->value = value;
179         (*watchpoint_p)->mask = mask;
180         (*watchpoint_p)->rw = rw;
181         (*watchpoint_p)->set = 0;
182         (*watchpoint_p)->next = NULL;
183         
184         DEBUG("added %s watchpoint at 0x%8.8x of length 0x%8.8x", watchpoint_rw_strings[rw], address, length);
185         
186         return ERROR_OK;
187 }
188
189 int watchpoint_remove(target_t *target, u32 address)
190 {
191         watchpoint_t *watchpoint = target->watchpoints;
192         watchpoint_t **watchpoint_p = &target->watchpoints;
193         int retval;
194         
195         while (watchpoint)
196         {
197                 if (watchpoint->address == address)
198                         break;
199                 watchpoint_p = &watchpoint->next;
200                 watchpoint = watchpoint->next;
201         }
202         
203         if (watchpoint)
204         {
205                 if ((retval = target->type->remove_watchpoint(target, watchpoint)) != ERROR_OK)
206                 {
207                         ERROR("BUG: can't remove watchpoint");
208                         exit(-1);
209                 }
210                 (*watchpoint_p) = watchpoint->next;
211                 free(watchpoint);
212         }
213         else
214         {
215                 ERROR("no watchpoint at address 0x%8.8x found", address);
216         }
217         
218         return ERROR_OK;
219 }