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