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