Commit | Line | Data |
---|---|---|
0a30fbc4 | 1 | /* Register support routines for the remote server for GDB. |
6f0f660e | 2 | Copyright (C) 2001, 2002, 2004, 2005 |
0a30fbc4 DJ |
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 | |
6f0f660e EZ |
19 | Foundation, Inc., 51 Franklin Street, Fifth Floor, |
20 | Boston, MA 02110-1301, USA. */ | |
0a30fbc4 DJ |
21 | |
22 | #include "server.h" | |
23 | #include "regdef.h" | |
24 | ||
25 | #include <stdlib.h> | |
26 | #include <string.h> | |
27 | ||
a7f48742 DJ |
28 | /* The private data for the register cache. Note that we have one |
29 | per inferior; this is primarily for simplicity, as the performance | |
30 | benefit is minimal. */ | |
31 | ||
c04a1aa8 DJ |
32 | struct inferior_regcache_data |
33 | { | |
0d62e5e8 | 34 | int registers_valid; |
f450004a | 35 | unsigned char *registers; |
c04a1aa8 DJ |
36 | }; |
37 | ||
0a30fbc4 DJ |
38 | static int register_bytes; |
39 | ||
40 | static struct reg *reg_defs; | |
41 | static int num_registers; | |
42 | ||
43 | const char **gdbserver_expedite_regs; | |
44 | ||
c04a1aa8 | 45 | static struct inferior_regcache_data * |
0d62e5e8 | 46 | get_regcache (struct thread_info *inf, int fetch) |
c04a1aa8 DJ |
47 | { |
48 | struct inferior_regcache_data *regcache; | |
49 | ||
50 | regcache = (struct inferior_regcache_data *) inferior_regcache_data (inf); | |
51 | ||
52 | if (regcache == NULL) | |
53 | fatal ("no register cache"); | |
54 | ||
0d62e5e8 DJ |
55 | /* FIXME - fetch registers for INF */ |
56 | if (fetch && regcache->registers_valid == 0) | |
57 | { | |
58 | fetch_inferior_registers (0); | |
59 | regcache->registers_valid = 1; | |
60 | } | |
61 | ||
c04a1aa8 DJ |
62 | return regcache; |
63 | } | |
64 | ||
0d62e5e8 DJ |
65 | void |
66 | regcache_invalidate_one (struct inferior_list_entry *entry) | |
67 | { | |
68 | struct thread_info *thread = (struct thread_info *) entry; | |
69 | struct inferior_regcache_data *regcache; | |
70 | ||
71 | regcache = (struct inferior_regcache_data *) inferior_regcache_data (thread); | |
72 | ||
73 | if (regcache->registers_valid) | |
74 | { | |
75 | struct thread_info *saved_inferior = current_inferior; | |
76 | ||
77 | current_inferior = thread; | |
78 | store_inferior_registers (-1); | |
79 | current_inferior = saved_inferior; | |
80 | } | |
81 | ||
82 | regcache->registers_valid = 0; | |
83 | } | |
84 | ||
85 | void | |
86 | regcache_invalidate () | |
87 | { | |
88 | for_each_inferior (&all_threads, regcache_invalidate_one); | |
89 | } | |
90 | ||
0a30fbc4 DJ |
91 | int |
92 | registers_length (void) | |
93 | { | |
94 | return 2 * register_bytes; | |
95 | } | |
96 | ||
0d62e5e8 DJ |
97 | void * |
98 | new_register_cache (void) | |
c04a1aa8 DJ |
99 | { |
100 | struct inferior_regcache_data *regcache; | |
101 | ||
102 | regcache = malloc (sizeof (*regcache)); | |
103 | ||
2a68b70e DJ |
104 | /* Make sure to zero-initialize the register cache when it is created, |
105 | in case there are registers the target never fetches. This way they'll | |
106 | read as zero instead of garbage. */ | |
107 | regcache->registers = calloc (1, register_bytes); | |
c04a1aa8 DJ |
108 | if (regcache->registers == NULL) |
109 | fatal ("Could not allocate register cache."); | |
110 | ||
0d62e5e8 DJ |
111 | regcache->registers_valid = 0; |
112 | ||
113 | return regcache; | |
c04a1aa8 DJ |
114 | } |
115 | ||
116 | void | |
0d62e5e8 | 117 | free_register_cache (void *regcache_p) |
c04a1aa8 | 118 | { |
0d62e5e8 DJ |
119 | struct inferior_regcache_data *regcache |
120 | = (struct inferior_regcache_data *) regcache_p; | |
121 | ||
122 | free (regcache->registers); | |
123 | free (regcache); | |
c04a1aa8 DJ |
124 | } |
125 | ||
0a30fbc4 DJ |
126 | void |
127 | set_register_cache (struct reg *regs, int n) | |
128 | { | |
129 | int offset, i; | |
130 | ||
131 | reg_defs = regs; | |
132 | num_registers = n; | |
133 | ||
134 | offset = 0; | |
135 | for (i = 0; i < n; i++) | |
136 | { | |
137 | regs[i].offset = offset; | |
138 | offset += regs[i].size; | |
139 | } | |
140 | ||
141 | register_bytes = offset / 8; | |
0a30fbc4 DJ |
142 | } |
143 | ||
144 | void | |
145 | registers_to_string (char *buf) | |
146 | { | |
f450004a | 147 | unsigned char *registers = get_regcache (current_inferior, 1)->registers; |
c04a1aa8 | 148 | |
0a30fbc4 DJ |
149 | convert_int_to_ascii (registers, buf, register_bytes); |
150 | } | |
151 | ||
152 | void | |
153 | registers_from_string (char *buf) | |
154 | { | |
155 | int len = strlen (buf); | |
f450004a | 156 | unsigned char *registers = get_regcache (current_inferior, 1)->registers; |
0a30fbc4 DJ |
157 | |
158 | if (len != register_bytes * 2) | |
159 | { | |
160 | warning ("Wrong sized register packet (expected %d bytes, got %d)", 2*register_bytes, len); | |
161 | if (len > register_bytes * 2) | |
162 | len = register_bytes * 2; | |
163 | } | |
164 | convert_ascii_to_int (buf, registers, len / 2); | |
165 | } | |
166 | ||
167 | struct reg * | |
168 | find_register_by_name (const char *name) | |
169 | { | |
170 | int i; | |
171 | ||
172 | for (i = 0; i < num_registers; i++) | |
173 | if (!strcmp (name, reg_defs[i].name)) | |
174 | return ®_defs[i]; | |
175 | fatal ("Unknown register %s requested", name); | |
176 | return 0; | |
177 | } | |
178 | ||
179 | int | |
180 | find_regno (const char *name) | |
181 | { | |
182 | int i; | |
183 | ||
184 | for (i = 0; i < num_registers; i++) | |
185 | if (!strcmp (name, reg_defs[i].name)) | |
186 | return i; | |
187 | fatal ("Unknown register %s requested", name); | |
188 | return -1; | |
189 | } | |
190 | ||
191 | struct reg * | |
192 | find_register_by_number (int n) | |
193 | { | |
194 | return ®_defs[n]; | |
195 | } | |
196 | ||
197 | int | |
198 | register_size (int n) | |
199 | { | |
200 | return reg_defs[n].size / 8; | |
201 | } | |
202 | ||
f450004a | 203 | static unsigned char * |
0d62e5e8 | 204 | register_data (int n, int fetch) |
0a30fbc4 | 205 | { |
f450004a DJ |
206 | unsigned char *registers |
207 | = get_regcache (current_inferior, fetch)->registers; | |
c04a1aa8 | 208 | |
0a30fbc4 DJ |
209 | return registers + (reg_defs[n].offset / 8); |
210 | } | |
211 | ||
58caa3dc | 212 | void |
0729219d | 213 | supply_register (int n, const void *buf) |
58caa3dc | 214 | { |
0d62e5e8 | 215 | memcpy (register_data (n, 0), buf, register_size (n)); |
58caa3dc DJ |
216 | } |
217 | ||
218 | void | |
0729219d | 219 | supply_register_by_name (const char *name, const void *buf) |
58caa3dc DJ |
220 | { |
221 | supply_register (find_regno (name), buf); | |
222 | } | |
223 | ||
224 | void | |
0729219d | 225 | collect_register (int n, void *buf) |
58caa3dc | 226 | { |
0d62e5e8 DJ |
227 | memcpy (buf, register_data (n, 1), register_size (n)); |
228 | } | |
229 | ||
230 | void | |
231 | collect_register_as_string (int n, char *buf) | |
232 | { | |
233 | convert_int_to_ascii (register_data (n, 1), buf, register_size (n)); | |
58caa3dc DJ |
234 | } |
235 | ||
236 | void | |
0729219d | 237 | collect_register_by_name (const char *name, void *buf) |
58caa3dc DJ |
238 | { |
239 | collect_register (find_regno (name), buf); | |
240 | } |