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