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