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