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