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