summaryrefslogtreecommitdiff
path: root/gdb/regbuf.c
blob: 0ebfdd14b5b8dd9271e61f5ae78810f5a2c6f464 (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
/* Register buffer sufficient to hold all raw registers

   Copyright 2002 Free Software Foundation, Inc.

   This file is part of GDB.

   Contributed by Red Hat, Inc.

   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 of the License, 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 "defs.h"
#include "regbuf.h"
#include "gdb_assert.h"

extern void _initialize_regbuf (void);

/* Per-architecture object describing the layout of a register buffer.
   Computed once when the architecture is created */

struct gdbarch_data *regbuf_data_handle;

struct regbuf_descr
{
  struct gdbarch *gdbarch;
  /* Size of the register buffer, over-allocate making room for both
     real and pseudo-registers.  */
  /* FIXME: cagney/2002-05-11: This over-allocation shouldn't be
     necessary, only some targets store values in the pseudo-register
     section and we want to be sure that GDB won't trash memory.  */
  long sizeof_registers;
  /* Total number of registers in the buffer.  */
  int nr_registers;
  /* Offset into the register buffer for each register.  */
  long *register_offset;
  /* Size, in ``bytes'', of a register.  */
  long *sizeof_register;
};

static struct regbuf_descr *
regbuf_descr (struct gdbarch *gdbarch)
{
  int i;
  /* FIXME: cagney/2002-05-11: gdbarch_data() should take that
     ``gdbarch'' as a parameter.  */
  struct regbuf_descr *descr = gdbarch_data (gdbarch,
					     regbuf_data_handle);
  if (descr != NULL)
    return descr;
  
  descr = XMALLOC (struct regbuf_descr);
  descr->gdbarch = gdbarch;
  /* FIXME: cagney/2002-05-11: Shouldn't be including pseudo-registers
     in the register buffer.  Unfortunatly some architectures are
     storing pseudo register values in the raw register buffer.  */
  descr->nr_registers = NUM_REGS + NUM_PSEUDO_REGS;
  /* FIXME: cagney/2002-05-11: Instead of using REGISTER_BYTE() this
     should compute, at runtime, an offset table describing where each
     register is in the register table.  This currently isn't possible
     because some targets overlap registers, ulgh!  */
  descr->register_offset = XCALLOC (descr->nr_registers, long);
  descr->sizeof_register = XCALLOC (descr->nr_registers, long);
  for (i = 0; i < descr->nr_registers; i++)
    {
      descr->register_offset[i] = REGISTER_BYTE (i);
      descr->sizeof_register[i] = REGISTER_RAW_SIZE (i);
    }
  /* Come up with the real size of the registers buffer.  */
  descr->sizeof_registers = REGISTER_BYTES; /* OK use.  */
  for (i = 0; i < descr->nr_registers; i++)
    {
      long regend;
      /* Keep extending the buffer so that there is always enough
         space for all registers.  The comparison is necessary since
         legacy code is free to put registers in random places in the
         buffer separated by holes.  Once REGISTER_BYTE() is killed
         this can be greatly simplified.  */
      /* FIXME: cagney/2001-12-04: This code shouldn't need to use
         REGISTER_BYTE().  Unfortunatly, legacy code likes to lay the
         buffer out so that certain registers just happen to overlap.
         Ulgh!  New targets use gdbarch's register read/write and
         entirely avoid this uglyness.  */
      regend = descr->register_offset[i] + descr->sizeof_register[i];
      if (descr->sizeof_registers < regend)
	descr->sizeof_registers = regend;
    }
  set_gdbarch_data (gdbarch, regbuf_data_handle, descr);
  return descr;
}

static void
xfree_regbuf (struct gdbarch *gdbarch, void *ptr)
{
  struct regbuf_descr *descr = ptr;
  if (descr == NULL)
    return;
  xfree (descr->register_offset);
  xfree (descr->sizeof_register);
  xfree (descr);
}

/* For moment, ``struct regbuf'' is just a character buffer.  */

struct regbuf
{
  struct regbuf_descr *descr;
  char *registers;
  char *valid_p;
};

struct regbuf *
regbuf_xmalloc (struct gdbarch *gdbarch)
{
  struct regbuf_descr *descr = regbuf_descr (gdbarch);
  struct regbuf *regbuf = XMALLOC (struct regbuf);
  regbuf->descr = descr;
  regbuf->registers = xmalloc (descr->sizeof_registers);
  regbuf->valid_p = xmalloc (descr->nr_registers);
  return regbuf;
}

void
regbuf_xfree (struct regbuf *regbuf)
{
  gdb_assert (regbuf != NULL);
  xfree (regbuf->registers);
  xfree (regbuf->valid_p);
  xfree (regbuf);
}

void
do_regbuf_xfree (void *buf)
{
  regbuf_xfree (buf);
}

struct regbuf *
regbuf_xmalloc_with_cleanup (struct gdbarch *gdbarch)
{
  struct regbuf *regbuf = regbuf_xmalloc (gdbarch);
  make_cleanup (do_regbuf_xfree, regbuf);
  return regbuf;
}

struct regbuf *
regbuf_dup (struct regbuf *regbuf)
{
  struct regbuf *newbuf = regbuf_xmalloc (regbuf->descr->gdbarch);
  memcpy (newbuf->registers, regbuf->registers,
	  regbuf->descr->sizeof_registers);
  memcpy (newbuf->valid_p, regbuf->valid_p, regbuf->descr->nr_registers);
  return newbuf;
}

int
regbuf_valid_p (struct regbuf *regbuf, int regnum)
{
  gdb_assert (regnum != NULL);
  gdb_assert (regnum >= 0 && regnum < NUM_REGS + NUM_PSEUDO_REGS);
  return regbuf->valid_p[regnum];
}

void
regbuf_read (struct regbuf *regbuf, int regnum, void *buf)
{
  gdb_assert (regbuf != NULL);
  gdb_assert (regnum >= 0 && regnum < regbuf->descr->nr_registers);
  memcpy (buf, regbuf->registers + regbuf->descr->register_offset[regnum],
	  regbuf->descr->sizeof_register[regnum]);
}

void
regbuf_write (struct regbuf *regbuf, int regnum, const void *buf)
{
  gdb_assert (regbuf != NULL);
  gdb_assert (regnum >= 0 && regnum < regbuf->descr->nr_registers);
  memcpy (regbuf->registers + regbuf->descr->register_offset[regnum], buf,
	  regbuf->descr->sizeof_register[regnum]);
  regbuf->valid_p[regnum] = 1;
}

CORE_ADDR
regbuf_read_as_address (struct regbuf *regbuf, int regnum)
{
  char *buf;
  gdb_assert (regbuf != NULL);
  gdb_assert (regnum >= 0 && regnum < regbuf->descr->nr_registers);
  buf = alloca (regbuf->descr->sizeof_register[regnum]);
  regbuf_read (regbuf, regnum, buf);
  return extract_address (buf, regbuf->descr->sizeof_register[regnum]);
}

char *
grub_around_regbuf_for_registers (struct regbuf *regbuf)
{
  return regbuf->registers;
}

char *
grub_around_regbuf_for_register_valid (struct regbuf *regbuf)
{
  return regbuf->valid_p;
}

void
_initialize_regbuf (void)
{
  regbuf_data_handle = register_gdbarch_data (NULL, xfree_regbuf);
}