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