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