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