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