1 /* Register support routines for the remote server for GDB.
2 Copyright (C) 2001, 2002, 2004, 2005, 2011
3 Free Software Foundation, Inc.
5 This file is part of GDB.
6 It has been modified to integrate it in valgrind
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
26 /* The private data for the register cache. Note that we have one
27 per inferior; this is primarily for simplicity, as the performance
28 benefit is minimal. */
30 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
;
43 struct inferior_regcache_data
* get_regcache (struct thread_info
*inf
)
45 struct inferior_regcache_data
*regcache
;
47 regcache
= (struct inferior_regcache_data
*) inferior_regcache_data (inf
);
50 fatal ("no register cache\n");
55 int registers_length (void)
57 return 2 * register_bytes
;
60 void *new_register_cache (void)
62 struct inferior_regcache_data
*regcache
;
64 regcache
= malloc (sizeof (*regcache
));
66 /* Make sure to zero-initialize the register cache when it is created,
67 in case there are registers the target never fetches. This way they'll
68 read as zero instead of garbage. */
69 regcache
->registers
= calloc (1, register_bytes
);
70 if (regcache
->registers
== NULL
)
71 fatal ("Could not allocate register cache.\n");
76 void free_register_cache (void *regcache_p
)
78 struct inferior_regcache_data
*regcache
79 = (struct inferior_regcache_data
*) regcache_p
;
81 free (regcache
->registers
);
85 /* if a regcache exists for entry, reallocate it.
86 This is needed if the shadow registers are added.
87 In such a case, a 2nd call to set_register_cache is done
88 which will cause the reallocation of already created caches. */
90 void regcache_realloc_one (struct inferior_list_entry
*entry
)
92 struct thread_info
*thread
= (struct thread_info
*) entry
;
93 struct inferior_regcache_data
*regcache
;
95 regcache
= (struct inferior_regcache_data
*) inferior_regcache_data (thread
);
98 free_register_cache (regcache
);
99 set_inferior_regcache_data (thread
, new_register_cache ());
103 void set_register_cache (struct reg
*regs
, int n
)
111 for (i
= 0; i
< n
; i
++) {
112 regs
[i
].offset
= offset
;
113 offset
+= regs
[i
].size
;
116 register_bytes
= offset
/ 8;
118 for_each_inferior (&all_threads
, regcache_realloc_one
);
121 void registers_to_string (char *buf
)
123 unsigned char *registers
= get_regcache (current_inferior
)->registers
;
125 for (int i
= 0; i
< num_registers
; i
++)
126 valgrind_fetch_register (i
, registers
+ (reg_defs
[i
].offset
/ 8));
127 convert_int_to_ascii (registers
, buf
, register_bytes
);
130 void registers_from_string (const char *buf
)
132 int len
= strlen (buf
);
133 unsigned char *registers
= get_regcache (current_inferior
)->registers
;
135 if (len
!= register_bytes
* 2) {
136 warning ("Wrong sized register packet (expected %d bytes, got %d)\n",
137 2*register_bytes
, len
);
138 if (len
> register_bytes
* 2)
139 len
= register_bytes
* 2;
141 convert_ascii_to_int (buf
, registers
, len
/ 2);
144 int find_regno (const char *name
)
148 for (i
= 0; i
< num_registers
; i
++)
149 if (!strcmp (name
, reg_defs
[i
].name
))
151 fatal ("Unknown register %s requested\n", name
);
155 struct reg
*find_register_by_number (int n
)
160 int register_size (int n
)
162 return reg_defs
[n
].size
/ 8;
165 void supply_register (int n
, const void *buf
)
167 valgrind_store_register (n
, buf
);
170 void supply_register_from_string (int n
, const char *buf
)
172 unsigned char bytes_register
[register_size (n
)];
173 convert_ascii_to_int (buf
, bytes_register
, register_size (n
));
174 valgrind_store_register (n
, bytes_register
);
177 void supply_register_by_name (const char *name
, const void *buf
)
179 supply_register (find_regno (name
), buf
);
182 void collect_register (int n
, void *buf
)
184 valgrind_fetch_register (n
, buf
);
187 void collect_register_as_string (int n
, char *buf
)
189 unsigned char local_buf
[register_size (n
)];
190 valgrind_fetch_register (n
, local_buf
);
191 convert_int_to_ascii (local_buf
, buf
, register_size (n
));
194 void collect_register_by_name (const char *name
, void *buf
)
196 collect_register (find_regno (name
), buf
);