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