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