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