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