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