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