119370Spst/* Remote target communications for serial-line targets in custom GDB protocol
219370Spst
398948Sobrien   Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4130809Smarcel   1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
5130809Smarcel   Free Software Foundation, Inc.
619370Spst
798948Sobrien   This file is part of GDB.
819370Spst
998948Sobrien   This program is free software; you can redistribute it and/or modify
1098948Sobrien   it under the terms of the GNU General Public License as published by
1198948Sobrien   the Free Software Foundation; either version 2 of the License, or
1298948Sobrien   (at your option) any later version.
1319370Spst
1498948Sobrien   This program is distributed in the hope that it will be useful,
1598948Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1698948Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1798948Sobrien   GNU General Public License for more details.
1819370Spst
1998948Sobrien   You should have received a copy of the GNU General Public License
2098948Sobrien   along with this program; if not, write to the Free Software
2198948Sobrien   Foundation, Inc., 59 Temple Place - Suite 330,
2298948Sobrien   Boston, MA 02111-1307, USA.  */
2319370Spst
2498948Sobrien/* See the GDB User Guide for details of the GDB remote protocol. */
2519370Spst
2619370Spst#include "defs.h"
2719370Spst#include "gdb_string.h"
2846289Sdfr#include <ctype.h>
2919370Spst#include <fcntl.h>
3019370Spst#include "inferior.h"
3119370Spst#include "bfd.h"
3219370Spst#include "symfile.h"
3319370Spst#include "target.h"
3498948Sobrien/*#include "terminal.h" */
3519370Spst#include "gdbcmd.h"
3619370Spst#include "objfiles.h"
3719370Spst#include "gdb-stabs.h"
3846289Sdfr#include "gdbthread.h"
3998948Sobrien#include "remote.h"
4098948Sobrien#include "regcache.h"
4198948Sobrien#include "value.h"
4298948Sobrien#include "gdb_assert.h"
4319370Spst
4498948Sobrien#include <ctype.h>
4598948Sobrien#include <sys/time.h>
4619370Spst#ifdef USG
4719370Spst#include <sys/types.h>
4819370Spst#endif
4919370Spst
5098948Sobrien#include "event-loop.h"
5198948Sobrien#include "event-top.h"
5298948Sobrien#include "inf-loop.h"
5398948Sobrien
5419370Spst#include <signal.h>
5519370Spst#include "serial.h"
5619370Spst
5798948Sobrien#include "gdbcore.h" /* for exec_bfd */
5898948Sobrien
59130809Smarcel#include "remote-fileio.h"
60130809Smarcel
6119370Spst/* Prototypes for local functions */
6298948Sobrienstatic void cleanup_sigint_signal_handler (void *dummy);
6398948Sobrienstatic void initialize_sigint_signal_handler (void);
6498948Sobrienstatic int getpkt_sane (char *buf, long sizeof_buf, int forever);
6519370Spst
6698948Sobrienstatic void handle_remote_sigint (int);
6798948Sobrienstatic void handle_remote_sigint_twice (int);
6898948Sobrienstatic void async_remote_interrupt (gdb_client_data);
6998948Sobrienvoid async_remote_interrupt_twice (gdb_client_data);
7019370Spst
7198948Sobrienstatic void build_remote_gdbarch_data (void);
7219370Spst
7398948Sobrienstatic void remote_files_info (struct target_ops *ignore);
7419370Spst
7598948Sobrienstatic int remote_xfer_memory (CORE_ADDR memaddr, char *myaddr,
7698948Sobrien			       int len, int should_write,
7798948Sobrien			       struct mem_attrib *attrib,
7898948Sobrien			       struct target_ops *target);
7919370Spst
8098948Sobrienstatic void remote_prepare_to_store (void);
8119370Spst
8298948Sobrienstatic void remote_fetch_registers (int regno);
8319370Spst
8498948Sobrienstatic void remote_resume (ptid_t ptid, int step,
8598948Sobrien                           enum target_signal siggnal);
8698948Sobrienstatic void remote_async_resume (ptid_t ptid, int step,
8798948Sobrien				 enum target_signal siggnal);
88130809Smarcelstatic int remote_start_remote (struct ui_out *uiout, void *dummy);
8919370Spst
9098948Sobrienstatic void remote_open (char *name, int from_tty);
9198948Sobrienstatic void remote_async_open (char *name, int from_tty);
9219370Spst
9398948Sobrienstatic void extended_remote_open (char *name, int from_tty);
9498948Sobrienstatic void extended_remote_async_open (char *name, int from_tty);
9519370Spst
96130809Smarcelstatic void remote_open_1 (char *, int, struct target_ops *, int extended_p,
97130809Smarcel			   int async_p);
9819370Spst
9998948Sobrienstatic void remote_close (int quitting);
10019370Spst
10198948Sobrienstatic void remote_store_registers (int regno);
10219370Spst
10398948Sobrienstatic void remote_mourn (void);
10498948Sobrienstatic void remote_async_mourn (void);
10519370Spst
10698948Sobrienstatic void extended_remote_restart (void);
10719370Spst
10898948Sobrienstatic void extended_remote_mourn (void);
10919370Spst
11098948Sobrienstatic void extended_remote_create_inferior (char *, char *, char **);
11198948Sobrienstatic void extended_remote_async_create_inferior (char *, char *, char **);
11219370Spst
11398948Sobrienstatic void remote_mourn_1 (struct target_ops *);
11419370Spst
11598948Sobrienstatic void remote_send (char *buf, long sizeof_buf);
11619370Spst
11798948Sobrienstatic int readchar (int timeout);
11819370Spst
11998948Sobrienstatic ptid_t remote_wait (ptid_t ptid,
12098948Sobrien                                 struct target_waitstatus *status);
12198948Sobrienstatic ptid_t remote_async_wait (ptid_t ptid,
12298948Sobrien                                       struct target_waitstatus *status);
12319370Spst
12498948Sobrienstatic void remote_kill (void);
12598948Sobrienstatic void remote_async_kill (void);
12619370Spst
12798948Sobrienstatic int tohex (int nib);
12819370Spst
12998948Sobrienstatic void remote_detach (char *args, int from_tty);
13019370Spst
13198948Sobrienstatic void remote_interrupt (int signo);
13219370Spst
13398948Sobrienstatic void remote_interrupt_twice (int signo);
13419370Spst
13598948Sobrienstatic void interrupt_query (void);
13646289Sdfr
13798948Sobrienstatic void set_thread (int, int);
13846289Sdfr
13998948Sobrienstatic int remote_thread_alive (ptid_t);
14046289Sdfr
14198948Sobrienstatic void get_offsets (void);
14246289Sdfr
14398948Sobrienstatic long read_frame (char *buf, long sizeof_buf);
14446289Sdfr
14598948Sobrienstatic int remote_insert_breakpoint (CORE_ADDR, char *);
14646289Sdfr
14798948Sobrienstatic int remote_remove_breakpoint (CORE_ADDR, char *);
14846289Sdfr
14998948Sobrienstatic int hexnumlen (ULONGEST num);
15046289Sdfr
15198948Sobrienstatic void init_remote_ops (void);
15246289Sdfr
15398948Sobrienstatic void init_extended_remote_ops (void);
15446289Sdfr
15598948Sobrienstatic void remote_stop (void);
15646289Sdfr
15798948Sobrienstatic int ishex (int ch, int *val);
15846289Sdfr
15998948Sobrienstatic int stubhex (int ch);
16046289Sdfr
16198948Sobrienstatic int hexnumstr (char *, ULONGEST);
16246289Sdfr
16398948Sobrienstatic int hexnumnstr (char *, ULONGEST, int);
16446289Sdfr
16598948Sobrienstatic CORE_ADDR remote_address_masked (CORE_ADDR);
16646289Sdfr
16798948Sobrienstatic void print_packet (char *);
16846289Sdfr
16998948Sobrienstatic unsigned long crc32 (unsigned char *, int, unsigned int);
17046289Sdfr
17198948Sobrienstatic void compare_sections_command (char *, int);
17246289Sdfr
17398948Sobrienstatic void packet_command (char *, int);
17446289Sdfr
17598948Sobrienstatic int stub_unpack_int (char *buff, int fieldlength);
17646289Sdfr
17798948Sobrienstatic ptid_t remote_current_thread (ptid_t oldptid);
17846289Sdfr
17998948Sobrienstatic void remote_find_new_threads (void);
18046289Sdfr
18198948Sobrienstatic void record_currthread (int currthread);
18246289Sdfr
18398948Sobrienstatic int fromhex (int a);
18446289Sdfr
18598948Sobrienstatic int hex2bin (const char *hex, char *bin, int count);
18646289Sdfr
18798948Sobrienstatic int bin2hex (const char *bin, char *hex, int count);
18846289Sdfr
18998948Sobrienstatic int putpkt_binary (char *buf, int cnt);
19046289Sdfr
19198948Sobrienstatic void check_binary_download (CORE_ADDR addr);
19246289Sdfr
19398948Sobrienstruct packet_config;
19446289Sdfr
19598948Sobrienstatic void show_packet_config_cmd (struct packet_config *config);
19646289Sdfr
19798948Sobrienstatic void update_packet_config (struct packet_config *config);
19846289Sdfr
19998948Sobrienvoid _initialize_remote (void);
20046289Sdfr
201130809Smarcel/* Description of the remote protocol.  Strictly speaking, when the
20298948Sobrien   target is open()ed, remote.c should create a per-target description
20398948Sobrien   of the remote protocol using that target's architecture.
204130809Smarcel   Unfortunately, the target stack doesn't include local state.  For
20598948Sobrien   the moment keep the information in the target's architecture
20698948Sobrien   object.  Sigh..  */
20746289Sdfr
20898948Sobrienstruct packet_reg
20998948Sobrien{
21098948Sobrien  long offset; /* Offset into G packet.  */
21198948Sobrien  long regnum; /* GDB's internal register number.  */
21298948Sobrien  LONGEST pnum; /* Remote protocol register number.  */
21398948Sobrien  int in_g_packet; /* Always part of G packet.  */
214130809Smarcel  /* long size in bytes;  == DEPRECATED_REGISTER_RAW_SIZE (regnum); at present.  */
21598948Sobrien  /* char *name; == REGISTER_NAME (regnum); at present.  */
21698948Sobrien};
21746289Sdfr
21898948Sobrienstruct remote_state
21998948Sobrien{
22098948Sobrien  /* Description of the remote protocol registers.  */
22198948Sobrien  long sizeof_g_packet;
22246289Sdfr
22398948Sobrien  /* Description of the remote protocol registers indexed by REGNUM
22498948Sobrien     (making an array of NUM_REGS + NUM_PSEUDO_REGS in size).  */
22598948Sobrien  struct packet_reg *regs;
22646289Sdfr
22798948Sobrien  /* This is the size (in chars) of the first response to the ``g''
22898948Sobrien     packet.  It is used as a heuristic when determining the maximum
22998948Sobrien     size of memory-read and memory-write packets.  A target will
23098948Sobrien     typically only reserve a buffer large enough to hold the ``g''
23198948Sobrien     packet.  The size does not include packet overhead (headers and
23298948Sobrien     trailers). */
23398948Sobrien  long actual_register_packet_size;
23446289Sdfr
23598948Sobrien  /* This is the maximum size (in chars) of a non read/write packet.
23698948Sobrien     It is also used as a cap on the size of read/write packets. */
23798948Sobrien  long remote_packet_size;
23898948Sobrien};
23946289Sdfr
240130809Smarcel
24198948Sobrien/* Handle for retreving the remote protocol data from gdbarch.  */
24298948Sobrienstatic struct gdbarch_data *remote_gdbarch_data_handle;
24346289Sdfr
24498948Sobrienstatic struct remote_state *
245130809Smarcelget_remote_state (void)
24698948Sobrien{
247130809Smarcel  return gdbarch_data (current_gdbarch, remote_gdbarch_data_handle);
24898948Sobrien}
24946289Sdfr
25098948Sobrienstatic void *
25198948Sobrieninit_remote_state (struct gdbarch *gdbarch)
25298948Sobrien{
25398948Sobrien  int regnum;
254130809Smarcel  struct remote_state *rs = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct remote_state);
25546289Sdfr
256130809Smarcel  if (DEPRECATED_REGISTER_BYTES != 0)
257130809Smarcel    rs->sizeof_g_packet = DEPRECATED_REGISTER_BYTES;
258130809Smarcel  else
259130809Smarcel    rs->sizeof_g_packet = 0;
26046289Sdfr
26198948Sobrien  /* Assume a 1:1 regnum<->pnum table.  */
262130809Smarcel  rs->regs = GDBARCH_OBSTACK_CALLOC (gdbarch, NUM_REGS + NUM_PSEUDO_REGS,
263130809Smarcel				     struct packet_reg);
26498948Sobrien  for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
26598948Sobrien    {
26698948Sobrien      struct packet_reg *r = &rs->regs[regnum];
26798948Sobrien      r->pnum = regnum;
26898948Sobrien      r->regnum = regnum;
269130809Smarcel      r->offset = DEPRECATED_REGISTER_BYTE (regnum);
27098948Sobrien      r->in_g_packet = (regnum < NUM_REGS);
27198948Sobrien      /* ...name = REGISTER_NAME (regnum); */
272130809Smarcel
273130809Smarcel      /* Compute packet size by accumulating the size of all registers. */
274130809Smarcel      if (DEPRECATED_REGISTER_BYTES == 0)
275130809Smarcel        rs->sizeof_g_packet += register_size (current_gdbarch, regnum);
27698948Sobrien    }
27746289Sdfr
27898948Sobrien  /* Default maximum number of characters in a packet body. Many
27998948Sobrien     remote stubs have a hardwired buffer size of 400 bytes
28098948Sobrien     (c.f. BUFMAX in m68k-stub.c and i386-stub.c).  BUFMAX-1 is used
28198948Sobrien     as the maximum packet-size to ensure that the packet and an extra
28298948Sobrien     NUL character can always fit in the buffer.  This stops GDB
28398948Sobrien     trashing stubs that try to squeeze an extra NUL into what is
28498948Sobrien     already a full buffer (As of 1999-12-04 that was most stubs. */
28598948Sobrien  rs->remote_packet_size = 400 - 1;
28646289Sdfr
28798948Sobrien  /* Should rs->sizeof_g_packet needs more space than the
28898948Sobrien     default, adjust the size accordingly. Remember that each byte is
28998948Sobrien     encoded as two characters. 32 is the overhead for the packet
29098948Sobrien     header / footer. NOTE: cagney/1999-10-26: I suspect that 8
29198948Sobrien     (``$NN:G...#NN'') is a better guess, the below has been padded a
29298948Sobrien     little. */
29398948Sobrien  if (rs->sizeof_g_packet > ((rs->remote_packet_size - 32) / 2))
29498948Sobrien    rs->remote_packet_size = (rs->sizeof_g_packet * 2 + 32);
295130809Smarcel
29698948Sobrien  /* This one is filled in when a ``g'' packet is received. */
29798948Sobrien  rs->actual_register_packet_size = 0;
29846289Sdfr
29998948Sobrien  return rs;
30098948Sobrien}
30146289Sdfr
30298948Sobrienstatic struct packet_reg *
30398948Sobrienpacket_reg_from_regnum (struct remote_state *rs, long regnum)
30498948Sobrien{
30598948Sobrien  if (regnum < 0 && regnum >= NUM_REGS + NUM_PSEUDO_REGS)
30698948Sobrien    return NULL;
30798948Sobrien  else
30898948Sobrien    {
30998948Sobrien      struct packet_reg *r = &rs->regs[regnum];
31098948Sobrien      gdb_assert (r->regnum == regnum);
31198948Sobrien      return r;
31298948Sobrien    }
31398948Sobrien}
31446289Sdfr
31598948Sobrienstatic struct packet_reg *
31698948Sobrienpacket_reg_from_pnum (struct remote_state *rs, LONGEST pnum)
31798948Sobrien{
31898948Sobrien  int i;
31998948Sobrien  for (i = 0; i < NUM_REGS + NUM_PSEUDO_REGS; i++)
32098948Sobrien    {
32198948Sobrien      struct packet_reg *r = &rs->regs[i];
32298948Sobrien      if (r->pnum == pnum)
32398948Sobrien	return r;
32498948Sobrien    }
32598948Sobrien  return NULL;
32698948Sobrien}
32746289Sdfr
328130809Smarcel/* FIXME: graces/2002-08-08: These variables should eventually be
329130809Smarcel   bound to an instance of the target object (as in gdbarch-tdep()),
330130809Smarcel   when such a thing exists.  */
33146289Sdfr
332130809Smarcel/* This is set to the data address of the access causing the target
333130809Smarcel   to stop for a watchpoint.  */
334130809Smarcelstatic CORE_ADDR remote_watch_data_address;
335130809Smarcel
336130809Smarcel/* This is non-zero if taregt stopped for a watchpoint. */
337130809Smarcelstatic int remote_stopped_by_watchpoint_p;
338130809Smarcel
339130809Smarcel
34046289Sdfrstatic struct target_ops remote_ops;
34146289Sdfr
34246289Sdfrstatic struct target_ops extended_remote_ops;
34346289Sdfr
34498948Sobrien/* Temporary target ops. Just like the remote_ops and
34598948Sobrien   extended_remote_ops, but with asynchronous support. */
34698948Sobrienstatic struct target_ops remote_async_ops;
34746289Sdfr
34898948Sobrienstatic struct target_ops extended_async_remote_ops;
34919370Spst
35098948Sobrien/* FIXME: cagney/1999-09-23: Even though getpkt was called with
35198948Sobrien   ``forever'' still use the normal timeout mechanism.  This is
35298948Sobrien   currently used by the ASYNC code to guarentee that target reads
35398948Sobrien   during the initial connect always time-out.  Once getpkt has been
35498948Sobrien   modified to return a timeout indication and, in turn
35598948Sobrien   remote_wait()/wait_for_inferior() have gained a timeout parameter
35698948Sobrien   this can go away. */
35798948Sobrienstatic int wait_forever_enabled_p = 1;
35819370Spst
35998948Sobrien
36019370Spst/* This variable chooses whether to send a ^C or a break when the user
36119370Spst   requests program interruption.  Although ^C is usually what remote
36219370Spst   systems expect, and that is the default here, sometimes a break is
36319370Spst   preferable instead.  */
36419370Spst
36519370Spststatic int remote_break;
36619370Spst
36719370Spst/* Descriptor for I/O to remote machine.  Initialize it to NULL so that
36819370Spst   remote_open knows that we don't have a file open when the program
36919370Spst   starts.  */
37098948Sobrienstatic struct serial *remote_desc = NULL;
37119370Spst
37246289Sdfr/* This variable sets the number of bits in an address that are to be
37346289Sdfr   sent in a memory ("M" or "m") packet.  Normally, after stripping
37446289Sdfr   leading zeros, the entire address would be sent. This variable
37546289Sdfr   restricts the address to REMOTE_ADDRESS_SIZE bits.  HISTORY: The
37646289Sdfr   initial implementation of remote.c restricted the address sent in
37746289Sdfr   memory packets to ``host::sizeof long'' bytes - (typically 32
37846289Sdfr   bits).  Consequently, for 64 bit targets, the upper 32 bits of an
37946289Sdfr   address was never sent.  Since fixing this bug may cause a break in
38046289Sdfr   some remote targets this variable is principly provided to
38146289Sdfr   facilitate backward compatibility. */
38246289Sdfr
38346289Sdfrstatic int remote_address_size;
38446289Sdfr
38598948Sobrien/* Tempoary to track who currently owns the terminal.  See
38698948Sobrien   target_async_terminal_* for more details.  */
38746289Sdfr
38898948Sobrienstatic int remote_async_terminal_ours_p;
38946289Sdfr
39098948Sobrien
39198948Sobrien/* User configurable variables for the number of characters in a
39298948Sobrien   memory read/write packet.  MIN ((rs->remote_packet_size),
39398948Sobrien   rs->sizeof_g_packet) is the default.  Some targets need smaller
39498948Sobrien   values (fifo overruns, et.al.)  and some users need larger values
39598948Sobrien   (speed up transfers).  The variables ``preferred_*'' (the user
39698948Sobrien   request), ``current_*'' (what was actually set) and ``forced_*''
39798948Sobrien   (Positive - a soft limit, negative - a hard limit). */
39819370Spst
39998948Sobrienstruct memory_packet_config
40098948Sobrien{
40198948Sobrien  char *name;
40298948Sobrien  long size;
40398948Sobrien  int fixed_p;
40498948Sobrien};
40546289Sdfr
40698948Sobrien/* Compute the current size of a read/write packet.  Since this makes
40798948Sobrien   use of ``actual_register_packet_size'' the computation is dynamic.  */
40846289Sdfr
40998948Sobrienstatic long
41098948Sobrienget_memory_packet_size (struct memory_packet_config *config)
41198948Sobrien{
41298948Sobrien  struct remote_state *rs = get_remote_state ();
41398948Sobrien  /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
41498948Sobrien     law?) that some hosts don't cope very well with large alloca()
41598948Sobrien     calls.  Eventually the alloca() code will be replaced by calls to
41698948Sobrien     xmalloc() and make_cleanups() allowing this restriction to either
41798948Sobrien     be lifted or removed. */
41898948Sobrien#ifndef MAX_REMOTE_PACKET_SIZE
41998948Sobrien#define MAX_REMOTE_PACKET_SIZE 16384
42098948Sobrien#endif
42198948Sobrien  /* NOTE: 16 is just chosen at random. */
42298948Sobrien#ifndef MIN_REMOTE_PACKET_SIZE
42398948Sobrien#define MIN_REMOTE_PACKET_SIZE 16
42498948Sobrien#endif
42598948Sobrien  long what_they_get;
42698948Sobrien  if (config->fixed_p)
42798948Sobrien    {
42898948Sobrien      if (config->size <= 0)
42998948Sobrien	what_they_get = MAX_REMOTE_PACKET_SIZE;
43098948Sobrien      else
43198948Sobrien	what_they_get = config->size;
43298948Sobrien    }
43398948Sobrien  else
43498948Sobrien    {
43598948Sobrien      what_they_get = (rs->remote_packet_size);
43698948Sobrien      /* Limit the packet to the size specified by the user. */
43798948Sobrien      if (config->size > 0
43898948Sobrien	  && what_they_get > config->size)
43998948Sobrien	what_they_get = config->size;
44098948Sobrien      /* Limit it to the size of the targets ``g'' response. */
44198948Sobrien      if ((rs->actual_register_packet_size) > 0
44298948Sobrien	  && what_they_get > (rs->actual_register_packet_size))
44398948Sobrien	what_they_get = (rs->actual_register_packet_size);
44498948Sobrien    }
44598948Sobrien  if (what_they_get > MAX_REMOTE_PACKET_SIZE)
44698948Sobrien    what_they_get = MAX_REMOTE_PACKET_SIZE;
44798948Sobrien  if (what_they_get < MIN_REMOTE_PACKET_SIZE)
44898948Sobrien    what_they_get = MIN_REMOTE_PACKET_SIZE;
44998948Sobrien  return what_they_get;
45098948Sobrien}
45121738Sgj
45298948Sobrien/* Update the size of a read/write packet. If they user wants
45398948Sobrien   something really big then do a sanity check. */
45421738Sgj
45598948Sobrienstatic void
45698948Sobrienset_memory_packet_size (char *args, struct memory_packet_config *config)
45798948Sobrien{
45898948Sobrien  int fixed_p = config->fixed_p;
45998948Sobrien  long size = config->size;
46098948Sobrien  if (args == NULL)
46198948Sobrien    error ("Argument required (integer, `fixed' or `limited').");
46298948Sobrien  else if (strcmp (args, "hard") == 0
46398948Sobrien      || strcmp (args, "fixed") == 0)
46498948Sobrien    fixed_p = 1;
46598948Sobrien  else if (strcmp (args, "soft") == 0
46698948Sobrien	   || strcmp (args, "limit") == 0)
46798948Sobrien    fixed_p = 0;
46898948Sobrien  else
46998948Sobrien    {
47098948Sobrien      char *end;
47198948Sobrien      size = strtoul (args, &end, 0);
47298948Sobrien      if (args == end)
47398948Sobrien	error ("Invalid %s (bad syntax).", config->name);
47498948Sobrien#if 0
47598948Sobrien      /* Instead of explicitly capping the size of a packet to
47698948Sobrien         MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
47798948Sobrien         instead allowed to set the size to something arbitrarily
47898948Sobrien         large. */
47998948Sobrien      if (size > MAX_REMOTE_PACKET_SIZE)
48098948Sobrien	error ("Invalid %s (too large).", config->name);
48198948Sobrien#endif
48298948Sobrien    }
48398948Sobrien  /* Extra checks? */
48498948Sobrien  if (fixed_p && !config->fixed_p)
48598948Sobrien    {
48698948Sobrien      if (! query ("The target may not be able to correctly handle a %s\n"
48798948Sobrien		   "of %ld bytes. Change the packet size? ",
48898948Sobrien		   config->name, size))
48998948Sobrien	error ("Packet size not changed.");
49098948Sobrien    }
49198948Sobrien  /* Update the config. */
49298948Sobrien  config->fixed_p = fixed_p;
49398948Sobrien  config->size = size;
49498948Sobrien}
49521738Sgj
49698948Sobrienstatic void
49798948Sobrienshow_memory_packet_size (struct memory_packet_config *config)
49898948Sobrien{
49998948Sobrien  printf_filtered ("The %s is %ld. ", config->name, config->size);
50098948Sobrien  if (config->fixed_p)
50198948Sobrien    printf_filtered ("Packets are fixed at %ld bytes.\n",
50298948Sobrien		     get_memory_packet_size (config));
50398948Sobrien  else
50498948Sobrien    printf_filtered ("Packets are limited to %ld bytes.\n",
50598948Sobrien		     get_memory_packet_size (config));
50698948Sobrien}
50798948Sobrien
50898948Sobrienstatic struct memory_packet_config memory_write_packet_config =
50998948Sobrien{
51098948Sobrien  "memory-write-packet-size",
51121738Sgj};
51221738Sgj
51398948Sobrienstatic void
51498948Sobrienset_memory_write_packet_size (char *args, int from_tty)
51521738Sgj{
51698948Sobrien  set_memory_packet_size (args, &memory_write_packet_config);
51798948Sobrien}
51821738Sgj
51998948Sobrienstatic void
52098948Sobrienshow_memory_write_packet_size (char *args, int from_tty)
52198948Sobrien{
52298948Sobrien  show_memory_packet_size (&memory_write_packet_config);
52398948Sobrien}
52421738Sgj
52598948Sobrienstatic long
52698948Sobrienget_memory_write_packet_size (void)
52798948Sobrien{
52898948Sobrien  return get_memory_packet_size (&memory_write_packet_config);
52998948Sobrien}
53021738Sgj
53198948Sobrienstatic struct memory_packet_config memory_read_packet_config =
53298948Sobrien{
53398948Sobrien  "memory-read-packet-size",
53498948Sobrien};
53521738Sgj
53698948Sobrienstatic void
53798948Sobrienset_memory_read_packet_size (char *args, int from_tty)
53898948Sobrien{
53998948Sobrien  set_memory_packet_size (args, &memory_read_packet_config);
54098948Sobrien}
54121738Sgj
54298948Sobrienstatic void
54398948Sobrienshow_memory_read_packet_size (char *args, int from_tty)
54498948Sobrien{
54598948Sobrien  show_memory_packet_size (&memory_read_packet_config);
54698948Sobrien}
54721738Sgj
54898948Sobrienstatic long
54998948Sobrienget_memory_read_packet_size (void)
55098948Sobrien{
55198948Sobrien  struct remote_state *rs = get_remote_state ();
55298948Sobrien  long size = get_memory_packet_size (&memory_read_packet_config);
55398948Sobrien  /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
55498948Sobrien     extra buffer size argument before the memory read size can be
55598948Sobrien     increased beyond (rs->remote_packet_size). */
55698948Sobrien  if (size > (rs->remote_packet_size))
55798948Sobrien    size = (rs->remote_packet_size);
55898948Sobrien  return size;
55998948Sobrien}
56021738Sgj
56198948Sobrien
56298948Sobrien/* Generic configuration support for packets the stub optionally
56398948Sobrien   supports. Allows the user to specify the use of the packet as well
56498948Sobrien   as allowing GDB to auto-detect support in the remote stub. */
56521738Sgj
56698948Sobrienenum packet_support
56798948Sobrien  {
56898948Sobrien    PACKET_SUPPORT_UNKNOWN = 0,
56998948Sobrien    PACKET_ENABLE,
57098948Sobrien    PACKET_DISABLE
57198948Sobrien  };
57298948Sobrien
57398948Sobrienstruct packet_config
57498948Sobrien  {
57598948Sobrien    char *name;
57698948Sobrien    char *title;
577130809Smarcel    enum auto_boolean detect;
57898948Sobrien    enum packet_support support;
57998948Sobrien  };
58098948Sobrien
58198948Sobrien/* Analyze a packet's return value and update the packet config
58298948Sobrien   accordingly. */
58398948Sobrien
58498948Sobrienenum packet_result
58598948Sobrien{
58698948Sobrien  PACKET_ERROR,
58798948Sobrien  PACKET_OK,
58898948Sobrien  PACKET_UNKNOWN
58998948Sobrien};
59098948Sobrien
59198948Sobrienstatic void
59298948Sobrienupdate_packet_config (struct packet_config *config)
59398948Sobrien{
59498948Sobrien  switch (config->detect)
59598948Sobrien    {
596130809Smarcel    case AUTO_BOOLEAN_TRUE:
59798948Sobrien      config->support = PACKET_ENABLE;
59898948Sobrien      break;
599130809Smarcel    case AUTO_BOOLEAN_FALSE:
60098948Sobrien      config->support = PACKET_DISABLE;
60198948Sobrien      break;
602130809Smarcel    case AUTO_BOOLEAN_AUTO:
60398948Sobrien      config->support = PACKET_SUPPORT_UNKNOWN;
60498948Sobrien      break;
60521738Sgj    }
60621738Sgj}
60721738Sgj
60898948Sobrienstatic void
60998948Sobrienshow_packet_config_cmd (struct packet_config *config)
61021738Sgj{
61198948Sobrien  char *support = "internal-error";
61298948Sobrien  switch (config->support)
61398948Sobrien    {
61498948Sobrien    case PACKET_ENABLE:
61598948Sobrien      support = "enabled";
61698948Sobrien      break;
61798948Sobrien    case PACKET_DISABLE:
61898948Sobrien      support = "disabled";
61998948Sobrien      break;
62098948Sobrien    case PACKET_SUPPORT_UNKNOWN:
62198948Sobrien      support = "unknown";
62298948Sobrien      break;
62398948Sobrien    }
62498948Sobrien  switch (config->detect)
62598948Sobrien    {
626130809Smarcel    case AUTO_BOOLEAN_AUTO:
62798948Sobrien      printf_filtered ("Support for remote protocol `%s' (%s) packet is auto-detected, currently %s.\n",
62898948Sobrien		       config->name, config->title, support);
62998948Sobrien      break;
630130809Smarcel    case AUTO_BOOLEAN_TRUE:
631130809Smarcel    case AUTO_BOOLEAN_FALSE:
63298948Sobrien      printf_filtered ("Support for remote protocol `%s' (%s) packet is currently %s.\n",
63398948Sobrien		       config->name, config->title, support);
63498948Sobrien      break;
63598948Sobrien    }
63698948Sobrien}
63721738Sgj
63898948Sobrienstatic void
63998948Sobrienadd_packet_config_cmd (struct packet_config *config,
64098948Sobrien		       char *name,
64198948Sobrien		       char *title,
642130809Smarcel		       cmd_sfunc_ftype *set_func,
643130809Smarcel		       cmd_sfunc_ftype *show_func,
64498948Sobrien		       struct cmd_list_element **set_remote_list,
64598948Sobrien		       struct cmd_list_element **show_remote_list,
64698948Sobrien		       int legacy)
64798948Sobrien{
64898948Sobrien  struct cmd_list_element *set_cmd;
64998948Sobrien  struct cmd_list_element *show_cmd;
65098948Sobrien  char *set_doc;
65198948Sobrien  char *show_doc;
65298948Sobrien  char *cmd_name;
65398948Sobrien  config->name = name;
65498948Sobrien  config->title = title;
655130809Smarcel  config->detect = AUTO_BOOLEAN_AUTO;
65698948Sobrien  config->support = PACKET_SUPPORT_UNKNOWN;
65798948Sobrien  xasprintf (&set_doc, "Set use of remote protocol `%s' (%s) packet",
65898948Sobrien	     name, title);
65998948Sobrien  xasprintf (&show_doc, "Show current use of remote protocol `%s' (%s) packet",
66098948Sobrien	     name, title);
66198948Sobrien  /* set/show TITLE-packet {auto,on,off} */
66298948Sobrien  xasprintf (&cmd_name, "%s-packet", title);
663130809Smarcel  add_setshow_auto_boolean_cmd (cmd_name, class_obscure,
664130809Smarcel				&config->detect, set_doc, show_doc,
665130809Smarcel				set_func, show_func,
666130809Smarcel				set_remote_list, show_remote_list);
66798948Sobrien  /* set/show remote NAME-packet {auto,on,off} -- legacy */
66898948Sobrien  if (legacy)
66998948Sobrien    {
67098948Sobrien      char *legacy_name;
67198948Sobrien      xasprintf (&legacy_name, "%s-packet", name);
67298948Sobrien      add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
67398948Sobrien		     set_remote_list);
67498948Sobrien      add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
67598948Sobrien		     show_remote_list);
67621738Sgj    }
67798948Sobrien}
67821738Sgj
67998948Sobrienstatic enum packet_result
68098948Sobrienpacket_ok (const char *buf, struct packet_config *config)
68198948Sobrien{
68298948Sobrien  if (buf[0] != '\0')
68398948Sobrien    {
68498948Sobrien      /* The stub recognized the packet request.  Check that the
68598948Sobrien	 operation succeeded. */
68698948Sobrien      switch (config->support)
68798948Sobrien	{
68898948Sobrien	case PACKET_SUPPORT_UNKNOWN:
68998948Sobrien	  if (remote_debug)
69098948Sobrien	    fprintf_unfiltered (gdb_stdlog,
69198948Sobrien				    "Packet %s (%s) is supported\n",
69298948Sobrien				    config->name, config->title);
69398948Sobrien	  config->support = PACKET_ENABLE;
69498948Sobrien	  break;
69598948Sobrien	case PACKET_DISABLE:
69698948Sobrien	  internal_error (__FILE__, __LINE__,
69798948Sobrien			  "packet_ok: attempt to use a disabled packet");
69898948Sobrien	  break;
69998948Sobrien	case PACKET_ENABLE:
70098948Sobrien	  break;
70198948Sobrien	}
70298948Sobrien      if (buf[0] == 'O' && buf[1] == 'K' && buf[2] == '\0')
70398948Sobrien	/* "OK" - definitly OK. */
70498948Sobrien	return PACKET_OK;
70598948Sobrien      if (buf[0] == 'E'
70698948Sobrien	  && isxdigit (buf[1]) && isxdigit (buf[2])
70798948Sobrien	  && buf[3] == '\0')
70898948Sobrien	/* "Enn"  - definitly an error. */
70998948Sobrien	return PACKET_ERROR;
71098948Sobrien      /* The packet may or may not be OK.  Just assume it is */
71198948Sobrien      return PACKET_OK;
71298948Sobrien    }
71398948Sobrien  else
71498948Sobrien    {
71598948Sobrien      /* The stub does not support the packet. */
71698948Sobrien      switch (config->support)
71798948Sobrien	{
71898948Sobrien	case PACKET_ENABLE:
719130809Smarcel	  if (config->detect == AUTO_BOOLEAN_AUTO)
72098948Sobrien	    /* If the stub previously indicated that the packet was
72198948Sobrien	       supported then there is a protocol error.. */
72298948Sobrien	    error ("Protocol error: %s (%s) conflicting enabled responses.",
72398948Sobrien		   config->name, config->title);
72498948Sobrien	  else
72598948Sobrien	    /* The user set it wrong. */
72698948Sobrien	    error ("Enabled packet %s (%s) not recognized by stub",
72798948Sobrien		   config->name, config->title);
72898948Sobrien	  break;
72998948Sobrien	case PACKET_SUPPORT_UNKNOWN:
73098948Sobrien	  if (remote_debug)
73198948Sobrien	    fprintf_unfiltered (gdb_stdlog,
73298948Sobrien				"Packet %s (%s) is NOT supported\n",
73398948Sobrien				config->name, config->title);
73498948Sobrien	  config->support = PACKET_DISABLE;
73598948Sobrien	  break;
73698948Sobrien	case PACKET_DISABLE:
73798948Sobrien	  break;
73898948Sobrien	}
73998948Sobrien      return PACKET_UNKNOWN;
74098948Sobrien    }
74198948Sobrien}
74221738Sgj
743130809Smarcel/* Should we try the 'vCont' (descriptive resume) request? */
744130809Smarcelstatic struct packet_config remote_protocol_vcont;
745130809Smarcel
746130809Smarcelstatic void
747130809Smarcelset_remote_protocol_vcont_packet_cmd (char *args, int from_tty,
748130809Smarcel				      struct cmd_list_element *c)
749130809Smarcel{
750130809Smarcel  update_packet_config (&remote_protocol_vcont);
751130809Smarcel}
752130809Smarcel
753130809Smarcelstatic void
754130809Smarcelshow_remote_protocol_vcont_packet_cmd (char *args, int from_tty,
755130809Smarcel				       struct cmd_list_element *c)
756130809Smarcel{
757130809Smarcel  show_packet_config_cmd (&remote_protocol_vcont);
758130809Smarcel}
759130809Smarcel
76098948Sobrien/* Should we try the 'qSymbol' (target symbol lookup service) request? */
76198948Sobrienstatic struct packet_config remote_protocol_qSymbol;
76221738Sgj
76398948Sobrienstatic void
76498948Sobrienset_remote_protocol_qSymbol_packet_cmd (char *args, int from_tty,
76598948Sobrien				  struct cmd_list_element *c)
76698948Sobrien{
76798948Sobrien  update_packet_config (&remote_protocol_qSymbol);
76898948Sobrien}
76921738Sgj
77098948Sobrienstatic void
771130809Smarcelshow_remote_protocol_qSymbol_packet_cmd (char *args, int from_tty,
772130809Smarcel					 struct cmd_list_element *c)
77398948Sobrien{
77498948Sobrien  show_packet_config_cmd (&remote_protocol_qSymbol);
77598948Sobrien}
77621738Sgj
77798948Sobrien/* Should we try the 'e' (step over range) request? */
77898948Sobrienstatic struct packet_config remote_protocol_e;
77998948Sobrien
78098948Sobrienstatic void
78198948Sobrienset_remote_protocol_e_packet_cmd (char *args, int from_tty,
78298948Sobrien				  struct cmd_list_element *c)
78398948Sobrien{
78498948Sobrien  update_packet_config (&remote_protocol_e);
78521738Sgj}
78621738Sgj
78798948Sobrienstatic void
788130809Smarcelshow_remote_protocol_e_packet_cmd (char *args, int from_tty,
789130809Smarcel				   struct cmd_list_element *c)
79021738Sgj{
79198948Sobrien  show_packet_config_cmd (&remote_protocol_e);
79298948Sobrien}
79321738Sgj
794130809Smarcel
79598948Sobrien/* Should we try the 'E' (step over range / w signal #) request? */
79698948Sobrienstatic struct packet_config remote_protocol_E;
79721738Sgj
79898948Sobrienstatic void
79998948Sobrienset_remote_protocol_E_packet_cmd (char *args, int from_tty,
80098948Sobrien				  struct cmd_list_element *c)
80198948Sobrien{
80298948Sobrien  update_packet_config (&remote_protocol_E);
80398948Sobrien}
80421738Sgj
80598948Sobrienstatic void
806130809Smarcelshow_remote_protocol_E_packet_cmd (char *args, int from_tty,
807130809Smarcel				   struct cmd_list_element *c)
80898948Sobrien{
80998948Sobrien  show_packet_config_cmd (&remote_protocol_E);
81098948Sobrien}
81121738Sgj
812130809Smarcel
81398948Sobrien/* Should we try the 'P' (set register) request?  */
81421738Sgj
81598948Sobrienstatic struct packet_config remote_protocol_P;
81621738Sgj
81798948Sobrienstatic void
81898948Sobrienset_remote_protocol_P_packet_cmd (char *args, int from_tty,
81998948Sobrien				  struct cmd_list_element *c)
82098948Sobrien{
82198948Sobrien  update_packet_config (&remote_protocol_P);
82298948Sobrien}
82321738Sgj
82498948Sobrienstatic void
825130809Smarcelshow_remote_protocol_P_packet_cmd (char *args, int from_tty,
826130809Smarcel				   struct cmd_list_element *c)
82798948Sobrien{
82898948Sobrien  show_packet_config_cmd (&remote_protocol_P);
82998948Sobrien}
83021738Sgj
83198948Sobrien/* Should we try one of the 'Z' requests?  */
83221738Sgj
83398948Sobrienenum Z_packet_type
83498948Sobrien{
83598948Sobrien  Z_PACKET_SOFTWARE_BP,
83698948Sobrien  Z_PACKET_HARDWARE_BP,
83798948Sobrien  Z_PACKET_WRITE_WP,
83898948Sobrien  Z_PACKET_READ_WP,
83998948Sobrien  Z_PACKET_ACCESS_WP,
84098948Sobrien  NR_Z_PACKET_TYPES
84198948Sobrien};
84298948Sobrien
84398948Sobrienstatic struct packet_config remote_protocol_Z[NR_Z_PACKET_TYPES];
84498948Sobrien
84598948Sobrien/* FIXME: Instead of having all these boiler plate functions, the
84698948Sobrien   command callback should include a context argument. */
84798948Sobrien
84898948Sobrienstatic void
84998948Sobrienset_remote_protocol_Z_software_bp_packet_cmd (char *args, int from_tty,
85098948Sobrien					      struct cmd_list_element *c)
85198948Sobrien{
85298948Sobrien  update_packet_config (&remote_protocol_Z[Z_PACKET_SOFTWARE_BP]);
85398948Sobrien}
85498948Sobrien
85598948Sobrienstatic void
856130809Smarcelshow_remote_protocol_Z_software_bp_packet_cmd (char *args, int from_tty,
857130809Smarcel					       struct cmd_list_element *c)
85898948Sobrien{
85998948Sobrien  show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_SOFTWARE_BP]);
86098948Sobrien}
86198948Sobrien
86298948Sobrienstatic void
86398948Sobrienset_remote_protocol_Z_hardware_bp_packet_cmd (char *args, int from_tty,
86498948Sobrien					      struct cmd_list_element *c)
86598948Sobrien{
86698948Sobrien  update_packet_config (&remote_protocol_Z[Z_PACKET_HARDWARE_BP]);
86798948Sobrien}
86898948Sobrien
86998948Sobrienstatic void
870130809Smarcelshow_remote_protocol_Z_hardware_bp_packet_cmd (char *args, int from_tty,
871130809Smarcel					       struct cmd_list_element *c)
87298948Sobrien{
87398948Sobrien  show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_HARDWARE_BP]);
87498948Sobrien}
87598948Sobrien
87698948Sobrienstatic void
87798948Sobrienset_remote_protocol_Z_write_wp_packet_cmd (char *args, int from_tty,
87898948Sobrien					      struct cmd_list_element *c)
87998948Sobrien{
88098948Sobrien  update_packet_config (&remote_protocol_Z[Z_PACKET_WRITE_WP]);
88198948Sobrien}
88298948Sobrien
88398948Sobrienstatic void
884130809Smarcelshow_remote_protocol_Z_write_wp_packet_cmd (char *args, int from_tty,
885130809Smarcel					    struct cmd_list_element *c)
88698948Sobrien{
88798948Sobrien  show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_WRITE_WP]);
88898948Sobrien}
88998948Sobrien
89098948Sobrienstatic void
89198948Sobrienset_remote_protocol_Z_read_wp_packet_cmd (char *args, int from_tty,
89298948Sobrien					      struct cmd_list_element *c)
89398948Sobrien{
89498948Sobrien  update_packet_config (&remote_protocol_Z[Z_PACKET_READ_WP]);
89598948Sobrien}
89698948Sobrien
89798948Sobrienstatic void
898130809Smarcelshow_remote_protocol_Z_read_wp_packet_cmd (char *args, int from_tty,
899130809Smarcel					   struct cmd_list_element *c)
90098948Sobrien{
90198948Sobrien  show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_READ_WP]);
90298948Sobrien}
90398948Sobrien
90498948Sobrienstatic void
90598948Sobrienset_remote_protocol_Z_access_wp_packet_cmd (char *args, int from_tty,
90698948Sobrien					      struct cmd_list_element *c)
90798948Sobrien{
90898948Sobrien  update_packet_config (&remote_protocol_Z[Z_PACKET_ACCESS_WP]);
90998948Sobrien}
91098948Sobrien
91198948Sobrienstatic void
912130809Smarcelshow_remote_protocol_Z_access_wp_packet_cmd (char *args, int from_tty,
913130809Smarcel					     struct cmd_list_element *c)
91498948Sobrien{
91598948Sobrien  show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_ACCESS_WP]);
91698948Sobrien}
91798948Sobrien
91898948Sobrien/* For compatibility with older distributions.  Provide a ``set remote
91998948Sobrien   Z-packet ...'' command that updates all the Z packet types. */
92098948Sobrien
921130809Smarcelstatic enum auto_boolean remote_Z_packet_detect;
92298948Sobrien
92398948Sobrienstatic void
92498948Sobrienset_remote_protocol_Z_packet_cmd (char *args, int from_tty,
92598948Sobrien				  struct cmd_list_element *c)
92698948Sobrien{
92798948Sobrien  int i;
92898948Sobrien  for (i = 0; i < NR_Z_PACKET_TYPES; i++)
92998948Sobrien    {
93098948Sobrien      remote_protocol_Z[i].detect = remote_Z_packet_detect;
93198948Sobrien      update_packet_config (&remote_protocol_Z[i]);
93221738Sgj    }
93321738Sgj}
93421738Sgj
93598948Sobrienstatic void
936130809Smarcelshow_remote_protocol_Z_packet_cmd (char *args, int from_tty,
937130809Smarcel				   struct cmd_list_element *c)
93898948Sobrien{
93998948Sobrien  int i;
94098948Sobrien  for (i = 0; i < NR_Z_PACKET_TYPES; i++)
94198948Sobrien    {
94298948Sobrien      show_packet_config_cmd (&remote_protocol_Z[i]);
94398948Sobrien    }
94498948Sobrien}
94598948Sobrien
94698948Sobrien/* Should we try the 'X' (remote binary download) packet?
94798948Sobrien
94898948Sobrien   This variable (available to the user via "set remote X-packet")
94998948Sobrien   dictates whether downloads are sent in binary (via the 'X' packet).
95098948Sobrien   We assume that the stub can, and attempt to do it. This will be
95198948Sobrien   cleared if the stub does not understand it. This switch is still
95298948Sobrien   needed, though in cases when the packet is supported in the stub,
95398948Sobrien   but the connection does not allow it (i.e., 7-bit serial connection
95498948Sobrien   only). */
95598948Sobrien
95698948Sobrienstatic struct packet_config remote_protocol_binary_download;
95798948Sobrien
95898948Sobrien/* Should we try the 'ThreadInfo' query packet?
95998948Sobrien
96098948Sobrien   This variable (NOT available to the user: auto-detect only!)
96198948Sobrien   determines whether GDB will use the new, simpler "ThreadInfo"
96298948Sobrien   query or the older, more complex syntax for thread queries.
963130809Smarcel   This is an auto-detect variable (set to true at each connect,
96498948Sobrien   and set to false when the target fails to recognize it).  */
96598948Sobrien
96698948Sobrienstatic int use_threadinfo_query;
96798948Sobrienstatic int use_threadextra_query;
96898948Sobrien
96998948Sobrienstatic void
97098948Sobrienset_remote_protocol_binary_download_cmd (char *args,
97198948Sobrien					 int from_tty,
97298948Sobrien					 struct cmd_list_element *c)
97398948Sobrien{
97498948Sobrien  update_packet_config (&remote_protocol_binary_download);
97598948Sobrien}
97698948Sobrien
97798948Sobrienstatic void
978130809Smarcelshow_remote_protocol_binary_download_cmd (char *args, int from_tty,
979130809Smarcel					  struct cmd_list_element *c)
98098948Sobrien{
98198948Sobrien  show_packet_config_cmd (&remote_protocol_binary_download);
98298948Sobrien}
98398948Sobrien
984130809Smarcel/* Should we try the 'qPart:auxv' (target auxiliary vector read) request? */
985130809Smarcelstatic struct packet_config remote_protocol_qPart_auxv;
98698948Sobrien
987130809Smarcelstatic void
988130809Smarcelset_remote_protocol_qPart_auxv_packet_cmd (char *args, int from_tty,
989130809Smarcel					   struct cmd_list_element *c)
990130809Smarcel{
991130809Smarcel  update_packet_config (&remote_protocol_qPart_auxv);
992130809Smarcel}
993130809Smarcel
994130809Smarcelstatic void
995130809Smarcelshow_remote_protocol_qPart_auxv_packet_cmd (char *args, int from_tty,
996130809Smarcel					    struct cmd_list_element *c)
997130809Smarcel{
998130809Smarcel  show_packet_config_cmd (&remote_protocol_qPart_auxv);
999130809Smarcel}
1000130809Smarcel
1001131086Smarcel/* Should we try the 'qPart:dirty' (target dirty register read) request? */
1002131086Smarcelstatic struct packet_config remote_protocol_qPart_dirty;
1003130809Smarcel
1004131086Smarcelstatic void
1005131086Smarcelset_remote_protocol_qPart_dirty_packet_cmd (char *args, int from_tty,
1006131086Smarcel					    struct cmd_list_element *c)
1007131086Smarcel{
1008131086Smarcel  update_packet_config (&remote_protocol_qPart_dirty);
1009131086Smarcel}
1010131086Smarcel
1011131086Smarcelstatic void
1012131086Smarcelshow_remote_protocol_qPart_dirty_packet_cmd (char *args, int from_tty,
1013131086Smarcel					     struct cmd_list_element *c)
1014131086Smarcel{
1015131086Smarcel  show_packet_config_cmd (&remote_protocol_qPart_dirty);
1016131086Smarcel}
1017131086Smarcel
1018131086Smarcel
101998948Sobrien/* Tokens for use by the asynchronous signal handlers for SIGINT */
1020130809Smarcelstatic void *sigint_remote_twice_token;
1021130809Smarcelstatic void *sigint_remote_token;
102298948Sobrien
102398948Sobrien/* These are pointers to hook functions that may be set in order to
102498948Sobrien   modify resume/wait behavior for a particular architecture.  */
102598948Sobrien
102698948Sobrienvoid (*target_resume_hook) (void);
102798948Sobrienvoid (*target_wait_loop_hook) (void);
102821738Sgj
102998948Sobrien
103098948Sobrien
103146289Sdfr/* These are the threads which we last sent to the remote system.
103246289Sdfr   -1 for all or -2 for not sent yet.  */
103346289Sdfrstatic int general_thread;
103498948Sobrienstatic int continue_thread;
103519370Spst
103646289Sdfr/* Call this function as a result of
103746289Sdfr   1) A halt indication (T packet) containing a thread id
103846289Sdfr   2) A direct query of currthread
103946289Sdfr   3) Successful execution of set thread
104046289Sdfr */
104146289Sdfr
104219370Spststatic void
104398948Sobrienrecord_currthread (int currthread)
104446289Sdfr{
104546289Sdfr  general_thread = currthread;
104698948Sobrien
104746289Sdfr  /* If this is a new thread, add it to GDB's thread list.
104846289Sdfr     If we leave it up to WFI to do this, bad things will happen.  */
104998948Sobrien  if (!in_thread_list (pid_to_ptid (currthread)))
105098948Sobrien    {
105198948Sobrien      add_thread (pid_to_ptid (currthread));
105298948Sobrien      ui_out_text (uiout, "[New ");
105398948Sobrien      ui_out_text (uiout, target_pid_to_str (pid_to_ptid (currthread)));
105498948Sobrien      ui_out_text (uiout, "]\n");
105598948Sobrien    }
105646289Sdfr}
105746289Sdfr
105846289Sdfr#define MAGIC_NULL_PID 42000
105946289Sdfr
106046289Sdfrstatic void
106198948Sobrienset_thread (int th, int gen)
106219370Spst{
106398948Sobrien  struct remote_state *rs = get_remote_state ();
106498948Sobrien  char *buf = alloca (rs->remote_packet_size);
106598948Sobrien  int state = gen ? general_thread : continue_thread;
106646289Sdfr
106719370Spst  if (state == th)
106819370Spst    return;
106946289Sdfr
107019370Spst  buf[0] = 'H';
107119370Spst  buf[1] = gen ? 'g' : 'c';
107246289Sdfr  if (th == MAGIC_NULL_PID)
107319370Spst    {
107419370Spst      buf[2] = '0';
107519370Spst      buf[3] = '\0';
107619370Spst    }
107719370Spst  else if (th < 0)
107819370Spst    sprintf (&buf[2], "-%x", -th);
107919370Spst  else
108019370Spst    sprintf (&buf[2], "%x", th);
108119370Spst  putpkt (buf);
108298948Sobrien  getpkt (buf, (rs->remote_packet_size), 0);
108319370Spst  if (gen)
108419370Spst    general_thread = th;
108519370Spst  else
108698948Sobrien    continue_thread = th;
108719370Spst}
108819370Spst
108919370Spst/*  Return nonzero if the thread TH is still alive on the remote system.  */
109019370Spst
109119370Spststatic int
109298948Sobrienremote_thread_alive (ptid_t ptid)
109319370Spst{
109498948Sobrien  int tid = PIDGET (ptid);
109598948Sobrien  char buf[16];
109619370Spst
109798948Sobrien  if (tid < 0)
109898948Sobrien    sprintf (buf, "T-%08x", -tid);
109919370Spst  else
110098948Sobrien    sprintf (buf, "T%08x", tid);
110119370Spst  putpkt (buf);
110298948Sobrien  getpkt (buf, sizeof (buf), 0);
110319370Spst  return (buf[0] == 'O' && buf[1] == 'K');
110419370Spst}
110519370Spst
110646289Sdfr/* About these extended threadlist and threadinfo packets.  They are
110746289Sdfr   variable length packets but, the fields within them are often fixed
110846289Sdfr   length.  They are redundent enough to send over UDP as is the
110946289Sdfr   remote protocol in general.  There is a matching unit test module
111046289Sdfr   in libstub.  */
111146289Sdfr
111298948Sobrien#define OPAQUETHREADBYTES 8
111398948Sobrien
111498948Sobrien/* a 64 bit opaque identifier */
111598948Sobrientypedef unsigned char threadref[OPAQUETHREADBYTES];
111698948Sobrien
111798948Sobrien/* WARNING: This threadref data structure comes from the remote O.S., libstub
111898948Sobrien   protocol encoding, and remote.c. it is not particularly changable */
111998948Sobrien
112098948Sobrien/* Right now, the internal structure is int. We want it to be bigger.
112198948Sobrien   Plan to fix this.
112298948Sobrien */
112398948Sobrien
112498948Sobrientypedef int gdb_threadref;	/* internal GDB thread reference */
112598948Sobrien
112698948Sobrien/* gdb_ext_thread_info is an internal GDB data structure which is
112798948Sobrien   equivalint to the reply of the remote threadinfo packet */
112898948Sobrien
112998948Sobrienstruct gdb_ext_thread_info
113098948Sobrien  {
113198948Sobrien    threadref threadid;		/* External form of thread reference */
113298948Sobrien    int active;			/* Has state interesting to GDB? , regs, stack */
113398948Sobrien    char display[256];		/* Brief state display, name, blocked/syspended */
113498948Sobrien    char shortname[32];		/* To be used to name threads */
113598948Sobrien    char more_display[256];	/* Long info, statistics, queue depth, whatever */
113698948Sobrien  };
113798948Sobrien
113898948Sobrien/* The volume of remote transfers can be limited by submitting
113998948Sobrien   a mask containing bits specifying the desired information.
114098948Sobrien   Use a union of these values as the 'selection' parameter to
114198948Sobrien   get_thread_info. FIXME: Make these TAG names more thread specific.
114298948Sobrien */
114398948Sobrien
114498948Sobrien#define TAG_THREADID 1
114598948Sobrien#define TAG_EXISTS 2
114698948Sobrien#define TAG_DISPLAY 4
114798948Sobrien#define TAG_THREADNAME 8
114898948Sobrien#define TAG_MOREDISPLAY 16
114998948Sobrien
115046289Sdfr#define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES*2)
115146289Sdfr
1152130809Smarcelchar *unpack_varlen_hex (char *buff, ULONGEST *result);
115398948Sobrien
115498948Sobrienstatic char *unpack_nibble (char *buf, int *val);
115598948Sobrien
115698948Sobrienstatic char *pack_nibble (char *buf, int nibble);
115798948Sobrien
115898948Sobrienstatic char *pack_hex_byte (char *pkt, int /*unsigned char */ byte);
115998948Sobrien
116098948Sobrienstatic char *unpack_byte (char *buf, int *value);
116198948Sobrien
116298948Sobrienstatic char *pack_int (char *buf, int value);
116398948Sobrien
116498948Sobrienstatic char *unpack_int (char *buf, int *value);
116598948Sobrien
116698948Sobrienstatic char *unpack_string (char *src, char *dest, int length);
116798948Sobrien
116898948Sobrienstatic char *pack_threadid (char *pkt, threadref * id);
116998948Sobrien
117098948Sobrienstatic char *unpack_threadid (char *inbuf, threadref * id);
117198948Sobrien
117298948Sobrienvoid int_to_threadref (threadref * id, int value);
117398948Sobrien
117498948Sobrienstatic int threadref_to_int (threadref * ref);
117598948Sobrien
117698948Sobrienstatic void copy_threadref (threadref * dest, threadref * src);
117798948Sobrien
117898948Sobrienstatic int threadmatch (threadref * dest, threadref * src);
117998948Sobrien
118098948Sobrienstatic char *pack_threadinfo_request (char *pkt, int mode, threadref * id);
118198948Sobrien
118298948Sobrienstatic int remote_unpack_thread_info_response (char *pkt,
118398948Sobrien					       threadref * expectedref,
118498948Sobrien					       struct gdb_ext_thread_info
118598948Sobrien					       *info);
118698948Sobrien
118798948Sobrien
118898948Sobrienstatic int remote_get_threadinfo (threadref * threadid, int fieldset,	/*TAG mask */
118998948Sobrien				  struct gdb_ext_thread_info *info);
119098948Sobrien
119198948Sobrienstatic char *pack_threadlist_request (char *pkt, int startflag,
119298948Sobrien				      int threadcount,
119398948Sobrien				      threadref * nextthread);
119498948Sobrien
119598948Sobrienstatic int parse_threadlist_response (char *pkt,
119698948Sobrien				      int result_limit,
119798948Sobrien				      threadref * original_echo,
119898948Sobrien				      threadref * resultlist, int *doneflag);
119998948Sobrien
120098948Sobrienstatic int remote_get_threadlist (int startflag,
120198948Sobrien				  threadref * nextthread,
120298948Sobrien				  int result_limit,
120398948Sobrien				  int *done,
120498948Sobrien				  int *result_count, threadref * threadlist);
120598948Sobrien
120698948Sobrientypedef int (*rmt_thread_action) (threadref * ref, void *context);
120798948Sobrien
120898948Sobrienstatic int remote_threadlist_iterator (rmt_thread_action stepfunction,
120998948Sobrien				       void *context, int looplimit);
121098948Sobrien
121198948Sobrienstatic int remote_newthread_step (threadref * ref, void *context);
121298948Sobrien
121346289Sdfr/* encode 64 bits in 16 chars of hex */
121446289Sdfr
121546289Sdfrstatic const char hexchars[] = "0123456789abcdef";
121646289Sdfr
121746289Sdfrstatic int
121898948Sobrienishex (int ch, int *val)
121946289Sdfr{
122046289Sdfr  if ((ch >= 'a') && (ch <= 'f'))
122146289Sdfr    {
122246289Sdfr      *val = ch - 'a' + 10;
122346289Sdfr      return 1;
122446289Sdfr    }
122546289Sdfr  if ((ch >= 'A') && (ch <= 'F'))
122646289Sdfr    {
122746289Sdfr      *val = ch - 'A' + 10;
122846289Sdfr      return 1;
122946289Sdfr    }
123046289Sdfr  if ((ch >= '0') && (ch <= '9'))
123146289Sdfr    {
123246289Sdfr      *val = ch - '0';
123346289Sdfr      return 1;
123446289Sdfr    }
123546289Sdfr  return 0;
123646289Sdfr}
123746289Sdfr
123846289Sdfrstatic int
123998948Sobrienstubhex (int ch)
124046289Sdfr{
124146289Sdfr  if (ch >= 'a' && ch <= 'f')
124246289Sdfr    return ch - 'a' + 10;
124346289Sdfr  if (ch >= '0' && ch <= '9')
124446289Sdfr    return ch - '0';
124546289Sdfr  if (ch >= 'A' && ch <= 'F')
124646289Sdfr    return ch - 'A' + 10;
124746289Sdfr  return -1;
124846289Sdfr}
124946289Sdfr
125046289Sdfrstatic int
125198948Sobrienstub_unpack_int (char *buff, int fieldlength)
125246289Sdfr{
125346289Sdfr  int nibble;
125446289Sdfr  int retval = 0;
125546289Sdfr
125646289Sdfr  while (fieldlength)
125746289Sdfr    {
125846289Sdfr      nibble = stubhex (*buff++);
125946289Sdfr      retval |= nibble;
126046289Sdfr      fieldlength--;
126146289Sdfr      if (fieldlength)
126246289Sdfr	retval = retval << 4;
126346289Sdfr    }
126446289Sdfr  return retval;
126546289Sdfr}
126646289Sdfr
126746289Sdfrchar *
126898948Sobrienunpack_varlen_hex (char *buff,	/* packet to parse */
1269130809Smarcel		   ULONGEST *result)
127046289Sdfr{
127146289Sdfr  int nibble;
127246289Sdfr  int retval = 0;
127346289Sdfr
127446289Sdfr  while (ishex (*buff, &nibble))
127546289Sdfr    {
127646289Sdfr      buff++;
127746289Sdfr      retval = retval << 4;
127846289Sdfr      retval |= nibble & 0x0f;
127946289Sdfr    }
128046289Sdfr  *result = retval;
128146289Sdfr  return buff;
128246289Sdfr}
128346289Sdfr
128446289Sdfrstatic char *
128598948Sobrienunpack_nibble (char *buf, int *val)
128646289Sdfr{
128746289Sdfr  ishex (*buf++, val);
128846289Sdfr  return buf;
128946289Sdfr}
129046289Sdfr
129146289Sdfrstatic char *
129298948Sobrienpack_nibble (char *buf, int nibble)
129346289Sdfr{
129446289Sdfr  *buf++ = hexchars[(nibble & 0x0f)];
129546289Sdfr  return buf;
129646289Sdfr}
129746289Sdfr
129846289Sdfrstatic char *
129998948Sobrienpack_hex_byte (char *pkt, int byte)
130046289Sdfr{
130146289Sdfr  *pkt++ = hexchars[(byte >> 4) & 0xf];
130246289Sdfr  *pkt++ = hexchars[(byte & 0xf)];
130346289Sdfr  return pkt;
130446289Sdfr}
130546289Sdfr
130646289Sdfrstatic char *
130798948Sobrienunpack_byte (char *buf, int *value)
130846289Sdfr{
130946289Sdfr  *value = stub_unpack_int (buf, 2);
131046289Sdfr  return buf + 2;
131146289Sdfr}
131246289Sdfr
131346289Sdfrstatic char *
131498948Sobrienpack_int (char *buf, int value)
131546289Sdfr{
131646289Sdfr  buf = pack_hex_byte (buf, (value >> 24) & 0xff);
131746289Sdfr  buf = pack_hex_byte (buf, (value >> 16) & 0xff);
131846289Sdfr  buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
131946289Sdfr  buf = pack_hex_byte (buf, (value & 0xff));
132046289Sdfr  return buf;
132146289Sdfr}
132246289Sdfr
132346289Sdfrstatic char *
132498948Sobrienunpack_int (char *buf, int *value)
132546289Sdfr{
132646289Sdfr  *value = stub_unpack_int (buf, 8);
132746289Sdfr  return buf + 8;
132846289Sdfr}
132946289Sdfr
133098948Sobrien#if 0				/* currently unused, uncomment when needed */
133198948Sobrienstatic char *pack_string (char *pkt, char *string);
133246289Sdfr
133346289Sdfrstatic char *
133498948Sobrienpack_string (char *pkt, char *string)
133546289Sdfr{
133646289Sdfr  char ch;
133746289Sdfr  int len;
133846289Sdfr
133946289Sdfr  len = strlen (string);
134046289Sdfr  if (len > 200)
134146289Sdfr    len = 200;			/* Bigger than most GDB packets, junk??? */
134246289Sdfr  pkt = pack_hex_byte (pkt, len);
134346289Sdfr  while (len-- > 0)
134446289Sdfr    {
134546289Sdfr      ch = *string++;
134646289Sdfr      if ((ch == '\0') || (ch == '#'))
134746289Sdfr	ch = '*';		/* Protect encapsulation */
134846289Sdfr      *pkt++ = ch;
134946289Sdfr    }
135046289Sdfr  return pkt;
135146289Sdfr}
135246289Sdfr#endif /* 0 (unused) */
135346289Sdfr
135446289Sdfrstatic char *
135598948Sobrienunpack_string (char *src, char *dest, int length)
135646289Sdfr{
135746289Sdfr  while (length--)
135846289Sdfr    *dest++ = *src++;
135946289Sdfr  *dest = '\0';
136046289Sdfr  return src;
136146289Sdfr}
136246289Sdfr
136346289Sdfrstatic char *
136498948Sobrienpack_threadid (char *pkt, threadref *id)
136546289Sdfr{
136646289Sdfr  char *limit;
136746289Sdfr  unsigned char *altid;
136846289Sdfr
136946289Sdfr  altid = (unsigned char *) id;
137046289Sdfr  limit = pkt + BUF_THREAD_ID_SIZE;
137146289Sdfr  while (pkt < limit)
137246289Sdfr    pkt = pack_hex_byte (pkt, *altid++);
137346289Sdfr  return pkt;
137446289Sdfr}
137546289Sdfr
137646289Sdfr
137746289Sdfrstatic char *
137898948Sobrienunpack_threadid (char *inbuf, threadref *id)
137946289Sdfr{
138046289Sdfr  char *altref;
138146289Sdfr  char *limit = inbuf + BUF_THREAD_ID_SIZE;
138246289Sdfr  int x, y;
138346289Sdfr
138446289Sdfr  altref = (char *) id;
138546289Sdfr
138646289Sdfr  while (inbuf < limit)
138746289Sdfr    {
138846289Sdfr      x = stubhex (*inbuf++);
138946289Sdfr      y = stubhex (*inbuf++);
139046289Sdfr      *altref++ = (x << 4) | y;
139146289Sdfr    }
139246289Sdfr  return inbuf;
139346289Sdfr}
139446289Sdfr
139546289Sdfr/* Externally, threadrefs are 64 bits but internally, they are still
139646289Sdfr   ints. This is due to a mismatch of specifications.  We would like
139746289Sdfr   to use 64bit thread references internally.  This is an adapter
139846289Sdfr   function.  */
139946289Sdfr
140046289Sdfrvoid
140198948Sobrienint_to_threadref (threadref *id, int value)
140246289Sdfr{
140346289Sdfr  unsigned char *scan;
140446289Sdfr
140546289Sdfr  scan = (unsigned char *) id;
140646289Sdfr  {
140746289Sdfr    int i = 4;
140846289Sdfr    while (i--)
140946289Sdfr      *scan++ = 0;
141046289Sdfr  }
141146289Sdfr  *scan++ = (value >> 24) & 0xff;
141246289Sdfr  *scan++ = (value >> 16) & 0xff;
141346289Sdfr  *scan++ = (value >> 8) & 0xff;
141446289Sdfr  *scan++ = (value & 0xff);
141546289Sdfr}
141646289Sdfr
141746289Sdfrstatic int
141898948Sobrienthreadref_to_int (threadref *ref)
141946289Sdfr{
142046289Sdfr  int i, value = 0;
142146289Sdfr  unsigned char *scan;
142246289Sdfr
142346289Sdfr  scan = (char *) ref;
142446289Sdfr  scan += 4;
142546289Sdfr  i = 4;
142646289Sdfr  while (i-- > 0)
142746289Sdfr    value = (value << 8) | ((*scan++) & 0xff);
142846289Sdfr  return value;
142946289Sdfr}
143046289Sdfr
143146289Sdfrstatic void
143298948Sobriencopy_threadref (threadref *dest, threadref *src)
143346289Sdfr{
143446289Sdfr  int i;
143546289Sdfr  unsigned char *csrc, *cdest;
143646289Sdfr
143746289Sdfr  csrc = (unsigned char *) src;
143846289Sdfr  cdest = (unsigned char *) dest;
143946289Sdfr  i = 8;
144046289Sdfr  while (i--)
144146289Sdfr    *cdest++ = *csrc++;
144246289Sdfr}
144346289Sdfr
144446289Sdfrstatic int
144598948Sobrienthreadmatch (threadref *dest, threadref *src)
144646289Sdfr{
144746289Sdfr  /* things are broken right now, so just assume we got a match */
144846289Sdfr#if 0
144946289Sdfr  unsigned char *srcp, *destp;
145046289Sdfr  int i, result;
145146289Sdfr  srcp = (char *) src;
145246289Sdfr  destp = (char *) dest;
145346289Sdfr
145446289Sdfr  result = 1;
145546289Sdfr  while (i-- > 0)
145646289Sdfr    result &= (*srcp++ == *destp++) ? 1 : 0;
145746289Sdfr  return result;
145846289Sdfr#endif
145946289Sdfr  return 1;
146046289Sdfr}
146146289Sdfr
146246289Sdfr/*
146398948Sobrien   threadid:1,        # always request threadid
146498948Sobrien   context_exists:2,
146598948Sobrien   display:4,
146698948Sobrien   unique_name:8,
146798948Sobrien   more_display:16
146898948Sobrien */
146946289Sdfr
147046289Sdfr/* Encoding:  'Q':8,'P':8,mask:32,threadid:64 */
147146289Sdfr
147246289Sdfrstatic char *
147398948Sobrienpack_threadinfo_request (char *pkt, int mode, threadref *id)
147446289Sdfr{
147546289Sdfr  *pkt++ = 'q';			/* Info Query */
147646289Sdfr  *pkt++ = 'P';			/* process or thread info */
147746289Sdfr  pkt = pack_int (pkt, mode);	/* mode */
147846289Sdfr  pkt = pack_threadid (pkt, id);	/* threadid */
147946289Sdfr  *pkt = '\0';			/* terminate */
148046289Sdfr  return pkt;
148146289Sdfr}
148246289Sdfr
148346289Sdfr/* These values tag the fields in a thread info response packet */
148446289Sdfr/* Tagging the fields allows us to request specific fields and to
148546289Sdfr   add more fields as time goes by */
148646289Sdfr
148798948Sobrien#define TAG_THREADID 1		/* Echo the thread identifier */
148898948Sobrien#define TAG_EXISTS 2		/* Is this process defined enough to
148998948Sobrien				   fetch registers and its stack */
149098948Sobrien#define TAG_DISPLAY 4		/* A short thing maybe to put on a window */
149198948Sobrien#define TAG_THREADNAME 8	/* string, maps 1-to-1 with a thread is */
1492130809Smarcel#define TAG_MOREDISPLAY 16	/* Whatever the kernel wants to say about
149398948Sobrien				   the process */
149446289Sdfr
149546289Sdfrstatic int
149698948Sobrienremote_unpack_thread_info_response (char *pkt, threadref *expectedref,
149798948Sobrien				    struct gdb_ext_thread_info *info)
149846289Sdfr{
149998948Sobrien  struct remote_state *rs = get_remote_state ();
150046289Sdfr  int mask, length;
150146289Sdfr  unsigned int tag;
150246289Sdfr  threadref ref;
150398948Sobrien  char *limit = pkt + (rs->remote_packet_size);	/* plausable parsing limit */
150446289Sdfr  int retval = 1;
150546289Sdfr
150646289Sdfr  /* info->threadid = 0; FIXME: implement zero_threadref */
150746289Sdfr  info->active = 0;
150846289Sdfr  info->display[0] = '\0';
150946289Sdfr  info->shortname[0] = '\0';
151046289Sdfr  info->more_display[0] = '\0';
151146289Sdfr
151246289Sdfr  /* Assume the characters indicating the packet type have been stripped */
151346289Sdfr  pkt = unpack_int (pkt, &mask);	/* arg mask */
151446289Sdfr  pkt = unpack_threadid (pkt, &ref);
151546289Sdfr
151646289Sdfr  if (mask == 0)
151746289Sdfr    warning ("Incomplete response to threadinfo request\n");
151846289Sdfr  if (!threadmatch (&ref, expectedref))
151946289Sdfr    {				/* This is an answer to a different request */
152046289Sdfr      warning ("ERROR RMT Thread info mismatch\n");
152146289Sdfr      return 0;
152246289Sdfr    }
152346289Sdfr  copy_threadref (&info->threadid, &ref);
152446289Sdfr
152546289Sdfr  /* Loop on tagged fields , try to bail if somthing goes wrong */
152646289Sdfr
152798948Sobrien  while ((pkt < limit) && mask && *pkt)		/* packets are terminated with nulls */
152846289Sdfr    {
152946289Sdfr      pkt = unpack_int (pkt, &tag);	/* tag */
153046289Sdfr      pkt = unpack_byte (pkt, &length);		/* length */
153146289Sdfr      if (!(tag & mask))	/* tags out of synch with mask */
153246289Sdfr	{
153346289Sdfr	  warning ("ERROR RMT: threadinfo tag mismatch\n");
153446289Sdfr	  retval = 0;
153546289Sdfr	  break;
153646289Sdfr	}
153746289Sdfr      if (tag == TAG_THREADID)
153846289Sdfr	{
153946289Sdfr	  if (length != 16)
154046289Sdfr	    {
154146289Sdfr	      warning ("ERROR RMT: length of threadid is not 16\n");
154246289Sdfr	      retval = 0;
154346289Sdfr	      break;
154446289Sdfr	    }
154546289Sdfr	  pkt = unpack_threadid (pkt, &ref);
154646289Sdfr	  mask = mask & ~TAG_THREADID;
154746289Sdfr	  continue;
154846289Sdfr	}
154946289Sdfr      if (tag == TAG_EXISTS)
155046289Sdfr	{
155146289Sdfr	  info->active = stub_unpack_int (pkt, length);
155246289Sdfr	  pkt += length;
155346289Sdfr	  mask = mask & ~(TAG_EXISTS);
155446289Sdfr	  if (length > 8)
155546289Sdfr	    {
155646289Sdfr	      warning ("ERROR RMT: 'exists' length too long\n");
155746289Sdfr	      retval = 0;
155846289Sdfr	      break;
155946289Sdfr	    }
156046289Sdfr	  continue;
156146289Sdfr	}
156246289Sdfr      if (tag == TAG_THREADNAME)
156346289Sdfr	{
156446289Sdfr	  pkt = unpack_string (pkt, &info->shortname[0], length);
156546289Sdfr	  mask = mask & ~TAG_THREADNAME;
156646289Sdfr	  continue;
156746289Sdfr	}
156846289Sdfr      if (tag == TAG_DISPLAY)
156946289Sdfr	{
157046289Sdfr	  pkt = unpack_string (pkt, &info->display[0], length);
157146289Sdfr	  mask = mask & ~TAG_DISPLAY;
157246289Sdfr	  continue;
157346289Sdfr	}
157446289Sdfr      if (tag == TAG_MOREDISPLAY)
157546289Sdfr	{
157646289Sdfr	  pkt = unpack_string (pkt, &info->more_display[0], length);
157746289Sdfr	  mask = mask & ~TAG_MOREDISPLAY;
157846289Sdfr	  continue;
157946289Sdfr	}
158046289Sdfr      warning ("ERROR RMT: unknown thread info tag\n");
158146289Sdfr      break;			/* Not a tag we know about */
158246289Sdfr    }
158346289Sdfr  return retval;
158446289Sdfr}
158546289Sdfr
158646289Sdfrstatic int
158798948Sobrienremote_get_threadinfo (threadref *threadid, int fieldset,	/* TAG mask */
158898948Sobrien		       struct gdb_ext_thread_info *info)
158946289Sdfr{
159098948Sobrien  struct remote_state *rs = get_remote_state ();
159146289Sdfr  int result;
159298948Sobrien  char *threadinfo_pkt = alloca (rs->remote_packet_size);
159346289Sdfr
159446289Sdfr  pack_threadinfo_request (threadinfo_pkt, fieldset, threadid);
159546289Sdfr  putpkt (threadinfo_pkt);
159698948Sobrien  getpkt (threadinfo_pkt, (rs->remote_packet_size), 0);
159746289Sdfr  result = remote_unpack_thread_info_response (threadinfo_pkt + 2, threadid,
159846289Sdfr					       info);
159946289Sdfr  return result;
160046289Sdfr}
160146289Sdfr
160246289Sdfr/*    Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32   */
160346289Sdfr
160446289Sdfrstatic char *
160598948Sobrienpack_threadlist_request (char *pkt, int startflag, int threadcount,
160698948Sobrien			 threadref *nextthread)
160746289Sdfr{
160846289Sdfr  *pkt++ = 'q';			/* info query packet */
160946289Sdfr  *pkt++ = 'L';			/* Process LIST or threadLIST request */
161046289Sdfr  pkt = pack_nibble (pkt, startflag);	/* initflag 1 bytes */
161146289Sdfr  pkt = pack_hex_byte (pkt, threadcount);	/* threadcount 2 bytes */
161246289Sdfr  pkt = pack_threadid (pkt, nextthread);	/* 64 bit thread identifier */
161346289Sdfr  *pkt = '\0';
161446289Sdfr  return pkt;
161546289Sdfr}
161646289Sdfr
161746289Sdfr/* Encoding:   'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
161846289Sdfr
161946289Sdfrstatic int
162098948Sobrienparse_threadlist_response (char *pkt, int result_limit,
162198948Sobrien			   threadref *original_echo, threadref *resultlist,
162298948Sobrien			   int *doneflag)
162346289Sdfr{
162498948Sobrien  struct remote_state *rs = get_remote_state ();
162546289Sdfr  char *limit;
162646289Sdfr  int count, resultcount, done;
162746289Sdfr
162846289Sdfr  resultcount = 0;
162946289Sdfr  /* Assume the 'q' and 'M chars have been stripped.  */
163098948Sobrien  limit = pkt + ((rs->remote_packet_size) - BUF_THREAD_ID_SIZE);		/* done parse past here */
163146289Sdfr  pkt = unpack_byte (pkt, &count);	/* count field */
163246289Sdfr  pkt = unpack_nibble (pkt, &done);
163346289Sdfr  /* The first threadid is the argument threadid.  */
163446289Sdfr  pkt = unpack_threadid (pkt, original_echo);	/* should match query packet */
163546289Sdfr  while ((count-- > 0) && (pkt < limit))
163646289Sdfr    {
163746289Sdfr      pkt = unpack_threadid (pkt, resultlist++);
163846289Sdfr      if (resultcount++ >= result_limit)
163946289Sdfr	break;
164046289Sdfr    }
164146289Sdfr  if (doneflag)
164246289Sdfr    *doneflag = done;
164346289Sdfr  return resultcount;
164446289Sdfr}
164546289Sdfr
164646289Sdfrstatic int
164798948Sobrienremote_get_threadlist (int startflag, threadref *nextthread, int result_limit,
164898948Sobrien		       int *done, int *result_count, threadref *threadlist)
164946289Sdfr{
165098948Sobrien  struct remote_state *rs = get_remote_state ();
165146289Sdfr  static threadref echo_nextthread;
165298948Sobrien  char *threadlist_packet = alloca (rs->remote_packet_size);
165398948Sobrien  char *t_response = alloca (rs->remote_packet_size);
165446289Sdfr  int result = 1;
165546289Sdfr
165646289Sdfr  /* Trancate result limit to be smaller than the packet size */
165798948Sobrien  if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10) >= (rs->remote_packet_size))
165898948Sobrien    result_limit = ((rs->remote_packet_size) / BUF_THREAD_ID_SIZE) - 2;
165946289Sdfr
166046289Sdfr  pack_threadlist_request (threadlist_packet,
166146289Sdfr			   startflag, result_limit, nextthread);
166246289Sdfr  putpkt (threadlist_packet);
166398948Sobrien  getpkt (t_response, (rs->remote_packet_size), 0);
166446289Sdfr
166546289Sdfr  *result_count =
166646289Sdfr    parse_threadlist_response (t_response + 2, result_limit, &echo_nextthread,
166746289Sdfr			       threadlist, done);
166846289Sdfr
166946289Sdfr  if (!threadmatch (&echo_nextthread, nextthread))
167046289Sdfr    {
167146289Sdfr      /* FIXME: This is a good reason to drop the packet */
167246289Sdfr      /* Possably, there is a duplicate response */
167346289Sdfr      /* Possabilities :
167446289Sdfr         retransmit immediatly - race conditions
167546289Sdfr         retransmit after timeout - yes
167646289Sdfr         exit
167746289Sdfr         wait for packet, then exit
167846289Sdfr       */
167946289Sdfr      warning ("HMM: threadlist did not echo arg thread, dropping it\n");
168046289Sdfr      return 0;			/* I choose simply exiting */
168146289Sdfr    }
168246289Sdfr  if (*result_count <= 0)
168346289Sdfr    {
168446289Sdfr      if (*done != 1)
168546289Sdfr	{
168646289Sdfr	  warning ("RMT ERROR : failed to get remote thread list\n");
168746289Sdfr	  result = 0;
168846289Sdfr	}
168946289Sdfr      return result;		/* break; */
169046289Sdfr    }
169146289Sdfr  if (*result_count > result_limit)
169246289Sdfr    {
169346289Sdfr      *result_count = 0;
169446289Sdfr      warning ("RMT ERROR: threadlist response longer than requested\n");
169546289Sdfr      return 0;
169646289Sdfr    }
169746289Sdfr  return result;
169846289Sdfr}
169946289Sdfr
170046289Sdfr/* This is the interface between remote and threads, remotes upper interface */
170146289Sdfr
170246289Sdfr/* remote_find_new_threads retrieves the thread list and for each
170346289Sdfr   thread in the list, looks up the thread in GDB's internal list,
170446289Sdfr   ading the thread if it does not already exist.  This involves
170546289Sdfr   getting partial thread lists from the remote target so, polling the
170646289Sdfr   quit_flag is required.  */
170746289Sdfr
170846289Sdfr
170946289Sdfr/* About this many threadisds fit in a packet. */
171046289Sdfr
171146289Sdfr#define MAXTHREADLISTRESULTS 32
171246289Sdfr
171346289Sdfrstatic int
171498948Sobrienremote_threadlist_iterator (rmt_thread_action stepfunction, void *context,
171598948Sobrien			    int looplimit)
171646289Sdfr{
171746289Sdfr  int done, i, result_count;
171846289Sdfr  int startflag = 1;
171946289Sdfr  int result = 1;
172046289Sdfr  int loopcount = 0;
172146289Sdfr  static threadref nextthread;
172246289Sdfr  static threadref resultthreadlist[MAXTHREADLISTRESULTS];
172346289Sdfr
172446289Sdfr  done = 0;
172546289Sdfr  while (!done)
172646289Sdfr    {
172746289Sdfr      if (loopcount++ > looplimit)
172846289Sdfr	{
172946289Sdfr	  result = 0;
173046289Sdfr	  warning ("Remote fetch threadlist -infinite loop-\n");
173146289Sdfr	  break;
173246289Sdfr	}
173346289Sdfr      if (!remote_get_threadlist (startflag, &nextthread, MAXTHREADLISTRESULTS,
173446289Sdfr				  &done, &result_count, resultthreadlist))
173546289Sdfr	{
173646289Sdfr	  result = 0;
173746289Sdfr	  break;
173846289Sdfr	}
173946289Sdfr      /* clear for later iterations */
174046289Sdfr      startflag = 0;
174146289Sdfr      /* Setup to resume next batch of thread references, set nextthread.  */
174246289Sdfr      if (result_count >= 1)
174346289Sdfr	copy_threadref (&nextthread, &resultthreadlist[result_count - 1]);
174446289Sdfr      i = 0;
174546289Sdfr      while (result_count--)
174646289Sdfr	if (!(result = (*stepfunction) (&resultthreadlist[i++], context)))
174746289Sdfr	  break;
174846289Sdfr    }
174946289Sdfr  return result;
175046289Sdfr}
175146289Sdfr
175246289Sdfrstatic int
175398948Sobrienremote_newthread_step (threadref *ref, void *context)
175446289Sdfr{
175598948Sobrien  ptid_t ptid;
175646289Sdfr
175798948Sobrien  ptid = pid_to_ptid (threadref_to_int (ref));
175898948Sobrien
175998948Sobrien  if (!in_thread_list (ptid))
176098948Sobrien    add_thread (ptid);
176146289Sdfr  return 1;			/* continue iterator */
176246289Sdfr}
176346289Sdfr
176446289Sdfr#define CRAZY_MAX_THREADS 1000
176546289Sdfr
176698948Sobrienstatic ptid_t
176798948Sobrienremote_current_thread (ptid_t oldpid)
176846289Sdfr{
176998948Sobrien  struct remote_state *rs = get_remote_state ();
177098948Sobrien  char *buf = alloca (rs->remote_packet_size);
177146289Sdfr
177246289Sdfr  putpkt ("qC");
177398948Sobrien  getpkt (buf, (rs->remote_packet_size), 0);
177446289Sdfr  if (buf[0] == 'Q' && buf[1] == 'C')
177598948Sobrien    return pid_to_ptid (strtol (&buf[2], NULL, 16));
177646289Sdfr  else
177746289Sdfr    return oldpid;
177846289Sdfr}
177946289Sdfr
1780130809Smarcel/* Find new threads for info threads command.
1781130809Smarcel * Original version, using John Metzler's thread protocol.
178298948Sobrien */
178398948Sobrien
178498948Sobrienstatic void
178598948Sobrienremote_find_new_threads (void)
178646289Sdfr{
178798948Sobrien  remote_threadlist_iterator (remote_newthread_step, 0,
178898948Sobrien			      CRAZY_MAX_THREADS);
178998948Sobrien  if (PIDGET (inferior_ptid) == MAGIC_NULL_PID)	/* ack ack ack */
179098948Sobrien    inferior_ptid = remote_current_thread (inferior_ptid);
179146289Sdfr}
179246289Sdfr
179398948Sobrien/*
179498948Sobrien * Find all threads for info threads command.
179598948Sobrien * Uses new thread protocol contributed by Cisco.
179698948Sobrien * Falls back and attempts to use the older method (above)
179798948Sobrien * if the target doesn't respond to the new method.
179898948Sobrien */
179946289Sdfr
180046289Sdfrstatic void
180198948Sobrienremote_threads_info (void)
180246289Sdfr{
180398948Sobrien  struct remote_state *rs = get_remote_state ();
180498948Sobrien  char *buf = alloca (rs->remote_packet_size);
180598948Sobrien  char *bufp;
180698948Sobrien  int tid;
180798948Sobrien
180898948Sobrien  if (remote_desc == 0)		/* paranoia */
180998948Sobrien    error ("Command can only be used when connected to the remote target.");
181098948Sobrien
181198948Sobrien  if (use_threadinfo_query)
181298948Sobrien    {
181398948Sobrien      putpkt ("qfThreadInfo");
181498948Sobrien      bufp = buf;
181598948Sobrien      getpkt (bufp, (rs->remote_packet_size), 0);
181698948Sobrien      if (bufp[0] != '\0')		/* q packet recognized */
1817130809Smarcel	{
181898948Sobrien	  while (*bufp++ == 'm')	/* reply contains one or more TID */
181998948Sobrien	    {
182098948Sobrien	      do
182198948Sobrien		{
182298948Sobrien		  tid = strtol (bufp, &bufp, 16);
182398948Sobrien		  if (tid != 0 && !in_thread_list (pid_to_ptid (tid)))
182498948Sobrien		    add_thread (pid_to_ptid (tid));
182598948Sobrien		}
182698948Sobrien	      while (*bufp++ == ',');	/* comma-separated list */
182798948Sobrien	      putpkt ("qsThreadInfo");
182898948Sobrien	      bufp = buf;
182998948Sobrien	      getpkt (bufp, (rs->remote_packet_size), 0);
183098948Sobrien	    }
183198948Sobrien	  return;	/* done */
183298948Sobrien	}
183398948Sobrien    }
183498948Sobrien
183598948Sobrien  /* Else fall back to old method based on jmetzler protocol. */
183698948Sobrien  use_threadinfo_query = 0;
183798948Sobrien  remote_find_new_threads ();
183898948Sobrien  return;
183946289Sdfr}
184046289Sdfr
1841130809Smarcel/*
184298948Sobrien * Collect a descriptive string about the given thread.
184398948Sobrien * The target may say anything it wants to about the thread
184498948Sobrien * (typically info about its blocked / runnable state, name, etc.).
184598948Sobrien * This string will appear in the info threads display.
1846130809Smarcel *
184798948Sobrien * Optional: targets are not required to implement this function.
184898948Sobrien */
184998948Sobrien
185098948Sobrienstatic char *
185198948Sobrienremote_threads_extra_info (struct thread_info *tp)
185298948Sobrien{
185398948Sobrien  struct remote_state *rs = get_remote_state ();
185498948Sobrien  int result;
185598948Sobrien  int set;
185698948Sobrien  threadref id;
185798948Sobrien  struct gdb_ext_thread_info threadinfo;
185898948Sobrien  static char display_buf[100];	/* arbitrary... */
185998948Sobrien  char *bufp = alloca (rs->remote_packet_size);
186098948Sobrien  int n = 0;                    /* position in display_buf */
186198948Sobrien
186298948Sobrien  if (remote_desc == 0)		/* paranoia */
186398948Sobrien    internal_error (__FILE__, __LINE__,
186498948Sobrien		    "remote_threads_extra_info");
186598948Sobrien
186698948Sobrien  if (use_threadextra_query)
186798948Sobrien    {
186898948Sobrien      sprintf (bufp, "qThreadExtraInfo,%x", PIDGET (tp->ptid));
186998948Sobrien      putpkt (bufp);
187098948Sobrien      getpkt (bufp, (rs->remote_packet_size), 0);
187198948Sobrien      if (bufp[0] != 0)
187298948Sobrien	{
187398948Sobrien	  n = min (strlen (bufp) / 2, sizeof (display_buf));
187498948Sobrien	  result = hex2bin (bufp, display_buf, n);
187598948Sobrien	  display_buf [result] = '\0';
187698948Sobrien	  return display_buf;
187798948Sobrien	}
187898948Sobrien    }
187998948Sobrien
188098948Sobrien  /* If the above query fails, fall back to the old method.  */
188198948Sobrien  use_threadextra_query = 0;
188298948Sobrien  set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
188398948Sobrien    | TAG_MOREDISPLAY | TAG_DISPLAY;
188498948Sobrien  int_to_threadref (&id, PIDGET (tp->ptid));
188598948Sobrien  if (remote_get_threadinfo (&id, set, &threadinfo))
188698948Sobrien    if (threadinfo.active)
188798948Sobrien      {
188898948Sobrien	if (*threadinfo.shortname)
188998948Sobrien	  n += sprintf(&display_buf[0], " Name: %s,", threadinfo.shortname);
189098948Sobrien	if (*threadinfo.display)
189198948Sobrien	  n += sprintf(&display_buf[n], " State: %s,", threadinfo.display);
189298948Sobrien	if (*threadinfo.more_display)
189398948Sobrien	  n += sprintf(&display_buf[n], " Priority: %s",
189498948Sobrien		       threadinfo.more_display);
189598948Sobrien
189698948Sobrien	if (n > 0)
189798948Sobrien	  {
189898948Sobrien	    /* for purely cosmetic reasons, clear up trailing commas */
189998948Sobrien	    if (',' == display_buf[n-1])
190098948Sobrien	      display_buf[n-1] = ' ';
190198948Sobrien	    return display_buf;
190298948Sobrien	  }
190398948Sobrien      }
190498948Sobrien  return NULL;
190598948Sobrien}
190698948Sobrien
190746289Sdfr
190898948Sobrien
190919370Spst/*  Restart the remote side; this is an extended protocol operation.  */
191019370Spst
191119370Spststatic void
191298948Sobrienextended_remote_restart (void)
191319370Spst{
191498948Sobrien  struct remote_state *rs = get_remote_state ();
191598948Sobrien  char *buf = alloca (rs->remote_packet_size);
191619370Spst
191719370Spst  /* Send the restart command; for reasons I don't understand the
191819370Spst     remote side really expects a number after the "R".  */
191919370Spst  buf[0] = 'R';
192019370Spst  sprintf (&buf[1], "%x", 0);
192119370Spst  putpkt (buf);
192219370Spst
192319370Spst  /* Now query for status so this looks just like we restarted
192419370Spst     gdbserver from scratch.  */
192519370Spst  putpkt ("?");
192698948Sobrien  getpkt (buf, (rs->remote_packet_size), 0);
192719370Spst}
192819370Spst
192919370Spst/* Clean up connection to a remote debugger.  */
193019370Spst
193119370Spststatic void
193298948Sobrienremote_close (int quitting)
193319370Spst{
193419370Spst  if (remote_desc)
193598948Sobrien    serial_close (remote_desc);
193619370Spst  remote_desc = NULL;
193719370Spst}
193819370Spst
193919370Spst/* Query the remote side for the text, data and bss offsets. */
194019370Spst
194119370Spststatic void
194298948Sobrienget_offsets (void)
194319370Spst{
194498948Sobrien  struct remote_state *rs = get_remote_state ();
194598948Sobrien  char *buf = alloca (rs->remote_packet_size);
194698948Sobrien  char *ptr;
194746289Sdfr  int lose;
194819370Spst  CORE_ADDR text_addr, data_addr, bss_addr;
194919370Spst  struct section_offsets *offs;
195019370Spst
195119370Spst  putpkt ("qOffsets");
195219370Spst
195398948Sobrien  getpkt (buf, (rs->remote_packet_size), 0);
195419370Spst
195519370Spst  if (buf[0] == '\000')
195646289Sdfr    return;			/* Return silently.  Stub doesn't support
195746289Sdfr				   this command. */
195819370Spst  if (buf[0] == 'E')
195919370Spst    {
196019370Spst      warning ("Remote failure reply: %s", buf);
196119370Spst      return;
196219370Spst    }
196319370Spst
196446289Sdfr  /* Pick up each field in turn.  This used to be done with scanf, but
196546289Sdfr     scanf will make trouble if CORE_ADDR size doesn't match
196646289Sdfr     conversion directives correctly.  The following code will work
196746289Sdfr     with any size of CORE_ADDR.  */
196846289Sdfr  text_addr = data_addr = bss_addr = 0;
196946289Sdfr  ptr = buf;
197046289Sdfr  lose = 0;
197146289Sdfr
197246289Sdfr  if (strncmp (ptr, "Text=", 5) == 0)
197346289Sdfr    {
197446289Sdfr      ptr += 5;
197546289Sdfr      /* Don't use strtol, could lose on big values.  */
197646289Sdfr      while (*ptr && *ptr != ';')
197746289Sdfr	text_addr = (text_addr << 4) + fromhex (*ptr++);
197846289Sdfr    }
197946289Sdfr  else
198046289Sdfr    lose = 1;
198146289Sdfr
198246289Sdfr  if (!lose && strncmp (ptr, ";Data=", 6) == 0)
198346289Sdfr    {
198446289Sdfr      ptr += 6;
198546289Sdfr      while (*ptr && *ptr != ';')
198646289Sdfr	data_addr = (data_addr << 4) + fromhex (*ptr++);
198746289Sdfr    }
198846289Sdfr  else
198946289Sdfr    lose = 1;
199046289Sdfr
199146289Sdfr  if (!lose && strncmp (ptr, ";Bss=", 5) == 0)
199246289Sdfr    {
199346289Sdfr      ptr += 5;
199446289Sdfr      while (*ptr && *ptr != ';')
199546289Sdfr	bss_addr = (bss_addr << 4) + fromhex (*ptr++);
199646289Sdfr    }
199746289Sdfr  else
199846289Sdfr    lose = 1;
199946289Sdfr
200046289Sdfr  if (lose)
200119370Spst    error ("Malformed response to offset query, %s", buf);
200219370Spst
200319370Spst  if (symfile_objfile == NULL)
200419370Spst    return;
200519370Spst
2006130809Smarcel  offs = ((struct section_offsets *)
2007130809Smarcel	  alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections)));
2008130809Smarcel  memcpy (offs, symfile_objfile->section_offsets,
2009130809Smarcel	  SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections));
201019370Spst
201198948Sobrien  offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
201219370Spst
201319370Spst  /* This is a temporary kludge to force data and bss to use the same offsets
201419370Spst     because that's what nlmconv does now.  The real solution requires changes
201519370Spst     to the stub and remote.c that I don't have time to do right now.  */
201619370Spst
201798948Sobrien  offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr;
201898948Sobrien  offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr;
201919370Spst
202019370Spst  objfile_relocate (symfile_objfile, offs);
202119370Spst}
202219370Spst
202319370Spst/* Stub for catch_errors.  */
202419370Spst
202519370Spststatic int
2026130809Smarcelremote_start_remote_dummy (struct ui_out *uiout, void *dummy)
202719370Spst{
202898948Sobrien  start_remote ();		/* Initialize gdb process mechanisms */
2029130809Smarcel  /* NOTE: Return something >=0.  A -ve value is reserved for
2030130809Smarcel     catch_exceptions.  */
203198948Sobrien  return 1;
203298948Sobrien}
203319370Spst
203498948Sobrienstatic int
2035130809Smarcelremote_start_remote (struct ui_out *uiout, void *dummy)
203698948Sobrien{
203798948Sobrien  immediate_quit++;		/* Allow user to interrupt it */
203898948Sobrien
203919370Spst  /* Ack any packet which the remote side has already sent.  */
204098948Sobrien  serial_write (remote_desc, "+", 1);
204119370Spst
204219370Spst  /* Let the stub know that we want it to return the thread.  */
204319370Spst  set_thread (-1, 0);
204419370Spst
204598948Sobrien  inferior_ptid = remote_current_thread (inferior_ptid);
204646289Sdfr
204719370Spst  get_offsets ();		/* Get text, data & bss offsets */
204819370Spst
204919370Spst  putpkt ("?");			/* initiate a query from remote machine */
205098948Sobrien  immediate_quit--;
205119370Spst
2052130809Smarcel  /* NOTE: See comment above in remote_start_remote_dummy().  This
2053130809Smarcel     function returns something >=0.  */
2054130809Smarcel  return remote_start_remote_dummy (uiout, dummy);
205519370Spst}
205619370Spst
205719370Spst/* Open a connection to a remote debugger.
205819370Spst   NAME is the filename used for communication.  */
205919370Spst
206019370Spststatic void
206198948Sobrienremote_open (char *name, int from_tty)
206219370Spst{
2063130809Smarcel  remote_open_1 (name, from_tty, &remote_ops, 0, 0);
206419370Spst}
206519370Spst
206698948Sobrien/* Just like remote_open, but with asynchronous support. */
206798948Sobrienstatic void
206898948Sobrienremote_async_open (char *name, int from_tty)
206998948Sobrien{
2070130809Smarcel  remote_open_1 (name, from_tty, &remote_async_ops, 0, 1);
207198948Sobrien}
207298948Sobrien
207319370Spst/* Open a connection to a remote debugger using the extended
207419370Spst   remote gdb protocol.  NAME is the filename used for communication.  */
207519370Spst
207619370Spststatic void
207798948Sobrienextended_remote_open (char *name, int from_tty)
207819370Spst{
2079130809Smarcel  remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */,
2080130809Smarcel		 0 /* async_p */);
208119370Spst}
208219370Spst
208398948Sobrien/* Just like extended_remote_open, but with asynchronous support. */
208498948Sobrienstatic void
208598948Sobrienextended_remote_async_open (char *name, int from_tty)
208698948Sobrien{
2087130809Smarcel  remote_open_1 (name, from_tty, &extended_async_remote_ops,
2088130809Smarcel		 1 /*extended_p */, 1 /* async_p */);
208998948Sobrien}
209098948Sobrien
209119370Spst/* Generic code for opening a connection to a remote target.  */
209246289Sdfr
209398948Sobrienstatic void
209498948Sobrieninit_all_packet_configs (void)
209598948Sobrien{
209698948Sobrien  int i;
209798948Sobrien  update_packet_config (&remote_protocol_e);
209898948Sobrien  update_packet_config (&remote_protocol_E);
209998948Sobrien  update_packet_config (&remote_protocol_P);
210098948Sobrien  update_packet_config (&remote_protocol_qSymbol);
2101130809Smarcel  update_packet_config (&remote_protocol_vcont);
210298948Sobrien  for (i = 0; i < NR_Z_PACKET_TYPES; i++)
210398948Sobrien    update_packet_config (&remote_protocol_Z[i]);
210498948Sobrien  /* Force remote_write_bytes to check whether target supports binary
210598948Sobrien     downloading. */
210698948Sobrien  update_packet_config (&remote_protocol_binary_download);
2107130809Smarcel  update_packet_config (&remote_protocol_qPart_auxv);
2108131086Smarcel  update_packet_config (&remote_protocol_qPart_dirty);
210998948Sobrien}
211019370Spst
211198948Sobrien/* Symbol look-up. */
211298948Sobrien
211319370Spststatic void
211498948Sobrienremote_check_symbols (struct objfile *objfile)
211519370Spst{
211698948Sobrien  struct remote_state *rs = get_remote_state ();
211798948Sobrien  char *msg, *reply, *tmp;
211898948Sobrien  struct minimal_symbol *sym;
211998948Sobrien  int end;
212098948Sobrien
212198948Sobrien  if (remote_protocol_qSymbol.support == PACKET_DISABLE)
212298948Sobrien    return;
212398948Sobrien
212498948Sobrien  msg   = alloca (rs->remote_packet_size);
212598948Sobrien  reply = alloca (rs->remote_packet_size);
212698948Sobrien
212798948Sobrien  /* Invite target to request symbol lookups. */
212898948Sobrien
212998948Sobrien  putpkt ("qSymbol::");
213098948Sobrien  getpkt (reply, (rs->remote_packet_size), 0);
213198948Sobrien  packet_ok (reply, &remote_protocol_qSymbol);
213298948Sobrien
213398948Sobrien  while (strncmp (reply, "qSymbol:", 8) == 0)
213498948Sobrien    {
213598948Sobrien      tmp = &reply[8];
213698948Sobrien      end = hex2bin (tmp, msg, strlen (tmp) / 2);
213798948Sobrien      msg[end] = '\0';
213898948Sobrien      sym = lookup_minimal_symbol (msg, NULL, NULL);
213998948Sobrien      if (sym == NULL)
214098948Sobrien	sprintf (msg, "qSymbol::%s", &reply[8]);
214198948Sobrien      else
2142130809Smarcel	sprintf (msg, "qSymbol:%s:%s",
214398948Sobrien		 paddr_nz (SYMBOL_VALUE_ADDRESS (sym)),
214498948Sobrien		 &reply[8]);
214598948Sobrien      putpkt (msg);
214698948Sobrien      getpkt (reply, (rs->remote_packet_size), 0);
214798948Sobrien    }
214898948Sobrien}
214998948Sobrien
2150130809Smarcelstatic struct serial *
2151130809Smarcelremote_serial_open (char *name)
215298948Sobrien{
2153130809Smarcel  static int udp_warning = 0;
215419370Spst
2155130809Smarcel  /* FIXME: Parsing NAME here is a hack.  But we want to warn here instead
2156130809Smarcel     of in ser-tcp.c, because it is the remote protocol assuming that the
2157130809Smarcel     serial connection is reliable and not the serial connection promising
2158130809Smarcel     to be.  */
2159130809Smarcel  if (!udp_warning && strncmp (name, "udp:", 4) == 0)
216098948Sobrien    {
2161130809Smarcel      warning ("The remote protocol may be unreliable over UDP.");
2162130809Smarcel      warning ("Some events may be lost, rendering further debugging "
2163130809Smarcel	       "impossible.");
2164130809Smarcel      udp_warning = 1;
216598948Sobrien    }
216698948Sobrien
2167130809Smarcel  return serial_open (name);
216898948Sobrien}
216998948Sobrien
217098948Sobrienstatic void
2171130809Smarcelremote_open_1 (char *name, int from_tty, struct target_ops *target,
2172130809Smarcel	       int extended_p, int async_p)
217398948Sobrien{
2174130809Smarcel  int ex;
217598948Sobrien  struct remote_state *rs = get_remote_state ();
217698948Sobrien  if (name == 0)
217798948Sobrien    error ("To open a remote debug connection, you need to specify what\n"
217898948Sobrien	   "serial device is attached to the remote system\n"
217998948Sobrien	   "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).");
218098948Sobrien
2181130809Smarcel  /* See FIXME above */
2182130809Smarcel  if (!async_p)
2183130809Smarcel    wait_forever_enabled_p = 1;
2184130809Smarcel
218598948Sobrien  target_preopen (from_tty);
218698948Sobrien
218798948Sobrien  unpush_target (target);
218898948Sobrien
2189130809Smarcel  remote_desc = remote_serial_open (name);
219019370Spst  if (!remote_desc)
219119370Spst    perror_with_name (name);
219219370Spst
219319370Spst  if (baud_rate != -1)
219419370Spst    {
219598948Sobrien      if (serial_setbaudrate (remote_desc, baud_rate))
219619370Spst	{
2197130809Smarcel	  /* The requested speed could not be set.  Error out to
2198130809Smarcel	     top level after closing remote_desc.  Take care to
2199130809Smarcel	     set remote_desc to NULL to avoid closing remote_desc
2200130809Smarcel	     more than once.  */
220198948Sobrien	  serial_close (remote_desc);
2202130809Smarcel	  remote_desc = NULL;
220319370Spst	  perror_with_name (name);
220419370Spst	}
220519370Spst    }
220619370Spst
220798948Sobrien  serial_raw (remote_desc);
220819370Spst
220919370Spst  /* If there is something sitting in the buffer we might take it as a
221019370Spst     response to a command, which would be bad.  */
221198948Sobrien  serial_flush_input (remote_desc);
221219370Spst
221319370Spst  if (from_tty)
221419370Spst    {
221519370Spst      puts_filtered ("Remote debugging using ");
221619370Spst      puts_filtered (name);
221719370Spst      puts_filtered ("\n");
221819370Spst    }
221998948Sobrien  push_target (target);		/* Switch to using remote target now */
222046289Sdfr
222198948Sobrien  init_all_packet_configs ();
222219370Spst
222319370Spst  general_thread = -2;
222498948Sobrien  continue_thread = -2;
222519370Spst
222698948Sobrien  /* Probe for ability to use "ThreadInfo" query, as required.  */
222798948Sobrien  use_threadinfo_query = 1;
222898948Sobrien  use_threadextra_query = 1;
222919370Spst
223046289Sdfr  /* Without this, some commands which require an active target (such
223146289Sdfr     as kill) won't work.  This variable serves (at least) double duty
223246289Sdfr     as both the pid of the target process (if it has such), and as a
223346289Sdfr     flag indicating that a target is active.  These functions should
223446289Sdfr     be split out into seperate variables, especially since GDB will
223546289Sdfr     someday have a notion of debugging several processes.  */
2236130809Smarcel
223798948Sobrien  inferior_ptid = pid_to_ptid (MAGIC_NULL_PID);
223846289Sdfr
2239130809Smarcel  if (async_p)
2240130809Smarcel    {
2241130809Smarcel      /* With this target we start out by owning the terminal. */
2242130809Smarcel      remote_async_terminal_ours_p = 1;
224398948Sobrien
2244130809Smarcel      /* FIXME: cagney/1999-09-23: During the initial connection it is
2245130809Smarcel	 assumed that the target is already ready and able to respond to
2246130809Smarcel	 requests. Unfortunately remote_start_remote() eventually calls
2247130809Smarcel	 wait_for_inferior() with no timeout.  wait_forever_enabled_p gets
2248130809Smarcel	 around this. Eventually a mechanism that allows
2249130809Smarcel	 wait_for_inferior() to expect/get timeouts will be
2250130809Smarcel	 implemented. */
2251130809Smarcel      wait_forever_enabled_p = 0;
2252130809Smarcel    }
225398948Sobrien
225498948Sobrien#ifdef SOLIB_CREATE_INFERIOR_HOOK
225598948Sobrien  /* First delete any symbols previously loaded from shared libraries. */
225698948Sobrien  no_shared_libraries (NULL, 0);
225798948Sobrien#endif
225898948Sobrien
2259130809Smarcel  /* Start the remote connection.  If error() or QUIT, discard this
2260130809Smarcel     target (we'd otherwise be in an inconsistent state) and then
2261130809Smarcel     propogate the error on up the exception chain.  This ensures that
2262130809Smarcel     the caller doesn't stumble along blindly assuming that the
2263130809Smarcel     function succeeded.  The CLI doesn't have this problem but other
2264130809Smarcel     UI's, such as MI do.
2265130809Smarcel
2266130809Smarcel     FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
2267130809Smarcel     this function should return an error indication letting the
2268130809Smarcel     caller restore the previous state.  Unfortunately the command
2269130809Smarcel     ``target remote'' is directly wired to this function making that
2270130809Smarcel     impossible.  On a positive note, the CLI side of this problem has
2271130809Smarcel     been fixed - the function set_cmd_context() makes it possible for
2272130809Smarcel     all the ``target ....'' commands to share a common callback
2273130809Smarcel     function.  See cli-dump.c.  */
2274130809Smarcel  ex = catch_exceptions (uiout,
2275130809Smarcel			 remote_start_remote, NULL,
2276130809Smarcel			 "Couldn't establish connection to remote"
2277130809Smarcel			 " target\n",
2278130809Smarcel			 RETURN_MASK_ALL);
2279130809Smarcel  if (ex < 0)
228046289Sdfr    {
228146289Sdfr      pop_target ();
2282130809Smarcel      if (async_p)
2283130809Smarcel	wait_forever_enabled_p = 1;
2284130809Smarcel      throw_exception (ex);
228546289Sdfr    }
228646289Sdfr
2287130809Smarcel  if (async_p)
2288130809Smarcel    wait_forever_enabled_p = 1;
228998948Sobrien
229046289Sdfr  if (extended_p)
229146289Sdfr    {
229298948Sobrien      /* Tell the remote that we are using the extended protocol.  */
229398948Sobrien      char *buf = alloca (rs->remote_packet_size);
229446289Sdfr      putpkt ("!");
229598948Sobrien      getpkt (buf, (rs->remote_packet_size), 0);
229646289Sdfr    }
229798948Sobrien#ifdef SOLIB_CREATE_INFERIOR_HOOK
2298130809Smarcel  /* FIXME: need a master target_open vector from which all
2299130809Smarcel     remote_opens can be called, so that stuff like this can
230098948Sobrien     go there.  Failing that, the following code must be copied
2301130809Smarcel     to the open function for any remote target that wants to
230298948Sobrien     support svr4 shared libraries.  */
230398948Sobrien
230498948Sobrien  /* Set up to detect and load shared libraries. */
230598948Sobrien  if (exec_bfd) 	/* No use without an exec file. */
230698948Sobrien    {
230798948Sobrien      SOLIB_CREATE_INFERIOR_HOOK (PIDGET (inferior_ptid));
230898948Sobrien      remote_check_symbols (symfile_objfile);
230998948Sobrien    }
231098948Sobrien#endif
231119370Spst}
231219370Spst
231319370Spst/* This takes a program previously attached to and detaches it.  After
231419370Spst   this is done, GDB can be used to debug some other program.  We
231519370Spst   better not have left any breakpoints in the target program or it'll
231619370Spst   die when it hits one.  */
231719370Spst
231819370Spststatic void
231998948Sobrienremote_detach (char *args, int from_tty)
232019370Spst{
232198948Sobrien  struct remote_state *rs = get_remote_state ();
232298948Sobrien  char *buf = alloca (rs->remote_packet_size);
232319370Spst
232419370Spst  if (args)
232519370Spst    error ("Argument given to \"detach\" when remotely debugging.");
232619370Spst
232719370Spst  /* Tell the remote target to detach.  */
232819370Spst  strcpy (buf, "D");
232998948Sobrien  remote_send (buf, (rs->remote_packet_size));
233019370Spst
2331130809Smarcel  /* Unregister the file descriptor from the event loop. */
2332130809Smarcel  if (target_is_async_p ())
2333130809Smarcel    serial_async (remote_desc, NULL, 0);
2334130809Smarcel
233598948Sobrien  target_mourn_inferior ();
233619370Spst  if (from_tty)
233719370Spst    puts_filtered ("Ending remote debugging.\n");
233819370Spst}
233919370Spst
2340130809Smarcel/* Same as remote_detach, but don't send the "D" packet; just disconnect.  */
2341130809Smarcel
234298948Sobrienstatic void
2343130809Smarcelremote_disconnect (char *args, int from_tty)
234498948Sobrien{
234598948Sobrien  struct remote_state *rs = get_remote_state ();
234698948Sobrien  char *buf = alloca (rs->remote_packet_size);
234798948Sobrien
234898948Sobrien  if (args)
234998948Sobrien    error ("Argument given to \"detach\" when remotely debugging.");
235098948Sobrien
235198948Sobrien  /* Unregister the file descriptor from the event loop. */
235298948Sobrien  if (target_is_async_p ())
235398948Sobrien    serial_async (remote_desc, NULL, 0);
235498948Sobrien
235598948Sobrien  target_mourn_inferior ();
235698948Sobrien  if (from_tty)
235798948Sobrien    puts_filtered ("Ending remote debugging.\n");
235898948Sobrien}
235998948Sobrien
236019370Spst/* Convert hex digit A to a number.  */
236119370Spst
236298948Sobrienstatic int
236398948Sobrienfromhex (int a)
236419370Spst{
236519370Spst  if (a >= '0' && a <= '9')
236619370Spst    return a - '0';
236719370Spst  else if (a >= 'a' && a <= 'f')
236819370Spst    return a - 'a' + 10;
236946289Sdfr  else if (a >= 'A' && a <= 'F')
237046289Sdfr    return a - 'A' + 10;
237198948Sobrien  else
237219370Spst    error ("Reply contains invalid hex digit %d", a);
237319370Spst}
237419370Spst
237598948Sobrienstatic int
237698948Sobrienhex2bin (const char *hex, char *bin, int count)
237798948Sobrien{
237898948Sobrien  int i;
237998948Sobrien
238098948Sobrien  for (i = 0; i < count; i++)
238198948Sobrien    {
238298948Sobrien      if (hex[0] == 0 || hex[1] == 0)
238398948Sobrien	{
238498948Sobrien	  /* Hex string is short, or of uneven length.
238598948Sobrien	     Return the count that has been converted so far. */
238698948Sobrien	  return i;
238798948Sobrien	}
238898948Sobrien      *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
238998948Sobrien      hex += 2;
239098948Sobrien    }
239198948Sobrien  return i;
239298948Sobrien}
239398948Sobrien
239419370Spst/* Convert number NIB to a hex digit.  */
239519370Spst
239619370Spststatic int
239798948Sobrientohex (int nib)
239819370Spst{
239919370Spst  if (nib < 10)
240098948Sobrien    return '0' + nib;
240119370Spst  else
240298948Sobrien    return 'a' + nib - 10;
240319370Spst}
240498948Sobrien
240598948Sobrienstatic int
240698948Sobrienbin2hex (const char *bin, char *hex, int count)
240798948Sobrien{
240898948Sobrien  int i;
240998948Sobrien  /* May use a length, or a nul-terminated string as input. */
241098948Sobrien  if (count == 0)
241198948Sobrien    count = strlen (bin);
241298948Sobrien
241398948Sobrien  for (i = 0; i < count; i++)
241498948Sobrien    {
241598948Sobrien      *hex++ = tohex ((*bin >> 4) & 0xf);
241698948Sobrien      *hex++ = tohex (*bin++ & 0xf);
241798948Sobrien    }
241898948Sobrien  *hex = 0;
241998948Sobrien  return i;
242098948Sobrien}
242119370Spst
2422130809Smarcel/* Check for the availability of vCont.  This function should also check
2423130809Smarcel   the response.  */
242419370Spst
242519370Spststatic void
2426130809Smarcelremote_vcont_probe (struct remote_state *rs, char *buf)
242719370Spst{
2428130809Smarcel  strcpy (buf, "vCont?");
2429130809Smarcel  putpkt (buf);
2430130809Smarcel  getpkt (buf, rs->remote_packet_size, 0);
243119370Spst
2432130809Smarcel  /* Make sure that the features we assume are supported.  */
2433130809Smarcel  if (strncmp (buf, "vCont", 5) == 0)
2434130809Smarcel    {
2435130809Smarcel      char *p = &buf[5];
2436130809Smarcel      int support_s, support_S, support_c, support_C;
243719370Spst
2438130809Smarcel      support_s = 0;
2439130809Smarcel      support_S = 0;
2440130809Smarcel      support_c = 0;
2441130809Smarcel      support_C = 0;
2442130809Smarcel      while (p && *p == ';')
2443130809Smarcel	{
2444130809Smarcel	  p++;
2445130809Smarcel	  if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0))
2446130809Smarcel	    support_s = 1;
2447130809Smarcel	  else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0))
2448130809Smarcel	    support_S = 1;
2449130809Smarcel	  else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0))
2450130809Smarcel	    support_c = 1;
2451130809Smarcel	  else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0))
2452130809Smarcel	    support_C = 1;
245319370Spst
2454130809Smarcel	  p = strchr (p, ';');
2455130809Smarcel	}
245646289Sdfr
2457130809Smarcel      /* If s, S, c, and C are not all supported, we can't use vCont.  Clearing
2458130809Smarcel         BUF will make packet_ok disable the packet.  */
2459130809Smarcel      if (!support_s || !support_S || !support_c || !support_C)
2460130809Smarcel	buf[0] = 0;
2461130809Smarcel    }
246298948Sobrien
2463130809Smarcel  packet_ok (buf, &remote_protocol_vcont);
2464130809Smarcel}
246598948Sobrien
2466130809Smarcel/* Resume the remote inferior by using a "vCont" packet.  The thread
2467130809Smarcel   to be resumed is PTID; STEP and SIGGNAL indicate whether the
2468130809Smarcel   resumed thread should be single-stepped and/or signalled.  If PTID's
2469130809Smarcel   PID is -1, then all threads are resumed; the thread to be stepped and/or
2470130809Smarcel   signalled is given in the global INFERIOR_PTID.  This function returns
2471130809Smarcel   non-zero iff it resumes the inferior.
247298948Sobrien
2473130809Smarcel   This function issues a strict subset of all possible vCont commands at the
2474130809Smarcel   moment.  */
247598948Sobrien
2476130809Smarcelstatic int
2477130809Smarcelremote_vcont_resume (ptid_t ptid, int step, enum target_signal siggnal)
2478130809Smarcel{
2479130809Smarcel  struct remote_state *rs = get_remote_state ();
2480130809Smarcel  int pid = PIDGET (ptid);
2481130809Smarcel  char *buf = NULL, *outbuf;
2482130809Smarcel  struct cleanup *old_cleanup;
248398948Sobrien
2484130809Smarcel  buf = xmalloc (rs->remote_packet_size);
2485130809Smarcel  old_cleanup = make_cleanup (xfree, buf);
248698948Sobrien
2487130809Smarcel  if (remote_protocol_vcont.support == PACKET_SUPPORT_UNKNOWN)
2488130809Smarcel    remote_vcont_probe (rs, buf);
2489130809Smarcel
2490130809Smarcel  if (remote_protocol_vcont.support == PACKET_DISABLE)
2491130809Smarcel    {
2492130809Smarcel      do_cleanups (old_cleanup);
2493130809Smarcel      return 0;
249498948Sobrien    }
249598948Sobrien
2496130809Smarcel  /* If we could generate a wider range of packets, we'd have to worry
2497130809Smarcel     about overflowing BUF.  Should there be a generic
2498130809Smarcel     "multi-part-packet" packet?  */
2499130809Smarcel
2500130809Smarcel  if (PIDGET (inferior_ptid) == MAGIC_NULL_PID)
250119370Spst    {
2502130809Smarcel      /* MAGIC_NULL_PTID means that we don't have any active threads, so we
2503130809Smarcel	 don't have any PID numbers the inferior will understand.  Make sure
2504130809Smarcel	 to only send forms that do not specify a PID.  */
2505130809Smarcel      if (step && siggnal != TARGET_SIGNAL_0)
2506130809Smarcel	outbuf = xstrprintf ("vCont;S%02x", siggnal);
2507130809Smarcel      else if (step)
2508130809Smarcel	outbuf = xstrprintf ("vCont;s");
2509130809Smarcel      else if (siggnal != TARGET_SIGNAL_0)
2510130809Smarcel	outbuf = xstrprintf ("vCont;C%02x", siggnal);
2511130809Smarcel      else
2512130809Smarcel	outbuf = xstrprintf ("vCont;c");
251319370Spst    }
2514130809Smarcel  else if (pid == -1)
2515130809Smarcel    {
2516130809Smarcel      /* Resume all threads, with preference for INFERIOR_PTID.  */
2517130809Smarcel      if (step && siggnal != TARGET_SIGNAL_0)
2518130809Smarcel	outbuf = xstrprintf ("vCont;S%02x:%x;c", siggnal,
2519130809Smarcel			     PIDGET (inferior_ptid));
2520130809Smarcel      else if (step)
2521130809Smarcel	outbuf = xstrprintf ("vCont;s:%x;c", PIDGET (inferior_ptid));
2522130809Smarcel      else if (siggnal != TARGET_SIGNAL_0)
2523130809Smarcel	outbuf = xstrprintf ("vCont;C%02x:%x;c", siggnal,
2524130809Smarcel			     PIDGET (inferior_ptid));
2525130809Smarcel      else
2526130809Smarcel	outbuf = xstrprintf ("vCont;c");
2527130809Smarcel    }
252819370Spst  else
2529130809Smarcel    {
2530130809Smarcel      /* Scheduler locking; resume only PTID.  */
2531130809Smarcel      if (step && siggnal != TARGET_SIGNAL_0)
2532130809Smarcel	outbuf = xstrprintf ("vCont;S%02x:%x", siggnal, pid);
2533130809Smarcel      else if (step)
2534130809Smarcel	outbuf = xstrprintf ("vCont;s:%x", pid);
2535130809Smarcel      else if (siggnal != TARGET_SIGNAL_0)
2536130809Smarcel	outbuf = xstrprintf ("vCont;C%02x:%x", siggnal, pid);
2537130809Smarcel      else
2538130809Smarcel	outbuf = xstrprintf ("vCont;c:%x", pid);
2539130809Smarcel    }
254019370Spst
2541130809Smarcel  gdb_assert (outbuf && strlen (outbuf) < rs->remote_packet_size);
2542130809Smarcel  make_cleanup (xfree, outbuf);
2543130809Smarcel
2544130809Smarcel  putpkt (outbuf);
2545130809Smarcel
2546130809Smarcel  do_cleanups (old_cleanup);
2547130809Smarcel
2548130809Smarcel  return 1;
254919370Spst}
255098948Sobrien
2551130809Smarcel/* Tell the remote machine to resume.  */
2552130809Smarcel
2553130809Smarcelstatic enum target_signal last_sent_signal = TARGET_SIGNAL_0;
2554130809Smarcel
2555130809Smarcelstatic int last_sent_step;
2556130809Smarcel
255798948Sobrienstatic void
2558130809Smarcelremote_resume (ptid_t ptid, int step, enum target_signal siggnal)
255998948Sobrien{
256098948Sobrien  struct remote_state *rs = get_remote_state ();
256198948Sobrien  char *buf = alloca (rs->remote_packet_size);
256298948Sobrien  int pid = PIDGET (ptid);
256398948Sobrien  char *p;
256498948Sobrien
256598948Sobrien  last_sent_signal = siggnal;
256698948Sobrien  last_sent_step = step;
256798948Sobrien
256898948Sobrien  /* A hook for when we need to do something at the last moment before
256998948Sobrien     resumption.  */
257098948Sobrien  if (target_resume_hook)
257198948Sobrien    (*target_resume_hook) ();
257298948Sobrien
2573130809Smarcel  /* The vCont packet doesn't need to specify threads via Hc.  */
2574130809Smarcel  if (remote_vcont_resume (ptid, step, siggnal))
2575130809Smarcel    return;
2576130809Smarcel
2577130809Smarcel  /* All other supported resume packets do use Hc, so call set_thread.  */
2578130809Smarcel  if (pid == -1)
2579130809Smarcel    set_thread (0, 0);		/* run any thread */
2580130809Smarcel  else
2581130809Smarcel    set_thread (pid, 0);	/* run this thread */
2582130809Smarcel
258398948Sobrien  /* The s/S/c/C packets do not return status.  So if the target does
258498948Sobrien     not support the S or C packets, the debug agent returns an empty
258598948Sobrien     string which is detected in remote_wait().  This protocol defect
258698948Sobrien     is fixed in the e/E packets. */
258798948Sobrien
258898948Sobrien  if (step && step_range_end)
258998948Sobrien    {
259098948Sobrien      /* If the target does not support the 'E' packet, we try the 'S'
259198948Sobrien	 packet.  Ideally we would fall back to the 'e' packet if that
259298948Sobrien	 too is not supported.  But that would require another copy of
259398948Sobrien	 the code to issue the 'e' packet (and fall back to 's' if not
259498948Sobrien	 supported) in remote_wait().  */
2595130809Smarcel
259698948Sobrien      if (siggnal != TARGET_SIGNAL_0)
259798948Sobrien	{
259898948Sobrien	  if (remote_protocol_E.support != PACKET_DISABLE)
259998948Sobrien	    {
260098948Sobrien	      p = buf;
260198948Sobrien	      *p++ = 'E';
260298948Sobrien	      *p++ = tohex (((int) siggnal >> 4) & 0xf);
260398948Sobrien	      *p++ = tohex (((int) siggnal) & 0xf);
260498948Sobrien	      *p++ = ',';
260598948Sobrien	      p += hexnumstr (p, (ULONGEST) step_range_start);
260698948Sobrien	      *p++ = ',';
260798948Sobrien	      p += hexnumstr (p, (ULONGEST) step_range_end);
260898948Sobrien	      *p++ = 0;
260998948Sobrien
261098948Sobrien	      putpkt (buf);
261198948Sobrien	      getpkt (buf, (rs->remote_packet_size), 0);
261298948Sobrien
261398948Sobrien	      if (packet_ok (buf, &remote_protocol_E) == PACKET_OK)
2614130809Smarcel		return;
261598948Sobrien	    }
261698948Sobrien	}
261798948Sobrien      else
261898948Sobrien	{
261998948Sobrien	  if (remote_protocol_e.support != PACKET_DISABLE)
262098948Sobrien	    {
262198948Sobrien	      p = buf;
262298948Sobrien	      *p++ = 'e';
262398948Sobrien	      p += hexnumstr (p, (ULONGEST) step_range_start);
262498948Sobrien	      *p++ = ',';
262598948Sobrien	      p += hexnumstr (p, (ULONGEST) step_range_end);
262698948Sobrien	      *p++ = 0;
262798948Sobrien
262898948Sobrien	      putpkt (buf);
262998948Sobrien	      getpkt (buf, (rs->remote_packet_size), 0);
263098948Sobrien
263198948Sobrien	      if (packet_ok (buf, &remote_protocol_e) == PACKET_OK)
2632130809Smarcel		return;
263398948Sobrien	    }
263498948Sobrien	}
263598948Sobrien    }
263698948Sobrien
263798948Sobrien  if (siggnal != TARGET_SIGNAL_0)
263898948Sobrien    {
263998948Sobrien      buf[0] = step ? 'S' : 'C';
264098948Sobrien      buf[1] = tohex (((int) siggnal >> 4) & 0xf);
2641130809Smarcel      buf[2] = tohex (((int) siggnal) & 0xf);
264298948Sobrien      buf[3] = '\0';
264398948Sobrien    }
264498948Sobrien  else
264598948Sobrien    strcpy (buf, step ? "s" : "c");
2646130809Smarcel
264798948Sobrien  putpkt (buf);
2648130809Smarcel}
264998948Sobrien
2650130809Smarcel/* Same as remote_resume, but with async support. */
2651130809Smarcelstatic void
2652130809Smarcelremote_async_resume (ptid_t ptid, int step, enum target_signal siggnal)
2653130809Smarcel{
2654130809Smarcel  remote_resume (ptid, step, siggnal);
2655130809Smarcel
265698948Sobrien  /* We are about to start executing the inferior, let's register it
265798948Sobrien     with the event loop. NOTE: this is the one place where all the
265898948Sobrien     execution commands end up. We could alternatively do this in each
265998948Sobrien     of the execution commands in infcmd.c.*/
266098948Sobrien  /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
266198948Sobrien     into infcmd.c in order to allow inferior function calls to work
266298948Sobrien     NOT asynchronously. */
266398948Sobrien  if (event_loop_p && target_can_async_p ())
266498948Sobrien    target_async (inferior_event_handler, 0);
266598948Sobrien  /* Tell the world that the target is now executing. */
266698948Sobrien  /* FIXME: cagney/1999-09-23: Is it the targets responsibility to set
266798948Sobrien     this?  Instead, should the client of target just assume (for
266898948Sobrien     async targets) that the target is going to start executing?  Is
266998948Sobrien     this information already found in the continuation block?  */
267098948Sobrien  if (target_is_async_p ())
267198948Sobrien    target_executing = 1;
267298948Sobrien}
267319370Spst
267419370Spst
267598948Sobrien/* Set up the signal handler for SIGINT, while the target is
267698948Sobrien   executing, ovewriting the 'regular' SIGINT signal handler. */
267798948Sobrienstatic void
267898948Sobrieninitialize_sigint_signal_handler (void)
267998948Sobrien{
268098948Sobrien  sigint_remote_token =
268198948Sobrien    create_async_signal_handler (async_remote_interrupt, NULL);
268298948Sobrien  signal (SIGINT, handle_remote_sigint);
268398948Sobrien}
268446289Sdfr
268598948Sobrien/* Signal handler for SIGINT, while the target is executing. */
268619370Spststatic void
268798948Sobrienhandle_remote_sigint (int sig)
268819370Spst{
268998948Sobrien  signal (sig, handle_remote_sigint_twice);
269098948Sobrien  sigint_remote_twice_token =
269198948Sobrien    create_async_signal_handler (async_remote_interrupt_twice, NULL);
269298948Sobrien  mark_async_signal_handler_wrapper (sigint_remote_token);
269346289Sdfr}
269498948Sobrien
269598948Sobrien/* Signal handler for SIGINT, installed after SIGINT has already been
269698948Sobrien   sent once.  It will take effect the second time that the user sends
269798948Sobrien   a ^C. */
269819370Spststatic void
269998948Sobrienhandle_remote_sigint_twice (int sig)
270019370Spst{
270198948Sobrien  signal (sig, handle_sigint);
270298948Sobrien  sigint_remote_twice_token =
270398948Sobrien    create_async_signal_handler (inferior_event_handler_wrapper, NULL);
270498948Sobrien  mark_async_signal_handler_wrapper (sigint_remote_twice_token);
270598948Sobrien}
270619370Spst
270798948Sobrien/* Perform the real interruption of the target execution, in response
270898948Sobrien   to a ^C. */
270998948Sobrienstatic void
271098948Sobrienasync_remote_interrupt (gdb_client_data arg)
271198948Sobrien{
271298948Sobrien  if (remote_debug)
271398948Sobrien    fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
271446289Sdfr
271598948Sobrien  target_stop ();
271698948Sobrien}
271798948Sobrien
271898948Sobrien/* Perform interrupt, if the first attempt did not succeed. Just give
271998948Sobrien   up on the target alltogether. */
272098948Sobrienvoid
272198948Sobrienasync_remote_interrupt_twice (gdb_client_data arg)
272298948Sobrien{
272398948Sobrien  if (remote_debug)
272498948Sobrien    fprintf_unfiltered (gdb_stdlog, "remote_interrupt_twice called\n");
272598948Sobrien  /* Do something only if the target was not killed by the previous
272698948Sobrien     cntl-C. */
272798948Sobrien  if (target_executing)
272846289Sdfr    {
272946289Sdfr      interrupt_query ();
273098948Sobrien      signal (SIGINT, handle_remote_sigint);
273146289Sdfr    }
273219370Spst}
273319370Spst
273498948Sobrien/* Reinstall the usual SIGINT handlers, after the target has
273598948Sobrien   stopped. */
273698948Sobrienstatic void
273798948Sobriencleanup_sigint_signal_handler (void *dummy)
273898948Sobrien{
273998948Sobrien  signal (SIGINT, handle_sigint);
274098948Sobrien  if (sigint_remote_twice_token)
274198948Sobrien    delete_async_signal_handler ((struct async_signal_handler **) & sigint_remote_twice_token);
274298948Sobrien  if (sigint_remote_token)
274398948Sobrien    delete_async_signal_handler ((struct async_signal_handler **) & sigint_remote_token);
274498948Sobrien}
274598948Sobrien
274698948Sobrien/* Send ^C to target to halt it.  Target will respond, and send us a
274798948Sobrien   packet.  */
274898948Sobrienstatic void (*ofunc) (int);
274998948Sobrien
275098948Sobrien/* The command line interface's stop routine. This function is installed
275198948Sobrien   as a signal handler for SIGINT. The first time a user requests a
275298948Sobrien   stop, we call remote_stop to send a break or ^C. If there is no
275398948Sobrien   response from the target (it didn't stop when the user requested it),
275498948Sobrien   we ask the user if he'd like to detach from the target. */
275598948Sobrienstatic void
275698948Sobrienremote_interrupt (int signo)
275798948Sobrien{
275898948Sobrien  /* If this doesn't work, try more severe steps. */
275998948Sobrien  signal (signo, remote_interrupt_twice);
276098948Sobrien
276198948Sobrien  if (remote_debug)
276298948Sobrien    fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
276398948Sobrien
276498948Sobrien  target_stop ();
276598948Sobrien}
276698948Sobrien
276798948Sobrien/* The user typed ^C twice.  */
276898948Sobrien
276998948Sobrienstatic void
277098948Sobrienremote_interrupt_twice (int signo)
277198948Sobrien{
277298948Sobrien  signal (signo, ofunc);
277398948Sobrien  interrupt_query ();
277498948Sobrien  signal (signo, remote_interrupt);
277598948Sobrien}
277698948Sobrien
277798948Sobrien/* This is the generic stop called via the target vector. When a target
277898948Sobrien   interrupt is requested, either by the command line or the GUI, we
277998948Sobrien   will eventually end up here. */
278098948Sobrienstatic void
278198948Sobrienremote_stop (void)
278298948Sobrien{
278398948Sobrien  /* Send a break or a ^C, depending on user preference.  */
278498948Sobrien  if (remote_debug)
278598948Sobrien    fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
278698948Sobrien
278798948Sobrien  if (remote_break)
278898948Sobrien    serial_send_break (remote_desc);
278998948Sobrien  else
279098948Sobrien    serial_write (remote_desc, "\003", 1);
279198948Sobrien}
279298948Sobrien
279319370Spst/* Ask the user what to do when an interrupt is received.  */
279419370Spst
279519370Spststatic void
279698948Sobrieninterrupt_query (void)
279719370Spst{
279819370Spst  target_terminal_ours ();
279919370Spst
280019370Spst  if (query ("Interrupted while waiting for the program.\n\
280119370SpstGive up (and stop debugging it)? "))
280219370Spst    {
280319370Spst      target_mourn_inferior ();
280498948Sobrien      throw_exception (RETURN_QUIT);
280519370Spst    }
280619370Spst
280719370Spst  target_terminal_inferior ();
280819370Spst}
280919370Spst
281098948Sobrien/* Enable/disable target terminal ownership.  Most targets can use
281198948Sobrien   terminal groups to control terminal ownership.  Remote targets are
281298948Sobrien   different in that explicit transfer of ownership to/from GDB/target
281398948Sobrien   is required. */
281498948Sobrien
281598948Sobrienstatic void
281698948Sobrienremote_async_terminal_inferior (void)
281798948Sobrien{
281898948Sobrien  /* FIXME: cagney/1999-09-27: Shouldn't need to test for
281998948Sobrien     sync_execution here.  This function should only be called when
282098948Sobrien     GDB is resuming the inferior in the forground.  A background
282198948Sobrien     resume (``run&'') should leave GDB in control of the terminal and
282298948Sobrien     consequently should not call this code. */
282398948Sobrien  if (!sync_execution)
282498948Sobrien    return;
282598948Sobrien  /* FIXME: cagney/1999-09-27: Closely related to the above.  Make
282698948Sobrien     calls target_terminal_*() idenpotent. The event-loop GDB talking
282798948Sobrien     to an asynchronous target with a synchronous command calls this
282898948Sobrien     function from both event-top.c and infrun.c/infcmd.c.  Once GDB
282998948Sobrien     stops trying to transfer the terminal to the target when it
283098948Sobrien     shouldn't this guard can go away.  */
283198948Sobrien  if (!remote_async_terminal_ours_p)
283298948Sobrien    return;
283398948Sobrien  delete_file_handler (input_fd);
283498948Sobrien  remote_async_terminal_ours_p = 0;
283598948Sobrien  initialize_sigint_signal_handler ();
283698948Sobrien  /* NOTE: At this point we could also register our selves as the
283798948Sobrien     recipient of all input.  Any characters typed could then be
283898948Sobrien     passed on down to the target. */
283998948Sobrien}
284098948Sobrien
284198948Sobrienstatic void
284298948Sobrienremote_async_terminal_ours (void)
284398948Sobrien{
284498948Sobrien  /* See FIXME in remote_async_terminal_inferior. */
284598948Sobrien  if (!sync_execution)
284698948Sobrien    return;
284798948Sobrien  /* See FIXME in remote_async_terminal_inferior. */
284898948Sobrien  if (remote_async_terminal_ours_p)
284998948Sobrien    return;
285098948Sobrien  cleanup_sigint_signal_handler (NULL);
285198948Sobrien  add_file_handler (input_fd, stdin_event_handler, 0);
285298948Sobrien  remote_async_terminal_ours_p = 1;
285398948Sobrien}
285498948Sobrien
285519370Spst/* If nonzero, ignore the next kill.  */
285646289Sdfr
285719370Spstint kill_kludge;
285819370Spst
285946289Sdfrvoid
286098948Sobrienremote_console_output (char *msg)
286146289Sdfr{
286246289Sdfr  char *p;
286319370Spst
286498948Sobrien  for (p = msg; p[0] && p[1]; p += 2)
286546289Sdfr    {
286646289Sdfr      char tb[2];
286746289Sdfr      char c = fromhex (p[0]) * 16 + fromhex (p[1]);
286846289Sdfr      tb[0] = c;
286946289Sdfr      tb[1] = 0;
287098948Sobrien      fputs_unfiltered (tb, gdb_stdtarg);
287146289Sdfr    }
287298948Sobrien  gdb_flush (gdb_stdtarg);
287346289Sdfr}
287446289Sdfr
287598948Sobrien/* Wait until the remote machine stops, then return,
287698948Sobrien   storing status in STATUS just as `wait' would.
2877130809Smarcel   Returns "pid", which in the case of a multi-threaded
287898948Sobrien   remote OS, is the thread-id.  */
287946289Sdfr
288098948Sobrienstatic ptid_t
288198948Sobrienremote_wait (ptid_t ptid, struct target_waitstatus *status)
288219370Spst{
288398948Sobrien  struct remote_state *rs = get_remote_state ();
288498948Sobrien  unsigned char *buf = alloca (rs->remote_packet_size);
2885130809Smarcel  ULONGEST thread_num = -1;
2886130809Smarcel  ULONGEST addr;
288719370Spst
288819370Spst  status->kind = TARGET_WAITKIND_EXITED;
288919370Spst  status->value.integer = 0;
289019370Spst
289119370Spst  while (1)
289219370Spst    {
289319370Spst      unsigned char *p;
289419370Spst
289546289Sdfr      ofunc = signal (SIGINT, remote_interrupt);
289698948Sobrien      getpkt (buf, (rs->remote_packet_size), 1);
289719370Spst      signal (SIGINT, ofunc);
289819370Spst
289946289Sdfr      /* This is a hook for when we need to do something (perhaps the
290098948Sobrien         collection of trace data) every time the target stops.  */
290146289Sdfr      if (target_wait_loop_hook)
290246289Sdfr	(*target_wait_loop_hook) ();
290346289Sdfr
2904130809Smarcel      remote_stopped_by_watchpoint_p = 0;
2905130809Smarcel
290619370Spst      switch (buf[0])
290719370Spst	{
290819370Spst	case 'E':		/* Error of some sort */
290919370Spst	  warning ("Remote failure reply: %s", buf);
291019370Spst	  continue;
2911130809Smarcel	case 'F':		/* File-I/O request */
2912130809Smarcel	  remote_fileio_request (buf);
2913130809Smarcel	  continue;
291419370Spst	case 'T':		/* Status with PC, SP, FP, ... */
291519370Spst	  {
291619370Spst	    int i;
2917130809Smarcel	    char regs[MAX_REGISTER_SIZE];
291819370Spst
291919370Spst	    /* Expedited reply, containing Signal, {regno, reg} repeat */
292019370Spst	    /*  format is:  'Tssn...:r...;n...:r...;n...:r...;#cc', where
292198948Sobrien	       ss = signal number
292298948Sobrien	       n... = register number
292398948Sobrien	       r... = register contents
292498948Sobrien	     */
292519370Spst	    p = &buf[3];	/* after Txx */
292619370Spst
292719370Spst	    while (*p)
292819370Spst	      {
292919370Spst		unsigned char *p1;
293019370Spst		char *p_temp;
293198948Sobrien		int fieldsize;
2932130809Smarcel		LONGEST pnum = 0;
293319370Spst
2934130809Smarcel		/* If the packet contains a register number save it in pnum
2935130809Smarcel		   and set p1 to point to the character following it.
2936130809Smarcel		   Otherwise p1 points to p.  */
293719370Spst
2938130809Smarcel		/* If this packet is an awatch packet, don't parse the 'a'
2939130809Smarcel		   as a register number.  */
2940130809Smarcel
2941130809Smarcel		if (strncmp (p, "awatch", strlen("awatch")) != 0)
2942130809Smarcel		  {
2943130809Smarcel		    /* Read the ``P'' register number.  */
2944130809Smarcel		    pnum = strtol (p, &p_temp, 16);
2945130809Smarcel		    p1 = (unsigned char *) p_temp;
2946130809Smarcel		  }
2947130809Smarcel		else
2948130809Smarcel		  p1 = p;
2949130809Smarcel
295098948Sobrien		if (p1 == p)	/* No register number present here */
295119370Spst		  {
2952130809Smarcel		    p1 = (unsigned char *) strchr (p, ':');
295319370Spst		    if (p1 == NULL)
295446289Sdfr		      warning ("Malformed packet(a) (missing colon): %s\n\
295519370SpstPacket: '%s'\n",
295619370Spst			       p, buf);
2957130809Smarcel		    if (strncmp (p, "thread", p1 - p) == 0)
295819370Spst		      {
295946289Sdfr			p_temp = unpack_varlen_hex (++p1, &thread_num);
296046289Sdfr			record_currthread (thread_num);
296146289Sdfr			p = (unsigned char *) p_temp;
296219370Spst		      }
2963130809Smarcel		    else if ((strncmp (p, "watch", p1 - p) == 0)
2964130809Smarcel			     || (strncmp (p, "rwatch", p1 - p) == 0)
2965130809Smarcel			     || (strncmp (p, "awatch", p1 - p) == 0))
2966130809Smarcel		      {
2967130809Smarcel			remote_stopped_by_watchpoint_p = 1;
2968130809Smarcel			p = unpack_varlen_hex (++p1, &addr);
2969130809Smarcel			remote_watch_data_address = (CORE_ADDR)addr;
2970130809Smarcel		      }
2971130809Smarcel		    else
2972130809Smarcel 		      {
2973130809Smarcel 			/* Silently skip unknown optional info.  */
2974130809Smarcel 			p_temp = strchr (p1 + 1, ';');
2975130809Smarcel 			if (p_temp)
2976130809Smarcel			  p = (unsigned char *) p_temp;
2977130809Smarcel 		      }
297819370Spst		  }
297919370Spst		else
298019370Spst		  {
298198948Sobrien		    struct packet_reg *reg = packet_reg_from_pnum (rs, pnum);
298219370Spst		    p = p1;
298319370Spst
298419370Spst		    if (*p++ != ':')
2985130809Smarcel		      error ("Malformed packet(b) (missing colon): %s\nPacket: '%s'\n",
2986130809Smarcel			     p, buf);
298719370Spst
298898948Sobrien		    if (reg == NULL)
2989130809Smarcel		      error ("Remote sent bad register number %s: %s\nPacket: '%s'\n",
2990130809Smarcel			     phex_nz (pnum, 0), p, buf);
299119370Spst
2992130809Smarcel		    fieldsize = hex2bin (p, regs, DEPRECATED_REGISTER_RAW_SIZE (reg->regnum));
299398948Sobrien		    p += 2 * fieldsize;
2994130809Smarcel		    if (fieldsize < DEPRECATED_REGISTER_RAW_SIZE (reg->regnum))
299598948Sobrien		      warning ("Remote reply is too short: %s", buf);
299698948Sobrien		    supply_register (reg->regnum, regs);
299719370Spst		  }
299819370Spst
299919370Spst		if (*p++ != ';')
3000130809Smarcel		  error ("Remote register badly formatted: %s\nhere: %s", buf, p);
300119370Spst	      }
300219370Spst	  }
300319370Spst	  /* fall through */
300419370Spst	case 'S':		/* Old style status, just signal only */
300519370Spst	  status->kind = TARGET_WAITKIND_STOPPED;
300619370Spst	  status->value.sig = (enum target_signal)
300719370Spst	    (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
300819370Spst
300998948Sobrien	  if (buf[3] == 'p')
301098948Sobrien	    {
301198948Sobrien	      thread_num = strtol ((const char *) &buf[4], NULL, 16);
301298948Sobrien	      record_currthread (thread_num);
301398948Sobrien	    }
301419370Spst	  goto got_status;
301519370Spst	case 'W':		/* Target exited */
301619370Spst	  {
301719370Spst	    /* The remote process exited.  */
301819370Spst	    status->kind = TARGET_WAITKIND_EXITED;
301919370Spst	    status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
302019370Spst	    goto got_status;
302119370Spst	  }
302219370Spst	case 'X':
302319370Spst	  status->kind = TARGET_WAITKIND_SIGNALLED;
302419370Spst	  status->value.sig = (enum target_signal)
302519370Spst	    (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
302619370Spst	  kill_kludge = 1;
302719370Spst
302819370Spst	  goto got_status;
302919370Spst	case 'O':		/* Console output */
303046289Sdfr	  remote_console_output (buf + 1);
303119370Spst	  continue;
303219370Spst	case '\0':
303319370Spst	  if (last_sent_signal != TARGET_SIGNAL_0)
303419370Spst	    {
303519370Spst	      /* Zero length reply means that we tried 'S' or 'C' and
303698948Sobrien	         the remote system doesn't support it.  */
303719370Spst	      target_terminal_ours_for_output ();
303819370Spst	      printf_filtered
303919370Spst		("Can't send signals to this remote system.  %s not sent.\n",
304019370Spst		 target_signal_to_name (last_sent_signal));
304119370Spst	      last_sent_signal = TARGET_SIGNAL_0;
304219370Spst	      target_terminal_inferior ();
304319370Spst
304419370Spst	      strcpy ((char *) buf, last_sent_step ? "s" : "c");
304519370Spst	      putpkt ((char *) buf);
304619370Spst	      continue;
304719370Spst	    }
304819370Spst	  /* else fallthrough */
304919370Spst	default:
305019370Spst	  warning ("Invalid remote reply: %s", buf);
305119370Spst	  continue;
305219370Spst	}
305319370Spst    }
305498948Sobriengot_status:
305519370Spst  if (thread_num != -1)
305619370Spst    {
305798948Sobrien      return pid_to_ptid (thread_num);
305898948Sobrien    }
305998948Sobrien  return inferior_ptid;
306098948Sobrien}
306198948Sobrien
306298948Sobrien/* Async version of remote_wait. */
306398948Sobrienstatic ptid_t
306498948Sobrienremote_async_wait (ptid_t ptid, struct target_waitstatus *status)
306598948Sobrien{
306698948Sobrien  struct remote_state *rs = get_remote_state ();
306798948Sobrien  unsigned char *buf = alloca (rs->remote_packet_size);
3068130809Smarcel  ULONGEST thread_num = -1;
3069130809Smarcel  ULONGEST addr;
307098948Sobrien
307198948Sobrien  status->kind = TARGET_WAITKIND_EXITED;
307298948Sobrien  status->value.integer = 0;
307398948Sobrien
3074130809Smarcel  remote_stopped_by_watchpoint_p = 0;
3075130809Smarcel
307698948Sobrien  while (1)
307798948Sobrien    {
307898948Sobrien      unsigned char *p;
307998948Sobrien
308098948Sobrien      if (!target_is_async_p ())
308198948Sobrien	ofunc = signal (SIGINT, remote_interrupt);
308298948Sobrien      /* FIXME: cagney/1999-09-27: If we're in async mode we should
308398948Sobrien         _never_ wait for ever -> test on target_is_async_p().
308498948Sobrien         However, before we do that we need to ensure that the caller
308598948Sobrien         knows how to take the target into/out of async mode. */
308698948Sobrien      getpkt (buf, (rs->remote_packet_size), wait_forever_enabled_p);
308798948Sobrien      if (!target_is_async_p ())
308898948Sobrien	signal (SIGINT, ofunc);
308998948Sobrien
309098948Sobrien      /* This is a hook for when we need to do something (perhaps the
309198948Sobrien         collection of trace data) every time the target stops.  */
309298948Sobrien      if (target_wait_loop_hook)
309398948Sobrien	(*target_wait_loop_hook) ();
309498948Sobrien
309598948Sobrien      switch (buf[0])
309619370Spst	{
309798948Sobrien	case 'E':		/* Error of some sort */
309898948Sobrien	  warning ("Remote failure reply: %s", buf);
309998948Sobrien	  continue;
3100130809Smarcel	case 'F':		/* File-I/O request */
3101130809Smarcel	  remote_fileio_request (buf);
3102130809Smarcel	  continue;
310398948Sobrien	case 'T':		/* Status with PC, SP, FP, ... */
310498948Sobrien	  {
310598948Sobrien	    int i;
3106130809Smarcel	    char regs[MAX_REGISTER_SIZE];
310798948Sobrien
310898948Sobrien	    /* Expedited reply, containing Signal, {regno, reg} repeat */
310998948Sobrien	    /*  format is:  'Tssn...:r...;n...:r...;n...:r...;#cc', where
311098948Sobrien	       ss = signal number
311198948Sobrien	       n... = register number
311298948Sobrien	       r... = register contents
311398948Sobrien	     */
311498948Sobrien	    p = &buf[3];	/* after Txx */
311598948Sobrien
311698948Sobrien	    while (*p)
311798948Sobrien	      {
311898948Sobrien		unsigned char *p1;
311998948Sobrien		char *p_temp;
312098948Sobrien		int fieldsize;
3121130809Smarcel		long pnum = 0;
312298948Sobrien
3123130809Smarcel		/* If the packet contains a register number, save it in pnum
3124130809Smarcel		   and set p1 to point to the character following it.
3125130809Smarcel		   Otherwise p1 points to p.  */
312698948Sobrien
3127130809Smarcel		/* If this packet is an awatch packet, don't parse the 'a'
3128130809Smarcel		   as a register number.  */
3129130809Smarcel
3130130809Smarcel		if (!strncmp (p, "awatch", strlen ("awatch")) != 0)
3131130809Smarcel		  {
3132130809Smarcel		    /* Read the register number.  */
3133130809Smarcel		    pnum = strtol (p, &p_temp, 16);
3134130809Smarcel		    p1 = (unsigned char *) p_temp;
3135130809Smarcel		  }
3136130809Smarcel		else
3137130809Smarcel		  p1 = p;
3138130809Smarcel
313998948Sobrien		if (p1 == p)	/* No register number present here */
314098948Sobrien		  {
3141130809Smarcel		    p1 = (unsigned char *) strchr (p, ':');
314298948Sobrien		    if (p1 == NULL)
3143130809Smarcel		      error ("Malformed packet(a) (missing colon): %s\nPacket: '%s'\n",
3144130809Smarcel			     p, buf);
3145130809Smarcel		    if (strncmp (p, "thread", p1 - p) == 0)
314698948Sobrien		      {
314798948Sobrien			p_temp = unpack_varlen_hex (++p1, &thread_num);
314898948Sobrien			record_currthread (thread_num);
314998948Sobrien			p = (unsigned char *) p_temp;
315098948Sobrien		      }
3151130809Smarcel		    else if ((strncmp (p, "watch", p1 - p) == 0)
3152130809Smarcel			     || (strncmp (p, "rwatch", p1 - p) == 0)
3153130809Smarcel			     || (strncmp (p, "awatch", p1 - p) == 0))
3154130809Smarcel		      {
3155130809Smarcel			remote_stopped_by_watchpoint_p = 1;
3156130809Smarcel			p = unpack_varlen_hex (++p1, &addr);
3157130809Smarcel			remote_watch_data_address = (CORE_ADDR)addr;
3158130809Smarcel		      }
3159130809Smarcel		    else
3160130809Smarcel 		      {
3161130809Smarcel 			/* Silently skip unknown optional info.  */
3162130809Smarcel 			p_temp = (unsigned char *) strchr (p1 + 1, ';');
3163130809Smarcel 			if (p_temp)
3164130809Smarcel			  p = p_temp;
3165130809Smarcel 		      }
316698948Sobrien		  }
3167130809Smarcel
316898948Sobrien		else
316998948Sobrien		  {
317098948Sobrien		    struct packet_reg *reg = packet_reg_from_pnum (rs, pnum);
317198948Sobrien		    p = p1;
317298948Sobrien		    if (*p++ != ':')
3173130809Smarcel		      error ("Malformed packet(b) (missing colon): %s\nPacket: '%s'\n",
3174130809Smarcel			     p, buf);
317598948Sobrien
317698948Sobrien		    if (reg == NULL)
3177130809Smarcel		      error ("Remote sent bad register number %ld: %s\nPacket: '%s'\n",
3178130809Smarcel			     pnum, p, buf);
317998948Sobrien
3180130809Smarcel		    fieldsize = hex2bin (p, regs, DEPRECATED_REGISTER_RAW_SIZE (reg->regnum));
318198948Sobrien		    p += 2 * fieldsize;
3182130809Smarcel		    if (fieldsize < DEPRECATED_REGISTER_RAW_SIZE (reg->regnum))
318398948Sobrien		      warning ("Remote reply is too short: %s", buf);
318498948Sobrien		    supply_register (reg->regnum, regs);
318598948Sobrien		  }
318698948Sobrien
318798948Sobrien		if (*p++ != ';')
3188130809Smarcel		  error ("Remote register badly formatted: %s\nhere: %s",
3189130809Smarcel			 buf, p);
319098948Sobrien	      }
319198948Sobrien	  }
319298948Sobrien	  /* fall through */
319398948Sobrien	case 'S':		/* Old style status, just signal only */
319498948Sobrien	  status->kind = TARGET_WAITKIND_STOPPED;
319598948Sobrien	  status->value.sig = (enum target_signal)
319698948Sobrien	    (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
319798948Sobrien
319898948Sobrien	  if (buf[3] == 'p')
319998948Sobrien	    {
320098948Sobrien	      thread_num = strtol ((const char *) &buf[4], NULL, 16);
320198948Sobrien	      record_currthread (thread_num);
320298948Sobrien	    }
320398948Sobrien	  goto got_status;
320498948Sobrien	case 'W':		/* Target exited */
320598948Sobrien	  {
320698948Sobrien	    /* The remote process exited.  */
320798948Sobrien	    status->kind = TARGET_WAITKIND_EXITED;
320898948Sobrien	    status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
320998948Sobrien	    goto got_status;
321098948Sobrien	  }
321198948Sobrien	case 'X':
321298948Sobrien	  status->kind = TARGET_WAITKIND_SIGNALLED;
321398948Sobrien	  status->value.sig = (enum target_signal)
321498948Sobrien	    (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
321598948Sobrien	  kill_kludge = 1;
321698948Sobrien
321798948Sobrien	  goto got_status;
321898948Sobrien	case 'O':		/* Console output */
321998948Sobrien	  remote_console_output (buf + 1);
322098948Sobrien	  /* Return immediately to the event loop. The event loop will
322198948Sobrien             still be waiting on the inferior afterwards. */
322298948Sobrien          status->kind = TARGET_WAITKIND_IGNORE;
322398948Sobrien          goto got_status;
322498948Sobrien	case '\0':
322598948Sobrien	  if (last_sent_signal != TARGET_SIGNAL_0)
322698948Sobrien	    {
322798948Sobrien	      /* Zero length reply means that we tried 'S' or 'C' and
322898948Sobrien	         the remote system doesn't support it.  */
322998948Sobrien	      target_terminal_ours_for_output ();
323098948Sobrien	      printf_filtered
323198948Sobrien		("Can't send signals to this remote system.  %s not sent.\n",
323298948Sobrien		 target_signal_to_name (last_sent_signal));
323398948Sobrien	      last_sent_signal = TARGET_SIGNAL_0;
323498948Sobrien	      target_terminal_inferior ();
323598948Sobrien
323698948Sobrien	      strcpy ((char *) buf, last_sent_step ? "s" : "c");
323798948Sobrien	      putpkt ((char *) buf);
323898948Sobrien	      continue;
323998948Sobrien	    }
324098948Sobrien	  /* else fallthrough */
324198948Sobrien	default:
324298948Sobrien	  warning ("Invalid remote reply: %s", buf);
324398948Sobrien	  continue;
324419370Spst	}
324519370Spst    }
324698948Sobriengot_status:
324798948Sobrien  if (thread_num != -1)
324898948Sobrien    {
324998948Sobrien      return pid_to_ptid (thread_num);
325098948Sobrien    }
325198948Sobrien  return inferior_ptid;
325219370Spst}
325319370Spst
325419370Spst/* Number of bytes of registers this stub implements.  */
325546289Sdfr
325619370Spststatic int register_bytes_found;
325719370Spst
325819370Spst/* Read the remote registers into the block REGS.  */
325998948Sobrien/* Currently we just read all the registers, so we don't use regnum.  */
326046289Sdfr
326119370Spststatic void
326298948Sobrienremote_fetch_registers (int regnum)
326319370Spst{
326498948Sobrien  struct remote_state *rs = get_remote_state ();
326598948Sobrien  char *buf = alloca (rs->remote_packet_size);
326619370Spst  int i;
326719370Spst  char *p;
326898948Sobrien  char *regs = alloca (rs->sizeof_g_packet);
326919370Spst
327098948Sobrien  set_thread (PIDGET (inferior_ptid), 1);
327119370Spst
327298948Sobrien  if (regnum >= 0)
327398948Sobrien    {
327498948Sobrien      struct packet_reg *reg = packet_reg_from_regnum (rs, regnum);
327598948Sobrien      gdb_assert (reg != NULL);
327698948Sobrien      if (!reg->in_g_packet)
327798948Sobrien	internal_error (__FILE__, __LINE__,
327898948Sobrien			"Attempt to fetch a non G-packet register when this "
327998948Sobrien			"remote.c does not support the p-packet.");
328098948Sobrien    }
328198948Sobrien
328219370Spst  sprintf (buf, "g");
328398948Sobrien  remote_send (buf, (rs->remote_packet_size));
328419370Spst
328598948Sobrien  /* Save the size of the packet sent to us by the target.  Its used
328698948Sobrien     as a heuristic when determining the max size of packets that the
328798948Sobrien     target can safely receive. */
328898948Sobrien  if ((rs->actual_register_packet_size) == 0)
328998948Sobrien    (rs->actual_register_packet_size) = strlen (buf);
329046289Sdfr
329119370Spst  /* Unimplemented registers read as all bits zero.  */
329298948Sobrien  memset (regs, 0, rs->sizeof_g_packet);
329319370Spst
329419370Spst  /* We can get out of synch in various cases.  If the first character
329519370Spst     in the buffer is not a hex character, assume that has happened
329619370Spst     and try to fetch another packet to read.  */
329719370Spst  while ((buf[0] < '0' || buf[0] > '9')
329846289Sdfr	 && (buf[0] < 'a' || buf[0] > 'f')
329946289Sdfr	 && buf[0] != 'x')	/* New: unavailable register value */
330019370Spst    {
330119370Spst      if (remote_debug)
330298948Sobrien	fprintf_unfiltered (gdb_stdlog,
330398948Sobrien			    "Bad register packet; fetching a new packet\n");
330498948Sobrien      getpkt (buf, (rs->remote_packet_size), 0);
330519370Spst    }
330619370Spst
330719370Spst  /* Reply describes registers byte by byte, each byte encoded as two
330819370Spst     hex characters.  Suck them all up, then supply them to the
330919370Spst     register cacheing/storage mechanism.  */
331019370Spst
331119370Spst  p = buf;
331298948Sobrien  for (i = 0; i < rs->sizeof_g_packet; i++)
331319370Spst    {
331419370Spst      if (p[0] == 0)
331519370Spst	break;
331619370Spst      if (p[1] == 0)
331719370Spst	{
331819370Spst	  warning ("Remote reply is of odd length: %s", buf);
331919370Spst	  /* Don't change register_bytes_found in this case, and don't
332019370Spst	     print a second warning.  */
332119370Spst	  goto supply_them;
332219370Spst	}
332346289Sdfr      if (p[0] == 'x' && p[1] == 'x')
332498948Sobrien	regs[i] = 0;		/* 'x' */
332546289Sdfr      else
332646289Sdfr	regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
332719370Spst      p += 2;
332819370Spst    }
332919370Spst
333019370Spst  if (i != register_bytes_found)
333119370Spst    {
333219370Spst      register_bytes_found = i;
333398948Sobrien      if (REGISTER_BYTES_OK_P ()
333498948Sobrien	  && !REGISTER_BYTES_OK (i))
333519370Spst	warning ("Remote reply is too short: %s", buf);
333619370Spst    }
333798948Sobrien
333898948Sobrien supply_them:
333946289Sdfr  {
334098948Sobrien    int i;
334198948Sobrien    for (i = 0; i < NUM_REGS + NUM_PSEUDO_REGS; i++)
334298948Sobrien      {
334398948Sobrien	struct packet_reg *r = &rs->regs[i];
334498948Sobrien	if (r->in_g_packet)
334598948Sobrien	  {
3346130809Smarcel	    if (r->offset * 2 >= strlen (buf))
3347130809Smarcel	      /* A short packet that didn't include the register's
3348130809Smarcel                 value, this implies that the register is zero (and
3349130809Smarcel                 not that the register is unavailable).  Supply that
3350130809Smarcel                 zero value.  */
3351130809Smarcel	      regcache_raw_supply (current_regcache, r->regnum, NULL);
3352130809Smarcel	    else if (buf[r->offset * 2] == 'x')
3353130809Smarcel	      {
3354130809Smarcel		gdb_assert (r->offset * 2 < strlen (buf));
3355130809Smarcel		/* The register isn't available, mark it as such (at
3356130809Smarcel                   the same time setting the value to zero).  */
3357130809Smarcel		regcache_raw_supply (current_regcache, r->regnum, NULL);
3358130809Smarcel		set_register_cached (i, -1);
3359130809Smarcel	      }
3360130809Smarcel	    else
3361130809Smarcel	      regcache_raw_supply (current_regcache, r->regnum,
3362130809Smarcel				   regs + r->offset);
336398948Sobrien	  }
336498948Sobrien      }
336546289Sdfr  }
336619370Spst}
336719370Spst
336819370Spst/* Prepare to store registers.  Since we may send them all (using a
336919370Spst   'G' request), we have to read out the ones we don't want to change
337019370Spst   first.  */
337119370Spst
337298948Sobrienstatic void
337398948Sobrienremote_prepare_to_store (void)
337419370Spst{
3375130809Smarcel  struct remote_state *rs = get_remote_state ();
3376130809Smarcel  int i;
3377130809Smarcel  char buf[MAX_REGISTER_SIZE];
3378130809Smarcel
337919370Spst  /* Make sure the entire registers array is valid.  */
338098948Sobrien  switch (remote_protocol_P.support)
338198948Sobrien    {
338298948Sobrien    case PACKET_DISABLE:
338398948Sobrien    case PACKET_SUPPORT_UNKNOWN:
3384130809Smarcel      /* Make sure all the necessary registers are cached.  */
3385130809Smarcel      for (i = 0; i < NUM_REGS; i++)
3386130809Smarcel	if (rs->regs[i].in_g_packet)
3387130809Smarcel	  regcache_raw_read (current_regcache, rs->regs[i].regnum, buf);
338898948Sobrien      break;
338998948Sobrien    case PACKET_ENABLE:
339098948Sobrien      break;
339198948Sobrien    }
339219370Spst}
339319370Spst
339498948Sobrien/* Helper: Attempt to store REGNUM using the P packet.  Return fail IFF
339598948Sobrien   packet was not recognized. */
339619370Spst
339798948Sobrienstatic int
339898948Sobrienstore_register_using_P (int regnum)
339998948Sobrien{
340098948Sobrien  struct remote_state *rs = get_remote_state ();
340198948Sobrien  struct packet_reg *reg = packet_reg_from_regnum (rs, regnum);
340298948Sobrien  /* Try storing a single register.  */
340398948Sobrien  char *buf = alloca (rs->remote_packet_size);
3404130809Smarcel  char regp[MAX_REGISTER_SIZE];
340598948Sobrien  char *p;
340698948Sobrien  int i;
340798948Sobrien
340898948Sobrien  sprintf (buf, "P%s=", phex_nz (reg->pnum, 0));
340998948Sobrien  p = buf + strlen (buf);
341098948Sobrien  regcache_collect (reg->regnum, regp);
3411130809Smarcel  bin2hex (regp, p, DEPRECATED_REGISTER_RAW_SIZE (reg->regnum));
341298948Sobrien  remote_send (buf, rs->remote_packet_size);
341398948Sobrien
341498948Sobrien  return buf[0] != '\0';
341598948Sobrien}
341698948Sobrien
341798948Sobrien
341898948Sobrien/* Store register REGNUM, or all registers if REGNUM == -1, from the contents
341998948Sobrien   of the register cache buffer.  FIXME: ignores errors.  */
342098948Sobrien
342119370Spststatic void
342298948Sobrienremote_store_registers (int regnum)
342319370Spst{
342498948Sobrien  struct remote_state *rs = get_remote_state ();
342598948Sobrien  char *buf;
342698948Sobrien  char *regs;
342719370Spst  int i;
342819370Spst  char *p;
342919370Spst
343098948Sobrien  set_thread (PIDGET (inferior_ptid), 1);
343119370Spst
343298948Sobrien  if (regnum >= 0)
343319370Spst    {
343498948Sobrien      switch (remote_protocol_P.support)
343519370Spst	{
343698948Sobrien	case PACKET_DISABLE:
343798948Sobrien	  break;
343898948Sobrien	case PACKET_ENABLE:
343998948Sobrien	  if (store_register_using_P (regnum))
344098948Sobrien	    return;
344198948Sobrien	  else
344298948Sobrien	    error ("Protocol error: P packet not recognized by stub");
344398948Sobrien	case PACKET_SUPPORT_UNKNOWN:
344498948Sobrien	  if (store_register_using_P (regnum))
344598948Sobrien	    {
344698948Sobrien	      /* The stub recognized the 'P' packet.  Remember this.  */
344798948Sobrien	      remote_protocol_P.support = PACKET_ENABLE;
344898948Sobrien	      return;
344998948Sobrien	    }
345098948Sobrien	  else
345198948Sobrien	    {
345298948Sobrien	      /* The stub does not support the 'P' packet.  Use 'G'
345398948Sobrien	         instead, and don't try using 'P' in the future (it
345498948Sobrien	         will just waste our time).  */
345598948Sobrien	      remote_protocol_P.support = PACKET_DISABLE;
345698948Sobrien	      break;
345798948Sobrien	    }
345819370Spst	}
345919370Spst    }
346019370Spst
346198948Sobrien  /* Extract all the registers in the regcache copying them into a
346298948Sobrien     local buffer.  */
346398948Sobrien  {
346498948Sobrien    int i;
346598948Sobrien    regs = alloca (rs->sizeof_g_packet);
346698948Sobrien    memset (regs, rs->sizeof_g_packet, 0);
346798948Sobrien    for (i = 0; i < NUM_REGS + NUM_PSEUDO_REGS; i++)
346898948Sobrien      {
346998948Sobrien	struct packet_reg *r = &rs->regs[i];
347098948Sobrien	if (r->in_g_packet)
347198948Sobrien	  regcache_collect (r->regnum, regs + r->offset);
347298948Sobrien      }
347398948Sobrien  }
347419370Spst
347519370Spst  /* Command describes registers byte by byte,
347619370Spst     each byte encoded as two hex characters.  */
347798948Sobrien  buf = alloca (rs->remote_packet_size);
347898948Sobrien  p = buf;
347998948Sobrien  *p++ = 'G';
348019370Spst  /* remote_prepare_to_store insures that register_bytes_found gets set.  */
348198948Sobrien  bin2hex (regs, p, register_bytes_found);
348298948Sobrien  remote_send (buf, (rs->remote_packet_size));
348319370Spst}
348419370Spst
348546289Sdfr
348646289Sdfr/* Return the number of hex digits in num.  */
348746289Sdfr
348846289Sdfrstatic int
348998948Sobrienhexnumlen (ULONGEST num)
349046289Sdfr{
349146289Sdfr  int i;
349246289Sdfr
349346289Sdfr  for (i = 0; num != 0; i++)
349446289Sdfr    num >>= 4;
349546289Sdfr
349646289Sdfr  return max (i, 1);
349746289Sdfr}
349846289Sdfr
349998948Sobrien/* Set BUF to the minimum number of hex digits representing NUM.  */
350046289Sdfr
350146289Sdfrstatic int
350298948Sobrienhexnumstr (char *buf, ULONGEST num)
350346289Sdfr{
350446289Sdfr  int len = hexnumlen (num);
350598948Sobrien  return hexnumnstr (buf, num, len);
350698948Sobrien}
350746289Sdfr
350846289Sdfr
350998948Sobrien/* Set BUF to the hex digits representing NUM, padded to WIDTH characters.  */
351098948Sobrien
351198948Sobrienstatic int
351298948Sobrienhexnumnstr (char *buf, ULONGEST num, int width)
351398948Sobrien{
351498948Sobrien  int i;
351598948Sobrien
351698948Sobrien  buf[width] = '\0';
351798948Sobrien
351898948Sobrien  for (i = width - 1; i >= 0; i--)
351946289Sdfr    {
352098948Sobrien      buf[i] = "0123456789abcdef"[(num & 0xf)];
352146289Sdfr      num >>= 4;
352246289Sdfr    }
352346289Sdfr
352498948Sobrien  return width;
352546289Sdfr}
352646289Sdfr
352746289Sdfr/* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
352846289Sdfr
352946289Sdfrstatic CORE_ADDR
353098948Sobrienremote_address_masked (CORE_ADDR addr)
353146289Sdfr{
353246289Sdfr  if (remote_address_size > 0
353346289Sdfr      && remote_address_size < (sizeof (ULONGEST) * 8))
353446289Sdfr    {
353546289Sdfr      /* Only create a mask when that mask can safely be constructed
353646289Sdfr         in a ULONGEST variable. */
353746289Sdfr      ULONGEST mask = 1;
353846289Sdfr      mask = (mask << remote_address_size) - 1;
353946289Sdfr      addr &= mask;
354046289Sdfr    }
354146289Sdfr  return addr;
354246289Sdfr}
354346289Sdfr
354446289Sdfr/* Determine whether the remote target supports binary downloading.
354546289Sdfr   This is accomplished by sending a no-op memory write of zero length
354646289Sdfr   to the target at the specified address. It does not suffice to send
354746289Sdfr   the whole packet, since many stubs strip the eighth bit and subsequently
354898948Sobrien   compute a wrong checksum, which causes real havoc with remote_write_bytes.
354998948Sobrien
355098948Sobrien   NOTE: This can still lose if the serial line is not eight-bit
355198948Sobrien   clean. In cases like this, the user should clear "remote
355298948Sobrien   X-packet". */
355398948Sobrien
355446289Sdfrstatic void
355598948Sobriencheck_binary_download (CORE_ADDR addr)
355646289Sdfr{
355798948Sobrien  struct remote_state *rs = get_remote_state ();
355898948Sobrien  switch (remote_protocol_binary_download.support)
355946289Sdfr    {
356098948Sobrien    case PACKET_DISABLE:
356198948Sobrien      break;
356298948Sobrien    case PACKET_ENABLE:
356398948Sobrien      break;
356498948Sobrien    case PACKET_SUPPORT_UNKNOWN:
356598948Sobrien      {
356698948Sobrien	char *buf = alloca (rs->remote_packet_size);
356798948Sobrien	char *p;
3568130809Smarcel
356998948Sobrien	p = buf;
357098948Sobrien	*p++ = 'X';
357198948Sobrien	p += hexnumstr (p, (ULONGEST) addr);
357298948Sobrien	*p++ = ',';
357398948Sobrien	p += hexnumstr (p, (ULONGEST) 0);
357498948Sobrien	*p++ = ':';
357598948Sobrien	*p = '\0';
3576130809Smarcel
357798948Sobrien	putpkt_binary (buf, (int) (p - buf));
357898948Sobrien	getpkt (buf, (rs->remote_packet_size), 0);
357946289Sdfr
358098948Sobrien	if (buf[0] == '\0')
358198948Sobrien	  {
358298948Sobrien	    if (remote_debug)
358398948Sobrien	      fprintf_unfiltered (gdb_stdlog,
358498948Sobrien				  "binary downloading NOT suppported by target\n");
358598948Sobrien	    remote_protocol_binary_download.support = PACKET_DISABLE;
358698948Sobrien	  }
358798948Sobrien	else
358898948Sobrien	  {
358998948Sobrien	    if (remote_debug)
359098948Sobrien	      fprintf_unfiltered (gdb_stdlog,
359198948Sobrien				  "binary downloading suppported by target\n");
359298948Sobrien	    remote_protocol_binary_download.support = PACKET_ENABLE;
359398948Sobrien	  }
359498948Sobrien	break;
359598948Sobrien      }
359646289Sdfr    }
359746289Sdfr}
359846289Sdfr
359919370Spst/* Write memory data directly to the remote machine.
360019370Spst   This does not inform the data cache; the data cache uses this.
360119370Spst   MEMADDR is the address in the remote memory space.
360219370Spst   MYADDR is the address of the buffer in our space.
360319370Spst   LEN is the number of bytes.
360419370Spst
360598948Sobrien   Returns number of bytes transferred, or 0 (setting errno) for
360698948Sobrien   error.  Only transfer a single packet. */
360719370Spst
3608130809Smarcelint
360998948Sobrienremote_write_bytes (CORE_ADDR memaddr, char *myaddr, int len)
361019370Spst{
361198948Sobrien  unsigned char *buf;
361298948Sobrien  unsigned char *p;
361398948Sobrien  unsigned char *plen;
361498948Sobrien  long sizeof_buf;
361598948Sobrien  int plenlen;
361698948Sobrien  int todo;
361798948Sobrien  int nr_bytes;
3618130809Smarcel  int payload_size;
3619130809Smarcel  unsigned char *payload_start;
362046289Sdfr
3621130809Smarcel  /* Verify that the target can support a binary download.  */
362246289Sdfr  check_binary_download (memaddr);
362346289Sdfr
3624130809Smarcel  /* Compute the size, and then allocate space for the largest
3625130809Smarcel     possible packet.  Include space for an extra trailing NUL.  */
3626130809Smarcel  sizeof_buf = get_memory_write_packet_size () + 1;
362798948Sobrien  buf = alloca (sizeof_buf);
362819370Spst
3629130809Smarcel  /* Compute the size of the actual payload by subtracting out the
3630130809Smarcel     packet header and footer overhead: "$M<memaddr>,<len>:...#nn".  */
3631130809Smarcel  payload_size = (get_memory_write_packet_size () - (strlen ("$M,:#NN")
3632130809Smarcel						     + hexnumlen (memaddr)
3633130809Smarcel						     + hexnumlen (len)));
363419370Spst
3635130809Smarcel  /* Construct the packet header: "[MX]<memaddr>,<len>:".   */
363698948Sobrien
3637130809Smarcel  /* Append "[XM]".  Compute a best guess of the number of bytes
363898948Sobrien     actually transfered. */
3639130809Smarcel  p = buf;
364098948Sobrien  switch (remote_protocol_binary_download.support)
364146289Sdfr    {
364298948Sobrien    case PACKET_ENABLE:
364398948Sobrien      *p++ = 'X';
364498948Sobrien      /* Best guess at number of bytes that will fit. */
3645130809Smarcel      todo = min (len, payload_size);
364698948Sobrien      break;
364798948Sobrien    case PACKET_DISABLE:
364898948Sobrien      *p++ = 'M';
364998948Sobrien      /* num bytes that will fit */
3650130809Smarcel      todo = min (len, payload_size / 2);
365198948Sobrien      break;
365298948Sobrien    case PACKET_SUPPORT_UNKNOWN:
365398948Sobrien      internal_error (__FILE__, __LINE__,
365498948Sobrien		      "remote_write_bytes: bad internal state");
365598948Sobrien    default:
365698948Sobrien      internal_error (__FILE__, __LINE__, "bad switch");
365798948Sobrien    }
3658130809Smarcel
3659130809Smarcel  /* Append "<memaddr>".  */
366098948Sobrien  memaddr = remote_address_masked (memaddr);
366198948Sobrien  p += hexnumstr (p, (ULONGEST) memaddr);
3662130809Smarcel
3663130809Smarcel  /* Append ",".  */
366498948Sobrien  *p++ = ',';
3665130809Smarcel
3666130809Smarcel  /* Append <len>.  Retain the location/size of <len>.  It may need to
3667130809Smarcel     be adjusted once the packet body has been created.  */
366898948Sobrien  plen = p;
366998948Sobrien  plenlen = hexnumstr (p, (ULONGEST) todo);
367098948Sobrien  p += plenlen;
3671130809Smarcel
3672130809Smarcel  /* Append ":".  */
367398948Sobrien  *p++ = ':';
367498948Sobrien  *p = '\0';
3675130809Smarcel
3676130809Smarcel  /* Append the packet body.  */
3677130809Smarcel  payload_start = p;
367898948Sobrien  switch (remote_protocol_binary_download.support)
367998948Sobrien    {
368098948Sobrien    case PACKET_ENABLE:
368198948Sobrien      /* Binary mode.  Send target system values byte by byte, in
368298948Sobrien	 increasing byte addresses.  Only escape certain critical
368398948Sobrien	 characters.  */
368498948Sobrien      for (nr_bytes = 0;
3685130809Smarcel	   (nr_bytes < todo) && (p - payload_start) < payload_size;
368698948Sobrien	   nr_bytes++)
368719370Spst	{
368898948Sobrien	  switch (myaddr[nr_bytes] & 0xff)
368946289Sdfr	    {
369098948Sobrien	    case '$':
369198948Sobrien	    case '#':
369298948Sobrien	    case 0x7d:
369398948Sobrien	      /* These must be escaped */
369498948Sobrien	      *p++ = 0x7d;
369598948Sobrien	      *p++ = (myaddr[nr_bytes] & 0xff) ^ 0x20;
369698948Sobrien	      break;
369798948Sobrien	    default:
369898948Sobrien	      *p++ = myaddr[nr_bytes] & 0xff;
369998948Sobrien	      break;
370046289Sdfr	    }
370146289Sdfr	}
370298948Sobrien      if (nr_bytes < todo)
370346289Sdfr	{
3704130809Smarcel	  /* Escape chars have filled up the buffer prematurely,
370598948Sobrien	     and we have actually sent fewer bytes than planned.
370698948Sobrien	     Fix-up the length field of the packet.  Use the same
370798948Sobrien	     number of characters as before.  */
370898948Sobrien	  plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen);
370998948Sobrien	  *plen = ':';  /* overwrite \0 from hexnumnstr() */
371046289Sdfr	}
371198948Sobrien      break;
371298948Sobrien    case PACKET_DISABLE:
371398948Sobrien      /* Normal mode: Send target system values byte by byte, in
371498948Sobrien	 increasing byte addresses.  Each byte is encoded as a two hex
371598948Sobrien	 value.  */
371698948Sobrien      nr_bytes = bin2hex (myaddr, p, todo);
371798948Sobrien      p += 2 * nr_bytes;
371898948Sobrien      break;
371998948Sobrien    case PACKET_SUPPORT_UNKNOWN:
372098948Sobrien      internal_error (__FILE__, __LINE__,
372198948Sobrien		      "remote_write_bytes: bad internal state");
372298948Sobrien    default:
372398948Sobrien      internal_error (__FILE__, __LINE__, "bad switch");
372419370Spst    }
3725130809Smarcel
372698948Sobrien  putpkt_binary (buf, (int) (p - buf));
372798948Sobrien  getpkt (buf, sizeof_buf, 0);
3728130809Smarcel
372998948Sobrien  if (buf[0] == 'E')
373098948Sobrien    {
373198948Sobrien      /* There is no correspondance between what the remote protocol
373298948Sobrien	 uses for errors and errno codes.  We would like a cleaner way
373398948Sobrien	 of representing errors (big enough to include errno codes,
373498948Sobrien	 bfd_error codes, and others).  But for now just return EIO.  */
373598948Sobrien      errno = EIO;
373698948Sobrien      return 0;
373798948Sobrien    }
3738130809Smarcel
373998948Sobrien  /* Return NR_BYTES, not TODO, in case escape chars caused us to send fewer
374098948Sobrien     bytes than we'd planned.  */
374198948Sobrien  return nr_bytes;
374219370Spst}
374319370Spst
374419370Spst/* Read memory data directly from the remote machine.
374519370Spst   This does not use the data cache; the data cache uses this.
374619370Spst   MEMADDR is the address in the remote memory space.
374719370Spst   MYADDR is the address of the buffer in our space.
374819370Spst   LEN is the number of bytes.
374919370Spst
375019370Spst   Returns number of bytes transferred, or 0 for error.  */
375119370Spst
375298948Sobrien/* NOTE: cagney/1999-10-18: This function (and its siblings in other
375398948Sobrien   remote targets) shouldn't attempt to read the entire buffer.
375498948Sobrien   Instead it should read a single packet worth of data and then
375598948Sobrien   return the byte size of that packet to the caller.  The caller (its
375698948Sobrien   caller and its callers caller ;-) already contains code for
375798948Sobrien   handling partial reads. */
375898948Sobrien
3759130809Smarcelint
376098948Sobrienremote_read_bytes (CORE_ADDR memaddr, char *myaddr, int len)
376119370Spst{
376298948Sobrien  char *buf;
376346289Sdfr  int max_buf_size;		/* Max size of packet output buffer */
376498948Sobrien  long sizeof_buf;
376546289Sdfr  int origlen;
376619370Spst
376798948Sobrien  /* Create a buffer big enough for this packet. */
376898948Sobrien  max_buf_size = get_memory_read_packet_size ();
376998948Sobrien  sizeof_buf = max_buf_size + 1; /* Space for trailing NUL */
377098948Sobrien  buf = alloca (sizeof_buf);
377146289Sdfr
377246289Sdfr  origlen = len;
377346289Sdfr  while (len > 0)
377419370Spst    {
377546289Sdfr      char *p;
377646289Sdfr      int todo;
377746289Sdfr      int i;
377819370Spst
377998948Sobrien      todo = min (len, max_buf_size / 2);	/* num bytes that will fit */
378046289Sdfr
378146289Sdfr      /* construct "m"<memaddr>","<len>" */
378246289Sdfr      /* sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo); */
378346289Sdfr      memaddr = remote_address_masked (memaddr);
378446289Sdfr      p = buf;
378546289Sdfr      *p++ = 'm';
378646289Sdfr      p += hexnumstr (p, (ULONGEST) memaddr);
378746289Sdfr      *p++ = ',';
378846289Sdfr      p += hexnumstr (p, (ULONGEST) todo);
378946289Sdfr      *p = '\0';
379046289Sdfr
379119370Spst      putpkt (buf);
379298948Sobrien      getpkt (buf, sizeof_buf, 0);
379319370Spst
3794130809Smarcel      if (buf[0] == 'E'
3795130809Smarcel	  && isxdigit (buf[1]) && isxdigit (buf[2])
3796130809Smarcel	  && buf[3] == '\0')
379719370Spst	{
379819370Spst	  /* There is no correspondance between what the remote protocol uses
379919370Spst	     for errors and errno codes.  We would like a cleaner way of
380019370Spst	     representing errors (big enough to include errno codes, bfd_error
380119370Spst	     codes, and others).  But for now just return EIO.  */
380219370Spst	  errno = EIO;
380319370Spst	  return 0;
380419370Spst	}
380519370Spst
380698948Sobrien      /* Reply describes memory byte by byte,
380798948Sobrien         each byte encoded as two hex characters.  */
380819370Spst
380919370Spst      p = buf;
381098948Sobrien      if ((i = hex2bin (p, myaddr, todo)) < todo)
381119370Spst	{
381298948Sobrien	  /* Reply is short.  This means that we were able to read
381398948Sobrien	     only part of what we wanted to. */
381498948Sobrien	  return i + (origlen - len);
381519370Spst	}
381646289Sdfr      myaddr += todo;
381746289Sdfr      memaddr += todo;
381846289Sdfr      len -= todo;
381919370Spst    }
382046289Sdfr  return origlen;
382119370Spst}
382219370Spst
382346289Sdfr/* Read or write LEN bytes from inferior memory at MEMADDR,
382498948Sobrien   transferring to or from debugger address BUFFER.  Write to inferior if
382598948Sobrien   SHOULD_WRITE is nonzero.  Returns length of data written or read; 0
382698948Sobrien   for error.  TARGET is unused.  */
382719370Spst
382819370Spststatic int
382998948Sobrienremote_xfer_memory (CORE_ADDR mem_addr, char *buffer, int mem_len,
383098948Sobrien		    int should_write, struct mem_attrib *attrib,
383198948Sobrien		    struct target_ops *target)
383219370Spst{
383398948Sobrien  CORE_ADDR targ_addr;
383498948Sobrien  int targ_len;
383598948Sobrien  int res;
383698948Sobrien
3837130809Smarcel  /* Should this be the selected frame?  */
3838130809Smarcel  gdbarch_remote_translate_xfer_address (current_gdbarch, current_regcache,
3839130809Smarcel					 mem_addr, mem_len,
3840130809Smarcel					 &targ_addr, &targ_len);
384198948Sobrien  if (targ_len <= 0)
384246289Sdfr    return 0;
384346289Sdfr
384498948Sobrien  if (should_write)
384598948Sobrien    res = remote_write_bytes (targ_addr, buffer, targ_len);
384698948Sobrien  else
384798948Sobrien    res = remote_read_bytes (targ_addr, buffer, targ_len);
384898948Sobrien
384998948Sobrien  return res;
385019370Spst}
385119370Spst
385219370Spststatic void
385398948Sobrienremote_files_info (struct target_ops *ignore)
385419370Spst{
385519370Spst  puts_filtered ("Debugging a target over a serial line.\n");
385619370Spst}
385719370Spst
385819370Spst/* Stuff for dealing with the packets which are part of this protocol.
385919370Spst   See comment at top of file for details.  */
386019370Spst
386119370Spst/* Read a single character from the remote end, masking it down to 7 bits. */
386219370Spst
386319370Spststatic int
386498948Sobrienreadchar (int timeout)
386519370Spst{
386619370Spst  int ch;
386719370Spst
386898948Sobrien  ch = serial_readchar (remote_desc, timeout);
386919370Spst
387098948Sobrien  if (ch >= 0)
387198948Sobrien    return (ch & 0x7f);
387298948Sobrien
387398948Sobrien  switch ((enum serial_rc) ch)
387419370Spst    {
387519370Spst    case SERIAL_EOF:
387698948Sobrien      target_mourn_inferior ();
387719370Spst      error ("Remote connection closed");
387898948Sobrien      /* no return */
387919370Spst    case SERIAL_ERROR:
388019370Spst      perror_with_name ("Remote communication error");
388198948Sobrien      /* no return */
388219370Spst    case SERIAL_TIMEOUT:
388398948Sobrien      break;
388419370Spst    }
388598948Sobrien  return ch;
388619370Spst}
388719370Spst
388846289Sdfr/* Send the command in BUF to the remote machine, and read the reply
388946289Sdfr   into BUF.  Report an error if we get an error reply.  */
389019370Spst
389119370Spststatic void
389298948Sobrienremote_send (char *buf,
389398948Sobrien	     long sizeof_buf)
389419370Spst{
389519370Spst  putpkt (buf);
389698948Sobrien  getpkt (buf, sizeof_buf, 0);
389719370Spst
389819370Spst  if (buf[0] == 'E')
389919370Spst    error ("Remote failure reply: %s", buf);
390019370Spst}
390119370Spst
390246289Sdfr/* Display a null-terminated packet on stdout, for debugging, using C
390346289Sdfr   string notation.  */
390419370Spst
390546289Sdfrstatic void
390698948Sobrienprint_packet (char *buf)
390746289Sdfr{
390846289Sdfr  puts_filtered ("\"");
390998948Sobrien  fputstr_filtered (buf, '"', gdb_stdout);
391046289Sdfr  puts_filtered ("\"");
391146289Sdfr}
391246289Sdfr
391346289Sdfrint
391498948Sobrienputpkt (char *buf)
391519370Spst{
391646289Sdfr  return putpkt_binary (buf, strlen (buf));
391746289Sdfr}
391846289Sdfr
391946289Sdfr/* Send a packet to the remote machine, with error checking.  The data
392098948Sobrien   of the packet is in BUF.  The string in BUF can be at most  (rs->remote_packet_size) - 5
392146289Sdfr   to account for the $, # and checksum, and for a possible /0 if we are
392246289Sdfr   debugging (remote_debug) and want to print the sent packet as a string */
392346289Sdfr
392446289Sdfrstatic int
392598948Sobrienputpkt_binary (char *buf, int cnt)
392646289Sdfr{
392798948Sobrien  struct remote_state *rs = get_remote_state ();
392819370Spst  int i;
392919370Spst  unsigned char csum = 0;
393098948Sobrien  char *buf2 = alloca (cnt + 6);
393198948Sobrien  long sizeof_junkbuf = (rs->remote_packet_size);
393298948Sobrien  char *junkbuf = alloca (sizeof_junkbuf);
393398948Sobrien
393419370Spst  int ch;
393519370Spst  int tcount = 0;
393619370Spst  char *p;
393719370Spst
393819370Spst  /* Copy the packet into buffer BUF2, encapsulating it
393919370Spst     and giving it a checksum.  */
394019370Spst
394119370Spst  p = buf2;
394219370Spst  *p++ = '$';
394319370Spst
394419370Spst  for (i = 0; i < cnt; i++)
394519370Spst    {
394619370Spst      csum += buf[i];
394719370Spst      *p++ = buf[i];
394819370Spst    }
394919370Spst  *p++ = '#';
395019370Spst  *p++ = tohex ((csum >> 4) & 0xf);
395119370Spst  *p++ = tohex (csum & 0xf);
395219370Spst
395319370Spst  /* Send it over and over until we get a positive ack.  */
395419370Spst
395519370Spst  while (1)
395619370Spst    {
395719370Spst      int started_error_output = 0;
395819370Spst
395919370Spst      if (remote_debug)
396019370Spst	{
396119370Spst	  *p = '\0';
396298948Sobrien	  fprintf_unfiltered (gdb_stdlog, "Sending packet: ");
396398948Sobrien	  fputstrn_unfiltered (buf2, p - buf2, 0, gdb_stdlog);
396498948Sobrien	  fprintf_unfiltered (gdb_stdlog, "...");
396598948Sobrien	  gdb_flush (gdb_stdlog);
396619370Spst	}
396798948Sobrien      if (serial_write (remote_desc, buf2, p - buf2))
396819370Spst	perror_with_name ("putpkt: write failed");
396919370Spst
397019370Spst      /* read until either a timeout occurs (-2) or '+' is read */
397119370Spst      while (1)
397219370Spst	{
397319370Spst	  ch = readchar (remote_timeout);
397419370Spst
397598948Sobrien	  if (remote_debug)
397619370Spst	    {
397719370Spst	      switch (ch)
397819370Spst		{
397919370Spst		case '+':
398098948Sobrien		case '-':
398119370Spst		case SERIAL_TIMEOUT:
398219370Spst		case '$':
398319370Spst		  if (started_error_output)
398419370Spst		    {
398519370Spst		      putchar_unfiltered ('\n');
398619370Spst		      started_error_output = 0;
398719370Spst		    }
398819370Spst		}
398919370Spst	    }
399019370Spst
399119370Spst	  switch (ch)
399219370Spst	    {
399319370Spst	    case '+':
399419370Spst	      if (remote_debug)
399598948Sobrien		fprintf_unfiltered (gdb_stdlog, "Ack\n");
399619370Spst	      return 1;
399798948Sobrien	    case '-':
399898948Sobrien	      if (remote_debug)
399998948Sobrien		fprintf_unfiltered (gdb_stdlog, "Nak\n");
400019370Spst	    case SERIAL_TIMEOUT:
400198948Sobrien	      tcount++;
400219370Spst	      if (tcount > 3)
400319370Spst		return 0;
400419370Spst	      break;		/* Retransmit buffer */
400519370Spst	    case '$':
400619370Spst	      {
400798948Sobrien	        if (remote_debug)
400898948Sobrien		  fprintf_unfiltered (gdb_stdlog, "Packet instead of Ack, ignoring it\n");
400998948Sobrien		/* It's probably an old response, and we're out of sync.
401098948Sobrien		   Just gobble up the packet and ignore it.  */
401198948Sobrien		read_frame (junkbuf, sizeof_junkbuf);
401298948Sobrien		continue;	/* Now, go look for + */
401319370Spst	      }
401419370Spst	    default:
401519370Spst	      if (remote_debug)
401619370Spst		{
401719370Spst		  if (!started_error_output)
401819370Spst		    {
401919370Spst		      started_error_output = 1;
402098948Sobrien		      fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
402119370Spst		    }
402298948Sobrien		  fputc_unfiltered (ch & 0177, gdb_stdlog);
402319370Spst		}
402419370Spst	      continue;
402519370Spst	    }
402619370Spst	  break;		/* Here to retransmit */
402719370Spst	}
402819370Spst
402919370Spst#if 0
403019370Spst      /* This is wrong.  If doing a long backtrace, the user should be
403198948Sobrien         able to get out next time we call QUIT, without anything as
403298948Sobrien         violent as interrupt_query.  If we want to provide a way out of
403398948Sobrien         here without getting to the next QUIT, it should be based on
403498948Sobrien         hitting ^C twice as in remote_wait.  */
403519370Spst      if (quit_flag)
403619370Spst	{
403719370Spst	  quit_flag = 0;
403819370Spst	  interrupt_query ();
403919370Spst	}
404019370Spst#endif
404119370Spst    }
404219370Spst}
404319370Spst
404446289Sdfr/* Come here after finding the start of the frame.  Collect the rest
404546289Sdfr   into BUF, verifying the checksum, length, and handling run-length
404698948Sobrien   compression.  No more than sizeof_buf-1 characters are read so that
404798948Sobrien   the buffer can be NUL terminated.
404819370Spst
404998948Sobrien   Returns -1 on error, number of characters in buffer (ignoring the
405098948Sobrien   trailing NULL) on success. (could be extended to return one of the
405198948Sobrien   SERIAL status indications). */
405298948Sobrien
405398948Sobrienstatic long
405498948Sobrienread_frame (char *buf,
405598948Sobrien	    long sizeof_buf)
405619370Spst{
405719370Spst  unsigned char csum;
405898948Sobrien  long bc;
405919370Spst  int c;
406019370Spst
406119370Spst  csum = 0;
406298948Sobrien  bc = 0;
406319370Spst
406419370Spst  while (1)
406519370Spst    {
406698948Sobrien      /* ASSERT (bc < sizeof_buf - 1) - space for trailing NUL */
406719370Spst      c = readchar (remote_timeout);
406819370Spst      switch (c)
406919370Spst	{
407019370Spst	case SERIAL_TIMEOUT:
407119370Spst	  if (remote_debug)
407298948Sobrien	    fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
407398948Sobrien	  return -1;
407419370Spst	case '$':
407519370Spst	  if (remote_debug)
407698948Sobrien	    fputs_filtered ("Saw new packet start in middle of old one\n",
407798948Sobrien			    gdb_stdlog);
407898948Sobrien	  return -1;		/* Start a new packet, count retries */
407919370Spst	case '#':
408019370Spst	  {
408119370Spst	    unsigned char pktcsum;
408298948Sobrien	    int check_0 = 0;
408398948Sobrien	    int check_1 = 0;
408419370Spst
408598948Sobrien	    buf[bc] = '\0';
408619370Spst
408798948Sobrien	    check_0 = readchar (remote_timeout);
408898948Sobrien	    if (check_0 >= 0)
408998948Sobrien	      check_1 = readchar (remote_timeout);
4090130809Smarcel
409198948Sobrien	    if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
409298948Sobrien	      {
409398948Sobrien		if (remote_debug)
409498948Sobrien		  fputs_filtered ("Timeout in checksum, retrying\n", gdb_stdlog);
409598948Sobrien		return -1;
409698948Sobrien	      }
409798948Sobrien	    else if (check_0 < 0 || check_1 < 0)
409898948Sobrien	      {
409998948Sobrien		if (remote_debug)
410098948Sobrien		  fputs_filtered ("Communication error in checksum\n", gdb_stdlog);
410198948Sobrien		return -1;
410298948Sobrien	      }
410319370Spst
410498948Sobrien	    pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
410519370Spst	    if (csum == pktcsum)
410698948Sobrien              return bc;
410719370Spst
410898948Sobrien	    if (remote_debug)
410919370Spst	      {
411098948Sobrien		fprintf_filtered (gdb_stdlog,
411198948Sobrien			      "Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
411298948Sobrien				  pktcsum, csum);
411398948Sobrien		fputs_filtered (buf, gdb_stdlog);
411498948Sobrien		fputs_filtered ("\n", gdb_stdlog);
411519370Spst	      }
411698948Sobrien	    /* Number of characters in buffer ignoring trailing
411798948Sobrien               NUL. */
411898948Sobrien	    return -1;
411919370Spst	  }
412019370Spst	case '*':		/* Run length encoding */
412198948Sobrien          {
412298948Sobrien	    int repeat;
412398948Sobrien 	    csum += c;
412419370Spst
4125130809Smarcel	    c = readchar (remote_timeout);
4126130809Smarcel	    csum += c;
4127130809Smarcel	    repeat = c - ' ' + 3;	/* Compute repeat count */
412819370Spst
412998948Sobrien	    /* The character before ``*'' is repeated. */
413019370Spst
4131130809Smarcel	    if (repeat > 0 && repeat <= 255
413298948Sobrien		&& bc > 0
413398948Sobrien                && bc + repeat - 1 < sizeof_buf - 1)
413498948Sobrien	      {
413598948Sobrien		memset (&buf[bc], buf[bc - 1], repeat);
413698948Sobrien		bc += repeat;
413798948Sobrien		continue;
413898948Sobrien	      }
413998948Sobrien
414098948Sobrien	    buf[bc] = '\0';
414198948Sobrien	    printf_filtered ("Repeat count %d too large for buffer: ", repeat);
414298948Sobrien	    puts_filtered (buf);
414398948Sobrien	    puts_filtered ("\n");
414498948Sobrien	    return -1;
414598948Sobrien	  }
414619370Spst	default:
414798948Sobrien	  if (bc < sizeof_buf - 1)
414819370Spst	    {
414998948Sobrien	      buf[bc++] = c;
415019370Spst	      csum += c;
415119370Spst	      continue;
415219370Spst	    }
415319370Spst
415498948Sobrien	  buf[bc] = '\0';
415519370Spst	  puts_filtered ("Remote packet too long: ");
415619370Spst	  puts_filtered (buf);
415719370Spst	  puts_filtered ("\n");
415819370Spst
415998948Sobrien	  return -1;
416019370Spst	}
416119370Spst    }
416219370Spst}
416319370Spst
416446289Sdfr/* Read a packet from the remote machine, with error checking, and
416598948Sobrien   store it in BUF.  If FOREVER, wait forever rather than timing out;
416698948Sobrien   this is used (in synchronous mode) to wait for a target that is is
416798948Sobrien   executing user code to stop.  */
416898948Sobrien/* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
416998948Sobrien   don't have to change all the calls to getpkt to deal with the
417098948Sobrien   return value, because at the moment I don't know what the right
417198948Sobrien   thing to do it for those. */
417246289Sdfrvoid
417398948Sobriengetpkt (char *buf,
417498948Sobrien	long sizeof_buf,
417598948Sobrien	int forever)
417619370Spst{
417798948Sobrien  int timed_out;
417898948Sobrien
417998948Sobrien  timed_out = getpkt_sane (buf, sizeof_buf, forever);
418098948Sobrien}
418198948Sobrien
418298948Sobrien
418398948Sobrien/* Read a packet from the remote machine, with error checking, and
418498948Sobrien   store it in BUF.  If FOREVER, wait forever rather than timing out;
418598948Sobrien   this is used (in synchronous mode) to wait for a target that is is
418698948Sobrien   executing user code to stop. If FOREVER == 0, this function is
418798948Sobrien   allowed to time out gracefully and return an indication of this to
418898948Sobrien   the caller. */
418998948Sobrienstatic int
419098948Sobriengetpkt_sane (char *buf,
419198948Sobrien	long sizeof_buf,
419298948Sobrien	int forever)
419398948Sobrien{
419419370Spst  int c;
419519370Spst  int tries;
419619370Spst  int timeout;
419719370Spst  int val;
419819370Spst
419998948Sobrien  strcpy (buf, "timeout");
420019370Spst
420119370Spst  if (forever)
420219370Spst    {
420319370Spst      timeout = watchdog > 0 ? watchdog : -1;
420419370Spst    }
420519370Spst
420619370Spst  else
420719370Spst    timeout = remote_timeout;
420819370Spst
420919370Spst#define MAX_TRIES 3
421019370Spst
421119370Spst  for (tries = 1; tries <= MAX_TRIES; tries++)
421219370Spst    {
421319370Spst      /* This can loop forever if the remote side sends us characters
421498948Sobrien         continuously, but if it pauses, we'll get a zero from readchar
421598948Sobrien         because of timeout.  Then we'll count that as a retry.  */
421619370Spst
421719370Spst      /* Note that we will only wait forever prior to the start of a packet.
421898948Sobrien         After that, we expect characters to arrive at a brisk pace.  They
421998948Sobrien         should show up within remote_timeout intervals.  */
422019370Spst
422119370Spst      do
422219370Spst	{
422319370Spst	  c = readchar (timeout);
422419370Spst
422519370Spst	  if (c == SERIAL_TIMEOUT)
422619370Spst	    {
422798948Sobrien	      if (forever)	/* Watchdog went off?  Kill the target. */
422819370Spst		{
422998948Sobrien		  QUIT;
423019370Spst		  target_mourn_inferior ();
423119370Spst		  error ("Watchdog has expired.  Target detached.\n");
423219370Spst		}
423319370Spst	      if (remote_debug)
423498948Sobrien		fputs_filtered ("Timed out.\n", gdb_stdlog);
423519370Spst	      goto retry;
423619370Spst	    }
423719370Spst	}
423819370Spst      while (c != '$');
423919370Spst
424019370Spst      /* We've found the start of a packet, now collect the data.  */
424119370Spst
424298948Sobrien      val = read_frame (buf, sizeof_buf);
424319370Spst
424498948Sobrien      if (val >= 0)
424519370Spst	{
424619370Spst	  if (remote_debug)
424798948Sobrien	    {
424898948Sobrien	      fprintf_unfiltered (gdb_stdlog, "Packet received: ");
424998948Sobrien	      fputstr_unfiltered (buf, 0, gdb_stdlog);
425098948Sobrien	      fprintf_unfiltered (gdb_stdlog, "\n");
425198948Sobrien	    }
425298948Sobrien	  serial_write (remote_desc, "+", 1);
425398948Sobrien	  return 0;
425419370Spst	}
425519370Spst
425619370Spst      /* Try the whole thing again.  */
425719370Spst    retry:
425898948Sobrien      serial_write (remote_desc, "-", 1);
425919370Spst    }
426019370Spst
426119370Spst  /* We have tried hard enough, and just can't receive the packet.  Give up. */
426219370Spst
426319370Spst  printf_unfiltered ("Ignoring packet error, continuing...\n");
426498948Sobrien  serial_write (remote_desc, "+", 1);
426598948Sobrien  return 1;
426619370Spst}
426719370Spst
426819370Spststatic void
426998948Sobrienremote_kill (void)
427019370Spst{
427119370Spst  /* For some mysterious reason, wait_for_inferior calls kill instead of
427219370Spst     mourn after it gets TARGET_WAITKIND_SIGNALLED.  Work around it.  */
427319370Spst  if (kill_kludge)
427419370Spst    {
427519370Spst      kill_kludge = 0;
427619370Spst      target_mourn_inferior ();
427719370Spst      return;
427819370Spst    }
427919370Spst
428019370Spst  /* Use catch_errors so the user can quit from gdb even when we aren't on
428119370Spst     speaking terms with the remote system.  */
428246289Sdfr  catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR);
428319370Spst
428419370Spst  /* Don't wait for it to die.  I'm not really sure it matters whether
428519370Spst     we do or not.  For the existing stubs, kill is a noop.  */
428619370Spst  target_mourn_inferior ();
428719370Spst}
428819370Spst
428998948Sobrien/* Async version of remote_kill. */
429019370Spststatic void
429198948Sobrienremote_async_kill (void)
429219370Spst{
429398948Sobrien  /* Unregister the file descriptor from the event loop. */
429498948Sobrien  if (target_is_async_p ())
429598948Sobrien    serial_async (remote_desc, NULL, 0);
429698948Sobrien
429798948Sobrien  /* For some mysterious reason, wait_for_inferior calls kill instead of
429898948Sobrien     mourn after it gets TARGET_WAITKIND_SIGNALLED.  Work around it.  */
429998948Sobrien  if (kill_kludge)
430098948Sobrien    {
430198948Sobrien      kill_kludge = 0;
430298948Sobrien      target_mourn_inferior ();
430398948Sobrien      return;
430498948Sobrien    }
430598948Sobrien
430698948Sobrien  /* Use catch_errors so the user can quit from gdb even when we aren't on
430798948Sobrien     speaking terms with the remote system.  */
430898948Sobrien  catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR);
430998948Sobrien
431098948Sobrien  /* Don't wait for it to die.  I'm not really sure it matters whether
431198948Sobrien     we do or not.  For the existing stubs, kill is a noop.  */
431298948Sobrien  target_mourn_inferior ();
431398948Sobrien}
431498948Sobrien
431598948Sobrienstatic void
431698948Sobrienremote_mourn (void)
431798948Sobrien{
431819370Spst  remote_mourn_1 (&remote_ops);
431919370Spst}
432019370Spst
432119370Spststatic void
432298948Sobrienremote_async_mourn (void)
432319370Spst{
432498948Sobrien  remote_mourn_1 (&remote_async_ops);
432598948Sobrien}
432698948Sobrien
432798948Sobrienstatic void
432898948Sobrienextended_remote_mourn (void)
432998948Sobrien{
433019370Spst  /* We do _not_ want to mourn the target like this; this will
433119370Spst     remove the extended remote target  from the target stack,
4332130809Smarcel     and the next time the user says "run" it'll fail.
433319370Spst
433419370Spst     FIXME: What is the right thing to do here?  */
433519370Spst#if 0
433619370Spst  remote_mourn_1 (&extended_remote_ops);
433719370Spst#endif
433819370Spst}
433919370Spst
434019370Spst/* Worker function for remote_mourn.  */
434119370Spststatic void
434298948Sobrienremote_mourn_1 (struct target_ops *target)
434319370Spst{
434419370Spst  unpush_target (target);
434519370Spst  generic_mourn_inferior ();
434619370Spst}
434719370Spst
434819370Spst/* In the extended protocol we want to be able to do things like
434919370Spst   "run" and have them basically work as expected.  So we need
4350130809Smarcel   a special create_inferior function.
435119370Spst
435219370Spst   FIXME: One day add support for changing the exec file
435319370Spst   we're debugging, arguments and an environment.  */
435419370Spst
435519370Spststatic void
435698948Sobrienextended_remote_create_inferior (char *exec_file, char *args, char **env)
435719370Spst{
435819370Spst  /* Rip out the breakpoints; we'll reinsert them after restarting
435919370Spst     the remote server.  */
436019370Spst  remove_breakpoints ();
436119370Spst
436219370Spst  /* Now restart the remote server.  */
436319370Spst  extended_remote_restart ();
436419370Spst
436519370Spst  /* Now put the breakpoints back in.  This way we're safe if the
436619370Spst     restart function works via a unix fork on the remote side.  */
436719370Spst  insert_breakpoints ();
436819370Spst
436919370Spst  /* Clean up from the last time we were running.  */
437019370Spst  clear_proceed_status ();
437119370Spst
437219370Spst  /* Let the remote process run.  */
437319370Spst  proceed (-1, TARGET_SIGNAL_0, 0);
437419370Spst}
437519370Spst
437698948Sobrien/* Async version of extended_remote_create_inferior. */
437798948Sobrienstatic void
437898948Sobrienextended_remote_async_create_inferior (char *exec_file, char *args, char **env)
437998948Sobrien{
438098948Sobrien  /* Rip out the breakpoints; we'll reinsert them after restarting
438198948Sobrien     the remote server.  */
438298948Sobrien  remove_breakpoints ();
438398948Sobrien
438498948Sobrien  /* If running asynchronously, register the target file descriptor
438598948Sobrien     with the event loop. */
438698948Sobrien  if (event_loop_p && target_can_async_p ())
438798948Sobrien    target_async (inferior_event_handler, 0);
438898948Sobrien
438998948Sobrien  /* Now restart the remote server.  */
439098948Sobrien  extended_remote_restart ();
439198948Sobrien
439298948Sobrien  /* Now put the breakpoints back in.  This way we're safe if the
439398948Sobrien     restart function works via a unix fork on the remote side.  */
439498948Sobrien  insert_breakpoints ();
439598948Sobrien
439698948Sobrien  /* Clean up from the last time we were running.  */
439798948Sobrien  clear_proceed_status ();
439898948Sobrien
439998948Sobrien  /* Let the remote process run.  */
440098948Sobrien  proceed (-1, TARGET_SIGNAL_0, 0);
440198948Sobrien}
440219370Spst
440398948Sobrien
4404130809Smarcel/* On some machines, e.g. 68k, we may use a different breakpoint
4405130809Smarcel   instruction than other targets; in those use
4406130809Smarcel   DEPRECATED_REMOTE_BREAKPOINT instead of just BREAKPOINT_FROM_PC.
4407130809Smarcel   Also, bi-endian targets may define
4408130809Smarcel   DEPRECATED_LITTLE_REMOTE_BREAKPOINT and
4409130809Smarcel   DEPRECATED_BIG_REMOTE_BREAKPOINT.  If none of these are defined, we
4410130809Smarcel   just call the standard routines that are in mem-break.c.  */
441146289Sdfr
4412130809Smarcel/* NOTE: cagney/2003-06-08: This is silly.  A remote and simulator
4413130809Smarcel   target should use an identical BREAKPOINT_FROM_PC.  As for native,
4414130809Smarcel   the ARCH-OS-tdep.c code can override the default.  */
441546289Sdfr
4416130809Smarcel#if defined (DEPRECATED_LITTLE_REMOTE_BREAKPOINT) && defined (DEPRECATED_BIG_REMOTE_BREAKPOINT) && !defined(DEPRECATED_REMOTE_BREAKPOINT)
4417130809Smarcel#define DEPRECATED_REMOTE_BREAKPOINT
441846289Sdfr#endif
441946289Sdfr
4420130809Smarcel#ifdef DEPRECATED_REMOTE_BREAKPOINT
442119370Spst
442246289Sdfr/* If the target isn't bi-endian, just pretend it is.  */
4423130809Smarcel#if !defined (DEPRECATED_LITTLE_REMOTE_BREAKPOINT) && !defined (DEPRECATED_BIG_REMOTE_BREAKPOINT)
4424130809Smarcel#define DEPRECATED_LITTLE_REMOTE_BREAKPOINT DEPRECATED_REMOTE_BREAKPOINT
4425130809Smarcel#define DEPRECATED_BIG_REMOTE_BREAKPOINT DEPRECATED_REMOTE_BREAKPOINT
442646289Sdfr#endif
442719370Spst
4428130809Smarcelstatic unsigned char big_break_insn[] = DEPRECATED_BIG_REMOTE_BREAKPOINT;
4429130809Smarcelstatic unsigned char little_break_insn[] = DEPRECATED_LITTLE_REMOTE_BREAKPOINT;
443019370Spst
4431130809Smarcel#endif /* DEPRECATED_REMOTE_BREAKPOINT */
443219370Spst
4433130809Smarcel/* Insert a breakpoint on targets that don't have any better
4434130809Smarcel   breakpoint support.  We read the contents of the target location
4435130809Smarcel   and stash it, then overwrite it with a breakpoint instruction.
4436130809Smarcel   ADDR is the target location in the target machine.  CONTENTS_CACHE
4437130809Smarcel   is a pointer to memory allocated for saving the target contents.
4438130809Smarcel   It is guaranteed by the caller to be long enough to save the number
4439130809Smarcel   of bytes returned by BREAKPOINT_FROM_PC.  */
444019370Spst
444119370Spststatic int
444298948Sobrienremote_insert_breakpoint (CORE_ADDR addr, char *contents_cache)
444319370Spst{
444498948Sobrien  struct remote_state *rs = get_remote_state ();
4445130809Smarcel#ifdef DEPRECATED_REMOTE_BREAKPOINT
444619370Spst  int val;
4447130809Smarcel#endif
444898948Sobrien  int bp_size;
444919370Spst
445098948Sobrien  /* Try the "Z" s/w breakpoint packet if it is not already disabled.
445198948Sobrien     If it succeeds, then set the support to PACKET_ENABLE.  If it
445298948Sobrien     fails, and the user has explicitly requested the Z support then
445398948Sobrien     report an error, otherwise, mark it disabled and go on. */
4454130809Smarcel
445598948Sobrien  if (remote_protocol_Z[Z_PACKET_SOFTWARE_BP].support != PACKET_DISABLE)
445698948Sobrien    {
445798948Sobrien      char *buf = alloca (rs->remote_packet_size);
445898948Sobrien      char *p = buf;
4459130809Smarcel
446098948Sobrien      addr = remote_address_masked (addr);
446198948Sobrien      *(p++) = 'Z';
446298948Sobrien      *(p++) = '0';
446398948Sobrien      *(p++) = ',';
446498948Sobrien      p += hexnumstr (p, (ULONGEST) addr);
446598948Sobrien      BREAKPOINT_FROM_PC (&addr, &bp_size);
446698948Sobrien      sprintf (p, ",%d", bp_size);
4467130809Smarcel
446898948Sobrien      putpkt (buf);
446998948Sobrien      getpkt (buf, (rs->remote_packet_size), 0);
447098948Sobrien
447198948Sobrien      switch (packet_ok (buf, &remote_protocol_Z[Z_PACKET_SOFTWARE_BP]))
447298948Sobrien	{
447398948Sobrien	case PACKET_ERROR:
447498948Sobrien	  return -1;
447598948Sobrien	case PACKET_OK:
447698948Sobrien	  return 0;
447798948Sobrien	case PACKET_UNKNOWN:
447898948Sobrien	  break;
447998948Sobrien	}
448098948Sobrien    }
448198948Sobrien
4482130809Smarcel#ifdef DEPRECATED_REMOTE_BREAKPOINT
448346289Sdfr  val = target_read_memory (addr, contents_cache, sizeof big_break_insn);
448419370Spst
448519370Spst  if (val == 0)
448646289Sdfr    {
448798948Sobrien      if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
448846289Sdfr	val = target_write_memory (addr, (char *) big_break_insn,
448946289Sdfr				   sizeof big_break_insn);
449046289Sdfr      else
449146289Sdfr	val = target_write_memory (addr, (char *) little_break_insn,
449246289Sdfr				   sizeof little_break_insn);
449346289Sdfr    }
449419370Spst
449519370Spst  return val;
449646289Sdfr#else
449746289Sdfr  return memory_insert_breakpoint (addr, contents_cache);
4498130809Smarcel#endif /* DEPRECATED_REMOTE_BREAKPOINT */
449919370Spst}
450019370Spst
450119370Spststatic int
450298948Sobrienremote_remove_breakpoint (CORE_ADDR addr, char *contents_cache)
450319370Spst{
450498948Sobrien  struct remote_state *rs = get_remote_state ();
450598948Sobrien  int bp_size;
450698948Sobrien
450798948Sobrien  if (remote_protocol_Z[Z_PACKET_SOFTWARE_BP].support != PACKET_DISABLE)
450898948Sobrien    {
450998948Sobrien      char *buf = alloca (rs->remote_packet_size);
451098948Sobrien      char *p = buf;
4511130809Smarcel
451298948Sobrien      *(p++) = 'z';
451398948Sobrien      *(p++) = '0';
451498948Sobrien      *(p++) = ',';
451598948Sobrien
451698948Sobrien      addr = remote_address_masked (addr);
451798948Sobrien      p += hexnumstr (p, (ULONGEST) addr);
451898948Sobrien      BREAKPOINT_FROM_PC (&addr, &bp_size);
451998948Sobrien      sprintf (p, ",%d", bp_size);
4520130809Smarcel
452198948Sobrien      putpkt (buf);
452298948Sobrien      getpkt (buf, (rs->remote_packet_size), 0);
452398948Sobrien
452498948Sobrien      return (buf[0] == 'E');
452598948Sobrien    }
452698948Sobrien
4527130809Smarcel#ifdef DEPRECATED_REMOTE_BREAKPOINT
452846289Sdfr  return target_write_memory (addr, contents_cache, sizeof big_break_insn);
452946289Sdfr#else
453046289Sdfr  return memory_remove_breakpoint (addr, contents_cache);
4531130809Smarcel#endif /* DEPRECATED_REMOTE_BREAKPOINT */
453219370Spst}
453319370Spst
453498948Sobrienstatic int
453598948Sobrienwatchpoint_to_Z_packet (int type)
453698948Sobrien{
453798948Sobrien  switch (type)
453898948Sobrien    {
453998948Sobrien    case hw_write:
454098948Sobrien      return 2;
454198948Sobrien      break;
454298948Sobrien    case hw_read:
454398948Sobrien      return 3;
454498948Sobrien      break;
454598948Sobrien    case hw_access:
454698948Sobrien      return 4;
454798948Sobrien      break;
454898948Sobrien    default:
454998948Sobrien      internal_error (__FILE__, __LINE__,
455098948Sobrien		      "hw_bp_to_z: bad watchpoint type %d", type);
455198948Sobrien    }
455298948Sobrien}
455398948Sobrien
4554130809Smarcelstatic int
455598948Sobrienremote_insert_watchpoint (CORE_ADDR addr, int len, int type)
455698948Sobrien{
455798948Sobrien  struct remote_state *rs = get_remote_state ();
455898948Sobrien  char *buf = alloca (rs->remote_packet_size);
455998948Sobrien  char *p;
456098948Sobrien  enum Z_packet_type packet = watchpoint_to_Z_packet (type);
456198948Sobrien
456298948Sobrien  if (remote_protocol_Z[packet].support == PACKET_DISABLE)
456398948Sobrien    error ("Can't set hardware watchpoints without the '%s' (%s) packet\n",
456498948Sobrien	   remote_protocol_Z[packet].name,
456598948Sobrien	   remote_protocol_Z[packet].title);
4566130809Smarcel
456798948Sobrien  sprintf (buf, "Z%x,", packet);
456898948Sobrien  p = strchr (buf, '\0');
456998948Sobrien  addr = remote_address_masked (addr);
457098948Sobrien  p += hexnumstr (p, (ULONGEST) addr);
457198948Sobrien  sprintf (p, ",%x", len);
4572130809Smarcel
457398948Sobrien  putpkt (buf);
457498948Sobrien  getpkt (buf, (rs->remote_packet_size), 0);
457598948Sobrien
457698948Sobrien  switch (packet_ok (buf, &remote_protocol_Z[packet]))
457798948Sobrien    {
457898948Sobrien    case PACKET_ERROR:
457998948Sobrien    case PACKET_UNKNOWN:
458098948Sobrien      return -1;
458198948Sobrien    case PACKET_OK:
458298948Sobrien      return 0;
458398948Sobrien    }
458498948Sobrien  internal_error (__FILE__, __LINE__,
458598948Sobrien		  "remote_insert_watchpoint: reached end of function");
458698948Sobrien}
458798948Sobrien
458898948Sobrien
4589130809Smarcelstatic int
459098948Sobrienremote_remove_watchpoint (CORE_ADDR addr, int len, int type)
459198948Sobrien{
459298948Sobrien  struct remote_state *rs = get_remote_state ();
459398948Sobrien  char *buf = alloca (rs->remote_packet_size);
459498948Sobrien  char *p;
459598948Sobrien  enum Z_packet_type packet = watchpoint_to_Z_packet (type);
459698948Sobrien
459798948Sobrien  if (remote_protocol_Z[packet].support == PACKET_DISABLE)
459898948Sobrien    error ("Can't clear hardware watchpoints without the '%s' (%s) packet\n",
459998948Sobrien	   remote_protocol_Z[packet].name,
460098948Sobrien	   remote_protocol_Z[packet].title);
4601130809Smarcel
460298948Sobrien  sprintf (buf, "z%x,", packet);
460398948Sobrien  p = strchr (buf, '\0');
460498948Sobrien  addr = remote_address_masked (addr);
460598948Sobrien  p += hexnumstr (p, (ULONGEST) addr);
460698948Sobrien  sprintf (p, ",%x", len);
460798948Sobrien  putpkt (buf);
460898948Sobrien  getpkt (buf, (rs->remote_packet_size), 0);
460998948Sobrien
461098948Sobrien  switch (packet_ok (buf, &remote_protocol_Z[packet]))
461198948Sobrien    {
461298948Sobrien    case PACKET_ERROR:
461398948Sobrien    case PACKET_UNKNOWN:
461498948Sobrien      return -1;
461598948Sobrien    case PACKET_OK:
461698948Sobrien      return 0;
461798948Sobrien    }
461898948Sobrien  internal_error (__FILE__, __LINE__,
461998948Sobrien		  "remote_remove_watchpoint: reached end of function");
462098948Sobrien}
462198948Sobrien
462298948Sobrien
4623130809Smarcelint remote_hw_watchpoint_limit = -1;
4624130809Smarcelint remote_hw_breakpoint_limit = -1;
4625130809Smarcel
4626130809Smarcelstatic int
4627130809Smarcelremote_check_watch_resources (int type, int cnt, int ot)
462898948Sobrien{
4629130809Smarcel  if (type == bp_hardware_breakpoint)
4630130809Smarcel    {
4631130809Smarcel      if (remote_hw_breakpoint_limit == 0)
4632130809Smarcel	return 0;
4633130809Smarcel      else if (remote_hw_breakpoint_limit < 0)
4634130809Smarcel	return 1;
4635130809Smarcel      else if (cnt <= remote_hw_breakpoint_limit)
4636130809Smarcel	return 1;
4637130809Smarcel    }
4638130809Smarcel  else
4639130809Smarcel    {
4640130809Smarcel      if (remote_hw_watchpoint_limit == 0)
4641130809Smarcel	return 0;
4642130809Smarcel      else if (remote_hw_watchpoint_limit < 0)
4643130809Smarcel	return 1;
4644130809Smarcel      else if (ot)
4645130809Smarcel	return -1;
4646130809Smarcel      else if (cnt <= remote_hw_watchpoint_limit)
4647130809Smarcel	return 1;
4648130809Smarcel    }
4649130809Smarcel  return -1;
4650130809Smarcel}
4651130809Smarcel
4652130809Smarcelstatic int
4653130809Smarcelremote_stopped_by_watchpoint (void)
4654130809Smarcel{
4655130809Smarcel    return remote_stopped_by_watchpoint_p;
4656130809Smarcel}
4657130809Smarcel
4658130809Smarcelstatic CORE_ADDR
4659130809Smarcelremote_stopped_data_address (void)
4660130809Smarcel{
4661130809Smarcel  if (remote_stopped_by_watchpoint ())
4662130809Smarcel    return remote_watch_data_address;
4663130809Smarcel  return (CORE_ADDR)0;
4664130809Smarcel}
4665130809Smarcel
4666130809Smarcel
4667130809Smarcelstatic int
4668130809Smarcelremote_insert_hw_breakpoint (CORE_ADDR addr, char *shadow)
4669130809Smarcel{
4670130809Smarcel  int len = 0;
467198948Sobrien  struct remote_state *rs = get_remote_state ();
467298948Sobrien  char *buf = alloca (rs->remote_packet_size);
467398948Sobrien  char *p = buf;
4674130809Smarcel
4675130809Smarcel  /* The length field should be set to the size of a breakpoint
4676130809Smarcel     instruction.  */
4677130809Smarcel
4678130809Smarcel  BREAKPOINT_FROM_PC (&addr, &len);
4679130809Smarcel
468098948Sobrien  if (remote_protocol_Z[Z_PACKET_HARDWARE_BP].support == PACKET_DISABLE)
468198948Sobrien    error ("Can't set hardware breakpoint without the '%s' (%s) packet\n",
468298948Sobrien	   remote_protocol_Z[Z_PACKET_HARDWARE_BP].name,
468398948Sobrien	   remote_protocol_Z[Z_PACKET_HARDWARE_BP].title);
4684130809Smarcel
468598948Sobrien  *(p++) = 'Z';
468698948Sobrien  *(p++) = '1';
468798948Sobrien  *(p++) = ',';
4688130809Smarcel
468998948Sobrien  addr = remote_address_masked (addr);
469098948Sobrien  p += hexnumstr (p, (ULONGEST) addr);
469198948Sobrien  sprintf (p, ",%x", len);
469298948Sobrien
469398948Sobrien  putpkt (buf);
469498948Sobrien  getpkt (buf, (rs->remote_packet_size), 0);
469598948Sobrien
469698948Sobrien  switch (packet_ok (buf, &remote_protocol_Z[Z_PACKET_HARDWARE_BP]))
469798948Sobrien    {
469898948Sobrien    case PACKET_ERROR:
469998948Sobrien    case PACKET_UNKNOWN:
470098948Sobrien      return -1;
470198948Sobrien    case PACKET_OK:
470298948Sobrien      return 0;
470398948Sobrien    }
470498948Sobrien  internal_error (__FILE__, __LINE__,
4705130809Smarcel		  "remote_insert_hw_breakpoint: reached end of function");
470698948Sobrien}
470798948Sobrien
470898948Sobrien
4709130809Smarcelstatic int
4710130809Smarcelremote_remove_hw_breakpoint (CORE_ADDR addr, char *shadow)
471198948Sobrien{
4712130809Smarcel  int len;
471398948Sobrien  struct remote_state *rs = get_remote_state ();
471498948Sobrien  char *buf = alloca (rs->remote_packet_size);
471598948Sobrien  char *p = buf;
4716130809Smarcel
4717130809Smarcel  /* The length field should be set to the size of a breakpoint
4718130809Smarcel     instruction.  */
4719130809Smarcel
4720130809Smarcel  BREAKPOINT_FROM_PC (&addr, &len);
4721130809Smarcel
472298948Sobrien  if (remote_protocol_Z[Z_PACKET_HARDWARE_BP].support == PACKET_DISABLE)
472398948Sobrien    error ("Can't clear hardware breakpoint without the '%s' (%s) packet\n",
472498948Sobrien	   remote_protocol_Z[Z_PACKET_HARDWARE_BP].name,
472598948Sobrien	   remote_protocol_Z[Z_PACKET_HARDWARE_BP].title);
4726130809Smarcel
472798948Sobrien  *(p++) = 'z';
472898948Sobrien  *(p++) = '1';
472998948Sobrien  *(p++) = ',';
4730130809Smarcel
473198948Sobrien  addr = remote_address_masked (addr);
473298948Sobrien  p += hexnumstr (p, (ULONGEST) addr);
473398948Sobrien  sprintf (p, ",%x", len);
473498948Sobrien
473598948Sobrien  putpkt(buf);
473698948Sobrien  getpkt (buf, (rs->remote_packet_size), 0);
4737130809Smarcel
473898948Sobrien  switch (packet_ok (buf, &remote_protocol_Z[Z_PACKET_HARDWARE_BP]))
473998948Sobrien    {
474098948Sobrien    case PACKET_ERROR:
474198948Sobrien    case PACKET_UNKNOWN:
474298948Sobrien      return -1;
474398948Sobrien    case PACKET_OK:
474498948Sobrien      return 0;
474598948Sobrien    }
474698948Sobrien  internal_error (__FILE__, __LINE__,
4747130809Smarcel		  "remote_remove_hw_breakpoint: reached end of function");
474898948Sobrien}
474998948Sobrien
475046289Sdfr/* Some targets are only capable of doing downloads, and afterwards
475146289Sdfr   they switch to the remote serial protocol.  This function provides
475246289Sdfr   a clean way to get from the download target to the remote target.
475346289Sdfr   It's basically just a wrapper so that we don't have to expose any
475446289Sdfr   of the internal workings of remote.c.
475519370Spst
475646289Sdfr   Prior to calling this routine, you should shutdown the current
475746289Sdfr   target code, else you will get the "A program is being debugged
475846289Sdfr   already..." message.  Usually a call to pop_target() suffices.  */
475919370Spst
476046289Sdfrvoid
476198948Sobrienpush_remote_target (char *name, int from_tty)
476246289Sdfr{
476346289Sdfr  printf_filtered ("Switching to remote protocol\n");
476446289Sdfr  remote_open (name, from_tty);
476546289Sdfr}
476619370Spst
476746289Sdfr/* Table used by the crc32 function to calcuate the checksum. */
476846289Sdfr
476998948Sobrienstatic unsigned long crc32_table[256] =
477098948Sobrien{0, 0};
477146289Sdfr
477246289Sdfrstatic unsigned long
477398948Sobriencrc32 (unsigned char *buf, int len, unsigned int crc)
477446289Sdfr{
477598948Sobrien  if (!crc32_table[1])
477646289Sdfr    {
477746289Sdfr      /* Initialize the CRC table and the decoding table. */
477846289Sdfr      int i, j;
477946289Sdfr      unsigned int c;
478046289Sdfr
478146289Sdfr      for (i = 0; i < 256; i++)
478298948Sobrien	{
478398948Sobrien	  for (c = i << 24, j = 8; j > 0; --j)
478498948Sobrien	    c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
478598948Sobrien	  crc32_table[i] = c;
478698948Sobrien	}
478746289Sdfr    }
478846289Sdfr
478946289Sdfr  while (len--)
479046289Sdfr    {
479146289Sdfr      crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buf) & 255];
479246289Sdfr      buf++;
479346289Sdfr    }
479446289Sdfr  return crc;
479546289Sdfr}
479646289Sdfr
479746289Sdfr/* compare-sections command
479846289Sdfr
479946289Sdfr   With no arguments, compares each loadable section in the exec bfd
480046289Sdfr   with the same memory range on the target, and reports mismatches.
480146289Sdfr   Useful for verifying the image on the target against the exec file.
480246289Sdfr   Depends on the target understanding the new "qCRC:" request.  */
480346289Sdfr
480498948Sobrien/* FIXME: cagney/1999-10-26: This command should be broken down into a
480598948Sobrien   target method (target verify memory) and generic version of the
480698948Sobrien   actual command.  This will allow other high-level code (especially
480798948Sobrien   generic_load()) to make use of this target functionality. */
480898948Sobrien
480946289Sdfrstatic void
481098948Sobriencompare_sections_command (char *args, int from_tty)
481146289Sdfr{
481298948Sobrien  struct remote_state *rs = get_remote_state ();
481346289Sdfr  asection *s;
481446289Sdfr  unsigned long host_crc, target_crc;
481546289Sdfr  extern bfd *exec_bfd;
481646289Sdfr  struct cleanup *old_chain;
481798948Sobrien  char *tmp;
481898948Sobrien  char *sectdata;
481998948Sobrien  const char *sectname;
482098948Sobrien  char *buf = alloca (rs->remote_packet_size);
482146289Sdfr  bfd_size_type size;
482246289Sdfr  bfd_vma lma;
482346289Sdfr  int matched = 0;
482446289Sdfr  int mismatched = 0;
482546289Sdfr
482646289Sdfr  if (!exec_bfd)
482746289Sdfr    error ("command cannot be used without an exec file");
482846289Sdfr  if (!current_target.to_shortname ||
482946289Sdfr      strcmp (current_target.to_shortname, "remote") != 0)
483046289Sdfr    error ("command can only be used with remote target");
483146289Sdfr
483298948Sobrien  for (s = exec_bfd->sections; s; s = s->next)
483346289Sdfr    {
483446289Sdfr      if (!(s->flags & SEC_LOAD))
483598948Sobrien	continue;		/* skip non-loadable section */
483646289Sdfr
4837218822Sdim      size = bfd_get_section_size (s);
483846289Sdfr      if (size == 0)
483998948Sobrien	continue;		/* skip zero-length section */
484046289Sdfr
484198948Sobrien      sectname = bfd_get_section_name (exec_bfd, s);
484246289Sdfr      if (args && strcmp (args, sectname) != 0)
484398948Sobrien	continue;		/* not the section selected by user */
484446289Sdfr
484598948Sobrien      matched = 1;		/* do this section */
484646289Sdfr      lma = s->lma;
484746289Sdfr      /* FIXME: assumes lma can fit into long */
484846289Sdfr      sprintf (buf, "qCRC:%lx,%lx", (long) lma, (long) size);
484946289Sdfr      putpkt (buf);
485046289Sdfr
485146289Sdfr      /* be clever; compute the host_crc before waiting for target reply */
485246289Sdfr      sectdata = xmalloc (size);
485398948Sobrien      old_chain = make_cleanup (xfree, sectdata);
485446289Sdfr      bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
485546289Sdfr      host_crc = crc32 ((unsigned char *) sectdata, size, 0xffffffff);
485646289Sdfr
485798948Sobrien      getpkt (buf, (rs->remote_packet_size), 0);
485846289Sdfr      if (buf[0] == 'E')
4859130809Smarcel	error ("target memory fault, section %s, range 0x%s -- 0x%s",
4860130809Smarcel	       sectname, paddr (lma), paddr (lma + size));
486146289Sdfr      if (buf[0] != 'C')
486246289Sdfr	error ("remote target does not support this operation");
486346289Sdfr
486446289Sdfr      for (target_crc = 0, tmp = &buf[1]; *tmp; tmp++)
486546289Sdfr	target_crc = target_crc * 16 + fromhex (*tmp);
486646289Sdfr
486798948Sobrien      printf_filtered ("Section %s, range 0x%s -- 0x%s: ",
486898948Sobrien		       sectname, paddr (lma), paddr (lma + size));
486946289Sdfr      if (host_crc == target_crc)
487046289Sdfr	printf_filtered ("matched.\n");
487146289Sdfr      else
487298948Sobrien	{
487398948Sobrien	  printf_filtered ("MIS-MATCHED!\n");
487498948Sobrien	  mismatched++;
487598948Sobrien	}
487646289Sdfr
487746289Sdfr      do_cleanups (old_chain);
487846289Sdfr    }
487946289Sdfr  if (mismatched > 0)
488046289Sdfr    warning ("One or more sections of the remote executable does not match\n\
488146289Sdfrthe loaded file\n");
488246289Sdfr  if (args && !matched)
488346289Sdfr    printf_filtered ("No loaded section named '%s'.\n", args);
488446289Sdfr}
488546289Sdfr
4886130809Smarcelstatic LONGEST
4887130809Smarcelremote_xfer_partial (struct target_ops *ops, enum target_object object,
4888130809Smarcel		     const char *annex, void *readbuf, const void *writebuf,
4889130809Smarcel		     ULONGEST offset, LONGEST len)
489046289Sdfr{
489198948Sobrien  struct remote_state *rs = get_remote_state ();
489246289Sdfr  int i;
489398948Sobrien  char *buf2 = alloca (rs->remote_packet_size);
489446289Sdfr  char *p2 = &buf2[0];
4895130809Smarcel  char query_type;
489646289Sdfr
4897130809Smarcel  /* Only handle reads.  */
4898130809Smarcel  if (writebuf != NULL || readbuf == NULL)
4899130809Smarcel    return -1;
490046289Sdfr
4901130809Smarcel  /* Map pre-existing objects onto letters.  DO NOT do this for new
4902130809Smarcel     objects!!!  Instead specify new query packets.  */
4903130809Smarcel  switch (object)
490446289Sdfr    {
4905130809Smarcel    case TARGET_OBJECT_KOD:
4906130809Smarcel      query_type = 'K';
4907130809Smarcel      break;
4908130809Smarcel    case TARGET_OBJECT_AVR:
4909130809Smarcel      query_type = 'R';
4910130809Smarcel      break;
4911130809Smarcel
4912130809Smarcel    case TARGET_OBJECT_AUXV:
4913130809Smarcel      if (remote_protocol_qPart_auxv.support != PACKET_DISABLE)
4914130809Smarcel	{
4915130809Smarcel	  unsigned int total = 0;
4916130809Smarcel	  while (len > 0)
4917130809Smarcel	    {
4918130809Smarcel	      LONGEST n = min ((rs->remote_packet_size - 2) / 2, len);
4919130809Smarcel	      snprintf (buf2, rs->remote_packet_size,
4920130809Smarcel			"qPart:auxv:read::%s,%s",
4921130809Smarcel			phex_nz (offset, sizeof offset),
4922130809Smarcel			phex_nz (n, sizeof n));
4923130809Smarcel	      i = putpkt (buf2);
4924130809Smarcel	      if (i < 0)
4925130809Smarcel		return total > 0 ? total : i;
4926130809Smarcel	      buf2[0] = '\0';
4927130809Smarcel	      getpkt (buf2, rs->remote_packet_size, 0);
4928130809Smarcel	      if (packet_ok (buf2, &remote_protocol_qPart_auxv) != PACKET_OK)
4929130809Smarcel		return total > 0 ? total : -1;
4930130809Smarcel	      if (buf2[0] == 'O' && buf2[1] == 'K' && buf2[2] == '\0')
4931130809Smarcel		break;		/* Got EOF indicator.  */
4932130809Smarcel	      /* Got some data.  */
4933130809Smarcel	      i = hex2bin (buf2, readbuf, len);
4934130809Smarcel	      if (i > 0)
4935130809Smarcel		{
4936130809Smarcel		  readbuf = (void *) ((char *) readbuf + i);
4937130809Smarcel		  offset += i;
4938130809Smarcel		  len -= i;
4939130809Smarcel		  total += i;
4940130809Smarcel		}
4941130809Smarcel	    }
4942130809Smarcel	  return total;
4943130809Smarcel	}
494446289Sdfr      return -1;
4945130809Smarcel
4946131086Smarcel    case TARGET_OBJECT_DIRTY:
4947131086Smarcel      if (remote_protocol_qPart_dirty.support != PACKET_DISABLE)
4948131086Smarcel	{
4949131086Smarcel	  snprintf (buf2, rs->remote_packet_size, "qPart:dirty:read::%lx",
4950131086Smarcel		    (long)(offset >> 3));
4951131086Smarcel	  i = putpkt (buf2);
4952131086Smarcel	  if (i < 0)
4953131086Smarcel	    return i;
4954131086Smarcel	  buf2[0] = '\0';
4955131086Smarcel	  getpkt (buf2, rs->remote_packet_size, 0);
4956131086Smarcel	  if (packet_ok (buf2, &remote_protocol_qPart_dirty) != PACKET_OK)
4957131086Smarcel	    return -1;
4958131086Smarcel	  i = hex2bin (buf2, readbuf, len);
4959131086Smarcel	  return i;
4960131086Smarcel	}
4961131086Smarcel      return -1;
4962131086Smarcel
4963130809Smarcel    default:
4964130809Smarcel      return -1;
496546289Sdfr    }
496646289Sdfr
4967130809Smarcel  /* Note: a zero OFFSET and LEN can be used to query the minimum
4968130809Smarcel     buffer size.  */
4969130809Smarcel  if (offset == 0 && len == 0)
4970130809Smarcel    return (rs->remote_packet_size);
4971130809Smarcel  /* Minimum outbuf size is (rs->remote_packet_size) - if bufsiz is
4972130809Smarcel     not large enough let the caller.  */
4973130809Smarcel  if (len < (rs->remote_packet_size))
4974130809Smarcel    return -1;
4975130809Smarcel  len = rs->remote_packet_size;
4976130809Smarcel
497746289Sdfr  /* except for querying the minimum buffer size, target must be open */
497898948Sobrien  if (!remote_desc)
497946289Sdfr    error ("remote query is only available after target open");
498046289Sdfr
4981130809Smarcel  gdb_assert (annex != NULL);
4982130809Smarcel  gdb_assert (readbuf != NULL);
498346289Sdfr
498446289Sdfr  *p2++ = 'q';
498546289Sdfr  *p2++ = query_type;
498646289Sdfr
498746289Sdfr  /* we used one buffer char for the remote protocol q command and another
498846289Sdfr     for the query type.  As the remote protocol encapsulation uses 4 chars
498946289Sdfr     plus one extra in case we are debugging (remote_debug),
499046289Sdfr     we have PBUFZIZ - 7 left to pack the query string */
499146289Sdfr  i = 0;
4992130809Smarcel  while (annex[i] && (i < ((rs->remote_packet_size) - 8)))
499346289Sdfr    {
4994130809Smarcel      /* Bad caller may have sent forbidden characters.  */
4995130809Smarcel      gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#');
4996130809Smarcel      *p2++ = annex[i];
499746289Sdfr      i++;
499846289Sdfr    }
4999130809Smarcel  *p2 = '\0';
5000130809Smarcel  gdb_assert (annex[i] == '\0');
500146289Sdfr
500246289Sdfr  i = putpkt (buf2);
500398948Sobrien  if (i < 0)
500498948Sobrien    return i;
500546289Sdfr
5006130809Smarcel  getpkt (readbuf, len, 0);
500746289Sdfr
5008130809Smarcel  return strlen (readbuf);
500946289Sdfr}
501046289Sdfr
501146289Sdfrstatic void
501298948Sobrienremote_rcmd (char *command,
501398948Sobrien	     struct ui_file *outbuf)
501446289Sdfr{
501598948Sobrien  struct remote_state *rs = get_remote_state ();
501698948Sobrien  int i;
501798948Sobrien  char *buf = alloca (rs->remote_packet_size);
501898948Sobrien  char *p = buf;
501946289Sdfr
502098948Sobrien  if (!remote_desc)
502198948Sobrien    error ("remote rcmd is only available after target open");
502298948Sobrien
502398948Sobrien  /* Send a NULL command across as an empty command */
502498948Sobrien  if (command == NULL)
502598948Sobrien    command = "";
502698948Sobrien
502798948Sobrien  /* The query prefix */
502898948Sobrien  strcpy (buf, "qRcmd,");
502998948Sobrien  p = strchr (buf, '\0');
503098948Sobrien
503198948Sobrien  if ((strlen (buf) + strlen (command) * 2 + 8/*misc*/) > (rs->remote_packet_size))
503298948Sobrien    error ("\"monitor\" command ``%s'' is too long\n", command);
503398948Sobrien
503498948Sobrien  /* Encode the actual command */
503598948Sobrien  bin2hex (command, p, 0);
503698948Sobrien
503798948Sobrien  if (putpkt (buf) < 0)
503898948Sobrien    error ("Communication problem with target\n");
503998948Sobrien
504098948Sobrien  /* get/display the response */
504198948Sobrien  while (1)
504298948Sobrien    {
504398948Sobrien      /* XXX - see also tracepoint.c:remote_get_noisy_reply() */
504498948Sobrien      buf[0] = '\0';
504598948Sobrien      getpkt (buf, (rs->remote_packet_size), 0);
504698948Sobrien      if (buf[0] == '\0')
504798948Sobrien	error ("Target does not support this command\n");
504898948Sobrien      if (buf[0] == 'O' && buf[1] != 'K')
504998948Sobrien	{
505098948Sobrien	  remote_console_output (buf + 1); /* 'O' message from stub */
505198948Sobrien	  continue;
505298948Sobrien	}
505398948Sobrien      if (strcmp (buf, "OK") == 0)
505498948Sobrien	break;
505598948Sobrien      if (strlen (buf) == 3 && buf[0] == 'E'
505698948Sobrien	  && isdigit (buf[1]) && isdigit (buf[2]))
505798948Sobrien	{
505898948Sobrien	  error ("Protocol error with Rcmd");
505998948Sobrien	}
506098948Sobrien      for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
506198948Sobrien	{
506298948Sobrien	  char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
506398948Sobrien	  fputc_unfiltered (c, outbuf);
506498948Sobrien	}
506598948Sobrien      break;
506698948Sobrien    }
506798948Sobrien}
506898948Sobrien
506998948Sobrienstatic void
507098948Sobrienpacket_command (char *args, int from_tty)
507198948Sobrien{
507298948Sobrien  struct remote_state *rs = get_remote_state ();
507398948Sobrien  char *buf = alloca (rs->remote_packet_size);
507498948Sobrien
507598948Sobrien  if (!remote_desc)
507646289Sdfr    error ("command can only be used with remote target");
507746289Sdfr
507898948Sobrien  if (!args)
507946289Sdfr    error ("remote-packet command requires packet text as argument");
508046289Sdfr
508146289Sdfr  puts_filtered ("sending: ");
508246289Sdfr  print_packet (args);
508346289Sdfr  puts_filtered ("\n");
508446289Sdfr  putpkt (args);
508546289Sdfr
508698948Sobrien  getpkt (buf, (rs->remote_packet_size), 0);
508746289Sdfr  puts_filtered ("received: ");
508846289Sdfr  print_packet (buf);
508946289Sdfr  puts_filtered ("\n");
509046289Sdfr}
509146289Sdfr
509246289Sdfr#if 0
509346289Sdfr/* --------- UNIT_TEST for THREAD oriented PACKETS ------------------------- */
509446289Sdfr
509598948Sobrienstatic void display_thread_info (struct gdb_ext_thread_info *info);
509646289Sdfr
509798948Sobrienstatic void threadset_test_cmd (char *cmd, int tty);
509846289Sdfr
509998948Sobrienstatic void threadalive_test (char *cmd, int tty);
510046289Sdfr
510198948Sobrienstatic void threadlist_test_cmd (char *cmd, int tty);
510246289Sdfr
510398948Sobrienint get_and_display_threadinfo (threadref * ref);
510446289Sdfr
510598948Sobrienstatic void threadinfo_test_cmd (char *cmd, int tty);
510646289Sdfr
510798948Sobrienstatic int thread_display_step (threadref * ref, void *context);
510846289Sdfr
510998948Sobrienstatic void threadlist_update_test_cmd (char *cmd, int tty);
511046289Sdfr
511198948Sobrienstatic void init_remote_threadtests (void);
511246289Sdfr
511398948Sobrien#define SAMPLE_THREAD  0x05060708	/* Truncated 64 bit threadid */
511446289Sdfr
511546289Sdfrstatic void
511698948Sobrienthreadset_test_cmd (char *cmd, int tty)
511746289Sdfr{
511846289Sdfr  int sample_thread = SAMPLE_THREAD;
511946289Sdfr
512046289Sdfr  printf_filtered ("Remote threadset test\n");
512146289Sdfr  set_thread (sample_thread, 1);
512246289Sdfr}
512346289Sdfr
512446289Sdfr
512546289Sdfrstatic void
512698948Sobrienthreadalive_test (char *cmd, int tty)
512746289Sdfr{
512846289Sdfr  int sample_thread = SAMPLE_THREAD;
512946289Sdfr
513098948Sobrien  if (remote_thread_alive (pid_to_ptid (sample_thread)))
513146289Sdfr    printf_filtered ("PASS: Thread alive test\n");
513246289Sdfr  else
513346289Sdfr    printf_filtered ("FAIL: Thread alive test\n");
513446289Sdfr}
513546289Sdfr
513698948Sobrienvoid output_threadid (char *title, threadref * ref);
513746289Sdfr
513846289Sdfrvoid
513998948Sobrienoutput_threadid (char *title, threadref *ref)
514046289Sdfr{
514146289Sdfr  char hexid[20];
514246289Sdfr
514346289Sdfr  pack_threadid (&hexid[0], ref);	/* Convert threead id into hex */
514446289Sdfr  hexid[16] = 0;
514546289Sdfr  printf_filtered ("%s  %s\n", title, (&hexid[0]));
514646289Sdfr}
514746289Sdfr
514846289Sdfrstatic void
514998948Sobrienthreadlist_test_cmd (char *cmd, int tty)
515046289Sdfr{
515146289Sdfr  int startflag = 1;
515246289Sdfr  threadref nextthread;
515346289Sdfr  int done, result_count;
515446289Sdfr  threadref threadlist[3];
515546289Sdfr
515646289Sdfr  printf_filtered ("Remote Threadlist test\n");
515746289Sdfr  if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
515846289Sdfr			      &result_count, &threadlist[0]))
515946289Sdfr    printf_filtered ("FAIL: threadlist test\n");
516046289Sdfr  else
516146289Sdfr    {
516246289Sdfr      threadref *scan = threadlist;
516346289Sdfr      threadref *limit = scan + result_count;
516446289Sdfr
516546289Sdfr      while (scan < limit)
516646289Sdfr	output_threadid (" thread ", scan++);
516746289Sdfr    }
516846289Sdfr}
516946289Sdfr
517046289Sdfrvoid
517198948Sobriendisplay_thread_info (struct gdb_ext_thread_info *info)
517246289Sdfr{
517346289Sdfr  output_threadid ("Threadid: ", &info->threadid);
517446289Sdfr  printf_filtered ("Name: %s\n ", info->shortname);
517546289Sdfr  printf_filtered ("State: %s\n", info->display);
517646289Sdfr  printf_filtered ("other: %s\n\n", info->more_display);
517746289Sdfr}
517846289Sdfr
517946289Sdfrint
518098948Sobrienget_and_display_threadinfo (threadref *ref)
518146289Sdfr{
518246289Sdfr  int result;
518346289Sdfr  int set;
518446289Sdfr  struct gdb_ext_thread_info threadinfo;
518546289Sdfr
518646289Sdfr  set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
518746289Sdfr    | TAG_MOREDISPLAY | TAG_DISPLAY;
518846289Sdfr  if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
518946289Sdfr    display_thread_info (&threadinfo);
519046289Sdfr  return result;
519146289Sdfr}
519246289Sdfr
519346289Sdfrstatic void
519498948Sobrienthreadinfo_test_cmd (char *cmd, int tty)
519546289Sdfr{
519646289Sdfr  int athread = SAMPLE_THREAD;
519746289Sdfr  threadref thread;
519846289Sdfr  int set;
519946289Sdfr
520046289Sdfr  int_to_threadref (&thread, athread);
520146289Sdfr  printf_filtered ("Remote Threadinfo test\n");
520246289Sdfr  if (!get_and_display_threadinfo (&thread))
520346289Sdfr    printf_filtered ("FAIL cannot get thread info\n");
520446289Sdfr}
520546289Sdfr
520646289Sdfrstatic int
520798948Sobrienthread_display_step (threadref *ref, void *context)
520846289Sdfr{
520946289Sdfr  /* output_threadid(" threadstep ",ref); *//* simple test */
521046289Sdfr  return get_and_display_threadinfo (ref);
521146289Sdfr}
521246289Sdfr
521346289Sdfrstatic void
521498948Sobrienthreadlist_update_test_cmd (char *cmd, int tty)
521546289Sdfr{
521646289Sdfr  printf_filtered ("Remote Threadlist update test\n");
521746289Sdfr  remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
521846289Sdfr}
521946289Sdfr
522046289Sdfrstatic void
522146289Sdfrinit_remote_threadtests (void)
522246289Sdfr{
522346289Sdfr  add_com ("tlist", class_obscure, threadlist_test_cmd,
522446289Sdfr     "Fetch and print the remote list of thread identifiers, one pkt only");
522546289Sdfr  add_com ("tinfo", class_obscure, threadinfo_test_cmd,
522646289Sdfr	   "Fetch and display info about one thread");
522746289Sdfr  add_com ("tset", class_obscure, threadset_test_cmd,
522846289Sdfr	   "Test setting to a different thread");
522946289Sdfr  add_com ("tupd", class_obscure, threadlist_update_test_cmd,
523046289Sdfr	   "Iterate through updating all remote thread info");
523146289Sdfr  add_com ("talive", class_obscure, threadalive_test,
523246289Sdfr	   " Remote thread alive test ");
523346289Sdfr}
523446289Sdfr
523546289Sdfr#endif /* 0 */
523646289Sdfr
523798948Sobrien/* Convert a thread ID to a string.  Returns the string in a static
523898948Sobrien   buffer.  */
523998948Sobrien
524098948Sobrienstatic char *
524198948Sobrienremote_pid_to_str (ptid_t ptid)
524298948Sobrien{
524398948Sobrien  static char buf[30];
524498948Sobrien
524598948Sobrien  sprintf (buf, "Thread %d", PIDGET (ptid));
524698948Sobrien  return buf;
524798948Sobrien}
524898948Sobrien
524946289Sdfrstatic void
525098948Sobrieninit_remote_ops (void)
525146289Sdfr{
525298948Sobrien  remote_ops.to_shortname = "remote";
525346289Sdfr  remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
525498948Sobrien  remote_ops.to_doc =
525546289Sdfr    "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
525698948SobrienSpecify the serial device it is connected to\n\
525798948Sobrien(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
525898948Sobrien  remote_ops.to_open = remote_open;
525998948Sobrien  remote_ops.to_close = remote_close;
526046289Sdfr  remote_ops.to_detach = remote_detach;
5261130809Smarcel  remote_ops.to_disconnect = remote_disconnect;
526298948Sobrien  remote_ops.to_resume = remote_resume;
526346289Sdfr  remote_ops.to_wait = remote_wait;
526446289Sdfr  remote_ops.to_fetch_registers = remote_fetch_registers;
526546289Sdfr  remote_ops.to_store_registers = remote_store_registers;
526646289Sdfr  remote_ops.to_prepare_to_store = remote_prepare_to_store;
526798948Sobrien  remote_ops.to_xfer_memory = remote_xfer_memory;
526898948Sobrien  remote_ops.to_files_info = remote_files_info;
526946289Sdfr  remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
527046289Sdfr  remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
5271130809Smarcel  remote_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
5272130809Smarcel  remote_ops.to_stopped_data_address = remote_stopped_data_address;
5273130809Smarcel  remote_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
5274130809Smarcel  remote_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
5275130809Smarcel  remote_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
5276130809Smarcel  remote_ops.to_insert_watchpoint = remote_insert_watchpoint;
5277130809Smarcel  remote_ops.to_remove_watchpoint = remote_remove_watchpoint;
527898948Sobrien  remote_ops.to_kill = remote_kill;
527998948Sobrien  remote_ops.to_load = generic_load;
528046289Sdfr  remote_ops.to_mourn_inferior = remote_mourn;
528146289Sdfr  remote_ops.to_thread_alive = remote_thread_alive;
528298948Sobrien  remote_ops.to_find_new_threads = remote_threads_info;
528398948Sobrien  remote_ops.to_pid_to_str = remote_pid_to_str;
528498948Sobrien  remote_ops.to_extra_thread_info = remote_threads_extra_info;
528546289Sdfr  remote_ops.to_stop = remote_stop;
5286130809Smarcel  remote_ops.to_xfer_partial = remote_xfer_partial;
528798948Sobrien  remote_ops.to_rcmd = remote_rcmd;
528846289Sdfr  remote_ops.to_stratum = process_stratum;
528998948Sobrien  remote_ops.to_has_all_memory = 1;
529098948Sobrien  remote_ops.to_has_memory = 1;
529198948Sobrien  remote_ops.to_has_stack = 1;
529298948Sobrien  remote_ops.to_has_registers = 1;
529398948Sobrien  remote_ops.to_has_execution = 1;
529498948Sobrien  remote_ops.to_has_thread_control = tc_schedlock;	/* can lock scheduler */
529598948Sobrien  remote_ops.to_magic = OPS_MAGIC;
529646289Sdfr}
529746289Sdfr
529846289Sdfr/* Set up the extended remote vector by making a copy of the standard
529946289Sdfr   remote vector and adding to it.  */
530046289Sdfr
530146289Sdfrstatic void
530298948Sobrieninit_extended_remote_ops (void)
530346289Sdfr{
530446289Sdfr  extended_remote_ops = remote_ops;
530546289Sdfr
530698948Sobrien  extended_remote_ops.to_shortname = "extended-remote";
530798948Sobrien  extended_remote_ops.to_longname =
530846289Sdfr    "Extended remote serial target in gdb-specific protocol";
530998948Sobrien  extended_remote_ops.to_doc =
531046289Sdfr    "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
531146289SdfrSpecify the serial device it is connected to (e.g. /dev/ttya).",
531298948Sobrien    extended_remote_ops.to_open = extended_remote_open;
531346289Sdfr  extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
531446289Sdfr  extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
531598948Sobrien}
531646289Sdfr
531798948Sobrienstatic int
531898948Sobrienremote_can_async_p (void)
531998948Sobrien{
532098948Sobrien  /* We're async whenever the serial device is. */
532198948Sobrien  return (current_target.to_async_mask_value) && serial_can_async_p (remote_desc);
532298948Sobrien}
532398948Sobrien
532498948Sobrienstatic int
532598948Sobrienremote_is_async_p (void)
532698948Sobrien{
532798948Sobrien  /* We're async whenever the serial device is. */
532898948Sobrien  return (current_target.to_async_mask_value) && serial_is_async_p (remote_desc);
532998948Sobrien}
533098948Sobrien
533198948Sobrien/* Pass the SERIAL event on and up to the client.  One day this code
533298948Sobrien   will be able to delay notifying the client of an event until the
533398948Sobrien   point where an entire packet has been received. */
533498948Sobrien
533598948Sobrienstatic void (*async_client_callback) (enum inferior_event_type event_type, void *context);
533698948Sobrienstatic void *async_client_context;
533798948Sobrienstatic serial_event_ftype remote_async_serial_handler;
533898948Sobrien
533998948Sobrienstatic void
534098948Sobrienremote_async_serial_handler (struct serial *scb, void *context)
534198948Sobrien{
534298948Sobrien  /* Don't propogate error information up to the client.  Instead let
534398948Sobrien     the client find out about the error by querying the target.  */
534498948Sobrien  async_client_callback (INF_REG_EVENT, async_client_context);
534598948Sobrien}
534698948Sobrien
534798948Sobrienstatic void
534898948Sobrienremote_async (void (*callback) (enum inferior_event_type event_type, void *context), void *context)
534998948Sobrien{
535098948Sobrien  if (current_target.to_async_mask_value == 0)
535198948Sobrien    internal_error (__FILE__, __LINE__,
535298948Sobrien		    "Calling remote_async when async is masked");
535398948Sobrien
535498948Sobrien  if (callback != NULL)
535598948Sobrien    {
535698948Sobrien      serial_async (remote_desc, remote_async_serial_handler, NULL);
535798948Sobrien      async_client_callback = callback;
535898948Sobrien      async_client_context = context;
535998948Sobrien    }
536098948Sobrien  else
536198948Sobrien    serial_async (remote_desc, NULL, NULL);
536298948Sobrien}
536398948Sobrien
536498948Sobrien/* Target async and target extended-async.
536598948Sobrien
536698948Sobrien   This are temporary targets, until it is all tested.  Eventually
536798948Sobrien   async support will be incorporated int the usual 'remote'
536898948Sobrien   target. */
536998948Sobrien
537098948Sobrienstatic void
537198948Sobrieninit_remote_async_ops (void)
537298948Sobrien{
537398948Sobrien  remote_async_ops.to_shortname = "async";
537498948Sobrien  remote_async_ops.to_longname = "Remote serial target in async version of the gdb-specific protocol";
537598948Sobrien  remote_async_ops.to_doc =
537698948Sobrien    "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
537798948SobrienSpecify the serial device it is connected to (e.g. /dev/ttya).";
537898948Sobrien  remote_async_ops.to_open = remote_async_open;
537998948Sobrien  remote_async_ops.to_close = remote_close;
5380130809Smarcel  remote_async_ops.to_detach = remote_detach;
5381130809Smarcel  remote_async_ops.to_disconnect = remote_disconnect;
538298948Sobrien  remote_async_ops.to_resume = remote_async_resume;
538398948Sobrien  remote_async_ops.to_wait = remote_async_wait;
538498948Sobrien  remote_async_ops.to_fetch_registers = remote_fetch_registers;
538598948Sobrien  remote_async_ops.to_store_registers = remote_store_registers;
538698948Sobrien  remote_async_ops.to_prepare_to_store = remote_prepare_to_store;
538798948Sobrien  remote_async_ops.to_xfer_memory = remote_xfer_memory;
538898948Sobrien  remote_async_ops.to_files_info = remote_files_info;
538998948Sobrien  remote_async_ops.to_insert_breakpoint = remote_insert_breakpoint;
539098948Sobrien  remote_async_ops.to_remove_breakpoint = remote_remove_breakpoint;
5391130809Smarcel  remote_async_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
5392130809Smarcel  remote_async_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
5393130809Smarcel  remote_async_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
5394130809Smarcel  remote_async_ops.to_insert_watchpoint = remote_insert_watchpoint;
5395130809Smarcel  remote_async_ops.to_remove_watchpoint = remote_remove_watchpoint;
5396130809Smarcel  remote_async_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
5397130809Smarcel  remote_async_ops.to_stopped_data_address = remote_stopped_data_address;
539898948Sobrien  remote_async_ops.to_terminal_inferior = remote_async_terminal_inferior;
539998948Sobrien  remote_async_ops.to_terminal_ours = remote_async_terminal_ours;
540098948Sobrien  remote_async_ops.to_kill = remote_async_kill;
540198948Sobrien  remote_async_ops.to_load = generic_load;
540298948Sobrien  remote_async_ops.to_mourn_inferior = remote_async_mourn;
540398948Sobrien  remote_async_ops.to_thread_alive = remote_thread_alive;
540498948Sobrien  remote_async_ops.to_find_new_threads = remote_threads_info;
540598948Sobrien  remote_async_ops.to_pid_to_str = remote_pid_to_str;
540698948Sobrien  remote_async_ops.to_extra_thread_info = remote_threads_extra_info;
540798948Sobrien  remote_async_ops.to_stop = remote_stop;
5408130809Smarcel  remote_async_ops.to_xfer_partial = remote_xfer_partial;
540998948Sobrien  remote_async_ops.to_rcmd = remote_rcmd;
541098948Sobrien  remote_async_ops.to_stratum = process_stratum;
541198948Sobrien  remote_async_ops.to_has_all_memory = 1;
541298948Sobrien  remote_async_ops.to_has_memory = 1;
541398948Sobrien  remote_async_ops.to_has_stack = 1;
541498948Sobrien  remote_async_ops.to_has_registers = 1;
541598948Sobrien  remote_async_ops.to_has_execution = 1;
541698948Sobrien  remote_async_ops.to_has_thread_control = tc_schedlock;	/* can lock scheduler */
541798948Sobrien  remote_async_ops.to_can_async_p = remote_can_async_p;
541898948Sobrien  remote_async_ops.to_is_async_p = remote_is_async_p;
541998948Sobrien  remote_async_ops.to_async = remote_async;
542098948Sobrien  remote_async_ops.to_async_mask_value = 1;
542198948Sobrien  remote_async_ops.to_magic = OPS_MAGIC;
542298948Sobrien}
542398948Sobrien
542498948Sobrien/* Set up the async extended remote vector by making a copy of the standard
542598948Sobrien   remote vector and adding to it.  */
542698948Sobrien
542798948Sobrienstatic void
542898948Sobrieninit_extended_async_remote_ops (void)
542998948Sobrien{
543098948Sobrien  extended_async_remote_ops = remote_async_ops;
543198948Sobrien
543298948Sobrien  extended_async_remote_ops.to_shortname = "extended-async";
543398948Sobrien  extended_async_remote_ops.to_longname =
543498948Sobrien    "Extended remote serial target in async gdb-specific protocol";
543598948Sobrien  extended_async_remote_ops.to_doc =
543698948Sobrien    "Use a remote computer via a serial line, using an async gdb-specific protocol.\n\
543798948SobrienSpecify the serial device it is connected to (e.g. /dev/ttya).",
543898948Sobrien    extended_async_remote_ops.to_open = extended_remote_async_open;
543998948Sobrien  extended_async_remote_ops.to_create_inferior = extended_remote_async_create_inferior;
544098948Sobrien  extended_async_remote_ops.to_mourn_inferior = extended_remote_mourn;
544198948Sobrien}
544298948Sobrien
544398948Sobrienstatic void
544498948Sobrienset_remote_cmd (char *args, int from_tty)
544598948Sobrien{
544698948Sobrien}
544798948Sobrien
544898948Sobrienstatic void
544998948Sobrienshow_remote_cmd (char *args, int from_tty)
545098948Sobrien{
5451130809Smarcel  /* FIXME: cagney/2002-06-15: This function should iterate over
5452130809Smarcel     remote_show_cmdlist for a list of sub commands to show.  */
5453130809Smarcel  show_remote_protocol_Z_packet_cmd (args, from_tty, NULL);
5454130809Smarcel  show_remote_protocol_e_packet_cmd (args, from_tty, NULL);
5455130809Smarcel  show_remote_protocol_E_packet_cmd (args, from_tty, NULL);
5456130809Smarcel  show_remote_protocol_P_packet_cmd (args, from_tty, NULL);
5457130809Smarcel  show_remote_protocol_qSymbol_packet_cmd (args, from_tty, NULL);
5458130809Smarcel  show_remote_protocol_vcont_packet_cmd (args, from_tty, NULL);
5459130809Smarcel  show_remote_protocol_binary_download_cmd (args, from_tty, NULL);
5460130809Smarcel  show_remote_protocol_qPart_auxv_packet_cmd (args, from_tty, NULL);
5461131086Smarcel  show_remote_protocol_qPart_dirty_packet_cmd (args, from_tty, NULL);
546298948Sobrien}
546398948Sobrien
546498948Sobrienstatic void
546598948Sobrienbuild_remote_gdbarch_data (void)
546698948Sobrien{
546798948Sobrien  remote_address_size = TARGET_ADDR_BIT;
546898948Sobrien}
546998948Sobrien
547098948Sobrien/* Saved pointer to previous owner of the new_objfile event. */
547198948Sobrienstatic void (*remote_new_objfile_chain) (struct objfile *);
547298948Sobrien
547398948Sobrien/* Function to be called whenever a new objfile (shlib) is detected. */
547498948Sobrienstatic void
547598948Sobrienremote_new_objfile (struct objfile *objfile)
547698948Sobrien{
547798948Sobrien  if (remote_desc != 0)		/* Have a remote connection */
547898948Sobrien    {
547998948Sobrien      remote_check_symbols (objfile);
548098948Sobrien    }
548198948Sobrien  /* Call predecessor on chain, if any. */
548298948Sobrien  if (remote_new_objfile_chain != 0 &&
548398948Sobrien      remote_desc == 0)
548498948Sobrien    remote_new_objfile_chain (objfile);
548598948Sobrien}
548698948Sobrien
548746289Sdfrvoid
548898948Sobrien_initialize_remote (void)
548919370Spst{
549098948Sobrien  static struct cmd_list_element *remote_set_cmdlist;
549198948Sobrien  static struct cmd_list_element *remote_show_cmdlist;
549298948Sobrien  struct cmd_list_element *tmpcmd;
549398948Sobrien
549498948Sobrien  /* architecture specific data */
5495130809Smarcel  remote_gdbarch_data_handle = register_gdbarch_data (init_remote_state);
549698948Sobrien
549798948Sobrien  /* Old tacky stuff.  NOTE: This comes after the remote protocol so
549898948Sobrien     that the remote protocol has been initialized.  */
5499130809Smarcel  DEPRECATED_REGISTER_GDBARCH_SWAP (remote_address_size);
5500130809Smarcel  deprecated_register_gdbarch_swap (NULL, 0, build_remote_gdbarch_data);
550198948Sobrien
550246289Sdfr  init_remote_ops ();
550319370Spst  add_target (&remote_ops);
550446289Sdfr
550546289Sdfr  init_extended_remote_ops ();
550619370Spst  add_target (&extended_remote_ops);
550798948Sobrien
550898948Sobrien  init_remote_async_ops ();
550998948Sobrien  add_target (&remote_async_ops);
551098948Sobrien
551198948Sobrien  init_extended_async_remote_ops ();
551298948Sobrien  add_target (&extended_async_remote_ops);
551398948Sobrien
551498948Sobrien  /* Hook into new objfile notification.  */
551598948Sobrien  remote_new_objfile_chain = target_new_objfile_hook;
551698948Sobrien  target_new_objfile_hook  = remote_new_objfile;
551798948Sobrien
551846289Sdfr#if 0
551946289Sdfr  init_remote_threadtests ();
552046289Sdfr#endif
552119370Spst
552298948Sobrien  /* set/show remote ... */
552398948Sobrien
552498948Sobrien  add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, "\
552598948SobrienRemote protocol specific variables\n\
552698948SobrienConfigure various remote-protocol specific variables such as\n\
552798948Sobrienthe packets being used",
552898948Sobrien		  &remote_set_cmdlist, "set remote ",
552998948Sobrien		  0/*allow-unknown*/, &setlist);
553098948Sobrien  add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, "\
553198948SobrienRemote protocol specific variables\n\
553298948SobrienConfigure various remote-protocol specific variables such as\n\
553398948Sobrienthe packets being used",
553498948Sobrien		  &remote_show_cmdlist, "show remote ",
553598948Sobrien		  0/*allow-unknown*/, &showlist);
553698948Sobrien
553798948Sobrien  add_cmd ("compare-sections", class_obscure, compare_sections_command,
553846289Sdfr	   "Compare section data on target to the exec file.\n\
553998948SobrienArgument is a single section name (default: all loaded sections).",
554046289Sdfr	   &cmdlist);
554119370Spst
554246289Sdfr  add_cmd ("packet", class_maintenance, packet_command,
554346289Sdfr	   "Send an arbitrary packet to a remote target.\n\
554446289Sdfr   maintenance packet TEXT\n\
554546289SdfrIf GDB is talking to an inferior via the GDB serial protocol, then\n\
554646289Sdfrthis command sends the string TEXT to the inferior, and displays the\n\
554746289Sdfrresponse packet.  GDB supplies the initial `$' character, and the\n\
554846289Sdfrterminating `#' character and checksum.",
554946289Sdfr	   &maintenancelist);
555021738Sgj
5551130809Smarcel  add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break,
5552130809Smarcel			   "Set whether to send break if interrupted.\n",
5553130809Smarcel			   "Show whether to send break if interrupted.\n",
5554130809Smarcel			   NULL, NULL,
5555130809Smarcel			   &setlist, &showlist);
555646289Sdfr
555798948Sobrien  /* Install commands for configuring memory read/write packets. */
555846289Sdfr
555998948Sobrien  add_cmd ("remotewritesize", no_class, set_memory_write_packet_size,
556098948Sobrien	   "Set the maximum number of bytes per memory write packet (deprecated).\n",
556198948Sobrien	   &setlist);
556298948Sobrien  add_cmd ("remotewritesize", no_class, show_memory_write_packet_size,
556398948Sobrien	   "Show the maximum number of bytes per memory write packet (deprecated).\n",
556498948Sobrien	   &showlist);
556598948Sobrien  add_cmd ("memory-write-packet-size", no_class,
556698948Sobrien	   set_memory_write_packet_size,
556798948Sobrien	   "Set the maximum number of bytes per memory-write packet.\n"
556898948Sobrien	   "Specify the number of bytes in a packet or 0 (zero) for the\n"
556998948Sobrien	   "default packet size.  The actual limit is further reduced\n"
557098948Sobrien	   "dependent on the target.  Specify ``fixed'' to disable the\n"
557198948Sobrien	   "further restriction and ``limit'' to enable that restriction\n",
557298948Sobrien	   &remote_set_cmdlist);
557398948Sobrien  add_cmd ("memory-read-packet-size", no_class,
557498948Sobrien	   set_memory_read_packet_size,
557598948Sobrien	   "Set the maximum number of bytes per memory-read packet.\n"
557698948Sobrien	   "Specify the number of bytes in a packet or 0 (zero) for the\n"
557798948Sobrien	   "default packet size.  The actual limit is further reduced\n"
557898948Sobrien	   "dependent on the target.  Specify ``fixed'' to disable the\n"
557998948Sobrien	   "further restriction and ``limit'' to enable that restriction\n",
558098948Sobrien	   &remote_set_cmdlist);
558198948Sobrien  add_cmd ("memory-write-packet-size", no_class,
558298948Sobrien	   show_memory_write_packet_size,
558398948Sobrien	   "Show the maximum number of bytes per memory-write packet.\n",
558498948Sobrien	   &remote_show_cmdlist);
558598948Sobrien  add_cmd ("memory-read-packet-size", no_class,
558698948Sobrien	   show_memory_read_packet_size,
558798948Sobrien	   "Show the maximum number of bytes per memory-read packet.\n",
558898948Sobrien	   &remote_show_cmdlist);
558946289Sdfr
5590130809Smarcel  add_setshow_cmd ("hardware-watchpoint-limit", no_class,
5591130809Smarcel		   var_zinteger, &remote_hw_watchpoint_limit, "\
5592130809SmarcelSet the maximum number of target hardware watchpoints.\n\
5593130809SmarcelSpecify a negative limit for unlimited.", "\
5594130809SmarcelShow the maximum number of target hardware watchpoints.\n",
5595130809Smarcel		   NULL, NULL, &remote_set_cmdlist, &remote_show_cmdlist);
5596130809Smarcel  add_setshow_cmd ("hardware-breakpoint-limit", no_class,
5597130809Smarcel		   var_zinteger, &remote_hw_breakpoint_limit, "\
5598130809SmarcelSet the maximum number of target hardware breakpoints.\n\
5599130809SmarcelSpecify a negative limit for unlimited.", "\
5600130809SmarcelShow the maximum number of target hardware breakpoints.\n",
5601130809Smarcel		   NULL, NULL, &remote_set_cmdlist, &remote_show_cmdlist);
5602130809Smarcel
560398948Sobrien  add_show_from_set
560446289Sdfr    (add_set_cmd ("remoteaddresssize", class_obscure,
560598948Sobrien		  var_integer, (char *) &remote_address_size,
560646289Sdfr		  "Set the maximum size of the address (in bits) \
560746289Sdfrin a memory packet.\n",
560846289Sdfr		  &setlist),
560998948Sobrien     &showlist);
561046289Sdfr
561198948Sobrien  add_packet_config_cmd (&remote_protocol_binary_download,
561298948Sobrien			 "X", "binary-download",
561398948Sobrien			 set_remote_protocol_binary_download_cmd,
561498948Sobrien			 show_remote_protocol_binary_download_cmd,
561598948Sobrien			 &remote_set_cmdlist, &remote_show_cmdlist,
561698948Sobrien			 1);
561798948Sobrien#if 0
561898948Sobrien  /* XXXX - should ``set remotebinarydownload'' be retained for
561998948Sobrien     compatibility. */
562098948Sobrien  add_show_from_set
562198948Sobrien    (add_set_cmd ("remotebinarydownload", no_class,
562298948Sobrien		  var_boolean, (char *) &remote_binary_download,
562398948Sobrien		  "Set binary downloads.\n", &setlist),
562498948Sobrien     &showlist);
562598948Sobrien#endif
562698948Sobrien
5627130809Smarcel  add_packet_config_cmd (&remote_protocol_vcont,
5628130809Smarcel			 "vCont", "verbose-resume",
5629130809Smarcel			 set_remote_protocol_vcont_packet_cmd,
5630130809Smarcel			 show_remote_protocol_vcont_packet_cmd,
5631130809Smarcel			 &remote_set_cmdlist, &remote_show_cmdlist,
5632130809Smarcel			 0);
563398948Sobrien
563498948Sobrien  add_packet_config_cmd (&remote_protocol_qSymbol,
563598948Sobrien			 "qSymbol", "symbol-lookup",
563698948Sobrien			 set_remote_protocol_qSymbol_packet_cmd,
563798948Sobrien			 show_remote_protocol_qSymbol_packet_cmd,
563898948Sobrien			 &remote_set_cmdlist, &remote_show_cmdlist,
563998948Sobrien			 0);
564098948Sobrien
564198948Sobrien  add_packet_config_cmd (&remote_protocol_e,
564298948Sobrien			 "e", "step-over-range",
564398948Sobrien			 set_remote_protocol_e_packet_cmd,
564498948Sobrien			 show_remote_protocol_e_packet_cmd,
564598948Sobrien			 &remote_set_cmdlist, &remote_show_cmdlist,
564698948Sobrien			 0);
564798948Sobrien  /* Disable by default.  The ``e'' packet has nasty interactions with
564898948Sobrien     the threading code - it relies on global state.  */
5649130809Smarcel  remote_protocol_e.detect = AUTO_BOOLEAN_FALSE;
565098948Sobrien  update_packet_config (&remote_protocol_e);
565198948Sobrien
565298948Sobrien  add_packet_config_cmd (&remote_protocol_E,
565398948Sobrien			 "E", "step-over-range-w-signal",
565498948Sobrien			 set_remote_protocol_E_packet_cmd,
565598948Sobrien			 show_remote_protocol_E_packet_cmd,
565698948Sobrien			 &remote_set_cmdlist, &remote_show_cmdlist,
565798948Sobrien			 0);
565898948Sobrien  /* Disable by default.  The ``e'' packet has nasty interactions with
565998948Sobrien     the threading code - it relies on global state.  */
5660130809Smarcel  remote_protocol_E.detect = AUTO_BOOLEAN_FALSE;
566198948Sobrien  update_packet_config (&remote_protocol_E);
566298948Sobrien
566398948Sobrien  add_packet_config_cmd (&remote_protocol_P,
566498948Sobrien			 "P", "set-register",
566598948Sobrien			 set_remote_protocol_P_packet_cmd,
566698948Sobrien			 show_remote_protocol_P_packet_cmd,
566798948Sobrien			 &remote_set_cmdlist, &remote_show_cmdlist,
566898948Sobrien			 1);
566998948Sobrien
567098948Sobrien  add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_SOFTWARE_BP],
567198948Sobrien			 "Z0", "software-breakpoint",
567298948Sobrien			 set_remote_protocol_Z_software_bp_packet_cmd,
567398948Sobrien			 show_remote_protocol_Z_software_bp_packet_cmd,
567498948Sobrien			 &remote_set_cmdlist, &remote_show_cmdlist,
567598948Sobrien			 0);
567698948Sobrien
567798948Sobrien  add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_HARDWARE_BP],
567898948Sobrien			 "Z1", "hardware-breakpoint",
567998948Sobrien			 set_remote_protocol_Z_hardware_bp_packet_cmd,
568098948Sobrien			 show_remote_protocol_Z_hardware_bp_packet_cmd,
568198948Sobrien			 &remote_set_cmdlist, &remote_show_cmdlist,
568298948Sobrien			 0);
568398948Sobrien
568498948Sobrien  add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_WRITE_WP],
568598948Sobrien			 "Z2", "write-watchpoint",
568698948Sobrien			 set_remote_protocol_Z_write_wp_packet_cmd,
568798948Sobrien			 show_remote_protocol_Z_write_wp_packet_cmd,
568898948Sobrien			 &remote_set_cmdlist, &remote_show_cmdlist,
568998948Sobrien			 0);
569098948Sobrien
569198948Sobrien  add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_READ_WP],
569298948Sobrien			 "Z3", "read-watchpoint",
569398948Sobrien			 set_remote_protocol_Z_read_wp_packet_cmd,
569498948Sobrien			 show_remote_protocol_Z_read_wp_packet_cmd,
569598948Sobrien			 &remote_set_cmdlist, &remote_show_cmdlist,
569698948Sobrien			 0);
569798948Sobrien
569898948Sobrien  add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_ACCESS_WP],
569998948Sobrien			 "Z4", "access-watchpoint",
570098948Sobrien			 set_remote_protocol_Z_access_wp_packet_cmd,
570198948Sobrien			 show_remote_protocol_Z_access_wp_packet_cmd,
570298948Sobrien			 &remote_set_cmdlist, &remote_show_cmdlist,
570398948Sobrien			 0);
570498948Sobrien
5705130809Smarcel  add_packet_config_cmd (&remote_protocol_qPart_auxv,
5706130809Smarcel			 "qPart_auxv", "read-aux-vector",
5707130809Smarcel			 set_remote_protocol_qPart_auxv_packet_cmd,
5708130809Smarcel			 show_remote_protocol_qPart_auxv_packet_cmd,
5709130809Smarcel			 &remote_set_cmdlist, &remote_show_cmdlist,
5710130809Smarcel			 0);
5711130809Smarcel
5712131086Smarcel  add_packet_config_cmd (&remote_protocol_qPart_dirty,
5713131086Smarcel			 "qPart_dirty", "read-dirty-registers",
5714131086Smarcel			 set_remote_protocol_qPart_dirty_packet_cmd,
5715131086Smarcel			 show_remote_protocol_qPart_dirty_packet_cmd,
5716131086Smarcel			 &remote_set_cmdlist, &remote_show_cmdlist,
5717131086Smarcel			 0);
5718131086Smarcel
571998948Sobrien  /* Keep the old ``set remote Z-packet ...'' working. */
5720130809Smarcel  add_setshow_auto_boolean_cmd ("Z-packet", class_obscure,
5721130809Smarcel				&remote_Z_packet_detect, "\
5722130809SmarcelSet use of remote protocol `Z' packets",
5723130809Smarcel				"Show use of remote protocol `Z' packets ",
5724130809Smarcel				set_remote_protocol_Z_packet_cmd,
5725130809Smarcel				show_remote_protocol_Z_packet_cmd,
5726130809Smarcel				&remote_set_cmdlist, &remote_show_cmdlist);
5727130809Smarcel
5728130809Smarcel  /* Eventually initialize fileio.  See fileio.c */
5729130809Smarcel  initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist);
573019370Spst}
5731