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