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