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