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