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