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