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