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