gdb/
[deliverable/binutils-gdb.git] / gdb / remote.c
CommitLineData
c906108c 1/* Remote target communications for serial-line targets in custom GDB protocol
8926118c 2
197e01b6 3 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
24b06219 4 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
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
11 the Free Software Foundation; either version 2 of the License, or
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
20 along with this program; if not, write to the Free Software
197e01b6
EZ
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
c5aa993b 23
23860348 24/* See the GDB User Guide for details of the GDB remote protocol. */
c5aa993b 25
c906108c
SS
26#include "defs.h"
27#include "gdb_string.h"
28#include <ctype.h>
29#include <fcntl.h>
c906108c
SS
30#include "inferior.h"
31#include "bfd.h"
32#include "symfile.h"
60250e8b 33#include "exceptions.h"
c906108c 34#include "target.h"
c5aa993b 35/*#include "terminal.h" */
c906108c
SS
36#include "gdbcmd.h"
37#include "objfiles.h"
38#include "gdb-stabs.h"
39#include "gdbthread.h"
c2c6d25f 40#include "remote.h"
4e052eda 41#include "regcache.h"
fd0407d6 42#include "value.h"
1ff9c3d6 43#include "gdb_assert.h"
6867ae3e 44#include "observer.h"
a77053c2 45#include "solib.h"
37a105a1
DJ
46#include "cli/cli-decode.h"
47#include "cli/cli-setshow.h"
c906108c 48
7a292a7a 49#include <ctype.h>
9846de1b 50#include <sys/time.h>
c906108c 51
43ff13b4 52#include "event-loop.h"
c2c6d25f 53#include "event-top.h"
2acceee2 54#include "inf-loop.h"
43ff13b4 55
c906108c
SS
56#include <signal.h>
57#include "serial.h"
58
6240bebf
MS
59#include "gdbcore.h" /* for exec_bfd */
60
449092f6
CV
61#include "remote-fileio.h"
62
23860348 63/* Prototypes for local functions. */
6426a772
JM
64static void cleanup_sigint_signal_handler (void *dummy);
65static void initialize_sigint_signal_handler (void);
6d820c5c 66static int getpkt_sane (char **buf, long *sizeof_buf, int forever);
6426a772 67
a14ed312
KB
68static void handle_remote_sigint (int);
69static void handle_remote_sigint_twice (int);
70static void async_remote_interrupt (gdb_client_data);
71void async_remote_interrupt_twice (gdb_client_data);
43ff13b4 72
a14ed312 73static void build_remote_gdbarch_data (void);
0f71a2f6 74
a14ed312 75static void remote_files_info (struct target_ops *ignore);
c906108c 76
a14ed312 77static void remote_prepare_to_store (void);
c906108c 78
a14ed312 79static void remote_fetch_registers (int regno);
c906108c 80
39f77062
KB
81static void remote_resume (ptid_t ptid, int step,
82 enum target_signal siggnal);
83static void remote_async_resume (ptid_t ptid, int step,
a14ed312 84 enum target_signal siggnal);
a14ed312
KB
85static void remote_open (char *name, int from_tty);
86static void remote_async_open (char *name, int from_tty);
c906108c 87
a14ed312
KB
88static void extended_remote_open (char *name, int from_tty);
89static void extended_remote_async_open (char *name, int from_tty);
c906108c 90
92d1e331
DJ
91static void remote_open_1 (char *, int, struct target_ops *, int extended_p,
92 int async_p);
c906108c 93
a14ed312 94static void remote_close (int quitting);
c906108c 95
a14ed312 96static void remote_store_registers (int regno);
c906108c 97
a14ed312
KB
98static void remote_mourn (void);
99static void remote_async_mourn (void);
c906108c 100
a14ed312 101static void extended_remote_restart (void);
c906108c 102
a14ed312 103static void extended_remote_mourn (void);
c906108c 104
a14ed312 105static void remote_mourn_1 (struct target_ops *);
c906108c 106
6d820c5c 107static void remote_send (char **buf, long *sizeof_buf_p);
c906108c 108
a14ed312 109static int readchar (int timeout);
c906108c 110
39f77062
KB
111static ptid_t remote_wait (ptid_t ptid,
112 struct target_waitstatus *status);
113static ptid_t remote_async_wait (ptid_t ptid,
114 struct target_waitstatus *status);
c906108c 115
a14ed312
KB
116static void remote_kill (void);
117static void remote_async_kill (void);
c906108c 118
a14ed312 119static int tohex (int nib);
c906108c 120
a14ed312 121static void remote_detach (char *args, int from_tty);
c906108c 122
a14ed312 123static void remote_interrupt (int signo);
c906108c 124
a14ed312 125static void remote_interrupt_twice (int signo);
7a292a7a 126
a14ed312 127static void interrupt_query (void);
c906108c 128
a14ed312 129static void set_thread (int, int);
c906108c 130
39f77062 131static int remote_thread_alive (ptid_t);
c906108c 132
a14ed312 133static void get_offsets (void);
c906108c 134
6d820c5c
DJ
135static void skip_frame (void);
136
137static long read_frame (char **buf_p, long *sizeof_buf);
c906108c 138
a14ed312 139static int hexnumlen (ULONGEST num);
c906108c 140
a14ed312 141static void init_remote_ops (void);
c906108c 142
a14ed312 143static void init_extended_remote_ops (void);
c906108c 144
a14ed312 145static void remote_stop (void);
c906108c 146
a14ed312 147static int ishex (int ch, int *val);
c906108c 148
a14ed312 149static int stubhex (int ch);
c906108c 150
a14ed312 151static int hexnumstr (char *, ULONGEST);
c906108c 152
a14ed312 153static int hexnumnstr (char *, ULONGEST, int);
2df3850c 154
a14ed312 155static CORE_ADDR remote_address_masked (CORE_ADDR);
c906108c 156
a14ed312 157static void print_packet (char *);
c906108c 158
a14ed312 159static unsigned long crc32 (unsigned char *, int, unsigned int);
c906108c 160
a14ed312 161static void compare_sections_command (char *, int);
c906108c 162
a14ed312 163static void packet_command (char *, int);
c906108c 164
a14ed312 165static int stub_unpack_int (char *buff, int fieldlength);
c906108c 166
39f77062 167static ptid_t remote_current_thread (ptid_t oldptid);
c906108c 168
a14ed312 169static void remote_find_new_threads (void);
c906108c 170
a14ed312 171static void record_currthread (int currthread);
c906108c 172
30559e10 173static int fromhex (int a);
c906108c 174
cfd77fa1 175static int hex2bin (const char *hex, gdb_byte *bin, int count);
c906108c 176
cfd77fa1 177static int bin2hex (const gdb_byte *bin, char *hex, int count);
234fa6d1 178
a14ed312 179static int putpkt_binary (char *buf, int cnt);
c906108c 180
a14ed312 181static void check_binary_download (CORE_ADDR addr);
c906108c 182
5a2468f5 183struct packet_config;
5a2468f5 184
a14ed312 185static void show_packet_config_cmd (struct packet_config *config);
5a2468f5 186
d471ea57 187static void update_packet_config (struct packet_config *config);
5a2468f5 188
bb572ddd
DJ
189static void set_remote_protocol_packet_cmd (char *args, int from_tty,
190 struct cmd_list_element *c);
191
192static void show_remote_protocol_packet_cmd (struct ui_file *file,
193 int from_tty,
194 struct cmd_list_element *c,
195 const char *value);
196
a14ed312 197void _initialize_remote (void);
c906108c 198
bb572ddd
DJ
199/* For "set remote" and "show remote". */
200
201static struct cmd_list_element *remote_set_cmdlist;
202static struct cmd_list_element *remote_show_cmdlist;
203
ea9c271d
DJ
204/* Description of the remote protocol state for the currently
205 connected target. This is per-target state, and independent of the
206 selected architecture. */
207
208struct remote_state
209{
210 /* A buffer to use for incoming packets, and its current size. The
211 buffer is grown dynamically for larger incoming packets.
212 Outgoing packets may also be constructed in this buffer.
213 BUF_SIZE is always at least REMOTE_PACKET_SIZE;
214 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
215 packets. */
216 char *buf;
217 long buf_size;
be2a5f71
DJ
218
219 /* If we negotiated packet size explicitly (and thus can bypass
220 heuristics for the largest packet size that will not overflow
221 a buffer in the stub), this will be set to that packet size.
222 Otherwise zero, meaning to use the guessed size. */
223 long explicit_packet_size;
ea9c271d
DJ
224};
225
226/* This data could be associated with a target, but we do not always
227 have access to the current target when we need it, so for now it is
228 static. This will be fine for as long as only one target is in use
229 at a time. */
230static struct remote_state remote_state;
231
232static struct remote_state *
233get_remote_state (void)
234{
235 return &remote_state;
236}
237
238/* Description of the remote protocol for a given architecture. */
d01949b6 239
ad10f812
AC
240struct packet_reg
241{
242 long offset; /* Offset into G packet. */
243 long regnum; /* GDB's internal register number. */
244 LONGEST pnum; /* Remote protocol register number. */
b323314b 245 int in_g_packet; /* Always part of G packet. */
23860348
MS
246 /* long size in bytes; == register_size (current_gdbarch, regnum);
247 at present. */
ad10f812
AC
248 /* char *name; == REGISTER_NAME (regnum); at present. */
249};
250
ea9c271d 251struct remote_arch_state
d01949b6 252{
ad10f812
AC
253 /* Description of the remote protocol registers. */
254 long sizeof_g_packet;
b323314b
AC
255
256 /* Description of the remote protocol registers indexed by REGNUM
257 (making an array of NUM_REGS + NUM_PSEUDO_REGS in size). */
258 struct packet_reg *regs;
ad10f812 259
d01949b6
AC
260 /* This is the size (in chars) of the first response to the ``g''
261 packet. It is used as a heuristic when determining the maximum
262 size of memory-read and memory-write packets. A target will
263 typically only reserve a buffer large enough to hold the ``g''
264 packet. The size does not include packet overhead (headers and
23860348 265 trailers). */
d01949b6
AC
266 long actual_register_packet_size;
267
268 /* This is the maximum size (in chars) of a non read/write packet.
23860348 269 It is also used as a cap on the size of read/write packets. */
d01949b6
AC
270 long remote_packet_size;
271};
272
3c3bea1c 273
d01949b6
AC
274/* Handle for retreving the remote protocol data from gdbarch. */
275static struct gdbarch_data *remote_gdbarch_data_handle;
276
ea9c271d
DJ
277static struct remote_arch_state *
278get_remote_arch_state (void)
d01949b6 279{
451fbdda 280 return gdbarch_data (current_gdbarch, remote_gdbarch_data_handle);
d01949b6
AC
281}
282
283static void *
284init_remote_state (struct gdbarch *gdbarch)
285{
286 int regnum;
ea9c271d
DJ
287 struct remote_state *rs = get_remote_state ();
288 struct remote_arch_state *rsa;
289
290 rsa = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct remote_arch_state);
d01949b6 291
ea9c271d 292 rsa->sizeof_g_packet = 0;
ad10f812 293
b323314b 294 /* Assume a 1:1 regnum<->pnum table. */
ea9c271d
DJ
295 rsa->regs = GDBARCH_OBSTACK_CALLOC (gdbarch, NUM_REGS + NUM_PSEUDO_REGS,
296 struct packet_reg);
b323314b 297 for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
ad10f812 298 {
ea9c271d 299 struct packet_reg *r = &rsa->regs[regnum];
b323314b
AC
300 r->pnum = regnum;
301 r->regnum = regnum;
62700349 302 r->offset = DEPRECATED_REGISTER_BYTE (regnum);
b323314b 303 r->in_g_packet = (regnum < NUM_REGS);
ad10f812 304 /* ...name = REGISTER_NAME (regnum); */
7d58c67d 305
23860348 306 /* Compute packet size by accumulating the size of all registers. */
46845f5e 307 if (regnum < NUM_REGS)
ea9c271d 308 rsa->sizeof_g_packet += register_size (current_gdbarch, regnum);
ad10f812
AC
309 }
310
d01949b6
AC
311 /* Default maximum number of characters in a packet body. Many
312 remote stubs have a hardwired buffer size of 400 bytes
313 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
314 as the maximum packet-size to ensure that the packet and an extra
315 NUL character can always fit in the buffer. This stops GDB
316 trashing stubs that try to squeeze an extra NUL into what is
ea9c271d
DJ
317 already a full buffer (As of 1999-12-04 that was most stubs). */
318 rsa->remote_packet_size = 400 - 1;
d01949b6 319
ea9c271d
DJ
320 /* This one is filled in when a ``g'' packet is received. */
321 rsa->actual_register_packet_size = 0;
322
323 /* Should rsa->sizeof_g_packet needs more space than the
ad10f812
AC
324 default, adjust the size accordingly. Remember that each byte is
325 encoded as two characters. 32 is the overhead for the packet
326 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
d01949b6 327 (``$NN:G...#NN'') is a better guess, the below has been padded a
23860348 328 little. */
ea9c271d
DJ
329 if (rsa->sizeof_g_packet > ((rsa->remote_packet_size - 32) / 2))
330 rsa->remote_packet_size = (rsa->sizeof_g_packet * 2 + 32);
802188a7 331
ea9c271d
DJ
332 /* Make sure that the packet buffer is plenty big enough for
333 this architecture. */
334 if (rs->buf_size < rsa->remote_packet_size)
335 {
336 rs->buf_size = 2 * rsa->remote_packet_size;
7fca722e 337 rs->buf = xrealloc (rs->buf, rs->buf_size);
ea9c271d 338 }
6d820c5c 339
ea9c271d
DJ
340 return rsa;
341}
342
343/* Return the current allowed size of a remote packet. This is
344 inferred from the current architecture, and should be used to
345 limit the length of outgoing packets. */
346static long
347get_remote_packet_size (void)
348{
be2a5f71 349 struct remote_state *rs = get_remote_state ();
ea9c271d
DJ
350 struct remote_arch_state *rsa = get_remote_arch_state ();
351
be2a5f71
DJ
352 if (rs->explicit_packet_size)
353 return rs->explicit_packet_size;
354
ea9c271d 355 return rsa->remote_packet_size;
d01949b6
AC
356}
357
ad10f812 358static struct packet_reg *
ea9c271d 359packet_reg_from_regnum (struct remote_arch_state *rsa, long regnum)
ad10f812 360{
b323314b
AC
361 if (regnum < 0 && regnum >= NUM_REGS + NUM_PSEUDO_REGS)
362 return NULL;
363 else
ad10f812 364 {
ea9c271d 365 struct packet_reg *r = &rsa->regs[regnum];
b323314b
AC
366 gdb_assert (r->regnum == regnum);
367 return r;
ad10f812 368 }
ad10f812
AC
369}
370
371static struct packet_reg *
ea9c271d 372packet_reg_from_pnum (struct remote_arch_state *rsa, LONGEST pnum)
ad10f812 373{
b323314b
AC
374 int i;
375 for (i = 0; i < NUM_REGS + NUM_PSEUDO_REGS; i++)
ad10f812 376 {
ea9c271d 377 struct packet_reg *r = &rsa->regs[i];
b323314b
AC
378 if (r->pnum == pnum)
379 return r;
ad10f812
AC
380 }
381 return NULL;
d01949b6
AC
382}
383
3c3bea1c
GS
384/* FIXME: graces/2002-08-08: These variables should eventually be
385 bound to an instance of the target object (as in gdbarch-tdep()),
386 when such a thing exists. */
387
388/* This is set to the data address of the access causing the target
389 to stop for a watchpoint. */
390static CORE_ADDR remote_watch_data_address;
391
94e08568 392/* This is non-zero if target stopped for a watchpoint. */
3c3bea1c
GS
393static int remote_stopped_by_watchpoint_p;
394
c906108c
SS
395static struct target_ops remote_ops;
396
397static struct target_ops extended_remote_ops;
398
43ff13b4 399/* Temporary target ops. Just like the remote_ops and
23860348 400 extended_remote_ops, but with asynchronous support. */
43ff13b4
JM
401static struct target_ops remote_async_ops;
402
403static struct target_ops extended_async_remote_ops;
404
6426a772
JM
405/* FIXME: cagney/1999-09-23: Even though getpkt was called with
406 ``forever'' still use the normal timeout mechanism. This is
407 currently used by the ASYNC code to guarentee that target reads
408 during the initial connect always time-out. Once getpkt has been
409 modified to return a timeout indication and, in turn
410 remote_wait()/wait_for_inferior() have gained a timeout parameter
23860348 411 this can go away. */
6426a772
JM
412static int wait_forever_enabled_p = 1;
413
414
c906108c
SS
415/* This variable chooses whether to send a ^C or a break when the user
416 requests program interruption. Although ^C is usually what remote
417 systems expect, and that is the default here, sometimes a break is
418 preferable instead. */
419
420static int remote_break;
421
c906108c
SS
422/* Descriptor for I/O to remote machine. Initialize it to NULL so that
423 remote_open knows that we don't have a file open when the program
424 starts. */
819cc324 425static struct serial *remote_desc = NULL;
c906108c 426
c906108c
SS
427/* This variable sets the number of bits in an address that are to be
428 sent in a memory ("M" or "m") packet. Normally, after stripping
429 leading zeros, the entire address would be sent. This variable
430 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
431 initial implementation of remote.c restricted the address sent in
432 memory packets to ``host::sizeof long'' bytes - (typically 32
433 bits). Consequently, for 64 bit targets, the upper 32 bits of an
434 address was never sent. Since fixing this bug may cause a break in
435 some remote targets this variable is principly provided to
23860348 436 facilitate backward compatibility. */
c906108c
SS
437
438static int remote_address_size;
439
6426a772
JM
440/* Tempoary to track who currently owns the terminal. See
441 target_async_terminal_* for more details. */
442
443static int remote_async_terminal_ours_p;
444
11cf8741 445\f
11cf8741 446/* User configurable variables for the number of characters in a
ea9c271d
DJ
447 memory read/write packet. MIN (rsa->remote_packet_size,
448 rsa->sizeof_g_packet) is the default. Some targets need smaller
24b06219 449 values (fifo overruns, et.al.) and some users need larger values
ad10f812
AC
450 (speed up transfers). The variables ``preferred_*'' (the user
451 request), ``current_*'' (what was actually set) and ``forced_*''
23860348 452 (Positive - a soft limit, negative - a hard limit). */
11cf8741
JM
453
454struct memory_packet_config
455{
456 char *name;
457 long size;
458 int fixed_p;
459};
460
461/* Compute the current size of a read/write packet. Since this makes
462 use of ``actual_register_packet_size'' the computation is dynamic. */
463
464static long
465get_memory_packet_size (struct memory_packet_config *config)
466{
d01949b6 467 struct remote_state *rs = get_remote_state ();
ea9c271d
DJ
468 struct remote_arch_state *rsa = get_remote_arch_state ();
469
11cf8741
JM
470 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
471 law?) that some hosts don't cope very well with large alloca()
472 calls. Eventually the alloca() code will be replaced by calls to
473 xmalloc() and make_cleanups() allowing this restriction to either
23860348 474 be lifted or removed. */
11cf8741
JM
475#ifndef MAX_REMOTE_PACKET_SIZE
476#define MAX_REMOTE_PACKET_SIZE 16384
477#endif
3de11b2e 478 /* NOTE: 20 ensures we can write at least one byte. */
11cf8741 479#ifndef MIN_REMOTE_PACKET_SIZE
3de11b2e 480#define MIN_REMOTE_PACKET_SIZE 20
11cf8741
JM
481#endif
482 long what_they_get;
483 if (config->fixed_p)
484 {
485 if (config->size <= 0)
486 what_they_get = MAX_REMOTE_PACKET_SIZE;
487 else
488 what_they_get = config->size;
489 }
490 else
491 {
ea9c271d 492 what_they_get = get_remote_packet_size ();
23860348 493 /* Limit the packet to the size specified by the user. */
11cf8741
JM
494 if (config->size > 0
495 && what_they_get > config->size)
496 what_they_get = config->size;
be2a5f71
DJ
497
498 /* Limit it to the size of the targets ``g'' response unless we have
499 permission from the stub to use a larger packet size. */
500 if (rs->explicit_packet_size == 0
501 && rsa->actual_register_packet_size > 0
502 && what_they_get > rsa->actual_register_packet_size)
503 what_they_get = rsa->actual_register_packet_size;
11cf8741
JM
504 }
505 if (what_they_get > MAX_REMOTE_PACKET_SIZE)
506 what_they_get = MAX_REMOTE_PACKET_SIZE;
507 if (what_they_get < MIN_REMOTE_PACKET_SIZE)
508 what_they_get = MIN_REMOTE_PACKET_SIZE;
6d820c5c
DJ
509
510 /* Make sure there is room in the global buffer for this packet
511 (including its trailing NUL byte). */
512 if (rs->buf_size < what_they_get + 1)
513 {
514 rs->buf_size = 2 * what_they_get;
515 rs->buf = xrealloc (rs->buf, 2 * what_they_get);
516 }
517
11cf8741
JM
518 return what_they_get;
519}
520
521/* Update the size of a read/write packet. If they user wants
23860348 522 something really big then do a sanity check. */
11cf8741
JM
523
524static void
525set_memory_packet_size (char *args, struct memory_packet_config *config)
526{
527 int fixed_p = config->fixed_p;
528 long size = config->size;
529 if (args == NULL)
8a3fe4f8 530 error (_("Argument required (integer, `fixed' or `limited')."));
11cf8741
JM
531 else if (strcmp (args, "hard") == 0
532 || strcmp (args, "fixed") == 0)
533 fixed_p = 1;
534 else if (strcmp (args, "soft") == 0
535 || strcmp (args, "limit") == 0)
536 fixed_p = 0;
537 else
538 {
539 char *end;
540 size = strtoul (args, &end, 0);
541 if (args == end)
8a3fe4f8 542 error (_("Invalid %s (bad syntax)."), config->name);
11cf8741
JM
543#if 0
544 /* Instead of explicitly capping the size of a packet to
545 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
546 instead allowed to set the size to something arbitrarily
23860348 547 large. */
11cf8741 548 if (size > MAX_REMOTE_PACKET_SIZE)
8a3fe4f8 549 error (_("Invalid %s (too large)."), config->name);
11cf8741
JM
550#endif
551 }
23860348 552 /* Extra checks? */
11cf8741
JM
553 if (fixed_p && !config->fixed_p)
554 {
e2e0b3e5
AC
555 if (! query (_("The target may not be able to correctly handle a %s\n"
556 "of %ld bytes. Change the packet size? "),
11cf8741 557 config->name, size))
8a3fe4f8 558 error (_("Packet size not changed."));
11cf8741 559 }
23860348 560 /* Update the config. */
11cf8741
JM
561 config->fixed_p = fixed_p;
562 config->size = size;
563}
564
565static void
566show_memory_packet_size (struct memory_packet_config *config)
567{
a3f17187 568 printf_filtered (_("The %s is %ld. "), config->name, config->size);
11cf8741 569 if (config->fixed_p)
a3f17187 570 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
11cf8741
JM
571 get_memory_packet_size (config));
572 else
a3f17187 573 printf_filtered (_("Packets are limited to %ld bytes.\n"),
11cf8741
JM
574 get_memory_packet_size (config));
575}
576
577static struct memory_packet_config memory_write_packet_config =
578{
579 "memory-write-packet-size",
580};
581
582static void
583set_memory_write_packet_size (char *args, int from_tty)
584{
585 set_memory_packet_size (args, &memory_write_packet_config);
586}
587
588static void
589show_memory_write_packet_size (char *args, int from_tty)
590{
591 show_memory_packet_size (&memory_write_packet_config);
592}
593
594static long
595get_memory_write_packet_size (void)
596{
597 return get_memory_packet_size (&memory_write_packet_config);
598}
599
600static struct memory_packet_config memory_read_packet_config =
601{
602 "memory-read-packet-size",
603};
604
605static void
606set_memory_read_packet_size (char *args, int from_tty)
607{
608 set_memory_packet_size (args, &memory_read_packet_config);
609}
610
611static void
612show_memory_read_packet_size (char *args, int from_tty)
613{
614 show_memory_packet_size (&memory_read_packet_config);
615}
616
617static long
618get_memory_read_packet_size (void)
619{
620 long size = get_memory_packet_size (&memory_read_packet_config);
621 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
622 extra buffer size argument before the memory read size can be
ea9c271d
DJ
623 increased beyond this. */
624 if (size > get_remote_packet_size ())
625 size = get_remote_packet_size ();
11cf8741
JM
626 return size;
627}
628
11cf8741 629\f
5a2468f5
JM
630/* Generic configuration support for packets the stub optionally
631 supports. Allows the user to specify the use of the packet as well
23860348 632 as allowing GDB to auto-detect support in the remote stub. */
5a2468f5
JM
633
634enum packet_support
635 {
636 PACKET_SUPPORT_UNKNOWN = 0,
637 PACKET_ENABLE,
638 PACKET_DISABLE
639 };
640
5a2468f5
JM
641struct packet_config
642 {
bb572ddd
DJ
643 const char *name;
644 const char *title;
7f19b9a2 645 enum auto_boolean detect;
5a2468f5
JM
646 enum packet_support support;
647 };
648
d471ea57 649/* Analyze a packet's return value and update the packet config
23860348 650 accordingly. */
d471ea57
AC
651
652enum packet_result
653{
654 PACKET_ERROR,
655 PACKET_OK,
656 PACKET_UNKNOWN
657};
658
5a2468f5 659static void
d471ea57 660update_packet_config (struct packet_config *config)
5a2468f5 661{
d471ea57
AC
662 switch (config->detect)
663 {
7f19b9a2 664 case AUTO_BOOLEAN_TRUE:
d471ea57
AC
665 config->support = PACKET_ENABLE;
666 break;
7f19b9a2 667 case AUTO_BOOLEAN_FALSE:
d471ea57
AC
668 config->support = PACKET_DISABLE;
669 break;
7f19b9a2 670 case AUTO_BOOLEAN_AUTO:
d471ea57
AC
671 config->support = PACKET_SUPPORT_UNKNOWN;
672 break;
673 }
5a2468f5
JM
674}
675
676static void
fba45db2 677show_packet_config_cmd (struct packet_config *config)
5a2468f5
JM
678{
679 char *support = "internal-error";
680 switch (config->support)
681 {
682 case PACKET_ENABLE:
683 support = "enabled";
684 break;
685 case PACKET_DISABLE:
686 support = "disabled";
687 break;
688 case PACKET_SUPPORT_UNKNOWN:
689 support = "unknown";
690 break;
691 }
692 switch (config->detect)
693 {
7f19b9a2 694 case AUTO_BOOLEAN_AUTO:
37a105a1
DJ
695 printf_filtered (_("Support for the `%s' packet is auto-detected, currently %s.\n"),
696 config->name, support);
5a2468f5 697 break;
7f19b9a2
AC
698 case AUTO_BOOLEAN_TRUE:
699 case AUTO_BOOLEAN_FALSE:
37a105a1
DJ
700 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
701 config->name, support);
8e248173 702 break;
5a2468f5
JM
703 }
704}
705
706static void
bb572ddd
DJ
707add_packet_config_cmd (struct packet_config *config, const char *name,
708 const char *title, int legacy)
d471ea57 709{
5a2468f5
JM
710 char *set_doc;
711 char *show_doc;
d471ea57 712 char *cmd_name;
3ed07be4 713
5a2468f5
JM
714 config->name = name;
715 config->title = title;
7f19b9a2 716 config->detect = AUTO_BOOLEAN_AUTO;
8e248173 717 config->support = PACKET_SUPPORT_UNKNOWN;
b435e160
AC
718 set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet",
719 name, title);
720 show_doc = xstrprintf ("Show current use of remote protocol `%s' (%s) packet",
721 name, title);
d471ea57 722 /* set/show TITLE-packet {auto,on,off} */
b435e160 723 cmd_name = xstrprintf ("%s-packet", title);
e9e68a56 724 add_setshow_auto_boolean_cmd (cmd_name, class_obscure,
2c5b56ce 725 &config->detect, set_doc, show_doc, NULL, /* help_doc */
bb572ddd
DJ
726 set_remote_protocol_packet_cmd,
727 show_remote_protocol_packet_cmd,
728 &remote_set_cmdlist, &remote_show_cmdlist);
23860348 729 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
d471ea57
AC
730 if (legacy)
731 {
732 char *legacy_name;
b435e160 733 legacy_name = xstrprintf ("%s-packet", name);
d471ea57 734 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
bb572ddd 735 &remote_set_cmdlist);
d471ea57 736 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
bb572ddd 737 &remote_show_cmdlist);
d471ea57 738 }
5a2468f5
JM
739}
740
d471ea57
AC
741static enum packet_result
742packet_ok (const char *buf, struct packet_config *config)
5a2468f5 743{
d471ea57 744 if (buf[0] != '\0')
5a2468f5 745 {
d471ea57 746 /* The stub recognized the packet request. Check that the
23860348 747 operation succeeded. */
d471ea57
AC
748 switch (config->support)
749 {
750 case PACKET_SUPPORT_UNKNOWN:
751 if (remote_debug)
752 fprintf_unfiltered (gdb_stdlog,
753 "Packet %s (%s) is supported\n",
754 config->name, config->title);
755 config->support = PACKET_ENABLE;
756 break;
757 case PACKET_DISABLE:
8e65ff28 758 internal_error (__FILE__, __LINE__,
e2e0b3e5 759 _("packet_ok: attempt to use a disabled packet"));
d471ea57
AC
760 break;
761 case PACKET_ENABLE:
762 break;
763 }
764 if (buf[0] == 'O' && buf[1] == 'K' && buf[2] == '\0')
23860348 765 /* "OK" - definitly OK. */
d471ea57
AC
766 return PACKET_OK;
767 if (buf[0] == 'E'
768 && isxdigit (buf[1]) && isxdigit (buf[2])
769 && buf[3] == '\0')
23860348 770 /* "Enn" - definitly an error. */
d471ea57 771 return PACKET_ERROR;
23860348 772 /* The packet may or may not be OK. Just assume it is. */
d471ea57
AC
773 return PACKET_OK;
774 }
775 else
776 {
23860348 777 /* The stub does not support the packet. */
d471ea57
AC
778 switch (config->support)
779 {
780 case PACKET_ENABLE:
7f19b9a2 781 if (config->detect == AUTO_BOOLEAN_AUTO)
d471ea57 782 /* If the stub previously indicated that the packet was
23860348 783 supported then there is a protocol error.. */
8a3fe4f8 784 error (_("Protocol error: %s (%s) conflicting enabled responses."),
d471ea57
AC
785 config->name, config->title);
786 else
23860348 787 /* The user set it wrong. */
8a3fe4f8 788 error (_("Enabled packet %s (%s) not recognized by stub"),
d471ea57
AC
789 config->name, config->title);
790 break;
791 case PACKET_SUPPORT_UNKNOWN:
792 if (remote_debug)
793 fprintf_unfiltered (gdb_stdlog,
794 "Packet %s (%s) is NOT supported\n",
795 config->name, config->title);
796 config->support = PACKET_DISABLE;
797 break;
798 case PACKET_DISABLE:
799 break;
800 }
801 return PACKET_UNKNOWN;
5a2468f5
JM
802 }
803}
804
444abaca
DJ
805enum {
806 PACKET_vCont = 0,
807 PACKET_X,
808 PACKET_qSymbol,
809 PACKET_P,
810 PACKET_p,
811 PACKET_Z0,
812 PACKET_Z1,
813 PACKET_Z2,
814 PACKET_Z3,
815 PACKET_Z4,
0876f84a 816 PACKET_qXfer_auxv,
444abaca 817 PACKET_qGetTLSAddr,
be2a5f71 818 PACKET_qSupported,
444abaca
DJ
819 PACKET_MAX
820};
506fb367 821
444abaca 822static struct packet_config remote_protocol_packets[PACKET_MAX];
dc8acb97
MS
823
824static void
444abaca
DJ
825set_remote_protocol_packet_cmd (char *args, int from_tty,
826 struct cmd_list_element *c)
dc8acb97 827{
444abaca 828 struct packet_config *packet;
dc8acb97 829
444abaca
DJ
830 for (packet = remote_protocol_packets;
831 packet < &remote_protocol_packets[PACKET_MAX];
832 packet++)
833 {
834 if (&packet->detect == c->var)
835 {
836 update_packet_config (packet);
837 return;
838 }
839 }
840 internal_error (__FILE__, __LINE__, "Could not find config for %s",
841 c->name);
dc8acb97
MS
842}
843
5a2468f5 844static void
444abaca
DJ
845show_remote_protocol_packet_cmd (struct ui_file *file, int from_tty,
846 struct cmd_list_element *c,
847 const char *value)
5a2468f5 848{
444abaca 849 struct packet_config *packet;
5a2468f5 850
444abaca
DJ
851 for (packet = remote_protocol_packets;
852 packet < &remote_protocol_packets[PACKET_MAX];
853 packet++)
854 {
855 if (&packet->detect == c->var)
856 {
857 show_packet_config_cmd (packet);
858 return;
859 }
860 }
861 internal_error (__FILE__, __LINE__, "Could not find config for %s",
862 c->name);
5a2468f5
JM
863}
864
d471ea57
AC
865/* Should we try one of the 'Z' requests? */
866
867enum Z_packet_type
868{
869 Z_PACKET_SOFTWARE_BP,
870 Z_PACKET_HARDWARE_BP,
871 Z_PACKET_WRITE_WP,
872 Z_PACKET_READ_WP,
873 Z_PACKET_ACCESS_WP,
874 NR_Z_PACKET_TYPES
875};
96baa820 876
d471ea57 877/* For compatibility with older distributions. Provide a ``set remote
23860348 878 Z-packet ...'' command that updates all the Z packet types. */
d471ea57 879
7f19b9a2 880static enum auto_boolean remote_Z_packet_detect;
96baa820
JM
881
882static void
fba45db2
KB
883set_remote_protocol_Z_packet_cmd (char *args, int from_tty,
884 struct cmd_list_element *c)
96baa820 885{
d471ea57
AC
886 int i;
887 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
888 {
444abaca
DJ
889 remote_protocol_packets[PACKET_Z0 + i].detect = remote_Z_packet_detect;
890 update_packet_config (&remote_protocol_packets[PACKET_Z0 + i]);
d471ea57 891 }
96baa820
JM
892}
893
894static void
08546159
AC
895show_remote_protocol_Z_packet_cmd (struct ui_file *file, int from_tty,
896 struct cmd_list_element *c,
897 const char *value)
96baa820 898{
d471ea57
AC
899 int i;
900 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
901 {
444abaca 902 show_packet_config_cmd (&remote_protocol_packets[PACKET_Z0 + i]);
d471ea57 903 }
96baa820
JM
904}
905
9d1f7ab2
MS
906/* Should we try the 'ThreadInfo' query packet?
907
908 This variable (NOT available to the user: auto-detect only!)
909 determines whether GDB will use the new, simpler "ThreadInfo"
910 query or the older, more complex syntax for thread queries.
802188a7 911 This is an auto-detect variable (set to true at each connect,
9d1f7ab2
MS
912 and set to false when the target fails to recognize it). */
913
914static int use_threadinfo_query;
915static int use_threadextra_query;
916
23860348 917/* Tokens for use by the asynchronous signal handlers for SIGINT. */
ae44c0c4
AC
918static void *sigint_remote_twice_token;
919static void *sigint_remote_token;
43ff13b4 920
c906108c
SS
921/* These are pointers to hook functions that may be set in order to
922 modify resume/wait behavior for a particular architecture. */
923
9a4105ab
AC
924void (*deprecated_target_resume_hook) (void);
925void (*deprecated_target_wait_loop_hook) (void);
c906108c
SS
926\f
927
c5aa993b 928
c906108c
SS
929/* These are the threads which we last sent to the remote system.
930 -1 for all or -2 for not sent yet. */
931static int general_thread;
cce74817 932static int continue_thread;
c906108c
SS
933
934/* Call this function as a result of
935 1) A halt indication (T packet) containing a thread id
936 2) A direct query of currthread
937 3) Successful execution of set thread
938 */
939
940static void
fba45db2 941record_currthread (int currthread)
c906108c 942{
c906108c 943 general_thread = currthread;
cce74817 944
c906108c
SS
945 /* If this is a new thread, add it to GDB's thread list.
946 If we leave it up to WFI to do this, bad things will happen. */
39f77062 947 if (!in_thread_list (pid_to_ptid (currthread)))
0f71a2f6 948 {
39f77062 949 add_thread (pid_to_ptid (currthread));
8b93c638 950 ui_out_text (uiout, "[New ");
39f77062 951 ui_out_text (uiout, target_pid_to_str (pid_to_ptid (currthread)));
8b93c638 952 ui_out_text (uiout, "]\n");
0f71a2f6 953 }
c906108c
SS
954}
955
956#define MAGIC_NULL_PID 42000
957
958static void
fba45db2 959set_thread (int th, int gen)
c906108c 960{
d01949b6 961 struct remote_state *rs = get_remote_state ();
6d820c5c 962 char *buf = rs->buf;
cce74817 963 int state = gen ? general_thread : continue_thread;
c906108c
SS
964
965 if (state == th)
966 return;
967
968 buf[0] = 'H';
969 buf[1] = gen ? 'g' : 'c';
970 if (th == MAGIC_NULL_PID)
971 {
972 buf[2] = '0';
973 buf[3] = '\0';
974 }
975 else if (th < 0)
ea9c271d 976 xsnprintf (&buf[2], get_remote_packet_size () - 2, "-%x", -th);
c906108c 977 else
ea9c271d 978 xsnprintf (&buf[2], get_remote_packet_size () - 2, "%x", th);
c906108c 979 putpkt (buf);
6d820c5c 980 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 981 if (gen)
c5aa993b 982 general_thread = th;
c906108c 983 else
cce74817 984 continue_thread = th;
c906108c
SS
985}
986\f
987/* Return nonzero if the thread TH is still alive on the remote system. */
988
989static int
39f77062 990remote_thread_alive (ptid_t ptid)
c906108c 991{
6d820c5c 992 struct remote_state *rs = get_remote_state ();
39f77062 993 int tid = PIDGET (ptid);
6d820c5c 994 char *buf = rs->buf;
c906108c 995
cce74817 996 if (tid < 0)
ea9c271d 997 xsnprintf (buf, get_remote_packet_size (), "T-%08x", -tid);
c906108c 998 else
ea9c271d 999 xsnprintf (buf, get_remote_packet_size (), "T%08x", tid);
c906108c 1000 putpkt (buf);
6d820c5c 1001 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c
SS
1002 return (buf[0] == 'O' && buf[1] == 'K');
1003}
1004
1005/* About these extended threadlist and threadinfo packets. They are
1006 variable length packets but, the fields within them are often fixed
1007 length. They are redundent enough to send over UDP as is the
1008 remote protocol in general. There is a matching unit test module
1009 in libstub. */
1010
cce74817
JM
1011#define OPAQUETHREADBYTES 8
1012
1013/* a 64 bit opaque identifier */
1014typedef unsigned char threadref[OPAQUETHREADBYTES];
1015
23860348
MS
1016/* WARNING: This threadref data structure comes from the remote O.S.,
1017 libstub protocol encoding, and remote.c. it is not particularly
1018 changable. */
cce74817
JM
1019
1020/* Right now, the internal structure is int. We want it to be bigger.
1021 Plan to fix this.
c5aa993b 1022 */
cce74817 1023
23860348 1024typedef int gdb_threadref; /* Internal GDB thread reference. */
cce74817 1025
9d1f7ab2 1026/* gdb_ext_thread_info is an internal GDB data structure which is
cfde0993 1027 equivalent to the reply of the remote threadinfo packet. */
cce74817
JM
1028
1029struct gdb_ext_thread_info
c5aa993b 1030 {
23860348
MS
1031 threadref threadid; /* External form of thread reference. */
1032 int active; /* Has state interesting to GDB?
1033 regs, stack. */
1034 char display[256]; /* Brief state display, name,
cedea757 1035 blocked/suspended. */
23860348
MS
1036 char shortname[32]; /* To be used to name threads. */
1037 char more_display[256]; /* Long info, statistics, queue depth,
1038 whatever. */
c5aa993b 1039 };
cce74817
JM
1040
1041/* The volume of remote transfers can be limited by submitting
1042 a mask containing bits specifying the desired information.
1043 Use a union of these values as the 'selection' parameter to
1044 get_thread_info. FIXME: Make these TAG names more thread specific.
c5aa993b 1045 */
cce74817
JM
1046
1047#define TAG_THREADID 1
1048#define TAG_EXISTS 2
1049#define TAG_DISPLAY 4
1050#define TAG_THREADNAME 8
c5aa993b 1051#define TAG_MOREDISPLAY 16
cce74817 1052
23860348 1053#define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
c906108c 1054
b2dd6311 1055char *unpack_varlen_hex (char *buff, ULONGEST *result);
cce74817 1056
a14ed312 1057static char *unpack_nibble (char *buf, int *val);
cce74817 1058
a14ed312 1059static char *pack_nibble (char *buf, int nibble);
cce74817 1060
23860348 1061static char *pack_hex_byte (char *pkt, int /* unsigned char */ byte);
cce74817 1062
a14ed312 1063static char *unpack_byte (char *buf, int *value);
cce74817 1064
a14ed312 1065static char *pack_int (char *buf, int value);
cce74817 1066
a14ed312 1067static char *unpack_int (char *buf, int *value);
cce74817 1068
a14ed312 1069static char *unpack_string (char *src, char *dest, int length);
cce74817 1070
23860348 1071static char *pack_threadid (char *pkt, threadref *id);
cce74817 1072
23860348 1073static char *unpack_threadid (char *inbuf, threadref *id);
cce74817 1074
23860348 1075void int_to_threadref (threadref *id, int value);
cce74817 1076
23860348 1077static int threadref_to_int (threadref *ref);
cce74817 1078
23860348 1079static void copy_threadref (threadref *dest, threadref *src);
cce74817 1080
23860348 1081static int threadmatch (threadref *dest, threadref *src);
cce74817 1082
23860348
MS
1083static char *pack_threadinfo_request (char *pkt, int mode,
1084 threadref *id);
cce74817 1085
a14ed312 1086static int remote_unpack_thread_info_response (char *pkt,
23860348 1087 threadref *expectedref,
a14ed312
KB
1088 struct gdb_ext_thread_info
1089 *info);
cce74817
JM
1090
1091
23860348
MS
1092static int remote_get_threadinfo (threadref *threadid,
1093 int fieldset, /*TAG mask */
a14ed312 1094 struct gdb_ext_thread_info *info);
cce74817 1095
a14ed312
KB
1096static char *pack_threadlist_request (char *pkt, int startflag,
1097 int threadcount,
23860348 1098 threadref *nextthread);
cce74817 1099
a14ed312
KB
1100static int parse_threadlist_response (char *pkt,
1101 int result_limit,
23860348
MS
1102 threadref *original_echo,
1103 threadref *resultlist,
1104 int *doneflag);
cce74817 1105
a14ed312 1106static int remote_get_threadlist (int startflag,
23860348 1107 threadref *nextthread,
a14ed312
KB
1108 int result_limit,
1109 int *done,
23860348
MS
1110 int *result_count,
1111 threadref *threadlist);
cce74817 1112
23860348 1113typedef int (*rmt_thread_action) (threadref *ref, void *context);
cce74817 1114
a14ed312
KB
1115static int remote_threadlist_iterator (rmt_thread_action stepfunction,
1116 void *context, int looplimit);
cce74817 1117
23860348 1118static int remote_newthread_step (threadref *ref, void *context);
cce74817 1119
23860348 1120/* Encode 64 bits in 16 chars of hex. */
c906108c
SS
1121
1122static const char hexchars[] = "0123456789abcdef";
1123
1124static int
fba45db2 1125ishex (int ch, int *val)
c906108c
SS
1126{
1127 if ((ch >= 'a') && (ch <= 'f'))
1128 {
1129 *val = ch - 'a' + 10;
1130 return 1;
1131 }
1132 if ((ch >= 'A') && (ch <= 'F'))
1133 {
1134 *val = ch - 'A' + 10;
1135 return 1;
1136 }
1137 if ((ch >= '0') && (ch <= '9'))
1138 {
1139 *val = ch - '0';
1140 return 1;
1141 }
1142 return 0;
1143}
1144
1145static int
fba45db2 1146stubhex (int ch)
c906108c
SS
1147{
1148 if (ch >= 'a' && ch <= 'f')
1149 return ch - 'a' + 10;
1150 if (ch >= '0' && ch <= '9')
1151 return ch - '0';
1152 if (ch >= 'A' && ch <= 'F')
1153 return ch - 'A' + 10;
1154 return -1;
1155}
1156
1157static int
fba45db2 1158stub_unpack_int (char *buff, int fieldlength)
c906108c
SS
1159{
1160 int nibble;
1161 int retval = 0;
1162
1163 while (fieldlength)
1164 {
1165 nibble = stubhex (*buff++);
1166 retval |= nibble;
1167 fieldlength--;
1168 if (fieldlength)
1169 retval = retval << 4;
1170 }
1171 return retval;
1172}
1173
1174char *
fba45db2 1175unpack_varlen_hex (char *buff, /* packet to parse */
b2dd6311 1176 ULONGEST *result)
c906108c
SS
1177{
1178 int nibble;
1179 int retval = 0;
1180
1181 while (ishex (*buff, &nibble))
1182 {
1183 buff++;
1184 retval = retval << 4;
1185 retval |= nibble & 0x0f;
1186 }
1187 *result = retval;
1188 return buff;
1189}
1190
1191static char *
fba45db2 1192unpack_nibble (char *buf, int *val)
c906108c
SS
1193{
1194 ishex (*buf++, val);
1195 return buf;
1196}
1197
1198static char *
fba45db2 1199pack_nibble (char *buf, int nibble)
c906108c
SS
1200{
1201 *buf++ = hexchars[(nibble & 0x0f)];
1202 return buf;
1203}
1204
1205static char *
fba45db2 1206pack_hex_byte (char *pkt, int byte)
c906108c
SS
1207{
1208 *pkt++ = hexchars[(byte >> 4) & 0xf];
1209 *pkt++ = hexchars[(byte & 0xf)];
1210 return pkt;
1211}
1212
1213static char *
fba45db2 1214unpack_byte (char *buf, int *value)
c906108c
SS
1215{
1216 *value = stub_unpack_int (buf, 2);
1217 return buf + 2;
1218}
1219
1220static char *
fba45db2 1221pack_int (char *buf, int value)
c906108c
SS
1222{
1223 buf = pack_hex_byte (buf, (value >> 24) & 0xff);
1224 buf = pack_hex_byte (buf, (value >> 16) & 0xff);
1225 buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
1226 buf = pack_hex_byte (buf, (value & 0xff));
1227 return buf;
1228}
1229
1230static char *
fba45db2 1231unpack_int (char *buf, int *value)
c906108c
SS
1232{
1233 *value = stub_unpack_int (buf, 8);
1234 return buf + 8;
1235}
1236
23860348 1237#if 0 /* Currently unused, uncomment when needed. */
a14ed312 1238static char *pack_string (char *pkt, char *string);
c906108c
SS
1239
1240static char *
fba45db2 1241pack_string (char *pkt, char *string)
c906108c
SS
1242{
1243 char ch;
1244 int len;
1245
1246 len = strlen (string);
1247 if (len > 200)
23860348 1248 len = 200; /* Bigger than most GDB packets, junk??? */
c906108c
SS
1249 pkt = pack_hex_byte (pkt, len);
1250 while (len-- > 0)
1251 {
1252 ch = *string++;
1253 if ((ch == '\0') || (ch == '#'))
23860348 1254 ch = '*'; /* Protect encapsulation. */
c906108c
SS
1255 *pkt++ = ch;
1256 }
1257 return pkt;
1258}
1259#endif /* 0 (unused) */
1260
1261static char *
fba45db2 1262unpack_string (char *src, char *dest, int length)
c906108c
SS
1263{
1264 while (length--)
1265 *dest++ = *src++;
1266 *dest = '\0';
1267 return src;
1268}
1269
1270static char *
fba45db2 1271pack_threadid (char *pkt, threadref *id)
c906108c
SS
1272{
1273 char *limit;
1274 unsigned char *altid;
1275
1276 altid = (unsigned char *) id;
1277 limit = pkt + BUF_THREAD_ID_SIZE;
1278 while (pkt < limit)
1279 pkt = pack_hex_byte (pkt, *altid++);
1280 return pkt;
1281}
1282
1283
1284static char *
fba45db2 1285unpack_threadid (char *inbuf, threadref *id)
c906108c
SS
1286{
1287 char *altref;
1288 char *limit = inbuf + BUF_THREAD_ID_SIZE;
1289 int x, y;
1290
1291 altref = (char *) id;
1292
1293 while (inbuf < limit)
1294 {
1295 x = stubhex (*inbuf++);
1296 y = stubhex (*inbuf++);
1297 *altref++ = (x << 4) | y;
1298 }
1299 return inbuf;
1300}
1301
1302/* Externally, threadrefs are 64 bits but internally, they are still
1303 ints. This is due to a mismatch of specifications. We would like
1304 to use 64bit thread references internally. This is an adapter
1305 function. */
1306
1307void
fba45db2 1308int_to_threadref (threadref *id, int value)
c906108c
SS
1309{
1310 unsigned char *scan;
1311
1312 scan = (unsigned char *) id;
1313 {
1314 int i = 4;
1315 while (i--)
1316 *scan++ = 0;
1317 }
1318 *scan++ = (value >> 24) & 0xff;
1319 *scan++ = (value >> 16) & 0xff;
1320 *scan++ = (value >> 8) & 0xff;
1321 *scan++ = (value & 0xff);
1322}
1323
1324static int
fba45db2 1325threadref_to_int (threadref *ref)
c906108c
SS
1326{
1327 int i, value = 0;
1328 unsigned char *scan;
1329
cfd77fa1 1330 scan = *ref;
c906108c
SS
1331 scan += 4;
1332 i = 4;
1333 while (i-- > 0)
1334 value = (value << 8) | ((*scan++) & 0xff);
1335 return value;
1336}
1337
1338static void
fba45db2 1339copy_threadref (threadref *dest, threadref *src)
c906108c
SS
1340{
1341 int i;
1342 unsigned char *csrc, *cdest;
1343
1344 csrc = (unsigned char *) src;
1345 cdest = (unsigned char *) dest;
1346 i = 8;
1347 while (i--)
1348 *cdest++ = *csrc++;
1349}
1350
1351static int
fba45db2 1352threadmatch (threadref *dest, threadref *src)
c906108c 1353{
23860348 1354 /* Things are broken right now, so just assume we got a match. */
c906108c
SS
1355#if 0
1356 unsigned char *srcp, *destp;
1357 int i, result;
1358 srcp = (char *) src;
1359 destp = (char *) dest;
1360
1361 result = 1;
1362 while (i-- > 0)
1363 result &= (*srcp++ == *destp++) ? 1 : 0;
1364 return result;
1365#endif
1366 return 1;
1367}
1368
1369/*
c5aa993b
JM
1370 threadid:1, # always request threadid
1371 context_exists:2,
1372 display:4,
1373 unique_name:8,
1374 more_display:16
1375 */
c906108c
SS
1376
1377/* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
1378
1379static char *
fba45db2 1380pack_threadinfo_request (char *pkt, int mode, threadref *id)
c906108c 1381{
23860348
MS
1382 *pkt++ = 'q'; /* Info Query */
1383 *pkt++ = 'P'; /* process or thread info */
1384 pkt = pack_int (pkt, mode); /* mode */
c906108c 1385 pkt = pack_threadid (pkt, id); /* threadid */
23860348 1386 *pkt = '\0'; /* terminate */
c906108c
SS
1387 return pkt;
1388}
1389
23860348 1390/* These values tag the fields in a thread info response packet. */
c906108c 1391/* Tagging the fields allows us to request specific fields and to
23860348 1392 add more fields as time goes by. */
c906108c 1393
23860348 1394#define TAG_THREADID 1 /* Echo the thread identifier. */
c5aa993b 1395#define TAG_EXISTS 2 /* Is this process defined enough to
23860348 1396 fetch registers and its stack? */
c5aa993b 1397#define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
23860348 1398#define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
802188a7 1399#define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
23860348 1400 the process. */
c906108c
SS
1401
1402static int
fba45db2
KB
1403remote_unpack_thread_info_response (char *pkt, threadref *expectedref,
1404 struct gdb_ext_thread_info *info)
c906108c 1405{
d01949b6 1406 struct remote_state *rs = get_remote_state ();
c906108c 1407 int mask, length;
cfd77fa1 1408 int tag;
c906108c 1409 threadref ref;
6d820c5c 1410 char *limit = pkt + rs->buf_size; /* Plausible parsing limit. */
c906108c
SS
1411 int retval = 1;
1412
23860348 1413 /* info->threadid = 0; FIXME: implement zero_threadref. */
c906108c
SS
1414 info->active = 0;
1415 info->display[0] = '\0';
1416 info->shortname[0] = '\0';
1417 info->more_display[0] = '\0';
1418
23860348
MS
1419 /* Assume the characters indicating the packet type have been
1420 stripped. */
c906108c
SS
1421 pkt = unpack_int (pkt, &mask); /* arg mask */
1422 pkt = unpack_threadid (pkt, &ref);
1423
1424 if (mask == 0)
8a3fe4f8 1425 warning (_("Incomplete response to threadinfo request."));
c906108c 1426 if (!threadmatch (&ref, expectedref))
23860348 1427 { /* This is an answer to a different request. */
8a3fe4f8 1428 warning (_("ERROR RMT Thread info mismatch."));
c906108c
SS
1429 return 0;
1430 }
1431 copy_threadref (&info->threadid, &ref);
1432
23860348 1433 /* Loop on tagged fields , try to bail if somthing goes wrong. */
c906108c 1434
23860348
MS
1435 /* Packets are terminated with nulls. */
1436 while ((pkt < limit) && mask && *pkt)
c906108c
SS
1437 {
1438 pkt = unpack_int (pkt, &tag); /* tag */
23860348
MS
1439 pkt = unpack_byte (pkt, &length); /* length */
1440 if (!(tag & mask)) /* Tags out of synch with mask. */
c906108c 1441 {
8a3fe4f8 1442 warning (_("ERROR RMT: threadinfo tag mismatch."));
c906108c
SS
1443 retval = 0;
1444 break;
1445 }
1446 if (tag == TAG_THREADID)
1447 {
1448 if (length != 16)
1449 {
8a3fe4f8 1450 warning (_("ERROR RMT: length of threadid is not 16."));
c906108c
SS
1451 retval = 0;
1452 break;
1453 }
1454 pkt = unpack_threadid (pkt, &ref);
1455 mask = mask & ~TAG_THREADID;
1456 continue;
1457 }
1458 if (tag == TAG_EXISTS)
1459 {
1460 info->active = stub_unpack_int (pkt, length);
1461 pkt += length;
1462 mask = mask & ~(TAG_EXISTS);
1463 if (length > 8)
1464 {
8a3fe4f8 1465 warning (_("ERROR RMT: 'exists' length too long."));
c906108c
SS
1466 retval = 0;
1467 break;
1468 }
1469 continue;
1470 }
1471 if (tag == TAG_THREADNAME)
1472 {
1473 pkt = unpack_string (pkt, &info->shortname[0], length);
1474 mask = mask & ~TAG_THREADNAME;
1475 continue;
1476 }
1477 if (tag == TAG_DISPLAY)
1478 {
1479 pkt = unpack_string (pkt, &info->display[0], length);
1480 mask = mask & ~TAG_DISPLAY;
1481 continue;
1482 }
1483 if (tag == TAG_MOREDISPLAY)
1484 {
1485 pkt = unpack_string (pkt, &info->more_display[0], length);
1486 mask = mask & ~TAG_MOREDISPLAY;
1487 continue;
1488 }
8a3fe4f8 1489 warning (_("ERROR RMT: unknown thread info tag."));
23860348 1490 break; /* Not a tag we know about. */
c906108c
SS
1491 }
1492 return retval;
1493}
1494
1495static int
fba45db2
KB
1496remote_get_threadinfo (threadref *threadid, int fieldset, /* TAG mask */
1497 struct gdb_ext_thread_info *info)
c906108c 1498{
d01949b6 1499 struct remote_state *rs = get_remote_state ();
c906108c 1500 int result;
6d820c5c 1501 char *threadinfo_pkt = rs->buf;
c906108c
SS
1502
1503 pack_threadinfo_request (threadinfo_pkt, fieldset, threadid);
1504 putpkt (threadinfo_pkt);
6d820c5c 1505 getpkt (&rs->buf, &rs->buf_size, 0);
23860348
MS
1506 result = remote_unpack_thread_info_response (threadinfo_pkt + 2,
1507 threadid, info);
c906108c
SS
1508 return result;
1509}
1510
c906108c
SS
1511/* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
1512
1513static char *
fba45db2
KB
1514pack_threadlist_request (char *pkt, int startflag, int threadcount,
1515 threadref *nextthread)
c906108c
SS
1516{
1517 *pkt++ = 'q'; /* info query packet */
1518 *pkt++ = 'L'; /* Process LIST or threadLIST request */
23860348 1519 pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */
c906108c
SS
1520 pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */
1521 pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */
1522 *pkt = '\0';
1523 return pkt;
1524}
1525
1526/* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
1527
1528static int
fba45db2
KB
1529parse_threadlist_response (char *pkt, int result_limit,
1530 threadref *original_echo, threadref *resultlist,
1531 int *doneflag)
c906108c 1532{
d01949b6 1533 struct remote_state *rs = get_remote_state ();
c906108c
SS
1534 char *limit;
1535 int count, resultcount, done;
1536
1537 resultcount = 0;
1538 /* Assume the 'q' and 'M chars have been stripped. */
6d820c5c 1539 limit = pkt + (rs->buf_size - BUF_THREAD_ID_SIZE);
23860348 1540 /* done parse past here */
c906108c
SS
1541 pkt = unpack_byte (pkt, &count); /* count field */
1542 pkt = unpack_nibble (pkt, &done);
1543 /* The first threadid is the argument threadid. */
1544 pkt = unpack_threadid (pkt, original_echo); /* should match query packet */
1545 while ((count-- > 0) && (pkt < limit))
1546 {
1547 pkt = unpack_threadid (pkt, resultlist++);
1548 if (resultcount++ >= result_limit)
1549 break;
1550 }
1551 if (doneflag)
1552 *doneflag = done;
1553 return resultcount;
1554}
1555
1556static int
fba45db2
KB
1557remote_get_threadlist (int startflag, threadref *nextthread, int result_limit,
1558 int *done, int *result_count, threadref *threadlist)
c906108c 1559{
d01949b6 1560 struct remote_state *rs = get_remote_state ();
c906108c 1561 static threadref echo_nextthread;
6d820c5c 1562 char *threadlist_packet = rs->buf;
c906108c
SS
1563 int result = 1;
1564
23860348 1565 /* Trancate result limit to be smaller than the packet size. */
ea9c271d
DJ
1566 if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10) >= get_remote_packet_size ())
1567 result_limit = (get_remote_packet_size () / BUF_THREAD_ID_SIZE) - 2;
c906108c 1568
6d820c5c
DJ
1569 pack_threadlist_request (rs->buf, startflag, result_limit, nextthread);
1570 putpkt (rs->buf);
1571 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c
SS
1572
1573 *result_count =
6d820c5c 1574 parse_threadlist_response (rs->buf + 2, result_limit, &echo_nextthread,
c906108c
SS
1575 threadlist, done);
1576
1577 if (!threadmatch (&echo_nextthread, nextthread))
1578 {
23860348
MS
1579 /* FIXME: This is a good reason to drop the packet. */
1580 /* Possably, there is a duplicate response. */
c906108c
SS
1581 /* Possabilities :
1582 retransmit immediatly - race conditions
1583 retransmit after timeout - yes
1584 exit
1585 wait for packet, then exit
1586 */
8a3fe4f8 1587 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
23860348 1588 return 0; /* I choose simply exiting. */
c906108c
SS
1589 }
1590 if (*result_count <= 0)
1591 {
1592 if (*done != 1)
1593 {
8a3fe4f8 1594 warning (_("RMT ERROR : failed to get remote thread list."));
c906108c
SS
1595 result = 0;
1596 }
1597 return result; /* break; */
1598 }
1599 if (*result_count > result_limit)
1600 {
1601 *result_count = 0;
8a3fe4f8 1602 warning (_("RMT ERROR: threadlist response longer than requested."));
c906108c
SS
1603 return 0;
1604 }
1605 return result;
1606}
1607
23860348
MS
1608/* This is the interface between remote and threads, remotes upper
1609 interface. */
c906108c
SS
1610
1611/* remote_find_new_threads retrieves the thread list and for each
1612 thread in the list, looks up the thread in GDB's internal list,
1613 ading the thread if it does not already exist. This involves
1614 getting partial thread lists from the remote target so, polling the
1615 quit_flag is required. */
1616
1617
23860348 1618/* About this many threadisds fit in a packet. */
c906108c
SS
1619
1620#define MAXTHREADLISTRESULTS 32
1621
1622static int
fba45db2
KB
1623remote_threadlist_iterator (rmt_thread_action stepfunction, void *context,
1624 int looplimit)
c906108c
SS
1625{
1626 int done, i, result_count;
1627 int startflag = 1;
1628 int result = 1;
1629 int loopcount = 0;
1630 static threadref nextthread;
1631 static threadref resultthreadlist[MAXTHREADLISTRESULTS];
1632
1633 done = 0;
1634 while (!done)
1635 {
1636 if (loopcount++ > looplimit)
1637 {
1638 result = 0;
8a3fe4f8 1639 warning (_("Remote fetch threadlist -infinite loop-."));
c906108c
SS
1640 break;
1641 }
1642 if (!remote_get_threadlist (startflag, &nextthread, MAXTHREADLISTRESULTS,
1643 &done, &result_count, resultthreadlist))
1644 {
1645 result = 0;
1646 break;
1647 }
23860348 1648 /* Clear for later iterations. */
c906108c
SS
1649 startflag = 0;
1650 /* Setup to resume next batch of thread references, set nextthread. */
1651 if (result_count >= 1)
1652 copy_threadref (&nextthread, &resultthreadlist[result_count - 1]);
1653 i = 0;
1654 while (result_count--)
1655 if (!(result = (*stepfunction) (&resultthreadlist[i++], context)))
1656 break;
1657 }
1658 return result;
1659}
1660
1661static int
fba45db2 1662remote_newthread_step (threadref *ref, void *context)
c906108c 1663{
39f77062 1664 ptid_t ptid;
c906108c 1665
39f77062
KB
1666 ptid = pid_to_ptid (threadref_to_int (ref));
1667
1668 if (!in_thread_list (ptid))
1669 add_thread (ptid);
c906108c
SS
1670 return 1; /* continue iterator */
1671}
1672
1673#define CRAZY_MAX_THREADS 1000
1674
39f77062
KB
1675static ptid_t
1676remote_current_thread (ptid_t oldpid)
c906108c 1677{
d01949b6 1678 struct remote_state *rs = get_remote_state ();
6d820c5c 1679 char *buf = rs->buf;
c906108c
SS
1680
1681 putpkt ("qC");
6d820c5c 1682 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 1683 if (buf[0] == 'Q' && buf[1] == 'C')
c273b20f
JB
1684 /* Use strtoul here, so we'll correctly parse values whose highest
1685 bit is set. The protocol carries them as a simple series of
1686 hex digits; in the absence of a sign, strtol will see such
1687 values as positive numbers out of range for signed 'long', and
1688 return LONG_MAX to indicate an overflow. */
1689 return pid_to_ptid (strtoul (&buf[2], NULL, 16));
c906108c
SS
1690 else
1691 return oldpid;
1692}
1693
802188a7
RM
1694/* Find new threads for info threads command.
1695 * Original version, using John Metzler's thread protocol.
9d1f7ab2 1696 */
cce74817
JM
1697
1698static void
fba45db2 1699remote_find_new_threads (void)
c906108c 1700{
c5aa993b
JM
1701 remote_threadlist_iterator (remote_newthread_step, 0,
1702 CRAZY_MAX_THREADS);
39f77062
KB
1703 if (PIDGET (inferior_ptid) == MAGIC_NULL_PID) /* ack ack ack */
1704 inferior_ptid = remote_current_thread (inferior_ptid);
c906108c
SS
1705}
1706
9d1f7ab2
MS
1707/*
1708 * Find all threads for info threads command.
1709 * Uses new thread protocol contributed by Cisco.
1710 * Falls back and attempts to use the older method (above)
1711 * if the target doesn't respond to the new method.
1712 */
1713
0f71a2f6
JM
1714static void
1715remote_threads_info (void)
1716{
d01949b6 1717 struct remote_state *rs = get_remote_state ();
085dd6e6 1718 char *bufp;
0f71a2f6
JM
1719 int tid;
1720
1721 if (remote_desc == 0) /* paranoia */
8a3fe4f8 1722 error (_("Command can only be used when connected to the remote target."));
0f71a2f6 1723
9d1f7ab2
MS
1724 if (use_threadinfo_query)
1725 {
1726 putpkt ("qfThreadInfo");
6d820c5c
DJ
1727 bufp = rs->buf;
1728 getpkt (&rs->buf, &rs->buf_size, 0);
9d1f7ab2 1729 if (bufp[0] != '\0') /* q packet recognized */
802188a7 1730 {
9d1f7ab2
MS
1731 while (*bufp++ == 'm') /* reply contains one or more TID */
1732 {
1733 do
1734 {
c273b20f
JB
1735 /* Use strtoul here, so we'll correctly parse values
1736 whose highest bit is set. The protocol carries
1737 them as a simple series of hex digits; in the
1738 absence of a sign, strtol will see such values as
1739 positive numbers out of range for signed 'long',
1740 and return LONG_MAX to indicate an overflow. */
1741 tid = strtoul (bufp, &bufp, 16);
39f77062
KB
1742 if (tid != 0 && !in_thread_list (pid_to_ptid (tid)))
1743 add_thread (pid_to_ptid (tid));
9d1f7ab2
MS
1744 }
1745 while (*bufp++ == ','); /* comma-separated list */
1746 putpkt ("qsThreadInfo");
6d820c5c
DJ
1747 bufp = rs->buf;
1748 getpkt (&rs->buf, &rs->buf_size, 0);
9d1f7ab2
MS
1749 }
1750 return; /* done */
1751 }
1752 }
1753
23860348 1754 /* Else fall back to old method based on jmetzler protocol. */
9d1f7ab2
MS
1755 use_threadinfo_query = 0;
1756 remote_find_new_threads ();
1757 return;
1758}
1759
802188a7 1760/*
9d1f7ab2
MS
1761 * Collect a descriptive string about the given thread.
1762 * The target may say anything it wants to about the thread
1763 * (typically info about its blocked / runnable state, name, etc.).
1764 * This string will appear in the info threads display.
802188a7 1765 *
9d1f7ab2
MS
1766 * Optional: targets are not required to implement this function.
1767 */
1768
1769static char *
1770remote_threads_extra_info (struct thread_info *tp)
1771{
d01949b6 1772 struct remote_state *rs = get_remote_state ();
9d1f7ab2
MS
1773 int result;
1774 int set;
1775 threadref id;
1776 struct gdb_ext_thread_info threadinfo;
23860348 1777 static char display_buf[100]; /* arbitrary... */
9d1f7ab2
MS
1778 int n = 0; /* position in display_buf */
1779
1780 if (remote_desc == 0) /* paranoia */
8e65ff28 1781 internal_error (__FILE__, __LINE__,
e2e0b3e5 1782 _("remote_threads_extra_info"));
9d1f7ab2
MS
1783
1784 if (use_threadextra_query)
1785 {
6d820c5c
DJ
1786 char *bufp = rs->buf;
1787
ea9c271d 1788 xsnprintf (bufp, get_remote_packet_size (), "qThreadExtraInfo,%x",
ecbc58df 1789 PIDGET (tp->ptid));
9d1f7ab2 1790 putpkt (bufp);
6d820c5c 1791 getpkt (&rs->buf, &rs->buf_size, 0);
9d1f7ab2
MS
1792 if (bufp[0] != 0)
1793 {
30559e10 1794 n = min (strlen (bufp) / 2, sizeof (display_buf));
cfd77fa1 1795 result = hex2bin (bufp, (gdb_byte *) display_buf, n);
30559e10 1796 display_buf [result] = '\0';
9d1f7ab2
MS
1797 return display_buf;
1798 }
0f71a2f6 1799 }
9d1f7ab2
MS
1800
1801 /* If the above query fails, fall back to the old method. */
1802 use_threadextra_query = 0;
1803 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
1804 | TAG_MOREDISPLAY | TAG_DISPLAY;
39f77062 1805 int_to_threadref (&id, PIDGET (tp->ptid));
9d1f7ab2
MS
1806 if (remote_get_threadinfo (&id, set, &threadinfo))
1807 if (threadinfo.active)
0f71a2f6 1808 {
9d1f7ab2 1809 if (*threadinfo.shortname)
ecbc58df
WZ
1810 n += xsnprintf (&display_buf[0], sizeof (display_buf) - n,
1811 " Name: %s,", threadinfo.shortname);
9d1f7ab2 1812 if (*threadinfo.display)
ecbc58df
WZ
1813 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
1814 " State: %s,", threadinfo.display);
9d1f7ab2 1815 if (*threadinfo.more_display)
ecbc58df
WZ
1816 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
1817 " Priority: %s", threadinfo.more_display);
9d1f7ab2
MS
1818
1819 if (n > 0)
c5aa993b 1820 {
23860348 1821 /* For purely cosmetic reasons, clear up trailing commas. */
9d1f7ab2
MS
1822 if (',' == display_buf[n-1])
1823 display_buf[n-1] = ' ';
1824 return display_buf;
c5aa993b 1825 }
0f71a2f6 1826 }
9d1f7ab2 1827 return NULL;
0f71a2f6 1828}
c906108c 1829\f
c5aa993b 1830
24b06219 1831/* Restart the remote side; this is an extended protocol operation. */
c906108c
SS
1832
1833static void
fba45db2 1834extended_remote_restart (void)
c906108c 1835{
d01949b6 1836 struct remote_state *rs = get_remote_state ();
c906108c
SS
1837
1838 /* Send the restart command; for reasons I don't understand the
1839 remote side really expects a number after the "R". */
ea9c271d 1840 xsnprintf (rs->buf, get_remote_packet_size (), "R%x", 0);
6d820c5c 1841 putpkt (rs->buf);
c906108c 1842
ad9a8f3f
NS
1843 remote_fileio_reset ();
1844
c906108c
SS
1845 /* Now query for status so this looks just like we restarted
1846 gdbserver from scratch. */
1847 putpkt ("?");
01d3a6ce 1848 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c
SS
1849}
1850\f
1851/* Clean up connection to a remote debugger. */
1852
c906108c 1853static void
fba45db2 1854remote_close (int quitting)
c906108c
SS
1855{
1856 if (remote_desc)
2cd58942 1857 serial_close (remote_desc);
c906108c
SS
1858 remote_desc = NULL;
1859}
1860
23860348 1861/* Query the remote side for the text, data and bss offsets. */
c906108c
SS
1862
1863static void
fba45db2 1864get_offsets (void)
c906108c 1865{
d01949b6 1866 struct remote_state *rs = get_remote_state ();
6d820c5c 1867 char *buf = rs->buf;
085dd6e6 1868 char *ptr;
c906108c
SS
1869 int lose;
1870 CORE_ADDR text_addr, data_addr, bss_addr;
1871 struct section_offsets *offs;
1872
1873 putpkt ("qOffsets");
6d820c5c 1874 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c
SS
1875
1876 if (buf[0] == '\000')
1877 return; /* Return silently. Stub doesn't support
23860348 1878 this command. */
c906108c
SS
1879 if (buf[0] == 'E')
1880 {
8a3fe4f8 1881 warning (_("Remote failure reply: %s"), buf);
c906108c
SS
1882 return;
1883 }
1884
1885 /* Pick up each field in turn. This used to be done with scanf, but
1886 scanf will make trouble if CORE_ADDR size doesn't match
1887 conversion directives correctly. The following code will work
1888 with any size of CORE_ADDR. */
1889 text_addr = data_addr = bss_addr = 0;
1890 ptr = buf;
1891 lose = 0;
1892
1893 if (strncmp (ptr, "Text=", 5) == 0)
1894 {
1895 ptr += 5;
1896 /* Don't use strtol, could lose on big values. */
1897 while (*ptr && *ptr != ';')
1898 text_addr = (text_addr << 4) + fromhex (*ptr++);
1899 }
1900 else
1901 lose = 1;
1902
1903 if (!lose && strncmp (ptr, ";Data=", 6) == 0)
1904 {
1905 ptr += 6;
1906 while (*ptr && *ptr != ';')
1907 data_addr = (data_addr << 4) + fromhex (*ptr++);
1908 }
1909 else
1910 lose = 1;
1911
1912 if (!lose && strncmp (ptr, ";Bss=", 5) == 0)
1913 {
1914 ptr += 5;
1915 while (*ptr && *ptr != ';')
1916 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
1917 }
1918 else
1919 lose = 1;
1920
1921 if (lose)
8a3fe4f8 1922 error (_("Malformed response to offset query, %s"), buf);
c906108c
SS
1923
1924 if (symfile_objfile == NULL)
1925 return;
1926
802188a7 1927 offs = ((struct section_offsets *)
a39a16c4 1928 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections)));
802188a7 1929 memcpy (offs, symfile_objfile->section_offsets,
a39a16c4 1930 SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections));
c906108c 1931
a4c8257b 1932 offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
c906108c
SS
1933
1934 /* This is a temporary kludge to force data and bss to use the same offsets
1935 because that's what nlmconv does now. The real solution requires changes
1936 to the stub and remote.c that I don't have time to do right now. */
1937
a4c8257b
EZ
1938 offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr;
1939 offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr;
c906108c
SS
1940
1941 objfile_relocate (symfile_objfile, offs);
1942}
1943
1944/* Stub for catch_errors. */
1945
0f71a2f6 1946static int
36918e70 1947remote_start_remote_dummy (struct ui_out *uiout, void *dummy)
0f71a2f6 1948{
23860348 1949 start_remote (); /* Initialize gdb process mechanisms. */
36918e70
AC
1950 /* NOTE: Return something >=0. A -ve value is reserved for
1951 catch_exceptions. */
0f71a2f6
JM
1952 return 1;
1953}
1954
9cbc821d 1955static void
36918e70 1956remote_start_remote (struct ui_out *uiout, void *dummy)
c906108c 1957{
23860348 1958 immediate_quit++; /* Allow user to interrupt it. */
c906108c
SS
1959
1960 /* Ack any packet which the remote side has already sent. */
2cd58942 1961 serial_write (remote_desc, "+", 1);
c906108c
SS
1962
1963 /* Let the stub know that we want it to return the thread. */
1964 set_thread (-1, 0);
1965
39f77062 1966 inferior_ptid = remote_current_thread (inferior_ptid);
c906108c 1967
23860348 1968 get_offsets (); /* Get text, data & bss offsets. */
c906108c 1969
23860348 1970 putpkt ("?"); /* Initiate a query from remote machine. */
8edbea78 1971 immediate_quit--;
c906108c 1972
9cbc821d 1973 remote_start_remote_dummy (uiout, dummy);
c906108c
SS
1974}
1975
1976/* Open a connection to a remote debugger.
1977 NAME is the filename used for communication. */
1978
1979static void
fba45db2 1980remote_open (char *name, int from_tty)
c906108c 1981{
92d1e331 1982 remote_open_1 (name, from_tty, &remote_ops, 0, 0);
c906108c
SS
1983}
1984
23860348 1985/* Just like remote_open, but with asynchronous support. */
43ff13b4 1986static void
fba45db2 1987remote_async_open (char *name, int from_tty)
43ff13b4 1988{
92d1e331 1989 remote_open_1 (name, from_tty, &remote_async_ops, 0, 1);
43ff13b4
JM
1990}
1991
c906108c
SS
1992/* Open a connection to a remote debugger using the extended
1993 remote gdb protocol. NAME is the filename used for communication. */
1994
1995static void
fba45db2 1996extended_remote_open (char *name, int from_tty)
c906108c 1997{
92d1e331
DJ
1998 remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */,
1999 0 /* async_p */);
c906108c
SS
2000}
2001
23860348 2002/* Just like extended_remote_open, but with asynchronous support. */
43ff13b4 2003static void
fba45db2 2004extended_remote_async_open (char *name, int from_tty)
43ff13b4 2005{
92d1e331
DJ
2006 remote_open_1 (name, from_tty, &extended_async_remote_ops,
2007 1 /*extended_p */, 1 /* async_p */);
43ff13b4
JM
2008}
2009
c906108c
SS
2010/* Generic code for opening a connection to a remote target. */
2011
d471ea57
AC
2012static void
2013init_all_packet_configs (void)
2014{
2015 int i;
444abaca
DJ
2016 for (i = 0; i < PACKET_MAX; i++)
2017 update_packet_config (&remote_protocol_packets[i]);
d471ea57
AC
2018}
2019
23860348 2020/* Symbol look-up. */
dc8acb97
MS
2021
2022static void
2023remote_check_symbols (struct objfile *objfile)
2024{
d01949b6 2025 struct remote_state *rs = get_remote_state ();
dc8acb97
MS
2026 char *msg, *reply, *tmp;
2027 struct minimal_symbol *sym;
2028 int end;
2029
444abaca 2030 if (remote_protocol_packets[PACKET_qSymbol].support == PACKET_DISABLE)
dc8acb97
MS
2031 return;
2032
6d820c5c
DJ
2033 /* Allocate a message buffer. We can't reuse the input buffer in RS,
2034 because we need both at the same time. */
ea9c271d 2035 msg = alloca (get_remote_packet_size ());
6d820c5c
DJ
2036
2037 reply = rs->buf;
dc8acb97 2038
23860348 2039 /* Invite target to request symbol lookups. */
dc8acb97
MS
2040
2041 putpkt ("qSymbol::");
6d820c5c
DJ
2042 getpkt (&rs->buf, &rs->buf_size, 0);
2043 packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSymbol]);
dc8acb97
MS
2044
2045 while (strncmp (reply, "qSymbol:", 8) == 0)
2046 {
2047 tmp = &reply[8];
cfd77fa1 2048 end = hex2bin (tmp, (gdb_byte *) msg, strlen (tmp) / 2);
dc8acb97
MS
2049 msg[end] = '\0';
2050 sym = lookup_minimal_symbol (msg, NULL, NULL);
2051 if (sym == NULL)
ea9c271d 2052 xsnprintf (msg, get_remote_packet_size (), "qSymbol::%s", &reply[8]);
dc8acb97 2053 else
ea9c271d 2054 xsnprintf (msg, get_remote_packet_size (), "qSymbol:%s:%s",
ecbc58df
WZ
2055 paddr_nz (SYMBOL_VALUE_ADDRESS (sym)),
2056 &reply[8]);
dc8acb97 2057 putpkt (msg);
6d820c5c 2058 getpkt (&rs->buf, &rs->buf_size, 0);
dc8acb97
MS
2059 }
2060}
2061
9db8d71f
DJ
2062static struct serial *
2063remote_serial_open (char *name)
2064{
2065 static int udp_warning = 0;
2066
2067 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
2068 of in ser-tcp.c, because it is the remote protocol assuming that the
2069 serial connection is reliable and not the serial connection promising
2070 to be. */
2071 if (!udp_warning && strncmp (name, "udp:", 4) == 0)
2072 {
8a3fe4f8
AC
2073 warning (_("\
2074The remote protocol may be unreliable over UDP.\n\
2075Some events may be lost, rendering further debugging impossible."));
9db8d71f
DJ
2076 udp_warning = 1;
2077 }
2078
2079 return serial_open (name);
2080}
2081
be2a5f71
DJ
2082/* This type describes each known response to the qSupported
2083 packet. */
2084struct protocol_feature
2085{
2086 /* The name of this protocol feature. */
2087 const char *name;
2088
2089 /* The default for this protocol feature. */
2090 enum packet_support default_support;
2091
2092 /* The function to call when this feature is reported, or after
2093 qSupported processing if the feature is not supported.
2094 The first argument points to this structure. The second
2095 argument indicates whether the packet requested support be
2096 enabled, disabled, or probed (or the default, if this function
2097 is being called at the end of processing and this feature was
2098 not reported). The third argument may be NULL; if not NULL, it
2099 is a NUL-terminated string taken from the packet following
2100 this feature's name and an equals sign. */
2101 void (*func) (const struct protocol_feature *, enum packet_support,
2102 const char *);
2103
2104 /* The corresponding packet for this feature. Only used if
2105 FUNC is remote_supported_packet. */
2106 int packet;
2107};
2108
be2a5f71
DJ
2109static void
2110remote_supported_packet (const struct protocol_feature *feature,
2111 enum packet_support support,
2112 const char *argument)
2113{
2114 if (argument)
2115 {
2116 warning (_("Remote qSupported response supplied an unexpected value for"
2117 " \"%s\"."), feature->name);
2118 return;
2119 }
2120
2121 if (remote_protocol_packets[feature->packet].support
2122 == PACKET_SUPPORT_UNKNOWN)
2123 remote_protocol_packets[feature->packet].support = support;
2124}
be2a5f71
DJ
2125
2126static void
2127remote_packet_size (const struct protocol_feature *feature,
2128 enum packet_support support, const char *value)
2129{
2130 struct remote_state *rs = get_remote_state ();
2131
2132 int packet_size;
2133 char *value_end;
2134
2135 if (support != PACKET_ENABLE)
2136 return;
2137
2138 if (value == NULL || *value == '\0')
2139 {
2140 warning (_("Remote target reported \"%s\" without a size."),
2141 feature->name);
2142 return;
2143 }
2144
2145 errno = 0;
2146 packet_size = strtol (value, &value_end, 16);
2147 if (errno != 0 || *value_end != '\0' || packet_size < 0)
2148 {
2149 warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
2150 feature->name, value);
2151 return;
2152 }
2153
2154 if (packet_size > MAX_REMOTE_PACKET_SIZE)
2155 {
2156 warning (_("limiting remote suggested packet size (%d bytes) to %d"),
2157 packet_size, MAX_REMOTE_PACKET_SIZE);
2158 packet_size = MAX_REMOTE_PACKET_SIZE;
2159 }
2160
2161 /* Record the new maximum packet size. */
2162 rs->explicit_packet_size = packet_size;
2163}
2164
2165static struct protocol_feature remote_protocol_features[] = {
0876f84a
DJ
2166 { "PacketSize", PACKET_DISABLE, remote_packet_size, -1 },
2167 { "qPart:auxv:read", PACKET_DISABLE, remote_supported_packet,
2168 PACKET_qXfer_auxv }
be2a5f71
DJ
2169};
2170
2171static void
2172remote_query_supported (void)
2173{
2174 struct remote_state *rs = get_remote_state ();
2175 char *next;
2176 int i;
2177 unsigned char seen [ARRAY_SIZE (remote_protocol_features)];
2178
2179 /* The packet support flags are handled differently for this packet
2180 than for most others. We treat an error, a disabled packet, and
2181 an empty response identically: any features which must be reported
2182 to be used will be automatically disabled. An empty buffer
2183 accomplishes this, since that is also the representation for a list
2184 containing no features. */
2185
2186 rs->buf[0] = 0;
2187 if (remote_protocol_packets[PACKET_qSupported].support != PACKET_DISABLE)
2188 {
2189 putpkt ("qSupported");
2190 getpkt (&rs->buf, &rs->buf_size, 0);
2191
2192 /* If an error occured, warn, but do not return - just reset the
2193 buffer to empty and go on to disable features. */
2194 if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSupported])
2195 == PACKET_ERROR)
2196 {
2197 warning (_("Remote failure reply: %s"), rs->buf);
2198 rs->buf[0] = 0;
2199 }
2200 }
2201
2202 memset (seen, 0, sizeof (seen));
2203
2204 next = rs->buf;
2205 while (*next)
2206 {
2207 enum packet_support is_supported;
2208 char *p, *end, *name_end, *value;
2209
2210 /* First separate out this item from the rest of the packet. If
2211 there's another item after this, we overwrite the separator
2212 (terminated strings are much easier to work with). */
2213 p = next;
2214 end = strchr (p, ';');
2215 if (end == NULL)
2216 {
2217 end = p + strlen (p);
2218 next = end;
2219 }
2220 else
2221 {
2222 if (end == p)
2223 {
2224 warning (_("empty item in \"qSupported\" response"));
2225 continue;
2226 }
2227
2228 *end = '\0';
2229 next = end + 1;
2230 }
2231
2232 name_end = strchr (p, '=');
2233 if (name_end)
2234 {
2235 /* This is a name=value entry. */
2236 is_supported = PACKET_ENABLE;
2237 value = name_end + 1;
2238 *name_end = '\0';
2239 }
2240 else
2241 {
2242 value = NULL;
2243 switch (end[-1])
2244 {
2245 case '+':
2246 is_supported = PACKET_ENABLE;
2247 break;
2248
2249 case '-':
2250 is_supported = PACKET_DISABLE;
2251 break;
2252
2253 case '?':
2254 is_supported = PACKET_SUPPORT_UNKNOWN;
2255 break;
2256
2257 default:
2258 warning (_("unrecognized item \"%s\" in \"qSupported\" response"), p);
2259 continue;
2260 }
2261 end[-1] = '\0';
2262 }
2263
2264 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
2265 if (strcmp (remote_protocol_features[i].name, p) == 0)
2266 {
2267 const struct protocol_feature *feature;
2268
2269 seen[i] = 1;
2270 feature = &remote_protocol_features[i];
2271 feature->func (feature, is_supported, value);
2272 break;
2273 }
2274 }
2275
2276 /* If we increased the packet size, make sure to increase the global
2277 buffer size also. We delay this until after parsing the entire
2278 qSupported packet, because this is the same buffer we were
2279 parsing. */
2280 if (rs->buf_size < rs->explicit_packet_size)
2281 {
2282 rs->buf_size = rs->explicit_packet_size;
2283 rs->buf = xrealloc (rs->buf, rs->buf_size);
2284 }
2285
2286 /* Handle the defaults for unmentioned features. */
2287 for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
2288 if (!seen[i])
2289 {
2290 const struct protocol_feature *feature;
2291
2292 feature = &remote_protocol_features[i];
2293 feature->func (feature, feature->default_support, NULL);
2294 }
2295}
2296
2297
c906108c 2298static void
fba45db2 2299remote_open_1 (char *name, int from_tty, struct target_ops *target,
92d1e331 2300 int extended_p, int async_p)
c906108c 2301{
d01949b6 2302 struct remote_state *rs = get_remote_state ();
c906108c 2303 if (name == 0)
8a3fe4f8 2304 error (_("To open a remote debug connection, you need to specify what\n"
22e04375 2305 "serial device is attached to the remote system\n"
8a3fe4f8 2306 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
c906108c 2307
23860348 2308 /* See FIXME above. */
92d1e331
DJ
2309 if (!async_p)
2310 wait_forever_enabled_p = 1;
6426a772 2311
c906108c
SS
2312 target_preopen (from_tty);
2313
2314 unpush_target (target);
2315
ad9a8f3f 2316 remote_fileio_reset ();
1dd41f16
NS
2317 reopen_exec_file ();
2318 reread_symbols ();
2319
9db8d71f 2320 remote_desc = remote_serial_open (name);
c906108c
SS
2321 if (!remote_desc)
2322 perror_with_name (name);
2323
2324 if (baud_rate != -1)
2325 {
2cd58942 2326 if (serial_setbaudrate (remote_desc, baud_rate))
c906108c 2327 {
9b74d5d3
KB
2328 /* The requested speed could not be set. Error out to
2329 top level after closing remote_desc. Take care to
2330 set remote_desc to NULL to avoid closing remote_desc
2331 more than once. */
2cd58942 2332 serial_close (remote_desc);
9b74d5d3 2333 remote_desc = NULL;
c906108c
SS
2334 perror_with_name (name);
2335 }
2336 }
2337
2cd58942 2338 serial_raw (remote_desc);
c906108c
SS
2339
2340 /* If there is something sitting in the buffer we might take it as a
2341 response to a command, which would be bad. */
2cd58942 2342 serial_flush_input (remote_desc);
c906108c
SS
2343
2344 if (from_tty)
2345 {
2346 puts_filtered ("Remote debugging using ");
2347 puts_filtered (name);
2348 puts_filtered ("\n");
2349 }
23860348 2350 push_target (target); /* Switch to using remote target now. */
c906108c 2351
be2a5f71
DJ
2352 /* Reset the target state; these things will be queried either by
2353 remote_query_supported or as they are needed. */
d471ea57 2354 init_all_packet_configs ();
be2a5f71 2355 rs->explicit_packet_size = 0;
802188a7 2356
c5aa993b 2357 general_thread = -2;
cce74817 2358 continue_thread = -2;
c906108c 2359
9d1f7ab2
MS
2360 /* Probe for ability to use "ThreadInfo" query, as required. */
2361 use_threadinfo_query = 1;
2362 use_threadextra_query = 1;
2363
be2a5f71
DJ
2364 /* The first packet we send to the target is the optional "supported
2365 packets" request. If the target can answer this, it will tell us
2366 which later probes to skip. */
2367 remote_query_supported ();
2368
c906108c
SS
2369 /* Without this, some commands which require an active target (such
2370 as kill) won't work. This variable serves (at least) double duty
2371 as both the pid of the target process (if it has such), and as a
2372 flag indicating that a target is active. These functions should
2373 be split out into seperate variables, especially since GDB will
2374 someday have a notion of debugging several processes. */
2375
39f77062 2376 inferior_ptid = pid_to_ptid (MAGIC_NULL_PID);
92d1e331
DJ
2377
2378 if (async_p)
2379 {
23860348 2380 /* With this target we start out by owning the terminal. */
92d1e331
DJ
2381 remote_async_terminal_ours_p = 1;
2382
2383 /* FIXME: cagney/1999-09-23: During the initial connection it is
2384 assumed that the target is already ready and able to respond to
2385 requests. Unfortunately remote_start_remote() eventually calls
2386 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
2387 around this. Eventually a mechanism that allows
2388 wait_for_inferior() to expect/get timeouts will be
23860348 2389 implemented. */
92d1e331
DJ
2390 wait_forever_enabled_p = 0;
2391 }
2392
23860348 2393 /* First delete any symbols previously loaded from shared libraries. */
f78f6cf1 2394 no_shared_libraries (NULL, 0);
f78f6cf1 2395
36918e70 2396 /* Start the remote connection. If error() or QUIT, discard this
165b8e33
AC
2397 target (we'd otherwise be in an inconsistent state) and then
2398 propogate the error on up the exception chain. This ensures that
2399 the caller doesn't stumble along blindly assuming that the
2400 function succeeded. The CLI doesn't have this problem but other
2401 UI's, such as MI do.
36918e70
AC
2402
2403 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
2404 this function should return an error indication letting the
ce2826aa 2405 caller restore the previous state. Unfortunately the command
36918e70
AC
2406 ``target remote'' is directly wired to this function making that
2407 impossible. On a positive note, the CLI side of this problem has
2408 been fixed - the function set_cmd_context() makes it possible for
2409 all the ``target ....'' commands to share a common callback
2410 function. See cli-dump.c. */
109c3e39
AC
2411 {
2412 struct gdb_exception ex
2413 = catch_exception (uiout, remote_start_remote, NULL, RETURN_MASK_ALL);
2414 if (ex.reason < 0)
2415 {
2416 pop_target ();
2417 if (async_p)
2418 wait_forever_enabled_p = 1;
2419 throw_exception (ex);
2420 }
2421 }
c906108c 2422
92d1e331
DJ
2423 if (async_p)
2424 wait_forever_enabled_p = 1;
6426a772
JM
2425
2426 if (extended_p)
43ff13b4 2427 {
6240bebf 2428 /* Tell the remote that we are using the extended protocol. */
6426a772 2429 putpkt ("!");
6d820c5c 2430 getpkt (&rs->buf, &rs->buf_size, 0);
43ff13b4 2431 }
a77053c2 2432
9353355f 2433 post_create_inferior (&current_target, from_tty);
f78f6cf1 2434
23860348 2435 if (exec_bfd) /* No use without an exec file. */
9353355f 2436 remote_check_symbols (symfile_objfile);
43ff13b4
JM
2437}
2438
c906108c
SS
2439/* This takes a program previously attached to and detaches it. After
2440 this is done, GDB can be used to debug some other program. We
2441 better not have left any breakpoints in the target program or it'll
2442 die when it hits one. */
2443
2444static void
fba45db2 2445remote_detach (char *args, int from_tty)
c906108c 2446{
d01949b6 2447 struct remote_state *rs = get_remote_state ();
c906108c
SS
2448
2449 if (args)
8a3fe4f8 2450 error (_("Argument given to \"detach\" when remotely debugging."));
c906108c
SS
2451
2452 /* Tell the remote target to detach. */
6d820c5c
DJ
2453 strcpy (rs->buf, "D");
2454 remote_send (&rs->buf, &rs->buf_size);
c906108c 2455
23860348 2456 /* Unregister the file descriptor from the event loop. */
6ad8ae5c
DJ
2457 if (target_is_async_p ())
2458 serial_async (remote_desc, NULL, 0);
2459
cca728d0 2460 target_mourn_inferior ();
c906108c
SS
2461 if (from_tty)
2462 puts_filtered ("Ending remote debugging.\n");
2463}
2464
6ad8ae5c
DJ
2465/* Same as remote_detach, but don't send the "D" packet; just disconnect. */
2466
43ff13b4 2467static void
597320e7 2468remote_disconnect (struct target_ops *target, char *args, int from_tty)
43ff13b4 2469{
43ff13b4 2470 if (args)
8a3fe4f8 2471 error (_("Argument given to \"detach\" when remotely debugging."));
43ff13b4 2472
23860348 2473 /* Unregister the file descriptor from the event loop. */
ed9a39eb 2474 if (target_is_async_p ())
2cd58942 2475 serial_async (remote_desc, NULL, 0);
43ff13b4 2476
cca728d0 2477 target_mourn_inferior ();
43ff13b4
JM
2478 if (from_tty)
2479 puts_filtered ("Ending remote debugging.\n");
2480}
2481
c906108c
SS
2482/* Convert hex digit A to a number. */
2483
30559e10 2484static int
fba45db2 2485fromhex (int a)
c906108c
SS
2486{
2487 if (a >= '0' && a <= '9')
2488 return a - '0';
2489 else if (a >= 'a' && a <= 'f')
2490 return a - 'a' + 10;
2491 else if (a >= 'A' && a <= 'F')
2492 return a - 'A' + 10;
c5aa993b 2493 else
8a3fe4f8 2494 error (_("Reply contains invalid hex digit %d"), a);
c906108c
SS
2495}
2496
30559e10 2497static int
cfd77fa1 2498hex2bin (const char *hex, gdb_byte *bin, int count)
30559e10
MS
2499{
2500 int i;
2501
30559e10
MS
2502 for (i = 0; i < count; i++)
2503 {
2504 if (hex[0] == 0 || hex[1] == 0)
2505 {
2506 /* Hex string is short, or of uneven length.
23860348 2507 Return the count that has been converted so far. */
30559e10
MS
2508 return i;
2509 }
2510 *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
2511 hex += 2;
2512 }
2513 return i;
2514}
2515
c906108c
SS
2516/* Convert number NIB to a hex digit. */
2517
2518static int
fba45db2 2519tohex (int nib)
c906108c
SS
2520{
2521 if (nib < 10)
c5aa993b 2522 return '0' + nib;
c906108c 2523 else
c5aa993b 2524 return 'a' + nib - 10;
c906108c 2525}
30559e10
MS
2526
2527static int
cfd77fa1 2528bin2hex (const gdb_byte *bin, char *hex, int count)
30559e10
MS
2529{
2530 int i;
23860348 2531 /* May use a length, or a nul-terminated string as input. */
30559e10 2532 if (count == 0)
cfd77fa1 2533 count = strlen ((char *) bin);
30559e10
MS
2534
2535 for (i = 0; i < count; i++)
2536 {
2537 *hex++ = tohex ((*bin >> 4) & 0xf);
2538 *hex++ = tohex (*bin++ & 0xf);
2539 }
2540 *hex = 0;
2541 return i;
2542}
c906108c 2543\f
506fb367
DJ
2544/* Check for the availability of vCont. This function should also check
2545 the response. */
c906108c
SS
2546
2547static void
6d820c5c 2548remote_vcont_probe (struct remote_state *rs)
c906108c 2549{
6d820c5c
DJ
2550 char *buf = rs->buf;
2551
506fb367
DJ
2552 strcpy (buf, "vCont?");
2553 putpkt (buf);
6d820c5c 2554 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 2555
506fb367
DJ
2556 /* Make sure that the features we assume are supported. */
2557 if (strncmp (buf, "vCont", 5) == 0)
2558 {
2559 char *p = &buf[5];
2560 int support_s, support_S, support_c, support_C;
2561
2562 support_s = 0;
2563 support_S = 0;
2564 support_c = 0;
2565 support_C = 0;
2566 while (p && *p == ';')
2567 {
2568 p++;
2569 if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0))
2570 support_s = 1;
2571 else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0))
2572 support_S = 1;
2573 else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0))
2574 support_c = 1;
2575 else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0))
2576 support_C = 1;
2577
2578 p = strchr (p, ';');
2579 }
c906108c 2580
506fb367
DJ
2581 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
2582 BUF will make packet_ok disable the packet. */
2583 if (!support_s || !support_S || !support_c || !support_C)
2584 buf[0] = 0;
2585 }
c906108c 2586
444abaca 2587 packet_ok (buf, &remote_protocol_packets[PACKET_vCont]);
506fb367 2588}
c906108c 2589
506fb367
DJ
2590/* Resume the remote inferior by using a "vCont" packet. The thread
2591 to be resumed is PTID; STEP and SIGGNAL indicate whether the
2592 resumed thread should be single-stepped and/or signalled. If PTID's
2593 PID is -1, then all threads are resumed; the thread to be stepped and/or
2594 signalled is given in the global INFERIOR_PTID. This function returns
2595 non-zero iff it resumes the inferior.
44eaed12 2596
506fb367
DJ
2597 This function issues a strict subset of all possible vCont commands at the
2598 moment. */
44eaed12 2599
506fb367
DJ
2600static int
2601remote_vcont_resume (ptid_t ptid, int step, enum target_signal siggnal)
2602{
2603 struct remote_state *rs = get_remote_state ();
2604 int pid = PIDGET (ptid);
2963ee1d 2605 char *buf = NULL, *outbuf;
506fb367 2606 struct cleanup *old_cleanup;
44eaed12 2607
444abaca 2608 if (remote_protocol_packets[PACKET_vCont].support == PACKET_SUPPORT_UNKNOWN)
6d820c5c 2609 remote_vcont_probe (rs);
44eaed12 2610
444abaca 2611 if (remote_protocol_packets[PACKET_vCont].support == PACKET_DISABLE)
6d820c5c 2612 return 0;
44eaed12 2613
506fb367
DJ
2614 /* If we could generate a wider range of packets, we'd have to worry
2615 about overflowing BUF. Should there be a generic
2616 "multi-part-packet" packet? */
2617
2618 if (PIDGET (inferior_ptid) == MAGIC_NULL_PID)
c906108c 2619 {
506fb367
DJ
2620 /* MAGIC_NULL_PTID means that we don't have any active threads, so we
2621 don't have any PID numbers the inferior will understand. Make sure
2622 to only send forms that do not specify a PID. */
2623 if (step && siggnal != TARGET_SIGNAL_0)
2963ee1d 2624 outbuf = xstrprintf ("vCont;S%02x", siggnal);
506fb367 2625 else if (step)
2963ee1d 2626 outbuf = xstrprintf ("vCont;s");
506fb367 2627 else if (siggnal != TARGET_SIGNAL_0)
2963ee1d 2628 outbuf = xstrprintf ("vCont;C%02x", siggnal);
506fb367 2629 else
2963ee1d 2630 outbuf = xstrprintf ("vCont;c");
506fb367
DJ
2631 }
2632 else if (pid == -1)
2633 {
2634 /* Resume all threads, with preference for INFERIOR_PTID. */
2635 if (step && siggnal != TARGET_SIGNAL_0)
2963ee1d
DJ
2636 outbuf = xstrprintf ("vCont;S%02x:%x;c", siggnal,
2637 PIDGET (inferior_ptid));
506fb367 2638 else if (step)
2963ee1d 2639 outbuf = xstrprintf ("vCont;s:%x;c", PIDGET (inferior_ptid));
506fb367 2640 else if (siggnal != TARGET_SIGNAL_0)
2963ee1d
DJ
2641 outbuf = xstrprintf ("vCont;C%02x:%x;c", siggnal,
2642 PIDGET (inferior_ptid));
506fb367 2643 else
2963ee1d 2644 outbuf = xstrprintf ("vCont;c");
c906108c
SS
2645 }
2646 else
506fb367
DJ
2647 {
2648 /* Scheduler locking; resume only PTID. */
2649 if (step && siggnal != TARGET_SIGNAL_0)
2963ee1d 2650 outbuf = xstrprintf ("vCont;S%02x:%x", siggnal, pid);
506fb367 2651 else if (step)
2963ee1d 2652 outbuf = xstrprintf ("vCont;s:%x", pid);
506fb367 2653 else if (siggnal != TARGET_SIGNAL_0)
2963ee1d 2654 outbuf = xstrprintf ("vCont;C%02x:%x", siggnal, pid);
506fb367 2655 else
2963ee1d 2656 outbuf = xstrprintf ("vCont;c:%x", pid);
506fb367 2657 }
c906108c 2658
ea9c271d 2659 gdb_assert (outbuf && strlen (outbuf) < get_remote_packet_size ());
6d820c5c 2660 old_cleanup = make_cleanup (xfree, outbuf);
2963ee1d
DJ
2661
2662 putpkt (outbuf);
506fb367
DJ
2663
2664 do_cleanups (old_cleanup);
2665
2666 return 1;
c906108c 2667}
43ff13b4 2668
506fb367
DJ
2669/* Tell the remote machine to resume. */
2670
2671static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
2672
2673static int last_sent_step;
2674
43ff13b4 2675static void
506fb367 2676remote_resume (ptid_t ptid, int step, enum target_signal siggnal)
43ff13b4 2677{
d01949b6 2678 struct remote_state *rs = get_remote_state ();
6d820c5c 2679 char *buf = rs->buf;
39f77062 2680 int pid = PIDGET (ptid);
43ff13b4 2681
43ff13b4
JM
2682 last_sent_signal = siggnal;
2683 last_sent_step = step;
2684
2685 /* A hook for when we need to do something at the last moment before
2686 resumption. */
9a4105ab
AC
2687 if (deprecated_target_resume_hook)
2688 (*deprecated_target_resume_hook) ();
43ff13b4 2689
506fb367
DJ
2690 /* The vCont packet doesn't need to specify threads via Hc. */
2691 if (remote_vcont_resume (ptid, step, siggnal))
2692 return;
2693
2694 /* All other supported resume packets do use Hc, so call set_thread. */
2695 if (pid == -1)
23860348 2696 set_thread (0, 0); /* Run any thread. */
506fb367 2697 else
23860348 2698 set_thread (pid, 0); /* Run this thread. */
506fb367 2699
43ff13b4
JM
2700 if (siggnal != TARGET_SIGNAL_0)
2701 {
2702 buf[0] = step ? 'S' : 'C';
c5aa993b 2703 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
506fb367 2704 buf[2] = tohex (((int) siggnal) & 0xf);
43ff13b4
JM
2705 buf[3] = '\0';
2706 }
2707 else
c5aa993b 2708 strcpy (buf, step ? "s" : "c");
506fb367 2709
44eaed12 2710 putpkt (buf);
506fb367
DJ
2711}
2712
23860348 2713/* Same as remote_resume, but with async support. */
506fb367
DJ
2714static void
2715remote_async_resume (ptid_t ptid, int step, enum target_signal siggnal)
2716{
2717 remote_resume (ptid, step, siggnal);
43ff13b4 2718
2acceee2
JM
2719 /* We are about to start executing the inferior, let's register it
2720 with the event loop. NOTE: this is the one place where all the
2721 execution commands end up. We could alternatively do this in each
23860348 2722 of the execution commands in infcmd.c. */
2acceee2
JM
2723 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
2724 into infcmd.c in order to allow inferior function calls to work
23860348 2725 NOT asynchronously. */
362646f5 2726 if (target_can_async_p ())
2acceee2 2727 target_async (inferior_event_handler, 0);
23860348 2728 /* Tell the world that the target is now executing. */
2acceee2
JM
2729 /* FIXME: cagney/1999-09-23: Is it the targets responsibility to set
2730 this? Instead, should the client of target just assume (for
2731 async targets) that the target is going to start executing? Is
2732 this information already found in the continuation block? */
ed9a39eb 2733 if (target_is_async_p ())
2acceee2 2734 target_executing = 1;
43ff13b4 2735}
c906108c 2736\f
43ff13b4
JM
2737
2738/* Set up the signal handler for SIGINT, while the target is
23860348 2739 executing, ovewriting the 'regular' SIGINT signal handler. */
43ff13b4 2740static void
fba45db2 2741initialize_sigint_signal_handler (void)
43ff13b4 2742{
c5aa993b 2743 sigint_remote_token =
43ff13b4
JM
2744 create_async_signal_handler (async_remote_interrupt, NULL);
2745 signal (SIGINT, handle_remote_sigint);
2746}
2747
23860348 2748/* Signal handler for SIGINT, while the target is executing. */
43ff13b4 2749static void
fba45db2 2750handle_remote_sigint (int sig)
43ff13b4
JM
2751{
2752 signal (sig, handle_remote_sigint_twice);
c5aa993b 2753 sigint_remote_twice_token =
43ff13b4
JM
2754 create_async_signal_handler (async_remote_interrupt_twice, NULL);
2755 mark_async_signal_handler_wrapper (sigint_remote_token);
2756}
2757
2758/* Signal handler for SIGINT, installed after SIGINT has already been
2759 sent once. It will take effect the second time that the user sends
23860348 2760 a ^C. */
43ff13b4 2761static void
fba45db2 2762handle_remote_sigint_twice (int sig)
43ff13b4
JM
2763{
2764 signal (sig, handle_sigint);
c5aa993b 2765 sigint_remote_twice_token =
2df3850c 2766 create_async_signal_handler (inferior_event_handler_wrapper, NULL);
43ff13b4
JM
2767 mark_async_signal_handler_wrapper (sigint_remote_twice_token);
2768}
2769
6426a772 2770/* Perform the real interruption of the target execution, in response
23860348 2771 to a ^C. */
c5aa993b 2772static void
fba45db2 2773async_remote_interrupt (gdb_client_data arg)
43ff13b4
JM
2774{
2775 if (remote_debug)
2776 fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
2777
2778 target_stop ();
2779}
2780
2781/* Perform interrupt, if the first attempt did not succeed. Just give
23860348 2782 up on the target alltogether. */
2df3850c 2783void
fba45db2 2784async_remote_interrupt_twice (gdb_client_data arg)
43ff13b4 2785{
2df3850c
JM
2786 if (remote_debug)
2787 fprintf_unfiltered (gdb_stdlog, "remote_interrupt_twice called\n");
6426a772 2788 /* Do something only if the target was not killed by the previous
23860348 2789 cntl-C. */
6426a772
JM
2790 if (target_executing)
2791 {
2792 interrupt_query ();
2793 signal (SIGINT, handle_remote_sigint);
2794 }
43ff13b4
JM
2795}
2796
2797/* Reinstall the usual SIGINT handlers, after the target has
23860348 2798 stopped. */
6426a772
JM
2799static void
2800cleanup_sigint_signal_handler (void *dummy)
43ff13b4
JM
2801{
2802 signal (SIGINT, handle_sigint);
2803 if (sigint_remote_twice_token)
23860348
MS
2804 delete_async_signal_handler ((struct async_signal_handler **)
2805 &sigint_remote_twice_token);
43ff13b4 2806 if (sigint_remote_token)
23860348
MS
2807 delete_async_signal_handler ((struct async_signal_handler **)
2808 &sigint_remote_token);
43ff13b4
JM
2809}
2810
c906108c
SS
2811/* Send ^C to target to halt it. Target will respond, and send us a
2812 packet. */
507f3c78 2813static void (*ofunc) (int);
c906108c 2814
7a292a7a
SS
2815/* The command line interface's stop routine. This function is installed
2816 as a signal handler for SIGINT. The first time a user requests a
2817 stop, we call remote_stop to send a break or ^C. If there is no
2818 response from the target (it didn't stop when the user requested it),
23860348 2819 we ask the user if he'd like to detach from the target. */
c906108c 2820static void
fba45db2 2821remote_interrupt (int signo)
c906108c 2822{
23860348 2823 /* If this doesn't work, try more severe steps. */
7a292a7a
SS
2824 signal (signo, remote_interrupt_twice);
2825
2826 if (remote_debug)
0f71a2f6 2827 fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
7a292a7a
SS
2828
2829 target_stop ();
2830}
2831
2832/* The user typed ^C twice. */
2833
2834static void
fba45db2 2835remote_interrupt_twice (int signo)
7a292a7a
SS
2836{
2837 signal (signo, ofunc);
2838 interrupt_query ();
c906108c
SS
2839 signal (signo, remote_interrupt);
2840}
7a292a7a
SS
2841
2842/* This is the generic stop called via the target vector. When a target
2843 interrupt is requested, either by the command line or the GUI, we
23860348 2844 will eventually end up here. */
c906108c 2845static void
fba45db2 2846remote_stop (void)
c906108c 2847{
7a292a7a
SS
2848 /* Send a break or a ^C, depending on user preference. */
2849 if (remote_debug)
0f71a2f6 2850 fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
c906108c 2851
7a292a7a 2852 if (remote_break)
2cd58942 2853 serial_send_break (remote_desc);
c906108c 2854 else
2cd58942 2855 serial_write (remote_desc, "\003", 1);
c906108c
SS
2856}
2857
2858/* Ask the user what to do when an interrupt is received. */
2859
2860static void
fba45db2 2861interrupt_query (void)
c906108c
SS
2862{
2863 target_terminal_ours ();
2864
2865 if (query ("Interrupted while waiting for the program.\n\
2866Give up (and stop debugging it)? "))
2867 {
2868 target_mourn_inferior ();
315a522e 2869 deprecated_throw_reason (RETURN_QUIT);
c906108c
SS
2870 }
2871
2872 target_terminal_inferior ();
2873}
2874
6426a772
JM
2875/* Enable/disable target terminal ownership. Most targets can use
2876 terminal groups to control terminal ownership. Remote targets are
2877 different in that explicit transfer of ownership to/from GDB/target
23860348 2878 is required. */
6426a772
JM
2879
2880static void
2881remote_async_terminal_inferior (void)
2882{
2883 /* FIXME: cagney/1999-09-27: Shouldn't need to test for
2884 sync_execution here. This function should only be called when
2885 GDB is resuming the inferior in the forground. A background
2886 resume (``run&'') should leave GDB in control of the terminal and
23860348 2887 consequently should not call this code. */
6426a772
JM
2888 if (!sync_execution)
2889 return;
2890 /* FIXME: cagney/1999-09-27: Closely related to the above. Make
2891 calls target_terminal_*() idenpotent. The event-loop GDB talking
2892 to an asynchronous target with a synchronous command calls this
2893 function from both event-top.c and infrun.c/infcmd.c. Once GDB
2894 stops trying to transfer the terminal to the target when it
2895 shouldn't this guard can go away. */
2896 if (!remote_async_terminal_ours_p)
2897 return;
2898 delete_file_handler (input_fd);
2899 remote_async_terminal_ours_p = 0;
2900 initialize_sigint_signal_handler ();
2901 /* NOTE: At this point we could also register our selves as the
2902 recipient of all input. Any characters typed could then be
23860348 2903 passed on down to the target. */
6426a772
JM
2904}
2905
2906static void
2907remote_async_terminal_ours (void)
2908{
23860348 2909 /* See FIXME in remote_async_terminal_inferior. */
6426a772
JM
2910 if (!sync_execution)
2911 return;
23860348 2912 /* See FIXME in remote_async_terminal_inferior. */
6426a772
JM
2913 if (remote_async_terminal_ours_p)
2914 return;
2915 cleanup_sigint_signal_handler (NULL);
2916 add_file_handler (input_fd, stdin_event_handler, 0);
2917 remote_async_terminal_ours_p = 1;
2918}
2919
c906108c
SS
2920/* If nonzero, ignore the next kill. */
2921
2922int kill_kludge;
2923
2924void
917317f4 2925remote_console_output (char *msg)
c906108c
SS
2926{
2927 char *p;
2928
c5aa993b 2929 for (p = msg; p[0] && p[1]; p += 2)
c906108c
SS
2930 {
2931 char tb[2];
2932 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
2933 tb[0] = c;
2934 tb[1] = 0;
43ff13b4 2935 fputs_unfiltered (tb, gdb_stdtarg);
c906108c 2936 }
917317f4 2937 gdb_flush (gdb_stdtarg);
c906108c
SS
2938}
2939
0f71a2f6
JM
2940/* Wait until the remote machine stops, then return,
2941 storing status in STATUS just as `wait' would.
802188a7 2942 Returns "pid", which in the case of a multi-threaded
0f71a2f6 2943 remote OS, is the thread-id. */
c906108c 2944
39f77062
KB
2945static ptid_t
2946remote_wait (ptid_t ptid, struct target_waitstatus *status)
c906108c 2947{
d01949b6 2948 struct remote_state *rs = get_remote_state ();
ea9c271d 2949 struct remote_arch_state *rsa = get_remote_arch_state ();
6d820c5c 2950 char *buf = rs->buf;
b2dd6311 2951 ULONGEST thread_num = -1;
3c3bea1c 2952 ULONGEST addr;
c906108c
SS
2953
2954 status->kind = TARGET_WAITKIND_EXITED;
2955 status->value.integer = 0;
2956
2957 while (1)
2958 {
cfd77fa1 2959 char *p;
c906108c 2960
c906108c 2961 ofunc = signal (SIGINT, remote_interrupt);
6d820c5c 2962 getpkt (&rs->buf, &rs->buf_size, 1);
c906108c
SS
2963 signal (SIGINT, ofunc);
2964
2965 /* This is a hook for when we need to do something (perhaps the
c5aa993b 2966 collection of trace data) every time the target stops. */
9a4105ab
AC
2967 if (deprecated_target_wait_loop_hook)
2968 (*deprecated_target_wait_loop_hook) ();
c906108c 2969
3c3bea1c
GS
2970 remote_stopped_by_watchpoint_p = 0;
2971
c906108c
SS
2972 switch (buf[0])
2973 {
23860348 2974 case 'E': /* Error of some sort. */
8a3fe4f8 2975 warning (_("Remote failure reply: %s"), buf);
c906108c 2976 continue;
23860348 2977 case 'F': /* File-I/O request. */
449092f6
CV
2978 remote_fileio_request (buf);
2979 continue;
23860348 2980 case 'T': /* Status with PC, SP, FP, ... */
c906108c 2981 {
cfd77fa1 2982 gdb_byte regs[MAX_REGISTER_SIZE];
c906108c 2983
23860348 2984 /* Expedited reply, containing Signal, {regno, reg} repeat. */
c906108c 2985 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
c5aa993b
JM
2986 ss = signal number
2987 n... = register number
2988 r... = register contents
2989 */
c906108c
SS
2990 p = &buf[3]; /* after Txx */
2991
2992 while (*p)
2993 {
cfd77fa1 2994 char *p1;
c906108c 2995 char *p_temp;
97345198 2996 int fieldsize;
3c3bea1c
GS
2997 LONGEST pnum = 0;
2998
23860348
MS
2999 /* If the packet contains a register number save it in
3000 pnum and set p1 to point to the character following
3001 it. Otherwise p1 points to p. */
c906108c 3002
23860348
MS
3003 /* If this packet is an awatch packet, don't parse the
3004 'a' as a register number. */
3c3bea1c
GS
3005
3006 if (strncmp (p, "awatch", strlen("awatch")) != 0)
3007 {
3008 /* Read the ``P'' register number. */
3009 pnum = strtol (p, &p_temp, 16);
cfd77fa1 3010 p1 = p_temp;
3c3bea1c 3011 }
802188a7 3012 else
3c3bea1c 3013 p1 = p;
c906108c 3014
23860348 3015 if (p1 == p) /* No register number present here. */
c906108c 3016 {
cfd77fa1 3017 p1 = strchr (p, ':');
c906108c 3018 if (p1 == NULL)
670aa98f 3019 error (_("Malformed packet(a) (missing colon): %s\n\
8a3fe4f8 3020Packet: '%s'\n"),
670aa98f 3021 p, buf);
3c3bea1c 3022 if (strncmp (p, "thread", p1 - p) == 0)
c906108c
SS
3023 {
3024 p_temp = unpack_varlen_hex (++p1, &thread_num);
3025 record_currthread (thread_num);
cfd77fa1 3026 p = p_temp;
c906108c 3027 }
3c3bea1c
GS
3028 else if ((strncmp (p, "watch", p1 - p) == 0)
3029 || (strncmp (p, "rwatch", p1 - p) == 0)
3030 || (strncmp (p, "awatch", p1 - p) == 0))
3031 {
3032 remote_stopped_by_watchpoint_p = 1;
3033 p = unpack_varlen_hex (++p1, &addr);
3034 remote_watch_data_address = (CORE_ADDR)addr;
3035 }
3036 else
3037 {
3038 /* Silently skip unknown optional info. */
3039 p_temp = strchr (p1 + 1, ';');
3040 if (p_temp)
cfd77fa1 3041 p = p_temp;
3c3bea1c 3042 }
c906108c
SS
3043 }
3044 else
3045 {
ea9c271d 3046 struct packet_reg *reg = packet_reg_from_pnum (rsa, pnum);
c906108c
SS
3047 p = p1;
3048
3049 if (*p++ != ':')
8a3fe4f8
AC
3050 error (_("Malformed packet(b) (missing colon): %s\n\
3051Packet: '%s'\n"),
3fcb8548 3052 p, buf);
c906108c 3053
ad10f812 3054 if (reg == NULL)
8a3fe4f8
AC
3055 error (_("Remote sent bad register number %s: %s\n\
3056Packet: '%s'\n"),
3fcb8548 3057 phex_nz (pnum, 0), p, buf);
c906108c 3058
cfd77fa1 3059 fieldsize = hex2bin (p, regs,
23860348
MS
3060 register_size (current_gdbarch,
3061 reg->regnum));
97345198 3062 p += 2 * fieldsize;
23860348
MS
3063 if (fieldsize < register_size (current_gdbarch,
3064 reg->regnum))
8a3fe4f8 3065 warning (_("Remote reply is too short: %s"), buf);
23860348
MS
3066 regcache_raw_supply (current_regcache,
3067 reg->regnum, regs);
c906108c
SS
3068 }
3069
3070 if (*p++ != ';')
8a3fe4f8 3071 error (_("Remote register badly formatted: %s\nhere: %s"),
23860348 3072 buf, p);
c906108c
SS
3073 }
3074 }
3075 /* fall through */
23860348 3076 case 'S': /* Old style status, just signal only. */
c906108c
SS
3077 status->kind = TARGET_WAITKIND_STOPPED;
3078 status->value.sig = (enum target_signal)
3079 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
3080
0f71a2f6
JM
3081 if (buf[3] == 'p')
3082 {
0f71a2f6
JM
3083 thread_num = strtol ((const char *) &buf[4], NULL, 16);
3084 record_currthread (thread_num);
3085 }
c906108c 3086 goto got_status;
23860348 3087 case 'W': /* Target exited. */
c906108c
SS
3088 {
3089 /* The remote process exited. */
3090 status->kind = TARGET_WAITKIND_EXITED;
3091 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
3092 goto got_status;
3093 }
3094 case 'X':
3095 status->kind = TARGET_WAITKIND_SIGNALLED;
3096 status->value.sig = (enum target_signal)
3097 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
3098 kill_kludge = 1;
3099
3100 goto got_status;
23860348 3101 case 'O': /* Console output. */
c906108c
SS
3102 remote_console_output (buf + 1);
3103 continue;
3104 case '\0':
3105 if (last_sent_signal != TARGET_SIGNAL_0)
3106 {
3107 /* Zero length reply means that we tried 'S' or 'C' and
c5aa993b 3108 the remote system doesn't support it. */
c906108c
SS
3109 target_terminal_ours_for_output ();
3110 printf_filtered
3111 ("Can't send signals to this remote system. %s not sent.\n",
3112 target_signal_to_name (last_sent_signal));
3113 last_sent_signal = TARGET_SIGNAL_0;
3114 target_terminal_inferior ();
3115
3116 strcpy ((char *) buf, last_sent_step ? "s" : "c");
3117 putpkt ((char *) buf);
3118 continue;
3119 }
3120 /* else fallthrough */
3121 default:
8a3fe4f8 3122 warning (_("Invalid remote reply: %s"), buf);
c906108c
SS
3123 continue;
3124 }
3125 }
c5aa993b 3126got_status:
c906108c
SS
3127 if (thread_num != -1)
3128 {
39f77062 3129 return pid_to_ptid (thread_num);
c906108c 3130 }
39f77062 3131 return inferior_ptid;
c906108c
SS
3132}
3133
23860348 3134/* Async version of remote_wait. */
39f77062
KB
3135static ptid_t
3136remote_async_wait (ptid_t ptid, struct target_waitstatus *status)
43ff13b4 3137{
d01949b6 3138 struct remote_state *rs = get_remote_state ();
ea9c271d 3139 struct remote_arch_state *rsa = get_remote_arch_state ();
6d820c5c 3140 char *buf = rs->buf;
b2dd6311 3141 ULONGEST thread_num = -1;
3c3bea1c 3142 ULONGEST addr;
43ff13b4
JM
3143
3144 status->kind = TARGET_WAITKIND_EXITED;
3145 status->value.integer = 0;
3146
3c3bea1c
GS
3147 remote_stopped_by_watchpoint_p = 0;
3148
43ff13b4
JM
3149 while (1)
3150 {
cfd77fa1 3151 char *p;
c5aa993b 3152
ed9a39eb 3153 if (!target_is_async_p ())
43ff13b4 3154 ofunc = signal (SIGINT, remote_interrupt);
6426a772
JM
3155 /* FIXME: cagney/1999-09-27: If we're in async mode we should
3156 _never_ wait for ever -> test on target_is_async_p().
3157 However, before we do that we need to ensure that the caller
23860348 3158 knows how to take the target into/out of async mode. */
6d820c5c 3159 getpkt (&rs->buf, &rs->buf_size, wait_forever_enabled_p);
ed9a39eb 3160 if (!target_is_async_p ())
43ff13b4
JM
3161 signal (SIGINT, ofunc);
3162
3163 /* This is a hook for when we need to do something (perhaps the
c5aa993b 3164 collection of trace data) every time the target stops. */
9a4105ab
AC
3165 if (deprecated_target_wait_loop_hook)
3166 (*deprecated_target_wait_loop_hook) ();
43ff13b4
JM
3167
3168 switch (buf[0])
3169 {
23860348 3170 case 'E': /* Error of some sort. */
8a3fe4f8 3171 warning (_("Remote failure reply: %s"), buf);
43ff13b4 3172 continue;
23860348 3173 case 'F': /* File-I/O request. */
449092f6
CV
3174 remote_fileio_request (buf);
3175 continue;
23860348 3176 case 'T': /* Status with PC, SP, FP, ... */
43ff13b4 3177 {
cfd77fa1 3178 gdb_byte regs[MAX_REGISTER_SIZE];
43ff13b4 3179
23860348 3180 /* Expedited reply, containing Signal, {regno, reg} repeat. */
43ff13b4 3181 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
c5aa993b
JM
3182 ss = signal number
3183 n... = register number
3184 r... = register contents
3185 */
43ff13b4
JM
3186 p = &buf[3]; /* after Txx */
3187
3188 while (*p)
3189 {
cfd77fa1 3190 char *p1;
43ff13b4 3191 char *p_temp;
6c3f2dbf 3192 int fieldsize;
3c3bea1c 3193 long pnum = 0;
43ff13b4 3194
23860348
MS
3195 /* If the packet contains a register number, save it
3196 in pnum and set p1 to point to the character
3197 following it. Otherwise p1 points to p. */
3c3bea1c
GS
3198
3199 /* If this packet is an awatch packet, don't parse the 'a'
3200 as a register number. */
802188a7 3201
3c3bea1c
GS
3202 if (!strncmp (p, "awatch", strlen ("awatch")) != 0)
3203 {
3204 /* Read the register number. */
3205 pnum = strtol (p, &p_temp, 16);
cfd77fa1 3206 p1 = p_temp;
3c3bea1c 3207 }
802188a7 3208 else
3c3bea1c 3209 p1 = p;
43ff13b4 3210
23860348 3211 if (p1 == p) /* No register number present here. */
43ff13b4 3212 {
cfd77fa1 3213 p1 = strchr (p, ':');
43ff13b4 3214 if (p1 == NULL)
8a3fe4f8
AC
3215 error (_("Malformed packet(a) (missing colon): %s\n\
3216Packet: '%s'\n"),
3fcb8548 3217 p, buf);
3c3bea1c 3218 if (strncmp (p, "thread", p1 - p) == 0)
43ff13b4
JM
3219 {
3220 p_temp = unpack_varlen_hex (++p1, &thread_num);
3221 record_currthread (thread_num);
cfd77fa1 3222 p = p_temp;
43ff13b4 3223 }
3c3bea1c
GS
3224 else if ((strncmp (p, "watch", p1 - p) == 0)
3225 || (strncmp (p, "rwatch", p1 - p) == 0)
3226 || (strncmp (p, "awatch", p1 - p) == 0))
3227 {
3228 remote_stopped_by_watchpoint_p = 1;
3229 p = unpack_varlen_hex (++p1, &addr);
3230 remote_watch_data_address = (CORE_ADDR)addr;
3231 }
3232 else
3233 {
3234 /* Silently skip unknown optional info. */
cfd77fa1 3235 p_temp = strchr (p1 + 1, ';');
3c3bea1c
GS
3236 if (p_temp)
3237 p = p_temp;
3238 }
43ff13b4 3239 }
802188a7 3240
43ff13b4
JM
3241 else
3242 {
ea9c271d 3243 struct packet_reg *reg = packet_reg_from_pnum (rsa, pnum);
43ff13b4 3244 p = p1;
43ff13b4 3245 if (*p++ != ':')
8a3fe4f8
AC
3246 error (_("Malformed packet(b) (missing colon): %s\n\
3247Packet: '%s'\n"),
3fcb8548 3248 p, buf);
43ff13b4 3249
ad10f812 3250 if (reg == NULL)
8a3fe4f8
AC
3251 error (_("Remote sent bad register number %ld: %s\n\
3252Packet: '%s'\n"),
3fcb8548 3253 pnum, p, buf);
43ff13b4 3254
cfd77fa1 3255 fieldsize = hex2bin (p, regs,
23860348
MS
3256 register_size (current_gdbarch,
3257 reg->regnum));
6c3f2dbf 3258 p += 2 * fieldsize;
23860348
MS
3259 if (fieldsize < register_size (current_gdbarch,
3260 reg->regnum))
8a3fe4f8 3261 warning (_("Remote reply is too short: %s"), buf);
23a6d369 3262 regcache_raw_supply (current_regcache, reg->regnum, regs);
43ff13b4
JM
3263 }
3264
3265 if (*p++ != ';')
8a3fe4f8 3266 error (_("Remote register badly formatted: %s\nhere: %s"),
0a2cfde4 3267 buf, p);
43ff13b4
JM
3268 }
3269 }
3270 /* fall through */
23860348 3271 case 'S': /* Old style status, just signal only. */
43ff13b4
JM
3272 status->kind = TARGET_WAITKIND_STOPPED;
3273 status->value.sig = (enum target_signal)
3274 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
3275
3276 if (buf[3] == 'p')
3277 {
43ff13b4
JM
3278 thread_num = strtol ((const char *) &buf[4], NULL, 16);
3279 record_currthread (thread_num);
3280 }
43ff13b4 3281 goto got_status;
23860348 3282 case 'W': /* Target exited. */
43ff13b4
JM
3283 {
3284 /* The remote process exited. */
3285 status->kind = TARGET_WAITKIND_EXITED;
3286 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
3287 goto got_status;
3288 }
3289 case 'X':
3290 status->kind = TARGET_WAITKIND_SIGNALLED;
3291 status->value.sig = (enum target_signal)
3292 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
3293 kill_kludge = 1;
3294
3295 goto got_status;
23860348 3296 case 'O': /* Console output. */
43ff13b4 3297 remote_console_output (buf + 1);
c4093a6a 3298 /* Return immediately to the event loop. The event loop will
23860348 3299 still be waiting on the inferior afterwards. */
c4093a6a
JM
3300 status->kind = TARGET_WAITKIND_IGNORE;
3301 goto got_status;
43ff13b4
JM
3302 case '\0':
3303 if (last_sent_signal != TARGET_SIGNAL_0)
3304 {
3305 /* Zero length reply means that we tried 'S' or 'C' and
c5aa993b 3306 the remote system doesn't support it. */
43ff13b4
JM
3307 target_terminal_ours_for_output ();
3308 printf_filtered
3309 ("Can't send signals to this remote system. %s not sent.\n",
3310 target_signal_to_name (last_sent_signal));
3311 last_sent_signal = TARGET_SIGNAL_0;
3312 target_terminal_inferior ();
3313
3314 strcpy ((char *) buf, last_sent_step ? "s" : "c");
3315 putpkt ((char *) buf);
3316 continue;
3317 }
3318 /* else fallthrough */
3319 default:
8a3fe4f8 3320 warning (_("Invalid remote reply: %s"), buf);
43ff13b4
JM
3321 continue;
3322 }
3323 }
c5aa993b 3324got_status:
43ff13b4
JM
3325 if (thread_num != -1)
3326 {
39f77062 3327 return pid_to_ptid (thread_num);
43ff13b4 3328 }
39f77062 3329 return inferior_ptid;
43ff13b4
JM
3330}
3331
c906108c
SS
3332/* Number of bytes of registers this stub implements. */
3333
3334static int register_bytes_found;
3335
3336/* Read the remote registers into the block REGS. */
ad10f812 3337/* Currently we just read all the registers, so we don't use regnum. */
c906108c 3338
b96ec7ac
AC
3339static int
3340fetch_register_using_p (int regnum)
3341{
3342 struct remote_state *rs = get_remote_state ();
6d820c5c 3343 char *buf = rs->buf, *p;
b96ec7ac
AC
3344 char regp[MAX_REGISTER_SIZE];
3345 int i;
3346
fcad0fa4
JB
3347 p = buf;
3348 *p++ = 'p';
3349 p += hexnumstr (p, regnum);
3350 *p++ = '\0';
6d820c5c 3351 remote_send (&rs->buf, &rs->buf_size);
3f9a994c
JB
3352
3353 /* If the stub didn't recognize the packet, or if we got an error,
3354 tell our caller. */
3355 if (buf[0] == '\0' || buf[0] == 'E')
3356 return 0;
3357
3358 /* If this register is unfetchable, tell the regcache. */
3359 if (buf[0] == 'x')
8480adf2 3360 {
3f9a994c
JB
3361 regcache_raw_supply (current_regcache, regnum, NULL);
3362 set_register_cached (regnum, -1);
8480adf2 3363 return 1;
b96ec7ac 3364 }
b96ec7ac 3365
3f9a994c
JB
3366 /* Otherwise, parse and supply the value. */
3367 p = buf;
3368 i = 0;
3369 while (p[0] != 0)
3370 {
3371 if (p[1] == 0)
3372 {
8a3fe4f8 3373 error (_("fetch_register_using_p: early buf termination"));
3f9a994c
JB
3374 return 0;
3375 }
3376
3377 regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]);
3378 p += 2;
3379 }
3380 regcache_raw_supply (current_regcache, regnum, regp);
3381 return 1;
b96ec7ac
AC
3382}
3383
c906108c 3384static void
ad10f812 3385remote_fetch_registers (int regnum)
c906108c 3386{
d01949b6 3387 struct remote_state *rs = get_remote_state ();
ea9c271d 3388 struct remote_arch_state *rsa = get_remote_arch_state ();
6d820c5c 3389 char *buf = rs->buf;
c906108c
SS
3390 int i;
3391 char *p;
ea9c271d 3392 char *regs = alloca (rsa->sizeof_g_packet);
c906108c 3393
39f77062 3394 set_thread (PIDGET (inferior_ptid), 1);
c906108c 3395
b323314b
AC
3396 if (regnum >= 0)
3397 {
ea9c271d 3398 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
b323314b
AC
3399 gdb_assert (reg != NULL);
3400 if (!reg->in_g_packet)
3401 internal_error (__FILE__, __LINE__,
e2e0b3e5
AC
3402 _("Attempt to fetch a non G-packet register when this "
3403 "remote.c does not support the p-packet."));
b323314b 3404 }
444abaca 3405 switch (remote_protocol_packets[PACKET_p].support)
b96ec7ac
AC
3406 {
3407 case PACKET_DISABLE:
3408 break;
3409 case PACKET_ENABLE:
3410 if (fetch_register_using_p (regnum))
3411 return;
3412 else
8a3fe4f8 3413 error (_("Protocol error: p packet not recognized by stub"));
b96ec7ac
AC
3414 case PACKET_SUPPORT_UNKNOWN:
3415 if (fetch_register_using_p (regnum))
3416 {
3417 /* The stub recognized the 'p' packet. Remember this. */
444abaca 3418 remote_protocol_packets[PACKET_p].support = PACKET_ENABLE;
b96ec7ac
AC
3419 return;
3420 }
3421 else
3422 {
3423 /* The stub does not support the 'P' packet. Use 'G'
3424 instead, and don't try using 'P' in the future (it
3425 will just waste our time). */
444abaca 3426 remote_protocol_packets[PACKET_p].support = PACKET_DISABLE;
b96ec7ac
AC
3427 break;
3428 }
3429 }
b323314b 3430
c906108c 3431 sprintf (buf, "g");
6d820c5c 3432 remote_send (&rs->buf, &rs->buf_size);
c906108c 3433
11cf8741
JM
3434 /* Save the size of the packet sent to us by the target. Its used
3435 as a heuristic when determining the max size of packets that the
23860348 3436 target can safely receive. */
ea9c271d
DJ
3437 if ((rsa->actual_register_packet_size) == 0)
3438 (rsa->actual_register_packet_size) = strlen (buf);
c906108c
SS
3439
3440 /* Unimplemented registers read as all bits zero. */
ea9c271d 3441 memset (regs, 0, rsa->sizeof_g_packet);
c906108c
SS
3442
3443 /* We can get out of synch in various cases. If the first character
3444 in the buffer is not a hex character, assume that has happened
3445 and try to fetch another packet to read. */
3446 while ((buf[0] < '0' || buf[0] > '9')
3de11b2e 3447 && (buf[0] < 'A' || buf[0] > 'F')
c906108c 3448 && (buf[0] < 'a' || buf[0] > 'f')
23860348 3449 && buf[0] != 'x') /* New: unavailable register value. */
c906108c
SS
3450 {
3451 if (remote_debug)
0f71a2f6
JM
3452 fprintf_unfiltered (gdb_stdlog,
3453 "Bad register packet; fetching a new packet\n");
6d820c5c 3454 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c
SS
3455 }
3456
3457 /* Reply describes registers byte by byte, each byte encoded as two
3458 hex characters. Suck them all up, then supply them to the
3459 register cacheing/storage mechanism. */
3460
3461 p = buf;
ea9c271d 3462 for (i = 0; i < rsa->sizeof_g_packet; i++)
c906108c
SS
3463 {
3464 if (p[0] == 0)
3465 break;
3466 if (p[1] == 0)
3467 {
8a3fe4f8 3468 warning (_("Remote reply is of odd length: %s"), buf);
c906108c
SS
3469 /* Don't change register_bytes_found in this case, and don't
3470 print a second warning. */
3471 goto supply_them;
3472 }
3473 if (p[0] == 'x' && p[1] == 'x')
c5aa993b 3474 regs[i] = 0; /* 'x' */
c906108c
SS
3475 else
3476 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
3477 p += 2;
3478 }
3479
3480 if (i != register_bytes_found)
3481 {
3482 register_bytes_found = i;
2649061d
AC
3483 if (REGISTER_BYTES_OK_P ()
3484 && !REGISTER_BYTES_OK (i))
8a3fe4f8 3485 warning (_("Remote reply is too short: %s"), buf);
c906108c 3486 }
c5aa993b 3487
b323314b 3488 supply_them:
ad10f812 3489 {
b323314b
AC
3490 int i;
3491 for (i = 0; i < NUM_REGS + NUM_PSEUDO_REGS; i++)
ad10f812 3492 {
ea9c271d 3493 struct packet_reg *r = &rsa->regs[i];
b323314b
AC
3494 if (r->in_g_packet)
3495 {
8ccc1287
AC
3496 if (r->offset * 2 >= strlen (buf))
3497 /* A short packet that didn't include the register's
3498 value, this implies that the register is zero (and
3499 not that the register is unavailable). Supply that
3500 zero value. */
3501 regcache_raw_supply (current_regcache, r->regnum, NULL);
3502 else if (buf[r->offset * 2] == 'x')
3503 {
3504 gdb_assert (r->offset * 2 < strlen (buf));
3505 /* The register isn't available, mark it as such (at
3506 the same time setting the value to zero). */
3507 regcache_raw_supply (current_regcache, r->regnum, NULL);
3508 set_register_cached (i, -1);
3509 }
3510 else
3511 regcache_raw_supply (current_regcache, r->regnum,
3512 regs + r->offset);
b323314b 3513 }
ad10f812
AC
3514 }
3515 }
c906108c
SS
3516}
3517
3518/* Prepare to store registers. Since we may send them all (using a
3519 'G' request), we have to read out the ones we don't want to change
3520 first. */
3521
c5aa993b 3522static void
fba45db2 3523remote_prepare_to_store (void)
c906108c 3524{
ea9c271d 3525 struct remote_arch_state *rsa = get_remote_arch_state ();
cf0e1e0d 3526 int i;
cfd77fa1 3527 gdb_byte buf[MAX_REGISTER_SIZE];
cf0e1e0d 3528
c906108c 3529 /* Make sure the entire registers array is valid. */
444abaca 3530 switch (remote_protocol_packets[PACKET_P].support)
5a2468f5
JM
3531 {
3532 case PACKET_DISABLE:
3533 case PACKET_SUPPORT_UNKNOWN:
cf0e1e0d
DJ
3534 /* Make sure all the necessary registers are cached. */
3535 for (i = 0; i < NUM_REGS; i++)
ea9c271d
DJ
3536 if (rsa->regs[i].in_g_packet)
3537 regcache_raw_read (current_regcache, rsa->regs[i].regnum, buf);
5a2468f5
JM
3538 break;
3539 case PACKET_ENABLE:
3540 break;
3541 }
3542}
3543
ad10f812 3544/* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
23860348 3545 packet was not recognized. */
5a2468f5
JM
3546
3547static int
ad10f812 3548store_register_using_P (int regnum)
5a2468f5 3549{
d01949b6 3550 struct remote_state *rs = get_remote_state ();
ea9c271d
DJ
3551 struct remote_arch_state *rsa = get_remote_arch_state ();
3552 struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum);
5a2468f5 3553 /* Try storing a single register. */
6d820c5c 3554 char *buf = rs->buf;
cfd77fa1 3555 gdb_byte regp[MAX_REGISTER_SIZE];
5a2468f5 3556 char *p;
5a2468f5 3557
ea9c271d 3558 xsnprintf (buf, get_remote_packet_size (), "P%s=", phex_nz (reg->pnum, 0));
5a2468f5 3559 p = buf + strlen (buf);
822c9732 3560 regcache_raw_collect (current_regcache, reg->regnum, regp);
3acba339 3561 bin2hex (regp, p, register_size (current_gdbarch, reg->regnum));
6d820c5c 3562 remote_send (&rs->buf, &rs->buf_size);
5a2468f5
JM
3563
3564 return buf[0] != '\0';
c906108c
SS
3565}
3566
5a2468f5 3567
23860348
MS
3568/* Store register REGNUM, or all registers if REGNUM == -1, from the
3569 contents of the register cache buffer. FIXME: ignores errors. */
c906108c
SS
3570
3571static void
ad10f812 3572remote_store_registers (int regnum)
c906108c 3573{
d01949b6 3574 struct remote_state *rs = get_remote_state ();
ea9c271d 3575 struct remote_arch_state *rsa = get_remote_arch_state ();
cfd77fa1 3576 gdb_byte *regs;
c906108c
SS
3577 char *p;
3578
39f77062 3579 set_thread (PIDGET (inferior_ptid), 1);
c906108c 3580
ad10f812 3581 if (regnum >= 0)
c906108c 3582 {
444abaca 3583 switch (remote_protocol_packets[PACKET_P].support)
c906108c 3584 {
5a2468f5
JM
3585 case PACKET_DISABLE:
3586 break;
3587 case PACKET_ENABLE:
ad10f812 3588 if (store_register_using_P (regnum))
5a2468f5
JM
3589 return;
3590 else
8a3fe4f8 3591 error (_("Protocol error: P packet not recognized by stub"));
5a2468f5 3592 case PACKET_SUPPORT_UNKNOWN:
ad10f812 3593 if (store_register_using_P (regnum))
5a2468f5
JM
3594 {
3595 /* The stub recognized the 'P' packet. Remember this. */
444abaca 3596 remote_protocol_packets[PACKET_P].support = PACKET_ENABLE;
5a2468f5
JM
3597 return;
3598 }
3599 else
3600 {
3601 /* The stub does not support the 'P' packet. Use 'G'
3602 instead, and don't try using 'P' in the future (it
3603 will just waste our time). */
444abaca 3604 remote_protocol_packets[PACKET_P].support = PACKET_DISABLE;
5a2468f5
JM
3605 break;
3606 }
c906108c 3607 }
c906108c
SS
3608 }
3609
193cb69f
AC
3610 /* Extract all the registers in the regcache copying them into a
3611 local buffer. */
3612 {
b323314b 3613 int i;
ea9c271d
DJ
3614 regs = alloca (rsa->sizeof_g_packet);
3615 memset (regs, 0, rsa->sizeof_g_packet);
b323314b 3616 for (i = 0; i < NUM_REGS + NUM_PSEUDO_REGS; i++)
193cb69f 3617 {
ea9c271d 3618 struct packet_reg *r = &rsa->regs[i];
b323314b 3619 if (r->in_g_packet)
822c9732 3620 regcache_raw_collect (current_regcache, r->regnum, regs + r->offset);
193cb69f
AC
3621 }
3622 }
c906108c
SS
3623
3624 /* Command describes registers byte by byte,
3625 each byte encoded as two hex characters. */
6d820c5c 3626 p = rs->buf;
193cb69f 3627 *p++ = 'G';
c906108c 3628 /* remote_prepare_to_store insures that register_bytes_found gets set. */
30559e10 3629 bin2hex (regs, p, register_bytes_found);
6d820c5c 3630 remote_send (&rs->buf, &rs->buf_size);
c906108c 3631}
c906108c
SS
3632\f
3633
3634/* Return the number of hex digits in num. */
3635
3636static int
fba45db2 3637hexnumlen (ULONGEST num)
c906108c
SS
3638{
3639 int i;
3640
3641 for (i = 0; num != 0; i++)
3642 num >>= 4;
3643
3644 return max (i, 1);
3645}
3646
2df3850c 3647/* Set BUF to the minimum number of hex digits representing NUM. */
c906108c
SS
3648
3649static int
fba45db2 3650hexnumstr (char *buf, ULONGEST num)
c906108c 3651{
c906108c 3652 int len = hexnumlen (num);
2df3850c
JM
3653 return hexnumnstr (buf, num, len);
3654}
3655
c906108c 3656
2df3850c 3657/* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
c906108c 3658
2df3850c 3659static int
fba45db2 3660hexnumnstr (char *buf, ULONGEST num, int width)
2df3850c
JM
3661{
3662 int i;
3663
3664 buf[width] = '\0';
3665
3666 for (i = width - 1; i >= 0; i--)
c906108c 3667 {
c5aa993b 3668 buf[i] = "0123456789abcdef"[(num & 0xf)];
c906108c
SS
3669 num >>= 4;
3670 }
3671
2df3850c 3672 return width;
c906108c
SS
3673}
3674
23860348 3675/* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
c906108c
SS
3676
3677static CORE_ADDR
fba45db2 3678remote_address_masked (CORE_ADDR addr)
c906108c
SS
3679{
3680 if (remote_address_size > 0
3681 && remote_address_size < (sizeof (ULONGEST) * 8))
3682 {
3683 /* Only create a mask when that mask can safely be constructed
23860348 3684 in a ULONGEST variable. */
c906108c
SS
3685 ULONGEST mask = 1;
3686 mask = (mask << remote_address_size) - 1;
3687 addr &= mask;
3688 }
3689 return addr;
3690}
3691
a31ea83d
DJ
3692/* Convert BUFFER, binary data at least LEN bytes long, into escaped
3693 binary data in OUT_BUF. Set *OUT_LEN to the length of the data
3694 encoded in OUT_BUF, and return the number of bytes in OUT_BUF
3695 (which may be more than *OUT_LEN due to escape characters). The
3696 total number of bytes in the output buffer will be at most
3697 OUT_MAXLEN. */
3698
3699static int
3700remote_escape_output (const gdb_byte *buffer, int len,
3701 gdb_byte *out_buf, int *out_len,
3702 int out_maxlen)
3703{
3704 int input_index, output_index;
3705
3706 output_index = 0;
3707 for (input_index = 0; input_index < len; input_index++)
3708 {
3709 gdb_byte b = buffer[input_index];
3710
3711 if (b == '$' || b == '#' || b == '}')
3712 {
3713 /* These must be escaped. */
3714 if (output_index + 2 > out_maxlen)
3715 break;
3716 out_buf[output_index++] = '}';
3717 out_buf[output_index++] = b ^ 0x20;
3718 }
3719 else
3720 {
3721 if (output_index + 1 > out_maxlen)
3722 break;
3723 out_buf[output_index++] = b;
3724 }
3725 }
3726
3727 *out_len = input_index;
3728 return output_index;
3729}
3730
0876f84a
DJ
3731/* Convert BUFFER, escaped data LEN bytes long, into binary data
3732 in OUT_BUF. Return the number of bytes written to OUT_BUF.
3733 Raise an error if the total number of bytes exceeds OUT_MAXLEN.
3734
3735 This function reverses remote_escape_output. It allows more
3736 escaped characters than that function does, in particular because
3737 '*' must be escaped to avoid the run-length encoding processing
3738 in reading packets. */
3739
3740static int
3741remote_unescape_input (const gdb_byte *buffer, int len,
3742 gdb_byte *out_buf, int out_maxlen)
3743{
3744 int input_index, output_index;
3745 int escaped;
3746
3747 output_index = 0;
3748 escaped = 0;
3749 for (input_index = 0; input_index < len; input_index++)
3750 {
3751 gdb_byte b = buffer[input_index];
3752
3753 if (output_index + 1 > out_maxlen)
3754 {
3755 warning (_("Received too much data from remote target;"
3756 " ignoring overflow."));
3757 return output_index;
3758 }
3759
3760 if (escaped)
3761 {
3762 out_buf[output_index++] = b ^ 0x20;
3763 escaped = 0;
3764 }
3765 else if (b == '}')
3766 escaped = 1;
3767 else
3768 out_buf[output_index++] = b;
3769 }
3770
3771 if (escaped)
3772 error (_("Unmatched escape character in target response."));
3773
3774 return output_index;
3775}
3776
c906108c
SS
3777/* Determine whether the remote target supports binary downloading.
3778 This is accomplished by sending a no-op memory write of zero length
3779 to the target at the specified address. It does not suffice to send
23860348
MS
3780 the whole packet, since many stubs strip the eighth bit and
3781 subsequently compute a wrong checksum, which causes real havoc with
3782 remote_write_bytes.
7a292a7a 3783
96baa820
JM
3784 NOTE: This can still lose if the serial line is not eight-bit
3785 clean. In cases like this, the user should clear "remote
23860348 3786 X-packet". */
96baa820 3787
c906108c 3788static void
fba45db2 3789check_binary_download (CORE_ADDR addr)
c906108c 3790{
d01949b6 3791 struct remote_state *rs = get_remote_state ();
24b06219 3792
444abaca 3793 switch (remote_protocol_packets[PACKET_X].support)
c906108c 3794 {
96baa820
JM
3795 case PACKET_DISABLE:
3796 break;
3797 case PACKET_ENABLE:
3798 break;
3799 case PACKET_SUPPORT_UNKNOWN:
3800 {
6d820c5c 3801 char *buf = rs->buf;
96baa820 3802 char *p;
802188a7 3803
96baa820
JM
3804 p = buf;
3805 *p++ = 'X';
3806 p += hexnumstr (p, (ULONGEST) addr);
3807 *p++ = ',';
3808 p += hexnumstr (p, (ULONGEST) 0);
3809 *p++ = ':';
3810 *p = '\0';
802188a7 3811
96baa820 3812 putpkt_binary (buf, (int) (p - buf));
6d820c5c 3813 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 3814
96baa820
JM
3815 if (buf[0] == '\0')
3816 {
3817 if (remote_debug)
3818 fprintf_unfiltered (gdb_stdlog,
3819 "binary downloading NOT suppported by target\n");
444abaca 3820 remote_protocol_packets[PACKET_X].support = PACKET_DISABLE;
96baa820
JM
3821 }
3822 else
3823 {
3824 if (remote_debug)
3825 fprintf_unfiltered (gdb_stdlog,
3826 "binary downloading suppported by target\n");
444abaca 3827 remote_protocol_packets[PACKET_X].support = PACKET_ENABLE;
96baa820
JM
3828 }
3829 break;
3830 }
c906108c
SS
3831 }
3832}
3833
3834/* Write memory data directly to the remote machine.
3835 This does not inform the data cache; the data cache uses this.
3836 MEMADDR is the address in the remote memory space.
3837 MYADDR is the address of the buffer in our space.
3838 LEN is the number of bytes.
3839
917317f4 3840 Returns number of bytes transferred, or 0 (setting errno) for
23860348 3841 error. Only transfer a single packet. */
c906108c 3842
449092f6 3843int
cfd77fa1 3844remote_write_bytes (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
c906108c 3845{
6d820c5c 3846 struct remote_state *rs = get_remote_state ();
cfd77fa1
DJ
3847 char *buf;
3848 char *p;
3849 char *plen;
917317f4
JM
3850 int plenlen;
3851 int todo;
3852 int nr_bytes;
a257b5bb 3853 int payload_size;
cfd77fa1 3854 char *payload_start;
c906108c 3855
a257b5bb 3856 /* Verify that the target can support a binary download. */
c906108c
SS
3857 check_binary_download (memaddr);
3858
3de11b2e
NS
3859 payload_size = get_memory_write_packet_size ();
3860
6d820c5c
DJ
3861 /* The packet buffer will be large enough for the payload;
3862 get_memory_packet_size ensures this. */
3863 buf = rs->buf;
c906108c 3864
a257b5bb 3865 /* Compute the size of the actual payload by subtracting out the
3de11b2e
NS
3866 packet header and footer overhead: "$M<memaddr>,<len>:...#nn".
3867 */
3868 payload_size -= strlen ("$M,:#NN");
3869 payload_size -= hexnumlen (memaddr);
c906108c 3870
a257b5bb 3871 /* Construct the packet header: "[MX]<memaddr>,<len>:". */
917317f4 3872
a257b5bb 3873 /* Append "[XM]". Compute a best guess of the number of bytes
23860348 3874 actually transfered. */
a257b5bb 3875 p = buf;
444abaca 3876 switch (remote_protocol_packets[PACKET_X].support)
c906108c 3877 {
917317f4
JM
3878 case PACKET_ENABLE:
3879 *p++ = 'X';
23860348 3880 /* Best guess at number of bytes that will fit. */
a257b5bb 3881 todo = min (len, payload_size);
3de11b2e
NS
3882 payload_size -= hexnumlen (todo);
3883 todo = min (todo, payload_size);
917317f4
JM
3884 break;
3885 case PACKET_DISABLE:
3886 *p++ = 'M';
23860348 3887 /* Num bytes that will fit. */
a257b5bb 3888 todo = min (len, payload_size / 2);
3de11b2e
NS
3889 payload_size -= hexnumlen (todo);
3890 todo = min (todo, payload_size / 2);
917317f4
JM
3891 break;
3892 case PACKET_SUPPORT_UNKNOWN:
8e65ff28 3893 internal_error (__FILE__, __LINE__,
e2e0b3e5 3894 _("remote_write_bytes: bad internal state"));
7f7e9482 3895 default:
e2e0b3e5 3896 internal_error (__FILE__, __LINE__, _("bad switch"));
917317f4 3897 }
3de11b2e
NS
3898 if (todo <= 0)
3899 internal_error (__FILE__, __LINE__,
3900 _("minumum packet size too small to write data"));
802188a7 3901
a257b5bb 3902 /* Append "<memaddr>". */
917317f4
JM
3903 memaddr = remote_address_masked (memaddr);
3904 p += hexnumstr (p, (ULONGEST) memaddr);
a257b5bb
AC
3905
3906 /* Append ",". */
917317f4 3907 *p++ = ',';
802188a7 3908
a257b5bb
AC
3909 /* Append <len>. Retain the location/size of <len>. It may need to
3910 be adjusted once the packet body has been created. */
917317f4
JM
3911 plen = p;
3912 plenlen = hexnumstr (p, (ULONGEST) todo);
3913 p += plenlen;
a257b5bb
AC
3914
3915 /* Append ":". */
917317f4
JM
3916 *p++ = ':';
3917 *p = '\0';
802188a7 3918
a257b5bb
AC
3919 /* Append the packet body. */
3920 payload_start = p;
444abaca 3921 switch (remote_protocol_packets[PACKET_X].support)
917317f4
JM
3922 {
3923 case PACKET_ENABLE:
3924 /* Binary mode. Send target system values byte by byte, in
3925 increasing byte addresses. Only escape certain critical
3926 characters. */
a31ea83d 3927 p += remote_escape_output (myaddr, todo, p, &nr_bytes, payload_size);
917317f4 3928 if (nr_bytes < todo)
c906108c 3929 {
802188a7 3930 /* Escape chars have filled up the buffer prematurely,
917317f4
JM
3931 and we have actually sent fewer bytes than planned.
3932 Fix-up the length field of the packet. Use the same
3933 number of characters as before. */
917317f4
JM
3934 plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen);
3935 *plen = ':'; /* overwrite \0 from hexnumnstr() */
c906108c 3936 }
917317f4
JM
3937 break;
3938 case PACKET_DISABLE:
3939 /* Normal mode: Send target system values byte by byte, in
3940 increasing byte addresses. Each byte is encoded as a two hex
3941 value. */
2644f393 3942 nr_bytes = bin2hex (myaddr, p, todo);
aa6c0017 3943 p += 2 * nr_bytes;
917317f4
JM
3944 break;
3945 case PACKET_SUPPORT_UNKNOWN:
8e65ff28 3946 internal_error (__FILE__, __LINE__,
e2e0b3e5 3947 _("remote_write_bytes: bad internal state"));
7f7e9482 3948 default:
e2e0b3e5 3949 internal_error (__FILE__, __LINE__, _("bad switch"));
c906108c 3950 }
802188a7 3951
917317f4 3952 putpkt_binary (buf, (int) (p - buf));
6d820c5c 3953 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 3954
917317f4
JM
3955 if (buf[0] == 'E')
3956 {
3957 /* There is no correspondance between what the remote protocol
3958 uses for errors and errno codes. We would like a cleaner way
3959 of representing errors (big enough to include errno codes,
3960 bfd_error codes, and others). But for now just return EIO. */
3961 errno = EIO;
3962 return 0;
3963 }
802188a7 3964
23860348
MS
3965 /* Return NR_BYTES, not TODO, in case escape chars caused us to send
3966 fewer bytes than we'd planned. */
917317f4 3967 return nr_bytes;
c906108c
SS
3968}
3969
3970/* Read memory data directly from the remote machine.
3971 This does not use the data cache; the data cache uses this.
3972 MEMADDR is the address in the remote memory space.
3973 MYADDR is the address of the buffer in our space.
3974 LEN is the number of bytes.
3975
3976 Returns number of bytes transferred, or 0 for error. */
3977
917317f4
JM
3978/* NOTE: cagney/1999-10-18: This function (and its siblings in other
3979 remote targets) shouldn't attempt to read the entire buffer.
3980 Instead it should read a single packet worth of data and then
3981 return the byte size of that packet to the caller. The caller (its
3982 caller and its callers caller ;-) already contains code for
23860348 3983 handling partial reads. */
917317f4 3984
449092f6 3985int
cfd77fa1 3986remote_read_bytes (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
c906108c 3987{
6d820c5c 3988 struct remote_state *rs = get_remote_state ();
11cf8741 3989 char *buf;
23860348 3990 int max_buf_size; /* Max size of packet output buffer. */
c906108c
SS
3991 int origlen;
3992
11cf8741 3993 max_buf_size = get_memory_read_packet_size ();
6d820c5c
DJ
3994 /* The packet buffer will be large enough for the payload;
3995 get_memory_packet_size ensures this. */
3996 buf = rs->buf;
c906108c
SS
3997
3998 origlen = len;
3999 while (len > 0)
4000 {
c906108c
SS
4001 char *p;
4002 int todo;
4003 int i;
4004
c5aa993b 4005 todo = min (len, max_buf_size / 2); /* num bytes that will fit */
c906108c
SS
4006
4007 /* construct "m"<memaddr>","<len>" */
4008 /* sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo); */
4009 memaddr = remote_address_masked (memaddr);
4010 p = buf;
4011 *p++ = 'm';
4012 p += hexnumstr (p, (ULONGEST) memaddr);
4013 *p++ = ',';
4014 p += hexnumstr (p, (ULONGEST) todo);
4015 *p = '\0';
4016
4017 putpkt (buf);
6d820c5c 4018 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 4019
66504d44
MS
4020 if (buf[0] == 'E'
4021 && isxdigit (buf[1]) && isxdigit (buf[2])
4022 && buf[3] == '\0')
c906108c 4023 {
23860348
MS
4024 /* There is no correspondance between what the remote
4025 protocol uses for errors and errno codes. We would like
4026 a cleaner way of representing errors (big enough to
4027 include errno codes, bfd_error codes, and others). But
4028 for now just return EIO. */
c906108c
SS
4029 errno = EIO;
4030 return 0;
4031 }
4032
c5aa993b
JM
4033 /* Reply describes memory byte by byte,
4034 each byte encoded as two hex characters. */
c906108c
SS
4035
4036 p = buf;
30559e10 4037 if ((i = hex2bin (p, myaddr, todo)) < todo)
c906108c 4038 {
30559e10 4039 /* Reply is short. This means that we were able to read
23860348 4040 only part of what we wanted to. */
30559e10 4041 return i + (origlen - len);
c906108c
SS
4042 }
4043 myaddr += todo;
4044 memaddr += todo;
4045 len -= todo;
4046 }
4047 return origlen;
4048}
4049\f
4050/* Read or write LEN bytes from inferior memory at MEMADDR,
23860348
MS
4051 transferring to or from debugger address BUFFER. Write to inferior
4052 if SHOULD_WRITE is nonzero. Returns length of data written or
4053 read; 0 for error. TARGET is unused. */
392a587b 4054
c906108c 4055static int
961cb7b5 4056remote_xfer_memory (CORE_ADDR mem_addr, gdb_byte *buffer, int mem_len,
0a65a603 4057 int should_write, struct mem_attrib *attrib,
29e57380 4058 struct target_ops *target)
c906108c 4059{
392a587b
JM
4060 CORE_ADDR targ_addr;
4061 int targ_len;
4930751a
C
4062 int res;
4063
f6684c31 4064 /* Should this be the selected frame? */
23860348
MS
4065 gdbarch_remote_translate_xfer_address (current_gdbarch,
4066 current_regcache,
f6684c31
AC
4067 mem_addr, mem_len,
4068 &targ_addr, &targ_len);
392a587b 4069 if (targ_len <= 0)
c906108c 4070 return 0;
c906108c 4071
4930751a
C
4072 if (should_write)
4073 res = remote_write_bytes (targ_addr, buffer, targ_len);
4074 else
4075 res = remote_read_bytes (targ_addr, buffer, targ_len);
4076
4077 return res;
c906108c
SS
4078}
4079
c906108c 4080static void
fba45db2 4081remote_files_info (struct target_ops *ignore)
c906108c
SS
4082{
4083 puts_filtered ("Debugging a target over a serial line.\n");
4084}
4085\f
4086/* Stuff for dealing with the packets which are part of this protocol.
4087 See comment at top of file for details. */
4088
0876f84a 4089/* Read a single character from the remote end. */
c906108c
SS
4090
4091static int
fba45db2 4092readchar (int timeout)
c906108c
SS
4093{
4094 int ch;
4095
2cd58942 4096 ch = serial_readchar (remote_desc, timeout);
c906108c 4097
2acceee2 4098 if (ch >= 0)
0876f84a 4099 return ch;
2acceee2
JM
4100
4101 switch ((enum serial_rc) ch)
c906108c
SS
4102 {
4103 case SERIAL_EOF:
2acceee2 4104 target_mourn_inferior ();
8a3fe4f8 4105 error (_("Remote connection closed"));
2acceee2 4106 /* no return */
c906108c 4107 case SERIAL_ERROR:
e2e0b3e5 4108 perror_with_name (_("Remote communication error"));
2acceee2 4109 /* no return */
c906108c 4110 case SERIAL_TIMEOUT:
2acceee2 4111 break;
c906108c 4112 }
2acceee2 4113 return ch;
c906108c
SS
4114}
4115
6d820c5c
DJ
4116/* Send the command in *BUF to the remote machine, and read the reply
4117 into *BUF. Report an error if we get an error reply. Resize
4118 *BUF using xrealloc if necessary to hold the result, and update
4119 *SIZEOF_BUF. */
c906108c
SS
4120
4121static void
6d820c5c
DJ
4122remote_send (char **buf,
4123 long *sizeof_buf)
c906108c 4124{
6d820c5c 4125 putpkt (*buf);
c2d11a7d 4126 getpkt (buf, sizeof_buf, 0);
c906108c 4127
6d820c5c
DJ
4128 if ((*buf)[0] == 'E')
4129 error (_("Remote failure reply: %s"), *buf);
c906108c
SS
4130}
4131
4132/* Display a null-terminated packet on stdout, for debugging, using C
4133 string notation. */
4134
4135static void
fba45db2 4136print_packet (char *buf)
c906108c
SS
4137{
4138 puts_filtered ("\"");
43e526b9 4139 fputstr_filtered (buf, '"', gdb_stdout);
c906108c
SS
4140 puts_filtered ("\"");
4141}
4142
4143int
fba45db2 4144putpkt (char *buf)
c906108c
SS
4145{
4146 return putpkt_binary (buf, strlen (buf));
4147}
4148
4149/* Send a packet to the remote machine, with error checking. The data
23860348 4150 of the packet is in BUF. The string in BUF can be at most
ea9c271d 4151 get_remote_packet_size () - 5 to account for the $, # and checksum,
23860348
MS
4152 and for a possible /0 if we are debugging (remote_debug) and want
4153 to print the sent packet as a string. */
c906108c
SS
4154
4155static int
fba45db2 4156putpkt_binary (char *buf, int cnt)
c906108c
SS
4157{
4158 int i;
4159 unsigned char csum = 0;
11cf8741 4160 char *buf2 = alloca (cnt + 6);
085dd6e6 4161
c906108c
SS
4162 int ch;
4163 int tcount = 0;
4164 char *p;
4165
4166 /* Copy the packet into buffer BUF2, encapsulating it
4167 and giving it a checksum. */
4168
c906108c
SS
4169 p = buf2;
4170 *p++ = '$';
4171
4172 for (i = 0; i < cnt; i++)
4173 {
4174 csum += buf[i];
4175 *p++ = buf[i];
4176 }
4177 *p++ = '#';
4178 *p++ = tohex ((csum >> 4) & 0xf);
4179 *p++ = tohex (csum & 0xf);
4180
4181 /* Send it over and over until we get a positive ack. */
4182
4183 while (1)
4184 {
4185 int started_error_output = 0;
4186
4187 if (remote_debug)
4188 {
4189 *p = '\0';
43e526b9
JM
4190 fprintf_unfiltered (gdb_stdlog, "Sending packet: ");
4191 fputstrn_unfiltered (buf2, p - buf2, 0, gdb_stdlog);
d4f3574e 4192 fprintf_unfiltered (gdb_stdlog, "...");
0f71a2f6 4193 gdb_flush (gdb_stdlog);
c906108c 4194 }
2cd58942 4195 if (serial_write (remote_desc, buf2, p - buf2))
e2e0b3e5 4196 perror_with_name (_("putpkt: write failed"));
c906108c 4197
23860348 4198 /* Read until either a timeout occurs (-2) or '+' is read. */
c906108c
SS
4199 while (1)
4200 {
4201 ch = readchar (remote_timeout);
4202
c5aa993b 4203 if (remote_debug)
c906108c
SS
4204 {
4205 switch (ch)
4206 {
4207 case '+':
1216fa2c 4208 case '-':
c906108c
SS
4209 case SERIAL_TIMEOUT:
4210 case '$':
4211 if (started_error_output)
4212 {
4213 putchar_unfiltered ('\n');
4214 started_error_output = 0;
4215 }
4216 }
4217 }
4218
4219 switch (ch)
4220 {
4221 case '+':
4222 if (remote_debug)
0f71a2f6 4223 fprintf_unfiltered (gdb_stdlog, "Ack\n");
c906108c 4224 return 1;
1216fa2c
AC
4225 case '-':
4226 if (remote_debug)
4227 fprintf_unfiltered (gdb_stdlog, "Nak\n");
c906108c 4228 case SERIAL_TIMEOUT:
c5aa993b 4229 tcount++;
c906108c
SS
4230 if (tcount > 3)
4231 return 0;
23860348 4232 break; /* Retransmit buffer. */
c906108c
SS
4233 case '$':
4234 {
40e3f985 4235 if (remote_debug)
23860348
MS
4236 fprintf_unfiltered (gdb_stdlog,
4237 "Packet instead of Ack, ignoring it\n");
d6f7abdf
AC
4238 /* It's probably an old response sent because an ACK
4239 was lost. Gobble up the packet and ack it so it
4240 doesn't get retransmitted when we resend this
4241 packet. */
6d820c5c 4242 skip_frame ();
d6f7abdf 4243 serial_write (remote_desc, "+", 1);
23860348 4244 continue; /* Now, go look for +. */
c906108c
SS
4245 }
4246 default:
4247 if (remote_debug)
4248 {
4249 if (!started_error_output)
4250 {
4251 started_error_output = 1;
0f71a2f6 4252 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
c906108c 4253 }
0f71a2f6 4254 fputc_unfiltered (ch & 0177, gdb_stdlog);
c906108c
SS
4255 }
4256 continue;
4257 }
23860348 4258 break; /* Here to retransmit. */
c906108c
SS
4259 }
4260
4261#if 0
4262 /* This is wrong. If doing a long backtrace, the user should be
c5aa993b
JM
4263 able to get out next time we call QUIT, without anything as
4264 violent as interrupt_query. If we want to provide a way out of
4265 here without getting to the next QUIT, it should be based on
4266 hitting ^C twice as in remote_wait. */
c906108c
SS
4267 if (quit_flag)
4268 {
4269 quit_flag = 0;
4270 interrupt_query ();
4271 }
4272#endif
4273 }
4274}
4275
6d820c5c
DJ
4276/* Come here after finding the start of a frame when we expected an
4277 ack. Do our best to discard the rest of this packet. */
4278
4279static void
4280skip_frame (void)
4281{
4282 int c;
4283
4284 while (1)
4285 {
4286 c = readchar (remote_timeout);
4287 switch (c)
4288 {
4289 case SERIAL_TIMEOUT:
4290 /* Nothing we can do. */
4291 return;
4292 case '#':
4293 /* Discard the two bytes of checksum and stop. */
4294 c = readchar (remote_timeout);
4295 if (c >= 0)
4296 c = readchar (remote_timeout);
4297
4298 return;
4299 case '*': /* Run length encoding. */
4300 /* Discard the repeat count. */
4301 c = readchar (remote_timeout);
4302 if (c < 0)
4303 return;
4304 break;
4305 default:
4306 /* A regular character. */
4307 break;
4308 }
4309 }
4310}
4311
c906108c 4312/* Come here after finding the start of the frame. Collect the rest
6d820c5c
DJ
4313 into *BUF, verifying the checksum, length, and handling run-length
4314 compression. NUL terminate the buffer. If there is not enough room,
4315 expand *BUF using xrealloc.
c906108c 4316
c2d11a7d
JM
4317 Returns -1 on error, number of characters in buffer (ignoring the
4318 trailing NULL) on success. (could be extended to return one of the
23860348 4319 SERIAL status indications). */
c2d11a7d
JM
4320
4321static long
6d820c5c
DJ
4322read_frame (char **buf_p,
4323 long *sizeof_buf)
c906108c
SS
4324{
4325 unsigned char csum;
c2d11a7d 4326 long bc;
c906108c 4327 int c;
6d820c5c 4328 char *buf = *buf_p;
c906108c
SS
4329
4330 csum = 0;
c2d11a7d 4331 bc = 0;
c906108c
SS
4332
4333 while (1)
4334 {
4335 c = readchar (remote_timeout);
c906108c
SS
4336 switch (c)
4337 {
4338 case SERIAL_TIMEOUT:
4339 if (remote_debug)
0f71a2f6 4340 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
c2d11a7d 4341 return -1;
c906108c
SS
4342 case '$':
4343 if (remote_debug)
0f71a2f6
JM
4344 fputs_filtered ("Saw new packet start in middle of old one\n",
4345 gdb_stdlog);
23860348 4346 return -1; /* Start a new packet, count retries. */
c906108c
SS
4347 case '#':
4348 {
4349 unsigned char pktcsum;
e1b09194
AC
4350 int check_0 = 0;
4351 int check_1 = 0;
c906108c 4352
c2d11a7d 4353 buf[bc] = '\0';
c906108c 4354
e1b09194
AC
4355 check_0 = readchar (remote_timeout);
4356 if (check_0 >= 0)
4357 check_1 = readchar (remote_timeout);
802188a7 4358
e1b09194
AC
4359 if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
4360 {
4361 if (remote_debug)
23860348
MS
4362 fputs_filtered ("Timeout in checksum, retrying\n",
4363 gdb_stdlog);
e1b09194
AC
4364 return -1;
4365 }
4366 else if (check_0 < 0 || check_1 < 0)
40e3f985
FN
4367 {
4368 if (remote_debug)
23860348
MS
4369 fputs_filtered ("Communication error in checksum\n",
4370 gdb_stdlog);
40e3f985
FN
4371 return -1;
4372 }
c906108c 4373
e1b09194 4374 pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
c906108c 4375 if (csum == pktcsum)
c2d11a7d 4376 return bc;
c906108c 4377
c5aa993b 4378 if (remote_debug)
c906108c 4379 {
0f71a2f6 4380 fprintf_filtered (gdb_stdlog,
c5aa993b 4381 "Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
0f71a2f6 4382 pktcsum, csum);
0876f84a 4383 fputstrn_filtered (buf, bc, 0, gdb_stdlog);
0f71a2f6 4384 fputs_filtered ("\n", gdb_stdlog);
c906108c 4385 }
c2d11a7d 4386 /* Number of characters in buffer ignoring trailing
23860348 4387 NULL. */
c2d11a7d 4388 return -1;
c906108c 4389 }
23860348 4390 case '*': /* Run length encoding. */
c2c6d25f
JM
4391 {
4392 int repeat;
4393 csum += c;
c906108c 4394
b4501125
AC
4395 c = readchar (remote_timeout);
4396 csum += c;
23860348 4397 repeat = c - ' ' + 3; /* Compute repeat count. */
c906108c 4398
23860348 4399 /* The character before ``*'' is repeated. */
c2d11a7d 4400
6d820c5c 4401 if (repeat > 0 && repeat <= 255 && bc > 0)
c2c6d25f 4402 {
6d820c5c
DJ
4403 if (bc + repeat - 1 >= *sizeof_buf - 1)
4404 {
4405 /* Make some more room in the buffer. */
4406 *sizeof_buf += repeat;
4407 *buf_p = xrealloc (*buf_p, *sizeof_buf);
4408 buf = *buf_p;
4409 }
4410
c2d11a7d
JM
4411 memset (&buf[bc], buf[bc - 1], repeat);
4412 bc += repeat;
c2c6d25f
JM
4413 continue;
4414 }
4415
c2d11a7d 4416 buf[bc] = '\0';
6d820c5c 4417 printf_filtered (_("Invalid run length encoding: %s\n"), buf);
c2d11a7d 4418 return -1;
c2c6d25f 4419 }
c906108c 4420 default:
6d820c5c 4421 if (bc >= *sizeof_buf - 1)
c906108c 4422 {
6d820c5c
DJ
4423 /* Make some more room in the buffer. */
4424 *sizeof_buf *= 2;
4425 *buf_p = xrealloc (*buf_p, *sizeof_buf);
4426 buf = *buf_p;
c906108c
SS
4427 }
4428
6d820c5c
DJ
4429 buf[bc++] = c;
4430 csum += c;
4431 continue;
c906108c
SS
4432 }
4433 }
4434}
4435
4436/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
4437 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
4438 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
4439 rather than timing out; this is used (in synchronous mode) to wait
4440 for a target that is is executing user code to stop. */
d9fcf2fb
JM
4441/* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
4442 don't have to change all the calls to getpkt to deal with the
4443 return value, because at the moment I don't know what the right
23860348 4444 thing to do it for those. */
c906108c 4445void
6d820c5c
DJ
4446getpkt (char **buf,
4447 long *sizeof_buf,
c2d11a7d 4448 int forever)
d9fcf2fb
JM
4449{
4450 int timed_out;
4451
4452 timed_out = getpkt_sane (buf, sizeof_buf, forever);
4453}
4454
4455
4456/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
4457 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
4458 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
4459 rather than timing out; this is used (in synchronous mode) to wait
4460 for a target that is is executing user code to stop. If FOREVER ==
4461 0, this function is allowed to time out gracefully and return an
0876f84a
DJ
4462 indication of this to the caller. Otherwise return the number
4463 of bytes read. */
3172dc30 4464static int
6d820c5c 4465getpkt_sane (char **buf, long *sizeof_buf, int forever)
c906108c
SS
4466{
4467 int c;
4468 int tries;
4469 int timeout;
4470 int val;
4471
6d820c5c 4472 strcpy (*buf, "timeout");
c906108c
SS
4473
4474 if (forever)
4475 {
c906108c 4476 timeout = watchdog > 0 ? watchdog : -1;
c906108c
SS
4477 }
4478
4479 else
4480 timeout = remote_timeout;
4481
4482#define MAX_TRIES 3
4483
4484 for (tries = 1; tries <= MAX_TRIES; tries++)
4485 {
4486 /* This can loop forever if the remote side sends us characters
23860348
MS
4487 continuously, but if it pauses, we'll get a zero from
4488 readchar because of timeout. Then we'll count that as a
4489 retry. */
c906108c 4490
23860348
MS
4491 /* Note that we will only wait forever prior to the start of a
4492 packet. After that, we expect characters to arrive at a
4493 brisk pace. They should show up within remote_timeout
4494 intervals. */
c906108c
SS
4495
4496 do
4497 {
4498 c = readchar (timeout);
4499
4500 if (c == SERIAL_TIMEOUT)
4501 {
23860348 4502 if (forever) /* Watchdog went off? Kill the target. */
c906108c 4503 {
2acceee2 4504 QUIT;
c906108c 4505 target_mourn_inferior ();
8a3fe4f8 4506 error (_("Watchdog has expired. Target detached."));
c906108c 4507 }
c906108c 4508 if (remote_debug)
0f71a2f6 4509 fputs_filtered ("Timed out.\n", gdb_stdlog);
c906108c
SS
4510 goto retry;
4511 }
4512 }
4513 while (c != '$');
4514
4515 /* We've found the start of a packet, now collect the data. */
4516
c2d11a7d 4517 val = read_frame (buf, sizeof_buf);
c906108c 4518
c2d11a7d 4519 if (val >= 0)
c906108c
SS
4520 {
4521 if (remote_debug)
43e526b9
JM
4522 {
4523 fprintf_unfiltered (gdb_stdlog, "Packet received: ");
0876f84a 4524 fputstrn_unfiltered (*buf, val, 0, gdb_stdlog);
43e526b9
JM
4525 fprintf_unfiltered (gdb_stdlog, "\n");
4526 }
2cd58942 4527 serial_write (remote_desc, "+", 1);
0876f84a 4528 return val;
c906108c
SS
4529 }
4530
4531 /* Try the whole thing again. */
4532 retry:
2cd58942 4533 serial_write (remote_desc, "-", 1);
c906108c
SS
4534 }
4535
23860348
MS
4536 /* We have tried hard enough, and just can't receive the packet.
4537 Give up. */
c906108c 4538
a3f17187 4539 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
2cd58942 4540 serial_write (remote_desc, "+", 1);
0876f84a 4541 return -1;
c906108c
SS
4542}
4543\f
4544static void
fba45db2 4545remote_kill (void)
c906108c
SS
4546{
4547 /* For some mysterious reason, wait_for_inferior calls kill instead of
4548 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4549 if (kill_kludge)
4550 {
4551 kill_kludge = 0;
4552 target_mourn_inferior ();
4553 return;
4554 }
4555
4556 /* Use catch_errors so the user can quit from gdb even when we aren't on
4557 speaking terms with the remote system. */
c5aa993b 4558 catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR);
c906108c
SS
4559
4560 /* Don't wait for it to die. I'm not really sure it matters whether
4561 we do or not. For the existing stubs, kill is a noop. */
4562 target_mourn_inferior ();
4563}
4564
23860348 4565/* Async version of remote_kill. */
43ff13b4 4566static void
fba45db2 4567remote_async_kill (void)
43ff13b4 4568{
23860348 4569 /* Unregister the file descriptor from the event loop. */
ed9a39eb 4570 if (target_is_async_p ())
2cd58942 4571 serial_async (remote_desc, NULL, 0);
43ff13b4
JM
4572
4573 /* For some mysterious reason, wait_for_inferior calls kill instead of
4574 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4575 if (kill_kludge)
4576 {
4577 kill_kludge = 0;
4578 target_mourn_inferior ();
4579 return;
4580 }
4581
23860348
MS
4582 /* Use catch_errors so the user can quit from gdb even when we
4583 aren't on speaking terms with the remote system. */
c5aa993b 4584 catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR);
43ff13b4
JM
4585
4586 /* Don't wait for it to die. I'm not really sure it matters whether
4587 we do or not. For the existing stubs, kill is a noop. */
4588 target_mourn_inferior ();
4589}
4590
c906108c 4591static void
fba45db2 4592remote_mourn (void)
c906108c
SS
4593{
4594 remote_mourn_1 (&remote_ops);
4595}
4596
53a5351d 4597static void
fba45db2 4598remote_async_mourn (void)
53a5351d
JM
4599{
4600 remote_mourn_1 (&remote_async_ops);
4601}
4602
c906108c 4603static void
fba45db2 4604extended_remote_mourn (void)
c906108c
SS
4605{
4606 /* We do _not_ want to mourn the target like this; this will
4607 remove the extended remote target from the target stack,
802188a7 4608 and the next time the user says "run" it'll fail.
c906108c
SS
4609
4610 FIXME: What is the right thing to do here? */
4611#if 0
4612 remote_mourn_1 (&extended_remote_ops);
4613#endif
4614}
4615
4616/* Worker function for remote_mourn. */
4617static void
fba45db2 4618remote_mourn_1 (struct target_ops *target)
c906108c
SS
4619{
4620 unpush_target (target);
4621 generic_mourn_inferior ();
4622}
4623
4624/* In the extended protocol we want to be able to do things like
4625 "run" and have them basically work as expected. So we need
802188a7 4626 a special create_inferior function.
c906108c
SS
4627
4628 FIXME: One day add support for changing the exec file
4629 we're debugging, arguments and an environment. */
4630
4631static void
23860348
MS
4632extended_remote_create_inferior (char *exec_file, char *args,
4633 char **env, int from_tty)
c906108c
SS
4634{
4635 /* Rip out the breakpoints; we'll reinsert them after restarting
4636 the remote server. */
4637 remove_breakpoints ();
4638
4639 /* Now restart the remote server. */
4640 extended_remote_restart ();
4641
4642 /* Now put the breakpoints back in. This way we're safe if the
4643 restart function works via a unix fork on the remote side. */
4644 insert_breakpoints ();
4645
4646 /* Clean up from the last time we were running. */
4647 clear_proceed_status ();
c906108c
SS
4648}
4649
23860348 4650/* Async version of extended_remote_create_inferior. */
43ff13b4 4651static void
23860348
MS
4652extended_remote_async_create_inferior (char *exec_file, char *args,
4653 char **env, int from_tty)
43ff13b4
JM
4654{
4655 /* Rip out the breakpoints; we'll reinsert them after restarting
4656 the remote server. */
4657 remove_breakpoints ();
4658
4659 /* If running asynchronously, register the target file descriptor
23860348 4660 with the event loop. */
362646f5 4661 if (target_can_async_p ())
2acceee2 4662 target_async (inferior_event_handler, 0);
43ff13b4
JM
4663
4664 /* Now restart the remote server. */
4665 extended_remote_restart ();
4666
4667 /* Now put the breakpoints back in. This way we're safe if the
4668 restart function works via a unix fork on the remote side. */
4669 insert_breakpoints ();
4670
4671 /* Clean up from the last time we were running. */
4672 clear_proceed_status ();
43ff13b4 4673}
c906108c 4674\f
c5aa993b 4675
aaab4dba 4676/* On some machines, e.g. 68k, we may use a different breakpoint
7fec2c59
AC
4677 instruction than other targets; in those use
4678 DEPRECATED_REMOTE_BREAKPOINT instead of just BREAKPOINT_FROM_PC.
4679 Also, bi-endian targets may define
4680 DEPRECATED_LITTLE_REMOTE_BREAKPOINT and
4681 DEPRECATED_BIG_REMOTE_BREAKPOINT. If none of these are defined, we
4682 just call the standard routines that are in mem-break.c. */
4683
4684/* NOTE: cagney/2003-06-08: This is silly. A remote and simulator
4685 target should use an identical BREAKPOINT_FROM_PC. As for native,
4686 the ARCH-OS-tdep.c code can override the default. */
4687
4688#if defined (DEPRECATED_LITTLE_REMOTE_BREAKPOINT) && defined (DEPRECATED_BIG_REMOTE_BREAKPOINT) && !defined(DEPRECATED_REMOTE_BREAKPOINT)
4689#define DEPRECATED_REMOTE_BREAKPOINT
c906108c
SS
4690#endif
4691
7fec2c59 4692#ifdef DEPRECATED_REMOTE_BREAKPOINT
c906108c
SS
4693
4694/* If the target isn't bi-endian, just pretend it is. */
7fec2c59
AC
4695#if !defined (DEPRECATED_LITTLE_REMOTE_BREAKPOINT) && !defined (DEPRECATED_BIG_REMOTE_BREAKPOINT)
4696#define DEPRECATED_LITTLE_REMOTE_BREAKPOINT DEPRECATED_REMOTE_BREAKPOINT
4697#define DEPRECATED_BIG_REMOTE_BREAKPOINT DEPRECATED_REMOTE_BREAKPOINT
c906108c
SS
4698#endif
4699
7fec2c59
AC
4700static unsigned char big_break_insn[] = DEPRECATED_BIG_REMOTE_BREAKPOINT;
4701static unsigned char little_break_insn[] = DEPRECATED_LITTLE_REMOTE_BREAKPOINT;
c906108c 4702
7fec2c59 4703#endif /* DEPRECATED_REMOTE_BREAKPOINT */
c906108c 4704
8181d85f
DJ
4705/* Insert a breakpoint. On targets that have software breakpoint
4706 support, we ask the remote target to do the work; on targets
4707 which don't, we insert a traditional memory breakpoint. */
c906108c
SS
4708
4709static int
8181d85f 4710remote_insert_breakpoint (struct bp_target_info *bp_tgt)
c906108c 4711{
8181d85f 4712 CORE_ADDR addr = bp_tgt->placed_address;
d01949b6 4713 struct remote_state *rs = get_remote_state ();
7fec2c59 4714#ifdef DEPRECATED_REMOTE_BREAKPOINT
c906108c 4715 int val;
802188a7 4716#endif
96baa820 4717
d471ea57
AC
4718 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
4719 If it succeeds, then set the support to PACKET_ENABLE. If it
4720 fails, and the user has explicitly requested the Z support then
23860348 4721 report an error, otherwise, mark it disabled and go on. */
802188a7 4722
444abaca 4723 if (remote_protocol_packets[PACKET_Z0].support != PACKET_DISABLE)
96baa820 4724 {
6d820c5c 4725 char *p = rs->buf;
802188a7 4726
96baa820
JM
4727 *(p++) = 'Z';
4728 *(p++) = '0';
4729 *(p++) = ',';
8181d85f
DJ
4730 BREAKPOINT_FROM_PC (&bp_tgt->placed_address, &bp_tgt->placed_size);
4731 addr = (ULONGEST) remote_address_masked (bp_tgt->placed_address);
4732 p += hexnumstr (p, addr);
4733 sprintf (p, ",%d", bp_tgt->placed_size);
802188a7 4734
6d820c5c
DJ
4735 putpkt (rs->buf);
4736 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 4737
6d820c5c 4738 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0]))
96baa820 4739 {
d471ea57
AC
4740 case PACKET_ERROR:
4741 return -1;
4742 case PACKET_OK:
4743 return 0;
4744 case PACKET_UNKNOWN:
4745 break;
96baa820
JM
4746 }
4747 }
c906108c 4748
802188a7 4749#ifdef DEPRECATED_REMOTE_BREAKPOINT
8181d85f
DJ
4750 bp_tgt->placed_size = bp_tgt->shadow_len = sizeof big_break_insn;
4751 val = target_read_memory (addr, bp_tgt->shadow_contents, bp_tgt->shadow_len);
c906108c
SS
4752
4753 if (val == 0)
4754 {
d7449b42 4755 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
c906108c
SS
4756 val = target_write_memory (addr, (char *) big_break_insn,
4757 sizeof big_break_insn);
4758 else
4759 val = target_write_memory (addr, (char *) little_break_insn,
4760 sizeof little_break_insn);
4761 }
4762
4763 return val;
4764#else
8181d85f 4765 return memory_insert_breakpoint (bp_tgt);
7fec2c59 4766#endif /* DEPRECATED_REMOTE_BREAKPOINT */
c906108c
SS
4767}
4768
4769static int
8181d85f 4770remote_remove_breakpoint (struct bp_target_info *bp_tgt)
c906108c 4771{
8181d85f 4772 CORE_ADDR addr = bp_tgt->placed_address;
d01949b6 4773 struct remote_state *rs = get_remote_state ();
96baa820
JM
4774 int bp_size;
4775
444abaca 4776 if (remote_protocol_packets[PACKET_Z0].support != PACKET_DISABLE)
96baa820 4777 {
6d820c5c 4778 char *p = rs->buf;
802188a7 4779
96baa820
JM
4780 *(p++) = 'z';
4781 *(p++) = '0';
4782 *(p++) = ',';
4783
8181d85f
DJ
4784 addr = (ULONGEST) remote_address_masked (bp_tgt->placed_address);
4785 p += hexnumstr (p, addr);
4786 sprintf (p, ",%d", bp_tgt->placed_size);
802188a7 4787
6d820c5c
DJ
4788 putpkt (rs->buf);
4789 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 4790
6d820c5c 4791 return (rs->buf[0] == 'E');
96baa820
JM
4792 }
4793
7fec2c59 4794#ifdef DEPRECATED_REMOTE_BREAKPOINT
8181d85f
DJ
4795 return target_write_memory (bp_tgt->placed_address, bp_tgt->shadow_contents,
4796 bp_tgt->shadow_len);
c906108c 4797#else
8181d85f 4798 return memory_remove_breakpoint (bp_tgt);
7fec2c59 4799#endif /* DEPRECATED_REMOTE_BREAKPOINT */
c906108c
SS
4800}
4801
d471ea57
AC
4802static int
4803watchpoint_to_Z_packet (int type)
4804{
4805 switch (type)
4806 {
4807 case hw_write:
bb858e6a 4808 return Z_PACKET_WRITE_WP;
d471ea57
AC
4809 break;
4810 case hw_read:
bb858e6a 4811 return Z_PACKET_READ_WP;
d471ea57
AC
4812 break;
4813 case hw_access:
bb858e6a 4814 return Z_PACKET_ACCESS_WP;
d471ea57
AC
4815 break;
4816 default:
8e65ff28 4817 internal_error (__FILE__, __LINE__,
e2e0b3e5 4818 _("hw_bp_to_z: bad watchpoint type %d"), type);
d471ea57
AC
4819 }
4820}
4821
3c3bea1c 4822static int
fba45db2 4823remote_insert_watchpoint (CORE_ADDR addr, int len, int type)
96baa820 4824{
d01949b6 4825 struct remote_state *rs = get_remote_state ();
e514a9d6 4826 char *p;
d471ea57 4827 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
96baa820 4828
444abaca 4829 if (remote_protocol_packets[PACKET_Z0 + packet].support == PACKET_DISABLE)
5cffb350 4830 return -1;
802188a7 4831
6d820c5c
DJ
4832 sprintf (rs->buf, "Z%x,", packet);
4833 p = strchr (rs->buf, '\0');
96baa820
JM
4834 addr = remote_address_masked (addr);
4835 p += hexnumstr (p, (ULONGEST) addr);
d4f3574e 4836 sprintf (p, ",%x", len);
802188a7 4837
6d820c5c
DJ
4838 putpkt (rs->buf);
4839 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 4840
6d820c5c 4841 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
4842 {
4843 case PACKET_ERROR:
4844 case PACKET_UNKNOWN:
4845 return -1;
4846 case PACKET_OK:
4847 return 0;
4848 }
8e65ff28 4849 internal_error (__FILE__, __LINE__,
e2e0b3e5 4850 _("remote_insert_watchpoint: reached end of function"));
96baa820
JM
4851}
4852
d471ea57 4853
3c3bea1c 4854static int
fba45db2 4855remote_remove_watchpoint (CORE_ADDR addr, int len, int type)
96baa820 4856{
d01949b6 4857 struct remote_state *rs = get_remote_state ();
e514a9d6 4858 char *p;
d471ea57
AC
4859 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
4860
444abaca 4861 if (remote_protocol_packets[PACKET_Z0 + packet].support == PACKET_DISABLE)
5cffb350 4862 return -1;
802188a7 4863
6d820c5c
DJ
4864 sprintf (rs->buf, "z%x,", packet);
4865 p = strchr (rs->buf, '\0');
96baa820
JM
4866 addr = remote_address_masked (addr);
4867 p += hexnumstr (p, (ULONGEST) addr);
d4f3574e 4868 sprintf (p, ",%x", len);
6d820c5c
DJ
4869 putpkt (rs->buf);
4870 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 4871
6d820c5c 4872 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
4873 {
4874 case PACKET_ERROR:
4875 case PACKET_UNKNOWN:
4876 return -1;
4877 case PACKET_OK:
4878 return 0;
4879 }
8e65ff28 4880 internal_error (__FILE__, __LINE__,
e2e0b3e5 4881 _("remote_remove_watchpoint: reached end of function"));
96baa820
JM
4882}
4883
3c3bea1c 4884
501eef12
AC
4885int remote_hw_watchpoint_limit = -1;
4886int remote_hw_breakpoint_limit = -1;
d471ea57 4887
b9362cc7 4888static int
3c3bea1c 4889remote_check_watch_resources (int type, int cnt, int ot)
96baa820 4890{
3c3bea1c
GS
4891 if (type == bp_hardware_breakpoint)
4892 {
4893 if (remote_hw_breakpoint_limit == 0)
4894 return 0;
501eef12
AC
4895 else if (remote_hw_breakpoint_limit < 0)
4896 return 1;
3c3bea1c
GS
4897 else if (cnt <= remote_hw_breakpoint_limit)
4898 return 1;
4899 }
4900 else
4901 {
4902 if (remote_hw_watchpoint_limit == 0)
4903 return 0;
501eef12
AC
4904 else if (remote_hw_watchpoint_limit < 0)
4905 return 1;
3c3bea1c
GS
4906 else if (ot)
4907 return -1;
4908 else if (cnt <= remote_hw_watchpoint_limit)
4909 return 1;
4910 }
4911 return -1;
4912}
4913
b9362cc7 4914static int
3c3bea1c
GS
4915remote_stopped_by_watchpoint (void)
4916{
4917 return remote_stopped_by_watchpoint_p;
4918}
4919
7270d8f2
OF
4920extern int stepped_after_stopped_by_watchpoint;
4921
4aa7a7f5
JJ
4922static int
4923remote_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
3c3bea1c 4924{
4aa7a7f5 4925 int rc = 0;
7270d8f2
OF
4926 if (remote_stopped_by_watchpoint ()
4927 || stepped_after_stopped_by_watchpoint)
4aa7a7f5
JJ
4928 {
4929 *addr_p = remote_watch_data_address;
4930 rc = 1;
4931 }
4932
4933 return rc;
3c3bea1c
GS
4934}
4935
4936
4937static int
8181d85f 4938remote_insert_hw_breakpoint (struct bp_target_info *bp_tgt)
3c3bea1c 4939{
8181d85f 4940 CORE_ADDR addr;
d01949b6 4941 struct remote_state *rs = get_remote_state ();
6d820c5c 4942 char *p = rs->buf;
802188a7 4943
c8189ed1 4944 /* The length field should be set to the size of a breakpoint
8181d85f 4945 instruction, even though we aren't inserting one ourselves. */
c8189ed1 4946
8181d85f 4947 BREAKPOINT_FROM_PC (&bp_tgt->placed_address, &bp_tgt->placed_size);
3c3bea1c 4948
444abaca 4949 if (remote_protocol_packets[PACKET_Z1].support == PACKET_DISABLE)
5cffb350
NS
4950 return -1;
4951
96baa820
JM
4952 *(p++) = 'Z';
4953 *(p++) = '1';
4954 *(p++) = ',';
802188a7 4955
8181d85f 4956 addr = remote_address_masked (bp_tgt->placed_address);
96baa820 4957 p += hexnumstr (p, (ULONGEST) addr);
8181d85f 4958 sprintf (p, ",%x", bp_tgt->placed_size);
96baa820 4959
6d820c5c
DJ
4960 putpkt (rs->buf);
4961 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 4962
6d820c5c 4963 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
4964 {
4965 case PACKET_ERROR:
4966 case PACKET_UNKNOWN:
4967 return -1;
4968 case PACKET_OK:
4969 return 0;
4970 }
8e65ff28 4971 internal_error (__FILE__, __LINE__,
e2e0b3e5 4972 _("remote_insert_hw_breakpoint: reached end of function"));
96baa820
JM
4973}
4974
d471ea57 4975
802188a7 4976static int
8181d85f 4977remote_remove_hw_breakpoint (struct bp_target_info *bp_tgt)
96baa820 4978{
8181d85f 4979 CORE_ADDR addr;
d01949b6 4980 struct remote_state *rs = get_remote_state ();
6d820c5c 4981 char *p = rs->buf;
c8189ed1 4982
444abaca 4983 if (remote_protocol_packets[PACKET_Z1].support == PACKET_DISABLE)
5cffb350 4984 return -1;
802188a7 4985
96baa820
JM
4986 *(p++) = 'z';
4987 *(p++) = '1';
4988 *(p++) = ',';
802188a7 4989
8181d85f 4990 addr = remote_address_masked (bp_tgt->placed_address);
96baa820 4991 p += hexnumstr (p, (ULONGEST) addr);
8181d85f 4992 sprintf (p, ",%x", bp_tgt->placed_size);
96baa820 4993
6d820c5c
DJ
4994 putpkt (rs->buf);
4995 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 4996
6d820c5c 4997 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
4998 {
4999 case PACKET_ERROR:
5000 case PACKET_UNKNOWN:
5001 return -1;
5002 case PACKET_OK:
5003 return 0;
5004 }
8e65ff28 5005 internal_error (__FILE__, __LINE__,
e2e0b3e5 5006 _("remote_remove_hw_breakpoint: reached end of function"));
96baa820 5007}
96baa820 5008
c906108c
SS
5009/* Some targets are only capable of doing downloads, and afterwards
5010 they switch to the remote serial protocol. This function provides
5011 a clean way to get from the download target to the remote target.
5012 It's basically just a wrapper so that we don't have to expose any
5013 of the internal workings of remote.c.
5014
5015 Prior to calling this routine, you should shutdown the current
5016 target code, else you will get the "A program is being debugged
5017 already..." message. Usually a call to pop_target() suffices. */
5018
5019void
fba45db2 5020push_remote_target (char *name, int from_tty)
c906108c 5021{
a3f17187 5022 printf_filtered (_("Switching to remote protocol\n"));
c906108c
SS
5023 remote_open (name, from_tty);
5024}
5025
23860348 5026/* Table used by the crc32 function to calcuate the checksum. */
c906108c 5027
c5aa993b
JM
5028static unsigned long crc32_table[256] =
5029{0, 0};
c906108c
SS
5030
5031static unsigned long
fba45db2 5032crc32 (unsigned char *buf, int len, unsigned int crc)
c906108c 5033{
c5aa993b 5034 if (!crc32_table[1])
c906108c 5035 {
23860348 5036 /* Initialize the CRC table and the decoding table. */
c906108c
SS
5037 int i, j;
5038 unsigned int c;
5039
5040 for (i = 0; i < 256; i++)
c5aa993b
JM
5041 {
5042 for (c = i << 24, j = 8; j > 0; --j)
5043 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
5044 crc32_table[i] = c;
5045 }
c906108c
SS
5046 }
5047
5048 while (len--)
5049 {
5050 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buf) & 255];
5051 buf++;
5052 }
5053 return crc;
5054}
5055
5056/* compare-sections command
5057
5058 With no arguments, compares each loadable section in the exec bfd
5059 with the same memory range on the target, and reports mismatches.
5060 Useful for verifying the image on the target against the exec file.
5061 Depends on the target understanding the new "qCRC:" request. */
5062
e514a9d6
JM
5063/* FIXME: cagney/1999-10-26: This command should be broken down into a
5064 target method (target verify memory) and generic version of the
5065 actual command. This will allow other high-level code (especially
23860348 5066 generic_load()) to make use of this target functionality. */
e514a9d6 5067
c906108c 5068static void
fba45db2 5069compare_sections_command (char *args, int from_tty)
c906108c 5070{
d01949b6 5071 struct remote_state *rs = get_remote_state ();
c906108c
SS
5072 asection *s;
5073 unsigned long host_crc, target_crc;
5074 extern bfd *exec_bfd;
5075 struct cleanup *old_chain;
085dd6e6
JM
5076 char *tmp;
5077 char *sectdata;
ce359b09 5078 const char *sectname;
c906108c
SS
5079 bfd_size_type size;
5080 bfd_vma lma;
5081 int matched = 0;
5082 int mismatched = 0;
5083
5084 if (!exec_bfd)
8a3fe4f8 5085 error (_("command cannot be used without an exec file"));
c906108c
SS
5086 if (!current_target.to_shortname ||
5087 strcmp (current_target.to_shortname, "remote") != 0)
8a3fe4f8 5088 error (_("command can only be used with remote target"));
c906108c 5089
c5aa993b 5090 for (s = exec_bfd->sections; s; s = s->next)
c906108c
SS
5091 {
5092 if (!(s->flags & SEC_LOAD))
c5aa993b 5093 continue; /* skip non-loadable section */
c906108c 5094
2c500098 5095 size = bfd_get_section_size (s);
c906108c 5096 if (size == 0)
c5aa993b 5097 continue; /* skip zero-length section */
c906108c 5098
ce359b09 5099 sectname = bfd_get_section_name (exec_bfd, s);
c906108c 5100 if (args && strcmp (args, sectname) != 0)
c5aa993b 5101 continue; /* not the section selected by user */
c906108c 5102
c5aa993b 5103 matched = 1; /* do this section */
c906108c 5104 lma = s->lma;
23860348 5105 /* FIXME: assumes lma can fit into long. */
ea9c271d 5106 xsnprintf (rs->buf, get_remote_packet_size (), "qCRC:%lx,%lx",
ecbc58df 5107 (long) lma, (long) size);
6d820c5c 5108 putpkt (rs->buf);
c906108c 5109
23860348
MS
5110 /* Be clever; compute the host_crc before waiting for target
5111 reply. */
c906108c 5112 sectdata = xmalloc (size);
b8c9b27d 5113 old_chain = make_cleanup (xfree, sectdata);
c906108c
SS
5114 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
5115 host_crc = crc32 ((unsigned char *) sectdata, size, 0xffffffff);
5116
6d820c5c
DJ
5117 getpkt (&rs->buf, &rs->buf_size, 0);
5118 if (rs->buf[0] == 'E')
8a3fe4f8 5119 error (_("target memory fault, section %s, range 0x%s -- 0x%s"),
823ca731 5120 sectname, paddr (lma), paddr (lma + size));
6d820c5c 5121 if (rs->buf[0] != 'C')
8a3fe4f8 5122 error (_("remote target does not support this operation"));
c906108c 5123
6d820c5c 5124 for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++)
c906108c
SS
5125 target_crc = target_crc * 16 + fromhex (*tmp);
5126
d4f3574e
SS
5127 printf_filtered ("Section %s, range 0x%s -- 0x%s: ",
5128 sectname, paddr (lma), paddr (lma + size));
c906108c
SS
5129 if (host_crc == target_crc)
5130 printf_filtered ("matched.\n");
5131 else
c5aa993b
JM
5132 {
5133 printf_filtered ("MIS-MATCHED!\n");
5134 mismatched++;
5135 }
c906108c
SS
5136
5137 do_cleanups (old_chain);
5138 }
5139 if (mismatched > 0)
8a3fe4f8
AC
5140 warning (_("One or more sections of the remote executable does not match\n\
5141the loaded file\n"));
c906108c 5142 if (args && !matched)
a3f17187 5143 printf_filtered (_("No loaded section named '%s'.\n"), args);
c906108c
SS
5144}
5145
0876f84a
DJ
5146/* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
5147 Data at OFFSET, of up to LEN bytes, is read into READBUF; the
5148 number of bytes read is returned, or 0 for EOF, or -1 for error.
5149 The number of bytes read may be less than LEN without indicating an
5150 EOF. PACKET is checked and updated to indicate whether the remote
5151 target supports this object. */
5152
5153static LONGEST
5154remote_read_qxfer (struct target_ops *ops, const char *object_name,
5155 const char *annex,
5156 gdb_byte *readbuf, ULONGEST offset, LONGEST len,
5157 struct packet_config *packet)
5158{
5159 static char *finished_object;
5160 static char *finished_annex;
5161 static ULONGEST finished_offset;
5162
5163 struct remote_state *rs = get_remote_state ();
5164 unsigned int total = 0;
5165 LONGEST i, n, packet_len;
5166
5167 if (packet->support == PACKET_DISABLE)
5168 return -1;
5169
5170 /* Check whether we've cached an end-of-object packet that matches
5171 this request. */
5172 if (finished_object)
5173 {
5174 if (strcmp (object_name, finished_object) == 0
5175 && strcmp (annex ? annex : "", finished_annex) == 0
5176 && offset == finished_offset)
5177 return 0;
5178
5179 /* Otherwise, we're now reading something different. Discard
5180 the cache. */
5181 xfree (finished_object);
5182 xfree (finished_annex);
5183 finished_object = NULL;
5184 finished_annex = NULL;
5185 }
5186
5187 /* Request only enough to fit in a single packet. The actual data
5188 may not, since we don't know how much of it will need to be escaped;
5189 the target is free to respond with slightly less data. We subtract
5190 five to account for the response type and the protocol frame. */
5191 n = min (get_remote_packet_size () - 5, len);
5192 snprintf (rs->buf, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s",
5193 object_name, annex ? annex : "",
5194 phex_nz (offset, sizeof offset),
5195 phex_nz (n, sizeof n));
5196 i = putpkt (rs->buf);
5197 if (i < 0)
5198 return -1;
5199
5200 rs->buf[0] = '\0';
5201 packet_len = getpkt_sane (&rs->buf, &rs->buf_size, 0);
5202 if (packet_len < 0 || packet_ok (rs->buf, packet) != PACKET_OK)
5203 return -1;
5204
5205 if (rs->buf[0] != 'l' && rs->buf[0] != 'm')
5206 error (_("Unknown remote qXfer reply: %s"), rs->buf);
5207
5208 /* 'm' means there is (or at least might be) more data after this
5209 batch. That does not make sense unless there's at least one byte
5210 of data in this reply. */
5211 if (rs->buf[0] == 'm' && packet_len == 1)
5212 error (_("Remote qXfer reply contained no data."));
5213
5214 /* Got some data. */
5215 i = remote_unescape_input (rs->buf + 1, packet_len - 1, readbuf, n);
5216
5217 /* 'l' is an EOF marker, possibly including a final block of data,
5218 or possibly empty. Record it to bypass the next read, if one is
5219 issued. */
5220 if (rs->buf[0] == 'l')
5221 {
5222 finished_object = xstrdup (object_name);
5223 finished_annex = xstrdup (annex ? annex : "");
5224 finished_offset = offset + i;
5225 }
5226
5227 return i;
5228}
5229
1e3ff5ad 5230static LONGEST
4b8a223f 5231remote_xfer_partial (struct target_ops *ops, enum target_object object,
961cb7b5
MK
5232 const char *annex, gdb_byte *readbuf,
5233 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
c906108c 5234{
d01949b6 5235 struct remote_state *rs = get_remote_state ();
c906108c 5236 int i;
6d820c5c 5237 char *p2;
1e3ff5ad 5238 char query_type;
c906108c 5239
21e3b9b9
DJ
5240 /* Handle memory using remote_xfer_memory. */
5241 if (object == TARGET_OBJECT_MEMORY)
5242 {
5243 int xfered;
5244 errno = 0;
5245
5246 if (writebuf != NULL)
5247 {
5248 void *buffer = xmalloc (len);
5249 struct cleanup *cleanup = make_cleanup (xfree, buffer);
5250 memcpy (buffer, writebuf, len);
5251 xfered = remote_xfer_memory (offset, buffer, len, 1, NULL, ops);
5252 do_cleanups (cleanup);
5253 }
5254 else
5255 xfered = remote_xfer_memory (offset, readbuf, len, 0, NULL, ops);
5256
5257 if (xfered > 0)
5258 return xfered;
5259 else if (xfered == 0 && errno == 0)
5260 return 0;
5261 else
5262 return -1;
5263 }
5264
4b8a223f
AC
5265 /* Only handle reads. */
5266 if (writebuf != NULL || readbuf == NULL)
5267 return -1;
5268
1e3ff5ad
AC
5269 /* Map pre-existing objects onto letters. DO NOT do this for new
5270 objects!!! Instead specify new query packets. */
5271 switch (object)
c906108c 5272 {
1e3ff5ad
AC
5273 case TARGET_OBJECT_AVR:
5274 query_type = 'R';
5275 break;
802188a7
RM
5276
5277 case TARGET_OBJECT_AUXV:
0876f84a
DJ
5278 gdb_assert (annex == NULL);
5279 return remote_read_qxfer (ops, "auxv", annex, readbuf, offset, len,
5280 &remote_protocol_packets[PACKET_qXfer_auxv]);
802188a7 5281
1e3ff5ad 5282 default:
c906108c
SS
5283 return -1;
5284 }
5285
4b8a223f 5286 /* Note: a zero OFFSET and LEN can be used to query the minimum
1e3ff5ad 5287 buffer size. */
4b8a223f 5288 if (offset == 0 && len == 0)
ea9c271d
DJ
5289 return (get_remote_packet_size ());
5290 /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
24b06219 5291 large enough let the caller deal with it. */
ea9c271d 5292 if (len < get_remote_packet_size ())
1e3ff5ad 5293 return -1;
ea9c271d 5294 len = get_remote_packet_size ();
1e3ff5ad 5295
23860348 5296 /* Except for querying the minimum buffer size, target must be open. */
c5aa993b 5297 if (!remote_desc)
8a3fe4f8 5298 error (_("remote query is only available after target open"));
c906108c 5299
1e3ff5ad 5300 gdb_assert (annex != NULL);
4b8a223f 5301 gdb_assert (readbuf != NULL);
c906108c 5302
6d820c5c 5303 p2 = rs->buf;
c906108c
SS
5304 *p2++ = 'q';
5305 *p2++ = query_type;
5306
23860348
MS
5307 /* We used one buffer char for the remote protocol q command and
5308 another for the query type. As the remote protocol encapsulation
5309 uses 4 chars plus one extra in case we are debugging
5310 (remote_debug), we have PBUFZIZ - 7 left to pack the query
5311 string. */
c906108c 5312 i = 0;
ea9c271d 5313 while (annex[i] && (i < (get_remote_packet_size () - 8)))
c906108c 5314 {
1e3ff5ad
AC
5315 /* Bad caller may have sent forbidden characters. */
5316 gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#');
5317 *p2++ = annex[i];
c906108c
SS
5318 i++;
5319 }
1e3ff5ad
AC
5320 *p2 = '\0';
5321 gdb_assert (annex[i] == '\0');
c906108c 5322
6d820c5c 5323 i = putpkt (rs->buf);
c5aa993b
JM
5324 if (i < 0)
5325 return i;
c906108c 5326
6d820c5c
DJ
5327 getpkt (&rs->buf, &rs->buf_size, 0);
5328 strcpy ((char *) readbuf, rs->buf);
c906108c 5329
cfd77fa1 5330 return strlen ((char *) readbuf);
c906108c
SS
5331}
5332
96baa820
JM
5333static void
5334remote_rcmd (char *command,
d9fcf2fb 5335 struct ui_file *outbuf)
96baa820 5336{
d01949b6 5337 struct remote_state *rs = get_remote_state ();
6d820c5c 5338 char *buf = rs->buf;
96baa820
JM
5339 char *p = buf;
5340
5341 if (!remote_desc)
8a3fe4f8 5342 error (_("remote rcmd is only available after target open"));
96baa820 5343
23860348 5344 /* Send a NULL command across as an empty command. */
7be570e7
JM
5345 if (command == NULL)
5346 command = "";
5347
23860348 5348 /* The query prefix. */
96baa820
JM
5349 strcpy (buf, "qRcmd,");
5350 p = strchr (buf, '\0');
5351
ea9c271d 5352 if ((strlen (buf) + strlen (command) * 2 + 8/*misc*/) > get_remote_packet_size ())
8a3fe4f8 5353 error (_("\"monitor\" command ``%s'' is too long."), command);
96baa820 5354
23860348 5355 /* Encode the actual command. */
cfd77fa1 5356 bin2hex ((gdb_byte *) command, p, 0);
96baa820 5357
6d820c5c 5358 if (putpkt (rs->buf) < 0)
8a3fe4f8 5359 error (_("Communication problem with target."));
96baa820
JM
5360
5361 /* get/display the response */
5362 while (1)
5363 {
23860348 5364 /* XXX - see also tracepoint.c:remote_get_noisy_reply(). */
96baa820 5365 buf[0] = '\0';
6d820c5c 5366 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 5367 if (buf[0] == '\0')
8a3fe4f8 5368 error (_("Target does not support this command."));
96baa820
JM
5369 if (buf[0] == 'O' && buf[1] != 'K')
5370 {
23860348 5371 remote_console_output (buf + 1); /* 'O' message from stub. */
96baa820
JM
5372 continue;
5373 }
5374 if (strcmp (buf, "OK") == 0)
5375 break;
7be570e7
JM
5376 if (strlen (buf) == 3 && buf[0] == 'E'
5377 && isdigit (buf[1]) && isdigit (buf[2]))
5378 {
8a3fe4f8 5379 error (_("Protocol error with Rcmd"));
7be570e7 5380 }
96baa820
JM
5381 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
5382 {
5383 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
5384 fputc_unfiltered (c, outbuf);
5385 }
5386 break;
5387 }
5388}
5389
c906108c 5390static void
fba45db2 5391packet_command (char *args, int from_tty)
c906108c 5392{
d01949b6 5393 struct remote_state *rs = get_remote_state ();
c906108c 5394
c5aa993b 5395 if (!remote_desc)
8a3fe4f8 5396 error (_("command can only be used with remote target"));
c906108c 5397
c5aa993b 5398 if (!args)
8a3fe4f8 5399 error (_("remote-packet command requires packet text as argument"));
c906108c
SS
5400
5401 puts_filtered ("sending: ");
5402 print_packet (args);
5403 puts_filtered ("\n");
5404 putpkt (args);
5405
6d820c5c 5406 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 5407 puts_filtered ("received: ");
6d820c5c 5408 print_packet (rs->buf);
c906108c
SS
5409 puts_filtered ("\n");
5410}
5411
5412#if 0
23860348 5413/* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
c906108c 5414
a14ed312 5415static void display_thread_info (struct gdb_ext_thread_info *info);
c906108c 5416
a14ed312 5417static void threadset_test_cmd (char *cmd, int tty);
c906108c 5418
a14ed312 5419static void threadalive_test (char *cmd, int tty);
c906108c 5420
a14ed312 5421static void threadlist_test_cmd (char *cmd, int tty);
c906108c 5422
23860348 5423int get_and_display_threadinfo (threadref *ref);
c906108c 5424
a14ed312 5425static void threadinfo_test_cmd (char *cmd, int tty);
c906108c 5426
23860348 5427static int thread_display_step (threadref *ref, void *context);
c906108c 5428
a14ed312 5429static void threadlist_update_test_cmd (char *cmd, int tty);
c906108c 5430
a14ed312 5431static void init_remote_threadtests (void);
c906108c 5432
23860348 5433#define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
c906108c
SS
5434
5435static void
fba45db2 5436threadset_test_cmd (char *cmd, int tty)
c906108c
SS
5437{
5438 int sample_thread = SAMPLE_THREAD;
5439
a3f17187 5440 printf_filtered (_("Remote threadset test\n"));
c906108c
SS
5441 set_thread (sample_thread, 1);
5442}
5443
5444
5445static void
fba45db2 5446threadalive_test (char *cmd, int tty)
c906108c
SS
5447{
5448 int sample_thread = SAMPLE_THREAD;
5449
39f77062 5450 if (remote_thread_alive (pid_to_ptid (sample_thread)))
c906108c
SS
5451 printf_filtered ("PASS: Thread alive test\n");
5452 else
5453 printf_filtered ("FAIL: Thread alive test\n");
5454}
5455
23860348 5456void output_threadid (char *title, threadref *ref);
c906108c
SS
5457
5458void
fba45db2 5459output_threadid (char *title, threadref *ref)
c906108c
SS
5460{
5461 char hexid[20];
5462
23860348 5463 pack_threadid (&hexid[0], ref); /* Convert threead id into hex. */
c906108c
SS
5464 hexid[16] = 0;
5465 printf_filtered ("%s %s\n", title, (&hexid[0]));
5466}
5467
5468static void
fba45db2 5469threadlist_test_cmd (char *cmd, int tty)
c906108c
SS
5470{
5471 int startflag = 1;
5472 threadref nextthread;
5473 int done, result_count;
5474 threadref threadlist[3];
5475
5476 printf_filtered ("Remote Threadlist test\n");
5477 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
5478 &result_count, &threadlist[0]))
5479 printf_filtered ("FAIL: threadlist test\n");
5480 else
5481 {
5482 threadref *scan = threadlist;
5483 threadref *limit = scan + result_count;
5484
5485 while (scan < limit)
5486 output_threadid (" thread ", scan++);
5487 }
5488}
5489
5490void
fba45db2 5491display_thread_info (struct gdb_ext_thread_info *info)
c906108c
SS
5492{
5493 output_threadid ("Threadid: ", &info->threadid);
5494 printf_filtered ("Name: %s\n ", info->shortname);
5495 printf_filtered ("State: %s\n", info->display);
5496 printf_filtered ("other: %s\n\n", info->more_display);
5497}
5498
5499int
fba45db2 5500get_and_display_threadinfo (threadref *ref)
c906108c
SS
5501{
5502 int result;
5503 int set;
5504 struct gdb_ext_thread_info threadinfo;
5505
5506 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
5507 | TAG_MOREDISPLAY | TAG_DISPLAY;
5508 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
5509 display_thread_info (&threadinfo);
5510 return result;
5511}
5512
5513static void
fba45db2 5514threadinfo_test_cmd (char *cmd, int tty)
c906108c
SS
5515{
5516 int athread = SAMPLE_THREAD;
5517 threadref thread;
5518 int set;
5519
5520 int_to_threadref (&thread, athread);
5521 printf_filtered ("Remote Threadinfo test\n");
5522 if (!get_and_display_threadinfo (&thread))
5523 printf_filtered ("FAIL cannot get thread info\n");
5524}
5525
5526static int
fba45db2 5527thread_display_step (threadref *ref, void *context)
c906108c
SS
5528{
5529 /* output_threadid(" threadstep ",ref); *//* simple test */
5530 return get_and_display_threadinfo (ref);
5531}
5532
5533static void
fba45db2 5534threadlist_update_test_cmd (char *cmd, int tty)
c906108c
SS
5535{
5536 printf_filtered ("Remote Threadlist update test\n");
5537 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
5538}
5539
5540static void
5541init_remote_threadtests (void)
5542{
1bedd215
AC
5543 add_com ("tlist", class_obscure, threadlist_test_cmd, _("\
5544Fetch and print the remote list of thread identifiers, one pkt only"));
c906108c 5545 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
1bedd215 5546 _("Fetch and display info about one thread"));
c906108c 5547 add_com ("tset", class_obscure, threadset_test_cmd,
1bedd215 5548 _("Test setting to a different thread"));
c906108c 5549 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
1bedd215 5550 _("Iterate through updating all remote thread info"));
c906108c 5551 add_com ("talive", class_obscure, threadalive_test,
1bedd215 5552 _(" Remote thread alive test "));
c906108c
SS
5553}
5554
5555#endif /* 0 */
5556
f3fb8c85
MS
5557/* Convert a thread ID to a string. Returns the string in a static
5558 buffer. */
5559
5560static char *
39f77062 5561remote_pid_to_str (ptid_t ptid)
f3fb8c85 5562{
fd0a2a6f 5563 static char buf[32];
f3fb8c85 5564
6c76e4a1 5565 xsnprintf (buf, sizeof buf, "thread %d", ptid_get_pid (ptid));
f3fb8c85
MS
5566 return buf;
5567}
5568
38691318
KB
5569/* Get the address of the thread local variable in OBJFILE which is
5570 stored at OFFSET within the thread local storage for thread PTID. */
5571
5572static CORE_ADDR
5573remote_get_thread_local_address (ptid_t ptid, CORE_ADDR lm, CORE_ADDR offset)
5574{
444abaca 5575 if (remote_protocol_packets[PACKET_qGetTLSAddr].support != PACKET_DISABLE)
38691318
KB
5576 {
5577 struct remote_state *rs = get_remote_state ();
6d820c5c 5578 char *p = rs->buf;
571dd617 5579 enum packet_result result;
38691318
KB
5580
5581 strcpy (p, "qGetTLSAddr:");
5582 p += strlen (p);
5583 p += hexnumstr (p, PIDGET (ptid));
5584 *p++ = ',';
5585 p += hexnumstr (p, offset);
5586 *p++ = ',';
5587 p += hexnumstr (p, lm);
5588 *p++ = '\0';
5589
6d820c5c
DJ
5590 putpkt (rs->buf);
5591 getpkt (&rs->buf, &rs->buf_size, 0);
5592 result = packet_ok (rs->buf, &remote_protocol_packets[PACKET_qGetTLSAddr]);
571dd617 5593 if (result == PACKET_OK)
38691318
KB
5594 {
5595 ULONGEST result;
5596
6d820c5c 5597 unpack_varlen_hex (rs->buf, &result);
38691318
KB
5598 return result;
5599 }
571dd617 5600 else if (result == PACKET_UNKNOWN)
109c3e39
AC
5601 throw_error (TLS_GENERIC_ERROR,
5602 _("Remote target doesn't support qGetTLSAddr packet"));
38691318 5603 else
109c3e39
AC
5604 throw_error (TLS_GENERIC_ERROR,
5605 _("Remote target failed to process qGetTLSAddr request"));
38691318
KB
5606 }
5607 else
109c3e39
AC
5608 throw_error (TLS_GENERIC_ERROR,
5609 _("TLS not supported or disabled on this target"));
38691318
KB
5610 /* Not reached. */
5611 return 0;
5612}
5613
c906108c 5614static void
fba45db2 5615init_remote_ops (void)
c906108c 5616{
c5aa993b 5617 remote_ops.to_shortname = "remote";
c906108c 5618 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
c5aa993b 5619 remote_ops.to_doc =
c906108c 5620 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
0d06e24b
JM
5621Specify the serial device it is connected to\n\
5622(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
c5aa993b
JM
5623 remote_ops.to_open = remote_open;
5624 remote_ops.to_close = remote_close;
c906108c 5625 remote_ops.to_detach = remote_detach;
6ad8ae5c 5626 remote_ops.to_disconnect = remote_disconnect;
c5aa993b 5627 remote_ops.to_resume = remote_resume;
c906108c
SS
5628 remote_ops.to_wait = remote_wait;
5629 remote_ops.to_fetch_registers = remote_fetch_registers;
5630 remote_ops.to_store_registers = remote_store_registers;
5631 remote_ops.to_prepare_to_store = remote_prepare_to_store;
c8e73a31 5632 remote_ops.deprecated_xfer_memory = remote_xfer_memory;
c5aa993b 5633 remote_ops.to_files_info = remote_files_info;
c906108c
SS
5634 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
5635 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
3c3bea1c
GS
5636 remote_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
5637 remote_ops.to_stopped_data_address = remote_stopped_data_address;
5638 remote_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
5639 remote_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
5640 remote_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
5641 remote_ops.to_insert_watchpoint = remote_insert_watchpoint;
5642 remote_ops.to_remove_watchpoint = remote_remove_watchpoint;
c5aa993b
JM
5643 remote_ops.to_kill = remote_kill;
5644 remote_ops.to_load = generic_load;
c906108c
SS
5645 remote_ops.to_mourn_inferior = remote_mourn;
5646 remote_ops.to_thread_alive = remote_thread_alive;
0f71a2f6 5647 remote_ops.to_find_new_threads = remote_threads_info;
0caabb7e 5648 remote_ops.to_pid_to_str = remote_pid_to_str;
cf759d3b 5649 remote_ops.to_extra_thread_info = remote_threads_extra_info;
c906108c 5650 remote_ops.to_stop = remote_stop;
4b8a223f 5651 remote_ops.to_xfer_partial = remote_xfer_partial;
96baa820 5652 remote_ops.to_rcmd = remote_rcmd;
38691318 5653 remote_ops.to_get_thread_local_address = remote_get_thread_local_address;
c906108c 5654 remote_ops.to_stratum = process_stratum;
c5aa993b
JM
5655 remote_ops.to_has_all_memory = 1;
5656 remote_ops.to_has_memory = 1;
5657 remote_ops.to_has_stack = 1;
5658 remote_ops.to_has_registers = 1;
5659 remote_ops.to_has_execution = 1;
5660 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
5661 remote_ops.to_magic = OPS_MAGIC;
c906108c
SS
5662}
5663
5664/* Set up the extended remote vector by making a copy of the standard
5665 remote vector and adding to it. */
5666
5667static void
fba45db2 5668init_extended_remote_ops (void)
c906108c
SS
5669{
5670 extended_remote_ops = remote_ops;
5671
0f71a2f6 5672 extended_remote_ops.to_shortname = "extended-remote";
c5aa993b 5673 extended_remote_ops.to_longname =
c906108c 5674 "Extended remote serial target in gdb-specific protocol";
c5aa993b 5675 extended_remote_ops.to_doc =
c906108c
SS
5676 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5677Specify the serial device it is connected to (e.g. /dev/ttya).",
c5aa993b 5678 extended_remote_ops.to_open = extended_remote_open;
c906108c
SS
5679 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
5680 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
0f71a2f6
JM
5681}
5682
6426a772
JM
5683static int
5684remote_can_async_p (void)
5685{
23860348 5686 /* We're async whenever the serial device is. */
2cd58942 5687 return (current_target.to_async_mask_value) && serial_can_async_p (remote_desc);
6426a772
JM
5688}
5689
5690static int
5691remote_is_async_p (void)
5692{
23860348 5693 /* We're async whenever the serial device is. */
2cd58942 5694 return (current_target.to_async_mask_value) && serial_is_async_p (remote_desc);
6426a772
JM
5695}
5696
2acceee2
JM
5697/* Pass the SERIAL event on and up to the client. One day this code
5698 will be able to delay notifying the client of an event until the
23860348 5699 point where an entire packet has been received. */
2acceee2 5700
23860348
MS
5701static void (*async_client_callback) (enum inferior_event_type event_type,
5702 void *context);
2acceee2
JM
5703static void *async_client_context;
5704static serial_event_ftype remote_async_serial_handler;
5705
6426a772 5706static void
819cc324 5707remote_async_serial_handler (struct serial *scb, void *context)
6426a772 5708{
2acceee2
JM
5709 /* Don't propogate error information up to the client. Instead let
5710 the client find out about the error by querying the target. */
5711 async_client_callback (INF_REG_EVENT, async_client_context);
5712}
5713
5714static void
23860348
MS
5715remote_async (void (*callback) (enum inferior_event_type event_type,
5716 void *context), void *context)
2acceee2 5717{
ed9a39eb 5718 if (current_target.to_async_mask_value == 0)
8e65ff28 5719 internal_error (__FILE__, __LINE__,
e2e0b3e5 5720 _("Calling remote_async when async is masked"));
ed9a39eb 5721
2acceee2
JM
5722 if (callback != NULL)
5723 {
2cd58942 5724 serial_async (remote_desc, remote_async_serial_handler, NULL);
2acceee2
JM
5725 async_client_callback = callback;
5726 async_client_context = context;
5727 }
5728 else
2cd58942 5729 serial_async (remote_desc, NULL, NULL);
6426a772
JM
5730}
5731
43ff13b4
JM
5732/* Target async and target extended-async.
5733
5734 This are temporary targets, until it is all tested. Eventually
5735 async support will be incorporated int the usual 'remote'
23860348 5736 target. */
43ff13b4
JM
5737
5738static void
c2d11a7d 5739init_remote_async_ops (void)
43ff13b4
JM
5740{
5741 remote_async_ops.to_shortname = "async";
23860348
MS
5742 remote_async_ops.to_longname =
5743 "Remote serial target in async version of the gdb-specific protocol";
c5aa993b 5744 remote_async_ops.to_doc =
43ff13b4
JM
5745 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5746Specify the serial device it is connected to (e.g. /dev/ttya).";
c5aa993b
JM
5747 remote_async_ops.to_open = remote_async_open;
5748 remote_async_ops.to_close = remote_close;
6ad8ae5c
DJ
5749 remote_async_ops.to_detach = remote_detach;
5750 remote_async_ops.to_disconnect = remote_disconnect;
c5aa993b
JM
5751 remote_async_ops.to_resume = remote_async_resume;
5752 remote_async_ops.to_wait = remote_async_wait;
5753 remote_async_ops.to_fetch_registers = remote_fetch_registers;
5754 remote_async_ops.to_store_registers = remote_store_registers;
5755 remote_async_ops.to_prepare_to_store = remote_prepare_to_store;
c8e73a31 5756 remote_async_ops.deprecated_xfer_memory = remote_xfer_memory;
c5aa993b 5757 remote_async_ops.to_files_info = remote_files_info;
43ff13b4
JM
5758 remote_async_ops.to_insert_breakpoint = remote_insert_breakpoint;
5759 remote_async_ops.to_remove_breakpoint = remote_remove_breakpoint;
3c3bea1c
GS
5760 remote_async_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
5761 remote_async_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
5762 remote_async_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
5763 remote_async_ops.to_insert_watchpoint = remote_insert_watchpoint;
5764 remote_async_ops.to_remove_watchpoint = remote_remove_watchpoint;
5765 remote_async_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
5766 remote_async_ops.to_stopped_data_address = remote_stopped_data_address;
6426a772
JM
5767 remote_async_ops.to_terminal_inferior = remote_async_terminal_inferior;
5768 remote_async_ops.to_terminal_ours = remote_async_terminal_ours;
c5aa993b
JM
5769 remote_async_ops.to_kill = remote_async_kill;
5770 remote_async_ops.to_load = generic_load;
53a5351d 5771 remote_async_ops.to_mourn_inferior = remote_async_mourn;
c5aa993b
JM
5772 remote_async_ops.to_thread_alive = remote_thread_alive;
5773 remote_async_ops.to_find_new_threads = remote_threads_info;
cf759d3b
ND
5774 remote_async_ops.to_pid_to_str = remote_pid_to_str;
5775 remote_async_ops.to_extra_thread_info = remote_threads_extra_info;
43ff13b4 5776 remote_async_ops.to_stop = remote_stop;
4b8a223f 5777 remote_async_ops.to_xfer_partial = remote_xfer_partial;
96baa820 5778 remote_async_ops.to_rcmd = remote_rcmd;
c5aa993b
JM
5779 remote_async_ops.to_stratum = process_stratum;
5780 remote_async_ops.to_has_all_memory = 1;
5781 remote_async_ops.to_has_memory = 1;
5782 remote_async_ops.to_has_stack = 1;
5783 remote_async_ops.to_has_registers = 1;
5784 remote_async_ops.to_has_execution = 1;
5785 remote_async_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
6426a772
JM
5786 remote_async_ops.to_can_async_p = remote_can_async_p;
5787 remote_async_ops.to_is_async_p = remote_is_async_p;
5788 remote_async_ops.to_async = remote_async;
ed9a39eb 5789 remote_async_ops.to_async_mask_value = 1;
c5aa993b 5790 remote_async_ops.to_magic = OPS_MAGIC;
43ff13b4
JM
5791}
5792
5793/* Set up the async extended remote vector by making a copy of the standard
5794 remote vector and adding to it. */
5795
5796static void
c2d11a7d 5797init_extended_async_remote_ops (void)
43ff13b4
JM
5798{
5799 extended_async_remote_ops = remote_async_ops;
5800
5801 extended_async_remote_ops.to_shortname = "extended-async";
c5aa993b 5802 extended_async_remote_ops.to_longname =
43ff13b4 5803 "Extended remote serial target in async gdb-specific protocol";
c5aa993b 5804 extended_async_remote_ops.to_doc =
43ff13b4
JM
5805 "Use a remote computer via a serial line, using an async gdb-specific protocol.\n\
5806Specify the serial device it is connected to (e.g. /dev/ttya).",
c5aa993b 5807 extended_async_remote_ops.to_open = extended_remote_async_open;
43ff13b4
JM
5808 extended_async_remote_ops.to_create_inferior = extended_remote_async_create_inferior;
5809 extended_async_remote_ops.to_mourn_inferior = extended_remote_mourn;
5810}
5811
5a2468f5 5812static void
c2d11a7d 5813set_remote_cmd (char *args, int from_tty)
5a2468f5 5814{
5a2468f5
JM
5815}
5816
d471ea57
AC
5817static void
5818show_remote_cmd (char *args, int from_tty)
5819{
37a105a1
DJ
5820 /* We can't just use cmd_show_list here, because we want to skip
5821 the redundant "show remote Z-packet". */
5822 struct cleanup *showlist_chain;
5823 struct cmd_list_element *list = remote_show_cmdlist;
5824
5825 showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist");
5826 for (; list != NULL; list = list->next)
5827 if (strcmp (list->name, "Z-packet") == 0)
5828 continue;
5829 else if (list->type == show_cmd)
5830 {
5831 struct cleanup *option_chain
5832 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
5833 ui_out_field_string (uiout, "name", list->name);
5834 ui_out_text (uiout, ": ");
5835 do_setshow_command ((char *) NULL, from_tty, list);
5836 /* Close the tuple. */
5837 do_cleanups (option_chain);
5838 }
d471ea57 5839}
5a2468f5 5840
0f71a2f6 5841static void
fba45db2 5842build_remote_gdbarch_data (void)
0f71a2f6 5843{
d696208f 5844 remote_address_size = TARGET_ADDR_BIT;
0f71a2f6
JM
5845}
5846
23860348 5847/* Saved pointer to previous owner of the new_objfile event. */
dc8acb97
MS
5848static void (*remote_new_objfile_chain) (struct objfile *);
5849
23860348 5850/* Function to be called whenever a new objfile (shlib) is detected. */
dc8acb97
MS
5851static void
5852remote_new_objfile (struct objfile *objfile)
5853{
23860348 5854 if (remote_desc != 0) /* Have a remote connection. */
dc8acb97
MS
5855 {
5856 remote_check_symbols (objfile);
5857 }
23860348 5858 /* Call predecessor on chain, if any. */
dc8acb97
MS
5859 if (remote_new_objfile_chain != 0 &&
5860 remote_desc == 0)
5861 remote_new_objfile_chain (objfile);
5862}
5863
c906108c 5864void
fba45db2 5865_initialize_remote (void)
c906108c 5866{
ea9c271d
DJ
5867 struct remote_state *rs;
5868
0f71a2f6 5869 /* architecture specific data */
23860348
MS
5870 remote_gdbarch_data_handle =
5871 gdbarch_data_register_post_init (init_remote_state);
d01949b6
AC
5872
5873 /* Old tacky stuff. NOTE: This comes after the remote protocol so
5874 that the remote protocol has been initialized. */
046a4708
AC
5875 DEPRECATED_REGISTER_GDBARCH_SWAP (remote_address_size);
5876 deprecated_register_gdbarch_swap (NULL, 0, build_remote_gdbarch_data);
0f71a2f6 5877
ea9c271d
DJ
5878 /* Initialize the per-target state. At the moment there is only one
5879 of these, not one per target. Only one target is active at a
5880 time. The default buffer size is unimportant; it will be expanded
5881 whenever a larger buffer is needed. */
5882 rs = get_remote_state ();
5883 rs->buf_size = 400;
5884 rs->buf = xmalloc (rs->buf_size);
5885
c906108c
SS
5886 init_remote_ops ();
5887 add_target (&remote_ops);
5888
5889 init_extended_remote_ops ();
5890 add_target (&extended_remote_ops);
cce74817 5891
43ff13b4
JM
5892 init_remote_async_ops ();
5893 add_target (&remote_async_ops);
5894
5895 init_extended_async_remote_ops ();
5896 add_target (&extended_async_remote_ops);
5897
dc8acb97 5898 /* Hook into new objfile notification. */
9a4105ab
AC
5899 remote_new_objfile_chain = deprecated_target_new_objfile_hook;
5900 deprecated_target_new_objfile_hook = remote_new_objfile;
dc8acb97 5901
c906108c
SS
5902#if 0
5903 init_remote_threadtests ();
5904#endif
5905
23860348 5906 /* set/show remote ... */
d471ea57 5907
1bedd215 5908 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, _("\
5a2468f5
JM
5909Remote protocol specific variables\n\
5910Configure various remote-protocol specific variables such as\n\
1bedd215 5911the packets being used"),
cff3e48b 5912 &remote_set_cmdlist, "set remote ",
23860348 5913 0 /* allow-unknown */, &setlist);
1bedd215 5914 add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\
5a2468f5
JM
5915Remote protocol specific variables\n\
5916Configure various remote-protocol specific variables such as\n\
1bedd215 5917the packets being used"),
cff3e48b 5918 &remote_show_cmdlist, "show remote ",
23860348 5919 0 /* allow-unknown */, &showlist);
5a2468f5 5920
1a966eab
AC
5921 add_cmd ("compare-sections", class_obscure, compare_sections_command, _("\
5922Compare section data on target to the exec file.\n\
5923Argument is a single section name (default: all loaded sections)."),
c906108c
SS
5924 &cmdlist);
5925
1a966eab
AC
5926 add_cmd ("packet", class_maintenance, packet_command, _("\
5927Send an arbitrary packet to a remote target.\n\
c906108c
SS
5928 maintenance packet TEXT\n\
5929If GDB is talking to an inferior via the GDB serial protocol, then\n\
5930this command sends the string TEXT to the inferior, and displays the\n\
5931response packet. GDB supplies the initial `$' character, and the\n\
1a966eab 5932terminating `#' character and checksum."),
c906108c
SS
5933 &maintenancelist);
5934
7915a72c
AC
5935 add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, _("\
5936Set whether to send break if interrupted."), _("\
5937Show whether to send break if interrupted."), _("\
5938If set, a break, instead of a cntrl-c, is sent to the remote target."),
2c5b56ce 5939 NULL, NULL, /* FIXME: i18n: Whether to send break if interrupted is %s. */
e707bbc2 5940 &setlist, &showlist);
c906108c 5941
23860348 5942 /* Install commands for configuring memory read/write packets. */
11cf8741 5943
1a966eab
AC
5944 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size, _("\
5945Set the maximum number of bytes per memory write packet (deprecated)."),
11cf8741 5946 &setlist);
1a966eab
AC
5947 add_cmd ("remotewritesize", no_class, show_memory_write_packet_size, _("\
5948Show the maximum number of bytes per memory write packet (deprecated)."),
11cf8741
JM
5949 &showlist);
5950 add_cmd ("memory-write-packet-size", no_class,
1a966eab
AC
5951 set_memory_write_packet_size, _("\
5952Set the maximum number of bytes per memory-write packet.\n\
5953Specify the number of bytes in a packet or 0 (zero) for the\n\
5954default packet size. The actual limit is further reduced\n\
5955dependent on the target. Specify ``fixed'' to disable the\n\
5956further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
5957 &remote_set_cmdlist);
5958 add_cmd ("memory-read-packet-size", no_class,
1a966eab
AC
5959 set_memory_read_packet_size, _("\
5960Set the maximum number of bytes per memory-read packet.\n\
5961Specify the number of bytes in a packet or 0 (zero) for the\n\
5962default packet size. The actual limit is further reduced\n\
5963dependent on the target. Specify ``fixed'' to disable the\n\
5964further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
5965 &remote_set_cmdlist);
5966 add_cmd ("memory-write-packet-size", no_class,
5967 show_memory_write_packet_size,
1a966eab 5968 _("Show the maximum number of bytes per memory-write packet."),
11cf8741
JM
5969 &remote_show_cmdlist);
5970 add_cmd ("memory-read-packet-size", no_class,
5971 show_memory_read_packet_size,
1a966eab 5972 _("Show the maximum number of bytes per memory-read packet."),
11cf8741 5973 &remote_show_cmdlist);
c906108c 5974
b3f42336 5975 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class,
7915a72c
AC
5976 &remote_hw_watchpoint_limit, _("\
5977Set the maximum number of target hardware watchpoints."), _("\
5978Show the maximum number of target hardware watchpoints."), _("\
5979Specify a negative limit for unlimited."),
2c5b56ce 5980 NULL, NULL, /* FIXME: i18n: The maximum number of target hardware watchpoints is %s. */
b3f42336
AC
5981 &remote_set_cmdlist, &remote_show_cmdlist);
5982 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class,
7915a72c
AC
5983 &remote_hw_breakpoint_limit, _("\
5984Set the maximum number of target hardware breakpoints."), _("\
5985Show the maximum number of target hardware breakpoints."), _("\
5986Specify a negative limit for unlimited."),
2c5b56ce 5987 NULL, NULL, /* FIXME: i18n: The maximum number of target hardware breakpoints is %s. */
b3f42336 5988 &remote_set_cmdlist, &remote_show_cmdlist);
501eef12 5989
4d28ad1e
AC
5990 add_setshow_integer_cmd ("remoteaddresssize", class_obscure,
5991 &remote_address_size, _("\
5992Set the maximum size of the address (in bits) in a memory packet."), _("\
5993Show the maximum size of the address (in bits) in a memory packet."), NULL,
5994 NULL,
5995 NULL, /* FIXME: i18n: */
5996 &setlist, &showlist);
c906108c 5997
444abaca 5998 add_packet_config_cmd (&remote_protocol_packets[PACKET_X],
bb572ddd 5999 "X", "binary-download", 1);
0f71a2f6 6000
444abaca 6001 add_packet_config_cmd (&remote_protocol_packets[PACKET_vCont],
bb572ddd 6002 "vCont", "verbose-resume", 0);
506fb367 6003
444abaca 6004 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
bb572ddd 6005 "qSymbol", "symbol-lookup", 0);
dc8acb97 6006
444abaca 6007 add_packet_config_cmd (&remote_protocol_packets[PACKET_P],
bb572ddd 6008 "P", "set-register", 1);
d471ea57 6009
444abaca 6010 add_packet_config_cmd (&remote_protocol_packets[PACKET_p],
bb572ddd 6011 "p", "fetch-register", 1);
b96ec7ac 6012
444abaca 6013 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z0],
bb572ddd 6014 "Z0", "software-breakpoint", 0);
d471ea57 6015
444abaca 6016 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z1],
bb572ddd 6017 "Z1", "hardware-breakpoint", 0);
d471ea57 6018
444abaca 6019 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z2],
bb572ddd 6020 "Z2", "write-watchpoint", 0);
d471ea57 6021
444abaca 6022 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z3],
bb572ddd 6023 "Z3", "read-watchpoint", 0);
d471ea57 6024
444abaca 6025 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z4],
bb572ddd 6026 "Z4", "access-watchpoint", 0);
d471ea57 6027
0876f84a
DJ
6028 add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_auxv],
6029 "qXfer:auxv:read", "read-aux-vector", 0);
802188a7 6030
444abaca 6031 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTLSAddr],
38691318 6032 "qGetTLSAddr", "get-thread-local-storage-address",
38691318
KB
6033 0);
6034
be2a5f71
DJ
6035 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSupported],
6036 "qSupported", "supported-packets", 0);
6037
37a105a1
DJ
6038 /* Keep the old ``set remote Z-packet ...'' working. Each individual
6039 Z sub-packet has its own set and show commands, but users may
6040 have sets to this variable in their .gdbinit files (or in their
6041 documentation). */
e9e68a56 6042 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure,
7915a72c
AC
6043 &remote_Z_packet_detect, _("\
6044Set use of remote protocol `Z' packets"), _("\
6045Show use of remote protocol `Z' packets "), _("\
3b64bf98 6046When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
7915a72c 6047packets."),
e9e68a56 6048 set_remote_protocol_Z_packet_cmd,
2c5b56ce 6049 show_remote_protocol_Z_packet_cmd, /* FIXME: i18n: Use of remote protocol `Z' packets is %s. */
e9e68a56 6050 &remote_set_cmdlist, &remote_show_cmdlist);
449092f6
CV
6051
6052 /* Eventually initialize fileio. See fileio.c */
6053 initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist);
c906108c 6054}
This page took 1.06471 seconds and 4 git commands to generate.