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