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