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