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