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