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