* remote.c (remote_threads_info, remote_current_thread): Use
[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')
c273b20f
JB
1738 /* Use strtoul here, so we'll correctly parse values whose highest
1739 bit is set. The protocol carries them as a simple series of
1740 hex digits; in the absence of a sign, strtol will see such
1741 values as positive numbers out of range for signed 'long', and
1742 return LONG_MAX to indicate an overflow. */
1743 return pid_to_ptid (strtoul (&buf[2], NULL, 16));
c906108c
SS
1744 else
1745 return oldpid;
1746}
1747
802188a7
RM
1748/* Find new threads for info threads command.
1749 * Original version, using John Metzler's thread protocol.
9d1f7ab2 1750 */
cce74817
JM
1751
1752static void
fba45db2 1753remote_find_new_threads (void)
c906108c 1754{
c5aa993b
JM
1755 remote_threadlist_iterator (remote_newthread_step, 0,
1756 CRAZY_MAX_THREADS);
39f77062
KB
1757 if (PIDGET (inferior_ptid) == MAGIC_NULL_PID) /* ack ack ack */
1758 inferior_ptid = remote_current_thread (inferior_ptid);
c906108c
SS
1759}
1760
9d1f7ab2
MS
1761/*
1762 * Find all threads for info threads command.
1763 * Uses new thread protocol contributed by Cisco.
1764 * Falls back and attempts to use the older method (above)
1765 * if the target doesn't respond to the new method.
1766 */
1767
0f71a2f6
JM
1768static void
1769remote_threads_info (void)
1770{
d01949b6
AC
1771 struct remote_state *rs = get_remote_state ();
1772 char *buf = alloca (rs->remote_packet_size);
085dd6e6 1773 char *bufp;
0f71a2f6
JM
1774 int tid;
1775
1776 if (remote_desc == 0) /* paranoia */
1777 error ("Command can only be used when connected to the remote target.");
1778
9d1f7ab2
MS
1779 if (use_threadinfo_query)
1780 {
1781 putpkt ("qfThreadInfo");
1782 bufp = buf;
d01949b6 1783 getpkt (bufp, (rs->remote_packet_size), 0);
9d1f7ab2 1784 if (bufp[0] != '\0') /* q packet recognized */
802188a7 1785 {
9d1f7ab2
MS
1786 while (*bufp++ == 'm') /* reply contains one or more TID */
1787 {
1788 do
1789 {
c273b20f
JB
1790 /* Use strtoul here, so we'll correctly parse values
1791 whose highest bit is set. The protocol carries
1792 them as a simple series of hex digits; in the
1793 absence of a sign, strtol will see such values as
1794 positive numbers out of range for signed 'long',
1795 and return LONG_MAX to indicate an overflow. */
1796 tid = strtoul (bufp, &bufp, 16);
39f77062
KB
1797 if (tid != 0 && !in_thread_list (pid_to_ptid (tid)))
1798 add_thread (pid_to_ptid (tid));
9d1f7ab2
MS
1799 }
1800 while (*bufp++ == ','); /* comma-separated list */
1801 putpkt ("qsThreadInfo");
1802 bufp = buf;
d01949b6 1803 getpkt (bufp, (rs->remote_packet_size), 0);
9d1f7ab2
MS
1804 }
1805 return; /* done */
1806 }
1807 }
1808
1809 /* Else fall back to old method based on jmetzler protocol. */
1810 use_threadinfo_query = 0;
1811 remote_find_new_threads ();
1812 return;
1813}
1814
802188a7 1815/*
9d1f7ab2
MS
1816 * Collect a descriptive string about the given thread.
1817 * The target may say anything it wants to about the thread
1818 * (typically info about its blocked / runnable state, name, etc.).
1819 * This string will appear in the info threads display.
802188a7 1820 *
9d1f7ab2
MS
1821 * Optional: targets are not required to implement this function.
1822 */
1823
1824static char *
1825remote_threads_extra_info (struct thread_info *tp)
1826{
d01949b6 1827 struct remote_state *rs = get_remote_state ();
9d1f7ab2
MS
1828 int result;
1829 int set;
1830 threadref id;
1831 struct gdb_ext_thread_info threadinfo;
1832 static char display_buf[100]; /* arbitrary... */
d01949b6 1833 char *bufp = alloca (rs->remote_packet_size);
9d1f7ab2
MS
1834 int n = 0; /* position in display_buf */
1835
1836 if (remote_desc == 0) /* paranoia */
8e65ff28
AC
1837 internal_error (__FILE__, __LINE__,
1838 "remote_threads_extra_info");
9d1f7ab2
MS
1839
1840 if (use_threadextra_query)
1841 {
39f77062 1842 sprintf (bufp, "qThreadExtraInfo,%x", PIDGET (tp->ptid));
9d1f7ab2 1843 putpkt (bufp);
d01949b6 1844 getpkt (bufp, (rs->remote_packet_size), 0);
9d1f7ab2
MS
1845 if (bufp[0] != 0)
1846 {
30559e10
MS
1847 n = min (strlen (bufp) / 2, sizeof (display_buf));
1848 result = hex2bin (bufp, display_buf, n);
1849 display_buf [result] = '\0';
9d1f7ab2
MS
1850 return display_buf;
1851 }
0f71a2f6 1852 }
9d1f7ab2
MS
1853
1854 /* If the above query fails, fall back to the old method. */
1855 use_threadextra_query = 0;
1856 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
1857 | TAG_MOREDISPLAY | TAG_DISPLAY;
39f77062 1858 int_to_threadref (&id, PIDGET (tp->ptid));
9d1f7ab2
MS
1859 if (remote_get_threadinfo (&id, set, &threadinfo))
1860 if (threadinfo.active)
0f71a2f6 1861 {
9d1f7ab2
MS
1862 if (*threadinfo.shortname)
1863 n += sprintf(&display_buf[0], " Name: %s,", threadinfo.shortname);
1864 if (*threadinfo.display)
1865 n += sprintf(&display_buf[n], " State: %s,", threadinfo.display);
1866 if (*threadinfo.more_display)
1867 n += sprintf(&display_buf[n], " Priority: %s",
1868 threadinfo.more_display);
1869
1870 if (n > 0)
c5aa993b 1871 {
9d1f7ab2
MS
1872 /* for purely cosmetic reasons, clear up trailing commas */
1873 if (',' == display_buf[n-1])
1874 display_buf[n-1] = ' ';
1875 return display_buf;
c5aa993b 1876 }
0f71a2f6 1877 }
9d1f7ab2 1878 return NULL;
0f71a2f6 1879}
9d1f7ab2 1880
c906108c 1881\f
c5aa993b 1882
c906108c
SS
1883/* Restart the remote side; this is an extended protocol operation. */
1884
1885static void
fba45db2 1886extended_remote_restart (void)
c906108c 1887{
d01949b6
AC
1888 struct remote_state *rs = get_remote_state ();
1889 char *buf = alloca (rs->remote_packet_size);
c906108c
SS
1890
1891 /* Send the restart command; for reasons I don't understand the
1892 remote side really expects a number after the "R". */
1893 buf[0] = 'R';
1894 sprintf (&buf[1], "%x", 0);
1895 putpkt (buf);
1896
1897 /* Now query for status so this looks just like we restarted
1898 gdbserver from scratch. */
1899 putpkt ("?");
d01949b6 1900 getpkt (buf, (rs->remote_packet_size), 0);
c906108c
SS
1901}
1902\f
1903/* Clean up connection to a remote debugger. */
1904
c906108c 1905static void
fba45db2 1906remote_close (int quitting)
c906108c
SS
1907{
1908 if (remote_desc)
2cd58942 1909 serial_close (remote_desc);
c906108c
SS
1910 remote_desc = NULL;
1911}
1912
1913/* Query the remote side for the text, data and bss offsets. */
1914
1915static void
fba45db2 1916get_offsets (void)
c906108c 1917{
d01949b6
AC
1918 struct remote_state *rs = get_remote_state ();
1919 char *buf = alloca (rs->remote_packet_size);
085dd6e6 1920 char *ptr;
c906108c
SS
1921 int lose;
1922 CORE_ADDR text_addr, data_addr, bss_addr;
1923 struct section_offsets *offs;
1924
1925 putpkt ("qOffsets");
1926
d01949b6 1927 getpkt (buf, (rs->remote_packet_size), 0);
c906108c
SS
1928
1929 if (buf[0] == '\000')
1930 return; /* Return silently. Stub doesn't support
1931 this command. */
1932 if (buf[0] == 'E')
1933 {
1934 warning ("Remote failure reply: %s", buf);
1935 return;
1936 }
1937
1938 /* Pick up each field in turn. This used to be done with scanf, but
1939 scanf will make trouble if CORE_ADDR size doesn't match
1940 conversion directives correctly. The following code will work
1941 with any size of CORE_ADDR. */
1942 text_addr = data_addr = bss_addr = 0;
1943 ptr = buf;
1944 lose = 0;
1945
1946 if (strncmp (ptr, "Text=", 5) == 0)
1947 {
1948 ptr += 5;
1949 /* Don't use strtol, could lose on big values. */
1950 while (*ptr && *ptr != ';')
1951 text_addr = (text_addr << 4) + fromhex (*ptr++);
1952 }
1953 else
1954 lose = 1;
1955
1956 if (!lose && strncmp (ptr, ";Data=", 6) == 0)
1957 {
1958 ptr += 6;
1959 while (*ptr && *ptr != ';')
1960 data_addr = (data_addr << 4) + fromhex (*ptr++);
1961 }
1962 else
1963 lose = 1;
1964
1965 if (!lose && strncmp (ptr, ";Bss=", 5) == 0)
1966 {
1967 ptr += 5;
1968 while (*ptr && *ptr != ';')
1969 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
1970 }
1971 else
1972 lose = 1;
1973
1974 if (lose)
1975 error ("Malformed response to offset query, %s", buf);
1976
1977 if (symfile_objfile == NULL)
1978 return;
1979
802188a7 1980 offs = ((struct section_offsets *)
a39a16c4 1981 alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections)));
802188a7 1982 memcpy (offs, symfile_objfile->section_offsets,
a39a16c4 1983 SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections));
c906108c 1984
a4c8257b 1985 offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
c906108c
SS
1986
1987 /* This is a temporary kludge to force data and bss to use the same offsets
1988 because that's what nlmconv does now. The real solution requires changes
1989 to the stub and remote.c that I don't have time to do right now. */
1990
a4c8257b
EZ
1991 offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr;
1992 offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr;
c906108c
SS
1993
1994 objfile_relocate (symfile_objfile, offs);
1995}
1996
1997/* Stub for catch_errors. */
1998
0f71a2f6 1999static int
36918e70 2000remote_start_remote_dummy (struct ui_out *uiout, void *dummy)
0f71a2f6
JM
2001{
2002 start_remote (); /* Initialize gdb process mechanisms */
36918e70
AC
2003 /* NOTE: Return something >=0. A -ve value is reserved for
2004 catch_exceptions. */
0f71a2f6
JM
2005 return 1;
2006}
2007
c906108c 2008static int
36918e70 2009remote_start_remote (struct ui_out *uiout, void *dummy)
c906108c 2010{
8edbea78 2011 immediate_quit++; /* Allow user to interrupt it */
c906108c
SS
2012
2013 /* Ack any packet which the remote side has already sent. */
2cd58942 2014 serial_write (remote_desc, "+", 1);
c906108c
SS
2015
2016 /* Let the stub know that we want it to return the thread. */
2017 set_thread (-1, 0);
2018
39f77062 2019 inferior_ptid = remote_current_thread (inferior_ptid);
c906108c
SS
2020
2021 get_offsets (); /* Get text, data & bss offsets */
2022
2023 putpkt ("?"); /* initiate a query from remote machine */
8edbea78 2024 immediate_quit--;
c906108c 2025
36918e70
AC
2026 /* NOTE: See comment above in remote_start_remote_dummy(). This
2027 function returns something >=0. */
2028 return remote_start_remote_dummy (uiout, dummy);
c906108c
SS
2029}
2030
2031/* Open a connection to a remote debugger.
2032 NAME is the filename used for communication. */
2033
2034static void
fba45db2 2035remote_open (char *name, int from_tty)
c906108c 2036{
92d1e331 2037 remote_open_1 (name, from_tty, &remote_ops, 0, 0);
c906108c
SS
2038}
2039
43ff13b4
JM
2040/* Just like remote_open, but with asynchronous support. */
2041static void
fba45db2 2042remote_async_open (char *name, int from_tty)
43ff13b4 2043{
92d1e331 2044 remote_open_1 (name, from_tty, &remote_async_ops, 0, 1);
43ff13b4
JM
2045}
2046
c906108c
SS
2047/* Open a connection to a remote debugger using the extended
2048 remote gdb protocol. NAME is the filename used for communication. */
2049
2050static void
fba45db2 2051extended_remote_open (char *name, int from_tty)
c906108c 2052{
92d1e331
DJ
2053 remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */,
2054 0 /* async_p */);
c906108c
SS
2055}
2056
43ff13b4
JM
2057/* Just like extended_remote_open, but with asynchronous support. */
2058static void
fba45db2 2059extended_remote_async_open (char *name, int from_tty)
43ff13b4 2060{
92d1e331
DJ
2061 remote_open_1 (name, from_tty, &extended_async_remote_ops,
2062 1 /*extended_p */, 1 /* async_p */);
43ff13b4
JM
2063}
2064
c906108c
SS
2065/* Generic code for opening a connection to a remote target. */
2066
d471ea57
AC
2067static void
2068init_all_packet_configs (void)
2069{
2070 int i;
2071 update_packet_config (&remote_protocol_P);
b96ec7ac 2072 update_packet_config (&remote_protocol_p);
dc8acb97 2073 update_packet_config (&remote_protocol_qSymbol);
506fb367 2074 update_packet_config (&remote_protocol_vcont);
d471ea57
AC
2075 for (i = 0; i < NR_Z_PACKET_TYPES; i++)
2076 update_packet_config (&remote_protocol_Z[i]);
2077 /* Force remote_write_bytes to check whether target supports binary
2078 downloading. */
2079 update_packet_config (&remote_protocol_binary_download);
802188a7 2080 update_packet_config (&remote_protocol_qPart_auxv);
d471ea57
AC
2081}
2082
dc8acb97
MS
2083/* Symbol look-up. */
2084
2085static void
2086remote_check_symbols (struct objfile *objfile)
2087{
d01949b6 2088 struct remote_state *rs = get_remote_state ();
dc8acb97
MS
2089 char *msg, *reply, *tmp;
2090 struct minimal_symbol *sym;
2091 int end;
2092
2093 if (remote_protocol_qSymbol.support == PACKET_DISABLE)
2094 return;
2095
d01949b6
AC
2096 msg = alloca (rs->remote_packet_size);
2097 reply = alloca (rs->remote_packet_size);
dc8acb97
MS
2098
2099 /* Invite target to request symbol lookups. */
2100
2101 putpkt ("qSymbol::");
d01949b6 2102 getpkt (reply, (rs->remote_packet_size), 0);
dc8acb97
MS
2103 packet_ok (reply, &remote_protocol_qSymbol);
2104
2105 while (strncmp (reply, "qSymbol:", 8) == 0)
2106 {
2107 tmp = &reply[8];
2108 end = hex2bin (tmp, msg, strlen (tmp) / 2);
2109 msg[end] = '\0';
2110 sym = lookup_minimal_symbol (msg, NULL, NULL);
2111 if (sym == NULL)
2112 sprintf (msg, "qSymbol::%s", &reply[8]);
2113 else
802188a7 2114 sprintf (msg, "qSymbol:%s:%s",
dc8acb97
MS
2115 paddr_nz (SYMBOL_VALUE_ADDRESS (sym)),
2116 &reply[8]);
2117 putpkt (msg);
d01949b6 2118 getpkt (reply, (rs->remote_packet_size), 0);
dc8acb97
MS
2119 }
2120}
2121
9db8d71f
DJ
2122static struct serial *
2123remote_serial_open (char *name)
2124{
2125 static int udp_warning = 0;
2126
2127 /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
2128 of in ser-tcp.c, because it is the remote protocol assuming that the
2129 serial connection is reliable and not the serial connection promising
2130 to be. */
2131 if (!udp_warning && strncmp (name, "udp:", 4) == 0)
2132 {
2133 warning ("The remote protocol may be unreliable over UDP.");
2134 warning ("Some events may be lost, rendering further debugging "
2135 "impossible.");
2136 udp_warning = 1;
2137 }
2138
2139 return serial_open (name);
2140}
2141
c906108c 2142static void
fba45db2 2143remote_open_1 (char *name, int from_tty, struct target_ops *target,
92d1e331 2144 int extended_p, int async_p)
c906108c 2145{
36918e70 2146 int ex;
d01949b6 2147 struct remote_state *rs = get_remote_state ();
c906108c 2148 if (name == 0)
22e04375
AC
2149 error ("To open a remote debug connection, you need to specify what\n"
2150 "serial device is attached to the remote system\n"
2151 "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).");
c906108c 2152
6426a772 2153 /* See FIXME above */
92d1e331
DJ
2154 if (!async_p)
2155 wait_forever_enabled_p = 1;
6426a772 2156
00cac895
DJ
2157 reopen_exec_file ();
2158 reread_symbols ();
2159
c906108c
SS
2160 target_preopen (from_tty);
2161
2162 unpush_target (target);
2163
9db8d71f 2164 remote_desc = remote_serial_open (name);
c906108c
SS
2165 if (!remote_desc)
2166 perror_with_name (name);
2167
2168 if (baud_rate != -1)
2169 {
2cd58942 2170 if (serial_setbaudrate (remote_desc, baud_rate))
c906108c 2171 {
9b74d5d3
KB
2172 /* The requested speed could not be set. Error out to
2173 top level after closing remote_desc. Take care to
2174 set remote_desc to NULL to avoid closing remote_desc
2175 more than once. */
2cd58942 2176 serial_close (remote_desc);
9b74d5d3 2177 remote_desc = NULL;
c906108c
SS
2178 perror_with_name (name);
2179 }
2180 }
2181
2cd58942 2182 serial_raw (remote_desc);
c906108c
SS
2183
2184 /* If there is something sitting in the buffer we might take it as a
2185 response to a command, which would be bad. */
2cd58942 2186 serial_flush_input (remote_desc);
c906108c
SS
2187
2188 if (from_tty)
2189 {
2190 puts_filtered ("Remote debugging using ");
2191 puts_filtered (name);
2192 puts_filtered ("\n");
2193 }
c5aa993b 2194 push_target (target); /* Switch to using remote target now */
c906108c 2195
d471ea57 2196 init_all_packet_configs ();
802188a7 2197
c5aa993b 2198 general_thread = -2;
cce74817 2199 continue_thread = -2;
c906108c 2200
9d1f7ab2
MS
2201 /* Probe for ability to use "ThreadInfo" query, as required. */
2202 use_threadinfo_query = 1;
2203 use_threadextra_query = 1;
2204
c906108c
SS
2205 /* Without this, some commands which require an active target (such
2206 as kill) won't work. This variable serves (at least) double duty
2207 as both the pid of the target process (if it has such), and as a
2208 flag indicating that a target is active. These functions should
2209 be split out into seperate variables, especially since GDB will
2210 someday have a notion of debugging several processes. */
2211
39f77062 2212 inferior_ptid = pid_to_ptid (MAGIC_NULL_PID);
92d1e331
DJ
2213
2214 if (async_p)
2215 {
2216 /* With this target we start out by owning the terminal. */
2217 remote_async_terminal_ours_p = 1;
2218
2219 /* FIXME: cagney/1999-09-23: During the initial connection it is
2220 assumed that the target is already ready and able to respond to
2221 requests. Unfortunately remote_start_remote() eventually calls
2222 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
2223 around this. Eventually a mechanism that allows
2224 wait_for_inferior() to expect/get timeouts will be
2225 implemented. */
2226 wait_forever_enabled_p = 0;
2227 }
2228
f78f6cf1
MS
2229#ifdef SOLIB_CREATE_INFERIOR_HOOK
2230 /* First delete any symbols previously loaded from shared libraries. */
2231 no_shared_libraries (NULL, 0);
2232#endif
2233
36918e70 2234 /* Start the remote connection. If error() or QUIT, discard this
165b8e33
AC
2235 target (we'd otherwise be in an inconsistent state) and then
2236 propogate the error on up the exception chain. This ensures that
2237 the caller doesn't stumble along blindly assuming that the
2238 function succeeded. The CLI doesn't have this problem but other
2239 UI's, such as MI do.
36918e70
AC
2240
2241 FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
2242 this function should return an error indication letting the
ce2826aa 2243 caller restore the previous state. Unfortunately the command
36918e70
AC
2244 ``target remote'' is directly wired to this function making that
2245 impossible. On a positive note, the CLI side of this problem has
2246 been fixed - the function set_cmd_context() makes it possible for
2247 all the ``target ....'' commands to share a common callback
2248 function. See cli-dump.c. */
2249 ex = catch_exceptions (uiout,
2250 remote_start_remote, NULL,
2251 "Couldn't establish connection to remote"
2252 " target\n",
2253 RETURN_MASK_ALL);
2254 if (ex < 0)
c906108c
SS
2255 {
2256 pop_target ();
92d1e331
DJ
2257 if (async_p)
2258 wait_forever_enabled_p = 1;
165b8e33 2259 throw_exception (ex);
c906108c
SS
2260 }
2261
92d1e331
DJ
2262 if (async_p)
2263 wait_forever_enabled_p = 1;
6426a772
JM
2264
2265 if (extended_p)
43ff13b4 2266 {
6240bebf 2267 /* Tell the remote that we are using the extended protocol. */
d01949b6 2268 char *buf = alloca (rs->remote_packet_size);
6426a772 2269 putpkt ("!");
d01949b6 2270 getpkt (buf, (rs->remote_packet_size), 0);
43ff13b4 2271 }
f78f6cf1 2272#ifdef SOLIB_CREATE_INFERIOR_HOOK
802188a7
RM
2273 /* FIXME: need a master target_open vector from which all
2274 remote_opens can be called, so that stuff like this can
6240bebf 2275 go there. Failing that, the following code must be copied
802188a7 2276 to the open function for any remote target that wants to
6240bebf 2277 support svr4 shared libraries. */
f78f6cf1
MS
2278
2279 /* Set up to detect and load shared libraries. */
6240bebf 2280 if (exec_bfd) /* No use without an exec file. */
dc8acb97
MS
2281 {
2282 SOLIB_CREATE_INFERIOR_HOOK (PIDGET (inferior_ptid));
2283 remote_check_symbols (symfile_objfile);
2284 }
6240bebf 2285#endif
43ff13b4
JM
2286}
2287
c906108c
SS
2288/* This takes a program previously attached to and detaches it. After
2289 this is done, GDB can be used to debug some other program. We
2290 better not have left any breakpoints in the target program or it'll
2291 die when it hits one. */
2292
2293static void
fba45db2 2294remote_detach (char *args, int from_tty)
c906108c 2295{
d01949b6
AC
2296 struct remote_state *rs = get_remote_state ();
2297 char *buf = alloca (rs->remote_packet_size);
c906108c
SS
2298
2299 if (args)
2300 error ("Argument given to \"detach\" when remotely debugging.");
2301
2302 /* Tell the remote target to detach. */
2303 strcpy (buf, "D");
d01949b6 2304 remote_send (buf, (rs->remote_packet_size));
c906108c 2305
6ad8ae5c
DJ
2306 /* Unregister the file descriptor from the event loop. */
2307 if (target_is_async_p ())
2308 serial_async (remote_desc, NULL, 0);
2309
cca728d0 2310 target_mourn_inferior ();
c906108c
SS
2311 if (from_tty)
2312 puts_filtered ("Ending remote debugging.\n");
2313}
2314
6ad8ae5c
DJ
2315/* Same as remote_detach, but don't send the "D" packet; just disconnect. */
2316
43ff13b4 2317static void
6ad8ae5c 2318remote_disconnect (char *args, int from_tty)
43ff13b4 2319{
d01949b6
AC
2320 struct remote_state *rs = get_remote_state ();
2321 char *buf = alloca (rs->remote_packet_size);
43ff13b4
JM
2322
2323 if (args)
2324 error ("Argument given to \"detach\" when remotely debugging.");
2325
43ff13b4 2326 /* Unregister the file descriptor from the event loop. */
ed9a39eb 2327 if (target_is_async_p ())
2cd58942 2328 serial_async (remote_desc, NULL, 0);
43ff13b4 2329
cca728d0 2330 target_mourn_inferior ();
43ff13b4
JM
2331 if (from_tty)
2332 puts_filtered ("Ending remote debugging.\n");
2333}
2334
c906108c
SS
2335/* Convert hex digit A to a number. */
2336
30559e10 2337static int
fba45db2 2338fromhex (int a)
c906108c
SS
2339{
2340 if (a >= '0' && a <= '9')
2341 return a - '0';
2342 else if (a >= 'a' && a <= 'f')
2343 return a - 'a' + 10;
2344 else if (a >= 'A' && a <= 'F')
2345 return a - 'A' + 10;
c5aa993b 2346 else
c906108c
SS
2347 error ("Reply contains invalid hex digit %d", a);
2348}
2349
30559e10
MS
2350static int
2351hex2bin (const char *hex, char *bin, int count)
2352{
2353 int i;
2354
30559e10
MS
2355 for (i = 0; i < count; i++)
2356 {
2357 if (hex[0] == 0 || hex[1] == 0)
2358 {
2359 /* Hex string is short, or of uneven length.
2360 Return the count that has been converted so far. */
2361 return i;
2362 }
2363 *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
2364 hex += 2;
2365 }
2366 return i;
2367}
2368
c906108c
SS
2369/* Convert number NIB to a hex digit. */
2370
2371static int
fba45db2 2372tohex (int nib)
c906108c
SS
2373{
2374 if (nib < 10)
c5aa993b 2375 return '0' + nib;
c906108c 2376 else
c5aa993b 2377 return 'a' + nib - 10;
c906108c 2378}
30559e10
MS
2379
2380static int
234fa6d1 2381bin2hex (const char *bin, char *hex, int count)
30559e10
MS
2382{
2383 int i;
2384 /* May use a length, or a nul-terminated string as input. */
2385 if (count == 0)
2386 count = strlen (bin);
2387
2388 for (i = 0; i < count; i++)
2389 {
2390 *hex++ = tohex ((*bin >> 4) & 0xf);
2391 *hex++ = tohex (*bin++ & 0xf);
2392 }
2393 *hex = 0;
2394 return i;
2395}
c906108c 2396\f
506fb367
DJ
2397/* Check for the availability of vCont. This function should also check
2398 the response. */
c906108c
SS
2399
2400static void
506fb367 2401remote_vcont_probe (struct remote_state *rs, char *buf)
c906108c 2402{
506fb367
DJ
2403 strcpy (buf, "vCont?");
2404 putpkt (buf);
2405 getpkt (buf, rs->remote_packet_size, 0);
c906108c 2406
506fb367
DJ
2407 /* Make sure that the features we assume are supported. */
2408 if (strncmp (buf, "vCont", 5) == 0)
2409 {
2410 char *p = &buf[5];
2411 int support_s, support_S, support_c, support_C;
2412
2413 support_s = 0;
2414 support_S = 0;
2415 support_c = 0;
2416 support_C = 0;
2417 while (p && *p == ';')
2418 {
2419 p++;
2420 if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0))
2421 support_s = 1;
2422 else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0))
2423 support_S = 1;
2424 else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0))
2425 support_c = 1;
2426 else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0))
2427 support_C = 1;
2428
2429 p = strchr (p, ';');
2430 }
c906108c 2431
506fb367
DJ
2432 /* If s, S, c, and C are not all supported, we can't use vCont. Clearing
2433 BUF will make packet_ok disable the packet. */
2434 if (!support_s || !support_S || !support_c || !support_C)
2435 buf[0] = 0;
2436 }
c906108c 2437
506fb367
DJ
2438 packet_ok (buf, &remote_protocol_vcont);
2439}
c906108c 2440
506fb367
DJ
2441/* Resume the remote inferior by using a "vCont" packet. The thread
2442 to be resumed is PTID; STEP and SIGGNAL indicate whether the
2443 resumed thread should be single-stepped and/or signalled. If PTID's
2444 PID is -1, then all threads are resumed; the thread to be stepped and/or
2445 signalled is given in the global INFERIOR_PTID. This function returns
2446 non-zero iff it resumes the inferior.
44eaed12 2447
506fb367
DJ
2448 This function issues a strict subset of all possible vCont commands at the
2449 moment. */
44eaed12 2450
506fb367
DJ
2451static int
2452remote_vcont_resume (ptid_t ptid, int step, enum target_signal siggnal)
2453{
2454 struct remote_state *rs = get_remote_state ();
2455 int pid = PIDGET (ptid);
2963ee1d 2456 char *buf = NULL, *outbuf;
506fb367 2457 struct cleanup *old_cleanup;
44eaed12 2458
506fb367
DJ
2459 buf = xmalloc (rs->remote_packet_size);
2460 old_cleanup = make_cleanup (xfree, buf);
44eaed12 2461
506fb367
DJ
2462 if (remote_protocol_vcont.support == PACKET_SUPPORT_UNKNOWN)
2463 remote_vcont_probe (rs, buf);
44eaed12 2464
506fb367
DJ
2465 if (remote_protocol_vcont.support == PACKET_DISABLE)
2466 {
2467 do_cleanups (old_cleanup);
2468 return 0;
44eaed12
C
2469 }
2470
506fb367
DJ
2471 /* If we could generate a wider range of packets, we'd have to worry
2472 about overflowing BUF. Should there be a generic
2473 "multi-part-packet" packet? */
2474
2475 if (PIDGET (inferior_ptid) == MAGIC_NULL_PID)
c906108c 2476 {
506fb367
DJ
2477 /* MAGIC_NULL_PTID means that we don't have any active threads, so we
2478 don't have any PID numbers the inferior will understand. Make sure
2479 to only send forms that do not specify a PID. */
2480 if (step && siggnal != TARGET_SIGNAL_0)
2963ee1d 2481 outbuf = xstrprintf ("vCont;S%02x", siggnal);
506fb367 2482 else if (step)
2963ee1d 2483 outbuf = xstrprintf ("vCont;s");
506fb367 2484 else if (siggnal != TARGET_SIGNAL_0)
2963ee1d 2485 outbuf = xstrprintf ("vCont;C%02x", siggnal);
506fb367 2486 else
2963ee1d 2487 outbuf = xstrprintf ("vCont;c");
506fb367
DJ
2488 }
2489 else if (pid == -1)
2490 {
2491 /* Resume all threads, with preference for INFERIOR_PTID. */
2492 if (step && siggnal != TARGET_SIGNAL_0)
2963ee1d
DJ
2493 outbuf = xstrprintf ("vCont;S%02x:%x;c", siggnal,
2494 PIDGET (inferior_ptid));
506fb367 2495 else if (step)
2963ee1d 2496 outbuf = xstrprintf ("vCont;s:%x;c", PIDGET (inferior_ptid));
506fb367 2497 else if (siggnal != TARGET_SIGNAL_0)
2963ee1d
DJ
2498 outbuf = xstrprintf ("vCont;C%02x:%x;c", siggnal,
2499 PIDGET (inferior_ptid));
506fb367 2500 else
2963ee1d 2501 outbuf = xstrprintf ("vCont;c");
c906108c
SS
2502 }
2503 else
506fb367
DJ
2504 {
2505 /* Scheduler locking; resume only PTID. */
2506 if (step && siggnal != TARGET_SIGNAL_0)
2963ee1d 2507 outbuf = xstrprintf ("vCont;S%02x:%x", siggnal, pid);
506fb367 2508 else if (step)
2963ee1d 2509 outbuf = xstrprintf ("vCont;s:%x", pid);
506fb367 2510 else if (siggnal != TARGET_SIGNAL_0)
2963ee1d 2511 outbuf = xstrprintf ("vCont;C%02x:%x", siggnal, pid);
506fb367 2512 else
2963ee1d 2513 outbuf = xstrprintf ("vCont;c:%x", pid);
506fb367 2514 }
c906108c 2515
2963ee1d
DJ
2516 gdb_assert (outbuf && strlen (outbuf) < rs->remote_packet_size);
2517 make_cleanup (xfree, outbuf);
2518
2519 putpkt (outbuf);
506fb367
DJ
2520
2521 do_cleanups (old_cleanup);
2522
2523 return 1;
c906108c 2524}
43ff13b4 2525
506fb367
DJ
2526/* Tell the remote machine to resume. */
2527
2528static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
2529
2530static int last_sent_step;
2531
43ff13b4 2532static void
506fb367 2533remote_resume (ptid_t ptid, int step, enum target_signal siggnal)
43ff13b4 2534{
d01949b6
AC
2535 struct remote_state *rs = get_remote_state ();
2536 char *buf = alloca (rs->remote_packet_size);
39f77062 2537 int pid = PIDGET (ptid);
44eaed12 2538 char *p;
43ff13b4 2539
43ff13b4
JM
2540 last_sent_signal = siggnal;
2541 last_sent_step = step;
2542
2543 /* A hook for when we need to do something at the last moment before
2544 resumption. */
9a4105ab
AC
2545 if (deprecated_target_resume_hook)
2546 (*deprecated_target_resume_hook) ();
43ff13b4 2547
506fb367
DJ
2548 /* The vCont packet doesn't need to specify threads via Hc. */
2549 if (remote_vcont_resume (ptid, step, siggnal))
2550 return;
2551
2552 /* All other supported resume packets do use Hc, so call set_thread. */
2553 if (pid == -1)
2554 set_thread (0, 0); /* run any thread */
2555 else
2556 set_thread (pid, 0); /* run this thread */
2557
43ff13b4
JM
2558 if (siggnal != TARGET_SIGNAL_0)
2559 {
2560 buf[0] = step ? 'S' : 'C';
c5aa993b 2561 buf[1] = tohex (((int) siggnal >> 4) & 0xf);
506fb367 2562 buf[2] = tohex (((int) siggnal) & 0xf);
43ff13b4
JM
2563 buf[3] = '\0';
2564 }
2565 else
c5aa993b 2566 strcpy (buf, step ? "s" : "c");
506fb367 2567
44eaed12 2568 putpkt (buf);
506fb367
DJ
2569}
2570
2571/* Same as remote_resume, but with async support. */
2572static void
2573remote_async_resume (ptid_t ptid, int step, enum target_signal siggnal)
2574{
2575 remote_resume (ptid, step, siggnal);
43ff13b4 2576
2acceee2
JM
2577 /* We are about to start executing the inferior, let's register it
2578 with the event loop. NOTE: this is the one place where all the
2579 execution commands end up. We could alternatively do this in each
2580 of the execution commands in infcmd.c.*/
2581 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
2582 into infcmd.c in order to allow inferior function calls to work
2583 NOT asynchronously. */
362646f5 2584 if (target_can_async_p ())
2acceee2
JM
2585 target_async (inferior_event_handler, 0);
2586 /* Tell the world that the target is now executing. */
2587 /* FIXME: cagney/1999-09-23: Is it the targets responsibility to set
2588 this? Instead, should the client of target just assume (for
2589 async targets) that the target is going to start executing? Is
2590 this information already found in the continuation block? */
ed9a39eb 2591 if (target_is_async_p ())
2acceee2 2592 target_executing = 1;
43ff13b4 2593}
c906108c 2594\f
43ff13b4
JM
2595
2596/* Set up the signal handler for SIGINT, while the target is
2597 executing, ovewriting the 'regular' SIGINT signal handler. */
2598static void
fba45db2 2599initialize_sigint_signal_handler (void)
43ff13b4 2600{
c5aa993b 2601 sigint_remote_token =
43ff13b4
JM
2602 create_async_signal_handler (async_remote_interrupt, NULL);
2603 signal (SIGINT, handle_remote_sigint);
2604}
2605
2606/* Signal handler for SIGINT, while the target is executing. */
2607static void
fba45db2 2608handle_remote_sigint (int sig)
43ff13b4
JM
2609{
2610 signal (sig, handle_remote_sigint_twice);
c5aa993b 2611 sigint_remote_twice_token =
43ff13b4
JM
2612 create_async_signal_handler (async_remote_interrupt_twice, NULL);
2613 mark_async_signal_handler_wrapper (sigint_remote_token);
2614}
2615
2616/* Signal handler for SIGINT, installed after SIGINT has already been
2617 sent once. It will take effect the second time that the user sends
2618 a ^C. */
2619static void
fba45db2 2620handle_remote_sigint_twice (int sig)
43ff13b4
JM
2621{
2622 signal (sig, handle_sigint);
c5aa993b 2623 sigint_remote_twice_token =
2df3850c 2624 create_async_signal_handler (inferior_event_handler_wrapper, NULL);
43ff13b4
JM
2625 mark_async_signal_handler_wrapper (sigint_remote_twice_token);
2626}
2627
6426a772 2628/* Perform the real interruption of the target execution, in response
43ff13b4 2629 to a ^C. */
c5aa993b 2630static void
fba45db2 2631async_remote_interrupt (gdb_client_data arg)
43ff13b4
JM
2632{
2633 if (remote_debug)
2634 fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
2635
2636 target_stop ();
2637}
2638
2639/* Perform interrupt, if the first attempt did not succeed. Just give
2640 up on the target alltogether. */
2df3850c 2641void
fba45db2 2642async_remote_interrupt_twice (gdb_client_data arg)
43ff13b4 2643{
2df3850c
JM
2644 if (remote_debug)
2645 fprintf_unfiltered (gdb_stdlog, "remote_interrupt_twice called\n");
6426a772
JM
2646 /* Do something only if the target was not killed by the previous
2647 cntl-C. */
2648 if (target_executing)
2649 {
2650 interrupt_query ();
2651 signal (SIGINT, handle_remote_sigint);
2652 }
43ff13b4
JM
2653}
2654
2655/* Reinstall the usual SIGINT handlers, after the target has
2656 stopped. */
6426a772
JM
2657static void
2658cleanup_sigint_signal_handler (void *dummy)
43ff13b4
JM
2659{
2660 signal (SIGINT, handle_sigint);
2661 if (sigint_remote_twice_token)
c2c6d25f 2662 delete_async_signal_handler ((struct async_signal_handler **) & sigint_remote_twice_token);
43ff13b4 2663 if (sigint_remote_token)
c2c6d25f 2664 delete_async_signal_handler ((struct async_signal_handler **) & sigint_remote_token);
43ff13b4
JM
2665}
2666
c906108c
SS
2667/* Send ^C to target to halt it. Target will respond, and send us a
2668 packet. */
507f3c78 2669static void (*ofunc) (int);
c906108c 2670
7a292a7a
SS
2671/* The command line interface's stop routine. This function is installed
2672 as a signal handler for SIGINT. The first time a user requests a
2673 stop, we call remote_stop to send a break or ^C. If there is no
2674 response from the target (it didn't stop when the user requested it),
2675 we ask the user if he'd like to detach from the target. */
c906108c 2676static void
fba45db2 2677remote_interrupt (int signo)
c906108c 2678{
7a292a7a
SS
2679 /* If this doesn't work, try more severe steps. */
2680 signal (signo, remote_interrupt_twice);
2681
2682 if (remote_debug)
0f71a2f6 2683 fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n");
7a292a7a
SS
2684
2685 target_stop ();
2686}
2687
2688/* The user typed ^C twice. */
2689
2690static void
fba45db2 2691remote_interrupt_twice (int signo)
7a292a7a
SS
2692{
2693 signal (signo, ofunc);
2694 interrupt_query ();
c906108c
SS
2695 signal (signo, remote_interrupt);
2696}
7a292a7a
SS
2697
2698/* This is the generic stop called via the target vector. When a target
2699 interrupt is requested, either by the command line or the GUI, we
2700 will eventually end up here. */
c906108c 2701static void
fba45db2 2702remote_stop (void)
c906108c 2703{
7a292a7a
SS
2704 /* Send a break or a ^C, depending on user preference. */
2705 if (remote_debug)
0f71a2f6 2706 fprintf_unfiltered (gdb_stdlog, "remote_stop called\n");
c906108c 2707
7a292a7a 2708 if (remote_break)
2cd58942 2709 serial_send_break (remote_desc);
c906108c 2710 else
2cd58942 2711 serial_write (remote_desc, "\003", 1);
c906108c
SS
2712}
2713
2714/* Ask the user what to do when an interrupt is received. */
2715
2716static void
fba45db2 2717interrupt_query (void)
c906108c
SS
2718{
2719 target_terminal_ours ();
2720
2721 if (query ("Interrupted while waiting for the program.\n\
2722Give up (and stop debugging it)? "))
2723 {
2724 target_mourn_inferior ();
b5a2688f 2725 throw_exception (RETURN_QUIT);
c906108c
SS
2726 }
2727
2728 target_terminal_inferior ();
2729}
2730
6426a772
JM
2731/* Enable/disable target terminal ownership. Most targets can use
2732 terminal groups to control terminal ownership. Remote targets are
2733 different in that explicit transfer of ownership to/from GDB/target
2734 is required. */
2735
2736static void
2737remote_async_terminal_inferior (void)
2738{
2739 /* FIXME: cagney/1999-09-27: Shouldn't need to test for
2740 sync_execution here. This function should only be called when
2741 GDB is resuming the inferior in the forground. A background
2742 resume (``run&'') should leave GDB in control of the terminal and
2743 consequently should not call this code. */
2744 if (!sync_execution)
2745 return;
2746 /* FIXME: cagney/1999-09-27: Closely related to the above. Make
2747 calls target_terminal_*() idenpotent. The event-loop GDB talking
2748 to an asynchronous target with a synchronous command calls this
2749 function from both event-top.c and infrun.c/infcmd.c. Once GDB
2750 stops trying to transfer the terminal to the target when it
2751 shouldn't this guard can go away. */
2752 if (!remote_async_terminal_ours_p)
2753 return;
2754 delete_file_handler (input_fd);
2755 remote_async_terminal_ours_p = 0;
2756 initialize_sigint_signal_handler ();
2757 /* NOTE: At this point we could also register our selves as the
2758 recipient of all input. Any characters typed could then be
2759 passed on down to the target. */
2760}
2761
2762static void
2763remote_async_terminal_ours (void)
2764{
2765 /* See FIXME in remote_async_terminal_inferior. */
2766 if (!sync_execution)
2767 return;
2768 /* See FIXME in remote_async_terminal_inferior. */
2769 if (remote_async_terminal_ours_p)
2770 return;
2771 cleanup_sigint_signal_handler (NULL);
2772 add_file_handler (input_fd, stdin_event_handler, 0);
2773 remote_async_terminal_ours_p = 1;
2774}
2775
c906108c
SS
2776/* If nonzero, ignore the next kill. */
2777
2778int kill_kludge;
2779
2780void
917317f4 2781remote_console_output (char *msg)
c906108c
SS
2782{
2783 char *p;
2784
c5aa993b 2785 for (p = msg; p[0] && p[1]; p += 2)
c906108c
SS
2786 {
2787 char tb[2];
2788 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
2789 tb[0] = c;
2790 tb[1] = 0;
43ff13b4 2791 fputs_unfiltered (tb, gdb_stdtarg);
c906108c 2792 }
917317f4 2793 gdb_flush (gdb_stdtarg);
c906108c
SS
2794}
2795
0f71a2f6
JM
2796/* Wait until the remote machine stops, then return,
2797 storing status in STATUS just as `wait' would.
802188a7 2798 Returns "pid", which in the case of a multi-threaded
0f71a2f6 2799 remote OS, is the thread-id. */
c906108c 2800
39f77062
KB
2801static ptid_t
2802remote_wait (ptid_t ptid, struct target_waitstatus *status)
c906108c 2803{
d01949b6
AC
2804 struct remote_state *rs = get_remote_state ();
2805 unsigned char *buf = alloca (rs->remote_packet_size);
b2dd6311 2806 ULONGEST thread_num = -1;
3c3bea1c 2807 ULONGEST addr;
c906108c
SS
2808
2809 status->kind = TARGET_WAITKIND_EXITED;
2810 status->value.integer = 0;
2811
2812 while (1)
2813 {
2814 unsigned char *p;
2815
c906108c 2816 ofunc = signal (SIGINT, remote_interrupt);
d01949b6 2817 getpkt (buf, (rs->remote_packet_size), 1);
c906108c
SS
2818 signal (SIGINT, ofunc);
2819
2820 /* This is a hook for when we need to do something (perhaps the
c5aa993b 2821 collection of trace data) every time the target stops. */
9a4105ab
AC
2822 if (deprecated_target_wait_loop_hook)
2823 (*deprecated_target_wait_loop_hook) ();
c906108c 2824
3c3bea1c
GS
2825 remote_stopped_by_watchpoint_p = 0;
2826
c906108c
SS
2827 switch (buf[0])
2828 {
2829 case 'E': /* Error of some sort */
2830 warning ("Remote failure reply: %s", buf);
2831 continue;
449092f6
CV
2832 case 'F': /* File-I/O request */
2833 remote_fileio_request (buf);
2834 continue;
c906108c
SS
2835 case 'T': /* Status with PC, SP, FP, ... */
2836 {
2837 int i;
d9d9c31f 2838 char regs[MAX_REGISTER_SIZE];
c906108c
SS
2839
2840 /* Expedited reply, containing Signal, {regno, reg} repeat */
2841 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
c5aa993b
JM
2842 ss = signal number
2843 n... = register number
2844 r... = register contents
2845 */
c906108c
SS
2846 p = &buf[3]; /* after Txx */
2847
2848 while (*p)
2849 {
2850 unsigned char *p1;
2851 char *p_temp;
97345198 2852 int fieldsize;
3c3bea1c
GS
2853 LONGEST pnum = 0;
2854
2855 /* If the packet contains a register number save it in pnum
802188a7 2856 and set p1 to point to the character following it.
3c3bea1c 2857 Otherwise p1 points to p. */
c906108c 2858
3c3bea1c
GS
2859 /* If this packet is an awatch packet, don't parse the 'a'
2860 as a register number. */
2861
2862 if (strncmp (p, "awatch", strlen("awatch")) != 0)
2863 {
2864 /* Read the ``P'' register number. */
2865 pnum = strtol (p, &p_temp, 16);
2866 p1 = (unsigned char *) p_temp;
2867 }
802188a7 2868 else
3c3bea1c 2869 p1 = p;
c906108c 2870
c5aa993b 2871 if (p1 == p) /* No register number present here */
c906108c 2872 {
3c3bea1c 2873 p1 = (unsigned char *) strchr (p, ':');
c906108c
SS
2874 if (p1 == NULL)
2875 warning ("Malformed packet(a) (missing colon): %s\n\
2876Packet: '%s'\n",
2877 p, buf);
3c3bea1c 2878 if (strncmp (p, "thread", p1 - p) == 0)
c906108c
SS
2879 {
2880 p_temp = unpack_varlen_hex (++p1, &thread_num);
2881 record_currthread (thread_num);
2882 p = (unsigned char *) p_temp;
2883 }
3c3bea1c
GS
2884 else if ((strncmp (p, "watch", p1 - p) == 0)
2885 || (strncmp (p, "rwatch", p1 - p) == 0)
2886 || (strncmp (p, "awatch", p1 - p) == 0))
2887 {
2888 remote_stopped_by_watchpoint_p = 1;
2889 p = unpack_varlen_hex (++p1, &addr);
2890 remote_watch_data_address = (CORE_ADDR)addr;
2891 }
2892 else
2893 {
2894 /* Silently skip unknown optional info. */
2895 p_temp = strchr (p1 + 1, ';');
2896 if (p_temp)
2897 p = (unsigned char *) p_temp;
2898 }
c906108c
SS
2899 }
2900 else
2901 {
ad10f812 2902 struct packet_reg *reg = packet_reg_from_pnum (rs, pnum);
c906108c
SS
2903 p = p1;
2904
2905 if (*p++ != ':')
3fcb8548
AC
2906 error ("Malformed packet(b) (missing colon): %s\nPacket: '%s'\n",
2907 p, buf);
c906108c 2908
ad10f812 2909 if (reg == NULL)
3fcb8548
AC
2910 error ("Remote sent bad register number %s: %s\nPacket: '%s'\n",
2911 phex_nz (pnum, 0), p, buf);
c906108c 2912
3acba339 2913 fieldsize = hex2bin (p, regs, register_size (current_gdbarch, reg->regnum));
97345198 2914 p += 2 * fieldsize;
3acba339 2915 if (fieldsize < register_size (current_gdbarch, reg->regnum))
30559e10 2916 warning ("Remote reply is too short: %s", buf);
23a6d369 2917 regcache_raw_supply (current_regcache, reg->regnum, regs);
c906108c
SS
2918 }
2919
2920 if (*p++ != ';')
3fcb8548 2921 error ("Remote register badly formatted: %s\nhere: %s", buf, p);
c906108c
SS
2922 }
2923 }
2924 /* fall through */
2925 case 'S': /* Old style status, just signal only */
2926 status->kind = TARGET_WAITKIND_STOPPED;
2927 status->value.sig = (enum target_signal)
2928 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
2929
0f71a2f6
JM
2930 if (buf[3] == 'p')
2931 {
0f71a2f6
JM
2932 thread_num = strtol ((const char *) &buf[4], NULL, 16);
2933 record_currthread (thread_num);
2934 }
c906108c
SS
2935 goto got_status;
2936 case 'W': /* Target exited */
2937 {
2938 /* The remote process exited. */
2939 status->kind = TARGET_WAITKIND_EXITED;
2940 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
2941 goto got_status;
2942 }
2943 case 'X':
2944 status->kind = TARGET_WAITKIND_SIGNALLED;
2945 status->value.sig = (enum target_signal)
2946 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
2947 kill_kludge = 1;
2948
2949 goto got_status;
2950 case 'O': /* Console output */
2951 remote_console_output (buf + 1);
2952 continue;
2953 case '\0':
2954 if (last_sent_signal != TARGET_SIGNAL_0)
2955 {
2956 /* Zero length reply means that we tried 'S' or 'C' and
c5aa993b 2957 the remote system doesn't support it. */
c906108c
SS
2958 target_terminal_ours_for_output ();
2959 printf_filtered
2960 ("Can't send signals to this remote system. %s not sent.\n",
2961 target_signal_to_name (last_sent_signal));
2962 last_sent_signal = TARGET_SIGNAL_0;
2963 target_terminal_inferior ();
2964
2965 strcpy ((char *) buf, last_sent_step ? "s" : "c");
2966 putpkt ((char *) buf);
2967 continue;
2968 }
2969 /* else fallthrough */
2970 default:
2971 warning ("Invalid remote reply: %s", buf);
2972 continue;
2973 }
2974 }
c5aa993b 2975got_status:
c906108c
SS
2976 if (thread_num != -1)
2977 {
39f77062 2978 return pid_to_ptid (thread_num);
c906108c 2979 }
39f77062 2980 return inferior_ptid;
c906108c
SS
2981}
2982
43ff13b4 2983/* Async version of remote_wait. */
39f77062
KB
2984static ptid_t
2985remote_async_wait (ptid_t ptid, struct target_waitstatus *status)
43ff13b4 2986{
d01949b6
AC
2987 struct remote_state *rs = get_remote_state ();
2988 unsigned char *buf = alloca (rs->remote_packet_size);
b2dd6311 2989 ULONGEST thread_num = -1;
3c3bea1c 2990 ULONGEST addr;
43ff13b4
JM
2991
2992 status->kind = TARGET_WAITKIND_EXITED;
2993 status->value.integer = 0;
2994
3c3bea1c
GS
2995 remote_stopped_by_watchpoint_p = 0;
2996
43ff13b4
JM
2997 while (1)
2998 {
2999 unsigned char *p;
c5aa993b 3000
ed9a39eb 3001 if (!target_is_async_p ())
43ff13b4 3002 ofunc = signal (SIGINT, remote_interrupt);
6426a772
JM
3003 /* FIXME: cagney/1999-09-27: If we're in async mode we should
3004 _never_ wait for ever -> test on target_is_async_p().
3005 However, before we do that we need to ensure that the caller
3006 knows how to take the target into/out of async mode. */
d01949b6 3007 getpkt (buf, (rs->remote_packet_size), wait_forever_enabled_p);
ed9a39eb 3008 if (!target_is_async_p ())
43ff13b4
JM
3009 signal (SIGINT, ofunc);
3010
3011 /* This is a hook for when we need to do something (perhaps the
c5aa993b 3012 collection of trace data) every time the target stops. */
9a4105ab
AC
3013 if (deprecated_target_wait_loop_hook)
3014 (*deprecated_target_wait_loop_hook) ();
43ff13b4
JM
3015
3016 switch (buf[0])
3017 {
3018 case 'E': /* Error of some sort */
3019 warning ("Remote failure reply: %s", buf);
3020 continue;
449092f6
CV
3021 case 'F': /* File-I/O request */
3022 remote_fileio_request (buf);
3023 continue;
43ff13b4
JM
3024 case 'T': /* Status with PC, SP, FP, ... */
3025 {
3026 int i;
d9d9c31f 3027 char regs[MAX_REGISTER_SIZE];
43ff13b4
JM
3028
3029 /* Expedited reply, containing Signal, {regno, reg} repeat */
3030 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
c5aa993b
JM
3031 ss = signal number
3032 n... = register number
3033 r... = register contents
3034 */
43ff13b4
JM
3035 p = &buf[3]; /* after Txx */
3036
3037 while (*p)
3038 {
3039 unsigned char *p1;
3040 char *p_temp;
6c3f2dbf 3041 int fieldsize;
3c3bea1c 3042 long pnum = 0;
43ff13b4 3043
3c3bea1c 3044 /* If the packet contains a register number, save it in pnum
802188a7 3045 and set p1 to point to the character following it.
3c3bea1c
GS
3046 Otherwise p1 points to p. */
3047
3048 /* If this packet is an awatch packet, don't parse the 'a'
3049 as a register number. */
802188a7 3050
3c3bea1c
GS
3051 if (!strncmp (p, "awatch", strlen ("awatch")) != 0)
3052 {
3053 /* Read the register number. */
3054 pnum = strtol (p, &p_temp, 16);
3055 p1 = (unsigned char *) p_temp;
3056 }
802188a7 3057 else
3c3bea1c 3058 p1 = p;
43ff13b4 3059
c5aa993b 3060 if (p1 == p) /* No register number present here */
43ff13b4 3061 {
3c3bea1c 3062 p1 = (unsigned char *) strchr (p, ':');
43ff13b4 3063 if (p1 == NULL)
3fcb8548
AC
3064 error ("Malformed packet(a) (missing colon): %s\nPacket: '%s'\n",
3065 p, buf);
3c3bea1c 3066 if (strncmp (p, "thread", p1 - p) == 0)
43ff13b4
JM
3067 {
3068 p_temp = unpack_varlen_hex (++p1, &thread_num);
3069 record_currthread (thread_num);
3070 p = (unsigned char *) p_temp;
3071 }
3c3bea1c
GS
3072 else if ((strncmp (p, "watch", p1 - p) == 0)
3073 || (strncmp (p, "rwatch", p1 - p) == 0)
3074 || (strncmp (p, "awatch", p1 - p) == 0))
3075 {
3076 remote_stopped_by_watchpoint_p = 1;
3077 p = unpack_varlen_hex (++p1, &addr);
3078 remote_watch_data_address = (CORE_ADDR)addr;
3079 }
3080 else
3081 {
3082 /* Silently skip unknown optional info. */
3083 p_temp = (unsigned char *) strchr (p1 + 1, ';');
3084 if (p_temp)
3085 p = p_temp;
3086 }
43ff13b4 3087 }
802188a7 3088
43ff13b4
JM
3089 else
3090 {
ad10f812 3091 struct packet_reg *reg = packet_reg_from_pnum (rs, pnum);
43ff13b4 3092 p = p1;
43ff13b4 3093 if (*p++ != ':')
3fcb8548
AC
3094 error ("Malformed packet(b) (missing colon): %s\nPacket: '%s'\n",
3095 p, buf);
43ff13b4 3096
ad10f812 3097 if (reg == NULL)
3fcb8548
AC
3098 error ("Remote sent bad register number %ld: %s\nPacket: '%s'\n",
3099 pnum, p, buf);
43ff13b4 3100
3acba339 3101 fieldsize = hex2bin (p, regs, register_size (current_gdbarch, reg->regnum));
6c3f2dbf 3102 p += 2 * fieldsize;
3acba339 3103 if (fieldsize < register_size (current_gdbarch, reg->regnum))
30559e10 3104 warning ("Remote reply is too short: %s", buf);
23a6d369 3105 regcache_raw_supply (current_regcache, reg->regnum, regs);
43ff13b4
JM
3106 }
3107
3108 if (*p++ != ';')
0a2cfde4
AC
3109 error ("Remote register badly formatted: %s\nhere: %s",
3110 buf, p);
43ff13b4
JM
3111 }
3112 }
3113 /* fall through */
3114 case 'S': /* Old style status, just signal only */
3115 status->kind = TARGET_WAITKIND_STOPPED;
3116 status->value.sig = (enum target_signal)
3117 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
3118
3119 if (buf[3] == 'p')
3120 {
43ff13b4
JM
3121 thread_num = strtol ((const char *) &buf[4], NULL, 16);
3122 record_currthread (thread_num);
3123 }
43ff13b4 3124 goto got_status;
43ff13b4
JM
3125 case 'W': /* Target exited */
3126 {
3127 /* The remote process exited. */
3128 status->kind = TARGET_WAITKIND_EXITED;
3129 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
3130 goto got_status;
3131 }
3132 case 'X':
3133 status->kind = TARGET_WAITKIND_SIGNALLED;
3134 status->value.sig = (enum target_signal)
3135 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
3136 kill_kludge = 1;
3137
3138 goto got_status;
3139 case 'O': /* Console output */
3140 remote_console_output (buf + 1);
c4093a6a
JM
3141 /* Return immediately to the event loop. The event loop will
3142 still be waiting on the inferior afterwards. */
3143 status->kind = TARGET_WAITKIND_IGNORE;
3144 goto got_status;
43ff13b4
JM
3145 case '\0':
3146 if (last_sent_signal != TARGET_SIGNAL_0)
3147 {
3148 /* Zero length reply means that we tried 'S' or 'C' and
c5aa993b 3149 the remote system doesn't support it. */
43ff13b4
JM
3150 target_terminal_ours_for_output ();
3151 printf_filtered
3152 ("Can't send signals to this remote system. %s not sent.\n",
3153 target_signal_to_name (last_sent_signal));
3154 last_sent_signal = TARGET_SIGNAL_0;
3155 target_terminal_inferior ();
3156
3157 strcpy ((char *) buf, last_sent_step ? "s" : "c");
3158 putpkt ((char *) buf);
3159 continue;
3160 }
3161 /* else fallthrough */
3162 default:
3163 warning ("Invalid remote reply: %s", buf);
3164 continue;
3165 }
3166 }
c5aa993b 3167got_status:
43ff13b4
JM
3168 if (thread_num != -1)
3169 {
39f77062 3170 return pid_to_ptid (thread_num);
43ff13b4 3171 }
39f77062 3172 return inferior_ptid;
43ff13b4
JM
3173}
3174
c906108c
SS
3175/* Number of bytes of registers this stub implements. */
3176
3177static int register_bytes_found;
3178
3179/* Read the remote registers into the block REGS. */
ad10f812 3180/* Currently we just read all the registers, so we don't use regnum. */
c906108c 3181
b96ec7ac
AC
3182static int
3183fetch_register_using_p (int regnum)
3184{
3185 struct remote_state *rs = get_remote_state ();
3186 char *buf = alloca (rs->remote_packet_size), *p;
3187 char regp[MAX_REGISTER_SIZE];
3188 int i;
3189
fcad0fa4
JB
3190 p = buf;
3191 *p++ = 'p';
3192 p += hexnumstr (p, regnum);
3193 *p++ = '\0';
b96ec7ac
AC
3194 remote_send (buf, rs->remote_packet_size);
3195 if (buf[0] != 0 && buf[0] != 'E') {
3196 p = buf;
3197 i = 0;
3198 while (p[0] != 0) {
3199 if (p[1] == 0) {
3200 error("fetch_register_using_p: early buf termination");
3201 return 0;
3202 }
3203 regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]);
3204 p += 2;
3205 }
3206 regcache_raw_supply (current_regcache, regnum, regp);
3207 return 1;
3208 }
3209
3210 return 0;
3211}
3212
c906108c 3213static void
ad10f812 3214remote_fetch_registers (int regnum)
c906108c 3215{
d01949b6
AC
3216 struct remote_state *rs = get_remote_state ();
3217 char *buf = alloca (rs->remote_packet_size);
c906108c
SS
3218 int i;
3219 char *p;
ad10f812 3220 char *regs = alloca (rs->sizeof_g_packet);
c906108c 3221
39f77062 3222 set_thread (PIDGET (inferior_ptid), 1);
c906108c 3223
b323314b
AC
3224 if (regnum >= 0)
3225 {
3226 struct packet_reg *reg = packet_reg_from_regnum (rs, regnum);
3227 gdb_assert (reg != NULL);
3228 if (!reg->in_g_packet)
3229 internal_error (__FILE__, __LINE__,
3230 "Attempt to fetch a non G-packet register when this "
3231 "remote.c does not support the p-packet.");
3232 }
b96ec7ac
AC
3233 switch (remote_protocol_p.support)
3234 {
3235 case PACKET_DISABLE:
3236 break;
3237 case PACKET_ENABLE:
3238 if (fetch_register_using_p (regnum))
3239 return;
3240 else
3241 error ("Protocol error: p packet not recognized by stub");
3242 case PACKET_SUPPORT_UNKNOWN:
3243 if (fetch_register_using_p (regnum))
3244 {
3245 /* The stub recognized the 'p' packet. Remember this. */
3246 remote_protocol_p.support = PACKET_ENABLE;
3247 return;
3248 }
3249 else
3250 {
3251 /* The stub does not support the 'P' packet. Use 'G'
3252 instead, and don't try using 'P' in the future (it
3253 will just waste our time). */
3254 remote_protocol_p.support = PACKET_DISABLE;
3255 break;
3256 }
3257 }
b323314b 3258
c906108c 3259 sprintf (buf, "g");
d01949b6 3260 remote_send (buf, (rs->remote_packet_size));
c906108c 3261
11cf8741
JM
3262 /* Save the size of the packet sent to us by the target. Its used
3263 as a heuristic when determining the max size of packets that the
3264 target can safely receive. */
d01949b6
AC
3265 if ((rs->actual_register_packet_size) == 0)
3266 (rs->actual_register_packet_size) = strlen (buf);
c906108c
SS
3267
3268 /* Unimplemented registers read as all bits zero. */
ad10f812 3269 memset (regs, 0, rs->sizeof_g_packet);
c906108c
SS
3270
3271 /* We can get out of synch in various cases. If the first character
3272 in the buffer is not a hex character, assume that has happened
3273 and try to fetch another packet to read. */
3274 while ((buf[0] < '0' || buf[0] > '9')
3275 && (buf[0] < 'a' || buf[0] > 'f')
3276 && buf[0] != 'x') /* New: unavailable register value */
3277 {
3278 if (remote_debug)
0f71a2f6
JM
3279 fprintf_unfiltered (gdb_stdlog,
3280 "Bad register packet; fetching a new packet\n");
d01949b6 3281 getpkt (buf, (rs->remote_packet_size), 0);
c906108c
SS
3282 }
3283
3284 /* Reply describes registers byte by byte, each byte encoded as two
3285 hex characters. Suck them all up, then supply them to the
3286 register cacheing/storage mechanism. */
3287
3288 p = buf;
ad10f812 3289 for (i = 0; i < rs->sizeof_g_packet; i++)
c906108c
SS
3290 {
3291 if (p[0] == 0)
3292 break;
3293 if (p[1] == 0)
3294 {
3295 warning ("Remote reply is of odd length: %s", buf);
3296 /* Don't change register_bytes_found in this case, and don't
3297 print a second warning. */
3298 goto supply_them;
3299 }
3300 if (p[0] == 'x' && p[1] == 'x')
c5aa993b 3301 regs[i] = 0; /* 'x' */
c906108c
SS
3302 else
3303 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
3304 p += 2;
3305 }
3306
3307 if (i != register_bytes_found)
3308 {
3309 register_bytes_found = i;
2649061d
AC
3310 if (REGISTER_BYTES_OK_P ()
3311 && !REGISTER_BYTES_OK (i))
c906108c 3312 warning ("Remote reply is too short: %s", buf);
c906108c 3313 }
c5aa993b 3314
b323314b 3315 supply_them:
ad10f812 3316 {
b323314b
AC
3317 int i;
3318 for (i = 0; i < NUM_REGS + NUM_PSEUDO_REGS; i++)
ad10f812 3319 {
b323314b
AC
3320 struct packet_reg *r = &rs->regs[i];
3321 if (r->in_g_packet)
3322 {
8ccc1287
AC
3323 if (r->offset * 2 >= strlen (buf))
3324 /* A short packet that didn't include the register's
3325 value, this implies that the register is zero (and
3326 not that the register is unavailable). Supply that
3327 zero value. */
3328 regcache_raw_supply (current_regcache, r->regnum, NULL);
3329 else if (buf[r->offset * 2] == 'x')
3330 {
3331 gdb_assert (r->offset * 2 < strlen (buf));
3332 /* The register isn't available, mark it as such (at
3333 the same time setting the value to zero). */
3334 regcache_raw_supply (current_regcache, r->regnum, NULL);
3335 set_register_cached (i, -1);
3336 }
3337 else
3338 regcache_raw_supply (current_regcache, r->regnum,
3339 regs + r->offset);
b323314b 3340 }
ad10f812
AC
3341 }
3342 }
c906108c
SS
3343}
3344
3345/* Prepare to store registers. Since we may send them all (using a
3346 'G' request), we have to read out the ones we don't want to change
3347 first. */
3348
c5aa993b 3349static void
fba45db2 3350remote_prepare_to_store (void)
c906108c 3351{
cf0e1e0d
DJ
3352 struct remote_state *rs = get_remote_state ();
3353 int i;
3354 char buf[MAX_REGISTER_SIZE];
3355
c906108c 3356 /* Make sure the entire registers array is valid. */
5a2468f5
JM
3357 switch (remote_protocol_P.support)
3358 {
3359 case PACKET_DISABLE:
3360 case PACKET_SUPPORT_UNKNOWN:
cf0e1e0d
DJ
3361 /* Make sure all the necessary registers are cached. */
3362 for (i = 0; i < NUM_REGS; i++)
3363 if (rs->regs[i].in_g_packet)
3364 regcache_raw_read (current_regcache, rs->regs[i].regnum, buf);
5a2468f5
JM
3365 break;
3366 case PACKET_ENABLE:
3367 break;
3368 }
3369}
3370
ad10f812 3371/* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
5a2468f5
JM
3372 packet was not recognized. */
3373
3374static int
ad10f812 3375store_register_using_P (int regnum)
5a2468f5 3376{
d01949b6 3377 struct remote_state *rs = get_remote_state ();
ad10f812 3378 struct packet_reg *reg = packet_reg_from_regnum (rs, regnum);
5a2468f5 3379 /* Try storing a single register. */
d01949b6 3380 char *buf = alloca (rs->remote_packet_size);
d9d9c31f 3381 char regp[MAX_REGISTER_SIZE];
5a2468f5
JM
3382 char *p;
3383 int i;
3384
ad10f812 3385 sprintf (buf, "P%s=", phex_nz (reg->pnum, 0));
5a2468f5 3386 p = buf + strlen (buf);
822c9732 3387 regcache_raw_collect (current_regcache, reg->regnum, regp);
3acba339 3388 bin2hex (regp, p, register_size (current_gdbarch, reg->regnum));
ad10f812 3389 remote_send (buf, rs->remote_packet_size);
5a2468f5
JM
3390
3391 return buf[0] != '\0';
c906108c
SS
3392}
3393
5a2468f5 3394
ad10f812 3395/* Store register REGNUM, or all registers if REGNUM == -1, from the contents
7302a204 3396 of the register cache buffer. FIXME: ignores errors. */
c906108c
SS
3397
3398static void
ad10f812 3399remote_store_registers (int regnum)
c906108c 3400{
d01949b6 3401 struct remote_state *rs = get_remote_state ();
193cb69f
AC
3402 char *buf;
3403 char *regs;
c906108c
SS
3404 int i;
3405 char *p;
3406
39f77062 3407 set_thread (PIDGET (inferior_ptid), 1);
c906108c 3408
ad10f812 3409 if (regnum >= 0)
c906108c 3410 {
5a2468f5 3411 switch (remote_protocol_P.support)
c906108c 3412 {
5a2468f5
JM
3413 case PACKET_DISABLE:
3414 break;
3415 case PACKET_ENABLE:
ad10f812 3416 if (store_register_using_P (regnum))
5a2468f5
JM
3417 return;
3418 else
3419 error ("Protocol error: P packet not recognized by stub");
3420 case PACKET_SUPPORT_UNKNOWN:
ad10f812 3421 if (store_register_using_P (regnum))
5a2468f5
JM
3422 {
3423 /* The stub recognized the 'P' packet. Remember this. */
3424 remote_protocol_P.support = PACKET_ENABLE;
3425 return;
3426 }
3427 else
3428 {
3429 /* The stub does not support the 'P' packet. Use 'G'
3430 instead, and don't try using 'P' in the future (it
3431 will just waste our time). */
3432 remote_protocol_P.support = PACKET_DISABLE;
3433 break;
3434 }
c906108c 3435 }
c906108c
SS
3436 }
3437
193cb69f
AC
3438 /* Extract all the registers in the regcache copying them into a
3439 local buffer. */
3440 {
b323314b 3441 int i;
ad10f812
AC
3442 regs = alloca (rs->sizeof_g_packet);
3443 memset (regs, rs->sizeof_g_packet, 0);
b323314b 3444 for (i = 0; i < NUM_REGS + NUM_PSEUDO_REGS; i++)
193cb69f 3445 {
b323314b
AC
3446 struct packet_reg *r = &rs->regs[i];
3447 if (r->in_g_packet)
822c9732 3448 regcache_raw_collect (current_regcache, r->regnum, regs + r->offset);
193cb69f
AC
3449 }
3450 }
c906108c
SS
3451
3452 /* Command describes registers byte by byte,
3453 each byte encoded as two hex characters. */
193cb69f
AC
3454 buf = alloca (rs->remote_packet_size);
3455 p = buf;
3456 *p++ = 'G';
c906108c 3457 /* remote_prepare_to_store insures that register_bytes_found gets set. */
30559e10 3458 bin2hex (regs, p, register_bytes_found);
d01949b6 3459 remote_send (buf, (rs->remote_packet_size));
c906108c 3460}
c906108c
SS
3461\f
3462
3463/* Return the number of hex digits in num. */
3464
3465static int
fba45db2 3466hexnumlen (ULONGEST num)
c906108c
SS
3467{
3468 int i;
3469
3470 for (i = 0; num != 0; i++)
3471 num >>= 4;
3472
3473 return max (i, 1);
3474}
3475
2df3850c 3476/* Set BUF to the minimum number of hex digits representing NUM. */
c906108c
SS
3477
3478static int
fba45db2 3479hexnumstr (char *buf, ULONGEST num)
c906108c 3480{
c906108c 3481 int len = hexnumlen (num);
2df3850c
JM
3482 return hexnumnstr (buf, num, len);
3483}
3484
c906108c 3485
2df3850c 3486/* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
c906108c 3487
2df3850c 3488static int
fba45db2 3489hexnumnstr (char *buf, ULONGEST num, int width)
2df3850c
JM
3490{
3491 int i;
3492
3493 buf[width] = '\0';
3494
3495 for (i = width - 1; i >= 0; i--)
c906108c 3496 {
c5aa993b 3497 buf[i] = "0123456789abcdef"[(num & 0xf)];
c906108c
SS
3498 num >>= 4;
3499 }
3500
2df3850c 3501 return width;
c906108c
SS
3502}
3503
3504/* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
3505
3506static CORE_ADDR
fba45db2 3507remote_address_masked (CORE_ADDR addr)
c906108c
SS
3508{
3509 if (remote_address_size > 0
3510 && remote_address_size < (sizeof (ULONGEST) * 8))
3511 {
3512 /* Only create a mask when that mask can safely be constructed
3513 in a ULONGEST variable. */
3514 ULONGEST mask = 1;
3515 mask = (mask << remote_address_size) - 1;
3516 addr &= mask;
3517 }
3518 return addr;
3519}
3520
3521/* Determine whether the remote target supports binary downloading.
3522 This is accomplished by sending a no-op memory write of zero length
3523 to the target at the specified address. It does not suffice to send
3524 the whole packet, since many stubs strip the eighth bit and subsequently
7a292a7a
SS
3525 compute a wrong checksum, which causes real havoc with remote_write_bytes.
3526
96baa820
JM
3527 NOTE: This can still lose if the serial line is not eight-bit
3528 clean. In cases like this, the user should clear "remote
3529 X-packet". */
3530
c906108c 3531static void
fba45db2 3532check_binary_download (CORE_ADDR addr)
c906108c 3533{
d01949b6 3534 struct remote_state *rs = get_remote_state ();
96baa820 3535 switch (remote_protocol_binary_download.support)
c906108c 3536 {
96baa820
JM
3537 case PACKET_DISABLE:
3538 break;
3539 case PACKET_ENABLE:
3540 break;
3541 case PACKET_SUPPORT_UNKNOWN:
3542 {
d01949b6 3543 char *buf = alloca (rs->remote_packet_size);
96baa820 3544 char *p;
802188a7 3545
96baa820
JM
3546 p = buf;
3547 *p++ = 'X';
3548 p += hexnumstr (p, (ULONGEST) addr);
3549 *p++ = ',';
3550 p += hexnumstr (p, (ULONGEST) 0);
3551 *p++ = ':';
3552 *p = '\0';
802188a7 3553
96baa820 3554 putpkt_binary (buf, (int) (p - buf));
d01949b6 3555 getpkt (buf, (rs->remote_packet_size), 0);
c906108c 3556
96baa820
JM
3557 if (buf[0] == '\0')
3558 {
3559 if (remote_debug)
3560 fprintf_unfiltered (gdb_stdlog,
3561 "binary downloading NOT suppported by target\n");
3562 remote_protocol_binary_download.support = PACKET_DISABLE;
3563 }
3564 else
3565 {
3566 if (remote_debug)
3567 fprintf_unfiltered (gdb_stdlog,
3568 "binary downloading suppported by target\n");
3569 remote_protocol_binary_download.support = PACKET_ENABLE;
3570 }
3571 break;
3572 }
c906108c
SS
3573 }
3574}
3575
3576/* Write memory data directly to the remote machine.
3577 This does not inform the data cache; the data cache uses this.
3578 MEMADDR is the address in the remote memory space.
3579 MYADDR is the address of the buffer in our space.
3580 LEN is the number of bytes.
3581
917317f4
JM
3582 Returns number of bytes transferred, or 0 (setting errno) for
3583 error. Only transfer a single packet. */
c906108c 3584
449092f6 3585int
917317f4 3586remote_write_bytes (CORE_ADDR memaddr, char *myaddr, int len)
c906108c 3587{
917317f4 3588 unsigned char *buf;
917317f4
JM
3589 unsigned char *p;
3590 unsigned char *plen;
c2d11a7d 3591 long sizeof_buf;
917317f4
JM
3592 int plenlen;
3593 int todo;
3594 int nr_bytes;
a257b5bb
AC
3595 int payload_size;
3596 unsigned char *payload_start;
c906108c 3597
a257b5bb 3598 /* Verify that the target can support a binary download. */
c906108c
SS
3599 check_binary_download (memaddr);
3600
a257b5bb
AC
3601 /* Compute the size, and then allocate space for the largest
3602 possible packet. Include space for an extra trailing NUL. */
3603 sizeof_buf = get_memory_write_packet_size () + 1;
c2d11a7d 3604 buf = alloca (sizeof_buf);
c906108c 3605
a257b5bb
AC
3606 /* Compute the size of the actual payload by subtracting out the
3607 packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
3608 payload_size = (get_memory_write_packet_size () - (strlen ("$M,:#NN")
3609 + hexnumlen (memaddr)
3610 + hexnumlen (len)));
c906108c 3611
a257b5bb 3612 /* Construct the packet header: "[MX]<memaddr>,<len>:". */
917317f4 3613
a257b5bb 3614 /* Append "[XM]". Compute a best guess of the number of bytes
917317f4 3615 actually transfered. */
a257b5bb 3616 p = buf;
917317f4 3617 switch (remote_protocol_binary_download.support)
c906108c 3618 {
917317f4
JM
3619 case PACKET_ENABLE:
3620 *p++ = 'X';
3621 /* Best guess at number of bytes that will fit. */
a257b5bb 3622 todo = min (len, payload_size);
917317f4
JM
3623 break;
3624 case PACKET_DISABLE:
3625 *p++ = 'M';
3626 /* num bytes that will fit */
a257b5bb 3627 todo = min (len, payload_size / 2);
917317f4
JM
3628 break;
3629 case PACKET_SUPPORT_UNKNOWN:
8e65ff28
AC
3630 internal_error (__FILE__, __LINE__,
3631 "remote_write_bytes: bad internal state");
7f7e9482 3632 default:
8e65ff28 3633 internal_error (__FILE__, __LINE__, "bad switch");
917317f4 3634 }
802188a7 3635
a257b5bb 3636 /* Append "<memaddr>". */
917317f4
JM
3637 memaddr = remote_address_masked (memaddr);
3638 p += hexnumstr (p, (ULONGEST) memaddr);
a257b5bb
AC
3639
3640 /* Append ",". */
917317f4 3641 *p++ = ',';
802188a7 3642
a257b5bb
AC
3643 /* Append <len>. Retain the location/size of <len>. It may need to
3644 be adjusted once the packet body has been created. */
917317f4
JM
3645 plen = p;
3646 plenlen = hexnumstr (p, (ULONGEST) todo);
3647 p += plenlen;
a257b5bb
AC
3648
3649 /* Append ":". */
917317f4
JM
3650 *p++ = ':';
3651 *p = '\0';
802188a7 3652
a257b5bb
AC
3653 /* Append the packet body. */
3654 payload_start = p;
917317f4
JM
3655 switch (remote_protocol_binary_download.support)
3656 {
3657 case PACKET_ENABLE:
3658 /* Binary mode. Send target system values byte by byte, in
3659 increasing byte addresses. Only escape certain critical
3660 characters. */
3661 for (nr_bytes = 0;
a257b5bb 3662 (nr_bytes < todo) && (p - payload_start) < payload_size;
917317f4 3663 nr_bytes++)
c906108c 3664 {
917317f4
JM
3665 switch (myaddr[nr_bytes] & 0xff)
3666 {
3667 case '$':
3668 case '#':
3669 case 0x7d:
3670 /* These must be escaped */
3671 *p++ = 0x7d;
3672 *p++ = (myaddr[nr_bytes] & 0xff) ^ 0x20;
3673 break;
3674 default:
3675 *p++ = myaddr[nr_bytes] & 0xff;
3676 break;
3677 }
c906108c 3678 }
917317f4 3679 if (nr_bytes < todo)
c906108c 3680 {
802188a7 3681 /* Escape chars have filled up the buffer prematurely,
917317f4
JM
3682 and we have actually sent fewer bytes than planned.
3683 Fix-up the length field of the packet. Use the same
3684 number of characters as before. */
917317f4
JM
3685 plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen);
3686 *plen = ':'; /* overwrite \0 from hexnumnstr() */
c906108c 3687 }
917317f4
JM
3688 break;
3689 case PACKET_DISABLE:
3690 /* Normal mode: Send target system values byte by byte, in
3691 increasing byte addresses. Each byte is encoded as a two hex
3692 value. */
2644f393 3693 nr_bytes = bin2hex (myaddr, p, todo);
aa6c0017 3694 p += 2 * nr_bytes;
917317f4
JM
3695 break;
3696 case PACKET_SUPPORT_UNKNOWN:
8e65ff28
AC
3697 internal_error (__FILE__, __LINE__,
3698 "remote_write_bytes: bad internal state");
7f7e9482 3699 default:
8e65ff28 3700 internal_error (__FILE__, __LINE__, "bad switch");
c906108c 3701 }
802188a7 3702
917317f4 3703 putpkt_binary (buf, (int) (p - buf));
c2d11a7d 3704 getpkt (buf, sizeof_buf, 0);
802188a7 3705
917317f4
JM
3706 if (buf[0] == 'E')
3707 {
3708 /* There is no correspondance between what the remote protocol
3709 uses for errors and errno codes. We would like a cleaner way
3710 of representing errors (big enough to include errno codes,
3711 bfd_error codes, and others). But for now just return EIO. */
3712 errno = EIO;
3713 return 0;
3714 }
802188a7 3715
917317f4
JM
3716 /* Return NR_BYTES, not TODO, in case escape chars caused us to send fewer
3717 bytes than we'd planned. */
3718 return nr_bytes;
c906108c
SS
3719}
3720
3721/* Read memory data directly from the remote machine.
3722 This does not use the data cache; the data cache uses this.
3723 MEMADDR is the address in the remote memory space.
3724 MYADDR is the address of the buffer in our space.
3725 LEN is the number of bytes.
3726
3727 Returns number of bytes transferred, or 0 for error. */
3728
917317f4
JM
3729/* NOTE: cagney/1999-10-18: This function (and its siblings in other
3730 remote targets) shouldn't attempt to read the entire buffer.
3731 Instead it should read a single packet worth of data and then
3732 return the byte size of that packet to the caller. The caller (its
3733 caller and its callers caller ;-) already contains code for
3734 handling partial reads. */
3735
449092f6 3736int
fba45db2 3737remote_read_bytes (CORE_ADDR memaddr, char *myaddr, int len)
c906108c 3738{
11cf8741 3739 char *buf;
c906108c 3740 int max_buf_size; /* Max size of packet output buffer */
c2d11a7d 3741 long sizeof_buf;
c906108c
SS
3742 int origlen;
3743
11cf8741
JM
3744 /* Create a buffer big enough for this packet. */
3745 max_buf_size = get_memory_read_packet_size ();
c2d11a7d
JM
3746 sizeof_buf = max_buf_size + 1; /* Space for trailing NUL */
3747 buf = alloca (sizeof_buf);
c906108c
SS
3748
3749 origlen = len;
3750 while (len > 0)
3751 {
c906108c
SS
3752 char *p;
3753 int todo;
3754 int i;
3755
c5aa993b 3756 todo = min (len, max_buf_size / 2); /* num bytes that will fit */
c906108c
SS
3757
3758 /* construct "m"<memaddr>","<len>" */
3759 /* sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo); */
3760 memaddr = remote_address_masked (memaddr);
3761 p = buf;
3762 *p++ = 'm';
3763 p += hexnumstr (p, (ULONGEST) memaddr);
3764 *p++ = ',';
3765 p += hexnumstr (p, (ULONGEST) todo);
3766 *p = '\0';
3767
3768 putpkt (buf);
c2d11a7d 3769 getpkt (buf, sizeof_buf, 0);
c906108c 3770
66504d44
MS
3771 if (buf[0] == 'E'
3772 && isxdigit (buf[1]) && isxdigit (buf[2])
3773 && buf[3] == '\0')
c906108c
SS
3774 {
3775 /* There is no correspondance between what the remote protocol uses
3776 for errors and errno codes. We would like a cleaner way of
3777 representing errors (big enough to include errno codes, bfd_error
3778 codes, and others). But for now just return EIO. */
3779 errno = EIO;
3780 return 0;
3781 }
3782
c5aa993b
JM
3783 /* Reply describes memory byte by byte,
3784 each byte encoded as two hex characters. */
c906108c
SS
3785
3786 p = buf;
30559e10 3787 if ((i = hex2bin (p, myaddr, todo)) < todo)
c906108c 3788 {
30559e10
MS
3789 /* Reply is short. This means that we were able to read
3790 only part of what we wanted to. */
3791 return i + (origlen - len);
c906108c
SS
3792 }
3793 myaddr += todo;
3794 memaddr += todo;
3795 len -= todo;
3796 }
3797 return origlen;
3798}
3799\f
3800/* Read or write LEN bytes from inferior memory at MEMADDR,
392a587b
JM
3801 transferring to or from debugger address BUFFER. Write to inferior if
3802 SHOULD_WRITE is nonzero. Returns length of data written or read; 0
c338868a 3803 for error. TARGET is unused. */
392a587b 3804
c906108c 3805static int
c338868a 3806remote_xfer_memory (CORE_ADDR mem_addr, char *buffer, int mem_len,
0a65a603 3807 int should_write, struct mem_attrib *attrib,
29e57380 3808 struct target_ops *target)
c906108c 3809{
392a587b
JM
3810 CORE_ADDR targ_addr;
3811 int targ_len;
4930751a
C
3812 int res;
3813
f6684c31
AC
3814 /* Should this be the selected frame? */
3815 gdbarch_remote_translate_xfer_address (current_gdbarch, current_regcache,
3816 mem_addr, mem_len,
3817 &targ_addr, &targ_len);
392a587b 3818 if (targ_len <= 0)
c906108c 3819 return 0;
c906108c 3820
4930751a
C
3821 if (should_write)
3822 res = remote_write_bytes (targ_addr, buffer, targ_len);
3823 else
3824 res = remote_read_bytes (targ_addr, buffer, targ_len);
3825
3826 return res;
c906108c
SS
3827}
3828
c906108c 3829static void
fba45db2 3830remote_files_info (struct target_ops *ignore)
c906108c
SS
3831{
3832 puts_filtered ("Debugging a target over a serial line.\n");
3833}
3834\f
3835/* Stuff for dealing with the packets which are part of this protocol.
3836 See comment at top of file for details. */
3837
3838/* Read a single character from the remote end, masking it down to 7 bits. */
3839
3840static int
fba45db2 3841readchar (int timeout)
c906108c
SS
3842{
3843 int ch;
3844
2cd58942 3845 ch = serial_readchar (remote_desc, timeout);
c906108c 3846
2acceee2
JM
3847 if (ch >= 0)
3848 return (ch & 0x7f);
3849
3850 switch ((enum serial_rc) ch)
c906108c
SS
3851 {
3852 case SERIAL_EOF:
2acceee2 3853 target_mourn_inferior ();
c906108c 3854 error ("Remote connection closed");
2acceee2 3855 /* no return */
c906108c
SS
3856 case SERIAL_ERROR:
3857 perror_with_name ("Remote communication error");
2acceee2 3858 /* no return */
c906108c 3859 case SERIAL_TIMEOUT:
2acceee2 3860 break;
c906108c 3861 }
2acceee2 3862 return ch;
c906108c
SS
3863}
3864
3865/* Send the command in BUF to the remote machine, and read the reply
3866 into BUF. Report an error if we get an error reply. */
3867
3868static void
c2d11a7d
JM
3869remote_send (char *buf,
3870 long sizeof_buf)
c906108c
SS
3871{
3872 putpkt (buf);
c2d11a7d 3873 getpkt (buf, sizeof_buf, 0);
c906108c
SS
3874
3875 if (buf[0] == 'E')
3876 error ("Remote failure reply: %s", buf);
3877}
3878
3879/* Display a null-terminated packet on stdout, for debugging, using C
3880 string notation. */
3881
3882static void
fba45db2 3883print_packet (char *buf)
c906108c
SS
3884{
3885 puts_filtered ("\"");
43e526b9 3886 fputstr_filtered (buf, '"', gdb_stdout);
c906108c
SS
3887 puts_filtered ("\"");
3888}
3889
3890int
fba45db2 3891putpkt (char *buf)
c906108c
SS
3892{
3893 return putpkt_binary (buf, strlen (buf));
3894}
3895
3896/* Send a packet to the remote machine, with error checking. The data
d01949b6 3897 of the packet is in BUF. The string in BUF can be at most (rs->remote_packet_size) - 5
c906108c
SS
3898 to account for the $, # and checksum, and for a possible /0 if we are
3899 debugging (remote_debug) and want to print the sent packet as a string */
3900
3901static int
fba45db2 3902putpkt_binary (char *buf, int cnt)
c906108c 3903{
d01949b6 3904 struct remote_state *rs = get_remote_state ();
c906108c
SS
3905 int i;
3906 unsigned char csum = 0;
11cf8741 3907 char *buf2 = alloca (cnt + 6);
d01949b6 3908 long sizeof_junkbuf = (rs->remote_packet_size);
c2d11a7d 3909 char *junkbuf = alloca (sizeof_junkbuf);
085dd6e6 3910
c906108c
SS
3911 int ch;
3912 int tcount = 0;
3913 char *p;
3914
3915 /* Copy the packet into buffer BUF2, encapsulating it
3916 and giving it a checksum. */
3917
c906108c
SS
3918 p = buf2;
3919 *p++ = '$';
3920
3921 for (i = 0; i < cnt; i++)
3922 {
3923 csum += buf[i];
3924 *p++ = buf[i];
3925 }
3926 *p++ = '#';
3927 *p++ = tohex ((csum >> 4) & 0xf);
3928 *p++ = tohex (csum & 0xf);
3929
3930 /* Send it over and over until we get a positive ack. */
3931
3932 while (1)
3933 {
3934 int started_error_output = 0;
3935
3936 if (remote_debug)
3937 {
3938 *p = '\0';
43e526b9
JM
3939 fprintf_unfiltered (gdb_stdlog, "Sending packet: ");
3940 fputstrn_unfiltered (buf2, p - buf2, 0, gdb_stdlog);
d4f3574e 3941 fprintf_unfiltered (gdb_stdlog, "...");
0f71a2f6 3942 gdb_flush (gdb_stdlog);
c906108c 3943 }
2cd58942 3944 if (serial_write (remote_desc, buf2, p - buf2))
c906108c
SS
3945 perror_with_name ("putpkt: write failed");
3946
3947 /* read until either a timeout occurs (-2) or '+' is read */
3948 while (1)
3949 {
3950 ch = readchar (remote_timeout);
3951
c5aa993b 3952 if (remote_debug)
c906108c
SS
3953 {
3954 switch (ch)
3955 {
3956 case '+':
1216fa2c 3957 case '-':
c906108c
SS
3958 case SERIAL_TIMEOUT:
3959 case '$':
3960 if (started_error_output)
3961 {
3962 putchar_unfiltered ('\n');
3963 started_error_output = 0;
3964 }
3965 }
3966 }
3967
3968 switch (ch)
3969 {
3970 case '+':
3971 if (remote_debug)
0f71a2f6 3972 fprintf_unfiltered (gdb_stdlog, "Ack\n");
c906108c 3973 return 1;
1216fa2c
AC
3974 case '-':
3975 if (remote_debug)
3976 fprintf_unfiltered (gdb_stdlog, "Nak\n");
c906108c 3977 case SERIAL_TIMEOUT:
c5aa993b 3978 tcount++;
c906108c
SS
3979 if (tcount > 3)
3980 return 0;
3981 break; /* Retransmit buffer */
3982 case '$':
3983 {
40e3f985
FN
3984 if (remote_debug)
3985 fprintf_unfiltered (gdb_stdlog, "Packet instead of Ack, ignoring it\n");
d6f7abdf
AC
3986 /* It's probably an old response sent because an ACK
3987 was lost. Gobble up the packet and ack it so it
3988 doesn't get retransmitted when we resend this
3989 packet. */
40e3f985 3990 read_frame (junkbuf, sizeof_junkbuf);
d6f7abdf 3991 serial_write (remote_desc, "+", 1);
c5aa993b 3992 continue; /* Now, go look for + */
c906108c
SS
3993 }
3994 default:
3995 if (remote_debug)
3996 {
3997 if (!started_error_output)
3998 {
3999 started_error_output = 1;
0f71a2f6 4000 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
c906108c 4001 }
0f71a2f6 4002 fputc_unfiltered (ch & 0177, gdb_stdlog);
c906108c
SS
4003 }
4004 continue;
4005 }
4006 break; /* Here to retransmit */
4007 }
4008
4009#if 0
4010 /* This is wrong. If doing a long backtrace, the user should be
c5aa993b
JM
4011 able to get out next time we call QUIT, without anything as
4012 violent as interrupt_query. If we want to provide a way out of
4013 here without getting to the next QUIT, it should be based on
4014 hitting ^C twice as in remote_wait. */
c906108c
SS
4015 if (quit_flag)
4016 {
4017 quit_flag = 0;
4018 interrupt_query ();
4019 }
4020#endif
4021 }
4022}
4023
4024/* Come here after finding the start of the frame. Collect the rest
4025 into BUF, verifying the checksum, length, and handling run-length
c2d11a7d
JM
4026 compression. No more than sizeof_buf-1 characters are read so that
4027 the buffer can be NUL terminated.
c906108c 4028
c2d11a7d
JM
4029 Returns -1 on error, number of characters in buffer (ignoring the
4030 trailing NULL) on success. (could be extended to return one of the
4031 SERIAL status indications). */
4032
4033static long
4034read_frame (char *buf,
4035 long sizeof_buf)
c906108c
SS
4036{
4037 unsigned char csum;
c2d11a7d 4038 long bc;
c906108c
SS
4039 int c;
4040
4041 csum = 0;
c2d11a7d 4042 bc = 0;
c906108c
SS
4043
4044 while (1)
4045 {
c2d11a7d 4046 /* ASSERT (bc < sizeof_buf - 1) - space for trailing NUL */
c906108c 4047 c = readchar (remote_timeout);
c906108c
SS
4048 switch (c)
4049 {
4050 case SERIAL_TIMEOUT:
4051 if (remote_debug)
0f71a2f6 4052 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
c2d11a7d 4053 return -1;
c906108c
SS
4054 case '$':
4055 if (remote_debug)
0f71a2f6
JM
4056 fputs_filtered ("Saw new packet start in middle of old one\n",
4057 gdb_stdlog);
c2d11a7d 4058 return -1; /* Start a new packet, count retries */
c906108c
SS
4059 case '#':
4060 {
4061 unsigned char pktcsum;
e1b09194
AC
4062 int check_0 = 0;
4063 int check_1 = 0;
c906108c 4064
c2d11a7d 4065 buf[bc] = '\0';
c906108c 4066
e1b09194
AC
4067 check_0 = readchar (remote_timeout);
4068 if (check_0 >= 0)
4069 check_1 = readchar (remote_timeout);
802188a7 4070
e1b09194
AC
4071 if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
4072 {
4073 if (remote_debug)
4074 fputs_filtered ("Timeout in checksum, retrying\n", gdb_stdlog);
4075 return -1;
4076 }
4077 else if (check_0 < 0 || check_1 < 0)
40e3f985
FN
4078 {
4079 if (remote_debug)
4080 fputs_filtered ("Communication error in checksum\n", gdb_stdlog);
4081 return -1;
4082 }
c906108c 4083
e1b09194 4084 pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
c906108c 4085 if (csum == pktcsum)
c2d11a7d 4086 return bc;
c906108c 4087
c5aa993b 4088 if (remote_debug)
c906108c 4089 {
0f71a2f6 4090 fprintf_filtered (gdb_stdlog,
c5aa993b 4091 "Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
0f71a2f6
JM
4092 pktcsum, csum);
4093 fputs_filtered (buf, gdb_stdlog);
4094 fputs_filtered ("\n", gdb_stdlog);
c906108c 4095 }
c2d11a7d
JM
4096 /* Number of characters in buffer ignoring trailing
4097 NUL. */
4098 return -1;
c906108c
SS
4099 }
4100 case '*': /* Run length encoding */
c2c6d25f
JM
4101 {
4102 int repeat;
4103 csum += c;
c906108c 4104
b4501125
AC
4105 c = readchar (remote_timeout);
4106 csum += c;
4107 repeat = c - ' ' + 3; /* Compute repeat count */
c906108c 4108
c2d11a7d
JM
4109 /* The character before ``*'' is repeated. */
4110
802188a7 4111 if (repeat > 0 && repeat <= 255
c2d11a7d 4112 && bc > 0
fb6e5c55 4113 && bc + repeat - 1 < sizeof_buf - 1)
c2c6d25f 4114 {
c2d11a7d
JM
4115 memset (&buf[bc], buf[bc - 1], repeat);
4116 bc += repeat;
c2c6d25f
JM
4117 continue;
4118 }
4119
c2d11a7d 4120 buf[bc] = '\0';
c2c6d25f
JM
4121 printf_filtered ("Repeat count %d too large for buffer: ", repeat);
4122 puts_filtered (buf);
4123 puts_filtered ("\n");
c2d11a7d 4124 return -1;
c2c6d25f 4125 }
c906108c 4126 default:
c2d11a7d 4127 if (bc < sizeof_buf - 1)
c906108c 4128 {
c2d11a7d 4129 buf[bc++] = c;
c906108c
SS
4130 csum += c;
4131 continue;
4132 }
4133
c2d11a7d 4134 buf[bc] = '\0';
c906108c
SS
4135 puts_filtered ("Remote packet too long: ");
4136 puts_filtered (buf);
4137 puts_filtered ("\n");
4138
c2d11a7d 4139 return -1;
c906108c
SS
4140 }
4141 }
4142}
4143
4144/* Read a packet from the remote machine, with error checking, and
c2d11a7d
JM
4145 store it in BUF. If FOREVER, wait forever rather than timing out;
4146 this is used (in synchronous mode) to wait for a target that is is
4147 executing user code to stop. */
d9fcf2fb
JM
4148/* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
4149 don't have to change all the calls to getpkt to deal with the
4150 return value, because at the moment I don't know what the right
4151 thing to do it for those. */
c906108c 4152void
c2d11a7d
JM
4153getpkt (char *buf,
4154 long sizeof_buf,
4155 int forever)
d9fcf2fb
JM
4156{
4157 int timed_out;
4158
4159 timed_out = getpkt_sane (buf, sizeof_buf, forever);
4160}
4161
4162
4163/* Read a packet from the remote machine, with error checking, and
4164 store it in BUF. If FOREVER, wait forever rather than timing out;
4165 this is used (in synchronous mode) to wait for a target that is is
4166 executing user code to stop. If FOREVER == 0, this function is
4167 allowed to time out gracefully and return an indication of this to
4168 the caller. */
3172dc30 4169static int
d9fcf2fb
JM
4170getpkt_sane (char *buf,
4171 long sizeof_buf,
4172 int forever)
c906108c
SS
4173{
4174 int c;
4175 int tries;
4176 int timeout;
4177 int val;
4178
c5aa993b 4179 strcpy (buf, "timeout");
c906108c
SS
4180
4181 if (forever)
4182 {
c906108c 4183 timeout = watchdog > 0 ? watchdog : -1;
c906108c
SS
4184 }
4185
4186 else
4187 timeout = remote_timeout;
4188
4189#define MAX_TRIES 3
4190
4191 for (tries = 1; tries <= MAX_TRIES; tries++)
4192 {
4193 /* This can loop forever if the remote side sends us characters
c5aa993b
JM
4194 continuously, but if it pauses, we'll get a zero from readchar
4195 because of timeout. Then we'll count that as a retry. */
c906108c
SS
4196
4197 /* Note that we will only wait forever prior to the start of a packet.
c5aa993b
JM
4198 After that, we expect characters to arrive at a brisk pace. They
4199 should show up within remote_timeout intervals. */
c906108c
SS
4200
4201 do
4202 {
4203 c = readchar (timeout);
4204
4205 if (c == SERIAL_TIMEOUT)
4206 {
2acceee2 4207 if (forever) /* Watchdog went off? Kill the target. */
c906108c 4208 {
2acceee2 4209 QUIT;
c906108c
SS
4210 target_mourn_inferior ();
4211 error ("Watchdog has expired. Target detached.\n");
4212 }
c906108c 4213 if (remote_debug)
0f71a2f6 4214 fputs_filtered ("Timed out.\n", gdb_stdlog);
c906108c
SS
4215 goto retry;
4216 }
4217 }
4218 while (c != '$');
4219
4220 /* We've found the start of a packet, now collect the data. */
4221
c2d11a7d 4222 val = read_frame (buf, sizeof_buf);
c906108c 4223
c2d11a7d 4224 if (val >= 0)
c906108c
SS
4225 {
4226 if (remote_debug)
43e526b9
JM
4227 {
4228 fprintf_unfiltered (gdb_stdlog, "Packet received: ");
4229 fputstr_unfiltered (buf, 0, gdb_stdlog);
4230 fprintf_unfiltered (gdb_stdlog, "\n");
4231 }
2cd58942 4232 serial_write (remote_desc, "+", 1);
d9fcf2fb 4233 return 0;
c906108c
SS
4234 }
4235
4236 /* Try the whole thing again. */
4237 retry:
2cd58942 4238 serial_write (remote_desc, "-", 1);
c906108c
SS
4239 }
4240
4241 /* We have tried hard enough, and just can't receive the packet. Give up. */
4242
4243 printf_unfiltered ("Ignoring packet error, continuing...\n");
2cd58942 4244 serial_write (remote_desc, "+", 1);
d9fcf2fb 4245 return 1;
c906108c
SS
4246}
4247\f
4248static void
fba45db2 4249remote_kill (void)
c906108c
SS
4250{
4251 /* For some mysterious reason, wait_for_inferior calls kill instead of
4252 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4253 if (kill_kludge)
4254 {
4255 kill_kludge = 0;
4256 target_mourn_inferior ();
4257 return;
4258 }
4259
4260 /* Use catch_errors so the user can quit from gdb even when we aren't on
4261 speaking terms with the remote system. */
c5aa993b 4262 catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR);
c906108c
SS
4263
4264 /* Don't wait for it to die. I'm not really sure it matters whether
4265 we do or not. For the existing stubs, kill is a noop. */
4266 target_mourn_inferior ();
4267}
4268
43ff13b4
JM
4269/* Async version of remote_kill. */
4270static void
fba45db2 4271remote_async_kill (void)
43ff13b4
JM
4272{
4273 /* Unregister the file descriptor from the event loop. */
ed9a39eb 4274 if (target_is_async_p ())
2cd58942 4275 serial_async (remote_desc, NULL, 0);
43ff13b4
JM
4276
4277 /* For some mysterious reason, wait_for_inferior calls kill instead of
4278 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4279 if (kill_kludge)
4280 {
4281 kill_kludge = 0;
4282 target_mourn_inferior ();
4283 return;
4284 }
4285
4286 /* Use catch_errors so the user can quit from gdb even when we aren't on
4287 speaking terms with the remote system. */
c5aa993b 4288 catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR);
43ff13b4
JM
4289
4290 /* Don't wait for it to die. I'm not really sure it matters whether
4291 we do or not. For the existing stubs, kill is a noop. */
4292 target_mourn_inferior ();
4293}
4294
c906108c 4295static void
fba45db2 4296remote_mourn (void)
c906108c
SS
4297{
4298 remote_mourn_1 (&remote_ops);
4299}
4300
53a5351d 4301static void
fba45db2 4302remote_async_mourn (void)
53a5351d
JM
4303{
4304 remote_mourn_1 (&remote_async_ops);
4305}
4306
c906108c 4307static void
fba45db2 4308extended_remote_mourn (void)
c906108c
SS
4309{
4310 /* We do _not_ want to mourn the target like this; this will
4311 remove the extended remote target from the target stack,
802188a7 4312 and the next time the user says "run" it'll fail.
c906108c
SS
4313
4314 FIXME: What is the right thing to do here? */
4315#if 0
4316 remote_mourn_1 (&extended_remote_ops);
4317#endif
4318}
4319
4320/* Worker function for remote_mourn. */
4321static void
fba45db2 4322remote_mourn_1 (struct target_ops *target)
c906108c
SS
4323{
4324 unpush_target (target);
4325 generic_mourn_inferior ();
4326}
4327
4328/* In the extended protocol we want to be able to do things like
4329 "run" and have them basically work as expected. So we need
802188a7 4330 a special create_inferior function.
c906108c
SS
4331
4332 FIXME: One day add support for changing the exec file
4333 we're debugging, arguments and an environment. */
4334
4335static void
c27cda74
AC
4336extended_remote_create_inferior (char *exec_file, char *args, char **env,
4337 int from_tty)
c906108c
SS
4338{
4339 /* Rip out the breakpoints; we'll reinsert them after restarting
4340 the remote server. */
4341 remove_breakpoints ();
4342
4343 /* Now restart the remote server. */
4344 extended_remote_restart ();
4345
4346 /* Now put the breakpoints back in. This way we're safe if the
4347 restart function works via a unix fork on the remote side. */
4348 insert_breakpoints ();
4349
4350 /* Clean up from the last time we were running. */
4351 clear_proceed_status ();
4352
4353 /* Let the remote process run. */
4354 proceed (-1, TARGET_SIGNAL_0, 0);
4355}
4356
43ff13b4
JM
4357/* Async version of extended_remote_create_inferior. */
4358static void
c27cda74
AC
4359extended_remote_async_create_inferior (char *exec_file, char *args, char **env,
4360 int from_tty)
43ff13b4
JM
4361{
4362 /* Rip out the breakpoints; we'll reinsert them after restarting
4363 the remote server. */
4364 remove_breakpoints ();
4365
4366 /* If running asynchronously, register the target file descriptor
4367 with the event loop. */
362646f5 4368 if (target_can_async_p ())
2acceee2 4369 target_async (inferior_event_handler, 0);
43ff13b4
JM
4370
4371 /* Now restart the remote server. */
4372 extended_remote_restart ();
4373
4374 /* Now put the breakpoints back in. This way we're safe if the
4375 restart function works via a unix fork on the remote side. */
4376 insert_breakpoints ();
4377
4378 /* Clean up from the last time we were running. */
4379 clear_proceed_status ();
4380
4381 /* Let the remote process run. */
4382 proceed (-1, TARGET_SIGNAL_0, 0);
4383}
c906108c 4384\f
c5aa993b 4385
aaab4dba 4386/* On some machines, e.g. 68k, we may use a different breakpoint
7fec2c59
AC
4387 instruction than other targets; in those use
4388 DEPRECATED_REMOTE_BREAKPOINT instead of just BREAKPOINT_FROM_PC.
4389 Also, bi-endian targets may define
4390 DEPRECATED_LITTLE_REMOTE_BREAKPOINT and
4391 DEPRECATED_BIG_REMOTE_BREAKPOINT. If none of these are defined, we
4392 just call the standard routines that are in mem-break.c. */
4393
4394/* NOTE: cagney/2003-06-08: This is silly. A remote and simulator
4395 target should use an identical BREAKPOINT_FROM_PC. As for native,
4396 the ARCH-OS-tdep.c code can override the default. */
4397
4398#if defined (DEPRECATED_LITTLE_REMOTE_BREAKPOINT) && defined (DEPRECATED_BIG_REMOTE_BREAKPOINT) && !defined(DEPRECATED_REMOTE_BREAKPOINT)
4399#define DEPRECATED_REMOTE_BREAKPOINT
c906108c
SS
4400#endif
4401
7fec2c59 4402#ifdef DEPRECATED_REMOTE_BREAKPOINT
c906108c
SS
4403
4404/* If the target isn't bi-endian, just pretend it is. */
7fec2c59
AC
4405#if !defined (DEPRECATED_LITTLE_REMOTE_BREAKPOINT) && !defined (DEPRECATED_BIG_REMOTE_BREAKPOINT)
4406#define DEPRECATED_LITTLE_REMOTE_BREAKPOINT DEPRECATED_REMOTE_BREAKPOINT
4407#define DEPRECATED_BIG_REMOTE_BREAKPOINT DEPRECATED_REMOTE_BREAKPOINT
c906108c
SS
4408#endif
4409
7fec2c59
AC
4410static unsigned char big_break_insn[] = DEPRECATED_BIG_REMOTE_BREAKPOINT;
4411static unsigned char little_break_insn[] = DEPRECATED_LITTLE_REMOTE_BREAKPOINT;
c906108c 4412
7fec2c59 4413#endif /* DEPRECATED_REMOTE_BREAKPOINT */
c906108c 4414
aaab4dba
AC
4415/* Insert a breakpoint on targets that don't have any better
4416 breakpoint support. We read the contents of the target location
4417 and stash it, then overwrite it with a breakpoint instruction.
4418 ADDR is the target location in the target machine. CONTENTS_CACHE
4419 is a pointer to memory allocated for saving the target contents.
4420 It is guaranteed by the caller to be long enough to save the number
4421 of bytes returned by BREAKPOINT_FROM_PC. */
c906108c
SS
4422
4423static int
fba45db2 4424remote_insert_breakpoint (CORE_ADDR addr, char *contents_cache)
c906108c 4425{
d01949b6 4426 struct remote_state *rs = get_remote_state ();
7fec2c59 4427#ifdef DEPRECATED_REMOTE_BREAKPOINT
c906108c 4428 int val;
802188a7 4429#endif
96baa820
JM
4430 int bp_size;
4431
d471ea57
AC
4432 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
4433 If it succeeds, then set the support to PACKET_ENABLE. If it
4434 fails, and the user has explicitly requested the Z support then
4435 report an error, otherwise, mark it disabled and go on. */
802188a7 4436
d471ea57 4437 if (remote_protocol_Z[Z_PACKET_SOFTWARE_BP].support != PACKET_DISABLE)
96baa820 4438 {
d01949b6 4439 char *buf = alloca (rs->remote_packet_size);
e514a9d6 4440 char *p = buf;
802188a7 4441
0caabb7e 4442 addr = remote_address_masked (addr);
96baa820
JM
4443 *(p++) = 'Z';
4444 *(p++) = '0';
4445 *(p++) = ',';
0caabb7e
AC
4446 p += hexnumstr (p, (ULONGEST) addr);
4447 BREAKPOINT_FROM_PC (&addr, &bp_size);
96baa820 4448 sprintf (p, ",%d", bp_size);
802188a7 4449
96baa820 4450 putpkt (buf);
d01949b6 4451 getpkt (buf, (rs->remote_packet_size), 0);
96baa820 4452
d471ea57 4453 switch (packet_ok (buf, &remote_protocol_Z[Z_PACKET_SOFTWARE_BP]))
96baa820 4454 {
d471ea57
AC
4455 case PACKET_ERROR:
4456 return -1;
4457 case PACKET_OK:
4458 return 0;
4459 case PACKET_UNKNOWN:
4460 break;
96baa820
JM
4461 }
4462 }
c906108c 4463
802188a7 4464#ifdef DEPRECATED_REMOTE_BREAKPOINT
c906108c
SS
4465 val = target_read_memory (addr, contents_cache, sizeof big_break_insn);
4466
4467 if (val == 0)
4468 {
d7449b42 4469 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
c906108c
SS
4470 val = target_write_memory (addr, (char *) big_break_insn,
4471 sizeof big_break_insn);
4472 else
4473 val = target_write_memory (addr, (char *) little_break_insn,
4474 sizeof little_break_insn);
4475 }
4476
4477 return val;
4478#else
4479 return memory_insert_breakpoint (addr, contents_cache);
7fec2c59 4480#endif /* DEPRECATED_REMOTE_BREAKPOINT */
c906108c
SS
4481}
4482
4483static int
fba45db2 4484remote_remove_breakpoint (CORE_ADDR addr, char *contents_cache)
c906108c 4485{
d01949b6 4486 struct remote_state *rs = get_remote_state ();
96baa820
JM
4487 int bp_size;
4488
d471ea57 4489 if (remote_protocol_Z[Z_PACKET_SOFTWARE_BP].support != PACKET_DISABLE)
96baa820 4490 {
d01949b6 4491 char *buf = alloca (rs->remote_packet_size);
e514a9d6 4492 char *p = buf;
802188a7 4493
96baa820
JM
4494 *(p++) = 'z';
4495 *(p++) = '0';
4496 *(p++) = ',';
4497
0caabb7e
AC
4498 addr = remote_address_masked (addr);
4499 p += hexnumstr (p, (ULONGEST) addr);
4500 BREAKPOINT_FROM_PC (&addr, &bp_size);
96baa820 4501 sprintf (p, ",%d", bp_size);
802188a7 4502
96baa820 4503 putpkt (buf);
d01949b6 4504 getpkt (buf, (rs->remote_packet_size), 0);
96baa820
JM
4505
4506 return (buf[0] == 'E');
4507 }
4508
7fec2c59 4509#ifdef DEPRECATED_REMOTE_BREAKPOINT
c906108c
SS
4510 return target_write_memory (addr, contents_cache, sizeof big_break_insn);
4511#else
4512 return memory_remove_breakpoint (addr, contents_cache);
7fec2c59 4513#endif /* DEPRECATED_REMOTE_BREAKPOINT */
c906108c
SS
4514}
4515
d471ea57
AC
4516static int
4517watchpoint_to_Z_packet (int type)
4518{
4519 switch (type)
4520 {
4521 case hw_write:
4522 return 2;
4523 break;
4524 case hw_read:
4525 return 3;
4526 break;
4527 case hw_access:
4528 return 4;
4529 break;
4530 default:
8e65ff28
AC
4531 internal_error (__FILE__, __LINE__,
4532 "hw_bp_to_z: bad watchpoint type %d", type);
d471ea57
AC
4533 }
4534}
4535
3c3bea1c 4536static int
fba45db2 4537remote_insert_watchpoint (CORE_ADDR addr, int len, int type)
96baa820 4538{
d01949b6
AC
4539 struct remote_state *rs = get_remote_state ();
4540 char *buf = alloca (rs->remote_packet_size);
e514a9d6 4541 char *p;
d471ea57 4542 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
96baa820 4543
d471ea57
AC
4544 if (remote_protocol_Z[packet].support == PACKET_DISABLE)
4545 error ("Can't set hardware watchpoints without the '%s' (%s) packet\n",
4546 remote_protocol_Z[packet].name,
4547 remote_protocol_Z[packet].title);
802188a7 4548
d471ea57 4549 sprintf (buf, "Z%x,", packet);
96baa820
JM
4550 p = strchr (buf, '\0');
4551 addr = remote_address_masked (addr);
4552 p += hexnumstr (p, (ULONGEST) addr);
d4f3574e 4553 sprintf (p, ",%x", len);
802188a7 4554
96baa820 4555 putpkt (buf);
d01949b6 4556 getpkt (buf, (rs->remote_packet_size), 0);
96baa820 4557
d471ea57
AC
4558 switch (packet_ok (buf, &remote_protocol_Z[packet]))
4559 {
4560 case PACKET_ERROR:
4561 case PACKET_UNKNOWN:
4562 return -1;
4563 case PACKET_OK:
4564 return 0;
4565 }
8e65ff28
AC
4566 internal_error (__FILE__, __LINE__,
4567 "remote_insert_watchpoint: reached end of function");
96baa820
JM
4568}
4569
d471ea57 4570
3c3bea1c 4571static int
fba45db2 4572remote_remove_watchpoint (CORE_ADDR addr, int len, int type)
96baa820 4573{
d01949b6
AC
4574 struct remote_state *rs = get_remote_state ();
4575 char *buf = alloca (rs->remote_packet_size);
e514a9d6 4576 char *p;
d471ea57
AC
4577 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
4578
4579 if (remote_protocol_Z[packet].support == PACKET_DISABLE)
4580 error ("Can't clear hardware watchpoints without the '%s' (%s) packet\n",
4581 remote_protocol_Z[packet].name,
4582 remote_protocol_Z[packet].title);
802188a7 4583
d471ea57 4584 sprintf (buf, "z%x,", packet);
96baa820
JM
4585 p = strchr (buf, '\0');
4586 addr = remote_address_masked (addr);
4587 p += hexnumstr (p, (ULONGEST) addr);
d4f3574e 4588 sprintf (p, ",%x", len);
96baa820 4589 putpkt (buf);
d01949b6 4590 getpkt (buf, (rs->remote_packet_size), 0);
96baa820 4591
d471ea57
AC
4592 switch (packet_ok (buf, &remote_protocol_Z[packet]))
4593 {
4594 case PACKET_ERROR:
4595 case PACKET_UNKNOWN:
4596 return -1;
4597 case PACKET_OK:
4598 return 0;
4599 }
8e65ff28
AC
4600 internal_error (__FILE__, __LINE__,
4601 "remote_remove_watchpoint: reached end of function");
96baa820
JM
4602}
4603
3c3bea1c 4604
501eef12
AC
4605int remote_hw_watchpoint_limit = -1;
4606int remote_hw_breakpoint_limit = -1;
d471ea57 4607
b9362cc7 4608static int
3c3bea1c 4609remote_check_watch_resources (int type, int cnt, int ot)
96baa820 4610{
3c3bea1c
GS
4611 if (type == bp_hardware_breakpoint)
4612 {
4613 if (remote_hw_breakpoint_limit == 0)
4614 return 0;
501eef12
AC
4615 else if (remote_hw_breakpoint_limit < 0)
4616 return 1;
3c3bea1c
GS
4617 else if (cnt <= remote_hw_breakpoint_limit)
4618 return 1;
4619 }
4620 else
4621 {
4622 if (remote_hw_watchpoint_limit == 0)
4623 return 0;
501eef12
AC
4624 else if (remote_hw_watchpoint_limit < 0)
4625 return 1;
3c3bea1c
GS
4626 else if (ot)
4627 return -1;
4628 else if (cnt <= remote_hw_watchpoint_limit)
4629 return 1;
4630 }
4631 return -1;
4632}
4633
b9362cc7 4634static int
3c3bea1c
GS
4635remote_stopped_by_watchpoint (void)
4636{
4637 return remote_stopped_by_watchpoint_p;
4638}
4639
7270d8f2
OF
4640extern int stepped_after_stopped_by_watchpoint;
4641
4aa7a7f5
JJ
4642static int
4643remote_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p)
3c3bea1c 4644{
4aa7a7f5 4645 int rc = 0;
7270d8f2
OF
4646 if (remote_stopped_by_watchpoint ()
4647 || stepped_after_stopped_by_watchpoint)
4aa7a7f5
JJ
4648 {
4649 *addr_p = remote_watch_data_address;
4650 rc = 1;
4651 }
4652
4653 return rc;
3c3bea1c
GS
4654}
4655
4656
4657static int
4658remote_insert_hw_breakpoint (CORE_ADDR addr, char *shadow)
4659{
4660 int len = 0;
d01949b6
AC
4661 struct remote_state *rs = get_remote_state ();
4662 char *buf = alloca (rs->remote_packet_size);
e514a9d6 4663 char *p = buf;
802188a7 4664
c8189ed1
GS
4665 /* The length field should be set to the size of a breakpoint
4666 instruction. */
4667
802188a7 4668 BREAKPOINT_FROM_PC (&addr, &len);
3c3bea1c 4669
d471ea57
AC
4670 if (remote_protocol_Z[Z_PACKET_HARDWARE_BP].support == PACKET_DISABLE)
4671 error ("Can't set hardware breakpoint without the '%s' (%s) packet\n",
4672 remote_protocol_Z[Z_PACKET_HARDWARE_BP].name,
4673 remote_protocol_Z[Z_PACKET_HARDWARE_BP].title);
802188a7 4674
96baa820
JM
4675 *(p++) = 'Z';
4676 *(p++) = '1';
4677 *(p++) = ',';
802188a7 4678
96baa820
JM
4679 addr = remote_address_masked (addr);
4680 p += hexnumstr (p, (ULONGEST) addr);
ad6525fc 4681 sprintf (p, ",%x", len);
96baa820
JM
4682
4683 putpkt (buf);
d01949b6 4684 getpkt (buf, (rs->remote_packet_size), 0);
96baa820 4685
d471ea57
AC
4686 switch (packet_ok (buf, &remote_protocol_Z[Z_PACKET_HARDWARE_BP]))
4687 {
4688 case PACKET_ERROR:
4689 case PACKET_UNKNOWN:
4690 return -1;
4691 case PACKET_OK:
4692 return 0;
4693 }
8e65ff28 4694 internal_error (__FILE__, __LINE__,
3c3bea1c 4695 "remote_insert_hw_breakpoint: reached end of function");
96baa820
JM
4696}
4697
d471ea57 4698
802188a7 4699static int
3c3bea1c 4700remote_remove_hw_breakpoint (CORE_ADDR addr, char *shadow)
96baa820 4701{
3c3bea1c 4702 int len;
d01949b6
AC
4703 struct remote_state *rs = get_remote_state ();
4704 char *buf = alloca (rs->remote_packet_size);
e514a9d6 4705 char *p = buf;
c8189ed1
GS
4706
4707 /* The length field should be set to the size of a breakpoint
4708 instruction. */
4709
4710 BREAKPOINT_FROM_PC (&addr, &len);
4711
d471ea57
AC
4712 if (remote_protocol_Z[Z_PACKET_HARDWARE_BP].support == PACKET_DISABLE)
4713 error ("Can't clear hardware breakpoint without the '%s' (%s) packet\n",
4714 remote_protocol_Z[Z_PACKET_HARDWARE_BP].name,
4715 remote_protocol_Z[Z_PACKET_HARDWARE_BP].title);
802188a7 4716
96baa820
JM
4717 *(p++) = 'z';
4718 *(p++) = '1';
4719 *(p++) = ',';
802188a7 4720
96baa820
JM
4721 addr = remote_address_masked (addr);
4722 p += hexnumstr (p, (ULONGEST) addr);
ad6525fc 4723 sprintf (p, ",%x", len);
96baa820
JM
4724
4725 putpkt(buf);
d01949b6 4726 getpkt (buf, (rs->remote_packet_size), 0);
802188a7 4727
d471ea57
AC
4728 switch (packet_ok (buf, &remote_protocol_Z[Z_PACKET_HARDWARE_BP]))
4729 {
4730 case PACKET_ERROR:
4731 case PACKET_UNKNOWN:
4732 return -1;
4733 case PACKET_OK:
4734 return 0;
4735 }
8e65ff28 4736 internal_error (__FILE__, __LINE__,
3c3bea1c 4737 "remote_remove_hw_breakpoint: reached end of function");
96baa820 4738}
96baa820 4739
c906108c
SS
4740/* Some targets are only capable of doing downloads, and afterwards
4741 they switch to the remote serial protocol. This function provides
4742 a clean way to get from the download target to the remote target.
4743 It's basically just a wrapper so that we don't have to expose any
4744 of the internal workings of remote.c.
4745
4746 Prior to calling this routine, you should shutdown the current
4747 target code, else you will get the "A program is being debugged
4748 already..." message. Usually a call to pop_target() suffices. */
4749
4750void
fba45db2 4751push_remote_target (char *name, int from_tty)
c906108c
SS
4752{
4753 printf_filtered ("Switching to remote protocol\n");
4754 remote_open (name, from_tty);
4755}
4756
c906108c
SS
4757/* Table used by the crc32 function to calcuate the checksum. */
4758
c5aa993b
JM
4759static unsigned long crc32_table[256] =
4760{0, 0};
c906108c
SS
4761
4762static unsigned long
fba45db2 4763crc32 (unsigned char *buf, int len, unsigned int crc)
c906108c 4764{
c5aa993b 4765 if (!crc32_table[1])
c906108c
SS
4766 {
4767 /* Initialize the CRC table and the decoding table. */
4768 int i, j;
4769 unsigned int c;
4770
4771 for (i = 0; i < 256; i++)
c5aa993b
JM
4772 {
4773 for (c = i << 24, j = 8; j > 0; --j)
4774 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
4775 crc32_table[i] = c;
4776 }
c906108c
SS
4777 }
4778
4779 while (len--)
4780 {
4781 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buf) & 255];
4782 buf++;
4783 }
4784 return crc;
4785}
4786
4787/* compare-sections command
4788
4789 With no arguments, compares each loadable section in the exec bfd
4790 with the same memory range on the target, and reports mismatches.
4791 Useful for verifying the image on the target against the exec file.
4792 Depends on the target understanding the new "qCRC:" request. */
4793
e514a9d6
JM
4794/* FIXME: cagney/1999-10-26: This command should be broken down into a
4795 target method (target verify memory) and generic version of the
4796 actual command. This will allow other high-level code (especially
4797 generic_load()) to make use of this target functionality. */
4798
c906108c 4799static void
fba45db2 4800compare_sections_command (char *args, int from_tty)
c906108c 4801{
d01949b6 4802 struct remote_state *rs = get_remote_state ();
c906108c
SS
4803 asection *s;
4804 unsigned long host_crc, target_crc;
4805 extern bfd *exec_bfd;
4806 struct cleanup *old_chain;
085dd6e6
JM
4807 char *tmp;
4808 char *sectdata;
ce359b09 4809 const char *sectname;
d01949b6 4810 char *buf = alloca (rs->remote_packet_size);
c906108c
SS
4811 bfd_size_type size;
4812 bfd_vma lma;
4813 int matched = 0;
4814 int mismatched = 0;
4815
4816 if (!exec_bfd)
4817 error ("command cannot be used without an exec file");
4818 if (!current_target.to_shortname ||
4819 strcmp (current_target.to_shortname, "remote") != 0)
4820 error ("command can only be used with remote target");
4821
c5aa993b 4822 for (s = exec_bfd->sections; s; s = s->next)
c906108c
SS
4823 {
4824 if (!(s->flags & SEC_LOAD))
c5aa993b 4825 continue; /* skip non-loadable section */
c906108c 4826
2c500098 4827 size = bfd_get_section_size (s);
c906108c 4828 if (size == 0)
c5aa993b 4829 continue; /* skip zero-length section */
c906108c 4830
ce359b09 4831 sectname = bfd_get_section_name (exec_bfd, s);
c906108c 4832 if (args && strcmp (args, sectname) != 0)
c5aa993b 4833 continue; /* not the section selected by user */
c906108c 4834
c5aa993b 4835 matched = 1; /* do this section */
c906108c
SS
4836 lma = s->lma;
4837 /* FIXME: assumes lma can fit into long */
4838 sprintf (buf, "qCRC:%lx,%lx", (long) lma, (long) size);
4839 putpkt (buf);
4840
4841 /* be clever; compute the host_crc before waiting for target reply */
4842 sectdata = xmalloc (size);
b8c9b27d 4843 old_chain = make_cleanup (xfree, sectdata);
c906108c
SS
4844 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
4845 host_crc = crc32 ((unsigned char *) sectdata, size, 0xffffffff);
4846
d01949b6 4847 getpkt (buf, (rs->remote_packet_size), 0);
c906108c 4848 if (buf[0] == 'E')
823ca731
AC
4849 error ("target memory fault, section %s, range 0x%s -- 0x%s",
4850 sectname, paddr (lma), paddr (lma + size));
c906108c
SS
4851 if (buf[0] != 'C')
4852 error ("remote target does not support this operation");
4853
4854 for (target_crc = 0, tmp = &buf[1]; *tmp; tmp++)
4855 target_crc = target_crc * 16 + fromhex (*tmp);
4856
d4f3574e
SS
4857 printf_filtered ("Section %s, range 0x%s -- 0x%s: ",
4858 sectname, paddr (lma), paddr (lma + size));
c906108c
SS
4859 if (host_crc == target_crc)
4860 printf_filtered ("matched.\n");
4861 else
c5aa993b
JM
4862 {
4863 printf_filtered ("MIS-MATCHED!\n");
4864 mismatched++;
4865 }
c906108c
SS
4866
4867 do_cleanups (old_chain);
4868 }
4869 if (mismatched > 0)
4870 warning ("One or more sections of the remote executable does not match\n\
4871the loaded file\n");
4872 if (args && !matched)
4873 printf_filtered ("No loaded section named '%s'.\n", args);
4874}
4875
1e3ff5ad 4876static LONGEST
4b8a223f 4877remote_xfer_partial (struct target_ops *ops, enum target_object object,
8aa91c1e 4878 const char *annex, void *readbuf, const void *writebuf,
1e3ff5ad 4879 ULONGEST offset, LONGEST len)
c906108c 4880{
d01949b6 4881 struct remote_state *rs = get_remote_state ();
c906108c 4882 int i;
d01949b6 4883 char *buf2 = alloca (rs->remote_packet_size);
c906108c 4884 char *p2 = &buf2[0];
1e3ff5ad 4885 char query_type;
c906108c 4886
21e3b9b9
DJ
4887 /* Handle memory using remote_xfer_memory. */
4888 if (object == TARGET_OBJECT_MEMORY)
4889 {
4890 int xfered;
4891 errno = 0;
4892
4893 if (writebuf != NULL)
4894 {
4895 void *buffer = xmalloc (len);
4896 struct cleanup *cleanup = make_cleanup (xfree, buffer);
4897 memcpy (buffer, writebuf, len);
4898 xfered = remote_xfer_memory (offset, buffer, len, 1, NULL, ops);
4899 do_cleanups (cleanup);
4900 }
4901 else
4902 xfered = remote_xfer_memory (offset, readbuf, len, 0, NULL, ops);
4903
4904 if (xfered > 0)
4905 return xfered;
4906 else if (xfered == 0 && errno == 0)
4907 return 0;
4908 else
4909 return -1;
4910 }
4911
4b8a223f
AC
4912 /* Only handle reads. */
4913 if (writebuf != NULL || readbuf == NULL)
4914 return -1;
4915
1e3ff5ad
AC
4916 /* Map pre-existing objects onto letters. DO NOT do this for new
4917 objects!!! Instead specify new query packets. */
4918 switch (object)
c906108c 4919 {
1e3ff5ad
AC
4920 case TARGET_OBJECT_KOD:
4921 query_type = 'K';
4922 break;
4923 case TARGET_OBJECT_AVR:
4924 query_type = 'R';
4925 break;
802188a7
RM
4926
4927 case TARGET_OBJECT_AUXV:
4928 if (remote_protocol_qPart_auxv.support != PACKET_DISABLE)
4929 {
4930 unsigned int total = 0;
4931 while (len > 0)
4932 {
4933 LONGEST n = min ((rs->remote_packet_size - 2) / 2, len);
4934 snprintf (buf2, rs->remote_packet_size,
4935 "qPart:auxv:read::%s,%s",
4936 phex_nz (offset, sizeof offset),
4937 phex_nz (n, sizeof n));
4938 i = putpkt (buf2);
4939 if (i < 0)
4940 return total > 0 ? total : i;
4941 buf2[0] = '\0';
4942 getpkt (buf2, rs->remote_packet_size, 0);
4943 if (packet_ok (buf2, &remote_protocol_qPart_auxv) != PACKET_OK)
4944 return total > 0 ? total : -1;
4945 if (buf2[0] == 'O' && buf2[1] == 'K' && buf2[2] == '\0')
4946 break; /* Got EOF indicator. */
4947 /* Got some data. */
4948 i = hex2bin (buf2, readbuf, len);
4949 if (i > 0)
4950 {
4951 readbuf = (void *) ((char *) readbuf + i);
4952 offset += i;
4953 len -= i;
4954 total += i;
4955 }
4956 }
4957 return total;
4958 }
4959 return -1;
4960
1e3ff5ad 4961 default:
c906108c
SS
4962 return -1;
4963 }
4964
4b8a223f 4965 /* Note: a zero OFFSET and LEN can be used to query the minimum
1e3ff5ad 4966 buffer size. */
4b8a223f 4967 if (offset == 0 && len == 0)
1e3ff5ad
AC
4968 return (rs->remote_packet_size);
4969 /* Minimum outbuf size is (rs->remote_packet_size) - if bufsiz is
4970 not large enough let the caller. */
4971 if (len < (rs->remote_packet_size))
4972 return -1;
4973 len = rs->remote_packet_size;
4974
c906108c 4975 /* except for querying the minimum buffer size, target must be open */
c5aa993b 4976 if (!remote_desc)
c906108c
SS
4977 error ("remote query is only available after target open");
4978
1e3ff5ad 4979 gdb_assert (annex != NULL);
4b8a223f 4980 gdb_assert (readbuf != NULL);
c906108c
SS
4981
4982 *p2++ = 'q';
4983 *p2++ = query_type;
4984
4985 /* we used one buffer char for the remote protocol q command and another
4986 for the query type. As the remote protocol encapsulation uses 4 chars
4987 plus one extra in case we are debugging (remote_debug),
4988 we have PBUFZIZ - 7 left to pack the query string */
4989 i = 0;
1e3ff5ad 4990 while (annex[i] && (i < ((rs->remote_packet_size) - 8)))
c906108c 4991 {
1e3ff5ad
AC
4992 /* Bad caller may have sent forbidden characters. */
4993 gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#');
4994 *p2++ = annex[i];
c906108c
SS
4995 i++;
4996 }
1e3ff5ad
AC
4997 *p2 = '\0';
4998 gdb_assert (annex[i] == '\0');
c906108c
SS
4999
5000 i = putpkt (buf2);
c5aa993b
JM
5001 if (i < 0)
5002 return i;
c906108c 5003
4b8a223f 5004 getpkt (readbuf, len, 0);
c906108c 5005
4b8a223f 5006 return strlen (readbuf);
c906108c
SS
5007}
5008
96baa820
JM
5009static void
5010remote_rcmd (char *command,
d9fcf2fb 5011 struct ui_file *outbuf)
96baa820 5012{
d01949b6 5013 struct remote_state *rs = get_remote_state ();
96baa820 5014 int i;
d01949b6 5015 char *buf = alloca (rs->remote_packet_size);
96baa820
JM
5016 char *p = buf;
5017
5018 if (!remote_desc)
5019 error ("remote rcmd is only available after target open");
5020
7be570e7
JM
5021 /* Send a NULL command across as an empty command */
5022 if (command == NULL)
5023 command = "";
5024
96baa820
JM
5025 /* The query prefix */
5026 strcpy (buf, "qRcmd,");
5027 p = strchr (buf, '\0');
5028
d01949b6 5029 if ((strlen (buf) + strlen (command) * 2 + 8/*misc*/) > (rs->remote_packet_size))
96baa820
JM
5030 error ("\"monitor\" command ``%s'' is too long\n", command);
5031
5032 /* Encode the actual command */
30559e10 5033 bin2hex (command, p, 0);
96baa820
JM
5034
5035 if (putpkt (buf) < 0)
5036 error ("Communication problem with target\n");
5037
5038 /* get/display the response */
5039 while (1)
5040 {
5041 /* XXX - see also tracepoint.c:remote_get_noisy_reply() */
5042 buf[0] = '\0';
d01949b6 5043 getpkt (buf, (rs->remote_packet_size), 0);
96baa820
JM
5044 if (buf[0] == '\0')
5045 error ("Target does not support this command\n");
5046 if (buf[0] == 'O' && buf[1] != 'K')
5047 {
5048 remote_console_output (buf + 1); /* 'O' message from stub */
5049 continue;
5050 }
5051 if (strcmp (buf, "OK") == 0)
5052 break;
7be570e7
JM
5053 if (strlen (buf) == 3 && buf[0] == 'E'
5054 && isdigit (buf[1]) && isdigit (buf[2]))
5055 {
5056 error ("Protocol error with Rcmd");
5057 }
96baa820
JM
5058 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
5059 {
5060 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
5061 fputc_unfiltered (c, outbuf);
5062 }
5063 break;
5064 }
5065}
5066
c906108c 5067static void
fba45db2 5068packet_command (char *args, int from_tty)
c906108c 5069{
d01949b6
AC
5070 struct remote_state *rs = get_remote_state ();
5071 char *buf = alloca (rs->remote_packet_size);
c906108c 5072
c5aa993b 5073 if (!remote_desc)
c906108c
SS
5074 error ("command can only be used with remote target");
5075
c5aa993b 5076 if (!args)
c906108c
SS
5077 error ("remote-packet command requires packet text as argument");
5078
5079 puts_filtered ("sending: ");
5080 print_packet (args);
5081 puts_filtered ("\n");
5082 putpkt (args);
5083
d01949b6 5084 getpkt (buf, (rs->remote_packet_size), 0);
c906108c
SS
5085 puts_filtered ("received: ");
5086 print_packet (buf);
5087 puts_filtered ("\n");
5088}
5089
5090#if 0
5091/* --------- UNIT_TEST for THREAD oriented PACKETS ------------------------- */
5092
a14ed312 5093static void display_thread_info (struct gdb_ext_thread_info *info);
c906108c 5094
a14ed312 5095static void threadset_test_cmd (char *cmd, int tty);
c906108c 5096
a14ed312 5097static void threadalive_test (char *cmd, int tty);
c906108c 5098
a14ed312 5099static void threadlist_test_cmd (char *cmd, int tty);
c906108c 5100
a14ed312 5101int get_and_display_threadinfo (threadref * ref);
c906108c 5102
a14ed312 5103static void threadinfo_test_cmd (char *cmd, int tty);
c906108c 5104
a14ed312 5105static int thread_display_step (threadref * ref, void *context);
c906108c 5106
a14ed312 5107static void threadlist_update_test_cmd (char *cmd, int tty);
c906108c 5108
a14ed312 5109static void init_remote_threadtests (void);
c906108c 5110
c5aa993b 5111#define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid */
c906108c
SS
5112
5113static void
fba45db2 5114threadset_test_cmd (char *cmd, int tty)
c906108c
SS
5115{
5116 int sample_thread = SAMPLE_THREAD;
5117
5118 printf_filtered ("Remote threadset test\n");
5119 set_thread (sample_thread, 1);
5120}
5121
5122
5123static void
fba45db2 5124threadalive_test (char *cmd, int tty)
c906108c
SS
5125{
5126 int sample_thread = SAMPLE_THREAD;
5127
39f77062 5128 if (remote_thread_alive (pid_to_ptid (sample_thread)))
c906108c
SS
5129 printf_filtered ("PASS: Thread alive test\n");
5130 else
5131 printf_filtered ("FAIL: Thread alive test\n");
5132}
5133
a14ed312 5134void output_threadid (char *title, threadref * ref);
c906108c
SS
5135
5136void
fba45db2 5137output_threadid (char *title, threadref *ref)
c906108c
SS
5138{
5139 char hexid[20];
5140
5141 pack_threadid (&hexid[0], ref); /* Convert threead id into hex */
5142 hexid[16] = 0;
5143 printf_filtered ("%s %s\n", title, (&hexid[0]));
5144}
5145
5146static void
fba45db2 5147threadlist_test_cmd (char *cmd, int tty)
c906108c
SS
5148{
5149 int startflag = 1;
5150 threadref nextthread;
5151 int done, result_count;
5152 threadref threadlist[3];
5153
5154 printf_filtered ("Remote Threadlist test\n");
5155 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
5156 &result_count, &threadlist[0]))
5157 printf_filtered ("FAIL: threadlist test\n");
5158 else
5159 {
5160 threadref *scan = threadlist;
5161 threadref *limit = scan + result_count;
5162
5163 while (scan < limit)
5164 output_threadid (" thread ", scan++);
5165 }
5166}
5167
5168void
fba45db2 5169display_thread_info (struct gdb_ext_thread_info *info)
c906108c
SS
5170{
5171 output_threadid ("Threadid: ", &info->threadid);
5172 printf_filtered ("Name: %s\n ", info->shortname);
5173 printf_filtered ("State: %s\n", info->display);
5174 printf_filtered ("other: %s\n\n", info->more_display);
5175}
5176
5177int
fba45db2 5178get_and_display_threadinfo (threadref *ref)
c906108c
SS
5179{
5180 int result;
5181 int set;
5182 struct gdb_ext_thread_info threadinfo;
5183
5184 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
5185 | TAG_MOREDISPLAY | TAG_DISPLAY;
5186 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
5187 display_thread_info (&threadinfo);
5188 return result;
5189}
5190
5191static void
fba45db2 5192threadinfo_test_cmd (char *cmd, int tty)
c906108c
SS
5193{
5194 int athread = SAMPLE_THREAD;
5195 threadref thread;
5196 int set;
5197
5198 int_to_threadref (&thread, athread);
5199 printf_filtered ("Remote Threadinfo test\n");
5200 if (!get_and_display_threadinfo (&thread))
5201 printf_filtered ("FAIL cannot get thread info\n");
5202}
5203
5204static int
fba45db2 5205thread_display_step (threadref *ref, void *context)
c906108c
SS
5206{
5207 /* output_threadid(" threadstep ",ref); *//* simple test */
5208 return get_and_display_threadinfo (ref);
5209}
5210
5211static void
fba45db2 5212threadlist_update_test_cmd (char *cmd, int tty)
c906108c
SS
5213{
5214 printf_filtered ("Remote Threadlist update test\n");
5215 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
5216}
5217
5218static void
5219init_remote_threadtests (void)
5220{
5221 add_com ("tlist", class_obscure, threadlist_test_cmd,
5222 "Fetch and print the remote list of thread identifiers, one pkt only");
5223 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
5224 "Fetch and display info about one thread");
5225 add_com ("tset", class_obscure, threadset_test_cmd,
5226 "Test setting to a different thread");
5227 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
5228 "Iterate through updating all remote thread info");
5229 add_com ("talive", class_obscure, threadalive_test,
5230 " Remote thread alive test ");
5231}
5232
5233#endif /* 0 */
5234
f3fb8c85
MS
5235/* Convert a thread ID to a string. Returns the string in a static
5236 buffer. */
5237
5238static char *
39f77062 5239remote_pid_to_str (ptid_t ptid)
f3fb8c85
MS
5240{
5241 static char buf[30];
5242
39f77062 5243 sprintf (buf, "Thread %d", PIDGET (ptid));
f3fb8c85
MS
5244 return buf;
5245}
5246
c906108c 5247static void
fba45db2 5248init_remote_ops (void)
c906108c 5249{
c5aa993b 5250 remote_ops.to_shortname = "remote";
c906108c 5251 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
c5aa993b 5252 remote_ops.to_doc =
c906108c 5253 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
0d06e24b
JM
5254Specify the serial device it is connected to\n\
5255(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
c5aa993b
JM
5256 remote_ops.to_open = remote_open;
5257 remote_ops.to_close = remote_close;
c906108c 5258 remote_ops.to_detach = remote_detach;
6ad8ae5c 5259 remote_ops.to_disconnect = remote_disconnect;
c5aa993b 5260 remote_ops.to_resume = remote_resume;
c906108c
SS
5261 remote_ops.to_wait = remote_wait;
5262 remote_ops.to_fetch_registers = remote_fetch_registers;
5263 remote_ops.to_store_registers = remote_store_registers;
5264 remote_ops.to_prepare_to_store = remote_prepare_to_store;
c8e73a31 5265 remote_ops.deprecated_xfer_memory = remote_xfer_memory;
c5aa993b 5266 remote_ops.to_files_info = remote_files_info;
c906108c
SS
5267 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
5268 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
3c3bea1c
GS
5269 remote_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
5270 remote_ops.to_stopped_data_address = remote_stopped_data_address;
5271 remote_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
5272 remote_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
5273 remote_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
5274 remote_ops.to_insert_watchpoint = remote_insert_watchpoint;
5275 remote_ops.to_remove_watchpoint = remote_remove_watchpoint;
c5aa993b
JM
5276 remote_ops.to_kill = remote_kill;
5277 remote_ops.to_load = generic_load;
c906108c
SS
5278 remote_ops.to_mourn_inferior = remote_mourn;
5279 remote_ops.to_thread_alive = remote_thread_alive;
0f71a2f6 5280 remote_ops.to_find_new_threads = remote_threads_info;
0caabb7e 5281 remote_ops.to_pid_to_str = remote_pid_to_str;
cf759d3b 5282 remote_ops.to_extra_thread_info = remote_threads_extra_info;
c906108c 5283 remote_ops.to_stop = remote_stop;
4b8a223f 5284 remote_ops.to_xfer_partial = remote_xfer_partial;
96baa820 5285 remote_ops.to_rcmd = remote_rcmd;
c906108c 5286 remote_ops.to_stratum = process_stratum;
c5aa993b
JM
5287 remote_ops.to_has_all_memory = 1;
5288 remote_ops.to_has_memory = 1;
5289 remote_ops.to_has_stack = 1;
5290 remote_ops.to_has_registers = 1;
5291 remote_ops.to_has_execution = 1;
5292 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
5293 remote_ops.to_magic = OPS_MAGIC;
c906108c
SS
5294}
5295
5296/* Set up the extended remote vector by making a copy of the standard
5297 remote vector and adding to it. */
5298
5299static void
fba45db2 5300init_extended_remote_ops (void)
c906108c
SS
5301{
5302 extended_remote_ops = remote_ops;
5303
0f71a2f6 5304 extended_remote_ops.to_shortname = "extended-remote";
c5aa993b 5305 extended_remote_ops.to_longname =
c906108c 5306 "Extended remote serial target in gdb-specific protocol";
c5aa993b 5307 extended_remote_ops.to_doc =
c906108c
SS
5308 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5309Specify the serial device it is connected to (e.g. /dev/ttya).",
c5aa993b 5310 extended_remote_ops.to_open = extended_remote_open;
c906108c
SS
5311 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
5312 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
0f71a2f6
JM
5313}
5314
6426a772
JM
5315static int
5316remote_can_async_p (void)
5317{
5318 /* We're async whenever the serial device is. */
2cd58942 5319 return (current_target.to_async_mask_value) && serial_can_async_p (remote_desc);
6426a772
JM
5320}
5321
5322static int
5323remote_is_async_p (void)
5324{
5325 /* We're async whenever the serial device is. */
2cd58942 5326 return (current_target.to_async_mask_value) && serial_is_async_p (remote_desc);
6426a772
JM
5327}
5328
2acceee2
JM
5329/* Pass the SERIAL event on and up to the client. One day this code
5330 will be able to delay notifying the client of an event until the
5331 point where an entire packet has been received. */
5332
5333static void (*async_client_callback) (enum inferior_event_type event_type, void *context);
5334static void *async_client_context;
5335static serial_event_ftype remote_async_serial_handler;
5336
6426a772 5337static void
819cc324 5338remote_async_serial_handler (struct serial *scb, void *context)
6426a772 5339{
2acceee2
JM
5340 /* Don't propogate error information up to the client. Instead let
5341 the client find out about the error by querying the target. */
5342 async_client_callback (INF_REG_EVENT, async_client_context);
5343}
5344
5345static void
5346remote_async (void (*callback) (enum inferior_event_type event_type, void *context), void *context)
5347{
ed9a39eb 5348 if (current_target.to_async_mask_value == 0)
8e65ff28
AC
5349 internal_error (__FILE__, __LINE__,
5350 "Calling remote_async when async is masked");
ed9a39eb 5351
2acceee2
JM
5352 if (callback != NULL)
5353 {
2cd58942 5354 serial_async (remote_desc, remote_async_serial_handler, NULL);
2acceee2
JM
5355 async_client_callback = callback;
5356 async_client_context = context;
5357 }
5358 else
2cd58942 5359 serial_async (remote_desc, NULL, NULL);
6426a772
JM
5360}
5361
43ff13b4
JM
5362/* Target async and target extended-async.
5363
5364 This are temporary targets, until it is all tested. Eventually
5365 async support will be incorporated int the usual 'remote'
5366 target. */
5367
5368static void
c2d11a7d 5369init_remote_async_ops (void)
43ff13b4
JM
5370{
5371 remote_async_ops.to_shortname = "async";
c5aa993b
JM
5372 remote_async_ops.to_longname = "Remote serial target in async version of the gdb-specific protocol";
5373 remote_async_ops.to_doc =
43ff13b4
JM
5374 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5375Specify the serial device it is connected to (e.g. /dev/ttya).";
c5aa993b
JM
5376 remote_async_ops.to_open = remote_async_open;
5377 remote_async_ops.to_close = remote_close;
6ad8ae5c
DJ
5378 remote_async_ops.to_detach = remote_detach;
5379 remote_async_ops.to_disconnect = remote_disconnect;
c5aa993b
JM
5380 remote_async_ops.to_resume = remote_async_resume;
5381 remote_async_ops.to_wait = remote_async_wait;
5382 remote_async_ops.to_fetch_registers = remote_fetch_registers;
5383 remote_async_ops.to_store_registers = remote_store_registers;
5384 remote_async_ops.to_prepare_to_store = remote_prepare_to_store;
c8e73a31 5385 remote_async_ops.deprecated_xfer_memory = remote_xfer_memory;
c5aa993b 5386 remote_async_ops.to_files_info = remote_files_info;
43ff13b4
JM
5387 remote_async_ops.to_insert_breakpoint = remote_insert_breakpoint;
5388 remote_async_ops.to_remove_breakpoint = remote_remove_breakpoint;
3c3bea1c
GS
5389 remote_async_ops.to_can_use_hw_breakpoint = remote_check_watch_resources;
5390 remote_async_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint;
5391 remote_async_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint;
5392 remote_async_ops.to_insert_watchpoint = remote_insert_watchpoint;
5393 remote_async_ops.to_remove_watchpoint = remote_remove_watchpoint;
5394 remote_async_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint;
5395 remote_async_ops.to_stopped_data_address = remote_stopped_data_address;
6426a772
JM
5396 remote_async_ops.to_terminal_inferior = remote_async_terminal_inferior;
5397 remote_async_ops.to_terminal_ours = remote_async_terminal_ours;
c5aa993b
JM
5398 remote_async_ops.to_kill = remote_async_kill;
5399 remote_async_ops.to_load = generic_load;
53a5351d 5400 remote_async_ops.to_mourn_inferior = remote_async_mourn;
c5aa993b
JM
5401 remote_async_ops.to_thread_alive = remote_thread_alive;
5402 remote_async_ops.to_find_new_threads = remote_threads_info;
cf759d3b
ND
5403 remote_async_ops.to_pid_to_str = remote_pid_to_str;
5404 remote_async_ops.to_extra_thread_info = remote_threads_extra_info;
43ff13b4 5405 remote_async_ops.to_stop = remote_stop;
4b8a223f 5406 remote_async_ops.to_xfer_partial = remote_xfer_partial;
96baa820 5407 remote_async_ops.to_rcmd = remote_rcmd;
c5aa993b
JM
5408 remote_async_ops.to_stratum = process_stratum;
5409 remote_async_ops.to_has_all_memory = 1;
5410 remote_async_ops.to_has_memory = 1;
5411 remote_async_ops.to_has_stack = 1;
5412 remote_async_ops.to_has_registers = 1;
5413 remote_async_ops.to_has_execution = 1;
5414 remote_async_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
6426a772
JM
5415 remote_async_ops.to_can_async_p = remote_can_async_p;
5416 remote_async_ops.to_is_async_p = remote_is_async_p;
5417 remote_async_ops.to_async = remote_async;
ed9a39eb 5418 remote_async_ops.to_async_mask_value = 1;
c5aa993b 5419 remote_async_ops.to_magic = OPS_MAGIC;
43ff13b4
JM
5420}
5421
5422/* Set up the async extended remote vector by making a copy of the standard
5423 remote vector and adding to it. */
5424
5425static void
c2d11a7d 5426init_extended_async_remote_ops (void)
43ff13b4
JM
5427{
5428 extended_async_remote_ops = remote_async_ops;
5429
5430 extended_async_remote_ops.to_shortname = "extended-async";
c5aa993b 5431 extended_async_remote_ops.to_longname =
43ff13b4 5432 "Extended remote serial target in async gdb-specific protocol";
c5aa993b 5433 extended_async_remote_ops.to_doc =
43ff13b4
JM
5434 "Use a remote computer via a serial line, using an async gdb-specific protocol.\n\
5435Specify the serial device it is connected to (e.g. /dev/ttya).",
c5aa993b 5436 extended_async_remote_ops.to_open = extended_remote_async_open;
43ff13b4
JM
5437 extended_async_remote_ops.to_create_inferior = extended_remote_async_create_inferior;
5438 extended_async_remote_ops.to_mourn_inferior = extended_remote_mourn;
5439}
5440
5a2468f5 5441static void
c2d11a7d 5442set_remote_cmd (char *args, int from_tty)
5a2468f5 5443{
5a2468f5
JM
5444}
5445
d471ea57
AC
5446static void
5447show_remote_cmd (char *args, int from_tty)
5448{
e9e68a56
AC
5449 /* FIXME: cagney/2002-06-15: This function should iterate over
5450 remote_show_cmdlist for a list of sub commands to show. */
5451 show_remote_protocol_Z_packet_cmd (args, from_tty, NULL);
e9e68a56 5452 show_remote_protocol_P_packet_cmd (args, from_tty, NULL);
b96ec7ac 5453 show_remote_protocol_p_packet_cmd (args, from_tty, NULL);
e9e68a56 5454 show_remote_protocol_qSymbol_packet_cmd (args, from_tty, NULL);
506fb367 5455 show_remote_protocol_vcont_packet_cmd (args, from_tty, NULL);
e9e68a56 5456 show_remote_protocol_binary_download_cmd (args, from_tty, NULL);
802188a7 5457 show_remote_protocol_qPart_auxv_packet_cmd (args, from_tty, NULL);
d471ea57 5458}
5a2468f5 5459
0f71a2f6 5460static void
fba45db2 5461build_remote_gdbarch_data (void)
0f71a2f6 5462{
d696208f 5463 remote_address_size = TARGET_ADDR_BIT;
0f71a2f6
JM
5464}
5465
dc8acb97
MS
5466/* Saved pointer to previous owner of the new_objfile event. */
5467static void (*remote_new_objfile_chain) (struct objfile *);
5468
5469/* Function to be called whenever a new objfile (shlib) is detected. */
5470static void
5471remote_new_objfile (struct objfile *objfile)
5472{
5473 if (remote_desc != 0) /* Have a remote connection */
5474 {
5475 remote_check_symbols (objfile);
5476 }
5477 /* Call predecessor on chain, if any. */
5478 if (remote_new_objfile_chain != 0 &&
5479 remote_desc == 0)
5480 remote_new_objfile_chain (objfile);
5481}
5482
c906108c 5483void
fba45db2 5484_initialize_remote (void)
c906108c 5485{
5a2468f5
JM
5486 static struct cmd_list_element *remote_set_cmdlist;
5487 static struct cmd_list_element *remote_show_cmdlist;
11cf8741 5488 struct cmd_list_element *tmpcmd;
5a2468f5 5489
0f71a2f6 5490 /* architecture specific data */
030f20e1 5491 remote_gdbarch_data_handle = gdbarch_data_register_post_init (init_remote_state);
d01949b6
AC
5492
5493 /* Old tacky stuff. NOTE: This comes after the remote protocol so
5494 that the remote protocol has been initialized. */
046a4708
AC
5495 DEPRECATED_REGISTER_GDBARCH_SWAP (remote_address_size);
5496 deprecated_register_gdbarch_swap (NULL, 0, build_remote_gdbarch_data);
0f71a2f6 5497
c906108c
SS
5498 init_remote_ops ();
5499 add_target (&remote_ops);
5500
5501 init_extended_remote_ops ();
5502 add_target (&extended_remote_ops);
cce74817 5503
43ff13b4
JM
5504 init_remote_async_ops ();
5505 add_target (&remote_async_ops);
5506
5507 init_extended_async_remote_ops ();
5508 add_target (&extended_async_remote_ops);
5509
dc8acb97 5510 /* Hook into new objfile notification. */
9a4105ab
AC
5511 remote_new_objfile_chain = deprecated_target_new_objfile_hook;
5512 deprecated_target_new_objfile_hook = remote_new_objfile;
dc8acb97 5513
c906108c
SS
5514#if 0
5515 init_remote_threadtests ();
5516#endif
5517
d471ea57
AC
5518 /* set/show remote ... */
5519
5a2468f5
JM
5520 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, "\
5521Remote protocol specific variables\n\
5522Configure various remote-protocol specific variables such as\n\
5523the packets being used",
cff3e48b 5524 &remote_set_cmdlist, "set remote ",
5a2468f5 5525 0/*allow-unknown*/, &setlist);
d471ea57 5526 add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, "\
5a2468f5
JM
5527Remote protocol specific variables\n\
5528Configure various remote-protocol specific variables such as\n\
5529the packets being used",
cff3e48b 5530 &remote_show_cmdlist, "show remote ",
5a2468f5
JM
5531 0/*allow-unknown*/, &showlist);
5532
c5aa993b 5533 add_cmd ("compare-sections", class_obscure, compare_sections_command,
c906108c 5534 "Compare section data on target to the exec file.\n\
c5aa993b 5535Argument is a single section name (default: all loaded sections).",
c906108c
SS
5536 &cmdlist);
5537
5538 add_cmd ("packet", class_maintenance, packet_command,
5539 "Send an arbitrary packet to a remote target.\n\
5540 maintenance packet TEXT\n\
5541If GDB is talking to an inferior via the GDB serial protocol, then\n\
5542this command sends the string TEXT to the inferior, and displays the\n\
5543response packet. GDB supplies the initial `$' character, and the\n\
5544terminating `#' character and checksum.",
5545 &maintenancelist);
5546
3b64bf98
AC
5547 add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, "\
5548Set whether to send break if interrupted.", "\
5549Show whether to send break if interrupted.", "\
5550If set, a break, instead of a cntrl-c, is sent to the remote target.", "\
5551Whether to send break if interrupted is %s.",
e707bbc2
AC
5552 NULL, NULL,
5553 &setlist, &showlist);
c906108c 5554
11cf8741
JM
5555 /* Install commands for configuring memory read/write packets. */
5556
5557 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size,
5558 "Set the maximum number of bytes per memory write packet (deprecated).\n",
5559 &setlist);
4ad5b0f7 5560 add_cmd ("remotewritesize", no_class, show_memory_write_packet_size,
11cf8741
JM
5561 "Show the maximum number of bytes per memory write packet (deprecated).\n",
5562 &showlist);
5563 add_cmd ("memory-write-packet-size", no_class,
5564 set_memory_write_packet_size,
5565 "Set the maximum number of bytes per memory-write packet.\n"
5566 "Specify the number of bytes in a packet or 0 (zero) for the\n"
5567 "default packet size. The actual limit is further reduced\n"
5568 "dependent on the target. Specify ``fixed'' to disable the\n"
5569 "further restriction and ``limit'' to enable that restriction\n",
5570 &remote_set_cmdlist);
5571 add_cmd ("memory-read-packet-size", no_class,
5572 set_memory_read_packet_size,
5573 "Set the maximum number of bytes per memory-read packet.\n"
5574 "Specify the number of bytes in a packet or 0 (zero) for the\n"
5575 "default packet size. The actual limit is further reduced\n"
5576 "dependent on the target. Specify ``fixed'' to disable the\n"
5577 "further restriction and ``limit'' to enable that restriction\n",
5578 &remote_set_cmdlist);
5579 add_cmd ("memory-write-packet-size", no_class,
5580 show_memory_write_packet_size,
5581 "Show the maximum number of bytes per memory-write packet.\n",
5582 &remote_show_cmdlist);
5583 add_cmd ("memory-read-packet-size", no_class,
5584 show_memory_read_packet_size,
5585 "Show the maximum number of bytes per memory-read packet.\n",
5586 &remote_show_cmdlist);
c906108c 5587
b3f42336
AC
5588 add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class,
5589 &remote_hw_watchpoint_limit, "\
3b64bf98
AC
5590Set the maximum number of target hardware watchpoints.", "\
5591Show the maximum number of target hardware watchpoints.", "\
501eef12 5592Specify a negative limit for unlimited.", "\
3b64bf98 5593The maximum number of target hardware watchpoints is %s.",
b3f42336
AC
5594 NULL, NULL,
5595 &remote_set_cmdlist, &remote_show_cmdlist);
5596 add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class,
5597 &remote_hw_breakpoint_limit, "\
3b64bf98
AC
5598Set the maximum number of target hardware breakpoints.", "\
5599Show the maximum number of target hardware breakpoints.", "\
501eef12 5600Specify a negative limit for unlimited.", "\
3b64bf98 5601The maximum number of target hardware breakpoints is %s.",
b3f42336
AC
5602 NULL, NULL,
5603 &remote_set_cmdlist, &remote_show_cmdlist);
501eef12 5604
cb1a6d5f 5605 deprecated_add_show_from_set
c906108c 5606 (add_set_cmd ("remoteaddresssize", class_obscure,
c5aa993b 5607 var_integer, (char *) &remote_address_size,
c906108c
SS
5608 "Set the maximum size of the address (in bits) \
5609in a memory packet.\n",
5610 &setlist),
c5aa993b 5611 &showlist);
c906108c 5612
96baa820
JM
5613 add_packet_config_cmd (&remote_protocol_binary_download,
5614 "X", "binary-download",
5615 set_remote_protocol_binary_download_cmd,
5616 show_remote_protocol_binary_download_cmd,
d471ea57
AC
5617 &remote_set_cmdlist, &remote_show_cmdlist,
5618 1);
96baa820
JM
5619#if 0
5620 /* XXXX - should ``set remotebinarydownload'' be retained for
5621 compatibility. */
cb1a6d5f 5622 deprecated_add_show_from_set
b83266a0
SS
5623 (add_set_cmd ("remotebinarydownload", no_class,
5624 var_boolean, (char *) &remote_binary_download,
5625 "Set binary downloads.\n", &setlist),
5626 &showlist);
96baa820 5627#endif
0f71a2f6 5628
506fb367
DJ
5629 add_packet_config_cmd (&remote_protocol_vcont,
5630 "vCont", "verbose-resume",
5631 set_remote_protocol_vcont_packet_cmd,
5632 show_remote_protocol_vcont_packet_cmd,
5633 &remote_set_cmdlist, &remote_show_cmdlist,
5634 0);
5635
dc8acb97
MS
5636 add_packet_config_cmd (&remote_protocol_qSymbol,
5637 "qSymbol", "symbol-lookup",
5638 set_remote_protocol_qSymbol_packet_cmd,
5639 show_remote_protocol_qSymbol_packet_cmd,
5640 &remote_set_cmdlist, &remote_show_cmdlist,
5641 0);
5642
d471ea57
AC
5643 add_packet_config_cmd (&remote_protocol_P,
5644 "P", "set-register",
5a2468f5
JM
5645 set_remote_protocol_P_packet_cmd,
5646 show_remote_protocol_P_packet_cmd,
d471ea57
AC
5647 &remote_set_cmdlist, &remote_show_cmdlist,
5648 1);
5649
b96ec7ac
AC
5650 add_packet_config_cmd (&remote_protocol_p,
5651 "p", "fetch-register",
5652 set_remote_protocol_p_packet_cmd,
5653 show_remote_protocol_p_packet_cmd,
5654 &remote_set_cmdlist, &remote_show_cmdlist,
5655 1);
5656
d471ea57
AC
5657 add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_SOFTWARE_BP],
5658 "Z0", "software-breakpoint",
5659 set_remote_protocol_Z_software_bp_packet_cmd,
5660 show_remote_protocol_Z_software_bp_packet_cmd,
5661 &remote_set_cmdlist, &remote_show_cmdlist,
5662 0);
5663
5664 add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_HARDWARE_BP],
5665 "Z1", "hardware-breakpoint",
5666 set_remote_protocol_Z_hardware_bp_packet_cmd,
5667 show_remote_protocol_Z_hardware_bp_packet_cmd,
5668 &remote_set_cmdlist, &remote_show_cmdlist,
5669 0);
5670
5671 add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_WRITE_WP],
5672 "Z2", "write-watchpoint",
5673 set_remote_protocol_Z_write_wp_packet_cmd,
5674 show_remote_protocol_Z_write_wp_packet_cmd,
5675 &remote_set_cmdlist, &remote_show_cmdlist,
5676 0);
5677
5678 add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_READ_WP],
5679 "Z3", "read-watchpoint",
5680 set_remote_protocol_Z_read_wp_packet_cmd,
5681 show_remote_protocol_Z_read_wp_packet_cmd,
5682 &remote_set_cmdlist, &remote_show_cmdlist,
5683 0);
5684
5685 add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_ACCESS_WP],
5686 "Z4", "access-watchpoint",
5687 set_remote_protocol_Z_access_wp_packet_cmd,
5688 show_remote_protocol_Z_access_wp_packet_cmd,
5689 &remote_set_cmdlist, &remote_show_cmdlist,
5690 0);
5691
802188a7
RM
5692 add_packet_config_cmd (&remote_protocol_qPart_auxv,
5693 "qPart_auxv", "read-aux-vector",
5694 set_remote_protocol_qPart_auxv_packet_cmd,
5695 show_remote_protocol_qPart_auxv_packet_cmd,
5696 &remote_set_cmdlist, &remote_show_cmdlist,
5697 0);
5698
d471ea57 5699 /* Keep the old ``set remote Z-packet ...'' working. */
e9e68a56
AC
5700 add_setshow_auto_boolean_cmd ("Z-packet", class_obscure,
5701 &remote_Z_packet_detect, "\
3b64bf98
AC
5702Set use of remote protocol `Z' packets", "\
5703Show use of remote protocol `Z' packets ", "\
5704When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
5705packets.", "\
5706Use of remote protocol `Z' packets is %s",
e9e68a56
AC
5707 set_remote_protocol_Z_packet_cmd,
5708 show_remote_protocol_Z_packet_cmd,
5709 &remote_set_cmdlist, &remote_show_cmdlist);
449092f6
CV
5710
5711 /* Eventually initialize fileio. See fileio.c */
5712 initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist);
c906108c 5713}
This page took 0.835304 seconds and 4 git commands to generate.