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