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