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