* config/tc-arm.c (do_vmrs): Accept priviledged mode VFP system
[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. */
3381 return;
2d717e4f
DJ
3382 }
3383 else
74531fed 3384 {
74531fed
PA
3385 /* Save the reply for later. */
3386 wait_status = alloca (strlen (rs->buf) + 1);
3387 strcpy (wait_status, rs->buf);
3388 }
3389
3390 /* Let the stub know that we want it to return the thread. */
3391 set_continue_thread (minus_one_ptid);
3392
49c62f2e 3393 add_current_inferior_and_thread ();
74531fed 3394
6e586cc5
YQ
3395 /* init_wait_for_inferior should be called before get_offsets in order
3396 to manage `inserted' flag in bp loc in a correct state.
3397 breakpoint_init_inferior, called from init_wait_for_inferior, set
3398 `inserted' flag to 0, while before breakpoint_re_set, called from
3399 start_remote, set `inserted' flag to 1. In the initialization of
3400 inferior, breakpoint_init_inferior should be called first, and then
3401 breakpoint_re_set can be called. If this order is broken, state of
3402 `inserted' flag is wrong, and cause some problems on breakpoint
3403 manipulation. */
3404 init_wait_for_inferior ();
3405
74531fed
PA
3406 get_offsets (); /* Get text, data & bss offsets. */
3407
d962ef82
DJ
3408 /* If we could not find a description using qXfer, and we know
3409 how to do it some other way, try again. This is not
3410 supported for non-stop; it could be, but it is tricky if
3411 there are no stopped threads when we connect. */
04bd08de 3412 if (remote_read_description_p (target)
d962ef82
DJ
3413 && gdbarch_target_desc (target_gdbarch) == NULL)
3414 {
3415 target_clear_description ();
3416 target_find_description ();
3417 }
3418
74531fed
PA
3419 /* Use the previously fetched status. */
3420 gdb_assert (wait_status != NULL);
3421 strcpy (rs->buf, wait_status);
3422 rs->cached_wait_status = 1;
3423
3424 immediate_quit--;
04bd08de 3425 start_remote (from_tty); /* Initialize gdb process mechanisms. */
2d717e4f
DJ
3426 }
3427 else
3428 {
68c97600
PA
3429 /* Clear WFI global state. Do this before finding about new
3430 threads and inferiors, and setting the current inferior.
3431 Otherwise we would clear the proceed status of the current
3432 inferior when we want its stop_soon state to be preserved
3433 (see notice_new_inferior). */
3434 init_wait_for_inferior ();
3435
74531fed
PA
3436 /* In non-stop, we will either get an "OK", meaning that there
3437 are no stopped threads at this time; or, a regular stop
3438 reply. In the latter case, there may be more than one thread
3439 stopped --- we pull them all out using the vStopped
3440 mechanism. */
3441 if (strcmp (rs->buf, "OK") != 0)
3442 {
3443 struct stop_reply *stop_reply;
3444 struct cleanup *old_chain;
2d717e4f 3445
74531fed
PA
3446 stop_reply = stop_reply_xmalloc ();
3447 old_chain = make_cleanup (do_stop_reply_xfree, stop_reply);
2d717e4f 3448
74531fed
PA
3449 remote_parse_stop_reply (rs->buf, stop_reply);
3450 discard_cleanups (old_chain);
c0a2216e 3451
74531fed
PA
3452 /* get_pending_stop_replies acks this one, and gets the rest
3453 out. */
3454 pending_stop_reply = stop_reply;
3455 remote_get_pending_stop_replies ();
c906108c 3456
74531fed
PA
3457 /* Make sure that threads that were stopped remain
3458 stopped. */
3459 iterate_over_threads (set_stop_requested_callback, NULL);
3460 }
2d717e4f 3461
74531fed
PA
3462 if (target_can_async_p ())
3463 target_async (inferior_event_handler, 0);
c906108c 3464
74531fed
PA
3465 if (thread_count () == 0)
3466 {
04bd08de 3467 if (!extended_p)
74531fed 3468 error (_("The target is not running (try extended-remote?)"));
82f73884 3469
c35b1492
PA
3470 /* We're connected, but not running. Drop out before we
3471 call start_remote. */
3472 return;
3473 }
74531fed
PA
3474
3475 /* Let the stub know that we want it to return the thread. */
c0a2216e 3476
74531fed
PA
3477 /* Force the stub to choose a thread. */
3478 set_general_thread (null_ptid);
c906108c 3479
74531fed
PA
3480 /* Query it. */
3481 inferior_ptid = remote_current_thread (minus_one_ptid);
3482 if (ptid_equal (inferior_ptid, minus_one_ptid))
3483 error (_("remote didn't report the current thread in non-stop mode"));
c906108c 3484
74531fed
PA
3485 get_offsets (); /* Get text, data & bss offsets. */
3486
3487 /* In non-stop mode, any cached wait status will be stored in
3488 the stop reply queue. */
3489 gdb_assert (wait_status == NULL);
f0223081 3490
2455069d
UW
3491 /* Report all signals during attach/startup. */
3492 remote_pass_signals (0, NULL);
74531fed 3493 }
c8d104ad 3494
c8d104ad
PA
3495 /* If we connected to a live target, do some additional setup. */
3496 if (target_has_execution)
3497 {
3498 if (exec_bfd) /* No use without an exec file. */
3499 remote_check_symbols (symfile_objfile);
3500 }
50c71eaf 3501
d5551862
SS
3502 /* Possibly the target has been engaged in a trace run started
3503 previously; find out where things are at. */
26afc0d7 3504 if (remote_get_trace_status (current_trace_status ()) != -1)
d5551862 3505 {
00bf0b85
SS
3506 struct uploaded_tp *uploaded_tps = NULL;
3507 struct uploaded_tsv *uploaded_tsvs = NULL;
3508
00bf0b85
SS
3509 if (current_trace_status ()->running)
3510 printf_filtered (_("Trace is already running on the target.\n"));
3511
3512 /* Get trace state variables first, they may be checked when
3513 parsing uploaded commands. */
3514
3515 remote_upload_trace_state_variables (&uploaded_tsvs);
3516
3517 merge_uploaded_trace_state_variables (&uploaded_tsvs);
3518
3519 remote_upload_tracepoints (&uploaded_tps);
3520
3521 merge_uploaded_tracepoints (&uploaded_tps);
d5551862
SS
3522 }
3523
1e51243a
PA
3524 /* The thread and inferior lists are now synchronized with the
3525 target, our symbols have been relocated, and we're merged the
3526 target's tracepoints with ours. We're done with basic start
3527 up. */
3528 rs->starting_up = 0;
3529
2567c7d9
PA
3530 /* If breakpoints are global, insert them now. */
3531 if (gdbarch_has_global_breakpoints (target_gdbarch)
50c71eaf
PA
3532 && breakpoints_always_inserted_mode ())
3533 insert_breakpoints ();
c906108c
SS
3534}
3535
3536/* Open a connection to a remote debugger.
3537 NAME is the filename used for communication. */
3538
3539static void
fba45db2 3540remote_open (char *name, int from_tty)
c906108c 3541{
75c99385 3542 remote_open_1 (name, from_tty, &remote_ops, 0);
43ff13b4
JM
3543}
3544
c906108c
SS
3545/* Open a connection to a remote debugger using the extended
3546 remote gdb protocol. NAME is the filename used for communication. */
3547
3548static void
fba45db2 3549extended_remote_open (char *name, int from_tty)
c906108c 3550{
75c99385 3551 remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */);
43ff13b4
JM
3552}
3553
c906108c
SS
3554/* Generic code for opening a connection to a remote target. */
3555
d471ea57
AC
3556static void
3557init_all_packet_configs (void)
3558{
3559 int i;
a744cf53 3560
444abaca
DJ
3561 for (i = 0; i < PACKET_MAX; i++)
3562 update_packet_config (&remote_protocol_packets[i]);
d471ea57
AC
3563}
3564
23860348 3565/* Symbol look-up. */
dc8acb97
MS
3566
3567static void
3568remote_check_symbols (struct objfile *objfile)
3569{
d01949b6 3570 struct remote_state *rs = get_remote_state ();
dc8acb97
MS
3571 char *msg, *reply, *tmp;
3572 struct minimal_symbol *sym;
3573 int end;
3574
63154eca
PA
3575 /* The remote side has no concept of inferiors that aren't running
3576 yet, it only knows about running processes. If we're connected
3577 but our current inferior is not running, we should not invite the
3578 remote target to request symbol lookups related to its
3579 (unrelated) current process. */
3580 if (!target_has_execution)
3581 return;
3582
444abaca 3583 if (remote_protocol_packets[PACKET_qSymbol].support == PACKET_DISABLE)
dc8acb97
MS
3584 return;
3585
63154eca
PA
3586 /* Make sure the remote is pointing at the right process. Note
3587 there's no way to select "no process". */
3c9c4b83
PA
3588 set_general_process ();
3589
6d820c5c
DJ
3590 /* Allocate a message buffer. We can't reuse the input buffer in RS,
3591 because we need both at the same time. */
ea9c271d 3592 msg = alloca (get_remote_packet_size ());
6d820c5c 3593
23860348 3594 /* Invite target to request symbol lookups. */
dc8acb97
MS
3595
3596 putpkt ("qSymbol::");
6d820c5c
DJ
3597 getpkt (&rs->buf, &rs->buf_size, 0);
3598 packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSymbol]);
2e9f7625 3599 reply = rs->buf;
dc8acb97
MS
3600
3601 while (strncmp (reply, "qSymbol:", 8) == 0)
3602 {
3603 tmp = &reply[8];
cfd77fa1 3604 end = hex2bin (tmp, (gdb_byte *) msg, strlen (tmp) / 2);
dc8acb97
MS
3605 msg[end] = '\0';
3606 sym = lookup_minimal_symbol (msg, NULL, NULL);
3607 if (sym == NULL)
ea9c271d 3608 xsnprintf (msg, get_remote_packet_size (), "qSymbol::%s", &reply[8]);
dc8acb97 3609 else
2bbe3cc1 3610 {
5af949e3 3611 int addr_size = gdbarch_addr_bit (target_gdbarch) / 8;
2bbe3cc1
DJ
3612 CORE_ADDR sym_addr = SYMBOL_VALUE_ADDRESS (sym);
3613
3614 /* If this is a function address, return the start of code
3615 instead of any data function descriptor. */
1cf3db46 3616 sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch,
2bbe3cc1
DJ
3617 sym_addr,
3618 &current_target);
3619
3620 xsnprintf (msg, get_remote_packet_size (), "qSymbol:%s:%s",
5af949e3 3621 phex_nz (sym_addr, addr_size), &reply[8]);
2bbe3cc1
DJ
3622 }
3623
dc8acb97 3624 putpkt (msg);
6d820c5c 3625 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 3626 reply = rs->buf;
dc8acb97
MS
3627 }
3628}
3629
9db8d71f
DJ
3630static struct serial *
3631remote_serial_open (char *name)
3632{
3633 static int udp_warning = 0;
3634
3635 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
3636 of in ser-tcp.c, because it is the remote protocol assuming that the
3637 serial connection is reliable and not the serial connection promising
3638 to be. */
3639 if (!udp_warning && strncmp (name, "udp:", 4) == 0)
3640 {
3e43a32a
MS
3641 warning (_("The remote protocol may be unreliable over UDP.\n"
3642 "Some events may be lost, rendering further debugging "
3643 "impossible."));
9db8d71f
DJ
3644 udp_warning = 1;
3645 }
3646
3647 return serial_open (name);
3648}
3649
d914c394
SS
3650/* Inform the target of our permission settings. The permission flags
3651 work without this, but if the target knows the settings, it can do
3652 a couple things. First, it can add its own check, to catch cases
3653 that somehow manage to get by the permissions checks in target
3654 methods. Second, if the target is wired to disallow particular
3655 settings (for instance, a system in the field that is not set up to
3656 be able to stop at a breakpoint), it can object to any unavailable
3657 permissions. */
3658
3659void
3660remote_set_permissions (void)
3661{
3662 struct remote_state *rs = get_remote_state ();
3663
bba74b36
YQ
3664 xsnprintf (rs->buf, get_remote_packet_size (), "QAllow:"
3665 "WriteReg:%x;WriteMem:%x;"
3666 "InsertBreak:%x;InsertTrace:%x;"
3667 "InsertFastTrace:%x;Stop:%x",
3668 may_write_registers, may_write_memory,
3669 may_insert_breakpoints, may_insert_tracepoints,
3670 may_insert_fast_tracepoints, may_stop);
d914c394
SS
3671 putpkt (rs->buf);
3672 getpkt (&rs->buf, &rs->buf_size, 0);
3673
3674 /* If the target didn't like the packet, warn the user. Do not try
3675 to undo the user's settings, that would just be maddening. */
3676 if (strcmp (rs->buf, "OK") != 0)
7ea6d463 3677 warning (_("Remote refused setting permissions with: %s"), rs->buf);
d914c394
SS
3678}
3679
be2a5f71
DJ
3680/* This type describes each known response to the qSupported
3681 packet. */
3682struct protocol_feature
3683{
3684 /* The name of this protocol feature. */
3685 const char *name;
3686
3687 /* The default for this protocol feature. */
3688 enum packet_support default_support;
3689
3690 /* The function to call when this feature is reported, or after
3691 qSupported processing if the feature is not supported.
3692 The first argument points to this structure. The second
3693 argument indicates whether the packet requested support be
3694 enabled, disabled, or probed (or the default, if this function
3695 is being called at the end of processing and this feature was
3696 not reported). The third argument may be NULL; if not NULL, it
3697 is a NUL-terminated string taken from the packet following
3698 this feature's name and an equals sign. */
3699 void (*func) (const struct protocol_feature *, enum packet_support,
3700 const char *);
3701
3702 /* The corresponding packet for this feature. Only used if
3703 FUNC is remote_supported_packet. */
3704 int packet;
3705};
3706
be2a5f71
DJ
3707static void
3708remote_supported_packet (const struct protocol_feature *feature,
3709 enum packet_support support,
3710 const char *argument)
3711{
3712 if (argument)
3713 {
3714 warning (_("Remote qSupported response supplied an unexpected value for"
3715 " \"%s\"."), feature->name);
3716 return;
3717 }
3718
3719 if (remote_protocol_packets[feature->packet].support
3720 == PACKET_SUPPORT_UNKNOWN)
3721 remote_protocol_packets[feature->packet].support = support;
3722}
be2a5f71
DJ
3723
3724static void
3725remote_packet_size (const struct protocol_feature *feature,
3726 enum packet_support support, const char *value)
3727{
3728 struct remote_state *rs = get_remote_state ();
3729
3730 int packet_size;
3731 char *value_end;
3732
3733 if (support != PACKET_ENABLE)
3734 return;
3735
3736 if (value == NULL || *value == '\0')
3737 {
3738 warning (_("Remote target reported \"%s\" without a size."),
3739 feature->name);
3740 return;
3741 }
3742
3743 errno = 0;
3744 packet_size = strtol (value, &value_end, 16);
3745 if (errno != 0 || *value_end != '\0' || packet_size < 0)
3746 {
3747 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
3748 feature->name, value);
3749 return;
3750 }
3751
3752 if (packet_size > MAX_REMOTE_PACKET_SIZE)
3753 {
3754 warning (_("limiting remote suggested packet size (%d bytes) to %d"),
3755 packet_size, MAX_REMOTE_PACKET_SIZE);
3756 packet_size = MAX_REMOTE_PACKET_SIZE;
3757 }
3758
3759 /* Record the new maximum packet size. */
3760 rs->explicit_packet_size = packet_size;
3761}
3762
82f73884
PA
3763static void
3764remote_multi_process_feature (const struct protocol_feature *feature,
3765 enum packet_support support, const char *value)
3766{
3767 struct remote_state *rs = get_remote_state ();
a744cf53 3768
82f73884
PA
3769 rs->multi_process_aware = (support == PACKET_ENABLE);
3770}
3771
74531fed
PA
3772static void
3773remote_non_stop_feature (const struct protocol_feature *feature,
3774 enum packet_support support, const char *value)
3775{
3776 struct remote_state *rs = get_remote_state ();
a744cf53 3777
74531fed
PA
3778 rs->non_stop_aware = (support == PACKET_ENABLE);
3779}
3780
782b2b07
SS
3781static void
3782remote_cond_tracepoint_feature (const struct protocol_feature *feature,
3783 enum packet_support support,
3784 const char *value)
3785{
3786 struct remote_state *rs = get_remote_state ();
a744cf53 3787
782b2b07
SS
3788 rs->cond_tracepoints = (support == PACKET_ENABLE);
3789}
3790
3788aec7
LM
3791static void
3792remote_cond_breakpoint_feature (const struct protocol_feature *feature,
3793 enum packet_support support,
3794 const char *value)
3795{
3796 struct remote_state *rs = get_remote_state ();
3797
3798 rs->cond_breakpoints = (support == PACKET_ENABLE);
3799}
3800
7a697b8d
SS
3801static void
3802remote_fast_tracepoint_feature (const struct protocol_feature *feature,
3803 enum packet_support support,
3804 const char *value)
3805{
3806 struct remote_state *rs = get_remote_state ();
a744cf53 3807
7a697b8d
SS
3808 rs->fast_tracepoints = (support == PACKET_ENABLE);
3809}
3810
0fb4aa4b
PA
3811static void
3812remote_static_tracepoint_feature (const struct protocol_feature *feature,
3813 enum packet_support support,
3814 const char *value)
3815{
3816 struct remote_state *rs = get_remote_state ();
3817
3818 rs->static_tracepoints = (support == PACKET_ENABLE);
3819}
3820
1e4d1764
YQ
3821static void
3822remote_install_in_trace_feature (const struct protocol_feature *feature,
3823 enum packet_support support,
3824 const char *value)
3825{
3826 struct remote_state *rs = get_remote_state ();
3827
3828 rs->install_in_trace = (support == PACKET_ENABLE);
3829}
3830
d5551862
SS
3831static void
3832remote_disconnected_tracing_feature (const struct protocol_feature *feature,
3833 enum packet_support support,
3834 const char *value)
3835{
3836 struct remote_state *rs = get_remote_state ();
a744cf53 3837
d5551862
SS
3838 rs->disconnected_tracing = (support == PACKET_ENABLE);
3839}
3840
d248b706
KY
3841static void
3842remote_enable_disable_tracepoint_feature (const struct protocol_feature *feature,
3843 enum packet_support support,
3844 const char *value)
3845{
3846 struct remote_state *rs = get_remote_state ();
3847
3848 rs->enable_disable_tracepoints = (support == PACKET_ENABLE);
3849}
3850
3065dfb6
SS
3851static void
3852remote_string_tracing_feature (const struct protocol_feature *feature,
3853 enum packet_support support,
3854 const char *value)
3855{
3856 struct remote_state *rs = get_remote_state ();
3857
3858 rs->string_tracing = (support == PACKET_ENABLE);
3859}
3860
be2a5f71 3861static struct protocol_feature remote_protocol_features[] = {
0876f84a 3862 { "PacketSize", PACKET_DISABLE, remote_packet_size, -1 },
40e57cf2 3863 { "qXfer:auxv:read", PACKET_DISABLE, remote_supported_packet,
fd79ecee 3864 PACKET_qXfer_auxv },
23181151
DJ
3865 { "qXfer:features:read", PACKET_DISABLE, remote_supported_packet,
3866 PACKET_qXfer_features },
cfa9d6d9
DJ
3867 { "qXfer:libraries:read", PACKET_DISABLE, remote_supported_packet,
3868 PACKET_qXfer_libraries },
2268b414
JK
3869 { "qXfer:libraries-svr4:read", PACKET_DISABLE, remote_supported_packet,
3870 PACKET_qXfer_libraries_svr4 },
fd79ecee 3871 { "qXfer:memory-map:read", PACKET_DISABLE, remote_supported_packet,
89be2091 3872 PACKET_qXfer_memory_map },
4de6483e
UW
3873 { "qXfer:spu:read", PACKET_DISABLE, remote_supported_packet,
3874 PACKET_qXfer_spu_read },
3875 { "qXfer:spu:write", PACKET_DISABLE, remote_supported_packet,
3876 PACKET_qXfer_spu_write },
07e059b5
VP
3877 { "qXfer:osdata:read", PACKET_DISABLE, remote_supported_packet,
3878 PACKET_qXfer_osdata },
dc146f7c
VP
3879 { "qXfer:threads:read", PACKET_DISABLE, remote_supported_packet,
3880 PACKET_qXfer_threads },
b3b9301e
PA
3881 { "qXfer:traceframe-info:read", PACKET_DISABLE, remote_supported_packet,
3882 PACKET_qXfer_traceframe_info },
89be2091
DJ
3883 { "QPassSignals", PACKET_DISABLE, remote_supported_packet,
3884 PACKET_QPassSignals },
9b224c5e
PA
3885 { "QProgramSignals", PACKET_DISABLE, remote_supported_packet,
3886 PACKET_QProgramSignals },
a6f3e723
SL
3887 { "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet,
3888 PACKET_QStartNoAckMode },
82f73884 3889 { "multiprocess", PACKET_DISABLE, remote_multi_process_feature, -1 },
74531fed 3890 { "QNonStop", PACKET_DISABLE, remote_non_stop_feature, -1 },
4aa995e1
PA
3891 { "qXfer:siginfo:read", PACKET_DISABLE, remote_supported_packet,
3892 PACKET_qXfer_siginfo_read },
3893 { "qXfer:siginfo:write", PACKET_DISABLE, remote_supported_packet,
3894 PACKET_qXfer_siginfo_write },
782b2b07
SS
3895 { "ConditionalTracepoints", PACKET_DISABLE, remote_cond_tracepoint_feature,
3896 PACKET_ConditionalTracepoints },
3788aec7
LM
3897 { "ConditionalBreakpoints", PACKET_DISABLE, remote_cond_breakpoint_feature,
3898 PACKET_ConditionalBreakpoints },
7a697b8d
SS
3899 { "FastTracepoints", PACKET_DISABLE, remote_fast_tracepoint_feature,
3900 PACKET_FastTracepoints },
0fb4aa4b
PA
3901 { "StaticTracepoints", PACKET_DISABLE, remote_static_tracepoint_feature,
3902 PACKET_StaticTracepoints },
1e4d1764
YQ
3903 {"InstallInTrace", PACKET_DISABLE, remote_install_in_trace_feature,
3904 PACKET_InstallInTrace},
d5551862
SS
3905 { "DisconnectedTracing", PACKET_DISABLE, remote_disconnected_tracing_feature,
3906 -1 },
40ab02ce
MS
3907 { "ReverseContinue", PACKET_DISABLE, remote_supported_packet,
3908 PACKET_bc },
3909 { "ReverseStep", PACKET_DISABLE, remote_supported_packet,
3910 PACKET_bs },
409873ef
SS
3911 { "TracepointSource", PACKET_DISABLE, remote_supported_packet,
3912 PACKET_TracepointSource },
d914c394
SS
3913 { "QAllow", PACKET_DISABLE, remote_supported_packet,
3914 PACKET_QAllow },
d248b706
KY
3915 { "EnableDisableTracepoints", PACKET_DISABLE,
3916 remote_enable_disable_tracepoint_feature, -1 },
78d85199
YQ
3917 { "qXfer:fdpic:read", PACKET_DISABLE, remote_supported_packet,
3918 PACKET_qXfer_fdpic },
169081d0
TG
3919 { "qXfer:uib:read", PACKET_DISABLE, remote_supported_packet,
3920 PACKET_qXfer_uib },
03583c20
UW
3921 { "QDisableRandomization", PACKET_DISABLE, remote_supported_packet,
3922 PACKET_QDisableRandomization },
d1feda86 3923 { "QAgent", PACKET_DISABLE, remote_supported_packet, PACKET_QAgent},
3065dfb6
SS
3924 { "tracenz", PACKET_DISABLE,
3925 remote_string_tracing_feature, -1 },
be2a5f71
DJ
3926};
3927
c8d5aac9
L
3928static char *remote_support_xml;
3929
3930/* Register string appended to "xmlRegisters=" in qSupported query. */
3931
3932void
6e39997a 3933register_remote_support_xml (const char *xml)
c8d5aac9
L
3934{
3935#if defined(HAVE_LIBEXPAT)
3936 if (remote_support_xml == NULL)
c4f7c687 3937 remote_support_xml = concat ("xmlRegisters=", xml, (char *) NULL);
c8d5aac9
L
3938 else
3939 {
3940 char *copy = xstrdup (remote_support_xml + 13);
3941 char *p = strtok (copy, ",");
3942
3943 do
3944 {
3945 if (strcmp (p, xml) == 0)
3946 {
3947 /* already there */
3948 xfree (copy);
3949 return;
3950 }
3951 }
3952 while ((p = strtok (NULL, ",")) != NULL);
3953 xfree (copy);
3954
94b0dee1
PA
3955 remote_support_xml = reconcat (remote_support_xml,
3956 remote_support_xml, ",", xml,
3957 (char *) NULL);
c8d5aac9
L
3958 }
3959#endif
3960}
3961
3962static char *
3963remote_query_supported_append (char *msg, const char *append)
3964{
3965 if (msg)
94b0dee1 3966 return reconcat (msg, msg, ";", append, (char *) NULL);
c8d5aac9
L
3967 else
3968 return xstrdup (append);
3969}
3970
be2a5f71
DJ
3971static void
3972remote_query_supported (void)
3973{
3974 struct remote_state *rs = get_remote_state ();
3975 char *next;
3976 int i;
3977 unsigned char seen [ARRAY_SIZE (remote_protocol_features)];
3978
3979 /* The packet support flags are handled differently for this packet
3980 than for most others. We treat an error, a disabled packet, and
3981 an empty response identically: any features which must be reported
3982 to be used will be automatically disabled. An empty buffer
3983 accomplishes this, since that is also the representation for a list
3984 containing no features. */
3985
3986 rs->buf[0] = 0;
3987 if (remote_protocol_packets[PACKET_qSupported].support != PACKET_DISABLE)
3988 {
c8d5aac9 3989 char *q = NULL;
94b0dee1 3990 struct cleanup *old_chain = make_cleanup (free_current_contents, &q);
c8d5aac9 3991
901f9912 3992 q = remote_query_supported_append (q, "multiprocess+");
c8d5aac9
L
3993
3994 if (remote_support_xml)
3995 q = remote_query_supported_append (q, remote_support_xml);
3996
dde08ee1
PA
3997 q = remote_query_supported_append (q, "qRelocInsn+");
3998
3999 q = reconcat (q, "qSupported:", q, (char *) NULL);
4000 putpkt (q);
82f73884 4001
94b0dee1
PA
4002 do_cleanups (old_chain);
4003
be2a5f71
DJ
4004 getpkt (&rs->buf, &rs->buf_size, 0);
4005
4006 /* If an error occured, warn, but do not return - just reset the
4007 buffer to empty and go on to disable features. */
4008 if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSupported])
4009 == PACKET_ERROR)
4010 {
4011 warning (_("Remote failure reply: %s"), rs->buf);
4012 rs->buf[0] = 0;
4013 }
4014 }
4015
4016 memset (seen, 0, sizeof (seen));
4017
4018 next = rs->buf;
4019 while (*next)
4020 {
4021 enum packet_support is_supported;
4022 char *p, *end, *name_end, *value;
4023
4024 /* First separate out this item from the rest of the packet. If
4025 there's another item after this, we overwrite the separator
4026 (terminated strings are much easier to work with). */
4027 p = next;
4028 end = strchr (p, ';');
4029 if (end == NULL)
4030 {
4031 end = p + strlen (p);
4032 next = end;
4033 }
4034 else
4035 {
89be2091
DJ
4036 *end = '\0';
4037 next = end + 1;
4038
be2a5f71
DJ
4039 if (end == p)
4040 {
4041 warning (_("empty item in \"qSupported\" response"));
4042 continue;
4043 }
be2a5f71
DJ
4044 }
4045
4046 name_end = strchr (p, '=');
4047 if (name_end)
4048 {
4049 /* This is a name=value entry. */
4050 is_supported = PACKET_ENABLE;
4051 value = name_end + 1;
4052 *name_end = '\0';
4053 }
4054 else
4055 {
4056 value = NULL;
4057 switch (end[-1])
4058 {
4059 case '+':
4060 is_supported = PACKET_ENABLE;
4061 break;
4062
4063 case '-':
4064 is_supported = PACKET_DISABLE;
4065 break;
4066
4067 case '?':
4068 is_supported = PACKET_SUPPORT_UNKNOWN;
4069 break;
4070
4071 default:
3e43a32a
MS
4072 warning (_("unrecognized item \"%s\" "
4073 "in \"qSupported\" response"), p);
be2a5f71
DJ
4074 continue;
4075 }
4076 end[-1] = '\0';
4077 }
4078
4079 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4080 if (strcmp (remote_protocol_features[i].name, p) == 0)
4081 {
4082 const struct protocol_feature *feature;
4083
4084 seen[i] = 1;
4085 feature = &remote_protocol_features[i];
4086 feature->func (feature, is_supported, value);
4087 break;
4088 }
4089 }
4090
4091 /* If we increased the packet size, make sure to increase the global
4092 buffer size also. We delay this until after parsing the entire
4093 qSupported packet, because this is the same buffer we were
4094 parsing. */
4095 if (rs->buf_size < rs->explicit_packet_size)
4096 {
4097 rs->buf_size = rs->explicit_packet_size;
4098 rs->buf = xrealloc (rs->buf, rs->buf_size);
4099 }
4100
4101 /* Handle the defaults for unmentioned features. */
4102 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
4103 if (!seen[i])
4104 {
4105 const struct protocol_feature *feature;
4106
4107 feature = &remote_protocol_features[i];
4108 feature->func (feature, feature->default_support, NULL);
4109 }
4110}
4111
4112
c906108c 4113static void
3e43a32a
MS
4114remote_open_1 (char *name, int from_tty,
4115 struct target_ops *target, int extended_p)
c906108c 4116{
d01949b6 4117 struct remote_state *rs = get_remote_state ();
a6f3e723 4118
c906108c 4119 if (name == 0)
8a3fe4f8 4120 error (_("To open a remote debug connection, you need to specify what\n"
22e04375 4121 "serial device is attached to the remote system\n"
8a3fe4f8 4122 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
c906108c 4123
23860348 4124 /* See FIXME above. */
c6ebd6cf 4125 if (!target_async_permitted)
92d1e331 4126 wait_forever_enabled_p = 1;
6426a772 4127
2d717e4f
DJ
4128 /* If we're connected to a running target, target_preopen will kill it.
4129 But if we're connected to a target system with no running process,
4130 then we will still be connected when it returns. Ask this question
4131 first, before target_preopen has a chance to kill anything. */
c35b1492 4132 if (remote_desc != NULL && !have_inferiors ())
2d717e4f
DJ
4133 {
4134 if (!from_tty
4135 || query (_("Already connected to a remote target. Disconnect? ")))
4136 pop_target ();
4137 else
4138 error (_("Still connected."));
4139 }
4140
c906108c
SS
4141 target_preopen (from_tty);
4142
4143 unpush_target (target);
4144
2d717e4f
DJ
4145 /* This time without a query. If we were connected to an
4146 extended-remote target and target_preopen killed the running
4147 process, we may still be connected. If we are starting "target
4148 remote" now, the extended-remote target will not have been
4149 removed by unpush_target. */
c35b1492 4150 if (remote_desc != NULL && !have_inferiors ())
2d717e4f
DJ
4151 pop_target ();
4152
89be2091
DJ
4153 /* Make sure we send the passed signals list the next time we resume. */
4154 xfree (last_pass_packet);
4155 last_pass_packet = NULL;
4156
9b224c5e
PA
4157 /* Make sure we send the program signals list the next time we
4158 resume. */
4159 xfree (last_program_signals_packet);
4160 last_program_signals_packet = NULL;
4161
ad9a8f3f 4162 remote_fileio_reset ();
1dd41f16
NS
4163 reopen_exec_file ();
4164 reread_symbols ();
4165
9db8d71f 4166 remote_desc = remote_serial_open (name);
c906108c
SS
4167 if (!remote_desc)
4168 perror_with_name (name);
4169
4170 if (baud_rate != -1)
4171 {
2cd58942 4172 if (serial_setbaudrate (remote_desc, baud_rate))
c906108c 4173 {
9b74d5d3
KB
4174 /* The requested speed could not be set. Error out to
4175 top level after closing remote_desc. Take care to
4176 set remote_desc to NULL to avoid closing remote_desc
4177 more than once. */
2cd58942 4178 serial_close (remote_desc);
9b74d5d3 4179 remote_desc = NULL;
c906108c
SS
4180 perror_with_name (name);
4181 }
4182 }
4183
2cd58942 4184 serial_raw (remote_desc);
c906108c
SS
4185
4186 /* If there is something sitting in the buffer we might take it as a
4187 response to a command, which would be bad. */
2cd58942 4188 serial_flush_input (remote_desc);
c906108c
SS
4189
4190 if (from_tty)
4191 {
4192 puts_filtered ("Remote debugging using ");
4193 puts_filtered (name);
4194 puts_filtered ("\n");
4195 }
23860348 4196 push_target (target); /* Switch to using remote target now. */
c906108c 4197
74531fed
PA
4198 /* Register extra event sources in the event loop. */
4199 remote_async_inferior_event_token
4200 = create_async_event_handler (remote_async_inferior_event_handler,
4201 NULL);
4202 remote_async_get_pending_events_token
4203 = create_async_event_handler (remote_async_get_pending_events_handler,
4204 NULL);
4205
be2a5f71
DJ
4206 /* Reset the target state; these things will be queried either by
4207 remote_query_supported or as they are needed. */
d471ea57 4208 init_all_packet_configs ();
74531fed 4209 rs->cached_wait_status = 0;
be2a5f71 4210 rs->explicit_packet_size = 0;
a6f3e723 4211 rs->noack_mode = 0;
82f73884
PA
4212 rs->multi_process_aware = 0;
4213 rs->extended = extended_p;
74531fed 4214 rs->non_stop_aware = 0;
e24a49d8 4215 rs->waiting_for_stop_reply = 0;
3a29589a 4216 rs->ctrlc_pending_p = 0;
802188a7 4217
79d7f229
PA
4218 general_thread = not_sent_ptid;
4219 continue_thread = not_sent_ptid;
e6e4e701 4220 remote_traceframe_number = -1;
c906108c 4221
9d1f7ab2
MS
4222 /* Probe for ability to use "ThreadInfo" query, as required. */
4223 use_threadinfo_query = 1;
4224 use_threadextra_query = 1;
4225
c6ebd6cf 4226 if (target_async_permitted)
92d1e331 4227 {
23860348 4228 /* With this target we start out by owning the terminal. */
92d1e331
DJ
4229 remote_async_terminal_ours_p = 1;
4230
4231 /* FIXME: cagney/1999-09-23: During the initial connection it is
4232 assumed that the target is already ready and able to respond to
0df8b418 4233 requests. Unfortunately remote_start_remote() eventually calls
92d1e331 4234 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
0df8b418 4235 around this. Eventually a mechanism that allows
92d1e331 4236 wait_for_inferior() to expect/get timeouts will be
23860348 4237 implemented. */
92d1e331
DJ
4238 wait_forever_enabled_p = 0;
4239 }
4240
23860348 4241 /* First delete any symbols previously loaded from shared libraries. */
f78f6cf1 4242 no_shared_libraries (NULL, 0);
f78f6cf1 4243
74531fed
PA
4244 /* Start afresh. */
4245 init_thread_list ();
4246
36918e70 4247 /* Start the remote connection. If error() or QUIT, discard this
165b8e33
AC
4248 target (we'd otherwise be in an inconsistent state) and then
4249 propogate the error on up the exception chain. This ensures that
4250 the caller doesn't stumble along blindly assuming that the
4251 function succeeded. The CLI doesn't have this problem but other
4252 UI's, such as MI do.
36918e70
AC
4253
4254 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
4255 this function should return an error indication letting the
ce2826aa 4256 caller restore the previous state. Unfortunately the command
36918e70
AC
4257 ``target remote'' is directly wired to this function making that
4258 impossible. On a positive note, the CLI side of this problem has
4259 been fixed - the function set_cmd_context() makes it possible for
4260 all the ``target ....'' commands to share a common callback
4261 function. See cli-dump.c. */
109c3e39 4262 {
04bd08de 4263 volatile struct gdb_exception ex;
2d717e4f 4264
04bd08de
TT
4265 TRY_CATCH (ex, RETURN_MASK_ALL)
4266 {
4267 remote_start_remote (from_tty, target, extended_p);
4268 }
109c3e39
AC
4269 if (ex.reason < 0)
4270 {
c8d104ad
PA
4271 /* Pop the partially set up target - unless something else did
4272 already before throwing the exception. */
4273 if (remote_desc != NULL)
4274 pop_target ();
c6ebd6cf 4275 if (target_async_permitted)
109c3e39
AC
4276 wait_forever_enabled_p = 1;
4277 throw_exception (ex);
4278 }
4279 }
c906108c 4280
c6ebd6cf 4281 if (target_async_permitted)
92d1e331 4282 wait_forever_enabled_p = 1;
43ff13b4
JM
4283}
4284
c906108c
SS
4285/* This takes a program previously attached to and detaches it. After
4286 this is done, GDB can be used to debug some other program. We
4287 better not have left any breakpoints in the target program or it'll
4288 die when it hits one. */
4289
4290static void
2d717e4f 4291remote_detach_1 (char *args, int from_tty, int extended)
c906108c 4292{
82f73884 4293 int pid = ptid_get_pid (inferior_ptid);
d01949b6 4294 struct remote_state *rs = get_remote_state ();
c906108c
SS
4295
4296 if (args)
8a3fe4f8 4297 error (_("Argument given to \"detach\" when remotely debugging."));
c906108c 4298
2d717e4f
DJ
4299 if (!target_has_execution)
4300 error (_("No process to detach from."));
4301
7cee1e54
PA
4302 if (from_tty)
4303 {
4304 char *exec_file = get_exec_file (0);
4305 if (exec_file == NULL)
4306 exec_file = "";
4307 printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file,
4308 target_pid_to_str (pid_to_ptid (pid)));
4309 gdb_flush (gdb_stdout);
4310 }
4311
c906108c 4312 /* Tell the remote target to detach. */
82f73884 4313 if (remote_multi_process_p (rs))
bba74b36 4314 xsnprintf (rs->buf, get_remote_packet_size (), "D;%x", pid);
82f73884
PA
4315 else
4316 strcpy (rs->buf, "D");
4317
4ddda9b5
PA
4318 putpkt (rs->buf);
4319 getpkt (&rs->buf, &rs->buf_size, 0);
4320
82f73884
PA
4321 if (rs->buf[0] == 'O' && rs->buf[1] == 'K')
4322 ;
4323 else if (rs->buf[0] == '\0')
4324 error (_("Remote doesn't know how to detach"));
4325 else
4ddda9b5 4326 error (_("Can't detach process."));
c906108c 4327
7cee1e54
PA
4328 if (from_tty && !extended)
4329 puts_filtered (_("Ending remote debugging.\n"));
82f73884 4330
74531fed 4331 discard_pending_stop_replies (pid);
82f73884 4332 target_mourn_inferior ();
2d717e4f
DJ
4333}
4334
4335static void
136d6dae 4336remote_detach (struct target_ops *ops, char *args, int from_tty)
2d717e4f
DJ
4337{
4338 remote_detach_1 (args, from_tty, 0);
4339}
4340
4341static void
136d6dae 4342extended_remote_detach (struct target_ops *ops, char *args, int from_tty)
2d717e4f
DJ
4343{
4344 remote_detach_1 (args, from_tty, 1);
c906108c
SS
4345}
4346
6ad8ae5c
DJ
4347/* Same as remote_detach, but don't send the "D" packet; just disconnect. */
4348
43ff13b4 4349static void
597320e7 4350remote_disconnect (struct target_ops *target, char *args, int from_tty)
43ff13b4 4351{
43ff13b4 4352 if (args)
2d717e4f 4353 error (_("Argument given to \"disconnect\" when remotely debugging."));
43ff13b4 4354
2d717e4f
DJ
4355 /* Make sure we unpush even the extended remote targets; mourn
4356 won't do it. So call remote_mourn_1 directly instead of
4357 target_mourn_inferior. */
4358 remote_mourn_1 (target);
4359
43ff13b4
JM
4360 if (from_tty)
4361 puts_filtered ("Ending remote debugging.\n");
4362}
4363
2d717e4f
DJ
4364/* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
4365 be chatty about it. */
4366
4367static void
4368extended_remote_attach_1 (struct target_ops *target, char *args, int from_tty)
4369{
4370 struct remote_state *rs = get_remote_state ();
be86555c 4371 int pid;
96ef3384 4372 char *wait_status = NULL;
2d717e4f 4373
74164c56 4374 pid = parse_pid_to_attach (args);
2d717e4f 4375
74164c56
JK
4376 /* Remote PID can be freely equal to getpid, do not check it here the same
4377 way as in other targets. */
2d717e4f
DJ
4378
4379 if (remote_protocol_packets[PACKET_vAttach].support == PACKET_DISABLE)
4380 error (_("This target does not support attaching to a process"));
4381
7cee1e54
PA
4382 if (from_tty)
4383 {
4384 char *exec_file = get_exec_file (0);
4385
4386 if (exec_file)
4387 printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
4388 target_pid_to_str (pid_to_ptid (pid)));
4389 else
4390 printf_unfiltered (_("Attaching to %s\n"),
4391 target_pid_to_str (pid_to_ptid (pid)));
4392
4393 gdb_flush (gdb_stdout);
4394 }
4395
bba74b36 4396 xsnprintf (rs->buf, get_remote_packet_size (), "vAttach;%x", pid);
2d717e4f
DJ
4397 putpkt (rs->buf);
4398 getpkt (&rs->buf, &rs->buf_size, 0);
4399
3e43a32a
MS
4400 if (packet_ok (rs->buf,
4401 &remote_protocol_packets[PACKET_vAttach]) == PACKET_OK)
2d717e4f 4402 {
74531fed
PA
4403 if (!non_stop)
4404 {
4405 /* Save the reply for later. */
4406 wait_status = alloca (strlen (rs->buf) + 1);
4407 strcpy (wait_status, rs->buf);
4408 }
4409 else if (strcmp (rs->buf, "OK") != 0)
4410 error (_("Attaching to %s failed with: %s"),
4411 target_pid_to_str (pid_to_ptid (pid)),
4412 rs->buf);
2d717e4f
DJ
4413 }
4414 else if (remote_protocol_packets[PACKET_vAttach].support == PACKET_DISABLE)
4415 error (_("This target does not support attaching to a process"));
4416 else
4417 error (_("Attaching to %s failed"),
4418 target_pid_to_str (pid_to_ptid (pid)));
4419
49c62f2e 4420 set_current_inferior (remote_add_inferior (0, pid, 1));
bad34192 4421
2d717e4f 4422 inferior_ptid = pid_to_ptid (pid);
79d7f229 4423
bad34192
PA
4424 if (non_stop)
4425 {
4426 struct thread_info *thread;
79d7f229 4427
bad34192
PA
4428 /* Get list of threads. */
4429 remote_threads_info (target);
82f73884 4430
bad34192
PA
4431 thread = first_thread_of_process (pid);
4432 if (thread)
4433 inferior_ptid = thread->ptid;
4434 else
4435 inferior_ptid = pid_to_ptid (pid);
4436
4437 /* Invalidate our notion of the remote current thread. */
4438 record_currthread (minus_one_ptid);
4439 }
74531fed 4440 else
bad34192
PA
4441 {
4442 /* Now, if we have thread information, update inferior_ptid. */
4443 inferior_ptid = remote_current_thread (inferior_ptid);
4444
4445 /* Add the main thread to the thread list. */
4446 add_thread_silent (inferior_ptid);
4447 }
c0a2216e 4448
96ef3384
UW
4449 /* Next, if the target can specify a description, read it. We do
4450 this before anything involving memory or registers. */
4451 target_find_description ();
4452
74531fed
PA
4453 if (!non_stop)
4454 {
4455 /* Use the previously fetched status. */
4456 gdb_assert (wait_status != NULL);
4457
4458 if (target_can_async_p ())
4459 {
4460 struct stop_reply *stop_reply;
4461 struct cleanup *old_chain;
4462
4463 stop_reply = stop_reply_xmalloc ();
4464 old_chain = make_cleanup (do_stop_reply_xfree, stop_reply);
4465 remote_parse_stop_reply (wait_status, stop_reply);
4466 discard_cleanups (old_chain);
4467 push_stop_reply (stop_reply);
4468
4469 target_async (inferior_event_handler, 0);
4470 }
4471 else
4472 {
4473 gdb_assert (wait_status != NULL);
4474 strcpy (rs->buf, wait_status);
4475 rs->cached_wait_status = 1;
4476 }
4477 }
4478 else
4479 gdb_assert (wait_status == NULL);
2d717e4f
DJ
4480}
4481
4482static void
136d6dae 4483extended_remote_attach (struct target_ops *ops, char *args, int from_tty)
2d717e4f 4484{
136d6dae 4485 extended_remote_attach_1 (ops, args, from_tty);
2d717e4f
DJ
4486}
4487
c906108c
SS
4488/* Convert hex digit A to a number. */
4489
30559e10 4490static int
fba45db2 4491fromhex (int a)
c906108c
SS
4492{
4493 if (a >= '0' && a <= '9')
4494 return a - '0';
4495 else if (a >= 'a' && a <= 'f')
4496 return a - 'a' + 10;
4497 else if (a >= 'A' && a <= 'F')
4498 return a - 'A' + 10;
c5aa993b 4499 else
8a3fe4f8 4500 error (_("Reply contains invalid hex digit %d"), a);
c906108c
SS
4501}
4502
00bf0b85 4503int
cfd77fa1 4504hex2bin (const char *hex, gdb_byte *bin, int count)
30559e10
MS
4505{
4506 int i;
4507
30559e10
MS
4508 for (i = 0; i < count; i++)
4509 {
4510 if (hex[0] == 0 || hex[1] == 0)
4511 {
4512 /* Hex string is short, or of uneven length.
23860348 4513 Return the count that has been converted so far. */
30559e10
MS
4514 return i;
4515 }
4516 *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
4517 hex += 2;
4518 }
4519 return i;
4520}
4521
c906108c
SS
4522/* Convert number NIB to a hex digit. */
4523
4524static int
fba45db2 4525tohex (int nib)
c906108c
SS
4526{
4527 if (nib < 10)
c5aa993b 4528 return '0' + nib;
c906108c 4529 else
c5aa993b 4530 return 'a' + nib - 10;
c906108c 4531}
30559e10 4532
00bf0b85 4533int
cfd77fa1 4534bin2hex (const gdb_byte *bin, char *hex, int count)
30559e10
MS
4535{
4536 int i;
a744cf53 4537
23860348 4538 /* May use a length, or a nul-terminated string as input. */
30559e10 4539 if (count == 0)
cfd77fa1 4540 count = strlen ((char *) bin);
30559e10
MS
4541
4542 for (i = 0; i < count; i++)
4543 {
4544 *hex++ = tohex ((*bin >> 4) & 0xf);
4545 *hex++ = tohex (*bin++ & 0xf);
4546 }
4547 *hex = 0;
4548 return i;
4549}
c906108c 4550\f
506fb367
DJ
4551/* Check for the availability of vCont. This function should also check
4552 the response. */
c906108c
SS
4553
4554static void
6d820c5c 4555remote_vcont_probe (struct remote_state *rs)
c906108c 4556{
2e9f7625 4557 char *buf;
6d820c5c 4558
2e9f7625
DJ
4559 strcpy (rs->buf, "vCont?");
4560 putpkt (rs->buf);
6d820c5c 4561 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 4562 buf = rs->buf;
c906108c 4563
506fb367
DJ
4564 /* Make sure that the features we assume are supported. */
4565 if (strncmp (buf, "vCont", 5) == 0)
4566 {
4567 char *p = &buf[5];
4568 int support_s, support_S, support_c, support_C;
4569
4570 support_s = 0;
4571 support_S = 0;
4572 support_c = 0;
4573 support_C = 0;
74531fed 4574 rs->support_vCont_t = 0;
506fb367
DJ
4575 while (p && *p == ';')
4576 {
4577 p++;
4578 if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0))
4579 support_s = 1;
4580 else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0))
4581 support_S = 1;
4582 else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0))
4583 support_c = 1;
4584 else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0))
4585 support_C = 1;
74531fed
PA
4586 else if (*p == 't' && (*(p + 1) == ';' || *(p + 1) == 0))
4587 rs->support_vCont_t = 1;
506fb367
DJ
4588
4589 p = strchr (p, ';');
4590 }
c906108c 4591
506fb367
DJ
4592 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
4593 BUF will make packet_ok disable the packet. */
4594 if (!support_s || !support_S || !support_c || !support_C)
4595 buf[0] = 0;
4596 }
c906108c 4597
444abaca 4598 packet_ok (buf, &remote_protocol_packets[PACKET_vCont]);
506fb367 4599}
c906108c 4600
0d8f58ca
PA
4601/* Helper function for building "vCont" resumptions. Write a
4602 resumption to P. ENDP points to one-passed-the-end of the buffer
4603 we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
4604 thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
4605 resumed thread should be single-stepped and/or signalled. If PTID
4606 equals minus_one_ptid, then all threads are resumed; if PTID
4607 represents a process, then all threads of the process are resumed;
4608 the thread to be stepped and/or signalled is given in the global
4609 INFERIOR_PTID. */
4610
4611static char *
4612append_resumption (char *p, char *endp,
4613 ptid_t ptid, int step, enum target_signal siggnal)
4614{
4615 struct remote_state *rs = get_remote_state ();
4616
4617 if (step && siggnal != TARGET_SIGNAL_0)
4618 p += xsnprintf (p, endp - p, ";S%02x", siggnal);
4619 else if (step)
4620 p += xsnprintf (p, endp - p, ";s");
4621 else if (siggnal != TARGET_SIGNAL_0)
4622 p += xsnprintf (p, endp - p, ";C%02x", siggnal);
4623 else
4624 p += xsnprintf (p, endp - p, ";c");
4625
4626 if (remote_multi_process_p (rs) && ptid_is_pid (ptid))
4627 {
4628 ptid_t nptid;
4629
4630 /* All (-1) threads of process. */
4631 nptid = ptid_build (ptid_get_pid (ptid), 0, -1);
4632
4633 p += xsnprintf (p, endp - p, ":");
4634 p = write_ptid (p, endp, nptid);
4635 }
4636 else if (!ptid_equal (ptid, minus_one_ptid))
4637 {
4638 p += xsnprintf (p, endp - p, ":");
4639 p = write_ptid (p, endp, ptid);
4640 }
4641
4642 return p;
4643}
4644
506fb367
DJ
4645/* Resume the remote inferior by using a "vCont" packet. The thread
4646 to be resumed is PTID; STEP and SIGGNAL indicate whether the
79d7f229
PA
4647 resumed thread should be single-stepped and/or signalled. If PTID
4648 equals minus_one_ptid, then all threads are resumed; the thread to
4649 be stepped and/or signalled is given in the global INFERIOR_PTID.
4650 This function returns non-zero iff it resumes the inferior.
44eaed12 4651
506fb367
DJ
4652 This function issues a strict subset of all possible vCont commands at the
4653 moment. */
44eaed12 4654
506fb367
DJ
4655static int
4656remote_vcont_resume (ptid_t ptid, int step, enum target_signal siggnal)
4657{
4658 struct remote_state *rs = get_remote_state ();
82f73884
PA
4659 char *p;
4660 char *endp;
44eaed12 4661
444abaca 4662 if (remote_protocol_packets[PACKET_vCont].support == PACKET_SUPPORT_UNKNOWN)
6d820c5c 4663 remote_vcont_probe (rs);
44eaed12 4664
444abaca 4665 if (remote_protocol_packets[PACKET_vCont].support == PACKET_DISABLE)
6d820c5c 4666 return 0;
44eaed12 4667
82f73884
PA
4668 p = rs->buf;
4669 endp = rs->buf + get_remote_packet_size ();
4670
506fb367
DJ
4671 /* If we could generate a wider range of packets, we'd have to worry
4672 about overflowing BUF. Should there be a generic
4673 "multi-part-packet" packet? */
4674
0d8f58ca
PA
4675 p += xsnprintf (p, endp - p, "vCont");
4676
79d7f229 4677 if (ptid_equal (ptid, magic_null_ptid))
c906108c 4678 {
79d7f229
PA
4679 /* MAGIC_NULL_PTID means that we don't have any active threads,
4680 so we don't have any TID numbers the inferior will
4681 understand. Make sure to only send forms that do not specify
4682 a TID. */
a9cbf802 4683 append_resumption (p, endp, minus_one_ptid, step, siggnal);
506fb367 4684 }
0d8f58ca 4685 else if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
506fb367 4686 {
0d8f58ca
PA
4687 /* Resume all threads (of all processes, or of a single
4688 process), with preference for INFERIOR_PTID. This assumes
4689 inferior_ptid belongs to the set of all threads we are about
4690 to resume. */
4691 if (step || siggnal != TARGET_SIGNAL_0)
82f73884 4692 {
0d8f58ca
PA
4693 /* Step inferior_ptid, with or without signal. */
4694 p = append_resumption (p, endp, inferior_ptid, step, siggnal);
82f73884 4695 }
0d8f58ca
PA
4696
4697 /* And continue others without a signal. */
a9cbf802 4698 append_resumption (p, endp, ptid, /*step=*/ 0, TARGET_SIGNAL_0);
c906108c
SS
4699 }
4700 else
506fb367
DJ
4701 {
4702 /* Scheduler locking; resume only PTID. */
a9cbf802 4703 append_resumption (p, endp, ptid, step, siggnal);
506fb367 4704 }
c906108c 4705
82f73884
PA
4706 gdb_assert (strlen (rs->buf) < get_remote_packet_size ());
4707 putpkt (rs->buf);
506fb367 4708
74531fed
PA
4709 if (non_stop)
4710 {
4711 /* In non-stop, the stub replies to vCont with "OK". The stop
4712 reply will be reported asynchronously by means of a `%Stop'
4713 notification. */
4714 getpkt (&rs->buf, &rs->buf_size, 0);
4715 if (strcmp (rs->buf, "OK") != 0)
4716 error (_("Unexpected vCont reply in non-stop mode: %s"), rs->buf);
4717 }
4718
506fb367 4719 return 1;
c906108c 4720}
43ff13b4 4721
506fb367
DJ
4722/* Tell the remote machine to resume. */
4723
4724static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
4725
4726static int last_sent_step;
4727
43ff13b4 4728static void
28439f5e
PA
4729remote_resume (struct target_ops *ops,
4730 ptid_t ptid, int step, enum target_signal siggnal)
43ff13b4 4731{
d01949b6 4732 struct remote_state *rs = get_remote_state ();
2e9f7625 4733 char *buf;
43ff13b4 4734
43ff13b4
JM
4735 last_sent_signal = siggnal;
4736 last_sent_step = step;
4737
506fb367 4738 /* The vCont packet doesn't need to specify threads via Hc. */
40ab02ce
MS
4739 /* No reverse support (yet) for vCont. */
4740 if (execution_direction != EXEC_REVERSE)
4741 if (remote_vcont_resume (ptid, step, siggnal))
4742 goto done;
506fb367 4743
79d7f229
PA
4744 /* All other supported resume packets do use Hc, so set the continue
4745 thread. */
4746 if (ptid_equal (ptid, minus_one_ptid))
4747 set_continue_thread (any_thread_ptid);
506fb367 4748 else
79d7f229 4749 set_continue_thread (ptid);
506fb367 4750
2e9f7625 4751 buf = rs->buf;
b2175913
MS
4752 if (execution_direction == EXEC_REVERSE)
4753 {
4754 /* We don't pass signals to the target in reverse exec mode. */
4755 if (info_verbose && siggnal != TARGET_SIGNAL_0)
7ea6d463 4756 warning (_(" - Can't pass signal %d to target in reverse: ignored."),
b2175913 4757 siggnal);
40ab02ce
MS
4758
4759 if (step
4760 && remote_protocol_packets[PACKET_bs].support == PACKET_DISABLE)
4761 error (_("Remote reverse-step not supported."));
4762 if (!step
4763 && remote_protocol_packets[PACKET_bc].support == PACKET_DISABLE)
08c93ed9 4764 error (_("Remote reverse-continue not supported."));
40ab02ce 4765
b2175913
MS
4766 strcpy (buf, step ? "bs" : "bc");
4767 }
4768 else if (siggnal != TARGET_SIGNAL_0)
43ff13b4
JM
4769 {
4770 buf[0] = step ? 'S' : 'C';
c5aa993b 4771 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
506fb367 4772 buf[2] = tohex (((int) siggnal) & 0xf);
43ff13b4
JM
4773 buf[3] = '\0';
4774 }
4775 else
c5aa993b 4776 strcpy (buf, step ? "s" : "c");
506fb367 4777
44eaed12 4778 putpkt (buf);
43ff13b4 4779
75c99385 4780 done:
2acceee2 4781 /* We are about to start executing the inferior, let's register it
0df8b418
MS
4782 with the event loop. NOTE: this is the one place where all the
4783 execution commands end up. We could alternatively do this in each
23860348 4784 of the execution commands in infcmd.c. */
2acceee2
JM
4785 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
4786 into infcmd.c in order to allow inferior function calls to work
23860348 4787 NOT asynchronously. */
362646f5 4788 if (target_can_async_p ())
2acceee2 4789 target_async (inferior_event_handler, 0);
e24a49d8
PA
4790
4791 /* We've just told the target to resume. The remote server will
4792 wait for the inferior to stop, and then send a stop reply. In
4793 the mean time, we can't start another command/query ourselves
74531fed
PA
4794 because the stub wouldn't be ready to process it. This applies
4795 only to the base all-stop protocol, however. In non-stop (which
4796 only supports vCont), the stub replies with an "OK", and is
4797 immediate able to process further serial input. */
4798 if (!non_stop)
4799 rs->waiting_for_stop_reply = 1;
43ff13b4 4800}
c906108c 4801\f
43ff13b4
JM
4802
4803/* Set up the signal handler for SIGINT, while the target is
23860348 4804 executing, ovewriting the 'regular' SIGINT signal handler. */
43ff13b4 4805static void
fba45db2 4806initialize_sigint_signal_handler (void)
43ff13b4 4807{
43ff13b4
JM
4808 signal (SIGINT, handle_remote_sigint);
4809}
4810
23860348 4811/* Signal handler for SIGINT, while the target is executing. */
43ff13b4 4812static void
fba45db2 4813handle_remote_sigint (int sig)
43ff13b4
JM
4814{
4815 signal (sig, handle_remote_sigint_twice);
43ff13b4
JM
4816 mark_async_signal_handler_wrapper (sigint_remote_token);
4817}
4818
4819/* Signal handler for SIGINT, installed after SIGINT has already been
4820 sent once. It will take effect the second time that the user sends
23860348 4821 a ^C. */
43ff13b4 4822static void
fba45db2 4823handle_remote_sigint_twice (int sig)
43ff13b4 4824{
b803fb0f 4825 signal (sig, handle_remote_sigint);
43ff13b4
JM
4826 mark_async_signal_handler_wrapper (sigint_remote_twice_token);
4827}
4828
6426a772 4829/* Perform the real interruption of the target execution, in response
23860348 4830 to a ^C. */
c5aa993b 4831static void
fba45db2 4832async_remote_interrupt (gdb_client_data arg)
43ff13b4
JM
4833{
4834 if (remote_debug)
4835 fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
4836
94cc34af 4837 target_stop (inferior_ptid);
43ff13b4
JM
4838}
4839
0df8b418 4840/* Perform interrupt, if the first attempt did not succeed. Just give
23860348 4841 up on the target alltogether. */
2df3850c 4842void
fba45db2 4843async_remote_interrupt_twice (gdb_client_data arg)
43ff13b4 4844{
2df3850c
JM
4845 if (remote_debug)
4846 fprintf_unfiltered (gdb_stdlog, "remote_interrupt_twice called\n");
b803fb0f
DJ
4847
4848 interrupt_query ();
43ff13b4
JM
4849}
4850
4851/* Reinstall the usual SIGINT handlers, after the target has
23860348 4852 stopped. */
6426a772
JM
4853static void
4854cleanup_sigint_signal_handler (void *dummy)
43ff13b4
JM
4855{
4856 signal (SIGINT, handle_sigint);
43ff13b4
JM
4857}
4858
c906108c
SS
4859/* Send ^C to target to halt it. Target will respond, and send us a
4860 packet. */
507f3c78 4861static void (*ofunc) (int);
c906108c 4862
0df8b418
MS
4863/* The command line interface's stop routine. This function is installed
4864 as a signal handler for SIGINT. The first time a user requests a
4865 stop, we call remote_stop to send a break or ^C. If there is no
7a292a7a 4866 response from the target (it didn't stop when the user requested it),
23860348 4867 we ask the user if he'd like to detach from the target. */
c906108c 4868static void
fba45db2 4869remote_interrupt (int signo)
c906108c 4870{
23860348 4871 /* If this doesn't work, try more severe steps. */
7a292a7a
SS
4872 signal (signo, remote_interrupt_twice);
4873
b803fb0f 4874 gdb_call_async_signal_handler (sigint_remote_token, 1);
7a292a7a
SS
4875}
4876
4877/* The user typed ^C twice. */
4878
4879static void
fba45db2 4880remote_interrupt_twice (int signo)
7a292a7a
SS
4881{
4882 signal (signo, ofunc);
b803fb0f 4883 gdb_call_async_signal_handler (sigint_remote_twice_token, 1);
c906108c
SS
4884 signal (signo, remote_interrupt);
4885}
7a292a7a 4886
74531fed
PA
4887/* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
4888 thread, all threads of a remote process, or all threads of all
4889 processes. */
4890
4891static void
4892remote_stop_ns (ptid_t ptid)
4893{
4894 struct remote_state *rs = get_remote_state ();
4895 char *p = rs->buf;
4896 char *endp = rs->buf + get_remote_packet_size ();
74531fed
PA
4897
4898 if (remote_protocol_packets[PACKET_vCont].support == PACKET_SUPPORT_UNKNOWN)
4899 remote_vcont_probe (rs);
4900
4901 if (!rs->support_vCont_t)
4902 error (_("Remote server does not support stopping threads"));
4903
f91d3df5
PA
4904 if (ptid_equal (ptid, minus_one_ptid)
4905 || (!remote_multi_process_p (rs) && ptid_is_pid (ptid)))
74531fed
PA
4906 p += xsnprintf (p, endp - p, "vCont;t");
4907 else
4908 {
4909 ptid_t nptid;
4910
74531fed
PA
4911 p += xsnprintf (p, endp - p, "vCont;t:");
4912
4913 if (ptid_is_pid (ptid))
4914 /* All (-1) threads of process. */
4915 nptid = ptid_build (ptid_get_pid (ptid), 0, -1);
4916 else
4917 {
4918 /* Small optimization: if we already have a stop reply for
4919 this thread, no use in telling the stub we want this
4920 stopped. */
4921 if (peek_stop_reply (ptid))
4922 return;
4923
4924 nptid = ptid;
4925 }
4926
a9cbf802 4927 write_ptid (p, endp, nptid);
74531fed
PA
4928 }
4929
4930 /* In non-stop, we get an immediate OK reply. The stop reply will
4931 come in asynchronously by notification. */
4932 putpkt (rs->buf);
4933 getpkt (&rs->buf, &rs->buf_size, 0);
4934 if (strcmp (rs->buf, "OK") != 0)
4935 error (_("Stopping %s failed: %s"), target_pid_to_str (ptid), rs->buf);
4936}
4937
4938/* All-stop version of target_stop. Sends a break or a ^C to stop the
4939 remote target. It is undefined which thread of which process
4940 reports the stop. */
4941
4942static void
4943remote_stop_as (ptid_t ptid)
4944{
4945 struct remote_state *rs = get_remote_state ();
4946
3a29589a
DJ
4947 rs->ctrlc_pending_p = 1;
4948
74531fed
PA
4949 /* If the inferior is stopped already, but the core didn't know
4950 about it yet, just ignore the request. The cached wait status
4951 will be collected in remote_wait. */
4952 if (rs->cached_wait_status)
4953 return;
4954
9a7071a8
JB
4955 /* Send interrupt_sequence to remote target. */
4956 send_interrupt_sequence ();
74531fed
PA
4957}
4958
0df8b418 4959/* This is the generic stop called via the target vector. When a target
7a292a7a 4960 interrupt is requested, either by the command line or the GUI, we
23860348 4961 will eventually end up here. */
74531fed 4962
c906108c 4963static void
94cc34af 4964remote_stop (ptid_t ptid)
c906108c 4965{
7a292a7a 4966 if (remote_debug)
0f71a2f6 4967 fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
c906108c 4968
74531fed
PA
4969 if (non_stop)
4970 remote_stop_ns (ptid);
c906108c 4971 else
74531fed 4972 remote_stop_as (ptid);
c906108c
SS
4973}
4974
4975/* Ask the user what to do when an interrupt is received. */
4976
4977static void
fba45db2 4978interrupt_query (void)
c906108c
SS
4979{
4980 target_terminal_ours ();
4981
74531fed 4982 if (target_can_async_p ())
c906108c 4983 {
74531fed 4984 signal (SIGINT, handle_sigint);
315a522e 4985 deprecated_throw_reason (RETURN_QUIT);
c906108c 4986 }
74531fed
PA
4987 else
4988 {
9e2f0ad4
HZ
4989 if (query (_("Interrupted while waiting for the program.\n\
4990Give up (and stop debugging it)? ")))
74531fed
PA
4991 {
4992 pop_target ();
4993 deprecated_throw_reason (RETURN_QUIT);
4994 }
4995 }
c906108c
SS
4996
4997 target_terminal_inferior ();
4998}
4999
6426a772
JM
5000/* Enable/disable target terminal ownership. Most targets can use
5001 terminal groups to control terminal ownership. Remote targets are
5002 different in that explicit transfer of ownership to/from GDB/target
23860348 5003 is required. */
6426a772
JM
5004
5005static void
75c99385 5006remote_terminal_inferior (void)
6426a772 5007{
c6ebd6cf 5008 if (!target_async_permitted)
75c99385
PA
5009 /* Nothing to do. */
5010 return;
5011
d9d2d8b6
PA
5012 /* FIXME: cagney/1999-09-27: Make calls to target_terminal_*()
5013 idempotent. The event-loop GDB talking to an asynchronous target
5014 with a synchronous command calls this function from both
5015 event-top.c and infrun.c/infcmd.c. Once GDB stops trying to
5016 transfer the terminal to the target when it shouldn't this guard
5017 can go away. */
6426a772
JM
5018 if (!remote_async_terminal_ours_p)
5019 return;
5020 delete_file_handler (input_fd);
5021 remote_async_terminal_ours_p = 0;
5022 initialize_sigint_signal_handler ();
5023 /* NOTE: At this point we could also register our selves as the
5024 recipient of all input. Any characters typed could then be
23860348 5025 passed on down to the target. */
6426a772
JM
5026}
5027
5028static void
75c99385 5029remote_terminal_ours (void)
6426a772 5030{
c6ebd6cf 5031 if (!target_async_permitted)
75c99385
PA
5032 /* Nothing to do. */
5033 return;
5034
5035 /* See FIXME in remote_terminal_inferior. */
6426a772
JM
5036 if (remote_async_terminal_ours_p)
5037 return;
5038 cleanup_sigint_signal_handler (NULL);
5039 add_file_handler (input_fd, stdin_event_handler, 0);
5040 remote_async_terminal_ours_p = 1;
5041}
5042
176a6961 5043static void
917317f4 5044remote_console_output (char *msg)
c906108c
SS
5045{
5046 char *p;
5047
c5aa993b 5048 for (p = msg; p[0] && p[1]; p += 2)
c906108c
SS
5049 {
5050 char tb[2];
5051 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
a744cf53 5052
c906108c
SS
5053 tb[0] = c;
5054 tb[1] = 0;
43ff13b4 5055 fputs_unfiltered (tb, gdb_stdtarg);
c906108c 5056 }
00db5b94
PA
5057 gdb_flush (gdb_stdtarg);
5058}
74531fed
PA
5059
5060typedef struct cached_reg
5061{
5062 int num;
5063 gdb_byte data[MAX_REGISTER_SIZE];
5064} cached_reg_t;
5065
5066DEF_VEC_O(cached_reg_t);
5067
5068struct stop_reply
5069{
5070 struct stop_reply *next;
5071
5072 ptid_t ptid;
5073
5074 struct target_waitstatus ws;
5075
15148d6a
PA
5076 /* Expedited registers. This makes remote debugging a bit more
5077 efficient for those targets that provide critical registers as
5078 part of their normal status mechanism (as another roundtrip to
5079 fetch them is avoided). */
74531fed
PA
5080 VEC(cached_reg_t) *regcache;
5081
5082 int stopped_by_watchpoint_p;
5083 CORE_ADDR watch_data_address;
5084
5085 int solibs_changed;
5086 int replay_event;
dc146f7c
VP
5087
5088 int core;
74531fed
PA
5089};
5090
5091/* The list of already fetched and acknowledged stop events. */
5092static struct stop_reply *stop_reply_queue;
5093
5094static struct stop_reply *
5095stop_reply_xmalloc (void)
5096{
5097 struct stop_reply *r = XMALLOC (struct stop_reply);
a744cf53 5098
74531fed
PA
5099 r->next = NULL;
5100 return r;
5101}
5102
5103static void
5104stop_reply_xfree (struct stop_reply *r)
5105{
5106 if (r != NULL)
5107 {
5108 VEC_free (cached_reg_t, r->regcache);
5109 xfree (r);
5110 }
c906108c
SS
5111}
5112
74531fed
PA
5113/* Discard all pending stop replies of inferior PID. If PID is -1,
5114 discard everything. */
c906108c 5115
74531fed
PA
5116static void
5117discard_pending_stop_replies (int pid)
c906108c 5118{
74531fed 5119 struct stop_reply *prev = NULL, *reply, *next;
c906108c 5120
74531fed
PA
5121 /* Discard the in-flight notification. */
5122 if (pending_stop_reply != NULL
5123 && (pid == -1
5124 || ptid_get_pid (pending_stop_reply->ptid) == pid))
5125 {
5126 stop_reply_xfree (pending_stop_reply);
5127 pending_stop_reply = NULL;
5128 }
c906108c 5129
74531fed
PA
5130 /* Discard the stop replies we have already pulled with
5131 vStopped. */
5132 for (reply = stop_reply_queue; reply; reply = next)
43ff13b4 5133 {
74531fed
PA
5134 next = reply->next;
5135 if (pid == -1
5136 || ptid_get_pid (reply->ptid) == pid)
9fa2223d 5137 {
74531fed
PA
5138 if (reply == stop_reply_queue)
5139 stop_reply_queue = reply->next;
5140 else
5141 prev->next = reply->next;
5142
5143 stop_reply_xfree (reply);
9fa2223d 5144 }
74531fed
PA
5145 else
5146 prev = reply;
c8e38a49 5147 }
74531fed 5148}
43ff13b4 5149
74531fed 5150/* Cleanup wrapper. */
2e9f7625 5151
74531fed
PA
5152static void
5153do_stop_reply_xfree (void *arg)
5154{
5155 struct stop_reply *r = arg;
a744cf53 5156
74531fed
PA
5157 stop_reply_xfree (r);
5158}
75c99385 5159
74531fed
PA
5160/* Look for a queued stop reply belonging to PTID. If one is found,
5161 remove it from the queue, and return it. Returns NULL if none is
5162 found. If there are still queued events left to process, tell the
5163 event loop to get back to target_wait soon. */
e24a49d8 5164
74531fed
PA
5165static struct stop_reply *
5166queued_stop_reply (ptid_t ptid)
5167{
0723dbf5
PA
5168 struct stop_reply *it;
5169 struct stop_reply **it_link;
74531fed 5170
0723dbf5
PA
5171 it = stop_reply_queue;
5172 it_link = &stop_reply_queue;
5173 while (it)
c8e38a49 5174 {
0723dbf5
PA
5175 if (ptid_match (it->ptid, ptid))
5176 {
5177 *it_link = it->next;
5178 it->next = NULL;
5179 break;
5180 }
e24a49d8 5181
0723dbf5
PA
5182 it_link = &it->next;
5183 it = *it_link;
5184 }
74531fed
PA
5185
5186 if (stop_reply_queue)
5187 /* There's still at least an event left. */
5188 mark_async_event_handler (remote_async_inferior_event_token);
5189
5190 return it;
5191}
5192
5193/* Push a fully parsed stop reply in the stop reply queue. Since we
5194 know that we now have at least one queued event left to pass to the
5195 core side, tell the event loop to get back to target_wait soon. */
5196
5197static void
5198push_stop_reply (struct stop_reply *new_event)
5199{
5200 struct stop_reply *event;
5201
5202 if (stop_reply_queue)
5203 {
5204 for (event = stop_reply_queue;
5205 event && event->next;
5206 event = event->next)
5207 ;
5208
5209 event->next = new_event;
5210 }
5211 else
5212 stop_reply_queue = new_event;
5213
5214 mark_async_event_handler (remote_async_inferior_event_token);
5215}
5216
5217/* Returns true if we have a stop reply for PTID. */
5218
5219static int
5220peek_stop_reply (ptid_t ptid)
5221{
5222 struct stop_reply *it;
5223
5224 for (it = stop_reply_queue; it; it = it->next)
5225 if (ptid_equal (ptid, it->ptid))
5226 {
5227 if (it->ws.kind == TARGET_WAITKIND_STOPPED)
5228 return 1;
5229 }
5230
5231 return 0;
5232}
5233
5234/* Parse the stop reply in BUF. Either the function succeeds, and the
5235 result is stored in EVENT, or throws an error. */
5236
5237static void
5238remote_parse_stop_reply (char *buf, struct stop_reply *event)
5239{
5240 struct remote_arch_state *rsa = get_remote_arch_state ();
5241 ULONGEST addr;
5242 char *p;
5243
5244 event->ptid = null_ptid;
5245 event->ws.kind = TARGET_WAITKIND_IGNORE;
5246 event->ws.value.integer = 0;
5247 event->solibs_changed = 0;
5248 event->replay_event = 0;
5249 event->stopped_by_watchpoint_p = 0;
5250 event->regcache = NULL;
dc146f7c 5251 event->core = -1;
74531fed
PA
5252
5253 switch (buf[0])
5254 {
5255 case 'T': /* Status with PC, SP, FP, ... */
cea39f65
MS
5256 /* Expedited reply, containing Signal, {regno, reg} repeat. */
5257 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
5258 ss = signal number
5259 n... = register number
5260 r... = register contents
5261 */
5262
5263 p = &buf[3]; /* after Txx */
5264 while (*p)
5265 {
5266 char *p1;
5267 char *p_temp;
5268 int fieldsize;
5269 LONGEST pnum = 0;
43ff13b4 5270
cea39f65
MS
5271 /* If the packet contains a register number, save it in
5272 pnum and set p1 to point to the character following it.
5273 Otherwise p1 points to p. */
3c3bea1c 5274
cea39f65
MS
5275 /* If this packet is an awatch packet, don't parse the 'a'
5276 as a register number. */
c8e38a49 5277
dc146f7c
VP
5278 if (strncmp (p, "awatch", strlen("awatch")) != 0
5279 && strncmp (p, "core", strlen ("core") != 0))
cea39f65
MS
5280 {
5281 /* Read the ``P'' register number. */
5282 pnum = strtol (p, &p_temp, 16);
5283 p1 = p_temp;
5284 }
5285 else
5286 p1 = p;
802188a7 5287
cea39f65
MS
5288 if (p1 == p) /* No register number present here. */
5289 {
5290 p1 = strchr (p, ':');
5291 if (p1 == NULL)
5292 error (_("Malformed packet(a) (missing colon): %s\n\
c8e38a49 5293Packet: '%s'\n"),
cea39f65
MS
5294 p, buf);
5295 if (strncmp (p, "thread", p1 - p) == 0)
5296 event->ptid = read_ptid (++p1, &p);
5297 else if ((strncmp (p, "watch", p1 - p) == 0)
5298 || (strncmp (p, "rwatch", p1 - p) == 0)
5299 || (strncmp (p, "awatch", p1 - p) == 0))
5300 {
5301 event->stopped_by_watchpoint_p = 1;
5302 p = unpack_varlen_hex (++p1, &addr);
5303 event->watch_data_address = (CORE_ADDR) addr;
5304 }
5305 else if (strncmp (p, "library", p1 - p) == 0)
5306 {
5307 p1++;
5308 p_temp = p1;
5309 while (*p_temp && *p_temp != ';')
5310 p_temp++;
c8e38a49 5311
cea39f65
MS
5312 event->solibs_changed = 1;
5313 p = p_temp;
5314 }
5315 else if (strncmp (p, "replaylog", p1 - p) == 0)
5316 {
5317 /* NO_HISTORY event.
5318 p1 will indicate "begin" or "end", but
5319 it makes no difference for now, so ignore it. */
5320 event->replay_event = 1;
5321 p_temp = strchr (p1 + 1, ';');
5322 if (p_temp)
c8e38a49 5323 p = p_temp;
cea39f65 5324 }
dc146f7c
VP
5325 else if (strncmp (p, "core", p1 - p) == 0)
5326 {
5327 ULONGEST c;
a744cf53 5328
dc146f7c
VP
5329 p = unpack_varlen_hex (++p1, &c);
5330 event->core = c;
5331 }
cea39f65
MS
5332 else
5333 {
5334 /* Silently skip unknown optional info. */
5335 p_temp = strchr (p1 + 1, ';');
5336 if (p_temp)
5337 p = p_temp;
5338 }
5339 }
5340 else
5341 {
5342 struct packet_reg *reg = packet_reg_from_pnum (rsa, pnum);
5343 cached_reg_t cached_reg;
74531fed 5344
cea39f65 5345 p = p1;
75c99385 5346
cea39f65
MS
5347 if (*p != ':')
5348 error (_("Malformed packet(b) (missing colon): %s\n\
8a3fe4f8 5349Packet: '%s'\n"),
cea39f65
MS
5350 p, buf);
5351 ++p;
43ff13b4 5352
cea39f65
MS
5353 if (reg == NULL)
5354 error (_("Remote sent bad register number %s: %s\n\
8a3fe4f8 5355Packet: '%s'\n"),
7c47795c 5356 hex_string (pnum), p, buf);
c8e38a49 5357
cea39f65 5358 cached_reg.num = reg->regnum;
4100683b 5359
cea39f65
MS
5360 fieldsize = hex2bin (p, cached_reg.data,
5361 register_size (target_gdbarch,
5362 reg->regnum));
5363 p += 2 * fieldsize;
5364 if (fieldsize < register_size (target_gdbarch,
5365 reg->regnum))
5366 warning (_("Remote reply is too short: %s"), buf);
74531fed 5367
cea39f65
MS
5368 VEC_safe_push (cached_reg_t, event->regcache, &cached_reg);
5369 }
c8e38a49 5370
cea39f65
MS
5371 if (*p != ';')
5372 error (_("Remote register badly formatted: %s\nhere: %s"),
5373 buf, p);
5374 ++p;
5375 }
c8e38a49
PA
5376 /* fall through */
5377 case 'S': /* Old style status, just signal only. */
74531fed
PA
5378 if (event->solibs_changed)
5379 event->ws.kind = TARGET_WAITKIND_LOADED;
5380 else if (event->replay_event)
5381 event->ws.kind = TARGET_WAITKIND_NO_HISTORY;
c8e38a49
PA
5382 else
5383 {
74531fed
PA
5384 event->ws.kind = TARGET_WAITKIND_STOPPED;
5385 event->ws.value.sig = (enum target_signal)
c8e38a49
PA
5386 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
5387 }
5388 break;
5389 case 'W': /* Target exited. */
5390 case 'X':
5391 {
5392 char *p;
5393 int pid;
5394 ULONGEST value;
82f73884 5395
c8e38a49
PA
5396 /* GDB used to accept only 2 hex chars here. Stubs should
5397 only send more if they detect GDB supports multi-process
5398 support. */
5399 p = unpack_varlen_hex (&buf[1], &value);
82f73884 5400
c8e38a49
PA
5401 if (buf[0] == 'W')
5402 {
5403 /* The remote process exited. */
74531fed
PA
5404 event->ws.kind = TARGET_WAITKIND_EXITED;
5405 event->ws.value.integer = value;
c8e38a49
PA
5406 }
5407 else
5408 {
5409 /* The remote process exited with a signal. */
74531fed
PA
5410 event->ws.kind = TARGET_WAITKIND_SIGNALLED;
5411 event->ws.value.sig = (enum target_signal) value;
c8e38a49 5412 }
82f73884 5413
c8e38a49
PA
5414 /* If no process is specified, assume inferior_ptid. */
5415 pid = ptid_get_pid (inferior_ptid);
5416 if (*p == '\0')
5417 ;
5418 else if (*p == ';')
5419 {
5420 p++;
5421
5422 if (p == '\0')
82f73884 5423 ;
c8e38a49
PA
5424 else if (strncmp (p,
5425 "process:", sizeof ("process:") - 1) == 0)
82f73884 5426 {
c8e38a49 5427 ULONGEST upid;
a744cf53 5428
c8e38a49
PA
5429 p += sizeof ("process:") - 1;
5430 unpack_varlen_hex (p, &upid);
5431 pid = upid;
82f73884
PA
5432 }
5433 else
5434 error (_("unknown stop reply packet: %s"), buf);
43ff13b4 5435 }
c8e38a49
PA
5436 else
5437 error (_("unknown stop reply packet: %s"), buf);
74531fed
PA
5438 event->ptid = pid_to_ptid (pid);
5439 }
5440 break;
5441 }
5442
5443 if (non_stop && ptid_equal (event->ptid, null_ptid))
5444 error (_("No process or thread specified in stop reply: %s"), buf);
5445}
5446
5447/* When the stub wants to tell GDB about a new stop reply, it sends a
5448 stop notification (%Stop). Those can come it at any time, hence,
5449 we have to make sure that any pending putpkt/getpkt sequence we're
5450 making is finished, before querying the stub for more events with
5451 vStopped. E.g., if we started a vStopped sequence immediatelly
5452 upon receiving the %Stop notification, something like this could
5453 happen:
5454
5455 1.1) --> Hg 1
5456 1.2) <-- OK
5457 1.3) --> g
5458 1.4) <-- %Stop
5459 1.5) --> vStopped
5460 1.6) <-- (registers reply to step #1.3)
5461
5462 Obviously, the reply in step #1.6 would be unexpected to a vStopped
5463 query.
5464
5465 To solve this, whenever we parse a %Stop notification sucessfully,
5466 we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
5467 doing whatever we were doing:
5468
5469 2.1) --> Hg 1
5470 2.2) <-- OK
5471 2.3) --> g
5472 2.4) <-- %Stop
5473 <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
5474 2.5) <-- (registers reply to step #2.3)
5475
5476 Eventualy after step #2.5, we return to the event loop, which
5477 notices there's an event on the
5478 REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
5479 associated callback --- the function below. At this point, we're
5480 always safe to start a vStopped sequence. :
5481
5482 2.6) --> vStopped
5483 2.7) <-- T05 thread:2
5484 2.8) --> vStopped
5485 2.9) --> OK
5486*/
5487
5488static void
5489remote_get_pending_stop_replies (void)
5490{
5491 struct remote_state *rs = get_remote_state ();
74531fed
PA
5492
5493 if (pending_stop_reply)
5494 {
5495 /* acknowledge */
5496 putpkt ("vStopped");
5497
5498 /* Now we can rely on it. */
5499 push_stop_reply (pending_stop_reply);
5500 pending_stop_reply = NULL;
5501
5502 while (1)
5503 {
5504 getpkt (&rs->buf, &rs->buf_size, 0);
5505 if (strcmp (rs->buf, "OK") == 0)
5506 break;
5507 else
5508 {
5509 struct cleanup *old_chain;
5510 struct stop_reply *stop_reply = stop_reply_xmalloc ();
5511
5512 old_chain = make_cleanup (do_stop_reply_xfree, stop_reply);
5513 remote_parse_stop_reply (rs->buf, stop_reply);
5514
5515 /* acknowledge */
5516 putpkt ("vStopped");
5517
5518 if (stop_reply->ws.kind != TARGET_WAITKIND_IGNORE)
5519 {
5520 /* Now we can rely on it. */
5521 discard_cleanups (old_chain);
5522 push_stop_reply (stop_reply);
5523 }
5524 else
5525 /* We got an unknown stop reply. */
5526 do_cleanups (old_chain);
5527 }
5528 }
5529 }
5530}
5531
5532
5533/* Called when it is decided that STOP_REPLY holds the info of the
5534 event that is to be returned to the core. This function always
5535 destroys STOP_REPLY. */
5536
5537static ptid_t
5538process_stop_reply (struct stop_reply *stop_reply,
5539 struct target_waitstatus *status)
5540{
5541 ptid_t ptid;
5542
5543 *status = stop_reply->ws;
5544 ptid = stop_reply->ptid;
5545
5546 /* If no thread/process was reported by the stub, assume the current
5547 inferior. */
5548 if (ptid_equal (ptid, null_ptid))
5549 ptid = inferior_ptid;
5550
5f3563ea
PA
5551 if (status->kind != TARGET_WAITKIND_EXITED
5552 && status->kind != TARGET_WAITKIND_SIGNALLED)
74531fed 5553 {
5f3563ea
PA
5554 /* Expedited registers. */
5555 if (stop_reply->regcache)
5556 {
217f1f79
UW
5557 struct regcache *regcache
5558 = get_thread_arch_regcache (ptid, target_gdbarch);
5f3563ea
PA
5559 cached_reg_t *reg;
5560 int ix;
5561
5562 for (ix = 0;
5563 VEC_iterate(cached_reg_t, stop_reply->regcache, ix, reg);
5564 ix++)
217f1f79 5565 regcache_raw_supply (regcache, reg->num, reg->data);
5f3563ea
PA
5566 VEC_free (cached_reg_t, stop_reply->regcache);
5567 }
74531fed 5568
5f3563ea
PA
5569 remote_stopped_by_watchpoint_p = stop_reply->stopped_by_watchpoint_p;
5570 remote_watch_data_address = stop_reply->watch_data_address;
1941c569
PA
5571
5572 remote_notice_new_inferior (ptid, 0);
dc146f7c 5573 demand_private_info (ptid)->core = stop_reply->core;
74531fed
PA
5574 }
5575
74531fed
PA
5576 stop_reply_xfree (stop_reply);
5577 return ptid;
5578}
5579
5580/* The non-stop mode version of target_wait. */
5581
5582static ptid_t
47608cb1 5583remote_wait_ns (ptid_t ptid, struct target_waitstatus *status, int options)
74531fed
PA
5584{
5585 struct remote_state *rs = get_remote_state ();
74531fed
PA
5586 struct stop_reply *stop_reply;
5587 int ret;
5588
5589 /* If in non-stop mode, get out of getpkt even if a
5590 notification is received. */
5591
5592 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
5593 0 /* forever */);
5594 while (1)
5595 {
5596 if (ret != -1)
5597 switch (rs->buf[0])
5598 {
5599 case 'E': /* Error of some sort. */
5600 /* We're out of sync with the target now. Did it continue
5601 or not? We can't tell which thread it was in non-stop,
5602 so just ignore this. */
5603 warning (_("Remote failure reply: %s"), rs->buf);
5604 break;
5605 case 'O': /* Console output. */
5606 remote_console_output (rs->buf + 1);
5607 break;
5608 default:
5609 warning (_("Invalid remote reply: %s"), rs->buf);
5610 break;
5611 }
5612
5613 /* Acknowledge a pending stop reply that may have arrived in the
5614 mean time. */
5615 if (pending_stop_reply != NULL)
5616 remote_get_pending_stop_replies ();
5617
5618 /* If indeed we noticed a stop reply, we're done. */
5619 stop_reply = queued_stop_reply (ptid);
5620 if (stop_reply != NULL)
5621 return process_stop_reply (stop_reply, status);
5622
47608cb1 5623 /* Still no event. If we're just polling for an event, then
74531fed 5624 return to the event loop. */
47608cb1 5625 if (options & TARGET_WNOHANG)
74531fed
PA
5626 {
5627 status->kind = TARGET_WAITKIND_IGNORE;
5628 return minus_one_ptid;
5629 }
5630
47608cb1 5631 /* Otherwise do a blocking wait. */
74531fed
PA
5632 ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
5633 1 /* forever */);
5634 }
5635}
5636
5637/* Wait until the remote machine stops, then return, storing status in
5638 STATUS just as `wait' would. */
5639
5640static ptid_t
47608cb1 5641remote_wait_as (ptid_t ptid, struct target_waitstatus *status, int options)
74531fed
PA
5642{
5643 struct remote_state *rs = get_remote_state ();
74531fed 5644 ptid_t event_ptid = null_ptid;
cea39f65 5645 char *buf;
74531fed
PA
5646 struct stop_reply *stop_reply;
5647
47608cb1
PA
5648 again:
5649
74531fed
PA
5650 status->kind = TARGET_WAITKIND_IGNORE;
5651 status->value.integer = 0;
5652
5653 stop_reply = queued_stop_reply (ptid);
5654 if (stop_reply != NULL)
5655 return process_stop_reply (stop_reply, status);
5656
5657 if (rs->cached_wait_status)
5658 /* Use the cached wait status, but only once. */
5659 rs->cached_wait_status = 0;
5660 else
5661 {
5662 int ret;
5663
5664 if (!target_is_async_p ())
5665 {
5666 ofunc = signal (SIGINT, remote_interrupt);
5667 /* If the user hit C-c before this packet, or between packets,
5668 pretend that it was hit right here. */
5669 if (quit_flag)
5670 {
5671 quit_flag = 0;
5672 remote_interrupt (SIGINT);
5673 }
5674 }
5675
5676 /* FIXME: cagney/1999-09-27: If we're in async mode we should
5677 _never_ wait for ever -> test on target_is_async_p().
5678 However, before we do that we need to ensure that the caller
5679 knows how to take the target into/out of async mode. */
5680 ret = getpkt_sane (&rs->buf, &rs->buf_size, wait_forever_enabled_p);
5681 if (!target_is_async_p ())
5682 signal (SIGINT, ofunc);
5683 }
5684
5685 buf = rs->buf;
5686
5687 remote_stopped_by_watchpoint_p = 0;
5688
5689 /* We got something. */
5690 rs->waiting_for_stop_reply = 0;
5691
3a29589a
DJ
5692 /* Assume that the target has acknowledged Ctrl-C unless we receive
5693 an 'F' or 'O' packet. */
5694 if (buf[0] != 'F' && buf[0] != 'O')
5695 rs->ctrlc_pending_p = 0;
5696
74531fed
PA
5697 switch (buf[0])
5698 {
5699 case 'E': /* Error of some sort. */
5700 /* We're out of sync with the target now. Did it continue or
5701 not? Not is more likely, so report a stop. */
5702 warning (_("Remote failure reply: %s"), buf);
5703 status->kind = TARGET_WAITKIND_STOPPED;
5704 status->value.sig = TARGET_SIGNAL_0;
5705 break;
5706 case 'F': /* File-I/O request. */
3a29589a
DJ
5707 remote_fileio_request (buf, rs->ctrlc_pending_p);
5708 rs->ctrlc_pending_p = 0;
74531fed
PA
5709 break;
5710 case 'T': case 'S': case 'X': case 'W':
5711 {
5712 struct stop_reply *stop_reply;
5713 struct cleanup *old_chain;
5714
5715 stop_reply = stop_reply_xmalloc ();
5716 old_chain = make_cleanup (do_stop_reply_xfree, stop_reply);
5717 remote_parse_stop_reply (buf, stop_reply);
5718 discard_cleanups (old_chain);
5719 event_ptid = process_stop_reply (stop_reply, status);
c8e38a49
PA
5720 break;
5721 }
5722 case 'O': /* Console output. */
5723 remote_console_output (buf + 1);
e24a49d8 5724
c8e38a49
PA
5725 /* The target didn't really stop; keep waiting. */
5726 rs->waiting_for_stop_reply = 1;
e24a49d8 5727
c8e38a49
PA
5728 break;
5729 case '\0':
5730 if (last_sent_signal != TARGET_SIGNAL_0)
5731 {
5732 /* Zero length reply means that we tried 'S' or 'C' and the
5733 remote system doesn't support it. */
5734 target_terminal_ours_for_output ();
5735 printf_filtered
5736 ("Can't send signals to this remote system. %s not sent.\n",
5737 target_signal_to_name (last_sent_signal));
5738 last_sent_signal = TARGET_SIGNAL_0;
5739 target_terminal_inferior ();
5740
5741 strcpy ((char *) buf, last_sent_step ? "s" : "c");
5742 putpkt ((char *) buf);
5743
5744 /* We just told the target to resume, so a stop reply is in
5745 order. */
e24a49d8 5746 rs->waiting_for_stop_reply = 1;
c8e38a49 5747 break;
43ff13b4 5748 }
c8e38a49
PA
5749 /* else fallthrough */
5750 default:
5751 warning (_("Invalid remote reply: %s"), buf);
5752 /* Keep waiting. */
5753 rs->waiting_for_stop_reply = 1;
5754 break;
43ff13b4 5755 }
c8e38a49 5756
c8e38a49 5757 if (status->kind == TARGET_WAITKIND_IGNORE)
47608cb1
PA
5758 {
5759 /* Nothing interesting happened. If we're doing a non-blocking
5760 poll, we're done. Otherwise, go back to waiting. */
5761 if (options & TARGET_WNOHANG)
5762 return minus_one_ptid;
5763 else
5764 goto again;
5765 }
74531fed
PA
5766 else if (status->kind != TARGET_WAITKIND_EXITED
5767 && status->kind != TARGET_WAITKIND_SIGNALLED)
82f73884
PA
5768 {
5769 if (!ptid_equal (event_ptid, null_ptid))
5770 record_currthread (event_ptid);
5771 else
5772 event_ptid = inferior_ptid;
43ff13b4 5773 }
74531fed
PA
5774 else
5775 /* A process exit. Invalidate our notion of current thread. */
5776 record_currthread (minus_one_ptid);
79d7f229 5777
82f73884 5778 return event_ptid;
43ff13b4
JM
5779}
5780
74531fed
PA
5781/* Wait until the remote machine stops, then return, storing status in
5782 STATUS just as `wait' would. */
5783
c8e38a49 5784static ptid_t
117de6a9 5785remote_wait (struct target_ops *ops,
47608cb1 5786 ptid_t ptid, struct target_waitstatus *status, int options)
c8e38a49
PA
5787{
5788 ptid_t event_ptid;
5789
74531fed 5790 if (non_stop)
47608cb1 5791 event_ptid = remote_wait_ns (ptid, status, options);
74531fed 5792 else
47608cb1 5793 event_ptid = remote_wait_as (ptid, status, options);
c8e38a49 5794
74531fed 5795 if (target_can_async_p ())
c8e38a49 5796 {
74531fed
PA
5797 /* If there are are events left in the queue tell the event loop
5798 to return here. */
5799 if (stop_reply_queue)
5800 mark_async_event_handler (remote_async_inferior_event_token);
c8e38a49 5801 }
c8e38a49
PA
5802
5803 return event_ptid;
5804}
5805
74ca34ce 5806/* Fetch a single register using a 'p' packet. */
c906108c 5807
b96ec7ac 5808static int
56be3814 5809fetch_register_using_p (struct regcache *regcache, struct packet_reg *reg)
b96ec7ac
AC
5810{
5811 struct remote_state *rs = get_remote_state ();
2e9f7625 5812 char *buf, *p;
b96ec7ac
AC
5813 char regp[MAX_REGISTER_SIZE];
5814 int i;
5815
74ca34ce
DJ
5816 if (remote_protocol_packets[PACKET_p].support == PACKET_DISABLE)
5817 return 0;
5818
5819 if (reg->pnum == -1)
5820 return 0;
5821
2e9f7625 5822 p = rs->buf;
fcad0fa4 5823 *p++ = 'p';
74ca34ce 5824 p += hexnumstr (p, reg->pnum);
fcad0fa4 5825 *p++ = '\0';
1f4437a4
MS
5826 putpkt (rs->buf);
5827 getpkt (&rs->buf, &rs->buf_size, 0);
3f9a994c 5828
2e9f7625
DJ
5829 buf = rs->buf;
5830
74ca34ce
DJ
5831 switch (packet_ok (buf, &remote_protocol_packets[PACKET_p]))
5832 {
5833 case PACKET_OK:
5834 break;
5835 case PACKET_UNKNOWN:
5836 return 0;
5837 case PACKET_ERROR:
27a9c0bf
MS
5838 error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
5839 gdbarch_register_name (get_regcache_arch (regcache),
5840 reg->regnum),
5841 buf);
74ca34ce 5842 }
3f9a994c
JB
5843
5844 /* If this register is unfetchable, tell the regcache. */
5845 if (buf[0] == 'x')
8480adf2 5846 {
56be3814 5847 regcache_raw_supply (regcache, reg->regnum, NULL);
8480adf2 5848 return 1;
b96ec7ac 5849 }
b96ec7ac 5850
3f9a994c
JB
5851 /* Otherwise, parse and supply the value. */
5852 p = buf;
5853 i = 0;
5854 while (p[0] != 0)
5855 {
5856 if (p[1] == 0)
74ca34ce 5857 error (_("fetch_register_using_p: early buf termination"));
3f9a994c
JB
5858
5859 regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]);
5860 p += 2;
5861 }
56be3814 5862 regcache_raw_supply (regcache, reg->regnum, regp);
3f9a994c 5863 return 1;
b96ec7ac
AC
5864}
5865
74ca34ce
DJ
5866/* Fetch the registers included in the target's 'g' packet. */
5867
29709017
DJ
5868static int
5869send_g_packet (void)
c906108c 5870{
d01949b6 5871 struct remote_state *rs = get_remote_state ();
cea39f65 5872 int buf_len;
c906108c 5873
bba74b36 5874 xsnprintf (rs->buf, get_remote_packet_size (), "g");
74ca34ce 5875 remote_send (&rs->buf, &rs->buf_size);
c906108c 5876
29709017
DJ
5877 /* We can get out of synch in various cases. If the first character
5878 in the buffer is not a hex character, assume that has happened
5879 and try to fetch another packet to read. */
5880 while ((rs->buf[0] < '0' || rs->buf[0] > '9')
5881 && (rs->buf[0] < 'A' || rs->buf[0] > 'F')
5882 && (rs->buf[0] < 'a' || rs->buf[0] > 'f')
5883 && rs->buf[0] != 'x') /* New: unavailable register value. */
5884 {
5885 if (remote_debug)
5886 fprintf_unfiltered (gdb_stdlog,
5887 "Bad register packet; fetching a new packet\n");
5888 getpkt (&rs->buf, &rs->buf_size, 0);
5889 }
5890
74ca34ce
DJ
5891 buf_len = strlen (rs->buf);
5892
5893 /* Sanity check the received packet. */
5894 if (buf_len % 2 != 0)
5895 error (_("Remote 'g' packet reply is of odd length: %s"), rs->buf);
29709017
DJ
5896
5897 return buf_len / 2;
5898}
5899
5900static void
56be3814 5901process_g_packet (struct regcache *regcache)
29709017 5902{
4a22f64d 5903 struct gdbarch *gdbarch = get_regcache_arch (regcache);
29709017
DJ
5904 struct remote_state *rs = get_remote_state ();
5905 struct remote_arch_state *rsa = get_remote_arch_state ();
5906 int i, buf_len;
5907 char *p;
5908 char *regs;
5909
5910 buf_len = strlen (rs->buf);
5911
5912 /* Further sanity checks, with knowledge of the architecture. */
74ca34ce
DJ
5913 if (buf_len > 2 * rsa->sizeof_g_packet)
5914 error (_("Remote 'g' packet reply is too long: %s"), rs->buf);
5915
5916 /* Save the size of the packet sent to us by the target. It is used
5917 as a heuristic when determining the max size of packets that the
5918 target can safely receive. */
5919 if (rsa->actual_register_packet_size == 0)
5920 rsa->actual_register_packet_size = buf_len;
5921
5922 /* If this is smaller than we guessed the 'g' packet would be,
5923 update our records. A 'g' reply that doesn't include a register's
5924 value implies either that the register is not available, or that
5925 the 'p' packet must be used. */
5926 if (buf_len < 2 * rsa->sizeof_g_packet)
b323314b 5927 {
74ca34ce
DJ
5928 rsa->sizeof_g_packet = buf_len / 2;
5929
4a22f64d 5930 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
b96ec7ac 5931 {
74ca34ce
DJ
5932 if (rsa->regs[i].pnum == -1)
5933 continue;
5934
5935 if (rsa->regs[i].offset >= rsa->sizeof_g_packet)
5936 rsa->regs[i].in_g_packet = 0;
b96ec7ac 5937 else
74ca34ce 5938 rsa->regs[i].in_g_packet = 1;
b96ec7ac 5939 }
74ca34ce 5940 }
b323314b 5941
74ca34ce 5942 regs = alloca (rsa->sizeof_g_packet);
c906108c
SS
5943
5944 /* Unimplemented registers read as all bits zero. */
ea9c271d 5945 memset (regs, 0, rsa->sizeof_g_packet);
c906108c 5946
c906108c
SS
5947 /* Reply describes registers byte by byte, each byte encoded as two
5948 hex characters. Suck them all up, then supply them to the
5949 register cacheing/storage mechanism. */
5950
74ca34ce 5951 p = rs->buf;
ea9c271d 5952 for (i = 0; i < rsa->sizeof_g_packet; i++)
c906108c 5953 {
74ca34ce
DJ
5954 if (p[0] == 0 || p[1] == 0)
5955 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
5956 internal_error (__FILE__, __LINE__,
9b20d036 5957 _("unexpected end of 'g' packet reply"));
74ca34ce 5958
c906108c 5959 if (p[0] == 'x' && p[1] == 'x')
c5aa993b 5960 regs[i] = 0; /* 'x' */
c906108c
SS
5961 else
5962 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
5963 p += 2;
5964 }
5965
a744cf53
MS
5966 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
5967 {
5968 struct packet_reg *r = &rsa->regs[i];
5969
5970 if (r->in_g_packet)
5971 {
5972 if (r->offset * 2 >= strlen (rs->buf))
5973 /* This shouldn't happen - we adjusted in_g_packet above. */
5974 internal_error (__FILE__, __LINE__,
9b20d036 5975 _("unexpected end of 'g' packet reply"));
a744cf53
MS
5976 else if (rs->buf[r->offset * 2] == 'x')
5977 {
5978 gdb_assert (r->offset * 2 < strlen (rs->buf));
5979 /* The register isn't available, mark it as such (at
5980 the same time setting the value to zero). */
5981 regcache_raw_supply (regcache, r->regnum, NULL);
5982 }
5983 else
5984 regcache_raw_supply (regcache, r->regnum,
5985 regs + r->offset);
5986 }
5987 }
c906108c
SS
5988}
5989
29709017 5990static void
56be3814 5991fetch_registers_using_g (struct regcache *regcache)
29709017
DJ
5992{
5993 send_g_packet ();
56be3814 5994 process_g_packet (regcache);
29709017
DJ
5995}
5996
e6e4e701
PA
5997/* Make the remote selected traceframe match GDB's selected
5998 traceframe. */
5999
6000static void
6001set_remote_traceframe (void)
6002{
6003 int newnum;
6004
6005 if (remote_traceframe_number == get_traceframe_number ())
6006 return;
6007
6008 /* Avoid recursion, remote_trace_find calls us again. */
6009 remote_traceframe_number = get_traceframe_number ();
6010
6011 newnum = target_trace_find (tfind_number,
6012 get_traceframe_number (), 0, 0, NULL);
6013
6014 /* Should not happen. If it does, all bets are off. */
6015 if (newnum != get_traceframe_number ())
6016 warning (_("could not set remote traceframe"));
6017}
6018
74ca34ce 6019static void
28439f5e
PA
6020remote_fetch_registers (struct target_ops *ops,
6021 struct regcache *regcache, int regnum)
74ca34ce 6022{
74ca34ce
DJ
6023 struct remote_arch_state *rsa = get_remote_arch_state ();
6024 int i;
6025
e6e4e701 6026 set_remote_traceframe ();
79d7f229 6027 set_general_thread (inferior_ptid);
74ca34ce
DJ
6028
6029 if (regnum >= 0)
6030 {
6031 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
a744cf53 6032
74ca34ce
DJ
6033 gdb_assert (reg != NULL);
6034
6035 /* If this register might be in the 'g' packet, try that first -
6036 we are likely to read more than one register. If this is the
6037 first 'g' packet, we might be overly optimistic about its
6038 contents, so fall back to 'p'. */
6039 if (reg->in_g_packet)
6040 {
56be3814 6041 fetch_registers_using_g (regcache);
74ca34ce
DJ
6042 if (reg->in_g_packet)
6043 return;
6044 }
6045
56be3814 6046 if (fetch_register_using_p (regcache, reg))
74ca34ce
DJ
6047 return;
6048
6049 /* This register is not available. */
56be3814 6050 regcache_raw_supply (regcache, reg->regnum, NULL);
74ca34ce
DJ
6051
6052 return;
6053 }
6054
56be3814 6055 fetch_registers_using_g (regcache);
74ca34ce 6056
4a22f64d 6057 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
74ca34ce 6058 if (!rsa->regs[i].in_g_packet)
56be3814 6059 if (!fetch_register_using_p (regcache, &rsa->regs[i]))
74ca34ce
DJ
6060 {
6061 /* This register is not available. */
56be3814 6062 regcache_raw_supply (regcache, i, NULL);
74ca34ce
DJ
6063 }
6064}
6065
c906108c
SS
6066/* Prepare to store registers. Since we may send them all (using a
6067 'G' request), we have to read out the ones we don't want to change
6068 first. */
6069
c5aa993b 6070static void
316f2060 6071remote_prepare_to_store (struct regcache *regcache)
c906108c 6072{
ea9c271d 6073 struct remote_arch_state *rsa = get_remote_arch_state ();
cf0e1e0d 6074 int i;
cfd77fa1 6075 gdb_byte buf[MAX_REGISTER_SIZE];
cf0e1e0d 6076
c906108c 6077 /* Make sure the entire registers array is valid. */
444abaca 6078 switch (remote_protocol_packets[PACKET_P].support)
5a2468f5
JM
6079 {
6080 case PACKET_DISABLE:
6081 case PACKET_SUPPORT_UNKNOWN:
cf0e1e0d 6082 /* Make sure all the necessary registers are cached. */
4a22f64d 6083 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
ea9c271d 6084 if (rsa->regs[i].in_g_packet)
316f2060 6085 regcache_raw_read (regcache, rsa->regs[i].regnum, buf);
5a2468f5
JM
6086 break;
6087 case PACKET_ENABLE:
6088 break;
6089 }
6090}
6091
ad10f812 6092/* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
23860348 6093 packet was not recognized. */
5a2468f5
JM
6094
6095static int
1f4437a4
MS
6096store_register_using_P (const struct regcache *regcache,
6097 struct packet_reg *reg)
5a2468f5 6098{
4a22f64d 6099 struct gdbarch *gdbarch = get_regcache_arch (regcache);
d01949b6 6100 struct remote_state *rs = get_remote_state ();
5a2468f5 6101 /* Try storing a single register. */
6d820c5c 6102 char *buf = rs->buf;
cfd77fa1 6103 gdb_byte regp[MAX_REGISTER_SIZE];
5a2468f5 6104 char *p;
5a2468f5 6105
74ca34ce
DJ
6106 if (remote_protocol_packets[PACKET_P].support == PACKET_DISABLE)
6107 return 0;
6108
6109 if (reg->pnum == -1)
6110 return 0;
6111
ea9c271d 6112 xsnprintf (buf, get_remote_packet_size (), "P%s=", phex_nz (reg->pnum, 0));
5a2468f5 6113 p = buf + strlen (buf);
56be3814 6114 regcache_raw_collect (regcache, reg->regnum, regp);
4a22f64d 6115 bin2hex (regp, p, register_size (gdbarch, reg->regnum));
1f4437a4
MS
6116 putpkt (rs->buf);
6117 getpkt (&rs->buf, &rs->buf_size, 0);
5a2468f5 6118
74ca34ce
DJ
6119 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_P]))
6120 {
6121 case PACKET_OK:
6122 return 1;
6123 case PACKET_ERROR:
27a9c0bf
MS
6124 error (_("Could not write register \"%s\"; remote failure reply '%s'"),
6125 gdbarch_register_name (gdbarch, reg->regnum), rs->buf);
74ca34ce
DJ
6126 case PACKET_UNKNOWN:
6127 return 0;
6128 default:
6129 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
6130 }
c906108c
SS
6131}
6132
23860348
MS
6133/* Store register REGNUM, or all registers if REGNUM == -1, from the
6134 contents of the register cache buffer. FIXME: ignores errors. */
c906108c
SS
6135
6136static void
56be3814 6137store_registers_using_G (const struct regcache *regcache)
c906108c 6138{
d01949b6 6139 struct remote_state *rs = get_remote_state ();
ea9c271d 6140 struct remote_arch_state *rsa = get_remote_arch_state ();
cfd77fa1 6141 gdb_byte *regs;
c906108c
SS
6142 char *p;
6143
193cb69f
AC
6144 /* Extract all the registers in the regcache copying them into a
6145 local buffer. */
6146 {
b323314b 6147 int i;
a744cf53 6148
ea9c271d
DJ
6149 regs = alloca (rsa->sizeof_g_packet);
6150 memset (regs, 0, rsa->sizeof_g_packet);
4a22f64d 6151 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
193cb69f 6152 {
ea9c271d 6153 struct packet_reg *r = &rsa->regs[i];
a744cf53 6154
b323314b 6155 if (r->in_g_packet)
56be3814 6156 regcache_raw_collect (regcache, r->regnum, regs + r->offset);
193cb69f
AC
6157 }
6158 }
c906108c
SS
6159
6160 /* Command describes registers byte by byte,
6161 each byte encoded as two hex characters. */
6d820c5c 6162 p = rs->buf;
193cb69f 6163 *p++ = 'G';
74ca34ce
DJ
6164 /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets
6165 updated. */
6166 bin2hex (regs, p, rsa->sizeof_g_packet);
1f4437a4
MS
6167 putpkt (rs->buf);
6168 getpkt (&rs->buf, &rs->buf_size, 0);
6169 if (packet_check_result (rs->buf) == PACKET_ERROR)
27a9c0bf
MS
6170 error (_("Could not write registers; remote failure reply '%s'"),
6171 rs->buf);
c906108c 6172}
74ca34ce
DJ
6173
6174/* Store register REGNUM, or all registers if REGNUM == -1, from the contents
6175 of the register cache buffer. FIXME: ignores errors. */
6176
6177static void
28439f5e
PA
6178remote_store_registers (struct target_ops *ops,
6179 struct regcache *regcache, int regnum)
74ca34ce 6180{
74ca34ce
DJ
6181 struct remote_arch_state *rsa = get_remote_arch_state ();
6182 int i;
6183
e6e4e701 6184 set_remote_traceframe ();
79d7f229 6185 set_general_thread (inferior_ptid);
74ca34ce
DJ
6186
6187 if (regnum >= 0)
6188 {
6189 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
a744cf53 6190
74ca34ce
DJ
6191 gdb_assert (reg != NULL);
6192
6193 /* Always prefer to store registers using the 'P' packet if
6194 possible; we often change only a small number of registers.
6195 Sometimes we change a larger number; we'd need help from a
6196 higher layer to know to use 'G'. */
56be3814 6197 if (store_register_using_P (regcache, reg))
74ca34ce
DJ
6198 return;
6199
6200 /* For now, don't complain if we have no way to write the
6201 register. GDB loses track of unavailable registers too
6202 easily. Some day, this may be an error. We don't have
0df8b418 6203 any way to read the register, either... */
74ca34ce
DJ
6204 if (!reg->in_g_packet)
6205 return;
6206
56be3814 6207 store_registers_using_G (regcache);
74ca34ce
DJ
6208 return;
6209 }
6210
56be3814 6211 store_registers_using_G (regcache);
74ca34ce 6212
4a22f64d 6213 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
74ca34ce 6214 if (!rsa->regs[i].in_g_packet)
56be3814 6215 if (!store_register_using_P (regcache, &rsa->regs[i]))
74ca34ce
DJ
6216 /* See above for why we do not issue an error here. */
6217 continue;
6218}
c906108c
SS
6219\f
6220
6221/* Return the number of hex digits in num. */
6222
6223static int
fba45db2 6224hexnumlen (ULONGEST num)
c906108c
SS
6225{
6226 int i;
6227
6228 for (i = 0; num != 0; i++)
6229 num >>= 4;
6230
6231 return max (i, 1);
6232}
6233
2df3850c 6234/* Set BUF to the minimum number of hex digits representing NUM. */
c906108c
SS
6235
6236static int
fba45db2 6237hexnumstr (char *buf, ULONGEST num)
c906108c 6238{
c906108c 6239 int len = hexnumlen (num);
a744cf53 6240
2df3850c
JM
6241 return hexnumnstr (buf, num, len);
6242}
6243
c906108c 6244
2df3850c 6245/* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
c906108c 6246
2df3850c 6247static int
fba45db2 6248hexnumnstr (char *buf, ULONGEST num, int width)
2df3850c
JM
6249{
6250 int i;
6251
6252 buf[width] = '\0';
6253
6254 for (i = width - 1; i >= 0; i--)
c906108c 6255 {
c5aa993b 6256 buf[i] = "0123456789abcdef"[(num & 0xf)];
c906108c
SS
6257 num >>= 4;
6258 }
6259
2df3850c 6260 return width;
c906108c
SS
6261}
6262
23860348 6263/* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
c906108c
SS
6264
6265static CORE_ADDR
fba45db2 6266remote_address_masked (CORE_ADDR addr)
c906108c 6267{
911c95a5 6268 int address_size = remote_address_size;
a744cf53 6269
911c95a5
UW
6270 /* If "remoteaddresssize" was not set, default to target address size. */
6271 if (!address_size)
1cf3db46 6272 address_size = gdbarch_addr_bit (target_gdbarch);
911c95a5
UW
6273
6274 if (address_size > 0
6275 && address_size < (sizeof (ULONGEST) * 8))
c906108c
SS
6276 {
6277 /* Only create a mask when that mask can safely be constructed
23860348 6278 in a ULONGEST variable. */
c906108c 6279 ULONGEST mask = 1;
a744cf53 6280
911c95a5 6281 mask = (mask << address_size) - 1;
c906108c
SS
6282 addr &= mask;
6283 }
6284 return addr;
6285}
6286
a31ea83d
DJ
6287/* Convert BUFFER, binary data at least LEN bytes long, into escaped
6288 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
6289 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
6290 (which may be more than *OUT_LEN due to escape characters). The
6291 total number of bytes in the output buffer will be at most
6292 OUT_MAXLEN. */
6293
6294static int
6295remote_escape_output (const gdb_byte *buffer, int len,
6296 gdb_byte *out_buf, int *out_len,
6297 int out_maxlen)
6298{
6299 int input_index, output_index;
6300
6301 output_index = 0;
6302 for (input_index = 0; input_index < len; input_index++)
6303 {
6304 gdb_byte b = buffer[input_index];
6305
6306 if (b == '$' || b == '#' || b == '}')
6307 {
6308 /* These must be escaped. */
6309 if (output_index + 2 > out_maxlen)
6310 break;
6311 out_buf[output_index++] = '}';
6312 out_buf[output_index++] = b ^ 0x20;
6313 }
6314 else
6315 {
6316 if (output_index + 1 > out_maxlen)
6317 break;
6318 out_buf[output_index++] = b;
6319 }
6320 }
6321
6322 *out_len = input_index;
6323 return output_index;
6324}
6325
0876f84a
DJ
6326/* Convert BUFFER, escaped data LEN bytes long, into binary data
6327 in OUT_BUF. Return the number of bytes written to OUT_BUF.
6328 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
6329
6330 This function reverses remote_escape_output. It allows more
6331 escaped characters than that function does, in particular because
6332 '*' must be escaped to avoid the run-length encoding processing
6333 in reading packets. */
6334
6335static int
6336remote_unescape_input (const gdb_byte *buffer, int len,
6337 gdb_byte *out_buf, int out_maxlen)
6338{
6339 int input_index, output_index;
6340 int escaped;
6341
6342 output_index = 0;
6343 escaped = 0;
6344 for (input_index = 0; input_index < len; input_index++)
6345 {
6346 gdb_byte b = buffer[input_index];
6347
6348 if (output_index + 1 > out_maxlen)
6349 {
6350 warning (_("Received too much data from remote target;"
6351 " ignoring overflow."));
6352 return output_index;
6353 }
6354
6355 if (escaped)
6356 {
6357 out_buf[output_index++] = b ^ 0x20;
6358 escaped = 0;
6359 }
6360 else if (b == '}')
6361 escaped = 1;
6362 else
6363 out_buf[output_index++] = b;
6364 }
6365
6366 if (escaped)
6367 error (_("Unmatched escape character in target response."));
6368
6369 return output_index;
6370}
6371
c906108c
SS
6372/* Determine whether the remote target supports binary downloading.
6373 This is accomplished by sending a no-op memory write of zero length
6374 to the target at the specified address. It does not suffice to send
23860348
MS
6375 the whole packet, since many stubs strip the eighth bit and
6376 subsequently compute a wrong checksum, which causes real havoc with
6377 remote_write_bytes.
7a292a7a 6378
96baa820 6379 NOTE: This can still lose if the serial line is not eight-bit
0df8b418 6380 clean. In cases like this, the user should clear "remote
23860348 6381 X-packet". */
96baa820 6382
c906108c 6383static void
fba45db2 6384check_binary_download (CORE_ADDR addr)
c906108c 6385{
d01949b6 6386 struct remote_state *rs = get_remote_state ();
24b06219 6387
444abaca 6388 switch (remote_protocol_packets[PACKET_X].support)
c906108c 6389 {
96baa820
JM
6390 case PACKET_DISABLE:
6391 break;
6392 case PACKET_ENABLE:
6393 break;
6394 case PACKET_SUPPORT_UNKNOWN:
6395 {
96baa820 6396 char *p;
802188a7 6397
2e9f7625 6398 p = rs->buf;
96baa820
JM
6399 *p++ = 'X';
6400 p += hexnumstr (p, (ULONGEST) addr);
6401 *p++ = ',';
6402 p += hexnumstr (p, (ULONGEST) 0);
6403 *p++ = ':';
6404 *p = '\0';
802188a7 6405
2e9f7625 6406 putpkt_binary (rs->buf, (int) (p - rs->buf));
6d820c5c 6407 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 6408
2e9f7625 6409 if (rs->buf[0] == '\0')
96baa820
JM
6410 {
6411 if (remote_debug)
6412 fprintf_unfiltered (gdb_stdlog,
3e43a32a
MS
6413 "binary downloading NOT "
6414 "supported by target\n");
444abaca 6415 remote_protocol_packets[PACKET_X].support = PACKET_DISABLE;
96baa820
JM
6416 }
6417 else
6418 {
6419 if (remote_debug)
6420 fprintf_unfiltered (gdb_stdlog,
64b9b334 6421 "binary downloading supported by target\n");
444abaca 6422 remote_protocol_packets[PACKET_X].support = PACKET_ENABLE;
96baa820
JM
6423 }
6424 break;
6425 }
c906108c
SS
6426 }
6427}
6428
6429/* Write memory data directly to the remote machine.
6430 This does not inform the data cache; the data cache uses this.
a76d924d 6431 HEADER is the starting part of the packet.
c906108c
SS
6432 MEMADDR is the address in the remote memory space.
6433 MYADDR is the address of the buffer in our space.
6434 LEN is the number of bytes.
a76d924d
DJ
6435 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
6436 should send data as binary ('X'), or hex-encoded ('M').
6437
6438 The function creates packet of the form
6439 <HEADER><ADDRESS>,<LENGTH>:<DATA>
6440
6441 where encoding of <DATA> is termined by PACKET_FORMAT.
6442
6443 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
6444 are omitted.
6445
6446 Returns the number of bytes transferred, or 0 (setting errno) for
23860348 6447 error. Only transfer a single packet. */
c906108c 6448
a76d924d
DJ
6449static int
6450remote_write_bytes_aux (const char *header, CORE_ADDR memaddr,
6451 const gdb_byte *myaddr, int len,
6452 char packet_format, int use_length)
c906108c 6453{
6d820c5c 6454 struct remote_state *rs = get_remote_state ();
cfd77fa1 6455 char *p;
a76d924d
DJ
6456 char *plen = NULL;
6457 int plenlen = 0;
917317f4
JM
6458 int todo;
6459 int nr_bytes;
a257b5bb 6460 int payload_size;
6765f3e5 6461 int payload_length;
a76d924d
DJ
6462 int header_length;
6463
6464 if (packet_format != 'X' && packet_format != 'M')
6465 internal_error (__FILE__, __LINE__,
9b20d036 6466 _("remote_write_bytes_aux: bad packet format"));
c906108c 6467
b2182ed2
DJ
6468 if (len <= 0)
6469 return 0;
6470
3de11b2e 6471 payload_size = get_memory_write_packet_size ();
2bc416ba 6472
6d820c5c
DJ
6473 /* The packet buffer will be large enough for the payload;
6474 get_memory_packet_size ensures this. */
a76d924d 6475 rs->buf[0] = '\0';
c906108c 6476
a257b5bb 6477 /* Compute the size of the actual payload by subtracting out the
0df8b418
MS
6478 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
6479
a76d924d
DJ
6480 payload_size -= strlen ("$,:#NN");
6481 if (!use_length)
0df8b418 6482 /* The comma won't be used. */
a76d924d
DJ
6483 payload_size += 1;
6484 header_length = strlen (header);
6485 payload_size -= header_length;
3de11b2e 6486 payload_size -= hexnumlen (memaddr);
c906108c 6487
a76d924d 6488 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
917317f4 6489
a76d924d
DJ
6490 strcat (rs->buf, header);
6491 p = rs->buf + strlen (header);
6492
6493 /* Compute a best guess of the number of bytes actually transfered. */
6494 if (packet_format == 'X')
c906108c 6495 {
23860348 6496 /* Best guess at number of bytes that will fit. */
a257b5bb 6497 todo = min (len, payload_size);
a76d924d
DJ
6498 if (use_length)
6499 payload_size -= hexnumlen (todo);
3de11b2e 6500 todo = min (todo, payload_size);
a76d924d
DJ
6501 }
6502 else
6503 {
23860348 6504 /* Num bytes that will fit. */
a257b5bb 6505 todo = min (len, payload_size / 2);
a76d924d
DJ
6506 if (use_length)
6507 payload_size -= hexnumlen (todo);
3de11b2e 6508 todo = min (todo, payload_size / 2);
917317f4 6509 }
a76d924d 6510
3de11b2e
NS
6511 if (todo <= 0)
6512 internal_error (__FILE__, __LINE__,
405f8e94 6513 _("minimum packet size too small to write data"));
802188a7 6514
6765f3e5
DJ
6515 /* If we already need another packet, then try to align the end
6516 of this packet to a useful boundary. */
6517 if (todo > 2 * REMOTE_ALIGN_WRITES && todo < len)
6518 todo = ((memaddr + todo) & ~(REMOTE_ALIGN_WRITES - 1)) - memaddr;
6519
a257b5bb 6520 /* Append "<memaddr>". */
917317f4
JM
6521 memaddr = remote_address_masked (memaddr);
6522 p += hexnumstr (p, (ULONGEST) memaddr);
a257b5bb 6523
a76d924d
DJ
6524 if (use_length)
6525 {
6526 /* Append ",". */
6527 *p++ = ',';
802188a7 6528
a76d924d
DJ
6529 /* Append <len>. Retain the location/size of <len>. It may need to
6530 be adjusted once the packet body has been created. */
6531 plen = p;
6532 plenlen = hexnumstr (p, (ULONGEST) todo);
6533 p += plenlen;
6534 }
a257b5bb
AC
6535
6536 /* Append ":". */
917317f4
JM
6537 *p++ = ':';
6538 *p = '\0';
802188a7 6539
a257b5bb 6540 /* Append the packet body. */
a76d924d 6541 if (packet_format == 'X')
917317f4 6542 {
917317f4
JM
6543 /* Binary mode. Send target system values byte by byte, in
6544 increasing byte addresses. Only escape certain critical
6545 characters. */
6765f3e5
DJ
6546 payload_length = remote_escape_output (myaddr, todo, p, &nr_bytes,
6547 payload_size);
6548
6549 /* If not all TODO bytes fit, then we'll need another packet. Make
9b7194bc
DJ
6550 a second try to keep the end of the packet aligned. Don't do
6551 this if the packet is tiny. */
6552 if (nr_bytes < todo && nr_bytes > 2 * REMOTE_ALIGN_WRITES)
6765f3e5
DJ
6553 {
6554 int new_nr_bytes;
6555
6556 new_nr_bytes = (((memaddr + nr_bytes) & ~(REMOTE_ALIGN_WRITES - 1))
6557 - memaddr);
6558 if (new_nr_bytes != nr_bytes)
6559 payload_length = remote_escape_output (myaddr, new_nr_bytes,
6560 p, &nr_bytes,
6561 payload_size);
6562 }
6563
6564 p += payload_length;
a76d924d 6565 if (use_length && nr_bytes < todo)
c906108c 6566 {
802188a7 6567 /* Escape chars have filled up the buffer prematurely,
917317f4
JM
6568 and we have actually sent fewer bytes than planned.
6569 Fix-up the length field of the packet. Use the same
6570 number of characters as before. */
917317f4
JM
6571 plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen);
6572 *plen = ':'; /* overwrite \0 from hexnumnstr() */
c906108c 6573 }
a76d924d
DJ
6574 }
6575 else
6576 {
917317f4
JM
6577 /* Normal mode: Send target system values byte by byte, in
6578 increasing byte addresses. Each byte is encoded as a two hex
6579 value. */
2644f393 6580 nr_bytes = bin2hex (myaddr, p, todo);
aa6c0017 6581 p += 2 * nr_bytes;
c906108c 6582 }
802188a7 6583
2e9f7625 6584 putpkt_binary (rs->buf, (int) (p - rs->buf));
6d820c5c 6585 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 6586
2e9f7625 6587 if (rs->buf[0] == 'E')
917317f4
JM
6588 {
6589 /* There is no correspondance between what the remote protocol
6590 uses for errors and errno codes. We would like a cleaner way
6591 of representing errors (big enough to include errno codes,
6592 bfd_error codes, and others). But for now just return EIO. */
6593 errno = EIO;
6594 return 0;
6595 }
802188a7 6596
23860348
MS
6597 /* Return NR_BYTES, not TODO, in case escape chars caused us to send
6598 fewer bytes than we'd planned. */
917317f4 6599 return nr_bytes;
c906108c
SS
6600}
6601
a76d924d
DJ
6602/* Write memory data directly to the remote machine.
6603 This does not inform the data cache; the data cache uses this.
6604 MEMADDR is the address in the remote memory space.
6605 MYADDR is the address of the buffer in our space.
6606 LEN is the number of bytes.
6607
6608 Returns number of bytes transferred, or 0 (setting errno) for
6609 error. Only transfer a single packet. */
6610
f7605bc2 6611static int
a76d924d
DJ
6612remote_write_bytes (CORE_ADDR memaddr, const gdb_byte *myaddr, int len)
6613{
6614 char *packet_format = 0;
6615
6616 /* Check whether the target supports binary download. */
6617 check_binary_download (memaddr);
6618
6619 switch (remote_protocol_packets[PACKET_X].support)
6620 {
6621 case PACKET_ENABLE:
6622 packet_format = "X";
6623 break;
6624 case PACKET_DISABLE:
6625 packet_format = "M";
6626 break;
6627 case PACKET_SUPPORT_UNKNOWN:
6628 internal_error (__FILE__, __LINE__,
6629 _("remote_write_bytes: bad internal state"));
6630 default:
6631 internal_error (__FILE__, __LINE__, _("bad switch"));
6632 }
6633
6634 return remote_write_bytes_aux (packet_format,
6635 memaddr, myaddr, len, packet_format[0], 1);
6636}
6637
c906108c
SS
6638/* Read memory data directly from the remote machine.
6639 This does not use the data cache; the data cache uses this.
6640 MEMADDR is the address in the remote memory space.
6641 MYADDR is the address of the buffer in our space.
6642 LEN is the number of bytes.
6643
6644 Returns number of bytes transferred, or 0 for error. */
6645
f7605bc2 6646static int
cfd77fa1 6647remote_read_bytes (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
c906108c 6648{
6d820c5c 6649 struct remote_state *rs = get_remote_state ();
23860348 6650 int max_buf_size; /* Max size of packet output buffer. */
f7605bc2
PA
6651 char *p;
6652 int todo;
6653 int i;
c906108c 6654
b2182ed2
DJ
6655 if (len <= 0)
6656 return 0;
6657
11cf8741 6658 max_buf_size = get_memory_read_packet_size ();
6d820c5c
DJ
6659 /* The packet buffer will be large enough for the payload;
6660 get_memory_packet_size ensures this. */
c906108c 6661
f7605bc2
PA
6662 /* Number if bytes that will fit. */
6663 todo = min (len, max_buf_size / 2);
c906108c 6664
f7605bc2
PA
6665 /* Construct "m"<memaddr>","<len>". */
6666 memaddr = remote_address_masked (memaddr);
6667 p = rs->buf;
6668 *p++ = 'm';
6669 p += hexnumstr (p, (ULONGEST) memaddr);
6670 *p++ = ',';
6671 p += hexnumstr (p, (ULONGEST) todo);
6672 *p = '\0';
6673 putpkt (rs->buf);
6674 getpkt (&rs->buf, &rs->buf_size, 0);
6675 if (rs->buf[0] == 'E'
6676 && isxdigit (rs->buf[1]) && isxdigit (rs->buf[2])
6677 && rs->buf[3] == '\0')
6678 {
6679 /* There is no correspondance between what the remote protocol
6680 uses for errors and errno codes. We would like a cleaner way
6681 of representing errors (big enough to include errno codes,
6682 bfd_error codes, and others). But for now just return
6683 EIO. */
6684 errno = EIO;
6685 return 0;
c906108c 6686 }
f7605bc2
PA
6687 /* Reply describes memory byte by byte, each byte encoded as two hex
6688 characters. */
6689 p = rs->buf;
6690 i = hex2bin (p, myaddr, todo);
6691 /* Return what we have. Let higher layers handle partial reads. */
6692 return i;
c906108c 6693}
74531fed
PA
6694\f
6695
6696/* Remote notification handler. */
6697
6698static void
6699handle_notification (char *buf, size_t length)
6700{
6701 if (strncmp (buf, "Stop:", 5) == 0)
6702 {
6703 if (pending_stop_reply)
0723dbf5
PA
6704 {
6705 /* We've already parsed the in-flight stop-reply, but the
6706 stub for some reason thought we didn't, possibly due to
6707 timeout on its side. Just ignore it. */
6708 if (remote_debug)
6709 fprintf_unfiltered (gdb_stdlog, "ignoring resent notification\n");
6710 }
74531fed
PA
6711 else
6712 {
6713 struct cleanup *old_chain;
6714 struct stop_reply *reply = stop_reply_xmalloc ();
a744cf53 6715
74531fed
PA
6716 old_chain = make_cleanup (do_stop_reply_xfree, reply);
6717
6718 remote_parse_stop_reply (buf + 5, reply);
6719
6720 discard_cleanups (old_chain);
6721
6722 /* Be careful to only set it after parsing, since an error
6723 may be thrown then. */
6724 pending_stop_reply = reply;
6725
6726 /* Notify the event loop there's a stop reply to acknowledge
6727 and that there may be more events to fetch. */
6728 mark_async_event_handler (remote_async_get_pending_events_token);
0723dbf5
PA
6729
6730 if (remote_debug)
6731 fprintf_unfiltered (gdb_stdlog, "stop notification captured\n");
74531fed
PA
6732 }
6733 }
6734 else
6735 /* We ignore notifications we don't recognize, for compatibility
6736 with newer stubs. */
6737 ;
6738}
6739
c906108c
SS
6740\f
6741/* Read or write LEN bytes from inferior memory at MEMADDR,
23860348
MS
6742 transferring to or from debugger address BUFFER. Write to inferior
6743 if SHOULD_WRITE is nonzero. Returns length of data written or
6744 read; 0 for error. TARGET is unused. */
392a587b 6745
c906108c 6746static int
961cb7b5 6747remote_xfer_memory (CORE_ADDR mem_addr, gdb_byte *buffer, int mem_len,
0a65a603 6748 int should_write, struct mem_attrib *attrib,
29e57380 6749 struct target_ops *target)
c906108c 6750{
4930751a
C
6751 int res;
6752
e6e4e701 6753 set_remote_traceframe ();
82f73884
PA
6754 set_general_thread (inferior_ptid);
6755
4930751a 6756 if (should_write)
b2182ed2 6757 res = remote_write_bytes (mem_addr, buffer, mem_len);
4930751a 6758 else
b2182ed2 6759 res = remote_read_bytes (mem_addr, buffer, mem_len);
4930751a
C
6760
6761 return res;
c906108c
SS
6762}
6763
a76d924d
DJ
6764/* Sends a packet with content determined by the printf format string
6765 FORMAT and the remaining arguments, then gets the reply. Returns
6766 whether the packet was a success, a failure, or unknown. */
6767
2c0b251b 6768static enum packet_result
a76d924d
DJ
6769remote_send_printf (const char *format, ...)
6770{
6771 struct remote_state *rs = get_remote_state ();
6772 int max_size = get_remote_packet_size ();
a76d924d 6773 va_list ap;
a744cf53 6774
a76d924d
DJ
6775 va_start (ap, format);
6776
6777 rs->buf[0] = '\0';
6778 if (vsnprintf (rs->buf, max_size, format, ap) >= max_size)
9b20d036 6779 internal_error (__FILE__, __LINE__, _("Too long remote packet."));
a76d924d
DJ
6780
6781 if (putpkt (rs->buf) < 0)
6782 error (_("Communication problem with target."));
6783
6784 rs->buf[0] = '\0';
6785 getpkt (&rs->buf, &rs->buf_size, 0);
6786
6787 return packet_check_result (rs->buf);
6788}
6789
6790static void
6791restore_remote_timeout (void *p)
6792{
6793 int value = *(int *)p;
a744cf53 6794
a76d924d
DJ
6795 remote_timeout = value;
6796}
6797
6798/* Flash writing can take quite some time. We'll set
6799 effectively infinite timeout for flash operations.
6800 In future, we'll need to decide on a better approach. */
6801static const int remote_flash_timeout = 1000;
6802
6803static void
6804remote_flash_erase (struct target_ops *ops,
6805 ULONGEST address, LONGEST length)
6806{
5af949e3 6807 int addr_size = gdbarch_addr_bit (target_gdbarch) / 8;
a76d924d
DJ
6808 int saved_remote_timeout = remote_timeout;
6809 enum packet_result ret;
a76d924d
DJ
6810 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
6811 &saved_remote_timeout);
a744cf53 6812
a76d924d
DJ
6813 remote_timeout = remote_flash_timeout;
6814
6815 ret = remote_send_printf ("vFlashErase:%s,%s",
5af949e3 6816 phex (address, addr_size),
a76d924d
DJ
6817 phex (length, 4));
6818 switch (ret)
6819 {
6820 case PACKET_UNKNOWN:
6821 error (_("Remote target does not support flash erase"));
6822 case PACKET_ERROR:
6823 error (_("Error erasing flash with vFlashErase packet"));
6824 default:
6825 break;
6826 }
6827
6828 do_cleanups (back_to);
6829}
6830
6831static LONGEST
6832remote_flash_write (struct target_ops *ops,
6833 ULONGEST address, LONGEST length,
6834 const gdb_byte *data)
6835{
6836 int saved_remote_timeout = remote_timeout;
6837 int ret;
6838 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
6839 &saved_remote_timeout);
6840
6841 remote_timeout = remote_flash_timeout;
6842 ret = remote_write_bytes_aux ("vFlashWrite:", address, data, length, 'X', 0);
6843 do_cleanups (back_to);
6844
6845 return ret;
6846}
6847
6848static void
6849remote_flash_done (struct target_ops *ops)
6850{
6851 int saved_remote_timeout = remote_timeout;
6852 int ret;
6853 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
6854 &saved_remote_timeout);
6855
6856 remote_timeout = remote_flash_timeout;
6857 ret = remote_send_printf ("vFlashDone");
6858 do_cleanups (back_to);
6859
6860 switch (ret)
6861 {
6862 case PACKET_UNKNOWN:
6863 error (_("Remote target does not support vFlashDone"));
6864 case PACKET_ERROR:
6865 error (_("Error finishing flash operation"));
6866 default:
6867 break;
6868 }
6869}
6870
c906108c 6871static void
fba45db2 6872remote_files_info (struct target_ops *ignore)
c906108c
SS
6873{
6874 puts_filtered ("Debugging a target over a serial line.\n");
6875}
6876\f
6877/* Stuff for dealing with the packets which are part of this protocol.
6878 See comment at top of file for details. */
6879
0876f84a 6880/* Read a single character from the remote end. */
c906108c
SS
6881
6882static int
fba45db2 6883readchar (int timeout)
c906108c
SS
6884{
6885 int ch;
6886
2cd58942 6887 ch = serial_readchar (remote_desc, timeout);
c906108c 6888
2acceee2 6889 if (ch >= 0)
0876f84a 6890 return ch;
2acceee2
JM
6891
6892 switch ((enum serial_rc) ch)
c906108c
SS
6893 {
6894 case SERIAL_EOF:
ce5ce7ed 6895 pop_target ();
8a3fe4f8 6896 error (_("Remote connection closed"));
2acceee2 6897 /* no return */
c906108c 6898 case SERIAL_ERROR:
4ac8c4da 6899 pop_target ();
3e43a32a
MS
6900 perror_with_name (_("Remote communication error. "
6901 "Target disconnected."));
2acceee2 6902 /* no return */
c906108c 6903 case SERIAL_TIMEOUT:
2acceee2 6904 break;
c906108c 6905 }
2acceee2 6906 return ch;
c906108c
SS
6907}
6908
6d820c5c
DJ
6909/* Send the command in *BUF to the remote machine, and read the reply
6910 into *BUF. Report an error if we get an error reply. Resize
6911 *BUF using xrealloc if necessary to hold the result, and update
6912 *SIZEOF_BUF. */
c906108c
SS
6913
6914static void
6d820c5c
DJ
6915remote_send (char **buf,
6916 long *sizeof_buf)
c906108c 6917{
6d820c5c 6918 putpkt (*buf);
c2d11a7d 6919 getpkt (buf, sizeof_buf, 0);
c906108c 6920
6d820c5c
DJ
6921 if ((*buf)[0] == 'E')
6922 error (_("Remote failure reply: %s"), *buf);
c906108c
SS
6923}
6924
6e5abd65
PA
6925/* Return a pointer to an xmalloc'ed string representing an escaped
6926 version of BUF, of len N. E.g. \n is converted to \\n, \t to \\t,
6927 etc. The caller is responsible for releasing the returned
6928 memory. */
6929
6930static char *
6931escape_buffer (const char *buf, int n)
6932{
6933 struct cleanup *old_chain;
6934 struct ui_file *stb;
6935 char *str;
6e5abd65
PA
6936
6937 stb = mem_fileopen ();
6938 old_chain = make_cleanup_ui_file_delete (stb);
6939
6940 fputstrn_unfiltered (buf, n, 0, stb);
759ef836 6941 str = ui_file_xstrdup (stb, NULL);
6e5abd65
PA
6942 do_cleanups (old_chain);
6943 return str;
6944}
6945
c906108c
SS
6946/* Display a null-terminated packet on stdout, for debugging, using C
6947 string notation. */
6948
6949static void
fba45db2 6950print_packet (char *buf)
c906108c
SS
6951{
6952 puts_filtered ("\"");
43e526b9 6953 fputstr_filtered (buf, '"', gdb_stdout);
c906108c
SS
6954 puts_filtered ("\"");
6955}
6956
6957int
fba45db2 6958putpkt (char *buf)
c906108c
SS
6959{
6960 return putpkt_binary (buf, strlen (buf));
6961}
6962
6963/* Send a packet to the remote machine, with error checking. The data
23860348 6964 of the packet is in BUF. The string in BUF can be at most
ea9c271d 6965 get_remote_packet_size () - 5 to account for the $, # and checksum,
23860348
MS
6966 and for a possible /0 if we are debugging (remote_debug) and want
6967 to print the sent packet as a string. */
c906108c
SS
6968
6969static int
fba45db2 6970putpkt_binary (char *buf, int cnt)
c906108c 6971{
2d717e4f 6972 struct remote_state *rs = get_remote_state ();
c906108c
SS
6973 int i;
6974 unsigned char csum = 0;
11cf8741 6975 char *buf2 = alloca (cnt + 6);
085dd6e6 6976
c906108c
SS
6977 int ch;
6978 int tcount = 0;
6979 char *p;
6980
e24a49d8
PA
6981 /* Catch cases like trying to read memory or listing threads while
6982 we're waiting for a stop reply. The remote server wouldn't be
6983 ready to handle this request, so we'd hang and timeout. We don't
6984 have to worry about this in synchronous mode, because in that
6985 case it's not possible to issue a command while the target is
74531fed
PA
6986 running. This is not a problem in non-stop mode, because in that
6987 case, the stub is always ready to process serial input. */
6988 if (!non_stop && target_can_async_p () && rs->waiting_for_stop_reply)
e24a49d8
PA
6989 error (_("Cannot execute this command while the target is running."));
6990
2d717e4f
DJ
6991 /* We're sending out a new packet. Make sure we don't look at a
6992 stale cached response. */
6993 rs->cached_wait_status = 0;
6994
c906108c
SS
6995 /* Copy the packet into buffer BUF2, encapsulating it
6996 and giving it a checksum. */
6997
c906108c
SS
6998 p = buf2;
6999 *p++ = '$';
7000
7001 for (i = 0; i < cnt; i++)
7002 {
7003 csum += buf[i];
7004 *p++ = buf[i];
7005 }
7006 *p++ = '#';
7007 *p++ = tohex ((csum >> 4) & 0xf);
7008 *p++ = tohex (csum & 0xf);
7009
7010 /* Send it over and over until we get a positive ack. */
7011
7012 while (1)
7013 {
7014 int started_error_output = 0;
7015
7016 if (remote_debug)
7017 {
6e5abd65
PA
7018 struct cleanup *old_chain;
7019 char *str;
7020
c906108c 7021 *p = '\0';
6e5abd65
PA
7022 str = escape_buffer (buf2, p - buf2);
7023 old_chain = make_cleanup (xfree, str);
7024 fprintf_unfiltered (gdb_stdlog, "Sending packet: %s...", str);
0f71a2f6 7025 gdb_flush (gdb_stdlog);
6e5abd65 7026 do_cleanups (old_chain);
c906108c 7027 }
2cd58942 7028 if (serial_write (remote_desc, buf2, p - buf2))
e2e0b3e5 7029 perror_with_name (_("putpkt: write failed"));
c906108c 7030
a6f3e723
SL
7031 /* If this is a no acks version of the remote protocol, send the
7032 packet and move on. */
7033 if (rs->noack_mode)
7034 break;
7035
74531fed
PA
7036 /* Read until either a timeout occurs (-2) or '+' is read.
7037 Handle any notification that arrives in the mean time. */
c906108c
SS
7038 while (1)
7039 {
7040 ch = readchar (remote_timeout);
7041
c5aa993b 7042 if (remote_debug)
c906108c
SS
7043 {
7044 switch (ch)
7045 {
7046 case '+':
1216fa2c 7047 case '-':
c906108c
SS
7048 case SERIAL_TIMEOUT:
7049 case '$':
74531fed 7050 case '%':
c906108c
SS
7051 if (started_error_output)
7052 {
7053 putchar_unfiltered ('\n');
7054 started_error_output = 0;
7055 }
7056 }
7057 }
7058
7059 switch (ch)
7060 {
7061 case '+':
7062 if (remote_debug)
0f71a2f6 7063 fprintf_unfiltered (gdb_stdlog, "Ack\n");
c906108c 7064 return 1;
1216fa2c
AC
7065 case '-':
7066 if (remote_debug)
7067 fprintf_unfiltered (gdb_stdlog, "Nak\n");
a17d146e 7068 /* FALLTHROUGH */
c906108c 7069 case SERIAL_TIMEOUT:
c5aa993b 7070 tcount++;
c906108c
SS
7071 if (tcount > 3)
7072 return 0;
23860348 7073 break; /* Retransmit buffer. */
c906108c
SS
7074 case '$':
7075 {
40e3f985 7076 if (remote_debug)
2bc416ba 7077 fprintf_unfiltered (gdb_stdlog,
23860348 7078 "Packet instead of Ack, ignoring it\n");
d6f7abdf
AC
7079 /* It's probably an old response sent because an ACK
7080 was lost. Gobble up the packet and ack it so it
7081 doesn't get retransmitted when we resend this
7082 packet. */
6d820c5c 7083 skip_frame ();
d6f7abdf 7084 serial_write (remote_desc, "+", 1);
23860348 7085 continue; /* Now, go look for +. */
c906108c 7086 }
74531fed
PA
7087
7088 case '%':
7089 {
7090 int val;
7091
7092 /* If we got a notification, handle it, and go back to looking
7093 for an ack. */
7094 /* We've found the start of a notification. Now
7095 collect the data. */
7096 val = read_frame (&rs->buf, &rs->buf_size);
7097 if (val >= 0)
7098 {
7099 if (remote_debug)
7100 {
6e5abd65
PA
7101 struct cleanup *old_chain;
7102 char *str;
7103
7104 str = escape_buffer (rs->buf, val);
7105 old_chain = make_cleanup (xfree, str);
7106 fprintf_unfiltered (gdb_stdlog,
7107 " Notification received: %s\n",
7108 str);
7109 do_cleanups (old_chain);
74531fed
PA
7110 }
7111 handle_notification (rs->buf, val);
7112 /* We're in sync now, rewait for the ack. */
7113 tcount = 0;
7114 }
7115 else
7116 {
7117 if (remote_debug)
7118 {
7119 if (!started_error_output)
7120 {
7121 started_error_output = 1;
7122 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
7123 }
7124 fputc_unfiltered (ch & 0177, gdb_stdlog);
7125 fprintf_unfiltered (gdb_stdlog, "%s", rs->buf);
7126 }
7127 }
7128 continue;
7129 }
7130 /* fall-through */
c906108c
SS
7131 default:
7132 if (remote_debug)
7133 {
7134 if (!started_error_output)
7135 {
7136 started_error_output = 1;
0f71a2f6 7137 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
c906108c 7138 }
0f71a2f6 7139 fputc_unfiltered (ch & 0177, gdb_stdlog);
c906108c
SS
7140 }
7141 continue;
7142 }
23860348 7143 break; /* Here to retransmit. */
c906108c
SS
7144 }
7145
7146#if 0
7147 /* This is wrong. If doing a long backtrace, the user should be
c5aa993b
JM
7148 able to get out next time we call QUIT, without anything as
7149 violent as interrupt_query. If we want to provide a way out of
7150 here without getting to the next QUIT, it should be based on
7151 hitting ^C twice as in remote_wait. */
c906108c
SS
7152 if (quit_flag)
7153 {
7154 quit_flag = 0;
7155 interrupt_query ();
7156 }
7157#endif
7158 }
a6f3e723 7159 return 0;
c906108c
SS
7160}
7161
6d820c5c
DJ
7162/* Come here after finding the start of a frame when we expected an
7163 ack. Do our best to discard the rest of this packet. */
7164
7165static void
7166skip_frame (void)
7167{
7168 int c;
7169
7170 while (1)
7171 {
7172 c = readchar (remote_timeout);
7173 switch (c)
7174 {
7175 case SERIAL_TIMEOUT:
7176 /* Nothing we can do. */
7177 return;
7178 case '#':
7179 /* Discard the two bytes of checksum and stop. */
7180 c = readchar (remote_timeout);
7181 if (c >= 0)
7182 c = readchar (remote_timeout);
7183
7184 return;
7185 case '*': /* Run length encoding. */
7186 /* Discard the repeat count. */
7187 c = readchar (remote_timeout);
7188 if (c < 0)
7189 return;
7190 break;
7191 default:
7192 /* A regular character. */
7193 break;
7194 }
7195 }
7196}
7197
c906108c 7198/* Come here after finding the start of the frame. Collect the rest
6d820c5c
DJ
7199 into *BUF, verifying the checksum, length, and handling run-length
7200 compression. NUL terminate the buffer. If there is not enough room,
7201 expand *BUF using xrealloc.
c906108c 7202
c2d11a7d
JM
7203 Returns -1 on error, number of characters in buffer (ignoring the
7204 trailing NULL) on success. (could be extended to return one of the
23860348 7205 SERIAL status indications). */
c2d11a7d
JM
7206
7207static long
6d820c5c
DJ
7208read_frame (char **buf_p,
7209 long *sizeof_buf)
c906108c
SS
7210{
7211 unsigned char csum;
c2d11a7d 7212 long bc;
c906108c 7213 int c;
6d820c5c 7214 char *buf = *buf_p;
a6f3e723 7215 struct remote_state *rs = get_remote_state ();
c906108c
SS
7216
7217 csum = 0;
c2d11a7d 7218 bc = 0;
c906108c
SS
7219
7220 while (1)
7221 {
7222 c = readchar (remote_timeout);
c906108c
SS
7223 switch (c)
7224 {
7225 case SERIAL_TIMEOUT:
7226 if (remote_debug)
0f71a2f6 7227 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
c2d11a7d 7228 return -1;
c906108c
SS
7229 case '$':
7230 if (remote_debug)
0f71a2f6
JM
7231 fputs_filtered ("Saw new packet start in middle of old one\n",
7232 gdb_stdlog);
23860348 7233 return -1; /* Start a new packet, count retries. */
c906108c
SS
7234 case '#':
7235 {
7236 unsigned char pktcsum;
e1b09194
AC
7237 int check_0 = 0;
7238 int check_1 = 0;
c906108c 7239
c2d11a7d 7240 buf[bc] = '\0';
c906108c 7241
e1b09194
AC
7242 check_0 = readchar (remote_timeout);
7243 if (check_0 >= 0)
7244 check_1 = readchar (remote_timeout);
802188a7 7245
e1b09194
AC
7246 if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
7247 {
7248 if (remote_debug)
2bc416ba 7249 fputs_filtered ("Timeout in checksum, retrying\n",
23860348 7250 gdb_stdlog);
e1b09194
AC
7251 return -1;
7252 }
7253 else if (check_0 < 0 || check_1 < 0)
40e3f985
FN
7254 {
7255 if (remote_debug)
2bc416ba 7256 fputs_filtered ("Communication error in checksum\n",
23860348 7257 gdb_stdlog);
40e3f985
FN
7258 return -1;
7259 }
c906108c 7260
a6f3e723
SL
7261 /* Don't recompute the checksum; with no ack packets we
7262 don't have any way to indicate a packet retransmission
7263 is necessary. */
7264 if (rs->noack_mode)
7265 return bc;
7266
e1b09194 7267 pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
c906108c 7268 if (csum == pktcsum)
c2d11a7d 7269 return bc;
c906108c 7270
c5aa993b 7271 if (remote_debug)
c906108c 7272 {
6e5abd65
PA
7273 struct cleanup *old_chain;
7274 char *str;
7275
7276 str = escape_buffer (buf, bc);
7277 old_chain = make_cleanup (xfree, str);
7278 fprintf_unfiltered (gdb_stdlog,
3e43a32a
MS
7279 "Bad checksum, sentsum=0x%x, "
7280 "csum=0x%x, buf=%s\n",
6e5abd65
PA
7281 pktcsum, csum, str);
7282 do_cleanups (old_chain);
c906108c 7283 }
c2d11a7d 7284 /* Number of characters in buffer ignoring trailing
23860348 7285 NULL. */
c2d11a7d 7286 return -1;
c906108c 7287 }
23860348 7288 case '*': /* Run length encoding. */
c2c6d25f
JM
7289 {
7290 int repeat;
c906108c 7291
a744cf53 7292 csum += c;
b4501125
AC
7293 c = readchar (remote_timeout);
7294 csum += c;
23860348 7295 repeat = c - ' ' + 3; /* Compute repeat count. */
c906108c 7296
23860348 7297 /* The character before ``*'' is repeated. */
c2d11a7d 7298
6d820c5c 7299 if (repeat > 0 && repeat <= 255 && bc > 0)
c2c6d25f 7300 {
6d820c5c
DJ
7301 if (bc + repeat - 1 >= *sizeof_buf - 1)
7302 {
7303 /* Make some more room in the buffer. */
7304 *sizeof_buf += repeat;
7305 *buf_p = xrealloc (*buf_p, *sizeof_buf);
7306 buf = *buf_p;
7307 }
7308
c2d11a7d
JM
7309 memset (&buf[bc], buf[bc - 1], repeat);
7310 bc += repeat;
c2c6d25f
JM
7311 continue;
7312 }
7313
c2d11a7d 7314 buf[bc] = '\0';
6d820c5c 7315 printf_filtered (_("Invalid run length encoding: %s\n"), buf);
c2d11a7d 7316 return -1;
c2c6d25f 7317 }
c906108c 7318 default:
6d820c5c 7319 if (bc >= *sizeof_buf - 1)
c906108c 7320 {
6d820c5c
DJ
7321 /* Make some more room in the buffer. */
7322 *sizeof_buf *= 2;
7323 *buf_p = xrealloc (*buf_p, *sizeof_buf);
7324 buf = *buf_p;
c906108c
SS
7325 }
7326
6d820c5c
DJ
7327 buf[bc++] = c;
7328 csum += c;
7329 continue;
c906108c
SS
7330 }
7331 }
7332}
7333
7334/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
7335 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7336 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7337 rather than timing out; this is used (in synchronous mode) to wait
7338 for a target that is is executing user code to stop. */
d9fcf2fb
JM
7339/* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
7340 don't have to change all the calls to getpkt to deal with the
7341 return value, because at the moment I don't know what the right
23860348 7342 thing to do it for those. */
c906108c 7343void
6d820c5c
DJ
7344getpkt (char **buf,
7345 long *sizeof_buf,
c2d11a7d 7346 int forever)
d9fcf2fb
JM
7347{
7348 int timed_out;
7349
7350 timed_out = getpkt_sane (buf, sizeof_buf, forever);
7351}
7352
7353
7354/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
7355 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
7356 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
7357 rather than timing out; this is used (in synchronous mode) to wait
7358 for a target that is is executing user code to stop. If FOREVER ==
7359 0, this function is allowed to time out gracefully and return an
74531fed
PA
7360 indication of this to the caller. Otherwise return the number of
7361 bytes read. If EXPECTING_NOTIF, consider receiving a notification
7362 enough reason to return to the caller. */
7363
3172dc30 7364static int
74531fed
PA
7365getpkt_or_notif_sane_1 (char **buf, long *sizeof_buf, int forever,
7366 int expecting_notif)
c906108c 7367{
2d717e4f 7368 struct remote_state *rs = get_remote_state ();
c906108c
SS
7369 int c;
7370 int tries;
7371 int timeout;
df4b58fe 7372 int val = -1;
c906108c 7373
2d717e4f
DJ
7374 /* We're reading a new response. Make sure we don't look at a
7375 previously cached response. */
7376 rs->cached_wait_status = 0;
7377
6d820c5c 7378 strcpy (*buf, "timeout");
c906108c
SS
7379
7380 if (forever)
74531fed
PA
7381 timeout = watchdog > 0 ? watchdog : -1;
7382 else if (expecting_notif)
7383 timeout = 0; /* There should already be a char in the buffer. If
7384 not, bail out. */
c906108c
SS
7385 else
7386 timeout = remote_timeout;
7387
7388#define MAX_TRIES 3
7389
74531fed
PA
7390 /* Process any number of notifications, and then return when
7391 we get a packet. */
7392 for (;;)
c906108c 7393 {
74531fed
PA
7394 /* If we get a timeout or bad checksm, retry up to MAX_TRIES
7395 times. */
7396 for (tries = 1; tries <= MAX_TRIES; tries++)
c906108c 7397 {
74531fed
PA
7398 /* This can loop forever if the remote side sends us
7399 characters continuously, but if it pauses, we'll get
7400 SERIAL_TIMEOUT from readchar because of timeout. Then
7401 we'll count that as a retry.
7402
7403 Note that even when forever is set, we will only wait
7404 forever prior to the start of a packet. After that, we
7405 expect characters to arrive at a brisk pace. They should
7406 show up within remote_timeout intervals. */
7407 do
7408 c = readchar (timeout);
7409 while (c != SERIAL_TIMEOUT && c != '$' && c != '%');
c906108c
SS
7410
7411 if (c == SERIAL_TIMEOUT)
7412 {
74531fed
PA
7413 if (expecting_notif)
7414 return -1; /* Don't complain, it's normal to not get
7415 anything in this case. */
7416
23860348 7417 if (forever) /* Watchdog went off? Kill the target. */
c906108c 7418 {
2acceee2 7419 QUIT;
ce5ce7ed 7420 pop_target ();
489eaeba 7421 error (_("Watchdog timeout has expired. Target detached."));
c906108c 7422 }
c906108c 7423 if (remote_debug)
0f71a2f6 7424 fputs_filtered ("Timed out.\n", gdb_stdlog);
c906108c 7425 }
74531fed
PA
7426 else
7427 {
7428 /* We've found the start of a packet or notification.
7429 Now collect the data. */
7430 val = read_frame (buf, sizeof_buf);
7431 if (val >= 0)
7432 break;
7433 }
7434
7435 serial_write (remote_desc, "-", 1);
c906108c 7436 }
c906108c 7437
74531fed
PA
7438 if (tries > MAX_TRIES)
7439 {
7440 /* We have tried hard enough, and just can't receive the
7441 packet/notification. Give up. */
7442 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
c906108c 7443
74531fed
PA
7444 /* Skip the ack char if we're in no-ack mode. */
7445 if (!rs->noack_mode)
7446 serial_write (remote_desc, "+", 1);
7447 return -1;
7448 }
c906108c 7449
74531fed
PA
7450 /* If we got an ordinary packet, return that to our caller. */
7451 if (c == '$')
c906108c
SS
7452 {
7453 if (remote_debug)
43e526b9 7454 {
6e5abd65
PA
7455 struct cleanup *old_chain;
7456 char *str;
7457
7458 str = escape_buffer (*buf, val);
7459 old_chain = make_cleanup (xfree, str);
7460 fprintf_unfiltered (gdb_stdlog, "Packet received: %s\n", str);
7461 do_cleanups (old_chain);
43e526b9 7462 }
a6f3e723
SL
7463
7464 /* Skip the ack char if we're in no-ack mode. */
7465 if (!rs->noack_mode)
7466 serial_write (remote_desc, "+", 1);
0876f84a 7467 return val;
c906108c
SS
7468 }
7469
74531fed
PA
7470 /* If we got a notification, handle it, and go back to looking
7471 for a packet. */
7472 else
7473 {
7474 gdb_assert (c == '%');
7475
7476 if (remote_debug)
7477 {
6e5abd65
PA
7478 struct cleanup *old_chain;
7479 char *str;
7480
7481 str = escape_buffer (*buf, val);
7482 old_chain = make_cleanup (xfree, str);
7483 fprintf_unfiltered (gdb_stdlog,
7484 " Notification received: %s\n",
7485 str);
7486 do_cleanups (old_chain);
74531fed 7487 }
c906108c 7488
74531fed 7489 handle_notification (*buf, val);
c906108c 7490
74531fed 7491 /* Notifications require no acknowledgement. */
a6f3e723 7492
74531fed
PA
7493 if (expecting_notif)
7494 return -1;
7495 }
7496 }
7497}
7498
7499static int
7500getpkt_sane (char **buf, long *sizeof_buf, int forever)
7501{
7502 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 0);
7503}
7504
7505static int
7506getpkt_or_notif_sane (char **buf, long *sizeof_buf, int forever)
7507{
7508 return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 1);
c906108c 7509}
74531fed 7510
c906108c 7511\f
732f3f12
TT
7512/* A helper function that just calls putpkt; for type correctness. */
7513
7514static int
7515putpkt_for_catch_errors (void *arg)
7516{
7517 return putpkt (arg);
7518}
7519
c906108c 7520static void
7d85a9c0 7521remote_kill (struct target_ops *ops)
43ff13b4 7522{
23860348
MS
7523 /* Use catch_errors so the user can quit from gdb even when we
7524 aren't on speaking terms with the remote system. */
732f3f12 7525 catch_errors (putpkt_for_catch_errors, "k", "", RETURN_MASK_ERROR);
43ff13b4
JM
7526
7527 /* Don't wait for it to die. I'm not really sure it matters whether
7528 we do or not. For the existing stubs, kill is a noop. */
7529 target_mourn_inferior ();
7530}
7531
82f73884
PA
7532static int
7533remote_vkill (int pid, struct remote_state *rs)
7534{
7535 if (remote_protocol_packets[PACKET_vKill].support == PACKET_DISABLE)
7536 return -1;
7537
7538 /* Tell the remote target to detach. */
bba74b36 7539 xsnprintf (rs->buf, get_remote_packet_size (), "vKill;%x", pid);
82f73884
PA
7540 putpkt (rs->buf);
7541 getpkt (&rs->buf, &rs->buf_size, 0);
7542
7543 if (packet_ok (rs->buf,
7544 &remote_protocol_packets[PACKET_vKill]) == PACKET_OK)
7545 return 0;
7546 else if (remote_protocol_packets[PACKET_vKill].support == PACKET_DISABLE)
7547 return -1;
7548 else
7549 return 1;
7550}
7551
7552static void
7d85a9c0 7553extended_remote_kill (struct target_ops *ops)
82f73884
PA
7554{
7555 int res;
7556 int pid = ptid_get_pid (inferior_ptid);
7557 struct remote_state *rs = get_remote_state ();
7558
7559 res = remote_vkill (pid, rs);
901f9912 7560 if (res == -1 && !(rs->extended && remote_multi_process_p (rs)))
82f73884
PA
7561 {
7562 /* Don't try 'k' on a multi-process aware stub -- it has no way
7563 to specify the pid. */
7564
7565 putpkt ("k");
7566#if 0
7567 getpkt (&rs->buf, &rs->buf_size, 0);
7568 if (rs->buf[0] != 'O' || rs->buf[0] != 'K')
7569 res = 1;
7570#else
7571 /* Don't wait for it to die. I'm not really sure it matters whether
7572 we do or not. For the existing stubs, kill is a noop. */
7573 res = 0;
7574#endif
7575 }
7576
7577 if (res != 0)
7578 error (_("Can't kill process"));
7579
82f73884
PA
7580 target_mourn_inferior ();
7581}
7582
c906108c 7583static void
136d6dae 7584remote_mourn (struct target_ops *ops)
c906108c 7585{
136d6dae 7586 remote_mourn_1 (ops);
c906108c
SS
7587}
7588
c906108c
SS
7589/* Worker function for remote_mourn. */
7590static void
fba45db2 7591remote_mourn_1 (struct target_ops *target)
c906108c
SS
7592{
7593 unpush_target (target);
ce5ce7ed 7594
8a2492ee
PA
7595 /* remote_close takes care of doing most of the clean up. */
7596 generic_mourn_inferior ();
c906108c
SS
7597}
7598
2d717e4f
DJ
7599static void
7600extended_remote_mourn_1 (struct target_ops *target)
7601{
7602 struct remote_state *rs = get_remote_state ();
c906108c 7603
e24a49d8
PA
7604 /* In case we got here due to an error, but we're going to stay
7605 connected. */
7606 rs->waiting_for_stop_reply = 0;
7607
74531fed
PA
7608 /* We're no longer interested in these events. */
7609 discard_pending_stop_replies (ptid_get_pid (inferior_ptid));
7610
dc1981d7
PA
7611 /* If the current general thread belonged to the process we just
7612 detached from or has exited, the remote side current general
7613 thread becomes undefined. Considering a case like this:
7614
7615 - We just got here due to a detach.
7616 - The process that we're detaching from happens to immediately
7617 report a global breakpoint being hit in non-stop mode, in the
7618 same thread we had selected before.
7619 - GDB attaches to this process again.
7620 - This event happens to be the next event we handle.
7621
7622 GDB would consider that the current general thread didn't need to
7623 be set on the stub side (with Hg), since for all it knew,
7624 GENERAL_THREAD hadn't changed.
7625
7626 Notice that although in all-stop mode, the remote server always
7627 sets the current thread to the thread reporting the stop event,
7628 that doesn't happen in non-stop mode; in non-stop, the stub *must
7629 not* change the current thread when reporting a breakpoint hit,
7630 due to the decoupling of event reporting and event handling.
7631
7632 To keep things simple, we always invalidate our notion of the
7633 current thread. */
7634 record_currthread (minus_one_ptid);
7635
2d717e4f
DJ
7636 /* Unlike "target remote", we do not want to unpush the target; then
7637 the next time the user says "run", we won't be connected. */
7638
48aa3c27
PA
7639 /* Call common code to mark the inferior as not running. */
7640 generic_mourn_inferior ();
7641
d729566a 7642 if (!have_inferiors ())
2d717e4f 7643 {
82f73884
PA
7644 if (!remote_multi_process_p (rs))
7645 {
7646 /* Check whether the target is running now - some remote stubs
7647 automatically restart after kill. */
7648 putpkt ("?");
7649 getpkt (&rs->buf, &rs->buf_size, 0);
7650
7651 if (rs->buf[0] == 'S' || rs->buf[0] == 'T')
7652 {
3e43a32a
MS
7653 /* Assume that the target has been restarted. Set
7654 inferior_ptid so that bits of core GDB realizes
7655 there's something here, e.g., so that the user can
7656 say "kill" again. */
82f73884
PA
7657 inferior_ptid = magic_null_ptid;
7658 }
82f73884 7659 }
2d717e4f
DJ
7660 }
7661}
c906108c
SS
7662
7663static void
136d6dae 7664extended_remote_mourn (struct target_ops *ops)
c906108c 7665{
136d6dae 7666 extended_remote_mourn_1 (ops);
2d717e4f 7667}
c906108c 7668
03583c20
UW
7669static int
7670extended_remote_supports_disable_randomization (void)
7671{
7672 return (remote_protocol_packets[PACKET_QDisableRandomization].support
7673 == PACKET_ENABLE);
7674}
7675
7676static void
7677extended_remote_disable_randomization (int val)
7678{
7679 struct remote_state *rs = get_remote_state ();
7680 char *reply;
7681
bba74b36
YQ
7682 xsnprintf (rs->buf, get_remote_packet_size (), "QDisableRandomization:%x",
7683 val);
03583c20
UW
7684 putpkt (rs->buf);
7685 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
7686 if (*reply == '\0')
7687 error (_("Target does not support QDisableRandomization."));
7688 if (strcmp (reply, "OK") != 0)
7689 error (_("Bogus QDisableRandomization reply from target: %s"), reply);
7690}
7691
2d717e4f
DJ
7692static int
7693extended_remote_run (char *args)
7694{
7695 struct remote_state *rs = get_remote_state ();
2d717e4f 7696 int len;
c906108c 7697
2d717e4f
DJ
7698 /* If the user has disabled vRun support, or we have detected that
7699 support is not available, do not try it. */
7700 if (remote_protocol_packets[PACKET_vRun].support == PACKET_DISABLE)
7701 return -1;
424163ea 7702
2d717e4f
DJ
7703 strcpy (rs->buf, "vRun;");
7704 len = strlen (rs->buf);
c906108c 7705
2d717e4f
DJ
7706 if (strlen (remote_exec_file) * 2 + len >= get_remote_packet_size ())
7707 error (_("Remote file name too long for run packet"));
7708 len += 2 * bin2hex ((gdb_byte *) remote_exec_file, rs->buf + len, 0);
7709
d1a41061 7710 gdb_assert (args != NULL);
2d717e4f
DJ
7711 if (*args)
7712 {
7713 struct cleanup *back_to;
7714 int i;
7715 char **argv;
7716
d1a41061 7717 argv = gdb_buildargv (args);
2d717e4f
DJ
7718 back_to = make_cleanup ((void (*) (void *)) freeargv, argv);
7719 for (i = 0; argv[i] != NULL; i++)
7720 {
7721 if (strlen (argv[i]) * 2 + 1 + len >= get_remote_packet_size ())
7722 error (_("Argument list too long for run packet"));
7723 rs->buf[len++] = ';';
7724 len += 2 * bin2hex ((gdb_byte *) argv[i], rs->buf + len, 0);
7725 }
7726 do_cleanups (back_to);
7727 }
7728
7729 rs->buf[len++] = '\0';
7730
7731 putpkt (rs->buf);
7732 getpkt (&rs->buf, &rs->buf_size, 0);
7733
7734 if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vRun]) == PACKET_OK)
7735 {
7736 /* We have a wait response; we don't need it, though. All is well. */
7737 return 0;
7738 }
7739 else if (remote_protocol_packets[PACKET_vRun].support == PACKET_DISABLE)
7740 /* It wasn't disabled before, but it is now. */
7741 return -1;
7742 else
7743 {
7744 if (remote_exec_file[0] == '\0')
7745 error (_("Running the default executable on the remote target failed; "
7746 "try \"set remote exec-file\"?"));
7747 else
7748 error (_("Running \"%s\" on the remote target failed"),
7749 remote_exec_file);
7750 }
c906108c
SS
7751}
7752
2d717e4f
DJ
7753/* In the extended protocol we want to be able to do things like
7754 "run" and have them basically work as expected. So we need
7755 a special create_inferior function. We support changing the
7756 executable file and the command line arguments, but not the
7757 environment. */
7758
43ff13b4 7759static void
2d717e4f 7760extended_remote_create_inferior_1 (char *exec_file, char *args,
75c99385 7761 char **env, int from_tty)
43ff13b4 7762{
43ff13b4 7763 /* If running asynchronously, register the target file descriptor
23860348 7764 with the event loop. */
75c99385 7765 if (target_can_async_p ())
2acceee2 7766 target_async (inferior_event_handler, 0);
43ff13b4 7767
03583c20
UW
7768 /* Disable address space randomization if requested (and supported). */
7769 if (extended_remote_supports_disable_randomization ())
7770 extended_remote_disable_randomization (disable_randomization);
7771
43ff13b4 7772 /* Now restart the remote server. */
2d717e4f
DJ
7773 if (extended_remote_run (args) == -1)
7774 {
7775 /* vRun was not supported. Fail if we need it to do what the
7776 user requested. */
7777 if (remote_exec_file[0])
7778 error (_("Remote target does not support \"set remote exec-file\""));
7779 if (args[0])
7780 error (_("Remote target does not support \"set args\" or run <ARGS>"));
43ff13b4 7781
2d717e4f
DJ
7782 /* Fall back to "R". */
7783 extended_remote_restart ();
7784 }
424163ea 7785
6c95b8df
PA
7786 if (!have_inferiors ())
7787 {
7788 /* Clean up from the last time we ran, before we mark the target
7789 running again. This will mark breakpoints uninserted, and
7790 get_offsets may insert breakpoints. */
7791 init_thread_list ();
7792 init_wait_for_inferior ();
7793 }
45280a52 7794
49c62f2e 7795 add_current_inferior_and_thread ();
c0a2216e 7796
2d717e4f
DJ
7797 /* Get updated offsets, if the stub uses qOffsets. */
7798 get_offsets ();
2d717e4f
DJ
7799}
7800
7801static void
136d6dae
VP
7802extended_remote_create_inferior (struct target_ops *ops,
7803 char *exec_file, char *args,
2d717e4f
DJ
7804 char **env, int from_tty)
7805{
75c99385 7806 extended_remote_create_inferior_1 (exec_file, args, env, from_tty);
43ff13b4 7807}
c906108c 7808\f
c5aa993b 7809
b775012e
LM
7810/* Given a location's target info BP_TGT and the packet buffer BUF, output
7811 the list of conditions (in agent expression bytecode format), if any, the
7812 target needs to evaluate. The output is placed into the packet buffer
bba74b36 7813 started from BUF and ended at BUF_END. */
b775012e
LM
7814
7815static int
7816remote_add_target_side_condition (struct gdbarch *gdbarch,
bba74b36
YQ
7817 struct bp_target_info *bp_tgt, char *buf,
7818 char *buf_end)
b775012e
LM
7819{
7820 struct agent_expr *aexpr = NULL;
7821 int i, ix;
7822 char *pkt;
7823 char *buf_start = buf;
7824
7825 if (VEC_empty (agent_expr_p, bp_tgt->conditions))
7826 return 0;
7827
7828 buf += strlen (buf);
bba74b36 7829 xsnprintf (buf, buf_end - buf, "%s", ";");
b775012e
LM
7830 buf++;
7831
7832 /* Send conditions to the target and free the vector. */
7833 for (ix = 0;
7834 VEC_iterate (agent_expr_p, bp_tgt->conditions, ix, aexpr);
7835 ix++)
7836 {
bba74b36 7837 xsnprintf (buf, buf_end - buf, "X%x,", aexpr->len);
b775012e
LM
7838 buf += strlen (buf);
7839 for (i = 0; i < aexpr->len; ++i)
7840 buf = pack_hex_byte (buf, aexpr->buf[i]);
7841 *buf = '\0';
7842 }
7843
7844 VEC_free (agent_expr_p, bp_tgt->conditions);
7845 return 0;
7846}
7847
8181d85f
DJ
7848/* Insert a breakpoint. On targets that have software breakpoint
7849 support, we ask the remote target to do the work; on targets
7850 which don't, we insert a traditional memory breakpoint. */
c906108c
SS
7851
7852static int
a6d9a66e
UW
7853remote_insert_breakpoint (struct gdbarch *gdbarch,
7854 struct bp_target_info *bp_tgt)
c906108c 7855{
d471ea57
AC
7856 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
7857 If it succeeds, then set the support to PACKET_ENABLE. If it
7858 fails, and the user has explicitly requested the Z support then
23860348 7859 report an error, otherwise, mark it disabled and go on. */
802188a7 7860
444abaca 7861 if (remote_protocol_packets[PACKET_Z0].support != PACKET_DISABLE)
96baa820 7862 {
7c0f6dcc 7863 CORE_ADDR addr = bp_tgt->placed_address;
4fff2411 7864 struct remote_state *rs;
bba74b36 7865 char *p, *endbuf;
7c0f6dcc 7866 int bpsize;
b775012e 7867 struct condition_list *cond = NULL;
4fff2411 7868
a1dcb23a 7869 gdbarch_remote_breakpoint_from_pc (gdbarch, &addr, &bpsize);
4fff2411
JZ
7870
7871 rs = get_remote_state ();
7872 p = rs->buf;
bba74b36 7873 endbuf = rs->buf + get_remote_packet_size ();
802188a7 7874
96baa820
JM
7875 *(p++) = 'Z';
7876 *(p++) = '0';
7877 *(p++) = ',';
7c0f6dcc 7878 addr = (ULONGEST) remote_address_masked (addr);
8181d85f 7879 p += hexnumstr (p, addr);
bba74b36 7880 xsnprintf (p, endbuf - p, ",%d", bpsize);
802188a7 7881
b775012e 7882 if (remote_supports_cond_breakpoints ())
bba74b36 7883 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
b775012e 7884
6d820c5c
DJ
7885 putpkt (rs->buf);
7886 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 7887
6d820c5c 7888 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0]))
96baa820 7889 {
d471ea57
AC
7890 case PACKET_ERROR:
7891 return -1;
7892 case PACKET_OK:
7c0f6dcc
JL
7893 bp_tgt->placed_address = addr;
7894 bp_tgt->placed_size = bpsize;
d471ea57
AC
7895 return 0;
7896 case PACKET_UNKNOWN:
7897 break;
96baa820
JM
7898 }
7899 }
c906108c 7900
a6d9a66e 7901 return memory_insert_breakpoint (gdbarch, bp_tgt);
c906108c
SS
7902}
7903
7904static int
a6d9a66e
UW
7905remote_remove_breakpoint (struct gdbarch *gdbarch,
7906 struct bp_target_info *bp_tgt)
c906108c 7907{
8181d85f 7908 CORE_ADDR addr = bp_tgt->placed_address;
d01949b6 7909 struct remote_state *rs = get_remote_state ();
96baa820 7910
444abaca 7911 if (remote_protocol_packets[PACKET_Z0].support != PACKET_DISABLE)
96baa820 7912 {
6d820c5c 7913 char *p = rs->buf;
bba74b36 7914 char *endbuf = rs->buf + get_remote_packet_size ();
802188a7 7915
96baa820
JM
7916 *(p++) = 'z';
7917 *(p++) = '0';
7918 *(p++) = ',';
7919
8181d85f
DJ
7920 addr = (ULONGEST) remote_address_masked (bp_tgt->placed_address);
7921 p += hexnumstr (p, addr);
bba74b36 7922 xsnprintf (p, endbuf - p, ",%d", bp_tgt->placed_size);
802188a7 7923
6d820c5c
DJ
7924 putpkt (rs->buf);
7925 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 7926
6d820c5c 7927 return (rs->buf[0] == 'E');
96baa820
JM
7928 }
7929
a6d9a66e 7930 return memory_remove_breakpoint (gdbarch, bp_tgt);
c906108c
SS
7931}
7932
d471ea57
AC
7933static int
7934watchpoint_to_Z_packet (int type)
7935{
7936 switch (type)
7937 {
7938 case hw_write:
bb858e6a 7939 return Z_PACKET_WRITE_WP;
d471ea57
AC
7940 break;
7941 case hw_read:
bb858e6a 7942 return Z_PACKET_READ_WP;
d471ea57
AC
7943 break;
7944 case hw_access:
bb858e6a 7945 return Z_PACKET_ACCESS_WP;
d471ea57
AC
7946 break;
7947 default:
8e65ff28 7948 internal_error (__FILE__, __LINE__,
e2e0b3e5 7949 _("hw_bp_to_z: bad watchpoint type %d"), type);
d471ea57
AC
7950 }
7951}
7952
3c3bea1c 7953static int
0cf6dd15
TJB
7954remote_insert_watchpoint (CORE_ADDR addr, int len, int type,
7955 struct expression *cond)
96baa820 7956{
d01949b6 7957 struct remote_state *rs = get_remote_state ();
bba74b36 7958 char *endbuf = rs->buf + get_remote_packet_size ();
e514a9d6 7959 char *p;
d471ea57 7960 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
96baa820 7961
444abaca 7962 if (remote_protocol_packets[PACKET_Z0 + packet].support == PACKET_DISABLE)
85d721b8 7963 return 1;
802188a7 7964
bba74b36 7965 xsnprintf (rs->buf, endbuf - rs->buf, "Z%x,", packet);
6d820c5c 7966 p = strchr (rs->buf, '\0');
96baa820
JM
7967 addr = remote_address_masked (addr);
7968 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 7969 xsnprintf (p, endbuf - p, ",%x", len);
802188a7 7970
6d820c5c
DJ
7971 putpkt (rs->buf);
7972 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 7973
6d820c5c 7974 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
7975 {
7976 case PACKET_ERROR:
d471ea57 7977 return -1;
85d721b8
PA
7978 case PACKET_UNKNOWN:
7979 return 1;
d471ea57
AC
7980 case PACKET_OK:
7981 return 0;
7982 }
8e65ff28 7983 internal_error (__FILE__, __LINE__,
e2e0b3e5 7984 _("remote_insert_watchpoint: reached end of function"));
96baa820
JM
7985}
7986
283002cf
MR
7987static int
7988remote_watchpoint_addr_within_range (struct target_ops *target, CORE_ADDR addr,
7989 CORE_ADDR start, int length)
7990{
7991 CORE_ADDR diff = remote_address_masked (addr - start);
7992
7993 return diff < length;
7994}
7995
d471ea57 7996
3c3bea1c 7997static int
0cf6dd15
TJB
7998remote_remove_watchpoint (CORE_ADDR addr, int len, int type,
7999 struct expression *cond)
96baa820 8000{
d01949b6 8001 struct remote_state *rs = get_remote_state ();
bba74b36 8002 char *endbuf = rs->buf + get_remote_packet_size ();
e514a9d6 8003 char *p;
d471ea57
AC
8004 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
8005
444abaca 8006 if (remote_protocol_packets[PACKET_Z0 + packet].support == PACKET_DISABLE)
5cffb350 8007 return -1;
802188a7 8008
bba74b36 8009 xsnprintf (rs->buf, endbuf - rs->buf, "z%x,", packet);
6d820c5c 8010 p = strchr (rs->buf, '\0');
96baa820
JM
8011 addr = remote_address_masked (addr);
8012 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 8013 xsnprintf (p, endbuf - p, ",%x", len);
6d820c5c
DJ
8014 putpkt (rs->buf);
8015 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8016
6d820c5c 8017 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
8018 {
8019 case PACKET_ERROR:
8020 case PACKET_UNKNOWN:
8021 return -1;
8022 case PACKET_OK:
8023 return 0;
8024 }
8e65ff28 8025 internal_error (__FILE__, __LINE__,
e2e0b3e5 8026 _("remote_remove_watchpoint: reached end of function"));
96baa820
JM
8027}
8028
3c3bea1c 8029
501eef12 8030int remote_hw_watchpoint_limit = -1;
480a3f21 8031int remote_hw_watchpoint_length_limit = -1;
501eef12 8032int remote_hw_breakpoint_limit = -1;
d471ea57 8033
480a3f21
PW
8034static int
8035remote_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
8036{
8037 if (remote_hw_watchpoint_length_limit == 0)
8038 return 0;
8039 else if (remote_hw_watchpoint_length_limit < 0)
8040 return 1;
8041 else if (len <= remote_hw_watchpoint_length_limit)
8042 return 1;
8043 else
8044 return 0;
8045}
8046
b9362cc7 8047static int
3c3bea1c 8048remote_check_watch_resources (int type, int cnt, int ot)
96baa820 8049{
3c3bea1c
GS
8050 if (type == bp_hardware_breakpoint)
8051 {
8052 if (remote_hw_breakpoint_limit == 0)
8053 return 0;
501eef12
AC
8054 else if (remote_hw_breakpoint_limit < 0)
8055 return 1;
3c3bea1c
GS
8056 else if (cnt <= remote_hw_breakpoint_limit)
8057 return 1;
8058 }
8059 else
8060 {
8061 if (remote_hw_watchpoint_limit == 0)
8062 return 0;
501eef12
AC
8063 else if (remote_hw_watchpoint_limit < 0)
8064 return 1;
3c3bea1c
GS
8065 else if (ot)
8066 return -1;
8067 else if (cnt <= remote_hw_watchpoint_limit)
8068 return 1;
8069 }
8070 return -1;
8071}
8072
b9362cc7 8073static int
3c3bea1c
GS
8074remote_stopped_by_watchpoint (void)
8075{
82f73884 8076 return remote_stopped_by_watchpoint_p;
3c3bea1c
GS
8077}
8078
4aa7a7f5
JJ
8079static int
8080remote_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
3c3bea1c 8081{
4aa7a7f5 8082 int rc = 0;
a744cf53 8083
d983da9c 8084 if (remote_stopped_by_watchpoint ())
4aa7a7f5
JJ
8085 {
8086 *addr_p = remote_watch_data_address;
8087 rc = 1;
8088 }
8089
8090 return rc;
3c3bea1c
GS
8091}
8092
8093
8094static int
a6d9a66e
UW
8095remote_insert_hw_breakpoint (struct gdbarch *gdbarch,
8096 struct bp_target_info *bp_tgt)
3c3bea1c 8097{
8181d85f 8098 CORE_ADDR addr;
4fff2411 8099 struct remote_state *rs;
bba74b36 8100 char *p, *endbuf;
802188a7 8101
c8189ed1 8102 /* The length field should be set to the size of a breakpoint
8181d85f 8103 instruction, even though we aren't inserting one ourselves. */
c8189ed1 8104
a1dcb23a 8105 gdbarch_remote_breakpoint_from_pc
a6d9a66e 8106 (gdbarch, &bp_tgt->placed_address, &bp_tgt->placed_size);
3c3bea1c 8107
444abaca 8108 if (remote_protocol_packets[PACKET_Z1].support == PACKET_DISABLE)
5cffb350 8109 return -1;
2bc416ba 8110
4fff2411
JZ
8111 rs = get_remote_state ();
8112 p = rs->buf;
bba74b36 8113 endbuf = rs->buf + get_remote_packet_size ();
4fff2411 8114
96baa820
JM
8115 *(p++) = 'Z';
8116 *(p++) = '1';
8117 *(p++) = ',';
802188a7 8118
8181d85f 8119 addr = remote_address_masked (bp_tgt->placed_address);
96baa820 8120 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 8121 xsnprintf (p, endbuf - p, ",%x", bp_tgt->placed_size);
96baa820 8122
b775012e 8123 if (remote_supports_cond_breakpoints ())
bba74b36 8124 remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
b775012e 8125
6d820c5c
DJ
8126 putpkt (rs->buf);
8127 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 8128
6d820c5c 8129 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
8130 {
8131 case PACKET_ERROR:
8132 case PACKET_UNKNOWN:
8133 return -1;
8134 case PACKET_OK:
8135 return 0;
8136 }
8e65ff28 8137 internal_error (__FILE__, __LINE__,
e2e0b3e5 8138 _("remote_insert_hw_breakpoint: reached end of function"));
96baa820
JM
8139}
8140
d471ea57 8141
802188a7 8142static int
a6d9a66e
UW
8143remote_remove_hw_breakpoint (struct gdbarch *gdbarch,
8144 struct bp_target_info *bp_tgt)
96baa820 8145{
8181d85f 8146 CORE_ADDR addr;
d01949b6 8147 struct remote_state *rs = get_remote_state ();
6d820c5c 8148 char *p = rs->buf;
bba74b36 8149 char *endbuf = rs->buf + get_remote_packet_size ();
c8189ed1 8150
444abaca 8151 if (remote_protocol_packets[PACKET_Z1].support == PACKET_DISABLE)
5cffb350 8152 return -1;
802188a7 8153
96baa820
JM
8154 *(p++) = 'z';
8155 *(p++) = '1';
8156 *(p++) = ',';
802188a7 8157
8181d85f 8158 addr = remote_address_masked (bp_tgt->placed_address);
96baa820 8159 p += hexnumstr (p, (ULONGEST) addr);
bba74b36 8160 xsnprintf (p, endbuf - p, ",%x", bp_tgt->placed_size);
96baa820 8161
6d820c5c
DJ
8162 putpkt (rs->buf);
8163 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 8164
6d820c5c 8165 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
8166 {
8167 case PACKET_ERROR:
8168 case PACKET_UNKNOWN:
8169 return -1;
8170 case PACKET_OK:
8171 return 0;
8172 }
8e65ff28 8173 internal_error (__FILE__, __LINE__,
e2e0b3e5 8174 _("remote_remove_hw_breakpoint: reached end of function"));
96baa820 8175}
96baa820 8176
23860348 8177/* Table used by the crc32 function to calcuate the checksum. */
c906108c 8178
c5aa993b
JM
8179static unsigned long crc32_table[256] =
8180{0, 0};
c906108c
SS
8181
8182static unsigned long
4a5e7a5b 8183crc32 (const unsigned char *buf, int len, unsigned int crc)
c906108c 8184{
c5aa993b 8185 if (!crc32_table[1])
c906108c 8186 {
23860348 8187 /* Initialize the CRC table and the decoding table. */
c906108c
SS
8188 int i, j;
8189 unsigned int c;
8190
8191 for (i = 0; i < 256; i++)
c5aa993b
JM
8192 {
8193 for (c = i << 24, j = 8; j > 0; --j)
8194 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
8195 crc32_table[i] = c;
8196 }
c906108c
SS
8197 }
8198
8199 while (len--)
8200 {
8201 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buf) & 255];
8202 buf++;
8203 }
8204 return crc;
8205}
8206
4a5e7a5b
PA
8207/* Verify memory using the "qCRC:" request. */
8208
8209static int
8210remote_verify_memory (struct target_ops *ops,
8211 const gdb_byte *data, CORE_ADDR lma, ULONGEST size)
8212{
8213 struct remote_state *rs = get_remote_state ();
8214 unsigned long host_crc, target_crc;
8215 char *tmp;
8216
8217 /* FIXME: assumes lma can fit into long. */
8218 xsnprintf (rs->buf, get_remote_packet_size (), "qCRC:%lx,%lx",
8219 (long) lma, (long) size);
8220 putpkt (rs->buf);
8221
8222 /* Be clever; compute the host_crc before waiting for target
8223 reply. */
8224 host_crc = crc32 (data, size, 0xffffffff);
8225
8226 getpkt (&rs->buf, &rs->buf_size, 0);
8227 if (rs->buf[0] == 'E')
8228 return -1;
8229
8230 if (rs->buf[0] != 'C')
8231 error (_("remote target does not support this operation"));
8232
8233 for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++)
8234 target_crc = target_crc * 16 + fromhex (*tmp);
8235
8236 return (host_crc == target_crc);
8237}
8238
c906108c
SS
8239/* compare-sections command
8240
8241 With no arguments, compares each loadable section in the exec bfd
8242 with the same memory range on the target, and reports mismatches.
4a5e7a5b 8243 Useful for verifying the image on the target against the exec file. */
e514a9d6 8244
c906108c 8245static void
fba45db2 8246compare_sections_command (char *args, int from_tty)
c906108c
SS
8247{
8248 asection *s;
c906108c 8249 struct cleanup *old_chain;
085dd6e6 8250 char *sectdata;
ce359b09 8251 const char *sectname;
c906108c
SS
8252 bfd_size_type size;
8253 bfd_vma lma;
8254 int matched = 0;
8255 int mismatched = 0;
4a5e7a5b 8256 int res;
c906108c
SS
8257
8258 if (!exec_bfd)
8a3fe4f8 8259 error (_("command cannot be used without an exec file"));
c906108c 8260
c5aa993b 8261 for (s = exec_bfd->sections; s; s = s->next)
c906108c
SS
8262 {
8263 if (!(s->flags & SEC_LOAD))
0df8b418 8264 continue; /* Skip non-loadable section. */
c906108c 8265
2c500098 8266 size = bfd_get_section_size (s);
c906108c 8267 if (size == 0)
0df8b418 8268 continue; /* Skip zero-length section. */
c906108c 8269
ce359b09 8270 sectname = bfd_get_section_name (exec_bfd, s);
c906108c 8271 if (args && strcmp (args, sectname) != 0)
0df8b418 8272 continue; /* Not the section selected by user. */
c906108c 8273
0df8b418 8274 matched = 1; /* Do this section. */
c906108c 8275 lma = s->lma;
c906108c 8276
c906108c 8277 sectdata = xmalloc (size);
b8c9b27d 8278 old_chain = make_cleanup (xfree, sectdata);
c906108c 8279 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
c906108c 8280
4a5e7a5b
PA
8281 res = target_verify_memory (sectdata, lma, size);
8282
8283 if (res == -1)
5af949e3
UW
8284 error (_("target memory fault, section %s, range %s -- %s"), sectname,
8285 paddress (target_gdbarch, lma),
8286 paddress (target_gdbarch, lma + size));
c906108c 8287
5af949e3
UW
8288 printf_filtered ("Section %s, range %s -- %s: ", sectname,
8289 paddress (target_gdbarch, lma),
8290 paddress (target_gdbarch, lma + size));
4a5e7a5b 8291 if (res)
c906108c
SS
8292 printf_filtered ("matched.\n");
8293 else
c5aa993b
JM
8294 {
8295 printf_filtered ("MIS-MATCHED!\n");
8296 mismatched++;
8297 }
c906108c
SS
8298
8299 do_cleanups (old_chain);
8300 }
8301 if (mismatched > 0)
8a3fe4f8
AC
8302 warning (_("One or more sections of the remote executable does not match\n\
8303the loaded file\n"));
c906108c 8304 if (args && !matched)
a3f17187 8305 printf_filtered (_("No loaded section named '%s'.\n"), args);
c906108c
SS
8306}
8307
0e7f50da
UW
8308/* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
8309 into remote target. The number of bytes written to the remote
8310 target is returned, or -1 for error. */
8311
8312static LONGEST
8313remote_write_qxfer (struct target_ops *ops, const char *object_name,
8314 const char *annex, const gdb_byte *writebuf,
8315 ULONGEST offset, LONGEST len,
8316 struct packet_config *packet)
8317{
8318 int i, buf_len;
8319 ULONGEST n;
0e7f50da
UW
8320 struct remote_state *rs = get_remote_state ();
8321 int max_size = get_memory_write_packet_size ();
8322
8323 if (packet->support == PACKET_DISABLE)
8324 return -1;
8325
8326 /* Insert header. */
8327 i = snprintf (rs->buf, max_size,
8328 "qXfer:%s:write:%s:%s:",
8329 object_name, annex ? annex : "",
8330 phex_nz (offset, sizeof offset));
8331 max_size -= (i + 1);
8332
8333 /* Escape as much data as fits into rs->buf. */
8334 buf_len = remote_escape_output
8335 (writebuf, len, (rs->buf + i), &max_size, max_size);
8336
8337 if (putpkt_binary (rs->buf, i + buf_len) < 0
8338 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
8339 || packet_ok (rs->buf, packet) != PACKET_OK)
8340 return -1;
8341
8342 unpack_varlen_hex (rs->buf, &n);
8343 return n;
8344}
8345
0876f84a
DJ
8346/* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
8347 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
8348 number of bytes read is returned, or 0 for EOF, or -1 for error.
8349 The number of bytes read may be less than LEN without indicating an
8350 EOF. PACKET is checked and updated to indicate whether the remote
8351 target supports this object. */
8352
8353static LONGEST
8354remote_read_qxfer (struct target_ops *ops, const char *object_name,
8355 const char *annex,
8356 gdb_byte *readbuf, ULONGEST offset, LONGEST len,
8357 struct packet_config *packet)
8358{
8359 static char *finished_object;
8360 static char *finished_annex;
8361 static ULONGEST finished_offset;
8362
8363 struct remote_state *rs = get_remote_state ();
0876f84a
DJ
8364 LONGEST i, n, packet_len;
8365
8366 if (packet->support == PACKET_DISABLE)
8367 return -1;
8368
8369 /* Check whether we've cached an end-of-object packet that matches
8370 this request. */
8371 if (finished_object)
8372 {
8373 if (strcmp (object_name, finished_object) == 0
8374 && strcmp (annex ? annex : "", finished_annex) == 0
8375 && offset == finished_offset)
8376 return 0;
8377
8378 /* Otherwise, we're now reading something different. Discard
8379 the cache. */
8380 xfree (finished_object);
8381 xfree (finished_annex);
8382 finished_object = NULL;
8383 finished_annex = NULL;
8384 }
8385
8386 /* Request only enough to fit in a single packet. The actual data
8387 may not, since we don't know how much of it will need to be escaped;
8388 the target is free to respond with slightly less data. We subtract
8389 five to account for the response type and the protocol frame. */
8390 n = min (get_remote_packet_size () - 5, len);
8391 snprintf (rs->buf, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
8392 object_name, annex ? annex : "",
8393 phex_nz (offset, sizeof offset),
8394 phex_nz (n, sizeof n));
8395 i = putpkt (rs->buf);
8396 if (i < 0)
8397 return -1;
8398
8399 rs->buf[0] = '\0';
8400 packet_len = getpkt_sane (&rs->buf, &rs->buf_size, 0);
8401 if (packet_len < 0 || packet_ok (rs->buf, packet) != PACKET_OK)
8402 return -1;
8403
8404 if (rs->buf[0] != 'l' && rs->buf[0] != 'm')
8405 error (_("Unknown remote qXfer reply: %s"), rs->buf);
8406
8407 /* 'm' means there is (or at least might be) more data after this
8408 batch. That does not make sense unless there's at least one byte
8409 of data in this reply. */
8410 if (rs->buf[0] == 'm' && packet_len == 1)
8411 error (_("Remote qXfer reply contained no data."));
8412
8413 /* Got some data. */
8414 i = remote_unescape_input (rs->buf + 1, packet_len - 1, readbuf, n);
8415
8416 /* 'l' is an EOF marker, possibly including a final block of data,
0e7f50da
UW
8417 or possibly empty. If we have the final block of a non-empty
8418 object, record this fact to bypass a subsequent partial read. */
8419 if (rs->buf[0] == 'l' && offset + i > 0)
0876f84a
DJ
8420 {
8421 finished_object = xstrdup (object_name);
8422 finished_annex = xstrdup (annex ? annex : "");
8423 finished_offset = offset + i;
8424 }
8425
8426 return i;
8427}
8428
1e3ff5ad 8429static LONGEST
4b8a223f 8430remote_xfer_partial (struct target_ops *ops, enum target_object object,
961cb7b5
MK
8431 const char *annex, gdb_byte *readbuf,
8432 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
c906108c 8433{
82f73884 8434 struct remote_state *rs;
c906108c 8435 int i;
6d820c5c 8436 char *p2;
1e3ff5ad 8437 char query_type;
c906108c 8438
e6e4e701 8439 set_remote_traceframe ();
82f73884
PA
8440 set_general_thread (inferior_ptid);
8441
8442 rs = get_remote_state ();
8443
b2182ed2 8444 /* Handle memory using the standard memory routines. */
21e3b9b9
DJ
8445 if (object == TARGET_OBJECT_MEMORY)
8446 {
8447 int xfered;
a744cf53 8448
21e3b9b9
DJ
8449 errno = 0;
8450
2d717e4f
DJ
8451 /* If the remote target is connected but not running, we should
8452 pass this request down to a lower stratum (e.g. the executable
8453 file). */
8454 if (!target_has_execution)
8455 return 0;
8456
21e3b9b9 8457 if (writebuf != NULL)
b2182ed2 8458 xfered = remote_write_bytes (offset, writebuf, len);
21e3b9b9 8459 else
b2182ed2 8460 xfered = remote_read_bytes (offset, readbuf, len);
21e3b9b9
DJ
8461
8462 if (xfered > 0)
8463 return xfered;
8464 else if (xfered == 0 && errno == 0)
8465 return 0;
8466 else
8467 return -1;
8468 }
8469
0df8b418 8470 /* Handle SPU memory using qxfer packets. */
0e7f50da
UW
8471 if (object == TARGET_OBJECT_SPU)
8472 {
8473 if (readbuf)
8474 return remote_read_qxfer (ops, "spu", annex, readbuf, offset, len,
8475 &remote_protocol_packets
8476 [PACKET_qXfer_spu_read]);
8477 else
8478 return remote_write_qxfer (ops, "spu", annex, writebuf, offset, len,
8479 &remote_protocol_packets
8480 [PACKET_qXfer_spu_write]);
8481 }
8482
4aa995e1
PA
8483 /* Handle extra signal info using qxfer packets. */
8484 if (object == TARGET_OBJECT_SIGNAL_INFO)
8485 {
8486 if (readbuf)
8487 return remote_read_qxfer (ops, "siginfo", annex, readbuf, offset, len,
8488 &remote_protocol_packets
8489 [PACKET_qXfer_siginfo_read]);
8490 else
3e43a32a
MS
8491 return remote_write_qxfer (ops, "siginfo", annex,
8492 writebuf, offset, len,
4aa995e1
PA
8493 &remote_protocol_packets
8494 [PACKET_qXfer_siginfo_write]);
8495 }
8496
0fb4aa4b
PA
8497 if (object == TARGET_OBJECT_STATIC_TRACE_DATA)
8498 {
8499 if (readbuf)
3e43a32a
MS
8500 return remote_read_qxfer (ops, "statictrace", annex,
8501 readbuf, offset, len,
0fb4aa4b
PA
8502 &remote_protocol_packets
8503 [PACKET_qXfer_statictrace_read]);
8504 else
8505 return -1;
8506 }
8507
a76d924d
DJ
8508 /* Only handle flash writes. */
8509 if (writebuf != NULL)
8510 {
8511 LONGEST xfered;
8512
8513 switch (object)
8514 {
8515 case TARGET_OBJECT_FLASH:
8516 xfered = remote_flash_write (ops, offset, len, writebuf);
8517
8518 if (xfered > 0)
8519 return xfered;
8520 else if (xfered == 0 && errno == 0)
8521 return 0;
8522 else
8523 return -1;
8524
8525 default:
8526 return -1;
8527 }
8528 }
4b8a223f 8529
1e3ff5ad
AC
8530 /* Map pre-existing objects onto letters. DO NOT do this for new
8531 objects!!! Instead specify new query packets. */
8532 switch (object)
c906108c 8533 {
1e3ff5ad
AC
8534 case TARGET_OBJECT_AVR:
8535 query_type = 'R';
8536 break;
802188a7
RM
8537
8538 case TARGET_OBJECT_AUXV:
0876f84a
DJ
8539 gdb_assert (annex == NULL);
8540 return remote_read_qxfer (ops, "auxv", annex, readbuf, offset, len,
8541 &remote_protocol_packets[PACKET_qXfer_auxv]);
802188a7 8542
23181151
DJ
8543 case TARGET_OBJECT_AVAILABLE_FEATURES:
8544 return remote_read_qxfer
8545 (ops, "features", annex, readbuf, offset, len,
8546 &remote_protocol_packets[PACKET_qXfer_features]);
8547
cfa9d6d9
DJ
8548 case TARGET_OBJECT_LIBRARIES:
8549 return remote_read_qxfer
8550 (ops, "libraries", annex, readbuf, offset, len,
8551 &remote_protocol_packets[PACKET_qXfer_libraries]);
8552
2268b414
JK
8553 case TARGET_OBJECT_LIBRARIES_SVR4:
8554 return remote_read_qxfer
8555 (ops, "libraries-svr4", annex, readbuf, offset, len,
8556 &remote_protocol_packets[PACKET_qXfer_libraries_svr4]);
8557
fd79ecee
DJ
8558 case TARGET_OBJECT_MEMORY_MAP:
8559 gdb_assert (annex == NULL);
8560 return remote_read_qxfer (ops, "memory-map", annex, readbuf, offset, len,
8561 &remote_protocol_packets[PACKET_qXfer_memory_map]);
8562
07e059b5
VP
8563 case TARGET_OBJECT_OSDATA:
8564 /* Should only get here if we're connected. */
8565 gdb_assert (remote_desc);
8566 return remote_read_qxfer
8567 (ops, "osdata", annex, readbuf, offset, len,
8568 &remote_protocol_packets[PACKET_qXfer_osdata]);
8569
dc146f7c
VP
8570 case TARGET_OBJECT_THREADS:
8571 gdb_assert (annex == NULL);
8572 return remote_read_qxfer (ops, "threads", annex, readbuf, offset, len,
8573 &remote_protocol_packets[PACKET_qXfer_threads]);
8574
b3b9301e
PA
8575 case TARGET_OBJECT_TRACEFRAME_INFO:
8576 gdb_assert (annex == NULL);
8577 return remote_read_qxfer
8578 (ops, "traceframe-info", annex, readbuf, offset, len,
8579 &remote_protocol_packets[PACKET_qXfer_traceframe_info]);
78d85199
YQ
8580
8581 case TARGET_OBJECT_FDPIC:
8582 return remote_read_qxfer (ops, "fdpic", annex, readbuf, offset, len,
8583 &remote_protocol_packets[PACKET_qXfer_fdpic]);
169081d0
TG
8584
8585 case TARGET_OBJECT_OPENVMS_UIB:
8586 return remote_read_qxfer (ops, "uib", annex, readbuf, offset, len,
8587 &remote_protocol_packets[PACKET_qXfer_uib]);
8588
1e3ff5ad 8589 default:
c906108c
SS
8590 return -1;
8591 }
8592
4b8a223f 8593 /* Note: a zero OFFSET and LEN can be used to query the minimum
1e3ff5ad 8594 buffer size. */
4b8a223f 8595 if (offset == 0 && len == 0)
ea9c271d 8596 return (get_remote_packet_size ());
0df8b418 8597 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
24b06219 8598 large enough let the caller deal with it. */
ea9c271d 8599 if (len < get_remote_packet_size ())
1e3ff5ad 8600 return -1;
ea9c271d 8601 len = get_remote_packet_size ();
1e3ff5ad 8602
23860348 8603 /* Except for querying the minimum buffer size, target must be open. */
c5aa993b 8604 if (!remote_desc)
8a3fe4f8 8605 error (_("remote query is only available after target open"));
c906108c 8606
1e3ff5ad 8607 gdb_assert (annex != NULL);
4b8a223f 8608 gdb_assert (readbuf != NULL);
c906108c 8609
6d820c5c 8610 p2 = rs->buf;
c906108c
SS
8611 *p2++ = 'q';
8612 *p2++ = query_type;
8613
23860348
MS
8614 /* We used one buffer char for the remote protocol q command and
8615 another for the query type. As the remote protocol encapsulation
8616 uses 4 chars plus one extra in case we are debugging
8617 (remote_debug), we have PBUFZIZ - 7 left to pack the query
8618 string. */
c906108c 8619 i = 0;
ea9c271d 8620 while (annex[i] && (i < (get_remote_packet_size () - 8)))
c906108c 8621 {
1e3ff5ad
AC
8622 /* Bad caller may have sent forbidden characters. */
8623 gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#');
8624 *p2++ = annex[i];
c906108c
SS
8625 i++;
8626 }
1e3ff5ad
AC
8627 *p2 = '\0';
8628 gdb_assert (annex[i] == '\0');
c906108c 8629
6d820c5c 8630 i = putpkt (rs->buf);
c5aa993b
JM
8631 if (i < 0)
8632 return i;
c906108c 8633
6d820c5c
DJ
8634 getpkt (&rs->buf, &rs->buf_size, 0);
8635 strcpy ((char *) readbuf, rs->buf);
c906108c 8636
cfd77fa1 8637 return strlen ((char *) readbuf);
c906108c
SS
8638}
8639
08388c79
DE
8640static int
8641remote_search_memory (struct target_ops* ops,
8642 CORE_ADDR start_addr, ULONGEST search_space_len,
8643 const gdb_byte *pattern, ULONGEST pattern_len,
8644 CORE_ADDR *found_addrp)
8645{
5af949e3 8646 int addr_size = gdbarch_addr_bit (target_gdbarch) / 8;
08388c79
DE
8647 struct remote_state *rs = get_remote_state ();
8648 int max_size = get_memory_write_packet_size ();
8649 struct packet_config *packet =
8650 &remote_protocol_packets[PACKET_qSearch_memory];
0df8b418
MS
8651 /* Number of packet bytes used to encode the pattern;
8652 this could be more than PATTERN_LEN due to escape characters. */
08388c79 8653 int escaped_pattern_len;
0df8b418 8654 /* Amount of pattern that was encodable in the packet. */
08388c79
DE
8655 int used_pattern_len;
8656 int i;
8657 int found;
8658 ULONGEST found_addr;
8659
8660 /* Don't go to the target if we don't have to.
8661 This is done before checking packet->support to avoid the possibility that
8662 a success for this edge case means the facility works in general. */
8663 if (pattern_len > search_space_len)
8664 return 0;
8665 if (pattern_len == 0)
8666 {
8667 *found_addrp = start_addr;
8668 return 1;
8669 }
8670
8671 /* If we already know the packet isn't supported, fall back to the simple
8672 way of searching memory. */
8673
8674 if (packet->support == PACKET_DISABLE)
8675 {
8676 /* Target doesn't provided special support, fall back and use the
8677 standard support (copy memory and do the search here). */
8678 return simple_search_memory (ops, start_addr, search_space_len,
8679 pattern, pattern_len, found_addrp);
8680 }
8681
8682 /* Insert header. */
8683 i = snprintf (rs->buf, max_size,
8684 "qSearch:memory:%s;%s;",
5af949e3 8685 phex_nz (start_addr, addr_size),
08388c79
DE
8686 phex_nz (search_space_len, sizeof (search_space_len)));
8687 max_size -= (i + 1);
8688
8689 /* Escape as much data as fits into rs->buf. */
8690 escaped_pattern_len =
8691 remote_escape_output (pattern, pattern_len, (rs->buf + i),
8692 &used_pattern_len, max_size);
8693
8694 /* Bail if the pattern is too large. */
8695 if (used_pattern_len != pattern_len)
9b20d036 8696 error (_("Pattern is too large to transmit to remote target."));
08388c79
DE
8697
8698 if (putpkt_binary (rs->buf, i + escaped_pattern_len) < 0
8699 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
8700 || packet_ok (rs->buf, packet) != PACKET_OK)
8701 {
8702 /* The request may not have worked because the command is not
8703 supported. If so, fall back to the simple way. */
8704 if (packet->support == PACKET_DISABLE)
8705 {
8706 return simple_search_memory (ops, start_addr, search_space_len,
8707 pattern, pattern_len, found_addrp);
8708 }
8709 return -1;
8710 }
8711
8712 if (rs->buf[0] == '0')
8713 found = 0;
8714 else if (rs->buf[0] == '1')
8715 {
8716 found = 1;
8717 if (rs->buf[1] != ',')
10e0fa18 8718 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
08388c79
DE
8719 unpack_varlen_hex (rs->buf + 2, &found_addr);
8720 *found_addrp = found_addr;
8721 }
8722 else
10e0fa18 8723 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
08388c79
DE
8724
8725 return found;
8726}
8727
96baa820
JM
8728static void
8729remote_rcmd (char *command,
d9fcf2fb 8730 struct ui_file *outbuf)
96baa820 8731{
d01949b6 8732 struct remote_state *rs = get_remote_state ();
2e9f7625 8733 char *p = rs->buf;
96baa820
JM
8734
8735 if (!remote_desc)
8a3fe4f8 8736 error (_("remote rcmd is only available after target open"));
96baa820 8737
23860348 8738 /* Send a NULL command across as an empty command. */
7be570e7
JM
8739 if (command == NULL)
8740 command = "";
8741
23860348 8742 /* The query prefix. */
2e9f7625
DJ
8743 strcpy (rs->buf, "qRcmd,");
8744 p = strchr (rs->buf, '\0');
96baa820 8745
3e43a32a
MS
8746 if ((strlen (rs->buf) + strlen (command) * 2 + 8/*misc*/)
8747 > get_remote_packet_size ())
8a3fe4f8 8748 error (_("\"monitor\" command ``%s'' is too long."), command);
96baa820 8749
23860348 8750 /* Encode the actual command. */
cfd77fa1 8751 bin2hex ((gdb_byte *) command, p, 0);
96baa820 8752
6d820c5c 8753 if (putpkt (rs->buf) < 0)
8a3fe4f8 8754 error (_("Communication problem with target."));
96baa820
JM
8755
8756 /* get/display the response */
8757 while (1)
8758 {
2e9f7625
DJ
8759 char *buf;
8760
00bf0b85 8761 /* XXX - see also remote_get_noisy_reply(). */
5b37825d 8762 QUIT; /* Allow user to bail out with ^C. */
2e9f7625 8763 rs->buf[0] = '\0';
5b37825d
PW
8764 if (getpkt_sane (&rs->buf, &rs->buf_size, 0) == -1)
8765 {
8766 /* Timeout. Continue to (try to) read responses.
8767 This is better than stopping with an error, assuming the stub
8768 is still executing the (long) monitor command.
8769 If needed, the user can interrupt gdb using C-c, obtaining
8770 an effect similar to stop on timeout. */
8771 continue;
8772 }
2e9f7625 8773 buf = rs->buf;
96baa820 8774 if (buf[0] == '\0')
8a3fe4f8 8775 error (_("Target does not support this command."));
96baa820
JM
8776 if (buf[0] == 'O' && buf[1] != 'K')
8777 {
23860348 8778 remote_console_output (buf + 1); /* 'O' message from stub. */
96baa820
JM
8779 continue;
8780 }
8781 if (strcmp (buf, "OK") == 0)
8782 break;
7be570e7
JM
8783 if (strlen (buf) == 3 && buf[0] == 'E'
8784 && isdigit (buf[1]) && isdigit (buf[2]))
8785 {
8a3fe4f8 8786 error (_("Protocol error with Rcmd"));
7be570e7 8787 }
96baa820
JM
8788 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
8789 {
8790 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
a744cf53 8791
96baa820
JM
8792 fputc_unfiltered (c, outbuf);
8793 }
8794 break;
8795 }
8796}
8797
fd79ecee
DJ
8798static VEC(mem_region_s) *
8799remote_memory_map (struct target_ops *ops)
8800{
8801 VEC(mem_region_s) *result = NULL;
8802 char *text = target_read_stralloc (&current_target,
8803 TARGET_OBJECT_MEMORY_MAP, NULL);
8804
8805 if (text)
8806 {
8807 struct cleanup *back_to = make_cleanup (xfree, text);
a744cf53 8808
fd79ecee
DJ
8809 result = parse_memory_map (text);
8810 do_cleanups (back_to);
8811 }
8812
8813 return result;
8814}
8815
c906108c 8816static void
fba45db2 8817packet_command (char *args, int from_tty)
c906108c 8818{
d01949b6 8819 struct remote_state *rs = get_remote_state ();
c906108c 8820
c5aa993b 8821 if (!remote_desc)
8a3fe4f8 8822 error (_("command can only be used with remote target"));
c906108c 8823
c5aa993b 8824 if (!args)
8a3fe4f8 8825 error (_("remote-packet command requires packet text as argument"));
c906108c
SS
8826
8827 puts_filtered ("sending: ");
8828 print_packet (args);
8829 puts_filtered ("\n");
8830 putpkt (args);
8831
6d820c5c 8832 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 8833 puts_filtered ("received: ");
6d820c5c 8834 print_packet (rs->buf);
c906108c
SS
8835 puts_filtered ("\n");
8836}
8837
8838#if 0
23860348 8839/* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
c906108c 8840
a14ed312 8841static void display_thread_info (struct gdb_ext_thread_info *info);
c906108c 8842
a14ed312 8843static void threadset_test_cmd (char *cmd, int tty);
c906108c 8844
a14ed312 8845static void threadalive_test (char *cmd, int tty);
c906108c 8846
a14ed312 8847static void threadlist_test_cmd (char *cmd, int tty);
c906108c 8848
23860348 8849int get_and_display_threadinfo (threadref *ref);
c906108c 8850
a14ed312 8851static void threadinfo_test_cmd (char *cmd, int tty);
c906108c 8852
23860348 8853static int thread_display_step (threadref *ref, void *context);
c906108c 8854
a14ed312 8855static void threadlist_update_test_cmd (char *cmd, int tty);
c906108c 8856
a14ed312 8857static void init_remote_threadtests (void);
c906108c 8858
23860348 8859#define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
c906108c
SS
8860
8861static void
fba45db2 8862threadset_test_cmd (char *cmd, int tty)
c906108c
SS
8863{
8864 int sample_thread = SAMPLE_THREAD;
8865
a3f17187 8866 printf_filtered (_("Remote threadset test\n"));
79d7f229 8867 set_general_thread (sample_thread);
c906108c
SS
8868}
8869
8870
8871static void
fba45db2 8872threadalive_test (char *cmd, int tty)
c906108c
SS
8873{
8874 int sample_thread = SAMPLE_THREAD;
79d7f229
PA
8875 int pid = ptid_get_pid (inferior_ptid);
8876 ptid_t ptid = ptid_build (pid, 0, sample_thread);
c906108c 8877
79d7f229 8878 if (remote_thread_alive (ptid))
c906108c
SS
8879 printf_filtered ("PASS: Thread alive test\n");
8880 else
8881 printf_filtered ("FAIL: Thread alive test\n");
8882}
8883
23860348 8884void output_threadid (char *title, threadref *ref);
c906108c
SS
8885
8886void
fba45db2 8887output_threadid (char *title, threadref *ref)
c906108c
SS
8888{
8889 char hexid[20];
8890
23860348 8891 pack_threadid (&hexid[0], ref); /* Convert threead id into hex. */
c906108c
SS
8892 hexid[16] = 0;
8893 printf_filtered ("%s %s\n", title, (&hexid[0]));
8894}
8895
8896static void
fba45db2 8897threadlist_test_cmd (char *cmd, int tty)
c906108c
SS
8898{
8899 int startflag = 1;
8900 threadref nextthread;
8901 int done, result_count;
8902 threadref threadlist[3];
8903
8904 printf_filtered ("Remote Threadlist test\n");
8905 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
8906 &result_count, &threadlist[0]))
8907 printf_filtered ("FAIL: threadlist test\n");
8908 else
8909 {
8910 threadref *scan = threadlist;
8911 threadref *limit = scan + result_count;
8912
8913 while (scan < limit)
8914 output_threadid (" thread ", scan++);
8915 }
8916}
8917
8918void
fba45db2 8919display_thread_info (struct gdb_ext_thread_info *info)
c906108c
SS
8920{
8921 output_threadid ("Threadid: ", &info->threadid);
8922 printf_filtered ("Name: %s\n ", info->shortname);
8923 printf_filtered ("State: %s\n", info->display);
8924 printf_filtered ("other: %s\n\n", info->more_display);
8925}
8926
8927int
fba45db2 8928get_and_display_threadinfo (threadref *ref)
c906108c
SS
8929{
8930 int result;
8931 int set;
8932 struct gdb_ext_thread_info threadinfo;
8933
8934 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
8935 | TAG_MOREDISPLAY | TAG_DISPLAY;
8936 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
8937 display_thread_info (&threadinfo);
8938 return result;
8939}
8940
8941static void
fba45db2 8942threadinfo_test_cmd (char *cmd, int tty)
c906108c
SS
8943{
8944 int athread = SAMPLE_THREAD;
8945 threadref thread;
8946 int set;
8947
8948 int_to_threadref (&thread, athread);
8949 printf_filtered ("Remote Threadinfo test\n");
8950 if (!get_and_display_threadinfo (&thread))
8951 printf_filtered ("FAIL cannot get thread info\n");
8952}
8953
8954static int
fba45db2 8955thread_display_step (threadref *ref, void *context)
c906108c
SS
8956{
8957 /* output_threadid(" threadstep ",ref); *//* simple test */
8958 return get_and_display_threadinfo (ref);
8959}
8960
8961static void
fba45db2 8962threadlist_update_test_cmd (char *cmd, int tty)
c906108c
SS
8963{
8964 printf_filtered ("Remote Threadlist update test\n");
8965 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
8966}
8967
8968static void
8969init_remote_threadtests (void)
8970{
3e43a32a
MS
8971 add_com ("tlist", class_obscure, threadlist_test_cmd,
8972 _("Fetch and print the remote list of "
8973 "thread identifiers, one pkt only"));
c906108c 8974 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
1bedd215 8975 _("Fetch and display info about one thread"));
c906108c 8976 add_com ("tset", class_obscure, threadset_test_cmd,
1bedd215 8977 _("Test setting to a different thread"));
c906108c 8978 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
1bedd215 8979 _("Iterate through updating all remote thread info"));
c906108c 8980 add_com ("talive", class_obscure, threadalive_test,
1bedd215 8981 _(" Remote thread alive test "));
c906108c
SS
8982}
8983
8984#endif /* 0 */
8985
f3fb8c85
MS
8986/* Convert a thread ID to a string. Returns the string in a static
8987 buffer. */
8988
8989static char *
117de6a9 8990remote_pid_to_str (struct target_ops *ops, ptid_t ptid)
f3fb8c85 8991{
79d7f229 8992 static char buf[64];
82f73884 8993 struct remote_state *rs = get_remote_state ();
f3fb8c85 8994
7cee1e54
PA
8995 if (ptid_equal (ptid, null_ptid))
8996 return normal_pid_to_str (ptid);
8997 else if (ptid_is_pid (ptid))
ecd0ada5
PA
8998 {
8999 /* Printing an inferior target id. */
9000
9001 /* When multi-process extensions are off, there's no way in the
9002 remote protocol to know the remote process id, if there's any
9003 at all. There's one exception --- when we're connected with
9004 target extended-remote, and we manually attached to a process
9005 with "attach PID". We don't record anywhere a flag that
9006 allows us to distinguish that case from the case of
9007 connecting with extended-remote and the stub already being
9008 attached to a process, and reporting yes to qAttached, hence
9009 no smart special casing here. */
9010 if (!remote_multi_process_p (rs))
9011 {
9012 xsnprintf (buf, sizeof buf, "Remote target");
9013 return buf;
9014 }
9015
9016 return normal_pid_to_str (ptid);
82f73884 9017 }
ecd0ada5 9018 else
79d7f229 9019 {
ecd0ada5
PA
9020 if (ptid_equal (magic_null_ptid, ptid))
9021 xsnprintf (buf, sizeof buf, "Thread <main>");
901f9912 9022 else if (rs->extended && remote_multi_process_p (rs))
ecd0ada5
PA
9023 xsnprintf (buf, sizeof buf, "Thread %d.%ld",
9024 ptid_get_pid (ptid), ptid_get_tid (ptid));
9025 else
9026 xsnprintf (buf, sizeof buf, "Thread %ld",
9027 ptid_get_tid (ptid));
79d7f229
PA
9028 return buf;
9029 }
f3fb8c85
MS
9030}
9031
38691318
KB
9032/* Get the address of the thread local variable in OBJFILE which is
9033 stored at OFFSET within the thread local storage for thread PTID. */
9034
9035static CORE_ADDR
117de6a9
PA
9036remote_get_thread_local_address (struct target_ops *ops,
9037 ptid_t ptid, CORE_ADDR lm, CORE_ADDR offset)
38691318 9038{
444abaca 9039 if (remote_protocol_packets[PACKET_qGetTLSAddr].support != PACKET_DISABLE)
38691318
KB
9040 {
9041 struct remote_state *rs = get_remote_state ();
6d820c5c 9042 char *p = rs->buf;
82f73884 9043 char *endp = rs->buf + get_remote_packet_size ();
571dd617 9044 enum packet_result result;
38691318
KB
9045
9046 strcpy (p, "qGetTLSAddr:");
9047 p += strlen (p);
82f73884 9048 p = write_ptid (p, endp, ptid);
38691318
KB
9049 *p++ = ',';
9050 p += hexnumstr (p, offset);
9051 *p++ = ',';
9052 p += hexnumstr (p, lm);
9053 *p++ = '\0';
9054
6d820c5c
DJ
9055 putpkt (rs->buf);
9056 getpkt (&rs->buf, &rs->buf_size, 0);
3e43a32a
MS
9057 result = packet_ok (rs->buf,
9058 &remote_protocol_packets[PACKET_qGetTLSAddr]);
571dd617 9059 if (result == PACKET_OK)
38691318
KB
9060 {
9061 ULONGEST result;
9062
6d820c5c 9063 unpack_varlen_hex (rs->buf, &result);
38691318
KB
9064 return result;
9065 }
571dd617 9066 else if (result == PACKET_UNKNOWN)
109c3e39
AC
9067 throw_error (TLS_GENERIC_ERROR,
9068 _("Remote target doesn't support qGetTLSAddr packet"));
38691318 9069 else
109c3e39
AC
9070 throw_error (TLS_GENERIC_ERROR,
9071 _("Remote target failed to process qGetTLSAddr request"));
38691318
KB
9072 }
9073 else
109c3e39
AC
9074 throw_error (TLS_GENERIC_ERROR,
9075 _("TLS not supported or disabled on this target"));
38691318
KB
9076 /* Not reached. */
9077 return 0;
9078}
9079
711e434b
PM
9080/* Provide thread local base, i.e. Thread Information Block address.
9081 Returns 1 if ptid is found and thread_local_base is non zero. */
9082
70221824 9083static int
711e434b
PM
9084remote_get_tib_address (ptid_t ptid, CORE_ADDR *addr)
9085{
9086 if (remote_protocol_packets[PACKET_qGetTIBAddr].support != PACKET_DISABLE)
9087 {
9088 struct remote_state *rs = get_remote_state ();
9089 char *p = rs->buf;
9090 char *endp = rs->buf + get_remote_packet_size ();
9091 enum packet_result result;
9092
9093 strcpy (p, "qGetTIBAddr:");
9094 p += strlen (p);
9095 p = write_ptid (p, endp, ptid);
9096 *p++ = '\0';
9097
9098 putpkt (rs->buf);
9099 getpkt (&rs->buf, &rs->buf_size, 0);
9100 result = packet_ok (rs->buf,
9101 &remote_protocol_packets[PACKET_qGetTIBAddr]);
9102 if (result == PACKET_OK)
9103 {
9104 ULONGEST result;
9105
9106 unpack_varlen_hex (rs->buf, &result);
9107 if (addr)
9108 *addr = (CORE_ADDR) result;
9109 return 1;
9110 }
9111 else if (result == PACKET_UNKNOWN)
9112 error (_("Remote target doesn't support qGetTIBAddr packet"));
9113 else
9114 error (_("Remote target failed to process qGetTIBAddr request"));
9115 }
9116 else
9117 error (_("qGetTIBAddr not supported or disabled on this target"));
9118 /* Not reached. */
9119 return 0;
9120}
9121
29709017
DJ
9122/* Support for inferring a target description based on the current
9123 architecture and the size of a 'g' packet. While the 'g' packet
9124 can have any size (since optional registers can be left off the
9125 end), some sizes are easily recognizable given knowledge of the
9126 approximate architecture. */
9127
9128struct remote_g_packet_guess
9129{
9130 int bytes;
9131 const struct target_desc *tdesc;
9132};
9133typedef struct remote_g_packet_guess remote_g_packet_guess_s;
9134DEF_VEC_O(remote_g_packet_guess_s);
9135
9136struct remote_g_packet_data
9137{
9138 VEC(remote_g_packet_guess_s) *guesses;
9139};
9140
9141static struct gdbarch_data *remote_g_packet_data_handle;
9142
9143static void *
9144remote_g_packet_data_init (struct obstack *obstack)
9145{
9146 return OBSTACK_ZALLOC (obstack, struct remote_g_packet_data);
9147}
9148
9149void
9150register_remote_g_packet_guess (struct gdbarch *gdbarch, int bytes,
9151 const struct target_desc *tdesc)
9152{
9153 struct remote_g_packet_data *data
9154 = gdbarch_data (gdbarch, remote_g_packet_data_handle);
9155 struct remote_g_packet_guess new_guess, *guess;
9156 int ix;
9157
9158 gdb_assert (tdesc != NULL);
9159
9160 for (ix = 0;
9161 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
9162 ix++)
9163 if (guess->bytes == bytes)
9164 internal_error (__FILE__, __LINE__,
9b20d036 9165 _("Duplicate g packet description added for size %d"),
29709017
DJ
9166 bytes);
9167
9168 new_guess.bytes = bytes;
9169 new_guess.tdesc = tdesc;
9170 VEC_safe_push (remote_g_packet_guess_s, data->guesses, &new_guess);
9171}
9172
d962ef82
DJ
9173/* Return 1 if remote_read_description would do anything on this target
9174 and architecture, 0 otherwise. */
9175
9176static int
9177remote_read_description_p (struct target_ops *target)
9178{
9179 struct remote_g_packet_data *data
9180 = gdbarch_data (target_gdbarch, remote_g_packet_data_handle);
9181
9182 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
9183 return 1;
9184
9185 return 0;
9186}
9187
29709017
DJ
9188static const struct target_desc *
9189remote_read_description (struct target_ops *target)
9190{
9191 struct remote_g_packet_data *data
1cf3db46 9192 = gdbarch_data (target_gdbarch, remote_g_packet_data_handle);
29709017 9193
d962ef82
DJ
9194 /* Do not try this during initial connection, when we do not know
9195 whether there is a running but stopped thread. */
9196 if (!target_has_execution || ptid_equal (inferior_ptid, null_ptid))
9197 return NULL;
9198
29709017
DJ
9199 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
9200 {
9201 struct remote_g_packet_guess *guess;
9202 int ix;
9203 int bytes = send_g_packet ();
9204
9205 for (ix = 0;
9206 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
9207 ix++)
9208 if (guess->bytes == bytes)
9209 return guess->tdesc;
9210
9211 /* We discard the g packet. A minor optimization would be to
9212 hold on to it, and fill the register cache once we have selected
9213 an architecture, but it's too tricky to do safely. */
9214 }
9215
9216 return NULL;
9217}
9218
a6b151f1
DJ
9219/* Remote file transfer support. This is host-initiated I/O, not
9220 target-initiated; for target-initiated, see remote-fileio.c. */
9221
9222/* If *LEFT is at least the length of STRING, copy STRING to
9223 *BUFFER, update *BUFFER to point to the new end of the buffer, and
9224 decrease *LEFT. Otherwise raise an error. */
9225
9226static void
9227remote_buffer_add_string (char **buffer, int *left, char *string)
9228{
9229 int len = strlen (string);
9230
9231 if (len > *left)
9232 error (_("Packet too long for target."));
9233
9234 memcpy (*buffer, string, len);
9235 *buffer += len;
9236 *left -= len;
9237
9238 /* NUL-terminate the buffer as a convenience, if there is
9239 room. */
9240 if (*left)
9241 **buffer = '\0';
9242}
9243
9244/* If *LEFT is large enough, hex encode LEN bytes from BYTES into
9245 *BUFFER, update *BUFFER to point to the new end of the buffer, and
9246 decrease *LEFT. Otherwise raise an error. */
9247
9248static void
9249remote_buffer_add_bytes (char **buffer, int *left, const gdb_byte *bytes,
9250 int len)
9251{
9252 if (2 * len > *left)
9253 error (_("Packet too long for target."));
9254
9255 bin2hex (bytes, *buffer, len);
9256 *buffer += 2 * len;
9257 *left -= 2 * len;
9258
9259 /* NUL-terminate the buffer as a convenience, if there is
9260 room. */
9261 if (*left)
9262 **buffer = '\0';
9263}
9264
9265/* If *LEFT is large enough, convert VALUE to hex and add it to
9266 *BUFFER, update *BUFFER to point to the new end of the buffer, and
9267 decrease *LEFT. Otherwise raise an error. */
9268
9269static void
9270remote_buffer_add_int (char **buffer, int *left, ULONGEST value)
9271{
9272 int len = hexnumlen (value);
9273
9274 if (len > *left)
9275 error (_("Packet too long for target."));
9276
9277 hexnumstr (*buffer, value);
9278 *buffer += len;
9279 *left -= len;
9280
9281 /* NUL-terminate the buffer as a convenience, if there is
9282 room. */
9283 if (*left)
9284 **buffer = '\0';
9285}
9286
9287/* Parse an I/O result packet from BUFFER. Set RETCODE to the return
9288 value, *REMOTE_ERRNO to the remote error number or zero if none
9289 was included, and *ATTACHMENT to point to the start of the annex
9290 if any. The length of the packet isn't needed here; there may
9291 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
9292
9293 Return 0 if the packet could be parsed, -1 if it could not. If
9294 -1 is returned, the other variables may not be initialized. */
9295
9296static int
9297remote_hostio_parse_result (char *buffer, int *retcode,
9298 int *remote_errno, char **attachment)
9299{
9300 char *p, *p2;
9301
9302 *remote_errno = 0;
9303 *attachment = NULL;
9304
9305 if (buffer[0] != 'F')
9306 return -1;
9307
9308 errno = 0;
9309 *retcode = strtol (&buffer[1], &p, 16);
9310 if (errno != 0 || p == &buffer[1])
9311 return -1;
9312
9313 /* Check for ",errno". */
9314 if (*p == ',')
9315 {
9316 errno = 0;
9317 *remote_errno = strtol (p + 1, &p2, 16);
9318 if (errno != 0 || p + 1 == p2)
9319 return -1;
9320 p = p2;
9321 }
9322
9323 /* Check for ";attachment". If there is no attachment, the
9324 packet should end here. */
9325 if (*p == ';')
9326 {
9327 *attachment = p + 1;
9328 return 0;
9329 }
9330 else if (*p == '\0')
9331 return 0;
9332 else
9333 return -1;
9334}
9335
9336/* Send a prepared I/O packet to the target and read its response.
9337 The prepared packet is in the global RS->BUF before this function
9338 is called, and the answer is there when we return.
9339
9340 COMMAND_BYTES is the length of the request to send, which may include
9341 binary data. WHICH_PACKET is the packet configuration to check
9342 before attempting a packet. If an error occurs, *REMOTE_ERRNO
9343 is set to the error number and -1 is returned. Otherwise the value
9344 returned by the function is returned.
9345
9346 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
9347 attachment is expected; an error will be reported if there's a
9348 mismatch. If one is found, *ATTACHMENT will be set to point into
9349 the packet buffer and *ATTACHMENT_LEN will be set to the
9350 attachment's length. */
9351
9352static int
9353remote_hostio_send_command (int command_bytes, int which_packet,
9354 int *remote_errno, char **attachment,
9355 int *attachment_len)
9356{
9357 struct remote_state *rs = get_remote_state ();
9358 int ret, bytes_read;
9359 char *attachment_tmp;
9360
f1838a98
UW
9361 if (!remote_desc
9362 || remote_protocol_packets[which_packet].support == PACKET_DISABLE)
a6b151f1
DJ
9363 {
9364 *remote_errno = FILEIO_ENOSYS;
9365 return -1;
9366 }
9367
9368 putpkt_binary (rs->buf, command_bytes);
9369 bytes_read = getpkt_sane (&rs->buf, &rs->buf_size, 0);
9370
9371 /* If it timed out, something is wrong. Don't try to parse the
9372 buffer. */
9373 if (bytes_read < 0)
9374 {
9375 *remote_errno = FILEIO_EINVAL;
9376 return -1;
9377 }
9378
9379 switch (packet_ok (rs->buf, &remote_protocol_packets[which_packet]))
9380 {
9381 case PACKET_ERROR:
9382 *remote_errno = FILEIO_EINVAL;
9383 return -1;
9384 case PACKET_UNKNOWN:
9385 *remote_errno = FILEIO_ENOSYS;
9386 return -1;
9387 case PACKET_OK:
9388 break;
9389 }
9390
9391 if (remote_hostio_parse_result (rs->buf, &ret, remote_errno,
9392 &attachment_tmp))
9393 {
9394 *remote_errno = FILEIO_EINVAL;
9395 return -1;
9396 }
9397
9398 /* Make sure we saw an attachment if and only if we expected one. */
9399 if ((attachment_tmp == NULL && attachment != NULL)
9400 || (attachment_tmp != NULL && attachment == NULL))
9401 {
9402 *remote_errno = FILEIO_EINVAL;
9403 return -1;
9404 }
9405
9406 /* If an attachment was found, it must point into the packet buffer;
9407 work out how many bytes there were. */
9408 if (attachment_tmp != NULL)
9409 {
9410 *attachment = attachment_tmp;
9411 *attachment_len = bytes_read - (*attachment - rs->buf);
9412 }
9413
9414 return ret;
9415}
9416
9417/* Open FILENAME on the remote target, using FLAGS and MODE. Return a
9418 remote file descriptor, or -1 if an error occurs (and set
9419 *REMOTE_ERRNO). */
9420
9421static int
9422remote_hostio_open (const char *filename, int flags, int mode,
9423 int *remote_errno)
9424{
9425 struct remote_state *rs = get_remote_state ();
9426 char *p = rs->buf;
9427 int left = get_remote_packet_size () - 1;
9428
9429 remote_buffer_add_string (&p, &left, "vFile:open:");
9430
9431 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
9432 strlen (filename));
9433 remote_buffer_add_string (&p, &left, ",");
9434
9435 remote_buffer_add_int (&p, &left, flags);
9436 remote_buffer_add_string (&p, &left, ",");
9437
9438 remote_buffer_add_int (&p, &left, mode);
9439
9440 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_open,
9441 remote_errno, NULL, NULL);
9442}
9443
9444/* Write up to LEN bytes from WRITE_BUF to FD on the remote target.
9445 Return the number of bytes written, or -1 if an error occurs (and
9446 set *REMOTE_ERRNO). */
9447
9448static int
9449remote_hostio_pwrite (int fd, const gdb_byte *write_buf, int len,
9450 ULONGEST offset, int *remote_errno)
9451{
9452 struct remote_state *rs = get_remote_state ();
9453 char *p = rs->buf;
9454 int left = get_remote_packet_size ();
9455 int out_len;
9456
9457 remote_buffer_add_string (&p, &left, "vFile:pwrite:");
9458
9459 remote_buffer_add_int (&p, &left, fd);
9460 remote_buffer_add_string (&p, &left, ",");
9461
9462 remote_buffer_add_int (&p, &left, offset);
9463 remote_buffer_add_string (&p, &left, ",");
9464
9465 p += remote_escape_output (write_buf, len, p, &out_len,
9466 get_remote_packet_size () - (p - rs->buf));
9467
9468 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_pwrite,
9469 remote_errno, NULL, NULL);
9470}
9471
9472/* Read up to LEN bytes FD on the remote target into READ_BUF
9473 Return the number of bytes read, or -1 if an error occurs (and
9474 set *REMOTE_ERRNO). */
9475
9476static int
9477remote_hostio_pread (int fd, gdb_byte *read_buf, int len,
9478 ULONGEST offset, int *remote_errno)
9479{
9480 struct remote_state *rs = get_remote_state ();
9481 char *p = rs->buf;
9482 char *attachment;
9483 int left = get_remote_packet_size ();
9484 int ret, attachment_len;
9485 int read_len;
9486
9487 remote_buffer_add_string (&p, &left, "vFile:pread:");
9488
9489 remote_buffer_add_int (&p, &left, fd);
9490 remote_buffer_add_string (&p, &left, ",");
9491
9492 remote_buffer_add_int (&p, &left, len);
9493 remote_buffer_add_string (&p, &left, ",");
9494
9495 remote_buffer_add_int (&p, &left, offset);
9496
9497 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_pread,
9498 remote_errno, &attachment,
9499 &attachment_len);
9500
9501 if (ret < 0)
9502 return ret;
9503
9504 read_len = remote_unescape_input (attachment, attachment_len,
9505 read_buf, len);
9506 if (read_len != ret)
9507 error (_("Read returned %d, but %d bytes."), ret, (int) read_len);
9508
9509 return ret;
9510}
9511
9512/* Close FD on the remote target. Return 0, or -1 if an error occurs
9513 (and set *REMOTE_ERRNO). */
9514
9515static int
9516remote_hostio_close (int fd, int *remote_errno)
9517{
9518 struct remote_state *rs = get_remote_state ();
9519 char *p = rs->buf;
9520 int left = get_remote_packet_size () - 1;
9521
9522 remote_buffer_add_string (&p, &left, "vFile:close:");
9523
9524 remote_buffer_add_int (&p, &left, fd);
9525
9526 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_close,
9527 remote_errno, NULL, NULL);
9528}
9529
9530/* Unlink FILENAME on the remote target. Return 0, or -1 if an error
9531 occurs (and set *REMOTE_ERRNO). */
9532
9533static int
9534remote_hostio_unlink (const char *filename, int *remote_errno)
9535{
9536 struct remote_state *rs = get_remote_state ();
9537 char *p = rs->buf;
9538 int left = get_remote_packet_size () - 1;
9539
9540 remote_buffer_add_string (&p, &left, "vFile:unlink:");
9541
9542 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
9543 strlen (filename));
9544
9545 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_unlink,
9546 remote_errno, NULL, NULL);
9547}
9548
b9e7b9c3
UW
9549/* Read value of symbolic link FILENAME on the remote target. Return
9550 a null-terminated string allocated via xmalloc, or NULL if an error
9551 occurs (and set *REMOTE_ERRNO). */
9552
9553static char *
9554remote_hostio_readlink (const char *filename, int *remote_errno)
9555{
9556 struct remote_state *rs = get_remote_state ();
9557 char *p = rs->buf;
9558 char *attachment;
9559 int left = get_remote_packet_size ();
9560 int len, attachment_len;
9561 int read_len;
9562 char *ret;
9563
9564 remote_buffer_add_string (&p, &left, "vFile:readlink:");
9565
9566 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
9567 strlen (filename));
9568
9569 len = remote_hostio_send_command (p - rs->buf, PACKET_vFile_readlink,
9570 remote_errno, &attachment,
9571 &attachment_len);
9572
9573 if (len < 0)
9574 return NULL;
9575
9576 ret = xmalloc (len + 1);
9577
9578 read_len = remote_unescape_input (attachment, attachment_len,
9579 ret, len);
9580 if (read_len != len)
9581 error (_("Readlink returned %d, but %d bytes."), len, read_len);
9582
9583 ret[len] = '\0';
9584 return ret;
9585}
9586
a6b151f1
DJ
9587static int
9588remote_fileio_errno_to_host (int errnum)
9589{
9590 switch (errnum)
9591 {
9592 case FILEIO_EPERM:
9593 return EPERM;
9594 case FILEIO_ENOENT:
9595 return ENOENT;
9596 case FILEIO_EINTR:
9597 return EINTR;
9598 case FILEIO_EIO:
9599 return EIO;
9600 case FILEIO_EBADF:
9601 return EBADF;
9602 case FILEIO_EACCES:
9603 return EACCES;
9604 case FILEIO_EFAULT:
9605 return EFAULT;
9606 case FILEIO_EBUSY:
9607 return EBUSY;
9608 case FILEIO_EEXIST:
9609 return EEXIST;
9610 case FILEIO_ENODEV:
9611 return ENODEV;
9612 case FILEIO_ENOTDIR:
9613 return ENOTDIR;
9614 case FILEIO_EISDIR:
9615 return EISDIR;
9616 case FILEIO_EINVAL:
9617 return EINVAL;
9618 case FILEIO_ENFILE:
9619 return ENFILE;
9620 case FILEIO_EMFILE:
9621 return EMFILE;
9622 case FILEIO_EFBIG:
9623 return EFBIG;
9624 case FILEIO_ENOSPC:
9625 return ENOSPC;
9626 case FILEIO_ESPIPE:
9627 return ESPIPE;
9628 case FILEIO_EROFS:
9629 return EROFS;
9630 case FILEIO_ENOSYS:
9631 return ENOSYS;
9632 case FILEIO_ENAMETOOLONG:
9633 return ENAMETOOLONG;
9634 }
9635 return -1;
9636}
9637
9638static char *
9639remote_hostio_error (int errnum)
9640{
9641 int host_error = remote_fileio_errno_to_host (errnum);
9642
9643 if (host_error == -1)
9644 error (_("Unknown remote I/O error %d"), errnum);
9645 else
9646 error (_("Remote I/O error: %s"), safe_strerror (host_error));
9647}
9648
a6b151f1
DJ
9649static void
9650remote_hostio_close_cleanup (void *opaque)
9651{
9652 int fd = *(int *) opaque;
9653 int remote_errno;
9654
9655 remote_hostio_close (fd, &remote_errno);
9656}
9657
f1838a98
UW
9658
9659static void *
9660remote_bfd_iovec_open (struct bfd *abfd, void *open_closure)
9661{
9662 const char *filename = bfd_get_filename (abfd);
9663 int fd, remote_errno;
9664 int *stream;
9665
9666 gdb_assert (remote_filename_p (filename));
9667
9668 fd = remote_hostio_open (filename + 7, FILEIO_O_RDONLY, 0, &remote_errno);
9669 if (fd == -1)
9670 {
9671 errno = remote_fileio_errno_to_host (remote_errno);
9672 bfd_set_error (bfd_error_system_call);
9673 return NULL;
9674 }
9675
9676 stream = xmalloc (sizeof (int));
9677 *stream = fd;
9678 return stream;
9679}
9680
9681static int
9682remote_bfd_iovec_close (struct bfd *abfd, void *stream)
9683{
9684 int fd = *(int *)stream;
9685 int remote_errno;
9686
9687 xfree (stream);
9688
9689 /* Ignore errors on close; these may happen if the remote
9690 connection was already torn down. */
9691 remote_hostio_close (fd, &remote_errno);
9692
9693 return 1;
9694}
9695
9696static file_ptr
9697remote_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
9698 file_ptr nbytes, file_ptr offset)
9699{
9700 int fd = *(int *)stream;
9701 int remote_errno;
9702 file_ptr pos, bytes;
9703
9704 pos = 0;
9705 while (nbytes > pos)
9706 {
9707 bytes = remote_hostio_pread (fd, (char *)buf + pos, nbytes - pos,
9708 offset + pos, &remote_errno);
9709 if (bytes == 0)
9710 /* Success, but no bytes, means end-of-file. */
9711 break;
9712 if (bytes == -1)
9713 {
9714 errno = remote_fileio_errno_to_host (remote_errno);
9715 bfd_set_error (bfd_error_system_call);
9716 return -1;
9717 }
9718
9719 pos += bytes;
9720 }
9721
9722 return pos;
9723}
9724
9725static int
9726remote_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
9727{
9728 /* FIXME: We should probably implement remote_hostio_stat. */
9729 sb->st_size = INT_MAX;
9730 return 0;
9731}
9732
9733int
9734remote_filename_p (const char *filename)
9735{
9736 return strncmp (filename, "remote:", 7) == 0;
9737}
9738
9739bfd *
9740remote_bfd_open (const char *remote_file, const char *target)
9741{
9742 return bfd_openr_iovec (remote_file, target,
9743 remote_bfd_iovec_open, NULL,
9744 remote_bfd_iovec_pread,
9745 remote_bfd_iovec_close,
9746 remote_bfd_iovec_stat);
9747}
9748
a6b151f1
DJ
9749void
9750remote_file_put (const char *local_file, const char *remote_file, int from_tty)
9751{
9752 struct cleanup *back_to, *close_cleanup;
9753 int retcode, fd, remote_errno, bytes, io_size;
9754 FILE *file;
9755 gdb_byte *buffer;
9756 int bytes_in_buffer;
9757 int saw_eof;
9758 ULONGEST offset;
9759
9760 if (!remote_desc)
9761 error (_("command can only be used with remote target"));
9762
9763 file = fopen (local_file, "rb");
9764 if (file == NULL)
9765 perror_with_name (local_file);
7c8a8b04 9766 back_to = make_cleanup_fclose (file);
a6b151f1
DJ
9767
9768 fd = remote_hostio_open (remote_file, (FILEIO_O_WRONLY | FILEIO_O_CREAT
9769 | FILEIO_O_TRUNC),
9770 0700, &remote_errno);
9771 if (fd == -1)
9772 remote_hostio_error (remote_errno);
9773
9774 /* Send up to this many bytes at once. They won't all fit in the
9775 remote packet limit, so we'll transfer slightly fewer. */
9776 io_size = get_remote_packet_size ();
9777 buffer = xmalloc (io_size);
9778 make_cleanup (xfree, buffer);
9779
9780 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
9781
9782 bytes_in_buffer = 0;
9783 saw_eof = 0;
9784 offset = 0;
9785 while (bytes_in_buffer || !saw_eof)
9786 {
9787 if (!saw_eof)
9788 {
3e43a32a
MS
9789 bytes = fread (buffer + bytes_in_buffer, 1,
9790 io_size - bytes_in_buffer,
a6b151f1
DJ
9791 file);
9792 if (bytes == 0)
9793 {
9794 if (ferror (file))
9795 error (_("Error reading %s."), local_file);
9796 else
9797 {
9798 /* EOF. Unless there is something still in the
9799 buffer from the last iteration, we are done. */
9800 saw_eof = 1;
9801 if (bytes_in_buffer == 0)
9802 break;
9803 }
9804 }
9805 }
9806 else
9807 bytes = 0;
9808
9809 bytes += bytes_in_buffer;
9810 bytes_in_buffer = 0;
9811
3e43a32a
MS
9812 retcode = remote_hostio_pwrite (fd, buffer, bytes,
9813 offset, &remote_errno);
a6b151f1
DJ
9814
9815 if (retcode < 0)
9816 remote_hostio_error (remote_errno);
9817 else if (retcode == 0)
9818 error (_("Remote write of %d bytes returned 0!"), bytes);
9819 else if (retcode < bytes)
9820 {
9821 /* Short write. Save the rest of the read data for the next
9822 write. */
9823 bytes_in_buffer = bytes - retcode;
9824 memmove (buffer, buffer + retcode, bytes_in_buffer);
9825 }
9826
9827 offset += retcode;
9828 }
9829
9830 discard_cleanups (close_cleanup);
9831 if (remote_hostio_close (fd, &remote_errno))
9832 remote_hostio_error (remote_errno);
9833
9834 if (from_tty)
9835 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file);
9836 do_cleanups (back_to);
9837}
9838
9839void
9840remote_file_get (const char *remote_file, const char *local_file, int from_tty)
9841{
9842 struct cleanup *back_to, *close_cleanup;
cea39f65 9843 int fd, remote_errno, bytes, io_size;
a6b151f1
DJ
9844 FILE *file;
9845 gdb_byte *buffer;
9846 ULONGEST offset;
9847
9848 if (!remote_desc)
9849 error (_("command can only be used with remote target"));
9850
9851 fd = remote_hostio_open (remote_file, FILEIO_O_RDONLY, 0, &remote_errno);
9852 if (fd == -1)
9853 remote_hostio_error (remote_errno);
9854
9855 file = fopen (local_file, "wb");
9856 if (file == NULL)
9857 perror_with_name (local_file);
7c8a8b04 9858 back_to = make_cleanup_fclose (file);
a6b151f1
DJ
9859
9860 /* Send up to this many bytes at once. They won't all fit in the
9861 remote packet limit, so we'll transfer slightly fewer. */
9862 io_size = get_remote_packet_size ();
9863 buffer = xmalloc (io_size);
9864 make_cleanup (xfree, buffer);
9865
9866 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
9867
9868 offset = 0;
9869 while (1)
9870 {
9871 bytes = remote_hostio_pread (fd, buffer, io_size, offset, &remote_errno);
9872 if (bytes == 0)
9873 /* Success, but no bytes, means end-of-file. */
9874 break;
9875 if (bytes == -1)
9876 remote_hostio_error (remote_errno);
9877
9878 offset += bytes;
9879
9880 bytes = fwrite (buffer, 1, bytes, file);
9881 if (bytes == 0)
9882 perror_with_name (local_file);
9883 }
9884
9885 discard_cleanups (close_cleanup);
9886 if (remote_hostio_close (fd, &remote_errno))
9887 remote_hostio_error (remote_errno);
9888
9889 if (from_tty)
9890 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file);
9891 do_cleanups (back_to);
9892}
9893
9894void
9895remote_file_delete (const char *remote_file, int from_tty)
9896{
9897 int retcode, remote_errno;
9898
9899 if (!remote_desc)
9900 error (_("command can only be used with remote target"));
9901
9902 retcode = remote_hostio_unlink (remote_file, &remote_errno);
9903 if (retcode == -1)
9904 remote_hostio_error (remote_errno);
9905
9906 if (from_tty)
9907 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file);
9908}
9909
9910static void
9911remote_put_command (char *args, int from_tty)
9912{
9913 struct cleanup *back_to;
9914 char **argv;
9915
d1a41061
PP
9916 if (args == NULL)
9917 error_no_arg (_("file to put"));
9918
9919 argv = gdb_buildargv (args);
a6b151f1
DJ
9920 back_to = make_cleanup_freeargv (argv);
9921 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
9922 error (_("Invalid parameters to remote put"));
9923
9924 remote_file_put (argv[0], argv[1], from_tty);
9925
9926 do_cleanups (back_to);
9927}
9928
9929static void
9930remote_get_command (char *args, int from_tty)
9931{
9932 struct cleanup *back_to;
9933 char **argv;
9934
d1a41061
PP
9935 if (args == NULL)
9936 error_no_arg (_("file to get"));
9937
9938 argv = gdb_buildargv (args);
a6b151f1
DJ
9939 back_to = make_cleanup_freeargv (argv);
9940 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
9941 error (_("Invalid parameters to remote get"));
9942
9943 remote_file_get (argv[0], argv[1], from_tty);
9944
9945 do_cleanups (back_to);
9946}
9947
9948static void
9949remote_delete_command (char *args, int from_tty)
9950{
9951 struct cleanup *back_to;
9952 char **argv;
9953
d1a41061
PP
9954 if (args == NULL)
9955 error_no_arg (_("file to delete"));
9956
9957 argv = gdb_buildargv (args);
a6b151f1
DJ
9958 back_to = make_cleanup_freeargv (argv);
9959 if (argv[0] == NULL || argv[1] != NULL)
9960 error (_("Invalid parameters to remote delete"));
9961
9962 remote_file_delete (argv[0], from_tty);
9963
9964 do_cleanups (back_to);
9965}
9966
9967static void
9968remote_command (char *args, int from_tty)
9969{
9970 help_list (remote_cmdlist, "remote ", -1, gdb_stdout);
9971}
9972
b2175913
MS
9973static int
9974remote_can_execute_reverse (void)
9975{
40ab02ce
MS
9976 if (remote_protocol_packets[PACKET_bs].support == PACKET_ENABLE
9977 || remote_protocol_packets[PACKET_bc].support == PACKET_ENABLE)
9978 return 1;
9979 else
9980 return 0;
b2175913
MS
9981}
9982
74531fed
PA
9983static int
9984remote_supports_non_stop (void)
9985{
9986 return 1;
9987}
9988
03583c20
UW
9989static int
9990remote_supports_disable_randomization (void)
9991{
9992 /* Only supported in extended mode. */
9993 return 0;
9994}
9995
8a305172
PA
9996static int
9997remote_supports_multi_process (void)
9998{
9999 struct remote_state *rs = get_remote_state ();
a744cf53 10000
901f9912
UW
10001 /* Only extended-remote handles being attached to multiple
10002 processes, even though plain remote can use the multi-process
10003 thread id extensions, so that GDB knows the target process's
10004 PID. */
10005 return rs->extended && remote_multi_process_p (rs);
8a305172
PA
10006}
10007
70221824 10008static int
782b2b07
SS
10009remote_supports_cond_tracepoints (void)
10010{
10011 struct remote_state *rs = get_remote_state ();
a744cf53 10012
782b2b07
SS
10013 return rs->cond_tracepoints;
10014}
10015
3788aec7
LM
10016static int
10017remote_supports_cond_breakpoints (void)
10018{
10019 struct remote_state *rs = get_remote_state ();
10020
10021 return rs->cond_breakpoints;
10022}
10023
70221824 10024static int
7a697b8d
SS
10025remote_supports_fast_tracepoints (void)
10026{
10027 struct remote_state *rs = get_remote_state ();
a744cf53 10028
7a697b8d
SS
10029 return rs->fast_tracepoints;
10030}
10031
0fb4aa4b
PA
10032static int
10033remote_supports_static_tracepoints (void)
10034{
10035 struct remote_state *rs = get_remote_state ();
10036
10037 return rs->static_tracepoints;
10038}
10039
1e4d1764
YQ
10040static int
10041remote_supports_install_in_trace (void)
10042{
10043 struct remote_state *rs = get_remote_state ();
10044
10045 return rs->install_in_trace;
10046}
10047
d248b706
KY
10048static int
10049remote_supports_enable_disable_tracepoint (void)
10050{
10051 struct remote_state *rs = get_remote_state ();
10052
10053 return rs->enable_disable_tracepoints;
10054}
10055
3065dfb6
SS
10056static int
10057remote_supports_string_tracing (void)
10058{
10059 struct remote_state *rs = get_remote_state ();
10060
10061 return rs->string_tracing;
10062}
10063
35b1e5cc 10064static void
ad91cd99 10065remote_trace_init (void)
35b1e5cc
SS
10066{
10067 putpkt ("QTinit");
10068 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99 10069 if (strcmp (target_buf, "OK") != 0)
35b1e5cc
SS
10070 error (_("Target does not support this command."));
10071}
10072
10073static void free_actions_list (char **actions_list);
10074static void free_actions_list_cleanup_wrapper (void *);
10075static void
10076free_actions_list_cleanup_wrapper (void *al)
10077{
10078 free_actions_list (al);
10079}
10080
10081static void
10082free_actions_list (char **actions_list)
10083{
10084 int ndx;
10085
10086 if (actions_list == 0)
10087 return;
10088
10089 for (ndx = 0; actions_list[ndx]; ndx++)
10090 xfree (actions_list[ndx]);
10091
10092 xfree (actions_list);
10093}
10094
409873ef
SS
10095/* Recursive routine to walk through command list including loops, and
10096 download packets for each command. */
10097
10098static void
10099remote_download_command_source (int num, ULONGEST addr,
10100 struct command_line *cmds)
10101{
10102 struct remote_state *rs = get_remote_state ();
10103 struct command_line *cmd;
10104
10105 for (cmd = cmds; cmd; cmd = cmd->next)
10106 {
0df8b418 10107 QUIT; /* Allow user to bail out with ^C. */
409873ef
SS
10108 strcpy (rs->buf, "QTDPsrc:");
10109 encode_source_string (num, addr, "cmd", cmd->line,
10110 rs->buf + strlen (rs->buf),
10111 rs->buf_size - strlen (rs->buf));
10112 putpkt (rs->buf);
10113 remote_get_noisy_reply (&target_buf, &target_buf_size);
10114 if (strcmp (target_buf, "OK"))
10115 warning (_("Target does not support source download."));
10116
10117 if (cmd->control_type == while_control
10118 || cmd->control_type == while_stepping_control)
10119 {
10120 remote_download_command_source (num, addr, *cmd->body_list);
10121
0df8b418 10122 QUIT; /* Allow user to bail out with ^C. */
409873ef
SS
10123 strcpy (rs->buf, "QTDPsrc:");
10124 encode_source_string (num, addr, "cmd", "end",
10125 rs->buf + strlen (rs->buf),
10126 rs->buf_size - strlen (rs->buf));
10127 putpkt (rs->buf);
10128 remote_get_noisy_reply (&target_buf, &target_buf_size);
10129 if (strcmp (target_buf, "OK"))
10130 warning (_("Target does not support source download."));
10131 }
10132 }
10133}
10134
35b1e5cc 10135static void
e8ba3115 10136remote_download_tracepoint (struct bp_location *loc)
35b1e5cc 10137{
bba74b36 10138#define BUF_SIZE 2048
e8ba3115 10139
35b1e5cc 10140 CORE_ADDR tpaddr;
409873ef 10141 char addrbuf[40];
bba74b36 10142 char buf[BUF_SIZE];
35b1e5cc
SS
10143 char **tdp_actions;
10144 char **stepping_actions;
10145 int ndx;
10146 struct cleanup *old_chain = NULL;
10147 struct agent_expr *aexpr;
10148 struct cleanup *aexpr_chain = NULL;
10149 char *pkt;
e8ba3115 10150 struct breakpoint *b = loc->owner;
d9b3f62e 10151 struct tracepoint *t = (struct tracepoint *) b;
35b1e5cc 10152
e8ba3115
YQ
10153 encode_actions (loc->owner, loc, &tdp_actions, &stepping_actions);
10154 old_chain = make_cleanup (free_actions_list_cleanup_wrapper,
10155 tdp_actions);
10156 (void) make_cleanup (free_actions_list_cleanup_wrapper,
10157 stepping_actions);
10158
10159 tpaddr = loc->address;
10160 sprintf_vma (addrbuf, tpaddr);
bba74b36
YQ
10161 xsnprintf (buf, BUF_SIZE, "QTDP:%x:%s:%c:%lx:%x", b->number,
10162 addrbuf, /* address */
10163 (b->enable_state == bp_enabled ? 'E' : 'D'),
10164 t->step_count, t->pass_count);
e8ba3115
YQ
10165 /* Fast tracepoints are mostly handled by the target, but we can
10166 tell the target how big of an instruction block should be moved
10167 around. */
10168 if (b->type == bp_fast_tracepoint)
10169 {
10170 /* Only test for support at download time; we may not know
10171 target capabilities at definition time. */
10172 if (remote_supports_fast_tracepoints ())
35b1e5cc 10173 {
e8ba3115 10174 int isize;
35b1e5cc 10175
e8ba3115
YQ
10176 if (gdbarch_fast_tracepoint_valid_at (target_gdbarch,
10177 tpaddr, &isize, NULL))
bba74b36
YQ
10178 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":F%x",
10179 isize);
35b1e5cc 10180 else
e8ba3115
YQ
10181 /* If it passed validation at definition but fails now,
10182 something is very wrong. */
10183 internal_error (__FILE__, __LINE__,
10184 _("Fast tracepoint not "
10185 "valid during download"));
35b1e5cc 10186 }
e8ba3115
YQ
10187 else
10188 /* Fast tracepoints are functionally identical to regular
10189 tracepoints, so don't take lack of support as a reason to
10190 give up on the trace run. */
10191 warning (_("Target does not support fast tracepoints, "
10192 "downloading %d as regular tracepoint"), b->number);
10193 }
10194 else if (b->type == bp_static_tracepoint)
10195 {
10196 /* Only test for support at download time; we may not know
10197 target capabilities at definition time. */
10198 if (remote_supports_static_tracepoints ())
0fb4aa4b 10199 {
e8ba3115 10200 struct static_tracepoint_marker marker;
0fb4aa4b 10201
e8ba3115
YQ
10202 if (target_static_tracepoint_marker_at (tpaddr, &marker))
10203 strcat (buf, ":S");
0fb4aa4b 10204 else
e8ba3115 10205 error (_("Static tracepoint not valid during download"));
0fb4aa4b 10206 }
e8ba3115
YQ
10207 else
10208 /* Fast tracepoints are functionally identical to regular
10209 tracepoints, so don't take lack of support as a reason
10210 to give up on the trace run. */
10211 error (_("Target does not support static tracepoints"));
10212 }
10213 /* If the tracepoint has a conditional, make it into an agent
10214 expression and append to the definition. */
10215 if (loc->cond)
10216 {
10217 /* Only test support at download time, we may not know target
10218 capabilities at definition time. */
10219 if (remote_supports_cond_tracepoints ())
35b1e5cc 10220 {
e8ba3115
YQ
10221 aexpr = gen_eval_for_expr (tpaddr, loc->cond);
10222 aexpr_chain = make_cleanup_free_agent_expr (aexpr);
bba74b36
YQ
10223 xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":X%x,",
10224 aexpr->len);
e8ba3115
YQ
10225 pkt = buf + strlen (buf);
10226 for (ndx = 0; ndx < aexpr->len; ++ndx)
10227 pkt = pack_hex_byte (pkt, aexpr->buf[ndx]);
10228 *pkt = '\0';
10229 do_cleanups (aexpr_chain);
35b1e5cc 10230 }
e8ba3115
YQ
10231 else
10232 warning (_("Target does not support conditional tracepoints, "
10233 "ignoring tp %d cond"), b->number);
10234 }
35b1e5cc 10235
d9b3f62e 10236 if (b->commands || *default_collect)
e8ba3115
YQ
10237 strcat (buf, "-");
10238 putpkt (buf);
10239 remote_get_noisy_reply (&target_buf, &target_buf_size);
10240 if (strcmp (target_buf, "OK"))
10241 error (_("Target does not support tracepoints."));
35b1e5cc 10242
e8ba3115
YQ
10243 /* do_single_steps (t); */
10244 if (tdp_actions)
10245 {
10246 for (ndx = 0; tdp_actions[ndx]; ndx++)
35b1e5cc 10247 {
e8ba3115 10248 QUIT; /* Allow user to bail out with ^C. */
bba74b36
YQ
10249 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%c",
10250 b->number, addrbuf, /* address */
10251 tdp_actions[ndx],
10252 ((tdp_actions[ndx + 1] || stepping_actions)
10253 ? '-' : 0));
e8ba3115
YQ
10254 putpkt (buf);
10255 remote_get_noisy_reply (&target_buf,
10256 &target_buf_size);
10257 if (strcmp (target_buf, "OK"))
10258 error (_("Error on target while setting tracepoints."));
35b1e5cc 10259 }
e8ba3115
YQ
10260 }
10261 if (stepping_actions)
10262 {
10263 for (ndx = 0; stepping_actions[ndx]; ndx++)
35b1e5cc 10264 {
e8ba3115 10265 QUIT; /* Allow user to bail out with ^C. */
bba74b36
YQ
10266 xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%s%s",
10267 b->number, addrbuf, /* address */
10268 ((ndx == 0) ? "S" : ""),
10269 stepping_actions[ndx],
10270 (stepping_actions[ndx + 1] ? "-" : ""));
e8ba3115
YQ
10271 putpkt (buf);
10272 remote_get_noisy_reply (&target_buf,
10273 &target_buf_size);
10274 if (strcmp (target_buf, "OK"))
10275 error (_("Error on target while setting tracepoints."));
35b1e5cc 10276 }
e8ba3115 10277 }
409873ef 10278
e8ba3115
YQ
10279 if (remote_protocol_packets[PACKET_TracepointSource].support
10280 == PACKET_ENABLE)
10281 {
10282 if (b->addr_string)
409873ef 10283 {
e8ba3115
YQ
10284 strcpy (buf, "QTDPsrc:");
10285 encode_source_string (b->number, loc->address,
10286 "at", b->addr_string, buf + strlen (buf),
10287 2048 - strlen (buf));
409873ef 10288
e8ba3115
YQ
10289 putpkt (buf);
10290 remote_get_noisy_reply (&target_buf, &target_buf_size);
10291 if (strcmp (target_buf, "OK"))
10292 warning (_("Target does not support source download."));
409873ef 10293 }
e8ba3115
YQ
10294 if (b->cond_string)
10295 {
10296 strcpy (buf, "QTDPsrc:");
10297 encode_source_string (b->number, loc->address,
10298 "cond", b->cond_string, buf + strlen (buf),
10299 2048 - strlen (buf));
10300 putpkt (buf);
10301 remote_get_noisy_reply (&target_buf, &target_buf_size);
10302 if (strcmp (target_buf, "OK"))
10303 warning (_("Target does not support source download."));
10304 }
10305 remote_download_command_source (b->number, loc->address,
10306 breakpoint_commands (b));
35b1e5cc 10307 }
e8ba3115
YQ
10308
10309 do_cleanups (old_chain);
35b1e5cc
SS
10310}
10311
1e4d1764
YQ
10312static int
10313remote_can_download_tracepoint (void)
10314{
1e51243a
PA
10315 struct remote_state *rs = get_remote_state ();
10316 struct trace_status *ts;
10317 int status;
10318
10319 /* Don't try to install tracepoints until we've relocated our
10320 symbols, and fetched and merged the target's tracepoint list with
10321 ours. */
10322 if (rs->starting_up)
10323 return 0;
10324
10325 ts = current_trace_status ();
10326 status = remote_get_trace_status (ts);
1e4d1764
YQ
10327
10328 if (status == -1 || !ts->running_known || !ts->running)
10329 return 0;
10330
10331 /* If we are in a tracing experiment, but remote stub doesn't support
10332 installing tracepoint in trace, we have to return. */
10333 if (!remote_supports_install_in_trace ())
10334 return 0;
10335
10336 return 1;
10337}
10338
10339
35b1e5cc
SS
10340static void
10341remote_download_trace_state_variable (struct trace_state_variable *tsv)
10342{
10343 struct remote_state *rs = get_remote_state ();
00bf0b85 10344 char *p;
35b1e5cc 10345
bba74b36
YQ
10346 xsnprintf (rs->buf, get_remote_packet_size (), "QTDV:%x:%s:%x:",
10347 tsv->number, phex ((ULONGEST) tsv->initial_value, 8),
10348 tsv->builtin);
00bf0b85
SS
10349 p = rs->buf + strlen (rs->buf);
10350 if ((p - rs->buf) + strlen (tsv->name) * 2 >= get_remote_packet_size ())
10351 error (_("Trace state variable name too long for tsv definition packet"));
10352 p += 2 * bin2hex ((gdb_byte *) (tsv->name), p, 0);
10353 *p++ = '\0';
35b1e5cc
SS
10354 putpkt (rs->buf);
10355 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99
PA
10356 if (*target_buf == '\0')
10357 error (_("Target does not support this command."));
10358 if (strcmp (target_buf, "OK") != 0)
10359 error (_("Error on target while downloading trace state variable."));
35b1e5cc
SS
10360}
10361
d248b706
KY
10362static void
10363remote_enable_tracepoint (struct bp_location *location)
10364{
10365 struct remote_state *rs = get_remote_state ();
10366 char addr_buf[40];
10367
10368 sprintf_vma (addr_buf, location->address);
bba74b36
YQ
10369 xsnprintf (rs->buf, get_remote_packet_size (), "QTEnable:%x:%s",
10370 location->owner->number, addr_buf);
d248b706
KY
10371 putpkt (rs->buf);
10372 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
10373 if (*rs->buf == '\0')
10374 error (_("Target does not support enabling tracepoints while a trace run is ongoing."));
10375 if (strcmp (rs->buf, "OK") != 0)
10376 error (_("Error on target while enabling tracepoint."));
10377}
10378
10379static void
10380remote_disable_tracepoint (struct bp_location *location)
10381{
10382 struct remote_state *rs = get_remote_state ();
10383 char addr_buf[40];
10384
10385 sprintf_vma (addr_buf, location->address);
bba74b36
YQ
10386 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisable:%x:%s",
10387 location->owner->number, addr_buf);
d248b706
KY
10388 putpkt (rs->buf);
10389 remote_get_noisy_reply (&rs->buf, &rs->buf_size);
10390 if (*rs->buf == '\0')
10391 error (_("Target does not support disabling tracepoints while a trace run is ongoing."));
10392 if (strcmp (rs->buf, "OK") != 0)
10393 error (_("Error on target while disabling tracepoint."));
10394}
10395
35b1e5cc 10396static void
ad91cd99 10397remote_trace_set_readonly_regions (void)
35b1e5cc
SS
10398{
10399 asection *s;
10400 bfd_size_type size;
608bcef2 10401 bfd_vma vma;
35b1e5cc 10402 int anysecs = 0;
c2fa21f1 10403 int offset = 0;
35b1e5cc
SS
10404
10405 if (!exec_bfd)
10406 return; /* No information to give. */
10407
10408 strcpy (target_buf, "QTro");
10409 for (s = exec_bfd->sections; s; s = s->next)
10410 {
10411 char tmp1[40], tmp2[40];
c2fa21f1 10412 int sec_length;
35b1e5cc
SS
10413
10414 if ((s->flags & SEC_LOAD) == 0 ||
0df8b418 10415 /* (s->flags & SEC_CODE) == 0 || */
35b1e5cc
SS
10416 (s->flags & SEC_READONLY) == 0)
10417 continue;
10418
10419 anysecs = 1;
608bcef2 10420 vma = bfd_get_section_vma (,s);
35b1e5cc 10421 size = bfd_get_section_size (s);
608bcef2
HZ
10422 sprintf_vma (tmp1, vma);
10423 sprintf_vma (tmp2, vma + size);
c2fa21f1
HZ
10424 sec_length = 1 + strlen (tmp1) + 1 + strlen (tmp2);
10425 if (offset + sec_length + 1 > target_buf_size)
10426 {
864ac8a7
HZ
10427 if (remote_protocol_packets[PACKET_qXfer_traceframe_info].support
10428 != PACKET_ENABLE)
10429 warning (_("\
c2fa21f1
HZ
10430Too many sections for read-only sections definition packet."));
10431 break;
10432 }
bba74b36
YQ
10433 xsnprintf (target_buf + offset, target_buf_size - offset, ":%s,%s",
10434 tmp1, tmp2);
c2fa21f1 10435 offset += sec_length;
35b1e5cc
SS
10436 }
10437 if (anysecs)
10438 {
10439 putpkt (target_buf);
10440 getpkt (&target_buf, &target_buf_size, 0);
10441 }
10442}
10443
10444static void
ad91cd99 10445remote_trace_start (void)
35b1e5cc
SS
10446{
10447 putpkt ("QTStart");
10448 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99
PA
10449 if (*target_buf == '\0')
10450 error (_("Target does not support this command."));
10451 if (strcmp (target_buf, "OK") != 0)
35b1e5cc
SS
10452 error (_("Bogus reply from target: %s"), target_buf);
10453}
10454
10455static int
00bf0b85 10456remote_get_trace_status (struct trace_status *ts)
35b1e5cc 10457{
953b98d1 10458 /* Initialize it just to avoid a GCC false warning. */
f652de6f 10459 char *p = NULL;
0df8b418 10460 /* FIXME we need to get register block size some other way. */
00bf0b85 10461 extern int trace_regblock_size;
67f41397 10462 volatile struct gdb_exception ex;
a744cf53 10463
00bf0b85
SS
10464 trace_regblock_size = get_remote_arch_state ()->sizeof_g_packet;
10465
35b1e5cc 10466 putpkt ("qTStatus");
67f41397
JK
10467
10468 TRY_CATCH (ex, RETURN_MASK_ERROR)
10469 {
10470 p = remote_get_noisy_reply (&target_buf, &target_buf_size);
10471 }
10472 if (ex.reason < 0)
10473 {
10474 exception_fprintf (gdb_stderr, ex, "qTStatus: ");
10475 return -1;
10476 }
00bf0b85
SS
10477
10478 /* If the remote target doesn't do tracing, flag it. */
10479 if (*p == '\0')
10480 return -1;
35b1e5cc 10481
00bf0b85
SS
10482 /* We're working with a live target. */
10483 ts->from_file = 0;
10484
10485 /* Set some defaults. */
10486 ts->running_known = 0;
10487 ts->stop_reason = trace_stop_reason_unknown;
10488 ts->traceframe_count = -1;
10489 ts->buffer_free = 0;
10490
10491 if (*p++ != 'T')
35b1e5cc
SS
10492 error (_("Bogus trace status reply from target: %s"), target_buf);
10493
00bf0b85
SS
10494 parse_trace_status (p, ts);
10495
10496 return ts->running;
35b1e5cc
SS
10497}
10498
70221824 10499static void
f196051f
SS
10500remote_get_tracepoint_status (struct breakpoint *bp,
10501 struct uploaded_tp *utp)
10502{
10503 struct remote_state *rs = get_remote_state ();
f196051f
SS
10504 char *reply;
10505 struct bp_location *loc;
10506 struct tracepoint *tp = (struct tracepoint *) bp;
bba74b36 10507 size_t size = get_remote_packet_size ();
f196051f
SS
10508
10509 if (tp)
10510 {
10511 tp->base.hit_count = 0;
10512 tp->traceframe_usage = 0;
10513 for (loc = tp->base.loc; loc; loc = loc->next)
10514 {
10515 /* If the tracepoint was never downloaded, don't go asking for
10516 any status. */
10517 if (tp->number_on_target == 0)
10518 continue;
bba74b36
YQ
10519 xsnprintf (rs->buf, size, "qTP:%x:%s", tp->number_on_target,
10520 phex_nz (loc->address, 0));
f196051f
SS
10521 putpkt (rs->buf);
10522 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10523 if (reply && *reply)
10524 {
10525 if (*reply == 'V')
10526 parse_tracepoint_status (reply + 1, bp, utp);
10527 }
10528 }
10529 }
10530 else if (utp)
10531 {
10532 utp->hit_count = 0;
10533 utp->traceframe_usage = 0;
bba74b36
YQ
10534 xsnprintf (rs->buf, size, "qTP:%x:%s", utp->number,
10535 phex_nz (utp->addr, 0));
f196051f
SS
10536 putpkt (rs->buf);
10537 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10538 if (reply && *reply)
10539 {
10540 if (*reply == 'V')
10541 parse_tracepoint_status (reply + 1, bp, utp);
10542 }
10543 }
10544}
10545
35b1e5cc 10546static void
ad91cd99 10547remote_trace_stop (void)
35b1e5cc
SS
10548{
10549 putpkt ("QTStop");
10550 remote_get_noisy_reply (&target_buf, &target_buf_size);
ad91cd99
PA
10551 if (*target_buf == '\0')
10552 error (_("Target does not support this command."));
10553 if (strcmp (target_buf, "OK") != 0)
35b1e5cc
SS
10554 error (_("Bogus reply from target: %s"), target_buf);
10555}
10556
10557static int
10558remote_trace_find (enum trace_find_type type, int num,
10559 ULONGEST addr1, ULONGEST addr2,
10560 int *tpp)
10561{
10562 struct remote_state *rs = get_remote_state ();
bba74b36 10563 char *endbuf = rs->buf + get_remote_packet_size ();
35b1e5cc
SS
10564 char *p, *reply;
10565 int target_frameno = -1, target_tracept = -1;
10566
e6e4e701
PA
10567 /* Lookups other than by absolute frame number depend on the current
10568 trace selected, so make sure it is correct on the remote end
10569 first. */
10570 if (type != tfind_number)
10571 set_remote_traceframe ();
10572
35b1e5cc
SS
10573 p = rs->buf;
10574 strcpy (p, "QTFrame:");
10575 p = strchr (p, '\0');
10576 switch (type)
10577 {
10578 case tfind_number:
bba74b36 10579 xsnprintf (p, endbuf - p, "%x", num);
35b1e5cc
SS
10580 break;
10581 case tfind_pc:
bba74b36 10582 xsnprintf (p, endbuf - p, "pc:%s", phex_nz (addr1, 0));
35b1e5cc
SS
10583 break;
10584 case tfind_tp:
bba74b36 10585 xsnprintf (p, endbuf - p, "tdp:%x", num);
35b1e5cc
SS
10586 break;
10587 case tfind_range:
bba74b36
YQ
10588 xsnprintf (p, endbuf - p, "range:%s:%s", phex_nz (addr1, 0),
10589 phex_nz (addr2, 0));
35b1e5cc
SS
10590 break;
10591 case tfind_outside:
bba74b36
YQ
10592 xsnprintf (p, endbuf - p, "outside:%s:%s", phex_nz (addr1, 0),
10593 phex_nz (addr2, 0));
35b1e5cc
SS
10594 break;
10595 default:
9b20d036 10596 error (_("Unknown trace find type %d"), type);
35b1e5cc
SS
10597 }
10598
10599 putpkt (rs->buf);
10600 reply = remote_get_noisy_reply (&(rs->buf), &sizeof_pkt);
ad91cd99
PA
10601 if (*reply == '\0')
10602 error (_("Target does not support this command."));
35b1e5cc
SS
10603
10604 while (reply && *reply)
10605 switch (*reply)
10606 {
10607 case 'F':
f197e0f1
VP
10608 p = ++reply;
10609 target_frameno = (int) strtol (p, &reply, 16);
10610 if (reply == p)
10611 error (_("Unable to parse trace frame number"));
e6e4e701
PA
10612 /* Don't update our remote traceframe number cache on failure
10613 to select a remote traceframe. */
f197e0f1
VP
10614 if (target_frameno == -1)
10615 return -1;
35b1e5cc
SS
10616 break;
10617 case 'T':
f197e0f1
VP
10618 p = ++reply;
10619 target_tracept = (int) strtol (p, &reply, 16);
10620 if (reply == p)
10621 error (_("Unable to parse tracepoint number"));
35b1e5cc
SS
10622 break;
10623 case 'O': /* "OK"? */
10624 if (reply[1] == 'K' && reply[2] == '\0')
10625 reply += 2;
10626 else
10627 error (_("Bogus reply from target: %s"), reply);
10628 break;
10629 default:
10630 error (_("Bogus reply from target: %s"), reply);
10631 }
10632 if (tpp)
10633 *tpp = target_tracept;
e6e4e701
PA
10634
10635 remote_traceframe_number = target_frameno;
35b1e5cc
SS
10636 return target_frameno;
10637}
10638
10639static int
10640remote_get_trace_state_variable_value (int tsvnum, LONGEST *val)
10641{
10642 struct remote_state *rs = get_remote_state ();
10643 char *reply;
10644 ULONGEST uval;
10645
e6e4e701
PA
10646 set_remote_traceframe ();
10647
bba74b36 10648 xsnprintf (rs->buf, get_remote_packet_size (), "qTV:%x", tsvnum);
35b1e5cc
SS
10649 putpkt (rs->buf);
10650 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10651 if (reply && *reply)
10652 {
10653 if (*reply == 'V')
10654 {
10655 unpack_varlen_hex (reply + 1, &uval);
10656 *val = (LONGEST) uval;
10657 return 1;
10658 }
10659 }
10660 return 0;
10661}
10662
00bf0b85 10663static int
011aacb0 10664remote_save_trace_data (const char *filename)
00bf0b85
SS
10665{
10666 struct remote_state *rs = get_remote_state ();
10667 char *p, *reply;
10668
10669 p = rs->buf;
10670 strcpy (p, "QTSave:");
10671 p += strlen (p);
10672 if ((p - rs->buf) + strlen (filename) * 2 >= get_remote_packet_size ())
10673 error (_("Remote file name too long for trace save packet"));
10674 p += 2 * bin2hex ((gdb_byte *) filename, p, 0);
10675 *p++ = '\0';
10676 putpkt (rs->buf);
ad91cd99 10677 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
d6c5869f 10678 if (*reply == '\0')
ad91cd99
PA
10679 error (_("Target does not support this command."));
10680 if (strcmp (reply, "OK") != 0)
10681 error (_("Bogus reply from target: %s"), reply);
00bf0b85
SS
10682 return 0;
10683}
10684
10685/* This is basically a memory transfer, but needs to be its own packet
10686 because we don't know how the target actually organizes its trace
10687 memory, plus we want to be able to ask for as much as possible, but
10688 not be unhappy if we don't get as much as we ask for. */
10689
10690static LONGEST
10691remote_get_raw_trace_data (gdb_byte *buf, ULONGEST offset, LONGEST len)
10692{
10693 struct remote_state *rs = get_remote_state ();
10694 char *reply;
10695 char *p;
10696 int rslt;
10697
10698 p = rs->buf;
10699 strcpy (p, "qTBuffer:");
10700 p += strlen (p);
10701 p += hexnumstr (p, offset);
10702 *p++ = ',';
10703 p += hexnumstr (p, len);
10704 *p++ = '\0';
10705
10706 putpkt (rs->buf);
10707 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10708 if (reply && *reply)
10709 {
10710 /* 'l' by itself means we're at the end of the buffer and
10711 there is nothing more to get. */
10712 if (*reply == 'l')
10713 return 0;
10714
10715 /* Convert the reply into binary. Limit the number of bytes to
10716 convert according to our passed-in buffer size, rather than
10717 what was returned in the packet; if the target is
10718 unexpectedly generous and gives us a bigger reply than we
10719 asked for, we don't want to crash. */
10720 rslt = hex2bin (target_buf, buf, len);
10721 return rslt;
10722 }
10723
10724 /* Something went wrong, flag as an error. */
10725 return -1;
10726}
10727
35b1e5cc
SS
10728static void
10729remote_set_disconnected_tracing (int val)
10730{
10731 struct remote_state *rs = get_remote_state ();
10732
33da3f1c
SS
10733 if (rs->disconnected_tracing)
10734 {
ad91cd99
PA
10735 char *reply;
10736
bba74b36 10737 xsnprintf (rs->buf, get_remote_packet_size (), "QTDisconnected:%x", val);
33da3f1c 10738 putpkt (rs->buf);
ad91cd99
PA
10739 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10740 if (*reply == '\0')
33da3f1c 10741 error (_("Target does not support this command."));
ad91cd99
PA
10742 if (strcmp (reply, "OK") != 0)
10743 error (_("Bogus reply from target: %s"), reply);
33da3f1c
SS
10744 }
10745 else if (val)
10746 warning (_("Target does not support disconnected tracing."));
35b1e5cc
SS
10747}
10748
dc146f7c
VP
10749static int
10750remote_core_of_thread (struct target_ops *ops, ptid_t ptid)
10751{
10752 struct thread_info *info = find_thread_ptid (ptid);
a744cf53 10753
dc146f7c
VP
10754 if (info && info->private)
10755 return info->private->core;
10756 return -1;
10757}
10758
4daf5ac0
SS
10759static void
10760remote_set_circular_trace_buffer (int val)
10761{
10762 struct remote_state *rs = get_remote_state ();
ad91cd99 10763 char *reply;
4daf5ac0 10764
bba74b36 10765 xsnprintf (rs->buf, get_remote_packet_size (), "QTBuffer:circular:%x", val);
4daf5ac0 10766 putpkt (rs->buf);
ad91cd99
PA
10767 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10768 if (*reply == '\0')
4daf5ac0 10769 error (_("Target does not support this command."));
ad91cd99
PA
10770 if (strcmp (reply, "OK") != 0)
10771 error (_("Bogus reply from target: %s"), reply);
4daf5ac0
SS
10772}
10773
b3b9301e
PA
10774static struct traceframe_info *
10775remote_traceframe_info (void)
10776{
10777 char *text;
10778
10779 text = target_read_stralloc (&current_target,
10780 TARGET_OBJECT_TRACEFRAME_INFO, NULL);
10781 if (text != NULL)
10782 {
10783 struct traceframe_info *info;
10784 struct cleanup *back_to = make_cleanup (xfree, text);
10785
10786 info = parse_traceframe_info (text);
10787 do_cleanups (back_to);
10788 return info;
10789 }
10790
10791 return NULL;
10792}
10793
405f8e94
SS
10794/* Handle the qTMinFTPILen packet. Returns the minimum length of
10795 instruction on which a fast tracepoint may be placed. Returns -1
10796 if the packet is not supported, and 0 if the minimum instruction
10797 length is unknown. */
10798
10799static int
10800remote_get_min_fast_tracepoint_insn_len (void)
10801{
10802 struct remote_state *rs = get_remote_state ();
10803 char *reply;
10804
e886a173
PA
10805 /* If we're not debugging a process yet, the IPA can't be
10806 loaded. */
10807 if (!target_has_execution)
10808 return 0;
10809
10810 /* Make sure the remote is pointing at the right process. */
10811 set_general_process ();
10812
bba74b36 10813 xsnprintf (rs->buf, get_remote_packet_size (), "qTMinFTPILen");
405f8e94
SS
10814 putpkt (rs->buf);
10815 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10816 if (*reply == '\0')
10817 return -1;
10818 else
10819 {
10820 ULONGEST min_insn_len;
10821
10822 unpack_varlen_hex (reply, &min_insn_len);
10823
10824 return (int) min_insn_len;
10825 }
10826}
10827
f196051f
SS
10828static int
10829remote_set_trace_notes (char *user, char *notes, char *stop_notes)
10830{
10831 struct remote_state *rs = get_remote_state ();
10832 char *reply;
10833 char *buf = rs->buf;
10834 char *endbuf = rs->buf + get_remote_packet_size ();
10835 int nbytes;
10836
10837 buf += xsnprintf (buf, endbuf - buf, "QTNotes:");
10838 if (user)
10839 {
10840 buf += xsnprintf (buf, endbuf - buf, "user:");
10841 nbytes = bin2hex (user, buf, 0);
10842 buf += 2 * nbytes;
10843 *buf++ = ';';
10844 }
10845 if (notes)
10846 {
10847 buf += xsnprintf (buf, endbuf - buf, "notes:");
10848 nbytes = bin2hex (notes, buf, 0);
10849 buf += 2 * nbytes;
10850 *buf++ = ';';
10851 }
10852 if (stop_notes)
10853 {
10854 buf += xsnprintf (buf, endbuf - buf, "tstop:");
10855 nbytes = bin2hex (stop_notes, buf, 0);
10856 buf += 2 * nbytes;
10857 *buf++ = ';';
10858 }
10859 /* Ensure the buffer is terminated. */
10860 *buf = '\0';
10861
10862 putpkt (rs->buf);
10863 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
10864 if (*reply == '\0')
10865 return 0;
10866
10867 if (strcmp (reply, "OK") != 0)
10868 error (_("Bogus reply from target: %s"), reply);
10869
10870 return 1;
10871}
10872
d1feda86
YQ
10873static int
10874remote_use_agent (int use)
10875{
10876 if (remote_protocol_packets[PACKET_QAgent].support != PACKET_DISABLE)
10877 {
10878 struct remote_state *rs = get_remote_state ();
10879
10880 /* If the stub supports QAgent. */
bba74b36 10881 xsnprintf (rs->buf, get_remote_packet_size (), "QAgent:%d", use);
d1feda86
YQ
10882 putpkt (rs->buf);
10883 getpkt (&rs->buf, &rs->buf_size, 0);
10884
10885 if (strcmp (rs->buf, "OK") == 0)
10886 {
10887 use_agent = use;
10888 return 1;
10889 }
10890 }
10891
10892 return 0;
10893}
10894
10895static int
10896remote_can_use_agent (void)
10897{
10898 return (remote_protocol_packets[PACKET_QAgent].support != PACKET_DISABLE);
10899}
10900
c906108c 10901static void
fba45db2 10902init_remote_ops (void)
c906108c 10903{
c5aa993b 10904 remote_ops.to_shortname = "remote";
c906108c 10905 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
c5aa993b 10906 remote_ops.to_doc =
c906108c 10907 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
0d06e24b
JM
10908Specify the serial device it is connected to\n\
10909(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
c5aa993b
JM
10910 remote_ops.to_open = remote_open;
10911 remote_ops.to_close = remote_close;
c906108c 10912 remote_ops.to_detach = remote_detach;
6ad8ae5c 10913 remote_ops.to_disconnect = remote_disconnect;
c5aa993b 10914 remote_ops.to_resume = remote_resume;
c906108c
SS
10915 remote_ops.to_wait = remote_wait;
10916 remote_ops.to_fetch_registers = remote_fetch_registers;
10917 remote_ops.to_store_registers = remote_store_registers;
10918 remote_ops.to_prepare_to_store = remote_prepare_to_store;
c8e73a31 10919 remote_ops.deprecated_xfer_memory = remote_xfer_memory;
c5aa993b 10920 remote_ops.to_files_info = remote_files_info;
c906108c
SS
10921 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
10922 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
3c3bea1c
GS
10923 remote_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
10924 remote_ops.to_stopped_data_address = remote_stopped_data_address;
283002cf
MR
10925 remote_ops.to_watchpoint_addr_within_range =
10926 remote_watchpoint_addr_within_range;
3c3bea1c
GS
10927 remote_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
10928 remote_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
10929 remote_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
480a3f21
PW
10930 remote_ops.to_region_ok_for_hw_watchpoint
10931 = remote_region_ok_for_hw_watchpoint;
3c3bea1c
GS
10932 remote_ops.to_insert_watchpoint = remote_insert_watchpoint;
10933 remote_ops.to_remove_watchpoint = remote_remove_watchpoint;
c5aa993b
JM
10934 remote_ops.to_kill = remote_kill;
10935 remote_ops.to_load = generic_load;
c906108c 10936 remote_ops.to_mourn_inferior = remote_mourn;
2455069d 10937 remote_ops.to_pass_signals = remote_pass_signals;
9b224c5e 10938 remote_ops.to_program_signals = remote_program_signals;
c906108c 10939 remote_ops.to_thread_alive = remote_thread_alive;
0f71a2f6 10940 remote_ops.to_find_new_threads = remote_threads_info;
0caabb7e 10941 remote_ops.to_pid_to_str = remote_pid_to_str;
cf759d3b 10942 remote_ops.to_extra_thread_info = remote_threads_extra_info;
10760264 10943 remote_ops.to_get_ada_task_ptid = remote_get_ada_task_ptid;
c906108c 10944 remote_ops.to_stop = remote_stop;
4b8a223f 10945 remote_ops.to_xfer_partial = remote_xfer_partial;
96baa820 10946 remote_ops.to_rcmd = remote_rcmd;
49d03eab 10947 remote_ops.to_log_command = serial_log_command;
38691318 10948 remote_ops.to_get_thread_local_address = remote_get_thread_local_address;
c906108c 10949 remote_ops.to_stratum = process_stratum;
c35b1492
PA
10950 remote_ops.to_has_all_memory = default_child_has_all_memory;
10951 remote_ops.to_has_memory = default_child_has_memory;
10952 remote_ops.to_has_stack = default_child_has_stack;
10953 remote_ops.to_has_registers = default_child_has_registers;
10954 remote_ops.to_has_execution = default_child_has_execution;
3e43a32a 10955 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
b2175913 10956 remote_ops.to_can_execute_reverse = remote_can_execute_reverse;
c5aa993b 10957 remote_ops.to_magic = OPS_MAGIC;
fd79ecee 10958 remote_ops.to_memory_map = remote_memory_map;
a76d924d
DJ
10959 remote_ops.to_flash_erase = remote_flash_erase;
10960 remote_ops.to_flash_done = remote_flash_done;
29709017 10961 remote_ops.to_read_description = remote_read_description;
08388c79 10962 remote_ops.to_search_memory = remote_search_memory;
75c99385
PA
10963 remote_ops.to_can_async_p = remote_can_async_p;
10964 remote_ops.to_is_async_p = remote_is_async_p;
10965 remote_ops.to_async = remote_async;
75c99385
PA
10966 remote_ops.to_terminal_inferior = remote_terminal_inferior;
10967 remote_ops.to_terminal_ours = remote_terminal_ours;
74531fed 10968 remote_ops.to_supports_non_stop = remote_supports_non_stop;
8a305172 10969 remote_ops.to_supports_multi_process = remote_supports_multi_process;
03583c20
UW
10970 remote_ops.to_supports_disable_randomization
10971 = remote_supports_disable_randomization;
7313baad
UW
10972 remote_ops.to_fileio_open = remote_hostio_open;
10973 remote_ops.to_fileio_pwrite = remote_hostio_pwrite;
10974 remote_ops.to_fileio_pread = remote_hostio_pread;
10975 remote_ops.to_fileio_close = remote_hostio_close;
10976 remote_ops.to_fileio_unlink = remote_hostio_unlink;
b9e7b9c3 10977 remote_ops.to_fileio_readlink = remote_hostio_readlink;
d248b706 10978 remote_ops.to_supports_enable_disable_tracepoint = remote_supports_enable_disable_tracepoint;
3065dfb6 10979 remote_ops.to_supports_string_tracing = remote_supports_string_tracing;
b775012e 10980 remote_ops.to_supports_evaluation_of_breakpoint_conditions = remote_supports_cond_breakpoints;
35b1e5cc
SS
10981 remote_ops.to_trace_init = remote_trace_init;
10982 remote_ops.to_download_tracepoint = remote_download_tracepoint;
1e4d1764 10983 remote_ops.to_can_download_tracepoint = remote_can_download_tracepoint;
3e43a32a
MS
10984 remote_ops.to_download_trace_state_variable
10985 = remote_download_trace_state_variable;
d248b706
KY
10986 remote_ops.to_enable_tracepoint = remote_enable_tracepoint;
10987 remote_ops.to_disable_tracepoint = remote_disable_tracepoint;
35b1e5cc
SS
10988 remote_ops.to_trace_set_readonly_regions = remote_trace_set_readonly_regions;
10989 remote_ops.to_trace_start = remote_trace_start;
10990 remote_ops.to_get_trace_status = remote_get_trace_status;
f196051f 10991 remote_ops.to_get_tracepoint_status = remote_get_tracepoint_status;
35b1e5cc
SS
10992 remote_ops.to_trace_stop = remote_trace_stop;
10993 remote_ops.to_trace_find = remote_trace_find;
3e43a32a
MS
10994 remote_ops.to_get_trace_state_variable_value
10995 = remote_get_trace_state_variable_value;
00bf0b85
SS
10996 remote_ops.to_save_trace_data = remote_save_trace_data;
10997 remote_ops.to_upload_tracepoints = remote_upload_tracepoints;
3e43a32a
MS
10998 remote_ops.to_upload_trace_state_variables
10999 = remote_upload_trace_state_variables;
00bf0b85 11000 remote_ops.to_get_raw_trace_data = remote_get_raw_trace_data;
405f8e94 11001 remote_ops.to_get_min_fast_tracepoint_insn_len = remote_get_min_fast_tracepoint_insn_len;
35b1e5cc 11002 remote_ops.to_set_disconnected_tracing = remote_set_disconnected_tracing;
4daf5ac0 11003 remote_ops.to_set_circular_trace_buffer = remote_set_circular_trace_buffer;
f196051f 11004 remote_ops.to_set_trace_notes = remote_set_trace_notes;
dc146f7c 11005 remote_ops.to_core_of_thread = remote_core_of_thread;
4a5e7a5b 11006 remote_ops.to_verify_memory = remote_verify_memory;
711e434b 11007 remote_ops.to_get_tib_address = remote_get_tib_address;
d914c394 11008 remote_ops.to_set_permissions = remote_set_permissions;
0fb4aa4b
PA
11009 remote_ops.to_static_tracepoint_marker_at
11010 = remote_static_tracepoint_marker_at;
11011 remote_ops.to_static_tracepoint_markers_by_strid
11012 = remote_static_tracepoint_markers_by_strid;
b3b9301e 11013 remote_ops.to_traceframe_info = remote_traceframe_info;
d1feda86
YQ
11014 remote_ops.to_use_agent = remote_use_agent;
11015 remote_ops.to_can_use_agent = remote_can_use_agent;
c906108c
SS
11016}
11017
11018/* Set up the extended remote vector by making a copy of the standard
11019 remote vector and adding to it. */
11020
11021static void
fba45db2 11022init_extended_remote_ops (void)
c906108c
SS
11023{
11024 extended_remote_ops = remote_ops;
11025
0f71a2f6 11026 extended_remote_ops.to_shortname = "extended-remote";
c5aa993b 11027 extended_remote_ops.to_longname =
c906108c 11028 "Extended remote serial target in gdb-specific protocol";
c5aa993b 11029 extended_remote_ops.to_doc =
c906108c 11030 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
39237dd1
PA
11031Specify the serial device it is connected to (e.g. /dev/ttya).";
11032 extended_remote_ops.to_open = extended_remote_open;
c906108c
SS
11033 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
11034 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
2d717e4f
DJ
11035 extended_remote_ops.to_detach = extended_remote_detach;
11036 extended_remote_ops.to_attach = extended_remote_attach;
82f73884 11037 extended_remote_ops.to_kill = extended_remote_kill;
03583c20
UW
11038 extended_remote_ops.to_supports_disable_randomization
11039 = extended_remote_supports_disable_randomization;
0f71a2f6
JM
11040}
11041
6426a772
JM
11042static int
11043remote_can_async_p (void)
11044{
c6ebd6cf 11045 if (!target_async_permitted)
75c99385
PA
11046 /* We only enable async when the user specifically asks for it. */
11047 return 0;
11048
23860348 11049 /* We're async whenever the serial device is. */
3dd5b83d 11050 return serial_can_async_p (remote_desc);
6426a772
JM
11051}
11052
11053static int
11054remote_is_async_p (void)
11055{
c6ebd6cf 11056 if (!target_async_permitted)
75c99385
PA
11057 /* We only enable async when the user specifically asks for it. */
11058 return 0;
11059
23860348 11060 /* We're async whenever the serial device is. */
3dd5b83d 11061 return serial_is_async_p (remote_desc);
6426a772
JM
11062}
11063
2acceee2
JM
11064/* Pass the SERIAL event on and up to the client. One day this code
11065 will be able to delay notifying the client of an event until the
23860348 11066 point where an entire packet has been received. */
2acceee2 11067
2bc416ba 11068static void (*async_client_callback) (enum inferior_event_type event_type,
23860348 11069 void *context);
2acceee2
JM
11070static void *async_client_context;
11071static serial_event_ftype remote_async_serial_handler;
11072
6426a772 11073static void
819cc324 11074remote_async_serial_handler (struct serial *scb, void *context)
6426a772 11075{
2acceee2
JM
11076 /* Don't propogate error information up to the client. Instead let
11077 the client find out about the error by querying the target. */
11078 async_client_callback (INF_REG_EVENT, async_client_context);
11079}
11080
74531fed
PA
11081static void
11082remote_async_inferior_event_handler (gdb_client_data data)
11083{
11084 inferior_event_handler (INF_REG_EVENT, NULL);
11085}
11086
11087static void
11088remote_async_get_pending_events_handler (gdb_client_data data)
11089{
11090 remote_get_pending_stop_replies ();
11091}
11092
2acceee2 11093static void
2bc416ba 11094remote_async (void (*callback) (enum inferior_event_type event_type,
23860348 11095 void *context), void *context)
2acceee2
JM
11096{
11097 if (callback != NULL)
11098 {
2cd58942 11099 serial_async (remote_desc, remote_async_serial_handler, NULL);
2acceee2
JM
11100 async_client_callback = callback;
11101 async_client_context = context;
11102 }
11103 else
2cd58942 11104 serial_async (remote_desc, NULL, NULL);
6426a772
JM
11105}
11106
5a2468f5 11107static void
c2d11a7d 11108set_remote_cmd (char *args, int from_tty)
5a2468f5 11109{
427c3a89 11110 help_list (remote_set_cmdlist, "set remote ", -1, gdb_stdout);
5a2468f5
JM
11111}
11112
d471ea57
AC
11113static void
11114show_remote_cmd (char *args, int from_tty)
11115{
37a105a1 11116 /* We can't just use cmd_show_list here, because we want to skip
427c3a89 11117 the redundant "show remote Z-packet" and the legacy aliases. */
37a105a1
DJ
11118 struct cleanup *showlist_chain;
11119 struct cmd_list_element *list = remote_show_cmdlist;
79a45e25 11120 struct ui_out *uiout = current_uiout;
37a105a1
DJ
11121
11122 showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist");
11123 for (; list != NULL; list = list->next)
11124 if (strcmp (list->name, "Z-packet") == 0)
11125 continue;
427c3a89
DJ
11126 else if (list->type == not_set_cmd)
11127 /* Alias commands are exactly like the original, except they
11128 don't have the normal type. */
11129 continue;
11130 else
37a105a1
DJ
11131 {
11132 struct cleanup *option_chain
11133 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
a744cf53 11134
37a105a1
DJ
11135 ui_out_field_string (uiout, "name", list->name);
11136 ui_out_text (uiout, ": ");
427c3a89
DJ
11137 if (list->type == show_cmd)
11138 do_setshow_command ((char *) NULL, from_tty, list);
11139 else
11140 cmd_func (list, NULL, from_tty);
37a105a1
DJ
11141 /* Close the tuple. */
11142 do_cleanups (option_chain);
11143 }
427c3a89
DJ
11144
11145 /* Close the tuple. */
11146 do_cleanups (showlist_chain);
d471ea57 11147}
5a2468f5 11148
0f71a2f6 11149
23860348 11150/* Function to be called whenever a new objfile (shlib) is detected. */
dc8acb97
MS
11151static void
11152remote_new_objfile (struct objfile *objfile)
11153{
23860348 11154 if (remote_desc != 0) /* Have a remote connection. */
06d3b283 11155 remote_check_symbols (objfile);
dc8acb97
MS
11156}
11157
00bf0b85
SS
11158/* Pull all the tracepoints defined on the target and create local
11159 data structures representing them. We don't want to create real
11160 tracepoints yet, we don't want to mess up the user's existing
11161 collection. */
11162
11163static int
11164remote_upload_tracepoints (struct uploaded_tp **utpp)
d5551862 11165{
00bf0b85
SS
11166 struct remote_state *rs = get_remote_state ();
11167 char *p;
d5551862 11168
00bf0b85
SS
11169 /* Ask for a first packet of tracepoint definition. */
11170 putpkt ("qTfP");
11171 getpkt (&rs->buf, &rs->buf_size, 0);
11172 p = rs->buf;
11173 while (*p && *p != 'l')
d5551862 11174 {
00bf0b85
SS
11175 parse_tracepoint_definition (p, utpp);
11176 /* Ask for another packet of tracepoint definition. */
11177 putpkt ("qTsP");
11178 getpkt (&rs->buf, &rs->buf_size, 0);
11179 p = rs->buf;
d5551862 11180 }
00bf0b85 11181 return 0;
d5551862
SS
11182}
11183
00bf0b85
SS
11184static int
11185remote_upload_trace_state_variables (struct uploaded_tsv **utsvp)
d5551862 11186{
00bf0b85 11187 struct remote_state *rs = get_remote_state ();
d5551862 11188 char *p;
d5551862 11189
00bf0b85
SS
11190 /* Ask for a first packet of variable definition. */
11191 putpkt ("qTfV");
d5551862
SS
11192 getpkt (&rs->buf, &rs->buf_size, 0);
11193 p = rs->buf;
00bf0b85 11194 while (*p && *p != 'l')
d5551862 11195 {
00bf0b85
SS
11196 parse_tsv_definition (p, utsvp);
11197 /* Ask for another packet of variable definition. */
11198 putpkt ("qTsV");
d5551862
SS
11199 getpkt (&rs->buf, &rs->buf_size, 0);
11200 p = rs->buf;
11201 }
00bf0b85 11202 return 0;
d5551862
SS
11203}
11204
c906108c 11205void
fba45db2 11206_initialize_remote (void)
c906108c 11207{
ea9c271d 11208 struct remote_state *rs;
9a7071a8
JB
11209 struct cmd_list_element *cmd;
11210 char *cmd_name;
ea9c271d 11211
0f71a2f6 11212 /* architecture specific data */
2bc416ba 11213 remote_gdbarch_data_handle =
23860348 11214 gdbarch_data_register_post_init (init_remote_state);
29709017
DJ
11215 remote_g_packet_data_handle =
11216 gdbarch_data_register_pre_init (remote_g_packet_data_init);
d01949b6 11217
ea9c271d
DJ
11218 /* Initialize the per-target state. At the moment there is only one
11219 of these, not one per target. Only one target is active at a
11220 time. The default buffer size is unimportant; it will be expanded
11221 whenever a larger buffer is needed. */
0b83947e 11222 rs = get_remote_state_raw ();
ea9c271d
DJ
11223 rs->buf_size = 400;
11224 rs->buf = xmalloc (rs->buf_size);
11225
c906108c
SS
11226 init_remote_ops ();
11227 add_target (&remote_ops);
11228
11229 init_extended_remote_ops ();
11230 add_target (&extended_remote_ops);
cce74817 11231
dc8acb97 11232 /* Hook into new objfile notification. */
06d3b283 11233 observer_attach_new_objfile (remote_new_objfile);
dc8acb97 11234
b803fb0f
DJ
11235 /* Set up signal handlers. */
11236 sigint_remote_token =
11237 create_async_signal_handler (async_remote_interrupt, NULL);
11238 sigint_remote_twice_token =
6d549500 11239 create_async_signal_handler (async_remote_interrupt_twice, NULL);
b803fb0f 11240
c906108c
SS
11241#if 0
11242 init_remote_threadtests ();
11243#endif
11244
23860348 11245 /* set/show remote ... */
d471ea57 11246
1bedd215 11247 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, _("\
5a2468f5
JM
11248Remote protocol specific variables\n\
11249Configure various remote-protocol specific variables such as\n\
1bedd215 11250the packets being used"),
cff3e48b 11251 &remote_set_cmdlist, "set remote ",
23860348 11252 0 /* allow-unknown */, &setlist);
1bedd215 11253 add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\
5a2468f5
JM
11254Remote protocol specific variables\n\
11255Configure various remote-protocol specific variables such as\n\
1bedd215 11256the packets being used"),
cff3e48b 11257 &remote_show_cmdlist, "show remote ",
23860348 11258 0 /* allow-unknown */, &showlist);
5a2468f5 11259
1a966eab
AC
11260 add_cmd ("compare-sections", class_obscure, compare_sections_command, _("\
11261Compare section data on target to the exec file.\n\
11262Argument is a single section name (default: all loaded sections)."),
c906108c
SS
11263 &cmdlist);
11264
1a966eab
AC
11265 add_cmd ("packet", class_maintenance, packet_command, _("\
11266Send an arbitrary packet to a remote target.\n\
c906108c
SS
11267 maintenance packet TEXT\n\
11268If GDB is talking to an inferior via the GDB serial protocol, then\n\
11269this command sends the string TEXT to the inferior, and displays the\n\
11270response packet. GDB supplies the initial `$' character, and the\n\
1a966eab 11271terminating `#' character and checksum."),
c906108c
SS
11272 &maintenancelist);
11273
7915a72c
AC
11274 add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, _("\
11275Set whether to send break if interrupted."), _("\
11276Show whether to send break if interrupted."), _("\
11277If set, a break, instead of a cntrl-c, is sent to the remote target."),
9a7071a8 11278 set_remotebreak, show_remotebreak,
e707bbc2 11279 &setlist, &showlist);
9a7071a8
JB
11280 cmd_name = "remotebreak";
11281 cmd = lookup_cmd (&cmd_name, setlist, "", -1, 1);
11282 deprecate_cmd (cmd, "set remote interrupt-sequence");
11283 cmd_name = "remotebreak"; /* needed because lookup_cmd updates the pointer */
11284 cmd = lookup_cmd (&cmd_name, showlist, "", -1, 1);
11285 deprecate_cmd (cmd, "show remote interrupt-sequence");
11286
11287 add_setshow_enum_cmd ("interrupt-sequence", class_support,
3e43a32a
MS
11288 interrupt_sequence_modes, &interrupt_sequence_mode,
11289 _("\
9a7071a8
JB
11290Set interrupt sequence to remote target."), _("\
11291Show interrupt sequence to remote target."), _("\
11292Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
11293 NULL, show_interrupt_sequence,
11294 &remote_set_cmdlist,
11295 &remote_show_cmdlist);
11296
11297 add_setshow_boolean_cmd ("interrupt-on-connect", class_support,
11298 &interrupt_on_connect, _("\
11299Set whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
11300Show whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \
11301If set, interrupt sequence is sent to remote target."),
11302 NULL, NULL,
11303 &remote_set_cmdlist, &remote_show_cmdlist);
c906108c 11304
23860348 11305 /* Install commands for configuring memory read/write packets. */
11cf8741 11306
1a966eab
AC
11307 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size, _("\
11308Set the maximum number of bytes per memory write packet (deprecated)."),
11cf8741 11309 &setlist);
1a966eab
AC
11310 add_cmd ("remotewritesize", no_class, show_memory_write_packet_size, _("\
11311Show the maximum number of bytes per memory write packet (deprecated)."),
11cf8741
JM
11312 &showlist);
11313 add_cmd ("memory-write-packet-size", no_class,
1a966eab
AC
11314 set_memory_write_packet_size, _("\
11315Set the maximum number of bytes per memory-write packet.\n\
11316Specify the number of bytes in a packet or 0 (zero) for the\n\
11317default packet size. The actual limit is further reduced\n\
11318dependent on the target. Specify ``fixed'' to disable the\n\
11319further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
11320 &remote_set_cmdlist);
11321 add_cmd ("memory-read-packet-size", no_class,
1a966eab
AC
11322 set_memory_read_packet_size, _("\
11323Set the maximum number of bytes per memory-read packet.\n\
11324Specify the number of bytes in a packet or 0 (zero) for the\n\
11325default packet size. The actual limit is further reduced\n\
11326dependent on the target. Specify ``fixed'' to disable the\n\
11327further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
11328 &remote_set_cmdlist);
11329 add_cmd ("memory-write-packet-size", no_class,
11330 show_memory_write_packet_size,
1a966eab 11331 _("Show the maximum number of bytes per memory-write packet."),
11cf8741
JM
11332 &remote_show_cmdlist);
11333 add_cmd ("memory-read-packet-size", no_class,
11334 show_memory_read_packet_size,
1a966eab 11335 _("Show the maximum number of bytes per memory-read packet."),
11cf8741 11336 &remote_show_cmdlist);
c906108c 11337
b3f42336 11338 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class,
7915a72c
AC
11339 &remote_hw_watchpoint_limit, _("\
11340Set the maximum number of target hardware watchpoints."), _("\
11341Show the maximum number of target hardware watchpoints."), _("\
11342Specify a negative limit for unlimited."),
3e43a32a
MS
11343 NULL, NULL, /* FIXME: i18n: The maximum
11344 number of target hardware
11345 watchpoints is %s. */
b3f42336 11346 &remote_set_cmdlist, &remote_show_cmdlist);
480a3f21
PW
11347 add_setshow_zinteger_cmd ("hardware-watchpoint-length-limit", no_class,
11348 &remote_hw_watchpoint_length_limit, _("\
11349Set the maximum length (in bytes) of a target hardware watchpoint."), _("\
11350Show the maximum length (in bytes) of a target hardware watchpoint."), _("\
11351Specify a negative limit for unlimited."),
11352 NULL, NULL, /* FIXME: i18n: The maximum
11353 length (in bytes) of a target
11354 hardware watchpoint is %s. */
11355 &remote_set_cmdlist, &remote_show_cmdlist);
b3f42336 11356 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class,
7915a72c
AC
11357 &remote_hw_breakpoint_limit, _("\
11358Set the maximum number of target hardware breakpoints."), _("\
11359Show the maximum number of target hardware breakpoints."), _("\
11360Specify a negative limit for unlimited."),
3e43a32a
MS
11361 NULL, NULL, /* FIXME: i18n: The maximum
11362 number of target hardware
11363 breakpoints is %s. */
b3f42336 11364 &remote_set_cmdlist, &remote_show_cmdlist);
501eef12 11365
4d28ad1e
AC
11366 add_setshow_integer_cmd ("remoteaddresssize", class_obscure,
11367 &remote_address_size, _("\
11368Set the maximum size of the address (in bits) in a memory packet."), _("\
11369Show the maximum size of the address (in bits) in a memory packet."), NULL,
11370 NULL,
11371 NULL, /* FIXME: i18n: */
11372 &setlist, &showlist);
c906108c 11373
444abaca 11374 add_packet_config_cmd (&remote_protocol_packets[PACKET_X],
bb572ddd 11375 "X", "binary-download", 1);
0f71a2f6 11376
444abaca 11377 add_packet_config_cmd (&remote_protocol_packets[PACKET_vCont],
bb572ddd 11378 "vCont", "verbose-resume", 0);
506fb367 11379
89be2091
DJ
11380 add_packet_config_cmd (&remote_protocol_packets[PACKET_QPassSignals],
11381 "QPassSignals", "pass-signals", 0);
11382
9b224c5e
PA
11383 add_packet_config_cmd (&remote_protocol_packets[PACKET_QProgramSignals],
11384 "QProgramSignals", "program-signals", 0);
11385
444abaca 11386 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
bb572ddd 11387 "qSymbol", "symbol-lookup", 0);
dc8acb97 11388
444abaca 11389 add_packet_config_cmd (&remote_protocol_packets[PACKET_P],
bb572ddd 11390 "P", "set-register", 1);
d471ea57 11391
444abaca 11392 add_packet_config_cmd (&remote_protocol_packets[PACKET_p],
bb572ddd 11393 "p", "fetch-register", 1);
b96ec7ac 11394
444abaca 11395 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z0],
bb572ddd 11396 "Z0", "software-breakpoint", 0);
d471ea57 11397
444abaca 11398 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z1],
bb572ddd 11399 "Z1", "hardware-breakpoint", 0);
d471ea57 11400
444abaca 11401 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z2],
bb572ddd 11402 "Z2", "write-watchpoint", 0);
d471ea57 11403
444abaca 11404 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z3],
bb572ddd 11405 "Z3", "read-watchpoint", 0);
d471ea57 11406
444abaca 11407 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z4],
bb572ddd 11408 "Z4", "access-watchpoint", 0);
d471ea57 11409
0876f84a
DJ
11410 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_auxv],
11411 "qXfer:auxv:read", "read-aux-vector", 0);
802188a7 11412
23181151
DJ
11413 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_features],
11414 "qXfer:features:read", "target-features", 0);
11415
cfa9d6d9
DJ
11416 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries],
11417 "qXfer:libraries:read", "library-info", 0);
11418
2268b414
JK
11419 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries_svr4],
11420 "qXfer:libraries-svr4:read", "library-info-svr4", 0);
11421
fd79ecee
DJ
11422 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_memory_map],
11423 "qXfer:memory-map:read", "memory-map", 0);
11424
0e7f50da
UW
11425 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_read],
11426 "qXfer:spu:read", "read-spu-object", 0);
11427
11428 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_write],
11429 "qXfer:spu:write", "write-spu-object", 0);
11430
07e059b5
VP
11431 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_osdata],
11432 "qXfer:osdata:read", "osdata", 0);
11433
dc146f7c
VP
11434 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_threads],
11435 "qXfer:threads:read", "threads", 0);
11436
4aa995e1
PA
11437 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_read],
11438 "qXfer:siginfo:read", "read-siginfo-object", 0);
11439
11440 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_write],
11441 "qXfer:siginfo:write", "write-siginfo-object", 0);
11442
b3b9301e
PA
11443 add_packet_config_cmd
11444 (&remote_protocol_packets[PACKET_qXfer_traceframe_info],
11445 "qXfer:trace-frame-info:read", "traceframe-info", 0);
11446
169081d0
TG
11447 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_uib],
11448 "qXfer:uib:read", "unwind-info-block", 0);
11449
444abaca 11450 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTLSAddr],
38691318 11451 "qGetTLSAddr", "get-thread-local-storage-address",
38691318
KB
11452 0);
11453
711e434b
PM
11454 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTIBAddr],
11455 "qGetTIBAddr", "get-thread-information-block-address",
11456 0);
11457
40ab02ce
MS
11458 add_packet_config_cmd (&remote_protocol_packets[PACKET_bc],
11459 "bc", "reverse-continue", 0);
11460
11461 add_packet_config_cmd (&remote_protocol_packets[PACKET_bs],
11462 "bs", "reverse-step", 0);
11463
be2a5f71
DJ
11464 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSupported],
11465 "qSupported", "supported-packets", 0);
11466
08388c79
DE
11467 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSearch_memory],
11468 "qSearch:memory", "search-memory", 0);
11469
a6b151f1
DJ
11470 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_open],
11471 "vFile:open", "hostio-open", 0);
11472
11473 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pread],
11474 "vFile:pread", "hostio-pread", 0);
11475
11476 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pwrite],
11477 "vFile:pwrite", "hostio-pwrite", 0);
11478
11479 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_close],
11480 "vFile:close", "hostio-close", 0);
11481
11482 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_unlink],
11483 "vFile:unlink", "hostio-unlink", 0);
11484
b9e7b9c3
UW
11485 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_readlink],
11486 "vFile:readlink", "hostio-readlink", 0);
11487
2d717e4f
DJ
11488 add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach],
11489 "vAttach", "attach", 0);
11490
11491 add_packet_config_cmd (&remote_protocol_packets[PACKET_vRun],
11492 "vRun", "run", 0);
11493
a6f3e723
SL
11494 add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartNoAckMode],
11495 "QStartNoAckMode", "noack", 0);
11496
82f73884
PA
11497 add_packet_config_cmd (&remote_protocol_packets[PACKET_vKill],
11498 "vKill", "kill", 0);
11499
0b16c5cf
PA
11500 add_packet_config_cmd (&remote_protocol_packets[PACKET_qAttached],
11501 "qAttached", "query-attached", 0);
11502
782b2b07 11503 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalTracepoints],
3e43a32a
MS
11504 "ConditionalTracepoints",
11505 "conditional-tracepoints", 0);
3788aec7
LM
11506
11507 add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalBreakpoints],
11508 "ConditionalBreakpoints",
11509 "conditional-breakpoints", 0);
11510
7a697b8d
SS
11511 add_packet_config_cmd (&remote_protocol_packets[PACKET_FastTracepoints],
11512 "FastTracepoints", "fast-tracepoints", 0);
782b2b07 11513
409873ef
SS
11514 add_packet_config_cmd (&remote_protocol_packets[PACKET_TracepointSource],
11515 "TracepointSource", "TracepointSource", 0);
11516
d914c394
SS
11517 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAllow],
11518 "QAllow", "allow", 0);
11519
0fb4aa4b
PA
11520 add_packet_config_cmd (&remote_protocol_packets[PACKET_StaticTracepoints],
11521 "StaticTracepoints", "static-tracepoints", 0);
11522
1e4d1764
YQ
11523 add_packet_config_cmd (&remote_protocol_packets[PACKET_InstallInTrace],
11524 "InstallInTrace", "install-in-trace", 0);
11525
0fb4aa4b
PA
11526 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_statictrace_read],
11527 "qXfer:statictrace:read", "read-sdata-object", 0);
11528
78d85199
YQ
11529 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_fdpic],
11530 "qXfer:fdpic:read", "read-fdpic-loadmap", 0);
11531
03583c20
UW
11532 add_packet_config_cmd (&remote_protocol_packets[PACKET_QDisableRandomization],
11533 "QDisableRandomization", "disable-randomization", 0);
11534
d1feda86
YQ
11535 add_packet_config_cmd (&remote_protocol_packets[PACKET_QAgent],
11536 "QAgent", "agent", 0);
11537
37a105a1
DJ
11538 /* Keep the old ``set remote Z-packet ...'' working. Each individual
11539 Z sub-packet has its own set and show commands, but users may
11540 have sets to this variable in their .gdbinit files (or in their
11541 documentation). */
e9e68a56 11542 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure,
7915a72c
AC
11543 &remote_Z_packet_detect, _("\
11544Set use of remote protocol `Z' packets"), _("\
11545Show use of remote protocol `Z' packets "), _("\
3b64bf98 11546When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
7915a72c 11547packets."),
e9e68a56 11548 set_remote_protocol_Z_packet_cmd,
3e43a32a
MS
11549 show_remote_protocol_Z_packet_cmd,
11550 /* FIXME: i18n: Use of remote protocol
11551 `Z' packets is %s. */
e9e68a56 11552 &remote_set_cmdlist, &remote_show_cmdlist);
449092f6 11553
a6b151f1
DJ
11554 add_prefix_cmd ("remote", class_files, remote_command, _("\
11555Manipulate files on the remote system\n\
11556Transfer files to and from the remote target system."),
11557 &remote_cmdlist, "remote ",
11558 0 /* allow-unknown */, &cmdlist);
11559
11560 add_cmd ("put", class_files, remote_put_command,
11561 _("Copy a local file to the remote system."),
11562 &remote_cmdlist);
11563
11564 add_cmd ("get", class_files, remote_get_command,
11565 _("Copy a remote file to the local system."),
11566 &remote_cmdlist);
11567
11568 add_cmd ("delete", class_files, remote_delete_command,
11569 _("Delete a remote file."),
11570 &remote_cmdlist);
11571
2d717e4f
DJ
11572 remote_exec_file = xstrdup ("");
11573 add_setshow_string_noescape_cmd ("exec-file", class_files,
11574 &remote_exec_file, _("\
11575Set the remote pathname for \"run\""), _("\
11576Show the remote pathname for \"run\""), NULL, NULL, NULL,
11577 &remote_set_cmdlist, &remote_show_cmdlist);
11578
449092f6
CV
11579 /* Eventually initialize fileio. See fileio.c */
11580 initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist);
79d7f229
PA
11581
11582 /* Take advantage of the fact that the LWP field is not used, to tag
11583 special ptids with it set to != 0. */
82f73884
PA
11584 magic_null_ptid = ptid_build (42000, 1, -1);
11585 not_sent_ptid = ptid_build (42000, 1, -2);
11586 any_thread_ptid = ptid_build (42000, 1, 0);
35b1e5cc
SS
11587
11588 target_buf_size = 2048;
11589 target_buf = xmalloc (target_buf_size);
c906108c 11590}
10760264 11591
This page took 2.086603 seconds and 4 git commands to generate.