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