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