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