Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Remote target communications for serial-line targets in custom GDB protocol |
8926118c | 2 | |
6aba47ca | 3 | Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, |
4c38e0a4 JB |
4 | 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, |
5 | 2010 Free Software Foundation, Inc. | |
c906108c | 6 | |
c5aa993b JM |
7 | This file is part of GDB. |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 11 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b JM |
12 | (at your option) any later version. |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c5aa993b | 21 | |
23860348 | 22 | /* See the GDB User Guide for details of the GDB remote protocol. */ |
c5aa993b | 23 | |
c906108c SS |
24 | #include "defs.h" |
25 | #include "gdb_string.h" | |
26 | #include <ctype.h> | |
27 | #include <fcntl.h> | |
c906108c SS |
28 | #include "inferior.h" |
29 | #include "bfd.h" | |
30 | #include "symfile.h" | |
60250e8b | 31 | #include "exceptions.h" |
c906108c | 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" |
6867ae3e | 42 | #include "observer.h" |
a77053c2 | 43 | #include "solib.h" |
37a105a1 DJ |
44 | #include "cli/cli-decode.h" |
45 | #include "cli/cli-setshow.h" | |
424163ea | 46 | #include "target-descriptions.h" |
c906108c | 47 | |
7a292a7a | 48 | #include <ctype.h> |
9846de1b | 49 | #include <sys/time.h> |
c906108c | 50 | |
43ff13b4 | 51 | #include "event-loop.h" |
c2c6d25f | 52 | #include "event-top.h" |
2acceee2 | 53 | #include "inf-loop.h" |
43ff13b4 | 54 | |
c906108c SS |
55 | #include <signal.h> |
56 | #include "serial.h" | |
57 | ||
6240bebf MS |
58 | #include "gdbcore.h" /* for exec_bfd */ |
59 | ||
449092f6 | 60 | #include "remote-fileio.h" |
a6b151f1 | 61 | #include "gdb/fileio.h" |
3e88cf8d | 62 | #include "gdb_stat.h" |
dc146f7c | 63 | #include "xml-support.h" |
449092f6 | 64 | |
fd79ecee DJ |
65 | #include "memory-map.h" |
66 | ||
35b1e5cc SS |
67 | #include "tracepoint.h" |
68 | #include "ax.h" | |
69 | #include "ax-gdb.h" | |
70 | ||
71 | /* temp hacks for tracepoint encoding migration */ | |
72 | static char *target_buf; | |
73 | static long target_buf_size; | |
74 | /*static*/ void | |
9355b391 SS |
75 | encode_actions (struct breakpoint *t, struct bp_location *tloc, |
76 | char ***tdp_actions, char ***stepping_actions); | |
35b1e5cc | 77 | |
6765f3e5 DJ |
78 | /* The size to align memory write packets, when practical. The protocol |
79 | does not guarantee any alignment, and gdb will generate short | |
80 | writes and unaligned writes, but even as a best-effort attempt this | |
81 | can improve bulk transfers. For instance, if a write is misaligned | |
82 | relative to the target's data bus, the stub may need to make an extra | |
83 | round trip fetching data from the target. This doesn't make a | |
84 | huge difference, but it's easy to do, so we try to be helpful. | |
85 | ||
86 | The alignment chosen is arbitrary; usually data bus width is | |
87 | important here, not the possibly larger cache line size. */ | |
88 | enum { REMOTE_ALIGN_WRITES = 16 }; | |
89 | ||
23860348 | 90 | /* Prototypes for local functions. */ |
6426a772 JM |
91 | static void cleanup_sigint_signal_handler (void *dummy); |
92 | static void initialize_sigint_signal_handler (void); | |
6d820c5c | 93 | static int getpkt_sane (char **buf, long *sizeof_buf, int forever); |
74531fed PA |
94 | static int getpkt_or_notif_sane (char **buf, long *sizeof_buf, |
95 | int forever); | |
6426a772 | 96 | |
a14ed312 KB |
97 | static void handle_remote_sigint (int); |
98 | static void handle_remote_sigint_twice (int); | |
99 | static void async_remote_interrupt (gdb_client_data); | |
100 | void async_remote_interrupt_twice (gdb_client_data); | |
43ff13b4 | 101 | |
a14ed312 | 102 | static void remote_files_info (struct target_ops *ignore); |
c906108c | 103 | |
316f2060 | 104 | static void remote_prepare_to_store (struct regcache *regcache); |
c906108c | 105 | |
a14ed312 | 106 | static void remote_open (char *name, int from_tty); |
c906108c | 107 | |
a14ed312 | 108 | static void extended_remote_open (char *name, int from_tty); |
c906108c | 109 | |
75c99385 | 110 | static void remote_open_1 (char *, int, struct target_ops *, int extended_p); |
c906108c | 111 | |
a14ed312 | 112 | static void remote_close (int quitting); |
c906108c | 113 | |
136d6dae | 114 | static void remote_mourn (struct target_ops *ops); |
c906108c | 115 | |
a14ed312 | 116 | static void extended_remote_restart (void); |
c906108c | 117 | |
136d6dae | 118 | static void extended_remote_mourn (struct target_ops *); |
c906108c | 119 | |
a14ed312 | 120 | static void remote_mourn_1 (struct target_ops *); |
c906108c | 121 | |
6d820c5c | 122 | static void remote_send (char **buf, long *sizeof_buf_p); |
c906108c | 123 | |
a14ed312 | 124 | static int readchar (int timeout); |
c906108c | 125 | |
7d85a9c0 | 126 | static void remote_kill (struct target_ops *ops); |
c906108c | 127 | |
a14ed312 | 128 | static int tohex (int nib); |
c906108c | 129 | |
75c99385 PA |
130 | static int remote_can_async_p (void); |
131 | ||
132 | static int remote_is_async_p (void); | |
133 | ||
134 | static void remote_async (void (*callback) (enum inferior_event_type event_type, | |
135 | void *context), void *context); | |
136 | ||
137 | static int remote_async_mask (int new_mask); | |
138 | ||
136d6dae | 139 | static void remote_detach (struct target_ops *ops, char *args, int from_tty); |
c906108c | 140 | |
a14ed312 | 141 | static void remote_interrupt (int signo); |
c906108c | 142 | |
a14ed312 | 143 | static void remote_interrupt_twice (int signo); |
7a292a7a | 144 | |
a14ed312 | 145 | static void interrupt_query (void); |
c906108c | 146 | |
79d7f229 PA |
147 | static void set_general_thread (struct ptid ptid); |
148 | static void set_continue_thread (struct ptid ptid); | |
c906108c | 149 | |
a14ed312 | 150 | static void get_offsets (void); |
c906108c | 151 | |
6d820c5c DJ |
152 | static void skip_frame (void); |
153 | ||
154 | static long read_frame (char **buf_p, long *sizeof_buf); | |
c906108c | 155 | |
a14ed312 | 156 | static int hexnumlen (ULONGEST num); |
c906108c | 157 | |
a14ed312 | 158 | static void init_remote_ops (void); |
c906108c | 159 | |
a14ed312 | 160 | static void init_extended_remote_ops (void); |
c906108c | 161 | |
94cc34af | 162 | static void remote_stop (ptid_t); |
c906108c | 163 | |
a14ed312 | 164 | static int ishex (int ch, int *val); |
c906108c | 165 | |
a14ed312 | 166 | static int stubhex (int ch); |
c906108c | 167 | |
a14ed312 | 168 | static int hexnumstr (char *, ULONGEST); |
c906108c | 169 | |
a14ed312 | 170 | static int hexnumnstr (char *, ULONGEST, int); |
2df3850c | 171 | |
a14ed312 | 172 | static CORE_ADDR remote_address_masked (CORE_ADDR); |
c906108c | 173 | |
a14ed312 | 174 | static void print_packet (char *); |
c906108c | 175 | |
a14ed312 | 176 | static void compare_sections_command (char *, int); |
c906108c | 177 | |
a14ed312 | 178 | static void packet_command (char *, int); |
c906108c | 179 | |
a14ed312 | 180 | static int stub_unpack_int (char *buff, int fieldlength); |
c906108c | 181 | |
39f77062 | 182 | static ptid_t remote_current_thread (ptid_t oldptid); |
c906108c | 183 | |
a14ed312 | 184 | static void remote_find_new_threads (void); |
c906108c | 185 | |
79d7f229 | 186 | static void record_currthread (ptid_t currthread); |
c906108c | 187 | |
30559e10 | 188 | static int fromhex (int a); |
c906108c | 189 | |
00bf0b85 | 190 | extern int hex2bin (const char *hex, gdb_byte *bin, int count); |
c906108c | 191 | |
00bf0b85 | 192 | extern int bin2hex (const gdb_byte *bin, char *hex, int count); |
234fa6d1 | 193 | |
a14ed312 | 194 | static int putpkt_binary (char *buf, int cnt); |
c906108c | 195 | |
a14ed312 | 196 | static void check_binary_download (CORE_ADDR addr); |
c906108c | 197 | |
5a2468f5 | 198 | struct packet_config; |
5a2468f5 | 199 | |
a14ed312 | 200 | static void show_packet_config_cmd (struct packet_config *config); |
5a2468f5 | 201 | |
d471ea57 | 202 | static void update_packet_config (struct packet_config *config); |
5a2468f5 | 203 | |
bb572ddd DJ |
204 | static void set_remote_protocol_packet_cmd (char *args, int from_tty, |
205 | struct cmd_list_element *c); | |
206 | ||
207 | static void show_remote_protocol_packet_cmd (struct ui_file *file, | |
208 | int from_tty, | |
209 | struct cmd_list_element *c, | |
210 | const char *value); | |
211 | ||
82f73884 PA |
212 | static char *write_ptid (char *buf, const char *endbuf, ptid_t ptid); |
213 | static ptid_t read_ptid (char *buf, char **obuf); | |
214 | ||
d914c394 SS |
215 | static void remote_set_permissions (void); |
216 | ||
d5551862 | 217 | struct remote_state; |
00bf0b85 | 218 | static int remote_get_trace_status (struct trace_status *ts); |
d5551862 | 219 | |
00bf0b85 SS |
220 | static int remote_upload_tracepoints (struct uploaded_tp **utpp); |
221 | ||
222 | static int remote_upload_trace_state_variables (struct uploaded_tsv **utsvp); | |
223 | ||
c8d104ad PA |
224 | static void remote_query_supported (void); |
225 | ||
226 | static void remote_check_symbols (struct objfile *objfile); | |
227 | ||
a14ed312 | 228 | void _initialize_remote (void); |
c906108c | 229 | |
74531fed PA |
230 | struct stop_reply; |
231 | static struct stop_reply *stop_reply_xmalloc (void); | |
232 | static void stop_reply_xfree (struct stop_reply *); | |
233 | static void do_stop_reply_xfree (void *arg); | |
234 | static void remote_parse_stop_reply (char *buf, struct stop_reply *); | |
235 | static void push_stop_reply (struct stop_reply *); | |
236 | static void remote_get_pending_stop_replies (void); | |
237 | static void discard_pending_stop_replies (int pid); | |
238 | static int peek_stop_reply (ptid_t ptid); | |
239 | ||
240 | static void remote_async_inferior_event_handler (gdb_client_data); | |
241 | static void remote_async_get_pending_events_handler (gdb_client_data); | |
242 | ||
d3fd5342 PA |
243 | static void remote_terminal_ours (void); |
244 | ||
d962ef82 DJ |
245 | static int remote_read_description_p (struct target_ops *target); |
246 | ||
176a6961 | 247 | static void remote_console_output (char *msg); |
dde08ee1 | 248 | |
74531fed PA |
249 | /* The non-stop remote protocol provisions for one pending stop reply. |
250 | This is where we keep it until it is acknowledged. */ | |
251 | ||
252 | static struct stop_reply *pending_stop_reply = NULL; | |
253 | ||
a6b151f1 DJ |
254 | /* For "remote". */ |
255 | ||
256 | static struct cmd_list_element *remote_cmdlist; | |
257 | ||
bb572ddd DJ |
258 | /* For "set remote" and "show remote". */ |
259 | ||
260 | static struct cmd_list_element *remote_set_cmdlist; | |
261 | static struct cmd_list_element *remote_show_cmdlist; | |
262 | ||
ea9c271d DJ |
263 | /* Description of the remote protocol state for the currently |
264 | connected target. This is per-target state, and independent of the | |
265 | selected architecture. */ | |
266 | ||
267 | struct remote_state | |
268 | { | |
269 | /* A buffer to use for incoming packets, and its current size. The | |
270 | buffer is grown dynamically for larger incoming packets. | |
271 | Outgoing packets may also be constructed in this buffer. | |
272 | BUF_SIZE is always at least REMOTE_PACKET_SIZE; | |
273 | REMOTE_PACKET_SIZE should be used to limit the length of outgoing | |
274 | packets. */ | |
275 | char *buf; | |
276 | long buf_size; | |
be2a5f71 DJ |
277 | |
278 | /* If we negotiated packet size explicitly (and thus can bypass | |
279 | heuristics for the largest packet size that will not overflow | |
280 | a buffer in the stub), this will be set to that packet size. | |
281 | Otherwise zero, meaning to use the guessed size. */ | |
282 | long explicit_packet_size; | |
2d717e4f DJ |
283 | |
284 | /* remote_wait is normally called when the target is running and | |
285 | waits for a stop reply packet. But sometimes we need to call it | |
286 | when the target is already stopped. We can send a "?" packet | |
287 | and have remote_wait read the response. Or, if we already have | |
288 | the response, we can stash it in BUF and tell remote_wait to | |
289 | skip calling getpkt. This flag is set when BUF contains a | |
290 | stop reply packet and the target is not waiting. */ | |
291 | int cached_wait_status; | |
a6f3e723 SL |
292 | |
293 | /* True, if in no ack mode. That is, neither GDB nor the stub will | |
294 | expect acks from each other. The connection is assumed to be | |
295 | reliable. */ | |
296 | int noack_mode; | |
82f73884 PA |
297 | |
298 | /* True if we're connected in extended remote mode. */ | |
299 | int extended; | |
300 | ||
301 | /* True if the stub reported support for multi-process | |
302 | extensions. */ | |
303 | int multi_process_aware; | |
e24a49d8 PA |
304 | |
305 | /* True if we resumed the target and we're waiting for the target to | |
306 | stop. In the mean time, we can't start another command/query. | |
307 | The remote server wouldn't be ready to process it, so we'd | |
308 | timeout waiting for a reply that would never come and eventually | |
309 | we'd close the connection. This can happen in asynchronous mode | |
310 | because we allow GDB commands while the target is running. */ | |
311 | int waiting_for_stop_reply; | |
74531fed PA |
312 | |
313 | /* True if the stub reports support for non-stop mode. */ | |
314 | int non_stop_aware; | |
315 | ||
316 | /* True if the stub reports support for vCont;t. */ | |
317 | int support_vCont_t; | |
782b2b07 SS |
318 | |
319 | /* True if the stub reports support for conditional tracepoints. */ | |
320 | int cond_tracepoints; | |
3a29589a | 321 | |
7a697b8d SS |
322 | /* True if the stub reports support for fast tracepoints. */ |
323 | int fast_tracepoints; | |
324 | ||
0fb4aa4b PA |
325 | /* True if the stub reports support for static tracepoints. */ |
326 | int static_tracepoints; | |
327 | ||
d5551862 SS |
328 | /* True if the stub can continue running a trace while GDB is |
329 | disconnected. */ | |
330 | int disconnected_tracing; | |
331 | ||
3a29589a DJ |
332 | /* Nonzero if the user has pressed Ctrl-C, but the target hasn't |
333 | responded to that. */ | |
334 | int ctrlc_pending_p; | |
ea9c271d DJ |
335 | }; |
336 | ||
dc146f7c VP |
337 | /* Private data that we'll store in (struct thread_info)->private. */ |
338 | struct private_thread_info | |
339 | { | |
340 | char *extra; | |
341 | int core; | |
342 | }; | |
343 | ||
344 | static void | |
345 | free_private_thread_info (struct private_thread_info *info) | |
346 | { | |
347 | xfree (info->extra); | |
348 | xfree (info); | |
349 | } | |
350 | ||
82f73884 PA |
351 | /* Returns true if the multi-process extensions are in effect. */ |
352 | static int | |
353 | remote_multi_process_p (struct remote_state *rs) | |
354 | { | |
355 | return rs->extended && rs->multi_process_aware; | |
356 | } | |
357 | ||
ea9c271d DJ |
358 | /* This data could be associated with a target, but we do not always |
359 | have access to the current target when we need it, so for now it is | |
360 | static. This will be fine for as long as only one target is in use | |
361 | at a time. */ | |
362 | static struct remote_state remote_state; | |
363 | ||
364 | static struct remote_state * | |
0b83947e | 365 | get_remote_state_raw (void) |
ea9c271d DJ |
366 | { |
367 | return &remote_state; | |
368 | } | |
369 | ||
370 | /* Description of the remote protocol for a given architecture. */ | |
d01949b6 | 371 | |
ad10f812 AC |
372 | struct packet_reg |
373 | { | |
374 | long offset; /* Offset into G packet. */ | |
375 | long regnum; /* GDB's internal register number. */ | |
376 | LONGEST pnum; /* Remote protocol register number. */ | |
b323314b | 377 | int in_g_packet; /* Always part of G packet. */ |
1cf3db46 | 378 | /* long size in bytes; == register_size (target_gdbarch, regnum); |
23860348 | 379 | at present. */ |
1cf3db46 | 380 | /* char *name; == gdbarch_register_name (target_gdbarch, regnum); |
c9f4d572 | 381 | at present. */ |
ad10f812 AC |
382 | }; |
383 | ||
ea9c271d | 384 | struct remote_arch_state |
d01949b6 | 385 | { |
ad10f812 AC |
386 | /* Description of the remote protocol registers. */ |
387 | long sizeof_g_packet; | |
b323314b AC |
388 | |
389 | /* Description of the remote protocol registers indexed by REGNUM | |
f57d151a | 390 | (making an array gdbarch_num_regs in size). */ |
b323314b | 391 | struct packet_reg *regs; |
ad10f812 | 392 | |
d01949b6 AC |
393 | /* This is the size (in chars) of the first response to the ``g'' |
394 | packet. It is used as a heuristic when determining the maximum | |
395 | size of memory-read and memory-write packets. A target will | |
396 | typically only reserve a buffer large enough to hold the ``g'' | |
397 | packet. The size does not include packet overhead (headers and | |
23860348 | 398 | trailers). */ |
d01949b6 AC |
399 | long actual_register_packet_size; |
400 | ||
401 | /* This is the maximum size (in chars) of a non read/write packet. | |
23860348 | 402 | It is also used as a cap on the size of read/write packets. */ |
d01949b6 AC |
403 | long remote_packet_size; |
404 | }; | |
405 | ||
35b1e5cc SS |
406 | long sizeof_pkt = 2000; |
407 | ||
408 | /* Utility: generate error from an incoming stub packet. */ | |
409 | static void | |
410 | trace_error (char *buf) | |
411 | { | |
412 | if (*buf++ != 'E') | |
413 | return; /* not an error msg */ | |
414 | switch (*buf) | |
415 | { | |
416 | case '1': /* malformed packet error */ | |
417 | if (*++buf == '0') /* general case: */ | |
418 | error (_("remote.c: error in outgoing packet.")); | |
419 | else | |
420 | error (_("remote.c: error in outgoing packet at field #%ld."), | |
421 | strtol (buf, NULL, 16)); | |
422 | case '2': | |
423 | error (_("trace API error 0x%s."), ++buf); | |
424 | default: | |
425 | error (_("Target returns error code '%s'."), buf); | |
426 | } | |
427 | } | |
428 | ||
429 | /* Utility: wait for reply from stub, while accepting "O" packets. */ | |
430 | static char * | |
431 | remote_get_noisy_reply (char **buf_p, | |
432 | long *sizeof_buf) | |
433 | { | |
434 | do /* Loop on reply from remote stub. */ | |
435 | { | |
436 | char *buf; | |
a744cf53 | 437 | |
35b1e5cc SS |
438 | QUIT; /* allow user to bail out with ^C */ |
439 | getpkt (buf_p, sizeof_buf, 0); | |
440 | buf = *buf_p; | |
ad91cd99 | 441 | if (buf[0] == 'E') |
35b1e5cc | 442 | trace_error (buf); |
dde08ee1 PA |
443 | else if (strncmp (buf, "qRelocInsn:", strlen ("qRelocInsn:")) == 0) |
444 | { | |
445 | ULONGEST ul; | |
446 | CORE_ADDR from, to, org_to; | |
447 | char *p, *pp; | |
448 | int adjusted_size = 0; | |
449 | volatile struct gdb_exception ex; | |
450 | ||
451 | p = buf + strlen ("qRelocInsn:"); | |
452 | pp = unpack_varlen_hex (p, &ul); | |
453 | if (*pp != ';') | |
cb91c06a | 454 | error (_("invalid qRelocInsn packet: %s"), buf); |
dde08ee1 PA |
455 | from = ul; |
456 | ||
457 | p = pp + 1; | |
458 | pp = unpack_varlen_hex (p, &ul); | |
459 | to = ul; | |
460 | ||
461 | org_to = to; | |
462 | ||
463 | TRY_CATCH (ex, RETURN_MASK_ALL) | |
464 | { | |
465 | gdbarch_relocate_instruction (target_gdbarch, &to, from); | |
466 | } | |
467 | if (ex.reason >= 0) | |
468 | { | |
469 | adjusted_size = to - org_to; | |
470 | ||
471 | sprintf (buf, "qRelocInsn:%x", adjusted_size); | |
472 | putpkt (buf); | |
473 | } | |
474 | else if (ex.reason < 0 && ex.error == MEMORY_ERROR) | |
475 | { | |
476 | /* Propagate memory errors silently back to the target. | |
477 | The stub may have limited the range of addresses we | |
478 | can write to, for example. */ | |
479 | putpkt ("E01"); | |
480 | } | |
481 | else | |
482 | { | |
483 | /* Something unexpectedly bad happened. Be verbose so | |
484 | we can tell what, and propagate the error back to the | |
485 | stub, so it doesn't get stuck waiting for a | |
486 | response. */ | |
487 | exception_fprintf (gdb_stderr, ex, | |
488 | _("warning: relocating instruction: ")); | |
489 | putpkt ("E01"); | |
490 | } | |
491 | } | |
ad91cd99 | 492 | else if (buf[0] == 'O' && buf[1] != 'K') |
35b1e5cc SS |
493 | remote_console_output (buf + 1); /* 'O' message from stub */ |
494 | else | |
495 | return buf; /* here's the actual reply */ | |
496 | } | |
497 | while (1); | |
498 | } | |
3c3bea1c | 499 | |
d01949b6 AC |
500 | /* Handle for retreving the remote protocol data from gdbarch. */ |
501 | static struct gdbarch_data *remote_gdbarch_data_handle; | |
502 | ||
ea9c271d DJ |
503 | static struct remote_arch_state * |
504 | get_remote_arch_state (void) | |
d01949b6 | 505 | { |
1cf3db46 | 506 | return gdbarch_data (target_gdbarch, remote_gdbarch_data_handle); |
d01949b6 AC |
507 | } |
508 | ||
0b83947e DJ |
509 | /* Fetch the global remote target state. */ |
510 | ||
511 | static struct remote_state * | |
512 | get_remote_state (void) | |
513 | { | |
514 | /* Make sure that the remote architecture state has been | |
515 | initialized, because doing so might reallocate rs->buf. Any | |
516 | function which calls getpkt also needs to be mindful of changes | |
517 | to rs->buf, but this call limits the number of places which run | |
518 | into trouble. */ | |
519 | get_remote_arch_state (); | |
520 | ||
521 | return get_remote_state_raw (); | |
522 | } | |
523 | ||
74ca34ce DJ |
524 | static int |
525 | compare_pnums (const void *lhs_, const void *rhs_) | |
526 | { | |
527 | const struct packet_reg * const *lhs = lhs_; | |
528 | const struct packet_reg * const *rhs = rhs_; | |
529 | ||
530 | if ((*lhs)->pnum < (*rhs)->pnum) | |
531 | return -1; | |
532 | else if ((*lhs)->pnum == (*rhs)->pnum) | |
533 | return 0; | |
534 | else | |
535 | return 1; | |
536 | } | |
537 | ||
d01949b6 AC |
538 | static void * |
539 | init_remote_state (struct gdbarch *gdbarch) | |
540 | { | |
74ca34ce | 541 | int regnum, num_remote_regs, offset; |
0b83947e | 542 | struct remote_state *rs = get_remote_state_raw (); |
ea9c271d | 543 | struct remote_arch_state *rsa; |
74ca34ce | 544 | struct packet_reg **remote_regs; |
ea9c271d DJ |
545 | |
546 | rsa = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct remote_arch_state); | |
d01949b6 | 547 | |
123dc839 DJ |
548 | /* Use the architecture to build a regnum<->pnum table, which will be |
549 | 1:1 unless a feature set specifies otherwise. */ | |
f57d151a | 550 | rsa->regs = GDBARCH_OBSTACK_CALLOC (gdbarch, |
4a22f64d | 551 | gdbarch_num_regs (gdbarch), |
f57d151a | 552 | struct packet_reg); |
4a22f64d | 553 | for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++) |
ad10f812 | 554 | { |
ea9c271d | 555 | struct packet_reg *r = &rsa->regs[regnum]; |
baef701f | 556 | |
4a22f64d | 557 | if (register_size (gdbarch, regnum) == 0) |
baef701f DJ |
558 | /* Do not try to fetch zero-sized (placeholder) registers. */ |
559 | r->pnum = -1; | |
560 | else | |
561 | r->pnum = gdbarch_remote_register_number (gdbarch, regnum); | |
562 | ||
b323314b | 563 | r->regnum = regnum; |
74ca34ce DJ |
564 | } |
565 | ||
566 | /* Define the g/G packet format as the contents of each register | |
567 | with a remote protocol number, in order of ascending protocol | |
568 | number. */ | |
569 | ||
4a22f64d UW |
570 | remote_regs = alloca (gdbarch_num_regs (gdbarch) |
571 | * sizeof (struct packet_reg *)); | |
f57d151a | 572 | for (num_remote_regs = 0, regnum = 0; |
4a22f64d | 573 | regnum < gdbarch_num_regs (gdbarch); |
f57d151a | 574 | regnum++) |
74ca34ce DJ |
575 | if (rsa->regs[regnum].pnum != -1) |
576 | remote_regs[num_remote_regs++] = &rsa->regs[regnum]; | |
7d58c67d | 577 | |
74ca34ce DJ |
578 | qsort (remote_regs, num_remote_regs, sizeof (struct packet_reg *), |
579 | compare_pnums); | |
580 | ||
581 | for (regnum = 0, offset = 0; regnum < num_remote_regs; regnum++) | |
582 | { | |
583 | remote_regs[regnum]->in_g_packet = 1; | |
584 | remote_regs[regnum]->offset = offset; | |
4a22f64d | 585 | offset += register_size (gdbarch, remote_regs[regnum]->regnum); |
ad10f812 AC |
586 | } |
587 | ||
74ca34ce DJ |
588 | /* Record the maximum possible size of the g packet - it may turn out |
589 | to be smaller. */ | |
590 | rsa->sizeof_g_packet = offset; | |
591 | ||
d01949b6 AC |
592 | /* Default maximum number of characters in a packet body. Many |
593 | remote stubs have a hardwired buffer size of 400 bytes | |
594 | (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used | |
595 | as the maximum packet-size to ensure that the packet and an extra | |
596 | NUL character can always fit in the buffer. This stops GDB | |
597 | trashing stubs that try to squeeze an extra NUL into what is | |
ea9c271d DJ |
598 | already a full buffer (As of 1999-12-04 that was most stubs). */ |
599 | rsa->remote_packet_size = 400 - 1; | |
d01949b6 | 600 | |
ea9c271d DJ |
601 | /* This one is filled in when a ``g'' packet is received. */ |
602 | rsa->actual_register_packet_size = 0; | |
603 | ||
604 | /* Should rsa->sizeof_g_packet needs more space than the | |
ad10f812 AC |
605 | default, adjust the size accordingly. Remember that each byte is |
606 | encoded as two characters. 32 is the overhead for the packet | |
607 | header / footer. NOTE: cagney/1999-10-26: I suspect that 8 | |
d01949b6 | 608 | (``$NN:G...#NN'') is a better guess, the below has been padded a |
23860348 | 609 | little. */ |
ea9c271d DJ |
610 | if (rsa->sizeof_g_packet > ((rsa->remote_packet_size - 32) / 2)) |
611 | rsa->remote_packet_size = (rsa->sizeof_g_packet * 2 + 32); | |
802188a7 | 612 | |
ea9c271d DJ |
613 | /* Make sure that the packet buffer is plenty big enough for |
614 | this architecture. */ | |
615 | if (rs->buf_size < rsa->remote_packet_size) | |
616 | { | |
617 | rs->buf_size = 2 * rsa->remote_packet_size; | |
7fca722e | 618 | rs->buf = xrealloc (rs->buf, rs->buf_size); |
ea9c271d | 619 | } |
6d820c5c | 620 | |
ea9c271d DJ |
621 | return rsa; |
622 | } | |
623 | ||
624 | /* Return the current allowed size of a remote packet. This is | |
625 | inferred from the current architecture, and should be used to | |
626 | limit the length of outgoing packets. */ | |
627 | static long | |
628 | get_remote_packet_size (void) | |
629 | { | |
be2a5f71 | 630 | struct remote_state *rs = get_remote_state (); |
ea9c271d DJ |
631 | struct remote_arch_state *rsa = get_remote_arch_state (); |
632 | ||
be2a5f71 DJ |
633 | if (rs->explicit_packet_size) |
634 | return rs->explicit_packet_size; | |
635 | ||
ea9c271d | 636 | return rsa->remote_packet_size; |
d01949b6 AC |
637 | } |
638 | ||
ad10f812 | 639 | static struct packet_reg * |
ea9c271d | 640 | packet_reg_from_regnum (struct remote_arch_state *rsa, long regnum) |
ad10f812 | 641 | { |
1cf3db46 | 642 | if (regnum < 0 && regnum >= gdbarch_num_regs (target_gdbarch)) |
b323314b AC |
643 | return NULL; |
644 | else | |
ad10f812 | 645 | { |
ea9c271d | 646 | struct packet_reg *r = &rsa->regs[regnum]; |
a744cf53 | 647 | |
b323314b AC |
648 | gdb_assert (r->regnum == regnum); |
649 | return r; | |
ad10f812 | 650 | } |
ad10f812 AC |
651 | } |
652 | ||
653 | static struct packet_reg * | |
ea9c271d | 654 | packet_reg_from_pnum (struct remote_arch_state *rsa, LONGEST pnum) |
ad10f812 | 655 | { |
b323314b | 656 | int i; |
a744cf53 | 657 | |
1cf3db46 | 658 | for (i = 0; i < gdbarch_num_regs (target_gdbarch); i++) |
ad10f812 | 659 | { |
ea9c271d | 660 | struct packet_reg *r = &rsa->regs[i]; |
a744cf53 | 661 | |
b323314b AC |
662 | if (r->pnum == pnum) |
663 | return r; | |
ad10f812 AC |
664 | } |
665 | return NULL; | |
d01949b6 AC |
666 | } |
667 | ||
3c3bea1c GS |
668 | /* FIXME: graces/2002-08-08: These variables should eventually be |
669 | bound to an instance of the target object (as in gdbarch-tdep()), | |
670 | when such a thing exists. */ | |
671 | ||
672 | /* This is set to the data address of the access causing the target | |
673 | to stop for a watchpoint. */ | |
674 | static CORE_ADDR remote_watch_data_address; | |
675 | ||
94e08568 | 676 | /* This is non-zero if target stopped for a watchpoint. */ |
3c3bea1c GS |
677 | static int remote_stopped_by_watchpoint_p; |
678 | ||
c906108c SS |
679 | static struct target_ops remote_ops; |
680 | ||
681 | static struct target_ops extended_remote_ops; | |
682 | ||
b84876c2 PA |
683 | static int remote_async_mask_value = 1; |
684 | ||
6426a772 JM |
685 | /* FIXME: cagney/1999-09-23: Even though getpkt was called with |
686 | ``forever'' still use the normal timeout mechanism. This is | |
687 | currently used by the ASYNC code to guarentee that target reads | |
688 | during the initial connect always time-out. Once getpkt has been | |
689 | modified to return a timeout indication and, in turn | |
690 | remote_wait()/wait_for_inferior() have gained a timeout parameter | |
23860348 | 691 | this can go away. */ |
6426a772 JM |
692 | static int wait_forever_enabled_p = 1; |
693 | ||
9a7071a8 JB |
694 | /* Allow the user to specify what sequence to send to the remote |
695 | when he requests a program interruption: Although ^C is usually | |
696 | what remote systems expect (this is the default, here), it is | |
697 | sometimes preferable to send a break. On other systems such | |
698 | as the Linux kernel, a break followed by g, which is Magic SysRq g | |
699 | is required in order to interrupt the execution. */ | |
700 | const char interrupt_sequence_control_c[] = "Ctrl-C"; | |
701 | const char interrupt_sequence_break[] = "BREAK"; | |
702 | const char interrupt_sequence_break_g[] = "BREAK-g"; | |
703 | static const char *interrupt_sequence_modes[] = | |
704 | { | |
705 | interrupt_sequence_control_c, | |
706 | interrupt_sequence_break, | |
707 | interrupt_sequence_break_g, | |
708 | NULL | |
709 | }; | |
710 | static const char *interrupt_sequence_mode = interrupt_sequence_control_c; | |
711 | ||
712 | static void | |
713 | show_interrupt_sequence (struct ui_file *file, int from_tty, | |
714 | struct cmd_list_element *c, | |
715 | const char *value) | |
716 | { | |
717 | if (interrupt_sequence_mode == interrupt_sequence_control_c) | |
718 | fprintf_filtered (file, | |
719 | _("Send the ASCII ETX character (Ctrl-c) " | |
720 | "to the remote target to interrupt the " | |
721 | "execution of the program.\n")); | |
722 | else if (interrupt_sequence_mode == interrupt_sequence_break) | |
723 | fprintf_filtered (file, | |
724 | _("send a break signal to the remote target " | |
725 | "to interrupt the execution of the program.\n")); | |
726 | else if (interrupt_sequence_mode == interrupt_sequence_break_g) | |
727 | fprintf_filtered (file, | |
728 | _("Send a break signal and 'g' a.k.a. Magic SysRq g to " | |
729 | "the remote target to interrupt the execution " | |
730 | "of Linux kernel.\n")); | |
731 | else | |
732 | internal_error (__FILE__, __LINE__, | |
733 | _("Invalid value for interrupt_sequence_mode: %s."), | |
734 | interrupt_sequence_mode); | |
735 | } | |
6426a772 | 736 | |
9a7071a8 JB |
737 | /* This boolean variable specifies whether interrupt_sequence is sent |
738 | to the remote target when gdb connects to it. | |
739 | This is mostly needed when you debug the Linux kernel: The Linux kernel | |
740 | expects BREAK g which is Magic SysRq g for connecting gdb. */ | |
741 | static int interrupt_on_connect = 0; | |
c906108c | 742 | |
9a7071a8 JB |
743 | /* This variable is used to implement the "set/show remotebreak" commands. |
744 | Since these commands are now deprecated in favor of "set/show remote | |
745 | interrupt-sequence", it no longer has any effect on the code. */ | |
c906108c SS |
746 | static int remote_break; |
747 | ||
9a7071a8 JB |
748 | static void |
749 | set_remotebreak (char *args, int from_tty, struct cmd_list_element *c) | |
750 | { | |
751 | if (remote_break) | |
752 | interrupt_sequence_mode = interrupt_sequence_break; | |
753 | else | |
754 | interrupt_sequence_mode = interrupt_sequence_control_c; | |
755 | } | |
756 | ||
757 | static void | |
758 | show_remotebreak (struct ui_file *file, int from_tty, | |
759 | struct cmd_list_element *c, | |
760 | const char *value) | |
761 | { | |
762 | } | |
763 | ||
c906108c SS |
764 | /* Descriptor for I/O to remote machine. Initialize it to NULL so that |
765 | remote_open knows that we don't have a file open when the program | |
766 | starts. */ | |
819cc324 | 767 | static struct serial *remote_desc = NULL; |
c906108c | 768 | |
c906108c SS |
769 | /* This variable sets the number of bits in an address that are to be |
770 | sent in a memory ("M" or "m") packet. Normally, after stripping | |
771 | leading zeros, the entire address would be sent. This variable | |
772 | restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The | |
773 | initial implementation of remote.c restricted the address sent in | |
774 | memory packets to ``host::sizeof long'' bytes - (typically 32 | |
775 | bits). Consequently, for 64 bit targets, the upper 32 bits of an | |
776 | address was never sent. Since fixing this bug may cause a break in | |
777 | some remote targets this variable is principly provided to | |
23860348 | 778 | facilitate backward compatibility. */ |
c906108c SS |
779 | |
780 | static int remote_address_size; | |
781 | ||
75c99385 PA |
782 | /* Temporary to track who currently owns the terminal. See |
783 | remote_terminal_* for more details. */ | |
6426a772 JM |
784 | |
785 | static int remote_async_terminal_ours_p; | |
786 | ||
2d717e4f DJ |
787 | /* The executable file to use for "run" on the remote side. */ |
788 | ||
789 | static char *remote_exec_file = ""; | |
790 | ||
11cf8741 | 791 | \f |
11cf8741 | 792 | /* User configurable variables for the number of characters in a |
ea9c271d DJ |
793 | memory read/write packet. MIN (rsa->remote_packet_size, |
794 | rsa->sizeof_g_packet) is the default. Some targets need smaller | |
24b06219 | 795 | values (fifo overruns, et.al.) and some users need larger values |
ad10f812 AC |
796 | (speed up transfers). The variables ``preferred_*'' (the user |
797 | request), ``current_*'' (what was actually set) and ``forced_*'' | |
23860348 | 798 | (Positive - a soft limit, negative - a hard limit). */ |
11cf8741 JM |
799 | |
800 | struct memory_packet_config | |
801 | { | |
802 | char *name; | |
803 | long size; | |
804 | int fixed_p; | |
805 | }; | |
806 | ||
807 | /* Compute the current size of a read/write packet. Since this makes | |
808 | use of ``actual_register_packet_size'' the computation is dynamic. */ | |
809 | ||
810 | static long | |
811 | get_memory_packet_size (struct memory_packet_config *config) | |
812 | { | |
d01949b6 | 813 | struct remote_state *rs = get_remote_state (); |
ea9c271d DJ |
814 | struct remote_arch_state *rsa = get_remote_arch_state (); |
815 | ||
11cf8741 JM |
816 | /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk |
817 | law?) that some hosts don't cope very well with large alloca() | |
818 | calls. Eventually the alloca() code will be replaced by calls to | |
819 | xmalloc() and make_cleanups() allowing this restriction to either | |
23860348 | 820 | be lifted or removed. */ |
11cf8741 JM |
821 | #ifndef MAX_REMOTE_PACKET_SIZE |
822 | #define MAX_REMOTE_PACKET_SIZE 16384 | |
823 | #endif | |
3de11b2e | 824 | /* NOTE: 20 ensures we can write at least one byte. */ |
11cf8741 | 825 | #ifndef MIN_REMOTE_PACKET_SIZE |
3de11b2e | 826 | #define MIN_REMOTE_PACKET_SIZE 20 |
11cf8741 JM |
827 | #endif |
828 | long what_they_get; | |
829 | if (config->fixed_p) | |
830 | { | |
831 | if (config->size <= 0) | |
832 | what_they_get = MAX_REMOTE_PACKET_SIZE; | |
833 | else | |
834 | what_they_get = config->size; | |
835 | } | |
836 | else | |
837 | { | |
ea9c271d | 838 | what_they_get = get_remote_packet_size (); |
23860348 | 839 | /* Limit the packet to the size specified by the user. */ |
11cf8741 JM |
840 | if (config->size > 0 |
841 | && what_they_get > config->size) | |
842 | what_they_get = config->size; | |
be2a5f71 DJ |
843 | |
844 | /* Limit it to the size of the targets ``g'' response unless we have | |
845 | permission from the stub to use a larger packet size. */ | |
846 | if (rs->explicit_packet_size == 0 | |
847 | && rsa->actual_register_packet_size > 0 | |
848 | && what_they_get > rsa->actual_register_packet_size) | |
849 | what_they_get = rsa->actual_register_packet_size; | |
11cf8741 JM |
850 | } |
851 | if (what_they_get > MAX_REMOTE_PACKET_SIZE) | |
852 | what_they_get = MAX_REMOTE_PACKET_SIZE; | |
853 | if (what_they_get < MIN_REMOTE_PACKET_SIZE) | |
854 | what_they_get = MIN_REMOTE_PACKET_SIZE; | |
6d820c5c DJ |
855 | |
856 | /* Make sure there is room in the global buffer for this packet | |
857 | (including its trailing NUL byte). */ | |
858 | if (rs->buf_size < what_they_get + 1) | |
859 | { | |
860 | rs->buf_size = 2 * what_they_get; | |
861 | rs->buf = xrealloc (rs->buf, 2 * what_they_get); | |
862 | } | |
863 | ||
11cf8741 JM |
864 | return what_they_get; |
865 | } | |
866 | ||
867 | /* Update the size of a read/write packet. If they user wants | |
23860348 | 868 | something really big then do a sanity check. */ |
11cf8741 JM |
869 | |
870 | static void | |
871 | set_memory_packet_size (char *args, struct memory_packet_config *config) | |
872 | { | |
873 | int fixed_p = config->fixed_p; | |
874 | long size = config->size; | |
a744cf53 | 875 | |
11cf8741 | 876 | if (args == NULL) |
8a3fe4f8 | 877 | error (_("Argument required (integer, `fixed' or `limited').")); |
11cf8741 JM |
878 | else if (strcmp (args, "hard") == 0 |
879 | || strcmp (args, "fixed") == 0) | |
880 | fixed_p = 1; | |
881 | else if (strcmp (args, "soft") == 0 | |
882 | || strcmp (args, "limit") == 0) | |
883 | fixed_p = 0; | |
884 | else | |
885 | { | |
886 | char *end; | |
a744cf53 | 887 | |
11cf8741 JM |
888 | size = strtoul (args, &end, 0); |
889 | if (args == end) | |
8a3fe4f8 | 890 | error (_("Invalid %s (bad syntax)."), config->name); |
11cf8741 JM |
891 | #if 0 |
892 | /* Instead of explicitly capping the size of a packet to | |
893 | MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is | |
894 | instead allowed to set the size to something arbitrarily | |
23860348 | 895 | large. */ |
11cf8741 | 896 | if (size > MAX_REMOTE_PACKET_SIZE) |
8a3fe4f8 | 897 | error (_("Invalid %s (too large)."), config->name); |
11cf8741 JM |
898 | #endif |
899 | } | |
23860348 | 900 | /* Extra checks? */ |
11cf8741 JM |
901 | if (fixed_p && !config->fixed_p) |
902 | { | |
e2e0b3e5 AC |
903 | if (! query (_("The target may not be able to correctly handle a %s\n" |
904 | "of %ld bytes. Change the packet size? "), | |
11cf8741 | 905 | config->name, size)) |
8a3fe4f8 | 906 | error (_("Packet size not changed.")); |
11cf8741 | 907 | } |
23860348 | 908 | /* Update the config. */ |
11cf8741 JM |
909 | config->fixed_p = fixed_p; |
910 | config->size = size; | |
911 | } | |
912 | ||
913 | static void | |
914 | show_memory_packet_size (struct memory_packet_config *config) | |
915 | { | |
a3f17187 | 916 | printf_filtered (_("The %s is %ld. "), config->name, config->size); |
11cf8741 | 917 | if (config->fixed_p) |
a3f17187 | 918 | printf_filtered (_("Packets are fixed at %ld bytes.\n"), |
11cf8741 JM |
919 | get_memory_packet_size (config)); |
920 | else | |
a3f17187 | 921 | printf_filtered (_("Packets are limited to %ld bytes.\n"), |
11cf8741 JM |
922 | get_memory_packet_size (config)); |
923 | } | |
924 | ||
925 | static struct memory_packet_config memory_write_packet_config = | |
926 | { | |
927 | "memory-write-packet-size", | |
928 | }; | |
929 | ||
930 | static void | |
931 | set_memory_write_packet_size (char *args, int from_tty) | |
932 | { | |
933 | set_memory_packet_size (args, &memory_write_packet_config); | |
934 | } | |
935 | ||
936 | static void | |
937 | show_memory_write_packet_size (char *args, int from_tty) | |
938 | { | |
939 | show_memory_packet_size (&memory_write_packet_config); | |
940 | } | |
941 | ||
942 | static long | |
943 | get_memory_write_packet_size (void) | |
944 | { | |
945 | return get_memory_packet_size (&memory_write_packet_config); | |
946 | } | |
947 | ||
948 | static struct memory_packet_config memory_read_packet_config = | |
949 | { | |
950 | "memory-read-packet-size", | |
951 | }; | |
952 | ||
953 | static void | |
954 | set_memory_read_packet_size (char *args, int from_tty) | |
955 | { | |
956 | set_memory_packet_size (args, &memory_read_packet_config); | |
957 | } | |
958 | ||
959 | static void | |
960 | show_memory_read_packet_size (char *args, int from_tty) | |
961 | { | |
962 | show_memory_packet_size (&memory_read_packet_config); | |
963 | } | |
964 | ||
965 | static long | |
966 | get_memory_read_packet_size (void) | |
967 | { | |
968 | long size = get_memory_packet_size (&memory_read_packet_config); | |
a744cf53 | 969 | |
11cf8741 JM |
970 | /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an |
971 | extra buffer size argument before the memory read size can be | |
ea9c271d DJ |
972 | increased beyond this. */ |
973 | if (size > get_remote_packet_size ()) | |
974 | size = get_remote_packet_size (); | |
11cf8741 JM |
975 | return size; |
976 | } | |
977 | ||
11cf8741 | 978 | \f |
5a2468f5 JM |
979 | /* Generic configuration support for packets the stub optionally |
980 | supports. Allows the user to specify the use of the packet as well | |
23860348 | 981 | as allowing GDB to auto-detect support in the remote stub. */ |
5a2468f5 JM |
982 | |
983 | enum packet_support | |
984 | { | |
985 | PACKET_SUPPORT_UNKNOWN = 0, | |
986 | PACKET_ENABLE, | |
987 | PACKET_DISABLE | |
988 | }; | |
989 | ||
5a2468f5 JM |
990 | struct packet_config |
991 | { | |
bb572ddd DJ |
992 | const char *name; |
993 | const char *title; | |
7f19b9a2 | 994 | enum auto_boolean detect; |
5a2468f5 JM |
995 | enum packet_support support; |
996 | }; | |
997 | ||
d471ea57 | 998 | /* Analyze a packet's return value and update the packet config |
23860348 | 999 | accordingly. */ |
d471ea57 AC |
1000 | |
1001 | enum packet_result | |
1002 | { | |
1003 | PACKET_ERROR, | |
1004 | PACKET_OK, | |
1005 | PACKET_UNKNOWN | |
1006 | }; | |
1007 | ||
5a2468f5 | 1008 | static void |
d471ea57 | 1009 | update_packet_config (struct packet_config *config) |
5a2468f5 | 1010 | { |
d471ea57 AC |
1011 | switch (config->detect) |
1012 | { | |
7f19b9a2 | 1013 | case AUTO_BOOLEAN_TRUE: |
d471ea57 AC |
1014 | config->support = PACKET_ENABLE; |
1015 | break; | |
7f19b9a2 | 1016 | case AUTO_BOOLEAN_FALSE: |
d471ea57 AC |
1017 | config->support = PACKET_DISABLE; |
1018 | break; | |
7f19b9a2 | 1019 | case AUTO_BOOLEAN_AUTO: |
d471ea57 AC |
1020 | config->support = PACKET_SUPPORT_UNKNOWN; |
1021 | break; | |
1022 | } | |
5a2468f5 JM |
1023 | } |
1024 | ||
1025 | static void | |
fba45db2 | 1026 | show_packet_config_cmd (struct packet_config *config) |
5a2468f5 JM |
1027 | { |
1028 | char *support = "internal-error"; | |
a744cf53 | 1029 | |
5a2468f5 JM |
1030 | switch (config->support) |
1031 | { | |
1032 | case PACKET_ENABLE: | |
1033 | support = "enabled"; | |
1034 | break; | |
1035 | case PACKET_DISABLE: | |
1036 | support = "disabled"; | |
1037 | break; | |
1038 | case PACKET_SUPPORT_UNKNOWN: | |
1039 | support = "unknown"; | |
1040 | break; | |
1041 | } | |
1042 | switch (config->detect) | |
1043 | { | |
7f19b9a2 | 1044 | case AUTO_BOOLEAN_AUTO: |
37a105a1 DJ |
1045 | printf_filtered (_("Support for the `%s' packet is auto-detected, currently %s.\n"), |
1046 | config->name, support); | |
5a2468f5 | 1047 | break; |
7f19b9a2 AC |
1048 | case AUTO_BOOLEAN_TRUE: |
1049 | case AUTO_BOOLEAN_FALSE: | |
37a105a1 DJ |
1050 | printf_filtered (_("Support for the `%s' packet is currently %s.\n"), |
1051 | config->name, support); | |
8e248173 | 1052 | break; |
5a2468f5 JM |
1053 | } |
1054 | } | |
1055 | ||
1056 | static void | |
bb572ddd DJ |
1057 | add_packet_config_cmd (struct packet_config *config, const char *name, |
1058 | const char *title, int legacy) | |
d471ea57 | 1059 | { |
5a2468f5 JM |
1060 | char *set_doc; |
1061 | char *show_doc; | |
d471ea57 | 1062 | char *cmd_name; |
3ed07be4 | 1063 | |
5a2468f5 JM |
1064 | config->name = name; |
1065 | config->title = title; | |
7f19b9a2 | 1066 | config->detect = AUTO_BOOLEAN_AUTO; |
8e248173 | 1067 | config->support = PACKET_SUPPORT_UNKNOWN; |
b435e160 AC |
1068 | set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet", |
1069 | name, title); | |
1070 | show_doc = xstrprintf ("Show current use of remote protocol `%s' (%s) packet", | |
1071 | name, title); | |
d471ea57 | 1072 | /* set/show TITLE-packet {auto,on,off} */ |
b435e160 | 1073 | cmd_name = xstrprintf ("%s-packet", title); |
e9e68a56 | 1074 | add_setshow_auto_boolean_cmd (cmd_name, class_obscure, |
2c5b56ce | 1075 | &config->detect, set_doc, show_doc, NULL, /* help_doc */ |
bb572ddd DJ |
1076 | set_remote_protocol_packet_cmd, |
1077 | show_remote_protocol_packet_cmd, | |
1078 | &remote_set_cmdlist, &remote_show_cmdlist); | |
1eefb858 TT |
1079 | /* The command code copies the documentation strings. */ |
1080 | xfree (set_doc); | |
1081 | xfree (show_doc); | |
23860348 | 1082 | /* set/show remote NAME-packet {auto,on,off} -- legacy. */ |
d471ea57 AC |
1083 | if (legacy) |
1084 | { | |
1085 | char *legacy_name; | |
a744cf53 | 1086 | |
b435e160 | 1087 | legacy_name = xstrprintf ("%s-packet", name); |
d471ea57 | 1088 | add_alias_cmd (legacy_name, cmd_name, class_obscure, 0, |
bb572ddd | 1089 | &remote_set_cmdlist); |
d471ea57 | 1090 | add_alias_cmd (legacy_name, cmd_name, class_obscure, 0, |
bb572ddd | 1091 | &remote_show_cmdlist); |
d471ea57 | 1092 | } |
5a2468f5 JM |
1093 | } |
1094 | ||
d471ea57 | 1095 | static enum packet_result |
a76d924d | 1096 | packet_check_result (const char *buf) |
5a2468f5 | 1097 | { |
d471ea57 | 1098 | if (buf[0] != '\0') |
5a2468f5 | 1099 | { |
d471ea57 | 1100 | /* The stub recognized the packet request. Check that the |
23860348 | 1101 | operation succeeded. */ |
a76d924d DJ |
1102 | if (buf[0] == 'E' |
1103 | && isxdigit (buf[1]) && isxdigit (buf[2]) | |
1104 | && buf[3] == '\0') | |
1105 | /* "Enn" - definitly an error. */ | |
1106 | return PACKET_ERROR; | |
1107 | ||
1108 | /* Always treat "E." as an error. This will be used for | |
1109 | more verbose error messages, such as E.memtypes. */ | |
1110 | if (buf[0] == 'E' && buf[1] == '.') | |
1111 | return PACKET_ERROR; | |
1112 | ||
1113 | /* The packet may or may not be OK. Just assume it is. */ | |
1114 | return PACKET_OK; | |
1115 | } | |
1116 | else | |
1117 | /* The stub does not support the packet. */ | |
1118 | return PACKET_UNKNOWN; | |
1119 | } | |
1120 | ||
1121 | static enum packet_result | |
1122 | packet_ok (const char *buf, struct packet_config *config) | |
1123 | { | |
1124 | enum packet_result result; | |
1125 | ||
1126 | result = packet_check_result (buf); | |
1127 | switch (result) | |
1128 | { | |
1129 | case PACKET_OK: | |
1130 | case PACKET_ERROR: | |
1131 | /* The stub recognized the packet request. */ | |
d471ea57 AC |
1132 | switch (config->support) |
1133 | { | |
1134 | case PACKET_SUPPORT_UNKNOWN: | |
1135 | if (remote_debug) | |
1136 | fprintf_unfiltered (gdb_stdlog, | |
1137 | "Packet %s (%s) is supported\n", | |
1138 | config->name, config->title); | |
1139 | config->support = PACKET_ENABLE; | |
1140 | break; | |
1141 | case PACKET_DISABLE: | |
8e65ff28 | 1142 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 1143 | _("packet_ok: attempt to use a disabled packet")); |
d471ea57 AC |
1144 | break; |
1145 | case PACKET_ENABLE: | |
1146 | break; | |
1147 | } | |
a76d924d DJ |
1148 | break; |
1149 | case PACKET_UNKNOWN: | |
23860348 | 1150 | /* The stub does not support the packet. */ |
d471ea57 AC |
1151 | switch (config->support) |
1152 | { | |
1153 | case PACKET_ENABLE: | |
7f19b9a2 | 1154 | if (config->detect == AUTO_BOOLEAN_AUTO) |
d471ea57 | 1155 | /* If the stub previously indicated that the packet was |
23860348 | 1156 | supported then there is a protocol error.. */ |
8a3fe4f8 | 1157 | error (_("Protocol error: %s (%s) conflicting enabled responses."), |
d471ea57 AC |
1158 | config->name, config->title); |
1159 | else | |
23860348 | 1160 | /* The user set it wrong. */ |
8a3fe4f8 | 1161 | error (_("Enabled packet %s (%s) not recognized by stub"), |
d471ea57 AC |
1162 | config->name, config->title); |
1163 | break; | |
1164 | case PACKET_SUPPORT_UNKNOWN: | |
1165 | if (remote_debug) | |
1166 | fprintf_unfiltered (gdb_stdlog, | |
1167 | "Packet %s (%s) is NOT supported\n", | |
1168 | config->name, config->title); | |
1169 | config->support = PACKET_DISABLE; | |
1170 | break; | |
1171 | case PACKET_DISABLE: | |
1172 | break; | |
1173 | } | |
a76d924d | 1174 | break; |
5a2468f5 | 1175 | } |
a76d924d DJ |
1176 | |
1177 | return result; | |
5a2468f5 JM |
1178 | } |
1179 | ||
444abaca DJ |
1180 | enum { |
1181 | PACKET_vCont = 0, | |
1182 | PACKET_X, | |
1183 | PACKET_qSymbol, | |
1184 | PACKET_P, | |
1185 | PACKET_p, | |
1186 | PACKET_Z0, | |
1187 | PACKET_Z1, | |
1188 | PACKET_Z2, | |
1189 | PACKET_Z3, | |
1190 | PACKET_Z4, | |
a6b151f1 DJ |
1191 | PACKET_vFile_open, |
1192 | PACKET_vFile_pread, | |
1193 | PACKET_vFile_pwrite, | |
1194 | PACKET_vFile_close, | |
1195 | PACKET_vFile_unlink, | |
0876f84a | 1196 | PACKET_qXfer_auxv, |
23181151 | 1197 | PACKET_qXfer_features, |
cfa9d6d9 | 1198 | PACKET_qXfer_libraries, |
fd79ecee | 1199 | PACKET_qXfer_memory_map, |
0e7f50da UW |
1200 | PACKET_qXfer_spu_read, |
1201 | PACKET_qXfer_spu_write, | |
07e059b5 | 1202 | PACKET_qXfer_osdata, |
dc146f7c | 1203 | PACKET_qXfer_threads, |
0fb4aa4b | 1204 | PACKET_qXfer_statictrace_read, |
711e434b | 1205 | PACKET_qGetTIBAddr, |
444abaca | 1206 | PACKET_qGetTLSAddr, |
be2a5f71 | 1207 | PACKET_qSupported, |
89be2091 | 1208 | PACKET_QPassSignals, |
08388c79 | 1209 | PACKET_qSearch_memory, |
2d717e4f DJ |
1210 | PACKET_vAttach, |
1211 | PACKET_vRun, | |
a6f3e723 | 1212 | PACKET_QStartNoAckMode, |
82f73884 | 1213 | PACKET_vKill, |
4aa995e1 PA |
1214 | PACKET_qXfer_siginfo_read, |
1215 | PACKET_qXfer_siginfo_write, | |
0b16c5cf | 1216 | PACKET_qAttached, |
782b2b07 | 1217 | PACKET_ConditionalTracepoints, |
7a697b8d | 1218 | PACKET_FastTracepoints, |
0fb4aa4b | 1219 | PACKET_StaticTracepoints, |
40ab02ce MS |
1220 | PACKET_bc, |
1221 | PACKET_bs, | |
409873ef | 1222 | PACKET_TracepointSource, |
d914c394 | 1223 | PACKET_QAllow, |
444abaca DJ |
1224 | PACKET_MAX |
1225 | }; | |
506fb367 | 1226 | |
444abaca | 1227 | static struct packet_config remote_protocol_packets[PACKET_MAX]; |
dc8acb97 MS |
1228 | |
1229 | static void | |
444abaca DJ |
1230 | set_remote_protocol_packet_cmd (char *args, int from_tty, |
1231 | struct cmd_list_element *c) | |
dc8acb97 | 1232 | { |
444abaca | 1233 | struct packet_config *packet; |
dc8acb97 | 1234 | |
444abaca DJ |
1235 | for (packet = remote_protocol_packets; |
1236 | packet < &remote_protocol_packets[PACKET_MAX]; | |
1237 | packet++) | |
1238 | { | |
1239 | if (&packet->detect == c->var) | |
1240 | { | |
1241 | update_packet_config (packet); | |
1242 | return; | |
1243 | } | |
1244 | } | |
1245 | internal_error (__FILE__, __LINE__, "Could not find config for %s", | |
1246 | c->name); | |
dc8acb97 MS |
1247 | } |
1248 | ||
5a2468f5 | 1249 | static void |
444abaca DJ |
1250 | show_remote_protocol_packet_cmd (struct ui_file *file, int from_tty, |
1251 | struct cmd_list_element *c, | |
1252 | const char *value) | |
5a2468f5 | 1253 | { |
444abaca | 1254 | struct packet_config *packet; |
5a2468f5 | 1255 | |
444abaca DJ |
1256 | for (packet = remote_protocol_packets; |
1257 | packet < &remote_protocol_packets[PACKET_MAX]; | |
1258 | packet++) | |
1259 | { | |
1260 | if (&packet->detect == c->var) | |
1261 | { | |
1262 | show_packet_config_cmd (packet); | |
1263 | return; | |
1264 | } | |
1265 | } | |
1266 | internal_error (__FILE__, __LINE__, "Could not find config for %s", | |
1267 | c->name); | |
5a2468f5 JM |
1268 | } |
1269 | ||
d471ea57 AC |
1270 | /* Should we try one of the 'Z' requests? */ |
1271 | ||
1272 | enum Z_packet_type | |
1273 | { | |
1274 | Z_PACKET_SOFTWARE_BP, | |
1275 | Z_PACKET_HARDWARE_BP, | |
1276 | Z_PACKET_WRITE_WP, | |
1277 | Z_PACKET_READ_WP, | |
1278 | Z_PACKET_ACCESS_WP, | |
1279 | NR_Z_PACKET_TYPES | |
1280 | }; | |
96baa820 | 1281 | |
d471ea57 | 1282 | /* For compatibility with older distributions. Provide a ``set remote |
23860348 | 1283 | Z-packet ...'' command that updates all the Z packet types. */ |
d471ea57 | 1284 | |
7f19b9a2 | 1285 | static enum auto_boolean remote_Z_packet_detect; |
96baa820 JM |
1286 | |
1287 | static void | |
fba45db2 KB |
1288 | set_remote_protocol_Z_packet_cmd (char *args, int from_tty, |
1289 | struct cmd_list_element *c) | |
96baa820 | 1290 | { |
d471ea57 | 1291 | int i; |
a744cf53 | 1292 | |
d471ea57 AC |
1293 | for (i = 0; i < NR_Z_PACKET_TYPES; i++) |
1294 | { | |
444abaca DJ |
1295 | remote_protocol_packets[PACKET_Z0 + i].detect = remote_Z_packet_detect; |
1296 | update_packet_config (&remote_protocol_packets[PACKET_Z0 + i]); | |
d471ea57 | 1297 | } |
96baa820 JM |
1298 | } |
1299 | ||
1300 | static void | |
08546159 AC |
1301 | show_remote_protocol_Z_packet_cmd (struct ui_file *file, int from_tty, |
1302 | struct cmd_list_element *c, | |
1303 | const char *value) | |
96baa820 | 1304 | { |
d471ea57 | 1305 | int i; |
a744cf53 | 1306 | |
d471ea57 AC |
1307 | for (i = 0; i < NR_Z_PACKET_TYPES; i++) |
1308 | { | |
444abaca | 1309 | show_packet_config_cmd (&remote_protocol_packets[PACKET_Z0 + i]); |
d471ea57 | 1310 | } |
96baa820 JM |
1311 | } |
1312 | ||
9d1f7ab2 MS |
1313 | /* Should we try the 'ThreadInfo' query packet? |
1314 | ||
1315 | This variable (NOT available to the user: auto-detect only!) | |
1316 | determines whether GDB will use the new, simpler "ThreadInfo" | |
1317 | query or the older, more complex syntax for thread queries. | |
802188a7 | 1318 | This is an auto-detect variable (set to true at each connect, |
9d1f7ab2 MS |
1319 | and set to false when the target fails to recognize it). */ |
1320 | ||
1321 | static int use_threadinfo_query; | |
1322 | static int use_threadextra_query; | |
1323 | ||
23860348 | 1324 | /* Tokens for use by the asynchronous signal handlers for SIGINT. */ |
d5d6fca5 DJ |
1325 | static struct async_signal_handler *sigint_remote_twice_token; |
1326 | static struct async_signal_handler *sigint_remote_token; | |
43ff13b4 | 1327 | |
74531fed PA |
1328 | \f |
1329 | /* Asynchronous signal handle registered as event loop source for | |
1330 | when we have pending events ready to be passed to the core. */ | |
1331 | ||
1332 | static struct async_event_handler *remote_async_inferior_event_token; | |
1333 | ||
1334 | /* Asynchronous signal handle registered as event loop source for when | |
1335 | the remote sent us a %Stop notification. The registered callback | |
1336 | will do a vStopped sequence to pull the rest of the events out of | |
1337 | the remote side into our event queue. */ | |
1338 | ||
1339 | static struct async_event_handler *remote_async_get_pending_events_token; | |
c906108c SS |
1340 | \f |
1341 | ||
79d7f229 PA |
1342 | static ptid_t magic_null_ptid; |
1343 | static ptid_t not_sent_ptid; | |
1344 | static ptid_t any_thread_ptid; | |
1345 | ||
1346 | /* These are the threads which we last sent to the remote system. The | |
1347 | TID member will be -1 for all or -2 for not sent yet. */ | |
1348 | ||
1349 | static ptid_t general_thread; | |
1350 | static ptid_t continue_thread; | |
c5aa993b | 1351 | |
0b16c5cf PA |
1352 | /* Find out if the stub attached to PID (and hence GDB should offer to |
1353 | detach instead of killing it when bailing out). */ | |
1354 | ||
1355 | static int | |
1356 | remote_query_attached (int pid) | |
1357 | { | |
1358 | struct remote_state *rs = get_remote_state (); | |
1359 | ||
1360 | if (remote_protocol_packets[PACKET_qAttached].support == PACKET_DISABLE) | |
1361 | return 0; | |
1362 | ||
1363 | if (remote_multi_process_p (rs)) | |
1364 | sprintf (rs->buf, "qAttached:%x", pid); | |
1365 | else | |
1366 | sprintf (rs->buf, "qAttached"); | |
1367 | ||
1368 | putpkt (rs->buf); | |
1369 | getpkt (&rs->buf, &rs->buf_size, 0); | |
1370 | ||
1371 | switch (packet_ok (rs->buf, | |
1554e9be | 1372 | &remote_protocol_packets[PACKET_qAttached])) |
0b16c5cf PA |
1373 | { |
1374 | case PACKET_OK: | |
1375 | if (strcmp (rs->buf, "1") == 0) | |
1376 | return 1; | |
1377 | break; | |
1378 | case PACKET_ERROR: | |
1379 | warning (_("Remote failure reply: %s"), rs->buf); | |
1380 | break; | |
1381 | case PACKET_UNKNOWN: | |
1382 | break; | |
1383 | } | |
1384 | ||
1385 | return 0; | |
1386 | } | |
1387 | ||
1941c569 PA |
1388 | /* Add PID to GDB's inferior table. Since we can be connected to a |
1389 | remote system before before knowing about any inferior, mark the | |
0b16c5cf PA |
1390 | target with execution when we find the first inferior. If ATTACHED |
1391 | is 1, then we had just attached to this inferior. If it is 0, then | |
1392 | we just created this inferior. If it is -1, then try querying the | |
1393 | remote stub to find out if it had attached to the inferior or | |
1394 | not. */ | |
1941c569 PA |
1395 | |
1396 | static struct inferior * | |
0b16c5cf | 1397 | remote_add_inferior (int pid, int attached) |
1941c569 | 1398 | { |
1941c569 PA |
1399 | struct inferior *inf; |
1400 | ||
0b16c5cf PA |
1401 | /* Check whether this process we're learning about is to be |
1402 | considered attached, or if is to be considered to have been | |
1403 | spawned by the stub. */ | |
1404 | if (attached == -1) | |
1405 | attached = remote_query_attached (pid); | |
1406 | ||
6c95b8df PA |
1407 | if (gdbarch_has_global_solist (target_gdbarch)) |
1408 | { | |
1409 | /* If the target shares code across all inferiors, then every | |
1410 | attach adds a new inferior. */ | |
1411 | inf = add_inferior (pid); | |
1412 | ||
1413 | /* ... and every inferior is bound to the same program space. | |
1414 | However, each inferior may still have its own address | |
1415 | space. */ | |
1416 | inf->aspace = maybe_new_address_space (); | |
1417 | inf->pspace = current_program_space; | |
1418 | } | |
1419 | else | |
1420 | { | |
1421 | /* In the traditional debugging scenario, there's a 1-1 match | |
1422 | between program/address spaces. We simply bind the inferior | |
1423 | to the program space's address space. */ | |
1424 | inf = current_inferior (); | |
1425 | inferior_appeared (inf, pid); | |
1426 | } | |
1941c569 | 1427 | |
0b16c5cf PA |
1428 | inf->attach_flag = attached; |
1429 | ||
1941c569 PA |
1430 | return inf; |
1431 | } | |
1432 | ||
1433 | /* Add thread PTID to GDB's thread list. Tag it as executing/running | |
1434 | according to RUNNING. */ | |
1435 | ||
c906108c | 1436 | static void |
1941c569 | 1437 | remote_add_thread (ptid_t ptid, int running) |
c906108c | 1438 | { |
1941c569 PA |
1439 | add_thread (ptid); |
1440 | ||
1441 | set_executing (ptid, running); | |
1442 | set_running (ptid, running); | |
1443 | } | |
1444 | ||
1445 | /* Come here when we learn about a thread id from the remote target. | |
1446 | It may be the first time we hear about such thread, so take the | |
1447 | opportunity to add it to GDB's thread list. In case this is the | |
1448 | first time we're noticing its corresponding inferior, add it to | |
1449 | GDB's inferior list as well. */ | |
1450 | ||
1451 | static void | |
1452 | remote_notice_new_inferior (ptid_t currthread, int running) | |
1453 | { | |
c906108c SS |
1454 | /* If this is a new thread, add it to GDB's thread list. |
1455 | If we leave it up to WFI to do this, bad things will happen. */ | |
82f73884 PA |
1456 | |
1457 | if (in_thread_list (currthread) && is_exited (currthread)) | |
1458 | { | |
1459 | /* We're seeing an event on a thread id we knew had exited. | |
1460 | This has to be a new thread reusing the old id. Add it. */ | |
1941c569 | 1461 | remote_add_thread (currthread, running); |
82f73884 PA |
1462 | return; |
1463 | } | |
1464 | ||
79d7f229 | 1465 | if (!in_thread_list (currthread)) |
c0a2216e | 1466 | { |
1941c569 | 1467 | struct inferior *inf = NULL; |
bad34192 | 1468 | int pid = ptid_get_pid (currthread); |
1941c569 | 1469 | |
bad34192 PA |
1470 | if (ptid_is_pid (inferior_ptid) |
1471 | && pid == ptid_get_pid (inferior_ptid)) | |
c0a2216e PA |
1472 | { |
1473 | /* inferior_ptid has no thread member yet. This can happen | |
1474 | with the vAttach -> remote_wait,"TAAthread:" path if the | |
1475 | stub doesn't support qC. This is the first stop reported | |
1476 | after an attach, so this is the main thread. Update the | |
1477 | ptid in the thread list. */ | |
bad34192 PA |
1478 | if (in_thread_list (pid_to_ptid (pid))) |
1479 | thread_change_ptid (inferior_ptid, currthread); | |
1480 | else | |
1481 | { | |
1482 | remote_add_thread (currthread, running); | |
1483 | inferior_ptid = currthread; | |
1484 | } | |
dc146f7c | 1485 | return; |
c0a2216e | 1486 | } |
82f73884 PA |
1487 | |
1488 | if (ptid_equal (magic_null_ptid, inferior_ptid)) | |
c0a2216e PA |
1489 | { |
1490 | /* inferior_ptid is not set yet. This can happen with the | |
1491 | vRun -> remote_wait,"TAAthread:" path if the stub | |
1492 | doesn't support qC. This is the first stop reported | |
1493 | after an attach, so this is the main thread. Update the | |
1494 | ptid in the thread list. */ | |
dc146f7c | 1495 | thread_change_ptid (inferior_ptid, currthread); |
82f73884 | 1496 | return; |
c0a2216e | 1497 | } |
82f73884 | 1498 | |
29c87f7f PA |
1499 | /* When connecting to a target remote, or to a target |
1500 | extended-remote which already was debugging an inferior, we | |
1501 | may not know about it yet. Add it before adding its child | |
1502 | thread, so notifications are emitted in a sensible order. */ | |
1503 | if (!in_inferior_list (ptid_get_pid (currthread))) | |
0b16c5cf | 1504 | inf = remote_add_inferior (ptid_get_pid (currthread), -1); |
29c87f7f | 1505 | |
82f73884 | 1506 | /* This is really a new thread. Add it. */ |
1941c569 PA |
1507 | remote_add_thread (currthread, running); |
1508 | ||
1509 | /* If we found a new inferior, let the common code do whatever | |
1510 | it needs to with it (e.g., read shared libraries, insert | |
1511 | breakpoints). */ | |
1512 | if (inf != NULL) | |
1513 | notice_new_inferior (currthread, running, 0); | |
c0a2216e | 1514 | } |
c906108c SS |
1515 | } |
1516 | ||
dc146f7c VP |
1517 | /* Return the private thread data, creating it if necessary. */ |
1518 | ||
1519 | struct private_thread_info * | |
1520 | demand_private_info (ptid_t ptid) | |
1521 | { | |
1522 | struct thread_info *info = find_thread_ptid (ptid); | |
1523 | ||
1524 | gdb_assert (info); | |
1525 | ||
1526 | if (!info->private) | |
1527 | { | |
1528 | info->private = xmalloc (sizeof (*(info->private))); | |
1529 | info->private_dtor = free_private_thread_info; | |
1530 | info->private->core = -1; | |
1531 | info->private->extra = 0; | |
1532 | } | |
1533 | ||
1534 | return info->private; | |
1535 | } | |
1536 | ||
74531fed PA |
1537 | /* Call this function as a result of |
1538 | 1) A halt indication (T packet) containing a thread id | |
1539 | 2) A direct query of currthread | |
1540 | 3) Successful execution of set thread | |
1541 | */ | |
1542 | ||
1543 | static void | |
1544 | record_currthread (ptid_t currthread) | |
1545 | { | |
1546 | general_thread = currthread; | |
74531fed PA |
1547 | } |
1548 | ||
89be2091 DJ |
1549 | static char *last_pass_packet; |
1550 | ||
1551 | /* If 'QPassSignals' is supported, tell the remote stub what signals | |
1552 | it can simply pass through to the inferior without reporting. */ | |
1553 | ||
1554 | static void | |
1555 | remote_pass_signals (void) | |
1556 | { | |
1557 | if (remote_protocol_packets[PACKET_QPassSignals].support != PACKET_DISABLE) | |
1558 | { | |
1559 | char *pass_packet, *p; | |
1560 | int numsigs = (int) TARGET_SIGNAL_LAST; | |
1561 | int count = 0, i; | |
1562 | ||
1563 | gdb_assert (numsigs < 256); | |
1564 | for (i = 0; i < numsigs; i++) | |
1565 | { | |
1566 | if (signal_stop_state (i) == 0 | |
1567 | && signal_print_state (i) == 0 | |
1568 | && signal_pass_state (i) == 1) | |
1569 | count++; | |
1570 | } | |
1571 | pass_packet = xmalloc (count * 3 + strlen ("QPassSignals:") + 1); | |
1572 | strcpy (pass_packet, "QPassSignals:"); | |
1573 | p = pass_packet + strlen (pass_packet); | |
1574 | for (i = 0; i < numsigs; i++) | |
1575 | { | |
1576 | if (signal_stop_state (i) == 0 | |
1577 | && signal_print_state (i) == 0 | |
1578 | && signal_pass_state (i) == 1) | |
1579 | { | |
1580 | if (i >= 16) | |
1581 | *p++ = tohex (i >> 4); | |
1582 | *p++ = tohex (i & 15); | |
1583 | if (count) | |
1584 | *p++ = ';'; | |
1585 | else | |
1586 | break; | |
1587 | count--; | |
1588 | } | |
1589 | } | |
1590 | *p = 0; | |
1591 | if (!last_pass_packet || strcmp (last_pass_packet, pass_packet)) | |
1592 | { | |
1593 | struct remote_state *rs = get_remote_state (); | |
1594 | char *buf = rs->buf; | |
1595 | ||
1596 | putpkt (pass_packet); | |
1597 | getpkt (&rs->buf, &rs->buf_size, 0); | |
1598 | packet_ok (buf, &remote_protocol_packets[PACKET_QPassSignals]); | |
1599 | if (last_pass_packet) | |
1600 | xfree (last_pass_packet); | |
1601 | last_pass_packet = pass_packet; | |
1602 | } | |
1603 | else | |
1604 | xfree (pass_packet); | |
1605 | } | |
1606 | } | |
1607 | ||
f0223081 PA |
1608 | static void |
1609 | remote_notice_signals (ptid_t ptid) | |
1610 | { | |
1611 | /* Update the remote on signals to silently pass, if they've | |
1612 | changed. */ | |
1613 | remote_pass_signals (); | |
1614 | } | |
1615 | ||
79d7f229 PA |
1616 | /* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is |
1617 | MINUS_ONE_PTID, set the thread to -1, so the stub returns the | |
1618 | thread. If GEN is set, set the general thread, if not, then set | |
1619 | the step/continue thread. */ | |
c906108c | 1620 | static void |
79d7f229 | 1621 | set_thread (struct ptid ptid, int gen) |
c906108c | 1622 | { |
d01949b6 | 1623 | struct remote_state *rs = get_remote_state (); |
79d7f229 | 1624 | ptid_t state = gen ? general_thread : continue_thread; |
6d820c5c | 1625 | char *buf = rs->buf; |
79d7f229 | 1626 | char *endbuf = rs->buf + get_remote_packet_size (); |
c906108c | 1627 | |
79d7f229 | 1628 | if (ptid_equal (state, ptid)) |
c906108c SS |
1629 | return; |
1630 | ||
79d7f229 PA |
1631 | *buf++ = 'H'; |
1632 | *buf++ = gen ? 'g' : 'c'; | |
1633 | if (ptid_equal (ptid, magic_null_ptid)) | |
1634 | xsnprintf (buf, endbuf - buf, "0"); | |
1635 | else if (ptid_equal (ptid, any_thread_ptid)) | |
1636 | xsnprintf (buf, endbuf - buf, "0"); | |
1637 | else if (ptid_equal (ptid, minus_one_ptid)) | |
1638 | xsnprintf (buf, endbuf - buf, "-1"); | |
1639 | else | |
82f73884 | 1640 | write_ptid (buf, endbuf, ptid); |
79d7f229 | 1641 | putpkt (rs->buf); |
6d820c5c | 1642 | getpkt (&rs->buf, &rs->buf_size, 0); |
c906108c | 1643 | if (gen) |
79d7f229 | 1644 | general_thread = ptid; |
c906108c | 1645 | else |
79d7f229 | 1646 | continue_thread = ptid; |
c906108c | 1647 | } |
79d7f229 PA |
1648 | |
1649 | static void | |
1650 | set_general_thread (struct ptid ptid) | |
1651 | { | |
1652 | set_thread (ptid, 1); | |
1653 | } | |
1654 | ||
1655 | static void | |
1656 | set_continue_thread (struct ptid ptid) | |
1657 | { | |
1658 | set_thread (ptid, 0); | |
1659 | } | |
1660 | ||
3c9c4b83 PA |
1661 | /* Change the remote current process. Which thread within the process |
1662 | ends up selected isn't important, as long as it is the same process | |
1663 | as what INFERIOR_PTID points to. | |
1664 | ||
1665 | This comes from that fact that there is no explicit notion of | |
1666 | "selected process" in the protocol. The selected process for | |
1667 | general operations is the process the selected general thread | |
1668 | belongs to. */ | |
1669 | ||
1670 | static void | |
1671 | set_general_process (void) | |
1672 | { | |
1673 | struct remote_state *rs = get_remote_state (); | |
1674 | ||
1675 | /* If the remote can't handle multiple processes, don't bother. */ | |
1676 | if (!remote_multi_process_p (rs)) | |
1677 | return; | |
1678 | ||
1679 | /* We only need to change the remote current thread if it's pointing | |
1680 | at some other process. */ | |
1681 | if (ptid_get_pid (general_thread) != ptid_get_pid (inferior_ptid)) | |
1682 | set_general_thread (inferior_ptid); | |
1683 | } | |
1684 | ||
c906108c | 1685 | \f |
79d7f229 PA |
1686 | /* Return nonzero if the thread PTID is still alive on the remote |
1687 | system. */ | |
c906108c SS |
1688 | |
1689 | static int | |
28439f5e | 1690 | remote_thread_alive (struct target_ops *ops, ptid_t ptid) |
c906108c | 1691 | { |
6d820c5c | 1692 | struct remote_state *rs = get_remote_state (); |
82f73884 | 1693 | char *p, *endp; |
c906108c | 1694 | |
c0a2216e PA |
1695 | if (ptid_equal (ptid, magic_null_ptid)) |
1696 | /* The main thread is always alive. */ | |
1697 | return 1; | |
1698 | ||
1699 | if (ptid_get_pid (ptid) != 0 && ptid_get_tid (ptid) == 0) | |
1700 | /* The main thread is always alive. This can happen after a | |
1701 | vAttach, if the remote side doesn't support | |
1702 | multi-threading. */ | |
1703 | return 1; | |
1704 | ||
82f73884 PA |
1705 | p = rs->buf; |
1706 | endp = rs->buf + get_remote_packet_size (); | |
1707 | ||
1708 | *p++ = 'T'; | |
1709 | write_ptid (p, endp, ptid); | |
1710 | ||
2e9f7625 | 1711 | putpkt (rs->buf); |
6d820c5c | 1712 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 1713 | return (rs->buf[0] == 'O' && rs->buf[1] == 'K'); |
c906108c SS |
1714 | } |
1715 | ||
1716 | /* About these extended threadlist and threadinfo packets. They are | |
1717 | variable length packets but, the fields within them are often fixed | |
1718 | length. They are redundent enough to send over UDP as is the | |
1719 | remote protocol in general. There is a matching unit test module | |
1720 | in libstub. */ | |
1721 | ||
cce74817 JM |
1722 | #define OPAQUETHREADBYTES 8 |
1723 | ||
1724 | /* a 64 bit opaque identifier */ | |
1725 | typedef unsigned char threadref[OPAQUETHREADBYTES]; | |
1726 | ||
23860348 MS |
1727 | /* WARNING: This threadref data structure comes from the remote O.S., |
1728 | libstub protocol encoding, and remote.c. it is not particularly | |
1729 | changable. */ | |
cce74817 JM |
1730 | |
1731 | /* Right now, the internal structure is int. We want it to be bigger. | |
1732 | Plan to fix this. | |
c5aa993b | 1733 | */ |
cce74817 | 1734 | |
23860348 | 1735 | typedef int gdb_threadref; /* Internal GDB thread reference. */ |
cce74817 | 1736 | |
9d1f7ab2 | 1737 | /* gdb_ext_thread_info is an internal GDB data structure which is |
cfde0993 | 1738 | equivalent to the reply of the remote threadinfo packet. */ |
cce74817 JM |
1739 | |
1740 | struct gdb_ext_thread_info | |
c5aa993b | 1741 | { |
23860348 | 1742 | threadref threadid; /* External form of thread reference. */ |
2bc416ba | 1743 | int active; /* Has state interesting to GDB? |
23860348 | 1744 | regs, stack. */ |
2bc416ba | 1745 | char display[256]; /* Brief state display, name, |
cedea757 | 1746 | blocked/suspended. */ |
23860348 | 1747 | char shortname[32]; /* To be used to name threads. */ |
2bc416ba | 1748 | char more_display[256]; /* Long info, statistics, queue depth, |
23860348 | 1749 | whatever. */ |
c5aa993b | 1750 | }; |
cce74817 JM |
1751 | |
1752 | /* The volume of remote transfers can be limited by submitting | |
1753 | a mask containing bits specifying the desired information. | |
1754 | Use a union of these values as the 'selection' parameter to | |
1755 | get_thread_info. FIXME: Make these TAG names more thread specific. | |
c5aa993b | 1756 | */ |
cce74817 JM |
1757 | |
1758 | #define TAG_THREADID 1 | |
1759 | #define TAG_EXISTS 2 | |
1760 | #define TAG_DISPLAY 4 | |
1761 | #define TAG_THREADNAME 8 | |
c5aa993b | 1762 | #define TAG_MOREDISPLAY 16 |
cce74817 | 1763 | |
23860348 | 1764 | #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2) |
c906108c | 1765 | |
b2dd6311 | 1766 | char *unpack_varlen_hex (char *buff, ULONGEST *result); |
cce74817 | 1767 | |
a14ed312 | 1768 | static char *unpack_nibble (char *buf, int *val); |
cce74817 | 1769 | |
a14ed312 | 1770 | static char *pack_nibble (char *buf, int nibble); |
cce74817 | 1771 | |
23860348 | 1772 | static char *pack_hex_byte (char *pkt, int /* unsigned char */ byte); |
cce74817 | 1773 | |
a14ed312 | 1774 | static char *unpack_byte (char *buf, int *value); |
cce74817 | 1775 | |
a14ed312 | 1776 | static char *pack_int (char *buf, int value); |
cce74817 | 1777 | |
a14ed312 | 1778 | static char *unpack_int (char *buf, int *value); |
cce74817 | 1779 | |
a14ed312 | 1780 | static char *unpack_string (char *src, char *dest, int length); |
cce74817 | 1781 | |
23860348 | 1782 | static char *pack_threadid (char *pkt, threadref *id); |
cce74817 | 1783 | |
23860348 | 1784 | static char *unpack_threadid (char *inbuf, threadref *id); |
cce74817 | 1785 | |
23860348 | 1786 | void int_to_threadref (threadref *id, int value); |
cce74817 | 1787 | |
23860348 | 1788 | static int threadref_to_int (threadref *ref); |
cce74817 | 1789 | |
23860348 | 1790 | static void copy_threadref (threadref *dest, threadref *src); |
cce74817 | 1791 | |
23860348 | 1792 | static int threadmatch (threadref *dest, threadref *src); |
cce74817 | 1793 | |
2bc416ba | 1794 | static char *pack_threadinfo_request (char *pkt, int mode, |
23860348 | 1795 | threadref *id); |
cce74817 | 1796 | |
a14ed312 | 1797 | static int remote_unpack_thread_info_response (char *pkt, |
23860348 | 1798 | threadref *expectedref, |
a14ed312 KB |
1799 | struct gdb_ext_thread_info |
1800 | *info); | |
cce74817 JM |
1801 | |
1802 | ||
2bc416ba | 1803 | static int remote_get_threadinfo (threadref *threadid, |
23860348 | 1804 | int fieldset, /*TAG mask */ |
a14ed312 | 1805 | struct gdb_ext_thread_info *info); |
cce74817 | 1806 | |
a14ed312 KB |
1807 | static char *pack_threadlist_request (char *pkt, int startflag, |
1808 | int threadcount, | |
23860348 | 1809 | threadref *nextthread); |
cce74817 | 1810 | |
a14ed312 KB |
1811 | static int parse_threadlist_response (char *pkt, |
1812 | int result_limit, | |
23860348 | 1813 | threadref *original_echo, |
2bc416ba | 1814 | threadref *resultlist, |
23860348 | 1815 | int *doneflag); |
cce74817 | 1816 | |
a14ed312 | 1817 | static int remote_get_threadlist (int startflag, |
23860348 | 1818 | threadref *nextthread, |
a14ed312 KB |
1819 | int result_limit, |
1820 | int *done, | |
2bc416ba | 1821 | int *result_count, |
23860348 | 1822 | threadref *threadlist); |
cce74817 | 1823 | |
23860348 | 1824 | typedef int (*rmt_thread_action) (threadref *ref, void *context); |
cce74817 | 1825 | |
a14ed312 KB |
1826 | static int remote_threadlist_iterator (rmt_thread_action stepfunction, |
1827 | void *context, int looplimit); | |
cce74817 | 1828 | |
23860348 | 1829 | static int remote_newthread_step (threadref *ref, void *context); |
cce74817 | 1830 | |
82f73884 PA |
1831 | |
1832 | /* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the | |
1833 | buffer we're allowed to write to. Returns | |
1834 | BUF+CHARACTERS_WRITTEN. */ | |
1835 | ||
1836 | static char * | |
1837 | write_ptid (char *buf, const char *endbuf, ptid_t ptid) | |
1838 | { | |
1839 | int pid, tid; | |
1840 | struct remote_state *rs = get_remote_state (); | |
1841 | ||
1842 | if (remote_multi_process_p (rs)) | |
1843 | { | |
1844 | pid = ptid_get_pid (ptid); | |
1845 | if (pid < 0) | |
1846 | buf += xsnprintf (buf, endbuf - buf, "p-%x.", -pid); | |
1847 | else | |
1848 | buf += xsnprintf (buf, endbuf - buf, "p%x.", pid); | |
1849 | } | |
1850 | tid = ptid_get_tid (ptid); | |
1851 | if (tid < 0) | |
1852 | buf += xsnprintf (buf, endbuf - buf, "-%x", -tid); | |
1853 | else | |
1854 | buf += xsnprintf (buf, endbuf - buf, "%x", tid); | |
1855 | ||
1856 | return buf; | |
1857 | } | |
1858 | ||
1859 | /* Extract a PTID from BUF. If non-null, OBUF is set to the to one | |
1860 | passed the last parsed char. Returns null_ptid on error. */ | |
1861 | ||
1862 | static ptid_t | |
1863 | read_ptid (char *buf, char **obuf) | |
1864 | { | |
1865 | char *p = buf; | |
1866 | char *pp; | |
1867 | ULONGEST pid = 0, tid = 0; | |
82f73884 PA |
1868 | |
1869 | if (*p == 'p') | |
1870 | { | |
1871 | /* Multi-process ptid. */ | |
1872 | pp = unpack_varlen_hex (p + 1, &pid); | |
1873 | if (*pp != '.') | |
1874 | error (_("invalid remote ptid: %s\n"), p); | |
1875 | ||
1876 | p = pp; | |
1877 | pp = unpack_varlen_hex (p + 1, &tid); | |
1878 | if (obuf) | |
1879 | *obuf = pp; | |
1880 | return ptid_build (pid, 0, tid); | |
1881 | } | |
1882 | ||
1883 | /* No multi-process. Just a tid. */ | |
1884 | pp = unpack_varlen_hex (p, &tid); | |
1885 | ||
1886 | /* Since the stub is not sending a process id, then default to | |
ca19bf23 PA |
1887 | what's in inferior_ptid, unless it's null at this point. If so, |
1888 | then since there's no way to know the pid of the reported | |
1889 | threads, use the magic number. */ | |
1890 | if (ptid_equal (inferior_ptid, null_ptid)) | |
1891 | pid = ptid_get_pid (magic_null_ptid); | |
1892 | else | |
1893 | pid = ptid_get_pid (inferior_ptid); | |
82f73884 PA |
1894 | |
1895 | if (obuf) | |
1896 | *obuf = pp; | |
1897 | return ptid_build (pid, 0, tid); | |
1898 | } | |
1899 | ||
23860348 | 1900 | /* Encode 64 bits in 16 chars of hex. */ |
c906108c SS |
1901 | |
1902 | static const char hexchars[] = "0123456789abcdef"; | |
1903 | ||
1904 | static int | |
fba45db2 | 1905 | ishex (int ch, int *val) |
c906108c SS |
1906 | { |
1907 | if ((ch >= 'a') && (ch <= 'f')) | |
1908 | { | |
1909 | *val = ch - 'a' + 10; | |
1910 | return 1; | |
1911 | } | |
1912 | if ((ch >= 'A') && (ch <= 'F')) | |
1913 | { | |
1914 | *val = ch - 'A' + 10; | |
1915 | return 1; | |
1916 | } | |
1917 | if ((ch >= '0') && (ch <= '9')) | |
1918 | { | |
1919 | *val = ch - '0'; | |
1920 | return 1; | |
1921 | } | |
1922 | return 0; | |
1923 | } | |
1924 | ||
1925 | static int | |
fba45db2 | 1926 | stubhex (int ch) |
c906108c SS |
1927 | { |
1928 | if (ch >= 'a' && ch <= 'f') | |
1929 | return ch - 'a' + 10; | |
1930 | if (ch >= '0' && ch <= '9') | |
1931 | return ch - '0'; | |
1932 | if (ch >= 'A' && ch <= 'F') | |
1933 | return ch - 'A' + 10; | |
1934 | return -1; | |
1935 | } | |
1936 | ||
1937 | static int | |
fba45db2 | 1938 | stub_unpack_int (char *buff, int fieldlength) |
c906108c SS |
1939 | { |
1940 | int nibble; | |
1941 | int retval = 0; | |
1942 | ||
1943 | while (fieldlength) | |
1944 | { | |
1945 | nibble = stubhex (*buff++); | |
1946 | retval |= nibble; | |
1947 | fieldlength--; | |
1948 | if (fieldlength) | |
1949 | retval = retval << 4; | |
1950 | } | |
1951 | return retval; | |
1952 | } | |
1953 | ||
1954 | char * | |
fba45db2 | 1955 | unpack_varlen_hex (char *buff, /* packet to parse */ |
b2dd6311 | 1956 | ULONGEST *result) |
c906108c SS |
1957 | { |
1958 | int nibble; | |
d49c44d5 | 1959 | ULONGEST retval = 0; |
c906108c SS |
1960 | |
1961 | while (ishex (*buff, &nibble)) | |
1962 | { | |
1963 | buff++; | |
1964 | retval = retval << 4; | |
1965 | retval |= nibble & 0x0f; | |
1966 | } | |
1967 | *result = retval; | |
1968 | return buff; | |
1969 | } | |
1970 | ||
1971 | static char * | |
fba45db2 | 1972 | unpack_nibble (char *buf, int *val) |
c906108c | 1973 | { |
b7589f7d | 1974 | *val = fromhex (*buf++); |
c906108c SS |
1975 | return buf; |
1976 | } | |
1977 | ||
1978 | static char * | |
fba45db2 | 1979 | pack_nibble (char *buf, int nibble) |
c906108c SS |
1980 | { |
1981 | *buf++ = hexchars[(nibble & 0x0f)]; | |
1982 | return buf; | |
1983 | } | |
1984 | ||
1985 | static char * | |
fba45db2 | 1986 | pack_hex_byte (char *pkt, int byte) |
c906108c SS |
1987 | { |
1988 | *pkt++ = hexchars[(byte >> 4) & 0xf]; | |
1989 | *pkt++ = hexchars[(byte & 0xf)]; | |
1990 | return pkt; | |
1991 | } | |
1992 | ||
1993 | static char * | |
fba45db2 | 1994 | unpack_byte (char *buf, int *value) |
c906108c SS |
1995 | { |
1996 | *value = stub_unpack_int (buf, 2); | |
1997 | return buf + 2; | |
1998 | } | |
1999 | ||
2000 | static char * | |
fba45db2 | 2001 | pack_int (char *buf, int value) |
c906108c SS |
2002 | { |
2003 | buf = pack_hex_byte (buf, (value >> 24) & 0xff); | |
2004 | buf = pack_hex_byte (buf, (value >> 16) & 0xff); | |
2005 | buf = pack_hex_byte (buf, (value >> 8) & 0x0ff); | |
2006 | buf = pack_hex_byte (buf, (value & 0xff)); | |
2007 | return buf; | |
2008 | } | |
2009 | ||
2010 | static char * | |
fba45db2 | 2011 | unpack_int (char *buf, int *value) |
c906108c SS |
2012 | { |
2013 | *value = stub_unpack_int (buf, 8); | |
2014 | return buf + 8; | |
2015 | } | |
2016 | ||
23860348 | 2017 | #if 0 /* Currently unused, uncomment when needed. */ |
a14ed312 | 2018 | static char *pack_string (char *pkt, char *string); |
c906108c SS |
2019 | |
2020 | static char * | |
fba45db2 | 2021 | pack_string (char *pkt, char *string) |
c906108c SS |
2022 | { |
2023 | char ch; | |
2024 | int len; | |
2025 | ||
2026 | len = strlen (string); | |
2027 | if (len > 200) | |
23860348 | 2028 | len = 200; /* Bigger than most GDB packets, junk??? */ |
c906108c SS |
2029 | pkt = pack_hex_byte (pkt, len); |
2030 | while (len-- > 0) | |
2031 | { | |
2032 | ch = *string++; | |
2033 | if ((ch == '\0') || (ch == '#')) | |
23860348 | 2034 | ch = '*'; /* Protect encapsulation. */ |
c906108c SS |
2035 | *pkt++ = ch; |
2036 | } | |
2037 | return pkt; | |
2038 | } | |
2039 | #endif /* 0 (unused) */ | |
2040 | ||
2041 | static char * | |
fba45db2 | 2042 | unpack_string (char *src, char *dest, int length) |
c906108c SS |
2043 | { |
2044 | while (length--) | |
2045 | *dest++ = *src++; | |
2046 | *dest = '\0'; | |
2047 | return src; | |
2048 | } | |
2049 | ||
2050 | static char * | |
fba45db2 | 2051 | pack_threadid (char *pkt, threadref *id) |
c906108c SS |
2052 | { |
2053 | char *limit; | |
2054 | unsigned char *altid; | |
2055 | ||
2056 | altid = (unsigned char *) id; | |
2057 | limit = pkt + BUF_THREAD_ID_SIZE; | |
2058 | while (pkt < limit) | |
2059 | pkt = pack_hex_byte (pkt, *altid++); | |
2060 | return pkt; | |
2061 | } | |
2062 | ||
2063 | ||
2064 | static char * | |
fba45db2 | 2065 | unpack_threadid (char *inbuf, threadref *id) |
c906108c SS |
2066 | { |
2067 | char *altref; | |
2068 | char *limit = inbuf + BUF_THREAD_ID_SIZE; | |
2069 | int x, y; | |
2070 | ||
2071 | altref = (char *) id; | |
2072 | ||
2073 | while (inbuf < limit) | |
2074 | { | |
2075 | x = stubhex (*inbuf++); | |
2076 | y = stubhex (*inbuf++); | |
2077 | *altref++ = (x << 4) | y; | |
2078 | } | |
2079 | return inbuf; | |
2080 | } | |
2081 | ||
2082 | /* Externally, threadrefs are 64 bits but internally, they are still | |
2083 | ints. This is due to a mismatch of specifications. We would like | |
2084 | to use 64bit thread references internally. This is an adapter | |
2085 | function. */ | |
2086 | ||
2087 | void | |
fba45db2 | 2088 | int_to_threadref (threadref *id, int value) |
c906108c SS |
2089 | { |
2090 | unsigned char *scan; | |
2091 | ||
2092 | scan = (unsigned char *) id; | |
2093 | { | |
2094 | int i = 4; | |
2095 | while (i--) | |
2096 | *scan++ = 0; | |
2097 | } | |
2098 | *scan++ = (value >> 24) & 0xff; | |
2099 | *scan++ = (value >> 16) & 0xff; | |
2100 | *scan++ = (value >> 8) & 0xff; | |
2101 | *scan++ = (value & 0xff); | |
2102 | } | |
2103 | ||
2104 | static int | |
fba45db2 | 2105 | threadref_to_int (threadref *ref) |
c906108c SS |
2106 | { |
2107 | int i, value = 0; | |
2108 | unsigned char *scan; | |
2109 | ||
cfd77fa1 | 2110 | scan = *ref; |
c906108c SS |
2111 | scan += 4; |
2112 | i = 4; | |
2113 | while (i-- > 0) | |
2114 | value = (value << 8) | ((*scan++) & 0xff); | |
2115 | return value; | |
2116 | } | |
2117 | ||
2118 | static void | |
fba45db2 | 2119 | copy_threadref (threadref *dest, threadref *src) |
c906108c SS |
2120 | { |
2121 | int i; | |
2122 | unsigned char *csrc, *cdest; | |
2123 | ||
2124 | csrc = (unsigned char *) src; | |
2125 | cdest = (unsigned char *) dest; | |
2126 | i = 8; | |
2127 | while (i--) | |
2128 | *cdest++ = *csrc++; | |
2129 | } | |
2130 | ||
2131 | static int | |
fba45db2 | 2132 | threadmatch (threadref *dest, threadref *src) |
c906108c | 2133 | { |
23860348 | 2134 | /* Things are broken right now, so just assume we got a match. */ |
c906108c SS |
2135 | #if 0 |
2136 | unsigned char *srcp, *destp; | |
2137 | int i, result; | |
2138 | srcp = (char *) src; | |
2139 | destp = (char *) dest; | |
2140 | ||
2141 | result = 1; | |
2142 | while (i-- > 0) | |
2143 | result &= (*srcp++ == *destp++) ? 1 : 0; | |
2144 | return result; | |
2145 | #endif | |
2146 | return 1; | |
2147 | } | |
2148 | ||
2149 | /* | |
c5aa993b JM |
2150 | threadid:1, # always request threadid |
2151 | context_exists:2, | |
2152 | display:4, | |
2153 | unique_name:8, | |
2154 | more_display:16 | |
2155 | */ | |
c906108c SS |
2156 | |
2157 | /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */ | |
2158 | ||
2159 | static char * | |
fba45db2 | 2160 | pack_threadinfo_request (char *pkt, int mode, threadref *id) |
c906108c | 2161 | { |
23860348 MS |
2162 | *pkt++ = 'q'; /* Info Query */ |
2163 | *pkt++ = 'P'; /* process or thread info */ | |
2164 | pkt = pack_int (pkt, mode); /* mode */ | |
c906108c | 2165 | pkt = pack_threadid (pkt, id); /* threadid */ |
23860348 | 2166 | *pkt = '\0'; /* terminate */ |
c906108c SS |
2167 | return pkt; |
2168 | } | |
2169 | ||
23860348 | 2170 | /* These values tag the fields in a thread info response packet. */ |
c906108c | 2171 | /* Tagging the fields allows us to request specific fields and to |
23860348 | 2172 | add more fields as time goes by. */ |
c906108c | 2173 | |
23860348 | 2174 | #define TAG_THREADID 1 /* Echo the thread identifier. */ |
c5aa993b | 2175 | #define TAG_EXISTS 2 /* Is this process defined enough to |
23860348 | 2176 | fetch registers and its stack? */ |
c5aa993b | 2177 | #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */ |
23860348 | 2178 | #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */ |
802188a7 | 2179 | #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about |
23860348 | 2180 | the process. */ |
c906108c SS |
2181 | |
2182 | static int | |
fba45db2 KB |
2183 | remote_unpack_thread_info_response (char *pkt, threadref *expectedref, |
2184 | struct gdb_ext_thread_info *info) | |
c906108c | 2185 | { |
d01949b6 | 2186 | struct remote_state *rs = get_remote_state (); |
c906108c | 2187 | int mask, length; |
cfd77fa1 | 2188 | int tag; |
c906108c | 2189 | threadref ref; |
6d820c5c | 2190 | char *limit = pkt + rs->buf_size; /* Plausible parsing limit. */ |
c906108c SS |
2191 | int retval = 1; |
2192 | ||
23860348 | 2193 | /* info->threadid = 0; FIXME: implement zero_threadref. */ |
c906108c SS |
2194 | info->active = 0; |
2195 | info->display[0] = '\0'; | |
2196 | info->shortname[0] = '\0'; | |
2197 | info->more_display[0] = '\0'; | |
2198 | ||
23860348 MS |
2199 | /* Assume the characters indicating the packet type have been |
2200 | stripped. */ | |
c906108c SS |
2201 | pkt = unpack_int (pkt, &mask); /* arg mask */ |
2202 | pkt = unpack_threadid (pkt, &ref); | |
2203 | ||
2204 | if (mask == 0) | |
8a3fe4f8 | 2205 | warning (_("Incomplete response to threadinfo request.")); |
c906108c | 2206 | if (!threadmatch (&ref, expectedref)) |
23860348 | 2207 | { /* This is an answer to a different request. */ |
8a3fe4f8 | 2208 | warning (_("ERROR RMT Thread info mismatch.")); |
c906108c SS |
2209 | return 0; |
2210 | } | |
2211 | copy_threadref (&info->threadid, &ref); | |
2212 | ||
23860348 | 2213 | /* Loop on tagged fields , try to bail if somthing goes wrong. */ |
c906108c | 2214 | |
23860348 MS |
2215 | /* Packets are terminated with nulls. */ |
2216 | while ((pkt < limit) && mask && *pkt) | |
c906108c SS |
2217 | { |
2218 | pkt = unpack_int (pkt, &tag); /* tag */ | |
23860348 MS |
2219 | pkt = unpack_byte (pkt, &length); /* length */ |
2220 | if (!(tag & mask)) /* Tags out of synch with mask. */ | |
c906108c | 2221 | { |
8a3fe4f8 | 2222 | warning (_("ERROR RMT: threadinfo tag mismatch.")); |
c906108c SS |
2223 | retval = 0; |
2224 | break; | |
2225 | } | |
2226 | if (tag == TAG_THREADID) | |
2227 | { | |
2228 | if (length != 16) | |
2229 | { | |
8a3fe4f8 | 2230 | warning (_("ERROR RMT: length of threadid is not 16.")); |
c906108c SS |
2231 | retval = 0; |
2232 | break; | |
2233 | } | |
2234 | pkt = unpack_threadid (pkt, &ref); | |
2235 | mask = mask & ~TAG_THREADID; | |
2236 | continue; | |
2237 | } | |
2238 | if (tag == TAG_EXISTS) | |
2239 | { | |
2240 | info->active = stub_unpack_int (pkt, length); | |
2241 | pkt += length; | |
2242 | mask = mask & ~(TAG_EXISTS); | |
2243 | if (length > 8) | |
2244 | { | |
8a3fe4f8 | 2245 | warning (_("ERROR RMT: 'exists' length too long.")); |
c906108c SS |
2246 | retval = 0; |
2247 | break; | |
2248 | } | |
2249 | continue; | |
2250 | } | |
2251 | if (tag == TAG_THREADNAME) | |
2252 | { | |
2253 | pkt = unpack_string (pkt, &info->shortname[0], length); | |
2254 | mask = mask & ~TAG_THREADNAME; | |
2255 | continue; | |
2256 | } | |
2257 | if (tag == TAG_DISPLAY) | |
2258 | { | |
2259 | pkt = unpack_string (pkt, &info->display[0], length); | |
2260 | mask = mask & ~TAG_DISPLAY; | |
2261 | continue; | |
2262 | } | |
2263 | if (tag == TAG_MOREDISPLAY) | |
2264 | { | |
2265 | pkt = unpack_string (pkt, &info->more_display[0], length); | |
2266 | mask = mask & ~TAG_MOREDISPLAY; | |
2267 | continue; | |
2268 | } | |
8a3fe4f8 | 2269 | warning (_("ERROR RMT: unknown thread info tag.")); |
23860348 | 2270 | break; /* Not a tag we know about. */ |
c906108c SS |
2271 | } |
2272 | return retval; | |
2273 | } | |
2274 | ||
2275 | static int | |
fba45db2 KB |
2276 | remote_get_threadinfo (threadref *threadid, int fieldset, /* TAG mask */ |
2277 | struct gdb_ext_thread_info *info) | |
c906108c | 2278 | { |
d01949b6 | 2279 | struct remote_state *rs = get_remote_state (); |
c906108c | 2280 | int result; |
c906108c | 2281 | |
2e9f7625 DJ |
2282 | pack_threadinfo_request (rs->buf, fieldset, threadid); |
2283 | putpkt (rs->buf); | |
6d820c5c | 2284 | getpkt (&rs->buf, &rs->buf_size, 0); |
3084dd77 PA |
2285 | |
2286 | if (rs->buf[0] == '\0') | |
2287 | return 0; | |
2288 | ||
2e9f7625 | 2289 | result = remote_unpack_thread_info_response (rs->buf + 2, |
23860348 | 2290 | threadid, info); |
c906108c SS |
2291 | return result; |
2292 | } | |
2293 | ||
c906108c SS |
2294 | /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */ |
2295 | ||
2296 | static char * | |
fba45db2 KB |
2297 | pack_threadlist_request (char *pkt, int startflag, int threadcount, |
2298 | threadref *nextthread) | |
c906108c SS |
2299 | { |
2300 | *pkt++ = 'q'; /* info query packet */ | |
2301 | *pkt++ = 'L'; /* Process LIST or threadLIST request */ | |
23860348 | 2302 | pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */ |
c906108c SS |
2303 | pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */ |
2304 | pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */ | |
2305 | *pkt = '\0'; | |
2306 | return pkt; | |
2307 | } | |
2308 | ||
2309 | /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */ | |
2310 | ||
2311 | static int | |
fba45db2 KB |
2312 | parse_threadlist_response (char *pkt, int result_limit, |
2313 | threadref *original_echo, threadref *resultlist, | |
2314 | int *doneflag) | |
c906108c | 2315 | { |
d01949b6 | 2316 | struct remote_state *rs = get_remote_state (); |
c906108c SS |
2317 | char *limit; |
2318 | int count, resultcount, done; | |
2319 | ||
2320 | resultcount = 0; | |
2321 | /* Assume the 'q' and 'M chars have been stripped. */ | |
6d820c5c | 2322 | limit = pkt + (rs->buf_size - BUF_THREAD_ID_SIZE); |
23860348 | 2323 | /* done parse past here */ |
c906108c SS |
2324 | pkt = unpack_byte (pkt, &count); /* count field */ |
2325 | pkt = unpack_nibble (pkt, &done); | |
2326 | /* The first threadid is the argument threadid. */ | |
2327 | pkt = unpack_threadid (pkt, original_echo); /* should match query packet */ | |
2328 | while ((count-- > 0) && (pkt < limit)) | |
2329 | { | |
2330 | pkt = unpack_threadid (pkt, resultlist++); | |
2331 | if (resultcount++ >= result_limit) | |
2332 | break; | |
2333 | } | |
2334 | if (doneflag) | |
2335 | *doneflag = done; | |
2336 | return resultcount; | |
2337 | } | |
2338 | ||
2339 | static int | |
fba45db2 KB |
2340 | remote_get_threadlist (int startflag, threadref *nextthread, int result_limit, |
2341 | int *done, int *result_count, threadref *threadlist) | |
c906108c | 2342 | { |
d01949b6 | 2343 | struct remote_state *rs = get_remote_state (); |
c906108c | 2344 | static threadref echo_nextthread; |
c906108c SS |
2345 | int result = 1; |
2346 | ||
23860348 | 2347 | /* Trancate result limit to be smaller than the packet size. */ |
ea9c271d DJ |
2348 | if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10) >= get_remote_packet_size ()) |
2349 | result_limit = (get_remote_packet_size () / BUF_THREAD_ID_SIZE) - 2; | |
c906108c | 2350 | |
6d820c5c DJ |
2351 | pack_threadlist_request (rs->buf, startflag, result_limit, nextthread); |
2352 | putpkt (rs->buf); | |
2353 | getpkt (&rs->buf, &rs->buf_size, 0); | |
c906108c | 2354 | |
d8f2712d VP |
2355 | if (*rs->buf == '\0') |
2356 | *result_count = 0; | |
2357 | else | |
2358 | *result_count = | |
2359 | parse_threadlist_response (rs->buf + 2, result_limit, &echo_nextthread, | |
2360 | threadlist, done); | |
c906108c SS |
2361 | |
2362 | if (!threadmatch (&echo_nextthread, nextthread)) | |
2363 | { | |
23860348 MS |
2364 | /* FIXME: This is a good reason to drop the packet. */ |
2365 | /* Possably, there is a duplicate response. */ | |
c906108c SS |
2366 | /* Possabilities : |
2367 | retransmit immediatly - race conditions | |
2368 | retransmit after timeout - yes | |
2369 | exit | |
2370 | wait for packet, then exit | |
2371 | */ | |
8a3fe4f8 | 2372 | warning (_("HMM: threadlist did not echo arg thread, dropping it.")); |
23860348 | 2373 | return 0; /* I choose simply exiting. */ |
c906108c SS |
2374 | } |
2375 | if (*result_count <= 0) | |
2376 | { | |
2377 | if (*done != 1) | |
2378 | { | |
8a3fe4f8 | 2379 | warning (_("RMT ERROR : failed to get remote thread list.")); |
c906108c SS |
2380 | result = 0; |
2381 | } | |
2382 | return result; /* break; */ | |
2383 | } | |
2384 | if (*result_count > result_limit) | |
2385 | { | |
2386 | *result_count = 0; | |
8a3fe4f8 | 2387 | warning (_("RMT ERROR: threadlist response longer than requested.")); |
c906108c SS |
2388 | return 0; |
2389 | } | |
2390 | return result; | |
2391 | } | |
2392 | ||
23860348 MS |
2393 | /* This is the interface between remote and threads, remotes upper |
2394 | interface. */ | |
c906108c SS |
2395 | |
2396 | /* remote_find_new_threads retrieves the thread list and for each | |
2397 | thread in the list, looks up the thread in GDB's internal list, | |
79d7f229 | 2398 | adding the thread if it does not already exist. This involves |
c906108c SS |
2399 | getting partial thread lists from the remote target so, polling the |
2400 | quit_flag is required. */ | |
2401 | ||
2402 | ||
23860348 | 2403 | /* About this many threadisds fit in a packet. */ |
c906108c SS |
2404 | |
2405 | #define MAXTHREADLISTRESULTS 32 | |
2406 | ||
2407 | static int | |
fba45db2 KB |
2408 | remote_threadlist_iterator (rmt_thread_action stepfunction, void *context, |
2409 | int looplimit) | |
c906108c SS |
2410 | { |
2411 | int done, i, result_count; | |
2412 | int startflag = 1; | |
2413 | int result = 1; | |
2414 | int loopcount = 0; | |
2415 | static threadref nextthread; | |
2416 | static threadref resultthreadlist[MAXTHREADLISTRESULTS]; | |
2417 | ||
2418 | done = 0; | |
2419 | while (!done) | |
2420 | { | |
2421 | if (loopcount++ > looplimit) | |
2422 | { | |
2423 | result = 0; | |
8a3fe4f8 | 2424 | warning (_("Remote fetch threadlist -infinite loop-.")); |
c906108c SS |
2425 | break; |
2426 | } | |
2427 | if (!remote_get_threadlist (startflag, &nextthread, MAXTHREADLISTRESULTS, | |
2428 | &done, &result_count, resultthreadlist)) | |
2429 | { | |
2430 | result = 0; | |
2431 | break; | |
2432 | } | |
23860348 | 2433 | /* Clear for later iterations. */ |
c906108c SS |
2434 | startflag = 0; |
2435 | /* Setup to resume next batch of thread references, set nextthread. */ | |
2436 | if (result_count >= 1) | |
2437 | copy_threadref (&nextthread, &resultthreadlist[result_count - 1]); | |
2438 | i = 0; | |
2439 | while (result_count--) | |
2440 | if (!(result = (*stepfunction) (&resultthreadlist[i++], context))) | |
2441 | break; | |
2442 | } | |
2443 | return result; | |
2444 | } | |
2445 | ||
2446 | static int | |
fba45db2 | 2447 | remote_newthread_step (threadref *ref, void *context) |
c906108c | 2448 | { |
79d7f229 PA |
2449 | int pid = ptid_get_pid (inferior_ptid); |
2450 | ptid_t ptid = ptid_build (pid, 0, threadref_to_int (ref)); | |
39f77062 KB |
2451 | |
2452 | if (!in_thread_list (ptid)) | |
2453 | add_thread (ptid); | |
c906108c SS |
2454 | return 1; /* continue iterator */ |
2455 | } | |
2456 | ||
2457 | #define CRAZY_MAX_THREADS 1000 | |
2458 | ||
39f77062 KB |
2459 | static ptid_t |
2460 | remote_current_thread (ptid_t oldpid) | |
c906108c | 2461 | { |
d01949b6 | 2462 | struct remote_state *rs = get_remote_state (); |
c906108c SS |
2463 | |
2464 | putpkt ("qC"); | |
6d820c5c | 2465 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 2466 | if (rs->buf[0] == 'Q' && rs->buf[1] == 'C') |
82f73884 | 2467 | return read_ptid (&rs->buf[2], NULL); |
c906108c SS |
2468 | else |
2469 | return oldpid; | |
2470 | } | |
2471 | ||
802188a7 RM |
2472 | /* Find new threads for info threads command. |
2473 | * Original version, using John Metzler's thread protocol. | |
9d1f7ab2 | 2474 | */ |
cce74817 JM |
2475 | |
2476 | static void | |
fba45db2 | 2477 | remote_find_new_threads (void) |
c906108c | 2478 | { |
c5aa993b JM |
2479 | remote_threadlist_iterator (remote_newthread_step, 0, |
2480 | CRAZY_MAX_THREADS); | |
c906108c SS |
2481 | } |
2482 | ||
dc146f7c VP |
2483 | #if defined(HAVE_LIBEXPAT) |
2484 | ||
2485 | typedef struct thread_item | |
2486 | { | |
2487 | ptid_t ptid; | |
2488 | char *extra; | |
2489 | int core; | |
2490 | } thread_item_t; | |
2491 | DEF_VEC_O(thread_item_t); | |
2492 | ||
2493 | struct threads_parsing_context | |
2494 | { | |
2495 | VEC (thread_item_t) *items; | |
2496 | }; | |
2497 | ||
2498 | static void | |
2499 | start_thread (struct gdb_xml_parser *parser, | |
2500 | const struct gdb_xml_element *element, | |
2501 | void *user_data, VEC(gdb_xml_value_s) *attributes) | |
2502 | { | |
2503 | struct threads_parsing_context *data = user_data; | |
2504 | ||
2505 | struct thread_item item; | |
2506 | char *id; | |
2507 | ||
2508 | id = VEC_index (gdb_xml_value_s, attributes, 0)->value; | |
2509 | item.ptid = read_ptid (id, NULL); | |
2510 | ||
2511 | if (VEC_length (gdb_xml_value_s, attributes) > 1) | |
2512 | item.core = *(ULONGEST *) VEC_index (gdb_xml_value_s, attributes, 1)->value; | |
2513 | else | |
2514 | item.core = -1; | |
2515 | ||
2516 | item.extra = 0; | |
2517 | ||
2518 | VEC_safe_push (thread_item_t, data->items, &item); | |
2519 | } | |
2520 | ||
2521 | static void | |
2522 | end_thread (struct gdb_xml_parser *parser, | |
2523 | const struct gdb_xml_element *element, | |
2524 | void *user_data, const char *body_text) | |
2525 | { | |
2526 | struct threads_parsing_context *data = user_data; | |
2527 | ||
2528 | if (body_text && *body_text) | |
2ae2a0b7 | 2529 | VEC_last (thread_item_t, data->items)->extra = xstrdup (body_text); |
dc146f7c VP |
2530 | } |
2531 | ||
2532 | const struct gdb_xml_attribute thread_attributes[] = { | |
2533 | { "id", GDB_XML_AF_NONE, NULL, NULL }, | |
2534 | { "core", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL }, | |
2535 | { NULL, GDB_XML_AF_NONE, NULL, NULL } | |
2536 | }; | |
2537 | ||
2538 | const struct gdb_xml_element thread_children[] = { | |
2539 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } | |
2540 | }; | |
2541 | ||
2542 | const struct gdb_xml_element threads_children[] = { | |
2543 | { "thread", thread_attributes, thread_children, | |
2544 | GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL, | |
2545 | start_thread, end_thread }, | |
2546 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } | |
2547 | }; | |
2548 | ||
2549 | const struct gdb_xml_element threads_elements[] = { | |
2550 | { "threads", NULL, threads_children, | |
2551 | GDB_XML_EF_NONE, NULL, NULL }, | |
2552 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } | |
2553 | }; | |
2554 | ||
02357a4a PA |
2555 | /* Discard the contents of the constructed thread info context. */ |
2556 | ||
2557 | static void | |
2558 | clear_threads_parsing_context (void *p) | |
2559 | { | |
2560 | struct threads_parsing_context *context = p; | |
2561 | int i; | |
2562 | struct thread_item *item; | |
2563 | ||
2564 | for (i = 0; VEC_iterate (thread_item_t, context->items, i, item); ++i) | |
2565 | xfree (item->extra); | |
2566 | ||
2567 | VEC_free (thread_item_t, context->items); | |
2568 | } | |
2569 | ||
dc146f7c VP |
2570 | #endif |
2571 | ||
9d1f7ab2 MS |
2572 | /* |
2573 | * Find all threads for info threads command. | |
2574 | * Uses new thread protocol contributed by Cisco. | |
2575 | * Falls back and attempts to use the older method (above) | |
2576 | * if the target doesn't respond to the new method. | |
2577 | */ | |
2578 | ||
0f71a2f6 | 2579 | static void |
28439f5e | 2580 | remote_threads_info (struct target_ops *ops) |
0f71a2f6 | 2581 | { |
d01949b6 | 2582 | struct remote_state *rs = get_remote_state (); |
085dd6e6 | 2583 | char *bufp; |
79d7f229 | 2584 | ptid_t new_thread; |
0f71a2f6 JM |
2585 | |
2586 | if (remote_desc == 0) /* paranoia */ | |
8a3fe4f8 | 2587 | error (_("Command can only be used when connected to the remote target.")); |
0f71a2f6 | 2588 | |
dc146f7c VP |
2589 | #if defined(HAVE_LIBEXPAT) |
2590 | if (remote_protocol_packets[PACKET_qXfer_threads].support == PACKET_ENABLE) | |
2591 | { | |
2592 | char *xml = target_read_stralloc (¤t_target, | |
2593 | TARGET_OBJECT_THREADS, NULL); | |
2594 | ||
2595 | struct cleanup *back_to = make_cleanup (xfree, xml); | |
2596 | if (xml && *xml) | |
2597 | { | |
2598 | struct gdb_xml_parser *parser; | |
2599 | struct threads_parsing_context context; | |
02357a4a | 2600 | struct cleanup *clear_parsing_context; |
dc146f7c VP |
2601 | |
2602 | context.items = 0; | |
02357a4a PA |
2603 | /* Note: this parser cleanup is already guarded by BACK_TO |
2604 | above. */ | |
dc146f7c VP |
2605 | parser = gdb_xml_create_parser_and_cleanup (_("threads"), |
2606 | threads_elements, | |
2607 | &context); | |
2608 | ||
2609 | gdb_xml_use_dtd (parser, "threads.dtd"); | |
2610 | ||
02357a4a PA |
2611 | clear_parsing_context |
2612 | = make_cleanup (clear_threads_parsing_context, &context); | |
2613 | ||
dc146f7c VP |
2614 | if (gdb_xml_parse (parser, xml) == 0) |
2615 | { | |
2616 | int i; | |
2617 | struct thread_item *item; | |
2618 | ||
2619 | for (i = 0; VEC_iterate (thread_item_t, context.items, i, item); ++i) | |
2620 | { | |
2621 | if (!ptid_equal (item->ptid, null_ptid)) | |
2622 | { | |
2623 | struct private_thread_info *info; | |
2624 | /* In non-stop mode, we assume new found threads | |
2625 | are running until proven otherwise with a | |
2626 | stop reply. In all-stop, we can only get | |
2627 | here if all threads are stopped. */ | |
2628 | int running = non_stop ? 1 : 0; | |
2629 | ||
2630 | remote_notice_new_inferior (item->ptid, running); | |
2631 | ||
2632 | info = demand_private_info (item->ptid); | |
2633 | info->core = item->core; | |
2634 | info->extra = item->extra; | |
02357a4a | 2635 | item->extra = NULL; |
dc146f7c | 2636 | } |
dc146f7c VP |
2637 | } |
2638 | } | |
2639 | ||
02357a4a | 2640 | do_cleanups (clear_parsing_context); |
dc146f7c VP |
2641 | } |
2642 | ||
2643 | do_cleanups (back_to); | |
2644 | return; | |
2645 | } | |
2646 | #endif | |
2647 | ||
9d1f7ab2 MS |
2648 | if (use_threadinfo_query) |
2649 | { | |
2650 | putpkt ("qfThreadInfo"); | |
6d820c5c | 2651 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 2652 | bufp = rs->buf; |
9d1f7ab2 | 2653 | if (bufp[0] != '\0') /* q packet recognized */ |
802188a7 | 2654 | { |
9d1f7ab2 MS |
2655 | while (*bufp++ == 'm') /* reply contains one or more TID */ |
2656 | { | |
2657 | do | |
2658 | { | |
82f73884 | 2659 | new_thread = read_ptid (bufp, &bufp); |
1941c569 | 2660 | if (!ptid_equal (new_thread, null_ptid)) |
82f73884 | 2661 | { |
74531fed | 2662 | /* In non-stop mode, we assume new found threads |
1941c569 | 2663 | are running until proven otherwise with a |
74531fed PA |
2664 | stop reply. In all-stop, we can only get |
2665 | here if all threads are stopped. */ | |
1941c569 PA |
2666 | int running = non_stop ? 1 : 0; |
2667 | ||
2668 | remote_notice_new_inferior (new_thread, running); | |
82f73884 | 2669 | } |
9d1f7ab2 MS |
2670 | } |
2671 | while (*bufp++ == ','); /* comma-separated list */ | |
2672 | putpkt ("qsThreadInfo"); | |
6d820c5c | 2673 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 2674 | bufp = rs->buf; |
9d1f7ab2 MS |
2675 | } |
2676 | return; /* done */ | |
2677 | } | |
2678 | } | |
2679 | ||
74531fed PA |
2680 | /* Only qfThreadInfo is supported in non-stop mode. */ |
2681 | if (non_stop) | |
2682 | return; | |
2683 | ||
23860348 | 2684 | /* Else fall back to old method based on jmetzler protocol. */ |
9d1f7ab2 MS |
2685 | use_threadinfo_query = 0; |
2686 | remote_find_new_threads (); | |
2687 | return; | |
2688 | } | |
2689 | ||
802188a7 | 2690 | /* |
9d1f7ab2 MS |
2691 | * Collect a descriptive string about the given thread. |
2692 | * The target may say anything it wants to about the thread | |
2693 | * (typically info about its blocked / runnable state, name, etc.). | |
2694 | * This string will appear in the info threads display. | |
802188a7 | 2695 | * |
9d1f7ab2 MS |
2696 | * Optional: targets are not required to implement this function. |
2697 | */ | |
2698 | ||
2699 | static char * | |
2700 | remote_threads_extra_info (struct thread_info *tp) | |
2701 | { | |
d01949b6 | 2702 | struct remote_state *rs = get_remote_state (); |
9d1f7ab2 MS |
2703 | int result; |
2704 | int set; | |
2705 | threadref id; | |
2706 | struct gdb_ext_thread_info threadinfo; | |
23860348 | 2707 | static char display_buf[100]; /* arbitrary... */ |
9d1f7ab2 MS |
2708 | int n = 0; /* position in display_buf */ |
2709 | ||
2710 | if (remote_desc == 0) /* paranoia */ | |
8e65ff28 | 2711 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 2712 | _("remote_threads_extra_info")); |
9d1f7ab2 | 2713 | |
60e569b9 PA |
2714 | if (ptid_equal (tp->ptid, magic_null_ptid) |
2715 | || (ptid_get_pid (tp->ptid) != 0 && ptid_get_tid (tp->ptid) == 0)) | |
2716 | /* This is the main thread which was added by GDB. The remote | |
2717 | server doesn't know about it. */ | |
2718 | return NULL; | |
2719 | ||
dc146f7c VP |
2720 | if (remote_protocol_packets[PACKET_qXfer_threads].support == PACKET_ENABLE) |
2721 | { | |
2722 | struct thread_info *info = find_thread_ptid (tp->ptid); | |
a744cf53 | 2723 | |
dc146f7c VP |
2724 | if (info && info->private) |
2725 | return info->private->extra; | |
2726 | else | |
2727 | return NULL; | |
2728 | } | |
2729 | ||
9d1f7ab2 MS |
2730 | if (use_threadextra_query) |
2731 | { | |
82f73884 PA |
2732 | char *b = rs->buf; |
2733 | char *endb = rs->buf + get_remote_packet_size (); | |
2734 | ||
2735 | xsnprintf (b, endb - b, "qThreadExtraInfo,"); | |
2736 | b += strlen (b); | |
2737 | write_ptid (b, endb, tp->ptid); | |
2738 | ||
2e9f7625 | 2739 | putpkt (rs->buf); |
6d820c5c | 2740 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 2741 | if (rs->buf[0] != 0) |
9d1f7ab2 | 2742 | { |
2e9f7625 DJ |
2743 | n = min (strlen (rs->buf) / 2, sizeof (display_buf)); |
2744 | result = hex2bin (rs->buf, (gdb_byte *) display_buf, n); | |
30559e10 | 2745 | display_buf [result] = '\0'; |
9d1f7ab2 MS |
2746 | return display_buf; |
2747 | } | |
0f71a2f6 | 2748 | } |
9d1f7ab2 MS |
2749 | |
2750 | /* If the above query fails, fall back to the old method. */ | |
2751 | use_threadextra_query = 0; | |
2752 | set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME | |
2753 | | TAG_MOREDISPLAY | TAG_DISPLAY; | |
79d7f229 | 2754 | int_to_threadref (&id, ptid_get_tid (tp->ptid)); |
9d1f7ab2 MS |
2755 | if (remote_get_threadinfo (&id, set, &threadinfo)) |
2756 | if (threadinfo.active) | |
0f71a2f6 | 2757 | { |
9d1f7ab2 | 2758 | if (*threadinfo.shortname) |
2bc416ba | 2759 | n += xsnprintf (&display_buf[0], sizeof (display_buf) - n, |
ecbc58df | 2760 | " Name: %s,", threadinfo.shortname); |
9d1f7ab2 | 2761 | if (*threadinfo.display) |
2bc416ba | 2762 | n += xsnprintf (&display_buf[n], sizeof (display_buf) - n, |
ecbc58df | 2763 | " State: %s,", threadinfo.display); |
9d1f7ab2 | 2764 | if (*threadinfo.more_display) |
2bc416ba | 2765 | n += xsnprintf (&display_buf[n], sizeof (display_buf) - n, |
ecbc58df | 2766 | " Priority: %s", threadinfo.more_display); |
9d1f7ab2 MS |
2767 | |
2768 | if (n > 0) | |
c5aa993b | 2769 | { |
23860348 | 2770 | /* For purely cosmetic reasons, clear up trailing commas. */ |
9d1f7ab2 MS |
2771 | if (',' == display_buf[n-1]) |
2772 | display_buf[n-1] = ' '; | |
2773 | return display_buf; | |
c5aa993b | 2774 | } |
0f71a2f6 | 2775 | } |
9d1f7ab2 | 2776 | return NULL; |
0f71a2f6 | 2777 | } |
c906108c | 2778 | \f |
c5aa993b | 2779 | |
0fb4aa4b PA |
2780 | static int |
2781 | remote_static_tracepoint_marker_at (CORE_ADDR addr, | |
2782 | struct static_tracepoint_marker *marker) | |
2783 | { | |
2784 | struct remote_state *rs = get_remote_state (); | |
2785 | char *p = rs->buf; | |
2786 | ||
2787 | sprintf (p, "qTSTMat:"); | |
2788 | p += strlen (p); | |
2789 | p += hexnumstr (p, addr); | |
2790 | putpkt (rs->buf); | |
2791 | getpkt (&rs->buf, &rs->buf_size, 0); | |
2792 | p = rs->buf; | |
2793 | ||
2794 | if (*p == 'E') | |
2795 | error (_("Remote failure reply: %s"), p); | |
2796 | ||
2797 | if (*p++ == 'm') | |
2798 | { | |
2799 | parse_static_tracepoint_marker_definition (p, &p, marker); | |
2800 | return 1; | |
2801 | } | |
2802 | ||
2803 | return 0; | |
2804 | } | |
2805 | ||
2806 | static void | |
2807 | free_current_marker (void *arg) | |
2808 | { | |
2809 | struct static_tracepoint_marker **marker_p = arg; | |
2810 | ||
2811 | if (*marker_p != NULL) | |
2812 | { | |
2813 | release_static_tracepoint_marker (*marker_p); | |
2814 | xfree (*marker_p); | |
2815 | } | |
2816 | else | |
2817 | *marker_p = NULL; | |
2818 | } | |
2819 | ||
2820 | static VEC(static_tracepoint_marker_p) * | |
2821 | remote_static_tracepoint_markers_by_strid (const char *strid) | |
2822 | { | |
2823 | struct remote_state *rs = get_remote_state (); | |
2824 | VEC(static_tracepoint_marker_p) *markers = NULL; | |
2825 | struct static_tracepoint_marker *marker = NULL; | |
2826 | struct cleanup *old_chain; | |
2827 | char *p; | |
2828 | ||
2829 | /* Ask for a first packet of static tracepoint marker | |
2830 | definition. */ | |
2831 | putpkt ("qTfSTM"); | |
2832 | getpkt (&rs->buf, &rs->buf_size, 0); | |
2833 | p = rs->buf; | |
2834 | if (*p == 'E') | |
2835 | error (_("Remote failure reply: %s"), p); | |
2836 | ||
2837 | old_chain = make_cleanup (free_current_marker, &marker); | |
2838 | ||
2839 | while (*p++ == 'm') | |
2840 | { | |
2841 | if (marker == NULL) | |
2842 | marker = XCNEW (struct static_tracepoint_marker); | |
2843 | ||
2844 | do | |
2845 | { | |
2846 | parse_static_tracepoint_marker_definition (p, &p, marker); | |
2847 | ||
2848 | if (strid == NULL || strcmp (strid, marker->str_id) == 0) | |
2849 | { | |
2850 | VEC_safe_push (static_tracepoint_marker_p, | |
2851 | markers, marker); | |
2852 | marker = NULL; | |
2853 | } | |
2854 | else | |
2855 | { | |
2856 | release_static_tracepoint_marker (marker); | |
2857 | memset (marker, 0, sizeof (*marker)); | |
2858 | } | |
2859 | } | |
2860 | while (*p++ == ','); /* comma-separated list */ | |
2861 | /* Ask for another packet of static tracepoint definition. */ | |
2862 | putpkt ("qTsSTM"); | |
2863 | getpkt (&rs->buf, &rs->buf_size, 0); | |
2864 | p = rs->buf; | |
2865 | } | |
2866 | ||
2867 | do_cleanups (old_chain); | |
2868 | return markers; | |
2869 | } | |
2870 | ||
2871 | \f | |
10760264 JB |
2872 | /* Implement the to_get_ada_task_ptid function for the remote targets. */ |
2873 | ||
2874 | static ptid_t | |
2875 | remote_get_ada_task_ptid (long lwp, long thread) | |
2876 | { | |
2877 | return ptid_build (ptid_get_pid (inferior_ptid), 0, lwp); | |
2878 | } | |
2879 | \f | |
2880 | ||
24b06219 | 2881 | /* Restart the remote side; this is an extended protocol operation. */ |
c906108c SS |
2882 | |
2883 | static void | |
fba45db2 | 2884 | extended_remote_restart (void) |
c906108c | 2885 | { |
d01949b6 | 2886 | struct remote_state *rs = get_remote_state (); |
c906108c SS |
2887 | |
2888 | /* Send the restart command; for reasons I don't understand the | |
2889 | remote side really expects a number after the "R". */ | |
ea9c271d | 2890 | xsnprintf (rs->buf, get_remote_packet_size (), "R%x", 0); |
6d820c5c | 2891 | putpkt (rs->buf); |
c906108c | 2892 | |
ad9a8f3f | 2893 | remote_fileio_reset (); |
c906108c SS |
2894 | } |
2895 | \f | |
2896 | /* Clean up connection to a remote debugger. */ | |
2897 | ||
c906108c | 2898 | static void |
fba45db2 | 2899 | remote_close (int quitting) |
c906108c | 2900 | { |
d3fd5342 PA |
2901 | if (remote_desc == NULL) |
2902 | return; /* already closed */ | |
2903 | ||
2904 | /* Make sure we leave stdin registered in the event loop, and we | |
2905 | don't leave the async SIGINT signal handler installed. */ | |
2906 | remote_terminal_ours (); | |
ce5ce7ed | 2907 | |
d3fd5342 PA |
2908 | serial_close (remote_desc); |
2909 | remote_desc = NULL; | |
ce5ce7ed PA |
2910 | |
2911 | /* We don't have a connection to the remote stub anymore. Get rid | |
2912 | of all the inferiors and their threads we were controlling. */ | |
2913 | discard_all_inferiors (); | |
0f2caa1b | 2914 | inferior_ptid = null_ptid; |
ce5ce7ed | 2915 | |
74531fed PA |
2916 | /* We're no longer interested in any of these events. */ |
2917 | discard_pending_stop_replies (-1); | |
2918 | ||
2919 | if (remote_async_inferior_event_token) | |
2920 | delete_async_event_handler (&remote_async_inferior_event_token); | |
2921 | if (remote_async_get_pending_events_token) | |
2922 | delete_async_event_handler (&remote_async_get_pending_events_token); | |
c906108c SS |
2923 | } |
2924 | ||
23860348 | 2925 | /* Query the remote side for the text, data and bss offsets. */ |
c906108c SS |
2926 | |
2927 | static void | |
fba45db2 | 2928 | get_offsets (void) |
c906108c | 2929 | { |
d01949b6 | 2930 | struct remote_state *rs = get_remote_state (); |
2e9f7625 | 2931 | char *buf; |
085dd6e6 | 2932 | char *ptr; |
31d99776 DJ |
2933 | int lose, num_segments = 0, do_sections, do_segments; |
2934 | CORE_ADDR text_addr, data_addr, bss_addr, segments[2]; | |
c906108c | 2935 | struct section_offsets *offs; |
31d99776 DJ |
2936 | struct symfile_segment_data *data; |
2937 | ||
2938 | if (symfile_objfile == NULL) | |
2939 | return; | |
c906108c SS |
2940 | |
2941 | putpkt ("qOffsets"); | |
6d820c5c | 2942 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 2943 | buf = rs->buf; |
c906108c SS |
2944 | |
2945 | if (buf[0] == '\000') | |
2946 | return; /* Return silently. Stub doesn't support | |
23860348 | 2947 | this command. */ |
c906108c SS |
2948 | if (buf[0] == 'E') |
2949 | { | |
8a3fe4f8 | 2950 | warning (_("Remote failure reply: %s"), buf); |
c906108c SS |
2951 | return; |
2952 | } | |
2953 | ||
2954 | /* Pick up each field in turn. This used to be done with scanf, but | |
2955 | scanf will make trouble if CORE_ADDR size doesn't match | |
2956 | conversion directives correctly. The following code will work | |
2957 | with any size of CORE_ADDR. */ | |
2958 | text_addr = data_addr = bss_addr = 0; | |
2959 | ptr = buf; | |
2960 | lose = 0; | |
2961 | ||
2962 | if (strncmp (ptr, "Text=", 5) == 0) | |
2963 | { | |
2964 | ptr += 5; | |
2965 | /* Don't use strtol, could lose on big values. */ | |
2966 | while (*ptr && *ptr != ';') | |
2967 | text_addr = (text_addr << 4) + fromhex (*ptr++); | |
c906108c | 2968 | |
31d99776 DJ |
2969 | if (strncmp (ptr, ";Data=", 6) == 0) |
2970 | { | |
2971 | ptr += 6; | |
2972 | while (*ptr && *ptr != ';') | |
2973 | data_addr = (data_addr << 4) + fromhex (*ptr++); | |
2974 | } | |
2975 | else | |
2976 | lose = 1; | |
2977 | ||
2978 | if (!lose && strncmp (ptr, ";Bss=", 5) == 0) | |
2979 | { | |
2980 | ptr += 5; | |
2981 | while (*ptr && *ptr != ';') | |
2982 | bss_addr = (bss_addr << 4) + fromhex (*ptr++); | |
c906108c | 2983 | |
31d99776 DJ |
2984 | if (bss_addr != data_addr) |
2985 | warning (_("Target reported unsupported offsets: %s"), buf); | |
2986 | } | |
2987 | else | |
2988 | lose = 1; | |
2989 | } | |
2990 | else if (strncmp (ptr, "TextSeg=", 8) == 0) | |
c906108c | 2991 | { |
31d99776 DJ |
2992 | ptr += 8; |
2993 | /* Don't use strtol, could lose on big values. */ | |
c906108c | 2994 | while (*ptr && *ptr != ';') |
31d99776 DJ |
2995 | text_addr = (text_addr << 4) + fromhex (*ptr++); |
2996 | num_segments = 1; | |
2997 | ||
2998 | if (strncmp (ptr, ";DataSeg=", 9) == 0) | |
2999 | { | |
3000 | ptr += 9; | |
3001 | while (*ptr && *ptr != ';') | |
3002 | data_addr = (data_addr << 4) + fromhex (*ptr++); | |
3003 | num_segments++; | |
3004 | } | |
c906108c SS |
3005 | } |
3006 | else | |
3007 | lose = 1; | |
3008 | ||
3009 | if (lose) | |
8a3fe4f8 | 3010 | error (_("Malformed response to offset query, %s"), buf); |
31d99776 DJ |
3011 | else if (*ptr != '\0') |
3012 | warning (_("Target reported unsupported offsets: %s"), buf); | |
c906108c | 3013 | |
802188a7 | 3014 | offs = ((struct section_offsets *) |
a39a16c4 | 3015 | alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections))); |
802188a7 | 3016 | memcpy (offs, symfile_objfile->section_offsets, |
a39a16c4 | 3017 | SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections)); |
c906108c | 3018 | |
31d99776 DJ |
3019 | data = get_symfile_segment_data (symfile_objfile->obfd); |
3020 | do_segments = (data != NULL); | |
3021 | do_sections = num_segments == 0; | |
c906108c | 3022 | |
28c32713 | 3023 | if (num_segments > 0) |
31d99776 | 3024 | { |
31d99776 DJ |
3025 | segments[0] = text_addr; |
3026 | segments[1] = data_addr; | |
3027 | } | |
28c32713 JB |
3028 | /* If we have two segments, we can still try to relocate everything |
3029 | by assuming that the .text and .data offsets apply to the whole | |
3030 | text and data segments. Convert the offsets given in the packet | |
3031 | to base addresses for symfile_map_offsets_to_segments. */ | |
3032 | else if (data && data->num_segments == 2) | |
3033 | { | |
3034 | segments[0] = data->segment_bases[0] + text_addr; | |
3035 | segments[1] = data->segment_bases[1] + data_addr; | |
3036 | num_segments = 2; | |
3037 | } | |
8d385431 DJ |
3038 | /* If the object file has only one segment, assume that it is text |
3039 | rather than data; main programs with no writable data are rare, | |
3040 | but programs with no code are useless. Of course the code might | |
3041 | have ended up in the data segment... to detect that we would need | |
3042 | the permissions here. */ | |
3043 | else if (data && data->num_segments == 1) | |
3044 | { | |
3045 | segments[0] = data->segment_bases[0] + text_addr; | |
3046 | num_segments = 1; | |
3047 | } | |
28c32713 JB |
3048 | /* There's no way to relocate by segment. */ |
3049 | else | |
3050 | do_segments = 0; | |
31d99776 DJ |
3051 | |
3052 | if (do_segments) | |
3053 | { | |
3054 | int ret = symfile_map_offsets_to_segments (symfile_objfile->obfd, data, | |
3055 | offs, num_segments, segments); | |
3056 | ||
3057 | if (ret == 0 && !do_sections) | |
3058 | error (_("Can not handle qOffsets TextSeg response with this symbol file")); | |
3059 | ||
3060 | if (ret > 0) | |
3061 | do_sections = 0; | |
3062 | } | |
c906108c | 3063 | |
9ef895d6 DJ |
3064 | if (data) |
3065 | free_symfile_segment_data (data); | |
31d99776 DJ |
3066 | |
3067 | if (do_sections) | |
3068 | { | |
3069 | offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr; | |
3070 | ||
3071 | /* This is a temporary kludge to force data and bss to use the same offsets | |
3072 | because that's what nlmconv does now. The real solution requires changes | |
3073 | to the stub and remote.c that I don't have time to do right now. */ | |
3074 | ||
3075 | offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr; | |
3076 | offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr; | |
3077 | } | |
c906108c SS |
3078 | |
3079 | objfile_relocate (symfile_objfile, offs); | |
3080 | } | |
3081 | ||
74531fed PA |
3082 | /* Callback for iterate_over_threads. Set the STOP_REQUESTED flags in |
3083 | threads we know are stopped already. This is used during the | |
3084 | initial remote connection in non-stop mode --- threads that are | |
3085 | reported as already being stopped are left stopped. */ | |
3086 | ||
3087 | static int | |
3088 | set_stop_requested_callback (struct thread_info *thread, void *data) | |
3089 | { | |
3090 | /* If we have a stop reply for this thread, it must be stopped. */ | |
3091 | if (peek_stop_reply (thread->ptid)) | |
3092 | set_stop_requested (thread->ptid, 1); | |
3093 | ||
3094 | return 0; | |
3095 | } | |
3096 | ||
8621d6a9 | 3097 | /* Stub for catch_exception. */ |
0f71a2f6 | 3098 | |
2d717e4f DJ |
3099 | struct start_remote_args |
3100 | { | |
3101 | int from_tty; | |
3102 | ||
3103 | /* The current target. */ | |
3104 | struct target_ops *target; | |
3105 | ||
3106 | /* Non-zero if this is an extended-remote target. */ | |
3107 | int extended_p; | |
3108 | }; | |
3109 | ||
9a7071a8 JB |
3110 | /* Send interrupt_sequence to remote target. */ |
3111 | static void | |
3112 | send_interrupt_sequence () | |
3113 | { | |
3114 | if (interrupt_sequence_mode == interrupt_sequence_control_c) | |
3115 | serial_write (remote_desc, "\x03", 1); | |
3116 | else if (interrupt_sequence_mode == interrupt_sequence_break) | |
3117 | serial_send_break (remote_desc); | |
3118 | else if (interrupt_sequence_mode == interrupt_sequence_break_g) | |
3119 | { | |
3120 | serial_send_break (remote_desc); | |
3121 | serial_write (remote_desc, "g", 1); | |
3122 | } | |
3123 | else | |
3124 | internal_error (__FILE__, __LINE__, | |
3125 | _("Invalid value for interrupt_sequence_mode: %s."), | |
3126 | interrupt_sequence_mode); | |
3127 | } | |
3128 | ||
9cbc821d | 3129 | static void |
2d717e4f | 3130 | remote_start_remote (struct ui_out *uiout, void *opaque) |
c906108c | 3131 | { |
2d717e4f | 3132 | struct start_remote_args *args = opaque; |
c8d104ad PA |
3133 | struct remote_state *rs = get_remote_state (); |
3134 | struct packet_config *noack_config; | |
2d717e4f | 3135 | char *wait_status = NULL; |
8621d6a9 | 3136 | |
23860348 | 3137 | immediate_quit++; /* Allow user to interrupt it. */ |
c906108c | 3138 | |
c8d104ad PA |
3139 | /* Ack any packet which the remote side has already sent. */ |
3140 | serial_write (remote_desc, "+", 1); | |
3141 | ||
9a7071a8 JB |
3142 | if (interrupt_on_connect) |
3143 | send_interrupt_sequence (); | |
3144 | ||
c8d104ad PA |
3145 | /* The first packet we send to the target is the optional "supported |
3146 | packets" request. If the target can answer this, it will tell us | |
3147 | which later probes to skip. */ | |
3148 | remote_query_supported (); | |
3149 | ||
d914c394 SS |
3150 | /* If the stub wants to get a QAllow, compose one and send it. */ |
3151 | if (remote_protocol_packets[PACKET_QAllow].support != PACKET_DISABLE) | |
3152 | remote_set_permissions (); | |
3153 | ||
c8d104ad PA |
3154 | /* Next, we possibly activate noack mode. |
3155 | ||
3156 | If the QStartNoAckMode packet configuration is set to AUTO, | |
3157 | enable noack mode if the stub reported a wish for it with | |
3158 | qSupported. | |
3159 | ||
3160 | If set to TRUE, then enable noack mode even if the stub didn't | |
3161 | report it in qSupported. If the stub doesn't reply OK, the | |
3162 | session ends with an error. | |
3163 | ||
3164 | If FALSE, then don't activate noack mode, regardless of what the | |
3165 | stub claimed should be the default with qSupported. */ | |
3166 | ||
3167 | noack_config = &remote_protocol_packets[PACKET_QStartNoAckMode]; | |
3168 | ||
3169 | if (noack_config->detect == AUTO_BOOLEAN_TRUE | |
3170 | || (noack_config->detect == AUTO_BOOLEAN_AUTO | |
3171 | && noack_config->support == PACKET_ENABLE)) | |
3172 | { | |
3173 | putpkt ("QStartNoAckMode"); | |
3174 | getpkt (&rs->buf, &rs->buf_size, 0); | |
3175 | if (packet_ok (rs->buf, noack_config) == PACKET_OK) | |
3176 | rs->noack_mode = 1; | |
3177 | } | |
3178 | ||
5fe04517 PA |
3179 | if (args->extended_p) |
3180 | { | |
3181 | /* Tell the remote that we are using the extended protocol. */ | |
3182 | putpkt ("!"); | |
3183 | getpkt (&rs->buf, &rs->buf_size, 0); | |
3184 | } | |
3185 | ||
d962ef82 DJ |
3186 | /* Next, if the target can specify a description, read it. We do |
3187 | this before anything involving memory or registers. */ | |
3188 | target_find_description (); | |
3189 | ||
6c95b8df PA |
3190 | /* Next, now that we know something about the target, update the |
3191 | address spaces in the program spaces. */ | |
3192 | update_address_spaces (); | |
3193 | ||
50c71eaf PA |
3194 | /* On OSs where the list of libraries is global to all |
3195 | processes, we fetch them early. */ | |
3196 | if (gdbarch_has_global_solist (target_gdbarch)) | |
3197 | solib_add (NULL, args->from_tty, args->target, auto_solib_add); | |
3198 | ||
74531fed PA |
3199 | if (non_stop) |
3200 | { | |
3201 | if (!rs->non_stop_aware) | |
3202 | error (_("Non-stop mode requested, but remote does not support non-stop")); | |
3203 | ||
3204 | putpkt ("QNonStop:1"); | |
3205 | getpkt (&rs->buf, &rs->buf_size, 0); | |
3206 | ||
3207 | if (strcmp (rs->buf, "OK") != 0) | |
3208 | error ("Remote refused setting non-stop mode with: %s", rs->buf); | |
3209 | ||
3210 | /* Find about threads and processes the stub is already | |
3211 | controlling. We default to adding them in the running state. | |
3212 | The '?' query below will then tell us about which threads are | |
3213 | stopped. */ | |
28439f5e | 3214 | remote_threads_info (args->target); |
74531fed PA |
3215 | } |
3216 | else if (rs->non_stop_aware) | |
3217 | { | |
3218 | /* Don't assume that the stub can operate in all-stop mode. | |
3219 | Request it explicitely. */ | |
3220 | putpkt ("QNonStop:0"); | |
3221 | getpkt (&rs->buf, &rs->buf_size, 0); | |
3222 | ||
3223 | if (strcmp (rs->buf, "OK") != 0) | |
3224 | error ("Remote refused setting all-stop mode with: %s", rs->buf); | |
3225 | } | |
3226 | ||
2d717e4f DJ |
3227 | /* Check whether the target is running now. */ |
3228 | putpkt ("?"); | |
3229 | getpkt (&rs->buf, &rs->buf_size, 0); | |
3230 | ||
74531fed | 3231 | if (!non_stop) |
2d717e4f | 3232 | { |
74531fed | 3233 | if (rs->buf[0] == 'W' || rs->buf[0] == 'X') |
2d717e4f | 3234 | { |
c35b1492 | 3235 | if (!args->extended_p) |
74531fed | 3236 | error (_("The target is not running (try extended-remote?)")); |
c35b1492 PA |
3237 | |
3238 | /* We're connected, but not running. Drop out before we | |
3239 | call start_remote. */ | |
3240 | return; | |
2d717e4f DJ |
3241 | } |
3242 | else | |
74531fed | 3243 | { |
74531fed PA |
3244 | /* Save the reply for later. */ |
3245 | wait_status = alloca (strlen (rs->buf) + 1); | |
3246 | strcpy (wait_status, rs->buf); | |
3247 | } | |
3248 | ||
3249 | /* Let the stub know that we want it to return the thread. */ | |
3250 | set_continue_thread (minus_one_ptid); | |
3251 | ||
3252 | /* Without this, some commands which require an active target | |
3253 | (such as kill) won't work. This variable serves (at least) | |
3254 | double duty as both the pid of the target process (if it has | |
3255 | such), and as a flag indicating that a target is active. | |
3256 | These functions should be split out into seperate variables, | |
3257 | especially since GDB will someday have a notion of debugging | |
3258 | several processes. */ | |
3259 | inferior_ptid = magic_null_ptid; | |
3260 | ||
3261 | /* Now, if we have thread information, update inferior_ptid. */ | |
3262 | inferior_ptid = remote_current_thread (inferior_ptid); | |
3263 | ||
0b16c5cf | 3264 | remote_add_inferior (ptid_get_pid (inferior_ptid), -1); |
74531fed PA |
3265 | |
3266 | /* Always add the main thread. */ | |
3267 | add_thread_silent (inferior_ptid); | |
3268 | ||
3269 | get_offsets (); /* Get text, data & bss offsets. */ | |
3270 | ||
d962ef82 DJ |
3271 | /* If we could not find a description using qXfer, and we know |
3272 | how to do it some other way, try again. This is not | |
3273 | supported for non-stop; it could be, but it is tricky if | |
3274 | there are no stopped threads when we connect. */ | |
3275 | if (remote_read_description_p (args->target) | |
3276 | && gdbarch_target_desc (target_gdbarch) == NULL) | |
3277 | { | |
3278 | target_clear_description (); | |
3279 | target_find_description (); | |
3280 | } | |
3281 | ||
74531fed PA |
3282 | /* Use the previously fetched status. */ |
3283 | gdb_assert (wait_status != NULL); | |
3284 | strcpy (rs->buf, wait_status); | |
3285 | rs->cached_wait_status = 1; | |
3286 | ||
3287 | immediate_quit--; | |
3288 | start_remote (args->from_tty); /* Initialize gdb process mechanisms. */ | |
2d717e4f DJ |
3289 | } |
3290 | else | |
3291 | { | |
68c97600 PA |
3292 | /* Clear WFI global state. Do this before finding about new |
3293 | threads and inferiors, and setting the current inferior. | |
3294 | Otherwise we would clear the proceed status of the current | |
3295 | inferior when we want its stop_soon state to be preserved | |
3296 | (see notice_new_inferior). */ | |
3297 | init_wait_for_inferior (); | |
3298 | ||
74531fed PA |
3299 | /* In non-stop, we will either get an "OK", meaning that there |
3300 | are no stopped threads at this time; or, a regular stop | |
3301 | reply. In the latter case, there may be more than one thread | |
3302 | stopped --- we pull them all out using the vStopped | |
3303 | mechanism. */ | |
3304 | if (strcmp (rs->buf, "OK") != 0) | |
3305 | { | |
3306 | struct stop_reply *stop_reply; | |
3307 | struct cleanup *old_chain; | |
2d717e4f | 3308 | |
74531fed PA |
3309 | stop_reply = stop_reply_xmalloc (); |
3310 | old_chain = make_cleanup (do_stop_reply_xfree, stop_reply); | |
2d717e4f | 3311 | |
74531fed PA |
3312 | remote_parse_stop_reply (rs->buf, stop_reply); |
3313 | discard_cleanups (old_chain); | |
c0a2216e | 3314 | |
74531fed PA |
3315 | /* get_pending_stop_replies acks this one, and gets the rest |
3316 | out. */ | |
3317 | pending_stop_reply = stop_reply; | |
3318 | remote_get_pending_stop_replies (); | |
c906108c | 3319 | |
74531fed PA |
3320 | /* Make sure that threads that were stopped remain |
3321 | stopped. */ | |
3322 | iterate_over_threads (set_stop_requested_callback, NULL); | |
3323 | } | |
2d717e4f | 3324 | |
74531fed PA |
3325 | if (target_can_async_p ()) |
3326 | target_async (inferior_event_handler, 0); | |
c906108c | 3327 | |
74531fed PA |
3328 | if (thread_count () == 0) |
3329 | { | |
c35b1492 | 3330 | if (!args->extended_p) |
74531fed | 3331 | error (_("The target is not running (try extended-remote?)")); |
82f73884 | 3332 | |
c35b1492 PA |
3333 | /* We're connected, but not running. Drop out before we |
3334 | call start_remote. */ | |
3335 | return; | |
3336 | } | |
74531fed PA |
3337 | |
3338 | /* Let the stub know that we want it to return the thread. */ | |
c0a2216e | 3339 | |
74531fed PA |
3340 | /* Force the stub to choose a thread. */ |
3341 | set_general_thread (null_ptid); | |
c906108c | 3342 | |
74531fed PA |
3343 | /* Query it. */ |
3344 | inferior_ptid = remote_current_thread (minus_one_ptid); | |
3345 | if (ptid_equal (inferior_ptid, minus_one_ptid)) | |
3346 | error (_("remote didn't report the current thread in non-stop mode")); | |
c906108c | 3347 | |
74531fed PA |
3348 | get_offsets (); /* Get text, data & bss offsets. */ |
3349 | ||
3350 | /* In non-stop mode, any cached wait status will be stored in | |
3351 | the stop reply queue. */ | |
3352 | gdb_assert (wait_status == NULL); | |
f0223081 PA |
3353 | |
3354 | /* Update the remote on signals to silently pass, or more | |
3355 | importantly, which to not ignore, in case a previous session | |
3356 | had set some different set of signals to be ignored. */ | |
3357 | remote_pass_signals (); | |
74531fed | 3358 | } |
c8d104ad | 3359 | |
c8d104ad PA |
3360 | /* If we connected to a live target, do some additional setup. */ |
3361 | if (target_has_execution) | |
3362 | { | |
3363 | if (exec_bfd) /* No use without an exec file. */ | |
3364 | remote_check_symbols (symfile_objfile); | |
3365 | } | |
50c71eaf | 3366 | |
d5551862 SS |
3367 | /* Possibly the target has been engaged in a trace run started |
3368 | previously; find out where things are at. */ | |
ad91cd99 | 3369 | if (remote_get_trace_status (current_trace_status ()) != -1) |
d5551862 | 3370 | { |
00bf0b85 SS |
3371 | struct uploaded_tp *uploaded_tps = NULL; |
3372 | struct uploaded_tsv *uploaded_tsvs = NULL; | |
3373 | ||
00bf0b85 SS |
3374 | if (current_trace_status ()->running) |
3375 | printf_filtered (_("Trace is already running on the target.\n")); | |
3376 | ||
3377 | /* Get trace state variables first, they may be checked when | |
3378 | parsing uploaded commands. */ | |
3379 | ||
3380 | remote_upload_trace_state_variables (&uploaded_tsvs); | |
3381 | ||
3382 | merge_uploaded_trace_state_variables (&uploaded_tsvs); | |
3383 | ||
3384 | remote_upload_tracepoints (&uploaded_tps); | |
3385 | ||
3386 | merge_uploaded_tracepoints (&uploaded_tps); | |
d5551862 SS |
3387 | } |
3388 | ||
2567c7d9 PA |
3389 | /* If breakpoints are global, insert them now. */ |
3390 | if (gdbarch_has_global_breakpoints (target_gdbarch) | |
50c71eaf PA |
3391 | && breakpoints_always_inserted_mode ()) |
3392 | insert_breakpoints (); | |
c906108c SS |
3393 | } |
3394 | ||
3395 | /* Open a connection to a remote debugger. | |
3396 | NAME is the filename used for communication. */ | |
3397 | ||
3398 | static void | |
fba45db2 | 3399 | remote_open (char *name, int from_tty) |
c906108c | 3400 | { |
75c99385 | 3401 | remote_open_1 (name, from_tty, &remote_ops, 0); |
43ff13b4 JM |
3402 | } |
3403 | ||
c906108c SS |
3404 | /* Open a connection to a remote debugger using the extended |
3405 | remote gdb protocol. NAME is the filename used for communication. */ | |
3406 | ||
3407 | static void | |
fba45db2 | 3408 | extended_remote_open (char *name, int from_tty) |
c906108c | 3409 | { |
75c99385 | 3410 | remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */); |
43ff13b4 JM |
3411 | } |
3412 | ||
c906108c SS |
3413 | /* Generic code for opening a connection to a remote target. */ |
3414 | ||
d471ea57 AC |
3415 | static void |
3416 | init_all_packet_configs (void) | |
3417 | { | |
3418 | int i; | |
a744cf53 | 3419 | |
444abaca DJ |
3420 | for (i = 0; i < PACKET_MAX; i++) |
3421 | update_packet_config (&remote_protocol_packets[i]); | |
d471ea57 AC |
3422 | } |
3423 | ||
23860348 | 3424 | /* Symbol look-up. */ |
dc8acb97 MS |
3425 | |
3426 | static void | |
3427 | remote_check_symbols (struct objfile *objfile) | |
3428 | { | |
d01949b6 | 3429 | struct remote_state *rs = get_remote_state (); |
dc8acb97 MS |
3430 | char *msg, *reply, *tmp; |
3431 | struct minimal_symbol *sym; | |
3432 | int end; | |
3433 | ||
444abaca | 3434 | if (remote_protocol_packets[PACKET_qSymbol].support == PACKET_DISABLE) |
dc8acb97 MS |
3435 | return; |
3436 | ||
3c9c4b83 PA |
3437 | /* Make sure the remote is pointing at the right process. */ |
3438 | set_general_process (); | |
3439 | ||
6d820c5c DJ |
3440 | /* Allocate a message buffer. We can't reuse the input buffer in RS, |
3441 | because we need both at the same time. */ | |
ea9c271d | 3442 | msg = alloca (get_remote_packet_size ()); |
6d820c5c | 3443 | |
23860348 | 3444 | /* Invite target to request symbol lookups. */ |
dc8acb97 MS |
3445 | |
3446 | putpkt ("qSymbol::"); | |
6d820c5c DJ |
3447 | getpkt (&rs->buf, &rs->buf_size, 0); |
3448 | packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSymbol]); | |
2e9f7625 | 3449 | reply = rs->buf; |
dc8acb97 MS |
3450 | |
3451 | while (strncmp (reply, "qSymbol:", 8) == 0) | |
3452 | { | |
3453 | tmp = &reply[8]; | |
cfd77fa1 | 3454 | end = hex2bin (tmp, (gdb_byte *) msg, strlen (tmp) / 2); |
dc8acb97 MS |
3455 | msg[end] = '\0'; |
3456 | sym = lookup_minimal_symbol (msg, NULL, NULL); | |
3457 | if (sym == NULL) | |
ea9c271d | 3458 | xsnprintf (msg, get_remote_packet_size (), "qSymbol::%s", &reply[8]); |
dc8acb97 | 3459 | else |
2bbe3cc1 | 3460 | { |
5af949e3 | 3461 | int addr_size = gdbarch_addr_bit (target_gdbarch) / 8; |
2bbe3cc1 DJ |
3462 | CORE_ADDR sym_addr = SYMBOL_VALUE_ADDRESS (sym); |
3463 | ||
3464 | /* If this is a function address, return the start of code | |
3465 | instead of any data function descriptor. */ | |
1cf3db46 | 3466 | sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch, |
2bbe3cc1 DJ |
3467 | sym_addr, |
3468 | ¤t_target); | |
3469 | ||
3470 | xsnprintf (msg, get_remote_packet_size (), "qSymbol:%s:%s", | |
5af949e3 | 3471 | phex_nz (sym_addr, addr_size), &reply[8]); |
2bbe3cc1 DJ |
3472 | } |
3473 | ||
dc8acb97 | 3474 | putpkt (msg); |
6d820c5c | 3475 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 3476 | reply = rs->buf; |
dc8acb97 MS |
3477 | } |
3478 | } | |
3479 | ||
9db8d71f DJ |
3480 | static struct serial * |
3481 | remote_serial_open (char *name) | |
3482 | { | |
3483 | static int udp_warning = 0; | |
3484 | ||
3485 | /* FIXME: Parsing NAME here is a hack. But we want to warn here instead | |
3486 | of in ser-tcp.c, because it is the remote protocol assuming that the | |
3487 | serial connection is reliable and not the serial connection promising | |
3488 | to be. */ | |
3489 | if (!udp_warning && strncmp (name, "udp:", 4) == 0) | |
3490 | { | |
8a3fe4f8 AC |
3491 | warning (_("\ |
3492 | The remote protocol may be unreliable over UDP.\n\ | |
3493 | Some events may be lost, rendering further debugging impossible.")); | |
9db8d71f DJ |
3494 | udp_warning = 1; |
3495 | } | |
3496 | ||
3497 | return serial_open (name); | |
3498 | } | |
3499 | ||
d914c394 SS |
3500 | /* Inform the target of our permission settings. The permission flags |
3501 | work without this, but if the target knows the settings, it can do | |
3502 | a couple things. First, it can add its own check, to catch cases | |
3503 | that somehow manage to get by the permissions checks in target | |
3504 | methods. Second, if the target is wired to disallow particular | |
3505 | settings (for instance, a system in the field that is not set up to | |
3506 | be able to stop at a breakpoint), it can object to any unavailable | |
3507 | permissions. */ | |
3508 | ||
3509 | void | |
3510 | remote_set_permissions (void) | |
3511 | { | |
3512 | struct remote_state *rs = get_remote_state (); | |
3513 | ||
3514 | sprintf (rs->buf, "QAllow:" | |
3515 | "WriteReg:%x;WriteMem:%x;" | |
3516 | "InsertBreak:%x;InsertTrace:%x;" | |
3517 | "InsertFastTrace:%x;Stop:%x", | |
3518 | may_write_registers, may_write_memory, | |
3519 | may_insert_breakpoints, may_insert_tracepoints, | |
3520 | may_insert_fast_tracepoints, may_stop); | |
3521 | putpkt (rs->buf); | |
3522 | getpkt (&rs->buf, &rs->buf_size, 0); | |
3523 | ||
3524 | /* If the target didn't like the packet, warn the user. Do not try | |
3525 | to undo the user's settings, that would just be maddening. */ | |
3526 | if (strcmp (rs->buf, "OK") != 0) | |
3527 | warning ("Remote refused setting permissions with: %s", rs->buf); | |
3528 | } | |
3529 | ||
be2a5f71 DJ |
3530 | /* This type describes each known response to the qSupported |
3531 | packet. */ | |
3532 | struct protocol_feature | |
3533 | { | |
3534 | /* The name of this protocol feature. */ | |
3535 | const char *name; | |
3536 | ||
3537 | /* The default for this protocol feature. */ | |
3538 | enum packet_support default_support; | |
3539 | ||
3540 | /* The function to call when this feature is reported, or after | |
3541 | qSupported processing if the feature is not supported. | |
3542 | The first argument points to this structure. The second | |
3543 | argument indicates whether the packet requested support be | |
3544 | enabled, disabled, or probed (or the default, if this function | |
3545 | is being called at the end of processing and this feature was | |
3546 | not reported). The third argument may be NULL; if not NULL, it | |
3547 | is a NUL-terminated string taken from the packet following | |
3548 | this feature's name and an equals sign. */ | |
3549 | void (*func) (const struct protocol_feature *, enum packet_support, | |
3550 | const char *); | |
3551 | ||
3552 | /* The corresponding packet for this feature. Only used if | |
3553 | FUNC is remote_supported_packet. */ | |
3554 | int packet; | |
3555 | }; | |
3556 | ||
be2a5f71 DJ |
3557 | static void |
3558 | remote_supported_packet (const struct protocol_feature *feature, | |
3559 | enum packet_support support, | |
3560 | const char *argument) | |
3561 | { | |
3562 | if (argument) | |
3563 | { | |
3564 | warning (_("Remote qSupported response supplied an unexpected value for" | |
3565 | " \"%s\"."), feature->name); | |
3566 | return; | |
3567 | } | |
3568 | ||
3569 | if (remote_protocol_packets[feature->packet].support | |
3570 | == PACKET_SUPPORT_UNKNOWN) | |
3571 | remote_protocol_packets[feature->packet].support = support; | |
3572 | } | |
be2a5f71 DJ |
3573 | |
3574 | static void | |
3575 | remote_packet_size (const struct protocol_feature *feature, | |
3576 | enum packet_support support, const char *value) | |
3577 | { | |
3578 | struct remote_state *rs = get_remote_state (); | |
3579 | ||
3580 | int packet_size; | |
3581 | char *value_end; | |
3582 | ||
3583 | if (support != PACKET_ENABLE) | |
3584 | return; | |
3585 | ||
3586 | if (value == NULL || *value == '\0') | |
3587 | { | |
3588 | warning (_("Remote target reported \"%s\" without a size."), | |
3589 | feature->name); | |
3590 | return; | |
3591 | } | |
3592 | ||
3593 | errno = 0; | |
3594 | packet_size = strtol (value, &value_end, 16); | |
3595 | if (errno != 0 || *value_end != '\0' || packet_size < 0) | |
3596 | { | |
3597 | warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."), | |
3598 | feature->name, value); | |
3599 | return; | |
3600 | } | |
3601 | ||
3602 | if (packet_size > MAX_REMOTE_PACKET_SIZE) | |
3603 | { | |
3604 | warning (_("limiting remote suggested packet size (%d bytes) to %d"), | |
3605 | packet_size, MAX_REMOTE_PACKET_SIZE); | |
3606 | packet_size = MAX_REMOTE_PACKET_SIZE; | |
3607 | } | |
3608 | ||
3609 | /* Record the new maximum packet size. */ | |
3610 | rs->explicit_packet_size = packet_size; | |
3611 | } | |
3612 | ||
82f73884 PA |
3613 | static void |
3614 | remote_multi_process_feature (const struct protocol_feature *feature, | |
3615 | enum packet_support support, const char *value) | |
3616 | { | |
3617 | struct remote_state *rs = get_remote_state (); | |
a744cf53 | 3618 | |
82f73884 PA |
3619 | rs->multi_process_aware = (support == PACKET_ENABLE); |
3620 | } | |
3621 | ||
74531fed PA |
3622 | static void |
3623 | remote_non_stop_feature (const struct protocol_feature *feature, | |
3624 | enum packet_support support, const char *value) | |
3625 | { | |
3626 | struct remote_state *rs = get_remote_state (); | |
a744cf53 | 3627 | |
74531fed PA |
3628 | rs->non_stop_aware = (support == PACKET_ENABLE); |
3629 | } | |
3630 | ||
782b2b07 SS |
3631 | static void |
3632 | remote_cond_tracepoint_feature (const struct protocol_feature *feature, | |
3633 | enum packet_support support, | |
3634 | const char *value) | |
3635 | { | |
3636 | struct remote_state *rs = get_remote_state (); | |
a744cf53 | 3637 | |
782b2b07 SS |
3638 | rs->cond_tracepoints = (support == PACKET_ENABLE); |
3639 | } | |
3640 | ||
7a697b8d SS |
3641 | static void |
3642 | remote_fast_tracepoint_feature (const struct protocol_feature *feature, | |
3643 | enum packet_support support, | |
3644 | const char *value) | |
3645 | { | |
3646 | struct remote_state *rs = get_remote_state (); | |
a744cf53 | 3647 | |
7a697b8d SS |
3648 | rs->fast_tracepoints = (support == PACKET_ENABLE); |
3649 | } | |
3650 | ||
0fb4aa4b PA |
3651 | static void |
3652 | remote_static_tracepoint_feature (const struct protocol_feature *feature, | |
3653 | enum packet_support support, | |
3654 | const char *value) | |
3655 | { | |
3656 | struct remote_state *rs = get_remote_state (); | |
3657 | ||
3658 | rs->static_tracepoints = (support == PACKET_ENABLE); | |
3659 | } | |
3660 | ||
d5551862 SS |
3661 | static void |
3662 | remote_disconnected_tracing_feature (const struct protocol_feature *feature, | |
3663 | enum packet_support support, | |
3664 | const char *value) | |
3665 | { | |
3666 | struct remote_state *rs = get_remote_state (); | |
a744cf53 | 3667 | |
d5551862 SS |
3668 | rs->disconnected_tracing = (support == PACKET_ENABLE); |
3669 | } | |
3670 | ||
be2a5f71 | 3671 | static struct protocol_feature remote_protocol_features[] = { |
0876f84a | 3672 | { "PacketSize", PACKET_DISABLE, remote_packet_size, -1 }, |
40e57cf2 | 3673 | { "qXfer:auxv:read", PACKET_DISABLE, remote_supported_packet, |
fd79ecee | 3674 | PACKET_qXfer_auxv }, |
23181151 DJ |
3675 | { "qXfer:features:read", PACKET_DISABLE, remote_supported_packet, |
3676 | PACKET_qXfer_features }, | |
cfa9d6d9 DJ |
3677 | { "qXfer:libraries:read", PACKET_DISABLE, remote_supported_packet, |
3678 | PACKET_qXfer_libraries }, | |
fd79ecee | 3679 | { "qXfer:memory-map:read", PACKET_DISABLE, remote_supported_packet, |
89be2091 | 3680 | PACKET_qXfer_memory_map }, |
4de6483e UW |
3681 | { "qXfer:spu:read", PACKET_DISABLE, remote_supported_packet, |
3682 | PACKET_qXfer_spu_read }, | |
3683 | { "qXfer:spu:write", PACKET_DISABLE, remote_supported_packet, | |
3684 | PACKET_qXfer_spu_write }, | |
07e059b5 VP |
3685 | { "qXfer:osdata:read", PACKET_DISABLE, remote_supported_packet, |
3686 | PACKET_qXfer_osdata }, | |
dc146f7c VP |
3687 | { "qXfer:threads:read", PACKET_DISABLE, remote_supported_packet, |
3688 | PACKET_qXfer_threads }, | |
89be2091 DJ |
3689 | { "QPassSignals", PACKET_DISABLE, remote_supported_packet, |
3690 | PACKET_QPassSignals }, | |
a6f3e723 SL |
3691 | { "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet, |
3692 | PACKET_QStartNoAckMode }, | |
82f73884 | 3693 | { "multiprocess", PACKET_DISABLE, remote_multi_process_feature, -1 }, |
74531fed | 3694 | { "QNonStop", PACKET_DISABLE, remote_non_stop_feature, -1 }, |
4aa995e1 PA |
3695 | { "qXfer:siginfo:read", PACKET_DISABLE, remote_supported_packet, |
3696 | PACKET_qXfer_siginfo_read }, | |
3697 | { "qXfer:siginfo:write", PACKET_DISABLE, remote_supported_packet, | |
3698 | PACKET_qXfer_siginfo_write }, | |
782b2b07 SS |
3699 | { "ConditionalTracepoints", PACKET_DISABLE, remote_cond_tracepoint_feature, |
3700 | PACKET_ConditionalTracepoints }, | |
7a697b8d SS |
3701 | { "FastTracepoints", PACKET_DISABLE, remote_fast_tracepoint_feature, |
3702 | PACKET_FastTracepoints }, | |
0fb4aa4b PA |
3703 | { "StaticTracepoints", PACKET_DISABLE, remote_static_tracepoint_feature, |
3704 | PACKET_StaticTracepoints }, | |
d5551862 SS |
3705 | { "DisconnectedTracing", PACKET_DISABLE, remote_disconnected_tracing_feature, |
3706 | -1 }, | |
40ab02ce MS |
3707 | { "ReverseContinue", PACKET_DISABLE, remote_supported_packet, |
3708 | PACKET_bc }, | |
3709 | { "ReverseStep", PACKET_DISABLE, remote_supported_packet, | |
3710 | PACKET_bs }, | |
409873ef SS |
3711 | { "TracepointSource", PACKET_DISABLE, remote_supported_packet, |
3712 | PACKET_TracepointSource }, | |
d914c394 SS |
3713 | { "QAllow", PACKET_DISABLE, remote_supported_packet, |
3714 | PACKET_QAllow }, | |
be2a5f71 DJ |
3715 | }; |
3716 | ||
c8d5aac9 L |
3717 | static char *remote_support_xml; |
3718 | ||
3719 | /* Register string appended to "xmlRegisters=" in qSupported query. */ | |
3720 | ||
3721 | void | |
6e39997a | 3722 | register_remote_support_xml (const char *xml) |
c8d5aac9 L |
3723 | { |
3724 | #if defined(HAVE_LIBEXPAT) | |
3725 | if (remote_support_xml == NULL) | |
c4f7c687 | 3726 | remote_support_xml = concat ("xmlRegisters=", xml, (char *) NULL); |
c8d5aac9 L |
3727 | else |
3728 | { | |
3729 | char *copy = xstrdup (remote_support_xml + 13); | |
3730 | char *p = strtok (copy, ","); | |
3731 | ||
3732 | do | |
3733 | { | |
3734 | if (strcmp (p, xml) == 0) | |
3735 | { | |
3736 | /* already there */ | |
3737 | xfree (copy); | |
3738 | return; | |
3739 | } | |
3740 | } | |
3741 | while ((p = strtok (NULL, ",")) != NULL); | |
3742 | xfree (copy); | |
3743 | ||
94b0dee1 PA |
3744 | remote_support_xml = reconcat (remote_support_xml, |
3745 | remote_support_xml, ",", xml, | |
3746 | (char *) NULL); | |
c8d5aac9 L |
3747 | } |
3748 | #endif | |
3749 | } | |
3750 | ||
3751 | static char * | |
3752 | remote_query_supported_append (char *msg, const char *append) | |
3753 | { | |
3754 | if (msg) | |
94b0dee1 | 3755 | return reconcat (msg, msg, ";", append, (char *) NULL); |
c8d5aac9 L |
3756 | else |
3757 | return xstrdup (append); | |
3758 | } | |
3759 | ||
be2a5f71 DJ |
3760 | static void |
3761 | remote_query_supported (void) | |
3762 | { | |
3763 | struct remote_state *rs = get_remote_state (); | |
3764 | char *next; | |
3765 | int i; | |
3766 | unsigned char seen [ARRAY_SIZE (remote_protocol_features)]; | |
3767 | ||
3768 | /* The packet support flags are handled differently for this packet | |
3769 | than for most others. We treat an error, a disabled packet, and | |
3770 | an empty response identically: any features which must be reported | |
3771 | to be used will be automatically disabled. An empty buffer | |
3772 | accomplishes this, since that is also the representation for a list | |
3773 | containing no features. */ | |
3774 | ||
3775 | rs->buf[0] = 0; | |
3776 | if (remote_protocol_packets[PACKET_qSupported].support != PACKET_DISABLE) | |
3777 | { | |
c8d5aac9 | 3778 | char *q = NULL; |
94b0dee1 | 3779 | struct cleanup *old_chain = make_cleanup (free_current_contents, &q); |
c8d5aac9 L |
3780 | |
3781 | if (rs->extended) | |
3782 | q = remote_query_supported_append (q, "multiprocess+"); | |
c8d5aac9 L |
3783 | |
3784 | if (remote_support_xml) | |
3785 | q = remote_query_supported_append (q, remote_support_xml); | |
3786 | ||
dde08ee1 PA |
3787 | q = remote_query_supported_append (q, "qRelocInsn+"); |
3788 | ||
3789 | q = reconcat (q, "qSupported:", q, (char *) NULL); | |
3790 | putpkt (q); | |
82f73884 | 3791 | |
94b0dee1 PA |
3792 | do_cleanups (old_chain); |
3793 | ||
be2a5f71 DJ |
3794 | getpkt (&rs->buf, &rs->buf_size, 0); |
3795 | ||
3796 | /* If an error occured, warn, but do not return - just reset the | |
3797 | buffer to empty and go on to disable features. */ | |
3798 | if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSupported]) | |
3799 | == PACKET_ERROR) | |
3800 | { | |
3801 | warning (_("Remote failure reply: %s"), rs->buf); | |
3802 | rs->buf[0] = 0; | |
3803 | } | |
3804 | } | |
3805 | ||
3806 | memset (seen, 0, sizeof (seen)); | |
3807 | ||
3808 | next = rs->buf; | |
3809 | while (*next) | |
3810 | { | |
3811 | enum packet_support is_supported; | |
3812 | char *p, *end, *name_end, *value; | |
3813 | ||
3814 | /* First separate out this item from the rest of the packet. If | |
3815 | there's another item after this, we overwrite the separator | |
3816 | (terminated strings are much easier to work with). */ | |
3817 | p = next; | |
3818 | end = strchr (p, ';'); | |
3819 | if (end == NULL) | |
3820 | { | |
3821 | end = p + strlen (p); | |
3822 | next = end; | |
3823 | } | |
3824 | else | |
3825 | { | |
89be2091 DJ |
3826 | *end = '\0'; |
3827 | next = end + 1; | |
3828 | ||
be2a5f71 DJ |
3829 | if (end == p) |
3830 | { | |
3831 | warning (_("empty item in \"qSupported\" response")); | |
3832 | continue; | |
3833 | } | |
be2a5f71 DJ |
3834 | } |
3835 | ||
3836 | name_end = strchr (p, '='); | |
3837 | if (name_end) | |
3838 | { | |
3839 | /* This is a name=value entry. */ | |
3840 | is_supported = PACKET_ENABLE; | |
3841 | value = name_end + 1; | |
3842 | *name_end = '\0'; | |
3843 | } | |
3844 | else | |
3845 | { | |
3846 | value = NULL; | |
3847 | switch (end[-1]) | |
3848 | { | |
3849 | case '+': | |
3850 | is_supported = PACKET_ENABLE; | |
3851 | break; | |
3852 | ||
3853 | case '-': | |
3854 | is_supported = PACKET_DISABLE; | |
3855 | break; | |
3856 | ||
3857 | case '?': | |
3858 | is_supported = PACKET_SUPPORT_UNKNOWN; | |
3859 | break; | |
3860 | ||
3861 | default: | |
3862 | warning (_("unrecognized item \"%s\" in \"qSupported\" response"), p); | |
3863 | continue; | |
3864 | } | |
3865 | end[-1] = '\0'; | |
3866 | } | |
3867 | ||
3868 | for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++) | |
3869 | if (strcmp (remote_protocol_features[i].name, p) == 0) | |
3870 | { | |
3871 | const struct protocol_feature *feature; | |
3872 | ||
3873 | seen[i] = 1; | |
3874 | feature = &remote_protocol_features[i]; | |
3875 | feature->func (feature, is_supported, value); | |
3876 | break; | |
3877 | } | |
3878 | } | |
3879 | ||
3880 | /* If we increased the packet size, make sure to increase the global | |
3881 | buffer size also. We delay this until after parsing the entire | |
3882 | qSupported packet, because this is the same buffer we were | |
3883 | parsing. */ | |
3884 | if (rs->buf_size < rs->explicit_packet_size) | |
3885 | { | |
3886 | rs->buf_size = rs->explicit_packet_size; | |
3887 | rs->buf = xrealloc (rs->buf, rs->buf_size); | |
3888 | } | |
3889 | ||
3890 | /* Handle the defaults for unmentioned features. */ | |
3891 | for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++) | |
3892 | if (!seen[i]) | |
3893 | { | |
3894 | const struct protocol_feature *feature; | |
3895 | ||
3896 | feature = &remote_protocol_features[i]; | |
3897 | feature->func (feature, feature->default_support, NULL); | |
3898 | } | |
3899 | } | |
3900 | ||
3901 | ||
c906108c | 3902 | static void |
75c99385 | 3903 | remote_open_1 (char *name, int from_tty, struct target_ops *target, int extended_p) |
c906108c | 3904 | { |
d01949b6 | 3905 | struct remote_state *rs = get_remote_state (); |
a6f3e723 | 3906 | |
c906108c | 3907 | if (name == 0) |
8a3fe4f8 | 3908 | error (_("To open a remote debug connection, you need to specify what\n" |
22e04375 | 3909 | "serial device is attached to the remote system\n" |
8a3fe4f8 | 3910 | "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).")); |
c906108c | 3911 | |
23860348 | 3912 | /* See FIXME above. */ |
c6ebd6cf | 3913 | if (!target_async_permitted) |
92d1e331 | 3914 | wait_forever_enabled_p = 1; |
6426a772 | 3915 | |
2d717e4f DJ |
3916 | /* If we're connected to a running target, target_preopen will kill it. |
3917 | But if we're connected to a target system with no running process, | |
3918 | then we will still be connected when it returns. Ask this question | |
3919 | first, before target_preopen has a chance to kill anything. */ | |
c35b1492 | 3920 | if (remote_desc != NULL && !have_inferiors ()) |
2d717e4f DJ |
3921 | { |
3922 | if (!from_tty | |
3923 | || query (_("Already connected to a remote target. Disconnect? "))) | |
3924 | pop_target (); | |
3925 | else | |
3926 | error (_("Still connected.")); | |
3927 | } | |
3928 | ||
c906108c SS |
3929 | target_preopen (from_tty); |
3930 | ||
3931 | unpush_target (target); | |
3932 | ||
2d717e4f DJ |
3933 | /* This time without a query. If we were connected to an |
3934 | extended-remote target and target_preopen killed the running | |
3935 | process, we may still be connected. If we are starting "target | |
3936 | remote" now, the extended-remote target will not have been | |
3937 | removed by unpush_target. */ | |
c35b1492 | 3938 | if (remote_desc != NULL && !have_inferiors ()) |
2d717e4f DJ |
3939 | pop_target (); |
3940 | ||
89be2091 DJ |
3941 | /* Make sure we send the passed signals list the next time we resume. */ |
3942 | xfree (last_pass_packet); | |
3943 | last_pass_packet = NULL; | |
3944 | ||
ad9a8f3f | 3945 | remote_fileio_reset (); |
1dd41f16 NS |
3946 | reopen_exec_file (); |
3947 | reread_symbols (); | |
3948 | ||
9db8d71f | 3949 | remote_desc = remote_serial_open (name); |
c906108c SS |
3950 | if (!remote_desc) |
3951 | perror_with_name (name); | |
3952 | ||
3953 | if (baud_rate != -1) | |
3954 | { | |
2cd58942 | 3955 | if (serial_setbaudrate (remote_desc, baud_rate)) |
c906108c | 3956 | { |
9b74d5d3 KB |
3957 | /* The requested speed could not be set. Error out to |
3958 | top level after closing remote_desc. Take care to | |
3959 | set remote_desc to NULL to avoid closing remote_desc | |
3960 | more than once. */ | |
2cd58942 | 3961 | serial_close (remote_desc); |
9b74d5d3 | 3962 | remote_desc = NULL; |
c906108c SS |
3963 | perror_with_name (name); |
3964 | } | |
3965 | } | |
3966 | ||
2cd58942 | 3967 | serial_raw (remote_desc); |
c906108c SS |
3968 | |
3969 | /* If there is something sitting in the buffer we might take it as a | |
3970 | response to a command, which would be bad. */ | |
2cd58942 | 3971 | serial_flush_input (remote_desc); |
c906108c SS |
3972 | |
3973 | if (from_tty) | |
3974 | { | |
3975 | puts_filtered ("Remote debugging using "); | |
3976 | puts_filtered (name); | |
3977 | puts_filtered ("\n"); | |
3978 | } | |
23860348 | 3979 | push_target (target); /* Switch to using remote target now. */ |
c906108c | 3980 | |
74531fed PA |
3981 | /* Register extra event sources in the event loop. */ |
3982 | remote_async_inferior_event_token | |
3983 | = create_async_event_handler (remote_async_inferior_event_handler, | |
3984 | NULL); | |
3985 | remote_async_get_pending_events_token | |
3986 | = create_async_event_handler (remote_async_get_pending_events_handler, | |
3987 | NULL); | |
3988 | ||
be2a5f71 DJ |
3989 | /* Reset the target state; these things will be queried either by |
3990 | remote_query_supported or as they are needed. */ | |
d471ea57 | 3991 | init_all_packet_configs (); |
74531fed | 3992 | rs->cached_wait_status = 0; |
be2a5f71 | 3993 | rs->explicit_packet_size = 0; |
a6f3e723 | 3994 | rs->noack_mode = 0; |
82f73884 PA |
3995 | rs->multi_process_aware = 0; |
3996 | rs->extended = extended_p; | |
74531fed | 3997 | rs->non_stop_aware = 0; |
e24a49d8 | 3998 | rs->waiting_for_stop_reply = 0; |
3a29589a | 3999 | rs->ctrlc_pending_p = 0; |
802188a7 | 4000 | |
79d7f229 PA |
4001 | general_thread = not_sent_ptid; |
4002 | continue_thread = not_sent_ptid; | |
c906108c | 4003 | |
9d1f7ab2 MS |
4004 | /* Probe for ability to use "ThreadInfo" query, as required. */ |
4005 | use_threadinfo_query = 1; | |
4006 | use_threadextra_query = 1; | |
4007 | ||
c6ebd6cf | 4008 | if (target_async_permitted) |
92d1e331 | 4009 | { |
23860348 | 4010 | /* With this target we start out by owning the terminal. */ |
92d1e331 DJ |
4011 | remote_async_terminal_ours_p = 1; |
4012 | ||
4013 | /* FIXME: cagney/1999-09-23: During the initial connection it is | |
4014 | assumed that the target is already ready and able to respond to | |
4015 | requests. Unfortunately remote_start_remote() eventually calls | |
4016 | wait_for_inferior() with no timeout. wait_forever_enabled_p gets | |
4017 | around this. Eventually a mechanism that allows | |
4018 | wait_for_inferior() to expect/get timeouts will be | |
23860348 | 4019 | implemented. */ |
92d1e331 DJ |
4020 | wait_forever_enabled_p = 0; |
4021 | } | |
4022 | ||
23860348 | 4023 | /* First delete any symbols previously loaded from shared libraries. */ |
f78f6cf1 | 4024 | no_shared_libraries (NULL, 0); |
f78f6cf1 | 4025 | |
74531fed PA |
4026 | /* Start afresh. */ |
4027 | init_thread_list (); | |
4028 | ||
36918e70 | 4029 | /* Start the remote connection. If error() or QUIT, discard this |
165b8e33 AC |
4030 | target (we'd otherwise be in an inconsistent state) and then |
4031 | propogate the error on up the exception chain. This ensures that | |
4032 | the caller doesn't stumble along blindly assuming that the | |
4033 | function succeeded. The CLI doesn't have this problem but other | |
4034 | UI's, such as MI do. | |
36918e70 AC |
4035 | |
4036 | FIXME: cagney/2002-05-19: Instead of re-throwing the exception, | |
4037 | this function should return an error indication letting the | |
ce2826aa | 4038 | caller restore the previous state. Unfortunately the command |
36918e70 AC |
4039 | ``target remote'' is directly wired to this function making that |
4040 | impossible. On a positive note, the CLI side of this problem has | |
4041 | been fixed - the function set_cmd_context() makes it possible for | |
4042 | all the ``target ....'' commands to share a common callback | |
4043 | function. See cli-dump.c. */ | |
109c3e39 | 4044 | { |
2d717e4f DJ |
4045 | struct gdb_exception ex; |
4046 | struct start_remote_args args; | |
4047 | ||
4048 | args.from_tty = from_tty; | |
4049 | args.target = target; | |
4050 | args.extended_p = extended_p; | |
4051 | ||
4052 | ex = catch_exception (uiout, remote_start_remote, &args, RETURN_MASK_ALL); | |
109c3e39 AC |
4053 | if (ex.reason < 0) |
4054 | { | |
c8d104ad PA |
4055 | /* Pop the partially set up target - unless something else did |
4056 | already before throwing the exception. */ | |
4057 | if (remote_desc != NULL) | |
4058 | pop_target (); | |
c6ebd6cf | 4059 | if (target_async_permitted) |
109c3e39 AC |
4060 | wait_forever_enabled_p = 1; |
4061 | throw_exception (ex); | |
4062 | } | |
4063 | } | |
c906108c | 4064 | |
c6ebd6cf | 4065 | if (target_async_permitted) |
92d1e331 | 4066 | wait_forever_enabled_p = 1; |
43ff13b4 JM |
4067 | } |
4068 | ||
c906108c SS |
4069 | /* This takes a program previously attached to and detaches it. After |
4070 | this is done, GDB can be used to debug some other program. We | |
4071 | better not have left any breakpoints in the target program or it'll | |
4072 | die when it hits one. */ | |
4073 | ||
4074 | static void | |
2d717e4f | 4075 | remote_detach_1 (char *args, int from_tty, int extended) |
c906108c | 4076 | { |
82f73884 | 4077 | int pid = ptid_get_pid (inferior_ptid); |
d01949b6 | 4078 | struct remote_state *rs = get_remote_state (); |
c906108c SS |
4079 | |
4080 | if (args) | |
8a3fe4f8 | 4081 | error (_("Argument given to \"detach\" when remotely debugging.")); |
c906108c | 4082 | |
2d717e4f DJ |
4083 | if (!target_has_execution) |
4084 | error (_("No process to detach from.")); | |
4085 | ||
c906108c | 4086 | /* Tell the remote target to detach. */ |
82f73884 PA |
4087 | if (remote_multi_process_p (rs)) |
4088 | sprintf (rs->buf, "D;%x", pid); | |
4089 | else | |
4090 | strcpy (rs->buf, "D"); | |
4091 | ||
4ddda9b5 PA |
4092 | putpkt (rs->buf); |
4093 | getpkt (&rs->buf, &rs->buf_size, 0); | |
4094 | ||
82f73884 PA |
4095 | if (rs->buf[0] == 'O' && rs->buf[1] == 'K') |
4096 | ; | |
4097 | else if (rs->buf[0] == '\0') | |
4098 | error (_("Remote doesn't know how to detach")); | |
4099 | else | |
4ddda9b5 | 4100 | error (_("Can't detach process.")); |
c906108c | 4101 | |
c906108c | 4102 | if (from_tty) |
2d717e4f | 4103 | { |
82f73884 PA |
4104 | if (remote_multi_process_p (rs)) |
4105 | printf_filtered (_("Detached from remote %s.\n"), | |
4106 | target_pid_to_str (pid_to_ptid (pid))); | |
2d717e4f | 4107 | else |
82f73884 PA |
4108 | { |
4109 | if (extended) | |
4110 | puts_filtered (_("Detached from remote process.\n")); | |
4111 | else | |
4112 | puts_filtered (_("Ending remote debugging.\n")); | |
4113 | } | |
2d717e4f | 4114 | } |
82f73884 | 4115 | |
74531fed | 4116 | discard_pending_stop_replies (pid); |
82f73884 | 4117 | target_mourn_inferior (); |
2d717e4f DJ |
4118 | } |
4119 | ||
4120 | static void | |
136d6dae | 4121 | remote_detach (struct target_ops *ops, char *args, int from_tty) |
2d717e4f DJ |
4122 | { |
4123 | remote_detach_1 (args, from_tty, 0); | |
4124 | } | |
4125 | ||
4126 | static void | |
136d6dae | 4127 | extended_remote_detach (struct target_ops *ops, char *args, int from_tty) |
2d717e4f DJ |
4128 | { |
4129 | remote_detach_1 (args, from_tty, 1); | |
c906108c SS |
4130 | } |
4131 | ||
6ad8ae5c DJ |
4132 | /* Same as remote_detach, but don't send the "D" packet; just disconnect. */ |
4133 | ||
43ff13b4 | 4134 | static void |
597320e7 | 4135 | remote_disconnect (struct target_ops *target, char *args, int from_tty) |
43ff13b4 | 4136 | { |
43ff13b4 | 4137 | if (args) |
2d717e4f | 4138 | error (_("Argument given to \"disconnect\" when remotely debugging.")); |
43ff13b4 | 4139 | |
2d717e4f DJ |
4140 | /* Make sure we unpush even the extended remote targets; mourn |
4141 | won't do it. So call remote_mourn_1 directly instead of | |
4142 | target_mourn_inferior. */ | |
4143 | remote_mourn_1 (target); | |
4144 | ||
43ff13b4 JM |
4145 | if (from_tty) |
4146 | puts_filtered ("Ending remote debugging.\n"); | |
4147 | } | |
4148 | ||
2d717e4f DJ |
4149 | /* Attach to the process specified by ARGS. If FROM_TTY is non-zero, |
4150 | be chatty about it. */ | |
4151 | ||
4152 | static void | |
4153 | extended_remote_attach_1 (struct target_ops *target, char *args, int from_tty) | |
4154 | { | |
4155 | struct remote_state *rs = get_remote_state (); | |
be86555c | 4156 | int pid; |
96ef3384 | 4157 | char *wait_status = NULL; |
2d717e4f | 4158 | |
74164c56 | 4159 | pid = parse_pid_to_attach (args); |
2d717e4f | 4160 | |
74164c56 JK |
4161 | /* Remote PID can be freely equal to getpid, do not check it here the same |
4162 | way as in other targets. */ | |
2d717e4f DJ |
4163 | |
4164 | if (remote_protocol_packets[PACKET_vAttach].support == PACKET_DISABLE) | |
4165 | error (_("This target does not support attaching to a process")); | |
4166 | ||
4167 | sprintf (rs->buf, "vAttach;%x", pid); | |
4168 | putpkt (rs->buf); | |
4169 | getpkt (&rs->buf, &rs->buf_size, 0); | |
4170 | ||
4171 | if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vAttach]) == PACKET_OK) | |
4172 | { | |
4173 | if (from_tty) | |
4174 | printf_unfiltered (_("Attached to %s\n"), | |
4175 | target_pid_to_str (pid_to_ptid (pid))); | |
4176 | ||
74531fed PA |
4177 | if (!non_stop) |
4178 | { | |
4179 | /* Save the reply for later. */ | |
4180 | wait_status = alloca (strlen (rs->buf) + 1); | |
4181 | strcpy (wait_status, rs->buf); | |
4182 | } | |
4183 | else if (strcmp (rs->buf, "OK") != 0) | |
4184 | error (_("Attaching to %s failed with: %s"), | |
4185 | target_pid_to_str (pid_to_ptid (pid)), | |
4186 | rs->buf); | |
2d717e4f DJ |
4187 | } |
4188 | else if (remote_protocol_packets[PACKET_vAttach].support == PACKET_DISABLE) | |
4189 | error (_("This target does not support attaching to a process")); | |
4190 | else | |
4191 | error (_("Attaching to %s failed"), | |
4192 | target_pid_to_str (pid_to_ptid (pid))); | |
4193 | ||
6c95b8df | 4194 | set_current_inferior (remote_add_inferior (pid, 1)); |
bad34192 | 4195 | |
2d717e4f | 4196 | inferior_ptid = pid_to_ptid (pid); |
79d7f229 | 4197 | |
bad34192 PA |
4198 | if (non_stop) |
4199 | { | |
4200 | struct thread_info *thread; | |
79d7f229 | 4201 | |
bad34192 PA |
4202 | /* Get list of threads. */ |
4203 | remote_threads_info (target); | |
82f73884 | 4204 | |
bad34192 PA |
4205 | thread = first_thread_of_process (pid); |
4206 | if (thread) | |
4207 | inferior_ptid = thread->ptid; | |
4208 | else | |
4209 | inferior_ptid = pid_to_ptid (pid); | |
4210 | ||
4211 | /* Invalidate our notion of the remote current thread. */ | |
4212 | record_currthread (minus_one_ptid); | |
4213 | } | |
74531fed | 4214 | else |
bad34192 PA |
4215 | { |
4216 | /* Now, if we have thread information, update inferior_ptid. */ | |
4217 | inferior_ptid = remote_current_thread (inferior_ptid); | |
4218 | ||
4219 | /* Add the main thread to the thread list. */ | |
4220 | add_thread_silent (inferior_ptid); | |
4221 | } | |
c0a2216e | 4222 | |
96ef3384 UW |
4223 | /* Next, if the target can specify a description, read it. We do |
4224 | this before anything involving memory or registers. */ | |
4225 | target_find_description (); | |
4226 | ||
74531fed PA |
4227 | if (!non_stop) |
4228 | { | |
4229 | /* Use the previously fetched status. */ | |
4230 | gdb_assert (wait_status != NULL); | |
4231 | ||
4232 | if (target_can_async_p ()) | |
4233 | { | |
4234 | struct stop_reply *stop_reply; | |
4235 | struct cleanup *old_chain; | |
4236 | ||
4237 | stop_reply = stop_reply_xmalloc (); | |
4238 | old_chain = make_cleanup (do_stop_reply_xfree, stop_reply); | |
4239 | remote_parse_stop_reply (wait_status, stop_reply); | |
4240 | discard_cleanups (old_chain); | |
4241 | push_stop_reply (stop_reply); | |
4242 | ||
4243 | target_async (inferior_event_handler, 0); | |
4244 | } | |
4245 | else | |
4246 | { | |
4247 | gdb_assert (wait_status != NULL); | |
4248 | strcpy (rs->buf, wait_status); | |
4249 | rs->cached_wait_status = 1; | |
4250 | } | |
4251 | } | |
4252 | else | |
4253 | gdb_assert (wait_status == NULL); | |
2d717e4f DJ |
4254 | } |
4255 | ||
4256 | static void | |
136d6dae | 4257 | extended_remote_attach (struct target_ops *ops, char *args, int from_tty) |
2d717e4f | 4258 | { |
136d6dae | 4259 | extended_remote_attach_1 (ops, args, from_tty); |
2d717e4f DJ |
4260 | } |
4261 | ||
c906108c SS |
4262 | /* Convert hex digit A to a number. */ |
4263 | ||
30559e10 | 4264 | static int |
fba45db2 | 4265 | fromhex (int a) |
c906108c SS |
4266 | { |
4267 | if (a >= '0' && a <= '9') | |
4268 | return a - '0'; | |
4269 | else if (a >= 'a' && a <= 'f') | |
4270 | return a - 'a' + 10; | |
4271 | else if (a >= 'A' && a <= 'F') | |
4272 | return a - 'A' + 10; | |
c5aa993b | 4273 | else |
8a3fe4f8 | 4274 | error (_("Reply contains invalid hex digit %d"), a); |
c906108c SS |
4275 | } |
4276 | ||
00bf0b85 | 4277 | int |
cfd77fa1 | 4278 | hex2bin (const char *hex, gdb_byte *bin, int count) |
30559e10 MS |
4279 | { |
4280 | int i; | |
4281 | ||
30559e10 MS |
4282 | for (i = 0; i < count; i++) |
4283 | { | |
4284 | if (hex[0] == 0 || hex[1] == 0) | |
4285 | { | |
4286 | /* Hex string is short, or of uneven length. | |
23860348 | 4287 | Return the count that has been converted so far. */ |
30559e10 MS |
4288 | return i; |
4289 | } | |
4290 | *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]); | |
4291 | hex += 2; | |
4292 | } | |
4293 | return i; | |
4294 | } | |
4295 | ||
c906108c SS |
4296 | /* Convert number NIB to a hex digit. */ |
4297 | ||
4298 | static int | |
fba45db2 | 4299 | tohex (int nib) |
c906108c SS |
4300 | { |
4301 | if (nib < 10) | |
c5aa993b | 4302 | return '0' + nib; |
c906108c | 4303 | else |
c5aa993b | 4304 | return 'a' + nib - 10; |
c906108c | 4305 | } |
30559e10 | 4306 | |
00bf0b85 | 4307 | int |
cfd77fa1 | 4308 | bin2hex (const gdb_byte *bin, char *hex, int count) |
30559e10 MS |
4309 | { |
4310 | int i; | |
a744cf53 | 4311 | |
23860348 | 4312 | /* May use a length, or a nul-terminated string as input. */ |
30559e10 | 4313 | if (count == 0) |
cfd77fa1 | 4314 | count = strlen ((char *) bin); |
30559e10 MS |
4315 | |
4316 | for (i = 0; i < count; i++) | |
4317 | { | |
4318 | *hex++ = tohex ((*bin >> 4) & 0xf); | |
4319 | *hex++ = tohex (*bin++ & 0xf); | |
4320 | } | |
4321 | *hex = 0; | |
4322 | return i; | |
4323 | } | |
c906108c | 4324 | \f |
506fb367 DJ |
4325 | /* Check for the availability of vCont. This function should also check |
4326 | the response. */ | |
c906108c SS |
4327 | |
4328 | static void | |
6d820c5c | 4329 | remote_vcont_probe (struct remote_state *rs) |
c906108c | 4330 | { |
2e9f7625 | 4331 | char *buf; |
6d820c5c | 4332 | |
2e9f7625 DJ |
4333 | strcpy (rs->buf, "vCont?"); |
4334 | putpkt (rs->buf); | |
6d820c5c | 4335 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 4336 | buf = rs->buf; |
c906108c | 4337 | |
506fb367 DJ |
4338 | /* Make sure that the features we assume are supported. */ |
4339 | if (strncmp (buf, "vCont", 5) == 0) | |
4340 | { | |
4341 | char *p = &buf[5]; | |
4342 | int support_s, support_S, support_c, support_C; | |
4343 | ||
4344 | support_s = 0; | |
4345 | support_S = 0; | |
4346 | support_c = 0; | |
4347 | support_C = 0; | |
74531fed | 4348 | rs->support_vCont_t = 0; |
506fb367 DJ |
4349 | while (p && *p == ';') |
4350 | { | |
4351 | p++; | |
4352 | if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0)) | |
4353 | support_s = 1; | |
4354 | else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0)) | |
4355 | support_S = 1; | |
4356 | else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0)) | |
4357 | support_c = 1; | |
4358 | else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0)) | |
4359 | support_C = 1; | |
74531fed PA |
4360 | else if (*p == 't' && (*(p + 1) == ';' || *(p + 1) == 0)) |
4361 | rs->support_vCont_t = 1; | |
506fb367 DJ |
4362 | |
4363 | p = strchr (p, ';'); | |
4364 | } | |
c906108c | 4365 | |
506fb367 DJ |
4366 | /* If s, S, c, and C are not all supported, we can't use vCont. Clearing |
4367 | BUF will make packet_ok disable the packet. */ | |
4368 | if (!support_s || !support_S || !support_c || !support_C) | |
4369 | buf[0] = 0; | |
4370 | } | |
c906108c | 4371 | |
444abaca | 4372 | packet_ok (buf, &remote_protocol_packets[PACKET_vCont]); |
506fb367 | 4373 | } |
c906108c | 4374 | |
0d8f58ca PA |
4375 | /* Helper function for building "vCont" resumptions. Write a |
4376 | resumption to P. ENDP points to one-passed-the-end of the buffer | |
4377 | we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The | |
4378 | thread to be resumed is PTID; STEP and SIGGNAL indicate whether the | |
4379 | resumed thread should be single-stepped and/or signalled. If PTID | |
4380 | equals minus_one_ptid, then all threads are resumed; if PTID | |
4381 | represents a process, then all threads of the process are resumed; | |
4382 | the thread to be stepped and/or signalled is given in the global | |
4383 | INFERIOR_PTID. */ | |
4384 | ||
4385 | static char * | |
4386 | append_resumption (char *p, char *endp, | |
4387 | ptid_t ptid, int step, enum target_signal siggnal) | |
4388 | { | |
4389 | struct remote_state *rs = get_remote_state (); | |
4390 | ||
4391 | if (step && siggnal != TARGET_SIGNAL_0) | |
4392 | p += xsnprintf (p, endp - p, ";S%02x", siggnal); | |
4393 | else if (step) | |
4394 | p += xsnprintf (p, endp - p, ";s"); | |
4395 | else if (siggnal != TARGET_SIGNAL_0) | |
4396 | p += xsnprintf (p, endp - p, ";C%02x", siggnal); | |
4397 | else | |
4398 | p += xsnprintf (p, endp - p, ";c"); | |
4399 | ||
4400 | if (remote_multi_process_p (rs) && ptid_is_pid (ptid)) | |
4401 | { | |
4402 | ptid_t nptid; | |
4403 | ||
4404 | /* All (-1) threads of process. */ | |
4405 | nptid = ptid_build (ptid_get_pid (ptid), 0, -1); | |
4406 | ||
4407 | p += xsnprintf (p, endp - p, ":"); | |
4408 | p = write_ptid (p, endp, nptid); | |
4409 | } | |
4410 | else if (!ptid_equal (ptid, minus_one_ptid)) | |
4411 | { | |
4412 | p += xsnprintf (p, endp - p, ":"); | |
4413 | p = write_ptid (p, endp, ptid); | |
4414 | } | |
4415 | ||
4416 | return p; | |
4417 | } | |
4418 | ||
506fb367 DJ |
4419 | /* Resume the remote inferior by using a "vCont" packet. The thread |
4420 | to be resumed is PTID; STEP and SIGGNAL indicate whether the | |
79d7f229 PA |
4421 | resumed thread should be single-stepped and/or signalled. If PTID |
4422 | equals minus_one_ptid, then all threads are resumed; the thread to | |
4423 | be stepped and/or signalled is given in the global INFERIOR_PTID. | |
4424 | This function returns non-zero iff it resumes the inferior. | |
44eaed12 | 4425 | |
506fb367 DJ |
4426 | This function issues a strict subset of all possible vCont commands at the |
4427 | moment. */ | |
44eaed12 | 4428 | |
506fb367 DJ |
4429 | static int |
4430 | remote_vcont_resume (ptid_t ptid, int step, enum target_signal siggnal) | |
4431 | { | |
4432 | struct remote_state *rs = get_remote_state (); | |
82f73884 PA |
4433 | char *p; |
4434 | char *endp; | |
44eaed12 | 4435 | |
444abaca | 4436 | if (remote_protocol_packets[PACKET_vCont].support == PACKET_SUPPORT_UNKNOWN) |
6d820c5c | 4437 | remote_vcont_probe (rs); |
44eaed12 | 4438 | |
444abaca | 4439 | if (remote_protocol_packets[PACKET_vCont].support == PACKET_DISABLE) |
6d820c5c | 4440 | return 0; |
44eaed12 | 4441 | |
82f73884 PA |
4442 | p = rs->buf; |
4443 | endp = rs->buf + get_remote_packet_size (); | |
4444 | ||
506fb367 DJ |
4445 | /* If we could generate a wider range of packets, we'd have to worry |
4446 | about overflowing BUF. Should there be a generic | |
4447 | "multi-part-packet" packet? */ | |
4448 | ||
0d8f58ca PA |
4449 | p += xsnprintf (p, endp - p, "vCont"); |
4450 | ||
79d7f229 | 4451 | if (ptid_equal (ptid, magic_null_ptid)) |
c906108c | 4452 | { |
79d7f229 PA |
4453 | /* MAGIC_NULL_PTID means that we don't have any active threads, |
4454 | so we don't have any TID numbers the inferior will | |
4455 | understand. Make sure to only send forms that do not specify | |
4456 | a TID. */ | |
0d8f58ca | 4457 | p = append_resumption (p, endp, minus_one_ptid, step, siggnal); |
506fb367 | 4458 | } |
0d8f58ca | 4459 | else if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid)) |
506fb367 | 4460 | { |
0d8f58ca PA |
4461 | /* Resume all threads (of all processes, or of a single |
4462 | process), with preference for INFERIOR_PTID. This assumes | |
4463 | inferior_ptid belongs to the set of all threads we are about | |
4464 | to resume. */ | |
4465 | if (step || siggnal != TARGET_SIGNAL_0) | |
82f73884 | 4466 | { |
0d8f58ca PA |
4467 | /* Step inferior_ptid, with or without signal. */ |
4468 | p = append_resumption (p, endp, inferior_ptid, step, siggnal); | |
82f73884 | 4469 | } |
0d8f58ca PA |
4470 | |
4471 | /* And continue others without a signal. */ | |
4472 | p = append_resumption (p, endp, ptid, /*step=*/ 0, TARGET_SIGNAL_0); | |
c906108c SS |
4473 | } |
4474 | else | |
506fb367 DJ |
4475 | { |
4476 | /* Scheduler locking; resume only PTID. */ | |
0d8f58ca | 4477 | p = append_resumption (p, endp, ptid, step, siggnal); |
506fb367 | 4478 | } |
c906108c | 4479 | |
82f73884 PA |
4480 | gdb_assert (strlen (rs->buf) < get_remote_packet_size ()); |
4481 | putpkt (rs->buf); | |
506fb367 | 4482 | |
74531fed PA |
4483 | if (non_stop) |
4484 | { | |
4485 | /* In non-stop, the stub replies to vCont with "OK". The stop | |
4486 | reply will be reported asynchronously by means of a `%Stop' | |
4487 | notification. */ | |
4488 | getpkt (&rs->buf, &rs->buf_size, 0); | |
4489 | if (strcmp (rs->buf, "OK") != 0) | |
4490 | error (_("Unexpected vCont reply in non-stop mode: %s"), rs->buf); | |
4491 | } | |
4492 | ||
506fb367 | 4493 | return 1; |
c906108c | 4494 | } |
43ff13b4 | 4495 | |
506fb367 DJ |
4496 | /* Tell the remote machine to resume. */ |
4497 | ||
4498 | static enum target_signal last_sent_signal = TARGET_SIGNAL_0; | |
4499 | ||
4500 | static int last_sent_step; | |
4501 | ||
43ff13b4 | 4502 | static void |
28439f5e PA |
4503 | remote_resume (struct target_ops *ops, |
4504 | ptid_t ptid, int step, enum target_signal siggnal) | |
43ff13b4 | 4505 | { |
d01949b6 | 4506 | struct remote_state *rs = get_remote_state (); |
2e9f7625 | 4507 | char *buf; |
43ff13b4 | 4508 | |
43ff13b4 JM |
4509 | last_sent_signal = siggnal; |
4510 | last_sent_step = step; | |
4511 | ||
89be2091 DJ |
4512 | /* Update the inferior on signals to silently pass, if they've changed. */ |
4513 | remote_pass_signals (); | |
4514 | ||
506fb367 | 4515 | /* The vCont packet doesn't need to specify threads via Hc. */ |
40ab02ce MS |
4516 | /* No reverse support (yet) for vCont. */ |
4517 | if (execution_direction != EXEC_REVERSE) | |
4518 | if (remote_vcont_resume (ptid, step, siggnal)) | |
4519 | goto done; | |
506fb367 | 4520 | |
79d7f229 PA |
4521 | /* All other supported resume packets do use Hc, so set the continue |
4522 | thread. */ | |
4523 | if (ptid_equal (ptid, minus_one_ptid)) | |
4524 | set_continue_thread (any_thread_ptid); | |
506fb367 | 4525 | else |
79d7f229 | 4526 | set_continue_thread (ptid); |
506fb367 | 4527 | |
2e9f7625 | 4528 | buf = rs->buf; |
b2175913 MS |
4529 | if (execution_direction == EXEC_REVERSE) |
4530 | { | |
4531 | /* We don't pass signals to the target in reverse exec mode. */ | |
4532 | if (info_verbose && siggnal != TARGET_SIGNAL_0) | |
4533 | warning (" - Can't pass signal %d to target in reverse: ignored.\n", | |
4534 | siggnal); | |
40ab02ce MS |
4535 | |
4536 | if (step | |
4537 | && remote_protocol_packets[PACKET_bs].support == PACKET_DISABLE) | |
4538 | error (_("Remote reverse-step not supported.")); | |
4539 | if (!step | |
4540 | && remote_protocol_packets[PACKET_bc].support == PACKET_DISABLE) | |
08c93ed9 | 4541 | error (_("Remote reverse-continue not supported.")); |
40ab02ce | 4542 | |
b2175913 MS |
4543 | strcpy (buf, step ? "bs" : "bc"); |
4544 | } | |
4545 | else if (siggnal != TARGET_SIGNAL_0) | |
43ff13b4 JM |
4546 | { |
4547 | buf[0] = step ? 'S' : 'C'; | |
c5aa993b | 4548 | buf[1] = tohex (((int) siggnal >> 4) & 0xf); |
506fb367 | 4549 | buf[2] = tohex (((int) siggnal) & 0xf); |
43ff13b4 JM |
4550 | buf[3] = '\0'; |
4551 | } | |
4552 | else | |
c5aa993b | 4553 | strcpy (buf, step ? "s" : "c"); |
506fb367 | 4554 | |
44eaed12 | 4555 | putpkt (buf); |
43ff13b4 | 4556 | |
75c99385 | 4557 | done: |
2acceee2 JM |
4558 | /* We are about to start executing the inferior, let's register it |
4559 | with the event loop. NOTE: this is the one place where all the | |
4560 | execution commands end up. We could alternatively do this in each | |
23860348 | 4561 | of the execution commands in infcmd.c. */ |
2acceee2 JM |
4562 | /* FIXME: ezannoni 1999-09-28: We may need to move this out of here |
4563 | into infcmd.c in order to allow inferior function calls to work | |
23860348 | 4564 | NOT asynchronously. */ |
362646f5 | 4565 | if (target_can_async_p ()) |
2acceee2 | 4566 | target_async (inferior_event_handler, 0); |
e24a49d8 PA |
4567 | |
4568 | /* We've just told the target to resume. The remote server will | |
4569 | wait for the inferior to stop, and then send a stop reply. In | |
4570 | the mean time, we can't start another command/query ourselves | |
74531fed PA |
4571 | because the stub wouldn't be ready to process it. This applies |
4572 | only to the base all-stop protocol, however. In non-stop (which | |
4573 | only supports vCont), the stub replies with an "OK", and is | |
4574 | immediate able to process further serial input. */ | |
4575 | if (!non_stop) | |
4576 | rs->waiting_for_stop_reply = 1; | |
43ff13b4 | 4577 | } |
c906108c | 4578 | \f |
43ff13b4 JM |
4579 | |
4580 | /* Set up the signal handler for SIGINT, while the target is | |
23860348 | 4581 | executing, ovewriting the 'regular' SIGINT signal handler. */ |
43ff13b4 | 4582 | static void |
fba45db2 | 4583 | initialize_sigint_signal_handler (void) |
43ff13b4 | 4584 | { |
43ff13b4 JM |
4585 | signal (SIGINT, handle_remote_sigint); |
4586 | } | |
4587 | ||
23860348 | 4588 | /* Signal handler for SIGINT, while the target is executing. */ |
43ff13b4 | 4589 | static void |
fba45db2 | 4590 | handle_remote_sigint (int sig) |
43ff13b4 JM |
4591 | { |
4592 | signal (sig, handle_remote_sigint_twice); | |
43ff13b4 JM |
4593 | mark_async_signal_handler_wrapper (sigint_remote_token); |
4594 | } | |
4595 | ||
4596 | /* Signal handler for SIGINT, installed after SIGINT has already been | |
4597 | sent once. It will take effect the second time that the user sends | |
23860348 | 4598 | a ^C. */ |
43ff13b4 | 4599 | static void |
fba45db2 | 4600 | handle_remote_sigint_twice (int sig) |
43ff13b4 | 4601 | { |
b803fb0f | 4602 | signal (sig, handle_remote_sigint); |
43ff13b4 JM |
4603 | mark_async_signal_handler_wrapper (sigint_remote_twice_token); |
4604 | } | |
4605 | ||
6426a772 | 4606 | /* Perform the real interruption of the target execution, in response |
23860348 | 4607 | to a ^C. */ |
c5aa993b | 4608 | static void |
fba45db2 | 4609 | async_remote_interrupt (gdb_client_data arg) |
43ff13b4 JM |
4610 | { |
4611 | if (remote_debug) | |
4612 | fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n"); | |
4613 | ||
94cc34af | 4614 | target_stop (inferior_ptid); |
43ff13b4 JM |
4615 | } |
4616 | ||
4617 | /* Perform interrupt, if the first attempt did not succeed. Just give | |
23860348 | 4618 | up on the target alltogether. */ |
2df3850c | 4619 | void |
fba45db2 | 4620 | async_remote_interrupt_twice (gdb_client_data arg) |
43ff13b4 | 4621 | { |
2df3850c JM |
4622 | if (remote_debug) |
4623 | fprintf_unfiltered (gdb_stdlog, "remote_interrupt_twice called\n"); | |
b803fb0f DJ |
4624 | |
4625 | interrupt_query (); | |
43ff13b4 JM |
4626 | } |
4627 | ||
4628 | /* Reinstall the usual SIGINT handlers, after the target has | |
23860348 | 4629 | stopped. */ |
6426a772 JM |
4630 | static void |
4631 | cleanup_sigint_signal_handler (void *dummy) | |
43ff13b4 JM |
4632 | { |
4633 | signal (SIGINT, handle_sigint); | |
43ff13b4 JM |
4634 | } |
4635 | ||
c906108c SS |
4636 | /* Send ^C to target to halt it. Target will respond, and send us a |
4637 | packet. */ | |
507f3c78 | 4638 | static void (*ofunc) (int); |
c906108c | 4639 | |
7a292a7a SS |
4640 | /* The command line interface's stop routine. This function is installed |
4641 | as a signal handler for SIGINT. The first time a user requests a | |
4642 | stop, we call remote_stop to send a break or ^C. If there is no | |
4643 | response from the target (it didn't stop when the user requested it), | |
23860348 | 4644 | we ask the user if he'd like to detach from the target. */ |
c906108c | 4645 | static void |
fba45db2 | 4646 | remote_interrupt (int signo) |
c906108c | 4647 | { |
23860348 | 4648 | /* If this doesn't work, try more severe steps. */ |
7a292a7a SS |
4649 | signal (signo, remote_interrupt_twice); |
4650 | ||
b803fb0f | 4651 | gdb_call_async_signal_handler (sigint_remote_token, 1); |
7a292a7a SS |
4652 | } |
4653 | ||
4654 | /* The user typed ^C twice. */ | |
4655 | ||
4656 | static void | |
fba45db2 | 4657 | remote_interrupt_twice (int signo) |
7a292a7a SS |
4658 | { |
4659 | signal (signo, ofunc); | |
b803fb0f | 4660 | gdb_call_async_signal_handler (sigint_remote_twice_token, 1); |
c906108c SS |
4661 | signal (signo, remote_interrupt); |
4662 | } | |
7a292a7a | 4663 | |
74531fed PA |
4664 | /* Non-stop version of target_stop. Uses `vCont;t' to stop a remote |
4665 | thread, all threads of a remote process, or all threads of all | |
4666 | processes. */ | |
4667 | ||
4668 | static void | |
4669 | remote_stop_ns (ptid_t ptid) | |
4670 | { | |
4671 | struct remote_state *rs = get_remote_state (); | |
4672 | char *p = rs->buf; | |
4673 | char *endp = rs->buf + get_remote_packet_size (); | |
74531fed PA |
4674 | |
4675 | if (remote_protocol_packets[PACKET_vCont].support == PACKET_SUPPORT_UNKNOWN) | |
4676 | remote_vcont_probe (rs); | |
4677 | ||
4678 | if (!rs->support_vCont_t) | |
4679 | error (_("Remote server does not support stopping threads")); | |
4680 | ||
f91d3df5 PA |
4681 | if (ptid_equal (ptid, minus_one_ptid) |
4682 | || (!remote_multi_process_p (rs) && ptid_is_pid (ptid))) | |
74531fed PA |
4683 | p += xsnprintf (p, endp - p, "vCont;t"); |
4684 | else | |
4685 | { | |
4686 | ptid_t nptid; | |
4687 | ||
74531fed PA |
4688 | p += xsnprintf (p, endp - p, "vCont;t:"); |
4689 | ||
4690 | if (ptid_is_pid (ptid)) | |
4691 | /* All (-1) threads of process. */ | |
4692 | nptid = ptid_build (ptid_get_pid (ptid), 0, -1); | |
4693 | else | |
4694 | { | |
4695 | /* Small optimization: if we already have a stop reply for | |
4696 | this thread, no use in telling the stub we want this | |
4697 | stopped. */ | |
4698 | if (peek_stop_reply (ptid)) | |
4699 | return; | |
4700 | ||
4701 | nptid = ptid; | |
4702 | } | |
4703 | ||
4704 | p = write_ptid (p, endp, nptid); | |
4705 | } | |
4706 | ||
4707 | /* In non-stop, we get an immediate OK reply. The stop reply will | |
4708 | come in asynchronously by notification. */ | |
4709 | putpkt (rs->buf); | |
4710 | getpkt (&rs->buf, &rs->buf_size, 0); | |
4711 | if (strcmp (rs->buf, "OK") != 0) | |
4712 | error (_("Stopping %s failed: %s"), target_pid_to_str (ptid), rs->buf); | |
4713 | } | |
4714 | ||
4715 | /* All-stop version of target_stop. Sends a break or a ^C to stop the | |
4716 | remote target. It is undefined which thread of which process | |
4717 | reports the stop. */ | |
4718 | ||
4719 | static void | |
4720 | remote_stop_as (ptid_t ptid) | |
4721 | { | |
4722 | struct remote_state *rs = get_remote_state (); | |
4723 | ||
3a29589a DJ |
4724 | rs->ctrlc_pending_p = 1; |
4725 | ||
74531fed PA |
4726 | /* If the inferior is stopped already, but the core didn't know |
4727 | about it yet, just ignore the request. The cached wait status | |
4728 | will be collected in remote_wait. */ | |
4729 | if (rs->cached_wait_status) | |
4730 | return; | |
4731 | ||
9a7071a8 JB |
4732 | /* Send interrupt_sequence to remote target. */ |
4733 | send_interrupt_sequence (); | |
74531fed PA |
4734 | } |
4735 | ||
7a292a7a SS |
4736 | /* This is the generic stop called via the target vector. When a target |
4737 | interrupt is requested, either by the command line or the GUI, we | |
23860348 | 4738 | will eventually end up here. */ |
74531fed | 4739 | |
c906108c | 4740 | static void |
94cc34af | 4741 | remote_stop (ptid_t ptid) |
c906108c | 4742 | { |
7a292a7a | 4743 | if (remote_debug) |
0f71a2f6 | 4744 | fprintf_unfiltered (gdb_stdlog, "remote_stop called\n"); |
c906108c | 4745 | |
74531fed PA |
4746 | if (non_stop) |
4747 | remote_stop_ns (ptid); | |
c906108c | 4748 | else |
74531fed | 4749 | remote_stop_as (ptid); |
c906108c SS |
4750 | } |
4751 | ||
4752 | /* Ask the user what to do when an interrupt is received. */ | |
4753 | ||
4754 | static void | |
fba45db2 | 4755 | interrupt_query (void) |
c906108c SS |
4756 | { |
4757 | target_terminal_ours (); | |
4758 | ||
74531fed | 4759 | if (target_can_async_p ()) |
c906108c | 4760 | { |
74531fed | 4761 | signal (SIGINT, handle_sigint); |
315a522e | 4762 | deprecated_throw_reason (RETURN_QUIT); |
c906108c | 4763 | } |
74531fed PA |
4764 | else |
4765 | { | |
9e2f0ad4 HZ |
4766 | if (query (_("Interrupted while waiting for the program.\n\ |
4767 | Give up (and stop debugging it)? "))) | |
74531fed PA |
4768 | { |
4769 | pop_target (); | |
4770 | deprecated_throw_reason (RETURN_QUIT); | |
4771 | } | |
4772 | } | |
c906108c SS |
4773 | |
4774 | target_terminal_inferior (); | |
4775 | } | |
4776 | ||
6426a772 JM |
4777 | /* Enable/disable target terminal ownership. Most targets can use |
4778 | terminal groups to control terminal ownership. Remote targets are | |
4779 | different in that explicit transfer of ownership to/from GDB/target | |
23860348 | 4780 | is required. */ |
6426a772 JM |
4781 | |
4782 | static void | |
75c99385 | 4783 | remote_terminal_inferior (void) |
6426a772 | 4784 | { |
c6ebd6cf | 4785 | if (!target_async_permitted) |
75c99385 PA |
4786 | /* Nothing to do. */ |
4787 | return; | |
4788 | ||
d9d2d8b6 PA |
4789 | /* FIXME: cagney/1999-09-27: Make calls to target_terminal_*() |
4790 | idempotent. The event-loop GDB talking to an asynchronous target | |
4791 | with a synchronous command calls this function from both | |
4792 | event-top.c and infrun.c/infcmd.c. Once GDB stops trying to | |
4793 | transfer the terminal to the target when it shouldn't this guard | |
4794 | can go away. */ | |
6426a772 JM |
4795 | if (!remote_async_terminal_ours_p) |
4796 | return; | |
4797 | delete_file_handler (input_fd); | |
4798 | remote_async_terminal_ours_p = 0; | |
4799 | initialize_sigint_signal_handler (); | |
4800 | /* NOTE: At this point we could also register our selves as the | |
4801 | recipient of all input. Any characters typed could then be | |
23860348 | 4802 | passed on down to the target. */ |
6426a772 JM |
4803 | } |
4804 | ||
4805 | static void | |
75c99385 | 4806 | remote_terminal_ours (void) |
6426a772 | 4807 | { |
c6ebd6cf | 4808 | if (!target_async_permitted) |
75c99385 PA |
4809 | /* Nothing to do. */ |
4810 | return; | |
4811 | ||
4812 | /* See FIXME in remote_terminal_inferior. */ | |
6426a772 JM |
4813 | if (remote_async_terminal_ours_p) |
4814 | return; | |
4815 | cleanup_sigint_signal_handler (NULL); | |
4816 | add_file_handler (input_fd, stdin_event_handler, 0); | |
4817 | remote_async_terminal_ours_p = 1; | |
4818 | } | |
4819 | ||
176a6961 | 4820 | static void |
917317f4 | 4821 | remote_console_output (char *msg) |
c906108c SS |
4822 | { |
4823 | char *p; | |
4824 | ||
c5aa993b | 4825 | for (p = msg; p[0] && p[1]; p += 2) |
c906108c SS |
4826 | { |
4827 | char tb[2]; | |
4828 | char c = fromhex (p[0]) * 16 + fromhex (p[1]); | |
a744cf53 | 4829 | |
c906108c SS |
4830 | tb[0] = c; |
4831 | tb[1] = 0; | |
43ff13b4 | 4832 | fputs_unfiltered (tb, gdb_stdtarg); |
c906108c | 4833 | } |
74531fed PA |
4834 | gdb_flush (gdb_stdtarg); |
4835 | } | |
4836 | ||
4837 | typedef struct cached_reg | |
4838 | { | |
4839 | int num; | |
4840 | gdb_byte data[MAX_REGISTER_SIZE]; | |
4841 | } cached_reg_t; | |
4842 | ||
4843 | DEF_VEC_O(cached_reg_t); | |
4844 | ||
4845 | struct stop_reply | |
4846 | { | |
4847 | struct stop_reply *next; | |
4848 | ||
4849 | ptid_t ptid; | |
4850 | ||
4851 | struct target_waitstatus ws; | |
4852 | ||
4853 | VEC(cached_reg_t) *regcache; | |
4854 | ||
4855 | int stopped_by_watchpoint_p; | |
4856 | CORE_ADDR watch_data_address; | |
4857 | ||
4858 | int solibs_changed; | |
4859 | int replay_event; | |
dc146f7c VP |
4860 | |
4861 | int core; | |
74531fed PA |
4862 | }; |
4863 | ||
4864 | /* The list of already fetched and acknowledged stop events. */ | |
4865 | static struct stop_reply *stop_reply_queue; | |
4866 | ||
4867 | static struct stop_reply * | |
4868 | stop_reply_xmalloc (void) | |
4869 | { | |
4870 | struct stop_reply *r = XMALLOC (struct stop_reply); | |
a744cf53 | 4871 | |
74531fed PA |
4872 | r->next = NULL; |
4873 | return r; | |
4874 | } | |
4875 | ||
4876 | static void | |
4877 | stop_reply_xfree (struct stop_reply *r) | |
4878 | { | |
4879 | if (r != NULL) | |
4880 | { | |
4881 | VEC_free (cached_reg_t, r->regcache); | |
4882 | xfree (r); | |
4883 | } | |
c906108c SS |
4884 | } |
4885 | ||
74531fed PA |
4886 | /* Discard all pending stop replies of inferior PID. If PID is -1, |
4887 | discard everything. */ | |
c906108c | 4888 | |
74531fed PA |
4889 | static void |
4890 | discard_pending_stop_replies (int pid) | |
c906108c | 4891 | { |
74531fed | 4892 | struct stop_reply *prev = NULL, *reply, *next; |
c906108c | 4893 | |
74531fed PA |
4894 | /* Discard the in-flight notification. */ |
4895 | if (pending_stop_reply != NULL | |
4896 | && (pid == -1 | |
4897 | || ptid_get_pid (pending_stop_reply->ptid) == pid)) | |
4898 | { | |
4899 | stop_reply_xfree (pending_stop_reply); | |
4900 | pending_stop_reply = NULL; | |
4901 | } | |
c906108c | 4902 | |
74531fed PA |
4903 | /* Discard the stop replies we have already pulled with |
4904 | vStopped. */ | |
4905 | for (reply = stop_reply_queue; reply; reply = next) | |
43ff13b4 | 4906 | { |
74531fed PA |
4907 | next = reply->next; |
4908 | if (pid == -1 | |
4909 | || ptid_get_pid (reply->ptid) == pid) | |
9fa2223d | 4910 | { |
74531fed PA |
4911 | if (reply == stop_reply_queue) |
4912 | stop_reply_queue = reply->next; | |
4913 | else | |
4914 | prev->next = reply->next; | |
4915 | ||
4916 | stop_reply_xfree (reply); | |
9fa2223d | 4917 | } |
74531fed PA |
4918 | else |
4919 | prev = reply; | |
c8e38a49 | 4920 | } |
74531fed | 4921 | } |
43ff13b4 | 4922 | |
74531fed | 4923 | /* Cleanup wrapper. */ |
2e9f7625 | 4924 | |
74531fed PA |
4925 | static void |
4926 | do_stop_reply_xfree (void *arg) | |
4927 | { | |
4928 | struct stop_reply *r = arg; | |
a744cf53 | 4929 | |
74531fed PA |
4930 | stop_reply_xfree (r); |
4931 | } | |
75c99385 | 4932 | |
74531fed PA |
4933 | /* Look for a queued stop reply belonging to PTID. If one is found, |
4934 | remove it from the queue, and return it. Returns NULL if none is | |
4935 | found. If there are still queued events left to process, tell the | |
4936 | event loop to get back to target_wait soon. */ | |
e24a49d8 | 4937 | |
74531fed PA |
4938 | static struct stop_reply * |
4939 | queued_stop_reply (ptid_t ptid) | |
4940 | { | |
0723dbf5 PA |
4941 | struct stop_reply *it; |
4942 | struct stop_reply **it_link; | |
74531fed | 4943 | |
0723dbf5 PA |
4944 | it = stop_reply_queue; |
4945 | it_link = &stop_reply_queue; | |
4946 | while (it) | |
c8e38a49 | 4947 | { |
0723dbf5 PA |
4948 | if (ptid_match (it->ptid, ptid)) |
4949 | { | |
4950 | *it_link = it->next; | |
4951 | it->next = NULL; | |
4952 | break; | |
4953 | } | |
e24a49d8 | 4954 | |
0723dbf5 PA |
4955 | it_link = &it->next; |
4956 | it = *it_link; | |
4957 | } | |
74531fed PA |
4958 | |
4959 | if (stop_reply_queue) | |
4960 | /* There's still at least an event left. */ | |
4961 | mark_async_event_handler (remote_async_inferior_event_token); | |
4962 | ||
4963 | return it; | |
4964 | } | |
4965 | ||
4966 | /* Push a fully parsed stop reply in the stop reply queue. Since we | |
4967 | know that we now have at least one queued event left to pass to the | |
4968 | core side, tell the event loop to get back to target_wait soon. */ | |
4969 | ||
4970 | static void | |
4971 | push_stop_reply (struct stop_reply *new_event) | |
4972 | { | |
4973 | struct stop_reply *event; | |
4974 | ||
4975 | if (stop_reply_queue) | |
4976 | { | |
4977 | for (event = stop_reply_queue; | |
4978 | event && event->next; | |
4979 | event = event->next) | |
4980 | ; | |
4981 | ||
4982 | event->next = new_event; | |
4983 | } | |
4984 | else | |
4985 | stop_reply_queue = new_event; | |
4986 | ||
4987 | mark_async_event_handler (remote_async_inferior_event_token); | |
4988 | } | |
4989 | ||
4990 | /* Returns true if we have a stop reply for PTID. */ | |
4991 | ||
4992 | static int | |
4993 | peek_stop_reply (ptid_t ptid) | |
4994 | { | |
4995 | struct stop_reply *it; | |
4996 | ||
4997 | for (it = stop_reply_queue; it; it = it->next) | |
4998 | if (ptid_equal (ptid, it->ptid)) | |
4999 | { | |
5000 | if (it->ws.kind == TARGET_WAITKIND_STOPPED) | |
5001 | return 1; | |
5002 | } | |
5003 | ||
5004 | return 0; | |
5005 | } | |
5006 | ||
5007 | /* Parse the stop reply in BUF. Either the function succeeds, and the | |
5008 | result is stored in EVENT, or throws an error. */ | |
5009 | ||
5010 | static void | |
5011 | remote_parse_stop_reply (char *buf, struct stop_reply *event) | |
5012 | { | |
5013 | struct remote_arch_state *rsa = get_remote_arch_state (); | |
5014 | ULONGEST addr; | |
5015 | char *p; | |
5016 | ||
5017 | event->ptid = null_ptid; | |
5018 | event->ws.kind = TARGET_WAITKIND_IGNORE; | |
5019 | event->ws.value.integer = 0; | |
5020 | event->solibs_changed = 0; | |
5021 | event->replay_event = 0; | |
5022 | event->stopped_by_watchpoint_p = 0; | |
5023 | event->regcache = NULL; | |
dc146f7c | 5024 | event->core = -1; |
74531fed PA |
5025 | |
5026 | switch (buf[0]) | |
5027 | { | |
5028 | case 'T': /* Status with PC, SP, FP, ... */ | |
cea39f65 MS |
5029 | /* Expedited reply, containing Signal, {regno, reg} repeat. */ |
5030 | /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where | |
5031 | ss = signal number | |
5032 | n... = register number | |
5033 | r... = register contents | |
5034 | */ | |
5035 | ||
5036 | p = &buf[3]; /* after Txx */ | |
5037 | while (*p) | |
5038 | { | |
5039 | char *p1; | |
5040 | char *p_temp; | |
5041 | int fieldsize; | |
5042 | LONGEST pnum = 0; | |
43ff13b4 | 5043 | |
cea39f65 MS |
5044 | /* If the packet contains a register number, save it in |
5045 | pnum and set p1 to point to the character following it. | |
5046 | Otherwise p1 points to p. */ | |
3c3bea1c | 5047 | |
cea39f65 MS |
5048 | /* If this packet is an awatch packet, don't parse the 'a' |
5049 | as a register number. */ | |
c8e38a49 | 5050 | |
dc146f7c VP |
5051 | if (strncmp (p, "awatch", strlen("awatch")) != 0 |
5052 | && strncmp (p, "core", strlen ("core") != 0)) | |
cea39f65 MS |
5053 | { |
5054 | /* Read the ``P'' register number. */ | |
5055 | pnum = strtol (p, &p_temp, 16); | |
5056 | p1 = p_temp; | |
5057 | } | |
5058 | else | |
5059 | p1 = p; | |
802188a7 | 5060 | |
cea39f65 MS |
5061 | if (p1 == p) /* No register number present here. */ |
5062 | { | |
5063 | p1 = strchr (p, ':'); | |
5064 | if (p1 == NULL) | |
5065 | error (_("Malformed packet(a) (missing colon): %s\n\ | |
c8e38a49 | 5066 | Packet: '%s'\n"), |
cea39f65 MS |
5067 | p, buf); |
5068 | if (strncmp (p, "thread", p1 - p) == 0) | |
5069 | event->ptid = read_ptid (++p1, &p); | |
5070 | else if ((strncmp (p, "watch", p1 - p) == 0) | |
5071 | || (strncmp (p, "rwatch", p1 - p) == 0) | |
5072 | || (strncmp (p, "awatch", p1 - p) == 0)) | |
5073 | { | |
5074 | event->stopped_by_watchpoint_p = 1; | |
5075 | p = unpack_varlen_hex (++p1, &addr); | |
5076 | event->watch_data_address = (CORE_ADDR) addr; | |
5077 | } | |
5078 | else if (strncmp (p, "library", p1 - p) == 0) | |
5079 | { | |
5080 | p1++; | |
5081 | p_temp = p1; | |
5082 | while (*p_temp && *p_temp != ';') | |
5083 | p_temp++; | |
c8e38a49 | 5084 | |
cea39f65 MS |
5085 | event->solibs_changed = 1; |
5086 | p = p_temp; | |
5087 | } | |
5088 | else if (strncmp (p, "replaylog", p1 - p) == 0) | |
5089 | { | |
5090 | /* NO_HISTORY event. | |
5091 | p1 will indicate "begin" or "end", but | |
5092 | it makes no difference for now, so ignore it. */ | |
5093 | event->replay_event = 1; | |
5094 | p_temp = strchr (p1 + 1, ';'); | |
5095 | if (p_temp) | |
c8e38a49 | 5096 | p = p_temp; |
cea39f65 | 5097 | } |
dc146f7c VP |
5098 | else if (strncmp (p, "core", p1 - p) == 0) |
5099 | { | |
5100 | ULONGEST c; | |
a744cf53 | 5101 | |
dc146f7c VP |
5102 | p = unpack_varlen_hex (++p1, &c); |
5103 | event->core = c; | |
5104 | } | |
cea39f65 MS |
5105 | else |
5106 | { | |
5107 | /* Silently skip unknown optional info. */ | |
5108 | p_temp = strchr (p1 + 1, ';'); | |
5109 | if (p_temp) | |
5110 | p = p_temp; | |
5111 | } | |
5112 | } | |
5113 | else | |
5114 | { | |
5115 | struct packet_reg *reg = packet_reg_from_pnum (rsa, pnum); | |
5116 | cached_reg_t cached_reg; | |
74531fed | 5117 | |
cea39f65 | 5118 | p = p1; |
75c99385 | 5119 | |
cea39f65 MS |
5120 | if (*p != ':') |
5121 | error (_("Malformed packet(b) (missing colon): %s\n\ | |
8a3fe4f8 | 5122 | Packet: '%s'\n"), |
cea39f65 MS |
5123 | p, buf); |
5124 | ++p; | |
43ff13b4 | 5125 | |
cea39f65 MS |
5126 | if (reg == NULL) |
5127 | error (_("Remote sent bad register number %s: %s\n\ | |
8a3fe4f8 | 5128 | Packet: '%s'\n"), |
7c47795c | 5129 | hex_string (pnum), p, buf); |
c8e38a49 | 5130 | |
cea39f65 | 5131 | cached_reg.num = reg->regnum; |
4100683b | 5132 | |
cea39f65 MS |
5133 | fieldsize = hex2bin (p, cached_reg.data, |
5134 | register_size (target_gdbarch, | |
5135 | reg->regnum)); | |
5136 | p += 2 * fieldsize; | |
5137 | if (fieldsize < register_size (target_gdbarch, | |
5138 | reg->regnum)) | |
5139 | warning (_("Remote reply is too short: %s"), buf); | |
74531fed | 5140 | |
cea39f65 MS |
5141 | VEC_safe_push (cached_reg_t, event->regcache, &cached_reg); |
5142 | } | |
c8e38a49 | 5143 | |
cea39f65 MS |
5144 | if (*p != ';') |
5145 | error (_("Remote register badly formatted: %s\nhere: %s"), | |
5146 | buf, p); | |
5147 | ++p; | |
5148 | } | |
c8e38a49 PA |
5149 | /* fall through */ |
5150 | case 'S': /* Old style status, just signal only. */ | |
74531fed PA |
5151 | if (event->solibs_changed) |
5152 | event->ws.kind = TARGET_WAITKIND_LOADED; | |
5153 | else if (event->replay_event) | |
5154 | event->ws.kind = TARGET_WAITKIND_NO_HISTORY; | |
c8e38a49 PA |
5155 | else |
5156 | { | |
74531fed PA |
5157 | event->ws.kind = TARGET_WAITKIND_STOPPED; |
5158 | event->ws.value.sig = (enum target_signal) | |
c8e38a49 PA |
5159 | (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))); |
5160 | } | |
5161 | break; | |
5162 | case 'W': /* Target exited. */ | |
5163 | case 'X': | |
5164 | { | |
5165 | char *p; | |
5166 | int pid; | |
5167 | ULONGEST value; | |
82f73884 | 5168 | |
c8e38a49 PA |
5169 | /* GDB used to accept only 2 hex chars here. Stubs should |
5170 | only send more if they detect GDB supports multi-process | |
5171 | support. */ | |
5172 | p = unpack_varlen_hex (&buf[1], &value); | |
82f73884 | 5173 | |
c8e38a49 PA |
5174 | if (buf[0] == 'W') |
5175 | { | |
5176 | /* The remote process exited. */ | |
74531fed PA |
5177 | event->ws.kind = TARGET_WAITKIND_EXITED; |
5178 | event->ws.value.integer = value; | |
c8e38a49 PA |
5179 | } |
5180 | else | |
5181 | { | |
5182 | /* The remote process exited with a signal. */ | |
74531fed PA |
5183 | event->ws.kind = TARGET_WAITKIND_SIGNALLED; |
5184 | event->ws.value.sig = (enum target_signal) value; | |
c8e38a49 | 5185 | } |
82f73884 | 5186 | |
c8e38a49 PA |
5187 | /* If no process is specified, assume inferior_ptid. */ |
5188 | pid = ptid_get_pid (inferior_ptid); | |
5189 | if (*p == '\0') | |
5190 | ; | |
5191 | else if (*p == ';') | |
5192 | { | |
5193 | p++; | |
5194 | ||
5195 | if (p == '\0') | |
82f73884 | 5196 | ; |
c8e38a49 PA |
5197 | else if (strncmp (p, |
5198 | "process:", sizeof ("process:") - 1) == 0) | |
82f73884 | 5199 | { |
c8e38a49 | 5200 | ULONGEST upid; |
a744cf53 | 5201 | |
c8e38a49 PA |
5202 | p += sizeof ("process:") - 1; |
5203 | unpack_varlen_hex (p, &upid); | |
5204 | pid = upid; | |
82f73884 PA |
5205 | } |
5206 | else | |
5207 | error (_("unknown stop reply packet: %s"), buf); | |
43ff13b4 | 5208 | } |
c8e38a49 PA |
5209 | else |
5210 | error (_("unknown stop reply packet: %s"), buf); | |
74531fed PA |
5211 | event->ptid = pid_to_ptid (pid); |
5212 | } | |
5213 | break; | |
5214 | } | |
5215 | ||
5216 | if (non_stop && ptid_equal (event->ptid, null_ptid)) | |
5217 | error (_("No process or thread specified in stop reply: %s"), buf); | |
5218 | } | |
5219 | ||
5220 | /* When the stub wants to tell GDB about a new stop reply, it sends a | |
5221 | stop notification (%Stop). Those can come it at any time, hence, | |
5222 | we have to make sure that any pending putpkt/getpkt sequence we're | |
5223 | making is finished, before querying the stub for more events with | |
5224 | vStopped. E.g., if we started a vStopped sequence immediatelly | |
5225 | upon receiving the %Stop notification, something like this could | |
5226 | happen: | |
5227 | ||
5228 | 1.1) --> Hg 1 | |
5229 | 1.2) <-- OK | |
5230 | 1.3) --> g | |
5231 | 1.4) <-- %Stop | |
5232 | 1.5) --> vStopped | |
5233 | 1.6) <-- (registers reply to step #1.3) | |
5234 | ||
5235 | Obviously, the reply in step #1.6 would be unexpected to a vStopped | |
5236 | query. | |
5237 | ||
5238 | To solve this, whenever we parse a %Stop notification sucessfully, | |
5239 | we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on | |
5240 | doing whatever we were doing: | |
5241 | ||
5242 | 2.1) --> Hg 1 | |
5243 | 2.2) <-- OK | |
5244 | 2.3) --> g | |
5245 | 2.4) <-- %Stop | |
5246 | <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN> | |
5247 | 2.5) <-- (registers reply to step #2.3) | |
5248 | ||
5249 | Eventualy after step #2.5, we return to the event loop, which | |
5250 | notices there's an event on the | |
5251 | REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the | |
5252 | associated callback --- the function below. At this point, we're | |
5253 | always safe to start a vStopped sequence. : | |
5254 | ||
5255 | 2.6) --> vStopped | |
5256 | 2.7) <-- T05 thread:2 | |
5257 | 2.8) --> vStopped | |
5258 | 2.9) --> OK | |
5259 | */ | |
5260 | ||
5261 | static void | |
5262 | remote_get_pending_stop_replies (void) | |
5263 | { | |
5264 | struct remote_state *rs = get_remote_state (); | |
74531fed PA |
5265 | |
5266 | if (pending_stop_reply) | |
5267 | { | |
5268 | /* acknowledge */ | |
5269 | putpkt ("vStopped"); | |
5270 | ||
5271 | /* Now we can rely on it. */ | |
5272 | push_stop_reply (pending_stop_reply); | |
5273 | pending_stop_reply = NULL; | |
5274 | ||
5275 | while (1) | |
5276 | { | |
5277 | getpkt (&rs->buf, &rs->buf_size, 0); | |
5278 | if (strcmp (rs->buf, "OK") == 0) | |
5279 | break; | |
5280 | else | |
5281 | { | |
5282 | struct cleanup *old_chain; | |
5283 | struct stop_reply *stop_reply = stop_reply_xmalloc (); | |
5284 | ||
5285 | old_chain = make_cleanup (do_stop_reply_xfree, stop_reply); | |
5286 | remote_parse_stop_reply (rs->buf, stop_reply); | |
5287 | ||
5288 | /* acknowledge */ | |
5289 | putpkt ("vStopped"); | |
5290 | ||
5291 | if (stop_reply->ws.kind != TARGET_WAITKIND_IGNORE) | |
5292 | { | |
5293 | /* Now we can rely on it. */ | |
5294 | discard_cleanups (old_chain); | |
5295 | push_stop_reply (stop_reply); | |
5296 | } | |
5297 | else | |
5298 | /* We got an unknown stop reply. */ | |
5299 | do_cleanups (old_chain); | |
5300 | } | |
5301 | } | |
5302 | } | |
5303 | } | |
5304 | ||
5305 | ||
5306 | /* Called when it is decided that STOP_REPLY holds the info of the | |
5307 | event that is to be returned to the core. This function always | |
5308 | destroys STOP_REPLY. */ | |
5309 | ||
5310 | static ptid_t | |
5311 | process_stop_reply (struct stop_reply *stop_reply, | |
5312 | struct target_waitstatus *status) | |
5313 | { | |
5314 | ptid_t ptid; | |
5315 | ||
5316 | *status = stop_reply->ws; | |
5317 | ptid = stop_reply->ptid; | |
5318 | ||
5319 | /* If no thread/process was reported by the stub, assume the current | |
5320 | inferior. */ | |
5321 | if (ptid_equal (ptid, null_ptid)) | |
5322 | ptid = inferior_ptid; | |
5323 | ||
5f3563ea PA |
5324 | if (status->kind != TARGET_WAITKIND_EXITED |
5325 | && status->kind != TARGET_WAITKIND_SIGNALLED) | |
74531fed | 5326 | { |
5f3563ea PA |
5327 | /* Expedited registers. */ |
5328 | if (stop_reply->regcache) | |
5329 | { | |
217f1f79 UW |
5330 | struct regcache *regcache |
5331 | = get_thread_arch_regcache (ptid, target_gdbarch); | |
5f3563ea PA |
5332 | cached_reg_t *reg; |
5333 | int ix; | |
5334 | ||
5335 | for (ix = 0; | |
5336 | VEC_iterate(cached_reg_t, stop_reply->regcache, ix, reg); | |
5337 | ix++) | |
217f1f79 | 5338 | regcache_raw_supply (regcache, reg->num, reg->data); |
5f3563ea PA |
5339 | VEC_free (cached_reg_t, stop_reply->regcache); |
5340 | } | |
74531fed | 5341 | |
5f3563ea PA |
5342 | remote_stopped_by_watchpoint_p = stop_reply->stopped_by_watchpoint_p; |
5343 | remote_watch_data_address = stop_reply->watch_data_address; | |
1941c569 PA |
5344 | |
5345 | remote_notice_new_inferior (ptid, 0); | |
dc146f7c | 5346 | demand_private_info (ptid)->core = stop_reply->core; |
74531fed PA |
5347 | } |
5348 | ||
74531fed PA |
5349 | stop_reply_xfree (stop_reply); |
5350 | return ptid; | |
5351 | } | |
5352 | ||
5353 | /* The non-stop mode version of target_wait. */ | |
5354 | ||
5355 | static ptid_t | |
47608cb1 | 5356 | remote_wait_ns (ptid_t ptid, struct target_waitstatus *status, int options) |
74531fed PA |
5357 | { |
5358 | struct remote_state *rs = get_remote_state (); | |
74531fed PA |
5359 | struct stop_reply *stop_reply; |
5360 | int ret; | |
5361 | ||
5362 | /* If in non-stop mode, get out of getpkt even if a | |
5363 | notification is received. */ | |
5364 | ||
5365 | ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size, | |
5366 | 0 /* forever */); | |
5367 | while (1) | |
5368 | { | |
5369 | if (ret != -1) | |
5370 | switch (rs->buf[0]) | |
5371 | { | |
5372 | case 'E': /* Error of some sort. */ | |
5373 | /* We're out of sync with the target now. Did it continue | |
5374 | or not? We can't tell which thread it was in non-stop, | |
5375 | so just ignore this. */ | |
5376 | warning (_("Remote failure reply: %s"), rs->buf); | |
5377 | break; | |
5378 | case 'O': /* Console output. */ | |
5379 | remote_console_output (rs->buf + 1); | |
5380 | break; | |
5381 | default: | |
5382 | warning (_("Invalid remote reply: %s"), rs->buf); | |
5383 | break; | |
5384 | } | |
5385 | ||
5386 | /* Acknowledge a pending stop reply that may have arrived in the | |
5387 | mean time. */ | |
5388 | if (pending_stop_reply != NULL) | |
5389 | remote_get_pending_stop_replies (); | |
5390 | ||
5391 | /* If indeed we noticed a stop reply, we're done. */ | |
5392 | stop_reply = queued_stop_reply (ptid); | |
5393 | if (stop_reply != NULL) | |
5394 | return process_stop_reply (stop_reply, status); | |
5395 | ||
47608cb1 | 5396 | /* Still no event. If we're just polling for an event, then |
74531fed | 5397 | return to the event loop. */ |
47608cb1 | 5398 | if (options & TARGET_WNOHANG) |
74531fed PA |
5399 | { |
5400 | status->kind = TARGET_WAITKIND_IGNORE; | |
5401 | return minus_one_ptid; | |
5402 | } | |
5403 | ||
47608cb1 | 5404 | /* Otherwise do a blocking wait. */ |
74531fed PA |
5405 | ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size, |
5406 | 1 /* forever */); | |
5407 | } | |
5408 | } | |
5409 | ||
5410 | /* Wait until the remote machine stops, then return, storing status in | |
5411 | STATUS just as `wait' would. */ | |
5412 | ||
5413 | static ptid_t | |
47608cb1 | 5414 | remote_wait_as (ptid_t ptid, struct target_waitstatus *status, int options) |
74531fed PA |
5415 | { |
5416 | struct remote_state *rs = get_remote_state (); | |
74531fed | 5417 | ptid_t event_ptid = null_ptid; |
cea39f65 | 5418 | char *buf; |
74531fed PA |
5419 | struct stop_reply *stop_reply; |
5420 | ||
47608cb1 PA |
5421 | again: |
5422 | ||
74531fed PA |
5423 | status->kind = TARGET_WAITKIND_IGNORE; |
5424 | status->value.integer = 0; | |
5425 | ||
5426 | stop_reply = queued_stop_reply (ptid); | |
5427 | if (stop_reply != NULL) | |
5428 | return process_stop_reply (stop_reply, status); | |
5429 | ||
5430 | if (rs->cached_wait_status) | |
5431 | /* Use the cached wait status, but only once. */ | |
5432 | rs->cached_wait_status = 0; | |
5433 | else | |
5434 | { | |
5435 | int ret; | |
5436 | ||
5437 | if (!target_is_async_p ()) | |
5438 | { | |
5439 | ofunc = signal (SIGINT, remote_interrupt); | |
5440 | /* If the user hit C-c before this packet, or between packets, | |
5441 | pretend that it was hit right here. */ | |
5442 | if (quit_flag) | |
5443 | { | |
5444 | quit_flag = 0; | |
5445 | remote_interrupt (SIGINT); | |
5446 | } | |
5447 | } | |
5448 | ||
5449 | /* FIXME: cagney/1999-09-27: If we're in async mode we should | |
5450 | _never_ wait for ever -> test on target_is_async_p(). | |
5451 | However, before we do that we need to ensure that the caller | |
5452 | knows how to take the target into/out of async mode. */ | |
5453 | ret = getpkt_sane (&rs->buf, &rs->buf_size, wait_forever_enabled_p); | |
5454 | if (!target_is_async_p ()) | |
5455 | signal (SIGINT, ofunc); | |
5456 | } | |
5457 | ||
5458 | buf = rs->buf; | |
5459 | ||
5460 | remote_stopped_by_watchpoint_p = 0; | |
5461 | ||
5462 | /* We got something. */ | |
5463 | rs->waiting_for_stop_reply = 0; | |
5464 | ||
3a29589a DJ |
5465 | /* Assume that the target has acknowledged Ctrl-C unless we receive |
5466 | an 'F' or 'O' packet. */ | |
5467 | if (buf[0] != 'F' && buf[0] != 'O') | |
5468 | rs->ctrlc_pending_p = 0; | |
5469 | ||
74531fed PA |
5470 | switch (buf[0]) |
5471 | { | |
5472 | case 'E': /* Error of some sort. */ | |
5473 | /* We're out of sync with the target now. Did it continue or | |
5474 | not? Not is more likely, so report a stop. */ | |
5475 | warning (_("Remote failure reply: %s"), buf); | |
5476 | status->kind = TARGET_WAITKIND_STOPPED; | |
5477 | status->value.sig = TARGET_SIGNAL_0; | |
5478 | break; | |
5479 | case 'F': /* File-I/O request. */ | |
3a29589a DJ |
5480 | remote_fileio_request (buf, rs->ctrlc_pending_p); |
5481 | rs->ctrlc_pending_p = 0; | |
74531fed PA |
5482 | break; |
5483 | case 'T': case 'S': case 'X': case 'W': | |
5484 | { | |
5485 | struct stop_reply *stop_reply; | |
5486 | struct cleanup *old_chain; | |
5487 | ||
5488 | stop_reply = stop_reply_xmalloc (); | |
5489 | old_chain = make_cleanup (do_stop_reply_xfree, stop_reply); | |
5490 | remote_parse_stop_reply (buf, stop_reply); | |
5491 | discard_cleanups (old_chain); | |
5492 | event_ptid = process_stop_reply (stop_reply, status); | |
c8e38a49 PA |
5493 | break; |
5494 | } | |
5495 | case 'O': /* Console output. */ | |
5496 | remote_console_output (buf + 1); | |
e24a49d8 | 5497 | |
c8e38a49 PA |
5498 | /* The target didn't really stop; keep waiting. */ |
5499 | rs->waiting_for_stop_reply = 1; | |
e24a49d8 | 5500 | |
c8e38a49 PA |
5501 | break; |
5502 | case '\0': | |
5503 | if (last_sent_signal != TARGET_SIGNAL_0) | |
5504 | { | |
5505 | /* Zero length reply means that we tried 'S' or 'C' and the | |
5506 | remote system doesn't support it. */ | |
5507 | target_terminal_ours_for_output (); | |
5508 | printf_filtered | |
5509 | ("Can't send signals to this remote system. %s not sent.\n", | |
5510 | target_signal_to_name (last_sent_signal)); | |
5511 | last_sent_signal = TARGET_SIGNAL_0; | |
5512 | target_terminal_inferior (); | |
5513 | ||
5514 | strcpy ((char *) buf, last_sent_step ? "s" : "c"); | |
5515 | putpkt ((char *) buf); | |
5516 | ||
5517 | /* We just told the target to resume, so a stop reply is in | |
5518 | order. */ | |
e24a49d8 | 5519 | rs->waiting_for_stop_reply = 1; |
c8e38a49 | 5520 | break; |
43ff13b4 | 5521 | } |
c8e38a49 PA |
5522 | /* else fallthrough */ |
5523 | default: | |
5524 | warning (_("Invalid remote reply: %s"), buf); | |
5525 | /* Keep waiting. */ | |
5526 | rs->waiting_for_stop_reply = 1; | |
5527 | break; | |
43ff13b4 | 5528 | } |
c8e38a49 | 5529 | |
c8e38a49 | 5530 | if (status->kind == TARGET_WAITKIND_IGNORE) |
47608cb1 PA |
5531 | { |
5532 | /* Nothing interesting happened. If we're doing a non-blocking | |
5533 | poll, we're done. Otherwise, go back to waiting. */ | |
5534 | if (options & TARGET_WNOHANG) | |
5535 | return minus_one_ptid; | |
5536 | else | |
5537 | goto again; | |
5538 | } | |
74531fed PA |
5539 | else if (status->kind != TARGET_WAITKIND_EXITED |
5540 | && status->kind != TARGET_WAITKIND_SIGNALLED) | |
82f73884 PA |
5541 | { |
5542 | if (!ptid_equal (event_ptid, null_ptid)) | |
5543 | record_currthread (event_ptid); | |
5544 | else | |
5545 | event_ptid = inferior_ptid; | |
43ff13b4 | 5546 | } |
74531fed PA |
5547 | else |
5548 | /* A process exit. Invalidate our notion of current thread. */ | |
5549 | record_currthread (minus_one_ptid); | |
79d7f229 | 5550 | |
82f73884 | 5551 | return event_ptid; |
43ff13b4 JM |
5552 | } |
5553 | ||
74531fed PA |
5554 | /* Wait until the remote machine stops, then return, storing status in |
5555 | STATUS just as `wait' would. */ | |
5556 | ||
c8e38a49 | 5557 | static ptid_t |
117de6a9 | 5558 | remote_wait (struct target_ops *ops, |
47608cb1 | 5559 | ptid_t ptid, struct target_waitstatus *status, int options) |
c8e38a49 PA |
5560 | { |
5561 | ptid_t event_ptid; | |
5562 | ||
74531fed | 5563 | if (non_stop) |
47608cb1 | 5564 | event_ptid = remote_wait_ns (ptid, status, options); |
74531fed | 5565 | else |
47608cb1 | 5566 | event_ptid = remote_wait_as (ptid, status, options); |
c8e38a49 | 5567 | |
74531fed | 5568 | if (target_can_async_p ()) |
c8e38a49 | 5569 | { |
74531fed PA |
5570 | /* If there are are events left in the queue tell the event loop |
5571 | to return here. */ | |
5572 | if (stop_reply_queue) | |
5573 | mark_async_event_handler (remote_async_inferior_event_token); | |
c8e38a49 | 5574 | } |
c8e38a49 PA |
5575 | |
5576 | return event_ptid; | |
5577 | } | |
5578 | ||
74ca34ce | 5579 | /* Fetch a single register using a 'p' packet. */ |
c906108c | 5580 | |
b96ec7ac | 5581 | static int |
56be3814 | 5582 | fetch_register_using_p (struct regcache *regcache, struct packet_reg *reg) |
b96ec7ac AC |
5583 | { |
5584 | struct remote_state *rs = get_remote_state (); | |
2e9f7625 | 5585 | char *buf, *p; |
b96ec7ac AC |
5586 | char regp[MAX_REGISTER_SIZE]; |
5587 | int i; | |
5588 | ||
74ca34ce DJ |
5589 | if (remote_protocol_packets[PACKET_p].support == PACKET_DISABLE) |
5590 | return 0; | |
5591 | ||
5592 | if (reg->pnum == -1) | |
5593 | return 0; | |
5594 | ||
2e9f7625 | 5595 | p = rs->buf; |
fcad0fa4 | 5596 | *p++ = 'p'; |
74ca34ce | 5597 | p += hexnumstr (p, reg->pnum); |
fcad0fa4 | 5598 | *p++ = '\0'; |
1f4437a4 MS |
5599 | putpkt (rs->buf); |
5600 | getpkt (&rs->buf, &rs->buf_size, 0); | |
3f9a994c | 5601 | |
2e9f7625 DJ |
5602 | buf = rs->buf; |
5603 | ||
74ca34ce DJ |
5604 | switch (packet_ok (buf, &remote_protocol_packets[PACKET_p])) |
5605 | { | |
5606 | case PACKET_OK: | |
5607 | break; | |
5608 | case PACKET_UNKNOWN: | |
5609 | return 0; | |
5610 | case PACKET_ERROR: | |
27a9c0bf MS |
5611 | error (_("Could not fetch register \"%s\"; remote failure reply '%s'"), |
5612 | gdbarch_register_name (get_regcache_arch (regcache), | |
5613 | reg->regnum), | |
5614 | buf); | |
74ca34ce | 5615 | } |
3f9a994c JB |
5616 | |
5617 | /* If this register is unfetchable, tell the regcache. */ | |
5618 | if (buf[0] == 'x') | |
8480adf2 | 5619 | { |
56be3814 | 5620 | regcache_raw_supply (regcache, reg->regnum, NULL); |
8480adf2 | 5621 | return 1; |
b96ec7ac | 5622 | } |
b96ec7ac | 5623 | |
3f9a994c JB |
5624 | /* Otherwise, parse and supply the value. */ |
5625 | p = buf; | |
5626 | i = 0; | |
5627 | while (p[0] != 0) | |
5628 | { | |
5629 | if (p[1] == 0) | |
74ca34ce | 5630 | error (_("fetch_register_using_p: early buf termination")); |
3f9a994c JB |
5631 | |
5632 | regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]); | |
5633 | p += 2; | |
5634 | } | |
56be3814 | 5635 | regcache_raw_supply (regcache, reg->regnum, regp); |
3f9a994c | 5636 | return 1; |
b96ec7ac AC |
5637 | } |
5638 | ||
74ca34ce DJ |
5639 | /* Fetch the registers included in the target's 'g' packet. */ |
5640 | ||
29709017 DJ |
5641 | static int |
5642 | send_g_packet (void) | |
c906108c | 5643 | { |
d01949b6 | 5644 | struct remote_state *rs = get_remote_state (); |
cea39f65 | 5645 | int buf_len; |
c906108c | 5646 | |
74ca34ce DJ |
5647 | sprintf (rs->buf, "g"); |
5648 | remote_send (&rs->buf, &rs->buf_size); | |
c906108c | 5649 | |
29709017 DJ |
5650 | /* We can get out of synch in various cases. If the first character |
5651 | in the buffer is not a hex character, assume that has happened | |
5652 | and try to fetch another packet to read. */ | |
5653 | while ((rs->buf[0] < '0' || rs->buf[0] > '9') | |
5654 | && (rs->buf[0] < 'A' || rs->buf[0] > 'F') | |
5655 | && (rs->buf[0] < 'a' || rs->buf[0] > 'f') | |
5656 | && rs->buf[0] != 'x') /* New: unavailable register value. */ | |
5657 | { | |
5658 | if (remote_debug) | |
5659 | fprintf_unfiltered (gdb_stdlog, | |
5660 | "Bad register packet; fetching a new packet\n"); | |
5661 | getpkt (&rs->buf, &rs->buf_size, 0); | |
5662 | } | |
5663 | ||
74ca34ce DJ |
5664 | buf_len = strlen (rs->buf); |
5665 | ||
5666 | /* Sanity check the received packet. */ | |
5667 | if (buf_len % 2 != 0) | |
5668 | error (_("Remote 'g' packet reply is of odd length: %s"), rs->buf); | |
29709017 DJ |
5669 | |
5670 | return buf_len / 2; | |
5671 | } | |
5672 | ||
5673 | static void | |
56be3814 | 5674 | process_g_packet (struct regcache *regcache) |
29709017 | 5675 | { |
4a22f64d | 5676 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
29709017 DJ |
5677 | struct remote_state *rs = get_remote_state (); |
5678 | struct remote_arch_state *rsa = get_remote_arch_state (); | |
5679 | int i, buf_len; | |
5680 | char *p; | |
5681 | char *regs; | |
5682 | ||
5683 | buf_len = strlen (rs->buf); | |
5684 | ||
5685 | /* Further sanity checks, with knowledge of the architecture. */ | |
74ca34ce DJ |
5686 | if (buf_len > 2 * rsa->sizeof_g_packet) |
5687 | error (_("Remote 'g' packet reply is too long: %s"), rs->buf); | |
5688 | ||
5689 | /* Save the size of the packet sent to us by the target. It is used | |
5690 | as a heuristic when determining the max size of packets that the | |
5691 | target can safely receive. */ | |
5692 | if (rsa->actual_register_packet_size == 0) | |
5693 | rsa->actual_register_packet_size = buf_len; | |
5694 | ||
5695 | /* If this is smaller than we guessed the 'g' packet would be, | |
5696 | update our records. A 'g' reply that doesn't include a register's | |
5697 | value implies either that the register is not available, or that | |
5698 | the 'p' packet must be used. */ | |
5699 | if (buf_len < 2 * rsa->sizeof_g_packet) | |
b323314b | 5700 | { |
74ca34ce DJ |
5701 | rsa->sizeof_g_packet = buf_len / 2; |
5702 | ||
4a22f64d | 5703 | for (i = 0; i < gdbarch_num_regs (gdbarch); i++) |
b96ec7ac | 5704 | { |
74ca34ce DJ |
5705 | if (rsa->regs[i].pnum == -1) |
5706 | continue; | |
5707 | ||
5708 | if (rsa->regs[i].offset >= rsa->sizeof_g_packet) | |
5709 | rsa->regs[i].in_g_packet = 0; | |
b96ec7ac | 5710 | else |
74ca34ce | 5711 | rsa->regs[i].in_g_packet = 1; |
b96ec7ac | 5712 | } |
74ca34ce | 5713 | } |
b323314b | 5714 | |
74ca34ce | 5715 | regs = alloca (rsa->sizeof_g_packet); |
c906108c SS |
5716 | |
5717 | /* Unimplemented registers read as all bits zero. */ | |
ea9c271d | 5718 | memset (regs, 0, rsa->sizeof_g_packet); |
c906108c | 5719 | |
c906108c SS |
5720 | /* Reply describes registers byte by byte, each byte encoded as two |
5721 | hex characters. Suck them all up, then supply them to the | |
5722 | register cacheing/storage mechanism. */ | |
5723 | ||
74ca34ce | 5724 | p = rs->buf; |
ea9c271d | 5725 | for (i = 0; i < rsa->sizeof_g_packet; i++) |
c906108c | 5726 | { |
74ca34ce DJ |
5727 | if (p[0] == 0 || p[1] == 0) |
5728 | /* This shouldn't happen - we adjusted sizeof_g_packet above. */ | |
5729 | internal_error (__FILE__, __LINE__, | |
5730 | "unexpected end of 'g' packet reply"); | |
5731 | ||
c906108c | 5732 | if (p[0] == 'x' && p[1] == 'x') |
c5aa993b | 5733 | regs[i] = 0; /* 'x' */ |
c906108c SS |
5734 | else |
5735 | regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); | |
5736 | p += 2; | |
5737 | } | |
5738 | ||
a744cf53 MS |
5739 | for (i = 0; i < gdbarch_num_regs (gdbarch); i++) |
5740 | { | |
5741 | struct packet_reg *r = &rsa->regs[i]; | |
5742 | ||
5743 | if (r->in_g_packet) | |
5744 | { | |
5745 | if (r->offset * 2 >= strlen (rs->buf)) | |
5746 | /* This shouldn't happen - we adjusted in_g_packet above. */ | |
5747 | internal_error (__FILE__, __LINE__, | |
5748 | "unexpected end of 'g' packet reply"); | |
5749 | else if (rs->buf[r->offset * 2] == 'x') | |
5750 | { | |
5751 | gdb_assert (r->offset * 2 < strlen (rs->buf)); | |
5752 | /* The register isn't available, mark it as such (at | |
5753 | the same time setting the value to zero). */ | |
5754 | regcache_raw_supply (regcache, r->regnum, NULL); | |
5755 | } | |
5756 | else | |
5757 | regcache_raw_supply (regcache, r->regnum, | |
5758 | regs + r->offset); | |
5759 | } | |
5760 | } | |
c906108c SS |
5761 | } |
5762 | ||
29709017 | 5763 | static void |
56be3814 | 5764 | fetch_registers_using_g (struct regcache *regcache) |
29709017 DJ |
5765 | { |
5766 | send_g_packet (); | |
56be3814 | 5767 | process_g_packet (regcache); |
29709017 DJ |
5768 | } |
5769 | ||
74ca34ce | 5770 | static void |
28439f5e PA |
5771 | remote_fetch_registers (struct target_ops *ops, |
5772 | struct regcache *regcache, int regnum) | |
74ca34ce | 5773 | { |
74ca34ce DJ |
5774 | struct remote_arch_state *rsa = get_remote_arch_state (); |
5775 | int i; | |
5776 | ||
79d7f229 | 5777 | set_general_thread (inferior_ptid); |
74ca34ce DJ |
5778 | |
5779 | if (regnum >= 0) | |
5780 | { | |
5781 | struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum); | |
a744cf53 | 5782 | |
74ca34ce DJ |
5783 | gdb_assert (reg != NULL); |
5784 | ||
5785 | /* If this register might be in the 'g' packet, try that first - | |
5786 | we are likely to read more than one register. If this is the | |
5787 | first 'g' packet, we might be overly optimistic about its | |
5788 | contents, so fall back to 'p'. */ | |
5789 | if (reg->in_g_packet) | |
5790 | { | |
56be3814 | 5791 | fetch_registers_using_g (regcache); |
74ca34ce DJ |
5792 | if (reg->in_g_packet) |
5793 | return; | |
5794 | } | |
5795 | ||
56be3814 | 5796 | if (fetch_register_using_p (regcache, reg)) |
74ca34ce DJ |
5797 | return; |
5798 | ||
5799 | /* This register is not available. */ | |
56be3814 | 5800 | regcache_raw_supply (regcache, reg->regnum, NULL); |
74ca34ce DJ |
5801 | |
5802 | return; | |
5803 | } | |
5804 | ||
56be3814 | 5805 | fetch_registers_using_g (regcache); |
74ca34ce | 5806 | |
4a22f64d | 5807 | for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++) |
74ca34ce | 5808 | if (!rsa->regs[i].in_g_packet) |
56be3814 | 5809 | if (!fetch_register_using_p (regcache, &rsa->regs[i])) |
74ca34ce DJ |
5810 | { |
5811 | /* This register is not available. */ | |
56be3814 | 5812 | regcache_raw_supply (regcache, i, NULL); |
74ca34ce DJ |
5813 | } |
5814 | } | |
5815 | ||
c906108c SS |
5816 | /* Prepare to store registers. Since we may send them all (using a |
5817 | 'G' request), we have to read out the ones we don't want to change | |
5818 | first. */ | |
5819 | ||
c5aa993b | 5820 | static void |
316f2060 | 5821 | remote_prepare_to_store (struct regcache *regcache) |
c906108c | 5822 | { |
ea9c271d | 5823 | struct remote_arch_state *rsa = get_remote_arch_state (); |
cf0e1e0d | 5824 | int i; |
cfd77fa1 | 5825 | gdb_byte buf[MAX_REGISTER_SIZE]; |
cf0e1e0d | 5826 | |
c906108c | 5827 | /* Make sure the entire registers array is valid. */ |
444abaca | 5828 | switch (remote_protocol_packets[PACKET_P].support) |
5a2468f5 JM |
5829 | { |
5830 | case PACKET_DISABLE: | |
5831 | case PACKET_SUPPORT_UNKNOWN: | |
cf0e1e0d | 5832 | /* Make sure all the necessary registers are cached. */ |
4a22f64d | 5833 | for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++) |
ea9c271d | 5834 | if (rsa->regs[i].in_g_packet) |
316f2060 | 5835 | regcache_raw_read (regcache, rsa->regs[i].regnum, buf); |
5a2468f5 JM |
5836 | break; |
5837 | case PACKET_ENABLE: | |
5838 | break; | |
5839 | } | |
5840 | } | |
5841 | ||
ad10f812 | 5842 | /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF |
23860348 | 5843 | packet was not recognized. */ |
5a2468f5 JM |
5844 | |
5845 | static int | |
1f4437a4 MS |
5846 | store_register_using_P (const struct regcache *regcache, |
5847 | struct packet_reg *reg) | |
5a2468f5 | 5848 | { |
4a22f64d | 5849 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
d01949b6 | 5850 | struct remote_state *rs = get_remote_state (); |
5a2468f5 | 5851 | /* Try storing a single register. */ |
6d820c5c | 5852 | char *buf = rs->buf; |
cfd77fa1 | 5853 | gdb_byte regp[MAX_REGISTER_SIZE]; |
5a2468f5 | 5854 | char *p; |
5a2468f5 | 5855 | |
74ca34ce DJ |
5856 | if (remote_protocol_packets[PACKET_P].support == PACKET_DISABLE) |
5857 | return 0; | |
5858 | ||
5859 | if (reg->pnum == -1) | |
5860 | return 0; | |
5861 | ||
ea9c271d | 5862 | xsnprintf (buf, get_remote_packet_size (), "P%s=", phex_nz (reg->pnum, 0)); |
5a2468f5 | 5863 | p = buf + strlen (buf); |
56be3814 | 5864 | regcache_raw_collect (regcache, reg->regnum, regp); |
4a22f64d | 5865 | bin2hex (regp, p, register_size (gdbarch, reg->regnum)); |
1f4437a4 MS |
5866 | putpkt (rs->buf); |
5867 | getpkt (&rs->buf, &rs->buf_size, 0); | |
5a2468f5 | 5868 | |
74ca34ce DJ |
5869 | switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_P])) |
5870 | { | |
5871 | case PACKET_OK: | |
5872 | return 1; | |
5873 | case PACKET_ERROR: | |
27a9c0bf MS |
5874 | error (_("Could not write register \"%s\"; remote failure reply '%s'"), |
5875 | gdbarch_register_name (gdbarch, reg->regnum), rs->buf); | |
74ca34ce DJ |
5876 | case PACKET_UNKNOWN: |
5877 | return 0; | |
5878 | default: | |
5879 | internal_error (__FILE__, __LINE__, _("Bad result from packet_ok")); | |
5880 | } | |
c906108c SS |
5881 | } |
5882 | ||
23860348 MS |
5883 | /* Store register REGNUM, or all registers if REGNUM == -1, from the |
5884 | contents of the register cache buffer. FIXME: ignores errors. */ | |
c906108c SS |
5885 | |
5886 | static void | |
56be3814 | 5887 | store_registers_using_G (const struct regcache *regcache) |
c906108c | 5888 | { |
d01949b6 | 5889 | struct remote_state *rs = get_remote_state (); |
ea9c271d | 5890 | struct remote_arch_state *rsa = get_remote_arch_state (); |
cfd77fa1 | 5891 | gdb_byte *regs; |
c906108c SS |
5892 | char *p; |
5893 | ||
193cb69f AC |
5894 | /* Extract all the registers in the regcache copying them into a |
5895 | local buffer. */ | |
5896 | { | |
b323314b | 5897 | int i; |
a744cf53 | 5898 | |
ea9c271d DJ |
5899 | regs = alloca (rsa->sizeof_g_packet); |
5900 | memset (regs, 0, rsa->sizeof_g_packet); | |
4a22f64d | 5901 | for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++) |
193cb69f | 5902 | { |
ea9c271d | 5903 | struct packet_reg *r = &rsa->regs[i]; |
a744cf53 | 5904 | |
b323314b | 5905 | if (r->in_g_packet) |
56be3814 | 5906 | regcache_raw_collect (regcache, r->regnum, regs + r->offset); |
193cb69f AC |
5907 | } |
5908 | } | |
c906108c SS |
5909 | |
5910 | /* Command describes registers byte by byte, | |
5911 | each byte encoded as two hex characters. */ | |
6d820c5c | 5912 | p = rs->buf; |
193cb69f | 5913 | *p++ = 'G'; |
74ca34ce DJ |
5914 | /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets |
5915 | updated. */ | |
5916 | bin2hex (regs, p, rsa->sizeof_g_packet); | |
1f4437a4 MS |
5917 | putpkt (rs->buf); |
5918 | getpkt (&rs->buf, &rs->buf_size, 0); | |
5919 | if (packet_check_result (rs->buf) == PACKET_ERROR) | |
27a9c0bf MS |
5920 | error (_("Could not write registers; remote failure reply '%s'"), |
5921 | rs->buf); | |
c906108c | 5922 | } |
74ca34ce DJ |
5923 | |
5924 | /* Store register REGNUM, or all registers if REGNUM == -1, from the contents | |
5925 | of the register cache buffer. FIXME: ignores errors. */ | |
5926 | ||
5927 | static void | |
28439f5e PA |
5928 | remote_store_registers (struct target_ops *ops, |
5929 | struct regcache *regcache, int regnum) | |
74ca34ce | 5930 | { |
74ca34ce DJ |
5931 | struct remote_arch_state *rsa = get_remote_arch_state (); |
5932 | int i; | |
5933 | ||
79d7f229 | 5934 | set_general_thread (inferior_ptid); |
74ca34ce DJ |
5935 | |
5936 | if (regnum >= 0) | |
5937 | { | |
5938 | struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum); | |
a744cf53 | 5939 | |
74ca34ce DJ |
5940 | gdb_assert (reg != NULL); |
5941 | ||
5942 | /* Always prefer to store registers using the 'P' packet if | |
5943 | possible; we often change only a small number of registers. | |
5944 | Sometimes we change a larger number; we'd need help from a | |
5945 | higher layer to know to use 'G'. */ | |
56be3814 | 5946 | if (store_register_using_P (regcache, reg)) |
74ca34ce DJ |
5947 | return; |
5948 | ||
5949 | /* For now, don't complain if we have no way to write the | |
5950 | register. GDB loses track of unavailable registers too | |
5951 | easily. Some day, this may be an error. We don't have | |
5952 | any way to read the register, either... */ | |
5953 | if (!reg->in_g_packet) | |
5954 | return; | |
5955 | ||
56be3814 | 5956 | store_registers_using_G (regcache); |
74ca34ce DJ |
5957 | return; |
5958 | } | |
5959 | ||
56be3814 | 5960 | store_registers_using_G (regcache); |
74ca34ce | 5961 | |
4a22f64d | 5962 | for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++) |
74ca34ce | 5963 | if (!rsa->regs[i].in_g_packet) |
56be3814 | 5964 | if (!store_register_using_P (regcache, &rsa->regs[i])) |
74ca34ce DJ |
5965 | /* See above for why we do not issue an error here. */ |
5966 | continue; | |
5967 | } | |
c906108c SS |
5968 | \f |
5969 | ||
5970 | /* Return the number of hex digits in num. */ | |
5971 | ||
5972 | static int | |
fba45db2 | 5973 | hexnumlen (ULONGEST num) |
c906108c SS |
5974 | { |
5975 | int i; | |
5976 | ||
5977 | for (i = 0; num != 0; i++) | |
5978 | num >>= 4; | |
5979 | ||
5980 | return max (i, 1); | |
5981 | } | |
5982 | ||
2df3850c | 5983 | /* Set BUF to the minimum number of hex digits representing NUM. */ |
c906108c SS |
5984 | |
5985 | static int | |
fba45db2 | 5986 | hexnumstr (char *buf, ULONGEST num) |
c906108c | 5987 | { |
c906108c | 5988 | int len = hexnumlen (num); |
a744cf53 | 5989 | |
2df3850c JM |
5990 | return hexnumnstr (buf, num, len); |
5991 | } | |
5992 | ||
c906108c | 5993 | |
2df3850c | 5994 | /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */ |
c906108c | 5995 | |
2df3850c | 5996 | static int |
fba45db2 | 5997 | hexnumnstr (char *buf, ULONGEST num, int width) |
2df3850c JM |
5998 | { |
5999 | int i; | |
6000 | ||
6001 | buf[width] = '\0'; | |
6002 | ||
6003 | for (i = width - 1; i >= 0; i--) | |
c906108c | 6004 | { |
c5aa993b | 6005 | buf[i] = "0123456789abcdef"[(num & 0xf)]; |
c906108c SS |
6006 | num >>= 4; |
6007 | } | |
6008 | ||
2df3850c | 6009 | return width; |
c906108c SS |
6010 | } |
6011 | ||
23860348 | 6012 | /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */ |
c906108c SS |
6013 | |
6014 | static CORE_ADDR | |
fba45db2 | 6015 | remote_address_masked (CORE_ADDR addr) |
c906108c | 6016 | { |
911c95a5 | 6017 | int address_size = remote_address_size; |
a744cf53 | 6018 | |
911c95a5 UW |
6019 | /* If "remoteaddresssize" was not set, default to target address size. */ |
6020 | if (!address_size) | |
1cf3db46 | 6021 | address_size = gdbarch_addr_bit (target_gdbarch); |
911c95a5 UW |
6022 | |
6023 | if (address_size > 0 | |
6024 | && address_size < (sizeof (ULONGEST) * 8)) | |
c906108c SS |
6025 | { |
6026 | /* Only create a mask when that mask can safely be constructed | |
23860348 | 6027 | in a ULONGEST variable. */ |
c906108c | 6028 | ULONGEST mask = 1; |
a744cf53 | 6029 | |
911c95a5 | 6030 | mask = (mask << address_size) - 1; |
c906108c SS |
6031 | addr &= mask; |
6032 | } | |
6033 | return addr; | |
6034 | } | |
6035 | ||
a31ea83d DJ |
6036 | /* Convert BUFFER, binary data at least LEN bytes long, into escaped |
6037 | binary data in OUT_BUF. Set *OUT_LEN to the length of the data | |
6038 | encoded in OUT_BUF, and return the number of bytes in OUT_BUF | |
6039 | (which may be more than *OUT_LEN due to escape characters). The | |
6040 | total number of bytes in the output buffer will be at most | |
6041 | OUT_MAXLEN. */ | |
6042 | ||
6043 | static int | |
6044 | remote_escape_output (const gdb_byte *buffer, int len, | |
6045 | gdb_byte *out_buf, int *out_len, | |
6046 | int out_maxlen) | |
6047 | { | |
6048 | int input_index, output_index; | |
6049 | ||
6050 | output_index = 0; | |
6051 | for (input_index = 0; input_index < len; input_index++) | |
6052 | { | |
6053 | gdb_byte b = buffer[input_index]; | |
6054 | ||
6055 | if (b == '$' || b == '#' || b == '}') | |
6056 | { | |
6057 | /* These must be escaped. */ | |
6058 | if (output_index + 2 > out_maxlen) | |
6059 | break; | |
6060 | out_buf[output_index++] = '}'; | |
6061 | out_buf[output_index++] = b ^ 0x20; | |
6062 | } | |
6063 | else | |
6064 | { | |
6065 | if (output_index + 1 > out_maxlen) | |
6066 | break; | |
6067 | out_buf[output_index++] = b; | |
6068 | } | |
6069 | } | |
6070 | ||
6071 | *out_len = input_index; | |
6072 | return output_index; | |
6073 | } | |
6074 | ||
0876f84a DJ |
6075 | /* Convert BUFFER, escaped data LEN bytes long, into binary data |
6076 | in OUT_BUF. Return the number of bytes written to OUT_BUF. | |
6077 | Raise an error if the total number of bytes exceeds OUT_MAXLEN. | |
6078 | ||
6079 | This function reverses remote_escape_output. It allows more | |
6080 | escaped characters than that function does, in particular because | |
6081 | '*' must be escaped to avoid the run-length encoding processing | |
6082 | in reading packets. */ | |
6083 | ||
6084 | static int | |
6085 | remote_unescape_input (const gdb_byte *buffer, int len, | |
6086 | gdb_byte *out_buf, int out_maxlen) | |
6087 | { | |
6088 | int input_index, output_index; | |
6089 | int escaped; | |
6090 | ||
6091 | output_index = 0; | |
6092 | escaped = 0; | |
6093 | for (input_index = 0; input_index < len; input_index++) | |
6094 | { | |
6095 | gdb_byte b = buffer[input_index]; | |
6096 | ||
6097 | if (output_index + 1 > out_maxlen) | |
6098 | { | |
6099 | warning (_("Received too much data from remote target;" | |
6100 | " ignoring overflow.")); | |
6101 | return output_index; | |
6102 | } | |
6103 | ||
6104 | if (escaped) | |
6105 | { | |
6106 | out_buf[output_index++] = b ^ 0x20; | |
6107 | escaped = 0; | |
6108 | } | |
6109 | else if (b == '}') | |
6110 | escaped = 1; | |
6111 | else | |
6112 | out_buf[output_index++] = b; | |
6113 | } | |
6114 | ||
6115 | if (escaped) | |
6116 | error (_("Unmatched escape character in target response.")); | |
6117 | ||
6118 | return output_index; | |
6119 | } | |
6120 | ||
c906108c SS |
6121 | /* Determine whether the remote target supports binary downloading. |
6122 | This is accomplished by sending a no-op memory write of zero length | |
6123 | to the target at the specified address. It does not suffice to send | |
23860348 MS |
6124 | the whole packet, since many stubs strip the eighth bit and |
6125 | subsequently compute a wrong checksum, which causes real havoc with | |
6126 | remote_write_bytes. | |
7a292a7a | 6127 | |
96baa820 JM |
6128 | NOTE: This can still lose if the serial line is not eight-bit |
6129 | clean. In cases like this, the user should clear "remote | |
23860348 | 6130 | X-packet". */ |
96baa820 | 6131 | |
c906108c | 6132 | static void |
fba45db2 | 6133 | check_binary_download (CORE_ADDR addr) |
c906108c | 6134 | { |
d01949b6 | 6135 | struct remote_state *rs = get_remote_state (); |
24b06219 | 6136 | |
444abaca | 6137 | switch (remote_protocol_packets[PACKET_X].support) |
c906108c | 6138 | { |
96baa820 JM |
6139 | case PACKET_DISABLE: |
6140 | break; | |
6141 | case PACKET_ENABLE: | |
6142 | break; | |
6143 | case PACKET_SUPPORT_UNKNOWN: | |
6144 | { | |
96baa820 | 6145 | char *p; |
802188a7 | 6146 | |
2e9f7625 | 6147 | p = rs->buf; |
96baa820 JM |
6148 | *p++ = 'X'; |
6149 | p += hexnumstr (p, (ULONGEST) addr); | |
6150 | *p++ = ','; | |
6151 | p += hexnumstr (p, (ULONGEST) 0); | |
6152 | *p++ = ':'; | |
6153 | *p = '\0'; | |
802188a7 | 6154 | |
2e9f7625 | 6155 | putpkt_binary (rs->buf, (int) (p - rs->buf)); |
6d820c5c | 6156 | getpkt (&rs->buf, &rs->buf_size, 0); |
c906108c | 6157 | |
2e9f7625 | 6158 | if (rs->buf[0] == '\0') |
96baa820 JM |
6159 | { |
6160 | if (remote_debug) | |
6161 | fprintf_unfiltered (gdb_stdlog, | |
6162 | "binary downloading NOT suppported by target\n"); | |
444abaca | 6163 | remote_protocol_packets[PACKET_X].support = PACKET_DISABLE; |
96baa820 JM |
6164 | } |
6165 | else | |
6166 | { | |
6167 | if (remote_debug) | |
6168 | fprintf_unfiltered (gdb_stdlog, | |
6169 | "binary downloading suppported by target\n"); | |
444abaca | 6170 | remote_protocol_packets[PACKET_X].support = PACKET_ENABLE; |
96baa820 JM |
6171 | } |
6172 | break; | |
6173 | } | |
c906108c SS |
6174 | } |
6175 | } | |
6176 | ||
6177 | /* Write memory data directly to the remote machine. | |
6178 | This does not inform the data cache; the data cache uses this. | |
a76d924d | 6179 | HEADER is the starting part of the packet. |
c906108c SS |
6180 | MEMADDR is the address in the remote memory space. |
6181 | MYADDR is the address of the buffer in our space. | |
6182 | LEN is the number of bytes. | |
a76d924d DJ |
6183 | PACKET_FORMAT should be either 'X' or 'M', and indicates if we |
6184 | should send data as binary ('X'), or hex-encoded ('M'). | |
6185 | ||
6186 | The function creates packet of the form | |
6187 | <HEADER><ADDRESS>,<LENGTH>:<DATA> | |
6188 | ||
6189 | where encoding of <DATA> is termined by PACKET_FORMAT. | |
6190 | ||
6191 | If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma | |
6192 | are omitted. | |
6193 | ||
6194 | Returns the number of bytes transferred, or 0 (setting errno) for | |
23860348 | 6195 | error. Only transfer a single packet. */ |
c906108c | 6196 | |
a76d924d DJ |
6197 | static int |
6198 | remote_write_bytes_aux (const char *header, CORE_ADDR memaddr, | |
6199 | const gdb_byte *myaddr, int len, | |
6200 | char packet_format, int use_length) | |
c906108c | 6201 | { |
6d820c5c | 6202 | struct remote_state *rs = get_remote_state (); |
cfd77fa1 | 6203 | char *p; |
a76d924d DJ |
6204 | char *plen = NULL; |
6205 | int plenlen = 0; | |
917317f4 JM |
6206 | int todo; |
6207 | int nr_bytes; | |
a257b5bb | 6208 | int payload_size; |
6765f3e5 | 6209 | int payload_length; |
a76d924d DJ |
6210 | int header_length; |
6211 | ||
6212 | if (packet_format != 'X' && packet_format != 'M') | |
6213 | internal_error (__FILE__, __LINE__, | |
6214 | "remote_write_bytes_aux: bad packet format"); | |
c906108c | 6215 | |
b2182ed2 DJ |
6216 | if (len <= 0) |
6217 | return 0; | |
6218 | ||
3de11b2e | 6219 | payload_size = get_memory_write_packet_size (); |
2bc416ba | 6220 | |
6d820c5c DJ |
6221 | /* The packet buffer will be large enough for the payload; |
6222 | get_memory_packet_size ensures this. */ | |
a76d924d | 6223 | rs->buf[0] = '\0'; |
c906108c | 6224 | |
a257b5bb | 6225 | /* Compute the size of the actual payload by subtracting out the |
3de11b2e NS |
6226 | packet header and footer overhead: "$M<memaddr>,<len>:...#nn". |
6227 | */ | |
a76d924d DJ |
6228 | payload_size -= strlen ("$,:#NN"); |
6229 | if (!use_length) | |
6230 | /* The comma won't be used. */ | |
6231 | payload_size += 1; | |
6232 | header_length = strlen (header); | |
6233 | payload_size -= header_length; | |
3de11b2e | 6234 | payload_size -= hexnumlen (memaddr); |
c906108c | 6235 | |
a76d924d | 6236 | /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */ |
917317f4 | 6237 | |
a76d924d DJ |
6238 | strcat (rs->buf, header); |
6239 | p = rs->buf + strlen (header); | |
6240 | ||
6241 | /* Compute a best guess of the number of bytes actually transfered. */ | |
6242 | if (packet_format == 'X') | |
c906108c | 6243 | { |
23860348 | 6244 | /* Best guess at number of bytes that will fit. */ |
a257b5bb | 6245 | todo = min (len, payload_size); |
a76d924d DJ |
6246 | if (use_length) |
6247 | payload_size -= hexnumlen (todo); | |
3de11b2e | 6248 | todo = min (todo, payload_size); |
a76d924d DJ |
6249 | } |
6250 | else | |
6251 | { | |
23860348 | 6252 | /* Num bytes that will fit. */ |
a257b5bb | 6253 | todo = min (len, payload_size / 2); |
a76d924d DJ |
6254 | if (use_length) |
6255 | payload_size -= hexnumlen (todo); | |
3de11b2e | 6256 | todo = min (todo, payload_size / 2); |
917317f4 | 6257 | } |
a76d924d | 6258 | |
3de11b2e NS |
6259 | if (todo <= 0) |
6260 | internal_error (__FILE__, __LINE__, | |
6261 | _("minumum packet size too small to write data")); | |
802188a7 | 6262 | |
6765f3e5 DJ |
6263 | /* If we already need another packet, then try to align the end |
6264 | of this packet to a useful boundary. */ | |
6265 | if (todo > 2 * REMOTE_ALIGN_WRITES && todo < len) | |
6266 | todo = ((memaddr + todo) & ~(REMOTE_ALIGN_WRITES - 1)) - memaddr; | |
6267 | ||
a257b5bb | 6268 | /* Append "<memaddr>". */ |
917317f4 JM |
6269 | memaddr = remote_address_masked (memaddr); |
6270 | p += hexnumstr (p, (ULONGEST) memaddr); | |
a257b5bb | 6271 | |
a76d924d DJ |
6272 | if (use_length) |
6273 | { | |
6274 | /* Append ",". */ | |
6275 | *p++ = ','; | |
802188a7 | 6276 | |
a76d924d DJ |
6277 | /* Append <len>. Retain the location/size of <len>. It may need to |
6278 | be adjusted once the packet body has been created. */ | |
6279 | plen = p; | |
6280 | plenlen = hexnumstr (p, (ULONGEST) todo); | |
6281 | p += plenlen; | |
6282 | } | |
a257b5bb AC |
6283 | |
6284 | /* Append ":". */ | |
917317f4 JM |
6285 | *p++ = ':'; |
6286 | *p = '\0'; | |
802188a7 | 6287 | |
a257b5bb | 6288 | /* Append the packet body. */ |
a76d924d | 6289 | if (packet_format == 'X') |
917317f4 | 6290 | { |
917317f4 JM |
6291 | /* Binary mode. Send target system values byte by byte, in |
6292 | increasing byte addresses. Only escape certain critical | |
6293 | characters. */ | |
6765f3e5 DJ |
6294 | payload_length = remote_escape_output (myaddr, todo, p, &nr_bytes, |
6295 | payload_size); | |
6296 | ||
6297 | /* If not all TODO bytes fit, then we'll need another packet. Make | |
9b7194bc DJ |
6298 | a second try to keep the end of the packet aligned. Don't do |
6299 | this if the packet is tiny. */ | |
6300 | if (nr_bytes < todo && nr_bytes > 2 * REMOTE_ALIGN_WRITES) | |
6765f3e5 DJ |
6301 | { |
6302 | int new_nr_bytes; | |
6303 | ||
6304 | new_nr_bytes = (((memaddr + nr_bytes) & ~(REMOTE_ALIGN_WRITES - 1)) | |
6305 | - memaddr); | |
6306 | if (new_nr_bytes != nr_bytes) | |
6307 | payload_length = remote_escape_output (myaddr, new_nr_bytes, | |
6308 | p, &nr_bytes, | |
6309 | payload_size); | |
6310 | } | |
6311 | ||
6312 | p += payload_length; | |
a76d924d | 6313 | if (use_length && nr_bytes < todo) |
c906108c | 6314 | { |
802188a7 | 6315 | /* Escape chars have filled up the buffer prematurely, |
917317f4 JM |
6316 | and we have actually sent fewer bytes than planned. |
6317 | Fix-up the length field of the packet. Use the same | |
6318 | number of characters as before. */ | |
917317f4 JM |
6319 | plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen); |
6320 | *plen = ':'; /* overwrite \0 from hexnumnstr() */ | |
c906108c | 6321 | } |
a76d924d DJ |
6322 | } |
6323 | else | |
6324 | { | |
917317f4 JM |
6325 | /* Normal mode: Send target system values byte by byte, in |
6326 | increasing byte addresses. Each byte is encoded as a two hex | |
6327 | value. */ | |
2644f393 | 6328 | nr_bytes = bin2hex (myaddr, p, todo); |
aa6c0017 | 6329 | p += 2 * nr_bytes; |
c906108c | 6330 | } |
802188a7 | 6331 | |
2e9f7625 | 6332 | putpkt_binary (rs->buf, (int) (p - rs->buf)); |
6d820c5c | 6333 | getpkt (&rs->buf, &rs->buf_size, 0); |
802188a7 | 6334 | |
2e9f7625 | 6335 | if (rs->buf[0] == 'E') |
917317f4 JM |
6336 | { |
6337 | /* There is no correspondance between what the remote protocol | |
6338 | uses for errors and errno codes. We would like a cleaner way | |
6339 | of representing errors (big enough to include errno codes, | |
6340 | bfd_error codes, and others). But for now just return EIO. */ | |
6341 | errno = EIO; | |
6342 | return 0; | |
6343 | } | |
802188a7 | 6344 | |
23860348 MS |
6345 | /* Return NR_BYTES, not TODO, in case escape chars caused us to send |
6346 | fewer bytes than we'd planned. */ | |
917317f4 | 6347 | return nr_bytes; |
c906108c SS |
6348 | } |
6349 | ||
a76d924d DJ |
6350 | /* Write memory data directly to the remote machine. |
6351 | This does not inform the data cache; the data cache uses this. | |
6352 | MEMADDR is the address in the remote memory space. | |
6353 | MYADDR is the address of the buffer in our space. | |
6354 | LEN is the number of bytes. | |
6355 | ||
6356 | Returns number of bytes transferred, or 0 (setting errno) for | |
6357 | error. Only transfer a single packet. */ | |
6358 | ||
6359 | int | |
6360 | remote_write_bytes (CORE_ADDR memaddr, const gdb_byte *myaddr, int len) | |
6361 | { | |
6362 | char *packet_format = 0; | |
6363 | ||
6364 | /* Check whether the target supports binary download. */ | |
6365 | check_binary_download (memaddr); | |
6366 | ||
6367 | switch (remote_protocol_packets[PACKET_X].support) | |
6368 | { | |
6369 | case PACKET_ENABLE: | |
6370 | packet_format = "X"; | |
6371 | break; | |
6372 | case PACKET_DISABLE: | |
6373 | packet_format = "M"; | |
6374 | break; | |
6375 | case PACKET_SUPPORT_UNKNOWN: | |
6376 | internal_error (__FILE__, __LINE__, | |
6377 | _("remote_write_bytes: bad internal state")); | |
6378 | default: | |
6379 | internal_error (__FILE__, __LINE__, _("bad switch")); | |
6380 | } | |
6381 | ||
6382 | return remote_write_bytes_aux (packet_format, | |
6383 | memaddr, myaddr, len, packet_format[0], 1); | |
6384 | } | |
6385 | ||
c906108c SS |
6386 | /* Read memory data directly from the remote machine. |
6387 | This does not use the data cache; the data cache uses this. | |
6388 | MEMADDR is the address in the remote memory space. | |
6389 | MYADDR is the address of the buffer in our space. | |
6390 | LEN is the number of bytes. | |
6391 | ||
6392 | Returns number of bytes transferred, or 0 for error. */ | |
6393 | ||
917317f4 JM |
6394 | /* NOTE: cagney/1999-10-18: This function (and its siblings in other |
6395 | remote targets) shouldn't attempt to read the entire buffer. | |
6396 | Instead it should read a single packet worth of data and then | |
6397 | return the byte size of that packet to the caller. The caller (its | |
6398 | caller and its callers caller ;-) already contains code for | |
23860348 | 6399 | handling partial reads. */ |
917317f4 | 6400 | |
449092f6 | 6401 | int |
cfd77fa1 | 6402 | remote_read_bytes (CORE_ADDR memaddr, gdb_byte *myaddr, int len) |
c906108c | 6403 | { |
6d820c5c | 6404 | struct remote_state *rs = get_remote_state (); |
23860348 | 6405 | int max_buf_size; /* Max size of packet output buffer. */ |
c906108c SS |
6406 | int origlen; |
6407 | ||
b2182ed2 DJ |
6408 | if (len <= 0) |
6409 | return 0; | |
6410 | ||
11cf8741 | 6411 | max_buf_size = get_memory_read_packet_size (); |
6d820c5c DJ |
6412 | /* The packet buffer will be large enough for the payload; |
6413 | get_memory_packet_size ensures this. */ | |
c906108c SS |
6414 | |
6415 | origlen = len; | |
6416 | while (len > 0) | |
6417 | { | |
c906108c SS |
6418 | char *p; |
6419 | int todo; | |
6420 | int i; | |
6421 | ||
c5aa993b | 6422 | todo = min (len, max_buf_size / 2); /* num bytes that will fit */ |
c906108c SS |
6423 | |
6424 | /* construct "m"<memaddr>","<len>" */ | |
2e9f7625 | 6425 | /* sprintf (rs->buf, "m%lx,%x", (unsigned long) memaddr, todo); */ |
c906108c | 6426 | memaddr = remote_address_masked (memaddr); |
2e9f7625 | 6427 | p = rs->buf; |
c906108c SS |
6428 | *p++ = 'm'; |
6429 | p += hexnumstr (p, (ULONGEST) memaddr); | |
6430 | *p++ = ','; | |
6431 | p += hexnumstr (p, (ULONGEST) todo); | |
6432 | *p = '\0'; | |
6433 | ||
2e9f7625 | 6434 | putpkt (rs->buf); |
6d820c5c | 6435 | getpkt (&rs->buf, &rs->buf_size, 0); |
c906108c | 6436 | |
2e9f7625 DJ |
6437 | if (rs->buf[0] == 'E' |
6438 | && isxdigit (rs->buf[1]) && isxdigit (rs->buf[2]) | |
6439 | && rs->buf[3] == '\0') | |
c906108c | 6440 | { |
23860348 MS |
6441 | /* There is no correspondance between what the remote |
6442 | protocol uses for errors and errno codes. We would like | |
6443 | a cleaner way of representing errors (big enough to | |
6444 | include errno codes, bfd_error codes, and others). But | |
6445 | for now just return EIO. */ | |
c906108c SS |
6446 | errno = EIO; |
6447 | return 0; | |
6448 | } | |
6449 | ||
c5aa993b JM |
6450 | /* Reply describes memory byte by byte, |
6451 | each byte encoded as two hex characters. */ | |
c906108c | 6452 | |
2e9f7625 | 6453 | p = rs->buf; |
30559e10 | 6454 | if ((i = hex2bin (p, myaddr, todo)) < todo) |
c906108c | 6455 | { |
30559e10 | 6456 | /* Reply is short. This means that we were able to read |
23860348 | 6457 | only part of what we wanted to. */ |
30559e10 | 6458 | return i + (origlen - len); |
c906108c SS |
6459 | } |
6460 | myaddr += todo; | |
6461 | memaddr += todo; | |
6462 | len -= todo; | |
6463 | } | |
6464 | return origlen; | |
6465 | } | |
74531fed PA |
6466 | \f |
6467 | ||
6468 | /* Remote notification handler. */ | |
6469 | ||
6470 | static void | |
6471 | handle_notification (char *buf, size_t length) | |
6472 | { | |
6473 | if (strncmp (buf, "Stop:", 5) == 0) | |
6474 | { | |
6475 | if (pending_stop_reply) | |
0723dbf5 PA |
6476 | { |
6477 | /* We've already parsed the in-flight stop-reply, but the | |
6478 | stub for some reason thought we didn't, possibly due to | |
6479 | timeout on its side. Just ignore it. */ | |
6480 | if (remote_debug) | |
6481 | fprintf_unfiltered (gdb_stdlog, "ignoring resent notification\n"); | |
6482 | } | |
74531fed PA |
6483 | else |
6484 | { | |
6485 | struct cleanup *old_chain; | |
6486 | struct stop_reply *reply = stop_reply_xmalloc (); | |
a744cf53 | 6487 | |
74531fed PA |
6488 | old_chain = make_cleanup (do_stop_reply_xfree, reply); |
6489 | ||
6490 | remote_parse_stop_reply (buf + 5, reply); | |
6491 | ||
6492 | discard_cleanups (old_chain); | |
6493 | ||
6494 | /* Be careful to only set it after parsing, since an error | |
6495 | may be thrown then. */ | |
6496 | pending_stop_reply = reply; | |
6497 | ||
6498 | /* Notify the event loop there's a stop reply to acknowledge | |
6499 | and that there may be more events to fetch. */ | |
6500 | mark_async_event_handler (remote_async_get_pending_events_token); | |
0723dbf5 PA |
6501 | |
6502 | if (remote_debug) | |
6503 | fprintf_unfiltered (gdb_stdlog, "stop notification captured\n"); | |
74531fed PA |
6504 | } |
6505 | } | |
6506 | else | |
6507 | /* We ignore notifications we don't recognize, for compatibility | |
6508 | with newer stubs. */ | |
6509 | ; | |
6510 | } | |
6511 | ||
c906108c SS |
6512 | \f |
6513 | /* Read or write LEN bytes from inferior memory at MEMADDR, | |
23860348 MS |
6514 | transferring to or from debugger address BUFFER. Write to inferior |
6515 | if SHOULD_WRITE is nonzero. Returns length of data written or | |
6516 | read; 0 for error. TARGET is unused. */ | |
392a587b | 6517 | |
c906108c | 6518 | static int |
961cb7b5 | 6519 | remote_xfer_memory (CORE_ADDR mem_addr, gdb_byte *buffer, int mem_len, |
0a65a603 | 6520 | int should_write, struct mem_attrib *attrib, |
29e57380 | 6521 | struct target_ops *target) |
c906108c | 6522 | { |
4930751a C |
6523 | int res; |
6524 | ||
82f73884 PA |
6525 | set_general_thread (inferior_ptid); |
6526 | ||
4930751a | 6527 | if (should_write) |
b2182ed2 | 6528 | res = remote_write_bytes (mem_addr, buffer, mem_len); |
4930751a | 6529 | else |
b2182ed2 | 6530 | res = remote_read_bytes (mem_addr, buffer, mem_len); |
4930751a C |
6531 | |
6532 | return res; | |
c906108c SS |
6533 | } |
6534 | ||
a76d924d DJ |
6535 | /* Sends a packet with content determined by the printf format string |
6536 | FORMAT and the remaining arguments, then gets the reply. Returns | |
6537 | whether the packet was a success, a failure, or unknown. */ | |
6538 | ||
2c0b251b | 6539 | static enum packet_result |
a76d924d DJ |
6540 | remote_send_printf (const char *format, ...) |
6541 | { | |
6542 | struct remote_state *rs = get_remote_state (); | |
6543 | int max_size = get_remote_packet_size (); | |
a76d924d | 6544 | va_list ap; |
a744cf53 | 6545 | |
a76d924d DJ |
6546 | va_start (ap, format); |
6547 | ||
6548 | rs->buf[0] = '\0'; | |
6549 | if (vsnprintf (rs->buf, max_size, format, ap) >= max_size) | |
6550 | internal_error (__FILE__, __LINE__, "Too long remote packet."); | |
6551 | ||
6552 | if (putpkt (rs->buf) < 0) | |
6553 | error (_("Communication problem with target.")); | |
6554 | ||
6555 | rs->buf[0] = '\0'; | |
6556 | getpkt (&rs->buf, &rs->buf_size, 0); | |
6557 | ||
6558 | return packet_check_result (rs->buf); | |
6559 | } | |
6560 | ||
6561 | static void | |
6562 | restore_remote_timeout (void *p) | |
6563 | { | |
6564 | int value = *(int *)p; | |
a744cf53 | 6565 | |
a76d924d DJ |
6566 | remote_timeout = value; |
6567 | } | |
6568 | ||
6569 | /* Flash writing can take quite some time. We'll set | |
6570 | effectively infinite timeout for flash operations. | |
6571 | In future, we'll need to decide on a better approach. */ | |
6572 | static const int remote_flash_timeout = 1000; | |
6573 | ||
6574 | static void | |
6575 | remote_flash_erase (struct target_ops *ops, | |
6576 | ULONGEST address, LONGEST length) | |
6577 | { | |
5af949e3 | 6578 | int addr_size = gdbarch_addr_bit (target_gdbarch) / 8; |
a76d924d DJ |
6579 | int saved_remote_timeout = remote_timeout; |
6580 | enum packet_result ret; | |
a76d924d DJ |
6581 | struct cleanup *back_to = make_cleanup (restore_remote_timeout, |
6582 | &saved_remote_timeout); | |
a744cf53 | 6583 | |
a76d924d DJ |
6584 | remote_timeout = remote_flash_timeout; |
6585 | ||
6586 | ret = remote_send_printf ("vFlashErase:%s,%s", | |
5af949e3 | 6587 | phex (address, addr_size), |
a76d924d DJ |
6588 | phex (length, 4)); |
6589 | switch (ret) | |
6590 | { | |
6591 | case PACKET_UNKNOWN: | |
6592 | error (_("Remote target does not support flash erase")); | |
6593 | case PACKET_ERROR: | |
6594 | error (_("Error erasing flash with vFlashErase packet")); | |
6595 | default: | |
6596 | break; | |
6597 | } | |
6598 | ||
6599 | do_cleanups (back_to); | |
6600 | } | |
6601 | ||
6602 | static LONGEST | |
6603 | remote_flash_write (struct target_ops *ops, | |
6604 | ULONGEST address, LONGEST length, | |
6605 | const gdb_byte *data) | |
6606 | { | |
6607 | int saved_remote_timeout = remote_timeout; | |
6608 | int ret; | |
6609 | struct cleanup *back_to = make_cleanup (restore_remote_timeout, | |
6610 | &saved_remote_timeout); | |
6611 | ||
6612 | remote_timeout = remote_flash_timeout; | |
6613 | ret = remote_write_bytes_aux ("vFlashWrite:", address, data, length, 'X', 0); | |
6614 | do_cleanups (back_to); | |
6615 | ||
6616 | return ret; | |
6617 | } | |
6618 | ||
6619 | static void | |
6620 | remote_flash_done (struct target_ops *ops) | |
6621 | { | |
6622 | int saved_remote_timeout = remote_timeout; | |
6623 | int ret; | |
6624 | struct cleanup *back_to = make_cleanup (restore_remote_timeout, | |
6625 | &saved_remote_timeout); | |
6626 | ||
6627 | remote_timeout = remote_flash_timeout; | |
6628 | ret = remote_send_printf ("vFlashDone"); | |
6629 | do_cleanups (back_to); | |
6630 | ||
6631 | switch (ret) | |
6632 | { | |
6633 | case PACKET_UNKNOWN: | |
6634 | error (_("Remote target does not support vFlashDone")); | |
6635 | case PACKET_ERROR: | |
6636 | error (_("Error finishing flash operation")); | |
6637 | default: | |
6638 | break; | |
6639 | } | |
6640 | } | |
6641 | ||
c906108c | 6642 | static void |
fba45db2 | 6643 | remote_files_info (struct target_ops *ignore) |
c906108c SS |
6644 | { |
6645 | puts_filtered ("Debugging a target over a serial line.\n"); | |
6646 | } | |
6647 | \f | |
6648 | /* Stuff for dealing with the packets which are part of this protocol. | |
6649 | See comment at top of file for details. */ | |
6650 | ||
0876f84a | 6651 | /* Read a single character from the remote end. */ |
c906108c SS |
6652 | |
6653 | static int | |
fba45db2 | 6654 | readchar (int timeout) |
c906108c SS |
6655 | { |
6656 | int ch; | |
6657 | ||
2cd58942 | 6658 | ch = serial_readchar (remote_desc, timeout); |
c906108c | 6659 | |
2acceee2 | 6660 | if (ch >= 0) |
0876f84a | 6661 | return ch; |
2acceee2 JM |
6662 | |
6663 | switch ((enum serial_rc) ch) | |
c906108c SS |
6664 | { |
6665 | case SERIAL_EOF: | |
ce5ce7ed | 6666 | pop_target (); |
8a3fe4f8 | 6667 | error (_("Remote connection closed")); |
2acceee2 | 6668 | /* no return */ |
c906108c | 6669 | case SERIAL_ERROR: |
4ac8c4da TT |
6670 | pop_target (); |
6671 | perror_with_name (_("Remote communication error. Target disconnected.")); | |
2acceee2 | 6672 | /* no return */ |
c906108c | 6673 | case SERIAL_TIMEOUT: |
2acceee2 | 6674 | break; |
c906108c | 6675 | } |
2acceee2 | 6676 | return ch; |
c906108c SS |
6677 | } |
6678 | ||
6d820c5c DJ |
6679 | /* Send the command in *BUF to the remote machine, and read the reply |
6680 | into *BUF. Report an error if we get an error reply. Resize | |
6681 | *BUF using xrealloc if necessary to hold the result, and update | |
6682 | *SIZEOF_BUF. */ | |
c906108c SS |
6683 | |
6684 | static void | |
6d820c5c DJ |
6685 | remote_send (char **buf, |
6686 | long *sizeof_buf) | |
c906108c | 6687 | { |
6d820c5c | 6688 | putpkt (*buf); |
c2d11a7d | 6689 | getpkt (buf, sizeof_buf, 0); |
c906108c | 6690 | |
6d820c5c DJ |
6691 | if ((*buf)[0] == 'E') |
6692 | error (_("Remote failure reply: %s"), *buf); | |
c906108c SS |
6693 | } |
6694 | ||
6e5abd65 PA |
6695 | /* Return a pointer to an xmalloc'ed string representing an escaped |
6696 | version of BUF, of len N. E.g. \n is converted to \\n, \t to \\t, | |
6697 | etc. The caller is responsible for releasing the returned | |
6698 | memory. */ | |
6699 | ||
6700 | static char * | |
6701 | escape_buffer (const char *buf, int n) | |
6702 | { | |
6703 | struct cleanup *old_chain; | |
6704 | struct ui_file *stb; | |
6705 | char *str; | |
6e5abd65 PA |
6706 | |
6707 | stb = mem_fileopen (); | |
6708 | old_chain = make_cleanup_ui_file_delete (stb); | |
6709 | ||
6710 | fputstrn_unfiltered (buf, n, 0, stb); | |
759ef836 | 6711 | str = ui_file_xstrdup (stb, NULL); |
6e5abd65 PA |
6712 | do_cleanups (old_chain); |
6713 | return str; | |
6714 | } | |
6715 | ||
c906108c SS |
6716 | /* Display a null-terminated packet on stdout, for debugging, using C |
6717 | string notation. */ | |
6718 | ||
6719 | static void | |
fba45db2 | 6720 | print_packet (char *buf) |
c906108c SS |
6721 | { |
6722 | puts_filtered ("\""); | |
43e526b9 | 6723 | fputstr_filtered (buf, '"', gdb_stdout); |
c906108c SS |
6724 | puts_filtered ("\""); |
6725 | } | |
6726 | ||
6727 | int | |
fba45db2 | 6728 | putpkt (char *buf) |
c906108c SS |
6729 | { |
6730 | return putpkt_binary (buf, strlen (buf)); | |
6731 | } | |
6732 | ||
6733 | /* Send a packet to the remote machine, with error checking. The data | |
23860348 | 6734 | of the packet is in BUF. The string in BUF can be at most |
ea9c271d | 6735 | get_remote_packet_size () - 5 to account for the $, # and checksum, |
23860348 MS |
6736 | and for a possible /0 if we are debugging (remote_debug) and want |
6737 | to print the sent packet as a string. */ | |
c906108c SS |
6738 | |
6739 | static int | |
fba45db2 | 6740 | putpkt_binary (char *buf, int cnt) |
c906108c | 6741 | { |
2d717e4f | 6742 | struct remote_state *rs = get_remote_state (); |
c906108c SS |
6743 | int i; |
6744 | unsigned char csum = 0; | |
11cf8741 | 6745 | char *buf2 = alloca (cnt + 6); |
085dd6e6 | 6746 | |
c906108c SS |
6747 | int ch; |
6748 | int tcount = 0; | |
6749 | char *p; | |
6750 | ||
e24a49d8 PA |
6751 | /* Catch cases like trying to read memory or listing threads while |
6752 | we're waiting for a stop reply. The remote server wouldn't be | |
6753 | ready to handle this request, so we'd hang and timeout. We don't | |
6754 | have to worry about this in synchronous mode, because in that | |
6755 | case it's not possible to issue a command while the target is | |
74531fed PA |
6756 | running. This is not a problem in non-stop mode, because in that |
6757 | case, the stub is always ready to process serial input. */ | |
6758 | if (!non_stop && target_can_async_p () && rs->waiting_for_stop_reply) | |
e24a49d8 PA |
6759 | error (_("Cannot execute this command while the target is running.")); |
6760 | ||
2d717e4f DJ |
6761 | /* We're sending out a new packet. Make sure we don't look at a |
6762 | stale cached response. */ | |
6763 | rs->cached_wait_status = 0; | |
6764 | ||
c906108c SS |
6765 | /* Copy the packet into buffer BUF2, encapsulating it |
6766 | and giving it a checksum. */ | |
6767 | ||
c906108c SS |
6768 | p = buf2; |
6769 | *p++ = '$'; | |
6770 | ||
6771 | for (i = 0; i < cnt; i++) | |
6772 | { | |
6773 | csum += buf[i]; | |
6774 | *p++ = buf[i]; | |
6775 | } | |
6776 | *p++ = '#'; | |
6777 | *p++ = tohex ((csum >> 4) & 0xf); | |
6778 | *p++ = tohex (csum & 0xf); | |
6779 | ||
6780 | /* Send it over and over until we get a positive ack. */ | |
6781 | ||
6782 | while (1) | |
6783 | { | |
6784 | int started_error_output = 0; | |
6785 | ||
6786 | if (remote_debug) | |
6787 | { | |
6e5abd65 PA |
6788 | struct cleanup *old_chain; |
6789 | char *str; | |
6790 | ||
c906108c | 6791 | *p = '\0'; |
6e5abd65 PA |
6792 | str = escape_buffer (buf2, p - buf2); |
6793 | old_chain = make_cleanup (xfree, str); | |
6794 | fprintf_unfiltered (gdb_stdlog, "Sending packet: %s...", str); | |
0f71a2f6 | 6795 | gdb_flush (gdb_stdlog); |
6e5abd65 | 6796 | do_cleanups (old_chain); |
c906108c | 6797 | } |
2cd58942 | 6798 | if (serial_write (remote_desc, buf2, p - buf2)) |
e2e0b3e5 | 6799 | perror_with_name (_("putpkt: write failed")); |
c906108c | 6800 | |
a6f3e723 SL |
6801 | /* If this is a no acks version of the remote protocol, send the |
6802 | packet and move on. */ | |
6803 | if (rs->noack_mode) | |
6804 | break; | |
6805 | ||
74531fed PA |
6806 | /* Read until either a timeout occurs (-2) or '+' is read. |
6807 | Handle any notification that arrives in the mean time. */ | |
c906108c SS |
6808 | while (1) |
6809 | { | |
6810 | ch = readchar (remote_timeout); | |
6811 | ||
c5aa993b | 6812 | if (remote_debug) |
c906108c SS |
6813 | { |
6814 | switch (ch) | |
6815 | { | |
6816 | case '+': | |
1216fa2c | 6817 | case '-': |
c906108c SS |
6818 | case SERIAL_TIMEOUT: |
6819 | case '$': | |
74531fed | 6820 | case '%': |
c906108c SS |
6821 | if (started_error_output) |
6822 | { | |
6823 | putchar_unfiltered ('\n'); | |
6824 | started_error_output = 0; | |
6825 | } | |
6826 | } | |
6827 | } | |
6828 | ||
6829 | switch (ch) | |
6830 | { | |
6831 | case '+': | |
6832 | if (remote_debug) | |
0f71a2f6 | 6833 | fprintf_unfiltered (gdb_stdlog, "Ack\n"); |
c906108c | 6834 | return 1; |
1216fa2c AC |
6835 | case '-': |
6836 | if (remote_debug) | |
6837 | fprintf_unfiltered (gdb_stdlog, "Nak\n"); | |
c906108c | 6838 | case SERIAL_TIMEOUT: |
c5aa993b | 6839 | tcount++; |
c906108c SS |
6840 | if (tcount > 3) |
6841 | return 0; | |
23860348 | 6842 | break; /* Retransmit buffer. */ |
c906108c SS |
6843 | case '$': |
6844 | { | |
40e3f985 | 6845 | if (remote_debug) |
2bc416ba | 6846 | fprintf_unfiltered (gdb_stdlog, |
23860348 | 6847 | "Packet instead of Ack, ignoring it\n"); |
d6f7abdf AC |
6848 | /* It's probably an old response sent because an ACK |
6849 | was lost. Gobble up the packet and ack it so it | |
6850 | doesn't get retransmitted when we resend this | |
6851 | packet. */ | |
6d820c5c | 6852 | skip_frame (); |
d6f7abdf | 6853 | serial_write (remote_desc, "+", 1); |
23860348 | 6854 | continue; /* Now, go look for +. */ |
c906108c | 6855 | } |
74531fed PA |
6856 | |
6857 | case '%': | |
6858 | { | |
6859 | int val; | |
6860 | ||
6861 | /* If we got a notification, handle it, and go back to looking | |
6862 | for an ack. */ | |
6863 | /* We've found the start of a notification. Now | |
6864 | collect the data. */ | |
6865 | val = read_frame (&rs->buf, &rs->buf_size); | |
6866 | if (val >= 0) | |
6867 | { | |
6868 | if (remote_debug) | |
6869 | { | |
6e5abd65 PA |
6870 | struct cleanup *old_chain; |
6871 | char *str; | |
6872 | ||
6873 | str = escape_buffer (rs->buf, val); | |
6874 | old_chain = make_cleanup (xfree, str); | |
6875 | fprintf_unfiltered (gdb_stdlog, | |
6876 | " Notification received: %s\n", | |
6877 | str); | |
6878 | do_cleanups (old_chain); | |
74531fed PA |
6879 | } |
6880 | handle_notification (rs->buf, val); | |
6881 | /* We're in sync now, rewait for the ack. */ | |
6882 | tcount = 0; | |
6883 | } | |
6884 | else | |
6885 | { | |
6886 | if (remote_debug) | |
6887 | { | |
6888 | if (!started_error_output) | |
6889 | { | |
6890 | started_error_output = 1; | |
6891 | fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: "); | |
6892 | } | |
6893 | fputc_unfiltered (ch & 0177, gdb_stdlog); | |
6894 | fprintf_unfiltered (gdb_stdlog, "%s", rs->buf); | |
6895 | } | |
6896 | } | |
6897 | continue; | |
6898 | } | |
6899 | /* fall-through */ | |
c906108c SS |
6900 | default: |
6901 | if (remote_debug) | |
6902 | { | |
6903 | if (!started_error_output) | |
6904 | { | |
6905 | started_error_output = 1; | |
0f71a2f6 | 6906 | fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: "); |
c906108c | 6907 | } |
0f71a2f6 | 6908 | fputc_unfiltered (ch & 0177, gdb_stdlog); |
c906108c SS |
6909 | } |
6910 | continue; | |
6911 | } | |
23860348 | 6912 | break; /* Here to retransmit. */ |
c906108c SS |
6913 | } |
6914 | ||
6915 | #if 0 | |
6916 | /* This is wrong. If doing a long backtrace, the user should be | |
c5aa993b JM |
6917 | able to get out next time we call QUIT, without anything as |
6918 | violent as interrupt_query. If we want to provide a way out of | |
6919 | here without getting to the next QUIT, it should be based on | |
6920 | hitting ^C twice as in remote_wait. */ | |
c906108c SS |
6921 | if (quit_flag) |
6922 | { | |
6923 | quit_flag = 0; | |
6924 | interrupt_query (); | |
6925 | } | |
6926 | #endif | |
6927 | } | |
a6f3e723 | 6928 | return 0; |
c906108c SS |
6929 | } |
6930 | ||
6d820c5c DJ |
6931 | /* Come here after finding the start of a frame when we expected an |
6932 | ack. Do our best to discard the rest of this packet. */ | |
6933 | ||
6934 | static void | |
6935 | skip_frame (void) | |
6936 | { | |
6937 | int c; | |
6938 | ||
6939 | while (1) | |
6940 | { | |
6941 | c = readchar (remote_timeout); | |
6942 | switch (c) | |
6943 | { | |
6944 | case SERIAL_TIMEOUT: | |
6945 | /* Nothing we can do. */ | |
6946 | return; | |
6947 | case '#': | |
6948 | /* Discard the two bytes of checksum and stop. */ | |
6949 | c = readchar (remote_timeout); | |
6950 | if (c >= 0) | |
6951 | c = readchar (remote_timeout); | |
6952 | ||
6953 | return; | |
6954 | case '*': /* Run length encoding. */ | |
6955 | /* Discard the repeat count. */ | |
6956 | c = readchar (remote_timeout); | |
6957 | if (c < 0) | |
6958 | return; | |
6959 | break; | |
6960 | default: | |
6961 | /* A regular character. */ | |
6962 | break; | |
6963 | } | |
6964 | } | |
6965 | } | |
6966 | ||
c906108c | 6967 | /* Come here after finding the start of the frame. Collect the rest |
6d820c5c DJ |
6968 | into *BUF, verifying the checksum, length, and handling run-length |
6969 | compression. NUL terminate the buffer. If there is not enough room, | |
6970 | expand *BUF using xrealloc. | |
c906108c | 6971 | |
c2d11a7d JM |
6972 | Returns -1 on error, number of characters in buffer (ignoring the |
6973 | trailing NULL) on success. (could be extended to return one of the | |
23860348 | 6974 | SERIAL status indications). */ |
c2d11a7d JM |
6975 | |
6976 | static long | |
6d820c5c DJ |
6977 | read_frame (char **buf_p, |
6978 | long *sizeof_buf) | |
c906108c SS |
6979 | { |
6980 | unsigned char csum; | |
c2d11a7d | 6981 | long bc; |
c906108c | 6982 | int c; |
6d820c5c | 6983 | char *buf = *buf_p; |
a6f3e723 | 6984 | struct remote_state *rs = get_remote_state (); |
c906108c SS |
6985 | |
6986 | csum = 0; | |
c2d11a7d | 6987 | bc = 0; |
c906108c SS |
6988 | |
6989 | while (1) | |
6990 | { | |
6991 | c = readchar (remote_timeout); | |
c906108c SS |
6992 | switch (c) |
6993 | { | |
6994 | case SERIAL_TIMEOUT: | |
6995 | if (remote_debug) | |
0f71a2f6 | 6996 | fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog); |
c2d11a7d | 6997 | return -1; |
c906108c SS |
6998 | case '$': |
6999 | if (remote_debug) | |
0f71a2f6 JM |
7000 | fputs_filtered ("Saw new packet start in middle of old one\n", |
7001 | gdb_stdlog); | |
23860348 | 7002 | return -1; /* Start a new packet, count retries. */ |
c906108c SS |
7003 | case '#': |
7004 | { | |
7005 | unsigned char pktcsum; | |
e1b09194 AC |
7006 | int check_0 = 0; |
7007 | int check_1 = 0; | |
c906108c | 7008 | |
c2d11a7d | 7009 | buf[bc] = '\0'; |
c906108c | 7010 | |
e1b09194 AC |
7011 | check_0 = readchar (remote_timeout); |
7012 | if (check_0 >= 0) | |
7013 | check_1 = readchar (remote_timeout); | |
802188a7 | 7014 | |
e1b09194 AC |
7015 | if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT) |
7016 | { | |
7017 | if (remote_debug) | |
2bc416ba | 7018 | fputs_filtered ("Timeout in checksum, retrying\n", |
23860348 | 7019 | gdb_stdlog); |
e1b09194 AC |
7020 | return -1; |
7021 | } | |
7022 | else if (check_0 < 0 || check_1 < 0) | |
40e3f985 FN |
7023 | { |
7024 | if (remote_debug) | |
2bc416ba | 7025 | fputs_filtered ("Communication error in checksum\n", |
23860348 | 7026 | gdb_stdlog); |
40e3f985 FN |
7027 | return -1; |
7028 | } | |
c906108c | 7029 | |
a6f3e723 SL |
7030 | /* Don't recompute the checksum; with no ack packets we |
7031 | don't have any way to indicate a packet retransmission | |
7032 | is necessary. */ | |
7033 | if (rs->noack_mode) | |
7034 | return bc; | |
7035 | ||
e1b09194 | 7036 | pktcsum = (fromhex (check_0) << 4) | fromhex (check_1); |
c906108c | 7037 | if (csum == pktcsum) |
c2d11a7d | 7038 | return bc; |
c906108c | 7039 | |
c5aa993b | 7040 | if (remote_debug) |
c906108c | 7041 | { |
6e5abd65 PA |
7042 | struct cleanup *old_chain; |
7043 | char *str; | |
7044 | ||
7045 | str = escape_buffer (buf, bc); | |
7046 | old_chain = make_cleanup (xfree, str); | |
7047 | fprintf_unfiltered (gdb_stdlog, | |
7048 | "\ | |
7049 | Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n", | |
7050 | pktcsum, csum, str); | |
7051 | do_cleanups (old_chain); | |
c906108c | 7052 | } |
c2d11a7d | 7053 | /* Number of characters in buffer ignoring trailing |
23860348 | 7054 | NULL. */ |
c2d11a7d | 7055 | return -1; |
c906108c | 7056 | } |
23860348 | 7057 | case '*': /* Run length encoding. */ |
c2c6d25f JM |
7058 | { |
7059 | int repeat; | |
c906108c | 7060 | |
a744cf53 | 7061 | csum += c; |
b4501125 AC |
7062 | c = readchar (remote_timeout); |
7063 | csum += c; | |
23860348 | 7064 | repeat = c - ' ' + 3; /* Compute repeat count. */ |
c906108c | 7065 | |
23860348 | 7066 | /* The character before ``*'' is repeated. */ |
c2d11a7d | 7067 | |
6d820c5c | 7068 | if (repeat > 0 && repeat <= 255 && bc > 0) |
c2c6d25f | 7069 | { |
6d820c5c DJ |
7070 | if (bc + repeat - 1 >= *sizeof_buf - 1) |
7071 | { | |
7072 | /* Make some more room in the buffer. */ | |
7073 | *sizeof_buf += repeat; | |
7074 | *buf_p = xrealloc (*buf_p, *sizeof_buf); | |
7075 | buf = *buf_p; | |
7076 | } | |
7077 | ||
c2d11a7d JM |
7078 | memset (&buf[bc], buf[bc - 1], repeat); |
7079 | bc += repeat; | |
c2c6d25f JM |
7080 | continue; |
7081 | } | |
7082 | ||
c2d11a7d | 7083 | buf[bc] = '\0'; |
6d820c5c | 7084 | printf_filtered (_("Invalid run length encoding: %s\n"), buf); |
c2d11a7d | 7085 | return -1; |
c2c6d25f | 7086 | } |
c906108c | 7087 | default: |
6d820c5c | 7088 | if (bc >= *sizeof_buf - 1) |
c906108c | 7089 | { |
6d820c5c DJ |
7090 | /* Make some more room in the buffer. */ |
7091 | *sizeof_buf *= 2; | |
7092 | *buf_p = xrealloc (*buf_p, *sizeof_buf); | |
7093 | buf = *buf_p; | |
c906108c SS |
7094 | } |
7095 | ||
6d820c5c DJ |
7096 | buf[bc++] = c; |
7097 | csum += c; | |
7098 | continue; | |
c906108c SS |
7099 | } |
7100 | } | |
7101 | } | |
7102 | ||
7103 | /* Read a packet from the remote machine, with error checking, and | |
6d820c5c DJ |
7104 | store it in *BUF. Resize *BUF using xrealloc if necessary to hold |
7105 | the result, and update *SIZEOF_BUF. If FOREVER, wait forever | |
7106 | rather than timing out; this is used (in synchronous mode) to wait | |
7107 | for a target that is is executing user code to stop. */ | |
d9fcf2fb JM |
7108 | /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we |
7109 | don't have to change all the calls to getpkt to deal with the | |
7110 | return value, because at the moment I don't know what the right | |
23860348 | 7111 | thing to do it for those. */ |
c906108c | 7112 | void |
6d820c5c DJ |
7113 | getpkt (char **buf, |
7114 | long *sizeof_buf, | |
c2d11a7d | 7115 | int forever) |
d9fcf2fb JM |
7116 | { |
7117 | int timed_out; | |
7118 | ||
7119 | timed_out = getpkt_sane (buf, sizeof_buf, forever); | |
7120 | } | |
7121 | ||
7122 | ||
7123 | /* Read a packet from the remote machine, with error checking, and | |
6d820c5c DJ |
7124 | store it in *BUF. Resize *BUF using xrealloc if necessary to hold |
7125 | the result, and update *SIZEOF_BUF. If FOREVER, wait forever | |
7126 | rather than timing out; this is used (in synchronous mode) to wait | |
7127 | for a target that is is executing user code to stop. If FOREVER == | |
7128 | 0, this function is allowed to time out gracefully and return an | |
74531fed PA |
7129 | indication of this to the caller. Otherwise return the number of |
7130 | bytes read. If EXPECTING_NOTIF, consider receiving a notification | |
7131 | enough reason to return to the caller. */ | |
7132 | ||
3172dc30 | 7133 | static int |
74531fed PA |
7134 | getpkt_or_notif_sane_1 (char **buf, long *sizeof_buf, int forever, |
7135 | int expecting_notif) | |
c906108c | 7136 | { |
2d717e4f | 7137 | struct remote_state *rs = get_remote_state (); |
c906108c SS |
7138 | int c; |
7139 | int tries; | |
7140 | int timeout; | |
df4b58fe | 7141 | int val = -1; |
c906108c | 7142 | |
2d717e4f DJ |
7143 | /* We're reading a new response. Make sure we don't look at a |
7144 | previously cached response. */ | |
7145 | rs->cached_wait_status = 0; | |
7146 | ||
6d820c5c | 7147 | strcpy (*buf, "timeout"); |
c906108c SS |
7148 | |
7149 | if (forever) | |
74531fed PA |
7150 | timeout = watchdog > 0 ? watchdog : -1; |
7151 | else if (expecting_notif) | |
7152 | timeout = 0; /* There should already be a char in the buffer. If | |
7153 | not, bail out. */ | |
c906108c SS |
7154 | else |
7155 | timeout = remote_timeout; | |
7156 | ||
7157 | #define MAX_TRIES 3 | |
7158 | ||
74531fed PA |
7159 | /* Process any number of notifications, and then return when |
7160 | we get a packet. */ | |
7161 | for (;;) | |
c906108c | 7162 | { |
74531fed PA |
7163 | /* If we get a timeout or bad checksm, retry up to MAX_TRIES |
7164 | times. */ | |
7165 | for (tries = 1; tries <= MAX_TRIES; tries++) | |
c906108c | 7166 | { |
74531fed PA |
7167 | /* This can loop forever if the remote side sends us |
7168 | characters continuously, but if it pauses, we'll get | |
7169 | SERIAL_TIMEOUT from readchar because of timeout. Then | |
7170 | we'll count that as a retry. | |
7171 | ||
7172 | Note that even when forever is set, we will only wait | |
7173 | forever prior to the start of a packet. After that, we | |
7174 | expect characters to arrive at a brisk pace. They should | |
7175 | show up within remote_timeout intervals. */ | |
7176 | do | |
7177 | c = readchar (timeout); | |
7178 | while (c != SERIAL_TIMEOUT && c != '$' && c != '%'); | |
c906108c SS |
7179 | |
7180 | if (c == SERIAL_TIMEOUT) | |
7181 | { | |
74531fed PA |
7182 | if (expecting_notif) |
7183 | return -1; /* Don't complain, it's normal to not get | |
7184 | anything in this case. */ | |
7185 | ||
23860348 | 7186 | if (forever) /* Watchdog went off? Kill the target. */ |
c906108c | 7187 | { |
2acceee2 | 7188 | QUIT; |
ce5ce7ed | 7189 | pop_target (); |
489eaeba | 7190 | error (_("Watchdog timeout has expired. Target detached.")); |
c906108c | 7191 | } |
c906108c | 7192 | if (remote_debug) |
0f71a2f6 | 7193 | fputs_filtered ("Timed out.\n", gdb_stdlog); |
c906108c | 7194 | } |
74531fed PA |
7195 | else |
7196 | { | |
7197 | /* We've found the start of a packet or notification. | |
7198 | Now collect the data. */ | |
7199 | val = read_frame (buf, sizeof_buf); | |
7200 | if (val >= 0) | |
7201 | break; | |
7202 | } | |
7203 | ||
7204 | serial_write (remote_desc, "-", 1); | |
c906108c | 7205 | } |
c906108c | 7206 | |
74531fed PA |
7207 | if (tries > MAX_TRIES) |
7208 | { | |
7209 | /* We have tried hard enough, and just can't receive the | |
7210 | packet/notification. Give up. */ | |
7211 | printf_unfiltered (_("Ignoring packet error, continuing...\n")); | |
c906108c | 7212 | |
74531fed PA |
7213 | /* Skip the ack char if we're in no-ack mode. */ |
7214 | if (!rs->noack_mode) | |
7215 | serial_write (remote_desc, "+", 1); | |
7216 | return -1; | |
7217 | } | |
c906108c | 7218 | |
74531fed PA |
7219 | /* If we got an ordinary packet, return that to our caller. */ |
7220 | if (c == '$') | |
c906108c SS |
7221 | { |
7222 | if (remote_debug) | |
43e526b9 | 7223 | { |
6e5abd65 PA |
7224 | struct cleanup *old_chain; |
7225 | char *str; | |
7226 | ||
7227 | str = escape_buffer (*buf, val); | |
7228 | old_chain = make_cleanup (xfree, str); | |
7229 | fprintf_unfiltered (gdb_stdlog, "Packet received: %s\n", str); | |
7230 | do_cleanups (old_chain); | |
43e526b9 | 7231 | } |
a6f3e723 SL |
7232 | |
7233 | /* Skip the ack char if we're in no-ack mode. */ | |
7234 | if (!rs->noack_mode) | |
7235 | serial_write (remote_desc, "+", 1); | |
0876f84a | 7236 | return val; |
c906108c SS |
7237 | } |
7238 | ||
74531fed PA |
7239 | /* If we got a notification, handle it, and go back to looking |
7240 | for a packet. */ | |
7241 | else | |
7242 | { | |
7243 | gdb_assert (c == '%'); | |
7244 | ||
7245 | if (remote_debug) | |
7246 | { | |
6e5abd65 PA |
7247 | struct cleanup *old_chain; |
7248 | char *str; | |
7249 | ||
7250 | str = escape_buffer (*buf, val); | |
7251 | old_chain = make_cleanup (xfree, str); | |
7252 | fprintf_unfiltered (gdb_stdlog, | |
7253 | " Notification received: %s\n", | |
7254 | str); | |
7255 | do_cleanups (old_chain); | |
74531fed | 7256 | } |
c906108c | 7257 | |
74531fed | 7258 | handle_notification (*buf, val); |
c906108c | 7259 | |
74531fed | 7260 | /* Notifications require no acknowledgement. */ |
a6f3e723 | 7261 | |
74531fed PA |
7262 | if (expecting_notif) |
7263 | return -1; | |
7264 | } | |
7265 | } | |
7266 | } | |
7267 | ||
7268 | static int | |
7269 | getpkt_sane (char **buf, long *sizeof_buf, int forever) | |
7270 | { | |
7271 | return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 0); | |
7272 | } | |
7273 | ||
7274 | static int | |
7275 | getpkt_or_notif_sane (char **buf, long *sizeof_buf, int forever) | |
7276 | { | |
7277 | return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 1); | |
c906108c | 7278 | } |
74531fed | 7279 | |
c906108c SS |
7280 | \f |
7281 | static void | |
7d85a9c0 | 7282 | remote_kill (struct target_ops *ops) |
43ff13b4 | 7283 | { |
23860348 MS |
7284 | /* Use catch_errors so the user can quit from gdb even when we |
7285 | aren't on speaking terms with the remote system. */ | |
c5aa993b | 7286 | catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR); |
43ff13b4 JM |
7287 | |
7288 | /* Don't wait for it to die. I'm not really sure it matters whether | |
7289 | we do or not. For the existing stubs, kill is a noop. */ | |
7290 | target_mourn_inferior (); | |
7291 | } | |
7292 | ||
82f73884 PA |
7293 | static int |
7294 | remote_vkill (int pid, struct remote_state *rs) | |
7295 | { | |
7296 | if (remote_protocol_packets[PACKET_vKill].support == PACKET_DISABLE) | |
7297 | return -1; | |
7298 | ||
7299 | /* Tell the remote target to detach. */ | |
7300 | sprintf (rs->buf, "vKill;%x", pid); | |
7301 | putpkt (rs->buf); | |
7302 | getpkt (&rs->buf, &rs->buf_size, 0); | |
7303 | ||
7304 | if (packet_ok (rs->buf, | |
7305 | &remote_protocol_packets[PACKET_vKill]) == PACKET_OK) | |
7306 | return 0; | |
7307 | else if (remote_protocol_packets[PACKET_vKill].support == PACKET_DISABLE) | |
7308 | return -1; | |
7309 | else | |
7310 | return 1; | |
7311 | } | |
7312 | ||
7313 | static void | |
7d85a9c0 | 7314 | extended_remote_kill (struct target_ops *ops) |
82f73884 PA |
7315 | { |
7316 | int res; | |
7317 | int pid = ptid_get_pid (inferior_ptid); | |
7318 | struct remote_state *rs = get_remote_state (); | |
7319 | ||
7320 | res = remote_vkill (pid, rs); | |
7321 | if (res == -1 && !remote_multi_process_p (rs)) | |
7322 | { | |
7323 | /* Don't try 'k' on a multi-process aware stub -- it has no way | |
7324 | to specify the pid. */ | |
7325 | ||
7326 | putpkt ("k"); | |
7327 | #if 0 | |
7328 | getpkt (&rs->buf, &rs->buf_size, 0); | |
7329 | if (rs->buf[0] != 'O' || rs->buf[0] != 'K') | |
7330 | res = 1; | |
7331 | #else | |
7332 | /* Don't wait for it to die. I'm not really sure it matters whether | |
7333 | we do or not. For the existing stubs, kill is a noop. */ | |
7334 | res = 0; | |
7335 | #endif | |
7336 | } | |
7337 | ||
7338 | if (res != 0) | |
7339 | error (_("Can't kill process")); | |
7340 | ||
82f73884 PA |
7341 | target_mourn_inferior (); |
7342 | } | |
7343 | ||
c906108c | 7344 | static void |
136d6dae | 7345 | remote_mourn (struct target_ops *ops) |
c906108c | 7346 | { |
136d6dae | 7347 | remote_mourn_1 (ops); |
c906108c SS |
7348 | } |
7349 | ||
c906108c SS |
7350 | /* Worker function for remote_mourn. */ |
7351 | static void | |
fba45db2 | 7352 | remote_mourn_1 (struct target_ops *target) |
c906108c SS |
7353 | { |
7354 | unpush_target (target); | |
ce5ce7ed | 7355 | |
8a2492ee PA |
7356 | /* remote_close takes care of doing most of the clean up. */ |
7357 | generic_mourn_inferior (); | |
c906108c SS |
7358 | } |
7359 | ||
2d717e4f DJ |
7360 | static void |
7361 | extended_remote_mourn_1 (struct target_ops *target) | |
7362 | { | |
7363 | struct remote_state *rs = get_remote_state (); | |
c906108c | 7364 | |
e24a49d8 PA |
7365 | /* In case we got here due to an error, but we're going to stay |
7366 | connected. */ | |
7367 | rs->waiting_for_stop_reply = 0; | |
7368 | ||
74531fed PA |
7369 | /* We're no longer interested in these events. */ |
7370 | discard_pending_stop_replies (ptid_get_pid (inferior_ptid)); | |
7371 | ||
dc1981d7 PA |
7372 | /* If the current general thread belonged to the process we just |
7373 | detached from or has exited, the remote side current general | |
7374 | thread becomes undefined. Considering a case like this: | |
7375 | ||
7376 | - We just got here due to a detach. | |
7377 | - The process that we're detaching from happens to immediately | |
7378 | report a global breakpoint being hit in non-stop mode, in the | |
7379 | same thread we had selected before. | |
7380 | - GDB attaches to this process again. | |
7381 | - This event happens to be the next event we handle. | |
7382 | ||
7383 | GDB would consider that the current general thread didn't need to | |
7384 | be set on the stub side (with Hg), since for all it knew, | |
7385 | GENERAL_THREAD hadn't changed. | |
7386 | ||
7387 | Notice that although in all-stop mode, the remote server always | |
7388 | sets the current thread to the thread reporting the stop event, | |
7389 | that doesn't happen in non-stop mode; in non-stop, the stub *must | |
7390 | not* change the current thread when reporting a breakpoint hit, | |
7391 | due to the decoupling of event reporting and event handling. | |
7392 | ||
7393 | To keep things simple, we always invalidate our notion of the | |
7394 | current thread. */ | |
7395 | record_currthread (minus_one_ptid); | |
7396 | ||
2d717e4f DJ |
7397 | /* Unlike "target remote", we do not want to unpush the target; then |
7398 | the next time the user says "run", we won't be connected. */ | |
7399 | ||
48aa3c27 PA |
7400 | /* Call common code to mark the inferior as not running. */ |
7401 | generic_mourn_inferior (); | |
7402 | ||
d729566a | 7403 | if (!have_inferiors ()) |
2d717e4f | 7404 | { |
82f73884 PA |
7405 | if (!remote_multi_process_p (rs)) |
7406 | { | |
7407 | /* Check whether the target is running now - some remote stubs | |
7408 | automatically restart after kill. */ | |
7409 | putpkt ("?"); | |
7410 | getpkt (&rs->buf, &rs->buf_size, 0); | |
7411 | ||
7412 | if (rs->buf[0] == 'S' || rs->buf[0] == 'T') | |
7413 | { | |
7414 | /* Assume that the target has been restarted. Set inferior_ptid | |
7415 | so that bits of core GDB realizes there's something here, e.g., | |
7416 | so that the user can say "kill" again. */ | |
7417 | inferior_ptid = magic_null_ptid; | |
7418 | } | |
82f73884 | 7419 | } |
2d717e4f DJ |
7420 | } |
7421 | } | |
c906108c SS |
7422 | |
7423 | static void | |
136d6dae | 7424 | extended_remote_mourn (struct target_ops *ops) |
c906108c | 7425 | { |
136d6dae | 7426 | extended_remote_mourn_1 (ops); |
2d717e4f | 7427 | } |
c906108c | 7428 | |
2d717e4f DJ |
7429 | static int |
7430 | extended_remote_run (char *args) | |
7431 | { | |
7432 | struct remote_state *rs = get_remote_state (); | |
2d717e4f | 7433 | int len; |
c906108c | 7434 | |
2d717e4f DJ |
7435 | /* If the user has disabled vRun support, or we have detected that |
7436 | support is not available, do not try it. */ | |
7437 | if (remote_protocol_packets[PACKET_vRun].support == PACKET_DISABLE) | |
7438 | return -1; | |
424163ea | 7439 | |
2d717e4f DJ |
7440 | strcpy (rs->buf, "vRun;"); |
7441 | len = strlen (rs->buf); | |
c906108c | 7442 | |
2d717e4f DJ |
7443 | if (strlen (remote_exec_file) * 2 + len >= get_remote_packet_size ()) |
7444 | error (_("Remote file name too long for run packet")); | |
7445 | len += 2 * bin2hex ((gdb_byte *) remote_exec_file, rs->buf + len, 0); | |
7446 | ||
d1a41061 | 7447 | gdb_assert (args != NULL); |
2d717e4f DJ |
7448 | if (*args) |
7449 | { | |
7450 | struct cleanup *back_to; | |
7451 | int i; | |
7452 | char **argv; | |
7453 | ||
d1a41061 | 7454 | argv = gdb_buildargv (args); |
2d717e4f DJ |
7455 | back_to = make_cleanup ((void (*) (void *)) freeargv, argv); |
7456 | for (i = 0; argv[i] != NULL; i++) | |
7457 | { | |
7458 | if (strlen (argv[i]) * 2 + 1 + len >= get_remote_packet_size ()) | |
7459 | error (_("Argument list too long for run packet")); | |
7460 | rs->buf[len++] = ';'; | |
7461 | len += 2 * bin2hex ((gdb_byte *) argv[i], rs->buf + len, 0); | |
7462 | } | |
7463 | do_cleanups (back_to); | |
7464 | } | |
7465 | ||
7466 | rs->buf[len++] = '\0'; | |
7467 | ||
7468 | putpkt (rs->buf); | |
7469 | getpkt (&rs->buf, &rs->buf_size, 0); | |
7470 | ||
7471 | if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vRun]) == PACKET_OK) | |
7472 | { | |
7473 | /* We have a wait response; we don't need it, though. All is well. */ | |
7474 | return 0; | |
7475 | } | |
7476 | else if (remote_protocol_packets[PACKET_vRun].support == PACKET_DISABLE) | |
7477 | /* It wasn't disabled before, but it is now. */ | |
7478 | return -1; | |
7479 | else | |
7480 | { | |
7481 | if (remote_exec_file[0] == '\0') | |
7482 | error (_("Running the default executable on the remote target failed; " | |
7483 | "try \"set remote exec-file\"?")); | |
7484 | else | |
7485 | error (_("Running \"%s\" on the remote target failed"), | |
7486 | remote_exec_file); | |
7487 | } | |
c906108c SS |
7488 | } |
7489 | ||
2d717e4f DJ |
7490 | /* In the extended protocol we want to be able to do things like |
7491 | "run" and have them basically work as expected. So we need | |
7492 | a special create_inferior function. We support changing the | |
7493 | executable file and the command line arguments, but not the | |
7494 | environment. */ | |
7495 | ||
43ff13b4 | 7496 | static void |
2d717e4f | 7497 | extended_remote_create_inferior_1 (char *exec_file, char *args, |
75c99385 | 7498 | char **env, int from_tty) |
43ff13b4 | 7499 | { |
43ff13b4 | 7500 | /* If running asynchronously, register the target file descriptor |
23860348 | 7501 | with the event loop. */ |
75c99385 | 7502 | if (target_can_async_p ()) |
2acceee2 | 7503 | target_async (inferior_event_handler, 0); |
43ff13b4 JM |
7504 | |
7505 | /* Now restart the remote server. */ | |
2d717e4f DJ |
7506 | if (extended_remote_run (args) == -1) |
7507 | { | |
7508 | /* vRun was not supported. Fail if we need it to do what the | |
7509 | user requested. */ | |
7510 | if (remote_exec_file[0]) | |
7511 | error (_("Remote target does not support \"set remote exec-file\"")); | |
7512 | if (args[0]) | |
7513 | error (_("Remote target does not support \"set args\" or run <ARGS>")); | |
43ff13b4 | 7514 | |
2d717e4f DJ |
7515 | /* Fall back to "R". */ |
7516 | extended_remote_restart (); | |
7517 | } | |
424163ea | 7518 | |
6c95b8df PA |
7519 | if (!have_inferiors ()) |
7520 | { | |
7521 | /* Clean up from the last time we ran, before we mark the target | |
7522 | running again. This will mark breakpoints uninserted, and | |
7523 | get_offsets may insert breakpoints. */ | |
7524 | init_thread_list (); | |
7525 | init_wait_for_inferior (); | |
7526 | } | |
45280a52 | 7527 | |
2d717e4f | 7528 | /* Now mark the inferior as running before we do anything else. */ |
79d7f229 | 7529 | inferior_ptid = magic_null_ptid; |
c0a2216e | 7530 | |
74531fed PA |
7531 | /* Now, if we have thread information, update inferior_ptid. */ |
7532 | inferior_ptid = remote_current_thread (inferior_ptid); | |
7533 | ||
0b16c5cf | 7534 | remote_add_inferior (ptid_get_pid (inferior_ptid), 0); |
c0a2216e PA |
7535 | add_thread_silent (inferior_ptid); |
7536 | ||
2d717e4f DJ |
7537 | /* Get updated offsets, if the stub uses qOffsets. */ |
7538 | get_offsets (); | |
2d717e4f DJ |
7539 | } |
7540 | ||
7541 | static void | |
136d6dae VP |
7542 | extended_remote_create_inferior (struct target_ops *ops, |
7543 | char *exec_file, char *args, | |
2d717e4f DJ |
7544 | char **env, int from_tty) |
7545 | { | |
75c99385 | 7546 | extended_remote_create_inferior_1 (exec_file, args, env, from_tty); |
43ff13b4 | 7547 | } |
c906108c | 7548 | \f |
c5aa993b | 7549 | |
8181d85f DJ |
7550 | /* Insert a breakpoint. On targets that have software breakpoint |
7551 | support, we ask the remote target to do the work; on targets | |
7552 | which don't, we insert a traditional memory breakpoint. */ | |
c906108c SS |
7553 | |
7554 | static int | |
a6d9a66e UW |
7555 | remote_insert_breakpoint (struct gdbarch *gdbarch, |
7556 | struct bp_target_info *bp_tgt) | |
c906108c | 7557 | { |
d471ea57 AC |
7558 | /* Try the "Z" s/w breakpoint packet if it is not already disabled. |
7559 | If it succeeds, then set the support to PACKET_ENABLE. If it | |
7560 | fails, and the user has explicitly requested the Z support then | |
23860348 | 7561 | report an error, otherwise, mark it disabled and go on. */ |
802188a7 | 7562 | |
444abaca | 7563 | if (remote_protocol_packets[PACKET_Z0].support != PACKET_DISABLE) |
96baa820 | 7564 | { |
7c0f6dcc | 7565 | CORE_ADDR addr = bp_tgt->placed_address; |
4fff2411 JZ |
7566 | struct remote_state *rs; |
7567 | char *p; | |
7c0f6dcc | 7568 | int bpsize; |
4fff2411 | 7569 | |
a1dcb23a | 7570 | gdbarch_remote_breakpoint_from_pc (gdbarch, &addr, &bpsize); |
4fff2411 JZ |
7571 | |
7572 | rs = get_remote_state (); | |
7573 | p = rs->buf; | |
802188a7 | 7574 | |
96baa820 JM |
7575 | *(p++) = 'Z'; |
7576 | *(p++) = '0'; | |
7577 | *(p++) = ','; | |
7c0f6dcc | 7578 | addr = (ULONGEST) remote_address_masked (addr); |
8181d85f | 7579 | p += hexnumstr (p, addr); |
7c0f6dcc | 7580 | sprintf (p, ",%d", bpsize); |
802188a7 | 7581 | |
6d820c5c DJ |
7582 | putpkt (rs->buf); |
7583 | getpkt (&rs->buf, &rs->buf_size, 0); | |
96baa820 | 7584 | |
6d820c5c | 7585 | switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0])) |
96baa820 | 7586 | { |
d471ea57 AC |
7587 | case PACKET_ERROR: |
7588 | return -1; | |
7589 | case PACKET_OK: | |
7c0f6dcc JL |
7590 | bp_tgt->placed_address = addr; |
7591 | bp_tgt->placed_size = bpsize; | |
d471ea57 AC |
7592 | return 0; |
7593 | case PACKET_UNKNOWN: | |
7594 | break; | |
96baa820 JM |
7595 | } |
7596 | } | |
c906108c | 7597 | |
a6d9a66e | 7598 | return memory_insert_breakpoint (gdbarch, bp_tgt); |
c906108c SS |
7599 | } |
7600 | ||
7601 | static int | |
a6d9a66e UW |
7602 | remote_remove_breakpoint (struct gdbarch *gdbarch, |
7603 | struct bp_target_info *bp_tgt) | |
c906108c | 7604 | { |
8181d85f | 7605 | CORE_ADDR addr = bp_tgt->placed_address; |
d01949b6 | 7606 | struct remote_state *rs = get_remote_state (); |
96baa820 | 7607 | |
444abaca | 7608 | if (remote_protocol_packets[PACKET_Z0].support != PACKET_DISABLE) |
96baa820 | 7609 | { |
6d820c5c | 7610 | char *p = rs->buf; |
802188a7 | 7611 | |
96baa820 JM |
7612 | *(p++) = 'z'; |
7613 | *(p++) = '0'; | |
7614 | *(p++) = ','; | |
7615 | ||
8181d85f DJ |
7616 | addr = (ULONGEST) remote_address_masked (bp_tgt->placed_address); |
7617 | p += hexnumstr (p, addr); | |
7618 | sprintf (p, ",%d", bp_tgt->placed_size); | |
802188a7 | 7619 | |
6d820c5c DJ |
7620 | putpkt (rs->buf); |
7621 | getpkt (&rs->buf, &rs->buf_size, 0); | |
96baa820 | 7622 | |
6d820c5c | 7623 | return (rs->buf[0] == 'E'); |
96baa820 JM |
7624 | } |
7625 | ||
a6d9a66e | 7626 | return memory_remove_breakpoint (gdbarch, bp_tgt); |
c906108c SS |
7627 | } |
7628 | ||
d471ea57 AC |
7629 | static int |
7630 | watchpoint_to_Z_packet (int type) | |
7631 | { | |
7632 | switch (type) | |
7633 | { | |
7634 | case hw_write: | |
bb858e6a | 7635 | return Z_PACKET_WRITE_WP; |
d471ea57 AC |
7636 | break; |
7637 | case hw_read: | |
bb858e6a | 7638 | return Z_PACKET_READ_WP; |
d471ea57 AC |
7639 | break; |
7640 | case hw_access: | |
bb858e6a | 7641 | return Z_PACKET_ACCESS_WP; |
d471ea57 AC |
7642 | break; |
7643 | default: | |
8e65ff28 | 7644 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 7645 | _("hw_bp_to_z: bad watchpoint type %d"), type); |
d471ea57 AC |
7646 | } |
7647 | } | |
7648 | ||
3c3bea1c | 7649 | static int |
0cf6dd15 TJB |
7650 | remote_insert_watchpoint (CORE_ADDR addr, int len, int type, |
7651 | struct expression *cond) | |
96baa820 | 7652 | { |
d01949b6 | 7653 | struct remote_state *rs = get_remote_state (); |
e514a9d6 | 7654 | char *p; |
d471ea57 | 7655 | enum Z_packet_type packet = watchpoint_to_Z_packet (type); |
96baa820 | 7656 | |
444abaca | 7657 | if (remote_protocol_packets[PACKET_Z0 + packet].support == PACKET_DISABLE) |
85d721b8 | 7658 | return 1; |
802188a7 | 7659 | |
6d820c5c DJ |
7660 | sprintf (rs->buf, "Z%x,", packet); |
7661 | p = strchr (rs->buf, '\0'); | |
96baa820 JM |
7662 | addr = remote_address_masked (addr); |
7663 | p += hexnumstr (p, (ULONGEST) addr); | |
d4f3574e | 7664 | sprintf (p, ",%x", len); |
802188a7 | 7665 | |
6d820c5c DJ |
7666 | putpkt (rs->buf); |
7667 | getpkt (&rs->buf, &rs->buf_size, 0); | |
96baa820 | 7668 | |
6d820c5c | 7669 | switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet])) |
d471ea57 AC |
7670 | { |
7671 | case PACKET_ERROR: | |
d471ea57 | 7672 | return -1; |
85d721b8 PA |
7673 | case PACKET_UNKNOWN: |
7674 | return 1; | |
d471ea57 AC |
7675 | case PACKET_OK: |
7676 | return 0; | |
7677 | } | |
8e65ff28 | 7678 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 7679 | _("remote_insert_watchpoint: reached end of function")); |
96baa820 JM |
7680 | } |
7681 | ||
d471ea57 | 7682 | |
3c3bea1c | 7683 | static int |
0cf6dd15 TJB |
7684 | remote_remove_watchpoint (CORE_ADDR addr, int len, int type, |
7685 | struct expression *cond) | |
96baa820 | 7686 | { |
d01949b6 | 7687 | struct remote_state *rs = get_remote_state (); |
e514a9d6 | 7688 | char *p; |
d471ea57 AC |
7689 | enum Z_packet_type packet = watchpoint_to_Z_packet (type); |
7690 | ||
444abaca | 7691 | if (remote_protocol_packets[PACKET_Z0 + packet].support == PACKET_DISABLE) |
5cffb350 | 7692 | return -1; |
802188a7 | 7693 | |
6d820c5c DJ |
7694 | sprintf (rs->buf, "z%x,", packet); |
7695 | p = strchr (rs->buf, '\0'); | |
96baa820 JM |
7696 | addr = remote_address_masked (addr); |
7697 | p += hexnumstr (p, (ULONGEST) addr); | |
d4f3574e | 7698 | sprintf (p, ",%x", len); |
6d820c5c DJ |
7699 | putpkt (rs->buf); |
7700 | getpkt (&rs->buf, &rs->buf_size, 0); | |
96baa820 | 7701 | |
6d820c5c | 7702 | switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet])) |
d471ea57 AC |
7703 | { |
7704 | case PACKET_ERROR: | |
7705 | case PACKET_UNKNOWN: | |
7706 | return -1; | |
7707 | case PACKET_OK: | |
7708 | return 0; | |
7709 | } | |
8e65ff28 | 7710 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 7711 | _("remote_remove_watchpoint: reached end of function")); |
96baa820 JM |
7712 | } |
7713 | ||
3c3bea1c | 7714 | |
501eef12 AC |
7715 | int remote_hw_watchpoint_limit = -1; |
7716 | int remote_hw_breakpoint_limit = -1; | |
d471ea57 | 7717 | |
b9362cc7 | 7718 | static int |
3c3bea1c | 7719 | remote_check_watch_resources (int type, int cnt, int ot) |
96baa820 | 7720 | { |
3c3bea1c GS |
7721 | if (type == bp_hardware_breakpoint) |
7722 | { | |
7723 | if (remote_hw_breakpoint_limit == 0) | |
7724 | return 0; | |
501eef12 AC |
7725 | else if (remote_hw_breakpoint_limit < 0) |
7726 | return 1; | |
3c3bea1c GS |
7727 | else if (cnt <= remote_hw_breakpoint_limit) |
7728 | return 1; | |
7729 | } | |
7730 | else | |
7731 | { | |
7732 | if (remote_hw_watchpoint_limit == 0) | |
7733 | return 0; | |
501eef12 AC |
7734 | else if (remote_hw_watchpoint_limit < 0) |
7735 | return 1; | |
3c3bea1c GS |
7736 | else if (ot) |
7737 | return -1; | |
7738 | else if (cnt <= remote_hw_watchpoint_limit) | |
7739 | return 1; | |
7740 | } | |
7741 | return -1; | |
7742 | } | |
7743 | ||
b9362cc7 | 7744 | static int |
3c3bea1c GS |
7745 | remote_stopped_by_watchpoint (void) |
7746 | { | |
82f73884 | 7747 | return remote_stopped_by_watchpoint_p; |
3c3bea1c GS |
7748 | } |
7749 | ||
4aa7a7f5 JJ |
7750 | static int |
7751 | remote_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p) | |
3c3bea1c | 7752 | { |
4aa7a7f5 | 7753 | int rc = 0; |
a744cf53 | 7754 | |
d983da9c | 7755 | if (remote_stopped_by_watchpoint ()) |
4aa7a7f5 JJ |
7756 | { |
7757 | *addr_p = remote_watch_data_address; | |
7758 | rc = 1; | |
7759 | } | |
7760 | ||
7761 | return rc; | |
3c3bea1c GS |
7762 | } |
7763 | ||
7764 | ||
7765 | static int | |
a6d9a66e UW |
7766 | remote_insert_hw_breakpoint (struct gdbarch *gdbarch, |
7767 | struct bp_target_info *bp_tgt) | |
3c3bea1c | 7768 | { |
8181d85f | 7769 | CORE_ADDR addr; |
4fff2411 JZ |
7770 | struct remote_state *rs; |
7771 | char *p; | |
802188a7 | 7772 | |
c8189ed1 | 7773 | /* The length field should be set to the size of a breakpoint |
8181d85f | 7774 | instruction, even though we aren't inserting one ourselves. */ |
c8189ed1 | 7775 | |
a1dcb23a | 7776 | gdbarch_remote_breakpoint_from_pc |
a6d9a66e | 7777 | (gdbarch, &bp_tgt->placed_address, &bp_tgt->placed_size); |
3c3bea1c | 7778 | |
444abaca | 7779 | if (remote_protocol_packets[PACKET_Z1].support == PACKET_DISABLE) |
5cffb350 | 7780 | return -1; |
2bc416ba | 7781 | |
4fff2411 JZ |
7782 | rs = get_remote_state (); |
7783 | p = rs->buf; | |
7784 | ||
96baa820 JM |
7785 | *(p++) = 'Z'; |
7786 | *(p++) = '1'; | |
7787 | *(p++) = ','; | |
802188a7 | 7788 | |
8181d85f | 7789 | addr = remote_address_masked (bp_tgt->placed_address); |
96baa820 | 7790 | p += hexnumstr (p, (ULONGEST) addr); |
8181d85f | 7791 | sprintf (p, ",%x", bp_tgt->placed_size); |
96baa820 | 7792 | |
6d820c5c DJ |
7793 | putpkt (rs->buf); |
7794 | getpkt (&rs->buf, &rs->buf_size, 0); | |
96baa820 | 7795 | |
6d820c5c | 7796 | switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1])) |
d471ea57 AC |
7797 | { |
7798 | case PACKET_ERROR: | |
7799 | case PACKET_UNKNOWN: | |
7800 | return -1; | |
7801 | case PACKET_OK: | |
7802 | return 0; | |
7803 | } | |
8e65ff28 | 7804 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 7805 | _("remote_insert_hw_breakpoint: reached end of function")); |
96baa820 JM |
7806 | } |
7807 | ||
d471ea57 | 7808 | |
802188a7 | 7809 | static int |
a6d9a66e UW |
7810 | remote_remove_hw_breakpoint (struct gdbarch *gdbarch, |
7811 | struct bp_target_info *bp_tgt) | |
96baa820 | 7812 | { |
8181d85f | 7813 | CORE_ADDR addr; |
d01949b6 | 7814 | struct remote_state *rs = get_remote_state (); |
6d820c5c | 7815 | char *p = rs->buf; |
c8189ed1 | 7816 | |
444abaca | 7817 | if (remote_protocol_packets[PACKET_Z1].support == PACKET_DISABLE) |
5cffb350 | 7818 | return -1; |
802188a7 | 7819 | |
96baa820 JM |
7820 | *(p++) = 'z'; |
7821 | *(p++) = '1'; | |
7822 | *(p++) = ','; | |
802188a7 | 7823 | |
8181d85f | 7824 | addr = remote_address_masked (bp_tgt->placed_address); |
96baa820 | 7825 | p += hexnumstr (p, (ULONGEST) addr); |
8181d85f | 7826 | sprintf (p, ",%x", bp_tgt->placed_size); |
96baa820 | 7827 | |
6d820c5c DJ |
7828 | putpkt (rs->buf); |
7829 | getpkt (&rs->buf, &rs->buf_size, 0); | |
802188a7 | 7830 | |
6d820c5c | 7831 | switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1])) |
d471ea57 AC |
7832 | { |
7833 | case PACKET_ERROR: | |
7834 | case PACKET_UNKNOWN: | |
7835 | return -1; | |
7836 | case PACKET_OK: | |
7837 | return 0; | |
7838 | } | |
8e65ff28 | 7839 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 7840 | _("remote_remove_hw_breakpoint: reached end of function")); |
96baa820 | 7841 | } |
96baa820 | 7842 | |
23860348 | 7843 | /* Table used by the crc32 function to calcuate the checksum. */ |
c906108c | 7844 | |
c5aa993b JM |
7845 | static unsigned long crc32_table[256] = |
7846 | {0, 0}; | |
c906108c SS |
7847 | |
7848 | static unsigned long | |
4a5e7a5b | 7849 | crc32 (const unsigned char *buf, int len, unsigned int crc) |
c906108c | 7850 | { |
c5aa993b | 7851 | if (!crc32_table[1]) |
c906108c | 7852 | { |
23860348 | 7853 | /* Initialize the CRC table and the decoding table. */ |
c906108c SS |
7854 | int i, j; |
7855 | unsigned int c; | |
7856 | ||
7857 | for (i = 0; i < 256; i++) | |
c5aa993b JM |
7858 | { |
7859 | for (c = i << 24, j = 8; j > 0; --j) | |
7860 | c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1); | |
7861 | crc32_table[i] = c; | |
7862 | } | |
c906108c SS |
7863 | } |
7864 | ||
7865 | while (len--) | |
7866 | { | |
7867 | crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buf) & 255]; | |
7868 | buf++; | |
7869 | } | |
7870 | return crc; | |
7871 | } | |
7872 | ||
4a5e7a5b PA |
7873 | /* Verify memory using the "qCRC:" request. */ |
7874 | ||
7875 | static int | |
7876 | remote_verify_memory (struct target_ops *ops, | |
7877 | const gdb_byte *data, CORE_ADDR lma, ULONGEST size) | |
7878 | { | |
7879 | struct remote_state *rs = get_remote_state (); | |
7880 | unsigned long host_crc, target_crc; | |
7881 | char *tmp; | |
7882 | ||
7883 | /* FIXME: assumes lma can fit into long. */ | |
7884 | xsnprintf (rs->buf, get_remote_packet_size (), "qCRC:%lx,%lx", | |
7885 | (long) lma, (long) size); | |
7886 | putpkt (rs->buf); | |
7887 | ||
7888 | /* Be clever; compute the host_crc before waiting for target | |
7889 | reply. */ | |
7890 | host_crc = crc32 (data, size, 0xffffffff); | |
7891 | ||
7892 | getpkt (&rs->buf, &rs->buf_size, 0); | |
7893 | if (rs->buf[0] == 'E') | |
7894 | return -1; | |
7895 | ||
7896 | if (rs->buf[0] != 'C') | |
7897 | error (_("remote target does not support this operation")); | |
7898 | ||
7899 | for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++) | |
7900 | target_crc = target_crc * 16 + fromhex (*tmp); | |
7901 | ||
7902 | return (host_crc == target_crc); | |
7903 | } | |
7904 | ||
c906108c SS |
7905 | /* compare-sections command |
7906 | ||
7907 | With no arguments, compares each loadable section in the exec bfd | |
7908 | with the same memory range on the target, and reports mismatches. | |
4a5e7a5b | 7909 | Useful for verifying the image on the target against the exec file. */ |
e514a9d6 | 7910 | |
c906108c | 7911 | static void |
fba45db2 | 7912 | compare_sections_command (char *args, int from_tty) |
c906108c SS |
7913 | { |
7914 | asection *s; | |
c906108c | 7915 | struct cleanup *old_chain; |
085dd6e6 | 7916 | char *sectdata; |
ce359b09 | 7917 | const char *sectname; |
c906108c SS |
7918 | bfd_size_type size; |
7919 | bfd_vma lma; | |
7920 | int matched = 0; | |
7921 | int mismatched = 0; | |
4a5e7a5b | 7922 | int res; |
c906108c SS |
7923 | |
7924 | if (!exec_bfd) | |
8a3fe4f8 | 7925 | error (_("command cannot be used without an exec file")); |
c906108c | 7926 | |
c5aa993b | 7927 | for (s = exec_bfd->sections; s; s = s->next) |
c906108c SS |
7928 | { |
7929 | if (!(s->flags & SEC_LOAD)) | |
c5aa993b | 7930 | continue; /* skip non-loadable section */ |
c906108c | 7931 | |
2c500098 | 7932 | size = bfd_get_section_size (s); |
c906108c | 7933 | if (size == 0) |
c5aa993b | 7934 | continue; /* skip zero-length section */ |
c906108c | 7935 | |
ce359b09 | 7936 | sectname = bfd_get_section_name (exec_bfd, s); |
c906108c | 7937 | if (args && strcmp (args, sectname) != 0) |
c5aa993b | 7938 | continue; /* not the section selected by user */ |
c906108c | 7939 | |
c5aa993b | 7940 | matched = 1; /* do this section */ |
c906108c | 7941 | lma = s->lma; |
c906108c | 7942 | |
c906108c | 7943 | sectdata = xmalloc (size); |
b8c9b27d | 7944 | old_chain = make_cleanup (xfree, sectdata); |
c906108c | 7945 | bfd_get_section_contents (exec_bfd, s, sectdata, 0, size); |
c906108c | 7946 | |
4a5e7a5b PA |
7947 | res = target_verify_memory (sectdata, lma, size); |
7948 | ||
7949 | if (res == -1) | |
5af949e3 UW |
7950 | error (_("target memory fault, section %s, range %s -- %s"), sectname, |
7951 | paddress (target_gdbarch, lma), | |
7952 | paddress (target_gdbarch, lma + size)); | |
c906108c | 7953 | |
5af949e3 UW |
7954 | printf_filtered ("Section %s, range %s -- %s: ", sectname, |
7955 | paddress (target_gdbarch, lma), | |
7956 | paddress (target_gdbarch, lma + size)); | |
4a5e7a5b | 7957 | if (res) |
c906108c SS |
7958 | printf_filtered ("matched.\n"); |
7959 | else | |
c5aa993b JM |
7960 | { |
7961 | printf_filtered ("MIS-MATCHED!\n"); | |
7962 | mismatched++; | |
7963 | } | |
c906108c SS |
7964 | |
7965 | do_cleanups (old_chain); | |
7966 | } | |
7967 | if (mismatched > 0) | |
8a3fe4f8 AC |
7968 | warning (_("One or more sections of the remote executable does not match\n\ |
7969 | the loaded file\n")); | |
c906108c | 7970 | if (args && !matched) |
a3f17187 | 7971 | printf_filtered (_("No loaded section named '%s'.\n"), args); |
c906108c SS |
7972 | } |
7973 | ||
0e7f50da UW |
7974 | /* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET |
7975 | into remote target. The number of bytes written to the remote | |
7976 | target is returned, or -1 for error. */ | |
7977 | ||
7978 | static LONGEST | |
7979 | remote_write_qxfer (struct target_ops *ops, const char *object_name, | |
7980 | const char *annex, const gdb_byte *writebuf, | |
7981 | ULONGEST offset, LONGEST len, | |
7982 | struct packet_config *packet) | |
7983 | { | |
7984 | int i, buf_len; | |
7985 | ULONGEST n; | |
0e7f50da UW |
7986 | struct remote_state *rs = get_remote_state (); |
7987 | int max_size = get_memory_write_packet_size (); | |
7988 | ||
7989 | if (packet->support == PACKET_DISABLE) | |
7990 | return -1; | |
7991 | ||
7992 | /* Insert header. */ | |
7993 | i = snprintf (rs->buf, max_size, | |
7994 | "qXfer:%s:write:%s:%s:", | |
7995 | object_name, annex ? annex : "", | |
7996 | phex_nz (offset, sizeof offset)); | |
7997 | max_size -= (i + 1); | |
7998 | ||
7999 | /* Escape as much data as fits into rs->buf. */ | |
8000 | buf_len = remote_escape_output | |
8001 | (writebuf, len, (rs->buf + i), &max_size, max_size); | |
8002 | ||
8003 | if (putpkt_binary (rs->buf, i + buf_len) < 0 | |
8004 | || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0 | |
8005 | || packet_ok (rs->buf, packet) != PACKET_OK) | |
8006 | return -1; | |
8007 | ||
8008 | unpack_varlen_hex (rs->buf, &n); | |
8009 | return n; | |
8010 | } | |
8011 | ||
0876f84a DJ |
8012 | /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet. |
8013 | Data at OFFSET, of up to LEN bytes, is read into READBUF; the | |
8014 | number of bytes read is returned, or 0 for EOF, or -1 for error. | |
8015 | The number of bytes read may be less than LEN without indicating an | |
8016 | EOF. PACKET is checked and updated to indicate whether the remote | |
8017 | target supports this object. */ | |
8018 | ||
8019 | static LONGEST | |
8020 | remote_read_qxfer (struct target_ops *ops, const char *object_name, | |
8021 | const char *annex, | |
8022 | gdb_byte *readbuf, ULONGEST offset, LONGEST len, | |
8023 | struct packet_config *packet) | |
8024 | { | |
8025 | static char *finished_object; | |
8026 | static char *finished_annex; | |
8027 | static ULONGEST finished_offset; | |
8028 | ||
8029 | struct remote_state *rs = get_remote_state (); | |
0876f84a DJ |
8030 | LONGEST i, n, packet_len; |
8031 | ||
8032 | if (packet->support == PACKET_DISABLE) | |
8033 | return -1; | |
8034 | ||
8035 | /* Check whether we've cached an end-of-object packet that matches | |
8036 | this request. */ | |
8037 | if (finished_object) | |
8038 | { | |
8039 | if (strcmp (object_name, finished_object) == 0 | |
8040 | && strcmp (annex ? annex : "", finished_annex) == 0 | |
8041 | && offset == finished_offset) | |
8042 | return 0; | |
8043 | ||
8044 | /* Otherwise, we're now reading something different. Discard | |
8045 | the cache. */ | |
8046 | xfree (finished_object); | |
8047 | xfree (finished_annex); | |
8048 | finished_object = NULL; | |
8049 | finished_annex = NULL; | |
8050 | } | |
8051 | ||
8052 | /* Request only enough to fit in a single packet. The actual data | |
8053 | may not, since we don't know how much of it will need to be escaped; | |
8054 | the target is free to respond with slightly less data. We subtract | |
8055 | five to account for the response type and the protocol frame. */ | |
8056 | n = min (get_remote_packet_size () - 5, len); | |
8057 | snprintf (rs->buf, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s", | |
8058 | object_name, annex ? annex : "", | |
8059 | phex_nz (offset, sizeof offset), | |
8060 | phex_nz (n, sizeof n)); | |
8061 | i = putpkt (rs->buf); | |
8062 | if (i < 0) | |
8063 | return -1; | |
8064 | ||
8065 | rs->buf[0] = '\0'; | |
8066 | packet_len = getpkt_sane (&rs->buf, &rs->buf_size, 0); | |
8067 | if (packet_len < 0 || packet_ok (rs->buf, packet) != PACKET_OK) | |
8068 | return -1; | |
8069 | ||
8070 | if (rs->buf[0] != 'l' && rs->buf[0] != 'm') | |
8071 | error (_("Unknown remote qXfer reply: %s"), rs->buf); | |
8072 | ||
8073 | /* 'm' means there is (or at least might be) more data after this | |
8074 | batch. That does not make sense unless there's at least one byte | |
8075 | of data in this reply. */ | |
8076 | if (rs->buf[0] == 'm' && packet_len == 1) | |
8077 | error (_("Remote qXfer reply contained no data.")); | |
8078 | ||
8079 | /* Got some data. */ | |
8080 | i = remote_unescape_input (rs->buf + 1, packet_len - 1, readbuf, n); | |
8081 | ||
8082 | /* 'l' is an EOF marker, possibly including a final block of data, | |
0e7f50da UW |
8083 | or possibly empty. If we have the final block of a non-empty |
8084 | object, record this fact to bypass a subsequent partial read. */ | |
8085 | if (rs->buf[0] == 'l' && offset + i > 0) | |
0876f84a DJ |
8086 | { |
8087 | finished_object = xstrdup (object_name); | |
8088 | finished_annex = xstrdup (annex ? annex : ""); | |
8089 | finished_offset = offset + i; | |
8090 | } | |
8091 | ||
8092 | return i; | |
8093 | } | |
8094 | ||
1e3ff5ad | 8095 | static LONGEST |
4b8a223f | 8096 | remote_xfer_partial (struct target_ops *ops, enum target_object object, |
961cb7b5 MK |
8097 | const char *annex, gdb_byte *readbuf, |
8098 | const gdb_byte *writebuf, ULONGEST offset, LONGEST len) | |
c906108c | 8099 | { |
82f73884 | 8100 | struct remote_state *rs; |
c906108c | 8101 | int i; |
6d820c5c | 8102 | char *p2; |
1e3ff5ad | 8103 | char query_type; |
c906108c | 8104 | |
82f73884 PA |
8105 | set_general_thread (inferior_ptid); |
8106 | ||
8107 | rs = get_remote_state (); | |
8108 | ||
b2182ed2 | 8109 | /* Handle memory using the standard memory routines. */ |
21e3b9b9 DJ |
8110 | if (object == TARGET_OBJECT_MEMORY) |
8111 | { | |
8112 | int xfered; | |
a744cf53 | 8113 | |
21e3b9b9 DJ |
8114 | errno = 0; |
8115 | ||
2d717e4f DJ |
8116 | /* If the remote target is connected but not running, we should |
8117 | pass this request down to a lower stratum (e.g. the executable | |
8118 | file). */ | |
8119 | if (!target_has_execution) | |
8120 | return 0; | |
8121 | ||
21e3b9b9 | 8122 | if (writebuf != NULL) |
b2182ed2 | 8123 | xfered = remote_write_bytes (offset, writebuf, len); |
21e3b9b9 | 8124 | else |
b2182ed2 | 8125 | xfered = remote_read_bytes (offset, readbuf, len); |
21e3b9b9 DJ |
8126 | |
8127 | if (xfered > 0) | |
8128 | return xfered; | |
8129 | else if (xfered == 0 && errno == 0) | |
8130 | return 0; | |
8131 | else | |
8132 | return -1; | |
8133 | } | |
8134 | ||
0e7f50da UW |
8135 | /* Handle SPU memory using qxfer packets. */ |
8136 | if (object == TARGET_OBJECT_SPU) | |
8137 | { | |
8138 | if (readbuf) | |
8139 | return remote_read_qxfer (ops, "spu", annex, readbuf, offset, len, | |
8140 | &remote_protocol_packets | |
8141 | [PACKET_qXfer_spu_read]); | |
8142 | else | |
8143 | return remote_write_qxfer (ops, "spu", annex, writebuf, offset, len, | |
8144 | &remote_protocol_packets | |
8145 | [PACKET_qXfer_spu_write]); | |
8146 | } | |
8147 | ||
4aa995e1 PA |
8148 | /* Handle extra signal info using qxfer packets. */ |
8149 | if (object == TARGET_OBJECT_SIGNAL_INFO) | |
8150 | { | |
8151 | if (readbuf) | |
8152 | return remote_read_qxfer (ops, "siginfo", annex, readbuf, offset, len, | |
8153 | &remote_protocol_packets | |
8154 | [PACKET_qXfer_siginfo_read]); | |
8155 | else | |
8156 | return remote_write_qxfer (ops, "siginfo", annex, writebuf, offset, len, | |
8157 | &remote_protocol_packets | |
8158 | [PACKET_qXfer_siginfo_write]); | |
8159 | } | |
8160 | ||
0fb4aa4b PA |
8161 | if (object == TARGET_OBJECT_STATIC_TRACE_DATA) |
8162 | { | |
8163 | if (readbuf) | |
8164 | return remote_read_qxfer (ops, "statictrace", annex, readbuf, offset, len, | |
8165 | &remote_protocol_packets | |
8166 | [PACKET_qXfer_statictrace_read]); | |
8167 | else | |
8168 | return -1; | |
8169 | } | |
8170 | ||
a76d924d DJ |
8171 | /* Only handle flash writes. */ |
8172 | if (writebuf != NULL) | |
8173 | { | |
8174 | LONGEST xfered; | |
8175 | ||
8176 | switch (object) | |
8177 | { | |
8178 | case TARGET_OBJECT_FLASH: | |
8179 | xfered = remote_flash_write (ops, offset, len, writebuf); | |
8180 | ||
8181 | if (xfered > 0) | |
8182 | return xfered; | |
8183 | else if (xfered == 0 && errno == 0) | |
8184 | return 0; | |
8185 | else | |
8186 | return -1; | |
8187 | ||
8188 | default: | |
8189 | return -1; | |
8190 | } | |
8191 | } | |
4b8a223f | 8192 | |
1e3ff5ad AC |
8193 | /* Map pre-existing objects onto letters. DO NOT do this for new |
8194 | objects!!! Instead specify new query packets. */ | |
8195 | switch (object) | |
c906108c | 8196 | { |
1e3ff5ad AC |
8197 | case TARGET_OBJECT_AVR: |
8198 | query_type = 'R'; | |
8199 | break; | |
802188a7 RM |
8200 | |
8201 | case TARGET_OBJECT_AUXV: | |
0876f84a DJ |
8202 | gdb_assert (annex == NULL); |
8203 | return remote_read_qxfer (ops, "auxv", annex, readbuf, offset, len, | |
8204 | &remote_protocol_packets[PACKET_qXfer_auxv]); | |
802188a7 | 8205 | |
23181151 DJ |
8206 | case TARGET_OBJECT_AVAILABLE_FEATURES: |
8207 | return remote_read_qxfer | |
8208 | (ops, "features", annex, readbuf, offset, len, | |
8209 | &remote_protocol_packets[PACKET_qXfer_features]); | |
8210 | ||
cfa9d6d9 DJ |
8211 | case TARGET_OBJECT_LIBRARIES: |
8212 | return remote_read_qxfer | |
8213 | (ops, "libraries", annex, readbuf, offset, len, | |
8214 | &remote_protocol_packets[PACKET_qXfer_libraries]); | |
8215 | ||
fd79ecee DJ |
8216 | case TARGET_OBJECT_MEMORY_MAP: |
8217 | gdb_assert (annex == NULL); | |
8218 | return remote_read_qxfer (ops, "memory-map", annex, readbuf, offset, len, | |
8219 | &remote_protocol_packets[PACKET_qXfer_memory_map]); | |
8220 | ||
07e059b5 VP |
8221 | case TARGET_OBJECT_OSDATA: |
8222 | /* Should only get here if we're connected. */ | |
8223 | gdb_assert (remote_desc); | |
8224 | return remote_read_qxfer | |
8225 | (ops, "osdata", annex, readbuf, offset, len, | |
8226 | &remote_protocol_packets[PACKET_qXfer_osdata]); | |
8227 | ||
dc146f7c VP |
8228 | case TARGET_OBJECT_THREADS: |
8229 | gdb_assert (annex == NULL); | |
8230 | return remote_read_qxfer (ops, "threads", annex, readbuf, offset, len, | |
8231 | &remote_protocol_packets[PACKET_qXfer_threads]); | |
8232 | ||
1e3ff5ad | 8233 | default: |
c906108c SS |
8234 | return -1; |
8235 | } | |
8236 | ||
4b8a223f | 8237 | /* Note: a zero OFFSET and LEN can be used to query the minimum |
1e3ff5ad | 8238 | buffer size. */ |
4b8a223f | 8239 | if (offset == 0 && len == 0) |
ea9c271d DJ |
8240 | return (get_remote_packet_size ()); |
8241 | /* Minimum outbuf size is get_remote_packet_size (). If LEN is not | |
24b06219 | 8242 | large enough let the caller deal with it. */ |
ea9c271d | 8243 | if (len < get_remote_packet_size ()) |
1e3ff5ad | 8244 | return -1; |
ea9c271d | 8245 | len = get_remote_packet_size (); |
1e3ff5ad | 8246 | |
23860348 | 8247 | /* Except for querying the minimum buffer size, target must be open. */ |
c5aa993b | 8248 | if (!remote_desc) |
8a3fe4f8 | 8249 | error (_("remote query is only available after target open")); |
c906108c | 8250 | |
1e3ff5ad | 8251 | gdb_assert (annex != NULL); |
4b8a223f | 8252 | gdb_assert (readbuf != NULL); |
c906108c | 8253 | |
6d820c5c | 8254 | p2 = rs->buf; |
c906108c SS |
8255 | *p2++ = 'q'; |
8256 | *p2++ = query_type; | |
8257 | ||
23860348 MS |
8258 | /* We used one buffer char for the remote protocol q command and |
8259 | another for the query type. As the remote protocol encapsulation | |
8260 | uses 4 chars plus one extra in case we are debugging | |
8261 | (remote_debug), we have PBUFZIZ - 7 left to pack the query | |
8262 | string. */ | |
c906108c | 8263 | i = 0; |
ea9c271d | 8264 | while (annex[i] && (i < (get_remote_packet_size () - 8))) |
c906108c | 8265 | { |
1e3ff5ad AC |
8266 | /* Bad caller may have sent forbidden characters. */ |
8267 | gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#'); | |
8268 | *p2++ = annex[i]; | |
c906108c SS |
8269 | i++; |
8270 | } | |
1e3ff5ad AC |
8271 | *p2 = '\0'; |
8272 | gdb_assert (annex[i] == '\0'); | |
c906108c | 8273 | |
6d820c5c | 8274 | i = putpkt (rs->buf); |
c5aa993b JM |
8275 | if (i < 0) |
8276 | return i; | |
c906108c | 8277 | |
6d820c5c DJ |
8278 | getpkt (&rs->buf, &rs->buf_size, 0); |
8279 | strcpy ((char *) readbuf, rs->buf); | |
c906108c | 8280 | |
cfd77fa1 | 8281 | return strlen ((char *) readbuf); |
c906108c SS |
8282 | } |
8283 | ||
08388c79 DE |
8284 | static int |
8285 | remote_search_memory (struct target_ops* ops, | |
8286 | CORE_ADDR start_addr, ULONGEST search_space_len, | |
8287 | const gdb_byte *pattern, ULONGEST pattern_len, | |
8288 | CORE_ADDR *found_addrp) | |
8289 | { | |
5af949e3 | 8290 | int addr_size = gdbarch_addr_bit (target_gdbarch) / 8; |
08388c79 DE |
8291 | struct remote_state *rs = get_remote_state (); |
8292 | int max_size = get_memory_write_packet_size (); | |
8293 | struct packet_config *packet = | |
8294 | &remote_protocol_packets[PACKET_qSearch_memory]; | |
8295 | /* number of packet bytes used to encode the pattern, | |
8296 | this could be more than PATTERN_LEN due to escape characters */ | |
8297 | int escaped_pattern_len; | |
8298 | /* amount of pattern that was encodable in the packet */ | |
8299 | int used_pattern_len; | |
8300 | int i; | |
8301 | int found; | |
8302 | ULONGEST found_addr; | |
8303 | ||
8304 | /* Don't go to the target if we don't have to. | |
8305 | This is done before checking packet->support to avoid the possibility that | |
8306 | a success for this edge case means the facility works in general. */ | |
8307 | if (pattern_len > search_space_len) | |
8308 | return 0; | |
8309 | if (pattern_len == 0) | |
8310 | { | |
8311 | *found_addrp = start_addr; | |
8312 | return 1; | |
8313 | } | |
8314 | ||
8315 | /* If we already know the packet isn't supported, fall back to the simple | |
8316 | way of searching memory. */ | |
8317 | ||
8318 | if (packet->support == PACKET_DISABLE) | |
8319 | { | |
8320 | /* Target doesn't provided special support, fall back and use the | |
8321 | standard support (copy memory and do the search here). */ | |
8322 | return simple_search_memory (ops, start_addr, search_space_len, | |
8323 | pattern, pattern_len, found_addrp); | |
8324 | } | |
8325 | ||
8326 | /* Insert header. */ | |
8327 | i = snprintf (rs->buf, max_size, | |
8328 | "qSearch:memory:%s;%s;", | |
5af949e3 | 8329 | phex_nz (start_addr, addr_size), |
08388c79 DE |
8330 | phex_nz (search_space_len, sizeof (search_space_len))); |
8331 | max_size -= (i + 1); | |
8332 | ||
8333 | /* Escape as much data as fits into rs->buf. */ | |
8334 | escaped_pattern_len = | |
8335 | remote_escape_output (pattern, pattern_len, (rs->buf + i), | |
8336 | &used_pattern_len, max_size); | |
8337 | ||
8338 | /* Bail if the pattern is too large. */ | |
8339 | if (used_pattern_len != pattern_len) | |
10e0fa18 | 8340 | error ("Pattern is too large to transmit to remote target."); |
08388c79 DE |
8341 | |
8342 | if (putpkt_binary (rs->buf, i + escaped_pattern_len) < 0 | |
8343 | || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0 | |
8344 | || packet_ok (rs->buf, packet) != PACKET_OK) | |
8345 | { | |
8346 | /* The request may not have worked because the command is not | |
8347 | supported. If so, fall back to the simple way. */ | |
8348 | if (packet->support == PACKET_DISABLE) | |
8349 | { | |
8350 | return simple_search_memory (ops, start_addr, search_space_len, | |
8351 | pattern, pattern_len, found_addrp); | |
8352 | } | |
8353 | return -1; | |
8354 | } | |
8355 | ||
8356 | if (rs->buf[0] == '0') | |
8357 | found = 0; | |
8358 | else if (rs->buf[0] == '1') | |
8359 | { | |
8360 | found = 1; | |
8361 | if (rs->buf[1] != ',') | |
10e0fa18 | 8362 | error (_("Unknown qSearch:memory reply: %s"), rs->buf); |
08388c79 DE |
8363 | unpack_varlen_hex (rs->buf + 2, &found_addr); |
8364 | *found_addrp = found_addr; | |
8365 | } | |
8366 | else | |
10e0fa18 | 8367 | error (_("Unknown qSearch:memory reply: %s"), rs->buf); |
08388c79 DE |
8368 | |
8369 | return found; | |
8370 | } | |
8371 | ||
96baa820 JM |
8372 | static void |
8373 | remote_rcmd (char *command, | |
d9fcf2fb | 8374 | struct ui_file *outbuf) |
96baa820 | 8375 | { |
d01949b6 | 8376 | struct remote_state *rs = get_remote_state (); |
2e9f7625 | 8377 | char *p = rs->buf; |
96baa820 JM |
8378 | |
8379 | if (!remote_desc) | |
8a3fe4f8 | 8380 | error (_("remote rcmd is only available after target open")); |
96baa820 | 8381 | |
23860348 | 8382 | /* Send a NULL command across as an empty command. */ |
7be570e7 JM |
8383 | if (command == NULL) |
8384 | command = ""; | |
8385 | ||
23860348 | 8386 | /* The query prefix. */ |
2e9f7625 DJ |
8387 | strcpy (rs->buf, "qRcmd,"); |
8388 | p = strchr (rs->buf, '\0'); | |
96baa820 | 8389 | |
2e9f7625 | 8390 | if ((strlen (rs->buf) + strlen (command) * 2 + 8/*misc*/) > get_remote_packet_size ()) |
8a3fe4f8 | 8391 | error (_("\"monitor\" command ``%s'' is too long."), command); |
96baa820 | 8392 | |
23860348 | 8393 | /* Encode the actual command. */ |
cfd77fa1 | 8394 | bin2hex ((gdb_byte *) command, p, 0); |
96baa820 | 8395 | |
6d820c5c | 8396 | if (putpkt (rs->buf) < 0) |
8a3fe4f8 | 8397 | error (_("Communication problem with target.")); |
96baa820 JM |
8398 | |
8399 | /* get/display the response */ | |
8400 | while (1) | |
8401 | { | |
2e9f7625 DJ |
8402 | char *buf; |
8403 | ||
00bf0b85 | 8404 | /* XXX - see also remote_get_noisy_reply(). */ |
2e9f7625 | 8405 | rs->buf[0] = '\0'; |
6d820c5c | 8406 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 8407 | buf = rs->buf; |
96baa820 | 8408 | if (buf[0] == '\0') |
8a3fe4f8 | 8409 | error (_("Target does not support this command.")); |
96baa820 JM |
8410 | if (buf[0] == 'O' && buf[1] != 'K') |
8411 | { | |
23860348 | 8412 | remote_console_output (buf + 1); /* 'O' message from stub. */ |
96baa820 JM |
8413 | continue; |
8414 | } | |
8415 | if (strcmp (buf, "OK") == 0) | |
8416 | break; | |
7be570e7 JM |
8417 | if (strlen (buf) == 3 && buf[0] == 'E' |
8418 | && isdigit (buf[1]) && isdigit (buf[2])) | |
8419 | { | |
8a3fe4f8 | 8420 | error (_("Protocol error with Rcmd")); |
7be570e7 | 8421 | } |
96baa820 JM |
8422 | for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2) |
8423 | { | |
8424 | char c = (fromhex (p[0]) << 4) + fromhex (p[1]); | |
a744cf53 | 8425 | |
96baa820 JM |
8426 | fputc_unfiltered (c, outbuf); |
8427 | } | |
8428 | break; | |
8429 | } | |
8430 | } | |
8431 | ||
fd79ecee DJ |
8432 | static VEC(mem_region_s) * |
8433 | remote_memory_map (struct target_ops *ops) | |
8434 | { | |
8435 | VEC(mem_region_s) *result = NULL; | |
8436 | char *text = target_read_stralloc (¤t_target, | |
8437 | TARGET_OBJECT_MEMORY_MAP, NULL); | |
8438 | ||
8439 | if (text) | |
8440 | { | |
8441 | struct cleanup *back_to = make_cleanup (xfree, text); | |
a744cf53 | 8442 | |
fd79ecee DJ |
8443 | result = parse_memory_map (text); |
8444 | do_cleanups (back_to); | |
8445 | } | |
8446 | ||
8447 | return result; | |
8448 | } | |
8449 | ||
c906108c | 8450 | static void |
fba45db2 | 8451 | packet_command (char *args, int from_tty) |
c906108c | 8452 | { |
d01949b6 | 8453 | struct remote_state *rs = get_remote_state (); |
c906108c | 8454 | |
c5aa993b | 8455 | if (!remote_desc) |
8a3fe4f8 | 8456 | error (_("command can only be used with remote target")); |
c906108c | 8457 | |
c5aa993b | 8458 | if (!args) |
8a3fe4f8 | 8459 | error (_("remote-packet command requires packet text as argument")); |
c906108c SS |
8460 | |
8461 | puts_filtered ("sending: "); | |
8462 | print_packet (args); | |
8463 | puts_filtered ("\n"); | |
8464 | putpkt (args); | |
8465 | ||
6d820c5c | 8466 | getpkt (&rs->buf, &rs->buf_size, 0); |
c906108c | 8467 | puts_filtered ("received: "); |
6d820c5c | 8468 | print_packet (rs->buf); |
c906108c SS |
8469 | puts_filtered ("\n"); |
8470 | } | |
8471 | ||
8472 | #if 0 | |
23860348 | 8473 | /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */ |
c906108c | 8474 | |
a14ed312 | 8475 | static void display_thread_info (struct gdb_ext_thread_info *info); |
c906108c | 8476 | |
a14ed312 | 8477 | static void threadset_test_cmd (char *cmd, int tty); |
c906108c | 8478 | |
a14ed312 | 8479 | static void threadalive_test (char *cmd, int tty); |
c906108c | 8480 | |
a14ed312 | 8481 | static void threadlist_test_cmd (char *cmd, int tty); |
c906108c | 8482 | |
23860348 | 8483 | int get_and_display_threadinfo (threadref *ref); |
c906108c | 8484 | |
a14ed312 | 8485 | static void threadinfo_test_cmd (char *cmd, int tty); |
c906108c | 8486 | |
23860348 | 8487 | static int thread_display_step (threadref *ref, void *context); |
c906108c | 8488 | |
a14ed312 | 8489 | static void threadlist_update_test_cmd (char *cmd, int tty); |
c906108c | 8490 | |
a14ed312 | 8491 | static void init_remote_threadtests (void); |
c906108c | 8492 | |
23860348 | 8493 | #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */ |
c906108c SS |
8494 | |
8495 | static void | |
fba45db2 | 8496 | threadset_test_cmd (char *cmd, int tty) |
c906108c SS |
8497 | { |
8498 | int sample_thread = SAMPLE_THREAD; | |
8499 | ||
a3f17187 | 8500 | printf_filtered (_("Remote threadset test\n")); |
79d7f229 | 8501 | set_general_thread (sample_thread); |
c906108c SS |
8502 | } |
8503 | ||
8504 | ||
8505 | static void | |
fba45db2 | 8506 | threadalive_test (char *cmd, int tty) |
c906108c SS |
8507 | { |
8508 | int sample_thread = SAMPLE_THREAD; | |
79d7f229 PA |
8509 | int pid = ptid_get_pid (inferior_ptid); |
8510 | ptid_t ptid = ptid_build (pid, 0, sample_thread); | |
c906108c | 8511 | |
79d7f229 | 8512 | if (remote_thread_alive (ptid)) |
c906108c SS |
8513 | printf_filtered ("PASS: Thread alive test\n"); |
8514 | else | |
8515 | printf_filtered ("FAIL: Thread alive test\n"); | |
8516 | } | |
8517 | ||
23860348 | 8518 | void output_threadid (char *title, threadref *ref); |
c906108c SS |
8519 | |
8520 | void | |
fba45db2 | 8521 | output_threadid (char *title, threadref *ref) |
c906108c SS |
8522 | { |
8523 | char hexid[20]; | |
8524 | ||
23860348 | 8525 | pack_threadid (&hexid[0], ref); /* Convert threead id into hex. */ |
c906108c SS |
8526 | hexid[16] = 0; |
8527 | printf_filtered ("%s %s\n", title, (&hexid[0])); | |
8528 | } | |
8529 | ||
8530 | static void | |
fba45db2 | 8531 | threadlist_test_cmd (char *cmd, int tty) |
c906108c SS |
8532 | { |
8533 | int startflag = 1; | |
8534 | threadref nextthread; | |
8535 | int done, result_count; | |
8536 | threadref threadlist[3]; | |
8537 | ||
8538 | printf_filtered ("Remote Threadlist test\n"); | |
8539 | if (!remote_get_threadlist (startflag, &nextthread, 3, &done, | |
8540 | &result_count, &threadlist[0])) | |
8541 | printf_filtered ("FAIL: threadlist test\n"); | |
8542 | else | |
8543 | { | |
8544 | threadref *scan = threadlist; | |
8545 | threadref *limit = scan + result_count; | |
8546 | ||
8547 | while (scan < limit) | |
8548 | output_threadid (" thread ", scan++); | |
8549 | } | |
8550 | } | |
8551 | ||
8552 | void | |
fba45db2 | 8553 | display_thread_info (struct gdb_ext_thread_info *info) |
c906108c SS |
8554 | { |
8555 | output_threadid ("Threadid: ", &info->threadid); | |
8556 | printf_filtered ("Name: %s\n ", info->shortname); | |
8557 | printf_filtered ("State: %s\n", info->display); | |
8558 | printf_filtered ("other: %s\n\n", info->more_display); | |
8559 | } | |
8560 | ||
8561 | int | |
fba45db2 | 8562 | get_and_display_threadinfo (threadref *ref) |
c906108c SS |
8563 | { |
8564 | int result; | |
8565 | int set; | |
8566 | struct gdb_ext_thread_info threadinfo; | |
8567 | ||
8568 | set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME | |
8569 | | TAG_MOREDISPLAY | TAG_DISPLAY; | |
8570 | if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo))) | |
8571 | display_thread_info (&threadinfo); | |
8572 | return result; | |
8573 | } | |
8574 | ||
8575 | static void | |
fba45db2 | 8576 | threadinfo_test_cmd (char *cmd, int tty) |
c906108c SS |
8577 | { |
8578 | int athread = SAMPLE_THREAD; | |
8579 | threadref thread; | |
8580 | int set; | |
8581 | ||
8582 | int_to_threadref (&thread, athread); | |
8583 | printf_filtered ("Remote Threadinfo test\n"); | |
8584 | if (!get_and_display_threadinfo (&thread)) | |
8585 | printf_filtered ("FAIL cannot get thread info\n"); | |
8586 | } | |
8587 | ||
8588 | static int | |
fba45db2 | 8589 | thread_display_step (threadref *ref, void *context) |
c906108c SS |
8590 | { |
8591 | /* output_threadid(" threadstep ",ref); *//* simple test */ | |
8592 | return get_and_display_threadinfo (ref); | |
8593 | } | |
8594 | ||
8595 | static void | |
fba45db2 | 8596 | threadlist_update_test_cmd (char *cmd, int tty) |
c906108c SS |
8597 | { |
8598 | printf_filtered ("Remote Threadlist update test\n"); | |
8599 | remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS); | |
8600 | } | |
8601 | ||
8602 | static void | |
8603 | init_remote_threadtests (void) | |
8604 | { | |
1bedd215 AC |
8605 | add_com ("tlist", class_obscure, threadlist_test_cmd, _("\ |
8606 | Fetch and print the remote list of thread identifiers, one pkt only")); | |
c906108c | 8607 | add_com ("tinfo", class_obscure, threadinfo_test_cmd, |
1bedd215 | 8608 | _("Fetch and display info about one thread")); |
c906108c | 8609 | add_com ("tset", class_obscure, threadset_test_cmd, |
1bedd215 | 8610 | _("Test setting to a different thread")); |
c906108c | 8611 | add_com ("tupd", class_obscure, threadlist_update_test_cmd, |
1bedd215 | 8612 | _("Iterate through updating all remote thread info")); |
c906108c | 8613 | add_com ("talive", class_obscure, threadalive_test, |
1bedd215 | 8614 | _(" Remote thread alive test ")); |
c906108c SS |
8615 | } |
8616 | ||
8617 | #endif /* 0 */ | |
8618 | ||
f3fb8c85 MS |
8619 | /* Convert a thread ID to a string. Returns the string in a static |
8620 | buffer. */ | |
8621 | ||
8622 | static char * | |
117de6a9 | 8623 | remote_pid_to_str (struct target_ops *ops, ptid_t ptid) |
f3fb8c85 | 8624 | { |
79d7f229 | 8625 | static char buf[64]; |
82f73884 | 8626 | struct remote_state *rs = get_remote_state (); |
f3fb8c85 | 8627 | |
ecd0ada5 PA |
8628 | if (ptid_is_pid (ptid)) |
8629 | { | |
8630 | /* Printing an inferior target id. */ | |
8631 | ||
8632 | /* When multi-process extensions are off, there's no way in the | |
8633 | remote protocol to know the remote process id, if there's any | |
8634 | at all. There's one exception --- when we're connected with | |
8635 | target extended-remote, and we manually attached to a process | |
8636 | with "attach PID". We don't record anywhere a flag that | |
8637 | allows us to distinguish that case from the case of | |
8638 | connecting with extended-remote and the stub already being | |
8639 | attached to a process, and reporting yes to qAttached, hence | |
8640 | no smart special casing here. */ | |
8641 | if (!remote_multi_process_p (rs)) | |
8642 | { | |
8643 | xsnprintf (buf, sizeof buf, "Remote target"); | |
8644 | return buf; | |
8645 | } | |
8646 | ||
8647 | return normal_pid_to_str (ptid); | |
82f73884 | 8648 | } |
ecd0ada5 | 8649 | else |
79d7f229 | 8650 | { |
ecd0ada5 PA |
8651 | if (ptid_equal (magic_null_ptid, ptid)) |
8652 | xsnprintf (buf, sizeof buf, "Thread <main>"); | |
8653 | else if (remote_multi_process_p (rs)) | |
8654 | xsnprintf (buf, sizeof buf, "Thread %d.%ld", | |
8655 | ptid_get_pid (ptid), ptid_get_tid (ptid)); | |
8656 | else | |
8657 | xsnprintf (buf, sizeof buf, "Thread %ld", | |
8658 | ptid_get_tid (ptid)); | |
79d7f229 PA |
8659 | return buf; |
8660 | } | |
f3fb8c85 MS |
8661 | } |
8662 | ||
38691318 KB |
8663 | /* Get the address of the thread local variable in OBJFILE which is |
8664 | stored at OFFSET within the thread local storage for thread PTID. */ | |
8665 | ||
8666 | static CORE_ADDR | |
117de6a9 PA |
8667 | remote_get_thread_local_address (struct target_ops *ops, |
8668 | ptid_t ptid, CORE_ADDR lm, CORE_ADDR offset) | |
38691318 | 8669 | { |
444abaca | 8670 | if (remote_protocol_packets[PACKET_qGetTLSAddr].support != PACKET_DISABLE) |
38691318 KB |
8671 | { |
8672 | struct remote_state *rs = get_remote_state (); | |
6d820c5c | 8673 | char *p = rs->buf; |
82f73884 | 8674 | char *endp = rs->buf + get_remote_packet_size (); |
571dd617 | 8675 | enum packet_result result; |
38691318 KB |
8676 | |
8677 | strcpy (p, "qGetTLSAddr:"); | |
8678 | p += strlen (p); | |
82f73884 | 8679 | p = write_ptid (p, endp, ptid); |
38691318 KB |
8680 | *p++ = ','; |
8681 | p += hexnumstr (p, offset); | |
8682 | *p++ = ','; | |
8683 | p += hexnumstr (p, lm); | |
8684 | *p++ = '\0'; | |
8685 | ||
6d820c5c DJ |
8686 | putpkt (rs->buf); |
8687 | getpkt (&rs->buf, &rs->buf_size, 0); | |
8688 | result = packet_ok (rs->buf, &remote_protocol_packets[PACKET_qGetTLSAddr]); | |
571dd617 | 8689 | if (result == PACKET_OK) |
38691318 KB |
8690 | { |
8691 | ULONGEST result; | |
8692 | ||
6d820c5c | 8693 | unpack_varlen_hex (rs->buf, &result); |
38691318 KB |
8694 | return result; |
8695 | } | |
571dd617 | 8696 | else if (result == PACKET_UNKNOWN) |
109c3e39 AC |
8697 | throw_error (TLS_GENERIC_ERROR, |
8698 | _("Remote target doesn't support qGetTLSAddr packet")); | |
38691318 | 8699 | else |
109c3e39 AC |
8700 | throw_error (TLS_GENERIC_ERROR, |
8701 | _("Remote target failed to process qGetTLSAddr request")); | |
38691318 KB |
8702 | } |
8703 | else | |
109c3e39 AC |
8704 | throw_error (TLS_GENERIC_ERROR, |
8705 | _("TLS not supported or disabled on this target")); | |
38691318 KB |
8706 | /* Not reached. */ |
8707 | return 0; | |
8708 | } | |
8709 | ||
711e434b PM |
8710 | /* Provide thread local base, i.e. Thread Information Block address. |
8711 | Returns 1 if ptid is found and thread_local_base is non zero. */ | |
8712 | ||
8713 | int | |
8714 | remote_get_tib_address (ptid_t ptid, CORE_ADDR *addr) | |
8715 | { | |
8716 | if (remote_protocol_packets[PACKET_qGetTIBAddr].support != PACKET_DISABLE) | |
8717 | { | |
8718 | struct remote_state *rs = get_remote_state (); | |
8719 | char *p = rs->buf; | |
8720 | char *endp = rs->buf + get_remote_packet_size (); | |
8721 | enum packet_result result; | |
8722 | ||
8723 | strcpy (p, "qGetTIBAddr:"); | |
8724 | p += strlen (p); | |
8725 | p = write_ptid (p, endp, ptid); | |
8726 | *p++ = '\0'; | |
8727 | ||
8728 | putpkt (rs->buf); | |
8729 | getpkt (&rs->buf, &rs->buf_size, 0); | |
8730 | result = packet_ok (rs->buf, | |
8731 | &remote_protocol_packets[PACKET_qGetTIBAddr]); | |
8732 | if (result == PACKET_OK) | |
8733 | { | |
8734 | ULONGEST result; | |
8735 | ||
8736 | unpack_varlen_hex (rs->buf, &result); | |
8737 | if (addr) | |
8738 | *addr = (CORE_ADDR) result; | |
8739 | return 1; | |
8740 | } | |
8741 | else if (result == PACKET_UNKNOWN) | |
8742 | error (_("Remote target doesn't support qGetTIBAddr packet")); | |
8743 | else | |
8744 | error (_("Remote target failed to process qGetTIBAddr request")); | |
8745 | } | |
8746 | else | |
8747 | error (_("qGetTIBAddr not supported or disabled on this target")); | |
8748 | /* Not reached. */ | |
8749 | return 0; | |
8750 | } | |
8751 | ||
29709017 DJ |
8752 | /* Support for inferring a target description based on the current |
8753 | architecture and the size of a 'g' packet. While the 'g' packet | |
8754 | can have any size (since optional registers can be left off the | |
8755 | end), some sizes are easily recognizable given knowledge of the | |
8756 | approximate architecture. */ | |
8757 | ||
8758 | struct remote_g_packet_guess | |
8759 | { | |
8760 | int bytes; | |
8761 | const struct target_desc *tdesc; | |
8762 | }; | |
8763 | typedef struct remote_g_packet_guess remote_g_packet_guess_s; | |
8764 | DEF_VEC_O(remote_g_packet_guess_s); | |
8765 | ||
8766 | struct remote_g_packet_data | |
8767 | { | |
8768 | VEC(remote_g_packet_guess_s) *guesses; | |
8769 | }; | |
8770 | ||
8771 | static struct gdbarch_data *remote_g_packet_data_handle; | |
8772 | ||
8773 | static void * | |
8774 | remote_g_packet_data_init (struct obstack *obstack) | |
8775 | { | |
8776 | return OBSTACK_ZALLOC (obstack, struct remote_g_packet_data); | |
8777 | } | |
8778 | ||
8779 | void | |
8780 | register_remote_g_packet_guess (struct gdbarch *gdbarch, int bytes, | |
8781 | const struct target_desc *tdesc) | |
8782 | { | |
8783 | struct remote_g_packet_data *data | |
8784 | = gdbarch_data (gdbarch, remote_g_packet_data_handle); | |
8785 | struct remote_g_packet_guess new_guess, *guess; | |
8786 | int ix; | |
8787 | ||
8788 | gdb_assert (tdesc != NULL); | |
8789 | ||
8790 | for (ix = 0; | |
8791 | VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess); | |
8792 | ix++) | |
8793 | if (guess->bytes == bytes) | |
8794 | internal_error (__FILE__, __LINE__, | |
8795 | "Duplicate g packet description added for size %d", | |
8796 | bytes); | |
8797 | ||
8798 | new_guess.bytes = bytes; | |
8799 | new_guess.tdesc = tdesc; | |
8800 | VEC_safe_push (remote_g_packet_guess_s, data->guesses, &new_guess); | |
8801 | } | |
8802 | ||
d962ef82 DJ |
8803 | /* Return 1 if remote_read_description would do anything on this target |
8804 | and architecture, 0 otherwise. */ | |
8805 | ||
8806 | static int | |
8807 | remote_read_description_p (struct target_ops *target) | |
8808 | { | |
8809 | struct remote_g_packet_data *data | |
8810 | = gdbarch_data (target_gdbarch, remote_g_packet_data_handle); | |
8811 | ||
8812 | if (!VEC_empty (remote_g_packet_guess_s, data->guesses)) | |
8813 | return 1; | |
8814 | ||
8815 | return 0; | |
8816 | } | |
8817 | ||
29709017 DJ |
8818 | static const struct target_desc * |
8819 | remote_read_description (struct target_ops *target) | |
8820 | { | |
8821 | struct remote_g_packet_data *data | |
1cf3db46 | 8822 | = gdbarch_data (target_gdbarch, remote_g_packet_data_handle); |
29709017 | 8823 | |
d962ef82 DJ |
8824 | /* Do not try this during initial connection, when we do not know |
8825 | whether there is a running but stopped thread. */ | |
8826 | if (!target_has_execution || ptid_equal (inferior_ptid, null_ptid)) | |
8827 | return NULL; | |
8828 | ||
29709017 DJ |
8829 | if (!VEC_empty (remote_g_packet_guess_s, data->guesses)) |
8830 | { | |
8831 | struct remote_g_packet_guess *guess; | |
8832 | int ix; | |
8833 | int bytes = send_g_packet (); | |
8834 | ||
8835 | for (ix = 0; | |
8836 | VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess); | |
8837 | ix++) | |
8838 | if (guess->bytes == bytes) | |
8839 | return guess->tdesc; | |
8840 | ||
8841 | /* We discard the g packet. A minor optimization would be to | |
8842 | hold on to it, and fill the register cache once we have selected | |
8843 | an architecture, but it's too tricky to do safely. */ | |
8844 | } | |
8845 | ||
8846 | return NULL; | |
8847 | } | |
8848 | ||
a6b151f1 DJ |
8849 | /* Remote file transfer support. This is host-initiated I/O, not |
8850 | target-initiated; for target-initiated, see remote-fileio.c. */ | |
8851 | ||
8852 | /* If *LEFT is at least the length of STRING, copy STRING to | |
8853 | *BUFFER, update *BUFFER to point to the new end of the buffer, and | |
8854 | decrease *LEFT. Otherwise raise an error. */ | |
8855 | ||
8856 | static void | |
8857 | remote_buffer_add_string (char **buffer, int *left, char *string) | |
8858 | { | |
8859 | int len = strlen (string); | |
8860 | ||
8861 | if (len > *left) | |
8862 | error (_("Packet too long for target.")); | |
8863 | ||
8864 | memcpy (*buffer, string, len); | |
8865 | *buffer += len; | |
8866 | *left -= len; | |
8867 | ||
8868 | /* NUL-terminate the buffer as a convenience, if there is | |
8869 | room. */ | |
8870 | if (*left) | |
8871 | **buffer = '\0'; | |
8872 | } | |
8873 | ||
8874 | /* If *LEFT is large enough, hex encode LEN bytes from BYTES into | |
8875 | *BUFFER, update *BUFFER to point to the new end of the buffer, and | |
8876 | decrease *LEFT. Otherwise raise an error. */ | |
8877 | ||
8878 | static void | |
8879 | remote_buffer_add_bytes (char **buffer, int *left, const gdb_byte *bytes, | |
8880 | int len) | |
8881 | { | |
8882 | if (2 * len > *left) | |
8883 | error (_("Packet too long for target.")); | |
8884 | ||
8885 | bin2hex (bytes, *buffer, len); | |
8886 | *buffer += 2 * len; | |
8887 | *left -= 2 * len; | |
8888 | ||
8889 | /* NUL-terminate the buffer as a convenience, if there is | |
8890 | room. */ | |
8891 | if (*left) | |
8892 | **buffer = '\0'; | |
8893 | } | |
8894 | ||
8895 | /* If *LEFT is large enough, convert VALUE to hex and add it to | |
8896 | *BUFFER, update *BUFFER to point to the new end of the buffer, and | |
8897 | decrease *LEFT. Otherwise raise an error. */ | |
8898 | ||
8899 | static void | |
8900 | remote_buffer_add_int (char **buffer, int *left, ULONGEST value) | |
8901 | { | |
8902 | int len = hexnumlen (value); | |
8903 | ||
8904 | if (len > *left) | |
8905 | error (_("Packet too long for target.")); | |
8906 | ||
8907 | hexnumstr (*buffer, value); | |
8908 | *buffer += len; | |
8909 | *left -= len; | |
8910 | ||
8911 | /* NUL-terminate the buffer as a convenience, if there is | |
8912 | room. */ | |
8913 | if (*left) | |
8914 | **buffer = '\0'; | |
8915 | } | |
8916 | ||
8917 | /* Parse an I/O result packet from BUFFER. Set RETCODE to the return | |
8918 | value, *REMOTE_ERRNO to the remote error number or zero if none | |
8919 | was included, and *ATTACHMENT to point to the start of the annex | |
8920 | if any. The length of the packet isn't needed here; there may | |
8921 | be NUL bytes in BUFFER, but they will be after *ATTACHMENT. | |
8922 | ||
8923 | Return 0 if the packet could be parsed, -1 if it could not. If | |
8924 | -1 is returned, the other variables may not be initialized. */ | |
8925 | ||
8926 | static int | |
8927 | remote_hostio_parse_result (char *buffer, int *retcode, | |
8928 | int *remote_errno, char **attachment) | |
8929 | { | |
8930 | char *p, *p2; | |
8931 | ||
8932 | *remote_errno = 0; | |
8933 | *attachment = NULL; | |
8934 | ||
8935 | if (buffer[0] != 'F') | |
8936 | return -1; | |
8937 | ||
8938 | errno = 0; | |
8939 | *retcode = strtol (&buffer[1], &p, 16); | |
8940 | if (errno != 0 || p == &buffer[1]) | |
8941 | return -1; | |
8942 | ||
8943 | /* Check for ",errno". */ | |
8944 | if (*p == ',') | |
8945 | { | |
8946 | errno = 0; | |
8947 | *remote_errno = strtol (p + 1, &p2, 16); | |
8948 | if (errno != 0 || p + 1 == p2) | |
8949 | return -1; | |
8950 | p = p2; | |
8951 | } | |
8952 | ||
8953 | /* Check for ";attachment". If there is no attachment, the | |
8954 | packet should end here. */ | |
8955 | if (*p == ';') | |
8956 | { | |
8957 | *attachment = p + 1; | |
8958 | return 0; | |
8959 | } | |
8960 | else if (*p == '\0') | |
8961 | return 0; | |
8962 | else | |
8963 | return -1; | |
8964 | } | |
8965 | ||
8966 | /* Send a prepared I/O packet to the target and read its response. | |
8967 | The prepared packet is in the global RS->BUF before this function | |
8968 | is called, and the answer is there when we return. | |
8969 | ||
8970 | COMMAND_BYTES is the length of the request to send, which may include | |
8971 | binary data. WHICH_PACKET is the packet configuration to check | |
8972 | before attempting a packet. If an error occurs, *REMOTE_ERRNO | |
8973 | is set to the error number and -1 is returned. Otherwise the value | |
8974 | returned by the function is returned. | |
8975 | ||
8976 | ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an | |
8977 | attachment is expected; an error will be reported if there's a | |
8978 | mismatch. If one is found, *ATTACHMENT will be set to point into | |
8979 | the packet buffer and *ATTACHMENT_LEN will be set to the | |
8980 | attachment's length. */ | |
8981 | ||
8982 | static int | |
8983 | remote_hostio_send_command (int command_bytes, int which_packet, | |
8984 | int *remote_errno, char **attachment, | |
8985 | int *attachment_len) | |
8986 | { | |
8987 | struct remote_state *rs = get_remote_state (); | |
8988 | int ret, bytes_read; | |
8989 | char *attachment_tmp; | |
8990 | ||
f1838a98 UW |
8991 | if (!remote_desc |
8992 | || remote_protocol_packets[which_packet].support == PACKET_DISABLE) | |
a6b151f1 DJ |
8993 | { |
8994 | *remote_errno = FILEIO_ENOSYS; | |
8995 | return -1; | |
8996 | } | |
8997 | ||
8998 | putpkt_binary (rs->buf, command_bytes); | |
8999 | bytes_read = getpkt_sane (&rs->buf, &rs->buf_size, 0); | |
9000 | ||
9001 | /* If it timed out, something is wrong. Don't try to parse the | |
9002 | buffer. */ | |
9003 | if (bytes_read < 0) | |
9004 | { | |
9005 | *remote_errno = FILEIO_EINVAL; | |
9006 | return -1; | |
9007 | } | |
9008 | ||
9009 | switch (packet_ok (rs->buf, &remote_protocol_packets[which_packet])) | |
9010 | { | |
9011 | case PACKET_ERROR: | |
9012 | *remote_errno = FILEIO_EINVAL; | |
9013 | return -1; | |
9014 | case PACKET_UNKNOWN: | |
9015 | *remote_errno = FILEIO_ENOSYS; | |
9016 | return -1; | |
9017 | case PACKET_OK: | |
9018 | break; | |
9019 | } | |
9020 | ||
9021 | if (remote_hostio_parse_result (rs->buf, &ret, remote_errno, | |
9022 | &attachment_tmp)) | |
9023 | { | |
9024 | *remote_errno = FILEIO_EINVAL; | |
9025 | return -1; | |
9026 | } | |
9027 | ||
9028 | /* Make sure we saw an attachment if and only if we expected one. */ | |
9029 | if ((attachment_tmp == NULL && attachment != NULL) | |
9030 | || (attachment_tmp != NULL && attachment == NULL)) | |
9031 | { | |
9032 | *remote_errno = FILEIO_EINVAL; | |
9033 | return -1; | |
9034 | } | |
9035 | ||
9036 | /* If an attachment was found, it must point into the packet buffer; | |
9037 | work out how many bytes there were. */ | |
9038 | if (attachment_tmp != NULL) | |
9039 | { | |
9040 | *attachment = attachment_tmp; | |
9041 | *attachment_len = bytes_read - (*attachment - rs->buf); | |
9042 | } | |
9043 | ||
9044 | return ret; | |
9045 | } | |
9046 | ||
9047 | /* Open FILENAME on the remote target, using FLAGS and MODE. Return a | |
9048 | remote file descriptor, or -1 if an error occurs (and set | |
9049 | *REMOTE_ERRNO). */ | |
9050 | ||
9051 | static int | |
9052 | remote_hostio_open (const char *filename, int flags, int mode, | |
9053 | int *remote_errno) | |
9054 | { | |
9055 | struct remote_state *rs = get_remote_state (); | |
9056 | char *p = rs->buf; | |
9057 | int left = get_remote_packet_size () - 1; | |
9058 | ||
9059 | remote_buffer_add_string (&p, &left, "vFile:open:"); | |
9060 | ||
9061 | remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename, | |
9062 | strlen (filename)); | |
9063 | remote_buffer_add_string (&p, &left, ","); | |
9064 | ||
9065 | remote_buffer_add_int (&p, &left, flags); | |
9066 | remote_buffer_add_string (&p, &left, ","); | |
9067 | ||
9068 | remote_buffer_add_int (&p, &left, mode); | |
9069 | ||
9070 | return remote_hostio_send_command (p - rs->buf, PACKET_vFile_open, | |
9071 | remote_errno, NULL, NULL); | |
9072 | } | |
9073 | ||
9074 | /* Write up to LEN bytes from WRITE_BUF to FD on the remote target. | |
9075 | Return the number of bytes written, or -1 if an error occurs (and | |
9076 | set *REMOTE_ERRNO). */ | |
9077 | ||
9078 | static int | |
9079 | remote_hostio_pwrite (int fd, const gdb_byte *write_buf, int len, | |
9080 | ULONGEST offset, int *remote_errno) | |
9081 | { | |
9082 | struct remote_state *rs = get_remote_state (); | |
9083 | char *p = rs->buf; | |
9084 | int left = get_remote_packet_size (); | |
9085 | int out_len; | |
9086 | ||
9087 | remote_buffer_add_string (&p, &left, "vFile:pwrite:"); | |
9088 | ||
9089 | remote_buffer_add_int (&p, &left, fd); | |
9090 | remote_buffer_add_string (&p, &left, ","); | |
9091 | ||
9092 | remote_buffer_add_int (&p, &left, offset); | |
9093 | remote_buffer_add_string (&p, &left, ","); | |
9094 | ||
9095 | p += remote_escape_output (write_buf, len, p, &out_len, | |
9096 | get_remote_packet_size () - (p - rs->buf)); | |
9097 | ||
9098 | return remote_hostio_send_command (p - rs->buf, PACKET_vFile_pwrite, | |
9099 | remote_errno, NULL, NULL); | |
9100 | } | |
9101 | ||
9102 | /* Read up to LEN bytes FD on the remote target into READ_BUF | |
9103 | Return the number of bytes read, or -1 if an error occurs (and | |
9104 | set *REMOTE_ERRNO). */ | |
9105 | ||
9106 | static int | |
9107 | remote_hostio_pread (int fd, gdb_byte *read_buf, int len, | |
9108 | ULONGEST offset, int *remote_errno) | |
9109 | { | |
9110 | struct remote_state *rs = get_remote_state (); | |
9111 | char *p = rs->buf; | |
9112 | char *attachment; | |
9113 | int left = get_remote_packet_size (); | |
9114 | int ret, attachment_len; | |
9115 | int read_len; | |
9116 | ||
9117 | remote_buffer_add_string (&p, &left, "vFile:pread:"); | |
9118 | ||
9119 | remote_buffer_add_int (&p, &left, fd); | |
9120 | remote_buffer_add_string (&p, &left, ","); | |
9121 | ||
9122 | remote_buffer_add_int (&p, &left, len); | |
9123 | remote_buffer_add_string (&p, &left, ","); | |
9124 | ||
9125 | remote_buffer_add_int (&p, &left, offset); | |
9126 | ||
9127 | ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_pread, | |
9128 | remote_errno, &attachment, | |
9129 | &attachment_len); | |
9130 | ||
9131 | if (ret < 0) | |
9132 | return ret; | |
9133 | ||
9134 | read_len = remote_unescape_input (attachment, attachment_len, | |
9135 | read_buf, len); | |
9136 | if (read_len != ret) | |
9137 | error (_("Read returned %d, but %d bytes."), ret, (int) read_len); | |
9138 | ||
9139 | return ret; | |
9140 | } | |
9141 | ||
9142 | /* Close FD on the remote target. Return 0, or -1 if an error occurs | |
9143 | (and set *REMOTE_ERRNO). */ | |
9144 | ||
9145 | static int | |
9146 | remote_hostio_close (int fd, int *remote_errno) | |
9147 | { | |
9148 | struct remote_state *rs = get_remote_state (); | |
9149 | char *p = rs->buf; | |
9150 | int left = get_remote_packet_size () - 1; | |
9151 | ||
9152 | remote_buffer_add_string (&p, &left, "vFile:close:"); | |
9153 | ||
9154 | remote_buffer_add_int (&p, &left, fd); | |
9155 | ||
9156 | return remote_hostio_send_command (p - rs->buf, PACKET_vFile_close, | |
9157 | remote_errno, NULL, NULL); | |
9158 | } | |
9159 | ||
9160 | /* Unlink FILENAME on the remote target. Return 0, or -1 if an error | |
9161 | occurs (and set *REMOTE_ERRNO). */ | |
9162 | ||
9163 | static int | |
9164 | remote_hostio_unlink (const char *filename, int *remote_errno) | |
9165 | { | |
9166 | struct remote_state *rs = get_remote_state (); | |
9167 | char *p = rs->buf; | |
9168 | int left = get_remote_packet_size () - 1; | |
9169 | ||
9170 | remote_buffer_add_string (&p, &left, "vFile:unlink:"); | |
9171 | ||
9172 | remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename, | |
9173 | strlen (filename)); | |
9174 | ||
9175 | return remote_hostio_send_command (p - rs->buf, PACKET_vFile_unlink, | |
9176 | remote_errno, NULL, NULL); | |
9177 | } | |
9178 | ||
9179 | static int | |
9180 | remote_fileio_errno_to_host (int errnum) | |
9181 | { | |
9182 | switch (errnum) | |
9183 | { | |
9184 | case FILEIO_EPERM: | |
9185 | return EPERM; | |
9186 | case FILEIO_ENOENT: | |
9187 | return ENOENT; | |
9188 | case FILEIO_EINTR: | |
9189 | return EINTR; | |
9190 | case FILEIO_EIO: | |
9191 | return EIO; | |
9192 | case FILEIO_EBADF: | |
9193 | return EBADF; | |
9194 | case FILEIO_EACCES: | |
9195 | return EACCES; | |
9196 | case FILEIO_EFAULT: | |
9197 | return EFAULT; | |
9198 | case FILEIO_EBUSY: | |
9199 | return EBUSY; | |
9200 | case FILEIO_EEXIST: | |
9201 | return EEXIST; | |
9202 | case FILEIO_ENODEV: | |
9203 | return ENODEV; | |
9204 | case FILEIO_ENOTDIR: | |
9205 | return ENOTDIR; | |
9206 | case FILEIO_EISDIR: | |
9207 | return EISDIR; | |
9208 | case FILEIO_EINVAL: | |
9209 | return EINVAL; | |
9210 | case FILEIO_ENFILE: | |
9211 | return ENFILE; | |
9212 | case FILEIO_EMFILE: | |
9213 | return EMFILE; | |
9214 | case FILEIO_EFBIG: | |
9215 | return EFBIG; | |
9216 | case FILEIO_ENOSPC: | |
9217 | return ENOSPC; | |
9218 | case FILEIO_ESPIPE: | |
9219 | return ESPIPE; | |
9220 | case FILEIO_EROFS: | |
9221 | return EROFS; | |
9222 | case FILEIO_ENOSYS: | |
9223 | return ENOSYS; | |
9224 | case FILEIO_ENAMETOOLONG: | |
9225 | return ENAMETOOLONG; | |
9226 | } | |
9227 | return -1; | |
9228 | } | |
9229 | ||
9230 | static char * | |
9231 | remote_hostio_error (int errnum) | |
9232 | { | |
9233 | int host_error = remote_fileio_errno_to_host (errnum); | |
9234 | ||
9235 | if (host_error == -1) | |
9236 | error (_("Unknown remote I/O error %d"), errnum); | |
9237 | else | |
9238 | error (_("Remote I/O error: %s"), safe_strerror (host_error)); | |
9239 | } | |
9240 | ||
a6b151f1 DJ |
9241 | static void |
9242 | remote_hostio_close_cleanup (void *opaque) | |
9243 | { | |
9244 | int fd = *(int *) opaque; | |
9245 | int remote_errno; | |
9246 | ||
9247 | remote_hostio_close (fd, &remote_errno); | |
9248 | } | |
9249 | ||
f1838a98 UW |
9250 | |
9251 | static void * | |
9252 | remote_bfd_iovec_open (struct bfd *abfd, void *open_closure) | |
9253 | { | |
9254 | const char *filename = bfd_get_filename (abfd); | |
9255 | int fd, remote_errno; | |
9256 | int *stream; | |
9257 | ||
9258 | gdb_assert (remote_filename_p (filename)); | |
9259 | ||
9260 | fd = remote_hostio_open (filename + 7, FILEIO_O_RDONLY, 0, &remote_errno); | |
9261 | if (fd == -1) | |
9262 | { | |
9263 | errno = remote_fileio_errno_to_host (remote_errno); | |
9264 | bfd_set_error (bfd_error_system_call); | |
9265 | return NULL; | |
9266 | } | |
9267 | ||
9268 | stream = xmalloc (sizeof (int)); | |
9269 | *stream = fd; | |
9270 | return stream; | |
9271 | } | |
9272 | ||
9273 | static int | |
9274 | remote_bfd_iovec_close (struct bfd *abfd, void *stream) | |
9275 | { | |
9276 | int fd = *(int *)stream; | |
9277 | int remote_errno; | |
9278 | ||
9279 | xfree (stream); | |
9280 | ||
9281 | /* Ignore errors on close; these may happen if the remote | |
9282 | connection was already torn down. */ | |
9283 | remote_hostio_close (fd, &remote_errno); | |
9284 | ||
9285 | return 1; | |
9286 | } | |
9287 | ||
9288 | static file_ptr | |
9289 | remote_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf, | |
9290 | file_ptr nbytes, file_ptr offset) | |
9291 | { | |
9292 | int fd = *(int *)stream; | |
9293 | int remote_errno; | |
9294 | file_ptr pos, bytes; | |
9295 | ||
9296 | pos = 0; | |
9297 | while (nbytes > pos) | |
9298 | { | |
9299 | bytes = remote_hostio_pread (fd, (char *)buf + pos, nbytes - pos, | |
9300 | offset + pos, &remote_errno); | |
9301 | if (bytes == 0) | |
9302 | /* Success, but no bytes, means end-of-file. */ | |
9303 | break; | |
9304 | if (bytes == -1) | |
9305 | { | |
9306 | errno = remote_fileio_errno_to_host (remote_errno); | |
9307 | bfd_set_error (bfd_error_system_call); | |
9308 | return -1; | |
9309 | } | |
9310 | ||
9311 | pos += bytes; | |
9312 | } | |
9313 | ||
9314 | return pos; | |
9315 | } | |
9316 | ||
9317 | static int | |
9318 | remote_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb) | |
9319 | { | |
9320 | /* FIXME: We should probably implement remote_hostio_stat. */ | |
9321 | sb->st_size = INT_MAX; | |
9322 | return 0; | |
9323 | } | |
9324 | ||
9325 | int | |
9326 | remote_filename_p (const char *filename) | |
9327 | { | |
9328 | return strncmp (filename, "remote:", 7) == 0; | |
9329 | } | |
9330 | ||
9331 | bfd * | |
9332 | remote_bfd_open (const char *remote_file, const char *target) | |
9333 | { | |
9334 | return bfd_openr_iovec (remote_file, target, | |
9335 | remote_bfd_iovec_open, NULL, | |
9336 | remote_bfd_iovec_pread, | |
9337 | remote_bfd_iovec_close, | |
9338 | remote_bfd_iovec_stat); | |
9339 | } | |
9340 | ||
a6b151f1 DJ |
9341 | void |
9342 | remote_file_put (const char *local_file, const char *remote_file, int from_tty) | |
9343 | { | |
9344 | struct cleanup *back_to, *close_cleanup; | |
9345 | int retcode, fd, remote_errno, bytes, io_size; | |
9346 | FILE *file; | |
9347 | gdb_byte *buffer; | |
9348 | int bytes_in_buffer; | |
9349 | int saw_eof; | |
9350 | ULONGEST offset; | |
9351 | ||
9352 | if (!remote_desc) | |
9353 | error (_("command can only be used with remote target")); | |
9354 | ||
9355 | file = fopen (local_file, "rb"); | |
9356 | if (file == NULL) | |
9357 | perror_with_name (local_file); | |
7c8a8b04 | 9358 | back_to = make_cleanup_fclose (file); |
a6b151f1 DJ |
9359 | |
9360 | fd = remote_hostio_open (remote_file, (FILEIO_O_WRONLY | FILEIO_O_CREAT | |
9361 | | FILEIO_O_TRUNC), | |
9362 | 0700, &remote_errno); | |
9363 | if (fd == -1) | |
9364 | remote_hostio_error (remote_errno); | |
9365 | ||
9366 | /* Send up to this many bytes at once. They won't all fit in the | |
9367 | remote packet limit, so we'll transfer slightly fewer. */ | |
9368 | io_size = get_remote_packet_size (); | |
9369 | buffer = xmalloc (io_size); | |
9370 | make_cleanup (xfree, buffer); | |
9371 | ||
9372 | close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd); | |
9373 | ||
9374 | bytes_in_buffer = 0; | |
9375 | saw_eof = 0; | |
9376 | offset = 0; | |
9377 | while (bytes_in_buffer || !saw_eof) | |
9378 | { | |
9379 | if (!saw_eof) | |
9380 | { | |
9381 | bytes = fread (buffer + bytes_in_buffer, 1, io_size - bytes_in_buffer, | |
9382 | file); | |
9383 | if (bytes == 0) | |
9384 | { | |
9385 | if (ferror (file)) | |
9386 | error (_("Error reading %s."), local_file); | |
9387 | else | |
9388 | { | |
9389 | /* EOF. Unless there is something still in the | |
9390 | buffer from the last iteration, we are done. */ | |
9391 | saw_eof = 1; | |
9392 | if (bytes_in_buffer == 0) | |
9393 | break; | |
9394 | } | |
9395 | } | |
9396 | } | |
9397 | else | |
9398 | bytes = 0; | |
9399 | ||
9400 | bytes += bytes_in_buffer; | |
9401 | bytes_in_buffer = 0; | |
9402 | ||
9403 | retcode = remote_hostio_pwrite (fd, buffer, bytes, offset, &remote_errno); | |
9404 | ||
9405 | if (retcode < 0) | |
9406 | remote_hostio_error (remote_errno); | |
9407 | else if (retcode == 0) | |
9408 | error (_("Remote write of %d bytes returned 0!"), bytes); | |
9409 | else if (retcode < bytes) | |
9410 | { | |
9411 | /* Short write. Save the rest of the read data for the next | |
9412 | write. */ | |
9413 | bytes_in_buffer = bytes - retcode; | |
9414 | memmove (buffer, buffer + retcode, bytes_in_buffer); | |
9415 | } | |
9416 | ||
9417 | offset += retcode; | |
9418 | } | |
9419 | ||
9420 | discard_cleanups (close_cleanup); | |
9421 | if (remote_hostio_close (fd, &remote_errno)) | |
9422 | remote_hostio_error (remote_errno); | |
9423 | ||
9424 | if (from_tty) | |
9425 | printf_filtered (_("Successfully sent file \"%s\".\n"), local_file); | |
9426 | do_cleanups (back_to); | |
9427 | } | |
9428 | ||
9429 | void | |
9430 | remote_file_get (const char *remote_file, const char *local_file, int from_tty) | |
9431 | { | |
9432 | struct cleanup *back_to, *close_cleanup; | |
cea39f65 | 9433 | int fd, remote_errno, bytes, io_size; |
a6b151f1 DJ |
9434 | FILE *file; |
9435 | gdb_byte *buffer; | |
9436 | ULONGEST offset; | |
9437 | ||
9438 | if (!remote_desc) | |
9439 | error (_("command can only be used with remote target")); | |
9440 | ||
9441 | fd = remote_hostio_open (remote_file, FILEIO_O_RDONLY, 0, &remote_errno); | |
9442 | if (fd == -1) | |
9443 | remote_hostio_error (remote_errno); | |
9444 | ||
9445 | file = fopen (local_file, "wb"); | |
9446 | if (file == NULL) | |
9447 | perror_with_name (local_file); | |
7c8a8b04 | 9448 | back_to = make_cleanup_fclose (file); |
a6b151f1 DJ |
9449 | |
9450 | /* Send up to this many bytes at once. They won't all fit in the | |
9451 | remote packet limit, so we'll transfer slightly fewer. */ | |
9452 | io_size = get_remote_packet_size (); | |
9453 | buffer = xmalloc (io_size); | |
9454 | make_cleanup (xfree, buffer); | |
9455 | ||
9456 | close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd); | |
9457 | ||
9458 | offset = 0; | |
9459 | while (1) | |
9460 | { | |
9461 | bytes = remote_hostio_pread (fd, buffer, io_size, offset, &remote_errno); | |
9462 | if (bytes == 0) | |
9463 | /* Success, but no bytes, means end-of-file. */ | |
9464 | break; | |
9465 | if (bytes == -1) | |
9466 | remote_hostio_error (remote_errno); | |
9467 | ||
9468 | offset += bytes; | |
9469 | ||
9470 | bytes = fwrite (buffer, 1, bytes, file); | |
9471 | if (bytes == 0) | |
9472 | perror_with_name (local_file); | |
9473 | } | |
9474 | ||
9475 | discard_cleanups (close_cleanup); | |
9476 | if (remote_hostio_close (fd, &remote_errno)) | |
9477 | remote_hostio_error (remote_errno); | |
9478 | ||
9479 | if (from_tty) | |
9480 | printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file); | |
9481 | do_cleanups (back_to); | |
9482 | } | |
9483 | ||
9484 | void | |
9485 | remote_file_delete (const char *remote_file, int from_tty) | |
9486 | { | |
9487 | int retcode, remote_errno; | |
9488 | ||
9489 | if (!remote_desc) | |
9490 | error (_("command can only be used with remote target")); | |
9491 | ||
9492 | retcode = remote_hostio_unlink (remote_file, &remote_errno); | |
9493 | if (retcode == -1) | |
9494 | remote_hostio_error (remote_errno); | |
9495 | ||
9496 | if (from_tty) | |
9497 | printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file); | |
9498 | } | |
9499 | ||
9500 | static void | |
9501 | remote_put_command (char *args, int from_tty) | |
9502 | { | |
9503 | struct cleanup *back_to; | |
9504 | char **argv; | |
9505 | ||
d1a41061 PP |
9506 | if (args == NULL) |
9507 | error_no_arg (_("file to put")); | |
9508 | ||
9509 | argv = gdb_buildargv (args); | |
a6b151f1 DJ |
9510 | back_to = make_cleanup_freeargv (argv); |
9511 | if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL) | |
9512 | error (_("Invalid parameters to remote put")); | |
9513 | ||
9514 | remote_file_put (argv[0], argv[1], from_tty); | |
9515 | ||
9516 | do_cleanups (back_to); | |
9517 | } | |
9518 | ||
9519 | static void | |
9520 | remote_get_command (char *args, int from_tty) | |
9521 | { | |
9522 | struct cleanup *back_to; | |
9523 | char **argv; | |
9524 | ||
d1a41061 PP |
9525 | if (args == NULL) |
9526 | error_no_arg (_("file to get")); | |
9527 | ||
9528 | argv = gdb_buildargv (args); | |
a6b151f1 DJ |
9529 | back_to = make_cleanup_freeargv (argv); |
9530 | if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL) | |
9531 | error (_("Invalid parameters to remote get")); | |
9532 | ||
9533 | remote_file_get (argv[0], argv[1], from_tty); | |
9534 | ||
9535 | do_cleanups (back_to); | |
9536 | } | |
9537 | ||
9538 | static void | |
9539 | remote_delete_command (char *args, int from_tty) | |
9540 | { | |
9541 | struct cleanup *back_to; | |
9542 | char **argv; | |
9543 | ||
d1a41061 PP |
9544 | if (args == NULL) |
9545 | error_no_arg (_("file to delete")); | |
9546 | ||
9547 | argv = gdb_buildargv (args); | |
a6b151f1 DJ |
9548 | back_to = make_cleanup_freeargv (argv); |
9549 | if (argv[0] == NULL || argv[1] != NULL) | |
9550 | error (_("Invalid parameters to remote delete")); | |
9551 | ||
9552 | remote_file_delete (argv[0], from_tty); | |
9553 | ||
9554 | do_cleanups (back_to); | |
9555 | } | |
9556 | ||
9557 | static void | |
9558 | remote_command (char *args, int from_tty) | |
9559 | { | |
9560 | help_list (remote_cmdlist, "remote ", -1, gdb_stdout); | |
9561 | } | |
9562 | ||
b2175913 MS |
9563 | static int |
9564 | remote_can_execute_reverse (void) | |
9565 | { | |
40ab02ce MS |
9566 | if (remote_protocol_packets[PACKET_bs].support == PACKET_ENABLE |
9567 | || remote_protocol_packets[PACKET_bc].support == PACKET_ENABLE) | |
9568 | return 1; | |
9569 | else | |
9570 | return 0; | |
b2175913 MS |
9571 | } |
9572 | ||
74531fed PA |
9573 | static int |
9574 | remote_supports_non_stop (void) | |
9575 | { | |
9576 | return 1; | |
9577 | } | |
9578 | ||
8a305172 PA |
9579 | static int |
9580 | remote_supports_multi_process (void) | |
9581 | { | |
9582 | struct remote_state *rs = get_remote_state (); | |
a744cf53 | 9583 | |
8a305172 PA |
9584 | return remote_multi_process_p (rs); |
9585 | } | |
9586 | ||
782b2b07 SS |
9587 | int |
9588 | remote_supports_cond_tracepoints (void) | |
9589 | { | |
9590 | struct remote_state *rs = get_remote_state (); | |
a744cf53 | 9591 | |
782b2b07 SS |
9592 | return rs->cond_tracepoints; |
9593 | } | |
9594 | ||
7a697b8d SS |
9595 | int |
9596 | remote_supports_fast_tracepoints (void) | |
9597 | { | |
9598 | struct remote_state *rs = get_remote_state (); | |
a744cf53 | 9599 | |
7a697b8d SS |
9600 | return rs->fast_tracepoints; |
9601 | } | |
9602 | ||
0fb4aa4b PA |
9603 | static int |
9604 | remote_supports_static_tracepoints (void) | |
9605 | { | |
9606 | struct remote_state *rs = get_remote_state (); | |
9607 | ||
9608 | return rs->static_tracepoints; | |
9609 | } | |
9610 | ||
35b1e5cc | 9611 | static void |
ad91cd99 | 9612 | remote_trace_init (void) |
35b1e5cc SS |
9613 | { |
9614 | putpkt ("QTinit"); | |
9615 | remote_get_noisy_reply (&target_buf, &target_buf_size); | |
ad91cd99 | 9616 | if (strcmp (target_buf, "OK") != 0) |
35b1e5cc SS |
9617 | error (_("Target does not support this command.")); |
9618 | } | |
9619 | ||
9620 | static void free_actions_list (char **actions_list); | |
9621 | static void free_actions_list_cleanup_wrapper (void *); | |
9622 | static void | |
9623 | free_actions_list_cleanup_wrapper (void *al) | |
9624 | { | |
9625 | free_actions_list (al); | |
9626 | } | |
9627 | ||
9628 | static void | |
9629 | free_actions_list (char **actions_list) | |
9630 | { | |
9631 | int ndx; | |
9632 | ||
9633 | if (actions_list == 0) | |
9634 | return; | |
9635 | ||
9636 | for (ndx = 0; actions_list[ndx]; ndx++) | |
9637 | xfree (actions_list[ndx]); | |
9638 | ||
9639 | xfree (actions_list); | |
9640 | } | |
9641 | ||
409873ef SS |
9642 | /* Recursive routine to walk through command list including loops, and |
9643 | download packets for each command. */ | |
9644 | ||
9645 | static void | |
9646 | remote_download_command_source (int num, ULONGEST addr, | |
9647 | struct command_line *cmds) | |
9648 | { | |
9649 | struct remote_state *rs = get_remote_state (); | |
9650 | struct command_line *cmd; | |
9651 | ||
9652 | for (cmd = cmds; cmd; cmd = cmd->next) | |
9653 | { | |
9654 | QUIT; /* allow user to bail out with ^C */ | |
9655 | strcpy (rs->buf, "QTDPsrc:"); | |
9656 | encode_source_string (num, addr, "cmd", cmd->line, | |
9657 | rs->buf + strlen (rs->buf), | |
9658 | rs->buf_size - strlen (rs->buf)); | |
9659 | putpkt (rs->buf); | |
9660 | remote_get_noisy_reply (&target_buf, &target_buf_size); | |
9661 | if (strcmp (target_buf, "OK")) | |
9662 | warning (_("Target does not support source download.")); | |
9663 | ||
9664 | if (cmd->control_type == while_control | |
9665 | || cmd->control_type == while_stepping_control) | |
9666 | { | |
9667 | remote_download_command_source (num, addr, *cmd->body_list); | |
9668 | ||
9669 | QUIT; /* allow user to bail out with ^C */ | |
9670 | strcpy (rs->buf, "QTDPsrc:"); | |
9671 | encode_source_string (num, addr, "cmd", "end", | |
9672 | rs->buf + strlen (rs->buf), | |
9673 | rs->buf_size - strlen (rs->buf)); | |
9674 | putpkt (rs->buf); | |
9675 | remote_get_noisy_reply (&target_buf, &target_buf_size); | |
9676 | if (strcmp (target_buf, "OK")) | |
9677 | warning (_("Target does not support source download.")); | |
9678 | } | |
9679 | } | |
9680 | } | |
9681 | ||
35b1e5cc SS |
9682 | static void |
9683 | remote_download_tracepoint (struct breakpoint *t) | |
9684 | { | |
9355b391 | 9685 | struct bp_location *loc; |
35b1e5cc | 9686 | CORE_ADDR tpaddr; |
409873ef | 9687 | char addrbuf[40]; |
35b1e5cc SS |
9688 | char buf[2048]; |
9689 | char **tdp_actions; | |
9690 | char **stepping_actions; | |
9691 | int ndx; | |
9692 | struct cleanup *old_chain = NULL; | |
9693 | struct agent_expr *aexpr; | |
9694 | struct cleanup *aexpr_chain = NULL; | |
9695 | char *pkt; | |
9696 | ||
9355b391 SS |
9697 | /* Iterate over all the tracepoint locations. It's up to the target to |
9698 | notice multiple tracepoint packets with the same number but different | |
9699 | addresses, and treat them as multiple locations. */ | |
9700 | for (loc = t->loc; loc; loc = loc->next) | |
9701 | { | |
9702 | encode_actions (t, loc, &tdp_actions, &stepping_actions); | |
9703 | old_chain = make_cleanup (free_actions_list_cleanup_wrapper, | |
9704 | tdp_actions); | |
9705 | (void) make_cleanup (free_actions_list_cleanup_wrapper, stepping_actions); | |
9706 | ||
9707 | tpaddr = loc->address; | |
409873ef | 9708 | sprintf_vma (addrbuf, tpaddr); |
9355b391 | 9709 | sprintf (buf, "QTDP:%x:%s:%c:%lx:%x", t->number, |
409873ef | 9710 | addrbuf, /* address */ |
9355b391 SS |
9711 | (t->enable_state == bp_enabled ? 'E' : 'D'), |
9712 | t->step_count, t->pass_count); | |
9713 | /* Fast tracepoints are mostly handled by the target, but we can | |
9714 | tell the target how big of an instruction block should be moved | |
9715 | around. */ | |
9716 | if (t->type == bp_fast_tracepoint) | |
35b1e5cc | 9717 | { |
9355b391 SS |
9718 | /* Only test for support at download time; we may not know |
9719 | target capabilities at definition time. */ | |
9720 | if (remote_supports_fast_tracepoints ()) | |
9721 | { | |
9722 | int isize; | |
35b1e5cc | 9723 | |
9355b391 SS |
9724 | if (gdbarch_fast_tracepoint_valid_at (target_gdbarch, |
9725 | tpaddr, &isize, NULL)) | |
9726 | sprintf (buf + strlen (buf), ":F%x", isize); | |
9727 | else | |
9728 | /* If it passed validation at definition but fails now, | |
9729 | something is very wrong. */ | |
9730 | internal_error (__FILE__, __LINE__, | |
9731 | "Fast tracepoint not valid during download"); | |
9732 | } | |
35b1e5cc | 9733 | else |
9355b391 SS |
9734 | /* Fast tracepoints are functionally identical to regular |
9735 | tracepoints, so don't take lack of support as a reason to | |
9736 | give up on the trace run. */ | |
9737 | warning (_("Target does not support fast tracepoints, downloading %d as regular tracepoint"), t->number); | |
35b1e5cc | 9738 | } |
0fb4aa4b PA |
9739 | else if (t->type == bp_static_tracepoint) |
9740 | { | |
9741 | /* Only test for support at download time; we may not know | |
9742 | target capabilities at definition time. */ | |
9743 | if (remote_supports_static_tracepoints ()) | |
9744 | { | |
9745 | struct static_tracepoint_marker marker; | |
9746 | ||
9747 | if (target_static_tracepoint_marker_at (tpaddr, &marker)) | |
9748 | strcat (buf, ":S"); | |
9749 | else | |
74232302 | 9750 | error (_("Static tracepoint not valid during download")); |
0fb4aa4b PA |
9751 | } |
9752 | else | |
9753 | /* Fast tracepoints are functionally identical to regular | |
9754 | tracepoints, so don't take lack of support as a reason | |
9755 | to give up on the trace run. */ | |
9756 | error (_("Target does not support static tracepoints")); | |
9757 | } | |
9355b391 SS |
9758 | /* If the tracepoint has a conditional, make it into an agent |
9759 | expression and append to the definition. */ | |
9760 | if (loc->cond) | |
35b1e5cc | 9761 | { |
9355b391 SS |
9762 | /* Only test support at download time, we may not know target |
9763 | capabilities at definition time. */ | |
9764 | if (remote_supports_cond_tracepoints ()) | |
9765 | { | |
9766 | aexpr = gen_eval_for_expr (tpaddr, loc->cond); | |
9767 | aexpr_chain = make_cleanup_free_agent_expr (aexpr); | |
9768 | sprintf (buf + strlen (buf), ":X%x,", aexpr->len); | |
9769 | pkt = buf + strlen (buf); | |
9770 | for (ndx = 0; ndx < aexpr->len; ++ndx) | |
9771 | pkt = pack_hex_byte (pkt, aexpr->buf[ndx]); | |
9772 | *pkt = '\0'; | |
9773 | do_cleanups (aexpr_chain); | |
9774 | } | |
9775 | else | |
9776 | warning (_("Target does not support conditional tracepoints, ignoring tp %d cond"), t->number); | |
35b1e5cc | 9777 | } |
35b1e5cc | 9778 | |
a7bdde9e | 9779 | if (t->commands || *default_collect) |
9355b391 SS |
9780 | strcat (buf, "-"); |
9781 | putpkt (buf); | |
9782 | remote_get_noisy_reply (&target_buf, &target_buf_size); | |
9783 | if (strcmp (target_buf, "OK")) | |
9784 | error (_("Target does not support tracepoints.")); | |
35b1e5cc | 9785 | |
9355b391 SS |
9786 | /* do_single_steps (t); */ |
9787 | if (tdp_actions) | |
35b1e5cc | 9788 | { |
9355b391 SS |
9789 | for (ndx = 0; tdp_actions[ndx]; ndx++) |
9790 | { | |
9791 | QUIT; /* allow user to bail out with ^C */ | |
9792 | sprintf (buf, "QTDP:-%x:%s:%s%c", | |
409873ef | 9793 | t->number, addrbuf, /* address */ |
9355b391 SS |
9794 | tdp_actions[ndx], |
9795 | ((tdp_actions[ndx + 1] || stepping_actions) | |
9796 | ? '-' : 0)); | |
9797 | putpkt (buf); | |
9798 | remote_get_noisy_reply (&target_buf, | |
9799 | &target_buf_size); | |
9800 | if (strcmp (target_buf, "OK")) | |
9801 | error (_("Error on target while setting tracepoints.")); | |
9802 | } | |
35b1e5cc | 9803 | } |
9355b391 | 9804 | if (stepping_actions) |
35b1e5cc | 9805 | { |
9355b391 SS |
9806 | for (ndx = 0; stepping_actions[ndx]; ndx++) |
9807 | { | |
9808 | QUIT; /* allow user to bail out with ^C */ | |
9809 | sprintf (buf, "QTDP:-%x:%s:%s%s%s", | |
409873ef | 9810 | t->number, addrbuf, /* address */ |
9355b391 SS |
9811 | ((ndx == 0) ? "S" : ""), |
9812 | stepping_actions[ndx], | |
9813 | (stepping_actions[ndx + 1] ? "-" : "")); | |
9814 | putpkt (buf); | |
9815 | remote_get_noisy_reply (&target_buf, | |
9816 | &target_buf_size); | |
9817 | if (strcmp (target_buf, "OK")) | |
9818 | error (_("Error on target while setting tracepoints.")); | |
9819 | } | |
35b1e5cc | 9820 | } |
409873ef SS |
9821 | |
9822 | if (remote_protocol_packets[PACKET_TracepointSource].support == PACKET_ENABLE) | |
9823 | { | |
9824 | if (t->addr_string) | |
9825 | { | |
9826 | strcpy (buf, "QTDPsrc:"); | |
9827 | encode_source_string (t->number, loc->address, | |
9828 | "at", t->addr_string, buf + strlen (buf), | |
9829 | 2048 - strlen (buf)); | |
9830 | ||
9831 | putpkt (buf); | |
9832 | remote_get_noisy_reply (&target_buf, &target_buf_size); | |
9833 | if (strcmp (target_buf, "OK")) | |
9834 | warning (_("Target does not support source download.")); | |
9835 | } | |
9836 | if (t->cond_string) | |
9837 | { | |
9838 | strcpy (buf, "QTDPsrc:"); | |
9839 | encode_source_string (t->number, loc->address, | |
9840 | "cond", t->cond_string, buf + strlen (buf), | |
9841 | 2048 - strlen (buf)); | |
9842 | putpkt (buf); | |
9843 | remote_get_noisy_reply (&target_buf, &target_buf_size); | |
9844 | if (strcmp (target_buf, "OK")) | |
9845 | warning (_("Target does not support source download.")); | |
9846 | } | |
9847 | remote_download_command_source (t->number, loc->address, | |
5cea2a26 | 9848 | breakpoint_commands (t)); |
409873ef SS |
9849 | } |
9850 | ||
9355b391 | 9851 | do_cleanups (old_chain); |
35b1e5cc | 9852 | } |
35b1e5cc SS |
9853 | } |
9854 | ||
9855 | static void | |
9856 | remote_download_trace_state_variable (struct trace_state_variable *tsv) | |
9857 | { | |
9858 | struct remote_state *rs = get_remote_state (); | |
00bf0b85 | 9859 | char *p; |
35b1e5cc | 9860 | |
00bf0b85 SS |
9861 | sprintf (rs->buf, "QTDV:%x:%s:%x:", |
9862 | tsv->number, phex ((ULONGEST) tsv->initial_value, 8), tsv->builtin); | |
9863 | p = rs->buf + strlen (rs->buf); | |
9864 | if ((p - rs->buf) + strlen (tsv->name) * 2 >= get_remote_packet_size ()) | |
9865 | error (_("Trace state variable name too long for tsv definition packet")); | |
9866 | p += 2 * bin2hex ((gdb_byte *) (tsv->name), p, 0); | |
9867 | *p++ = '\0'; | |
35b1e5cc SS |
9868 | putpkt (rs->buf); |
9869 | remote_get_noisy_reply (&target_buf, &target_buf_size); | |
ad91cd99 PA |
9870 | if (*target_buf == '\0') |
9871 | error (_("Target does not support this command.")); | |
9872 | if (strcmp (target_buf, "OK") != 0) | |
9873 | error (_("Error on target while downloading trace state variable.")); | |
35b1e5cc SS |
9874 | } |
9875 | ||
9876 | static void | |
ad91cd99 | 9877 | remote_trace_set_readonly_regions (void) |
35b1e5cc SS |
9878 | { |
9879 | asection *s; | |
9880 | bfd_size_type size; | |
9881 | bfd_vma lma; | |
9882 | int anysecs = 0; | |
9883 | ||
9884 | if (!exec_bfd) | |
9885 | return; /* No information to give. */ | |
9886 | ||
9887 | strcpy (target_buf, "QTro"); | |
9888 | for (s = exec_bfd->sections; s; s = s->next) | |
9889 | { | |
9890 | char tmp1[40], tmp2[40]; | |
9891 | ||
9892 | if ((s->flags & SEC_LOAD) == 0 || | |
9893 | /* (s->flags & SEC_CODE) == 0 || */ | |
9894 | (s->flags & SEC_READONLY) == 0) | |
9895 | continue; | |
9896 | ||
9897 | anysecs = 1; | |
9898 | lma = s->lma; | |
9899 | size = bfd_get_section_size (s); | |
9900 | sprintf_vma (tmp1, lma); | |
9901 | sprintf_vma (tmp2, lma + size); | |
9902 | sprintf (target_buf + strlen (target_buf), | |
9903 | ":%s,%s", tmp1, tmp2); | |
9904 | } | |
9905 | if (anysecs) | |
9906 | { | |
9907 | putpkt (target_buf); | |
9908 | getpkt (&target_buf, &target_buf_size, 0); | |
9909 | } | |
9910 | } | |
9911 | ||
9912 | static void | |
ad91cd99 | 9913 | remote_trace_start (void) |
35b1e5cc SS |
9914 | { |
9915 | putpkt ("QTStart"); | |
9916 | remote_get_noisy_reply (&target_buf, &target_buf_size); | |
ad91cd99 PA |
9917 | if (*target_buf == '\0') |
9918 | error (_("Target does not support this command.")); | |
9919 | if (strcmp (target_buf, "OK") != 0) | |
35b1e5cc SS |
9920 | error (_("Bogus reply from target: %s"), target_buf); |
9921 | } | |
9922 | ||
9923 | static int | |
00bf0b85 | 9924 | remote_get_trace_status (struct trace_status *ts) |
35b1e5cc | 9925 | { |
aa369b55 | 9926 | char *p; |
00bf0b85 SS |
9927 | /* FIXME we need to get register block size some other way */ |
9928 | extern int trace_regblock_size; | |
a744cf53 | 9929 | |
00bf0b85 SS |
9930 | trace_regblock_size = get_remote_arch_state ()->sizeof_g_packet; |
9931 | ||
35b1e5cc | 9932 | putpkt ("qTStatus"); |
ad91cd99 | 9933 | p = remote_get_noisy_reply (&target_buf, &target_buf_size); |
00bf0b85 SS |
9934 | |
9935 | /* If the remote target doesn't do tracing, flag it. */ | |
9936 | if (*p == '\0') | |
9937 | return -1; | |
35b1e5cc | 9938 | |
00bf0b85 SS |
9939 | /* We're working with a live target. */ |
9940 | ts->from_file = 0; | |
9941 | ||
9942 | /* Set some defaults. */ | |
9943 | ts->running_known = 0; | |
9944 | ts->stop_reason = trace_stop_reason_unknown; | |
9945 | ts->traceframe_count = -1; | |
9946 | ts->buffer_free = 0; | |
9947 | ||
9948 | if (*p++ != 'T') | |
35b1e5cc SS |
9949 | error (_("Bogus trace status reply from target: %s"), target_buf); |
9950 | ||
00bf0b85 SS |
9951 | parse_trace_status (p, ts); |
9952 | ||
9953 | return ts->running; | |
35b1e5cc SS |
9954 | } |
9955 | ||
9956 | static void | |
ad91cd99 | 9957 | remote_trace_stop (void) |
35b1e5cc SS |
9958 | { |
9959 | putpkt ("QTStop"); | |
9960 | remote_get_noisy_reply (&target_buf, &target_buf_size); | |
ad91cd99 PA |
9961 | if (*target_buf == '\0') |
9962 | error (_("Target does not support this command.")); | |
9963 | if (strcmp (target_buf, "OK") != 0) | |
35b1e5cc SS |
9964 | error (_("Bogus reply from target: %s"), target_buf); |
9965 | } | |
9966 | ||
9967 | static int | |
9968 | remote_trace_find (enum trace_find_type type, int num, | |
9969 | ULONGEST addr1, ULONGEST addr2, | |
9970 | int *tpp) | |
9971 | { | |
9972 | struct remote_state *rs = get_remote_state (); | |
9973 | char *p, *reply; | |
9974 | int target_frameno = -1, target_tracept = -1; | |
9975 | ||
9976 | p = rs->buf; | |
9977 | strcpy (p, "QTFrame:"); | |
9978 | p = strchr (p, '\0'); | |
9979 | switch (type) | |
9980 | { | |
9981 | case tfind_number: | |
9982 | sprintf (p, "%x", num); | |
9983 | break; | |
9984 | case tfind_pc: | |
00bf0b85 | 9985 | sprintf (p, "pc:%s", phex_nz (addr1, 0)); |
35b1e5cc SS |
9986 | break; |
9987 | case tfind_tp: | |
9988 | sprintf (p, "tdp:%x", num); | |
9989 | break; | |
9990 | case tfind_range: | |
00bf0b85 | 9991 | sprintf (p, "range:%s:%s", phex_nz (addr1, 0), phex_nz (addr2, 0)); |
35b1e5cc SS |
9992 | break; |
9993 | case tfind_outside: | |
00bf0b85 | 9994 | sprintf (p, "outside:%s:%s", phex_nz (addr1, 0), phex_nz (addr2, 0)); |
35b1e5cc SS |
9995 | break; |
9996 | default: | |
9997 | error ("Unknown trace find type %d", type); | |
9998 | } | |
9999 | ||
10000 | putpkt (rs->buf); | |
10001 | reply = remote_get_noisy_reply (&(rs->buf), &sizeof_pkt); | |
ad91cd99 PA |
10002 | if (*reply == '\0') |
10003 | error (_("Target does not support this command.")); | |
35b1e5cc SS |
10004 | |
10005 | while (reply && *reply) | |
10006 | switch (*reply) | |
10007 | { | |
10008 | case 'F': | |
f197e0f1 VP |
10009 | p = ++reply; |
10010 | target_frameno = (int) strtol (p, &reply, 16); | |
10011 | if (reply == p) | |
10012 | error (_("Unable to parse trace frame number")); | |
10013 | if (target_frameno == -1) | |
10014 | return -1; | |
35b1e5cc SS |
10015 | break; |
10016 | case 'T': | |
f197e0f1 VP |
10017 | p = ++reply; |
10018 | target_tracept = (int) strtol (p, &reply, 16); | |
10019 | if (reply == p) | |
10020 | error (_("Unable to parse tracepoint number")); | |
35b1e5cc SS |
10021 | break; |
10022 | case 'O': /* "OK"? */ | |
10023 | if (reply[1] == 'K' && reply[2] == '\0') | |
10024 | reply += 2; | |
10025 | else | |
10026 | error (_("Bogus reply from target: %s"), reply); | |
10027 | break; | |
10028 | default: | |
10029 | error (_("Bogus reply from target: %s"), reply); | |
10030 | } | |
10031 | if (tpp) | |
10032 | *tpp = target_tracept; | |
10033 | return target_frameno; | |
10034 | } | |
10035 | ||
10036 | static int | |
10037 | remote_get_trace_state_variable_value (int tsvnum, LONGEST *val) | |
10038 | { | |
10039 | struct remote_state *rs = get_remote_state (); | |
10040 | char *reply; | |
10041 | ULONGEST uval; | |
10042 | ||
10043 | sprintf (rs->buf, "qTV:%x", tsvnum); | |
10044 | putpkt (rs->buf); | |
10045 | reply = remote_get_noisy_reply (&target_buf, &target_buf_size); | |
10046 | if (reply && *reply) | |
10047 | { | |
10048 | if (*reply == 'V') | |
10049 | { | |
10050 | unpack_varlen_hex (reply + 1, &uval); | |
10051 | *val = (LONGEST) uval; | |
10052 | return 1; | |
10053 | } | |
10054 | } | |
10055 | return 0; | |
10056 | } | |
10057 | ||
00bf0b85 | 10058 | static int |
011aacb0 | 10059 | remote_save_trace_data (const char *filename) |
00bf0b85 SS |
10060 | { |
10061 | struct remote_state *rs = get_remote_state (); | |
10062 | char *p, *reply; | |
10063 | ||
10064 | p = rs->buf; | |
10065 | strcpy (p, "QTSave:"); | |
10066 | p += strlen (p); | |
10067 | if ((p - rs->buf) + strlen (filename) * 2 >= get_remote_packet_size ()) | |
10068 | error (_("Remote file name too long for trace save packet")); | |
10069 | p += 2 * bin2hex ((gdb_byte *) filename, p, 0); | |
10070 | *p++ = '\0'; | |
10071 | putpkt (rs->buf); | |
ad91cd99 PA |
10072 | reply = remote_get_noisy_reply (&target_buf, &target_buf_size); |
10073 | if (*reply != '\0') | |
10074 | error (_("Target does not support this command.")); | |
10075 | if (strcmp (reply, "OK") != 0) | |
10076 | error (_("Bogus reply from target: %s"), reply); | |
00bf0b85 SS |
10077 | return 0; |
10078 | } | |
10079 | ||
10080 | /* This is basically a memory transfer, but needs to be its own packet | |
10081 | because we don't know how the target actually organizes its trace | |
10082 | memory, plus we want to be able to ask for as much as possible, but | |
10083 | not be unhappy if we don't get as much as we ask for. */ | |
10084 | ||
10085 | static LONGEST | |
10086 | remote_get_raw_trace_data (gdb_byte *buf, ULONGEST offset, LONGEST len) | |
10087 | { | |
10088 | struct remote_state *rs = get_remote_state (); | |
10089 | char *reply; | |
10090 | char *p; | |
10091 | int rslt; | |
10092 | ||
10093 | p = rs->buf; | |
10094 | strcpy (p, "qTBuffer:"); | |
10095 | p += strlen (p); | |
10096 | p += hexnumstr (p, offset); | |
10097 | *p++ = ','; | |
10098 | p += hexnumstr (p, len); | |
10099 | *p++ = '\0'; | |
10100 | ||
10101 | putpkt (rs->buf); | |
10102 | reply = remote_get_noisy_reply (&target_buf, &target_buf_size); | |
10103 | if (reply && *reply) | |
10104 | { | |
10105 | /* 'l' by itself means we're at the end of the buffer and | |
10106 | there is nothing more to get. */ | |
10107 | if (*reply == 'l') | |
10108 | return 0; | |
10109 | ||
10110 | /* Convert the reply into binary. Limit the number of bytes to | |
10111 | convert according to our passed-in buffer size, rather than | |
10112 | what was returned in the packet; if the target is | |
10113 | unexpectedly generous and gives us a bigger reply than we | |
10114 | asked for, we don't want to crash. */ | |
10115 | rslt = hex2bin (target_buf, buf, len); | |
10116 | return rslt; | |
10117 | } | |
10118 | ||
10119 | /* Something went wrong, flag as an error. */ | |
10120 | return -1; | |
10121 | } | |
10122 | ||
35b1e5cc SS |
10123 | static void |
10124 | remote_set_disconnected_tracing (int val) | |
10125 | { | |
10126 | struct remote_state *rs = get_remote_state (); | |
10127 | ||
33da3f1c SS |
10128 | if (rs->disconnected_tracing) |
10129 | { | |
ad91cd99 PA |
10130 | char *reply; |
10131 | ||
33da3f1c SS |
10132 | sprintf (rs->buf, "QTDisconnected:%x", val); |
10133 | putpkt (rs->buf); | |
ad91cd99 PA |
10134 | reply = remote_get_noisy_reply (&target_buf, &target_buf_size); |
10135 | if (*reply == '\0') | |
33da3f1c | 10136 | error (_("Target does not support this command.")); |
ad91cd99 PA |
10137 | if (strcmp (reply, "OK") != 0) |
10138 | error (_("Bogus reply from target: %s"), reply); | |
33da3f1c SS |
10139 | } |
10140 | else if (val) | |
10141 | warning (_("Target does not support disconnected tracing.")); | |
35b1e5cc SS |
10142 | } |
10143 | ||
dc146f7c VP |
10144 | static int |
10145 | remote_core_of_thread (struct target_ops *ops, ptid_t ptid) | |
10146 | { | |
10147 | struct thread_info *info = find_thread_ptid (ptid); | |
a744cf53 | 10148 | |
dc146f7c VP |
10149 | if (info && info->private) |
10150 | return info->private->core; | |
10151 | return -1; | |
10152 | } | |
10153 | ||
4daf5ac0 SS |
10154 | static void |
10155 | remote_set_circular_trace_buffer (int val) | |
10156 | { | |
10157 | struct remote_state *rs = get_remote_state (); | |
ad91cd99 | 10158 | char *reply; |
4daf5ac0 SS |
10159 | |
10160 | sprintf (rs->buf, "QTBuffer:circular:%x", val); | |
10161 | putpkt (rs->buf); | |
ad91cd99 PA |
10162 | reply = remote_get_noisy_reply (&target_buf, &target_buf_size); |
10163 | if (*reply == '\0') | |
4daf5ac0 | 10164 | error (_("Target does not support this command.")); |
ad91cd99 PA |
10165 | if (strcmp (reply, "OK") != 0) |
10166 | error (_("Bogus reply from target: %s"), reply); | |
4daf5ac0 SS |
10167 | } |
10168 | ||
c906108c | 10169 | static void |
fba45db2 | 10170 | init_remote_ops (void) |
c906108c | 10171 | { |
c5aa993b | 10172 | remote_ops.to_shortname = "remote"; |
c906108c | 10173 | remote_ops.to_longname = "Remote serial target in gdb-specific protocol"; |
c5aa993b | 10174 | remote_ops.to_doc = |
c906108c | 10175 | "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ |
0d06e24b JM |
10176 | Specify the serial device it is connected to\n\ |
10177 | (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."; | |
c5aa993b JM |
10178 | remote_ops.to_open = remote_open; |
10179 | remote_ops.to_close = remote_close; | |
c906108c | 10180 | remote_ops.to_detach = remote_detach; |
6ad8ae5c | 10181 | remote_ops.to_disconnect = remote_disconnect; |
c5aa993b | 10182 | remote_ops.to_resume = remote_resume; |
c906108c SS |
10183 | remote_ops.to_wait = remote_wait; |
10184 | remote_ops.to_fetch_registers = remote_fetch_registers; | |
10185 | remote_ops.to_store_registers = remote_store_registers; | |
10186 | remote_ops.to_prepare_to_store = remote_prepare_to_store; | |
c8e73a31 | 10187 | remote_ops.deprecated_xfer_memory = remote_xfer_memory; |
c5aa993b | 10188 | remote_ops.to_files_info = remote_files_info; |
c906108c SS |
10189 | remote_ops.to_insert_breakpoint = remote_insert_breakpoint; |
10190 | remote_ops.to_remove_breakpoint = remote_remove_breakpoint; | |
3c3bea1c GS |
10191 | remote_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint; |
10192 | remote_ops.to_stopped_data_address = remote_stopped_data_address; | |
10193 | remote_ops.to_can_use_hw_breakpoint = remote_check_watch_resources; | |
10194 | remote_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint; | |
10195 | remote_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint; | |
10196 | remote_ops.to_insert_watchpoint = remote_insert_watchpoint; | |
10197 | remote_ops.to_remove_watchpoint = remote_remove_watchpoint; | |
c5aa993b JM |
10198 | remote_ops.to_kill = remote_kill; |
10199 | remote_ops.to_load = generic_load; | |
c906108c | 10200 | remote_ops.to_mourn_inferior = remote_mourn; |
f0223081 | 10201 | remote_ops.to_notice_signals = remote_notice_signals; |
c906108c | 10202 | remote_ops.to_thread_alive = remote_thread_alive; |
0f71a2f6 | 10203 | remote_ops.to_find_new_threads = remote_threads_info; |
0caabb7e | 10204 | remote_ops.to_pid_to_str = remote_pid_to_str; |
cf759d3b | 10205 | remote_ops.to_extra_thread_info = remote_threads_extra_info; |
10760264 | 10206 | remote_ops.to_get_ada_task_ptid = remote_get_ada_task_ptid; |
c906108c | 10207 | remote_ops.to_stop = remote_stop; |
4b8a223f | 10208 | remote_ops.to_xfer_partial = remote_xfer_partial; |
96baa820 | 10209 | remote_ops.to_rcmd = remote_rcmd; |
49d03eab | 10210 | remote_ops.to_log_command = serial_log_command; |
38691318 | 10211 | remote_ops.to_get_thread_local_address = remote_get_thread_local_address; |
c906108c | 10212 | remote_ops.to_stratum = process_stratum; |
c35b1492 PA |
10213 | remote_ops.to_has_all_memory = default_child_has_all_memory; |
10214 | remote_ops.to_has_memory = default_child_has_memory; | |
10215 | remote_ops.to_has_stack = default_child_has_stack; | |
10216 | remote_ops.to_has_registers = default_child_has_registers; | |
10217 | remote_ops.to_has_execution = default_child_has_execution; | |
c5aa993b | 10218 | remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */ |
b2175913 | 10219 | remote_ops.to_can_execute_reverse = remote_can_execute_reverse; |
c5aa993b | 10220 | remote_ops.to_magic = OPS_MAGIC; |
fd79ecee | 10221 | remote_ops.to_memory_map = remote_memory_map; |
a76d924d DJ |
10222 | remote_ops.to_flash_erase = remote_flash_erase; |
10223 | remote_ops.to_flash_done = remote_flash_done; | |
29709017 | 10224 | remote_ops.to_read_description = remote_read_description; |
08388c79 | 10225 | remote_ops.to_search_memory = remote_search_memory; |
75c99385 PA |
10226 | remote_ops.to_can_async_p = remote_can_async_p; |
10227 | remote_ops.to_is_async_p = remote_is_async_p; | |
10228 | remote_ops.to_async = remote_async; | |
10229 | remote_ops.to_async_mask = remote_async_mask; | |
10230 | remote_ops.to_terminal_inferior = remote_terminal_inferior; | |
10231 | remote_ops.to_terminal_ours = remote_terminal_ours; | |
74531fed | 10232 | remote_ops.to_supports_non_stop = remote_supports_non_stop; |
8a305172 | 10233 | remote_ops.to_supports_multi_process = remote_supports_multi_process; |
35b1e5cc SS |
10234 | remote_ops.to_trace_init = remote_trace_init; |
10235 | remote_ops.to_download_tracepoint = remote_download_tracepoint; | |
10236 | remote_ops.to_download_trace_state_variable = remote_download_trace_state_variable; | |
10237 | remote_ops.to_trace_set_readonly_regions = remote_trace_set_readonly_regions; | |
10238 | remote_ops.to_trace_start = remote_trace_start; | |
10239 | remote_ops.to_get_trace_status = remote_get_trace_status; | |
10240 | remote_ops.to_trace_stop = remote_trace_stop; | |
10241 | remote_ops.to_trace_find = remote_trace_find; | |
10242 | remote_ops.to_get_trace_state_variable_value = remote_get_trace_state_variable_value; | |
00bf0b85 SS |
10243 | remote_ops.to_save_trace_data = remote_save_trace_data; |
10244 | remote_ops.to_upload_tracepoints = remote_upload_tracepoints; | |
10245 | remote_ops.to_upload_trace_state_variables = remote_upload_trace_state_variables; | |
10246 | remote_ops.to_get_raw_trace_data = remote_get_raw_trace_data; | |
35b1e5cc | 10247 | remote_ops.to_set_disconnected_tracing = remote_set_disconnected_tracing; |
4daf5ac0 | 10248 | remote_ops.to_set_circular_trace_buffer = remote_set_circular_trace_buffer; |
dc146f7c | 10249 | remote_ops.to_core_of_thread = remote_core_of_thread; |
4a5e7a5b | 10250 | remote_ops.to_verify_memory = remote_verify_memory; |
711e434b | 10251 | remote_ops.to_get_tib_address = remote_get_tib_address; |
d914c394 | 10252 | remote_ops.to_set_permissions = remote_set_permissions; |
0fb4aa4b PA |
10253 | remote_ops.to_static_tracepoint_marker_at |
10254 | = remote_static_tracepoint_marker_at; | |
10255 | remote_ops.to_static_tracepoint_markers_by_strid | |
10256 | = remote_static_tracepoint_markers_by_strid; | |
c906108c SS |
10257 | } |
10258 | ||
10259 | /* Set up the extended remote vector by making a copy of the standard | |
10260 | remote vector and adding to it. */ | |
10261 | ||
10262 | static void | |
fba45db2 | 10263 | init_extended_remote_ops (void) |
c906108c SS |
10264 | { |
10265 | extended_remote_ops = remote_ops; | |
10266 | ||
0f71a2f6 | 10267 | extended_remote_ops.to_shortname = "extended-remote"; |
c5aa993b | 10268 | extended_remote_ops.to_longname = |
c906108c | 10269 | "Extended remote serial target in gdb-specific protocol"; |
c5aa993b | 10270 | extended_remote_ops.to_doc = |
c906108c | 10271 | "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ |
39237dd1 PA |
10272 | Specify the serial device it is connected to (e.g. /dev/ttya)."; |
10273 | extended_remote_ops.to_open = extended_remote_open; | |
c906108c SS |
10274 | extended_remote_ops.to_create_inferior = extended_remote_create_inferior; |
10275 | extended_remote_ops.to_mourn_inferior = extended_remote_mourn; | |
2d717e4f DJ |
10276 | extended_remote_ops.to_detach = extended_remote_detach; |
10277 | extended_remote_ops.to_attach = extended_remote_attach; | |
82f73884 | 10278 | extended_remote_ops.to_kill = extended_remote_kill; |
0f71a2f6 JM |
10279 | } |
10280 | ||
6426a772 JM |
10281 | static int |
10282 | remote_can_async_p (void) | |
10283 | { | |
c6ebd6cf | 10284 | if (!target_async_permitted) |
75c99385 PA |
10285 | /* We only enable async when the user specifically asks for it. */ |
10286 | return 0; | |
10287 | ||
23860348 | 10288 | /* We're async whenever the serial device is. */ |
b84876c2 | 10289 | return remote_async_mask_value && serial_can_async_p (remote_desc); |
6426a772 JM |
10290 | } |
10291 | ||
10292 | static int | |
10293 | remote_is_async_p (void) | |
10294 | { | |
c6ebd6cf | 10295 | if (!target_async_permitted) |
75c99385 PA |
10296 | /* We only enable async when the user specifically asks for it. */ |
10297 | return 0; | |
10298 | ||
23860348 | 10299 | /* We're async whenever the serial device is. */ |
b84876c2 | 10300 | return remote_async_mask_value && serial_is_async_p (remote_desc); |
6426a772 JM |
10301 | } |
10302 | ||
2acceee2 JM |
10303 | /* Pass the SERIAL event on and up to the client. One day this code |
10304 | will be able to delay notifying the client of an event until the | |
23860348 | 10305 | point where an entire packet has been received. */ |
2acceee2 | 10306 | |
2bc416ba | 10307 | static void (*async_client_callback) (enum inferior_event_type event_type, |
23860348 | 10308 | void *context); |
2acceee2 JM |
10309 | static void *async_client_context; |
10310 | static serial_event_ftype remote_async_serial_handler; | |
10311 | ||
6426a772 | 10312 | static void |
819cc324 | 10313 | remote_async_serial_handler (struct serial *scb, void *context) |
6426a772 | 10314 | { |
2acceee2 JM |
10315 | /* Don't propogate error information up to the client. Instead let |
10316 | the client find out about the error by querying the target. */ | |
10317 | async_client_callback (INF_REG_EVENT, async_client_context); | |
10318 | } | |
10319 | ||
74531fed PA |
10320 | static void |
10321 | remote_async_inferior_event_handler (gdb_client_data data) | |
10322 | { | |
10323 | inferior_event_handler (INF_REG_EVENT, NULL); | |
10324 | } | |
10325 | ||
10326 | static void | |
10327 | remote_async_get_pending_events_handler (gdb_client_data data) | |
10328 | { | |
10329 | remote_get_pending_stop_replies (); | |
10330 | } | |
10331 | ||
2acceee2 | 10332 | static void |
2bc416ba | 10333 | remote_async (void (*callback) (enum inferior_event_type event_type, |
23860348 | 10334 | void *context), void *context) |
2acceee2 | 10335 | { |
b84876c2 | 10336 | if (remote_async_mask_value == 0) |
8e65ff28 | 10337 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 10338 | _("Calling remote_async when async is masked")); |
ed9a39eb | 10339 | |
2acceee2 JM |
10340 | if (callback != NULL) |
10341 | { | |
2cd58942 | 10342 | serial_async (remote_desc, remote_async_serial_handler, NULL); |
2acceee2 JM |
10343 | async_client_callback = callback; |
10344 | async_client_context = context; | |
10345 | } | |
10346 | else | |
2cd58942 | 10347 | serial_async (remote_desc, NULL, NULL); |
6426a772 JM |
10348 | } |
10349 | ||
b84876c2 PA |
10350 | static int |
10351 | remote_async_mask (int new_mask) | |
10352 | { | |
10353 | int curr_mask = remote_async_mask_value; | |
a744cf53 | 10354 | |
b84876c2 PA |
10355 | remote_async_mask_value = new_mask; |
10356 | return curr_mask; | |
10357 | } | |
10358 | ||
5a2468f5 | 10359 | static void |
c2d11a7d | 10360 | set_remote_cmd (char *args, int from_tty) |
5a2468f5 | 10361 | { |
427c3a89 | 10362 | help_list (remote_set_cmdlist, "set remote ", -1, gdb_stdout); |
5a2468f5 JM |
10363 | } |
10364 | ||
d471ea57 AC |
10365 | static void |
10366 | show_remote_cmd (char *args, int from_tty) | |
10367 | { | |
37a105a1 | 10368 | /* We can't just use cmd_show_list here, because we want to skip |
427c3a89 | 10369 | the redundant "show remote Z-packet" and the legacy aliases. */ |
37a105a1 DJ |
10370 | struct cleanup *showlist_chain; |
10371 | struct cmd_list_element *list = remote_show_cmdlist; | |
10372 | ||
10373 | showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist"); | |
10374 | for (; list != NULL; list = list->next) | |
10375 | if (strcmp (list->name, "Z-packet") == 0) | |
10376 | continue; | |
427c3a89 DJ |
10377 | else if (list->type == not_set_cmd) |
10378 | /* Alias commands are exactly like the original, except they | |
10379 | don't have the normal type. */ | |
10380 | continue; | |
10381 | else | |
37a105a1 DJ |
10382 | { |
10383 | struct cleanup *option_chain | |
10384 | = make_cleanup_ui_out_tuple_begin_end (uiout, "option"); | |
a744cf53 | 10385 | |
37a105a1 DJ |
10386 | ui_out_field_string (uiout, "name", list->name); |
10387 | ui_out_text (uiout, ": "); | |
427c3a89 DJ |
10388 | if (list->type == show_cmd) |
10389 | do_setshow_command ((char *) NULL, from_tty, list); | |
10390 | else | |
10391 | cmd_func (list, NULL, from_tty); | |
37a105a1 DJ |
10392 | /* Close the tuple. */ |
10393 | do_cleanups (option_chain); | |
10394 | } | |
427c3a89 DJ |
10395 | |
10396 | /* Close the tuple. */ | |
10397 | do_cleanups (showlist_chain); | |
d471ea57 | 10398 | } |
5a2468f5 | 10399 | |
0f71a2f6 | 10400 | |
23860348 | 10401 | /* Function to be called whenever a new objfile (shlib) is detected. */ |
dc8acb97 MS |
10402 | static void |
10403 | remote_new_objfile (struct objfile *objfile) | |
10404 | { | |
23860348 | 10405 | if (remote_desc != 0) /* Have a remote connection. */ |
06d3b283 | 10406 | remote_check_symbols (objfile); |
dc8acb97 MS |
10407 | } |
10408 | ||
00bf0b85 SS |
10409 | /* Pull all the tracepoints defined on the target and create local |
10410 | data structures representing them. We don't want to create real | |
10411 | tracepoints yet, we don't want to mess up the user's existing | |
10412 | collection. */ | |
10413 | ||
10414 | static int | |
10415 | remote_upload_tracepoints (struct uploaded_tp **utpp) | |
d5551862 | 10416 | { |
00bf0b85 SS |
10417 | struct remote_state *rs = get_remote_state (); |
10418 | char *p; | |
d5551862 | 10419 | |
00bf0b85 SS |
10420 | /* Ask for a first packet of tracepoint definition. */ |
10421 | putpkt ("qTfP"); | |
10422 | getpkt (&rs->buf, &rs->buf_size, 0); | |
10423 | p = rs->buf; | |
10424 | while (*p && *p != 'l') | |
d5551862 | 10425 | { |
00bf0b85 SS |
10426 | parse_tracepoint_definition (p, utpp); |
10427 | /* Ask for another packet of tracepoint definition. */ | |
10428 | putpkt ("qTsP"); | |
10429 | getpkt (&rs->buf, &rs->buf_size, 0); | |
10430 | p = rs->buf; | |
d5551862 | 10431 | } |
00bf0b85 | 10432 | return 0; |
d5551862 SS |
10433 | } |
10434 | ||
00bf0b85 SS |
10435 | static int |
10436 | remote_upload_trace_state_variables (struct uploaded_tsv **utsvp) | |
d5551862 | 10437 | { |
00bf0b85 | 10438 | struct remote_state *rs = get_remote_state (); |
d5551862 | 10439 | char *p; |
d5551862 | 10440 | |
00bf0b85 SS |
10441 | /* Ask for a first packet of variable definition. */ |
10442 | putpkt ("qTfV"); | |
d5551862 SS |
10443 | getpkt (&rs->buf, &rs->buf_size, 0); |
10444 | p = rs->buf; | |
00bf0b85 | 10445 | while (*p && *p != 'l') |
d5551862 | 10446 | { |
00bf0b85 SS |
10447 | parse_tsv_definition (p, utsvp); |
10448 | /* Ask for another packet of variable definition. */ | |
10449 | putpkt ("qTsV"); | |
d5551862 SS |
10450 | getpkt (&rs->buf, &rs->buf_size, 0); |
10451 | p = rs->buf; | |
10452 | } | |
00bf0b85 | 10453 | return 0; |
d5551862 SS |
10454 | } |
10455 | ||
c906108c | 10456 | void |
fba45db2 | 10457 | _initialize_remote (void) |
c906108c | 10458 | { |
ea9c271d | 10459 | struct remote_state *rs; |
9a7071a8 JB |
10460 | struct cmd_list_element *cmd; |
10461 | char *cmd_name; | |
ea9c271d | 10462 | |
0f71a2f6 | 10463 | /* architecture specific data */ |
2bc416ba | 10464 | remote_gdbarch_data_handle = |
23860348 | 10465 | gdbarch_data_register_post_init (init_remote_state); |
29709017 DJ |
10466 | remote_g_packet_data_handle = |
10467 | gdbarch_data_register_pre_init (remote_g_packet_data_init); | |
d01949b6 | 10468 | |
ea9c271d DJ |
10469 | /* Initialize the per-target state. At the moment there is only one |
10470 | of these, not one per target. Only one target is active at a | |
10471 | time. The default buffer size is unimportant; it will be expanded | |
10472 | whenever a larger buffer is needed. */ | |
0b83947e | 10473 | rs = get_remote_state_raw (); |
ea9c271d DJ |
10474 | rs->buf_size = 400; |
10475 | rs->buf = xmalloc (rs->buf_size); | |
10476 | ||
c906108c SS |
10477 | init_remote_ops (); |
10478 | add_target (&remote_ops); | |
10479 | ||
10480 | init_extended_remote_ops (); | |
10481 | add_target (&extended_remote_ops); | |
cce74817 | 10482 | |
dc8acb97 | 10483 | /* Hook into new objfile notification. */ |
06d3b283 | 10484 | observer_attach_new_objfile (remote_new_objfile); |
dc8acb97 | 10485 | |
b803fb0f DJ |
10486 | /* Set up signal handlers. */ |
10487 | sigint_remote_token = | |
10488 | create_async_signal_handler (async_remote_interrupt, NULL); | |
10489 | sigint_remote_twice_token = | |
10490 | create_async_signal_handler (inferior_event_handler_wrapper, NULL); | |
10491 | ||
c906108c SS |
10492 | #if 0 |
10493 | init_remote_threadtests (); | |
10494 | #endif | |
10495 | ||
23860348 | 10496 | /* set/show remote ... */ |
d471ea57 | 10497 | |
1bedd215 | 10498 | add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, _("\ |
5a2468f5 JM |
10499 | Remote protocol specific variables\n\ |
10500 | Configure various remote-protocol specific variables such as\n\ | |
1bedd215 | 10501 | the packets being used"), |
cff3e48b | 10502 | &remote_set_cmdlist, "set remote ", |
23860348 | 10503 | 0 /* allow-unknown */, &setlist); |
1bedd215 | 10504 | add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\ |
5a2468f5 JM |
10505 | Remote protocol specific variables\n\ |
10506 | Configure various remote-protocol specific variables such as\n\ | |
1bedd215 | 10507 | the packets being used"), |
cff3e48b | 10508 | &remote_show_cmdlist, "show remote ", |
23860348 | 10509 | 0 /* allow-unknown */, &showlist); |
5a2468f5 | 10510 | |
1a966eab AC |
10511 | add_cmd ("compare-sections", class_obscure, compare_sections_command, _("\ |
10512 | Compare section data on target to the exec file.\n\ | |
10513 | Argument is a single section name (default: all loaded sections)."), | |
c906108c SS |
10514 | &cmdlist); |
10515 | ||
1a966eab AC |
10516 | add_cmd ("packet", class_maintenance, packet_command, _("\ |
10517 | Send an arbitrary packet to a remote target.\n\ | |
c906108c SS |
10518 | maintenance packet TEXT\n\ |
10519 | If GDB is talking to an inferior via the GDB serial protocol, then\n\ | |
10520 | this command sends the string TEXT to the inferior, and displays the\n\ | |
10521 | response packet. GDB supplies the initial `$' character, and the\n\ | |
1a966eab | 10522 | terminating `#' character and checksum."), |
c906108c SS |
10523 | &maintenancelist); |
10524 | ||
7915a72c AC |
10525 | add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, _("\ |
10526 | Set whether to send break if interrupted."), _("\ | |
10527 | Show whether to send break if interrupted."), _("\ | |
10528 | If set, a break, instead of a cntrl-c, is sent to the remote target."), | |
9a7071a8 | 10529 | set_remotebreak, show_remotebreak, |
e707bbc2 | 10530 | &setlist, &showlist); |
9a7071a8 JB |
10531 | cmd_name = "remotebreak"; |
10532 | cmd = lookup_cmd (&cmd_name, setlist, "", -1, 1); | |
10533 | deprecate_cmd (cmd, "set remote interrupt-sequence"); | |
10534 | cmd_name = "remotebreak"; /* needed because lookup_cmd updates the pointer */ | |
10535 | cmd = lookup_cmd (&cmd_name, showlist, "", -1, 1); | |
10536 | deprecate_cmd (cmd, "show remote interrupt-sequence"); | |
10537 | ||
10538 | add_setshow_enum_cmd ("interrupt-sequence", class_support, | |
10539 | interrupt_sequence_modes, &interrupt_sequence_mode, _("\ | |
10540 | Set interrupt sequence to remote target."), _("\ | |
10541 | Show interrupt sequence to remote target."), _("\ | |
10542 | Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."), | |
10543 | NULL, show_interrupt_sequence, | |
10544 | &remote_set_cmdlist, | |
10545 | &remote_show_cmdlist); | |
10546 | ||
10547 | add_setshow_boolean_cmd ("interrupt-on-connect", class_support, | |
10548 | &interrupt_on_connect, _("\ | |
10549 | Set whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \ | |
10550 | Show whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \ | |
10551 | If set, interrupt sequence is sent to remote target."), | |
10552 | NULL, NULL, | |
10553 | &remote_set_cmdlist, &remote_show_cmdlist); | |
c906108c | 10554 | |
23860348 | 10555 | /* Install commands for configuring memory read/write packets. */ |
11cf8741 | 10556 | |
1a966eab AC |
10557 | add_cmd ("remotewritesize", no_class, set_memory_write_packet_size, _("\ |
10558 | Set the maximum number of bytes per memory write packet (deprecated)."), | |
11cf8741 | 10559 | &setlist); |
1a966eab AC |
10560 | add_cmd ("remotewritesize", no_class, show_memory_write_packet_size, _("\ |
10561 | Show the maximum number of bytes per memory write packet (deprecated)."), | |
11cf8741 JM |
10562 | &showlist); |
10563 | add_cmd ("memory-write-packet-size", no_class, | |
1a966eab AC |
10564 | set_memory_write_packet_size, _("\ |
10565 | Set the maximum number of bytes per memory-write packet.\n\ | |
10566 | Specify the number of bytes in a packet or 0 (zero) for the\n\ | |
10567 | default packet size. The actual limit is further reduced\n\ | |
10568 | dependent on the target. Specify ``fixed'' to disable the\n\ | |
10569 | further restriction and ``limit'' to enable that restriction."), | |
11cf8741 JM |
10570 | &remote_set_cmdlist); |
10571 | add_cmd ("memory-read-packet-size", no_class, | |
1a966eab AC |
10572 | set_memory_read_packet_size, _("\ |
10573 | Set the maximum number of bytes per memory-read packet.\n\ | |
10574 | Specify the number of bytes in a packet or 0 (zero) for the\n\ | |
10575 | default packet size. The actual limit is further reduced\n\ | |
10576 | dependent on the target. Specify ``fixed'' to disable the\n\ | |
10577 | further restriction and ``limit'' to enable that restriction."), | |
11cf8741 JM |
10578 | &remote_set_cmdlist); |
10579 | add_cmd ("memory-write-packet-size", no_class, | |
10580 | show_memory_write_packet_size, | |
1a966eab | 10581 | _("Show the maximum number of bytes per memory-write packet."), |
11cf8741 JM |
10582 | &remote_show_cmdlist); |
10583 | add_cmd ("memory-read-packet-size", no_class, | |
10584 | show_memory_read_packet_size, | |
1a966eab | 10585 | _("Show the maximum number of bytes per memory-read packet."), |
11cf8741 | 10586 | &remote_show_cmdlist); |
c906108c | 10587 | |
b3f42336 | 10588 | add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class, |
7915a72c AC |
10589 | &remote_hw_watchpoint_limit, _("\ |
10590 | Set the maximum number of target hardware watchpoints."), _("\ | |
10591 | Show the maximum number of target hardware watchpoints."), _("\ | |
10592 | Specify a negative limit for unlimited."), | |
2c5b56ce | 10593 | NULL, NULL, /* FIXME: i18n: The maximum number of target hardware watchpoints is %s. */ |
b3f42336 AC |
10594 | &remote_set_cmdlist, &remote_show_cmdlist); |
10595 | add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class, | |
7915a72c AC |
10596 | &remote_hw_breakpoint_limit, _("\ |
10597 | Set the maximum number of target hardware breakpoints."), _("\ | |
10598 | Show the maximum number of target hardware breakpoints."), _("\ | |
10599 | Specify a negative limit for unlimited."), | |
2c5b56ce | 10600 | NULL, NULL, /* FIXME: i18n: The maximum number of target hardware breakpoints is %s. */ |
b3f42336 | 10601 | &remote_set_cmdlist, &remote_show_cmdlist); |
501eef12 | 10602 | |
4d28ad1e AC |
10603 | add_setshow_integer_cmd ("remoteaddresssize", class_obscure, |
10604 | &remote_address_size, _("\ | |
10605 | Set the maximum size of the address (in bits) in a memory packet."), _("\ | |
10606 | Show the maximum size of the address (in bits) in a memory packet."), NULL, | |
10607 | NULL, | |
10608 | NULL, /* FIXME: i18n: */ | |
10609 | &setlist, &showlist); | |
c906108c | 10610 | |
444abaca | 10611 | add_packet_config_cmd (&remote_protocol_packets[PACKET_X], |
bb572ddd | 10612 | "X", "binary-download", 1); |
0f71a2f6 | 10613 | |
444abaca | 10614 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vCont], |
bb572ddd | 10615 | "vCont", "verbose-resume", 0); |
506fb367 | 10616 | |
89be2091 DJ |
10617 | add_packet_config_cmd (&remote_protocol_packets[PACKET_QPassSignals], |
10618 | "QPassSignals", "pass-signals", 0); | |
10619 | ||
444abaca | 10620 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol], |
bb572ddd | 10621 | "qSymbol", "symbol-lookup", 0); |
dc8acb97 | 10622 | |
444abaca | 10623 | add_packet_config_cmd (&remote_protocol_packets[PACKET_P], |
bb572ddd | 10624 | "P", "set-register", 1); |
d471ea57 | 10625 | |
444abaca | 10626 | add_packet_config_cmd (&remote_protocol_packets[PACKET_p], |
bb572ddd | 10627 | "p", "fetch-register", 1); |
b96ec7ac | 10628 | |
444abaca | 10629 | add_packet_config_cmd (&remote_protocol_packets[PACKET_Z0], |
bb572ddd | 10630 | "Z0", "software-breakpoint", 0); |
d471ea57 | 10631 | |
444abaca | 10632 | add_packet_config_cmd (&remote_protocol_packets[PACKET_Z1], |
bb572ddd | 10633 | "Z1", "hardware-breakpoint", 0); |
d471ea57 | 10634 | |
444abaca | 10635 | add_packet_config_cmd (&remote_protocol_packets[PACKET_Z2], |
bb572ddd | 10636 | "Z2", "write-watchpoint", 0); |
d471ea57 | 10637 | |
444abaca | 10638 | add_packet_config_cmd (&remote_protocol_packets[PACKET_Z3], |
bb572ddd | 10639 | "Z3", "read-watchpoint", 0); |
d471ea57 | 10640 | |
444abaca | 10641 | add_packet_config_cmd (&remote_protocol_packets[PACKET_Z4], |
bb572ddd | 10642 | "Z4", "access-watchpoint", 0); |
d471ea57 | 10643 | |
0876f84a DJ |
10644 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_auxv], |
10645 | "qXfer:auxv:read", "read-aux-vector", 0); | |
802188a7 | 10646 | |
23181151 DJ |
10647 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_features], |
10648 | "qXfer:features:read", "target-features", 0); | |
10649 | ||
cfa9d6d9 DJ |
10650 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries], |
10651 | "qXfer:libraries:read", "library-info", 0); | |
10652 | ||
fd79ecee DJ |
10653 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_memory_map], |
10654 | "qXfer:memory-map:read", "memory-map", 0); | |
10655 | ||
0e7f50da UW |
10656 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_read], |
10657 | "qXfer:spu:read", "read-spu-object", 0); | |
10658 | ||
10659 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_write], | |
10660 | "qXfer:spu:write", "write-spu-object", 0); | |
10661 | ||
07e059b5 VP |
10662 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_osdata], |
10663 | "qXfer:osdata:read", "osdata", 0); | |
10664 | ||
dc146f7c VP |
10665 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_threads], |
10666 | "qXfer:threads:read", "threads", 0); | |
10667 | ||
4aa995e1 PA |
10668 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_read], |
10669 | "qXfer:siginfo:read", "read-siginfo-object", 0); | |
10670 | ||
10671 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_write], | |
10672 | "qXfer:siginfo:write", "write-siginfo-object", 0); | |
10673 | ||
444abaca | 10674 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTLSAddr], |
38691318 | 10675 | "qGetTLSAddr", "get-thread-local-storage-address", |
38691318 KB |
10676 | 0); |
10677 | ||
711e434b PM |
10678 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTIBAddr], |
10679 | "qGetTIBAddr", "get-thread-information-block-address", | |
10680 | 0); | |
10681 | ||
40ab02ce MS |
10682 | add_packet_config_cmd (&remote_protocol_packets[PACKET_bc], |
10683 | "bc", "reverse-continue", 0); | |
10684 | ||
10685 | add_packet_config_cmd (&remote_protocol_packets[PACKET_bs], | |
10686 | "bs", "reverse-step", 0); | |
10687 | ||
be2a5f71 DJ |
10688 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qSupported], |
10689 | "qSupported", "supported-packets", 0); | |
10690 | ||
08388c79 DE |
10691 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qSearch_memory], |
10692 | "qSearch:memory", "search-memory", 0); | |
10693 | ||
a6b151f1 DJ |
10694 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_open], |
10695 | "vFile:open", "hostio-open", 0); | |
10696 | ||
10697 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pread], | |
10698 | "vFile:pread", "hostio-pread", 0); | |
10699 | ||
10700 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pwrite], | |
10701 | "vFile:pwrite", "hostio-pwrite", 0); | |
10702 | ||
10703 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_close], | |
10704 | "vFile:close", "hostio-close", 0); | |
10705 | ||
10706 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_unlink], | |
10707 | "vFile:unlink", "hostio-unlink", 0); | |
10708 | ||
2d717e4f DJ |
10709 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach], |
10710 | "vAttach", "attach", 0); | |
10711 | ||
10712 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vRun], | |
10713 | "vRun", "run", 0); | |
10714 | ||
a6f3e723 SL |
10715 | add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartNoAckMode], |
10716 | "QStartNoAckMode", "noack", 0); | |
10717 | ||
82f73884 PA |
10718 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vKill], |
10719 | "vKill", "kill", 0); | |
10720 | ||
0b16c5cf PA |
10721 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qAttached], |
10722 | "qAttached", "query-attached", 0); | |
10723 | ||
782b2b07 SS |
10724 | add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalTracepoints], |
10725 | "ConditionalTracepoints", "conditional-tracepoints", 0); | |
7a697b8d SS |
10726 | add_packet_config_cmd (&remote_protocol_packets[PACKET_FastTracepoints], |
10727 | "FastTracepoints", "fast-tracepoints", 0); | |
782b2b07 | 10728 | |
409873ef SS |
10729 | add_packet_config_cmd (&remote_protocol_packets[PACKET_TracepointSource], |
10730 | "TracepointSource", "TracepointSource", 0); | |
10731 | ||
d914c394 SS |
10732 | add_packet_config_cmd (&remote_protocol_packets[PACKET_QAllow], |
10733 | "QAllow", "allow", 0); | |
10734 | ||
0fb4aa4b PA |
10735 | add_packet_config_cmd (&remote_protocol_packets[PACKET_StaticTracepoints], |
10736 | "StaticTracepoints", "static-tracepoints", 0); | |
10737 | ||
10738 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_statictrace_read], | |
10739 | "qXfer:statictrace:read", "read-sdata-object", 0); | |
10740 | ||
37a105a1 DJ |
10741 | /* Keep the old ``set remote Z-packet ...'' working. Each individual |
10742 | Z sub-packet has its own set and show commands, but users may | |
10743 | have sets to this variable in their .gdbinit files (or in their | |
10744 | documentation). */ | |
e9e68a56 | 10745 | add_setshow_auto_boolean_cmd ("Z-packet", class_obscure, |
7915a72c AC |
10746 | &remote_Z_packet_detect, _("\ |
10747 | Set use of remote protocol `Z' packets"), _("\ | |
10748 | Show use of remote protocol `Z' packets "), _("\ | |
3b64bf98 | 10749 | When set, GDB will attempt to use the remote breakpoint and watchpoint\n\ |
7915a72c | 10750 | packets."), |
e9e68a56 | 10751 | set_remote_protocol_Z_packet_cmd, |
2c5b56ce | 10752 | show_remote_protocol_Z_packet_cmd, /* FIXME: i18n: Use of remote protocol `Z' packets is %s. */ |
e9e68a56 | 10753 | &remote_set_cmdlist, &remote_show_cmdlist); |
449092f6 | 10754 | |
a6b151f1 DJ |
10755 | add_prefix_cmd ("remote", class_files, remote_command, _("\ |
10756 | Manipulate files on the remote system\n\ | |
10757 | Transfer files to and from the remote target system."), | |
10758 | &remote_cmdlist, "remote ", | |
10759 | 0 /* allow-unknown */, &cmdlist); | |
10760 | ||
10761 | add_cmd ("put", class_files, remote_put_command, | |
10762 | _("Copy a local file to the remote system."), | |
10763 | &remote_cmdlist); | |
10764 | ||
10765 | add_cmd ("get", class_files, remote_get_command, | |
10766 | _("Copy a remote file to the local system."), | |
10767 | &remote_cmdlist); | |
10768 | ||
10769 | add_cmd ("delete", class_files, remote_delete_command, | |
10770 | _("Delete a remote file."), | |
10771 | &remote_cmdlist); | |
10772 | ||
2d717e4f DJ |
10773 | remote_exec_file = xstrdup (""); |
10774 | add_setshow_string_noescape_cmd ("exec-file", class_files, | |
10775 | &remote_exec_file, _("\ | |
10776 | Set the remote pathname for \"run\""), _("\ | |
10777 | Show the remote pathname for \"run\""), NULL, NULL, NULL, | |
10778 | &remote_set_cmdlist, &remote_show_cmdlist); | |
10779 | ||
449092f6 CV |
10780 | /* Eventually initialize fileio. See fileio.c */ |
10781 | initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist); | |
79d7f229 PA |
10782 | |
10783 | /* Take advantage of the fact that the LWP field is not used, to tag | |
10784 | special ptids with it set to != 0. */ | |
82f73884 PA |
10785 | magic_null_ptid = ptid_build (42000, 1, -1); |
10786 | not_sent_ptid = ptid_build (42000, 1, -2); | |
10787 | any_thread_ptid = ptid_build (42000, 1, 0); | |
35b1e5cc SS |
10788 | |
10789 | target_buf_size = 2048; | |
10790 | target_buf = xmalloc (target_buf_size); | |
c906108c | 10791 | } |
10760264 | 10792 |