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