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