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