aboutsummaryrefslogtreecommitdiff
path: root/sim/common/sim-break.c
blob: 07d1f954e83996a1233fc6611441e2ea3714a867 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* Simulator breakpoint support.
   Copyright (C) 1997 Free Software Foundation, Inc.
   Contributed by Cygnus Support.

This file is part of GDB, the GNU debugger.

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, 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.  */

#include <stdio.h>

#include "sim-main.h"
#include "sim-assert.h"
#include "sim-break.h"

#ifndef SIM_BREAKPOINT
#define SIM_BREAKPOINT {0x00}
#define SIM_BREAKPOINT_SIZE (1)
#endif

struct
sim_breakpoint
{
  struct sim_breakpoint *next;
  SIM_ADDR addr;		/* Address of this breakpoint */
  int flags;
  unsigned char loc_contents[SIM_BREAKPOINT_SIZE]; /* Contents of addr while
						      BP is enabled */
};

#define SIM_BREAK_INSERTED 0x1	/* Breakpoint has been inserted */
#define SIM_BREAK_DISABLED 0x2	/* Breakpoint is disabled */

static unsigned char sim_breakpoint [] = SIM_BREAKPOINT;

static void insert_breakpoint PARAMS ((SIM_DESC sd,
				       struct sim_breakpoint *bp));
static void remove_breakpoint PARAMS ((SIM_DESC sd,
				       struct sim_breakpoint *bp));
static SIM_RC resume_handler PARAMS ((SIM_DESC sd));
static SIM_RC suspend_handler PARAMS ((SIM_DESC sd));


/* Do the actual work of inserting a breakpoint into the instruction
   stream. */

static void
insert_breakpoint (sd, bp)
     SIM_DESC sd;
     struct sim_breakpoint *bp;
{
  if (bp->flags & (SIM_BREAK_INSERTED | SIM_BREAK_DISABLED))
    return;

  sim_core_read_buffer (sd, NULL, exec_map, bp->loc_contents,
			bp->addr, SIM_BREAKPOINT_SIZE);
  sim_core_write_buffer (sd, NULL, exec_map, sim_breakpoint,
			 bp->addr, SIM_BREAKPOINT_SIZE);
  bp->flags |= SIM_BREAK_INSERTED;
}

/* Do the actual work of removing a breakpoint. */

static void
remove_breakpoint (sd, bp)
     SIM_DESC sd;
     struct sim_breakpoint *bp;
{
  if (!(bp->flags & SIM_BREAK_INSERTED))
    return;

  sim_core_write_buffer (sd, NULL, exec_map, bp->loc_contents,
			 bp->addr, SIM_BREAKPOINT_SIZE);
  bp->flags &= SIM_BREAK_INSERTED;
}

/* Come here when a breakpoint insn is hit.  If it's really a breakpoint, we
   halt things, and never return.  If it's a false hit, we return to let the
   caller handle things.  */

void
sim_handle_breakpoint (sd, cpu, cia)
     SIM_DESC sd;
     sim_cpu *cpu;
     sim_cia cia;
{
  struct sim_breakpoint *bp;

  for (bp = STATE_BREAKPOINTS (sd); bp; bp = bp->next)
    if (bp->addr == CIA_ADDR (cia))
      break;

  if (!bp || !(bp->flags & SIM_BREAK_INSERTED))
    return;

  sim_engine_halt (sd, STATE_CPU (sd, 0), NULL, cia, sim_stopped, SIM_SIGTRAP);
}

/* Handler functions for simulator resume and suspend events. */

static SIM_RC
resume_handler (sd)
     SIM_DESC sd;
{
  struct sim_breakpoint *bp;

  for (bp = STATE_BREAKPOINTS (sd); bp; bp = bp->next)
    insert_breakpoint (sd, bp);

  return SIM_RC_OK;
}

static SIM_RC
suspend_handler (sd)
     SIM_DESC sd;
{
  struct sim_breakpoint *bp;

  for (bp = STATE_BREAKPOINTS (sd); bp; bp = bp->next)
    remove_breakpoint (sd, bp);

  return SIM_RC_OK;
}

/* Called from simulator module initialization.  */

SIM_RC
sim_break_install (sd)
     SIM_DESC sd;
{
  sim_module_add_resume_fn (sd, resume_handler);
  sim_module_add_suspend_fn (sd, suspend_handler);

  return SIM_RC_OK;
}

/* Install a breakpoint.  This is a user-function.  The breakpoint isn't
   actually installed here.  We just record it.  Resume_handler does the
   actual work.
*/

SIM_RC
sim_set_breakpoint (sd, addr)
     SIM_DESC sd;
     SIM_ADDR addr;
{
  struct sim_breakpoint *bp;

  for (bp = STATE_BREAKPOINTS (sd); bp; bp = bp->next)
    if (bp->addr == addr)
      return SIM_RC_DUPLICATE_BREAKPOINT; /* Already there */
    else
      break;

  bp = ZALLOC (struct sim_breakpoint);

  bp->addr = addr;
  bp->next = STATE_BREAKPOINTS (sd);
  bp->flags = 0;
  STATE_BREAKPOINTS (sd) = bp;

  return SIM_RC_OK;
}

/* Delete a breakpoint.  All knowlege of the breakpoint is removed from the
   simulator.
*/

SIM_RC
sim_clear_breakpoint (sd, addr)
     SIM_DESC sd;
     SIM_ADDR addr;
{
  struct sim_breakpoint *bp, *bpprev;

  for (bp = STATE_BREAKPOINTS (sd), bpprev = NULL;
       bp;
       bpprev = bp, bp = bp->next)
    if (bp->addr == addr)
      break;

  if (!bp)
    return SIM_RC_UNKNOWN_BREAKPOINT;

  remove_breakpoint (sd, bp);

  if (bpprev)
    bpprev->next = bp->next;
  else
    STATE_BREAKPOINTS (sd) = NULL;

  zfree (bp);

  return SIM_RC_OK;
}

SIM_RC
sim_clear_all_breakpoints (sd)
     SIM_DESC sd;
{
  while (STATE_BREAKPOINTS (sd))
    sim_clear_breakpoint (sd, STATE_BREAKPOINTS (sd)->addr);

  return SIM_RC_OK;
}

SIM_RC
sim_enable_breakpoint (sd, addr)
     SIM_DESC sd;
     SIM_ADDR addr;
{
  struct sim_breakpoint *bp;

  for (bp = STATE_BREAKPOINTS (sd); bp; bp = bp->next)
    if (bp->addr == addr)
      break;

  if (!bp)
    return SIM_RC_UNKNOWN_BREAKPOINT;

  bp->flags &= ~SIM_BREAK_DISABLED;

  return SIM_RC_OK;
}

SIM_RC
sim_disable_breakpoint (sd, addr)
     SIM_DESC sd;
     SIM_ADDR addr;
{
  struct sim_breakpoint *bp;

  for (bp = STATE_BREAKPOINTS (sd); bp; bp = bp->next)
    if (bp->addr == addr)
      break;

  if (!bp)
    return SIM_RC_UNKNOWN_BREAKPOINT;

  bp->flags |= SIM_BREAK_DISABLED;

  return SIM_RC_OK;
}

SIM_RC
sim_enable_all_breakpoints (sd)
     SIM_DESC sd;
{
  struct sim_breakpoint *bp;

  for (bp = STATE_BREAKPOINTS (sd); bp; bp = bp->next)
    bp->flags &= ~SIM_BREAK_DISABLED;

  return SIM_RC_OK;
}

SIM_RC
sim_disable_all_breakpoints (sd)
     SIM_DESC sd;
{
  struct sim_breakpoint *bp;

  for (bp = STATE_BREAKPOINTS (sd); bp; bp = bp->next)
    bp->flags |= SIM_BREAK_DISABLED;

  return SIM_RC_OK;
}