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