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