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