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