Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Remote target communications for serial-line targets in custom GDB protocol |
8926118c | 2 | |
ecd75fc8 | 3 | Copyright (C) 1988-2014 Free Software Foundation, Inc. |
c906108c | 4 | |
c5aa993b JM |
5 | This file is part of GDB. |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b JM |
10 | (at your option) any later version. |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c5aa993b | 19 | |
23860348 | 20 | /* See the GDB User Guide for details of the GDB remote protocol. */ |
c5aa993b | 21 | |
c906108c | 22 | #include "defs.h" |
0e9f083f | 23 | #include <string.h> |
c906108c SS |
24 | #include <ctype.h> |
25 | #include <fcntl.h> | |
c906108c SS |
26 | #include "inferior.h" |
27 | #include "bfd.h" | |
28 | #include "symfile.h" | |
60250e8b | 29 | #include "exceptions.h" |
c906108c | 30 | #include "target.h" |
c5aa993b | 31 | /*#include "terminal.h" */ |
c906108c SS |
32 | #include "gdbcmd.h" |
33 | #include "objfiles.h" | |
34 | #include "gdb-stabs.h" | |
35 | #include "gdbthread.h" | |
c2c6d25f | 36 | #include "remote.h" |
722247f1 | 37 | #include "remote-notif.h" |
4e052eda | 38 | #include "regcache.h" |
fd0407d6 | 39 | #include "value.h" |
1ff9c3d6 | 40 | #include "gdb_assert.h" |
6867ae3e | 41 | #include "observer.h" |
a77053c2 | 42 | #include "solib.h" |
37a105a1 DJ |
43 | #include "cli/cli-decode.h" |
44 | #include "cli/cli-setshow.h" | |
424163ea | 45 | #include "target-descriptions.h" |
a4453b7e | 46 | #include "gdb_bfd.h" |
614c279d | 47 | #include "filestuff.h" |
c906108c | 48 | |
7a292a7a | 49 | #include <ctype.h> |
9846de1b | 50 | #include <sys/time.h> |
c906108c | 51 | |
43ff13b4 | 52 | #include "event-loop.h" |
c2c6d25f | 53 | #include "event-top.h" |
2acceee2 | 54 | #include "inf-loop.h" |
43ff13b4 | 55 | |
c906108c SS |
56 | #include <signal.h> |
57 | #include "serial.h" | |
58 | ||
6240bebf MS |
59 | #include "gdbcore.h" /* for exec_bfd */ |
60 | ||
449092f6 | 61 | #include "remote-fileio.h" |
a6b151f1 | 62 | #include "gdb/fileio.h" |
53ce3c39 | 63 | #include <sys/stat.h> |
dc146f7c | 64 | #include "xml-support.h" |
449092f6 | 65 | |
fd79ecee DJ |
66 | #include "memory-map.h" |
67 | ||
35b1e5cc SS |
68 | #include "tracepoint.h" |
69 | #include "ax.h" | |
70 | #include "ax-gdb.h" | |
d1feda86 | 71 | #include "agent.h" |
9accd112 | 72 | #include "btrace.h" |
35b1e5cc | 73 | |
0df8b418 | 74 | /* Temp hacks for tracepoint encoding migration. */ |
35b1e5cc SS |
75 | static char *target_buf; |
76 | static long target_buf_size; | |
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. */ |
934b9bac | 91 | static void async_cleanup_sigint_signal_handler (void *dummy); |
6d820c5c | 92 | static int getpkt_sane (char **buf, long *sizeof_buf, int forever); |
74531fed | 93 | static int getpkt_or_notif_sane (char **buf, long *sizeof_buf, |
fee9eda9 | 94 | int forever, int *is_notif); |
6426a772 | 95 | |
934b9bac JK |
96 | static void async_handle_remote_sigint (int); |
97 | static void async_handle_remote_sigint_twice (int); | |
43ff13b4 | 98 | |
a14ed312 | 99 | static void remote_files_info (struct target_ops *ignore); |
c906108c | 100 | |
316f2060 | 101 | static void remote_prepare_to_store (struct regcache *regcache); |
c906108c | 102 | |
a14ed312 | 103 | static void remote_open (char *name, int from_tty); |
c906108c | 104 | |
a14ed312 | 105 | static void extended_remote_open (char *name, int from_tty); |
c906108c | 106 | |
75c99385 | 107 | static void remote_open_1 (char *, int, struct target_ops *, int extended_p); |
c906108c | 108 | |
460014f5 | 109 | static void remote_close (void); |
c906108c | 110 | |
136d6dae | 111 | static void remote_mourn (struct target_ops *ops); |
c906108c | 112 | |
a14ed312 | 113 | static void extended_remote_restart (void); |
c906108c | 114 | |
136d6dae | 115 | static void extended_remote_mourn (struct target_ops *); |
c906108c | 116 | |
a14ed312 | 117 | static void remote_mourn_1 (struct target_ops *); |
c906108c | 118 | |
6d820c5c | 119 | static void remote_send (char **buf, long *sizeof_buf_p); |
c906108c | 120 | |
a14ed312 | 121 | static int readchar (int timeout); |
c906108c | 122 | |
c33e31fd PA |
123 | static void remote_serial_write (const char *str, int len); |
124 | ||
7d85a9c0 | 125 | static void remote_kill (struct target_ops *ops); |
c906108c | 126 | |
a14ed312 | 127 | static int tohex (int nib); |
c906108c | 128 | |
75c99385 PA |
129 | static int remote_can_async_p (void); |
130 | ||
131 | static int remote_is_async_p (void); | |
132 | ||
133 | static void remote_async (void (*callback) (enum inferior_event_type event_type, | |
134 | void *context), void *context); | |
135 | ||
934b9bac | 136 | static void sync_remote_interrupt_twice (int signo); |
7a292a7a | 137 | |
a14ed312 | 138 | static void interrupt_query (void); |
c906108c | 139 | |
79d7f229 PA |
140 | static void set_general_thread (struct ptid ptid); |
141 | static void set_continue_thread (struct ptid ptid); | |
c906108c | 142 | |
a14ed312 | 143 | static void get_offsets (void); |
c906108c | 144 | |
6d820c5c DJ |
145 | static void skip_frame (void); |
146 | ||
147 | static long read_frame (char **buf_p, long *sizeof_buf); | |
c906108c | 148 | |
a14ed312 | 149 | static int hexnumlen (ULONGEST num); |
c906108c | 150 | |
a14ed312 | 151 | static void init_remote_ops (void); |
c906108c | 152 | |
a14ed312 | 153 | static void init_extended_remote_ops (void); |
c906108c | 154 | |
94cc34af | 155 | static void remote_stop (ptid_t); |
c906108c | 156 | |
a14ed312 | 157 | static int ishex (int ch, int *val); |
c906108c | 158 | |
a14ed312 | 159 | static int stubhex (int ch); |
c906108c | 160 | |
a14ed312 | 161 | static int hexnumstr (char *, ULONGEST); |
c906108c | 162 | |
a14ed312 | 163 | static int hexnumnstr (char *, ULONGEST, int); |
2df3850c | 164 | |
a14ed312 | 165 | static CORE_ADDR remote_address_masked (CORE_ADDR); |
c906108c | 166 | |
a14ed312 | 167 | static void print_packet (char *); |
c906108c | 168 | |
a14ed312 | 169 | static void compare_sections_command (char *, int); |
c906108c | 170 | |
a14ed312 | 171 | static void packet_command (char *, int); |
c906108c | 172 | |
a14ed312 | 173 | static int stub_unpack_int (char *buff, int fieldlength); |
c906108c | 174 | |
39f77062 | 175 | static ptid_t remote_current_thread (ptid_t oldptid); |
c906108c | 176 | |
a14ed312 | 177 | static void remote_find_new_threads (void); |
c906108c | 178 | |
30559e10 | 179 | static int fromhex (int a); |
c906108c | 180 | |
a14ed312 | 181 | static int putpkt_binary (char *buf, int cnt); |
c906108c | 182 | |
a14ed312 | 183 | static void check_binary_download (CORE_ADDR addr); |
c906108c | 184 | |
5a2468f5 | 185 | struct packet_config; |
5a2468f5 | 186 | |
a14ed312 | 187 | static void show_packet_config_cmd (struct packet_config *config); |
5a2468f5 | 188 | |
d471ea57 | 189 | static void update_packet_config (struct packet_config *config); |
5a2468f5 | 190 | |
bb572ddd DJ |
191 | static void set_remote_protocol_packet_cmd (char *args, int from_tty, |
192 | struct cmd_list_element *c); | |
193 | ||
194 | static void show_remote_protocol_packet_cmd (struct ui_file *file, | |
195 | int from_tty, | |
196 | struct cmd_list_element *c, | |
197 | const char *value); | |
198 | ||
82f73884 PA |
199 | static char *write_ptid (char *buf, const char *endbuf, ptid_t ptid); |
200 | static ptid_t read_ptid (char *buf, char **obuf); | |
201 | ||
d914c394 SS |
202 | static void remote_set_permissions (void); |
203 | ||
d5551862 | 204 | struct remote_state; |
00bf0b85 | 205 | static int remote_get_trace_status (struct trace_status *ts); |
d5551862 | 206 | |
00bf0b85 SS |
207 | static int remote_upload_tracepoints (struct uploaded_tp **utpp); |
208 | ||
209 | static int remote_upload_trace_state_variables (struct uploaded_tsv **utsvp); | |
210 | ||
c8d104ad PA |
211 | static void remote_query_supported (void); |
212 | ||
36d25514 | 213 | static void remote_check_symbols (void); |
c8d104ad | 214 | |
a14ed312 | 215 | void _initialize_remote (void); |
c906108c | 216 | |
74531fed | 217 | struct stop_reply; |
74531fed | 218 | static void stop_reply_xfree (struct stop_reply *); |
722247f1 | 219 | static void remote_parse_stop_reply (char *, struct stop_reply *); |
74531fed | 220 | static void push_stop_reply (struct stop_reply *); |
bcc75809 | 221 | static void discard_pending_stop_replies_in_queue (struct remote_state *); |
74531fed PA |
222 | static int peek_stop_reply (ptid_t ptid); |
223 | ||
224 | static void remote_async_inferior_event_handler (gdb_client_data); | |
74531fed | 225 | |
d3fd5342 PA |
226 | static void remote_terminal_ours (void); |
227 | ||
d962ef82 DJ |
228 | static int remote_read_description_p (struct target_ops *target); |
229 | ||
176a6961 | 230 | static void remote_console_output (char *msg); |
dde08ee1 | 231 | |
b775012e LM |
232 | static int remote_supports_cond_breakpoints (void); |
233 | ||
d3ce09f5 SS |
234 | static int remote_can_run_breakpoint_commands (void); |
235 | ||
a6b151f1 DJ |
236 | /* For "remote". */ |
237 | ||
238 | static struct cmd_list_element *remote_cmdlist; | |
239 | ||
bb572ddd DJ |
240 | /* For "set remote" and "show remote". */ |
241 | ||
242 | static struct cmd_list_element *remote_set_cmdlist; | |
243 | static struct cmd_list_element *remote_show_cmdlist; | |
244 | ||
d458bd84 PA |
245 | /* Stub vCont actions support. |
246 | ||
247 | Each field is a boolean flag indicating whether the stub reports | |
248 | support for the corresponding action. */ | |
249 | ||
250 | struct vCont_action_support | |
251 | { | |
252 | /* vCont;t */ | |
253 | int t; | |
c1e36e3e PA |
254 | |
255 | /* vCont;r */ | |
256 | int r; | |
d458bd84 PA |
257 | }; |
258 | ||
c1e36e3e PA |
259 | /* Controls whether GDB is willing to use range stepping. */ |
260 | ||
261 | static int use_range_stepping = 1; | |
262 | ||
0d031856 TT |
263 | #define OPAQUETHREADBYTES 8 |
264 | ||
265 | /* a 64 bit opaque identifier */ | |
266 | typedef unsigned char threadref[OPAQUETHREADBYTES]; | |
267 | ||
268 | /* About this many threadisds fit in a packet. */ | |
269 | ||
270 | #define MAXTHREADLISTRESULTS 32 | |
271 | ||
ea9c271d DJ |
272 | /* Description of the remote protocol state for the currently |
273 | connected target. This is per-target state, and independent of the | |
274 | selected architecture. */ | |
275 | ||
276 | struct remote_state | |
277 | { | |
278 | /* A buffer to use for incoming packets, and its current size. The | |
279 | buffer is grown dynamically for larger incoming packets. | |
280 | Outgoing packets may also be constructed in this buffer. | |
281 | BUF_SIZE is always at least REMOTE_PACKET_SIZE; | |
282 | REMOTE_PACKET_SIZE should be used to limit the length of outgoing | |
283 | packets. */ | |
284 | char *buf; | |
285 | long buf_size; | |
be2a5f71 | 286 | |
1e51243a PA |
287 | /* True if we're going through initial connection setup (finding out |
288 | about the remote side's threads, relocating symbols, etc.). */ | |
289 | int starting_up; | |
290 | ||
be2a5f71 DJ |
291 | /* If we negotiated packet size explicitly (and thus can bypass |
292 | heuristics for the largest packet size that will not overflow | |
293 | a buffer in the stub), this will be set to that packet size. | |
294 | Otherwise zero, meaning to use the guessed size. */ | |
295 | long explicit_packet_size; | |
2d717e4f DJ |
296 | |
297 | /* remote_wait is normally called when the target is running and | |
298 | waits for a stop reply packet. But sometimes we need to call it | |
299 | when the target is already stopped. We can send a "?" packet | |
300 | and have remote_wait read the response. Or, if we already have | |
301 | the response, we can stash it in BUF and tell remote_wait to | |
302 | skip calling getpkt. This flag is set when BUF contains a | |
303 | stop reply packet and the target is not waiting. */ | |
304 | int cached_wait_status; | |
a6f3e723 SL |
305 | |
306 | /* True, if in no ack mode. That is, neither GDB nor the stub will | |
307 | expect acks from each other. The connection is assumed to be | |
308 | reliable. */ | |
309 | int noack_mode; | |
82f73884 PA |
310 | |
311 | /* True if we're connected in extended remote mode. */ | |
312 | int extended; | |
313 | ||
314 | /* True if the stub reported support for multi-process | |
315 | extensions. */ | |
316 | int multi_process_aware; | |
e24a49d8 PA |
317 | |
318 | /* True if we resumed the target and we're waiting for the target to | |
319 | stop. In the mean time, we can't start another command/query. | |
320 | The remote server wouldn't be ready to process it, so we'd | |
321 | timeout waiting for a reply that would never come and eventually | |
322 | we'd close the connection. This can happen in asynchronous mode | |
323 | because we allow GDB commands while the target is running. */ | |
324 | int waiting_for_stop_reply; | |
74531fed PA |
325 | |
326 | /* True if the stub reports support for non-stop mode. */ | |
327 | int non_stop_aware; | |
328 | ||
d458bd84 PA |
329 | /* The status of the stub support for the various vCont actions. */ |
330 | struct vCont_action_support supports_vCont; | |
782b2b07 SS |
331 | |
332 | /* True if the stub reports support for conditional tracepoints. */ | |
333 | int cond_tracepoints; | |
3a29589a | 334 | |
3788aec7 LM |
335 | /* True if the stub reports support for target-side breakpoint |
336 | conditions. */ | |
337 | int cond_breakpoints; | |
338 | ||
d3ce09f5 SS |
339 | /* True if the stub reports support for target-side breakpoint |
340 | commands. */ | |
341 | int breakpoint_commands; | |
342 | ||
7a697b8d SS |
343 | /* True if the stub reports support for fast tracepoints. */ |
344 | int fast_tracepoints; | |
345 | ||
0fb4aa4b PA |
346 | /* True if the stub reports support for static tracepoints. */ |
347 | int static_tracepoints; | |
348 | ||
1e4d1764 YQ |
349 | /* True if the stub reports support for installing tracepoint while |
350 | tracing. */ | |
351 | int install_in_trace; | |
352 | ||
d5551862 SS |
353 | /* True if the stub can continue running a trace while GDB is |
354 | disconnected. */ | |
355 | int disconnected_tracing; | |
356 | ||
d248b706 KY |
357 | /* True if the stub reports support for enabling and disabling |
358 | tracepoints while a trace experiment is running. */ | |
359 | int enable_disable_tracepoints; | |
360 | ||
3065dfb6 SS |
361 | /* True if the stub can collect strings using tracenz bytecode. */ |
362 | int string_tracing; | |
363 | ||
ced63ec0 GB |
364 | /* True if the stub supports qXfer:libraries-svr4:read with a |
365 | non-empty annex. */ | |
366 | int augmented_libraries_svr4_read; | |
367 | ||
3a29589a DJ |
368 | /* Nonzero if the user has pressed Ctrl-C, but the target hasn't |
369 | responded to that. */ | |
370 | int ctrlc_pending_p; | |
5d93a237 TT |
371 | |
372 | /* Descriptor for I/O to remote machine. Initialize it to NULL so that | |
373 | remote_open knows that we don't have a file open when the program | |
374 | starts. */ | |
375 | struct serial *remote_desc; | |
47f8a51d TT |
376 | |
377 | /* These are the threads which we last sent to the remote system. The | |
378 | TID member will be -1 for all or -2 for not sent yet. */ | |
379 | ptid_t general_thread; | |
380 | ptid_t continue_thread; | |
262e1174 TT |
381 | |
382 | /* This is the traceframe which we last selected on the remote system. | |
383 | It will be -1 if no traceframe is selected. */ | |
384 | int remote_traceframe_number; | |
747dc59d TT |
385 | |
386 | char *last_pass_packet; | |
5e4a05c4 TT |
387 | |
388 | /* The last QProgramSignals packet sent to the target. We bypass | |
389 | sending a new program signals list down to the target if the new | |
390 | packet is exactly the same as the last we sent. IOW, we only let | |
391 | the target know about program signals list changes. */ | |
392 | char *last_program_signals_packet; | |
b73be471 TT |
393 | |
394 | enum gdb_signal last_sent_signal; | |
280ceea3 TT |
395 | |
396 | int last_sent_step; | |
8e88304f TT |
397 | |
398 | char *finished_object; | |
399 | char *finished_annex; | |
400 | ULONGEST finished_offset; | |
b80fafe3 TT |
401 | |
402 | /* Should we try the 'ThreadInfo' query packet? | |
403 | ||
404 | This variable (NOT available to the user: auto-detect only!) | |
405 | determines whether GDB will use the new, simpler "ThreadInfo" | |
406 | query or the older, more complex syntax for thread queries. | |
407 | This is an auto-detect variable (set to true at each connect, | |
408 | and set to false when the target fails to recognize it). */ | |
409 | int use_threadinfo_query; | |
410 | int use_threadextra_query; | |
88b496c3 TT |
411 | |
412 | void (*async_client_callback) (enum inferior_event_type event_type, | |
413 | void *context); | |
414 | void *async_client_context; | |
ee154bee TT |
415 | |
416 | /* This is set to the data address of the access causing the target | |
417 | to stop for a watchpoint. */ | |
418 | CORE_ADDR remote_watch_data_address; | |
419 | ||
420 | /* This is non-zero if target stopped for a watchpoint. */ | |
421 | int remote_stopped_by_watchpoint_p; | |
0d031856 TT |
422 | |
423 | threadref echo_nextthread; | |
424 | threadref nextthread; | |
425 | threadref resultthreadlist[MAXTHREADLISTRESULTS]; | |
5965e028 YQ |
426 | |
427 | /* The state of remote notification. */ | |
428 | struct remote_notif_state *notif_state; | |
ea9c271d DJ |
429 | }; |
430 | ||
dc146f7c VP |
431 | /* Private data that we'll store in (struct thread_info)->private. */ |
432 | struct private_thread_info | |
433 | { | |
434 | char *extra; | |
435 | int core; | |
436 | }; | |
437 | ||
438 | static void | |
439 | free_private_thread_info (struct private_thread_info *info) | |
440 | { | |
441 | xfree (info->extra); | |
442 | xfree (info); | |
443 | } | |
444 | ||
82f73884 PA |
445 | /* Returns true if the multi-process extensions are in effect. */ |
446 | static int | |
447 | remote_multi_process_p (struct remote_state *rs) | |
448 | { | |
901f9912 | 449 | return rs->multi_process_aware; |
82f73884 PA |
450 | } |
451 | ||
ea9c271d DJ |
452 | /* This data could be associated with a target, but we do not always |
453 | have access to the current target when we need it, so for now it is | |
454 | static. This will be fine for as long as only one target is in use | |
455 | at a time. */ | |
cf792862 | 456 | static struct remote_state *remote_state; |
ea9c271d DJ |
457 | |
458 | static struct remote_state * | |
0b83947e | 459 | get_remote_state_raw (void) |
ea9c271d | 460 | { |
cf792862 TT |
461 | return remote_state; |
462 | } | |
463 | ||
464 | /* Allocate a new struct remote_state with xmalloc, initialize it, and | |
465 | return it. */ | |
466 | ||
467 | static struct remote_state * | |
468 | new_remote_state (void) | |
469 | { | |
470 | struct remote_state *result = XCNEW (struct remote_state); | |
471 | ||
472 | /* The default buffer size is unimportant; it will be expanded | |
473 | whenever a larger buffer is needed. */ | |
474 | result->buf_size = 400; | |
475 | result->buf = xmalloc (result->buf_size); | |
262e1174 | 476 | result->remote_traceframe_number = -1; |
b73be471 | 477 | result->last_sent_signal = GDB_SIGNAL_0; |
cf792862 TT |
478 | |
479 | return result; | |
ea9c271d DJ |
480 | } |
481 | ||
482 | /* Description of the remote protocol for a given architecture. */ | |
d01949b6 | 483 | |
ad10f812 AC |
484 | struct packet_reg |
485 | { | |
486 | long offset; /* Offset into G packet. */ | |
487 | long regnum; /* GDB's internal register number. */ | |
488 | LONGEST pnum; /* Remote protocol register number. */ | |
b323314b | 489 | int in_g_packet; /* Always part of G packet. */ |
f5656ead | 490 | /* long size in bytes; == register_size (target_gdbarch (), regnum); |
23860348 | 491 | at present. */ |
f5656ead | 492 | /* char *name; == gdbarch_register_name (target_gdbarch (), regnum); |
c9f4d572 | 493 | at present. */ |
ad10f812 AC |
494 | }; |
495 | ||
ea9c271d | 496 | struct remote_arch_state |
d01949b6 | 497 | { |
ad10f812 AC |
498 | /* Description of the remote protocol registers. */ |
499 | long sizeof_g_packet; | |
b323314b AC |
500 | |
501 | /* Description of the remote protocol registers indexed by REGNUM | |
f57d151a | 502 | (making an array gdbarch_num_regs in size). */ |
b323314b | 503 | struct packet_reg *regs; |
ad10f812 | 504 | |
d01949b6 AC |
505 | /* This is the size (in chars) of the first response to the ``g'' |
506 | packet. It is used as a heuristic when determining the maximum | |
507 | size of memory-read and memory-write packets. A target will | |
508 | typically only reserve a buffer large enough to hold the ``g'' | |
509 | packet. The size does not include packet overhead (headers and | |
23860348 | 510 | trailers). */ |
d01949b6 AC |
511 | long actual_register_packet_size; |
512 | ||
513 | /* This is the maximum size (in chars) of a non read/write packet. | |
23860348 | 514 | It is also used as a cap on the size of read/write packets. */ |
d01949b6 AC |
515 | long remote_packet_size; |
516 | }; | |
517 | ||
35b1e5cc SS |
518 | /* Utility: generate error from an incoming stub packet. */ |
519 | static void | |
520 | trace_error (char *buf) | |
521 | { | |
522 | if (*buf++ != 'E') | |
523 | return; /* not an error msg */ | |
524 | switch (*buf) | |
525 | { | |
526 | case '1': /* malformed packet error */ | |
527 | if (*++buf == '0') /* general case: */ | |
528 | error (_("remote.c: error in outgoing packet.")); | |
529 | else | |
530 | error (_("remote.c: error in outgoing packet at field #%ld."), | |
531 | strtol (buf, NULL, 16)); | |
35b1e5cc SS |
532 | default: |
533 | error (_("Target returns error code '%s'."), buf); | |
534 | } | |
535 | } | |
536 | ||
537 | /* Utility: wait for reply from stub, while accepting "O" packets. */ | |
538 | static char * | |
539 | remote_get_noisy_reply (char **buf_p, | |
540 | long *sizeof_buf) | |
541 | { | |
542 | do /* Loop on reply from remote stub. */ | |
543 | { | |
544 | char *buf; | |
a744cf53 | 545 | |
0df8b418 | 546 | QUIT; /* Allow user to bail out with ^C. */ |
35b1e5cc SS |
547 | getpkt (buf_p, sizeof_buf, 0); |
548 | buf = *buf_p; | |
ad91cd99 | 549 | if (buf[0] == 'E') |
35b1e5cc | 550 | trace_error (buf); |
dde08ee1 PA |
551 | else if (strncmp (buf, "qRelocInsn:", strlen ("qRelocInsn:")) == 0) |
552 | { | |
553 | ULONGEST ul; | |
554 | CORE_ADDR from, to, org_to; | |
555 | char *p, *pp; | |
556 | int adjusted_size = 0; | |
557 | volatile struct gdb_exception ex; | |
558 | ||
559 | p = buf + strlen ("qRelocInsn:"); | |
560 | pp = unpack_varlen_hex (p, &ul); | |
561 | if (*pp != ';') | |
cb91c06a | 562 | error (_("invalid qRelocInsn packet: %s"), buf); |
dde08ee1 PA |
563 | from = ul; |
564 | ||
565 | p = pp + 1; | |
a9cbf802 | 566 | unpack_varlen_hex (p, &ul); |
dde08ee1 PA |
567 | to = ul; |
568 | ||
569 | org_to = to; | |
570 | ||
571 | TRY_CATCH (ex, RETURN_MASK_ALL) | |
572 | { | |
f5656ead | 573 | gdbarch_relocate_instruction (target_gdbarch (), &to, from); |
dde08ee1 PA |
574 | } |
575 | if (ex.reason >= 0) | |
576 | { | |
577 | adjusted_size = to - org_to; | |
578 | ||
bba74b36 | 579 | xsnprintf (buf, *sizeof_buf, "qRelocInsn:%x", adjusted_size); |
dde08ee1 PA |
580 | putpkt (buf); |
581 | } | |
582 | else if (ex.reason < 0 && ex.error == MEMORY_ERROR) | |
583 | { | |
584 | /* Propagate memory errors silently back to the target. | |
585 | The stub may have limited the range of addresses we | |
586 | can write to, for example. */ | |
587 | putpkt ("E01"); | |
588 | } | |
589 | else | |
590 | { | |
591 | /* Something unexpectedly bad happened. Be verbose so | |
592 | we can tell what, and propagate the error back to the | |
593 | stub, so it doesn't get stuck waiting for a | |
594 | response. */ | |
595 | exception_fprintf (gdb_stderr, ex, | |
596 | _("warning: relocating instruction: ")); | |
597 | putpkt ("E01"); | |
598 | } | |
599 | } | |
ad91cd99 | 600 | else if (buf[0] == 'O' && buf[1] != 'K') |
35b1e5cc SS |
601 | remote_console_output (buf + 1); /* 'O' message from stub */ |
602 | else | |
0df8b418 | 603 | return buf; /* Here's the actual reply. */ |
35b1e5cc SS |
604 | } |
605 | while (1); | |
606 | } | |
3c3bea1c | 607 | |
d01949b6 AC |
608 | /* Handle for retreving the remote protocol data from gdbarch. */ |
609 | static struct gdbarch_data *remote_gdbarch_data_handle; | |
610 | ||
ea9c271d DJ |
611 | static struct remote_arch_state * |
612 | get_remote_arch_state (void) | |
d01949b6 | 613 | { |
f5656ead | 614 | return gdbarch_data (target_gdbarch (), remote_gdbarch_data_handle); |
d01949b6 AC |
615 | } |
616 | ||
0b83947e DJ |
617 | /* Fetch the global remote target state. */ |
618 | ||
619 | static struct remote_state * | |
620 | get_remote_state (void) | |
621 | { | |
622 | /* Make sure that the remote architecture state has been | |
623 | initialized, because doing so might reallocate rs->buf. Any | |
624 | function which calls getpkt also needs to be mindful of changes | |
625 | to rs->buf, but this call limits the number of places which run | |
626 | into trouble. */ | |
627 | get_remote_arch_state (); | |
628 | ||
629 | return get_remote_state_raw (); | |
630 | } | |
631 | ||
74ca34ce DJ |
632 | static int |
633 | compare_pnums (const void *lhs_, const void *rhs_) | |
634 | { | |
635 | const struct packet_reg * const *lhs = lhs_; | |
636 | const struct packet_reg * const *rhs = rhs_; | |
637 | ||
638 | if ((*lhs)->pnum < (*rhs)->pnum) | |
639 | return -1; | |
640 | else if ((*lhs)->pnum == (*rhs)->pnum) | |
641 | return 0; | |
642 | else | |
643 | return 1; | |
644 | } | |
645 | ||
c21236dc PA |
646 | static int |
647 | map_regcache_remote_table (struct gdbarch *gdbarch, struct packet_reg *regs) | |
d01949b6 | 648 | { |
74ca34ce | 649 | int regnum, num_remote_regs, offset; |
74ca34ce | 650 | struct packet_reg **remote_regs; |
ea9c271d | 651 | |
4a22f64d | 652 | for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++) |
ad10f812 | 653 | { |
c21236dc | 654 | struct packet_reg *r = ®s[regnum]; |
baef701f | 655 | |
4a22f64d | 656 | if (register_size (gdbarch, regnum) == 0) |
baef701f DJ |
657 | /* Do not try to fetch zero-sized (placeholder) registers. */ |
658 | r->pnum = -1; | |
659 | else | |
660 | r->pnum = gdbarch_remote_register_number (gdbarch, regnum); | |
661 | ||
b323314b | 662 | r->regnum = regnum; |
74ca34ce DJ |
663 | } |
664 | ||
665 | /* Define the g/G packet format as the contents of each register | |
666 | with a remote protocol number, in order of ascending protocol | |
667 | number. */ | |
668 | ||
4a22f64d | 669 | remote_regs = alloca (gdbarch_num_regs (gdbarch) |
c21236dc | 670 | * sizeof (struct packet_reg *)); |
f57d151a | 671 | for (num_remote_regs = 0, regnum = 0; |
4a22f64d | 672 | regnum < gdbarch_num_regs (gdbarch); |
f57d151a | 673 | regnum++) |
c21236dc PA |
674 | if (regs[regnum].pnum != -1) |
675 | remote_regs[num_remote_regs++] = ®s[regnum]; | |
7d58c67d | 676 | |
74ca34ce DJ |
677 | qsort (remote_regs, num_remote_regs, sizeof (struct packet_reg *), |
678 | compare_pnums); | |
679 | ||
680 | for (regnum = 0, offset = 0; regnum < num_remote_regs; regnum++) | |
681 | { | |
682 | remote_regs[regnum]->in_g_packet = 1; | |
683 | remote_regs[regnum]->offset = offset; | |
4a22f64d | 684 | offset += register_size (gdbarch, remote_regs[regnum]->regnum); |
ad10f812 AC |
685 | } |
686 | ||
c21236dc PA |
687 | return offset; |
688 | } | |
689 | ||
690 | /* Given the architecture described by GDBARCH, return the remote | |
691 | protocol register's number and the register's offset in the g/G | |
692 | packets of GDB register REGNUM, in PNUM and POFFSET respectively. | |
693 | If the target does not have a mapping for REGNUM, return false, | |
694 | otherwise, return true. */ | |
695 | ||
696 | int | |
697 | remote_register_number_and_offset (struct gdbarch *gdbarch, int regnum, | |
698 | int *pnum, int *poffset) | |
699 | { | |
700 | int sizeof_g_packet; | |
701 | struct packet_reg *regs; | |
702 | struct cleanup *old_chain; | |
703 | ||
704 | gdb_assert (regnum < gdbarch_num_regs (gdbarch)); | |
705 | ||
706 | regs = xcalloc (gdbarch_num_regs (gdbarch), sizeof (struct packet_reg)); | |
707 | old_chain = make_cleanup (xfree, regs); | |
708 | ||
709 | sizeof_g_packet = map_regcache_remote_table (gdbarch, regs); | |
710 | ||
711 | *pnum = regs[regnum].pnum; | |
712 | *poffset = regs[regnum].offset; | |
713 | ||
714 | do_cleanups (old_chain); | |
715 | ||
716 | return *pnum != -1; | |
717 | } | |
718 | ||
719 | static void * | |
720 | init_remote_state (struct gdbarch *gdbarch) | |
721 | { | |
722 | struct remote_state *rs = get_remote_state_raw (); | |
723 | struct remote_arch_state *rsa; | |
724 | ||
725 | rsa = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct remote_arch_state); | |
726 | ||
727 | /* Use the architecture to build a regnum<->pnum table, which will be | |
728 | 1:1 unless a feature set specifies otherwise. */ | |
729 | rsa->regs = GDBARCH_OBSTACK_CALLOC (gdbarch, | |
730 | gdbarch_num_regs (gdbarch), | |
731 | struct packet_reg); | |
732 | ||
74ca34ce DJ |
733 | /* Record the maximum possible size of the g packet - it may turn out |
734 | to be smaller. */ | |
c21236dc | 735 | rsa->sizeof_g_packet = map_regcache_remote_table (gdbarch, rsa->regs); |
74ca34ce | 736 | |
0df8b418 | 737 | /* Default maximum number of characters in a packet body. Many |
d01949b6 AC |
738 | remote stubs have a hardwired buffer size of 400 bytes |
739 | (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used | |
740 | as the maximum packet-size to ensure that the packet and an extra | |
741 | NUL character can always fit in the buffer. This stops GDB | |
742 | trashing stubs that try to squeeze an extra NUL into what is | |
ea9c271d DJ |
743 | already a full buffer (As of 1999-12-04 that was most stubs). */ |
744 | rsa->remote_packet_size = 400 - 1; | |
d01949b6 | 745 | |
ea9c271d DJ |
746 | /* This one is filled in when a ``g'' packet is received. */ |
747 | rsa->actual_register_packet_size = 0; | |
748 | ||
749 | /* Should rsa->sizeof_g_packet needs more space than the | |
0df8b418 MS |
750 | default, adjust the size accordingly. Remember that each byte is |
751 | encoded as two characters. 32 is the overhead for the packet | |
752 | header / footer. NOTE: cagney/1999-10-26: I suspect that 8 | |
d01949b6 | 753 | (``$NN:G...#NN'') is a better guess, the below has been padded a |
23860348 | 754 | little. */ |
ea9c271d DJ |
755 | if (rsa->sizeof_g_packet > ((rsa->remote_packet_size - 32) / 2)) |
756 | rsa->remote_packet_size = (rsa->sizeof_g_packet * 2 + 32); | |
802188a7 | 757 | |
ea9c271d DJ |
758 | /* Make sure that the packet buffer is plenty big enough for |
759 | this architecture. */ | |
760 | if (rs->buf_size < rsa->remote_packet_size) | |
761 | { | |
762 | rs->buf_size = 2 * rsa->remote_packet_size; | |
7fca722e | 763 | rs->buf = xrealloc (rs->buf, rs->buf_size); |
ea9c271d | 764 | } |
6d820c5c | 765 | |
ea9c271d DJ |
766 | return rsa; |
767 | } | |
768 | ||
769 | /* Return the current allowed size of a remote packet. This is | |
770 | inferred from the current architecture, and should be used to | |
771 | limit the length of outgoing packets. */ | |
772 | static long | |
773 | get_remote_packet_size (void) | |
774 | { | |
be2a5f71 | 775 | struct remote_state *rs = get_remote_state (); |
ea9c271d DJ |
776 | struct remote_arch_state *rsa = get_remote_arch_state (); |
777 | ||
be2a5f71 DJ |
778 | if (rs->explicit_packet_size) |
779 | return rs->explicit_packet_size; | |
780 | ||
ea9c271d | 781 | return rsa->remote_packet_size; |
d01949b6 AC |
782 | } |
783 | ||
ad10f812 | 784 | static struct packet_reg * |
ea9c271d | 785 | packet_reg_from_regnum (struct remote_arch_state *rsa, long regnum) |
ad10f812 | 786 | { |
f5656ead | 787 | if (regnum < 0 && regnum >= gdbarch_num_regs (target_gdbarch ())) |
b323314b AC |
788 | return NULL; |
789 | else | |
ad10f812 | 790 | { |
ea9c271d | 791 | struct packet_reg *r = &rsa->regs[regnum]; |
a744cf53 | 792 | |
b323314b AC |
793 | gdb_assert (r->regnum == regnum); |
794 | return r; | |
ad10f812 | 795 | } |
ad10f812 AC |
796 | } |
797 | ||
798 | static struct packet_reg * | |
ea9c271d | 799 | packet_reg_from_pnum (struct remote_arch_state *rsa, LONGEST pnum) |
ad10f812 | 800 | { |
b323314b | 801 | int i; |
a744cf53 | 802 | |
f5656ead | 803 | for (i = 0; i < gdbarch_num_regs (target_gdbarch ()); i++) |
ad10f812 | 804 | { |
ea9c271d | 805 | struct packet_reg *r = &rsa->regs[i]; |
a744cf53 | 806 | |
b323314b AC |
807 | if (r->pnum == pnum) |
808 | return r; | |
ad10f812 AC |
809 | } |
810 | return NULL; | |
d01949b6 AC |
811 | } |
812 | ||
c906108c SS |
813 | static struct target_ops remote_ops; |
814 | ||
815 | static struct target_ops extended_remote_ops; | |
816 | ||
6426a772 JM |
817 | /* FIXME: cagney/1999-09-23: Even though getpkt was called with |
818 | ``forever'' still use the normal timeout mechanism. This is | |
819 | currently used by the ASYNC code to guarentee that target reads | |
820 | during the initial connect always time-out. Once getpkt has been | |
821 | modified to return a timeout indication and, in turn | |
822 | remote_wait()/wait_for_inferior() have gained a timeout parameter | |
23860348 | 823 | this can go away. */ |
6426a772 JM |
824 | static int wait_forever_enabled_p = 1; |
825 | ||
9a7071a8 JB |
826 | /* Allow the user to specify what sequence to send to the remote |
827 | when he requests a program interruption: Although ^C is usually | |
828 | what remote systems expect (this is the default, here), it is | |
829 | sometimes preferable to send a break. On other systems such | |
830 | as the Linux kernel, a break followed by g, which is Magic SysRq g | |
831 | is required in order to interrupt the execution. */ | |
832 | const char interrupt_sequence_control_c[] = "Ctrl-C"; | |
833 | const char interrupt_sequence_break[] = "BREAK"; | |
834 | const char interrupt_sequence_break_g[] = "BREAK-g"; | |
40478521 | 835 | static const char *const interrupt_sequence_modes[] = |
9a7071a8 JB |
836 | { |
837 | interrupt_sequence_control_c, | |
838 | interrupt_sequence_break, | |
839 | interrupt_sequence_break_g, | |
840 | NULL | |
841 | }; | |
842 | static const char *interrupt_sequence_mode = interrupt_sequence_control_c; | |
843 | ||
844 | static void | |
845 | show_interrupt_sequence (struct ui_file *file, int from_tty, | |
846 | struct cmd_list_element *c, | |
847 | const char *value) | |
848 | { | |
849 | if (interrupt_sequence_mode == interrupt_sequence_control_c) | |
850 | fprintf_filtered (file, | |
851 | _("Send the ASCII ETX character (Ctrl-c) " | |
852 | "to the remote target to interrupt the " | |
853 | "execution of the program.\n")); | |
854 | else if (interrupt_sequence_mode == interrupt_sequence_break) | |
855 | fprintf_filtered (file, | |
856 | _("send a break signal to the remote target " | |
857 | "to interrupt the execution of the program.\n")); | |
858 | else if (interrupt_sequence_mode == interrupt_sequence_break_g) | |
859 | fprintf_filtered (file, | |
860 | _("Send a break signal and 'g' a.k.a. Magic SysRq g to " | |
861 | "the remote target to interrupt the execution " | |
862 | "of Linux kernel.\n")); | |
863 | else | |
864 | internal_error (__FILE__, __LINE__, | |
865 | _("Invalid value for interrupt_sequence_mode: %s."), | |
866 | interrupt_sequence_mode); | |
867 | } | |
6426a772 | 868 | |
9a7071a8 JB |
869 | /* This boolean variable specifies whether interrupt_sequence is sent |
870 | to the remote target when gdb connects to it. | |
871 | This is mostly needed when you debug the Linux kernel: The Linux kernel | |
872 | expects BREAK g which is Magic SysRq g for connecting gdb. */ | |
873 | static int interrupt_on_connect = 0; | |
c906108c | 874 | |
9a7071a8 JB |
875 | /* This variable is used to implement the "set/show remotebreak" commands. |
876 | Since these commands are now deprecated in favor of "set/show remote | |
877 | interrupt-sequence", it no longer has any effect on the code. */ | |
c906108c SS |
878 | static int remote_break; |
879 | ||
9a7071a8 JB |
880 | static void |
881 | set_remotebreak (char *args, int from_tty, struct cmd_list_element *c) | |
882 | { | |
883 | if (remote_break) | |
884 | interrupt_sequence_mode = interrupt_sequence_break; | |
885 | else | |
886 | interrupt_sequence_mode = interrupt_sequence_control_c; | |
887 | } | |
888 | ||
889 | static void | |
890 | show_remotebreak (struct ui_file *file, int from_tty, | |
891 | struct cmd_list_element *c, | |
892 | const char *value) | |
893 | { | |
894 | } | |
895 | ||
c906108c SS |
896 | /* This variable sets the number of bits in an address that are to be |
897 | sent in a memory ("M" or "m") packet. Normally, after stripping | |
0df8b418 | 898 | leading zeros, the entire address would be sent. This variable |
c906108c SS |
899 | restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The |
900 | initial implementation of remote.c restricted the address sent in | |
901 | memory packets to ``host::sizeof long'' bytes - (typically 32 | |
902 | bits). Consequently, for 64 bit targets, the upper 32 bits of an | |
903 | address was never sent. Since fixing this bug may cause a break in | |
904 | some remote targets this variable is principly provided to | |
23860348 | 905 | facilitate backward compatibility. */ |
c906108c | 906 | |
883b9c6c | 907 | static unsigned int remote_address_size; |
c906108c | 908 | |
75c99385 PA |
909 | /* Temporary to track who currently owns the terminal. See |
910 | remote_terminal_* for more details. */ | |
6426a772 JM |
911 | |
912 | static int remote_async_terminal_ours_p; | |
913 | ||
2d717e4f DJ |
914 | /* The executable file to use for "run" on the remote side. */ |
915 | ||
916 | static char *remote_exec_file = ""; | |
917 | ||
11cf8741 | 918 | \f |
11cf8741 | 919 | /* User configurable variables for the number of characters in a |
ea9c271d DJ |
920 | memory read/write packet. MIN (rsa->remote_packet_size, |
921 | rsa->sizeof_g_packet) is the default. Some targets need smaller | |
24b06219 | 922 | values (fifo overruns, et.al.) and some users need larger values |
ad10f812 AC |
923 | (speed up transfers). The variables ``preferred_*'' (the user |
924 | request), ``current_*'' (what was actually set) and ``forced_*'' | |
23860348 | 925 | (Positive - a soft limit, negative - a hard limit). */ |
11cf8741 JM |
926 | |
927 | struct memory_packet_config | |
928 | { | |
929 | char *name; | |
930 | long size; | |
931 | int fixed_p; | |
932 | }; | |
933 | ||
934 | /* Compute the current size of a read/write packet. Since this makes | |
935 | use of ``actual_register_packet_size'' the computation is dynamic. */ | |
936 | ||
937 | static long | |
938 | get_memory_packet_size (struct memory_packet_config *config) | |
939 | { | |
d01949b6 | 940 | struct remote_state *rs = get_remote_state (); |
ea9c271d DJ |
941 | struct remote_arch_state *rsa = get_remote_arch_state (); |
942 | ||
11cf8741 JM |
943 | /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk |
944 | law?) that some hosts don't cope very well with large alloca() | |
945 | calls. Eventually the alloca() code will be replaced by calls to | |
946 | xmalloc() and make_cleanups() allowing this restriction to either | |
23860348 | 947 | be lifted or removed. */ |
11cf8741 JM |
948 | #ifndef MAX_REMOTE_PACKET_SIZE |
949 | #define MAX_REMOTE_PACKET_SIZE 16384 | |
950 | #endif | |
3de11b2e | 951 | /* NOTE: 20 ensures we can write at least one byte. */ |
11cf8741 | 952 | #ifndef MIN_REMOTE_PACKET_SIZE |
3de11b2e | 953 | #define MIN_REMOTE_PACKET_SIZE 20 |
11cf8741 JM |
954 | #endif |
955 | long what_they_get; | |
956 | if (config->fixed_p) | |
957 | { | |
958 | if (config->size <= 0) | |
959 | what_they_get = MAX_REMOTE_PACKET_SIZE; | |
960 | else | |
961 | what_they_get = config->size; | |
962 | } | |
963 | else | |
964 | { | |
ea9c271d | 965 | what_they_get = get_remote_packet_size (); |
23860348 | 966 | /* Limit the packet to the size specified by the user. */ |
11cf8741 JM |
967 | if (config->size > 0 |
968 | && what_they_get > config->size) | |
969 | what_they_get = config->size; | |
be2a5f71 DJ |
970 | |
971 | /* Limit it to the size of the targets ``g'' response unless we have | |
972 | permission from the stub to use a larger packet size. */ | |
973 | if (rs->explicit_packet_size == 0 | |
974 | && rsa->actual_register_packet_size > 0 | |
975 | && what_they_get > rsa->actual_register_packet_size) | |
976 | what_they_get = rsa->actual_register_packet_size; | |
11cf8741 JM |
977 | } |
978 | if (what_they_get > MAX_REMOTE_PACKET_SIZE) | |
979 | what_they_get = MAX_REMOTE_PACKET_SIZE; | |
980 | if (what_they_get < MIN_REMOTE_PACKET_SIZE) | |
981 | what_they_get = MIN_REMOTE_PACKET_SIZE; | |
6d820c5c DJ |
982 | |
983 | /* Make sure there is room in the global buffer for this packet | |
984 | (including its trailing NUL byte). */ | |
985 | if (rs->buf_size < what_they_get + 1) | |
986 | { | |
987 | rs->buf_size = 2 * what_they_get; | |
988 | rs->buf = xrealloc (rs->buf, 2 * what_they_get); | |
989 | } | |
990 | ||
11cf8741 JM |
991 | return what_they_get; |
992 | } | |
993 | ||
0df8b418 | 994 | /* Update the size of a read/write packet. If they user wants |
23860348 | 995 | something really big then do a sanity check. */ |
11cf8741 JM |
996 | |
997 | static void | |
998 | set_memory_packet_size (char *args, struct memory_packet_config *config) | |
999 | { | |
1000 | int fixed_p = config->fixed_p; | |
1001 | long size = config->size; | |
a744cf53 | 1002 | |
11cf8741 | 1003 | if (args == NULL) |
8a3fe4f8 | 1004 | error (_("Argument required (integer, `fixed' or `limited').")); |
11cf8741 JM |
1005 | else if (strcmp (args, "hard") == 0 |
1006 | || strcmp (args, "fixed") == 0) | |
1007 | fixed_p = 1; | |
1008 | else if (strcmp (args, "soft") == 0 | |
1009 | || strcmp (args, "limit") == 0) | |
1010 | fixed_p = 0; | |
1011 | else | |
1012 | { | |
1013 | char *end; | |
a744cf53 | 1014 | |
11cf8741 JM |
1015 | size = strtoul (args, &end, 0); |
1016 | if (args == end) | |
8a3fe4f8 | 1017 | error (_("Invalid %s (bad syntax)."), config->name); |
11cf8741 JM |
1018 | #if 0 |
1019 | /* Instead of explicitly capping the size of a packet to | |
1020 | MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is | |
1021 | instead allowed to set the size to something arbitrarily | |
23860348 | 1022 | large. */ |
11cf8741 | 1023 | if (size > MAX_REMOTE_PACKET_SIZE) |
8a3fe4f8 | 1024 | error (_("Invalid %s (too large)."), config->name); |
11cf8741 JM |
1025 | #endif |
1026 | } | |
23860348 | 1027 | /* Extra checks? */ |
11cf8741 JM |
1028 | if (fixed_p && !config->fixed_p) |
1029 | { | |
e2e0b3e5 AC |
1030 | if (! query (_("The target may not be able to correctly handle a %s\n" |
1031 | "of %ld bytes. Change the packet size? "), | |
11cf8741 | 1032 | config->name, size)) |
8a3fe4f8 | 1033 | error (_("Packet size not changed.")); |
11cf8741 | 1034 | } |
23860348 | 1035 | /* Update the config. */ |
11cf8741 JM |
1036 | config->fixed_p = fixed_p; |
1037 | config->size = size; | |
1038 | } | |
1039 | ||
1040 | static void | |
1041 | show_memory_packet_size (struct memory_packet_config *config) | |
1042 | { | |
a3f17187 | 1043 | printf_filtered (_("The %s is %ld. "), config->name, config->size); |
11cf8741 | 1044 | if (config->fixed_p) |
a3f17187 | 1045 | printf_filtered (_("Packets are fixed at %ld bytes.\n"), |
11cf8741 JM |
1046 | get_memory_packet_size (config)); |
1047 | else | |
a3f17187 | 1048 | printf_filtered (_("Packets are limited to %ld bytes.\n"), |
11cf8741 JM |
1049 | get_memory_packet_size (config)); |
1050 | } | |
1051 | ||
1052 | static struct memory_packet_config memory_write_packet_config = | |
1053 | { | |
1054 | "memory-write-packet-size", | |
1055 | }; | |
1056 | ||
1057 | static void | |
1058 | set_memory_write_packet_size (char *args, int from_tty) | |
1059 | { | |
1060 | set_memory_packet_size (args, &memory_write_packet_config); | |
1061 | } | |
1062 | ||
1063 | static void | |
1064 | show_memory_write_packet_size (char *args, int from_tty) | |
1065 | { | |
1066 | show_memory_packet_size (&memory_write_packet_config); | |
1067 | } | |
1068 | ||
1069 | static long | |
1070 | get_memory_write_packet_size (void) | |
1071 | { | |
1072 | return get_memory_packet_size (&memory_write_packet_config); | |
1073 | } | |
1074 | ||
1075 | static struct memory_packet_config memory_read_packet_config = | |
1076 | { | |
1077 | "memory-read-packet-size", | |
1078 | }; | |
1079 | ||
1080 | static void | |
1081 | set_memory_read_packet_size (char *args, int from_tty) | |
1082 | { | |
1083 | set_memory_packet_size (args, &memory_read_packet_config); | |
1084 | } | |
1085 | ||
1086 | static void | |
1087 | show_memory_read_packet_size (char *args, int from_tty) | |
1088 | { | |
1089 | show_memory_packet_size (&memory_read_packet_config); | |
1090 | } | |
1091 | ||
1092 | static long | |
1093 | get_memory_read_packet_size (void) | |
1094 | { | |
1095 | long size = get_memory_packet_size (&memory_read_packet_config); | |
a744cf53 | 1096 | |
11cf8741 JM |
1097 | /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an |
1098 | extra buffer size argument before the memory read size can be | |
ea9c271d DJ |
1099 | increased beyond this. */ |
1100 | if (size > get_remote_packet_size ()) | |
1101 | size = get_remote_packet_size (); | |
11cf8741 JM |
1102 | return size; |
1103 | } | |
1104 | ||
11cf8741 | 1105 | \f |
5a2468f5 | 1106 | /* Generic configuration support for packets the stub optionally |
0df8b418 | 1107 | supports. Allows the user to specify the use of the packet as well |
23860348 | 1108 | as allowing GDB to auto-detect support in the remote stub. */ |
5a2468f5 JM |
1109 | |
1110 | enum packet_support | |
1111 | { | |
1112 | PACKET_SUPPORT_UNKNOWN = 0, | |
1113 | PACKET_ENABLE, | |
1114 | PACKET_DISABLE | |
1115 | }; | |
1116 | ||
5a2468f5 JM |
1117 | struct packet_config |
1118 | { | |
bb572ddd DJ |
1119 | const char *name; |
1120 | const char *title; | |
7f19b9a2 | 1121 | enum auto_boolean detect; |
5a2468f5 JM |
1122 | enum packet_support support; |
1123 | }; | |
1124 | ||
d471ea57 | 1125 | /* Analyze a packet's return value and update the packet config |
23860348 | 1126 | accordingly. */ |
d471ea57 AC |
1127 | |
1128 | enum packet_result | |
1129 | { | |
1130 | PACKET_ERROR, | |
1131 | PACKET_OK, | |
1132 | PACKET_UNKNOWN | |
1133 | }; | |
1134 | ||
5a2468f5 | 1135 | static void |
d471ea57 | 1136 | update_packet_config (struct packet_config *config) |
5a2468f5 | 1137 | { |
d471ea57 AC |
1138 | switch (config->detect) |
1139 | { | |
7f19b9a2 | 1140 | case AUTO_BOOLEAN_TRUE: |
d471ea57 AC |
1141 | config->support = PACKET_ENABLE; |
1142 | break; | |
7f19b9a2 | 1143 | case AUTO_BOOLEAN_FALSE: |
d471ea57 AC |
1144 | config->support = PACKET_DISABLE; |
1145 | break; | |
7f19b9a2 | 1146 | case AUTO_BOOLEAN_AUTO: |
d471ea57 AC |
1147 | config->support = PACKET_SUPPORT_UNKNOWN; |
1148 | break; | |
1149 | } | |
5a2468f5 JM |
1150 | } |
1151 | ||
1152 | static void | |
fba45db2 | 1153 | show_packet_config_cmd (struct packet_config *config) |
5a2468f5 JM |
1154 | { |
1155 | char *support = "internal-error"; | |
a744cf53 | 1156 | |
5a2468f5 JM |
1157 | switch (config->support) |
1158 | { | |
1159 | case PACKET_ENABLE: | |
1160 | support = "enabled"; | |
1161 | break; | |
1162 | case PACKET_DISABLE: | |
1163 | support = "disabled"; | |
1164 | break; | |
1165 | case PACKET_SUPPORT_UNKNOWN: | |
1166 | support = "unknown"; | |
1167 | break; | |
1168 | } | |
1169 | switch (config->detect) | |
1170 | { | |
7f19b9a2 | 1171 | case AUTO_BOOLEAN_AUTO: |
3e43a32a MS |
1172 | printf_filtered (_("Support for the `%s' packet " |
1173 | "is auto-detected, currently %s.\n"), | |
37a105a1 | 1174 | config->name, support); |
5a2468f5 | 1175 | break; |
7f19b9a2 AC |
1176 | case AUTO_BOOLEAN_TRUE: |
1177 | case AUTO_BOOLEAN_FALSE: | |
37a105a1 DJ |
1178 | printf_filtered (_("Support for the `%s' packet is currently %s.\n"), |
1179 | config->name, support); | |
8e248173 | 1180 | break; |
5a2468f5 JM |
1181 | } |
1182 | } | |
1183 | ||
1184 | static void | |
bb572ddd DJ |
1185 | add_packet_config_cmd (struct packet_config *config, const char *name, |
1186 | const char *title, int legacy) | |
d471ea57 | 1187 | { |
5a2468f5 JM |
1188 | char *set_doc; |
1189 | char *show_doc; | |
d471ea57 | 1190 | char *cmd_name; |
3ed07be4 | 1191 | |
5a2468f5 JM |
1192 | config->name = name; |
1193 | config->title = title; | |
7f19b9a2 | 1194 | config->detect = AUTO_BOOLEAN_AUTO; |
8e248173 | 1195 | config->support = PACKET_SUPPORT_UNKNOWN; |
b435e160 AC |
1196 | set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet", |
1197 | name, title); | |
3e43a32a MS |
1198 | show_doc = xstrprintf ("Show current use of remote " |
1199 | "protocol `%s' (%s) packet", | |
b435e160 | 1200 | name, title); |
d471ea57 | 1201 | /* set/show TITLE-packet {auto,on,off} */ |
b435e160 | 1202 | cmd_name = xstrprintf ("%s-packet", title); |
e9e68a56 | 1203 | add_setshow_auto_boolean_cmd (cmd_name, class_obscure, |
3e43a32a MS |
1204 | &config->detect, set_doc, |
1205 | show_doc, NULL, /* help_doc */ | |
bb572ddd DJ |
1206 | set_remote_protocol_packet_cmd, |
1207 | show_remote_protocol_packet_cmd, | |
1208 | &remote_set_cmdlist, &remote_show_cmdlist); | |
1eefb858 TT |
1209 | /* The command code copies the documentation strings. */ |
1210 | xfree (set_doc); | |
1211 | xfree (show_doc); | |
23860348 | 1212 | /* set/show remote NAME-packet {auto,on,off} -- legacy. */ |
d471ea57 AC |
1213 | if (legacy) |
1214 | { | |
1215 | char *legacy_name; | |
a744cf53 | 1216 | |
b435e160 | 1217 | legacy_name = xstrprintf ("%s-packet", name); |
d471ea57 | 1218 | add_alias_cmd (legacy_name, cmd_name, class_obscure, 0, |
bb572ddd | 1219 | &remote_set_cmdlist); |
d471ea57 | 1220 | add_alias_cmd (legacy_name, cmd_name, class_obscure, 0, |
bb572ddd | 1221 | &remote_show_cmdlist); |
d471ea57 | 1222 | } |
5a2468f5 JM |
1223 | } |
1224 | ||
d471ea57 | 1225 | static enum packet_result |
a76d924d | 1226 | packet_check_result (const char *buf) |
5a2468f5 | 1227 | { |
d471ea57 | 1228 | if (buf[0] != '\0') |
5a2468f5 | 1229 | { |
d471ea57 | 1230 | /* The stub recognized the packet request. Check that the |
23860348 | 1231 | operation succeeded. */ |
a76d924d DJ |
1232 | if (buf[0] == 'E' |
1233 | && isxdigit (buf[1]) && isxdigit (buf[2]) | |
1234 | && buf[3] == '\0') | |
1235 | /* "Enn" - definitly an error. */ | |
1236 | return PACKET_ERROR; | |
1237 | ||
1238 | /* Always treat "E." as an error. This will be used for | |
1239 | more verbose error messages, such as E.memtypes. */ | |
1240 | if (buf[0] == 'E' && buf[1] == '.') | |
1241 | return PACKET_ERROR; | |
1242 | ||
1243 | /* The packet may or may not be OK. Just assume it is. */ | |
1244 | return PACKET_OK; | |
1245 | } | |
1246 | else | |
1247 | /* The stub does not support the packet. */ | |
1248 | return PACKET_UNKNOWN; | |
1249 | } | |
1250 | ||
1251 | static enum packet_result | |
1252 | packet_ok (const char *buf, struct packet_config *config) | |
1253 | { | |
1254 | enum packet_result result; | |
1255 | ||
1256 | result = packet_check_result (buf); | |
1257 | switch (result) | |
1258 | { | |
1259 | case PACKET_OK: | |
1260 | case PACKET_ERROR: | |
1261 | /* The stub recognized the packet request. */ | |
d471ea57 AC |
1262 | switch (config->support) |
1263 | { | |
1264 | case PACKET_SUPPORT_UNKNOWN: | |
1265 | if (remote_debug) | |
1266 | fprintf_unfiltered (gdb_stdlog, | |
1267 | "Packet %s (%s) is supported\n", | |
1268 | config->name, config->title); | |
1269 | config->support = PACKET_ENABLE; | |
1270 | break; | |
1271 | case PACKET_DISABLE: | |
8e65ff28 | 1272 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 1273 | _("packet_ok: attempt to use a disabled packet")); |
d471ea57 AC |
1274 | break; |
1275 | case PACKET_ENABLE: | |
1276 | break; | |
1277 | } | |
a76d924d DJ |
1278 | break; |
1279 | case PACKET_UNKNOWN: | |
23860348 | 1280 | /* The stub does not support the packet. */ |
d471ea57 AC |
1281 | switch (config->support) |
1282 | { | |
1283 | case PACKET_ENABLE: | |
7f19b9a2 | 1284 | if (config->detect == AUTO_BOOLEAN_AUTO) |
d471ea57 | 1285 | /* If the stub previously indicated that the packet was |
23860348 | 1286 | supported then there is a protocol error.. */ |
8a3fe4f8 | 1287 | error (_("Protocol error: %s (%s) conflicting enabled responses."), |
d471ea57 AC |
1288 | config->name, config->title); |
1289 | else | |
23860348 | 1290 | /* The user set it wrong. */ |
8a3fe4f8 | 1291 | error (_("Enabled packet %s (%s) not recognized by stub"), |
d471ea57 AC |
1292 | config->name, config->title); |
1293 | break; | |
1294 | case PACKET_SUPPORT_UNKNOWN: | |
1295 | if (remote_debug) | |
1296 | fprintf_unfiltered (gdb_stdlog, | |
1297 | "Packet %s (%s) is NOT supported\n", | |
1298 | config->name, config->title); | |
1299 | config->support = PACKET_DISABLE; | |
1300 | break; | |
1301 | case PACKET_DISABLE: | |
1302 | break; | |
1303 | } | |
a76d924d | 1304 | break; |
5a2468f5 | 1305 | } |
a76d924d DJ |
1306 | |
1307 | return result; | |
5a2468f5 JM |
1308 | } |
1309 | ||
444abaca DJ |
1310 | enum { |
1311 | PACKET_vCont = 0, | |
1312 | PACKET_X, | |
1313 | PACKET_qSymbol, | |
1314 | PACKET_P, | |
1315 | PACKET_p, | |
1316 | PACKET_Z0, | |
1317 | PACKET_Z1, | |
1318 | PACKET_Z2, | |
1319 | PACKET_Z3, | |
1320 | PACKET_Z4, | |
a6b151f1 DJ |
1321 | PACKET_vFile_open, |
1322 | PACKET_vFile_pread, | |
1323 | PACKET_vFile_pwrite, | |
1324 | PACKET_vFile_close, | |
1325 | PACKET_vFile_unlink, | |
b9e7b9c3 | 1326 | PACKET_vFile_readlink, |
0876f84a | 1327 | PACKET_qXfer_auxv, |
23181151 | 1328 | PACKET_qXfer_features, |
cfa9d6d9 | 1329 | PACKET_qXfer_libraries, |
2268b414 | 1330 | PACKET_qXfer_libraries_svr4, |
fd79ecee | 1331 | PACKET_qXfer_memory_map, |
0e7f50da UW |
1332 | PACKET_qXfer_spu_read, |
1333 | PACKET_qXfer_spu_write, | |
07e059b5 | 1334 | PACKET_qXfer_osdata, |
dc146f7c | 1335 | PACKET_qXfer_threads, |
0fb4aa4b | 1336 | PACKET_qXfer_statictrace_read, |
b3b9301e | 1337 | PACKET_qXfer_traceframe_info, |
169081d0 | 1338 | PACKET_qXfer_uib, |
711e434b | 1339 | PACKET_qGetTIBAddr, |
444abaca | 1340 | PACKET_qGetTLSAddr, |
be2a5f71 | 1341 | PACKET_qSupported, |
bd3eecc3 | 1342 | PACKET_qTStatus, |
89be2091 | 1343 | PACKET_QPassSignals, |
9b224c5e | 1344 | PACKET_QProgramSignals, |
08388c79 | 1345 | PACKET_qSearch_memory, |
2d717e4f DJ |
1346 | PACKET_vAttach, |
1347 | PACKET_vRun, | |
a6f3e723 | 1348 | PACKET_QStartNoAckMode, |
82f73884 | 1349 | PACKET_vKill, |
4aa995e1 PA |
1350 | PACKET_qXfer_siginfo_read, |
1351 | PACKET_qXfer_siginfo_write, | |
0b16c5cf | 1352 | PACKET_qAttached, |
782b2b07 | 1353 | PACKET_ConditionalTracepoints, |
3788aec7 | 1354 | PACKET_ConditionalBreakpoints, |
d3ce09f5 | 1355 | PACKET_BreakpointCommands, |
7a697b8d | 1356 | PACKET_FastTracepoints, |
0fb4aa4b | 1357 | PACKET_StaticTracepoints, |
1e4d1764 | 1358 | PACKET_InstallInTrace, |
40ab02ce MS |
1359 | PACKET_bc, |
1360 | PACKET_bs, | |
409873ef | 1361 | PACKET_TracepointSource, |
d914c394 | 1362 | PACKET_QAllow, |
78d85199 | 1363 | PACKET_qXfer_fdpic, |
03583c20 | 1364 | PACKET_QDisableRandomization, |
d1feda86 | 1365 | PACKET_QAgent, |
f6f899bf | 1366 | PACKET_QTBuffer_size, |
9accd112 MM |
1367 | PACKET_Qbtrace_off, |
1368 | PACKET_Qbtrace_bts, | |
1369 | PACKET_qXfer_btrace, | |
444abaca DJ |
1370 | PACKET_MAX |
1371 | }; | |
506fb367 | 1372 | |
444abaca | 1373 | static struct packet_config remote_protocol_packets[PACKET_MAX]; |
dc8acb97 MS |
1374 | |
1375 | static void | |
444abaca DJ |
1376 | set_remote_protocol_packet_cmd (char *args, int from_tty, |
1377 | struct cmd_list_element *c) | |
dc8acb97 | 1378 | { |
444abaca | 1379 | struct packet_config *packet; |
dc8acb97 | 1380 | |
444abaca DJ |
1381 | for (packet = remote_protocol_packets; |
1382 | packet < &remote_protocol_packets[PACKET_MAX]; | |
1383 | packet++) | |
1384 | { | |
1385 | if (&packet->detect == c->var) | |
1386 | { | |
1387 | update_packet_config (packet); | |
1388 | return; | |
1389 | } | |
1390 | } | |
9b20d036 | 1391 | internal_error (__FILE__, __LINE__, _("Could not find config for %s"), |
444abaca | 1392 | c->name); |
dc8acb97 MS |
1393 | } |
1394 | ||
5a2468f5 | 1395 | static void |
444abaca DJ |
1396 | show_remote_protocol_packet_cmd (struct ui_file *file, int from_tty, |
1397 | struct cmd_list_element *c, | |
1398 | const char *value) | |
5a2468f5 | 1399 | { |
444abaca | 1400 | struct packet_config *packet; |
5a2468f5 | 1401 | |
444abaca DJ |
1402 | for (packet = remote_protocol_packets; |
1403 | packet < &remote_protocol_packets[PACKET_MAX]; | |
1404 | packet++) | |
1405 | { | |
1406 | if (&packet->detect == c->var) | |
1407 | { | |
1408 | show_packet_config_cmd (packet); | |
1409 | return; | |
1410 | } | |
1411 | } | |
9b20d036 | 1412 | internal_error (__FILE__, __LINE__, _("Could not find config for %s"), |
444abaca | 1413 | c->name); |
5a2468f5 JM |
1414 | } |
1415 | ||
d471ea57 AC |
1416 | /* Should we try one of the 'Z' requests? */ |
1417 | ||
1418 | enum Z_packet_type | |
1419 | { | |
1420 | Z_PACKET_SOFTWARE_BP, | |
1421 | Z_PACKET_HARDWARE_BP, | |
1422 | Z_PACKET_WRITE_WP, | |
1423 | Z_PACKET_READ_WP, | |
1424 | Z_PACKET_ACCESS_WP, | |
1425 | NR_Z_PACKET_TYPES | |
1426 | }; | |
96baa820 | 1427 | |
d471ea57 | 1428 | /* For compatibility with older distributions. Provide a ``set remote |
23860348 | 1429 | Z-packet ...'' command that updates all the Z packet types. */ |
d471ea57 | 1430 | |
7f19b9a2 | 1431 | static enum auto_boolean remote_Z_packet_detect; |
96baa820 JM |
1432 | |
1433 | static void | |
fba45db2 KB |
1434 | set_remote_protocol_Z_packet_cmd (char *args, int from_tty, |
1435 | struct cmd_list_element *c) | |
96baa820 | 1436 | { |
d471ea57 | 1437 | int i; |
a744cf53 | 1438 | |
d471ea57 AC |
1439 | for (i = 0; i < NR_Z_PACKET_TYPES; i++) |
1440 | { | |
444abaca DJ |
1441 | remote_protocol_packets[PACKET_Z0 + i].detect = remote_Z_packet_detect; |
1442 | update_packet_config (&remote_protocol_packets[PACKET_Z0 + i]); | |
d471ea57 | 1443 | } |
96baa820 JM |
1444 | } |
1445 | ||
1446 | static void | |
08546159 AC |
1447 | show_remote_protocol_Z_packet_cmd (struct ui_file *file, int from_tty, |
1448 | struct cmd_list_element *c, | |
1449 | const char *value) | |
96baa820 | 1450 | { |
d471ea57 | 1451 | int i; |
a744cf53 | 1452 | |
d471ea57 AC |
1453 | for (i = 0; i < NR_Z_PACKET_TYPES; i++) |
1454 | { | |
444abaca | 1455 | show_packet_config_cmd (&remote_protocol_packets[PACKET_Z0 + i]); |
d471ea57 | 1456 | } |
96baa820 JM |
1457 | } |
1458 | ||
23860348 | 1459 | /* Tokens for use by the asynchronous signal handlers for SIGINT. */ |
934b9bac JK |
1460 | static struct async_signal_handler *async_sigint_remote_twice_token; |
1461 | static struct async_signal_handler *async_sigint_remote_token; | |
43ff13b4 | 1462 | |
74531fed PA |
1463 | \f |
1464 | /* Asynchronous signal handle registered as event loop source for | |
1465 | when we have pending events ready to be passed to the core. */ | |
1466 | ||
1467 | static struct async_event_handler *remote_async_inferior_event_token; | |
1468 | ||
c906108c SS |
1469 | \f |
1470 | ||
79d7f229 PA |
1471 | static ptid_t magic_null_ptid; |
1472 | static ptid_t not_sent_ptid; | |
1473 | static ptid_t any_thread_ptid; | |
1474 | ||
0b16c5cf PA |
1475 | /* Find out if the stub attached to PID (and hence GDB should offer to |
1476 | detach instead of killing it when bailing out). */ | |
1477 | ||
1478 | static int | |
1479 | remote_query_attached (int pid) | |
1480 | { | |
1481 | struct remote_state *rs = get_remote_state (); | |
bba74b36 | 1482 | size_t size = get_remote_packet_size (); |
0b16c5cf PA |
1483 | |
1484 | if (remote_protocol_packets[PACKET_qAttached].support == PACKET_DISABLE) | |
1485 | return 0; | |
1486 | ||
1487 | if (remote_multi_process_p (rs)) | |
bba74b36 | 1488 | xsnprintf (rs->buf, size, "qAttached:%x", pid); |
0b16c5cf | 1489 | else |
bba74b36 | 1490 | xsnprintf (rs->buf, size, "qAttached"); |
0b16c5cf PA |
1491 | |
1492 | putpkt (rs->buf); | |
1493 | getpkt (&rs->buf, &rs->buf_size, 0); | |
1494 | ||
1495 | switch (packet_ok (rs->buf, | |
1554e9be | 1496 | &remote_protocol_packets[PACKET_qAttached])) |
0b16c5cf PA |
1497 | { |
1498 | case PACKET_OK: | |
1499 | if (strcmp (rs->buf, "1") == 0) | |
1500 | return 1; | |
1501 | break; | |
1502 | case PACKET_ERROR: | |
1503 | warning (_("Remote failure reply: %s"), rs->buf); | |
1504 | break; | |
1505 | case PACKET_UNKNOWN: | |
1506 | break; | |
1507 | } | |
1508 | ||
1509 | return 0; | |
1510 | } | |
1511 | ||
49c62f2e PA |
1512 | /* Add PID to GDB's inferior table. If FAKE_PID_P is true, then PID |
1513 | has been invented by GDB, instead of reported by the target. Since | |
1514 | we can be connected to a remote system before before knowing about | |
1515 | any inferior, mark the target with execution when we find the first | |
1516 | inferior. If ATTACHED is 1, then we had just attached to this | |
1517 | inferior. If it is 0, then we just created this inferior. If it | |
1518 | is -1, then try querying the remote stub to find out if it had | |
1519 | attached to the inferior or not. */ | |
1941c569 PA |
1520 | |
1521 | static struct inferior * | |
49c62f2e | 1522 | remote_add_inferior (int fake_pid_p, int pid, int attached) |
1941c569 | 1523 | { |
1941c569 PA |
1524 | struct inferior *inf; |
1525 | ||
0b16c5cf PA |
1526 | /* Check whether this process we're learning about is to be |
1527 | considered attached, or if is to be considered to have been | |
1528 | spawned by the stub. */ | |
1529 | if (attached == -1) | |
1530 | attached = remote_query_attached (pid); | |
1531 | ||
f5656ead | 1532 | if (gdbarch_has_global_solist (target_gdbarch ())) |
6c95b8df PA |
1533 | { |
1534 | /* If the target shares code across all inferiors, then every | |
1535 | attach adds a new inferior. */ | |
1536 | inf = add_inferior (pid); | |
1537 | ||
1538 | /* ... and every inferior is bound to the same program space. | |
1539 | However, each inferior may still have its own address | |
1540 | space. */ | |
1541 | inf->aspace = maybe_new_address_space (); | |
1542 | inf->pspace = current_program_space; | |
1543 | } | |
1544 | else | |
1545 | { | |
1546 | /* In the traditional debugging scenario, there's a 1-1 match | |
1547 | between program/address spaces. We simply bind the inferior | |
1548 | to the program space's address space. */ | |
1549 | inf = current_inferior (); | |
1550 | inferior_appeared (inf, pid); | |
1551 | } | |
1941c569 | 1552 | |
0b16c5cf | 1553 | inf->attach_flag = attached; |
49c62f2e | 1554 | inf->fake_pid_p = fake_pid_p; |
0b16c5cf | 1555 | |
1941c569 PA |
1556 | return inf; |
1557 | } | |
1558 | ||
1559 | /* Add thread PTID to GDB's thread list. Tag it as executing/running | |
1560 | according to RUNNING. */ | |
1561 | ||
c906108c | 1562 | static void |
1941c569 | 1563 | remote_add_thread (ptid_t ptid, int running) |
c906108c | 1564 | { |
1941c569 PA |
1565 | add_thread (ptid); |
1566 | ||
1567 | set_executing (ptid, running); | |
1568 | set_running (ptid, running); | |
1569 | } | |
1570 | ||
1571 | /* Come here when we learn about a thread id from the remote target. | |
1572 | It may be the first time we hear about such thread, so take the | |
1573 | opportunity to add it to GDB's thread list. In case this is the | |
1574 | first time we're noticing its corresponding inferior, add it to | |
1575 | GDB's inferior list as well. */ | |
1576 | ||
1577 | static void | |
1578 | remote_notice_new_inferior (ptid_t currthread, int running) | |
1579 | { | |
c906108c SS |
1580 | /* If this is a new thread, add it to GDB's thread list. |
1581 | If we leave it up to WFI to do this, bad things will happen. */ | |
82f73884 PA |
1582 | |
1583 | if (in_thread_list (currthread) && is_exited (currthread)) | |
1584 | { | |
1585 | /* We're seeing an event on a thread id we knew had exited. | |
1586 | This has to be a new thread reusing the old id. Add it. */ | |
1941c569 | 1587 | remote_add_thread (currthread, running); |
82f73884 PA |
1588 | return; |
1589 | } | |
1590 | ||
79d7f229 | 1591 | if (!in_thread_list (currthread)) |
c0a2216e | 1592 | { |
1941c569 | 1593 | struct inferior *inf = NULL; |
bad34192 | 1594 | int pid = ptid_get_pid (currthread); |
1941c569 | 1595 | |
bad34192 PA |
1596 | if (ptid_is_pid (inferior_ptid) |
1597 | && pid == ptid_get_pid (inferior_ptid)) | |
c0a2216e PA |
1598 | { |
1599 | /* inferior_ptid has no thread member yet. This can happen | |
1600 | with the vAttach -> remote_wait,"TAAthread:" path if the | |
1601 | stub doesn't support qC. This is the first stop reported | |
1602 | after an attach, so this is the main thread. Update the | |
1603 | ptid in the thread list. */ | |
bad34192 PA |
1604 | if (in_thread_list (pid_to_ptid (pid))) |
1605 | thread_change_ptid (inferior_ptid, currthread); | |
1606 | else | |
1607 | { | |
1608 | remote_add_thread (currthread, running); | |
1609 | inferior_ptid = currthread; | |
1610 | } | |
dc146f7c | 1611 | return; |
c0a2216e | 1612 | } |
82f73884 PA |
1613 | |
1614 | if (ptid_equal (magic_null_ptid, inferior_ptid)) | |
c0a2216e PA |
1615 | { |
1616 | /* inferior_ptid is not set yet. This can happen with the | |
1617 | vRun -> remote_wait,"TAAthread:" path if the stub | |
1618 | doesn't support qC. This is the first stop reported | |
1619 | after an attach, so this is the main thread. Update the | |
1620 | ptid in the thread list. */ | |
dc146f7c | 1621 | thread_change_ptid (inferior_ptid, currthread); |
82f73884 | 1622 | return; |
c0a2216e | 1623 | } |
82f73884 | 1624 | |
29c87f7f PA |
1625 | /* When connecting to a target remote, or to a target |
1626 | extended-remote which already was debugging an inferior, we | |
1627 | may not know about it yet. Add it before adding its child | |
1628 | thread, so notifications are emitted in a sensible order. */ | |
1629 | if (!in_inferior_list (ptid_get_pid (currthread))) | |
49c62f2e PA |
1630 | { |
1631 | struct remote_state *rs = get_remote_state (); | |
1632 | int fake_pid_p = !remote_multi_process_p (rs); | |
1633 | ||
1634 | inf = remote_add_inferior (fake_pid_p, | |
1635 | ptid_get_pid (currthread), -1); | |
1636 | } | |
29c87f7f | 1637 | |
82f73884 | 1638 | /* This is really a new thread. Add it. */ |
1941c569 PA |
1639 | remote_add_thread (currthread, running); |
1640 | ||
1641 | /* If we found a new inferior, let the common code do whatever | |
1642 | it needs to with it (e.g., read shared libraries, insert | |
1643 | breakpoints). */ | |
1644 | if (inf != NULL) | |
1645 | notice_new_inferior (currthread, running, 0); | |
c0a2216e | 1646 | } |
c906108c SS |
1647 | } |
1648 | ||
dc146f7c VP |
1649 | /* Return the private thread data, creating it if necessary. */ |
1650 | ||
70221824 | 1651 | static struct private_thread_info * |
dc146f7c VP |
1652 | demand_private_info (ptid_t ptid) |
1653 | { | |
1654 | struct thread_info *info = find_thread_ptid (ptid); | |
1655 | ||
1656 | gdb_assert (info); | |
1657 | ||
1658 | if (!info->private) | |
1659 | { | |
1660 | info->private = xmalloc (sizeof (*(info->private))); | |
1661 | info->private_dtor = free_private_thread_info; | |
1662 | info->private->core = -1; | |
1663 | info->private->extra = 0; | |
1664 | } | |
1665 | ||
1666 | return info->private; | |
1667 | } | |
1668 | ||
74531fed PA |
1669 | /* Call this function as a result of |
1670 | 1) A halt indication (T packet) containing a thread id | |
1671 | 2) A direct query of currthread | |
0df8b418 | 1672 | 3) Successful execution of set thread */ |
74531fed PA |
1673 | |
1674 | static void | |
47f8a51d | 1675 | record_currthread (struct remote_state *rs, ptid_t currthread) |
74531fed | 1676 | { |
47f8a51d | 1677 | rs->general_thread = currthread; |
74531fed PA |
1678 | } |
1679 | ||
89be2091 DJ |
1680 | /* If 'QPassSignals' is supported, tell the remote stub what signals |
1681 | it can simply pass through to the inferior without reporting. */ | |
1682 | ||
1683 | static void | |
2455069d | 1684 | remote_pass_signals (int numsigs, unsigned char *pass_signals) |
89be2091 DJ |
1685 | { |
1686 | if (remote_protocol_packets[PACKET_QPassSignals].support != PACKET_DISABLE) | |
1687 | { | |
1688 | char *pass_packet, *p; | |
89be2091 | 1689 | int count = 0, i; |
747dc59d | 1690 | struct remote_state *rs = get_remote_state (); |
89be2091 DJ |
1691 | |
1692 | gdb_assert (numsigs < 256); | |
1693 | for (i = 0; i < numsigs; i++) | |
1694 | { | |
2455069d | 1695 | if (pass_signals[i]) |
89be2091 DJ |
1696 | count++; |
1697 | } | |
1698 | pass_packet = xmalloc (count * 3 + strlen ("QPassSignals:") + 1); | |
1699 | strcpy (pass_packet, "QPassSignals:"); | |
1700 | p = pass_packet + strlen (pass_packet); | |
1701 | for (i = 0; i < numsigs; i++) | |
1702 | { | |
2455069d | 1703 | if (pass_signals[i]) |
89be2091 DJ |
1704 | { |
1705 | if (i >= 16) | |
1706 | *p++ = tohex (i >> 4); | |
1707 | *p++ = tohex (i & 15); | |
1708 | if (count) | |
1709 | *p++ = ';'; | |
1710 | else | |
1711 | break; | |
1712 | count--; | |
1713 | } | |
1714 | } | |
1715 | *p = 0; | |
747dc59d | 1716 | if (!rs->last_pass_packet || strcmp (rs->last_pass_packet, pass_packet)) |
89be2091 | 1717 | { |
89be2091 DJ |
1718 | char *buf = rs->buf; |
1719 | ||
1720 | putpkt (pass_packet); | |
1721 | getpkt (&rs->buf, &rs->buf_size, 0); | |
1722 | packet_ok (buf, &remote_protocol_packets[PACKET_QPassSignals]); | |
747dc59d TT |
1723 | if (rs->last_pass_packet) |
1724 | xfree (rs->last_pass_packet); | |
1725 | rs->last_pass_packet = pass_packet; | |
89be2091 DJ |
1726 | } |
1727 | else | |
1728 | xfree (pass_packet); | |
1729 | } | |
1730 | } | |
1731 | ||
9b224c5e PA |
1732 | /* If 'QProgramSignals' is supported, tell the remote stub what |
1733 | signals it should pass through to the inferior when detaching. */ | |
1734 | ||
1735 | static void | |
1736 | remote_program_signals (int numsigs, unsigned char *signals) | |
1737 | { | |
1738 | if (remote_protocol_packets[PACKET_QProgramSignals].support != PACKET_DISABLE) | |
1739 | { | |
1740 | char *packet, *p; | |
1741 | int count = 0, i; | |
5e4a05c4 | 1742 | struct remote_state *rs = get_remote_state (); |
9b224c5e PA |
1743 | |
1744 | gdb_assert (numsigs < 256); | |
1745 | for (i = 0; i < numsigs; i++) | |
1746 | { | |
1747 | if (signals[i]) | |
1748 | count++; | |
1749 | } | |
1750 | packet = xmalloc (count * 3 + strlen ("QProgramSignals:") + 1); | |
1751 | strcpy (packet, "QProgramSignals:"); | |
1752 | p = packet + strlen (packet); | |
1753 | for (i = 0; i < numsigs; i++) | |
1754 | { | |
1755 | if (signal_pass_state (i)) | |
1756 | { | |
1757 | if (i >= 16) | |
1758 | *p++ = tohex (i >> 4); | |
1759 | *p++ = tohex (i & 15); | |
1760 | if (count) | |
1761 | *p++ = ';'; | |
1762 | else | |
1763 | break; | |
1764 | count--; | |
1765 | } | |
1766 | } | |
1767 | *p = 0; | |
5e4a05c4 TT |
1768 | if (!rs->last_program_signals_packet |
1769 | || strcmp (rs->last_program_signals_packet, packet) != 0) | |
9b224c5e | 1770 | { |
9b224c5e PA |
1771 | char *buf = rs->buf; |
1772 | ||
1773 | putpkt (packet); | |
1774 | getpkt (&rs->buf, &rs->buf_size, 0); | |
1775 | packet_ok (buf, &remote_protocol_packets[PACKET_QProgramSignals]); | |
5e4a05c4 TT |
1776 | xfree (rs->last_program_signals_packet); |
1777 | rs->last_program_signals_packet = packet; | |
9b224c5e PA |
1778 | } |
1779 | else | |
1780 | xfree (packet); | |
1781 | } | |
1782 | } | |
1783 | ||
79d7f229 PA |
1784 | /* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is |
1785 | MINUS_ONE_PTID, set the thread to -1, so the stub returns the | |
1786 | thread. If GEN is set, set the general thread, if not, then set | |
1787 | the step/continue thread. */ | |
c906108c | 1788 | static void |
79d7f229 | 1789 | set_thread (struct ptid ptid, int gen) |
c906108c | 1790 | { |
d01949b6 | 1791 | struct remote_state *rs = get_remote_state (); |
47f8a51d | 1792 | ptid_t state = gen ? rs->general_thread : rs->continue_thread; |
6d820c5c | 1793 | char *buf = rs->buf; |
79d7f229 | 1794 | char *endbuf = rs->buf + get_remote_packet_size (); |
c906108c | 1795 | |
79d7f229 | 1796 | if (ptid_equal (state, ptid)) |
c906108c SS |
1797 | return; |
1798 | ||
79d7f229 PA |
1799 | *buf++ = 'H'; |
1800 | *buf++ = gen ? 'g' : 'c'; | |
1801 | if (ptid_equal (ptid, magic_null_ptid)) | |
1802 | xsnprintf (buf, endbuf - buf, "0"); | |
1803 | else if (ptid_equal (ptid, any_thread_ptid)) | |
1804 | xsnprintf (buf, endbuf - buf, "0"); | |
1805 | else if (ptid_equal (ptid, minus_one_ptid)) | |
1806 | xsnprintf (buf, endbuf - buf, "-1"); | |
1807 | else | |
82f73884 | 1808 | write_ptid (buf, endbuf, ptid); |
79d7f229 | 1809 | putpkt (rs->buf); |
6d820c5c | 1810 | getpkt (&rs->buf, &rs->buf_size, 0); |
c906108c | 1811 | if (gen) |
47f8a51d | 1812 | rs->general_thread = ptid; |
c906108c | 1813 | else |
47f8a51d | 1814 | rs->continue_thread = ptid; |
c906108c | 1815 | } |
79d7f229 PA |
1816 | |
1817 | static void | |
1818 | set_general_thread (struct ptid ptid) | |
1819 | { | |
1820 | set_thread (ptid, 1); | |
1821 | } | |
1822 | ||
1823 | static void | |
1824 | set_continue_thread (struct ptid ptid) | |
1825 | { | |
1826 | set_thread (ptid, 0); | |
1827 | } | |
1828 | ||
3c9c4b83 PA |
1829 | /* Change the remote current process. Which thread within the process |
1830 | ends up selected isn't important, as long as it is the same process | |
1831 | as what INFERIOR_PTID points to. | |
1832 | ||
1833 | This comes from that fact that there is no explicit notion of | |
1834 | "selected process" in the protocol. The selected process for | |
1835 | general operations is the process the selected general thread | |
1836 | belongs to. */ | |
1837 | ||
1838 | static void | |
1839 | set_general_process (void) | |
1840 | { | |
1841 | struct remote_state *rs = get_remote_state (); | |
1842 | ||
1843 | /* If the remote can't handle multiple processes, don't bother. */ | |
901f9912 | 1844 | if (!rs->extended || !remote_multi_process_p (rs)) |
3c9c4b83 PA |
1845 | return; |
1846 | ||
1847 | /* We only need to change the remote current thread if it's pointing | |
1848 | at some other process. */ | |
47f8a51d | 1849 | if (ptid_get_pid (rs->general_thread) != ptid_get_pid (inferior_ptid)) |
3c9c4b83 PA |
1850 | set_general_thread (inferior_ptid); |
1851 | } | |
1852 | ||
c906108c | 1853 | \f |
79d7f229 PA |
1854 | /* Return nonzero if the thread PTID is still alive on the remote |
1855 | system. */ | |
c906108c SS |
1856 | |
1857 | static int | |
28439f5e | 1858 | remote_thread_alive (struct target_ops *ops, ptid_t ptid) |
c906108c | 1859 | { |
6d820c5c | 1860 | struct remote_state *rs = get_remote_state (); |
82f73884 | 1861 | char *p, *endp; |
c906108c | 1862 | |
c0a2216e PA |
1863 | if (ptid_equal (ptid, magic_null_ptid)) |
1864 | /* The main thread is always alive. */ | |
1865 | return 1; | |
1866 | ||
1867 | if (ptid_get_pid (ptid) != 0 && ptid_get_tid (ptid) == 0) | |
1868 | /* The main thread is always alive. This can happen after a | |
1869 | vAttach, if the remote side doesn't support | |
1870 | multi-threading. */ | |
1871 | return 1; | |
1872 | ||
82f73884 PA |
1873 | p = rs->buf; |
1874 | endp = rs->buf + get_remote_packet_size (); | |
1875 | ||
1876 | *p++ = 'T'; | |
1877 | write_ptid (p, endp, ptid); | |
1878 | ||
2e9f7625 | 1879 | putpkt (rs->buf); |
6d820c5c | 1880 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 1881 | return (rs->buf[0] == 'O' && rs->buf[1] == 'K'); |
c906108c SS |
1882 | } |
1883 | ||
1884 | /* About these extended threadlist and threadinfo packets. They are | |
1885 | variable length packets but, the fields within them are often fixed | |
1886 | length. They are redundent enough to send over UDP as is the | |
1887 | remote protocol in general. There is a matching unit test module | |
1888 | in libstub. */ | |
1889 | ||
23860348 | 1890 | /* WARNING: This threadref data structure comes from the remote O.S., |
0df8b418 | 1891 | libstub protocol encoding, and remote.c. It is not particularly |
23860348 | 1892 | changable. */ |
cce74817 JM |
1893 | |
1894 | /* Right now, the internal structure is int. We want it to be bigger. | |
0df8b418 | 1895 | Plan to fix this. */ |
cce74817 | 1896 | |
23860348 | 1897 | typedef int gdb_threadref; /* Internal GDB thread reference. */ |
cce74817 | 1898 | |
9d1f7ab2 | 1899 | /* gdb_ext_thread_info is an internal GDB data structure which is |
cfde0993 | 1900 | equivalent to the reply of the remote threadinfo packet. */ |
cce74817 JM |
1901 | |
1902 | struct gdb_ext_thread_info | |
c5aa993b | 1903 | { |
23860348 | 1904 | threadref threadid; /* External form of thread reference. */ |
2bc416ba | 1905 | int active; /* Has state interesting to GDB? |
23860348 | 1906 | regs, stack. */ |
2bc416ba | 1907 | char display[256]; /* Brief state display, name, |
cedea757 | 1908 | blocked/suspended. */ |
23860348 | 1909 | char shortname[32]; /* To be used to name threads. */ |
2bc416ba | 1910 | char more_display[256]; /* Long info, statistics, queue depth, |
23860348 | 1911 | whatever. */ |
c5aa993b | 1912 | }; |
cce74817 JM |
1913 | |
1914 | /* The volume of remote transfers can be limited by submitting | |
1915 | a mask containing bits specifying the desired information. | |
1916 | Use a union of these values as the 'selection' parameter to | |
0df8b418 | 1917 | get_thread_info. FIXME: Make these TAG names more thread specific. */ |
cce74817 JM |
1918 | |
1919 | #define TAG_THREADID 1 | |
1920 | #define TAG_EXISTS 2 | |
1921 | #define TAG_DISPLAY 4 | |
1922 | #define TAG_THREADNAME 8 | |
c5aa993b | 1923 | #define TAG_MOREDISPLAY 16 |
cce74817 | 1924 | |
23860348 | 1925 | #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2) |
c906108c | 1926 | |
b2dd6311 | 1927 | char *unpack_varlen_hex (char *buff, ULONGEST *result); |
cce74817 | 1928 | |
a14ed312 | 1929 | static char *unpack_nibble (char *buf, int *val); |
cce74817 | 1930 | |
a14ed312 | 1931 | static char *pack_nibble (char *buf, int nibble); |
cce74817 | 1932 | |
23860348 | 1933 | static char *pack_hex_byte (char *pkt, int /* unsigned char */ byte); |
cce74817 | 1934 | |
a14ed312 | 1935 | static char *unpack_byte (char *buf, int *value); |
cce74817 | 1936 | |
a14ed312 | 1937 | static char *pack_int (char *buf, int value); |
cce74817 | 1938 | |
a14ed312 | 1939 | static char *unpack_int (char *buf, int *value); |
cce74817 | 1940 | |
a14ed312 | 1941 | static char *unpack_string (char *src, char *dest, int length); |
cce74817 | 1942 | |
23860348 | 1943 | static char *pack_threadid (char *pkt, threadref *id); |
cce74817 | 1944 | |
23860348 | 1945 | static char *unpack_threadid (char *inbuf, threadref *id); |
cce74817 | 1946 | |
23860348 | 1947 | void int_to_threadref (threadref *id, int value); |
cce74817 | 1948 | |
23860348 | 1949 | static int threadref_to_int (threadref *ref); |
cce74817 | 1950 | |
23860348 | 1951 | static void copy_threadref (threadref *dest, threadref *src); |
cce74817 | 1952 | |
23860348 | 1953 | static int threadmatch (threadref *dest, threadref *src); |
cce74817 | 1954 | |
2bc416ba | 1955 | static char *pack_threadinfo_request (char *pkt, int mode, |
23860348 | 1956 | threadref *id); |
cce74817 | 1957 | |
a14ed312 | 1958 | static int remote_unpack_thread_info_response (char *pkt, |
23860348 | 1959 | threadref *expectedref, |
a14ed312 KB |
1960 | struct gdb_ext_thread_info |
1961 | *info); | |
cce74817 JM |
1962 | |
1963 | ||
2bc416ba | 1964 | static int remote_get_threadinfo (threadref *threadid, |
23860348 | 1965 | int fieldset, /*TAG mask */ |
a14ed312 | 1966 | struct gdb_ext_thread_info *info); |
cce74817 | 1967 | |
a14ed312 KB |
1968 | static char *pack_threadlist_request (char *pkt, int startflag, |
1969 | int threadcount, | |
23860348 | 1970 | threadref *nextthread); |
cce74817 | 1971 | |
a14ed312 KB |
1972 | static int parse_threadlist_response (char *pkt, |
1973 | int result_limit, | |
23860348 | 1974 | threadref *original_echo, |
2bc416ba | 1975 | threadref *resultlist, |
23860348 | 1976 | int *doneflag); |
cce74817 | 1977 | |
a14ed312 | 1978 | static int remote_get_threadlist (int startflag, |
23860348 | 1979 | threadref *nextthread, |
a14ed312 KB |
1980 | int result_limit, |
1981 | int *done, | |
2bc416ba | 1982 | int *result_count, |
23860348 | 1983 | threadref *threadlist); |
cce74817 | 1984 | |
23860348 | 1985 | typedef int (*rmt_thread_action) (threadref *ref, void *context); |
cce74817 | 1986 | |
a14ed312 KB |
1987 | static int remote_threadlist_iterator (rmt_thread_action stepfunction, |
1988 | void *context, int looplimit); | |
cce74817 | 1989 | |
23860348 | 1990 | static int remote_newthread_step (threadref *ref, void *context); |
cce74817 | 1991 | |
82f73884 PA |
1992 | |
1993 | /* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the | |
1994 | buffer we're allowed to write to. Returns | |
1995 | BUF+CHARACTERS_WRITTEN. */ | |
1996 | ||
1997 | static char * | |
1998 | write_ptid (char *buf, const char *endbuf, ptid_t ptid) | |
1999 | { | |
2000 | int pid, tid; | |
2001 | struct remote_state *rs = get_remote_state (); | |
2002 | ||
2003 | if (remote_multi_process_p (rs)) | |
2004 | { | |
2005 | pid = ptid_get_pid (ptid); | |
2006 | if (pid < 0) | |
2007 | buf += xsnprintf (buf, endbuf - buf, "p-%x.", -pid); | |
2008 | else | |
2009 | buf += xsnprintf (buf, endbuf - buf, "p%x.", pid); | |
2010 | } | |
2011 | tid = ptid_get_tid (ptid); | |
2012 | if (tid < 0) | |
2013 | buf += xsnprintf (buf, endbuf - buf, "-%x", -tid); | |
2014 | else | |
2015 | buf += xsnprintf (buf, endbuf - buf, "%x", tid); | |
2016 | ||
2017 | return buf; | |
2018 | } | |
2019 | ||
2020 | /* Extract a PTID from BUF. If non-null, OBUF is set to the to one | |
2021 | passed the last parsed char. Returns null_ptid on error. */ | |
2022 | ||
2023 | static ptid_t | |
2024 | read_ptid (char *buf, char **obuf) | |
2025 | { | |
2026 | char *p = buf; | |
2027 | char *pp; | |
2028 | ULONGEST pid = 0, tid = 0; | |
82f73884 PA |
2029 | |
2030 | if (*p == 'p') | |
2031 | { | |
2032 | /* Multi-process ptid. */ | |
2033 | pp = unpack_varlen_hex (p + 1, &pid); | |
2034 | if (*pp != '.') | |
b37520b6 | 2035 | error (_("invalid remote ptid: %s"), p); |
82f73884 PA |
2036 | |
2037 | p = pp; | |
2038 | pp = unpack_varlen_hex (p + 1, &tid); | |
2039 | if (obuf) | |
2040 | *obuf = pp; | |
2041 | return ptid_build (pid, 0, tid); | |
2042 | } | |
2043 | ||
2044 | /* No multi-process. Just a tid. */ | |
2045 | pp = unpack_varlen_hex (p, &tid); | |
2046 | ||
2047 | /* Since the stub is not sending a process id, then default to | |
ca19bf23 PA |
2048 | what's in inferior_ptid, unless it's null at this point. If so, |
2049 | then since there's no way to know the pid of the reported | |
2050 | threads, use the magic number. */ | |
2051 | if (ptid_equal (inferior_ptid, null_ptid)) | |
2052 | pid = ptid_get_pid (magic_null_ptid); | |
2053 | else | |
2054 | pid = ptid_get_pid (inferior_ptid); | |
82f73884 PA |
2055 | |
2056 | if (obuf) | |
2057 | *obuf = pp; | |
2058 | return ptid_build (pid, 0, tid); | |
2059 | } | |
2060 | ||
23860348 | 2061 | /* Encode 64 bits in 16 chars of hex. */ |
c906108c SS |
2062 | |
2063 | static const char hexchars[] = "0123456789abcdef"; | |
2064 | ||
2065 | static int | |
fba45db2 | 2066 | ishex (int ch, int *val) |
c906108c SS |
2067 | { |
2068 | if ((ch >= 'a') && (ch <= 'f')) | |
2069 | { | |
2070 | *val = ch - 'a' + 10; | |
2071 | return 1; | |
2072 | } | |
2073 | if ((ch >= 'A') && (ch <= 'F')) | |
2074 | { | |
2075 | *val = ch - 'A' + 10; | |
2076 | return 1; | |
2077 | } | |
2078 | if ((ch >= '0') && (ch <= '9')) | |
2079 | { | |
2080 | *val = ch - '0'; | |
2081 | return 1; | |
2082 | } | |
2083 | return 0; | |
2084 | } | |
2085 | ||
2086 | static int | |
fba45db2 | 2087 | stubhex (int ch) |
c906108c SS |
2088 | { |
2089 | if (ch >= 'a' && ch <= 'f') | |
2090 | return ch - 'a' + 10; | |
2091 | if (ch >= '0' && ch <= '9') | |
2092 | return ch - '0'; | |
2093 | if (ch >= 'A' && ch <= 'F') | |
2094 | return ch - 'A' + 10; | |
2095 | return -1; | |
2096 | } | |
2097 | ||
2098 | static int | |
fba45db2 | 2099 | stub_unpack_int (char *buff, int fieldlength) |
c906108c SS |
2100 | { |
2101 | int nibble; | |
2102 | int retval = 0; | |
2103 | ||
2104 | while (fieldlength) | |
2105 | { | |
2106 | nibble = stubhex (*buff++); | |
2107 | retval |= nibble; | |
2108 | fieldlength--; | |
2109 | if (fieldlength) | |
2110 | retval = retval << 4; | |
2111 | } | |
2112 | return retval; | |
2113 | } | |
2114 | ||
2115 | char * | |
fba45db2 | 2116 | unpack_varlen_hex (char *buff, /* packet to parse */ |
b2dd6311 | 2117 | ULONGEST *result) |
c906108c SS |
2118 | { |
2119 | int nibble; | |
d49c44d5 | 2120 | ULONGEST retval = 0; |
c906108c SS |
2121 | |
2122 | while (ishex (*buff, &nibble)) | |
2123 | { | |
2124 | buff++; | |
2125 | retval = retval << 4; | |
2126 | retval |= nibble & 0x0f; | |
2127 | } | |
2128 | *result = retval; | |
2129 | return buff; | |
2130 | } | |
2131 | ||
2132 | static char * | |
fba45db2 | 2133 | unpack_nibble (char *buf, int *val) |
c906108c | 2134 | { |
b7589f7d | 2135 | *val = fromhex (*buf++); |
c906108c SS |
2136 | return buf; |
2137 | } | |
2138 | ||
2139 | static char * | |
fba45db2 | 2140 | pack_nibble (char *buf, int nibble) |
c906108c SS |
2141 | { |
2142 | *buf++ = hexchars[(nibble & 0x0f)]; | |
2143 | return buf; | |
2144 | } | |
2145 | ||
2146 | static char * | |
fba45db2 | 2147 | pack_hex_byte (char *pkt, int byte) |
c906108c SS |
2148 | { |
2149 | *pkt++ = hexchars[(byte >> 4) & 0xf]; | |
2150 | *pkt++ = hexchars[(byte & 0xf)]; | |
2151 | return pkt; | |
2152 | } | |
2153 | ||
2154 | static char * | |
fba45db2 | 2155 | unpack_byte (char *buf, int *value) |
c906108c SS |
2156 | { |
2157 | *value = stub_unpack_int (buf, 2); | |
2158 | return buf + 2; | |
2159 | } | |
2160 | ||
2161 | static char * | |
fba45db2 | 2162 | pack_int (char *buf, int value) |
c906108c SS |
2163 | { |
2164 | buf = pack_hex_byte (buf, (value >> 24) & 0xff); | |
2165 | buf = pack_hex_byte (buf, (value >> 16) & 0xff); | |
2166 | buf = pack_hex_byte (buf, (value >> 8) & 0x0ff); | |
2167 | buf = pack_hex_byte (buf, (value & 0xff)); | |
2168 | return buf; | |
2169 | } | |
2170 | ||
2171 | static char * | |
fba45db2 | 2172 | unpack_int (char *buf, int *value) |
c906108c SS |
2173 | { |
2174 | *value = stub_unpack_int (buf, 8); | |
2175 | return buf + 8; | |
2176 | } | |
2177 | ||
23860348 | 2178 | #if 0 /* Currently unused, uncomment when needed. */ |
a14ed312 | 2179 | static char *pack_string (char *pkt, char *string); |
c906108c SS |
2180 | |
2181 | static char * | |
fba45db2 | 2182 | pack_string (char *pkt, char *string) |
c906108c SS |
2183 | { |
2184 | char ch; | |
2185 | int len; | |
2186 | ||
2187 | len = strlen (string); | |
2188 | if (len > 200) | |
23860348 | 2189 | len = 200; /* Bigger than most GDB packets, junk??? */ |
c906108c SS |
2190 | pkt = pack_hex_byte (pkt, len); |
2191 | while (len-- > 0) | |
2192 | { | |
2193 | ch = *string++; | |
2194 | if ((ch == '\0') || (ch == '#')) | |
23860348 | 2195 | ch = '*'; /* Protect encapsulation. */ |
c906108c SS |
2196 | *pkt++ = ch; |
2197 | } | |
2198 | return pkt; | |
2199 | } | |
2200 | #endif /* 0 (unused) */ | |
2201 | ||
2202 | static char * | |
fba45db2 | 2203 | unpack_string (char *src, char *dest, int length) |
c906108c SS |
2204 | { |
2205 | while (length--) | |
2206 | *dest++ = *src++; | |
2207 | *dest = '\0'; | |
2208 | return src; | |
2209 | } | |
2210 | ||
2211 | static char * | |
fba45db2 | 2212 | pack_threadid (char *pkt, threadref *id) |
c906108c SS |
2213 | { |
2214 | char *limit; | |
2215 | unsigned char *altid; | |
2216 | ||
2217 | altid = (unsigned char *) id; | |
2218 | limit = pkt + BUF_THREAD_ID_SIZE; | |
2219 | while (pkt < limit) | |
2220 | pkt = pack_hex_byte (pkt, *altid++); | |
2221 | return pkt; | |
2222 | } | |
2223 | ||
2224 | ||
2225 | static char * | |
fba45db2 | 2226 | unpack_threadid (char *inbuf, threadref *id) |
c906108c SS |
2227 | { |
2228 | char *altref; | |
2229 | char *limit = inbuf + BUF_THREAD_ID_SIZE; | |
2230 | int x, y; | |
2231 | ||
2232 | altref = (char *) id; | |
2233 | ||
2234 | while (inbuf < limit) | |
2235 | { | |
2236 | x = stubhex (*inbuf++); | |
2237 | y = stubhex (*inbuf++); | |
2238 | *altref++ = (x << 4) | y; | |
2239 | } | |
2240 | return inbuf; | |
2241 | } | |
2242 | ||
2243 | /* Externally, threadrefs are 64 bits but internally, they are still | |
0df8b418 | 2244 | ints. This is due to a mismatch of specifications. We would like |
c906108c SS |
2245 | to use 64bit thread references internally. This is an adapter |
2246 | function. */ | |
2247 | ||
2248 | void | |
fba45db2 | 2249 | int_to_threadref (threadref *id, int value) |
c906108c SS |
2250 | { |
2251 | unsigned char *scan; | |
2252 | ||
2253 | scan = (unsigned char *) id; | |
2254 | { | |
2255 | int i = 4; | |
2256 | while (i--) | |
2257 | *scan++ = 0; | |
2258 | } | |
2259 | *scan++ = (value >> 24) & 0xff; | |
2260 | *scan++ = (value >> 16) & 0xff; | |
2261 | *scan++ = (value >> 8) & 0xff; | |
2262 | *scan++ = (value & 0xff); | |
2263 | } | |
2264 | ||
2265 | static int | |
fba45db2 | 2266 | threadref_to_int (threadref *ref) |
c906108c SS |
2267 | { |
2268 | int i, value = 0; | |
2269 | unsigned char *scan; | |
2270 | ||
cfd77fa1 | 2271 | scan = *ref; |
c906108c SS |
2272 | scan += 4; |
2273 | i = 4; | |
2274 | while (i-- > 0) | |
2275 | value = (value << 8) | ((*scan++) & 0xff); | |
2276 | return value; | |
2277 | } | |
2278 | ||
2279 | static void | |
fba45db2 | 2280 | copy_threadref (threadref *dest, threadref *src) |
c906108c SS |
2281 | { |
2282 | int i; | |
2283 | unsigned char *csrc, *cdest; | |
2284 | ||
2285 | csrc = (unsigned char *) src; | |
2286 | cdest = (unsigned char *) dest; | |
2287 | i = 8; | |
2288 | while (i--) | |
2289 | *cdest++ = *csrc++; | |
2290 | } | |
2291 | ||
2292 | static int | |
fba45db2 | 2293 | threadmatch (threadref *dest, threadref *src) |
c906108c | 2294 | { |
23860348 | 2295 | /* Things are broken right now, so just assume we got a match. */ |
c906108c SS |
2296 | #if 0 |
2297 | unsigned char *srcp, *destp; | |
2298 | int i, result; | |
2299 | srcp = (char *) src; | |
2300 | destp = (char *) dest; | |
2301 | ||
2302 | result = 1; | |
2303 | while (i-- > 0) | |
2304 | result &= (*srcp++ == *destp++) ? 1 : 0; | |
2305 | return result; | |
2306 | #endif | |
2307 | return 1; | |
2308 | } | |
2309 | ||
2310 | /* | |
c5aa993b JM |
2311 | threadid:1, # always request threadid |
2312 | context_exists:2, | |
2313 | display:4, | |
2314 | unique_name:8, | |
2315 | more_display:16 | |
2316 | */ | |
c906108c SS |
2317 | |
2318 | /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */ | |
2319 | ||
2320 | static char * | |
fba45db2 | 2321 | pack_threadinfo_request (char *pkt, int mode, threadref *id) |
c906108c | 2322 | { |
23860348 MS |
2323 | *pkt++ = 'q'; /* Info Query */ |
2324 | *pkt++ = 'P'; /* process or thread info */ | |
2325 | pkt = pack_int (pkt, mode); /* mode */ | |
c906108c | 2326 | pkt = pack_threadid (pkt, id); /* threadid */ |
23860348 | 2327 | *pkt = '\0'; /* terminate */ |
c906108c SS |
2328 | return pkt; |
2329 | } | |
2330 | ||
23860348 | 2331 | /* These values tag the fields in a thread info response packet. */ |
c906108c | 2332 | /* Tagging the fields allows us to request specific fields and to |
23860348 | 2333 | add more fields as time goes by. */ |
c906108c | 2334 | |
23860348 | 2335 | #define TAG_THREADID 1 /* Echo the thread identifier. */ |
c5aa993b | 2336 | #define TAG_EXISTS 2 /* Is this process defined enough to |
23860348 | 2337 | fetch registers and its stack? */ |
c5aa993b | 2338 | #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */ |
23860348 | 2339 | #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */ |
802188a7 | 2340 | #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about |
23860348 | 2341 | the process. */ |
c906108c SS |
2342 | |
2343 | static int | |
fba45db2 KB |
2344 | remote_unpack_thread_info_response (char *pkt, threadref *expectedref, |
2345 | struct gdb_ext_thread_info *info) | |
c906108c | 2346 | { |
d01949b6 | 2347 | struct remote_state *rs = get_remote_state (); |
c906108c | 2348 | int mask, length; |
cfd77fa1 | 2349 | int tag; |
c906108c | 2350 | threadref ref; |
6d820c5c | 2351 | char *limit = pkt + rs->buf_size; /* Plausible parsing limit. */ |
c906108c SS |
2352 | int retval = 1; |
2353 | ||
23860348 | 2354 | /* info->threadid = 0; FIXME: implement zero_threadref. */ |
c906108c SS |
2355 | info->active = 0; |
2356 | info->display[0] = '\0'; | |
2357 | info->shortname[0] = '\0'; | |
2358 | info->more_display[0] = '\0'; | |
2359 | ||
23860348 MS |
2360 | /* Assume the characters indicating the packet type have been |
2361 | stripped. */ | |
c906108c SS |
2362 | pkt = unpack_int (pkt, &mask); /* arg mask */ |
2363 | pkt = unpack_threadid (pkt, &ref); | |
2364 | ||
2365 | if (mask == 0) | |
8a3fe4f8 | 2366 | warning (_("Incomplete response to threadinfo request.")); |
c906108c | 2367 | if (!threadmatch (&ref, expectedref)) |
23860348 | 2368 | { /* This is an answer to a different request. */ |
8a3fe4f8 | 2369 | warning (_("ERROR RMT Thread info mismatch.")); |
c906108c SS |
2370 | return 0; |
2371 | } | |
2372 | copy_threadref (&info->threadid, &ref); | |
2373 | ||
23860348 | 2374 | /* Loop on tagged fields , try to bail if somthing goes wrong. */ |
c906108c | 2375 | |
23860348 MS |
2376 | /* Packets are terminated with nulls. */ |
2377 | while ((pkt < limit) && mask && *pkt) | |
c906108c SS |
2378 | { |
2379 | pkt = unpack_int (pkt, &tag); /* tag */ | |
23860348 MS |
2380 | pkt = unpack_byte (pkt, &length); /* length */ |
2381 | if (!(tag & mask)) /* Tags out of synch with mask. */ | |
c906108c | 2382 | { |
8a3fe4f8 | 2383 | warning (_("ERROR RMT: threadinfo tag mismatch.")); |
c906108c SS |
2384 | retval = 0; |
2385 | break; | |
2386 | } | |
2387 | if (tag == TAG_THREADID) | |
2388 | { | |
2389 | if (length != 16) | |
2390 | { | |
8a3fe4f8 | 2391 | warning (_("ERROR RMT: length of threadid is not 16.")); |
c906108c SS |
2392 | retval = 0; |
2393 | break; | |
2394 | } | |
2395 | pkt = unpack_threadid (pkt, &ref); | |
2396 | mask = mask & ~TAG_THREADID; | |
2397 | continue; | |
2398 | } | |
2399 | if (tag == TAG_EXISTS) | |
2400 | { | |
2401 | info->active = stub_unpack_int (pkt, length); | |
2402 | pkt += length; | |
2403 | mask = mask & ~(TAG_EXISTS); | |
2404 | if (length > 8) | |
2405 | { | |
8a3fe4f8 | 2406 | warning (_("ERROR RMT: 'exists' length too long.")); |
c906108c SS |
2407 | retval = 0; |
2408 | break; | |
2409 | } | |
2410 | continue; | |
2411 | } | |
2412 | if (tag == TAG_THREADNAME) | |
2413 | { | |
2414 | pkt = unpack_string (pkt, &info->shortname[0], length); | |
2415 | mask = mask & ~TAG_THREADNAME; | |
2416 | continue; | |
2417 | } | |
2418 | if (tag == TAG_DISPLAY) | |
2419 | { | |
2420 | pkt = unpack_string (pkt, &info->display[0], length); | |
2421 | mask = mask & ~TAG_DISPLAY; | |
2422 | continue; | |
2423 | } | |
2424 | if (tag == TAG_MOREDISPLAY) | |
2425 | { | |
2426 | pkt = unpack_string (pkt, &info->more_display[0], length); | |
2427 | mask = mask & ~TAG_MOREDISPLAY; | |
2428 | continue; | |
2429 | } | |
8a3fe4f8 | 2430 | warning (_("ERROR RMT: unknown thread info tag.")); |
23860348 | 2431 | break; /* Not a tag we know about. */ |
c906108c SS |
2432 | } |
2433 | return retval; | |
2434 | } | |
2435 | ||
2436 | static int | |
fba45db2 KB |
2437 | remote_get_threadinfo (threadref *threadid, int fieldset, /* TAG mask */ |
2438 | struct gdb_ext_thread_info *info) | |
c906108c | 2439 | { |
d01949b6 | 2440 | struct remote_state *rs = get_remote_state (); |
c906108c | 2441 | int result; |
c906108c | 2442 | |
2e9f7625 DJ |
2443 | pack_threadinfo_request (rs->buf, fieldset, threadid); |
2444 | putpkt (rs->buf); | |
6d820c5c | 2445 | getpkt (&rs->buf, &rs->buf_size, 0); |
3084dd77 PA |
2446 | |
2447 | if (rs->buf[0] == '\0') | |
2448 | return 0; | |
2449 | ||
2e9f7625 | 2450 | result = remote_unpack_thread_info_response (rs->buf + 2, |
23860348 | 2451 | threadid, info); |
c906108c SS |
2452 | return result; |
2453 | } | |
2454 | ||
c906108c SS |
2455 | /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */ |
2456 | ||
2457 | static char * | |
fba45db2 KB |
2458 | pack_threadlist_request (char *pkt, int startflag, int threadcount, |
2459 | threadref *nextthread) | |
c906108c SS |
2460 | { |
2461 | *pkt++ = 'q'; /* info query packet */ | |
2462 | *pkt++ = 'L'; /* Process LIST or threadLIST request */ | |
23860348 | 2463 | pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */ |
c906108c SS |
2464 | pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */ |
2465 | pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */ | |
2466 | *pkt = '\0'; | |
2467 | return pkt; | |
2468 | } | |
2469 | ||
2470 | /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */ | |
2471 | ||
2472 | static int | |
fba45db2 KB |
2473 | parse_threadlist_response (char *pkt, int result_limit, |
2474 | threadref *original_echo, threadref *resultlist, | |
2475 | int *doneflag) | |
c906108c | 2476 | { |
d01949b6 | 2477 | struct remote_state *rs = get_remote_state (); |
c906108c SS |
2478 | char *limit; |
2479 | int count, resultcount, done; | |
2480 | ||
2481 | resultcount = 0; | |
2482 | /* Assume the 'q' and 'M chars have been stripped. */ | |
6d820c5c | 2483 | limit = pkt + (rs->buf_size - BUF_THREAD_ID_SIZE); |
23860348 | 2484 | /* done parse past here */ |
c906108c SS |
2485 | pkt = unpack_byte (pkt, &count); /* count field */ |
2486 | pkt = unpack_nibble (pkt, &done); | |
2487 | /* The first threadid is the argument threadid. */ | |
2488 | pkt = unpack_threadid (pkt, original_echo); /* should match query packet */ | |
2489 | while ((count-- > 0) && (pkt < limit)) | |
2490 | { | |
2491 | pkt = unpack_threadid (pkt, resultlist++); | |
2492 | if (resultcount++ >= result_limit) | |
2493 | break; | |
2494 | } | |
2495 | if (doneflag) | |
2496 | *doneflag = done; | |
2497 | return resultcount; | |
2498 | } | |
2499 | ||
2500 | static int | |
fba45db2 KB |
2501 | remote_get_threadlist (int startflag, threadref *nextthread, int result_limit, |
2502 | int *done, int *result_count, threadref *threadlist) | |
c906108c | 2503 | { |
d01949b6 | 2504 | struct remote_state *rs = get_remote_state (); |
c906108c SS |
2505 | int result = 1; |
2506 | ||
23860348 | 2507 | /* Trancate result limit to be smaller than the packet size. */ |
3e43a32a MS |
2508 | if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10) |
2509 | >= get_remote_packet_size ()) | |
ea9c271d | 2510 | result_limit = (get_remote_packet_size () / BUF_THREAD_ID_SIZE) - 2; |
c906108c | 2511 | |
6d820c5c DJ |
2512 | pack_threadlist_request (rs->buf, startflag, result_limit, nextthread); |
2513 | putpkt (rs->buf); | |
2514 | getpkt (&rs->buf, &rs->buf_size, 0); | |
c906108c | 2515 | |
d8f2712d | 2516 | if (*rs->buf == '\0') |
21bce120 | 2517 | return 0; |
d8f2712d VP |
2518 | else |
2519 | *result_count = | |
0d031856 TT |
2520 | parse_threadlist_response (rs->buf + 2, result_limit, |
2521 | &rs->echo_nextthread, threadlist, done); | |
c906108c | 2522 | |
0d031856 | 2523 | if (!threadmatch (&rs->echo_nextthread, nextthread)) |
c906108c | 2524 | { |
23860348 MS |
2525 | /* FIXME: This is a good reason to drop the packet. */ |
2526 | /* Possably, there is a duplicate response. */ | |
c906108c SS |
2527 | /* Possabilities : |
2528 | retransmit immediatly - race conditions | |
2529 | retransmit after timeout - yes | |
2530 | exit | |
2531 | wait for packet, then exit | |
2532 | */ | |
8a3fe4f8 | 2533 | warning (_("HMM: threadlist did not echo arg thread, dropping it.")); |
23860348 | 2534 | return 0; /* I choose simply exiting. */ |
c906108c SS |
2535 | } |
2536 | if (*result_count <= 0) | |
2537 | { | |
2538 | if (*done != 1) | |
2539 | { | |
8a3fe4f8 | 2540 | warning (_("RMT ERROR : failed to get remote thread list.")); |
c906108c SS |
2541 | result = 0; |
2542 | } | |
2543 | return result; /* break; */ | |
2544 | } | |
2545 | if (*result_count > result_limit) | |
2546 | { | |
2547 | *result_count = 0; | |
8a3fe4f8 | 2548 | warning (_("RMT ERROR: threadlist response longer than requested.")); |
c906108c SS |
2549 | return 0; |
2550 | } | |
2551 | return result; | |
2552 | } | |
2553 | ||
23860348 MS |
2554 | /* This is the interface between remote and threads, remotes upper |
2555 | interface. */ | |
c906108c SS |
2556 | |
2557 | /* remote_find_new_threads retrieves the thread list and for each | |
2558 | thread in the list, looks up the thread in GDB's internal list, | |
79d7f229 | 2559 | adding the thread if it does not already exist. This involves |
c906108c SS |
2560 | getting partial thread lists from the remote target so, polling the |
2561 | quit_flag is required. */ | |
2562 | ||
2563 | ||
c906108c | 2564 | static int |
fba45db2 KB |
2565 | remote_threadlist_iterator (rmt_thread_action stepfunction, void *context, |
2566 | int looplimit) | |
c906108c | 2567 | { |
0d031856 | 2568 | struct remote_state *rs = get_remote_state (); |
c906108c SS |
2569 | int done, i, result_count; |
2570 | int startflag = 1; | |
2571 | int result = 1; | |
2572 | int loopcount = 0; | |
c906108c SS |
2573 | |
2574 | done = 0; | |
2575 | while (!done) | |
2576 | { | |
2577 | if (loopcount++ > looplimit) | |
2578 | { | |
2579 | result = 0; | |
8a3fe4f8 | 2580 | warning (_("Remote fetch threadlist -infinite loop-.")); |
c906108c SS |
2581 | break; |
2582 | } | |
0d031856 TT |
2583 | if (!remote_get_threadlist (startflag, &rs->nextthread, |
2584 | MAXTHREADLISTRESULTS, | |
2585 | &done, &result_count, rs->resultthreadlist)) | |
c906108c SS |
2586 | { |
2587 | result = 0; | |
2588 | break; | |
2589 | } | |
23860348 | 2590 | /* Clear for later iterations. */ |
c906108c SS |
2591 | startflag = 0; |
2592 | /* Setup to resume next batch of thread references, set nextthread. */ | |
2593 | if (result_count >= 1) | |
0d031856 TT |
2594 | copy_threadref (&rs->nextthread, |
2595 | &rs->resultthreadlist[result_count - 1]); | |
c906108c SS |
2596 | i = 0; |
2597 | while (result_count--) | |
0d031856 | 2598 | if (!(result = (*stepfunction) (&rs->resultthreadlist[i++], context))) |
c906108c SS |
2599 | break; |
2600 | } | |
2601 | return result; | |
2602 | } | |
2603 | ||
2604 | static int | |
fba45db2 | 2605 | remote_newthread_step (threadref *ref, void *context) |
c906108c | 2606 | { |
79d7f229 PA |
2607 | int pid = ptid_get_pid (inferior_ptid); |
2608 | ptid_t ptid = ptid_build (pid, 0, threadref_to_int (ref)); | |
39f77062 KB |
2609 | |
2610 | if (!in_thread_list (ptid)) | |
2611 | add_thread (ptid); | |
c906108c SS |
2612 | return 1; /* continue iterator */ |
2613 | } | |
2614 | ||
2615 | #define CRAZY_MAX_THREADS 1000 | |
2616 | ||
39f77062 KB |
2617 | static ptid_t |
2618 | remote_current_thread (ptid_t oldpid) | |
c906108c | 2619 | { |
d01949b6 | 2620 | struct remote_state *rs = get_remote_state (); |
c906108c SS |
2621 | |
2622 | putpkt ("qC"); | |
6d820c5c | 2623 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 2624 | if (rs->buf[0] == 'Q' && rs->buf[1] == 'C') |
82f73884 | 2625 | return read_ptid (&rs->buf[2], NULL); |
c906108c SS |
2626 | else |
2627 | return oldpid; | |
2628 | } | |
2629 | ||
802188a7 RM |
2630 | /* Find new threads for info threads command. |
2631 | * Original version, using John Metzler's thread protocol. | |
9d1f7ab2 | 2632 | */ |
cce74817 JM |
2633 | |
2634 | static void | |
fba45db2 | 2635 | remote_find_new_threads (void) |
c906108c | 2636 | { |
c5aa993b JM |
2637 | remote_threadlist_iterator (remote_newthread_step, 0, |
2638 | CRAZY_MAX_THREADS); | |
c906108c SS |
2639 | } |
2640 | ||
dc146f7c VP |
2641 | #if defined(HAVE_LIBEXPAT) |
2642 | ||
2643 | typedef struct thread_item | |
2644 | { | |
2645 | ptid_t ptid; | |
2646 | char *extra; | |
2647 | int core; | |
2648 | } thread_item_t; | |
2649 | DEF_VEC_O(thread_item_t); | |
2650 | ||
2651 | struct threads_parsing_context | |
2652 | { | |
2653 | VEC (thread_item_t) *items; | |
2654 | }; | |
2655 | ||
2656 | static void | |
2657 | start_thread (struct gdb_xml_parser *parser, | |
2658 | const struct gdb_xml_element *element, | |
2659 | void *user_data, VEC(gdb_xml_value_s) *attributes) | |
2660 | { | |
2661 | struct threads_parsing_context *data = user_data; | |
2662 | ||
2663 | struct thread_item item; | |
2664 | char *id; | |
3d2c1d41 | 2665 | struct gdb_xml_value *attr; |
dc146f7c | 2666 | |
3d2c1d41 | 2667 | id = xml_find_attribute (attributes, "id")->value; |
dc146f7c VP |
2668 | item.ptid = read_ptid (id, NULL); |
2669 | ||
3d2c1d41 PA |
2670 | attr = xml_find_attribute (attributes, "core"); |
2671 | if (attr != NULL) | |
2672 | item.core = *(ULONGEST *) attr->value; | |
dc146f7c VP |
2673 | else |
2674 | item.core = -1; | |
2675 | ||
2676 | item.extra = 0; | |
2677 | ||
2678 | VEC_safe_push (thread_item_t, data->items, &item); | |
2679 | } | |
2680 | ||
2681 | static void | |
2682 | end_thread (struct gdb_xml_parser *parser, | |
2683 | const struct gdb_xml_element *element, | |
2684 | void *user_data, const char *body_text) | |
2685 | { | |
2686 | struct threads_parsing_context *data = user_data; | |
2687 | ||
2688 | if (body_text && *body_text) | |
2ae2a0b7 | 2689 | VEC_last (thread_item_t, data->items)->extra = xstrdup (body_text); |
dc146f7c VP |
2690 | } |
2691 | ||
2692 | const struct gdb_xml_attribute thread_attributes[] = { | |
2693 | { "id", GDB_XML_AF_NONE, NULL, NULL }, | |
2694 | { "core", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL }, | |
2695 | { NULL, GDB_XML_AF_NONE, NULL, NULL } | |
2696 | }; | |
2697 | ||
2698 | const struct gdb_xml_element thread_children[] = { | |
2699 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } | |
2700 | }; | |
2701 | ||
2702 | const struct gdb_xml_element threads_children[] = { | |
2703 | { "thread", thread_attributes, thread_children, | |
2704 | GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL, | |
2705 | start_thread, end_thread }, | |
2706 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } | |
2707 | }; | |
2708 | ||
2709 | const struct gdb_xml_element threads_elements[] = { | |
2710 | { "threads", NULL, threads_children, | |
2711 | GDB_XML_EF_NONE, NULL, NULL }, | |
2712 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } | |
2713 | }; | |
2714 | ||
02357a4a PA |
2715 | /* Discard the contents of the constructed thread info context. */ |
2716 | ||
2717 | static void | |
2718 | clear_threads_parsing_context (void *p) | |
2719 | { | |
2720 | struct threads_parsing_context *context = p; | |
2721 | int i; | |
2722 | struct thread_item *item; | |
2723 | ||
2724 | for (i = 0; VEC_iterate (thread_item_t, context->items, i, item); ++i) | |
2725 | xfree (item->extra); | |
2726 | ||
2727 | VEC_free (thread_item_t, context->items); | |
2728 | } | |
2729 | ||
dc146f7c VP |
2730 | #endif |
2731 | ||
9d1f7ab2 MS |
2732 | /* |
2733 | * Find all threads for info threads command. | |
2734 | * Uses new thread protocol contributed by Cisco. | |
2735 | * Falls back and attempts to use the older method (above) | |
2736 | * if the target doesn't respond to the new method. | |
2737 | */ | |
2738 | ||
0f71a2f6 | 2739 | static void |
28439f5e | 2740 | remote_threads_info (struct target_ops *ops) |
0f71a2f6 | 2741 | { |
d01949b6 | 2742 | struct remote_state *rs = get_remote_state (); |
085dd6e6 | 2743 | char *bufp; |
79d7f229 | 2744 | ptid_t new_thread; |
0f71a2f6 | 2745 | |
5d93a237 | 2746 | if (rs->remote_desc == 0) /* paranoia */ |
8a3fe4f8 | 2747 | error (_("Command can only be used when connected to the remote target.")); |
0f71a2f6 | 2748 | |
dc146f7c VP |
2749 | #if defined(HAVE_LIBEXPAT) |
2750 | if (remote_protocol_packets[PACKET_qXfer_threads].support == PACKET_ENABLE) | |
2751 | { | |
2752 | char *xml = target_read_stralloc (¤t_target, | |
2753 | TARGET_OBJECT_THREADS, NULL); | |
2754 | ||
2755 | struct cleanup *back_to = make_cleanup (xfree, xml); | |
efc0eabd | 2756 | |
dc146f7c VP |
2757 | if (xml && *xml) |
2758 | { | |
dc146f7c | 2759 | struct threads_parsing_context context; |
dc146f7c | 2760 | |
efc0eabd PA |
2761 | context.items = NULL; |
2762 | make_cleanup (clear_threads_parsing_context, &context); | |
dc146f7c | 2763 | |
efc0eabd PA |
2764 | if (gdb_xml_parse_quick (_("threads"), "threads.dtd", |
2765 | threads_elements, xml, &context) == 0) | |
dc146f7c VP |
2766 | { |
2767 | int i; | |
2768 | struct thread_item *item; | |
2769 | ||
3e43a32a MS |
2770 | for (i = 0; |
2771 | VEC_iterate (thread_item_t, context.items, i, item); | |
2772 | ++i) | |
dc146f7c VP |
2773 | { |
2774 | if (!ptid_equal (item->ptid, null_ptid)) | |
2775 | { | |
2776 | struct private_thread_info *info; | |
2777 | /* In non-stop mode, we assume new found threads | |
2778 | are running until proven otherwise with a | |
2779 | stop reply. In all-stop, we can only get | |
2780 | here if all threads are stopped. */ | |
2781 | int running = non_stop ? 1 : 0; | |
2782 | ||
2783 | remote_notice_new_inferior (item->ptid, running); | |
2784 | ||
2785 | info = demand_private_info (item->ptid); | |
2786 | info->core = item->core; | |
2787 | info->extra = item->extra; | |
02357a4a | 2788 | item->extra = NULL; |
dc146f7c | 2789 | } |
dc146f7c VP |
2790 | } |
2791 | } | |
dc146f7c VP |
2792 | } |
2793 | ||
2794 | do_cleanups (back_to); | |
2795 | return; | |
2796 | } | |
2797 | #endif | |
2798 | ||
b80fafe3 | 2799 | if (rs->use_threadinfo_query) |
9d1f7ab2 MS |
2800 | { |
2801 | putpkt ("qfThreadInfo"); | |
6d820c5c | 2802 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 2803 | bufp = rs->buf; |
9d1f7ab2 | 2804 | if (bufp[0] != '\0') /* q packet recognized */ |
802188a7 | 2805 | { |
44d594fd PA |
2806 | struct cleanup *old_chain; |
2807 | char *saved_reply; | |
2808 | ||
2809 | /* remote_notice_new_inferior (in the loop below) may make | |
2810 | new RSP calls, which clobber rs->buf. Work with a | |
2811 | copy. */ | |
2812 | bufp = saved_reply = xstrdup (rs->buf); | |
2813 | old_chain = make_cleanup (free_current_contents, &saved_reply); | |
2814 | ||
9d1f7ab2 MS |
2815 | while (*bufp++ == 'm') /* reply contains one or more TID */ |
2816 | { | |
2817 | do | |
2818 | { | |
82f73884 | 2819 | new_thread = read_ptid (bufp, &bufp); |
1941c569 | 2820 | if (!ptid_equal (new_thread, null_ptid)) |
82f73884 | 2821 | { |
74531fed | 2822 | /* In non-stop mode, we assume new found threads |
1941c569 | 2823 | are running until proven otherwise with a |
74531fed PA |
2824 | stop reply. In all-stop, we can only get |
2825 | here if all threads are stopped. */ | |
1941c569 PA |
2826 | int running = non_stop ? 1 : 0; |
2827 | ||
2828 | remote_notice_new_inferior (new_thread, running); | |
82f73884 | 2829 | } |
9d1f7ab2 MS |
2830 | } |
2831 | while (*bufp++ == ','); /* comma-separated list */ | |
44d594fd | 2832 | free_current_contents (&saved_reply); |
9d1f7ab2 | 2833 | putpkt ("qsThreadInfo"); |
6d820c5c | 2834 | getpkt (&rs->buf, &rs->buf_size, 0); |
44d594fd | 2835 | bufp = saved_reply = xstrdup (rs->buf); |
9d1f7ab2 | 2836 | } |
44d594fd | 2837 | do_cleanups (old_chain); |
9d1f7ab2 MS |
2838 | return; /* done */ |
2839 | } | |
2840 | } | |
2841 | ||
74531fed PA |
2842 | /* Only qfThreadInfo is supported in non-stop mode. */ |
2843 | if (non_stop) | |
2844 | return; | |
2845 | ||
23860348 | 2846 | /* Else fall back to old method based on jmetzler protocol. */ |
b80fafe3 | 2847 | rs->use_threadinfo_query = 0; |
9d1f7ab2 MS |
2848 | remote_find_new_threads (); |
2849 | return; | |
2850 | } | |
2851 | ||
802188a7 | 2852 | /* |
9d1f7ab2 MS |
2853 | * Collect a descriptive string about the given thread. |
2854 | * The target may say anything it wants to about the thread | |
2855 | * (typically info about its blocked / runnable state, name, etc.). | |
2856 | * This string will appear in the info threads display. | |
802188a7 | 2857 | * |
9d1f7ab2 MS |
2858 | * Optional: targets are not required to implement this function. |
2859 | */ | |
2860 | ||
2861 | static char * | |
2862 | remote_threads_extra_info (struct thread_info *tp) | |
2863 | { | |
d01949b6 | 2864 | struct remote_state *rs = get_remote_state (); |
9d1f7ab2 MS |
2865 | int result; |
2866 | int set; | |
2867 | threadref id; | |
2868 | struct gdb_ext_thread_info threadinfo; | |
23860348 | 2869 | static char display_buf[100]; /* arbitrary... */ |
9d1f7ab2 MS |
2870 | int n = 0; /* position in display_buf */ |
2871 | ||
5d93a237 | 2872 | if (rs->remote_desc == 0) /* paranoia */ |
8e65ff28 | 2873 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 2874 | _("remote_threads_extra_info")); |
9d1f7ab2 | 2875 | |
60e569b9 PA |
2876 | if (ptid_equal (tp->ptid, magic_null_ptid) |
2877 | || (ptid_get_pid (tp->ptid) != 0 && ptid_get_tid (tp->ptid) == 0)) | |
2878 | /* This is the main thread which was added by GDB. The remote | |
2879 | server doesn't know about it. */ | |
2880 | return NULL; | |
2881 | ||
dc146f7c VP |
2882 | if (remote_protocol_packets[PACKET_qXfer_threads].support == PACKET_ENABLE) |
2883 | { | |
2884 | struct thread_info *info = find_thread_ptid (tp->ptid); | |
a744cf53 | 2885 | |
dc146f7c VP |
2886 | if (info && info->private) |
2887 | return info->private->extra; | |
2888 | else | |
2889 | return NULL; | |
2890 | } | |
2891 | ||
b80fafe3 | 2892 | if (rs->use_threadextra_query) |
9d1f7ab2 | 2893 | { |
82f73884 PA |
2894 | char *b = rs->buf; |
2895 | char *endb = rs->buf + get_remote_packet_size (); | |
2896 | ||
2897 | xsnprintf (b, endb - b, "qThreadExtraInfo,"); | |
2898 | b += strlen (b); | |
2899 | write_ptid (b, endb, tp->ptid); | |
2900 | ||
2e9f7625 | 2901 | putpkt (rs->buf); |
6d820c5c | 2902 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 2903 | if (rs->buf[0] != 0) |
9d1f7ab2 | 2904 | { |
2e9f7625 DJ |
2905 | n = min (strlen (rs->buf) / 2, sizeof (display_buf)); |
2906 | result = hex2bin (rs->buf, (gdb_byte *) display_buf, n); | |
30559e10 | 2907 | display_buf [result] = '\0'; |
9d1f7ab2 MS |
2908 | return display_buf; |
2909 | } | |
0f71a2f6 | 2910 | } |
9d1f7ab2 MS |
2911 | |
2912 | /* If the above query fails, fall back to the old method. */ | |
b80fafe3 | 2913 | rs->use_threadextra_query = 0; |
9d1f7ab2 MS |
2914 | set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME |
2915 | | TAG_MOREDISPLAY | TAG_DISPLAY; | |
79d7f229 | 2916 | int_to_threadref (&id, ptid_get_tid (tp->ptid)); |
9d1f7ab2 MS |
2917 | if (remote_get_threadinfo (&id, set, &threadinfo)) |
2918 | if (threadinfo.active) | |
0f71a2f6 | 2919 | { |
9d1f7ab2 | 2920 | if (*threadinfo.shortname) |
2bc416ba | 2921 | n += xsnprintf (&display_buf[0], sizeof (display_buf) - n, |
ecbc58df | 2922 | " Name: %s,", threadinfo.shortname); |
9d1f7ab2 | 2923 | if (*threadinfo.display) |
2bc416ba | 2924 | n += xsnprintf (&display_buf[n], sizeof (display_buf) - n, |
ecbc58df | 2925 | " State: %s,", threadinfo.display); |
9d1f7ab2 | 2926 | if (*threadinfo.more_display) |
2bc416ba | 2927 | n += xsnprintf (&display_buf[n], sizeof (display_buf) - n, |
ecbc58df | 2928 | " Priority: %s", threadinfo.more_display); |
9d1f7ab2 MS |
2929 | |
2930 | if (n > 0) | |
c5aa993b | 2931 | { |
23860348 | 2932 | /* For purely cosmetic reasons, clear up trailing commas. */ |
9d1f7ab2 MS |
2933 | if (',' == display_buf[n-1]) |
2934 | display_buf[n-1] = ' '; | |
2935 | return display_buf; | |
c5aa993b | 2936 | } |
0f71a2f6 | 2937 | } |
9d1f7ab2 | 2938 | return NULL; |
0f71a2f6 | 2939 | } |
c906108c | 2940 | \f |
c5aa993b | 2941 | |
0fb4aa4b PA |
2942 | static int |
2943 | remote_static_tracepoint_marker_at (CORE_ADDR addr, | |
2944 | struct static_tracepoint_marker *marker) | |
2945 | { | |
2946 | struct remote_state *rs = get_remote_state (); | |
2947 | char *p = rs->buf; | |
2948 | ||
bba74b36 | 2949 | xsnprintf (p, get_remote_packet_size (), "qTSTMat:"); |
0fb4aa4b PA |
2950 | p += strlen (p); |
2951 | p += hexnumstr (p, addr); | |
2952 | putpkt (rs->buf); | |
2953 | getpkt (&rs->buf, &rs->buf_size, 0); | |
2954 | p = rs->buf; | |
2955 | ||
2956 | if (*p == 'E') | |
2957 | error (_("Remote failure reply: %s"), p); | |
2958 | ||
2959 | if (*p++ == 'm') | |
2960 | { | |
2961 | parse_static_tracepoint_marker_definition (p, &p, marker); | |
2962 | return 1; | |
2963 | } | |
2964 | ||
2965 | return 0; | |
2966 | } | |
2967 | ||
0fb4aa4b PA |
2968 | static VEC(static_tracepoint_marker_p) * |
2969 | remote_static_tracepoint_markers_by_strid (const char *strid) | |
2970 | { | |
2971 | struct remote_state *rs = get_remote_state (); | |
2972 | VEC(static_tracepoint_marker_p) *markers = NULL; | |
2973 | struct static_tracepoint_marker *marker = NULL; | |
2974 | struct cleanup *old_chain; | |
2975 | char *p; | |
2976 | ||
2977 | /* Ask for a first packet of static tracepoint marker | |
2978 | definition. */ | |
2979 | putpkt ("qTfSTM"); | |
2980 | getpkt (&rs->buf, &rs->buf_size, 0); | |
2981 | p = rs->buf; | |
2982 | if (*p == 'E') | |
2983 | error (_("Remote failure reply: %s"), p); | |
2984 | ||
2985 | old_chain = make_cleanup (free_current_marker, &marker); | |
2986 | ||
2987 | while (*p++ == 'm') | |
2988 | { | |
2989 | if (marker == NULL) | |
2990 | marker = XCNEW (struct static_tracepoint_marker); | |
2991 | ||
2992 | do | |
2993 | { | |
2994 | parse_static_tracepoint_marker_definition (p, &p, marker); | |
2995 | ||
2996 | if (strid == NULL || strcmp (strid, marker->str_id) == 0) | |
2997 | { | |
2998 | VEC_safe_push (static_tracepoint_marker_p, | |
2999 | markers, marker); | |
3000 | marker = NULL; | |
3001 | } | |
3002 | else | |
3003 | { | |
3004 | release_static_tracepoint_marker (marker); | |
3005 | memset (marker, 0, sizeof (*marker)); | |
3006 | } | |
3007 | } | |
3008 | while (*p++ == ','); /* comma-separated list */ | |
3009 | /* Ask for another packet of static tracepoint definition. */ | |
3010 | putpkt ("qTsSTM"); | |
3011 | getpkt (&rs->buf, &rs->buf_size, 0); | |
3012 | p = rs->buf; | |
3013 | } | |
3014 | ||
3015 | do_cleanups (old_chain); | |
3016 | return markers; | |
3017 | } | |
3018 | ||
3019 | \f | |
10760264 JB |
3020 | /* Implement the to_get_ada_task_ptid function for the remote targets. */ |
3021 | ||
3022 | static ptid_t | |
3023 | remote_get_ada_task_ptid (long lwp, long thread) | |
3024 | { | |
3025 | return ptid_build (ptid_get_pid (inferior_ptid), 0, lwp); | |
3026 | } | |
3027 | \f | |
3028 | ||
24b06219 | 3029 | /* Restart the remote side; this is an extended protocol operation. */ |
c906108c SS |
3030 | |
3031 | static void | |
fba45db2 | 3032 | extended_remote_restart (void) |
c906108c | 3033 | { |
d01949b6 | 3034 | struct remote_state *rs = get_remote_state (); |
c906108c SS |
3035 | |
3036 | /* Send the restart command; for reasons I don't understand the | |
3037 | remote side really expects a number after the "R". */ | |
ea9c271d | 3038 | xsnprintf (rs->buf, get_remote_packet_size (), "R%x", 0); |
6d820c5c | 3039 | putpkt (rs->buf); |
c906108c | 3040 | |
ad9a8f3f | 3041 | remote_fileio_reset (); |
c906108c SS |
3042 | } |
3043 | \f | |
3044 | /* Clean up connection to a remote debugger. */ | |
3045 | ||
c906108c | 3046 | static void |
460014f5 | 3047 | remote_close (void) |
c906108c | 3048 | { |
5d93a237 TT |
3049 | struct remote_state *rs = get_remote_state (); |
3050 | ||
3051 | if (rs->remote_desc == NULL) | |
d3fd5342 PA |
3052 | return; /* already closed */ |
3053 | ||
3054 | /* Make sure we leave stdin registered in the event loop, and we | |
3055 | don't leave the async SIGINT signal handler installed. */ | |
3056 | remote_terminal_ours (); | |
ce5ce7ed | 3057 | |
5d93a237 TT |
3058 | serial_close (rs->remote_desc); |
3059 | rs->remote_desc = NULL; | |
ce5ce7ed PA |
3060 | |
3061 | /* We don't have a connection to the remote stub anymore. Get rid | |
f67fd822 PM |
3062 | of all the inferiors and their threads we were controlling. |
3063 | Reset inferior_ptid to null_ptid first, as otherwise has_stack_frame | |
3064 | will be unable to find the thread corresponding to (pid, 0, 0). */ | |
0f2caa1b | 3065 | inferior_ptid = null_ptid; |
f67fd822 | 3066 | discard_all_inferiors (); |
ce5ce7ed | 3067 | |
f48ff2a7 YQ |
3068 | /* We are closing the remote target, so we should discard |
3069 | everything of this target. */ | |
bcc75809 | 3070 | discard_pending_stop_replies_in_queue (rs); |
74531fed PA |
3071 | |
3072 | if (remote_async_inferior_event_token) | |
3073 | delete_async_event_handler (&remote_async_inferior_event_token); | |
722247f1 | 3074 | |
5965e028 | 3075 | remote_notif_state_xfree (rs->notif_state); |
aef525cb YQ |
3076 | |
3077 | trace_reset_local_state (); | |
c906108c SS |
3078 | } |
3079 | ||
23860348 | 3080 | /* Query the remote side for the text, data and bss offsets. */ |
c906108c SS |
3081 | |
3082 | static void | |
fba45db2 | 3083 | get_offsets (void) |
c906108c | 3084 | { |
d01949b6 | 3085 | struct remote_state *rs = get_remote_state (); |
2e9f7625 | 3086 | char *buf; |
085dd6e6 | 3087 | char *ptr; |
31d99776 DJ |
3088 | int lose, num_segments = 0, do_sections, do_segments; |
3089 | CORE_ADDR text_addr, data_addr, bss_addr, segments[2]; | |
c906108c | 3090 | struct section_offsets *offs; |
31d99776 DJ |
3091 | struct symfile_segment_data *data; |
3092 | ||
3093 | if (symfile_objfile == NULL) | |
3094 | return; | |
c906108c SS |
3095 | |
3096 | putpkt ("qOffsets"); | |
6d820c5c | 3097 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 3098 | buf = rs->buf; |
c906108c SS |
3099 | |
3100 | if (buf[0] == '\000') | |
3101 | return; /* Return silently. Stub doesn't support | |
23860348 | 3102 | this command. */ |
c906108c SS |
3103 | if (buf[0] == 'E') |
3104 | { | |
8a3fe4f8 | 3105 | warning (_("Remote failure reply: %s"), buf); |
c906108c SS |
3106 | return; |
3107 | } | |
3108 | ||
3109 | /* Pick up each field in turn. This used to be done with scanf, but | |
3110 | scanf will make trouble if CORE_ADDR size doesn't match | |
3111 | conversion directives correctly. The following code will work | |
3112 | with any size of CORE_ADDR. */ | |
3113 | text_addr = data_addr = bss_addr = 0; | |
3114 | ptr = buf; | |
3115 | lose = 0; | |
3116 | ||
3117 | if (strncmp (ptr, "Text=", 5) == 0) | |
3118 | { | |
3119 | ptr += 5; | |
3120 | /* Don't use strtol, could lose on big values. */ | |
3121 | while (*ptr && *ptr != ';') | |
3122 | text_addr = (text_addr << 4) + fromhex (*ptr++); | |
c906108c | 3123 | |
31d99776 DJ |
3124 | if (strncmp (ptr, ";Data=", 6) == 0) |
3125 | { | |
3126 | ptr += 6; | |
3127 | while (*ptr && *ptr != ';') | |
3128 | data_addr = (data_addr << 4) + fromhex (*ptr++); | |
3129 | } | |
3130 | else | |
3131 | lose = 1; | |
3132 | ||
3133 | if (!lose && strncmp (ptr, ";Bss=", 5) == 0) | |
3134 | { | |
3135 | ptr += 5; | |
3136 | while (*ptr && *ptr != ';') | |
3137 | bss_addr = (bss_addr << 4) + fromhex (*ptr++); | |
c906108c | 3138 | |
31d99776 DJ |
3139 | if (bss_addr != data_addr) |
3140 | warning (_("Target reported unsupported offsets: %s"), buf); | |
3141 | } | |
3142 | else | |
3143 | lose = 1; | |
3144 | } | |
3145 | else if (strncmp (ptr, "TextSeg=", 8) == 0) | |
c906108c | 3146 | { |
31d99776 DJ |
3147 | ptr += 8; |
3148 | /* Don't use strtol, could lose on big values. */ | |
c906108c | 3149 | while (*ptr && *ptr != ';') |
31d99776 DJ |
3150 | text_addr = (text_addr << 4) + fromhex (*ptr++); |
3151 | num_segments = 1; | |
3152 | ||
3153 | if (strncmp (ptr, ";DataSeg=", 9) == 0) | |
3154 | { | |
3155 | ptr += 9; | |
3156 | while (*ptr && *ptr != ';') | |
3157 | data_addr = (data_addr << 4) + fromhex (*ptr++); | |
3158 | num_segments++; | |
3159 | } | |
c906108c SS |
3160 | } |
3161 | else | |
3162 | lose = 1; | |
3163 | ||
3164 | if (lose) | |
8a3fe4f8 | 3165 | error (_("Malformed response to offset query, %s"), buf); |
31d99776 DJ |
3166 | else if (*ptr != '\0') |
3167 | warning (_("Target reported unsupported offsets: %s"), buf); | |
c906108c | 3168 | |
802188a7 | 3169 | offs = ((struct section_offsets *) |
a39a16c4 | 3170 | alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections))); |
802188a7 | 3171 | memcpy (offs, symfile_objfile->section_offsets, |
a39a16c4 | 3172 | SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections)); |
c906108c | 3173 | |
31d99776 DJ |
3174 | data = get_symfile_segment_data (symfile_objfile->obfd); |
3175 | do_segments = (data != NULL); | |
3176 | do_sections = num_segments == 0; | |
c906108c | 3177 | |
28c32713 | 3178 | if (num_segments > 0) |
31d99776 | 3179 | { |
31d99776 DJ |
3180 | segments[0] = text_addr; |
3181 | segments[1] = data_addr; | |
3182 | } | |
28c32713 JB |
3183 | /* If we have two segments, we can still try to relocate everything |
3184 | by assuming that the .text and .data offsets apply to the whole | |
3185 | text and data segments. Convert the offsets given in the packet | |
3186 | to base addresses for symfile_map_offsets_to_segments. */ | |
3187 | else if (data && data->num_segments == 2) | |
3188 | { | |
3189 | segments[0] = data->segment_bases[0] + text_addr; | |
3190 | segments[1] = data->segment_bases[1] + data_addr; | |
3191 | num_segments = 2; | |
3192 | } | |
8d385431 DJ |
3193 | /* If the object file has only one segment, assume that it is text |
3194 | rather than data; main programs with no writable data are rare, | |
3195 | but programs with no code are useless. Of course the code might | |
3196 | have ended up in the data segment... to detect that we would need | |
3197 | the permissions here. */ | |
3198 | else if (data && data->num_segments == 1) | |
3199 | { | |
3200 | segments[0] = data->segment_bases[0] + text_addr; | |
3201 | num_segments = 1; | |
3202 | } | |
28c32713 JB |
3203 | /* There's no way to relocate by segment. */ |
3204 | else | |
3205 | do_segments = 0; | |
31d99776 DJ |
3206 | |
3207 | if (do_segments) | |
3208 | { | |
3209 | int ret = symfile_map_offsets_to_segments (symfile_objfile->obfd, data, | |
3210 | offs, num_segments, segments); | |
3211 | ||
3212 | if (ret == 0 && !do_sections) | |
3e43a32a MS |
3213 | error (_("Can not handle qOffsets TextSeg " |
3214 | "response with this symbol file")); | |
31d99776 DJ |
3215 | |
3216 | if (ret > 0) | |
3217 | do_sections = 0; | |
3218 | } | |
c906108c | 3219 | |
9ef895d6 DJ |
3220 | if (data) |
3221 | free_symfile_segment_data (data); | |
31d99776 DJ |
3222 | |
3223 | if (do_sections) | |
3224 | { | |
3225 | offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr; | |
3226 | ||
3e43a32a MS |
3227 | /* This is a temporary kludge to force data and bss to use the |
3228 | same offsets because that's what nlmconv does now. The real | |
3229 | solution requires changes to the stub and remote.c that I | |
3230 | don't have time to do right now. */ | |
31d99776 DJ |
3231 | |
3232 | offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr; | |
3233 | offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr; | |
3234 | } | |
c906108c SS |
3235 | |
3236 | objfile_relocate (symfile_objfile, offs); | |
3237 | } | |
3238 | ||
74531fed PA |
3239 | /* Callback for iterate_over_threads. Set the STOP_REQUESTED flags in |
3240 | threads we know are stopped already. This is used during the | |
3241 | initial remote connection in non-stop mode --- threads that are | |
3242 | reported as already being stopped are left stopped. */ | |
3243 | ||
3244 | static int | |
3245 | set_stop_requested_callback (struct thread_info *thread, void *data) | |
3246 | { | |
3247 | /* If we have a stop reply for this thread, it must be stopped. */ | |
3248 | if (peek_stop_reply (thread->ptid)) | |
3249 | set_stop_requested (thread->ptid, 1); | |
3250 | ||
3251 | return 0; | |
3252 | } | |
3253 | ||
9a7071a8 JB |
3254 | /* Send interrupt_sequence to remote target. */ |
3255 | static void | |
eeae04df | 3256 | send_interrupt_sequence (void) |
9a7071a8 | 3257 | { |
5d93a237 TT |
3258 | struct remote_state *rs = get_remote_state (); |
3259 | ||
9a7071a8 | 3260 | if (interrupt_sequence_mode == interrupt_sequence_control_c) |
c33e31fd | 3261 | remote_serial_write ("\x03", 1); |
9a7071a8 | 3262 | else if (interrupt_sequence_mode == interrupt_sequence_break) |
5d93a237 | 3263 | serial_send_break (rs->remote_desc); |
9a7071a8 JB |
3264 | else if (interrupt_sequence_mode == interrupt_sequence_break_g) |
3265 | { | |
5d93a237 | 3266 | serial_send_break (rs->remote_desc); |
c33e31fd | 3267 | remote_serial_write ("g", 1); |
9a7071a8 JB |
3268 | } |
3269 | else | |
3270 | internal_error (__FILE__, __LINE__, | |
3271 | _("Invalid value for interrupt_sequence_mode: %s."), | |
3272 | interrupt_sequence_mode); | |
3273 | } | |
3274 | ||
3405876a PA |
3275 | |
3276 | /* If STOP_REPLY is a T stop reply, look for the "thread" register, | |
3277 | and extract the PTID. Returns NULL_PTID if not found. */ | |
3278 | ||
3279 | static ptid_t | |
3280 | stop_reply_extract_thread (char *stop_reply) | |
3281 | { | |
3282 | if (stop_reply[0] == 'T' && strlen (stop_reply) > 3) | |
3283 | { | |
3284 | char *p; | |
3285 | ||
3286 | /* Txx r:val ; r:val (...) */ | |
3287 | p = &stop_reply[3]; | |
3288 | ||
3289 | /* Look for "register" named "thread". */ | |
3290 | while (*p != '\0') | |
3291 | { | |
3292 | char *p1; | |
3293 | ||
3294 | p1 = strchr (p, ':'); | |
3295 | if (p1 == NULL) | |
3296 | return null_ptid; | |
3297 | ||
3298 | if (strncmp (p, "thread", p1 - p) == 0) | |
3299 | return read_ptid (++p1, &p); | |
3300 | ||
3301 | p1 = strchr (p, ';'); | |
3302 | if (p1 == NULL) | |
3303 | return null_ptid; | |
3304 | p1++; | |
3305 | ||
3306 | p = p1; | |
3307 | } | |
3308 | } | |
3309 | ||
3310 | return null_ptid; | |
3311 | } | |
3312 | ||
49c62f2e PA |
3313 | /* Query the remote target for which is the current thread/process, |
3314 | add it to our tables, and update INFERIOR_PTID. The caller is | |
3315 | responsible for setting the state such that the remote end is ready | |
3405876a PA |
3316 | to return the current thread. |
3317 | ||
3318 | This function is called after handling the '?' or 'vRun' packets, | |
3319 | whose response is a stop reply from which we can also try | |
3320 | extracting the thread. If the target doesn't support the explicit | |
3321 | qC query, we infer the current thread from that stop reply, passed | |
3322 | in in WAIT_STATUS, which may be NULL. */ | |
49c62f2e PA |
3323 | |
3324 | static void | |
3405876a | 3325 | add_current_inferior_and_thread (char *wait_status) |
49c62f2e PA |
3326 | { |
3327 | struct remote_state *rs = get_remote_state (); | |
3328 | int fake_pid_p = 0; | |
3405876a | 3329 | ptid_t ptid = null_ptid; |
49c62f2e PA |
3330 | |
3331 | inferior_ptid = null_ptid; | |
3332 | ||
3405876a PA |
3333 | /* Now, if we have thread information, update inferior_ptid. First |
3334 | if we have a stop reply handy, maybe it's a T stop reply with a | |
3335 | "thread" register we can extract the current thread from. If | |
3336 | not, ask the remote which is the current thread, with qC. The | |
3337 | former method avoids a roundtrip. Note we don't use | |
3338 | remote_parse_stop_reply as that makes use of the target | |
3339 | architecture, which we haven't yet fully determined at this | |
3340 | point. */ | |
3341 | if (wait_status != NULL) | |
3342 | ptid = stop_reply_extract_thread (wait_status); | |
3343 | if (ptid_equal (ptid, null_ptid)) | |
3344 | ptid = remote_current_thread (inferior_ptid); | |
3345 | ||
49c62f2e PA |
3346 | if (!ptid_equal (ptid, null_ptid)) |
3347 | { | |
3348 | if (!remote_multi_process_p (rs)) | |
3349 | fake_pid_p = 1; | |
3350 | ||
3351 | inferior_ptid = ptid; | |
3352 | } | |
3353 | else | |
3354 | { | |
3355 | /* Without this, some commands which require an active target | |
3356 | (such as kill) won't work. This variable serves (at least) | |
3357 | double duty as both the pid of the target process (if it has | |
3358 | such), and as a flag indicating that a target is active. */ | |
3359 | inferior_ptid = magic_null_ptid; | |
3360 | fake_pid_p = 1; | |
3361 | } | |
3362 | ||
3363 | remote_add_inferior (fake_pid_p, ptid_get_pid (inferior_ptid), -1); | |
3364 | ||
3365 | /* Add the main thread. */ | |
3366 | add_thread_silent (inferior_ptid); | |
3367 | } | |
3368 | ||
9cbc821d | 3369 | static void |
04bd08de | 3370 | remote_start_remote (int from_tty, struct target_ops *target, int extended_p) |
c906108c | 3371 | { |
c8d104ad PA |
3372 | struct remote_state *rs = get_remote_state (); |
3373 | struct packet_config *noack_config; | |
2d717e4f | 3374 | char *wait_status = NULL; |
8621d6a9 | 3375 | |
23860348 | 3376 | immediate_quit++; /* Allow user to interrupt it. */ |
522002f9 | 3377 | QUIT; |
c906108c | 3378 | |
9a7071a8 JB |
3379 | if (interrupt_on_connect) |
3380 | send_interrupt_sequence (); | |
3381 | ||
57e12211 | 3382 | /* Ack any packet which the remote side has already sent. */ |
5d93a237 | 3383 | serial_write (rs->remote_desc, "+", 1); |
57e12211 | 3384 | |
1e51243a PA |
3385 | /* Signal other parts that we're going through the initial setup, |
3386 | and so things may not be stable yet. */ | |
3387 | rs->starting_up = 1; | |
3388 | ||
c8d104ad PA |
3389 | /* The first packet we send to the target is the optional "supported |
3390 | packets" request. If the target can answer this, it will tell us | |
3391 | which later probes to skip. */ | |
3392 | remote_query_supported (); | |
3393 | ||
d914c394 SS |
3394 | /* If the stub wants to get a QAllow, compose one and send it. */ |
3395 | if (remote_protocol_packets[PACKET_QAllow].support != PACKET_DISABLE) | |
3396 | remote_set_permissions (); | |
3397 | ||
c8d104ad PA |
3398 | /* Next, we possibly activate noack mode. |
3399 | ||
3400 | If the QStartNoAckMode packet configuration is set to AUTO, | |
3401 | enable noack mode if the stub reported a wish for it with | |
3402 | qSupported. | |
3403 | ||
3404 | If set to TRUE, then enable noack mode even if the stub didn't | |
3405 | report it in qSupported. If the stub doesn't reply OK, the | |
3406 | session ends with an error. | |
3407 | ||
3408 | If FALSE, then don't activate noack mode, regardless of what the | |
3409 | stub claimed should be the default with qSupported. */ | |
3410 | ||
3411 | noack_config = &remote_protocol_packets[PACKET_QStartNoAckMode]; | |
3412 | ||
3413 | if (noack_config->detect == AUTO_BOOLEAN_TRUE | |
3414 | || (noack_config->detect == AUTO_BOOLEAN_AUTO | |
3415 | && noack_config->support == PACKET_ENABLE)) | |
3416 | { | |
3417 | putpkt ("QStartNoAckMode"); | |
3418 | getpkt (&rs->buf, &rs->buf_size, 0); | |
3419 | if (packet_ok (rs->buf, noack_config) == PACKET_OK) | |
3420 | rs->noack_mode = 1; | |
3421 | } | |
3422 | ||
04bd08de | 3423 | if (extended_p) |
5fe04517 PA |
3424 | { |
3425 | /* Tell the remote that we are using the extended protocol. */ | |
3426 | putpkt ("!"); | |
3427 | getpkt (&rs->buf, &rs->buf_size, 0); | |
3428 | } | |
3429 | ||
9b224c5e PA |
3430 | /* Let the target know which signals it is allowed to pass down to |
3431 | the program. */ | |
3432 | update_signals_program_target (); | |
3433 | ||
d962ef82 DJ |
3434 | /* Next, if the target can specify a description, read it. We do |
3435 | this before anything involving memory or registers. */ | |
3436 | target_find_description (); | |
3437 | ||
6c95b8df PA |
3438 | /* Next, now that we know something about the target, update the |
3439 | address spaces in the program spaces. */ | |
3440 | update_address_spaces (); | |
3441 | ||
50c71eaf PA |
3442 | /* On OSs where the list of libraries is global to all |
3443 | processes, we fetch them early. */ | |
f5656ead | 3444 | if (gdbarch_has_global_solist (target_gdbarch ())) |
04bd08de | 3445 | solib_add (NULL, from_tty, target, auto_solib_add); |
50c71eaf | 3446 | |
74531fed PA |
3447 | if (non_stop) |
3448 | { | |
3449 | if (!rs->non_stop_aware) | |
3e43a32a MS |
3450 | error (_("Non-stop mode requested, but remote " |
3451 | "does not support non-stop")); | |
74531fed PA |
3452 | |
3453 | putpkt ("QNonStop:1"); | |
3454 | getpkt (&rs->buf, &rs->buf_size, 0); | |
3455 | ||
3456 | if (strcmp (rs->buf, "OK") != 0) | |
9b20d036 | 3457 | error (_("Remote refused setting non-stop mode with: %s"), rs->buf); |
74531fed PA |
3458 | |
3459 | /* Find about threads and processes the stub is already | |
3460 | controlling. We default to adding them in the running state. | |
3461 | The '?' query below will then tell us about which threads are | |
3462 | stopped. */ | |
04bd08de | 3463 | remote_threads_info (target); |
74531fed PA |
3464 | } |
3465 | else if (rs->non_stop_aware) | |
3466 | { | |
3467 | /* Don't assume that the stub can operate in all-stop mode. | |
e6f3fa52 | 3468 | Request it explicitly. */ |
74531fed PA |
3469 | putpkt ("QNonStop:0"); |
3470 | getpkt (&rs->buf, &rs->buf_size, 0); | |
3471 | ||
3472 | if (strcmp (rs->buf, "OK") != 0) | |
9b20d036 | 3473 | error (_("Remote refused setting all-stop mode with: %s"), rs->buf); |
74531fed PA |
3474 | } |
3475 | ||
a0743c90 YQ |
3476 | /* Upload TSVs regardless of whether the target is running or not. The |
3477 | remote stub, such as GDBserver, may have some predefined or builtin | |
3478 | TSVs, even if the target is not running. */ | |
3479 | if (remote_get_trace_status (current_trace_status ()) != -1) | |
3480 | { | |
3481 | struct uploaded_tsv *uploaded_tsvs = NULL; | |
3482 | ||
3483 | remote_upload_trace_state_variables (&uploaded_tsvs); | |
3484 | merge_uploaded_trace_state_variables (&uploaded_tsvs); | |
3485 | } | |
3486 | ||
2d717e4f DJ |
3487 | /* Check whether the target is running now. */ |
3488 | putpkt ("?"); | |
3489 | getpkt (&rs->buf, &rs->buf_size, 0); | |
3490 | ||
74531fed | 3491 | if (!non_stop) |
2d717e4f | 3492 | { |
e714e1bf UW |
3493 | ptid_t ptid; |
3494 | int fake_pid_p = 0; | |
3495 | struct inferior *inf; | |
3496 | ||
74531fed | 3497 | if (rs->buf[0] == 'W' || rs->buf[0] == 'X') |
2d717e4f | 3498 | { |
04bd08de | 3499 | if (!extended_p) |
74531fed | 3500 | error (_("The target is not running (try extended-remote?)")); |
c35b1492 PA |
3501 | |
3502 | /* We're connected, but not running. Drop out before we | |
3503 | call start_remote. */ | |
e278ad5b | 3504 | rs->starting_up = 0; |
c35b1492 | 3505 | return; |
2d717e4f DJ |
3506 | } |
3507 | else | |
74531fed | 3508 | { |
74531fed PA |
3509 | /* Save the reply for later. */ |
3510 | wait_status = alloca (strlen (rs->buf) + 1); | |
3511 | strcpy (wait_status, rs->buf); | |
3512 | } | |
3513 | ||
3514 | /* Let the stub know that we want it to return the thread. */ | |
3515 | set_continue_thread (minus_one_ptid); | |
3516 | ||
3405876a | 3517 | add_current_inferior_and_thread (wait_status); |
74531fed | 3518 | |
6e586cc5 YQ |
3519 | /* init_wait_for_inferior should be called before get_offsets in order |
3520 | to manage `inserted' flag in bp loc in a correct state. | |
3521 | breakpoint_init_inferior, called from init_wait_for_inferior, set | |
3522 | `inserted' flag to 0, while before breakpoint_re_set, called from | |
3523 | start_remote, set `inserted' flag to 1. In the initialization of | |
3524 | inferior, breakpoint_init_inferior should be called first, and then | |
3525 | breakpoint_re_set can be called. If this order is broken, state of | |
3526 | `inserted' flag is wrong, and cause some problems on breakpoint | |
3527 | manipulation. */ | |
3528 | init_wait_for_inferior (); | |
3529 | ||
74531fed PA |
3530 | get_offsets (); /* Get text, data & bss offsets. */ |
3531 | ||
d962ef82 DJ |
3532 | /* If we could not find a description using qXfer, and we know |
3533 | how to do it some other way, try again. This is not | |
3534 | supported for non-stop; it could be, but it is tricky if | |
3535 | there are no stopped threads when we connect. */ | |
04bd08de | 3536 | if (remote_read_description_p (target) |
f5656ead | 3537 | && gdbarch_target_desc (target_gdbarch ()) == NULL) |
d962ef82 DJ |
3538 | { |
3539 | target_clear_description (); | |
3540 | target_find_description (); | |
3541 | } | |
3542 | ||
74531fed PA |
3543 | /* Use the previously fetched status. */ |
3544 | gdb_assert (wait_status != NULL); | |
3545 | strcpy (rs->buf, wait_status); | |
3546 | rs->cached_wait_status = 1; | |
3547 | ||
3548 | immediate_quit--; | |
04bd08de | 3549 | start_remote (from_tty); /* Initialize gdb process mechanisms. */ |
2d717e4f DJ |
3550 | } |
3551 | else | |
3552 | { | |
68c97600 PA |
3553 | /* Clear WFI global state. Do this before finding about new |
3554 | threads and inferiors, and setting the current inferior. | |
3555 | Otherwise we would clear the proceed status of the current | |
3556 | inferior when we want its stop_soon state to be preserved | |
3557 | (see notice_new_inferior). */ | |
3558 | init_wait_for_inferior (); | |
3559 | ||
74531fed PA |
3560 | /* In non-stop, we will either get an "OK", meaning that there |
3561 | are no stopped threads at this time; or, a regular stop | |
3562 | reply. In the latter case, there may be more than one thread | |
3563 | stopped --- we pull them all out using the vStopped | |
3564 | mechanism. */ | |
3565 | if (strcmp (rs->buf, "OK") != 0) | |
3566 | { | |
722247f1 | 3567 | struct notif_client *notif = ¬if_client_stop; |
2d717e4f | 3568 | |
722247f1 YQ |
3569 | /* remote_notif_get_pending_replies acks this one, and gets |
3570 | the rest out. */ | |
f48ff2a7 | 3571 | rs->notif_state->pending_event[notif_client_stop.id] |
722247f1 YQ |
3572 | = remote_notif_parse (notif, rs->buf); |
3573 | remote_notif_get_pending_events (notif); | |
c906108c | 3574 | |
74531fed PA |
3575 | /* Make sure that threads that were stopped remain |
3576 | stopped. */ | |
3577 | iterate_over_threads (set_stop_requested_callback, NULL); | |
3578 | } | |
2d717e4f | 3579 | |
74531fed PA |
3580 | if (target_can_async_p ()) |
3581 | target_async (inferior_event_handler, 0); | |
c906108c | 3582 | |
74531fed PA |
3583 | if (thread_count () == 0) |
3584 | { | |
04bd08de | 3585 | if (!extended_p) |
74531fed | 3586 | error (_("The target is not running (try extended-remote?)")); |
82f73884 | 3587 | |
c35b1492 PA |
3588 | /* We're connected, but not running. Drop out before we |
3589 | call start_remote. */ | |
e278ad5b | 3590 | rs->starting_up = 0; |
c35b1492 PA |
3591 | return; |
3592 | } | |
74531fed PA |
3593 | |
3594 | /* Let the stub know that we want it to return the thread. */ | |
c0a2216e | 3595 | |
74531fed PA |
3596 | /* Force the stub to choose a thread. */ |
3597 | set_general_thread (null_ptid); | |
c906108c | 3598 | |
74531fed PA |
3599 | /* Query it. */ |
3600 | inferior_ptid = remote_current_thread (minus_one_ptid); | |
3601 | if (ptid_equal (inferior_ptid, minus_one_ptid)) | |
3602 | error (_("remote didn't report the current thread in non-stop mode")); | |
c906108c | 3603 | |
74531fed PA |
3604 | get_offsets (); /* Get text, data & bss offsets. */ |
3605 | ||
3606 | /* In non-stop mode, any cached wait status will be stored in | |
3607 | the stop reply queue. */ | |
3608 | gdb_assert (wait_status == NULL); | |
f0223081 | 3609 | |
2455069d UW |
3610 | /* Report all signals during attach/startup. */ |
3611 | remote_pass_signals (0, NULL); | |
74531fed | 3612 | } |
c8d104ad | 3613 | |
c8d104ad PA |
3614 | /* If we connected to a live target, do some additional setup. */ |
3615 | if (target_has_execution) | |
3616 | { | |
3617 | if (exec_bfd) /* No use without an exec file. */ | |
36d25514 | 3618 | remote_check_symbols (); |
c8d104ad | 3619 | } |
50c71eaf | 3620 | |
d5551862 SS |
3621 | /* Possibly the target has been engaged in a trace run started |
3622 | previously; find out where things are at. */ | |
26afc0d7 | 3623 | if (remote_get_trace_status (current_trace_status ()) != -1) |
d5551862 | 3624 | { |
00bf0b85 | 3625 | struct uploaded_tp *uploaded_tps = NULL; |
00bf0b85 | 3626 | |
00bf0b85 SS |
3627 | if (current_trace_status ()->running) |
3628 | printf_filtered (_("Trace is already running on the target.\n")); | |
3629 | ||
00bf0b85 SS |
3630 | remote_upload_tracepoints (&uploaded_tps); |
3631 | ||
3632 | merge_uploaded_tracepoints (&uploaded_tps); | |
d5551862 SS |
3633 | } |
3634 | ||
1e51243a PA |
3635 | /* The thread and inferior lists are now synchronized with the |
3636 | target, our symbols have been relocated, and we're merged the | |
3637 | target's tracepoints with ours. We're done with basic start | |
3638 | up. */ | |
3639 | rs->starting_up = 0; | |
3640 | ||
2567c7d9 | 3641 | /* If breakpoints are global, insert them now. */ |
f5656ead | 3642 | if (gdbarch_has_global_breakpoints (target_gdbarch ()) |
50c71eaf PA |
3643 | && breakpoints_always_inserted_mode ()) |
3644 | insert_breakpoints (); | |
c906108c SS |
3645 | } |
3646 | ||
3647 | /* Open a connection to a remote debugger. | |
3648 | NAME is the filename used for communication. */ | |
3649 | ||
3650 | static void | |
fba45db2 | 3651 | remote_open (char *name, int from_tty) |
c906108c | 3652 | { |
75c99385 | 3653 | remote_open_1 (name, from_tty, &remote_ops, 0); |
43ff13b4 JM |
3654 | } |
3655 | ||
c906108c SS |
3656 | /* Open a connection to a remote debugger using the extended |
3657 | remote gdb protocol. NAME is the filename used for communication. */ | |
3658 | ||
3659 | static void | |
fba45db2 | 3660 | extended_remote_open (char *name, int from_tty) |
c906108c | 3661 | { |
75c99385 | 3662 | remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */); |
43ff13b4 JM |
3663 | } |
3664 | ||
c906108c SS |
3665 | /* Generic code for opening a connection to a remote target. */ |
3666 | ||
d471ea57 AC |
3667 | static void |
3668 | init_all_packet_configs (void) | |
3669 | { | |
3670 | int i; | |
a744cf53 | 3671 | |
444abaca DJ |
3672 | for (i = 0; i < PACKET_MAX; i++) |
3673 | update_packet_config (&remote_protocol_packets[i]); | |
d471ea57 AC |
3674 | } |
3675 | ||
23860348 | 3676 | /* Symbol look-up. */ |
dc8acb97 MS |
3677 | |
3678 | static void | |
36d25514 | 3679 | remote_check_symbols (void) |
dc8acb97 | 3680 | { |
d01949b6 | 3681 | struct remote_state *rs = get_remote_state (); |
dc8acb97 MS |
3682 | char *msg, *reply, *tmp; |
3683 | struct minimal_symbol *sym; | |
3684 | int end; | |
3685 | ||
63154eca PA |
3686 | /* The remote side has no concept of inferiors that aren't running |
3687 | yet, it only knows about running processes. If we're connected | |
3688 | but our current inferior is not running, we should not invite the | |
3689 | remote target to request symbol lookups related to its | |
3690 | (unrelated) current process. */ | |
3691 | if (!target_has_execution) | |
3692 | return; | |
3693 | ||
444abaca | 3694 | if (remote_protocol_packets[PACKET_qSymbol].support == PACKET_DISABLE) |
dc8acb97 MS |
3695 | return; |
3696 | ||
63154eca PA |
3697 | /* Make sure the remote is pointing at the right process. Note |
3698 | there's no way to select "no process". */ | |
3c9c4b83 PA |
3699 | set_general_process (); |
3700 | ||
6d820c5c DJ |
3701 | /* Allocate a message buffer. We can't reuse the input buffer in RS, |
3702 | because we need both at the same time. */ | |
ea9c271d | 3703 | msg = alloca (get_remote_packet_size ()); |
6d820c5c | 3704 | |
23860348 | 3705 | /* Invite target to request symbol lookups. */ |
dc8acb97 MS |
3706 | |
3707 | putpkt ("qSymbol::"); | |
6d820c5c DJ |
3708 | getpkt (&rs->buf, &rs->buf_size, 0); |
3709 | packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSymbol]); | |
2e9f7625 | 3710 | reply = rs->buf; |
dc8acb97 MS |
3711 | |
3712 | while (strncmp (reply, "qSymbol:", 8) == 0) | |
3713 | { | |
3714 | tmp = &reply[8]; | |
cfd77fa1 | 3715 | end = hex2bin (tmp, (gdb_byte *) msg, strlen (tmp) / 2); |
dc8acb97 MS |
3716 | msg[end] = '\0'; |
3717 | sym = lookup_minimal_symbol (msg, NULL, NULL); | |
3718 | if (sym == NULL) | |
ea9c271d | 3719 | xsnprintf (msg, get_remote_packet_size (), "qSymbol::%s", &reply[8]); |
dc8acb97 | 3720 | else |
2bbe3cc1 | 3721 | { |
f5656ead | 3722 | int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8; |
2bbe3cc1 DJ |
3723 | CORE_ADDR sym_addr = SYMBOL_VALUE_ADDRESS (sym); |
3724 | ||
3725 | /* If this is a function address, return the start of code | |
3726 | instead of any data function descriptor. */ | |
f5656ead | 3727 | sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (), |
2bbe3cc1 DJ |
3728 | sym_addr, |
3729 | ¤t_target); | |
3730 | ||
3731 | xsnprintf (msg, get_remote_packet_size (), "qSymbol:%s:%s", | |
5af949e3 | 3732 | phex_nz (sym_addr, addr_size), &reply[8]); |
2bbe3cc1 DJ |
3733 | } |
3734 | ||
dc8acb97 | 3735 | putpkt (msg); |
6d820c5c | 3736 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 3737 | reply = rs->buf; |
dc8acb97 MS |
3738 | } |
3739 | } | |
3740 | ||
9db8d71f DJ |
3741 | static struct serial * |
3742 | remote_serial_open (char *name) | |
3743 | { | |
3744 | static int udp_warning = 0; | |
3745 | ||
3746 | /* FIXME: Parsing NAME here is a hack. But we want to warn here instead | |
3747 | of in ser-tcp.c, because it is the remote protocol assuming that the | |
3748 | serial connection is reliable and not the serial connection promising | |
3749 | to be. */ | |
3750 | if (!udp_warning && strncmp (name, "udp:", 4) == 0) | |
3751 | { | |
3e43a32a MS |
3752 | warning (_("The remote protocol may be unreliable over UDP.\n" |
3753 | "Some events may be lost, rendering further debugging " | |
3754 | "impossible.")); | |
9db8d71f DJ |
3755 | udp_warning = 1; |
3756 | } | |
3757 | ||
3758 | return serial_open (name); | |
3759 | } | |
3760 | ||
d914c394 SS |
3761 | /* Inform the target of our permission settings. The permission flags |
3762 | work without this, but if the target knows the settings, it can do | |
3763 | a couple things. First, it can add its own check, to catch cases | |
3764 | that somehow manage to get by the permissions checks in target | |
3765 | methods. Second, if the target is wired to disallow particular | |
3766 | settings (for instance, a system in the field that is not set up to | |
3767 | be able to stop at a breakpoint), it can object to any unavailable | |
3768 | permissions. */ | |
3769 | ||
3770 | void | |
3771 | remote_set_permissions (void) | |
3772 | { | |
3773 | struct remote_state *rs = get_remote_state (); | |
3774 | ||
bba74b36 YQ |
3775 | xsnprintf (rs->buf, get_remote_packet_size (), "QAllow:" |
3776 | "WriteReg:%x;WriteMem:%x;" | |
3777 | "InsertBreak:%x;InsertTrace:%x;" | |
3778 | "InsertFastTrace:%x;Stop:%x", | |
3779 | may_write_registers, may_write_memory, | |
3780 | may_insert_breakpoints, may_insert_tracepoints, | |
3781 | may_insert_fast_tracepoints, may_stop); | |
d914c394 SS |
3782 | putpkt (rs->buf); |
3783 | getpkt (&rs->buf, &rs->buf_size, 0); | |
3784 | ||
3785 | /* If the target didn't like the packet, warn the user. Do not try | |
3786 | to undo the user's settings, that would just be maddening. */ | |
3787 | if (strcmp (rs->buf, "OK") != 0) | |
7ea6d463 | 3788 | warning (_("Remote refused setting permissions with: %s"), rs->buf); |
d914c394 SS |
3789 | } |
3790 | ||
be2a5f71 DJ |
3791 | /* This type describes each known response to the qSupported |
3792 | packet. */ | |
3793 | struct protocol_feature | |
3794 | { | |
3795 | /* The name of this protocol feature. */ | |
3796 | const char *name; | |
3797 | ||
3798 | /* The default for this protocol feature. */ | |
3799 | enum packet_support default_support; | |
3800 | ||
3801 | /* The function to call when this feature is reported, or after | |
3802 | qSupported processing if the feature is not supported. | |
3803 | The first argument points to this structure. The second | |
3804 | argument indicates whether the packet requested support be | |
3805 | enabled, disabled, or probed (or the default, if this function | |
3806 | is being called at the end of processing and this feature was | |
3807 | not reported). The third argument may be NULL; if not NULL, it | |
3808 | is a NUL-terminated string taken from the packet following | |
3809 | this feature's name and an equals sign. */ | |
3810 | void (*func) (const struct protocol_feature *, enum packet_support, | |
3811 | const char *); | |
3812 | ||
3813 | /* The corresponding packet for this feature. Only used if | |
3814 | FUNC is remote_supported_packet. */ | |
3815 | int packet; | |
3816 | }; | |
3817 | ||
be2a5f71 DJ |
3818 | static void |
3819 | remote_supported_packet (const struct protocol_feature *feature, | |
3820 | enum packet_support support, | |
3821 | const char *argument) | |
3822 | { | |
3823 | if (argument) | |
3824 | { | |
3825 | warning (_("Remote qSupported response supplied an unexpected value for" | |
3826 | " \"%s\"."), feature->name); | |
3827 | return; | |
3828 | } | |
3829 | ||
3830 | if (remote_protocol_packets[feature->packet].support | |
3831 | == PACKET_SUPPORT_UNKNOWN) | |
3832 | remote_protocol_packets[feature->packet].support = support; | |
3833 | } | |
be2a5f71 DJ |
3834 | |
3835 | static void | |
3836 | remote_packet_size (const struct protocol_feature *feature, | |
3837 | enum packet_support support, const char *value) | |
3838 | { | |
3839 | struct remote_state *rs = get_remote_state (); | |
3840 | ||
3841 | int packet_size; | |
3842 | char *value_end; | |
3843 | ||
3844 | if (support != PACKET_ENABLE) | |
3845 | return; | |
3846 | ||
3847 | if (value == NULL || *value == '\0') | |
3848 | { | |
3849 | warning (_("Remote target reported \"%s\" without a size."), | |
3850 | feature->name); | |
3851 | return; | |
3852 | } | |
3853 | ||
3854 | errno = 0; | |
3855 | packet_size = strtol (value, &value_end, 16); | |
3856 | if (errno != 0 || *value_end != '\0' || packet_size < 0) | |
3857 | { | |
3858 | warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."), | |
3859 | feature->name, value); | |
3860 | return; | |
3861 | } | |
3862 | ||
3863 | if (packet_size > MAX_REMOTE_PACKET_SIZE) | |
3864 | { | |
3865 | warning (_("limiting remote suggested packet size (%d bytes) to %d"), | |
3866 | packet_size, MAX_REMOTE_PACKET_SIZE); | |
3867 | packet_size = MAX_REMOTE_PACKET_SIZE; | |
3868 | } | |
3869 | ||
3870 | /* Record the new maximum packet size. */ | |
3871 | rs->explicit_packet_size = packet_size; | |
3872 | } | |
3873 | ||
82f73884 PA |
3874 | static void |
3875 | remote_multi_process_feature (const struct protocol_feature *feature, | |
3876 | enum packet_support support, const char *value) | |
3877 | { | |
3878 | struct remote_state *rs = get_remote_state (); | |
a744cf53 | 3879 | |
82f73884 PA |
3880 | rs->multi_process_aware = (support == PACKET_ENABLE); |
3881 | } | |
3882 | ||
74531fed PA |
3883 | static void |
3884 | remote_non_stop_feature (const struct protocol_feature *feature, | |
3885 | enum packet_support support, const char *value) | |
3886 | { | |
3887 | struct remote_state *rs = get_remote_state (); | |
a744cf53 | 3888 | |
74531fed PA |
3889 | rs->non_stop_aware = (support == PACKET_ENABLE); |
3890 | } | |
3891 | ||
782b2b07 SS |
3892 | static void |
3893 | remote_cond_tracepoint_feature (const struct protocol_feature *feature, | |
3894 | enum packet_support support, | |
3895 | const char *value) | |
3896 | { | |
3897 | struct remote_state *rs = get_remote_state (); | |
a744cf53 | 3898 | |
782b2b07 SS |
3899 | rs->cond_tracepoints = (support == PACKET_ENABLE); |
3900 | } | |
3901 | ||
3788aec7 LM |
3902 | static void |
3903 | remote_cond_breakpoint_feature (const struct protocol_feature *feature, | |
3904 | enum packet_support support, | |
3905 | const char *value) | |
3906 | { | |
3907 | struct remote_state *rs = get_remote_state (); | |
3908 | ||
3909 | rs->cond_breakpoints = (support == PACKET_ENABLE); | |
3910 | } | |
3911 | ||
d3ce09f5 SS |
3912 | static void |
3913 | remote_breakpoint_commands_feature (const struct protocol_feature *feature, | |
3914 | enum packet_support support, | |
3915 | const char *value) | |
3916 | { | |
3917 | struct remote_state *rs = get_remote_state (); | |
3918 | ||
3919 | rs->breakpoint_commands = (support == PACKET_ENABLE); | |
3920 | } | |
3921 | ||
7a697b8d SS |
3922 | static void |
3923 | remote_fast_tracepoint_feature (const struct protocol_feature *feature, | |
3924 | enum packet_support support, | |
3925 | const char *value) | |
3926 | { | |
3927 | struct remote_state *rs = get_remote_state (); | |
a744cf53 | 3928 | |
7a697b8d SS |
3929 | rs->fast_tracepoints = (support == PACKET_ENABLE); |
3930 | } | |
3931 | ||
0fb4aa4b PA |
3932 | static void |
3933 | remote_static_tracepoint_feature (const struct protocol_feature *feature, | |
3934 | enum packet_support support, | |
3935 | const char *value) | |
3936 | { | |
3937 | struct remote_state *rs = get_remote_state (); | |
3938 | ||
3939 | rs->static_tracepoints = (support == PACKET_ENABLE); | |
3940 | } | |
3941 | ||
1e4d1764 YQ |
3942 | static void |
3943 | remote_install_in_trace_feature (const struct protocol_feature *feature, | |
3944 | enum packet_support support, | |
3945 | const char *value) | |
3946 | { | |
3947 | struct remote_state *rs = get_remote_state (); | |
3948 | ||
3949 | rs->install_in_trace = (support == PACKET_ENABLE); | |
3950 | } | |
3951 | ||
d5551862 SS |
3952 | static void |
3953 | remote_disconnected_tracing_feature (const struct protocol_feature *feature, | |
3954 | enum packet_support support, | |
3955 | const char *value) | |
3956 | { | |
3957 | struct remote_state *rs = get_remote_state (); | |
a744cf53 | 3958 | |
d5551862 SS |
3959 | rs->disconnected_tracing = (support == PACKET_ENABLE); |
3960 | } | |
3961 | ||
d248b706 KY |
3962 | static void |
3963 | remote_enable_disable_tracepoint_feature (const struct protocol_feature *feature, | |
3964 | enum packet_support support, | |
3965 | const char *value) | |
3966 | { | |
3967 | struct remote_state *rs = get_remote_state (); | |
3968 | ||
3969 | rs->enable_disable_tracepoints = (support == PACKET_ENABLE); | |
3970 | } | |
3971 | ||
3065dfb6 SS |
3972 | static void |
3973 | remote_string_tracing_feature (const struct protocol_feature *feature, | |
3974 | enum packet_support support, | |
3975 | const char *value) | |
3976 | { | |
3977 | struct remote_state *rs = get_remote_state (); | |
3978 | ||
3979 | rs->string_tracing = (support == PACKET_ENABLE); | |
3980 | } | |
3981 | ||
ced63ec0 GB |
3982 | static void |
3983 | remote_augmented_libraries_svr4_read_feature | |
3984 | (const struct protocol_feature *feature, | |
3985 | enum packet_support support, const char *value) | |
3986 | { | |
3987 | struct remote_state *rs = get_remote_state (); | |
3988 | ||
3989 | rs->augmented_libraries_svr4_read = (support == PACKET_ENABLE); | |
3990 | } | |
3991 | ||
dc473cfb | 3992 | static const struct protocol_feature remote_protocol_features[] = { |
0876f84a | 3993 | { "PacketSize", PACKET_DISABLE, remote_packet_size, -1 }, |
40e57cf2 | 3994 | { "qXfer:auxv:read", PACKET_DISABLE, remote_supported_packet, |
fd79ecee | 3995 | PACKET_qXfer_auxv }, |
23181151 DJ |
3996 | { "qXfer:features:read", PACKET_DISABLE, remote_supported_packet, |
3997 | PACKET_qXfer_features }, | |
cfa9d6d9 DJ |
3998 | { "qXfer:libraries:read", PACKET_DISABLE, remote_supported_packet, |
3999 | PACKET_qXfer_libraries }, | |
2268b414 JK |
4000 | { "qXfer:libraries-svr4:read", PACKET_DISABLE, remote_supported_packet, |
4001 | PACKET_qXfer_libraries_svr4 }, | |
ced63ec0 GB |
4002 | { "augmented-libraries-svr4-read", PACKET_DISABLE, |
4003 | remote_augmented_libraries_svr4_read_feature, -1 }, | |
fd79ecee | 4004 | { "qXfer:memory-map:read", PACKET_DISABLE, remote_supported_packet, |
89be2091 | 4005 | PACKET_qXfer_memory_map }, |
4de6483e UW |
4006 | { "qXfer:spu:read", PACKET_DISABLE, remote_supported_packet, |
4007 | PACKET_qXfer_spu_read }, | |
4008 | { "qXfer:spu:write", PACKET_DISABLE, remote_supported_packet, | |
4009 | PACKET_qXfer_spu_write }, | |
07e059b5 VP |
4010 | { "qXfer:osdata:read", PACKET_DISABLE, remote_supported_packet, |
4011 | PACKET_qXfer_osdata }, | |
dc146f7c VP |
4012 | { "qXfer:threads:read", PACKET_DISABLE, remote_supported_packet, |
4013 | PACKET_qXfer_threads }, | |
b3b9301e PA |
4014 | { "qXfer:traceframe-info:read", PACKET_DISABLE, remote_supported_packet, |
4015 | PACKET_qXfer_traceframe_info }, | |
89be2091 DJ |
4016 | { "QPassSignals", PACKET_DISABLE, remote_supported_packet, |
4017 | PACKET_QPassSignals }, | |
9b224c5e PA |
4018 | { "QProgramSignals", PACKET_DISABLE, remote_supported_packet, |
4019 | PACKET_QProgramSignals }, | |
a6f3e723 SL |
4020 | { "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet, |
4021 | PACKET_QStartNoAckMode }, | |
82f73884 | 4022 | { "multiprocess", PACKET_DISABLE, remote_multi_process_feature, -1 }, |
74531fed | 4023 | { "QNonStop", PACKET_DISABLE, remote_non_stop_feature, -1 }, |
4aa995e1 PA |
4024 | { "qXfer:siginfo:read", PACKET_DISABLE, remote_supported_packet, |
4025 | PACKET_qXfer_siginfo_read }, | |
4026 | { "qXfer:siginfo:write", PACKET_DISABLE, remote_supported_packet, | |
4027 | PACKET_qXfer_siginfo_write }, | |
782b2b07 SS |
4028 | { "ConditionalTracepoints", PACKET_DISABLE, remote_cond_tracepoint_feature, |
4029 | PACKET_ConditionalTracepoints }, | |
3788aec7 LM |
4030 | { "ConditionalBreakpoints", PACKET_DISABLE, remote_cond_breakpoint_feature, |
4031 | PACKET_ConditionalBreakpoints }, | |
d3ce09f5 SS |
4032 | { "BreakpointCommands", PACKET_DISABLE, remote_breakpoint_commands_feature, |
4033 | PACKET_BreakpointCommands }, | |
7a697b8d SS |
4034 | { "FastTracepoints", PACKET_DISABLE, remote_fast_tracepoint_feature, |
4035 | PACKET_FastTracepoints }, | |
0fb4aa4b PA |
4036 | { "StaticTracepoints", PACKET_DISABLE, remote_static_tracepoint_feature, |
4037 | PACKET_StaticTracepoints }, | |
1e4d1764 YQ |
4038 | {"InstallInTrace", PACKET_DISABLE, remote_install_in_trace_feature, |
4039 | PACKET_InstallInTrace}, | |
d5551862 SS |
4040 | { "DisconnectedTracing", PACKET_DISABLE, remote_disconnected_tracing_feature, |
4041 | -1 }, | |
40ab02ce MS |
4042 | { "ReverseContinue", PACKET_DISABLE, remote_supported_packet, |
4043 | PACKET_bc }, | |
4044 | { "ReverseStep", PACKET_DISABLE, remote_supported_packet, | |
4045 | PACKET_bs }, | |
409873ef SS |
4046 | { "TracepointSource", PACKET_DISABLE, remote_supported_packet, |
4047 | PACKET_TracepointSource }, | |
d914c394 SS |
4048 | { "QAllow", PACKET_DISABLE, remote_supported_packet, |
4049 | PACKET_QAllow }, | |
d248b706 KY |
4050 | { "EnableDisableTracepoints", PACKET_DISABLE, |
4051 | remote_enable_disable_tracepoint_feature, -1 }, | |
78d85199 YQ |
4052 | { "qXfer:fdpic:read", PACKET_DISABLE, remote_supported_packet, |
4053 | PACKET_qXfer_fdpic }, | |
169081d0 TG |
4054 | { "qXfer:uib:read", PACKET_DISABLE, remote_supported_packet, |
4055 | PACKET_qXfer_uib }, | |
03583c20 UW |
4056 | { "QDisableRandomization", PACKET_DISABLE, remote_supported_packet, |
4057 | PACKET_QDisableRandomization }, | |
d1feda86 | 4058 | { "QAgent", PACKET_DISABLE, remote_supported_packet, PACKET_QAgent}, |
f6f899bf HAQ |
4059 | { "QTBuffer:size", PACKET_DISABLE, |
4060 | remote_supported_packet, PACKET_QTBuffer_size}, | |
3065dfb6 SS |
4061 | { "tracenz", PACKET_DISABLE, |
4062 | remote_string_tracing_feature, -1 }, | |
9accd112 MM |
4063 | { "Qbtrace:off", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_off }, |
4064 | { "Qbtrace:bts", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_bts }, | |
4065 | { "qXfer:btrace:read", PACKET_DISABLE, remote_supported_packet, | |
4066 | PACKET_qXfer_btrace } | |
be2a5f71 DJ |
4067 | }; |
4068 | ||
c8d5aac9 L |
4069 | static char *remote_support_xml; |
4070 | ||
4071 | /* Register string appended to "xmlRegisters=" in qSupported query. */ | |
4072 | ||
4073 | void | |
6e39997a | 4074 | register_remote_support_xml (const char *xml) |
c8d5aac9 L |
4075 | { |
4076 | #if defined(HAVE_LIBEXPAT) | |
4077 | if (remote_support_xml == NULL) | |
c4f7c687 | 4078 | remote_support_xml = concat ("xmlRegisters=", xml, (char *) NULL); |
c8d5aac9 L |
4079 | else |
4080 | { | |
4081 | char *copy = xstrdup (remote_support_xml + 13); | |
4082 | char *p = strtok (copy, ","); | |
4083 | ||
4084 | do | |
4085 | { | |
4086 | if (strcmp (p, xml) == 0) | |
4087 | { | |
4088 | /* already there */ | |
4089 | xfree (copy); | |
4090 | return; | |
4091 | } | |
4092 | } | |
4093 | while ((p = strtok (NULL, ",")) != NULL); | |
4094 | xfree (copy); | |
4095 | ||
94b0dee1 PA |
4096 | remote_support_xml = reconcat (remote_support_xml, |
4097 | remote_support_xml, ",", xml, | |
4098 | (char *) NULL); | |
c8d5aac9 L |
4099 | } |
4100 | #endif | |
4101 | } | |
4102 | ||
4103 | static char * | |
4104 | remote_query_supported_append (char *msg, const char *append) | |
4105 | { | |
4106 | if (msg) | |
94b0dee1 | 4107 | return reconcat (msg, msg, ";", append, (char *) NULL); |
c8d5aac9 L |
4108 | else |
4109 | return xstrdup (append); | |
4110 | } | |
4111 | ||
be2a5f71 DJ |
4112 | static void |
4113 | remote_query_supported (void) | |
4114 | { | |
4115 | struct remote_state *rs = get_remote_state (); | |
4116 | char *next; | |
4117 | int i; | |
4118 | unsigned char seen [ARRAY_SIZE (remote_protocol_features)]; | |
4119 | ||
4120 | /* The packet support flags are handled differently for this packet | |
4121 | than for most others. We treat an error, a disabled packet, and | |
4122 | an empty response identically: any features which must be reported | |
4123 | to be used will be automatically disabled. An empty buffer | |
4124 | accomplishes this, since that is also the representation for a list | |
4125 | containing no features. */ | |
4126 | ||
4127 | rs->buf[0] = 0; | |
4128 | if (remote_protocol_packets[PACKET_qSupported].support != PACKET_DISABLE) | |
4129 | { | |
c8d5aac9 | 4130 | char *q = NULL; |
94b0dee1 | 4131 | struct cleanup *old_chain = make_cleanup (free_current_contents, &q); |
c8d5aac9 | 4132 | |
901f9912 | 4133 | q = remote_query_supported_append (q, "multiprocess+"); |
c8d5aac9 L |
4134 | |
4135 | if (remote_support_xml) | |
4136 | q = remote_query_supported_append (q, remote_support_xml); | |
4137 | ||
dde08ee1 PA |
4138 | q = remote_query_supported_append (q, "qRelocInsn+"); |
4139 | ||
4140 | q = reconcat (q, "qSupported:", q, (char *) NULL); | |
4141 | putpkt (q); | |
82f73884 | 4142 | |
94b0dee1 PA |
4143 | do_cleanups (old_chain); |
4144 | ||
be2a5f71 DJ |
4145 | getpkt (&rs->buf, &rs->buf_size, 0); |
4146 | ||
4147 | /* If an error occured, warn, but do not return - just reset the | |
4148 | buffer to empty and go on to disable features. */ | |
4149 | if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSupported]) | |
4150 | == PACKET_ERROR) | |
4151 | { | |
4152 | warning (_("Remote failure reply: %s"), rs->buf); | |
4153 | rs->buf[0] = 0; | |
4154 | } | |
4155 | } | |
4156 | ||
4157 | memset (seen, 0, sizeof (seen)); | |
4158 | ||
4159 | next = rs->buf; | |
4160 | while (*next) | |
4161 | { | |
4162 | enum packet_support is_supported; | |
4163 | char *p, *end, *name_end, *value; | |
4164 | ||
4165 | /* First separate out this item from the rest of the packet. If | |
4166 | there's another item after this, we overwrite the separator | |
4167 | (terminated strings are much easier to work with). */ | |
4168 | p = next; | |
4169 | end = strchr (p, ';'); | |
4170 | if (end == NULL) | |
4171 | { | |
4172 | end = p + strlen (p); | |
4173 | next = end; | |
4174 | } | |
4175 | else | |
4176 | { | |
89be2091 DJ |
4177 | *end = '\0'; |
4178 | next = end + 1; | |
4179 | ||
be2a5f71 DJ |
4180 | if (end == p) |
4181 | { | |
4182 | warning (_("empty item in \"qSupported\" response")); | |
4183 | continue; | |
4184 | } | |
be2a5f71 DJ |
4185 | } |
4186 | ||
4187 | name_end = strchr (p, '='); | |
4188 | if (name_end) | |
4189 | { | |
4190 | /* This is a name=value entry. */ | |
4191 | is_supported = PACKET_ENABLE; | |
4192 | value = name_end + 1; | |
4193 | *name_end = '\0'; | |
4194 | } | |
4195 | else | |
4196 | { | |
4197 | value = NULL; | |
4198 | switch (end[-1]) | |
4199 | { | |
4200 | case '+': | |
4201 | is_supported = PACKET_ENABLE; | |
4202 | break; | |
4203 | ||
4204 | case '-': | |
4205 | is_supported = PACKET_DISABLE; | |
4206 | break; | |
4207 | ||
4208 | case '?': | |
4209 | is_supported = PACKET_SUPPORT_UNKNOWN; | |
4210 | break; | |
4211 | ||
4212 | default: | |
3e43a32a MS |
4213 | warning (_("unrecognized item \"%s\" " |
4214 | "in \"qSupported\" response"), p); | |
be2a5f71 DJ |
4215 | continue; |
4216 | } | |
4217 | end[-1] = '\0'; | |
4218 | } | |
4219 | ||
4220 | for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++) | |
4221 | if (strcmp (remote_protocol_features[i].name, p) == 0) | |
4222 | { | |
4223 | const struct protocol_feature *feature; | |
4224 | ||
4225 | seen[i] = 1; | |
4226 | feature = &remote_protocol_features[i]; | |
4227 | feature->func (feature, is_supported, value); | |
4228 | break; | |
4229 | } | |
4230 | } | |
4231 | ||
4232 | /* If we increased the packet size, make sure to increase the global | |
4233 | buffer size also. We delay this until after parsing the entire | |
4234 | qSupported packet, because this is the same buffer we were | |
4235 | parsing. */ | |
4236 | if (rs->buf_size < rs->explicit_packet_size) | |
4237 | { | |
4238 | rs->buf_size = rs->explicit_packet_size; | |
4239 | rs->buf = xrealloc (rs->buf, rs->buf_size); | |
4240 | } | |
4241 | ||
4242 | /* Handle the defaults for unmentioned features. */ | |
4243 | for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++) | |
4244 | if (!seen[i]) | |
4245 | { | |
4246 | const struct protocol_feature *feature; | |
4247 | ||
4248 | feature = &remote_protocol_features[i]; | |
4249 | feature->func (feature, feature->default_support, NULL); | |
4250 | } | |
4251 | } | |
4252 | ||
78a095c3 JK |
4253 | /* Remove any of the remote.c targets from target stack. Upper targets depend |
4254 | on it so remove them first. */ | |
4255 | ||
4256 | static void | |
4257 | remote_unpush_target (void) | |
4258 | { | |
4259 | pop_all_targets_above (process_stratum - 1); | |
4260 | } | |
be2a5f71 | 4261 | |
c906108c | 4262 | static void |
3e43a32a MS |
4263 | remote_open_1 (char *name, int from_tty, |
4264 | struct target_ops *target, int extended_p) | |
c906108c | 4265 | { |
d01949b6 | 4266 | struct remote_state *rs = get_remote_state (); |
a6f3e723 | 4267 | |
c906108c | 4268 | if (name == 0) |
8a3fe4f8 | 4269 | error (_("To open a remote debug connection, you need to specify what\n" |
22e04375 | 4270 | "serial device is attached to the remote system\n" |
8a3fe4f8 | 4271 | "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).")); |
c906108c | 4272 | |
23860348 | 4273 | /* See FIXME above. */ |
c6ebd6cf | 4274 | if (!target_async_permitted) |
92d1e331 | 4275 | wait_forever_enabled_p = 1; |
6426a772 | 4276 | |
2d717e4f | 4277 | /* If we're connected to a running target, target_preopen will kill it. |
78a095c3 JK |
4278 | Ask this question first, before target_preopen has a chance to kill |
4279 | anything. */ | |
5d93a237 | 4280 | if (rs->remote_desc != NULL && !have_inferiors ()) |
2d717e4f | 4281 | { |
78a095c3 JK |
4282 | if (from_tty |
4283 | && !query (_("Already connected to a remote target. Disconnect? "))) | |
2d717e4f DJ |
4284 | error (_("Still connected.")); |
4285 | } | |
4286 | ||
78a095c3 | 4287 | /* Here the possibly existing remote target gets unpushed. */ |
c906108c SS |
4288 | target_preopen (from_tty); |
4289 | ||
89be2091 | 4290 | /* Make sure we send the passed signals list the next time we resume. */ |
747dc59d TT |
4291 | xfree (rs->last_pass_packet); |
4292 | rs->last_pass_packet = NULL; | |
89be2091 | 4293 | |
9b224c5e PA |
4294 | /* Make sure we send the program signals list the next time we |
4295 | resume. */ | |
5e4a05c4 TT |
4296 | xfree (rs->last_program_signals_packet); |
4297 | rs->last_program_signals_packet = NULL; | |
9b224c5e | 4298 | |
ad9a8f3f | 4299 | remote_fileio_reset (); |
1dd41f16 NS |
4300 | reopen_exec_file (); |
4301 | reread_symbols (); | |
4302 | ||
5d93a237 TT |
4303 | rs->remote_desc = remote_serial_open (name); |
4304 | if (!rs->remote_desc) | |
c906108c SS |
4305 | perror_with_name (name); |
4306 | ||
4307 | if (baud_rate != -1) | |
4308 | { | |
5d93a237 | 4309 | if (serial_setbaudrate (rs->remote_desc, baud_rate)) |
c906108c | 4310 | { |
9b74d5d3 KB |
4311 | /* The requested speed could not be set. Error out to |
4312 | top level after closing remote_desc. Take care to | |
4313 | set remote_desc to NULL to avoid closing remote_desc | |
4314 | more than once. */ | |
5d93a237 TT |
4315 | serial_close (rs->remote_desc); |
4316 | rs->remote_desc = NULL; | |
c906108c SS |
4317 | perror_with_name (name); |
4318 | } | |
4319 | } | |
4320 | ||
5d93a237 | 4321 | serial_raw (rs->remote_desc); |
c906108c SS |
4322 | |
4323 | /* If there is something sitting in the buffer we might take it as a | |
4324 | response to a command, which would be bad. */ | |
5d93a237 | 4325 | serial_flush_input (rs->remote_desc); |
c906108c SS |
4326 | |
4327 | if (from_tty) | |
4328 | { | |
4329 | puts_filtered ("Remote debugging using "); | |
4330 | puts_filtered (name); | |
4331 | puts_filtered ("\n"); | |
4332 | } | |
23860348 | 4333 | push_target (target); /* Switch to using remote target now. */ |
c906108c | 4334 | |
74531fed PA |
4335 | /* Register extra event sources in the event loop. */ |
4336 | remote_async_inferior_event_token | |
4337 | = create_async_event_handler (remote_async_inferior_event_handler, | |
4338 | NULL); | |
5965e028 | 4339 | rs->notif_state = remote_notif_state_allocate (); |
74531fed | 4340 | |
be2a5f71 DJ |
4341 | /* Reset the target state; these things will be queried either by |
4342 | remote_query_supported or as they are needed. */ | |
d471ea57 | 4343 | init_all_packet_configs (); |
74531fed | 4344 | rs->cached_wait_status = 0; |
be2a5f71 | 4345 | rs->explicit_packet_size = 0; |
a6f3e723 | 4346 | rs->noack_mode = 0; |
82f73884 PA |
4347 | rs->multi_process_aware = 0; |
4348 | rs->extended = extended_p; | |
74531fed | 4349 | rs->non_stop_aware = 0; |
e24a49d8 | 4350 | rs->waiting_for_stop_reply = 0; |
3a29589a | 4351 | rs->ctrlc_pending_p = 0; |
802188a7 | 4352 | |
47f8a51d TT |
4353 | rs->general_thread = not_sent_ptid; |
4354 | rs->continue_thread = not_sent_ptid; | |
262e1174 | 4355 | rs->remote_traceframe_number = -1; |
c906108c | 4356 | |
9d1f7ab2 | 4357 | /* Probe for ability to use "ThreadInfo" query, as required. */ |
b80fafe3 TT |
4358 | rs->use_threadinfo_query = 1; |
4359 | rs->use_threadextra_query = 1; | |
9d1f7ab2 | 4360 | |
c6ebd6cf | 4361 | if (target_async_permitted) |
92d1e331 | 4362 | { |
23860348 | 4363 | /* With this target we start out by owning the terminal. */ |
92d1e331 DJ |
4364 | remote_async_terminal_ours_p = 1; |
4365 | ||
4366 | /* FIXME: cagney/1999-09-23: During the initial connection it is | |
4367 | assumed that the target is already ready and able to respond to | |
0df8b418 | 4368 | requests. Unfortunately remote_start_remote() eventually calls |
92d1e331 | 4369 | wait_for_inferior() with no timeout. wait_forever_enabled_p gets |
0df8b418 | 4370 | around this. Eventually a mechanism that allows |
92d1e331 | 4371 | wait_for_inferior() to expect/get timeouts will be |
23860348 | 4372 | implemented. */ |
92d1e331 DJ |
4373 | wait_forever_enabled_p = 0; |
4374 | } | |
4375 | ||
23860348 | 4376 | /* First delete any symbols previously loaded from shared libraries. */ |
f78f6cf1 | 4377 | no_shared_libraries (NULL, 0); |
f78f6cf1 | 4378 | |
74531fed PA |
4379 | /* Start afresh. */ |
4380 | init_thread_list (); | |
4381 | ||
36918e70 | 4382 | /* Start the remote connection. If error() or QUIT, discard this |
165b8e33 AC |
4383 | target (we'd otherwise be in an inconsistent state) and then |
4384 | propogate the error on up the exception chain. This ensures that | |
4385 | the caller doesn't stumble along blindly assuming that the | |
4386 | function succeeded. The CLI doesn't have this problem but other | |
4387 | UI's, such as MI do. | |
36918e70 AC |
4388 | |
4389 | FIXME: cagney/2002-05-19: Instead of re-throwing the exception, | |
4390 | this function should return an error indication letting the | |
ce2826aa | 4391 | caller restore the previous state. Unfortunately the command |
36918e70 AC |
4392 | ``target remote'' is directly wired to this function making that |
4393 | impossible. On a positive note, the CLI side of this problem has | |
4394 | been fixed - the function set_cmd_context() makes it possible for | |
4395 | all the ``target ....'' commands to share a common callback | |
4396 | function. See cli-dump.c. */ | |
109c3e39 | 4397 | { |
04bd08de | 4398 | volatile struct gdb_exception ex; |
2d717e4f | 4399 | |
04bd08de TT |
4400 | TRY_CATCH (ex, RETURN_MASK_ALL) |
4401 | { | |
4402 | remote_start_remote (from_tty, target, extended_p); | |
4403 | } | |
109c3e39 AC |
4404 | if (ex.reason < 0) |
4405 | { | |
c8d104ad PA |
4406 | /* Pop the partially set up target - unless something else did |
4407 | already before throwing the exception. */ | |
5d93a237 | 4408 | if (rs->remote_desc != NULL) |
78a095c3 | 4409 | remote_unpush_target (); |
c6ebd6cf | 4410 | if (target_async_permitted) |
109c3e39 AC |
4411 | wait_forever_enabled_p = 1; |
4412 | throw_exception (ex); | |
4413 | } | |
4414 | } | |
c906108c | 4415 | |
c6ebd6cf | 4416 | if (target_async_permitted) |
92d1e331 | 4417 | wait_forever_enabled_p = 1; |
43ff13b4 JM |
4418 | } |
4419 | ||
c906108c SS |
4420 | /* This takes a program previously attached to and detaches it. After |
4421 | this is done, GDB can be used to debug some other program. We | |
4422 | better not have left any breakpoints in the target program or it'll | |
4423 | die when it hits one. */ | |
4424 | ||
4425 | static void | |
52554a0e | 4426 | remote_detach_1 (const char *args, int from_tty, int extended) |
c906108c | 4427 | { |
82f73884 | 4428 | int pid = ptid_get_pid (inferior_ptid); |
d01949b6 | 4429 | struct remote_state *rs = get_remote_state (); |
c906108c SS |
4430 | |
4431 | if (args) | |
8a3fe4f8 | 4432 | error (_("Argument given to \"detach\" when remotely debugging.")); |
c906108c | 4433 | |
2d717e4f DJ |
4434 | if (!target_has_execution) |
4435 | error (_("No process to detach from.")); | |
4436 | ||
7cee1e54 PA |
4437 | if (from_tty) |
4438 | { | |
4439 | char *exec_file = get_exec_file (0); | |
4440 | if (exec_file == NULL) | |
4441 | exec_file = ""; | |
4442 | printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file, | |
4443 | target_pid_to_str (pid_to_ptid (pid))); | |
4444 | gdb_flush (gdb_stdout); | |
4445 | } | |
4446 | ||
c906108c | 4447 | /* Tell the remote target to detach. */ |
82f73884 | 4448 | if (remote_multi_process_p (rs)) |
bba74b36 | 4449 | xsnprintf (rs->buf, get_remote_packet_size (), "D;%x", pid); |
82f73884 PA |
4450 | else |
4451 | strcpy (rs->buf, "D"); | |
4452 | ||
4ddda9b5 PA |
4453 | putpkt (rs->buf); |
4454 | getpkt (&rs->buf, &rs->buf_size, 0); | |
4455 | ||
82f73884 PA |
4456 | if (rs->buf[0] == 'O' && rs->buf[1] == 'K') |
4457 | ; | |
4458 | else if (rs->buf[0] == '\0') | |
4459 | error (_("Remote doesn't know how to detach")); | |
4460 | else | |
4ddda9b5 | 4461 | error (_("Can't detach process.")); |
c906108c | 4462 | |
7cee1e54 PA |
4463 | if (from_tty && !extended) |
4464 | puts_filtered (_("Ending remote debugging.\n")); | |
82f73884 | 4465 | |
82f73884 | 4466 | target_mourn_inferior (); |
2d717e4f DJ |
4467 | } |
4468 | ||
4469 | static void | |
52554a0e | 4470 | remote_detach (struct target_ops *ops, const char *args, int from_tty) |
2d717e4f DJ |
4471 | { |
4472 | remote_detach_1 (args, from_tty, 0); | |
4473 | } | |
4474 | ||
4475 | static void | |
52554a0e | 4476 | extended_remote_detach (struct target_ops *ops, const char *args, int from_tty) |
2d717e4f DJ |
4477 | { |
4478 | remote_detach_1 (args, from_tty, 1); | |
c906108c SS |
4479 | } |
4480 | ||
6ad8ae5c DJ |
4481 | /* Same as remote_detach, but don't send the "D" packet; just disconnect. */ |
4482 | ||
43ff13b4 | 4483 | static void |
597320e7 | 4484 | remote_disconnect (struct target_ops *target, char *args, int from_tty) |
43ff13b4 | 4485 | { |
43ff13b4 | 4486 | if (args) |
2d717e4f | 4487 | error (_("Argument given to \"disconnect\" when remotely debugging.")); |
43ff13b4 | 4488 | |
2d717e4f DJ |
4489 | /* Make sure we unpush even the extended remote targets; mourn |
4490 | won't do it. So call remote_mourn_1 directly instead of | |
4491 | target_mourn_inferior. */ | |
4492 | remote_mourn_1 (target); | |
4493 | ||
43ff13b4 JM |
4494 | if (from_tty) |
4495 | puts_filtered ("Ending remote debugging.\n"); | |
4496 | } | |
4497 | ||
2d717e4f DJ |
4498 | /* Attach to the process specified by ARGS. If FROM_TTY is non-zero, |
4499 | be chatty about it. */ | |
4500 | ||
4501 | static void | |
4502 | extended_remote_attach_1 (struct target_ops *target, char *args, int from_tty) | |
4503 | { | |
4504 | struct remote_state *rs = get_remote_state (); | |
be86555c | 4505 | int pid; |
96ef3384 | 4506 | char *wait_status = NULL; |
2d717e4f | 4507 | |
74164c56 | 4508 | pid = parse_pid_to_attach (args); |
2d717e4f | 4509 | |
74164c56 JK |
4510 | /* Remote PID can be freely equal to getpid, do not check it here the same |
4511 | way as in other targets. */ | |
2d717e4f DJ |
4512 | |
4513 | if (remote_protocol_packets[PACKET_vAttach].support == PACKET_DISABLE) | |
4514 | error (_("This target does not support attaching to a process")); | |
4515 | ||
7cee1e54 PA |
4516 | if (from_tty) |
4517 | { | |
4518 | char *exec_file = get_exec_file (0); | |
4519 | ||
4520 | if (exec_file) | |
4521 | printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file, | |
4522 | target_pid_to_str (pid_to_ptid (pid))); | |
4523 | else | |
4524 | printf_unfiltered (_("Attaching to %s\n"), | |
4525 | target_pid_to_str (pid_to_ptid (pid))); | |
4526 | ||
4527 | gdb_flush (gdb_stdout); | |
4528 | } | |
4529 | ||
bba74b36 | 4530 | xsnprintf (rs->buf, get_remote_packet_size (), "vAttach;%x", pid); |
2d717e4f DJ |
4531 | putpkt (rs->buf); |
4532 | getpkt (&rs->buf, &rs->buf_size, 0); | |
4533 | ||
3e43a32a MS |
4534 | if (packet_ok (rs->buf, |
4535 | &remote_protocol_packets[PACKET_vAttach]) == PACKET_OK) | |
2d717e4f | 4536 | { |
74531fed PA |
4537 | if (!non_stop) |
4538 | { | |
4539 | /* Save the reply for later. */ | |
4540 | wait_status = alloca (strlen (rs->buf) + 1); | |
4541 | strcpy (wait_status, rs->buf); | |
4542 | } | |
4543 | else if (strcmp (rs->buf, "OK") != 0) | |
4544 | error (_("Attaching to %s failed with: %s"), | |
4545 | target_pid_to_str (pid_to_ptid (pid)), | |
4546 | rs->buf); | |
2d717e4f DJ |
4547 | } |
4548 | else if (remote_protocol_packets[PACKET_vAttach].support == PACKET_DISABLE) | |
4549 | error (_("This target does not support attaching to a process")); | |
4550 | else | |
4551 | error (_("Attaching to %s failed"), | |
4552 | target_pid_to_str (pid_to_ptid (pid))); | |
4553 | ||
49c62f2e | 4554 | set_current_inferior (remote_add_inferior (0, pid, 1)); |
bad34192 | 4555 | |
2d717e4f | 4556 | inferior_ptid = pid_to_ptid (pid); |
79d7f229 | 4557 | |
bad34192 PA |
4558 | if (non_stop) |
4559 | { | |
4560 | struct thread_info *thread; | |
79d7f229 | 4561 | |
bad34192 PA |
4562 | /* Get list of threads. */ |
4563 | remote_threads_info (target); | |
82f73884 | 4564 | |
bad34192 PA |
4565 | thread = first_thread_of_process (pid); |
4566 | if (thread) | |
4567 | inferior_ptid = thread->ptid; | |
4568 | else | |
4569 | inferior_ptid = pid_to_ptid (pid); | |
4570 | ||
4571 | /* Invalidate our notion of the remote current thread. */ | |
47f8a51d | 4572 | record_currthread (rs, minus_one_ptid); |
bad34192 | 4573 | } |
74531fed | 4574 | else |
bad34192 PA |
4575 | { |
4576 | /* Now, if we have thread information, update inferior_ptid. */ | |
4577 | inferior_ptid = remote_current_thread (inferior_ptid); | |
4578 | ||
4579 | /* Add the main thread to the thread list. */ | |
4580 | add_thread_silent (inferior_ptid); | |
4581 | } | |
c0a2216e | 4582 | |
96ef3384 UW |
4583 | /* Next, if the target can specify a description, read it. We do |
4584 | this before anything involving memory or registers. */ | |
4585 | target_find_description (); | |
4586 | ||
74531fed PA |
4587 | if (!non_stop) |
4588 | { | |
4589 | /* Use the previously fetched status. */ | |
4590 | gdb_assert (wait_status != NULL); | |
4591 | ||
4592 | if (target_can_async_p ()) | |
4593 | { | |
722247f1 YQ |
4594 | struct notif_event *reply |
4595 | = remote_notif_parse (¬if_client_stop, wait_status); | |
74531fed | 4596 | |
722247f1 | 4597 | push_stop_reply ((struct stop_reply *) reply); |
74531fed PA |
4598 | |
4599 | target_async (inferior_event_handler, 0); | |
4600 | } | |
4601 | else | |
4602 | { | |
4603 | gdb_assert (wait_status != NULL); | |
4604 | strcpy (rs->buf, wait_status); | |
4605 | rs->cached_wait_status = 1; | |
4606 | } | |
4607 | } | |
4608 | else | |
4609 | gdb_assert (wait_status == NULL); | |
2d717e4f DJ |
4610 | } |
4611 | ||
4612 | static void | |
136d6dae | 4613 | extended_remote_attach (struct target_ops *ops, char *args, int from_tty) |
2d717e4f | 4614 | { |
136d6dae | 4615 | extended_remote_attach_1 (ops, args, from_tty); |
2d717e4f DJ |
4616 | } |
4617 | ||
c906108c SS |
4618 | /* Convert hex digit A to a number. */ |
4619 | ||
30559e10 | 4620 | static int |
fba45db2 | 4621 | fromhex (int a) |
c906108c SS |
4622 | { |
4623 | if (a >= '0' && a <= '9') | |
4624 | return a - '0'; | |
4625 | else if (a >= 'a' && a <= 'f') | |
4626 | return a - 'a' + 10; | |
4627 | else if (a >= 'A' && a <= 'F') | |
4628 | return a - 'A' + 10; | |
c5aa993b | 4629 | else |
8a3fe4f8 | 4630 | error (_("Reply contains invalid hex digit %d"), a); |
c906108c SS |
4631 | } |
4632 | ||
00bf0b85 | 4633 | int |
cfd77fa1 | 4634 | hex2bin (const char *hex, gdb_byte *bin, int count) |
30559e10 MS |
4635 | { |
4636 | int i; | |
4637 | ||
30559e10 MS |
4638 | for (i = 0; i < count; i++) |
4639 | { | |
4640 | if (hex[0] == 0 || hex[1] == 0) | |
4641 | { | |
4642 | /* Hex string is short, or of uneven length. | |
23860348 | 4643 | Return the count that has been converted so far. */ |
30559e10 MS |
4644 | return i; |
4645 | } | |
4646 | *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]); | |
4647 | hex += 2; | |
4648 | } | |
4649 | return i; | |
4650 | } | |
4651 | ||
c906108c SS |
4652 | /* Convert number NIB to a hex digit. */ |
4653 | ||
4654 | static int | |
fba45db2 | 4655 | tohex (int nib) |
c906108c SS |
4656 | { |
4657 | if (nib < 10) | |
c5aa993b | 4658 | return '0' + nib; |
c906108c | 4659 | else |
c5aa993b | 4660 | return 'a' + nib - 10; |
c906108c | 4661 | } |
30559e10 | 4662 | |
00bf0b85 | 4663 | int |
cfd77fa1 | 4664 | bin2hex (const gdb_byte *bin, char *hex, int count) |
30559e10 MS |
4665 | { |
4666 | int i; | |
a744cf53 | 4667 | |
23860348 | 4668 | /* May use a length, or a nul-terminated string as input. */ |
30559e10 | 4669 | if (count == 0) |
cfd77fa1 | 4670 | count = strlen ((char *) bin); |
30559e10 MS |
4671 | |
4672 | for (i = 0; i < count; i++) | |
4673 | { | |
4674 | *hex++ = tohex ((*bin >> 4) & 0xf); | |
4675 | *hex++ = tohex (*bin++ & 0xf); | |
4676 | } | |
4677 | *hex = 0; | |
4678 | return i; | |
4679 | } | |
c906108c | 4680 | \f |
506fb367 DJ |
4681 | /* Check for the availability of vCont. This function should also check |
4682 | the response. */ | |
c906108c SS |
4683 | |
4684 | static void | |
6d820c5c | 4685 | remote_vcont_probe (struct remote_state *rs) |
c906108c | 4686 | { |
2e9f7625 | 4687 | char *buf; |
6d820c5c | 4688 | |
2e9f7625 DJ |
4689 | strcpy (rs->buf, "vCont?"); |
4690 | putpkt (rs->buf); | |
6d820c5c | 4691 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 4692 | buf = rs->buf; |
c906108c | 4693 | |
506fb367 DJ |
4694 | /* Make sure that the features we assume are supported. */ |
4695 | if (strncmp (buf, "vCont", 5) == 0) | |
4696 | { | |
4697 | char *p = &buf[5]; | |
4698 | int support_s, support_S, support_c, support_C; | |
4699 | ||
4700 | support_s = 0; | |
4701 | support_S = 0; | |
4702 | support_c = 0; | |
4703 | support_C = 0; | |
d458bd84 | 4704 | rs->supports_vCont.t = 0; |
c1e36e3e | 4705 | rs->supports_vCont.r = 0; |
506fb367 DJ |
4706 | while (p && *p == ';') |
4707 | { | |
4708 | p++; | |
4709 | if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0)) | |
4710 | support_s = 1; | |
4711 | else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0)) | |
4712 | support_S = 1; | |
4713 | else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0)) | |
4714 | support_c = 1; | |
4715 | else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0)) | |
4716 | support_C = 1; | |
74531fed | 4717 | else if (*p == 't' && (*(p + 1) == ';' || *(p + 1) == 0)) |
d458bd84 | 4718 | rs->supports_vCont.t = 1; |
c1e36e3e PA |
4719 | else if (*p == 'r' && (*(p + 1) == ';' || *(p + 1) == 0)) |
4720 | rs->supports_vCont.r = 1; | |
506fb367 DJ |
4721 | |
4722 | p = strchr (p, ';'); | |
4723 | } | |
c906108c | 4724 | |
506fb367 DJ |
4725 | /* If s, S, c, and C are not all supported, we can't use vCont. Clearing |
4726 | BUF will make packet_ok disable the packet. */ | |
4727 | if (!support_s || !support_S || !support_c || !support_C) | |
4728 | buf[0] = 0; | |
4729 | } | |
c906108c | 4730 | |
444abaca | 4731 | packet_ok (buf, &remote_protocol_packets[PACKET_vCont]); |
506fb367 | 4732 | } |
c906108c | 4733 | |
0d8f58ca PA |
4734 | /* Helper function for building "vCont" resumptions. Write a |
4735 | resumption to P. ENDP points to one-passed-the-end of the buffer | |
4736 | we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The | |
4737 | thread to be resumed is PTID; STEP and SIGGNAL indicate whether the | |
4738 | resumed thread should be single-stepped and/or signalled. If PTID | |
4739 | equals minus_one_ptid, then all threads are resumed; if PTID | |
4740 | represents a process, then all threads of the process are resumed; | |
4741 | the thread to be stepped and/or signalled is given in the global | |
4742 | INFERIOR_PTID. */ | |
4743 | ||
4744 | static char * | |
4745 | append_resumption (char *p, char *endp, | |
2ea28649 | 4746 | ptid_t ptid, int step, enum gdb_signal siggnal) |
0d8f58ca PA |
4747 | { |
4748 | struct remote_state *rs = get_remote_state (); | |
4749 | ||
a493e3e2 | 4750 | if (step && siggnal != GDB_SIGNAL_0) |
0d8f58ca | 4751 | p += xsnprintf (p, endp - p, ";S%02x", siggnal); |
c1e36e3e PA |
4752 | else if (step |
4753 | /* GDB is willing to range step. */ | |
4754 | && use_range_stepping | |
4755 | /* Target supports range stepping. */ | |
4756 | && rs->supports_vCont.r | |
4757 | /* We don't currently support range stepping multiple | |
4758 | threads with a wildcard (though the protocol allows it, | |
4759 | so stubs shouldn't make an active effort to forbid | |
4760 | it). */ | |
4761 | && !(remote_multi_process_p (rs) && ptid_is_pid (ptid))) | |
4762 | { | |
4763 | struct thread_info *tp; | |
4764 | ||
4765 | if (ptid_equal (ptid, minus_one_ptid)) | |
4766 | { | |
4767 | /* If we don't know about the target thread's tid, then | |
4768 | we're resuming magic_null_ptid (see caller). */ | |
4769 | tp = find_thread_ptid (magic_null_ptid); | |
4770 | } | |
4771 | else | |
4772 | tp = find_thread_ptid (ptid); | |
4773 | gdb_assert (tp != NULL); | |
4774 | ||
4775 | if (tp->control.may_range_step) | |
4776 | { | |
4777 | int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8; | |
4778 | ||
4779 | p += xsnprintf (p, endp - p, ";r%s,%s", | |
4780 | phex_nz (tp->control.step_range_start, | |
4781 | addr_size), | |
4782 | phex_nz (tp->control.step_range_end, | |
4783 | addr_size)); | |
4784 | } | |
4785 | else | |
4786 | p += xsnprintf (p, endp - p, ";s"); | |
4787 | } | |
0d8f58ca PA |
4788 | else if (step) |
4789 | p += xsnprintf (p, endp - p, ";s"); | |
a493e3e2 | 4790 | else if (siggnal != GDB_SIGNAL_0) |
0d8f58ca PA |
4791 | p += xsnprintf (p, endp - p, ";C%02x", siggnal); |
4792 | else | |
4793 | p += xsnprintf (p, endp - p, ";c"); | |
4794 | ||
4795 | if (remote_multi_process_p (rs) && ptid_is_pid (ptid)) | |
4796 | { | |
4797 | ptid_t nptid; | |
4798 | ||
4799 | /* All (-1) threads of process. */ | |
4800 | nptid = ptid_build (ptid_get_pid (ptid), 0, -1); | |
4801 | ||
4802 | p += xsnprintf (p, endp - p, ":"); | |
4803 | p = write_ptid (p, endp, nptid); | |
4804 | } | |
4805 | else if (!ptid_equal (ptid, minus_one_ptid)) | |
4806 | { | |
4807 | p += xsnprintf (p, endp - p, ":"); | |
4808 | p = write_ptid (p, endp, ptid); | |
4809 | } | |
4810 | ||
4811 | return p; | |
4812 | } | |
4813 | ||
e5ef252a PA |
4814 | /* Append a vCont continue-with-signal action for threads that have a |
4815 | non-zero stop signal. */ | |
4816 | ||
4817 | static char * | |
4818 | append_pending_thread_resumptions (char *p, char *endp, ptid_t ptid) | |
4819 | { | |
4820 | struct thread_info *thread; | |
4821 | ||
4822 | ALL_THREADS (thread) | |
4823 | if (ptid_match (thread->ptid, ptid) | |
4824 | && !ptid_equal (inferior_ptid, thread->ptid) | |
4825 | && thread->suspend.stop_signal != GDB_SIGNAL_0 | |
4826 | && signal_pass_state (thread->suspend.stop_signal)) | |
4827 | { | |
4828 | p = append_resumption (p, endp, thread->ptid, | |
4829 | 0, thread->suspend.stop_signal); | |
4830 | thread->suspend.stop_signal = GDB_SIGNAL_0; | |
4831 | } | |
4832 | ||
4833 | return p; | |
4834 | } | |
4835 | ||
506fb367 DJ |
4836 | /* Resume the remote inferior by using a "vCont" packet. The thread |
4837 | to be resumed is PTID; STEP and SIGGNAL indicate whether the | |
79d7f229 PA |
4838 | resumed thread should be single-stepped and/or signalled. If PTID |
4839 | equals minus_one_ptid, then all threads are resumed; the thread to | |
4840 | be stepped and/or signalled is given in the global INFERIOR_PTID. | |
4841 | This function returns non-zero iff it resumes the inferior. | |
44eaed12 | 4842 | |
506fb367 DJ |
4843 | This function issues a strict subset of all possible vCont commands at the |
4844 | moment. */ | |
44eaed12 | 4845 | |
506fb367 | 4846 | static int |
2ea28649 | 4847 | remote_vcont_resume (ptid_t ptid, int step, enum gdb_signal siggnal) |
506fb367 DJ |
4848 | { |
4849 | struct remote_state *rs = get_remote_state (); | |
82f73884 PA |
4850 | char *p; |
4851 | char *endp; | |
44eaed12 | 4852 | |
444abaca | 4853 | if (remote_protocol_packets[PACKET_vCont].support == PACKET_SUPPORT_UNKNOWN) |
6d820c5c | 4854 | remote_vcont_probe (rs); |
44eaed12 | 4855 | |
444abaca | 4856 | if (remote_protocol_packets[PACKET_vCont].support == PACKET_DISABLE) |
6d820c5c | 4857 | return 0; |
44eaed12 | 4858 | |
82f73884 PA |
4859 | p = rs->buf; |
4860 | endp = rs->buf + get_remote_packet_size (); | |
4861 | ||
506fb367 DJ |
4862 | /* If we could generate a wider range of packets, we'd have to worry |
4863 | about overflowing BUF. Should there be a generic | |
4864 | "multi-part-packet" packet? */ | |
4865 | ||
0d8f58ca PA |
4866 | p += xsnprintf (p, endp - p, "vCont"); |
4867 | ||
79d7f229 | 4868 | if (ptid_equal (ptid, magic_null_ptid)) |
c906108c | 4869 | { |
79d7f229 PA |
4870 | /* MAGIC_NULL_PTID means that we don't have any active threads, |
4871 | so we don't have any TID numbers the inferior will | |
4872 | understand. Make sure to only send forms that do not specify | |
4873 | a TID. */ | |
a9cbf802 | 4874 | append_resumption (p, endp, minus_one_ptid, step, siggnal); |
506fb367 | 4875 | } |
0d8f58ca | 4876 | else if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid)) |
506fb367 | 4877 | { |
0d8f58ca PA |
4878 | /* Resume all threads (of all processes, or of a single |
4879 | process), with preference for INFERIOR_PTID. This assumes | |
4880 | inferior_ptid belongs to the set of all threads we are about | |
4881 | to resume. */ | |
a493e3e2 | 4882 | if (step || siggnal != GDB_SIGNAL_0) |
82f73884 | 4883 | { |
0d8f58ca PA |
4884 | /* Step inferior_ptid, with or without signal. */ |
4885 | p = append_resumption (p, endp, inferior_ptid, step, siggnal); | |
82f73884 | 4886 | } |
0d8f58ca | 4887 | |
e5ef252a PA |
4888 | /* Also pass down any pending signaled resumption for other |
4889 | threads not the current. */ | |
4890 | p = append_pending_thread_resumptions (p, endp, ptid); | |
4891 | ||
0d8f58ca | 4892 | /* And continue others without a signal. */ |
a493e3e2 | 4893 | append_resumption (p, endp, ptid, /*step=*/ 0, GDB_SIGNAL_0); |
c906108c SS |
4894 | } |
4895 | else | |
506fb367 DJ |
4896 | { |
4897 | /* Scheduler locking; resume only PTID. */ | |
a9cbf802 | 4898 | append_resumption (p, endp, ptid, step, siggnal); |
506fb367 | 4899 | } |
c906108c | 4900 | |
82f73884 PA |
4901 | gdb_assert (strlen (rs->buf) < get_remote_packet_size ()); |
4902 | putpkt (rs->buf); | |
506fb367 | 4903 | |
74531fed PA |
4904 | if (non_stop) |
4905 | { | |
4906 | /* In non-stop, the stub replies to vCont with "OK". The stop | |
4907 | reply will be reported asynchronously by means of a `%Stop' | |
4908 | notification. */ | |
4909 | getpkt (&rs->buf, &rs->buf_size, 0); | |
4910 | if (strcmp (rs->buf, "OK") != 0) | |
4911 | error (_("Unexpected vCont reply in non-stop mode: %s"), rs->buf); | |
4912 | } | |
4913 | ||
506fb367 | 4914 | return 1; |
c906108c | 4915 | } |
43ff13b4 | 4916 | |
506fb367 DJ |
4917 | /* Tell the remote machine to resume. */ |
4918 | ||
43ff13b4 | 4919 | static void |
28439f5e | 4920 | remote_resume (struct target_ops *ops, |
2ea28649 | 4921 | ptid_t ptid, int step, enum gdb_signal siggnal) |
43ff13b4 | 4922 | { |
d01949b6 | 4923 | struct remote_state *rs = get_remote_state (); |
2e9f7625 | 4924 | char *buf; |
43ff13b4 | 4925 | |
722247f1 YQ |
4926 | /* In all-stop, we can't mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN |
4927 | (explained in remote-notif.c:handle_notification) so | |
4928 | remote_notif_process is not called. We need find a place where | |
4929 | it is safe to start a 'vNotif' sequence. It is good to do it | |
4930 | before resuming inferior, because inferior was stopped and no RSP | |
4931 | traffic at that moment. */ | |
4932 | if (!non_stop) | |
5965e028 | 4933 | remote_notif_process (rs->notif_state, ¬if_client_stop); |
722247f1 | 4934 | |
b73be471 | 4935 | rs->last_sent_signal = siggnal; |
280ceea3 | 4936 | rs->last_sent_step = step; |
43ff13b4 | 4937 | |
506fb367 | 4938 | /* The vCont packet doesn't need to specify threads via Hc. */ |
40ab02ce MS |
4939 | /* No reverse support (yet) for vCont. */ |
4940 | if (execution_direction != EXEC_REVERSE) | |
4941 | if (remote_vcont_resume (ptid, step, siggnal)) | |
4942 | goto done; | |
506fb367 | 4943 | |
79d7f229 PA |
4944 | /* All other supported resume packets do use Hc, so set the continue |
4945 | thread. */ | |
4946 | if (ptid_equal (ptid, minus_one_ptid)) | |
4947 | set_continue_thread (any_thread_ptid); | |
506fb367 | 4948 | else |
79d7f229 | 4949 | set_continue_thread (ptid); |
506fb367 | 4950 | |
2e9f7625 | 4951 | buf = rs->buf; |
b2175913 MS |
4952 | if (execution_direction == EXEC_REVERSE) |
4953 | { | |
4954 | /* We don't pass signals to the target in reverse exec mode. */ | |
a493e3e2 | 4955 | if (info_verbose && siggnal != GDB_SIGNAL_0) |
7ea6d463 | 4956 | warning (_(" - Can't pass signal %d to target in reverse: ignored."), |
b2175913 | 4957 | siggnal); |
40ab02ce MS |
4958 | |
4959 | if (step | |
4960 | && remote_protocol_packets[PACKET_bs].support == PACKET_DISABLE) | |
4961 | error (_("Remote reverse-step not supported.")); | |
4962 | if (!step | |
4963 | && remote_protocol_packets[PACKET_bc].support == PACKET_DISABLE) | |
08c93ed9 | 4964 | error (_("Remote reverse-continue not supported.")); |
40ab02ce | 4965 | |
b2175913 MS |
4966 | strcpy (buf, step ? "bs" : "bc"); |
4967 | } | |
a493e3e2 | 4968 | else if (siggnal != GDB_SIGNAL_0) |
43ff13b4 JM |
4969 | { |
4970 | buf[0] = step ? 'S' : 'C'; | |
c5aa993b | 4971 | buf[1] = tohex (((int) siggnal >> 4) & 0xf); |
506fb367 | 4972 | buf[2] = tohex (((int) siggnal) & 0xf); |
43ff13b4 JM |
4973 | buf[3] = '\0'; |
4974 | } | |
4975 | else | |
c5aa993b | 4976 | strcpy (buf, step ? "s" : "c"); |
506fb367 | 4977 | |
44eaed12 | 4978 | putpkt (buf); |
43ff13b4 | 4979 | |
75c99385 | 4980 | done: |
2acceee2 | 4981 | /* We are about to start executing the inferior, let's register it |
0df8b418 MS |
4982 | with the event loop. NOTE: this is the one place where all the |
4983 | execution commands end up. We could alternatively do this in each | |
23860348 | 4984 | of the execution commands in infcmd.c. */ |
2acceee2 JM |
4985 | /* FIXME: ezannoni 1999-09-28: We may need to move this out of here |
4986 | into infcmd.c in order to allow inferior function calls to work | |
23860348 | 4987 | NOT asynchronously. */ |
362646f5 | 4988 | if (target_can_async_p ()) |
2acceee2 | 4989 | target_async (inferior_event_handler, 0); |
e24a49d8 PA |
4990 | |
4991 | /* We've just told the target to resume. The remote server will | |
4992 | wait for the inferior to stop, and then send a stop reply. In | |
4993 | the mean time, we can't start another command/query ourselves | |
74531fed PA |
4994 | because the stub wouldn't be ready to process it. This applies |
4995 | only to the base all-stop protocol, however. In non-stop (which | |
4996 | only supports vCont), the stub replies with an "OK", and is | |
4997 | immediate able to process further serial input. */ | |
4998 | if (!non_stop) | |
4999 | rs->waiting_for_stop_reply = 1; | |
43ff13b4 | 5000 | } |
c906108c | 5001 | \f |
43ff13b4 JM |
5002 | |
5003 | /* Set up the signal handler for SIGINT, while the target is | |
23860348 | 5004 | executing, ovewriting the 'regular' SIGINT signal handler. */ |
43ff13b4 | 5005 | static void |
934b9bac | 5006 | async_initialize_sigint_signal_handler (void) |
43ff13b4 | 5007 | { |
934b9bac | 5008 | signal (SIGINT, async_handle_remote_sigint); |
43ff13b4 JM |
5009 | } |
5010 | ||
23860348 | 5011 | /* Signal handler for SIGINT, while the target is executing. */ |
43ff13b4 | 5012 | static void |
934b9bac | 5013 | async_handle_remote_sigint (int sig) |
43ff13b4 | 5014 | { |
934b9bac JK |
5015 | signal (sig, async_handle_remote_sigint_twice); |
5016 | mark_async_signal_handler (async_sigint_remote_token); | |
43ff13b4 JM |
5017 | } |
5018 | ||
5019 | /* Signal handler for SIGINT, installed after SIGINT has already been | |
5020 | sent once. It will take effect the second time that the user sends | |
23860348 | 5021 | a ^C. */ |
43ff13b4 | 5022 | static void |
934b9bac | 5023 | async_handle_remote_sigint_twice (int sig) |
43ff13b4 | 5024 | { |
934b9bac JK |
5025 | signal (sig, async_handle_remote_sigint); |
5026 | mark_async_signal_handler (async_sigint_remote_twice_token); | |
43ff13b4 JM |
5027 | } |
5028 | ||
6426a772 | 5029 | /* Perform the real interruption of the target execution, in response |
23860348 | 5030 | to a ^C. */ |
c5aa993b | 5031 | static void |
fba45db2 | 5032 | async_remote_interrupt (gdb_client_data arg) |
43ff13b4 JM |
5033 | { |
5034 | if (remote_debug) | |
248fd3bf | 5035 | fprintf_unfiltered (gdb_stdlog, "async_remote_interrupt called\n"); |
43ff13b4 | 5036 | |
94cc34af | 5037 | target_stop (inferior_ptid); |
43ff13b4 JM |
5038 | } |
5039 | ||
0df8b418 | 5040 | /* Perform interrupt, if the first attempt did not succeed. Just give |
23860348 | 5041 | up on the target alltogether. */ |
47e1ce27 | 5042 | static void |
fba45db2 | 5043 | async_remote_interrupt_twice (gdb_client_data arg) |
43ff13b4 | 5044 | { |
2df3850c | 5045 | if (remote_debug) |
248fd3bf | 5046 | fprintf_unfiltered (gdb_stdlog, "async_remote_interrupt_twice called\n"); |
b803fb0f DJ |
5047 | |
5048 | interrupt_query (); | |
43ff13b4 JM |
5049 | } |
5050 | ||
5051 | /* Reinstall the usual SIGINT handlers, after the target has | |
23860348 | 5052 | stopped. */ |
6426a772 | 5053 | static void |
934b9bac | 5054 | async_cleanup_sigint_signal_handler (void *dummy) |
43ff13b4 JM |
5055 | { |
5056 | signal (SIGINT, handle_sigint); | |
43ff13b4 JM |
5057 | } |
5058 | ||
c906108c SS |
5059 | /* Send ^C to target to halt it. Target will respond, and send us a |
5060 | packet. */ | |
507f3c78 | 5061 | static void (*ofunc) (int); |
c906108c | 5062 | |
0df8b418 MS |
5063 | /* The command line interface's stop routine. This function is installed |
5064 | as a signal handler for SIGINT. The first time a user requests a | |
5065 | stop, we call remote_stop to send a break or ^C. If there is no | |
7a292a7a | 5066 | response from the target (it didn't stop when the user requested it), |
23860348 | 5067 | we ask the user if he'd like to detach from the target. */ |
c906108c | 5068 | static void |
934b9bac | 5069 | sync_remote_interrupt (int signo) |
c906108c | 5070 | { |
23860348 | 5071 | /* If this doesn't work, try more severe steps. */ |
934b9bac | 5072 | signal (signo, sync_remote_interrupt_twice); |
7a292a7a | 5073 | |
934b9bac | 5074 | gdb_call_async_signal_handler (async_sigint_remote_token, 1); |
7a292a7a SS |
5075 | } |
5076 | ||
5077 | /* The user typed ^C twice. */ | |
5078 | ||
5079 | static void | |
934b9bac | 5080 | sync_remote_interrupt_twice (int signo) |
7a292a7a SS |
5081 | { |
5082 | signal (signo, ofunc); | |
934b9bac JK |
5083 | gdb_call_async_signal_handler (async_sigint_remote_twice_token, 1); |
5084 | signal (signo, sync_remote_interrupt); | |
c906108c | 5085 | } |
7a292a7a | 5086 | |
74531fed PA |
5087 | /* Non-stop version of target_stop. Uses `vCont;t' to stop a remote |
5088 | thread, all threads of a remote process, or all threads of all | |
5089 | processes. */ | |
5090 | ||
5091 | static void | |
5092 | remote_stop_ns (ptid_t ptid) | |
5093 | { | |
5094 | struct remote_state *rs = get_remote_state (); | |
5095 | char *p = rs->buf; | |
5096 | char *endp = rs->buf + get_remote_packet_size (); | |
74531fed PA |
5097 | |
5098 | if (remote_protocol_packets[PACKET_vCont].support == PACKET_SUPPORT_UNKNOWN) | |
5099 | remote_vcont_probe (rs); | |
5100 | ||
d458bd84 | 5101 | if (!rs->supports_vCont.t) |
74531fed PA |
5102 | error (_("Remote server does not support stopping threads")); |
5103 | ||
f91d3df5 PA |
5104 | if (ptid_equal (ptid, minus_one_ptid) |
5105 | || (!remote_multi_process_p (rs) && ptid_is_pid (ptid))) | |
74531fed PA |
5106 | p += xsnprintf (p, endp - p, "vCont;t"); |
5107 | else | |
5108 | { | |
5109 | ptid_t nptid; | |
5110 | ||
74531fed PA |
5111 | p += xsnprintf (p, endp - p, "vCont;t:"); |
5112 | ||
5113 | if (ptid_is_pid (ptid)) | |
5114 | /* All (-1) threads of process. */ | |
5115 | nptid = ptid_build (ptid_get_pid (ptid), 0, -1); | |
5116 | else | |
5117 | { | |
5118 | /* Small optimization: if we already have a stop reply for | |
5119 | this thread, no use in telling the stub we want this | |
5120 | stopped. */ | |
5121 | if (peek_stop_reply (ptid)) | |
5122 | return; | |
5123 | ||
5124 | nptid = ptid; | |
5125 | } | |
5126 | ||
a9cbf802 | 5127 | write_ptid (p, endp, nptid); |
74531fed PA |
5128 | } |
5129 | ||
5130 | /* In non-stop, we get an immediate OK reply. The stop reply will | |
5131 | come in asynchronously by notification. */ | |
5132 | putpkt (rs->buf); | |
5133 | getpkt (&rs->buf, &rs->buf_size, 0); | |
5134 | if (strcmp (rs->buf, "OK") != 0) | |
5135 | error (_("Stopping %s failed: %s"), target_pid_to_str (ptid), rs->buf); | |
5136 | } | |
5137 | ||
5138 | /* All-stop version of target_stop. Sends a break or a ^C to stop the | |
5139 | remote target. It is undefined which thread of which process | |
5140 | reports the stop. */ | |
5141 | ||
5142 | static void | |
5143 | remote_stop_as (ptid_t ptid) | |
5144 | { | |
5145 | struct remote_state *rs = get_remote_state (); | |
5146 | ||
3a29589a DJ |
5147 | rs->ctrlc_pending_p = 1; |
5148 | ||
74531fed PA |
5149 | /* If the inferior is stopped already, but the core didn't know |
5150 | about it yet, just ignore the request. The cached wait status | |
5151 | will be collected in remote_wait. */ | |
5152 | if (rs->cached_wait_status) | |
5153 | return; | |
5154 | ||
9a7071a8 JB |
5155 | /* Send interrupt_sequence to remote target. */ |
5156 | send_interrupt_sequence (); | |
74531fed PA |
5157 | } |
5158 | ||
0df8b418 | 5159 | /* This is the generic stop called via the target vector. When a target |
7a292a7a | 5160 | interrupt is requested, either by the command line or the GUI, we |
23860348 | 5161 | will eventually end up here. */ |
74531fed | 5162 | |
c906108c | 5163 | static void |
94cc34af | 5164 | remote_stop (ptid_t ptid) |
c906108c | 5165 | { |
7a292a7a | 5166 | if (remote_debug) |
0f71a2f6 | 5167 | fprintf_unfiltered (gdb_stdlog, "remote_stop called\n"); |
c906108c | 5168 | |
74531fed PA |
5169 | if (non_stop) |
5170 | remote_stop_ns (ptid); | |
c906108c | 5171 | else |
74531fed | 5172 | remote_stop_as (ptid); |
c906108c SS |
5173 | } |
5174 | ||
5175 | /* Ask the user what to do when an interrupt is received. */ | |
5176 | ||
5177 | static void | |
fba45db2 | 5178 | interrupt_query (void) |
c906108c SS |
5179 | { |
5180 | target_terminal_ours (); | |
5181 | ||
74531fed | 5182 | if (target_can_async_p ()) |
c906108c | 5183 | { |
74531fed | 5184 | signal (SIGINT, handle_sigint); |
039e3c22 | 5185 | quit (); |
c906108c | 5186 | } |
74531fed PA |
5187 | else |
5188 | { | |
9e2f0ad4 HZ |
5189 | if (query (_("Interrupted while waiting for the program.\n\ |
5190 | Give up (and stop debugging it)? "))) | |
74531fed | 5191 | { |
78a095c3 | 5192 | remote_unpush_target (); |
039e3c22 | 5193 | quit (); |
74531fed PA |
5194 | } |
5195 | } | |
c906108c SS |
5196 | |
5197 | target_terminal_inferior (); | |
5198 | } | |
5199 | ||
6426a772 JM |
5200 | /* Enable/disable target terminal ownership. Most targets can use |
5201 | terminal groups to control terminal ownership. Remote targets are | |
5202 | different in that explicit transfer of ownership to/from GDB/target | |
23860348 | 5203 | is required. */ |
6426a772 JM |
5204 | |
5205 | static void | |
75c99385 | 5206 | remote_terminal_inferior (void) |
6426a772 | 5207 | { |
c6ebd6cf | 5208 | if (!target_async_permitted) |
75c99385 PA |
5209 | /* Nothing to do. */ |
5210 | return; | |
5211 | ||
d9d2d8b6 PA |
5212 | /* FIXME: cagney/1999-09-27: Make calls to target_terminal_*() |
5213 | idempotent. The event-loop GDB talking to an asynchronous target | |
5214 | with a synchronous command calls this function from both | |
5215 | event-top.c and infrun.c/infcmd.c. Once GDB stops trying to | |
5216 | transfer the terminal to the target when it shouldn't this guard | |
5217 | can go away. */ | |
6426a772 JM |
5218 | if (!remote_async_terminal_ours_p) |
5219 | return; | |
5220 | delete_file_handler (input_fd); | |
5221 | remote_async_terminal_ours_p = 0; | |
934b9bac | 5222 | async_initialize_sigint_signal_handler (); |
6426a772 JM |
5223 | /* NOTE: At this point we could also register our selves as the |
5224 | recipient of all input. Any characters typed could then be | |
23860348 | 5225 | passed on down to the target. */ |
6426a772 JM |
5226 | } |
5227 | ||
5228 | static void | |
75c99385 | 5229 | remote_terminal_ours (void) |
6426a772 | 5230 | { |
c6ebd6cf | 5231 | if (!target_async_permitted) |
75c99385 PA |
5232 | /* Nothing to do. */ |
5233 | return; | |
5234 | ||
5235 | /* See FIXME in remote_terminal_inferior. */ | |
6426a772 JM |
5236 | if (remote_async_terminal_ours_p) |
5237 | return; | |
934b9bac | 5238 | async_cleanup_sigint_signal_handler (NULL); |
6426a772 JM |
5239 | add_file_handler (input_fd, stdin_event_handler, 0); |
5240 | remote_async_terminal_ours_p = 1; | |
5241 | } | |
5242 | ||
176a6961 | 5243 | static void |
917317f4 | 5244 | remote_console_output (char *msg) |
c906108c SS |
5245 | { |
5246 | char *p; | |
5247 | ||
c5aa993b | 5248 | for (p = msg; p[0] && p[1]; p += 2) |
c906108c SS |
5249 | { |
5250 | char tb[2]; | |
5251 | char c = fromhex (p[0]) * 16 + fromhex (p[1]); | |
a744cf53 | 5252 | |
c906108c SS |
5253 | tb[0] = c; |
5254 | tb[1] = 0; | |
43ff13b4 | 5255 | fputs_unfiltered (tb, gdb_stdtarg); |
c906108c | 5256 | } |
00db5b94 PA |
5257 | gdb_flush (gdb_stdtarg); |
5258 | } | |
74531fed PA |
5259 | |
5260 | typedef struct cached_reg | |
5261 | { | |
5262 | int num; | |
5263 | gdb_byte data[MAX_REGISTER_SIZE]; | |
5264 | } cached_reg_t; | |
5265 | ||
5266 | DEF_VEC_O(cached_reg_t); | |
5267 | ||
722247f1 | 5268 | typedef struct stop_reply |
74531fed | 5269 | { |
722247f1 | 5270 | struct notif_event base; |
74531fed | 5271 | |
722247f1 | 5272 | /* The identifier of the thread about this event */ |
74531fed PA |
5273 | ptid_t ptid; |
5274 | ||
340e3c99 | 5275 | /* The remote state this event is associated with. When the remote |
bcc75809 YQ |
5276 | connection, represented by a remote_state object, is closed, |
5277 | all the associated stop_reply events should be released. */ | |
5278 | struct remote_state *rs; | |
5279 | ||
74531fed PA |
5280 | struct target_waitstatus ws; |
5281 | ||
15148d6a PA |
5282 | /* Expedited registers. This makes remote debugging a bit more |
5283 | efficient for those targets that provide critical registers as | |
5284 | part of their normal status mechanism (as another roundtrip to | |
5285 | fetch them is avoided). */ | |
74531fed PA |
5286 | VEC(cached_reg_t) *regcache; |
5287 | ||
5288 | int stopped_by_watchpoint_p; | |
5289 | CORE_ADDR watch_data_address; | |
5290 | ||
dc146f7c | 5291 | int core; |
722247f1 | 5292 | } *stop_reply_p; |
a744cf53 | 5293 | |
722247f1 YQ |
5294 | DECLARE_QUEUE_P (stop_reply_p); |
5295 | DEFINE_QUEUE_P (stop_reply_p); | |
5296 | /* The list of already fetched and acknowledged stop events. This | |
5297 | queue is used for notification Stop, and other notifications | |
5298 | don't need queue for their events, because the notification events | |
5299 | of Stop can't be consumed immediately, so that events should be | |
5300 | queued first, and be consumed by remote_wait_{ns,as} one per | |
5301 | time. Other notifications can consume their events immediately, | |
5302 | so queue is not needed for them. */ | |
5303 | static QUEUE (stop_reply_p) *stop_reply_queue; | |
74531fed PA |
5304 | |
5305 | static void | |
5306 | stop_reply_xfree (struct stop_reply *r) | |
5307 | { | |
f48ff2a7 | 5308 | notif_event_xfree ((struct notif_event *) r); |
c906108c SS |
5309 | } |
5310 | ||
722247f1 YQ |
5311 | static void |
5312 | remote_notif_stop_parse (struct notif_client *self, char *buf, | |
5313 | struct notif_event *event) | |
5314 | { | |
5315 | remote_parse_stop_reply (buf, (struct stop_reply *) event); | |
5316 | } | |
5317 | ||
5318 | static void | |
5319 | remote_notif_stop_ack (struct notif_client *self, char *buf, | |
5320 | struct notif_event *event) | |
5321 | { | |
5322 | struct stop_reply *stop_reply = (struct stop_reply *) event; | |
5323 | ||
5324 | /* acknowledge */ | |
5325 | putpkt ((char *) self->ack_command); | |
5326 | ||
5327 | if (stop_reply->ws.kind == TARGET_WAITKIND_IGNORE) | |
5328 | /* We got an unknown stop reply. */ | |
5329 | error (_("Unknown stop reply")); | |
5330 | ||
5331 | push_stop_reply (stop_reply); | |
5332 | } | |
5333 | ||
5334 | static int | |
5335 | remote_notif_stop_can_get_pending_events (struct notif_client *self) | |
5336 | { | |
5337 | /* We can't get pending events in remote_notif_process for | |
5338 | notification stop, and we have to do this in remote_wait_ns | |
5339 | instead. If we fetch all queued events from stub, remote stub | |
5340 | may exit and we have no chance to process them back in | |
5341 | remote_wait_ns. */ | |
5342 | mark_async_event_handler (remote_async_inferior_event_token); | |
5343 | return 0; | |
5344 | } | |
5345 | ||
5346 | static void | |
5347 | stop_reply_dtr (struct notif_event *event) | |
5348 | { | |
5349 | struct stop_reply *r = (struct stop_reply *) event; | |
5350 | ||
5351 | VEC_free (cached_reg_t, r->regcache); | |
5352 | } | |
5353 | ||
5354 | static struct notif_event * | |
5355 | remote_notif_stop_alloc_reply (void) | |
5356 | { | |
5357 | struct notif_event *r | |
5358 | = (struct notif_event *) XMALLOC (struct stop_reply); | |
5359 | ||
5360 | r->dtr = stop_reply_dtr; | |
5361 | ||
5362 | return r; | |
5363 | } | |
5364 | ||
5365 | /* A client of notification Stop. */ | |
5366 | ||
5367 | struct notif_client notif_client_stop = | |
5368 | { | |
5369 | "Stop", | |
5370 | "vStopped", | |
5371 | remote_notif_stop_parse, | |
5372 | remote_notif_stop_ack, | |
5373 | remote_notif_stop_can_get_pending_events, | |
5374 | remote_notif_stop_alloc_reply, | |
f48ff2a7 | 5375 | REMOTE_NOTIF_STOP, |
722247f1 YQ |
5376 | }; |
5377 | ||
5378 | /* A parameter to pass data in and out. */ | |
5379 | ||
5380 | struct queue_iter_param | |
5381 | { | |
5382 | void *input; | |
5383 | struct stop_reply *output; | |
5384 | }; | |
5385 | ||
f48ff2a7 YQ |
5386 | /* Remove stop replies in the queue if its pid is equal to the given |
5387 | inferior's pid. */ | |
722247f1 YQ |
5388 | |
5389 | static int | |
f48ff2a7 YQ |
5390 | remove_stop_reply_for_inferior (QUEUE (stop_reply_p) *q, |
5391 | QUEUE_ITER (stop_reply_p) *iter, | |
5392 | stop_reply_p event, | |
5393 | void *data) | |
722247f1 YQ |
5394 | { |
5395 | struct queue_iter_param *param = data; | |
5396 | struct inferior *inf = param->input; | |
5397 | ||
f48ff2a7 | 5398 | if (ptid_get_pid (event->ptid) == inf->pid) |
722247f1 YQ |
5399 | { |
5400 | stop_reply_xfree (event); | |
5401 | QUEUE_remove_elem (stop_reply_p, q, iter); | |
5402 | } | |
5403 | ||
5404 | return 1; | |
5405 | } | |
5406 | ||
f48ff2a7 | 5407 | /* Discard all pending stop replies of inferior INF. */ |
c906108c | 5408 | |
74531fed | 5409 | static void |
5f4cf0bb | 5410 | discard_pending_stop_replies (struct inferior *inf) |
c906108c | 5411 | { |
722247f1 YQ |
5412 | int i; |
5413 | struct queue_iter_param param; | |
f48ff2a7 YQ |
5414 | struct stop_reply *reply; |
5415 | struct remote_state *rs = get_remote_state (); | |
5416 | struct remote_notif_state *rns = rs->notif_state; | |
5417 | ||
5418 | /* This function can be notified when an inferior exists. When the | |
5419 | target is not remote, the notification state is NULL. */ | |
5420 | if (rs->remote_desc == NULL) | |
5421 | return; | |
5422 | ||
5423 | reply = (struct stop_reply *) rns->pending_event[notif_client_stop.id]; | |
c906108c | 5424 | |
74531fed | 5425 | /* Discard the in-flight notification. */ |
f48ff2a7 | 5426 | if (reply != NULL && ptid_get_pid (reply->ptid) == inf->pid) |
74531fed | 5427 | { |
722247f1 | 5428 | stop_reply_xfree (reply); |
f48ff2a7 | 5429 | rns->pending_event[notif_client_stop.id] = NULL; |
74531fed | 5430 | } |
c906108c | 5431 | |
722247f1 YQ |
5432 | param.input = inf; |
5433 | param.output = NULL; | |
74531fed PA |
5434 | /* Discard the stop replies we have already pulled with |
5435 | vStopped. */ | |
722247f1 | 5436 | QUEUE_iterate (stop_reply_p, stop_reply_queue, |
f48ff2a7 YQ |
5437 | remove_stop_reply_for_inferior, ¶m); |
5438 | } | |
5439 | ||
bcc75809 YQ |
5440 | /* If its remote state is equal to the given remote state, |
5441 | remove EVENT from the stop reply queue. */ | |
5442 | ||
5443 | static int | |
5444 | remove_stop_reply_of_remote_state (QUEUE (stop_reply_p) *q, | |
5445 | QUEUE_ITER (stop_reply_p) *iter, | |
5446 | stop_reply_p event, | |
5447 | void *data) | |
5448 | { | |
5449 | struct queue_iter_param *param = data; | |
5450 | struct remote_state *rs = param->input; | |
5451 | ||
5452 | if (event->rs == rs) | |
5453 | { | |
5454 | stop_reply_xfree (event); | |
5455 | QUEUE_remove_elem (stop_reply_p, q, iter); | |
5456 | } | |
5457 | ||
5458 | return 1; | |
5459 | } | |
5460 | ||
5461 | /* Discard the stop replies for RS in stop_reply_queue. */ | |
f48ff2a7 YQ |
5462 | |
5463 | static void | |
bcc75809 | 5464 | discard_pending_stop_replies_in_queue (struct remote_state *rs) |
f48ff2a7 YQ |
5465 | { |
5466 | struct queue_iter_param param; | |
5467 | ||
bcc75809 | 5468 | param.input = rs; |
f48ff2a7 YQ |
5469 | param.output = NULL; |
5470 | /* Discard the stop replies we have already pulled with | |
5471 | vStopped. */ | |
5472 | QUEUE_iterate (stop_reply_p, stop_reply_queue, | |
bcc75809 | 5473 | remove_stop_reply_of_remote_state, ¶m); |
722247f1 | 5474 | } |
74531fed | 5475 | |
722247f1 YQ |
5476 | /* A parameter to pass data in and out. */ |
5477 | ||
5478 | static int | |
5479 | remote_notif_remove_once_on_match (QUEUE (stop_reply_p) *q, | |
5480 | QUEUE_ITER (stop_reply_p) *iter, | |
5481 | stop_reply_p event, | |
5482 | void *data) | |
5483 | { | |
5484 | struct queue_iter_param *param = data; | |
5485 | ptid_t *ptid = param->input; | |
5486 | ||
5487 | if (ptid_match (event->ptid, *ptid)) | |
5488 | { | |
5489 | param->output = event; | |
5490 | QUEUE_remove_elem (stop_reply_p, q, iter); | |
5491 | return 0; | |
c8e38a49 | 5492 | } |
722247f1 YQ |
5493 | |
5494 | return 1; | |
74531fed | 5495 | } |
43ff13b4 | 5496 | |
722247f1 YQ |
5497 | /* Remove the first reply in 'stop_reply_queue' which matches |
5498 | PTID. */ | |
2e9f7625 | 5499 | |
722247f1 YQ |
5500 | static struct stop_reply * |
5501 | remote_notif_remove_queued_reply (ptid_t ptid) | |
74531fed | 5502 | { |
722247f1 YQ |
5503 | struct queue_iter_param param; |
5504 | ||
5505 | param.input = &ptid; | |
5506 | param.output = NULL; | |
5507 | ||
5508 | QUEUE_iterate (stop_reply_p, stop_reply_queue, | |
5509 | remote_notif_remove_once_on_match, ¶m); | |
5510 | if (notif_debug) | |
5511 | fprintf_unfiltered (gdb_stdlog, | |
5512 | "notif: discard queued event: 'Stop' in %s\n", | |
5513 | target_pid_to_str (ptid)); | |
a744cf53 | 5514 | |
722247f1 | 5515 | return param.output; |
74531fed | 5516 | } |
75c99385 | 5517 | |
74531fed PA |
5518 | /* Look for a queued stop reply belonging to PTID. If one is found, |
5519 | remove it from the queue, and return it. Returns NULL if none is | |
5520 | found. If there are still queued events left to process, tell the | |
5521 | event loop to get back to target_wait soon. */ | |
e24a49d8 | 5522 | |
74531fed PA |
5523 | static struct stop_reply * |
5524 | queued_stop_reply (ptid_t ptid) | |
5525 | { | |
722247f1 | 5526 | struct stop_reply *r = remote_notif_remove_queued_reply (ptid); |
74531fed | 5527 | |
722247f1 | 5528 | if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue)) |
74531fed PA |
5529 | /* There's still at least an event left. */ |
5530 | mark_async_event_handler (remote_async_inferior_event_token); | |
5531 | ||
722247f1 | 5532 | return r; |
74531fed PA |
5533 | } |
5534 | ||
5535 | /* Push a fully parsed stop reply in the stop reply queue. Since we | |
5536 | know that we now have at least one queued event left to pass to the | |
5537 | core side, tell the event loop to get back to target_wait soon. */ | |
5538 | ||
5539 | static void | |
5540 | push_stop_reply (struct stop_reply *new_event) | |
5541 | { | |
722247f1 | 5542 | QUEUE_enque (stop_reply_p, stop_reply_queue, new_event); |
74531fed | 5543 | |
722247f1 YQ |
5544 | if (notif_debug) |
5545 | fprintf_unfiltered (gdb_stdlog, | |
5546 | "notif: push 'Stop' %s to queue %d\n", | |
5547 | target_pid_to_str (new_event->ptid), | |
5548 | QUEUE_length (stop_reply_p, | |
5549 | stop_reply_queue)); | |
74531fed PA |
5550 | |
5551 | mark_async_event_handler (remote_async_inferior_event_token); | |
5552 | } | |
5553 | ||
722247f1 YQ |
5554 | static int |
5555 | stop_reply_match_ptid_and_ws (QUEUE (stop_reply_p) *q, | |
5556 | QUEUE_ITER (stop_reply_p) *iter, | |
5557 | struct stop_reply *event, | |
5558 | void *data) | |
5559 | { | |
5560 | ptid_t *ptid = data; | |
5561 | ||
5562 | return !(ptid_equal (*ptid, event->ptid) | |
5563 | && event->ws.kind == TARGET_WAITKIND_STOPPED); | |
5564 | } | |
5565 | ||
74531fed PA |
5566 | /* Returns true if we have a stop reply for PTID. */ |
5567 | ||
5568 | static int | |
5569 | peek_stop_reply (ptid_t ptid) | |
5570 | { | |
722247f1 YQ |
5571 | return !QUEUE_iterate (stop_reply_p, stop_reply_queue, |
5572 | stop_reply_match_ptid_and_ws, &ptid); | |
74531fed PA |
5573 | } |
5574 | ||
5575 | /* Parse the stop reply in BUF. Either the function succeeds, and the | |
5576 | result is stored in EVENT, or throws an error. */ | |
5577 | ||
5578 | static void | |
5579 | remote_parse_stop_reply (char *buf, struct stop_reply *event) | |
5580 | { | |
5581 | struct remote_arch_state *rsa = get_remote_arch_state (); | |
5582 | ULONGEST addr; | |
5583 | char *p; | |
5584 | ||
5585 | event->ptid = null_ptid; | |
bcc75809 | 5586 | event->rs = get_remote_state (); |
74531fed PA |
5587 | event->ws.kind = TARGET_WAITKIND_IGNORE; |
5588 | event->ws.value.integer = 0; | |
74531fed PA |
5589 | event->stopped_by_watchpoint_p = 0; |
5590 | event->regcache = NULL; | |
dc146f7c | 5591 | event->core = -1; |
74531fed PA |
5592 | |
5593 | switch (buf[0]) | |
5594 | { | |
5595 | case 'T': /* Status with PC, SP, FP, ... */ | |
cea39f65 MS |
5596 | /* Expedited reply, containing Signal, {regno, reg} repeat. */ |
5597 | /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where | |
5598 | ss = signal number | |
5599 | n... = register number | |
5600 | r... = register contents | |
5601 | */ | |
5602 | ||
5603 | p = &buf[3]; /* after Txx */ | |
5604 | while (*p) | |
5605 | { | |
5606 | char *p1; | |
5607 | char *p_temp; | |
5608 | int fieldsize; | |
5609 | LONGEST pnum = 0; | |
43ff13b4 | 5610 | |
cea39f65 MS |
5611 | /* If the packet contains a register number, save it in |
5612 | pnum and set p1 to point to the character following it. | |
5613 | Otherwise p1 points to p. */ | |
3c3bea1c | 5614 | |
cea39f65 MS |
5615 | /* If this packet is an awatch packet, don't parse the 'a' |
5616 | as a register number. */ | |
c8e38a49 | 5617 | |
dc146f7c VP |
5618 | if (strncmp (p, "awatch", strlen("awatch")) != 0 |
5619 | && strncmp (p, "core", strlen ("core") != 0)) | |
cea39f65 MS |
5620 | { |
5621 | /* Read the ``P'' register number. */ | |
5622 | pnum = strtol (p, &p_temp, 16); | |
5623 | p1 = p_temp; | |
5624 | } | |
5625 | else | |
5626 | p1 = p; | |
802188a7 | 5627 | |
cea39f65 MS |
5628 | if (p1 == p) /* No register number present here. */ |
5629 | { | |
5630 | p1 = strchr (p, ':'); | |
5631 | if (p1 == NULL) | |
5632 | error (_("Malformed packet(a) (missing colon): %s\n\ | |
c8e38a49 | 5633 | Packet: '%s'\n"), |
cea39f65 MS |
5634 | p, buf); |
5635 | if (strncmp (p, "thread", p1 - p) == 0) | |
5636 | event->ptid = read_ptid (++p1, &p); | |
5637 | else if ((strncmp (p, "watch", p1 - p) == 0) | |
5638 | || (strncmp (p, "rwatch", p1 - p) == 0) | |
5639 | || (strncmp (p, "awatch", p1 - p) == 0)) | |
5640 | { | |
5641 | event->stopped_by_watchpoint_p = 1; | |
5642 | p = unpack_varlen_hex (++p1, &addr); | |
5643 | event->watch_data_address = (CORE_ADDR) addr; | |
5644 | } | |
5645 | else if (strncmp (p, "library", p1 - p) == 0) | |
5646 | { | |
5647 | p1++; | |
5648 | p_temp = p1; | |
5649 | while (*p_temp && *p_temp != ';') | |
5650 | p_temp++; | |
c8e38a49 | 5651 | |
5b5596ff | 5652 | event->ws.kind = TARGET_WAITKIND_LOADED; |
cea39f65 MS |
5653 | p = p_temp; |
5654 | } | |
5655 | else if (strncmp (p, "replaylog", p1 - p) == 0) | |
5656 | { | |
5b5596ff PA |
5657 | event->ws.kind = TARGET_WAITKIND_NO_HISTORY; |
5658 | /* p1 will indicate "begin" or "end", but it makes | |
5659 | no difference for now, so ignore it. */ | |
cea39f65 MS |
5660 | p_temp = strchr (p1 + 1, ';'); |
5661 | if (p_temp) | |
c8e38a49 | 5662 | p = p_temp; |
cea39f65 | 5663 | } |
dc146f7c VP |
5664 | else if (strncmp (p, "core", p1 - p) == 0) |
5665 | { | |
5666 | ULONGEST c; | |
a744cf53 | 5667 | |
dc146f7c VP |
5668 | p = unpack_varlen_hex (++p1, &c); |
5669 | event->core = c; | |
5670 | } | |
cea39f65 MS |
5671 | else |
5672 | { | |
5673 | /* Silently skip unknown optional info. */ | |
5674 | p_temp = strchr (p1 + 1, ';'); | |
5675 | if (p_temp) | |
5676 | p = p_temp; | |
5677 | } | |
5678 | } | |
5679 | else | |
5680 | { | |
5681 | struct packet_reg *reg = packet_reg_from_pnum (rsa, pnum); | |
5682 | cached_reg_t cached_reg; | |
74531fed | 5683 | |
cea39f65 | 5684 | p = p1; |
75c99385 | 5685 | |
cea39f65 MS |
5686 | if (*p != ':') |
5687 | error (_("Malformed packet(b) (missing colon): %s\n\ | |
8a3fe4f8 | 5688 | Packet: '%s'\n"), |
cea39f65 MS |
5689 | p, buf); |
5690 | ++p; | |
43ff13b4 | 5691 | |
cea39f65 MS |
5692 | if (reg == NULL) |
5693 | error (_("Remote sent bad register number %s: %s\n\ | |
8a3fe4f8 | 5694 | Packet: '%s'\n"), |
7c47795c | 5695 | hex_string (pnum), p, buf); |
c8e38a49 | 5696 | |
cea39f65 | 5697 | cached_reg.num = reg->regnum; |
4100683b | 5698 | |
cea39f65 | 5699 | fieldsize = hex2bin (p, cached_reg.data, |
f5656ead | 5700 | register_size (target_gdbarch (), |
cea39f65 MS |
5701 | reg->regnum)); |
5702 | p += 2 * fieldsize; | |
f5656ead | 5703 | if (fieldsize < register_size (target_gdbarch (), |
cea39f65 MS |
5704 | reg->regnum)) |
5705 | warning (_("Remote reply is too short: %s"), buf); | |
74531fed | 5706 | |
cea39f65 MS |
5707 | VEC_safe_push (cached_reg_t, event->regcache, &cached_reg); |
5708 | } | |
c8e38a49 | 5709 | |
cea39f65 MS |
5710 | if (*p != ';') |
5711 | error (_("Remote register badly formatted: %s\nhere: %s"), | |
5712 | buf, p); | |
5713 | ++p; | |
5714 | } | |
5b5596ff PA |
5715 | |
5716 | if (event->ws.kind != TARGET_WAITKIND_IGNORE) | |
5717 | break; | |
5718 | ||
c8e38a49 PA |
5719 | /* fall through */ |
5720 | case 'S': /* Old style status, just signal only. */ | |
3a09da41 PA |
5721 | { |
5722 | int sig; | |
5723 | ||
5724 | event->ws.kind = TARGET_WAITKIND_STOPPED; | |
5725 | sig = (fromhex (buf[1]) << 4) + fromhex (buf[2]); | |
5726 | if (GDB_SIGNAL_FIRST <= sig && sig < GDB_SIGNAL_LAST) | |
5727 | event->ws.value.sig = (enum gdb_signal) sig; | |
5728 | else | |
5729 | event->ws.value.sig = GDB_SIGNAL_UNKNOWN; | |
5730 | } | |
c8e38a49 PA |
5731 | break; |
5732 | case 'W': /* Target exited. */ | |
5733 | case 'X': | |
5734 | { | |
5735 | char *p; | |
5736 | int pid; | |
5737 | ULONGEST value; | |
82f73884 | 5738 | |
c8e38a49 PA |
5739 | /* GDB used to accept only 2 hex chars here. Stubs should |
5740 | only send more if they detect GDB supports multi-process | |
5741 | support. */ | |
5742 | p = unpack_varlen_hex (&buf[1], &value); | |
82f73884 | 5743 | |
c8e38a49 PA |
5744 | if (buf[0] == 'W') |
5745 | { | |
5746 | /* The remote process exited. */ | |
74531fed PA |
5747 | event->ws.kind = TARGET_WAITKIND_EXITED; |
5748 | event->ws.value.integer = value; | |
c8e38a49 PA |
5749 | } |
5750 | else | |
5751 | { | |
5752 | /* The remote process exited with a signal. */ | |
74531fed | 5753 | event->ws.kind = TARGET_WAITKIND_SIGNALLED; |
3a09da41 PA |
5754 | if (GDB_SIGNAL_FIRST <= value && value < GDB_SIGNAL_LAST) |
5755 | event->ws.value.sig = (enum gdb_signal) value; | |
5756 | else | |
5757 | event->ws.value.sig = GDB_SIGNAL_UNKNOWN; | |
c8e38a49 | 5758 | } |
82f73884 | 5759 | |
c8e38a49 PA |
5760 | /* If no process is specified, assume inferior_ptid. */ |
5761 | pid = ptid_get_pid (inferior_ptid); | |
5762 | if (*p == '\0') | |
5763 | ; | |
5764 | else if (*p == ';') | |
5765 | { | |
5766 | p++; | |
5767 | ||
5768 | if (p == '\0') | |
82f73884 | 5769 | ; |
c8e38a49 PA |
5770 | else if (strncmp (p, |
5771 | "process:", sizeof ("process:") - 1) == 0) | |
82f73884 | 5772 | { |
c8e38a49 | 5773 | ULONGEST upid; |
a744cf53 | 5774 | |
c8e38a49 PA |
5775 | p += sizeof ("process:") - 1; |
5776 | unpack_varlen_hex (p, &upid); | |
5777 | pid = upid; | |
82f73884 PA |
5778 | } |
5779 | else | |
5780 | error (_("unknown stop reply packet: %s"), buf); | |
43ff13b4 | 5781 | } |
c8e38a49 PA |
5782 | else |
5783 | error (_("unknown stop reply packet: %s"), buf); | |
74531fed PA |
5784 | event->ptid = pid_to_ptid (pid); |
5785 | } | |
5786 | break; | |
5787 | } | |
5788 | ||
5789 | if (non_stop && ptid_equal (event->ptid, null_ptid)) | |
5790 | error (_("No process or thread specified in stop reply: %s"), buf); | |
5791 | } | |
5792 | ||
722247f1 YQ |
5793 | /* When the stub wants to tell GDB about a new notification reply, it |
5794 | sends a notification (%Stop, for example). Those can come it at | |
5795 | any time, hence, we have to make sure that any pending | |
5796 | putpkt/getpkt sequence we're making is finished, before querying | |
5797 | the stub for more events with the corresponding ack command | |
5798 | (vStopped, for example). E.g., if we started a vStopped sequence | |
5799 | immediately upon receiving the notification, something like this | |
5800 | could happen: | |
74531fed PA |
5801 | |
5802 | 1.1) --> Hg 1 | |
5803 | 1.2) <-- OK | |
5804 | 1.3) --> g | |
5805 | 1.4) <-- %Stop | |
5806 | 1.5) --> vStopped | |
5807 | 1.6) <-- (registers reply to step #1.3) | |
5808 | ||
5809 | Obviously, the reply in step #1.6 would be unexpected to a vStopped | |
5810 | query. | |
5811 | ||
796cb314 | 5812 | To solve this, whenever we parse a %Stop notification successfully, |
74531fed PA |
5813 | we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on |
5814 | doing whatever we were doing: | |
5815 | ||
5816 | 2.1) --> Hg 1 | |
5817 | 2.2) <-- OK | |
5818 | 2.3) --> g | |
5819 | 2.4) <-- %Stop | |
5820 | <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN> | |
5821 | 2.5) <-- (registers reply to step #2.3) | |
5822 | ||
5823 | Eventualy after step #2.5, we return to the event loop, which | |
5824 | notices there's an event on the | |
5825 | REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the | |
5826 | associated callback --- the function below. At this point, we're | |
5827 | always safe to start a vStopped sequence. : | |
5828 | ||
5829 | 2.6) --> vStopped | |
5830 | 2.7) <-- T05 thread:2 | |
5831 | 2.8) --> vStopped | |
5832 | 2.9) --> OK | |
5833 | */ | |
5834 | ||
722247f1 YQ |
5835 | void |
5836 | remote_notif_get_pending_events (struct notif_client *nc) | |
74531fed PA |
5837 | { |
5838 | struct remote_state *rs = get_remote_state (); | |
74531fed | 5839 | |
f48ff2a7 | 5840 | if (rs->notif_state->pending_event[nc->id] != NULL) |
74531fed | 5841 | { |
722247f1 YQ |
5842 | if (notif_debug) |
5843 | fprintf_unfiltered (gdb_stdlog, | |
5844 | "notif: process: '%s' ack pending event\n", | |
5845 | nc->name); | |
74531fed | 5846 | |
722247f1 | 5847 | /* acknowledge */ |
f48ff2a7 YQ |
5848 | nc->ack (nc, rs->buf, rs->notif_state->pending_event[nc->id]); |
5849 | rs->notif_state->pending_event[nc->id] = NULL; | |
74531fed PA |
5850 | |
5851 | while (1) | |
5852 | { | |
5853 | getpkt (&rs->buf, &rs->buf_size, 0); | |
5854 | if (strcmp (rs->buf, "OK") == 0) | |
5855 | break; | |
5856 | else | |
722247f1 | 5857 | remote_notif_ack (nc, rs->buf); |
74531fed PA |
5858 | } |
5859 | } | |
722247f1 YQ |
5860 | else |
5861 | { | |
5862 | if (notif_debug) | |
5863 | fprintf_unfiltered (gdb_stdlog, | |
5864 | "notif: process: '%s' no pending reply\n", | |
5865 | nc->name); | |
5866 | } | |
74531fed PA |
5867 | } |
5868 | ||
74531fed PA |
5869 | /* Called when it is decided that STOP_REPLY holds the info of the |
5870 | event that is to be returned to the core. This function always | |
5871 | destroys STOP_REPLY. */ | |
5872 | ||
5873 | static ptid_t | |
5874 | process_stop_reply (struct stop_reply *stop_reply, | |
5875 | struct target_waitstatus *status) | |
5876 | { | |
5877 | ptid_t ptid; | |
5878 | ||
5879 | *status = stop_reply->ws; | |
5880 | ptid = stop_reply->ptid; | |
5881 | ||
5882 | /* If no thread/process was reported by the stub, assume the current | |
5883 | inferior. */ | |
5884 | if (ptid_equal (ptid, null_ptid)) | |
5885 | ptid = inferior_ptid; | |
5886 | ||
5f3563ea PA |
5887 | if (status->kind != TARGET_WAITKIND_EXITED |
5888 | && status->kind != TARGET_WAITKIND_SIGNALLED) | |
74531fed | 5889 | { |
ee154bee TT |
5890 | struct remote_state *rs = get_remote_state (); |
5891 | ||
5f3563ea PA |
5892 | /* Expedited registers. */ |
5893 | if (stop_reply->regcache) | |
5894 | { | |
217f1f79 | 5895 | struct regcache *regcache |
f5656ead | 5896 | = get_thread_arch_regcache (ptid, target_gdbarch ()); |
5f3563ea PA |
5897 | cached_reg_t *reg; |
5898 | int ix; | |
5899 | ||
5900 | for (ix = 0; | |
5901 | VEC_iterate(cached_reg_t, stop_reply->regcache, ix, reg); | |
5902 | ix++) | |
217f1f79 | 5903 | regcache_raw_supply (regcache, reg->num, reg->data); |
5f3563ea PA |
5904 | VEC_free (cached_reg_t, stop_reply->regcache); |
5905 | } | |
74531fed | 5906 | |
ee154bee TT |
5907 | rs->remote_stopped_by_watchpoint_p = stop_reply->stopped_by_watchpoint_p; |
5908 | rs->remote_watch_data_address = stop_reply->watch_data_address; | |
1941c569 PA |
5909 | |
5910 | remote_notice_new_inferior (ptid, 0); | |
dc146f7c | 5911 | demand_private_info (ptid)->core = stop_reply->core; |
74531fed PA |
5912 | } |
5913 | ||
74531fed PA |
5914 | stop_reply_xfree (stop_reply); |
5915 | return ptid; | |
5916 | } | |
5917 | ||
5918 | /* The non-stop mode version of target_wait. */ | |
5919 | ||
5920 | static ptid_t | |
47608cb1 | 5921 | remote_wait_ns (ptid_t ptid, struct target_waitstatus *status, int options) |
74531fed PA |
5922 | { |
5923 | struct remote_state *rs = get_remote_state (); | |
74531fed PA |
5924 | struct stop_reply *stop_reply; |
5925 | int ret; | |
fee9eda9 | 5926 | int is_notif = 0; |
74531fed PA |
5927 | |
5928 | /* If in non-stop mode, get out of getpkt even if a | |
5929 | notification is received. */ | |
5930 | ||
5931 | ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size, | |
fee9eda9 | 5932 | 0 /* forever */, &is_notif); |
74531fed PA |
5933 | while (1) |
5934 | { | |
fee9eda9 | 5935 | if (ret != -1 && !is_notif) |
74531fed PA |
5936 | switch (rs->buf[0]) |
5937 | { | |
5938 | case 'E': /* Error of some sort. */ | |
5939 | /* We're out of sync with the target now. Did it continue | |
5940 | or not? We can't tell which thread it was in non-stop, | |
5941 | so just ignore this. */ | |
5942 | warning (_("Remote failure reply: %s"), rs->buf); | |
5943 | break; | |
5944 | case 'O': /* Console output. */ | |
5945 | remote_console_output (rs->buf + 1); | |
5946 | break; | |
5947 | default: | |
5948 | warning (_("Invalid remote reply: %s"), rs->buf); | |
5949 | break; | |
5950 | } | |
5951 | ||
5952 | /* Acknowledge a pending stop reply that may have arrived in the | |
5953 | mean time. */ | |
f48ff2a7 | 5954 | if (rs->notif_state->pending_event[notif_client_stop.id] != NULL) |
722247f1 | 5955 | remote_notif_get_pending_events (¬if_client_stop); |
74531fed PA |
5956 | |
5957 | /* If indeed we noticed a stop reply, we're done. */ | |
5958 | stop_reply = queued_stop_reply (ptid); | |
5959 | if (stop_reply != NULL) | |
5960 | return process_stop_reply (stop_reply, status); | |
5961 | ||
47608cb1 | 5962 | /* Still no event. If we're just polling for an event, then |
74531fed | 5963 | return to the event loop. */ |
47608cb1 | 5964 | if (options & TARGET_WNOHANG) |
74531fed PA |
5965 | { |
5966 | status->kind = TARGET_WAITKIND_IGNORE; | |
5967 | return minus_one_ptid; | |
5968 | } | |
5969 | ||
47608cb1 | 5970 | /* Otherwise do a blocking wait. */ |
74531fed | 5971 | ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size, |
fee9eda9 | 5972 | 1 /* forever */, &is_notif); |
74531fed PA |
5973 | } |
5974 | } | |
5975 | ||
5976 | /* Wait until the remote machine stops, then return, storing status in | |
5977 | STATUS just as `wait' would. */ | |
5978 | ||
5979 | static ptid_t | |
47608cb1 | 5980 | remote_wait_as (ptid_t ptid, struct target_waitstatus *status, int options) |
74531fed PA |
5981 | { |
5982 | struct remote_state *rs = get_remote_state (); | |
74531fed | 5983 | ptid_t event_ptid = null_ptid; |
cea39f65 | 5984 | char *buf; |
74531fed PA |
5985 | struct stop_reply *stop_reply; |
5986 | ||
47608cb1 PA |
5987 | again: |
5988 | ||
74531fed PA |
5989 | status->kind = TARGET_WAITKIND_IGNORE; |
5990 | status->value.integer = 0; | |
5991 | ||
5992 | stop_reply = queued_stop_reply (ptid); | |
5993 | if (stop_reply != NULL) | |
5994 | return process_stop_reply (stop_reply, status); | |
5995 | ||
5996 | if (rs->cached_wait_status) | |
5997 | /* Use the cached wait status, but only once. */ | |
5998 | rs->cached_wait_status = 0; | |
5999 | else | |
6000 | { | |
6001 | int ret; | |
722247f1 | 6002 | int is_notif; |
74531fed PA |
6003 | |
6004 | if (!target_is_async_p ()) | |
6005 | { | |
934b9bac | 6006 | ofunc = signal (SIGINT, sync_remote_interrupt); |
74531fed PA |
6007 | /* If the user hit C-c before this packet, or between packets, |
6008 | pretend that it was hit right here. */ | |
522002f9 | 6009 | if (check_quit_flag ()) |
74531fed | 6010 | { |
522002f9 | 6011 | clear_quit_flag (); |
934b9bac | 6012 | sync_remote_interrupt (SIGINT); |
74531fed PA |
6013 | } |
6014 | } | |
6015 | ||
6016 | /* FIXME: cagney/1999-09-27: If we're in async mode we should | |
6017 | _never_ wait for ever -> test on target_is_async_p(). | |
6018 | However, before we do that we need to ensure that the caller | |
6019 | knows how to take the target into/out of async mode. */ | |
722247f1 YQ |
6020 | ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size, |
6021 | wait_forever_enabled_p, &is_notif); | |
6022 | ||
5e1b953b SDJ |
6023 | if (!target_is_async_p ()) |
6024 | signal (SIGINT, ofunc); | |
6025 | ||
722247f1 YQ |
6026 | /* GDB gets a notification. Return to core as this event is |
6027 | not interesting. */ | |
6028 | if (ret != -1 && is_notif) | |
6029 | return minus_one_ptid; | |
74531fed PA |
6030 | } |
6031 | ||
6032 | buf = rs->buf; | |
6033 | ||
ee154bee | 6034 | rs->remote_stopped_by_watchpoint_p = 0; |
74531fed PA |
6035 | |
6036 | /* We got something. */ | |
6037 | rs->waiting_for_stop_reply = 0; | |
6038 | ||
3a29589a DJ |
6039 | /* Assume that the target has acknowledged Ctrl-C unless we receive |
6040 | an 'F' or 'O' packet. */ | |
6041 | if (buf[0] != 'F' && buf[0] != 'O') | |
6042 | rs->ctrlc_pending_p = 0; | |
6043 | ||
74531fed PA |
6044 | switch (buf[0]) |
6045 | { | |
6046 | case 'E': /* Error of some sort. */ | |
6047 | /* We're out of sync with the target now. Did it continue or | |
6048 | not? Not is more likely, so report a stop. */ | |
6049 | warning (_("Remote failure reply: %s"), buf); | |
6050 | status->kind = TARGET_WAITKIND_STOPPED; | |
a493e3e2 | 6051 | status->value.sig = GDB_SIGNAL_0; |
74531fed PA |
6052 | break; |
6053 | case 'F': /* File-I/O request. */ | |
3a29589a DJ |
6054 | remote_fileio_request (buf, rs->ctrlc_pending_p); |
6055 | rs->ctrlc_pending_p = 0; | |
74531fed PA |
6056 | break; |
6057 | case 'T': case 'S': case 'X': case 'W': | |
6058 | { | |
722247f1 YQ |
6059 | struct stop_reply *stop_reply |
6060 | = (struct stop_reply *) remote_notif_parse (¬if_client_stop, | |
6061 | rs->buf); | |
74531fed | 6062 | |
74531fed | 6063 | event_ptid = process_stop_reply (stop_reply, status); |
c8e38a49 PA |
6064 | break; |
6065 | } | |
6066 | case 'O': /* Console output. */ | |
6067 | remote_console_output (buf + 1); | |
e24a49d8 | 6068 | |
c8e38a49 PA |
6069 | /* The target didn't really stop; keep waiting. */ |
6070 | rs->waiting_for_stop_reply = 1; | |
e24a49d8 | 6071 | |
c8e38a49 PA |
6072 | break; |
6073 | case '\0': | |
b73be471 | 6074 | if (rs->last_sent_signal != GDB_SIGNAL_0) |
c8e38a49 PA |
6075 | { |
6076 | /* Zero length reply means that we tried 'S' or 'C' and the | |
6077 | remote system doesn't support it. */ | |
6078 | target_terminal_ours_for_output (); | |
6079 | printf_filtered | |
6080 | ("Can't send signals to this remote system. %s not sent.\n", | |
b73be471 TT |
6081 | gdb_signal_to_name (rs->last_sent_signal)); |
6082 | rs->last_sent_signal = GDB_SIGNAL_0; | |
c8e38a49 PA |
6083 | target_terminal_inferior (); |
6084 | ||
280ceea3 | 6085 | strcpy ((char *) buf, rs->last_sent_step ? "s" : "c"); |
c8e38a49 PA |
6086 | putpkt ((char *) buf); |
6087 | ||
6088 | /* We just told the target to resume, so a stop reply is in | |
6089 | order. */ | |
e24a49d8 | 6090 | rs->waiting_for_stop_reply = 1; |
c8e38a49 | 6091 | break; |
43ff13b4 | 6092 | } |
c8e38a49 PA |
6093 | /* else fallthrough */ |
6094 | default: | |
6095 | warning (_("Invalid remote reply: %s"), buf); | |
6096 | /* Keep waiting. */ | |
6097 | rs->waiting_for_stop_reply = 1; | |
6098 | break; | |
43ff13b4 | 6099 | } |
c8e38a49 | 6100 | |
c8e38a49 | 6101 | if (status->kind == TARGET_WAITKIND_IGNORE) |
47608cb1 PA |
6102 | { |
6103 | /* Nothing interesting happened. If we're doing a non-blocking | |
6104 | poll, we're done. Otherwise, go back to waiting. */ | |
6105 | if (options & TARGET_WNOHANG) | |
6106 | return minus_one_ptid; | |
6107 | else | |
6108 | goto again; | |
6109 | } | |
74531fed PA |
6110 | else if (status->kind != TARGET_WAITKIND_EXITED |
6111 | && status->kind != TARGET_WAITKIND_SIGNALLED) | |
82f73884 PA |
6112 | { |
6113 | if (!ptid_equal (event_ptid, null_ptid)) | |
47f8a51d | 6114 | record_currthread (rs, event_ptid); |
82f73884 PA |
6115 | else |
6116 | event_ptid = inferior_ptid; | |
43ff13b4 | 6117 | } |
74531fed PA |
6118 | else |
6119 | /* A process exit. Invalidate our notion of current thread. */ | |
47f8a51d | 6120 | record_currthread (rs, minus_one_ptid); |
79d7f229 | 6121 | |
82f73884 | 6122 | return event_ptid; |
43ff13b4 JM |
6123 | } |
6124 | ||
74531fed PA |
6125 | /* Wait until the remote machine stops, then return, storing status in |
6126 | STATUS just as `wait' would. */ | |
6127 | ||
c8e38a49 | 6128 | static ptid_t |
117de6a9 | 6129 | remote_wait (struct target_ops *ops, |
47608cb1 | 6130 | ptid_t ptid, struct target_waitstatus *status, int options) |
c8e38a49 PA |
6131 | { |
6132 | ptid_t event_ptid; | |
6133 | ||
74531fed | 6134 | if (non_stop) |
47608cb1 | 6135 | event_ptid = remote_wait_ns (ptid, status, options); |
74531fed | 6136 | else |
47608cb1 | 6137 | event_ptid = remote_wait_as (ptid, status, options); |
c8e38a49 | 6138 | |
74531fed | 6139 | if (target_can_async_p ()) |
c8e38a49 | 6140 | { |
74531fed PA |
6141 | /* If there are are events left in the queue tell the event loop |
6142 | to return here. */ | |
722247f1 | 6143 | if (!QUEUE_is_empty (stop_reply_p, stop_reply_queue)) |
74531fed | 6144 | mark_async_event_handler (remote_async_inferior_event_token); |
c8e38a49 | 6145 | } |
c8e38a49 PA |
6146 | |
6147 | return event_ptid; | |
6148 | } | |
6149 | ||
74ca34ce | 6150 | /* Fetch a single register using a 'p' packet. */ |
c906108c | 6151 | |
b96ec7ac | 6152 | static int |
56be3814 | 6153 | fetch_register_using_p (struct regcache *regcache, struct packet_reg *reg) |
b96ec7ac AC |
6154 | { |
6155 | struct remote_state *rs = get_remote_state (); | |
2e9f7625 | 6156 | char *buf, *p; |
b96ec7ac AC |
6157 | char regp[MAX_REGISTER_SIZE]; |
6158 | int i; | |
6159 | ||
74ca34ce DJ |
6160 | if (remote_protocol_packets[PACKET_p].support == PACKET_DISABLE) |
6161 | return 0; | |
6162 | ||
6163 | if (reg->pnum == -1) | |
6164 | return 0; | |
6165 | ||
2e9f7625 | 6166 | p = rs->buf; |
fcad0fa4 | 6167 | *p++ = 'p'; |
74ca34ce | 6168 | p += hexnumstr (p, reg->pnum); |
fcad0fa4 | 6169 | *p++ = '\0'; |
1f4437a4 MS |
6170 | putpkt (rs->buf); |
6171 | getpkt (&rs->buf, &rs->buf_size, 0); | |
3f9a994c | 6172 | |
2e9f7625 DJ |
6173 | buf = rs->buf; |
6174 | ||
74ca34ce DJ |
6175 | switch (packet_ok (buf, &remote_protocol_packets[PACKET_p])) |
6176 | { | |
6177 | case PACKET_OK: | |
6178 | break; | |
6179 | case PACKET_UNKNOWN: | |
6180 | return 0; | |
6181 | case PACKET_ERROR: | |
27a9c0bf MS |
6182 | error (_("Could not fetch register \"%s\"; remote failure reply '%s'"), |
6183 | gdbarch_register_name (get_regcache_arch (regcache), | |
6184 | reg->regnum), | |
6185 | buf); | |
74ca34ce | 6186 | } |
3f9a994c JB |
6187 | |
6188 | /* If this register is unfetchable, tell the regcache. */ | |
6189 | if (buf[0] == 'x') | |
8480adf2 | 6190 | { |
56be3814 | 6191 | regcache_raw_supply (regcache, reg->regnum, NULL); |
8480adf2 | 6192 | return 1; |
b96ec7ac | 6193 | } |
b96ec7ac | 6194 | |
3f9a994c JB |
6195 | /* Otherwise, parse and supply the value. */ |
6196 | p = buf; | |
6197 | i = 0; | |
6198 | while (p[0] != 0) | |
6199 | { | |
6200 | if (p[1] == 0) | |
74ca34ce | 6201 | error (_("fetch_register_using_p: early buf termination")); |
3f9a994c JB |
6202 | |
6203 | regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]); | |
6204 | p += 2; | |
6205 | } | |
56be3814 | 6206 | regcache_raw_supply (regcache, reg->regnum, regp); |
3f9a994c | 6207 | return 1; |
b96ec7ac AC |
6208 | } |
6209 | ||
74ca34ce DJ |
6210 | /* Fetch the registers included in the target's 'g' packet. */ |
6211 | ||
29709017 DJ |
6212 | static int |
6213 | send_g_packet (void) | |
c906108c | 6214 | { |
d01949b6 | 6215 | struct remote_state *rs = get_remote_state (); |
cea39f65 | 6216 | int buf_len; |
c906108c | 6217 | |
bba74b36 | 6218 | xsnprintf (rs->buf, get_remote_packet_size (), "g"); |
74ca34ce | 6219 | remote_send (&rs->buf, &rs->buf_size); |
c906108c | 6220 | |
29709017 DJ |
6221 | /* We can get out of synch in various cases. If the first character |
6222 | in the buffer is not a hex character, assume that has happened | |
6223 | and try to fetch another packet to read. */ | |
6224 | while ((rs->buf[0] < '0' || rs->buf[0] > '9') | |
6225 | && (rs->buf[0] < 'A' || rs->buf[0] > 'F') | |
6226 | && (rs->buf[0] < 'a' || rs->buf[0] > 'f') | |
6227 | && rs->buf[0] != 'x') /* New: unavailable register value. */ | |
6228 | { | |
6229 | if (remote_debug) | |
6230 | fprintf_unfiltered (gdb_stdlog, | |
6231 | "Bad register packet; fetching a new packet\n"); | |
6232 | getpkt (&rs->buf, &rs->buf_size, 0); | |
6233 | } | |
6234 | ||
74ca34ce DJ |
6235 | buf_len = strlen (rs->buf); |
6236 | ||
6237 | /* Sanity check the received packet. */ | |
6238 | if (buf_len % 2 != 0) | |
6239 | error (_("Remote 'g' packet reply is of odd length: %s"), rs->buf); | |
29709017 DJ |
6240 | |
6241 | return buf_len / 2; | |
6242 | } | |
6243 | ||
6244 | static void | |
56be3814 | 6245 | process_g_packet (struct regcache *regcache) |
29709017 | 6246 | { |
4a22f64d | 6247 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
29709017 DJ |
6248 | struct remote_state *rs = get_remote_state (); |
6249 | struct remote_arch_state *rsa = get_remote_arch_state (); | |
6250 | int i, buf_len; | |
6251 | char *p; | |
6252 | char *regs; | |
6253 | ||
6254 | buf_len = strlen (rs->buf); | |
6255 | ||
6256 | /* Further sanity checks, with knowledge of the architecture. */ | |
74ca34ce DJ |
6257 | if (buf_len > 2 * rsa->sizeof_g_packet) |
6258 | error (_("Remote 'g' packet reply is too long: %s"), rs->buf); | |
6259 | ||
6260 | /* Save the size of the packet sent to us by the target. It is used | |
6261 | as a heuristic when determining the max size of packets that the | |
6262 | target can safely receive. */ | |
6263 | if (rsa->actual_register_packet_size == 0) | |
6264 | rsa->actual_register_packet_size = buf_len; | |
6265 | ||
6266 | /* If this is smaller than we guessed the 'g' packet would be, | |
6267 | update our records. A 'g' reply that doesn't include a register's | |
6268 | value implies either that the register is not available, or that | |
6269 | the 'p' packet must be used. */ | |
6270 | if (buf_len < 2 * rsa->sizeof_g_packet) | |
b323314b | 6271 | { |
74ca34ce DJ |
6272 | rsa->sizeof_g_packet = buf_len / 2; |
6273 | ||
4a22f64d | 6274 | for (i = 0; i < gdbarch_num_regs (gdbarch); i++) |
b96ec7ac | 6275 | { |
74ca34ce DJ |
6276 | if (rsa->regs[i].pnum == -1) |
6277 | continue; | |
6278 | ||
6279 | if (rsa->regs[i].offset >= rsa->sizeof_g_packet) | |
6280 | rsa->regs[i].in_g_packet = 0; | |
b96ec7ac | 6281 | else |
74ca34ce | 6282 | rsa->regs[i].in_g_packet = 1; |
b96ec7ac | 6283 | } |
74ca34ce | 6284 | } |
b323314b | 6285 | |
74ca34ce | 6286 | regs = alloca (rsa->sizeof_g_packet); |
c906108c SS |
6287 | |
6288 | /* Unimplemented registers read as all bits zero. */ | |
ea9c271d | 6289 | memset (regs, 0, rsa->sizeof_g_packet); |
c906108c | 6290 | |
c906108c SS |
6291 | /* Reply describes registers byte by byte, each byte encoded as two |
6292 | hex characters. Suck them all up, then supply them to the | |
6293 | register cacheing/storage mechanism. */ | |
6294 | ||
74ca34ce | 6295 | p = rs->buf; |
ea9c271d | 6296 | for (i = 0; i < rsa->sizeof_g_packet; i++) |
c906108c | 6297 | { |
74ca34ce DJ |
6298 | if (p[0] == 0 || p[1] == 0) |
6299 | /* This shouldn't happen - we adjusted sizeof_g_packet above. */ | |
6300 | internal_error (__FILE__, __LINE__, | |
9b20d036 | 6301 | _("unexpected end of 'g' packet reply")); |
74ca34ce | 6302 | |
c906108c | 6303 | if (p[0] == 'x' && p[1] == 'x') |
c5aa993b | 6304 | regs[i] = 0; /* 'x' */ |
c906108c SS |
6305 | else |
6306 | regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); | |
6307 | p += 2; | |
6308 | } | |
6309 | ||
a744cf53 MS |
6310 | for (i = 0; i < gdbarch_num_regs (gdbarch); i++) |
6311 | { | |
6312 | struct packet_reg *r = &rsa->regs[i]; | |
6313 | ||
6314 | if (r->in_g_packet) | |
6315 | { | |
6316 | if (r->offset * 2 >= strlen (rs->buf)) | |
6317 | /* This shouldn't happen - we adjusted in_g_packet above. */ | |
6318 | internal_error (__FILE__, __LINE__, | |
9b20d036 | 6319 | _("unexpected end of 'g' packet reply")); |
a744cf53 MS |
6320 | else if (rs->buf[r->offset * 2] == 'x') |
6321 | { | |
6322 | gdb_assert (r->offset * 2 < strlen (rs->buf)); | |
6323 | /* The register isn't available, mark it as such (at | |
6324 | the same time setting the value to zero). */ | |
6325 | regcache_raw_supply (regcache, r->regnum, NULL); | |
6326 | } | |
6327 | else | |
6328 | regcache_raw_supply (regcache, r->regnum, | |
6329 | regs + r->offset); | |
6330 | } | |
6331 | } | |
c906108c SS |
6332 | } |
6333 | ||
29709017 | 6334 | static void |
56be3814 | 6335 | fetch_registers_using_g (struct regcache *regcache) |
29709017 DJ |
6336 | { |
6337 | send_g_packet (); | |
56be3814 | 6338 | process_g_packet (regcache); |
29709017 DJ |
6339 | } |
6340 | ||
e6e4e701 PA |
6341 | /* Make the remote selected traceframe match GDB's selected |
6342 | traceframe. */ | |
6343 | ||
6344 | static void | |
6345 | set_remote_traceframe (void) | |
6346 | { | |
6347 | int newnum; | |
262e1174 | 6348 | struct remote_state *rs = get_remote_state (); |
e6e4e701 | 6349 | |
262e1174 | 6350 | if (rs->remote_traceframe_number == get_traceframe_number ()) |
e6e4e701 PA |
6351 | return; |
6352 | ||
6353 | /* Avoid recursion, remote_trace_find calls us again. */ | |
262e1174 | 6354 | rs->remote_traceframe_number = get_traceframe_number (); |
e6e4e701 PA |
6355 | |
6356 | newnum = target_trace_find (tfind_number, | |
6357 | get_traceframe_number (), 0, 0, NULL); | |
6358 | ||
6359 | /* Should not happen. If it does, all bets are off. */ | |
6360 | if (newnum != get_traceframe_number ()) | |
6361 | warning (_("could not set remote traceframe")); | |
6362 | } | |
6363 | ||
74ca34ce | 6364 | static void |
28439f5e PA |
6365 | remote_fetch_registers (struct target_ops *ops, |
6366 | struct regcache *regcache, int regnum) | |
74ca34ce | 6367 | { |
74ca34ce DJ |
6368 | struct remote_arch_state *rsa = get_remote_arch_state (); |
6369 | int i; | |
6370 | ||
e6e4e701 | 6371 | set_remote_traceframe (); |
79d7f229 | 6372 | set_general_thread (inferior_ptid); |
74ca34ce DJ |
6373 | |
6374 | if (regnum >= 0) | |
6375 | { | |
6376 | struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum); | |
a744cf53 | 6377 | |
74ca34ce DJ |
6378 | gdb_assert (reg != NULL); |
6379 | ||
6380 | /* If this register might be in the 'g' packet, try that first - | |
6381 | we are likely to read more than one register. If this is the | |
6382 | first 'g' packet, we might be overly optimistic about its | |
6383 | contents, so fall back to 'p'. */ | |
6384 | if (reg->in_g_packet) | |
6385 | { | |
56be3814 | 6386 | fetch_registers_using_g (regcache); |
74ca34ce DJ |
6387 | if (reg->in_g_packet) |
6388 | return; | |
6389 | } | |
6390 | ||
56be3814 | 6391 | if (fetch_register_using_p (regcache, reg)) |
74ca34ce DJ |
6392 | return; |
6393 | ||
6394 | /* This register is not available. */ | |
56be3814 | 6395 | regcache_raw_supply (regcache, reg->regnum, NULL); |
74ca34ce DJ |
6396 | |
6397 | return; | |
6398 | } | |
6399 | ||
56be3814 | 6400 | fetch_registers_using_g (regcache); |
74ca34ce | 6401 | |
4a22f64d | 6402 | for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++) |
74ca34ce | 6403 | if (!rsa->regs[i].in_g_packet) |
56be3814 | 6404 | if (!fetch_register_using_p (regcache, &rsa->regs[i])) |
74ca34ce DJ |
6405 | { |
6406 | /* This register is not available. */ | |
56be3814 | 6407 | regcache_raw_supply (regcache, i, NULL); |
74ca34ce DJ |
6408 | } |
6409 | } | |
6410 | ||
c906108c SS |
6411 | /* Prepare to store registers. Since we may send them all (using a |
6412 | 'G' request), we have to read out the ones we don't want to change | |
6413 | first. */ | |
6414 | ||
c5aa993b | 6415 | static void |
316f2060 | 6416 | remote_prepare_to_store (struct regcache *regcache) |
c906108c | 6417 | { |
ea9c271d | 6418 | struct remote_arch_state *rsa = get_remote_arch_state (); |
cf0e1e0d | 6419 | int i; |
cfd77fa1 | 6420 | gdb_byte buf[MAX_REGISTER_SIZE]; |
cf0e1e0d | 6421 | |
c906108c | 6422 | /* Make sure the entire registers array is valid. */ |
444abaca | 6423 | switch (remote_protocol_packets[PACKET_P].support) |
5a2468f5 JM |
6424 | { |
6425 | case PACKET_DISABLE: | |
6426 | case PACKET_SUPPORT_UNKNOWN: | |
cf0e1e0d | 6427 | /* Make sure all the necessary registers are cached. */ |
4a22f64d | 6428 | for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++) |
ea9c271d | 6429 | if (rsa->regs[i].in_g_packet) |
316f2060 | 6430 | regcache_raw_read (regcache, rsa->regs[i].regnum, buf); |
5a2468f5 JM |
6431 | break; |
6432 | case PACKET_ENABLE: | |
6433 | break; | |
6434 | } | |
6435 | } | |
6436 | ||
ad10f812 | 6437 | /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF |
23860348 | 6438 | packet was not recognized. */ |
5a2468f5 JM |
6439 | |
6440 | static int | |
1f4437a4 MS |
6441 | store_register_using_P (const struct regcache *regcache, |
6442 | struct packet_reg *reg) | |
5a2468f5 | 6443 | { |
4a22f64d | 6444 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
d01949b6 | 6445 | struct remote_state *rs = get_remote_state (); |
5a2468f5 | 6446 | /* Try storing a single register. */ |
6d820c5c | 6447 | char *buf = rs->buf; |
cfd77fa1 | 6448 | gdb_byte regp[MAX_REGISTER_SIZE]; |
5a2468f5 | 6449 | char *p; |
5a2468f5 | 6450 | |
74ca34ce DJ |
6451 | if (remote_protocol_packets[PACKET_P].support == PACKET_DISABLE) |
6452 | return 0; | |
6453 | ||
6454 | if (reg->pnum == -1) | |
6455 | return 0; | |
6456 | ||
ea9c271d | 6457 | xsnprintf (buf, get_remote_packet_size (), "P%s=", phex_nz (reg->pnum, 0)); |
5a2468f5 | 6458 | p = buf + strlen (buf); |
56be3814 | 6459 | regcache_raw_collect (regcache, reg->regnum, regp); |
4a22f64d | 6460 | bin2hex (regp, p, register_size (gdbarch, reg->regnum)); |
1f4437a4 MS |
6461 | putpkt (rs->buf); |
6462 | getpkt (&rs->buf, &rs->buf_size, 0); | |
5a2468f5 | 6463 | |
74ca34ce DJ |
6464 | switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_P])) |
6465 | { | |
6466 | case PACKET_OK: | |
6467 | return 1; | |
6468 | case PACKET_ERROR: | |
27a9c0bf MS |
6469 | error (_("Could not write register \"%s\"; remote failure reply '%s'"), |
6470 | gdbarch_register_name (gdbarch, reg->regnum), rs->buf); | |
74ca34ce DJ |
6471 | case PACKET_UNKNOWN: |
6472 | return 0; | |
6473 | default: | |
6474 | internal_error (__FILE__, __LINE__, _("Bad result from packet_ok")); | |
6475 | } | |
c906108c SS |
6476 | } |
6477 | ||
23860348 MS |
6478 | /* Store register REGNUM, or all registers if REGNUM == -1, from the |
6479 | contents of the register cache buffer. FIXME: ignores errors. */ | |
c906108c SS |
6480 | |
6481 | static void | |
56be3814 | 6482 | store_registers_using_G (const struct regcache *regcache) |
c906108c | 6483 | { |
d01949b6 | 6484 | struct remote_state *rs = get_remote_state (); |
ea9c271d | 6485 | struct remote_arch_state *rsa = get_remote_arch_state (); |
cfd77fa1 | 6486 | gdb_byte *regs; |
c906108c SS |
6487 | char *p; |
6488 | ||
193cb69f AC |
6489 | /* Extract all the registers in the regcache copying them into a |
6490 | local buffer. */ | |
6491 | { | |
b323314b | 6492 | int i; |
a744cf53 | 6493 | |
ea9c271d DJ |
6494 | regs = alloca (rsa->sizeof_g_packet); |
6495 | memset (regs, 0, rsa->sizeof_g_packet); | |
4a22f64d | 6496 | for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++) |
193cb69f | 6497 | { |
ea9c271d | 6498 | struct packet_reg *r = &rsa->regs[i]; |
a744cf53 | 6499 | |
b323314b | 6500 | if (r->in_g_packet) |
56be3814 | 6501 | regcache_raw_collect (regcache, r->regnum, regs + r->offset); |
193cb69f AC |
6502 | } |
6503 | } | |
c906108c SS |
6504 | |
6505 | /* Command describes registers byte by byte, | |
6506 | each byte encoded as two hex characters. */ | |
6d820c5c | 6507 | p = rs->buf; |
193cb69f | 6508 | *p++ = 'G'; |
74ca34ce DJ |
6509 | /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets |
6510 | updated. */ | |
6511 | bin2hex (regs, p, rsa->sizeof_g_packet); | |
1f4437a4 MS |
6512 | putpkt (rs->buf); |
6513 | getpkt (&rs->buf, &rs->buf_size, 0); | |
6514 | if (packet_check_result (rs->buf) == PACKET_ERROR) | |
27a9c0bf MS |
6515 | error (_("Could not write registers; remote failure reply '%s'"), |
6516 | rs->buf); | |
c906108c | 6517 | } |
74ca34ce DJ |
6518 | |
6519 | /* Store register REGNUM, or all registers if REGNUM == -1, from the contents | |
6520 | of the register cache buffer. FIXME: ignores errors. */ | |
6521 | ||
6522 | static void | |
28439f5e PA |
6523 | remote_store_registers (struct target_ops *ops, |
6524 | struct regcache *regcache, int regnum) | |
74ca34ce | 6525 | { |
74ca34ce DJ |
6526 | struct remote_arch_state *rsa = get_remote_arch_state (); |
6527 | int i; | |
6528 | ||
e6e4e701 | 6529 | set_remote_traceframe (); |
79d7f229 | 6530 | set_general_thread (inferior_ptid); |
74ca34ce DJ |
6531 | |
6532 | if (regnum >= 0) | |
6533 | { | |
6534 | struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum); | |
a744cf53 | 6535 | |
74ca34ce DJ |
6536 | gdb_assert (reg != NULL); |
6537 | ||
6538 | /* Always prefer to store registers using the 'P' packet if | |
6539 | possible; we often change only a small number of registers. | |
6540 | Sometimes we change a larger number; we'd need help from a | |
6541 | higher layer to know to use 'G'. */ | |
56be3814 | 6542 | if (store_register_using_P (regcache, reg)) |
74ca34ce DJ |
6543 | return; |
6544 | ||
6545 | /* For now, don't complain if we have no way to write the | |
6546 | register. GDB loses track of unavailable registers too | |
6547 | easily. Some day, this may be an error. We don't have | |
0df8b418 | 6548 | any way to read the register, either... */ |
74ca34ce DJ |
6549 | if (!reg->in_g_packet) |
6550 | return; | |
6551 | ||
56be3814 | 6552 | store_registers_using_G (regcache); |
74ca34ce DJ |
6553 | return; |
6554 | } | |
6555 | ||
56be3814 | 6556 | store_registers_using_G (regcache); |
74ca34ce | 6557 | |
4a22f64d | 6558 | for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++) |
74ca34ce | 6559 | if (!rsa->regs[i].in_g_packet) |
56be3814 | 6560 | if (!store_register_using_P (regcache, &rsa->regs[i])) |
74ca34ce DJ |
6561 | /* See above for why we do not issue an error here. */ |
6562 | continue; | |
6563 | } | |
c906108c SS |
6564 | \f |
6565 | ||
6566 | /* Return the number of hex digits in num. */ | |
6567 | ||
6568 | static int | |
fba45db2 | 6569 | hexnumlen (ULONGEST num) |
c906108c SS |
6570 | { |
6571 | int i; | |
6572 | ||
6573 | for (i = 0; num != 0; i++) | |
6574 | num >>= 4; | |
6575 | ||
6576 | return max (i, 1); | |
6577 | } | |
6578 | ||
2df3850c | 6579 | /* Set BUF to the minimum number of hex digits representing NUM. */ |
c906108c SS |
6580 | |
6581 | static int | |
fba45db2 | 6582 | hexnumstr (char *buf, ULONGEST num) |
c906108c | 6583 | { |
c906108c | 6584 | int len = hexnumlen (num); |
a744cf53 | 6585 | |
2df3850c JM |
6586 | return hexnumnstr (buf, num, len); |
6587 | } | |
6588 | ||
c906108c | 6589 | |
2df3850c | 6590 | /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */ |
c906108c | 6591 | |
2df3850c | 6592 | static int |
fba45db2 | 6593 | hexnumnstr (char *buf, ULONGEST num, int width) |
2df3850c JM |
6594 | { |
6595 | int i; | |
6596 | ||
6597 | buf[width] = '\0'; | |
6598 | ||
6599 | for (i = width - 1; i >= 0; i--) | |
c906108c | 6600 | { |
c5aa993b | 6601 | buf[i] = "0123456789abcdef"[(num & 0xf)]; |
c906108c SS |
6602 | num >>= 4; |
6603 | } | |
6604 | ||
2df3850c | 6605 | return width; |
c906108c SS |
6606 | } |
6607 | ||
23860348 | 6608 | /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */ |
c906108c SS |
6609 | |
6610 | static CORE_ADDR | |
fba45db2 | 6611 | remote_address_masked (CORE_ADDR addr) |
c906108c | 6612 | { |
883b9c6c | 6613 | unsigned int address_size = remote_address_size; |
a744cf53 | 6614 | |
911c95a5 UW |
6615 | /* If "remoteaddresssize" was not set, default to target address size. */ |
6616 | if (!address_size) | |
f5656ead | 6617 | address_size = gdbarch_addr_bit (target_gdbarch ()); |
911c95a5 UW |
6618 | |
6619 | if (address_size > 0 | |
6620 | && address_size < (sizeof (ULONGEST) * 8)) | |
c906108c SS |
6621 | { |
6622 | /* Only create a mask when that mask can safely be constructed | |
23860348 | 6623 | in a ULONGEST variable. */ |
c906108c | 6624 | ULONGEST mask = 1; |
a744cf53 | 6625 | |
911c95a5 | 6626 | mask = (mask << address_size) - 1; |
c906108c SS |
6627 | addr &= mask; |
6628 | } | |
6629 | return addr; | |
6630 | } | |
6631 | ||
a31ea83d DJ |
6632 | /* Convert BUFFER, binary data at least LEN bytes long, into escaped |
6633 | binary data in OUT_BUF. Set *OUT_LEN to the length of the data | |
6634 | encoded in OUT_BUF, and return the number of bytes in OUT_BUF | |
6635 | (which may be more than *OUT_LEN due to escape characters). The | |
6636 | total number of bytes in the output buffer will be at most | |
6637 | OUT_MAXLEN. */ | |
6638 | ||
6639 | static int | |
6640 | remote_escape_output (const gdb_byte *buffer, int len, | |
6641 | gdb_byte *out_buf, int *out_len, | |
6642 | int out_maxlen) | |
6643 | { | |
6644 | int input_index, output_index; | |
6645 | ||
6646 | output_index = 0; | |
6647 | for (input_index = 0; input_index < len; input_index++) | |
6648 | { | |
6649 | gdb_byte b = buffer[input_index]; | |
6650 | ||
6651 | if (b == '$' || b == '#' || b == '}') | |
6652 | { | |
6653 | /* These must be escaped. */ | |
6654 | if (output_index + 2 > out_maxlen) | |
6655 | break; | |
6656 | out_buf[output_index++] = '}'; | |
6657 | out_buf[output_index++] = b ^ 0x20; | |
6658 | } | |
6659 | else | |
6660 | { | |
6661 | if (output_index + 1 > out_maxlen) | |
6662 | break; | |
6663 | out_buf[output_index++] = b; | |
6664 | } | |
6665 | } | |
6666 | ||
6667 | *out_len = input_index; | |
6668 | return output_index; | |
6669 | } | |
6670 | ||
0876f84a DJ |
6671 | /* Convert BUFFER, escaped data LEN bytes long, into binary data |
6672 | in OUT_BUF. Return the number of bytes written to OUT_BUF. | |
6673 | Raise an error if the total number of bytes exceeds OUT_MAXLEN. | |
6674 | ||
6675 | This function reverses remote_escape_output. It allows more | |
6676 | escaped characters than that function does, in particular because | |
6677 | '*' must be escaped to avoid the run-length encoding processing | |
6678 | in reading packets. */ | |
6679 | ||
6680 | static int | |
6681 | remote_unescape_input (const gdb_byte *buffer, int len, | |
6682 | gdb_byte *out_buf, int out_maxlen) | |
6683 | { | |
6684 | int input_index, output_index; | |
6685 | int escaped; | |
6686 | ||
6687 | output_index = 0; | |
6688 | escaped = 0; | |
6689 | for (input_index = 0; input_index < len; input_index++) | |
6690 | { | |
6691 | gdb_byte b = buffer[input_index]; | |
6692 | ||
6693 | if (output_index + 1 > out_maxlen) | |
6694 | { | |
6695 | warning (_("Received too much data from remote target;" | |
6696 | " ignoring overflow.")); | |
6697 | return output_index; | |
6698 | } | |
6699 | ||
6700 | if (escaped) | |
6701 | { | |
6702 | out_buf[output_index++] = b ^ 0x20; | |
6703 | escaped = 0; | |
6704 | } | |
6705 | else if (b == '}') | |
6706 | escaped = 1; | |
6707 | else | |
6708 | out_buf[output_index++] = b; | |
6709 | } | |
6710 | ||
6711 | if (escaped) | |
6712 | error (_("Unmatched escape character in target response.")); | |
6713 | ||
6714 | return output_index; | |
6715 | } | |
6716 | ||
c906108c SS |
6717 | /* Determine whether the remote target supports binary downloading. |
6718 | This is accomplished by sending a no-op memory write of zero length | |
6719 | to the target at the specified address. It does not suffice to send | |
23860348 MS |
6720 | the whole packet, since many stubs strip the eighth bit and |
6721 | subsequently compute a wrong checksum, which causes real havoc with | |
6722 | remote_write_bytes. | |
7a292a7a | 6723 | |
96baa820 | 6724 | NOTE: This can still lose if the serial line is not eight-bit |
0df8b418 | 6725 | clean. In cases like this, the user should clear "remote |
23860348 | 6726 | X-packet". */ |
96baa820 | 6727 | |
c906108c | 6728 | static void |
fba45db2 | 6729 | check_binary_download (CORE_ADDR addr) |
c906108c | 6730 | { |
d01949b6 | 6731 | struct remote_state *rs = get_remote_state (); |
24b06219 | 6732 | |
444abaca | 6733 | switch (remote_protocol_packets[PACKET_X].support) |
c906108c | 6734 | { |
96baa820 JM |
6735 | case PACKET_DISABLE: |
6736 | break; | |
6737 | case PACKET_ENABLE: | |
6738 | break; | |
6739 | case PACKET_SUPPORT_UNKNOWN: | |
6740 | { | |
96baa820 | 6741 | char *p; |
802188a7 | 6742 | |
2e9f7625 | 6743 | p = rs->buf; |
96baa820 JM |
6744 | *p++ = 'X'; |
6745 | p += hexnumstr (p, (ULONGEST) addr); | |
6746 | *p++ = ','; | |
6747 | p += hexnumstr (p, (ULONGEST) 0); | |
6748 | *p++ = ':'; | |
6749 | *p = '\0'; | |
802188a7 | 6750 | |
2e9f7625 | 6751 | putpkt_binary (rs->buf, (int) (p - rs->buf)); |
6d820c5c | 6752 | getpkt (&rs->buf, &rs->buf_size, 0); |
c906108c | 6753 | |
2e9f7625 | 6754 | if (rs->buf[0] == '\0') |
96baa820 JM |
6755 | { |
6756 | if (remote_debug) | |
6757 | fprintf_unfiltered (gdb_stdlog, | |
3e43a32a MS |
6758 | "binary downloading NOT " |
6759 | "supported by target\n"); | |
444abaca | 6760 | remote_protocol_packets[PACKET_X].support = PACKET_DISABLE; |
96baa820 JM |
6761 | } |
6762 | else | |
6763 | { | |
6764 | if (remote_debug) | |
6765 | fprintf_unfiltered (gdb_stdlog, | |
64b9b334 | 6766 | "binary downloading supported by target\n"); |
444abaca | 6767 | remote_protocol_packets[PACKET_X].support = PACKET_ENABLE; |
96baa820 JM |
6768 | } |
6769 | break; | |
6770 | } | |
c906108c SS |
6771 | } |
6772 | } | |
6773 | ||
6774 | /* Write memory data directly to the remote machine. | |
6775 | This does not inform the data cache; the data cache uses this. | |
a76d924d | 6776 | HEADER is the starting part of the packet. |
c906108c SS |
6777 | MEMADDR is the address in the remote memory space. |
6778 | MYADDR is the address of the buffer in our space. | |
6779 | LEN is the number of bytes. | |
a76d924d DJ |
6780 | PACKET_FORMAT should be either 'X' or 'M', and indicates if we |
6781 | should send data as binary ('X'), or hex-encoded ('M'). | |
6782 | ||
6783 | The function creates packet of the form | |
6784 | <HEADER><ADDRESS>,<LENGTH>:<DATA> | |
6785 | ||
6786 | where encoding of <DATA> is termined by PACKET_FORMAT. | |
6787 | ||
6788 | If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma | |
6789 | are omitted. | |
6790 | ||
00d84524 PA |
6791 | Returns the number of bytes transferred, or a negative value (an |
6792 | 'enum target_xfer_error' value) for error. Only transfer a single | |
6793 | packet. */ | |
c906108c | 6794 | |
00d84524 | 6795 | static LONGEST |
a76d924d | 6796 | remote_write_bytes_aux (const char *header, CORE_ADDR memaddr, |
45aa4659 | 6797 | const gdb_byte *myaddr, ssize_t len, |
a76d924d | 6798 | char packet_format, int use_length) |
c906108c | 6799 | { |
6d820c5c | 6800 | struct remote_state *rs = get_remote_state (); |
cfd77fa1 | 6801 | char *p; |
a76d924d DJ |
6802 | char *plen = NULL; |
6803 | int plenlen = 0; | |
917317f4 JM |
6804 | int todo; |
6805 | int nr_bytes; | |
a257b5bb | 6806 | int payload_size; |
6765f3e5 | 6807 | int payload_length; |
a76d924d DJ |
6808 | int header_length; |
6809 | ||
6810 | if (packet_format != 'X' && packet_format != 'M') | |
6811 | internal_error (__FILE__, __LINE__, | |
9b20d036 | 6812 | _("remote_write_bytes_aux: bad packet format")); |
c906108c | 6813 | |
b2182ed2 DJ |
6814 | if (len <= 0) |
6815 | return 0; | |
6816 | ||
3de11b2e | 6817 | payload_size = get_memory_write_packet_size (); |
2bc416ba | 6818 | |
6d820c5c DJ |
6819 | /* The packet buffer will be large enough for the payload; |
6820 | get_memory_packet_size ensures this. */ | |
a76d924d | 6821 | rs->buf[0] = '\0'; |
c906108c | 6822 | |
a257b5bb | 6823 | /* Compute the size of the actual payload by subtracting out the |
0df8b418 MS |
6824 | packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */ |
6825 | ||
a76d924d DJ |
6826 | payload_size -= strlen ("$,:#NN"); |
6827 | if (!use_length) | |
0df8b418 | 6828 | /* The comma won't be used. */ |
a76d924d DJ |
6829 | payload_size += 1; |
6830 | header_length = strlen (header); | |
6831 | payload_size -= header_length; | |
3de11b2e | 6832 | payload_size -= hexnumlen (memaddr); |
c906108c | 6833 | |
a76d924d | 6834 | /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */ |
917317f4 | 6835 | |
a76d924d DJ |
6836 | strcat (rs->buf, header); |
6837 | p = rs->buf + strlen (header); | |
6838 | ||
6839 | /* Compute a best guess of the number of bytes actually transfered. */ | |
6840 | if (packet_format == 'X') | |
c906108c | 6841 | { |
23860348 | 6842 | /* Best guess at number of bytes that will fit. */ |
a257b5bb | 6843 | todo = min (len, payload_size); |
a76d924d DJ |
6844 | if (use_length) |
6845 | payload_size -= hexnumlen (todo); | |
3de11b2e | 6846 | todo = min (todo, payload_size); |
a76d924d DJ |
6847 | } |
6848 | else | |
6849 | { | |
23860348 | 6850 | /* Num bytes that will fit. */ |
a257b5bb | 6851 | todo = min (len, payload_size / 2); |
a76d924d DJ |
6852 | if (use_length) |
6853 | payload_size -= hexnumlen (todo); | |
3de11b2e | 6854 | todo = min (todo, payload_size / 2); |
917317f4 | 6855 | } |
a76d924d | 6856 | |
3de11b2e NS |
6857 | if (todo <= 0) |
6858 | internal_error (__FILE__, __LINE__, | |
405f8e94 | 6859 | _("minimum packet size too small to write data")); |
802188a7 | 6860 | |
6765f3e5 DJ |
6861 | /* If we already need another packet, then try to align the end |
6862 | of this packet to a useful boundary. */ | |
6863 | if (todo > 2 * REMOTE_ALIGN_WRITES && todo < len) | |
6864 | todo = ((memaddr + todo) & ~(REMOTE_ALIGN_WRITES - 1)) - memaddr; | |
6865 | ||
a257b5bb | 6866 | /* Append "<memaddr>". */ |
917317f4 JM |
6867 | memaddr = remote_address_masked (memaddr); |
6868 | p += hexnumstr (p, (ULONGEST) memaddr); | |
a257b5bb | 6869 | |
a76d924d DJ |
6870 | if (use_length) |
6871 | { | |
6872 | /* Append ",". */ | |
6873 | *p++ = ','; | |
802188a7 | 6874 | |
a76d924d DJ |
6875 | /* Append <len>. Retain the location/size of <len>. It may need to |
6876 | be adjusted once the packet body has been created. */ | |
6877 | plen = p; | |
6878 | plenlen = hexnumstr (p, (ULONGEST) todo); | |
6879 | p += plenlen; | |
6880 | } | |
a257b5bb AC |
6881 | |
6882 | /* Append ":". */ | |
917317f4 JM |
6883 | *p++ = ':'; |
6884 | *p = '\0'; | |
802188a7 | 6885 | |
a257b5bb | 6886 | /* Append the packet body. */ |
a76d924d | 6887 | if (packet_format == 'X') |
917317f4 | 6888 | { |
917317f4 JM |
6889 | /* Binary mode. Send target system values byte by byte, in |
6890 | increasing byte addresses. Only escape certain critical | |
6891 | characters. */ | |
bc20a4af PA |
6892 | payload_length = remote_escape_output (myaddr, todo, (gdb_byte *) p, |
6893 | &nr_bytes, payload_size); | |
6765f3e5 DJ |
6894 | |
6895 | /* If not all TODO bytes fit, then we'll need another packet. Make | |
9b7194bc DJ |
6896 | a second try to keep the end of the packet aligned. Don't do |
6897 | this if the packet is tiny. */ | |
6898 | if (nr_bytes < todo && nr_bytes > 2 * REMOTE_ALIGN_WRITES) | |
6765f3e5 DJ |
6899 | { |
6900 | int new_nr_bytes; | |
6901 | ||
6902 | new_nr_bytes = (((memaddr + nr_bytes) & ~(REMOTE_ALIGN_WRITES - 1)) | |
6903 | - memaddr); | |
6904 | if (new_nr_bytes != nr_bytes) | |
6905 | payload_length = remote_escape_output (myaddr, new_nr_bytes, | |
bc20a4af | 6906 | (gdb_byte *) p, &nr_bytes, |
6765f3e5 DJ |
6907 | payload_size); |
6908 | } | |
6909 | ||
6910 | p += payload_length; | |
a76d924d | 6911 | if (use_length && nr_bytes < todo) |
c906108c | 6912 | { |
802188a7 | 6913 | /* Escape chars have filled up the buffer prematurely, |
917317f4 JM |
6914 | and we have actually sent fewer bytes than planned. |
6915 | Fix-up the length field of the packet. Use the same | |
6916 | number of characters as before. */ | |
917317f4 JM |
6917 | plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen); |
6918 | *plen = ':'; /* overwrite \0 from hexnumnstr() */ | |
c906108c | 6919 | } |
a76d924d DJ |
6920 | } |
6921 | else | |
6922 | { | |
917317f4 JM |
6923 | /* Normal mode: Send target system values byte by byte, in |
6924 | increasing byte addresses. Each byte is encoded as a two hex | |
6925 | value. */ | |
2644f393 | 6926 | nr_bytes = bin2hex (myaddr, p, todo); |
aa6c0017 | 6927 | p += 2 * nr_bytes; |
c906108c | 6928 | } |
802188a7 | 6929 | |
2e9f7625 | 6930 | putpkt_binary (rs->buf, (int) (p - rs->buf)); |
6d820c5c | 6931 | getpkt (&rs->buf, &rs->buf_size, 0); |
802188a7 | 6932 | |
2e9f7625 | 6933 | if (rs->buf[0] == 'E') |
00d84524 | 6934 | return TARGET_XFER_E_IO; |
802188a7 | 6935 | |
23860348 MS |
6936 | /* Return NR_BYTES, not TODO, in case escape chars caused us to send |
6937 | fewer bytes than we'd planned. */ | |
917317f4 | 6938 | return nr_bytes; |
c906108c SS |
6939 | } |
6940 | ||
a76d924d DJ |
6941 | /* Write memory data directly to the remote machine. |
6942 | This does not inform the data cache; the data cache uses this. | |
6943 | MEMADDR is the address in the remote memory space. | |
6944 | MYADDR is the address of the buffer in our space. | |
6945 | LEN is the number of bytes. | |
6946 | ||
00d84524 PA |
6947 | Returns number of bytes transferred, or a negative value (an 'enum |
6948 | target_xfer_error' value) for error. Only transfer a single | |
6949 | packet. */ | |
a76d924d | 6950 | |
00d84524 | 6951 | static LONGEST |
45aa4659 | 6952 | remote_write_bytes (CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len) |
a76d924d DJ |
6953 | { |
6954 | char *packet_format = 0; | |
6955 | ||
6956 | /* Check whether the target supports binary download. */ | |
6957 | check_binary_download (memaddr); | |
6958 | ||
6959 | switch (remote_protocol_packets[PACKET_X].support) | |
6960 | { | |
6961 | case PACKET_ENABLE: | |
6962 | packet_format = "X"; | |
6963 | break; | |
6964 | case PACKET_DISABLE: | |
6965 | packet_format = "M"; | |
6966 | break; | |
6967 | case PACKET_SUPPORT_UNKNOWN: | |
6968 | internal_error (__FILE__, __LINE__, | |
6969 | _("remote_write_bytes: bad internal state")); | |
6970 | default: | |
6971 | internal_error (__FILE__, __LINE__, _("bad switch")); | |
6972 | } | |
6973 | ||
6974 | return remote_write_bytes_aux (packet_format, | |
6975 | memaddr, myaddr, len, packet_format[0], 1); | |
6976 | } | |
6977 | ||
c906108c SS |
6978 | /* Read memory data directly from the remote machine. |
6979 | This does not use the data cache; the data cache uses this. | |
6980 | MEMADDR is the address in the remote memory space. | |
6981 | MYADDR is the address of the buffer in our space. | |
6982 | LEN is the number of bytes. | |
6983 | ||
00d84524 PA |
6984 | Returns number of bytes transferred, or a negative value (an 'enum |
6985 | target_xfer_error' value) for error. */ | |
c906108c | 6986 | |
00d84524 | 6987 | static LONGEST |
cfd77fa1 | 6988 | remote_read_bytes (CORE_ADDR memaddr, gdb_byte *myaddr, int len) |
c906108c | 6989 | { |
6d820c5c | 6990 | struct remote_state *rs = get_remote_state (); |
23860348 | 6991 | int max_buf_size; /* Max size of packet output buffer. */ |
f7605bc2 PA |
6992 | char *p; |
6993 | int todo; | |
6994 | int i; | |
c906108c | 6995 | |
b2182ed2 DJ |
6996 | if (len <= 0) |
6997 | return 0; | |
6998 | ||
11cf8741 | 6999 | max_buf_size = get_memory_read_packet_size (); |
6d820c5c DJ |
7000 | /* The packet buffer will be large enough for the payload; |
7001 | get_memory_packet_size ensures this. */ | |
c906108c | 7002 | |
f7605bc2 PA |
7003 | /* Number if bytes that will fit. */ |
7004 | todo = min (len, max_buf_size / 2); | |
c906108c | 7005 | |
f7605bc2 PA |
7006 | /* Construct "m"<memaddr>","<len>". */ |
7007 | memaddr = remote_address_masked (memaddr); | |
7008 | p = rs->buf; | |
7009 | *p++ = 'm'; | |
7010 | p += hexnumstr (p, (ULONGEST) memaddr); | |
7011 | *p++ = ','; | |
7012 | p += hexnumstr (p, (ULONGEST) todo); | |
7013 | *p = '\0'; | |
7014 | putpkt (rs->buf); | |
7015 | getpkt (&rs->buf, &rs->buf_size, 0); | |
7016 | if (rs->buf[0] == 'E' | |
7017 | && isxdigit (rs->buf[1]) && isxdigit (rs->buf[2]) | |
7018 | && rs->buf[3] == '\0') | |
00d84524 | 7019 | return TARGET_XFER_E_IO; |
f7605bc2 PA |
7020 | /* Reply describes memory byte by byte, each byte encoded as two hex |
7021 | characters. */ | |
7022 | p = rs->buf; | |
7023 | i = hex2bin (p, myaddr, todo); | |
7024 | /* Return what we have. Let higher layers handle partial reads. */ | |
7025 | return i; | |
c906108c | 7026 | } |
74531fed | 7027 | |
c906108c | 7028 | \f |
c906108c | 7029 | |
a76d924d DJ |
7030 | /* Sends a packet with content determined by the printf format string |
7031 | FORMAT and the remaining arguments, then gets the reply. Returns | |
7032 | whether the packet was a success, a failure, or unknown. */ | |
7033 | ||
2c0b251b | 7034 | static enum packet_result |
a76d924d DJ |
7035 | remote_send_printf (const char *format, ...) |
7036 | { | |
7037 | struct remote_state *rs = get_remote_state (); | |
7038 | int max_size = get_remote_packet_size (); | |
a76d924d | 7039 | va_list ap; |
a744cf53 | 7040 | |
a76d924d DJ |
7041 | va_start (ap, format); |
7042 | ||
7043 | rs->buf[0] = '\0'; | |
7044 | if (vsnprintf (rs->buf, max_size, format, ap) >= max_size) | |
9b20d036 | 7045 | internal_error (__FILE__, __LINE__, _("Too long remote packet.")); |
a76d924d DJ |
7046 | |
7047 | if (putpkt (rs->buf) < 0) | |
7048 | error (_("Communication problem with target.")); | |
7049 | ||
7050 | rs->buf[0] = '\0'; | |
7051 | getpkt (&rs->buf, &rs->buf_size, 0); | |
7052 | ||
7053 | return packet_check_result (rs->buf); | |
7054 | } | |
7055 | ||
7056 | static void | |
7057 | restore_remote_timeout (void *p) | |
7058 | { | |
7059 | int value = *(int *)p; | |
a744cf53 | 7060 | |
a76d924d DJ |
7061 | remote_timeout = value; |
7062 | } | |
7063 | ||
7064 | /* Flash writing can take quite some time. We'll set | |
7065 | effectively infinite timeout for flash operations. | |
7066 | In future, we'll need to decide on a better approach. */ | |
7067 | static const int remote_flash_timeout = 1000; | |
7068 | ||
7069 | static void | |
7070 | remote_flash_erase (struct target_ops *ops, | |
7071 | ULONGEST address, LONGEST length) | |
7072 | { | |
f5656ead | 7073 | int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8; |
a76d924d DJ |
7074 | int saved_remote_timeout = remote_timeout; |
7075 | enum packet_result ret; | |
a76d924d DJ |
7076 | struct cleanup *back_to = make_cleanup (restore_remote_timeout, |
7077 | &saved_remote_timeout); | |
a744cf53 | 7078 | |
a76d924d DJ |
7079 | remote_timeout = remote_flash_timeout; |
7080 | ||
7081 | ret = remote_send_printf ("vFlashErase:%s,%s", | |
5af949e3 | 7082 | phex (address, addr_size), |
a76d924d DJ |
7083 | phex (length, 4)); |
7084 | switch (ret) | |
7085 | { | |
7086 | case PACKET_UNKNOWN: | |
7087 | error (_("Remote target does not support flash erase")); | |
7088 | case PACKET_ERROR: | |
7089 | error (_("Error erasing flash with vFlashErase packet")); | |
7090 | default: | |
7091 | break; | |
7092 | } | |
7093 | ||
7094 | do_cleanups (back_to); | |
7095 | } | |
7096 | ||
7097 | static LONGEST | |
7098 | remote_flash_write (struct target_ops *ops, | |
7099 | ULONGEST address, LONGEST length, | |
7100 | const gdb_byte *data) | |
7101 | { | |
7102 | int saved_remote_timeout = remote_timeout; | |
00d84524 | 7103 | LONGEST ret; |
a76d924d DJ |
7104 | struct cleanup *back_to = make_cleanup (restore_remote_timeout, |
7105 | &saved_remote_timeout); | |
7106 | ||
7107 | remote_timeout = remote_flash_timeout; | |
7108 | ret = remote_write_bytes_aux ("vFlashWrite:", address, data, length, 'X', 0); | |
7109 | do_cleanups (back_to); | |
7110 | ||
7111 | return ret; | |
7112 | } | |
7113 | ||
7114 | static void | |
7115 | remote_flash_done (struct target_ops *ops) | |
7116 | { | |
7117 | int saved_remote_timeout = remote_timeout; | |
7118 | int ret; | |
7119 | struct cleanup *back_to = make_cleanup (restore_remote_timeout, | |
7120 | &saved_remote_timeout); | |
7121 | ||
7122 | remote_timeout = remote_flash_timeout; | |
7123 | ret = remote_send_printf ("vFlashDone"); | |
7124 | do_cleanups (back_to); | |
7125 | ||
7126 | switch (ret) | |
7127 | { | |
7128 | case PACKET_UNKNOWN: | |
7129 | error (_("Remote target does not support vFlashDone")); | |
7130 | case PACKET_ERROR: | |
7131 | error (_("Error finishing flash operation")); | |
7132 | default: | |
7133 | break; | |
7134 | } | |
7135 | } | |
7136 | ||
c906108c | 7137 | static void |
fba45db2 | 7138 | remote_files_info (struct target_ops *ignore) |
c906108c SS |
7139 | { |
7140 | puts_filtered ("Debugging a target over a serial line.\n"); | |
7141 | } | |
7142 | \f | |
7143 | /* Stuff for dealing with the packets which are part of this protocol. | |
7144 | See comment at top of file for details. */ | |
7145 | ||
1927e618 PA |
7146 | /* Close/unpush the remote target, and throw a TARGET_CLOSE_ERROR |
7147 | error to higher layers. Called when a serial error is detected. | |
7148 | The exception message is STRING, followed by a colon and a blank, | |
d6cb50a2 JK |
7149 | the system error message for errno at function entry and final dot |
7150 | for output compatibility with throw_perror_with_name. */ | |
1927e618 PA |
7151 | |
7152 | static void | |
7153 | unpush_and_perror (const char *string) | |
7154 | { | |
d6cb50a2 | 7155 | int saved_errno = errno; |
1927e618 PA |
7156 | |
7157 | remote_unpush_target (); | |
d6cb50a2 JK |
7158 | throw_error (TARGET_CLOSE_ERROR, "%s: %s.", string, |
7159 | safe_strerror (saved_errno)); | |
1927e618 PA |
7160 | } |
7161 | ||
0876f84a | 7162 | /* Read a single character from the remote end. */ |
c906108c SS |
7163 | |
7164 | static int | |
fba45db2 | 7165 | readchar (int timeout) |
c906108c SS |
7166 | { |
7167 | int ch; | |
5d93a237 | 7168 | struct remote_state *rs = get_remote_state (); |
c906108c | 7169 | |
5d93a237 | 7170 | ch = serial_readchar (rs->remote_desc, timeout); |
c906108c | 7171 | |
2acceee2 | 7172 | if (ch >= 0) |
0876f84a | 7173 | return ch; |
2acceee2 JM |
7174 | |
7175 | switch ((enum serial_rc) ch) | |
c906108c SS |
7176 | { |
7177 | case SERIAL_EOF: | |
78a095c3 | 7178 | remote_unpush_target (); |
598d3636 | 7179 | throw_error (TARGET_CLOSE_ERROR, _("Remote connection closed")); |
2acceee2 | 7180 | /* no return */ |
c906108c | 7181 | case SERIAL_ERROR: |
1927e618 PA |
7182 | unpush_and_perror (_("Remote communication error. " |
7183 | "Target disconnected.")); | |
2acceee2 | 7184 | /* no return */ |
c906108c | 7185 | case SERIAL_TIMEOUT: |
2acceee2 | 7186 | break; |
c906108c | 7187 | } |
2acceee2 | 7188 | return ch; |
c906108c SS |
7189 | } |
7190 | ||
c33e31fd PA |
7191 | /* Wrapper for serial_write that closes the target and throws if |
7192 | writing fails. */ | |
7193 | ||
7194 | static void | |
7195 | remote_serial_write (const char *str, int len) | |
7196 | { | |
5d93a237 TT |
7197 | struct remote_state *rs = get_remote_state (); |
7198 | ||
7199 | if (serial_write (rs->remote_desc, str, len)) | |
c33e31fd | 7200 | { |
1927e618 PA |
7201 | unpush_and_perror (_("Remote communication error. " |
7202 | "Target disconnected.")); | |
c33e31fd PA |
7203 | } |
7204 | } | |
7205 | ||
6d820c5c DJ |
7206 | /* Send the command in *BUF to the remote machine, and read the reply |
7207 | into *BUF. Report an error if we get an error reply. Resize | |
7208 | *BUF using xrealloc if necessary to hold the result, and update | |
7209 | *SIZEOF_BUF. */ | |
c906108c SS |
7210 | |
7211 | static void | |
6d820c5c DJ |
7212 | remote_send (char **buf, |
7213 | long *sizeof_buf) | |
c906108c | 7214 | { |
6d820c5c | 7215 | putpkt (*buf); |
c2d11a7d | 7216 | getpkt (buf, sizeof_buf, 0); |
c906108c | 7217 | |
6d820c5c DJ |
7218 | if ((*buf)[0] == 'E') |
7219 | error (_("Remote failure reply: %s"), *buf); | |
c906108c SS |
7220 | } |
7221 | ||
6e5abd65 PA |
7222 | /* Return a pointer to an xmalloc'ed string representing an escaped |
7223 | version of BUF, of len N. E.g. \n is converted to \\n, \t to \\t, | |
7224 | etc. The caller is responsible for releasing the returned | |
7225 | memory. */ | |
7226 | ||
7227 | static char * | |
7228 | escape_buffer (const char *buf, int n) | |
7229 | { | |
7230 | struct cleanup *old_chain; | |
7231 | struct ui_file *stb; | |
7232 | char *str; | |
6e5abd65 PA |
7233 | |
7234 | stb = mem_fileopen (); | |
7235 | old_chain = make_cleanup_ui_file_delete (stb); | |
7236 | ||
7237 | fputstrn_unfiltered (buf, n, 0, stb); | |
759ef836 | 7238 | str = ui_file_xstrdup (stb, NULL); |
6e5abd65 PA |
7239 | do_cleanups (old_chain); |
7240 | return str; | |
7241 | } | |
7242 | ||
c906108c SS |
7243 | /* Display a null-terminated packet on stdout, for debugging, using C |
7244 | string notation. */ | |
7245 | ||
7246 | static void | |
fba45db2 | 7247 | print_packet (char *buf) |
c906108c SS |
7248 | { |
7249 | puts_filtered ("\""); | |
43e526b9 | 7250 | fputstr_filtered (buf, '"', gdb_stdout); |
c906108c SS |
7251 | puts_filtered ("\""); |
7252 | } | |
7253 | ||
7254 | int | |
fba45db2 | 7255 | putpkt (char *buf) |
c906108c SS |
7256 | { |
7257 | return putpkt_binary (buf, strlen (buf)); | |
7258 | } | |
7259 | ||
7260 | /* Send a packet to the remote machine, with error checking. The data | |
23860348 | 7261 | of the packet is in BUF. The string in BUF can be at most |
ea9c271d | 7262 | get_remote_packet_size () - 5 to account for the $, # and checksum, |
23860348 MS |
7263 | and for a possible /0 if we are debugging (remote_debug) and want |
7264 | to print the sent packet as a string. */ | |
c906108c SS |
7265 | |
7266 | static int | |
fba45db2 | 7267 | putpkt_binary (char *buf, int cnt) |
c906108c | 7268 | { |
2d717e4f | 7269 | struct remote_state *rs = get_remote_state (); |
c906108c SS |
7270 | int i; |
7271 | unsigned char csum = 0; | |
11cf8741 | 7272 | char *buf2 = alloca (cnt + 6); |
085dd6e6 | 7273 | |
c906108c SS |
7274 | int ch; |
7275 | int tcount = 0; | |
7276 | char *p; | |
dd61ec5c | 7277 | char *message; |
c906108c | 7278 | |
e24a49d8 PA |
7279 | /* Catch cases like trying to read memory or listing threads while |
7280 | we're waiting for a stop reply. The remote server wouldn't be | |
7281 | ready to handle this request, so we'd hang and timeout. We don't | |
7282 | have to worry about this in synchronous mode, because in that | |
7283 | case it's not possible to issue a command while the target is | |
74531fed PA |
7284 | running. This is not a problem in non-stop mode, because in that |
7285 | case, the stub is always ready to process serial input. */ | |
7286 | if (!non_stop && target_can_async_p () && rs->waiting_for_stop_reply) | |
e24a49d8 PA |
7287 | error (_("Cannot execute this command while the target is running.")); |
7288 | ||
2d717e4f DJ |
7289 | /* We're sending out a new packet. Make sure we don't look at a |
7290 | stale cached response. */ | |
7291 | rs->cached_wait_status = 0; | |
7292 | ||
c906108c SS |
7293 | /* Copy the packet into buffer BUF2, encapsulating it |
7294 | and giving it a checksum. */ | |
7295 | ||
c906108c SS |
7296 | p = buf2; |
7297 | *p++ = '$'; | |
7298 | ||
7299 | for (i = 0; i < cnt; i++) | |
7300 | { | |
7301 | csum += buf[i]; | |
7302 | *p++ = buf[i]; | |
7303 | } | |
7304 | *p++ = '#'; | |
7305 | *p++ = tohex ((csum >> 4) & 0xf); | |
7306 | *p++ = tohex (csum & 0xf); | |
7307 | ||
7308 | /* Send it over and over until we get a positive ack. */ | |
7309 | ||
7310 | while (1) | |
7311 | { | |
7312 | int started_error_output = 0; | |
7313 | ||
7314 | if (remote_debug) | |
7315 | { | |
6e5abd65 PA |
7316 | struct cleanup *old_chain; |
7317 | char *str; | |
7318 | ||
c906108c | 7319 | *p = '\0'; |
6e5abd65 PA |
7320 | str = escape_buffer (buf2, p - buf2); |
7321 | old_chain = make_cleanup (xfree, str); | |
7322 | fprintf_unfiltered (gdb_stdlog, "Sending packet: %s...", str); | |
0f71a2f6 | 7323 | gdb_flush (gdb_stdlog); |
6e5abd65 | 7324 | do_cleanups (old_chain); |
c906108c | 7325 | } |
c33e31fd | 7326 | remote_serial_write (buf2, p - buf2); |
c906108c | 7327 | |
a6f3e723 SL |
7328 | /* If this is a no acks version of the remote protocol, send the |
7329 | packet and move on. */ | |
7330 | if (rs->noack_mode) | |
7331 | break; | |
7332 | ||
74531fed PA |
7333 | /* Read until either a timeout occurs (-2) or '+' is read. |
7334 | Handle any notification that arrives in the mean time. */ | |
c906108c SS |
7335 | while (1) |
7336 | { | |
7337 | ch = readchar (remote_timeout); | |
7338 | ||
c5aa993b | 7339 | if (remote_debug) |
c906108c SS |
7340 | { |
7341 | switch (ch) | |
7342 | { | |
7343 | case '+': | |
1216fa2c | 7344 | case '-': |
c906108c SS |
7345 | case SERIAL_TIMEOUT: |
7346 | case '$': | |
74531fed | 7347 | case '%': |
c906108c SS |
7348 | if (started_error_output) |
7349 | { | |
7350 | putchar_unfiltered ('\n'); | |
7351 | started_error_output = 0; | |
7352 | } | |
7353 | } | |
7354 | } | |
7355 | ||
7356 | switch (ch) | |
7357 | { | |
7358 | case '+': | |
7359 | if (remote_debug) | |
0f71a2f6 | 7360 | fprintf_unfiltered (gdb_stdlog, "Ack\n"); |
c906108c | 7361 | return 1; |
1216fa2c AC |
7362 | case '-': |
7363 | if (remote_debug) | |
7364 | fprintf_unfiltered (gdb_stdlog, "Nak\n"); | |
a17d146e | 7365 | /* FALLTHROUGH */ |
c906108c | 7366 | case SERIAL_TIMEOUT: |
c5aa993b | 7367 | tcount++; |
c906108c SS |
7368 | if (tcount > 3) |
7369 | return 0; | |
23860348 | 7370 | break; /* Retransmit buffer. */ |
c906108c SS |
7371 | case '$': |
7372 | { | |
40e3f985 | 7373 | if (remote_debug) |
2bc416ba | 7374 | fprintf_unfiltered (gdb_stdlog, |
23860348 | 7375 | "Packet instead of Ack, ignoring it\n"); |
d6f7abdf AC |
7376 | /* It's probably an old response sent because an ACK |
7377 | was lost. Gobble up the packet and ack it so it | |
7378 | doesn't get retransmitted when we resend this | |
7379 | packet. */ | |
6d820c5c | 7380 | skip_frame (); |
c33e31fd | 7381 | remote_serial_write ("+", 1); |
23860348 | 7382 | continue; /* Now, go look for +. */ |
c906108c | 7383 | } |
74531fed PA |
7384 | |
7385 | case '%': | |
7386 | { | |
7387 | int val; | |
7388 | ||
7389 | /* If we got a notification, handle it, and go back to looking | |
7390 | for an ack. */ | |
7391 | /* We've found the start of a notification. Now | |
7392 | collect the data. */ | |
7393 | val = read_frame (&rs->buf, &rs->buf_size); | |
7394 | if (val >= 0) | |
7395 | { | |
7396 | if (remote_debug) | |
7397 | { | |
6e5abd65 PA |
7398 | struct cleanup *old_chain; |
7399 | char *str; | |
7400 | ||
7401 | str = escape_buffer (rs->buf, val); | |
7402 | old_chain = make_cleanup (xfree, str); | |
7403 | fprintf_unfiltered (gdb_stdlog, | |
7404 | " Notification received: %s\n", | |
7405 | str); | |
7406 | do_cleanups (old_chain); | |
74531fed | 7407 | } |
5965e028 | 7408 | handle_notification (rs->notif_state, rs->buf); |
74531fed PA |
7409 | /* We're in sync now, rewait for the ack. */ |
7410 | tcount = 0; | |
7411 | } | |
7412 | else | |
7413 | { | |
7414 | if (remote_debug) | |
7415 | { | |
7416 | if (!started_error_output) | |
7417 | { | |
7418 | started_error_output = 1; | |
7419 | fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: "); | |
7420 | } | |
7421 | fputc_unfiltered (ch & 0177, gdb_stdlog); | |
7422 | fprintf_unfiltered (gdb_stdlog, "%s", rs->buf); | |
7423 | } | |
7424 | } | |
7425 | continue; | |
7426 | } | |
7427 | /* fall-through */ | |
c906108c SS |
7428 | default: |
7429 | if (remote_debug) | |
7430 | { | |
7431 | if (!started_error_output) | |
7432 | { | |
7433 | started_error_output = 1; | |
0f71a2f6 | 7434 | fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: "); |
c906108c | 7435 | } |
0f71a2f6 | 7436 | fputc_unfiltered (ch & 0177, gdb_stdlog); |
c906108c SS |
7437 | } |
7438 | continue; | |
7439 | } | |
23860348 | 7440 | break; /* Here to retransmit. */ |
c906108c SS |
7441 | } |
7442 | ||
7443 | #if 0 | |
7444 | /* This is wrong. If doing a long backtrace, the user should be | |
c5aa993b JM |
7445 | able to get out next time we call QUIT, without anything as |
7446 | violent as interrupt_query. If we want to provide a way out of | |
7447 | here without getting to the next QUIT, it should be based on | |
7448 | hitting ^C twice as in remote_wait. */ | |
c906108c SS |
7449 | if (quit_flag) |
7450 | { | |
7451 | quit_flag = 0; | |
7452 | interrupt_query (); | |
7453 | } | |
7454 | #endif | |
7455 | } | |
a6f3e723 | 7456 | return 0; |
c906108c SS |
7457 | } |
7458 | ||
6d820c5c DJ |
7459 | /* Come here after finding the start of a frame when we expected an |
7460 | ack. Do our best to discard the rest of this packet. */ | |
7461 | ||
7462 | static void | |
7463 | skip_frame (void) | |
7464 | { | |
7465 | int c; | |
7466 | ||
7467 | while (1) | |
7468 | { | |
7469 | c = readchar (remote_timeout); | |
7470 | switch (c) | |
7471 | { | |
7472 | case SERIAL_TIMEOUT: | |
7473 | /* Nothing we can do. */ | |
7474 | return; | |
7475 | case '#': | |
7476 | /* Discard the two bytes of checksum and stop. */ | |
7477 | c = readchar (remote_timeout); | |
7478 | if (c >= 0) | |
7479 | c = readchar (remote_timeout); | |
7480 | ||
7481 | return; | |
7482 | case '*': /* Run length encoding. */ | |
7483 | /* Discard the repeat count. */ | |
7484 | c = readchar (remote_timeout); | |
7485 | if (c < 0) | |
7486 | return; | |
7487 | break; | |
7488 | default: | |
7489 | /* A regular character. */ | |
7490 | break; | |
7491 | } | |
7492 | } | |
7493 | } | |
7494 | ||
c906108c | 7495 | /* Come here after finding the start of the frame. Collect the rest |
6d820c5c DJ |
7496 | into *BUF, verifying the checksum, length, and handling run-length |
7497 | compression. NUL terminate the buffer. If there is not enough room, | |
7498 | expand *BUF using xrealloc. | |
c906108c | 7499 | |
c2d11a7d JM |
7500 | Returns -1 on error, number of characters in buffer (ignoring the |
7501 | trailing NULL) on success. (could be extended to return one of the | |
23860348 | 7502 | SERIAL status indications). */ |
c2d11a7d JM |
7503 | |
7504 | static long | |
6d820c5c DJ |
7505 | read_frame (char **buf_p, |
7506 | long *sizeof_buf) | |
c906108c SS |
7507 | { |
7508 | unsigned char csum; | |
c2d11a7d | 7509 | long bc; |
c906108c | 7510 | int c; |
6d820c5c | 7511 | char *buf = *buf_p; |
a6f3e723 | 7512 | struct remote_state *rs = get_remote_state (); |
c906108c SS |
7513 | |
7514 | csum = 0; | |
c2d11a7d | 7515 | bc = 0; |
c906108c SS |
7516 | |
7517 | while (1) | |
7518 | { | |
7519 | c = readchar (remote_timeout); | |
c906108c SS |
7520 | switch (c) |
7521 | { | |
7522 | case SERIAL_TIMEOUT: | |
7523 | if (remote_debug) | |
0f71a2f6 | 7524 | fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog); |
c2d11a7d | 7525 | return -1; |
c906108c SS |
7526 | case '$': |
7527 | if (remote_debug) | |
0f71a2f6 JM |
7528 | fputs_filtered ("Saw new packet start in middle of old one\n", |
7529 | gdb_stdlog); | |
23860348 | 7530 | return -1; /* Start a new packet, count retries. */ |
c906108c SS |
7531 | case '#': |
7532 | { | |
7533 | unsigned char pktcsum; | |
e1b09194 AC |
7534 | int check_0 = 0; |
7535 | int check_1 = 0; | |
c906108c | 7536 | |
c2d11a7d | 7537 | buf[bc] = '\0'; |
c906108c | 7538 | |
e1b09194 AC |
7539 | check_0 = readchar (remote_timeout); |
7540 | if (check_0 >= 0) | |
7541 | check_1 = readchar (remote_timeout); | |
802188a7 | 7542 | |
e1b09194 AC |
7543 | if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT) |
7544 | { | |
7545 | if (remote_debug) | |
2bc416ba | 7546 | fputs_filtered ("Timeout in checksum, retrying\n", |
23860348 | 7547 | gdb_stdlog); |
e1b09194 AC |
7548 | return -1; |
7549 | } | |
7550 | else if (check_0 < 0 || check_1 < 0) | |
40e3f985 FN |
7551 | { |
7552 | if (remote_debug) | |
2bc416ba | 7553 | fputs_filtered ("Communication error in checksum\n", |
23860348 | 7554 | gdb_stdlog); |
40e3f985 FN |
7555 | return -1; |
7556 | } | |
c906108c | 7557 | |
a6f3e723 SL |
7558 | /* Don't recompute the checksum; with no ack packets we |
7559 | don't have any way to indicate a packet retransmission | |
7560 | is necessary. */ | |
7561 | if (rs->noack_mode) | |
7562 | return bc; | |
7563 | ||
e1b09194 | 7564 | pktcsum = (fromhex (check_0) << 4) | fromhex (check_1); |
c906108c | 7565 | if (csum == pktcsum) |
c2d11a7d | 7566 | return bc; |
c906108c | 7567 | |
c5aa993b | 7568 | if (remote_debug) |
c906108c | 7569 | { |
6e5abd65 PA |
7570 | struct cleanup *old_chain; |
7571 | char *str; | |
7572 | ||
7573 | str = escape_buffer (buf, bc); | |
7574 | old_chain = make_cleanup (xfree, str); | |
7575 | fprintf_unfiltered (gdb_stdlog, | |
3e43a32a MS |
7576 | "Bad checksum, sentsum=0x%x, " |
7577 | "csum=0x%x, buf=%s\n", | |
6e5abd65 PA |
7578 | pktcsum, csum, str); |
7579 | do_cleanups (old_chain); | |
c906108c | 7580 | } |
c2d11a7d | 7581 | /* Number of characters in buffer ignoring trailing |
23860348 | 7582 | NULL. */ |
c2d11a7d | 7583 | return -1; |
c906108c | 7584 | } |
23860348 | 7585 | case '*': /* Run length encoding. */ |
c2c6d25f JM |
7586 | { |
7587 | int repeat; | |
c906108c | 7588 | |
a744cf53 | 7589 | csum += c; |
b4501125 AC |
7590 | c = readchar (remote_timeout); |
7591 | csum += c; | |
23860348 | 7592 | repeat = c - ' ' + 3; /* Compute repeat count. */ |
c906108c | 7593 | |
23860348 | 7594 | /* The character before ``*'' is repeated. */ |
c2d11a7d | 7595 | |
6d820c5c | 7596 | if (repeat > 0 && repeat <= 255 && bc > 0) |
c2c6d25f | 7597 | { |
6d820c5c DJ |
7598 | if (bc + repeat - 1 >= *sizeof_buf - 1) |
7599 | { | |
7600 | /* Make some more room in the buffer. */ | |
7601 | *sizeof_buf += repeat; | |
7602 | *buf_p = xrealloc (*buf_p, *sizeof_buf); | |
7603 | buf = *buf_p; | |
7604 | } | |
7605 | ||
c2d11a7d JM |
7606 | memset (&buf[bc], buf[bc - 1], repeat); |
7607 | bc += repeat; | |
c2c6d25f JM |
7608 | continue; |
7609 | } | |
7610 | ||
c2d11a7d | 7611 | buf[bc] = '\0'; |
6d820c5c | 7612 | printf_filtered (_("Invalid run length encoding: %s\n"), buf); |
c2d11a7d | 7613 | return -1; |
c2c6d25f | 7614 | } |
c906108c | 7615 | default: |
6d820c5c | 7616 | if (bc >= *sizeof_buf - 1) |
c906108c | 7617 | { |
6d820c5c DJ |
7618 | /* Make some more room in the buffer. */ |
7619 | *sizeof_buf *= 2; | |
7620 | *buf_p = xrealloc (*buf_p, *sizeof_buf); | |
7621 | buf = *buf_p; | |
c906108c SS |
7622 | } |
7623 | ||
6d820c5c DJ |
7624 | buf[bc++] = c; |
7625 | csum += c; | |
7626 | continue; | |
c906108c SS |
7627 | } |
7628 | } | |
7629 | } | |
7630 | ||
7631 | /* Read a packet from the remote machine, with error checking, and | |
6d820c5c DJ |
7632 | store it in *BUF. Resize *BUF using xrealloc if necessary to hold |
7633 | the result, and update *SIZEOF_BUF. If FOREVER, wait forever | |
7634 | rather than timing out; this is used (in synchronous mode) to wait | |
7635 | for a target that is is executing user code to stop. */ | |
d9fcf2fb JM |
7636 | /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we |
7637 | don't have to change all the calls to getpkt to deal with the | |
7638 | return value, because at the moment I don't know what the right | |
23860348 | 7639 | thing to do it for those. */ |
c906108c | 7640 | void |
6d820c5c DJ |
7641 | getpkt (char **buf, |
7642 | long *sizeof_buf, | |
c2d11a7d | 7643 | int forever) |
d9fcf2fb JM |
7644 | { |
7645 | int timed_out; | |
7646 | ||
7647 | timed_out = getpkt_sane (buf, sizeof_buf, forever); | |
7648 | } | |
7649 | ||
7650 | ||
7651 | /* Read a packet from the remote machine, with error checking, and | |
6d820c5c DJ |
7652 | store it in *BUF. Resize *BUF using xrealloc if necessary to hold |
7653 | the result, and update *SIZEOF_BUF. If FOREVER, wait forever | |
7654 | rather than timing out; this is used (in synchronous mode) to wait | |
7655 | for a target that is is executing user code to stop. If FOREVER == | |
7656 | 0, this function is allowed to time out gracefully and return an | |
74531fed PA |
7657 | indication of this to the caller. Otherwise return the number of |
7658 | bytes read. If EXPECTING_NOTIF, consider receiving a notification | |
fee9eda9 YQ |
7659 | enough reason to return to the caller. *IS_NOTIF is an output |
7660 | boolean that indicates whether *BUF holds a notification or not | |
7661 | (a regular packet). */ | |
74531fed | 7662 | |
3172dc30 | 7663 | static int |
74531fed | 7664 | getpkt_or_notif_sane_1 (char **buf, long *sizeof_buf, int forever, |
fee9eda9 | 7665 | int expecting_notif, int *is_notif) |
c906108c | 7666 | { |
2d717e4f | 7667 | struct remote_state *rs = get_remote_state (); |
c906108c SS |
7668 | int c; |
7669 | int tries; | |
7670 | int timeout; | |
df4b58fe | 7671 | int val = -1; |
c906108c | 7672 | |
2d717e4f DJ |
7673 | /* We're reading a new response. Make sure we don't look at a |
7674 | previously cached response. */ | |
7675 | rs->cached_wait_status = 0; | |
7676 | ||
6d820c5c | 7677 | strcpy (*buf, "timeout"); |
c906108c SS |
7678 | |
7679 | if (forever) | |
74531fed PA |
7680 | timeout = watchdog > 0 ? watchdog : -1; |
7681 | else if (expecting_notif) | |
7682 | timeout = 0; /* There should already be a char in the buffer. If | |
7683 | not, bail out. */ | |
c906108c SS |
7684 | else |
7685 | timeout = remote_timeout; | |
7686 | ||
7687 | #define MAX_TRIES 3 | |
7688 | ||
74531fed PA |
7689 | /* Process any number of notifications, and then return when |
7690 | we get a packet. */ | |
7691 | for (;;) | |
c906108c | 7692 | { |
d9c43928 | 7693 | /* If we get a timeout or bad checksum, retry up to MAX_TRIES |
74531fed PA |
7694 | times. */ |
7695 | for (tries = 1; tries <= MAX_TRIES; tries++) | |
c906108c | 7696 | { |
74531fed PA |
7697 | /* This can loop forever if the remote side sends us |
7698 | characters continuously, but if it pauses, we'll get | |
7699 | SERIAL_TIMEOUT from readchar because of timeout. Then | |
7700 | we'll count that as a retry. | |
7701 | ||
7702 | Note that even when forever is set, we will only wait | |
7703 | forever prior to the start of a packet. After that, we | |
7704 | expect characters to arrive at a brisk pace. They should | |
7705 | show up within remote_timeout intervals. */ | |
7706 | do | |
7707 | c = readchar (timeout); | |
7708 | while (c != SERIAL_TIMEOUT && c != '$' && c != '%'); | |
c906108c SS |
7709 | |
7710 | if (c == SERIAL_TIMEOUT) | |
7711 | { | |
74531fed PA |
7712 | if (expecting_notif) |
7713 | return -1; /* Don't complain, it's normal to not get | |
7714 | anything in this case. */ | |
7715 | ||
23860348 | 7716 | if (forever) /* Watchdog went off? Kill the target. */ |
c906108c | 7717 | { |
2acceee2 | 7718 | QUIT; |
78a095c3 | 7719 | remote_unpush_target (); |
598d3636 JK |
7720 | throw_error (TARGET_CLOSE_ERROR, |
7721 | _("Watchdog timeout has expired. " | |
7722 | "Target detached.")); | |
c906108c | 7723 | } |
c906108c | 7724 | if (remote_debug) |
0f71a2f6 | 7725 | fputs_filtered ("Timed out.\n", gdb_stdlog); |
c906108c | 7726 | } |
74531fed PA |
7727 | else |
7728 | { | |
7729 | /* We've found the start of a packet or notification. | |
7730 | Now collect the data. */ | |
7731 | val = read_frame (buf, sizeof_buf); | |
7732 | if (val >= 0) | |
7733 | break; | |
7734 | } | |
7735 | ||
c33e31fd | 7736 | remote_serial_write ("-", 1); |
c906108c | 7737 | } |
c906108c | 7738 | |
74531fed PA |
7739 | if (tries > MAX_TRIES) |
7740 | { | |
7741 | /* We have tried hard enough, and just can't receive the | |
7742 | packet/notification. Give up. */ | |
7743 | printf_unfiltered (_("Ignoring packet error, continuing...\n")); | |
c906108c | 7744 | |
74531fed PA |
7745 | /* Skip the ack char if we're in no-ack mode. */ |
7746 | if (!rs->noack_mode) | |
c33e31fd | 7747 | remote_serial_write ("+", 1); |
74531fed PA |
7748 | return -1; |
7749 | } | |
c906108c | 7750 | |
74531fed PA |
7751 | /* If we got an ordinary packet, return that to our caller. */ |
7752 | if (c == '$') | |
c906108c SS |
7753 | { |
7754 | if (remote_debug) | |
43e526b9 | 7755 | { |
6e5abd65 PA |
7756 | struct cleanup *old_chain; |
7757 | char *str; | |
7758 | ||
7759 | str = escape_buffer (*buf, val); | |
7760 | old_chain = make_cleanup (xfree, str); | |
7761 | fprintf_unfiltered (gdb_stdlog, "Packet received: %s\n", str); | |
7762 | do_cleanups (old_chain); | |
43e526b9 | 7763 | } |
a6f3e723 SL |
7764 | |
7765 | /* Skip the ack char if we're in no-ack mode. */ | |
7766 | if (!rs->noack_mode) | |
c33e31fd | 7767 | remote_serial_write ("+", 1); |
fee9eda9 YQ |
7768 | if (is_notif != NULL) |
7769 | *is_notif = 0; | |
0876f84a | 7770 | return val; |
c906108c SS |
7771 | } |
7772 | ||
74531fed PA |
7773 | /* If we got a notification, handle it, and go back to looking |
7774 | for a packet. */ | |
7775 | else | |
7776 | { | |
7777 | gdb_assert (c == '%'); | |
7778 | ||
7779 | if (remote_debug) | |
7780 | { | |
6e5abd65 PA |
7781 | struct cleanup *old_chain; |
7782 | char *str; | |
7783 | ||
7784 | str = escape_buffer (*buf, val); | |
7785 | old_chain = make_cleanup (xfree, str); | |
7786 | fprintf_unfiltered (gdb_stdlog, | |
7787 | " Notification received: %s\n", | |
7788 | str); | |
7789 | do_cleanups (old_chain); | |
74531fed | 7790 | } |
fee9eda9 YQ |
7791 | if (is_notif != NULL) |
7792 | *is_notif = 1; | |
c906108c | 7793 | |
5965e028 | 7794 | handle_notification (rs->notif_state, *buf); |
c906108c | 7795 | |
74531fed | 7796 | /* Notifications require no acknowledgement. */ |
a6f3e723 | 7797 | |
74531fed | 7798 | if (expecting_notif) |
fee9eda9 | 7799 | return val; |
74531fed PA |
7800 | } |
7801 | } | |
7802 | } | |
7803 | ||
7804 | static int | |
7805 | getpkt_sane (char **buf, long *sizeof_buf, int forever) | |
7806 | { | |
fee9eda9 | 7807 | return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 0, NULL); |
74531fed PA |
7808 | } |
7809 | ||
7810 | static int | |
fee9eda9 YQ |
7811 | getpkt_or_notif_sane (char **buf, long *sizeof_buf, int forever, |
7812 | int *is_notif) | |
74531fed | 7813 | { |
fee9eda9 YQ |
7814 | return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 1, |
7815 | is_notif); | |
c906108c | 7816 | } |
74531fed | 7817 | |
c906108c SS |
7818 | \f |
7819 | static void | |
7d85a9c0 | 7820 | remote_kill (struct target_ops *ops) |
43ff13b4 | 7821 | { |
0fdf84ca PA |
7822 | struct gdb_exception ex; |
7823 | ||
7824 | /* Catch errors so the user can quit from gdb even when we | |
23860348 | 7825 | aren't on speaking terms with the remote system. */ |
0fdf84ca PA |
7826 | TRY_CATCH (ex, RETURN_MASK_ERROR) |
7827 | { | |
7828 | putpkt ("k"); | |
7829 | } | |
7830 | if (ex.reason < 0) | |
7831 | { | |
7832 | if (ex.error == TARGET_CLOSE_ERROR) | |
7833 | { | |
7834 | /* If we got an (EOF) error that caused the target | |
7835 | to go away, then we're done, that's what we wanted. | |
7836 | "k" is susceptible to cause a premature EOF, given | |
7837 | that the remote server isn't actually required to | |
7838 | reply to "k", and it can happen that it doesn't | |
7839 | even get to reply ACK to the "k". */ | |
7840 | return; | |
7841 | } | |
7842 | ||
7843 | /* Otherwise, something went wrong. We didn't actually kill | |
7844 | the target. Just propagate the exception, and let the | |
7845 | user or higher layers decide what to do. */ | |
7846 | throw_exception (ex); | |
7847 | } | |
43ff13b4 | 7848 | |
0fdf84ca PA |
7849 | /* We've killed the remote end, we get to mourn it. Since this is |
7850 | target remote, single-process, mourning the inferior also | |
7851 | unpushes remote_ops. */ | |
43ff13b4 JM |
7852 | target_mourn_inferior (); |
7853 | } | |
7854 | ||
82f73884 PA |
7855 | static int |
7856 | remote_vkill (int pid, struct remote_state *rs) | |
7857 | { | |
7858 | if (remote_protocol_packets[PACKET_vKill].support == PACKET_DISABLE) | |
7859 | return -1; | |
7860 | ||
7861 | /* Tell the remote target to detach. */ | |
bba74b36 | 7862 | xsnprintf (rs->buf, get_remote_packet_size (), "vKill;%x", pid); |
82f73884 PA |
7863 | putpkt (rs->buf); |
7864 | getpkt (&rs->buf, &rs->buf_size, 0); | |
7865 | ||
7866 | if (packet_ok (rs->buf, | |
7867 | &remote_protocol_packets[PACKET_vKill]) == PACKET_OK) | |
7868 | return 0; | |
7869 | else if (remote_protocol_packets[PACKET_vKill].support == PACKET_DISABLE) | |
7870 | return -1; | |
7871 | else | |
7872 | return 1; | |
7873 | } | |
7874 | ||
7875 | static void | |
7d85a9c0 | 7876 | extended_remote_kill (struct target_ops *ops) |
82f73884 PA |
7877 | { |
7878 | int res; | |
7879 | int pid = ptid_get_pid (inferior_ptid); | |
7880 | struct remote_state *rs = get_remote_state (); | |
7881 | ||
7882 | res = remote_vkill (pid, rs); | |
901f9912 | 7883 | if (res == -1 && !(rs->extended && remote_multi_process_p (rs))) |
82f73884 PA |
7884 | { |
7885 | /* Don't try 'k' on a multi-process aware stub -- it has no way | |
7886 | to specify the pid. */ | |
7887 | ||
7888 | putpkt ("k"); | |
7889 | #if 0 | |
7890 | getpkt (&rs->buf, &rs->buf_size, 0); | |
7891 | if (rs->buf[0] != 'O' || rs->buf[0] != 'K') | |
7892 | res = 1; | |
7893 | #else | |
7894 | /* Don't wait for it to die. I'm not really sure it matters whether | |
7895 | we do or not. For the existing stubs, kill is a noop. */ | |
7896 | res = 0; | |
7897 | #endif | |
7898 | } | |
7899 | ||
7900 | if (res != 0) | |
7901 | error (_("Can't kill process")); | |
7902 | ||
82f73884 PA |
7903 | target_mourn_inferior (); |
7904 | } | |
7905 | ||
c906108c | 7906 | static void |
136d6dae | 7907 | remote_mourn (struct target_ops *ops) |
c906108c | 7908 | { |
136d6dae | 7909 | remote_mourn_1 (ops); |
c906108c SS |
7910 | } |
7911 | ||
c906108c SS |
7912 | /* Worker function for remote_mourn. */ |
7913 | static void | |
fba45db2 | 7914 | remote_mourn_1 (struct target_ops *target) |
c906108c SS |
7915 | { |
7916 | unpush_target (target); | |
ce5ce7ed | 7917 | |
8a2492ee PA |
7918 | /* remote_close takes care of doing most of the clean up. */ |
7919 | generic_mourn_inferior (); | |
c906108c SS |
7920 | } |
7921 | ||
2d717e4f DJ |
7922 | static void |
7923 | extended_remote_mourn_1 (struct target_ops *target) | |
7924 | { | |
7925 | struct remote_state *rs = get_remote_state (); | |
c906108c | 7926 | |
e24a49d8 PA |
7927 | /* In case we got here due to an error, but we're going to stay |
7928 | connected. */ | |
7929 | rs->waiting_for_stop_reply = 0; | |
7930 | ||
dc1981d7 PA |
7931 | /* If the current general thread belonged to the process we just |
7932 | detached from or has exited, the remote side current general | |
7933 | thread becomes undefined. Considering a case like this: | |
7934 | ||
7935 | - We just got here due to a detach. | |
7936 | - The process that we're detaching from happens to immediately | |
7937 | report a global breakpoint being hit in non-stop mode, in the | |
7938 | same thread we had selected before. | |
7939 | - GDB attaches to this process again. | |
7940 | - This event happens to be the next event we handle. | |
7941 | ||
7942 | GDB would consider that the current general thread didn't need to | |
7943 | be set on the stub side (with Hg), since for all it knew, | |
7944 | GENERAL_THREAD hadn't changed. | |
7945 | ||
7946 | Notice that although in all-stop mode, the remote server always | |
7947 | sets the current thread to the thread reporting the stop event, | |
7948 | that doesn't happen in non-stop mode; in non-stop, the stub *must | |
7949 | not* change the current thread when reporting a breakpoint hit, | |
7950 | due to the decoupling of event reporting and event handling. | |
7951 | ||
7952 | To keep things simple, we always invalidate our notion of the | |
7953 | current thread. */ | |
47f8a51d | 7954 | record_currthread (rs, minus_one_ptid); |
dc1981d7 | 7955 | |
2d717e4f DJ |
7956 | /* Unlike "target remote", we do not want to unpush the target; then |
7957 | the next time the user says "run", we won't be connected. */ | |
7958 | ||
48aa3c27 PA |
7959 | /* Call common code to mark the inferior as not running. */ |
7960 | generic_mourn_inferior (); | |
7961 | ||
d729566a | 7962 | if (!have_inferiors ()) |
2d717e4f | 7963 | { |
82f73884 PA |
7964 | if (!remote_multi_process_p (rs)) |
7965 | { | |
7966 | /* Check whether the target is running now - some remote stubs | |
7967 | automatically restart after kill. */ | |
7968 | putpkt ("?"); | |
7969 | getpkt (&rs->buf, &rs->buf_size, 0); | |
7970 | ||
7971 | if (rs->buf[0] == 'S' || rs->buf[0] == 'T') | |
7972 | { | |
3e43a32a MS |
7973 | /* Assume that the target has been restarted. Set |
7974 | inferior_ptid so that bits of core GDB realizes | |
7975 | there's something here, e.g., so that the user can | |
7976 | say "kill" again. */ | |
82f73884 PA |
7977 | inferior_ptid = magic_null_ptid; |
7978 | } | |
82f73884 | 7979 | } |
2d717e4f DJ |
7980 | } |
7981 | } | |
c906108c SS |
7982 | |
7983 | static void | |
136d6dae | 7984 | extended_remote_mourn (struct target_ops *ops) |
c906108c | 7985 | { |
136d6dae | 7986 | extended_remote_mourn_1 (ops); |
2d717e4f | 7987 | } |
c906108c | 7988 | |
03583c20 UW |
7989 | static int |
7990 | extended_remote_supports_disable_randomization (void) | |
7991 | { | |
7992 | return (remote_protocol_packets[PACKET_QDisableRandomization].support | |
7993 | == PACKET_ENABLE); | |
7994 | } | |
7995 | ||
7996 | static void | |
7997 | extended_remote_disable_randomization (int val) | |
7998 | { | |
7999 | struct remote_state *rs = get_remote_state (); | |
8000 | char *reply; | |
8001 | ||
bba74b36 YQ |
8002 | xsnprintf (rs->buf, get_remote_packet_size (), "QDisableRandomization:%x", |
8003 | val); | |
03583c20 UW |
8004 | putpkt (rs->buf); |
8005 | reply = remote_get_noisy_reply (&target_buf, &target_buf_size); | |
8006 | if (*reply == '\0') | |
8007 | error (_("Target does not support QDisableRandomization.")); | |
8008 | if (strcmp (reply, "OK") != 0) | |
8009 | error (_("Bogus QDisableRandomization reply from target: %s"), reply); | |
8010 | } | |
8011 | ||
2d717e4f DJ |
8012 | static int |
8013 | extended_remote_run (char *args) | |
8014 | { | |
8015 | struct remote_state *rs = get_remote_state (); | |
2d717e4f | 8016 | int len; |
c906108c | 8017 | |
2d717e4f DJ |
8018 | /* If the user has disabled vRun support, or we have detected that |
8019 | support is not available, do not try it. */ | |
8020 | if (remote_protocol_packets[PACKET_vRun].support == PACKET_DISABLE) | |
8021 | return -1; | |
424163ea | 8022 | |
2d717e4f DJ |
8023 | strcpy (rs->buf, "vRun;"); |
8024 | len = strlen (rs->buf); | |
c906108c | 8025 | |
2d717e4f DJ |
8026 | if (strlen (remote_exec_file) * 2 + len >= get_remote_packet_size ()) |
8027 | error (_("Remote file name too long for run packet")); | |
8028 | len += 2 * bin2hex ((gdb_byte *) remote_exec_file, rs->buf + len, 0); | |
8029 | ||
d1a41061 | 8030 | gdb_assert (args != NULL); |
2d717e4f DJ |
8031 | if (*args) |
8032 | { | |
8033 | struct cleanup *back_to; | |
8034 | int i; | |
8035 | char **argv; | |
8036 | ||
d1a41061 | 8037 | argv = gdb_buildargv (args); |
2d717e4f DJ |
8038 | back_to = make_cleanup ((void (*) (void *)) freeargv, argv); |
8039 | for (i = 0; argv[i] != NULL; i++) | |
8040 | { | |
8041 | if (strlen (argv[i]) * 2 + 1 + len >= get_remote_packet_size ()) | |
8042 | error (_("Argument list too long for run packet")); | |
8043 | rs->buf[len++] = ';'; | |
8044 | len += 2 * bin2hex ((gdb_byte *) argv[i], rs->buf + len, 0); | |
8045 | } | |
8046 | do_cleanups (back_to); | |
8047 | } | |
8048 | ||
8049 | rs->buf[len++] = '\0'; | |
8050 | ||
8051 | putpkt (rs->buf); | |
8052 | getpkt (&rs->buf, &rs->buf_size, 0); | |
8053 | ||
8054 | if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vRun]) == PACKET_OK) | |
8055 | { | |
3405876a | 8056 | /* We have a wait response. All is well. */ |
2d717e4f DJ |
8057 | return 0; |
8058 | } | |
8059 | else if (remote_protocol_packets[PACKET_vRun].support == PACKET_DISABLE) | |
8060 | /* It wasn't disabled before, but it is now. */ | |
8061 | return -1; | |
8062 | else | |
8063 | { | |
8064 | if (remote_exec_file[0] == '\0') | |
8065 | error (_("Running the default executable on the remote target failed; " | |
8066 | "try \"set remote exec-file\"?")); | |
8067 | else | |
8068 | error (_("Running \"%s\" on the remote target failed"), | |
8069 | remote_exec_file); | |
8070 | } | |
c906108c SS |
8071 | } |
8072 | ||
2d717e4f DJ |
8073 | /* In the extended protocol we want to be able to do things like |
8074 | "run" and have them basically work as expected. So we need | |
8075 | a special create_inferior function. We support changing the | |
8076 | executable file and the command line arguments, but not the | |
8077 | environment. */ | |
8078 | ||
43ff13b4 | 8079 | static void |
2d717e4f | 8080 | extended_remote_create_inferior_1 (char *exec_file, char *args, |
75c99385 | 8081 | char **env, int from_tty) |
43ff13b4 | 8082 | { |
3405876a PA |
8083 | int run_worked; |
8084 | char *stop_reply; | |
8085 | struct remote_state *rs = get_remote_state (); | |
8086 | ||
43ff13b4 | 8087 | /* If running asynchronously, register the target file descriptor |
23860348 | 8088 | with the event loop. */ |
75c99385 | 8089 | if (target_can_async_p ()) |
2acceee2 | 8090 | target_async (inferior_event_handler, 0); |
43ff13b4 | 8091 | |
03583c20 UW |
8092 | /* Disable address space randomization if requested (and supported). */ |
8093 | if (extended_remote_supports_disable_randomization ()) | |
8094 | extended_remote_disable_randomization (disable_randomization); | |
8095 | ||
43ff13b4 | 8096 | /* Now restart the remote server. */ |
3405876a PA |
8097 | run_worked = extended_remote_run (args) != -1; |
8098 | if (!run_worked) | |
2d717e4f DJ |
8099 | { |
8100 | /* vRun was not supported. Fail if we need it to do what the | |
8101 | user requested. */ | |
8102 | if (remote_exec_file[0]) | |
8103 | error (_("Remote target does not support \"set remote exec-file\"")); | |
8104 | if (args[0]) | |
8105 | error (_("Remote target does not support \"set args\" or run <ARGS>")); | |
43ff13b4 | 8106 | |
2d717e4f DJ |
8107 | /* Fall back to "R". */ |
8108 | extended_remote_restart (); | |
8109 | } | |
424163ea | 8110 | |
6c95b8df PA |
8111 | if (!have_inferiors ()) |
8112 | { | |
8113 | /* Clean up from the last time we ran, before we mark the target | |
8114 | running again. This will mark breakpoints uninserted, and | |
8115 | get_offsets may insert breakpoints. */ | |
8116 | init_thread_list (); | |
8117 | init_wait_for_inferior (); | |
8118 | } | |
45280a52 | 8119 | |
3405876a PA |
8120 | /* vRun's success return is a stop reply. */ |
8121 | stop_reply = run_worked ? rs->buf : NULL; | |
8122 | add_current_inferior_and_thread (stop_reply); | |
c0a2216e | 8123 | |
2d717e4f DJ |
8124 | /* Get updated offsets, if the stub uses qOffsets. */ |
8125 | get_offsets (); | |
2d717e4f DJ |
8126 | } |
8127 | ||
8128 | static void | |
136d6dae VP |
8129 | extended_remote_create_inferior (struct target_ops *ops, |
8130 | char *exec_file, char *args, | |
2d717e4f DJ |
8131 | char **env, int from_tty) |
8132 | { | |
75c99385 | 8133 | extended_remote_create_inferior_1 (exec_file, args, env, from_tty); |
43ff13b4 | 8134 | } |
c906108c | 8135 | \f |
c5aa993b | 8136 | |
b775012e LM |
8137 | /* Given a location's target info BP_TGT and the packet buffer BUF, output |
8138 | the list of conditions (in agent expression bytecode format), if any, the | |
8139 | target needs to evaluate. The output is placed into the packet buffer | |
bba74b36 | 8140 | started from BUF and ended at BUF_END. */ |
b775012e LM |
8141 | |
8142 | static int | |
8143 | remote_add_target_side_condition (struct gdbarch *gdbarch, | |
bba74b36 YQ |
8144 | struct bp_target_info *bp_tgt, char *buf, |
8145 | char *buf_end) | |
b775012e LM |
8146 | { |
8147 | struct agent_expr *aexpr = NULL; | |
8148 | int i, ix; | |
8149 | char *pkt; | |
8150 | char *buf_start = buf; | |
8151 | ||
8152 | if (VEC_empty (agent_expr_p, bp_tgt->conditions)) | |
8153 | return 0; | |
8154 | ||
8155 | buf += strlen (buf); | |
bba74b36 | 8156 | xsnprintf (buf, buf_end - buf, "%s", ";"); |
b775012e LM |
8157 | buf++; |
8158 | ||
8159 | /* Send conditions to the target and free the vector. */ | |
8160 | for (ix = 0; | |
8161 | VEC_iterate (agent_expr_p, bp_tgt->conditions, ix, aexpr); | |
8162 | ix++) | |
8163 | { | |
bba74b36 | 8164 | xsnprintf (buf, buf_end - buf, "X%x,", aexpr->len); |
b775012e LM |
8165 | buf += strlen (buf); |
8166 | for (i = 0; i < aexpr->len; ++i) | |
8167 | buf = pack_hex_byte (buf, aexpr->buf[i]); | |
8168 | *buf = '\0'; | |
8169 | } | |
b775012e LM |
8170 | return 0; |
8171 | } | |
8172 | ||
d3ce09f5 SS |
8173 | static void |
8174 | remote_add_target_side_commands (struct gdbarch *gdbarch, | |
8175 | struct bp_target_info *bp_tgt, char *buf) | |
8176 | { | |
8177 | struct agent_expr *aexpr = NULL; | |
8178 | int i, ix; | |
8179 | ||
8180 | if (VEC_empty (agent_expr_p, bp_tgt->tcommands)) | |
8181 | return; | |
8182 | ||
8183 | buf += strlen (buf); | |
8184 | ||
8185 | sprintf (buf, ";cmds:%x,", bp_tgt->persist); | |
8186 | buf += strlen (buf); | |
8187 | ||
8188 | /* Concatenate all the agent expressions that are commands into the | |
8189 | cmds parameter. */ | |
8190 | for (ix = 0; | |
8191 | VEC_iterate (agent_expr_p, bp_tgt->tcommands, ix, aexpr); | |
8192 | ix++) | |
8193 | { | |
8194 | sprintf (buf, "X%x,", aexpr->len); | |
8195 | buf += strlen (buf); | |
8196 | for (i = 0; i < aexpr->len; ++i) | |
8197 | buf = pack_hex_byte (buf, aexpr->buf[i]); | |
8198 | *buf = '\0'; | |
8199 | } | |
d3ce09f5 SS |
8200 | } |
8201 | ||
8181d85f DJ |
8202 | /* Insert a breakpoint. On targets that have software breakpoint |
8203 | support, we ask the remote target to do the work; on targets | |
8204 | which don't, we insert a traditional memory breakpoint. */ | |
c906108c SS |
8205 | |
8206 | static int | |
a6d9a66e UW |
8207 | remote_insert_breakpoint (struct gdbarch *gdbarch, |
8208 | struct bp_target_info *bp_tgt) | |
c906108c | 8209 | { |
d471ea57 AC |
8210 | /* Try the "Z" s/w breakpoint packet if it is not already disabled. |
8211 | If it succeeds, then set the support to PACKET_ENABLE. If it | |
8212 | fails, and the user has explicitly requested the Z support then | |
23860348 | 8213 | report an error, otherwise, mark it disabled and go on. */ |
802188a7 | 8214 | |
444abaca | 8215 | if (remote_protocol_packets[PACKET_Z0].support != PACKET_DISABLE) |
96baa820 | 8216 | { |
7c0f6dcc | 8217 | CORE_ADDR addr = bp_tgt->placed_address; |
4fff2411 | 8218 | struct remote_state *rs; |
bba74b36 | 8219 | char *p, *endbuf; |
7c0f6dcc | 8220 | int bpsize; |
b775012e | 8221 | struct condition_list *cond = NULL; |
4fff2411 | 8222 | |
28439a30 PA |
8223 | /* Make sure the remote is pointing at the right process, if |
8224 | necessary. */ | |
8225 | if (!gdbarch_has_global_breakpoints (target_gdbarch ())) | |
8226 | set_general_process (); | |
8227 | ||
a1dcb23a | 8228 | gdbarch_remote_breakpoint_from_pc (gdbarch, &addr, &bpsize); |
4fff2411 JZ |
8229 | |
8230 | rs = get_remote_state (); | |
8231 | p = rs->buf; | |
bba74b36 | 8232 | endbuf = rs->buf + get_remote_packet_size (); |
802188a7 | 8233 | |
96baa820 JM |
8234 | *(p++) = 'Z'; |
8235 | *(p++) = '0'; | |
8236 | *(p++) = ','; | |
7c0f6dcc | 8237 | addr = (ULONGEST) remote_address_masked (addr); |
8181d85f | 8238 | p += hexnumstr (p, addr); |
bba74b36 | 8239 | xsnprintf (p, endbuf - p, ",%d", bpsize); |
802188a7 | 8240 | |
b775012e | 8241 | if (remote_supports_cond_breakpoints ()) |
bba74b36 | 8242 | remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf); |
b775012e | 8243 | |
d3ce09f5 SS |
8244 | if (remote_can_run_breakpoint_commands ()) |
8245 | remote_add_target_side_commands (gdbarch, bp_tgt, p); | |
8246 | ||
6d820c5c DJ |
8247 | putpkt (rs->buf); |
8248 | getpkt (&rs->buf, &rs->buf_size, 0); | |
96baa820 | 8249 | |
6d820c5c | 8250 | switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0])) |
96baa820 | 8251 | { |
d471ea57 AC |
8252 | case PACKET_ERROR: |
8253 | return -1; | |
8254 | case PACKET_OK: | |
7c0f6dcc JL |
8255 | bp_tgt->placed_address = addr; |
8256 | bp_tgt->placed_size = bpsize; | |
d471ea57 AC |
8257 | return 0; |
8258 | case PACKET_UNKNOWN: | |
8259 | break; | |
96baa820 JM |
8260 | } |
8261 | } | |
c906108c | 8262 | |
a6d9a66e | 8263 | return memory_insert_breakpoint (gdbarch, bp_tgt); |
c906108c SS |
8264 | } |
8265 | ||
8266 | static int | |
a6d9a66e UW |
8267 | remote_remove_breakpoint (struct gdbarch *gdbarch, |
8268 | struct bp_target_info *bp_tgt) | |
c906108c | 8269 | { |
8181d85f | 8270 | CORE_ADDR addr = bp_tgt->placed_address; |
d01949b6 | 8271 | struct remote_state *rs = get_remote_state (); |
96baa820 | 8272 | |
444abaca | 8273 | if (remote_protocol_packets[PACKET_Z0].support != PACKET_DISABLE) |
96baa820 | 8274 | { |
6d820c5c | 8275 | char *p = rs->buf; |
bba74b36 | 8276 | char *endbuf = rs->buf + get_remote_packet_size (); |
802188a7 | 8277 | |
28439a30 PA |
8278 | /* Make sure the remote is pointing at the right process, if |
8279 | necessary. */ | |
8280 | if (!gdbarch_has_global_breakpoints (target_gdbarch ())) | |
8281 | set_general_process (); | |
8282 | ||
96baa820 JM |
8283 | *(p++) = 'z'; |
8284 | *(p++) = '0'; | |
8285 | *(p++) = ','; | |
8286 | ||
8181d85f DJ |
8287 | addr = (ULONGEST) remote_address_masked (bp_tgt->placed_address); |
8288 | p += hexnumstr (p, addr); | |
bba74b36 | 8289 | xsnprintf (p, endbuf - p, ",%d", bp_tgt->placed_size); |
802188a7 | 8290 | |
6d820c5c DJ |
8291 | putpkt (rs->buf); |
8292 | getpkt (&rs->buf, &rs->buf_size, 0); | |
96baa820 | 8293 | |
6d820c5c | 8294 | return (rs->buf[0] == 'E'); |
96baa820 JM |
8295 | } |
8296 | ||
a6d9a66e | 8297 | return memory_remove_breakpoint (gdbarch, bp_tgt); |
c906108c SS |
8298 | } |
8299 | ||
d471ea57 AC |
8300 | static int |
8301 | watchpoint_to_Z_packet (int type) | |
8302 | { | |
8303 | switch (type) | |
8304 | { | |
8305 | case hw_write: | |
bb858e6a | 8306 | return Z_PACKET_WRITE_WP; |
d471ea57 AC |
8307 | break; |
8308 | case hw_read: | |
bb858e6a | 8309 | return Z_PACKET_READ_WP; |
d471ea57 AC |
8310 | break; |
8311 | case hw_access: | |
bb858e6a | 8312 | return Z_PACKET_ACCESS_WP; |
d471ea57 AC |
8313 | break; |
8314 | default: | |
8e65ff28 | 8315 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 8316 | _("hw_bp_to_z: bad watchpoint type %d"), type); |
d471ea57 AC |
8317 | } |
8318 | } | |
8319 | ||
3c3bea1c | 8320 | static int |
0cf6dd15 TJB |
8321 | remote_insert_watchpoint (CORE_ADDR addr, int len, int type, |
8322 | struct expression *cond) | |
96baa820 | 8323 | { |
d01949b6 | 8324 | struct remote_state *rs = get_remote_state (); |
bba74b36 | 8325 | char *endbuf = rs->buf + get_remote_packet_size (); |
e514a9d6 | 8326 | char *p; |
d471ea57 | 8327 | enum Z_packet_type packet = watchpoint_to_Z_packet (type); |
96baa820 | 8328 | |
444abaca | 8329 | if (remote_protocol_packets[PACKET_Z0 + packet].support == PACKET_DISABLE) |
85d721b8 | 8330 | return 1; |
802188a7 | 8331 | |
28439a30 PA |
8332 | /* Make sure the remote is pointing at the right process, if |
8333 | necessary. */ | |
8334 | if (!gdbarch_has_global_breakpoints (target_gdbarch ())) | |
8335 | set_general_process (); | |
8336 | ||
bba74b36 | 8337 | xsnprintf (rs->buf, endbuf - rs->buf, "Z%x,", packet); |
6d820c5c | 8338 | p = strchr (rs->buf, '\0'); |
96baa820 JM |
8339 | addr = remote_address_masked (addr); |
8340 | p += hexnumstr (p, (ULONGEST) addr); | |
bba74b36 | 8341 | xsnprintf (p, endbuf - p, ",%x", len); |
802188a7 | 8342 | |
6d820c5c DJ |
8343 | putpkt (rs->buf); |
8344 | getpkt (&rs->buf, &rs->buf_size, 0); | |
96baa820 | 8345 | |
6d820c5c | 8346 | switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet])) |
d471ea57 AC |
8347 | { |
8348 | case PACKET_ERROR: | |
d471ea57 | 8349 | return -1; |
85d721b8 PA |
8350 | case PACKET_UNKNOWN: |
8351 | return 1; | |
d471ea57 AC |
8352 | case PACKET_OK: |
8353 | return 0; | |
8354 | } | |
8e65ff28 | 8355 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 8356 | _("remote_insert_watchpoint: reached end of function")); |
96baa820 JM |
8357 | } |
8358 | ||
283002cf MR |
8359 | static int |
8360 | remote_watchpoint_addr_within_range (struct target_ops *target, CORE_ADDR addr, | |
8361 | CORE_ADDR start, int length) | |
8362 | { | |
8363 | CORE_ADDR diff = remote_address_masked (addr - start); | |
8364 | ||
8365 | return diff < length; | |
8366 | } | |
8367 | ||
d471ea57 | 8368 | |
3c3bea1c | 8369 | static int |
0cf6dd15 TJB |
8370 | remote_remove_watchpoint (CORE_ADDR addr, int len, int type, |
8371 | struct expression *cond) | |
96baa820 | 8372 | { |
d01949b6 | 8373 | struct remote_state *rs = get_remote_state (); |
bba74b36 | 8374 | char *endbuf = rs->buf + get_remote_packet_size (); |
e514a9d6 | 8375 | char *p; |
d471ea57 AC |
8376 | enum Z_packet_type packet = watchpoint_to_Z_packet (type); |
8377 | ||
444abaca | 8378 | if (remote_protocol_packets[PACKET_Z0 + packet].support == PACKET_DISABLE) |
5cffb350 | 8379 | return -1; |
802188a7 | 8380 | |
28439a30 PA |
8381 | /* Make sure the remote is pointing at the right process, if |
8382 | necessary. */ | |
8383 | if (!gdbarch_has_global_breakpoints (target_gdbarch ())) | |
8384 | set_general_process (); | |
8385 | ||
bba74b36 | 8386 | xsnprintf (rs->buf, endbuf - rs->buf, "z%x,", packet); |
6d820c5c | 8387 | p = strchr (rs->buf, '\0'); |
96baa820 JM |
8388 | addr = remote_address_masked (addr); |
8389 | p += hexnumstr (p, (ULONGEST) addr); | |
bba74b36 | 8390 | xsnprintf (p, endbuf - p, ",%x", len); |
6d820c5c DJ |
8391 | putpkt (rs->buf); |
8392 | getpkt (&rs->buf, &rs->buf_size, 0); | |
96baa820 | 8393 | |
6d820c5c | 8394 | switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet])) |
d471ea57 AC |
8395 | { |
8396 | case PACKET_ERROR: | |
8397 | case PACKET_UNKNOWN: | |
8398 | return -1; | |
8399 | case PACKET_OK: | |
8400 | return 0; | |
8401 | } | |
8e65ff28 | 8402 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 8403 | _("remote_remove_watchpoint: reached end of function")); |
96baa820 JM |
8404 | } |
8405 | ||
3c3bea1c | 8406 | |
501eef12 | 8407 | int remote_hw_watchpoint_limit = -1; |
480a3f21 | 8408 | int remote_hw_watchpoint_length_limit = -1; |
501eef12 | 8409 | int remote_hw_breakpoint_limit = -1; |
d471ea57 | 8410 | |
480a3f21 PW |
8411 | static int |
8412 | remote_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len) | |
8413 | { | |
8414 | if (remote_hw_watchpoint_length_limit == 0) | |
8415 | return 0; | |
8416 | else if (remote_hw_watchpoint_length_limit < 0) | |
8417 | return 1; | |
8418 | else if (len <= remote_hw_watchpoint_length_limit) | |
8419 | return 1; | |
8420 | else | |
8421 | return 0; | |
8422 | } | |
8423 | ||
b9362cc7 | 8424 | static int |
3c3bea1c | 8425 | remote_check_watch_resources (int type, int cnt, int ot) |
96baa820 | 8426 | { |
3c3bea1c GS |
8427 | if (type == bp_hardware_breakpoint) |
8428 | { | |
8429 | if (remote_hw_breakpoint_limit == 0) | |
8430 | return 0; | |
501eef12 AC |
8431 | else if (remote_hw_breakpoint_limit < 0) |
8432 | return 1; | |
3c3bea1c GS |
8433 | else if (cnt <= remote_hw_breakpoint_limit) |
8434 | return 1; | |
8435 | } | |
8436 | else | |
8437 | { | |
8438 | if (remote_hw_watchpoint_limit == 0) | |
8439 | return 0; | |
501eef12 AC |
8440 | else if (remote_hw_watchpoint_limit < 0) |
8441 | return 1; | |
3c3bea1c GS |
8442 | else if (ot) |
8443 | return -1; | |
8444 | else if (cnt <= remote_hw_watchpoint_limit) | |
8445 | return 1; | |
8446 | } | |
8447 | return -1; | |
8448 | } | |
8449 | ||
b9362cc7 | 8450 | static int |
3c3bea1c GS |
8451 | remote_stopped_by_watchpoint (void) |
8452 | { | |
ee154bee TT |
8453 | struct remote_state *rs = get_remote_state (); |
8454 | ||
8455 | return rs->remote_stopped_by_watchpoint_p; | |
3c3bea1c GS |
8456 | } |
8457 | ||
4aa7a7f5 JJ |
8458 | static int |
8459 | remote_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p) | |
3c3bea1c | 8460 | { |
ee154bee | 8461 | struct remote_state *rs = get_remote_state (); |
4aa7a7f5 | 8462 | int rc = 0; |
a744cf53 | 8463 | |
d983da9c | 8464 | if (remote_stopped_by_watchpoint ()) |
4aa7a7f5 | 8465 | { |
ee154bee | 8466 | *addr_p = rs->remote_watch_data_address; |
4aa7a7f5 JJ |
8467 | rc = 1; |
8468 | } | |
8469 | ||
8470 | return rc; | |
3c3bea1c GS |
8471 | } |
8472 | ||
8473 | ||
8474 | static int | |
a6d9a66e UW |
8475 | remote_insert_hw_breakpoint (struct gdbarch *gdbarch, |
8476 | struct bp_target_info *bp_tgt) | |
3c3bea1c | 8477 | { |
8181d85f | 8478 | CORE_ADDR addr; |
4fff2411 | 8479 | struct remote_state *rs; |
bba74b36 | 8480 | char *p, *endbuf; |
dd61ec5c | 8481 | char *message; |
802188a7 | 8482 | |
c8189ed1 | 8483 | /* The length field should be set to the size of a breakpoint |
8181d85f | 8484 | instruction, even though we aren't inserting one ourselves. */ |
c8189ed1 | 8485 | |
a1dcb23a | 8486 | gdbarch_remote_breakpoint_from_pc |
a6d9a66e | 8487 | (gdbarch, &bp_tgt->placed_address, &bp_tgt->placed_size); |
3c3bea1c | 8488 | |
444abaca | 8489 | if (remote_protocol_packets[PACKET_Z1].support == PACKET_DISABLE) |
5cffb350 | 8490 | return -1; |
2bc416ba | 8491 | |
28439a30 PA |
8492 | /* Make sure the remote is pointing at the right process, if |
8493 | necessary. */ | |
8494 | if (!gdbarch_has_global_breakpoints (target_gdbarch ())) | |
8495 | set_general_process (); | |
8496 | ||
4fff2411 JZ |
8497 | rs = get_remote_state (); |
8498 | p = rs->buf; | |
bba74b36 | 8499 | endbuf = rs->buf + get_remote_packet_size (); |
4fff2411 | 8500 | |
96baa820 JM |
8501 | *(p++) = 'Z'; |
8502 | *(p++) = '1'; | |
8503 | *(p++) = ','; | |
802188a7 | 8504 | |
8181d85f | 8505 | addr = remote_address_masked (bp_tgt->placed_address); |
96baa820 | 8506 | p += hexnumstr (p, (ULONGEST) addr); |
bba74b36 | 8507 | xsnprintf (p, endbuf - p, ",%x", bp_tgt->placed_size); |
96baa820 | 8508 | |
b775012e | 8509 | if (remote_supports_cond_breakpoints ()) |
bba74b36 | 8510 | remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf); |
b775012e | 8511 | |
d3ce09f5 SS |
8512 | if (remote_can_run_breakpoint_commands ()) |
8513 | remote_add_target_side_commands (gdbarch, bp_tgt, p); | |
8514 | ||
6d820c5c DJ |
8515 | putpkt (rs->buf); |
8516 | getpkt (&rs->buf, &rs->buf_size, 0); | |
96baa820 | 8517 | |
6d820c5c | 8518 | switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1])) |
d471ea57 AC |
8519 | { |
8520 | case PACKET_ERROR: | |
dd61ec5c MW |
8521 | if (rs->buf[1] == '.') |
8522 | { | |
8523 | message = strchr (rs->buf + 2, '.'); | |
8524 | if (message) | |
0316657e | 8525 | error (_("Remote failure reply: %s"), message + 1); |
dd61ec5c MW |
8526 | } |
8527 | return -1; | |
d471ea57 AC |
8528 | case PACKET_UNKNOWN: |
8529 | return -1; | |
8530 | case PACKET_OK: | |
8531 | return 0; | |
8532 | } | |
8e65ff28 | 8533 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 8534 | _("remote_insert_hw_breakpoint: reached end of function")); |
96baa820 JM |
8535 | } |
8536 | ||
d471ea57 | 8537 | |
802188a7 | 8538 | static int |
a6d9a66e UW |
8539 | remote_remove_hw_breakpoint (struct gdbarch *gdbarch, |
8540 | struct bp_target_info *bp_tgt) | |
96baa820 | 8541 | { |
8181d85f | 8542 | CORE_ADDR addr; |
d01949b6 | 8543 | struct remote_state *rs = get_remote_state (); |
6d820c5c | 8544 | char *p = rs->buf; |
bba74b36 | 8545 | char *endbuf = rs->buf + get_remote_packet_size (); |
c8189ed1 | 8546 | |
444abaca | 8547 | if (remote_protocol_packets[PACKET_Z1].support == PACKET_DISABLE) |
5cffb350 | 8548 | return -1; |
802188a7 | 8549 | |
28439a30 PA |
8550 | /* Make sure the remote is pointing at the right process, if |
8551 | necessary. */ | |
8552 | if (!gdbarch_has_global_breakpoints (target_gdbarch ())) | |
8553 | set_general_process (); | |
8554 | ||
96baa820 JM |
8555 | *(p++) = 'z'; |
8556 | *(p++) = '1'; | |
8557 | *(p++) = ','; | |
802188a7 | 8558 | |
8181d85f | 8559 | addr = remote_address_masked (bp_tgt->placed_address); |
96baa820 | 8560 | p += hexnumstr (p, (ULONGEST) addr); |
bba74b36 | 8561 | xsnprintf (p, endbuf - p, ",%x", bp_tgt->placed_size); |
96baa820 | 8562 | |
6d820c5c DJ |
8563 | putpkt (rs->buf); |
8564 | getpkt (&rs->buf, &rs->buf_size, 0); | |
802188a7 | 8565 | |
6d820c5c | 8566 | switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1])) |
d471ea57 AC |
8567 | { |
8568 | case PACKET_ERROR: | |
8569 | case PACKET_UNKNOWN: | |
8570 | return -1; | |
8571 | case PACKET_OK: | |
8572 | return 0; | |
8573 | } | |
8e65ff28 | 8574 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 8575 | _("remote_remove_hw_breakpoint: reached end of function")); |
96baa820 | 8576 | } |
96baa820 | 8577 | |
4a5e7a5b PA |
8578 | /* Verify memory using the "qCRC:" request. */ |
8579 | ||
8580 | static int | |
8581 | remote_verify_memory (struct target_ops *ops, | |
8582 | const gdb_byte *data, CORE_ADDR lma, ULONGEST size) | |
8583 | { | |
8584 | struct remote_state *rs = get_remote_state (); | |
8585 | unsigned long host_crc, target_crc; | |
8586 | char *tmp; | |
8587 | ||
28439a30 PA |
8588 | /* Make sure the remote is pointing at the right process. */ |
8589 | set_general_process (); | |
8590 | ||
4a5e7a5b PA |
8591 | /* FIXME: assumes lma can fit into long. */ |
8592 | xsnprintf (rs->buf, get_remote_packet_size (), "qCRC:%lx,%lx", | |
8593 | (long) lma, (long) size); | |
8594 | putpkt (rs->buf); | |
8595 | ||
8596 | /* Be clever; compute the host_crc before waiting for target | |
8597 | reply. */ | |
85ec6ce7 | 8598 | host_crc = xcrc32 (data, size, 0xffffffff); |
4a5e7a5b PA |
8599 | |
8600 | getpkt (&rs->buf, &rs->buf_size, 0); | |
8601 | if (rs->buf[0] == 'E') | |
8602 | return -1; | |
8603 | ||
8604 | if (rs->buf[0] != 'C') | |
8605 | error (_("remote target does not support this operation")); | |
8606 | ||
8607 | for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++) | |
8608 | target_crc = target_crc * 16 + fromhex (*tmp); | |
8609 | ||
8610 | return (host_crc == target_crc); | |
8611 | } | |
8612 | ||
c906108c SS |
8613 | /* compare-sections command |
8614 | ||
8615 | With no arguments, compares each loadable section in the exec bfd | |
8616 | with the same memory range on the target, and reports mismatches. | |
4a5e7a5b | 8617 | Useful for verifying the image on the target against the exec file. */ |
e514a9d6 | 8618 | |
c906108c | 8619 | static void |
fba45db2 | 8620 | compare_sections_command (char *args, int from_tty) |
c906108c SS |
8621 | { |
8622 | asection *s; | |
c906108c | 8623 | struct cleanup *old_chain; |
948f8e3d | 8624 | gdb_byte *sectdata; |
ce359b09 | 8625 | const char *sectname; |
c906108c SS |
8626 | bfd_size_type size; |
8627 | bfd_vma lma; | |
8628 | int matched = 0; | |
8629 | int mismatched = 0; | |
4a5e7a5b | 8630 | int res; |
c906108c SS |
8631 | |
8632 | if (!exec_bfd) | |
8a3fe4f8 | 8633 | error (_("command cannot be used without an exec file")); |
c906108c | 8634 | |
28439a30 PA |
8635 | /* Make sure the remote is pointing at the right process. */ |
8636 | set_general_process (); | |
8637 | ||
c5aa993b | 8638 | for (s = exec_bfd->sections; s; s = s->next) |
c906108c SS |
8639 | { |
8640 | if (!(s->flags & SEC_LOAD)) | |
0df8b418 | 8641 | continue; /* Skip non-loadable section. */ |
c906108c | 8642 | |
2c500098 | 8643 | size = bfd_get_section_size (s); |
c906108c | 8644 | if (size == 0) |
0df8b418 | 8645 | continue; /* Skip zero-length section. */ |
c906108c | 8646 | |
ce359b09 | 8647 | sectname = bfd_get_section_name (exec_bfd, s); |
c906108c | 8648 | if (args && strcmp (args, sectname) != 0) |
0df8b418 | 8649 | continue; /* Not the section selected by user. */ |
c906108c | 8650 | |
0df8b418 | 8651 | matched = 1; /* Do this section. */ |
c906108c | 8652 | lma = s->lma; |
c906108c | 8653 | |
c906108c | 8654 | sectdata = xmalloc (size); |
b8c9b27d | 8655 | old_chain = make_cleanup (xfree, sectdata); |
c906108c | 8656 | bfd_get_section_contents (exec_bfd, s, sectdata, 0, size); |
c906108c | 8657 | |
4a5e7a5b PA |
8658 | res = target_verify_memory (sectdata, lma, size); |
8659 | ||
8660 | if (res == -1) | |
5af949e3 | 8661 | error (_("target memory fault, section %s, range %s -- %s"), sectname, |
f5656ead TT |
8662 | paddress (target_gdbarch (), lma), |
8663 | paddress (target_gdbarch (), lma + size)); | |
c906108c | 8664 | |
5af949e3 | 8665 | printf_filtered ("Section %s, range %s -- %s: ", sectname, |
f5656ead TT |
8666 | paddress (target_gdbarch (), lma), |
8667 | paddress (target_gdbarch (), lma + size)); | |
4a5e7a5b | 8668 | if (res) |
c906108c SS |
8669 | printf_filtered ("matched.\n"); |
8670 | else | |
c5aa993b JM |
8671 | { |
8672 | printf_filtered ("MIS-MATCHED!\n"); | |
8673 | mismatched++; | |
8674 | } | |
c906108c SS |
8675 | |
8676 | do_cleanups (old_chain); | |
8677 | } | |
8678 | if (mismatched > 0) | |
8a3fe4f8 AC |
8679 | warning (_("One or more sections of the remote executable does not match\n\ |
8680 | the loaded file\n")); | |
c906108c | 8681 | if (args && !matched) |
a3f17187 | 8682 | printf_filtered (_("No loaded section named '%s'.\n"), args); |
c906108c SS |
8683 | } |
8684 | ||
0e7f50da UW |
8685 | /* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET |
8686 | into remote target. The number of bytes written to the remote | |
8687 | target is returned, or -1 for error. */ | |
8688 | ||
8689 | static LONGEST | |
8690 | remote_write_qxfer (struct target_ops *ops, const char *object_name, | |
8691 | const char *annex, const gdb_byte *writebuf, | |
8692 | ULONGEST offset, LONGEST len, | |
8693 | struct packet_config *packet) | |
8694 | { | |
8695 | int i, buf_len; | |
8696 | ULONGEST n; | |
0e7f50da UW |
8697 | struct remote_state *rs = get_remote_state (); |
8698 | int max_size = get_memory_write_packet_size (); | |
8699 | ||
8700 | if (packet->support == PACKET_DISABLE) | |
8701 | return -1; | |
8702 | ||
8703 | /* Insert header. */ | |
8704 | i = snprintf (rs->buf, max_size, | |
8705 | "qXfer:%s:write:%s:%s:", | |
8706 | object_name, annex ? annex : "", | |
8707 | phex_nz (offset, sizeof offset)); | |
8708 | max_size -= (i + 1); | |
8709 | ||
8710 | /* Escape as much data as fits into rs->buf. */ | |
8711 | buf_len = remote_escape_output | |
bc20a4af | 8712 | (writebuf, len, (gdb_byte *) rs->buf + i, &max_size, max_size); |
0e7f50da UW |
8713 | |
8714 | if (putpkt_binary (rs->buf, i + buf_len) < 0 | |
8715 | || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0 | |
8716 | || packet_ok (rs->buf, packet) != PACKET_OK) | |
8717 | return -1; | |
8718 | ||
8719 | unpack_varlen_hex (rs->buf, &n); | |
8720 | return n; | |
8721 | } | |
8722 | ||
0876f84a DJ |
8723 | /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet. |
8724 | Data at OFFSET, of up to LEN bytes, is read into READBUF; the | |
8725 | number of bytes read is returned, or 0 for EOF, or -1 for error. | |
8726 | The number of bytes read may be less than LEN without indicating an | |
8727 | EOF. PACKET is checked and updated to indicate whether the remote | |
8728 | target supports this object. */ | |
8729 | ||
8730 | static LONGEST | |
8731 | remote_read_qxfer (struct target_ops *ops, const char *object_name, | |
8732 | const char *annex, | |
8733 | gdb_byte *readbuf, ULONGEST offset, LONGEST len, | |
8734 | struct packet_config *packet) | |
8735 | { | |
0876f84a | 8736 | struct remote_state *rs = get_remote_state (); |
0876f84a DJ |
8737 | LONGEST i, n, packet_len; |
8738 | ||
8739 | if (packet->support == PACKET_DISABLE) | |
8740 | return -1; | |
8741 | ||
8742 | /* Check whether we've cached an end-of-object packet that matches | |
8743 | this request. */ | |
8e88304f | 8744 | if (rs->finished_object) |
0876f84a | 8745 | { |
8e88304f TT |
8746 | if (strcmp (object_name, rs->finished_object) == 0 |
8747 | && strcmp (annex ? annex : "", rs->finished_annex) == 0 | |
8748 | && offset == rs->finished_offset) | |
0876f84a DJ |
8749 | return 0; |
8750 | ||
8751 | /* Otherwise, we're now reading something different. Discard | |
8752 | the cache. */ | |
8e88304f TT |
8753 | xfree (rs->finished_object); |
8754 | xfree (rs->finished_annex); | |
8755 | rs->finished_object = NULL; | |
8756 | rs->finished_annex = NULL; | |
0876f84a DJ |
8757 | } |
8758 | ||
8759 | /* Request only enough to fit in a single packet. The actual data | |
8760 | may not, since we don't know how much of it will need to be escaped; | |
8761 | the target is free to respond with slightly less data. We subtract | |
8762 | five to account for the response type and the protocol frame. */ | |
8763 | n = min (get_remote_packet_size () - 5, len); | |
8764 | snprintf (rs->buf, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s", | |
8765 | object_name, annex ? annex : "", | |
8766 | phex_nz (offset, sizeof offset), | |
8767 | phex_nz (n, sizeof n)); | |
8768 | i = putpkt (rs->buf); | |
8769 | if (i < 0) | |
8770 | return -1; | |
8771 | ||
8772 | rs->buf[0] = '\0'; | |
8773 | packet_len = getpkt_sane (&rs->buf, &rs->buf_size, 0); | |
8774 | if (packet_len < 0 || packet_ok (rs->buf, packet) != PACKET_OK) | |
8775 | return -1; | |
8776 | ||
8777 | if (rs->buf[0] != 'l' && rs->buf[0] != 'm') | |
8778 | error (_("Unknown remote qXfer reply: %s"), rs->buf); | |
8779 | ||
8780 | /* 'm' means there is (or at least might be) more data after this | |
8781 | batch. That does not make sense unless there's at least one byte | |
8782 | of data in this reply. */ | |
8783 | if (rs->buf[0] == 'm' && packet_len == 1) | |
8784 | error (_("Remote qXfer reply contained no data.")); | |
8785 | ||
8786 | /* Got some data. */ | |
bc20a4af PA |
8787 | i = remote_unescape_input ((gdb_byte *) rs->buf + 1, |
8788 | packet_len - 1, readbuf, n); | |
0876f84a DJ |
8789 | |
8790 | /* 'l' is an EOF marker, possibly including a final block of data, | |
0e7f50da UW |
8791 | or possibly empty. If we have the final block of a non-empty |
8792 | object, record this fact to bypass a subsequent partial read. */ | |
8793 | if (rs->buf[0] == 'l' && offset + i > 0) | |
0876f84a | 8794 | { |
8e88304f TT |
8795 | rs->finished_object = xstrdup (object_name); |
8796 | rs->finished_annex = xstrdup (annex ? annex : ""); | |
8797 | rs->finished_offset = offset + i; | |
0876f84a DJ |
8798 | } |
8799 | ||
8800 | return i; | |
8801 | } | |
8802 | ||
1e3ff5ad | 8803 | static LONGEST |
4b8a223f | 8804 | remote_xfer_partial (struct target_ops *ops, enum target_object object, |
961cb7b5 MK |
8805 | const char *annex, gdb_byte *readbuf, |
8806 | const gdb_byte *writebuf, ULONGEST offset, LONGEST len) | |
c906108c | 8807 | { |
82f73884 | 8808 | struct remote_state *rs; |
c906108c | 8809 | int i; |
6d820c5c | 8810 | char *p2; |
1e3ff5ad | 8811 | char query_type; |
c906108c | 8812 | |
e6e4e701 | 8813 | set_remote_traceframe (); |
82f73884 PA |
8814 | set_general_thread (inferior_ptid); |
8815 | ||
8816 | rs = get_remote_state (); | |
8817 | ||
b2182ed2 | 8818 | /* Handle memory using the standard memory routines. */ |
21e3b9b9 DJ |
8819 | if (object == TARGET_OBJECT_MEMORY) |
8820 | { | |
00d84524 | 8821 | LONGEST xfered; |
21e3b9b9 | 8822 | |
2d717e4f DJ |
8823 | /* If the remote target is connected but not running, we should |
8824 | pass this request down to a lower stratum (e.g. the executable | |
8825 | file). */ | |
8826 | if (!target_has_execution) | |
8827 | return 0; | |
8828 | ||
21e3b9b9 | 8829 | if (writebuf != NULL) |
b2182ed2 | 8830 | xfered = remote_write_bytes (offset, writebuf, len); |
21e3b9b9 | 8831 | else |
b2182ed2 | 8832 | xfered = remote_read_bytes (offset, readbuf, len); |
21e3b9b9 | 8833 | |
00d84524 | 8834 | return xfered; |
21e3b9b9 DJ |
8835 | } |
8836 | ||
0df8b418 | 8837 | /* Handle SPU memory using qxfer packets. */ |
0e7f50da UW |
8838 | if (object == TARGET_OBJECT_SPU) |
8839 | { | |
8840 | if (readbuf) | |
8841 | return remote_read_qxfer (ops, "spu", annex, readbuf, offset, len, | |
8842 | &remote_protocol_packets | |
8843 | [PACKET_qXfer_spu_read]); | |
8844 | else | |
8845 | return remote_write_qxfer (ops, "spu", annex, writebuf, offset, len, | |
8846 | &remote_protocol_packets | |
8847 | [PACKET_qXfer_spu_write]); | |
8848 | } | |
8849 | ||
4aa995e1 PA |
8850 | /* Handle extra signal info using qxfer packets. */ |
8851 | if (object == TARGET_OBJECT_SIGNAL_INFO) | |
8852 | { | |
8853 | if (readbuf) | |
8854 | return remote_read_qxfer (ops, "siginfo", annex, readbuf, offset, len, | |
8855 | &remote_protocol_packets | |
8856 | [PACKET_qXfer_siginfo_read]); | |
8857 | else | |
3e43a32a MS |
8858 | return remote_write_qxfer (ops, "siginfo", annex, |
8859 | writebuf, offset, len, | |
4aa995e1 PA |
8860 | &remote_protocol_packets |
8861 | [PACKET_qXfer_siginfo_write]); | |
8862 | } | |
8863 | ||
0fb4aa4b PA |
8864 | if (object == TARGET_OBJECT_STATIC_TRACE_DATA) |
8865 | { | |
8866 | if (readbuf) | |
3e43a32a MS |
8867 | return remote_read_qxfer (ops, "statictrace", annex, |
8868 | readbuf, offset, len, | |
0fb4aa4b PA |
8869 | &remote_protocol_packets |
8870 | [PACKET_qXfer_statictrace_read]); | |
8871 | else | |
8872 | return -1; | |
8873 | } | |
8874 | ||
a76d924d DJ |
8875 | /* Only handle flash writes. */ |
8876 | if (writebuf != NULL) | |
8877 | { | |
8878 | LONGEST xfered; | |
8879 | ||
8880 | switch (object) | |
8881 | { | |
8882 | case TARGET_OBJECT_FLASH: | |
00d84524 | 8883 | return remote_flash_write (ops, offset, len, writebuf); |
a76d924d DJ |
8884 | |
8885 | default: | |
8886 | return -1; | |
8887 | } | |
8888 | } | |
4b8a223f | 8889 | |
1e3ff5ad AC |
8890 | /* Map pre-existing objects onto letters. DO NOT do this for new |
8891 | objects!!! Instead specify new query packets. */ | |
8892 | switch (object) | |
c906108c | 8893 | { |
1e3ff5ad AC |
8894 | case TARGET_OBJECT_AVR: |
8895 | query_type = 'R'; | |
8896 | break; | |
802188a7 RM |
8897 | |
8898 | case TARGET_OBJECT_AUXV: | |
0876f84a DJ |
8899 | gdb_assert (annex == NULL); |
8900 | return remote_read_qxfer (ops, "auxv", annex, readbuf, offset, len, | |
8901 | &remote_protocol_packets[PACKET_qXfer_auxv]); | |
802188a7 | 8902 | |
23181151 DJ |
8903 | case TARGET_OBJECT_AVAILABLE_FEATURES: |
8904 | return remote_read_qxfer | |
8905 | (ops, "features", annex, readbuf, offset, len, | |
8906 | &remote_protocol_packets[PACKET_qXfer_features]); | |
8907 | ||
cfa9d6d9 DJ |
8908 | case TARGET_OBJECT_LIBRARIES: |
8909 | return remote_read_qxfer | |
8910 | (ops, "libraries", annex, readbuf, offset, len, | |
8911 | &remote_protocol_packets[PACKET_qXfer_libraries]); | |
8912 | ||
2268b414 JK |
8913 | case TARGET_OBJECT_LIBRARIES_SVR4: |
8914 | return remote_read_qxfer | |
8915 | (ops, "libraries-svr4", annex, readbuf, offset, len, | |
8916 | &remote_protocol_packets[PACKET_qXfer_libraries_svr4]); | |
8917 | ||
fd79ecee DJ |
8918 | case TARGET_OBJECT_MEMORY_MAP: |
8919 | gdb_assert (annex == NULL); | |
8920 | return remote_read_qxfer (ops, "memory-map", annex, readbuf, offset, len, | |
8921 | &remote_protocol_packets[PACKET_qXfer_memory_map]); | |
8922 | ||
07e059b5 VP |
8923 | case TARGET_OBJECT_OSDATA: |
8924 | /* Should only get here if we're connected. */ | |
5d93a237 | 8925 | gdb_assert (rs->remote_desc); |
07e059b5 VP |
8926 | return remote_read_qxfer |
8927 | (ops, "osdata", annex, readbuf, offset, len, | |
8928 | &remote_protocol_packets[PACKET_qXfer_osdata]); | |
8929 | ||
dc146f7c VP |
8930 | case TARGET_OBJECT_THREADS: |
8931 | gdb_assert (annex == NULL); | |
8932 | return remote_read_qxfer (ops, "threads", annex, readbuf, offset, len, | |
8933 | &remote_protocol_packets[PACKET_qXfer_threads]); | |
8934 | ||
b3b9301e PA |
8935 | case TARGET_OBJECT_TRACEFRAME_INFO: |
8936 | gdb_assert (annex == NULL); | |
8937 | return remote_read_qxfer | |
8938 | (ops, "traceframe-info", annex, readbuf, offset, len, | |
8939 | &remote_protocol_packets[PACKET_qXfer_traceframe_info]); | |
78d85199 YQ |
8940 | |
8941 | case TARGET_OBJECT_FDPIC: | |
8942 | return remote_read_qxfer (ops, "fdpic", annex, readbuf, offset, len, | |
8943 | &remote_protocol_packets[PACKET_qXfer_fdpic]); | |
169081d0 TG |
8944 | |
8945 | case TARGET_OBJECT_OPENVMS_UIB: | |
8946 | return remote_read_qxfer (ops, "uib", annex, readbuf, offset, len, | |
8947 | &remote_protocol_packets[PACKET_qXfer_uib]); | |
8948 | ||
9accd112 MM |
8949 | case TARGET_OBJECT_BTRACE: |
8950 | return remote_read_qxfer (ops, "btrace", annex, readbuf, offset, len, | |
8951 | &remote_protocol_packets[PACKET_qXfer_btrace]); | |
8952 | ||
1e3ff5ad | 8953 | default: |
c906108c SS |
8954 | return -1; |
8955 | } | |
8956 | ||
4b8a223f | 8957 | /* Note: a zero OFFSET and LEN can be used to query the minimum |
1e3ff5ad | 8958 | buffer size. */ |
4b8a223f | 8959 | if (offset == 0 && len == 0) |
ea9c271d | 8960 | return (get_remote_packet_size ()); |
0df8b418 | 8961 | /* Minimum outbuf size is get_remote_packet_size (). If LEN is not |
24b06219 | 8962 | large enough let the caller deal with it. */ |
ea9c271d | 8963 | if (len < get_remote_packet_size ()) |
1e3ff5ad | 8964 | return -1; |
ea9c271d | 8965 | len = get_remote_packet_size (); |
1e3ff5ad | 8966 | |
23860348 | 8967 | /* Except for querying the minimum buffer size, target must be open. */ |
5d93a237 | 8968 | if (!rs->remote_desc) |
8a3fe4f8 | 8969 | error (_("remote query is only available after target open")); |
c906108c | 8970 | |
1e3ff5ad | 8971 | gdb_assert (annex != NULL); |
4b8a223f | 8972 | gdb_assert (readbuf != NULL); |
c906108c | 8973 | |
6d820c5c | 8974 | p2 = rs->buf; |
c906108c SS |
8975 | *p2++ = 'q'; |
8976 | *p2++ = query_type; | |
8977 | ||
23860348 MS |
8978 | /* We used one buffer char for the remote protocol q command and |
8979 | another for the query type. As the remote protocol encapsulation | |
8980 | uses 4 chars plus one extra in case we are debugging | |
8981 | (remote_debug), we have PBUFZIZ - 7 left to pack the query | |
8982 | string. */ | |
c906108c | 8983 | i = 0; |
ea9c271d | 8984 | while (annex[i] && (i < (get_remote_packet_size () - 8))) |
c906108c | 8985 | { |
1e3ff5ad AC |
8986 | /* Bad caller may have sent forbidden characters. */ |
8987 | gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#'); | |
8988 | *p2++ = annex[i]; | |
c906108c SS |
8989 | i++; |
8990 | } | |
1e3ff5ad AC |
8991 | *p2 = '\0'; |
8992 | gdb_assert (annex[i] == '\0'); | |
c906108c | 8993 | |
6d820c5c | 8994 | i = putpkt (rs->buf); |
c5aa993b JM |
8995 | if (i < 0) |
8996 | return i; | |
c906108c | 8997 | |
6d820c5c DJ |
8998 | getpkt (&rs->buf, &rs->buf_size, 0); |
8999 | strcpy ((char *) readbuf, rs->buf); | |
c906108c | 9000 | |
cfd77fa1 | 9001 | return strlen ((char *) readbuf); |
c906108c SS |
9002 | } |
9003 | ||
08388c79 DE |
9004 | static int |
9005 | remote_search_memory (struct target_ops* ops, | |
9006 | CORE_ADDR start_addr, ULONGEST search_space_len, | |
9007 | const gdb_byte *pattern, ULONGEST pattern_len, | |
9008 | CORE_ADDR *found_addrp) | |
9009 | { | |
f5656ead | 9010 | int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8; |
08388c79 DE |
9011 | struct remote_state *rs = get_remote_state (); |
9012 | int max_size = get_memory_write_packet_size (); | |
9013 | struct packet_config *packet = | |
9014 | &remote_protocol_packets[PACKET_qSearch_memory]; | |
0df8b418 MS |
9015 | /* Number of packet bytes used to encode the pattern; |
9016 | this could be more than PATTERN_LEN due to escape characters. */ | |
08388c79 | 9017 | int escaped_pattern_len; |
0df8b418 | 9018 | /* Amount of pattern that was encodable in the packet. */ |
08388c79 DE |
9019 | int used_pattern_len; |
9020 | int i; | |
9021 | int found; | |
9022 | ULONGEST found_addr; | |
9023 | ||
9024 | /* Don't go to the target if we don't have to. | |
9025 | This is done before checking packet->support to avoid the possibility that | |
9026 | a success for this edge case means the facility works in general. */ | |
9027 | if (pattern_len > search_space_len) | |
9028 | return 0; | |
9029 | if (pattern_len == 0) | |
9030 | { | |
9031 | *found_addrp = start_addr; | |
9032 | return 1; | |
9033 | } | |
9034 | ||
9035 | /* If we already know the packet isn't supported, fall back to the simple | |
9036 | way of searching memory. */ | |
9037 | ||
9038 | if (packet->support == PACKET_DISABLE) | |
9039 | { | |
9040 | /* Target doesn't provided special support, fall back and use the | |
9041 | standard support (copy memory and do the search here). */ | |
9042 | return simple_search_memory (ops, start_addr, search_space_len, | |
9043 | pattern, pattern_len, found_addrp); | |
9044 | } | |
9045 | ||
28439a30 PA |
9046 | /* Make sure the remote is pointing at the right process. */ |
9047 | set_general_process (); | |
9048 | ||
08388c79 DE |
9049 | /* Insert header. */ |
9050 | i = snprintf (rs->buf, max_size, | |
9051 | "qSearch:memory:%s;%s;", | |
5af949e3 | 9052 | phex_nz (start_addr, addr_size), |
08388c79 DE |
9053 | phex_nz (search_space_len, sizeof (search_space_len))); |
9054 | max_size -= (i + 1); | |
9055 | ||
9056 | /* Escape as much data as fits into rs->buf. */ | |
9057 | escaped_pattern_len = | |
bc20a4af | 9058 | remote_escape_output (pattern, pattern_len, (gdb_byte *) rs->buf + i, |
08388c79 DE |
9059 | &used_pattern_len, max_size); |
9060 | ||
9061 | /* Bail if the pattern is too large. */ | |
9062 | if (used_pattern_len != pattern_len) | |
9b20d036 | 9063 | error (_("Pattern is too large to transmit to remote target.")); |
08388c79 DE |
9064 | |
9065 | if (putpkt_binary (rs->buf, i + escaped_pattern_len) < 0 | |
9066 | || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0 | |
9067 | || packet_ok (rs->buf, packet) != PACKET_OK) | |
9068 | { | |
9069 | /* The request may not have worked because the command is not | |
9070 | supported. If so, fall back to the simple way. */ | |
9071 | if (packet->support == PACKET_DISABLE) | |
9072 | { | |
9073 | return simple_search_memory (ops, start_addr, search_space_len, | |
9074 | pattern, pattern_len, found_addrp); | |
9075 | } | |
9076 | return -1; | |
9077 | } | |
9078 | ||
9079 | if (rs->buf[0] == '0') | |
9080 | found = 0; | |
9081 | else if (rs->buf[0] == '1') | |
9082 | { | |
9083 | found = 1; | |
9084 | if (rs->buf[1] != ',') | |
10e0fa18 | 9085 | error (_("Unknown qSearch:memory reply: %s"), rs->buf); |
08388c79 DE |
9086 | unpack_varlen_hex (rs->buf + 2, &found_addr); |
9087 | *found_addrp = found_addr; | |
9088 | } | |
9089 | else | |
10e0fa18 | 9090 | error (_("Unknown qSearch:memory reply: %s"), rs->buf); |
08388c79 DE |
9091 | |
9092 | return found; | |
9093 | } | |
9094 | ||
96baa820 JM |
9095 | static void |
9096 | remote_rcmd (char *command, | |
d9fcf2fb | 9097 | struct ui_file *outbuf) |
96baa820 | 9098 | { |
d01949b6 | 9099 | struct remote_state *rs = get_remote_state (); |
2e9f7625 | 9100 | char *p = rs->buf; |
96baa820 | 9101 | |
5d93a237 | 9102 | if (!rs->remote_desc) |
8a3fe4f8 | 9103 | error (_("remote rcmd is only available after target open")); |
96baa820 | 9104 | |
23860348 | 9105 | /* Send a NULL command across as an empty command. */ |
7be570e7 JM |
9106 | if (command == NULL) |
9107 | command = ""; | |
9108 | ||
23860348 | 9109 | /* The query prefix. */ |
2e9f7625 DJ |
9110 | strcpy (rs->buf, "qRcmd,"); |
9111 | p = strchr (rs->buf, '\0'); | |
96baa820 | 9112 | |
3e43a32a MS |
9113 | if ((strlen (rs->buf) + strlen (command) * 2 + 8/*misc*/) |
9114 | > get_remote_packet_size ()) | |
8a3fe4f8 | 9115 | error (_("\"monitor\" command ``%s'' is too long."), command); |
96baa820 | 9116 | |
23860348 | 9117 | /* Encode the actual command. */ |
cfd77fa1 | 9118 | bin2hex ((gdb_byte *) command, p, 0); |
96baa820 | 9119 | |
6d820c5c | 9120 | if (putpkt (rs->buf) < 0) |
8a3fe4f8 | 9121 | error (_("Communication problem with target.")); |
96baa820 JM |
9122 | |
9123 | /* get/display the response */ | |
9124 | while (1) | |
9125 | { | |
2e9f7625 DJ |
9126 | char *buf; |
9127 | ||
00bf0b85 | 9128 | /* XXX - see also remote_get_noisy_reply(). */ |
5b37825d | 9129 | QUIT; /* Allow user to bail out with ^C. */ |
2e9f7625 | 9130 | rs->buf[0] = '\0'; |
5b37825d PW |
9131 | if (getpkt_sane (&rs->buf, &rs->buf_size, 0) == -1) |
9132 | { | |
9133 | /* Timeout. Continue to (try to) read responses. | |
9134 | This is better than stopping with an error, assuming the stub | |
9135 | is still executing the (long) monitor command. | |
9136 | If needed, the user can interrupt gdb using C-c, obtaining | |
9137 | an effect similar to stop on timeout. */ | |
9138 | continue; | |
9139 | } | |
2e9f7625 | 9140 | buf = rs->buf; |
96baa820 | 9141 | if (buf[0] == '\0') |
8a3fe4f8 | 9142 | error (_("Target does not support this command.")); |
96baa820 JM |
9143 | if (buf[0] == 'O' && buf[1] != 'K') |
9144 | { | |
23860348 | 9145 | remote_console_output (buf + 1); /* 'O' message from stub. */ |
96baa820 JM |
9146 | continue; |
9147 | } | |
9148 | if (strcmp (buf, "OK") == 0) | |
9149 | break; | |
7be570e7 JM |
9150 | if (strlen (buf) == 3 && buf[0] == 'E' |
9151 | && isdigit (buf[1]) && isdigit (buf[2])) | |
9152 | { | |
8a3fe4f8 | 9153 | error (_("Protocol error with Rcmd")); |
7be570e7 | 9154 | } |
96baa820 JM |
9155 | for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2) |
9156 | { | |
9157 | char c = (fromhex (p[0]) << 4) + fromhex (p[1]); | |
a744cf53 | 9158 | |
96baa820 JM |
9159 | fputc_unfiltered (c, outbuf); |
9160 | } | |
9161 | break; | |
9162 | } | |
9163 | } | |
9164 | ||
fd79ecee DJ |
9165 | static VEC(mem_region_s) * |
9166 | remote_memory_map (struct target_ops *ops) | |
9167 | { | |
9168 | VEC(mem_region_s) *result = NULL; | |
9169 | char *text = target_read_stralloc (¤t_target, | |
9170 | TARGET_OBJECT_MEMORY_MAP, NULL); | |
9171 | ||
9172 | if (text) | |
9173 | { | |
9174 | struct cleanup *back_to = make_cleanup (xfree, text); | |
a744cf53 | 9175 | |
fd79ecee DJ |
9176 | result = parse_memory_map (text); |
9177 | do_cleanups (back_to); | |
9178 | } | |
9179 | ||
9180 | return result; | |
9181 | } | |
9182 | ||
c906108c | 9183 | static void |
fba45db2 | 9184 | packet_command (char *args, int from_tty) |
c906108c | 9185 | { |
d01949b6 | 9186 | struct remote_state *rs = get_remote_state (); |
c906108c | 9187 | |
5d93a237 | 9188 | if (!rs->remote_desc) |
8a3fe4f8 | 9189 | error (_("command can only be used with remote target")); |
c906108c | 9190 | |
c5aa993b | 9191 | if (!args) |
8a3fe4f8 | 9192 | error (_("remote-packet command requires packet text as argument")); |
c906108c SS |
9193 | |
9194 | puts_filtered ("sending: "); | |
9195 | print_packet (args); | |
9196 | puts_filtered ("\n"); | |
9197 | putpkt (args); | |
9198 | ||
6d820c5c | 9199 | getpkt (&rs->buf, &rs->buf_size, 0); |
c906108c | 9200 | puts_filtered ("received: "); |
6d820c5c | 9201 | print_packet (rs->buf); |
c906108c SS |
9202 | puts_filtered ("\n"); |
9203 | } | |
9204 | ||
9205 | #if 0 | |
23860348 | 9206 | /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */ |
c906108c | 9207 | |
a14ed312 | 9208 | static void display_thread_info (struct gdb_ext_thread_info *info); |
c906108c | 9209 | |
a14ed312 | 9210 | static void threadset_test_cmd (char *cmd, int tty); |
c906108c | 9211 | |
a14ed312 | 9212 | static void threadalive_test (char *cmd, int tty); |
c906108c | 9213 | |
a14ed312 | 9214 | static void threadlist_test_cmd (char *cmd, int tty); |
c906108c | 9215 | |
23860348 | 9216 | int get_and_display_threadinfo (threadref *ref); |
c906108c | 9217 | |
a14ed312 | 9218 | static void threadinfo_test_cmd (char *cmd, int tty); |
c906108c | 9219 | |
23860348 | 9220 | static int thread_display_step (threadref *ref, void *context); |
c906108c | 9221 | |
a14ed312 | 9222 | static void threadlist_update_test_cmd (char *cmd, int tty); |
c906108c | 9223 | |
a14ed312 | 9224 | static void init_remote_threadtests (void); |
c906108c | 9225 | |
23860348 | 9226 | #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */ |
c906108c SS |
9227 | |
9228 | static void | |
fba45db2 | 9229 | threadset_test_cmd (char *cmd, int tty) |
c906108c SS |
9230 | { |
9231 | int sample_thread = SAMPLE_THREAD; | |
9232 | ||
a3f17187 | 9233 | printf_filtered (_("Remote threadset test\n")); |
79d7f229 | 9234 | set_general_thread (sample_thread); |
c906108c SS |
9235 | } |
9236 | ||
9237 | ||
9238 | static void | |
fba45db2 | 9239 | threadalive_test (char *cmd, int tty) |
c906108c SS |
9240 | { |
9241 | int sample_thread = SAMPLE_THREAD; | |
79d7f229 PA |
9242 | int pid = ptid_get_pid (inferior_ptid); |
9243 | ptid_t ptid = ptid_build (pid, 0, sample_thread); | |
c906108c | 9244 | |
79d7f229 | 9245 | if (remote_thread_alive (ptid)) |
c906108c SS |
9246 | printf_filtered ("PASS: Thread alive test\n"); |
9247 | else | |
9248 | printf_filtered ("FAIL: Thread alive test\n"); | |
9249 | } | |
9250 | ||
23860348 | 9251 | void output_threadid (char *title, threadref *ref); |
c906108c SS |
9252 | |
9253 | void | |
fba45db2 | 9254 | output_threadid (char *title, threadref *ref) |
c906108c SS |
9255 | { |
9256 | char hexid[20]; | |
9257 | ||
23860348 | 9258 | pack_threadid (&hexid[0], ref); /* Convert threead id into hex. */ |
c906108c SS |
9259 | hexid[16] = 0; |
9260 | printf_filtered ("%s %s\n", title, (&hexid[0])); | |
9261 | } | |
9262 | ||
9263 | static void | |
fba45db2 | 9264 | threadlist_test_cmd (char *cmd, int tty) |
c906108c SS |
9265 | { |
9266 | int startflag = 1; | |
9267 | threadref nextthread; | |
9268 | int done, result_count; | |
9269 | threadref threadlist[3]; | |
9270 | ||
9271 | printf_filtered ("Remote Threadlist test\n"); | |
9272 | if (!remote_get_threadlist (startflag, &nextthread, 3, &done, | |
9273 | &result_count, &threadlist[0])) | |
9274 | printf_filtered ("FAIL: threadlist test\n"); | |
9275 | else | |
9276 | { | |
9277 | threadref *scan = threadlist; | |
9278 | threadref *limit = scan + result_count; | |
9279 | ||
9280 | while (scan < limit) | |
9281 | output_threadid (" thread ", scan++); | |
9282 | } | |
9283 | } | |
9284 | ||
9285 | void | |
fba45db2 | 9286 | display_thread_info (struct gdb_ext_thread_info *info) |
c906108c SS |
9287 | { |
9288 | output_threadid ("Threadid: ", &info->threadid); | |
9289 | printf_filtered ("Name: %s\n ", info->shortname); | |
9290 | printf_filtered ("State: %s\n", info->display); | |
9291 | printf_filtered ("other: %s\n\n", info->more_display); | |
9292 | } | |
9293 | ||
9294 | int | |
fba45db2 | 9295 | get_and_display_threadinfo (threadref *ref) |
c906108c SS |
9296 | { |
9297 | int result; | |
9298 | int set; | |
9299 | struct gdb_ext_thread_info threadinfo; | |
9300 | ||
9301 | set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME | |
9302 | | TAG_MOREDISPLAY | TAG_DISPLAY; | |
9303 | if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo))) | |
9304 | display_thread_info (&threadinfo); | |
9305 | return result; | |
9306 | } | |
9307 | ||
9308 | static void | |
fba45db2 | 9309 | threadinfo_test_cmd (char *cmd, int tty) |
c906108c SS |
9310 | { |
9311 | int athread = SAMPLE_THREAD; | |
9312 | threadref thread; | |
9313 | int set; | |
9314 | ||
9315 | int_to_threadref (&thread, athread); | |
9316 | printf_filtered ("Remote Threadinfo test\n"); | |
9317 | if (!get_and_display_threadinfo (&thread)) | |
9318 | printf_filtered ("FAIL cannot get thread info\n"); | |
9319 | } | |
9320 | ||
9321 | static int | |
fba45db2 | 9322 | thread_display_step (threadref *ref, void *context) |
c906108c SS |
9323 | { |
9324 | /* output_threadid(" threadstep ",ref); *//* simple test */ | |
9325 | return get_and_display_threadinfo (ref); | |
9326 | } | |
9327 | ||
9328 | static void | |
fba45db2 | 9329 | threadlist_update_test_cmd (char *cmd, int tty) |
c906108c SS |
9330 | { |
9331 | printf_filtered ("Remote Threadlist update test\n"); | |
9332 | remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS); | |
9333 | } | |
9334 | ||
9335 | static void | |
9336 | init_remote_threadtests (void) | |
9337 | { | |
3e43a32a MS |
9338 | add_com ("tlist", class_obscure, threadlist_test_cmd, |
9339 | _("Fetch and print the remote list of " | |
9340 | "thread identifiers, one pkt only")); | |
c906108c | 9341 | add_com ("tinfo", class_obscure, threadinfo_test_cmd, |
1bedd215 | 9342 | _("Fetch and display info about one thread")); |
c906108c | 9343 | add_com ("tset", class_obscure, threadset_test_cmd, |
1bedd215 | 9344 | _("Test setting to a different thread")); |
c906108c | 9345 | add_com ("tupd", class_obscure, threadlist_update_test_cmd, |
1bedd215 | 9346 | _("Iterate through updating all remote thread info")); |
c906108c | 9347 | add_com ("talive", class_obscure, threadalive_test, |
1bedd215 | 9348 | _(" Remote thread alive test ")); |
c906108c SS |
9349 | } |
9350 | ||
9351 | #endif /* 0 */ | |
9352 | ||
f3fb8c85 MS |
9353 | /* Convert a thread ID to a string. Returns the string in a static |
9354 | buffer. */ | |
9355 | ||
9356 | static char * | |
117de6a9 | 9357 | remote_pid_to_str (struct target_ops *ops, ptid_t ptid) |
f3fb8c85 | 9358 | { |
79d7f229 | 9359 | static char buf[64]; |
82f73884 | 9360 | struct remote_state *rs = get_remote_state (); |
f3fb8c85 | 9361 | |
7cee1e54 PA |
9362 | if (ptid_equal (ptid, null_ptid)) |
9363 | return normal_pid_to_str (ptid); | |
9364 | else if (ptid_is_pid (ptid)) | |
ecd0ada5 PA |
9365 | { |
9366 | /* Printing an inferior target id. */ | |
9367 | ||
9368 | /* When multi-process extensions are off, there's no way in the | |
9369 | remote protocol to know the remote process id, if there's any | |
9370 | at all. There's one exception --- when we're connected with | |
9371 | target extended-remote, and we manually attached to a process | |
9372 | with "attach PID". We don't record anywhere a flag that | |
9373 | allows us to distinguish that case from the case of | |
9374 | connecting with extended-remote and the stub already being | |
9375 | attached to a process, and reporting yes to qAttached, hence | |
9376 | no smart special casing here. */ | |
9377 | if (!remote_multi_process_p (rs)) | |
9378 | { | |
9379 | xsnprintf (buf, sizeof buf, "Remote target"); | |
9380 | return buf; | |
9381 | } | |
9382 | ||
9383 | return normal_pid_to_str (ptid); | |
82f73884 | 9384 | } |
ecd0ada5 | 9385 | else |
79d7f229 | 9386 | { |
ecd0ada5 PA |
9387 | if (ptid_equal (magic_null_ptid, ptid)) |
9388 | xsnprintf (buf, sizeof buf, "Thread <main>"); | |
901f9912 | 9389 | else if (rs->extended && remote_multi_process_p (rs)) |
ecd0ada5 PA |
9390 | xsnprintf (buf, sizeof buf, "Thread %d.%ld", |
9391 | ptid_get_pid (ptid), ptid_get_tid (ptid)); | |
9392 | else | |
9393 | xsnprintf (buf, sizeof buf, "Thread %ld", | |
9394 | ptid_get_tid (ptid)); | |
79d7f229 PA |
9395 | return buf; |
9396 | } | |
f3fb8c85 MS |
9397 | } |
9398 | ||
38691318 KB |
9399 | /* Get the address of the thread local variable in OBJFILE which is |
9400 | stored at OFFSET within the thread local storage for thread PTID. */ | |
9401 | ||
9402 | static CORE_ADDR | |
117de6a9 PA |
9403 | remote_get_thread_local_address (struct target_ops *ops, |
9404 | ptid_t ptid, CORE_ADDR lm, CORE_ADDR offset) | |
38691318 | 9405 | { |
444abaca | 9406 | if (remote_protocol_packets[PACKET_qGetTLSAddr].support != PACKET_DISABLE) |
38691318 KB |
9407 | { |
9408 | struct remote_state *rs = get_remote_state (); | |
6d820c5c | 9409 | char *p = rs->buf; |
82f73884 | 9410 | char *endp = rs->buf + get_remote_packet_size (); |
571dd617 | 9411 | enum packet_result result; |
38691318 KB |
9412 | |
9413 | strcpy (p, "qGetTLSAddr:"); | |
9414 | p += strlen (p); | |
82f73884 | 9415 | p = write_ptid (p, endp, ptid); |
38691318 KB |
9416 | *p++ = ','; |
9417 | p += hexnumstr (p, offset); | |
9418 | *p++ = ','; | |
9419 | p += hexnumstr (p, lm); | |
9420 | *p++ = '\0'; | |
9421 | ||
6d820c5c DJ |
9422 | putpkt (rs->buf); |
9423 | getpkt (&rs->buf, &rs->buf_size, 0); | |
3e43a32a MS |
9424 | result = packet_ok (rs->buf, |
9425 | &remote_protocol_packets[PACKET_qGetTLSAddr]); | |
571dd617 | 9426 | if (result == PACKET_OK) |
38691318 KB |
9427 | { |
9428 | ULONGEST result; | |
9429 | ||
6d820c5c | 9430 | unpack_varlen_hex (rs->buf, &result); |
38691318 KB |
9431 | return result; |
9432 | } | |
571dd617 | 9433 | else if (result == PACKET_UNKNOWN) |
109c3e39 AC |
9434 | throw_error (TLS_GENERIC_ERROR, |
9435 | _("Remote target doesn't support qGetTLSAddr packet")); | |
38691318 | 9436 | else |
109c3e39 AC |
9437 | throw_error (TLS_GENERIC_ERROR, |
9438 | _("Remote target failed to process qGetTLSAddr request")); | |
38691318 KB |
9439 | } |
9440 | else | |
109c3e39 AC |
9441 | throw_error (TLS_GENERIC_ERROR, |
9442 | _("TLS not supported or disabled on this target")); | |
38691318 KB |
9443 | /* Not reached. */ |
9444 | return 0; | |
9445 | } | |
9446 | ||
711e434b PM |
9447 | /* Provide thread local base, i.e. Thread Information Block address. |
9448 | Returns 1 if ptid is found and thread_local_base is non zero. */ | |
9449 | ||
70221824 | 9450 | static int |
711e434b PM |
9451 | remote_get_tib_address (ptid_t ptid, CORE_ADDR *addr) |
9452 | { | |
9453 | if (remote_protocol_packets[PACKET_qGetTIBAddr].support != PACKET_DISABLE) | |
9454 | { | |
9455 | struct remote_state *rs = get_remote_state (); | |
9456 | char *p = rs->buf; | |
9457 | char *endp = rs->buf + get_remote_packet_size (); | |
9458 | enum packet_result result; | |
9459 | ||
9460 | strcpy (p, "qGetTIBAddr:"); | |
9461 | p += strlen (p); | |
9462 | p = write_ptid (p, endp, ptid); | |
9463 | *p++ = '\0'; | |
9464 | ||
9465 | putpkt (rs->buf); | |
9466 | getpkt (&rs->buf, &rs->buf_size, 0); | |
9467 | result = packet_ok (rs->buf, | |
9468 | &remote_protocol_packets[PACKET_qGetTIBAddr]); | |
9469 | if (result == PACKET_OK) | |
9470 | { | |
9471 | ULONGEST result; | |
9472 | ||
9473 | unpack_varlen_hex (rs->buf, &result); | |
9474 | if (addr) | |
9475 | *addr = (CORE_ADDR) result; | |
9476 | return 1; | |
9477 | } | |
9478 | else if (result == PACKET_UNKNOWN) | |
9479 | error (_("Remote target doesn't support qGetTIBAddr packet")); | |
9480 | else | |
9481 | error (_("Remote target failed to process qGetTIBAddr request")); | |
9482 | } | |
9483 | else | |
9484 | error (_("qGetTIBAddr not supported or disabled on this target")); | |
9485 | /* Not reached. */ | |
9486 | return 0; | |
9487 | } | |
9488 | ||
29709017 DJ |
9489 | /* Support for inferring a target description based on the current |
9490 | architecture and the size of a 'g' packet. While the 'g' packet | |
9491 | can have any size (since optional registers can be left off the | |
9492 | end), some sizes are easily recognizable given knowledge of the | |
9493 | approximate architecture. */ | |
9494 | ||
9495 | struct remote_g_packet_guess | |
9496 | { | |
9497 | int bytes; | |
9498 | const struct target_desc *tdesc; | |
9499 | }; | |
9500 | typedef struct remote_g_packet_guess remote_g_packet_guess_s; | |
9501 | DEF_VEC_O(remote_g_packet_guess_s); | |
9502 | ||
9503 | struct remote_g_packet_data | |
9504 | { | |
9505 | VEC(remote_g_packet_guess_s) *guesses; | |
9506 | }; | |
9507 | ||
9508 | static struct gdbarch_data *remote_g_packet_data_handle; | |
9509 | ||
9510 | static void * | |
9511 | remote_g_packet_data_init (struct obstack *obstack) | |
9512 | { | |
9513 | return OBSTACK_ZALLOC (obstack, struct remote_g_packet_data); | |
9514 | } | |
9515 | ||
9516 | void | |
9517 | register_remote_g_packet_guess (struct gdbarch *gdbarch, int bytes, | |
9518 | const struct target_desc *tdesc) | |
9519 | { | |
9520 | struct remote_g_packet_data *data | |
9521 | = gdbarch_data (gdbarch, remote_g_packet_data_handle); | |
9522 | struct remote_g_packet_guess new_guess, *guess; | |
9523 | int ix; | |
9524 | ||
9525 | gdb_assert (tdesc != NULL); | |
9526 | ||
9527 | for (ix = 0; | |
9528 | VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess); | |
9529 | ix++) | |
9530 | if (guess->bytes == bytes) | |
9531 | internal_error (__FILE__, __LINE__, | |
9b20d036 | 9532 | _("Duplicate g packet description added for size %d"), |
29709017 DJ |
9533 | bytes); |
9534 | ||
9535 | new_guess.bytes = bytes; | |
9536 | new_guess.tdesc = tdesc; | |
9537 | VEC_safe_push (remote_g_packet_guess_s, data->guesses, &new_guess); | |
9538 | } | |
9539 | ||
d962ef82 DJ |
9540 | /* Return 1 if remote_read_description would do anything on this target |
9541 | and architecture, 0 otherwise. */ | |
9542 | ||
9543 | static int | |
9544 | remote_read_description_p (struct target_ops *target) | |
9545 | { | |
9546 | struct remote_g_packet_data *data | |
f5656ead | 9547 | = gdbarch_data (target_gdbarch (), remote_g_packet_data_handle); |
d962ef82 DJ |
9548 | |
9549 | if (!VEC_empty (remote_g_packet_guess_s, data->guesses)) | |
9550 | return 1; | |
9551 | ||
9552 | return 0; | |
9553 | } | |
9554 | ||
29709017 DJ |
9555 | static const struct target_desc * |
9556 | remote_read_description (struct target_ops *target) | |
9557 | { | |
9558 | struct remote_g_packet_data *data | |
f5656ead | 9559 | = gdbarch_data (target_gdbarch (), remote_g_packet_data_handle); |
29709017 | 9560 | |
d962ef82 DJ |
9561 | /* Do not try this during initial connection, when we do not know |
9562 | whether there is a running but stopped thread. */ | |
9563 | if (!target_has_execution || ptid_equal (inferior_ptid, null_ptid)) | |
9564 | return NULL; | |
9565 | ||
29709017 DJ |
9566 | if (!VEC_empty (remote_g_packet_guess_s, data->guesses)) |
9567 | { | |
9568 | struct remote_g_packet_guess *guess; | |
9569 | int ix; | |
9570 | int bytes = send_g_packet (); | |
9571 | ||
9572 | for (ix = 0; | |
9573 | VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess); | |
9574 | ix++) | |
9575 | if (guess->bytes == bytes) | |
9576 | return guess->tdesc; | |
9577 | ||
9578 | /* We discard the g packet. A minor optimization would be to | |
9579 | hold on to it, and fill the register cache once we have selected | |
9580 | an architecture, but it's too tricky to do safely. */ | |
9581 | } | |
9582 | ||
9583 | return NULL; | |
9584 | } | |
9585 | ||
a6b151f1 DJ |
9586 | /* Remote file transfer support. This is host-initiated I/O, not |
9587 | target-initiated; for target-initiated, see remote-fileio.c. */ | |
9588 | ||
9589 | /* If *LEFT is at least the length of STRING, copy STRING to | |
9590 | *BUFFER, update *BUFFER to point to the new end of the buffer, and | |
9591 | decrease *LEFT. Otherwise raise an error. */ | |
9592 | ||
9593 | static void | |
9594 | remote_buffer_add_string (char **buffer, int *left, char *string) | |
9595 | { | |
9596 | int len = strlen (string); | |
9597 | ||
9598 | if (len > *left) | |
9599 | error (_("Packet too long for target.")); | |
9600 | ||
9601 | memcpy (*buffer, string, len); | |
9602 | *buffer += len; | |
9603 | *left -= len; | |
9604 | ||
9605 | /* NUL-terminate the buffer as a convenience, if there is | |
9606 | room. */ | |
9607 | if (*left) | |
9608 | **buffer = '\0'; | |
9609 | } | |
9610 | ||
9611 | /* If *LEFT is large enough, hex encode LEN bytes from BYTES into | |
9612 | *BUFFER, update *BUFFER to point to the new end of the buffer, and | |
9613 | decrease *LEFT. Otherwise raise an error. */ | |
9614 | ||
9615 | static void | |
9616 | remote_buffer_add_bytes (char **buffer, int *left, const gdb_byte *bytes, | |
9617 | int len) | |
9618 | { | |
9619 | if (2 * len > *left) | |
9620 | error (_("Packet too long for target.")); | |
9621 | ||
9622 | bin2hex (bytes, *buffer, len); | |
9623 | *buffer += 2 * len; | |
9624 | *left -= 2 * len; | |
9625 | ||
9626 | /* NUL-terminate the buffer as a convenience, if there is | |
9627 | room. */ | |
9628 | if (*left) | |
9629 | **buffer = '\0'; | |
9630 | } | |
9631 | ||
9632 | /* If *LEFT is large enough, convert VALUE to hex and add it to | |
9633 | *BUFFER, update *BUFFER to point to the new end of the buffer, and | |
9634 | decrease *LEFT. Otherwise raise an error. */ | |
9635 | ||
9636 | static void | |
9637 | remote_buffer_add_int (char **buffer, int *left, ULONGEST value) | |
9638 | { | |
9639 | int len = hexnumlen (value); | |
9640 | ||
9641 | if (len > *left) | |
9642 | error (_("Packet too long for target.")); | |
9643 | ||
9644 | hexnumstr (*buffer, value); | |
9645 | *buffer += len; | |
9646 | *left -= len; | |
9647 | ||
9648 | /* NUL-terminate the buffer as a convenience, if there is | |
9649 | room. */ | |
9650 | if (*left) | |
9651 | **buffer = '\0'; | |
9652 | } | |
9653 | ||
9654 | /* Parse an I/O result packet from BUFFER. Set RETCODE to the return | |
9655 | value, *REMOTE_ERRNO to the remote error number or zero if none | |
9656 | was included, and *ATTACHMENT to point to the start of the annex | |
9657 | if any. The length of the packet isn't needed here; there may | |
9658 | be NUL bytes in BUFFER, but they will be after *ATTACHMENT. | |
9659 | ||
9660 | Return 0 if the packet could be parsed, -1 if it could not. If | |
9661 | -1 is returned, the other variables may not be initialized. */ | |
9662 | ||
9663 | static int | |
9664 | remote_hostio_parse_result (char *buffer, int *retcode, | |
9665 | int *remote_errno, char **attachment) | |
9666 | { | |
9667 | char *p, *p2; | |
9668 | ||
9669 | *remote_errno = 0; | |
9670 | *attachment = NULL; | |
9671 | ||
9672 | if (buffer[0] != 'F') | |
9673 | return -1; | |
9674 | ||
9675 | errno = 0; | |
9676 | *retcode = strtol (&buffer[1], &p, 16); | |
9677 | if (errno != 0 || p == &buffer[1]) | |
9678 | return -1; | |
9679 | ||
9680 | /* Check for ",errno". */ | |
9681 | if (*p == ',') | |
9682 | { | |
9683 | errno = 0; | |
9684 | *remote_errno = strtol (p + 1, &p2, 16); | |
9685 | if (errno != 0 || p + 1 == p2) | |
9686 | return -1; | |
9687 | p = p2; | |
9688 | } | |
9689 | ||
9690 | /* Check for ";attachment". If there is no attachment, the | |
9691 | packet should end here. */ | |
9692 | if (*p == ';') | |
9693 | { | |
9694 | *attachment = p + 1; | |
9695 | return 0; | |
9696 | } | |
9697 | else if (*p == '\0') | |
9698 | return 0; | |
9699 | else | |
9700 | return -1; | |
9701 | } | |
9702 | ||
9703 | /* Send a prepared I/O packet to the target and read its response. | |
9704 | The prepared packet is in the global RS->BUF before this function | |
9705 | is called, and the answer is there when we return. | |
9706 | ||
9707 | COMMAND_BYTES is the length of the request to send, which may include | |
9708 | binary data. WHICH_PACKET is the packet configuration to check | |
9709 | before attempting a packet. If an error occurs, *REMOTE_ERRNO | |
9710 | is set to the error number and -1 is returned. Otherwise the value | |
9711 | returned by the function is returned. | |
9712 | ||
9713 | ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an | |
9714 | attachment is expected; an error will be reported if there's a | |
9715 | mismatch. If one is found, *ATTACHMENT will be set to point into | |
9716 | the packet buffer and *ATTACHMENT_LEN will be set to the | |
9717 | attachment's length. */ | |
9718 | ||
9719 | static int | |
9720 | remote_hostio_send_command (int command_bytes, int which_packet, | |
9721 | int *remote_errno, char **attachment, | |
9722 | int *attachment_len) | |
9723 | { | |
9724 | struct remote_state *rs = get_remote_state (); | |
9725 | int ret, bytes_read; | |
9726 | char *attachment_tmp; | |
9727 | ||
5d93a237 | 9728 | if (!rs->remote_desc |
f1838a98 | 9729 | || remote_protocol_packets[which_packet].support == PACKET_DISABLE) |
a6b151f1 DJ |
9730 | { |
9731 | *remote_errno = FILEIO_ENOSYS; | |
9732 | return -1; | |
9733 | } | |
9734 | ||
9735 | putpkt_binary (rs->buf, command_bytes); | |
9736 | bytes_read = getpkt_sane (&rs->buf, &rs->buf_size, 0); | |
9737 | ||
9738 | /* If it timed out, something is wrong. Don't try to parse the | |
9739 | buffer. */ | |
9740 | if (bytes_read < 0) | |
9741 | { | |
9742 | *remote_errno = FILEIO_EINVAL; | |
9743 | return -1; | |
9744 | } | |
9745 | ||
9746 | switch (packet_ok (rs->buf, &remote_protocol_packets[which_packet])) | |
9747 | { | |
9748 | case PACKET_ERROR: | |
9749 | *remote_errno = FILEIO_EINVAL; | |
9750 | return -1; | |
9751 | case PACKET_UNKNOWN: | |
9752 | *remote_errno = FILEIO_ENOSYS; | |
9753 | return -1; | |
9754 | case PACKET_OK: | |
9755 | break; | |
9756 | } | |
9757 | ||
9758 | if (remote_hostio_parse_result (rs->buf, &ret, remote_errno, | |
9759 | &attachment_tmp)) | |
9760 | { | |
9761 | *remote_errno = FILEIO_EINVAL; | |
9762 | return -1; | |
9763 | } | |
9764 | ||
9765 | /* Make sure we saw an attachment if and only if we expected one. */ | |
9766 | if ((attachment_tmp == NULL && attachment != NULL) | |
9767 | || (attachment_tmp != NULL && attachment == NULL)) | |
9768 | { | |
9769 | *remote_errno = FILEIO_EINVAL; | |
9770 | return -1; | |
9771 | } | |
9772 | ||
9773 | /* If an attachment was found, it must point into the packet buffer; | |
9774 | work out how many bytes there were. */ | |
9775 | if (attachment_tmp != NULL) | |
9776 | { | |
9777 | *attachment = attachment_tmp; | |
9778 | *attachment_len = bytes_read - (*attachment - rs->buf); | |
9779 | } | |
9780 | ||
9781 | return ret; | |
9782 | } | |
9783 | ||
9784 | /* Open FILENAME on the remote target, using FLAGS and MODE. Return a | |
9785 | remote file descriptor, or -1 if an error occurs (and set | |
9786 | *REMOTE_ERRNO). */ | |
9787 | ||
9788 | static int | |
9789 | remote_hostio_open (const char *filename, int flags, int mode, | |
9790 | int *remote_errno) | |
9791 | { | |
9792 | struct remote_state *rs = get_remote_state (); | |
9793 | char *p = rs->buf; | |
9794 | int left = get_remote_packet_size () - 1; | |
9795 | ||
9796 | remote_buffer_add_string (&p, &left, "vFile:open:"); | |
9797 | ||
9798 | remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename, | |
9799 | strlen (filename)); | |
9800 | remote_buffer_add_string (&p, &left, ","); | |
9801 | ||
9802 | remote_buffer_add_int (&p, &left, flags); | |
9803 | remote_buffer_add_string (&p, &left, ","); | |
9804 | ||
9805 | remote_buffer_add_int (&p, &left, mode); | |
9806 | ||
9807 | return remote_hostio_send_command (p - rs->buf, PACKET_vFile_open, | |
9808 | remote_errno, NULL, NULL); | |
9809 | } | |
9810 | ||
9811 | /* Write up to LEN bytes from WRITE_BUF to FD on the remote target. | |
9812 | Return the number of bytes written, or -1 if an error occurs (and | |
9813 | set *REMOTE_ERRNO). */ | |
9814 | ||
9815 | static int | |
9816 | remote_hostio_pwrite (int fd, const gdb_byte *write_buf, int len, | |
9817 | ULONGEST offset, int *remote_errno) | |
9818 | { | |
9819 | struct remote_state *rs = get_remote_state (); | |
9820 | char *p = rs->buf; | |
9821 | int left = get_remote_packet_size (); | |
9822 | int out_len; | |
9823 | ||
9824 | remote_buffer_add_string (&p, &left, "vFile:pwrite:"); | |
9825 | ||
9826 | remote_buffer_add_int (&p, &left, fd); | |
9827 | remote_buffer_add_string (&p, &left, ","); | |
9828 | ||
9829 | remote_buffer_add_int (&p, &left, offset); | |
9830 | remote_buffer_add_string (&p, &left, ","); | |
9831 | ||
bc20a4af | 9832 | p += remote_escape_output (write_buf, len, (gdb_byte *) p, &out_len, |
a6b151f1 DJ |
9833 | get_remote_packet_size () - (p - rs->buf)); |
9834 | ||
9835 | return remote_hostio_send_command (p - rs->buf, PACKET_vFile_pwrite, | |
9836 | remote_errno, NULL, NULL); | |
9837 | } | |
9838 | ||
9839 | /* Read up to LEN bytes FD on the remote target into READ_BUF | |
9840 | Return the number of bytes read, or -1 if an error occurs (and | |
9841 | set *REMOTE_ERRNO). */ | |
9842 | ||
9843 | static int | |
9844 | remote_hostio_pread (int fd, gdb_byte *read_buf, int len, | |
9845 | ULONGEST offset, int *remote_errno) | |
9846 | { | |
9847 | struct remote_state *rs = get_remote_state (); | |
9848 | char *p = rs->buf; | |
9849 | char *attachment; | |
9850 | int left = get_remote_packet_size (); | |
9851 | int ret, attachment_len; | |
9852 | int read_len; | |
9853 | ||
9854 | remote_buffer_add_string (&p, &left, "vFile:pread:"); | |
9855 | ||
9856 | remote_buffer_add_int (&p, &left, fd); | |
9857 | remote_buffer_add_string (&p, &left, ","); | |
9858 | ||
9859 | remote_buffer_add_int (&p, &left, len); | |
9860 | remote_buffer_add_string (&p, &left, ","); | |
9861 | ||
9862 | remote_buffer_add_int (&p, &left, offset); | |
9863 | ||
9864 | ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_pread, | |
9865 | remote_errno, &attachment, | |
9866 | &attachment_len); | |
9867 | ||
9868 | if (ret < 0) | |
9869 | return ret; | |
9870 | ||
bc20a4af | 9871 | read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len, |
a6b151f1 DJ |
9872 | read_buf, len); |
9873 | if (read_len != ret) | |
9874 | error (_("Read returned %d, but %d bytes."), ret, (int) read_len); | |
9875 | ||
9876 | return ret; | |
9877 | } | |
9878 | ||
9879 | /* Close FD on the remote target. Return 0, or -1 if an error occurs | |
9880 | (and set *REMOTE_ERRNO). */ | |
9881 | ||
9882 | static int | |
9883 | remote_hostio_close (int fd, int *remote_errno) | |
9884 | { | |
9885 | struct remote_state *rs = get_remote_state (); | |
9886 | char *p = rs->buf; | |
9887 | int left = get_remote_packet_size () - 1; | |
9888 | ||
9889 | remote_buffer_add_string (&p, &left, "vFile:close:"); | |
9890 | ||
9891 | remote_buffer_add_int (&p, &left, fd); | |
9892 | ||
9893 | return remote_hostio_send_command (p - rs->buf, PACKET_vFile_close, | |
9894 | remote_errno, NULL, NULL); | |
9895 | } | |
9896 | ||
9897 | /* Unlink FILENAME on the remote target. Return 0, or -1 if an error | |
9898 | occurs (and set *REMOTE_ERRNO). */ | |
9899 | ||
9900 | static int | |
9901 | remote_hostio_unlink (const char *filename, int *remote_errno) | |
9902 | { | |
9903 | struct remote_state *rs = get_remote_state (); | |
9904 | char *p = rs->buf; | |
9905 | int left = get_remote_packet_size () - 1; | |
9906 | ||
9907 | remote_buffer_add_string (&p, &left, "vFile:unlink:"); | |
9908 | ||
9909 | remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename, | |
9910 | strlen (filename)); | |
9911 | ||
9912 | return remote_hostio_send_command (p - rs->buf, PACKET_vFile_unlink, | |
9913 | remote_errno, NULL, NULL); | |
9914 | } | |
9915 | ||
b9e7b9c3 UW |
9916 | /* Read value of symbolic link FILENAME on the remote target. Return |
9917 | a null-terminated string allocated via xmalloc, or NULL if an error | |
9918 | occurs (and set *REMOTE_ERRNO). */ | |
9919 | ||
9920 | static char * | |
9921 | remote_hostio_readlink (const char *filename, int *remote_errno) | |
9922 | { | |
9923 | struct remote_state *rs = get_remote_state (); | |
9924 | char *p = rs->buf; | |
9925 | char *attachment; | |
9926 | int left = get_remote_packet_size (); | |
9927 | int len, attachment_len; | |
9928 | int read_len; | |
9929 | char *ret; | |
9930 | ||
9931 | remote_buffer_add_string (&p, &left, "vFile:readlink:"); | |
9932 | ||
9933 | remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename, | |
9934 | strlen (filename)); | |
9935 | ||
9936 | len = remote_hostio_send_command (p - rs->buf, PACKET_vFile_readlink, | |
9937 | remote_errno, &attachment, | |
9938 | &attachment_len); | |
9939 | ||
9940 | if (len < 0) | |
9941 | return NULL; | |
9942 | ||
9943 | ret = xmalloc (len + 1); | |
9944 | ||
bc20a4af PA |
9945 | read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len, |
9946 | (gdb_byte *) ret, len); | |
b9e7b9c3 UW |
9947 | if (read_len != len) |
9948 | error (_("Readlink returned %d, but %d bytes."), len, read_len); | |
9949 | ||
9950 | ret[len] = '\0'; | |
9951 | return ret; | |
9952 | } | |
9953 | ||
a6b151f1 DJ |
9954 | static int |
9955 | remote_fileio_errno_to_host (int errnum) | |
9956 | { | |
9957 | switch (errnum) | |
9958 | { | |
9959 | case FILEIO_EPERM: | |
9960 | return EPERM; | |
9961 | case FILEIO_ENOENT: | |
9962 | return ENOENT; | |
9963 | case FILEIO_EINTR: | |
9964 | return EINTR; | |
9965 | case FILEIO_EIO: | |
9966 | return EIO; | |
9967 | case FILEIO_EBADF: | |
9968 | return EBADF; | |
9969 | case FILEIO_EACCES: | |
9970 | return EACCES; | |
9971 | case FILEIO_EFAULT: | |
9972 | return EFAULT; | |
9973 | case FILEIO_EBUSY: | |
9974 | return EBUSY; | |
9975 | case FILEIO_EEXIST: | |
9976 | return EEXIST; | |
9977 | case FILEIO_ENODEV: | |
9978 | return ENODEV; | |
9979 | case FILEIO_ENOTDIR: | |
9980 | return ENOTDIR; | |
9981 | case FILEIO_EISDIR: | |
9982 | return EISDIR; | |
9983 | case FILEIO_EINVAL: | |
9984 | return EINVAL; | |
9985 | case FILEIO_ENFILE: | |
9986 | return ENFILE; | |
9987 | case FILEIO_EMFILE: | |
9988 | return EMFILE; | |
9989 | case FILEIO_EFBIG: | |
9990 | return EFBIG; | |
9991 | case FILEIO_ENOSPC: | |
9992 | return ENOSPC; | |
9993 | case FILEIO_ESPIPE: | |
9994 | return ESPIPE; | |
9995 | case FILEIO_EROFS: | |
9996 | return EROFS; | |
9997 | case FILEIO_ENOSYS: | |
9998 | return ENOSYS; | |
9999 | case FILEIO_ENAMETOOLONG: | |
10000 | return ENAMETOOLONG; | |
10001 | } | |
10002 | return -1; | |
10003 | } | |
10004 | ||
10005 | static char * | |
10006 | remote_hostio_error (int errnum) | |
10007 | { | |
10008 | int host_error = remote_fileio_errno_to_host (errnum); | |
10009 | ||
10010 | if (host_error == -1) | |
10011 | error (_("Unknown remote I/O error %d"), errnum); | |
10012 | else | |
10013 | error (_("Remote I/O error: %s"), safe_strerror (host_error)); | |
10014 | } | |
10015 | ||
a6b151f1 DJ |
10016 | static void |
10017 | remote_hostio_close_cleanup (void *opaque) | |
10018 | { | |
10019 | int fd = *(int *) opaque; | |
10020 | int remote_errno; | |
10021 | ||
10022 | remote_hostio_close (fd, &remote_errno); | |
10023 | } | |
10024 | ||
f1838a98 UW |
10025 | |
10026 | static void * | |
10027 | remote_bfd_iovec_open (struct bfd *abfd, void *open_closure) | |
10028 | { | |
10029 | const char *filename = bfd_get_filename (abfd); | |
10030 | int fd, remote_errno; | |
10031 | int *stream; | |
10032 | ||
10033 | gdb_assert (remote_filename_p (filename)); | |
10034 | ||
10035 | fd = remote_hostio_open (filename + 7, FILEIO_O_RDONLY, 0, &remote_errno); | |
10036 | if (fd == -1) | |
10037 | { | |
10038 | errno = remote_fileio_errno_to_host (remote_errno); | |
10039 | bfd_set_error (bfd_error_system_call); | |
10040 | return NULL; | |
10041 | } | |
10042 | ||
10043 | stream = xmalloc (sizeof (int)); | |
10044 | *stream = fd; | |
10045 | return stream; | |
10046 | } | |
10047 | ||
10048 | static int | |
10049 | remote_bfd_iovec_close (struct bfd *abfd, void *stream) | |
10050 | { | |
10051 | int fd = *(int *)stream; | |
10052 | int remote_errno; | |
10053 | ||
10054 | xfree (stream); | |
10055 | ||
10056 | /* Ignore errors on close; these may happen if the remote | |
10057 | connection was already torn down. */ | |
10058 | remote_hostio_close (fd, &remote_errno); | |
10059 | ||
39ed5604 JK |
10060 | /* Zero means success. */ |
10061 | return 0; | |
f1838a98 UW |
10062 | } |
10063 | ||
10064 | static file_ptr | |
10065 | remote_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf, | |
10066 | file_ptr nbytes, file_ptr offset) | |
10067 | { | |
10068 | int fd = *(int *)stream; | |
10069 | int remote_errno; | |
10070 | file_ptr pos, bytes; | |
10071 | ||
10072 | pos = 0; | |
10073 | while (nbytes > pos) | |
10074 | { | |
bc20a4af | 10075 | bytes = remote_hostio_pread (fd, (gdb_byte *) buf + pos, nbytes - pos, |
f1838a98 UW |
10076 | offset + pos, &remote_errno); |
10077 | if (bytes == 0) | |
10078 | /* Success, but no bytes, means end-of-file. */ | |
10079 | break; | |
10080 | if (bytes == -1) | |
10081 | { | |
10082 | errno = remote_fileio_errno_to_host (remote_errno); | |
10083 | bfd_set_error (bfd_error_system_call); | |
10084 | return -1; | |
10085 | } | |
10086 | ||
10087 | pos += bytes; | |
10088 | } | |
10089 | ||
10090 | return pos; | |
10091 | } | |
10092 | ||
10093 | static int | |
10094 | remote_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb) | |
10095 | { | |
10096 | /* FIXME: We should probably implement remote_hostio_stat. */ | |
10097 | sb->st_size = INT_MAX; | |
10098 | return 0; | |
10099 | } | |
10100 | ||
10101 | int | |
10102 | remote_filename_p (const char *filename) | |
10103 | { | |
3736004f PA |
10104 | return strncmp (filename, |
10105 | REMOTE_SYSROOT_PREFIX, | |
10106 | sizeof (REMOTE_SYSROOT_PREFIX) - 1) == 0; | |
f1838a98 UW |
10107 | } |
10108 | ||
10109 | bfd * | |
10110 | remote_bfd_open (const char *remote_file, const char *target) | |
10111 | { | |
64c31149 TT |
10112 | bfd *abfd = gdb_bfd_openr_iovec (remote_file, target, |
10113 | remote_bfd_iovec_open, NULL, | |
10114 | remote_bfd_iovec_pread, | |
10115 | remote_bfd_iovec_close, | |
10116 | remote_bfd_iovec_stat); | |
10117 | ||
a4453b7e | 10118 | return abfd; |
f1838a98 UW |
10119 | } |
10120 | ||
a6b151f1 DJ |
10121 | void |
10122 | remote_file_put (const char *local_file, const char *remote_file, int from_tty) | |
10123 | { | |
10124 | struct cleanup *back_to, *close_cleanup; | |
10125 | int retcode, fd, remote_errno, bytes, io_size; | |
10126 | FILE *file; | |
10127 | gdb_byte *buffer; | |
10128 | int bytes_in_buffer; | |
10129 | int saw_eof; | |
10130 | ULONGEST offset; | |
5d93a237 | 10131 | struct remote_state *rs = get_remote_state (); |
a6b151f1 | 10132 | |
5d93a237 | 10133 | if (!rs->remote_desc) |
a6b151f1 DJ |
10134 | error (_("command can only be used with remote target")); |
10135 | ||
614c279d | 10136 | file = gdb_fopen_cloexec (local_file, "rb"); |
a6b151f1 DJ |
10137 | if (file == NULL) |
10138 | perror_with_name (local_file); | |
7c8a8b04 | 10139 | back_to = make_cleanup_fclose (file); |
a6b151f1 DJ |
10140 | |
10141 | fd = remote_hostio_open (remote_file, (FILEIO_O_WRONLY | FILEIO_O_CREAT | |
10142 | | FILEIO_O_TRUNC), | |
10143 | 0700, &remote_errno); | |
10144 | if (fd == -1) | |
10145 | remote_hostio_error (remote_errno); | |
10146 | ||
10147 | /* Send up to this many bytes at once. They won't all fit in the | |
10148 | remote packet limit, so we'll transfer slightly fewer. */ | |
10149 | io_size = get_remote_packet_size (); | |
10150 | buffer = xmalloc (io_size); | |
10151 | make_cleanup (xfree, buffer); | |
10152 | ||
10153 | close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd); | |
10154 | ||
10155 | bytes_in_buffer = 0; | |
10156 | saw_eof = 0; | |
10157 | offset = 0; | |
10158 | while (bytes_in_buffer || !saw_eof) | |
10159 | { | |
10160 | if (!saw_eof) | |
10161 | { | |
3e43a32a MS |
10162 | bytes = fread (buffer + bytes_in_buffer, 1, |
10163 | io_size - bytes_in_buffer, | |
a6b151f1 DJ |
10164 | file); |
10165 | if (bytes == 0) | |
10166 | { | |
10167 | if (ferror (file)) | |
10168 | error (_("Error reading %s."), local_file); | |
10169 | else | |
10170 | { | |
10171 | /* EOF. Unless there is something still in the | |
10172 | buffer from the last iteration, we are done. */ | |
10173 | saw_eof = 1; | |
10174 | if (bytes_in_buffer == 0) | |
10175 | break; | |
10176 | } | |
10177 | } | |
10178 | } | |
10179 | else | |
10180 | bytes = 0; | |
10181 | ||
10182 | bytes += bytes_in_buffer; | |
10183 | bytes_in_buffer = 0; | |
10184 | ||
3e43a32a MS |
10185 | retcode = remote_hostio_pwrite (fd, buffer, bytes, |
10186 | offset, &remote_errno); | |
a6b151f1 DJ |
10187 | |
10188 | if (retcode < 0) | |
10189 | remote_hostio_error (remote_errno); | |
10190 | else if (retcode == 0) | |
10191 | error (_("Remote write of %d bytes returned 0!"), bytes); | |
10192 | else if (retcode < bytes) | |
10193 | { | |
10194 | /* Short write. Save the rest of the read data for the next | |
10195 | write. */ | |
10196 | bytes_in_buffer = bytes - retcode; | |
10197 | memmove (buffer, buffer + retcode, bytes_in_buffer); | |
10198 | } | |
10199 | ||
10200 | offset += retcode; | |
10201 | } | |
10202 | ||
10203 | discard_cleanups (close_cleanup); | |
10204 | if (remote_hostio_close (fd, &remote_errno)) | |
10205 | remote_hostio_error (remote_errno); | |
10206 | ||
10207 | if (from_tty) | |
10208 | printf_filtered (_("Successfully sent file \"%s\".\n"), local_file); | |
10209 | do_cleanups (back_to); | |
10210 | } | |
10211 | ||
10212 | void | |
10213 | remote_file_get (const char *remote_file, const char *local_file, int from_tty) | |
10214 | { | |
10215 | struct cleanup *back_to, *close_cleanup; | |
cea39f65 | 10216 | int fd, remote_errno, bytes, io_size; |
a6b151f1 DJ |
10217 | FILE *file; |
10218 | gdb_byte *buffer; | |
10219 | ULONGEST offset; | |
5d93a237 | 10220 | struct remote_state *rs = get_remote_state (); |
a6b151f1 | 10221 | |
5d93a237 | 10222 | if (!rs->remote_desc) |
a6b151f1 DJ |
10223 | error (_("command can only be used with remote target")); |
10224 | ||
10225 | fd = remote_hostio_open (remote_file, FILEIO_O_RDONLY, 0, &remote_errno); | |
10226 | if (fd == -1) | |
10227 | remote_hostio_error (remote_errno); | |
10228 | ||
614c279d | 10229 | file = gdb_fopen_cloexec (local_file, "wb"); |
a6b151f1 DJ |
10230 | if (file == NULL) |
10231 | perror_with_name (local_file); | |
7c8a8b04 | 10232 | back_to = make_cleanup_fclose (file); |
a6b151f1 DJ |
10233 | |
10234 | /* Send up to this many bytes at once. They won't all fit in the | |
10235 | remote packet limit, so we'll transfer slightly fewer. */ | |
10236 | io_size = get_remote_packet_size (); | |
10237 | buffer = xmalloc (io_size); | |
10238 | make_cleanup (xfree, buffer); | |
10239 | ||
10240 | close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd); | |
10241 | ||
10242 | offset = 0; | |
10243 | while (1) | |
10244 | { | |
10245 | bytes = remote_hostio_pread (fd, buffer, io_size, offset, &remote_errno); | |
10246 | if (bytes == 0) | |
10247 | /* Success, but no bytes, means end-of-file. */ | |
10248 | break; | |
10249 | if (bytes == -1) | |
10250 | remote_hostio_error (remote_errno); | |
10251 | ||
10252 | offset += bytes; | |
10253 | ||
10254 | bytes = fwrite (buffer, 1, bytes, file); | |
10255 | if (bytes == 0) | |
10256 | perror_with_name (local_file); | |
10257 | } | |
10258 | ||
10259 | discard_cleanups (close_cleanup); | |
10260 | if (remote_hostio_close (fd, &remote_errno)) | |
10261 | remote_hostio_error (remote_errno); | |
10262 | ||
10263 | if (from_tty) | |
10264 | printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file); | |
10265 | do_cleanups (back_to); | |
10266 | } | |
10267 | ||
10268 | void | |
10269 | remote_file_delete (const char *remote_file, int from_tty) | |
10270 | { | |
10271 | int retcode, remote_errno; | |
5d93a237 | 10272 | struct remote_state *rs = get_remote_state (); |
a6b151f1 | 10273 | |
5d93a237 | 10274 | if (!rs->remote_desc) |
a6b151f1 DJ |
10275 | error (_("command can only be used with remote target")); |
10276 | ||
10277 | retcode = remote_hostio_unlink (remote_file, &remote_errno); | |
10278 | if (retcode == -1) | |
10279 | remote_hostio_error (remote_errno); | |
10280 | ||
10281 | if (from_tty) | |
10282 | printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file); | |
10283 | } | |
10284 | ||
10285 | static void | |
10286 | remote_put_command (char *args, int from_tty) | |
10287 | { | |
10288 | struct cleanup *back_to; | |
10289 | char **argv; | |
10290 | ||
d1a41061 PP |
10291 | if (args == NULL) |
10292 | error_no_arg (_("file to put")); | |
10293 | ||
10294 | argv = gdb_buildargv (args); | |
a6b151f1 DJ |
10295 | back_to = make_cleanup_freeargv (argv); |
10296 | if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL) | |
10297 | error (_("Invalid parameters to remote put")); | |
10298 | ||
10299 | remote_file_put (argv[0], argv[1], from_tty); | |
10300 | ||
10301 | do_cleanups (back_to); | |
10302 | } | |
10303 | ||
10304 | static void | |
10305 | remote_get_command (char *args, int from_tty) | |
10306 | { | |
10307 | struct cleanup *back_to; | |
10308 | char **argv; | |
10309 | ||
d1a41061 PP |
10310 | if (args == NULL) |
10311 | error_no_arg (_("file to get")); | |
10312 | ||
10313 | argv = gdb_buildargv (args); | |
a6b151f1 DJ |
10314 | back_to = make_cleanup_freeargv (argv); |
10315 | if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL) | |
10316 | error (_("Invalid parameters to remote get")); | |
10317 | ||
10318 | remote_file_get (argv[0], argv[1], from_tty); | |
10319 | ||
10320 | do_cleanups (back_to); | |
10321 | } | |
10322 | ||
10323 | static void | |
10324 | remote_delete_command (char *args, int from_tty) | |
10325 | { | |
10326 | struct cleanup *back_to; | |
10327 | char **argv; | |
10328 | ||
d1a41061 PP |
10329 | if (args == NULL) |
10330 | error_no_arg (_("file to delete")); | |
10331 | ||
10332 | argv = gdb_buildargv (args); | |
a6b151f1 DJ |
10333 | back_to = make_cleanup_freeargv (argv); |
10334 | if (argv[0] == NULL || argv[1] != NULL) | |
10335 | error (_("Invalid parameters to remote delete")); | |
10336 | ||
10337 | remote_file_delete (argv[0], from_tty); | |
10338 | ||
10339 | do_cleanups (back_to); | |
10340 | } | |
10341 | ||
10342 | static void | |
10343 | remote_command (char *args, int from_tty) | |
10344 | { | |
10345 | help_list (remote_cmdlist, "remote ", -1, gdb_stdout); | |
10346 | } | |
10347 | ||
b2175913 MS |
10348 | static int |
10349 | remote_can_execute_reverse (void) | |
10350 | { | |
40ab02ce MS |
10351 | if (remote_protocol_packets[PACKET_bs].support == PACKET_ENABLE |
10352 | || remote_protocol_packets[PACKET_bc].support == PACKET_ENABLE) | |
10353 | return 1; | |
10354 | else | |
10355 | return 0; | |
b2175913 MS |
10356 | } |
10357 | ||
74531fed PA |
10358 | static int |
10359 | remote_supports_non_stop (void) | |
10360 | { | |
10361 | return 1; | |
10362 | } | |
10363 | ||
03583c20 UW |
10364 | static int |
10365 | remote_supports_disable_randomization (void) | |
10366 | { | |
10367 | /* Only supported in extended mode. */ | |
10368 | return 0; | |
10369 | } | |
10370 | ||
8a305172 PA |
10371 | static int |
10372 | remote_supports_multi_process (void) | |
10373 | { | |
10374 | struct remote_state *rs = get_remote_state (); | |
a744cf53 | 10375 | |
901f9912 UW |
10376 | /* Only extended-remote handles being attached to multiple |
10377 | processes, even though plain remote can use the multi-process | |
10378 | thread id extensions, so that GDB knows the target process's | |
10379 | PID. */ | |
10380 | return rs->extended && remote_multi_process_p (rs); | |
8a305172 PA |
10381 | } |
10382 | ||
70221824 | 10383 | static int |
782b2b07 SS |
10384 | remote_supports_cond_tracepoints (void) |
10385 | { | |
10386 | struct remote_state *rs = get_remote_state (); | |
a744cf53 | 10387 | |
782b2b07 SS |
10388 | return rs->cond_tracepoints; |
10389 | } | |
10390 | ||
3788aec7 LM |
10391 | static int |
10392 | remote_supports_cond_breakpoints (void) | |
10393 | { | |
10394 | struct remote_state *rs = get_remote_state (); | |
10395 | ||
10396 | return rs->cond_breakpoints; | |
10397 | } | |
10398 | ||
70221824 | 10399 | static int |
7a697b8d SS |
10400 | remote_supports_fast_tracepoints (void) |
10401 | { | |
10402 | struct remote_state *rs = get_remote_state (); | |
a744cf53 | 10403 | |
7a697b8d SS |
10404 | return rs->fast_tracepoints; |
10405 | } | |
10406 | ||
0fb4aa4b PA |
10407 | static int |
10408 | remote_supports_static_tracepoints (void) | |
10409 | { | |
10410 | struct remote_state *rs = get_remote_state (); | |
10411 | ||
10412 | return rs->static_tracepoints; | |
10413 | } | |
10414 | ||
1e4d1764 YQ |
10415 | static int |
10416 | remote_supports_install_in_trace (void) | |
10417 | { | |
10418 | struct remote_state *rs = get_remote_state (); | |
10419 | ||
10420 | return rs->install_in_trace; | |
10421 | } | |
10422 | ||
d248b706 KY |
10423 | static int |
10424 | remote_supports_enable_disable_tracepoint (void) | |
10425 | { | |
10426 | struct remote_state *rs = get_remote_state (); | |
10427 | ||
10428 | return rs->enable_disable_tracepoints; | |
10429 | } | |
10430 | ||
3065dfb6 SS |
10431 | static int |
10432 | remote_supports_string_tracing (void) | |
10433 | { | |
10434 | struct remote_state *rs = get_remote_state (); | |
10435 | ||
10436 | return rs->string_tracing; | |
10437 | } | |
10438 | ||
d3ce09f5 SS |
10439 | static int |
10440 | remote_can_run_breakpoint_commands (void) | |
10441 | { | |
10442 | struct remote_state *rs = get_remote_state (); | |
10443 | ||
10444 | return rs->breakpoint_commands; | |
10445 | } | |
10446 | ||
35b1e5cc | 10447 | static void |
ad91cd99 | 10448 | remote_trace_init (void) |
35b1e5cc SS |
10449 | { |
10450 | putpkt ("QTinit"); | |
10451 | remote_get_noisy_reply (&target_buf, &target_buf_size); | |
ad91cd99 | 10452 | if (strcmp (target_buf, "OK") != 0) |
35b1e5cc SS |
10453 | error (_("Target does not support this command.")); |
10454 | } | |
10455 | ||
10456 | static void free_actions_list (char **actions_list); | |
10457 | static void free_actions_list_cleanup_wrapper (void *); | |
10458 | static void | |
10459 | free_actions_list_cleanup_wrapper (void *al) | |
10460 | { | |
10461 | free_actions_list (al); | |
10462 | } | |
10463 | ||
10464 | static void | |
10465 | free_actions_list (char **actions_list) | |
10466 | { | |
10467 | int ndx; | |
10468 | ||
10469 | if (actions_list == 0) | |
10470 | return; | |
10471 | ||
10472 | for (ndx = 0; actions_list[ndx]; ndx++) | |
10473 | xfree (actions_list[ndx]); | |
10474 | ||
10475 | xfree (actions_list); | |
10476 | } | |
10477 | ||
409873ef SS |
10478 | /* Recursive routine to walk through command list including loops, and |
10479 | download packets for each command. */ | |
10480 | ||
10481 | static void | |
10482 | remote_download_command_source (int num, ULONGEST addr, | |
10483 | struct command_line *cmds) | |
10484 | { | |
10485 | struct remote_state *rs = get_remote_state (); | |
10486 | struct command_line *cmd; | |
10487 | ||
10488 | for (cmd = cmds; cmd; cmd = cmd->next) | |
10489 | { | |
0df8b418 | 10490 | QUIT; /* Allow user to bail out with ^C. */ |
409873ef SS |
10491 | strcpy (rs->buf, "QTDPsrc:"); |
10492 | encode_source_string (num, addr, "cmd", cmd->line, | |
10493 | rs->buf + strlen (rs->buf), | |
10494 | rs->buf_size - strlen (rs->buf)); | |
10495 | putpkt (rs->buf); | |
10496 | remote_get_noisy_reply (&target_buf, &target_buf_size); | |
10497 | if (strcmp (target_buf, "OK")) | |
10498 | warning (_("Target does not support source download.")); | |
10499 | ||
10500 | if (cmd->control_type == while_control | |
10501 | || cmd->control_type == while_stepping_control) | |
10502 | { | |
10503 | remote_download_command_source (num, addr, *cmd->body_list); | |
10504 | ||
0df8b418 | 10505 | QUIT; /* Allow user to bail out with ^C. */ |
409873ef SS |
10506 | strcpy (rs->buf, "QTDPsrc:"); |
10507 | encode_source_string (num, addr, "cmd", "end", | |
10508 | rs->buf + strlen (rs->buf), | |
10509 | rs->buf_size - strlen (rs->buf)); | |
10510 | putpkt (rs->buf); | |
10511 | remote_get_noisy_reply (&target_buf, &target_buf_size); | |
10512 | if (strcmp (target_buf, "OK")) | |
10513 | warning (_("Target does not support source download.")); | |
10514 | } | |
10515 | } | |
10516 | } | |
10517 | ||
35b1e5cc | 10518 | static void |
e8ba3115 | 10519 | remote_download_tracepoint (struct bp_location *loc) |
35b1e5cc | 10520 | { |
bba74b36 | 10521 | #define BUF_SIZE 2048 |
e8ba3115 | 10522 | |
35b1e5cc | 10523 | CORE_ADDR tpaddr; |
409873ef | 10524 | char addrbuf[40]; |
bba74b36 | 10525 | char buf[BUF_SIZE]; |
35b1e5cc SS |
10526 | char **tdp_actions; |
10527 | char **stepping_actions; | |
10528 | int ndx; | |
10529 | struct cleanup *old_chain = NULL; | |
10530 | struct agent_expr *aexpr; | |
10531 | struct cleanup *aexpr_chain = NULL; | |
10532 | char *pkt; | |
e8ba3115 | 10533 | struct breakpoint *b = loc->owner; |
d9b3f62e | 10534 | struct tracepoint *t = (struct tracepoint *) b; |
35b1e5cc | 10535 | |
dc673c81 | 10536 | encode_actions_rsp (loc, &tdp_actions, &stepping_actions); |
e8ba3115 YQ |
10537 | old_chain = make_cleanup (free_actions_list_cleanup_wrapper, |
10538 | tdp_actions); | |
10539 | (void) make_cleanup (free_actions_list_cleanup_wrapper, | |
10540 | stepping_actions); | |
10541 | ||
10542 | tpaddr = loc->address; | |
10543 | sprintf_vma (addrbuf, tpaddr); | |
bba74b36 YQ |
10544 | xsnprintf (buf, BUF_SIZE, "QTDP:%x:%s:%c:%lx:%x", b->number, |
10545 | addrbuf, /* address */ | |
10546 | (b->enable_state == bp_enabled ? 'E' : 'D'), | |
10547 | t->step_count, t->pass_count); | |
e8ba3115 YQ |
10548 | /* Fast tracepoints are mostly handled by the target, but we can |
10549 | tell the target how big of an instruction block should be moved | |
10550 | around. */ | |
10551 | if (b->type == bp_fast_tracepoint) | |
10552 | { | |
10553 | /* Only test for support at download time; we may not know | |
10554 | target capabilities at definition time. */ | |
10555 | if (remote_supports_fast_tracepoints ()) | |
35b1e5cc | 10556 | { |
e8ba3115 | 10557 | int isize; |
35b1e5cc | 10558 | |
f5656ead | 10559 | if (gdbarch_fast_tracepoint_valid_at (target_gdbarch (), |
e8ba3115 | 10560 | tpaddr, &isize, NULL)) |
bba74b36 YQ |
10561 | xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":F%x", |
10562 | isize); | |
35b1e5cc | 10563 | else |
e8ba3115 YQ |
10564 | /* If it passed validation at definition but fails now, |
10565 | something is very wrong. */ | |
10566 | internal_error (__FILE__, __LINE__, | |
10567 | _("Fast tracepoint not " | |
10568 | "valid during download")); | |
35b1e5cc | 10569 | } |
e8ba3115 YQ |
10570 | else |
10571 | /* Fast tracepoints are functionally identical to regular | |
10572 | tracepoints, so don't take lack of support as a reason to | |
10573 | give up on the trace run. */ | |
10574 | warning (_("Target does not support fast tracepoints, " | |
10575 | "downloading %d as regular tracepoint"), b->number); | |
10576 | } | |
10577 | else if (b->type == bp_static_tracepoint) | |
10578 | { | |
10579 | /* Only test for support at download time; we may not know | |
10580 | target capabilities at definition time. */ | |
10581 | if (remote_supports_static_tracepoints ()) | |
0fb4aa4b | 10582 | { |
e8ba3115 | 10583 | struct static_tracepoint_marker marker; |
0fb4aa4b | 10584 | |
e8ba3115 YQ |
10585 | if (target_static_tracepoint_marker_at (tpaddr, &marker)) |
10586 | strcat (buf, ":S"); | |
0fb4aa4b | 10587 | else |
e8ba3115 | 10588 | error (_("Static tracepoint not valid during download")); |
0fb4aa4b | 10589 | } |
e8ba3115 YQ |
10590 | else |
10591 | /* Fast tracepoints are functionally identical to regular | |
10592 | tracepoints, so don't take lack of support as a reason | |
10593 | to give up on the trace run. */ | |
10594 | error (_("Target does not support static tracepoints")); | |
10595 | } | |
10596 | /* If the tracepoint has a conditional, make it into an agent | |
10597 | expression and append to the definition. */ | |
10598 | if (loc->cond) | |
10599 | { | |
10600 | /* Only test support at download time, we may not know target | |
10601 | capabilities at definition time. */ | |
10602 | if (remote_supports_cond_tracepoints ()) | |
35b1e5cc | 10603 | { |
e8ba3115 YQ |
10604 | aexpr = gen_eval_for_expr (tpaddr, loc->cond); |
10605 | aexpr_chain = make_cleanup_free_agent_expr (aexpr); | |
bba74b36 YQ |
10606 | xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":X%x,", |
10607 | aexpr->len); | |
e8ba3115 YQ |
10608 | pkt = buf + strlen (buf); |
10609 | for (ndx = 0; ndx < aexpr->len; ++ndx) | |
10610 | pkt = pack_hex_byte (pkt, aexpr->buf[ndx]); | |
10611 | *pkt = '\0'; | |
10612 | do_cleanups (aexpr_chain); | |
35b1e5cc | 10613 | } |
e8ba3115 YQ |
10614 | else |
10615 | warning (_("Target does not support conditional tracepoints, " | |
10616 | "ignoring tp %d cond"), b->number); | |
10617 | } | |
35b1e5cc | 10618 | |
d9b3f62e | 10619 | if (b->commands || *default_collect) |
e8ba3115 YQ |
10620 | strcat (buf, "-"); |
10621 | putpkt (buf); | |
10622 | remote_get_noisy_reply (&target_buf, &target_buf_size); | |
10623 | if (strcmp (target_buf, "OK")) | |
10624 | error (_("Target does not support tracepoints.")); | |
35b1e5cc | 10625 | |
e8ba3115 YQ |
10626 | /* do_single_steps (t); */ |
10627 | if (tdp_actions) | |
10628 | { | |
10629 | for (ndx = 0; tdp_actions[ndx]; ndx++) | |
35b1e5cc | 10630 | { |
e8ba3115 | 10631 | QUIT; /* Allow user to bail out with ^C. */ |
bba74b36 YQ |
10632 | xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%c", |
10633 | b->number, addrbuf, /* address */ | |
10634 | tdp_actions[ndx], | |
10635 | ((tdp_actions[ndx + 1] || stepping_actions) | |
10636 | ? '-' : 0)); | |
e8ba3115 YQ |
10637 | putpkt (buf); |
10638 | remote_get_noisy_reply (&target_buf, | |
10639 | &target_buf_size); | |
10640 | if (strcmp (target_buf, "OK")) | |
10641 | error (_("Error on target while setting tracepoints.")); | |
35b1e5cc | 10642 | } |
e8ba3115 YQ |
10643 | } |
10644 | if (stepping_actions) | |
10645 | { | |
10646 | for (ndx = 0; stepping_actions[ndx]; ndx++) | |
35b1e5cc | 10647 | { |
e8ba3115 | 10648 | QUIT; /* Allow user to bail out with ^C. */ |
bba74b36 YQ |
10649 | xsnprintf (buf, BUF_SIZE, "QTDP:-%x:%s:%s%s%s", |
10650 | b->number, addrbuf, /* address */ | |
10651 | ((ndx == 0) ? "S" : ""), | |
10652 | stepping_actions[ndx], | |
10653 | (stepping_actions[ndx + 1] ? "-" : "")); | |
e8ba3115 YQ |
10654 | putpkt (buf); |
10655 | remote_get_noisy_reply (&target_buf, | |
10656 | &target_buf_size); | |
10657 | if (strcmp (target_buf, "OK")) | |
10658 | error (_("Error on target while setting tracepoints.")); | |
35b1e5cc | 10659 | } |
e8ba3115 | 10660 | } |
409873ef | 10661 | |
e8ba3115 YQ |
10662 | if (remote_protocol_packets[PACKET_TracepointSource].support |
10663 | == PACKET_ENABLE) | |
10664 | { | |
10665 | if (b->addr_string) | |
409873ef | 10666 | { |
e8ba3115 YQ |
10667 | strcpy (buf, "QTDPsrc:"); |
10668 | encode_source_string (b->number, loc->address, | |
10669 | "at", b->addr_string, buf + strlen (buf), | |
10670 | 2048 - strlen (buf)); | |
409873ef | 10671 | |
e8ba3115 YQ |
10672 | putpkt (buf); |
10673 | remote_get_noisy_reply (&target_buf, &target_buf_size); | |
10674 | if (strcmp (target_buf, "OK")) | |
10675 | warning (_("Target does not support source download.")); | |
409873ef | 10676 | } |
e8ba3115 YQ |
10677 | if (b->cond_string) |
10678 | { | |
10679 | strcpy (buf, "QTDPsrc:"); | |
10680 | encode_source_string (b->number, loc->address, | |
10681 | "cond", b->cond_string, buf + strlen (buf), | |
10682 | 2048 - strlen (buf)); | |
10683 | putpkt (buf); | |
10684 | remote_get_noisy_reply (&target_buf, &target_buf_size); | |
10685 | if (strcmp (target_buf, "OK")) | |
10686 | warning (_("Target does not support source download.")); | |
10687 | } | |
10688 | remote_download_command_source (b->number, loc->address, | |
10689 | breakpoint_commands (b)); | |
35b1e5cc | 10690 | } |
e8ba3115 YQ |
10691 | |
10692 | do_cleanups (old_chain); | |
35b1e5cc SS |
10693 | } |
10694 | ||
1e4d1764 YQ |
10695 | static int |
10696 | remote_can_download_tracepoint (void) | |
10697 | { | |
1e51243a PA |
10698 | struct remote_state *rs = get_remote_state (); |
10699 | struct trace_status *ts; | |
10700 | int status; | |
10701 | ||
10702 | /* Don't try to install tracepoints until we've relocated our | |
10703 | symbols, and fetched and merged the target's tracepoint list with | |
10704 | ours. */ | |
10705 | if (rs->starting_up) | |
10706 | return 0; | |
10707 | ||
10708 | ts = current_trace_status (); | |
10709 | status = remote_get_trace_status (ts); | |
1e4d1764 YQ |
10710 | |
10711 | if (status == -1 || !ts->running_known || !ts->running) | |
10712 | return 0; | |
10713 | ||
10714 | /* If we are in a tracing experiment, but remote stub doesn't support | |
10715 | installing tracepoint in trace, we have to return. */ | |
10716 | if (!remote_supports_install_in_trace ()) | |
10717 | return 0; | |
10718 | ||
10719 | return 1; | |
10720 | } | |
10721 | ||
10722 | ||
35b1e5cc SS |
10723 | static void |
10724 | remote_download_trace_state_variable (struct trace_state_variable *tsv) | |
10725 | { | |
10726 | struct remote_state *rs = get_remote_state (); | |
00bf0b85 | 10727 | char *p; |
35b1e5cc | 10728 | |
bba74b36 YQ |
10729 | xsnprintf (rs->buf, get_remote_packet_size (), "QTDV:%x:%s:%x:", |
10730 | tsv->number, phex ((ULONGEST) tsv->initial_value, 8), | |
10731 | tsv->builtin); | |
00bf0b85 SS |
10732 | p = rs->buf + strlen (rs->buf); |
10733 | if ((p - rs->buf) + strlen (tsv->name) * 2 >= get_remote_packet_size ()) | |
10734 | error (_("Trace state variable name too long for tsv definition packet")); | |
10735 | p += 2 * bin2hex ((gdb_byte *) (tsv->name), p, 0); | |
10736 | *p++ = '\0'; | |
35b1e5cc SS |
10737 | putpkt (rs->buf); |
10738 | remote_get_noisy_reply (&target_buf, &target_buf_size); | |
ad91cd99 PA |
10739 | if (*target_buf == '\0') |
10740 | error (_("Target does not support this command.")); | |
10741 | if (strcmp (target_buf, "OK") != 0) | |
10742 | error (_("Error on target while downloading trace state variable.")); | |
35b1e5cc SS |
10743 | } |
10744 | ||
d248b706 KY |
10745 | static void |
10746 | remote_enable_tracepoint (struct bp_location *location) | |
10747 | { | |
10748 | struct remote_state *rs = get_remote_state (); | |
10749 | char addr_buf[40]; | |
10750 | ||
10751 | sprintf_vma (addr_buf, location->address); | |
bba74b36 YQ |
10752 | xsnprintf (rs->buf, get_remote_packet_size (), "QTEnable:%x:%s", |
10753 | location->owner->number, addr_buf); | |
d248b706 KY |
10754 | putpkt (rs->buf); |
10755 | remote_get_noisy_reply (&rs->buf, &rs->buf_size); | |
10756 | if (*rs->buf == '\0') | |
10757 | error (_("Target does not support enabling tracepoints while a trace run is ongoing.")); | |
10758 | if (strcmp (rs->buf, "OK") != 0) | |
10759 | error (_("Error on target while enabling tracepoint.")); | |
10760 | } | |
10761 | ||
10762 | static void | |
10763 | remote_disable_tracepoint (struct bp_location *location) | |
10764 | { | |
10765 | struct remote_state *rs = get_remote_state (); | |
10766 | char addr_buf[40]; | |
10767 | ||
10768 | sprintf_vma (addr_buf, location->address); | |
bba74b36 YQ |
10769 | xsnprintf (rs->buf, get_remote_packet_size (), "QTDisable:%x:%s", |
10770 | location->owner->number, addr_buf); | |
d248b706 KY |
10771 | putpkt (rs->buf); |
10772 | remote_get_noisy_reply (&rs->buf, &rs->buf_size); | |
10773 | if (*rs->buf == '\0') | |
10774 | error (_("Target does not support disabling tracepoints while a trace run is ongoing.")); | |
10775 | if (strcmp (rs->buf, "OK") != 0) | |
10776 | error (_("Error on target while disabling tracepoint.")); | |
10777 | } | |
10778 | ||
35b1e5cc | 10779 | static void |
ad91cd99 | 10780 | remote_trace_set_readonly_regions (void) |
35b1e5cc SS |
10781 | { |
10782 | asection *s; | |
81b9b86e | 10783 | bfd *abfd = NULL; |
35b1e5cc | 10784 | bfd_size_type size; |
608bcef2 | 10785 | bfd_vma vma; |
35b1e5cc | 10786 | int anysecs = 0; |
c2fa21f1 | 10787 | int offset = 0; |
35b1e5cc SS |
10788 | |
10789 | if (!exec_bfd) | |
10790 | return; /* No information to give. */ | |
10791 | ||
10792 | strcpy (target_buf, "QTro"); | |
9779ab84 | 10793 | offset = strlen (target_buf); |
35b1e5cc SS |
10794 | for (s = exec_bfd->sections; s; s = s->next) |
10795 | { | |
10796 | char tmp1[40], tmp2[40]; | |
c2fa21f1 | 10797 | int sec_length; |
35b1e5cc SS |
10798 | |
10799 | if ((s->flags & SEC_LOAD) == 0 || | |
0df8b418 | 10800 | /* (s->flags & SEC_CODE) == 0 || */ |
35b1e5cc SS |
10801 | (s->flags & SEC_READONLY) == 0) |
10802 | continue; | |
10803 | ||
10804 | anysecs = 1; | |
81b9b86e | 10805 | vma = bfd_get_section_vma (abfd, s); |
35b1e5cc | 10806 | size = bfd_get_section_size (s); |
608bcef2 HZ |
10807 | sprintf_vma (tmp1, vma); |
10808 | sprintf_vma (tmp2, vma + size); | |
c2fa21f1 HZ |
10809 | sec_length = 1 + strlen (tmp1) + 1 + strlen (tmp2); |
10810 | if (offset + sec_length + 1 > target_buf_size) | |
10811 | { | |
864ac8a7 HZ |
10812 | if (remote_protocol_packets[PACKET_qXfer_traceframe_info].support |
10813 | != PACKET_ENABLE) | |
10814 | warning (_("\ | |
c2fa21f1 HZ |
10815 | Too many sections for read-only sections definition packet.")); |
10816 | break; | |
10817 | } | |
bba74b36 YQ |
10818 | xsnprintf (target_buf + offset, target_buf_size - offset, ":%s,%s", |
10819 | tmp1, tmp2); | |
c2fa21f1 | 10820 | offset += sec_length; |
35b1e5cc SS |
10821 | } |
10822 | if (anysecs) | |
10823 | { | |
10824 | putpkt (target_buf); | |
10825 | getpkt (&target_buf, &target_buf_size, 0); | |
10826 | } | |
10827 | } | |
10828 | ||
10829 | static void | |
ad91cd99 | 10830 | remote_trace_start (void) |
35b1e5cc SS |
10831 | { |
10832 | putpkt ("QTStart"); | |
10833 | remote_get_noisy_reply (&target_buf, &target_buf_size); | |
ad91cd99 PA |
10834 | if (*target_buf == '\0') |
10835 | error (_("Target does not support this command.")); | |
10836 | if (strcmp (target_buf, "OK") != 0) | |
35b1e5cc SS |
10837 | error (_("Bogus reply from target: %s"), target_buf); |
10838 | } | |
10839 | ||
10840 | static int | |
00bf0b85 | 10841 | remote_get_trace_status (struct trace_status *ts) |
35b1e5cc | 10842 | { |
953b98d1 | 10843 | /* Initialize it just to avoid a GCC false warning. */ |
f652de6f | 10844 | char *p = NULL; |
0df8b418 | 10845 | /* FIXME we need to get register block size some other way. */ |
00bf0b85 | 10846 | extern int trace_regblock_size; |
67f41397 | 10847 | volatile struct gdb_exception ex; |
bd3eecc3 PA |
10848 | enum packet_result result; |
10849 | ||
10850 | if (remote_protocol_packets[PACKET_qTStatus].support == PACKET_DISABLE) | |
10851 | return -1; | |
a744cf53 | 10852 | |
00bf0b85 SS |
10853 | trace_regblock_size = get_remote_arch_state ()->sizeof_g_packet; |
10854 | ||
049dc89b JK |
10855 | putpkt ("qTStatus"); |
10856 | ||
67f41397 JK |
10857 | TRY_CATCH (ex, RETURN_MASK_ERROR) |
10858 | { | |
10859 | p = remote_get_noisy_reply (&target_buf, &target_buf_size); | |
10860 | } | |
10861 | if (ex.reason < 0) | |
10862 | { | |
598d3636 JK |
10863 | if (ex.error != TARGET_CLOSE_ERROR) |
10864 | { | |
10865 | exception_fprintf (gdb_stderr, ex, "qTStatus: "); | |
10866 | return -1; | |
10867 | } | |
10868 | throw_exception (ex); | |
67f41397 | 10869 | } |
00bf0b85 | 10870 | |
bd3eecc3 PA |
10871 | result = packet_ok (p, &remote_protocol_packets[PACKET_qTStatus]); |
10872 | ||
00bf0b85 | 10873 | /* If the remote target doesn't do tracing, flag it. */ |
bd3eecc3 | 10874 | if (result == PACKET_UNKNOWN) |
00bf0b85 | 10875 | return -1; |
35b1e5cc | 10876 | |
00bf0b85 | 10877 | /* We're working with a live target. */ |
f5911ea1 | 10878 | ts->filename = NULL; |
00bf0b85 | 10879 | |
00bf0b85 | 10880 | if (*p++ != 'T') |
35b1e5cc SS |
10881 | error (_("Bogus trace status reply from target: %s"), target_buf); |
10882 | ||
84cebc4a YQ |
10883 | /* Function 'parse_trace_status' sets default value of each field of |
10884 | 'ts' at first, so we don't have to do it here. */ | |
00bf0b85 SS |
10885 | parse_trace_status (p, ts); |
10886 | ||
10887 | return ts->running; | |
35b1e5cc SS |
10888 | } |
10889 | ||
70221824 | 10890 | static void |
f196051f SS |
10891 | remote_get_tracepoint_status (struct breakpoint *bp, |
10892 | struct uploaded_tp *utp) | |
10893 | { | |
10894 | struct remote_state *rs = get_remote_state (); | |
f196051f SS |
10895 | char *reply; |
10896 | struct bp_location *loc; | |
10897 | struct tracepoint *tp = (struct tracepoint *) bp; | |
bba74b36 | 10898 | size_t size = get_remote_packet_size (); |
f196051f SS |
10899 | |
10900 | if (tp) | |
10901 | { | |
10902 | tp->base.hit_count = 0; | |
10903 | tp->traceframe_usage = 0; | |
10904 | for (loc = tp->base.loc; loc; loc = loc->next) | |
10905 | { | |
10906 | /* If the tracepoint was never downloaded, don't go asking for | |
10907 | any status. */ | |
10908 | if (tp->number_on_target == 0) | |
10909 | continue; | |
bba74b36 YQ |
10910 | xsnprintf (rs->buf, size, "qTP:%x:%s", tp->number_on_target, |
10911 | phex_nz (loc->address, 0)); | |
f196051f SS |
10912 | putpkt (rs->buf); |
10913 | reply = remote_get_noisy_reply (&target_buf, &target_buf_size); | |
10914 | if (reply && *reply) | |
10915 | { | |
10916 | if (*reply == 'V') | |
10917 | parse_tracepoint_status (reply + 1, bp, utp); | |
10918 | } | |
10919 | } | |
10920 | } | |
10921 | else if (utp) | |
10922 | { | |
10923 | utp->hit_count = 0; | |
10924 | utp->traceframe_usage = 0; | |
bba74b36 YQ |
10925 | xsnprintf (rs->buf, size, "qTP:%x:%s", utp->number, |
10926 | phex_nz (utp->addr, 0)); | |
f196051f SS |
10927 | putpkt (rs->buf); |
10928 | reply = remote_get_noisy_reply (&target_buf, &target_buf_size); | |
10929 | if (reply && *reply) | |
10930 | { | |
10931 | if (*reply == 'V') | |
10932 | parse_tracepoint_status (reply + 1, bp, utp); | |
10933 | } | |
10934 | } | |
10935 | } | |
10936 | ||
35b1e5cc | 10937 | static void |
ad91cd99 | 10938 | remote_trace_stop (void) |
35b1e5cc SS |
10939 | { |
10940 | putpkt ("QTStop"); | |
10941 | remote_get_noisy_reply (&target_buf, &target_buf_size); | |
ad91cd99 PA |
10942 | if (*target_buf == '\0') |
10943 | error (_("Target does not support this command.")); | |
10944 | if (strcmp (target_buf, "OK") != 0) | |
35b1e5cc SS |
10945 | error (_("Bogus reply from target: %s"), target_buf); |
10946 | } | |
10947 | ||
10948 | static int | |
10949 | remote_trace_find (enum trace_find_type type, int num, | |
cc5925ad | 10950 | CORE_ADDR addr1, CORE_ADDR addr2, |
35b1e5cc SS |
10951 | int *tpp) |
10952 | { | |
10953 | struct remote_state *rs = get_remote_state (); | |
bba74b36 | 10954 | char *endbuf = rs->buf + get_remote_packet_size (); |
35b1e5cc SS |
10955 | char *p, *reply; |
10956 | int target_frameno = -1, target_tracept = -1; | |
10957 | ||
e6e4e701 PA |
10958 | /* Lookups other than by absolute frame number depend on the current |
10959 | trace selected, so make sure it is correct on the remote end | |
10960 | first. */ | |
10961 | if (type != tfind_number) | |
10962 | set_remote_traceframe (); | |
10963 | ||
35b1e5cc SS |
10964 | p = rs->buf; |
10965 | strcpy (p, "QTFrame:"); | |
10966 | p = strchr (p, '\0'); | |
10967 | switch (type) | |
10968 | { | |
10969 | case tfind_number: | |
bba74b36 | 10970 | xsnprintf (p, endbuf - p, "%x", num); |
35b1e5cc SS |
10971 | break; |
10972 | case tfind_pc: | |
bba74b36 | 10973 | xsnprintf (p, endbuf - p, "pc:%s", phex_nz (addr1, 0)); |
35b1e5cc SS |
10974 | break; |
10975 | case tfind_tp: | |
bba74b36 | 10976 | xsnprintf (p, endbuf - p, "tdp:%x", num); |
35b1e5cc SS |
10977 | break; |
10978 | case tfind_range: | |
bba74b36 YQ |
10979 | xsnprintf (p, endbuf - p, "range:%s:%s", phex_nz (addr1, 0), |
10980 | phex_nz (addr2, 0)); | |
35b1e5cc SS |
10981 | break; |
10982 | case tfind_outside: | |
bba74b36 YQ |
10983 | xsnprintf (p, endbuf - p, "outside:%s:%s", phex_nz (addr1, 0), |
10984 | phex_nz (addr2, 0)); | |
35b1e5cc SS |
10985 | break; |
10986 | default: | |
9b20d036 | 10987 | error (_("Unknown trace find type %d"), type); |
35b1e5cc SS |
10988 | } |
10989 | ||
10990 | putpkt (rs->buf); | |
2f65bcb7 | 10991 | reply = remote_get_noisy_reply (&(rs->buf), &rs->buf_size); |
ad91cd99 PA |
10992 | if (*reply == '\0') |
10993 | error (_("Target does not support this command.")); | |
35b1e5cc SS |
10994 | |
10995 | while (reply && *reply) | |
10996 | switch (*reply) | |
10997 | { | |
10998 | case 'F': | |
f197e0f1 VP |
10999 | p = ++reply; |
11000 | target_frameno = (int) strtol (p, &reply, 16); | |
11001 | if (reply == p) | |
11002 | error (_("Unable to parse trace frame number")); | |
e6e4e701 PA |
11003 | /* Don't update our remote traceframe number cache on failure |
11004 | to select a remote traceframe. */ | |
f197e0f1 VP |
11005 | if (target_frameno == -1) |
11006 | return -1; | |
35b1e5cc SS |
11007 | break; |
11008 | case 'T': | |
f197e0f1 VP |
11009 | p = ++reply; |
11010 | target_tracept = (int) strtol (p, &reply, 16); | |
11011 | if (reply == p) | |
11012 | error (_("Unable to parse tracepoint number")); | |
35b1e5cc SS |
11013 | break; |
11014 | case 'O': /* "OK"? */ | |
11015 | if (reply[1] == 'K' && reply[2] == '\0') | |
11016 | reply += 2; | |
11017 | else | |
11018 | error (_("Bogus reply from target: %s"), reply); | |
11019 | break; | |
11020 | default: | |
11021 | error (_("Bogus reply from target: %s"), reply); | |
11022 | } | |
11023 | if (tpp) | |
11024 | *tpp = target_tracept; | |
e6e4e701 | 11025 | |
262e1174 | 11026 | rs->remote_traceframe_number = target_frameno; |
35b1e5cc SS |
11027 | return target_frameno; |
11028 | } | |
11029 | ||
11030 | static int | |
11031 | remote_get_trace_state_variable_value (int tsvnum, LONGEST *val) | |
11032 | { | |
11033 | struct remote_state *rs = get_remote_state (); | |
11034 | char *reply; | |
11035 | ULONGEST uval; | |
11036 | ||
e6e4e701 PA |
11037 | set_remote_traceframe (); |
11038 | ||
bba74b36 | 11039 | xsnprintf (rs->buf, get_remote_packet_size (), "qTV:%x", tsvnum); |
35b1e5cc SS |
11040 | putpkt (rs->buf); |
11041 | reply = remote_get_noisy_reply (&target_buf, &target_buf_size); | |
11042 | if (reply && *reply) | |
11043 | { | |
11044 | if (*reply == 'V') | |
11045 | { | |
11046 | unpack_varlen_hex (reply + 1, &uval); | |
11047 | *val = (LONGEST) uval; | |
11048 | return 1; | |
11049 | } | |
11050 | } | |
11051 | return 0; | |
11052 | } | |
11053 | ||
00bf0b85 | 11054 | static int |
011aacb0 | 11055 | remote_save_trace_data (const char *filename) |
00bf0b85 SS |
11056 | { |
11057 | struct remote_state *rs = get_remote_state (); | |
11058 | char *p, *reply; | |
11059 | ||
11060 | p = rs->buf; | |
11061 | strcpy (p, "QTSave:"); | |
11062 | p += strlen (p); | |
11063 | if ((p - rs->buf) + strlen (filename) * 2 >= get_remote_packet_size ()) | |
11064 | error (_("Remote file name too long for trace save packet")); | |
11065 | p += 2 * bin2hex ((gdb_byte *) filename, p, 0); | |
11066 | *p++ = '\0'; | |
11067 | putpkt (rs->buf); | |
ad91cd99 | 11068 | reply = remote_get_noisy_reply (&target_buf, &target_buf_size); |
d6c5869f | 11069 | if (*reply == '\0') |
ad91cd99 PA |
11070 | error (_("Target does not support this command.")); |
11071 | if (strcmp (reply, "OK") != 0) | |
11072 | error (_("Bogus reply from target: %s"), reply); | |
00bf0b85 SS |
11073 | return 0; |
11074 | } | |
11075 | ||
11076 | /* This is basically a memory transfer, but needs to be its own packet | |
11077 | because we don't know how the target actually organizes its trace | |
11078 | memory, plus we want to be able to ask for as much as possible, but | |
11079 | not be unhappy if we don't get as much as we ask for. */ | |
11080 | ||
11081 | static LONGEST | |
11082 | remote_get_raw_trace_data (gdb_byte *buf, ULONGEST offset, LONGEST len) | |
11083 | { | |
11084 | struct remote_state *rs = get_remote_state (); | |
11085 | char *reply; | |
11086 | char *p; | |
11087 | int rslt; | |
11088 | ||
11089 | p = rs->buf; | |
11090 | strcpy (p, "qTBuffer:"); | |
11091 | p += strlen (p); | |
11092 | p += hexnumstr (p, offset); | |
11093 | *p++ = ','; | |
11094 | p += hexnumstr (p, len); | |
11095 | *p++ = '\0'; | |
11096 | ||
11097 | putpkt (rs->buf); | |
11098 | reply = remote_get_noisy_reply (&target_buf, &target_buf_size); | |
11099 | if (reply && *reply) | |
11100 | { | |
11101 | /* 'l' by itself means we're at the end of the buffer and | |
11102 | there is nothing more to get. */ | |
11103 | if (*reply == 'l') | |
11104 | return 0; | |
11105 | ||
11106 | /* Convert the reply into binary. Limit the number of bytes to | |
11107 | convert according to our passed-in buffer size, rather than | |
11108 | what was returned in the packet; if the target is | |
11109 | unexpectedly generous and gives us a bigger reply than we | |
11110 | asked for, we don't want to crash. */ | |
11111 | rslt = hex2bin (target_buf, buf, len); | |
11112 | return rslt; | |
11113 | } | |
11114 | ||
11115 | /* Something went wrong, flag as an error. */ | |
11116 | return -1; | |
11117 | } | |
11118 | ||
35b1e5cc SS |
11119 | static void |
11120 | remote_set_disconnected_tracing (int val) | |
11121 | { | |
11122 | struct remote_state *rs = get_remote_state (); | |
11123 | ||
33da3f1c SS |
11124 | if (rs->disconnected_tracing) |
11125 | { | |
ad91cd99 PA |
11126 | char *reply; |
11127 | ||
bba74b36 | 11128 | xsnprintf (rs->buf, get_remote_packet_size (), "QTDisconnected:%x", val); |
33da3f1c | 11129 | putpkt (rs->buf); |
ad91cd99 PA |
11130 | reply = remote_get_noisy_reply (&target_buf, &target_buf_size); |
11131 | if (*reply == '\0') | |
33da3f1c | 11132 | error (_("Target does not support this command.")); |
ad91cd99 PA |
11133 | if (strcmp (reply, "OK") != 0) |
11134 | error (_("Bogus reply from target: %s"), reply); | |
33da3f1c SS |
11135 | } |
11136 | else if (val) | |
11137 | warning (_("Target does not support disconnected tracing.")); | |
35b1e5cc SS |
11138 | } |
11139 | ||
dc146f7c VP |
11140 | static int |
11141 | remote_core_of_thread (struct target_ops *ops, ptid_t ptid) | |
11142 | { | |
11143 | struct thread_info *info = find_thread_ptid (ptid); | |
a744cf53 | 11144 | |
dc146f7c VP |
11145 | if (info && info->private) |
11146 | return info->private->core; | |
11147 | return -1; | |
11148 | } | |
11149 | ||
4daf5ac0 SS |
11150 | static void |
11151 | remote_set_circular_trace_buffer (int val) | |
11152 | { | |
11153 | struct remote_state *rs = get_remote_state (); | |
ad91cd99 | 11154 | char *reply; |
4daf5ac0 | 11155 | |
bba74b36 | 11156 | xsnprintf (rs->buf, get_remote_packet_size (), "QTBuffer:circular:%x", val); |
4daf5ac0 | 11157 | putpkt (rs->buf); |
ad91cd99 PA |
11158 | reply = remote_get_noisy_reply (&target_buf, &target_buf_size); |
11159 | if (*reply == '\0') | |
4daf5ac0 | 11160 | error (_("Target does not support this command.")); |
ad91cd99 PA |
11161 | if (strcmp (reply, "OK") != 0) |
11162 | error (_("Bogus reply from target: %s"), reply); | |
4daf5ac0 SS |
11163 | } |
11164 | ||
b3b9301e PA |
11165 | static struct traceframe_info * |
11166 | remote_traceframe_info (void) | |
11167 | { | |
11168 | char *text; | |
11169 | ||
6c177e28 YQ |
11170 | /* If current traceframe is not selected, don't bother the remote |
11171 | stub. */ | |
11172 | if (get_traceframe_number () < 0) | |
11173 | return NULL; | |
11174 | ||
b3b9301e PA |
11175 | text = target_read_stralloc (¤t_target, |
11176 | TARGET_OBJECT_TRACEFRAME_INFO, NULL); | |
11177 | if (text != NULL) | |
11178 | { | |
11179 | struct traceframe_info *info; | |
11180 | struct cleanup *back_to = make_cleanup (xfree, text); | |
11181 | ||
11182 | info = parse_traceframe_info (text); | |
11183 | do_cleanups (back_to); | |
11184 | return info; | |
11185 | } | |
11186 | ||
11187 | return NULL; | |
11188 | } | |
11189 | ||
405f8e94 SS |
11190 | /* Handle the qTMinFTPILen packet. Returns the minimum length of |
11191 | instruction on which a fast tracepoint may be placed. Returns -1 | |
11192 | if the packet is not supported, and 0 if the minimum instruction | |
11193 | length is unknown. */ | |
11194 | ||
11195 | static int | |
11196 | remote_get_min_fast_tracepoint_insn_len (void) | |
11197 | { | |
11198 | struct remote_state *rs = get_remote_state (); | |
11199 | char *reply; | |
11200 | ||
e886a173 PA |
11201 | /* If we're not debugging a process yet, the IPA can't be |
11202 | loaded. */ | |
11203 | if (!target_has_execution) | |
11204 | return 0; | |
11205 | ||
11206 | /* Make sure the remote is pointing at the right process. */ | |
11207 | set_general_process (); | |
11208 | ||
bba74b36 | 11209 | xsnprintf (rs->buf, get_remote_packet_size (), "qTMinFTPILen"); |
405f8e94 SS |
11210 | putpkt (rs->buf); |
11211 | reply = remote_get_noisy_reply (&target_buf, &target_buf_size); | |
11212 | if (*reply == '\0') | |
11213 | return -1; | |
11214 | else | |
11215 | { | |
11216 | ULONGEST min_insn_len; | |
11217 | ||
11218 | unpack_varlen_hex (reply, &min_insn_len); | |
11219 | ||
11220 | return (int) min_insn_len; | |
11221 | } | |
11222 | } | |
11223 | ||
f6f899bf HAQ |
11224 | static void |
11225 | remote_set_trace_buffer_size (LONGEST val) | |
11226 | { | |
90585175 HAQ |
11227 | if (remote_protocol_packets[PACKET_QTBuffer_size].support |
11228 | != PACKET_DISABLE) | |
f6f899bf HAQ |
11229 | { |
11230 | struct remote_state *rs = get_remote_state (); | |
11231 | char *buf = rs->buf; | |
11232 | char *endbuf = rs->buf + get_remote_packet_size (); | |
11233 | enum packet_result result; | |
11234 | ||
11235 | gdb_assert (val >= 0 || val == -1); | |
11236 | buf += xsnprintf (buf, endbuf - buf, "QTBuffer:size:"); | |
11237 | /* Send -1 as literal "-1" to avoid host size dependency. */ | |
11238 | if (val < 0) | |
11239 | { | |
11240 | *buf++ = '-'; | |
11241 | buf += hexnumstr (buf, (ULONGEST) -val); | |
11242 | } | |
11243 | else | |
11244 | buf += hexnumstr (buf, (ULONGEST) val); | |
11245 | ||
11246 | putpkt (rs->buf); | |
11247 | remote_get_noisy_reply (&rs->buf, &rs->buf_size); | |
11248 | result = packet_ok (rs->buf, | |
11249 | &remote_protocol_packets[PACKET_QTBuffer_size]); | |
11250 | ||
11251 | if (result != PACKET_OK) | |
11252 | warning (_("Bogus reply from target: %s"), rs->buf); | |
11253 | } | |
11254 | } | |
11255 | ||
f196051f | 11256 | static int |
ca623f82 TT |
11257 | remote_set_trace_notes (const char *user, const char *notes, |
11258 | const char *stop_notes) | |
f196051f SS |
11259 | { |
11260 | struct remote_state *rs = get_remote_state (); | |
11261 | char *reply; | |
11262 | char *buf = rs->buf; | |
11263 | char *endbuf = rs->buf + get_remote_packet_size (); | |
11264 | int nbytes; | |
11265 | ||
11266 | buf += xsnprintf (buf, endbuf - buf, "QTNotes:"); | |
11267 | if (user) | |
11268 | { | |
11269 | buf += xsnprintf (buf, endbuf - buf, "user:"); | |
bc20a4af | 11270 | nbytes = bin2hex ((gdb_byte *) user, buf, 0); |
f196051f SS |
11271 | buf += 2 * nbytes; |
11272 | *buf++ = ';'; | |
11273 | } | |
11274 | if (notes) | |
11275 | { | |
11276 | buf += xsnprintf (buf, endbuf - buf, "notes:"); | |
bc20a4af | 11277 | nbytes = bin2hex ((gdb_byte *) notes, buf, 0); |
f196051f SS |
11278 | buf += 2 * nbytes; |
11279 | *buf++ = ';'; | |
11280 | } | |
11281 | if (stop_notes) | |
11282 | { | |
11283 | buf += xsnprintf (buf, endbuf - buf, "tstop:"); | |
bc20a4af | 11284 | nbytes = bin2hex ((gdb_byte *) stop_notes, buf, 0); |
f196051f SS |
11285 | buf += 2 * nbytes; |
11286 | *buf++ = ';'; | |
11287 | } | |
11288 | /* Ensure the buffer is terminated. */ | |
11289 | *buf = '\0'; | |
11290 | ||
11291 | putpkt (rs->buf); | |
11292 | reply = remote_get_noisy_reply (&target_buf, &target_buf_size); | |
11293 | if (*reply == '\0') | |
11294 | return 0; | |
11295 | ||
11296 | if (strcmp (reply, "OK") != 0) | |
11297 | error (_("Bogus reply from target: %s"), reply); | |
11298 | ||
11299 | return 1; | |
11300 | } | |
11301 | ||
d1feda86 YQ |
11302 | static int |
11303 | remote_use_agent (int use) | |
11304 | { | |
11305 | if (remote_protocol_packets[PACKET_QAgent].support != PACKET_DISABLE) | |
11306 | { | |
11307 | struct remote_state *rs = get_remote_state (); | |
11308 | ||
11309 | /* If the stub supports QAgent. */ | |
bba74b36 | 11310 | xsnprintf (rs->buf, get_remote_packet_size (), "QAgent:%d", use); |
d1feda86 YQ |
11311 | putpkt (rs->buf); |
11312 | getpkt (&rs->buf, &rs->buf_size, 0); | |
11313 | ||
11314 | if (strcmp (rs->buf, "OK") == 0) | |
11315 | { | |
11316 | use_agent = use; | |
11317 | return 1; | |
11318 | } | |
11319 | } | |
11320 | ||
11321 | return 0; | |
11322 | } | |
11323 | ||
11324 | static int | |
11325 | remote_can_use_agent (void) | |
11326 | { | |
11327 | return (remote_protocol_packets[PACKET_QAgent].support != PACKET_DISABLE); | |
11328 | } | |
11329 | ||
9accd112 MM |
11330 | struct btrace_target_info |
11331 | { | |
11332 | /* The ptid of the traced thread. */ | |
11333 | ptid_t ptid; | |
11334 | }; | |
11335 | ||
11336 | /* Check whether the target supports branch tracing. */ | |
11337 | ||
11338 | static int | |
11339 | remote_supports_btrace (void) | |
11340 | { | |
11341 | if (remote_protocol_packets[PACKET_Qbtrace_off].support != PACKET_ENABLE) | |
11342 | return 0; | |
11343 | if (remote_protocol_packets[PACKET_Qbtrace_bts].support != PACKET_ENABLE) | |
11344 | return 0; | |
11345 | if (remote_protocol_packets[PACKET_qXfer_btrace].support != PACKET_ENABLE) | |
11346 | return 0; | |
11347 | ||
11348 | return 1; | |
11349 | } | |
11350 | ||
11351 | /* Enable branch tracing. */ | |
11352 | ||
11353 | static struct btrace_target_info * | |
11354 | remote_enable_btrace (ptid_t ptid) | |
11355 | { | |
11356 | struct btrace_target_info *tinfo = NULL; | |
11357 | struct packet_config *packet = &remote_protocol_packets[PACKET_Qbtrace_bts]; | |
11358 | struct remote_state *rs = get_remote_state (); | |
11359 | char *buf = rs->buf; | |
11360 | char *endbuf = rs->buf + get_remote_packet_size (); | |
11361 | ||
11362 | if (packet->support != PACKET_ENABLE) | |
11363 | error (_("Target does not support branch tracing.")); | |
11364 | ||
11365 | set_general_thread (ptid); | |
11366 | ||
11367 | buf += xsnprintf (buf, endbuf - buf, "%s", packet->name); | |
11368 | putpkt (rs->buf); | |
11369 | getpkt (&rs->buf, &rs->buf_size, 0); | |
11370 | ||
11371 | if (packet_ok (rs->buf, packet) == PACKET_ERROR) | |
11372 | { | |
11373 | if (rs->buf[0] == 'E' && rs->buf[1] == '.') | |
11374 | error (_("Could not enable branch tracing for %s: %s"), | |
11375 | target_pid_to_str (ptid), rs->buf + 2); | |
11376 | else | |
11377 | error (_("Could not enable branch tracing for %s."), | |
11378 | target_pid_to_str (ptid)); | |
11379 | } | |
11380 | ||
11381 | tinfo = xzalloc (sizeof (*tinfo)); | |
11382 | tinfo->ptid = ptid; | |
11383 | ||
11384 | return tinfo; | |
11385 | } | |
11386 | ||
11387 | /* Disable branch tracing. */ | |
11388 | ||
11389 | static void | |
11390 | remote_disable_btrace (struct btrace_target_info *tinfo) | |
11391 | { | |
11392 | struct packet_config *packet = &remote_protocol_packets[PACKET_Qbtrace_off]; | |
11393 | struct remote_state *rs = get_remote_state (); | |
11394 | char *buf = rs->buf; | |
11395 | char *endbuf = rs->buf + get_remote_packet_size (); | |
11396 | ||
11397 | if (packet->support != PACKET_ENABLE) | |
11398 | error (_("Target does not support branch tracing.")); | |
11399 | ||
11400 | set_general_thread (tinfo->ptid); | |
11401 | ||
11402 | buf += xsnprintf (buf, endbuf - buf, "%s", packet->name); | |
11403 | putpkt (rs->buf); | |
11404 | getpkt (&rs->buf, &rs->buf_size, 0); | |
11405 | ||
11406 | if (packet_ok (rs->buf, packet) == PACKET_ERROR) | |
11407 | { | |
11408 | if (rs->buf[0] == 'E' && rs->buf[1] == '.') | |
11409 | error (_("Could not disable branch tracing for %s: %s"), | |
11410 | target_pid_to_str (tinfo->ptid), rs->buf + 2); | |
11411 | else | |
11412 | error (_("Could not disable branch tracing for %s."), | |
11413 | target_pid_to_str (tinfo->ptid)); | |
11414 | } | |
11415 | ||
11416 | xfree (tinfo); | |
11417 | } | |
11418 | ||
11419 | /* Teardown branch tracing. */ | |
11420 | ||
11421 | static void | |
11422 | remote_teardown_btrace (struct btrace_target_info *tinfo) | |
11423 | { | |
11424 | /* We must not talk to the target during teardown. */ | |
11425 | xfree (tinfo); | |
11426 | } | |
11427 | ||
11428 | /* Read the branch trace. */ | |
11429 | ||
11430 | static VEC (btrace_block_s) * | |
11431 | remote_read_btrace (struct btrace_target_info *tinfo, | |
11432 | enum btrace_read_type type) | |
11433 | { | |
11434 | struct packet_config *packet = &remote_protocol_packets[PACKET_qXfer_btrace]; | |
11435 | struct remote_state *rs = get_remote_state (); | |
11436 | VEC (btrace_block_s) *btrace = NULL; | |
11437 | const char *annex; | |
11438 | char *xml; | |
11439 | ||
11440 | if (packet->support != PACKET_ENABLE) | |
11441 | error (_("Target does not support branch tracing.")); | |
11442 | ||
11443 | #if !defined(HAVE_LIBEXPAT) | |
11444 | error (_("Cannot process branch tracing result. XML parsing not supported.")); | |
11445 | #endif | |
11446 | ||
11447 | switch (type) | |
11448 | { | |
11449 | case btrace_read_all: | |
11450 | annex = "all"; | |
11451 | break; | |
11452 | case btrace_read_new: | |
11453 | annex = "new"; | |
11454 | break; | |
11455 | default: | |
11456 | internal_error (__FILE__, __LINE__, | |
11457 | _("Bad branch tracing read type: %u."), | |
11458 | (unsigned int) type); | |
11459 | } | |
11460 | ||
11461 | xml = target_read_stralloc (¤t_target, | |
11462 | TARGET_OBJECT_BTRACE, annex); | |
11463 | if (xml != NULL) | |
11464 | { | |
11465 | struct cleanup *cleanup = make_cleanup (xfree, xml); | |
11466 | ||
11467 | btrace = parse_xml_btrace (xml); | |
11468 | do_cleanups (cleanup); | |
11469 | } | |
11470 | ||
11471 | return btrace; | |
11472 | } | |
11473 | ||
ced63ec0 GB |
11474 | static int |
11475 | remote_augmented_libraries_svr4_read (void) | |
11476 | { | |
11477 | struct remote_state *rs = get_remote_state (); | |
11478 | ||
11479 | return rs->augmented_libraries_svr4_read; | |
11480 | } | |
11481 | ||
c906108c | 11482 | static void |
fba45db2 | 11483 | init_remote_ops (void) |
c906108c | 11484 | { |
c5aa993b | 11485 | remote_ops.to_shortname = "remote"; |
c906108c | 11486 | remote_ops.to_longname = "Remote serial target in gdb-specific protocol"; |
c5aa993b | 11487 | remote_ops.to_doc = |
c906108c | 11488 | "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ |
0d06e24b JM |
11489 | Specify the serial device it is connected to\n\ |
11490 | (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."; | |
c5aa993b JM |
11491 | remote_ops.to_open = remote_open; |
11492 | remote_ops.to_close = remote_close; | |
c906108c | 11493 | remote_ops.to_detach = remote_detach; |
6ad8ae5c | 11494 | remote_ops.to_disconnect = remote_disconnect; |
c5aa993b | 11495 | remote_ops.to_resume = remote_resume; |
c906108c SS |
11496 | remote_ops.to_wait = remote_wait; |
11497 | remote_ops.to_fetch_registers = remote_fetch_registers; | |
11498 | remote_ops.to_store_registers = remote_store_registers; | |
11499 | remote_ops.to_prepare_to_store = remote_prepare_to_store; | |
c5aa993b | 11500 | remote_ops.to_files_info = remote_files_info; |
c906108c SS |
11501 | remote_ops.to_insert_breakpoint = remote_insert_breakpoint; |
11502 | remote_ops.to_remove_breakpoint = remote_remove_breakpoint; | |
3c3bea1c GS |
11503 | remote_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint; |
11504 | remote_ops.to_stopped_data_address = remote_stopped_data_address; | |
283002cf MR |
11505 | remote_ops.to_watchpoint_addr_within_range = |
11506 | remote_watchpoint_addr_within_range; | |
3c3bea1c GS |
11507 | remote_ops.to_can_use_hw_breakpoint = remote_check_watch_resources; |
11508 | remote_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint; | |
11509 | remote_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint; | |
480a3f21 PW |
11510 | remote_ops.to_region_ok_for_hw_watchpoint |
11511 | = remote_region_ok_for_hw_watchpoint; | |
3c3bea1c GS |
11512 | remote_ops.to_insert_watchpoint = remote_insert_watchpoint; |
11513 | remote_ops.to_remove_watchpoint = remote_remove_watchpoint; | |
c5aa993b JM |
11514 | remote_ops.to_kill = remote_kill; |
11515 | remote_ops.to_load = generic_load; | |
c906108c | 11516 | remote_ops.to_mourn_inferior = remote_mourn; |
2455069d | 11517 | remote_ops.to_pass_signals = remote_pass_signals; |
9b224c5e | 11518 | remote_ops.to_program_signals = remote_program_signals; |
c906108c | 11519 | remote_ops.to_thread_alive = remote_thread_alive; |
0f71a2f6 | 11520 | remote_ops.to_find_new_threads = remote_threads_info; |
0caabb7e | 11521 | remote_ops.to_pid_to_str = remote_pid_to_str; |
cf759d3b | 11522 | remote_ops.to_extra_thread_info = remote_threads_extra_info; |
10760264 | 11523 | remote_ops.to_get_ada_task_ptid = remote_get_ada_task_ptid; |
c906108c | 11524 | remote_ops.to_stop = remote_stop; |
4b8a223f | 11525 | remote_ops.to_xfer_partial = remote_xfer_partial; |
96baa820 | 11526 | remote_ops.to_rcmd = remote_rcmd; |
49d03eab | 11527 | remote_ops.to_log_command = serial_log_command; |
38691318 | 11528 | remote_ops.to_get_thread_local_address = remote_get_thread_local_address; |
c906108c | 11529 | remote_ops.to_stratum = process_stratum; |
c35b1492 PA |
11530 | remote_ops.to_has_all_memory = default_child_has_all_memory; |
11531 | remote_ops.to_has_memory = default_child_has_memory; | |
11532 | remote_ops.to_has_stack = default_child_has_stack; | |
11533 | remote_ops.to_has_registers = default_child_has_registers; | |
11534 | remote_ops.to_has_execution = default_child_has_execution; | |
3e43a32a | 11535 | remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */ |
b2175913 | 11536 | remote_ops.to_can_execute_reverse = remote_can_execute_reverse; |
c5aa993b | 11537 | remote_ops.to_magic = OPS_MAGIC; |
fd79ecee | 11538 | remote_ops.to_memory_map = remote_memory_map; |
a76d924d DJ |
11539 | remote_ops.to_flash_erase = remote_flash_erase; |
11540 | remote_ops.to_flash_done = remote_flash_done; | |
29709017 | 11541 | remote_ops.to_read_description = remote_read_description; |
08388c79 | 11542 | remote_ops.to_search_memory = remote_search_memory; |
75c99385 PA |
11543 | remote_ops.to_can_async_p = remote_can_async_p; |
11544 | remote_ops.to_is_async_p = remote_is_async_p; | |
11545 | remote_ops.to_async = remote_async; | |
75c99385 PA |
11546 | remote_ops.to_terminal_inferior = remote_terminal_inferior; |
11547 | remote_ops.to_terminal_ours = remote_terminal_ours; | |
74531fed | 11548 | remote_ops.to_supports_non_stop = remote_supports_non_stop; |
8a305172 | 11549 | remote_ops.to_supports_multi_process = remote_supports_multi_process; |
03583c20 UW |
11550 | remote_ops.to_supports_disable_randomization |
11551 | = remote_supports_disable_randomization; | |
7313baad UW |
11552 | remote_ops.to_fileio_open = remote_hostio_open; |
11553 | remote_ops.to_fileio_pwrite = remote_hostio_pwrite; | |
11554 | remote_ops.to_fileio_pread = remote_hostio_pread; | |
11555 | remote_ops.to_fileio_close = remote_hostio_close; | |
11556 | remote_ops.to_fileio_unlink = remote_hostio_unlink; | |
b9e7b9c3 | 11557 | remote_ops.to_fileio_readlink = remote_hostio_readlink; |
d248b706 | 11558 | remote_ops.to_supports_enable_disable_tracepoint = remote_supports_enable_disable_tracepoint; |
3065dfb6 | 11559 | remote_ops.to_supports_string_tracing = remote_supports_string_tracing; |
b775012e | 11560 | remote_ops.to_supports_evaluation_of_breakpoint_conditions = remote_supports_cond_breakpoints; |
d3ce09f5 | 11561 | remote_ops.to_can_run_breakpoint_commands = remote_can_run_breakpoint_commands; |
35b1e5cc SS |
11562 | remote_ops.to_trace_init = remote_trace_init; |
11563 | remote_ops.to_download_tracepoint = remote_download_tracepoint; | |
1e4d1764 | 11564 | remote_ops.to_can_download_tracepoint = remote_can_download_tracepoint; |
3e43a32a MS |
11565 | remote_ops.to_download_trace_state_variable |
11566 | = remote_download_trace_state_variable; | |
d248b706 KY |
11567 | remote_ops.to_enable_tracepoint = remote_enable_tracepoint; |
11568 | remote_ops.to_disable_tracepoint = remote_disable_tracepoint; | |
35b1e5cc SS |
11569 | remote_ops.to_trace_set_readonly_regions = remote_trace_set_readonly_regions; |
11570 | remote_ops.to_trace_start = remote_trace_start; | |
11571 | remote_ops.to_get_trace_status = remote_get_trace_status; | |
f196051f | 11572 | remote_ops.to_get_tracepoint_status = remote_get_tracepoint_status; |
35b1e5cc SS |
11573 | remote_ops.to_trace_stop = remote_trace_stop; |
11574 | remote_ops.to_trace_find = remote_trace_find; | |
3e43a32a MS |
11575 | remote_ops.to_get_trace_state_variable_value |
11576 | = remote_get_trace_state_variable_value; | |
00bf0b85 SS |
11577 | remote_ops.to_save_trace_data = remote_save_trace_data; |
11578 | remote_ops.to_upload_tracepoints = remote_upload_tracepoints; | |
3e43a32a MS |
11579 | remote_ops.to_upload_trace_state_variables |
11580 | = remote_upload_trace_state_variables; | |
00bf0b85 | 11581 | remote_ops.to_get_raw_trace_data = remote_get_raw_trace_data; |
405f8e94 | 11582 | remote_ops.to_get_min_fast_tracepoint_insn_len = remote_get_min_fast_tracepoint_insn_len; |
35b1e5cc | 11583 | remote_ops.to_set_disconnected_tracing = remote_set_disconnected_tracing; |
4daf5ac0 | 11584 | remote_ops.to_set_circular_trace_buffer = remote_set_circular_trace_buffer; |
f6f899bf | 11585 | remote_ops.to_set_trace_buffer_size = remote_set_trace_buffer_size; |
f196051f | 11586 | remote_ops.to_set_trace_notes = remote_set_trace_notes; |
dc146f7c | 11587 | remote_ops.to_core_of_thread = remote_core_of_thread; |
4a5e7a5b | 11588 | remote_ops.to_verify_memory = remote_verify_memory; |
711e434b | 11589 | remote_ops.to_get_tib_address = remote_get_tib_address; |
d914c394 | 11590 | remote_ops.to_set_permissions = remote_set_permissions; |
0fb4aa4b PA |
11591 | remote_ops.to_static_tracepoint_marker_at |
11592 | = remote_static_tracepoint_marker_at; | |
11593 | remote_ops.to_static_tracepoint_markers_by_strid | |
11594 | = remote_static_tracepoint_markers_by_strid; | |
b3b9301e | 11595 | remote_ops.to_traceframe_info = remote_traceframe_info; |
d1feda86 YQ |
11596 | remote_ops.to_use_agent = remote_use_agent; |
11597 | remote_ops.to_can_use_agent = remote_can_use_agent; | |
9accd112 MM |
11598 | remote_ops.to_supports_btrace = remote_supports_btrace; |
11599 | remote_ops.to_enable_btrace = remote_enable_btrace; | |
11600 | remote_ops.to_disable_btrace = remote_disable_btrace; | |
11601 | remote_ops.to_teardown_btrace = remote_teardown_btrace; | |
11602 | remote_ops.to_read_btrace = remote_read_btrace; | |
ced63ec0 GB |
11603 | remote_ops.to_augmented_libraries_svr4_read = |
11604 | remote_augmented_libraries_svr4_read; | |
c906108c SS |
11605 | } |
11606 | ||
11607 | /* Set up the extended remote vector by making a copy of the standard | |
11608 | remote vector and adding to it. */ | |
11609 | ||
11610 | static void | |
fba45db2 | 11611 | init_extended_remote_ops (void) |
c906108c SS |
11612 | { |
11613 | extended_remote_ops = remote_ops; | |
11614 | ||
0f71a2f6 | 11615 | extended_remote_ops.to_shortname = "extended-remote"; |
c5aa993b | 11616 | extended_remote_ops.to_longname = |
c906108c | 11617 | "Extended remote serial target in gdb-specific protocol"; |
c5aa993b | 11618 | extended_remote_ops.to_doc = |
c906108c | 11619 | "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ |
39237dd1 PA |
11620 | Specify the serial device it is connected to (e.g. /dev/ttya)."; |
11621 | extended_remote_ops.to_open = extended_remote_open; | |
c906108c SS |
11622 | extended_remote_ops.to_create_inferior = extended_remote_create_inferior; |
11623 | extended_remote_ops.to_mourn_inferior = extended_remote_mourn; | |
2d717e4f DJ |
11624 | extended_remote_ops.to_detach = extended_remote_detach; |
11625 | extended_remote_ops.to_attach = extended_remote_attach; | |
82f73884 | 11626 | extended_remote_ops.to_kill = extended_remote_kill; |
03583c20 UW |
11627 | extended_remote_ops.to_supports_disable_randomization |
11628 | = extended_remote_supports_disable_randomization; | |
0f71a2f6 JM |
11629 | } |
11630 | ||
6426a772 JM |
11631 | static int |
11632 | remote_can_async_p (void) | |
11633 | { | |
5d93a237 TT |
11634 | struct remote_state *rs = get_remote_state (); |
11635 | ||
c6ebd6cf | 11636 | if (!target_async_permitted) |
75c99385 PA |
11637 | /* We only enable async when the user specifically asks for it. */ |
11638 | return 0; | |
11639 | ||
23860348 | 11640 | /* We're async whenever the serial device is. */ |
5d93a237 | 11641 | return serial_can_async_p (rs->remote_desc); |
6426a772 JM |
11642 | } |
11643 | ||
11644 | static int | |
11645 | remote_is_async_p (void) | |
11646 | { | |
5d93a237 TT |
11647 | struct remote_state *rs = get_remote_state (); |
11648 | ||
c6ebd6cf | 11649 | if (!target_async_permitted) |
75c99385 PA |
11650 | /* We only enable async when the user specifically asks for it. */ |
11651 | return 0; | |
11652 | ||
23860348 | 11653 | /* We're async whenever the serial device is. */ |
5d93a237 | 11654 | return serial_is_async_p (rs->remote_desc); |
6426a772 JM |
11655 | } |
11656 | ||
2acceee2 JM |
11657 | /* Pass the SERIAL event on and up to the client. One day this code |
11658 | will be able to delay notifying the client of an event until the | |
23860348 | 11659 | point where an entire packet has been received. */ |
2acceee2 | 11660 | |
2acceee2 JM |
11661 | static serial_event_ftype remote_async_serial_handler; |
11662 | ||
6426a772 | 11663 | static void |
819cc324 | 11664 | remote_async_serial_handler (struct serial *scb, void *context) |
6426a772 | 11665 | { |
88b496c3 TT |
11666 | struct remote_state *rs = context; |
11667 | ||
2acceee2 JM |
11668 | /* Don't propogate error information up to the client. Instead let |
11669 | the client find out about the error by querying the target. */ | |
88b496c3 | 11670 | rs->async_client_callback (INF_REG_EVENT, rs->async_client_context); |
2acceee2 JM |
11671 | } |
11672 | ||
74531fed PA |
11673 | static void |
11674 | remote_async_inferior_event_handler (gdb_client_data data) | |
11675 | { | |
11676 | inferior_event_handler (INF_REG_EVENT, NULL); | |
11677 | } | |
11678 | ||
2acceee2 | 11679 | static void |
2bc416ba | 11680 | remote_async (void (*callback) (enum inferior_event_type event_type, |
23860348 | 11681 | void *context), void *context) |
2acceee2 | 11682 | { |
5d93a237 TT |
11683 | struct remote_state *rs = get_remote_state (); |
11684 | ||
2acceee2 JM |
11685 | if (callback != NULL) |
11686 | { | |
88b496c3 TT |
11687 | serial_async (rs->remote_desc, remote_async_serial_handler, rs); |
11688 | rs->async_client_callback = callback; | |
11689 | rs->async_client_context = context; | |
2acceee2 JM |
11690 | } |
11691 | else | |
5d93a237 | 11692 | serial_async (rs->remote_desc, NULL, NULL); |
6426a772 JM |
11693 | } |
11694 | ||
5a2468f5 | 11695 | static void |
c2d11a7d | 11696 | set_remote_cmd (char *args, int from_tty) |
5a2468f5 | 11697 | { |
427c3a89 | 11698 | help_list (remote_set_cmdlist, "set remote ", -1, gdb_stdout); |
5a2468f5 JM |
11699 | } |
11700 | ||
d471ea57 AC |
11701 | static void |
11702 | show_remote_cmd (char *args, int from_tty) | |
11703 | { | |
37a105a1 | 11704 | /* We can't just use cmd_show_list here, because we want to skip |
427c3a89 | 11705 | the redundant "show remote Z-packet" and the legacy aliases. */ |
37a105a1 DJ |
11706 | struct cleanup *showlist_chain; |
11707 | struct cmd_list_element *list = remote_show_cmdlist; | |
79a45e25 | 11708 | struct ui_out *uiout = current_uiout; |
37a105a1 DJ |
11709 | |
11710 | showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist"); | |
11711 | for (; list != NULL; list = list->next) | |
11712 | if (strcmp (list->name, "Z-packet") == 0) | |
11713 | continue; | |
427c3a89 DJ |
11714 | else if (list->type == not_set_cmd) |
11715 | /* Alias commands are exactly like the original, except they | |
11716 | don't have the normal type. */ | |
11717 | continue; | |
11718 | else | |
37a105a1 DJ |
11719 | { |
11720 | struct cleanup *option_chain | |
11721 | = make_cleanup_ui_out_tuple_begin_end (uiout, "option"); | |
a744cf53 | 11722 | |
37a105a1 DJ |
11723 | ui_out_field_string (uiout, "name", list->name); |
11724 | ui_out_text (uiout, ": "); | |
427c3a89 | 11725 | if (list->type == show_cmd) |
5b9afe8a | 11726 | do_show_command ((char *) NULL, from_tty, list); |
427c3a89 DJ |
11727 | else |
11728 | cmd_func (list, NULL, from_tty); | |
37a105a1 DJ |
11729 | /* Close the tuple. */ |
11730 | do_cleanups (option_chain); | |
11731 | } | |
427c3a89 DJ |
11732 | |
11733 | /* Close the tuple. */ | |
11734 | do_cleanups (showlist_chain); | |
d471ea57 | 11735 | } |
5a2468f5 | 11736 | |
0f71a2f6 | 11737 | |
23860348 | 11738 | /* Function to be called whenever a new objfile (shlib) is detected. */ |
dc8acb97 MS |
11739 | static void |
11740 | remote_new_objfile (struct objfile *objfile) | |
11741 | { | |
5d93a237 TT |
11742 | struct remote_state *rs = get_remote_state (); |
11743 | ||
11744 | if (rs->remote_desc != 0) /* Have a remote connection. */ | |
36d25514 | 11745 | remote_check_symbols (); |
dc8acb97 MS |
11746 | } |
11747 | ||
00bf0b85 SS |
11748 | /* Pull all the tracepoints defined on the target and create local |
11749 | data structures representing them. We don't want to create real | |
11750 | tracepoints yet, we don't want to mess up the user's existing | |
11751 | collection. */ | |
11752 | ||
11753 | static int | |
11754 | remote_upload_tracepoints (struct uploaded_tp **utpp) | |
d5551862 | 11755 | { |
00bf0b85 SS |
11756 | struct remote_state *rs = get_remote_state (); |
11757 | char *p; | |
d5551862 | 11758 | |
00bf0b85 SS |
11759 | /* Ask for a first packet of tracepoint definition. */ |
11760 | putpkt ("qTfP"); | |
11761 | getpkt (&rs->buf, &rs->buf_size, 0); | |
11762 | p = rs->buf; | |
11763 | while (*p && *p != 'l') | |
d5551862 | 11764 | { |
00bf0b85 SS |
11765 | parse_tracepoint_definition (p, utpp); |
11766 | /* Ask for another packet of tracepoint definition. */ | |
11767 | putpkt ("qTsP"); | |
11768 | getpkt (&rs->buf, &rs->buf_size, 0); | |
11769 | p = rs->buf; | |
d5551862 | 11770 | } |
00bf0b85 | 11771 | return 0; |
d5551862 SS |
11772 | } |
11773 | ||
00bf0b85 SS |
11774 | static int |
11775 | remote_upload_trace_state_variables (struct uploaded_tsv **utsvp) | |
d5551862 | 11776 | { |
00bf0b85 | 11777 | struct remote_state *rs = get_remote_state (); |
d5551862 | 11778 | char *p; |
d5551862 | 11779 | |
00bf0b85 SS |
11780 | /* Ask for a first packet of variable definition. */ |
11781 | putpkt ("qTfV"); | |
d5551862 SS |
11782 | getpkt (&rs->buf, &rs->buf_size, 0); |
11783 | p = rs->buf; | |
00bf0b85 | 11784 | while (*p && *p != 'l') |
d5551862 | 11785 | { |
00bf0b85 SS |
11786 | parse_tsv_definition (p, utsvp); |
11787 | /* Ask for another packet of variable definition. */ | |
11788 | putpkt ("qTsV"); | |
d5551862 SS |
11789 | getpkt (&rs->buf, &rs->buf_size, 0); |
11790 | p = rs->buf; | |
11791 | } | |
00bf0b85 | 11792 | return 0; |
d5551862 SS |
11793 | } |
11794 | ||
c1e36e3e PA |
11795 | /* The "set/show range-stepping" show hook. */ |
11796 | ||
11797 | static void | |
11798 | show_range_stepping (struct ui_file *file, int from_tty, | |
11799 | struct cmd_list_element *c, | |
11800 | const char *value) | |
11801 | { | |
11802 | fprintf_filtered (file, | |
11803 | _("Debugger's willingness to use range stepping " | |
11804 | "is %s.\n"), value); | |
11805 | } | |
11806 | ||
11807 | /* The "set/show range-stepping" set hook. */ | |
11808 | ||
11809 | static void | |
11810 | set_range_stepping (char *ignore_args, int from_tty, | |
11811 | struct cmd_list_element *c) | |
11812 | { | |
5d93a237 TT |
11813 | struct remote_state *rs = get_remote_state (); |
11814 | ||
c1e36e3e PA |
11815 | /* Whene enabling, check whether range stepping is actually |
11816 | supported by the target, and warn if not. */ | |
11817 | if (use_range_stepping) | |
11818 | { | |
5d93a237 | 11819 | if (rs->remote_desc != NULL) |
c1e36e3e | 11820 | { |
c1e36e3e PA |
11821 | if (remote_protocol_packets[PACKET_vCont].support == PACKET_SUPPORT_UNKNOWN) |
11822 | remote_vcont_probe (rs); | |
11823 | ||
11824 | if (remote_protocol_packets[PACKET_vCont].support == PACKET_ENABLE | |
11825 | && rs->supports_vCont.r) | |
11826 | return; | |
11827 | } | |
11828 | ||
11829 | warning (_("Range stepping is not supported by the current target")); | |
11830 | } | |
11831 | } | |
11832 | ||
c906108c | 11833 | void |
fba45db2 | 11834 | _initialize_remote (void) |
c906108c | 11835 | { |
ea9c271d | 11836 | struct remote_state *rs; |
9a7071a8 | 11837 | struct cmd_list_element *cmd; |
6f937416 | 11838 | const char *cmd_name; |
ea9c271d | 11839 | |
0f71a2f6 | 11840 | /* architecture specific data */ |
2bc416ba | 11841 | remote_gdbarch_data_handle = |
23860348 | 11842 | gdbarch_data_register_post_init (init_remote_state); |
29709017 DJ |
11843 | remote_g_packet_data_handle = |
11844 | gdbarch_data_register_pre_init (remote_g_packet_data_init); | |
d01949b6 | 11845 | |
ea9c271d DJ |
11846 | /* Initialize the per-target state. At the moment there is only one |
11847 | of these, not one per target. Only one target is active at a | |
cf792862 TT |
11848 | time. */ |
11849 | remote_state = new_remote_state (); | |
ea9c271d | 11850 | |
c906108c SS |
11851 | init_remote_ops (); |
11852 | add_target (&remote_ops); | |
11853 | ||
11854 | init_extended_remote_ops (); | |
11855 | add_target (&extended_remote_ops); | |
cce74817 | 11856 | |
dc8acb97 | 11857 | /* Hook into new objfile notification. */ |
06d3b283 | 11858 | observer_attach_new_objfile (remote_new_objfile); |
5f4cf0bb YQ |
11859 | /* We're no longer interested in notification events of an inferior |
11860 | when it exits. */ | |
11861 | observer_attach_inferior_exit (discard_pending_stop_replies); | |
dc8acb97 | 11862 | |
b803fb0f | 11863 | /* Set up signal handlers. */ |
934b9bac | 11864 | async_sigint_remote_token = |
b803fb0f | 11865 | create_async_signal_handler (async_remote_interrupt, NULL); |
934b9bac | 11866 | async_sigint_remote_twice_token = |
6d549500 | 11867 | create_async_signal_handler (async_remote_interrupt_twice, NULL); |
b803fb0f | 11868 | |
c906108c SS |
11869 | #if 0 |
11870 | init_remote_threadtests (); | |
11871 | #endif | |
11872 | ||
722247f1 | 11873 | stop_reply_queue = QUEUE_alloc (stop_reply_p, stop_reply_xfree); |
23860348 | 11874 | /* set/show remote ... */ |
d471ea57 | 11875 | |
1bedd215 | 11876 | add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, _("\ |
5a2468f5 JM |
11877 | Remote protocol specific variables\n\ |
11878 | Configure various remote-protocol specific variables such as\n\ | |
1bedd215 | 11879 | the packets being used"), |
cff3e48b | 11880 | &remote_set_cmdlist, "set remote ", |
23860348 | 11881 | 0 /* allow-unknown */, &setlist); |
1bedd215 | 11882 | add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\ |
5a2468f5 JM |
11883 | Remote protocol specific variables\n\ |
11884 | Configure various remote-protocol specific variables such as\n\ | |
1bedd215 | 11885 | the packets being used"), |
cff3e48b | 11886 | &remote_show_cmdlist, "show remote ", |
23860348 | 11887 | 0 /* allow-unknown */, &showlist); |
5a2468f5 | 11888 | |
1a966eab AC |
11889 | add_cmd ("compare-sections", class_obscure, compare_sections_command, _("\ |
11890 | Compare section data on target to the exec file.\n\ | |
11891 | Argument is a single section name (default: all loaded sections)."), | |
c906108c SS |
11892 | &cmdlist); |
11893 | ||
1a966eab AC |
11894 | add_cmd ("packet", class_maintenance, packet_command, _("\ |
11895 | Send an arbitrary packet to a remote target.\n\ | |
c906108c SS |
11896 | maintenance packet TEXT\n\ |
11897 | If GDB is talking to an inferior via the GDB serial protocol, then\n\ | |
11898 | this command sends the string TEXT to the inferior, and displays the\n\ | |
11899 | response packet. GDB supplies the initial `$' character, and the\n\ | |
1a966eab | 11900 | terminating `#' character and checksum."), |
c906108c SS |
11901 | &maintenancelist); |
11902 | ||
7915a72c AC |
11903 | add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, _("\ |
11904 | Set whether to send break if interrupted."), _("\ | |
11905 | Show whether to send break if interrupted."), _("\ | |
11906 | If set, a break, instead of a cntrl-c, is sent to the remote target."), | |
9a7071a8 | 11907 | set_remotebreak, show_remotebreak, |
e707bbc2 | 11908 | &setlist, &showlist); |
9a7071a8 JB |
11909 | cmd_name = "remotebreak"; |
11910 | cmd = lookup_cmd (&cmd_name, setlist, "", -1, 1); | |
11911 | deprecate_cmd (cmd, "set remote interrupt-sequence"); | |
11912 | cmd_name = "remotebreak"; /* needed because lookup_cmd updates the pointer */ | |
11913 | cmd = lookup_cmd (&cmd_name, showlist, "", -1, 1); | |
11914 | deprecate_cmd (cmd, "show remote interrupt-sequence"); | |
11915 | ||
11916 | add_setshow_enum_cmd ("interrupt-sequence", class_support, | |
3e43a32a MS |
11917 | interrupt_sequence_modes, &interrupt_sequence_mode, |
11918 | _("\ | |
9a7071a8 JB |
11919 | Set interrupt sequence to remote target."), _("\ |
11920 | Show interrupt sequence to remote target."), _("\ | |
11921 | Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."), | |
11922 | NULL, show_interrupt_sequence, | |
11923 | &remote_set_cmdlist, | |
11924 | &remote_show_cmdlist); | |
11925 | ||
11926 | add_setshow_boolean_cmd ("interrupt-on-connect", class_support, | |
11927 | &interrupt_on_connect, _("\ | |
11928 | Set whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \ | |
11929 | Show whether interrupt-sequence is sent to remote target when gdb connects to."), _(" \ | |
11930 | If set, interrupt sequence is sent to remote target."), | |
11931 | NULL, NULL, | |
11932 | &remote_set_cmdlist, &remote_show_cmdlist); | |
c906108c | 11933 | |
23860348 | 11934 | /* Install commands for configuring memory read/write packets. */ |
11cf8741 | 11935 | |
1a966eab AC |
11936 | add_cmd ("remotewritesize", no_class, set_memory_write_packet_size, _("\ |
11937 | Set the maximum number of bytes per memory write packet (deprecated)."), | |
11cf8741 | 11938 | &setlist); |
1a966eab AC |
11939 | add_cmd ("remotewritesize", no_class, show_memory_write_packet_size, _("\ |
11940 | Show the maximum number of bytes per memory write packet (deprecated)."), | |
11cf8741 JM |
11941 | &showlist); |
11942 | add_cmd ("memory-write-packet-size", no_class, | |
1a966eab AC |
11943 | set_memory_write_packet_size, _("\ |
11944 | Set the maximum number of bytes per memory-write packet.\n\ | |
11945 | Specify the number of bytes in a packet or 0 (zero) for the\n\ | |
11946 | default packet size. The actual limit is further reduced\n\ | |
11947 | dependent on the target. Specify ``fixed'' to disable the\n\ | |
11948 | further restriction and ``limit'' to enable that restriction."), | |
11cf8741 JM |
11949 | &remote_set_cmdlist); |
11950 | add_cmd ("memory-read-packet-size", no_class, | |
1a966eab AC |
11951 | set_memory_read_packet_size, _("\ |
11952 | Set the maximum number of bytes per memory-read packet.\n\ | |
11953 | Specify the number of bytes in a packet or 0 (zero) for the\n\ | |
11954 | default packet size. The actual limit is further reduced\n\ | |
11955 | dependent on the target. Specify ``fixed'' to disable the\n\ | |
11956 | further restriction and ``limit'' to enable that restriction."), | |
11cf8741 JM |
11957 | &remote_set_cmdlist); |
11958 | add_cmd ("memory-write-packet-size", no_class, | |
11959 | show_memory_write_packet_size, | |
1a966eab | 11960 | _("Show the maximum number of bytes per memory-write packet."), |
11cf8741 JM |
11961 | &remote_show_cmdlist); |
11962 | add_cmd ("memory-read-packet-size", no_class, | |
11963 | show_memory_read_packet_size, | |
1a966eab | 11964 | _("Show the maximum number of bytes per memory-read packet."), |
11cf8741 | 11965 | &remote_show_cmdlist); |
c906108c | 11966 | |
b3f42336 | 11967 | add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class, |
7915a72c AC |
11968 | &remote_hw_watchpoint_limit, _("\ |
11969 | Set the maximum number of target hardware watchpoints."), _("\ | |
11970 | Show the maximum number of target hardware watchpoints."), _("\ | |
11971 | Specify a negative limit for unlimited."), | |
3e43a32a MS |
11972 | NULL, NULL, /* FIXME: i18n: The maximum |
11973 | number of target hardware | |
11974 | watchpoints is %s. */ | |
b3f42336 | 11975 | &remote_set_cmdlist, &remote_show_cmdlist); |
480a3f21 PW |
11976 | add_setshow_zinteger_cmd ("hardware-watchpoint-length-limit", no_class, |
11977 | &remote_hw_watchpoint_length_limit, _("\ | |
11978 | Set the maximum length (in bytes) of a target hardware watchpoint."), _("\ | |
11979 | Show the maximum length (in bytes) of a target hardware watchpoint."), _("\ | |
11980 | Specify a negative limit for unlimited."), | |
11981 | NULL, NULL, /* FIXME: i18n: The maximum | |
11982 | length (in bytes) of a target | |
11983 | hardware watchpoint is %s. */ | |
11984 | &remote_set_cmdlist, &remote_show_cmdlist); | |
b3f42336 | 11985 | add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class, |
7915a72c AC |
11986 | &remote_hw_breakpoint_limit, _("\ |
11987 | Set the maximum number of target hardware breakpoints."), _("\ | |
11988 | Show the maximum number of target hardware breakpoints."), _("\ | |
11989 | Specify a negative limit for unlimited."), | |
3e43a32a MS |
11990 | NULL, NULL, /* FIXME: i18n: The maximum |
11991 | number of target hardware | |
11992 | breakpoints is %s. */ | |
b3f42336 | 11993 | &remote_set_cmdlist, &remote_show_cmdlist); |
501eef12 | 11994 | |
1b493192 PA |
11995 | add_setshow_zuinteger_cmd ("remoteaddresssize", class_obscure, |
11996 | &remote_address_size, _("\ | |
4d28ad1e AC |
11997 | Set the maximum size of the address (in bits) in a memory packet."), _("\ |
11998 | Show the maximum size of the address (in bits) in a memory packet."), NULL, | |
1b493192 PA |
11999 | NULL, |
12000 | NULL, /* FIXME: i18n: */ | |
12001 | &setlist, &showlist); | |
c906108c | 12002 | |
444abaca | 12003 | add_packet_config_cmd (&remote_protocol_packets[PACKET_X], |
bb572ddd | 12004 | "X", "binary-download", 1); |
0f71a2f6 | 12005 | |
444abaca | 12006 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vCont], |
bb572ddd | 12007 | "vCont", "verbose-resume", 0); |
506fb367 | 12008 | |
89be2091 DJ |
12009 | add_packet_config_cmd (&remote_protocol_packets[PACKET_QPassSignals], |
12010 | "QPassSignals", "pass-signals", 0); | |
12011 | ||
9b224c5e PA |
12012 | add_packet_config_cmd (&remote_protocol_packets[PACKET_QProgramSignals], |
12013 | "QProgramSignals", "program-signals", 0); | |
12014 | ||
444abaca | 12015 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol], |
bb572ddd | 12016 | "qSymbol", "symbol-lookup", 0); |
dc8acb97 | 12017 | |
444abaca | 12018 | add_packet_config_cmd (&remote_protocol_packets[PACKET_P], |
bb572ddd | 12019 | "P", "set-register", 1); |
d471ea57 | 12020 | |
444abaca | 12021 | add_packet_config_cmd (&remote_protocol_packets[PACKET_p], |
bb572ddd | 12022 | "p", "fetch-register", 1); |
b96ec7ac | 12023 | |
444abaca | 12024 | add_packet_config_cmd (&remote_protocol_packets[PACKET_Z0], |
bb572ddd | 12025 | "Z0", "software-breakpoint", 0); |
d471ea57 | 12026 | |
444abaca | 12027 | add_packet_config_cmd (&remote_protocol_packets[PACKET_Z1], |
bb572ddd | 12028 | "Z1", "hardware-breakpoint", 0); |
d471ea57 | 12029 | |
444abaca | 12030 | add_packet_config_cmd (&remote_protocol_packets[PACKET_Z2], |
bb572ddd | 12031 | "Z2", "write-watchpoint", 0); |
d471ea57 | 12032 | |
444abaca | 12033 | add_packet_config_cmd (&remote_protocol_packets[PACKET_Z3], |
bb572ddd | 12034 | "Z3", "read-watchpoint", 0); |
d471ea57 | 12035 | |
444abaca | 12036 | add_packet_config_cmd (&remote_protocol_packets[PACKET_Z4], |
bb572ddd | 12037 | "Z4", "access-watchpoint", 0); |
d471ea57 | 12038 | |
0876f84a DJ |
12039 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_auxv], |
12040 | "qXfer:auxv:read", "read-aux-vector", 0); | |
802188a7 | 12041 | |
23181151 DJ |
12042 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_features], |
12043 | "qXfer:features:read", "target-features", 0); | |
12044 | ||
cfa9d6d9 DJ |
12045 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries], |
12046 | "qXfer:libraries:read", "library-info", 0); | |
12047 | ||
2268b414 JK |
12048 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries_svr4], |
12049 | "qXfer:libraries-svr4:read", "library-info-svr4", 0); | |
12050 | ||
fd79ecee DJ |
12051 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_memory_map], |
12052 | "qXfer:memory-map:read", "memory-map", 0); | |
12053 | ||
0e7f50da UW |
12054 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_read], |
12055 | "qXfer:spu:read", "read-spu-object", 0); | |
12056 | ||
12057 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_write], | |
12058 | "qXfer:spu:write", "write-spu-object", 0); | |
12059 | ||
07e059b5 VP |
12060 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_osdata], |
12061 | "qXfer:osdata:read", "osdata", 0); | |
12062 | ||
dc146f7c VP |
12063 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_threads], |
12064 | "qXfer:threads:read", "threads", 0); | |
12065 | ||
4aa995e1 PA |
12066 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_read], |
12067 | "qXfer:siginfo:read", "read-siginfo-object", 0); | |
12068 | ||
12069 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_write], | |
12070 | "qXfer:siginfo:write", "write-siginfo-object", 0); | |
12071 | ||
b3b9301e PA |
12072 | add_packet_config_cmd |
12073 | (&remote_protocol_packets[PACKET_qXfer_traceframe_info], | |
eb9fe518 | 12074 | "qXfer:traceframe-info:read", "traceframe-info", 0); |
b3b9301e | 12075 | |
169081d0 TG |
12076 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_uib], |
12077 | "qXfer:uib:read", "unwind-info-block", 0); | |
12078 | ||
444abaca | 12079 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTLSAddr], |
38691318 | 12080 | "qGetTLSAddr", "get-thread-local-storage-address", |
38691318 KB |
12081 | 0); |
12082 | ||
711e434b PM |
12083 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTIBAddr], |
12084 | "qGetTIBAddr", "get-thread-information-block-address", | |
12085 | 0); | |
12086 | ||
40ab02ce MS |
12087 | add_packet_config_cmd (&remote_protocol_packets[PACKET_bc], |
12088 | "bc", "reverse-continue", 0); | |
12089 | ||
12090 | add_packet_config_cmd (&remote_protocol_packets[PACKET_bs], | |
12091 | "bs", "reverse-step", 0); | |
12092 | ||
be2a5f71 DJ |
12093 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qSupported], |
12094 | "qSupported", "supported-packets", 0); | |
12095 | ||
08388c79 DE |
12096 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qSearch_memory], |
12097 | "qSearch:memory", "search-memory", 0); | |
12098 | ||
bd3eecc3 PA |
12099 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qTStatus], |
12100 | "qTStatus", "trace-status", 0); | |
12101 | ||
a6b151f1 DJ |
12102 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_open], |
12103 | "vFile:open", "hostio-open", 0); | |
12104 | ||
12105 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pread], | |
12106 | "vFile:pread", "hostio-pread", 0); | |
12107 | ||
12108 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pwrite], | |
12109 | "vFile:pwrite", "hostio-pwrite", 0); | |
12110 | ||
12111 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_close], | |
12112 | "vFile:close", "hostio-close", 0); | |
12113 | ||
12114 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_unlink], | |
12115 | "vFile:unlink", "hostio-unlink", 0); | |
12116 | ||
b9e7b9c3 UW |
12117 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_readlink], |
12118 | "vFile:readlink", "hostio-readlink", 0); | |
12119 | ||
2d717e4f DJ |
12120 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach], |
12121 | "vAttach", "attach", 0); | |
12122 | ||
12123 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vRun], | |
12124 | "vRun", "run", 0); | |
12125 | ||
a6f3e723 SL |
12126 | add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartNoAckMode], |
12127 | "QStartNoAckMode", "noack", 0); | |
12128 | ||
82f73884 PA |
12129 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vKill], |
12130 | "vKill", "kill", 0); | |
12131 | ||
0b16c5cf PA |
12132 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qAttached], |
12133 | "qAttached", "query-attached", 0); | |
12134 | ||
782b2b07 | 12135 | add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalTracepoints], |
3e43a32a MS |
12136 | "ConditionalTracepoints", |
12137 | "conditional-tracepoints", 0); | |
3788aec7 LM |
12138 | |
12139 | add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalBreakpoints], | |
12140 | "ConditionalBreakpoints", | |
12141 | "conditional-breakpoints", 0); | |
12142 | ||
d3ce09f5 SS |
12143 | add_packet_config_cmd (&remote_protocol_packets[PACKET_BreakpointCommands], |
12144 | "BreakpointCommands", | |
12145 | "breakpoint-commands", 0); | |
12146 | ||
7a697b8d SS |
12147 | add_packet_config_cmd (&remote_protocol_packets[PACKET_FastTracepoints], |
12148 | "FastTracepoints", "fast-tracepoints", 0); | |
782b2b07 | 12149 | |
409873ef SS |
12150 | add_packet_config_cmd (&remote_protocol_packets[PACKET_TracepointSource], |
12151 | "TracepointSource", "TracepointSource", 0); | |
12152 | ||
d914c394 SS |
12153 | add_packet_config_cmd (&remote_protocol_packets[PACKET_QAllow], |
12154 | "QAllow", "allow", 0); | |
12155 | ||
0fb4aa4b PA |
12156 | add_packet_config_cmd (&remote_protocol_packets[PACKET_StaticTracepoints], |
12157 | "StaticTracepoints", "static-tracepoints", 0); | |
12158 | ||
1e4d1764 YQ |
12159 | add_packet_config_cmd (&remote_protocol_packets[PACKET_InstallInTrace], |
12160 | "InstallInTrace", "install-in-trace", 0); | |
12161 | ||
0fb4aa4b PA |
12162 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_statictrace_read], |
12163 | "qXfer:statictrace:read", "read-sdata-object", 0); | |
12164 | ||
78d85199 YQ |
12165 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_fdpic], |
12166 | "qXfer:fdpic:read", "read-fdpic-loadmap", 0); | |
12167 | ||
03583c20 UW |
12168 | add_packet_config_cmd (&remote_protocol_packets[PACKET_QDisableRandomization], |
12169 | "QDisableRandomization", "disable-randomization", 0); | |
12170 | ||
d1feda86 YQ |
12171 | add_packet_config_cmd (&remote_protocol_packets[PACKET_QAgent], |
12172 | "QAgent", "agent", 0); | |
12173 | ||
f6f899bf HAQ |
12174 | add_packet_config_cmd (&remote_protocol_packets[PACKET_QTBuffer_size], |
12175 | "QTBuffer:size", "trace-buffer-size", 0); | |
12176 | ||
9accd112 MM |
12177 | add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_off], |
12178 | "Qbtrace:off", "disable-btrace", 0); | |
12179 | ||
12180 | add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_bts], | |
12181 | "Qbtrace:bts", "enable-btrace", 0); | |
12182 | ||
12183 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace], | |
12184 | "qXfer:btrace", "read-btrace", 0); | |
12185 | ||
37a105a1 DJ |
12186 | /* Keep the old ``set remote Z-packet ...'' working. Each individual |
12187 | Z sub-packet has its own set and show commands, but users may | |
12188 | have sets to this variable in their .gdbinit files (or in their | |
12189 | documentation). */ | |
e9e68a56 | 12190 | add_setshow_auto_boolean_cmd ("Z-packet", class_obscure, |
7915a72c AC |
12191 | &remote_Z_packet_detect, _("\ |
12192 | Set use of remote protocol `Z' packets"), _("\ | |
12193 | Show use of remote protocol `Z' packets "), _("\ | |
3b64bf98 | 12194 | When set, GDB will attempt to use the remote breakpoint and watchpoint\n\ |
7915a72c | 12195 | packets."), |
e9e68a56 | 12196 | set_remote_protocol_Z_packet_cmd, |
3e43a32a MS |
12197 | show_remote_protocol_Z_packet_cmd, |
12198 | /* FIXME: i18n: Use of remote protocol | |
12199 | `Z' packets is %s. */ | |
e9e68a56 | 12200 | &remote_set_cmdlist, &remote_show_cmdlist); |
449092f6 | 12201 | |
a6b151f1 DJ |
12202 | add_prefix_cmd ("remote", class_files, remote_command, _("\ |
12203 | Manipulate files on the remote system\n\ | |
12204 | Transfer files to and from the remote target system."), | |
12205 | &remote_cmdlist, "remote ", | |
12206 | 0 /* allow-unknown */, &cmdlist); | |
12207 | ||
12208 | add_cmd ("put", class_files, remote_put_command, | |
12209 | _("Copy a local file to the remote system."), | |
12210 | &remote_cmdlist); | |
12211 | ||
12212 | add_cmd ("get", class_files, remote_get_command, | |
12213 | _("Copy a remote file to the local system."), | |
12214 | &remote_cmdlist); | |
12215 | ||
12216 | add_cmd ("delete", class_files, remote_delete_command, | |
12217 | _("Delete a remote file."), | |
12218 | &remote_cmdlist); | |
12219 | ||
2d717e4f DJ |
12220 | remote_exec_file = xstrdup (""); |
12221 | add_setshow_string_noescape_cmd ("exec-file", class_files, | |
12222 | &remote_exec_file, _("\ | |
12223 | Set the remote pathname for \"run\""), _("\ | |
12224 | Show the remote pathname for \"run\""), NULL, NULL, NULL, | |
12225 | &remote_set_cmdlist, &remote_show_cmdlist); | |
12226 | ||
c1e36e3e PA |
12227 | add_setshow_boolean_cmd ("range-stepping", class_run, |
12228 | &use_range_stepping, _("\ | |
12229 | Enable or disable range stepping."), _("\ | |
12230 | Show whether target-assisted range stepping is enabled."), _("\ | |
12231 | If on, and the target supports it, when stepping a source line, GDB\n\ | |
12232 | tells the target to step the corresponding range of addresses itself instead\n\ | |
12233 | of issuing multiple single-steps. This speeds up source level\n\ | |
12234 | stepping. If off, GDB always issues single-steps, even if range\n\ | |
12235 | stepping is supported by the target. The default is on."), | |
12236 | set_range_stepping, | |
12237 | show_range_stepping, | |
12238 | &setlist, | |
12239 | &showlist); | |
12240 | ||
449092f6 CV |
12241 | /* Eventually initialize fileio. See fileio.c */ |
12242 | initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist); | |
79d7f229 PA |
12243 | |
12244 | /* Take advantage of the fact that the LWP field is not used, to tag | |
12245 | special ptids with it set to != 0. */ | |
82f73884 PA |
12246 | magic_null_ptid = ptid_build (42000, 1, -1); |
12247 | not_sent_ptid = ptid_build (42000, 1, -2); | |
12248 | any_thread_ptid = ptid_build (42000, 1, 0); | |
35b1e5cc SS |
12249 | |
12250 | target_buf_size = 2048; | |
12251 | target_buf = xmalloc (target_buf_size); | |
c906108c | 12252 | } |
10760264 | 12253 |