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