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