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