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