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