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