Commit | Line | Data |
---|---|---|
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 |
28 | struct inferior_regcache_data |
29 | { | |
0d62e5e8 | 30 | int registers_valid; |
c04a1aa8 DJ |
31 | char *registers; |
32 | }; | |
33 | ||
0a30fbc4 DJ |
34 | static int register_bytes; |
35 | ||
36 | static struct reg *reg_defs; | |
37 | static int num_registers; | |
38 | ||
39 | const char **gdbserver_expedite_regs; | |
40 | ||
c04a1aa8 | 41 | static struct inferior_regcache_data * |
0d62e5e8 | 42 | get_regcache (struct thread_info *inf, int fetch) |
c04a1aa8 DJ |
43 | { |
44 | struct inferior_regcache_data *regcache; | |
45 | ||
46 | regcache = (struct inferior_regcache_data *) inferior_regcache_data (inf); | |
47 | ||
48 | if (regcache == NULL) | |
49 | fatal ("no register cache"); | |
50 | ||
0d62e5e8 DJ |
51 | /* FIXME - fetch registers for INF */ |
52 | if (fetch && regcache->registers_valid == 0) | |
53 | { | |
54 | fetch_inferior_registers (0); | |
55 | regcache->registers_valid = 1; | |
56 | } | |
57 | ||
c04a1aa8 DJ |
58 | return regcache; |
59 | } | |
60 | ||
0d62e5e8 DJ |
61 | void |
62 | regcache_invalidate_one (struct inferior_list_entry *entry) | |
63 | { | |
64 | struct thread_info *thread = (struct thread_info *) entry; | |
65 | struct inferior_regcache_data *regcache; | |
66 | ||
67 | regcache = (struct inferior_regcache_data *) inferior_regcache_data (thread); | |
68 | ||
69 | if (regcache->registers_valid) | |
70 | { | |
71 | struct thread_info *saved_inferior = current_inferior; | |
72 | ||
73 | current_inferior = thread; | |
74 | store_inferior_registers (-1); | |
75 | current_inferior = saved_inferior; | |
76 | } | |
77 | ||
78 | regcache->registers_valid = 0; | |
79 | } | |
80 | ||
81 | void | |
82 | regcache_invalidate () | |
83 | { | |
84 | for_each_inferior (&all_threads, regcache_invalidate_one); | |
85 | } | |
86 | ||
0a30fbc4 DJ |
87 | int |
88 | registers_length (void) | |
89 | { | |
90 | return 2 * register_bytes; | |
91 | } | |
92 | ||
0d62e5e8 DJ |
93 | void * |
94 | new_register_cache (void) | |
c04a1aa8 DJ |
95 | { |
96 | struct inferior_regcache_data *regcache; | |
97 | ||
98 | regcache = malloc (sizeof (*regcache)); | |
99 | ||
100 | regcache->registers = malloc (register_bytes); | |
101 | if (regcache->registers == NULL) | |
102 | fatal ("Could not allocate register cache."); | |
103 | ||
0d62e5e8 DJ |
104 | regcache->registers_valid = 0; |
105 | ||
106 | return regcache; | |
c04a1aa8 DJ |
107 | } |
108 | ||
109 | void | |
0d62e5e8 | 110 | free_register_cache (void *regcache_p) |
c04a1aa8 | 111 | { |
0d62e5e8 DJ |
112 | struct inferior_regcache_data *regcache |
113 | = (struct inferior_regcache_data *) regcache_p; | |
114 | ||
115 | free (regcache->registers); | |
116 | free (regcache); | |
c04a1aa8 DJ |
117 | } |
118 | ||
0a30fbc4 DJ |
119 | void |
120 | set_register_cache (struct reg *regs, int n) | |
121 | { | |
122 | int offset, i; | |
123 | ||
124 | reg_defs = regs; | |
125 | num_registers = n; | |
126 | ||
127 | offset = 0; | |
128 | for (i = 0; i < n; i++) | |
129 | { | |
130 | regs[i].offset = offset; | |
131 | offset += regs[i].size; | |
132 | } | |
133 | ||
134 | register_bytes = offset / 8; | |
0a30fbc4 DJ |
135 | } |
136 | ||
137 | void | |
138 | registers_to_string (char *buf) | |
139 | { | |
0d62e5e8 | 140 | char *registers = get_regcache (current_inferior, 1)->registers; |
c04a1aa8 | 141 | |
0a30fbc4 DJ |
142 | convert_int_to_ascii (registers, buf, register_bytes); |
143 | } | |
144 | ||
145 | void | |
146 | registers_from_string (char *buf) | |
147 | { | |
148 | int len = strlen (buf); | |
0d62e5e8 | 149 | char *registers = get_regcache (current_inferior, 1)->registers; |
0a30fbc4 DJ |
150 | |
151 | if (len != register_bytes * 2) | |
152 | { | |
153 | warning ("Wrong sized register packet (expected %d bytes, got %d)", 2*register_bytes, len); | |
154 | if (len > register_bytes * 2) | |
155 | len = register_bytes * 2; | |
156 | } | |
157 | convert_ascii_to_int (buf, registers, len / 2); | |
158 | } | |
159 | ||
160 | struct reg * | |
161 | find_register_by_name (const char *name) | |
162 | { | |
163 | int i; | |
164 | ||
165 | for (i = 0; i < num_registers; i++) | |
166 | if (!strcmp (name, reg_defs[i].name)) | |
167 | return ®_defs[i]; | |
168 | fatal ("Unknown register %s requested", name); | |
169 | return 0; | |
170 | } | |
171 | ||
172 | int | |
173 | find_regno (const char *name) | |
174 | { | |
175 | int i; | |
176 | ||
177 | for (i = 0; i < num_registers; i++) | |
178 | if (!strcmp (name, reg_defs[i].name)) | |
179 | return i; | |
180 | fatal ("Unknown register %s requested", name); | |
181 | return -1; | |
182 | } | |
183 | ||
184 | struct reg * | |
185 | find_register_by_number (int n) | |
186 | { | |
187 | return ®_defs[n]; | |
188 | } | |
189 | ||
190 | int | |
191 | register_size (int n) | |
192 | { | |
193 | return reg_defs[n].size / 8; | |
194 | } | |
195 | ||
0d62e5e8 DJ |
196 | static char * |
197 | register_data (int n, int fetch) | |
0a30fbc4 | 198 | { |
0d62e5e8 | 199 | char *registers = get_regcache (current_inferior, fetch)->registers; |
c04a1aa8 | 200 | |
0a30fbc4 DJ |
201 | return registers + (reg_defs[n].offset / 8); |
202 | } | |
203 | ||
58caa3dc | 204 | void |
0729219d | 205 | supply_register (int n, const void *buf) |
58caa3dc | 206 | { |
0d62e5e8 | 207 | memcpy (register_data (n, 0), buf, register_size (n)); |
58caa3dc DJ |
208 | } |
209 | ||
210 | void | |
0729219d | 211 | supply_register_by_name (const char *name, const void *buf) |
58caa3dc DJ |
212 | { |
213 | supply_register (find_regno (name), buf); | |
214 | } | |
215 | ||
216 | void | |
0729219d | 217 | collect_register (int n, void *buf) |
58caa3dc | 218 | { |
0d62e5e8 DJ |
219 | memcpy (buf, register_data (n, 1), register_size (n)); |
220 | } | |
221 | ||
222 | void | |
223 | collect_register_as_string (int n, char *buf) | |
224 | { | |
225 | convert_int_to_ascii (register_data (n, 1), buf, register_size (n)); | |
58caa3dc DJ |
226 | } |
227 | ||
228 | void | |
0729219d | 229 | collect_register_by_name (const char *name, void *buf) |
58caa3dc DJ |
230 | { |
231 | collect_register (find_regno (name), buf); | |
232 | } |