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