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