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