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