1 /* Register support routines for the remote server for GDB.
2 Copyright (C) 2001, 2002, 2004, 2005, 2007 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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/>. */
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. */
29 struct inferior_regcache_data
32 unsigned char *registers;
35 static int register_bytes;
37 static struct reg *reg_defs;
38 static int num_registers;
40 const char **gdbserver_expedite_regs;
42 static struct inferior_regcache_data *
43 get_regcache (struct thread_info *inf, int fetch)
45 struct inferior_regcache_data *regcache;
47 regcache = (struct inferior_regcache_data *) inferior_regcache_data (inf);
50 fatal ("no register cache");
52 /* FIXME - fetch registers for INF */
53 if (fetch && regcache->registers_valid == 0)
55 fetch_inferior_registers (0);
56 regcache->registers_valid = 1;
63 regcache_invalidate_one (struct inferior_list_entry *entry)
65 struct thread_info *thread = (struct thread_info *) entry;
66 struct inferior_regcache_data *regcache;
68 regcache = (struct inferior_regcache_data *) inferior_regcache_data (thread);
70 if (regcache->registers_valid)
72 struct thread_info *saved_inferior = current_inferior;
74 current_inferior = thread;
75 store_inferior_registers (-1);
76 current_inferior = saved_inferior;
79 regcache->registers_valid = 0;
83 regcache_invalidate ()
85 for_each_inferior (&all_threads, regcache_invalidate_one);
89 registers_length (void)
91 return 2 * register_bytes;
95 new_register_cache (void)
97 struct inferior_regcache_data *regcache;
99 regcache = malloc (sizeof (*regcache));
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.");
108 regcache->registers_valid = 0;
114 free_register_cache (void *regcache_p)
116 struct inferior_regcache_data *regcache
117 = (struct inferior_regcache_data *) regcache_p;
119 free (regcache->registers);
124 set_register_cache (struct reg *regs, int n)
132 for (i = 0; i < n; i++)
134 regs[i].offset = offset;
135 offset += regs[i].size;
138 register_bytes = offset / 8;
142 registers_to_string (char *buf)
144 unsigned char *registers = get_regcache (current_inferior, 1)->registers;
146 convert_int_to_ascii (registers, buf, register_bytes);
150 registers_from_string (char *buf)
152 int len = strlen (buf);
153 unsigned char *registers = get_regcache (current_inferior, 1)->registers;
155 if (len != register_bytes * 2)
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;
161 convert_ascii_to_int (buf, registers, len / 2);
165 find_register_by_name (const char *name)
169 for (i = 0; i < num_registers; i++)
170 if (!strcmp (name, reg_defs[i].name))
172 fatal ("Unknown register %s requested", name);
177 find_regno (const char *name)
181 for (i = 0; i < num_registers; i++)
182 if (!strcmp (name, reg_defs[i].name))
184 fatal ("Unknown register %s requested", name);
189 find_register_by_number (int n)
195 register_size (int n)
197 return reg_defs[n].size / 8;
200 static unsigned char *
201 register_data (int n, int fetch)
203 unsigned char *registers
204 = get_regcache (current_inferior, fetch)->registers;
206 return registers + (reg_defs[n].offset / 8);
210 supply_register (int n, const void *buf)
212 memcpy (register_data (n, 0), buf, register_size (n));
216 supply_register_by_name (const char *name, const void *buf)
218 supply_register (find_regno (name), buf);
222 collect_register (int n, void *buf)
224 memcpy (buf, register_data (n, 1), register_size (n));
228 collect_register_as_string (int n, char *buf)
230 convert_int_to_ascii (register_data (n, 1), buf, register_size (n));
234 collect_register_by_name (const char *name, void *buf)
236 collect_register (find_regno (name), buf);