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