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