2002-05-02 Pierre Muller <muller@ics.u-strasbg.fr>
[deliverable/binutils-gdb.git] / gdb / gdbserver / regcache.c
CommitLineData
0a30fbc4
DJ
1/* Register support routines for the remote server for GDB.
2 Copyright 2001, 2002
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22#include "server.h"
23#include "regdef.h"
24
25#include <stdlib.h>
26#include <string.h>
27
c04a1aa8
DJ
28struct inferior_regcache_data
29{
30 char *registers;
31};
32
0a30fbc4
DJ
33static int register_bytes;
34
35static struct reg *reg_defs;
36static int num_registers;
37
38const char **gdbserver_expedite_regs;
39
c04a1aa8
DJ
40static struct inferior_regcache_data *
41get_regcache (struct inferior_info *inf)
42{
43 struct inferior_regcache_data *regcache;
44
45 regcache = (struct inferior_regcache_data *) inferior_regcache_data (inf);
46
47 if (regcache == NULL)
48 fatal ("no register cache");
49
50 return regcache;
51}
52
0a30fbc4
DJ
53int
54registers_length (void)
55{
56 return 2 * register_bytes;
57}
58
c04a1aa8
DJ
59void
60create_register_cache (struct inferior_info *inferior)
61{
62 struct inferior_regcache_data *regcache;
63
64 regcache = malloc (sizeof (*regcache));
65
66 regcache->registers = malloc (register_bytes);
67 if (regcache->registers == NULL)
68 fatal ("Could not allocate register cache.");
69
70 set_inferior_regcache_data (inferior, regcache);
71}
72
73void
74free_register_cache (struct inferior_info *inferior)
75{
76 free (get_regcache (current_inferior)->registers);
77 free (get_regcache (current_inferior));
78 set_inferior_regcache_data (inferior, NULL);
79}
80
0a30fbc4
DJ
81void
82set_register_cache (struct reg *regs, int n)
83{
84 int offset, i;
85
86 reg_defs = regs;
87 num_registers = n;
88
89 offset = 0;
90 for (i = 0; i < n; i++)
91 {
92 regs[i].offset = offset;
93 offset += regs[i].size;
94 }
95
96 register_bytes = offset / 8;
0a30fbc4
DJ
97}
98
99void
100registers_to_string (char *buf)
101{
c04a1aa8
DJ
102 char *registers = get_regcache (current_inferior)->registers;
103
0a30fbc4
DJ
104 convert_int_to_ascii (registers, buf, register_bytes);
105}
106
107void
108registers_from_string (char *buf)
109{
110 int len = strlen (buf);
c04a1aa8 111 char *registers = get_regcache (current_inferior)->registers;
0a30fbc4
DJ
112
113 if (len != register_bytes * 2)
114 {
115 warning ("Wrong sized register packet (expected %d bytes, got %d)", 2*register_bytes, len);
116 if (len > register_bytes * 2)
117 len = register_bytes * 2;
118 }
119 convert_ascii_to_int (buf, registers, len / 2);
120}
121
122struct reg *
123find_register_by_name (const char *name)
124{
125 int i;
126
127 for (i = 0; i < num_registers; i++)
128 if (!strcmp (name, reg_defs[i].name))
129 return &reg_defs[i];
130 fatal ("Unknown register %s requested", name);
131 return 0;
132}
133
134int
135find_regno (const char *name)
136{
137 int i;
138
139 for (i = 0; i < num_registers; i++)
140 if (!strcmp (name, reg_defs[i].name))
141 return i;
142 fatal ("Unknown register %s requested", name);
143 return -1;
144}
145
146struct reg *
147find_register_by_number (int n)
148{
149 return &reg_defs[n];
150}
151
152int
153register_size (int n)
154{
155 return reg_defs[n].size / 8;
156}
157
158char *
159register_data (int n)
160{
c04a1aa8
DJ
161 char *registers = get_regcache (current_inferior)->registers;
162
0a30fbc4
DJ
163 return registers + (reg_defs[n].offset / 8);
164}
165
58caa3dc 166void
0729219d 167supply_register (int n, const void *buf)
58caa3dc
DJ
168{
169 memcpy (register_data (n), buf, register_size (n));
170}
171
172void
0729219d 173supply_register_by_name (const char *name, const void *buf)
58caa3dc
DJ
174{
175 supply_register (find_regno (name), buf);
176}
177
178void
0729219d 179collect_register (int n, void *buf)
58caa3dc
DJ
180{
181 memcpy (buf, register_data (n), register_size (n));
182}
183
184void
0729219d 185collect_register_by_name (const char *name, void *buf)
58caa3dc
DJ
186{
187 collect_register (find_regno (name), buf);
188}
This page took 0.046954 seconds and 4 git commands to generate.