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