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