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