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