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