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