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