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