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