Switch the license of all .c files to GPLv3.
[deliverable/binutils-gdb.git] / gdb / gdbserver / regcache.c
1 /* Register support routines for the remote server for GDB.
2 Copyright (C) 2001, 2002, 2004, 2005, 2007 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19 #include "server.h"
20 #include "regdef.h"
21
22 #include <stdlib.h>
23 #include <string.h>
24
25 /* The private data for the register cache. Note that we have one
26 per inferior; this is primarily for simplicity, as the performance
27 benefit is minimal. */
28
29 struct inferior_regcache_data
30 {
31 int registers_valid;
32 unsigned char *registers;
33 };
34
35 static int register_bytes;
36
37 static struct reg *reg_defs;
38 static int num_registers;
39
40 const char **gdbserver_expedite_regs;
41
42 static struct inferior_regcache_data *
43 get_regcache (struct thread_info *inf, int fetch)
44 {
45 struct inferior_regcache_data *regcache;
46
47 regcache = (struct inferior_regcache_data *) inferior_regcache_data (inf);
48
49 if (regcache == NULL)
50 fatal ("no register cache");
51
52 /* FIXME - fetch registers for INF */
53 if (fetch && regcache->registers_valid == 0)
54 {
55 fetch_inferior_registers (0);
56 regcache->registers_valid = 1;
57 }
58
59 return regcache;
60 }
61
62 void
63 regcache_invalidate_one (struct inferior_list_entry *entry)
64 {
65 struct thread_info *thread = (struct thread_info *) entry;
66 struct inferior_regcache_data *regcache;
67
68 regcache = (struct inferior_regcache_data *) inferior_regcache_data (thread);
69
70 if (regcache->registers_valid)
71 {
72 struct thread_info *saved_inferior = current_inferior;
73
74 current_inferior = thread;
75 store_inferior_registers (-1);
76 current_inferior = saved_inferior;
77 }
78
79 regcache->registers_valid = 0;
80 }
81
82 void
83 regcache_invalidate ()
84 {
85 for_each_inferior (&all_threads, regcache_invalidate_one);
86 }
87
88 int
89 registers_length (void)
90 {
91 return 2 * register_bytes;
92 }
93
94 void *
95 new_register_cache (void)
96 {
97 struct inferior_regcache_data *regcache;
98
99 regcache = malloc (sizeof (*regcache));
100
101 /* Make sure to zero-initialize the register cache when it is created,
102 in case there are registers the target never fetches. This way they'll
103 read as zero instead of garbage. */
104 regcache->registers = calloc (1, register_bytes);
105 if (regcache->registers == NULL)
106 fatal ("Could not allocate register cache.");
107
108 regcache->registers_valid = 0;
109
110 return regcache;
111 }
112
113 void
114 free_register_cache (void *regcache_p)
115 {
116 struct inferior_regcache_data *regcache
117 = (struct inferior_regcache_data *) regcache_p;
118
119 free (regcache->registers);
120 free (regcache);
121 }
122
123 void
124 set_register_cache (struct reg *regs, int n)
125 {
126 int offset, i;
127
128 reg_defs = regs;
129 num_registers = n;
130
131 offset = 0;
132 for (i = 0; i < n; i++)
133 {
134 regs[i].offset = offset;
135 offset += regs[i].size;
136 }
137
138 register_bytes = offset / 8;
139 }
140
141 void
142 registers_to_string (char *buf)
143 {
144 unsigned char *registers = get_regcache (current_inferior, 1)->registers;
145
146 convert_int_to_ascii (registers, buf, register_bytes);
147 }
148
149 void
150 registers_from_string (char *buf)
151 {
152 int len = strlen (buf);
153 unsigned char *registers = get_regcache (current_inferior, 1)->registers;
154
155 if (len != register_bytes * 2)
156 {
157 warning ("Wrong sized register packet (expected %d bytes, got %d)", 2*register_bytes, len);
158 if (len > register_bytes * 2)
159 len = register_bytes * 2;
160 }
161 convert_ascii_to_int (buf, registers, len / 2);
162 }
163
164 struct reg *
165 find_register_by_name (const char *name)
166 {
167 int i;
168
169 for (i = 0; i < num_registers; i++)
170 if (!strcmp (name, reg_defs[i].name))
171 return &reg_defs[i];
172 fatal ("Unknown register %s requested", name);
173 return 0;
174 }
175
176 int
177 find_regno (const char *name)
178 {
179 int i;
180
181 for (i = 0; i < num_registers; i++)
182 if (!strcmp (name, reg_defs[i].name))
183 return i;
184 fatal ("Unknown register %s requested", name);
185 return -1;
186 }
187
188 struct reg *
189 find_register_by_number (int n)
190 {
191 return &reg_defs[n];
192 }
193
194 int
195 register_size (int n)
196 {
197 return reg_defs[n].size / 8;
198 }
199
200 static unsigned char *
201 register_data (int n, int fetch)
202 {
203 unsigned char *registers
204 = get_regcache (current_inferior, fetch)->registers;
205
206 return registers + (reg_defs[n].offset / 8);
207 }
208
209 void
210 supply_register (int n, const void *buf)
211 {
212 memcpy (register_data (n, 0), buf, register_size (n));
213 }
214
215 void
216 supply_register_by_name (const char *name, const void *buf)
217 {
218 supply_register (find_regno (name), buf);
219 }
220
221 void
222 collect_register (int n, void *buf)
223 {
224 memcpy (buf, register_data (n, 1), register_size (n));
225 }
226
227 void
228 collect_register_as_string (int n, char *buf)
229 {
230 convert_int_to_ascii (register_data (n, 1), buf, register_size (n));
231 }
232
233 void
234 collect_register_by_name (const char *name, void *buf)
235 {
236 collect_register (find_regno (name), buf);
237 }
This page took 0.03364 seconds and 4 git commands to generate.