Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Remote target communications for serial-line targets in custom GDB protocol |
8926118c | 2 | |
6aba47ca | 3 | Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, |
9b254dd1 | 4 | 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 |
29182b13 | 5 | Free Software Foundation, Inc. |
c906108c | 6 | |
c5aa993b JM |
7 | This file is part of GDB. |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 11 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b JM |
12 | (at your option) any later version. |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c5aa993b | 21 | |
23860348 | 22 | /* See the GDB User Guide for details of the GDB remote protocol. */ |
c5aa993b | 23 | |
c906108c SS |
24 | #include "defs.h" |
25 | #include "gdb_string.h" | |
26 | #include <ctype.h> | |
27 | #include <fcntl.h> | |
c906108c SS |
28 | #include "inferior.h" |
29 | #include "bfd.h" | |
30 | #include "symfile.h" | |
60250e8b | 31 | #include "exceptions.h" |
c906108c | 32 | #include "target.h" |
c5aa993b | 33 | /*#include "terminal.h" */ |
c906108c SS |
34 | #include "gdbcmd.h" |
35 | #include "objfiles.h" | |
36 | #include "gdb-stabs.h" | |
37 | #include "gdbthread.h" | |
c2c6d25f | 38 | #include "remote.h" |
4e052eda | 39 | #include "regcache.h" |
fd0407d6 | 40 | #include "value.h" |
1ff9c3d6 | 41 | #include "gdb_assert.h" |
6867ae3e | 42 | #include "observer.h" |
a77053c2 | 43 | #include "solib.h" |
37a105a1 DJ |
44 | #include "cli/cli-decode.h" |
45 | #include "cli/cli-setshow.h" | |
424163ea | 46 | #include "target-descriptions.h" |
c906108c | 47 | |
7a292a7a | 48 | #include <ctype.h> |
9846de1b | 49 | #include <sys/time.h> |
c906108c | 50 | |
43ff13b4 | 51 | #include "event-loop.h" |
c2c6d25f | 52 | #include "event-top.h" |
2acceee2 | 53 | #include "inf-loop.h" |
43ff13b4 | 54 | |
c906108c SS |
55 | #include <signal.h> |
56 | #include "serial.h" | |
57 | ||
6240bebf MS |
58 | #include "gdbcore.h" /* for exec_bfd */ |
59 | ||
449092f6 | 60 | #include "remote-fileio.h" |
a6b151f1 | 61 | #include "gdb/fileio.h" |
3e88cf8d | 62 | #include "gdb_stat.h" |
449092f6 | 63 | |
fd79ecee DJ |
64 | #include "memory-map.h" |
65 | ||
6765f3e5 DJ |
66 | /* The size to align memory write packets, when practical. The protocol |
67 | does not guarantee any alignment, and gdb will generate short | |
68 | writes and unaligned writes, but even as a best-effort attempt this | |
69 | can improve bulk transfers. For instance, if a write is misaligned | |
70 | relative to the target's data bus, the stub may need to make an extra | |
71 | round trip fetching data from the target. This doesn't make a | |
72 | huge difference, but it's easy to do, so we try to be helpful. | |
73 | ||
74 | The alignment chosen is arbitrary; usually data bus width is | |
75 | important here, not the possibly larger cache line size. */ | |
76 | enum { REMOTE_ALIGN_WRITES = 16 }; | |
77 | ||
23860348 | 78 | /* Prototypes for local functions. */ |
6426a772 JM |
79 | static void cleanup_sigint_signal_handler (void *dummy); |
80 | static void initialize_sigint_signal_handler (void); | |
6d820c5c | 81 | static int getpkt_sane (char **buf, long *sizeof_buf, int forever); |
74531fed PA |
82 | static int getpkt_or_notif_sane (char **buf, long *sizeof_buf, |
83 | int forever); | |
6426a772 | 84 | |
a14ed312 KB |
85 | static void handle_remote_sigint (int); |
86 | static void handle_remote_sigint_twice (int); | |
87 | static void async_remote_interrupt (gdb_client_data); | |
88 | void async_remote_interrupt_twice (gdb_client_data); | |
43ff13b4 | 89 | |
a14ed312 | 90 | static void remote_files_info (struct target_ops *ignore); |
c906108c | 91 | |
316f2060 | 92 | static void remote_prepare_to_store (struct regcache *regcache); |
c906108c | 93 | |
56be3814 | 94 | static void remote_fetch_registers (struct regcache *regcache, int regno); |
c906108c | 95 | |
39f77062 KB |
96 | static void remote_resume (ptid_t ptid, int step, |
97 | enum target_signal siggnal); | |
a14ed312 | 98 | static void remote_open (char *name, int from_tty); |
c906108c | 99 | |
a14ed312 | 100 | static void extended_remote_open (char *name, int from_tty); |
c906108c | 101 | |
75c99385 | 102 | static void remote_open_1 (char *, int, struct target_ops *, int extended_p); |
c906108c | 103 | |
a14ed312 | 104 | static void remote_close (int quitting); |
c906108c | 105 | |
56be3814 | 106 | static void remote_store_registers (struct regcache *regcache, int regno); |
c906108c | 107 | |
136d6dae | 108 | static void remote_mourn (struct target_ops *ops); |
c906108c | 109 | |
a14ed312 | 110 | static void extended_remote_restart (void); |
c906108c | 111 | |
136d6dae | 112 | static void extended_remote_mourn (struct target_ops *); |
c906108c | 113 | |
a14ed312 | 114 | static void remote_mourn_1 (struct target_ops *); |
c906108c | 115 | |
6d820c5c | 116 | static void remote_send (char **buf, long *sizeof_buf_p); |
c906108c | 117 | |
a14ed312 | 118 | static int readchar (int timeout); |
c906108c | 119 | |
39f77062 | 120 | static ptid_t remote_wait (ptid_t ptid, |
75c99385 | 121 | struct target_waitstatus *status); |
c906108c | 122 | |
a14ed312 | 123 | static void remote_kill (void); |
c906108c | 124 | |
a14ed312 | 125 | static int tohex (int nib); |
c906108c | 126 | |
75c99385 PA |
127 | static int remote_can_async_p (void); |
128 | ||
129 | static int remote_is_async_p (void); | |
130 | ||
131 | static void remote_async (void (*callback) (enum inferior_event_type event_type, | |
132 | void *context), void *context); | |
133 | ||
134 | static int remote_async_mask (int new_mask); | |
135 | ||
136d6dae | 136 | static void remote_detach (struct target_ops *ops, char *args, int from_tty); |
c906108c | 137 | |
a14ed312 | 138 | static void remote_interrupt (int signo); |
c906108c | 139 | |
a14ed312 | 140 | static void remote_interrupt_twice (int signo); |
7a292a7a | 141 | |
a14ed312 | 142 | static void interrupt_query (void); |
c906108c | 143 | |
79d7f229 PA |
144 | static void set_general_thread (struct ptid ptid); |
145 | static void set_continue_thread (struct ptid ptid); | |
c906108c | 146 | |
39f77062 | 147 | static int remote_thread_alive (ptid_t); |
c906108c | 148 | |
a14ed312 | 149 | static void get_offsets (void); |
c906108c | 150 | |
6d820c5c DJ |
151 | static void skip_frame (void); |
152 | ||
153 | static long read_frame (char **buf_p, long *sizeof_buf); | |
c906108c | 154 | |
a14ed312 | 155 | static int hexnumlen (ULONGEST num); |
c906108c | 156 | |
a14ed312 | 157 | static void init_remote_ops (void); |
c906108c | 158 | |
a14ed312 | 159 | static void init_extended_remote_ops (void); |
c906108c | 160 | |
94cc34af | 161 | static void remote_stop (ptid_t); |
c906108c | 162 | |
a14ed312 | 163 | static int ishex (int ch, int *val); |
c906108c | 164 | |
a14ed312 | 165 | static int stubhex (int ch); |
c906108c | 166 | |
a14ed312 | 167 | static int hexnumstr (char *, ULONGEST); |
c906108c | 168 | |
a14ed312 | 169 | static int hexnumnstr (char *, ULONGEST, int); |
2df3850c | 170 | |
a14ed312 | 171 | static CORE_ADDR remote_address_masked (CORE_ADDR); |
c906108c | 172 | |
a14ed312 | 173 | static void print_packet (char *); |
c906108c | 174 | |
a14ed312 | 175 | static unsigned long crc32 (unsigned char *, int, unsigned int); |
c906108c | 176 | |
a14ed312 | 177 | static void compare_sections_command (char *, int); |
c906108c | 178 | |
a14ed312 | 179 | static void packet_command (char *, int); |
c906108c | 180 | |
a14ed312 | 181 | static int stub_unpack_int (char *buff, int fieldlength); |
c906108c | 182 | |
39f77062 | 183 | static ptid_t remote_current_thread (ptid_t oldptid); |
c906108c | 184 | |
a14ed312 | 185 | static void remote_find_new_threads (void); |
c906108c | 186 | |
79d7f229 | 187 | static void record_currthread (ptid_t currthread); |
c906108c | 188 | |
30559e10 | 189 | static int fromhex (int a); |
c906108c | 190 | |
cfd77fa1 | 191 | static int hex2bin (const char *hex, gdb_byte *bin, int count); |
c906108c | 192 | |
cfd77fa1 | 193 | static int bin2hex (const gdb_byte *bin, char *hex, int count); |
234fa6d1 | 194 | |
a14ed312 | 195 | static int putpkt_binary (char *buf, int cnt); |
c906108c | 196 | |
a14ed312 | 197 | static void check_binary_download (CORE_ADDR addr); |
c906108c | 198 | |
5a2468f5 | 199 | struct packet_config; |
5a2468f5 | 200 | |
a14ed312 | 201 | static void show_packet_config_cmd (struct packet_config *config); |
5a2468f5 | 202 | |
d471ea57 | 203 | static void update_packet_config (struct packet_config *config); |
5a2468f5 | 204 | |
bb572ddd DJ |
205 | static void set_remote_protocol_packet_cmd (char *args, int from_tty, |
206 | struct cmd_list_element *c); | |
207 | ||
208 | static void show_remote_protocol_packet_cmd (struct ui_file *file, | |
209 | int from_tty, | |
210 | struct cmd_list_element *c, | |
211 | const char *value); | |
212 | ||
82f73884 PA |
213 | static char *write_ptid (char *buf, const char *endbuf, ptid_t ptid); |
214 | static ptid_t read_ptid (char *buf, char **obuf); | |
215 | ||
c8d104ad PA |
216 | static void remote_query_supported (void); |
217 | ||
218 | static void remote_check_symbols (struct objfile *objfile); | |
219 | ||
a14ed312 | 220 | void _initialize_remote (void); |
c906108c | 221 | |
74531fed PA |
222 | struct stop_reply; |
223 | static struct stop_reply *stop_reply_xmalloc (void); | |
224 | static void stop_reply_xfree (struct stop_reply *); | |
225 | static void do_stop_reply_xfree (void *arg); | |
226 | static void remote_parse_stop_reply (char *buf, struct stop_reply *); | |
227 | static void push_stop_reply (struct stop_reply *); | |
228 | static void remote_get_pending_stop_replies (void); | |
229 | static void discard_pending_stop_replies (int pid); | |
230 | static int peek_stop_reply (ptid_t ptid); | |
231 | ||
232 | static void remote_async_inferior_event_handler (gdb_client_data); | |
233 | static void remote_async_get_pending_events_handler (gdb_client_data); | |
234 | ||
d3fd5342 PA |
235 | static void remote_terminal_ours (void); |
236 | ||
74531fed PA |
237 | /* The non-stop remote protocol provisions for one pending stop reply. |
238 | This is where we keep it until it is acknowledged. */ | |
239 | ||
240 | static struct stop_reply *pending_stop_reply = NULL; | |
241 | ||
a6b151f1 DJ |
242 | /* For "remote". */ |
243 | ||
244 | static struct cmd_list_element *remote_cmdlist; | |
245 | ||
bb572ddd DJ |
246 | /* For "set remote" and "show remote". */ |
247 | ||
248 | static struct cmd_list_element *remote_set_cmdlist; | |
249 | static struct cmd_list_element *remote_show_cmdlist; | |
250 | ||
ea9c271d DJ |
251 | /* Description of the remote protocol state for the currently |
252 | connected target. This is per-target state, and independent of the | |
253 | selected architecture. */ | |
254 | ||
255 | struct remote_state | |
256 | { | |
257 | /* A buffer to use for incoming packets, and its current size. The | |
258 | buffer is grown dynamically for larger incoming packets. | |
259 | Outgoing packets may also be constructed in this buffer. | |
260 | BUF_SIZE is always at least REMOTE_PACKET_SIZE; | |
261 | REMOTE_PACKET_SIZE should be used to limit the length of outgoing | |
262 | packets. */ | |
263 | char *buf; | |
264 | long buf_size; | |
be2a5f71 DJ |
265 | |
266 | /* If we negotiated packet size explicitly (and thus can bypass | |
267 | heuristics for the largest packet size that will not overflow | |
268 | a buffer in the stub), this will be set to that packet size. | |
269 | Otherwise zero, meaning to use the guessed size. */ | |
270 | long explicit_packet_size; | |
2d717e4f DJ |
271 | |
272 | /* remote_wait is normally called when the target is running and | |
273 | waits for a stop reply packet. But sometimes we need to call it | |
274 | when the target is already stopped. We can send a "?" packet | |
275 | and have remote_wait read the response. Or, if we already have | |
276 | the response, we can stash it in BUF and tell remote_wait to | |
277 | skip calling getpkt. This flag is set when BUF contains a | |
278 | stop reply packet and the target is not waiting. */ | |
279 | int cached_wait_status; | |
a6f3e723 SL |
280 | |
281 | /* True, if in no ack mode. That is, neither GDB nor the stub will | |
282 | expect acks from each other. The connection is assumed to be | |
283 | reliable. */ | |
284 | int noack_mode; | |
82f73884 PA |
285 | |
286 | /* True if we're connected in extended remote mode. */ | |
287 | int extended; | |
288 | ||
289 | /* True if the stub reported support for multi-process | |
290 | extensions. */ | |
291 | int multi_process_aware; | |
e24a49d8 PA |
292 | |
293 | /* True if we resumed the target and we're waiting for the target to | |
294 | stop. In the mean time, we can't start another command/query. | |
295 | The remote server wouldn't be ready to process it, so we'd | |
296 | timeout waiting for a reply that would never come and eventually | |
297 | we'd close the connection. This can happen in asynchronous mode | |
298 | because we allow GDB commands while the target is running. */ | |
299 | int waiting_for_stop_reply; | |
74531fed PA |
300 | |
301 | /* True if the stub reports support for non-stop mode. */ | |
302 | int non_stop_aware; | |
303 | ||
304 | /* True if the stub reports support for vCont;t. */ | |
305 | int support_vCont_t; | |
ea9c271d DJ |
306 | }; |
307 | ||
82f73884 PA |
308 | /* Returns true if the multi-process extensions are in effect. */ |
309 | static int | |
310 | remote_multi_process_p (struct remote_state *rs) | |
311 | { | |
312 | return rs->extended && rs->multi_process_aware; | |
313 | } | |
314 | ||
ea9c271d DJ |
315 | /* This data could be associated with a target, but we do not always |
316 | have access to the current target when we need it, so for now it is | |
317 | static. This will be fine for as long as only one target is in use | |
318 | at a time. */ | |
319 | static struct remote_state remote_state; | |
320 | ||
321 | static struct remote_state * | |
0b83947e | 322 | get_remote_state_raw (void) |
ea9c271d DJ |
323 | { |
324 | return &remote_state; | |
325 | } | |
326 | ||
327 | /* Description of the remote protocol for a given architecture. */ | |
d01949b6 | 328 | |
ad10f812 AC |
329 | struct packet_reg |
330 | { | |
331 | long offset; /* Offset into G packet. */ | |
332 | long regnum; /* GDB's internal register number. */ | |
333 | LONGEST pnum; /* Remote protocol register number. */ | |
b323314b | 334 | int in_g_packet; /* Always part of G packet. */ |
1cf3db46 | 335 | /* long size in bytes; == register_size (target_gdbarch, regnum); |
23860348 | 336 | at present. */ |
1cf3db46 | 337 | /* char *name; == gdbarch_register_name (target_gdbarch, regnum); |
c9f4d572 | 338 | at present. */ |
ad10f812 AC |
339 | }; |
340 | ||
ea9c271d | 341 | struct remote_arch_state |
d01949b6 | 342 | { |
ad10f812 AC |
343 | /* Description of the remote protocol registers. */ |
344 | long sizeof_g_packet; | |
b323314b AC |
345 | |
346 | /* Description of the remote protocol registers indexed by REGNUM | |
f57d151a | 347 | (making an array gdbarch_num_regs in size). */ |
b323314b | 348 | struct packet_reg *regs; |
ad10f812 | 349 | |
d01949b6 AC |
350 | /* This is the size (in chars) of the first response to the ``g'' |
351 | packet. It is used as a heuristic when determining the maximum | |
352 | size of memory-read and memory-write packets. A target will | |
353 | typically only reserve a buffer large enough to hold the ``g'' | |
354 | packet. The size does not include packet overhead (headers and | |
23860348 | 355 | trailers). */ |
d01949b6 AC |
356 | long actual_register_packet_size; |
357 | ||
358 | /* This is the maximum size (in chars) of a non read/write packet. | |
23860348 | 359 | It is also used as a cap on the size of read/write packets. */ |
d01949b6 AC |
360 | long remote_packet_size; |
361 | }; | |
362 | ||
3c3bea1c | 363 | |
d01949b6 AC |
364 | /* Handle for retreving the remote protocol data from gdbarch. */ |
365 | static struct gdbarch_data *remote_gdbarch_data_handle; | |
366 | ||
ea9c271d DJ |
367 | static struct remote_arch_state * |
368 | get_remote_arch_state (void) | |
d01949b6 | 369 | { |
1cf3db46 | 370 | return gdbarch_data (target_gdbarch, remote_gdbarch_data_handle); |
d01949b6 AC |
371 | } |
372 | ||
0b83947e DJ |
373 | /* Fetch the global remote target state. */ |
374 | ||
375 | static struct remote_state * | |
376 | get_remote_state (void) | |
377 | { | |
378 | /* Make sure that the remote architecture state has been | |
379 | initialized, because doing so might reallocate rs->buf. Any | |
380 | function which calls getpkt also needs to be mindful of changes | |
381 | to rs->buf, but this call limits the number of places which run | |
382 | into trouble. */ | |
383 | get_remote_arch_state (); | |
384 | ||
385 | return get_remote_state_raw (); | |
386 | } | |
387 | ||
74ca34ce DJ |
388 | static int |
389 | compare_pnums (const void *lhs_, const void *rhs_) | |
390 | { | |
391 | const struct packet_reg * const *lhs = lhs_; | |
392 | const struct packet_reg * const *rhs = rhs_; | |
393 | ||
394 | if ((*lhs)->pnum < (*rhs)->pnum) | |
395 | return -1; | |
396 | else if ((*lhs)->pnum == (*rhs)->pnum) | |
397 | return 0; | |
398 | else | |
399 | return 1; | |
400 | } | |
401 | ||
d01949b6 AC |
402 | static void * |
403 | init_remote_state (struct gdbarch *gdbarch) | |
404 | { | |
74ca34ce | 405 | int regnum, num_remote_regs, offset; |
0b83947e | 406 | struct remote_state *rs = get_remote_state_raw (); |
ea9c271d | 407 | struct remote_arch_state *rsa; |
74ca34ce | 408 | struct packet_reg **remote_regs; |
ea9c271d DJ |
409 | |
410 | rsa = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct remote_arch_state); | |
d01949b6 | 411 | |
123dc839 DJ |
412 | /* Use the architecture to build a regnum<->pnum table, which will be |
413 | 1:1 unless a feature set specifies otherwise. */ | |
f57d151a | 414 | rsa->regs = GDBARCH_OBSTACK_CALLOC (gdbarch, |
4a22f64d | 415 | gdbarch_num_regs (gdbarch), |
f57d151a | 416 | struct packet_reg); |
4a22f64d | 417 | for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++) |
ad10f812 | 418 | { |
ea9c271d | 419 | struct packet_reg *r = &rsa->regs[regnum]; |
baef701f | 420 | |
4a22f64d | 421 | if (register_size (gdbarch, regnum) == 0) |
baef701f DJ |
422 | /* Do not try to fetch zero-sized (placeholder) registers. */ |
423 | r->pnum = -1; | |
424 | else | |
425 | r->pnum = gdbarch_remote_register_number (gdbarch, regnum); | |
426 | ||
b323314b | 427 | r->regnum = regnum; |
74ca34ce DJ |
428 | } |
429 | ||
430 | /* Define the g/G packet format as the contents of each register | |
431 | with a remote protocol number, in order of ascending protocol | |
432 | number. */ | |
433 | ||
4a22f64d UW |
434 | remote_regs = alloca (gdbarch_num_regs (gdbarch) |
435 | * sizeof (struct packet_reg *)); | |
f57d151a | 436 | for (num_remote_regs = 0, regnum = 0; |
4a22f64d | 437 | regnum < gdbarch_num_regs (gdbarch); |
f57d151a | 438 | regnum++) |
74ca34ce DJ |
439 | if (rsa->regs[regnum].pnum != -1) |
440 | remote_regs[num_remote_regs++] = &rsa->regs[regnum]; | |
7d58c67d | 441 | |
74ca34ce DJ |
442 | qsort (remote_regs, num_remote_regs, sizeof (struct packet_reg *), |
443 | compare_pnums); | |
444 | ||
445 | for (regnum = 0, offset = 0; regnum < num_remote_regs; regnum++) | |
446 | { | |
447 | remote_regs[regnum]->in_g_packet = 1; | |
448 | remote_regs[regnum]->offset = offset; | |
4a22f64d | 449 | offset += register_size (gdbarch, remote_regs[regnum]->regnum); |
ad10f812 AC |
450 | } |
451 | ||
74ca34ce DJ |
452 | /* Record the maximum possible size of the g packet - it may turn out |
453 | to be smaller. */ | |
454 | rsa->sizeof_g_packet = offset; | |
455 | ||
d01949b6 AC |
456 | /* Default maximum number of characters in a packet body. Many |
457 | remote stubs have a hardwired buffer size of 400 bytes | |
458 | (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used | |
459 | as the maximum packet-size to ensure that the packet and an extra | |
460 | NUL character can always fit in the buffer. This stops GDB | |
461 | trashing stubs that try to squeeze an extra NUL into what is | |
ea9c271d DJ |
462 | already a full buffer (As of 1999-12-04 that was most stubs). */ |
463 | rsa->remote_packet_size = 400 - 1; | |
d01949b6 | 464 | |
ea9c271d DJ |
465 | /* This one is filled in when a ``g'' packet is received. */ |
466 | rsa->actual_register_packet_size = 0; | |
467 | ||
468 | /* Should rsa->sizeof_g_packet needs more space than the | |
ad10f812 AC |
469 | default, adjust the size accordingly. Remember that each byte is |
470 | encoded as two characters. 32 is the overhead for the packet | |
471 | header / footer. NOTE: cagney/1999-10-26: I suspect that 8 | |
d01949b6 | 472 | (``$NN:G...#NN'') is a better guess, the below has been padded a |
23860348 | 473 | little. */ |
ea9c271d DJ |
474 | if (rsa->sizeof_g_packet > ((rsa->remote_packet_size - 32) / 2)) |
475 | rsa->remote_packet_size = (rsa->sizeof_g_packet * 2 + 32); | |
802188a7 | 476 | |
ea9c271d DJ |
477 | /* Make sure that the packet buffer is plenty big enough for |
478 | this architecture. */ | |
479 | if (rs->buf_size < rsa->remote_packet_size) | |
480 | { | |
481 | rs->buf_size = 2 * rsa->remote_packet_size; | |
7fca722e | 482 | rs->buf = xrealloc (rs->buf, rs->buf_size); |
ea9c271d | 483 | } |
6d820c5c | 484 | |
ea9c271d DJ |
485 | return rsa; |
486 | } | |
487 | ||
488 | /* Return the current allowed size of a remote packet. This is | |
489 | inferred from the current architecture, and should be used to | |
490 | limit the length of outgoing packets. */ | |
491 | static long | |
492 | get_remote_packet_size (void) | |
493 | { | |
be2a5f71 | 494 | struct remote_state *rs = get_remote_state (); |
ea9c271d DJ |
495 | struct remote_arch_state *rsa = get_remote_arch_state (); |
496 | ||
be2a5f71 DJ |
497 | if (rs->explicit_packet_size) |
498 | return rs->explicit_packet_size; | |
499 | ||
ea9c271d | 500 | return rsa->remote_packet_size; |
d01949b6 AC |
501 | } |
502 | ||
ad10f812 | 503 | static struct packet_reg * |
ea9c271d | 504 | packet_reg_from_regnum (struct remote_arch_state *rsa, long regnum) |
ad10f812 | 505 | { |
1cf3db46 | 506 | if (regnum < 0 && regnum >= gdbarch_num_regs (target_gdbarch)) |
b323314b AC |
507 | return NULL; |
508 | else | |
ad10f812 | 509 | { |
ea9c271d | 510 | struct packet_reg *r = &rsa->regs[regnum]; |
b323314b AC |
511 | gdb_assert (r->regnum == regnum); |
512 | return r; | |
ad10f812 | 513 | } |
ad10f812 AC |
514 | } |
515 | ||
516 | static struct packet_reg * | |
ea9c271d | 517 | packet_reg_from_pnum (struct remote_arch_state *rsa, LONGEST pnum) |
ad10f812 | 518 | { |
b323314b | 519 | int i; |
1cf3db46 | 520 | for (i = 0; i < gdbarch_num_regs (target_gdbarch); i++) |
ad10f812 | 521 | { |
ea9c271d | 522 | struct packet_reg *r = &rsa->regs[i]; |
b323314b AC |
523 | if (r->pnum == pnum) |
524 | return r; | |
ad10f812 AC |
525 | } |
526 | return NULL; | |
d01949b6 AC |
527 | } |
528 | ||
3c3bea1c GS |
529 | /* FIXME: graces/2002-08-08: These variables should eventually be |
530 | bound to an instance of the target object (as in gdbarch-tdep()), | |
531 | when such a thing exists. */ | |
532 | ||
533 | /* This is set to the data address of the access causing the target | |
534 | to stop for a watchpoint. */ | |
535 | static CORE_ADDR remote_watch_data_address; | |
536 | ||
94e08568 | 537 | /* This is non-zero if target stopped for a watchpoint. */ |
3c3bea1c GS |
538 | static int remote_stopped_by_watchpoint_p; |
539 | ||
c906108c SS |
540 | static struct target_ops remote_ops; |
541 | ||
542 | static struct target_ops extended_remote_ops; | |
543 | ||
b84876c2 PA |
544 | static int remote_async_mask_value = 1; |
545 | ||
6426a772 JM |
546 | /* FIXME: cagney/1999-09-23: Even though getpkt was called with |
547 | ``forever'' still use the normal timeout mechanism. This is | |
548 | currently used by the ASYNC code to guarentee that target reads | |
549 | during the initial connect always time-out. Once getpkt has been | |
550 | modified to return a timeout indication and, in turn | |
551 | remote_wait()/wait_for_inferior() have gained a timeout parameter | |
23860348 | 552 | this can go away. */ |
6426a772 JM |
553 | static int wait_forever_enabled_p = 1; |
554 | ||
555 | ||
c906108c SS |
556 | /* This variable chooses whether to send a ^C or a break when the user |
557 | requests program interruption. Although ^C is usually what remote | |
558 | systems expect, and that is the default here, sometimes a break is | |
559 | preferable instead. */ | |
560 | ||
561 | static int remote_break; | |
562 | ||
c906108c SS |
563 | /* Descriptor for I/O to remote machine. Initialize it to NULL so that |
564 | remote_open knows that we don't have a file open when the program | |
565 | starts. */ | |
819cc324 | 566 | static struct serial *remote_desc = NULL; |
c906108c | 567 | |
c906108c SS |
568 | /* This variable sets the number of bits in an address that are to be |
569 | sent in a memory ("M" or "m") packet. Normally, after stripping | |
570 | leading zeros, the entire address would be sent. This variable | |
571 | restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The | |
572 | initial implementation of remote.c restricted the address sent in | |
573 | memory packets to ``host::sizeof long'' bytes - (typically 32 | |
574 | bits). Consequently, for 64 bit targets, the upper 32 bits of an | |
575 | address was never sent. Since fixing this bug may cause a break in | |
576 | some remote targets this variable is principly provided to | |
23860348 | 577 | facilitate backward compatibility. */ |
c906108c SS |
578 | |
579 | static int remote_address_size; | |
580 | ||
75c99385 PA |
581 | /* Temporary to track who currently owns the terminal. See |
582 | remote_terminal_* for more details. */ | |
6426a772 JM |
583 | |
584 | static int remote_async_terminal_ours_p; | |
585 | ||
2d717e4f DJ |
586 | /* The executable file to use for "run" on the remote side. */ |
587 | ||
588 | static char *remote_exec_file = ""; | |
589 | ||
11cf8741 | 590 | \f |
11cf8741 | 591 | /* User configurable variables for the number of characters in a |
ea9c271d DJ |
592 | memory read/write packet. MIN (rsa->remote_packet_size, |
593 | rsa->sizeof_g_packet) is the default. Some targets need smaller | |
24b06219 | 594 | values (fifo overruns, et.al.) and some users need larger values |
ad10f812 AC |
595 | (speed up transfers). The variables ``preferred_*'' (the user |
596 | request), ``current_*'' (what was actually set) and ``forced_*'' | |
23860348 | 597 | (Positive - a soft limit, negative - a hard limit). */ |
11cf8741 JM |
598 | |
599 | struct memory_packet_config | |
600 | { | |
601 | char *name; | |
602 | long size; | |
603 | int fixed_p; | |
604 | }; | |
605 | ||
606 | /* Compute the current size of a read/write packet. Since this makes | |
607 | use of ``actual_register_packet_size'' the computation is dynamic. */ | |
608 | ||
609 | static long | |
610 | get_memory_packet_size (struct memory_packet_config *config) | |
611 | { | |
d01949b6 | 612 | struct remote_state *rs = get_remote_state (); |
ea9c271d DJ |
613 | struct remote_arch_state *rsa = get_remote_arch_state (); |
614 | ||
11cf8741 JM |
615 | /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk |
616 | law?) that some hosts don't cope very well with large alloca() | |
617 | calls. Eventually the alloca() code will be replaced by calls to | |
618 | xmalloc() and make_cleanups() allowing this restriction to either | |
23860348 | 619 | be lifted or removed. */ |
11cf8741 JM |
620 | #ifndef MAX_REMOTE_PACKET_SIZE |
621 | #define MAX_REMOTE_PACKET_SIZE 16384 | |
622 | #endif | |
3de11b2e | 623 | /* NOTE: 20 ensures we can write at least one byte. */ |
11cf8741 | 624 | #ifndef MIN_REMOTE_PACKET_SIZE |
3de11b2e | 625 | #define MIN_REMOTE_PACKET_SIZE 20 |
11cf8741 JM |
626 | #endif |
627 | long what_they_get; | |
628 | if (config->fixed_p) | |
629 | { | |
630 | if (config->size <= 0) | |
631 | what_they_get = MAX_REMOTE_PACKET_SIZE; | |
632 | else | |
633 | what_they_get = config->size; | |
634 | } | |
635 | else | |
636 | { | |
ea9c271d | 637 | what_they_get = get_remote_packet_size (); |
23860348 | 638 | /* Limit the packet to the size specified by the user. */ |
11cf8741 JM |
639 | if (config->size > 0 |
640 | && what_they_get > config->size) | |
641 | what_they_get = config->size; | |
be2a5f71 DJ |
642 | |
643 | /* Limit it to the size of the targets ``g'' response unless we have | |
644 | permission from the stub to use a larger packet size. */ | |
645 | if (rs->explicit_packet_size == 0 | |
646 | && rsa->actual_register_packet_size > 0 | |
647 | && what_they_get > rsa->actual_register_packet_size) | |
648 | what_they_get = rsa->actual_register_packet_size; | |
11cf8741 JM |
649 | } |
650 | if (what_they_get > MAX_REMOTE_PACKET_SIZE) | |
651 | what_they_get = MAX_REMOTE_PACKET_SIZE; | |
652 | if (what_they_get < MIN_REMOTE_PACKET_SIZE) | |
653 | what_they_get = MIN_REMOTE_PACKET_SIZE; | |
6d820c5c DJ |
654 | |
655 | /* Make sure there is room in the global buffer for this packet | |
656 | (including its trailing NUL byte). */ | |
657 | if (rs->buf_size < what_they_get + 1) | |
658 | { | |
659 | rs->buf_size = 2 * what_they_get; | |
660 | rs->buf = xrealloc (rs->buf, 2 * what_they_get); | |
661 | } | |
662 | ||
11cf8741 JM |
663 | return what_they_get; |
664 | } | |
665 | ||
666 | /* Update the size of a read/write packet. If they user wants | |
23860348 | 667 | something really big then do a sanity check. */ |
11cf8741 JM |
668 | |
669 | static void | |
670 | set_memory_packet_size (char *args, struct memory_packet_config *config) | |
671 | { | |
672 | int fixed_p = config->fixed_p; | |
673 | long size = config->size; | |
674 | if (args == NULL) | |
8a3fe4f8 | 675 | error (_("Argument required (integer, `fixed' or `limited').")); |
11cf8741 JM |
676 | else if (strcmp (args, "hard") == 0 |
677 | || strcmp (args, "fixed") == 0) | |
678 | fixed_p = 1; | |
679 | else if (strcmp (args, "soft") == 0 | |
680 | || strcmp (args, "limit") == 0) | |
681 | fixed_p = 0; | |
682 | else | |
683 | { | |
684 | char *end; | |
685 | size = strtoul (args, &end, 0); | |
686 | if (args == end) | |
8a3fe4f8 | 687 | error (_("Invalid %s (bad syntax)."), config->name); |
11cf8741 JM |
688 | #if 0 |
689 | /* Instead of explicitly capping the size of a packet to | |
690 | MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is | |
691 | instead allowed to set the size to something arbitrarily | |
23860348 | 692 | large. */ |
11cf8741 | 693 | if (size > MAX_REMOTE_PACKET_SIZE) |
8a3fe4f8 | 694 | error (_("Invalid %s (too large)."), config->name); |
11cf8741 JM |
695 | #endif |
696 | } | |
23860348 | 697 | /* Extra checks? */ |
11cf8741 JM |
698 | if (fixed_p && !config->fixed_p) |
699 | { | |
e2e0b3e5 AC |
700 | if (! query (_("The target may not be able to correctly handle a %s\n" |
701 | "of %ld bytes. Change the packet size? "), | |
11cf8741 | 702 | config->name, size)) |
8a3fe4f8 | 703 | error (_("Packet size not changed.")); |
11cf8741 | 704 | } |
23860348 | 705 | /* Update the config. */ |
11cf8741 JM |
706 | config->fixed_p = fixed_p; |
707 | config->size = size; | |
708 | } | |
709 | ||
710 | static void | |
711 | show_memory_packet_size (struct memory_packet_config *config) | |
712 | { | |
a3f17187 | 713 | printf_filtered (_("The %s is %ld. "), config->name, config->size); |
11cf8741 | 714 | if (config->fixed_p) |
a3f17187 | 715 | printf_filtered (_("Packets are fixed at %ld bytes.\n"), |
11cf8741 JM |
716 | get_memory_packet_size (config)); |
717 | else | |
a3f17187 | 718 | printf_filtered (_("Packets are limited to %ld bytes.\n"), |
11cf8741 JM |
719 | get_memory_packet_size (config)); |
720 | } | |
721 | ||
722 | static struct memory_packet_config memory_write_packet_config = | |
723 | { | |
724 | "memory-write-packet-size", | |
725 | }; | |
726 | ||
727 | static void | |
728 | set_memory_write_packet_size (char *args, int from_tty) | |
729 | { | |
730 | set_memory_packet_size (args, &memory_write_packet_config); | |
731 | } | |
732 | ||
733 | static void | |
734 | show_memory_write_packet_size (char *args, int from_tty) | |
735 | { | |
736 | show_memory_packet_size (&memory_write_packet_config); | |
737 | } | |
738 | ||
739 | static long | |
740 | get_memory_write_packet_size (void) | |
741 | { | |
742 | return get_memory_packet_size (&memory_write_packet_config); | |
743 | } | |
744 | ||
745 | static struct memory_packet_config memory_read_packet_config = | |
746 | { | |
747 | "memory-read-packet-size", | |
748 | }; | |
749 | ||
750 | static void | |
751 | set_memory_read_packet_size (char *args, int from_tty) | |
752 | { | |
753 | set_memory_packet_size (args, &memory_read_packet_config); | |
754 | } | |
755 | ||
756 | static void | |
757 | show_memory_read_packet_size (char *args, int from_tty) | |
758 | { | |
759 | show_memory_packet_size (&memory_read_packet_config); | |
760 | } | |
761 | ||
762 | static long | |
763 | get_memory_read_packet_size (void) | |
764 | { | |
765 | long size = get_memory_packet_size (&memory_read_packet_config); | |
766 | /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an | |
767 | extra buffer size argument before the memory read size can be | |
ea9c271d DJ |
768 | increased beyond this. */ |
769 | if (size > get_remote_packet_size ()) | |
770 | size = get_remote_packet_size (); | |
11cf8741 JM |
771 | return size; |
772 | } | |
773 | ||
11cf8741 | 774 | \f |
5a2468f5 JM |
775 | /* Generic configuration support for packets the stub optionally |
776 | supports. Allows the user to specify the use of the packet as well | |
23860348 | 777 | as allowing GDB to auto-detect support in the remote stub. */ |
5a2468f5 JM |
778 | |
779 | enum packet_support | |
780 | { | |
781 | PACKET_SUPPORT_UNKNOWN = 0, | |
782 | PACKET_ENABLE, | |
783 | PACKET_DISABLE | |
784 | }; | |
785 | ||
5a2468f5 JM |
786 | struct packet_config |
787 | { | |
bb572ddd DJ |
788 | const char *name; |
789 | const char *title; | |
7f19b9a2 | 790 | enum auto_boolean detect; |
5a2468f5 JM |
791 | enum packet_support support; |
792 | }; | |
793 | ||
d471ea57 | 794 | /* Analyze a packet's return value and update the packet config |
23860348 | 795 | accordingly. */ |
d471ea57 AC |
796 | |
797 | enum packet_result | |
798 | { | |
799 | PACKET_ERROR, | |
800 | PACKET_OK, | |
801 | PACKET_UNKNOWN | |
802 | }; | |
803 | ||
5a2468f5 | 804 | static void |
d471ea57 | 805 | update_packet_config (struct packet_config *config) |
5a2468f5 | 806 | { |
d471ea57 AC |
807 | switch (config->detect) |
808 | { | |
7f19b9a2 | 809 | case AUTO_BOOLEAN_TRUE: |
d471ea57 AC |
810 | config->support = PACKET_ENABLE; |
811 | break; | |
7f19b9a2 | 812 | case AUTO_BOOLEAN_FALSE: |
d471ea57 AC |
813 | config->support = PACKET_DISABLE; |
814 | break; | |
7f19b9a2 | 815 | case AUTO_BOOLEAN_AUTO: |
d471ea57 AC |
816 | config->support = PACKET_SUPPORT_UNKNOWN; |
817 | break; | |
818 | } | |
5a2468f5 JM |
819 | } |
820 | ||
821 | static void | |
fba45db2 | 822 | show_packet_config_cmd (struct packet_config *config) |
5a2468f5 JM |
823 | { |
824 | char *support = "internal-error"; | |
825 | switch (config->support) | |
826 | { | |
827 | case PACKET_ENABLE: | |
828 | support = "enabled"; | |
829 | break; | |
830 | case PACKET_DISABLE: | |
831 | support = "disabled"; | |
832 | break; | |
833 | case PACKET_SUPPORT_UNKNOWN: | |
834 | support = "unknown"; | |
835 | break; | |
836 | } | |
837 | switch (config->detect) | |
838 | { | |
7f19b9a2 | 839 | case AUTO_BOOLEAN_AUTO: |
37a105a1 DJ |
840 | printf_filtered (_("Support for the `%s' packet is auto-detected, currently %s.\n"), |
841 | config->name, support); | |
5a2468f5 | 842 | break; |
7f19b9a2 AC |
843 | case AUTO_BOOLEAN_TRUE: |
844 | case AUTO_BOOLEAN_FALSE: | |
37a105a1 DJ |
845 | printf_filtered (_("Support for the `%s' packet is currently %s.\n"), |
846 | config->name, support); | |
8e248173 | 847 | break; |
5a2468f5 JM |
848 | } |
849 | } | |
850 | ||
851 | static void | |
bb572ddd DJ |
852 | add_packet_config_cmd (struct packet_config *config, const char *name, |
853 | const char *title, int legacy) | |
d471ea57 | 854 | { |
5a2468f5 JM |
855 | char *set_doc; |
856 | char *show_doc; | |
d471ea57 | 857 | char *cmd_name; |
3ed07be4 | 858 | |
5a2468f5 JM |
859 | config->name = name; |
860 | config->title = title; | |
7f19b9a2 | 861 | config->detect = AUTO_BOOLEAN_AUTO; |
8e248173 | 862 | config->support = PACKET_SUPPORT_UNKNOWN; |
b435e160 AC |
863 | set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet", |
864 | name, title); | |
865 | show_doc = xstrprintf ("Show current use of remote protocol `%s' (%s) packet", | |
866 | name, title); | |
d471ea57 | 867 | /* set/show TITLE-packet {auto,on,off} */ |
b435e160 | 868 | cmd_name = xstrprintf ("%s-packet", title); |
e9e68a56 | 869 | add_setshow_auto_boolean_cmd (cmd_name, class_obscure, |
2c5b56ce | 870 | &config->detect, set_doc, show_doc, NULL, /* help_doc */ |
bb572ddd DJ |
871 | set_remote_protocol_packet_cmd, |
872 | show_remote_protocol_packet_cmd, | |
873 | &remote_set_cmdlist, &remote_show_cmdlist); | |
23860348 | 874 | /* set/show remote NAME-packet {auto,on,off} -- legacy. */ |
d471ea57 AC |
875 | if (legacy) |
876 | { | |
877 | char *legacy_name; | |
b435e160 | 878 | legacy_name = xstrprintf ("%s-packet", name); |
d471ea57 | 879 | add_alias_cmd (legacy_name, cmd_name, class_obscure, 0, |
bb572ddd | 880 | &remote_set_cmdlist); |
d471ea57 | 881 | add_alias_cmd (legacy_name, cmd_name, class_obscure, 0, |
bb572ddd | 882 | &remote_show_cmdlist); |
d471ea57 | 883 | } |
5a2468f5 JM |
884 | } |
885 | ||
d471ea57 | 886 | static enum packet_result |
a76d924d | 887 | packet_check_result (const char *buf) |
5a2468f5 | 888 | { |
d471ea57 | 889 | if (buf[0] != '\0') |
5a2468f5 | 890 | { |
d471ea57 | 891 | /* The stub recognized the packet request. Check that the |
23860348 | 892 | operation succeeded. */ |
a76d924d DJ |
893 | if (buf[0] == 'E' |
894 | && isxdigit (buf[1]) && isxdigit (buf[2]) | |
895 | && buf[3] == '\0') | |
896 | /* "Enn" - definitly an error. */ | |
897 | return PACKET_ERROR; | |
898 | ||
899 | /* Always treat "E." as an error. This will be used for | |
900 | more verbose error messages, such as E.memtypes. */ | |
901 | if (buf[0] == 'E' && buf[1] == '.') | |
902 | return PACKET_ERROR; | |
903 | ||
904 | /* The packet may or may not be OK. Just assume it is. */ | |
905 | return PACKET_OK; | |
906 | } | |
907 | else | |
908 | /* The stub does not support the packet. */ | |
909 | return PACKET_UNKNOWN; | |
910 | } | |
911 | ||
912 | static enum packet_result | |
913 | packet_ok (const char *buf, struct packet_config *config) | |
914 | { | |
915 | enum packet_result result; | |
916 | ||
917 | result = packet_check_result (buf); | |
918 | switch (result) | |
919 | { | |
920 | case PACKET_OK: | |
921 | case PACKET_ERROR: | |
922 | /* The stub recognized the packet request. */ | |
d471ea57 AC |
923 | switch (config->support) |
924 | { | |
925 | case PACKET_SUPPORT_UNKNOWN: | |
926 | if (remote_debug) | |
927 | fprintf_unfiltered (gdb_stdlog, | |
928 | "Packet %s (%s) is supported\n", | |
929 | config->name, config->title); | |
930 | config->support = PACKET_ENABLE; | |
931 | break; | |
932 | case PACKET_DISABLE: | |
8e65ff28 | 933 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 934 | _("packet_ok: attempt to use a disabled packet")); |
d471ea57 AC |
935 | break; |
936 | case PACKET_ENABLE: | |
937 | break; | |
938 | } | |
a76d924d DJ |
939 | break; |
940 | case PACKET_UNKNOWN: | |
23860348 | 941 | /* The stub does not support the packet. */ |
d471ea57 AC |
942 | switch (config->support) |
943 | { | |
944 | case PACKET_ENABLE: | |
7f19b9a2 | 945 | if (config->detect == AUTO_BOOLEAN_AUTO) |
d471ea57 | 946 | /* If the stub previously indicated that the packet was |
23860348 | 947 | supported then there is a protocol error.. */ |
8a3fe4f8 | 948 | error (_("Protocol error: %s (%s) conflicting enabled responses."), |
d471ea57 AC |
949 | config->name, config->title); |
950 | else | |
23860348 | 951 | /* The user set it wrong. */ |
8a3fe4f8 | 952 | error (_("Enabled packet %s (%s) not recognized by stub"), |
d471ea57 AC |
953 | config->name, config->title); |
954 | break; | |
955 | case PACKET_SUPPORT_UNKNOWN: | |
956 | if (remote_debug) | |
957 | fprintf_unfiltered (gdb_stdlog, | |
958 | "Packet %s (%s) is NOT supported\n", | |
959 | config->name, config->title); | |
960 | config->support = PACKET_DISABLE; | |
961 | break; | |
962 | case PACKET_DISABLE: | |
963 | break; | |
964 | } | |
a76d924d | 965 | break; |
5a2468f5 | 966 | } |
a76d924d DJ |
967 | |
968 | return result; | |
5a2468f5 JM |
969 | } |
970 | ||
444abaca DJ |
971 | enum { |
972 | PACKET_vCont = 0, | |
973 | PACKET_X, | |
974 | PACKET_qSymbol, | |
975 | PACKET_P, | |
976 | PACKET_p, | |
977 | PACKET_Z0, | |
978 | PACKET_Z1, | |
979 | PACKET_Z2, | |
980 | PACKET_Z3, | |
981 | PACKET_Z4, | |
a6b151f1 DJ |
982 | PACKET_vFile_open, |
983 | PACKET_vFile_pread, | |
984 | PACKET_vFile_pwrite, | |
985 | PACKET_vFile_close, | |
986 | PACKET_vFile_unlink, | |
0876f84a | 987 | PACKET_qXfer_auxv, |
23181151 | 988 | PACKET_qXfer_features, |
cfa9d6d9 | 989 | PACKET_qXfer_libraries, |
fd79ecee | 990 | PACKET_qXfer_memory_map, |
0e7f50da UW |
991 | PACKET_qXfer_spu_read, |
992 | PACKET_qXfer_spu_write, | |
444abaca | 993 | PACKET_qGetTLSAddr, |
be2a5f71 | 994 | PACKET_qSupported, |
89be2091 | 995 | PACKET_QPassSignals, |
08388c79 | 996 | PACKET_qSearch_memory, |
2d717e4f DJ |
997 | PACKET_vAttach, |
998 | PACKET_vRun, | |
a6f3e723 | 999 | PACKET_QStartNoAckMode, |
82f73884 | 1000 | PACKET_vKill, |
444abaca DJ |
1001 | PACKET_MAX |
1002 | }; | |
506fb367 | 1003 | |
444abaca | 1004 | static struct packet_config remote_protocol_packets[PACKET_MAX]; |
dc8acb97 MS |
1005 | |
1006 | static void | |
444abaca DJ |
1007 | set_remote_protocol_packet_cmd (char *args, int from_tty, |
1008 | struct cmd_list_element *c) | |
dc8acb97 | 1009 | { |
444abaca | 1010 | struct packet_config *packet; |
dc8acb97 | 1011 | |
444abaca DJ |
1012 | for (packet = remote_protocol_packets; |
1013 | packet < &remote_protocol_packets[PACKET_MAX]; | |
1014 | packet++) | |
1015 | { | |
1016 | if (&packet->detect == c->var) | |
1017 | { | |
1018 | update_packet_config (packet); | |
1019 | return; | |
1020 | } | |
1021 | } | |
1022 | internal_error (__FILE__, __LINE__, "Could not find config for %s", | |
1023 | c->name); | |
dc8acb97 MS |
1024 | } |
1025 | ||
5a2468f5 | 1026 | static void |
444abaca DJ |
1027 | show_remote_protocol_packet_cmd (struct ui_file *file, int from_tty, |
1028 | struct cmd_list_element *c, | |
1029 | const char *value) | |
5a2468f5 | 1030 | { |
444abaca | 1031 | struct packet_config *packet; |
5a2468f5 | 1032 | |
444abaca DJ |
1033 | for (packet = remote_protocol_packets; |
1034 | packet < &remote_protocol_packets[PACKET_MAX]; | |
1035 | packet++) | |
1036 | { | |
1037 | if (&packet->detect == c->var) | |
1038 | { | |
1039 | show_packet_config_cmd (packet); | |
1040 | return; | |
1041 | } | |
1042 | } | |
1043 | internal_error (__FILE__, __LINE__, "Could not find config for %s", | |
1044 | c->name); | |
5a2468f5 JM |
1045 | } |
1046 | ||
d471ea57 AC |
1047 | /* Should we try one of the 'Z' requests? */ |
1048 | ||
1049 | enum Z_packet_type | |
1050 | { | |
1051 | Z_PACKET_SOFTWARE_BP, | |
1052 | Z_PACKET_HARDWARE_BP, | |
1053 | Z_PACKET_WRITE_WP, | |
1054 | Z_PACKET_READ_WP, | |
1055 | Z_PACKET_ACCESS_WP, | |
1056 | NR_Z_PACKET_TYPES | |
1057 | }; | |
96baa820 | 1058 | |
d471ea57 | 1059 | /* For compatibility with older distributions. Provide a ``set remote |
23860348 | 1060 | Z-packet ...'' command that updates all the Z packet types. */ |
d471ea57 | 1061 | |
7f19b9a2 | 1062 | static enum auto_boolean remote_Z_packet_detect; |
96baa820 JM |
1063 | |
1064 | static void | |
fba45db2 KB |
1065 | set_remote_protocol_Z_packet_cmd (char *args, int from_tty, |
1066 | struct cmd_list_element *c) | |
96baa820 | 1067 | { |
d471ea57 AC |
1068 | int i; |
1069 | for (i = 0; i < NR_Z_PACKET_TYPES; i++) | |
1070 | { | |
444abaca DJ |
1071 | remote_protocol_packets[PACKET_Z0 + i].detect = remote_Z_packet_detect; |
1072 | update_packet_config (&remote_protocol_packets[PACKET_Z0 + i]); | |
d471ea57 | 1073 | } |
96baa820 JM |
1074 | } |
1075 | ||
1076 | static void | |
08546159 AC |
1077 | show_remote_protocol_Z_packet_cmd (struct ui_file *file, int from_tty, |
1078 | struct cmd_list_element *c, | |
1079 | const char *value) | |
96baa820 | 1080 | { |
d471ea57 AC |
1081 | int i; |
1082 | for (i = 0; i < NR_Z_PACKET_TYPES; i++) | |
1083 | { | |
444abaca | 1084 | show_packet_config_cmd (&remote_protocol_packets[PACKET_Z0 + i]); |
d471ea57 | 1085 | } |
96baa820 JM |
1086 | } |
1087 | ||
9d1f7ab2 MS |
1088 | /* Should we try the 'ThreadInfo' query packet? |
1089 | ||
1090 | This variable (NOT available to the user: auto-detect only!) | |
1091 | determines whether GDB will use the new, simpler "ThreadInfo" | |
1092 | query or the older, more complex syntax for thread queries. | |
802188a7 | 1093 | This is an auto-detect variable (set to true at each connect, |
9d1f7ab2 MS |
1094 | and set to false when the target fails to recognize it). */ |
1095 | ||
1096 | static int use_threadinfo_query; | |
1097 | static int use_threadextra_query; | |
1098 | ||
23860348 | 1099 | /* Tokens for use by the asynchronous signal handlers for SIGINT. */ |
d5d6fca5 DJ |
1100 | static struct async_signal_handler *sigint_remote_twice_token; |
1101 | static struct async_signal_handler *sigint_remote_token; | |
43ff13b4 | 1102 | |
74531fed PA |
1103 | \f |
1104 | /* Asynchronous signal handle registered as event loop source for | |
1105 | when we have pending events ready to be passed to the core. */ | |
1106 | ||
1107 | static struct async_event_handler *remote_async_inferior_event_token; | |
1108 | ||
1109 | /* Asynchronous signal handle registered as event loop source for when | |
1110 | the remote sent us a %Stop notification. The registered callback | |
1111 | will do a vStopped sequence to pull the rest of the events out of | |
1112 | the remote side into our event queue. */ | |
1113 | ||
1114 | static struct async_event_handler *remote_async_get_pending_events_token; | |
c906108c SS |
1115 | \f |
1116 | ||
79d7f229 PA |
1117 | static ptid_t magic_null_ptid; |
1118 | static ptid_t not_sent_ptid; | |
1119 | static ptid_t any_thread_ptid; | |
1120 | ||
1121 | /* These are the threads which we last sent to the remote system. The | |
1122 | TID member will be -1 for all or -2 for not sent yet. */ | |
1123 | ||
1124 | static ptid_t general_thread; | |
1125 | static ptid_t continue_thread; | |
c5aa993b | 1126 | |
c906108c | 1127 | static void |
74531fed | 1128 | notice_new_inferiors (ptid_t currthread) |
c906108c | 1129 | { |
c906108c SS |
1130 | /* If this is a new thread, add it to GDB's thread list. |
1131 | If we leave it up to WFI to do this, bad things will happen. */ | |
82f73884 PA |
1132 | |
1133 | if (in_thread_list (currthread) && is_exited (currthread)) | |
1134 | { | |
1135 | /* We're seeing an event on a thread id we knew had exited. | |
1136 | This has to be a new thread reusing the old id. Add it. */ | |
1137 | add_thread (currthread); | |
1138 | return; | |
1139 | } | |
1140 | ||
79d7f229 | 1141 | if (!in_thread_list (currthread)) |
c0a2216e PA |
1142 | { |
1143 | if (ptid_equal (pid_to_ptid (ptid_get_pid (currthread)), inferior_ptid)) | |
1144 | { | |
1145 | /* inferior_ptid has no thread member yet. This can happen | |
1146 | with the vAttach -> remote_wait,"TAAthread:" path if the | |
1147 | stub doesn't support qC. This is the first stop reported | |
1148 | after an attach, so this is the main thread. Update the | |
1149 | ptid in the thread list. */ | |
82f73884 PA |
1150 | thread_change_ptid (inferior_ptid, currthread); |
1151 | return; | |
c0a2216e | 1152 | } |
82f73884 PA |
1153 | |
1154 | if (ptid_equal (magic_null_ptid, inferior_ptid)) | |
c0a2216e PA |
1155 | { |
1156 | /* inferior_ptid is not set yet. This can happen with the | |
1157 | vRun -> remote_wait,"TAAthread:" path if the stub | |
1158 | doesn't support qC. This is the first stop reported | |
1159 | after an attach, so this is the main thread. Update the | |
1160 | ptid in the thread list. */ | |
82f73884 PA |
1161 | thread_change_ptid (inferior_ptid, currthread); |
1162 | return; | |
c0a2216e | 1163 | } |
82f73884 | 1164 | |
29c87f7f PA |
1165 | /* When connecting to a target remote, or to a target |
1166 | extended-remote which already was debugging an inferior, we | |
1167 | may not know about it yet. Add it before adding its child | |
1168 | thread, so notifications are emitted in a sensible order. */ | |
1169 | if (!in_inferior_list (ptid_get_pid (currthread))) | |
1170 | add_inferior (ptid_get_pid (currthread)); | |
1171 | ||
82f73884 PA |
1172 | /* This is really a new thread. Add it. */ |
1173 | add_thread (currthread); | |
c0a2216e | 1174 | } |
c906108c SS |
1175 | } |
1176 | ||
74531fed PA |
1177 | /* Call this function as a result of |
1178 | 1) A halt indication (T packet) containing a thread id | |
1179 | 2) A direct query of currthread | |
1180 | 3) Successful execution of set thread | |
1181 | */ | |
1182 | ||
1183 | static void | |
1184 | record_currthread (ptid_t currthread) | |
1185 | { | |
1186 | general_thread = currthread; | |
1187 | ||
1188 | if (ptid_equal (currthread, minus_one_ptid)) | |
1189 | /* We're just invalidating the local thread mirror. */ | |
1190 | return; | |
1191 | ||
1192 | notice_new_inferiors (currthread); | |
1193 | } | |
1194 | ||
89be2091 DJ |
1195 | static char *last_pass_packet; |
1196 | ||
1197 | /* If 'QPassSignals' is supported, tell the remote stub what signals | |
1198 | it can simply pass through to the inferior without reporting. */ | |
1199 | ||
1200 | static void | |
1201 | remote_pass_signals (void) | |
1202 | { | |
1203 | if (remote_protocol_packets[PACKET_QPassSignals].support != PACKET_DISABLE) | |
1204 | { | |
1205 | char *pass_packet, *p; | |
1206 | int numsigs = (int) TARGET_SIGNAL_LAST; | |
1207 | int count = 0, i; | |
1208 | ||
1209 | gdb_assert (numsigs < 256); | |
1210 | for (i = 0; i < numsigs; i++) | |
1211 | { | |
1212 | if (signal_stop_state (i) == 0 | |
1213 | && signal_print_state (i) == 0 | |
1214 | && signal_pass_state (i) == 1) | |
1215 | count++; | |
1216 | } | |
1217 | pass_packet = xmalloc (count * 3 + strlen ("QPassSignals:") + 1); | |
1218 | strcpy (pass_packet, "QPassSignals:"); | |
1219 | p = pass_packet + strlen (pass_packet); | |
1220 | for (i = 0; i < numsigs; i++) | |
1221 | { | |
1222 | if (signal_stop_state (i) == 0 | |
1223 | && signal_print_state (i) == 0 | |
1224 | && signal_pass_state (i) == 1) | |
1225 | { | |
1226 | if (i >= 16) | |
1227 | *p++ = tohex (i >> 4); | |
1228 | *p++ = tohex (i & 15); | |
1229 | if (count) | |
1230 | *p++ = ';'; | |
1231 | else | |
1232 | break; | |
1233 | count--; | |
1234 | } | |
1235 | } | |
1236 | *p = 0; | |
1237 | if (!last_pass_packet || strcmp (last_pass_packet, pass_packet)) | |
1238 | { | |
1239 | struct remote_state *rs = get_remote_state (); | |
1240 | char *buf = rs->buf; | |
1241 | ||
1242 | putpkt (pass_packet); | |
1243 | getpkt (&rs->buf, &rs->buf_size, 0); | |
1244 | packet_ok (buf, &remote_protocol_packets[PACKET_QPassSignals]); | |
1245 | if (last_pass_packet) | |
1246 | xfree (last_pass_packet); | |
1247 | last_pass_packet = pass_packet; | |
1248 | } | |
1249 | else | |
1250 | xfree (pass_packet); | |
1251 | } | |
1252 | } | |
1253 | ||
79d7f229 PA |
1254 | /* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is |
1255 | MINUS_ONE_PTID, set the thread to -1, so the stub returns the | |
1256 | thread. If GEN is set, set the general thread, if not, then set | |
1257 | the step/continue thread. */ | |
c906108c | 1258 | static void |
79d7f229 | 1259 | set_thread (struct ptid ptid, int gen) |
c906108c | 1260 | { |
d01949b6 | 1261 | struct remote_state *rs = get_remote_state (); |
79d7f229 | 1262 | ptid_t state = gen ? general_thread : continue_thread; |
6d820c5c | 1263 | char *buf = rs->buf; |
79d7f229 | 1264 | char *endbuf = rs->buf + get_remote_packet_size (); |
c906108c | 1265 | |
79d7f229 | 1266 | if (ptid_equal (state, ptid)) |
c906108c SS |
1267 | return; |
1268 | ||
79d7f229 PA |
1269 | *buf++ = 'H'; |
1270 | *buf++ = gen ? 'g' : 'c'; | |
1271 | if (ptid_equal (ptid, magic_null_ptid)) | |
1272 | xsnprintf (buf, endbuf - buf, "0"); | |
1273 | else if (ptid_equal (ptid, any_thread_ptid)) | |
1274 | xsnprintf (buf, endbuf - buf, "0"); | |
1275 | else if (ptid_equal (ptid, minus_one_ptid)) | |
1276 | xsnprintf (buf, endbuf - buf, "-1"); | |
1277 | else | |
82f73884 | 1278 | write_ptid (buf, endbuf, ptid); |
79d7f229 | 1279 | putpkt (rs->buf); |
6d820c5c | 1280 | getpkt (&rs->buf, &rs->buf_size, 0); |
c906108c | 1281 | if (gen) |
79d7f229 | 1282 | general_thread = ptid; |
c906108c | 1283 | else |
79d7f229 | 1284 | continue_thread = ptid; |
c906108c | 1285 | } |
79d7f229 PA |
1286 | |
1287 | static void | |
1288 | set_general_thread (struct ptid ptid) | |
1289 | { | |
1290 | set_thread (ptid, 1); | |
1291 | } | |
1292 | ||
1293 | static void | |
1294 | set_continue_thread (struct ptid ptid) | |
1295 | { | |
1296 | set_thread (ptid, 0); | |
1297 | } | |
1298 | ||
3c9c4b83 PA |
1299 | /* Change the remote current process. Which thread within the process |
1300 | ends up selected isn't important, as long as it is the same process | |
1301 | as what INFERIOR_PTID points to. | |
1302 | ||
1303 | This comes from that fact that there is no explicit notion of | |
1304 | "selected process" in the protocol. The selected process for | |
1305 | general operations is the process the selected general thread | |
1306 | belongs to. */ | |
1307 | ||
1308 | static void | |
1309 | set_general_process (void) | |
1310 | { | |
1311 | struct remote_state *rs = get_remote_state (); | |
1312 | ||
1313 | /* If the remote can't handle multiple processes, don't bother. */ | |
1314 | if (!remote_multi_process_p (rs)) | |
1315 | return; | |
1316 | ||
1317 | /* We only need to change the remote current thread if it's pointing | |
1318 | at some other process. */ | |
1319 | if (ptid_get_pid (general_thread) != ptid_get_pid (inferior_ptid)) | |
1320 | set_general_thread (inferior_ptid); | |
1321 | } | |
1322 | ||
c906108c | 1323 | \f |
79d7f229 PA |
1324 | /* Return nonzero if the thread PTID is still alive on the remote |
1325 | system. */ | |
c906108c SS |
1326 | |
1327 | static int | |
39f77062 | 1328 | remote_thread_alive (ptid_t ptid) |
c906108c | 1329 | { |
6d820c5c | 1330 | struct remote_state *rs = get_remote_state (); |
79d7f229 | 1331 | int tid = ptid_get_tid (ptid); |
82f73884 | 1332 | char *p, *endp; |
c906108c | 1333 | |
c0a2216e PA |
1334 | if (ptid_equal (ptid, magic_null_ptid)) |
1335 | /* The main thread is always alive. */ | |
1336 | return 1; | |
1337 | ||
1338 | if (ptid_get_pid (ptid) != 0 && ptid_get_tid (ptid) == 0) | |
1339 | /* The main thread is always alive. This can happen after a | |
1340 | vAttach, if the remote side doesn't support | |
1341 | multi-threading. */ | |
1342 | return 1; | |
1343 | ||
82f73884 PA |
1344 | p = rs->buf; |
1345 | endp = rs->buf + get_remote_packet_size (); | |
1346 | ||
1347 | *p++ = 'T'; | |
1348 | write_ptid (p, endp, ptid); | |
1349 | ||
2e9f7625 | 1350 | putpkt (rs->buf); |
6d820c5c | 1351 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 1352 | return (rs->buf[0] == 'O' && rs->buf[1] == 'K'); |
c906108c SS |
1353 | } |
1354 | ||
1355 | /* About these extended threadlist and threadinfo packets. They are | |
1356 | variable length packets but, the fields within them are often fixed | |
1357 | length. They are redundent enough to send over UDP as is the | |
1358 | remote protocol in general. There is a matching unit test module | |
1359 | in libstub. */ | |
1360 | ||
cce74817 JM |
1361 | #define OPAQUETHREADBYTES 8 |
1362 | ||
1363 | /* a 64 bit opaque identifier */ | |
1364 | typedef unsigned char threadref[OPAQUETHREADBYTES]; | |
1365 | ||
23860348 MS |
1366 | /* WARNING: This threadref data structure comes from the remote O.S., |
1367 | libstub protocol encoding, and remote.c. it is not particularly | |
1368 | changable. */ | |
cce74817 JM |
1369 | |
1370 | /* Right now, the internal structure is int. We want it to be bigger. | |
1371 | Plan to fix this. | |
c5aa993b | 1372 | */ |
cce74817 | 1373 | |
23860348 | 1374 | typedef int gdb_threadref; /* Internal GDB thread reference. */ |
cce74817 | 1375 | |
9d1f7ab2 | 1376 | /* gdb_ext_thread_info is an internal GDB data structure which is |
cfde0993 | 1377 | equivalent to the reply of the remote threadinfo packet. */ |
cce74817 JM |
1378 | |
1379 | struct gdb_ext_thread_info | |
c5aa993b | 1380 | { |
23860348 | 1381 | threadref threadid; /* External form of thread reference. */ |
2bc416ba | 1382 | int active; /* Has state interesting to GDB? |
23860348 | 1383 | regs, stack. */ |
2bc416ba | 1384 | char display[256]; /* Brief state display, name, |
cedea757 | 1385 | blocked/suspended. */ |
23860348 | 1386 | char shortname[32]; /* To be used to name threads. */ |
2bc416ba | 1387 | char more_display[256]; /* Long info, statistics, queue depth, |
23860348 | 1388 | whatever. */ |
c5aa993b | 1389 | }; |
cce74817 JM |
1390 | |
1391 | /* The volume of remote transfers can be limited by submitting | |
1392 | a mask containing bits specifying the desired information. | |
1393 | Use a union of these values as the 'selection' parameter to | |
1394 | get_thread_info. FIXME: Make these TAG names more thread specific. | |
c5aa993b | 1395 | */ |
cce74817 JM |
1396 | |
1397 | #define TAG_THREADID 1 | |
1398 | #define TAG_EXISTS 2 | |
1399 | #define TAG_DISPLAY 4 | |
1400 | #define TAG_THREADNAME 8 | |
c5aa993b | 1401 | #define TAG_MOREDISPLAY 16 |
cce74817 | 1402 | |
23860348 | 1403 | #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2) |
c906108c | 1404 | |
b2dd6311 | 1405 | char *unpack_varlen_hex (char *buff, ULONGEST *result); |
cce74817 | 1406 | |
a14ed312 | 1407 | static char *unpack_nibble (char *buf, int *val); |
cce74817 | 1408 | |
a14ed312 | 1409 | static char *pack_nibble (char *buf, int nibble); |
cce74817 | 1410 | |
23860348 | 1411 | static char *pack_hex_byte (char *pkt, int /* unsigned char */ byte); |
cce74817 | 1412 | |
a14ed312 | 1413 | static char *unpack_byte (char *buf, int *value); |
cce74817 | 1414 | |
a14ed312 | 1415 | static char *pack_int (char *buf, int value); |
cce74817 | 1416 | |
a14ed312 | 1417 | static char *unpack_int (char *buf, int *value); |
cce74817 | 1418 | |
a14ed312 | 1419 | static char *unpack_string (char *src, char *dest, int length); |
cce74817 | 1420 | |
23860348 | 1421 | static char *pack_threadid (char *pkt, threadref *id); |
cce74817 | 1422 | |
23860348 | 1423 | static char *unpack_threadid (char *inbuf, threadref *id); |
cce74817 | 1424 | |
23860348 | 1425 | void int_to_threadref (threadref *id, int value); |
cce74817 | 1426 | |
23860348 | 1427 | static int threadref_to_int (threadref *ref); |
cce74817 | 1428 | |
23860348 | 1429 | static void copy_threadref (threadref *dest, threadref *src); |
cce74817 | 1430 | |
23860348 | 1431 | static int threadmatch (threadref *dest, threadref *src); |
cce74817 | 1432 | |
2bc416ba | 1433 | static char *pack_threadinfo_request (char *pkt, int mode, |
23860348 | 1434 | threadref *id); |
cce74817 | 1435 | |
a14ed312 | 1436 | static int remote_unpack_thread_info_response (char *pkt, |
23860348 | 1437 | threadref *expectedref, |
a14ed312 KB |
1438 | struct gdb_ext_thread_info |
1439 | *info); | |
cce74817 JM |
1440 | |
1441 | ||
2bc416ba | 1442 | static int remote_get_threadinfo (threadref *threadid, |
23860348 | 1443 | int fieldset, /*TAG mask */ |
a14ed312 | 1444 | struct gdb_ext_thread_info *info); |
cce74817 | 1445 | |
a14ed312 KB |
1446 | static char *pack_threadlist_request (char *pkt, int startflag, |
1447 | int threadcount, | |
23860348 | 1448 | threadref *nextthread); |
cce74817 | 1449 | |
a14ed312 KB |
1450 | static int parse_threadlist_response (char *pkt, |
1451 | int result_limit, | |
23860348 | 1452 | threadref *original_echo, |
2bc416ba | 1453 | threadref *resultlist, |
23860348 | 1454 | int *doneflag); |
cce74817 | 1455 | |
a14ed312 | 1456 | static int remote_get_threadlist (int startflag, |
23860348 | 1457 | threadref *nextthread, |
a14ed312 KB |
1458 | int result_limit, |
1459 | int *done, | |
2bc416ba | 1460 | int *result_count, |
23860348 | 1461 | threadref *threadlist); |
cce74817 | 1462 | |
23860348 | 1463 | typedef int (*rmt_thread_action) (threadref *ref, void *context); |
cce74817 | 1464 | |
a14ed312 KB |
1465 | static int remote_threadlist_iterator (rmt_thread_action stepfunction, |
1466 | void *context, int looplimit); | |
cce74817 | 1467 | |
23860348 | 1468 | static int remote_newthread_step (threadref *ref, void *context); |
cce74817 | 1469 | |
82f73884 PA |
1470 | |
1471 | /* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the | |
1472 | buffer we're allowed to write to. Returns | |
1473 | BUF+CHARACTERS_WRITTEN. */ | |
1474 | ||
1475 | static char * | |
1476 | write_ptid (char *buf, const char *endbuf, ptid_t ptid) | |
1477 | { | |
1478 | int pid, tid; | |
1479 | struct remote_state *rs = get_remote_state (); | |
1480 | ||
1481 | if (remote_multi_process_p (rs)) | |
1482 | { | |
1483 | pid = ptid_get_pid (ptid); | |
1484 | if (pid < 0) | |
1485 | buf += xsnprintf (buf, endbuf - buf, "p-%x.", -pid); | |
1486 | else | |
1487 | buf += xsnprintf (buf, endbuf - buf, "p%x.", pid); | |
1488 | } | |
1489 | tid = ptid_get_tid (ptid); | |
1490 | if (tid < 0) | |
1491 | buf += xsnprintf (buf, endbuf - buf, "-%x", -tid); | |
1492 | else | |
1493 | buf += xsnprintf (buf, endbuf - buf, "%x", tid); | |
1494 | ||
1495 | return buf; | |
1496 | } | |
1497 | ||
1498 | /* Extract a PTID from BUF. If non-null, OBUF is set to the to one | |
1499 | passed the last parsed char. Returns null_ptid on error. */ | |
1500 | ||
1501 | static ptid_t | |
1502 | read_ptid (char *buf, char **obuf) | |
1503 | { | |
1504 | char *p = buf; | |
1505 | char *pp; | |
1506 | ULONGEST pid = 0, tid = 0; | |
1507 | ptid_t ptid; | |
1508 | ||
1509 | if (*p == 'p') | |
1510 | { | |
1511 | /* Multi-process ptid. */ | |
1512 | pp = unpack_varlen_hex (p + 1, &pid); | |
1513 | if (*pp != '.') | |
1514 | error (_("invalid remote ptid: %s\n"), p); | |
1515 | ||
1516 | p = pp; | |
1517 | pp = unpack_varlen_hex (p + 1, &tid); | |
1518 | if (obuf) | |
1519 | *obuf = pp; | |
1520 | return ptid_build (pid, 0, tid); | |
1521 | } | |
1522 | ||
1523 | /* No multi-process. Just a tid. */ | |
1524 | pp = unpack_varlen_hex (p, &tid); | |
1525 | ||
1526 | /* Since the stub is not sending a process id, then default to | |
1527 | what's in inferior_ptid. */ | |
1528 | pid = ptid_get_pid (inferior_ptid); | |
1529 | ||
1530 | if (obuf) | |
1531 | *obuf = pp; | |
1532 | return ptid_build (pid, 0, tid); | |
1533 | } | |
1534 | ||
23860348 | 1535 | /* Encode 64 bits in 16 chars of hex. */ |
c906108c SS |
1536 | |
1537 | static const char hexchars[] = "0123456789abcdef"; | |
1538 | ||
1539 | static int | |
fba45db2 | 1540 | ishex (int ch, int *val) |
c906108c SS |
1541 | { |
1542 | if ((ch >= 'a') && (ch <= 'f')) | |
1543 | { | |
1544 | *val = ch - 'a' + 10; | |
1545 | return 1; | |
1546 | } | |
1547 | if ((ch >= 'A') && (ch <= 'F')) | |
1548 | { | |
1549 | *val = ch - 'A' + 10; | |
1550 | return 1; | |
1551 | } | |
1552 | if ((ch >= '0') && (ch <= '9')) | |
1553 | { | |
1554 | *val = ch - '0'; | |
1555 | return 1; | |
1556 | } | |
1557 | return 0; | |
1558 | } | |
1559 | ||
1560 | static int | |
fba45db2 | 1561 | stubhex (int ch) |
c906108c SS |
1562 | { |
1563 | if (ch >= 'a' && ch <= 'f') | |
1564 | return ch - 'a' + 10; | |
1565 | if (ch >= '0' && ch <= '9') | |
1566 | return ch - '0'; | |
1567 | if (ch >= 'A' && ch <= 'F') | |
1568 | return ch - 'A' + 10; | |
1569 | return -1; | |
1570 | } | |
1571 | ||
1572 | static int | |
fba45db2 | 1573 | stub_unpack_int (char *buff, int fieldlength) |
c906108c SS |
1574 | { |
1575 | int nibble; | |
1576 | int retval = 0; | |
1577 | ||
1578 | while (fieldlength) | |
1579 | { | |
1580 | nibble = stubhex (*buff++); | |
1581 | retval |= nibble; | |
1582 | fieldlength--; | |
1583 | if (fieldlength) | |
1584 | retval = retval << 4; | |
1585 | } | |
1586 | return retval; | |
1587 | } | |
1588 | ||
1589 | char * | |
fba45db2 | 1590 | unpack_varlen_hex (char *buff, /* packet to parse */ |
b2dd6311 | 1591 | ULONGEST *result) |
c906108c SS |
1592 | { |
1593 | int nibble; | |
d49c44d5 | 1594 | ULONGEST retval = 0; |
c906108c SS |
1595 | |
1596 | while (ishex (*buff, &nibble)) | |
1597 | { | |
1598 | buff++; | |
1599 | retval = retval << 4; | |
1600 | retval |= nibble & 0x0f; | |
1601 | } | |
1602 | *result = retval; | |
1603 | return buff; | |
1604 | } | |
1605 | ||
1606 | static char * | |
fba45db2 | 1607 | unpack_nibble (char *buf, int *val) |
c906108c | 1608 | { |
b7589f7d | 1609 | *val = fromhex (*buf++); |
c906108c SS |
1610 | return buf; |
1611 | } | |
1612 | ||
1613 | static char * | |
fba45db2 | 1614 | pack_nibble (char *buf, int nibble) |
c906108c SS |
1615 | { |
1616 | *buf++ = hexchars[(nibble & 0x0f)]; | |
1617 | return buf; | |
1618 | } | |
1619 | ||
1620 | static char * | |
fba45db2 | 1621 | pack_hex_byte (char *pkt, int byte) |
c906108c SS |
1622 | { |
1623 | *pkt++ = hexchars[(byte >> 4) & 0xf]; | |
1624 | *pkt++ = hexchars[(byte & 0xf)]; | |
1625 | return pkt; | |
1626 | } | |
1627 | ||
1628 | static char * | |
fba45db2 | 1629 | unpack_byte (char *buf, int *value) |
c906108c SS |
1630 | { |
1631 | *value = stub_unpack_int (buf, 2); | |
1632 | return buf + 2; | |
1633 | } | |
1634 | ||
1635 | static char * | |
fba45db2 | 1636 | pack_int (char *buf, int value) |
c906108c SS |
1637 | { |
1638 | buf = pack_hex_byte (buf, (value >> 24) & 0xff); | |
1639 | buf = pack_hex_byte (buf, (value >> 16) & 0xff); | |
1640 | buf = pack_hex_byte (buf, (value >> 8) & 0x0ff); | |
1641 | buf = pack_hex_byte (buf, (value & 0xff)); | |
1642 | return buf; | |
1643 | } | |
1644 | ||
1645 | static char * | |
fba45db2 | 1646 | unpack_int (char *buf, int *value) |
c906108c SS |
1647 | { |
1648 | *value = stub_unpack_int (buf, 8); | |
1649 | return buf + 8; | |
1650 | } | |
1651 | ||
23860348 | 1652 | #if 0 /* Currently unused, uncomment when needed. */ |
a14ed312 | 1653 | static char *pack_string (char *pkt, char *string); |
c906108c SS |
1654 | |
1655 | static char * | |
fba45db2 | 1656 | pack_string (char *pkt, char *string) |
c906108c SS |
1657 | { |
1658 | char ch; | |
1659 | int len; | |
1660 | ||
1661 | len = strlen (string); | |
1662 | if (len > 200) | |
23860348 | 1663 | len = 200; /* Bigger than most GDB packets, junk??? */ |
c906108c SS |
1664 | pkt = pack_hex_byte (pkt, len); |
1665 | while (len-- > 0) | |
1666 | { | |
1667 | ch = *string++; | |
1668 | if ((ch == '\0') || (ch == '#')) | |
23860348 | 1669 | ch = '*'; /* Protect encapsulation. */ |
c906108c SS |
1670 | *pkt++ = ch; |
1671 | } | |
1672 | return pkt; | |
1673 | } | |
1674 | #endif /* 0 (unused) */ | |
1675 | ||
1676 | static char * | |
fba45db2 | 1677 | unpack_string (char *src, char *dest, int length) |
c906108c SS |
1678 | { |
1679 | while (length--) | |
1680 | *dest++ = *src++; | |
1681 | *dest = '\0'; | |
1682 | return src; | |
1683 | } | |
1684 | ||
1685 | static char * | |
fba45db2 | 1686 | pack_threadid (char *pkt, threadref *id) |
c906108c SS |
1687 | { |
1688 | char *limit; | |
1689 | unsigned char *altid; | |
1690 | ||
1691 | altid = (unsigned char *) id; | |
1692 | limit = pkt + BUF_THREAD_ID_SIZE; | |
1693 | while (pkt < limit) | |
1694 | pkt = pack_hex_byte (pkt, *altid++); | |
1695 | return pkt; | |
1696 | } | |
1697 | ||
1698 | ||
1699 | static char * | |
fba45db2 | 1700 | unpack_threadid (char *inbuf, threadref *id) |
c906108c SS |
1701 | { |
1702 | char *altref; | |
1703 | char *limit = inbuf + BUF_THREAD_ID_SIZE; | |
1704 | int x, y; | |
1705 | ||
1706 | altref = (char *) id; | |
1707 | ||
1708 | while (inbuf < limit) | |
1709 | { | |
1710 | x = stubhex (*inbuf++); | |
1711 | y = stubhex (*inbuf++); | |
1712 | *altref++ = (x << 4) | y; | |
1713 | } | |
1714 | return inbuf; | |
1715 | } | |
1716 | ||
1717 | /* Externally, threadrefs are 64 bits but internally, they are still | |
1718 | ints. This is due to a mismatch of specifications. We would like | |
1719 | to use 64bit thread references internally. This is an adapter | |
1720 | function. */ | |
1721 | ||
1722 | void | |
fba45db2 | 1723 | int_to_threadref (threadref *id, int value) |
c906108c SS |
1724 | { |
1725 | unsigned char *scan; | |
1726 | ||
1727 | scan = (unsigned char *) id; | |
1728 | { | |
1729 | int i = 4; | |
1730 | while (i--) | |
1731 | *scan++ = 0; | |
1732 | } | |
1733 | *scan++ = (value >> 24) & 0xff; | |
1734 | *scan++ = (value >> 16) & 0xff; | |
1735 | *scan++ = (value >> 8) & 0xff; | |
1736 | *scan++ = (value & 0xff); | |
1737 | } | |
1738 | ||
1739 | static int | |
fba45db2 | 1740 | threadref_to_int (threadref *ref) |
c906108c SS |
1741 | { |
1742 | int i, value = 0; | |
1743 | unsigned char *scan; | |
1744 | ||
cfd77fa1 | 1745 | scan = *ref; |
c906108c SS |
1746 | scan += 4; |
1747 | i = 4; | |
1748 | while (i-- > 0) | |
1749 | value = (value << 8) | ((*scan++) & 0xff); | |
1750 | return value; | |
1751 | } | |
1752 | ||
1753 | static void | |
fba45db2 | 1754 | copy_threadref (threadref *dest, threadref *src) |
c906108c SS |
1755 | { |
1756 | int i; | |
1757 | unsigned char *csrc, *cdest; | |
1758 | ||
1759 | csrc = (unsigned char *) src; | |
1760 | cdest = (unsigned char *) dest; | |
1761 | i = 8; | |
1762 | while (i--) | |
1763 | *cdest++ = *csrc++; | |
1764 | } | |
1765 | ||
1766 | static int | |
fba45db2 | 1767 | threadmatch (threadref *dest, threadref *src) |
c906108c | 1768 | { |
23860348 | 1769 | /* Things are broken right now, so just assume we got a match. */ |
c906108c SS |
1770 | #if 0 |
1771 | unsigned char *srcp, *destp; | |
1772 | int i, result; | |
1773 | srcp = (char *) src; | |
1774 | destp = (char *) dest; | |
1775 | ||
1776 | result = 1; | |
1777 | while (i-- > 0) | |
1778 | result &= (*srcp++ == *destp++) ? 1 : 0; | |
1779 | return result; | |
1780 | #endif | |
1781 | return 1; | |
1782 | } | |
1783 | ||
1784 | /* | |
c5aa993b JM |
1785 | threadid:1, # always request threadid |
1786 | context_exists:2, | |
1787 | display:4, | |
1788 | unique_name:8, | |
1789 | more_display:16 | |
1790 | */ | |
c906108c SS |
1791 | |
1792 | /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */ | |
1793 | ||
1794 | static char * | |
fba45db2 | 1795 | pack_threadinfo_request (char *pkt, int mode, threadref *id) |
c906108c | 1796 | { |
23860348 MS |
1797 | *pkt++ = 'q'; /* Info Query */ |
1798 | *pkt++ = 'P'; /* process or thread info */ | |
1799 | pkt = pack_int (pkt, mode); /* mode */ | |
c906108c | 1800 | pkt = pack_threadid (pkt, id); /* threadid */ |
23860348 | 1801 | *pkt = '\0'; /* terminate */ |
c906108c SS |
1802 | return pkt; |
1803 | } | |
1804 | ||
23860348 | 1805 | /* These values tag the fields in a thread info response packet. */ |
c906108c | 1806 | /* Tagging the fields allows us to request specific fields and to |
23860348 | 1807 | add more fields as time goes by. */ |
c906108c | 1808 | |
23860348 | 1809 | #define TAG_THREADID 1 /* Echo the thread identifier. */ |
c5aa993b | 1810 | #define TAG_EXISTS 2 /* Is this process defined enough to |
23860348 | 1811 | fetch registers and its stack? */ |
c5aa993b | 1812 | #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */ |
23860348 | 1813 | #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */ |
802188a7 | 1814 | #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about |
23860348 | 1815 | the process. */ |
c906108c SS |
1816 | |
1817 | static int | |
fba45db2 KB |
1818 | remote_unpack_thread_info_response (char *pkt, threadref *expectedref, |
1819 | struct gdb_ext_thread_info *info) | |
c906108c | 1820 | { |
d01949b6 | 1821 | struct remote_state *rs = get_remote_state (); |
c906108c | 1822 | int mask, length; |
cfd77fa1 | 1823 | int tag; |
c906108c | 1824 | threadref ref; |
6d820c5c | 1825 | char *limit = pkt + rs->buf_size; /* Plausible parsing limit. */ |
c906108c SS |
1826 | int retval = 1; |
1827 | ||
23860348 | 1828 | /* info->threadid = 0; FIXME: implement zero_threadref. */ |
c906108c SS |
1829 | info->active = 0; |
1830 | info->display[0] = '\0'; | |
1831 | info->shortname[0] = '\0'; | |
1832 | info->more_display[0] = '\0'; | |
1833 | ||
23860348 MS |
1834 | /* Assume the characters indicating the packet type have been |
1835 | stripped. */ | |
c906108c SS |
1836 | pkt = unpack_int (pkt, &mask); /* arg mask */ |
1837 | pkt = unpack_threadid (pkt, &ref); | |
1838 | ||
1839 | if (mask == 0) | |
8a3fe4f8 | 1840 | warning (_("Incomplete response to threadinfo request.")); |
c906108c | 1841 | if (!threadmatch (&ref, expectedref)) |
23860348 | 1842 | { /* This is an answer to a different request. */ |
8a3fe4f8 | 1843 | warning (_("ERROR RMT Thread info mismatch.")); |
c906108c SS |
1844 | return 0; |
1845 | } | |
1846 | copy_threadref (&info->threadid, &ref); | |
1847 | ||
23860348 | 1848 | /* Loop on tagged fields , try to bail if somthing goes wrong. */ |
c906108c | 1849 | |
23860348 MS |
1850 | /* Packets are terminated with nulls. */ |
1851 | while ((pkt < limit) && mask && *pkt) | |
c906108c SS |
1852 | { |
1853 | pkt = unpack_int (pkt, &tag); /* tag */ | |
23860348 MS |
1854 | pkt = unpack_byte (pkt, &length); /* length */ |
1855 | if (!(tag & mask)) /* Tags out of synch with mask. */ | |
c906108c | 1856 | { |
8a3fe4f8 | 1857 | warning (_("ERROR RMT: threadinfo tag mismatch.")); |
c906108c SS |
1858 | retval = 0; |
1859 | break; | |
1860 | } | |
1861 | if (tag == TAG_THREADID) | |
1862 | { | |
1863 | if (length != 16) | |
1864 | { | |
8a3fe4f8 | 1865 | warning (_("ERROR RMT: length of threadid is not 16.")); |
c906108c SS |
1866 | retval = 0; |
1867 | break; | |
1868 | } | |
1869 | pkt = unpack_threadid (pkt, &ref); | |
1870 | mask = mask & ~TAG_THREADID; | |
1871 | continue; | |
1872 | } | |
1873 | if (tag == TAG_EXISTS) | |
1874 | { | |
1875 | info->active = stub_unpack_int (pkt, length); | |
1876 | pkt += length; | |
1877 | mask = mask & ~(TAG_EXISTS); | |
1878 | if (length > 8) | |
1879 | { | |
8a3fe4f8 | 1880 | warning (_("ERROR RMT: 'exists' length too long.")); |
c906108c SS |
1881 | retval = 0; |
1882 | break; | |
1883 | } | |
1884 | continue; | |
1885 | } | |
1886 | if (tag == TAG_THREADNAME) | |
1887 | { | |
1888 | pkt = unpack_string (pkt, &info->shortname[0], length); | |
1889 | mask = mask & ~TAG_THREADNAME; | |
1890 | continue; | |
1891 | } | |
1892 | if (tag == TAG_DISPLAY) | |
1893 | { | |
1894 | pkt = unpack_string (pkt, &info->display[0], length); | |
1895 | mask = mask & ~TAG_DISPLAY; | |
1896 | continue; | |
1897 | } | |
1898 | if (tag == TAG_MOREDISPLAY) | |
1899 | { | |
1900 | pkt = unpack_string (pkt, &info->more_display[0], length); | |
1901 | mask = mask & ~TAG_MOREDISPLAY; | |
1902 | continue; | |
1903 | } | |
8a3fe4f8 | 1904 | warning (_("ERROR RMT: unknown thread info tag.")); |
23860348 | 1905 | break; /* Not a tag we know about. */ |
c906108c SS |
1906 | } |
1907 | return retval; | |
1908 | } | |
1909 | ||
1910 | static int | |
fba45db2 KB |
1911 | remote_get_threadinfo (threadref *threadid, int fieldset, /* TAG mask */ |
1912 | struct gdb_ext_thread_info *info) | |
c906108c | 1913 | { |
d01949b6 | 1914 | struct remote_state *rs = get_remote_state (); |
c906108c | 1915 | int result; |
c906108c | 1916 | |
2e9f7625 DJ |
1917 | pack_threadinfo_request (rs->buf, fieldset, threadid); |
1918 | putpkt (rs->buf); | |
6d820c5c | 1919 | getpkt (&rs->buf, &rs->buf_size, 0); |
3084dd77 PA |
1920 | |
1921 | if (rs->buf[0] == '\0') | |
1922 | return 0; | |
1923 | ||
2e9f7625 | 1924 | result = remote_unpack_thread_info_response (rs->buf + 2, |
23860348 | 1925 | threadid, info); |
c906108c SS |
1926 | return result; |
1927 | } | |
1928 | ||
c906108c SS |
1929 | /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */ |
1930 | ||
1931 | static char * | |
fba45db2 KB |
1932 | pack_threadlist_request (char *pkt, int startflag, int threadcount, |
1933 | threadref *nextthread) | |
c906108c SS |
1934 | { |
1935 | *pkt++ = 'q'; /* info query packet */ | |
1936 | *pkt++ = 'L'; /* Process LIST or threadLIST request */ | |
23860348 | 1937 | pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */ |
c906108c SS |
1938 | pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */ |
1939 | pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */ | |
1940 | *pkt = '\0'; | |
1941 | return pkt; | |
1942 | } | |
1943 | ||
1944 | /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */ | |
1945 | ||
1946 | static int | |
fba45db2 KB |
1947 | parse_threadlist_response (char *pkt, int result_limit, |
1948 | threadref *original_echo, threadref *resultlist, | |
1949 | int *doneflag) | |
c906108c | 1950 | { |
d01949b6 | 1951 | struct remote_state *rs = get_remote_state (); |
c906108c SS |
1952 | char *limit; |
1953 | int count, resultcount, done; | |
1954 | ||
1955 | resultcount = 0; | |
1956 | /* Assume the 'q' and 'M chars have been stripped. */ | |
6d820c5c | 1957 | limit = pkt + (rs->buf_size - BUF_THREAD_ID_SIZE); |
23860348 | 1958 | /* done parse past here */ |
c906108c SS |
1959 | pkt = unpack_byte (pkt, &count); /* count field */ |
1960 | pkt = unpack_nibble (pkt, &done); | |
1961 | /* The first threadid is the argument threadid. */ | |
1962 | pkt = unpack_threadid (pkt, original_echo); /* should match query packet */ | |
1963 | while ((count-- > 0) && (pkt < limit)) | |
1964 | { | |
1965 | pkt = unpack_threadid (pkt, resultlist++); | |
1966 | if (resultcount++ >= result_limit) | |
1967 | break; | |
1968 | } | |
1969 | if (doneflag) | |
1970 | *doneflag = done; | |
1971 | return resultcount; | |
1972 | } | |
1973 | ||
1974 | static int | |
fba45db2 KB |
1975 | remote_get_threadlist (int startflag, threadref *nextthread, int result_limit, |
1976 | int *done, int *result_count, threadref *threadlist) | |
c906108c | 1977 | { |
d01949b6 | 1978 | struct remote_state *rs = get_remote_state (); |
c906108c | 1979 | static threadref echo_nextthread; |
c906108c SS |
1980 | int result = 1; |
1981 | ||
23860348 | 1982 | /* Trancate result limit to be smaller than the packet size. */ |
ea9c271d DJ |
1983 | if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10) >= get_remote_packet_size ()) |
1984 | result_limit = (get_remote_packet_size () / BUF_THREAD_ID_SIZE) - 2; | |
c906108c | 1985 | |
6d820c5c DJ |
1986 | pack_threadlist_request (rs->buf, startflag, result_limit, nextthread); |
1987 | putpkt (rs->buf); | |
1988 | getpkt (&rs->buf, &rs->buf_size, 0); | |
c906108c | 1989 | |
d8f2712d VP |
1990 | if (*rs->buf == '\0') |
1991 | *result_count = 0; | |
1992 | else | |
1993 | *result_count = | |
1994 | parse_threadlist_response (rs->buf + 2, result_limit, &echo_nextthread, | |
1995 | threadlist, done); | |
c906108c SS |
1996 | |
1997 | if (!threadmatch (&echo_nextthread, nextthread)) | |
1998 | { | |
23860348 MS |
1999 | /* FIXME: This is a good reason to drop the packet. */ |
2000 | /* Possably, there is a duplicate response. */ | |
c906108c SS |
2001 | /* Possabilities : |
2002 | retransmit immediatly - race conditions | |
2003 | retransmit after timeout - yes | |
2004 | exit | |
2005 | wait for packet, then exit | |
2006 | */ | |
8a3fe4f8 | 2007 | warning (_("HMM: threadlist did not echo arg thread, dropping it.")); |
23860348 | 2008 | return 0; /* I choose simply exiting. */ |
c906108c SS |
2009 | } |
2010 | if (*result_count <= 0) | |
2011 | { | |
2012 | if (*done != 1) | |
2013 | { | |
8a3fe4f8 | 2014 | warning (_("RMT ERROR : failed to get remote thread list.")); |
c906108c SS |
2015 | result = 0; |
2016 | } | |
2017 | return result; /* break; */ | |
2018 | } | |
2019 | if (*result_count > result_limit) | |
2020 | { | |
2021 | *result_count = 0; | |
8a3fe4f8 | 2022 | warning (_("RMT ERROR: threadlist response longer than requested.")); |
c906108c SS |
2023 | return 0; |
2024 | } | |
2025 | return result; | |
2026 | } | |
2027 | ||
23860348 MS |
2028 | /* This is the interface between remote and threads, remotes upper |
2029 | interface. */ | |
c906108c SS |
2030 | |
2031 | /* remote_find_new_threads retrieves the thread list and for each | |
2032 | thread in the list, looks up the thread in GDB's internal list, | |
79d7f229 | 2033 | adding the thread if it does not already exist. This involves |
c906108c SS |
2034 | getting partial thread lists from the remote target so, polling the |
2035 | quit_flag is required. */ | |
2036 | ||
2037 | ||
23860348 | 2038 | /* About this many threadisds fit in a packet. */ |
c906108c SS |
2039 | |
2040 | #define MAXTHREADLISTRESULTS 32 | |
2041 | ||
2042 | static int | |
fba45db2 KB |
2043 | remote_threadlist_iterator (rmt_thread_action stepfunction, void *context, |
2044 | int looplimit) | |
c906108c SS |
2045 | { |
2046 | int done, i, result_count; | |
2047 | int startflag = 1; | |
2048 | int result = 1; | |
2049 | int loopcount = 0; | |
2050 | static threadref nextthread; | |
2051 | static threadref resultthreadlist[MAXTHREADLISTRESULTS]; | |
2052 | ||
2053 | done = 0; | |
2054 | while (!done) | |
2055 | { | |
2056 | if (loopcount++ > looplimit) | |
2057 | { | |
2058 | result = 0; | |
8a3fe4f8 | 2059 | warning (_("Remote fetch threadlist -infinite loop-.")); |
c906108c SS |
2060 | break; |
2061 | } | |
2062 | if (!remote_get_threadlist (startflag, &nextthread, MAXTHREADLISTRESULTS, | |
2063 | &done, &result_count, resultthreadlist)) | |
2064 | { | |
2065 | result = 0; | |
2066 | break; | |
2067 | } | |
23860348 | 2068 | /* Clear for later iterations. */ |
c906108c SS |
2069 | startflag = 0; |
2070 | /* Setup to resume next batch of thread references, set nextthread. */ | |
2071 | if (result_count >= 1) | |
2072 | copy_threadref (&nextthread, &resultthreadlist[result_count - 1]); | |
2073 | i = 0; | |
2074 | while (result_count--) | |
2075 | if (!(result = (*stepfunction) (&resultthreadlist[i++], context))) | |
2076 | break; | |
2077 | } | |
2078 | return result; | |
2079 | } | |
2080 | ||
2081 | static int | |
fba45db2 | 2082 | remote_newthread_step (threadref *ref, void *context) |
c906108c | 2083 | { |
79d7f229 PA |
2084 | int pid = ptid_get_pid (inferior_ptid); |
2085 | ptid_t ptid = ptid_build (pid, 0, threadref_to_int (ref)); | |
39f77062 KB |
2086 | |
2087 | if (!in_thread_list (ptid)) | |
2088 | add_thread (ptid); | |
c906108c SS |
2089 | return 1; /* continue iterator */ |
2090 | } | |
2091 | ||
2092 | #define CRAZY_MAX_THREADS 1000 | |
2093 | ||
39f77062 KB |
2094 | static ptid_t |
2095 | remote_current_thread (ptid_t oldpid) | |
c906108c | 2096 | { |
d01949b6 | 2097 | struct remote_state *rs = get_remote_state (); |
79d7f229 PA |
2098 | char *p = rs->buf; |
2099 | int tid; | |
2100 | int pid; | |
c906108c SS |
2101 | |
2102 | putpkt ("qC"); | |
6d820c5c | 2103 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 2104 | if (rs->buf[0] == 'Q' && rs->buf[1] == 'C') |
82f73884 | 2105 | return read_ptid (&rs->buf[2], NULL); |
c906108c SS |
2106 | else |
2107 | return oldpid; | |
2108 | } | |
2109 | ||
802188a7 RM |
2110 | /* Find new threads for info threads command. |
2111 | * Original version, using John Metzler's thread protocol. | |
9d1f7ab2 | 2112 | */ |
cce74817 JM |
2113 | |
2114 | static void | |
fba45db2 | 2115 | remote_find_new_threads (void) |
c906108c | 2116 | { |
c5aa993b JM |
2117 | remote_threadlist_iterator (remote_newthread_step, 0, |
2118 | CRAZY_MAX_THREADS); | |
c906108c SS |
2119 | } |
2120 | ||
9d1f7ab2 MS |
2121 | /* |
2122 | * Find all threads for info threads command. | |
2123 | * Uses new thread protocol contributed by Cisco. | |
2124 | * Falls back and attempts to use the older method (above) | |
2125 | * if the target doesn't respond to the new method. | |
2126 | */ | |
2127 | ||
0f71a2f6 JM |
2128 | static void |
2129 | remote_threads_info (void) | |
2130 | { | |
d01949b6 | 2131 | struct remote_state *rs = get_remote_state (); |
085dd6e6 | 2132 | char *bufp; |
79d7f229 | 2133 | ptid_t new_thread; |
0f71a2f6 JM |
2134 | |
2135 | if (remote_desc == 0) /* paranoia */ | |
8a3fe4f8 | 2136 | error (_("Command can only be used when connected to the remote target.")); |
0f71a2f6 | 2137 | |
9d1f7ab2 MS |
2138 | if (use_threadinfo_query) |
2139 | { | |
2140 | putpkt ("qfThreadInfo"); | |
6d820c5c | 2141 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 2142 | bufp = rs->buf; |
9d1f7ab2 | 2143 | if (bufp[0] != '\0') /* q packet recognized */ |
802188a7 | 2144 | { |
9d1f7ab2 MS |
2145 | while (*bufp++ == 'm') /* reply contains one or more TID */ |
2146 | { | |
2147 | do | |
2148 | { | |
82f73884 PA |
2149 | new_thread = read_ptid (bufp, &bufp); |
2150 | if (!ptid_equal (new_thread, null_ptid) | |
153ccabd PA |
2151 | && (!in_thread_list (new_thread) |
2152 | || is_exited (new_thread))) | |
82f73884 | 2153 | { |
153ccabd PA |
2154 | /* When connected to a multi-process aware stub, |
2155 | "info threads" may show up threads of | |
2156 | inferiors we didn't know about yet. Add them | |
2157 | now, and before adding any of its child | |
2158 | threads, so notifications are emitted in a | |
2159 | sensible order. */ | |
82f73884 | 2160 | if (!in_inferior_list (ptid_get_pid (new_thread))) |
82f73884 PA |
2161 | add_inferior (ptid_get_pid (new_thread)); |
2162 | ||
2163 | add_thread (new_thread); | |
74531fed PA |
2164 | |
2165 | /* In non-stop mode, we assume new found threads | |
2166 | are running until we proven otherwise with a | |
2167 | stop reply. In all-stop, we can only get | |
2168 | here if all threads are stopped. */ | |
2169 | set_executing (new_thread, non_stop ? 1 : 0); | |
2170 | set_running (new_thread, non_stop ? 1 : 0); | |
82f73884 | 2171 | } |
9d1f7ab2 MS |
2172 | } |
2173 | while (*bufp++ == ','); /* comma-separated list */ | |
2174 | putpkt ("qsThreadInfo"); | |
6d820c5c | 2175 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 2176 | bufp = rs->buf; |
9d1f7ab2 MS |
2177 | } |
2178 | return; /* done */ | |
2179 | } | |
2180 | } | |
2181 | ||
74531fed PA |
2182 | /* Only qfThreadInfo is supported in non-stop mode. */ |
2183 | if (non_stop) | |
2184 | return; | |
2185 | ||
23860348 | 2186 | /* Else fall back to old method based on jmetzler protocol. */ |
9d1f7ab2 MS |
2187 | use_threadinfo_query = 0; |
2188 | remote_find_new_threads (); | |
2189 | return; | |
2190 | } | |
2191 | ||
802188a7 | 2192 | /* |
9d1f7ab2 MS |
2193 | * Collect a descriptive string about the given thread. |
2194 | * The target may say anything it wants to about the thread | |
2195 | * (typically info about its blocked / runnable state, name, etc.). | |
2196 | * This string will appear in the info threads display. | |
802188a7 | 2197 | * |
9d1f7ab2 MS |
2198 | * Optional: targets are not required to implement this function. |
2199 | */ | |
2200 | ||
2201 | static char * | |
2202 | remote_threads_extra_info (struct thread_info *tp) | |
2203 | { | |
d01949b6 | 2204 | struct remote_state *rs = get_remote_state (); |
9d1f7ab2 MS |
2205 | int result; |
2206 | int set; | |
2207 | threadref id; | |
2208 | struct gdb_ext_thread_info threadinfo; | |
23860348 | 2209 | static char display_buf[100]; /* arbitrary... */ |
9d1f7ab2 MS |
2210 | int n = 0; /* position in display_buf */ |
2211 | ||
2212 | if (remote_desc == 0) /* paranoia */ | |
8e65ff28 | 2213 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 2214 | _("remote_threads_extra_info")); |
9d1f7ab2 | 2215 | |
60e569b9 PA |
2216 | if (ptid_equal (tp->ptid, magic_null_ptid) |
2217 | || (ptid_get_pid (tp->ptid) != 0 && ptid_get_tid (tp->ptid) == 0)) | |
2218 | /* This is the main thread which was added by GDB. The remote | |
2219 | server doesn't know about it. */ | |
2220 | return NULL; | |
2221 | ||
9d1f7ab2 MS |
2222 | if (use_threadextra_query) |
2223 | { | |
82f73884 PA |
2224 | char *b = rs->buf; |
2225 | char *endb = rs->buf + get_remote_packet_size (); | |
2226 | ||
2227 | xsnprintf (b, endb - b, "qThreadExtraInfo,"); | |
2228 | b += strlen (b); | |
2229 | write_ptid (b, endb, tp->ptid); | |
2230 | ||
2e9f7625 | 2231 | putpkt (rs->buf); |
6d820c5c | 2232 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 2233 | if (rs->buf[0] != 0) |
9d1f7ab2 | 2234 | { |
2e9f7625 DJ |
2235 | n = min (strlen (rs->buf) / 2, sizeof (display_buf)); |
2236 | result = hex2bin (rs->buf, (gdb_byte *) display_buf, n); | |
30559e10 | 2237 | display_buf [result] = '\0'; |
9d1f7ab2 MS |
2238 | return display_buf; |
2239 | } | |
0f71a2f6 | 2240 | } |
9d1f7ab2 MS |
2241 | |
2242 | /* If the above query fails, fall back to the old method. */ | |
2243 | use_threadextra_query = 0; | |
2244 | set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME | |
2245 | | TAG_MOREDISPLAY | TAG_DISPLAY; | |
79d7f229 | 2246 | int_to_threadref (&id, ptid_get_tid (tp->ptid)); |
9d1f7ab2 MS |
2247 | if (remote_get_threadinfo (&id, set, &threadinfo)) |
2248 | if (threadinfo.active) | |
0f71a2f6 | 2249 | { |
9d1f7ab2 | 2250 | if (*threadinfo.shortname) |
2bc416ba | 2251 | n += xsnprintf (&display_buf[0], sizeof (display_buf) - n, |
ecbc58df | 2252 | " Name: %s,", threadinfo.shortname); |
9d1f7ab2 | 2253 | if (*threadinfo.display) |
2bc416ba | 2254 | n += xsnprintf (&display_buf[n], sizeof (display_buf) - n, |
ecbc58df | 2255 | " State: %s,", threadinfo.display); |
9d1f7ab2 | 2256 | if (*threadinfo.more_display) |
2bc416ba | 2257 | n += xsnprintf (&display_buf[n], sizeof (display_buf) - n, |
ecbc58df | 2258 | " Priority: %s", threadinfo.more_display); |
9d1f7ab2 MS |
2259 | |
2260 | if (n > 0) | |
c5aa993b | 2261 | { |
23860348 | 2262 | /* For purely cosmetic reasons, clear up trailing commas. */ |
9d1f7ab2 MS |
2263 | if (',' == display_buf[n-1]) |
2264 | display_buf[n-1] = ' '; | |
2265 | return display_buf; | |
c5aa993b | 2266 | } |
0f71a2f6 | 2267 | } |
9d1f7ab2 | 2268 | return NULL; |
0f71a2f6 | 2269 | } |
c906108c | 2270 | \f |
c5aa993b | 2271 | |
24b06219 | 2272 | /* Restart the remote side; this is an extended protocol operation. */ |
c906108c SS |
2273 | |
2274 | static void | |
fba45db2 | 2275 | extended_remote_restart (void) |
c906108c | 2276 | { |
d01949b6 | 2277 | struct remote_state *rs = get_remote_state (); |
c906108c SS |
2278 | |
2279 | /* Send the restart command; for reasons I don't understand the | |
2280 | remote side really expects a number after the "R". */ | |
ea9c271d | 2281 | xsnprintf (rs->buf, get_remote_packet_size (), "R%x", 0); |
6d820c5c | 2282 | putpkt (rs->buf); |
c906108c | 2283 | |
ad9a8f3f | 2284 | remote_fileio_reset (); |
c906108c SS |
2285 | } |
2286 | \f | |
2287 | /* Clean up connection to a remote debugger. */ | |
2288 | ||
c906108c | 2289 | static void |
fba45db2 | 2290 | remote_close (int quitting) |
c906108c | 2291 | { |
d3fd5342 PA |
2292 | if (remote_desc == NULL) |
2293 | return; /* already closed */ | |
2294 | ||
2295 | /* Make sure we leave stdin registered in the event loop, and we | |
2296 | don't leave the async SIGINT signal handler installed. */ | |
2297 | remote_terminal_ours (); | |
ce5ce7ed | 2298 | |
d3fd5342 PA |
2299 | serial_close (remote_desc); |
2300 | remote_desc = NULL; | |
ce5ce7ed PA |
2301 | |
2302 | /* We don't have a connection to the remote stub anymore. Get rid | |
2303 | of all the inferiors and their threads we were controlling. */ | |
2304 | discard_all_inferiors (); | |
2305 | ||
74531fed PA |
2306 | /* We're no longer interested in any of these events. */ |
2307 | discard_pending_stop_replies (-1); | |
2308 | ||
2309 | if (remote_async_inferior_event_token) | |
2310 | delete_async_event_handler (&remote_async_inferior_event_token); | |
2311 | if (remote_async_get_pending_events_token) | |
2312 | delete_async_event_handler (&remote_async_get_pending_events_token); | |
2313 | ||
ce5ce7ed | 2314 | generic_mourn_inferior (); |
c906108c SS |
2315 | } |
2316 | ||
23860348 | 2317 | /* Query the remote side for the text, data and bss offsets. */ |
c906108c SS |
2318 | |
2319 | static void | |
fba45db2 | 2320 | get_offsets (void) |
c906108c | 2321 | { |
d01949b6 | 2322 | struct remote_state *rs = get_remote_state (); |
2e9f7625 | 2323 | char *buf; |
085dd6e6 | 2324 | char *ptr; |
31d99776 DJ |
2325 | int lose, num_segments = 0, do_sections, do_segments; |
2326 | CORE_ADDR text_addr, data_addr, bss_addr, segments[2]; | |
c906108c | 2327 | struct section_offsets *offs; |
31d99776 DJ |
2328 | struct symfile_segment_data *data; |
2329 | ||
2330 | if (symfile_objfile == NULL) | |
2331 | return; | |
c906108c SS |
2332 | |
2333 | putpkt ("qOffsets"); | |
6d820c5c | 2334 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 2335 | buf = rs->buf; |
c906108c SS |
2336 | |
2337 | if (buf[0] == '\000') | |
2338 | return; /* Return silently. Stub doesn't support | |
23860348 | 2339 | this command. */ |
c906108c SS |
2340 | if (buf[0] == 'E') |
2341 | { | |
8a3fe4f8 | 2342 | warning (_("Remote failure reply: %s"), buf); |
c906108c SS |
2343 | return; |
2344 | } | |
2345 | ||
2346 | /* Pick up each field in turn. This used to be done with scanf, but | |
2347 | scanf will make trouble if CORE_ADDR size doesn't match | |
2348 | conversion directives correctly. The following code will work | |
2349 | with any size of CORE_ADDR. */ | |
2350 | text_addr = data_addr = bss_addr = 0; | |
2351 | ptr = buf; | |
2352 | lose = 0; | |
2353 | ||
2354 | if (strncmp (ptr, "Text=", 5) == 0) | |
2355 | { | |
2356 | ptr += 5; | |
2357 | /* Don't use strtol, could lose on big values. */ | |
2358 | while (*ptr && *ptr != ';') | |
2359 | text_addr = (text_addr << 4) + fromhex (*ptr++); | |
c906108c | 2360 | |
31d99776 DJ |
2361 | if (strncmp (ptr, ";Data=", 6) == 0) |
2362 | { | |
2363 | ptr += 6; | |
2364 | while (*ptr && *ptr != ';') | |
2365 | data_addr = (data_addr << 4) + fromhex (*ptr++); | |
2366 | } | |
2367 | else | |
2368 | lose = 1; | |
2369 | ||
2370 | if (!lose && strncmp (ptr, ";Bss=", 5) == 0) | |
2371 | { | |
2372 | ptr += 5; | |
2373 | while (*ptr && *ptr != ';') | |
2374 | bss_addr = (bss_addr << 4) + fromhex (*ptr++); | |
c906108c | 2375 | |
31d99776 DJ |
2376 | if (bss_addr != data_addr) |
2377 | warning (_("Target reported unsupported offsets: %s"), buf); | |
2378 | } | |
2379 | else | |
2380 | lose = 1; | |
2381 | } | |
2382 | else if (strncmp (ptr, "TextSeg=", 8) == 0) | |
c906108c | 2383 | { |
31d99776 DJ |
2384 | ptr += 8; |
2385 | /* Don't use strtol, could lose on big values. */ | |
c906108c | 2386 | while (*ptr && *ptr != ';') |
31d99776 DJ |
2387 | text_addr = (text_addr << 4) + fromhex (*ptr++); |
2388 | num_segments = 1; | |
2389 | ||
2390 | if (strncmp (ptr, ";DataSeg=", 9) == 0) | |
2391 | { | |
2392 | ptr += 9; | |
2393 | while (*ptr && *ptr != ';') | |
2394 | data_addr = (data_addr << 4) + fromhex (*ptr++); | |
2395 | num_segments++; | |
2396 | } | |
c906108c SS |
2397 | } |
2398 | else | |
2399 | lose = 1; | |
2400 | ||
2401 | if (lose) | |
8a3fe4f8 | 2402 | error (_("Malformed response to offset query, %s"), buf); |
31d99776 DJ |
2403 | else if (*ptr != '\0') |
2404 | warning (_("Target reported unsupported offsets: %s"), buf); | |
c906108c | 2405 | |
802188a7 | 2406 | offs = ((struct section_offsets *) |
a39a16c4 | 2407 | alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections))); |
802188a7 | 2408 | memcpy (offs, symfile_objfile->section_offsets, |
a39a16c4 | 2409 | SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections)); |
c906108c | 2410 | |
31d99776 DJ |
2411 | data = get_symfile_segment_data (symfile_objfile->obfd); |
2412 | do_segments = (data != NULL); | |
2413 | do_sections = num_segments == 0; | |
c906108c | 2414 | |
28c32713 | 2415 | if (num_segments > 0) |
31d99776 | 2416 | { |
31d99776 DJ |
2417 | segments[0] = text_addr; |
2418 | segments[1] = data_addr; | |
2419 | } | |
28c32713 JB |
2420 | /* If we have two segments, we can still try to relocate everything |
2421 | by assuming that the .text and .data offsets apply to the whole | |
2422 | text and data segments. Convert the offsets given in the packet | |
2423 | to base addresses for symfile_map_offsets_to_segments. */ | |
2424 | else if (data && data->num_segments == 2) | |
2425 | { | |
2426 | segments[0] = data->segment_bases[0] + text_addr; | |
2427 | segments[1] = data->segment_bases[1] + data_addr; | |
2428 | num_segments = 2; | |
2429 | } | |
8d385431 DJ |
2430 | /* If the object file has only one segment, assume that it is text |
2431 | rather than data; main programs with no writable data are rare, | |
2432 | but programs with no code are useless. Of course the code might | |
2433 | have ended up in the data segment... to detect that we would need | |
2434 | the permissions here. */ | |
2435 | else if (data && data->num_segments == 1) | |
2436 | { | |
2437 | segments[0] = data->segment_bases[0] + text_addr; | |
2438 | num_segments = 1; | |
2439 | } | |
28c32713 JB |
2440 | /* There's no way to relocate by segment. */ |
2441 | else | |
2442 | do_segments = 0; | |
31d99776 DJ |
2443 | |
2444 | if (do_segments) | |
2445 | { | |
2446 | int ret = symfile_map_offsets_to_segments (symfile_objfile->obfd, data, | |
2447 | offs, num_segments, segments); | |
2448 | ||
2449 | if (ret == 0 && !do_sections) | |
2450 | error (_("Can not handle qOffsets TextSeg response with this symbol file")); | |
2451 | ||
2452 | if (ret > 0) | |
2453 | do_sections = 0; | |
2454 | } | |
c906108c | 2455 | |
9ef895d6 DJ |
2456 | if (data) |
2457 | free_symfile_segment_data (data); | |
31d99776 DJ |
2458 | |
2459 | if (do_sections) | |
2460 | { | |
2461 | offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr; | |
2462 | ||
2463 | /* This is a temporary kludge to force data and bss to use the same offsets | |
2464 | because that's what nlmconv does now. The real solution requires changes | |
2465 | to the stub and remote.c that I don't have time to do right now. */ | |
2466 | ||
2467 | offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr; | |
2468 | offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr; | |
2469 | } | |
c906108c SS |
2470 | |
2471 | objfile_relocate (symfile_objfile, offs); | |
2472 | } | |
2473 | ||
74531fed PA |
2474 | /* Callback for iterate_over_threads. Set the STOP_REQUESTED flags in |
2475 | threads we know are stopped already. This is used during the | |
2476 | initial remote connection in non-stop mode --- threads that are | |
2477 | reported as already being stopped are left stopped. */ | |
2478 | ||
2479 | static int | |
2480 | set_stop_requested_callback (struct thread_info *thread, void *data) | |
2481 | { | |
2482 | /* If we have a stop reply for this thread, it must be stopped. */ | |
2483 | if (peek_stop_reply (thread->ptid)) | |
2484 | set_stop_requested (thread->ptid, 1); | |
2485 | ||
2486 | return 0; | |
2487 | } | |
2488 | ||
8621d6a9 | 2489 | /* Stub for catch_exception. */ |
0f71a2f6 | 2490 | |
2d717e4f DJ |
2491 | struct start_remote_args |
2492 | { | |
2493 | int from_tty; | |
2494 | ||
2495 | /* The current target. */ | |
2496 | struct target_ops *target; | |
2497 | ||
2498 | /* Non-zero if this is an extended-remote target. */ | |
2499 | int extended_p; | |
2500 | }; | |
2501 | ||
9cbc821d | 2502 | static void |
2d717e4f | 2503 | remote_start_remote (struct ui_out *uiout, void *opaque) |
c906108c | 2504 | { |
2d717e4f | 2505 | struct start_remote_args *args = opaque; |
c8d104ad PA |
2506 | struct remote_state *rs = get_remote_state (); |
2507 | struct packet_config *noack_config; | |
2d717e4f | 2508 | char *wait_status = NULL; |
8621d6a9 | 2509 | |
23860348 | 2510 | immediate_quit++; /* Allow user to interrupt it. */ |
c906108c | 2511 | |
c8d104ad PA |
2512 | /* Ack any packet which the remote side has already sent. */ |
2513 | serial_write (remote_desc, "+", 1); | |
2514 | ||
2515 | /* The first packet we send to the target is the optional "supported | |
2516 | packets" request. If the target can answer this, it will tell us | |
2517 | which later probes to skip. */ | |
2518 | remote_query_supported (); | |
2519 | ||
2520 | /* Next, we possibly activate noack mode. | |
2521 | ||
2522 | If the QStartNoAckMode packet configuration is set to AUTO, | |
2523 | enable noack mode if the stub reported a wish for it with | |
2524 | qSupported. | |
2525 | ||
2526 | If set to TRUE, then enable noack mode even if the stub didn't | |
2527 | report it in qSupported. If the stub doesn't reply OK, the | |
2528 | session ends with an error. | |
2529 | ||
2530 | If FALSE, then don't activate noack mode, regardless of what the | |
2531 | stub claimed should be the default with qSupported. */ | |
2532 | ||
2533 | noack_config = &remote_protocol_packets[PACKET_QStartNoAckMode]; | |
2534 | ||
2535 | if (noack_config->detect == AUTO_BOOLEAN_TRUE | |
2536 | || (noack_config->detect == AUTO_BOOLEAN_AUTO | |
2537 | && noack_config->support == PACKET_ENABLE)) | |
2538 | { | |
2539 | putpkt ("QStartNoAckMode"); | |
2540 | getpkt (&rs->buf, &rs->buf_size, 0); | |
2541 | if (packet_ok (rs->buf, noack_config) == PACKET_OK) | |
2542 | rs->noack_mode = 1; | |
2543 | } | |
2544 | ||
5fe04517 PA |
2545 | if (args->extended_p) |
2546 | { | |
2547 | /* Tell the remote that we are using the extended protocol. */ | |
2548 | putpkt ("!"); | |
2549 | getpkt (&rs->buf, &rs->buf_size, 0); | |
2550 | } | |
2551 | ||
50c71eaf PA |
2552 | /* On OSs where the list of libraries is global to all |
2553 | processes, we fetch them early. */ | |
2554 | if (gdbarch_has_global_solist (target_gdbarch)) | |
2555 | solib_add (NULL, args->from_tty, args->target, auto_solib_add); | |
2556 | ||
c8d104ad PA |
2557 | /* Next, if the target can specify a description, read it. We do |
2558 | this before anything involving memory or registers. */ | |
2559 | target_find_description (); | |
2560 | ||
74531fed PA |
2561 | if (non_stop) |
2562 | { | |
2563 | if (!rs->non_stop_aware) | |
2564 | error (_("Non-stop mode requested, but remote does not support non-stop")); | |
2565 | ||
2566 | putpkt ("QNonStop:1"); | |
2567 | getpkt (&rs->buf, &rs->buf_size, 0); | |
2568 | ||
2569 | if (strcmp (rs->buf, "OK") != 0) | |
2570 | error ("Remote refused setting non-stop mode with: %s", rs->buf); | |
2571 | ||
2572 | /* Find about threads and processes the stub is already | |
2573 | controlling. We default to adding them in the running state. | |
2574 | The '?' query below will then tell us about which threads are | |
2575 | stopped. */ | |
2576 | ||
2577 | /* If we're not using the multi-process extensions, there's no | |
2578 | way to know the pid of the reported threads; use the magic | |
2579 | number. */ | |
2580 | if (!remote_multi_process_p (rs)) | |
2581 | inferior_ptid = magic_null_ptid; | |
2582 | ||
2583 | remote_threads_info (); | |
2584 | } | |
2585 | else if (rs->non_stop_aware) | |
2586 | { | |
2587 | /* Don't assume that the stub can operate in all-stop mode. | |
2588 | Request it explicitely. */ | |
2589 | putpkt ("QNonStop:0"); | |
2590 | getpkt (&rs->buf, &rs->buf_size, 0); | |
2591 | ||
2592 | if (strcmp (rs->buf, "OK") != 0) | |
2593 | error ("Remote refused setting all-stop mode with: %s", rs->buf); | |
2594 | } | |
2595 | ||
2d717e4f DJ |
2596 | /* Check whether the target is running now. */ |
2597 | putpkt ("?"); | |
2598 | getpkt (&rs->buf, &rs->buf_size, 0); | |
2599 | ||
74531fed | 2600 | if (!non_stop) |
2d717e4f | 2601 | { |
74531fed | 2602 | if (rs->buf[0] == 'W' || rs->buf[0] == 'X') |
2d717e4f | 2603 | { |
74531fed PA |
2604 | if (args->extended_p) |
2605 | { | |
2606 | /* We're connected, but not running. Drop out before we | |
2607 | call start_remote. */ | |
2608 | target_mark_exited (args->target); | |
2609 | return; | |
2610 | } | |
2611 | else | |
2612 | error (_("The target is not running (try extended-remote?)")); | |
2d717e4f DJ |
2613 | } |
2614 | else | |
74531fed PA |
2615 | { |
2616 | if (args->extended_p) | |
2617 | target_mark_running (args->target); | |
2618 | ||
2619 | /* Save the reply for later. */ | |
2620 | wait_status = alloca (strlen (rs->buf) + 1); | |
2621 | strcpy (wait_status, rs->buf); | |
2622 | } | |
2623 | ||
2624 | /* Let the stub know that we want it to return the thread. */ | |
2625 | set_continue_thread (minus_one_ptid); | |
2626 | ||
2627 | /* Without this, some commands which require an active target | |
2628 | (such as kill) won't work. This variable serves (at least) | |
2629 | double duty as both the pid of the target process (if it has | |
2630 | such), and as a flag indicating that a target is active. | |
2631 | These functions should be split out into seperate variables, | |
2632 | especially since GDB will someday have a notion of debugging | |
2633 | several processes. */ | |
2634 | inferior_ptid = magic_null_ptid; | |
2635 | ||
2636 | /* Now, if we have thread information, update inferior_ptid. */ | |
2637 | inferior_ptid = remote_current_thread (inferior_ptid); | |
2638 | ||
2639 | add_inferior (ptid_get_pid (inferior_ptid)); | |
2640 | ||
2641 | /* Always add the main thread. */ | |
2642 | add_thread_silent (inferior_ptid); | |
2643 | ||
2644 | get_offsets (); /* Get text, data & bss offsets. */ | |
2645 | ||
2646 | /* Use the previously fetched status. */ | |
2647 | gdb_assert (wait_status != NULL); | |
2648 | strcpy (rs->buf, wait_status); | |
2649 | rs->cached_wait_status = 1; | |
2650 | ||
2651 | immediate_quit--; | |
2652 | start_remote (args->from_tty); /* Initialize gdb process mechanisms. */ | |
2d717e4f DJ |
2653 | } |
2654 | else | |
2655 | { | |
74531fed PA |
2656 | /* In non-stop, we will either get an "OK", meaning that there |
2657 | are no stopped threads at this time; or, a regular stop | |
2658 | reply. In the latter case, there may be more than one thread | |
2659 | stopped --- we pull them all out using the vStopped | |
2660 | mechanism. */ | |
2661 | if (strcmp (rs->buf, "OK") != 0) | |
2662 | { | |
2663 | struct stop_reply *stop_reply; | |
2664 | struct cleanup *old_chain; | |
2d717e4f | 2665 | |
74531fed PA |
2666 | stop_reply = stop_reply_xmalloc (); |
2667 | old_chain = make_cleanup (do_stop_reply_xfree, stop_reply); | |
2d717e4f | 2668 | |
74531fed PA |
2669 | remote_parse_stop_reply (rs->buf, stop_reply); |
2670 | discard_cleanups (old_chain); | |
c0a2216e | 2671 | |
74531fed PA |
2672 | /* get_pending_stop_replies acks this one, and gets the rest |
2673 | out. */ | |
2674 | pending_stop_reply = stop_reply; | |
2675 | remote_get_pending_stop_replies (); | |
c906108c | 2676 | |
74531fed PA |
2677 | /* Make sure that threads that were stopped remain |
2678 | stopped. */ | |
2679 | iterate_over_threads (set_stop_requested_callback, NULL); | |
2680 | } | |
2d717e4f | 2681 | |
74531fed PA |
2682 | if (target_can_async_p ()) |
2683 | target_async (inferior_event_handler, 0); | |
c906108c | 2684 | |
74531fed PA |
2685 | if (thread_count () == 0) |
2686 | { | |
2687 | if (args->extended_p) | |
2688 | { | |
2689 | /* We're connected, but not running. Drop out before we | |
2690 | call start_remote. */ | |
2691 | target_mark_exited (args->target); | |
2692 | return; | |
2693 | } | |
2694 | else | |
2695 | error (_("The target is not running (try extended-remote?)")); | |
2696 | } | |
82f73884 | 2697 | |
74531fed PA |
2698 | if (args->extended_p) |
2699 | target_mark_running (args->target); | |
2700 | ||
2701 | /* Let the stub know that we want it to return the thread. */ | |
c0a2216e | 2702 | |
74531fed PA |
2703 | /* Force the stub to choose a thread. */ |
2704 | set_general_thread (null_ptid); | |
c906108c | 2705 | |
74531fed PA |
2706 | /* Query it. */ |
2707 | inferior_ptid = remote_current_thread (minus_one_ptid); | |
2708 | if (ptid_equal (inferior_ptid, minus_one_ptid)) | |
2709 | error (_("remote didn't report the current thread in non-stop mode")); | |
c906108c | 2710 | |
74531fed PA |
2711 | get_offsets (); /* Get text, data & bss offsets. */ |
2712 | ||
2713 | /* In non-stop mode, any cached wait status will be stored in | |
2714 | the stop reply queue. */ | |
2715 | gdb_assert (wait_status == NULL); | |
2716 | } | |
c8d104ad | 2717 | |
c8d104ad PA |
2718 | /* If we connected to a live target, do some additional setup. */ |
2719 | if (target_has_execution) | |
2720 | { | |
2721 | if (exec_bfd) /* No use without an exec file. */ | |
2722 | remote_check_symbols (symfile_objfile); | |
2723 | } | |
50c71eaf PA |
2724 | |
2725 | /* If code is shared between processes, then breakpoints are global | |
2726 | too; Insert them now. */ | |
2727 | if (gdbarch_has_global_solist (target_gdbarch) | |
2728 | && breakpoints_always_inserted_mode ()) | |
2729 | insert_breakpoints (); | |
c906108c SS |
2730 | } |
2731 | ||
2732 | /* Open a connection to a remote debugger. | |
2733 | NAME is the filename used for communication. */ | |
2734 | ||
2735 | static void | |
fba45db2 | 2736 | remote_open (char *name, int from_tty) |
c906108c | 2737 | { |
75c99385 | 2738 | remote_open_1 (name, from_tty, &remote_ops, 0); |
43ff13b4 JM |
2739 | } |
2740 | ||
c906108c SS |
2741 | /* Open a connection to a remote debugger using the extended |
2742 | remote gdb protocol. NAME is the filename used for communication. */ | |
2743 | ||
2744 | static void | |
fba45db2 | 2745 | extended_remote_open (char *name, int from_tty) |
c906108c | 2746 | { |
75c99385 | 2747 | remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */); |
43ff13b4 JM |
2748 | } |
2749 | ||
c906108c SS |
2750 | /* Generic code for opening a connection to a remote target. */ |
2751 | ||
d471ea57 AC |
2752 | static void |
2753 | init_all_packet_configs (void) | |
2754 | { | |
2755 | int i; | |
444abaca DJ |
2756 | for (i = 0; i < PACKET_MAX; i++) |
2757 | update_packet_config (&remote_protocol_packets[i]); | |
d471ea57 AC |
2758 | } |
2759 | ||
23860348 | 2760 | /* Symbol look-up. */ |
dc8acb97 MS |
2761 | |
2762 | static void | |
2763 | remote_check_symbols (struct objfile *objfile) | |
2764 | { | |
d01949b6 | 2765 | struct remote_state *rs = get_remote_state (); |
dc8acb97 MS |
2766 | char *msg, *reply, *tmp; |
2767 | struct minimal_symbol *sym; | |
2768 | int end; | |
2769 | ||
444abaca | 2770 | if (remote_protocol_packets[PACKET_qSymbol].support == PACKET_DISABLE) |
dc8acb97 MS |
2771 | return; |
2772 | ||
3c9c4b83 PA |
2773 | /* Make sure the remote is pointing at the right process. */ |
2774 | set_general_process (); | |
2775 | ||
6d820c5c DJ |
2776 | /* Allocate a message buffer. We can't reuse the input buffer in RS, |
2777 | because we need both at the same time. */ | |
ea9c271d | 2778 | msg = alloca (get_remote_packet_size ()); |
6d820c5c | 2779 | |
23860348 | 2780 | /* Invite target to request symbol lookups. */ |
dc8acb97 MS |
2781 | |
2782 | putpkt ("qSymbol::"); | |
6d820c5c DJ |
2783 | getpkt (&rs->buf, &rs->buf_size, 0); |
2784 | packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSymbol]); | |
2e9f7625 | 2785 | reply = rs->buf; |
dc8acb97 MS |
2786 | |
2787 | while (strncmp (reply, "qSymbol:", 8) == 0) | |
2788 | { | |
2789 | tmp = &reply[8]; | |
cfd77fa1 | 2790 | end = hex2bin (tmp, (gdb_byte *) msg, strlen (tmp) / 2); |
dc8acb97 MS |
2791 | msg[end] = '\0'; |
2792 | sym = lookup_minimal_symbol (msg, NULL, NULL); | |
2793 | if (sym == NULL) | |
ea9c271d | 2794 | xsnprintf (msg, get_remote_packet_size (), "qSymbol::%s", &reply[8]); |
dc8acb97 | 2795 | else |
2bbe3cc1 DJ |
2796 | { |
2797 | CORE_ADDR sym_addr = SYMBOL_VALUE_ADDRESS (sym); | |
2798 | ||
2799 | /* If this is a function address, return the start of code | |
2800 | instead of any data function descriptor. */ | |
1cf3db46 | 2801 | sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch, |
2bbe3cc1 DJ |
2802 | sym_addr, |
2803 | ¤t_target); | |
2804 | ||
2805 | xsnprintf (msg, get_remote_packet_size (), "qSymbol:%s:%s", | |
2806 | paddr_nz (sym_addr), &reply[8]); | |
2807 | } | |
2808 | ||
dc8acb97 | 2809 | putpkt (msg); |
6d820c5c | 2810 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 2811 | reply = rs->buf; |
dc8acb97 MS |
2812 | } |
2813 | } | |
2814 | ||
9db8d71f DJ |
2815 | static struct serial * |
2816 | remote_serial_open (char *name) | |
2817 | { | |
2818 | static int udp_warning = 0; | |
2819 | ||
2820 | /* FIXME: Parsing NAME here is a hack. But we want to warn here instead | |
2821 | of in ser-tcp.c, because it is the remote protocol assuming that the | |
2822 | serial connection is reliable and not the serial connection promising | |
2823 | to be. */ | |
2824 | if (!udp_warning && strncmp (name, "udp:", 4) == 0) | |
2825 | { | |
8a3fe4f8 AC |
2826 | warning (_("\ |
2827 | The remote protocol may be unreliable over UDP.\n\ | |
2828 | Some events may be lost, rendering further debugging impossible.")); | |
9db8d71f DJ |
2829 | udp_warning = 1; |
2830 | } | |
2831 | ||
2832 | return serial_open (name); | |
2833 | } | |
2834 | ||
be2a5f71 DJ |
2835 | /* This type describes each known response to the qSupported |
2836 | packet. */ | |
2837 | struct protocol_feature | |
2838 | { | |
2839 | /* The name of this protocol feature. */ | |
2840 | const char *name; | |
2841 | ||
2842 | /* The default for this protocol feature. */ | |
2843 | enum packet_support default_support; | |
2844 | ||
2845 | /* The function to call when this feature is reported, or after | |
2846 | qSupported processing if the feature is not supported. | |
2847 | The first argument points to this structure. The second | |
2848 | argument indicates whether the packet requested support be | |
2849 | enabled, disabled, or probed (or the default, if this function | |
2850 | is being called at the end of processing and this feature was | |
2851 | not reported). The third argument may be NULL; if not NULL, it | |
2852 | is a NUL-terminated string taken from the packet following | |
2853 | this feature's name and an equals sign. */ | |
2854 | void (*func) (const struct protocol_feature *, enum packet_support, | |
2855 | const char *); | |
2856 | ||
2857 | /* The corresponding packet for this feature. Only used if | |
2858 | FUNC is remote_supported_packet. */ | |
2859 | int packet; | |
2860 | }; | |
2861 | ||
be2a5f71 DJ |
2862 | static void |
2863 | remote_supported_packet (const struct protocol_feature *feature, | |
2864 | enum packet_support support, | |
2865 | const char *argument) | |
2866 | { | |
2867 | if (argument) | |
2868 | { | |
2869 | warning (_("Remote qSupported response supplied an unexpected value for" | |
2870 | " \"%s\"."), feature->name); | |
2871 | return; | |
2872 | } | |
2873 | ||
2874 | if (remote_protocol_packets[feature->packet].support | |
2875 | == PACKET_SUPPORT_UNKNOWN) | |
2876 | remote_protocol_packets[feature->packet].support = support; | |
2877 | } | |
be2a5f71 DJ |
2878 | |
2879 | static void | |
2880 | remote_packet_size (const struct protocol_feature *feature, | |
2881 | enum packet_support support, const char *value) | |
2882 | { | |
2883 | struct remote_state *rs = get_remote_state (); | |
2884 | ||
2885 | int packet_size; | |
2886 | char *value_end; | |
2887 | ||
2888 | if (support != PACKET_ENABLE) | |
2889 | return; | |
2890 | ||
2891 | if (value == NULL || *value == '\0') | |
2892 | { | |
2893 | warning (_("Remote target reported \"%s\" without a size."), | |
2894 | feature->name); | |
2895 | return; | |
2896 | } | |
2897 | ||
2898 | errno = 0; | |
2899 | packet_size = strtol (value, &value_end, 16); | |
2900 | if (errno != 0 || *value_end != '\0' || packet_size < 0) | |
2901 | { | |
2902 | warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."), | |
2903 | feature->name, value); | |
2904 | return; | |
2905 | } | |
2906 | ||
2907 | if (packet_size > MAX_REMOTE_PACKET_SIZE) | |
2908 | { | |
2909 | warning (_("limiting remote suggested packet size (%d bytes) to %d"), | |
2910 | packet_size, MAX_REMOTE_PACKET_SIZE); | |
2911 | packet_size = MAX_REMOTE_PACKET_SIZE; | |
2912 | } | |
2913 | ||
2914 | /* Record the new maximum packet size. */ | |
2915 | rs->explicit_packet_size = packet_size; | |
2916 | } | |
2917 | ||
82f73884 PA |
2918 | static void |
2919 | remote_multi_process_feature (const struct protocol_feature *feature, | |
2920 | enum packet_support support, const char *value) | |
2921 | { | |
2922 | struct remote_state *rs = get_remote_state (); | |
2923 | rs->multi_process_aware = (support == PACKET_ENABLE); | |
2924 | } | |
2925 | ||
74531fed PA |
2926 | static void |
2927 | remote_non_stop_feature (const struct protocol_feature *feature, | |
2928 | enum packet_support support, const char *value) | |
2929 | { | |
2930 | struct remote_state *rs = get_remote_state (); | |
2931 | rs->non_stop_aware = (support == PACKET_ENABLE); | |
2932 | } | |
2933 | ||
be2a5f71 | 2934 | static struct protocol_feature remote_protocol_features[] = { |
0876f84a | 2935 | { "PacketSize", PACKET_DISABLE, remote_packet_size, -1 }, |
40e57cf2 | 2936 | { "qXfer:auxv:read", PACKET_DISABLE, remote_supported_packet, |
fd79ecee | 2937 | PACKET_qXfer_auxv }, |
23181151 DJ |
2938 | { "qXfer:features:read", PACKET_DISABLE, remote_supported_packet, |
2939 | PACKET_qXfer_features }, | |
cfa9d6d9 DJ |
2940 | { "qXfer:libraries:read", PACKET_DISABLE, remote_supported_packet, |
2941 | PACKET_qXfer_libraries }, | |
fd79ecee | 2942 | { "qXfer:memory-map:read", PACKET_DISABLE, remote_supported_packet, |
89be2091 | 2943 | PACKET_qXfer_memory_map }, |
4de6483e UW |
2944 | { "qXfer:spu:read", PACKET_DISABLE, remote_supported_packet, |
2945 | PACKET_qXfer_spu_read }, | |
2946 | { "qXfer:spu:write", PACKET_DISABLE, remote_supported_packet, | |
2947 | PACKET_qXfer_spu_write }, | |
89be2091 DJ |
2948 | { "QPassSignals", PACKET_DISABLE, remote_supported_packet, |
2949 | PACKET_QPassSignals }, | |
a6f3e723 SL |
2950 | { "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet, |
2951 | PACKET_QStartNoAckMode }, | |
82f73884 | 2952 | { "multiprocess", PACKET_DISABLE, remote_multi_process_feature, -1 }, |
74531fed | 2953 | { "QNonStop", PACKET_DISABLE, remote_non_stop_feature, -1 }, |
be2a5f71 DJ |
2954 | }; |
2955 | ||
2956 | static void | |
2957 | remote_query_supported (void) | |
2958 | { | |
2959 | struct remote_state *rs = get_remote_state (); | |
2960 | char *next; | |
2961 | int i; | |
2962 | unsigned char seen [ARRAY_SIZE (remote_protocol_features)]; | |
2963 | ||
2964 | /* The packet support flags are handled differently for this packet | |
2965 | than for most others. We treat an error, a disabled packet, and | |
2966 | an empty response identically: any features which must be reported | |
2967 | to be used will be automatically disabled. An empty buffer | |
2968 | accomplishes this, since that is also the representation for a list | |
2969 | containing no features. */ | |
2970 | ||
2971 | rs->buf[0] = 0; | |
2972 | if (remote_protocol_packets[PACKET_qSupported].support != PACKET_DISABLE) | |
2973 | { | |
82f73884 PA |
2974 | if (rs->extended) |
2975 | putpkt ("qSupported:multiprocess+"); | |
2976 | else | |
2977 | putpkt ("qSupported"); | |
2978 | ||
be2a5f71 DJ |
2979 | getpkt (&rs->buf, &rs->buf_size, 0); |
2980 | ||
2981 | /* If an error occured, warn, but do not return - just reset the | |
2982 | buffer to empty and go on to disable features. */ | |
2983 | if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSupported]) | |
2984 | == PACKET_ERROR) | |
2985 | { | |
2986 | warning (_("Remote failure reply: %s"), rs->buf); | |
2987 | rs->buf[0] = 0; | |
2988 | } | |
2989 | } | |
2990 | ||
2991 | memset (seen, 0, sizeof (seen)); | |
2992 | ||
2993 | next = rs->buf; | |
2994 | while (*next) | |
2995 | { | |
2996 | enum packet_support is_supported; | |
2997 | char *p, *end, *name_end, *value; | |
2998 | ||
2999 | /* First separate out this item from the rest of the packet. If | |
3000 | there's another item after this, we overwrite the separator | |
3001 | (terminated strings are much easier to work with). */ | |
3002 | p = next; | |
3003 | end = strchr (p, ';'); | |
3004 | if (end == NULL) | |
3005 | { | |
3006 | end = p + strlen (p); | |
3007 | next = end; | |
3008 | } | |
3009 | else | |
3010 | { | |
89be2091 DJ |
3011 | *end = '\0'; |
3012 | next = end + 1; | |
3013 | ||
be2a5f71 DJ |
3014 | if (end == p) |
3015 | { | |
3016 | warning (_("empty item in \"qSupported\" response")); | |
3017 | continue; | |
3018 | } | |
be2a5f71 DJ |
3019 | } |
3020 | ||
3021 | name_end = strchr (p, '='); | |
3022 | if (name_end) | |
3023 | { | |
3024 | /* This is a name=value entry. */ | |
3025 | is_supported = PACKET_ENABLE; | |
3026 | value = name_end + 1; | |
3027 | *name_end = '\0'; | |
3028 | } | |
3029 | else | |
3030 | { | |
3031 | value = NULL; | |
3032 | switch (end[-1]) | |
3033 | { | |
3034 | case '+': | |
3035 | is_supported = PACKET_ENABLE; | |
3036 | break; | |
3037 | ||
3038 | case '-': | |
3039 | is_supported = PACKET_DISABLE; | |
3040 | break; | |
3041 | ||
3042 | case '?': | |
3043 | is_supported = PACKET_SUPPORT_UNKNOWN; | |
3044 | break; | |
3045 | ||
3046 | default: | |
3047 | warning (_("unrecognized item \"%s\" in \"qSupported\" response"), p); | |
3048 | continue; | |
3049 | } | |
3050 | end[-1] = '\0'; | |
3051 | } | |
3052 | ||
3053 | for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++) | |
3054 | if (strcmp (remote_protocol_features[i].name, p) == 0) | |
3055 | { | |
3056 | const struct protocol_feature *feature; | |
3057 | ||
3058 | seen[i] = 1; | |
3059 | feature = &remote_protocol_features[i]; | |
3060 | feature->func (feature, is_supported, value); | |
3061 | break; | |
3062 | } | |
3063 | } | |
3064 | ||
3065 | /* If we increased the packet size, make sure to increase the global | |
3066 | buffer size also. We delay this until after parsing the entire | |
3067 | qSupported packet, because this is the same buffer we were | |
3068 | parsing. */ | |
3069 | if (rs->buf_size < rs->explicit_packet_size) | |
3070 | { | |
3071 | rs->buf_size = rs->explicit_packet_size; | |
3072 | rs->buf = xrealloc (rs->buf, rs->buf_size); | |
3073 | } | |
3074 | ||
3075 | /* Handle the defaults for unmentioned features. */ | |
3076 | for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++) | |
3077 | if (!seen[i]) | |
3078 | { | |
3079 | const struct protocol_feature *feature; | |
3080 | ||
3081 | feature = &remote_protocol_features[i]; | |
3082 | feature->func (feature, feature->default_support, NULL); | |
3083 | } | |
3084 | } | |
3085 | ||
3086 | ||
c906108c | 3087 | static void |
75c99385 | 3088 | remote_open_1 (char *name, int from_tty, struct target_ops *target, int extended_p) |
c906108c | 3089 | { |
d01949b6 | 3090 | struct remote_state *rs = get_remote_state (); |
a6f3e723 | 3091 | |
c906108c | 3092 | if (name == 0) |
8a3fe4f8 | 3093 | error (_("To open a remote debug connection, you need to specify what\n" |
22e04375 | 3094 | "serial device is attached to the remote system\n" |
8a3fe4f8 | 3095 | "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).")); |
c906108c | 3096 | |
23860348 | 3097 | /* See FIXME above. */ |
c6ebd6cf | 3098 | if (!target_async_permitted) |
92d1e331 | 3099 | wait_forever_enabled_p = 1; |
6426a772 | 3100 | |
2d717e4f DJ |
3101 | /* If we're connected to a running target, target_preopen will kill it. |
3102 | But if we're connected to a target system with no running process, | |
3103 | then we will still be connected when it returns. Ask this question | |
3104 | first, before target_preopen has a chance to kill anything. */ | |
3105 | if (remote_desc != NULL && !target_has_execution) | |
3106 | { | |
3107 | if (!from_tty | |
3108 | || query (_("Already connected to a remote target. Disconnect? "))) | |
3109 | pop_target (); | |
3110 | else | |
3111 | error (_("Still connected.")); | |
3112 | } | |
3113 | ||
c906108c SS |
3114 | target_preopen (from_tty); |
3115 | ||
3116 | unpush_target (target); | |
3117 | ||
2d717e4f DJ |
3118 | /* This time without a query. If we were connected to an |
3119 | extended-remote target and target_preopen killed the running | |
3120 | process, we may still be connected. If we are starting "target | |
3121 | remote" now, the extended-remote target will not have been | |
3122 | removed by unpush_target. */ | |
3123 | if (remote_desc != NULL && !target_has_execution) | |
3124 | pop_target (); | |
3125 | ||
89be2091 DJ |
3126 | /* Make sure we send the passed signals list the next time we resume. */ |
3127 | xfree (last_pass_packet); | |
3128 | last_pass_packet = NULL; | |
3129 | ||
ad9a8f3f | 3130 | remote_fileio_reset (); |
1dd41f16 NS |
3131 | reopen_exec_file (); |
3132 | reread_symbols (); | |
3133 | ||
9db8d71f | 3134 | remote_desc = remote_serial_open (name); |
c906108c SS |
3135 | if (!remote_desc) |
3136 | perror_with_name (name); | |
3137 | ||
3138 | if (baud_rate != -1) | |
3139 | { | |
2cd58942 | 3140 | if (serial_setbaudrate (remote_desc, baud_rate)) |
c906108c | 3141 | { |
9b74d5d3 KB |
3142 | /* The requested speed could not be set. Error out to |
3143 | top level after closing remote_desc. Take care to | |
3144 | set remote_desc to NULL to avoid closing remote_desc | |
3145 | more than once. */ | |
2cd58942 | 3146 | serial_close (remote_desc); |
9b74d5d3 | 3147 | remote_desc = NULL; |
c906108c SS |
3148 | perror_with_name (name); |
3149 | } | |
3150 | } | |
3151 | ||
2cd58942 | 3152 | serial_raw (remote_desc); |
c906108c SS |
3153 | |
3154 | /* If there is something sitting in the buffer we might take it as a | |
3155 | response to a command, which would be bad. */ | |
2cd58942 | 3156 | serial_flush_input (remote_desc); |
c906108c SS |
3157 | |
3158 | if (from_tty) | |
3159 | { | |
3160 | puts_filtered ("Remote debugging using "); | |
3161 | puts_filtered (name); | |
3162 | puts_filtered ("\n"); | |
3163 | } | |
23860348 | 3164 | push_target (target); /* Switch to using remote target now. */ |
c906108c | 3165 | |
2d717e4f DJ |
3166 | /* Assume that the target is running, unless we learn otherwise. */ |
3167 | target_mark_running (target); | |
3168 | ||
74531fed PA |
3169 | /* Register extra event sources in the event loop. */ |
3170 | remote_async_inferior_event_token | |
3171 | = create_async_event_handler (remote_async_inferior_event_handler, | |
3172 | NULL); | |
3173 | remote_async_get_pending_events_token | |
3174 | = create_async_event_handler (remote_async_get_pending_events_handler, | |
3175 | NULL); | |
3176 | ||
be2a5f71 DJ |
3177 | /* Reset the target state; these things will be queried either by |
3178 | remote_query_supported or as they are needed. */ | |
d471ea57 | 3179 | init_all_packet_configs (); |
74531fed | 3180 | rs->cached_wait_status = 0; |
be2a5f71 | 3181 | rs->explicit_packet_size = 0; |
a6f3e723 | 3182 | rs->noack_mode = 0; |
82f73884 PA |
3183 | rs->multi_process_aware = 0; |
3184 | rs->extended = extended_p; | |
74531fed | 3185 | rs->non_stop_aware = 0; |
e24a49d8 | 3186 | rs->waiting_for_stop_reply = 0; |
802188a7 | 3187 | |
79d7f229 PA |
3188 | general_thread = not_sent_ptid; |
3189 | continue_thread = not_sent_ptid; | |
c906108c | 3190 | |
9d1f7ab2 MS |
3191 | /* Probe for ability to use "ThreadInfo" query, as required. */ |
3192 | use_threadinfo_query = 1; | |
3193 | use_threadextra_query = 1; | |
3194 | ||
c6ebd6cf | 3195 | if (target_async_permitted) |
92d1e331 | 3196 | { |
23860348 | 3197 | /* With this target we start out by owning the terminal. */ |
92d1e331 DJ |
3198 | remote_async_terminal_ours_p = 1; |
3199 | ||
3200 | /* FIXME: cagney/1999-09-23: During the initial connection it is | |
3201 | assumed that the target is already ready and able to respond to | |
3202 | requests. Unfortunately remote_start_remote() eventually calls | |
3203 | wait_for_inferior() with no timeout. wait_forever_enabled_p gets | |
3204 | around this. Eventually a mechanism that allows | |
3205 | wait_for_inferior() to expect/get timeouts will be | |
23860348 | 3206 | implemented. */ |
92d1e331 DJ |
3207 | wait_forever_enabled_p = 0; |
3208 | } | |
3209 | ||
23860348 | 3210 | /* First delete any symbols previously loaded from shared libraries. */ |
f78f6cf1 | 3211 | no_shared_libraries (NULL, 0); |
f78f6cf1 | 3212 | |
74531fed PA |
3213 | /* Start afresh. */ |
3214 | init_thread_list (); | |
3215 | ||
36918e70 | 3216 | /* Start the remote connection. If error() or QUIT, discard this |
165b8e33 AC |
3217 | target (we'd otherwise be in an inconsistent state) and then |
3218 | propogate the error on up the exception chain. This ensures that | |
3219 | the caller doesn't stumble along blindly assuming that the | |
3220 | function succeeded. The CLI doesn't have this problem but other | |
3221 | UI's, such as MI do. | |
36918e70 AC |
3222 | |
3223 | FIXME: cagney/2002-05-19: Instead of re-throwing the exception, | |
3224 | this function should return an error indication letting the | |
ce2826aa | 3225 | caller restore the previous state. Unfortunately the command |
36918e70 AC |
3226 | ``target remote'' is directly wired to this function making that |
3227 | impossible. On a positive note, the CLI side of this problem has | |
3228 | been fixed - the function set_cmd_context() makes it possible for | |
3229 | all the ``target ....'' commands to share a common callback | |
3230 | function. See cli-dump.c. */ | |
109c3e39 | 3231 | { |
2d717e4f DJ |
3232 | struct gdb_exception ex; |
3233 | struct start_remote_args args; | |
3234 | ||
3235 | args.from_tty = from_tty; | |
3236 | args.target = target; | |
3237 | args.extended_p = extended_p; | |
3238 | ||
3239 | ex = catch_exception (uiout, remote_start_remote, &args, RETURN_MASK_ALL); | |
109c3e39 AC |
3240 | if (ex.reason < 0) |
3241 | { | |
c8d104ad PA |
3242 | /* Pop the partially set up target - unless something else did |
3243 | already before throwing the exception. */ | |
3244 | if (remote_desc != NULL) | |
3245 | pop_target (); | |
c6ebd6cf | 3246 | if (target_async_permitted) |
109c3e39 AC |
3247 | wait_forever_enabled_p = 1; |
3248 | throw_exception (ex); | |
3249 | } | |
3250 | } | |
c906108c | 3251 | |
c6ebd6cf | 3252 | if (target_async_permitted) |
92d1e331 | 3253 | wait_forever_enabled_p = 1; |
43ff13b4 JM |
3254 | } |
3255 | ||
c906108c SS |
3256 | /* This takes a program previously attached to and detaches it. After |
3257 | this is done, GDB can be used to debug some other program. We | |
3258 | better not have left any breakpoints in the target program or it'll | |
3259 | die when it hits one. */ | |
3260 | ||
3261 | static void | |
2d717e4f | 3262 | remote_detach_1 (char *args, int from_tty, int extended) |
c906108c | 3263 | { |
82f73884 | 3264 | int pid = ptid_get_pid (inferior_ptid); |
d01949b6 | 3265 | struct remote_state *rs = get_remote_state (); |
c906108c SS |
3266 | |
3267 | if (args) | |
8a3fe4f8 | 3268 | error (_("Argument given to \"detach\" when remotely debugging.")); |
c906108c | 3269 | |
2d717e4f DJ |
3270 | if (!target_has_execution) |
3271 | error (_("No process to detach from.")); | |
3272 | ||
c906108c | 3273 | /* Tell the remote target to detach. */ |
82f73884 PA |
3274 | if (remote_multi_process_p (rs)) |
3275 | sprintf (rs->buf, "D;%x", pid); | |
3276 | else | |
3277 | strcpy (rs->buf, "D"); | |
3278 | ||
4ddda9b5 PA |
3279 | putpkt (rs->buf); |
3280 | getpkt (&rs->buf, &rs->buf_size, 0); | |
3281 | ||
82f73884 PA |
3282 | if (rs->buf[0] == 'O' && rs->buf[1] == 'K') |
3283 | ; | |
3284 | else if (rs->buf[0] == '\0') | |
3285 | error (_("Remote doesn't know how to detach")); | |
3286 | else | |
4ddda9b5 | 3287 | error (_("Can't detach process.")); |
c906108c | 3288 | |
c906108c | 3289 | if (from_tty) |
2d717e4f | 3290 | { |
82f73884 PA |
3291 | if (remote_multi_process_p (rs)) |
3292 | printf_filtered (_("Detached from remote %s.\n"), | |
3293 | target_pid_to_str (pid_to_ptid (pid))); | |
2d717e4f | 3294 | else |
82f73884 PA |
3295 | { |
3296 | if (extended) | |
3297 | puts_filtered (_("Detached from remote process.\n")); | |
3298 | else | |
3299 | puts_filtered (_("Ending remote debugging.\n")); | |
3300 | } | |
2d717e4f | 3301 | } |
82f73884 | 3302 | |
74531fed | 3303 | discard_pending_stop_replies (pid); |
82f73884 PA |
3304 | detach_inferior (pid); |
3305 | target_mourn_inferior (); | |
2d717e4f DJ |
3306 | } |
3307 | ||
3308 | static void | |
136d6dae | 3309 | remote_detach (struct target_ops *ops, char *args, int from_tty) |
2d717e4f DJ |
3310 | { |
3311 | remote_detach_1 (args, from_tty, 0); | |
3312 | } | |
3313 | ||
3314 | static void | |
136d6dae | 3315 | extended_remote_detach (struct target_ops *ops, char *args, int from_tty) |
2d717e4f DJ |
3316 | { |
3317 | remote_detach_1 (args, from_tty, 1); | |
c906108c SS |
3318 | } |
3319 | ||
6ad8ae5c DJ |
3320 | /* Same as remote_detach, but don't send the "D" packet; just disconnect. */ |
3321 | ||
43ff13b4 | 3322 | static void |
597320e7 | 3323 | remote_disconnect (struct target_ops *target, char *args, int from_tty) |
43ff13b4 | 3324 | { |
43ff13b4 | 3325 | if (args) |
2d717e4f | 3326 | error (_("Argument given to \"disconnect\" when remotely debugging.")); |
43ff13b4 | 3327 | |
2d717e4f DJ |
3328 | /* Make sure we unpush even the extended remote targets; mourn |
3329 | won't do it. So call remote_mourn_1 directly instead of | |
3330 | target_mourn_inferior. */ | |
3331 | remote_mourn_1 (target); | |
3332 | ||
43ff13b4 JM |
3333 | if (from_tty) |
3334 | puts_filtered ("Ending remote debugging.\n"); | |
3335 | } | |
3336 | ||
2d717e4f DJ |
3337 | /* Attach to the process specified by ARGS. If FROM_TTY is non-zero, |
3338 | be chatty about it. */ | |
3339 | ||
3340 | static void | |
3341 | extended_remote_attach_1 (struct target_ops *target, char *args, int from_tty) | |
3342 | { | |
3343 | struct remote_state *rs = get_remote_state (); | |
be86555c | 3344 | int pid; |
2d717e4f | 3345 | char *dummy; |
96ef3384 | 3346 | char *wait_status = NULL; |
181e7f93 | 3347 | struct inferior *inf; |
2d717e4f DJ |
3348 | |
3349 | if (!args) | |
3350 | error_no_arg (_("process-id to attach")); | |
3351 | ||
3352 | dummy = args; | |
3353 | pid = strtol (args, &dummy, 0); | |
3354 | /* Some targets don't set errno on errors, grrr! */ | |
3355 | if (pid == 0 && args == dummy) | |
3356 | error (_("Illegal process-id: %s."), args); | |
3357 | ||
3358 | if (remote_protocol_packets[PACKET_vAttach].support == PACKET_DISABLE) | |
3359 | error (_("This target does not support attaching to a process")); | |
3360 | ||
3361 | sprintf (rs->buf, "vAttach;%x", pid); | |
3362 | putpkt (rs->buf); | |
3363 | getpkt (&rs->buf, &rs->buf_size, 0); | |
3364 | ||
3365 | if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vAttach]) == PACKET_OK) | |
3366 | { | |
3367 | if (from_tty) | |
3368 | printf_unfiltered (_("Attached to %s\n"), | |
3369 | target_pid_to_str (pid_to_ptid (pid))); | |
3370 | ||
74531fed PA |
3371 | if (!non_stop) |
3372 | { | |
3373 | /* Save the reply for later. */ | |
3374 | wait_status = alloca (strlen (rs->buf) + 1); | |
3375 | strcpy (wait_status, rs->buf); | |
3376 | } | |
3377 | else if (strcmp (rs->buf, "OK") != 0) | |
3378 | error (_("Attaching to %s failed with: %s"), | |
3379 | target_pid_to_str (pid_to_ptid (pid)), | |
3380 | rs->buf); | |
2d717e4f DJ |
3381 | } |
3382 | else if (remote_protocol_packets[PACKET_vAttach].support == PACKET_DISABLE) | |
3383 | error (_("This target does not support attaching to a process")); | |
3384 | else | |
3385 | error (_("Attaching to %s failed"), | |
3386 | target_pid_to_str (pid_to_ptid (pid))); | |
3387 | ||
3388 | target_mark_running (target); | |
3389 | inferior_ptid = pid_to_ptid (pid); | |
79d7f229 PA |
3390 | |
3391 | /* Now, if we have thread information, update inferior_ptid. */ | |
3392 | inferior_ptid = remote_current_thread (inferior_ptid); | |
3393 | ||
181e7f93 PA |
3394 | inf = add_inferior (pid); |
3395 | inf->attach_flag = 1; | |
82f73884 | 3396 | |
74531fed PA |
3397 | if (non_stop) |
3398 | /* Get list of threads. */ | |
3399 | remote_threads_info (); | |
3400 | else | |
3401 | /* Add the main thread to the thread list. */ | |
3402 | add_thread_silent (inferior_ptid); | |
c0a2216e | 3403 | |
96ef3384 UW |
3404 | /* Next, if the target can specify a description, read it. We do |
3405 | this before anything involving memory or registers. */ | |
3406 | target_find_description (); | |
3407 | ||
74531fed PA |
3408 | if (!non_stop) |
3409 | { | |
3410 | /* Use the previously fetched status. */ | |
3411 | gdb_assert (wait_status != NULL); | |
3412 | ||
3413 | if (target_can_async_p ()) | |
3414 | { | |
3415 | struct stop_reply *stop_reply; | |
3416 | struct cleanup *old_chain; | |
3417 | ||
3418 | stop_reply = stop_reply_xmalloc (); | |
3419 | old_chain = make_cleanup (do_stop_reply_xfree, stop_reply); | |
3420 | remote_parse_stop_reply (wait_status, stop_reply); | |
3421 | discard_cleanups (old_chain); | |
3422 | push_stop_reply (stop_reply); | |
3423 | ||
3424 | target_async (inferior_event_handler, 0); | |
3425 | } | |
3426 | else | |
3427 | { | |
3428 | gdb_assert (wait_status != NULL); | |
3429 | strcpy (rs->buf, wait_status); | |
3430 | rs->cached_wait_status = 1; | |
3431 | } | |
3432 | } | |
3433 | else | |
3434 | gdb_assert (wait_status == NULL); | |
2d717e4f DJ |
3435 | } |
3436 | ||
3437 | static void | |
136d6dae | 3438 | extended_remote_attach (struct target_ops *ops, char *args, int from_tty) |
2d717e4f | 3439 | { |
136d6dae | 3440 | extended_remote_attach_1 (ops, args, from_tty); |
2d717e4f DJ |
3441 | } |
3442 | ||
c906108c SS |
3443 | /* Convert hex digit A to a number. */ |
3444 | ||
30559e10 | 3445 | static int |
fba45db2 | 3446 | fromhex (int a) |
c906108c SS |
3447 | { |
3448 | if (a >= '0' && a <= '9') | |
3449 | return a - '0'; | |
3450 | else if (a >= 'a' && a <= 'f') | |
3451 | return a - 'a' + 10; | |
3452 | else if (a >= 'A' && a <= 'F') | |
3453 | return a - 'A' + 10; | |
c5aa993b | 3454 | else |
8a3fe4f8 | 3455 | error (_("Reply contains invalid hex digit %d"), a); |
c906108c SS |
3456 | } |
3457 | ||
30559e10 | 3458 | static int |
cfd77fa1 | 3459 | hex2bin (const char *hex, gdb_byte *bin, int count) |
30559e10 MS |
3460 | { |
3461 | int i; | |
3462 | ||
30559e10 MS |
3463 | for (i = 0; i < count; i++) |
3464 | { | |
3465 | if (hex[0] == 0 || hex[1] == 0) | |
3466 | { | |
3467 | /* Hex string is short, or of uneven length. | |
23860348 | 3468 | Return the count that has been converted so far. */ |
30559e10 MS |
3469 | return i; |
3470 | } | |
3471 | *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]); | |
3472 | hex += 2; | |
3473 | } | |
3474 | return i; | |
3475 | } | |
3476 | ||
c906108c SS |
3477 | /* Convert number NIB to a hex digit. */ |
3478 | ||
3479 | static int | |
fba45db2 | 3480 | tohex (int nib) |
c906108c SS |
3481 | { |
3482 | if (nib < 10) | |
c5aa993b | 3483 | return '0' + nib; |
c906108c | 3484 | else |
c5aa993b | 3485 | return 'a' + nib - 10; |
c906108c | 3486 | } |
30559e10 MS |
3487 | |
3488 | static int | |
cfd77fa1 | 3489 | bin2hex (const gdb_byte *bin, char *hex, int count) |
30559e10 MS |
3490 | { |
3491 | int i; | |
23860348 | 3492 | /* May use a length, or a nul-terminated string as input. */ |
30559e10 | 3493 | if (count == 0) |
cfd77fa1 | 3494 | count = strlen ((char *) bin); |
30559e10 MS |
3495 | |
3496 | for (i = 0; i < count; i++) | |
3497 | { | |
3498 | *hex++ = tohex ((*bin >> 4) & 0xf); | |
3499 | *hex++ = tohex (*bin++ & 0xf); | |
3500 | } | |
3501 | *hex = 0; | |
3502 | return i; | |
3503 | } | |
c906108c | 3504 | \f |
506fb367 DJ |
3505 | /* Check for the availability of vCont. This function should also check |
3506 | the response. */ | |
c906108c SS |
3507 | |
3508 | static void | |
6d820c5c | 3509 | remote_vcont_probe (struct remote_state *rs) |
c906108c | 3510 | { |
2e9f7625 | 3511 | char *buf; |
6d820c5c | 3512 | |
2e9f7625 DJ |
3513 | strcpy (rs->buf, "vCont?"); |
3514 | putpkt (rs->buf); | |
6d820c5c | 3515 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 3516 | buf = rs->buf; |
c906108c | 3517 | |
506fb367 DJ |
3518 | /* Make sure that the features we assume are supported. */ |
3519 | if (strncmp (buf, "vCont", 5) == 0) | |
3520 | { | |
3521 | char *p = &buf[5]; | |
3522 | int support_s, support_S, support_c, support_C; | |
3523 | ||
3524 | support_s = 0; | |
3525 | support_S = 0; | |
3526 | support_c = 0; | |
3527 | support_C = 0; | |
74531fed | 3528 | rs->support_vCont_t = 0; |
506fb367 DJ |
3529 | while (p && *p == ';') |
3530 | { | |
3531 | p++; | |
3532 | if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0)) | |
3533 | support_s = 1; | |
3534 | else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0)) | |
3535 | support_S = 1; | |
3536 | else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0)) | |
3537 | support_c = 1; | |
3538 | else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0)) | |
3539 | support_C = 1; | |
74531fed PA |
3540 | else if (*p == 't' && (*(p + 1) == ';' || *(p + 1) == 0)) |
3541 | rs->support_vCont_t = 1; | |
506fb367 DJ |
3542 | |
3543 | p = strchr (p, ';'); | |
3544 | } | |
c906108c | 3545 | |
506fb367 DJ |
3546 | /* If s, S, c, and C are not all supported, we can't use vCont. Clearing |
3547 | BUF will make packet_ok disable the packet. */ | |
3548 | if (!support_s || !support_S || !support_c || !support_C) | |
3549 | buf[0] = 0; | |
3550 | } | |
c906108c | 3551 | |
444abaca | 3552 | packet_ok (buf, &remote_protocol_packets[PACKET_vCont]); |
506fb367 | 3553 | } |
c906108c | 3554 | |
506fb367 DJ |
3555 | /* Resume the remote inferior by using a "vCont" packet. The thread |
3556 | to be resumed is PTID; STEP and SIGGNAL indicate whether the | |
79d7f229 PA |
3557 | resumed thread should be single-stepped and/or signalled. If PTID |
3558 | equals minus_one_ptid, then all threads are resumed; the thread to | |
3559 | be stepped and/or signalled is given in the global INFERIOR_PTID. | |
3560 | This function returns non-zero iff it resumes the inferior. | |
44eaed12 | 3561 | |
506fb367 DJ |
3562 | This function issues a strict subset of all possible vCont commands at the |
3563 | moment. */ | |
44eaed12 | 3564 | |
506fb367 DJ |
3565 | static int |
3566 | remote_vcont_resume (ptid_t ptid, int step, enum target_signal siggnal) | |
3567 | { | |
3568 | struct remote_state *rs = get_remote_state (); | |
82f73884 PA |
3569 | char *p; |
3570 | char *endp; | |
44eaed12 | 3571 | |
444abaca | 3572 | if (remote_protocol_packets[PACKET_vCont].support == PACKET_SUPPORT_UNKNOWN) |
6d820c5c | 3573 | remote_vcont_probe (rs); |
44eaed12 | 3574 | |
444abaca | 3575 | if (remote_protocol_packets[PACKET_vCont].support == PACKET_DISABLE) |
6d820c5c | 3576 | return 0; |
44eaed12 | 3577 | |
82f73884 PA |
3578 | p = rs->buf; |
3579 | endp = rs->buf + get_remote_packet_size (); | |
3580 | ||
506fb367 DJ |
3581 | /* If we could generate a wider range of packets, we'd have to worry |
3582 | about overflowing BUF. Should there be a generic | |
3583 | "multi-part-packet" packet? */ | |
3584 | ||
79d7f229 | 3585 | if (ptid_equal (ptid, magic_null_ptid)) |
c906108c | 3586 | { |
79d7f229 PA |
3587 | /* MAGIC_NULL_PTID means that we don't have any active threads, |
3588 | so we don't have any TID numbers the inferior will | |
3589 | understand. Make sure to only send forms that do not specify | |
3590 | a TID. */ | |
506fb367 | 3591 | if (step && siggnal != TARGET_SIGNAL_0) |
82f73884 | 3592 | xsnprintf (p, endp - p, "vCont;S%02x", siggnal); |
506fb367 | 3593 | else if (step) |
82f73884 | 3594 | xsnprintf (p, endp - p, "vCont;s"); |
506fb367 | 3595 | else if (siggnal != TARGET_SIGNAL_0) |
82f73884 | 3596 | xsnprintf (p, endp - p, "vCont;C%02x", siggnal); |
506fb367 | 3597 | else |
82f73884 | 3598 | xsnprintf (p, endp - p, "vCont;c"); |
506fb367 | 3599 | } |
79d7f229 | 3600 | else if (ptid_equal (ptid, minus_one_ptid)) |
506fb367 DJ |
3601 | { |
3602 | /* Resume all threads, with preference for INFERIOR_PTID. */ | |
3603 | if (step && siggnal != TARGET_SIGNAL_0) | |
82f73884 PA |
3604 | { |
3605 | /* Step inferior_ptid with signal. */ | |
3606 | p += xsnprintf (p, endp - p, "vCont;S%02x:", siggnal); | |
3607 | p = write_ptid (p, endp, inferior_ptid); | |
3608 | /* And continue others. */ | |
3609 | p += xsnprintf (p, endp - p, ";c"); | |
3610 | } | |
506fb367 | 3611 | else if (step) |
82f73884 PA |
3612 | { |
3613 | /* Step inferior_ptid. */ | |
3614 | p += xsnprintf (p, endp - p, "vCont;s:"); | |
3615 | p = write_ptid (p, endp, inferior_ptid); | |
3616 | /* And continue others. */ | |
3617 | p += xsnprintf (p, endp - p, ";c"); | |
3618 | } | |
506fb367 | 3619 | else if (siggnal != TARGET_SIGNAL_0) |
82f73884 PA |
3620 | { |
3621 | /* Continue inferior_ptid with signal. */ | |
3622 | p += xsnprintf (p, endp - p, "vCont;C%02x:", siggnal); | |
3623 | p = write_ptid (p, endp, inferior_ptid); | |
3624 | /* And continue others. */ | |
3625 | p += xsnprintf (p, endp - p, ";c"); | |
3626 | } | |
506fb367 | 3627 | else |
82f73884 | 3628 | xsnprintf (p, endp - p, "vCont;c"); |
c906108c SS |
3629 | } |
3630 | else | |
506fb367 DJ |
3631 | { |
3632 | /* Scheduler locking; resume only PTID. */ | |
3633 | if (step && siggnal != TARGET_SIGNAL_0) | |
82f73884 PA |
3634 | { |
3635 | /* Step ptid with signal. */ | |
3636 | p += xsnprintf (p, endp - p, "vCont;S%02x:", siggnal); | |
3637 | p = write_ptid (p, endp, ptid); | |
3638 | } | |
506fb367 | 3639 | else if (step) |
82f73884 PA |
3640 | { |
3641 | /* Step ptid. */ | |
3642 | p += xsnprintf (p, endp - p, "vCont;s:"); | |
3643 | p = write_ptid (p, endp, ptid); | |
3644 | } | |
506fb367 | 3645 | else if (siggnal != TARGET_SIGNAL_0) |
82f73884 PA |
3646 | { |
3647 | /* Continue ptid with signal. */ | |
3648 | p += xsnprintf (p, endp - p, "vCont;C%02x:", siggnal); | |
3649 | p = write_ptid (p, endp, ptid); | |
3650 | } | |
506fb367 | 3651 | else |
82f73884 PA |
3652 | { |
3653 | /* Continue ptid. */ | |
3654 | p += xsnprintf (p, endp - p, "vCont;c:"); | |
3655 | p = write_ptid (p, endp, ptid); | |
3656 | } | |
506fb367 | 3657 | } |
c906108c | 3658 | |
82f73884 PA |
3659 | gdb_assert (strlen (rs->buf) < get_remote_packet_size ()); |
3660 | putpkt (rs->buf); | |
506fb367 | 3661 | |
74531fed PA |
3662 | if (non_stop) |
3663 | { | |
3664 | /* In non-stop, the stub replies to vCont with "OK". The stop | |
3665 | reply will be reported asynchronously by means of a `%Stop' | |
3666 | notification. */ | |
3667 | getpkt (&rs->buf, &rs->buf_size, 0); | |
3668 | if (strcmp (rs->buf, "OK") != 0) | |
3669 | error (_("Unexpected vCont reply in non-stop mode: %s"), rs->buf); | |
3670 | } | |
3671 | ||
506fb367 | 3672 | return 1; |
c906108c | 3673 | } |
43ff13b4 | 3674 | |
506fb367 DJ |
3675 | /* Tell the remote machine to resume. */ |
3676 | ||
3677 | static enum target_signal last_sent_signal = TARGET_SIGNAL_0; | |
3678 | ||
3679 | static int last_sent_step; | |
3680 | ||
43ff13b4 | 3681 | static void |
506fb367 | 3682 | remote_resume (ptid_t ptid, int step, enum target_signal siggnal) |
43ff13b4 | 3683 | { |
d01949b6 | 3684 | struct remote_state *rs = get_remote_state (); |
2e9f7625 | 3685 | char *buf; |
43ff13b4 | 3686 | |
43ff13b4 JM |
3687 | last_sent_signal = siggnal; |
3688 | last_sent_step = step; | |
3689 | ||
89be2091 DJ |
3690 | /* Update the inferior on signals to silently pass, if they've changed. */ |
3691 | remote_pass_signals (); | |
3692 | ||
506fb367 DJ |
3693 | /* The vCont packet doesn't need to specify threads via Hc. */ |
3694 | if (remote_vcont_resume (ptid, step, siggnal)) | |
75c99385 | 3695 | goto done; |
506fb367 | 3696 | |
79d7f229 PA |
3697 | /* All other supported resume packets do use Hc, so set the continue |
3698 | thread. */ | |
3699 | if (ptid_equal (ptid, minus_one_ptid)) | |
3700 | set_continue_thread (any_thread_ptid); | |
506fb367 | 3701 | else |
79d7f229 | 3702 | set_continue_thread (ptid); |
506fb367 | 3703 | |
2e9f7625 | 3704 | buf = rs->buf; |
b2175913 MS |
3705 | if (execution_direction == EXEC_REVERSE) |
3706 | { | |
3707 | /* We don't pass signals to the target in reverse exec mode. */ | |
3708 | if (info_verbose && siggnal != TARGET_SIGNAL_0) | |
3709 | warning (" - Can't pass signal %d to target in reverse: ignored.\n", | |
3710 | siggnal); | |
3711 | strcpy (buf, step ? "bs" : "bc"); | |
3712 | } | |
3713 | else if (siggnal != TARGET_SIGNAL_0) | |
43ff13b4 JM |
3714 | { |
3715 | buf[0] = step ? 'S' : 'C'; | |
c5aa993b | 3716 | buf[1] = tohex (((int) siggnal >> 4) & 0xf); |
506fb367 | 3717 | buf[2] = tohex (((int) siggnal) & 0xf); |
43ff13b4 JM |
3718 | buf[3] = '\0'; |
3719 | } | |
3720 | else | |
c5aa993b | 3721 | strcpy (buf, step ? "s" : "c"); |
506fb367 | 3722 | |
44eaed12 | 3723 | putpkt (buf); |
43ff13b4 | 3724 | |
75c99385 | 3725 | done: |
2acceee2 JM |
3726 | /* We are about to start executing the inferior, let's register it |
3727 | with the event loop. NOTE: this is the one place where all the | |
3728 | execution commands end up. We could alternatively do this in each | |
23860348 | 3729 | of the execution commands in infcmd.c. */ |
2acceee2 JM |
3730 | /* FIXME: ezannoni 1999-09-28: We may need to move this out of here |
3731 | into infcmd.c in order to allow inferior function calls to work | |
23860348 | 3732 | NOT asynchronously. */ |
362646f5 | 3733 | if (target_can_async_p ()) |
2acceee2 | 3734 | target_async (inferior_event_handler, 0); |
e24a49d8 PA |
3735 | |
3736 | /* We've just told the target to resume. The remote server will | |
3737 | wait for the inferior to stop, and then send a stop reply. In | |
3738 | the mean time, we can't start another command/query ourselves | |
74531fed PA |
3739 | because the stub wouldn't be ready to process it. This applies |
3740 | only to the base all-stop protocol, however. In non-stop (which | |
3741 | only supports vCont), the stub replies with an "OK", and is | |
3742 | immediate able to process further serial input. */ | |
3743 | if (!non_stop) | |
3744 | rs->waiting_for_stop_reply = 1; | |
43ff13b4 | 3745 | } |
c906108c | 3746 | \f |
43ff13b4 JM |
3747 | |
3748 | /* Set up the signal handler for SIGINT, while the target is | |
23860348 | 3749 | executing, ovewriting the 'regular' SIGINT signal handler. */ |
43ff13b4 | 3750 | static void |
fba45db2 | 3751 | initialize_sigint_signal_handler (void) |
43ff13b4 | 3752 | { |
43ff13b4 JM |
3753 | signal (SIGINT, handle_remote_sigint); |
3754 | } | |
3755 | ||
23860348 | 3756 | /* Signal handler for SIGINT, while the target is executing. */ |
43ff13b4 | 3757 | static void |
fba45db2 | 3758 | handle_remote_sigint (int sig) |
43ff13b4 JM |
3759 | { |
3760 | signal (sig, handle_remote_sigint_twice); | |
43ff13b4 JM |
3761 | mark_async_signal_handler_wrapper (sigint_remote_token); |
3762 | } | |
3763 | ||
3764 | /* Signal handler for SIGINT, installed after SIGINT has already been | |
3765 | sent once. It will take effect the second time that the user sends | |
23860348 | 3766 | a ^C. */ |
43ff13b4 | 3767 | static void |
fba45db2 | 3768 | handle_remote_sigint_twice (int sig) |
43ff13b4 | 3769 | { |
b803fb0f | 3770 | signal (sig, handle_remote_sigint); |
43ff13b4 JM |
3771 | mark_async_signal_handler_wrapper (sigint_remote_twice_token); |
3772 | } | |
3773 | ||
6426a772 | 3774 | /* Perform the real interruption of the target execution, in response |
23860348 | 3775 | to a ^C. */ |
c5aa993b | 3776 | static void |
fba45db2 | 3777 | async_remote_interrupt (gdb_client_data arg) |
43ff13b4 JM |
3778 | { |
3779 | if (remote_debug) | |
3780 | fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n"); | |
3781 | ||
94cc34af | 3782 | target_stop (inferior_ptid); |
43ff13b4 JM |
3783 | } |
3784 | ||
3785 | /* Perform interrupt, if the first attempt did not succeed. Just give | |
23860348 | 3786 | up on the target alltogether. */ |
2df3850c | 3787 | void |
fba45db2 | 3788 | async_remote_interrupt_twice (gdb_client_data arg) |
43ff13b4 | 3789 | { |
2df3850c JM |
3790 | if (remote_debug) |
3791 | fprintf_unfiltered (gdb_stdlog, "remote_interrupt_twice called\n"); | |
b803fb0f DJ |
3792 | |
3793 | interrupt_query (); | |
43ff13b4 JM |
3794 | } |
3795 | ||
3796 | /* Reinstall the usual SIGINT handlers, after the target has | |
23860348 | 3797 | stopped. */ |
6426a772 JM |
3798 | static void |
3799 | cleanup_sigint_signal_handler (void *dummy) | |
43ff13b4 JM |
3800 | { |
3801 | signal (SIGINT, handle_sigint); | |
43ff13b4 JM |
3802 | } |
3803 | ||
c906108c SS |
3804 | /* Send ^C to target to halt it. Target will respond, and send us a |
3805 | packet. */ | |
507f3c78 | 3806 | static void (*ofunc) (int); |
c906108c | 3807 | |
7a292a7a SS |
3808 | /* The command line interface's stop routine. This function is installed |
3809 | as a signal handler for SIGINT. The first time a user requests a | |
3810 | stop, we call remote_stop to send a break or ^C. If there is no | |
3811 | response from the target (it didn't stop when the user requested it), | |
23860348 | 3812 | we ask the user if he'd like to detach from the target. */ |
c906108c | 3813 | static void |
fba45db2 | 3814 | remote_interrupt (int signo) |
c906108c | 3815 | { |
23860348 | 3816 | /* If this doesn't work, try more severe steps. */ |
7a292a7a SS |
3817 | signal (signo, remote_interrupt_twice); |
3818 | ||
b803fb0f | 3819 | gdb_call_async_signal_handler (sigint_remote_token, 1); |
7a292a7a SS |
3820 | } |
3821 | ||
3822 | /* The user typed ^C twice. */ | |
3823 | ||
3824 | static void | |
fba45db2 | 3825 | remote_interrupt_twice (int signo) |
7a292a7a SS |
3826 | { |
3827 | signal (signo, ofunc); | |
b803fb0f | 3828 | gdb_call_async_signal_handler (sigint_remote_twice_token, 1); |
c906108c SS |
3829 | signal (signo, remote_interrupt); |
3830 | } | |
7a292a7a | 3831 | |
74531fed PA |
3832 | /* Non-stop version of target_stop. Uses `vCont;t' to stop a remote |
3833 | thread, all threads of a remote process, or all threads of all | |
3834 | processes. */ | |
3835 | ||
3836 | static void | |
3837 | remote_stop_ns (ptid_t ptid) | |
3838 | { | |
3839 | struct remote_state *rs = get_remote_state (); | |
3840 | char *p = rs->buf; | |
3841 | char *endp = rs->buf + get_remote_packet_size (); | |
3842 | struct stop_reply *reply, *next; | |
3843 | ||
3844 | if (remote_protocol_packets[PACKET_vCont].support == PACKET_SUPPORT_UNKNOWN) | |
3845 | remote_vcont_probe (rs); | |
3846 | ||
3847 | if (!rs->support_vCont_t) | |
3848 | error (_("Remote server does not support stopping threads")); | |
3849 | ||
3850 | if (ptid_equal (ptid, minus_one_ptid)) | |
3851 | p += xsnprintf (p, endp - p, "vCont;t"); | |
3852 | else | |
3853 | { | |
3854 | ptid_t nptid; | |
3855 | ||
3856 | /* Step inferior_ptid. */ | |
3857 | p += xsnprintf (p, endp - p, "vCont;t:"); | |
3858 | ||
3859 | if (ptid_is_pid (ptid)) | |
3860 | /* All (-1) threads of process. */ | |
3861 | nptid = ptid_build (ptid_get_pid (ptid), 0, -1); | |
3862 | else | |
3863 | { | |
3864 | /* Small optimization: if we already have a stop reply for | |
3865 | this thread, no use in telling the stub we want this | |
3866 | stopped. */ | |
3867 | if (peek_stop_reply (ptid)) | |
3868 | return; | |
3869 | ||
3870 | nptid = ptid; | |
3871 | } | |
3872 | ||
3873 | p = write_ptid (p, endp, nptid); | |
3874 | } | |
3875 | ||
3876 | /* In non-stop, we get an immediate OK reply. The stop reply will | |
3877 | come in asynchronously by notification. */ | |
3878 | putpkt (rs->buf); | |
3879 | getpkt (&rs->buf, &rs->buf_size, 0); | |
3880 | if (strcmp (rs->buf, "OK") != 0) | |
3881 | error (_("Stopping %s failed: %s"), target_pid_to_str (ptid), rs->buf); | |
3882 | } | |
3883 | ||
3884 | /* All-stop version of target_stop. Sends a break or a ^C to stop the | |
3885 | remote target. It is undefined which thread of which process | |
3886 | reports the stop. */ | |
3887 | ||
3888 | static void | |
3889 | remote_stop_as (ptid_t ptid) | |
3890 | { | |
3891 | struct remote_state *rs = get_remote_state (); | |
3892 | ||
3893 | /* If the inferior is stopped already, but the core didn't know | |
3894 | about it yet, just ignore the request. The cached wait status | |
3895 | will be collected in remote_wait. */ | |
3896 | if (rs->cached_wait_status) | |
3897 | return; | |
3898 | ||
3899 | /* Send a break or a ^C, depending on user preference. */ | |
3900 | ||
3901 | if (remote_break) | |
3902 | serial_send_break (remote_desc); | |
3903 | else | |
3904 | serial_write (remote_desc, "\003", 1); | |
3905 | } | |
3906 | ||
7a292a7a SS |
3907 | /* This is the generic stop called via the target vector. When a target |
3908 | interrupt is requested, either by the command line or the GUI, we | |
23860348 | 3909 | will eventually end up here. */ |
74531fed | 3910 | |
c906108c | 3911 | static void |
94cc34af | 3912 | remote_stop (ptid_t ptid) |
c906108c | 3913 | { |
7a292a7a | 3914 | if (remote_debug) |
0f71a2f6 | 3915 | fprintf_unfiltered (gdb_stdlog, "remote_stop called\n"); |
c906108c | 3916 | |
74531fed PA |
3917 | if (non_stop) |
3918 | remote_stop_ns (ptid); | |
c906108c | 3919 | else |
74531fed | 3920 | remote_stop_as (ptid); |
c906108c SS |
3921 | } |
3922 | ||
3923 | /* Ask the user what to do when an interrupt is received. */ | |
3924 | ||
3925 | static void | |
fba45db2 | 3926 | interrupt_query (void) |
c906108c SS |
3927 | { |
3928 | target_terminal_ours (); | |
3929 | ||
74531fed | 3930 | if (target_can_async_p ()) |
c906108c | 3931 | { |
74531fed | 3932 | signal (SIGINT, handle_sigint); |
315a522e | 3933 | deprecated_throw_reason (RETURN_QUIT); |
c906108c | 3934 | } |
74531fed PA |
3935 | else |
3936 | { | |
3937 | if (query ("Interrupted while waiting for the program.\n\ | |
3938 | Give up (and stop debugging it)? ")) | |
3939 | { | |
3940 | pop_target (); | |
3941 | deprecated_throw_reason (RETURN_QUIT); | |
3942 | } | |
3943 | } | |
c906108c SS |
3944 | |
3945 | target_terminal_inferior (); | |
3946 | } | |
3947 | ||
6426a772 JM |
3948 | /* Enable/disable target terminal ownership. Most targets can use |
3949 | terminal groups to control terminal ownership. Remote targets are | |
3950 | different in that explicit transfer of ownership to/from GDB/target | |
23860348 | 3951 | is required. */ |
6426a772 JM |
3952 | |
3953 | static void | |
75c99385 | 3954 | remote_terminal_inferior (void) |
6426a772 | 3955 | { |
c6ebd6cf | 3956 | if (!target_async_permitted) |
75c99385 PA |
3957 | /* Nothing to do. */ |
3958 | return; | |
3959 | ||
6426a772 JM |
3960 | /* FIXME: cagney/1999-09-27: Shouldn't need to test for |
3961 | sync_execution here. This function should only be called when | |
3962 | GDB is resuming the inferior in the forground. A background | |
3963 | resume (``run&'') should leave GDB in control of the terminal and | |
23860348 | 3964 | consequently should not call this code. */ |
6426a772 JM |
3965 | if (!sync_execution) |
3966 | return; | |
3967 | /* FIXME: cagney/1999-09-27: Closely related to the above. Make | |
3968 | calls target_terminal_*() idenpotent. The event-loop GDB talking | |
3969 | to an asynchronous target with a synchronous command calls this | |
3970 | function from both event-top.c and infrun.c/infcmd.c. Once GDB | |
3971 | stops trying to transfer the terminal to the target when it | |
3972 | shouldn't this guard can go away. */ | |
3973 | if (!remote_async_terminal_ours_p) | |
3974 | return; | |
3975 | delete_file_handler (input_fd); | |
3976 | remote_async_terminal_ours_p = 0; | |
3977 | initialize_sigint_signal_handler (); | |
3978 | /* NOTE: At this point we could also register our selves as the | |
3979 | recipient of all input. Any characters typed could then be | |
23860348 | 3980 | passed on down to the target. */ |
6426a772 JM |
3981 | } |
3982 | ||
3983 | static void | |
75c99385 | 3984 | remote_terminal_ours (void) |
6426a772 | 3985 | { |
c6ebd6cf | 3986 | if (!target_async_permitted) |
75c99385 PA |
3987 | /* Nothing to do. */ |
3988 | return; | |
3989 | ||
3990 | /* See FIXME in remote_terminal_inferior. */ | |
6426a772 JM |
3991 | if (!sync_execution) |
3992 | return; | |
75c99385 | 3993 | /* See FIXME in remote_terminal_inferior. */ |
6426a772 JM |
3994 | if (remote_async_terminal_ours_p) |
3995 | return; | |
3996 | cleanup_sigint_signal_handler (NULL); | |
3997 | add_file_handler (input_fd, stdin_event_handler, 0); | |
3998 | remote_async_terminal_ours_p = 1; | |
3999 | } | |
4000 | ||
c906108c | 4001 | void |
917317f4 | 4002 | remote_console_output (char *msg) |
c906108c SS |
4003 | { |
4004 | char *p; | |
4005 | ||
c5aa993b | 4006 | for (p = msg; p[0] && p[1]; p += 2) |
c906108c SS |
4007 | { |
4008 | char tb[2]; | |
4009 | char c = fromhex (p[0]) * 16 + fromhex (p[1]); | |
4010 | tb[0] = c; | |
4011 | tb[1] = 0; | |
43ff13b4 | 4012 | fputs_unfiltered (tb, gdb_stdtarg); |
c906108c | 4013 | } |
74531fed PA |
4014 | gdb_flush (gdb_stdtarg); |
4015 | } | |
4016 | ||
4017 | typedef struct cached_reg | |
4018 | { | |
4019 | int num; | |
4020 | gdb_byte data[MAX_REGISTER_SIZE]; | |
4021 | } cached_reg_t; | |
4022 | ||
4023 | DEF_VEC_O(cached_reg_t); | |
4024 | ||
4025 | struct stop_reply | |
4026 | { | |
4027 | struct stop_reply *next; | |
4028 | ||
4029 | ptid_t ptid; | |
4030 | ||
4031 | struct target_waitstatus ws; | |
4032 | ||
4033 | VEC(cached_reg_t) *regcache; | |
4034 | ||
4035 | int stopped_by_watchpoint_p; | |
4036 | CORE_ADDR watch_data_address; | |
4037 | ||
4038 | int solibs_changed; | |
4039 | int replay_event; | |
4040 | }; | |
4041 | ||
4042 | /* The list of already fetched and acknowledged stop events. */ | |
4043 | static struct stop_reply *stop_reply_queue; | |
4044 | ||
4045 | static struct stop_reply * | |
4046 | stop_reply_xmalloc (void) | |
4047 | { | |
4048 | struct stop_reply *r = XMALLOC (struct stop_reply); | |
4049 | r->next = NULL; | |
4050 | return r; | |
4051 | } | |
4052 | ||
4053 | static void | |
4054 | stop_reply_xfree (struct stop_reply *r) | |
4055 | { | |
4056 | if (r != NULL) | |
4057 | { | |
4058 | VEC_free (cached_reg_t, r->regcache); | |
4059 | xfree (r); | |
4060 | } | |
c906108c SS |
4061 | } |
4062 | ||
74531fed PA |
4063 | /* Discard all pending stop replies of inferior PID. If PID is -1, |
4064 | discard everything. */ | |
c906108c | 4065 | |
74531fed PA |
4066 | static void |
4067 | discard_pending_stop_replies (int pid) | |
c906108c | 4068 | { |
74531fed | 4069 | struct stop_reply *prev = NULL, *reply, *next; |
c906108c | 4070 | |
74531fed PA |
4071 | /* Discard the in-flight notification. */ |
4072 | if (pending_stop_reply != NULL | |
4073 | && (pid == -1 | |
4074 | || ptid_get_pid (pending_stop_reply->ptid) == pid)) | |
4075 | { | |
4076 | stop_reply_xfree (pending_stop_reply); | |
4077 | pending_stop_reply = NULL; | |
4078 | } | |
c906108c | 4079 | |
74531fed PA |
4080 | /* Discard the stop replies we have already pulled with |
4081 | vStopped. */ | |
4082 | for (reply = stop_reply_queue; reply; reply = next) | |
43ff13b4 | 4083 | { |
74531fed PA |
4084 | next = reply->next; |
4085 | if (pid == -1 | |
4086 | || ptid_get_pid (reply->ptid) == pid) | |
9fa2223d | 4087 | { |
74531fed PA |
4088 | if (reply == stop_reply_queue) |
4089 | stop_reply_queue = reply->next; | |
4090 | else | |
4091 | prev->next = reply->next; | |
4092 | ||
4093 | stop_reply_xfree (reply); | |
9fa2223d | 4094 | } |
74531fed PA |
4095 | else |
4096 | prev = reply; | |
c8e38a49 | 4097 | } |
74531fed | 4098 | } |
43ff13b4 | 4099 | |
74531fed | 4100 | /* Cleanup wrapper. */ |
2e9f7625 | 4101 | |
74531fed PA |
4102 | static void |
4103 | do_stop_reply_xfree (void *arg) | |
4104 | { | |
4105 | struct stop_reply *r = arg; | |
4106 | stop_reply_xfree (r); | |
4107 | } | |
75c99385 | 4108 | |
74531fed PA |
4109 | /* Look for a queued stop reply belonging to PTID. If one is found, |
4110 | remove it from the queue, and return it. Returns NULL if none is | |
4111 | found. If there are still queued events left to process, tell the | |
4112 | event loop to get back to target_wait soon. */ | |
e24a49d8 | 4113 | |
74531fed PA |
4114 | static struct stop_reply * |
4115 | queued_stop_reply (ptid_t ptid) | |
4116 | { | |
4117 | struct stop_reply *it, *prev; | |
4118 | struct stop_reply head; | |
4119 | ||
4120 | head.next = stop_reply_queue; | |
4121 | prev = &head; | |
4122 | ||
4123 | it = head.next; | |
4124 | ||
4125 | if (!ptid_equal (ptid, minus_one_ptid)) | |
4126 | for (; it; prev = it, it = it->next) | |
4127 | if (ptid_equal (ptid, it->ptid)) | |
4128 | break; | |
4129 | ||
4130 | if (it) | |
c8e38a49 | 4131 | { |
74531fed PA |
4132 | prev->next = it->next; |
4133 | it->next = NULL; | |
4134 | } | |
e24a49d8 | 4135 | |
74531fed PA |
4136 | stop_reply_queue = head.next; |
4137 | ||
4138 | if (stop_reply_queue) | |
4139 | /* There's still at least an event left. */ | |
4140 | mark_async_event_handler (remote_async_inferior_event_token); | |
4141 | ||
4142 | return it; | |
4143 | } | |
4144 | ||
4145 | /* Push a fully parsed stop reply in the stop reply queue. Since we | |
4146 | know that we now have at least one queued event left to pass to the | |
4147 | core side, tell the event loop to get back to target_wait soon. */ | |
4148 | ||
4149 | static void | |
4150 | push_stop_reply (struct stop_reply *new_event) | |
4151 | { | |
4152 | struct stop_reply *event; | |
4153 | ||
4154 | if (stop_reply_queue) | |
4155 | { | |
4156 | for (event = stop_reply_queue; | |
4157 | event && event->next; | |
4158 | event = event->next) | |
4159 | ; | |
4160 | ||
4161 | event->next = new_event; | |
4162 | } | |
4163 | else | |
4164 | stop_reply_queue = new_event; | |
4165 | ||
4166 | mark_async_event_handler (remote_async_inferior_event_token); | |
4167 | } | |
4168 | ||
4169 | /* Returns true if we have a stop reply for PTID. */ | |
4170 | ||
4171 | static int | |
4172 | peek_stop_reply (ptid_t ptid) | |
4173 | { | |
4174 | struct stop_reply *it; | |
4175 | ||
4176 | for (it = stop_reply_queue; it; it = it->next) | |
4177 | if (ptid_equal (ptid, it->ptid)) | |
4178 | { | |
4179 | if (it->ws.kind == TARGET_WAITKIND_STOPPED) | |
4180 | return 1; | |
4181 | } | |
4182 | ||
4183 | return 0; | |
4184 | } | |
4185 | ||
4186 | /* Parse the stop reply in BUF. Either the function succeeds, and the | |
4187 | result is stored in EVENT, or throws an error. */ | |
4188 | ||
4189 | static void | |
4190 | remote_parse_stop_reply (char *buf, struct stop_reply *event) | |
4191 | { | |
4192 | struct remote_arch_state *rsa = get_remote_arch_state (); | |
4193 | ULONGEST addr; | |
4194 | char *p; | |
4195 | ||
4196 | event->ptid = null_ptid; | |
4197 | event->ws.kind = TARGET_WAITKIND_IGNORE; | |
4198 | event->ws.value.integer = 0; | |
4199 | event->solibs_changed = 0; | |
4200 | event->replay_event = 0; | |
4201 | event->stopped_by_watchpoint_p = 0; | |
4202 | event->regcache = NULL; | |
4203 | ||
4204 | switch (buf[0]) | |
4205 | { | |
4206 | case 'T': /* Status with PC, SP, FP, ... */ | |
c8e38a49 PA |
4207 | { |
4208 | gdb_byte regs[MAX_REGISTER_SIZE]; | |
43ff13b4 | 4209 | |
c8e38a49 PA |
4210 | /* Expedited reply, containing Signal, {regno, reg} repeat. */ |
4211 | /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where | |
74531fed PA |
4212 | ss = signal number |
4213 | n... = register number | |
4214 | r... = register contents | |
c8e38a49 | 4215 | */ |
43ff13b4 | 4216 | |
74531fed | 4217 | p = &buf[3]; /* after Txx */ |
c8e38a49 PA |
4218 | while (*p) |
4219 | { | |
4220 | char *p1; | |
4221 | char *p_temp; | |
4222 | int fieldsize; | |
4223 | LONGEST pnum = 0; | |
43ff13b4 | 4224 | |
c8e38a49 PA |
4225 | /* If the packet contains a register number, save it in |
4226 | pnum and set p1 to point to the character following it. | |
4227 | Otherwise p1 points to p. */ | |
3c3bea1c | 4228 | |
74531fed PA |
4229 | /* If this packet is an awatch packet, don't parse the 'a' |
4230 | as a register number. */ | |
c8e38a49 PA |
4231 | |
4232 | if (strncmp (p, "awatch", strlen("awatch")) != 0) | |
4233 | { | |
4234 | /* Read the ``P'' register number. */ | |
4235 | pnum = strtol (p, &p_temp, 16); | |
4236 | p1 = p_temp; | |
4237 | } | |
4238 | else | |
4239 | p1 = p; | |
802188a7 | 4240 | |
c8e38a49 PA |
4241 | if (p1 == p) /* No register number present here. */ |
4242 | { | |
4243 | p1 = strchr (p, ':'); | |
4244 | if (p1 == NULL) | |
4245 | error (_("Malformed packet(a) (missing colon): %s\n\ | |
4246 | Packet: '%s'\n"), | |
4247 | p, buf); | |
4248 | if (strncmp (p, "thread", p1 - p) == 0) | |
74531fed | 4249 | event->ptid = read_ptid (++p1, &p); |
c8e38a49 PA |
4250 | else if ((strncmp (p, "watch", p1 - p) == 0) |
4251 | || (strncmp (p, "rwatch", p1 - p) == 0) | |
4252 | || (strncmp (p, "awatch", p1 - p) == 0)) | |
3c3bea1c | 4253 | { |
74531fed | 4254 | event->stopped_by_watchpoint_p = 1; |
c8e38a49 | 4255 | p = unpack_varlen_hex (++p1, &addr); |
74531fed | 4256 | event->watch_data_address = (CORE_ADDR) addr; |
3c3bea1c | 4257 | } |
c8e38a49 | 4258 | else if (strncmp (p, "library", p1 - p) == 0) |
43ff13b4 | 4259 | { |
c8e38a49 PA |
4260 | p1++; |
4261 | p_temp = p1; | |
4262 | while (*p_temp && *p_temp != ';') | |
4263 | p_temp++; | |
4264 | ||
74531fed | 4265 | event->solibs_changed = 1; |
c8e38a49 | 4266 | p = p_temp; |
43ff13b4 | 4267 | } |
b2175913 MS |
4268 | else if (strncmp (p, "replaylog", p1 - p) == 0) |
4269 | { | |
4270 | /* NO_HISTORY event. | |
4271 | p1 will indicate "begin" or "end", but | |
4272 | it makes no difference for now, so ignore it. */ | |
74531fed | 4273 | event->replay_event = 1; |
b2175913 MS |
4274 | p_temp = strchr (p1 + 1, ';'); |
4275 | if (p_temp) | |
4276 | p = p_temp; | |
4277 | } | |
43ff13b4 JM |
4278 | else |
4279 | { | |
c8e38a49 PA |
4280 | /* Silently skip unknown optional info. */ |
4281 | p_temp = strchr (p1 + 1, ';'); | |
4282 | if (p_temp) | |
4283 | p = p_temp; | |
4284 | } | |
4285 | } | |
4286 | else | |
4287 | { | |
4288 | struct packet_reg *reg = packet_reg_from_pnum (rsa, pnum); | |
74531fed PA |
4289 | cached_reg_t cached_reg; |
4290 | ||
4291 | cached_reg.num = reg->regnum; | |
4292 | ||
c8e38a49 | 4293 | p = p1; |
75c99385 | 4294 | |
c8e38a49 PA |
4295 | if (*p != ':') |
4296 | error (_("Malformed packet(b) (missing colon): %s\n\ | |
8a3fe4f8 | 4297 | Packet: '%s'\n"), |
c8e38a49 PA |
4298 | p, buf); |
4299 | ++p; | |
43ff13b4 | 4300 | |
c8e38a49 PA |
4301 | if (reg == NULL) |
4302 | error (_("Remote sent bad register number %s: %s\n\ | |
8a3fe4f8 | 4303 | Packet: '%s'\n"), |
c8e38a49 PA |
4304 | phex_nz (pnum, 0), p, buf); |
4305 | ||
74531fed | 4306 | fieldsize = hex2bin (p, cached_reg.data, |
c8e38a49 PA |
4307 | register_size (target_gdbarch, |
4308 | reg->regnum)); | |
4309 | p += 2 * fieldsize; | |
4310 | if (fieldsize < register_size (target_gdbarch, | |
4311 | reg->regnum)) | |
4312 | warning (_("Remote reply is too short: %s"), buf); | |
74531fed PA |
4313 | |
4314 | VEC_safe_push (cached_reg_t, event->regcache, &cached_reg); | |
43ff13b4 | 4315 | } |
c8e38a49 PA |
4316 | |
4317 | if (*p != ';') | |
4318 | error (_("Remote register badly formatted: %s\nhere: %s"), | |
4319 | buf, p); | |
4320 | ++p; | |
43ff13b4 | 4321 | } |
c8e38a49 PA |
4322 | } |
4323 | /* fall through */ | |
4324 | case 'S': /* Old style status, just signal only. */ | |
74531fed PA |
4325 | if (event->solibs_changed) |
4326 | event->ws.kind = TARGET_WAITKIND_LOADED; | |
4327 | else if (event->replay_event) | |
4328 | event->ws.kind = TARGET_WAITKIND_NO_HISTORY; | |
c8e38a49 PA |
4329 | else |
4330 | { | |
74531fed PA |
4331 | event->ws.kind = TARGET_WAITKIND_STOPPED; |
4332 | event->ws.value.sig = (enum target_signal) | |
c8e38a49 PA |
4333 | (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))); |
4334 | } | |
4335 | break; | |
4336 | case 'W': /* Target exited. */ | |
4337 | case 'X': | |
4338 | { | |
4339 | char *p; | |
4340 | int pid; | |
4341 | ULONGEST value; | |
82f73884 | 4342 | |
c8e38a49 PA |
4343 | /* GDB used to accept only 2 hex chars here. Stubs should |
4344 | only send more if they detect GDB supports multi-process | |
4345 | support. */ | |
4346 | p = unpack_varlen_hex (&buf[1], &value); | |
82f73884 | 4347 | |
c8e38a49 PA |
4348 | if (buf[0] == 'W') |
4349 | { | |
4350 | /* The remote process exited. */ | |
74531fed PA |
4351 | event->ws.kind = TARGET_WAITKIND_EXITED; |
4352 | event->ws.value.integer = value; | |
c8e38a49 PA |
4353 | } |
4354 | else | |
4355 | { | |
4356 | /* The remote process exited with a signal. */ | |
74531fed PA |
4357 | event->ws.kind = TARGET_WAITKIND_SIGNALLED; |
4358 | event->ws.value.sig = (enum target_signal) value; | |
c8e38a49 | 4359 | } |
82f73884 | 4360 | |
c8e38a49 PA |
4361 | /* If no process is specified, assume inferior_ptid. */ |
4362 | pid = ptid_get_pid (inferior_ptid); | |
4363 | if (*p == '\0') | |
4364 | ; | |
4365 | else if (*p == ';') | |
4366 | { | |
4367 | p++; | |
4368 | ||
4369 | if (p == '\0') | |
82f73884 | 4370 | ; |
c8e38a49 PA |
4371 | else if (strncmp (p, |
4372 | "process:", sizeof ("process:") - 1) == 0) | |
82f73884 | 4373 | { |
c8e38a49 PA |
4374 | ULONGEST upid; |
4375 | p += sizeof ("process:") - 1; | |
4376 | unpack_varlen_hex (p, &upid); | |
4377 | pid = upid; | |
82f73884 PA |
4378 | } |
4379 | else | |
4380 | error (_("unknown stop reply packet: %s"), buf); | |
43ff13b4 | 4381 | } |
c8e38a49 PA |
4382 | else |
4383 | error (_("unknown stop reply packet: %s"), buf); | |
74531fed PA |
4384 | event->ptid = pid_to_ptid (pid); |
4385 | } | |
4386 | break; | |
4387 | } | |
4388 | ||
4389 | if (non_stop && ptid_equal (event->ptid, null_ptid)) | |
4390 | error (_("No process or thread specified in stop reply: %s"), buf); | |
4391 | } | |
4392 | ||
4393 | /* When the stub wants to tell GDB about a new stop reply, it sends a | |
4394 | stop notification (%Stop). Those can come it at any time, hence, | |
4395 | we have to make sure that any pending putpkt/getpkt sequence we're | |
4396 | making is finished, before querying the stub for more events with | |
4397 | vStopped. E.g., if we started a vStopped sequence immediatelly | |
4398 | upon receiving the %Stop notification, something like this could | |
4399 | happen: | |
4400 | ||
4401 | 1.1) --> Hg 1 | |
4402 | 1.2) <-- OK | |
4403 | 1.3) --> g | |
4404 | 1.4) <-- %Stop | |
4405 | 1.5) --> vStopped | |
4406 | 1.6) <-- (registers reply to step #1.3) | |
4407 | ||
4408 | Obviously, the reply in step #1.6 would be unexpected to a vStopped | |
4409 | query. | |
4410 | ||
4411 | To solve this, whenever we parse a %Stop notification sucessfully, | |
4412 | we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on | |
4413 | doing whatever we were doing: | |
4414 | ||
4415 | 2.1) --> Hg 1 | |
4416 | 2.2) <-- OK | |
4417 | 2.3) --> g | |
4418 | 2.4) <-- %Stop | |
4419 | <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN> | |
4420 | 2.5) <-- (registers reply to step #2.3) | |
4421 | ||
4422 | Eventualy after step #2.5, we return to the event loop, which | |
4423 | notices there's an event on the | |
4424 | REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the | |
4425 | associated callback --- the function below. At this point, we're | |
4426 | always safe to start a vStopped sequence. : | |
4427 | ||
4428 | 2.6) --> vStopped | |
4429 | 2.7) <-- T05 thread:2 | |
4430 | 2.8) --> vStopped | |
4431 | 2.9) --> OK | |
4432 | */ | |
4433 | ||
4434 | static void | |
4435 | remote_get_pending_stop_replies (void) | |
4436 | { | |
4437 | struct remote_state *rs = get_remote_state (); | |
4438 | int ret; | |
4439 | ||
4440 | if (pending_stop_reply) | |
4441 | { | |
4442 | /* acknowledge */ | |
4443 | putpkt ("vStopped"); | |
4444 | ||
4445 | /* Now we can rely on it. */ | |
4446 | push_stop_reply (pending_stop_reply); | |
4447 | pending_stop_reply = NULL; | |
4448 | ||
4449 | while (1) | |
4450 | { | |
4451 | getpkt (&rs->buf, &rs->buf_size, 0); | |
4452 | if (strcmp (rs->buf, "OK") == 0) | |
4453 | break; | |
4454 | else | |
4455 | { | |
4456 | struct cleanup *old_chain; | |
4457 | struct stop_reply *stop_reply = stop_reply_xmalloc (); | |
4458 | ||
4459 | old_chain = make_cleanup (do_stop_reply_xfree, stop_reply); | |
4460 | remote_parse_stop_reply (rs->buf, stop_reply); | |
4461 | ||
4462 | /* acknowledge */ | |
4463 | putpkt ("vStopped"); | |
4464 | ||
4465 | if (stop_reply->ws.kind != TARGET_WAITKIND_IGNORE) | |
4466 | { | |
4467 | /* Now we can rely on it. */ | |
4468 | discard_cleanups (old_chain); | |
4469 | push_stop_reply (stop_reply); | |
4470 | } | |
4471 | else | |
4472 | /* We got an unknown stop reply. */ | |
4473 | do_cleanups (old_chain); | |
4474 | } | |
4475 | } | |
4476 | } | |
4477 | } | |
4478 | ||
4479 | ||
4480 | /* Called when it is decided that STOP_REPLY holds the info of the | |
4481 | event that is to be returned to the core. This function always | |
4482 | destroys STOP_REPLY. */ | |
4483 | ||
4484 | static ptid_t | |
4485 | process_stop_reply (struct stop_reply *stop_reply, | |
4486 | struct target_waitstatus *status) | |
4487 | { | |
4488 | ptid_t ptid; | |
4489 | ||
4490 | *status = stop_reply->ws; | |
4491 | ptid = stop_reply->ptid; | |
4492 | ||
4493 | /* If no thread/process was reported by the stub, assume the current | |
4494 | inferior. */ | |
4495 | if (ptid_equal (ptid, null_ptid)) | |
4496 | ptid = inferior_ptid; | |
4497 | ||
4498 | if (status->kind == TARGET_WAITKIND_EXITED | |
4499 | || status->kind == TARGET_WAITKIND_SIGNALLED) | |
4500 | { | |
4501 | int pid = ptid_get_pid (ptid); | |
4502 | delete_inferior (pid); | |
4503 | } | |
4504 | else | |
4505 | notice_new_inferiors (ptid); | |
4506 | ||
4507 | /* Expedited registers. */ | |
4508 | if (stop_reply->regcache) | |
4509 | { | |
4510 | cached_reg_t *reg; | |
4511 | int ix; | |
4512 | ||
4513 | for (ix = 0; | |
4514 | VEC_iterate(cached_reg_t, stop_reply->regcache, ix, reg); | |
4515 | ix++) | |
4516 | regcache_raw_supply (get_thread_regcache (ptid), | |
4517 | reg->num, reg->data); | |
4518 | VEC_free (cached_reg_t, stop_reply->regcache); | |
4519 | } | |
4520 | ||
4521 | remote_stopped_by_watchpoint_p = stop_reply->stopped_by_watchpoint_p; | |
4522 | remote_watch_data_address = stop_reply->watch_data_address; | |
4523 | ||
4524 | stop_reply_xfree (stop_reply); | |
4525 | return ptid; | |
4526 | } | |
4527 | ||
4528 | /* The non-stop mode version of target_wait. */ | |
4529 | ||
4530 | static ptid_t | |
4531 | remote_wait_ns (ptid_t ptid, struct target_waitstatus *status) | |
4532 | { | |
4533 | struct remote_state *rs = get_remote_state (); | |
4534 | struct remote_arch_state *rsa = get_remote_arch_state (); | |
4535 | ptid_t event_ptid = null_ptid; | |
4536 | struct stop_reply *stop_reply; | |
4537 | int ret; | |
4538 | ||
4539 | /* If in non-stop mode, get out of getpkt even if a | |
4540 | notification is received. */ | |
4541 | ||
4542 | ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size, | |
4543 | 0 /* forever */); | |
4544 | while (1) | |
4545 | { | |
4546 | if (ret != -1) | |
4547 | switch (rs->buf[0]) | |
4548 | { | |
4549 | case 'E': /* Error of some sort. */ | |
4550 | /* We're out of sync with the target now. Did it continue | |
4551 | or not? We can't tell which thread it was in non-stop, | |
4552 | so just ignore this. */ | |
4553 | warning (_("Remote failure reply: %s"), rs->buf); | |
4554 | break; | |
4555 | case 'O': /* Console output. */ | |
4556 | remote_console_output (rs->buf + 1); | |
4557 | break; | |
4558 | default: | |
4559 | warning (_("Invalid remote reply: %s"), rs->buf); | |
4560 | break; | |
4561 | } | |
4562 | ||
4563 | /* Acknowledge a pending stop reply that may have arrived in the | |
4564 | mean time. */ | |
4565 | if (pending_stop_reply != NULL) | |
4566 | remote_get_pending_stop_replies (); | |
4567 | ||
4568 | /* If indeed we noticed a stop reply, we're done. */ | |
4569 | stop_reply = queued_stop_reply (ptid); | |
4570 | if (stop_reply != NULL) | |
4571 | return process_stop_reply (stop_reply, status); | |
4572 | ||
4573 | /* Still no event. If we're in asynchronous mode, then just | |
4574 | return to the event loop. */ | |
4575 | if (remote_is_async_p ()) | |
4576 | { | |
4577 | status->kind = TARGET_WAITKIND_IGNORE; | |
4578 | return minus_one_ptid; | |
4579 | } | |
4580 | ||
4581 | /* Otherwise, asynchronous mode is masked, so do a blocking | |
4582 | wait. */ | |
4583 | ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size, | |
4584 | 1 /* forever */); | |
4585 | } | |
4586 | } | |
4587 | ||
4588 | /* Wait until the remote machine stops, then return, storing status in | |
4589 | STATUS just as `wait' would. */ | |
4590 | ||
4591 | static ptid_t | |
4592 | remote_wait_as (ptid_t ptid, struct target_waitstatus *status) | |
4593 | { | |
4594 | struct remote_state *rs = get_remote_state (); | |
4595 | struct remote_arch_state *rsa = get_remote_arch_state (); | |
4596 | ptid_t event_ptid = null_ptid; | |
4597 | ULONGEST addr; | |
4598 | int solibs_changed = 0; | |
4599 | char *buf, *p; | |
4600 | struct stop_reply *stop_reply; | |
4601 | ||
4602 | status->kind = TARGET_WAITKIND_IGNORE; | |
4603 | status->value.integer = 0; | |
4604 | ||
4605 | stop_reply = queued_stop_reply (ptid); | |
4606 | if (stop_reply != NULL) | |
4607 | return process_stop_reply (stop_reply, status); | |
4608 | ||
4609 | if (rs->cached_wait_status) | |
4610 | /* Use the cached wait status, but only once. */ | |
4611 | rs->cached_wait_status = 0; | |
4612 | else | |
4613 | { | |
4614 | int ret; | |
4615 | ||
4616 | if (!target_is_async_p ()) | |
4617 | { | |
4618 | ofunc = signal (SIGINT, remote_interrupt); | |
4619 | /* If the user hit C-c before this packet, or between packets, | |
4620 | pretend that it was hit right here. */ | |
4621 | if (quit_flag) | |
4622 | { | |
4623 | quit_flag = 0; | |
4624 | remote_interrupt (SIGINT); | |
4625 | } | |
4626 | } | |
4627 | ||
4628 | /* FIXME: cagney/1999-09-27: If we're in async mode we should | |
4629 | _never_ wait for ever -> test on target_is_async_p(). | |
4630 | However, before we do that we need to ensure that the caller | |
4631 | knows how to take the target into/out of async mode. */ | |
4632 | ret = getpkt_sane (&rs->buf, &rs->buf_size, wait_forever_enabled_p); | |
4633 | if (!target_is_async_p ()) | |
4634 | signal (SIGINT, ofunc); | |
4635 | } | |
4636 | ||
4637 | buf = rs->buf; | |
4638 | ||
4639 | remote_stopped_by_watchpoint_p = 0; | |
4640 | ||
4641 | /* We got something. */ | |
4642 | rs->waiting_for_stop_reply = 0; | |
4643 | ||
4644 | switch (buf[0]) | |
4645 | { | |
4646 | case 'E': /* Error of some sort. */ | |
4647 | /* We're out of sync with the target now. Did it continue or | |
4648 | not? Not is more likely, so report a stop. */ | |
4649 | warning (_("Remote failure reply: %s"), buf); | |
4650 | status->kind = TARGET_WAITKIND_STOPPED; | |
4651 | status->value.sig = TARGET_SIGNAL_0; | |
4652 | break; | |
4653 | case 'F': /* File-I/O request. */ | |
4654 | remote_fileio_request (buf); | |
4655 | break; | |
4656 | case 'T': case 'S': case 'X': case 'W': | |
4657 | { | |
4658 | struct stop_reply *stop_reply; | |
4659 | struct cleanup *old_chain; | |
4660 | ||
4661 | stop_reply = stop_reply_xmalloc (); | |
4662 | old_chain = make_cleanup (do_stop_reply_xfree, stop_reply); | |
4663 | remote_parse_stop_reply (buf, stop_reply); | |
4664 | discard_cleanups (old_chain); | |
4665 | event_ptid = process_stop_reply (stop_reply, status); | |
c8e38a49 PA |
4666 | break; |
4667 | } | |
4668 | case 'O': /* Console output. */ | |
4669 | remote_console_output (buf + 1); | |
e24a49d8 | 4670 | |
c8e38a49 PA |
4671 | /* The target didn't really stop; keep waiting. */ |
4672 | rs->waiting_for_stop_reply = 1; | |
e24a49d8 | 4673 | |
c8e38a49 PA |
4674 | break; |
4675 | case '\0': | |
4676 | if (last_sent_signal != TARGET_SIGNAL_0) | |
4677 | { | |
4678 | /* Zero length reply means that we tried 'S' or 'C' and the | |
4679 | remote system doesn't support it. */ | |
4680 | target_terminal_ours_for_output (); | |
4681 | printf_filtered | |
4682 | ("Can't send signals to this remote system. %s not sent.\n", | |
4683 | target_signal_to_name (last_sent_signal)); | |
4684 | last_sent_signal = TARGET_SIGNAL_0; | |
4685 | target_terminal_inferior (); | |
4686 | ||
4687 | strcpy ((char *) buf, last_sent_step ? "s" : "c"); | |
4688 | putpkt ((char *) buf); | |
4689 | ||
4690 | /* We just told the target to resume, so a stop reply is in | |
4691 | order. */ | |
e24a49d8 | 4692 | rs->waiting_for_stop_reply = 1; |
c8e38a49 | 4693 | break; |
43ff13b4 | 4694 | } |
c8e38a49 PA |
4695 | /* else fallthrough */ |
4696 | default: | |
4697 | warning (_("Invalid remote reply: %s"), buf); | |
4698 | /* Keep waiting. */ | |
4699 | rs->waiting_for_stop_reply = 1; | |
4700 | break; | |
43ff13b4 | 4701 | } |
c8e38a49 | 4702 | |
c8e38a49 | 4703 | if (status->kind == TARGET_WAITKIND_IGNORE) |
74531fed | 4704 | /* Nothing interesting happened. */ |
c8e38a49 | 4705 | return minus_one_ptid; |
74531fed PA |
4706 | else if (status->kind != TARGET_WAITKIND_EXITED |
4707 | && status->kind != TARGET_WAITKIND_SIGNALLED) | |
82f73884 PA |
4708 | { |
4709 | if (!ptid_equal (event_ptid, null_ptid)) | |
4710 | record_currthread (event_ptid); | |
4711 | else | |
4712 | event_ptid = inferior_ptid; | |
43ff13b4 | 4713 | } |
74531fed PA |
4714 | else |
4715 | /* A process exit. Invalidate our notion of current thread. */ | |
4716 | record_currthread (minus_one_ptid); | |
79d7f229 | 4717 | |
82f73884 | 4718 | return event_ptid; |
43ff13b4 JM |
4719 | } |
4720 | ||
74531fed PA |
4721 | /* Wait until the remote machine stops, then return, storing status in |
4722 | STATUS just as `wait' would. */ | |
4723 | ||
c8e38a49 PA |
4724 | static ptid_t |
4725 | remote_wait (ptid_t ptid, struct target_waitstatus *status) | |
4726 | { | |
4727 | ptid_t event_ptid; | |
4728 | ||
74531fed PA |
4729 | if (non_stop) |
4730 | event_ptid = remote_wait_ns (ptid, status); | |
4731 | else | |
4732 | { | |
4733 | /* In synchronous mode, keep waiting until the target stops. In | |
4734 | asynchronous mode, always return to the event loop. */ | |
4735 | ||
4736 | do | |
4737 | { | |
4738 | event_ptid = remote_wait_as (ptid, status); | |
4739 | } | |
4740 | while (status->kind == TARGET_WAITKIND_IGNORE | |
4741 | && !target_can_async_p ()); | |
4742 | } | |
c8e38a49 | 4743 | |
74531fed | 4744 | if (target_can_async_p ()) |
c8e38a49 | 4745 | { |
74531fed PA |
4746 | /* If there are are events left in the queue tell the event loop |
4747 | to return here. */ | |
4748 | if (stop_reply_queue) | |
4749 | mark_async_event_handler (remote_async_inferior_event_token); | |
c8e38a49 | 4750 | } |
c8e38a49 PA |
4751 | |
4752 | return event_ptid; | |
4753 | } | |
4754 | ||
74ca34ce | 4755 | /* Fetch a single register using a 'p' packet. */ |
c906108c | 4756 | |
b96ec7ac | 4757 | static int |
56be3814 | 4758 | fetch_register_using_p (struct regcache *regcache, struct packet_reg *reg) |
b96ec7ac AC |
4759 | { |
4760 | struct remote_state *rs = get_remote_state (); | |
2e9f7625 | 4761 | char *buf, *p; |
b96ec7ac AC |
4762 | char regp[MAX_REGISTER_SIZE]; |
4763 | int i; | |
4764 | ||
74ca34ce DJ |
4765 | if (remote_protocol_packets[PACKET_p].support == PACKET_DISABLE) |
4766 | return 0; | |
4767 | ||
4768 | if (reg->pnum == -1) | |
4769 | return 0; | |
4770 | ||
2e9f7625 | 4771 | p = rs->buf; |
fcad0fa4 | 4772 | *p++ = 'p'; |
74ca34ce | 4773 | p += hexnumstr (p, reg->pnum); |
fcad0fa4 | 4774 | *p++ = '\0'; |
6d820c5c | 4775 | remote_send (&rs->buf, &rs->buf_size); |
3f9a994c | 4776 | |
2e9f7625 DJ |
4777 | buf = rs->buf; |
4778 | ||
74ca34ce DJ |
4779 | switch (packet_ok (buf, &remote_protocol_packets[PACKET_p])) |
4780 | { | |
4781 | case PACKET_OK: | |
4782 | break; | |
4783 | case PACKET_UNKNOWN: | |
4784 | return 0; | |
4785 | case PACKET_ERROR: | |
4786 | error (_("Could not fetch register \"%s\""), | |
4a22f64d | 4787 | gdbarch_register_name (get_regcache_arch (regcache), reg->regnum)); |
74ca34ce | 4788 | } |
3f9a994c JB |
4789 | |
4790 | /* If this register is unfetchable, tell the regcache. */ | |
4791 | if (buf[0] == 'x') | |
8480adf2 | 4792 | { |
56be3814 | 4793 | regcache_raw_supply (regcache, reg->regnum, NULL); |
8480adf2 | 4794 | return 1; |
b96ec7ac | 4795 | } |
b96ec7ac | 4796 | |
3f9a994c JB |
4797 | /* Otherwise, parse and supply the value. */ |
4798 | p = buf; | |
4799 | i = 0; | |
4800 | while (p[0] != 0) | |
4801 | { | |
4802 | if (p[1] == 0) | |
74ca34ce | 4803 | error (_("fetch_register_using_p: early buf termination")); |
3f9a994c JB |
4804 | |
4805 | regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]); | |
4806 | p += 2; | |
4807 | } | |
56be3814 | 4808 | regcache_raw_supply (regcache, reg->regnum, regp); |
3f9a994c | 4809 | return 1; |
b96ec7ac AC |
4810 | } |
4811 | ||
74ca34ce DJ |
4812 | /* Fetch the registers included in the target's 'g' packet. */ |
4813 | ||
29709017 DJ |
4814 | static int |
4815 | send_g_packet (void) | |
c906108c | 4816 | { |
d01949b6 | 4817 | struct remote_state *rs = get_remote_state (); |
74ca34ce | 4818 | int i, buf_len; |
c906108c | 4819 | char *p; |
74ca34ce | 4820 | char *regs; |
c906108c | 4821 | |
74ca34ce DJ |
4822 | sprintf (rs->buf, "g"); |
4823 | remote_send (&rs->buf, &rs->buf_size); | |
c906108c | 4824 | |
29709017 DJ |
4825 | /* We can get out of synch in various cases. If the first character |
4826 | in the buffer is not a hex character, assume that has happened | |
4827 | and try to fetch another packet to read. */ | |
4828 | while ((rs->buf[0] < '0' || rs->buf[0] > '9') | |
4829 | && (rs->buf[0] < 'A' || rs->buf[0] > 'F') | |
4830 | && (rs->buf[0] < 'a' || rs->buf[0] > 'f') | |
4831 | && rs->buf[0] != 'x') /* New: unavailable register value. */ | |
4832 | { | |
4833 | if (remote_debug) | |
4834 | fprintf_unfiltered (gdb_stdlog, | |
4835 | "Bad register packet; fetching a new packet\n"); | |
4836 | getpkt (&rs->buf, &rs->buf_size, 0); | |
4837 | } | |
4838 | ||
74ca34ce DJ |
4839 | buf_len = strlen (rs->buf); |
4840 | ||
4841 | /* Sanity check the received packet. */ | |
4842 | if (buf_len % 2 != 0) | |
4843 | error (_("Remote 'g' packet reply is of odd length: %s"), rs->buf); | |
29709017 DJ |
4844 | |
4845 | return buf_len / 2; | |
4846 | } | |
4847 | ||
4848 | static void | |
56be3814 | 4849 | process_g_packet (struct regcache *regcache) |
29709017 | 4850 | { |
4a22f64d | 4851 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
29709017 DJ |
4852 | struct remote_state *rs = get_remote_state (); |
4853 | struct remote_arch_state *rsa = get_remote_arch_state (); | |
4854 | int i, buf_len; | |
4855 | char *p; | |
4856 | char *regs; | |
4857 | ||
4858 | buf_len = strlen (rs->buf); | |
4859 | ||
4860 | /* Further sanity checks, with knowledge of the architecture. */ | |
74ca34ce DJ |
4861 | if (buf_len > 2 * rsa->sizeof_g_packet) |
4862 | error (_("Remote 'g' packet reply is too long: %s"), rs->buf); | |
4863 | ||
4864 | /* Save the size of the packet sent to us by the target. It is used | |
4865 | as a heuristic when determining the max size of packets that the | |
4866 | target can safely receive. */ | |
4867 | if (rsa->actual_register_packet_size == 0) | |
4868 | rsa->actual_register_packet_size = buf_len; | |
4869 | ||
4870 | /* If this is smaller than we guessed the 'g' packet would be, | |
4871 | update our records. A 'g' reply that doesn't include a register's | |
4872 | value implies either that the register is not available, or that | |
4873 | the 'p' packet must be used. */ | |
4874 | if (buf_len < 2 * rsa->sizeof_g_packet) | |
b323314b | 4875 | { |
74ca34ce DJ |
4876 | rsa->sizeof_g_packet = buf_len / 2; |
4877 | ||
4a22f64d | 4878 | for (i = 0; i < gdbarch_num_regs (gdbarch); i++) |
b96ec7ac | 4879 | { |
74ca34ce DJ |
4880 | if (rsa->regs[i].pnum == -1) |
4881 | continue; | |
4882 | ||
4883 | if (rsa->regs[i].offset >= rsa->sizeof_g_packet) | |
4884 | rsa->regs[i].in_g_packet = 0; | |
b96ec7ac | 4885 | else |
74ca34ce | 4886 | rsa->regs[i].in_g_packet = 1; |
b96ec7ac | 4887 | } |
74ca34ce | 4888 | } |
b323314b | 4889 | |
74ca34ce | 4890 | regs = alloca (rsa->sizeof_g_packet); |
c906108c SS |
4891 | |
4892 | /* Unimplemented registers read as all bits zero. */ | |
ea9c271d | 4893 | memset (regs, 0, rsa->sizeof_g_packet); |
c906108c | 4894 | |
c906108c SS |
4895 | /* Reply describes registers byte by byte, each byte encoded as two |
4896 | hex characters. Suck them all up, then supply them to the | |
4897 | register cacheing/storage mechanism. */ | |
4898 | ||
74ca34ce | 4899 | p = rs->buf; |
ea9c271d | 4900 | for (i = 0; i < rsa->sizeof_g_packet; i++) |
c906108c | 4901 | { |
74ca34ce DJ |
4902 | if (p[0] == 0 || p[1] == 0) |
4903 | /* This shouldn't happen - we adjusted sizeof_g_packet above. */ | |
4904 | internal_error (__FILE__, __LINE__, | |
4905 | "unexpected end of 'g' packet reply"); | |
4906 | ||
c906108c | 4907 | if (p[0] == 'x' && p[1] == 'x') |
c5aa993b | 4908 | regs[i] = 0; /* 'x' */ |
c906108c SS |
4909 | else |
4910 | regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); | |
4911 | p += 2; | |
4912 | } | |
4913 | ||
ad10f812 | 4914 | { |
b323314b | 4915 | int i; |
4a22f64d | 4916 | for (i = 0; i < gdbarch_num_regs (gdbarch); i++) |
ad10f812 | 4917 | { |
ea9c271d | 4918 | struct packet_reg *r = &rsa->regs[i]; |
b323314b AC |
4919 | if (r->in_g_packet) |
4920 | { | |
74ca34ce DJ |
4921 | if (r->offset * 2 >= strlen (rs->buf)) |
4922 | /* This shouldn't happen - we adjusted in_g_packet above. */ | |
4923 | internal_error (__FILE__, __LINE__, | |
4924 | "unexpected end of 'g' packet reply"); | |
4925 | else if (rs->buf[r->offset * 2] == 'x') | |
8ccc1287 | 4926 | { |
74ca34ce | 4927 | gdb_assert (r->offset * 2 < strlen (rs->buf)); |
8ccc1287 AC |
4928 | /* The register isn't available, mark it as such (at |
4929 | the same time setting the value to zero). */ | |
56be3814 | 4930 | regcache_raw_supply (regcache, r->regnum, NULL); |
8ccc1287 AC |
4931 | } |
4932 | else | |
56be3814 | 4933 | regcache_raw_supply (regcache, r->regnum, |
8ccc1287 | 4934 | regs + r->offset); |
b323314b | 4935 | } |
ad10f812 AC |
4936 | } |
4937 | } | |
c906108c SS |
4938 | } |
4939 | ||
29709017 | 4940 | static void |
56be3814 | 4941 | fetch_registers_using_g (struct regcache *regcache) |
29709017 DJ |
4942 | { |
4943 | send_g_packet (); | |
56be3814 | 4944 | process_g_packet (regcache); |
29709017 DJ |
4945 | } |
4946 | ||
74ca34ce | 4947 | static void |
56be3814 | 4948 | remote_fetch_registers (struct regcache *regcache, int regnum) |
74ca34ce DJ |
4949 | { |
4950 | struct remote_state *rs = get_remote_state (); | |
4951 | struct remote_arch_state *rsa = get_remote_arch_state (); | |
4952 | int i; | |
4953 | ||
79d7f229 | 4954 | set_general_thread (inferior_ptid); |
74ca34ce DJ |
4955 | |
4956 | if (regnum >= 0) | |
4957 | { | |
4958 | struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum); | |
4959 | gdb_assert (reg != NULL); | |
4960 | ||
4961 | /* If this register might be in the 'g' packet, try that first - | |
4962 | we are likely to read more than one register. If this is the | |
4963 | first 'g' packet, we might be overly optimistic about its | |
4964 | contents, so fall back to 'p'. */ | |
4965 | if (reg->in_g_packet) | |
4966 | { | |
56be3814 | 4967 | fetch_registers_using_g (regcache); |
74ca34ce DJ |
4968 | if (reg->in_g_packet) |
4969 | return; | |
4970 | } | |
4971 | ||
56be3814 | 4972 | if (fetch_register_using_p (regcache, reg)) |
74ca34ce DJ |
4973 | return; |
4974 | ||
4975 | /* This register is not available. */ | |
56be3814 | 4976 | regcache_raw_supply (regcache, reg->regnum, NULL); |
74ca34ce DJ |
4977 | |
4978 | return; | |
4979 | } | |
4980 | ||
56be3814 | 4981 | fetch_registers_using_g (regcache); |
74ca34ce | 4982 | |
4a22f64d | 4983 | for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++) |
74ca34ce | 4984 | if (!rsa->regs[i].in_g_packet) |
56be3814 | 4985 | if (!fetch_register_using_p (regcache, &rsa->regs[i])) |
74ca34ce DJ |
4986 | { |
4987 | /* This register is not available. */ | |
56be3814 | 4988 | regcache_raw_supply (regcache, i, NULL); |
74ca34ce DJ |
4989 | } |
4990 | } | |
4991 | ||
c906108c SS |
4992 | /* Prepare to store registers. Since we may send them all (using a |
4993 | 'G' request), we have to read out the ones we don't want to change | |
4994 | first. */ | |
4995 | ||
c5aa993b | 4996 | static void |
316f2060 | 4997 | remote_prepare_to_store (struct regcache *regcache) |
c906108c | 4998 | { |
ea9c271d | 4999 | struct remote_arch_state *rsa = get_remote_arch_state (); |
cf0e1e0d | 5000 | int i; |
cfd77fa1 | 5001 | gdb_byte buf[MAX_REGISTER_SIZE]; |
cf0e1e0d | 5002 | |
c906108c | 5003 | /* Make sure the entire registers array is valid. */ |
444abaca | 5004 | switch (remote_protocol_packets[PACKET_P].support) |
5a2468f5 JM |
5005 | { |
5006 | case PACKET_DISABLE: | |
5007 | case PACKET_SUPPORT_UNKNOWN: | |
cf0e1e0d | 5008 | /* Make sure all the necessary registers are cached. */ |
4a22f64d | 5009 | for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++) |
ea9c271d | 5010 | if (rsa->regs[i].in_g_packet) |
316f2060 | 5011 | regcache_raw_read (regcache, rsa->regs[i].regnum, buf); |
5a2468f5 JM |
5012 | break; |
5013 | case PACKET_ENABLE: | |
5014 | break; | |
5015 | } | |
5016 | } | |
5017 | ||
ad10f812 | 5018 | /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF |
23860348 | 5019 | packet was not recognized. */ |
5a2468f5 JM |
5020 | |
5021 | static int | |
56be3814 | 5022 | store_register_using_P (const struct regcache *regcache, struct packet_reg *reg) |
5a2468f5 | 5023 | { |
4a22f64d | 5024 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
d01949b6 | 5025 | struct remote_state *rs = get_remote_state (); |
ea9c271d | 5026 | struct remote_arch_state *rsa = get_remote_arch_state (); |
5a2468f5 | 5027 | /* Try storing a single register. */ |
6d820c5c | 5028 | char *buf = rs->buf; |
cfd77fa1 | 5029 | gdb_byte regp[MAX_REGISTER_SIZE]; |
5a2468f5 | 5030 | char *p; |
5a2468f5 | 5031 | |
74ca34ce DJ |
5032 | if (remote_protocol_packets[PACKET_P].support == PACKET_DISABLE) |
5033 | return 0; | |
5034 | ||
5035 | if (reg->pnum == -1) | |
5036 | return 0; | |
5037 | ||
ea9c271d | 5038 | xsnprintf (buf, get_remote_packet_size (), "P%s=", phex_nz (reg->pnum, 0)); |
5a2468f5 | 5039 | p = buf + strlen (buf); |
56be3814 | 5040 | regcache_raw_collect (regcache, reg->regnum, regp); |
4a22f64d | 5041 | bin2hex (regp, p, register_size (gdbarch, reg->regnum)); |
6d820c5c | 5042 | remote_send (&rs->buf, &rs->buf_size); |
5a2468f5 | 5043 | |
74ca34ce DJ |
5044 | switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_P])) |
5045 | { | |
5046 | case PACKET_OK: | |
5047 | return 1; | |
5048 | case PACKET_ERROR: | |
5049 | error (_("Could not write register \"%s\""), | |
4a22f64d | 5050 | gdbarch_register_name (gdbarch, reg->regnum)); |
74ca34ce DJ |
5051 | case PACKET_UNKNOWN: |
5052 | return 0; | |
5053 | default: | |
5054 | internal_error (__FILE__, __LINE__, _("Bad result from packet_ok")); | |
5055 | } | |
c906108c SS |
5056 | } |
5057 | ||
23860348 MS |
5058 | /* Store register REGNUM, or all registers if REGNUM == -1, from the |
5059 | contents of the register cache buffer. FIXME: ignores errors. */ | |
c906108c SS |
5060 | |
5061 | static void | |
56be3814 | 5062 | store_registers_using_G (const struct regcache *regcache) |
c906108c | 5063 | { |
d01949b6 | 5064 | struct remote_state *rs = get_remote_state (); |
ea9c271d | 5065 | struct remote_arch_state *rsa = get_remote_arch_state (); |
cfd77fa1 | 5066 | gdb_byte *regs; |
c906108c SS |
5067 | char *p; |
5068 | ||
193cb69f AC |
5069 | /* Extract all the registers in the regcache copying them into a |
5070 | local buffer. */ | |
5071 | { | |
b323314b | 5072 | int i; |
ea9c271d DJ |
5073 | regs = alloca (rsa->sizeof_g_packet); |
5074 | memset (regs, 0, rsa->sizeof_g_packet); | |
4a22f64d | 5075 | for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++) |
193cb69f | 5076 | { |
ea9c271d | 5077 | struct packet_reg *r = &rsa->regs[i]; |
b323314b | 5078 | if (r->in_g_packet) |
56be3814 | 5079 | regcache_raw_collect (regcache, r->regnum, regs + r->offset); |
193cb69f AC |
5080 | } |
5081 | } | |
c906108c SS |
5082 | |
5083 | /* Command describes registers byte by byte, | |
5084 | each byte encoded as two hex characters. */ | |
6d820c5c | 5085 | p = rs->buf; |
193cb69f | 5086 | *p++ = 'G'; |
74ca34ce DJ |
5087 | /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets |
5088 | updated. */ | |
5089 | bin2hex (regs, p, rsa->sizeof_g_packet); | |
6d820c5c | 5090 | remote_send (&rs->buf, &rs->buf_size); |
c906108c | 5091 | } |
74ca34ce DJ |
5092 | |
5093 | /* Store register REGNUM, or all registers if REGNUM == -1, from the contents | |
5094 | of the register cache buffer. FIXME: ignores errors. */ | |
5095 | ||
5096 | static void | |
56be3814 | 5097 | remote_store_registers (struct regcache *regcache, int regnum) |
74ca34ce DJ |
5098 | { |
5099 | struct remote_state *rs = get_remote_state (); | |
5100 | struct remote_arch_state *rsa = get_remote_arch_state (); | |
5101 | int i; | |
5102 | ||
79d7f229 | 5103 | set_general_thread (inferior_ptid); |
74ca34ce DJ |
5104 | |
5105 | if (regnum >= 0) | |
5106 | { | |
5107 | struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum); | |
5108 | gdb_assert (reg != NULL); | |
5109 | ||
5110 | /* Always prefer to store registers using the 'P' packet if | |
5111 | possible; we often change only a small number of registers. | |
5112 | Sometimes we change a larger number; we'd need help from a | |
5113 | higher layer to know to use 'G'. */ | |
56be3814 | 5114 | if (store_register_using_P (regcache, reg)) |
74ca34ce DJ |
5115 | return; |
5116 | ||
5117 | /* For now, don't complain if we have no way to write the | |
5118 | register. GDB loses track of unavailable registers too | |
5119 | easily. Some day, this may be an error. We don't have | |
5120 | any way to read the register, either... */ | |
5121 | if (!reg->in_g_packet) | |
5122 | return; | |
5123 | ||
56be3814 | 5124 | store_registers_using_G (regcache); |
74ca34ce DJ |
5125 | return; |
5126 | } | |
5127 | ||
56be3814 | 5128 | store_registers_using_G (regcache); |
74ca34ce | 5129 | |
4a22f64d | 5130 | for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++) |
74ca34ce | 5131 | if (!rsa->regs[i].in_g_packet) |
56be3814 | 5132 | if (!store_register_using_P (regcache, &rsa->regs[i])) |
74ca34ce DJ |
5133 | /* See above for why we do not issue an error here. */ |
5134 | continue; | |
5135 | } | |
c906108c SS |
5136 | \f |
5137 | ||
5138 | /* Return the number of hex digits in num. */ | |
5139 | ||
5140 | static int | |
fba45db2 | 5141 | hexnumlen (ULONGEST num) |
c906108c SS |
5142 | { |
5143 | int i; | |
5144 | ||
5145 | for (i = 0; num != 0; i++) | |
5146 | num >>= 4; | |
5147 | ||
5148 | return max (i, 1); | |
5149 | } | |
5150 | ||
2df3850c | 5151 | /* Set BUF to the minimum number of hex digits representing NUM. */ |
c906108c SS |
5152 | |
5153 | static int | |
fba45db2 | 5154 | hexnumstr (char *buf, ULONGEST num) |
c906108c | 5155 | { |
c906108c | 5156 | int len = hexnumlen (num); |
2df3850c JM |
5157 | return hexnumnstr (buf, num, len); |
5158 | } | |
5159 | ||
c906108c | 5160 | |
2df3850c | 5161 | /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */ |
c906108c | 5162 | |
2df3850c | 5163 | static int |
fba45db2 | 5164 | hexnumnstr (char *buf, ULONGEST num, int width) |
2df3850c JM |
5165 | { |
5166 | int i; | |
5167 | ||
5168 | buf[width] = '\0'; | |
5169 | ||
5170 | for (i = width - 1; i >= 0; i--) | |
c906108c | 5171 | { |
c5aa993b | 5172 | buf[i] = "0123456789abcdef"[(num & 0xf)]; |
c906108c SS |
5173 | num >>= 4; |
5174 | } | |
5175 | ||
2df3850c | 5176 | return width; |
c906108c SS |
5177 | } |
5178 | ||
23860348 | 5179 | /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */ |
c906108c SS |
5180 | |
5181 | static CORE_ADDR | |
fba45db2 | 5182 | remote_address_masked (CORE_ADDR addr) |
c906108c | 5183 | { |
911c95a5 UW |
5184 | int address_size = remote_address_size; |
5185 | /* If "remoteaddresssize" was not set, default to target address size. */ | |
5186 | if (!address_size) | |
1cf3db46 | 5187 | address_size = gdbarch_addr_bit (target_gdbarch); |
911c95a5 UW |
5188 | |
5189 | if (address_size > 0 | |
5190 | && address_size < (sizeof (ULONGEST) * 8)) | |
c906108c SS |
5191 | { |
5192 | /* Only create a mask when that mask can safely be constructed | |
23860348 | 5193 | in a ULONGEST variable. */ |
c906108c | 5194 | ULONGEST mask = 1; |
911c95a5 | 5195 | mask = (mask << address_size) - 1; |
c906108c SS |
5196 | addr &= mask; |
5197 | } | |
5198 | return addr; | |
5199 | } | |
5200 | ||
a31ea83d DJ |
5201 | /* Convert BUFFER, binary data at least LEN bytes long, into escaped |
5202 | binary data in OUT_BUF. Set *OUT_LEN to the length of the data | |
5203 | encoded in OUT_BUF, and return the number of bytes in OUT_BUF | |
5204 | (which may be more than *OUT_LEN due to escape characters). The | |
5205 | total number of bytes in the output buffer will be at most | |
5206 | OUT_MAXLEN. */ | |
5207 | ||
5208 | static int | |
5209 | remote_escape_output (const gdb_byte *buffer, int len, | |
5210 | gdb_byte *out_buf, int *out_len, | |
5211 | int out_maxlen) | |
5212 | { | |
5213 | int input_index, output_index; | |
5214 | ||
5215 | output_index = 0; | |
5216 | for (input_index = 0; input_index < len; input_index++) | |
5217 | { | |
5218 | gdb_byte b = buffer[input_index]; | |
5219 | ||
5220 | if (b == '$' || b == '#' || b == '}') | |
5221 | { | |
5222 | /* These must be escaped. */ | |
5223 | if (output_index + 2 > out_maxlen) | |
5224 | break; | |
5225 | out_buf[output_index++] = '}'; | |
5226 | out_buf[output_index++] = b ^ 0x20; | |
5227 | } | |
5228 | else | |
5229 | { | |
5230 | if (output_index + 1 > out_maxlen) | |
5231 | break; | |
5232 | out_buf[output_index++] = b; | |
5233 | } | |
5234 | } | |
5235 | ||
5236 | *out_len = input_index; | |
5237 | return output_index; | |
5238 | } | |
5239 | ||
0876f84a DJ |
5240 | /* Convert BUFFER, escaped data LEN bytes long, into binary data |
5241 | in OUT_BUF. Return the number of bytes written to OUT_BUF. | |
5242 | Raise an error if the total number of bytes exceeds OUT_MAXLEN. | |
5243 | ||
5244 | This function reverses remote_escape_output. It allows more | |
5245 | escaped characters than that function does, in particular because | |
5246 | '*' must be escaped to avoid the run-length encoding processing | |
5247 | in reading packets. */ | |
5248 | ||
5249 | static int | |
5250 | remote_unescape_input (const gdb_byte *buffer, int len, | |
5251 | gdb_byte *out_buf, int out_maxlen) | |
5252 | { | |
5253 | int input_index, output_index; | |
5254 | int escaped; | |
5255 | ||
5256 | output_index = 0; | |
5257 | escaped = 0; | |
5258 | for (input_index = 0; input_index < len; input_index++) | |
5259 | { | |
5260 | gdb_byte b = buffer[input_index]; | |
5261 | ||
5262 | if (output_index + 1 > out_maxlen) | |
5263 | { | |
5264 | warning (_("Received too much data from remote target;" | |
5265 | " ignoring overflow.")); | |
5266 | return output_index; | |
5267 | } | |
5268 | ||
5269 | if (escaped) | |
5270 | { | |
5271 | out_buf[output_index++] = b ^ 0x20; | |
5272 | escaped = 0; | |
5273 | } | |
5274 | else if (b == '}') | |
5275 | escaped = 1; | |
5276 | else | |
5277 | out_buf[output_index++] = b; | |
5278 | } | |
5279 | ||
5280 | if (escaped) | |
5281 | error (_("Unmatched escape character in target response.")); | |
5282 | ||
5283 | return output_index; | |
5284 | } | |
5285 | ||
c906108c SS |
5286 | /* Determine whether the remote target supports binary downloading. |
5287 | This is accomplished by sending a no-op memory write of zero length | |
5288 | to the target at the specified address. It does not suffice to send | |
23860348 MS |
5289 | the whole packet, since many stubs strip the eighth bit and |
5290 | subsequently compute a wrong checksum, which causes real havoc with | |
5291 | remote_write_bytes. | |
7a292a7a | 5292 | |
96baa820 JM |
5293 | NOTE: This can still lose if the serial line is not eight-bit |
5294 | clean. In cases like this, the user should clear "remote | |
23860348 | 5295 | X-packet". */ |
96baa820 | 5296 | |
c906108c | 5297 | static void |
fba45db2 | 5298 | check_binary_download (CORE_ADDR addr) |
c906108c | 5299 | { |
d01949b6 | 5300 | struct remote_state *rs = get_remote_state (); |
24b06219 | 5301 | |
444abaca | 5302 | switch (remote_protocol_packets[PACKET_X].support) |
c906108c | 5303 | { |
96baa820 JM |
5304 | case PACKET_DISABLE: |
5305 | break; | |
5306 | case PACKET_ENABLE: | |
5307 | break; | |
5308 | case PACKET_SUPPORT_UNKNOWN: | |
5309 | { | |
96baa820 | 5310 | char *p; |
802188a7 | 5311 | |
2e9f7625 | 5312 | p = rs->buf; |
96baa820 JM |
5313 | *p++ = 'X'; |
5314 | p += hexnumstr (p, (ULONGEST) addr); | |
5315 | *p++ = ','; | |
5316 | p += hexnumstr (p, (ULONGEST) 0); | |
5317 | *p++ = ':'; | |
5318 | *p = '\0'; | |
802188a7 | 5319 | |
2e9f7625 | 5320 | putpkt_binary (rs->buf, (int) (p - rs->buf)); |
6d820c5c | 5321 | getpkt (&rs->buf, &rs->buf_size, 0); |
c906108c | 5322 | |
2e9f7625 | 5323 | if (rs->buf[0] == '\0') |
96baa820 JM |
5324 | { |
5325 | if (remote_debug) | |
5326 | fprintf_unfiltered (gdb_stdlog, | |
5327 | "binary downloading NOT suppported by target\n"); | |
444abaca | 5328 | remote_protocol_packets[PACKET_X].support = PACKET_DISABLE; |
96baa820 JM |
5329 | } |
5330 | else | |
5331 | { | |
5332 | if (remote_debug) | |
5333 | fprintf_unfiltered (gdb_stdlog, | |
5334 | "binary downloading suppported by target\n"); | |
444abaca | 5335 | remote_protocol_packets[PACKET_X].support = PACKET_ENABLE; |
96baa820 JM |
5336 | } |
5337 | break; | |
5338 | } | |
c906108c SS |
5339 | } |
5340 | } | |
5341 | ||
5342 | /* Write memory data directly to the remote machine. | |
5343 | This does not inform the data cache; the data cache uses this. | |
a76d924d | 5344 | HEADER is the starting part of the packet. |
c906108c SS |
5345 | MEMADDR is the address in the remote memory space. |
5346 | MYADDR is the address of the buffer in our space. | |
5347 | LEN is the number of bytes. | |
a76d924d DJ |
5348 | PACKET_FORMAT should be either 'X' or 'M', and indicates if we |
5349 | should send data as binary ('X'), or hex-encoded ('M'). | |
5350 | ||
5351 | The function creates packet of the form | |
5352 | <HEADER><ADDRESS>,<LENGTH>:<DATA> | |
5353 | ||
5354 | where encoding of <DATA> is termined by PACKET_FORMAT. | |
5355 | ||
5356 | If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma | |
5357 | are omitted. | |
5358 | ||
5359 | Returns the number of bytes transferred, or 0 (setting errno) for | |
23860348 | 5360 | error. Only transfer a single packet. */ |
c906108c | 5361 | |
a76d924d DJ |
5362 | static int |
5363 | remote_write_bytes_aux (const char *header, CORE_ADDR memaddr, | |
5364 | const gdb_byte *myaddr, int len, | |
5365 | char packet_format, int use_length) | |
c906108c | 5366 | { |
6d820c5c | 5367 | struct remote_state *rs = get_remote_state (); |
cfd77fa1 | 5368 | char *p; |
a76d924d DJ |
5369 | char *plen = NULL; |
5370 | int plenlen = 0; | |
917317f4 JM |
5371 | int todo; |
5372 | int nr_bytes; | |
a257b5bb | 5373 | int payload_size; |
6765f3e5 | 5374 | int payload_length; |
a76d924d DJ |
5375 | int header_length; |
5376 | ||
5377 | if (packet_format != 'X' && packet_format != 'M') | |
5378 | internal_error (__FILE__, __LINE__, | |
5379 | "remote_write_bytes_aux: bad packet format"); | |
c906108c | 5380 | |
b2182ed2 DJ |
5381 | if (len <= 0) |
5382 | return 0; | |
5383 | ||
3de11b2e | 5384 | payload_size = get_memory_write_packet_size (); |
2bc416ba | 5385 | |
6d820c5c DJ |
5386 | /* The packet buffer will be large enough for the payload; |
5387 | get_memory_packet_size ensures this. */ | |
a76d924d | 5388 | rs->buf[0] = '\0'; |
c906108c | 5389 | |
a257b5bb | 5390 | /* Compute the size of the actual payload by subtracting out the |
3de11b2e NS |
5391 | packet header and footer overhead: "$M<memaddr>,<len>:...#nn". |
5392 | */ | |
a76d924d DJ |
5393 | payload_size -= strlen ("$,:#NN"); |
5394 | if (!use_length) | |
5395 | /* The comma won't be used. */ | |
5396 | payload_size += 1; | |
5397 | header_length = strlen (header); | |
5398 | payload_size -= header_length; | |
3de11b2e | 5399 | payload_size -= hexnumlen (memaddr); |
c906108c | 5400 | |
a76d924d | 5401 | /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */ |
917317f4 | 5402 | |
a76d924d DJ |
5403 | strcat (rs->buf, header); |
5404 | p = rs->buf + strlen (header); | |
5405 | ||
5406 | /* Compute a best guess of the number of bytes actually transfered. */ | |
5407 | if (packet_format == 'X') | |
c906108c | 5408 | { |
23860348 | 5409 | /* Best guess at number of bytes that will fit. */ |
a257b5bb | 5410 | todo = min (len, payload_size); |
a76d924d DJ |
5411 | if (use_length) |
5412 | payload_size -= hexnumlen (todo); | |
3de11b2e | 5413 | todo = min (todo, payload_size); |
a76d924d DJ |
5414 | } |
5415 | else | |
5416 | { | |
23860348 | 5417 | /* Num bytes that will fit. */ |
a257b5bb | 5418 | todo = min (len, payload_size / 2); |
a76d924d DJ |
5419 | if (use_length) |
5420 | payload_size -= hexnumlen (todo); | |
3de11b2e | 5421 | todo = min (todo, payload_size / 2); |
917317f4 | 5422 | } |
a76d924d | 5423 | |
3de11b2e NS |
5424 | if (todo <= 0) |
5425 | internal_error (__FILE__, __LINE__, | |
5426 | _("minumum packet size too small to write data")); | |
802188a7 | 5427 | |
6765f3e5 DJ |
5428 | /* If we already need another packet, then try to align the end |
5429 | of this packet to a useful boundary. */ | |
5430 | if (todo > 2 * REMOTE_ALIGN_WRITES && todo < len) | |
5431 | todo = ((memaddr + todo) & ~(REMOTE_ALIGN_WRITES - 1)) - memaddr; | |
5432 | ||
a257b5bb | 5433 | /* Append "<memaddr>". */ |
917317f4 JM |
5434 | memaddr = remote_address_masked (memaddr); |
5435 | p += hexnumstr (p, (ULONGEST) memaddr); | |
a257b5bb | 5436 | |
a76d924d DJ |
5437 | if (use_length) |
5438 | { | |
5439 | /* Append ",". */ | |
5440 | *p++ = ','; | |
802188a7 | 5441 | |
a76d924d DJ |
5442 | /* Append <len>. Retain the location/size of <len>. It may need to |
5443 | be adjusted once the packet body has been created. */ | |
5444 | plen = p; | |
5445 | plenlen = hexnumstr (p, (ULONGEST) todo); | |
5446 | p += plenlen; | |
5447 | } | |
a257b5bb AC |
5448 | |
5449 | /* Append ":". */ | |
917317f4 JM |
5450 | *p++ = ':'; |
5451 | *p = '\0'; | |
802188a7 | 5452 | |
a257b5bb | 5453 | /* Append the packet body. */ |
a76d924d | 5454 | if (packet_format == 'X') |
917317f4 | 5455 | { |
917317f4 JM |
5456 | /* Binary mode. Send target system values byte by byte, in |
5457 | increasing byte addresses. Only escape certain critical | |
5458 | characters. */ | |
6765f3e5 DJ |
5459 | payload_length = remote_escape_output (myaddr, todo, p, &nr_bytes, |
5460 | payload_size); | |
5461 | ||
5462 | /* If not all TODO bytes fit, then we'll need another packet. Make | |
9b7194bc DJ |
5463 | a second try to keep the end of the packet aligned. Don't do |
5464 | this if the packet is tiny. */ | |
5465 | if (nr_bytes < todo && nr_bytes > 2 * REMOTE_ALIGN_WRITES) | |
6765f3e5 DJ |
5466 | { |
5467 | int new_nr_bytes; | |
5468 | ||
5469 | new_nr_bytes = (((memaddr + nr_bytes) & ~(REMOTE_ALIGN_WRITES - 1)) | |
5470 | - memaddr); | |
5471 | if (new_nr_bytes != nr_bytes) | |
5472 | payload_length = remote_escape_output (myaddr, new_nr_bytes, | |
5473 | p, &nr_bytes, | |
5474 | payload_size); | |
5475 | } | |
5476 | ||
5477 | p += payload_length; | |
a76d924d | 5478 | if (use_length && nr_bytes < todo) |
c906108c | 5479 | { |
802188a7 | 5480 | /* Escape chars have filled up the buffer prematurely, |
917317f4 JM |
5481 | and we have actually sent fewer bytes than planned. |
5482 | Fix-up the length field of the packet. Use the same | |
5483 | number of characters as before. */ | |
917317f4 JM |
5484 | plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen); |
5485 | *plen = ':'; /* overwrite \0 from hexnumnstr() */ | |
c906108c | 5486 | } |
a76d924d DJ |
5487 | } |
5488 | else | |
5489 | { | |
917317f4 JM |
5490 | /* Normal mode: Send target system values byte by byte, in |
5491 | increasing byte addresses. Each byte is encoded as a two hex | |
5492 | value. */ | |
2644f393 | 5493 | nr_bytes = bin2hex (myaddr, p, todo); |
aa6c0017 | 5494 | p += 2 * nr_bytes; |
c906108c | 5495 | } |
802188a7 | 5496 | |
2e9f7625 | 5497 | putpkt_binary (rs->buf, (int) (p - rs->buf)); |
6d820c5c | 5498 | getpkt (&rs->buf, &rs->buf_size, 0); |
802188a7 | 5499 | |
2e9f7625 | 5500 | if (rs->buf[0] == 'E') |
917317f4 JM |
5501 | { |
5502 | /* There is no correspondance between what the remote protocol | |
5503 | uses for errors and errno codes. We would like a cleaner way | |
5504 | of representing errors (big enough to include errno codes, | |
5505 | bfd_error codes, and others). But for now just return EIO. */ | |
5506 | errno = EIO; | |
5507 | return 0; | |
5508 | } | |
802188a7 | 5509 | |
23860348 MS |
5510 | /* Return NR_BYTES, not TODO, in case escape chars caused us to send |
5511 | fewer bytes than we'd planned. */ | |
917317f4 | 5512 | return nr_bytes; |
c906108c SS |
5513 | } |
5514 | ||
a76d924d DJ |
5515 | /* Write memory data directly to the remote machine. |
5516 | This does not inform the data cache; the data cache uses this. | |
5517 | MEMADDR is the address in the remote memory space. | |
5518 | MYADDR is the address of the buffer in our space. | |
5519 | LEN is the number of bytes. | |
5520 | ||
5521 | Returns number of bytes transferred, or 0 (setting errno) for | |
5522 | error. Only transfer a single packet. */ | |
5523 | ||
5524 | int | |
5525 | remote_write_bytes (CORE_ADDR memaddr, const gdb_byte *myaddr, int len) | |
5526 | { | |
5527 | char *packet_format = 0; | |
5528 | ||
5529 | /* Check whether the target supports binary download. */ | |
5530 | check_binary_download (memaddr); | |
5531 | ||
5532 | switch (remote_protocol_packets[PACKET_X].support) | |
5533 | { | |
5534 | case PACKET_ENABLE: | |
5535 | packet_format = "X"; | |
5536 | break; | |
5537 | case PACKET_DISABLE: | |
5538 | packet_format = "M"; | |
5539 | break; | |
5540 | case PACKET_SUPPORT_UNKNOWN: | |
5541 | internal_error (__FILE__, __LINE__, | |
5542 | _("remote_write_bytes: bad internal state")); | |
5543 | default: | |
5544 | internal_error (__FILE__, __LINE__, _("bad switch")); | |
5545 | } | |
5546 | ||
5547 | return remote_write_bytes_aux (packet_format, | |
5548 | memaddr, myaddr, len, packet_format[0], 1); | |
5549 | } | |
5550 | ||
c906108c SS |
5551 | /* Read memory data directly from the remote machine. |
5552 | This does not use the data cache; the data cache uses this. | |
5553 | MEMADDR is the address in the remote memory space. | |
5554 | MYADDR is the address of the buffer in our space. | |
5555 | LEN is the number of bytes. | |
5556 | ||
5557 | Returns number of bytes transferred, or 0 for error. */ | |
5558 | ||
917317f4 JM |
5559 | /* NOTE: cagney/1999-10-18: This function (and its siblings in other |
5560 | remote targets) shouldn't attempt to read the entire buffer. | |
5561 | Instead it should read a single packet worth of data and then | |
5562 | return the byte size of that packet to the caller. The caller (its | |
5563 | caller and its callers caller ;-) already contains code for | |
23860348 | 5564 | handling partial reads. */ |
917317f4 | 5565 | |
449092f6 | 5566 | int |
cfd77fa1 | 5567 | remote_read_bytes (CORE_ADDR memaddr, gdb_byte *myaddr, int len) |
c906108c | 5568 | { |
6d820c5c | 5569 | struct remote_state *rs = get_remote_state (); |
23860348 | 5570 | int max_buf_size; /* Max size of packet output buffer. */ |
c906108c SS |
5571 | int origlen; |
5572 | ||
b2182ed2 DJ |
5573 | if (len <= 0) |
5574 | return 0; | |
5575 | ||
11cf8741 | 5576 | max_buf_size = get_memory_read_packet_size (); |
6d820c5c DJ |
5577 | /* The packet buffer will be large enough for the payload; |
5578 | get_memory_packet_size ensures this. */ | |
c906108c SS |
5579 | |
5580 | origlen = len; | |
5581 | while (len > 0) | |
5582 | { | |
c906108c SS |
5583 | char *p; |
5584 | int todo; | |
5585 | int i; | |
5586 | ||
c5aa993b | 5587 | todo = min (len, max_buf_size / 2); /* num bytes that will fit */ |
c906108c SS |
5588 | |
5589 | /* construct "m"<memaddr>","<len>" */ | |
2e9f7625 | 5590 | /* sprintf (rs->buf, "m%lx,%x", (unsigned long) memaddr, todo); */ |
c906108c | 5591 | memaddr = remote_address_masked (memaddr); |
2e9f7625 | 5592 | p = rs->buf; |
c906108c SS |
5593 | *p++ = 'm'; |
5594 | p += hexnumstr (p, (ULONGEST) memaddr); | |
5595 | *p++ = ','; | |
5596 | p += hexnumstr (p, (ULONGEST) todo); | |
5597 | *p = '\0'; | |
5598 | ||
2e9f7625 | 5599 | putpkt (rs->buf); |
6d820c5c | 5600 | getpkt (&rs->buf, &rs->buf_size, 0); |
c906108c | 5601 | |
2e9f7625 DJ |
5602 | if (rs->buf[0] == 'E' |
5603 | && isxdigit (rs->buf[1]) && isxdigit (rs->buf[2]) | |
5604 | && rs->buf[3] == '\0') | |
c906108c | 5605 | { |
23860348 MS |
5606 | /* There is no correspondance between what the remote |
5607 | protocol uses for errors and errno codes. We would like | |
5608 | a cleaner way of representing errors (big enough to | |
5609 | include errno codes, bfd_error codes, and others). But | |
5610 | for now just return EIO. */ | |
c906108c SS |
5611 | errno = EIO; |
5612 | return 0; | |
5613 | } | |
5614 | ||
c5aa993b JM |
5615 | /* Reply describes memory byte by byte, |
5616 | each byte encoded as two hex characters. */ | |
c906108c | 5617 | |
2e9f7625 | 5618 | p = rs->buf; |
30559e10 | 5619 | if ((i = hex2bin (p, myaddr, todo)) < todo) |
c906108c | 5620 | { |
30559e10 | 5621 | /* Reply is short. This means that we were able to read |
23860348 | 5622 | only part of what we wanted to. */ |
30559e10 | 5623 | return i + (origlen - len); |
c906108c SS |
5624 | } |
5625 | myaddr += todo; | |
5626 | memaddr += todo; | |
5627 | len -= todo; | |
5628 | } | |
5629 | return origlen; | |
5630 | } | |
74531fed PA |
5631 | \f |
5632 | ||
5633 | /* Remote notification handler. */ | |
5634 | ||
5635 | static void | |
5636 | handle_notification (char *buf, size_t length) | |
5637 | { | |
5638 | if (strncmp (buf, "Stop:", 5) == 0) | |
5639 | { | |
5640 | if (pending_stop_reply) | |
5641 | /* We've already parsed the in-flight stop-reply, but the stub | |
5642 | for some reason thought we didn't, possibly due to timeout | |
5643 | on its side. Just ignore it. */ | |
5644 | ; | |
5645 | else | |
5646 | { | |
5647 | struct cleanup *old_chain; | |
5648 | struct stop_reply *reply = stop_reply_xmalloc (); | |
5649 | old_chain = make_cleanup (do_stop_reply_xfree, reply); | |
5650 | ||
5651 | remote_parse_stop_reply (buf + 5, reply); | |
5652 | ||
5653 | discard_cleanups (old_chain); | |
5654 | ||
5655 | /* Be careful to only set it after parsing, since an error | |
5656 | may be thrown then. */ | |
5657 | pending_stop_reply = reply; | |
5658 | ||
5659 | /* Notify the event loop there's a stop reply to acknowledge | |
5660 | and that there may be more events to fetch. */ | |
5661 | mark_async_event_handler (remote_async_get_pending_events_token); | |
5662 | } | |
5663 | } | |
5664 | else | |
5665 | /* We ignore notifications we don't recognize, for compatibility | |
5666 | with newer stubs. */ | |
5667 | ; | |
5668 | } | |
5669 | ||
c906108c SS |
5670 | \f |
5671 | /* Read or write LEN bytes from inferior memory at MEMADDR, | |
23860348 MS |
5672 | transferring to or from debugger address BUFFER. Write to inferior |
5673 | if SHOULD_WRITE is nonzero. Returns length of data written or | |
5674 | read; 0 for error. TARGET is unused. */ | |
392a587b | 5675 | |
c906108c | 5676 | static int |
961cb7b5 | 5677 | remote_xfer_memory (CORE_ADDR mem_addr, gdb_byte *buffer, int mem_len, |
0a65a603 | 5678 | int should_write, struct mem_attrib *attrib, |
29e57380 | 5679 | struct target_ops *target) |
c906108c | 5680 | { |
4930751a C |
5681 | int res; |
5682 | ||
82f73884 PA |
5683 | set_general_thread (inferior_ptid); |
5684 | ||
4930751a | 5685 | if (should_write) |
b2182ed2 | 5686 | res = remote_write_bytes (mem_addr, buffer, mem_len); |
4930751a | 5687 | else |
b2182ed2 | 5688 | res = remote_read_bytes (mem_addr, buffer, mem_len); |
4930751a C |
5689 | |
5690 | return res; | |
c906108c SS |
5691 | } |
5692 | ||
a76d924d DJ |
5693 | /* Sends a packet with content determined by the printf format string |
5694 | FORMAT and the remaining arguments, then gets the reply. Returns | |
5695 | whether the packet was a success, a failure, or unknown. */ | |
5696 | ||
5697 | enum packet_result | |
5698 | remote_send_printf (const char *format, ...) | |
5699 | { | |
5700 | struct remote_state *rs = get_remote_state (); | |
5701 | int max_size = get_remote_packet_size (); | |
5702 | ||
5703 | va_list ap; | |
5704 | va_start (ap, format); | |
5705 | ||
5706 | rs->buf[0] = '\0'; | |
5707 | if (vsnprintf (rs->buf, max_size, format, ap) >= max_size) | |
5708 | internal_error (__FILE__, __LINE__, "Too long remote packet."); | |
5709 | ||
5710 | if (putpkt (rs->buf) < 0) | |
5711 | error (_("Communication problem with target.")); | |
5712 | ||
5713 | rs->buf[0] = '\0'; | |
5714 | getpkt (&rs->buf, &rs->buf_size, 0); | |
5715 | ||
5716 | return packet_check_result (rs->buf); | |
5717 | } | |
5718 | ||
5719 | static void | |
5720 | restore_remote_timeout (void *p) | |
5721 | { | |
5722 | int value = *(int *)p; | |
5723 | remote_timeout = value; | |
5724 | } | |
5725 | ||
5726 | /* Flash writing can take quite some time. We'll set | |
5727 | effectively infinite timeout for flash operations. | |
5728 | In future, we'll need to decide on a better approach. */ | |
5729 | static const int remote_flash_timeout = 1000; | |
5730 | ||
5731 | static void | |
5732 | remote_flash_erase (struct target_ops *ops, | |
5733 | ULONGEST address, LONGEST length) | |
5734 | { | |
5735 | int saved_remote_timeout = remote_timeout; | |
5736 | enum packet_result ret; | |
5737 | ||
5738 | struct cleanup *back_to = make_cleanup (restore_remote_timeout, | |
5739 | &saved_remote_timeout); | |
5740 | remote_timeout = remote_flash_timeout; | |
5741 | ||
5742 | ret = remote_send_printf ("vFlashErase:%s,%s", | |
5743 | paddr (address), | |
5744 | phex (length, 4)); | |
5745 | switch (ret) | |
5746 | { | |
5747 | case PACKET_UNKNOWN: | |
5748 | error (_("Remote target does not support flash erase")); | |
5749 | case PACKET_ERROR: | |
5750 | error (_("Error erasing flash with vFlashErase packet")); | |
5751 | default: | |
5752 | break; | |
5753 | } | |
5754 | ||
5755 | do_cleanups (back_to); | |
5756 | } | |
5757 | ||
5758 | static LONGEST | |
5759 | remote_flash_write (struct target_ops *ops, | |
5760 | ULONGEST address, LONGEST length, | |
5761 | const gdb_byte *data) | |
5762 | { | |
5763 | int saved_remote_timeout = remote_timeout; | |
5764 | int ret; | |
5765 | struct cleanup *back_to = make_cleanup (restore_remote_timeout, | |
5766 | &saved_remote_timeout); | |
5767 | ||
5768 | remote_timeout = remote_flash_timeout; | |
5769 | ret = remote_write_bytes_aux ("vFlashWrite:", address, data, length, 'X', 0); | |
5770 | do_cleanups (back_to); | |
5771 | ||
5772 | return ret; | |
5773 | } | |
5774 | ||
5775 | static void | |
5776 | remote_flash_done (struct target_ops *ops) | |
5777 | { | |
5778 | int saved_remote_timeout = remote_timeout; | |
5779 | int ret; | |
5780 | struct cleanup *back_to = make_cleanup (restore_remote_timeout, | |
5781 | &saved_remote_timeout); | |
5782 | ||
5783 | remote_timeout = remote_flash_timeout; | |
5784 | ret = remote_send_printf ("vFlashDone"); | |
5785 | do_cleanups (back_to); | |
5786 | ||
5787 | switch (ret) | |
5788 | { | |
5789 | case PACKET_UNKNOWN: | |
5790 | error (_("Remote target does not support vFlashDone")); | |
5791 | case PACKET_ERROR: | |
5792 | error (_("Error finishing flash operation")); | |
5793 | default: | |
5794 | break; | |
5795 | } | |
5796 | } | |
5797 | ||
c906108c | 5798 | static void |
fba45db2 | 5799 | remote_files_info (struct target_ops *ignore) |
c906108c SS |
5800 | { |
5801 | puts_filtered ("Debugging a target over a serial line.\n"); | |
5802 | } | |
5803 | \f | |
5804 | /* Stuff for dealing with the packets which are part of this protocol. | |
5805 | See comment at top of file for details. */ | |
5806 | ||
0876f84a | 5807 | /* Read a single character from the remote end. */ |
c906108c SS |
5808 | |
5809 | static int | |
fba45db2 | 5810 | readchar (int timeout) |
c906108c SS |
5811 | { |
5812 | int ch; | |
5813 | ||
2cd58942 | 5814 | ch = serial_readchar (remote_desc, timeout); |
c906108c | 5815 | |
2acceee2 | 5816 | if (ch >= 0) |
0876f84a | 5817 | return ch; |
2acceee2 JM |
5818 | |
5819 | switch ((enum serial_rc) ch) | |
c906108c SS |
5820 | { |
5821 | case SERIAL_EOF: | |
ce5ce7ed | 5822 | pop_target (); |
8a3fe4f8 | 5823 | error (_("Remote connection closed")); |
2acceee2 | 5824 | /* no return */ |
c906108c | 5825 | case SERIAL_ERROR: |
e2e0b3e5 | 5826 | perror_with_name (_("Remote communication error")); |
2acceee2 | 5827 | /* no return */ |
c906108c | 5828 | case SERIAL_TIMEOUT: |
2acceee2 | 5829 | break; |
c906108c | 5830 | } |
2acceee2 | 5831 | return ch; |
c906108c SS |
5832 | } |
5833 | ||
6d820c5c DJ |
5834 | /* Send the command in *BUF to the remote machine, and read the reply |
5835 | into *BUF. Report an error if we get an error reply. Resize | |
5836 | *BUF using xrealloc if necessary to hold the result, and update | |
5837 | *SIZEOF_BUF. */ | |
c906108c SS |
5838 | |
5839 | static void | |
6d820c5c DJ |
5840 | remote_send (char **buf, |
5841 | long *sizeof_buf) | |
c906108c | 5842 | { |
6d820c5c | 5843 | putpkt (*buf); |
c2d11a7d | 5844 | getpkt (buf, sizeof_buf, 0); |
c906108c | 5845 | |
6d820c5c DJ |
5846 | if ((*buf)[0] == 'E') |
5847 | error (_("Remote failure reply: %s"), *buf); | |
c906108c SS |
5848 | } |
5849 | ||
6e5abd65 PA |
5850 | /* Return a pointer to an xmalloc'ed string representing an escaped |
5851 | version of BUF, of len N. E.g. \n is converted to \\n, \t to \\t, | |
5852 | etc. The caller is responsible for releasing the returned | |
5853 | memory. */ | |
5854 | ||
5855 | static char * | |
5856 | escape_buffer (const char *buf, int n) | |
5857 | { | |
5858 | struct cleanup *old_chain; | |
5859 | struct ui_file *stb; | |
5860 | char *str; | |
5861 | long length; | |
5862 | ||
5863 | stb = mem_fileopen (); | |
5864 | old_chain = make_cleanup_ui_file_delete (stb); | |
5865 | ||
5866 | fputstrn_unfiltered (buf, n, 0, stb); | |
5867 | str = ui_file_xstrdup (stb, &length); | |
5868 | do_cleanups (old_chain); | |
5869 | return str; | |
5870 | } | |
5871 | ||
c906108c SS |
5872 | /* Display a null-terminated packet on stdout, for debugging, using C |
5873 | string notation. */ | |
5874 | ||
5875 | static void | |
fba45db2 | 5876 | print_packet (char *buf) |
c906108c SS |
5877 | { |
5878 | puts_filtered ("\""); | |
43e526b9 | 5879 | fputstr_filtered (buf, '"', gdb_stdout); |
c906108c SS |
5880 | puts_filtered ("\""); |
5881 | } | |
5882 | ||
5883 | int | |
fba45db2 | 5884 | putpkt (char *buf) |
c906108c SS |
5885 | { |
5886 | return putpkt_binary (buf, strlen (buf)); | |
5887 | } | |
5888 | ||
5889 | /* Send a packet to the remote machine, with error checking. The data | |
23860348 | 5890 | of the packet is in BUF. The string in BUF can be at most |
ea9c271d | 5891 | get_remote_packet_size () - 5 to account for the $, # and checksum, |
23860348 MS |
5892 | and for a possible /0 if we are debugging (remote_debug) and want |
5893 | to print the sent packet as a string. */ | |
c906108c SS |
5894 | |
5895 | static int | |
fba45db2 | 5896 | putpkt_binary (char *buf, int cnt) |
c906108c | 5897 | { |
2d717e4f | 5898 | struct remote_state *rs = get_remote_state (); |
c906108c SS |
5899 | int i; |
5900 | unsigned char csum = 0; | |
11cf8741 | 5901 | char *buf2 = alloca (cnt + 6); |
085dd6e6 | 5902 | |
c906108c SS |
5903 | int ch; |
5904 | int tcount = 0; | |
5905 | char *p; | |
5906 | ||
e24a49d8 PA |
5907 | /* Catch cases like trying to read memory or listing threads while |
5908 | we're waiting for a stop reply. The remote server wouldn't be | |
5909 | ready to handle this request, so we'd hang and timeout. We don't | |
5910 | have to worry about this in synchronous mode, because in that | |
5911 | case it's not possible to issue a command while the target is | |
74531fed PA |
5912 | running. This is not a problem in non-stop mode, because in that |
5913 | case, the stub is always ready to process serial input. */ | |
5914 | if (!non_stop && target_can_async_p () && rs->waiting_for_stop_reply) | |
e24a49d8 PA |
5915 | error (_("Cannot execute this command while the target is running.")); |
5916 | ||
2d717e4f DJ |
5917 | /* We're sending out a new packet. Make sure we don't look at a |
5918 | stale cached response. */ | |
5919 | rs->cached_wait_status = 0; | |
5920 | ||
c906108c SS |
5921 | /* Copy the packet into buffer BUF2, encapsulating it |
5922 | and giving it a checksum. */ | |
5923 | ||
c906108c SS |
5924 | p = buf2; |
5925 | *p++ = '$'; | |
5926 | ||
5927 | for (i = 0; i < cnt; i++) | |
5928 | { | |
5929 | csum += buf[i]; | |
5930 | *p++ = buf[i]; | |
5931 | } | |
5932 | *p++ = '#'; | |
5933 | *p++ = tohex ((csum >> 4) & 0xf); | |
5934 | *p++ = tohex (csum & 0xf); | |
5935 | ||
5936 | /* Send it over and over until we get a positive ack. */ | |
5937 | ||
5938 | while (1) | |
5939 | { | |
5940 | int started_error_output = 0; | |
5941 | ||
5942 | if (remote_debug) | |
5943 | { | |
6e5abd65 PA |
5944 | struct cleanup *old_chain; |
5945 | char *str; | |
5946 | ||
c906108c | 5947 | *p = '\0'; |
6e5abd65 PA |
5948 | str = escape_buffer (buf2, p - buf2); |
5949 | old_chain = make_cleanup (xfree, str); | |
5950 | fprintf_unfiltered (gdb_stdlog, "Sending packet: %s...", str); | |
0f71a2f6 | 5951 | gdb_flush (gdb_stdlog); |
6e5abd65 | 5952 | do_cleanups (old_chain); |
c906108c | 5953 | } |
2cd58942 | 5954 | if (serial_write (remote_desc, buf2, p - buf2)) |
e2e0b3e5 | 5955 | perror_with_name (_("putpkt: write failed")); |
c906108c | 5956 | |
a6f3e723 SL |
5957 | /* If this is a no acks version of the remote protocol, send the |
5958 | packet and move on. */ | |
5959 | if (rs->noack_mode) | |
5960 | break; | |
5961 | ||
74531fed PA |
5962 | /* Read until either a timeout occurs (-2) or '+' is read. |
5963 | Handle any notification that arrives in the mean time. */ | |
c906108c SS |
5964 | while (1) |
5965 | { | |
5966 | ch = readchar (remote_timeout); | |
5967 | ||
c5aa993b | 5968 | if (remote_debug) |
c906108c SS |
5969 | { |
5970 | switch (ch) | |
5971 | { | |
5972 | case '+': | |
1216fa2c | 5973 | case '-': |
c906108c SS |
5974 | case SERIAL_TIMEOUT: |
5975 | case '$': | |
74531fed | 5976 | case '%': |
c906108c SS |
5977 | if (started_error_output) |
5978 | { | |
5979 | putchar_unfiltered ('\n'); | |
5980 | started_error_output = 0; | |
5981 | } | |
5982 | } | |
5983 | } | |
5984 | ||
5985 | switch (ch) | |
5986 | { | |
5987 | case '+': | |
5988 | if (remote_debug) | |
0f71a2f6 | 5989 | fprintf_unfiltered (gdb_stdlog, "Ack\n"); |
c906108c | 5990 | return 1; |
1216fa2c AC |
5991 | case '-': |
5992 | if (remote_debug) | |
5993 | fprintf_unfiltered (gdb_stdlog, "Nak\n"); | |
c906108c | 5994 | case SERIAL_TIMEOUT: |
c5aa993b | 5995 | tcount++; |
c906108c SS |
5996 | if (tcount > 3) |
5997 | return 0; | |
23860348 | 5998 | break; /* Retransmit buffer. */ |
c906108c SS |
5999 | case '$': |
6000 | { | |
40e3f985 | 6001 | if (remote_debug) |
2bc416ba | 6002 | fprintf_unfiltered (gdb_stdlog, |
23860348 | 6003 | "Packet instead of Ack, ignoring it\n"); |
d6f7abdf AC |
6004 | /* It's probably an old response sent because an ACK |
6005 | was lost. Gobble up the packet and ack it so it | |
6006 | doesn't get retransmitted when we resend this | |
6007 | packet. */ | |
6d820c5c | 6008 | skip_frame (); |
d6f7abdf | 6009 | serial_write (remote_desc, "+", 1); |
23860348 | 6010 | continue; /* Now, go look for +. */ |
c906108c | 6011 | } |
74531fed PA |
6012 | |
6013 | case '%': | |
6014 | { | |
6015 | int val; | |
6016 | ||
6017 | /* If we got a notification, handle it, and go back to looking | |
6018 | for an ack. */ | |
6019 | /* We've found the start of a notification. Now | |
6020 | collect the data. */ | |
6021 | val = read_frame (&rs->buf, &rs->buf_size); | |
6022 | if (val >= 0) | |
6023 | { | |
6024 | if (remote_debug) | |
6025 | { | |
6e5abd65 PA |
6026 | struct cleanup *old_chain; |
6027 | char *str; | |
6028 | ||
6029 | str = escape_buffer (rs->buf, val); | |
6030 | old_chain = make_cleanup (xfree, str); | |
6031 | fprintf_unfiltered (gdb_stdlog, | |
6032 | " Notification received: %s\n", | |
6033 | str); | |
6034 | do_cleanups (old_chain); | |
74531fed PA |
6035 | } |
6036 | handle_notification (rs->buf, val); | |
6037 | /* We're in sync now, rewait for the ack. */ | |
6038 | tcount = 0; | |
6039 | } | |
6040 | else | |
6041 | { | |
6042 | if (remote_debug) | |
6043 | { | |
6044 | if (!started_error_output) | |
6045 | { | |
6046 | started_error_output = 1; | |
6047 | fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: "); | |
6048 | } | |
6049 | fputc_unfiltered (ch & 0177, gdb_stdlog); | |
6050 | fprintf_unfiltered (gdb_stdlog, "%s", rs->buf); | |
6051 | } | |
6052 | } | |
6053 | continue; | |
6054 | } | |
6055 | /* fall-through */ | |
c906108c SS |
6056 | default: |
6057 | if (remote_debug) | |
6058 | { | |
6059 | if (!started_error_output) | |
6060 | { | |
6061 | started_error_output = 1; | |
0f71a2f6 | 6062 | fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: "); |
c906108c | 6063 | } |
0f71a2f6 | 6064 | fputc_unfiltered (ch & 0177, gdb_stdlog); |
c906108c SS |
6065 | } |
6066 | continue; | |
6067 | } | |
23860348 | 6068 | break; /* Here to retransmit. */ |
c906108c SS |
6069 | } |
6070 | ||
6071 | #if 0 | |
6072 | /* This is wrong. If doing a long backtrace, the user should be | |
c5aa993b JM |
6073 | able to get out next time we call QUIT, without anything as |
6074 | violent as interrupt_query. If we want to provide a way out of | |
6075 | here without getting to the next QUIT, it should be based on | |
6076 | hitting ^C twice as in remote_wait. */ | |
c906108c SS |
6077 | if (quit_flag) |
6078 | { | |
6079 | quit_flag = 0; | |
6080 | interrupt_query (); | |
6081 | } | |
6082 | #endif | |
6083 | } | |
a6f3e723 | 6084 | return 0; |
c906108c SS |
6085 | } |
6086 | ||
6d820c5c DJ |
6087 | /* Come here after finding the start of a frame when we expected an |
6088 | ack. Do our best to discard the rest of this packet. */ | |
6089 | ||
6090 | static void | |
6091 | skip_frame (void) | |
6092 | { | |
6093 | int c; | |
6094 | ||
6095 | while (1) | |
6096 | { | |
6097 | c = readchar (remote_timeout); | |
6098 | switch (c) | |
6099 | { | |
6100 | case SERIAL_TIMEOUT: | |
6101 | /* Nothing we can do. */ | |
6102 | return; | |
6103 | case '#': | |
6104 | /* Discard the two bytes of checksum and stop. */ | |
6105 | c = readchar (remote_timeout); | |
6106 | if (c >= 0) | |
6107 | c = readchar (remote_timeout); | |
6108 | ||
6109 | return; | |
6110 | case '*': /* Run length encoding. */ | |
6111 | /* Discard the repeat count. */ | |
6112 | c = readchar (remote_timeout); | |
6113 | if (c < 0) | |
6114 | return; | |
6115 | break; | |
6116 | default: | |
6117 | /* A regular character. */ | |
6118 | break; | |
6119 | } | |
6120 | } | |
6121 | } | |
6122 | ||
c906108c | 6123 | /* Come here after finding the start of the frame. Collect the rest |
6d820c5c DJ |
6124 | into *BUF, verifying the checksum, length, and handling run-length |
6125 | compression. NUL terminate the buffer. If there is not enough room, | |
6126 | expand *BUF using xrealloc. | |
c906108c | 6127 | |
c2d11a7d JM |
6128 | Returns -1 on error, number of characters in buffer (ignoring the |
6129 | trailing NULL) on success. (could be extended to return one of the | |
23860348 | 6130 | SERIAL status indications). */ |
c2d11a7d JM |
6131 | |
6132 | static long | |
6d820c5c DJ |
6133 | read_frame (char **buf_p, |
6134 | long *sizeof_buf) | |
c906108c SS |
6135 | { |
6136 | unsigned char csum; | |
c2d11a7d | 6137 | long bc; |
c906108c | 6138 | int c; |
6d820c5c | 6139 | char *buf = *buf_p; |
a6f3e723 | 6140 | struct remote_state *rs = get_remote_state (); |
c906108c SS |
6141 | |
6142 | csum = 0; | |
c2d11a7d | 6143 | bc = 0; |
c906108c SS |
6144 | |
6145 | while (1) | |
6146 | { | |
6147 | c = readchar (remote_timeout); | |
c906108c SS |
6148 | switch (c) |
6149 | { | |
6150 | case SERIAL_TIMEOUT: | |
6151 | if (remote_debug) | |
0f71a2f6 | 6152 | fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog); |
c2d11a7d | 6153 | return -1; |
c906108c SS |
6154 | case '$': |
6155 | if (remote_debug) | |
0f71a2f6 JM |
6156 | fputs_filtered ("Saw new packet start in middle of old one\n", |
6157 | gdb_stdlog); | |
23860348 | 6158 | return -1; /* Start a new packet, count retries. */ |
c906108c SS |
6159 | case '#': |
6160 | { | |
6161 | unsigned char pktcsum; | |
e1b09194 AC |
6162 | int check_0 = 0; |
6163 | int check_1 = 0; | |
c906108c | 6164 | |
c2d11a7d | 6165 | buf[bc] = '\0'; |
c906108c | 6166 | |
e1b09194 AC |
6167 | check_0 = readchar (remote_timeout); |
6168 | if (check_0 >= 0) | |
6169 | check_1 = readchar (remote_timeout); | |
802188a7 | 6170 | |
e1b09194 AC |
6171 | if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT) |
6172 | { | |
6173 | if (remote_debug) | |
2bc416ba | 6174 | fputs_filtered ("Timeout in checksum, retrying\n", |
23860348 | 6175 | gdb_stdlog); |
e1b09194 AC |
6176 | return -1; |
6177 | } | |
6178 | else if (check_0 < 0 || check_1 < 0) | |
40e3f985 FN |
6179 | { |
6180 | if (remote_debug) | |
2bc416ba | 6181 | fputs_filtered ("Communication error in checksum\n", |
23860348 | 6182 | gdb_stdlog); |
40e3f985 FN |
6183 | return -1; |
6184 | } | |
c906108c | 6185 | |
a6f3e723 SL |
6186 | /* Don't recompute the checksum; with no ack packets we |
6187 | don't have any way to indicate a packet retransmission | |
6188 | is necessary. */ | |
6189 | if (rs->noack_mode) | |
6190 | return bc; | |
6191 | ||
e1b09194 | 6192 | pktcsum = (fromhex (check_0) << 4) | fromhex (check_1); |
c906108c | 6193 | if (csum == pktcsum) |
c2d11a7d | 6194 | return bc; |
c906108c | 6195 | |
c5aa993b | 6196 | if (remote_debug) |
c906108c | 6197 | { |
6e5abd65 PA |
6198 | struct cleanup *old_chain; |
6199 | char *str; | |
6200 | ||
6201 | str = escape_buffer (buf, bc); | |
6202 | old_chain = make_cleanup (xfree, str); | |
6203 | fprintf_unfiltered (gdb_stdlog, | |
6204 | "\ | |
6205 | Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n", | |
6206 | pktcsum, csum, str); | |
6207 | do_cleanups (old_chain); | |
c906108c | 6208 | } |
c2d11a7d | 6209 | /* Number of characters in buffer ignoring trailing |
23860348 | 6210 | NULL. */ |
c2d11a7d | 6211 | return -1; |
c906108c | 6212 | } |
23860348 | 6213 | case '*': /* Run length encoding. */ |
c2c6d25f JM |
6214 | { |
6215 | int repeat; | |
6216 | csum += c; | |
c906108c | 6217 | |
b4501125 AC |
6218 | c = readchar (remote_timeout); |
6219 | csum += c; | |
23860348 | 6220 | repeat = c - ' ' + 3; /* Compute repeat count. */ |
c906108c | 6221 | |
23860348 | 6222 | /* The character before ``*'' is repeated. */ |
c2d11a7d | 6223 | |
6d820c5c | 6224 | if (repeat > 0 && repeat <= 255 && bc > 0) |
c2c6d25f | 6225 | { |
6d820c5c DJ |
6226 | if (bc + repeat - 1 >= *sizeof_buf - 1) |
6227 | { | |
6228 | /* Make some more room in the buffer. */ | |
6229 | *sizeof_buf += repeat; | |
6230 | *buf_p = xrealloc (*buf_p, *sizeof_buf); | |
6231 | buf = *buf_p; | |
6232 | } | |
6233 | ||
c2d11a7d JM |
6234 | memset (&buf[bc], buf[bc - 1], repeat); |
6235 | bc += repeat; | |
c2c6d25f JM |
6236 | continue; |
6237 | } | |
6238 | ||
c2d11a7d | 6239 | buf[bc] = '\0'; |
6d820c5c | 6240 | printf_filtered (_("Invalid run length encoding: %s\n"), buf); |
c2d11a7d | 6241 | return -1; |
c2c6d25f | 6242 | } |
c906108c | 6243 | default: |
6d820c5c | 6244 | if (bc >= *sizeof_buf - 1) |
c906108c | 6245 | { |
6d820c5c DJ |
6246 | /* Make some more room in the buffer. */ |
6247 | *sizeof_buf *= 2; | |
6248 | *buf_p = xrealloc (*buf_p, *sizeof_buf); | |
6249 | buf = *buf_p; | |
c906108c SS |
6250 | } |
6251 | ||
6d820c5c DJ |
6252 | buf[bc++] = c; |
6253 | csum += c; | |
6254 | continue; | |
c906108c SS |
6255 | } |
6256 | } | |
6257 | } | |
6258 | ||
6259 | /* Read a packet from the remote machine, with error checking, and | |
6d820c5c DJ |
6260 | store it in *BUF. Resize *BUF using xrealloc if necessary to hold |
6261 | the result, and update *SIZEOF_BUF. If FOREVER, wait forever | |
6262 | rather than timing out; this is used (in synchronous mode) to wait | |
6263 | for a target that is is executing user code to stop. */ | |
d9fcf2fb JM |
6264 | /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we |
6265 | don't have to change all the calls to getpkt to deal with the | |
6266 | return value, because at the moment I don't know what the right | |
23860348 | 6267 | thing to do it for those. */ |
c906108c | 6268 | void |
6d820c5c DJ |
6269 | getpkt (char **buf, |
6270 | long *sizeof_buf, | |
c2d11a7d | 6271 | int forever) |
d9fcf2fb JM |
6272 | { |
6273 | int timed_out; | |
6274 | ||
6275 | timed_out = getpkt_sane (buf, sizeof_buf, forever); | |
6276 | } | |
6277 | ||
6278 | ||
6279 | /* Read a packet from the remote machine, with error checking, and | |
6d820c5c DJ |
6280 | store it in *BUF. Resize *BUF using xrealloc if necessary to hold |
6281 | the result, and update *SIZEOF_BUF. If FOREVER, wait forever | |
6282 | rather than timing out; this is used (in synchronous mode) to wait | |
6283 | for a target that is is executing user code to stop. If FOREVER == | |
6284 | 0, this function is allowed to time out gracefully and return an | |
74531fed PA |
6285 | indication of this to the caller. Otherwise return the number of |
6286 | bytes read. If EXPECTING_NOTIF, consider receiving a notification | |
6287 | enough reason to return to the caller. */ | |
6288 | ||
3172dc30 | 6289 | static int |
74531fed PA |
6290 | getpkt_or_notif_sane_1 (char **buf, long *sizeof_buf, int forever, |
6291 | int expecting_notif) | |
c906108c | 6292 | { |
2d717e4f | 6293 | struct remote_state *rs = get_remote_state (); |
c906108c SS |
6294 | int c; |
6295 | int tries; | |
6296 | int timeout; | |
6297 | int val; | |
6298 | ||
2d717e4f DJ |
6299 | /* We're reading a new response. Make sure we don't look at a |
6300 | previously cached response. */ | |
6301 | rs->cached_wait_status = 0; | |
6302 | ||
6d820c5c | 6303 | strcpy (*buf, "timeout"); |
c906108c SS |
6304 | |
6305 | if (forever) | |
74531fed PA |
6306 | timeout = watchdog > 0 ? watchdog : -1; |
6307 | else if (expecting_notif) | |
6308 | timeout = 0; /* There should already be a char in the buffer. If | |
6309 | not, bail out. */ | |
c906108c SS |
6310 | else |
6311 | timeout = remote_timeout; | |
6312 | ||
6313 | #define MAX_TRIES 3 | |
6314 | ||
74531fed PA |
6315 | /* Process any number of notifications, and then return when |
6316 | we get a packet. */ | |
6317 | for (;;) | |
c906108c | 6318 | { |
74531fed PA |
6319 | /* If we get a timeout or bad checksm, retry up to MAX_TRIES |
6320 | times. */ | |
6321 | for (tries = 1; tries <= MAX_TRIES; tries++) | |
c906108c | 6322 | { |
74531fed PA |
6323 | /* This can loop forever if the remote side sends us |
6324 | characters continuously, but if it pauses, we'll get | |
6325 | SERIAL_TIMEOUT from readchar because of timeout. Then | |
6326 | we'll count that as a retry. | |
6327 | ||
6328 | Note that even when forever is set, we will only wait | |
6329 | forever prior to the start of a packet. After that, we | |
6330 | expect characters to arrive at a brisk pace. They should | |
6331 | show up within remote_timeout intervals. */ | |
6332 | do | |
6333 | c = readchar (timeout); | |
6334 | while (c != SERIAL_TIMEOUT && c != '$' && c != '%'); | |
c906108c SS |
6335 | |
6336 | if (c == SERIAL_TIMEOUT) | |
6337 | { | |
74531fed PA |
6338 | if (expecting_notif) |
6339 | return -1; /* Don't complain, it's normal to not get | |
6340 | anything in this case. */ | |
6341 | ||
23860348 | 6342 | if (forever) /* Watchdog went off? Kill the target. */ |
c906108c | 6343 | { |
2acceee2 | 6344 | QUIT; |
ce5ce7ed | 6345 | pop_target (); |
489eaeba | 6346 | error (_("Watchdog timeout has expired. Target detached.")); |
c906108c | 6347 | } |
c906108c | 6348 | if (remote_debug) |
0f71a2f6 | 6349 | fputs_filtered ("Timed out.\n", gdb_stdlog); |
c906108c | 6350 | } |
74531fed PA |
6351 | else |
6352 | { | |
6353 | /* We've found the start of a packet or notification. | |
6354 | Now collect the data. */ | |
6355 | val = read_frame (buf, sizeof_buf); | |
6356 | if (val >= 0) | |
6357 | break; | |
6358 | } | |
6359 | ||
6360 | serial_write (remote_desc, "-", 1); | |
c906108c | 6361 | } |
c906108c | 6362 | |
74531fed PA |
6363 | if (tries > MAX_TRIES) |
6364 | { | |
6365 | /* We have tried hard enough, and just can't receive the | |
6366 | packet/notification. Give up. */ | |
6367 | printf_unfiltered (_("Ignoring packet error, continuing...\n")); | |
c906108c | 6368 | |
74531fed PA |
6369 | /* Skip the ack char if we're in no-ack mode. */ |
6370 | if (!rs->noack_mode) | |
6371 | serial_write (remote_desc, "+", 1); | |
6372 | return -1; | |
6373 | } | |
c906108c | 6374 | |
74531fed PA |
6375 | /* If we got an ordinary packet, return that to our caller. */ |
6376 | if (c == '$') | |
c906108c SS |
6377 | { |
6378 | if (remote_debug) | |
43e526b9 | 6379 | { |
6e5abd65 PA |
6380 | struct cleanup *old_chain; |
6381 | char *str; | |
6382 | ||
6383 | str = escape_buffer (*buf, val); | |
6384 | old_chain = make_cleanup (xfree, str); | |
6385 | fprintf_unfiltered (gdb_stdlog, "Packet received: %s\n", str); | |
6386 | do_cleanups (old_chain); | |
43e526b9 | 6387 | } |
a6f3e723 SL |
6388 | |
6389 | /* Skip the ack char if we're in no-ack mode. */ | |
6390 | if (!rs->noack_mode) | |
6391 | serial_write (remote_desc, "+", 1); | |
0876f84a | 6392 | return val; |
c906108c SS |
6393 | } |
6394 | ||
74531fed PA |
6395 | /* If we got a notification, handle it, and go back to looking |
6396 | for a packet. */ | |
6397 | else | |
6398 | { | |
6399 | gdb_assert (c == '%'); | |
6400 | ||
6401 | if (remote_debug) | |
6402 | { | |
6e5abd65 PA |
6403 | struct cleanup *old_chain; |
6404 | char *str; | |
6405 | ||
6406 | str = escape_buffer (*buf, val); | |
6407 | old_chain = make_cleanup (xfree, str); | |
6408 | fprintf_unfiltered (gdb_stdlog, | |
6409 | " Notification received: %s\n", | |
6410 | str); | |
6411 | do_cleanups (old_chain); | |
74531fed | 6412 | } |
c906108c | 6413 | |
74531fed | 6414 | handle_notification (*buf, val); |
c906108c | 6415 | |
74531fed | 6416 | /* Notifications require no acknowledgement. */ |
a6f3e723 | 6417 | |
74531fed PA |
6418 | if (expecting_notif) |
6419 | return -1; | |
6420 | } | |
6421 | } | |
6422 | } | |
6423 | ||
6424 | static int | |
6425 | getpkt_sane (char **buf, long *sizeof_buf, int forever) | |
6426 | { | |
6427 | return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 0); | |
6428 | } | |
6429 | ||
6430 | static int | |
6431 | getpkt_or_notif_sane (char **buf, long *sizeof_buf, int forever) | |
6432 | { | |
6433 | return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 1); | |
c906108c | 6434 | } |
74531fed | 6435 | |
c906108c SS |
6436 | \f |
6437 | static void | |
fba45db2 | 6438 | remote_kill (void) |
43ff13b4 | 6439 | { |
23860348 MS |
6440 | /* Use catch_errors so the user can quit from gdb even when we |
6441 | aren't on speaking terms with the remote system. */ | |
c5aa993b | 6442 | catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR); |
43ff13b4 JM |
6443 | |
6444 | /* Don't wait for it to die. I'm not really sure it matters whether | |
6445 | we do or not. For the existing stubs, kill is a noop. */ | |
6446 | target_mourn_inferior (); | |
6447 | } | |
6448 | ||
82f73884 PA |
6449 | static int |
6450 | remote_vkill (int pid, struct remote_state *rs) | |
6451 | { | |
6452 | if (remote_protocol_packets[PACKET_vKill].support == PACKET_DISABLE) | |
6453 | return -1; | |
6454 | ||
6455 | /* Tell the remote target to detach. */ | |
6456 | sprintf (rs->buf, "vKill;%x", pid); | |
6457 | putpkt (rs->buf); | |
6458 | getpkt (&rs->buf, &rs->buf_size, 0); | |
6459 | ||
6460 | if (packet_ok (rs->buf, | |
6461 | &remote_protocol_packets[PACKET_vKill]) == PACKET_OK) | |
6462 | return 0; | |
6463 | else if (remote_protocol_packets[PACKET_vKill].support == PACKET_DISABLE) | |
6464 | return -1; | |
6465 | else | |
6466 | return 1; | |
6467 | } | |
6468 | ||
6469 | static void | |
6470 | extended_remote_kill (void) | |
6471 | { | |
6472 | int res; | |
6473 | int pid = ptid_get_pid (inferior_ptid); | |
6474 | struct remote_state *rs = get_remote_state (); | |
6475 | ||
6476 | res = remote_vkill (pid, rs); | |
6477 | if (res == -1 && !remote_multi_process_p (rs)) | |
6478 | { | |
6479 | /* Don't try 'k' on a multi-process aware stub -- it has no way | |
6480 | to specify the pid. */ | |
6481 | ||
6482 | putpkt ("k"); | |
6483 | #if 0 | |
6484 | getpkt (&rs->buf, &rs->buf_size, 0); | |
6485 | if (rs->buf[0] != 'O' || rs->buf[0] != 'K') | |
6486 | res = 1; | |
6487 | #else | |
6488 | /* Don't wait for it to die. I'm not really sure it matters whether | |
6489 | we do or not. For the existing stubs, kill is a noop. */ | |
6490 | res = 0; | |
6491 | #endif | |
6492 | } | |
6493 | ||
6494 | if (res != 0) | |
6495 | error (_("Can't kill process")); | |
6496 | ||
6497 | delete_inferior (pid); | |
6498 | target_mourn_inferior (); | |
6499 | } | |
6500 | ||
c906108c | 6501 | static void |
136d6dae | 6502 | remote_mourn (struct target_ops *ops) |
c906108c | 6503 | { |
136d6dae | 6504 | remote_mourn_1 (ops); |
c906108c SS |
6505 | } |
6506 | ||
c906108c SS |
6507 | /* Worker function for remote_mourn. */ |
6508 | static void | |
fba45db2 | 6509 | remote_mourn_1 (struct target_ops *target) |
c906108c SS |
6510 | { |
6511 | unpush_target (target); | |
ce5ce7ed PA |
6512 | |
6513 | /* remote_close takes care of cleaning up. */ | |
c906108c SS |
6514 | } |
6515 | ||
82f73884 PA |
6516 | static int |
6517 | select_new_thread_callback (struct thread_info *th, void* data) | |
6518 | { | |
74531fed | 6519 | if (!is_exited (th->ptid)) |
82f73884 PA |
6520 | { |
6521 | switch_to_thread (th->ptid); | |
6522 | printf_filtered (_("[Switching to %s]\n"), | |
6523 | target_pid_to_str (inferior_ptid)); | |
6524 | return 1; | |
6525 | } | |
6526 | return 0; | |
6527 | } | |
6528 | ||
2d717e4f DJ |
6529 | static void |
6530 | extended_remote_mourn_1 (struct target_ops *target) | |
6531 | { | |
6532 | struct remote_state *rs = get_remote_state (); | |
c906108c | 6533 | |
e24a49d8 PA |
6534 | /* In case we got here due to an error, but we're going to stay |
6535 | connected. */ | |
6536 | rs->waiting_for_stop_reply = 0; | |
6537 | ||
74531fed PA |
6538 | /* We're no longer interested in these events. */ |
6539 | discard_pending_stop_replies (ptid_get_pid (inferior_ptid)); | |
6540 | ||
2d717e4f DJ |
6541 | /* Unlike "target remote", we do not want to unpush the target; then |
6542 | the next time the user says "run", we won't be connected. */ | |
6543 | ||
82f73884 | 6544 | if (have_inferiors ()) |
2d717e4f | 6545 | { |
82f73884 PA |
6546 | extern void nullify_last_target_wait_ptid (); |
6547 | /* Multi-process case. The current process has exited, but | |
6548 | there are other processes to debug. Switch to the first | |
6549 | available. */ | |
6550 | iterate_over_threads (select_new_thread_callback, NULL); | |
6551 | nullify_last_target_wait_ptid (); | |
2d717e4f DJ |
6552 | } |
6553 | else | |
6554 | { | |
82f73884 PA |
6555 | struct remote_state *rs = get_remote_state (); |
6556 | ||
6557 | /* Call common code to mark the inferior as not running. */ | |
6558 | generic_mourn_inferior (); | |
6559 | if (!remote_multi_process_p (rs)) | |
6560 | { | |
6561 | /* Check whether the target is running now - some remote stubs | |
6562 | automatically restart after kill. */ | |
6563 | putpkt ("?"); | |
6564 | getpkt (&rs->buf, &rs->buf_size, 0); | |
6565 | ||
6566 | if (rs->buf[0] == 'S' || rs->buf[0] == 'T') | |
6567 | { | |
6568 | /* Assume that the target has been restarted. Set inferior_ptid | |
6569 | so that bits of core GDB realizes there's something here, e.g., | |
6570 | so that the user can say "kill" again. */ | |
6571 | inferior_ptid = magic_null_ptid; | |
6572 | } | |
6573 | else | |
6574 | { | |
6575 | /* Mark this (still pushed) target as not executable until we | |
6576 | restart it. */ | |
6577 | target_mark_exited (target); | |
6578 | } | |
6579 | } | |
6580 | else | |
6581 | /* Always remove execution if this was the last process. */ | |
6582 | target_mark_exited (target); | |
2d717e4f DJ |
6583 | } |
6584 | } | |
c906108c SS |
6585 | |
6586 | static void | |
136d6dae | 6587 | extended_remote_mourn (struct target_ops *ops) |
c906108c | 6588 | { |
136d6dae | 6589 | extended_remote_mourn_1 (ops); |
2d717e4f | 6590 | } |
c906108c | 6591 | |
2d717e4f DJ |
6592 | static int |
6593 | extended_remote_run (char *args) | |
6594 | { | |
6595 | struct remote_state *rs = get_remote_state (); | |
6596 | char *p; | |
6597 | int len; | |
c906108c | 6598 | |
2d717e4f DJ |
6599 | /* If the user has disabled vRun support, or we have detected that |
6600 | support is not available, do not try it. */ | |
6601 | if (remote_protocol_packets[PACKET_vRun].support == PACKET_DISABLE) | |
6602 | return -1; | |
424163ea | 6603 | |
2d717e4f DJ |
6604 | strcpy (rs->buf, "vRun;"); |
6605 | len = strlen (rs->buf); | |
c906108c | 6606 | |
2d717e4f DJ |
6607 | if (strlen (remote_exec_file) * 2 + len >= get_remote_packet_size ()) |
6608 | error (_("Remote file name too long for run packet")); | |
6609 | len += 2 * bin2hex ((gdb_byte *) remote_exec_file, rs->buf + len, 0); | |
6610 | ||
d1a41061 | 6611 | gdb_assert (args != NULL); |
2d717e4f DJ |
6612 | if (*args) |
6613 | { | |
6614 | struct cleanup *back_to; | |
6615 | int i; | |
6616 | char **argv; | |
6617 | ||
d1a41061 | 6618 | argv = gdb_buildargv (args); |
2d717e4f DJ |
6619 | back_to = make_cleanup ((void (*) (void *)) freeargv, argv); |
6620 | for (i = 0; argv[i] != NULL; i++) | |
6621 | { | |
6622 | if (strlen (argv[i]) * 2 + 1 + len >= get_remote_packet_size ()) | |
6623 | error (_("Argument list too long for run packet")); | |
6624 | rs->buf[len++] = ';'; | |
6625 | len += 2 * bin2hex ((gdb_byte *) argv[i], rs->buf + len, 0); | |
6626 | } | |
6627 | do_cleanups (back_to); | |
6628 | } | |
6629 | ||
6630 | rs->buf[len++] = '\0'; | |
6631 | ||
6632 | putpkt (rs->buf); | |
6633 | getpkt (&rs->buf, &rs->buf_size, 0); | |
6634 | ||
6635 | if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vRun]) == PACKET_OK) | |
6636 | { | |
6637 | /* We have a wait response; we don't need it, though. All is well. */ | |
6638 | return 0; | |
6639 | } | |
6640 | else if (remote_protocol_packets[PACKET_vRun].support == PACKET_DISABLE) | |
6641 | /* It wasn't disabled before, but it is now. */ | |
6642 | return -1; | |
6643 | else | |
6644 | { | |
6645 | if (remote_exec_file[0] == '\0') | |
6646 | error (_("Running the default executable on the remote target failed; " | |
6647 | "try \"set remote exec-file\"?")); | |
6648 | else | |
6649 | error (_("Running \"%s\" on the remote target failed"), | |
6650 | remote_exec_file); | |
6651 | } | |
c906108c SS |
6652 | } |
6653 | ||
2d717e4f DJ |
6654 | /* In the extended protocol we want to be able to do things like |
6655 | "run" and have them basically work as expected. So we need | |
6656 | a special create_inferior function. We support changing the | |
6657 | executable file and the command line arguments, but not the | |
6658 | environment. */ | |
6659 | ||
43ff13b4 | 6660 | static void |
2d717e4f | 6661 | extended_remote_create_inferior_1 (char *exec_file, char *args, |
75c99385 | 6662 | char **env, int from_tty) |
43ff13b4 | 6663 | { |
43ff13b4 | 6664 | /* If running asynchronously, register the target file descriptor |
23860348 | 6665 | with the event loop. */ |
75c99385 | 6666 | if (target_can_async_p ()) |
2acceee2 | 6667 | target_async (inferior_event_handler, 0); |
43ff13b4 JM |
6668 | |
6669 | /* Now restart the remote server. */ | |
2d717e4f DJ |
6670 | if (extended_remote_run (args) == -1) |
6671 | { | |
6672 | /* vRun was not supported. Fail if we need it to do what the | |
6673 | user requested. */ | |
6674 | if (remote_exec_file[0]) | |
6675 | error (_("Remote target does not support \"set remote exec-file\"")); | |
6676 | if (args[0]) | |
6677 | error (_("Remote target does not support \"set args\" or run <ARGS>")); | |
43ff13b4 | 6678 | |
2d717e4f DJ |
6679 | /* Fall back to "R". */ |
6680 | extended_remote_restart (); | |
6681 | } | |
424163ea | 6682 | |
45280a52 DJ |
6683 | /* Clean up from the last time we ran, before we mark the target |
6684 | running again. This will mark breakpoints uninserted, and | |
6685 | get_offsets may insert breakpoints. */ | |
6686 | init_thread_list (); | |
6687 | init_wait_for_inferior (); | |
6688 | ||
2d717e4f | 6689 | /* Now mark the inferior as running before we do anything else. */ |
79d7f229 | 6690 | inferior_ptid = magic_null_ptid; |
c0a2216e | 6691 | |
74531fed PA |
6692 | /* Now, if we have thread information, update inferior_ptid. */ |
6693 | inferior_ptid = remote_current_thread (inferior_ptid); | |
6694 | ||
82f73884 | 6695 | add_inferior (ptid_get_pid (inferior_ptid)); |
c0a2216e PA |
6696 | add_thread_silent (inferior_ptid); |
6697 | ||
75c99385 | 6698 | target_mark_running (&extended_remote_ops); |
2d717e4f DJ |
6699 | |
6700 | /* Get updated offsets, if the stub uses qOffsets. */ | |
6701 | get_offsets (); | |
2d717e4f DJ |
6702 | } |
6703 | ||
6704 | static void | |
136d6dae VP |
6705 | extended_remote_create_inferior (struct target_ops *ops, |
6706 | char *exec_file, char *args, | |
2d717e4f DJ |
6707 | char **env, int from_tty) |
6708 | { | |
75c99385 | 6709 | extended_remote_create_inferior_1 (exec_file, args, env, from_tty); |
43ff13b4 | 6710 | } |
c906108c | 6711 | \f |
c5aa993b | 6712 | |
8181d85f DJ |
6713 | /* Insert a breakpoint. On targets that have software breakpoint |
6714 | support, we ask the remote target to do the work; on targets | |
6715 | which don't, we insert a traditional memory breakpoint. */ | |
c906108c SS |
6716 | |
6717 | static int | |
8181d85f | 6718 | remote_insert_breakpoint (struct bp_target_info *bp_tgt) |
c906108c | 6719 | { |
d471ea57 AC |
6720 | /* Try the "Z" s/w breakpoint packet if it is not already disabled. |
6721 | If it succeeds, then set the support to PACKET_ENABLE. If it | |
6722 | fails, and the user has explicitly requested the Z support then | |
23860348 | 6723 | report an error, otherwise, mark it disabled and go on. */ |
802188a7 | 6724 | |
444abaca | 6725 | if (remote_protocol_packets[PACKET_Z0].support != PACKET_DISABLE) |
96baa820 | 6726 | { |
7c0f6dcc | 6727 | CORE_ADDR addr = bp_tgt->placed_address; |
4fff2411 JZ |
6728 | struct remote_state *rs; |
6729 | char *p; | |
7c0f6dcc | 6730 | int bpsize; |
4fff2411 | 6731 | |
1cf3db46 | 6732 | gdbarch_breakpoint_from_pc (target_gdbarch, &addr, &bpsize); |
4fff2411 JZ |
6733 | |
6734 | rs = get_remote_state (); | |
6735 | p = rs->buf; | |
802188a7 | 6736 | |
96baa820 JM |
6737 | *(p++) = 'Z'; |
6738 | *(p++) = '0'; | |
6739 | *(p++) = ','; | |
7c0f6dcc | 6740 | addr = (ULONGEST) remote_address_masked (addr); |
8181d85f | 6741 | p += hexnumstr (p, addr); |
7c0f6dcc | 6742 | sprintf (p, ",%d", bpsize); |
802188a7 | 6743 | |
6d820c5c DJ |
6744 | putpkt (rs->buf); |
6745 | getpkt (&rs->buf, &rs->buf_size, 0); | |
96baa820 | 6746 | |
6d820c5c | 6747 | switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0])) |
96baa820 | 6748 | { |
d471ea57 AC |
6749 | case PACKET_ERROR: |
6750 | return -1; | |
6751 | case PACKET_OK: | |
7c0f6dcc JL |
6752 | bp_tgt->placed_address = addr; |
6753 | bp_tgt->placed_size = bpsize; | |
d471ea57 AC |
6754 | return 0; |
6755 | case PACKET_UNKNOWN: | |
6756 | break; | |
96baa820 JM |
6757 | } |
6758 | } | |
c906108c | 6759 | |
8181d85f | 6760 | return memory_insert_breakpoint (bp_tgt); |
c906108c SS |
6761 | } |
6762 | ||
6763 | static int | |
8181d85f | 6764 | remote_remove_breakpoint (struct bp_target_info *bp_tgt) |
c906108c | 6765 | { |
8181d85f | 6766 | CORE_ADDR addr = bp_tgt->placed_address; |
d01949b6 | 6767 | struct remote_state *rs = get_remote_state (); |
96baa820 JM |
6768 | int bp_size; |
6769 | ||
444abaca | 6770 | if (remote_protocol_packets[PACKET_Z0].support != PACKET_DISABLE) |
96baa820 | 6771 | { |
6d820c5c | 6772 | char *p = rs->buf; |
802188a7 | 6773 | |
96baa820 JM |
6774 | *(p++) = 'z'; |
6775 | *(p++) = '0'; | |
6776 | *(p++) = ','; | |
6777 | ||
8181d85f DJ |
6778 | addr = (ULONGEST) remote_address_masked (bp_tgt->placed_address); |
6779 | p += hexnumstr (p, addr); | |
6780 | sprintf (p, ",%d", bp_tgt->placed_size); | |
802188a7 | 6781 | |
6d820c5c DJ |
6782 | putpkt (rs->buf); |
6783 | getpkt (&rs->buf, &rs->buf_size, 0); | |
96baa820 | 6784 | |
6d820c5c | 6785 | return (rs->buf[0] == 'E'); |
96baa820 JM |
6786 | } |
6787 | ||
8181d85f | 6788 | return memory_remove_breakpoint (bp_tgt); |
c906108c SS |
6789 | } |
6790 | ||
d471ea57 AC |
6791 | static int |
6792 | watchpoint_to_Z_packet (int type) | |
6793 | { | |
6794 | switch (type) | |
6795 | { | |
6796 | case hw_write: | |
bb858e6a | 6797 | return Z_PACKET_WRITE_WP; |
d471ea57 AC |
6798 | break; |
6799 | case hw_read: | |
bb858e6a | 6800 | return Z_PACKET_READ_WP; |
d471ea57 AC |
6801 | break; |
6802 | case hw_access: | |
bb858e6a | 6803 | return Z_PACKET_ACCESS_WP; |
d471ea57 AC |
6804 | break; |
6805 | default: | |
8e65ff28 | 6806 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 6807 | _("hw_bp_to_z: bad watchpoint type %d"), type); |
d471ea57 AC |
6808 | } |
6809 | } | |
6810 | ||
3c3bea1c | 6811 | static int |
fba45db2 | 6812 | remote_insert_watchpoint (CORE_ADDR addr, int len, int type) |
96baa820 | 6813 | { |
d01949b6 | 6814 | struct remote_state *rs = get_remote_state (); |
e514a9d6 | 6815 | char *p; |
d471ea57 | 6816 | enum Z_packet_type packet = watchpoint_to_Z_packet (type); |
96baa820 | 6817 | |
444abaca | 6818 | if (remote_protocol_packets[PACKET_Z0 + packet].support == PACKET_DISABLE) |
5cffb350 | 6819 | return -1; |
802188a7 | 6820 | |
6d820c5c DJ |
6821 | sprintf (rs->buf, "Z%x,", packet); |
6822 | p = strchr (rs->buf, '\0'); | |
96baa820 JM |
6823 | addr = remote_address_masked (addr); |
6824 | p += hexnumstr (p, (ULONGEST) addr); | |
d4f3574e | 6825 | sprintf (p, ",%x", len); |
802188a7 | 6826 | |
6d820c5c DJ |
6827 | putpkt (rs->buf); |
6828 | getpkt (&rs->buf, &rs->buf_size, 0); | |
96baa820 | 6829 | |
6d820c5c | 6830 | switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet])) |
d471ea57 AC |
6831 | { |
6832 | case PACKET_ERROR: | |
6833 | case PACKET_UNKNOWN: | |
6834 | return -1; | |
6835 | case PACKET_OK: | |
6836 | return 0; | |
6837 | } | |
8e65ff28 | 6838 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 6839 | _("remote_insert_watchpoint: reached end of function")); |
96baa820 JM |
6840 | } |
6841 | ||
d471ea57 | 6842 | |
3c3bea1c | 6843 | static int |
fba45db2 | 6844 | remote_remove_watchpoint (CORE_ADDR addr, int len, int type) |
96baa820 | 6845 | { |
d01949b6 | 6846 | struct remote_state *rs = get_remote_state (); |
e514a9d6 | 6847 | char *p; |
d471ea57 AC |
6848 | enum Z_packet_type packet = watchpoint_to_Z_packet (type); |
6849 | ||
444abaca | 6850 | if (remote_protocol_packets[PACKET_Z0 + packet].support == PACKET_DISABLE) |
5cffb350 | 6851 | return -1; |
802188a7 | 6852 | |
6d820c5c DJ |
6853 | sprintf (rs->buf, "z%x,", packet); |
6854 | p = strchr (rs->buf, '\0'); | |
96baa820 JM |
6855 | addr = remote_address_masked (addr); |
6856 | p += hexnumstr (p, (ULONGEST) addr); | |
d4f3574e | 6857 | sprintf (p, ",%x", len); |
6d820c5c DJ |
6858 | putpkt (rs->buf); |
6859 | getpkt (&rs->buf, &rs->buf_size, 0); | |
96baa820 | 6860 | |
6d820c5c | 6861 | switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet])) |
d471ea57 AC |
6862 | { |
6863 | case PACKET_ERROR: | |
6864 | case PACKET_UNKNOWN: | |
6865 | return -1; | |
6866 | case PACKET_OK: | |
6867 | return 0; | |
6868 | } | |
8e65ff28 | 6869 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 6870 | _("remote_remove_watchpoint: reached end of function")); |
96baa820 JM |
6871 | } |
6872 | ||
3c3bea1c | 6873 | |
501eef12 AC |
6874 | int remote_hw_watchpoint_limit = -1; |
6875 | int remote_hw_breakpoint_limit = -1; | |
d471ea57 | 6876 | |
b9362cc7 | 6877 | static int |
3c3bea1c | 6878 | remote_check_watch_resources (int type, int cnt, int ot) |
96baa820 | 6879 | { |
3c3bea1c GS |
6880 | if (type == bp_hardware_breakpoint) |
6881 | { | |
6882 | if (remote_hw_breakpoint_limit == 0) | |
6883 | return 0; | |
501eef12 AC |
6884 | else if (remote_hw_breakpoint_limit < 0) |
6885 | return 1; | |
3c3bea1c GS |
6886 | else if (cnt <= remote_hw_breakpoint_limit) |
6887 | return 1; | |
6888 | } | |
6889 | else | |
6890 | { | |
6891 | if (remote_hw_watchpoint_limit == 0) | |
6892 | return 0; | |
501eef12 AC |
6893 | else if (remote_hw_watchpoint_limit < 0) |
6894 | return 1; | |
3c3bea1c GS |
6895 | else if (ot) |
6896 | return -1; | |
6897 | else if (cnt <= remote_hw_watchpoint_limit) | |
6898 | return 1; | |
6899 | } | |
6900 | return -1; | |
6901 | } | |
6902 | ||
b9362cc7 | 6903 | static int |
3c3bea1c GS |
6904 | remote_stopped_by_watchpoint (void) |
6905 | { | |
82f73884 | 6906 | return remote_stopped_by_watchpoint_p; |
3c3bea1c GS |
6907 | } |
6908 | ||
4aa7a7f5 JJ |
6909 | static int |
6910 | remote_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p) | |
3c3bea1c | 6911 | { |
4aa7a7f5 | 6912 | int rc = 0; |
d983da9c | 6913 | if (remote_stopped_by_watchpoint ()) |
4aa7a7f5 JJ |
6914 | { |
6915 | *addr_p = remote_watch_data_address; | |
6916 | rc = 1; | |
6917 | } | |
6918 | ||
6919 | return rc; | |
3c3bea1c GS |
6920 | } |
6921 | ||
6922 | ||
6923 | static int | |
8181d85f | 6924 | remote_insert_hw_breakpoint (struct bp_target_info *bp_tgt) |
3c3bea1c | 6925 | { |
8181d85f | 6926 | CORE_ADDR addr; |
4fff2411 JZ |
6927 | struct remote_state *rs; |
6928 | char *p; | |
802188a7 | 6929 | |
c8189ed1 | 6930 | /* The length field should be set to the size of a breakpoint |
8181d85f | 6931 | instruction, even though we aren't inserting one ourselves. */ |
c8189ed1 | 6932 | |
3b3b875c | 6933 | gdbarch_breakpoint_from_pc |
1cf3db46 | 6934 | (target_gdbarch, &bp_tgt->placed_address, &bp_tgt->placed_size); |
3c3bea1c | 6935 | |
444abaca | 6936 | if (remote_protocol_packets[PACKET_Z1].support == PACKET_DISABLE) |
5cffb350 | 6937 | return -1; |
2bc416ba | 6938 | |
4fff2411 JZ |
6939 | rs = get_remote_state (); |
6940 | p = rs->buf; | |
6941 | ||
96baa820 JM |
6942 | *(p++) = 'Z'; |
6943 | *(p++) = '1'; | |
6944 | *(p++) = ','; | |
802188a7 | 6945 | |
8181d85f | 6946 | addr = remote_address_masked (bp_tgt->placed_address); |
96baa820 | 6947 | p += hexnumstr (p, (ULONGEST) addr); |
8181d85f | 6948 | sprintf (p, ",%x", bp_tgt->placed_size); |
96baa820 | 6949 | |
6d820c5c DJ |
6950 | putpkt (rs->buf); |
6951 | getpkt (&rs->buf, &rs->buf_size, 0); | |
96baa820 | 6952 | |
6d820c5c | 6953 | switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1])) |
d471ea57 AC |
6954 | { |
6955 | case PACKET_ERROR: | |
6956 | case PACKET_UNKNOWN: | |
6957 | return -1; | |
6958 | case PACKET_OK: | |
6959 | return 0; | |
6960 | } | |
8e65ff28 | 6961 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 6962 | _("remote_insert_hw_breakpoint: reached end of function")); |
96baa820 JM |
6963 | } |
6964 | ||
d471ea57 | 6965 | |
802188a7 | 6966 | static int |
8181d85f | 6967 | remote_remove_hw_breakpoint (struct bp_target_info *bp_tgt) |
96baa820 | 6968 | { |
8181d85f | 6969 | CORE_ADDR addr; |
d01949b6 | 6970 | struct remote_state *rs = get_remote_state (); |
6d820c5c | 6971 | char *p = rs->buf; |
c8189ed1 | 6972 | |
444abaca | 6973 | if (remote_protocol_packets[PACKET_Z1].support == PACKET_DISABLE) |
5cffb350 | 6974 | return -1; |
802188a7 | 6975 | |
96baa820 JM |
6976 | *(p++) = 'z'; |
6977 | *(p++) = '1'; | |
6978 | *(p++) = ','; | |
802188a7 | 6979 | |
8181d85f | 6980 | addr = remote_address_masked (bp_tgt->placed_address); |
96baa820 | 6981 | p += hexnumstr (p, (ULONGEST) addr); |
8181d85f | 6982 | sprintf (p, ",%x", bp_tgt->placed_size); |
96baa820 | 6983 | |
6d820c5c DJ |
6984 | putpkt (rs->buf); |
6985 | getpkt (&rs->buf, &rs->buf_size, 0); | |
802188a7 | 6986 | |
6d820c5c | 6987 | switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1])) |
d471ea57 AC |
6988 | { |
6989 | case PACKET_ERROR: | |
6990 | case PACKET_UNKNOWN: | |
6991 | return -1; | |
6992 | case PACKET_OK: | |
6993 | return 0; | |
6994 | } | |
8e65ff28 | 6995 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 6996 | _("remote_remove_hw_breakpoint: reached end of function")); |
96baa820 | 6997 | } |
96baa820 | 6998 | |
23860348 | 6999 | /* Table used by the crc32 function to calcuate the checksum. */ |
c906108c | 7000 | |
c5aa993b JM |
7001 | static unsigned long crc32_table[256] = |
7002 | {0, 0}; | |
c906108c SS |
7003 | |
7004 | static unsigned long | |
fba45db2 | 7005 | crc32 (unsigned char *buf, int len, unsigned int crc) |
c906108c | 7006 | { |
c5aa993b | 7007 | if (!crc32_table[1]) |
c906108c | 7008 | { |
23860348 | 7009 | /* Initialize the CRC table and the decoding table. */ |
c906108c SS |
7010 | int i, j; |
7011 | unsigned int c; | |
7012 | ||
7013 | for (i = 0; i < 256; i++) | |
c5aa993b JM |
7014 | { |
7015 | for (c = i << 24, j = 8; j > 0; --j) | |
7016 | c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1); | |
7017 | crc32_table[i] = c; | |
7018 | } | |
c906108c SS |
7019 | } |
7020 | ||
7021 | while (len--) | |
7022 | { | |
7023 | crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buf) & 255]; | |
7024 | buf++; | |
7025 | } | |
7026 | return crc; | |
7027 | } | |
7028 | ||
7029 | /* compare-sections command | |
7030 | ||
7031 | With no arguments, compares each loadable section in the exec bfd | |
7032 | with the same memory range on the target, and reports mismatches. | |
7033 | Useful for verifying the image on the target against the exec file. | |
7034 | Depends on the target understanding the new "qCRC:" request. */ | |
7035 | ||
e514a9d6 JM |
7036 | /* FIXME: cagney/1999-10-26: This command should be broken down into a |
7037 | target method (target verify memory) and generic version of the | |
7038 | actual command. This will allow other high-level code (especially | |
23860348 | 7039 | generic_load()) to make use of this target functionality. */ |
e514a9d6 | 7040 | |
c906108c | 7041 | static void |
fba45db2 | 7042 | compare_sections_command (char *args, int from_tty) |
c906108c | 7043 | { |
d01949b6 | 7044 | struct remote_state *rs = get_remote_state (); |
c906108c SS |
7045 | asection *s; |
7046 | unsigned long host_crc, target_crc; | |
7047 | extern bfd *exec_bfd; | |
7048 | struct cleanup *old_chain; | |
085dd6e6 JM |
7049 | char *tmp; |
7050 | char *sectdata; | |
ce359b09 | 7051 | const char *sectname; |
c906108c SS |
7052 | bfd_size_type size; |
7053 | bfd_vma lma; | |
7054 | int matched = 0; | |
7055 | int mismatched = 0; | |
7056 | ||
7057 | if (!exec_bfd) | |
8a3fe4f8 | 7058 | error (_("command cannot be used without an exec file")); |
c906108c SS |
7059 | if (!current_target.to_shortname || |
7060 | strcmp (current_target.to_shortname, "remote") != 0) | |
8a3fe4f8 | 7061 | error (_("command can only be used with remote target")); |
c906108c | 7062 | |
c5aa993b | 7063 | for (s = exec_bfd->sections; s; s = s->next) |
c906108c SS |
7064 | { |
7065 | if (!(s->flags & SEC_LOAD)) | |
c5aa993b | 7066 | continue; /* skip non-loadable section */ |
c906108c | 7067 | |
2c500098 | 7068 | size = bfd_get_section_size (s); |
c906108c | 7069 | if (size == 0) |
c5aa993b | 7070 | continue; /* skip zero-length section */ |
c906108c | 7071 | |
ce359b09 | 7072 | sectname = bfd_get_section_name (exec_bfd, s); |
c906108c | 7073 | if (args && strcmp (args, sectname) != 0) |
c5aa993b | 7074 | continue; /* not the section selected by user */ |
c906108c | 7075 | |
c5aa993b | 7076 | matched = 1; /* do this section */ |
c906108c | 7077 | lma = s->lma; |
23860348 | 7078 | /* FIXME: assumes lma can fit into long. */ |
ea9c271d | 7079 | xsnprintf (rs->buf, get_remote_packet_size (), "qCRC:%lx,%lx", |
ecbc58df | 7080 | (long) lma, (long) size); |
6d820c5c | 7081 | putpkt (rs->buf); |
c906108c | 7082 | |
23860348 MS |
7083 | /* Be clever; compute the host_crc before waiting for target |
7084 | reply. */ | |
c906108c | 7085 | sectdata = xmalloc (size); |
b8c9b27d | 7086 | old_chain = make_cleanup (xfree, sectdata); |
c906108c SS |
7087 | bfd_get_section_contents (exec_bfd, s, sectdata, 0, size); |
7088 | host_crc = crc32 ((unsigned char *) sectdata, size, 0xffffffff); | |
7089 | ||
6d820c5c DJ |
7090 | getpkt (&rs->buf, &rs->buf_size, 0); |
7091 | if (rs->buf[0] == 'E') | |
8a3fe4f8 | 7092 | error (_("target memory fault, section %s, range 0x%s -- 0x%s"), |
823ca731 | 7093 | sectname, paddr (lma), paddr (lma + size)); |
6d820c5c | 7094 | if (rs->buf[0] != 'C') |
8a3fe4f8 | 7095 | error (_("remote target does not support this operation")); |
c906108c | 7096 | |
6d820c5c | 7097 | for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++) |
c906108c SS |
7098 | target_crc = target_crc * 16 + fromhex (*tmp); |
7099 | ||
d4f3574e SS |
7100 | printf_filtered ("Section %s, range 0x%s -- 0x%s: ", |
7101 | sectname, paddr (lma), paddr (lma + size)); | |
c906108c SS |
7102 | if (host_crc == target_crc) |
7103 | printf_filtered ("matched.\n"); | |
7104 | else | |
c5aa993b JM |
7105 | { |
7106 | printf_filtered ("MIS-MATCHED!\n"); | |
7107 | mismatched++; | |
7108 | } | |
c906108c SS |
7109 | |
7110 | do_cleanups (old_chain); | |
7111 | } | |
7112 | if (mismatched > 0) | |
8a3fe4f8 AC |
7113 | warning (_("One or more sections of the remote executable does not match\n\ |
7114 | the loaded file\n")); | |
c906108c | 7115 | if (args && !matched) |
a3f17187 | 7116 | printf_filtered (_("No loaded section named '%s'.\n"), args); |
c906108c SS |
7117 | } |
7118 | ||
0e7f50da UW |
7119 | /* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET |
7120 | into remote target. The number of bytes written to the remote | |
7121 | target is returned, or -1 for error. */ | |
7122 | ||
7123 | static LONGEST | |
7124 | remote_write_qxfer (struct target_ops *ops, const char *object_name, | |
7125 | const char *annex, const gdb_byte *writebuf, | |
7126 | ULONGEST offset, LONGEST len, | |
7127 | struct packet_config *packet) | |
7128 | { | |
7129 | int i, buf_len; | |
7130 | ULONGEST n; | |
7131 | gdb_byte *wbuf; | |
7132 | struct remote_state *rs = get_remote_state (); | |
7133 | int max_size = get_memory_write_packet_size (); | |
7134 | ||
7135 | if (packet->support == PACKET_DISABLE) | |
7136 | return -1; | |
7137 | ||
7138 | /* Insert header. */ | |
7139 | i = snprintf (rs->buf, max_size, | |
7140 | "qXfer:%s:write:%s:%s:", | |
7141 | object_name, annex ? annex : "", | |
7142 | phex_nz (offset, sizeof offset)); | |
7143 | max_size -= (i + 1); | |
7144 | ||
7145 | /* Escape as much data as fits into rs->buf. */ | |
7146 | buf_len = remote_escape_output | |
7147 | (writebuf, len, (rs->buf + i), &max_size, max_size); | |
7148 | ||
7149 | if (putpkt_binary (rs->buf, i + buf_len) < 0 | |
7150 | || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0 | |
7151 | || packet_ok (rs->buf, packet) != PACKET_OK) | |
7152 | return -1; | |
7153 | ||
7154 | unpack_varlen_hex (rs->buf, &n); | |
7155 | return n; | |
7156 | } | |
7157 | ||
0876f84a DJ |
7158 | /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet. |
7159 | Data at OFFSET, of up to LEN bytes, is read into READBUF; the | |
7160 | number of bytes read is returned, or 0 for EOF, or -1 for error. | |
7161 | The number of bytes read may be less than LEN without indicating an | |
7162 | EOF. PACKET is checked and updated to indicate whether the remote | |
7163 | target supports this object. */ | |
7164 | ||
7165 | static LONGEST | |
7166 | remote_read_qxfer (struct target_ops *ops, const char *object_name, | |
7167 | const char *annex, | |
7168 | gdb_byte *readbuf, ULONGEST offset, LONGEST len, | |
7169 | struct packet_config *packet) | |
7170 | { | |
7171 | static char *finished_object; | |
7172 | static char *finished_annex; | |
7173 | static ULONGEST finished_offset; | |
7174 | ||
7175 | struct remote_state *rs = get_remote_state (); | |
7176 | unsigned int total = 0; | |
7177 | LONGEST i, n, packet_len; | |
7178 | ||
7179 | if (packet->support == PACKET_DISABLE) | |
7180 | return -1; | |
7181 | ||
7182 | /* Check whether we've cached an end-of-object packet that matches | |
7183 | this request. */ | |
7184 | if (finished_object) | |
7185 | { | |
7186 | if (strcmp (object_name, finished_object) == 0 | |
7187 | && strcmp (annex ? annex : "", finished_annex) == 0 | |
7188 | && offset == finished_offset) | |
7189 | return 0; | |
7190 | ||
7191 | /* Otherwise, we're now reading something different. Discard | |
7192 | the cache. */ | |
7193 | xfree (finished_object); | |
7194 | xfree (finished_annex); | |
7195 | finished_object = NULL; | |
7196 | finished_annex = NULL; | |
7197 | } | |
7198 | ||
7199 | /* Request only enough to fit in a single packet. The actual data | |
7200 | may not, since we don't know how much of it will need to be escaped; | |
7201 | the target is free to respond with slightly less data. We subtract | |
7202 | five to account for the response type and the protocol frame. */ | |
7203 | n = min (get_remote_packet_size () - 5, len); | |
7204 | snprintf (rs->buf, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s", | |
7205 | object_name, annex ? annex : "", | |
7206 | phex_nz (offset, sizeof offset), | |
7207 | phex_nz (n, sizeof n)); | |
7208 | i = putpkt (rs->buf); | |
7209 | if (i < 0) | |
7210 | return -1; | |
7211 | ||
7212 | rs->buf[0] = '\0'; | |
7213 | packet_len = getpkt_sane (&rs->buf, &rs->buf_size, 0); | |
7214 | if (packet_len < 0 || packet_ok (rs->buf, packet) != PACKET_OK) | |
7215 | return -1; | |
7216 | ||
7217 | if (rs->buf[0] != 'l' && rs->buf[0] != 'm') | |
7218 | error (_("Unknown remote qXfer reply: %s"), rs->buf); | |
7219 | ||
7220 | /* 'm' means there is (or at least might be) more data after this | |
7221 | batch. That does not make sense unless there's at least one byte | |
7222 | of data in this reply. */ | |
7223 | if (rs->buf[0] == 'm' && packet_len == 1) | |
7224 | error (_("Remote qXfer reply contained no data.")); | |
7225 | ||
7226 | /* Got some data. */ | |
7227 | i = remote_unescape_input (rs->buf + 1, packet_len - 1, readbuf, n); | |
7228 | ||
7229 | /* 'l' is an EOF marker, possibly including a final block of data, | |
0e7f50da UW |
7230 | or possibly empty. If we have the final block of a non-empty |
7231 | object, record this fact to bypass a subsequent partial read. */ | |
7232 | if (rs->buf[0] == 'l' && offset + i > 0) | |
0876f84a DJ |
7233 | { |
7234 | finished_object = xstrdup (object_name); | |
7235 | finished_annex = xstrdup (annex ? annex : ""); | |
7236 | finished_offset = offset + i; | |
7237 | } | |
7238 | ||
7239 | return i; | |
7240 | } | |
7241 | ||
1e3ff5ad | 7242 | static LONGEST |
4b8a223f | 7243 | remote_xfer_partial (struct target_ops *ops, enum target_object object, |
961cb7b5 MK |
7244 | const char *annex, gdb_byte *readbuf, |
7245 | const gdb_byte *writebuf, ULONGEST offset, LONGEST len) | |
c906108c | 7246 | { |
82f73884 | 7247 | struct remote_state *rs; |
c906108c | 7248 | int i; |
6d820c5c | 7249 | char *p2; |
1e3ff5ad | 7250 | char query_type; |
c906108c | 7251 | |
82f73884 PA |
7252 | set_general_thread (inferior_ptid); |
7253 | ||
7254 | rs = get_remote_state (); | |
7255 | ||
b2182ed2 | 7256 | /* Handle memory using the standard memory routines. */ |
21e3b9b9 DJ |
7257 | if (object == TARGET_OBJECT_MEMORY) |
7258 | { | |
7259 | int xfered; | |
7260 | errno = 0; | |
7261 | ||
2d717e4f DJ |
7262 | /* If the remote target is connected but not running, we should |
7263 | pass this request down to a lower stratum (e.g. the executable | |
7264 | file). */ | |
7265 | if (!target_has_execution) | |
7266 | return 0; | |
7267 | ||
21e3b9b9 | 7268 | if (writebuf != NULL) |
b2182ed2 | 7269 | xfered = remote_write_bytes (offset, writebuf, len); |
21e3b9b9 | 7270 | else |
b2182ed2 | 7271 | xfered = remote_read_bytes (offset, readbuf, len); |
21e3b9b9 DJ |
7272 | |
7273 | if (xfered > 0) | |
7274 | return xfered; | |
7275 | else if (xfered == 0 && errno == 0) | |
7276 | return 0; | |
7277 | else | |
7278 | return -1; | |
7279 | } | |
7280 | ||
0e7f50da UW |
7281 | /* Handle SPU memory using qxfer packets. */ |
7282 | if (object == TARGET_OBJECT_SPU) | |
7283 | { | |
7284 | if (readbuf) | |
7285 | return remote_read_qxfer (ops, "spu", annex, readbuf, offset, len, | |
7286 | &remote_protocol_packets | |
7287 | [PACKET_qXfer_spu_read]); | |
7288 | else | |
7289 | return remote_write_qxfer (ops, "spu", annex, writebuf, offset, len, | |
7290 | &remote_protocol_packets | |
7291 | [PACKET_qXfer_spu_write]); | |
7292 | } | |
7293 | ||
a76d924d DJ |
7294 | /* Only handle flash writes. */ |
7295 | if (writebuf != NULL) | |
7296 | { | |
7297 | LONGEST xfered; | |
7298 | ||
7299 | switch (object) | |
7300 | { | |
7301 | case TARGET_OBJECT_FLASH: | |
7302 | xfered = remote_flash_write (ops, offset, len, writebuf); | |
7303 | ||
7304 | if (xfered > 0) | |
7305 | return xfered; | |
7306 | else if (xfered == 0 && errno == 0) | |
7307 | return 0; | |
7308 | else | |
7309 | return -1; | |
7310 | ||
7311 | default: | |
7312 | return -1; | |
7313 | } | |
7314 | } | |
4b8a223f | 7315 | |
1e3ff5ad AC |
7316 | /* Map pre-existing objects onto letters. DO NOT do this for new |
7317 | objects!!! Instead specify new query packets. */ | |
7318 | switch (object) | |
c906108c | 7319 | { |
1e3ff5ad AC |
7320 | case TARGET_OBJECT_AVR: |
7321 | query_type = 'R'; | |
7322 | break; | |
802188a7 RM |
7323 | |
7324 | case TARGET_OBJECT_AUXV: | |
0876f84a DJ |
7325 | gdb_assert (annex == NULL); |
7326 | return remote_read_qxfer (ops, "auxv", annex, readbuf, offset, len, | |
7327 | &remote_protocol_packets[PACKET_qXfer_auxv]); | |
802188a7 | 7328 | |
23181151 DJ |
7329 | case TARGET_OBJECT_AVAILABLE_FEATURES: |
7330 | return remote_read_qxfer | |
7331 | (ops, "features", annex, readbuf, offset, len, | |
7332 | &remote_protocol_packets[PACKET_qXfer_features]); | |
7333 | ||
cfa9d6d9 DJ |
7334 | case TARGET_OBJECT_LIBRARIES: |
7335 | return remote_read_qxfer | |
7336 | (ops, "libraries", annex, readbuf, offset, len, | |
7337 | &remote_protocol_packets[PACKET_qXfer_libraries]); | |
7338 | ||
fd79ecee DJ |
7339 | case TARGET_OBJECT_MEMORY_MAP: |
7340 | gdb_assert (annex == NULL); | |
7341 | return remote_read_qxfer (ops, "memory-map", annex, readbuf, offset, len, | |
7342 | &remote_protocol_packets[PACKET_qXfer_memory_map]); | |
7343 | ||
1e3ff5ad | 7344 | default: |
c906108c SS |
7345 | return -1; |
7346 | } | |
7347 | ||
4b8a223f | 7348 | /* Note: a zero OFFSET and LEN can be used to query the minimum |
1e3ff5ad | 7349 | buffer size. */ |
4b8a223f | 7350 | if (offset == 0 && len == 0) |
ea9c271d DJ |
7351 | return (get_remote_packet_size ()); |
7352 | /* Minimum outbuf size is get_remote_packet_size (). If LEN is not | |
24b06219 | 7353 | large enough let the caller deal with it. */ |
ea9c271d | 7354 | if (len < get_remote_packet_size ()) |
1e3ff5ad | 7355 | return -1; |
ea9c271d | 7356 | len = get_remote_packet_size (); |
1e3ff5ad | 7357 | |
23860348 | 7358 | /* Except for querying the minimum buffer size, target must be open. */ |
c5aa993b | 7359 | if (!remote_desc) |
8a3fe4f8 | 7360 | error (_("remote query is only available after target open")); |
c906108c | 7361 | |
1e3ff5ad | 7362 | gdb_assert (annex != NULL); |
4b8a223f | 7363 | gdb_assert (readbuf != NULL); |
c906108c | 7364 | |
6d820c5c | 7365 | p2 = rs->buf; |
c906108c SS |
7366 | *p2++ = 'q'; |
7367 | *p2++ = query_type; | |
7368 | ||
23860348 MS |
7369 | /* We used one buffer char for the remote protocol q command and |
7370 | another for the query type. As the remote protocol encapsulation | |
7371 | uses 4 chars plus one extra in case we are debugging | |
7372 | (remote_debug), we have PBUFZIZ - 7 left to pack the query | |
7373 | string. */ | |
c906108c | 7374 | i = 0; |
ea9c271d | 7375 | while (annex[i] && (i < (get_remote_packet_size () - 8))) |
c906108c | 7376 | { |
1e3ff5ad AC |
7377 | /* Bad caller may have sent forbidden characters. */ |
7378 | gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#'); | |
7379 | *p2++ = annex[i]; | |
c906108c SS |
7380 | i++; |
7381 | } | |
1e3ff5ad AC |
7382 | *p2 = '\0'; |
7383 | gdb_assert (annex[i] == '\0'); | |
c906108c | 7384 | |
6d820c5c | 7385 | i = putpkt (rs->buf); |
c5aa993b JM |
7386 | if (i < 0) |
7387 | return i; | |
c906108c | 7388 | |
6d820c5c DJ |
7389 | getpkt (&rs->buf, &rs->buf_size, 0); |
7390 | strcpy ((char *) readbuf, rs->buf); | |
c906108c | 7391 | |
cfd77fa1 | 7392 | return strlen ((char *) readbuf); |
c906108c SS |
7393 | } |
7394 | ||
08388c79 DE |
7395 | static int |
7396 | remote_search_memory (struct target_ops* ops, | |
7397 | CORE_ADDR start_addr, ULONGEST search_space_len, | |
7398 | const gdb_byte *pattern, ULONGEST pattern_len, | |
7399 | CORE_ADDR *found_addrp) | |
7400 | { | |
7401 | struct remote_state *rs = get_remote_state (); | |
7402 | int max_size = get_memory_write_packet_size (); | |
7403 | struct packet_config *packet = | |
7404 | &remote_protocol_packets[PACKET_qSearch_memory]; | |
7405 | /* number of packet bytes used to encode the pattern, | |
7406 | this could be more than PATTERN_LEN due to escape characters */ | |
7407 | int escaped_pattern_len; | |
7408 | /* amount of pattern that was encodable in the packet */ | |
7409 | int used_pattern_len; | |
7410 | int i; | |
7411 | int found; | |
7412 | ULONGEST found_addr; | |
7413 | ||
7414 | /* Don't go to the target if we don't have to. | |
7415 | This is done before checking packet->support to avoid the possibility that | |
7416 | a success for this edge case means the facility works in general. */ | |
7417 | if (pattern_len > search_space_len) | |
7418 | return 0; | |
7419 | if (pattern_len == 0) | |
7420 | { | |
7421 | *found_addrp = start_addr; | |
7422 | return 1; | |
7423 | } | |
7424 | ||
7425 | /* If we already know the packet isn't supported, fall back to the simple | |
7426 | way of searching memory. */ | |
7427 | ||
7428 | if (packet->support == PACKET_DISABLE) | |
7429 | { | |
7430 | /* Target doesn't provided special support, fall back and use the | |
7431 | standard support (copy memory and do the search here). */ | |
7432 | return simple_search_memory (ops, start_addr, search_space_len, | |
7433 | pattern, pattern_len, found_addrp); | |
7434 | } | |
7435 | ||
7436 | /* Insert header. */ | |
7437 | i = snprintf (rs->buf, max_size, | |
7438 | "qSearch:memory:%s;%s;", | |
7439 | paddr_nz (start_addr), | |
7440 | phex_nz (search_space_len, sizeof (search_space_len))); | |
7441 | max_size -= (i + 1); | |
7442 | ||
7443 | /* Escape as much data as fits into rs->buf. */ | |
7444 | escaped_pattern_len = | |
7445 | remote_escape_output (pattern, pattern_len, (rs->buf + i), | |
7446 | &used_pattern_len, max_size); | |
7447 | ||
7448 | /* Bail if the pattern is too large. */ | |
7449 | if (used_pattern_len != pattern_len) | |
10e0fa18 | 7450 | error ("Pattern is too large to transmit to remote target."); |
08388c79 DE |
7451 | |
7452 | if (putpkt_binary (rs->buf, i + escaped_pattern_len) < 0 | |
7453 | || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0 | |
7454 | || packet_ok (rs->buf, packet) != PACKET_OK) | |
7455 | { | |
7456 | /* The request may not have worked because the command is not | |
7457 | supported. If so, fall back to the simple way. */ | |
7458 | if (packet->support == PACKET_DISABLE) | |
7459 | { | |
7460 | return simple_search_memory (ops, start_addr, search_space_len, | |
7461 | pattern, pattern_len, found_addrp); | |
7462 | } | |
7463 | return -1; | |
7464 | } | |
7465 | ||
7466 | if (rs->buf[0] == '0') | |
7467 | found = 0; | |
7468 | else if (rs->buf[0] == '1') | |
7469 | { | |
7470 | found = 1; | |
7471 | if (rs->buf[1] != ',') | |
10e0fa18 | 7472 | error (_("Unknown qSearch:memory reply: %s"), rs->buf); |
08388c79 DE |
7473 | unpack_varlen_hex (rs->buf + 2, &found_addr); |
7474 | *found_addrp = found_addr; | |
7475 | } | |
7476 | else | |
10e0fa18 | 7477 | error (_("Unknown qSearch:memory reply: %s"), rs->buf); |
08388c79 DE |
7478 | |
7479 | return found; | |
7480 | } | |
7481 | ||
96baa820 JM |
7482 | static void |
7483 | remote_rcmd (char *command, | |
d9fcf2fb | 7484 | struct ui_file *outbuf) |
96baa820 | 7485 | { |
d01949b6 | 7486 | struct remote_state *rs = get_remote_state (); |
2e9f7625 | 7487 | char *p = rs->buf; |
96baa820 JM |
7488 | |
7489 | if (!remote_desc) | |
8a3fe4f8 | 7490 | error (_("remote rcmd is only available after target open")); |
96baa820 | 7491 | |
23860348 | 7492 | /* Send a NULL command across as an empty command. */ |
7be570e7 JM |
7493 | if (command == NULL) |
7494 | command = ""; | |
7495 | ||
23860348 | 7496 | /* The query prefix. */ |
2e9f7625 DJ |
7497 | strcpy (rs->buf, "qRcmd,"); |
7498 | p = strchr (rs->buf, '\0'); | |
96baa820 | 7499 | |
2e9f7625 | 7500 | if ((strlen (rs->buf) + strlen (command) * 2 + 8/*misc*/) > get_remote_packet_size ()) |
8a3fe4f8 | 7501 | error (_("\"monitor\" command ``%s'' is too long."), command); |
96baa820 | 7502 | |
23860348 | 7503 | /* Encode the actual command. */ |
cfd77fa1 | 7504 | bin2hex ((gdb_byte *) command, p, 0); |
96baa820 | 7505 | |
6d820c5c | 7506 | if (putpkt (rs->buf) < 0) |
8a3fe4f8 | 7507 | error (_("Communication problem with target.")); |
96baa820 JM |
7508 | |
7509 | /* get/display the response */ | |
7510 | while (1) | |
7511 | { | |
2e9f7625 DJ |
7512 | char *buf; |
7513 | ||
23860348 | 7514 | /* XXX - see also tracepoint.c:remote_get_noisy_reply(). */ |
2e9f7625 | 7515 | rs->buf[0] = '\0'; |
6d820c5c | 7516 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 7517 | buf = rs->buf; |
96baa820 | 7518 | if (buf[0] == '\0') |
8a3fe4f8 | 7519 | error (_("Target does not support this command.")); |
96baa820 JM |
7520 | if (buf[0] == 'O' && buf[1] != 'K') |
7521 | { | |
23860348 | 7522 | remote_console_output (buf + 1); /* 'O' message from stub. */ |
96baa820 JM |
7523 | continue; |
7524 | } | |
7525 | if (strcmp (buf, "OK") == 0) | |
7526 | break; | |
7be570e7 JM |
7527 | if (strlen (buf) == 3 && buf[0] == 'E' |
7528 | && isdigit (buf[1]) && isdigit (buf[2])) | |
7529 | { | |
8a3fe4f8 | 7530 | error (_("Protocol error with Rcmd")); |
7be570e7 | 7531 | } |
96baa820 JM |
7532 | for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2) |
7533 | { | |
7534 | char c = (fromhex (p[0]) << 4) + fromhex (p[1]); | |
7535 | fputc_unfiltered (c, outbuf); | |
7536 | } | |
7537 | break; | |
7538 | } | |
7539 | } | |
7540 | ||
fd79ecee DJ |
7541 | static VEC(mem_region_s) * |
7542 | remote_memory_map (struct target_ops *ops) | |
7543 | { | |
7544 | VEC(mem_region_s) *result = NULL; | |
7545 | char *text = target_read_stralloc (¤t_target, | |
7546 | TARGET_OBJECT_MEMORY_MAP, NULL); | |
7547 | ||
7548 | if (text) | |
7549 | { | |
7550 | struct cleanup *back_to = make_cleanup (xfree, text); | |
7551 | result = parse_memory_map (text); | |
7552 | do_cleanups (back_to); | |
7553 | } | |
7554 | ||
7555 | return result; | |
7556 | } | |
7557 | ||
c906108c | 7558 | static void |
fba45db2 | 7559 | packet_command (char *args, int from_tty) |
c906108c | 7560 | { |
d01949b6 | 7561 | struct remote_state *rs = get_remote_state (); |
c906108c | 7562 | |
c5aa993b | 7563 | if (!remote_desc) |
8a3fe4f8 | 7564 | error (_("command can only be used with remote target")); |
c906108c | 7565 | |
c5aa993b | 7566 | if (!args) |
8a3fe4f8 | 7567 | error (_("remote-packet command requires packet text as argument")); |
c906108c SS |
7568 | |
7569 | puts_filtered ("sending: "); | |
7570 | print_packet (args); | |
7571 | puts_filtered ("\n"); | |
7572 | putpkt (args); | |
7573 | ||
6d820c5c | 7574 | getpkt (&rs->buf, &rs->buf_size, 0); |
c906108c | 7575 | puts_filtered ("received: "); |
6d820c5c | 7576 | print_packet (rs->buf); |
c906108c SS |
7577 | puts_filtered ("\n"); |
7578 | } | |
7579 | ||
7580 | #if 0 | |
23860348 | 7581 | /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */ |
c906108c | 7582 | |
a14ed312 | 7583 | static void display_thread_info (struct gdb_ext_thread_info *info); |
c906108c | 7584 | |
a14ed312 | 7585 | static void threadset_test_cmd (char *cmd, int tty); |
c906108c | 7586 | |
a14ed312 | 7587 | static void threadalive_test (char *cmd, int tty); |
c906108c | 7588 | |
a14ed312 | 7589 | static void threadlist_test_cmd (char *cmd, int tty); |
c906108c | 7590 | |
23860348 | 7591 | int get_and_display_threadinfo (threadref *ref); |
c906108c | 7592 | |
a14ed312 | 7593 | static void threadinfo_test_cmd (char *cmd, int tty); |
c906108c | 7594 | |
23860348 | 7595 | static int thread_display_step (threadref *ref, void *context); |
c906108c | 7596 | |
a14ed312 | 7597 | static void threadlist_update_test_cmd (char *cmd, int tty); |
c906108c | 7598 | |
a14ed312 | 7599 | static void init_remote_threadtests (void); |
c906108c | 7600 | |
23860348 | 7601 | #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */ |
c906108c SS |
7602 | |
7603 | static void | |
fba45db2 | 7604 | threadset_test_cmd (char *cmd, int tty) |
c906108c SS |
7605 | { |
7606 | int sample_thread = SAMPLE_THREAD; | |
7607 | ||
a3f17187 | 7608 | printf_filtered (_("Remote threadset test\n")); |
79d7f229 | 7609 | set_general_thread (sample_thread); |
c906108c SS |
7610 | } |
7611 | ||
7612 | ||
7613 | static void | |
fba45db2 | 7614 | threadalive_test (char *cmd, int tty) |
c906108c SS |
7615 | { |
7616 | int sample_thread = SAMPLE_THREAD; | |
79d7f229 PA |
7617 | int pid = ptid_get_pid (inferior_ptid); |
7618 | ptid_t ptid = ptid_build (pid, 0, sample_thread); | |
c906108c | 7619 | |
79d7f229 | 7620 | if (remote_thread_alive (ptid)) |
c906108c SS |
7621 | printf_filtered ("PASS: Thread alive test\n"); |
7622 | else | |
7623 | printf_filtered ("FAIL: Thread alive test\n"); | |
7624 | } | |
7625 | ||
23860348 | 7626 | void output_threadid (char *title, threadref *ref); |
c906108c SS |
7627 | |
7628 | void | |
fba45db2 | 7629 | output_threadid (char *title, threadref *ref) |
c906108c SS |
7630 | { |
7631 | char hexid[20]; | |
7632 | ||
23860348 | 7633 | pack_threadid (&hexid[0], ref); /* Convert threead id into hex. */ |
c906108c SS |
7634 | hexid[16] = 0; |
7635 | printf_filtered ("%s %s\n", title, (&hexid[0])); | |
7636 | } | |
7637 | ||
7638 | static void | |
fba45db2 | 7639 | threadlist_test_cmd (char *cmd, int tty) |
c906108c SS |
7640 | { |
7641 | int startflag = 1; | |
7642 | threadref nextthread; | |
7643 | int done, result_count; | |
7644 | threadref threadlist[3]; | |
7645 | ||
7646 | printf_filtered ("Remote Threadlist test\n"); | |
7647 | if (!remote_get_threadlist (startflag, &nextthread, 3, &done, | |
7648 | &result_count, &threadlist[0])) | |
7649 | printf_filtered ("FAIL: threadlist test\n"); | |
7650 | else | |
7651 | { | |
7652 | threadref *scan = threadlist; | |
7653 | threadref *limit = scan + result_count; | |
7654 | ||
7655 | while (scan < limit) | |
7656 | output_threadid (" thread ", scan++); | |
7657 | } | |
7658 | } | |
7659 | ||
7660 | void | |
fba45db2 | 7661 | display_thread_info (struct gdb_ext_thread_info *info) |
c906108c SS |
7662 | { |
7663 | output_threadid ("Threadid: ", &info->threadid); | |
7664 | printf_filtered ("Name: %s\n ", info->shortname); | |
7665 | printf_filtered ("State: %s\n", info->display); | |
7666 | printf_filtered ("other: %s\n\n", info->more_display); | |
7667 | } | |
7668 | ||
7669 | int | |
fba45db2 | 7670 | get_and_display_threadinfo (threadref *ref) |
c906108c SS |
7671 | { |
7672 | int result; | |
7673 | int set; | |
7674 | struct gdb_ext_thread_info threadinfo; | |
7675 | ||
7676 | set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME | |
7677 | | TAG_MOREDISPLAY | TAG_DISPLAY; | |
7678 | if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo))) | |
7679 | display_thread_info (&threadinfo); | |
7680 | return result; | |
7681 | } | |
7682 | ||
7683 | static void | |
fba45db2 | 7684 | threadinfo_test_cmd (char *cmd, int tty) |
c906108c SS |
7685 | { |
7686 | int athread = SAMPLE_THREAD; | |
7687 | threadref thread; | |
7688 | int set; | |
7689 | ||
7690 | int_to_threadref (&thread, athread); | |
7691 | printf_filtered ("Remote Threadinfo test\n"); | |
7692 | if (!get_and_display_threadinfo (&thread)) | |
7693 | printf_filtered ("FAIL cannot get thread info\n"); | |
7694 | } | |
7695 | ||
7696 | static int | |
fba45db2 | 7697 | thread_display_step (threadref *ref, void *context) |
c906108c SS |
7698 | { |
7699 | /* output_threadid(" threadstep ",ref); *//* simple test */ | |
7700 | return get_and_display_threadinfo (ref); | |
7701 | } | |
7702 | ||
7703 | static void | |
fba45db2 | 7704 | threadlist_update_test_cmd (char *cmd, int tty) |
c906108c SS |
7705 | { |
7706 | printf_filtered ("Remote Threadlist update test\n"); | |
7707 | remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS); | |
7708 | } | |
7709 | ||
7710 | static void | |
7711 | init_remote_threadtests (void) | |
7712 | { | |
1bedd215 AC |
7713 | add_com ("tlist", class_obscure, threadlist_test_cmd, _("\ |
7714 | Fetch and print the remote list of thread identifiers, one pkt only")); | |
c906108c | 7715 | add_com ("tinfo", class_obscure, threadinfo_test_cmd, |
1bedd215 | 7716 | _("Fetch and display info about one thread")); |
c906108c | 7717 | add_com ("tset", class_obscure, threadset_test_cmd, |
1bedd215 | 7718 | _("Test setting to a different thread")); |
c906108c | 7719 | add_com ("tupd", class_obscure, threadlist_update_test_cmd, |
1bedd215 | 7720 | _("Iterate through updating all remote thread info")); |
c906108c | 7721 | add_com ("talive", class_obscure, threadalive_test, |
1bedd215 | 7722 | _(" Remote thread alive test ")); |
c906108c SS |
7723 | } |
7724 | ||
7725 | #endif /* 0 */ | |
7726 | ||
f3fb8c85 MS |
7727 | /* Convert a thread ID to a string. Returns the string in a static |
7728 | buffer. */ | |
7729 | ||
7730 | static char * | |
39f77062 | 7731 | remote_pid_to_str (ptid_t ptid) |
f3fb8c85 | 7732 | { |
79d7f229 | 7733 | static char buf[64]; |
82f73884 | 7734 | struct remote_state *rs = get_remote_state (); |
f3fb8c85 | 7735 | |
79d7f229 PA |
7736 | if (ptid_equal (magic_null_ptid, ptid)) |
7737 | { | |
7738 | xsnprintf (buf, sizeof buf, "Thread <main>"); | |
7739 | return buf; | |
7740 | } | |
82f73884 PA |
7741 | else if (remote_multi_process_p (rs) |
7742 | && ptid_get_tid (ptid) != 0 && ptid_get_pid (ptid) != 0) | |
7743 | { | |
7744 | xsnprintf (buf, sizeof buf, "Thread %d.%ld", | |
7745 | ptid_get_pid (ptid), ptid_get_tid (ptid)); | |
7746 | return buf; | |
7747 | } | |
79d7f229 PA |
7748 | else if (ptid_get_tid (ptid) != 0) |
7749 | { | |
7750 | xsnprintf (buf, sizeof buf, "Thread %ld", | |
7751 | ptid_get_tid (ptid)); | |
7752 | return buf; | |
7753 | } | |
7754 | ||
7755 | return normal_pid_to_str (ptid); | |
f3fb8c85 MS |
7756 | } |
7757 | ||
38691318 KB |
7758 | /* Get the address of the thread local variable in OBJFILE which is |
7759 | stored at OFFSET within the thread local storage for thread PTID. */ | |
7760 | ||
7761 | static CORE_ADDR | |
7762 | remote_get_thread_local_address (ptid_t ptid, CORE_ADDR lm, CORE_ADDR offset) | |
7763 | { | |
444abaca | 7764 | if (remote_protocol_packets[PACKET_qGetTLSAddr].support != PACKET_DISABLE) |
38691318 KB |
7765 | { |
7766 | struct remote_state *rs = get_remote_state (); | |
6d820c5c | 7767 | char *p = rs->buf; |
82f73884 | 7768 | char *endp = rs->buf + get_remote_packet_size (); |
571dd617 | 7769 | enum packet_result result; |
38691318 KB |
7770 | |
7771 | strcpy (p, "qGetTLSAddr:"); | |
7772 | p += strlen (p); | |
82f73884 | 7773 | p = write_ptid (p, endp, ptid); |
38691318 KB |
7774 | *p++ = ','; |
7775 | p += hexnumstr (p, offset); | |
7776 | *p++ = ','; | |
7777 | p += hexnumstr (p, lm); | |
7778 | *p++ = '\0'; | |
7779 | ||
6d820c5c DJ |
7780 | putpkt (rs->buf); |
7781 | getpkt (&rs->buf, &rs->buf_size, 0); | |
7782 | result = packet_ok (rs->buf, &remote_protocol_packets[PACKET_qGetTLSAddr]); | |
571dd617 | 7783 | if (result == PACKET_OK) |
38691318 KB |
7784 | { |
7785 | ULONGEST result; | |
7786 | ||
6d820c5c | 7787 | unpack_varlen_hex (rs->buf, &result); |
38691318 KB |
7788 | return result; |
7789 | } | |
571dd617 | 7790 | else if (result == PACKET_UNKNOWN) |
109c3e39 AC |
7791 | throw_error (TLS_GENERIC_ERROR, |
7792 | _("Remote target doesn't support qGetTLSAddr packet")); | |
38691318 | 7793 | else |
109c3e39 AC |
7794 | throw_error (TLS_GENERIC_ERROR, |
7795 | _("Remote target failed to process qGetTLSAddr request")); | |
38691318 KB |
7796 | } |
7797 | else | |
109c3e39 AC |
7798 | throw_error (TLS_GENERIC_ERROR, |
7799 | _("TLS not supported or disabled on this target")); | |
38691318 KB |
7800 | /* Not reached. */ |
7801 | return 0; | |
7802 | } | |
7803 | ||
29709017 DJ |
7804 | /* Support for inferring a target description based on the current |
7805 | architecture and the size of a 'g' packet. While the 'g' packet | |
7806 | can have any size (since optional registers can be left off the | |
7807 | end), some sizes are easily recognizable given knowledge of the | |
7808 | approximate architecture. */ | |
7809 | ||
7810 | struct remote_g_packet_guess | |
7811 | { | |
7812 | int bytes; | |
7813 | const struct target_desc *tdesc; | |
7814 | }; | |
7815 | typedef struct remote_g_packet_guess remote_g_packet_guess_s; | |
7816 | DEF_VEC_O(remote_g_packet_guess_s); | |
7817 | ||
7818 | struct remote_g_packet_data | |
7819 | { | |
7820 | VEC(remote_g_packet_guess_s) *guesses; | |
7821 | }; | |
7822 | ||
7823 | static struct gdbarch_data *remote_g_packet_data_handle; | |
7824 | ||
7825 | static void * | |
7826 | remote_g_packet_data_init (struct obstack *obstack) | |
7827 | { | |
7828 | return OBSTACK_ZALLOC (obstack, struct remote_g_packet_data); | |
7829 | } | |
7830 | ||
7831 | void | |
7832 | register_remote_g_packet_guess (struct gdbarch *gdbarch, int bytes, | |
7833 | const struct target_desc *tdesc) | |
7834 | { | |
7835 | struct remote_g_packet_data *data | |
7836 | = gdbarch_data (gdbarch, remote_g_packet_data_handle); | |
7837 | struct remote_g_packet_guess new_guess, *guess; | |
7838 | int ix; | |
7839 | ||
7840 | gdb_assert (tdesc != NULL); | |
7841 | ||
7842 | for (ix = 0; | |
7843 | VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess); | |
7844 | ix++) | |
7845 | if (guess->bytes == bytes) | |
7846 | internal_error (__FILE__, __LINE__, | |
7847 | "Duplicate g packet description added for size %d", | |
7848 | bytes); | |
7849 | ||
7850 | new_guess.bytes = bytes; | |
7851 | new_guess.tdesc = tdesc; | |
7852 | VEC_safe_push (remote_g_packet_guess_s, data->guesses, &new_guess); | |
7853 | } | |
7854 | ||
7855 | static const struct target_desc * | |
7856 | remote_read_description (struct target_ops *target) | |
7857 | { | |
7858 | struct remote_g_packet_data *data | |
1cf3db46 | 7859 | = gdbarch_data (target_gdbarch, remote_g_packet_data_handle); |
29709017 DJ |
7860 | |
7861 | if (!VEC_empty (remote_g_packet_guess_s, data->guesses)) | |
7862 | { | |
7863 | struct remote_g_packet_guess *guess; | |
7864 | int ix; | |
7865 | int bytes = send_g_packet (); | |
7866 | ||
7867 | for (ix = 0; | |
7868 | VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess); | |
7869 | ix++) | |
7870 | if (guess->bytes == bytes) | |
7871 | return guess->tdesc; | |
7872 | ||
7873 | /* We discard the g packet. A minor optimization would be to | |
7874 | hold on to it, and fill the register cache once we have selected | |
7875 | an architecture, but it's too tricky to do safely. */ | |
7876 | } | |
7877 | ||
7878 | return NULL; | |
7879 | } | |
7880 | ||
a6b151f1 DJ |
7881 | /* Remote file transfer support. This is host-initiated I/O, not |
7882 | target-initiated; for target-initiated, see remote-fileio.c. */ | |
7883 | ||
7884 | /* If *LEFT is at least the length of STRING, copy STRING to | |
7885 | *BUFFER, update *BUFFER to point to the new end of the buffer, and | |
7886 | decrease *LEFT. Otherwise raise an error. */ | |
7887 | ||
7888 | static void | |
7889 | remote_buffer_add_string (char **buffer, int *left, char *string) | |
7890 | { | |
7891 | int len = strlen (string); | |
7892 | ||
7893 | if (len > *left) | |
7894 | error (_("Packet too long for target.")); | |
7895 | ||
7896 | memcpy (*buffer, string, len); | |
7897 | *buffer += len; | |
7898 | *left -= len; | |
7899 | ||
7900 | /* NUL-terminate the buffer as a convenience, if there is | |
7901 | room. */ | |
7902 | if (*left) | |
7903 | **buffer = '\0'; | |
7904 | } | |
7905 | ||
7906 | /* If *LEFT is large enough, hex encode LEN bytes from BYTES into | |
7907 | *BUFFER, update *BUFFER to point to the new end of the buffer, and | |
7908 | decrease *LEFT. Otherwise raise an error. */ | |
7909 | ||
7910 | static void | |
7911 | remote_buffer_add_bytes (char **buffer, int *left, const gdb_byte *bytes, | |
7912 | int len) | |
7913 | { | |
7914 | if (2 * len > *left) | |
7915 | error (_("Packet too long for target.")); | |
7916 | ||
7917 | bin2hex (bytes, *buffer, len); | |
7918 | *buffer += 2 * len; | |
7919 | *left -= 2 * len; | |
7920 | ||
7921 | /* NUL-terminate the buffer as a convenience, if there is | |
7922 | room. */ | |
7923 | if (*left) | |
7924 | **buffer = '\0'; | |
7925 | } | |
7926 | ||
7927 | /* If *LEFT is large enough, convert VALUE to hex and add it to | |
7928 | *BUFFER, update *BUFFER to point to the new end of the buffer, and | |
7929 | decrease *LEFT. Otherwise raise an error. */ | |
7930 | ||
7931 | static void | |
7932 | remote_buffer_add_int (char **buffer, int *left, ULONGEST value) | |
7933 | { | |
7934 | int len = hexnumlen (value); | |
7935 | ||
7936 | if (len > *left) | |
7937 | error (_("Packet too long for target.")); | |
7938 | ||
7939 | hexnumstr (*buffer, value); | |
7940 | *buffer += len; | |
7941 | *left -= len; | |
7942 | ||
7943 | /* NUL-terminate the buffer as a convenience, if there is | |
7944 | room. */ | |
7945 | if (*left) | |
7946 | **buffer = '\0'; | |
7947 | } | |
7948 | ||
7949 | /* Parse an I/O result packet from BUFFER. Set RETCODE to the return | |
7950 | value, *REMOTE_ERRNO to the remote error number or zero if none | |
7951 | was included, and *ATTACHMENT to point to the start of the annex | |
7952 | if any. The length of the packet isn't needed here; there may | |
7953 | be NUL bytes in BUFFER, but they will be after *ATTACHMENT. | |
7954 | ||
7955 | Return 0 if the packet could be parsed, -1 if it could not. If | |
7956 | -1 is returned, the other variables may not be initialized. */ | |
7957 | ||
7958 | static int | |
7959 | remote_hostio_parse_result (char *buffer, int *retcode, | |
7960 | int *remote_errno, char **attachment) | |
7961 | { | |
7962 | char *p, *p2; | |
7963 | ||
7964 | *remote_errno = 0; | |
7965 | *attachment = NULL; | |
7966 | ||
7967 | if (buffer[0] != 'F') | |
7968 | return -1; | |
7969 | ||
7970 | errno = 0; | |
7971 | *retcode = strtol (&buffer[1], &p, 16); | |
7972 | if (errno != 0 || p == &buffer[1]) | |
7973 | return -1; | |
7974 | ||
7975 | /* Check for ",errno". */ | |
7976 | if (*p == ',') | |
7977 | { | |
7978 | errno = 0; | |
7979 | *remote_errno = strtol (p + 1, &p2, 16); | |
7980 | if (errno != 0 || p + 1 == p2) | |
7981 | return -1; | |
7982 | p = p2; | |
7983 | } | |
7984 | ||
7985 | /* Check for ";attachment". If there is no attachment, the | |
7986 | packet should end here. */ | |
7987 | if (*p == ';') | |
7988 | { | |
7989 | *attachment = p + 1; | |
7990 | return 0; | |
7991 | } | |
7992 | else if (*p == '\0') | |
7993 | return 0; | |
7994 | else | |
7995 | return -1; | |
7996 | } | |
7997 | ||
7998 | /* Send a prepared I/O packet to the target and read its response. | |
7999 | The prepared packet is in the global RS->BUF before this function | |
8000 | is called, and the answer is there when we return. | |
8001 | ||
8002 | COMMAND_BYTES is the length of the request to send, which may include | |
8003 | binary data. WHICH_PACKET is the packet configuration to check | |
8004 | before attempting a packet. If an error occurs, *REMOTE_ERRNO | |
8005 | is set to the error number and -1 is returned. Otherwise the value | |
8006 | returned by the function is returned. | |
8007 | ||
8008 | ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an | |
8009 | attachment is expected; an error will be reported if there's a | |
8010 | mismatch. If one is found, *ATTACHMENT will be set to point into | |
8011 | the packet buffer and *ATTACHMENT_LEN will be set to the | |
8012 | attachment's length. */ | |
8013 | ||
8014 | static int | |
8015 | remote_hostio_send_command (int command_bytes, int which_packet, | |
8016 | int *remote_errno, char **attachment, | |
8017 | int *attachment_len) | |
8018 | { | |
8019 | struct remote_state *rs = get_remote_state (); | |
8020 | int ret, bytes_read; | |
8021 | char *attachment_tmp; | |
8022 | ||
f1838a98 UW |
8023 | if (!remote_desc |
8024 | || remote_protocol_packets[which_packet].support == PACKET_DISABLE) | |
a6b151f1 DJ |
8025 | { |
8026 | *remote_errno = FILEIO_ENOSYS; | |
8027 | return -1; | |
8028 | } | |
8029 | ||
8030 | putpkt_binary (rs->buf, command_bytes); | |
8031 | bytes_read = getpkt_sane (&rs->buf, &rs->buf_size, 0); | |
8032 | ||
8033 | /* If it timed out, something is wrong. Don't try to parse the | |
8034 | buffer. */ | |
8035 | if (bytes_read < 0) | |
8036 | { | |
8037 | *remote_errno = FILEIO_EINVAL; | |
8038 | return -1; | |
8039 | } | |
8040 | ||
8041 | switch (packet_ok (rs->buf, &remote_protocol_packets[which_packet])) | |
8042 | { | |
8043 | case PACKET_ERROR: | |
8044 | *remote_errno = FILEIO_EINVAL; | |
8045 | return -1; | |
8046 | case PACKET_UNKNOWN: | |
8047 | *remote_errno = FILEIO_ENOSYS; | |
8048 | return -1; | |
8049 | case PACKET_OK: | |
8050 | break; | |
8051 | } | |
8052 | ||
8053 | if (remote_hostio_parse_result (rs->buf, &ret, remote_errno, | |
8054 | &attachment_tmp)) | |
8055 | { | |
8056 | *remote_errno = FILEIO_EINVAL; | |
8057 | return -1; | |
8058 | } | |
8059 | ||
8060 | /* Make sure we saw an attachment if and only if we expected one. */ | |
8061 | if ((attachment_tmp == NULL && attachment != NULL) | |
8062 | || (attachment_tmp != NULL && attachment == NULL)) | |
8063 | { | |
8064 | *remote_errno = FILEIO_EINVAL; | |
8065 | return -1; | |
8066 | } | |
8067 | ||
8068 | /* If an attachment was found, it must point into the packet buffer; | |
8069 | work out how many bytes there were. */ | |
8070 | if (attachment_tmp != NULL) | |
8071 | { | |
8072 | *attachment = attachment_tmp; | |
8073 | *attachment_len = bytes_read - (*attachment - rs->buf); | |
8074 | } | |
8075 | ||
8076 | return ret; | |
8077 | } | |
8078 | ||
8079 | /* Open FILENAME on the remote target, using FLAGS and MODE. Return a | |
8080 | remote file descriptor, or -1 if an error occurs (and set | |
8081 | *REMOTE_ERRNO). */ | |
8082 | ||
8083 | static int | |
8084 | remote_hostio_open (const char *filename, int flags, int mode, | |
8085 | int *remote_errno) | |
8086 | { | |
8087 | struct remote_state *rs = get_remote_state (); | |
8088 | char *p = rs->buf; | |
8089 | int left = get_remote_packet_size () - 1; | |
8090 | ||
8091 | remote_buffer_add_string (&p, &left, "vFile:open:"); | |
8092 | ||
8093 | remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename, | |
8094 | strlen (filename)); | |
8095 | remote_buffer_add_string (&p, &left, ","); | |
8096 | ||
8097 | remote_buffer_add_int (&p, &left, flags); | |
8098 | remote_buffer_add_string (&p, &left, ","); | |
8099 | ||
8100 | remote_buffer_add_int (&p, &left, mode); | |
8101 | ||
8102 | return remote_hostio_send_command (p - rs->buf, PACKET_vFile_open, | |
8103 | remote_errno, NULL, NULL); | |
8104 | } | |
8105 | ||
8106 | /* Write up to LEN bytes from WRITE_BUF to FD on the remote target. | |
8107 | Return the number of bytes written, or -1 if an error occurs (and | |
8108 | set *REMOTE_ERRNO). */ | |
8109 | ||
8110 | static int | |
8111 | remote_hostio_pwrite (int fd, const gdb_byte *write_buf, int len, | |
8112 | ULONGEST offset, int *remote_errno) | |
8113 | { | |
8114 | struct remote_state *rs = get_remote_state (); | |
8115 | char *p = rs->buf; | |
8116 | int left = get_remote_packet_size (); | |
8117 | int out_len; | |
8118 | ||
8119 | remote_buffer_add_string (&p, &left, "vFile:pwrite:"); | |
8120 | ||
8121 | remote_buffer_add_int (&p, &left, fd); | |
8122 | remote_buffer_add_string (&p, &left, ","); | |
8123 | ||
8124 | remote_buffer_add_int (&p, &left, offset); | |
8125 | remote_buffer_add_string (&p, &left, ","); | |
8126 | ||
8127 | p += remote_escape_output (write_buf, len, p, &out_len, | |
8128 | get_remote_packet_size () - (p - rs->buf)); | |
8129 | ||
8130 | return remote_hostio_send_command (p - rs->buf, PACKET_vFile_pwrite, | |
8131 | remote_errno, NULL, NULL); | |
8132 | } | |
8133 | ||
8134 | /* Read up to LEN bytes FD on the remote target into READ_BUF | |
8135 | Return the number of bytes read, or -1 if an error occurs (and | |
8136 | set *REMOTE_ERRNO). */ | |
8137 | ||
8138 | static int | |
8139 | remote_hostio_pread (int fd, gdb_byte *read_buf, int len, | |
8140 | ULONGEST offset, int *remote_errno) | |
8141 | { | |
8142 | struct remote_state *rs = get_remote_state (); | |
8143 | char *p = rs->buf; | |
8144 | char *attachment; | |
8145 | int left = get_remote_packet_size (); | |
8146 | int ret, attachment_len; | |
8147 | int read_len; | |
8148 | ||
8149 | remote_buffer_add_string (&p, &left, "vFile:pread:"); | |
8150 | ||
8151 | remote_buffer_add_int (&p, &left, fd); | |
8152 | remote_buffer_add_string (&p, &left, ","); | |
8153 | ||
8154 | remote_buffer_add_int (&p, &left, len); | |
8155 | remote_buffer_add_string (&p, &left, ","); | |
8156 | ||
8157 | remote_buffer_add_int (&p, &left, offset); | |
8158 | ||
8159 | ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_pread, | |
8160 | remote_errno, &attachment, | |
8161 | &attachment_len); | |
8162 | ||
8163 | if (ret < 0) | |
8164 | return ret; | |
8165 | ||
8166 | read_len = remote_unescape_input (attachment, attachment_len, | |
8167 | read_buf, len); | |
8168 | if (read_len != ret) | |
8169 | error (_("Read returned %d, but %d bytes."), ret, (int) read_len); | |
8170 | ||
8171 | return ret; | |
8172 | } | |
8173 | ||
8174 | /* Close FD on the remote target. Return 0, or -1 if an error occurs | |
8175 | (and set *REMOTE_ERRNO). */ | |
8176 | ||
8177 | static int | |
8178 | remote_hostio_close (int fd, int *remote_errno) | |
8179 | { | |
8180 | struct remote_state *rs = get_remote_state (); | |
8181 | char *p = rs->buf; | |
8182 | int left = get_remote_packet_size () - 1; | |
8183 | ||
8184 | remote_buffer_add_string (&p, &left, "vFile:close:"); | |
8185 | ||
8186 | remote_buffer_add_int (&p, &left, fd); | |
8187 | ||
8188 | return remote_hostio_send_command (p - rs->buf, PACKET_vFile_close, | |
8189 | remote_errno, NULL, NULL); | |
8190 | } | |
8191 | ||
8192 | /* Unlink FILENAME on the remote target. Return 0, or -1 if an error | |
8193 | occurs (and set *REMOTE_ERRNO). */ | |
8194 | ||
8195 | static int | |
8196 | remote_hostio_unlink (const char *filename, int *remote_errno) | |
8197 | { | |
8198 | struct remote_state *rs = get_remote_state (); | |
8199 | char *p = rs->buf; | |
8200 | int left = get_remote_packet_size () - 1; | |
8201 | ||
8202 | remote_buffer_add_string (&p, &left, "vFile:unlink:"); | |
8203 | ||
8204 | remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename, | |
8205 | strlen (filename)); | |
8206 | ||
8207 | return remote_hostio_send_command (p - rs->buf, PACKET_vFile_unlink, | |
8208 | remote_errno, NULL, NULL); | |
8209 | } | |
8210 | ||
8211 | static int | |
8212 | remote_fileio_errno_to_host (int errnum) | |
8213 | { | |
8214 | switch (errnum) | |
8215 | { | |
8216 | case FILEIO_EPERM: | |
8217 | return EPERM; | |
8218 | case FILEIO_ENOENT: | |
8219 | return ENOENT; | |
8220 | case FILEIO_EINTR: | |
8221 | return EINTR; | |
8222 | case FILEIO_EIO: | |
8223 | return EIO; | |
8224 | case FILEIO_EBADF: | |
8225 | return EBADF; | |
8226 | case FILEIO_EACCES: | |
8227 | return EACCES; | |
8228 | case FILEIO_EFAULT: | |
8229 | return EFAULT; | |
8230 | case FILEIO_EBUSY: | |
8231 | return EBUSY; | |
8232 | case FILEIO_EEXIST: | |
8233 | return EEXIST; | |
8234 | case FILEIO_ENODEV: | |
8235 | return ENODEV; | |
8236 | case FILEIO_ENOTDIR: | |
8237 | return ENOTDIR; | |
8238 | case FILEIO_EISDIR: | |
8239 | return EISDIR; | |
8240 | case FILEIO_EINVAL: | |
8241 | return EINVAL; | |
8242 | case FILEIO_ENFILE: | |
8243 | return ENFILE; | |
8244 | case FILEIO_EMFILE: | |
8245 | return EMFILE; | |
8246 | case FILEIO_EFBIG: | |
8247 | return EFBIG; | |
8248 | case FILEIO_ENOSPC: | |
8249 | return ENOSPC; | |
8250 | case FILEIO_ESPIPE: | |
8251 | return ESPIPE; | |
8252 | case FILEIO_EROFS: | |
8253 | return EROFS; | |
8254 | case FILEIO_ENOSYS: | |
8255 | return ENOSYS; | |
8256 | case FILEIO_ENAMETOOLONG: | |
8257 | return ENAMETOOLONG; | |
8258 | } | |
8259 | return -1; | |
8260 | } | |
8261 | ||
8262 | static char * | |
8263 | remote_hostio_error (int errnum) | |
8264 | { | |
8265 | int host_error = remote_fileio_errno_to_host (errnum); | |
8266 | ||
8267 | if (host_error == -1) | |
8268 | error (_("Unknown remote I/O error %d"), errnum); | |
8269 | else | |
8270 | error (_("Remote I/O error: %s"), safe_strerror (host_error)); | |
8271 | } | |
8272 | ||
a6b151f1 DJ |
8273 | static void |
8274 | remote_hostio_close_cleanup (void *opaque) | |
8275 | { | |
8276 | int fd = *(int *) opaque; | |
8277 | int remote_errno; | |
8278 | ||
8279 | remote_hostio_close (fd, &remote_errno); | |
8280 | } | |
8281 | ||
f1838a98 UW |
8282 | |
8283 | static void * | |
8284 | remote_bfd_iovec_open (struct bfd *abfd, void *open_closure) | |
8285 | { | |
8286 | const char *filename = bfd_get_filename (abfd); | |
8287 | int fd, remote_errno; | |
8288 | int *stream; | |
8289 | ||
8290 | gdb_assert (remote_filename_p (filename)); | |
8291 | ||
8292 | fd = remote_hostio_open (filename + 7, FILEIO_O_RDONLY, 0, &remote_errno); | |
8293 | if (fd == -1) | |
8294 | { | |
8295 | errno = remote_fileio_errno_to_host (remote_errno); | |
8296 | bfd_set_error (bfd_error_system_call); | |
8297 | return NULL; | |
8298 | } | |
8299 | ||
8300 | stream = xmalloc (sizeof (int)); | |
8301 | *stream = fd; | |
8302 | return stream; | |
8303 | } | |
8304 | ||
8305 | static int | |
8306 | remote_bfd_iovec_close (struct bfd *abfd, void *stream) | |
8307 | { | |
8308 | int fd = *(int *)stream; | |
8309 | int remote_errno; | |
8310 | ||
8311 | xfree (stream); | |
8312 | ||
8313 | /* Ignore errors on close; these may happen if the remote | |
8314 | connection was already torn down. */ | |
8315 | remote_hostio_close (fd, &remote_errno); | |
8316 | ||
8317 | return 1; | |
8318 | } | |
8319 | ||
8320 | static file_ptr | |
8321 | remote_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf, | |
8322 | file_ptr nbytes, file_ptr offset) | |
8323 | { | |
8324 | int fd = *(int *)stream; | |
8325 | int remote_errno; | |
8326 | file_ptr pos, bytes; | |
8327 | ||
8328 | pos = 0; | |
8329 | while (nbytes > pos) | |
8330 | { | |
8331 | bytes = remote_hostio_pread (fd, (char *)buf + pos, nbytes - pos, | |
8332 | offset + pos, &remote_errno); | |
8333 | if (bytes == 0) | |
8334 | /* Success, but no bytes, means end-of-file. */ | |
8335 | break; | |
8336 | if (bytes == -1) | |
8337 | { | |
8338 | errno = remote_fileio_errno_to_host (remote_errno); | |
8339 | bfd_set_error (bfd_error_system_call); | |
8340 | return -1; | |
8341 | } | |
8342 | ||
8343 | pos += bytes; | |
8344 | } | |
8345 | ||
8346 | return pos; | |
8347 | } | |
8348 | ||
8349 | static int | |
8350 | remote_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb) | |
8351 | { | |
8352 | /* FIXME: We should probably implement remote_hostio_stat. */ | |
8353 | sb->st_size = INT_MAX; | |
8354 | return 0; | |
8355 | } | |
8356 | ||
8357 | int | |
8358 | remote_filename_p (const char *filename) | |
8359 | { | |
8360 | return strncmp (filename, "remote:", 7) == 0; | |
8361 | } | |
8362 | ||
8363 | bfd * | |
8364 | remote_bfd_open (const char *remote_file, const char *target) | |
8365 | { | |
8366 | return bfd_openr_iovec (remote_file, target, | |
8367 | remote_bfd_iovec_open, NULL, | |
8368 | remote_bfd_iovec_pread, | |
8369 | remote_bfd_iovec_close, | |
8370 | remote_bfd_iovec_stat); | |
8371 | } | |
8372 | ||
a6b151f1 DJ |
8373 | void |
8374 | remote_file_put (const char *local_file, const char *remote_file, int from_tty) | |
8375 | { | |
8376 | struct cleanup *back_to, *close_cleanup; | |
8377 | int retcode, fd, remote_errno, bytes, io_size; | |
8378 | FILE *file; | |
8379 | gdb_byte *buffer; | |
8380 | int bytes_in_buffer; | |
8381 | int saw_eof; | |
8382 | ULONGEST offset; | |
8383 | ||
8384 | if (!remote_desc) | |
8385 | error (_("command can only be used with remote target")); | |
8386 | ||
8387 | file = fopen (local_file, "rb"); | |
8388 | if (file == NULL) | |
8389 | perror_with_name (local_file); | |
7c8a8b04 | 8390 | back_to = make_cleanup_fclose (file); |
a6b151f1 DJ |
8391 | |
8392 | fd = remote_hostio_open (remote_file, (FILEIO_O_WRONLY | FILEIO_O_CREAT | |
8393 | | FILEIO_O_TRUNC), | |
8394 | 0700, &remote_errno); | |
8395 | if (fd == -1) | |
8396 | remote_hostio_error (remote_errno); | |
8397 | ||
8398 | /* Send up to this many bytes at once. They won't all fit in the | |
8399 | remote packet limit, so we'll transfer slightly fewer. */ | |
8400 | io_size = get_remote_packet_size (); | |
8401 | buffer = xmalloc (io_size); | |
8402 | make_cleanup (xfree, buffer); | |
8403 | ||
8404 | close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd); | |
8405 | ||
8406 | bytes_in_buffer = 0; | |
8407 | saw_eof = 0; | |
8408 | offset = 0; | |
8409 | while (bytes_in_buffer || !saw_eof) | |
8410 | { | |
8411 | if (!saw_eof) | |
8412 | { | |
8413 | bytes = fread (buffer + bytes_in_buffer, 1, io_size - bytes_in_buffer, | |
8414 | file); | |
8415 | if (bytes == 0) | |
8416 | { | |
8417 | if (ferror (file)) | |
8418 | error (_("Error reading %s."), local_file); | |
8419 | else | |
8420 | { | |
8421 | /* EOF. Unless there is something still in the | |
8422 | buffer from the last iteration, we are done. */ | |
8423 | saw_eof = 1; | |
8424 | if (bytes_in_buffer == 0) | |
8425 | break; | |
8426 | } | |
8427 | } | |
8428 | } | |
8429 | else | |
8430 | bytes = 0; | |
8431 | ||
8432 | bytes += bytes_in_buffer; | |
8433 | bytes_in_buffer = 0; | |
8434 | ||
8435 | retcode = remote_hostio_pwrite (fd, buffer, bytes, offset, &remote_errno); | |
8436 | ||
8437 | if (retcode < 0) | |
8438 | remote_hostio_error (remote_errno); | |
8439 | else if (retcode == 0) | |
8440 | error (_("Remote write of %d bytes returned 0!"), bytes); | |
8441 | else if (retcode < bytes) | |
8442 | { | |
8443 | /* Short write. Save the rest of the read data for the next | |
8444 | write. */ | |
8445 | bytes_in_buffer = bytes - retcode; | |
8446 | memmove (buffer, buffer + retcode, bytes_in_buffer); | |
8447 | } | |
8448 | ||
8449 | offset += retcode; | |
8450 | } | |
8451 | ||
8452 | discard_cleanups (close_cleanup); | |
8453 | if (remote_hostio_close (fd, &remote_errno)) | |
8454 | remote_hostio_error (remote_errno); | |
8455 | ||
8456 | if (from_tty) | |
8457 | printf_filtered (_("Successfully sent file \"%s\".\n"), local_file); | |
8458 | do_cleanups (back_to); | |
8459 | } | |
8460 | ||
8461 | void | |
8462 | remote_file_get (const char *remote_file, const char *local_file, int from_tty) | |
8463 | { | |
8464 | struct cleanup *back_to, *close_cleanup; | |
8465 | int retcode, fd, remote_errno, bytes, io_size; | |
8466 | FILE *file; | |
8467 | gdb_byte *buffer; | |
8468 | ULONGEST offset; | |
8469 | ||
8470 | if (!remote_desc) | |
8471 | error (_("command can only be used with remote target")); | |
8472 | ||
8473 | fd = remote_hostio_open (remote_file, FILEIO_O_RDONLY, 0, &remote_errno); | |
8474 | if (fd == -1) | |
8475 | remote_hostio_error (remote_errno); | |
8476 | ||
8477 | file = fopen (local_file, "wb"); | |
8478 | if (file == NULL) | |
8479 | perror_with_name (local_file); | |
7c8a8b04 | 8480 | back_to = make_cleanup_fclose (file); |
a6b151f1 DJ |
8481 | |
8482 | /* Send up to this many bytes at once. They won't all fit in the | |
8483 | remote packet limit, so we'll transfer slightly fewer. */ | |
8484 | io_size = get_remote_packet_size (); | |
8485 | buffer = xmalloc (io_size); | |
8486 | make_cleanup (xfree, buffer); | |
8487 | ||
8488 | close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd); | |
8489 | ||
8490 | offset = 0; | |
8491 | while (1) | |
8492 | { | |
8493 | bytes = remote_hostio_pread (fd, buffer, io_size, offset, &remote_errno); | |
8494 | if (bytes == 0) | |
8495 | /* Success, but no bytes, means end-of-file. */ | |
8496 | break; | |
8497 | if (bytes == -1) | |
8498 | remote_hostio_error (remote_errno); | |
8499 | ||
8500 | offset += bytes; | |
8501 | ||
8502 | bytes = fwrite (buffer, 1, bytes, file); | |
8503 | if (bytes == 0) | |
8504 | perror_with_name (local_file); | |
8505 | } | |
8506 | ||
8507 | discard_cleanups (close_cleanup); | |
8508 | if (remote_hostio_close (fd, &remote_errno)) | |
8509 | remote_hostio_error (remote_errno); | |
8510 | ||
8511 | if (from_tty) | |
8512 | printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file); | |
8513 | do_cleanups (back_to); | |
8514 | } | |
8515 | ||
8516 | void | |
8517 | remote_file_delete (const char *remote_file, int from_tty) | |
8518 | { | |
8519 | int retcode, remote_errno; | |
8520 | ||
8521 | if (!remote_desc) | |
8522 | error (_("command can only be used with remote target")); | |
8523 | ||
8524 | retcode = remote_hostio_unlink (remote_file, &remote_errno); | |
8525 | if (retcode == -1) | |
8526 | remote_hostio_error (remote_errno); | |
8527 | ||
8528 | if (from_tty) | |
8529 | printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file); | |
8530 | } | |
8531 | ||
8532 | static void | |
8533 | remote_put_command (char *args, int from_tty) | |
8534 | { | |
8535 | struct cleanup *back_to; | |
8536 | char **argv; | |
8537 | ||
d1a41061 PP |
8538 | if (args == NULL) |
8539 | error_no_arg (_("file to put")); | |
8540 | ||
8541 | argv = gdb_buildargv (args); | |
a6b151f1 DJ |
8542 | back_to = make_cleanup_freeargv (argv); |
8543 | if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL) | |
8544 | error (_("Invalid parameters to remote put")); | |
8545 | ||
8546 | remote_file_put (argv[0], argv[1], from_tty); | |
8547 | ||
8548 | do_cleanups (back_to); | |
8549 | } | |
8550 | ||
8551 | static void | |
8552 | remote_get_command (char *args, int from_tty) | |
8553 | { | |
8554 | struct cleanup *back_to; | |
8555 | char **argv; | |
8556 | ||
d1a41061 PP |
8557 | if (args == NULL) |
8558 | error_no_arg (_("file to get")); | |
8559 | ||
8560 | argv = gdb_buildargv (args); | |
a6b151f1 DJ |
8561 | back_to = make_cleanup_freeargv (argv); |
8562 | if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL) | |
8563 | error (_("Invalid parameters to remote get")); | |
8564 | ||
8565 | remote_file_get (argv[0], argv[1], from_tty); | |
8566 | ||
8567 | do_cleanups (back_to); | |
8568 | } | |
8569 | ||
8570 | static void | |
8571 | remote_delete_command (char *args, int from_tty) | |
8572 | { | |
8573 | struct cleanup *back_to; | |
8574 | char **argv; | |
8575 | ||
d1a41061 PP |
8576 | if (args == NULL) |
8577 | error_no_arg (_("file to delete")); | |
8578 | ||
8579 | argv = gdb_buildargv (args); | |
a6b151f1 DJ |
8580 | back_to = make_cleanup_freeargv (argv); |
8581 | if (argv[0] == NULL || argv[1] != NULL) | |
8582 | error (_("Invalid parameters to remote delete")); | |
8583 | ||
8584 | remote_file_delete (argv[0], from_tty); | |
8585 | ||
8586 | do_cleanups (back_to); | |
8587 | } | |
8588 | ||
8589 | static void | |
8590 | remote_command (char *args, int from_tty) | |
8591 | { | |
8592 | help_list (remote_cmdlist, "remote ", -1, gdb_stdout); | |
8593 | } | |
8594 | ||
b2175913 MS |
8595 | static int remote_target_can_reverse = 1; |
8596 | ||
8597 | static int | |
8598 | remote_can_execute_reverse (void) | |
8599 | { | |
8600 | return remote_target_can_reverse; | |
8601 | } | |
8602 | ||
74531fed PA |
8603 | static int |
8604 | remote_supports_non_stop (void) | |
8605 | { | |
8606 | return 1; | |
8607 | } | |
8608 | ||
8a305172 PA |
8609 | static int |
8610 | remote_supports_multi_process (void) | |
8611 | { | |
8612 | struct remote_state *rs = get_remote_state (); | |
8613 | return remote_multi_process_p (rs); | |
8614 | } | |
8615 | ||
c906108c | 8616 | static void |
fba45db2 | 8617 | init_remote_ops (void) |
c906108c | 8618 | { |
c5aa993b | 8619 | remote_ops.to_shortname = "remote"; |
c906108c | 8620 | remote_ops.to_longname = "Remote serial target in gdb-specific protocol"; |
c5aa993b | 8621 | remote_ops.to_doc = |
c906108c | 8622 | "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ |
0d06e24b JM |
8623 | Specify the serial device it is connected to\n\ |
8624 | (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."; | |
c5aa993b JM |
8625 | remote_ops.to_open = remote_open; |
8626 | remote_ops.to_close = remote_close; | |
c906108c | 8627 | remote_ops.to_detach = remote_detach; |
6ad8ae5c | 8628 | remote_ops.to_disconnect = remote_disconnect; |
c5aa993b | 8629 | remote_ops.to_resume = remote_resume; |
c906108c SS |
8630 | remote_ops.to_wait = remote_wait; |
8631 | remote_ops.to_fetch_registers = remote_fetch_registers; | |
8632 | remote_ops.to_store_registers = remote_store_registers; | |
8633 | remote_ops.to_prepare_to_store = remote_prepare_to_store; | |
c8e73a31 | 8634 | remote_ops.deprecated_xfer_memory = remote_xfer_memory; |
c5aa993b | 8635 | remote_ops.to_files_info = remote_files_info; |
c906108c SS |
8636 | remote_ops.to_insert_breakpoint = remote_insert_breakpoint; |
8637 | remote_ops.to_remove_breakpoint = remote_remove_breakpoint; | |
3c3bea1c GS |
8638 | remote_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint; |
8639 | remote_ops.to_stopped_data_address = remote_stopped_data_address; | |
8640 | remote_ops.to_can_use_hw_breakpoint = remote_check_watch_resources; | |
8641 | remote_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint; | |
8642 | remote_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint; | |
8643 | remote_ops.to_insert_watchpoint = remote_insert_watchpoint; | |
8644 | remote_ops.to_remove_watchpoint = remote_remove_watchpoint; | |
c5aa993b JM |
8645 | remote_ops.to_kill = remote_kill; |
8646 | remote_ops.to_load = generic_load; | |
c906108c SS |
8647 | remote_ops.to_mourn_inferior = remote_mourn; |
8648 | remote_ops.to_thread_alive = remote_thread_alive; | |
0f71a2f6 | 8649 | remote_ops.to_find_new_threads = remote_threads_info; |
0caabb7e | 8650 | remote_ops.to_pid_to_str = remote_pid_to_str; |
cf759d3b | 8651 | remote_ops.to_extra_thread_info = remote_threads_extra_info; |
c906108c | 8652 | remote_ops.to_stop = remote_stop; |
4b8a223f | 8653 | remote_ops.to_xfer_partial = remote_xfer_partial; |
96baa820 | 8654 | remote_ops.to_rcmd = remote_rcmd; |
49d03eab | 8655 | remote_ops.to_log_command = serial_log_command; |
38691318 | 8656 | remote_ops.to_get_thread_local_address = remote_get_thread_local_address; |
c906108c | 8657 | remote_ops.to_stratum = process_stratum; |
c5aa993b JM |
8658 | remote_ops.to_has_all_memory = 1; |
8659 | remote_ops.to_has_memory = 1; | |
8660 | remote_ops.to_has_stack = 1; | |
8661 | remote_ops.to_has_registers = 1; | |
8662 | remote_ops.to_has_execution = 1; | |
8663 | remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */ | |
b2175913 | 8664 | remote_ops.to_can_execute_reverse = remote_can_execute_reverse; |
c5aa993b | 8665 | remote_ops.to_magic = OPS_MAGIC; |
fd79ecee | 8666 | remote_ops.to_memory_map = remote_memory_map; |
a76d924d DJ |
8667 | remote_ops.to_flash_erase = remote_flash_erase; |
8668 | remote_ops.to_flash_done = remote_flash_done; | |
29709017 | 8669 | remote_ops.to_read_description = remote_read_description; |
08388c79 | 8670 | remote_ops.to_search_memory = remote_search_memory; |
75c99385 PA |
8671 | remote_ops.to_can_async_p = remote_can_async_p; |
8672 | remote_ops.to_is_async_p = remote_is_async_p; | |
8673 | remote_ops.to_async = remote_async; | |
8674 | remote_ops.to_async_mask = remote_async_mask; | |
8675 | remote_ops.to_terminal_inferior = remote_terminal_inferior; | |
8676 | remote_ops.to_terminal_ours = remote_terminal_ours; | |
74531fed | 8677 | remote_ops.to_supports_non_stop = remote_supports_non_stop; |
8a305172 | 8678 | remote_ops.to_supports_multi_process = remote_supports_multi_process; |
c906108c SS |
8679 | } |
8680 | ||
8681 | /* Set up the extended remote vector by making a copy of the standard | |
8682 | remote vector and adding to it. */ | |
8683 | ||
8684 | static void | |
fba45db2 | 8685 | init_extended_remote_ops (void) |
c906108c SS |
8686 | { |
8687 | extended_remote_ops = remote_ops; | |
8688 | ||
0f71a2f6 | 8689 | extended_remote_ops.to_shortname = "extended-remote"; |
c5aa993b | 8690 | extended_remote_ops.to_longname = |
c906108c | 8691 | "Extended remote serial target in gdb-specific protocol"; |
c5aa993b | 8692 | extended_remote_ops.to_doc = |
c906108c | 8693 | "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ |
39237dd1 PA |
8694 | Specify the serial device it is connected to (e.g. /dev/ttya)."; |
8695 | extended_remote_ops.to_open = extended_remote_open; | |
c906108c SS |
8696 | extended_remote_ops.to_create_inferior = extended_remote_create_inferior; |
8697 | extended_remote_ops.to_mourn_inferior = extended_remote_mourn; | |
2d717e4f DJ |
8698 | extended_remote_ops.to_detach = extended_remote_detach; |
8699 | extended_remote_ops.to_attach = extended_remote_attach; | |
82f73884 | 8700 | extended_remote_ops.to_kill = extended_remote_kill; |
0f71a2f6 JM |
8701 | } |
8702 | ||
6426a772 JM |
8703 | static int |
8704 | remote_can_async_p (void) | |
8705 | { | |
c6ebd6cf | 8706 | if (!target_async_permitted) |
75c99385 PA |
8707 | /* We only enable async when the user specifically asks for it. */ |
8708 | return 0; | |
8709 | ||
23860348 | 8710 | /* We're async whenever the serial device is. */ |
b84876c2 | 8711 | return remote_async_mask_value && serial_can_async_p (remote_desc); |
6426a772 JM |
8712 | } |
8713 | ||
8714 | static int | |
8715 | remote_is_async_p (void) | |
8716 | { | |
c6ebd6cf | 8717 | if (!target_async_permitted) |
75c99385 PA |
8718 | /* We only enable async when the user specifically asks for it. */ |
8719 | return 0; | |
8720 | ||
23860348 | 8721 | /* We're async whenever the serial device is. */ |
b84876c2 | 8722 | return remote_async_mask_value && serial_is_async_p (remote_desc); |
6426a772 JM |
8723 | } |
8724 | ||
2acceee2 JM |
8725 | /* Pass the SERIAL event on and up to the client. One day this code |
8726 | will be able to delay notifying the client of an event until the | |
23860348 | 8727 | point where an entire packet has been received. */ |
2acceee2 | 8728 | |
2bc416ba | 8729 | static void (*async_client_callback) (enum inferior_event_type event_type, |
23860348 | 8730 | void *context); |
2acceee2 JM |
8731 | static void *async_client_context; |
8732 | static serial_event_ftype remote_async_serial_handler; | |
8733 | ||
6426a772 | 8734 | static void |
819cc324 | 8735 | remote_async_serial_handler (struct serial *scb, void *context) |
6426a772 | 8736 | { |
2acceee2 JM |
8737 | /* Don't propogate error information up to the client. Instead let |
8738 | the client find out about the error by querying the target. */ | |
8739 | async_client_callback (INF_REG_EVENT, async_client_context); | |
8740 | } | |
8741 | ||
74531fed PA |
8742 | static void |
8743 | remote_async_inferior_event_handler (gdb_client_data data) | |
8744 | { | |
8745 | inferior_event_handler (INF_REG_EVENT, NULL); | |
8746 | } | |
8747 | ||
8748 | static void | |
8749 | remote_async_get_pending_events_handler (gdb_client_data data) | |
8750 | { | |
8751 | remote_get_pending_stop_replies (); | |
8752 | } | |
8753 | ||
2acceee2 | 8754 | static void |
2bc416ba | 8755 | remote_async (void (*callback) (enum inferior_event_type event_type, |
23860348 | 8756 | void *context), void *context) |
2acceee2 | 8757 | { |
b84876c2 | 8758 | if (remote_async_mask_value == 0) |
8e65ff28 | 8759 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 8760 | _("Calling remote_async when async is masked")); |
ed9a39eb | 8761 | |
2acceee2 JM |
8762 | if (callback != NULL) |
8763 | { | |
2cd58942 | 8764 | serial_async (remote_desc, remote_async_serial_handler, NULL); |
2acceee2 JM |
8765 | async_client_callback = callback; |
8766 | async_client_context = context; | |
8767 | } | |
8768 | else | |
2cd58942 | 8769 | serial_async (remote_desc, NULL, NULL); |
6426a772 JM |
8770 | } |
8771 | ||
b84876c2 PA |
8772 | static int |
8773 | remote_async_mask (int new_mask) | |
8774 | { | |
8775 | int curr_mask = remote_async_mask_value; | |
8776 | remote_async_mask_value = new_mask; | |
8777 | return curr_mask; | |
8778 | } | |
8779 | ||
5a2468f5 | 8780 | static void |
c2d11a7d | 8781 | set_remote_cmd (char *args, int from_tty) |
5a2468f5 | 8782 | { |
427c3a89 | 8783 | help_list (remote_set_cmdlist, "set remote ", -1, gdb_stdout); |
5a2468f5 JM |
8784 | } |
8785 | ||
d471ea57 AC |
8786 | static void |
8787 | show_remote_cmd (char *args, int from_tty) | |
8788 | { | |
37a105a1 | 8789 | /* We can't just use cmd_show_list here, because we want to skip |
427c3a89 | 8790 | the redundant "show remote Z-packet" and the legacy aliases. */ |
37a105a1 DJ |
8791 | struct cleanup *showlist_chain; |
8792 | struct cmd_list_element *list = remote_show_cmdlist; | |
8793 | ||
8794 | showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist"); | |
8795 | for (; list != NULL; list = list->next) | |
8796 | if (strcmp (list->name, "Z-packet") == 0) | |
8797 | continue; | |
427c3a89 DJ |
8798 | else if (list->type == not_set_cmd) |
8799 | /* Alias commands are exactly like the original, except they | |
8800 | don't have the normal type. */ | |
8801 | continue; | |
8802 | else | |
37a105a1 DJ |
8803 | { |
8804 | struct cleanup *option_chain | |
8805 | = make_cleanup_ui_out_tuple_begin_end (uiout, "option"); | |
8806 | ui_out_field_string (uiout, "name", list->name); | |
8807 | ui_out_text (uiout, ": "); | |
427c3a89 DJ |
8808 | if (list->type == show_cmd) |
8809 | do_setshow_command ((char *) NULL, from_tty, list); | |
8810 | else | |
8811 | cmd_func (list, NULL, from_tty); | |
37a105a1 DJ |
8812 | /* Close the tuple. */ |
8813 | do_cleanups (option_chain); | |
8814 | } | |
427c3a89 DJ |
8815 | |
8816 | /* Close the tuple. */ | |
8817 | do_cleanups (showlist_chain); | |
d471ea57 | 8818 | } |
5a2468f5 | 8819 | |
0f71a2f6 | 8820 | |
23860348 | 8821 | /* Function to be called whenever a new objfile (shlib) is detected. */ |
dc8acb97 MS |
8822 | static void |
8823 | remote_new_objfile (struct objfile *objfile) | |
8824 | { | |
23860348 | 8825 | if (remote_desc != 0) /* Have a remote connection. */ |
06d3b283 | 8826 | remote_check_symbols (objfile); |
dc8acb97 MS |
8827 | } |
8828 | ||
c906108c | 8829 | void |
fba45db2 | 8830 | _initialize_remote (void) |
c906108c | 8831 | { |
ea9c271d DJ |
8832 | struct remote_state *rs; |
8833 | ||
0f71a2f6 | 8834 | /* architecture specific data */ |
2bc416ba | 8835 | remote_gdbarch_data_handle = |
23860348 | 8836 | gdbarch_data_register_post_init (init_remote_state); |
29709017 DJ |
8837 | remote_g_packet_data_handle = |
8838 | gdbarch_data_register_pre_init (remote_g_packet_data_init); | |
d01949b6 | 8839 | |
ea9c271d DJ |
8840 | /* Initialize the per-target state. At the moment there is only one |
8841 | of these, not one per target. Only one target is active at a | |
8842 | time. The default buffer size is unimportant; it will be expanded | |
8843 | whenever a larger buffer is needed. */ | |
0b83947e | 8844 | rs = get_remote_state_raw (); |
ea9c271d DJ |
8845 | rs->buf_size = 400; |
8846 | rs->buf = xmalloc (rs->buf_size); | |
8847 | ||
c906108c SS |
8848 | init_remote_ops (); |
8849 | add_target (&remote_ops); | |
8850 | ||
8851 | init_extended_remote_ops (); | |
8852 | add_target (&extended_remote_ops); | |
cce74817 | 8853 | |
dc8acb97 | 8854 | /* Hook into new objfile notification. */ |
06d3b283 | 8855 | observer_attach_new_objfile (remote_new_objfile); |
dc8acb97 | 8856 | |
b803fb0f DJ |
8857 | /* Set up signal handlers. */ |
8858 | sigint_remote_token = | |
8859 | create_async_signal_handler (async_remote_interrupt, NULL); | |
8860 | sigint_remote_twice_token = | |
8861 | create_async_signal_handler (inferior_event_handler_wrapper, NULL); | |
8862 | ||
c906108c SS |
8863 | #if 0 |
8864 | init_remote_threadtests (); | |
8865 | #endif | |
8866 | ||
23860348 | 8867 | /* set/show remote ... */ |
d471ea57 | 8868 | |
1bedd215 | 8869 | add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, _("\ |
5a2468f5 JM |
8870 | Remote protocol specific variables\n\ |
8871 | Configure various remote-protocol specific variables such as\n\ | |
1bedd215 | 8872 | the packets being used"), |
cff3e48b | 8873 | &remote_set_cmdlist, "set remote ", |
23860348 | 8874 | 0 /* allow-unknown */, &setlist); |
1bedd215 | 8875 | add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\ |
5a2468f5 JM |
8876 | Remote protocol specific variables\n\ |
8877 | Configure various remote-protocol specific variables such as\n\ | |
1bedd215 | 8878 | the packets being used"), |
cff3e48b | 8879 | &remote_show_cmdlist, "show remote ", |
23860348 | 8880 | 0 /* allow-unknown */, &showlist); |
5a2468f5 | 8881 | |
1a966eab AC |
8882 | add_cmd ("compare-sections", class_obscure, compare_sections_command, _("\ |
8883 | Compare section data on target to the exec file.\n\ | |
8884 | Argument is a single section name (default: all loaded sections)."), | |
c906108c SS |
8885 | &cmdlist); |
8886 | ||
1a966eab AC |
8887 | add_cmd ("packet", class_maintenance, packet_command, _("\ |
8888 | Send an arbitrary packet to a remote target.\n\ | |
c906108c SS |
8889 | maintenance packet TEXT\n\ |
8890 | If GDB is talking to an inferior via the GDB serial protocol, then\n\ | |
8891 | this command sends the string TEXT to the inferior, and displays the\n\ | |
8892 | response packet. GDB supplies the initial `$' character, and the\n\ | |
1a966eab | 8893 | terminating `#' character and checksum."), |
c906108c SS |
8894 | &maintenancelist); |
8895 | ||
7915a72c AC |
8896 | add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, _("\ |
8897 | Set whether to send break if interrupted."), _("\ | |
8898 | Show whether to send break if interrupted."), _("\ | |
8899 | If set, a break, instead of a cntrl-c, is sent to the remote target."), | |
2c5b56ce | 8900 | NULL, NULL, /* FIXME: i18n: Whether to send break if interrupted is %s. */ |
e707bbc2 | 8901 | &setlist, &showlist); |
c906108c | 8902 | |
23860348 | 8903 | /* Install commands for configuring memory read/write packets. */ |
11cf8741 | 8904 | |
1a966eab AC |
8905 | add_cmd ("remotewritesize", no_class, set_memory_write_packet_size, _("\ |
8906 | Set the maximum number of bytes per memory write packet (deprecated)."), | |
11cf8741 | 8907 | &setlist); |
1a966eab AC |
8908 | add_cmd ("remotewritesize", no_class, show_memory_write_packet_size, _("\ |
8909 | Show the maximum number of bytes per memory write packet (deprecated)."), | |
11cf8741 JM |
8910 | &showlist); |
8911 | add_cmd ("memory-write-packet-size", no_class, | |
1a966eab AC |
8912 | set_memory_write_packet_size, _("\ |
8913 | Set the maximum number of bytes per memory-write packet.\n\ | |
8914 | Specify the number of bytes in a packet or 0 (zero) for the\n\ | |
8915 | default packet size. The actual limit is further reduced\n\ | |
8916 | dependent on the target. Specify ``fixed'' to disable the\n\ | |
8917 | further restriction and ``limit'' to enable that restriction."), | |
11cf8741 JM |
8918 | &remote_set_cmdlist); |
8919 | add_cmd ("memory-read-packet-size", no_class, | |
1a966eab AC |
8920 | set_memory_read_packet_size, _("\ |
8921 | Set the maximum number of bytes per memory-read packet.\n\ | |
8922 | Specify the number of bytes in a packet or 0 (zero) for the\n\ | |
8923 | default packet size. The actual limit is further reduced\n\ | |
8924 | dependent on the target. Specify ``fixed'' to disable the\n\ | |
8925 | further restriction and ``limit'' to enable that restriction."), | |
11cf8741 JM |
8926 | &remote_set_cmdlist); |
8927 | add_cmd ("memory-write-packet-size", no_class, | |
8928 | show_memory_write_packet_size, | |
1a966eab | 8929 | _("Show the maximum number of bytes per memory-write packet."), |
11cf8741 JM |
8930 | &remote_show_cmdlist); |
8931 | add_cmd ("memory-read-packet-size", no_class, | |
8932 | show_memory_read_packet_size, | |
1a966eab | 8933 | _("Show the maximum number of bytes per memory-read packet."), |
11cf8741 | 8934 | &remote_show_cmdlist); |
c906108c | 8935 | |
b3f42336 | 8936 | add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class, |
7915a72c AC |
8937 | &remote_hw_watchpoint_limit, _("\ |
8938 | Set the maximum number of target hardware watchpoints."), _("\ | |
8939 | Show the maximum number of target hardware watchpoints."), _("\ | |
8940 | Specify a negative limit for unlimited."), | |
2c5b56ce | 8941 | NULL, NULL, /* FIXME: i18n: The maximum number of target hardware watchpoints is %s. */ |
b3f42336 AC |
8942 | &remote_set_cmdlist, &remote_show_cmdlist); |
8943 | add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class, | |
7915a72c AC |
8944 | &remote_hw_breakpoint_limit, _("\ |
8945 | Set the maximum number of target hardware breakpoints."), _("\ | |
8946 | Show the maximum number of target hardware breakpoints."), _("\ | |
8947 | Specify a negative limit for unlimited."), | |
2c5b56ce | 8948 | NULL, NULL, /* FIXME: i18n: The maximum number of target hardware breakpoints is %s. */ |
b3f42336 | 8949 | &remote_set_cmdlist, &remote_show_cmdlist); |
501eef12 | 8950 | |
4d28ad1e AC |
8951 | add_setshow_integer_cmd ("remoteaddresssize", class_obscure, |
8952 | &remote_address_size, _("\ | |
8953 | Set the maximum size of the address (in bits) in a memory packet."), _("\ | |
8954 | Show the maximum size of the address (in bits) in a memory packet."), NULL, | |
8955 | NULL, | |
8956 | NULL, /* FIXME: i18n: */ | |
8957 | &setlist, &showlist); | |
c906108c | 8958 | |
444abaca | 8959 | add_packet_config_cmd (&remote_protocol_packets[PACKET_X], |
bb572ddd | 8960 | "X", "binary-download", 1); |
0f71a2f6 | 8961 | |
444abaca | 8962 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vCont], |
bb572ddd | 8963 | "vCont", "verbose-resume", 0); |
506fb367 | 8964 | |
89be2091 DJ |
8965 | add_packet_config_cmd (&remote_protocol_packets[PACKET_QPassSignals], |
8966 | "QPassSignals", "pass-signals", 0); | |
8967 | ||
444abaca | 8968 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol], |
bb572ddd | 8969 | "qSymbol", "symbol-lookup", 0); |
dc8acb97 | 8970 | |
444abaca | 8971 | add_packet_config_cmd (&remote_protocol_packets[PACKET_P], |
bb572ddd | 8972 | "P", "set-register", 1); |
d471ea57 | 8973 | |
444abaca | 8974 | add_packet_config_cmd (&remote_protocol_packets[PACKET_p], |
bb572ddd | 8975 | "p", "fetch-register", 1); |
b96ec7ac | 8976 | |
444abaca | 8977 | add_packet_config_cmd (&remote_protocol_packets[PACKET_Z0], |
bb572ddd | 8978 | "Z0", "software-breakpoint", 0); |
d471ea57 | 8979 | |
444abaca | 8980 | add_packet_config_cmd (&remote_protocol_packets[PACKET_Z1], |
bb572ddd | 8981 | "Z1", "hardware-breakpoint", 0); |
d471ea57 | 8982 | |
444abaca | 8983 | add_packet_config_cmd (&remote_protocol_packets[PACKET_Z2], |
bb572ddd | 8984 | "Z2", "write-watchpoint", 0); |
d471ea57 | 8985 | |
444abaca | 8986 | add_packet_config_cmd (&remote_protocol_packets[PACKET_Z3], |
bb572ddd | 8987 | "Z3", "read-watchpoint", 0); |
d471ea57 | 8988 | |
444abaca | 8989 | add_packet_config_cmd (&remote_protocol_packets[PACKET_Z4], |
bb572ddd | 8990 | "Z4", "access-watchpoint", 0); |
d471ea57 | 8991 | |
0876f84a DJ |
8992 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_auxv], |
8993 | "qXfer:auxv:read", "read-aux-vector", 0); | |
802188a7 | 8994 | |
23181151 DJ |
8995 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_features], |
8996 | "qXfer:features:read", "target-features", 0); | |
8997 | ||
cfa9d6d9 DJ |
8998 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries], |
8999 | "qXfer:libraries:read", "library-info", 0); | |
9000 | ||
fd79ecee DJ |
9001 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_memory_map], |
9002 | "qXfer:memory-map:read", "memory-map", 0); | |
9003 | ||
0e7f50da UW |
9004 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_read], |
9005 | "qXfer:spu:read", "read-spu-object", 0); | |
9006 | ||
9007 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_write], | |
9008 | "qXfer:spu:write", "write-spu-object", 0); | |
9009 | ||
444abaca | 9010 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTLSAddr], |
38691318 | 9011 | "qGetTLSAddr", "get-thread-local-storage-address", |
38691318 KB |
9012 | 0); |
9013 | ||
be2a5f71 DJ |
9014 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qSupported], |
9015 | "qSupported", "supported-packets", 0); | |
9016 | ||
08388c79 DE |
9017 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qSearch_memory], |
9018 | "qSearch:memory", "search-memory", 0); | |
9019 | ||
a6b151f1 DJ |
9020 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_open], |
9021 | "vFile:open", "hostio-open", 0); | |
9022 | ||
9023 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pread], | |
9024 | "vFile:pread", "hostio-pread", 0); | |
9025 | ||
9026 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pwrite], | |
9027 | "vFile:pwrite", "hostio-pwrite", 0); | |
9028 | ||
9029 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_close], | |
9030 | "vFile:close", "hostio-close", 0); | |
9031 | ||
9032 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_unlink], | |
9033 | "vFile:unlink", "hostio-unlink", 0); | |
9034 | ||
2d717e4f DJ |
9035 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach], |
9036 | "vAttach", "attach", 0); | |
9037 | ||
9038 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vRun], | |
9039 | "vRun", "run", 0); | |
9040 | ||
a6f3e723 SL |
9041 | add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartNoAckMode], |
9042 | "QStartNoAckMode", "noack", 0); | |
9043 | ||
82f73884 PA |
9044 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vKill], |
9045 | "vKill", "kill", 0); | |
9046 | ||
37a105a1 DJ |
9047 | /* Keep the old ``set remote Z-packet ...'' working. Each individual |
9048 | Z sub-packet has its own set and show commands, but users may | |
9049 | have sets to this variable in their .gdbinit files (or in their | |
9050 | documentation). */ | |
e9e68a56 | 9051 | add_setshow_auto_boolean_cmd ("Z-packet", class_obscure, |
7915a72c AC |
9052 | &remote_Z_packet_detect, _("\ |
9053 | Set use of remote protocol `Z' packets"), _("\ | |
9054 | Show use of remote protocol `Z' packets "), _("\ | |
3b64bf98 | 9055 | When set, GDB will attempt to use the remote breakpoint and watchpoint\n\ |
7915a72c | 9056 | packets."), |
e9e68a56 | 9057 | set_remote_protocol_Z_packet_cmd, |
2c5b56ce | 9058 | show_remote_protocol_Z_packet_cmd, /* FIXME: i18n: Use of remote protocol `Z' packets is %s. */ |
e9e68a56 | 9059 | &remote_set_cmdlist, &remote_show_cmdlist); |
449092f6 | 9060 | |
a6b151f1 DJ |
9061 | add_prefix_cmd ("remote", class_files, remote_command, _("\ |
9062 | Manipulate files on the remote system\n\ | |
9063 | Transfer files to and from the remote target system."), | |
9064 | &remote_cmdlist, "remote ", | |
9065 | 0 /* allow-unknown */, &cmdlist); | |
9066 | ||
9067 | add_cmd ("put", class_files, remote_put_command, | |
9068 | _("Copy a local file to the remote system."), | |
9069 | &remote_cmdlist); | |
9070 | ||
9071 | add_cmd ("get", class_files, remote_get_command, | |
9072 | _("Copy a remote file to the local system."), | |
9073 | &remote_cmdlist); | |
9074 | ||
9075 | add_cmd ("delete", class_files, remote_delete_command, | |
9076 | _("Delete a remote file."), | |
9077 | &remote_cmdlist); | |
9078 | ||
2d717e4f DJ |
9079 | remote_exec_file = xstrdup (""); |
9080 | add_setshow_string_noescape_cmd ("exec-file", class_files, | |
9081 | &remote_exec_file, _("\ | |
9082 | Set the remote pathname for \"run\""), _("\ | |
9083 | Show the remote pathname for \"run\""), NULL, NULL, NULL, | |
9084 | &remote_set_cmdlist, &remote_show_cmdlist); | |
9085 | ||
449092f6 CV |
9086 | /* Eventually initialize fileio. See fileio.c */ |
9087 | initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist); | |
79d7f229 PA |
9088 | |
9089 | /* Take advantage of the fact that the LWP field is not used, to tag | |
9090 | special ptids with it set to != 0. */ | |
82f73884 PA |
9091 | magic_null_ptid = ptid_build (42000, 1, -1); |
9092 | not_sent_ptid = ptid_build (42000, 1, -2); | |
9093 | any_thread_ptid = ptid_build (42000, 1, 0); | |
c906108c | 9094 | } |