* symtab.h (enum address_class): Remove LOC_INDIRECT and
[deliverable/binutils-gdb.git] / gdb / remote.c
CommitLineData
c906108c 1/* Remote target communications for serial-line targets in custom GDB protocol
8926118c 2
6aba47ca 3 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
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 }
2137 /* There's no way to relocate by segment. */
2138 else
2139 do_segments = 0;
31d99776
DJ
2140
2141 if (do_segments)
2142 {
2143 int ret = symfile_map_offsets_to_segments (symfile_objfile->obfd, data,
2144 offs, num_segments, segments);
2145
2146 if (ret == 0 && !do_sections)
2147 error (_("Can not handle qOffsets TextSeg response with this symbol file"));
2148
2149 if (ret > 0)
2150 do_sections = 0;
2151 }
c906108c 2152
9ef895d6
DJ
2153 if (data)
2154 free_symfile_segment_data (data);
31d99776
DJ
2155
2156 if (do_sections)
2157 {
2158 offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
2159
2160 /* This is a temporary kludge to force data and bss to use the same offsets
2161 because that's what nlmconv does now. The real solution requires changes
2162 to the stub and remote.c that I don't have time to do right now. */
2163
2164 offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr;
2165 offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr;
2166 }
c906108c
SS
2167
2168 objfile_relocate (symfile_objfile, offs);
2169}
2170
8621d6a9 2171/* Stub for catch_exception. */
0f71a2f6 2172
2d717e4f
DJ
2173struct start_remote_args
2174{
2175 int from_tty;
2176
2177 /* The current target. */
2178 struct target_ops *target;
2179
2180 /* Non-zero if this is an extended-remote target. */
2181 int extended_p;
2182};
2183
9cbc821d 2184static void
2d717e4f 2185remote_start_remote (struct ui_out *uiout, void *opaque)
c906108c 2186{
2d717e4f
DJ
2187 struct remote_state *rs = get_remote_state ();
2188 struct start_remote_args *args = opaque;
2189 char *wait_status = NULL;
8621d6a9 2190
23860348 2191 immediate_quit++; /* Allow user to interrupt it. */
c906108c
SS
2192
2193 /* Ack any packet which the remote side has already sent. */
2cd58942 2194 serial_write (remote_desc, "+", 1);
c906108c 2195
2d717e4f
DJ
2196 /* Check whether the target is running now. */
2197 putpkt ("?");
2198 getpkt (&rs->buf, &rs->buf_size, 0);
2199
2200 if (rs->buf[0] == 'W' || rs->buf[0] == 'X')
2201 {
2202 if (args->extended_p)
2203 {
2204 /* We're connected, but not running. Drop out before we
2205 call start_remote. */
2206 target_mark_exited (args->target);
2207 return;
2208 }
2209 else
2210 error (_("The target is not running (try extended-remote?)"));
2211 }
2212 else
2213 {
2214 if (args->extended_p)
2215 target_mark_running (args->target);
2216
2217 /* Save the reply for later. */
2218 wait_status = alloca (strlen (rs->buf) + 1);
2219 strcpy (wait_status, rs->buf);
2220 }
2221
c906108c
SS
2222 /* Let the stub know that we want it to return the thread. */
2223 set_thread (-1, 0);
2224
2d717e4f
DJ
2225 /* Without this, some commands which require an active target
2226 (such as kill) won't work. This variable serves (at least)
2227 double duty as both the pid of the target process (if it has
2228 such), and as a flag indicating that a target is active.
2229 These functions should be split out into seperate variables,
2230 especially since GDB will someday have a notion of debugging
2231 several processes. */
2232 inferior_ptid = pid_to_ptid (MAGIC_NULL_PID);
2233
2234 /* Now, if we have thread information, update inferior_ptid. */
39f77062 2235 inferior_ptid = remote_current_thread (inferior_ptid);
c906108c 2236
23860348 2237 get_offsets (); /* Get text, data & bss offsets. */
c906108c 2238
2d717e4f
DJ
2239 /* Use the previously fetched status. */
2240 gdb_assert (wait_status != NULL);
2241 strcpy (rs->buf, wait_status);
2242 rs->cached_wait_status = 1;
c906108c 2243
2d717e4f
DJ
2244 immediate_quit--;
2245 start_remote (args->from_tty); /* Initialize gdb process mechanisms. */
c906108c
SS
2246}
2247
2248/* Open a connection to a remote debugger.
2249 NAME is the filename used for communication. */
2250
2251static void
fba45db2 2252remote_open (char *name, int from_tty)
c906108c 2253{
92d1e331 2254 remote_open_1 (name, from_tty, &remote_ops, 0, 0);
c906108c
SS
2255}
2256
23860348 2257/* Just like remote_open, but with asynchronous support. */
43ff13b4 2258static void
fba45db2 2259remote_async_open (char *name, int from_tty)
43ff13b4 2260{
92d1e331 2261 remote_open_1 (name, from_tty, &remote_async_ops, 0, 1);
43ff13b4
JM
2262}
2263
c906108c
SS
2264/* Open a connection to a remote debugger using the extended
2265 remote gdb protocol. NAME is the filename used for communication. */
2266
2267static void
fba45db2 2268extended_remote_open (char *name, int from_tty)
c906108c 2269{
92d1e331
DJ
2270 remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */,
2271 0 /* async_p */);
c906108c
SS
2272}
2273
23860348 2274/* Just like extended_remote_open, but with asynchronous support. */
43ff13b4 2275static void
fba45db2 2276extended_remote_async_open (char *name, int from_tty)
43ff13b4 2277{
92d1e331
DJ
2278 remote_open_1 (name, from_tty, &extended_async_remote_ops,
2279 1 /*extended_p */, 1 /* async_p */);
43ff13b4
JM
2280}
2281
c906108c
SS
2282/* Generic code for opening a connection to a remote target. */
2283
d471ea57
AC
2284static void
2285init_all_packet_configs (void)
2286{
2287 int i;
444abaca
DJ
2288 for (i = 0; i < PACKET_MAX; i++)
2289 update_packet_config (&remote_protocol_packets[i]);
d471ea57
AC
2290}
2291
23860348 2292/* Symbol look-up. */
dc8acb97
MS
2293
2294static void
2295remote_check_symbols (struct objfile *objfile)
2296{
d01949b6 2297 struct remote_state *rs = get_remote_state ();
dc8acb97
MS
2298 char *msg, *reply, *tmp;
2299 struct minimal_symbol *sym;
2300 int end;
2301
444abaca 2302 if (remote_protocol_packets[PACKET_qSymbol].support == PACKET_DISABLE)
dc8acb97
MS
2303 return;
2304
6d820c5c
DJ
2305 /* Allocate a message buffer. We can't reuse the input buffer in RS,
2306 because we need both at the same time. */
ea9c271d 2307 msg = alloca (get_remote_packet_size ());
6d820c5c 2308
23860348 2309 /* Invite target to request symbol lookups. */
dc8acb97
MS
2310
2311 putpkt ("qSymbol::");
6d820c5c
DJ
2312 getpkt (&rs->buf, &rs->buf_size, 0);
2313 packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSymbol]);
2e9f7625 2314 reply = rs->buf;
dc8acb97
MS
2315
2316 while (strncmp (reply, "qSymbol:", 8) == 0)
2317 {
2318 tmp = &reply[8];
cfd77fa1 2319 end = hex2bin (tmp, (gdb_byte *) msg, strlen (tmp) / 2);
dc8acb97
MS
2320 msg[end] = '\0';
2321 sym = lookup_minimal_symbol (msg, NULL, NULL);
2322 if (sym == NULL)
ea9c271d 2323 xsnprintf (msg, get_remote_packet_size (), "qSymbol::%s", &reply[8]);
dc8acb97 2324 else
2bbe3cc1
DJ
2325 {
2326 CORE_ADDR sym_addr = SYMBOL_VALUE_ADDRESS (sym);
2327
2328 /* If this is a function address, return the start of code
2329 instead of any data function descriptor. */
2330 sym_addr = gdbarch_convert_from_func_ptr_addr (current_gdbarch,
2331 sym_addr,
2332 &current_target);
2333
2334 xsnprintf (msg, get_remote_packet_size (), "qSymbol:%s:%s",
2335 paddr_nz (sym_addr), &reply[8]);
2336 }
2337
dc8acb97 2338 putpkt (msg);
6d820c5c 2339 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 2340 reply = rs->buf;
dc8acb97
MS
2341 }
2342}
2343
9db8d71f
DJ
2344static struct serial *
2345remote_serial_open (char *name)
2346{
2347 static int udp_warning = 0;
2348
2349 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
2350 of in ser-tcp.c, because it is the remote protocol assuming that the
2351 serial connection is reliable and not the serial connection promising
2352 to be. */
2353 if (!udp_warning && strncmp (name, "udp:", 4) == 0)
2354 {
8a3fe4f8
AC
2355 warning (_("\
2356The remote protocol may be unreliable over UDP.\n\
2357Some events may be lost, rendering further debugging impossible."));
9db8d71f
DJ
2358 udp_warning = 1;
2359 }
2360
2361 return serial_open (name);
2362}
2363
be2a5f71
DJ
2364/* This type describes each known response to the qSupported
2365 packet. */
2366struct protocol_feature
2367{
2368 /* The name of this protocol feature. */
2369 const char *name;
2370
2371 /* The default for this protocol feature. */
2372 enum packet_support default_support;
2373
2374 /* The function to call when this feature is reported, or after
2375 qSupported processing if the feature is not supported.
2376 The first argument points to this structure. The second
2377 argument indicates whether the packet requested support be
2378 enabled, disabled, or probed (or the default, if this function
2379 is being called at the end of processing and this feature was
2380 not reported). The third argument may be NULL; if not NULL, it
2381 is a NUL-terminated string taken from the packet following
2382 this feature's name and an equals sign. */
2383 void (*func) (const struct protocol_feature *, enum packet_support,
2384 const char *);
2385
2386 /* The corresponding packet for this feature. Only used if
2387 FUNC is remote_supported_packet. */
2388 int packet;
2389};
2390
be2a5f71
DJ
2391static void
2392remote_supported_packet (const struct protocol_feature *feature,
2393 enum packet_support support,
2394 const char *argument)
2395{
2396 if (argument)
2397 {
2398 warning (_("Remote qSupported response supplied an unexpected value for"
2399 " \"%s\"."), feature->name);
2400 return;
2401 }
2402
2403 if (remote_protocol_packets[feature->packet].support
2404 == PACKET_SUPPORT_UNKNOWN)
2405 remote_protocol_packets[feature->packet].support = support;
2406}
be2a5f71
DJ
2407
2408static void
2409remote_packet_size (const struct protocol_feature *feature,
2410 enum packet_support support, const char *value)
2411{
2412 struct remote_state *rs = get_remote_state ();
2413
2414 int packet_size;
2415 char *value_end;
2416
2417 if (support != PACKET_ENABLE)
2418 return;
2419
2420 if (value == NULL || *value == '\0')
2421 {
2422 warning (_("Remote target reported \"%s\" without a size."),
2423 feature->name);
2424 return;
2425 }
2426
2427 errno = 0;
2428 packet_size = strtol (value, &value_end, 16);
2429 if (errno != 0 || *value_end != '\0' || packet_size < 0)
2430 {
2431 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
2432 feature->name, value);
2433 return;
2434 }
2435
2436 if (packet_size > MAX_REMOTE_PACKET_SIZE)
2437 {
2438 warning (_("limiting remote suggested packet size (%d bytes) to %d"),
2439 packet_size, MAX_REMOTE_PACKET_SIZE);
2440 packet_size = MAX_REMOTE_PACKET_SIZE;
2441 }
2442
2443 /* Record the new maximum packet size. */
2444 rs->explicit_packet_size = packet_size;
2445}
2446
2447static struct protocol_feature remote_protocol_features[] = {
0876f84a 2448 { "PacketSize", PACKET_DISABLE, remote_packet_size, -1 },
40e57cf2 2449 { "qXfer:auxv:read", PACKET_DISABLE, remote_supported_packet,
fd79ecee 2450 PACKET_qXfer_auxv },
23181151
DJ
2451 { "qXfer:features:read", PACKET_DISABLE, remote_supported_packet,
2452 PACKET_qXfer_features },
cfa9d6d9
DJ
2453 { "qXfer:libraries:read", PACKET_DISABLE, remote_supported_packet,
2454 PACKET_qXfer_libraries },
fd79ecee 2455 { "qXfer:memory-map:read", PACKET_DISABLE, remote_supported_packet,
89be2091 2456 PACKET_qXfer_memory_map },
4de6483e
UW
2457 { "qXfer:spu:read", PACKET_DISABLE, remote_supported_packet,
2458 PACKET_qXfer_spu_read },
2459 { "qXfer:spu:write", PACKET_DISABLE, remote_supported_packet,
2460 PACKET_qXfer_spu_write },
89be2091
DJ
2461 { "QPassSignals", PACKET_DISABLE, remote_supported_packet,
2462 PACKET_QPassSignals },
be2a5f71
DJ
2463};
2464
2465static void
2466remote_query_supported (void)
2467{
2468 struct remote_state *rs = get_remote_state ();
2469 char *next;
2470 int i;
2471 unsigned char seen [ARRAY_SIZE (remote_protocol_features)];
2472
2473 /* The packet support flags are handled differently for this packet
2474 than for most others. We treat an error, a disabled packet, and
2475 an empty response identically: any features which must be reported
2476 to be used will be automatically disabled. An empty buffer
2477 accomplishes this, since that is also the representation for a list
2478 containing no features. */
2479
2480 rs->buf[0] = 0;
2481 if (remote_protocol_packets[PACKET_qSupported].support != PACKET_DISABLE)
2482 {
2483 putpkt ("qSupported");
2484 getpkt (&rs->buf, &rs->buf_size, 0);
2485
2486 /* If an error occured, warn, but do not return - just reset the
2487 buffer to empty and go on to disable features. */
2488 if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSupported])
2489 == PACKET_ERROR)
2490 {
2491 warning (_("Remote failure reply: %s"), rs->buf);
2492 rs->buf[0] = 0;
2493 }
2494 }
2495
2496 memset (seen, 0, sizeof (seen));
2497
2498 next = rs->buf;
2499 while (*next)
2500 {
2501 enum packet_support is_supported;
2502 char *p, *end, *name_end, *value;
2503
2504 /* First separate out this item from the rest of the packet. If
2505 there's another item after this, we overwrite the separator
2506 (terminated strings are much easier to work with). */
2507 p = next;
2508 end = strchr (p, ';');
2509 if (end == NULL)
2510 {
2511 end = p + strlen (p);
2512 next = end;
2513 }
2514 else
2515 {
89be2091
DJ
2516 *end = '\0';
2517 next = end + 1;
2518
be2a5f71
DJ
2519 if (end == p)
2520 {
2521 warning (_("empty item in \"qSupported\" response"));
2522 continue;
2523 }
be2a5f71
DJ
2524 }
2525
2526 name_end = strchr (p, '=');
2527 if (name_end)
2528 {
2529 /* This is a name=value entry. */
2530 is_supported = PACKET_ENABLE;
2531 value = name_end + 1;
2532 *name_end = '\0';
2533 }
2534 else
2535 {
2536 value = NULL;
2537 switch (end[-1])
2538 {
2539 case '+':
2540 is_supported = PACKET_ENABLE;
2541 break;
2542
2543 case '-':
2544 is_supported = PACKET_DISABLE;
2545 break;
2546
2547 case '?':
2548 is_supported = PACKET_SUPPORT_UNKNOWN;
2549 break;
2550
2551 default:
2552 warning (_("unrecognized item \"%s\" in \"qSupported\" response"), p);
2553 continue;
2554 }
2555 end[-1] = '\0';
2556 }
2557
2558 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
2559 if (strcmp (remote_protocol_features[i].name, p) == 0)
2560 {
2561 const struct protocol_feature *feature;
2562
2563 seen[i] = 1;
2564 feature = &remote_protocol_features[i];
2565 feature->func (feature, is_supported, value);
2566 break;
2567 }
2568 }
2569
2570 /* If we increased the packet size, make sure to increase the global
2571 buffer size also. We delay this until after parsing the entire
2572 qSupported packet, because this is the same buffer we were
2573 parsing. */
2574 if (rs->buf_size < rs->explicit_packet_size)
2575 {
2576 rs->buf_size = rs->explicit_packet_size;
2577 rs->buf = xrealloc (rs->buf, rs->buf_size);
2578 }
2579
2580 /* Handle the defaults for unmentioned features. */
2581 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
2582 if (!seen[i])
2583 {
2584 const struct protocol_feature *feature;
2585
2586 feature = &remote_protocol_features[i];
2587 feature->func (feature, feature->default_support, NULL);
2588 }
2589}
2590
2591
c906108c 2592static void
fba45db2 2593remote_open_1 (char *name, int from_tty, struct target_ops *target,
92d1e331 2594 int extended_p, int async_p)
c906108c 2595{
d01949b6 2596 struct remote_state *rs = get_remote_state ();
c906108c 2597 if (name == 0)
8a3fe4f8 2598 error (_("To open a remote debug connection, you need to specify what\n"
22e04375 2599 "serial device is attached to the remote system\n"
8a3fe4f8 2600 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
c906108c 2601
23860348 2602 /* See FIXME above. */
92d1e331
DJ
2603 if (!async_p)
2604 wait_forever_enabled_p = 1;
6426a772 2605
2d717e4f
DJ
2606 /* If we're connected to a running target, target_preopen will kill it.
2607 But if we're connected to a target system with no running process,
2608 then we will still be connected when it returns. Ask this question
2609 first, before target_preopen has a chance to kill anything. */
2610 if (remote_desc != NULL && !target_has_execution)
2611 {
2612 if (!from_tty
2613 || query (_("Already connected to a remote target. Disconnect? ")))
2614 pop_target ();
2615 else
2616 error (_("Still connected."));
2617 }
2618
c906108c
SS
2619 target_preopen (from_tty);
2620
2621 unpush_target (target);
2622
2d717e4f
DJ
2623 /* This time without a query. If we were connected to an
2624 extended-remote target and target_preopen killed the running
2625 process, we may still be connected. If we are starting "target
2626 remote" now, the extended-remote target will not have been
2627 removed by unpush_target. */
2628 if (remote_desc != NULL && !target_has_execution)
2629 pop_target ();
2630
89be2091
DJ
2631 /* Make sure we send the passed signals list the next time we resume. */
2632 xfree (last_pass_packet);
2633 last_pass_packet = NULL;
2634
ad9a8f3f 2635 remote_fileio_reset ();
1dd41f16
NS
2636 reopen_exec_file ();
2637 reread_symbols ();
2638
9db8d71f 2639 remote_desc = remote_serial_open (name);
c906108c
SS
2640 if (!remote_desc)
2641 perror_with_name (name);
2642
2643 if (baud_rate != -1)
2644 {
2cd58942 2645 if (serial_setbaudrate (remote_desc, baud_rate))
c906108c 2646 {
9b74d5d3
KB
2647 /* The requested speed could not be set. Error out to
2648 top level after closing remote_desc. Take care to
2649 set remote_desc to NULL to avoid closing remote_desc
2650 more than once. */
2cd58942 2651 serial_close (remote_desc);
9b74d5d3 2652 remote_desc = NULL;
c906108c
SS
2653 perror_with_name (name);
2654 }
2655 }
2656
2cd58942 2657 serial_raw (remote_desc);
c906108c
SS
2658
2659 /* If there is something sitting in the buffer we might take it as a
2660 response to a command, which would be bad. */
2cd58942 2661 serial_flush_input (remote_desc);
c906108c
SS
2662
2663 if (from_tty)
2664 {
2665 puts_filtered ("Remote debugging using ");
2666 puts_filtered (name);
2667 puts_filtered ("\n");
2668 }
23860348 2669 push_target (target); /* Switch to using remote target now. */
c906108c 2670
2d717e4f
DJ
2671 /* Assume that the target is running, unless we learn otherwise. */
2672 target_mark_running (target);
2673
be2a5f71
DJ
2674 /* Reset the target state; these things will be queried either by
2675 remote_query_supported or as they are needed. */
d471ea57 2676 init_all_packet_configs ();
be2a5f71 2677 rs->explicit_packet_size = 0;
802188a7 2678
c5aa993b 2679 general_thread = -2;
cce74817 2680 continue_thread = -2;
c906108c 2681
9d1f7ab2
MS
2682 /* Probe for ability to use "ThreadInfo" query, as required. */
2683 use_threadinfo_query = 1;
2684 use_threadextra_query = 1;
2685
be2a5f71
DJ
2686 /* The first packet we send to the target is the optional "supported
2687 packets" request. If the target can answer this, it will tell us
2688 which later probes to skip. */
2689 remote_query_supported ();
2690
424163ea
DJ
2691 /* Next, if the target can specify a description, read it. We do
2692 this before anything involving memory or registers. */
2693 target_find_description ();
2694
92d1e331
DJ
2695 if (async_p)
2696 {
23860348 2697 /* With this target we start out by owning the terminal. */
92d1e331
DJ
2698 remote_async_terminal_ours_p = 1;
2699
2700 /* FIXME: cagney/1999-09-23: During the initial connection it is
2701 assumed that the target is already ready and able to respond to
2702 requests. Unfortunately remote_start_remote() eventually calls
2703 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
2704 around this. Eventually a mechanism that allows
2705 wait_for_inferior() to expect/get timeouts will be
23860348 2706 implemented. */
92d1e331
DJ
2707 wait_forever_enabled_p = 0;
2708 }
2709
23860348 2710 /* First delete any symbols previously loaded from shared libraries. */
f78f6cf1 2711 no_shared_libraries (NULL, 0);
f78f6cf1 2712
36918e70 2713 /* Start the remote connection. If error() or QUIT, discard this
165b8e33
AC
2714 target (we'd otherwise be in an inconsistent state) and then
2715 propogate the error on up the exception chain. This ensures that
2716 the caller doesn't stumble along blindly assuming that the
2717 function succeeded. The CLI doesn't have this problem but other
2718 UI's, such as MI do.
36918e70
AC
2719
2720 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
2721 this function should return an error indication letting the
ce2826aa 2722 caller restore the previous state. Unfortunately the command
36918e70
AC
2723 ``target remote'' is directly wired to this function making that
2724 impossible. On a positive note, the CLI side of this problem has
2725 been fixed - the function set_cmd_context() makes it possible for
2726 all the ``target ....'' commands to share a common callback
2727 function. See cli-dump.c. */
109c3e39 2728 {
2d717e4f
DJ
2729 struct gdb_exception ex;
2730 struct start_remote_args args;
2731
2732 args.from_tty = from_tty;
2733 args.target = target;
2734 args.extended_p = extended_p;
2735
2736 ex = catch_exception (uiout, remote_start_remote, &args, RETURN_MASK_ALL);
109c3e39
AC
2737 if (ex.reason < 0)
2738 {
2739 pop_target ();
2740 if (async_p)
2741 wait_forever_enabled_p = 1;
2742 throw_exception (ex);
2743 }
2744 }
c906108c 2745
92d1e331
DJ
2746 if (async_p)
2747 wait_forever_enabled_p = 1;
6426a772
JM
2748
2749 if (extended_p)
43ff13b4 2750 {
6240bebf 2751 /* Tell the remote that we are using the extended protocol. */
6426a772 2752 putpkt ("!");
6d820c5c 2753 getpkt (&rs->buf, &rs->buf_size, 0);
43ff13b4 2754 }
a77053c2 2755
2d717e4f
DJ
2756 /* If we connected to a live target, do some additional setup. */
2757 if (target_has_execution)
2758 {
2759 if (exec_bfd) /* No use without an exec file. */
2760 remote_check_symbols (symfile_objfile);
2761 }
43ff13b4
JM
2762}
2763
c906108c
SS
2764/* This takes a program previously attached to and detaches it. After
2765 this is done, GDB can be used to debug some other program. We
2766 better not have left any breakpoints in the target program or it'll
2767 die when it hits one. */
2768
2769static void
2d717e4f 2770remote_detach_1 (char *args, int from_tty, int extended)
c906108c 2771{
d01949b6 2772 struct remote_state *rs = get_remote_state ();
c906108c
SS
2773
2774 if (args)
8a3fe4f8 2775 error (_("Argument given to \"detach\" when remotely debugging."));
c906108c 2776
2d717e4f
DJ
2777 if (!target_has_execution)
2778 error (_("No process to detach from."));
2779
c906108c 2780 /* Tell the remote target to detach. */
6d820c5c 2781 strcpy (rs->buf, "D");
4ddda9b5
PA
2782 putpkt (rs->buf);
2783 getpkt (&rs->buf, &rs->buf_size, 0);
2784
2785 if (rs->buf[0] == 'E')
2786 error (_("Can't detach process."));
c906108c 2787
23860348 2788 /* Unregister the file descriptor from the event loop. */
6ad8ae5c
DJ
2789 if (target_is_async_p ())
2790 serial_async (remote_desc, NULL, 0);
2791
cca728d0 2792 target_mourn_inferior ();
c906108c 2793 if (from_tty)
2d717e4f
DJ
2794 {
2795 if (extended)
2796 puts_filtered ("Detached from remote process.\n");
2797 else
2798 puts_filtered ("Ending remote debugging.\n");
2799 }
2800}
2801
2802static void
2803remote_detach (char *args, int from_tty)
2804{
2805 remote_detach_1 (args, from_tty, 0);
2806}
2807
2808static void
2809extended_remote_detach (char *args, int from_tty)
2810{
2811 remote_detach_1 (args, from_tty, 1);
c906108c
SS
2812}
2813
6ad8ae5c
DJ
2814/* Same as remote_detach, but don't send the "D" packet; just disconnect. */
2815
43ff13b4 2816static void
597320e7 2817remote_disconnect (struct target_ops *target, char *args, int from_tty)
43ff13b4 2818{
43ff13b4 2819 if (args)
2d717e4f 2820 error (_("Argument given to \"disconnect\" when remotely debugging."));
43ff13b4 2821
23860348 2822 /* Unregister the file descriptor from the event loop. */
ed9a39eb 2823 if (target_is_async_p ())
2cd58942 2824 serial_async (remote_desc, NULL, 0);
43ff13b4 2825
2d717e4f
DJ
2826 /* Make sure we unpush even the extended remote targets; mourn
2827 won't do it. So call remote_mourn_1 directly instead of
2828 target_mourn_inferior. */
2829 remote_mourn_1 (target);
2830
43ff13b4
JM
2831 if (from_tty)
2832 puts_filtered ("Ending remote debugging.\n");
2833}
2834
2d717e4f
DJ
2835/* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
2836 be chatty about it. */
2837
2838static void
2839extended_remote_attach_1 (struct target_ops *target, char *args, int from_tty)
2840{
2841 struct remote_state *rs = get_remote_state ();
be86555c 2842 int pid;
2d717e4f 2843 char *dummy;
96ef3384 2844 char *wait_status = NULL;
2d717e4f
DJ
2845
2846 if (!args)
2847 error_no_arg (_("process-id to attach"));
2848
2849 dummy = args;
2850 pid = strtol (args, &dummy, 0);
2851 /* Some targets don't set errno on errors, grrr! */
2852 if (pid == 0 && args == dummy)
2853 error (_("Illegal process-id: %s."), args);
2854
2855 if (remote_protocol_packets[PACKET_vAttach].support == PACKET_DISABLE)
2856 error (_("This target does not support attaching to a process"));
2857
2858 sprintf (rs->buf, "vAttach;%x", pid);
2859 putpkt (rs->buf);
2860 getpkt (&rs->buf, &rs->buf_size, 0);
2861
2862 if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vAttach]) == PACKET_OK)
2863 {
2864 if (from_tty)
2865 printf_unfiltered (_("Attached to %s\n"),
2866 target_pid_to_str (pid_to_ptid (pid)));
2867
96ef3384
UW
2868 /* Save the reply for later. */
2869 wait_status = alloca (strlen (rs->buf) + 1);
2870 strcpy (wait_status, rs->buf);
2d717e4f
DJ
2871 }
2872 else if (remote_protocol_packets[PACKET_vAttach].support == PACKET_DISABLE)
2873 error (_("This target does not support attaching to a process"));
2874 else
2875 error (_("Attaching to %s failed"),
2876 target_pid_to_str (pid_to_ptid (pid)));
2877
2878 target_mark_running (target);
2879 inferior_ptid = pid_to_ptid (pid);
df7df359 2880 attach_flag = 1;
96ef3384
UW
2881
2882 /* Next, if the target can specify a description, read it. We do
2883 this before anything involving memory or registers. */
2884 target_find_description ();
2885
2886 /* Use the previously fetched status. */
2887 gdb_assert (wait_status != NULL);
2888 strcpy (rs->buf, wait_status);
2889 rs->cached_wait_status = 1;
2d717e4f
DJ
2890}
2891
2892static void
2893extended_remote_attach (char *args, int from_tty)
2894{
2895 extended_remote_attach_1 (&extended_remote_ops, args, from_tty);
2896}
2897
2898static void
2899extended_async_remote_attach (char *args, int from_tty)
2900{
2901 extended_remote_attach_1 (&extended_async_remote_ops, args, from_tty);
2902}
2903
c906108c
SS
2904/* Convert hex digit A to a number. */
2905
30559e10 2906static int
fba45db2 2907fromhex (int a)
c906108c
SS
2908{
2909 if (a >= '0' && a <= '9')
2910 return a - '0';
2911 else if (a >= 'a' && a <= 'f')
2912 return a - 'a' + 10;
2913 else if (a >= 'A' && a <= 'F')
2914 return a - 'A' + 10;
c5aa993b 2915 else
8a3fe4f8 2916 error (_("Reply contains invalid hex digit %d"), a);
c906108c
SS
2917}
2918
30559e10 2919static int
cfd77fa1 2920hex2bin (const char *hex, gdb_byte *bin, int count)
30559e10
MS
2921{
2922 int i;
2923
30559e10
MS
2924 for (i = 0; i < count; i++)
2925 {
2926 if (hex[0] == 0 || hex[1] == 0)
2927 {
2928 /* Hex string is short, or of uneven length.
23860348 2929 Return the count that has been converted so far. */
30559e10
MS
2930 return i;
2931 }
2932 *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
2933 hex += 2;
2934 }
2935 return i;
2936}
2937
c906108c
SS
2938/* Convert number NIB to a hex digit. */
2939
2940static int
fba45db2 2941tohex (int nib)
c906108c
SS
2942{
2943 if (nib < 10)
c5aa993b 2944 return '0' + nib;
c906108c 2945 else
c5aa993b 2946 return 'a' + nib - 10;
c906108c 2947}
30559e10
MS
2948
2949static int
cfd77fa1 2950bin2hex (const gdb_byte *bin, char *hex, int count)
30559e10
MS
2951{
2952 int i;
23860348 2953 /* May use a length, or a nul-terminated string as input. */
30559e10 2954 if (count == 0)
cfd77fa1 2955 count = strlen ((char *) bin);
30559e10
MS
2956
2957 for (i = 0; i < count; i++)
2958 {
2959 *hex++ = tohex ((*bin >> 4) & 0xf);
2960 *hex++ = tohex (*bin++ & 0xf);
2961 }
2962 *hex = 0;
2963 return i;
2964}
c906108c 2965\f
506fb367
DJ
2966/* Check for the availability of vCont. This function should also check
2967 the response. */
c906108c
SS
2968
2969static void
6d820c5c 2970remote_vcont_probe (struct remote_state *rs)
c906108c 2971{
2e9f7625 2972 char *buf;
6d820c5c 2973
2e9f7625
DJ
2974 strcpy (rs->buf, "vCont?");
2975 putpkt (rs->buf);
6d820c5c 2976 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 2977 buf = rs->buf;
c906108c 2978
506fb367
DJ
2979 /* Make sure that the features we assume are supported. */
2980 if (strncmp (buf, "vCont", 5) == 0)
2981 {
2982 char *p = &buf[5];
2983 int support_s, support_S, support_c, support_C;
2984
2985 support_s = 0;
2986 support_S = 0;
2987 support_c = 0;
2988 support_C = 0;
2989 while (p && *p == ';')
2990 {
2991 p++;
2992 if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0))
2993 support_s = 1;
2994 else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0))
2995 support_S = 1;
2996 else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0))
2997 support_c = 1;
2998 else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0))
2999 support_C = 1;
3000
3001 p = strchr (p, ';');
3002 }
c906108c 3003
506fb367
DJ
3004 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
3005 BUF will make packet_ok disable the packet. */
3006 if (!support_s || !support_S || !support_c || !support_C)
3007 buf[0] = 0;
3008 }
c906108c 3009
444abaca 3010 packet_ok (buf, &remote_protocol_packets[PACKET_vCont]);
506fb367 3011}
c906108c 3012
506fb367
DJ
3013/* Resume the remote inferior by using a "vCont" packet. The thread
3014 to be resumed is PTID; STEP and SIGGNAL indicate whether the
3015 resumed thread should be single-stepped and/or signalled. If PTID's
3016 PID is -1, then all threads are resumed; the thread to be stepped and/or
3017 signalled is given in the global INFERIOR_PTID. This function returns
3018 non-zero iff it resumes the inferior.
44eaed12 3019
506fb367
DJ
3020 This function issues a strict subset of all possible vCont commands at the
3021 moment. */
44eaed12 3022
506fb367
DJ
3023static int
3024remote_vcont_resume (ptid_t ptid, int step, enum target_signal siggnal)
3025{
3026 struct remote_state *rs = get_remote_state ();
3027 int pid = PIDGET (ptid);
2d717e4f 3028 char *outbuf;
506fb367 3029 struct cleanup *old_cleanup;
44eaed12 3030
444abaca 3031 if (remote_protocol_packets[PACKET_vCont].support == PACKET_SUPPORT_UNKNOWN)
6d820c5c 3032 remote_vcont_probe (rs);
44eaed12 3033
444abaca 3034 if (remote_protocol_packets[PACKET_vCont].support == PACKET_DISABLE)
6d820c5c 3035 return 0;
44eaed12 3036
506fb367
DJ
3037 /* If we could generate a wider range of packets, we'd have to worry
3038 about overflowing BUF. Should there be a generic
3039 "multi-part-packet" packet? */
3040
3041 if (PIDGET (inferior_ptid) == MAGIC_NULL_PID)
c906108c 3042 {
506fb367
DJ
3043 /* MAGIC_NULL_PTID means that we don't have any active threads, so we
3044 don't have any PID numbers the inferior will understand. Make sure
3045 to only send forms that do not specify a PID. */
3046 if (step && siggnal != TARGET_SIGNAL_0)
2963ee1d 3047 outbuf = xstrprintf ("vCont;S%02x", siggnal);
506fb367 3048 else if (step)
2963ee1d 3049 outbuf = xstrprintf ("vCont;s");
506fb367 3050 else if (siggnal != TARGET_SIGNAL_0)
2963ee1d 3051 outbuf = xstrprintf ("vCont;C%02x", siggnal);
506fb367 3052 else
2963ee1d 3053 outbuf = xstrprintf ("vCont;c");
506fb367
DJ
3054 }
3055 else if (pid == -1)
3056 {
3057 /* Resume all threads, with preference for INFERIOR_PTID. */
3058 if (step && siggnal != TARGET_SIGNAL_0)
2963ee1d
DJ
3059 outbuf = xstrprintf ("vCont;S%02x:%x;c", siggnal,
3060 PIDGET (inferior_ptid));
506fb367 3061 else if (step)
2963ee1d 3062 outbuf = xstrprintf ("vCont;s:%x;c", PIDGET (inferior_ptid));
506fb367 3063 else if (siggnal != TARGET_SIGNAL_0)
2963ee1d
DJ
3064 outbuf = xstrprintf ("vCont;C%02x:%x;c", siggnal,
3065 PIDGET (inferior_ptid));
506fb367 3066 else
2963ee1d 3067 outbuf = xstrprintf ("vCont;c");
c906108c
SS
3068 }
3069 else
506fb367
DJ
3070 {
3071 /* Scheduler locking; resume only PTID. */
3072 if (step && siggnal != TARGET_SIGNAL_0)
2963ee1d 3073 outbuf = xstrprintf ("vCont;S%02x:%x", siggnal, pid);
506fb367 3074 else if (step)
2963ee1d 3075 outbuf = xstrprintf ("vCont;s:%x", pid);
506fb367 3076 else if (siggnal != TARGET_SIGNAL_0)
2963ee1d 3077 outbuf = xstrprintf ("vCont;C%02x:%x", siggnal, pid);
506fb367 3078 else
2963ee1d 3079 outbuf = xstrprintf ("vCont;c:%x", pid);
506fb367 3080 }
c906108c 3081
ea9c271d 3082 gdb_assert (outbuf && strlen (outbuf) < get_remote_packet_size ());
6d820c5c 3083 old_cleanup = make_cleanup (xfree, outbuf);
2963ee1d
DJ
3084
3085 putpkt (outbuf);
506fb367
DJ
3086
3087 do_cleanups (old_cleanup);
3088
3089 return 1;
c906108c 3090}
43ff13b4 3091
506fb367
DJ
3092/* Tell the remote machine to resume. */
3093
3094static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
3095
3096static int last_sent_step;
3097
43ff13b4 3098static void
506fb367 3099remote_resume (ptid_t ptid, int step, enum target_signal siggnal)
43ff13b4 3100{
d01949b6 3101 struct remote_state *rs = get_remote_state ();
2e9f7625 3102 char *buf;
39f77062 3103 int pid = PIDGET (ptid);
43ff13b4 3104
43ff13b4
JM
3105 last_sent_signal = siggnal;
3106 last_sent_step = step;
3107
89be2091
DJ
3108 /* Update the inferior on signals to silently pass, if they've changed. */
3109 remote_pass_signals ();
3110
506fb367
DJ
3111 /* The vCont packet doesn't need to specify threads via Hc. */
3112 if (remote_vcont_resume (ptid, step, siggnal))
3113 return;
3114
3115 /* All other supported resume packets do use Hc, so call set_thread. */
3116 if (pid == -1)
23860348 3117 set_thread (0, 0); /* Run any thread. */
506fb367 3118 else
23860348 3119 set_thread (pid, 0); /* Run this thread. */
506fb367 3120
2e9f7625 3121 buf = rs->buf;
43ff13b4
JM
3122 if (siggnal != TARGET_SIGNAL_0)
3123 {
3124 buf[0] = step ? 'S' : 'C';
c5aa993b 3125 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
506fb367 3126 buf[2] = tohex (((int) siggnal) & 0xf);
43ff13b4
JM
3127 buf[3] = '\0';
3128 }
3129 else
c5aa993b 3130 strcpy (buf, step ? "s" : "c");
506fb367 3131
44eaed12 3132 putpkt (buf);
506fb367
DJ
3133}
3134
23860348 3135/* Same as remote_resume, but with async support. */
506fb367
DJ
3136static void
3137remote_async_resume (ptid_t ptid, int step, enum target_signal siggnal)
3138{
3139 remote_resume (ptid, step, siggnal);
43ff13b4 3140
2acceee2
JM
3141 /* We are about to start executing the inferior, let's register it
3142 with the event loop. NOTE: this is the one place where all the
3143 execution commands end up. We could alternatively do this in each
23860348 3144 of the execution commands in infcmd.c. */
2acceee2
JM
3145 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
3146 into infcmd.c in order to allow inferior function calls to work
23860348 3147 NOT asynchronously. */
362646f5 3148 if (target_can_async_p ())
2acceee2 3149 target_async (inferior_event_handler, 0);
23860348 3150 /* Tell the world that the target is now executing. */
2acceee2
JM
3151 /* FIXME: cagney/1999-09-23: Is it the targets responsibility to set
3152 this? Instead, should the client of target just assume (for
3153 async targets) that the target is going to start executing? Is
3154 this information already found in the continuation block? */
ed9a39eb 3155 if (target_is_async_p ())
2acceee2 3156 target_executing = 1;
43ff13b4 3157}
c906108c 3158\f
43ff13b4
JM
3159
3160/* Set up the signal handler for SIGINT, while the target is
23860348 3161 executing, ovewriting the 'regular' SIGINT signal handler. */
43ff13b4 3162static void
fba45db2 3163initialize_sigint_signal_handler (void)
43ff13b4 3164{
43ff13b4
JM
3165 signal (SIGINT, handle_remote_sigint);
3166}
3167
23860348 3168/* Signal handler for SIGINT, while the target is executing. */
43ff13b4 3169static void
fba45db2 3170handle_remote_sigint (int sig)
43ff13b4
JM
3171{
3172 signal (sig, handle_remote_sigint_twice);
43ff13b4
JM
3173 mark_async_signal_handler_wrapper (sigint_remote_token);
3174}
3175
3176/* Signal handler for SIGINT, installed after SIGINT has already been
3177 sent once. It will take effect the second time that the user sends
23860348 3178 a ^C. */
43ff13b4 3179static void
fba45db2 3180handle_remote_sigint_twice (int sig)
43ff13b4 3181{
b803fb0f 3182 signal (sig, handle_remote_sigint);
43ff13b4
JM
3183 mark_async_signal_handler_wrapper (sigint_remote_twice_token);
3184}
3185
6426a772 3186/* Perform the real interruption of the target execution, in response
23860348 3187 to a ^C. */
c5aa993b 3188static void
fba45db2 3189async_remote_interrupt (gdb_client_data arg)
43ff13b4
JM
3190{
3191 if (remote_debug)
3192 fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
3193
3194 target_stop ();
3195}
3196
3197/* Perform interrupt, if the first attempt did not succeed. Just give
23860348 3198 up on the target alltogether. */
2df3850c 3199void
fba45db2 3200async_remote_interrupt_twice (gdb_client_data arg)
43ff13b4 3201{
2df3850c
JM
3202 if (remote_debug)
3203 fprintf_unfiltered (gdb_stdlog, "remote_interrupt_twice called\n");
b803fb0f
DJ
3204
3205 interrupt_query ();
43ff13b4
JM
3206}
3207
3208/* Reinstall the usual SIGINT handlers, after the target has
23860348 3209 stopped. */
6426a772
JM
3210static void
3211cleanup_sigint_signal_handler (void *dummy)
43ff13b4
JM
3212{
3213 signal (SIGINT, handle_sigint);
43ff13b4
JM
3214}
3215
c906108c
SS
3216/* Send ^C to target to halt it. Target will respond, and send us a
3217 packet. */
507f3c78 3218static void (*ofunc) (int);
c906108c 3219
7a292a7a
SS
3220/* The command line interface's stop routine. This function is installed
3221 as a signal handler for SIGINT. The first time a user requests a
3222 stop, we call remote_stop to send a break or ^C. If there is no
3223 response from the target (it didn't stop when the user requested it),
23860348 3224 we ask the user if he'd like to detach from the target. */
c906108c 3225static void
fba45db2 3226remote_interrupt (int signo)
c906108c 3227{
23860348 3228 /* If this doesn't work, try more severe steps. */
7a292a7a
SS
3229 signal (signo, remote_interrupt_twice);
3230
b803fb0f 3231 gdb_call_async_signal_handler (sigint_remote_token, 1);
7a292a7a
SS
3232}
3233
3234/* The user typed ^C twice. */
3235
3236static void
fba45db2 3237remote_interrupt_twice (int signo)
7a292a7a
SS
3238{
3239 signal (signo, ofunc);
b803fb0f 3240 gdb_call_async_signal_handler (sigint_remote_twice_token, 1);
c906108c
SS
3241 signal (signo, remote_interrupt);
3242}
7a292a7a
SS
3243
3244/* This is the generic stop called via the target vector. When a target
3245 interrupt is requested, either by the command line or the GUI, we
23860348 3246 will eventually end up here. */
c906108c 3247static void
fba45db2 3248remote_stop (void)
c906108c 3249{
7a292a7a
SS
3250 /* Send a break or a ^C, depending on user preference. */
3251 if (remote_debug)
0f71a2f6 3252 fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
c906108c 3253
7a292a7a 3254 if (remote_break)
2cd58942 3255 serial_send_break (remote_desc);
c906108c 3256 else
2cd58942 3257 serial_write (remote_desc, "\003", 1);
c906108c
SS
3258}
3259
3260/* Ask the user what to do when an interrupt is received. */
3261
3262static void
fba45db2 3263interrupt_query (void)
c906108c
SS
3264{
3265 target_terminal_ours ();
3266
3267 if (query ("Interrupted while waiting for the program.\n\
3268Give up (and stop debugging it)? "))
3269 {
3270 target_mourn_inferior ();
b803fb0f 3271 signal (SIGINT, handle_sigint);
315a522e 3272 deprecated_throw_reason (RETURN_QUIT);
c906108c
SS
3273 }
3274
3275 target_terminal_inferior ();
3276}
3277
6426a772
JM
3278/* Enable/disable target terminal ownership. Most targets can use
3279 terminal groups to control terminal ownership. Remote targets are
3280 different in that explicit transfer of ownership to/from GDB/target
23860348 3281 is required. */
6426a772
JM
3282
3283static void
3284remote_async_terminal_inferior (void)
3285{
3286 /* FIXME: cagney/1999-09-27: Shouldn't need to test for
3287 sync_execution here. This function should only be called when
3288 GDB is resuming the inferior in the forground. A background
3289 resume (``run&'') should leave GDB in control of the terminal and
23860348 3290 consequently should not call this code. */
6426a772
JM
3291 if (!sync_execution)
3292 return;
3293 /* FIXME: cagney/1999-09-27: Closely related to the above. Make
3294 calls target_terminal_*() idenpotent. The event-loop GDB talking
3295 to an asynchronous target with a synchronous command calls this
3296 function from both event-top.c and infrun.c/infcmd.c. Once GDB
3297 stops trying to transfer the terminal to the target when it
3298 shouldn't this guard can go away. */
3299 if (!remote_async_terminal_ours_p)
3300 return;
3301 delete_file_handler (input_fd);
3302 remote_async_terminal_ours_p = 0;
3303 initialize_sigint_signal_handler ();
3304 /* NOTE: At this point we could also register our selves as the
3305 recipient of all input. Any characters typed could then be
23860348 3306 passed on down to the target. */
6426a772
JM
3307}
3308
3309static void
3310remote_async_terminal_ours (void)
3311{
23860348 3312 /* See FIXME in remote_async_terminal_inferior. */
6426a772
JM
3313 if (!sync_execution)
3314 return;
23860348 3315 /* See FIXME in remote_async_terminal_inferior. */
6426a772
JM
3316 if (remote_async_terminal_ours_p)
3317 return;
3318 cleanup_sigint_signal_handler (NULL);
3319 add_file_handler (input_fd, stdin_event_handler, 0);
3320 remote_async_terminal_ours_p = 1;
3321}
3322
c906108c
SS
3323/* If nonzero, ignore the next kill. */
3324
3325int kill_kludge;
3326
3327void
917317f4 3328remote_console_output (char *msg)
c906108c
SS
3329{
3330 char *p;
3331
c5aa993b 3332 for (p = msg; p[0] && p[1]; p += 2)
c906108c
SS
3333 {
3334 char tb[2];
3335 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
3336 tb[0] = c;
3337 tb[1] = 0;
43ff13b4 3338 fputs_unfiltered (tb, gdb_stdtarg);
c906108c 3339 }
917317f4 3340 gdb_flush (gdb_stdtarg);
c906108c
SS
3341}
3342
0f71a2f6
JM
3343/* Wait until the remote machine stops, then return,
3344 storing status in STATUS just as `wait' would.
802188a7 3345 Returns "pid", which in the case of a multi-threaded
0f71a2f6 3346 remote OS, is the thread-id. */
c906108c 3347
39f77062
KB
3348static ptid_t
3349remote_wait (ptid_t ptid, struct target_waitstatus *status)
c906108c 3350{
d01949b6 3351 struct remote_state *rs = get_remote_state ();
ea9c271d 3352 struct remote_arch_state *rsa = get_remote_arch_state ();
b2dd6311 3353 ULONGEST thread_num = -1;
3c3bea1c 3354 ULONGEST addr;
cfa9d6d9 3355 int solibs_changed = 0;
c906108c
SS
3356
3357 status->kind = TARGET_WAITKIND_EXITED;
3358 status->value.integer = 0;
3359
3360 while (1)
3361 {
2e9f7625 3362 char *buf, *p;
c906108c 3363
2d717e4f
DJ
3364 if (rs->cached_wait_status)
3365 /* Use the cached wait status, but only once. */
3366 rs->cached_wait_status = 0;
3367 else
9fa2223d 3368 {
2d717e4f
DJ
3369 ofunc = signal (SIGINT, remote_interrupt);
3370 /* If the user hit C-c before this packet, or between packets,
3371 pretend that it was hit right here. */
3372 if (quit_flag)
3373 {
3374 quit_flag = 0;
3375 remote_interrupt (SIGINT);
3376 }
3377 getpkt (&rs->buf, &rs->buf_size, 1);
3378 signal (SIGINT, ofunc);
9fa2223d 3379 }
c906108c 3380
2e9f7625
DJ
3381 buf = rs->buf;
3382
3c3bea1c
GS
3383 remote_stopped_by_watchpoint_p = 0;
3384
c906108c
SS
3385 switch (buf[0])
3386 {
23860348 3387 case 'E': /* Error of some sort. */
20b4711e
DJ
3388 /* We're out of sync with the target now. Did it continue or not?
3389 Not is more likely, so report a stop. */
8a3fe4f8 3390 warning (_("Remote failure reply: %s"), buf);
20b4711e
DJ
3391 status->kind = TARGET_WAITKIND_STOPPED;
3392 status->value.sig = TARGET_SIGNAL_0;
3393 goto got_status;
23860348 3394 case 'F': /* File-I/O request. */
449092f6
CV
3395 remote_fileio_request (buf);
3396 continue;
23860348 3397 case 'T': /* Status with PC, SP, FP, ... */
c906108c 3398 {
cfd77fa1 3399 gdb_byte regs[MAX_REGISTER_SIZE];
c906108c 3400
23860348 3401 /* Expedited reply, containing Signal, {regno, reg} repeat. */
c906108c 3402 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
c5aa993b
JM
3403 ss = signal number
3404 n... = register number
3405 r... = register contents
3406 */
c906108c
SS
3407 p = &buf[3]; /* after Txx */
3408
3409 while (*p)
3410 {
cfd77fa1 3411 char *p1;
c906108c 3412 char *p_temp;
97345198 3413 int fieldsize;
3c3bea1c
GS
3414 LONGEST pnum = 0;
3415
23860348
MS
3416 /* If the packet contains a register number save it in
3417 pnum and set p1 to point to the character following
3418 it. Otherwise p1 points to p. */
c906108c 3419
23860348
MS
3420 /* If this packet is an awatch packet, don't parse the
3421 'a' as a register number. */
3c3bea1c
GS
3422
3423 if (strncmp (p, "awatch", strlen("awatch")) != 0)
3424 {
3425 /* Read the ``P'' register number. */
3426 pnum = strtol (p, &p_temp, 16);
cfd77fa1 3427 p1 = p_temp;
3c3bea1c 3428 }
802188a7 3429 else
3c3bea1c 3430 p1 = p;
c906108c 3431
23860348 3432 if (p1 == p) /* No register number present here. */
c906108c 3433 {
cfd77fa1 3434 p1 = strchr (p, ':');
c906108c 3435 if (p1 == NULL)
670aa98f 3436 error (_("Malformed packet(a) (missing colon): %s\n\
8a3fe4f8 3437Packet: '%s'\n"),
670aa98f 3438 p, buf);
3c3bea1c 3439 if (strncmp (p, "thread", p1 - p) == 0)
c906108c
SS
3440 {
3441 p_temp = unpack_varlen_hex (++p1, &thread_num);
3442 record_currthread (thread_num);
cfd77fa1 3443 p = p_temp;
c906108c 3444 }
3c3bea1c
GS
3445 else if ((strncmp (p, "watch", p1 - p) == 0)
3446 || (strncmp (p, "rwatch", p1 - p) == 0)
3447 || (strncmp (p, "awatch", p1 - p) == 0))
3448 {
3449 remote_stopped_by_watchpoint_p = 1;
3450 p = unpack_varlen_hex (++p1, &addr);
3451 remote_watch_data_address = (CORE_ADDR)addr;
3452 }
cfa9d6d9
DJ
3453 else if (strncmp (p, "library", p1 - p) == 0)
3454 {
3455 p1++;
3456 p_temp = p1;
3457 while (*p_temp && *p_temp != ';')
3458 p_temp++;
3459
3460 solibs_changed = 1;
3461 p = p_temp;
3462 }
3c3bea1c
GS
3463 else
3464 {
3465 /* Silently skip unknown optional info. */
3466 p_temp = strchr (p1 + 1, ';');
3467 if (p_temp)
cfd77fa1 3468 p = p_temp;
3c3bea1c 3469 }
c906108c
SS
3470 }
3471 else
3472 {
ea9c271d 3473 struct packet_reg *reg = packet_reg_from_pnum (rsa, pnum);
c906108c
SS
3474 p = p1;
3475
3476 if (*p++ != ':')
8a3fe4f8
AC
3477 error (_("Malformed packet(b) (missing colon): %s\n\
3478Packet: '%s'\n"),
3fcb8548 3479 p, buf);
c906108c 3480
ad10f812 3481 if (reg == NULL)
8a3fe4f8
AC
3482 error (_("Remote sent bad register number %s: %s\n\
3483Packet: '%s'\n"),
3fcb8548 3484 phex_nz (pnum, 0), p, buf);
c906108c 3485
cfd77fa1 3486 fieldsize = hex2bin (p, regs,
2bc416ba 3487 register_size (current_gdbarch,
23860348 3488 reg->regnum));
97345198 3489 p += 2 * fieldsize;
2bc416ba 3490 if (fieldsize < register_size (current_gdbarch,
23860348 3491 reg->regnum))
8a3fe4f8 3492 warning (_("Remote reply is too short: %s"), buf);
594f7785 3493 regcache_raw_supply (get_current_regcache (),
23860348 3494 reg->regnum, regs);
c906108c
SS
3495 }
3496
3497 if (*p++ != ';')
2bc416ba 3498 error (_("Remote register badly formatted: %s\nhere: %s"),
23860348 3499 buf, p);
c906108c
SS
3500 }
3501 }
3502 /* fall through */
23860348 3503 case 'S': /* Old style status, just signal only. */
cfa9d6d9
DJ
3504 if (solibs_changed)
3505 status->kind = TARGET_WAITKIND_LOADED;
3506 else
3507 {
3508 status->kind = TARGET_WAITKIND_STOPPED;
3509 status->value.sig = (enum target_signal)
3510 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
3511 }
c906108c 3512
0f71a2f6
JM
3513 if (buf[3] == 'p')
3514 {
0f71a2f6
JM
3515 thread_num = strtol ((const char *) &buf[4], NULL, 16);
3516 record_currthread (thread_num);
3517 }
c906108c 3518 goto got_status;
23860348 3519 case 'W': /* Target exited. */
c906108c
SS
3520 {
3521 /* The remote process exited. */
3522 status->kind = TARGET_WAITKIND_EXITED;
3523 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
3524 goto got_status;
3525 }
3526 case 'X':
3527 status->kind = TARGET_WAITKIND_SIGNALLED;
3528 status->value.sig = (enum target_signal)
3529 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
3530 kill_kludge = 1;
3531
3532 goto got_status;
23860348 3533 case 'O': /* Console output. */
c906108c
SS
3534 remote_console_output (buf + 1);
3535 continue;
3536 case '\0':
3537 if (last_sent_signal != TARGET_SIGNAL_0)
3538 {
3539 /* Zero length reply means that we tried 'S' or 'C' and
c5aa993b 3540 the remote system doesn't support it. */
c906108c
SS
3541 target_terminal_ours_for_output ();
3542 printf_filtered
3543 ("Can't send signals to this remote system. %s not sent.\n",
3544 target_signal_to_name (last_sent_signal));
3545 last_sent_signal = TARGET_SIGNAL_0;
3546 target_terminal_inferior ();
3547
3548 strcpy ((char *) buf, last_sent_step ? "s" : "c");
3549 putpkt ((char *) buf);
3550 continue;
3551 }
3552 /* else fallthrough */
3553 default:
8a3fe4f8 3554 warning (_("Invalid remote reply: %s"), buf);
c906108c
SS
3555 continue;
3556 }
3557 }
c5aa993b 3558got_status:
c906108c
SS
3559 if (thread_num != -1)
3560 {
39f77062 3561 return pid_to_ptid (thread_num);
c906108c 3562 }
39f77062 3563 return inferior_ptid;
c906108c
SS
3564}
3565
23860348 3566/* Async version of remote_wait. */
39f77062
KB
3567static ptid_t
3568remote_async_wait (ptid_t ptid, struct target_waitstatus *status)
43ff13b4 3569{
d01949b6 3570 struct remote_state *rs = get_remote_state ();
ea9c271d 3571 struct remote_arch_state *rsa = get_remote_arch_state ();
b2dd6311 3572 ULONGEST thread_num = -1;
3c3bea1c 3573 ULONGEST addr;
cfa9d6d9 3574 int solibs_changed = 0;
43ff13b4
JM
3575
3576 status->kind = TARGET_WAITKIND_EXITED;
3577 status->value.integer = 0;
3578
3c3bea1c
GS
3579 remote_stopped_by_watchpoint_p = 0;
3580
43ff13b4
JM
3581 while (1)
3582 {
2e9f7625 3583 char *buf, *p;
c5aa993b 3584
2d717e4f
DJ
3585 if (rs->cached_wait_status)
3586 /* Use the cached wait status, but only once. */
3587 rs->cached_wait_status = 0;
3588 else
9fa2223d 3589 {
2d717e4f 3590 if (!target_is_async_p ())
9fa2223d 3591 {
2d717e4f
DJ
3592 ofunc = signal (SIGINT, remote_interrupt);
3593 /* If the user hit C-c before this packet, or between packets,
3594 pretend that it was hit right here. */
3595 if (quit_flag)
3596 {
3597 quit_flag = 0;
3598 remote_interrupt (SIGINT);
3599 }
9fa2223d 3600 }
2d717e4f
DJ
3601 /* FIXME: cagney/1999-09-27: If we're in async mode we should
3602 _never_ wait for ever -> test on target_is_async_p().
3603 However, before we do that we need to ensure that the caller
3604 knows how to take the target into/out of async mode. */
3605 getpkt (&rs->buf, &rs->buf_size, wait_forever_enabled_p);
3606 if (!target_is_async_p ())
3607 signal (SIGINT, ofunc);
9fa2223d 3608 }
43ff13b4 3609
2e9f7625
DJ
3610 buf = rs->buf;
3611
43ff13b4
JM
3612 switch (buf[0])
3613 {
23860348 3614 case 'E': /* Error of some sort. */
20b4711e
DJ
3615 /* We're out of sync with the target now. Did it continue or not?
3616 Not is more likely, so report a stop. */
8a3fe4f8 3617 warning (_("Remote failure reply: %s"), buf);
20b4711e
DJ
3618 status->kind = TARGET_WAITKIND_STOPPED;
3619 status->value.sig = TARGET_SIGNAL_0;
3620 goto got_status;
23860348 3621 case 'F': /* File-I/O request. */
449092f6
CV
3622 remote_fileio_request (buf);
3623 continue;
23860348 3624 case 'T': /* Status with PC, SP, FP, ... */
43ff13b4 3625 {
cfd77fa1 3626 gdb_byte regs[MAX_REGISTER_SIZE];
43ff13b4 3627
23860348 3628 /* Expedited reply, containing Signal, {regno, reg} repeat. */
43ff13b4 3629 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
c5aa993b
JM
3630 ss = signal number
3631 n... = register number
3632 r... = register contents
3633 */
43ff13b4
JM
3634 p = &buf[3]; /* after Txx */
3635
3636 while (*p)
3637 {
cfd77fa1 3638 char *p1;
43ff13b4 3639 char *p_temp;
6c3f2dbf 3640 int fieldsize;
3c3bea1c 3641 long pnum = 0;
43ff13b4 3642
23860348
MS
3643 /* If the packet contains a register number, save it
3644 in pnum and set p1 to point to the character
3645 following it. Otherwise p1 points to p. */
3c3bea1c
GS
3646
3647 /* If this packet is an awatch packet, don't parse the 'a'
3648 as a register number. */
802188a7 3649
cfa9d6d9 3650 if (strncmp (p, "awatch", strlen("awatch")) != 0)
3c3bea1c
GS
3651 {
3652 /* Read the register number. */
3653 pnum = strtol (p, &p_temp, 16);
cfd77fa1 3654 p1 = p_temp;
3c3bea1c 3655 }
802188a7 3656 else
3c3bea1c 3657 p1 = p;
43ff13b4 3658
23860348 3659 if (p1 == p) /* No register number present here. */
43ff13b4 3660 {
cfd77fa1 3661 p1 = strchr (p, ':');
43ff13b4 3662 if (p1 == NULL)
8a3fe4f8
AC
3663 error (_("Malformed packet(a) (missing colon): %s\n\
3664Packet: '%s'\n"),
3fcb8548 3665 p, buf);
3c3bea1c 3666 if (strncmp (p, "thread", p1 - p) == 0)
43ff13b4
JM
3667 {
3668 p_temp = unpack_varlen_hex (++p1, &thread_num);
3669 record_currthread (thread_num);
cfd77fa1 3670 p = p_temp;
43ff13b4 3671 }
3c3bea1c
GS
3672 else if ((strncmp (p, "watch", p1 - p) == 0)
3673 || (strncmp (p, "rwatch", p1 - p) == 0)
3674 || (strncmp (p, "awatch", p1 - p) == 0))
3675 {
3676 remote_stopped_by_watchpoint_p = 1;
3677 p = unpack_varlen_hex (++p1, &addr);
3678 remote_watch_data_address = (CORE_ADDR)addr;
3679 }
cfa9d6d9
DJ
3680 else if (strncmp (p, "library", p1 - p) == 0)
3681 {
3682 p1++;
3683 p_temp = p1;
3684 while (*p_temp && *p_temp != ';')
3685 p_temp++;
3686
3687 solibs_changed = 1;
3688 p = p_temp;
3689 }
3c3bea1c
GS
3690 else
3691 {
3692 /* Silently skip unknown optional info. */
cfd77fa1 3693 p_temp = strchr (p1 + 1, ';');
3c3bea1c
GS
3694 if (p_temp)
3695 p = p_temp;
3696 }
43ff13b4 3697 }
802188a7 3698
43ff13b4
JM
3699 else
3700 {
ea9c271d 3701 struct packet_reg *reg = packet_reg_from_pnum (rsa, pnum);
43ff13b4 3702 p = p1;
43ff13b4 3703 if (*p++ != ':')
8a3fe4f8
AC
3704 error (_("Malformed packet(b) (missing colon): %s\n\
3705Packet: '%s'\n"),
3fcb8548 3706 p, buf);
43ff13b4 3707
ad10f812 3708 if (reg == NULL)
8a3fe4f8
AC
3709 error (_("Remote sent bad register number %ld: %s\n\
3710Packet: '%s'\n"),
3fcb8548 3711 pnum, p, buf);
43ff13b4 3712
cfd77fa1 3713 fieldsize = hex2bin (p, regs,
2bc416ba 3714 register_size (current_gdbarch,
23860348 3715 reg->regnum));
6c3f2dbf 3716 p += 2 * fieldsize;
2bc416ba 3717 if (fieldsize < register_size (current_gdbarch,
23860348 3718 reg->regnum))
8a3fe4f8 3719 warning (_("Remote reply is too short: %s"), buf);
594f7785
UW
3720 regcache_raw_supply (get_current_regcache (),
3721 reg->regnum, regs);
43ff13b4
JM
3722 }
3723
3724 if (*p++ != ';')
8a3fe4f8 3725 error (_("Remote register badly formatted: %s\nhere: %s"),
0a2cfde4 3726 buf, p);
43ff13b4
JM
3727 }
3728 }
3729 /* fall through */
23860348 3730 case 'S': /* Old style status, just signal only. */
cfa9d6d9
DJ
3731 if (solibs_changed)
3732 status->kind = TARGET_WAITKIND_LOADED;
3733 else
3734 {
3735 status->kind = TARGET_WAITKIND_STOPPED;
3736 status->value.sig = (enum target_signal)
3737 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
3738 }
43ff13b4
JM
3739
3740 if (buf[3] == 'p')
3741 {
43ff13b4
JM
3742 thread_num = strtol ((const char *) &buf[4], NULL, 16);
3743 record_currthread (thread_num);
3744 }
43ff13b4 3745 goto got_status;
23860348 3746 case 'W': /* Target exited. */
43ff13b4
JM
3747 {
3748 /* The remote process exited. */
3749 status->kind = TARGET_WAITKIND_EXITED;
3750 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
3751 goto got_status;
3752 }
3753 case 'X':
3754 status->kind = TARGET_WAITKIND_SIGNALLED;
3755 status->value.sig = (enum target_signal)
3756 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
3757 kill_kludge = 1;
3758
3759 goto got_status;
23860348 3760 case 'O': /* Console output. */
43ff13b4 3761 remote_console_output (buf + 1);
c4093a6a 3762 /* Return immediately to the event loop. The event loop will
23860348 3763 still be waiting on the inferior afterwards. */
c4093a6a
JM
3764 status->kind = TARGET_WAITKIND_IGNORE;
3765 goto got_status;
43ff13b4
JM
3766 case '\0':
3767 if (last_sent_signal != TARGET_SIGNAL_0)
3768 {
3769 /* Zero length reply means that we tried 'S' or 'C' and
c5aa993b 3770 the remote system doesn't support it. */
43ff13b4
JM
3771 target_terminal_ours_for_output ();
3772 printf_filtered
3773 ("Can't send signals to this remote system. %s not sent.\n",
3774 target_signal_to_name (last_sent_signal));
3775 last_sent_signal = TARGET_SIGNAL_0;
3776 target_terminal_inferior ();
3777
3778 strcpy ((char *) buf, last_sent_step ? "s" : "c");
3779 putpkt ((char *) buf);
3780 continue;
3781 }
3782 /* else fallthrough */
3783 default:
8a3fe4f8 3784 warning (_("Invalid remote reply: %s"), buf);
43ff13b4
JM
3785 continue;
3786 }
3787 }
c5aa993b 3788got_status:
43ff13b4
JM
3789 if (thread_num != -1)
3790 {
39f77062 3791 return pid_to_ptid (thread_num);
43ff13b4 3792 }
39f77062 3793 return inferior_ptid;
43ff13b4
JM
3794}
3795
74ca34ce 3796/* Fetch a single register using a 'p' packet. */
c906108c 3797
b96ec7ac 3798static int
56be3814 3799fetch_register_using_p (struct regcache *regcache, struct packet_reg *reg)
b96ec7ac
AC
3800{
3801 struct remote_state *rs = get_remote_state ();
2e9f7625 3802 char *buf, *p;
b96ec7ac
AC
3803 char regp[MAX_REGISTER_SIZE];
3804 int i;
3805
74ca34ce
DJ
3806 if (remote_protocol_packets[PACKET_p].support == PACKET_DISABLE)
3807 return 0;
3808
3809 if (reg->pnum == -1)
3810 return 0;
3811
2e9f7625 3812 p = rs->buf;
fcad0fa4 3813 *p++ = 'p';
74ca34ce 3814 p += hexnumstr (p, reg->pnum);
fcad0fa4 3815 *p++ = '\0';
6d820c5c 3816 remote_send (&rs->buf, &rs->buf_size);
3f9a994c 3817
2e9f7625
DJ
3818 buf = rs->buf;
3819
74ca34ce
DJ
3820 switch (packet_ok (buf, &remote_protocol_packets[PACKET_p]))
3821 {
3822 case PACKET_OK:
3823 break;
3824 case PACKET_UNKNOWN:
3825 return 0;
3826 case PACKET_ERROR:
3827 error (_("Could not fetch register \"%s\""),
4a22f64d 3828 gdbarch_register_name (get_regcache_arch (regcache), reg->regnum));
74ca34ce 3829 }
3f9a994c
JB
3830
3831 /* If this register is unfetchable, tell the regcache. */
3832 if (buf[0] == 'x')
8480adf2 3833 {
56be3814 3834 regcache_raw_supply (regcache, reg->regnum, NULL);
8480adf2 3835 return 1;
b96ec7ac 3836 }
b96ec7ac 3837
3f9a994c
JB
3838 /* Otherwise, parse and supply the value. */
3839 p = buf;
3840 i = 0;
3841 while (p[0] != 0)
3842 {
3843 if (p[1] == 0)
74ca34ce 3844 error (_("fetch_register_using_p: early buf termination"));
3f9a994c
JB
3845
3846 regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]);
3847 p += 2;
3848 }
56be3814 3849 regcache_raw_supply (regcache, reg->regnum, regp);
3f9a994c 3850 return 1;
b96ec7ac
AC
3851}
3852
74ca34ce
DJ
3853/* Fetch the registers included in the target's 'g' packet. */
3854
29709017
DJ
3855static int
3856send_g_packet (void)
c906108c 3857{
d01949b6 3858 struct remote_state *rs = get_remote_state ();
74ca34ce 3859 int i, buf_len;
c906108c 3860 char *p;
74ca34ce 3861 char *regs;
c906108c 3862
74ca34ce
DJ
3863 sprintf (rs->buf, "g");
3864 remote_send (&rs->buf, &rs->buf_size);
c906108c 3865
29709017
DJ
3866 /* We can get out of synch in various cases. If the first character
3867 in the buffer is not a hex character, assume that has happened
3868 and try to fetch another packet to read. */
3869 while ((rs->buf[0] < '0' || rs->buf[0] > '9')
3870 && (rs->buf[0] < 'A' || rs->buf[0] > 'F')
3871 && (rs->buf[0] < 'a' || rs->buf[0] > 'f')
3872 && rs->buf[0] != 'x') /* New: unavailable register value. */
3873 {
3874 if (remote_debug)
3875 fprintf_unfiltered (gdb_stdlog,
3876 "Bad register packet; fetching a new packet\n");
3877 getpkt (&rs->buf, &rs->buf_size, 0);
3878 }
3879
74ca34ce
DJ
3880 buf_len = strlen (rs->buf);
3881
3882 /* Sanity check the received packet. */
3883 if (buf_len % 2 != 0)
3884 error (_("Remote 'g' packet reply is of odd length: %s"), rs->buf);
29709017
DJ
3885
3886 return buf_len / 2;
3887}
3888
3889static void
56be3814 3890process_g_packet (struct regcache *regcache)
29709017 3891{
4a22f64d 3892 struct gdbarch *gdbarch = get_regcache_arch (regcache);
29709017
DJ
3893 struct remote_state *rs = get_remote_state ();
3894 struct remote_arch_state *rsa = get_remote_arch_state ();
3895 int i, buf_len;
3896 char *p;
3897 char *regs;
3898
3899 buf_len = strlen (rs->buf);
3900
3901 /* Further sanity checks, with knowledge of the architecture. */
74ca34ce
DJ
3902 if (buf_len > 2 * rsa->sizeof_g_packet)
3903 error (_("Remote 'g' packet reply is too long: %s"), rs->buf);
3904
3905 /* Save the size of the packet sent to us by the target. It is used
3906 as a heuristic when determining the max size of packets that the
3907 target can safely receive. */
3908 if (rsa->actual_register_packet_size == 0)
3909 rsa->actual_register_packet_size = buf_len;
3910
3911 /* If this is smaller than we guessed the 'g' packet would be,
3912 update our records. A 'g' reply that doesn't include a register's
3913 value implies either that the register is not available, or that
3914 the 'p' packet must be used. */
3915 if (buf_len < 2 * rsa->sizeof_g_packet)
b323314b 3916 {
74ca34ce
DJ
3917 rsa->sizeof_g_packet = buf_len / 2;
3918
4a22f64d 3919 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
b96ec7ac 3920 {
74ca34ce
DJ
3921 if (rsa->regs[i].pnum == -1)
3922 continue;
3923
3924 if (rsa->regs[i].offset >= rsa->sizeof_g_packet)
3925 rsa->regs[i].in_g_packet = 0;
b96ec7ac 3926 else
74ca34ce 3927 rsa->regs[i].in_g_packet = 1;
b96ec7ac 3928 }
74ca34ce 3929 }
b323314b 3930
74ca34ce 3931 regs = alloca (rsa->sizeof_g_packet);
c906108c
SS
3932
3933 /* Unimplemented registers read as all bits zero. */
ea9c271d 3934 memset (regs, 0, rsa->sizeof_g_packet);
c906108c 3935
c906108c
SS
3936 /* Reply describes registers byte by byte, each byte encoded as two
3937 hex characters. Suck them all up, then supply them to the
3938 register cacheing/storage mechanism. */
3939
74ca34ce 3940 p = rs->buf;
ea9c271d 3941 for (i = 0; i < rsa->sizeof_g_packet; i++)
c906108c 3942 {
74ca34ce
DJ
3943 if (p[0] == 0 || p[1] == 0)
3944 /* This shouldn't happen - we adjusted sizeof_g_packet above. */
3945 internal_error (__FILE__, __LINE__,
3946 "unexpected end of 'g' packet reply");
3947
c906108c 3948 if (p[0] == 'x' && p[1] == 'x')
c5aa993b 3949 regs[i] = 0; /* 'x' */
c906108c
SS
3950 else
3951 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
3952 p += 2;
3953 }
3954
ad10f812 3955 {
b323314b 3956 int i;
4a22f64d 3957 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
ad10f812 3958 {
ea9c271d 3959 struct packet_reg *r = &rsa->regs[i];
b323314b
AC
3960 if (r->in_g_packet)
3961 {
74ca34ce
DJ
3962 if (r->offset * 2 >= strlen (rs->buf))
3963 /* This shouldn't happen - we adjusted in_g_packet above. */
3964 internal_error (__FILE__, __LINE__,
3965 "unexpected end of 'g' packet reply");
3966 else if (rs->buf[r->offset * 2] == 'x')
8ccc1287 3967 {
74ca34ce 3968 gdb_assert (r->offset * 2 < strlen (rs->buf));
8ccc1287
AC
3969 /* The register isn't available, mark it as such (at
3970 the same time setting the value to zero). */
56be3814 3971 regcache_raw_supply (regcache, r->regnum, NULL);
8ccc1287
AC
3972 }
3973 else
56be3814 3974 regcache_raw_supply (regcache, r->regnum,
8ccc1287 3975 regs + r->offset);
b323314b 3976 }
ad10f812
AC
3977 }
3978 }
c906108c
SS
3979}
3980
29709017 3981static void
56be3814 3982fetch_registers_using_g (struct regcache *regcache)
29709017
DJ
3983{
3984 send_g_packet ();
56be3814 3985 process_g_packet (regcache);
29709017
DJ
3986}
3987
74ca34ce 3988static void
56be3814 3989remote_fetch_registers (struct regcache *regcache, int regnum)
74ca34ce
DJ
3990{
3991 struct remote_state *rs = get_remote_state ();
3992 struct remote_arch_state *rsa = get_remote_arch_state ();
3993 int i;
3994
3995 set_thread (PIDGET (inferior_ptid), 1);
3996
3997 if (regnum >= 0)
3998 {
3999 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
4000 gdb_assert (reg != NULL);
4001
4002 /* If this register might be in the 'g' packet, try that first -
4003 we are likely to read more than one register. If this is the
4004 first 'g' packet, we might be overly optimistic about its
4005 contents, so fall back to 'p'. */
4006 if (reg->in_g_packet)
4007 {
56be3814 4008 fetch_registers_using_g (regcache);
74ca34ce
DJ
4009 if (reg->in_g_packet)
4010 return;
4011 }
4012
56be3814 4013 if (fetch_register_using_p (regcache, reg))
74ca34ce
DJ
4014 return;
4015
4016 /* This register is not available. */
56be3814 4017 regcache_raw_supply (regcache, reg->regnum, NULL);
74ca34ce
DJ
4018
4019 return;
4020 }
4021
56be3814 4022 fetch_registers_using_g (regcache);
74ca34ce 4023
4a22f64d 4024 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
74ca34ce 4025 if (!rsa->regs[i].in_g_packet)
56be3814 4026 if (!fetch_register_using_p (regcache, &rsa->regs[i]))
74ca34ce
DJ
4027 {
4028 /* This register is not available. */
56be3814 4029 regcache_raw_supply (regcache, i, NULL);
74ca34ce
DJ
4030 }
4031}
4032
c906108c
SS
4033/* Prepare to store registers. Since we may send them all (using a
4034 'G' request), we have to read out the ones we don't want to change
4035 first. */
4036
c5aa993b 4037static void
316f2060 4038remote_prepare_to_store (struct regcache *regcache)
c906108c 4039{
ea9c271d 4040 struct remote_arch_state *rsa = get_remote_arch_state ();
cf0e1e0d 4041 int i;
cfd77fa1 4042 gdb_byte buf[MAX_REGISTER_SIZE];
cf0e1e0d 4043
c906108c 4044 /* Make sure the entire registers array is valid. */
444abaca 4045 switch (remote_protocol_packets[PACKET_P].support)
5a2468f5
JM
4046 {
4047 case PACKET_DISABLE:
4048 case PACKET_SUPPORT_UNKNOWN:
cf0e1e0d 4049 /* Make sure all the necessary registers are cached. */
4a22f64d 4050 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
ea9c271d 4051 if (rsa->regs[i].in_g_packet)
316f2060 4052 regcache_raw_read (regcache, rsa->regs[i].regnum, buf);
5a2468f5
JM
4053 break;
4054 case PACKET_ENABLE:
4055 break;
4056 }
4057}
4058
ad10f812 4059/* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
23860348 4060 packet was not recognized. */
5a2468f5
JM
4061
4062static int
56be3814 4063store_register_using_P (const struct regcache *regcache, struct packet_reg *reg)
5a2468f5 4064{
4a22f64d 4065 struct gdbarch *gdbarch = get_regcache_arch (regcache);
d01949b6 4066 struct remote_state *rs = get_remote_state ();
ea9c271d 4067 struct remote_arch_state *rsa = get_remote_arch_state ();
5a2468f5 4068 /* Try storing a single register. */
6d820c5c 4069 char *buf = rs->buf;
cfd77fa1 4070 gdb_byte regp[MAX_REGISTER_SIZE];
5a2468f5 4071 char *p;
5a2468f5 4072
74ca34ce
DJ
4073 if (remote_protocol_packets[PACKET_P].support == PACKET_DISABLE)
4074 return 0;
4075
4076 if (reg->pnum == -1)
4077 return 0;
4078
ea9c271d 4079 xsnprintf (buf, get_remote_packet_size (), "P%s=", phex_nz (reg->pnum, 0));
5a2468f5 4080 p = buf + strlen (buf);
56be3814 4081 regcache_raw_collect (regcache, reg->regnum, regp);
4a22f64d 4082 bin2hex (regp, p, register_size (gdbarch, reg->regnum));
6d820c5c 4083 remote_send (&rs->buf, &rs->buf_size);
5a2468f5 4084
74ca34ce
DJ
4085 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_P]))
4086 {
4087 case PACKET_OK:
4088 return 1;
4089 case PACKET_ERROR:
4090 error (_("Could not write register \"%s\""),
4a22f64d 4091 gdbarch_register_name (gdbarch, reg->regnum));
74ca34ce
DJ
4092 case PACKET_UNKNOWN:
4093 return 0;
4094 default:
4095 internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
4096 }
c906108c
SS
4097}
4098
23860348
MS
4099/* Store register REGNUM, or all registers if REGNUM == -1, from the
4100 contents of the register cache buffer. FIXME: ignores errors. */
c906108c
SS
4101
4102static void
56be3814 4103store_registers_using_G (const struct regcache *regcache)
c906108c 4104{
d01949b6 4105 struct remote_state *rs = get_remote_state ();
ea9c271d 4106 struct remote_arch_state *rsa = get_remote_arch_state ();
cfd77fa1 4107 gdb_byte *regs;
c906108c
SS
4108 char *p;
4109
193cb69f
AC
4110 /* Extract all the registers in the regcache copying them into a
4111 local buffer. */
4112 {
b323314b 4113 int i;
ea9c271d
DJ
4114 regs = alloca (rsa->sizeof_g_packet);
4115 memset (regs, 0, rsa->sizeof_g_packet);
4a22f64d 4116 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
193cb69f 4117 {
ea9c271d 4118 struct packet_reg *r = &rsa->regs[i];
b323314b 4119 if (r->in_g_packet)
56be3814 4120 regcache_raw_collect (regcache, r->regnum, regs + r->offset);
193cb69f
AC
4121 }
4122 }
c906108c
SS
4123
4124 /* Command describes registers byte by byte,
4125 each byte encoded as two hex characters. */
6d820c5c 4126 p = rs->buf;
193cb69f 4127 *p++ = 'G';
74ca34ce
DJ
4128 /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets
4129 updated. */
4130 bin2hex (regs, p, rsa->sizeof_g_packet);
6d820c5c 4131 remote_send (&rs->buf, &rs->buf_size);
c906108c 4132}
74ca34ce
DJ
4133
4134/* Store register REGNUM, or all registers if REGNUM == -1, from the contents
4135 of the register cache buffer. FIXME: ignores errors. */
4136
4137static void
56be3814 4138remote_store_registers (struct regcache *regcache, int regnum)
74ca34ce
DJ
4139{
4140 struct remote_state *rs = get_remote_state ();
4141 struct remote_arch_state *rsa = get_remote_arch_state ();
4142 int i;
4143
4144 set_thread (PIDGET (inferior_ptid), 1);
4145
4146 if (regnum >= 0)
4147 {
4148 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
4149 gdb_assert (reg != NULL);
4150
4151 /* Always prefer to store registers using the 'P' packet if
4152 possible; we often change only a small number of registers.
4153 Sometimes we change a larger number; we'd need help from a
4154 higher layer to know to use 'G'. */
56be3814 4155 if (store_register_using_P (regcache, reg))
74ca34ce
DJ
4156 return;
4157
4158 /* For now, don't complain if we have no way to write the
4159 register. GDB loses track of unavailable registers too
4160 easily. Some day, this may be an error. We don't have
4161 any way to read the register, either... */
4162 if (!reg->in_g_packet)
4163 return;
4164
56be3814 4165 store_registers_using_G (regcache);
74ca34ce
DJ
4166 return;
4167 }
4168
56be3814 4169 store_registers_using_G (regcache);
74ca34ce 4170
4a22f64d 4171 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
74ca34ce 4172 if (!rsa->regs[i].in_g_packet)
56be3814 4173 if (!store_register_using_P (regcache, &rsa->regs[i]))
74ca34ce
DJ
4174 /* See above for why we do not issue an error here. */
4175 continue;
4176}
c906108c
SS
4177\f
4178
4179/* Return the number of hex digits in num. */
4180
4181static int
fba45db2 4182hexnumlen (ULONGEST num)
c906108c
SS
4183{
4184 int i;
4185
4186 for (i = 0; num != 0; i++)
4187 num >>= 4;
4188
4189 return max (i, 1);
4190}
4191
2df3850c 4192/* Set BUF to the minimum number of hex digits representing NUM. */
c906108c
SS
4193
4194static int
fba45db2 4195hexnumstr (char *buf, ULONGEST num)
c906108c 4196{
c906108c 4197 int len = hexnumlen (num);
2df3850c
JM
4198 return hexnumnstr (buf, num, len);
4199}
4200
c906108c 4201
2df3850c 4202/* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
c906108c 4203
2df3850c 4204static int
fba45db2 4205hexnumnstr (char *buf, ULONGEST num, int width)
2df3850c
JM
4206{
4207 int i;
4208
4209 buf[width] = '\0';
4210
4211 for (i = width - 1; i >= 0; i--)
c906108c 4212 {
c5aa993b 4213 buf[i] = "0123456789abcdef"[(num & 0xf)];
c906108c
SS
4214 num >>= 4;
4215 }
4216
2df3850c 4217 return width;
c906108c
SS
4218}
4219
23860348 4220/* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
c906108c
SS
4221
4222static CORE_ADDR
fba45db2 4223remote_address_masked (CORE_ADDR addr)
c906108c 4224{
911c95a5
UW
4225 int address_size = remote_address_size;
4226 /* If "remoteaddresssize" was not set, default to target address size. */
4227 if (!address_size)
4228 address_size = gdbarch_addr_bit (current_gdbarch);
4229
4230 if (address_size > 0
4231 && address_size < (sizeof (ULONGEST) * 8))
c906108c
SS
4232 {
4233 /* Only create a mask when that mask can safely be constructed
23860348 4234 in a ULONGEST variable. */
c906108c 4235 ULONGEST mask = 1;
911c95a5 4236 mask = (mask << address_size) - 1;
c906108c
SS
4237 addr &= mask;
4238 }
4239 return addr;
4240}
4241
a31ea83d
DJ
4242/* Convert BUFFER, binary data at least LEN bytes long, into escaped
4243 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
4244 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
4245 (which may be more than *OUT_LEN due to escape characters). The
4246 total number of bytes in the output buffer will be at most
4247 OUT_MAXLEN. */
4248
4249static int
4250remote_escape_output (const gdb_byte *buffer, int len,
4251 gdb_byte *out_buf, int *out_len,
4252 int out_maxlen)
4253{
4254 int input_index, output_index;
4255
4256 output_index = 0;
4257 for (input_index = 0; input_index < len; input_index++)
4258 {
4259 gdb_byte b = buffer[input_index];
4260
4261 if (b == '$' || b == '#' || b == '}')
4262 {
4263 /* These must be escaped. */
4264 if (output_index + 2 > out_maxlen)
4265 break;
4266 out_buf[output_index++] = '}';
4267 out_buf[output_index++] = b ^ 0x20;
4268 }
4269 else
4270 {
4271 if (output_index + 1 > out_maxlen)
4272 break;
4273 out_buf[output_index++] = b;
4274 }
4275 }
4276
4277 *out_len = input_index;
4278 return output_index;
4279}
4280
0876f84a
DJ
4281/* Convert BUFFER, escaped data LEN bytes long, into binary data
4282 in OUT_BUF. Return the number of bytes written to OUT_BUF.
4283 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
4284
4285 This function reverses remote_escape_output. It allows more
4286 escaped characters than that function does, in particular because
4287 '*' must be escaped to avoid the run-length encoding processing
4288 in reading packets. */
4289
4290static int
4291remote_unescape_input (const gdb_byte *buffer, int len,
4292 gdb_byte *out_buf, int out_maxlen)
4293{
4294 int input_index, output_index;
4295 int escaped;
4296
4297 output_index = 0;
4298 escaped = 0;
4299 for (input_index = 0; input_index < len; input_index++)
4300 {
4301 gdb_byte b = buffer[input_index];
4302
4303 if (output_index + 1 > out_maxlen)
4304 {
4305 warning (_("Received too much data from remote target;"
4306 " ignoring overflow."));
4307 return output_index;
4308 }
4309
4310 if (escaped)
4311 {
4312 out_buf[output_index++] = b ^ 0x20;
4313 escaped = 0;
4314 }
4315 else if (b == '}')
4316 escaped = 1;
4317 else
4318 out_buf[output_index++] = b;
4319 }
4320
4321 if (escaped)
4322 error (_("Unmatched escape character in target response."));
4323
4324 return output_index;
4325}
4326
c906108c
SS
4327/* Determine whether the remote target supports binary downloading.
4328 This is accomplished by sending a no-op memory write of zero length
4329 to the target at the specified address. It does not suffice to send
23860348
MS
4330 the whole packet, since many stubs strip the eighth bit and
4331 subsequently compute a wrong checksum, which causes real havoc with
4332 remote_write_bytes.
7a292a7a 4333
96baa820
JM
4334 NOTE: This can still lose if the serial line is not eight-bit
4335 clean. In cases like this, the user should clear "remote
23860348 4336 X-packet". */
96baa820 4337
c906108c 4338static void
fba45db2 4339check_binary_download (CORE_ADDR addr)
c906108c 4340{
d01949b6 4341 struct remote_state *rs = get_remote_state ();
24b06219 4342
444abaca 4343 switch (remote_protocol_packets[PACKET_X].support)
c906108c 4344 {
96baa820
JM
4345 case PACKET_DISABLE:
4346 break;
4347 case PACKET_ENABLE:
4348 break;
4349 case PACKET_SUPPORT_UNKNOWN:
4350 {
96baa820 4351 char *p;
802188a7 4352
2e9f7625 4353 p = rs->buf;
96baa820
JM
4354 *p++ = 'X';
4355 p += hexnumstr (p, (ULONGEST) addr);
4356 *p++ = ',';
4357 p += hexnumstr (p, (ULONGEST) 0);
4358 *p++ = ':';
4359 *p = '\0';
802188a7 4360
2e9f7625 4361 putpkt_binary (rs->buf, (int) (p - rs->buf));
6d820c5c 4362 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 4363
2e9f7625 4364 if (rs->buf[0] == '\0')
96baa820
JM
4365 {
4366 if (remote_debug)
4367 fprintf_unfiltered (gdb_stdlog,
4368 "binary downloading NOT suppported by target\n");
444abaca 4369 remote_protocol_packets[PACKET_X].support = PACKET_DISABLE;
96baa820
JM
4370 }
4371 else
4372 {
4373 if (remote_debug)
4374 fprintf_unfiltered (gdb_stdlog,
4375 "binary downloading suppported by target\n");
444abaca 4376 remote_protocol_packets[PACKET_X].support = PACKET_ENABLE;
96baa820
JM
4377 }
4378 break;
4379 }
c906108c
SS
4380 }
4381}
4382
4383/* Write memory data directly to the remote machine.
4384 This does not inform the data cache; the data cache uses this.
a76d924d 4385 HEADER is the starting part of the packet.
c906108c
SS
4386 MEMADDR is the address in the remote memory space.
4387 MYADDR is the address of the buffer in our space.
4388 LEN is the number of bytes.
a76d924d
DJ
4389 PACKET_FORMAT should be either 'X' or 'M', and indicates if we
4390 should send data as binary ('X'), or hex-encoded ('M').
4391
4392 The function creates packet of the form
4393 <HEADER><ADDRESS>,<LENGTH>:<DATA>
4394
4395 where encoding of <DATA> is termined by PACKET_FORMAT.
4396
4397 If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
4398 are omitted.
4399
4400 Returns the number of bytes transferred, or 0 (setting errno) for
23860348 4401 error. Only transfer a single packet. */
c906108c 4402
a76d924d
DJ
4403static int
4404remote_write_bytes_aux (const char *header, CORE_ADDR memaddr,
4405 const gdb_byte *myaddr, int len,
4406 char packet_format, int use_length)
c906108c 4407{
6d820c5c 4408 struct remote_state *rs = get_remote_state ();
cfd77fa1 4409 char *p;
a76d924d
DJ
4410 char *plen = NULL;
4411 int plenlen = 0;
917317f4
JM
4412 int todo;
4413 int nr_bytes;
a257b5bb 4414 int payload_size;
6765f3e5 4415 int payload_length;
a76d924d
DJ
4416 int header_length;
4417
4418 if (packet_format != 'X' && packet_format != 'M')
4419 internal_error (__FILE__, __LINE__,
4420 "remote_write_bytes_aux: bad packet format");
c906108c 4421
b2182ed2
DJ
4422 if (len <= 0)
4423 return 0;
4424
3de11b2e 4425 payload_size = get_memory_write_packet_size ();
2bc416ba 4426
6d820c5c
DJ
4427 /* The packet buffer will be large enough for the payload;
4428 get_memory_packet_size ensures this. */
a76d924d 4429 rs->buf[0] = '\0';
c906108c 4430
a257b5bb 4431 /* Compute the size of the actual payload by subtracting out the
3de11b2e
NS
4432 packet header and footer overhead: "$M<memaddr>,<len>:...#nn".
4433 */
a76d924d
DJ
4434 payload_size -= strlen ("$,:#NN");
4435 if (!use_length)
4436 /* The comma won't be used. */
4437 payload_size += 1;
4438 header_length = strlen (header);
4439 payload_size -= header_length;
3de11b2e 4440 payload_size -= hexnumlen (memaddr);
c906108c 4441
a76d924d 4442 /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
917317f4 4443
a76d924d
DJ
4444 strcat (rs->buf, header);
4445 p = rs->buf + strlen (header);
4446
4447 /* Compute a best guess of the number of bytes actually transfered. */
4448 if (packet_format == 'X')
c906108c 4449 {
23860348 4450 /* Best guess at number of bytes that will fit. */
a257b5bb 4451 todo = min (len, payload_size);
a76d924d
DJ
4452 if (use_length)
4453 payload_size -= hexnumlen (todo);
3de11b2e 4454 todo = min (todo, payload_size);
a76d924d
DJ
4455 }
4456 else
4457 {
23860348 4458 /* Num bytes that will fit. */
a257b5bb 4459 todo = min (len, payload_size / 2);
a76d924d
DJ
4460 if (use_length)
4461 payload_size -= hexnumlen (todo);
3de11b2e 4462 todo = min (todo, payload_size / 2);
917317f4 4463 }
a76d924d 4464
3de11b2e
NS
4465 if (todo <= 0)
4466 internal_error (__FILE__, __LINE__,
4467 _("minumum packet size too small to write data"));
802188a7 4468
6765f3e5
DJ
4469 /* If we already need another packet, then try to align the end
4470 of this packet to a useful boundary. */
4471 if (todo > 2 * REMOTE_ALIGN_WRITES && todo < len)
4472 todo = ((memaddr + todo) & ~(REMOTE_ALIGN_WRITES - 1)) - memaddr;
4473
a257b5bb 4474 /* Append "<memaddr>". */
917317f4
JM
4475 memaddr = remote_address_masked (memaddr);
4476 p += hexnumstr (p, (ULONGEST) memaddr);
a257b5bb 4477
a76d924d
DJ
4478 if (use_length)
4479 {
4480 /* Append ",". */
4481 *p++ = ',';
802188a7 4482
a76d924d
DJ
4483 /* Append <len>. Retain the location/size of <len>. It may need to
4484 be adjusted once the packet body has been created. */
4485 plen = p;
4486 plenlen = hexnumstr (p, (ULONGEST) todo);
4487 p += plenlen;
4488 }
a257b5bb
AC
4489
4490 /* Append ":". */
917317f4
JM
4491 *p++ = ':';
4492 *p = '\0';
802188a7 4493
a257b5bb 4494 /* Append the packet body. */
a76d924d 4495 if (packet_format == 'X')
917317f4 4496 {
917317f4
JM
4497 /* Binary mode. Send target system values byte by byte, in
4498 increasing byte addresses. Only escape certain critical
4499 characters. */
6765f3e5
DJ
4500 payload_length = remote_escape_output (myaddr, todo, p, &nr_bytes,
4501 payload_size);
4502
4503 /* If not all TODO bytes fit, then we'll need another packet. Make
9b7194bc
DJ
4504 a second try to keep the end of the packet aligned. Don't do
4505 this if the packet is tiny. */
4506 if (nr_bytes < todo && nr_bytes > 2 * REMOTE_ALIGN_WRITES)
6765f3e5
DJ
4507 {
4508 int new_nr_bytes;
4509
4510 new_nr_bytes = (((memaddr + nr_bytes) & ~(REMOTE_ALIGN_WRITES - 1))
4511 - memaddr);
4512 if (new_nr_bytes != nr_bytes)
4513 payload_length = remote_escape_output (myaddr, new_nr_bytes,
4514 p, &nr_bytes,
4515 payload_size);
4516 }
4517
4518 p += payload_length;
a76d924d 4519 if (use_length && nr_bytes < todo)
c906108c 4520 {
802188a7 4521 /* Escape chars have filled up the buffer prematurely,
917317f4
JM
4522 and we have actually sent fewer bytes than planned.
4523 Fix-up the length field of the packet. Use the same
4524 number of characters as before. */
917317f4
JM
4525 plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen);
4526 *plen = ':'; /* overwrite \0 from hexnumnstr() */
c906108c 4527 }
a76d924d
DJ
4528 }
4529 else
4530 {
917317f4
JM
4531 /* Normal mode: Send target system values byte by byte, in
4532 increasing byte addresses. Each byte is encoded as a two hex
4533 value. */
2644f393 4534 nr_bytes = bin2hex (myaddr, p, todo);
aa6c0017 4535 p += 2 * nr_bytes;
c906108c 4536 }
802188a7 4537
2e9f7625 4538 putpkt_binary (rs->buf, (int) (p - rs->buf));
6d820c5c 4539 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 4540
2e9f7625 4541 if (rs->buf[0] == 'E')
917317f4
JM
4542 {
4543 /* There is no correspondance between what the remote protocol
4544 uses for errors and errno codes. We would like a cleaner way
4545 of representing errors (big enough to include errno codes,
4546 bfd_error codes, and others). But for now just return EIO. */
4547 errno = EIO;
4548 return 0;
4549 }
802188a7 4550
23860348
MS
4551 /* Return NR_BYTES, not TODO, in case escape chars caused us to send
4552 fewer bytes than we'd planned. */
917317f4 4553 return nr_bytes;
c906108c
SS
4554}
4555
a76d924d
DJ
4556/* Write memory data directly to the remote machine.
4557 This does not inform the data cache; the data cache uses this.
4558 MEMADDR is the address in the remote memory space.
4559 MYADDR is the address of the buffer in our space.
4560 LEN is the number of bytes.
4561
4562 Returns number of bytes transferred, or 0 (setting errno) for
4563 error. Only transfer a single packet. */
4564
4565int
4566remote_write_bytes (CORE_ADDR memaddr, const gdb_byte *myaddr, int len)
4567{
4568 char *packet_format = 0;
4569
4570 /* Check whether the target supports binary download. */
4571 check_binary_download (memaddr);
4572
4573 switch (remote_protocol_packets[PACKET_X].support)
4574 {
4575 case PACKET_ENABLE:
4576 packet_format = "X";
4577 break;
4578 case PACKET_DISABLE:
4579 packet_format = "M";
4580 break;
4581 case PACKET_SUPPORT_UNKNOWN:
4582 internal_error (__FILE__, __LINE__,
4583 _("remote_write_bytes: bad internal state"));
4584 default:
4585 internal_error (__FILE__, __LINE__, _("bad switch"));
4586 }
4587
4588 return remote_write_bytes_aux (packet_format,
4589 memaddr, myaddr, len, packet_format[0], 1);
4590}
4591
c906108c
SS
4592/* Read memory data directly from the remote machine.
4593 This does not use the data cache; the data cache uses this.
4594 MEMADDR is the address in the remote memory space.
4595 MYADDR is the address of the buffer in our space.
4596 LEN is the number of bytes.
4597
4598 Returns number of bytes transferred, or 0 for error. */
4599
917317f4
JM
4600/* NOTE: cagney/1999-10-18: This function (and its siblings in other
4601 remote targets) shouldn't attempt to read the entire buffer.
4602 Instead it should read a single packet worth of data and then
4603 return the byte size of that packet to the caller. The caller (its
4604 caller and its callers caller ;-) already contains code for
23860348 4605 handling partial reads. */
917317f4 4606
449092f6 4607int
cfd77fa1 4608remote_read_bytes (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
c906108c 4609{
6d820c5c 4610 struct remote_state *rs = get_remote_state ();
23860348 4611 int max_buf_size; /* Max size of packet output buffer. */
c906108c
SS
4612 int origlen;
4613
b2182ed2
DJ
4614 if (len <= 0)
4615 return 0;
4616
11cf8741 4617 max_buf_size = get_memory_read_packet_size ();
6d820c5c
DJ
4618 /* The packet buffer will be large enough for the payload;
4619 get_memory_packet_size ensures this. */
c906108c
SS
4620
4621 origlen = len;
4622 while (len > 0)
4623 {
c906108c
SS
4624 char *p;
4625 int todo;
4626 int i;
4627
c5aa993b 4628 todo = min (len, max_buf_size / 2); /* num bytes that will fit */
c906108c
SS
4629
4630 /* construct "m"<memaddr>","<len>" */
2e9f7625 4631 /* sprintf (rs->buf, "m%lx,%x", (unsigned long) memaddr, todo); */
c906108c 4632 memaddr = remote_address_masked (memaddr);
2e9f7625 4633 p = rs->buf;
c906108c
SS
4634 *p++ = 'm';
4635 p += hexnumstr (p, (ULONGEST) memaddr);
4636 *p++ = ',';
4637 p += hexnumstr (p, (ULONGEST) todo);
4638 *p = '\0';
4639
2e9f7625 4640 putpkt (rs->buf);
6d820c5c 4641 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 4642
2e9f7625
DJ
4643 if (rs->buf[0] == 'E'
4644 && isxdigit (rs->buf[1]) && isxdigit (rs->buf[2])
4645 && rs->buf[3] == '\0')
c906108c 4646 {
23860348
MS
4647 /* There is no correspondance between what the remote
4648 protocol uses for errors and errno codes. We would like
4649 a cleaner way of representing errors (big enough to
4650 include errno codes, bfd_error codes, and others). But
4651 for now just return EIO. */
c906108c
SS
4652 errno = EIO;
4653 return 0;
4654 }
4655
c5aa993b
JM
4656 /* Reply describes memory byte by byte,
4657 each byte encoded as two hex characters. */
c906108c 4658
2e9f7625 4659 p = rs->buf;
30559e10 4660 if ((i = hex2bin (p, myaddr, todo)) < todo)
c906108c 4661 {
30559e10 4662 /* Reply is short. This means that we were able to read
23860348 4663 only part of what we wanted to. */
30559e10 4664 return i + (origlen - len);
c906108c
SS
4665 }
4666 myaddr += todo;
4667 memaddr += todo;
4668 len -= todo;
4669 }
4670 return origlen;
4671}
4672\f
4673/* Read or write LEN bytes from inferior memory at MEMADDR,
23860348
MS
4674 transferring to or from debugger address BUFFER. Write to inferior
4675 if SHOULD_WRITE is nonzero. Returns length of data written or
4676 read; 0 for error. TARGET is unused. */
392a587b 4677
c906108c 4678static int
961cb7b5 4679remote_xfer_memory (CORE_ADDR mem_addr, gdb_byte *buffer, int mem_len,
0a65a603 4680 int should_write, struct mem_attrib *attrib,
29e57380 4681 struct target_ops *target)
c906108c 4682{
4930751a
C
4683 int res;
4684
4930751a 4685 if (should_write)
b2182ed2 4686 res = remote_write_bytes (mem_addr, buffer, mem_len);
4930751a 4687 else
b2182ed2 4688 res = remote_read_bytes (mem_addr, buffer, mem_len);
4930751a
C
4689
4690 return res;
c906108c
SS
4691}
4692
a76d924d
DJ
4693/* Sends a packet with content determined by the printf format string
4694 FORMAT and the remaining arguments, then gets the reply. Returns
4695 whether the packet was a success, a failure, or unknown. */
4696
4697enum packet_result
4698remote_send_printf (const char *format, ...)
4699{
4700 struct remote_state *rs = get_remote_state ();
4701 int max_size = get_remote_packet_size ();
4702
4703 va_list ap;
4704 va_start (ap, format);
4705
4706 rs->buf[0] = '\0';
4707 if (vsnprintf (rs->buf, max_size, format, ap) >= max_size)
4708 internal_error (__FILE__, __LINE__, "Too long remote packet.");
4709
4710 if (putpkt (rs->buf) < 0)
4711 error (_("Communication problem with target."));
4712
4713 rs->buf[0] = '\0';
4714 getpkt (&rs->buf, &rs->buf_size, 0);
4715
4716 return packet_check_result (rs->buf);
4717}
4718
4719static void
4720restore_remote_timeout (void *p)
4721{
4722 int value = *(int *)p;
4723 remote_timeout = value;
4724}
4725
4726/* Flash writing can take quite some time. We'll set
4727 effectively infinite timeout for flash operations.
4728 In future, we'll need to decide on a better approach. */
4729static const int remote_flash_timeout = 1000;
4730
4731static void
4732remote_flash_erase (struct target_ops *ops,
4733 ULONGEST address, LONGEST length)
4734{
4735 int saved_remote_timeout = remote_timeout;
4736 enum packet_result ret;
4737
4738 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
4739 &saved_remote_timeout);
4740 remote_timeout = remote_flash_timeout;
4741
4742 ret = remote_send_printf ("vFlashErase:%s,%s",
4743 paddr (address),
4744 phex (length, 4));
4745 switch (ret)
4746 {
4747 case PACKET_UNKNOWN:
4748 error (_("Remote target does not support flash erase"));
4749 case PACKET_ERROR:
4750 error (_("Error erasing flash with vFlashErase packet"));
4751 default:
4752 break;
4753 }
4754
4755 do_cleanups (back_to);
4756}
4757
4758static LONGEST
4759remote_flash_write (struct target_ops *ops,
4760 ULONGEST address, LONGEST length,
4761 const gdb_byte *data)
4762{
4763 int saved_remote_timeout = remote_timeout;
4764 int ret;
4765 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
4766 &saved_remote_timeout);
4767
4768 remote_timeout = remote_flash_timeout;
4769 ret = remote_write_bytes_aux ("vFlashWrite:", address, data, length, 'X', 0);
4770 do_cleanups (back_to);
4771
4772 return ret;
4773}
4774
4775static void
4776remote_flash_done (struct target_ops *ops)
4777{
4778 int saved_remote_timeout = remote_timeout;
4779 int ret;
4780 struct cleanup *back_to = make_cleanup (restore_remote_timeout,
4781 &saved_remote_timeout);
4782
4783 remote_timeout = remote_flash_timeout;
4784 ret = remote_send_printf ("vFlashDone");
4785 do_cleanups (back_to);
4786
4787 switch (ret)
4788 {
4789 case PACKET_UNKNOWN:
4790 error (_("Remote target does not support vFlashDone"));
4791 case PACKET_ERROR:
4792 error (_("Error finishing flash operation"));
4793 default:
4794 break;
4795 }
4796}
4797
c906108c 4798static void
fba45db2 4799remote_files_info (struct target_ops *ignore)
c906108c
SS
4800{
4801 puts_filtered ("Debugging a target over a serial line.\n");
4802}
4803\f
4804/* Stuff for dealing with the packets which are part of this protocol.
4805 See comment at top of file for details. */
4806
0876f84a 4807/* Read a single character from the remote end. */
c906108c
SS
4808
4809static int
fba45db2 4810readchar (int timeout)
c906108c
SS
4811{
4812 int ch;
4813
2cd58942 4814 ch = serial_readchar (remote_desc, timeout);
c906108c 4815
2acceee2 4816 if (ch >= 0)
0876f84a 4817 return ch;
2acceee2
JM
4818
4819 switch ((enum serial_rc) ch)
c906108c
SS
4820 {
4821 case SERIAL_EOF:
2acceee2 4822 target_mourn_inferior ();
8a3fe4f8 4823 error (_("Remote connection closed"));
2acceee2 4824 /* no return */
c906108c 4825 case SERIAL_ERROR:
e2e0b3e5 4826 perror_with_name (_("Remote communication error"));
2acceee2 4827 /* no return */
c906108c 4828 case SERIAL_TIMEOUT:
2acceee2 4829 break;
c906108c 4830 }
2acceee2 4831 return ch;
c906108c
SS
4832}
4833
6d820c5c
DJ
4834/* Send the command in *BUF to the remote machine, and read the reply
4835 into *BUF. Report an error if we get an error reply. Resize
4836 *BUF using xrealloc if necessary to hold the result, and update
4837 *SIZEOF_BUF. */
c906108c
SS
4838
4839static void
6d820c5c
DJ
4840remote_send (char **buf,
4841 long *sizeof_buf)
c906108c 4842{
6d820c5c 4843 putpkt (*buf);
c2d11a7d 4844 getpkt (buf, sizeof_buf, 0);
c906108c 4845
6d820c5c
DJ
4846 if ((*buf)[0] == 'E')
4847 error (_("Remote failure reply: %s"), *buf);
c906108c
SS
4848}
4849
4850/* Display a null-terminated packet on stdout, for debugging, using C
4851 string notation. */
4852
4853static void
fba45db2 4854print_packet (char *buf)
c906108c
SS
4855{
4856 puts_filtered ("\"");
43e526b9 4857 fputstr_filtered (buf, '"', gdb_stdout);
c906108c
SS
4858 puts_filtered ("\"");
4859}
4860
4861int
fba45db2 4862putpkt (char *buf)
c906108c
SS
4863{
4864 return putpkt_binary (buf, strlen (buf));
4865}
4866
4867/* Send a packet to the remote machine, with error checking. The data
23860348 4868 of the packet is in BUF. The string in BUF can be at most
ea9c271d 4869 get_remote_packet_size () - 5 to account for the $, # and checksum,
23860348
MS
4870 and for a possible /0 if we are debugging (remote_debug) and want
4871 to print the sent packet as a string. */
c906108c
SS
4872
4873static int
fba45db2 4874putpkt_binary (char *buf, int cnt)
c906108c 4875{
2d717e4f 4876 struct remote_state *rs = get_remote_state ();
c906108c
SS
4877 int i;
4878 unsigned char csum = 0;
11cf8741 4879 char *buf2 = alloca (cnt + 6);
085dd6e6 4880
c906108c
SS
4881 int ch;
4882 int tcount = 0;
4883 char *p;
4884
2d717e4f
DJ
4885 /* We're sending out a new packet. Make sure we don't look at a
4886 stale cached response. */
4887 rs->cached_wait_status = 0;
4888
c906108c
SS
4889 /* Copy the packet into buffer BUF2, encapsulating it
4890 and giving it a checksum. */
4891
c906108c
SS
4892 p = buf2;
4893 *p++ = '$';
4894
4895 for (i = 0; i < cnt; i++)
4896 {
4897 csum += buf[i];
4898 *p++ = buf[i];
4899 }
4900 *p++ = '#';
4901 *p++ = tohex ((csum >> 4) & 0xf);
4902 *p++ = tohex (csum & 0xf);
4903
4904 /* Send it over and over until we get a positive ack. */
4905
4906 while (1)
4907 {
4908 int started_error_output = 0;
4909
4910 if (remote_debug)
4911 {
4912 *p = '\0';
43e526b9
JM
4913 fprintf_unfiltered (gdb_stdlog, "Sending packet: ");
4914 fputstrn_unfiltered (buf2, p - buf2, 0, gdb_stdlog);
d4f3574e 4915 fprintf_unfiltered (gdb_stdlog, "...");
0f71a2f6 4916 gdb_flush (gdb_stdlog);
c906108c 4917 }
2cd58942 4918 if (serial_write (remote_desc, buf2, p - buf2))
e2e0b3e5 4919 perror_with_name (_("putpkt: write failed"));
c906108c 4920
23860348 4921 /* Read until either a timeout occurs (-2) or '+' is read. */
c906108c
SS
4922 while (1)
4923 {
4924 ch = readchar (remote_timeout);
4925
c5aa993b 4926 if (remote_debug)
c906108c
SS
4927 {
4928 switch (ch)
4929 {
4930 case '+':
1216fa2c 4931 case '-':
c906108c
SS
4932 case SERIAL_TIMEOUT:
4933 case '$':
4934 if (started_error_output)
4935 {
4936 putchar_unfiltered ('\n');
4937 started_error_output = 0;
4938 }
4939 }
4940 }
4941
4942 switch (ch)
4943 {
4944 case '+':
4945 if (remote_debug)
0f71a2f6 4946 fprintf_unfiltered (gdb_stdlog, "Ack\n");
c906108c 4947 return 1;
1216fa2c
AC
4948 case '-':
4949 if (remote_debug)
4950 fprintf_unfiltered (gdb_stdlog, "Nak\n");
c906108c 4951 case SERIAL_TIMEOUT:
c5aa993b 4952 tcount++;
c906108c
SS
4953 if (tcount > 3)
4954 return 0;
23860348 4955 break; /* Retransmit buffer. */
c906108c
SS
4956 case '$':
4957 {
40e3f985 4958 if (remote_debug)
2bc416ba 4959 fprintf_unfiltered (gdb_stdlog,
23860348 4960 "Packet instead of Ack, ignoring it\n");
d6f7abdf
AC
4961 /* It's probably an old response sent because an ACK
4962 was lost. Gobble up the packet and ack it so it
4963 doesn't get retransmitted when we resend this
4964 packet. */
6d820c5c 4965 skip_frame ();
d6f7abdf 4966 serial_write (remote_desc, "+", 1);
23860348 4967 continue; /* Now, go look for +. */
c906108c
SS
4968 }
4969 default:
4970 if (remote_debug)
4971 {
4972 if (!started_error_output)
4973 {
4974 started_error_output = 1;
0f71a2f6 4975 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
c906108c 4976 }
0f71a2f6 4977 fputc_unfiltered (ch & 0177, gdb_stdlog);
c906108c
SS
4978 }
4979 continue;
4980 }
23860348 4981 break; /* Here to retransmit. */
c906108c
SS
4982 }
4983
4984#if 0
4985 /* This is wrong. If doing a long backtrace, the user should be
c5aa993b
JM
4986 able to get out next time we call QUIT, without anything as
4987 violent as interrupt_query. If we want to provide a way out of
4988 here without getting to the next QUIT, it should be based on
4989 hitting ^C twice as in remote_wait. */
c906108c
SS
4990 if (quit_flag)
4991 {
4992 quit_flag = 0;
4993 interrupt_query ();
4994 }
4995#endif
4996 }
4997}
4998
6d820c5c
DJ
4999/* Come here after finding the start of a frame when we expected an
5000 ack. Do our best to discard the rest of this packet. */
5001
5002static void
5003skip_frame (void)
5004{
5005 int c;
5006
5007 while (1)
5008 {
5009 c = readchar (remote_timeout);
5010 switch (c)
5011 {
5012 case SERIAL_TIMEOUT:
5013 /* Nothing we can do. */
5014 return;
5015 case '#':
5016 /* Discard the two bytes of checksum and stop. */
5017 c = readchar (remote_timeout);
5018 if (c >= 0)
5019 c = readchar (remote_timeout);
5020
5021 return;
5022 case '*': /* Run length encoding. */
5023 /* Discard the repeat count. */
5024 c = readchar (remote_timeout);
5025 if (c < 0)
5026 return;
5027 break;
5028 default:
5029 /* A regular character. */
5030 break;
5031 }
5032 }
5033}
5034
c906108c 5035/* Come here after finding the start of the frame. Collect the rest
6d820c5c
DJ
5036 into *BUF, verifying the checksum, length, and handling run-length
5037 compression. NUL terminate the buffer. If there is not enough room,
5038 expand *BUF using xrealloc.
c906108c 5039
c2d11a7d
JM
5040 Returns -1 on error, number of characters in buffer (ignoring the
5041 trailing NULL) on success. (could be extended to return one of the
23860348 5042 SERIAL status indications). */
c2d11a7d
JM
5043
5044static long
6d820c5c
DJ
5045read_frame (char **buf_p,
5046 long *sizeof_buf)
c906108c
SS
5047{
5048 unsigned char csum;
c2d11a7d 5049 long bc;
c906108c 5050 int c;
6d820c5c 5051 char *buf = *buf_p;
c906108c
SS
5052
5053 csum = 0;
c2d11a7d 5054 bc = 0;
c906108c
SS
5055
5056 while (1)
5057 {
5058 c = readchar (remote_timeout);
c906108c
SS
5059 switch (c)
5060 {
5061 case SERIAL_TIMEOUT:
5062 if (remote_debug)
0f71a2f6 5063 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
c2d11a7d 5064 return -1;
c906108c
SS
5065 case '$':
5066 if (remote_debug)
0f71a2f6
JM
5067 fputs_filtered ("Saw new packet start in middle of old one\n",
5068 gdb_stdlog);
23860348 5069 return -1; /* Start a new packet, count retries. */
c906108c
SS
5070 case '#':
5071 {
5072 unsigned char pktcsum;
e1b09194
AC
5073 int check_0 = 0;
5074 int check_1 = 0;
c906108c 5075
c2d11a7d 5076 buf[bc] = '\0';
c906108c 5077
e1b09194
AC
5078 check_0 = readchar (remote_timeout);
5079 if (check_0 >= 0)
5080 check_1 = readchar (remote_timeout);
802188a7 5081
e1b09194
AC
5082 if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
5083 {
5084 if (remote_debug)
2bc416ba 5085 fputs_filtered ("Timeout in checksum, retrying\n",
23860348 5086 gdb_stdlog);
e1b09194
AC
5087 return -1;
5088 }
5089 else if (check_0 < 0 || check_1 < 0)
40e3f985
FN
5090 {
5091 if (remote_debug)
2bc416ba 5092 fputs_filtered ("Communication error in checksum\n",
23860348 5093 gdb_stdlog);
40e3f985
FN
5094 return -1;
5095 }
c906108c 5096
e1b09194 5097 pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
c906108c 5098 if (csum == pktcsum)
c2d11a7d 5099 return bc;
c906108c 5100
c5aa993b 5101 if (remote_debug)
c906108c 5102 {
0f71a2f6 5103 fprintf_filtered (gdb_stdlog,
c5aa993b 5104 "Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
0f71a2f6 5105 pktcsum, csum);
0876f84a 5106 fputstrn_filtered (buf, bc, 0, gdb_stdlog);
0f71a2f6 5107 fputs_filtered ("\n", gdb_stdlog);
c906108c 5108 }
c2d11a7d 5109 /* Number of characters in buffer ignoring trailing
23860348 5110 NULL. */
c2d11a7d 5111 return -1;
c906108c 5112 }
23860348 5113 case '*': /* Run length encoding. */
c2c6d25f
JM
5114 {
5115 int repeat;
5116 csum += c;
c906108c 5117
b4501125
AC
5118 c = readchar (remote_timeout);
5119 csum += c;
23860348 5120 repeat = c - ' ' + 3; /* Compute repeat count. */
c906108c 5121
23860348 5122 /* The character before ``*'' is repeated. */
c2d11a7d 5123
6d820c5c 5124 if (repeat > 0 && repeat <= 255 && bc > 0)
c2c6d25f 5125 {
6d820c5c
DJ
5126 if (bc + repeat - 1 >= *sizeof_buf - 1)
5127 {
5128 /* Make some more room in the buffer. */
5129 *sizeof_buf += repeat;
5130 *buf_p = xrealloc (*buf_p, *sizeof_buf);
5131 buf = *buf_p;
5132 }
5133
c2d11a7d
JM
5134 memset (&buf[bc], buf[bc - 1], repeat);
5135 bc += repeat;
c2c6d25f
JM
5136 continue;
5137 }
5138
c2d11a7d 5139 buf[bc] = '\0';
6d820c5c 5140 printf_filtered (_("Invalid run length encoding: %s\n"), buf);
c2d11a7d 5141 return -1;
c2c6d25f 5142 }
c906108c 5143 default:
6d820c5c 5144 if (bc >= *sizeof_buf - 1)
c906108c 5145 {
6d820c5c
DJ
5146 /* Make some more room in the buffer. */
5147 *sizeof_buf *= 2;
5148 *buf_p = xrealloc (*buf_p, *sizeof_buf);
5149 buf = *buf_p;
c906108c
SS
5150 }
5151
6d820c5c
DJ
5152 buf[bc++] = c;
5153 csum += c;
5154 continue;
c906108c
SS
5155 }
5156 }
5157}
5158
5159/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
5160 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
5161 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
5162 rather than timing out; this is used (in synchronous mode) to wait
5163 for a target that is is executing user code to stop. */
d9fcf2fb
JM
5164/* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
5165 don't have to change all the calls to getpkt to deal with the
5166 return value, because at the moment I don't know what the right
23860348 5167 thing to do it for those. */
c906108c 5168void
6d820c5c
DJ
5169getpkt (char **buf,
5170 long *sizeof_buf,
c2d11a7d 5171 int forever)
d9fcf2fb
JM
5172{
5173 int timed_out;
5174
5175 timed_out = getpkt_sane (buf, sizeof_buf, forever);
5176}
5177
5178
5179/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
5180 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
5181 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
5182 rather than timing out; this is used (in synchronous mode) to wait
5183 for a target that is is executing user code to stop. If FOREVER ==
5184 0, this function is allowed to time out gracefully and return an
0876f84a
DJ
5185 indication of this to the caller. Otherwise return the number
5186 of bytes read. */
3172dc30 5187static int
6d820c5c 5188getpkt_sane (char **buf, long *sizeof_buf, int forever)
c906108c 5189{
2d717e4f 5190 struct remote_state *rs = get_remote_state ();
c906108c
SS
5191 int c;
5192 int tries;
5193 int timeout;
5194 int val;
5195
2d717e4f
DJ
5196 /* We're reading a new response. Make sure we don't look at a
5197 previously cached response. */
5198 rs->cached_wait_status = 0;
5199
6d820c5c 5200 strcpy (*buf, "timeout");
c906108c
SS
5201
5202 if (forever)
5203 {
c906108c 5204 timeout = watchdog > 0 ? watchdog : -1;
c906108c
SS
5205 }
5206
5207 else
5208 timeout = remote_timeout;
5209
5210#define MAX_TRIES 3
5211
5212 for (tries = 1; tries <= MAX_TRIES; tries++)
5213 {
5214 /* This can loop forever if the remote side sends us characters
23860348
MS
5215 continuously, but if it pauses, we'll get a zero from
5216 readchar because of timeout. Then we'll count that as a
5217 retry. */
c906108c 5218
23860348
MS
5219 /* Note that we will only wait forever prior to the start of a
5220 packet. After that, we expect characters to arrive at a
5221 brisk pace. They should show up within remote_timeout
5222 intervals. */
c906108c
SS
5223
5224 do
5225 {
5226 c = readchar (timeout);
5227
5228 if (c == SERIAL_TIMEOUT)
5229 {
23860348 5230 if (forever) /* Watchdog went off? Kill the target. */
c906108c 5231 {
2acceee2 5232 QUIT;
c906108c 5233 target_mourn_inferior ();
489eaeba 5234 error (_("Watchdog timeout has expired. Target detached."));
c906108c 5235 }
c906108c 5236 if (remote_debug)
0f71a2f6 5237 fputs_filtered ("Timed out.\n", gdb_stdlog);
c906108c
SS
5238 goto retry;
5239 }
5240 }
5241 while (c != '$');
5242
5243 /* We've found the start of a packet, now collect the data. */
5244
c2d11a7d 5245 val = read_frame (buf, sizeof_buf);
c906108c 5246
c2d11a7d 5247 if (val >= 0)
c906108c
SS
5248 {
5249 if (remote_debug)
43e526b9
JM
5250 {
5251 fprintf_unfiltered (gdb_stdlog, "Packet received: ");
0876f84a 5252 fputstrn_unfiltered (*buf, val, 0, gdb_stdlog);
43e526b9
JM
5253 fprintf_unfiltered (gdb_stdlog, "\n");
5254 }
2cd58942 5255 serial_write (remote_desc, "+", 1);
0876f84a 5256 return val;
c906108c
SS
5257 }
5258
5259 /* Try the whole thing again. */
5260 retry:
2cd58942 5261 serial_write (remote_desc, "-", 1);
c906108c
SS
5262 }
5263
2bc416ba 5264 /* We have tried hard enough, and just can't receive the packet.
23860348 5265 Give up. */
c906108c 5266
a3f17187 5267 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
2cd58942 5268 serial_write (remote_desc, "+", 1);
0876f84a 5269 return -1;
c906108c
SS
5270}
5271\f
5272static void
fba45db2 5273remote_kill (void)
c906108c
SS
5274{
5275 /* For some mysterious reason, wait_for_inferior calls kill instead of
5276 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
5277 if (kill_kludge)
5278 {
5279 kill_kludge = 0;
5280 target_mourn_inferior ();
5281 return;
5282 }
5283
5284 /* Use catch_errors so the user can quit from gdb even when we aren't on
5285 speaking terms with the remote system. */
c5aa993b 5286 catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR);
c906108c
SS
5287
5288 /* Don't wait for it to die. I'm not really sure it matters whether
5289 we do or not. For the existing stubs, kill is a noop. */
5290 target_mourn_inferior ();
5291}
5292
23860348 5293/* Async version of remote_kill. */
43ff13b4 5294static void
fba45db2 5295remote_async_kill (void)
43ff13b4 5296{
23860348 5297 /* Unregister the file descriptor from the event loop. */
ed9a39eb 5298 if (target_is_async_p ())
2cd58942 5299 serial_async (remote_desc, NULL, 0);
43ff13b4
JM
5300
5301 /* For some mysterious reason, wait_for_inferior calls kill instead of
5302 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
5303 if (kill_kludge)
5304 {
5305 kill_kludge = 0;
5306 target_mourn_inferior ();
5307 return;
5308 }
5309
23860348
MS
5310 /* Use catch_errors so the user can quit from gdb even when we
5311 aren't on speaking terms with the remote system. */
c5aa993b 5312 catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR);
43ff13b4
JM
5313
5314 /* Don't wait for it to die. I'm not really sure it matters whether
5315 we do or not. For the existing stubs, kill is a noop. */
5316 target_mourn_inferior ();
5317}
5318
c906108c 5319static void
fba45db2 5320remote_mourn (void)
c906108c
SS
5321{
5322 remote_mourn_1 (&remote_ops);
5323}
5324
53a5351d 5325static void
fba45db2 5326remote_async_mourn (void)
53a5351d
JM
5327{
5328 remote_mourn_1 (&remote_async_ops);
5329}
5330
c906108c
SS
5331/* Worker function for remote_mourn. */
5332static void
fba45db2 5333remote_mourn_1 (struct target_ops *target)
c906108c
SS
5334{
5335 unpush_target (target);
5336 generic_mourn_inferior ();
5337}
5338
2d717e4f
DJ
5339static void
5340extended_remote_mourn_1 (struct target_ops *target)
5341{
5342 struct remote_state *rs = get_remote_state ();
c906108c 5343
2d717e4f
DJ
5344 /* Unlike "target remote", we do not want to unpush the target; then
5345 the next time the user says "run", we won't be connected. */
5346
5347 /* Call common code to mark the inferior as not running. */
5348 generic_mourn_inferior ();
5349
5350 /* Check whether the target is running now - some remote stubs
5351 automatically restart after kill. */
5352 putpkt ("?");
5353 getpkt (&rs->buf, &rs->buf_size, 0);
5354
5355 if (rs->buf[0] == 'S' || rs->buf[0] == 'T')
5356 {
5357 /* Assume that the target has been restarted. Set inferior_ptid
5358 so that bits of core GDB realizes there's something here, e.g.,
5359 so that the user can say "kill" again. */
5360 inferior_ptid = pid_to_ptid (MAGIC_NULL_PID);
5361 }
5362 else
5363 {
5364 /* Mark this (still pushed) target as not executable until we
5365 restart it. */
5366 target_mark_exited (target);
5367 }
5368}
c906108c
SS
5369
5370static void
2d717e4f 5371extended_remote_mourn (void)
c906108c 5372{
2d717e4f
DJ
5373 extended_remote_mourn_1 (&extended_remote_ops);
5374}
c906108c 5375
2d717e4f
DJ
5376static void
5377extended_async_remote_mourn (void)
5378{
5379 extended_remote_mourn_1 (&extended_async_remote_ops);
5380}
5381
5382static int
5383extended_remote_run (char *args)
5384{
5385 struct remote_state *rs = get_remote_state ();
5386 char *p;
5387 int len;
c906108c 5388
2d717e4f
DJ
5389 /* If the user has disabled vRun support, or we have detected that
5390 support is not available, do not try it. */
5391 if (remote_protocol_packets[PACKET_vRun].support == PACKET_DISABLE)
5392 return -1;
424163ea 5393
2d717e4f
DJ
5394 strcpy (rs->buf, "vRun;");
5395 len = strlen (rs->buf);
c906108c 5396
2d717e4f
DJ
5397 if (strlen (remote_exec_file) * 2 + len >= get_remote_packet_size ())
5398 error (_("Remote file name too long for run packet"));
5399 len += 2 * bin2hex ((gdb_byte *) remote_exec_file, rs->buf + len, 0);
5400
5401 if (*args)
5402 {
5403 struct cleanup *back_to;
5404 int i;
5405 char **argv;
5406
5407 argv = buildargv (args);
5408 back_to = make_cleanup ((void (*) (void *)) freeargv, argv);
5409 for (i = 0; argv[i] != NULL; i++)
5410 {
5411 if (strlen (argv[i]) * 2 + 1 + len >= get_remote_packet_size ())
5412 error (_("Argument list too long for run packet"));
5413 rs->buf[len++] = ';';
5414 len += 2 * bin2hex ((gdb_byte *) argv[i], rs->buf + len, 0);
5415 }
5416 do_cleanups (back_to);
5417 }
5418
5419 rs->buf[len++] = '\0';
5420
5421 putpkt (rs->buf);
5422 getpkt (&rs->buf, &rs->buf_size, 0);
5423
5424 if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vRun]) == PACKET_OK)
5425 {
5426 /* We have a wait response; we don't need it, though. All is well. */
5427 return 0;
5428 }
5429 else if (remote_protocol_packets[PACKET_vRun].support == PACKET_DISABLE)
5430 /* It wasn't disabled before, but it is now. */
5431 return -1;
5432 else
5433 {
5434 if (remote_exec_file[0] == '\0')
5435 error (_("Running the default executable on the remote target failed; "
5436 "try \"set remote exec-file\"?"));
5437 else
5438 error (_("Running \"%s\" on the remote target failed"),
5439 remote_exec_file);
5440 }
c906108c
SS
5441}
5442
2d717e4f
DJ
5443/* In the extended protocol we want to be able to do things like
5444 "run" and have them basically work as expected. So we need
5445 a special create_inferior function. We support changing the
5446 executable file and the command line arguments, but not the
5447 environment. */
5448
43ff13b4 5449static void
2d717e4f
DJ
5450extended_remote_create_inferior_1 (char *exec_file, char *args,
5451 char **env, int from_tty,
5452 int async_p)
43ff13b4 5453{
43ff13b4 5454 /* If running asynchronously, register the target file descriptor
23860348 5455 with the event loop. */
2d717e4f 5456 if (async_p && target_can_async_p ())
2acceee2 5457 target_async (inferior_event_handler, 0);
43ff13b4
JM
5458
5459 /* Now restart the remote server. */
2d717e4f
DJ
5460 if (extended_remote_run (args) == -1)
5461 {
5462 /* vRun was not supported. Fail if we need it to do what the
5463 user requested. */
5464 if (remote_exec_file[0])
5465 error (_("Remote target does not support \"set remote exec-file\""));
5466 if (args[0])
5467 error (_("Remote target does not support \"set args\" or run <ARGS>"));
43ff13b4 5468
2d717e4f
DJ
5469 /* Fall back to "R". */
5470 extended_remote_restart ();
5471 }
424163ea 5472
45280a52
DJ
5473 /* Clean up from the last time we ran, before we mark the target
5474 running again. This will mark breakpoints uninserted, and
5475 get_offsets may insert breakpoints. */
5476 init_thread_list ();
5477 init_wait_for_inferior ();
5478
2d717e4f 5479 /* Now mark the inferior as running before we do anything else. */
df7df359 5480 attach_flag = 0;
2d717e4f
DJ
5481 inferior_ptid = pid_to_ptid (MAGIC_NULL_PID);
5482 if (async_p)
5483 target_mark_running (&extended_async_remote_ops);
5484 else
5485 target_mark_running (&extended_remote_ops);
5486
5487 /* Get updated offsets, if the stub uses qOffsets. */
5488 get_offsets ();
2d717e4f
DJ
5489}
5490
5491static void
5492extended_remote_create_inferior (char *exec_file, char *args,
5493 char **env, int from_tty)
5494{
5495 extended_remote_create_inferior_1 (exec_file, args, env, from_tty, 0);
5496}
5497
5498static void
5499extended_remote_async_create_inferior (char *exec_file, char *args,
5500 char **env, int from_tty)
5501{
5502 extended_remote_create_inferior_1 (exec_file, args, env, from_tty, 1);
43ff13b4 5503}
c906108c 5504\f
c5aa993b 5505
8181d85f
DJ
5506/* Insert a breakpoint. On targets that have software breakpoint
5507 support, we ask the remote target to do the work; on targets
5508 which don't, we insert a traditional memory breakpoint. */
c906108c
SS
5509
5510static int
8181d85f 5511remote_insert_breakpoint (struct bp_target_info *bp_tgt)
c906108c 5512{
d471ea57
AC
5513 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
5514 If it succeeds, then set the support to PACKET_ENABLE. If it
5515 fails, and the user has explicitly requested the Z support then
23860348 5516 report an error, otherwise, mark it disabled and go on. */
802188a7 5517
444abaca 5518 if (remote_protocol_packets[PACKET_Z0].support != PACKET_DISABLE)
96baa820 5519 {
4fff2411
JZ
5520 CORE_ADDR addr;
5521 struct remote_state *rs;
5522 char *p;
5523
5524 gdbarch_breakpoint_from_pc
5525 (current_gdbarch, &bp_tgt->placed_address, &bp_tgt->placed_size);
5526
5527 rs = get_remote_state ();
5528 p = rs->buf;
802188a7 5529
96baa820
JM
5530 *(p++) = 'Z';
5531 *(p++) = '0';
5532 *(p++) = ',';
8181d85f
DJ
5533 addr = (ULONGEST) remote_address_masked (bp_tgt->placed_address);
5534 p += hexnumstr (p, addr);
5535 sprintf (p, ",%d", bp_tgt->placed_size);
802188a7 5536
6d820c5c
DJ
5537 putpkt (rs->buf);
5538 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 5539
6d820c5c 5540 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0]))
96baa820 5541 {
d471ea57
AC
5542 case PACKET_ERROR:
5543 return -1;
5544 case PACKET_OK:
5545 return 0;
5546 case PACKET_UNKNOWN:
5547 break;
96baa820
JM
5548 }
5549 }
c906108c 5550
8181d85f 5551 return memory_insert_breakpoint (bp_tgt);
c906108c
SS
5552}
5553
5554static int
8181d85f 5555remote_remove_breakpoint (struct bp_target_info *bp_tgt)
c906108c 5556{
8181d85f 5557 CORE_ADDR addr = bp_tgt->placed_address;
d01949b6 5558 struct remote_state *rs = get_remote_state ();
96baa820
JM
5559 int bp_size;
5560
444abaca 5561 if (remote_protocol_packets[PACKET_Z0].support != PACKET_DISABLE)
96baa820 5562 {
6d820c5c 5563 char *p = rs->buf;
802188a7 5564
96baa820
JM
5565 *(p++) = 'z';
5566 *(p++) = '0';
5567 *(p++) = ',';
5568
8181d85f
DJ
5569 addr = (ULONGEST) remote_address_masked (bp_tgt->placed_address);
5570 p += hexnumstr (p, addr);
5571 sprintf (p, ",%d", bp_tgt->placed_size);
802188a7 5572
6d820c5c
DJ
5573 putpkt (rs->buf);
5574 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 5575
6d820c5c 5576 return (rs->buf[0] == 'E');
96baa820
JM
5577 }
5578
8181d85f 5579 return memory_remove_breakpoint (bp_tgt);
c906108c
SS
5580}
5581
d471ea57
AC
5582static int
5583watchpoint_to_Z_packet (int type)
5584{
5585 switch (type)
5586 {
5587 case hw_write:
bb858e6a 5588 return Z_PACKET_WRITE_WP;
d471ea57
AC
5589 break;
5590 case hw_read:
bb858e6a 5591 return Z_PACKET_READ_WP;
d471ea57
AC
5592 break;
5593 case hw_access:
bb858e6a 5594 return Z_PACKET_ACCESS_WP;
d471ea57
AC
5595 break;
5596 default:
8e65ff28 5597 internal_error (__FILE__, __LINE__,
e2e0b3e5 5598 _("hw_bp_to_z: bad watchpoint type %d"), type);
d471ea57
AC
5599 }
5600}
5601
3c3bea1c 5602static int
fba45db2 5603remote_insert_watchpoint (CORE_ADDR addr, int len, int type)
96baa820 5604{
d01949b6 5605 struct remote_state *rs = get_remote_state ();
e514a9d6 5606 char *p;
d471ea57 5607 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
96baa820 5608
444abaca 5609 if (remote_protocol_packets[PACKET_Z0 + packet].support == PACKET_DISABLE)
5cffb350 5610 return -1;
802188a7 5611
6d820c5c
DJ
5612 sprintf (rs->buf, "Z%x,", packet);
5613 p = strchr (rs->buf, '\0');
96baa820
JM
5614 addr = remote_address_masked (addr);
5615 p += hexnumstr (p, (ULONGEST) addr);
d4f3574e 5616 sprintf (p, ",%x", len);
802188a7 5617
6d820c5c
DJ
5618 putpkt (rs->buf);
5619 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 5620
6d820c5c 5621 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
5622 {
5623 case PACKET_ERROR:
5624 case PACKET_UNKNOWN:
5625 return -1;
5626 case PACKET_OK:
5627 return 0;
5628 }
8e65ff28 5629 internal_error (__FILE__, __LINE__,
e2e0b3e5 5630 _("remote_insert_watchpoint: reached end of function"));
96baa820
JM
5631}
5632
d471ea57 5633
3c3bea1c 5634static int
fba45db2 5635remote_remove_watchpoint (CORE_ADDR addr, int len, int type)
96baa820 5636{
d01949b6 5637 struct remote_state *rs = get_remote_state ();
e514a9d6 5638 char *p;
d471ea57
AC
5639 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
5640
444abaca 5641 if (remote_protocol_packets[PACKET_Z0 + packet].support == PACKET_DISABLE)
5cffb350 5642 return -1;
802188a7 5643
6d820c5c
DJ
5644 sprintf (rs->buf, "z%x,", packet);
5645 p = strchr (rs->buf, '\0');
96baa820
JM
5646 addr = remote_address_masked (addr);
5647 p += hexnumstr (p, (ULONGEST) addr);
d4f3574e 5648 sprintf (p, ",%x", len);
6d820c5c
DJ
5649 putpkt (rs->buf);
5650 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 5651
6d820c5c 5652 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
5653 {
5654 case PACKET_ERROR:
5655 case PACKET_UNKNOWN:
5656 return -1;
5657 case PACKET_OK:
5658 return 0;
5659 }
8e65ff28 5660 internal_error (__FILE__, __LINE__,
e2e0b3e5 5661 _("remote_remove_watchpoint: reached end of function"));
96baa820
JM
5662}
5663
3c3bea1c 5664
501eef12
AC
5665int remote_hw_watchpoint_limit = -1;
5666int remote_hw_breakpoint_limit = -1;
d471ea57 5667
b9362cc7 5668static int
3c3bea1c 5669remote_check_watch_resources (int type, int cnt, int ot)
96baa820 5670{
3c3bea1c
GS
5671 if (type == bp_hardware_breakpoint)
5672 {
5673 if (remote_hw_breakpoint_limit == 0)
5674 return 0;
501eef12
AC
5675 else if (remote_hw_breakpoint_limit < 0)
5676 return 1;
3c3bea1c
GS
5677 else if (cnt <= remote_hw_breakpoint_limit)
5678 return 1;
5679 }
5680 else
5681 {
5682 if (remote_hw_watchpoint_limit == 0)
5683 return 0;
501eef12
AC
5684 else if (remote_hw_watchpoint_limit < 0)
5685 return 1;
3c3bea1c
GS
5686 else if (ot)
5687 return -1;
5688 else if (cnt <= remote_hw_watchpoint_limit)
5689 return 1;
5690 }
5691 return -1;
5692}
5693
b9362cc7 5694static int
3c3bea1c
GS
5695remote_stopped_by_watchpoint (void)
5696{
5697 return remote_stopped_by_watchpoint_p;
5698}
5699
4aa7a7f5
JJ
5700static int
5701remote_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
3c3bea1c 5702{
4aa7a7f5 5703 int rc = 0;
d983da9c 5704 if (remote_stopped_by_watchpoint ())
4aa7a7f5
JJ
5705 {
5706 *addr_p = remote_watch_data_address;
5707 rc = 1;
5708 }
5709
5710 return rc;
3c3bea1c
GS
5711}
5712
5713
5714static int
8181d85f 5715remote_insert_hw_breakpoint (struct bp_target_info *bp_tgt)
3c3bea1c 5716{
8181d85f 5717 CORE_ADDR addr;
4fff2411
JZ
5718 struct remote_state *rs;
5719 char *p;
802188a7 5720
c8189ed1 5721 /* The length field should be set to the size of a breakpoint
8181d85f 5722 instruction, even though we aren't inserting one ourselves. */
c8189ed1 5723
3b3b875c
UW
5724 gdbarch_breakpoint_from_pc
5725 (current_gdbarch, &bp_tgt->placed_address, &bp_tgt->placed_size);
3c3bea1c 5726
444abaca 5727 if (remote_protocol_packets[PACKET_Z1].support == PACKET_DISABLE)
5cffb350 5728 return -1;
2bc416ba 5729
4fff2411
JZ
5730 rs = get_remote_state ();
5731 p = rs->buf;
5732
96baa820
JM
5733 *(p++) = 'Z';
5734 *(p++) = '1';
5735 *(p++) = ',';
802188a7 5736
8181d85f 5737 addr = remote_address_masked (bp_tgt->placed_address);
96baa820 5738 p += hexnumstr (p, (ULONGEST) addr);
8181d85f 5739 sprintf (p, ",%x", bp_tgt->placed_size);
96baa820 5740
6d820c5c
DJ
5741 putpkt (rs->buf);
5742 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 5743
6d820c5c 5744 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
5745 {
5746 case PACKET_ERROR:
5747 case PACKET_UNKNOWN:
5748 return -1;
5749 case PACKET_OK:
5750 return 0;
5751 }
8e65ff28 5752 internal_error (__FILE__, __LINE__,
e2e0b3e5 5753 _("remote_insert_hw_breakpoint: reached end of function"));
96baa820
JM
5754}
5755
d471ea57 5756
802188a7 5757static int
8181d85f 5758remote_remove_hw_breakpoint (struct bp_target_info *bp_tgt)
96baa820 5759{
8181d85f 5760 CORE_ADDR addr;
d01949b6 5761 struct remote_state *rs = get_remote_state ();
6d820c5c 5762 char *p = rs->buf;
c8189ed1 5763
444abaca 5764 if (remote_protocol_packets[PACKET_Z1].support == PACKET_DISABLE)
5cffb350 5765 return -1;
802188a7 5766
96baa820
JM
5767 *(p++) = 'z';
5768 *(p++) = '1';
5769 *(p++) = ',';
802188a7 5770
8181d85f 5771 addr = remote_address_masked (bp_tgt->placed_address);
96baa820 5772 p += hexnumstr (p, (ULONGEST) addr);
8181d85f 5773 sprintf (p, ",%x", bp_tgt->placed_size);
96baa820 5774
6d820c5c
DJ
5775 putpkt (rs->buf);
5776 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 5777
6d820c5c 5778 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
5779 {
5780 case PACKET_ERROR:
5781 case PACKET_UNKNOWN:
5782 return -1;
5783 case PACKET_OK:
5784 return 0;
5785 }
8e65ff28 5786 internal_error (__FILE__, __LINE__,
e2e0b3e5 5787 _("remote_remove_hw_breakpoint: reached end of function"));
96baa820 5788}
96baa820 5789
c906108c
SS
5790/* Some targets are only capable of doing downloads, and afterwards
5791 they switch to the remote serial protocol. This function provides
5792 a clean way to get from the download target to the remote target.
5793 It's basically just a wrapper so that we don't have to expose any
5794 of the internal workings of remote.c.
5795
5796 Prior to calling this routine, you should shutdown the current
5797 target code, else you will get the "A program is being debugged
5798 already..." message. Usually a call to pop_target() suffices. */
5799
5800void
fba45db2 5801push_remote_target (char *name, int from_tty)
c906108c 5802{
a3f17187 5803 printf_filtered (_("Switching to remote protocol\n"));
c906108c
SS
5804 remote_open (name, from_tty);
5805}
5806
23860348 5807/* Table used by the crc32 function to calcuate the checksum. */
c906108c 5808
c5aa993b
JM
5809static unsigned long crc32_table[256] =
5810{0, 0};
c906108c
SS
5811
5812static unsigned long
fba45db2 5813crc32 (unsigned char *buf, int len, unsigned int crc)
c906108c 5814{
c5aa993b 5815 if (!crc32_table[1])
c906108c 5816 {
23860348 5817 /* Initialize the CRC table and the decoding table. */
c906108c
SS
5818 int i, j;
5819 unsigned int c;
5820
5821 for (i = 0; i < 256; i++)
c5aa993b
JM
5822 {
5823 for (c = i << 24, j = 8; j > 0; --j)
5824 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
5825 crc32_table[i] = c;
5826 }
c906108c
SS
5827 }
5828
5829 while (len--)
5830 {
5831 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buf) & 255];
5832 buf++;
5833 }
5834 return crc;
5835}
5836
5837/* compare-sections command
5838
5839 With no arguments, compares each loadable section in the exec bfd
5840 with the same memory range on the target, and reports mismatches.
5841 Useful for verifying the image on the target against the exec file.
5842 Depends on the target understanding the new "qCRC:" request. */
5843
e514a9d6
JM
5844/* FIXME: cagney/1999-10-26: This command should be broken down into a
5845 target method (target verify memory) and generic version of the
5846 actual command. This will allow other high-level code (especially
23860348 5847 generic_load()) to make use of this target functionality. */
e514a9d6 5848
c906108c 5849static void
fba45db2 5850compare_sections_command (char *args, int from_tty)
c906108c 5851{
d01949b6 5852 struct remote_state *rs = get_remote_state ();
c906108c
SS
5853 asection *s;
5854 unsigned long host_crc, target_crc;
5855 extern bfd *exec_bfd;
5856 struct cleanup *old_chain;
085dd6e6
JM
5857 char *tmp;
5858 char *sectdata;
ce359b09 5859 const char *sectname;
c906108c
SS
5860 bfd_size_type size;
5861 bfd_vma lma;
5862 int matched = 0;
5863 int mismatched = 0;
5864
5865 if (!exec_bfd)
8a3fe4f8 5866 error (_("command cannot be used without an exec file"));
c906108c
SS
5867 if (!current_target.to_shortname ||
5868 strcmp (current_target.to_shortname, "remote") != 0)
8a3fe4f8 5869 error (_("command can only be used with remote target"));
c906108c 5870
c5aa993b 5871 for (s = exec_bfd->sections; s; s = s->next)
c906108c
SS
5872 {
5873 if (!(s->flags & SEC_LOAD))
c5aa993b 5874 continue; /* skip non-loadable section */
c906108c 5875
2c500098 5876 size = bfd_get_section_size (s);
c906108c 5877 if (size == 0)
c5aa993b 5878 continue; /* skip zero-length section */
c906108c 5879
ce359b09 5880 sectname = bfd_get_section_name (exec_bfd, s);
c906108c 5881 if (args && strcmp (args, sectname) != 0)
c5aa993b 5882 continue; /* not the section selected by user */
c906108c 5883
c5aa993b 5884 matched = 1; /* do this section */
c906108c 5885 lma = s->lma;
23860348 5886 /* FIXME: assumes lma can fit into long. */
ea9c271d 5887 xsnprintf (rs->buf, get_remote_packet_size (), "qCRC:%lx,%lx",
ecbc58df 5888 (long) lma, (long) size);
6d820c5c 5889 putpkt (rs->buf);
c906108c 5890
23860348
MS
5891 /* Be clever; compute the host_crc before waiting for target
5892 reply. */
c906108c 5893 sectdata = xmalloc (size);
b8c9b27d 5894 old_chain = make_cleanup (xfree, sectdata);
c906108c
SS
5895 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
5896 host_crc = crc32 ((unsigned char *) sectdata, size, 0xffffffff);
5897
6d820c5c
DJ
5898 getpkt (&rs->buf, &rs->buf_size, 0);
5899 if (rs->buf[0] == 'E')
8a3fe4f8 5900 error (_("target memory fault, section %s, range 0x%s -- 0x%s"),
823ca731 5901 sectname, paddr (lma), paddr (lma + size));
6d820c5c 5902 if (rs->buf[0] != 'C')
8a3fe4f8 5903 error (_("remote target does not support this operation"));
c906108c 5904
6d820c5c 5905 for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++)
c906108c
SS
5906 target_crc = target_crc * 16 + fromhex (*tmp);
5907
d4f3574e
SS
5908 printf_filtered ("Section %s, range 0x%s -- 0x%s: ",
5909 sectname, paddr (lma), paddr (lma + size));
c906108c
SS
5910 if (host_crc == target_crc)
5911 printf_filtered ("matched.\n");
5912 else
c5aa993b
JM
5913 {
5914 printf_filtered ("MIS-MATCHED!\n");
5915 mismatched++;
5916 }
c906108c
SS
5917
5918 do_cleanups (old_chain);
5919 }
5920 if (mismatched > 0)
8a3fe4f8
AC
5921 warning (_("One or more sections of the remote executable does not match\n\
5922the loaded file\n"));
c906108c 5923 if (args && !matched)
a3f17187 5924 printf_filtered (_("No loaded section named '%s'.\n"), args);
c906108c
SS
5925}
5926
0e7f50da
UW
5927/* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
5928 into remote target. The number of bytes written to the remote
5929 target is returned, or -1 for error. */
5930
5931static LONGEST
5932remote_write_qxfer (struct target_ops *ops, const char *object_name,
5933 const char *annex, const gdb_byte *writebuf,
5934 ULONGEST offset, LONGEST len,
5935 struct packet_config *packet)
5936{
5937 int i, buf_len;
5938 ULONGEST n;
5939 gdb_byte *wbuf;
5940 struct remote_state *rs = get_remote_state ();
5941 int max_size = get_memory_write_packet_size ();
5942
5943 if (packet->support == PACKET_DISABLE)
5944 return -1;
5945
5946 /* Insert header. */
5947 i = snprintf (rs->buf, max_size,
5948 "qXfer:%s:write:%s:%s:",
5949 object_name, annex ? annex : "",
5950 phex_nz (offset, sizeof offset));
5951 max_size -= (i + 1);
5952
5953 /* Escape as much data as fits into rs->buf. */
5954 buf_len = remote_escape_output
5955 (writebuf, len, (rs->buf + i), &max_size, max_size);
5956
5957 if (putpkt_binary (rs->buf, i + buf_len) < 0
5958 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
5959 || packet_ok (rs->buf, packet) != PACKET_OK)
5960 return -1;
5961
5962 unpack_varlen_hex (rs->buf, &n);
5963 return n;
5964}
5965
0876f84a
DJ
5966/* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
5967 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
5968 number of bytes read is returned, or 0 for EOF, or -1 for error.
5969 The number of bytes read may be less than LEN without indicating an
5970 EOF. PACKET is checked and updated to indicate whether the remote
5971 target supports this object. */
5972
5973static LONGEST
5974remote_read_qxfer (struct target_ops *ops, const char *object_name,
5975 const char *annex,
5976 gdb_byte *readbuf, ULONGEST offset, LONGEST len,
5977 struct packet_config *packet)
5978{
5979 static char *finished_object;
5980 static char *finished_annex;
5981 static ULONGEST finished_offset;
5982
5983 struct remote_state *rs = get_remote_state ();
5984 unsigned int total = 0;
5985 LONGEST i, n, packet_len;
5986
5987 if (packet->support == PACKET_DISABLE)
5988 return -1;
5989
5990 /* Check whether we've cached an end-of-object packet that matches
5991 this request. */
5992 if (finished_object)
5993 {
5994 if (strcmp (object_name, finished_object) == 0
5995 && strcmp (annex ? annex : "", finished_annex) == 0
5996 && offset == finished_offset)
5997 return 0;
5998
5999 /* Otherwise, we're now reading something different. Discard
6000 the cache. */
6001 xfree (finished_object);
6002 xfree (finished_annex);
6003 finished_object = NULL;
6004 finished_annex = NULL;
6005 }
6006
6007 /* Request only enough to fit in a single packet. The actual data
6008 may not, since we don't know how much of it will need to be escaped;
6009 the target is free to respond with slightly less data. We subtract
6010 five to account for the response type and the protocol frame. */
6011 n = min (get_remote_packet_size () - 5, len);
6012 snprintf (rs->buf, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
6013 object_name, annex ? annex : "",
6014 phex_nz (offset, sizeof offset),
6015 phex_nz (n, sizeof n));
6016 i = putpkt (rs->buf);
6017 if (i < 0)
6018 return -1;
6019
6020 rs->buf[0] = '\0';
6021 packet_len = getpkt_sane (&rs->buf, &rs->buf_size, 0);
6022 if (packet_len < 0 || packet_ok (rs->buf, packet) != PACKET_OK)
6023 return -1;
6024
6025 if (rs->buf[0] != 'l' && rs->buf[0] != 'm')
6026 error (_("Unknown remote qXfer reply: %s"), rs->buf);
6027
6028 /* 'm' means there is (or at least might be) more data after this
6029 batch. That does not make sense unless there's at least one byte
6030 of data in this reply. */
6031 if (rs->buf[0] == 'm' && packet_len == 1)
6032 error (_("Remote qXfer reply contained no data."));
6033
6034 /* Got some data. */
6035 i = remote_unescape_input (rs->buf + 1, packet_len - 1, readbuf, n);
6036
6037 /* 'l' is an EOF marker, possibly including a final block of data,
0e7f50da
UW
6038 or possibly empty. If we have the final block of a non-empty
6039 object, record this fact to bypass a subsequent partial read. */
6040 if (rs->buf[0] == 'l' && offset + i > 0)
0876f84a
DJ
6041 {
6042 finished_object = xstrdup (object_name);
6043 finished_annex = xstrdup (annex ? annex : "");
6044 finished_offset = offset + i;
6045 }
6046
6047 return i;
6048}
6049
1e3ff5ad 6050static LONGEST
4b8a223f 6051remote_xfer_partial (struct target_ops *ops, enum target_object object,
961cb7b5
MK
6052 const char *annex, gdb_byte *readbuf,
6053 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
c906108c 6054{
d01949b6 6055 struct remote_state *rs = get_remote_state ();
c906108c 6056 int i;
6d820c5c 6057 char *p2;
1e3ff5ad 6058 char query_type;
c906108c 6059
b2182ed2 6060 /* Handle memory using the standard memory routines. */
21e3b9b9
DJ
6061 if (object == TARGET_OBJECT_MEMORY)
6062 {
6063 int xfered;
6064 errno = 0;
6065
2d717e4f
DJ
6066 /* If the remote target is connected but not running, we should
6067 pass this request down to a lower stratum (e.g. the executable
6068 file). */
6069 if (!target_has_execution)
6070 return 0;
6071
21e3b9b9 6072 if (writebuf != NULL)
b2182ed2 6073 xfered = remote_write_bytes (offset, writebuf, len);
21e3b9b9 6074 else
b2182ed2 6075 xfered = remote_read_bytes (offset, readbuf, len);
21e3b9b9
DJ
6076
6077 if (xfered > 0)
6078 return xfered;
6079 else if (xfered == 0 && errno == 0)
6080 return 0;
6081 else
6082 return -1;
6083 }
6084
0e7f50da
UW
6085 /* Handle SPU memory using qxfer packets. */
6086 if (object == TARGET_OBJECT_SPU)
6087 {
6088 if (readbuf)
6089 return remote_read_qxfer (ops, "spu", annex, readbuf, offset, len,
6090 &remote_protocol_packets
6091 [PACKET_qXfer_spu_read]);
6092 else
6093 return remote_write_qxfer (ops, "spu", annex, writebuf, offset, len,
6094 &remote_protocol_packets
6095 [PACKET_qXfer_spu_write]);
6096 }
6097
a76d924d
DJ
6098 /* Only handle flash writes. */
6099 if (writebuf != NULL)
6100 {
6101 LONGEST xfered;
6102
6103 switch (object)
6104 {
6105 case TARGET_OBJECT_FLASH:
6106 xfered = remote_flash_write (ops, offset, len, writebuf);
6107
6108 if (xfered > 0)
6109 return xfered;
6110 else if (xfered == 0 && errno == 0)
6111 return 0;
6112 else
6113 return -1;
6114
6115 default:
6116 return -1;
6117 }
6118 }
4b8a223f 6119
1e3ff5ad
AC
6120 /* Map pre-existing objects onto letters. DO NOT do this for new
6121 objects!!! Instead specify new query packets. */
6122 switch (object)
c906108c 6123 {
1e3ff5ad
AC
6124 case TARGET_OBJECT_AVR:
6125 query_type = 'R';
6126 break;
802188a7
RM
6127
6128 case TARGET_OBJECT_AUXV:
0876f84a
DJ
6129 gdb_assert (annex == NULL);
6130 return remote_read_qxfer (ops, "auxv", annex, readbuf, offset, len,
6131 &remote_protocol_packets[PACKET_qXfer_auxv]);
802188a7 6132
23181151
DJ
6133 case TARGET_OBJECT_AVAILABLE_FEATURES:
6134 return remote_read_qxfer
6135 (ops, "features", annex, readbuf, offset, len,
6136 &remote_protocol_packets[PACKET_qXfer_features]);
6137
cfa9d6d9
DJ
6138 case TARGET_OBJECT_LIBRARIES:
6139 return remote_read_qxfer
6140 (ops, "libraries", annex, readbuf, offset, len,
6141 &remote_protocol_packets[PACKET_qXfer_libraries]);
6142
fd79ecee
DJ
6143 case TARGET_OBJECT_MEMORY_MAP:
6144 gdb_assert (annex == NULL);
6145 return remote_read_qxfer (ops, "memory-map", annex, readbuf, offset, len,
6146 &remote_protocol_packets[PACKET_qXfer_memory_map]);
6147
1e3ff5ad 6148 default:
c906108c
SS
6149 return -1;
6150 }
6151
4b8a223f 6152 /* Note: a zero OFFSET and LEN can be used to query the minimum
1e3ff5ad 6153 buffer size. */
4b8a223f 6154 if (offset == 0 && len == 0)
ea9c271d
DJ
6155 return (get_remote_packet_size ());
6156 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
24b06219 6157 large enough let the caller deal with it. */
ea9c271d 6158 if (len < get_remote_packet_size ())
1e3ff5ad 6159 return -1;
ea9c271d 6160 len = get_remote_packet_size ();
1e3ff5ad 6161
23860348 6162 /* Except for querying the minimum buffer size, target must be open. */
c5aa993b 6163 if (!remote_desc)
8a3fe4f8 6164 error (_("remote query is only available after target open"));
c906108c 6165
1e3ff5ad 6166 gdb_assert (annex != NULL);
4b8a223f 6167 gdb_assert (readbuf != NULL);
c906108c 6168
6d820c5c 6169 p2 = rs->buf;
c906108c
SS
6170 *p2++ = 'q';
6171 *p2++ = query_type;
6172
23860348
MS
6173 /* We used one buffer char for the remote protocol q command and
6174 another for the query type. As the remote protocol encapsulation
6175 uses 4 chars plus one extra in case we are debugging
6176 (remote_debug), we have PBUFZIZ - 7 left to pack the query
6177 string. */
c906108c 6178 i = 0;
ea9c271d 6179 while (annex[i] && (i < (get_remote_packet_size () - 8)))
c906108c 6180 {
1e3ff5ad
AC
6181 /* Bad caller may have sent forbidden characters. */
6182 gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#');
6183 *p2++ = annex[i];
c906108c
SS
6184 i++;
6185 }
1e3ff5ad
AC
6186 *p2 = '\0';
6187 gdb_assert (annex[i] == '\0');
c906108c 6188
6d820c5c 6189 i = putpkt (rs->buf);
c5aa993b
JM
6190 if (i < 0)
6191 return i;
c906108c 6192
6d820c5c
DJ
6193 getpkt (&rs->buf, &rs->buf_size, 0);
6194 strcpy ((char *) readbuf, rs->buf);
c906108c 6195
cfd77fa1 6196 return strlen ((char *) readbuf);
c906108c
SS
6197}
6198
08388c79
DE
6199static int
6200remote_search_memory (struct target_ops* ops,
6201 CORE_ADDR start_addr, ULONGEST search_space_len,
6202 const gdb_byte *pattern, ULONGEST pattern_len,
6203 CORE_ADDR *found_addrp)
6204{
6205 struct remote_state *rs = get_remote_state ();
6206 int max_size = get_memory_write_packet_size ();
6207 struct packet_config *packet =
6208 &remote_protocol_packets[PACKET_qSearch_memory];
6209 /* number of packet bytes used to encode the pattern,
6210 this could be more than PATTERN_LEN due to escape characters */
6211 int escaped_pattern_len;
6212 /* amount of pattern that was encodable in the packet */
6213 int used_pattern_len;
6214 int i;
6215 int found;
6216 ULONGEST found_addr;
6217
6218 /* Don't go to the target if we don't have to.
6219 This is done before checking packet->support to avoid the possibility that
6220 a success for this edge case means the facility works in general. */
6221 if (pattern_len > search_space_len)
6222 return 0;
6223 if (pattern_len == 0)
6224 {
6225 *found_addrp = start_addr;
6226 return 1;
6227 }
6228
6229 /* If we already know the packet isn't supported, fall back to the simple
6230 way of searching memory. */
6231
6232 if (packet->support == PACKET_DISABLE)
6233 {
6234 /* Target doesn't provided special support, fall back and use the
6235 standard support (copy memory and do the search here). */
6236 return simple_search_memory (ops, start_addr, search_space_len,
6237 pattern, pattern_len, found_addrp);
6238 }
6239
6240 /* Insert header. */
6241 i = snprintf (rs->buf, max_size,
6242 "qSearch:memory:%s;%s;",
6243 paddr_nz (start_addr),
6244 phex_nz (search_space_len, sizeof (search_space_len)));
6245 max_size -= (i + 1);
6246
6247 /* Escape as much data as fits into rs->buf. */
6248 escaped_pattern_len =
6249 remote_escape_output (pattern, pattern_len, (rs->buf + i),
6250 &used_pattern_len, max_size);
6251
6252 /* Bail if the pattern is too large. */
6253 if (used_pattern_len != pattern_len)
10e0fa18 6254 error ("Pattern is too large to transmit to remote target.");
08388c79
DE
6255
6256 if (putpkt_binary (rs->buf, i + escaped_pattern_len) < 0
6257 || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0
6258 || packet_ok (rs->buf, packet) != PACKET_OK)
6259 {
6260 /* The request may not have worked because the command is not
6261 supported. If so, fall back to the simple way. */
6262 if (packet->support == PACKET_DISABLE)
6263 {
6264 return simple_search_memory (ops, start_addr, search_space_len,
6265 pattern, pattern_len, found_addrp);
6266 }
6267 return -1;
6268 }
6269
6270 if (rs->buf[0] == '0')
6271 found = 0;
6272 else if (rs->buf[0] == '1')
6273 {
6274 found = 1;
6275 if (rs->buf[1] != ',')
10e0fa18 6276 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
08388c79
DE
6277 unpack_varlen_hex (rs->buf + 2, &found_addr);
6278 *found_addrp = found_addr;
6279 }
6280 else
10e0fa18 6281 error (_("Unknown qSearch:memory reply: %s"), rs->buf);
08388c79
DE
6282
6283 return found;
6284}
6285
96baa820
JM
6286static void
6287remote_rcmd (char *command,
d9fcf2fb 6288 struct ui_file *outbuf)
96baa820 6289{
d01949b6 6290 struct remote_state *rs = get_remote_state ();
2e9f7625 6291 char *p = rs->buf;
96baa820
JM
6292
6293 if (!remote_desc)
8a3fe4f8 6294 error (_("remote rcmd is only available after target open"));
96baa820 6295
23860348 6296 /* Send a NULL command across as an empty command. */
7be570e7
JM
6297 if (command == NULL)
6298 command = "";
6299
23860348 6300 /* The query prefix. */
2e9f7625
DJ
6301 strcpy (rs->buf, "qRcmd,");
6302 p = strchr (rs->buf, '\0');
96baa820 6303
2e9f7625 6304 if ((strlen (rs->buf) + strlen (command) * 2 + 8/*misc*/) > get_remote_packet_size ())
8a3fe4f8 6305 error (_("\"monitor\" command ``%s'' is too long."), command);
96baa820 6306
23860348 6307 /* Encode the actual command. */
cfd77fa1 6308 bin2hex ((gdb_byte *) command, p, 0);
96baa820 6309
6d820c5c 6310 if (putpkt (rs->buf) < 0)
8a3fe4f8 6311 error (_("Communication problem with target."));
96baa820
JM
6312
6313 /* get/display the response */
6314 while (1)
6315 {
2e9f7625
DJ
6316 char *buf;
6317
23860348 6318 /* XXX - see also tracepoint.c:remote_get_noisy_reply(). */
2e9f7625 6319 rs->buf[0] = '\0';
6d820c5c 6320 getpkt (&rs->buf, &rs->buf_size, 0);
2e9f7625 6321 buf = rs->buf;
96baa820 6322 if (buf[0] == '\0')
8a3fe4f8 6323 error (_("Target does not support this command."));
96baa820
JM
6324 if (buf[0] == 'O' && buf[1] != 'K')
6325 {
23860348 6326 remote_console_output (buf + 1); /* 'O' message from stub. */
96baa820
JM
6327 continue;
6328 }
6329 if (strcmp (buf, "OK") == 0)
6330 break;
7be570e7
JM
6331 if (strlen (buf) == 3 && buf[0] == 'E'
6332 && isdigit (buf[1]) && isdigit (buf[2]))
6333 {
8a3fe4f8 6334 error (_("Protocol error with Rcmd"));
7be570e7 6335 }
96baa820
JM
6336 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
6337 {
6338 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
6339 fputc_unfiltered (c, outbuf);
6340 }
6341 break;
6342 }
6343}
6344
fd79ecee
DJ
6345static VEC(mem_region_s) *
6346remote_memory_map (struct target_ops *ops)
6347{
6348 VEC(mem_region_s) *result = NULL;
6349 char *text = target_read_stralloc (&current_target,
6350 TARGET_OBJECT_MEMORY_MAP, NULL);
6351
6352 if (text)
6353 {
6354 struct cleanup *back_to = make_cleanup (xfree, text);
6355 result = parse_memory_map (text);
6356 do_cleanups (back_to);
6357 }
6358
6359 return result;
6360}
6361
c906108c 6362static void
fba45db2 6363packet_command (char *args, int from_tty)
c906108c 6364{
d01949b6 6365 struct remote_state *rs = get_remote_state ();
c906108c 6366
c5aa993b 6367 if (!remote_desc)
8a3fe4f8 6368 error (_("command can only be used with remote target"));
c906108c 6369
c5aa993b 6370 if (!args)
8a3fe4f8 6371 error (_("remote-packet command requires packet text as argument"));
c906108c
SS
6372
6373 puts_filtered ("sending: ");
6374 print_packet (args);
6375 puts_filtered ("\n");
6376 putpkt (args);
6377
6d820c5c 6378 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 6379 puts_filtered ("received: ");
6d820c5c 6380 print_packet (rs->buf);
c906108c
SS
6381 puts_filtered ("\n");
6382}
6383
6384#if 0
23860348 6385/* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
c906108c 6386
a14ed312 6387static void display_thread_info (struct gdb_ext_thread_info *info);
c906108c 6388
a14ed312 6389static void threadset_test_cmd (char *cmd, int tty);
c906108c 6390
a14ed312 6391static void threadalive_test (char *cmd, int tty);
c906108c 6392
a14ed312 6393static void threadlist_test_cmd (char *cmd, int tty);
c906108c 6394
23860348 6395int get_and_display_threadinfo (threadref *ref);
c906108c 6396
a14ed312 6397static void threadinfo_test_cmd (char *cmd, int tty);
c906108c 6398
23860348 6399static int thread_display_step (threadref *ref, void *context);
c906108c 6400
a14ed312 6401static void threadlist_update_test_cmd (char *cmd, int tty);
c906108c 6402
a14ed312 6403static void init_remote_threadtests (void);
c906108c 6404
23860348 6405#define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
c906108c
SS
6406
6407static void
fba45db2 6408threadset_test_cmd (char *cmd, int tty)
c906108c
SS
6409{
6410 int sample_thread = SAMPLE_THREAD;
6411
a3f17187 6412 printf_filtered (_("Remote threadset test\n"));
c906108c
SS
6413 set_thread (sample_thread, 1);
6414}
6415
6416
6417static void
fba45db2 6418threadalive_test (char *cmd, int tty)
c906108c
SS
6419{
6420 int sample_thread = SAMPLE_THREAD;
6421
39f77062 6422 if (remote_thread_alive (pid_to_ptid (sample_thread)))
c906108c
SS
6423 printf_filtered ("PASS: Thread alive test\n");
6424 else
6425 printf_filtered ("FAIL: Thread alive test\n");
6426}
6427
23860348 6428void output_threadid (char *title, threadref *ref);
c906108c
SS
6429
6430void
fba45db2 6431output_threadid (char *title, threadref *ref)
c906108c
SS
6432{
6433 char hexid[20];
6434
23860348 6435 pack_threadid (&hexid[0], ref); /* Convert threead id into hex. */
c906108c
SS
6436 hexid[16] = 0;
6437 printf_filtered ("%s %s\n", title, (&hexid[0]));
6438}
6439
6440static void
fba45db2 6441threadlist_test_cmd (char *cmd, int tty)
c906108c
SS
6442{
6443 int startflag = 1;
6444 threadref nextthread;
6445 int done, result_count;
6446 threadref threadlist[3];
6447
6448 printf_filtered ("Remote Threadlist test\n");
6449 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
6450 &result_count, &threadlist[0]))
6451 printf_filtered ("FAIL: threadlist test\n");
6452 else
6453 {
6454 threadref *scan = threadlist;
6455 threadref *limit = scan + result_count;
6456
6457 while (scan < limit)
6458 output_threadid (" thread ", scan++);
6459 }
6460}
6461
6462void
fba45db2 6463display_thread_info (struct gdb_ext_thread_info *info)
c906108c
SS
6464{
6465 output_threadid ("Threadid: ", &info->threadid);
6466 printf_filtered ("Name: %s\n ", info->shortname);
6467 printf_filtered ("State: %s\n", info->display);
6468 printf_filtered ("other: %s\n\n", info->more_display);
6469}
6470
6471int
fba45db2 6472get_and_display_threadinfo (threadref *ref)
c906108c
SS
6473{
6474 int result;
6475 int set;
6476 struct gdb_ext_thread_info threadinfo;
6477
6478 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
6479 | TAG_MOREDISPLAY | TAG_DISPLAY;
6480 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
6481 display_thread_info (&threadinfo);
6482 return result;
6483}
6484
6485static void
fba45db2 6486threadinfo_test_cmd (char *cmd, int tty)
c906108c
SS
6487{
6488 int athread = SAMPLE_THREAD;
6489 threadref thread;
6490 int set;
6491
6492 int_to_threadref (&thread, athread);
6493 printf_filtered ("Remote Threadinfo test\n");
6494 if (!get_and_display_threadinfo (&thread))
6495 printf_filtered ("FAIL cannot get thread info\n");
6496}
6497
6498static int
fba45db2 6499thread_display_step (threadref *ref, void *context)
c906108c
SS
6500{
6501 /* output_threadid(" threadstep ",ref); *//* simple test */
6502 return get_and_display_threadinfo (ref);
6503}
6504
6505static void
fba45db2 6506threadlist_update_test_cmd (char *cmd, int tty)
c906108c
SS
6507{
6508 printf_filtered ("Remote Threadlist update test\n");
6509 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
6510}
6511
6512static void
6513init_remote_threadtests (void)
6514{
1bedd215
AC
6515 add_com ("tlist", class_obscure, threadlist_test_cmd, _("\
6516Fetch and print the remote list of thread identifiers, one pkt only"));
c906108c 6517 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
1bedd215 6518 _("Fetch and display info about one thread"));
c906108c 6519 add_com ("tset", class_obscure, threadset_test_cmd,
1bedd215 6520 _("Test setting to a different thread"));
c906108c 6521 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
1bedd215 6522 _("Iterate through updating all remote thread info"));
c906108c 6523 add_com ("talive", class_obscure, threadalive_test,
1bedd215 6524 _(" Remote thread alive test "));
c906108c
SS
6525}
6526
6527#endif /* 0 */
6528
f3fb8c85
MS
6529/* Convert a thread ID to a string. Returns the string in a static
6530 buffer. */
6531
6532static char *
39f77062 6533remote_pid_to_str (ptid_t ptid)
f3fb8c85 6534{
fd0a2a6f 6535 static char buf[32];
f3fb8c85 6536
32a5b2f1 6537 xsnprintf (buf, sizeof buf, "Thread %d", ptid_get_pid (ptid));
f3fb8c85
MS
6538 return buf;
6539}
6540
38691318
KB
6541/* Get the address of the thread local variable in OBJFILE which is
6542 stored at OFFSET within the thread local storage for thread PTID. */
6543
6544static CORE_ADDR
6545remote_get_thread_local_address (ptid_t ptid, CORE_ADDR lm, CORE_ADDR offset)
6546{
444abaca 6547 if (remote_protocol_packets[PACKET_qGetTLSAddr].support != PACKET_DISABLE)
38691318
KB
6548 {
6549 struct remote_state *rs = get_remote_state ();
6d820c5c 6550 char *p = rs->buf;
571dd617 6551 enum packet_result result;
38691318
KB
6552
6553 strcpy (p, "qGetTLSAddr:");
6554 p += strlen (p);
6555 p += hexnumstr (p, PIDGET (ptid));
6556 *p++ = ',';
6557 p += hexnumstr (p, offset);
6558 *p++ = ',';
6559 p += hexnumstr (p, lm);
6560 *p++ = '\0';
6561
6d820c5c
DJ
6562 putpkt (rs->buf);
6563 getpkt (&rs->buf, &rs->buf_size, 0);
6564 result = packet_ok (rs->buf, &remote_protocol_packets[PACKET_qGetTLSAddr]);
571dd617 6565 if (result == PACKET_OK)
38691318
KB
6566 {
6567 ULONGEST result;
6568
6d820c5c 6569 unpack_varlen_hex (rs->buf, &result);
38691318
KB
6570 return result;
6571 }
571dd617 6572 else if (result == PACKET_UNKNOWN)
109c3e39
AC
6573 throw_error (TLS_GENERIC_ERROR,
6574 _("Remote target doesn't support qGetTLSAddr packet"));
38691318 6575 else
109c3e39
AC
6576 throw_error (TLS_GENERIC_ERROR,
6577 _("Remote target failed to process qGetTLSAddr request"));
38691318
KB
6578 }
6579 else
109c3e39
AC
6580 throw_error (TLS_GENERIC_ERROR,
6581 _("TLS not supported or disabled on this target"));
38691318
KB
6582 /* Not reached. */
6583 return 0;
6584}
6585
29709017
DJ
6586/* Support for inferring a target description based on the current
6587 architecture and the size of a 'g' packet. While the 'g' packet
6588 can have any size (since optional registers can be left off the
6589 end), some sizes are easily recognizable given knowledge of the
6590 approximate architecture. */
6591
6592struct remote_g_packet_guess
6593{
6594 int bytes;
6595 const struct target_desc *tdesc;
6596};
6597typedef struct remote_g_packet_guess remote_g_packet_guess_s;
6598DEF_VEC_O(remote_g_packet_guess_s);
6599
6600struct remote_g_packet_data
6601{
6602 VEC(remote_g_packet_guess_s) *guesses;
6603};
6604
6605static struct gdbarch_data *remote_g_packet_data_handle;
6606
6607static void *
6608remote_g_packet_data_init (struct obstack *obstack)
6609{
6610 return OBSTACK_ZALLOC (obstack, struct remote_g_packet_data);
6611}
6612
6613void
6614register_remote_g_packet_guess (struct gdbarch *gdbarch, int bytes,
6615 const struct target_desc *tdesc)
6616{
6617 struct remote_g_packet_data *data
6618 = gdbarch_data (gdbarch, remote_g_packet_data_handle);
6619 struct remote_g_packet_guess new_guess, *guess;
6620 int ix;
6621
6622 gdb_assert (tdesc != NULL);
6623
6624 for (ix = 0;
6625 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
6626 ix++)
6627 if (guess->bytes == bytes)
6628 internal_error (__FILE__, __LINE__,
6629 "Duplicate g packet description added for size %d",
6630 bytes);
6631
6632 new_guess.bytes = bytes;
6633 new_guess.tdesc = tdesc;
6634 VEC_safe_push (remote_g_packet_guess_s, data->guesses, &new_guess);
6635}
6636
6637static const struct target_desc *
6638remote_read_description (struct target_ops *target)
6639{
6640 struct remote_g_packet_data *data
6641 = gdbarch_data (current_gdbarch, remote_g_packet_data_handle);
6642
6643 if (!VEC_empty (remote_g_packet_guess_s, data->guesses))
6644 {
6645 struct remote_g_packet_guess *guess;
6646 int ix;
6647 int bytes = send_g_packet ();
6648
6649 for (ix = 0;
6650 VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess);
6651 ix++)
6652 if (guess->bytes == bytes)
6653 return guess->tdesc;
6654
6655 /* We discard the g packet. A minor optimization would be to
6656 hold on to it, and fill the register cache once we have selected
6657 an architecture, but it's too tricky to do safely. */
6658 }
6659
6660 return NULL;
6661}
6662
a6b151f1
DJ
6663/* Remote file transfer support. This is host-initiated I/O, not
6664 target-initiated; for target-initiated, see remote-fileio.c. */
6665
6666/* If *LEFT is at least the length of STRING, copy STRING to
6667 *BUFFER, update *BUFFER to point to the new end of the buffer, and
6668 decrease *LEFT. Otherwise raise an error. */
6669
6670static void
6671remote_buffer_add_string (char **buffer, int *left, char *string)
6672{
6673 int len = strlen (string);
6674
6675 if (len > *left)
6676 error (_("Packet too long for target."));
6677
6678 memcpy (*buffer, string, len);
6679 *buffer += len;
6680 *left -= len;
6681
6682 /* NUL-terminate the buffer as a convenience, if there is
6683 room. */
6684 if (*left)
6685 **buffer = '\0';
6686}
6687
6688/* If *LEFT is large enough, hex encode LEN bytes from BYTES into
6689 *BUFFER, update *BUFFER to point to the new end of the buffer, and
6690 decrease *LEFT. Otherwise raise an error. */
6691
6692static void
6693remote_buffer_add_bytes (char **buffer, int *left, const gdb_byte *bytes,
6694 int len)
6695{
6696 if (2 * len > *left)
6697 error (_("Packet too long for target."));
6698
6699 bin2hex (bytes, *buffer, len);
6700 *buffer += 2 * len;
6701 *left -= 2 * len;
6702
6703 /* NUL-terminate the buffer as a convenience, if there is
6704 room. */
6705 if (*left)
6706 **buffer = '\0';
6707}
6708
6709/* If *LEFT is large enough, convert VALUE to hex and add it to
6710 *BUFFER, update *BUFFER to point to the new end of the buffer, and
6711 decrease *LEFT. Otherwise raise an error. */
6712
6713static void
6714remote_buffer_add_int (char **buffer, int *left, ULONGEST value)
6715{
6716 int len = hexnumlen (value);
6717
6718 if (len > *left)
6719 error (_("Packet too long for target."));
6720
6721 hexnumstr (*buffer, value);
6722 *buffer += len;
6723 *left -= len;
6724
6725 /* NUL-terminate the buffer as a convenience, if there is
6726 room. */
6727 if (*left)
6728 **buffer = '\0';
6729}
6730
6731/* Parse an I/O result packet from BUFFER. Set RETCODE to the return
6732 value, *REMOTE_ERRNO to the remote error number or zero if none
6733 was included, and *ATTACHMENT to point to the start of the annex
6734 if any. The length of the packet isn't needed here; there may
6735 be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
6736
6737 Return 0 if the packet could be parsed, -1 if it could not. If
6738 -1 is returned, the other variables may not be initialized. */
6739
6740static int
6741remote_hostio_parse_result (char *buffer, int *retcode,
6742 int *remote_errno, char **attachment)
6743{
6744 char *p, *p2;
6745
6746 *remote_errno = 0;
6747 *attachment = NULL;
6748
6749 if (buffer[0] != 'F')
6750 return -1;
6751
6752 errno = 0;
6753 *retcode = strtol (&buffer[1], &p, 16);
6754 if (errno != 0 || p == &buffer[1])
6755 return -1;
6756
6757 /* Check for ",errno". */
6758 if (*p == ',')
6759 {
6760 errno = 0;
6761 *remote_errno = strtol (p + 1, &p2, 16);
6762 if (errno != 0 || p + 1 == p2)
6763 return -1;
6764 p = p2;
6765 }
6766
6767 /* Check for ";attachment". If there is no attachment, the
6768 packet should end here. */
6769 if (*p == ';')
6770 {
6771 *attachment = p + 1;
6772 return 0;
6773 }
6774 else if (*p == '\0')
6775 return 0;
6776 else
6777 return -1;
6778}
6779
6780/* Send a prepared I/O packet to the target and read its response.
6781 The prepared packet is in the global RS->BUF before this function
6782 is called, and the answer is there when we return.
6783
6784 COMMAND_BYTES is the length of the request to send, which may include
6785 binary data. WHICH_PACKET is the packet configuration to check
6786 before attempting a packet. If an error occurs, *REMOTE_ERRNO
6787 is set to the error number and -1 is returned. Otherwise the value
6788 returned by the function is returned.
6789
6790 ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
6791 attachment is expected; an error will be reported if there's a
6792 mismatch. If one is found, *ATTACHMENT will be set to point into
6793 the packet buffer and *ATTACHMENT_LEN will be set to the
6794 attachment's length. */
6795
6796static int
6797remote_hostio_send_command (int command_bytes, int which_packet,
6798 int *remote_errno, char **attachment,
6799 int *attachment_len)
6800{
6801 struct remote_state *rs = get_remote_state ();
6802 int ret, bytes_read;
6803 char *attachment_tmp;
6804
6805 if (remote_protocol_packets[which_packet].support == PACKET_DISABLE)
6806 {
6807 *remote_errno = FILEIO_ENOSYS;
6808 return -1;
6809 }
6810
6811 putpkt_binary (rs->buf, command_bytes);
6812 bytes_read = getpkt_sane (&rs->buf, &rs->buf_size, 0);
6813
6814 /* If it timed out, something is wrong. Don't try to parse the
6815 buffer. */
6816 if (bytes_read < 0)
6817 {
6818 *remote_errno = FILEIO_EINVAL;
6819 return -1;
6820 }
6821
6822 switch (packet_ok (rs->buf, &remote_protocol_packets[which_packet]))
6823 {
6824 case PACKET_ERROR:
6825 *remote_errno = FILEIO_EINVAL;
6826 return -1;
6827 case PACKET_UNKNOWN:
6828 *remote_errno = FILEIO_ENOSYS;
6829 return -1;
6830 case PACKET_OK:
6831 break;
6832 }
6833
6834 if (remote_hostio_parse_result (rs->buf, &ret, remote_errno,
6835 &attachment_tmp))
6836 {
6837 *remote_errno = FILEIO_EINVAL;
6838 return -1;
6839 }
6840
6841 /* Make sure we saw an attachment if and only if we expected one. */
6842 if ((attachment_tmp == NULL && attachment != NULL)
6843 || (attachment_tmp != NULL && attachment == NULL))
6844 {
6845 *remote_errno = FILEIO_EINVAL;
6846 return -1;
6847 }
6848
6849 /* If an attachment was found, it must point into the packet buffer;
6850 work out how many bytes there were. */
6851 if (attachment_tmp != NULL)
6852 {
6853 *attachment = attachment_tmp;
6854 *attachment_len = bytes_read - (*attachment - rs->buf);
6855 }
6856
6857 return ret;
6858}
6859
6860/* Open FILENAME on the remote target, using FLAGS and MODE. Return a
6861 remote file descriptor, or -1 if an error occurs (and set
6862 *REMOTE_ERRNO). */
6863
6864static int
6865remote_hostio_open (const char *filename, int flags, int mode,
6866 int *remote_errno)
6867{
6868 struct remote_state *rs = get_remote_state ();
6869 char *p = rs->buf;
6870 int left = get_remote_packet_size () - 1;
6871
6872 remote_buffer_add_string (&p, &left, "vFile:open:");
6873
6874 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
6875 strlen (filename));
6876 remote_buffer_add_string (&p, &left, ",");
6877
6878 remote_buffer_add_int (&p, &left, flags);
6879 remote_buffer_add_string (&p, &left, ",");
6880
6881 remote_buffer_add_int (&p, &left, mode);
6882
6883 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_open,
6884 remote_errno, NULL, NULL);
6885}
6886
6887/* Write up to LEN bytes from WRITE_BUF to FD on the remote target.
6888 Return the number of bytes written, or -1 if an error occurs (and
6889 set *REMOTE_ERRNO). */
6890
6891static int
6892remote_hostio_pwrite (int fd, const gdb_byte *write_buf, int len,
6893 ULONGEST offset, int *remote_errno)
6894{
6895 struct remote_state *rs = get_remote_state ();
6896 char *p = rs->buf;
6897 int left = get_remote_packet_size ();
6898 int out_len;
6899
6900 remote_buffer_add_string (&p, &left, "vFile:pwrite:");
6901
6902 remote_buffer_add_int (&p, &left, fd);
6903 remote_buffer_add_string (&p, &left, ",");
6904
6905 remote_buffer_add_int (&p, &left, offset);
6906 remote_buffer_add_string (&p, &left, ",");
6907
6908 p += remote_escape_output (write_buf, len, p, &out_len,
6909 get_remote_packet_size () - (p - rs->buf));
6910
6911 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_pwrite,
6912 remote_errno, NULL, NULL);
6913}
6914
6915/* Read up to LEN bytes FD on the remote target into READ_BUF
6916 Return the number of bytes read, or -1 if an error occurs (and
6917 set *REMOTE_ERRNO). */
6918
6919static int
6920remote_hostio_pread (int fd, gdb_byte *read_buf, int len,
6921 ULONGEST offset, int *remote_errno)
6922{
6923 struct remote_state *rs = get_remote_state ();
6924 char *p = rs->buf;
6925 char *attachment;
6926 int left = get_remote_packet_size ();
6927 int ret, attachment_len;
6928 int read_len;
6929
6930 remote_buffer_add_string (&p, &left, "vFile:pread:");
6931
6932 remote_buffer_add_int (&p, &left, fd);
6933 remote_buffer_add_string (&p, &left, ",");
6934
6935 remote_buffer_add_int (&p, &left, len);
6936 remote_buffer_add_string (&p, &left, ",");
6937
6938 remote_buffer_add_int (&p, &left, offset);
6939
6940 ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_pread,
6941 remote_errno, &attachment,
6942 &attachment_len);
6943
6944 if (ret < 0)
6945 return ret;
6946
6947 read_len = remote_unescape_input (attachment, attachment_len,
6948 read_buf, len);
6949 if (read_len != ret)
6950 error (_("Read returned %d, but %d bytes."), ret, (int) read_len);
6951
6952 return ret;
6953}
6954
6955/* Close FD on the remote target. Return 0, or -1 if an error occurs
6956 (and set *REMOTE_ERRNO). */
6957
6958static int
6959remote_hostio_close (int fd, int *remote_errno)
6960{
6961 struct remote_state *rs = get_remote_state ();
6962 char *p = rs->buf;
6963 int left = get_remote_packet_size () - 1;
6964
6965 remote_buffer_add_string (&p, &left, "vFile:close:");
6966
6967 remote_buffer_add_int (&p, &left, fd);
6968
6969 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_close,
6970 remote_errno, NULL, NULL);
6971}
6972
6973/* Unlink FILENAME on the remote target. Return 0, or -1 if an error
6974 occurs (and set *REMOTE_ERRNO). */
6975
6976static int
6977remote_hostio_unlink (const char *filename, int *remote_errno)
6978{
6979 struct remote_state *rs = get_remote_state ();
6980 char *p = rs->buf;
6981 int left = get_remote_packet_size () - 1;
6982
6983 remote_buffer_add_string (&p, &left, "vFile:unlink:");
6984
6985 remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
6986 strlen (filename));
6987
6988 return remote_hostio_send_command (p - rs->buf, PACKET_vFile_unlink,
6989 remote_errno, NULL, NULL);
6990}
6991
6992static int
6993remote_fileio_errno_to_host (int errnum)
6994{
6995 switch (errnum)
6996 {
6997 case FILEIO_EPERM:
6998 return EPERM;
6999 case FILEIO_ENOENT:
7000 return ENOENT;
7001 case FILEIO_EINTR:
7002 return EINTR;
7003 case FILEIO_EIO:
7004 return EIO;
7005 case FILEIO_EBADF:
7006 return EBADF;
7007 case FILEIO_EACCES:
7008 return EACCES;
7009 case FILEIO_EFAULT:
7010 return EFAULT;
7011 case FILEIO_EBUSY:
7012 return EBUSY;
7013 case FILEIO_EEXIST:
7014 return EEXIST;
7015 case FILEIO_ENODEV:
7016 return ENODEV;
7017 case FILEIO_ENOTDIR:
7018 return ENOTDIR;
7019 case FILEIO_EISDIR:
7020 return EISDIR;
7021 case FILEIO_EINVAL:
7022 return EINVAL;
7023 case FILEIO_ENFILE:
7024 return ENFILE;
7025 case FILEIO_EMFILE:
7026 return EMFILE;
7027 case FILEIO_EFBIG:
7028 return EFBIG;
7029 case FILEIO_ENOSPC:
7030 return ENOSPC;
7031 case FILEIO_ESPIPE:
7032 return ESPIPE;
7033 case FILEIO_EROFS:
7034 return EROFS;
7035 case FILEIO_ENOSYS:
7036 return ENOSYS;
7037 case FILEIO_ENAMETOOLONG:
7038 return ENAMETOOLONG;
7039 }
7040 return -1;
7041}
7042
7043static char *
7044remote_hostio_error (int errnum)
7045{
7046 int host_error = remote_fileio_errno_to_host (errnum);
7047
7048 if (host_error == -1)
7049 error (_("Unknown remote I/O error %d"), errnum);
7050 else
7051 error (_("Remote I/O error: %s"), safe_strerror (host_error));
7052}
7053
7054static void
7055fclose_cleanup (void *file)
7056{
7057 fclose (file);
7058}
7059
7060static void
7061remote_hostio_close_cleanup (void *opaque)
7062{
7063 int fd = *(int *) opaque;
7064 int remote_errno;
7065
7066 remote_hostio_close (fd, &remote_errno);
7067}
7068
7069void
7070remote_file_put (const char *local_file, const char *remote_file, int from_tty)
7071{
7072 struct cleanup *back_to, *close_cleanup;
7073 int retcode, fd, remote_errno, bytes, io_size;
7074 FILE *file;
7075 gdb_byte *buffer;
7076 int bytes_in_buffer;
7077 int saw_eof;
7078 ULONGEST offset;
7079
7080 if (!remote_desc)
7081 error (_("command can only be used with remote target"));
7082
7083 file = fopen (local_file, "rb");
7084 if (file == NULL)
7085 perror_with_name (local_file);
7086 back_to = make_cleanup (fclose_cleanup, file);
7087
7088 fd = remote_hostio_open (remote_file, (FILEIO_O_WRONLY | FILEIO_O_CREAT
7089 | FILEIO_O_TRUNC),
7090 0700, &remote_errno);
7091 if (fd == -1)
7092 remote_hostio_error (remote_errno);
7093
7094 /* Send up to this many bytes at once. They won't all fit in the
7095 remote packet limit, so we'll transfer slightly fewer. */
7096 io_size = get_remote_packet_size ();
7097 buffer = xmalloc (io_size);
7098 make_cleanup (xfree, buffer);
7099
7100 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
7101
7102 bytes_in_buffer = 0;
7103 saw_eof = 0;
7104 offset = 0;
7105 while (bytes_in_buffer || !saw_eof)
7106 {
7107 if (!saw_eof)
7108 {
7109 bytes = fread (buffer + bytes_in_buffer, 1, io_size - bytes_in_buffer,
7110 file);
7111 if (bytes == 0)
7112 {
7113 if (ferror (file))
7114 error (_("Error reading %s."), local_file);
7115 else
7116 {
7117 /* EOF. Unless there is something still in the
7118 buffer from the last iteration, we are done. */
7119 saw_eof = 1;
7120 if (bytes_in_buffer == 0)
7121 break;
7122 }
7123 }
7124 }
7125 else
7126 bytes = 0;
7127
7128 bytes += bytes_in_buffer;
7129 bytes_in_buffer = 0;
7130
7131 retcode = remote_hostio_pwrite (fd, buffer, bytes, offset, &remote_errno);
7132
7133 if (retcode < 0)
7134 remote_hostio_error (remote_errno);
7135 else if (retcode == 0)
7136 error (_("Remote write of %d bytes returned 0!"), bytes);
7137 else if (retcode < bytes)
7138 {
7139 /* Short write. Save the rest of the read data for the next
7140 write. */
7141 bytes_in_buffer = bytes - retcode;
7142 memmove (buffer, buffer + retcode, bytes_in_buffer);
7143 }
7144
7145 offset += retcode;
7146 }
7147
7148 discard_cleanups (close_cleanup);
7149 if (remote_hostio_close (fd, &remote_errno))
7150 remote_hostio_error (remote_errno);
7151
7152 if (from_tty)
7153 printf_filtered (_("Successfully sent file \"%s\".\n"), local_file);
7154 do_cleanups (back_to);
7155}
7156
7157void
7158remote_file_get (const char *remote_file, const char *local_file, int from_tty)
7159{
7160 struct cleanup *back_to, *close_cleanup;
7161 int retcode, fd, remote_errno, bytes, io_size;
7162 FILE *file;
7163 gdb_byte *buffer;
7164 ULONGEST offset;
7165
7166 if (!remote_desc)
7167 error (_("command can only be used with remote target"));
7168
7169 fd = remote_hostio_open (remote_file, FILEIO_O_RDONLY, 0, &remote_errno);
7170 if (fd == -1)
7171 remote_hostio_error (remote_errno);
7172
7173 file = fopen (local_file, "wb");
7174 if (file == NULL)
7175 perror_with_name (local_file);
7176 back_to = make_cleanup (fclose_cleanup, file);
7177
7178 /* Send up to this many bytes at once. They won't all fit in the
7179 remote packet limit, so we'll transfer slightly fewer. */
7180 io_size = get_remote_packet_size ();
7181 buffer = xmalloc (io_size);
7182 make_cleanup (xfree, buffer);
7183
7184 close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
7185
7186 offset = 0;
7187 while (1)
7188 {
7189 bytes = remote_hostio_pread (fd, buffer, io_size, offset, &remote_errno);
7190 if (bytes == 0)
7191 /* Success, but no bytes, means end-of-file. */
7192 break;
7193 if (bytes == -1)
7194 remote_hostio_error (remote_errno);
7195
7196 offset += bytes;
7197
7198 bytes = fwrite (buffer, 1, bytes, file);
7199 if (bytes == 0)
7200 perror_with_name (local_file);
7201 }
7202
7203 discard_cleanups (close_cleanup);
7204 if (remote_hostio_close (fd, &remote_errno))
7205 remote_hostio_error (remote_errno);
7206
7207 if (from_tty)
7208 printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file);
7209 do_cleanups (back_to);
7210}
7211
7212void
7213remote_file_delete (const char *remote_file, int from_tty)
7214{
7215 int retcode, remote_errno;
7216
7217 if (!remote_desc)
7218 error (_("command can only be used with remote target"));
7219
7220 retcode = remote_hostio_unlink (remote_file, &remote_errno);
7221 if (retcode == -1)
7222 remote_hostio_error (remote_errno);
7223
7224 if (from_tty)
7225 printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file);
7226}
7227
7228static void
7229remote_put_command (char *args, int from_tty)
7230{
7231 struct cleanup *back_to;
7232 char **argv;
7233
7234 argv = buildargv (args);
7235 if (argv == NULL)
7236 nomem (0);
7237 back_to = make_cleanup_freeargv (argv);
7238 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
7239 error (_("Invalid parameters to remote put"));
7240
7241 remote_file_put (argv[0], argv[1], from_tty);
7242
7243 do_cleanups (back_to);
7244}
7245
7246static void
7247remote_get_command (char *args, int from_tty)
7248{
7249 struct cleanup *back_to;
7250 char **argv;
7251
7252 argv = buildargv (args);
7253 if (argv == NULL)
7254 nomem (0);
7255 back_to = make_cleanup_freeargv (argv);
7256 if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
7257 error (_("Invalid parameters to remote get"));
7258
7259 remote_file_get (argv[0], argv[1], from_tty);
7260
7261 do_cleanups (back_to);
7262}
7263
7264static void
7265remote_delete_command (char *args, int from_tty)
7266{
7267 struct cleanup *back_to;
7268 char **argv;
7269
7270 argv = buildargv (args);
7271 if (argv == NULL)
7272 nomem (0);
7273 back_to = make_cleanup_freeargv (argv);
7274 if (argv[0] == NULL || argv[1] != NULL)
7275 error (_("Invalid parameters to remote delete"));
7276
7277 remote_file_delete (argv[0], from_tty);
7278
7279 do_cleanups (back_to);
7280}
7281
7282static void
7283remote_command (char *args, int from_tty)
7284{
7285 help_list (remote_cmdlist, "remote ", -1, gdb_stdout);
7286}
7287
b84876c2
PA
7288static int
7289remote_return_zero (void)
7290{
7291 return 0;
7292}
7293
c906108c 7294static void
fba45db2 7295init_remote_ops (void)
c906108c 7296{
c5aa993b 7297 remote_ops.to_shortname = "remote";
c906108c 7298 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
c5aa993b 7299 remote_ops.to_doc =
c906108c 7300 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
0d06e24b
JM
7301Specify the serial device it is connected to\n\
7302(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
c5aa993b
JM
7303 remote_ops.to_open = remote_open;
7304 remote_ops.to_close = remote_close;
c906108c 7305 remote_ops.to_detach = remote_detach;
6ad8ae5c 7306 remote_ops.to_disconnect = remote_disconnect;
c5aa993b 7307 remote_ops.to_resume = remote_resume;
c906108c
SS
7308 remote_ops.to_wait = remote_wait;
7309 remote_ops.to_fetch_registers = remote_fetch_registers;
7310 remote_ops.to_store_registers = remote_store_registers;
7311 remote_ops.to_prepare_to_store = remote_prepare_to_store;
c8e73a31 7312 remote_ops.deprecated_xfer_memory = remote_xfer_memory;
c5aa993b 7313 remote_ops.to_files_info = remote_files_info;
c906108c
SS
7314 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
7315 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
3c3bea1c
GS
7316 remote_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
7317 remote_ops.to_stopped_data_address = remote_stopped_data_address;
7318 remote_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
7319 remote_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
7320 remote_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
7321 remote_ops.to_insert_watchpoint = remote_insert_watchpoint;
7322 remote_ops.to_remove_watchpoint = remote_remove_watchpoint;
c5aa993b
JM
7323 remote_ops.to_kill = remote_kill;
7324 remote_ops.to_load = generic_load;
c906108c
SS
7325 remote_ops.to_mourn_inferior = remote_mourn;
7326 remote_ops.to_thread_alive = remote_thread_alive;
0f71a2f6 7327 remote_ops.to_find_new_threads = remote_threads_info;
0caabb7e 7328 remote_ops.to_pid_to_str = remote_pid_to_str;
cf759d3b 7329 remote_ops.to_extra_thread_info = remote_threads_extra_info;
c906108c 7330 remote_ops.to_stop = remote_stop;
4b8a223f 7331 remote_ops.to_xfer_partial = remote_xfer_partial;
96baa820 7332 remote_ops.to_rcmd = remote_rcmd;
49d03eab 7333 remote_ops.to_log_command = serial_log_command;
38691318 7334 remote_ops.to_get_thread_local_address = remote_get_thread_local_address;
c906108c 7335 remote_ops.to_stratum = process_stratum;
c5aa993b
JM
7336 remote_ops.to_has_all_memory = 1;
7337 remote_ops.to_has_memory = 1;
7338 remote_ops.to_has_stack = 1;
7339 remote_ops.to_has_registers = 1;
7340 remote_ops.to_has_execution = 1;
7341 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
7342 remote_ops.to_magic = OPS_MAGIC;
fd79ecee 7343 remote_ops.to_memory_map = remote_memory_map;
a76d924d
DJ
7344 remote_ops.to_flash_erase = remote_flash_erase;
7345 remote_ops.to_flash_done = remote_flash_done;
29709017 7346 remote_ops.to_read_description = remote_read_description;
08388c79 7347 remote_ops.to_search_memory = remote_search_memory;
b84876c2
PA
7348 remote_ops.to_can_async_p = remote_return_zero;
7349 remote_ops.to_is_async_p = remote_return_zero;
c906108c
SS
7350}
7351
7352/* Set up the extended remote vector by making a copy of the standard
7353 remote vector and adding to it. */
7354
7355static void
fba45db2 7356init_extended_remote_ops (void)
c906108c
SS
7357{
7358 extended_remote_ops = remote_ops;
7359
0f71a2f6 7360 extended_remote_ops.to_shortname = "extended-remote";
c5aa993b 7361 extended_remote_ops.to_longname =
c906108c 7362 "Extended remote serial target in gdb-specific protocol";
c5aa993b 7363 extended_remote_ops.to_doc =
c906108c 7364 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
39237dd1
PA
7365Specify the serial device it is connected to (e.g. /dev/ttya).";
7366 extended_remote_ops.to_open = extended_remote_open;
c906108c
SS
7367 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
7368 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
2d717e4f
DJ
7369 extended_remote_ops.to_detach = extended_remote_detach;
7370 extended_remote_ops.to_attach = extended_remote_attach;
0f71a2f6
JM
7371}
7372
6426a772
JM
7373static int
7374remote_can_async_p (void)
7375{
23860348 7376 /* We're async whenever the serial device is. */
b84876c2 7377 return remote_async_mask_value && serial_can_async_p (remote_desc);
6426a772
JM
7378}
7379
7380static int
7381remote_is_async_p (void)
7382{
23860348 7383 /* We're async whenever the serial device is. */
b84876c2 7384 return remote_async_mask_value && serial_is_async_p (remote_desc);
6426a772
JM
7385}
7386
2acceee2
JM
7387/* Pass the SERIAL event on and up to the client. One day this code
7388 will be able to delay notifying the client of an event until the
23860348 7389 point where an entire packet has been received. */
2acceee2 7390
2bc416ba 7391static void (*async_client_callback) (enum inferior_event_type event_type,
23860348 7392 void *context);
2acceee2
JM
7393static void *async_client_context;
7394static serial_event_ftype remote_async_serial_handler;
7395
6426a772 7396static void
819cc324 7397remote_async_serial_handler (struct serial *scb, void *context)
6426a772 7398{
2acceee2
JM
7399 /* Don't propogate error information up to the client. Instead let
7400 the client find out about the error by querying the target. */
7401 async_client_callback (INF_REG_EVENT, async_client_context);
7402}
7403
7404static void
2bc416ba 7405remote_async (void (*callback) (enum inferior_event_type event_type,
23860348 7406 void *context), void *context)
2acceee2 7407{
b84876c2 7408 if (remote_async_mask_value == 0)
8e65ff28 7409 internal_error (__FILE__, __LINE__,
e2e0b3e5 7410 _("Calling remote_async when async is masked"));
ed9a39eb 7411
2acceee2
JM
7412 if (callback != NULL)
7413 {
2cd58942 7414 serial_async (remote_desc, remote_async_serial_handler, NULL);
2acceee2
JM
7415 async_client_callback = callback;
7416 async_client_context = context;
7417 }
7418 else
2cd58942 7419 serial_async (remote_desc, NULL, NULL);
6426a772
JM
7420}
7421
b84876c2
PA
7422static int
7423remote_async_mask (int new_mask)
7424{
7425 int curr_mask = remote_async_mask_value;
7426 remote_async_mask_value = new_mask;
7427 return curr_mask;
7428}
7429
43ff13b4
JM
7430/* Target async and target extended-async.
7431
7432 This are temporary targets, until it is all tested. Eventually
7433 async support will be incorporated int the usual 'remote'
23860348 7434 target. */
43ff13b4
JM
7435
7436static void
c2d11a7d 7437init_remote_async_ops (void)
43ff13b4
JM
7438{
7439 remote_async_ops.to_shortname = "async";
2bc416ba 7440 remote_async_ops.to_longname =
23860348 7441 "Remote serial target in async version of the gdb-specific protocol";
c5aa993b 7442 remote_async_ops.to_doc =
43ff13b4
JM
7443 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
7444Specify the serial device it is connected to (e.g. /dev/ttya).";
c5aa993b
JM
7445 remote_async_ops.to_open = remote_async_open;
7446 remote_async_ops.to_close = remote_close;
6ad8ae5c
DJ
7447 remote_async_ops.to_detach = remote_detach;
7448 remote_async_ops.to_disconnect = remote_disconnect;
c5aa993b
JM
7449 remote_async_ops.to_resume = remote_async_resume;
7450 remote_async_ops.to_wait = remote_async_wait;
7451 remote_async_ops.to_fetch_registers = remote_fetch_registers;
7452 remote_async_ops.to_store_registers = remote_store_registers;
7453 remote_async_ops.to_prepare_to_store = remote_prepare_to_store;
c8e73a31 7454 remote_async_ops.deprecated_xfer_memory = remote_xfer_memory;
c5aa993b 7455 remote_async_ops.to_files_info = remote_files_info;
43ff13b4
JM
7456 remote_async_ops.to_insert_breakpoint = remote_insert_breakpoint;
7457 remote_async_ops.to_remove_breakpoint = remote_remove_breakpoint;
3c3bea1c
GS
7458 remote_async_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
7459 remote_async_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
7460 remote_async_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
7461 remote_async_ops.to_insert_watchpoint = remote_insert_watchpoint;
7462 remote_async_ops.to_remove_watchpoint = remote_remove_watchpoint;
7463 remote_async_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
7464 remote_async_ops.to_stopped_data_address = remote_stopped_data_address;
6426a772
JM
7465 remote_async_ops.to_terminal_inferior = remote_async_terminal_inferior;
7466 remote_async_ops.to_terminal_ours = remote_async_terminal_ours;
c5aa993b
JM
7467 remote_async_ops.to_kill = remote_async_kill;
7468 remote_async_ops.to_load = generic_load;
53a5351d 7469 remote_async_ops.to_mourn_inferior = remote_async_mourn;
c5aa993b
JM
7470 remote_async_ops.to_thread_alive = remote_thread_alive;
7471 remote_async_ops.to_find_new_threads = remote_threads_info;
cf759d3b
ND
7472 remote_async_ops.to_pid_to_str = remote_pid_to_str;
7473 remote_async_ops.to_extra_thread_info = remote_threads_extra_info;
43ff13b4 7474 remote_async_ops.to_stop = remote_stop;
4b8a223f 7475 remote_async_ops.to_xfer_partial = remote_xfer_partial;
96baa820 7476 remote_async_ops.to_rcmd = remote_rcmd;
32c1e744
VP
7477 remote_async_ops.to_get_thread_local_address
7478 = remote_get_thread_local_address;
c5aa993b
JM
7479 remote_async_ops.to_stratum = process_stratum;
7480 remote_async_ops.to_has_all_memory = 1;
7481 remote_async_ops.to_has_memory = 1;
7482 remote_async_ops.to_has_stack = 1;
7483 remote_async_ops.to_has_registers = 1;
7484 remote_async_ops.to_has_execution = 1;
7485 remote_async_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
6426a772
JM
7486 remote_async_ops.to_can_async_p = remote_can_async_p;
7487 remote_async_ops.to_is_async_p = remote_is_async_p;
7488 remote_async_ops.to_async = remote_async;
b84876c2 7489 remote_async_ops.to_async_mask = remote_async_mask;
c5aa993b 7490 remote_async_ops.to_magic = OPS_MAGIC;
fd79ecee 7491 remote_async_ops.to_memory_map = remote_memory_map;
a76d924d
DJ
7492 remote_async_ops.to_flash_erase = remote_flash_erase;
7493 remote_async_ops.to_flash_done = remote_flash_done;
cfa9d6d9 7494 remote_async_ops.to_read_description = remote_read_description;
08388c79 7495 remote_async_ops.to_search_memory = remote_search_memory;
43ff13b4
JM
7496}
7497
7498/* Set up the async extended remote vector by making a copy of the standard
7499 remote vector and adding to it. */
7500
7501static void
c2d11a7d 7502init_extended_async_remote_ops (void)
43ff13b4
JM
7503{
7504 extended_async_remote_ops = remote_async_ops;
7505
7506 extended_async_remote_ops.to_shortname = "extended-async";
c5aa993b 7507 extended_async_remote_ops.to_longname =
43ff13b4 7508 "Extended remote serial target in async gdb-specific protocol";
c5aa993b 7509 extended_async_remote_ops.to_doc =
43ff13b4
JM
7510 "Use a remote computer via a serial line, using an async gdb-specific protocol.\n\
7511Specify the serial device it is connected to (e.g. /dev/ttya).",
c5aa993b 7512 extended_async_remote_ops.to_open = extended_remote_async_open;
43ff13b4 7513 extended_async_remote_ops.to_create_inferior = extended_remote_async_create_inferior;
2d717e4f
DJ
7514 extended_async_remote_ops.to_mourn_inferior = extended_async_remote_mourn;
7515 extended_async_remote_ops.to_detach = extended_remote_detach;
7516 extended_async_remote_ops.to_attach = extended_async_remote_attach;
43ff13b4
JM
7517}
7518
5a2468f5 7519static void
c2d11a7d 7520set_remote_cmd (char *args, int from_tty)
5a2468f5 7521{
427c3a89 7522 help_list (remote_set_cmdlist, "set remote ", -1, gdb_stdout);
5a2468f5
JM
7523}
7524
d471ea57
AC
7525static void
7526show_remote_cmd (char *args, int from_tty)
7527{
37a105a1 7528 /* We can't just use cmd_show_list here, because we want to skip
427c3a89 7529 the redundant "show remote Z-packet" and the legacy aliases. */
37a105a1
DJ
7530 struct cleanup *showlist_chain;
7531 struct cmd_list_element *list = remote_show_cmdlist;
7532
7533 showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist");
7534 for (; list != NULL; list = list->next)
7535 if (strcmp (list->name, "Z-packet") == 0)
7536 continue;
427c3a89
DJ
7537 else if (list->type == not_set_cmd)
7538 /* Alias commands are exactly like the original, except they
7539 don't have the normal type. */
7540 continue;
7541 else
37a105a1
DJ
7542 {
7543 struct cleanup *option_chain
7544 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
7545 ui_out_field_string (uiout, "name", list->name);
7546 ui_out_text (uiout, ": ");
427c3a89
DJ
7547 if (list->type == show_cmd)
7548 do_setshow_command ((char *) NULL, from_tty, list);
7549 else
7550 cmd_func (list, NULL, from_tty);
37a105a1
DJ
7551 /* Close the tuple. */
7552 do_cleanups (option_chain);
7553 }
427c3a89
DJ
7554
7555 /* Close the tuple. */
7556 do_cleanups (showlist_chain);
d471ea57 7557}
5a2468f5 7558
0f71a2f6 7559
23860348 7560/* Function to be called whenever a new objfile (shlib) is detected. */
dc8acb97
MS
7561static void
7562remote_new_objfile (struct objfile *objfile)
7563{
23860348 7564 if (remote_desc != 0) /* Have a remote connection. */
06d3b283 7565 remote_check_symbols (objfile);
dc8acb97
MS
7566}
7567
c906108c 7568void
fba45db2 7569_initialize_remote (void)
c906108c 7570{
ea9c271d
DJ
7571 struct remote_state *rs;
7572
0f71a2f6 7573 /* architecture specific data */
2bc416ba 7574 remote_gdbarch_data_handle =
23860348 7575 gdbarch_data_register_post_init (init_remote_state);
29709017
DJ
7576 remote_g_packet_data_handle =
7577 gdbarch_data_register_pre_init (remote_g_packet_data_init);
d01949b6 7578
ea9c271d
DJ
7579 /* Initialize the per-target state. At the moment there is only one
7580 of these, not one per target. Only one target is active at a
7581 time. The default buffer size is unimportant; it will be expanded
7582 whenever a larger buffer is needed. */
0b83947e 7583 rs = get_remote_state_raw ();
ea9c271d
DJ
7584 rs->buf_size = 400;
7585 rs->buf = xmalloc (rs->buf_size);
7586
c906108c
SS
7587 init_remote_ops ();
7588 add_target (&remote_ops);
7589
7590 init_extended_remote_ops ();
7591 add_target (&extended_remote_ops);
cce74817 7592
43ff13b4
JM
7593 init_remote_async_ops ();
7594 add_target (&remote_async_ops);
7595
7596 init_extended_async_remote_ops ();
7597 add_target (&extended_async_remote_ops);
7598
dc8acb97 7599 /* Hook into new objfile notification. */
06d3b283 7600 observer_attach_new_objfile (remote_new_objfile);
dc8acb97 7601
b803fb0f
DJ
7602 /* Set up signal handlers. */
7603 sigint_remote_token =
7604 create_async_signal_handler (async_remote_interrupt, NULL);
7605 sigint_remote_twice_token =
7606 create_async_signal_handler (inferior_event_handler_wrapper, NULL);
7607
c906108c
SS
7608#if 0
7609 init_remote_threadtests ();
7610#endif
7611
23860348 7612 /* set/show remote ... */
d471ea57 7613
1bedd215 7614 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, _("\
5a2468f5
JM
7615Remote protocol specific variables\n\
7616Configure various remote-protocol specific variables such as\n\
1bedd215 7617the packets being used"),
cff3e48b 7618 &remote_set_cmdlist, "set remote ",
23860348 7619 0 /* allow-unknown */, &setlist);
1bedd215 7620 add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\
5a2468f5
JM
7621Remote protocol specific variables\n\
7622Configure various remote-protocol specific variables such as\n\
1bedd215 7623the packets being used"),
cff3e48b 7624 &remote_show_cmdlist, "show remote ",
23860348 7625 0 /* allow-unknown */, &showlist);
5a2468f5 7626
1a966eab
AC
7627 add_cmd ("compare-sections", class_obscure, compare_sections_command, _("\
7628Compare section data on target to the exec file.\n\
7629Argument is a single section name (default: all loaded sections)."),
c906108c
SS
7630 &cmdlist);
7631
1a966eab
AC
7632 add_cmd ("packet", class_maintenance, packet_command, _("\
7633Send an arbitrary packet to a remote target.\n\
c906108c
SS
7634 maintenance packet TEXT\n\
7635If GDB is talking to an inferior via the GDB serial protocol, then\n\
7636this command sends the string TEXT to the inferior, and displays the\n\
7637response packet. GDB supplies the initial `$' character, and the\n\
1a966eab 7638terminating `#' character and checksum."),
c906108c
SS
7639 &maintenancelist);
7640
7915a72c
AC
7641 add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, _("\
7642Set whether to send break if interrupted."), _("\
7643Show whether to send break if interrupted."), _("\
7644If set, a break, instead of a cntrl-c, is sent to the remote target."),
2c5b56ce 7645 NULL, NULL, /* FIXME: i18n: Whether to send break if interrupted is %s. */
e707bbc2 7646 &setlist, &showlist);
c906108c 7647
23860348 7648 /* Install commands for configuring memory read/write packets. */
11cf8741 7649
1a966eab
AC
7650 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size, _("\
7651Set the maximum number of bytes per memory write packet (deprecated)."),
11cf8741 7652 &setlist);
1a966eab
AC
7653 add_cmd ("remotewritesize", no_class, show_memory_write_packet_size, _("\
7654Show the maximum number of bytes per memory write packet (deprecated)."),
11cf8741
JM
7655 &showlist);
7656 add_cmd ("memory-write-packet-size", no_class,
1a966eab
AC
7657 set_memory_write_packet_size, _("\
7658Set the maximum number of bytes per memory-write packet.\n\
7659Specify the number of bytes in a packet or 0 (zero) for the\n\
7660default packet size. The actual limit is further reduced\n\
7661dependent on the target. Specify ``fixed'' to disable the\n\
7662further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
7663 &remote_set_cmdlist);
7664 add_cmd ("memory-read-packet-size", no_class,
1a966eab
AC
7665 set_memory_read_packet_size, _("\
7666Set the maximum number of bytes per memory-read packet.\n\
7667Specify the number of bytes in a packet or 0 (zero) for the\n\
7668default packet size. The actual limit is further reduced\n\
7669dependent on the target. Specify ``fixed'' to disable the\n\
7670further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
7671 &remote_set_cmdlist);
7672 add_cmd ("memory-write-packet-size", no_class,
7673 show_memory_write_packet_size,
1a966eab 7674 _("Show the maximum number of bytes per memory-write packet."),
11cf8741
JM
7675 &remote_show_cmdlist);
7676 add_cmd ("memory-read-packet-size", no_class,
7677 show_memory_read_packet_size,
1a966eab 7678 _("Show the maximum number of bytes per memory-read packet."),
11cf8741 7679 &remote_show_cmdlist);
c906108c 7680
b3f42336 7681 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class,
7915a72c
AC
7682 &remote_hw_watchpoint_limit, _("\
7683Set the maximum number of target hardware watchpoints."), _("\
7684Show the maximum number of target hardware watchpoints."), _("\
7685Specify a negative limit for unlimited."),
2c5b56ce 7686 NULL, NULL, /* FIXME: i18n: The maximum number of target hardware watchpoints is %s. */
b3f42336
AC
7687 &remote_set_cmdlist, &remote_show_cmdlist);
7688 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class,
7915a72c
AC
7689 &remote_hw_breakpoint_limit, _("\
7690Set the maximum number of target hardware breakpoints."), _("\
7691Show the maximum number of target hardware breakpoints."), _("\
7692Specify a negative limit for unlimited."),
2c5b56ce 7693 NULL, NULL, /* FIXME: i18n: The maximum number of target hardware breakpoints is %s. */
b3f42336 7694 &remote_set_cmdlist, &remote_show_cmdlist);
501eef12 7695
4d28ad1e
AC
7696 add_setshow_integer_cmd ("remoteaddresssize", class_obscure,
7697 &remote_address_size, _("\
7698Set the maximum size of the address (in bits) in a memory packet."), _("\
7699Show the maximum size of the address (in bits) in a memory packet."), NULL,
7700 NULL,
7701 NULL, /* FIXME: i18n: */
7702 &setlist, &showlist);
c906108c 7703
444abaca 7704 add_packet_config_cmd (&remote_protocol_packets[PACKET_X],
bb572ddd 7705 "X", "binary-download", 1);
0f71a2f6 7706
444abaca 7707 add_packet_config_cmd (&remote_protocol_packets[PACKET_vCont],
bb572ddd 7708 "vCont", "verbose-resume", 0);
506fb367 7709
89be2091
DJ
7710 add_packet_config_cmd (&remote_protocol_packets[PACKET_QPassSignals],
7711 "QPassSignals", "pass-signals", 0);
7712
444abaca 7713 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
bb572ddd 7714 "qSymbol", "symbol-lookup", 0);
dc8acb97 7715
444abaca 7716 add_packet_config_cmd (&remote_protocol_packets[PACKET_P],
bb572ddd 7717 "P", "set-register", 1);
d471ea57 7718
444abaca 7719 add_packet_config_cmd (&remote_protocol_packets[PACKET_p],
bb572ddd 7720 "p", "fetch-register", 1);
b96ec7ac 7721
444abaca 7722 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z0],
bb572ddd 7723 "Z0", "software-breakpoint", 0);
d471ea57 7724
444abaca 7725 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z1],
bb572ddd 7726 "Z1", "hardware-breakpoint", 0);
d471ea57 7727
444abaca 7728 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z2],
bb572ddd 7729 "Z2", "write-watchpoint", 0);
d471ea57 7730
444abaca 7731 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z3],
bb572ddd 7732 "Z3", "read-watchpoint", 0);
d471ea57 7733
444abaca 7734 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z4],
bb572ddd 7735 "Z4", "access-watchpoint", 0);
d471ea57 7736
0876f84a
DJ
7737 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_auxv],
7738 "qXfer:auxv:read", "read-aux-vector", 0);
802188a7 7739
23181151
DJ
7740 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_features],
7741 "qXfer:features:read", "target-features", 0);
7742
cfa9d6d9
DJ
7743 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries],
7744 "qXfer:libraries:read", "library-info", 0);
7745
fd79ecee
DJ
7746 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_memory_map],
7747 "qXfer:memory-map:read", "memory-map", 0);
7748
0e7f50da
UW
7749 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_read],
7750 "qXfer:spu:read", "read-spu-object", 0);
7751
7752 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_write],
7753 "qXfer:spu:write", "write-spu-object", 0);
7754
444abaca 7755 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTLSAddr],
38691318 7756 "qGetTLSAddr", "get-thread-local-storage-address",
38691318
KB
7757 0);
7758
be2a5f71
DJ
7759 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSupported],
7760 "qSupported", "supported-packets", 0);
7761
08388c79
DE
7762 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSearch_memory],
7763 "qSearch:memory", "search-memory", 0);
7764
a6b151f1
DJ
7765 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_open],
7766 "vFile:open", "hostio-open", 0);
7767
7768 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pread],
7769 "vFile:pread", "hostio-pread", 0);
7770
7771 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pwrite],
7772 "vFile:pwrite", "hostio-pwrite", 0);
7773
7774 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_close],
7775 "vFile:close", "hostio-close", 0);
7776
7777 add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_unlink],
7778 "vFile:unlink", "hostio-unlink", 0);
7779
2d717e4f
DJ
7780 add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach],
7781 "vAttach", "attach", 0);
7782
7783 add_packet_config_cmd (&remote_protocol_packets[PACKET_vRun],
7784 "vRun", "run", 0);
7785
37a105a1
DJ
7786 /* Keep the old ``set remote Z-packet ...'' working. Each individual
7787 Z sub-packet has its own set and show commands, but users may
7788 have sets to this variable in their .gdbinit files (or in their
7789 documentation). */
e9e68a56 7790 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure,
7915a72c
AC
7791 &remote_Z_packet_detect, _("\
7792Set use of remote protocol `Z' packets"), _("\
7793Show use of remote protocol `Z' packets "), _("\
3b64bf98 7794When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
7915a72c 7795packets."),
e9e68a56 7796 set_remote_protocol_Z_packet_cmd,
2c5b56ce 7797 show_remote_protocol_Z_packet_cmd, /* FIXME: i18n: Use of remote protocol `Z' packets is %s. */
e9e68a56 7798 &remote_set_cmdlist, &remote_show_cmdlist);
449092f6 7799
a6b151f1
DJ
7800 add_prefix_cmd ("remote", class_files, remote_command, _("\
7801Manipulate files on the remote system\n\
7802Transfer files to and from the remote target system."),
7803 &remote_cmdlist, "remote ",
7804 0 /* allow-unknown */, &cmdlist);
7805
7806 add_cmd ("put", class_files, remote_put_command,
7807 _("Copy a local file to the remote system."),
7808 &remote_cmdlist);
7809
7810 add_cmd ("get", class_files, remote_get_command,
7811 _("Copy a remote file to the local system."),
7812 &remote_cmdlist);
7813
7814 add_cmd ("delete", class_files, remote_delete_command,
7815 _("Delete a remote file."),
7816 &remote_cmdlist);
7817
2d717e4f
DJ
7818 remote_exec_file = xstrdup ("");
7819 add_setshow_string_noescape_cmd ("exec-file", class_files,
7820 &remote_exec_file, _("\
7821Set the remote pathname for \"run\""), _("\
7822Show the remote pathname for \"run\""), NULL, NULL, NULL,
7823 &remote_set_cmdlist, &remote_show_cmdlist);
7824
449092f6
CV
7825 /* Eventually initialize fileio. See fileio.c */
7826 initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist);
c906108c 7827}
This page took 1.325019 seconds and 4 git commands to generate.