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