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