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