]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/remote.c
Fix no_shared_libraries() declaration.
[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
30559e10 189static int fromhex (int a);
c906108c 190
30559e10 191static int hex2bin (const char *hex, char *bin, int);
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 {
30559e10
MS
1738 n = min (strlen (bufp) / 2, sizeof (display_buf));
1739 result = hex2bin (bufp, display_buf, n);
1740 display_buf [result] = '\0';
9d1f7ab2
MS
1741 return display_buf;
1742 }
0f71a2f6 1743 }
9d1f7ab2
MS
1744
1745 /* If the above query fails, fall back to the old method. */
1746 use_threadextra_query = 0;
1747 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
1748 | TAG_MOREDISPLAY | TAG_DISPLAY;
39f77062 1749 int_to_threadref (&id, PIDGET (tp->ptid));
9d1f7ab2
MS
1750 if (remote_get_threadinfo (&id, set, &threadinfo))
1751 if (threadinfo.active)
0f71a2f6 1752 {
9d1f7ab2
MS
1753 if (*threadinfo.shortname)
1754 n += sprintf(&display_buf[0], " Name: %s,", threadinfo.shortname);
1755 if (*threadinfo.display)
1756 n += sprintf(&display_buf[n], " State: %s,", threadinfo.display);
1757 if (*threadinfo.more_display)
1758 n += sprintf(&display_buf[n], " Priority: %s",
1759 threadinfo.more_display);
1760
1761 if (n > 0)
c5aa993b 1762 {
9d1f7ab2
MS
1763 /* for purely cosmetic reasons, clear up trailing commas */
1764 if (',' == display_buf[n-1])
1765 display_buf[n-1] = ' ';
1766 return display_buf;
c5aa993b 1767 }
0f71a2f6 1768 }
9d1f7ab2 1769 return NULL;
0f71a2f6 1770}
9d1f7ab2 1771
c906108c 1772\f
c5aa993b 1773
c906108c
SS
1774/* Restart the remote side; this is an extended protocol operation. */
1775
1776static void
fba45db2 1777extended_remote_restart (void)
c906108c 1778{
085dd6e6 1779 char *buf = alloca (PBUFSIZ);
c906108c
SS
1780
1781 /* Send the restart command; for reasons I don't understand the
1782 remote side really expects a number after the "R". */
1783 buf[0] = 'R';
1784 sprintf (&buf[1], "%x", 0);
1785 putpkt (buf);
1786
1787 /* Now query for status so this looks just like we restarted
1788 gdbserver from scratch. */
1789 putpkt ("?");
c2d11a7d 1790 getpkt (buf, PBUFSIZ, 0);
c906108c
SS
1791}
1792\f
1793/* Clean up connection to a remote debugger. */
1794
1795/* ARGSUSED */
1796static void
fba45db2 1797remote_close (int quitting)
c906108c
SS
1798{
1799 if (remote_desc)
1800 SERIAL_CLOSE (remote_desc);
1801 remote_desc = NULL;
1802}
1803
1804/* Query the remote side for the text, data and bss offsets. */
1805
1806static void
fba45db2 1807get_offsets (void)
c906108c 1808{
085dd6e6
JM
1809 char *buf = alloca (PBUFSIZ);
1810 char *ptr;
c906108c
SS
1811 int lose;
1812 CORE_ADDR text_addr, data_addr, bss_addr;
1813 struct section_offsets *offs;
1814
1815 putpkt ("qOffsets");
1816
c2d11a7d 1817 getpkt (buf, PBUFSIZ, 0);
c906108c
SS
1818
1819 if (buf[0] == '\000')
1820 return; /* Return silently. Stub doesn't support
1821 this command. */
1822 if (buf[0] == 'E')
1823 {
1824 warning ("Remote failure reply: %s", buf);
1825 return;
1826 }
1827
1828 /* Pick up each field in turn. This used to be done with scanf, but
1829 scanf will make trouble if CORE_ADDR size doesn't match
1830 conversion directives correctly. The following code will work
1831 with any size of CORE_ADDR. */
1832 text_addr = data_addr = bss_addr = 0;
1833 ptr = buf;
1834 lose = 0;
1835
1836 if (strncmp (ptr, "Text=", 5) == 0)
1837 {
1838 ptr += 5;
1839 /* Don't use strtol, could lose on big values. */
1840 while (*ptr && *ptr != ';')
1841 text_addr = (text_addr << 4) + fromhex (*ptr++);
1842 }
1843 else
1844 lose = 1;
1845
1846 if (!lose && strncmp (ptr, ";Data=", 6) == 0)
1847 {
1848 ptr += 6;
1849 while (*ptr && *ptr != ';')
1850 data_addr = (data_addr << 4) + fromhex (*ptr++);
1851 }
1852 else
1853 lose = 1;
1854
1855 if (!lose && strncmp (ptr, ";Bss=", 5) == 0)
1856 {
1857 ptr += 5;
1858 while (*ptr && *ptr != ';')
1859 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
1860 }
1861 else
1862 lose = 1;
1863
1864 if (lose)
1865 error ("Malformed response to offset query, %s", buf);
1866
1867 if (symfile_objfile == NULL)
1868 return;
1869
d4f3574e
SS
1870 offs = (struct section_offsets *) alloca (SIZEOF_SECTION_OFFSETS);
1871 memcpy (offs, symfile_objfile->section_offsets, SIZEOF_SECTION_OFFSETS);
c906108c 1872
a4c8257b 1873 offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
c906108c
SS
1874
1875 /* This is a temporary kludge to force data and bss to use the same offsets
1876 because that's what nlmconv does now. The real solution requires changes
1877 to the stub and remote.c that I don't have time to do right now. */
1878
a4c8257b
EZ
1879 offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr;
1880 offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr;
c906108c
SS
1881
1882 objfile_relocate (symfile_objfile, offs);
1883}
1884
0f71a2f6
JM
1885/*
1886 * Cisco version of section offsets:
1887 *
1888 * Instead of having GDB query the target for the section offsets,
1889 * Cisco lets the target volunteer the information! It's also in
1890 * a different format, so here are the functions that will decode
1891 * a section offset packet from a Cisco target.
1892 */
1893
1894/*
1895 * Function: remote_cisco_section_offsets
1896 *
1897 * Returns: zero for success, non-zero for failure
1898 */
1899
c5aa993b 1900static int
c2d11a7d
JM
1901remote_cisco_section_offsets (bfd_vma text_addr,
1902 bfd_vma data_addr,
1903 bfd_vma bss_addr,
1904 bfd_signed_vma *text_offs,
1905 bfd_signed_vma *data_offs,
1906 bfd_signed_vma *bss_offs)
0f71a2f6
JM
1907{
1908 bfd_vma text_base, data_base, bss_base;
1909 struct minimal_symbol *start;
1910 asection *sect;
c5aa993b 1911 bfd *abfd;
0f71a2f6 1912 int len;
0f71a2f6
JM
1913
1914 if (symfile_objfile == NULL)
c5aa993b 1915 return -1; /* no can do nothin' */
0f71a2f6
JM
1916
1917 start = lookup_minimal_symbol ("_start", NULL, NULL);
1918 if (start == NULL)
c5aa993b 1919 return -1; /* Can't find "_start" symbol */
0f71a2f6
JM
1920
1921 data_base = bss_base = 0;
1922 text_base = SYMBOL_VALUE_ADDRESS (start);
1923
1924 abfd = symfile_objfile->obfd;
c5aa993b 1925 for (sect = abfd->sections;
0f71a2f6
JM
1926 sect != 0;
1927 sect = sect->next)
1928 {
ce359b09 1929 const char *p = bfd_get_section_name (abfd, sect);
0f71a2f6
JM
1930 len = strlen (p);
1931 if (strcmp (p + len - 4, "data") == 0) /* ends in "data" */
1932 if (data_base == 0 ||
1933 data_base > bfd_get_section_vma (abfd, sect))
1934 data_base = bfd_get_section_vma (abfd, sect);
1935 if (strcmp (p + len - 3, "bss") == 0) /* ends in "bss" */
c5aa993b 1936 if (bss_base == 0 ||
0f71a2f6
JM
1937 bss_base > bfd_get_section_vma (abfd, sect))
1938 bss_base = bfd_get_section_vma (abfd, sect);
1939 }
1940 *text_offs = text_addr - text_base;
1941 *data_offs = data_addr - data_base;
c5aa993b 1942 *bss_offs = bss_addr - bss_base;
0f71a2f6
JM
1943 if (remote_debug)
1944 {
1945 char tmp[128];
1946
1947 sprintf (tmp, "VMA: text = 0x");
1948 sprintf_vma (tmp + strlen (tmp), text_addr);
c5aa993b 1949 sprintf (tmp + strlen (tmp), " data = 0x");
0f71a2f6 1950 sprintf_vma (tmp + strlen (tmp), data_addr);
c5aa993b 1951 sprintf (tmp + strlen (tmp), " bss = 0x");
0f71a2f6
JM
1952 sprintf_vma (tmp + strlen (tmp), bss_addr);
1953 fprintf_filtered (gdb_stdlog, tmp);
1954 fprintf_filtered (gdb_stdlog,
d4f3574e
SS
1955 "Reloc offset: text = 0x%s data = 0x%s bss = 0x%s\n",
1956 paddr_nz (*text_offs),
1957 paddr_nz (*data_offs),
1958 paddr_nz (*bss_offs));
0f71a2f6
JM
1959 }
1960
1961 return 0;
1962}
1963
1964/*
1965 * Function: remote_cisco_objfile_relocate
1966 *
1967 * Relocate the symbol file for a remote target.
1968 */
1969
96baa820 1970void
fba45db2
KB
1971remote_cisco_objfile_relocate (bfd_signed_vma text_off, bfd_signed_vma data_off,
1972 bfd_signed_vma bss_off)
0f71a2f6
JM
1973{
1974 struct section_offsets *offs;
1975
c5aa993b 1976 if (text_off != 0 || data_off != 0 || bss_off != 0)
0f71a2f6
JM
1977 {
1978 /* FIXME: This code assumes gdb-stabs.h is being used; it's
c5aa993b
JM
1979 broken for xcoff, dwarf, sdb-coff, etc. But there is no
1980 simple canonical representation for this stuff. */
0f71a2f6 1981
d4f3574e
SS
1982 offs = (struct section_offsets *) alloca (SIZEOF_SECTION_OFFSETS);
1983 memcpy (offs, symfile_objfile->section_offsets, SIZEOF_SECTION_OFFSETS);
0f71a2f6 1984
a4c8257b
EZ
1985 offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_off;
1986 offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_off;
1987 offs->offsets[SECT_OFF_BSS (symfile_objfile)] = bss_off;
0f71a2f6
JM
1988
1989 /* First call the standard objfile_relocate. */
1990 objfile_relocate (symfile_objfile, offs);
1991
1992 /* Now we need to fix up the section entries already attached to
c5aa993b
JM
1993 the exec target. These entries will control memory transfers
1994 from the exec file. */
0f71a2f6
JM
1995
1996 exec_set_section_offsets (text_off, data_off, bss_off);
1997 }
1998}
1999
c906108c
SS
2000/* Stub for catch_errors. */
2001
0f71a2f6 2002static int
c2d11a7d 2003remote_start_remote_dummy (void *dummy)
0f71a2f6
JM
2004{
2005 start_remote (); /* Initialize gdb process mechanisms */
2006 return 1;
2007}
2008
c906108c 2009static int
fba45db2 2010remote_start_remote (PTR dummy)
c906108c 2011{
8edbea78 2012 immediate_quit++; /* Allow user to interrupt it */
c906108c
SS
2013
2014 /* Ack any packet which the remote side has already sent. */
2015 SERIAL_WRITE (remote_desc, "+", 1);
2016
2017 /* Let the stub know that we want it to return the thread. */
2018 set_thread (-1, 0);
2019
39f77062 2020 inferior_ptid = remote_current_thread (inferior_ptid);
c906108c
SS
2021
2022 get_offsets (); /* Get text, data & bss offsets */
2023
2024 putpkt ("?"); /* initiate a query from remote machine */
8edbea78 2025 immediate_quit--;
c906108c 2026
0f71a2f6 2027 return remote_start_remote_dummy (dummy);
c906108c
SS
2028}
2029
2030/* Open a connection to a remote debugger.
2031 NAME is the filename used for communication. */
2032
2033static void
fba45db2 2034remote_open (char *name, int from_tty)
c906108c
SS
2035{
2036 remote_open_1 (name, from_tty, &remote_ops, 0);
2037}
2038
43ff13b4
JM
2039/* Just like remote_open, but with asynchronous support. */
2040static void
fba45db2 2041remote_async_open (char *name, int from_tty)
43ff13b4
JM
2042{
2043 remote_async_open_1 (name, from_tty, &remote_async_ops, 0);
2044}
2045
c906108c
SS
2046/* Open a connection to a remote debugger using the extended
2047 remote gdb protocol. NAME is the filename used for communication. */
2048
2049static void
fba45db2 2050extended_remote_open (char *name, int from_tty)
c906108c 2051{
c5aa993b 2052 remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */ );
c906108c
SS
2053}
2054
43ff13b4
JM
2055/* Just like extended_remote_open, but with asynchronous support. */
2056static void
fba45db2 2057extended_remote_async_open (char *name, int from_tty)
43ff13b4 2058{
c5aa993b 2059 remote_async_open_1 (name, from_tty, &extended_async_remote_ops, 1 /*extended_p */ );
43ff13b4
JM
2060}
2061
c906108c
SS
2062/* Generic code for opening a connection to a remote target. */
2063
d471ea57
AC
2064static void
2065init_all_packet_configs (void)
2066{
2067 int i;
44eaed12
C
2068 update_packet_config (&remote_protocol_e);
2069 update_packet_config (&remote_protocol_E);
d471ea57
AC
2070 update_packet_config (&remote_protocol_P);
2071 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
2072 update_packet_config (&remote_protocol_Z[i]);
2073 /* Force remote_write_bytes to check whether target supports binary
2074 downloading. */
2075 update_packet_config (&remote_protocol_binary_download);
2076}
2077
c906108c 2078static void
fba45db2
KB
2079remote_open_1 (char *name, int from_tty, struct target_ops *target,
2080 int extended_p)
c906108c
SS
2081{
2082 if (name == 0)
2083 error ("To open a remote debug connection, you need to specify what\n\
0d06e24b
JM
2084serial device is attached to the remote system\n\
2085(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).");
c906108c 2086
6426a772
JM
2087 /* See FIXME above */
2088 wait_forever_enabled_p = 1;
2089
c906108c
SS
2090 target_preopen (from_tty);
2091
2092 unpush_target (target);
2093
c906108c
SS
2094 remote_desc = SERIAL_OPEN (name);
2095 if (!remote_desc)
2096 perror_with_name (name);
2097
2098 if (baud_rate != -1)
2099 {
2100 if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
2101 {
2102 SERIAL_CLOSE (remote_desc);
2103 perror_with_name (name);
2104 }
2105 }
2106
c906108c
SS
2107 SERIAL_RAW (remote_desc);
2108
2109 /* If there is something sitting in the buffer we might take it as a
2110 response to a command, which would be bad. */
2111 SERIAL_FLUSH_INPUT (remote_desc);
2112
2113 if (from_tty)
2114 {
2115 puts_filtered ("Remote debugging using ");
2116 puts_filtered (name);
2117 puts_filtered ("\n");
2118 }
c5aa993b 2119 push_target (target); /* Switch to using remote target now */
c906108c 2120
d471ea57 2121 init_all_packet_configs ();
96baa820 2122
c5aa993b 2123 general_thread = -2;
cce74817 2124 continue_thread = -2;
c906108c 2125
9d1f7ab2
MS
2126 /* Probe for ability to use "ThreadInfo" query, as required. */
2127 use_threadinfo_query = 1;
2128 use_threadextra_query = 1;
2129
c906108c
SS
2130 /* Without this, some commands which require an active target (such
2131 as kill) won't work. This variable serves (at least) double duty
2132 as both the pid of the target process (if it has such), and as a
2133 flag indicating that a target is active. These functions should
2134 be split out into seperate variables, especially since GDB will
2135 someday have a notion of debugging several processes. */
2136
39f77062 2137 inferior_ptid = pid_to_ptid (MAGIC_NULL_PID);
f78f6cf1
MS
2138#ifdef SOLIB_CREATE_INFERIOR_HOOK
2139 /* First delete any symbols previously loaded from shared libraries. */
2140 no_shared_libraries (NULL, 0);
2141#endif
2142
c906108c
SS
2143 /* Start the remote connection; if error (0), discard this target.
2144 In particular, if the user quits, be sure to discard it
2145 (we'd be in an inconsistent state otherwise). */
c5aa993b
JM
2146 if (!catch_errors (remote_start_remote, NULL,
2147 "Couldn't establish connection to remote target\n",
c906108c
SS
2148 RETURN_MASK_ALL))
2149 {
2150 pop_target ();
2151 return;
2152 }
2153
2154 if (extended_p)
2155 {
6240bebf 2156 /* Tell the remote that we are using the extended protocol. */
085dd6e6 2157 char *buf = alloca (PBUFSIZ);
c906108c 2158 putpkt ("!");
c2d11a7d 2159 getpkt (buf, PBUFSIZ, 0);
c906108c 2160 }
f78f6cf1 2161#ifdef SOLIB_CREATE_INFERIOR_HOOK
6240bebf
MS
2162 /* FIXME: need a master target_open vector from which all
2163 remote_opens can be called, so that stuff like this can
2164 go there. Failing that, the following code must be copied
2165 to the open function for any remote target that wants to
2166 support svr4 shared libraries. */
f78f6cf1
MS
2167
2168 /* Set up to detect and load shared libraries. */
6240bebf 2169 if (exec_bfd) /* No use without an exec file. */
39f77062 2170 SOLIB_CREATE_INFERIOR_HOOK (PIDGET (inferior_ptid));
6240bebf 2171#endif
c906108c
SS
2172}
2173
43ff13b4
JM
2174/* Just like remote_open but with asynchronous support. */
2175static void
fba45db2
KB
2176remote_async_open_1 (char *name, int from_tty, struct target_ops *target,
2177 int extended_p)
43ff13b4
JM
2178{
2179 if (name == 0)
2180 error ("To open a remote debug connection, you need to specify what\n\
0d06e24b
JM
2181serial device is attached to the remote system\n\
2182(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).");
43ff13b4
JM
2183
2184 target_preopen (from_tty);
2185
2186 unpush_target (target);
2187
43ff13b4
JM
2188 remote_desc = SERIAL_OPEN (name);
2189 if (!remote_desc)
2190 perror_with_name (name);
2191
2192 if (baud_rate != -1)
2193 {
2194 if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
2195 {
2196 SERIAL_CLOSE (remote_desc);
2197 perror_with_name (name);
2198 }
2199 }
2200
2201 SERIAL_RAW (remote_desc);
2202
2203 /* If there is something sitting in the buffer we might take it as a
2204 response to a command, which would be bad. */
2205 SERIAL_FLUSH_INPUT (remote_desc);
2206
2207 if (from_tty)
2208 {
2209 puts_filtered ("Remote debugging using ");
2210 puts_filtered (name);
2211 puts_filtered ("\n");
2212 }
2213
c5aa993b 2214 push_target (target); /* Switch to using remote target now */
43ff13b4 2215
d471ea57 2216 init_all_packet_configs ();
43ff13b4 2217
c5aa993b 2218 general_thread = -2;
43ff13b4
JM
2219 continue_thread = -2;
2220
9d1f7ab2
MS
2221 /* Probe for ability to use "ThreadInfo" query, as required. */
2222 use_threadinfo_query = 1;
2223 use_threadextra_query = 1;
2224
43ff13b4
JM
2225 /* Without this, some commands which require an active target (such
2226 as kill) won't work. This variable serves (at least) double duty
2227 as both the pid of the target process (if it has such), and as a
2228 flag indicating that a target is active. These functions should
2229 be split out into seperate variables, especially since GDB will
2230 someday have a notion of debugging several processes. */
39f77062 2231 inferior_ptid = pid_to_ptid (MAGIC_NULL_PID);
6426a772
JM
2232
2233 /* With this target we start out by owning the terminal. */
2234 remote_async_terminal_ours_p = 1;
2235
2236 /* FIXME: cagney/1999-09-23: During the initial connection it is
2237 assumed that the target is already ready and able to respond to
e26cc349 2238 requests. Unfortunately remote_start_remote() eventually calls
6426a772
JM
2239 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
2240 around this. Eventually a mechanism that allows
2241 wait_for_inferior() to expect/get timeouts will be
2242 implemented. */
2243 wait_forever_enabled_p = 0;
2244
f78f6cf1
MS
2245#ifdef SOLIB_CREATE_INFERIOR_HOOK
2246 /* First delete any symbols previously loaded from shared libraries. */
2247 no_shared_libraries (NULL, 0);
2248#endif
2249
43ff13b4
JM
2250 /* Start the remote connection; if error (0), discard this target.
2251 In particular, if the user quits, be sure to discard it
2252 (we'd be in an inconsistent state otherwise). */
c5aa993b
JM
2253 if (!catch_errors (remote_start_remote, NULL,
2254 "Couldn't establish connection to remote target\n",
43ff13b4
JM
2255 RETURN_MASK_ALL))
2256 {
43ff13b4 2257 pop_target ();
6426a772 2258 wait_forever_enabled_p = 1;
43ff13b4
JM
2259 return;
2260 }
2261
6426a772
JM
2262 wait_forever_enabled_p = 1;
2263
2264 if (extended_p)
43ff13b4 2265 {
6240bebf 2266 /* Tell the remote that we are using the extended protocol. */
6426a772
JM
2267 char *buf = alloca (PBUFSIZ);
2268 putpkt ("!");
c2d11a7d 2269 getpkt (buf, PBUFSIZ, 0);
43ff13b4 2270 }
f78f6cf1 2271#ifdef SOLIB_CREATE_INFERIOR_HOOK
6240bebf
MS
2272 /* FIXME: need a master target_open vector from which all
2273 remote_opens can be called, so that stuff like this can
2274 go there. Failing that, the following code must be copied
2275 to the open function for any remote target that wants to
2276 support svr4 shared libraries. */
f78f6cf1
MS
2277
2278 /* Set up to detect and load shared libraries. */
6240bebf 2279 if (exec_bfd) /* No use without an exec file. */
39f77062 2280 SOLIB_CREATE_INFERIOR_HOOK (PIDGET (inferior_ptid));
6240bebf 2281#endif
43ff13b4
JM
2282}
2283
c906108c
SS
2284/* This takes a program previously attached to and detaches it. After
2285 this is done, GDB can be used to debug some other program. We
2286 better not have left any breakpoints in the target program or it'll
2287 die when it hits one. */
2288
2289static void
fba45db2 2290remote_detach (char *args, int from_tty)
c906108c 2291{
085dd6e6 2292 char *buf = alloca (PBUFSIZ);
c906108c
SS
2293
2294 if (args)
2295 error ("Argument given to \"detach\" when remotely debugging.");
2296
2297 /* Tell the remote target to detach. */
2298 strcpy (buf, "D");
c2d11a7d 2299 remote_send (buf, PBUFSIZ);
c906108c 2300
cca728d0 2301 target_mourn_inferior ();
c906108c
SS
2302 if (from_tty)
2303 puts_filtered ("Ending remote debugging.\n");
96baa820 2304
c906108c
SS
2305}
2306
43ff13b4
JM
2307/* Same as remote_detach, but with async support. */
2308static void
fba45db2 2309remote_async_detach (char *args, int from_tty)
43ff13b4
JM
2310{
2311 char *buf = alloca (PBUFSIZ);
2312
2313 if (args)
2314 error ("Argument given to \"detach\" when remotely debugging.");
2315
2316 /* Tell the remote target to detach. */
2317 strcpy (buf, "D");
c2d11a7d 2318 remote_send (buf, PBUFSIZ);
43ff13b4
JM
2319
2320 /* Unregister the file descriptor from the event loop. */
ed9a39eb 2321 if (target_is_async_p ())
c2c6d25f 2322 SERIAL_ASYNC (remote_desc, NULL, 0);
43ff13b4 2323
cca728d0 2324 target_mourn_inferior ();
43ff13b4
JM
2325 if (from_tty)
2326 puts_filtered ("Ending remote debugging.\n");
2327}
2328
c906108c
SS
2329/* Convert hex digit A to a number. */
2330
30559e10 2331static int
fba45db2 2332fromhex (int a)
c906108c
SS
2333{
2334 if (a >= '0' && a <= '9')
2335 return a - '0';
2336 else if (a >= 'a' && a <= 'f')
2337 return a - 'a' + 10;
2338 else if (a >= 'A' && a <= 'F')
2339 return a - 'A' + 10;
c5aa993b 2340 else
c906108c
SS
2341 error ("Reply contains invalid hex digit %d", a);
2342}
2343
30559e10
MS
2344static int
2345hex2bin (const char *hex, char *bin, int count)
2346{
2347 int i;
2348
2349 /* May use a length, or a nul-terminated string as input. */
2350 if (count == 0)
2351 count = strlen (hex) / 2;
2352
2353 for (i = 0; i < count; i++)
2354 {
2355 if (hex[0] == 0 || hex[1] == 0)
2356 {
2357 /* Hex string is short, or of uneven length.
2358 Return the count that has been converted so far. */
2359 return i;
2360 }
2361 *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
2362 hex += 2;
2363 }
2364 return i;
2365}
2366
c906108c
SS
2367/* Convert number NIB to a hex digit. */
2368
2369static int
fba45db2 2370tohex (int nib)
c906108c
SS
2371{
2372 if (nib < 10)
c5aa993b 2373 return '0' + nib;
c906108c 2374 else
c5aa993b 2375 return 'a' + nib - 10;
c906108c 2376}
30559e10
MS
2377
2378static int
2379bin2hex (char *bin, char *hex, int count)
2380{
2381 int i;
2382 /* May use a length, or a nul-terminated string as input. */
2383 if (count == 0)
2384 count = strlen (bin);
2385
2386 for (i = 0; i < count; i++)
2387 {
2388 *hex++ = tohex ((*bin >> 4) & 0xf);
2389 *hex++ = tohex (*bin++ & 0xf);
2390 }
2391 *hex = 0;
2392 return i;
2393}
c906108c
SS
2394\f
2395/* Tell the remote machine to resume. */
2396
2397static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
2398
2399static int last_sent_step;
2400
2401static void
39f77062 2402remote_resume (ptid_t ptid, int step, enum target_signal siggnal)
c906108c 2403{
085dd6e6 2404 char *buf = alloca (PBUFSIZ);
39f77062 2405 int pid = PIDGET (ptid);
44eaed12 2406 char *p;
c906108c
SS
2407
2408 if (pid == -1)
2409 set_thread (0, 0); /* run any thread */
2410 else
2411 set_thread (pid, 0); /* run this thread */
2412
c906108c
SS
2413 last_sent_signal = siggnal;
2414 last_sent_step = step;
2415
2416 /* A hook for when we need to do something at the last moment before
2417 resumption. */
2418 if (target_resume_hook)
2419 (*target_resume_hook) ();
2420
44eaed12
C
2421
2422 /* The s/S/c/C packets do not return status. So if the target does
2423 not support the S or C packets, the debug agent returns an empty
2424 string which is detected in remote_wait(). This protocol defect
2425 is fixed in the e/E packets. */
2426
2427 if (step && step_range_end)
2428 {
2429 /* If the target does not support the 'E' packet, we try the 'S'
2430 packet. Ideally we would fall back to the 'e' packet if that
2431 too is not supported. But that would require another copy of
2432 the code to issue the 'e' packet (and fall back to 's' if not
2433 supported) in remote_wait(). */
2434
2435 if (siggnal != TARGET_SIGNAL_0)
2436 {
2437 if (remote_protocol_E.support != PACKET_DISABLE)
2438 {
2439 p = buf;
2440 *p++ = 'E';
2441 *p++ = tohex (((int) siggnal >> 4) & 0xf);
2442 *p++ = tohex (((int) siggnal) & 0xf);
2443 *p++ = ',';
2444 p += hexnumstr (p, (ULONGEST) step_range_start);
2445 *p++ = ',';
2446 p += hexnumstr (p, (ULONGEST) step_range_end);
2447 *p++ = 0;
2448
2449 putpkt (buf);
2450 getpkt (buf, PBUFSIZ, 0);
2451
2452 if (packet_ok(buf, &remote_protocol_E) == PACKET_OK)
2453 return;
2454 }
2455 }
2456 else
2457 {
2458 if (remote_protocol_e.support != PACKET_DISABLE)
2459 {
2460 p = buf;
2461 *p++ = 'e';
2462 p += hexnumstr (p, (ULONGEST) step_range_start);
2463 *p++ = ',';
2464 p += hexnumstr (p, (ULONGEST) step_range_end);
2465 *p++ = 0;
2466
2467 putpkt (buf);
2468 getpkt (buf, PBUFSIZ, 0);
2469
2470 if (packet_ok(buf, &remote_protocol_e) == PACKET_OK)
2471 return;
2472 }
2473 }
2474 }
2475
c906108c
SS
2476 if (siggnal != TARGET_SIGNAL_0)
2477 {
2478 buf[0] = step ? 'S' : 'C';
c5aa993b 2479 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
44eaed12 2480 buf[2] = tohex (((int) siggnal) & 0xf);
c906108c
SS
2481 buf[3] = '\0';
2482 }
2483 else
c5aa993b 2484 strcpy (buf, step ? "s" : "c");
c906108c
SS
2485
2486 putpkt (buf);
2487}
43ff13b4
JM
2488
2489/* Same as remote_resume, but with async support. */
2490static void
39f77062 2491remote_async_resume (ptid_t ptid, int step, enum target_signal siggnal)
43ff13b4
JM
2492{
2493 char *buf = alloca (PBUFSIZ);
39f77062 2494 int pid = PIDGET (ptid);
44eaed12 2495 char *p;
43ff13b4
JM
2496
2497 if (pid == -1)
2498 set_thread (0, 0); /* run any thread */
2499 else
2500 set_thread (pid, 0); /* run this thread */
2501
43ff13b4
JM
2502 last_sent_signal = siggnal;
2503 last_sent_step = step;
2504
2505 /* A hook for when we need to do something at the last moment before
2506 resumption. */
2507 if (target_resume_hook)
2508 (*target_resume_hook) ();
2509
44eaed12
C
2510 /* The s/S/c/C packets do not return status. So if the target does
2511 not support the S or C packets, the debug agent returns an empty
2512 string which is detected in remote_wait(). This protocol defect
2513 is fixed in the e/E packets. */
2514
2515 if (step && step_range_end)
2516 {
2517 /* If the target does not support the 'E' packet, we try the 'S'
2518 packet. Ideally we would fall back to the 'e' packet if that
2519 too is not supported. But that would require another copy of
2520 the code to issue the 'e' packet (and fall back to 's' if not
2521 supported) in remote_wait(). */
2522
2523 if (siggnal != TARGET_SIGNAL_0)
2524 {
2525 if (remote_protocol_E.support != PACKET_DISABLE)
2526 {
2527 p = buf;
2528 *p++ = 'E';
2529 *p++ = tohex (((int) siggnal >> 4) & 0xf);
2530 *p++ = tohex (((int) siggnal) & 0xf);
2531 *p++ = ',';
2532 p += hexnumstr (p, (ULONGEST) step_range_start);
2533 *p++ = ',';
2534 p += hexnumstr (p, (ULONGEST) step_range_end);
2535 *p++ = 0;
2536
2537 putpkt (buf);
2538 getpkt (buf, PBUFSIZ, 0);
2539
2540 if (packet_ok(buf, &remote_protocol_E) == PACKET_OK)
2541 goto register_event_loop;
2542 }
2543 }
2544 else
2545 {
2546 if (remote_protocol_e.support != PACKET_DISABLE)
2547 {
2548 p = buf;
2549 *p++ = 'e';
2550 p += hexnumstr (p, (ULONGEST) step_range_start);
2551 *p++ = ',';
2552 p += hexnumstr (p, (ULONGEST) step_range_end);
2553 *p++ = 0;
2554
2555 putpkt (buf);
2556 getpkt (buf, PBUFSIZ, 0);
2557
2558 if (packet_ok(buf, &remote_protocol_e) == PACKET_OK)
2559 goto register_event_loop;
2560 }
2561 }
2562 }
2563
43ff13b4
JM
2564 if (siggnal != TARGET_SIGNAL_0)
2565 {
2566 buf[0] = step ? 'S' : 'C';
c5aa993b
JM
2567 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
2568 buf[2] = tohex ((int) siggnal & 0xf);
43ff13b4
JM
2569 buf[3] = '\0';
2570 }
2571 else
c5aa993b 2572 strcpy (buf, step ? "s" : "c");
44eaed12
C
2573
2574 putpkt (buf);
43ff13b4 2575
44eaed12 2576register_event_loop:
2acceee2
JM
2577 /* We are about to start executing the inferior, let's register it
2578 with the event loop. NOTE: this is the one place where all the
2579 execution commands end up. We could alternatively do this in each
2580 of the execution commands in infcmd.c.*/
2581 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
2582 into infcmd.c in order to allow inferior function calls to work
2583 NOT asynchronously. */
ed9a39eb 2584 if (event_loop_p && target_can_async_p ())
2acceee2
JM
2585 target_async (inferior_event_handler, 0);
2586 /* Tell the world that the target is now executing. */
2587 /* FIXME: cagney/1999-09-23: Is it the targets responsibility to set
2588 this? Instead, should the client of target just assume (for
2589 async targets) that the target is going to start executing? Is
2590 this information already found in the continuation block? */
ed9a39eb 2591 if (target_is_async_p ())
2acceee2 2592 target_executing = 1;
43ff13b4 2593}
c906108c 2594\f
43ff13b4
JM
2595
2596/* Set up the signal handler for SIGINT, while the target is
2597 executing, ovewriting the 'regular' SIGINT signal handler. */
2598static void
fba45db2 2599initialize_sigint_signal_handler (void)
43ff13b4 2600{
c5aa993b 2601 sigint_remote_token =
43ff13b4
JM
2602 create_async_signal_handler (async_remote_interrupt, NULL);
2603 signal (SIGINT, handle_remote_sigint);
2604}
2605
2606/* Signal handler for SIGINT, while the target is executing. */
2607static void
fba45db2 2608handle_remote_sigint (int sig)
43ff13b4
JM
2609{
2610 signal (sig, handle_remote_sigint_twice);
c5aa993b 2611 sigint_remote_twice_token =
43ff13b4
JM
2612 create_async_signal_handler (async_remote_interrupt_twice, NULL);
2613 mark_async_signal_handler_wrapper (sigint_remote_token);
2614}
2615
2616/* Signal handler for SIGINT, installed after SIGINT has already been
2617 sent once. It will take effect the second time that the user sends
2618 a ^C. */
2619static void
fba45db2 2620handle_remote_sigint_twice (int sig)
43ff13b4
JM
2621{
2622 signal (sig, handle_sigint);
c5aa993b 2623 sigint_remote_twice_token =
2df3850c 2624 create_async_signal_handler (inferior_event_handler_wrapper, NULL);
43ff13b4
JM
2625 mark_async_signal_handler_wrapper (sigint_remote_twice_token);
2626}
2627
6426a772 2628/* Perform the real interruption of the target execution, in response
43ff13b4 2629 to a ^C. */
c5aa993b 2630static void
fba45db2 2631async_remote_interrupt (gdb_client_data arg)
43ff13b4
JM
2632{
2633 if (remote_debug)
2634 fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
2635
2636 target_stop ();
2637}
2638
2639/* Perform interrupt, if the first attempt did not succeed. Just give
2640 up on the target alltogether. */
2df3850c 2641void
fba45db2 2642async_remote_interrupt_twice (gdb_client_data arg)
43ff13b4 2643{
2df3850c
JM
2644 if (remote_debug)
2645 fprintf_unfiltered (gdb_stdlog, "remote_interrupt_twice called\n");
6426a772
JM
2646 /* Do something only if the target was not killed by the previous
2647 cntl-C. */
2648 if (target_executing)
2649 {
2650 interrupt_query ();
2651 signal (SIGINT, handle_remote_sigint);
2652 }
43ff13b4
JM
2653}
2654
2655/* Reinstall the usual SIGINT handlers, after the target has
2656 stopped. */
6426a772
JM
2657static void
2658cleanup_sigint_signal_handler (void *dummy)
43ff13b4
JM
2659{
2660 signal (SIGINT, handle_sigint);
2661 if (sigint_remote_twice_token)
c2c6d25f 2662 delete_async_signal_handler ((struct async_signal_handler **) & sigint_remote_twice_token);
43ff13b4 2663 if (sigint_remote_token)
c2c6d25f 2664 delete_async_signal_handler ((struct async_signal_handler **) & sigint_remote_token);
43ff13b4
JM
2665}
2666
c906108c
SS
2667/* Send ^C to target to halt it. Target will respond, and send us a
2668 packet. */
507f3c78 2669static void (*ofunc) (int);
c906108c 2670
7a292a7a
SS
2671/* The command line interface's stop routine. This function is installed
2672 as a signal handler for SIGINT. The first time a user requests a
2673 stop, we call remote_stop to send a break or ^C. If there is no
2674 response from the target (it didn't stop when the user requested it),
2675 we ask the user if he'd like to detach from the target. */
c906108c 2676static void
fba45db2 2677remote_interrupt (int signo)
c906108c 2678{
7a292a7a
SS
2679 /* If this doesn't work, try more severe steps. */
2680 signal (signo, remote_interrupt_twice);
2681
2682 if (remote_debug)
0f71a2f6 2683 fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
7a292a7a
SS
2684
2685 target_stop ();
2686}
2687
2688/* The user typed ^C twice. */
2689
2690static void
fba45db2 2691remote_interrupt_twice (int signo)
7a292a7a
SS
2692{
2693 signal (signo, ofunc);
2694 interrupt_query ();
c906108c
SS
2695 signal (signo, remote_interrupt);
2696}
7a292a7a
SS
2697
2698/* This is the generic stop called via the target vector. When a target
2699 interrupt is requested, either by the command line or the GUI, we
2700 will eventually end up here. */
c906108c 2701static void
fba45db2 2702remote_stop (void)
c906108c 2703{
7a292a7a
SS
2704 /* Send a break or a ^C, depending on user preference. */
2705 if (remote_debug)
0f71a2f6 2706 fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
c906108c 2707
7a292a7a
SS
2708 if (remote_break)
2709 SERIAL_SEND_BREAK (remote_desc);
c906108c 2710 else
7a292a7a 2711 SERIAL_WRITE (remote_desc, "\003", 1);
c906108c
SS
2712}
2713
2714/* Ask the user what to do when an interrupt is received. */
2715
2716static void
fba45db2 2717interrupt_query (void)
c906108c
SS
2718{
2719 target_terminal_ours ();
2720
2721 if (query ("Interrupted while waiting for the program.\n\
2722Give up (and stop debugging it)? "))
2723 {
2724 target_mourn_inferior ();
2725 return_to_top_level (RETURN_QUIT);
2726 }
2727
2728 target_terminal_inferior ();
2729}
2730
6426a772
JM
2731/* Enable/disable target terminal ownership. Most targets can use
2732 terminal groups to control terminal ownership. Remote targets are
2733 different in that explicit transfer of ownership to/from GDB/target
2734 is required. */
2735
2736static void
2737remote_async_terminal_inferior (void)
2738{
2739 /* FIXME: cagney/1999-09-27: Shouldn't need to test for
2740 sync_execution here. This function should only be called when
2741 GDB is resuming the inferior in the forground. A background
2742 resume (``run&'') should leave GDB in control of the terminal and
2743 consequently should not call this code. */
2744 if (!sync_execution)
2745 return;
2746 /* FIXME: cagney/1999-09-27: Closely related to the above. Make
2747 calls target_terminal_*() idenpotent. The event-loop GDB talking
2748 to an asynchronous target with a synchronous command calls this
2749 function from both event-top.c and infrun.c/infcmd.c. Once GDB
2750 stops trying to transfer the terminal to the target when it
2751 shouldn't this guard can go away. */
2752 if (!remote_async_terminal_ours_p)
2753 return;
2754 delete_file_handler (input_fd);
2755 remote_async_terminal_ours_p = 0;
2756 initialize_sigint_signal_handler ();
2757 /* NOTE: At this point we could also register our selves as the
2758 recipient of all input. Any characters typed could then be
2759 passed on down to the target. */
2760}
2761
2762static void
2763remote_async_terminal_ours (void)
2764{
2765 /* See FIXME in remote_async_terminal_inferior. */
2766 if (!sync_execution)
2767 return;
2768 /* See FIXME in remote_async_terminal_inferior. */
2769 if (remote_async_terminal_ours_p)
2770 return;
2771 cleanup_sigint_signal_handler (NULL);
2772 add_file_handler (input_fd, stdin_event_handler, 0);
2773 remote_async_terminal_ours_p = 1;
2774}
2775
c906108c
SS
2776/* If nonzero, ignore the next kill. */
2777
2778int kill_kludge;
2779
2780void
917317f4 2781remote_console_output (char *msg)
c906108c
SS
2782{
2783 char *p;
2784
c5aa993b 2785 for (p = msg; p[0] && p[1]; p += 2)
c906108c
SS
2786 {
2787 char tb[2];
2788 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
2789 tb[0] = c;
2790 tb[1] = 0;
43ff13b4 2791 fputs_unfiltered (tb, gdb_stdtarg);
c906108c 2792 }
917317f4 2793 gdb_flush (gdb_stdtarg);
c906108c
SS
2794}
2795
0f71a2f6
JM
2796/* Wait until the remote machine stops, then return,
2797 storing status in STATUS just as `wait' would.
2798 Returns "pid", which in the case of a multi-threaded
2799 remote OS, is the thread-id. */
c906108c 2800
39f77062
KB
2801static ptid_t
2802remote_wait (ptid_t ptid, struct target_waitstatus *status)
c906108c 2803{
085dd6e6 2804 unsigned char *buf = alloca (PBUFSIZ);
c906108c
SS
2805 int thread_num = -1;
2806
2807 status->kind = TARGET_WAITKIND_EXITED;
2808 status->value.integer = 0;
2809
2810 while (1)
2811 {
2812 unsigned char *p;
2813
c906108c 2814 ofunc = signal (SIGINT, remote_interrupt);
c2d11a7d 2815 getpkt (buf, PBUFSIZ, 1);
c906108c
SS
2816 signal (SIGINT, ofunc);
2817
2818 /* This is a hook for when we need to do something (perhaps the
c5aa993b 2819 collection of trace data) every time the target stops. */
c906108c
SS
2820 if (target_wait_loop_hook)
2821 (*target_wait_loop_hook) ();
2822
2823 switch (buf[0])
2824 {
2825 case 'E': /* Error of some sort */
2826 warning ("Remote failure reply: %s", buf);
2827 continue;
2828 case 'T': /* Status with PC, SP, FP, ... */
2829 {
2830 int i;
2831 long regno;
e6cbd02a 2832 char* regs = (char*) alloca (MAX_REGISTER_RAW_SIZE);
c906108c
SS
2833
2834 /* Expedited reply, containing Signal, {regno, reg} repeat */
2835 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
c5aa993b
JM
2836 ss = signal number
2837 n... = register number
2838 r... = register contents
2839 */
c906108c
SS
2840 p = &buf[3]; /* after Txx */
2841
2842 while (*p)
2843 {
2844 unsigned char *p1;
2845 char *p_temp;
2846
2847 /* Read the register number */
2848 regno = strtol ((const char *) p, &p_temp, 16);
c5aa993b 2849 p1 = (unsigned char *) p_temp;
c906108c 2850
c5aa993b 2851 if (p1 == p) /* No register number present here */
c906108c
SS
2852 {
2853 p1 = (unsigned char *) strchr ((const char *) p, ':');
2854 if (p1 == NULL)
2855 warning ("Malformed packet(a) (missing colon): %s\n\
2856Packet: '%s'\n",
2857 p, buf);
2858 if (strncmp ((const char *) p, "thread", p1 - p) == 0)
2859 {
2860 p_temp = unpack_varlen_hex (++p1, &thread_num);
2861 record_currthread (thread_num);
2862 p = (unsigned char *) p_temp;
2863 }
2864 }
2865 else
2866 {
2867 p = p1;
2868
2869 if (*p++ != ':')
2870 warning ("Malformed packet(b) (missing colon): %s\n\
2871Packet: '%s'\n",
2872 p, buf);
2873
2874 if (regno >= NUM_REGS)
2875 warning ("Remote sent bad register number %ld: %s\n\
2876Packet: '%s'\n",
2877 regno, p, buf);
2878
30559e10
MS
2879 if (hex2bin (p, regs, REGISTER_RAW_SIZE (regno))
2880 < REGISTER_RAW_SIZE (regno))
2881 warning ("Remote reply is too short: %s", buf);
c906108c
SS
2882 supply_register (regno, regs);
2883 }
2884
2885 if (*p++ != ';')
2886 {
2887 warning ("Remote register badly formatted: %s", buf);
c5aa993b 2888 warning (" here: %s", p);
c906108c
SS
2889 }
2890 }
2891 }
2892 /* fall through */
2893 case 'S': /* Old style status, just signal only */
2894 status->kind = TARGET_WAITKIND_STOPPED;
2895 status->value.sig = (enum target_signal)
2896 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
2897
0f71a2f6
JM
2898 if (buf[3] == 'p')
2899 {
2900 /* Export Cisco kernel mode as a convenience variable
c5aa993b 2901 (so that it can be used in the GDB prompt if desired). */
0f71a2f6
JM
2902
2903 if (cisco_kernel_mode == 1)
c5aa993b 2904 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
0f71a2f6
JM
2905 value_from_string ("PDEBUG-"));
2906 cisco_kernel_mode = 0;
2907 thread_num = strtol ((const char *) &buf[4], NULL, 16);
2908 record_currthread (thread_num);
2909 }
2910 else if (buf[3] == 'k')
2911 {
2912 /* Export Cisco kernel mode as a convenience variable
c5aa993b 2913 (so that it can be used in the GDB prompt if desired). */
0f71a2f6
JM
2914
2915 if (cisco_kernel_mode == 1)
c5aa993b 2916 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
0f71a2f6
JM
2917 value_from_string ("KDEBUG-"));
2918 cisco_kernel_mode = 1;
2919 }
c906108c 2920 goto got_status;
0f71a2f6
JM
2921 case 'N': /* Cisco special: status and offsets */
2922 {
2923 bfd_vma text_addr, data_addr, bss_addr;
2924 bfd_signed_vma text_off, data_off, bss_off;
2925 unsigned char *p1;
2926
2927 status->kind = TARGET_WAITKIND_STOPPED;
2928 status->value.sig = (enum target_signal)
2929 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
2930
c5aa993b 2931 if (symfile_objfile == NULL)
0f71a2f6 2932 {
d4f3574e 2933 warning ("Relocation packet received with no symbol file. \
0f71a2f6
JM
2934Packet Dropped");
2935 goto got_status;
2936 }
2937
2938 /* Relocate object file. Buffer format is NAATT;DD;BB
2939 * where AA is the signal number, TT is the new text
2940 * address, DD * is the new data address, and BB is the
2941 * new bss address. */
2942
2943 p = &buf[3];
2944 text_addr = strtoul (p, (char **) &p1, 16);
2945 if (p1 == p || *p1 != ';')
2946 warning ("Malformed relocation packet: Packet '%s'", buf);
2947 p = p1 + 1;
2948 data_addr = strtoul (p, (char **) &p1, 16);
2949 if (p1 == p || *p1 != ';')
2950 warning ("Malformed relocation packet: Packet '%s'", buf);
2951 p = p1 + 1;
2952 bss_addr = strtoul (p, (char **) &p1, 16);
c5aa993b 2953 if (p1 == p)
0f71a2f6
JM
2954 warning ("Malformed relocation packet: Packet '%s'", buf);
2955
2956 if (remote_cisco_section_offsets (text_addr, data_addr, bss_addr,
2957 &text_off, &data_off, &bss_off)
2958 == 0)
c5aa993b 2959 if (text_off != 0 || data_off != 0 || bss_off != 0)
0f71a2f6
JM
2960 remote_cisco_objfile_relocate (text_off, data_off, bss_off);
2961
2962 goto got_status;
2963 }
c906108c
SS
2964 case 'W': /* Target exited */
2965 {
2966 /* The remote process exited. */
2967 status->kind = TARGET_WAITKIND_EXITED;
2968 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
2969 goto got_status;
2970 }
2971 case 'X':
2972 status->kind = TARGET_WAITKIND_SIGNALLED;
2973 status->value.sig = (enum target_signal)
2974 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
2975 kill_kludge = 1;
2976
2977 goto got_status;
2978 case 'O': /* Console output */
2979 remote_console_output (buf + 1);
2980 continue;
2981 case '\0':
2982 if (last_sent_signal != TARGET_SIGNAL_0)
2983 {
2984 /* Zero length reply means that we tried 'S' or 'C' and
c5aa993b 2985 the remote system doesn't support it. */
c906108c
SS
2986 target_terminal_ours_for_output ();
2987 printf_filtered
2988 ("Can't send signals to this remote system. %s not sent.\n",
2989 target_signal_to_name (last_sent_signal));
2990 last_sent_signal = TARGET_SIGNAL_0;
2991 target_terminal_inferior ();
2992
2993 strcpy ((char *) buf, last_sent_step ? "s" : "c");
2994 putpkt ((char *) buf);
2995 continue;
2996 }
2997 /* else fallthrough */
2998 default:
2999 warning ("Invalid remote reply: %s", buf);
3000 continue;
3001 }
3002 }
c5aa993b 3003got_status:
c906108c
SS
3004 if (thread_num != -1)
3005 {
39f77062 3006 return pid_to_ptid (thread_num);
c906108c 3007 }
39f77062 3008 return inferior_ptid;
c906108c
SS
3009}
3010
43ff13b4 3011/* Async version of remote_wait. */
39f77062
KB
3012static ptid_t
3013remote_async_wait (ptid_t ptid, struct target_waitstatus *status)
43ff13b4
JM
3014{
3015 unsigned char *buf = alloca (PBUFSIZ);
3016 int thread_num = -1;
3017
3018 status->kind = TARGET_WAITKIND_EXITED;
3019 status->value.integer = 0;
3020
3021 while (1)
3022 {
3023 unsigned char *p;
c5aa993b 3024
ed9a39eb 3025 if (!target_is_async_p ())
43ff13b4 3026 ofunc = signal (SIGINT, remote_interrupt);
6426a772
JM
3027 /* FIXME: cagney/1999-09-27: If we're in async mode we should
3028 _never_ wait for ever -> test on target_is_async_p().
3029 However, before we do that we need to ensure that the caller
3030 knows how to take the target into/out of async mode. */
c2d11a7d 3031 getpkt (buf, PBUFSIZ, wait_forever_enabled_p);
ed9a39eb 3032 if (!target_is_async_p ())
43ff13b4
JM
3033 signal (SIGINT, ofunc);
3034
3035 /* This is a hook for when we need to do something (perhaps the
c5aa993b 3036 collection of trace data) every time the target stops. */
43ff13b4
JM
3037 if (target_wait_loop_hook)
3038 (*target_wait_loop_hook) ();
3039
3040 switch (buf[0])
3041 {
3042 case 'E': /* Error of some sort */
3043 warning ("Remote failure reply: %s", buf);
3044 continue;
3045 case 'T': /* Status with PC, SP, FP, ... */
3046 {
3047 int i;
3048 long regno;
e6cbd02a 3049 char* regs = (char*) alloca (MAX_REGISTER_RAW_SIZE);
43ff13b4
JM
3050
3051 /* Expedited reply, containing Signal, {regno, reg} repeat */
3052 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
c5aa993b
JM
3053 ss = signal number
3054 n... = register number
3055 r... = register contents
3056 */
43ff13b4
JM
3057 p = &buf[3]; /* after Txx */
3058
3059 while (*p)
3060 {
3061 unsigned char *p1;
3062 char *p_temp;
3063
3064 /* Read the register number */
3065 regno = strtol ((const char *) p, &p_temp, 16);
c5aa993b 3066 p1 = (unsigned char *) p_temp;
43ff13b4 3067
c5aa993b 3068 if (p1 == p) /* No register number present here */
43ff13b4
JM
3069 {
3070 p1 = (unsigned char *) strchr ((const char *) p, ':');
3071 if (p1 == NULL)
3072 warning ("Malformed packet(a) (missing colon): %s\n\
3073Packet: '%s'\n",
3074 p, buf);
3075 if (strncmp ((const char *) p, "thread", p1 - p) == 0)
3076 {
3077 p_temp = unpack_varlen_hex (++p1, &thread_num);
3078 record_currthread (thread_num);
3079 p = (unsigned char *) p_temp;
3080 }
3081 }
3082 else
3083 {
3084 p = p1;
3085
3086 if (*p++ != ':')
3087 warning ("Malformed packet(b) (missing colon): %s\n\
3088Packet: '%s'\n",
3089 p, buf);
3090
3091 if (regno >= NUM_REGS)
3092 warning ("Remote sent bad register number %ld: %s\n\
3093Packet: '%s'\n",
3094 regno, p, buf);
3095
30559e10
MS
3096 if (hex2bin (p, regs, REGISTER_RAW_SIZE (regno))
3097 < REGISTER_RAW_SIZE (regno))
3098 warning ("Remote reply is too short: %s", buf);
43ff13b4
JM
3099 supply_register (regno, regs);
3100 }
3101
3102 if (*p++ != ';')
3103 {
3104 warning ("Remote register badly formatted: %s", buf);
c5aa993b 3105 warning (" here: %s", p);
43ff13b4
JM
3106 }
3107 }
3108 }
3109 /* fall through */
3110 case 'S': /* Old style status, just signal only */
3111 status->kind = TARGET_WAITKIND_STOPPED;
3112 status->value.sig = (enum target_signal)
3113 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
3114
3115 if (buf[3] == 'p')
3116 {
3117 /* Export Cisco kernel mode as a convenience variable
c5aa993b 3118 (so that it can be used in the GDB prompt if desired). */
43ff13b4
JM
3119
3120 if (cisco_kernel_mode == 1)
c5aa993b 3121 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
43ff13b4
JM
3122 value_from_string ("PDEBUG-"));
3123 cisco_kernel_mode = 0;
3124 thread_num = strtol ((const char *) &buf[4], NULL, 16);
3125 record_currthread (thread_num);
3126 }
3127 else if (buf[3] == 'k')
3128 {
3129 /* Export Cisco kernel mode as a convenience variable
c5aa993b 3130 (so that it can be used in the GDB prompt if desired). */
43ff13b4
JM
3131
3132 if (cisco_kernel_mode == 1)
c5aa993b 3133 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
43ff13b4
JM
3134 value_from_string ("KDEBUG-"));
3135 cisco_kernel_mode = 1;
3136 }
3137 goto got_status;
3138 case 'N': /* Cisco special: status and offsets */
3139 {
3140 bfd_vma text_addr, data_addr, bss_addr;
3141 bfd_signed_vma text_off, data_off, bss_off;
3142 unsigned char *p1;
3143
3144 status->kind = TARGET_WAITKIND_STOPPED;
3145 status->value.sig = (enum target_signal)
3146 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
3147
c5aa993b 3148 if (symfile_objfile == NULL)
43ff13b4
JM
3149 {
3150 warning ("Relocation packet recieved with no symbol file. \
3151Packet Dropped");
3152 goto got_status;
3153 }
3154
3155 /* Relocate object file. Buffer format is NAATT;DD;BB
3156 * where AA is the signal number, TT is the new text
3157 * address, DD * is the new data address, and BB is the
3158 * new bss address. */
3159
3160 p = &buf[3];
3161 text_addr = strtoul (p, (char **) &p1, 16);
3162 if (p1 == p || *p1 != ';')
3163 warning ("Malformed relocation packet: Packet '%s'", buf);
3164 p = p1 + 1;
3165 data_addr = strtoul (p, (char **) &p1, 16);
3166 if (p1 == p || *p1 != ';')
3167 warning ("Malformed relocation packet: Packet '%s'", buf);
3168 p = p1 + 1;
3169 bss_addr = strtoul (p, (char **) &p1, 16);
c5aa993b 3170 if (p1 == p)
43ff13b4
JM
3171 warning ("Malformed relocation packet: Packet '%s'", buf);
3172
3173 if (remote_cisco_section_offsets (text_addr, data_addr, bss_addr,
3174 &text_off, &data_off, &bss_off)
3175 == 0)
c5aa993b 3176 if (text_off != 0 || data_off != 0 || bss_off != 0)
43ff13b4
JM
3177 remote_cisco_objfile_relocate (text_off, data_off, bss_off);
3178
3179 goto got_status;
3180 }
3181 case 'W': /* Target exited */
3182 {
3183 /* The remote process exited. */
3184 status->kind = TARGET_WAITKIND_EXITED;
3185 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
3186 goto got_status;
3187 }
3188 case 'X':
3189 status->kind = TARGET_WAITKIND_SIGNALLED;
3190 status->value.sig = (enum target_signal)
3191 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
3192 kill_kludge = 1;
3193
3194 goto got_status;
3195 case 'O': /* Console output */
3196 remote_console_output (buf + 1);
c4093a6a
JM
3197 /* Return immediately to the event loop. The event loop will
3198 still be waiting on the inferior afterwards. */
3199 status->kind = TARGET_WAITKIND_IGNORE;
3200 goto got_status;
43ff13b4
JM
3201 case '\0':
3202 if (last_sent_signal != TARGET_SIGNAL_0)
3203 {
3204 /* Zero length reply means that we tried 'S' or 'C' and
c5aa993b 3205 the remote system doesn't support it. */
43ff13b4
JM
3206 target_terminal_ours_for_output ();
3207 printf_filtered
3208 ("Can't send signals to this remote system. %s not sent.\n",
3209 target_signal_to_name (last_sent_signal));
3210 last_sent_signal = TARGET_SIGNAL_0;
3211 target_terminal_inferior ();
3212
3213 strcpy ((char *) buf, last_sent_step ? "s" : "c");
3214 putpkt ((char *) buf);
3215 continue;
3216 }
3217 /* else fallthrough */
3218 default:
3219 warning ("Invalid remote reply: %s", buf);
3220 continue;
3221 }
3222 }
c5aa993b 3223got_status:
43ff13b4
JM
3224 if (thread_num != -1)
3225 {
39f77062 3226 return pid_to_ptid (thread_num);
43ff13b4 3227 }
39f77062 3228 return inferior_ptid;
43ff13b4
JM
3229}
3230
c906108c
SS
3231/* Number of bytes of registers this stub implements. */
3232
3233static int register_bytes_found;
3234
3235/* Read the remote registers into the block REGS. */
3236/* Currently we just read all the registers, so we don't use regno. */
3237
3238/* ARGSUSED */
3239static void
fba45db2 3240remote_fetch_registers (int regno)
c906108c 3241{
085dd6e6 3242 char *buf = alloca (PBUFSIZ);
c906108c
SS
3243 int i;
3244 char *p;
7302a204 3245 char *regs = alloca (REGISTER_BYTES);
c906108c 3246
39f77062 3247 set_thread (PIDGET (inferior_ptid), 1);
c906108c
SS
3248
3249 sprintf (buf, "g");
c2d11a7d 3250 remote_send (buf, PBUFSIZ);
c906108c 3251
11cf8741
JM
3252 /* Save the size of the packet sent to us by the target. Its used
3253 as a heuristic when determining the max size of packets that the
3254 target can safely receive. */
3255 if (actual_register_packet_size == 0)
3256 actual_register_packet_size = strlen (buf);
c906108c
SS
3257
3258 /* Unimplemented registers read as all bits zero. */
3259 memset (regs, 0, REGISTER_BYTES);
3260
3261 /* We can get out of synch in various cases. If the first character
3262 in the buffer is not a hex character, assume that has happened
3263 and try to fetch another packet to read. */
3264 while ((buf[0] < '0' || buf[0] > '9')
3265 && (buf[0] < 'a' || buf[0] > 'f')
3266 && buf[0] != 'x') /* New: unavailable register value */
3267 {
3268 if (remote_debug)
0f71a2f6
JM
3269 fprintf_unfiltered (gdb_stdlog,
3270 "Bad register packet; fetching a new packet\n");
c2d11a7d 3271 getpkt (buf, PBUFSIZ, 0);
c906108c
SS
3272 }
3273
3274 /* Reply describes registers byte by byte, each byte encoded as two
3275 hex characters. Suck them all up, then supply them to the
3276 register cacheing/storage mechanism. */
3277
3278 p = buf;
3279 for (i = 0; i < REGISTER_BYTES; i++)
3280 {
3281 if (p[0] == 0)
3282 break;
3283 if (p[1] == 0)
3284 {
3285 warning ("Remote reply is of odd length: %s", buf);
3286 /* Don't change register_bytes_found in this case, and don't
3287 print a second warning. */
3288 goto supply_them;
3289 }
3290 if (p[0] == 'x' && p[1] == 'x')
c5aa993b 3291 regs[i] = 0; /* 'x' */
c906108c
SS
3292 else
3293 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
3294 p += 2;
3295 }
3296
3297 if (i != register_bytes_found)
3298 {
3299 register_bytes_found = i;
2649061d
AC
3300 if (REGISTER_BYTES_OK_P ()
3301 && !REGISTER_BYTES_OK (i))
c906108c 3302 warning ("Remote reply is too short: %s", buf);
c906108c 3303 }
c5aa993b
JM
3304
3305supply_them:
c906108c 3306 for (i = 0; i < NUM_REGS; i++)
c5aa993b
JM
3307 {
3308 supply_register (i, &regs[REGISTER_BYTE (i)]);
3309 if (buf[REGISTER_BYTE (i) * 2] == 'x')
7302a204 3310 set_register_cached (i, -1);
c5aa993b 3311 }
c906108c
SS
3312}
3313
3314/* Prepare to store registers. Since we may send them all (using a
3315 'G' request), we have to read out the ones we don't want to change
3316 first. */
3317
c5aa993b 3318static void
fba45db2 3319remote_prepare_to_store (void)
c906108c
SS
3320{
3321 /* Make sure the entire registers array is valid. */
5a2468f5
JM
3322 switch (remote_protocol_P.support)
3323 {
3324 case PACKET_DISABLE:
3325 case PACKET_SUPPORT_UNKNOWN:
3326 read_register_bytes (0, (char *) NULL, REGISTER_BYTES);
3327 break;
3328 case PACKET_ENABLE:
3329 break;
3330 }
3331}
3332
3333/* Helper: Attempt to store REGNO using the P packet. Return fail IFF
3334 packet was not recognized. */
3335
3336static int
c2d11a7d 3337store_register_using_P (int regno)
5a2468f5
JM
3338{
3339 /* Try storing a single register. */
3340 char *buf = alloca (PBUFSIZ);
3341 char *regp;
3342 char *p;
3343 int i;
3344
3345 sprintf (buf, "P%x=", regno);
3346 p = buf + strlen (buf);
7302a204 3347 regp = register_buffer (regno);
30559e10 3348 bin2hex (regp, p, REGISTER_RAW_SIZE (regno));
c2d11a7d 3349 remote_send (buf, PBUFSIZ);
5a2468f5
JM
3350
3351 return buf[0] != '\0';
c906108c
SS
3352}
3353
5a2468f5 3354
c906108c 3355/* Store register REGNO, or all registers if REGNO == -1, from the contents
7302a204 3356 of the register cache buffer. FIXME: ignores errors. */
c906108c
SS
3357
3358static void
fba45db2 3359remote_store_registers (int regno)
c906108c 3360{
085dd6e6 3361 char *buf = alloca (PBUFSIZ);
c906108c
SS
3362 int i;
3363 char *p;
7302a204 3364 char *regs;
c906108c 3365
39f77062 3366 set_thread (PIDGET (inferior_ptid), 1);
c906108c 3367
5a2468f5 3368 if (regno >= 0)
c906108c 3369 {
5a2468f5 3370 switch (remote_protocol_P.support)
c906108c 3371 {
5a2468f5
JM
3372 case PACKET_DISABLE:
3373 break;
3374 case PACKET_ENABLE:
3375 if (store_register_using_P (regno))
3376 return;
3377 else
3378 error ("Protocol error: P packet not recognized by stub");
3379 case PACKET_SUPPORT_UNKNOWN:
3380 if (store_register_using_P (regno))
3381 {
3382 /* The stub recognized the 'P' packet. Remember this. */
3383 remote_protocol_P.support = PACKET_ENABLE;
3384 return;
3385 }
3386 else
3387 {
3388 /* The stub does not support the 'P' packet. Use 'G'
3389 instead, and don't try using 'P' in the future (it
3390 will just waste our time). */
3391 remote_protocol_P.support = PACKET_DISABLE;
3392 break;
3393 }
c906108c 3394 }
c906108c
SS
3395 }
3396
3397 buf[0] = 'G';
3398
3399 /* Command describes registers byte by byte,
3400 each byte encoded as two hex characters. */
3401
7302a204 3402 regs = register_buffer (-1);
c906108c
SS
3403 p = buf + 1;
3404 /* remote_prepare_to_store insures that register_bytes_found gets set. */
30559e10 3405 bin2hex (regs, p, register_bytes_found);
c2d11a7d 3406 remote_send (buf, PBUFSIZ);
c906108c 3407}
c906108c
SS
3408\f
3409
3410/* Return the number of hex digits in num. */
3411
3412static int
fba45db2 3413hexnumlen (ULONGEST num)
c906108c
SS
3414{
3415 int i;
3416
3417 for (i = 0; num != 0; i++)
3418 num >>= 4;
3419
3420 return max (i, 1);
3421}
3422
2df3850c 3423/* Set BUF to the minimum number of hex digits representing NUM. */
c906108c
SS
3424
3425static int
fba45db2 3426hexnumstr (char *buf, ULONGEST num)
c906108c 3427{
c906108c 3428 int len = hexnumlen (num);
2df3850c
JM
3429 return hexnumnstr (buf, num, len);
3430}
3431
c906108c 3432
2df3850c 3433/* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
c906108c 3434
2df3850c 3435static int
fba45db2 3436hexnumnstr (char *buf, ULONGEST num, int width)
2df3850c
JM
3437{
3438 int i;
3439
3440 buf[width] = '\0';
3441
3442 for (i = width - 1; i >= 0; i--)
c906108c 3443 {
c5aa993b 3444 buf[i] = "0123456789abcdef"[(num & 0xf)];
c906108c
SS
3445 num >>= 4;
3446 }
3447
2df3850c 3448 return width;
c906108c
SS
3449}
3450
3451/* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
3452
3453static CORE_ADDR
fba45db2 3454remote_address_masked (CORE_ADDR addr)
c906108c
SS
3455{
3456 if (remote_address_size > 0
3457 && remote_address_size < (sizeof (ULONGEST) * 8))
3458 {
3459 /* Only create a mask when that mask can safely be constructed
3460 in a ULONGEST variable. */
3461 ULONGEST mask = 1;
3462 mask = (mask << remote_address_size) - 1;
3463 addr &= mask;
3464 }
3465 return addr;
3466}
3467
3468/* Determine whether the remote target supports binary downloading.
3469 This is accomplished by sending a no-op memory write of zero length
3470 to the target at the specified address. It does not suffice to send
3471 the whole packet, since many stubs strip the eighth bit and subsequently
7a292a7a
SS
3472 compute a wrong checksum, which causes real havoc with remote_write_bytes.
3473
96baa820
JM
3474 NOTE: This can still lose if the serial line is not eight-bit
3475 clean. In cases like this, the user should clear "remote
3476 X-packet". */
3477
c906108c 3478static void
fba45db2 3479check_binary_download (CORE_ADDR addr)
c906108c 3480{
96baa820 3481 switch (remote_protocol_binary_download.support)
c906108c 3482 {
96baa820
JM
3483 case PACKET_DISABLE:
3484 break;
3485 case PACKET_ENABLE:
3486 break;
3487 case PACKET_SUPPORT_UNKNOWN:
3488 {
3489 char *buf = alloca (PBUFSIZ);
3490 char *p;
3491
3492 p = buf;
3493 *p++ = 'X';
3494 p += hexnumstr (p, (ULONGEST) addr);
3495 *p++ = ',';
3496 p += hexnumstr (p, (ULONGEST) 0);
3497 *p++ = ':';
3498 *p = '\0';
3499
3500 putpkt_binary (buf, (int) (p - buf));
c2d11a7d 3501 getpkt (buf, PBUFSIZ, 0);
c906108c 3502
96baa820
JM
3503 if (buf[0] == '\0')
3504 {
3505 if (remote_debug)
3506 fprintf_unfiltered (gdb_stdlog,
3507 "binary downloading NOT suppported by target\n");
3508 remote_protocol_binary_download.support = PACKET_DISABLE;
3509 }
3510 else
3511 {
3512 if (remote_debug)
3513 fprintf_unfiltered (gdb_stdlog,
3514 "binary downloading suppported by target\n");
3515 remote_protocol_binary_download.support = PACKET_ENABLE;
3516 }
3517 break;
3518 }
c906108c
SS
3519 }
3520}
3521
3522/* Write memory data directly to the remote machine.
3523 This does not inform the data cache; the data cache uses this.
3524 MEMADDR is the address in the remote memory space.
3525 MYADDR is the address of the buffer in our space.
3526 LEN is the number of bytes.
3527
917317f4
JM
3528 Returns number of bytes transferred, or 0 (setting errno) for
3529 error. Only transfer a single packet. */
c906108c
SS
3530
3531static int
917317f4 3532remote_write_bytes (CORE_ADDR memaddr, char *myaddr, int len)
c906108c 3533{
917317f4 3534 unsigned char *buf;
c906108c 3535 int max_buf_size; /* Max size of packet output buffer */
917317f4
JM
3536 unsigned char *p;
3537 unsigned char *plen;
c2d11a7d 3538 long sizeof_buf;
917317f4
JM
3539 int plenlen;
3540 int todo;
3541 int nr_bytes;
c906108c
SS
3542
3543 /* Verify that the target can support a binary download */
3544 check_binary_download (memaddr);
3545
917317f4 3546 /* Determine the max packet size. */
11cf8741 3547 max_buf_size = get_memory_write_packet_size ();
c2d11a7d
JM
3548 sizeof_buf = max_buf_size + 1; /* Space for trailing NUL */
3549 buf = alloca (sizeof_buf);
c906108c 3550
7a292a7a 3551 /* Subtract header overhead from max payload size - $M<memaddr>,<len>:#nn */
c906108c
SS
3552 max_buf_size -= 2 + hexnumlen (memaddr + len - 1) + 1 + hexnumlen (len) + 4;
3553
917317f4
JM
3554 /* construct "M"<memaddr>","<len>":" */
3555 /* sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, todo); */
3556 p = buf;
3557
3558 /* Append [XM]. Compute a best guess of the number of bytes
3559 actually transfered. */
3560 switch (remote_protocol_binary_download.support)
c906108c 3561 {
917317f4
JM
3562 case PACKET_ENABLE:
3563 *p++ = 'X';
3564 /* Best guess at number of bytes that will fit. */
3565 todo = min (len, max_buf_size);
3566 break;
3567 case PACKET_DISABLE:
3568 *p++ = 'M';
3569 /* num bytes that will fit */
3570 todo = min (len, max_buf_size / 2);
3571 break;
3572 case PACKET_SUPPORT_UNKNOWN:
8e65ff28
AC
3573 internal_error (__FILE__, __LINE__,
3574 "remote_write_bytes: bad internal state");
7f7e9482 3575 default:
8e65ff28 3576 internal_error (__FILE__, __LINE__, "bad switch");
917317f4
JM
3577 }
3578
3579 /* Append <memaddr> */
3580 memaddr = remote_address_masked (memaddr);
3581 p += hexnumstr (p, (ULONGEST) memaddr);
3582 *p++ = ',';
3583
3584 /* Append <len>. Retain the location/size of <len>. It may
3585 need to be adjusted once the packet body has been created. */
3586 plen = p;
3587 plenlen = hexnumstr (p, (ULONGEST) todo);
3588 p += plenlen;
3589 *p++ = ':';
3590 *p = '\0';
3591
3592 /* Append the packet body. */
3593 switch (remote_protocol_binary_download.support)
3594 {
3595 case PACKET_ENABLE:
3596 /* Binary mode. Send target system values byte by byte, in
3597 increasing byte addresses. Only escape certain critical
3598 characters. */
3599 for (nr_bytes = 0;
3600 (nr_bytes < todo) && (p - buf) < (max_buf_size - 2);
3601 nr_bytes++)
c906108c 3602 {
917317f4
JM
3603 switch (myaddr[nr_bytes] & 0xff)
3604 {
3605 case '$':
3606 case '#':
3607 case 0x7d:
3608 /* These must be escaped */
3609 *p++ = 0x7d;
3610 *p++ = (myaddr[nr_bytes] & 0xff) ^ 0x20;
3611 break;
3612 default:
3613 *p++ = myaddr[nr_bytes] & 0xff;
3614 break;
3615 }
c906108c 3616 }
917317f4 3617 if (nr_bytes < todo)
c906108c 3618 {
917317f4
JM
3619 /* Escape chars have filled up the buffer prematurely,
3620 and we have actually sent fewer bytes than planned.
3621 Fix-up the length field of the packet. Use the same
3622 number of characters as before. */
3623
3624 plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen);
3625 *plen = ':'; /* overwrite \0 from hexnumnstr() */
c906108c 3626 }
917317f4
JM
3627 break;
3628 case PACKET_DISABLE:
3629 /* Normal mode: Send target system values byte by byte, in
3630 increasing byte addresses. Each byte is encoded as a two hex
3631 value. */
30559e10 3632 bin2hex (myaddr, p, todo);
1dd1751e 3633 nr_bytes = todo;
917317f4
JM
3634 break;
3635 case PACKET_SUPPORT_UNKNOWN:
8e65ff28
AC
3636 internal_error (__FILE__, __LINE__,
3637 "remote_write_bytes: bad internal state");
7f7e9482 3638 default:
8e65ff28 3639 internal_error (__FILE__, __LINE__, "bad switch");
c906108c 3640 }
917317f4
JM
3641
3642 putpkt_binary (buf, (int) (p - buf));
c2d11a7d 3643 getpkt (buf, sizeof_buf, 0);
917317f4
JM
3644
3645 if (buf[0] == 'E')
3646 {
3647 /* There is no correspondance between what the remote protocol
3648 uses for errors and errno codes. We would like a cleaner way
3649 of representing errors (big enough to include errno codes,
3650 bfd_error codes, and others). But for now just return EIO. */
3651 errno = EIO;
3652 return 0;
3653 }
3654
3655 /* Return NR_BYTES, not TODO, in case escape chars caused us to send fewer
3656 bytes than we'd planned. */
3657 return nr_bytes;
c906108c
SS
3658}
3659
3660/* Read memory data directly from the remote machine.
3661 This does not use the data cache; the data cache uses this.
3662 MEMADDR is the address in the remote memory space.
3663 MYADDR is the address of the buffer in our space.
3664 LEN is the number of bytes.
3665
3666 Returns number of bytes transferred, or 0 for error. */
3667
917317f4
JM
3668/* NOTE: cagney/1999-10-18: This function (and its siblings in other
3669 remote targets) shouldn't attempt to read the entire buffer.
3670 Instead it should read a single packet worth of data and then
3671 return the byte size of that packet to the caller. The caller (its
3672 caller and its callers caller ;-) already contains code for
3673 handling partial reads. */
3674
c906108c 3675static int
fba45db2 3676remote_read_bytes (CORE_ADDR memaddr, char *myaddr, int len)
c906108c 3677{
11cf8741 3678 char *buf;
c906108c 3679 int max_buf_size; /* Max size of packet output buffer */
c2d11a7d 3680 long sizeof_buf;
c906108c
SS
3681 int origlen;
3682
11cf8741
JM
3683 /* Create a buffer big enough for this packet. */
3684 max_buf_size = get_memory_read_packet_size ();
c2d11a7d
JM
3685 sizeof_buf = max_buf_size + 1; /* Space for trailing NUL */
3686 buf = alloca (sizeof_buf);
c906108c
SS
3687
3688 origlen = len;
3689 while (len > 0)
3690 {
c906108c
SS
3691 char *p;
3692 int todo;
3693 int i;
3694
c5aa993b 3695 todo = min (len, max_buf_size / 2); /* num bytes that will fit */
c906108c
SS
3696
3697 /* construct "m"<memaddr>","<len>" */
3698 /* sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo); */
3699 memaddr = remote_address_masked (memaddr);
3700 p = buf;
3701 *p++ = 'm';
3702 p += hexnumstr (p, (ULONGEST) memaddr);
3703 *p++ = ',';
3704 p += hexnumstr (p, (ULONGEST) todo);
3705 *p = '\0';
3706
3707 putpkt (buf);
c2d11a7d 3708 getpkt (buf, sizeof_buf, 0);
c906108c
SS
3709
3710 if (buf[0] == 'E')
3711 {
3712 /* There is no correspondance between what the remote protocol uses
3713 for errors and errno codes. We would like a cleaner way of
3714 representing errors (big enough to include errno codes, bfd_error
3715 codes, and others). But for now just return EIO. */
3716 errno = EIO;
3717 return 0;
3718 }
3719
c5aa993b
JM
3720 /* Reply describes memory byte by byte,
3721 each byte encoded as two hex characters. */
c906108c
SS
3722
3723 p = buf;
30559e10 3724 if ((i = hex2bin (p, myaddr, todo)) < todo)
c906108c 3725 {
30559e10
MS
3726 /* Reply is short. This means that we were able to read
3727 only part of what we wanted to. */
3728 return i + (origlen - len);
c906108c
SS
3729 }
3730 myaddr += todo;
3731 memaddr += todo;
3732 len -= todo;
3733 }
3734 return origlen;
3735}
3736\f
3737/* Read or write LEN bytes from inferior memory at MEMADDR,
392a587b
JM
3738 transferring to or from debugger address BUFFER. Write to inferior if
3739 SHOULD_WRITE is nonzero. Returns length of data written or read; 0
c338868a 3740 for error. TARGET is unused. */
392a587b 3741
c906108c
SS
3742/* ARGSUSED */
3743static int
c338868a 3744remote_xfer_memory (CORE_ADDR mem_addr, char *buffer, int mem_len,
29e57380
C
3745 int should_write,
3746 struct mem_attrib *attrib ATTRIBUTE_UNUSED,
3747 struct target_ops *target)
c906108c 3748{
392a587b
JM
3749 CORE_ADDR targ_addr;
3750 int targ_len;
4930751a
C
3751 int res;
3752
392a587b
JM
3753 REMOTE_TRANSLATE_XFER_ADDRESS (mem_addr, mem_len, &targ_addr, &targ_len);
3754 if (targ_len <= 0)
c906108c 3755 return 0;
c906108c 3756
4930751a
C
3757 if (should_write)
3758 res = remote_write_bytes (targ_addr, buffer, targ_len);
3759 else
3760 res = remote_read_bytes (targ_addr, buffer, targ_len);
3761
3762 return res;
c906108c
SS
3763}
3764
c5aa993b 3765
c906108c
SS
3766#if 0
3767/* Enable after 4.12. */
3768
3769void
c338868a
KB
3770remote_search (int len, char *data, char *mask, CORE_ADDR startaddr,
3771 int increment, CORE_ADDR lorange, CORE_ADDR hirange,
3772 CORE_ADDR *addr_found, char *data_found)
c906108c
SS
3773{
3774 if (increment == -4 && len == 4)
3775 {
3776 long mask_long, data_long;
3777 long data_found_long;
3778 CORE_ADDR addr_we_found;
085dd6e6 3779 char *buf = alloca (PBUFSIZ);
c906108c
SS
3780 long returned_long[2];
3781 char *p;
3782
3783 mask_long = extract_unsigned_integer (mask, len);
3784 data_long = extract_unsigned_integer (data, len);
3785 sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long);
3786 putpkt (buf);
c2d11a7d 3787 getpkt (buf, PBUFSIZ, 0);
c906108c
SS
3788 if (buf[0] == '\0')
3789 {
3790 /* The stub doesn't support the 't' request. We might want to
3791 remember this fact, but on the other hand the stub could be
3792 switched on us. Maybe we should remember it only until
3793 the next "target remote". */
3794 generic_search (len, data, mask, startaddr, increment, lorange,
3795 hirange, addr_found, data_found);
3796 return;
3797 }
3798
3799 if (buf[0] == 'E')
3800 /* There is no correspondance between what the remote protocol uses
3801 for errors and errno codes. We would like a cleaner way of
3802 representing errors (big enough to include errno codes, bfd_error
3803 codes, and others). But for now just use EIO. */
3804 memory_error (EIO, startaddr);
3805 p = buf;
3806 addr_we_found = 0;
3807 while (*p != '\0' && *p != ',')
3808 addr_we_found = (addr_we_found << 4) + fromhex (*p++);
3809 if (*p == '\0')
3810 error ("Protocol error: short return for search");
3811
3812 data_found_long = 0;
3813 while (*p != '\0' && *p != ',')
3814 data_found_long = (data_found_long << 4) + fromhex (*p++);
3815 /* Ignore anything after this comma, for future extensions. */
3816
3817 if (addr_we_found < lorange || addr_we_found >= hirange)
3818 {
3819 *addr_found = 0;
3820 return;
3821 }
3822
3823 *addr_found = addr_we_found;
3824 *data_found = store_unsigned_integer (data_we_found, len);
3825 return;
3826 }
3827 generic_search (len, data, mask, startaddr, increment, lorange,
3828 hirange, addr_found, data_found);
3829}
3830#endif /* 0 */
3831\f
3832static void
fba45db2 3833remote_files_info (struct target_ops *ignore)
c906108c
SS
3834{
3835 puts_filtered ("Debugging a target over a serial line.\n");
3836}
3837\f
3838/* Stuff for dealing with the packets which are part of this protocol.
3839 See comment at top of file for details. */
3840
3841/* Read a single character from the remote end, masking it down to 7 bits. */
3842
3843static int
fba45db2 3844readchar (int timeout)
c906108c
SS
3845{
3846 int ch;
3847
3848 ch = SERIAL_READCHAR (remote_desc, timeout);
3849
2acceee2
JM
3850 if (ch >= 0)
3851 return (ch & 0x7f);
3852
3853 switch ((enum serial_rc) ch)
c906108c
SS
3854 {
3855 case SERIAL_EOF:
2acceee2 3856 target_mourn_inferior ();
c906108c 3857 error ("Remote connection closed");
2acceee2 3858 /* no return */
c906108c
SS
3859 case SERIAL_ERROR:
3860 perror_with_name ("Remote communication error");
2acceee2 3861 /* no return */
c906108c 3862 case SERIAL_TIMEOUT:
2acceee2 3863 break;
c906108c 3864 }
2acceee2 3865 return ch;
c906108c
SS
3866}
3867
3868/* Send the command in BUF to the remote machine, and read the reply
3869 into BUF. Report an error if we get an error reply. */
3870
3871static void
c2d11a7d
JM
3872remote_send (char *buf,
3873 long sizeof_buf)
c906108c
SS
3874{
3875 putpkt (buf);
c2d11a7d 3876 getpkt (buf, sizeof_buf, 0);
c906108c
SS
3877
3878 if (buf[0] == 'E')
3879 error ("Remote failure reply: %s", buf);
3880}
3881
3882/* Display a null-terminated packet on stdout, for debugging, using C
3883 string notation. */
3884
3885static void
fba45db2 3886print_packet (char *buf)
c906108c
SS
3887{
3888 puts_filtered ("\"");
43e526b9 3889 fputstr_filtered (buf, '"', gdb_stdout);
c906108c
SS
3890 puts_filtered ("\"");
3891}
3892
3893int
fba45db2 3894putpkt (char *buf)
c906108c
SS
3895{
3896 return putpkt_binary (buf, strlen (buf));
3897}
3898
3899/* Send a packet to the remote machine, with error checking. The data
3900 of the packet is in BUF. The string in BUF can be at most PBUFSIZ - 5
3901 to account for the $, # and checksum, and for a possible /0 if we are
3902 debugging (remote_debug) and want to print the sent packet as a string */
3903
3904static int
fba45db2 3905putpkt_binary (char *buf, int cnt)
c906108c
SS
3906{
3907 int i;
3908 unsigned char csum = 0;
11cf8741 3909 char *buf2 = alloca (cnt + 6);
c2d11a7d
JM
3910 long sizeof_junkbuf = PBUFSIZ;
3911 char *junkbuf = alloca (sizeof_junkbuf);
085dd6e6 3912
c906108c
SS
3913 int ch;
3914 int tcount = 0;
3915 char *p;
3916
3917 /* Copy the packet into buffer BUF2, encapsulating it
3918 and giving it a checksum. */
3919
c906108c
SS
3920 p = buf2;
3921 *p++ = '$';
3922
3923 for (i = 0; i < cnt; i++)
3924 {
3925 csum += buf[i];
3926 *p++ = buf[i];
3927 }
3928 *p++ = '#';
3929 *p++ = tohex ((csum >> 4) & 0xf);
3930 *p++ = tohex (csum & 0xf);
3931
3932 /* Send it over and over until we get a positive ack. */
3933
3934 while (1)
3935 {
3936 int started_error_output = 0;
3937
3938 if (remote_debug)
3939 {
3940 *p = '\0';
43e526b9
JM
3941 fprintf_unfiltered (gdb_stdlog, "Sending packet: ");
3942 fputstrn_unfiltered (buf2, p - buf2, 0, gdb_stdlog);
d4f3574e 3943 fprintf_unfiltered (gdb_stdlog, "...");
0f71a2f6 3944 gdb_flush (gdb_stdlog);
c906108c
SS
3945 }
3946 if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
3947 perror_with_name ("putpkt: write failed");
3948
3949 /* read until either a timeout occurs (-2) or '+' is read */
3950 while (1)
3951 {
3952 ch = readchar (remote_timeout);
3953
c5aa993b 3954 if (remote_debug)
c906108c
SS
3955 {
3956 switch (ch)
3957 {
3958 case '+':
1216fa2c 3959 case '-':
c906108c
SS
3960 case SERIAL_TIMEOUT:
3961 case '$':
3962 if (started_error_output)
3963 {
3964 putchar_unfiltered ('\n');
3965 started_error_output = 0;
3966 }
3967 }
3968 }
3969
3970 switch (ch)
3971 {
3972 case '+':
3973 if (remote_debug)
0f71a2f6 3974 fprintf_unfiltered (gdb_stdlog, "Ack\n");
c906108c 3975 return 1;
1216fa2c
AC
3976 case '-':
3977 if (remote_debug)
3978 fprintf_unfiltered (gdb_stdlog, "Nak\n");
c906108c 3979 case SERIAL_TIMEOUT:
c5aa993b 3980 tcount++;
c906108c
SS
3981 if (tcount > 3)
3982 return 0;
3983 break; /* Retransmit buffer */
3984 case '$':
3985 {
40e3f985
FN
3986 if (remote_debug)
3987 fprintf_unfiltered (gdb_stdlog, "Packet instead of Ack, ignoring it\n");
c5aa993b
JM
3988 /* It's probably an old response, and we're out of sync.
3989 Just gobble up the packet and ignore it. */
40e3f985 3990 read_frame (junkbuf, sizeof_junkbuf);
c5aa993b 3991 continue; /* Now, go look for + */
c906108c
SS
3992 }
3993 default:
3994 if (remote_debug)
3995 {
3996 if (!started_error_output)
3997 {
3998 started_error_output = 1;
0f71a2f6 3999 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
c906108c 4000 }
0f71a2f6 4001 fputc_unfiltered (ch & 0177, gdb_stdlog);
c906108c
SS
4002 }
4003 continue;
4004 }
4005 break; /* Here to retransmit */
4006 }
4007
4008#if 0
4009 /* This is wrong. If doing a long backtrace, the user should be
c5aa993b
JM
4010 able to get out next time we call QUIT, without anything as
4011 violent as interrupt_query. If we want to provide a way out of
4012 here without getting to the next QUIT, it should be based on
4013 hitting ^C twice as in remote_wait. */
c906108c
SS
4014 if (quit_flag)
4015 {
4016 quit_flag = 0;
4017 interrupt_query ();
4018 }
4019#endif
4020 }
4021}
4022
0f71a2f6
JM
4023static int remote_cisco_mode;
4024
c906108c
SS
4025/* Come here after finding the start of the frame. Collect the rest
4026 into BUF, verifying the checksum, length, and handling run-length
c2d11a7d
JM
4027 compression. No more than sizeof_buf-1 characters are read so that
4028 the buffer can be NUL terminated.
c906108c 4029
c2d11a7d
JM
4030 Returns -1 on error, number of characters in buffer (ignoring the
4031 trailing NULL) on success. (could be extended to return one of the
4032 SERIAL status indications). */
4033
4034static long
4035read_frame (char *buf,
4036 long sizeof_buf)
c906108c
SS
4037{
4038 unsigned char csum;
c2d11a7d 4039 long bc;
c906108c
SS
4040 int c;
4041
4042 csum = 0;
c2d11a7d 4043 bc = 0;
c906108c
SS
4044
4045 while (1)
4046 {
c2d11a7d 4047 /* ASSERT (bc < sizeof_buf - 1) - space for trailing NUL */
c906108c 4048 c = readchar (remote_timeout);
c906108c
SS
4049 switch (c)
4050 {
4051 case SERIAL_TIMEOUT:
4052 if (remote_debug)
0f71a2f6 4053 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
c2d11a7d 4054 return -1;
c906108c
SS
4055 case '$':
4056 if (remote_debug)
0f71a2f6
JM
4057 fputs_filtered ("Saw new packet start in middle of old one\n",
4058 gdb_stdlog);
c2d11a7d 4059 return -1; /* Start a new packet, count retries */
c906108c
SS
4060 case '#':
4061 {
4062 unsigned char pktcsum;
e1b09194
AC
4063 int check_0 = 0;
4064 int check_1 = 0;
c906108c 4065
c2d11a7d 4066 buf[bc] = '\0';
c906108c 4067
e1b09194
AC
4068 check_0 = readchar (remote_timeout);
4069 if (check_0 >= 0)
4070 check_1 = readchar (remote_timeout);
4071
4072 if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
4073 {
4074 if (remote_debug)
4075 fputs_filtered ("Timeout in checksum, retrying\n", gdb_stdlog);
4076 return -1;
4077 }
4078 else if (check_0 < 0 || check_1 < 0)
40e3f985
FN
4079 {
4080 if (remote_debug)
4081 fputs_filtered ("Communication error in checksum\n", gdb_stdlog);
4082 return -1;
4083 }
c906108c 4084
e1b09194 4085 pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
c906108c 4086 if (csum == pktcsum)
c2d11a7d 4087 return bc;
c906108c 4088
c5aa993b 4089 if (remote_debug)
c906108c 4090 {
0f71a2f6 4091 fprintf_filtered (gdb_stdlog,
c5aa993b 4092 "Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
0f71a2f6
JM
4093 pktcsum, csum);
4094 fputs_filtered (buf, gdb_stdlog);
4095 fputs_filtered ("\n", gdb_stdlog);
c906108c 4096 }
c2d11a7d
JM
4097 /* Number of characters in buffer ignoring trailing
4098 NUL. */
4099 return -1;
c906108c
SS
4100 }
4101 case '*': /* Run length encoding */
c2c6d25f
JM
4102 {
4103 int repeat;
4104 csum += c;
c906108c 4105
c2c6d25f
JM
4106 if (remote_cisco_mode == 0)
4107 {
4108 c = readchar (remote_timeout);
4109 csum += c;
4110 repeat = c - ' ' + 3; /* Compute repeat count */
4111 }
4112 else
4113 {
4114 /* Cisco's run-length encoding variant uses two
4115 hex chars to represent the repeat count. */
4116
4117 c = readchar (remote_timeout);
4118 csum += c;
4119 repeat = fromhex (c) << 4;
4120 c = readchar (remote_timeout);
4121 csum += c;
4122 repeat += fromhex (c);
4123 }
c906108c 4124
c2d11a7d
JM
4125 /* The character before ``*'' is repeated. */
4126
c2c6d25f 4127 if (repeat > 0 && repeat <= 255
c2d11a7d
JM
4128 && bc > 0
4129 && bc + repeat < sizeof_buf - 1)
c2c6d25f 4130 {
c2d11a7d
JM
4131 memset (&buf[bc], buf[bc - 1], repeat);
4132 bc += repeat;
c2c6d25f
JM
4133 continue;
4134 }
4135
c2d11a7d 4136 buf[bc] = '\0';
c2c6d25f
JM
4137 printf_filtered ("Repeat count %d too large for buffer: ", repeat);
4138 puts_filtered (buf);
4139 puts_filtered ("\n");
c2d11a7d 4140 return -1;
c2c6d25f 4141 }
c906108c 4142 default:
c2d11a7d 4143 if (bc < sizeof_buf - 1)
c906108c 4144 {
c2d11a7d 4145 buf[bc++] = c;
c906108c
SS
4146 csum += c;
4147 continue;
4148 }
4149
c2d11a7d 4150 buf[bc] = '\0';
c906108c
SS
4151 puts_filtered ("Remote packet too long: ");
4152 puts_filtered (buf);
4153 puts_filtered ("\n");
4154
c2d11a7d 4155 return -1;
c906108c
SS
4156 }
4157 }
4158}
4159
4160/* Read a packet from the remote machine, with error checking, and
c2d11a7d
JM
4161 store it in BUF. If FOREVER, wait forever rather than timing out;
4162 this is used (in synchronous mode) to wait for a target that is is
4163 executing user code to stop. */
d9fcf2fb
JM
4164/* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
4165 don't have to change all the calls to getpkt to deal with the
4166 return value, because at the moment I don't know what the right
4167 thing to do it for those. */
c906108c 4168void
c2d11a7d
JM
4169getpkt (char *buf,
4170 long sizeof_buf,
4171 int forever)
d9fcf2fb
JM
4172{
4173 int timed_out;
4174
4175 timed_out = getpkt_sane (buf, sizeof_buf, forever);
4176}
4177
4178
4179/* Read a packet from the remote machine, with error checking, and
4180 store it in BUF. If FOREVER, wait forever rather than timing out;
4181 this is used (in synchronous mode) to wait for a target that is is
4182 executing user code to stop. If FOREVER == 0, this function is
4183 allowed to time out gracefully and return an indication of this to
4184 the caller. */
3172dc30 4185static int
d9fcf2fb
JM
4186getpkt_sane (char *buf,
4187 long sizeof_buf,
4188 int forever)
c906108c
SS
4189{
4190 int c;
4191 int tries;
4192 int timeout;
4193 int val;
4194
c5aa993b 4195 strcpy (buf, "timeout");
c906108c
SS
4196
4197 if (forever)
4198 {
c906108c 4199 timeout = watchdog > 0 ? watchdog : -1;
c906108c
SS
4200 }
4201
4202 else
4203 timeout = remote_timeout;
4204
4205#define MAX_TRIES 3
4206
4207 for (tries = 1; tries <= MAX_TRIES; tries++)
4208 {
4209 /* This can loop forever if the remote side sends us characters
c5aa993b
JM
4210 continuously, but if it pauses, we'll get a zero from readchar
4211 because of timeout. Then we'll count that as a retry. */
c906108c
SS
4212
4213 /* Note that we will only wait forever prior to the start of a packet.
c5aa993b
JM
4214 After that, we expect characters to arrive at a brisk pace. They
4215 should show up within remote_timeout intervals. */
c906108c
SS
4216
4217 do
4218 {
4219 c = readchar (timeout);
4220
4221 if (c == SERIAL_TIMEOUT)
4222 {
2acceee2 4223 if (forever) /* Watchdog went off? Kill the target. */
c906108c 4224 {
2acceee2 4225 QUIT;
c906108c
SS
4226 target_mourn_inferior ();
4227 error ("Watchdog has expired. Target detached.\n");
4228 }
c906108c 4229 if (remote_debug)
0f71a2f6 4230 fputs_filtered ("Timed out.\n", gdb_stdlog);
c906108c
SS
4231 goto retry;
4232 }
4233 }
4234 while (c != '$');
4235
4236 /* We've found the start of a packet, now collect the data. */
4237
c2d11a7d 4238 val = read_frame (buf, sizeof_buf);
c906108c 4239
c2d11a7d 4240 if (val >= 0)
c906108c
SS
4241 {
4242 if (remote_debug)
43e526b9
JM
4243 {
4244 fprintf_unfiltered (gdb_stdlog, "Packet received: ");
4245 fputstr_unfiltered (buf, 0, gdb_stdlog);
4246 fprintf_unfiltered (gdb_stdlog, "\n");
4247 }
c906108c 4248 SERIAL_WRITE (remote_desc, "+", 1);
d9fcf2fb 4249 return 0;
c906108c
SS
4250 }
4251
4252 /* Try the whole thing again. */
4253 retry:
4254 SERIAL_WRITE (remote_desc, "-", 1);
4255 }
4256
4257 /* We have tried hard enough, and just can't receive the packet. Give up. */
4258
4259 printf_unfiltered ("Ignoring packet error, continuing...\n");
4260 SERIAL_WRITE (remote_desc, "+", 1);
d9fcf2fb 4261 return 1;
c906108c
SS
4262}
4263\f
4264static void
fba45db2 4265remote_kill (void)
c906108c
SS
4266{
4267 /* For some mysterious reason, wait_for_inferior calls kill instead of
4268 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4269 if (kill_kludge)
4270 {
4271 kill_kludge = 0;
4272 target_mourn_inferior ();
4273 return;
4274 }
4275
4276 /* Use catch_errors so the user can quit from gdb even when we aren't on
4277 speaking terms with the remote system. */
c5aa993b 4278 catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR);
c906108c
SS
4279
4280 /* Don't wait for it to die. I'm not really sure it matters whether
4281 we do or not. For the existing stubs, kill is a noop. */
4282 target_mourn_inferior ();
4283}
4284
43ff13b4
JM
4285/* Async version of remote_kill. */
4286static void
fba45db2 4287remote_async_kill (void)
43ff13b4
JM
4288{
4289 /* Unregister the file descriptor from the event loop. */
ed9a39eb 4290 if (target_is_async_p ())
c2c6d25f 4291 SERIAL_ASYNC (remote_desc, NULL, 0);
43ff13b4
JM
4292
4293 /* For some mysterious reason, wait_for_inferior calls kill instead of
4294 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4295 if (kill_kludge)
4296 {
4297 kill_kludge = 0;
4298 target_mourn_inferior ();
4299 return;
4300 }
4301
4302 /* Use catch_errors so the user can quit from gdb even when we aren't on
4303 speaking terms with the remote system. */
c5aa993b 4304 catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR);
43ff13b4
JM
4305
4306 /* Don't wait for it to die. I'm not really sure it matters whether
4307 we do or not. For the existing stubs, kill is a noop. */
4308 target_mourn_inferior ();
4309}
4310
c906108c 4311static void
fba45db2 4312remote_mourn (void)
c906108c
SS
4313{
4314 remote_mourn_1 (&remote_ops);
4315}
4316
53a5351d 4317static void
fba45db2 4318remote_async_mourn (void)
53a5351d
JM
4319{
4320 remote_mourn_1 (&remote_async_ops);
4321}
4322
c906108c 4323static void
fba45db2 4324extended_remote_mourn (void)
c906108c
SS
4325{
4326 /* We do _not_ want to mourn the target like this; this will
4327 remove the extended remote target from the target stack,
4328 and the next time the user says "run" it'll fail.
4329
4330 FIXME: What is the right thing to do here? */
4331#if 0
4332 remote_mourn_1 (&extended_remote_ops);
4333#endif
4334}
4335
4336/* Worker function for remote_mourn. */
4337static void
fba45db2 4338remote_mourn_1 (struct target_ops *target)
c906108c
SS
4339{
4340 unpush_target (target);
4341 generic_mourn_inferior ();
4342}
4343
4344/* In the extended protocol we want to be able to do things like
4345 "run" and have them basically work as expected. So we need
4346 a special create_inferior function.
4347
4348 FIXME: One day add support for changing the exec file
4349 we're debugging, arguments and an environment. */
4350
4351static void
fba45db2 4352extended_remote_create_inferior (char *exec_file, char *args, char **env)
c906108c
SS
4353{
4354 /* Rip out the breakpoints; we'll reinsert them after restarting
4355 the remote server. */
4356 remove_breakpoints ();
4357
4358 /* Now restart the remote server. */
4359 extended_remote_restart ();
4360
4361 /* Now put the breakpoints back in. This way we're safe if the
4362 restart function works via a unix fork on the remote side. */
4363 insert_breakpoints ();
4364
4365 /* Clean up from the last time we were running. */
4366 clear_proceed_status ();
4367
4368 /* Let the remote process run. */
4369 proceed (-1, TARGET_SIGNAL_0, 0);
4370}
4371
43ff13b4
JM
4372/* Async version of extended_remote_create_inferior. */
4373static void
fba45db2 4374extended_remote_async_create_inferior (char *exec_file, char *args, char **env)
43ff13b4
JM
4375{
4376 /* Rip out the breakpoints; we'll reinsert them after restarting
4377 the remote server. */
4378 remove_breakpoints ();
4379
4380 /* If running asynchronously, register the target file descriptor
4381 with the event loop. */
2acceee2
JM
4382 if (event_loop_p && target_can_async_p ())
4383 target_async (inferior_event_handler, 0);
43ff13b4
JM
4384
4385 /* Now restart the remote server. */
4386 extended_remote_restart ();
4387
4388 /* Now put the breakpoints back in. This way we're safe if the
4389 restart function works via a unix fork on the remote side. */
4390 insert_breakpoints ();
4391
4392 /* Clean up from the last time we were running. */
4393 clear_proceed_status ();
4394
4395 /* Let the remote process run. */
4396 proceed (-1, TARGET_SIGNAL_0, 0);
4397}
c906108c 4398\f
c5aa993b 4399
c906108c
SS
4400/* On some machines, e.g. 68k, we may use a different breakpoint instruction
4401 than other targets; in those use REMOTE_BREAKPOINT instead of just
4402 BREAKPOINT. Also, bi-endian targets may define LITTLE_REMOTE_BREAKPOINT
4403 and BIG_REMOTE_BREAKPOINT. If none of these are defined, we just call
4404 the standard routines that are in mem-break.c. */
4405
4406/* FIXME, these ought to be done in a more dynamic fashion. For instance,
4407 the choice of breakpoint instruction affects target program design and
4408 vice versa, and by making it user-tweakable, the special code here
4409 goes away and we need fewer special GDB configurations. */
4410
4411#if defined (LITTLE_REMOTE_BREAKPOINT) && defined (BIG_REMOTE_BREAKPOINT) && !defined(REMOTE_BREAKPOINT)
4412#define REMOTE_BREAKPOINT
4413#endif
4414
4415#ifdef REMOTE_BREAKPOINT
4416
4417/* If the target isn't bi-endian, just pretend it is. */
4418#if !defined (LITTLE_REMOTE_BREAKPOINT) && !defined (BIG_REMOTE_BREAKPOINT)
4419#define LITTLE_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
4420#define BIG_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
4421#endif
4422
4423static unsigned char big_break_insn[] = BIG_REMOTE_BREAKPOINT;
4424static unsigned char little_break_insn[] = LITTLE_REMOTE_BREAKPOINT;
4425
4426#endif /* REMOTE_BREAKPOINT */
4427
4428/* Insert a breakpoint on targets that don't have any better breakpoint
4429 support. We read the contents of the target location and stash it,
4430 then overwrite it with a breakpoint instruction. ADDR is the target
4431 location in the target machine. CONTENTS_CACHE is a pointer to
4432 memory allocated for saving the target contents. It is guaranteed
4433 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
4434 is accomplished via BREAKPOINT_MAX). */
4435
4436static int
fba45db2 4437remote_insert_breakpoint (CORE_ADDR addr, char *contents_cache)
c906108c
SS
4438{
4439#ifdef REMOTE_BREAKPOINT
4440 int val;
96baa820
JM
4441#endif
4442 int bp_size;
4443
d471ea57
AC
4444 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
4445 If it succeeds, then set the support to PACKET_ENABLE. If it
4446 fails, and the user has explicitly requested the Z support then
4447 report an error, otherwise, mark it disabled and go on. */
96baa820 4448
d471ea57 4449 if (remote_protocol_Z[Z_PACKET_SOFTWARE_BP].support != PACKET_DISABLE)
96baa820 4450 {
e514a9d6
JM
4451 char *buf = alloca (PBUFSIZ);
4452 char *p = buf;
96baa820
JM
4453
4454 addr = remote_address_masked (addr);
4455 *(p++) = 'Z';
4456 *(p++) = '0';
4457 *(p++) = ',';
4458 p += hexnumstr (p, (ULONGEST) addr);
4459 BREAKPOINT_FROM_PC (&addr, &bp_size);
4460 sprintf (p, ",%d", bp_size);
4461
4462 putpkt (buf);
c2d11a7d 4463 getpkt (buf, PBUFSIZ, 0);
96baa820 4464
d471ea57 4465 switch (packet_ok (buf, &remote_protocol_Z[Z_PACKET_SOFTWARE_BP]))
96baa820 4466 {
d471ea57
AC
4467 case PACKET_ERROR:
4468 return -1;
4469 case PACKET_OK:
4470 return 0;
4471 case PACKET_UNKNOWN:
4472 break;
96baa820
JM
4473 }
4474 }
c906108c 4475
96baa820 4476#ifdef REMOTE_BREAKPOINT
c906108c
SS
4477 val = target_read_memory (addr, contents_cache, sizeof big_break_insn);
4478
4479 if (val == 0)
4480 {
4481 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
4482 val = target_write_memory (addr, (char *) big_break_insn,
4483 sizeof big_break_insn);
4484 else
4485 val = target_write_memory (addr, (char *) little_break_insn,
4486 sizeof little_break_insn);
4487 }
4488
4489 return val;
4490#else
4491 return memory_insert_breakpoint (addr, contents_cache);
4492#endif /* REMOTE_BREAKPOINT */
4493}
4494
4495static int
fba45db2 4496remote_remove_breakpoint (CORE_ADDR addr, char *contents_cache)
c906108c 4497{
96baa820
JM
4498 int bp_size;
4499
d471ea57 4500 if (remote_protocol_Z[Z_PACKET_SOFTWARE_BP].support != PACKET_DISABLE)
96baa820 4501 {
e514a9d6
JM
4502 char *buf = alloca (PBUFSIZ);
4503 char *p = buf;
96baa820
JM
4504
4505 *(p++) = 'z';
4506 *(p++) = '0';
4507 *(p++) = ',';
4508
4509 addr = remote_address_masked (addr);
4510 p += hexnumstr (p, (ULONGEST) addr);
4511 BREAKPOINT_FROM_PC (&addr, &bp_size);
4512 sprintf (p, ",%d", bp_size);
4513
4514 putpkt (buf);
c2d11a7d 4515 getpkt (buf, PBUFSIZ, 0);
96baa820
JM
4516
4517 return (buf[0] == 'E');
4518 }
4519
c906108c
SS
4520#ifdef REMOTE_BREAKPOINT
4521 return target_write_memory (addr, contents_cache, sizeof big_break_insn);
4522#else
4523 return memory_remove_breakpoint (addr, contents_cache);
4524#endif /* REMOTE_BREAKPOINT */
4525}
4526
d471ea57
AC
4527static int
4528watchpoint_to_Z_packet (int type)
4529{
4530 switch (type)
4531 {
4532 case hw_write:
4533 return 2;
4534 break;
4535 case hw_read:
4536 return 3;
4537 break;
4538 case hw_access:
4539 return 4;
4540 break;
4541 default:
8e65ff28
AC
4542 internal_error (__FILE__, __LINE__,
4543 "hw_bp_to_z: bad watchpoint type %d", type);
d471ea57
AC
4544 }
4545}
4546
4547/* FIXME: This function should be static and a member of the remote
4548 target vector. */
4549
96baa820 4550int
fba45db2 4551remote_insert_watchpoint (CORE_ADDR addr, int len, int type)
96baa820 4552{
e514a9d6
JM
4553 char *buf = alloca (PBUFSIZ);
4554 char *p;
d471ea57 4555 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
96baa820 4556
d471ea57
AC
4557 if (remote_protocol_Z[packet].support == PACKET_DISABLE)
4558 error ("Can't set hardware watchpoints without the '%s' (%s) packet\n",
4559 remote_protocol_Z[packet].name,
4560 remote_protocol_Z[packet].title);
96baa820 4561
d471ea57 4562 sprintf (buf, "Z%x,", packet);
96baa820
JM
4563 p = strchr (buf, '\0');
4564 addr = remote_address_masked (addr);
4565 p += hexnumstr (p, (ULONGEST) addr);
d4f3574e 4566 sprintf (p, ",%x", len);
96baa820
JM
4567
4568 putpkt (buf);
c2d11a7d 4569 getpkt (buf, PBUFSIZ, 0);
96baa820 4570
d471ea57
AC
4571 switch (packet_ok (buf, &remote_protocol_Z[packet]))
4572 {
4573 case PACKET_ERROR:
4574 case PACKET_UNKNOWN:
4575 return -1;
4576 case PACKET_OK:
4577 return 0;
4578 }
8e65ff28
AC
4579 internal_error (__FILE__, __LINE__,
4580 "remote_insert_watchpoint: reached end of function");
96baa820
JM
4581}
4582
d471ea57
AC
4583/* FIXME: This function should be static and a member of the remote
4584 target vector. */
4585
96baa820 4586int
fba45db2 4587remote_remove_watchpoint (CORE_ADDR addr, int len, int type)
96baa820 4588{
e514a9d6
JM
4589 char *buf = alloca (PBUFSIZ);
4590 char *p;
d471ea57
AC
4591 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
4592
4593 if (remote_protocol_Z[packet].support == PACKET_DISABLE)
4594 error ("Can't clear hardware watchpoints without the '%s' (%s) packet\n",
4595 remote_protocol_Z[packet].name,
4596 remote_protocol_Z[packet].title);
96baa820 4597
d471ea57 4598 sprintf (buf, "z%x,", packet);
96baa820
JM
4599 p = strchr (buf, '\0');
4600 addr = remote_address_masked (addr);
4601 p += hexnumstr (p, (ULONGEST) addr);
d4f3574e 4602 sprintf (p, ",%x", len);
96baa820 4603 putpkt (buf);
c2d11a7d 4604 getpkt (buf, PBUFSIZ, 0);
96baa820 4605
d471ea57
AC
4606 switch (packet_ok (buf, &remote_protocol_Z[packet]))
4607 {
4608 case PACKET_ERROR:
4609 case PACKET_UNKNOWN:
4610 return -1;
4611 case PACKET_OK:
4612 return 0;
4613 }
8e65ff28
AC
4614 internal_error (__FILE__, __LINE__,
4615 "remote_remove_watchpoint: reached end of function");
96baa820
JM
4616}
4617
d471ea57
AC
4618/* FIXME: This function should be static and a member of the remote
4619 target vector. */
4620
96baa820 4621int
fba45db2 4622remote_insert_hw_breakpoint (CORE_ADDR addr, int len)
96baa820 4623{
e514a9d6
JM
4624 char *buf = alloca (PBUFSIZ);
4625 char *p = buf;
96baa820 4626
d471ea57
AC
4627 if (remote_protocol_Z[Z_PACKET_HARDWARE_BP].support == PACKET_DISABLE)
4628 error ("Can't set hardware breakpoint without the '%s' (%s) packet\n",
4629 remote_protocol_Z[Z_PACKET_HARDWARE_BP].name,
4630 remote_protocol_Z[Z_PACKET_HARDWARE_BP].title);
4631
96baa820
JM
4632 *(p++) = 'Z';
4633 *(p++) = '1';
4634 *(p++) = ',';
4635
4636 addr = remote_address_masked (addr);
4637 p += hexnumstr (p, (ULONGEST) addr);
ad6525fc 4638 sprintf (p, ",%x", len);
96baa820
JM
4639
4640 putpkt (buf);
c2d11a7d 4641 getpkt (buf, PBUFSIZ, 0);
96baa820 4642
d471ea57
AC
4643 switch (packet_ok (buf, &remote_protocol_Z[Z_PACKET_HARDWARE_BP]))
4644 {
4645 case PACKET_ERROR:
4646 case PACKET_UNKNOWN:
4647 return -1;
4648 case PACKET_OK:
4649 return 0;
4650 }
8e65ff28
AC
4651 internal_error (__FILE__, __LINE__,
4652 "remote_remove_watchpoint: reached end of function");
96baa820
JM
4653}
4654
d471ea57
AC
4655/* FIXME: This function should be static and a member of the remote
4656 target vector. */
4657
96baa820 4658int
fba45db2 4659remote_remove_hw_breakpoint (CORE_ADDR addr, int len)
96baa820 4660{
e514a9d6
JM
4661 char *buf = alloca (PBUFSIZ);
4662 char *p = buf;
96baa820 4663
d471ea57
AC
4664 if (remote_protocol_Z[Z_PACKET_HARDWARE_BP].support == PACKET_DISABLE)
4665 error ("Can't clear hardware breakpoint without the '%s' (%s) packet\n",
4666 remote_protocol_Z[Z_PACKET_HARDWARE_BP].name,
4667 remote_protocol_Z[Z_PACKET_HARDWARE_BP].title);
4668
96baa820
JM
4669 *(p++) = 'z';
4670 *(p++) = '1';
4671 *(p++) = ',';
4672
4673 addr = remote_address_masked (addr);
4674 p += hexnumstr (p, (ULONGEST) addr);
ad6525fc 4675 sprintf (p, ",%x", len);
96baa820
JM
4676
4677 putpkt(buf);
c2d11a7d 4678 getpkt (buf, PBUFSIZ, 0);
96baa820 4679
d471ea57
AC
4680 switch (packet_ok (buf, &remote_protocol_Z[Z_PACKET_HARDWARE_BP]))
4681 {
4682 case PACKET_ERROR:
4683 case PACKET_UNKNOWN:
4684 return -1;
4685 case PACKET_OK:
4686 return 0;
4687 }
8e65ff28
AC
4688 internal_error (__FILE__, __LINE__,
4689 "remote_remove_watchpoint: reached end of function");
96baa820 4690}
96baa820 4691
c906108c
SS
4692/* Some targets are only capable of doing downloads, and afterwards
4693 they switch to the remote serial protocol. This function provides
4694 a clean way to get from the download target to the remote target.
4695 It's basically just a wrapper so that we don't have to expose any
4696 of the internal workings of remote.c.
4697
4698 Prior to calling this routine, you should shutdown the current
4699 target code, else you will get the "A program is being debugged
4700 already..." message. Usually a call to pop_target() suffices. */
4701
4702void
fba45db2 4703push_remote_target (char *name, int from_tty)
c906108c
SS
4704{
4705 printf_filtered ("Switching to remote protocol\n");
4706 remote_open (name, from_tty);
4707}
4708
4709/* Other targets want to use the entire remote serial module but with
4710 certain remote_ops overridden. */
4711
4712void
fba45db2
KB
4713open_remote_target (char *name, int from_tty, struct target_ops *target,
4714 int extended_p)
c906108c
SS
4715{
4716 printf_filtered ("Selecting the %sremote protocol\n",
4717 (extended_p ? "extended-" : ""));
4718 remote_open_1 (name, from_tty, target, extended_p);
4719}
4720
4721/* Table used by the crc32 function to calcuate the checksum. */
4722
c5aa993b
JM
4723static unsigned long crc32_table[256] =
4724{0, 0};
c906108c
SS
4725
4726static unsigned long
fba45db2 4727crc32 (unsigned char *buf, int len, unsigned int crc)
c906108c 4728{
c5aa993b 4729 if (!crc32_table[1])
c906108c
SS
4730 {
4731 /* Initialize the CRC table and the decoding table. */
4732 int i, j;
4733 unsigned int c;
4734
4735 for (i = 0; i < 256; i++)
c5aa993b
JM
4736 {
4737 for (c = i << 24, j = 8; j > 0; --j)
4738 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
4739 crc32_table[i] = c;
4740 }
c906108c
SS
4741 }
4742
4743 while (len--)
4744 {
4745 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buf) & 255];
4746 buf++;
4747 }
4748 return crc;
4749}
4750
4751/* compare-sections command
4752
4753 With no arguments, compares each loadable section in the exec bfd
4754 with the same memory range on the target, and reports mismatches.
4755 Useful for verifying the image on the target against the exec file.
4756 Depends on the target understanding the new "qCRC:" request. */
4757
e514a9d6
JM
4758/* FIXME: cagney/1999-10-26: This command should be broken down into a
4759 target method (target verify memory) and generic version of the
4760 actual command. This will allow other high-level code (especially
4761 generic_load()) to make use of this target functionality. */
4762
c906108c 4763static void
fba45db2 4764compare_sections_command (char *args, int from_tty)
c906108c
SS
4765{
4766 asection *s;
4767 unsigned long host_crc, target_crc;
4768 extern bfd *exec_bfd;
4769 struct cleanup *old_chain;
085dd6e6
JM
4770 char *tmp;
4771 char *sectdata;
ce359b09 4772 const char *sectname;
085dd6e6 4773 char *buf = alloca (PBUFSIZ);
c906108c
SS
4774 bfd_size_type size;
4775 bfd_vma lma;
4776 int matched = 0;
4777 int mismatched = 0;
4778
4779 if (!exec_bfd)
4780 error ("command cannot be used without an exec file");
4781 if (!current_target.to_shortname ||
4782 strcmp (current_target.to_shortname, "remote") != 0)
4783 error ("command can only be used with remote target");
4784
c5aa993b 4785 for (s = exec_bfd->sections; s; s = s->next)
c906108c
SS
4786 {
4787 if (!(s->flags & SEC_LOAD))
c5aa993b 4788 continue; /* skip non-loadable section */
c906108c
SS
4789
4790 size = bfd_get_section_size_before_reloc (s);
4791 if (size == 0)
c5aa993b 4792 continue; /* skip zero-length section */
c906108c 4793
ce359b09 4794 sectname = bfd_get_section_name (exec_bfd, s);
c906108c 4795 if (args && strcmp (args, sectname) != 0)
c5aa993b 4796 continue; /* not the section selected by user */
c906108c 4797
c5aa993b 4798 matched = 1; /* do this section */
c906108c
SS
4799 lma = s->lma;
4800 /* FIXME: assumes lma can fit into long */
4801 sprintf (buf, "qCRC:%lx,%lx", (long) lma, (long) size);
4802 putpkt (buf);
4803
4804 /* be clever; compute the host_crc before waiting for target reply */
4805 sectdata = xmalloc (size);
b8c9b27d 4806 old_chain = make_cleanup (xfree, sectdata);
c906108c
SS
4807 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
4808 host_crc = crc32 ((unsigned char *) sectdata, size, 0xffffffff);
4809
c2d11a7d 4810 getpkt (buf, PBUFSIZ, 0);
c906108c
SS
4811 if (buf[0] == 'E')
4812 error ("target memory fault, section %s, range 0x%08x -- 0x%08x",
4813 sectname, lma, lma + size);
4814 if (buf[0] != 'C')
4815 error ("remote target does not support this operation");
4816
4817 for (target_crc = 0, tmp = &buf[1]; *tmp; tmp++)
4818 target_crc = target_crc * 16 + fromhex (*tmp);
4819
d4f3574e
SS
4820 printf_filtered ("Section %s, range 0x%s -- 0x%s: ",
4821 sectname, paddr (lma), paddr (lma + size));
c906108c
SS
4822 if (host_crc == target_crc)
4823 printf_filtered ("matched.\n");
4824 else
c5aa993b
JM
4825 {
4826 printf_filtered ("MIS-MATCHED!\n");
4827 mismatched++;
4828 }
c906108c
SS
4829
4830 do_cleanups (old_chain);
4831 }
4832 if (mismatched > 0)
4833 warning ("One or more sections of the remote executable does not match\n\
4834the loaded file\n");
4835 if (args && !matched)
4836 printf_filtered ("No loaded section named '%s'.\n", args);
4837}
4838
4839static int
fba45db2 4840remote_query (int query_type, char *buf, char *outbuf, int *bufsiz)
c906108c
SS
4841{
4842 int i;
085dd6e6 4843 char *buf2 = alloca (PBUFSIZ);
c906108c 4844 char *p2 = &buf2[0];
c906108c 4845
c5aa993b 4846 if (!bufsiz)
c906108c
SS
4847 error ("null pointer to remote bufer size specified");
4848
cce74817 4849 /* minimum outbuf size is PBUFSIZ - if bufsiz is not large enough let
c906108c
SS
4850 the caller know and return what the minimum size is */
4851 /* Note: a zero bufsiz can be used to query the minimum buffer size */
c5aa993b 4852 if (*bufsiz < PBUFSIZ)
c906108c
SS
4853 {
4854 *bufsiz = PBUFSIZ;
4855 return -1;
4856 }
4857
4858 /* except for querying the minimum buffer size, target must be open */
c5aa993b 4859 if (!remote_desc)
c906108c
SS
4860 error ("remote query is only available after target open");
4861
4862 /* we only take uppercase letters as query types, at least for now */
c5aa993b 4863 if ((query_type < 'A') || (query_type > 'Z'))
c906108c
SS
4864 error ("invalid remote query type");
4865
c5aa993b 4866 if (!buf)
c906108c
SS
4867 error ("null remote query specified");
4868
c5aa993b 4869 if (!outbuf)
c906108c
SS
4870 error ("remote query requires a buffer to receive data");
4871
4872 outbuf[0] = '\0';
4873
4874 *p2++ = 'q';
4875 *p2++ = query_type;
4876
4877 /* we used one buffer char for the remote protocol q command and another
4878 for the query type. As the remote protocol encapsulation uses 4 chars
4879 plus one extra in case we are debugging (remote_debug),
4880 we have PBUFZIZ - 7 left to pack the query string */
4881 i = 0;
c5aa993b 4882 while (buf[i] && (i < (PBUFSIZ - 8)))
c906108c
SS
4883 {
4884 /* bad caller may have sent forbidden characters */
c5aa993b
JM
4885 if ((!isprint (buf[i])) || (buf[i] == '$') || (buf[i] == '#'))
4886 error ("illegal characters in query string");
c906108c
SS
4887
4888 *p2++ = buf[i];
4889 i++;
4890 }
4891 *p2 = buf[i];
4892
c5aa993b 4893 if (buf[i])
c906108c
SS
4894 error ("query larger than available buffer");
4895
4896 i = putpkt (buf2);
c5aa993b
JM
4897 if (i < 0)
4898 return i;
c906108c 4899
c2d11a7d 4900 getpkt (outbuf, *bufsiz, 0);
c906108c
SS
4901
4902 return 0;
4903}
4904
96baa820
JM
4905static void
4906remote_rcmd (char *command,
d9fcf2fb 4907 struct ui_file *outbuf)
96baa820
JM
4908{
4909 int i;
4910 char *buf = alloca (PBUFSIZ);
4911 char *p = buf;
4912
4913 if (!remote_desc)
4914 error ("remote rcmd is only available after target open");
4915
7be570e7
JM
4916 /* Send a NULL command across as an empty command */
4917 if (command == NULL)
4918 command = "";
4919
96baa820
JM
4920 /* The query prefix */
4921 strcpy (buf, "qRcmd,");
4922 p = strchr (buf, '\0');
4923
4924 if ((strlen (buf) + strlen (command) * 2 + 8/*misc*/) > PBUFSIZ)
4925 error ("\"monitor\" command ``%s'' is too long\n", command);
4926
4927 /* Encode the actual command */
30559e10 4928 bin2hex (command, p, 0);
96baa820
JM
4929
4930 if (putpkt (buf) < 0)
4931 error ("Communication problem with target\n");
4932
4933 /* get/display the response */
4934 while (1)
4935 {
4936 /* XXX - see also tracepoint.c:remote_get_noisy_reply() */
4937 buf[0] = '\0';
c2d11a7d 4938 getpkt (buf, PBUFSIZ, 0);
96baa820
JM
4939 if (buf[0] == '\0')
4940 error ("Target does not support this command\n");
4941 if (buf[0] == 'O' && buf[1] != 'K')
4942 {
4943 remote_console_output (buf + 1); /* 'O' message from stub */
4944 continue;
4945 }
4946 if (strcmp (buf, "OK") == 0)
4947 break;
7be570e7
JM
4948 if (strlen (buf) == 3 && buf[0] == 'E'
4949 && isdigit (buf[1]) && isdigit (buf[2]))
4950 {
4951 error ("Protocol error with Rcmd");
4952 }
96baa820
JM
4953 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
4954 {
4955 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
4956 fputc_unfiltered (c, outbuf);
4957 }
4958 break;
4959 }
4960}
4961
c906108c 4962static void
fba45db2 4963packet_command (char *args, int from_tty)
c906108c 4964{
085dd6e6 4965 char *buf = alloca (PBUFSIZ);
c906108c 4966
c5aa993b 4967 if (!remote_desc)
c906108c
SS
4968 error ("command can only be used with remote target");
4969
c5aa993b 4970 if (!args)
c906108c
SS
4971 error ("remote-packet command requires packet text as argument");
4972
4973 puts_filtered ("sending: ");
4974 print_packet (args);
4975 puts_filtered ("\n");
4976 putpkt (args);
4977
c2d11a7d 4978 getpkt (buf, PBUFSIZ, 0);
c906108c
SS
4979 puts_filtered ("received: ");
4980 print_packet (buf);
4981 puts_filtered ("\n");
4982}
4983
4984#if 0
4985/* --------- UNIT_TEST for THREAD oriented PACKETS ------------------------- */
4986
a14ed312 4987static void display_thread_info (struct gdb_ext_thread_info *info);
c906108c 4988
a14ed312 4989static void threadset_test_cmd (char *cmd, int tty);
c906108c 4990
a14ed312 4991static void threadalive_test (char *cmd, int tty);
c906108c 4992
a14ed312 4993static void threadlist_test_cmd (char *cmd, int tty);
c906108c 4994
a14ed312 4995int get_and_display_threadinfo (threadref * ref);
c906108c 4996
a14ed312 4997static void threadinfo_test_cmd (char *cmd, int tty);
c906108c 4998
a14ed312 4999static int thread_display_step (threadref * ref, void *context);
c906108c 5000
a14ed312 5001static void threadlist_update_test_cmd (char *cmd, int tty);
c906108c 5002
a14ed312 5003static void init_remote_threadtests (void);
c906108c 5004
c5aa993b 5005#define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid */
c906108c
SS
5006
5007static void
fba45db2 5008threadset_test_cmd (char *cmd, int tty)
c906108c
SS
5009{
5010 int sample_thread = SAMPLE_THREAD;
5011
5012 printf_filtered ("Remote threadset test\n");
5013 set_thread (sample_thread, 1);
5014}
5015
5016
5017static void
fba45db2 5018threadalive_test (char *cmd, int tty)
c906108c
SS
5019{
5020 int sample_thread = SAMPLE_THREAD;
5021
39f77062 5022 if (remote_thread_alive (pid_to_ptid (sample_thread)))
c906108c
SS
5023 printf_filtered ("PASS: Thread alive test\n");
5024 else
5025 printf_filtered ("FAIL: Thread alive test\n");
5026}
5027
a14ed312 5028void output_threadid (char *title, threadref * ref);
c906108c
SS
5029
5030void
fba45db2 5031output_threadid (char *title, threadref *ref)
c906108c
SS
5032{
5033 char hexid[20];
5034
5035 pack_threadid (&hexid[0], ref); /* Convert threead id into hex */
5036 hexid[16] = 0;
5037 printf_filtered ("%s %s\n", title, (&hexid[0]));
5038}
5039
5040static void
fba45db2 5041threadlist_test_cmd (char *cmd, int tty)
c906108c
SS
5042{
5043 int startflag = 1;
5044 threadref nextthread;
5045 int done, result_count;
5046 threadref threadlist[3];
5047
5048 printf_filtered ("Remote Threadlist test\n");
5049 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
5050 &result_count, &threadlist[0]))
5051 printf_filtered ("FAIL: threadlist test\n");
5052 else
5053 {
5054 threadref *scan = threadlist;
5055 threadref *limit = scan + result_count;
5056
5057 while (scan < limit)
5058 output_threadid (" thread ", scan++);
5059 }
5060}
5061
5062void
fba45db2 5063display_thread_info (struct gdb_ext_thread_info *info)
c906108c
SS
5064{
5065 output_threadid ("Threadid: ", &info->threadid);
5066 printf_filtered ("Name: %s\n ", info->shortname);
5067 printf_filtered ("State: %s\n", info->display);
5068 printf_filtered ("other: %s\n\n", info->more_display);
5069}
5070
5071int
fba45db2 5072get_and_display_threadinfo (threadref *ref)
c906108c
SS
5073{
5074 int result;
5075 int set;
5076 struct gdb_ext_thread_info threadinfo;
5077
5078 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
5079 | TAG_MOREDISPLAY | TAG_DISPLAY;
5080 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
5081 display_thread_info (&threadinfo);
5082 return result;
5083}
5084
5085static void
fba45db2 5086threadinfo_test_cmd (char *cmd, int tty)
c906108c
SS
5087{
5088 int athread = SAMPLE_THREAD;
5089 threadref thread;
5090 int set;
5091
5092 int_to_threadref (&thread, athread);
5093 printf_filtered ("Remote Threadinfo test\n");
5094 if (!get_and_display_threadinfo (&thread))
5095 printf_filtered ("FAIL cannot get thread info\n");
5096}
5097
5098static int
fba45db2 5099thread_display_step (threadref *ref, void *context)
c906108c
SS
5100{
5101 /* output_threadid(" threadstep ",ref); *//* simple test */
5102 return get_and_display_threadinfo (ref);
5103}
5104
5105static void
fba45db2 5106threadlist_update_test_cmd (char *cmd, int tty)
c906108c
SS
5107{
5108 printf_filtered ("Remote Threadlist update test\n");
5109 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
5110}
5111
5112static void
5113init_remote_threadtests (void)
5114{
5115 add_com ("tlist", class_obscure, threadlist_test_cmd,
5116 "Fetch and print the remote list of thread identifiers, one pkt only");
5117 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
5118 "Fetch and display info about one thread");
5119 add_com ("tset", class_obscure, threadset_test_cmd,
5120 "Test setting to a different thread");
5121 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
5122 "Iterate through updating all remote thread info");
5123 add_com ("talive", class_obscure, threadalive_test,
5124 " Remote thread alive test ");
5125}
5126
5127#endif /* 0 */
5128
f3fb8c85
MS
5129/* Convert a thread ID to a string. Returns the string in a static
5130 buffer. */
5131
5132static char *
39f77062 5133remote_pid_to_str (ptid_t ptid)
f3fb8c85
MS
5134{
5135 static char buf[30];
5136
39f77062 5137 sprintf (buf, "Thread %d", PIDGET (ptid));
f3fb8c85
MS
5138 return buf;
5139}
5140
c906108c 5141static void
fba45db2 5142init_remote_ops (void)
c906108c 5143{
c5aa993b 5144 remote_ops.to_shortname = "remote";
c906108c 5145 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
c5aa993b 5146 remote_ops.to_doc =
c906108c 5147 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
0d06e24b
JM
5148Specify the serial device it is connected to\n\
5149(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
c5aa993b
JM
5150 remote_ops.to_open = remote_open;
5151 remote_ops.to_close = remote_close;
c906108c 5152 remote_ops.to_detach = remote_detach;
c5aa993b 5153 remote_ops.to_resume = remote_resume;
c906108c
SS
5154 remote_ops.to_wait = remote_wait;
5155 remote_ops.to_fetch_registers = remote_fetch_registers;
5156 remote_ops.to_store_registers = remote_store_registers;
5157 remote_ops.to_prepare_to_store = remote_prepare_to_store;
c5aa993b
JM
5158 remote_ops.to_xfer_memory = remote_xfer_memory;
5159 remote_ops.to_files_info = remote_files_info;
c906108c
SS
5160 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
5161 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
c5aa993b
JM
5162 remote_ops.to_kill = remote_kill;
5163 remote_ops.to_load = generic_load;
c906108c
SS
5164 remote_ops.to_mourn_inferior = remote_mourn;
5165 remote_ops.to_thread_alive = remote_thread_alive;
0f71a2f6 5166 remote_ops.to_find_new_threads = remote_threads_info;
9d1f7ab2 5167 remote_ops.to_extra_thread_info = remote_threads_extra_info;
f3fb8c85 5168 remote_ops.to_pid_to_str = remote_pid_to_str;
c906108c
SS
5169 remote_ops.to_stop = remote_stop;
5170 remote_ops.to_query = remote_query;
96baa820 5171 remote_ops.to_rcmd = remote_rcmd;
c906108c 5172 remote_ops.to_stratum = process_stratum;
c5aa993b
JM
5173 remote_ops.to_has_all_memory = 1;
5174 remote_ops.to_has_memory = 1;
5175 remote_ops.to_has_stack = 1;
5176 remote_ops.to_has_registers = 1;
5177 remote_ops.to_has_execution = 1;
5178 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
5179 remote_ops.to_magic = OPS_MAGIC;
c906108c
SS
5180}
5181
5182/* Set up the extended remote vector by making a copy of the standard
5183 remote vector and adding to it. */
5184
5185static void
fba45db2 5186init_extended_remote_ops (void)
c906108c
SS
5187{
5188 extended_remote_ops = remote_ops;
5189
0f71a2f6 5190 extended_remote_ops.to_shortname = "extended-remote";
c5aa993b 5191 extended_remote_ops.to_longname =
c906108c 5192 "Extended remote serial target in gdb-specific protocol";
c5aa993b 5193 extended_remote_ops.to_doc =
c906108c
SS
5194 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5195Specify the serial device it is connected to (e.g. /dev/ttya).",
c5aa993b 5196 extended_remote_ops.to_open = extended_remote_open;
c906108c
SS
5197 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
5198 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
0f71a2f6
JM
5199}
5200
5201/*
5202 * Command: info remote-process
5203 *
5204 * This implements Cisco's version of the "info proc" command.
5205 *
5206 * This query allows the target stub to return an arbitrary string
5207 * (or strings) giving arbitrary information about the target process.
5208 * This is optional; the target stub isn't required to implement it.
5209 *
5210 * Syntax: qfProcessInfo request first string
5211 * qsProcessInfo request subsequent string
5212 * reply: 'O'<hex-encoded-string>
5213 * 'l' last reply (empty)
5214 */
5215
5216static void
c2d11a7d 5217remote_info_process (char *args, int from_tty)
0f71a2f6 5218{
085dd6e6 5219 char *buf = alloca (PBUFSIZ);
0f71a2f6
JM
5220
5221 if (remote_desc == 0)
5222 error ("Command can only be used when connected to the remote target.");
5223
5224 putpkt ("qfProcessInfo");
c2d11a7d 5225 getpkt (buf, PBUFSIZ, 0);
0f71a2f6 5226 if (buf[0] == 0)
c5aa993b 5227 return; /* Silently: target does not support this feature. */
0f71a2f6
JM
5228
5229 if (buf[0] == 'E')
5230 error ("info proc: target error.");
5231
c5aa993b 5232 while (buf[0] == 'O') /* Capitol-O packet */
0f71a2f6
JM
5233 {
5234 remote_console_output (&buf[1]);
5235 putpkt ("qsProcessInfo");
c2d11a7d 5236 getpkt (buf, PBUFSIZ, 0);
0f71a2f6
JM
5237 }
5238}
5239
5240/*
5241 * Target Cisco
5242 */
5243
5244static void
c2d11a7d 5245remote_cisco_open (char *name, int from_tty)
0f71a2f6
JM
5246{
5247 if (name == 0)
5248 error (
c5aa993b 5249 "To open a remote debug connection, you need to specify what \n\
0f71a2f6
JM
5250device is attached to the remote system (e.g. host:port).");
5251
6426a772
JM
5252 /* See FIXME above */
5253 wait_forever_enabled_p = 1;
5254
0f71a2f6
JM
5255 target_preopen (from_tty);
5256
5257 unpush_target (&remote_cisco_ops);
5258
0f71a2f6
JM
5259 remote_desc = SERIAL_OPEN (name);
5260 if (!remote_desc)
5261 perror_with_name (name);
5262
5263 /*
5264 * If a baud rate was specified on the gdb command line it will
5265 * be greater than the initial value of -1. If it is, use it otherwise
5266 * default to 9600
5267 */
5268
5269 baud_rate = (baud_rate > 0) ? baud_rate : 9600;
c5aa993b 5270 if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
0f71a2f6
JM
5271 {
5272 SERIAL_CLOSE (remote_desc);
5273 perror_with_name (name);
5274 }
5275
5276 SERIAL_RAW (remote_desc);
5277
5278 /* If there is something sitting in the buffer we might take it as a
5279 response to a command, which would be bad. */
5280 SERIAL_FLUSH_INPUT (remote_desc);
5281
5282 if (from_tty)
5283 {
5284 puts_filtered ("Remote debugging using ");
5285 puts_filtered (name);
5286 puts_filtered ("\n");
5287 }
5288
5289 remote_cisco_mode = 1;
5290
5291 push_target (&remote_cisco_ops); /* Switch to using cisco target now */
5292
d471ea57 5293 init_all_packet_configs ();
0f71a2f6 5294
c5aa993b 5295 general_thread = -2;
0f71a2f6
JM
5296 continue_thread = -2;
5297
9d1f7ab2
MS
5298 /* Probe for ability to use "ThreadInfo" query, as required. */
5299 use_threadinfo_query = 1;
5300 use_threadextra_query = 1;
5301
0f71a2f6
JM
5302 /* Without this, some commands which require an active target (such
5303 as kill) won't work. This variable serves (at least) double duty
5304 as both the pid of the target process (if it has such), and as a
5305 flag indicating that a target is active. These functions should
5306 be split out into seperate variables, especially since GDB will
5307 someday have a notion of debugging several processes. */
39f77062 5308 inferior_ptid = pid_to_ptid (MAGIC_NULL_PID);
0f71a2f6
JM
5309
5310 /* Start the remote connection; if error (0), discard this target. */
5311
c5aa993b
JM
5312 if (!catch_errors (remote_start_remote_dummy, (char *) 0,
5313 "Couldn't establish connection to remote target\n",
0f71a2f6
JM
5314 RETURN_MASK_ALL))
5315 {
5316 pop_target ();
5317 return;
5318 }
5319}
5320
5321static void
c2d11a7d 5322remote_cisco_close (int quitting)
0f71a2f6
JM
5323{
5324 remote_cisco_mode = 0;
5325 remote_close (quitting);
5326}
5327
c5aa993b 5328static void
8ab86381 5329remote_cisco_mourn (void)
0f71a2f6
JM
5330{
5331 remote_mourn_1 (&remote_cisco_ops);
5332}
5333
c5aa993b
JM
5334enum
5335{
5336 READ_MORE,
5337 FATAL_ERROR,
5338 ENTER_DEBUG,
0f71a2f6 5339 DISCONNECT_TELNET
c5aa993b
JM
5340}
5341minitelnet_return;
0f71a2f6
JM
5342
5343/* shared between readsocket() and readtty() */
5344static char *tty_input;
5345
5346static int escape_count;
5347static int echo_check;
5348extern int quit_flag;
5349
5350static int
c2d11a7d 5351readsocket (void)
0f71a2f6
JM
5352{
5353 int data;
5354
5355 /* Loop until the socket doesn't have any more data */
5356
c5aa993b 5357 while ((data = readchar (0)) >= 0)
0f71a2f6
JM
5358 {
5359 /* Check for the escape sequence */
c5aa993b 5360 if (data == '|')
0f71a2f6
JM
5361 {
5362 /* If this is the fourth escape, get out */
c5aa993b 5363 if (++escape_count == 4)
0f71a2f6
JM
5364 {
5365 return ENTER_DEBUG;
5366 }
c5aa993b
JM
5367 else
5368 { /* This is a '|', but not the fourth in a row.
5369 Continue without echoing it. If it isn't actually
5370 one of four in a row, it'll be echoed later. */
0f71a2f6
JM
5371 continue;
5372 }
5373 }
c5aa993b
JM
5374 else
5375 /* Not a '|' */
5376 {
0f71a2f6
JM
5377 /* Ensure any pending '|'s are flushed. */
5378
c5aa993b
JM
5379 for (; escape_count > 0; escape_count--)
5380 putchar ('|');
0f71a2f6 5381 }
0f71a2f6 5382
c5aa993b
JM
5383 if (data == '\r') /* If this is a return character, */
5384 continue; /* - just supress it. */
5385
5386 if (echo_check != -1) /* Check for echo of user input. */
0f71a2f6
JM
5387 {
5388 if (tty_input[echo_check] == data)
5389 {
c5aa993b
JM
5390 echo_check++; /* Character matched user input: */
5391 continue; /* Continue without echoing it. */
0f71a2f6 5392 }
c5aa993b
JM
5393 else if ((data == '\n') && (tty_input[echo_check] == '\r'))
5394 { /* End of the line (and of echo checking). */
5395 echo_check = -1; /* No more echo supression */
5396 continue; /* Continue without echoing. */
0f71a2f6
JM
5397 }
5398 else
c5aa993b
JM
5399 { /* Failed check for echo of user input.
5400 We now have some suppressed output to flush! */
0f71a2f6
JM
5401 int j;
5402
c5aa993b 5403 for (j = 0; j < echo_check; j++)
0f71a2f6
JM
5404 putchar (tty_input[j]);
5405 echo_check = -1;
5406 }
5407 }
c5aa993b 5408 putchar (data); /* Default case: output the char. */
0f71a2f6
JM
5409 }
5410
c5aa993b
JM
5411 if (data == SERIAL_TIMEOUT) /* Timeout returned from readchar. */
5412 return READ_MORE; /* Try to read some more */
5413 else
5414 return FATAL_ERROR; /* Trouble, bail out */
0f71a2f6
JM
5415}
5416
5417static int
c2d11a7d 5418readtty (void)
0f71a2f6 5419{
0f71a2f6
JM
5420 int tty_bytecount;
5421
5422 /* First, read a buffer full from the terminal */
5423 tty_bytecount = read (fileno (stdin), tty_input, sizeof (tty_input) - 1);
c5aa993b 5424 if (tty_bytecount == -1)
0f71a2f6
JM
5425 {
5426 perror ("readtty: read failed");
5427 return FATAL_ERROR;
5428 }
5429
5430 /* Remove a quoted newline. */
5431 if (tty_input[tty_bytecount - 1] == '\n' &&
5432 tty_input[tty_bytecount - 2] == '\\') /* line ending in backslash */
5433 {
c5aa993b
JM
5434 tty_input[--tty_bytecount] = 0; /* remove newline */
5435 tty_input[--tty_bytecount] = 0; /* remove backslash */
0f71a2f6
JM
5436 }
5437
5438 /* Turn trailing newlines into returns */
5439 if (tty_input[tty_bytecount - 1] == '\n')
c5aa993b 5440 tty_input[tty_bytecount - 1] = '\r';
0f71a2f6
JM
5441
5442 /* If the line consists of a ~, enter debugging mode. */
5443 if ((tty_input[0] == '~') && (tty_bytecount == 2))
5444 return ENTER_DEBUG;
5445
5446 /* Make this a zero terminated string and write it out */
5447 tty_input[tty_bytecount] = 0;
c5aa993b 5448 if (SERIAL_WRITE (remote_desc, tty_input, tty_bytecount))
0f71a2f6
JM
5449 {
5450 perror_with_name ("readtty: write failed");
5451 return FATAL_ERROR;
5452 }
5453
5454 return READ_MORE;
5455}
5456
5457static int
c2d11a7d 5458minitelnet (void)
0f71a2f6
JM
5459{
5460 fd_set input; /* file descriptors for select */
c5aa993b
JM
5461 int tablesize; /* max number of FDs for select */
5462 int status;
5463 int quit_count = 0;
0f71a2f6
JM
5464
5465 extern int escape_count; /* global shared by readsocket */
5466 extern int echo_check; /* ditto */
5467
5468 escape_count = 0;
c5aa993b 5469 echo_check = -1;
0f71a2f6
JM
5470
5471 tablesize = 8 * sizeof (input);
5472
c5aa993b 5473 for (;;)
0f71a2f6
JM
5474 {
5475 /* Check for anything from our socket - doesn't block. Note that
c5aa993b
JM
5476 this must be done *before* the select as there may be
5477 buffered I/O waiting to be processed. */
0f71a2f6 5478
c5aa993b 5479 if ((status = readsocket ()) == FATAL_ERROR)
0f71a2f6
JM
5480 {
5481 error ("Debugging terminated by communications error");
5482 }
c5aa993b 5483 else if (status != READ_MORE)
0f71a2f6
JM
5484 {
5485 return (status);
5486 }
5487
c5aa993b 5488 fflush (stdout); /* Flush output before blocking */
0f71a2f6
JM
5489
5490 /* Now block on more socket input or TTY input */
c5aa993b 5491
0f71a2f6 5492 FD_ZERO (&input);
c5aa993b 5493 FD_SET (fileno (stdin), &input);
c2c6d25f 5494 FD_SET (DEPRECATED_SERIAL_FD (remote_desc), &input);
0f71a2f6
JM
5495
5496 status = select (tablesize, &input, 0, 0, 0);
c5aa993b 5497 if ((status == -1) && (errno != EINTR))
0f71a2f6
JM
5498 {
5499 error ("Communications error on select %d", errno);
5500 }
5501
5502 /* Handle Control-C typed */
5503
c5aa993b 5504 if (quit_flag)
0f71a2f6
JM
5505 {
5506 if ((++quit_count) == 2)
5507 {
5508 if (query ("Interrupt GDB? "))
5509 {
5510 printf_filtered ("Interrupted by user.\n");
5511 return_to_top_level (RETURN_QUIT);
5512 }
5513 quit_count = 0;
5514 }
5515 quit_flag = 0;
5516
5517 if (remote_break)
5518 SERIAL_SEND_BREAK (remote_desc);
5519 else
5520 SERIAL_WRITE (remote_desc, "\003", 1);
5521
5522 continue;
5523 }
5524
5525 /* Handle console input */
5526
c5aa993b 5527 if (FD_ISSET (fileno (stdin), &input))
0f71a2f6
JM
5528 {
5529 quit_count = 0;
5530 echo_check = 0;
5531 status = readtty ();
5532 if (status == READ_MORE)
5533 continue;
5534
5535 return status; /* telnet session ended */
5536 }
5537 }
5538}
5539
39f77062
KB
5540static ptid_t
5541remote_cisco_wait (ptid_t ptid, struct target_waitstatus *status)
0f71a2f6 5542{
c5aa993b 5543 if (minitelnet () != ENTER_DEBUG)
0f71a2f6
JM
5544 {
5545 error ("Debugging session terminated by protocol error");
5546 }
5547 putpkt ("?");
39f77062 5548 return remote_wait (ptid, status);
0f71a2f6
JM
5549}
5550
5551static void
fba45db2 5552init_remote_cisco_ops (void)
0f71a2f6
JM
5553{
5554 remote_cisco_ops.to_shortname = "cisco";
c5aa993b
JM
5555 remote_cisco_ops.to_longname = "Remote serial target in cisco-specific protocol";
5556 remote_cisco_ops.to_doc =
0f71a2f6
JM
5557 "Use a remote machine via TCP, using a cisco-specific protocol.\n\
5558Specify the serial device it is connected to (e.g. host:2020).";
c5aa993b
JM
5559 remote_cisco_ops.to_open = remote_cisco_open;
5560 remote_cisco_ops.to_close = remote_cisco_close;
5561 remote_cisco_ops.to_detach = remote_detach;
5562 remote_cisco_ops.to_resume = remote_resume;
5563 remote_cisco_ops.to_wait = remote_cisco_wait;
5564 remote_cisco_ops.to_fetch_registers = remote_fetch_registers;
5565 remote_cisco_ops.to_store_registers = remote_store_registers;
5566 remote_cisco_ops.to_prepare_to_store = remote_prepare_to_store;
5567 remote_cisco_ops.to_xfer_memory = remote_xfer_memory;
5568 remote_cisco_ops.to_files_info = remote_files_info;
0f71a2f6
JM
5569 remote_cisco_ops.to_insert_breakpoint = remote_insert_breakpoint;
5570 remote_cisco_ops.to_remove_breakpoint = remote_remove_breakpoint;
c5aa993b
JM
5571 remote_cisco_ops.to_kill = remote_kill;
5572 remote_cisco_ops.to_load = generic_load;
5573 remote_cisco_ops.to_mourn_inferior = remote_cisco_mourn;
5574 remote_cisco_ops.to_thread_alive = remote_thread_alive;
5575 remote_cisco_ops.to_find_new_threads = remote_threads_info;
9d1f7ab2 5576 remote_ops.to_extra_thread_info = remote_threads_extra_info;
c5aa993b
JM
5577 remote_cisco_ops.to_stratum = process_stratum;
5578 remote_cisco_ops.to_has_all_memory = 1;
5579 remote_cisco_ops.to_has_memory = 1;
5580 remote_cisco_ops.to_has_stack = 1;
5581 remote_cisco_ops.to_has_registers = 1;
5582 remote_cisco_ops.to_has_execution = 1;
5583 remote_cisco_ops.to_magic = OPS_MAGIC;
0f71a2f6
JM
5584}
5585
6426a772
JM
5586static int
5587remote_can_async_p (void)
5588{
5589 /* We're async whenever the serial device is. */
ed9a39eb 5590 return (current_target.to_async_mask_value) && SERIAL_CAN_ASYNC_P (remote_desc);
6426a772
JM
5591}
5592
5593static int
5594remote_is_async_p (void)
5595{
5596 /* We're async whenever the serial device is. */
ed9a39eb 5597 return (current_target.to_async_mask_value) && SERIAL_IS_ASYNC_P (remote_desc);
6426a772
JM
5598}
5599
2acceee2
JM
5600/* Pass the SERIAL event on and up to the client. One day this code
5601 will be able to delay notifying the client of an event until the
5602 point where an entire packet has been received. */
5603
5604static void (*async_client_callback) (enum inferior_event_type event_type, void *context);
5605static void *async_client_context;
5606static serial_event_ftype remote_async_serial_handler;
5607
6426a772 5608static void
2acceee2 5609remote_async_serial_handler (serial_t scb, void *context)
6426a772 5610{
2acceee2
JM
5611 /* Don't propogate error information up to the client. Instead let
5612 the client find out about the error by querying the target. */
5613 async_client_callback (INF_REG_EVENT, async_client_context);
5614}
5615
5616static void
5617remote_async (void (*callback) (enum inferior_event_type event_type, void *context), void *context)
5618{
ed9a39eb 5619 if (current_target.to_async_mask_value == 0)
8e65ff28
AC
5620 internal_error (__FILE__, __LINE__,
5621 "Calling remote_async when async is masked");
ed9a39eb 5622
2acceee2
JM
5623 if (callback != NULL)
5624 {
5625 SERIAL_ASYNC (remote_desc, remote_async_serial_handler, NULL);
5626 async_client_callback = callback;
5627 async_client_context = context;
5628 }
5629 else
5630 SERIAL_ASYNC (remote_desc, NULL, NULL);
6426a772
JM
5631}
5632
43ff13b4
JM
5633/* Target async and target extended-async.
5634
5635 This are temporary targets, until it is all tested. Eventually
5636 async support will be incorporated int the usual 'remote'
5637 target. */
5638
5639static void
c2d11a7d 5640init_remote_async_ops (void)
43ff13b4
JM
5641{
5642 remote_async_ops.to_shortname = "async";
c5aa993b
JM
5643 remote_async_ops.to_longname = "Remote serial target in async version of the gdb-specific protocol";
5644 remote_async_ops.to_doc =
43ff13b4
JM
5645 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5646Specify the serial device it is connected to (e.g. /dev/ttya).";
c5aa993b
JM
5647 remote_async_ops.to_open = remote_async_open;
5648 remote_async_ops.to_close = remote_close;
5649 remote_async_ops.to_detach = remote_async_detach;
5650 remote_async_ops.to_resume = remote_async_resume;
5651 remote_async_ops.to_wait = remote_async_wait;
5652 remote_async_ops.to_fetch_registers = remote_fetch_registers;
5653 remote_async_ops.to_store_registers = remote_store_registers;
5654 remote_async_ops.to_prepare_to_store = remote_prepare_to_store;
5655 remote_async_ops.to_xfer_memory = remote_xfer_memory;
5656 remote_async_ops.to_files_info = remote_files_info;
43ff13b4
JM
5657 remote_async_ops.to_insert_breakpoint = remote_insert_breakpoint;
5658 remote_async_ops.to_remove_breakpoint = remote_remove_breakpoint;
6426a772
JM
5659 remote_async_ops.to_terminal_inferior = remote_async_terminal_inferior;
5660 remote_async_ops.to_terminal_ours = remote_async_terminal_ours;
c5aa993b
JM
5661 remote_async_ops.to_kill = remote_async_kill;
5662 remote_async_ops.to_load = generic_load;
53a5351d 5663 remote_async_ops.to_mourn_inferior = remote_async_mourn;
c5aa993b
JM
5664 remote_async_ops.to_thread_alive = remote_thread_alive;
5665 remote_async_ops.to_find_new_threads = remote_threads_info;
9d1f7ab2 5666 remote_ops.to_extra_thread_info = remote_threads_extra_info;
43ff13b4
JM
5667 remote_async_ops.to_stop = remote_stop;
5668 remote_async_ops.to_query = remote_query;
96baa820 5669 remote_async_ops.to_rcmd = remote_rcmd;
c5aa993b
JM
5670 remote_async_ops.to_stratum = process_stratum;
5671 remote_async_ops.to_has_all_memory = 1;
5672 remote_async_ops.to_has_memory = 1;
5673 remote_async_ops.to_has_stack = 1;
5674 remote_async_ops.to_has_registers = 1;
5675 remote_async_ops.to_has_execution = 1;
5676 remote_async_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
6426a772
JM
5677 remote_async_ops.to_can_async_p = remote_can_async_p;
5678 remote_async_ops.to_is_async_p = remote_is_async_p;
5679 remote_async_ops.to_async = remote_async;
ed9a39eb 5680 remote_async_ops.to_async_mask_value = 1;
c5aa993b 5681 remote_async_ops.to_magic = OPS_MAGIC;
43ff13b4
JM
5682}
5683
5684/* Set up the async extended remote vector by making a copy of the standard
5685 remote vector and adding to it. */
5686
5687static void
c2d11a7d 5688init_extended_async_remote_ops (void)
43ff13b4
JM
5689{
5690 extended_async_remote_ops = remote_async_ops;
5691
5692 extended_async_remote_ops.to_shortname = "extended-async";
c5aa993b 5693 extended_async_remote_ops.to_longname =
43ff13b4 5694 "Extended remote serial target in async gdb-specific protocol";
c5aa993b 5695 extended_async_remote_ops.to_doc =
43ff13b4
JM
5696 "Use a remote computer via a serial line, using an async gdb-specific protocol.\n\
5697Specify the serial device it is connected to (e.g. /dev/ttya).",
c5aa993b 5698 extended_async_remote_ops.to_open = extended_remote_async_open;
43ff13b4
JM
5699 extended_async_remote_ops.to_create_inferior = extended_remote_async_create_inferior;
5700 extended_async_remote_ops.to_mourn_inferior = extended_remote_mourn;
5701}
5702
5a2468f5 5703static void
c2d11a7d 5704set_remote_cmd (char *args, int from_tty)
5a2468f5
JM
5705{
5706
5707}
5708
d471ea57
AC
5709static void
5710show_remote_cmd (char *args, int from_tty)
5711{
44eaed12 5712
d471ea57 5713 show_remote_protocol_Z_packet_cmd (args, from_tty);
44eaed12
C
5714 show_remote_protocol_e_packet_cmd (args, from_tty);
5715 show_remote_protocol_E_packet_cmd (args, from_tty);
d471ea57
AC
5716 show_remote_protocol_P_packet_cmd (args, from_tty);
5717 show_remote_protocol_binary_download_cmd (args, from_tty);
5718}
5a2468f5 5719
0f71a2f6 5720static void
fba45db2 5721build_remote_gdbarch_data (void)
0f71a2f6 5722{
11cf8741
JM
5723 build_remote_packet_sizes ();
5724
5725 /* Cisco stuff */
0f71a2f6 5726 tty_input = xmalloc (PBUFSIZ);
d696208f 5727 remote_address_size = TARGET_ADDR_BIT;
0f71a2f6
JM
5728}
5729
c906108c 5730void
fba45db2 5731_initialize_remote (void)
c906108c 5732{
5a2468f5
JM
5733 static struct cmd_list_element *remote_set_cmdlist;
5734 static struct cmd_list_element *remote_show_cmdlist;
11cf8741 5735 struct cmd_list_element *tmpcmd;
5a2468f5 5736
0f71a2f6
JM
5737 /* architecture specific data */
5738 build_remote_gdbarch_data ();
5739 register_gdbarch_swap (&tty_input, sizeof (&tty_input), NULL);
11cf8741
JM
5740 register_remote_packet_sizes ();
5741 register_gdbarch_swap (&remote_address_size,
5742 sizeof (&remote_address_size), NULL);
0f71a2f6
JM
5743 register_gdbarch_swap (NULL, 0, build_remote_gdbarch_data);
5744
c906108c
SS
5745 init_remote_ops ();
5746 add_target (&remote_ops);
5747
5748 init_extended_remote_ops ();
5749 add_target (&extended_remote_ops);
cce74817 5750
43ff13b4
JM
5751 init_remote_async_ops ();
5752 add_target (&remote_async_ops);
5753
5754 init_extended_async_remote_ops ();
5755 add_target (&extended_async_remote_ops);
5756
0f71a2f6
JM
5757 init_remote_cisco_ops ();
5758 add_target (&remote_cisco_ops);
5759
c906108c
SS
5760#if 0
5761 init_remote_threadtests ();
5762#endif
5763
d471ea57
AC
5764 /* set/show remote ... */
5765
5a2468f5
JM
5766 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, "\
5767Remote protocol specific variables\n\
5768Configure various remote-protocol specific variables such as\n\
5769the packets being used",
cff3e48b 5770 &remote_set_cmdlist, "set remote ",
5a2468f5 5771 0/*allow-unknown*/, &setlist);
d471ea57 5772 add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, "\
5a2468f5
JM
5773Remote protocol specific variables\n\
5774Configure various remote-protocol specific variables such as\n\
5775the packets being used",
cff3e48b 5776 &remote_show_cmdlist, "show remote ",
5a2468f5
JM
5777 0/*allow-unknown*/, &showlist);
5778
c5aa993b 5779 add_cmd ("compare-sections", class_obscure, compare_sections_command,
c906108c 5780 "Compare section data on target to the exec file.\n\
c5aa993b 5781Argument is a single section name (default: all loaded sections).",
c906108c
SS
5782 &cmdlist);
5783
5784 add_cmd ("packet", class_maintenance, packet_command,
5785 "Send an arbitrary packet to a remote target.\n\
5786 maintenance packet TEXT\n\
5787If GDB is talking to an inferior via the GDB serial protocol, then\n\
5788this command sends the string TEXT to the inferior, and displays the\n\
5789response packet. GDB supplies the initial `$' character, and the\n\
5790terminating `#' character and checksum.",
5791 &maintenancelist);
5792
c5aa993b 5793 add_show_from_set
c906108c 5794 (add_set_cmd ("remotebreak", no_class,
adf40b2e 5795 var_boolean, (char *) &remote_break,
c906108c
SS
5796 "Set whether to send break if interrupted.\n",
5797 &setlist),
5798 &showlist);
5799
11cf8741
JM
5800 /* Install commands for configuring memory read/write packets. */
5801
5802 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size,
5803 "Set the maximum number of bytes per memory write packet (deprecated).\n",
5804 &setlist);
4ad5b0f7 5805 add_cmd ("remotewritesize", no_class, show_memory_write_packet_size,
11cf8741
JM
5806 "Show the maximum number of bytes per memory write packet (deprecated).\n",
5807 &showlist);
5808 add_cmd ("memory-write-packet-size", no_class,
5809 set_memory_write_packet_size,
5810 "Set the maximum number of bytes per memory-write packet.\n"
5811 "Specify the number of bytes in a packet or 0 (zero) for the\n"
5812 "default packet size. The actual limit is further reduced\n"
5813 "dependent on the target. Specify ``fixed'' to disable the\n"
5814 "further restriction and ``limit'' to enable that restriction\n",
5815 &remote_set_cmdlist);
5816 add_cmd ("memory-read-packet-size", no_class,
5817 set_memory_read_packet_size,
5818 "Set the maximum number of bytes per memory-read packet.\n"
5819 "Specify the number of bytes in a packet or 0 (zero) for the\n"
5820 "default packet size. The actual limit is further reduced\n"
5821 "dependent on the target. Specify ``fixed'' to disable the\n"
5822 "further restriction and ``limit'' to enable that restriction\n",
5823 &remote_set_cmdlist);
5824 add_cmd ("memory-write-packet-size", no_class,
5825 show_memory_write_packet_size,
5826 "Show the maximum number of bytes per memory-write packet.\n",
5827 &remote_show_cmdlist);
5828 add_cmd ("memory-read-packet-size", no_class,
5829 show_memory_read_packet_size,
5830 "Show the maximum number of bytes per memory-read packet.\n",
5831 &remote_show_cmdlist);
c906108c 5832
c5aa993b 5833 add_show_from_set
c906108c 5834 (add_set_cmd ("remoteaddresssize", class_obscure,
c5aa993b 5835 var_integer, (char *) &remote_address_size,
c906108c
SS
5836 "Set the maximum size of the address (in bits) \
5837in a memory packet.\n",
5838 &setlist),
c5aa993b 5839 &showlist);
c906108c 5840
96baa820
JM
5841 add_packet_config_cmd (&remote_protocol_binary_download,
5842 "X", "binary-download",
5843 set_remote_protocol_binary_download_cmd,
5844 show_remote_protocol_binary_download_cmd,
d471ea57
AC
5845 &remote_set_cmdlist, &remote_show_cmdlist,
5846 1);
96baa820
JM
5847#if 0
5848 /* XXXX - should ``set remotebinarydownload'' be retained for
5849 compatibility. */
c5aa993b 5850 add_show_from_set
b83266a0
SS
5851 (add_set_cmd ("remotebinarydownload", no_class,
5852 var_boolean, (char *) &remote_binary_download,
5853 "Set binary downloads.\n", &setlist),
5854 &showlist);
96baa820 5855#endif
0f71a2f6
JM
5856
5857 add_info ("remote-process", remote_info_process,
5858 "Query the remote system for process info.");
5859
44eaed12
C
5860 add_packet_config_cmd (&remote_protocol_e,
5861 "e", "step-over-range",
5862 set_remote_protocol_e_packet_cmd,
5863 show_remote_protocol_e_packet_cmd,
5864 &remote_set_cmdlist, &remote_show_cmdlist,
5865 0);
5866
5867 add_packet_config_cmd (&remote_protocol_E,
5868 "E", "step-over-range-w-signal",
5869 set_remote_protocol_E_packet_cmd,
5870 show_remote_protocol_E_packet_cmd,
5871 &remote_set_cmdlist, &remote_show_cmdlist,
5872 0);
5873
d471ea57
AC
5874 add_packet_config_cmd (&remote_protocol_P,
5875 "P", "set-register",
5a2468f5
JM
5876 set_remote_protocol_P_packet_cmd,
5877 show_remote_protocol_P_packet_cmd,
d471ea57
AC
5878 &remote_set_cmdlist, &remote_show_cmdlist,
5879 1);
5880
5881 add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_SOFTWARE_BP],
5882 "Z0", "software-breakpoint",
5883 set_remote_protocol_Z_software_bp_packet_cmd,
5884 show_remote_protocol_Z_software_bp_packet_cmd,
5885 &remote_set_cmdlist, &remote_show_cmdlist,
5886 0);
5887
5888 add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_HARDWARE_BP],
5889 "Z1", "hardware-breakpoint",
5890 set_remote_protocol_Z_hardware_bp_packet_cmd,
5891 show_remote_protocol_Z_hardware_bp_packet_cmd,
5892 &remote_set_cmdlist, &remote_show_cmdlist,
5893 0);
5894
5895 add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_WRITE_WP],
5896 "Z2", "write-watchpoint",
5897 set_remote_protocol_Z_write_wp_packet_cmd,
5898 show_remote_protocol_Z_write_wp_packet_cmd,
5899 &remote_set_cmdlist, &remote_show_cmdlist,
5900 0);
5901
5902 add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_READ_WP],
5903 "Z3", "read-watchpoint",
5904 set_remote_protocol_Z_read_wp_packet_cmd,
5905 show_remote_protocol_Z_read_wp_packet_cmd,
5906 &remote_set_cmdlist, &remote_show_cmdlist,
5907 0);
5908
5909 add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_ACCESS_WP],
5910 "Z4", "access-watchpoint",
5911 set_remote_protocol_Z_access_wp_packet_cmd,
5912 show_remote_protocol_Z_access_wp_packet_cmd,
5913 &remote_set_cmdlist, &remote_show_cmdlist,
5914 0);
5915
5916 /* Keep the old ``set remote Z-packet ...'' working. */
5917 tmpcmd = add_set_auto_boolean_cmd ("Z-packet", class_obscure,
5918 &remote_Z_packet_detect,
5919 "\
5920Set use of remote protocol `Z' packets", &remote_set_cmdlist);
5921 tmpcmd->function.sfunc = set_remote_protocol_Z_packet_cmd;
5922 add_cmd ("Z-packet", class_obscure, show_remote_protocol_Z_packet_cmd,
5923 "Show use of remote protocol `Z' packets ",
5924 &remote_show_cmdlist);
c906108c 5925}