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