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