]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/remote.c
Add support for attiny261, attiny461, attiny861, attiny25, attiny45,
[thirdparty/binutils-gdb.git] / gdb / remote.c
CommitLineData
c906108c 1/* Remote target communications for serial-line targets in custom GDB protocol
8926118c 2
197e01b6 3 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
24b06219 4 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
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
11 the Free Software Foundation; either version 2 of the License, or
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
20 along with this program; if not, write to the Free Software
197e01b6
EZ
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
c5aa993b 23
23860348 24/* See the GDB User Guide for details of the GDB remote protocol. */
c5aa993b 25
c906108c
SS
26#include "defs.h"
27#include "gdb_string.h"
28#include <ctype.h>
29#include <fcntl.h>
c906108c
SS
30#include "inferior.h"
31#include "bfd.h"
32#include "symfile.h"
60250e8b 33#include "exceptions.h"
c906108c 34#include "target.h"
c5aa993b 35/*#include "terminal.h" */
c906108c
SS
36#include "gdbcmd.h"
37#include "objfiles.h"
38#include "gdb-stabs.h"
39#include "gdbthread.h"
c2c6d25f 40#include "remote.h"
4e052eda 41#include "regcache.h"
fd0407d6 42#include "value.h"
1ff9c3d6 43#include "gdb_assert.h"
6867ae3e 44#include "observer.h"
a77053c2 45#include "solib.h"
37a105a1
DJ
46#include "cli/cli-decode.h"
47#include "cli/cli-setshow.h"
c906108c 48
7a292a7a 49#include <ctype.h>
9846de1b 50#include <sys/time.h>
c906108c 51
43ff13b4 52#include "event-loop.h"
c2c6d25f 53#include "event-top.h"
2acceee2 54#include "inf-loop.h"
43ff13b4 55
c906108c
SS
56#include <signal.h>
57#include "serial.h"
58
6240bebf
MS
59#include "gdbcore.h" /* for exec_bfd */
60
449092f6
CV
61#include "remote-fileio.h"
62
23860348 63/* Prototypes for local functions. */
6426a772
JM
64static void cleanup_sigint_signal_handler (void *dummy);
65static void initialize_sigint_signal_handler (void);
6d820c5c 66static int getpkt_sane (char **buf, long *sizeof_buf, int forever);
6426a772 67
a14ed312
KB
68static void handle_remote_sigint (int);
69static void handle_remote_sigint_twice (int);
70static void async_remote_interrupt (gdb_client_data);
71void async_remote_interrupt_twice (gdb_client_data);
43ff13b4 72
a14ed312 73static void build_remote_gdbarch_data (void);
0f71a2f6 74
a14ed312 75static void remote_files_info (struct target_ops *ignore);
c906108c 76
a14ed312 77static void remote_prepare_to_store (void);
c906108c 78
a14ed312 79static void remote_fetch_registers (int regno);
c906108c 80
39f77062
KB
81static void remote_resume (ptid_t ptid, int step,
82 enum target_signal siggnal);
83static void remote_async_resume (ptid_t ptid, int step,
a14ed312 84 enum target_signal siggnal);
a14ed312
KB
85static void remote_open (char *name, int from_tty);
86static void remote_async_open (char *name, int from_tty);
c906108c 87
a14ed312
KB
88static void extended_remote_open (char *name, int from_tty);
89static void extended_remote_async_open (char *name, int from_tty);
c906108c 90
92d1e331
DJ
91static void remote_open_1 (char *, int, struct target_ops *, int extended_p,
92 int async_p);
c906108c 93
a14ed312 94static void remote_close (int quitting);
c906108c 95
a14ed312 96static void remote_store_registers (int regno);
c906108c 97
a14ed312
KB
98static void remote_mourn (void);
99static void remote_async_mourn (void);
c906108c 100
a14ed312 101static void extended_remote_restart (void);
c906108c 102
a14ed312 103static void extended_remote_mourn (void);
c906108c 104
a14ed312 105static void remote_mourn_1 (struct target_ops *);
c906108c 106
6d820c5c 107static void remote_send (char **buf, long *sizeof_buf_p);
c906108c 108
a14ed312 109static int readchar (int timeout);
c906108c 110
39f77062
KB
111static ptid_t remote_wait (ptid_t ptid,
112 struct target_waitstatus *status);
113static ptid_t remote_async_wait (ptid_t ptid,
114 struct target_waitstatus *status);
c906108c 115
a14ed312
KB
116static void remote_kill (void);
117static void remote_async_kill (void);
c906108c 118
a14ed312 119static int tohex (int nib);
c906108c 120
a14ed312 121static void remote_detach (char *args, int from_tty);
c906108c 122
a14ed312 123static void remote_interrupt (int signo);
c906108c 124
a14ed312 125static void remote_interrupt_twice (int signo);
7a292a7a 126
a14ed312 127static void interrupt_query (void);
c906108c 128
a14ed312 129static void set_thread (int, int);
c906108c 130
39f77062 131static int remote_thread_alive (ptid_t);
c906108c 132
a14ed312 133static void get_offsets (void);
c906108c 134
6d820c5c
DJ
135static void skip_frame (void);
136
137static long read_frame (char **buf_p, long *sizeof_buf);
c906108c 138
5a168c78 139static int remote_insert_breakpoint (CORE_ADDR, bfd_byte *);
c906108c 140
5a168c78 141static int remote_remove_breakpoint (CORE_ADDR, bfd_byte *);
c906108c 142
a14ed312 143static int hexnumlen (ULONGEST num);
c906108c 144
a14ed312 145static void init_remote_ops (void);
c906108c 146
a14ed312 147static void init_extended_remote_ops (void);
c906108c 148
a14ed312 149static void remote_stop (void);
c906108c 150
a14ed312 151static int ishex (int ch, int *val);
c906108c 152
a14ed312 153static int stubhex (int ch);
c906108c 154
a14ed312 155static int hexnumstr (char *, ULONGEST);
c906108c 156
a14ed312 157static int hexnumnstr (char *, ULONGEST, int);
2df3850c 158
a14ed312 159static CORE_ADDR remote_address_masked (CORE_ADDR);
c906108c 160
a14ed312 161static void print_packet (char *);
c906108c 162
a14ed312 163static unsigned long crc32 (unsigned char *, int, unsigned int);
c906108c 164
a14ed312 165static void compare_sections_command (char *, int);
c906108c 166
a14ed312 167static void packet_command (char *, int);
c906108c 168
a14ed312 169static int stub_unpack_int (char *buff, int fieldlength);
c906108c 170
39f77062 171static ptid_t remote_current_thread (ptid_t oldptid);
c906108c 172
a14ed312 173static void remote_find_new_threads (void);
c906108c 174
a14ed312 175static void record_currthread (int currthread);
c906108c 176
30559e10 177static int fromhex (int a);
c906108c 178
cfd77fa1 179static int hex2bin (const char *hex, gdb_byte *bin, int count);
c906108c 180
cfd77fa1 181static int bin2hex (const gdb_byte *bin, char *hex, int count);
234fa6d1 182
a14ed312 183static int putpkt_binary (char *buf, int cnt);
c906108c 184
a14ed312 185static void check_binary_download (CORE_ADDR addr);
c906108c 186
5a2468f5 187struct packet_config;
5a2468f5 188
a14ed312 189static void show_packet_config_cmd (struct packet_config *config);
5a2468f5 190
d471ea57 191static void update_packet_config (struct packet_config *config);
5a2468f5 192
a14ed312 193void _initialize_remote (void);
c906108c 194
694f61fb 195/* Description of the remote protocol. Strictly speaking, when the
d01949b6
AC
196 target is open()ed, remote.c should create a per-target description
197 of the remote protocol using that target's architecture.
ce2826aa 198 Unfortunately, the target stack doesn't include local state. For
d01949b6
AC
199 the moment keep the information in the target's architecture
200 object. Sigh.. */
201
ad10f812
AC
202struct packet_reg
203{
204 long offset; /* Offset into G packet. */
205 long regnum; /* GDB's internal register number. */
206 LONGEST pnum; /* Remote protocol register number. */
b323314b 207 int in_g_packet; /* Always part of G packet. */
23860348
MS
208 /* long size in bytes; == register_size (current_gdbarch, regnum);
209 at present. */
ad10f812
AC
210 /* char *name; == REGISTER_NAME (regnum); at present. */
211};
212
d01949b6
AC
213struct remote_state
214{
ad10f812
AC
215 /* Description of the remote protocol registers. */
216 long sizeof_g_packet;
b323314b
AC
217
218 /* Description of the remote protocol registers indexed by REGNUM
219 (making an array of NUM_REGS + NUM_PSEUDO_REGS in size). */
220 struct packet_reg *regs;
ad10f812 221
d01949b6
AC
222 /* This is the size (in chars) of the first response to the ``g''
223 packet. It is used as a heuristic when determining the maximum
224 size of memory-read and memory-write packets. A target will
225 typically only reserve a buffer large enough to hold the ``g''
226 packet. The size does not include packet overhead (headers and
23860348 227 trailers). */
d01949b6
AC
228 long actual_register_packet_size;
229
230 /* This is the maximum size (in chars) of a non read/write packet.
23860348 231 It is also used as a cap on the size of read/write packets. */
d01949b6 232 long remote_packet_size;
6d820c5c
DJ
233
234 /* A buffer to use for incoming packets, and its current size. The
235 buffer is grown dynamically for larger incoming packets.
236 Outgoing packets may also be constructed in this buffer.
237 BUF_SIZE is always at least REMOTE_PACKET_SIZE;
238 REMOTE_PACKET_SIZE should be used to limit the length of outgoing
239 packets. */
240 char *buf;
241 long buf_size;
d01949b6
AC
242};
243
3c3bea1c 244
d01949b6
AC
245/* Handle for retreving the remote protocol data from gdbarch. */
246static struct gdbarch_data *remote_gdbarch_data_handle;
247
248static struct remote_state *
5ae5f592 249get_remote_state (void)
d01949b6 250{
451fbdda 251 return gdbarch_data (current_gdbarch, remote_gdbarch_data_handle);
d01949b6
AC
252}
253
254static void *
255init_remote_state (struct gdbarch *gdbarch)
256{
257 int regnum;
e5e78edc 258 struct remote_state *rs = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct remote_state);
d01949b6 259
46845f5e 260 rs->sizeof_g_packet = 0;
ad10f812 261
b323314b 262 /* Assume a 1:1 regnum<->pnum table. */
e5e78edc
AC
263 rs->regs = GDBARCH_OBSTACK_CALLOC (gdbarch, NUM_REGS + NUM_PSEUDO_REGS,
264 struct packet_reg);
b323314b 265 for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
ad10f812 266 {
b323314b
AC
267 struct packet_reg *r = &rs->regs[regnum];
268 r->pnum = regnum;
269 r->regnum = regnum;
62700349 270 r->offset = DEPRECATED_REGISTER_BYTE (regnum);
b323314b 271 r->in_g_packet = (regnum < NUM_REGS);
ad10f812 272 /* ...name = REGISTER_NAME (regnum); */
7d58c67d 273
23860348 274 /* Compute packet size by accumulating the size of all registers. */
46845f5e
KB
275 if (regnum < NUM_REGS)
276 rs->sizeof_g_packet += register_size (current_gdbarch, regnum);
ad10f812
AC
277 }
278
d01949b6
AC
279 /* Default maximum number of characters in a packet body. Many
280 remote stubs have a hardwired buffer size of 400 bytes
281 (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
282 as the maximum packet-size to ensure that the packet and an extra
283 NUL character can always fit in the buffer. This stops GDB
284 trashing stubs that try to squeeze an extra NUL into what is
23860348 285 already a full buffer (As of 1999-12-04 that was most stubs. */
d01949b6
AC
286 rs->remote_packet_size = 400 - 1;
287
ad10f812
AC
288 /* Should rs->sizeof_g_packet needs more space than the
289 default, adjust the size accordingly. Remember that each byte is
290 encoded as two characters. 32 is the overhead for the packet
291 header / footer. NOTE: cagney/1999-10-26: I suspect that 8
d01949b6 292 (``$NN:G...#NN'') is a better guess, the below has been padded a
23860348 293 little. */
ad10f812
AC
294 if (rs->sizeof_g_packet > ((rs->remote_packet_size - 32) / 2))
295 rs->remote_packet_size = (rs->sizeof_g_packet * 2 + 32);
802188a7 296
23860348 297 /* This one is filled in when a ``g'' packet is received. */
d01949b6
AC
298 rs->actual_register_packet_size = 0;
299
6d820c5c
DJ
300 /* Create the buffer at a default size. Note that this would
301 leak memory if the gdbarch were ever destroyed; there's no
302 way to register a destructor for it, and we can't realloc
303 using the gdbarch obstack. But gdbarches are never
304 destroyed. */
305 rs->buf_size = rs->remote_packet_size;
306 rs->buf = xmalloc (rs->buf_size);
307
d01949b6
AC
308 return rs;
309}
310
ad10f812
AC
311static struct packet_reg *
312packet_reg_from_regnum (struct remote_state *rs, long regnum)
313{
b323314b
AC
314 if (regnum < 0 && regnum >= NUM_REGS + NUM_PSEUDO_REGS)
315 return NULL;
316 else
ad10f812 317 {
b323314b
AC
318 struct packet_reg *r = &rs->regs[regnum];
319 gdb_assert (r->regnum == regnum);
320 return r;
ad10f812 321 }
ad10f812
AC
322}
323
324static struct packet_reg *
325packet_reg_from_pnum (struct remote_state *rs, LONGEST pnum)
326{
b323314b
AC
327 int i;
328 for (i = 0; i < NUM_REGS + NUM_PSEUDO_REGS; i++)
ad10f812 329 {
b323314b
AC
330 struct packet_reg *r = &rs->regs[i];
331 if (r->pnum == pnum)
332 return r;
ad10f812
AC
333 }
334 return NULL;
d01949b6
AC
335}
336
3c3bea1c
GS
337/* FIXME: graces/2002-08-08: These variables should eventually be
338 bound to an instance of the target object (as in gdbarch-tdep()),
339 when such a thing exists. */
340
341/* This is set to the data address of the access causing the target
342 to stop for a watchpoint. */
343static CORE_ADDR remote_watch_data_address;
344
94e08568 345/* This is non-zero if target stopped for a watchpoint. */
3c3bea1c
GS
346static int remote_stopped_by_watchpoint_p;
347
c906108c
SS
348static struct target_ops remote_ops;
349
350static struct target_ops extended_remote_ops;
351
43ff13b4 352/* Temporary target ops. Just like the remote_ops and
23860348 353 extended_remote_ops, but with asynchronous support. */
43ff13b4
JM
354static struct target_ops remote_async_ops;
355
356static struct target_ops extended_async_remote_ops;
357
6426a772
JM
358/* FIXME: cagney/1999-09-23: Even though getpkt was called with
359 ``forever'' still use the normal timeout mechanism. This is
360 currently used by the ASYNC code to guarentee that target reads
361 during the initial connect always time-out. Once getpkt has been
362 modified to return a timeout indication and, in turn
363 remote_wait()/wait_for_inferior() have gained a timeout parameter
23860348 364 this can go away. */
6426a772
JM
365static int wait_forever_enabled_p = 1;
366
367
c906108c
SS
368/* This variable chooses whether to send a ^C or a break when the user
369 requests program interruption. Although ^C is usually what remote
370 systems expect, and that is the default here, sometimes a break is
371 preferable instead. */
372
373static int remote_break;
374
c906108c
SS
375/* Descriptor for I/O to remote machine. Initialize it to NULL so that
376 remote_open knows that we don't have a file open when the program
377 starts. */
819cc324 378static struct serial *remote_desc = NULL;
c906108c 379
c906108c
SS
380/* This variable sets the number of bits in an address that are to be
381 sent in a memory ("M" or "m") packet. Normally, after stripping
382 leading zeros, the entire address would be sent. This variable
383 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
384 initial implementation of remote.c restricted the address sent in
385 memory packets to ``host::sizeof long'' bytes - (typically 32
386 bits). Consequently, for 64 bit targets, the upper 32 bits of an
387 address was never sent. Since fixing this bug may cause a break in
388 some remote targets this variable is principly provided to
23860348 389 facilitate backward compatibility. */
c906108c
SS
390
391static int remote_address_size;
392
6426a772
JM
393/* Tempoary to track who currently owns the terminal. See
394 target_async_terminal_* for more details. */
395
396static int remote_async_terminal_ours_p;
397
11cf8741 398\f
11cf8741 399/* User configurable variables for the number of characters in a
24b06219 400 memory read/write packet. MIN (rs->remote_packet_size,
ad10f812 401 rs->sizeof_g_packet) is the default. Some targets need smaller
24b06219 402 values (fifo overruns, et.al.) and some users need larger values
ad10f812
AC
403 (speed up transfers). The variables ``preferred_*'' (the user
404 request), ``current_*'' (what was actually set) and ``forced_*''
23860348 405 (Positive - a soft limit, negative - a hard limit). */
11cf8741
JM
406
407struct memory_packet_config
408{
409 char *name;
410 long size;
411 int fixed_p;
412};
413
414/* Compute the current size of a read/write packet. Since this makes
415 use of ``actual_register_packet_size'' the computation is dynamic. */
416
417static long
418get_memory_packet_size (struct memory_packet_config *config)
419{
d01949b6 420 struct remote_state *rs = get_remote_state ();
11cf8741
JM
421 /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk
422 law?) that some hosts don't cope very well with large alloca()
423 calls. Eventually the alloca() code will be replaced by calls to
424 xmalloc() and make_cleanups() allowing this restriction to either
23860348 425 be lifted or removed. */
11cf8741
JM
426#ifndef MAX_REMOTE_PACKET_SIZE
427#define MAX_REMOTE_PACKET_SIZE 16384
428#endif
3de11b2e 429 /* NOTE: 20 ensures we can write at least one byte. */
11cf8741 430#ifndef MIN_REMOTE_PACKET_SIZE
3de11b2e 431#define MIN_REMOTE_PACKET_SIZE 20
11cf8741
JM
432#endif
433 long what_they_get;
434 if (config->fixed_p)
435 {
436 if (config->size <= 0)
437 what_they_get = MAX_REMOTE_PACKET_SIZE;
438 else
439 what_they_get = config->size;
440 }
441 else
442 {
24b06219 443 what_they_get = rs->remote_packet_size;
23860348 444 /* Limit the packet to the size specified by the user. */
11cf8741
JM
445 if (config->size > 0
446 && what_they_get > config->size)
447 what_they_get = config->size;
23860348 448 /* Limit it to the size of the targets ``g'' response. */
d01949b6
AC
449 if ((rs->actual_register_packet_size) > 0
450 && what_they_get > (rs->actual_register_packet_size))
451 what_they_get = (rs->actual_register_packet_size);
11cf8741
JM
452 }
453 if (what_they_get > MAX_REMOTE_PACKET_SIZE)
454 what_they_get = MAX_REMOTE_PACKET_SIZE;
455 if (what_they_get < MIN_REMOTE_PACKET_SIZE)
456 what_they_get = MIN_REMOTE_PACKET_SIZE;
6d820c5c
DJ
457
458 /* Make sure there is room in the global buffer for this packet
459 (including its trailing NUL byte). */
460 if (rs->buf_size < what_they_get + 1)
461 {
462 rs->buf_size = 2 * what_they_get;
463 rs->buf = xrealloc (rs->buf, 2 * what_they_get);
464 }
465
11cf8741
JM
466 return what_they_get;
467}
468
469/* Update the size of a read/write packet. If they user wants
23860348 470 something really big then do a sanity check. */
11cf8741
JM
471
472static void
473set_memory_packet_size (char *args, struct memory_packet_config *config)
474{
475 int fixed_p = config->fixed_p;
476 long size = config->size;
477 if (args == NULL)
8a3fe4f8 478 error (_("Argument required (integer, `fixed' or `limited')."));
11cf8741
JM
479 else if (strcmp (args, "hard") == 0
480 || strcmp (args, "fixed") == 0)
481 fixed_p = 1;
482 else if (strcmp (args, "soft") == 0
483 || strcmp (args, "limit") == 0)
484 fixed_p = 0;
485 else
486 {
487 char *end;
488 size = strtoul (args, &end, 0);
489 if (args == end)
8a3fe4f8 490 error (_("Invalid %s (bad syntax)."), config->name);
11cf8741
JM
491#if 0
492 /* Instead of explicitly capping the size of a packet to
493 MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is
494 instead allowed to set the size to something arbitrarily
23860348 495 large. */
11cf8741 496 if (size > MAX_REMOTE_PACKET_SIZE)
8a3fe4f8 497 error (_("Invalid %s (too large)."), config->name);
11cf8741
JM
498#endif
499 }
23860348 500 /* Extra checks? */
11cf8741
JM
501 if (fixed_p && !config->fixed_p)
502 {
e2e0b3e5
AC
503 if (! query (_("The target may not be able to correctly handle a %s\n"
504 "of %ld bytes. Change the packet size? "),
11cf8741 505 config->name, size))
8a3fe4f8 506 error (_("Packet size not changed."));
11cf8741 507 }
23860348 508 /* Update the config. */
11cf8741
JM
509 config->fixed_p = fixed_p;
510 config->size = size;
511}
512
513static void
514show_memory_packet_size (struct memory_packet_config *config)
515{
a3f17187 516 printf_filtered (_("The %s is %ld. "), config->name, config->size);
11cf8741 517 if (config->fixed_p)
a3f17187 518 printf_filtered (_("Packets are fixed at %ld bytes.\n"),
11cf8741
JM
519 get_memory_packet_size (config));
520 else
a3f17187 521 printf_filtered (_("Packets are limited to %ld bytes.\n"),
11cf8741
JM
522 get_memory_packet_size (config));
523}
524
525static struct memory_packet_config memory_write_packet_config =
526{
527 "memory-write-packet-size",
528};
529
530static void
531set_memory_write_packet_size (char *args, int from_tty)
532{
533 set_memory_packet_size (args, &memory_write_packet_config);
534}
535
536static void
537show_memory_write_packet_size (char *args, int from_tty)
538{
539 show_memory_packet_size (&memory_write_packet_config);
540}
541
542static long
543get_memory_write_packet_size (void)
544{
545 return get_memory_packet_size (&memory_write_packet_config);
546}
547
548static struct memory_packet_config memory_read_packet_config =
549{
550 "memory-read-packet-size",
551};
552
553static void
554set_memory_read_packet_size (char *args, int from_tty)
555{
556 set_memory_packet_size (args, &memory_read_packet_config);
557}
558
559static void
560show_memory_read_packet_size (char *args, int from_tty)
561{
562 show_memory_packet_size (&memory_read_packet_config);
563}
564
565static long
566get_memory_read_packet_size (void)
567{
d01949b6 568 struct remote_state *rs = get_remote_state ();
11cf8741
JM
569 long size = get_memory_packet_size (&memory_read_packet_config);
570 /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
571 extra buffer size argument before the memory read size can be
24b06219
MK
572 increased beyond RS->remote_packet_size. */
573 if (size > rs->remote_packet_size)
574 size = rs->remote_packet_size;
11cf8741
JM
575 return size;
576}
577
11cf8741 578\f
5a2468f5
JM
579/* Generic configuration support for packets the stub optionally
580 supports. Allows the user to specify the use of the packet as well
23860348 581 as allowing GDB to auto-detect support in the remote stub. */
5a2468f5
JM
582
583enum packet_support
584 {
585 PACKET_SUPPORT_UNKNOWN = 0,
586 PACKET_ENABLE,
587 PACKET_DISABLE
588 };
589
5a2468f5
JM
590struct packet_config
591 {
5a2468f5
JM
592 char *name;
593 char *title;
7f19b9a2 594 enum auto_boolean detect;
5a2468f5
JM
595 enum packet_support support;
596 };
597
d471ea57 598/* Analyze a packet's return value and update the packet config
23860348 599 accordingly. */
d471ea57
AC
600
601enum packet_result
602{
603 PACKET_ERROR,
604 PACKET_OK,
605 PACKET_UNKNOWN
606};
607
5a2468f5 608static void
d471ea57 609update_packet_config (struct packet_config *config)
5a2468f5 610{
d471ea57
AC
611 switch (config->detect)
612 {
7f19b9a2 613 case AUTO_BOOLEAN_TRUE:
d471ea57
AC
614 config->support = PACKET_ENABLE;
615 break;
7f19b9a2 616 case AUTO_BOOLEAN_FALSE:
d471ea57
AC
617 config->support = PACKET_DISABLE;
618 break;
7f19b9a2 619 case AUTO_BOOLEAN_AUTO:
d471ea57
AC
620 config->support = PACKET_SUPPORT_UNKNOWN;
621 break;
622 }
5a2468f5
JM
623}
624
625static void
fba45db2 626show_packet_config_cmd (struct packet_config *config)
5a2468f5
JM
627{
628 char *support = "internal-error";
629 switch (config->support)
630 {
631 case PACKET_ENABLE:
632 support = "enabled";
633 break;
634 case PACKET_DISABLE:
635 support = "disabled";
636 break;
637 case PACKET_SUPPORT_UNKNOWN:
638 support = "unknown";
639 break;
640 }
641 switch (config->detect)
642 {
7f19b9a2 643 case AUTO_BOOLEAN_AUTO:
37a105a1
DJ
644 printf_filtered (_("Support for the `%s' packet is auto-detected, currently %s.\n"),
645 config->name, support);
5a2468f5 646 break;
7f19b9a2
AC
647 case AUTO_BOOLEAN_TRUE:
648 case AUTO_BOOLEAN_FALSE:
37a105a1
DJ
649 printf_filtered (_("Support for the `%s' packet is currently %s.\n"),
650 config->name, support);
8e248173 651 break;
5a2468f5
JM
652 }
653}
654
655static void
d471ea57
AC
656add_packet_config_cmd (struct packet_config *config,
657 char *name,
658 char *title,
e9e68a56 659 cmd_sfunc_ftype *set_func,
08546159 660 show_value_ftype *show_func,
d471ea57
AC
661 struct cmd_list_element **set_remote_list,
662 struct cmd_list_element **show_remote_list,
663 int legacy)
664{
5a2468f5
JM
665 char *set_doc;
666 char *show_doc;
d471ea57 667 char *cmd_name;
3ed07be4 668
5a2468f5
JM
669 config->name = name;
670 config->title = title;
7f19b9a2 671 config->detect = AUTO_BOOLEAN_AUTO;
8e248173 672 config->support = PACKET_SUPPORT_UNKNOWN;
b435e160
AC
673 set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet",
674 name, title);
675 show_doc = xstrprintf ("Show current use of remote protocol `%s' (%s) packet",
676 name, title);
d471ea57 677 /* set/show TITLE-packet {auto,on,off} */
b435e160 678 cmd_name = xstrprintf ("%s-packet", title);
e9e68a56 679 add_setshow_auto_boolean_cmd (cmd_name, class_obscure,
2c5b56ce 680 &config->detect, set_doc, show_doc, NULL, /* help_doc */
e9e68a56
AC
681 set_func, show_func,
682 set_remote_list, show_remote_list);
23860348 683 /* set/show remote NAME-packet {auto,on,off} -- legacy. */
d471ea57
AC
684 if (legacy)
685 {
686 char *legacy_name;
b435e160 687 legacy_name = xstrprintf ("%s-packet", name);
d471ea57
AC
688 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
689 set_remote_list);
690 add_alias_cmd (legacy_name, cmd_name, class_obscure, 0,
691 show_remote_list);
692 }
5a2468f5
JM
693}
694
d471ea57
AC
695static enum packet_result
696packet_ok (const char *buf, struct packet_config *config)
5a2468f5 697{
d471ea57 698 if (buf[0] != '\0')
5a2468f5 699 {
d471ea57 700 /* The stub recognized the packet request. Check that the
23860348 701 operation succeeded. */
d471ea57
AC
702 switch (config->support)
703 {
704 case PACKET_SUPPORT_UNKNOWN:
705 if (remote_debug)
706 fprintf_unfiltered (gdb_stdlog,
707 "Packet %s (%s) is supported\n",
708 config->name, config->title);
709 config->support = PACKET_ENABLE;
710 break;
711 case PACKET_DISABLE:
8e65ff28 712 internal_error (__FILE__, __LINE__,
e2e0b3e5 713 _("packet_ok: attempt to use a disabled packet"));
d471ea57
AC
714 break;
715 case PACKET_ENABLE:
716 break;
717 }
718 if (buf[0] == 'O' && buf[1] == 'K' && buf[2] == '\0')
23860348 719 /* "OK" - definitly OK. */
d471ea57
AC
720 return PACKET_OK;
721 if (buf[0] == 'E'
722 && isxdigit (buf[1]) && isxdigit (buf[2])
723 && buf[3] == '\0')
23860348 724 /* "Enn" - definitly an error. */
d471ea57 725 return PACKET_ERROR;
23860348 726 /* The packet may or may not be OK. Just assume it is. */
d471ea57
AC
727 return PACKET_OK;
728 }
729 else
730 {
23860348 731 /* The stub does not support the packet. */
d471ea57
AC
732 switch (config->support)
733 {
734 case PACKET_ENABLE:
7f19b9a2 735 if (config->detect == AUTO_BOOLEAN_AUTO)
d471ea57 736 /* If the stub previously indicated that the packet was
23860348 737 supported then there is a protocol error.. */
8a3fe4f8 738 error (_("Protocol error: %s (%s) conflicting enabled responses."),
d471ea57
AC
739 config->name, config->title);
740 else
23860348 741 /* The user set it wrong. */
8a3fe4f8 742 error (_("Enabled packet %s (%s) not recognized by stub"),
d471ea57
AC
743 config->name, config->title);
744 break;
745 case PACKET_SUPPORT_UNKNOWN:
746 if (remote_debug)
747 fprintf_unfiltered (gdb_stdlog,
748 "Packet %s (%s) is NOT supported\n",
749 config->name, config->title);
750 config->support = PACKET_DISABLE;
751 break;
752 case PACKET_DISABLE:
753 break;
754 }
755 return PACKET_UNKNOWN;
5a2468f5
JM
756 }
757}
758
444abaca
DJ
759enum {
760 PACKET_vCont = 0,
761 PACKET_X,
762 PACKET_qSymbol,
763 PACKET_P,
764 PACKET_p,
765 PACKET_Z0,
766 PACKET_Z1,
767 PACKET_Z2,
768 PACKET_Z3,
769 PACKET_Z4,
770 PACKET_qPart_auxv,
771 PACKET_qGetTLSAddr,
772 PACKET_MAX
773};
506fb367 774
444abaca 775static struct packet_config remote_protocol_packets[PACKET_MAX];
dc8acb97
MS
776
777static void
444abaca
DJ
778set_remote_protocol_packet_cmd (char *args, int from_tty,
779 struct cmd_list_element *c)
dc8acb97 780{
444abaca 781 struct packet_config *packet;
dc8acb97 782
444abaca
DJ
783 for (packet = remote_protocol_packets;
784 packet < &remote_protocol_packets[PACKET_MAX];
785 packet++)
786 {
787 if (&packet->detect == c->var)
788 {
789 update_packet_config (packet);
790 return;
791 }
792 }
793 internal_error (__FILE__, __LINE__, "Could not find config for %s",
794 c->name);
dc8acb97
MS
795}
796
5a2468f5 797static void
444abaca
DJ
798show_remote_protocol_packet_cmd (struct ui_file *file, int from_tty,
799 struct cmd_list_element *c,
800 const char *value)
5a2468f5 801{
444abaca 802 struct packet_config *packet;
5a2468f5 803
444abaca
DJ
804 for (packet = remote_protocol_packets;
805 packet < &remote_protocol_packets[PACKET_MAX];
806 packet++)
807 {
808 if (&packet->detect == c->var)
809 {
810 show_packet_config_cmd (packet);
811 return;
812 }
813 }
814 internal_error (__FILE__, __LINE__, "Could not find config for %s",
815 c->name);
5a2468f5
JM
816}
817
d471ea57
AC
818/* Should we try one of the 'Z' requests? */
819
820enum Z_packet_type
821{
822 Z_PACKET_SOFTWARE_BP,
823 Z_PACKET_HARDWARE_BP,
824 Z_PACKET_WRITE_WP,
825 Z_PACKET_READ_WP,
826 Z_PACKET_ACCESS_WP,
827 NR_Z_PACKET_TYPES
828};
96baa820 829
d471ea57 830/* For compatibility with older distributions. Provide a ``set remote
23860348 831 Z-packet ...'' command that updates all the Z packet types. */
d471ea57 832
7f19b9a2 833static enum auto_boolean remote_Z_packet_detect;
96baa820
JM
834
835static void
fba45db2
KB
836set_remote_protocol_Z_packet_cmd (char *args, int from_tty,
837 struct cmd_list_element *c)
96baa820 838{
d471ea57
AC
839 int i;
840 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
841 {
444abaca
DJ
842 remote_protocol_packets[PACKET_Z0 + i].detect = remote_Z_packet_detect;
843 update_packet_config (&remote_protocol_packets[PACKET_Z0 + i]);
d471ea57 844 }
96baa820
JM
845}
846
847static void
08546159
AC
848show_remote_protocol_Z_packet_cmd (struct ui_file *file, int from_tty,
849 struct cmd_list_element *c,
850 const char *value)
96baa820 851{
d471ea57
AC
852 int i;
853 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
854 {
444abaca 855 show_packet_config_cmd (&remote_protocol_packets[PACKET_Z0 + i]);
d471ea57 856 }
96baa820
JM
857}
858
9d1f7ab2
MS
859/* Should we try the 'ThreadInfo' query packet?
860
861 This variable (NOT available to the user: auto-detect only!)
862 determines whether GDB will use the new, simpler "ThreadInfo"
863 query or the older, more complex syntax for thread queries.
802188a7 864 This is an auto-detect variable (set to true at each connect,
9d1f7ab2
MS
865 and set to false when the target fails to recognize it). */
866
867static int use_threadinfo_query;
868static int use_threadextra_query;
869
23860348 870/* Tokens for use by the asynchronous signal handlers for SIGINT. */
ae44c0c4
AC
871static void *sigint_remote_twice_token;
872static void *sigint_remote_token;
43ff13b4 873
c906108c
SS
874/* These are pointers to hook functions that may be set in order to
875 modify resume/wait behavior for a particular architecture. */
876
9a4105ab
AC
877void (*deprecated_target_resume_hook) (void);
878void (*deprecated_target_wait_loop_hook) (void);
c906108c
SS
879\f
880
c5aa993b 881
c906108c
SS
882/* These are the threads which we last sent to the remote system.
883 -1 for all or -2 for not sent yet. */
884static int general_thread;
cce74817 885static int continue_thread;
c906108c
SS
886
887/* Call this function as a result of
888 1) A halt indication (T packet) containing a thread id
889 2) A direct query of currthread
890 3) Successful execution of set thread
891 */
892
893static void
fba45db2 894record_currthread (int currthread)
c906108c 895{
c906108c 896 general_thread = currthread;
cce74817 897
c906108c
SS
898 /* If this is a new thread, add it to GDB's thread list.
899 If we leave it up to WFI to do this, bad things will happen. */
39f77062 900 if (!in_thread_list (pid_to_ptid (currthread)))
0f71a2f6 901 {
39f77062 902 add_thread (pid_to_ptid (currthread));
8b93c638 903 ui_out_text (uiout, "[New ");
39f77062 904 ui_out_text (uiout, target_pid_to_str (pid_to_ptid (currthread)));
8b93c638 905 ui_out_text (uiout, "]\n");
0f71a2f6 906 }
c906108c
SS
907}
908
909#define MAGIC_NULL_PID 42000
910
911static void
fba45db2 912set_thread (int th, int gen)
c906108c 913{
d01949b6 914 struct remote_state *rs = get_remote_state ();
6d820c5c 915 char *buf = rs->buf;
cce74817 916 int state = gen ? general_thread : continue_thread;
c906108c
SS
917
918 if (state == th)
919 return;
920
921 buf[0] = 'H';
922 buf[1] = gen ? 'g' : 'c';
923 if (th == MAGIC_NULL_PID)
924 {
925 buf[2] = '0';
926 buf[3] = '\0';
927 }
928 else if (th < 0)
ecbc58df 929 xsnprintf (&buf[2], rs->remote_packet_size - 2, "-%x", -th);
c906108c 930 else
ecbc58df 931 xsnprintf (&buf[2], rs->remote_packet_size - 2, "%x", th);
c906108c 932 putpkt (buf);
6d820c5c 933 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 934 if (gen)
c5aa993b 935 general_thread = th;
c906108c 936 else
cce74817 937 continue_thread = th;
c906108c
SS
938}
939\f
940/* Return nonzero if the thread TH is still alive on the remote system. */
941
942static int
39f77062 943remote_thread_alive (ptid_t ptid)
c906108c 944{
6d820c5c 945 struct remote_state *rs = get_remote_state ();
39f77062 946 int tid = PIDGET (ptid);
6d820c5c 947 char *buf = rs->buf;
c906108c 948
cce74817 949 if (tid < 0)
6d820c5c 950 xsnprintf (buf, rs->remote_packet_size, "T-%08x", -tid);
c906108c 951 else
6d820c5c 952 xsnprintf (buf, rs->remote_packet_size, "T%08x", tid);
c906108c 953 putpkt (buf);
6d820c5c 954 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c
SS
955 return (buf[0] == 'O' && buf[1] == 'K');
956}
957
958/* About these extended threadlist and threadinfo packets. They are
959 variable length packets but, the fields within them are often fixed
960 length. They are redundent enough to send over UDP as is the
961 remote protocol in general. There is a matching unit test module
962 in libstub. */
963
cce74817
JM
964#define OPAQUETHREADBYTES 8
965
966/* a 64 bit opaque identifier */
967typedef unsigned char threadref[OPAQUETHREADBYTES];
968
23860348
MS
969/* WARNING: This threadref data structure comes from the remote O.S.,
970 libstub protocol encoding, and remote.c. it is not particularly
971 changable. */
cce74817
JM
972
973/* Right now, the internal structure is int. We want it to be bigger.
974 Plan to fix this.
c5aa993b 975 */
cce74817 976
23860348 977typedef int gdb_threadref; /* Internal GDB thread reference. */
cce74817 978
9d1f7ab2 979/* gdb_ext_thread_info is an internal GDB data structure which is
23860348 980 equivalint to the reply of the remote threadinfo packet. */
cce74817
JM
981
982struct gdb_ext_thread_info
c5aa993b 983 {
23860348
MS
984 threadref threadid; /* External form of thread reference. */
985 int active; /* Has state interesting to GDB?
986 regs, stack. */
987 char display[256]; /* Brief state display, name,
cedea757 988 blocked/suspended. */
23860348
MS
989 char shortname[32]; /* To be used to name threads. */
990 char more_display[256]; /* Long info, statistics, queue depth,
991 whatever. */
c5aa993b 992 };
cce74817
JM
993
994/* The volume of remote transfers can be limited by submitting
995 a mask containing bits specifying the desired information.
996 Use a union of these values as the 'selection' parameter to
997 get_thread_info. FIXME: Make these TAG names more thread specific.
c5aa993b 998 */
cce74817
JM
999
1000#define TAG_THREADID 1
1001#define TAG_EXISTS 2
1002#define TAG_DISPLAY 4
1003#define TAG_THREADNAME 8
c5aa993b 1004#define TAG_MOREDISPLAY 16
cce74817 1005
23860348 1006#define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
c906108c 1007
b2dd6311 1008char *unpack_varlen_hex (char *buff, ULONGEST *result);
cce74817 1009
a14ed312 1010static char *unpack_nibble (char *buf, int *val);
cce74817 1011
a14ed312 1012static char *pack_nibble (char *buf, int nibble);
cce74817 1013
23860348 1014static char *pack_hex_byte (char *pkt, int /* unsigned char */ byte);
cce74817 1015
a14ed312 1016static char *unpack_byte (char *buf, int *value);
cce74817 1017
a14ed312 1018static char *pack_int (char *buf, int value);
cce74817 1019
a14ed312 1020static char *unpack_int (char *buf, int *value);
cce74817 1021
a14ed312 1022static char *unpack_string (char *src, char *dest, int length);
cce74817 1023
23860348 1024static char *pack_threadid (char *pkt, threadref *id);
cce74817 1025
23860348 1026static char *unpack_threadid (char *inbuf, threadref *id);
cce74817 1027
23860348 1028void int_to_threadref (threadref *id, int value);
cce74817 1029
23860348 1030static int threadref_to_int (threadref *ref);
cce74817 1031
23860348 1032static void copy_threadref (threadref *dest, threadref *src);
cce74817 1033
23860348 1034static int threadmatch (threadref *dest, threadref *src);
cce74817 1035
23860348
MS
1036static char *pack_threadinfo_request (char *pkt, int mode,
1037 threadref *id);
cce74817 1038
a14ed312 1039static int remote_unpack_thread_info_response (char *pkt,
23860348 1040 threadref *expectedref,
a14ed312
KB
1041 struct gdb_ext_thread_info
1042 *info);
cce74817
JM
1043
1044
23860348
MS
1045static int remote_get_threadinfo (threadref *threadid,
1046 int fieldset, /*TAG mask */
a14ed312 1047 struct gdb_ext_thread_info *info);
cce74817 1048
a14ed312
KB
1049static char *pack_threadlist_request (char *pkt, int startflag,
1050 int threadcount,
23860348 1051 threadref *nextthread);
cce74817 1052
a14ed312
KB
1053static int parse_threadlist_response (char *pkt,
1054 int result_limit,
23860348
MS
1055 threadref *original_echo,
1056 threadref *resultlist,
1057 int *doneflag);
cce74817 1058
a14ed312 1059static int remote_get_threadlist (int startflag,
23860348 1060 threadref *nextthread,
a14ed312
KB
1061 int result_limit,
1062 int *done,
23860348
MS
1063 int *result_count,
1064 threadref *threadlist);
cce74817 1065
23860348 1066typedef int (*rmt_thread_action) (threadref *ref, void *context);
cce74817 1067
a14ed312
KB
1068static int remote_threadlist_iterator (rmt_thread_action stepfunction,
1069 void *context, int looplimit);
cce74817 1070
23860348 1071static int remote_newthread_step (threadref *ref, void *context);
cce74817 1072
23860348 1073/* Encode 64 bits in 16 chars of hex. */
c906108c
SS
1074
1075static const char hexchars[] = "0123456789abcdef";
1076
1077static int
fba45db2 1078ishex (int ch, int *val)
c906108c
SS
1079{
1080 if ((ch >= 'a') && (ch <= 'f'))
1081 {
1082 *val = ch - 'a' + 10;
1083 return 1;
1084 }
1085 if ((ch >= 'A') && (ch <= 'F'))
1086 {
1087 *val = ch - 'A' + 10;
1088 return 1;
1089 }
1090 if ((ch >= '0') && (ch <= '9'))
1091 {
1092 *val = ch - '0';
1093 return 1;
1094 }
1095 return 0;
1096}
1097
1098static int
fba45db2 1099stubhex (int ch)
c906108c
SS
1100{
1101 if (ch >= 'a' && ch <= 'f')
1102 return ch - 'a' + 10;
1103 if (ch >= '0' && ch <= '9')
1104 return ch - '0';
1105 if (ch >= 'A' && ch <= 'F')
1106 return ch - 'A' + 10;
1107 return -1;
1108}
1109
1110static int
fba45db2 1111stub_unpack_int (char *buff, int fieldlength)
c906108c
SS
1112{
1113 int nibble;
1114 int retval = 0;
1115
1116 while (fieldlength)
1117 {
1118 nibble = stubhex (*buff++);
1119 retval |= nibble;
1120 fieldlength--;
1121 if (fieldlength)
1122 retval = retval << 4;
1123 }
1124 return retval;
1125}
1126
1127char *
fba45db2 1128unpack_varlen_hex (char *buff, /* packet to parse */
b2dd6311 1129 ULONGEST *result)
c906108c
SS
1130{
1131 int nibble;
1132 int retval = 0;
1133
1134 while (ishex (*buff, &nibble))
1135 {
1136 buff++;
1137 retval = retval << 4;
1138 retval |= nibble & 0x0f;
1139 }
1140 *result = retval;
1141 return buff;
1142}
1143
1144static char *
fba45db2 1145unpack_nibble (char *buf, int *val)
c906108c
SS
1146{
1147 ishex (*buf++, val);
1148 return buf;
1149}
1150
1151static char *
fba45db2 1152pack_nibble (char *buf, int nibble)
c906108c
SS
1153{
1154 *buf++ = hexchars[(nibble & 0x0f)];
1155 return buf;
1156}
1157
1158static char *
fba45db2 1159pack_hex_byte (char *pkt, int byte)
c906108c
SS
1160{
1161 *pkt++ = hexchars[(byte >> 4) & 0xf];
1162 *pkt++ = hexchars[(byte & 0xf)];
1163 return pkt;
1164}
1165
1166static char *
fba45db2 1167unpack_byte (char *buf, int *value)
c906108c
SS
1168{
1169 *value = stub_unpack_int (buf, 2);
1170 return buf + 2;
1171}
1172
1173static char *
fba45db2 1174pack_int (char *buf, int value)
c906108c
SS
1175{
1176 buf = pack_hex_byte (buf, (value >> 24) & 0xff);
1177 buf = pack_hex_byte (buf, (value >> 16) & 0xff);
1178 buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
1179 buf = pack_hex_byte (buf, (value & 0xff));
1180 return buf;
1181}
1182
1183static char *
fba45db2 1184unpack_int (char *buf, int *value)
c906108c
SS
1185{
1186 *value = stub_unpack_int (buf, 8);
1187 return buf + 8;
1188}
1189
23860348 1190#if 0 /* Currently unused, uncomment when needed. */
a14ed312 1191static char *pack_string (char *pkt, char *string);
c906108c
SS
1192
1193static char *
fba45db2 1194pack_string (char *pkt, char *string)
c906108c
SS
1195{
1196 char ch;
1197 int len;
1198
1199 len = strlen (string);
1200 if (len > 200)
23860348 1201 len = 200; /* Bigger than most GDB packets, junk??? */
c906108c
SS
1202 pkt = pack_hex_byte (pkt, len);
1203 while (len-- > 0)
1204 {
1205 ch = *string++;
1206 if ((ch == '\0') || (ch == '#'))
23860348 1207 ch = '*'; /* Protect encapsulation. */
c906108c
SS
1208 *pkt++ = ch;
1209 }
1210 return pkt;
1211}
1212#endif /* 0 (unused) */
1213
1214static char *
fba45db2 1215unpack_string (char *src, char *dest, int length)
c906108c
SS
1216{
1217 while (length--)
1218 *dest++ = *src++;
1219 *dest = '\0';
1220 return src;
1221}
1222
1223static char *
fba45db2 1224pack_threadid (char *pkt, threadref *id)
c906108c
SS
1225{
1226 char *limit;
1227 unsigned char *altid;
1228
1229 altid = (unsigned char *) id;
1230 limit = pkt + BUF_THREAD_ID_SIZE;
1231 while (pkt < limit)
1232 pkt = pack_hex_byte (pkt, *altid++);
1233 return pkt;
1234}
1235
1236
1237static char *
fba45db2 1238unpack_threadid (char *inbuf, threadref *id)
c906108c
SS
1239{
1240 char *altref;
1241 char *limit = inbuf + BUF_THREAD_ID_SIZE;
1242 int x, y;
1243
1244 altref = (char *) id;
1245
1246 while (inbuf < limit)
1247 {
1248 x = stubhex (*inbuf++);
1249 y = stubhex (*inbuf++);
1250 *altref++ = (x << 4) | y;
1251 }
1252 return inbuf;
1253}
1254
1255/* Externally, threadrefs are 64 bits but internally, they are still
1256 ints. This is due to a mismatch of specifications. We would like
1257 to use 64bit thread references internally. This is an adapter
1258 function. */
1259
1260void
fba45db2 1261int_to_threadref (threadref *id, int value)
c906108c
SS
1262{
1263 unsigned char *scan;
1264
1265 scan = (unsigned char *) id;
1266 {
1267 int i = 4;
1268 while (i--)
1269 *scan++ = 0;
1270 }
1271 *scan++ = (value >> 24) & 0xff;
1272 *scan++ = (value >> 16) & 0xff;
1273 *scan++ = (value >> 8) & 0xff;
1274 *scan++ = (value & 0xff);
1275}
1276
1277static int
fba45db2 1278threadref_to_int (threadref *ref)
c906108c
SS
1279{
1280 int i, value = 0;
1281 unsigned char *scan;
1282
cfd77fa1 1283 scan = *ref;
c906108c
SS
1284 scan += 4;
1285 i = 4;
1286 while (i-- > 0)
1287 value = (value << 8) | ((*scan++) & 0xff);
1288 return value;
1289}
1290
1291static void
fba45db2 1292copy_threadref (threadref *dest, threadref *src)
c906108c
SS
1293{
1294 int i;
1295 unsigned char *csrc, *cdest;
1296
1297 csrc = (unsigned char *) src;
1298 cdest = (unsigned char *) dest;
1299 i = 8;
1300 while (i--)
1301 *cdest++ = *csrc++;
1302}
1303
1304static int
fba45db2 1305threadmatch (threadref *dest, threadref *src)
c906108c 1306{
23860348 1307 /* Things are broken right now, so just assume we got a match. */
c906108c
SS
1308#if 0
1309 unsigned char *srcp, *destp;
1310 int i, result;
1311 srcp = (char *) src;
1312 destp = (char *) dest;
1313
1314 result = 1;
1315 while (i-- > 0)
1316 result &= (*srcp++ == *destp++) ? 1 : 0;
1317 return result;
1318#endif
1319 return 1;
1320}
1321
1322/*
c5aa993b
JM
1323 threadid:1, # always request threadid
1324 context_exists:2,
1325 display:4,
1326 unique_name:8,
1327 more_display:16
1328 */
c906108c
SS
1329
1330/* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
1331
1332static char *
fba45db2 1333pack_threadinfo_request (char *pkt, int mode, threadref *id)
c906108c 1334{
23860348
MS
1335 *pkt++ = 'q'; /* Info Query */
1336 *pkt++ = 'P'; /* process or thread info */
1337 pkt = pack_int (pkt, mode); /* mode */
c906108c 1338 pkt = pack_threadid (pkt, id); /* threadid */
23860348 1339 *pkt = '\0'; /* terminate */
c906108c
SS
1340 return pkt;
1341}
1342
23860348 1343/* These values tag the fields in a thread info response packet. */
c906108c 1344/* Tagging the fields allows us to request specific fields and to
23860348 1345 add more fields as time goes by. */
c906108c 1346
23860348 1347#define TAG_THREADID 1 /* Echo the thread identifier. */
c5aa993b 1348#define TAG_EXISTS 2 /* Is this process defined enough to
23860348 1349 fetch registers and its stack? */
c5aa993b 1350#define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
23860348 1351#define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
802188a7 1352#define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
23860348 1353 the process. */
c906108c
SS
1354
1355static int
fba45db2
KB
1356remote_unpack_thread_info_response (char *pkt, threadref *expectedref,
1357 struct gdb_ext_thread_info *info)
c906108c 1358{
d01949b6 1359 struct remote_state *rs = get_remote_state ();
c906108c 1360 int mask, length;
cfd77fa1 1361 int tag;
c906108c 1362 threadref ref;
6d820c5c 1363 char *limit = pkt + rs->buf_size; /* Plausible parsing limit. */
c906108c
SS
1364 int retval = 1;
1365
23860348 1366 /* info->threadid = 0; FIXME: implement zero_threadref. */
c906108c
SS
1367 info->active = 0;
1368 info->display[0] = '\0';
1369 info->shortname[0] = '\0';
1370 info->more_display[0] = '\0';
1371
23860348
MS
1372 /* Assume the characters indicating the packet type have been
1373 stripped. */
c906108c
SS
1374 pkt = unpack_int (pkt, &mask); /* arg mask */
1375 pkt = unpack_threadid (pkt, &ref);
1376
1377 if (mask == 0)
8a3fe4f8 1378 warning (_("Incomplete response to threadinfo request."));
c906108c 1379 if (!threadmatch (&ref, expectedref))
23860348 1380 { /* This is an answer to a different request. */
8a3fe4f8 1381 warning (_("ERROR RMT Thread info mismatch."));
c906108c
SS
1382 return 0;
1383 }
1384 copy_threadref (&info->threadid, &ref);
1385
23860348 1386 /* Loop on tagged fields , try to bail if somthing goes wrong. */
c906108c 1387
23860348
MS
1388 /* Packets are terminated with nulls. */
1389 while ((pkt < limit) && mask && *pkt)
c906108c
SS
1390 {
1391 pkt = unpack_int (pkt, &tag); /* tag */
23860348
MS
1392 pkt = unpack_byte (pkt, &length); /* length */
1393 if (!(tag & mask)) /* Tags out of synch with mask. */
c906108c 1394 {
8a3fe4f8 1395 warning (_("ERROR RMT: threadinfo tag mismatch."));
c906108c
SS
1396 retval = 0;
1397 break;
1398 }
1399 if (tag == TAG_THREADID)
1400 {
1401 if (length != 16)
1402 {
8a3fe4f8 1403 warning (_("ERROR RMT: length of threadid is not 16."));
c906108c
SS
1404 retval = 0;
1405 break;
1406 }
1407 pkt = unpack_threadid (pkt, &ref);
1408 mask = mask & ~TAG_THREADID;
1409 continue;
1410 }
1411 if (tag == TAG_EXISTS)
1412 {
1413 info->active = stub_unpack_int (pkt, length);
1414 pkt += length;
1415 mask = mask & ~(TAG_EXISTS);
1416 if (length > 8)
1417 {
8a3fe4f8 1418 warning (_("ERROR RMT: 'exists' length too long."));
c906108c
SS
1419 retval = 0;
1420 break;
1421 }
1422 continue;
1423 }
1424 if (tag == TAG_THREADNAME)
1425 {
1426 pkt = unpack_string (pkt, &info->shortname[0], length);
1427 mask = mask & ~TAG_THREADNAME;
1428 continue;
1429 }
1430 if (tag == TAG_DISPLAY)
1431 {
1432 pkt = unpack_string (pkt, &info->display[0], length);
1433 mask = mask & ~TAG_DISPLAY;
1434 continue;
1435 }
1436 if (tag == TAG_MOREDISPLAY)
1437 {
1438 pkt = unpack_string (pkt, &info->more_display[0], length);
1439 mask = mask & ~TAG_MOREDISPLAY;
1440 continue;
1441 }
8a3fe4f8 1442 warning (_("ERROR RMT: unknown thread info tag."));
23860348 1443 break; /* Not a tag we know about. */
c906108c
SS
1444 }
1445 return retval;
1446}
1447
1448static int
fba45db2
KB
1449remote_get_threadinfo (threadref *threadid, int fieldset, /* TAG mask */
1450 struct gdb_ext_thread_info *info)
c906108c 1451{
d01949b6 1452 struct remote_state *rs = get_remote_state ();
c906108c 1453 int result;
6d820c5c 1454 char *threadinfo_pkt = rs->buf;
c906108c
SS
1455
1456 pack_threadinfo_request (threadinfo_pkt, fieldset, threadid);
1457 putpkt (threadinfo_pkt);
6d820c5c 1458 getpkt (&rs->buf, &rs->buf_size, 0);
23860348
MS
1459 result = remote_unpack_thread_info_response (threadinfo_pkt + 2,
1460 threadid, info);
c906108c
SS
1461 return result;
1462}
1463
c906108c
SS
1464/* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
1465
1466static char *
fba45db2
KB
1467pack_threadlist_request (char *pkt, int startflag, int threadcount,
1468 threadref *nextthread)
c906108c
SS
1469{
1470 *pkt++ = 'q'; /* info query packet */
1471 *pkt++ = 'L'; /* Process LIST or threadLIST request */
23860348 1472 pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */
c906108c
SS
1473 pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */
1474 pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */
1475 *pkt = '\0';
1476 return pkt;
1477}
1478
1479/* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
1480
1481static int
fba45db2
KB
1482parse_threadlist_response (char *pkt, int result_limit,
1483 threadref *original_echo, threadref *resultlist,
1484 int *doneflag)
c906108c 1485{
d01949b6 1486 struct remote_state *rs = get_remote_state ();
c906108c
SS
1487 char *limit;
1488 int count, resultcount, done;
1489
1490 resultcount = 0;
1491 /* Assume the 'q' and 'M chars have been stripped. */
6d820c5c 1492 limit = pkt + (rs->buf_size - BUF_THREAD_ID_SIZE);
23860348 1493 /* done parse past here */
c906108c
SS
1494 pkt = unpack_byte (pkt, &count); /* count field */
1495 pkt = unpack_nibble (pkt, &done);
1496 /* The first threadid is the argument threadid. */
1497 pkt = unpack_threadid (pkt, original_echo); /* should match query packet */
1498 while ((count-- > 0) && (pkt < limit))
1499 {
1500 pkt = unpack_threadid (pkt, resultlist++);
1501 if (resultcount++ >= result_limit)
1502 break;
1503 }
1504 if (doneflag)
1505 *doneflag = done;
1506 return resultcount;
1507}
1508
1509static int
fba45db2
KB
1510remote_get_threadlist (int startflag, threadref *nextthread, int result_limit,
1511 int *done, int *result_count, threadref *threadlist)
c906108c 1512{
d01949b6 1513 struct remote_state *rs = get_remote_state ();
c906108c 1514 static threadref echo_nextthread;
6d820c5c 1515 char *threadlist_packet = rs->buf;
c906108c
SS
1516 int result = 1;
1517
23860348 1518 /* Trancate result limit to be smaller than the packet size. */
24b06219
MK
1519 if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10) >= rs->remote_packet_size)
1520 result_limit = (rs->remote_packet_size / BUF_THREAD_ID_SIZE) - 2;
c906108c 1521
6d820c5c
DJ
1522 pack_threadlist_request (rs->buf, startflag, result_limit, nextthread);
1523 putpkt (rs->buf);
1524 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c
SS
1525
1526 *result_count =
6d820c5c 1527 parse_threadlist_response (rs->buf + 2, result_limit, &echo_nextthread,
c906108c
SS
1528 threadlist, done);
1529
1530 if (!threadmatch (&echo_nextthread, nextthread))
1531 {
23860348
MS
1532 /* FIXME: This is a good reason to drop the packet. */
1533 /* Possably, there is a duplicate response. */
c906108c
SS
1534 /* Possabilities :
1535 retransmit immediatly - race conditions
1536 retransmit after timeout - yes
1537 exit
1538 wait for packet, then exit
1539 */
8a3fe4f8 1540 warning (_("HMM: threadlist did not echo arg thread, dropping it."));
23860348 1541 return 0; /* I choose simply exiting. */
c906108c
SS
1542 }
1543 if (*result_count <= 0)
1544 {
1545 if (*done != 1)
1546 {
8a3fe4f8 1547 warning (_("RMT ERROR : failed to get remote thread list."));
c906108c
SS
1548 result = 0;
1549 }
1550 return result; /* break; */
1551 }
1552 if (*result_count > result_limit)
1553 {
1554 *result_count = 0;
8a3fe4f8 1555 warning (_("RMT ERROR: threadlist response longer than requested."));
c906108c
SS
1556 return 0;
1557 }
1558 return result;
1559}
1560
23860348
MS
1561/* This is the interface between remote and threads, remotes upper
1562 interface. */
c906108c
SS
1563
1564/* remote_find_new_threads retrieves the thread list and for each
1565 thread in the list, looks up the thread in GDB's internal list,
1566 ading the thread if it does not already exist. This involves
1567 getting partial thread lists from the remote target so, polling the
1568 quit_flag is required. */
1569
1570
23860348 1571/* About this many threadisds fit in a packet. */
c906108c
SS
1572
1573#define MAXTHREADLISTRESULTS 32
1574
1575static int
fba45db2
KB
1576remote_threadlist_iterator (rmt_thread_action stepfunction, void *context,
1577 int looplimit)
c906108c
SS
1578{
1579 int done, i, result_count;
1580 int startflag = 1;
1581 int result = 1;
1582 int loopcount = 0;
1583 static threadref nextthread;
1584 static threadref resultthreadlist[MAXTHREADLISTRESULTS];
1585
1586 done = 0;
1587 while (!done)
1588 {
1589 if (loopcount++ > looplimit)
1590 {
1591 result = 0;
8a3fe4f8 1592 warning (_("Remote fetch threadlist -infinite loop-."));
c906108c
SS
1593 break;
1594 }
1595 if (!remote_get_threadlist (startflag, &nextthread, MAXTHREADLISTRESULTS,
1596 &done, &result_count, resultthreadlist))
1597 {
1598 result = 0;
1599 break;
1600 }
23860348 1601 /* Clear for later iterations. */
c906108c
SS
1602 startflag = 0;
1603 /* Setup to resume next batch of thread references, set nextthread. */
1604 if (result_count >= 1)
1605 copy_threadref (&nextthread, &resultthreadlist[result_count - 1]);
1606 i = 0;
1607 while (result_count--)
1608 if (!(result = (*stepfunction) (&resultthreadlist[i++], context)))
1609 break;
1610 }
1611 return result;
1612}
1613
1614static int
fba45db2 1615remote_newthread_step (threadref *ref, void *context)
c906108c 1616{
39f77062 1617 ptid_t ptid;
c906108c 1618
39f77062
KB
1619 ptid = pid_to_ptid (threadref_to_int (ref));
1620
1621 if (!in_thread_list (ptid))
1622 add_thread (ptid);
c906108c
SS
1623 return 1; /* continue iterator */
1624}
1625
1626#define CRAZY_MAX_THREADS 1000
1627
39f77062
KB
1628static ptid_t
1629remote_current_thread (ptid_t oldpid)
c906108c 1630{
d01949b6 1631 struct remote_state *rs = get_remote_state ();
6d820c5c 1632 char *buf = rs->buf;
c906108c
SS
1633
1634 putpkt ("qC");
6d820c5c 1635 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 1636 if (buf[0] == 'Q' && buf[1] == 'C')
c273b20f
JB
1637 /* Use strtoul here, so we'll correctly parse values whose highest
1638 bit is set. The protocol carries them as a simple series of
1639 hex digits; in the absence of a sign, strtol will see such
1640 values as positive numbers out of range for signed 'long', and
1641 return LONG_MAX to indicate an overflow. */
1642 return pid_to_ptid (strtoul (&buf[2], NULL, 16));
c906108c
SS
1643 else
1644 return oldpid;
1645}
1646
802188a7
RM
1647/* Find new threads for info threads command.
1648 * Original version, using John Metzler's thread protocol.
9d1f7ab2 1649 */
cce74817
JM
1650
1651static void
fba45db2 1652remote_find_new_threads (void)
c906108c 1653{
c5aa993b
JM
1654 remote_threadlist_iterator (remote_newthread_step, 0,
1655 CRAZY_MAX_THREADS);
39f77062
KB
1656 if (PIDGET (inferior_ptid) == MAGIC_NULL_PID) /* ack ack ack */
1657 inferior_ptid = remote_current_thread (inferior_ptid);
c906108c
SS
1658}
1659
9d1f7ab2
MS
1660/*
1661 * Find all threads for info threads command.
1662 * Uses new thread protocol contributed by Cisco.
1663 * Falls back and attempts to use the older method (above)
1664 * if the target doesn't respond to the new method.
1665 */
1666
0f71a2f6
JM
1667static void
1668remote_threads_info (void)
1669{
d01949b6 1670 struct remote_state *rs = get_remote_state ();
085dd6e6 1671 char *bufp;
0f71a2f6
JM
1672 int tid;
1673
1674 if (remote_desc == 0) /* paranoia */
8a3fe4f8 1675 error (_("Command can only be used when connected to the remote target."));
0f71a2f6 1676
9d1f7ab2
MS
1677 if (use_threadinfo_query)
1678 {
1679 putpkt ("qfThreadInfo");
6d820c5c
DJ
1680 bufp = rs->buf;
1681 getpkt (&rs->buf, &rs->buf_size, 0);
9d1f7ab2 1682 if (bufp[0] != '\0') /* q packet recognized */
802188a7 1683 {
9d1f7ab2
MS
1684 while (*bufp++ == 'm') /* reply contains one or more TID */
1685 {
1686 do
1687 {
c273b20f
JB
1688 /* Use strtoul here, so we'll correctly parse values
1689 whose highest bit is set. The protocol carries
1690 them as a simple series of hex digits; in the
1691 absence of a sign, strtol will see such values as
1692 positive numbers out of range for signed 'long',
1693 and return LONG_MAX to indicate an overflow. */
1694 tid = strtoul (bufp, &bufp, 16);
39f77062
KB
1695 if (tid != 0 && !in_thread_list (pid_to_ptid (tid)))
1696 add_thread (pid_to_ptid (tid));
9d1f7ab2
MS
1697 }
1698 while (*bufp++ == ','); /* comma-separated list */
1699 putpkt ("qsThreadInfo");
6d820c5c
DJ
1700 bufp = rs->buf;
1701 getpkt (&rs->buf, &rs->buf_size, 0);
9d1f7ab2
MS
1702 }
1703 return; /* done */
1704 }
1705 }
1706
23860348 1707 /* Else fall back to old method based on jmetzler protocol. */
9d1f7ab2
MS
1708 use_threadinfo_query = 0;
1709 remote_find_new_threads ();
1710 return;
1711}
1712
802188a7 1713/*
9d1f7ab2
MS
1714 * Collect a descriptive string about the given thread.
1715 * The target may say anything it wants to about the thread
1716 * (typically info about its blocked / runnable state, name, etc.).
1717 * This string will appear in the info threads display.
802188a7 1718 *
9d1f7ab2
MS
1719 * Optional: targets are not required to implement this function.
1720 */
1721
1722static char *
1723remote_threads_extra_info (struct thread_info *tp)
1724{
d01949b6 1725 struct remote_state *rs = get_remote_state ();
9d1f7ab2
MS
1726 int result;
1727 int set;
1728 threadref id;
1729 struct gdb_ext_thread_info threadinfo;
23860348 1730 static char display_buf[100]; /* arbitrary... */
9d1f7ab2
MS
1731 int n = 0; /* position in display_buf */
1732
1733 if (remote_desc == 0) /* paranoia */
8e65ff28 1734 internal_error (__FILE__, __LINE__,
e2e0b3e5 1735 _("remote_threads_extra_info"));
9d1f7ab2
MS
1736
1737 if (use_threadextra_query)
1738 {
6d820c5c
DJ
1739 char *bufp = rs->buf;
1740
ecbc58df
WZ
1741 xsnprintf (bufp, rs->remote_packet_size, "qThreadExtraInfo,%x",
1742 PIDGET (tp->ptid));
9d1f7ab2 1743 putpkt (bufp);
6d820c5c 1744 getpkt (&rs->buf, &rs->buf_size, 0);
9d1f7ab2
MS
1745 if (bufp[0] != 0)
1746 {
30559e10 1747 n = min (strlen (bufp) / 2, sizeof (display_buf));
cfd77fa1 1748 result = hex2bin (bufp, (gdb_byte *) display_buf, n);
30559e10 1749 display_buf [result] = '\0';
9d1f7ab2
MS
1750 return display_buf;
1751 }
0f71a2f6 1752 }
9d1f7ab2
MS
1753
1754 /* If the above query fails, fall back to the old method. */
1755 use_threadextra_query = 0;
1756 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
1757 | TAG_MOREDISPLAY | TAG_DISPLAY;
39f77062 1758 int_to_threadref (&id, PIDGET (tp->ptid));
9d1f7ab2
MS
1759 if (remote_get_threadinfo (&id, set, &threadinfo))
1760 if (threadinfo.active)
0f71a2f6 1761 {
9d1f7ab2 1762 if (*threadinfo.shortname)
ecbc58df
WZ
1763 n += xsnprintf (&display_buf[0], sizeof (display_buf) - n,
1764 " Name: %s,", threadinfo.shortname);
9d1f7ab2 1765 if (*threadinfo.display)
ecbc58df
WZ
1766 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
1767 " State: %s,", threadinfo.display);
9d1f7ab2 1768 if (*threadinfo.more_display)
ecbc58df
WZ
1769 n += xsnprintf (&display_buf[n], sizeof (display_buf) - n,
1770 " Priority: %s", threadinfo.more_display);
9d1f7ab2
MS
1771
1772 if (n > 0)
c5aa993b 1773 {
23860348 1774 /* For purely cosmetic reasons, clear up trailing commas. */
9d1f7ab2
MS
1775 if (',' == display_buf[n-1])
1776 display_buf[n-1] = ' ';
1777 return display_buf;
c5aa993b 1778 }
0f71a2f6 1779 }
9d1f7ab2 1780 return NULL;
0f71a2f6 1781}
c906108c 1782\f
c5aa993b 1783
24b06219 1784/* Restart the remote side; this is an extended protocol operation. */
c906108c
SS
1785
1786static void
fba45db2 1787extended_remote_restart (void)
c906108c 1788{
d01949b6 1789 struct remote_state *rs = get_remote_state ();
c906108c
SS
1790
1791 /* Send the restart command; for reasons I don't understand the
1792 remote side really expects a number after the "R". */
6d820c5c
DJ
1793 xsnprintf (rs->buf, rs->remote_packet_size, "R%x", 0);
1794 putpkt (rs->buf);
c906108c
SS
1795
1796 /* Now query for status so this looks just like we restarted
1797 gdbserver from scratch. */
1798 putpkt ("?");
6d820c5c 1799 getpkt (&rs->buf, &rs->remote_packet_size, 0);
c906108c
SS
1800}
1801\f
1802/* Clean up connection to a remote debugger. */
1803
c906108c 1804static void
fba45db2 1805remote_close (int quitting)
c906108c
SS
1806{
1807 if (remote_desc)
2cd58942 1808 serial_close (remote_desc);
c906108c
SS
1809 remote_desc = NULL;
1810}
1811
23860348 1812/* Query the remote side for the text, data and bss offsets. */
c906108c
SS
1813
1814static void
fba45db2 1815get_offsets (void)
c906108c 1816{
d01949b6 1817 struct remote_state *rs = get_remote_state ();
6d820c5c 1818 char *buf = rs->buf;
085dd6e6 1819 char *ptr;
c906108c
SS
1820 int lose;
1821 CORE_ADDR text_addr, data_addr, bss_addr;
1822 struct section_offsets *offs;
1823
1824 putpkt ("qOffsets");
6d820c5c 1825 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c
SS
1826
1827 if (buf[0] == '\000')
1828 return; /* Return silently. Stub doesn't support
23860348 1829 this command. */
c906108c
SS
1830 if (buf[0] == 'E')
1831 {
8a3fe4f8 1832 warning (_("Remote failure reply: %s"), buf);
c906108c
SS
1833 return;
1834 }
1835
1836 /* Pick up each field in turn. This used to be done with scanf, but
1837 scanf will make trouble if CORE_ADDR size doesn't match
1838 conversion directives correctly. The following code will work
1839 with any size of CORE_ADDR. */
1840 text_addr = data_addr = bss_addr = 0;
1841 ptr = buf;
1842 lose = 0;
1843
1844 if (strncmp (ptr, "Text=", 5) == 0)
1845 {
1846 ptr += 5;
1847 /* Don't use strtol, could lose on big values. */
1848 while (*ptr && *ptr != ';')
1849 text_addr = (text_addr << 4) + fromhex (*ptr++);
1850 }
1851 else
1852 lose = 1;
1853
1854 if (!lose && strncmp (ptr, ";Data=", 6) == 0)
1855 {
1856 ptr += 6;
1857 while (*ptr && *ptr != ';')
1858 data_addr = (data_addr << 4) + fromhex (*ptr++);
1859 }
1860 else
1861 lose = 1;
1862
1863 if (!lose && strncmp (ptr, ";Bss=", 5) == 0)
1864 {
1865 ptr += 5;
1866 while (*ptr && *ptr != ';')
1867 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
1868 }
1869 else
1870 lose = 1;
1871
1872 if (lose)
8a3fe4f8 1873 error (_("Malformed response to offset query, %s"), buf);
c906108c
SS
1874
1875 if (symfile_objfile == NULL)
1876 return;
1877
802188a7 1878 offs = ((struct section_offsets *)
a39a16c4 1879 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections)));
802188a7 1880 memcpy (offs, symfile_objfile->section_offsets,
a39a16c4 1881 SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections));
c906108c 1882
a4c8257b 1883 offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
c906108c
SS
1884
1885 /* This is a temporary kludge to force data and bss to use the same offsets
1886 because that's what nlmconv does now. The real solution requires changes
1887 to the stub and remote.c that I don't have time to do right now. */
1888
a4c8257b
EZ
1889 offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr;
1890 offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr;
c906108c
SS
1891
1892 objfile_relocate (symfile_objfile, offs);
1893}
1894
1895/* Stub for catch_errors. */
1896
0f71a2f6 1897static int
36918e70 1898remote_start_remote_dummy (struct ui_out *uiout, void *dummy)
0f71a2f6 1899{
23860348 1900 start_remote (); /* Initialize gdb process mechanisms. */
36918e70
AC
1901 /* NOTE: Return something >=0. A -ve value is reserved for
1902 catch_exceptions. */
0f71a2f6
JM
1903 return 1;
1904}
1905
9cbc821d 1906static void
36918e70 1907remote_start_remote (struct ui_out *uiout, void *dummy)
c906108c 1908{
23860348 1909 immediate_quit++; /* Allow user to interrupt it. */
c906108c
SS
1910
1911 /* Ack any packet which the remote side has already sent. */
2cd58942 1912 serial_write (remote_desc, "+", 1);
c906108c
SS
1913
1914 /* Let the stub know that we want it to return the thread. */
1915 set_thread (-1, 0);
1916
39f77062 1917 inferior_ptid = remote_current_thread (inferior_ptid);
c906108c 1918
23860348 1919 get_offsets (); /* Get text, data & bss offsets. */
c906108c 1920
23860348 1921 putpkt ("?"); /* Initiate a query from remote machine. */
8edbea78 1922 immediate_quit--;
c906108c 1923
9cbc821d 1924 remote_start_remote_dummy (uiout, dummy);
c906108c
SS
1925}
1926
1927/* Open a connection to a remote debugger.
1928 NAME is the filename used for communication. */
1929
1930static void
fba45db2 1931remote_open (char *name, int from_tty)
c906108c 1932{
92d1e331 1933 remote_open_1 (name, from_tty, &remote_ops, 0, 0);
c906108c
SS
1934}
1935
23860348 1936/* Just like remote_open, but with asynchronous support. */
43ff13b4 1937static void
fba45db2 1938remote_async_open (char *name, int from_tty)
43ff13b4 1939{
92d1e331 1940 remote_open_1 (name, from_tty, &remote_async_ops, 0, 1);
43ff13b4
JM
1941}
1942
c906108c
SS
1943/* Open a connection to a remote debugger using the extended
1944 remote gdb protocol. NAME is the filename used for communication. */
1945
1946static void
fba45db2 1947extended_remote_open (char *name, int from_tty)
c906108c 1948{
92d1e331
DJ
1949 remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */,
1950 0 /* async_p */);
c906108c
SS
1951}
1952
23860348 1953/* Just like extended_remote_open, but with asynchronous support. */
43ff13b4 1954static void
fba45db2 1955extended_remote_async_open (char *name, int from_tty)
43ff13b4 1956{
92d1e331
DJ
1957 remote_open_1 (name, from_tty, &extended_async_remote_ops,
1958 1 /*extended_p */, 1 /* async_p */);
43ff13b4
JM
1959}
1960
c906108c
SS
1961/* Generic code for opening a connection to a remote target. */
1962
d471ea57
AC
1963static void
1964init_all_packet_configs (void)
1965{
1966 int i;
444abaca
DJ
1967 for (i = 0; i < PACKET_MAX; i++)
1968 update_packet_config (&remote_protocol_packets[i]);
d471ea57
AC
1969}
1970
23860348 1971/* Symbol look-up. */
dc8acb97
MS
1972
1973static void
1974remote_check_symbols (struct objfile *objfile)
1975{
d01949b6 1976 struct remote_state *rs = get_remote_state ();
dc8acb97
MS
1977 char *msg, *reply, *tmp;
1978 struct minimal_symbol *sym;
1979 int end;
1980
444abaca 1981 if (remote_protocol_packets[PACKET_qSymbol].support == PACKET_DISABLE)
dc8acb97
MS
1982 return;
1983
6d820c5c
DJ
1984 /* Allocate a message buffer. We can't reuse the input buffer in RS,
1985 because we need both at the same time. */
1986 msg = alloca (rs->remote_packet_size);
1987
1988 reply = rs->buf;
dc8acb97 1989
23860348 1990 /* Invite target to request symbol lookups. */
dc8acb97
MS
1991
1992 putpkt ("qSymbol::");
6d820c5c
DJ
1993 getpkt (&rs->buf, &rs->buf_size, 0);
1994 packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSymbol]);
dc8acb97
MS
1995
1996 while (strncmp (reply, "qSymbol:", 8) == 0)
1997 {
1998 tmp = &reply[8];
cfd77fa1 1999 end = hex2bin (tmp, (gdb_byte *) msg, strlen (tmp) / 2);
dc8acb97
MS
2000 msg[end] = '\0';
2001 sym = lookup_minimal_symbol (msg, NULL, NULL);
2002 if (sym == NULL)
ecbc58df 2003 xsnprintf (msg, rs->remote_packet_size, "qSymbol::%s", &reply[8]);
dc8acb97 2004 else
ecbc58df
WZ
2005 xsnprintf (msg, rs->remote_packet_size, "qSymbol:%s:%s",
2006 paddr_nz (SYMBOL_VALUE_ADDRESS (sym)),
2007 &reply[8]);
dc8acb97 2008 putpkt (msg);
6d820c5c 2009 getpkt (&rs->buf, &rs->buf_size, 0);
dc8acb97
MS
2010 }
2011}
2012
9db8d71f
DJ
2013static struct serial *
2014remote_serial_open (char *name)
2015{
2016 static int udp_warning = 0;
2017
2018 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
2019 of in ser-tcp.c, because it is the remote protocol assuming that the
2020 serial connection is reliable and not the serial connection promising
2021 to be. */
2022 if (!udp_warning && strncmp (name, "udp:", 4) == 0)
2023 {
8a3fe4f8
AC
2024 warning (_("\
2025The remote protocol may be unreliable over UDP.\n\
2026Some events may be lost, rendering further debugging impossible."));
9db8d71f
DJ
2027 udp_warning = 1;
2028 }
2029
2030 return serial_open (name);
2031}
2032
c906108c 2033static void
fba45db2 2034remote_open_1 (char *name, int from_tty, struct target_ops *target,
92d1e331 2035 int extended_p, int async_p)
c906108c 2036{
d01949b6 2037 struct remote_state *rs = get_remote_state ();
c906108c 2038 if (name == 0)
8a3fe4f8 2039 error (_("To open a remote debug connection, you need to specify what\n"
22e04375 2040 "serial device is attached to the remote system\n"
8a3fe4f8 2041 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
c906108c 2042
23860348 2043 /* See FIXME above. */
92d1e331
DJ
2044 if (!async_p)
2045 wait_forever_enabled_p = 1;
6426a772 2046
00cac895
DJ
2047 reopen_exec_file ();
2048 reread_symbols ();
2049
c906108c
SS
2050 target_preopen (from_tty);
2051
2052 unpush_target (target);
2053
9db8d71f 2054 remote_desc = remote_serial_open (name);
c906108c
SS
2055 if (!remote_desc)
2056 perror_with_name (name);
2057
2058 if (baud_rate != -1)
2059 {
2cd58942 2060 if (serial_setbaudrate (remote_desc, baud_rate))
c906108c 2061 {
9b74d5d3
KB
2062 /* The requested speed could not be set. Error out to
2063 top level after closing remote_desc. Take care to
2064 set remote_desc to NULL to avoid closing remote_desc
2065 more than once. */
2cd58942 2066 serial_close (remote_desc);
9b74d5d3 2067 remote_desc = NULL;
c906108c
SS
2068 perror_with_name (name);
2069 }
2070 }
2071
2cd58942 2072 serial_raw (remote_desc);
c906108c
SS
2073
2074 /* If there is something sitting in the buffer we might take it as a
2075 response to a command, which would be bad. */
2cd58942 2076 serial_flush_input (remote_desc);
c906108c
SS
2077
2078 if (from_tty)
2079 {
2080 puts_filtered ("Remote debugging using ");
2081 puts_filtered (name);
2082 puts_filtered ("\n");
2083 }
23860348 2084 push_target (target); /* Switch to using remote target now. */
c906108c 2085
d471ea57 2086 init_all_packet_configs ();
802188a7 2087
c5aa993b 2088 general_thread = -2;
cce74817 2089 continue_thread = -2;
c906108c 2090
9d1f7ab2
MS
2091 /* Probe for ability to use "ThreadInfo" query, as required. */
2092 use_threadinfo_query = 1;
2093 use_threadextra_query = 1;
2094
c906108c
SS
2095 /* Without this, some commands which require an active target (such
2096 as kill) won't work. This variable serves (at least) double duty
2097 as both the pid of the target process (if it has such), and as a
2098 flag indicating that a target is active. These functions should
2099 be split out into seperate variables, especially since GDB will
2100 someday have a notion of debugging several processes. */
2101
39f77062 2102 inferior_ptid = pid_to_ptid (MAGIC_NULL_PID);
92d1e331
DJ
2103
2104 if (async_p)
2105 {
23860348 2106 /* With this target we start out by owning the terminal. */
92d1e331
DJ
2107 remote_async_terminal_ours_p = 1;
2108
2109 /* FIXME: cagney/1999-09-23: During the initial connection it is
2110 assumed that the target is already ready and able to respond to
2111 requests. Unfortunately remote_start_remote() eventually calls
2112 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
2113 around this. Eventually a mechanism that allows
2114 wait_for_inferior() to expect/get timeouts will be
23860348 2115 implemented. */
92d1e331
DJ
2116 wait_forever_enabled_p = 0;
2117 }
2118
23860348 2119 /* First delete any symbols previously loaded from shared libraries. */
f78f6cf1 2120 no_shared_libraries (NULL, 0);
f78f6cf1 2121
36918e70 2122 /* Start the remote connection. If error() or QUIT, discard this
165b8e33
AC
2123 target (we'd otherwise be in an inconsistent state) and then
2124 propogate the error on up the exception chain. This ensures that
2125 the caller doesn't stumble along blindly assuming that the
2126 function succeeded. The CLI doesn't have this problem but other
2127 UI's, such as MI do.
36918e70
AC
2128
2129 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
2130 this function should return an error indication letting the
ce2826aa 2131 caller restore the previous state. Unfortunately the command
36918e70
AC
2132 ``target remote'' is directly wired to this function making that
2133 impossible. On a positive note, the CLI side of this problem has
2134 been fixed - the function set_cmd_context() makes it possible for
2135 all the ``target ....'' commands to share a common callback
2136 function. See cli-dump.c. */
109c3e39
AC
2137 {
2138 struct gdb_exception ex
2139 = catch_exception (uiout, remote_start_remote, NULL, RETURN_MASK_ALL);
2140 if (ex.reason < 0)
2141 {
2142 pop_target ();
2143 if (async_p)
2144 wait_forever_enabled_p = 1;
2145 throw_exception (ex);
2146 }
2147 }
c906108c 2148
92d1e331
DJ
2149 if (async_p)
2150 wait_forever_enabled_p = 1;
6426a772
JM
2151
2152 if (extended_p)
43ff13b4 2153 {
6240bebf 2154 /* Tell the remote that we are using the extended protocol. */
6426a772 2155 putpkt ("!");
6d820c5c 2156 getpkt (&rs->buf, &rs->buf_size, 0);
43ff13b4 2157 }
a77053c2 2158
9353355f 2159 post_create_inferior (&current_target, from_tty);
f78f6cf1 2160
23860348 2161 if (exec_bfd) /* No use without an exec file. */
9353355f 2162 remote_check_symbols (symfile_objfile);
43ff13b4
JM
2163}
2164
c906108c
SS
2165/* This takes a program previously attached to and detaches it. After
2166 this is done, GDB can be used to debug some other program. We
2167 better not have left any breakpoints in the target program or it'll
2168 die when it hits one. */
2169
2170static void
fba45db2 2171remote_detach (char *args, int from_tty)
c906108c 2172{
d01949b6 2173 struct remote_state *rs = get_remote_state ();
c906108c
SS
2174
2175 if (args)
8a3fe4f8 2176 error (_("Argument given to \"detach\" when remotely debugging."));
c906108c
SS
2177
2178 /* Tell the remote target to detach. */
6d820c5c
DJ
2179 strcpy (rs->buf, "D");
2180 remote_send (&rs->buf, &rs->buf_size);
c906108c 2181
23860348 2182 /* Unregister the file descriptor from the event loop. */
6ad8ae5c
DJ
2183 if (target_is_async_p ())
2184 serial_async (remote_desc, NULL, 0);
2185
cca728d0 2186 target_mourn_inferior ();
c906108c
SS
2187 if (from_tty)
2188 puts_filtered ("Ending remote debugging.\n");
2189}
2190
6ad8ae5c
DJ
2191/* Same as remote_detach, but don't send the "D" packet; just disconnect. */
2192
43ff13b4 2193static void
6ad8ae5c 2194remote_disconnect (char *args, int from_tty)
43ff13b4 2195{
43ff13b4 2196 if (args)
8a3fe4f8 2197 error (_("Argument given to \"detach\" when remotely debugging."));
43ff13b4 2198
23860348 2199 /* Unregister the file descriptor from the event loop. */
ed9a39eb 2200 if (target_is_async_p ())
2cd58942 2201 serial_async (remote_desc, NULL, 0);
43ff13b4 2202
cca728d0 2203 target_mourn_inferior ();
43ff13b4
JM
2204 if (from_tty)
2205 puts_filtered ("Ending remote debugging.\n");
2206}
2207
c906108c
SS
2208/* Convert hex digit A to a number. */
2209
30559e10 2210static int
fba45db2 2211fromhex (int a)
c906108c
SS
2212{
2213 if (a >= '0' && a <= '9')
2214 return a - '0';
2215 else if (a >= 'a' && a <= 'f')
2216 return a - 'a' + 10;
2217 else if (a >= 'A' && a <= 'F')
2218 return a - 'A' + 10;
c5aa993b 2219 else
8a3fe4f8 2220 error (_("Reply contains invalid hex digit %d"), a);
c906108c
SS
2221}
2222
30559e10 2223static int
cfd77fa1 2224hex2bin (const char *hex, gdb_byte *bin, int count)
30559e10
MS
2225{
2226 int i;
2227
30559e10
MS
2228 for (i = 0; i < count; i++)
2229 {
2230 if (hex[0] == 0 || hex[1] == 0)
2231 {
2232 /* Hex string is short, or of uneven length.
23860348 2233 Return the count that has been converted so far. */
30559e10
MS
2234 return i;
2235 }
2236 *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
2237 hex += 2;
2238 }
2239 return i;
2240}
2241
c906108c
SS
2242/* Convert number NIB to a hex digit. */
2243
2244static int
fba45db2 2245tohex (int nib)
c906108c
SS
2246{
2247 if (nib < 10)
c5aa993b 2248 return '0' + nib;
c906108c 2249 else
c5aa993b 2250 return 'a' + nib - 10;
c906108c 2251}
30559e10
MS
2252
2253static int
cfd77fa1 2254bin2hex (const gdb_byte *bin, char *hex, int count)
30559e10
MS
2255{
2256 int i;
23860348 2257 /* May use a length, or a nul-terminated string as input. */
30559e10 2258 if (count == 0)
cfd77fa1 2259 count = strlen ((char *) bin);
30559e10
MS
2260
2261 for (i = 0; i < count; i++)
2262 {
2263 *hex++ = tohex ((*bin >> 4) & 0xf);
2264 *hex++ = tohex (*bin++ & 0xf);
2265 }
2266 *hex = 0;
2267 return i;
2268}
c906108c 2269\f
506fb367
DJ
2270/* Check for the availability of vCont. This function should also check
2271 the response. */
c906108c
SS
2272
2273static void
6d820c5c 2274remote_vcont_probe (struct remote_state *rs)
c906108c 2275{
6d820c5c
DJ
2276 char *buf = rs->buf;
2277
506fb367
DJ
2278 strcpy (buf, "vCont?");
2279 putpkt (buf);
6d820c5c 2280 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 2281
506fb367
DJ
2282 /* Make sure that the features we assume are supported. */
2283 if (strncmp (buf, "vCont", 5) == 0)
2284 {
2285 char *p = &buf[5];
2286 int support_s, support_S, support_c, support_C;
2287
2288 support_s = 0;
2289 support_S = 0;
2290 support_c = 0;
2291 support_C = 0;
2292 while (p && *p == ';')
2293 {
2294 p++;
2295 if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0))
2296 support_s = 1;
2297 else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0))
2298 support_S = 1;
2299 else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0))
2300 support_c = 1;
2301 else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0))
2302 support_C = 1;
2303
2304 p = strchr (p, ';');
2305 }
c906108c 2306
506fb367
DJ
2307 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
2308 BUF will make packet_ok disable the packet. */
2309 if (!support_s || !support_S || !support_c || !support_C)
2310 buf[0] = 0;
2311 }
c906108c 2312
444abaca 2313 packet_ok (buf, &remote_protocol_packets[PACKET_vCont]);
506fb367 2314}
c906108c 2315
506fb367
DJ
2316/* Resume the remote inferior by using a "vCont" packet. The thread
2317 to be resumed is PTID; STEP and SIGGNAL indicate whether the
2318 resumed thread should be single-stepped and/or signalled. If PTID's
2319 PID is -1, then all threads are resumed; the thread to be stepped and/or
2320 signalled is given in the global INFERIOR_PTID. This function returns
2321 non-zero iff it resumes the inferior.
44eaed12 2322
506fb367
DJ
2323 This function issues a strict subset of all possible vCont commands at the
2324 moment. */
44eaed12 2325
506fb367
DJ
2326static int
2327remote_vcont_resume (ptid_t ptid, int step, enum target_signal siggnal)
2328{
2329 struct remote_state *rs = get_remote_state ();
2330 int pid = PIDGET (ptid);
2963ee1d 2331 char *buf = NULL, *outbuf;
506fb367 2332 struct cleanup *old_cleanup;
44eaed12 2333
444abaca 2334 if (remote_protocol_packets[PACKET_vCont].support == PACKET_SUPPORT_UNKNOWN)
6d820c5c 2335 remote_vcont_probe (rs);
44eaed12 2336
444abaca 2337 if (remote_protocol_packets[PACKET_vCont].support == PACKET_DISABLE)
6d820c5c 2338 return 0;
44eaed12 2339
506fb367
DJ
2340 /* If we could generate a wider range of packets, we'd have to worry
2341 about overflowing BUF. Should there be a generic
2342 "multi-part-packet" packet? */
2343
2344 if (PIDGET (inferior_ptid) == MAGIC_NULL_PID)
c906108c 2345 {
506fb367
DJ
2346 /* MAGIC_NULL_PTID means that we don't have any active threads, so we
2347 don't have any PID numbers the inferior will understand. Make sure
2348 to only send forms that do not specify a PID. */
2349 if (step && siggnal != TARGET_SIGNAL_0)
2963ee1d 2350 outbuf = xstrprintf ("vCont;S%02x", siggnal);
506fb367 2351 else if (step)
2963ee1d 2352 outbuf = xstrprintf ("vCont;s");
506fb367 2353 else if (siggnal != TARGET_SIGNAL_0)
2963ee1d 2354 outbuf = xstrprintf ("vCont;C%02x", siggnal);
506fb367 2355 else
2963ee1d 2356 outbuf = xstrprintf ("vCont;c");
506fb367
DJ
2357 }
2358 else if (pid == -1)
2359 {
2360 /* Resume all threads, with preference for INFERIOR_PTID. */
2361 if (step && siggnal != TARGET_SIGNAL_0)
2963ee1d
DJ
2362 outbuf = xstrprintf ("vCont;S%02x:%x;c", siggnal,
2363 PIDGET (inferior_ptid));
506fb367 2364 else if (step)
2963ee1d 2365 outbuf = xstrprintf ("vCont;s:%x;c", PIDGET (inferior_ptid));
506fb367 2366 else if (siggnal != TARGET_SIGNAL_0)
2963ee1d
DJ
2367 outbuf = xstrprintf ("vCont;C%02x:%x;c", siggnal,
2368 PIDGET (inferior_ptid));
506fb367 2369 else
2963ee1d 2370 outbuf = xstrprintf ("vCont;c");
c906108c
SS
2371 }
2372 else
506fb367
DJ
2373 {
2374 /* Scheduler locking; resume only PTID. */
2375 if (step && siggnal != TARGET_SIGNAL_0)
2963ee1d 2376 outbuf = xstrprintf ("vCont;S%02x:%x", siggnal, pid);
506fb367 2377 else if (step)
2963ee1d 2378 outbuf = xstrprintf ("vCont;s:%x", pid);
506fb367 2379 else if (siggnal != TARGET_SIGNAL_0)
2963ee1d 2380 outbuf = xstrprintf ("vCont;C%02x:%x", siggnal, pid);
506fb367 2381 else
2963ee1d 2382 outbuf = xstrprintf ("vCont;c:%x", pid);
506fb367 2383 }
c906108c 2384
2963ee1d 2385 gdb_assert (outbuf && strlen (outbuf) < rs->remote_packet_size);
6d820c5c 2386 old_cleanup = make_cleanup (xfree, outbuf);
2963ee1d
DJ
2387
2388 putpkt (outbuf);
506fb367
DJ
2389
2390 do_cleanups (old_cleanup);
2391
2392 return 1;
c906108c 2393}
43ff13b4 2394
506fb367
DJ
2395/* Tell the remote machine to resume. */
2396
2397static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
2398
2399static int last_sent_step;
2400
43ff13b4 2401static void
506fb367 2402remote_resume (ptid_t ptid, int step, enum target_signal siggnal)
43ff13b4 2403{
d01949b6 2404 struct remote_state *rs = get_remote_state ();
6d820c5c 2405 char *buf = rs->buf;
39f77062 2406 int pid = PIDGET (ptid);
43ff13b4 2407
43ff13b4
JM
2408 last_sent_signal = siggnal;
2409 last_sent_step = step;
2410
2411 /* A hook for when we need to do something at the last moment before
2412 resumption. */
9a4105ab
AC
2413 if (deprecated_target_resume_hook)
2414 (*deprecated_target_resume_hook) ();
43ff13b4 2415
506fb367
DJ
2416 /* The vCont packet doesn't need to specify threads via Hc. */
2417 if (remote_vcont_resume (ptid, step, siggnal))
2418 return;
2419
2420 /* All other supported resume packets do use Hc, so call set_thread. */
2421 if (pid == -1)
23860348 2422 set_thread (0, 0); /* Run any thread. */
506fb367 2423 else
23860348 2424 set_thread (pid, 0); /* Run this thread. */
506fb367 2425
43ff13b4
JM
2426 if (siggnal != TARGET_SIGNAL_0)
2427 {
2428 buf[0] = step ? 'S' : 'C';
c5aa993b 2429 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
506fb367 2430 buf[2] = tohex (((int) siggnal) & 0xf);
43ff13b4
JM
2431 buf[3] = '\0';
2432 }
2433 else
c5aa993b 2434 strcpy (buf, step ? "s" : "c");
506fb367 2435
44eaed12 2436 putpkt (buf);
506fb367
DJ
2437}
2438
23860348 2439/* Same as remote_resume, but with async support. */
506fb367
DJ
2440static void
2441remote_async_resume (ptid_t ptid, int step, enum target_signal siggnal)
2442{
2443 remote_resume (ptid, step, siggnal);
43ff13b4 2444
2acceee2
JM
2445 /* We are about to start executing the inferior, let's register it
2446 with the event loop. NOTE: this is the one place where all the
2447 execution commands end up. We could alternatively do this in each
23860348 2448 of the execution commands in infcmd.c. */
2acceee2
JM
2449 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
2450 into infcmd.c in order to allow inferior function calls to work
23860348 2451 NOT asynchronously. */
362646f5 2452 if (target_can_async_p ())
2acceee2 2453 target_async (inferior_event_handler, 0);
23860348 2454 /* Tell the world that the target is now executing. */
2acceee2
JM
2455 /* FIXME: cagney/1999-09-23: Is it the targets responsibility to set
2456 this? Instead, should the client of target just assume (for
2457 async targets) that the target is going to start executing? Is
2458 this information already found in the continuation block? */
ed9a39eb 2459 if (target_is_async_p ())
2acceee2 2460 target_executing = 1;
43ff13b4 2461}
c906108c 2462\f
43ff13b4
JM
2463
2464/* Set up the signal handler for SIGINT, while the target is
23860348 2465 executing, ovewriting the 'regular' SIGINT signal handler. */
43ff13b4 2466static void
fba45db2 2467initialize_sigint_signal_handler (void)
43ff13b4 2468{
c5aa993b 2469 sigint_remote_token =
43ff13b4
JM
2470 create_async_signal_handler (async_remote_interrupt, NULL);
2471 signal (SIGINT, handle_remote_sigint);
2472}
2473
23860348 2474/* Signal handler for SIGINT, while the target is executing. */
43ff13b4 2475static void
fba45db2 2476handle_remote_sigint (int sig)
43ff13b4
JM
2477{
2478 signal (sig, handle_remote_sigint_twice);
c5aa993b 2479 sigint_remote_twice_token =
43ff13b4
JM
2480 create_async_signal_handler (async_remote_interrupt_twice, NULL);
2481 mark_async_signal_handler_wrapper (sigint_remote_token);
2482}
2483
2484/* Signal handler for SIGINT, installed after SIGINT has already been
2485 sent once. It will take effect the second time that the user sends
23860348 2486 a ^C. */
43ff13b4 2487static void
fba45db2 2488handle_remote_sigint_twice (int sig)
43ff13b4
JM
2489{
2490 signal (sig, handle_sigint);
c5aa993b 2491 sigint_remote_twice_token =
2df3850c 2492 create_async_signal_handler (inferior_event_handler_wrapper, NULL);
43ff13b4
JM
2493 mark_async_signal_handler_wrapper (sigint_remote_twice_token);
2494}
2495
6426a772 2496/* Perform the real interruption of the target execution, in response
23860348 2497 to a ^C. */
c5aa993b 2498static void
fba45db2 2499async_remote_interrupt (gdb_client_data arg)
43ff13b4
JM
2500{
2501 if (remote_debug)
2502 fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
2503
2504 target_stop ();
2505}
2506
2507/* Perform interrupt, if the first attempt did not succeed. Just give
23860348 2508 up on the target alltogether. */
2df3850c 2509void
fba45db2 2510async_remote_interrupt_twice (gdb_client_data arg)
43ff13b4 2511{
2df3850c
JM
2512 if (remote_debug)
2513 fprintf_unfiltered (gdb_stdlog, "remote_interrupt_twice called\n");
6426a772 2514 /* Do something only if the target was not killed by the previous
23860348 2515 cntl-C. */
6426a772
JM
2516 if (target_executing)
2517 {
2518 interrupt_query ();
2519 signal (SIGINT, handle_remote_sigint);
2520 }
43ff13b4
JM
2521}
2522
2523/* Reinstall the usual SIGINT handlers, after the target has
23860348 2524 stopped. */
6426a772
JM
2525static void
2526cleanup_sigint_signal_handler (void *dummy)
43ff13b4
JM
2527{
2528 signal (SIGINT, handle_sigint);
2529 if (sigint_remote_twice_token)
23860348
MS
2530 delete_async_signal_handler ((struct async_signal_handler **)
2531 &sigint_remote_twice_token);
43ff13b4 2532 if (sigint_remote_token)
23860348
MS
2533 delete_async_signal_handler ((struct async_signal_handler **)
2534 &sigint_remote_token);
43ff13b4
JM
2535}
2536
c906108c
SS
2537/* Send ^C to target to halt it. Target will respond, and send us a
2538 packet. */
507f3c78 2539static void (*ofunc) (int);
c906108c 2540
7a292a7a
SS
2541/* The command line interface's stop routine. This function is installed
2542 as a signal handler for SIGINT. The first time a user requests a
2543 stop, we call remote_stop to send a break or ^C. If there is no
2544 response from the target (it didn't stop when the user requested it),
23860348 2545 we ask the user if he'd like to detach from the target. */
c906108c 2546static void
fba45db2 2547remote_interrupt (int signo)
c906108c 2548{
23860348 2549 /* If this doesn't work, try more severe steps. */
7a292a7a
SS
2550 signal (signo, remote_interrupt_twice);
2551
2552 if (remote_debug)
0f71a2f6 2553 fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
7a292a7a
SS
2554
2555 target_stop ();
2556}
2557
2558/* The user typed ^C twice. */
2559
2560static void
fba45db2 2561remote_interrupt_twice (int signo)
7a292a7a
SS
2562{
2563 signal (signo, ofunc);
2564 interrupt_query ();
c906108c
SS
2565 signal (signo, remote_interrupt);
2566}
7a292a7a
SS
2567
2568/* This is the generic stop called via the target vector. When a target
2569 interrupt is requested, either by the command line or the GUI, we
23860348 2570 will eventually end up here. */
c906108c 2571static void
fba45db2 2572remote_stop (void)
c906108c 2573{
7a292a7a
SS
2574 /* Send a break or a ^C, depending on user preference. */
2575 if (remote_debug)
0f71a2f6 2576 fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
c906108c 2577
7a292a7a 2578 if (remote_break)
2cd58942 2579 serial_send_break (remote_desc);
c906108c 2580 else
2cd58942 2581 serial_write (remote_desc, "\003", 1);
c906108c
SS
2582}
2583
2584/* Ask the user what to do when an interrupt is received. */
2585
2586static void
fba45db2 2587interrupt_query (void)
c906108c
SS
2588{
2589 target_terminal_ours ();
2590
2591 if (query ("Interrupted while waiting for the program.\n\
2592Give up (and stop debugging it)? "))
2593 {
2594 target_mourn_inferior ();
315a522e 2595 deprecated_throw_reason (RETURN_QUIT);
c906108c
SS
2596 }
2597
2598 target_terminal_inferior ();
2599}
2600
6426a772
JM
2601/* Enable/disable target terminal ownership. Most targets can use
2602 terminal groups to control terminal ownership. Remote targets are
2603 different in that explicit transfer of ownership to/from GDB/target
23860348 2604 is required. */
6426a772
JM
2605
2606static void
2607remote_async_terminal_inferior (void)
2608{
2609 /* FIXME: cagney/1999-09-27: Shouldn't need to test for
2610 sync_execution here. This function should only be called when
2611 GDB is resuming the inferior in the forground. A background
2612 resume (``run&'') should leave GDB in control of the terminal and
23860348 2613 consequently should not call this code. */
6426a772
JM
2614 if (!sync_execution)
2615 return;
2616 /* FIXME: cagney/1999-09-27: Closely related to the above. Make
2617 calls target_terminal_*() idenpotent. The event-loop GDB talking
2618 to an asynchronous target with a synchronous command calls this
2619 function from both event-top.c and infrun.c/infcmd.c. Once GDB
2620 stops trying to transfer the terminal to the target when it
2621 shouldn't this guard can go away. */
2622 if (!remote_async_terminal_ours_p)
2623 return;
2624 delete_file_handler (input_fd);
2625 remote_async_terminal_ours_p = 0;
2626 initialize_sigint_signal_handler ();
2627 /* NOTE: At this point we could also register our selves as the
2628 recipient of all input. Any characters typed could then be
23860348 2629 passed on down to the target. */
6426a772
JM
2630}
2631
2632static void
2633remote_async_terminal_ours (void)
2634{
23860348 2635 /* See FIXME in remote_async_terminal_inferior. */
6426a772
JM
2636 if (!sync_execution)
2637 return;
23860348 2638 /* See FIXME in remote_async_terminal_inferior. */
6426a772
JM
2639 if (remote_async_terminal_ours_p)
2640 return;
2641 cleanup_sigint_signal_handler (NULL);
2642 add_file_handler (input_fd, stdin_event_handler, 0);
2643 remote_async_terminal_ours_p = 1;
2644}
2645
c906108c
SS
2646/* If nonzero, ignore the next kill. */
2647
2648int kill_kludge;
2649
2650void
917317f4 2651remote_console_output (char *msg)
c906108c
SS
2652{
2653 char *p;
2654
c5aa993b 2655 for (p = msg; p[0] && p[1]; p += 2)
c906108c
SS
2656 {
2657 char tb[2];
2658 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
2659 tb[0] = c;
2660 tb[1] = 0;
43ff13b4 2661 fputs_unfiltered (tb, gdb_stdtarg);
c906108c 2662 }
917317f4 2663 gdb_flush (gdb_stdtarg);
c906108c
SS
2664}
2665
0f71a2f6
JM
2666/* Wait until the remote machine stops, then return,
2667 storing status in STATUS just as `wait' would.
802188a7 2668 Returns "pid", which in the case of a multi-threaded
0f71a2f6 2669 remote OS, is the thread-id. */
c906108c 2670
39f77062
KB
2671static ptid_t
2672remote_wait (ptid_t ptid, struct target_waitstatus *status)
c906108c 2673{
d01949b6 2674 struct remote_state *rs = get_remote_state ();
6d820c5c 2675 char *buf = rs->buf;
b2dd6311 2676 ULONGEST thread_num = -1;
3c3bea1c 2677 ULONGEST addr;
c906108c
SS
2678
2679 status->kind = TARGET_WAITKIND_EXITED;
2680 status->value.integer = 0;
2681
2682 while (1)
2683 {
cfd77fa1 2684 char *p;
c906108c 2685
c906108c 2686 ofunc = signal (SIGINT, remote_interrupt);
6d820c5c 2687 getpkt (&rs->buf, &rs->buf_size, 1);
c906108c
SS
2688 signal (SIGINT, ofunc);
2689
2690 /* This is a hook for when we need to do something (perhaps the
c5aa993b 2691 collection of trace data) every time the target stops. */
9a4105ab
AC
2692 if (deprecated_target_wait_loop_hook)
2693 (*deprecated_target_wait_loop_hook) ();
c906108c 2694
3c3bea1c
GS
2695 remote_stopped_by_watchpoint_p = 0;
2696
c906108c
SS
2697 switch (buf[0])
2698 {
23860348 2699 case 'E': /* Error of some sort. */
8a3fe4f8 2700 warning (_("Remote failure reply: %s"), buf);
c906108c 2701 continue;
23860348 2702 case 'F': /* File-I/O request. */
449092f6
CV
2703 remote_fileio_request (buf);
2704 continue;
23860348 2705 case 'T': /* Status with PC, SP, FP, ... */
c906108c 2706 {
cfd77fa1 2707 gdb_byte regs[MAX_REGISTER_SIZE];
c906108c 2708
23860348 2709 /* Expedited reply, containing Signal, {regno, reg} repeat. */
c906108c 2710 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
c5aa993b
JM
2711 ss = signal number
2712 n... = register number
2713 r... = register contents
2714 */
c906108c
SS
2715 p = &buf[3]; /* after Txx */
2716
2717 while (*p)
2718 {
cfd77fa1 2719 char *p1;
c906108c 2720 char *p_temp;
97345198 2721 int fieldsize;
3c3bea1c
GS
2722 LONGEST pnum = 0;
2723
23860348
MS
2724 /* If the packet contains a register number save it in
2725 pnum and set p1 to point to the character following
2726 it. Otherwise p1 points to p. */
c906108c 2727
23860348
MS
2728 /* If this packet is an awatch packet, don't parse the
2729 'a' as a register number. */
3c3bea1c
GS
2730
2731 if (strncmp (p, "awatch", strlen("awatch")) != 0)
2732 {
2733 /* Read the ``P'' register number. */
2734 pnum = strtol (p, &p_temp, 16);
cfd77fa1 2735 p1 = p_temp;
3c3bea1c 2736 }
802188a7 2737 else
3c3bea1c 2738 p1 = p;
c906108c 2739
23860348 2740 if (p1 == p) /* No register number present here. */
c906108c 2741 {
cfd77fa1 2742 p1 = strchr (p, ':');
c906108c 2743 if (p1 == NULL)
8a3fe4f8
AC
2744 warning (_("Malformed packet(a) (missing colon): %s\n\
2745Packet: '%s'\n"),
c906108c 2746 p, buf);
3c3bea1c 2747 if (strncmp (p, "thread", p1 - p) == 0)
c906108c
SS
2748 {
2749 p_temp = unpack_varlen_hex (++p1, &thread_num);
2750 record_currthread (thread_num);
cfd77fa1 2751 p = p_temp;
c906108c 2752 }
3c3bea1c
GS
2753 else if ((strncmp (p, "watch", p1 - p) == 0)
2754 || (strncmp (p, "rwatch", p1 - p) == 0)
2755 || (strncmp (p, "awatch", p1 - p) == 0))
2756 {
2757 remote_stopped_by_watchpoint_p = 1;
2758 p = unpack_varlen_hex (++p1, &addr);
2759 remote_watch_data_address = (CORE_ADDR)addr;
2760 }
2761 else
2762 {
2763 /* Silently skip unknown optional info. */
2764 p_temp = strchr (p1 + 1, ';');
2765 if (p_temp)
cfd77fa1 2766 p = p_temp;
3c3bea1c 2767 }
c906108c
SS
2768 }
2769 else
2770 {
ad10f812 2771 struct packet_reg *reg = packet_reg_from_pnum (rs, pnum);
c906108c
SS
2772 p = p1;
2773
2774 if (*p++ != ':')
8a3fe4f8
AC
2775 error (_("Malformed packet(b) (missing colon): %s\n\
2776Packet: '%s'\n"),
3fcb8548 2777 p, buf);
c906108c 2778
ad10f812 2779 if (reg == NULL)
8a3fe4f8
AC
2780 error (_("Remote sent bad register number %s: %s\n\
2781Packet: '%s'\n"),
3fcb8548 2782 phex_nz (pnum, 0), p, buf);
c906108c 2783
cfd77fa1 2784 fieldsize = hex2bin (p, regs,
23860348
MS
2785 register_size (current_gdbarch,
2786 reg->regnum));
97345198 2787 p += 2 * fieldsize;
23860348
MS
2788 if (fieldsize < register_size (current_gdbarch,
2789 reg->regnum))
8a3fe4f8 2790 warning (_("Remote reply is too short: %s"), buf);
23860348
MS
2791 regcache_raw_supply (current_regcache,
2792 reg->regnum, regs);
c906108c
SS
2793 }
2794
2795 if (*p++ != ';')
8a3fe4f8 2796 error (_("Remote register badly formatted: %s\nhere: %s"),
23860348 2797 buf, p);
c906108c
SS
2798 }
2799 }
2800 /* fall through */
23860348 2801 case 'S': /* Old style status, just signal only. */
c906108c
SS
2802 status->kind = TARGET_WAITKIND_STOPPED;
2803 status->value.sig = (enum target_signal)
2804 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
2805
0f71a2f6
JM
2806 if (buf[3] == 'p')
2807 {
0f71a2f6
JM
2808 thread_num = strtol ((const char *) &buf[4], NULL, 16);
2809 record_currthread (thread_num);
2810 }
c906108c 2811 goto got_status;
23860348 2812 case 'W': /* Target exited. */
c906108c
SS
2813 {
2814 /* The remote process exited. */
2815 status->kind = TARGET_WAITKIND_EXITED;
2816 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
2817 goto got_status;
2818 }
2819 case 'X':
2820 status->kind = TARGET_WAITKIND_SIGNALLED;
2821 status->value.sig = (enum target_signal)
2822 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
2823 kill_kludge = 1;
2824
2825 goto got_status;
23860348 2826 case 'O': /* Console output. */
c906108c
SS
2827 remote_console_output (buf + 1);
2828 continue;
2829 case '\0':
2830 if (last_sent_signal != TARGET_SIGNAL_0)
2831 {
2832 /* Zero length reply means that we tried 'S' or 'C' and
c5aa993b 2833 the remote system doesn't support it. */
c906108c
SS
2834 target_terminal_ours_for_output ();
2835 printf_filtered
2836 ("Can't send signals to this remote system. %s not sent.\n",
2837 target_signal_to_name (last_sent_signal));
2838 last_sent_signal = TARGET_SIGNAL_0;
2839 target_terminal_inferior ();
2840
2841 strcpy ((char *) buf, last_sent_step ? "s" : "c");
2842 putpkt ((char *) buf);
2843 continue;
2844 }
2845 /* else fallthrough */
2846 default:
8a3fe4f8 2847 warning (_("Invalid remote reply: %s"), buf);
c906108c
SS
2848 continue;
2849 }
2850 }
c5aa993b 2851got_status:
c906108c
SS
2852 if (thread_num != -1)
2853 {
39f77062 2854 return pid_to_ptid (thread_num);
c906108c 2855 }
39f77062 2856 return inferior_ptid;
c906108c
SS
2857}
2858
23860348 2859/* Async version of remote_wait. */
39f77062
KB
2860static ptid_t
2861remote_async_wait (ptid_t ptid, struct target_waitstatus *status)
43ff13b4 2862{
d01949b6 2863 struct remote_state *rs = get_remote_state ();
6d820c5c 2864 char *buf = rs->buf;
b2dd6311 2865 ULONGEST thread_num = -1;
3c3bea1c 2866 ULONGEST addr;
43ff13b4
JM
2867
2868 status->kind = TARGET_WAITKIND_EXITED;
2869 status->value.integer = 0;
2870
3c3bea1c
GS
2871 remote_stopped_by_watchpoint_p = 0;
2872
43ff13b4
JM
2873 while (1)
2874 {
cfd77fa1 2875 char *p;
c5aa993b 2876
ed9a39eb 2877 if (!target_is_async_p ())
43ff13b4 2878 ofunc = signal (SIGINT, remote_interrupt);
6426a772
JM
2879 /* FIXME: cagney/1999-09-27: If we're in async mode we should
2880 _never_ wait for ever -> test on target_is_async_p().
2881 However, before we do that we need to ensure that the caller
23860348 2882 knows how to take the target into/out of async mode. */
6d820c5c 2883 getpkt (&rs->buf, &rs->buf_size, wait_forever_enabled_p);
ed9a39eb 2884 if (!target_is_async_p ())
43ff13b4
JM
2885 signal (SIGINT, ofunc);
2886
2887 /* This is a hook for when we need to do something (perhaps the
c5aa993b 2888 collection of trace data) every time the target stops. */
9a4105ab
AC
2889 if (deprecated_target_wait_loop_hook)
2890 (*deprecated_target_wait_loop_hook) ();
43ff13b4
JM
2891
2892 switch (buf[0])
2893 {
23860348 2894 case 'E': /* Error of some sort. */
8a3fe4f8 2895 warning (_("Remote failure reply: %s"), buf);
43ff13b4 2896 continue;
23860348 2897 case 'F': /* File-I/O request. */
449092f6
CV
2898 remote_fileio_request (buf);
2899 continue;
23860348 2900 case 'T': /* Status with PC, SP, FP, ... */
43ff13b4 2901 {
cfd77fa1 2902 gdb_byte regs[MAX_REGISTER_SIZE];
43ff13b4 2903
23860348 2904 /* Expedited reply, containing Signal, {regno, reg} repeat. */
43ff13b4 2905 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
c5aa993b
JM
2906 ss = signal number
2907 n... = register number
2908 r... = register contents
2909 */
43ff13b4
JM
2910 p = &buf[3]; /* after Txx */
2911
2912 while (*p)
2913 {
cfd77fa1 2914 char *p1;
43ff13b4 2915 char *p_temp;
6c3f2dbf 2916 int fieldsize;
3c3bea1c 2917 long pnum = 0;
43ff13b4 2918
23860348
MS
2919 /* If the packet contains a register number, save it
2920 in pnum and set p1 to point to the character
2921 following it. Otherwise p1 points to p. */
3c3bea1c
GS
2922
2923 /* If this packet is an awatch packet, don't parse the 'a'
2924 as a register number. */
802188a7 2925
3c3bea1c
GS
2926 if (!strncmp (p, "awatch", strlen ("awatch")) != 0)
2927 {
2928 /* Read the register number. */
2929 pnum = strtol (p, &p_temp, 16);
cfd77fa1 2930 p1 = p_temp;
3c3bea1c 2931 }
802188a7 2932 else
3c3bea1c 2933 p1 = p;
43ff13b4 2934
23860348 2935 if (p1 == p) /* No register number present here. */
43ff13b4 2936 {
cfd77fa1 2937 p1 = strchr (p, ':');
43ff13b4 2938 if (p1 == NULL)
8a3fe4f8
AC
2939 error (_("Malformed packet(a) (missing colon): %s\n\
2940Packet: '%s'\n"),
3fcb8548 2941 p, buf);
3c3bea1c 2942 if (strncmp (p, "thread", p1 - p) == 0)
43ff13b4
JM
2943 {
2944 p_temp = unpack_varlen_hex (++p1, &thread_num);
2945 record_currthread (thread_num);
cfd77fa1 2946 p = p_temp;
43ff13b4 2947 }
3c3bea1c
GS
2948 else if ((strncmp (p, "watch", p1 - p) == 0)
2949 || (strncmp (p, "rwatch", p1 - p) == 0)
2950 || (strncmp (p, "awatch", p1 - p) == 0))
2951 {
2952 remote_stopped_by_watchpoint_p = 1;
2953 p = unpack_varlen_hex (++p1, &addr);
2954 remote_watch_data_address = (CORE_ADDR)addr;
2955 }
2956 else
2957 {
2958 /* Silently skip unknown optional info. */
cfd77fa1 2959 p_temp = strchr (p1 + 1, ';');
3c3bea1c
GS
2960 if (p_temp)
2961 p = p_temp;
2962 }
43ff13b4 2963 }
802188a7 2964
43ff13b4
JM
2965 else
2966 {
ad10f812 2967 struct packet_reg *reg = packet_reg_from_pnum (rs, pnum);
43ff13b4 2968 p = p1;
43ff13b4 2969 if (*p++ != ':')
8a3fe4f8
AC
2970 error (_("Malformed packet(b) (missing colon): %s\n\
2971Packet: '%s'\n"),
3fcb8548 2972 p, buf);
43ff13b4 2973
ad10f812 2974 if (reg == NULL)
8a3fe4f8
AC
2975 error (_("Remote sent bad register number %ld: %s\n\
2976Packet: '%s'\n"),
3fcb8548 2977 pnum, p, buf);
43ff13b4 2978
cfd77fa1 2979 fieldsize = hex2bin (p, regs,
23860348
MS
2980 register_size (current_gdbarch,
2981 reg->regnum));
6c3f2dbf 2982 p += 2 * fieldsize;
23860348
MS
2983 if (fieldsize < register_size (current_gdbarch,
2984 reg->regnum))
8a3fe4f8 2985 warning (_("Remote reply is too short: %s"), buf);
23a6d369 2986 regcache_raw_supply (current_regcache, reg->regnum, regs);
43ff13b4
JM
2987 }
2988
2989 if (*p++ != ';')
8a3fe4f8 2990 error (_("Remote register badly formatted: %s\nhere: %s"),
0a2cfde4 2991 buf, p);
43ff13b4
JM
2992 }
2993 }
2994 /* fall through */
23860348 2995 case 'S': /* Old style status, just signal only. */
43ff13b4
JM
2996 status->kind = TARGET_WAITKIND_STOPPED;
2997 status->value.sig = (enum target_signal)
2998 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
2999
3000 if (buf[3] == 'p')
3001 {
43ff13b4
JM
3002 thread_num = strtol ((const char *) &buf[4], NULL, 16);
3003 record_currthread (thread_num);
3004 }
43ff13b4 3005 goto got_status;
23860348 3006 case 'W': /* Target exited. */
43ff13b4
JM
3007 {
3008 /* The remote process exited. */
3009 status->kind = TARGET_WAITKIND_EXITED;
3010 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
3011 goto got_status;
3012 }
3013 case 'X':
3014 status->kind = TARGET_WAITKIND_SIGNALLED;
3015 status->value.sig = (enum target_signal)
3016 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
3017 kill_kludge = 1;
3018
3019 goto got_status;
23860348 3020 case 'O': /* Console output. */
43ff13b4 3021 remote_console_output (buf + 1);
c4093a6a 3022 /* Return immediately to the event loop. The event loop will
23860348 3023 still be waiting on the inferior afterwards. */
c4093a6a
JM
3024 status->kind = TARGET_WAITKIND_IGNORE;
3025 goto got_status;
43ff13b4
JM
3026 case '\0':
3027 if (last_sent_signal != TARGET_SIGNAL_0)
3028 {
3029 /* Zero length reply means that we tried 'S' or 'C' and
c5aa993b 3030 the remote system doesn't support it. */
43ff13b4
JM
3031 target_terminal_ours_for_output ();
3032 printf_filtered
3033 ("Can't send signals to this remote system. %s not sent.\n",
3034 target_signal_to_name (last_sent_signal));
3035 last_sent_signal = TARGET_SIGNAL_0;
3036 target_terminal_inferior ();
3037
3038 strcpy ((char *) buf, last_sent_step ? "s" : "c");
3039 putpkt ((char *) buf);
3040 continue;
3041 }
3042 /* else fallthrough */
3043 default:
8a3fe4f8 3044 warning (_("Invalid remote reply: %s"), buf);
43ff13b4
JM
3045 continue;
3046 }
3047 }
c5aa993b 3048got_status:
43ff13b4
JM
3049 if (thread_num != -1)
3050 {
39f77062 3051 return pid_to_ptid (thread_num);
43ff13b4 3052 }
39f77062 3053 return inferior_ptid;
43ff13b4
JM
3054}
3055
c906108c
SS
3056/* Number of bytes of registers this stub implements. */
3057
3058static int register_bytes_found;
3059
3060/* Read the remote registers into the block REGS. */
ad10f812 3061/* Currently we just read all the registers, so we don't use regnum. */
c906108c 3062
b96ec7ac
AC
3063static int
3064fetch_register_using_p (int regnum)
3065{
3066 struct remote_state *rs = get_remote_state ();
6d820c5c 3067 char *buf = rs->buf, *p;
b96ec7ac
AC
3068 char regp[MAX_REGISTER_SIZE];
3069 int i;
3070
fcad0fa4
JB
3071 p = buf;
3072 *p++ = 'p';
3073 p += hexnumstr (p, regnum);
3074 *p++ = '\0';
6d820c5c 3075 remote_send (&rs->buf, &rs->buf_size);
3f9a994c
JB
3076
3077 /* If the stub didn't recognize the packet, or if we got an error,
3078 tell our caller. */
3079 if (buf[0] == '\0' || buf[0] == 'E')
3080 return 0;
3081
3082 /* If this register is unfetchable, tell the regcache. */
3083 if (buf[0] == 'x')
8480adf2 3084 {
3f9a994c
JB
3085 regcache_raw_supply (current_regcache, regnum, NULL);
3086 set_register_cached (regnum, -1);
8480adf2 3087 return 1;
b96ec7ac 3088 }
b96ec7ac 3089
3f9a994c
JB
3090 /* Otherwise, parse and supply the value. */
3091 p = buf;
3092 i = 0;
3093 while (p[0] != 0)
3094 {
3095 if (p[1] == 0)
3096 {
8a3fe4f8 3097 error (_("fetch_register_using_p: early buf termination"));
3f9a994c
JB
3098 return 0;
3099 }
3100
3101 regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]);
3102 p += 2;
3103 }
3104 regcache_raw_supply (current_regcache, regnum, regp);
3105 return 1;
b96ec7ac
AC
3106}
3107
c906108c 3108static void
ad10f812 3109remote_fetch_registers (int regnum)
c906108c 3110{
d01949b6 3111 struct remote_state *rs = get_remote_state ();
6d820c5c 3112 char *buf = rs->buf;
c906108c
SS
3113 int i;
3114 char *p;
ad10f812 3115 char *regs = alloca (rs->sizeof_g_packet);
c906108c 3116
39f77062 3117 set_thread (PIDGET (inferior_ptid), 1);
c906108c 3118
b323314b
AC
3119 if (regnum >= 0)
3120 {
3121 struct packet_reg *reg = packet_reg_from_regnum (rs, regnum);
3122 gdb_assert (reg != NULL);
3123 if (!reg->in_g_packet)
3124 internal_error (__FILE__, __LINE__,
e2e0b3e5
AC
3125 _("Attempt to fetch a non G-packet register when this "
3126 "remote.c does not support the p-packet."));
b323314b 3127 }
444abaca 3128 switch (remote_protocol_packets[PACKET_p].support)
b96ec7ac
AC
3129 {
3130 case PACKET_DISABLE:
3131 break;
3132 case PACKET_ENABLE:
3133 if (fetch_register_using_p (regnum))
3134 return;
3135 else
8a3fe4f8 3136 error (_("Protocol error: p packet not recognized by stub"));
b96ec7ac
AC
3137 case PACKET_SUPPORT_UNKNOWN:
3138 if (fetch_register_using_p (regnum))
3139 {
3140 /* The stub recognized the 'p' packet. Remember this. */
444abaca 3141 remote_protocol_packets[PACKET_p].support = PACKET_ENABLE;
b96ec7ac
AC
3142 return;
3143 }
3144 else
3145 {
3146 /* The stub does not support the 'P' packet. Use 'G'
3147 instead, and don't try using 'P' in the future (it
3148 will just waste our time). */
444abaca 3149 remote_protocol_packets[PACKET_p].support = PACKET_DISABLE;
b96ec7ac
AC
3150 break;
3151 }
3152 }
b323314b 3153
c906108c 3154 sprintf (buf, "g");
6d820c5c 3155 remote_send (&rs->buf, &rs->buf_size);
c906108c 3156
11cf8741
JM
3157 /* Save the size of the packet sent to us by the target. Its used
3158 as a heuristic when determining the max size of packets that the
23860348 3159 target can safely receive. */
d01949b6
AC
3160 if ((rs->actual_register_packet_size) == 0)
3161 (rs->actual_register_packet_size) = strlen (buf);
c906108c
SS
3162
3163 /* Unimplemented registers read as all bits zero. */
ad10f812 3164 memset (regs, 0, rs->sizeof_g_packet);
c906108c
SS
3165
3166 /* We can get out of synch in various cases. If the first character
3167 in the buffer is not a hex character, assume that has happened
3168 and try to fetch another packet to read. */
3169 while ((buf[0] < '0' || buf[0] > '9')
3de11b2e 3170 && (buf[0] < 'A' || buf[0] > 'F')
c906108c 3171 && (buf[0] < 'a' || buf[0] > 'f')
23860348 3172 && buf[0] != 'x') /* New: unavailable register value. */
c906108c
SS
3173 {
3174 if (remote_debug)
0f71a2f6
JM
3175 fprintf_unfiltered (gdb_stdlog,
3176 "Bad register packet; fetching a new packet\n");
6d820c5c 3177 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c
SS
3178 }
3179
3180 /* Reply describes registers byte by byte, each byte encoded as two
3181 hex characters. Suck them all up, then supply them to the
3182 register cacheing/storage mechanism. */
3183
3184 p = buf;
ad10f812 3185 for (i = 0; i < rs->sizeof_g_packet; i++)
c906108c
SS
3186 {
3187 if (p[0] == 0)
3188 break;
3189 if (p[1] == 0)
3190 {
8a3fe4f8 3191 warning (_("Remote reply is of odd length: %s"), buf);
c906108c
SS
3192 /* Don't change register_bytes_found in this case, and don't
3193 print a second warning. */
3194 goto supply_them;
3195 }
3196 if (p[0] == 'x' && p[1] == 'x')
c5aa993b 3197 regs[i] = 0; /* 'x' */
c906108c
SS
3198 else
3199 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
3200 p += 2;
3201 }
3202
3203 if (i != register_bytes_found)
3204 {
3205 register_bytes_found = i;
2649061d
AC
3206 if (REGISTER_BYTES_OK_P ()
3207 && !REGISTER_BYTES_OK (i))
8a3fe4f8 3208 warning (_("Remote reply is too short: %s"), buf);
c906108c 3209 }
c5aa993b 3210
b323314b 3211 supply_them:
ad10f812 3212 {
b323314b
AC
3213 int i;
3214 for (i = 0; i < NUM_REGS + NUM_PSEUDO_REGS; i++)
ad10f812 3215 {
b323314b
AC
3216 struct packet_reg *r = &rs->regs[i];
3217 if (r->in_g_packet)
3218 {
8ccc1287
AC
3219 if (r->offset * 2 >= strlen (buf))
3220 /* A short packet that didn't include the register's
3221 value, this implies that the register is zero (and
3222 not that the register is unavailable). Supply that
3223 zero value. */
3224 regcache_raw_supply (current_regcache, r->regnum, NULL);
3225 else if (buf[r->offset * 2] == 'x')
3226 {
3227 gdb_assert (r->offset * 2 < strlen (buf));
3228 /* The register isn't available, mark it as such (at
3229 the same time setting the value to zero). */
3230 regcache_raw_supply (current_regcache, r->regnum, NULL);
3231 set_register_cached (i, -1);
3232 }
3233 else
3234 regcache_raw_supply (current_regcache, r->regnum,
3235 regs + r->offset);
b323314b 3236 }
ad10f812
AC
3237 }
3238 }
c906108c
SS
3239}
3240
3241/* Prepare to store registers. Since we may send them all (using a
3242 'G' request), we have to read out the ones we don't want to change
3243 first. */
3244
c5aa993b 3245static void
fba45db2 3246remote_prepare_to_store (void)
c906108c 3247{
cf0e1e0d
DJ
3248 struct remote_state *rs = get_remote_state ();
3249 int i;
cfd77fa1 3250 gdb_byte buf[MAX_REGISTER_SIZE];
cf0e1e0d 3251
c906108c 3252 /* Make sure the entire registers array is valid. */
444abaca 3253 switch (remote_protocol_packets[PACKET_P].support)
5a2468f5
JM
3254 {
3255 case PACKET_DISABLE:
3256 case PACKET_SUPPORT_UNKNOWN:
cf0e1e0d
DJ
3257 /* Make sure all the necessary registers are cached. */
3258 for (i = 0; i < NUM_REGS; i++)
3259 if (rs->regs[i].in_g_packet)
3260 regcache_raw_read (current_regcache, rs->regs[i].regnum, buf);
5a2468f5
JM
3261 break;
3262 case PACKET_ENABLE:
3263 break;
3264 }
3265}
3266
ad10f812 3267/* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
23860348 3268 packet was not recognized. */
5a2468f5
JM
3269
3270static int
ad10f812 3271store_register_using_P (int regnum)
5a2468f5 3272{
d01949b6 3273 struct remote_state *rs = get_remote_state ();
ad10f812 3274 struct packet_reg *reg = packet_reg_from_regnum (rs, regnum);
5a2468f5 3275 /* Try storing a single register. */
6d820c5c 3276 char *buf = rs->buf;
cfd77fa1 3277 gdb_byte regp[MAX_REGISTER_SIZE];
5a2468f5 3278 char *p;
5a2468f5 3279
ecbc58df 3280 xsnprintf (buf, rs->remote_packet_size, "P%s=", phex_nz (reg->pnum, 0));
5a2468f5 3281 p = buf + strlen (buf);
822c9732 3282 regcache_raw_collect (current_regcache, reg->regnum, regp);
3acba339 3283 bin2hex (regp, p, register_size (current_gdbarch, reg->regnum));
6d820c5c 3284 remote_send (&rs->buf, &rs->buf_size);
5a2468f5
JM
3285
3286 return buf[0] != '\0';
c906108c
SS
3287}
3288
5a2468f5 3289
23860348
MS
3290/* Store register REGNUM, or all registers if REGNUM == -1, from the
3291 contents of the register cache buffer. FIXME: ignores errors. */
c906108c
SS
3292
3293static void
ad10f812 3294remote_store_registers (int regnum)
c906108c 3295{
d01949b6 3296 struct remote_state *rs = get_remote_state ();
cfd77fa1 3297 gdb_byte *regs;
c906108c
SS
3298 char *p;
3299
39f77062 3300 set_thread (PIDGET (inferior_ptid), 1);
c906108c 3301
ad10f812 3302 if (regnum >= 0)
c906108c 3303 {
444abaca 3304 switch (remote_protocol_packets[PACKET_P].support)
c906108c 3305 {
5a2468f5
JM
3306 case PACKET_DISABLE:
3307 break;
3308 case PACKET_ENABLE:
ad10f812 3309 if (store_register_using_P (regnum))
5a2468f5
JM
3310 return;
3311 else
8a3fe4f8 3312 error (_("Protocol error: P packet not recognized by stub"));
5a2468f5 3313 case PACKET_SUPPORT_UNKNOWN:
ad10f812 3314 if (store_register_using_P (regnum))
5a2468f5
JM
3315 {
3316 /* The stub recognized the 'P' packet. Remember this. */
444abaca 3317 remote_protocol_packets[PACKET_P].support = PACKET_ENABLE;
5a2468f5
JM
3318 return;
3319 }
3320 else
3321 {
3322 /* The stub does not support the 'P' packet. Use 'G'
3323 instead, and don't try using 'P' in the future (it
3324 will just waste our time). */
444abaca 3325 remote_protocol_packets[PACKET_P].support = PACKET_DISABLE;
5a2468f5
JM
3326 break;
3327 }
c906108c 3328 }
c906108c
SS
3329 }
3330
193cb69f
AC
3331 /* Extract all the registers in the regcache copying them into a
3332 local buffer. */
3333 {
b323314b 3334 int i;
ad10f812 3335 regs = alloca (rs->sizeof_g_packet);
170cd118 3336 memset (regs, 0, rs->sizeof_g_packet);
b323314b 3337 for (i = 0; i < NUM_REGS + NUM_PSEUDO_REGS; i++)
193cb69f 3338 {
b323314b
AC
3339 struct packet_reg *r = &rs->regs[i];
3340 if (r->in_g_packet)
822c9732 3341 regcache_raw_collect (current_regcache, r->regnum, regs + r->offset);
193cb69f
AC
3342 }
3343 }
c906108c
SS
3344
3345 /* Command describes registers byte by byte,
3346 each byte encoded as two hex characters. */
6d820c5c 3347 p = rs->buf;
193cb69f 3348 *p++ = 'G';
c906108c 3349 /* remote_prepare_to_store insures that register_bytes_found gets set. */
30559e10 3350 bin2hex (regs, p, register_bytes_found);
6d820c5c 3351 remote_send (&rs->buf, &rs->buf_size);
c906108c 3352}
c906108c
SS
3353\f
3354
3355/* Return the number of hex digits in num. */
3356
3357static int
fba45db2 3358hexnumlen (ULONGEST num)
c906108c
SS
3359{
3360 int i;
3361
3362 for (i = 0; num != 0; i++)
3363 num >>= 4;
3364
3365 return max (i, 1);
3366}
3367
2df3850c 3368/* Set BUF to the minimum number of hex digits representing NUM. */
c906108c
SS
3369
3370static int
fba45db2 3371hexnumstr (char *buf, ULONGEST num)
c906108c 3372{
c906108c 3373 int len = hexnumlen (num);
2df3850c
JM
3374 return hexnumnstr (buf, num, len);
3375}
3376
c906108c 3377
2df3850c 3378/* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
c906108c 3379
2df3850c 3380static int
fba45db2 3381hexnumnstr (char *buf, ULONGEST num, int width)
2df3850c
JM
3382{
3383 int i;
3384
3385 buf[width] = '\0';
3386
3387 for (i = width - 1; i >= 0; i--)
c906108c 3388 {
c5aa993b 3389 buf[i] = "0123456789abcdef"[(num & 0xf)];
c906108c
SS
3390 num >>= 4;
3391 }
3392
2df3850c 3393 return width;
c906108c
SS
3394}
3395
23860348 3396/* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
c906108c
SS
3397
3398static CORE_ADDR
fba45db2 3399remote_address_masked (CORE_ADDR addr)
c906108c
SS
3400{
3401 if (remote_address_size > 0
3402 && remote_address_size < (sizeof (ULONGEST) * 8))
3403 {
3404 /* Only create a mask when that mask can safely be constructed
23860348 3405 in a ULONGEST variable. */
c906108c
SS
3406 ULONGEST mask = 1;
3407 mask = (mask << remote_address_size) - 1;
3408 addr &= mask;
3409 }
3410 return addr;
3411}
3412
3413/* Determine whether the remote target supports binary downloading.
3414 This is accomplished by sending a no-op memory write of zero length
3415 to the target at the specified address. It does not suffice to send
23860348
MS
3416 the whole packet, since many stubs strip the eighth bit and
3417 subsequently compute a wrong checksum, which causes real havoc with
3418 remote_write_bytes.
7a292a7a 3419
96baa820
JM
3420 NOTE: This can still lose if the serial line is not eight-bit
3421 clean. In cases like this, the user should clear "remote
23860348 3422 X-packet". */
96baa820 3423
c906108c 3424static void
fba45db2 3425check_binary_download (CORE_ADDR addr)
c906108c 3426{
d01949b6 3427 struct remote_state *rs = get_remote_state ();
24b06219 3428
444abaca 3429 switch (remote_protocol_packets[PACKET_X].support)
c906108c 3430 {
96baa820
JM
3431 case PACKET_DISABLE:
3432 break;
3433 case PACKET_ENABLE:
3434 break;
3435 case PACKET_SUPPORT_UNKNOWN:
3436 {
6d820c5c 3437 char *buf = rs->buf;
96baa820 3438 char *p;
802188a7 3439
96baa820
JM
3440 p = buf;
3441 *p++ = 'X';
3442 p += hexnumstr (p, (ULONGEST) addr);
3443 *p++ = ',';
3444 p += hexnumstr (p, (ULONGEST) 0);
3445 *p++ = ':';
3446 *p = '\0';
802188a7 3447
96baa820 3448 putpkt_binary (buf, (int) (p - buf));
6d820c5c 3449 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 3450
96baa820
JM
3451 if (buf[0] == '\0')
3452 {
3453 if (remote_debug)
3454 fprintf_unfiltered (gdb_stdlog,
3455 "binary downloading NOT suppported by target\n");
444abaca 3456 remote_protocol_packets[PACKET_X].support = PACKET_DISABLE;
96baa820
JM
3457 }
3458 else
3459 {
3460 if (remote_debug)
3461 fprintf_unfiltered (gdb_stdlog,
3462 "binary downloading suppported by target\n");
444abaca 3463 remote_protocol_packets[PACKET_X].support = PACKET_ENABLE;
96baa820
JM
3464 }
3465 break;
3466 }
c906108c
SS
3467 }
3468}
3469
3470/* Write memory data directly to the remote machine.
3471 This does not inform the data cache; the data cache uses this.
3472 MEMADDR is the address in the remote memory space.
3473 MYADDR is the address of the buffer in our space.
3474 LEN is the number of bytes.
3475
917317f4 3476 Returns number of bytes transferred, or 0 (setting errno) for
23860348 3477 error. Only transfer a single packet. */
c906108c 3478
449092f6 3479int
cfd77fa1 3480remote_write_bytes (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
c906108c 3481{
6d820c5c 3482 struct remote_state *rs = get_remote_state ();
cfd77fa1
DJ
3483 char *buf;
3484 char *p;
3485 char *plen;
917317f4
JM
3486 int plenlen;
3487 int todo;
3488 int nr_bytes;
a257b5bb 3489 int payload_size;
cfd77fa1 3490 char *payload_start;
c906108c 3491
a257b5bb 3492 /* Verify that the target can support a binary download. */
c906108c
SS
3493 check_binary_download (memaddr);
3494
3de11b2e
NS
3495 payload_size = get_memory_write_packet_size ();
3496
6d820c5c
DJ
3497 /* The packet buffer will be large enough for the payload;
3498 get_memory_packet_size ensures this. */
3499 buf = rs->buf;
c906108c 3500
a257b5bb 3501 /* Compute the size of the actual payload by subtracting out the
3de11b2e
NS
3502 packet header and footer overhead: "$M<memaddr>,<len>:...#nn".
3503 */
3504 payload_size -= strlen ("$M,:#NN");
3505 payload_size -= hexnumlen (memaddr);
c906108c 3506
a257b5bb 3507 /* Construct the packet header: "[MX]<memaddr>,<len>:". */
917317f4 3508
a257b5bb 3509 /* Append "[XM]". Compute a best guess of the number of bytes
23860348 3510 actually transfered. */
a257b5bb 3511 p = buf;
444abaca 3512 switch (remote_protocol_packets[PACKET_X].support)
c906108c 3513 {
917317f4
JM
3514 case PACKET_ENABLE:
3515 *p++ = 'X';
23860348 3516 /* Best guess at number of bytes that will fit. */
a257b5bb 3517 todo = min (len, payload_size);
3de11b2e
NS
3518 payload_size -= hexnumlen (todo);
3519 todo = min (todo, payload_size);
917317f4
JM
3520 break;
3521 case PACKET_DISABLE:
3522 *p++ = 'M';
23860348 3523 /* Num bytes that will fit. */
a257b5bb 3524 todo = min (len, payload_size / 2);
3de11b2e
NS
3525 payload_size -= hexnumlen (todo);
3526 todo = min (todo, payload_size / 2);
917317f4
JM
3527 break;
3528 case PACKET_SUPPORT_UNKNOWN:
8e65ff28 3529 internal_error (__FILE__, __LINE__,
e2e0b3e5 3530 _("remote_write_bytes: bad internal state"));
7f7e9482 3531 default:
e2e0b3e5 3532 internal_error (__FILE__, __LINE__, _("bad switch"));
917317f4 3533 }
3de11b2e
NS
3534 if (todo <= 0)
3535 internal_error (__FILE__, __LINE__,
3536 _("minumum packet size too small to write data"));
802188a7 3537
a257b5bb 3538 /* Append "<memaddr>". */
917317f4
JM
3539 memaddr = remote_address_masked (memaddr);
3540 p += hexnumstr (p, (ULONGEST) memaddr);
a257b5bb
AC
3541
3542 /* Append ",". */
917317f4 3543 *p++ = ',';
802188a7 3544
a257b5bb
AC
3545 /* Append <len>. Retain the location/size of <len>. It may need to
3546 be adjusted once the packet body has been created. */
917317f4
JM
3547 plen = p;
3548 plenlen = hexnumstr (p, (ULONGEST) todo);
3549 p += plenlen;
a257b5bb
AC
3550
3551 /* Append ":". */
917317f4
JM
3552 *p++ = ':';
3553 *p = '\0';
802188a7 3554
a257b5bb
AC
3555 /* Append the packet body. */
3556 payload_start = p;
444abaca 3557 switch (remote_protocol_packets[PACKET_X].support)
917317f4
JM
3558 {
3559 case PACKET_ENABLE:
3560 /* Binary mode. Send target system values byte by byte, in
3561 increasing byte addresses. Only escape certain critical
3562 characters. */
3563 for (nr_bytes = 0;
a257b5bb 3564 (nr_bytes < todo) && (p - payload_start) < payload_size;
917317f4 3565 nr_bytes++)
c906108c 3566 {
917317f4
JM
3567 switch (myaddr[nr_bytes] & 0xff)
3568 {
3569 case '$':
3570 case '#':
3571 case 0x7d:
23860348 3572 /* These must be escaped. */
917317f4
JM
3573 *p++ = 0x7d;
3574 *p++ = (myaddr[nr_bytes] & 0xff) ^ 0x20;
3575 break;
3576 default:
3577 *p++ = myaddr[nr_bytes] & 0xff;
3578 break;
3579 }
c906108c 3580 }
917317f4 3581 if (nr_bytes < todo)
c906108c 3582 {
802188a7 3583 /* Escape chars have filled up the buffer prematurely,
917317f4
JM
3584 and we have actually sent fewer bytes than planned.
3585 Fix-up the length field of the packet. Use the same
3586 number of characters as before. */
917317f4
JM
3587 plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen);
3588 *plen = ':'; /* overwrite \0 from hexnumnstr() */
c906108c 3589 }
917317f4
JM
3590 break;
3591 case PACKET_DISABLE:
3592 /* Normal mode: Send target system values byte by byte, in
3593 increasing byte addresses. Each byte is encoded as a two hex
3594 value. */
2644f393 3595 nr_bytes = bin2hex (myaddr, p, todo);
aa6c0017 3596 p += 2 * nr_bytes;
917317f4
JM
3597 break;
3598 case PACKET_SUPPORT_UNKNOWN:
8e65ff28 3599 internal_error (__FILE__, __LINE__,
e2e0b3e5 3600 _("remote_write_bytes: bad internal state"));
7f7e9482 3601 default:
e2e0b3e5 3602 internal_error (__FILE__, __LINE__, _("bad switch"));
c906108c 3603 }
802188a7 3604
917317f4 3605 putpkt_binary (buf, (int) (p - buf));
6d820c5c 3606 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 3607
917317f4
JM
3608 if (buf[0] == 'E')
3609 {
3610 /* There is no correspondance between what the remote protocol
3611 uses for errors and errno codes. We would like a cleaner way
3612 of representing errors (big enough to include errno codes,
3613 bfd_error codes, and others). But for now just return EIO. */
3614 errno = EIO;
3615 return 0;
3616 }
802188a7 3617
23860348
MS
3618 /* Return NR_BYTES, not TODO, in case escape chars caused us to send
3619 fewer bytes than we'd planned. */
917317f4 3620 return nr_bytes;
c906108c
SS
3621}
3622
3623/* Read memory data directly from the remote machine.
3624 This does not use the data cache; the data cache uses this.
3625 MEMADDR is the address in the remote memory space.
3626 MYADDR is the address of the buffer in our space.
3627 LEN is the number of bytes.
3628
3629 Returns number of bytes transferred, or 0 for error. */
3630
917317f4
JM
3631/* NOTE: cagney/1999-10-18: This function (and its siblings in other
3632 remote targets) shouldn't attempt to read the entire buffer.
3633 Instead it should read a single packet worth of data and then
3634 return the byte size of that packet to the caller. The caller (its
3635 caller and its callers caller ;-) already contains code for
23860348 3636 handling partial reads. */
917317f4 3637
449092f6 3638int
cfd77fa1 3639remote_read_bytes (CORE_ADDR memaddr, gdb_byte *myaddr, int len)
c906108c 3640{
6d820c5c 3641 struct remote_state *rs = get_remote_state ();
11cf8741 3642 char *buf;
23860348 3643 int max_buf_size; /* Max size of packet output buffer. */
c906108c
SS
3644 int origlen;
3645
11cf8741 3646 max_buf_size = get_memory_read_packet_size ();
6d820c5c
DJ
3647 /* The packet buffer will be large enough for the payload;
3648 get_memory_packet_size ensures this. */
3649 buf = rs->buf;
c906108c
SS
3650
3651 origlen = len;
3652 while (len > 0)
3653 {
c906108c
SS
3654 char *p;
3655 int todo;
3656 int i;
3657
c5aa993b 3658 todo = min (len, max_buf_size / 2); /* num bytes that will fit */
c906108c
SS
3659
3660 /* construct "m"<memaddr>","<len>" */
3661 /* sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo); */
3662 memaddr = remote_address_masked (memaddr);
3663 p = buf;
3664 *p++ = 'm';
3665 p += hexnumstr (p, (ULONGEST) memaddr);
3666 *p++ = ',';
3667 p += hexnumstr (p, (ULONGEST) todo);
3668 *p = '\0';
3669
3670 putpkt (buf);
6d820c5c 3671 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 3672
66504d44
MS
3673 if (buf[0] == 'E'
3674 && isxdigit (buf[1]) && isxdigit (buf[2])
3675 && buf[3] == '\0')
c906108c 3676 {
23860348
MS
3677 /* There is no correspondance between what the remote
3678 protocol uses for errors and errno codes. We would like
3679 a cleaner way of representing errors (big enough to
3680 include errno codes, bfd_error codes, and others). But
3681 for now just return EIO. */
c906108c
SS
3682 errno = EIO;
3683 return 0;
3684 }
3685
c5aa993b
JM
3686 /* Reply describes memory byte by byte,
3687 each byte encoded as two hex characters. */
c906108c
SS
3688
3689 p = buf;
30559e10 3690 if ((i = hex2bin (p, myaddr, todo)) < todo)
c906108c 3691 {
30559e10 3692 /* Reply is short. This means that we were able to read
23860348 3693 only part of what we wanted to. */
30559e10 3694 return i + (origlen - len);
c906108c
SS
3695 }
3696 myaddr += todo;
3697 memaddr += todo;
3698 len -= todo;
3699 }
3700 return origlen;
3701}
3702\f
3703/* Read or write LEN bytes from inferior memory at MEMADDR,
23860348
MS
3704 transferring to or from debugger address BUFFER. Write to inferior
3705 if SHOULD_WRITE is nonzero. Returns length of data written or
3706 read; 0 for error. TARGET is unused. */
392a587b 3707
c906108c 3708static int
961cb7b5 3709remote_xfer_memory (CORE_ADDR mem_addr, gdb_byte *buffer, int mem_len,
0a65a603 3710 int should_write, struct mem_attrib *attrib,
29e57380 3711 struct target_ops *target)
c906108c 3712{
392a587b
JM
3713 CORE_ADDR targ_addr;
3714 int targ_len;
4930751a
C
3715 int res;
3716
f6684c31 3717 /* Should this be the selected frame? */
23860348
MS
3718 gdbarch_remote_translate_xfer_address (current_gdbarch,
3719 current_regcache,
f6684c31
AC
3720 mem_addr, mem_len,
3721 &targ_addr, &targ_len);
392a587b 3722 if (targ_len <= 0)
c906108c 3723 return 0;
c906108c 3724
4930751a
C
3725 if (should_write)
3726 res = remote_write_bytes (targ_addr, buffer, targ_len);
3727 else
3728 res = remote_read_bytes (targ_addr, buffer, targ_len);
3729
3730 return res;
c906108c
SS
3731}
3732
c906108c 3733static void
fba45db2 3734remote_files_info (struct target_ops *ignore)
c906108c
SS
3735{
3736 puts_filtered ("Debugging a target over a serial line.\n");
3737}
3738\f
3739/* Stuff for dealing with the packets which are part of this protocol.
3740 See comment at top of file for details. */
3741
23860348
MS
3742/* Read a single character from the remote end, masking it down to 7
3743 bits. */
c906108c
SS
3744
3745static int
fba45db2 3746readchar (int timeout)
c906108c
SS
3747{
3748 int ch;
3749
2cd58942 3750 ch = serial_readchar (remote_desc, timeout);
c906108c 3751
2acceee2
JM
3752 if (ch >= 0)
3753 return (ch & 0x7f);
3754
3755 switch ((enum serial_rc) ch)
c906108c
SS
3756 {
3757 case SERIAL_EOF:
2acceee2 3758 target_mourn_inferior ();
8a3fe4f8 3759 error (_("Remote connection closed"));
2acceee2 3760 /* no return */
c906108c 3761 case SERIAL_ERROR:
e2e0b3e5 3762 perror_with_name (_("Remote communication error"));
2acceee2 3763 /* no return */
c906108c 3764 case SERIAL_TIMEOUT:
2acceee2 3765 break;
c906108c 3766 }
2acceee2 3767 return ch;
c906108c
SS
3768}
3769
6d820c5c
DJ
3770/* Send the command in *BUF to the remote machine, and read the reply
3771 into *BUF. Report an error if we get an error reply. Resize
3772 *BUF using xrealloc if necessary to hold the result, and update
3773 *SIZEOF_BUF. */
c906108c
SS
3774
3775static void
6d820c5c
DJ
3776remote_send (char **buf,
3777 long *sizeof_buf)
c906108c 3778{
6d820c5c 3779 putpkt (*buf);
c2d11a7d 3780 getpkt (buf, sizeof_buf, 0);
c906108c 3781
6d820c5c
DJ
3782 if ((*buf)[0] == 'E')
3783 error (_("Remote failure reply: %s"), *buf);
c906108c
SS
3784}
3785
3786/* Display a null-terminated packet on stdout, for debugging, using C
3787 string notation. */
3788
3789static void
fba45db2 3790print_packet (char *buf)
c906108c
SS
3791{
3792 puts_filtered ("\"");
43e526b9 3793 fputstr_filtered (buf, '"', gdb_stdout);
c906108c
SS
3794 puts_filtered ("\"");
3795}
3796
3797int
fba45db2 3798putpkt (char *buf)
c906108c
SS
3799{
3800 return putpkt_binary (buf, strlen (buf));
3801}
3802
3803/* Send a packet to the remote machine, with error checking. The data
23860348 3804 of the packet is in BUF. The string in BUF can be at most
24b06219 3805 RS->remote_packet_size - 5 to account for the $, # and checksum,
23860348
MS
3806 and for a possible /0 if we are debugging (remote_debug) and want
3807 to print the sent packet as a string. */
c906108c
SS
3808
3809static int
fba45db2 3810putpkt_binary (char *buf, int cnt)
c906108c 3811{
d01949b6 3812 struct remote_state *rs = get_remote_state ();
c906108c
SS
3813 int i;
3814 unsigned char csum = 0;
11cf8741 3815 char *buf2 = alloca (cnt + 6);
085dd6e6 3816
c906108c
SS
3817 int ch;
3818 int tcount = 0;
3819 char *p;
3820
3821 /* Copy the packet into buffer BUF2, encapsulating it
3822 and giving it a checksum. */
3823
c906108c
SS
3824 p = buf2;
3825 *p++ = '$';
3826
3827 for (i = 0; i < cnt; i++)
3828 {
3829 csum += buf[i];
3830 *p++ = buf[i];
3831 }
3832 *p++ = '#';
3833 *p++ = tohex ((csum >> 4) & 0xf);
3834 *p++ = tohex (csum & 0xf);
3835
3836 /* Send it over and over until we get a positive ack. */
3837
3838 while (1)
3839 {
3840 int started_error_output = 0;
3841
3842 if (remote_debug)
3843 {
3844 *p = '\0';
43e526b9
JM
3845 fprintf_unfiltered (gdb_stdlog, "Sending packet: ");
3846 fputstrn_unfiltered (buf2, p - buf2, 0, gdb_stdlog);
d4f3574e 3847 fprintf_unfiltered (gdb_stdlog, "...");
0f71a2f6 3848 gdb_flush (gdb_stdlog);
c906108c 3849 }
2cd58942 3850 if (serial_write (remote_desc, buf2, p - buf2))
e2e0b3e5 3851 perror_with_name (_("putpkt: write failed"));
c906108c 3852
23860348 3853 /* Read until either a timeout occurs (-2) or '+' is read. */
c906108c
SS
3854 while (1)
3855 {
3856 ch = readchar (remote_timeout);
3857
c5aa993b 3858 if (remote_debug)
c906108c
SS
3859 {
3860 switch (ch)
3861 {
3862 case '+':
1216fa2c 3863 case '-':
c906108c
SS
3864 case SERIAL_TIMEOUT:
3865 case '$':
3866 if (started_error_output)
3867 {
3868 putchar_unfiltered ('\n');
3869 started_error_output = 0;
3870 }
3871 }
3872 }
3873
3874 switch (ch)
3875 {
3876 case '+':
3877 if (remote_debug)
0f71a2f6 3878 fprintf_unfiltered (gdb_stdlog, "Ack\n");
c906108c 3879 return 1;
1216fa2c
AC
3880 case '-':
3881 if (remote_debug)
3882 fprintf_unfiltered (gdb_stdlog, "Nak\n");
c906108c 3883 case SERIAL_TIMEOUT:
c5aa993b 3884 tcount++;
c906108c
SS
3885 if (tcount > 3)
3886 return 0;
23860348 3887 break; /* Retransmit buffer. */
c906108c
SS
3888 case '$':
3889 {
40e3f985 3890 if (remote_debug)
23860348
MS
3891 fprintf_unfiltered (gdb_stdlog,
3892 "Packet instead of Ack, ignoring it\n");
d6f7abdf
AC
3893 /* It's probably an old response sent because an ACK
3894 was lost. Gobble up the packet and ack it so it
3895 doesn't get retransmitted when we resend this
3896 packet. */
6d820c5c 3897 skip_frame ();
d6f7abdf 3898 serial_write (remote_desc, "+", 1);
23860348 3899 continue; /* Now, go look for +. */
c906108c
SS
3900 }
3901 default:
3902 if (remote_debug)
3903 {
3904 if (!started_error_output)
3905 {
3906 started_error_output = 1;
0f71a2f6 3907 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
c906108c 3908 }
0f71a2f6 3909 fputc_unfiltered (ch & 0177, gdb_stdlog);
c906108c
SS
3910 }
3911 continue;
3912 }
23860348 3913 break; /* Here to retransmit. */
c906108c
SS
3914 }
3915
3916#if 0
3917 /* This is wrong. If doing a long backtrace, the user should be
c5aa993b
JM
3918 able to get out next time we call QUIT, without anything as
3919 violent as interrupt_query. If we want to provide a way out of
3920 here without getting to the next QUIT, it should be based on
3921 hitting ^C twice as in remote_wait. */
c906108c
SS
3922 if (quit_flag)
3923 {
3924 quit_flag = 0;
3925 interrupt_query ();
3926 }
3927#endif
3928 }
3929}
3930
6d820c5c
DJ
3931/* Come here after finding the start of a frame when we expected an
3932 ack. Do our best to discard the rest of this packet. */
3933
3934static void
3935skip_frame (void)
3936{
3937 int c;
3938
3939 while (1)
3940 {
3941 c = readchar (remote_timeout);
3942 switch (c)
3943 {
3944 case SERIAL_TIMEOUT:
3945 /* Nothing we can do. */
3946 return;
3947 case '#':
3948 /* Discard the two bytes of checksum and stop. */
3949 c = readchar (remote_timeout);
3950 if (c >= 0)
3951 c = readchar (remote_timeout);
3952
3953 return;
3954 case '*': /* Run length encoding. */
3955 /* Discard the repeat count. */
3956 c = readchar (remote_timeout);
3957 if (c < 0)
3958 return;
3959 break;
3960 default:
3961 /* A regular character. */
3962 break;
3963 }
3964 }
3965}
3966
c906108c 3967/* Come here after finding the start of the frame. Collect the rest
6d820c5c
DJ
3968 into *BUF, verifying the checksum, length, and handling run-length
3969 compression. NUL terminate the buffer. If there is not enough room,
3970 expand *BUF using xrealloc.
c906108c 3971
c2d11a7d
JM
3972 Returns -1 on error, number of characters in buffer (ignoring the
3973 trailing NULL) on success. (could be extended to return one of the
23860348 3974 SERIAL status indications). */
c2d11a7d
JM
3975
3976static long
6d820c5c
DJ
3977read_frame (char **buf_p,
3978 long *sizeof_buf)
c906108c
SS
3979{
3980 unsigned char csum;
c2d11a7d 3981 long bc;
c906108c 3982 int c;
6d820c5c 3983 char *buf = *buf_p;
c906108c
SS
3984
3985 csum = 0;
c2d11a7d 3986 bc = 0;
c906108c
SS
3987
3988 while (1)
3989 {
3990 c = readchar (remote_timeout);
c906108c
SS
3991 switch (c)
3992 {
3993 case SERIAL_TIMEOUT:
3994 if (remote_debug)
0f71a2f6 3995 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
c2d11a7d 3996 return -1;
c906108c
SS
3997 case '$':
3998 if (remote_debug)
0f71a2f6
JM
3999 fputs_filtered ("Saw new packet start in middle of old one\n",
4000 gdb_stdlog);
23860348 4001 return -1; /* Start a new packet, count retries. */
c906108c
SS
4002 case '#':
4003 {
4004 unsigned char pktcsum;
e1b09194
AC
4005 int check_0 = 0;
4006 int check_1 = 0;
c906108c 4007
c2d11a7d 4008 buf[bc] = '\0';
c906108c 4009
e1b09194
AC
4010 check_0 = readchar (remote_timeout);
4011 if (check_0 >= 0)
4012 check_1 = readchar (remote_timeout);
802188a7 4013
e1b09194
AC
4014 if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
4015 {
4016 if (remote_debug)
23860348
MS
4017 fputs_filtered ("Timeout in checksum, retrying\n",
4018 gdb_stdlog);
e1b09194
AC
4019 return -1;
4020 }
4021 else if (check_0 < 0 || check_1 < 0)
40e3f985
FN
4022 {
4023 if (remote_debug)
23860348
MS
4024 fputs_filtered ("Communication error in checksum\n",
4025 gdb_stdlog);
40e3f985
FN
4026 return -1;
4027 }
c906108c 4028
e1b09194 4029 pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
c906108c 4030 if (csum == pktcsum)
c2d11a7d 4031 return bc;
c906108c 4032
c5aa993b 4033 if (remote_debug)
c906108c 4034 {
0f71a2f6 4035 fprintf_filtered (gdb_stdlog,
c5aa993b 4036 "Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
0f71a2f6
JM
4037 pktcsum, csum);
4038 fputs_filtered (buf, gdb_stdlog);
4039 fputs_filtered ("\n", gdb_stdlog);
c906108c 4040 }
c2d11a7d 4041 /* Number of characters in buffer ignoring trailing
23860348 4042 NULL. */
c2d11a7d 4043 return -1;
c906108c 4044 }
23860348 4045 case '*': /* Run length encoding. */
c2c6d25f
JM
4046 {
4047 int repeat;
4048 csum += c;
c906108c 4049
b4501125
AC
4050 c = readchar (remote_timeout);
4051 csum += c;
23860348 4052 repeat = c - ' ' + 3; /* Compute repeat count. */
c906108c 4053
23860348 4054 /* The character before ``*'' is repeated. */
c2d11a7d 4055
6d820c5c 4056 if (repeat > 0 && repeat <= 255 && bc > 0)
c2c6d25f 4057 {
6d820c5c
DJ
4058 if (bc + repeat - 1 >= *sizeof_buf - 1)
4059 {
4060 /* Make some more room in the buffer. */
4061 *sizeof_buf += repeat;
4062 *buf_p = xrealloc (*buf_p, *sizeof_buf);
4063 buf = *buf_p;
4064 }
4065
c2d11a7d
JM
4066 memset (&buf[bc], buf[bc - 1], repeat);
4067 bc += repeat;
c2c6d25f
JM
4068 continue;
4069 }
4070
c2d11a7d 4071 buf[bc] = '\0';
6d820c5c 4072 printf_filtered (_("Invalid run length encoding: %s\n"), buf);
c2d11a7d 4073 return -1;
c2c6d25f 4074 }
c906108c 4075 default:
6d820c5c 4076 if (bc >= *sizeof_buf - 1)
c906108c 4077 {
6d820c5c
DJ
4078 /* Make some more room in the buffer. */
4079 *sizeof_buf *= 2;
4080 *buf_p = xrealloc (*buf_p, *sizeof_buf);
4081 buf = *buf_p;
c906108c
SS
4082 }
4083
6d820c5c
DJ
4084 buf[bc++] = c;
4085 csum += c;
4086 continue;
c906108c
SS
4087 }
4088 }
4089}
4090
4091/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
4092 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
4093 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
4094 rather than timing out; this is used (in synchronous mode) to wait
4095 for a target that is is executing user code to stop. */
d9fcf2fb
JM
4096/* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
4097 don't have to change all the calls to getpkt to deal with the
4098 return value, because at the moment I don't know what the right
23860348 4099 thing to do it for those. */
c906108c 4100void
6d820c5c
DJ
4101getpkt (char **buf,
4102 long *sizeof_buf,
c2d11a7d 4103 int forever)
d9fcf2fb
JM
4104{
4105 int timed_out;
4106
4107 timed_out = getpkt_sane (buf, sizeof_buf, forever);
4108}
4109
4110
4111/* Read a packet from the remote machine, with error checking, and
6d820c5c
DJ
4112 store it in *BUF. Resize *BUF using xrealloc if necessary to hold
4113 the result, and update *SIZEOF_BUF. If FOREVER, wait forever
4114 rather than timing out; this is used (in synchronous mode) to wait
4115 for a target that is is executing user code to stop. If FOREVER ==
4116 0, this function is allowed to time out gracefully and return an
4117 indication of this to the caller. */
3172dc30 4118static int
6d820c5c 4119getpkt_sane (char **buf, long *sizeof_buf, int forever)
c906108c
SS
4120{
4121 int c;
4122 int tries;
4123 int timeout;
4124 int val;
4125
6d820c5c 4126 strcpy (*buf, "timeout");
c906108c
SS
4127
4128 if (forever)
4129 {
c906108c 4130 timeout = watchdog > 0 ? watchdog : -1;
c906108c
SS
4131 }
4132
4133 else
4134 timeout = remote_timeout;
4135
4136#define MAX_TRIES 3
4137
4138 for (tries = 1; tries <= MAX_TRIES; tries++)
4139 {
4140 /* This can loop forever if the remote side sends us characters
23860348
MS
4141 continuously, but if it pauses, we'll get a zero from
4142 readchar because of timeout. Then we'll count that as a
4143 retry. */
c906108c 4144
23860348
MS
4145 /* Note that we will only wait forever prior to the start of a
4146 packet. After that, we expect characters to arrive at a
4147 brisk pace. They should show up within remote_timeout
4148 intervals. */
c906108c
SS
4149
4150 do
4151 {
4152 c = readchar (timeout);
4153
4154 if (c == SERIAL_TIMEOUT)
4155 {
23860348 4156 if (forever) /* Watchdog went off? Kill the target. */
c906108c 4157 {
2acceee2 4158 QUIT;
c906108c 4159 target_mourn_inferior ();
8a3fe4f8 4160 error (_("Watchdog has expired. Target detached."));
c906108c 4161 }
c906108c 4162 if (remote_debug)
0f71a2f6 4163 fputs_filtered ("Timed out.\n", gdb_stdlog);
c906108c
SS
4164 goto retry;
4165 }
4166 }
4167 while (c != '$');
4168
4169 /* We've found the start of a packet, now collect the data. */
4170
c2d11a7d 4171 val = read_frame (buf, sizeof_buf);
c906108c 4172
c2d11a7d 4173 if (val >= 0)
c906108c
SS
4174 {
4175 if (remote_debug)
43e526b9
JM
4176 {
4177 fprintf_unfiltered (gdb_stdlog, "Packet received: ");
6d820c5c 4178 fputstr_unfiltered (*buf, 0, gdb_stdlog);
43e526b9
JM
4179 fprintf_unfiltered (gdb_stdlog, "\n");
4180 }
2cd58942 4181 serial_write (remote_desc, "+", 1);
d9fcf2fb 4182 return 0;
c906108c
SS
4183 }
4184
4185 /* Try the whole thing again. */
4186 retry:
2cd58942 4187 serial_write (remote_desc, "-", 1);
c906108c
SS
4188 }
4189
23860348
MS
4190 /* We have tried hard enough, and just can't receive the packet.
4191 Give up. */
c906108c 4192
a3f17187 4193 printf_unfiltered (_("Ignoring packet error, continuing...\n"));
2cd58942 4194 serial_write (remote_desc, "+", 1);
d9fcf2fb 4195 return 1;
c906108c
SS
4196}
4197\f
4198static void
fba45db2 4199remote_kill (void)
c906108c
SS
4200{
4201 /* For some mysterious reason, wait_for_inferior calls kill instead of
4202 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4203 if (kill_kludge)
4204 {
4205 kill_kludge = 0;
4206 target_mourn_inferior ();
4207 return;
4208 }
4209
4210 /* Use catch_errors so the user can quit from gdb even when we aren't on
4211 speaking terms with the remote system. */
c5aa993b 4212 catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR);
c906108c
SS
4213
4214 /* Don't wait for it to die. I'm not really sure it matters whether
4215 we do or not. For the existing stubs, kill is a noop. */
4216 target_mourn_inferior ();
4217}
4218
23860348 4219/* Async version of remote_kill. */
43ff13b4 4220static void
fba45db2 4221remote_async_kill (void)
43ff13b4 4222{
23860348 4223 /* Unregister the file descriptor from the event loop. */
ed9a39eb 4224 if (target_is_async_p ())
2cd58942 4225 serial_async (remote_desc, NULL, 0);
43ff13b4
JM
4226
4227 /* For some mysterious reason, wait_for_inferior calls kill instead of
4228 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4229 if (kill_kludge)
4230 {
4231 kill_kludge = 0;
4232 target_mourn_inferior ();
4233 return;
4234 }
4235
23860348
MS
4236 /* Use catch_errors so the user can quit from gdb even when we
4237 aren't on speaking terms with the remote system. */
c5aa993b 4238 catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR);
43ff13b4
JM
4239
4240 /* Don't wait for it to die. I'm not really sure it matters whether
4241 we do or not. For the existing stubs, kill is a noop. */
4242 target_mourn_inferior ();
4243}
4244
c906108c 4245static void
fba45db2 4246remote_mourn (void)
c906108c
SS
4247{
4248 remote_mourn_1 (&remote_ops);
4249}
4250
53a5351d 4251static void
fba45db2 4252remote_async_mourn (void)
53a5351d
JM
4253{
4254 remote_mourn_1 (&remote_async_ops);
4255}
4256
c906108c 4257static void
fba45db2 4258extended_remote_mourn (void)
c906108c
SS
4259{
4260 /* We do _not_ want to mourn the target like this; this will
4261 remove the extended remote target from the target stack,
802188a7 4262 and the next time the user says "run" it'll fail.
c906108c
SS
4263
4264 FIXME: What is the right thing to do here? */
4265#if 0
4266 remote_mourn_1 (&extended_remote_ops);
4267#endif
4268}
4269
4270/* Worker function for remote_mourn. */
4271static void
fba45db2 4272remote_mourn_1 (struct target_ops *target)
c906108c
SS
4273{
4274 unpush_target (target);
4275 generic_mourn_inferior ();
4276}
4277
4278/* In the extended protocol we want to be able to do things like
4279 "run" and have them basically work as expected. So we need
802188a7 4280 a special create_inferior function.
c906108c
SS
4281
4282 FIXME: One day add support for changing the exec file
4283 we're debugging, arguments and an environment. */
4284
4285static void
23860348
MS
4286extended_remote_create_inferior (char *exec_file, char *args,
4287 char **env, int from_tty)
c906108c
SS
4288{
4289 /* Rip out the breakpoints; we'll reinsert them after restarting
4290 the remote server. */
4291 remove_breakpoints ();
4292
4293 /* Now restart the remote server. */
4294 extended_remote_restart ();
4295
4296 /* Now put the breakpoints back in. This way we're safe if the
4297 restart function works via a unix fork on the remote side. */
4298 insert_breakpoints ();
4299
4300 /* Clean up from the last time we were running. */
4301 clear_proceed_status ();
c906108c
SS
4302}
4303
23860348 4304/* Async version of extended_remote_create_inferior. */
43ff13b4 4305static void
23860348
MS
4306extended_remote_async_create_inferior (char *exec_file, char *args,
4307 char **env, int from_tty)
43ff13b4
JM
4308{
4309 /* Rip out the breakpoints; we'll reinsert them after restarting
4310 the remote server. */
4311 remove_breakpoints ();
4312
4313 /* If running asynchronously, register the target file descriptor
23860348 4314 with the event loop. */
362646f5 4315 if (target_can_async_p ())
2acceee2 4316 target_async (inferior_event_handler, 0);
43ff13b4
JM
4317
4318 /* Now restart the remote server. */
4319 extended_remote_restart ();
4320
4321 /* Now put the breakpoints back in. This way we're safe if the
4322 restart function works via a unix fork on the remote side. */
4323 insert_breakpoints ();
4324
4325 /* Clean up from the last time we were running. */
4326 clear_proceed_status ();
43ff13b4 4327}
c906108c 4328\f
c5aa993b 4329
aaab4dba 4330/* On some machines, e.g. 68k, we may use a different breakpoint
7fec2c59
AC
4331 instruction than other targets; in those use
4332 DEPRECATED_REMOTE_BREAKPOINT instead of just BREAKPOINT_FROM_PC.
4333 Also, bi-endian targets may define
4334 DEPRECATED_LITTLE_REMOTE_BREAKPOINT and
4335 DEPRECATED_BIG_REMOTE_BREAKPOINT. If none of these are defined, we
4336 just call the standard routines that are in mem-break.c. */
4337
4338/* NOTE: cagney/2003-06-08: This is silly. A remote and simulator
4339 target should use an identical BREAKPOINT_FROM_PC. As for native,
4340 the ARCH-OS-tdep.c code can override the default. */
4341
4342#if defined (DEPRECATED_LITTLE_REMOTE_BREAKPOINT) && defined (DEPRECATED_BIG_REMOTE_BREAKPOINT) && !defined(DEPRECATED_REMOTE_BREAKPOINT)
4343#define DEPRECATED_REMOTE_BREAKPOINT
c906108c
SS
4344#endif
4345
7fec2c59 4346#ifdef DEPRECATED_REMOTE_BREAKPOINT
c906108c
SS
4347
4348/* If the target isn't bi-endian, just pretend it is. */
7fec2c59
AC
4349#if !defined (DEPRECATED_LITTLE_REMOTE_BREAKPOINT) && !defined (DEPRECATED_BIG_REMOTE_BREAKPOINT)
4350#define DEPRECATED_LITTLE_REMOTE_BREAKPOINT DEPRECATED_REMOTE_BREAKPOINT
4351#define DEPRECATED_BIG_REMOTE_BREAKPOINT DEPRECATED_REMOTE_BREAKPOINT
c906108c
SS
4352#endif
4353
7fec2c59
AC
4354static unsigned char big_break_insn[] = DEPRECATED_BIG_REMOTE_BREAKPOINT;
4355static unsigned char little_break_insn[] = DEPRECATED_LITTLE_REMOTE_BREAKPOINT;
c906108c 4356
7fec2c59 4357#endif /* DEPRECATED_REMOTE_BREAKPOINT */
c906108c 4358
aaab4dba
AC
4359/* Insert a breakpoint on targets that don't have any better
4360 breakpoint support. We read the contents of the target location
4361 and stash it, then overwrite it with a breakpoint instruction.
4362 ADDR is the target location in the target machine. CONTENTS_CACHE
4363 is a pointer to memory allocated for saving the target contents.
4364 It is guaranteed by the caller to be long enough to save the number
4365 of bytes returned by BREAKPOINT_FROM_PC. */
c906108c
SS
4366
4367static int
5a168c78 4368remote_insert_breakpoint (CORE_ADDR addr, bfd_byte *contents_cache)
c906108c 4369{
d01949b6 4370 struct remote_state *rs = get_remote_state ();
7fec2c59 4371#ifdef DEPRECATED_REMOTE_BREAKPOINT
c906108c 4372 int val;
802188a7 4373#endif
96baa820
JM
4374 int bp_size;
4375
d471ea57
AC
4376 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
4377 If it succeeds, then set the support to PACKET_ENABLE. If it
4378 fails, and the user has explicitly requested the Z support then
23860348 4379 report an error, otherwise, mark it disabled and go on. */
802188a7 4380
444abaca 4381 if (remote_protocol_packets[PACKET_Z0].support != PACKET_DISABLE)
96baa820 4382 {
6d820c5c 4383 char *p = rs->buf;
802188a7 4384
0caabb7e 4385 addr = remote_address_masked (addr);
96baa820
JM
4386 *(p++) = 'Z';
4387 *(p++) = '0';
4388 *(p++) = ',';
0caabb7e
AC
4389 p += hexnumstr (p, (ULONGEST) addr);
4390 BREAKPOINT_FROM_PC (&addr, &bp_size);
96baa820 4391 sprintf (p, ",%d", bp_size);
802188a7 4392
6d820c5c
DJ
4393 putpkt (rs->buf);
4394 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 4395
6d820c5c 4396 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0]))
96baa820 4397 {
d471ea57
AC
4398 case PACKET_ERROR:
4399 return -1;
4400 case PACKET_OK:
4401 return 0;
4402 case PACKET_UNKNOWN:
4403 break;
96baa820
JM
4404 }
4405 }
c906108c 4406
802188a7 4407#ifdef DEPRECATED_REMOTE_BREAKPOINT
c906108c
SS
4408 val = target_read_memory (addr, contents_cache, sizeof big_break_insn);
4409
4410 if (val == 0)
4411 {
d7449b42 4412 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
c906108c
SS
4413 val = target_write_memory (addr, (char *) big_break_insn,
4414 sizeof big_break_insn);
4415 else
4416 val = target_write_memory (addr, (char *) little_break_insn,
4417 sizeof little_break_insn);
4418 }
4419
4420 return val;
4421#else
4422 return memory_insert_breakpoint (addr, contents_cache);
7fec2c59 4423#endif /* DEPRECATED_REMOTE_BREAKPOINT */
c906108c
SS
4424}
4425
4426static int
5a168c78 4427remote_remove_breakpoint (CORE_ADDR addr, bfd_byte *contents_cache)
c906108c 4428{
d01949b6 4429 struct remote_state *rs = get_remote_state ();
96baa820
JM
4430 int bp_size;
4431
444abaca 4432 if (remote_protocol_packets[PACKET_Z0].support != PACKET_DISABLE)
96baa820 4433 {
6d820c5c 4434 char *p = rs->buf;
802188a7 4435
96baa820
JM
4436 *(p++) = 'z';
4437 *(p++) = '0';
4438 *(p++) = ',';
4439
0caabb7e
AC
4440 addr = remote_address_masked (addr);
4441 p += hexnumstr (p, (ULONGEST) addr);
4442 BREAKPOINT_FROM_PC (&addr, &bp_size);
96baa820 4443 sprintf (p, ",%d", bp_size);
802188a7 4444
6d820c5c
DJ
4445 putpkt (rs->buf);
4446 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 4447
6d820c5c 4448 return (rs->buf[0] == 'E');
96baa820
JM
4449 }
4450
7fec2c59 4451#ifdef DEPRECATED_REMOTE_BREAKPOINT
c906108c
SS
4452 return target_write_memory (addr, contents_cache, sizeof big_break_insn);
4453#else
4454 return memory_remove_breakpoint (addr, contents_cache);
7fec2c59 4455#endif /* DEPRECATED_REMOTE_BREAKPOINT */
c906108c
SS
4456}
4457
d471ea57
AC
4458static int
4459watchpoint_to_Z_packet (int type)
4460{
4461 switch (type)
4462 {
4463 case hw_write:
bb858e6a 4464 return Z_PACKET_WRITE_WP;
d471ea57
AC
4465 break;
4466 case hw_read:
bb858e6a 4467 return Z_PACKET_READ_WP;
d471ea57
AC
4468 break;
4469 case hw_access:
bb858e6a 4470 return Z_PACKET_ACCESS_WP;
d471ea57
AC
4471 break;
4472 default:
8e65ff28 4473 internal_error (__FILE__, __LINE__,
e2e0b3e5 4474 _("hw_bp_to_z: bad watchpoint type %d"), type);
d471ea57
AC
4475 }
4476}
4477
3c3bea1c 4478static int
fba45db2 4479remote_insert_watchpoint (CORE_ADDR addr, int len, int type)
96baa820 4480{
d01949b6 4481 struct remote_state *rs = get_remote_state ();
e514a9d6 4482 char *p;
d471ea57 4483 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
96baa820 4484
444abaca 4485 if (remote_protocol_packets[PACKET_Z0 + packet].support == PACKET_DISABLE)
8a3fe4f8 4486 error (_("Can't set hardware watchpoints without the '%s' (%s) packet."),
444abaca
DJ
4487 remote_protocol_packets[PACKET_Z0 + packet].name,
4488 remote_protocol_packets[PACKET_Z0 + packet].title);
802188a7 4489
6d820c5c
DJ
4490 sprintf (rs->buf, "Z%x,", packet);
4491 p = strchr (rs->buf, '\0');
96baa820
JM
4492 addr = remote_address_masked (addr);
4493 p += hexnumstr (p, (ULONGEST) addr);
d4f3574e 4494 sprintf (p, ",%x", len);
802188a7 4495
6d820c5c
DJ
4496 putpkt (rs->buf);
4497 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 4498
6d820c5c 4499 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
4500 {
4501 case PACKET_ERROR:
4502 case PACKET_UNKNOWN:
4503 return -1;
4504 case PACKET_OK:
4505 return 0;
4506 }
8e65ff28 4507 internal_error (__FILE__, __LINE__,
e2e0b3e5 4508 _("remote_insert_watchpoint: reached end of function"));
96baa820
JM
4509}
4510
d471ea57 4511
3c3bea1c 4512static int
fba45db2 4513remote_remove_watchpoint (CORE_ADDR addr, int len, int type)
96baa820 4514{
d01949b6 4515 struct remote_state *rs = get_remote_state ();
e514a9d6 4516 char *p;
d471ea57
AC
4517 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
4518
444abaca 4519 if (remote_protocol_packets[PACKET_Z0 + packet].support == PACKET_DISABLE)
8a3fe4f8 4520 error (_("Can't clear hardware watchpoints without the '%s' (%s) packet."),
444abaca
DJ
4521 remote_protocol_packets[PACKET_Z0 + packet].name,
4522 remote_protocol_packets[PACKET_Z0 + packet].title);
802188a7 4523
6d820c5c
DJ
4524 sprintf (rs->buf, "z%x,", packet);
4525 p = strchr (rs->buf, '\0');
96baa820
JM
4526 addr = remote_address_masked (addr);
4527 p += hexnumstr (p, (ULONGEST) addr);
d4f3574e 4528 sprintf (p, ",%x", len);
6d820c5c
DJ
4529 putpkt (rs->buf);
4530 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 4531
6d820c5c 4532 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
d471ea57
AC
4533 {
4534 case PACKET_ERROR:
4535 case PACKET_UNKNOWN:
4536 return -1;
4537 case PACKET_OK:
4538 return 0;
4539 }
8e65ff28 4540 internal_error (__FILE__, __LINE__,
e2e0b3e5 4541 _("remote_remove_watchpoint: reached end of function"));
96baa820
JM
4542}
4543
3c3bea1c 4544
501eef12
AC
4545int remote_hw_watchpoint_limit = -1;
4546int remote_hw_breakpoint_limit = -1;
d471ea57 4547
b9362cc7 4548static int
3c3bea1c 4549remote_check_watch_resources (int type, int cnt, int ot)
96baa820 4550{
3c3bea1c
GS
4551 if (type == bp_hardware_breakpoint)
4552 {
4553 if (remote_hw_breakpoint_limit == 0)
4554 return 0;
501eef12
AC
4555 else if (remote_hw_breakpoint_limit < 0)
4556 return 1;
3c3bea1c
GS
4557 else if (cnt <= remote_hw_breakpoint_limit)
4558 return 1;
4559 }
4560 else
4561 {
4562 if (remote_hw_watchpoint_limit == 0)
4563 return 0;
501eef12
AC
4564 else if (remote_hw_watchpoint_limit < 0)
4565 return 1;
3c3bea1c
GS
4566 else if (ot)
4567 return -1;
4568 else if (cnt <= remote_hw_watchpoint_limit)
4569 return 1;
4570 }
4571 return -1;
4572}
4573
b9362cc7 4574static int
3c3bea1c
GS
4575remote_stopped_by_watchpoint (void)
4576{
4577 return remote_stopped_by_watchpoint_p;
4578}
4579
7270d8f2
OF
4580extern int stepped_after_stopped_by_watchpoint;
4581
4aa7a7f5
JJ
4582static int
4583remote_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
3c3bea1c 4584{
4aa7a7f5 4585 int rc = 0;
7270d8f2
OF
4586 if (remote_stopped_by_watchpoint ()
4587 || stepped_after_stopped_by_watchpoint)
4aa7a7f5
JJ
4588 {
4589 *addr_p = remote_watch_data_address;
4590 rc = 1;
4591 }
4592
4593 return rc;
3c3bea1c
GS
4594}
4595
4596
4597static int
c6826062 4598remote_insert_hw_breakpoint (CORE_ADDR addr, gdb_byte *shadow)
3c3bea1c
GS
4599{
4600 int len = 0;
d01949b6 4601 struct remote_state *rs = get_remote_state ();
6d820c5c 4602 char *p = rs->buf;
802188a7 4603
c8189ed1
GS
4604 /* The length field should be set to the size of a breakpoint
4605 instruction. */
4606
802188a7 4607 BREAKPOINT_FROM_PC (&addr, &len);
3c3bea1c 4608
444abaca 4609 if (remote_protocol_packets[PACKET_Z1].support == PACKET_DISABLE)
8a3fe4f8 4610 error (_("Can't set hardware breakpoint without the '%s' (%s) packet."),
444abaca
DJ
4611 remote_protocol_packets[PACKET_Z1].name,
4612 remote_protocol_packets[PACKET_Z1].title);
802188a7 4613
96baa820
JM
4614 *(p++) = 'Z';
4615 *(p++) = '1';
4616 *(p++) = ',';
802188a7 4617
96baa820
JM
4618 addr = remote_address_masked (addr);
4619 p += hexnumstr (p, (ULONGEST) addr);
ad6525fc 4620 sprintf (p, ",%x", len);
96baa820 4621
6d820c5c
DJ
4622 putpkt (rs->buf);
4623 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 4624
6d820c5c 4625 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
4626 {
4627 case PACKET_ERROR:
4628 case PACKET_UNKNOWN:
4629 return -1;
4630 case PACKET_OK:
4631 return 0;
4632 }
8e65ff28 4633 internal_error (__FILE__, __LINE__,
e2e0b3e5 4634 _("remote_insert_hw_breakpoint: reached end of function"));
96baa820
JM
4635}
4636
d471ea57 4637
802188a7 4638static int
c6826062 4639remote_remove_hw_breakpoint (CORE_ADDR addr, gdb_byte *shadow)
96baa820 4640{
3c3bea1c 4641 int len;
d01949b6 4642 struct remote_state *rs = get_remote_state ();
6d820c5c 4643 char *p = rs->buf;
c8189ed1
GS
4644
4645 /* The length field should be set to the size of a breakpoint
4646 instruction. */
4647
4648 BREAKPOINT_FROM_PC (&addr, &len);
4649
444abaca 4650 if (remote_protocol_packets[PACKET_Z1].support == PACKET_DISABLE)
8a3fe4f8 4651 error (_("Can't clear hardware breakpoint without the '%s' (%s) packet."),
444abaca
DJ
4652 remote_protocol_packets[PACKET_Z1].name,
4653 remote_protocol_packets[PACKET_Z1].title);
802188a7 4654
96baa820
JM
4655 *(p++) = 'z';
4656 *(p++) = '1';
4657 *(p++) = ',';
802188a7 4658
96baa820
JM
4659 addr = remote_address_masked (addr);
4660 p += hexnumstr (p, (ULONGEST) addr);
ad6525fc 4661 sprintf (p, ",%x", len);
96baa820 4662
6d820c5c
DJ
4663 putpkt (rs->buf);
4664 getpkt (&rs->buf, &rs->buf_size, 0);
802188a7 4665
6d820c5c 4666 switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
d471ea57
AC
4667 {
4668 case PACKET_ERROR:
4669 case PACKET_UNKNOWN:
4670 return -1;
4671 case PACKET_OK:
4672 return 0;
4673 }
8e65ff28 4674 internal_error (__FILE__, __LINE__,
e2e0b3e5 4675 _("remote_remove_hw_breakpoint: reached end of function"));
96baa820 4676}
96baa820 4677
c906108c
SS
4678/* Some targets are only capable of doing downloads, and afterwards
4679 they switch to the remote serial protocol. This function provides
4680 a clean way to get from the download target to the remote target.
4681 It's basically just a wrapper so that we don't have to expose any
4682 of the internal workings of remote.c.
4683
4684 Prior to calling this routine, you should shutdown the current
4685 target code, else you will get the "A program is being debugged
4686 already..." message. Usually a call to pop_target() suffices. */
4687
4688void
fba45db2 4689push_remote_target (char *name, int from_tty)
c906108c 4690{
a3f17187 4691 printf_filtered (_("Switching to remote protocol\n"));
c906108c
SS
4692 remote_open (name, from_tty);
4693}
4694
23860348 4695/* Table used by the crc32 function to calcuate the checksum. */
c906108c 4696
c5aa993b
JM
4697static unsigned long crc32_table[256] =
4698{0, 0};
c906108c
SS
4699
4700static unsigned long
fba45db2 4701crc32 (unsigned char *buf, int len, unsigned int crc)
c906108c 4702{
c5aa993b 4703 if (!crc32_table[1])
c906108c 4704 {
23860348 4705 /* Initialize the CRC table and the decoding table. */
c906108c
SS
4706 int i, j;
4707 unsigned int c;
4708
4709 for (i = 0; i < 256; i++)
c5aa993b
JM
4710 {
4711 for (c = i << 24, j = 8; j > 0; --j)
4712 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
4713 crc32_table[i] = c;
4714 }
c906108c
SS
4715 }
4716
4717 while (len--)
4718 {
4719 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buf) & 255];
4720 buf++;
4721 }
4722 return crc;
4723}
4724
4725/* compare-sections command
4726
4727 With no arguments, compares each loadable section in the exec bfd
4728 with the same memory range on the target, and reports mismatches.
4729 Useful for verifying the image on the target against the exec file.
4730 Depends on the target understanding the new "qCRC:" request. */
4731
e514a9d6
JM
4732/* FIXME: cagney/1999-10-26: This command should be broken down into a
4733 target method (target verify memory) and generic version of the
4734 actual command. This will allow other high-level code (especially
23860348 4735 generic_load()) to make use of this target functionality. */
e514a9d6 4736
c906108c 4737static void
fba45db2 4738compare_sections_command (char *args, int from_tty)
c906108c 4739{
d01949b6 4740 struct remote_state *rs = get_remote_state ();
c906108c
SS
4741 asection *s;
4742 unsigned long host_crc, target_crc;
4743 extern bfd *exec_bfd;
4744 struct cleanup *old_chain;
085dd6e6
JM
4745 char *tmp;
4746 char *sectdata;
ce359b09 4747 const char *sectname;
c906108c
SS
4748 bfd_size_type size;
4749 bfd_vma lma;
4750 int matched = 0;
4751 int mismatched = 0;
4752
4753 if (!exec_bfd)
8a3fe4f8 4754 error (_("command cannot be used without an exec file"));
c906108c
SS
4755 if (!current_target.to_shortname ||
4756 strcmp (current_target.to_shortname, "remote") != 0)
8a3fe4f8 4757 error (_("command can only be used with remote target"));
c906108c 4758
c5aa993b 4759 for (s = exec_bfd->sections; s; s = s->next)
c906108c
SS
4760 {
4761 if (!(s->flags & SEC_LOAD))
c5aa993b 4762 continue; /* skip non-loadable section */
c906108c 4763
2c500098 4764 size = bfd_get_section_size (s);
c906108c 4765 if (size == 0)
c5aa993b 4766 continue; /* skip zero-length section */
c906108c 4767
ce359b09 4768 sectname = bfd_get_section_name (exec_bfd, s);
c906108c 4769 if (args && strcmp (args, sectname) != 0)
c5aa993b 4770 continue; /* not the section selected by user */
c906108c 4771
c5aa993b 4772 matched = 1; /* do this section */
c906108c 4773 lma = s->lma;
23860348 4774 /* FIXME: assumes lma can fit into long. */
6d820c5c 4775 xsnprintf (rs->buf, rs->remote_packet_size, "qCRC:%lx,%lx",
ecbc58df 4776 (long) lma, (long) size);
6d820c5c 4777 putpkt (rs->buf);
c906108c 4778
23860348
MS
4779 /* Be clever; compute the host_crc before waiting for target
4780 reply. */
c906108c 4781 sectdata = xmalloc (size);
b8c9b27d 4782 old_chain = make_cleanup (xfree, sectdata);
c906108c
SS
4783 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
4784 host_crc = crc32 ((unsigned char *) sectdata, size, 0xffffffff);
4785
6d820c5c
DJ
4786 getpkt (&rs->buf, &rs->buf_size, 0);
4787 if (rs->buf[0] == 'E')
8a3fe4f8 4788 error (_("target memory fault, section %s, range 0x%s -- 0x%s"),
823ca731 4789 sectname, paddr (lma), paddr (lma + size));
6d820c5c 4790 if (rs->buf[0] != 'C')
8a3fe4f8 4791 error (_("remote target does not support this operation"));
c906108c 4792
6d820c5c 4793 for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++)
c906108c
SS
4794 target_crc = target_crc * 16 + fromhex (*tmp);
4795
d4f3574e
SS
4796 printf_filtered ("Section %s, range 0x%s -- 0x%s: ",
4797 sectname, paddr (lma), paddr (lma + size));
c906108c
SS
4798 if (host_crc == target_crc)
4799 printf_filtered ("matched.\n");
4800 else
c5aa993b
JM
4801 {
4802 printf_filtered ("MIS-MATCHED!\n");
4803 mismatched++;
4804 }
c906108c
SS
4805
4806 do_cleanups (old_chain);
4807 }
4808 if (mismatched > 0)
8a3fe4f8
AC
4809 warning (_("One or more sections of the remote executable does not match\n\
4810the loaded file\n"));
c906108c 4811 if (args && !matched)
a3f17187 4812 printf_filtered (_("No loaded section named '%s'.\n"), args);
c906108c
SS
4813}
4814
1e3ff5ad 4815static LONGEST
4b8a223f 4816remote_xfer_partial (struct target_ops *ops, enum target_object object,
961cb7b5
MK
4817 const char *annex, gdb_byte *readbuf,
4818 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
c906108c 4819{
d01949b6 4820 struct remote_state *rs = get_remote_state ();
c906108c 4821 int i;
6d820c5c 4822 char *p2;
1e3ff5ad 4823 char query_type;
c906108c 4824
21e3b9b9
DJ
4825 /* Handle memory using remote_xfer_memory. */
4826 if (object == TARGET_OBJECT_MEMORY)
4827 {
4828 int xfered;
4829 errno = 0;
4830
4831 if (writebuf != NULL)
4832 {
4833 void *buffer = xmalloc (len);
4834 struct cleanup *cleanup = make_cleanup (xfree, buffer);
4835 memcpy (buffer, writebuf, len);
4836 xfered = remote_xfer_memory (offset, buffer, len, 1, NULL, ops);
4837 do_cleanups (cleanup);
4838 }
4839 else
4840 xfered = remote_xfer_memory (offset, readbuf, len, 0, NULL, ops);
4841
4842 if (xfered > 0)
4843 return xfered;
4844 else if (xfered == 0 && errno == 0)
4845 return 0;
4846 else
4847 return -1;
4848 }
4849
4b8a223f
AC
4850 /* Only handle reads. */
4851 if (writebuf != NULL || readbuf == NULL)
4852 return -1;
4853
1e3ff5ad
AC
4854 /* Map pre-existing objects onto letters. DO NOT do this for new
4855 objects!!! Instead specify new query packets. */
4856 switch (object)
c906108c 4857 {
1e3ff5ad
AC
4858 case TARGET_OBJECT_KOD:
4859 query_type = 'K';
4860 break;
4861 case TARGET_OBJECT_AVR:
4862 query_type = 'R';
4863 break;
802188a7
RM
4864
4865 case TARGET_OBJECT_AUXV:
444abaca 4866 if (remote_protocol_packets[PACKET_qPart_auxv].support != PACKET_DISABLE)
802188a7
RM
4867 {
4868 unsigned int total = 0;
4869 while (len > 0)
4870 {
4871 LONGEST n = min ((rs->remote_packet_size - 2) / 2, len);
6d820c5c 4872 snprintf (rs->buf, rs->remote_packet_size,
802188a7
RM
4873 "qPart:auxv:read::%s,%s",
4874 phex_nz (offset, sizeof offset),
4875 phex_nz (n, sizeof n));
6d820c5c 4876 i = putpkt (rs->buf);
802188a7
RM
4877 if (i < 0)
4878 return total > 0 ? total : i;
6d820c5c
DJ
4879 rs->buf[0] = '\0';
4880 getpkt (&rs->buf, &rs->buf_size, 0);
4881 if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_qPart_auxv])
444abaca 4882 != PACKET_OK)
802188a7 4883 return total > 0 ? total : -1;
6d820c5c 4884 if (strcmp (rs->buf, "OK") == 0)
802188a7
RM
4885 break; /* Got EOF indicator. */
4886 /* Got some data. */
6d820c5c 4887 i = hex2bin (rs->buf, readbuf, len);
802188a7
RM
4888 if (i > 0)
4889 {
4890 readbuf = (void *) ((char *) readbuf + i);
4891 offset += i;
4892 len -= i;
4893 total += i;
4894 }
4895 }
4896 return total;
4897 }
4898 return -1;
4899
1e3ff5ad 4900 default:
c906108c
SS
4901 return -1;
4902 }
4903
4b8a223f 4904 /* Note: a zero OFFSET and LEN can be used to query the minimum
1e3ff5ad 4905 buffer size. */
4b8a223f 4906 if (offset == 0 && len == 0)
1e3ff5ad 4907 return (rs->remote_packet_size);
24b06219
MK
4908 /* Minimum outbuf size is RS->remote_packet_size. If LEN is not
4909 large enough let the caller deal with it. */
4910 if (len < rs->remote_packet_size)
1e3ff5ad
AC
4911 return -1;
4912 len = rs->remote_packet_size;
4913
23860348 4914 /* Except for querying the minimum buffer size, target must be open. */
c5aa993b 4915 if (!remote_desc)
8a3fe4f8 4916 error (_("remote query is only available after target open"));
c906108c 4917
1e3ff5ad 4918 gdb_assert (annex != NULL);
4b8a223f 4919 gdb_assert (readbuf != NULL);
c906108c 4920
6d820c5c 4921 p2 = rs->buf;
c906108c
SS
4922 *p2++ = 'q';
4923 *p2++ = query_type;
4924
23860348
MS
4925 /* We used one buffer char for the remote protocol q command and
4926 another for the query type. As the remote protocol encapsulation
4927 uses 4 chars plus one extra in case we are debugging
4928 (remote_debug), we have PBUFZIZ - 7 left to pack the query
4929 string. */
c906108c 4930 i = 0;
24b06219 4931 while (annex[i] && (i < (rs->remote_packet_size - 8)))
c906108c 4932 {
1e3ff5ad
AC
4933 /* Bad caller may have sent forbidden characters. */
4934 gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#');
4935 *p2++ = annex[i];
c906108c
SS
4936 i++;
4937 }
1e3ff5ad
AC
4938 *p2 = '\0';
4939 gdb_assert (annex[i] == '\0');
c906108c 4940
6d820c5c 4941 i = putpkt (rs->buf);
c5aa993b
JM
4942 if (i < 0)
4943 return i;
c906108c 4944
6d820c5c
DJ
4945 getpkt (&rs->buf, &rs->buf_size, 0);
4946 strcpy ((char *) readbuf, rs->buf);
c906108c 4947
cfd77fa1 4948 return strlen ((char *) readbuf);
c906108c
SS
4949}
4950
96baa820
JM
4951static void
4952remote_rcmd (char *command,
d9fcf2fb 4953 struct ui_file *outbuf)
96baa820 4954{
d01949b6 4955 struct remote_state *rs = get_remote_state ();
6d820c5c 4956 char *buf = rs->buf;
96baa820
JM
4957 char *p = buf;
4958
4959 if (!remote_desc)
8a3fe4f8 4960 error (_("remote rcmd is only available after target open"));
96baa820 4961
23860348 4962 /* Send a NULL command across as an empty command. */
7be570e7
JM
4963 if (command == NULL)
4964 command = "";
4965
23860348 4966 /* The query prefix. */
96baa820
JM
4967 strcpy (buf, "qRcmd,");
4968 p = strchr (buf, '\0');
4969
24b06219 4970 if ((strlen (buf) + strlen (command) * 2 + 8/*misc*/) > rs->remote_packet_size)
8a3fe4f8 4971 error (_("\"monitor\" command ``%s'' is too long."), command);
96baa820 4972
23860348 4973 /* Encode the actual command. */
cfd77fa1 4974 bin2hex ((gdb_byte *) command, p, 0);
96baa820 4975
6d820c5c 4976 if (putpkt (rs->buf) < 0)
8a3fe4f8 4977 error (_("Communication problem with target."));
96baa820
JM
4978
4979 /* get/display the response */
4980 while (1)
4981 {
23860348 4982 /* XXX - see also tracepoint.c:remote_get_noisy_reply(). */
96baa820 4983 buf[0] = '\0';
6d820c5c 4984 getpkt (&rs->buf, &rs->buf_size, 0);
96baa820 4985 if (buf[0] == '\0')
8a3fe4f8 4986 error (_("Target does not support this command."));
96baa820
JM
4987 if (buf[0] == 'O' && buf[1] != 'K')
4988 {
23860348 4989 remote_console_output (buf + 1); /* 'O' message from stub. */
96baa820
JM
4990 continue;
4991 }
4992 if (strcmp (buf, "OK") == 0)
4993 break;
7be570e7
JM
4994 if (strlen (buf) == 3 && buf[0] == 'E'
4995 && isdigit (buf[1]) && isdigit (buf[2]))
4996 {
8a3fe4f8 4997 error (_("Protocol error with Rcmd"));
7be570e7 4998 }
96baa820
JM
4999 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
5000 {
5001 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
5002 fputc_unfiltered (c, outbuf);
5003 }
5004 break;
5005 }
5006}
5007
c906108c 5008static void
fba45db2 5009packet_command (char *args, int from_tty)
c906108c 5010{
d01949b6 5011 struct remote_state *rs = get_remote_state ();
c906108c 5012
c5aa993b 5013 if (!remote_desc)
8a3fe4f8 5014 error (_("command can only be used with remote target"));
c906108c 5015
c5aa993b 5016 if (!args)
8a3fe4f8 5017 error (_("remote-packet command requires packet text as argument"));
c906108c
SS
5018
5019 puts_filtered ("sending: ");
5020 print_packet (args);
5021 puts_filtered ("\n");
5022 putpkt (args);
5023
6d820c5c 5024 getpkt (&rs->buf, &rs->buf_size, 0);
c906108c 5025 puts_filtered ("received: ");
6d820c5c 5026 print_packet (rs->buf);
c906108c
SS
5027 puts_filtered ("\n");
5028}
5029
5030#if 0
23860348 5031/* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
c906108c 5032
a14ed312 5033static void display_thread_info (struct gdb_ext_thread_info *info);
c906108c 5034
a14ed312 5035static void threadset_test_cmd (char *cmd, int tty);
c906108c 5036
a14ed312 5037static void threadalive_test (char *cmd, int tty);
c906108c 5038
a14ed312 5039static void threadlist_test_cmd (char *cmd, int tty);
c906108c 5040
23860348 5041int get_and_display_threadinfo (threadref *ref);
c906108c 5042
a14ed312 5043static void threadinfo_test_cmd (char *cmd, int tty);
c906108c 5044
23860348 5045static int thread_display_step (threadref *ref, void *context);
c906108c 5046
a14ed312 5047static void threadlist_update_test_cmd (char *cmd, int tty);
c906108c 5048
a14ed312 5049static void init_remote_threadtests (void);
c906108c 5050
23860348 5051#define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
c906108c
SS
5052
5053static void
fba45db2 5054threadset_test_cmd (char *cmd, int tty)
c906108c
SS
5055{
5056 int sample_thread = SAMPLE_THREAD;
5057
a3f17187 5058 printf_filtered (_("Remote threadset test\n"));
c906108c
SS
5059 set_thread (sample_thread, 1);
5060}
5061
5062
5063static void
fba45db2 5064threadalive_test (char *cmd, int tty)
c906108c
SS
5065{
5066 int sample_thread = SAMPLE_THREAD;
5067
39f77062 5068 if (remote_thread_alive (pid_to_ptid (sample_thread)))
c906108c
SS
5069 printf_filtered ("PASS: Thread alive test\n");
5070 else
5071 printf_filtered ("FAIL: Thread alive test\n");
5072}
5073
23860348 5074void output_threadid (char *title, threadref *ref);
c906108c
SS
5075
5076void
fba45db2 5077output_threadid (char *title, threadref *ref)
c906108c
SS
5078{
5079 char hexid[20];
5080
23860348 5081 pack_threadid (&hexid[0], ref); /* Convert threead id into hex. */
c906108c
SS
5082 hexid[16] = 0;
5083 printf_filtered ("%s %s\n", title, (&hexid[0]));
5084}
5085
5086static void
fba45db2 5087threadlist_test_cmd (char *cmd, int tty)
c906108c
SS
5088{
5089 int startflag = 1;
5090 threadref nextthread;
5091 int done, result_count;
5092 threadref threadlist[3];
5093
5094 printf_filtered ("Remote Threadlist test\n");
5095 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
5096 &result_count, &threadlist[0]))
5097 printf_filtered ("FAIL: threadlist test\n");
5098 else
5099 {
5100 threadref *scan = threadlist;
5101 threadref *limit = scan + result_count;
5102
5103 while (scan < limit)
5104 output_threadid (" thread ", scan++);
5105 }
5106}
5107
5108void
fba45db2 5109display_thread_info (struct gdb_ext_thread_info *info)
c906108c
SS
5110{
5111 output_threadid ("Threadid: ", &info->threadid);
5112 printf_filtered ("Name: %s\n ", info->shortname);
5113 printf_filtered ("State: %s\n", info->display);
5114 printf_filtered ("other: %s\n\n", info->more_display);
5115}
5116
5117int
fba45db2 5118get_and_display_threadinfo (threadref *ref)
c906108c
SS
5119{
5120 int result;
5121 int set;
5122 struct gdb_ext_thread_info threadinfo;
5123
5124 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
5125 | TAG_MOREDISPLAY | TAG_DISPLAY;
5126 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
5127 display_thread_info (&threadinfo);
5128 return result;
5129}
5130
5131static void
fba45db2 5132threadinfo_test_cmd (char *cmd, int tty)
c906108c
SS
5133{
5134 int athread = SAMPLE_THREAD;
5135 threadref thread;
5136 int set;
5137
5138 int_to_threadref (&thread, athread);
5139 printf_filtered ("Remote Threadinfo test\n");
5140 if (!get_and_display_threadinfo (&thread))
5141 printf_filtered ("FAIL cannot get thread info\n");
5142}
5143
5144static int
fba45db2 5145thread_display_step (threadref *ref, void *context)
c906108c
SS
5146{
5147 /* output_threadid(" threadstep ",ref); *//* simple test */
5148 return get_and_display_threadinfo (ref);
5149}
5150
5151static void
fba45db2 5152threadlist_update_test_cmd (char *cmd, int tty)
c906108c
SS
5153{
5154 printf_filtered ("Remote Threadlist update test\n");
5155 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
5156}
5157
5158static void
5159init_remote_threadtests (void)
5160{
1bedd215
AC
5161 add_com ("tlist", class_obscure, threadlist_test_cmd, _("\
5162Fetch and print the remote list of thread identifiers, one pkt only"));
c906108c 5163 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
1bedd215 5164 _("Fetch and display info about one thread"));
c906108c 5165 add_com ("tset", class_obscure, threadset_test_cmd,
1bedd215 5166 _("Test setting to a different thread"));
c906108c 5167 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
1bedd215 5168 _("Iterate through updating all remote thread info"));
c906108c 5169 add_com ("talive", class_obscure, threadalive_test,
1bedd215 5170 _(" Remote thread alive test "));
c906108c
SS
5171}
5172
5173#endif /* 0 */
5174
f3fb8c85
MS
5175/* Convert a thread ID to a string. Returns the string in a static
5176 buffer. */
5177
5178static char *
39f77062 5179remote_pid_to_str (ptid_t ptid)
f3fb8c85 5180{
fd0a2a6f 5181 static char buf[32];
f3fb8c85 5182
6c76e4a1 5183 xsnprintf (buf, sizeof buf, "thread %d", ptid_get_pid (ptid));
f3fb8c85
MS
5184 return buf;
5185}
5186
38691318
KB
5187/* Get the address of the thread local variable in OBJFILE which is
5188 stored at OFFSET within the thread local storage for thread PTID. */
5189
5190static CORE_ADDR
5191remote_get_thread_local_address (ptid_t ptid, CORE_ADDR lm, CORE_ADDR offset)
5192{
444abaca 5193 if (remote_protocol_packets[PACKET_qGetTLSAddr].support != PACKET_DISABLE)
38691318
KB
5194 {
5195 struct remote_state *rs = get_remote_state ();
6d820c5c 5196 char *p = rs->buf;
571dd617 5197 enum packet_result result;
38691318
KB
5198
5199 strcpy (p, "qGetTLSAddr:");
5200 p += strlen (p);
5201 p += hexnumstr (p, PIDGET (ptid));
5202 *p++ = ',';
5203 p += hexnumstr (p, offset);
5204 *p++ = ',';
5205 p += hexnumstr (p, lm);
5206 *p++ = '\0';
5207
6d820c5c
DJ
5208 putpkt (rs->buf);
5209 getpkt (&rs->buf, &rs->buf_size, 0);
5210 result = packet_ok (rs->buf, &remote_protocol_packets[PACKET_qGetTLSAddr]);
571dd617 5211 if (result == PACKET_OK)
38691318
KB
5212 {
5213 ULONGEST result;
5214
6d820c5c 5215 unpack_varlen_hex (rs->buf, &result);
38691318
KB
5216 return result;
5217 }
571dd617 5218 else if (result == PACKET_UNKNOWN)
109c3e39
AC
5219 throw_error (TLS_GENERIC_ERROR,
5220 _("Remote target doesn't support qGetTLSAddr packet"));
38691318 5221 else
109c3e39
AC
5222 throw_error (TLS_GENERIC_ERROR,
5223 _("Remote target failed to process qGetTLSAddr request"));
38691318
KB
5224 }
5225 else
109c3e39
AC
5226 throw_error (TLS_GENERIC_ERROR,
5227 _("TLS not supported or disabled on this target"));
38691318
KB
5228 /* Not reached. */
5229 return 0;
5230}
5231
c906108c 5232static void
fba45db2 5233init_remote_ops (void)
c906108c 5234{
c5aa993b 5235 remote_ops.to_shortname = "remote";
c906108c 5236 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
c5aa993b 5237 remote_ops.to_doc =
c906108c 5238 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
0d06e24b
JM
5239Specify the serial device it is connected to\n\
5240(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
c5aa993b
JM
5241 remote_ops.to_open = remote_open;
5242 remote_ops.to_close = remote_close;
c906108c 5243 remote_ops.to_detach = remote_detach;
6ad8ae5c 5244 remote_ops.to_disconnect = remote_disconnect;
c5aa993b 5245 remote_ops.to_resume = remote_resume;
c906108c
SS
5246 remote_ops.to_wait = remote_wait;
5247 remote_ops.to_fetch_registers = remote_fetch_registers;
5248 remote_ops.to_store_registers = remote_store_registers;
5249 remote_ops.to_prepare_to_store = remote_prepare_to_store;
c8e73a31 5250 remote_ops.deprecated_xfer_memory = remote_xfer_memory;
c5aa993b 5251 remote_ops.to_files_info = remote_files_info;
c906108c
SS
5252 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
5253 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
3c3bea1c
GS
5254 remote_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
5255 remote_ops.to_stopped_data_address = remote_stopped_data_address;
5256 remote_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
5257 remote_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
5258 remote_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
5259 remote_ops.to_insert_watchpoint = remote_insert_watchpoint;
5260 remote_ops.to_remove_watchpoint = remote_remove_watchpoint;
c5aa993b
JM
5261 remote_ops.to_kill = remote_kill;
5262 remote_ops.to_load = generic_load;
c906108c
SS
5263 remote_ops.to_mourn_inferior = remote_mourn;
5264 remote_ops.to_thread_alive = remote_thread_alive;
0f71a2f6 5265 remote_ops.to_find_new_threads = remote_threads_info;
0caabb7e 5266 remote_ops.to_pid_to_str = remote_pid_to_str;
cf759d3b 5267 remote_ops.to_extra_thread_info = remote_threads_extra_info;
c906108c 5268 remote_ops.to_stop = remote_stop;
4b8a223f 5269 remote_ops.to_xfer_partial = remote_xfer_partial;
96baa820 5270 remote_ops.to_rcmd = remote_rcmd;
38691318 5271 remote_ops.to_get_thread_local_address = remote_get_thread_local_address;
c906108c 5272 remote_ops.to_stratum = process_stratum;
c5aa993b
JM
5273 remote_ops.to_has_all_memory = 1;
5274 remote_ops.to_has_memory = 1;
5275 remote_ops.to_has_stack = 1;
5276 remote_ops.to_has_registers = 1;
5277 remote_ops.to_has_execution = 1;
5278 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
5279 remote_ops.to_magic = OPS_MAGIC;
c906108c
SS
5280}
5281
5282/* Set up the extended remote vector by making a copy of the standard
5283 remote vector and adding to it. */
5284
5285static void
fba45db2 5286init_extended_remote_ops (void)
c906108c
SS
5287{
5288 extended_remote_ops = remote_ops;
5289
0f71a2f6 5290 extended_remote_ops.to_shortname = "extended-remote";
c5aa993b 5291 extended_remote_ops.to_longname =
c906108c 5292 "Extended remote serial target in gdb-specific protocol";
c5aa993b 5293 extended_remote_ops.to_doc =
c906108c
SS
5294 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5295Specify the serial device it is connected to (e.g. /dev/ttya).",
c5aa993b 5296 extended_remote_ops.to_open = extended_remote_open;
c906108c
SS
5297 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
5298 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
0f71a2f6
JM
5299}
5300
6426a772
JM
5301static int
5302remote_can_async_p (void)
5303{
23860348 5304 /* We're async whenever the serial device is. */
2cd58942 5305 return (current_target.to_async_mask_value) && serial_can_async_p (remote_desc);
6426a772
JM
5306}
5307
5308static int
5309remote_is_async_p (void)
5310{
23860348 5311 /* We're async whenever the serial device is. */
2cd58942 5312 return (current_target.to_async_mask_value) && serial_is_async_p (remote_desc);
6426a772
JM
5313}
5314
2acceee2
JM
5315/* Pass the SERIAL event on and up to the client. One day this code
5316 will be able to delay notifying the client of an event until the
23860348 5317 point where an entire packet has been received. */
2acceee2 5318
23860348
MS
5319static void (*async_client_callback) (enum inferior_event_type event_type,
5320 void *context);
2acceee2
JM
5321static void *async_client_context;
5322static serial_event_ftype remote_async_serial_handler;
5323
6426a772 5324static void
819cc324 5325remote_async_serial_handler (struct serial *scb, void *context)
6426a772 5326{
2acceee2
JM
5327 /* Don't propogate error information up to the client. Instead let
5328 the client find out about the error by querying the target. */
5329 async_client_callback (INF_REG_EVENT, async_client_context);
5330}
5331
5332static void
23860348
MS
5333remote_async (void (*callback) (enum inferior_event_type event_type,
5334 void *context), void *context)
2acceee2 5335{
ed9a39eb 5336 if (current_target.to_async_mask_value == 0)
8e65ff28 5337 internal_error (__FILE__, __LINE__,
e2e0b3e5 5338 _("Calling remote_async when async is masked"));
ed9a39eb 5339
2acceee2
JM
5340 if (callback != NULL)
5341 {
2cd58942 5342 serial_async (remote_desc, remote_async_serial_handler, NULL);
2acceee2
JM
5343 async_client_callback = callback;
5344 async_client_context = context;
5345 }
5346 else
2cd58942 5347 serial_async (remote_desc, NULL, NULL);
6426a772
JM
5348}
5349
43ff13b4
JM
5350/* Target async and target extended-async.
5351
5352 This are temporary targets, until it is all tested. Eventually
5353 async support will be incorporated int the usual 'remote'
23860348 5354 target. */
43ff13b4
JM
5355
5356static void
c2d11a7d 5357init_remote_async_ops (void)
43ff13b4
JM
5358{
5359 remote_async_ops.to_shortname = "async";
23860348
MS
5360 remote_async_ops.to_longname =
5361 "Remote serial target in async version of the gdb-specific protocol";
c5aa993b 5362 remote_async_ops.to_doc =
43ff13b4
JM
5363 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5364Specify the serial device it is connected to (e.g. /dev/ttya).";
c5aa993b
JM
5365 remote_async_ops.to_open = remote_async_open;
5366 remote_async_ops.to_close = remote_close;
6ad8ae5c
DJ
5367 remote_async_ops.to_detach = remote_detach;
5368 remote_async_ops.to_disconnect = remote_disconnect;
c5aa993b
JM
5369 remote_async_ops.to_resume = remote_async_resume;
5370 remote_async_ops.to_wait = remote_async_wait;
5371 remote_async_ops.to_fetch_registers = remote_fetch_registers;
5372 remote_async_ops.to_store_registers = remote_store_registers;
5373 remote_async_ops.to_prepare_to_store = remote_prepare_to_store;
c8e73a31 5374 remote_async_ops.deprecated_xfer_memory = remote_xfer_memory;
c5aa993b 5375 remote_async_ops.to_files_info = remote_files_info;
43ff13b4
JM
5376 remote_async_ops.to_insert_breakpoint = remote_insert_breakpoint;
5377 remote_async_ops.to_remove_breakpoint = remote_remove_breakpoint;
3c3bea1c
GS
5378 remote_async_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
5379 remote_async_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
5380 remote_async_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
5381 remote_async_ops.to_insert_watchpoint = remote_insert_watchpoint;
5382 remote_async_ops.to_remove_watchpoint = remote_remove_watchpoint;
5383 remote_async_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
5384 remote_async_ops.to_stopped_data_address = remote_stopped_data_address;
6426a772
JM
5385 remote_async_ops.to_terminal_inferior = remote_async_terminal_inferior;
5386 remote_async_ops.to_terminal_ours = remote_async_terminal_ours;
c5aa993b
JM
5387 remote_async_ops.to_kill = remote_async_kill;
5388 remote_async_ops.to_load = generic_load;
53a5351d 5389 remote_async_ops.to_mourn_inferior = remote_async_mourn;
c5aa993b
JM
5390 remote_async_ops.to_thread_alive = remote_thread_alive;
5391 remote_async_ops.to_find_new_threads = remote_threads_info;
cf759d3b
ND
5392 remote_async_ops.to_pid_to_str = remote_pid_to_str;
5393 remote_async_ops.to_extra_thread_info = remote_threads_extra_info;
43ff13b4 5394 remote_async_ops.to_stop = remote_stop;
4b8a223f 5395 remote_async_ops.to_xfer_partial = remote_xfer_partial;
96baa820 5396 remote_async_ops.to_rcmd = remote_rcmd;
c5aa993b
JM
5397 remote_async_ops.to_stratum = process_stratum;
5398 remote_async_ops.to_has_all_memory = 1;
5399 remote_async_ops.to_has_memory = 1;
5400 remote_async_ops.to_has_stack = 1;
5401 remote_async_ops.to_has_registers = 1;
5402 remote_async_ops.to_has_execution = 1;
5403 remote_async_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
6426a772
JM
5404 remote_async_ops.to_can_async_p = remote_can_async_p;
5405 remote_async_ops.to_is_async_p = remote_is_async_p;
5406 remote_async_ops.to_async = remote_async;
ed9a39eb 5407 remote_async_ops.to_async_mask_value = 1;
c5aa993b 5408 remote_async_ops.to_magic = OPS_MAGIC;
43ff13b4
JM
5409}
5410
5411/* Set up the async extended remote vector by making a copy of the standard
5412 remote vector and adding to it. */
5413
5414static void
c2d11a7d 5415init_extended_async_remote_ops (void)
43ff13b4
JM
5416{
5417 extended_async_remote_ops = remote_async_ops;
5418
5419 extended_async_remote_ops.to_shortname = "extended-async";
c5aa993b 5420 extended_async_remote_ops.to_longname =
43ff13b4 5421 "Extended remote serial target in async gdb-specific protocol";
c5aa993b 5422 extended_async_remote_ops.to_doc =
43ff13b4
JM
5423 "Use a remote computer via a serial line, using an async gdb-specific protocol.\n\
5424Specify the serial device it is connected to (e.g. /dev/ttya).",
c5aa993b 5425 extended_async_remote_ops.to_open = extended_remote_async_open;
43ff13b4
JM
5426 extended_async_remote_ops.to_create_inferior = extended_remote_async_create_inferior;
5427 extended_async_remote_ops.to_mourn_inferior = extended_remote_mourn;
5428}
5429
37a105a1
DJ
5430static struct cmd_list_element *remote_set_cmdlist;
5431static struct cmd_list_element *remote_show_cmdlist;
5432
5a2468f5 5433static void
c2d11a7d 5434set_remote_cmd (char *args, int from_tty)
5a2468f5 5435{
5a2468f5
JM
5436}
5437
d471ea57
AC
5438static void
5439show_remote_cmd (char *args, int from_tty)
5440{
37a105a1
DJ
5441 /* We can't just use cmd_show_list here, because we want to skip
5442 the redundant "show remote Z-packet". */
5443 struct cleanup *showlist_chain;
5444 struct cmd_list_element *list = remote_show_cmdlist;
5445
5446 showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist");
5447 for (; list != NULL; list = list->next)
5448 if (strcmp (list->name, "Z-packet") == 0)
5449 continue;
5450 else if (list->type == show_cmd)
5451 {
5452 struct cleanup *option_chain
5453 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
5454 ui_out_field_string (uiout, "name", list->name);
5455 ui_out_text (uiout, ": ");
5456 do_setshow_command ((char *) NULL, from_tty, list);
5457 /* Close the tuple. */
5458 do_cleanups (option_chain);
5459 }
d471ea57 5460}
5a2468f5 5461
0f71a2f6 5462static void
fba45db2 5463build_remote_gdbarch_data (void)
0f71a2f6 5464{
d696208f 5465 remote_address_size = TARGET_ADDR_BIT;
0f71a2f6
JM
5466}
5467
23860348 5468/* Saved pointer to previous owner of the new_objfile event. */
dc8acb97
MS
5469static void (*remote_new_objfile_chain) (struct objfile *);
5470
23860348 5471/* Function to be called whenever a new objfile (shlib) is detected. */
dc8acb97
MS
5472static void
5473remote_new_objfile (struct objfile *objfile)
5474{
23860348 5475 if (remote_desc != 0) /* Have a remote connection. */
dc8acb97
MS
5476 {
5477 remote_check_symbols (objfile);
5478 }
23860348 5479 /* Call predecessor on chain, if any. */
dc8acb97
MS
5480 if (remote_new_objfile_chain != 0 &&
5481 remote_desc == 0)
5482 remote_new_objfile_chain (objfile);
5483}
5484
c906108c 5485void
fba45db2 5486_initialize_remote (void)
c906108c 5487{
0f71a2f6 5488 /* architecture specific data */
23860348
MS
5489 remote_gdbarch_data_handle =
5490 gdbarch_data_register_post_init (init_remote_state);
d01949b6
AC
5491
5492 /* Old tacky stuff. NOTE: This comes after the remote protocol so
5493 that the remote protocol has been initialized. */
046a4708
AC
5494 DEPRECATED_REGISTER_GDBARCH_SWAP (remote_address_size);
5495 deprecated_register_gdbarch_swap (NULL, 0, build_remote_gdbarch_data);
0f71a2f6 5496
c906108c
SS
5497 init_remote_ops ();
5498 add_target (&remote_ops);
5499
5500 init_extended_remote_ops ();
5501 add_target (&extended_remote_ops);
cce74817 5502
43ff13b4
JM
5503 init_remote_async_ops ();
5504 add_target (&remote_async_ops);
5505
5506 init_extended_async_remote_ops ();
5507 add_target (&extended_async_remote_ops);
5508
dc8acb97 5509 /* Hook into new objfile notification. */
9a4105ab
AC
5510 remote_new_objfile_chain = deprecated_target_new_objfile_hook;
5511 deprecated_target_new_objfile_hook = remote_new_objfile;
dc8acb97 5512
c906108c
SS
5513#if 0
5514 init_remote_threadtests ();
5515#endif
5516
23860348 5517 /* set/show remote ... */
d471ea57 5518
1bedd215 5519 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, _("\
5a2468f5
JM
5520Remote protocol specific variables\n\
5521Configure various remote-protocol specific variables such as\n\
1bedd215 5522the packets being used"),
cff3e48b 5523 &remote_set_cmdlist, "set remote ",
23860348 5524 0 /* allow-unknown */, &setlist);
1bedd215 5525 add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\
5a2468f5
JM
5526Remote protocol specific variables\n\
5527Configure various remote-protocol specific variables such as\n\
1bedd215 5528the packets being used"),
cff3e48b 5529 &remote_show_cmdlist, "show remote ",
23860348 5530 0 /* allow-unknown */, &showlist);
5a2468f5 5531
1a966eab
AC
5532 add_cmd ("compare-sections", class_obscure, compare_sections_command, _("\
5533Compare section data on target to the exec file.\n\
5534Argument is a single section name (default: all loaded sections)."),
c906108c
SS
5535 &cmdlist);
5536
1a966eab
AC
5537 add_cmd ("packet", class_maintenance, packet_command, _("\
5538Send an arbitrary packet to a remote target.\n\
c906108c
SS
5539 maintenance packet TEXT\n\
5540If GDB is talking to an inferior via the GDB serial protocol, then\n\
5541this command sends the string TEXT to the inferior, and displays the\n\
5542response packet. GDB supplies the initial `$' character, and the\n\
1a966eab 5543terminating `#' character and checksum."),
c906108c
SS
5544 &maintenancelist);
5545
7915a72c
AC
5546 add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, _("\
5547Set whether to send break if interrupted."), _("\
5548Show whether to send break if interrupted."), _("\
5549If set, a break, instead of a cntrl-c, is sent to the remote target."),
2c5b56ce 5550 NULL, NULL, /* FIXME: i18n: Whether to send break if interrupted is %s. */
e707bbc2 5551 &setlist, &showlist);
c906108c 5552
23860348 5553 /* Install commands for configuring memory read/write packets. */
11cf8741 5554
1a966eab
AC
5555 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size, _("\
5556Set the maximum number of bytes per memory write packet (deprecated)."),
11cf8741 5557 &setlist);
1a966eab
AC
5558 add_cmd ("remotewritesize", no_class, show_memory_write_packet_size, _("\
5559Show the maximum number of bytes per memory write packet (deprecated)."),
11cf8741
JM
5560 &showlist);
5561 add_cmd ("memory-write-packet-size", no_class,
1a966eab
AC
5562 set_memory_write_packet_size, _("\
5563Set the maximum number of bytes per memory-write packet.\n\
5564Specify the number of bytes in a packet or 0 (zero) for the\n\
5565default packet size. The actual limit is further reduced\n\
5566dependent on the target. Specify ``fixed'' to disable the\n\
5567further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
5568 &remote_set_cmdlist);
5569 add_cmd ("memory-read-packet-size", no_class,
1a966eab
AC
5570 set_memory_read_packet_size, _("\
5571Set the maximum number of bytes per memory-read packet.\n\
5572Specify the number of bytes in a packet or 0 (zero) for the\n\
5573default packet size. The actual limit is further reduced\n\
5574dependent on the target. Specify ``fixed'' to disable the\n\
5575further restriction and ``limit'' to enable that restriction."),
11cf8741
JM
5576 &remote_set_cmdlist);
5577 add_cmd ("memory-write-packet-size", no_class,
5578 show_memory_write_packet_size,
1a966eab 5579 _("Show the maximum number of bytes per memory-write packet."),
11cf8741
JM
5580 &remote_show_cmdlist);
5581 add_cmd ("memory-read-packet-size", no_class,
5582 show_memory_read_packet_size,
1a966eab 5583 _("Show the maximum number of bytes per memory-read packet."),
11cf8741 5584 &remote_show_cmdlist);
c906108c 5585
b3f42336 5586 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class,
7915a72c
AC
5587 &remote_hw_watchpoint_limit, _("\
5588Set the maximum number of target hardware watchpoints."), _("\
5589Show the maximum number of target hardware watchpoints."), _("\
5590Specify a negative limit for unlimited."),
2c5b56ce 5591 NULL, NULL, /* FIXME: i18n: The maximum number of target hardware watchpoints is %s. */
b3f42336
AC
5592 &remote_set_cmdlist, &remote_show_cmdlist);
5593 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class,
7915a72c
AC
5594 &remote_hw_breakpoint_limit, _("\
5595Set the maximum number of target hardware breakpoints."), _("\
5596Show the maximum number of target hardware breakpoints."), _("\
5597Specify a negative limit for unlimited."),
2c5b56ce 5598 NULL, NULL, /* FIXME: i18n: The maximum number of target hardware breakpoints is %s. */
b3f42336 5599 &remote_set_cmdlist, &remote_show_cmdlist);
501eef12 5600
4d28ad1e
AC
5601 add_setshow_integer_cmd ("remoteaddresssize", class_obscure,
5602 &remote_address_size, _("\
5603Set the maximum size of the address (in bits) in a memory packet."), _("\
5604Show the maximum size of the address (in bits) in a memory packet."), NULL,
5605 NULL,
5606 NULL, /* FIXME: i18n: */
5607 &setlist, &showlist);
c906108c 5608
444abaca 5609 add_packet_config_cmd (&remote_protocol_packets[PACKET_X],
96baa820 5610 "X", "binary-download",
444abaca
DJ
5611 set_remote_protocol_packet_cmd,
5612 show_remote_protocol_packet_cmd,
d471ea57
AC
5613 &remote_set_cmdlist, &remote_show_cmdlist,
5614 1);
0f71a2f6 5615
444abaca 5616 add_packet_config_cmd (&remote_protocol_packets[PACKET_vCont],
506fb367 5617 "vCont", "verbose-resume",
444abaca
DJ
5618 set_remote_protocol_packet_cmd,
5619 show_remote_protocol_packet_cmd,
506fb367
DJ
5620 &remote_set_cmdlist, &remote_show_cmdlist,
5621 0);
5622
444abaca 5623 add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
dc8acb97 5624 "qSymbol", "symbol-lookup",
444abaca
DJ
5625 set_remote_protocol_packet_cmd,
5626 show_remote_protocol_packet_cmd,
dc8acb97
MS
5627 &remote_set_cmdlist, &remote_show_cmdlist,
5628 0);
5629
444abaca 5630 add_packet_config_cmd (&remote_protocol_packets[PACKET_P],
d471ea57 5631 "P", "set-register",
444abaca
DJ
5632 set_remote_protocol_packet_cmd,
5633 show_remote_protocol_packet_cmd,
d471ea57
AC
5634 &remote_set_cmdlist, &remote_show_cmdlist,
5635 1);
5636
444abaca 5637 add_packet_config_cmd (&remote_protocol_packets[PACKET_p],
b96ec7ac 5638 "p", "fetch-register",
444abaca
DJ
5639 set_remote_protocol_packet_cmd,
5640 show_remote_protocol_packet_cmd,
b96ec7ac
AC
5641 &remote_set_cmdlist, &remote_show_cmdlist,
5642 1);
5643
444abaca 5644 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z0],
d471ea57 5645 "Z0", "software-breakpoint",
444abaca
DJ
5646 set_remote_protocol_packet_cmd,
5647 show_remote_protocol_packet_cmd,
d471ea57
AC
5648 &remote_set_cmdlist, &remote_show_cmdlist,
5649 0);
5650
444abaca 5651 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z1],
d471ea57 5652 "Z1", "hardware-breakpoint",
444abaca
DJ
5653 set_remote_protocol_packet_cmd,
5654 show_remote_protocol_packet_cmd,
d471ea57
AC
5655 &remote_set_cmdlist, &remote_show_cmdlist,
5656 0);
5657
444abaca 5658 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z2],
d471ea57 5659 "Z2", "write-watchpoint",
444abaca
DJ
5660 set_remote_protocol_packet_cmd,
5661 show_remote_protocol_packet_cmd,
d471ea57
AC
5662 &remote_set_cmdlist, &remote_show_cmdlist,
5663 0);
5664
444abaca 5665 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z3],
d471ea57 5666 "Z3", "read-watchpoint",
444abaca
DJ
5667 set_remote_protocol_packet_cmd,
5668 show_remote_protocol_packet_cmd,
d471ea57
AC
5669 &remote_set_cmdlist, &remote_show_cmdlist,
5670 0);
5671
444abaca 5672 add_packet_config_cmd (&remote_protocol_packets[PACKET_Z4],
d471ea57 5673 "Z4", "access-watchpoint",
444abaca
DJ
5674 set_remote_protocol_packet_cmd,
5675 show_remote_protocol_packet_cmd,
d471ea57
AC
5676 &remote_set_cmdlist, &remote_show_cmdlist,
5677 0);
5678
444abaca 5679 add_packet_config_cmd (&remote_protocol_packets[PACKET_qPart_auxv],
802188a7 5680 "qPart_auxv", "read-aux-vector",
444abaca
DJ
5681 set_remote_protocol_packet_cmd,
5682 show_remote_protocol_packet_cmd,
802188a7
RM
5683 &remote_set_cmdlist, &remote_show_cmdlist,
5684 0);
5685
444abaca 5686 add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTLSAddr],
38691318 5687 "qGetTLSAddr", "get-thread-local-storage-address",
444abaca
DJ
5688 set_remote_protocol_packet_cmd,
5689 show_remote_protocol_packet_cmd,
38691318
KB
5690 &remote_set_cmdlist, &remote_show_cmdlist,
5691 0);
5692
37a105a1
DJ
5693 /* Keep the old ``set remote Z-packet ...'' working. Each individual
5694 Z sub-packet has its own set and show commands, but users may
5695 have sets to this variable in their .gdbinit files (or in their
5696 documentation). */
e9e68a56 5697 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure,
7915a72c
AC
5698 &remote_Z_packet_detect, _("\
5699Set use of remote protocol `Z' packets"), _("\
5700Show use of remote protocol `Z' packets "), _("\
3b64bf98 5701When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
7915a72c 5702packets."),
e9e68a56 5703 set_remote_protocol_Z_packet_cmd,
2c5b56ce 5704 show_remote_protocol_Z_packet_cmd, /* FIXME: i18n: Use of remote protocol `Z' packets is %s. */
e9e68a56 5705 &remote_set_cmdlist, &remote_show_cmdlist);
449092f6
CV
5706
5707 /* Eventually initialize fileio. See fileio.c */
5708 initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist);
c906108c 5709}