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