Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Remote target communications for serial-line targets in custom GDB protocol |
8926118c | 2 | |
6aba47ca | 3 | Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, |
9b254dd1 | 4 | 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 |
29182b13 | 5 | Free Software Foundation, Inc. |
c906108c | 6 | |
c5aa993b JM |
7 | This file is part of GDB. |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 11 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b JM |
12 | (at your option) any later version. |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c5aa993b | 21 | |
23860348 | 22 | /* See the GDB User Guide for details of the GDB remote protocol. */ |
c5aa993b | 23 | |
c906108c SS |
24 | #include "defs.h" |
25 | #include "gdb_string.h" | |
26 | #include <ctype.h> | |
27 | #include <fcntl.h> | |
c906108c SS |
28 | #include "inferior.h" |
29 | #include "bfd.h" | |
30 | #include "symfile.h" | |
60250e8b | 31 | #include "exceptions.h" |
c906108c | 32 | #include "target.h" |
c5aa993b | 33 | /*#include "terminal.h" */ |
c906108c SS |
34 | #include "gdbcmd.h" |
35 | #include "objfiles.h" | |
36 | #include "gdb-stabs.h" | |
37 | #include "gdbthread.h" | |
c2c6d25f | 38 | #include "remote.h" |
4e052eda | 39 | #include "regcache.h" |
fd0407d6 | 40 | #include "value.h" |
1ff9c3d6 | 41 | #include "gdb_assert.h" |
6867ae3e | 42 | #include "observer.h" |
a77053c2 | 43 | #include "solib.h" |
37a105a1 DJ |
44 | #include "cli/cli-decode.h" |
45 | #include "cli/cli-setshow.h" | |
424163ea | 46 | #include "target-descriptions.h" |
c906108c | 47 | |
7a292a7a | 48 | #include <ctype.h> |
9846de1b | 49 | #include <sys/time.h> |
c906108c | 50 | |
43ff13b4 | 51 | #include "event-loop.h" |
c2c6d25f | 52 | #include "event-top.h" |
2acceee2 | 53 | #include "inf-loop.h" |
43ff13b4 | 54 | |
c906108c SS |
55 | #include <signal.h> |
56 | #include "serial.h" | |
57 | ||
6240bebf MS |
58 | #include "gdbcore.h" /* for exec_bfd */ |
59 | ||
449092f6 | 60 | #include "remote-fileio.h" |
a6b151f1 | 61 | #include "gdb/fileio.h" |
449092f6 | 62 | |
fd79ecee DJ |
63 | #include "memory-map.h" |
64 | ||
6765f3e5 DJ |
65 | /* The size to align memory write packets, when practical. The protocol |
66 | does not guarantee any alignment, and gdb will generate short | |
67 | writes and unaligned writes, but even as a best-effort attempt this | |
68 | can improve bulk transfers. For instance, if a write is misaligned | |
69 | relative to the target's data bus, the stub may need to make an extra | |
70 | round trip fetching data from the target. This doesn't make a | |
71 | huge difference, but it's easy to do, so we try to be helpful. | |
72 | ||
73 | The alignment chosen is arbitrary; usually data bus width is | |
74 | important here, not the possibly larger cache line size. */ | |
75 | enum { REMOTE_ALIGN_WRITES = 16 }; | |
76 | ||
23860348 | 77 | /* Prototypes for local functions. */ |
6426a772 JM |
78 | static void cleanup_sigint_signal_handler (void *dummy); |
79 | static void initialize_sigint_signal_handler (void); | |
6d820c5c | 80 | static int getpkt_sane (char **buf, long *sizeof_buf, int forever); |
6426a772 | 81 | |
a14ed312 KB |
82 | static void handle_remote_sigint (int); |
83 | static void handle_remote_sigint_twice (int); | |
84 | static void async_remote_interrupt (gdb_client_data); | |
85 | void async_remote_interrupt_twice (gdb_client_data); | |
43ff13b4 | 86 | |
a14ed312 | 87 | static void remote_files_info (struct target_ops *ignore); |
c906108c | 88 | |
316f2060 | 89 | static void remote_prepare_to_store (struct regcache *regcache); |
c906108c | 90 | |
56be3814 | 91 | static void remote_fetch_registers (struct regcache *regcache, int regno); |
c906108c | 92 | |
39f77062 KB |
93 | static void remote_resume (ptid_t ptid, int step, |
94 | enum target_signal siggnal); | |
a14ed312 | 95 | static void remote_open (char *name, int from_tty); |
c906108c | 96 | |
a14ed312 | 97 | static void extended_remote_open (char *name, int from_tty); |
c906108c | 98 | |
75c99385 | 99 | static void remote_open_1 (char *, int, struct target_ops *, int extended_p); |
c906108c | 100 | |
a14ed312 | 101 | static void remote_close (int quitting); |
c906108c | 102 | |
56be3814 | 103 | static void remote_store_registers (struct regcache *regcache, int regno); |
c906108c | 104 | |
a14ed312 | 105 | static void remote_mourn (void); |
c906108c | 106 | |
a14ed312 | 107 | static void extended_remote_restart (void); |
c906108c | 108 | |
a14ed312 | 109 | static void extended_remote_mourn (void); |
c906108c | 110 | |
a14ed312 | 111 | static void remote_mourn_1 (struct target_ops *); |
c906108c | 112 | |
6d820c5c | 113 | static void remote_send (char **buf, long *sizeof_buf_p); |
c906108c | 114 | |
a14ed312 | 115 | static int readchar (int timeout); |
c906108c | 116 | |
39f77062 | 117 | static ptid_t remote_wait (ptid_t ptid, |
75c99385 | 118 | struct target_waitstatus *status); |
c906108c | 119 | |
a14ed312 | 120 | static void remote_kill (void); |
c906108c | 121 | |
a14ed312 | 122 | static int tohex (int nib); |
c906108c | 123 | |
75c99385 PA |
124 | static int remote_can_async_p (void); |
125 | ||
126 | static int remote_is_async_p (void); | |
127 | ||
128 | static void remote_async (void (*callback) (enum inferior_event_type event_type, | |
129 | void *context), void *context); | |
130 | ||
131 | static int remote_async_mask (int new_mask); | |
132 | ||
a14ed312 | 133 | static void remote_detach (char *args, int from_tty); |
c906108c | 134 | |
a14ed312 | 135 | static void remote_interrupt (int signo); |
c906108c | 136 | |
a14ed312 | 137 | static void remote_interrupt_twice (int signo); |
7a292a7a | 138 | |
a14ed312 | 139 | static void interrupt_query (void); |
c906108c | 140 | |
a14ed312 | 141 | static void set_thread (int, int); |
c906108c | 142 | |
39f77062 | 143 | static int remote_thread_alive (ptid_t); |
c906108c | 144 | |
a14ed312 | 145 | static void get_offsets (void); |
c906108c | 146 | |
6d820c5c DJ |
147 | static void skip_frame (void); |
148 | ||
149 | static long read_frame (char **buf_p, long *sizeof_buf); | |
c906108c | 150 | |
a14ed312 | 151 | static int hexnumlen (ULONGEST num); |
c906108c | 152 | |
a14ed312 | 153 | static void init_remote_ops (void); |
c906108c | 154 | |
a14ed312 | 155 | static void init_extended_remote_ops (void); |
c906108c | 156 | |
a14ed312 | 157 | static void remote_stop (void); |
c906108c | 158 | |
a14ed312 | 159 | static int ishex (int ch, int *val); |
c906108c | 160 | |
a14ed312 | 161 | static int stubhex (int ch); |
c906108c | 162 | |
a14ed312 | 163 | static int hexnumstr (char *, ULONGEST); |
c906108c | 164 | |
a14ed312 | 165 | static int hexnumnstr (char *, ULONGEST, int); |
2df3850c | 166 | |
a14ed312 | 167 | static CORE_ADDR remote_address_masked (CORE_ADDR); |
c906108c | 168 | |
a14ed312 | 169 | static void print_packet (char *); |
c906108c | 170 | |
a14ed312 | 171 | static unsigned long crc32 (unsigned char *, int, unsigned int); |
c906108c | 172 | |
a14ed312 | 173 | static void compare_sections_command (char *, int); |
c906108c | 174 | |
a14ed312 | 175 | static void packet_command (char *, int); |
c906108c | 176 | |
a14ed312 | 177 | static int stub_unpack_int (char *buff, int fieldlength); |
c906108c | 178 | |
39f77062 | 179 | static ptid_t remote_current_thread (ptid_t oldptid); |
c906108c | 180 | |
a14ed312 | 181 | static void remote_find_new_threads (void); |
c906108c | 182 | |
a14ed312 | 183 | static void record_currthread (int currthread); |
c906108c | 184 | |
30559e10 | 185 | static int fromhex (int a); |
c906108c | 186 | |
cfd77fa1 | 187 | static int hex2bin (const char *hex, gdb_byte *bin, int count); |
c906108c | 188 | |
cfd77fa1 | 189 | static int bin2hex (const gdb_byte *bin, char *hex, int count); |
234fa6d1 | 190 | |
a14ed312 | 191 | static int putpkt_binary (char *buf, int cnt); |
c906108c | 192 | |
a14ed312 | 193 | static void check_binary_download (CORE_ADDR addr); |
c906108c | 194 | |
5a2468f5 | 195 | struct packet_config; |
5a2468f5 | 196 | |
a14ed312 | 197 | static void show_packet_config_cmd (struct packet_config *config); |
5a2468f5 | 198 | |
d471ea57 | 199 | static void update_packet_config (struct packet_config *config); |
5a2468f5 | 200 | |
bb572ddd DJ |
201 | static void set_remote_protocol_packet_cmd (char *args, int from_tty, |
202 | struct cmd_list_element *c); | |
203 | ||
204 | static void show_remote_protocol_packet_cmd (struct ui_file *file, | |
205 | int from_tty, | |
206 | struct cmd_list_element *c, | |
207 | const char *value); | |
208 | ||
a14ed312 | 209 | void _initialize_remote (void); |
c906108c | 210 | |
75c99385 PA |
211 | /* Controls if async mode is permitted. */ |
212 | static int remote_async_permitted = 0; | |
213 | ||
214 | static int remote_async_permitted_set = 0; | |
215 | ||
216 | static void | |
217 | set_maintenance_remote_async_permitted (char *args, int from_tty, | |
218 | struct cmd_list_element *c) | |
219 | { | |
220 | if (target_has_execution) | |
221 | { | |
222 | remote_async_permitted_set = remote_async_permitted; /* revert */ | |
223 | error (_("Cannot change this setting while the inferior is running.")); | |
224 | } | |
225 | ||
226 | remote_async_permitted = remote_async_permitted_set; | |
227 | } | |
228 | ||
229 | static void | |
230 | show_maintenance_remote_async_permitted (struct ui_file *file, int from_tty, | |
231 | struct cmd_list_element *c, const char *value) | |
232 | { | |
233 | fprintf_filtered (file, _("\ | |
234 | Controlling the remote inferior in asynchronous mode is %s.\n"), | |
235 | value); | |
236 | } | |
237 | ||
a6b151f1 DJ |
238 | /* For "remote". */ |
239 | ||
240 | static struct cmd_list_element *remote_cmdlist; | |
241 | ||
bb572ddd DJ |
242 | /* For "set remote" and "show remote". */ |
243 | ||
244 | static struct cmd_list_element *remote_set_cmdlist; | |
245 | static struct cmd_list_element *remote_show_cmdlist; | |
246 | ||
ea9c271d DJ |
247 | /* Description of the remote protocol state for the currently |
248 | connected target. This is per-target state, and independent of the | |
249 | selected architecture. */ | |
250 | ||
251 | struct remote_state | |
252 | { | |
253 | /* A buffer to use for incoming packets, and its current size. The | |
254 | buffer is grown dynamically for larger incoming packets. | |
255 | Outgoing packets may also be constructed in this buffer. | |
256 | BUF_SIZE is always at least REMOTE_PACKET_SIZE; | |
257 | REMOTE_PACKET_SIZE should be used to limit the length of outgoing | |
258 | packets. */ | |
259 | char *buf; | |
260 | long buf_size; | |
be2a5f71 DJ |
261 | |
262 | /* If we negotiated packet size explicitly (and thus can bypass | |
263 | heuristics for the largest packet size that will not overflow | |
264 | a buffer in the stub), this will be set to that packet size. | |
265 | Otherwise zero, meaning to use the guessed size. */ | |
266 | long explicit_packet_size; | |
2d717e4f DJ |
267 | |
268 | /* remote_wait is normally called when the target is running and | |
269 | waits for a stop reply packet. But sometimes we need to call it | |
270 | when the target is already stopped. We can send a "?" packet | |
271 | and have remote_wait read the response. Or, if we already have | |
272 | the response, we can stash it in BUF and tell remote_wait to | |
273 | skip calling getpkt. This flag is set when BUF contains a | |
274 | stop reply packet and the target is not waiting. */ | |
275 | int cached_wait_status; | |
ea9c271d DJ |
276 | }; |
277 | ||
278 | /* This data could be associated with a target, but we do not always | |
279 | have access to the current target when we need it, so for now it is | |
280 | static. This will be fine for as long as only one target is in use | |
281 | at a time. */ | |
282 | static struct remote_state remote_state; | |
283 | ||
284 | static struct remote_state * | |
0b83947e | 285 | get_remote_state_raw (void) |
ea9c271d DJ |
286 | { |
287 | return &remote_state; | |
288 | } | |
289 | ||
290 | /* Description of the remote protocol for a given architecture. */ | |
d01949b6 | 291 | |
ad10f812 AC |
292 | struct packet_reg |
293 | { | |
294 | long offset; /* Offset into G packet. */ | |
295 | long regnum; /* GDB's internal register number. */ | |
296 | LONGEST pnum; /* Remote protocol register number. */ | |
b323314b | 297 | int in_g_packet; /* Always part of G packet. */ |
2bc416ba | 298 | /* long size in bytes; == register_size (current_gdbarch, regnum); |
23860348 | 299 | at present. */ |
c9f4d572 UW |
300 | /* char *name; == gdbarch_register_name (current_gdbarch, regnum); |
301 | at present. */ | |
ad10f812 AC |
302 | }; |
303 | ||
ea9c271d | 304 | struct remote_arch_state |
d01949b6 | 305 | { |
ad10f812 AC |
306 | /* Description of the remote protocol registers. */ |
307 | long sizeof_g_packet; | |
b323314b AC |
308 | |
309 | /* Description of the remote protocol registers indexed by REGNUM | |
f57d151a | 310 | (making an array gdbarch_num_regs in size). */ |
b323314b | 311 | struct packet_reg *regs; |
ad10f812 | 312 | |
d01949b6 AC |
313 | /* This is the size (in chars) of the first response to the ``g'' |
314 | packet. It is used as a heuristic when determining the maximum | |
315 | size of memory-read and memory-write packets. A target will | |
316 | typically only reserve a buffer large enough to hold the ``g'' | |
317 | packet. The size does not include packet overhead (headers and | |
23860348 | 318 | trailers). */ |
d01949b6 AC |
319 | long actual_register_packet_size; |
320 | ||
321 | /* This is the maximum size (in chars) of a non read/write packet. | |
23860348 | 322 | It is also used as a cap on the size of read/write packets. */ |
d01949b6 AC |
323 | long remote_packet_size; |
324 | }; | |
325 | ||
3c3bea1c | 326 | |
d01949b6 AC |
327 | /* Handle for retreving the remote protocol data from gdbarch. */ |
328 | static struct gdbarch_data *remote_gdbarch_data_handle; | |
329 | ||
ea9c271d DJ |
330 | static struct remote_arch_state * |
331 | get_remote_arch_state (void) | |
d01949b6 | 332 | { |
451fbdda | 333 | return gdbarch_data (current_gdbarch, remote_gdbarch_data_handle); |
d01949b6 AC |
334 | } |
335 | ||
0b83947e DJ |
336 | /* Fetch the global remote target state. */ |
337 | ||
338 | static struct remote_state * | |
339 | get_remote_state (void) | |
340 | { | |
341 | /* Make sure that the remote architecture state has been | |
342 | initialized, because doing so might reallocate rs->buf. Any | |
343 | function which calls getpkt also needs to be mindful of changes | |
344 | to rs->buf, but this call limits the number of places which run | |
345 | into trouble. */ | |
346 | get_remote_arch_state (); | |
347 | ||
348 | return get_remote_state_raw (); | |
349 | } | |
350 | ||
74ca34ce DJ |
351 | static int |
352 | compare_pnums (const void *lhs_, const void *rhs_) | |
353 | { | |
354 | const struct packet_reg * const *lhs = lhs_; | |
355 | const struct packet_reg * const *rhs = rhs_; | |
356 | ||
357 | if ((*lhs)->pnum < (*rhs)->pnum) | |
358 | return -1; | |
359 | else if ((*lhs)->pnum == (*rhs)->pnum) | |
360 | return 0; | |
361 | else | |
362 | return 1; | |
363 | } | |
364 | ||
d01949b6 AC |
365 | static void * |
366 | init_remote_state (struct gdbarch *gdbarch) | |
367 | { | |
74ca34ce | 368 | int regnum, num_remote_regs, offset; |
0b83947e | 369 | struct remote_state *rs = get_remote_state_raw (); |
ea9c271d | 370 | struct remote_arch_state *rsa; |
74ca34ce | 371 | struct packet_reg **remote_regs; |
ea9c271d DJ |
372 | |
373 | rsa = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct remote_arch_state); | |
d01949b6 | 374 | |
123dc839 DJ |
375 | /* Use the architecture to build a regnum<->pnum table, which will be |
376 | 1:1 unless a feature set specifies otherwise. */ | |
f57d151a | 377 | rsa->regs = GDBARCH_OBSTACK_CALLOC (gdbarch, |
4a22f64d | 378 | gdbarch_num_regs (gdbarch), |
f57d151a | 379 | struct packet_reg); |
4a22f64d | 380 | for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++) |
ad10f812 | 381 | { |
ea9c271d | 382 | struct packet_reg *r = &rsa->regs[regnum]; |
baef701f | 383 | |
4a22f64d | 384 | if (register_size (gdbarch, regnum) == 0) |
baef701f DJ |
385 | /* Do not try to fetch zero-sized (placeholder) registers. */ |
386 | r->pnum = -1; | |
387 | else | |
388 | r->pnum = gdbarch_remote_register_number (gdbarch, regnum); | |
389 | ||
b323314b | 390 | r->regnum = regnum; |
74ca34ce DJ |
391 | } |
392 | ||
393 | /* Define the g/G packet format as the contents of each register | |
394 | with a remote protocol number, in order of ascending protocol | |
395 | number. */ | |
396 | ||
4a22f64d UW |
397 | remote_regs = alloca (gdbarch_num_regs (gdbarch) |
398 | * sizeof (struct packet_reg *)); | |
f57d151a | 399 | for (num_remote_regs = 0, regnum = 0; |
4a22f64d | 400 | regnum < gdbarch_num_regs (gdbarch); |
f57d151a | 401 | regnum++) |
74ca34ce DJ |
402 | if (rsa->regs[regnum].pnum != -1) |
403 | remote_regs[num_remote_regs++] = &rsa->regs[regnum]; | |
7d58c67d | 404 | |
74ca34ce DJ |
405 | qsort (remote_regs, num_remote_regs, sizeof (struct packet_reg *), |
406 | compare_pnums); | |
407 | ||
408 | for (regnum = 0, offset = 0; regnum < num_remote_regs; regnum++) | |
409 | { | |
410 | remote_regs[regnum]->in_g_packet = 1; | |
411 | remote_regs[regnum]->offset = offset; | |
4a22f64d | 412 | offset += register_size (gdbarch, remote_regs[regnum]->regnum); |
ad10f812 AC |
413 | } |
414 | ||
74ca34ce DJ |
415 | /* Record the maximum possible size of the g packet - it may turn out |
416 | to be smaller. */ | |
417 | rsa->sizeof_g_packet = offset; | |
418 | ||
d01949b6 AC |
419 | /* Default maximum number of characters in a packet body. Many |
420 | remote stubs have a hardwired buffer size of 400 bytes | |
421 | (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used | |
422 | as the maximum packet-size to ensure that the packet and an extra | |
423 | NUL character can always fit in the buffer. This stops GDB | |
424 | trashing stubs that try to squeeze an extra NUL into what is | |
ea9c271d DJ |
425 | already a full buffer (As of 1999-12-04 that was most stubs). */ |
426 | rsa->remote_packet_size = 400 - 1; | |
d01949b6 | 427 | |
ea9c271d DJ |
428 | /* This one is filled in when a ``g'' packet is received. */ |
429 | rsa->actual_register_packet_size = 0; | |
430 | ||
431 | /* Should rsa->sizeof_g_packet needs more space than the | |
ad10f812 AC |
432 | default, adjust the size accordingly. Remember that each byte is |
433 | encoded as two characters. 32 is the overhead for the packet | |
434 | header / footer. NOTE: cagney/1999-10-26: I suspect that 8 | |
d01949b6 | 435 | (``$NN:G...#NN'') is a better guess, the below has been padded a |
23860348 | 436 | little. */ |
ea9c271d DJ |
437 | if (rsa->sizeof_g_packet > ((rsa->remote_packet_size - 32) / 2)) |
438 | rsa->remote_packet_size = (rsa->sizeof_g_packet * 2 + 32); | |
802188a7 | 439 | |
ea9c271d DJ |
440 | /* Make sure that the packet buffer is plenty big enough for |
441 | this architecture. */ | |
442 | if (rs->buf_size < rsa->remote_packet_size) | |
443 | { | |
444 | rs->buf_size = 2 * rsa->remote_packet_size; | |
7fca722e | 445 | rs->buf = xrealloc (rs->buf, rs->buf_size); |
ea9c271d | 446 | } |
6d820c5c | 447 | |
ea9c271d DJ |
448 | return rsa; |
449 | } | |
450 | ||
451 | /* Return the current allowed size of a remote packet. This is | |
452 | inferred from the current architecture, and should be used to | |
453 | limit the length of outgoing packets. */ | |
454 | static long | |
455 | get_remote_packet_size (void) | |
456 | { | |
be2a5f71 | 457 | struct remote_state *rs = get_remote_state (); |
ea9c271d DJ |
458 | struct remote_arch_state *rsa = get_remote_arch_state (); |
459 | ||
be2a5f71 DJ |
460 | if (rs->explicit_packet_size) |
461 | return rs->explicit_packet_size; | |
462 | ||
ea9c271d | 463 | return rsa->remote_packet_size; |
d01949b6 AC |
464 | } |
465 | ||
ad10f812 | 466 | static struct packet_reg * |
ea9c271d | 467 | packet_reg_from_regnum (struct remote_arch_state *rsa, long regnum) |
ad10f812 | 468 | { |
f57d151a | 469 | if (regnum < 0 && regnum >= gdbarch_num_regs (current_gdbarch)) |
b323314b AC |
470 | return NULL; |
471 | else | |
ad10f812 | 472 | { |
ea9c271d | 473 | struct packet_reg *r = &rsa->regs[regnum]; |
b323314b AC |
474 | gdb_assert (r->regnum == regnum); |
475 | return r; | |
ad10f812 | 476 | } |
ad10f812 AC |
477 | } |
478 | ||
479 | static struct packet_reg * | |
ea9c271d | 480 | packet_reg_from_pnum (struct remote_arch_state *rsa, LONGEST pnum) |
ad10f812 | 481 | { |
b323314b | 482 | int i; |
f57d151a | 483 | for (i = 0; i < gdbarch_num_regs (current_gdbarch); i++) |
ad10f812 | 484 | { |
ea9c271d | 485 | struct packet_reg *r = &rsa->regs[i]; |
b323314b AC |
486 | if (r->pnum == pnum) |
487 | return r; | |
ad10f812 AC |
488 | } |
489 | return NULL; | |
d01949b6 AC |
490 | } |
491 | ||
3c3bea1c GS |
492 | /* FIXME: graces/2002-08-08: These variables should eventually be |
493 | bound to an instance of the target object (as in gdbarch-tdep()), | |
494 | when such a thing exists. */ | |
495 | ||
496 | /* This is set to the data address of the access causing the target | |
497 | to stop for a watchpoint. */ | |
498 | static CORE_ADDR remote_watch_data_address; | |
499 | ||
94e08568 | 500 | /* This is non-zero if target stopped for a watchpoint. */ |
3c3bea1c GS |
501 | static int remote_stopped_by_watchpoint_p; |
502 | ||
c906108c SS |
503 | static struct target_ops remote_ops; |
504 | ||
505 | static struct target_ops extended_remote_ops; | |
506 | ||
b84876c2 PA |
507 | static int remote_async_mask_value = 1; |
508 | ||
6426a772 JM |
509 | /* FIXME: cagney/1999-09-23: Even though getpkt was called with |
510 | ``forever'' still use the normal timeout mechanism. This is | |
511 | currently used by the ASYNC code to guarentee that target reads | |
512 | during the initial connect always time-out. Once getpkt has been | |
513 | modified to return a timeout indication and, in turn | |
514 | remote_wait()/wait_for_inferior() have gained a timeout parameter | |
23860348 | 515 | this can go away. */ |
6426a772 JM |
516 | static int wait_forever_enabled_p = 1; |
517 | ||
518 | ||
c906108c SS |
519 | /* This variable chooses whether to send a ^C or a break when the user |
520 | requests program interruption. Although ^C is usually what remote | |
521 | systems expect, and that is the default here, sometimes a break is | |
522 | preferable instead. */ | |
523 | ||
524 | static int remote_break; | |
525 | ||
c906108c SS |
526 | /* Descriptor for I/O to remote machine. Initialize it to NULL so that |
527 | remote_open knows that we don't have a file open when the program | |
528 | starts. */ | |
819cc324 | 529 | static struct serial *remote_desc = NULL; |
c906108c | 530 | |
c906108c SS |
531 | /* This variable sets the number of bits in an address that are to be |
532 | sent in a memory ("M" or "m") packet. Normally, after stripping | |
533 | leading zeros, the entire address would be sent. This variable | |
534 | restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The | |
535 | initial implementation of remote.c restricted the address sent in | |
536 | memory packets to ``host::sizeof long'' bytes - (typically 32 | |
537 | bits). Consequently, for 64 bit targets, the upper 32 bits of an | |
538 | address was never sent. Since fixing this bug may cause a break in | |
539 | some remote targets this variable is principly provided to | |
23860348 | 540 | facilitate backward compatibility. */ |
c906108c SS |
541 | |
542 | static int remote_address_size; | |
543 | ||
75c99385 PA |
544 | /* Temporary to track who currently owns the terminal. See |
545 | remote_terminal_* for more details. */ | |
6426a772 JM |
546 | |
547 | static int remote_async_terminal_ours_p; | |
548 | ||
2d717e4f DJ |
549 | /* The executable file to use for "run" on the remote side. */ |
550 | ||
551 | static char *remote_exec_file = ""; | |
552 | ||
11cf8741 | 553 | \f |
11cf8741 | 554 | /* User configurable variables for the number of characters in a |
ea9c271d DJ |
555 | memory read/write packet. MIN (rsa->remote_packet_size, |
556 | rsa->sizeof_g_packet) is the default. Some targets need smaller | |
24b06219 | 557 | values (fifo overruns, et.al.) and some users need larger values |
ad10f812 AC |
558 | (speed up transfers). The variables ``preferred_*'' (the user |
559 | request), ``current_*'' (what was actually set) and ``forced_*'' | |
23860348 | 560 | (Positive - a soft limit, negative - a hard limit). */ |
11cf8741 JM |
561 | |
562 | struct memory_packet_config | |
563 | { | |
564 | char *name; | |
565 | long size; | |
566 | int fixed_p; | |
567 | }; | |
568 | ||
569 | /* Compute the current size of a read/write packet. Since this makes | |
570 | use of ``actual_register_packet_size'' the computation is dynamic. */ | |
571 | ||
572 | static long | |
573 | get_memory_packet_size (struct memory_packet_config *config) | |
574 | { | |
d01949b6 | 575 | struct remote_state *rs = get_remote_state (); |
ea9c271d DJ |
576 | struct remote_arch_state *rsa = get_remote_arch_state (); |
577 | ||
11cf8741 JM |
578 | /* NOTE: The somewhat arbitrary 16k comes from the knowledge (folk |
579 | law?) that some hosts don't cope very well with large alloca() | |
580 | calls. Eventually the alloca() code will be replaced by calls to | |
581 | xmalloc() and make_cleanups() allowing this restriction to either | |
23860348 | 582 | be lifted or removed. */ |
11cf8741 JM |
583 | #ifndef MAX_REMOTE_PACKET_SIZE |
584 | #define MAX_REMOTE_PACKET_SIZE 16384 | |
585 | #endif | |
3de11b2e | 586 | /* NOTE: 20 ensures we can write at least one byte. */ |
11cf8741 | 587 | #ifndef MIN_REMOTE_PACKET_SIZE |
3de11b2e | 588 | #define MIN_REMOTE_PACKET_SIZE 20 |
11cf8741 JM |
589 | #endif |
590 | long what_they_get; | |
591 | if (config->fixed_p) | |
592 | { | |
593 | if (config->size <= 0) | |
594 | what_they_get = MAX_REMOTE_PACKET_SIZE; | |
595 | else | |
596 | what_they_get = config->size; | |
597 | } | |
598 | else | |
599 | { | |
ea9c271d | 600 | what_they_get = get_remote_packet_size (); |
23860348 | 601 | /* Limit the packet to the size specified by the user. */ |
11cf8741 JM |
602 | if (config->size > 0 |
603 | && what_they_get > config->size) | |
604 | what_they_get = config->size; | |
be2a5f71 DJ |
605 | |
606 | /* Limit it to the size of the targets ``g'' response unless we have | |
607 | permission from the stub to use a larger packet size. */ | |
608 | if (rs->explicit_packet_size == 0 | |
609 | && rsa->actual_register_packet_size > 0 | |
610 | && what_they_get > rsa->actual_register_packet_size) | |
611 | what_they_get = rsa->actual_register_packet_size; | |
11cf8741 JM |
612 | } |
613 | if (what_they_get > MAX_REMOTE_PACKET_SIZE) | |
614 | what_they_get = MAX_REMOTE_PACKET_SIZE; | |
615 | if (what_they_get < MIN_REMOTE_PACKET_SIZE) | |
616 | what_they_get = MIN_REMOTE_PACKET_SIZE; | |
6d820c5c DJ |
617 | |
618 | /* Make sure there is room in the global buffer for this packet | |
619 | (including its trailing NUL byte). */ | |
620 | if (rs->buf_size < what_they_get + 1) | |
621 | { | |
622 | rs->buf_size = 2 * what_they_get; | |
623 | rs->buf = xrealloc (rs->buf, 2 * what_they_get); | |
624 | } | |
625 | ||
11cf8741 JM |
626 | return what_they_get; |
627 | } | |
628 | ||
629 | /* Update the size of a read/write packet. If they user wants | |
23860348 | 630 | something really big then do a sanity check. */ |
11cf8741 JM |
631 | |
632 | static void | |
633 | set_memory_packet_size (char *args, struct memory_packet_config *config) | |
634 | { | |
635 | int fixed_p = config->fixed_p; | |
636 | long size = config->size; | |
637 | if (args == NULL) | |
8a3fe4f8 | 638 | error (_("Argument required (integer, `fixed' or `limited').")); |
11cf8741 JM |
639 | else if (strcmp (args, "hard") == 0 |
640 | || strcmp (args, "fixed") == 0) | |
641 | fixed_p = 1; | |
642 | else if (strcmp (args, "soft") == 0 | |
643 | || strcmp (args, "limit") == 0) | |
644 | fixed_p = 0; | |
645 | else | |
646 | { | |
647 | char *end; | |
648 | size = strtoul (args, &end, 0); | |
649 | if (args == end) | |
8a3fe4f8 | 650 | error (_("Invalid %s (bad syntax)."), config->name); |
11cf8741 JM |
651 | #if 0 |
652 | /* Instead of explicitly capping the size of a packet to | |
653 | MAX_REMOTE_PACKET_SIZE or dissallowing it, the user is | |
654 | instead allowed to set the size to something arbitrarily | |
23860348 | 655 | large. */ |
11cf8741 | 656 | if (size > MAX_REMOTE_PACKET_SIZE) |
8a3fe4f8 | 657 | error (_("Invalid %s (too large)."), config->name); |
11cf8741 JM |
658 | #endif |
659 | } | |
23860348 | 660 | /* Extra checks? */ |
11cf8741 JM |
661 | if (fixed_p && !config->fixed_p) |
662 | { | |
e2e0b3e5 AC |
663 | if (! query (_("The target may not be able to correctly handle a %s\n" |
664 | "of %ld bytes. Change the packet size? "), | |
11cf8741 | 665 | config->name, size)) |
8a3fe4f8 | 666 | error (_("Packet size not changed.")); |
11cf8741 | 667 | } |
23860348 | 668 | /* Update the config. */ |
11cf8741 JM |
669 | config->fixed_p = fixed_p; |
670 | config->size = size; | |
671 | } | |
672 | ||
673 | static void | |
674 | show_memory_packet_size (struct memory_packet_config *config) | |
675 | { | |
a3f17187 | 676 | printf_filtered (_("The %s is %ld. "), config->name, config->size); |
11cf8741 | 677 | if (config->fixed_p) |
a3f17187 | 678 | printf_filtered (_("Packets are fixed at %ld bytes.\n"), |
11cf8741 JM |
679 | get_memory_packet_size (config)); |
680 | else | |
a3f17187 | 681 | printf_filtered (_("Packets are limited to %ld bytes.\n"), |
11cf8741 JM |
682 | get_memory_packet_size (config)); |
683 | } | |
684 | ||
685 | static struct memory_packet_config memory_write_packet_config = | |
686 | { | |
687 | "memory-write-packet-size", | |
688 | }; | |
689 | ||
690 | static void | |
691 | set_memory_write_packet_size (char *args, int from_tty) | |
692 | { | |
693 | set_memory_packet_size (args, &memory_write_packet_config); | |
694 | } | |
695 | ||
696 | static void | |
697 | show_memory_write_packet_size (char *args, int from_tty) | |
698 | { | |
699 | show_memory_packet_size (&memory_write_packet_config); | |
700 | } | |
701 | ||
702 | static long | |
703 | get_memory_write_packet_size (void) | |
704 | { | |
705 | return get_memory_packet_size (&memory_write_packet_config); | |
706 | } | |
707 | ||
708 | static struct memory_packet_config memory_read_packet_config = | |
709 | { | |
710 | "memory-read-packet-size", | |
711 | }; | |
712 | ||
713 | static void | |
714 | set_memory_read_packet_size (char *args, int from_tty) | |
715 | { | |
716 | set_memory_packet_size (args, &memory_read_packet_config); | |
717 | } | |
718 | ||
719 | static void | |
720 | show_memory_read_packet_size (char *args, int from_tty) | |
721 | { | |
722 | show_memory_packet_size (&memory_read_packet_config); | |
723 | } | |
724 | ||
725 | static long | |
726 | get_memory_read_packet_size (void) | |
727 | { | |
728 | long size = get_memory_packet_size (&memory_read_packet_config); | |
729 | /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an | |
730 | extra buffer size argument before the memory read size can be | |
ea9c271d DJ |
731 | increased beyond this. */ |
732 | if (size > get_remote_packet_size ()) | |
733 | size = get_remote_packet_size (); | |
11cf8741 JM |
734 | return size; |
735 | } | |
736 | ||
11cf8741 | 737 | \f |
5a2468f5 JM |
738 | /* Generic configuration support for packets the stub optionally |
739 | supports. Allows the user to specify the use of the packet as well | |
23860348 | 740 | as allowing GDB to auto-detect support in the remote stub. */ |
5a2468f5 JM |
741 | |
742 | enum packet_support | |
743 | { | |
744 | PACKET_SUPPORT_UNKNOWN = 0, | |
745 | PACKET_ENABLE, | |
746 | PACKET_DISABLE | |
747 | }; | |
748 | ||
5a2468f5 JM |
749 | struct packet_config |
750 | { | |
bb572ddd DJ |
751 | const char *name; |
752 | const char *title; | |
7f19b9a2 | 753 | enum auto_boolean detect; |
5a2468f5 JM |
754 | enum packet_support support; |
755 | }; | |
756 | ||
d471ea57 | 757 | /* Analyze a packet's return value and update the packet config |
23860348 | 758 | accordingly. */ |
d471ea57 AC |
759 | |
760 | enum packet_result | |
761 | { | |
762 | PACKET_ERROR, | |
763 | PACKET_OK, | |
764 | PACKET_UNKNOWN | |
765 | }; | |
766 | ||
5a2468f5 | 767 | static void |
d471ea57 | 768 | update_packet_config (struct packet_config *config) |
5a2468f5 | 769 | { |
d471ea57 AC |
770 | switch (config->detect) |
771 | { | |
7f19b9a2 | 772 | case AUTO_BOOLEAN_TRUE: |
d471ea57 AC |
773 | config->support = PACKET_ENABLE; |
774 | break; | |
7f19b9a2 | 775 | case AUTO_BOOLEAN_FALSE: |
d471ea57 AC |
776 | config->support = PACKET_DISABLE; |
777 | break; | |
7f19b9a2 | 778 | case AUTO_BOOLEAN_AUTO: |
d471ea57 AC |
779 | config->support = PACKET_SUPPORT_UNKNOWN; |
780 | break; | |
781 | } | |
5a2468f5 JM |
782 | } |
783 | ||
784 | static void | |
fba45db2 | 785 | show_packet_config_cmd (struct packet_config *config) |
5a2468f5 JM |
786 | { |
787 | char *support = "internal-error"; | |
788 | switch (config->support) | |
789 | { | |
790 | case PACKET_ENABLE: | |
791 | support = "enabled"; | |
792 | break; | |
793 | case PACKET_DISABLE: | |
794 | support = "disabled"; | |
795 | break; | |
796 | case PACKET_SUPPORT_UNKNOWN: | |
797 | support = "unknown"; | |
798 | break; | |
799 | } | |
800 | switch (config->detect) | |
801 | { | |
7f19b9a2 | 802 | case AUTO_BOOLEAN_AUTO: |
37a105a1 DJ |
803 | printf_filtered (_("Support for the `%s' packet is auto-detected, currently %s.\n"), |
804 | config->name, support); | |
5a2468f5 | 805 | break; |
7f19b9a2 AC |
806 | case AUTO_BOOLEAN_TRUE: |
807 | case AUTO_BOOLEAN_FALSE: | |
37a105a1 DJ |
808 | printf_filtered (_("Support for the `%s' packet is currently %s.\n"), |
809 | config->name, support); | |
8e248173 | 810 | break; |
5a2468f5 JM |
811 | } |
812 | } | |
813 | ||
814 | static void | |
bb572ddd DJ |
815 | add_packet_config_cmd (struct packet_config *config, const char *name, |
816 | const char *title, int legacy) | |
d471ea57 | 817 | { |
5a2468f5 JM |
818 | char *set_doc; |
819 | char *show_doc; | |
d471ea57 | 820 | char *cmd_name; |
3ed07be4 | 821 | |
5a2468f5 JM |
822 | config->name = name; |
823 | config->title = title; | |
7f19b9a2 | 824 | config->detect = AUTO_BOOLEAN_AUTO; |
8e248173 | 825 | config->support = PACKET_SUPPORT_UNKNOWN; |
b435e160 AC |
826 | set_doc = xstrprintf ("Set use of remote protocol `%s' (%s) packet", |
827 | name, title); | |
828 | show_doc = xstrprintf ("Show current use of remote protocol `%s' (%s) packet", | |
829 | name, title); | |
d471ea57 | 830 | /* set/show TITLE-packet {auto,on,off} */ |
b435e160 | 831 | cmd_name = xstrprintf ("%s-packet", title); |
e9e68a56 | 832 | add_setshow_auto_boolean_cmd (cmd_name, class_obscure, |
2c5b56ce | 833 | &config->detect, set_doc, show_doc, NULL, /* help_doc */ |
bb572ddd DJ |
834 | set_remote_protocol_packet_cmd, |
835 | show_remote_protocol_packet_cmd, | |
836 | &remote_set_cmdlist, &remote_show_cmdlist); | |
23860348 | 837 | /* set/show remote NAME-packet {auto,on,off} -- legacy. */ |
d471ea57 AC |
838 | if (legacy) |
839 | { | |
840 | char *legacy_name; | |
b435e160 | 841 | legacy_name = xstrprintf ("%s-packet", name); |
d471ea57 | 842 | add_alias_cmd (legacy_name, cmd_name, class_obscure, 0, |
bb572ddd | 843 | &remote_set_cmdlist); |
d471ea57 | 844 | add_alias_cmd (legacy_name, cmd_name, class_obscure, 0, |
bb572ddd | 845 | &remote_show_cmdlist); |
d471ea57 | 846 | } |
5a2468f5 JM |
847 | } |
848 | ||
d471ea57 | 849 | static enum packet_result |
a76d924d | 850 | packet_check_result (const char *buf) |
5a2468f5 | 851 | { |
d471ea57 | 852 | if (buf[0] != '\0') |
5a2468f5 | 853 | { |
d471ea57 | 854 | /* The stub recognized the packet request. Check that the |
23860348 | 855 | operation succeeded. */ |
a76d924d DJ |
856 | if (buf[0] == 'E' |
857 | && isxdigit (buf[1]) && isxdigit (buf[2]) | |
858 | && buf[3] == '\0') | |
859 | /* "Enn" - definitly an error. */ | |
860 | return PACKET_ERROR; | |
861 | ||
862 | /* Always treat "E." as an error. This will be used for | |
863 | more verbose error messages, such as E.memtypes. */ | |
864 | if (buf[0] == 'E' && buf[1] == '.') | |
865 | return PACKET_ERROR; | |
866 | ||
867 | /* The packet may or may not be OK. Just assume it is. */ | |
868 | return PACKET_OK; | |
869 | } | |
870 | else | |
871 | /* The stub does not support the packet. */ | |
872 | return PACKET_UNKNOWN; | |
873 | } | |
874 | ||
875 | static enum packet_result | |
876 | packet_ok (const char *buf, struct packet_config *config) | |
877 | { | |
878 | enum packet_result result; | |
879 | ||
880 | result = packet_check_result (buf); | |
881 | switch (result) | |
882 | { | |
883 | case PACKET_OK: | |
884 | case PACKET_ERROR: | |
885 | /* The stub recognized the packet request. */ | |
d471ea57 AC |
886 | switch (config->support) |
887 | { | |
888 | case PACKET_SUPPORT_UNKNOWN: | |
889 | if (remote_debug) | |
890 | fprintf_unfiltered (gdb_stdlog, | |
891 | "Packet %s (%s) is supported\n", | |
892 | config->name, config->title); | |
893 | config->support = PACKET_ENABLE; | |
894 | break; | |
895 | case PACKET_DISABLE: | |
8e65ff28 | 896 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 897 | _("packet_ok: attempt to use a disabled packet")); |
d471ea57 AC |
898 | break; |
899 | case PACKET_ENABLE: | |
900 | break; | |
901 | } | |
a76d924d DJ |
902 | break; |
903 | case PACKET_UNKNOWN: | |
23860348 | 904 | /* The stub does not support the packet. */ |
d471ea57 AC |
905 | switch (config->support) |
906 | { | |
907 | case PACKET_ENABLE: | |
7f19b9a2 | 908 | if (config->detect == AUTO_BOOLEAN_AUTO) |
d471ea57 | 909 | /* If the stub previously indicated that the packet was |
23860348 | 910 | supported then there is a protocol error.. */ |
8a3fe4f8 | 911 | error (_("Protocol error: %s (%s) conflicting enabled responses."), |
d471ea57 AC |
912 | config->name, config->title); |
913 | else | |
23860348 | 914 | /* The user set it wrong. */ |
8a3fe4f8 | 915 | error (_("Enabled packet %s (%s) not recognized by stub"), |
d471ea57 AC |
916 | config->name, config->title); |
917 | break; | |
918 | case PACKET_SUPPORT_UNKNOWN: | |
919 | if (remote_debug) | |
920 | fprintf_unfiltered (gdb_stdlog, | |
921 | "Packet %s (%s) is NOT supported\n", | |
922 | config->name, config->title); | |
923 | config->support = PACKET_DISABLE; | |
924 | break; | |
925 | case PACKET_DISABLE: | |
926 | break; | |
927 | } | |
a76d924d | 928 | break; |
5a2468f5 | 929 | } |
a76d924d DJ |
930 | |
931 | return result; | |
5a2468f5 JM |
932 | } |
933 | ||
444abaca DJ |
934 | enum { |
935 | PACKET_vCont = 0, | |
936 | PACKET_X, | |
937 | PACKET_qSymbol, | |
938 | PACKET_P, | |
939 | PACKET_p, | |
940 | PACKET_Z0, | |
941 | PACKET_Z1, | |
942 | PACKET_Z2, | |
943 | PACKET_Z3, | |
944 | PACKET_Z4, | |
a6b151f1 DJ |
945 | PACKET_vFile_open, |
946 | PACKET_vFile_pread, | |
947 | PACKET_vFile_pwrite, | |
948 | PACKET_vFile_close, | |
949 | PACKET_vFile_unlink, | |
0876f84a | 950 | PACKET_qXfer_auxv, |
23181151 | 951 | PACKET_qXfer_features, |
cfa9d6d9 | 952 | PACKET_qXfer_libraries, |
fd79ecee | 953 | PACKET_qXfer_memory_map, |
0e7f50da UW |
954 | PACKET_qXfer_spu_read, |
955 | PACKET_qXfer_spu_write, | |
444abaca | 956 | PACKET_qGetTLSAddr, |
be2a5f71 | 957 | PACKET_qSupported, |
89be2091 | 958 | PACKET_QPassSignals, |
08388c79 | 959 | PACKET_qSearch_memory, |
2d717e4f DJ |
960 | PACKET_vAttach, |
961 | PACKET_vRun, | |
444abaca DJ |
962 | PACKET_MAX |
963 | }; | |
506fb367 | 964 | |
444abaca | 965 | static struct packet_config remote_protocol_packets[PACKET_MAX]; |
dc8acb97 MS |
966 | |
967 | static void | |
444abaca DJ |
968 | set_remote_protocol_packet_cmd (char *args, int from_tty, |
969 | struct cmd_list_element *c) | |
dc8acb97 | 970 | { |
444abaca | 971 | struct packet_config *packet; |
dc8acb97 | 972 | |
444abaca DJ |
973 | for (packet = remote_protocol_packets; |
974 | packet < &remote_protocol_packets[PACKET_MAX]; | |
975 | packet++) | |
976 | { | |
977 | if (&packet->detect == c->var) | |
978 | { | |
979 | update_packet_config (packet); | |
980 | return; | |
981 | } | |
982 | } | |
983 | internal_error (__FILE__, __LINE__, "Could not find config for %s", | |
984 | c->name); | |
dc8acb97 MS |
985 | } |
986 | ||
5a2468f5 | 987 | static void |
444abaca DJ |
988 | show_remote_protocol_packet_cmd (struct ui_file *file, int from_tty, |
989 | struct cmd_list_element *c, | |
990 | const char *value) | |
5a2468f5 | 991 | { |
444abaca | 992 | struct packet_config *packet; |
5a2468f5 | 993 | |
444abaca DJ |
994 | for (packet = remote_protocol_packets; |
995 | packet < &remote_protocol_packets[PACKET_MAX]; | |
996 | packet++) | |
997 | { | |
998 | if (&packet->detect == c->var) | |
999 | { | |
1000 | show_packet_config_cmd (packet); | |
1001 | return; | |
1002 | } | |
1003 | } | |
1004 | internal_error (__FILE__, __LINE__, "Could not find config for %s", | |
1005 | c->name); | |
5a2468f5 JM |
1006 | } |
1007 | ||
d471ea57 AC |
1008 | /* Should we try one of the 'Z' requests? */ |
1009 | ||
1010 | enum Z_packet_type | |
1011 | { | |
1012 | Z_PACKET_SOFTWARE_BP, | |
1013 | Z_PACKET_HARDWARE_BP, | |
1014 | Z_PACKET_WRITE_WP, | |
1015 | Z_PACKET_READ_WP, | |
1016 | Z_PACKET_ACCESS_WP, | |
1017 | NR_Z_PACKET_TYPES | |
1018 | }; | |
96baa820 | 1019 | |
d471ea57 | 1020 | /* For compatibility with older distributions. Provide a ``set remote |
23860348 | 1021 | Z-packet ...'' command that updates all the Z packet types. */ |
d471ea57 | 1022 | |
7f19b9a2 | 1023 | static enum auto_boolean remote_Z_packet_detect; |
96baa820 JM |
1024 | |
1025 | static void | |
fba45db2 KB |
1026 | set_remote_protocol_Z_packet_cmd (char *args, int from_tty, |
1027 | struct cmd_list_element *c) | |
96baa820 | 1028 | { |
d471ea57 AC |
1029 | int i; |
1030 | for (i = 0; i < NR_Z_PACKET_TYPES; i++) | |
1031 | { | |
444abaca DJ |
1032 | remote_protocol_packets[PACKET_Z0 + i].detect = remote_Z_packet_detect; |
1033 | update_packet_config (&remote_protocol_packets[PACKET_Z0 + i]); | |
d471ea57 | 1034 | } |
96baa820 JM |
1035 | } |
1036 | ||
1037 | static void | |
08546159 AC |
1038 | show_remote_protocol_Z_packet_cmd (struct ui_file *file, int from_tty, |
1039 | struct cmd_list_element *c, | |
1040 | const char *value) | |
96baa820 | 1041 | { |
d471ea57 AC |
1042 | int i; |
1043 | for (i = 0; i < NR_Z_PACKET_TYPES; i++) | |
1044 | { | |
444abaca | 1045 | show_packet_config_cmd (&remote_protocol_packets[PACKET_Z0 + i]); |
d471ea57 | 1046 | } |
96baa820 JM |
1047 | } |
1048 | ||
9d1f7ab2 MS |
1049 | /* Should we try the 'ThreadInfo' query packet? |
1050 | ||
1051 | This variable (NOT available to the user: auto-detect only!) | |
1052 | determines whether GDB will use the new, simpler "ThreadInfo" | |
1053 | query or the older, more complex syntax for thread queries. | |
802188a7 | 1054 | This is an auto-detect variable (set to true at each connect, |
9d1f7ab2 MS |
1055 | and set to false when the target fails to recognize it). */ |
1056 | ||
1057 | static int use_threadinfo_query; | |
1058 | static int use_threadextra_query; | |
1059 | ||
23860348 | 1060 | /* Tokens for use by the asynchronous signal handlers for SIGINT. */ |
d5d6fca5 DJ |
1061 | static struct async_signal_handler *sigint_remote_twice_token; |
1062 | static struct async_signal_handler *sigint_remote_token; | |
43ff13b4 | 1063 | |
c906108c SS |
1064 | \f |
1065 | ||
c5aa993b | 1066 | |
c906108c SS |
1067 | /* These are the threads which we last sent to the remote system. |
1068 | -1 for all or -2 for not sent yet. */ | |
1069 | static int general_thread; | |
cce74817 | 1070 | static int continue_thread; |
c906108c SS |
1071 | |
1072 | /* Call this function as a result of | |
1073 | 1) A halt indication (T packet) containing a thread id | |
1074 | 2) A direct query of currthread | |
1075 | 3) Successful execution of set thread | |
1076 | */ | |
1077 | ||
1078 | static void | |
fba45db2 | 1079 | record_currthread (int currthread) |
c906108c | 1080 | { |
c906108c | 1081 | general_thread = currthread; |
cce74817 | 1082 | |
c906108c SS |
1083 | /* If this is a new thread, add it to GDB's thread list. |
1084 | If we leave it up to WFI to do this, bad things will happen. */ | |
39f77062 | 1085 | if (!in_thread_list (pid_to_ptid (currthread))) |
93815fbf | 1086 | add_thread (pid_to_ptid (currthread)); |
c906108c SS |
1087 | } |
1088 | ||
89be2091 DJ |
1089 | static char *last_pass_packet; |
1090 | ||
1091 | /* If 'QPassSignals' is supported, tell the remote stub what signals | |
1092 | it can simply pass through to the inferior without reporting. */ | |
1093 | ||
1094 | static void | |
1095 | remote_pass_signals (void) | |
1096 | { | |
1097 | if (remote_protocol_packets[PACKET_QPassSignals].support != PACKET_DISABLE) | |
1098 | { | |
1099 | char *pass_packet, *p; | |
1100 | int numsigs = (int) TARGET_SIGNAL_LAST; | |
1101 | int count = 0, i; | |
1102 | ||
1103 | gdb_assert (numsigs < 256); | |
1104 | for (i = 0; i < numsigs; i++) | |
1105 | { | |
1106 | if (signal_stop_state (i) == 0 | |
1107 | && signal_print_state (i) == 0 | |
1108 | && signal_pass_state (i) == 1) | |
1109 | count++; | |
1110 | } | |
1111 | pass_packet = xmalloc (count * 3 + strlen ("QPassSignals:") + 1); | |
1112 | strcpy (pass_packet, "QPassSignals:"); | |
1113 | p = pass_packet + strlen (pass_packet); | |
1114 | for (i = 0; i < numsigs; i++) | |
1115 | { | |
1116 | if (signal_stop_state (i) == 0 | |
1117 | && signal_print_state (i) == 0 | |
1118 | && signal_pass_state (i) == 1) | |
1119 | { | |
1120 | if (i >= 16) | |
1121 | *p++ = tohex (i >> 4); | |
1122 | *p++ = tohex (i & 15); | |
1123 | if (count) | |
1124 | *p++ = ';'; | |
1125 | else | |
1126 | break; | |
1127 | count--; | |
1128 | } | |
1129 | } | |
1130 | *p = 0; | |
1131 | if (!last_pass_packet || strcmp (last_pass_packet, pass_packet)) | |
1132 | { | |
1133 | struct remote_state *rs = get_remote_state (); | |
1134 | char *buf = rs->buf; | |
1135 | ||
1136 | putpkt (pass_packet); | |
1137 | getpkt (&rs->buf, &rs->buf_size, 0); | |
1138 | packet_ok (buf, &remote_protocol_packets[PACKET_QPassSignals]); | |
1139 | if (last_pass_packet) | |
1140 | xfree (last_pass_packet); | |
1141 | last_pass_packet = pass_packet; | |
1142 | } | |
1143 | else | |
1144 | xfree (pass_packet); | |
1145 | } | |
1146 | } | |
1147 | ||
c906108c SS |
1148 | #define MAGIC_NULL_PID 42000 |
1149 | ||
1150 | static void | |
fba45db2 | 1151 | set_thread (int th, int gen) |
c906108c | 1152 | { |
d01949b6 | 1153 | struct remote_state *rs = get_remote_state (); |
6d820c5c | 1154 | char *buf = rs->buf; |
cce74817 | 1155 | int state = gen ? general_thread : continue_thread; |
c906108c SS |
1156 | |
1157 | if (state == th) | |
1158 | return; | |
1159 | ||
1160 | buf[0] = 'H'; | |
1161 | buf[1] = gen ? 'g' : 'c'; | |
1162 | if (th == MAGIC_NULL_PID) | |
1163 | { | |
1164 | buf[2] = '0'; | |
1165 | buf[3] = '\0'; | |
1166 | } | |
1167 | else if (th < 0) | |
ea9c271d | 1168 | xsnprintf (&buf[2], get_remote_packet_size () - 2, "-%x", -th); |
c906108c | 1169 | else |
ea9c271d | 1170 | xsnprintf (&buf[2], get_remote_packet_size () - 2, "%x", th); |
c906108c | 1171 | putpkt (buf); |
6d820c5c | 1172 | getpkt (&rs->buf, &rs->buf_size, 0); |
c906108c | 1173 | if (gen) |
c5aa993b | 1174 | general_thread = th; |
c906108c | 1175 | else |
cce74817 | 1176 | continue_thread = th; |
c906108c SS |
1177 | } |
1178 | \f | |
1179 | /* Return nonzero if the thread TH is still alive on the remote system. */ | |
1180 | ||
1181 | static int | |
39f77062 | 1182 | remote_thread_alive (ptid_t ptid) |
c906108c | 1183 | { |
6d820c5c | 1184 | struct remote_state *rs = get_remote_state (); |
39f77062 | 1185 | int tid = PIDGET (ptid); |
c906108c | 1186 | |
cce74817 | 1187 | if (tid < 0) |
2e9f7625 | 1188 | xsnprintf (rs->buf, get_remote_packet_size (), "T-%08x", -tid); |
c906108c | 1189 | else |
2e9f7625 DJ |
1190 | xsnprintf (rs->buf, get_remote_packet_size (), "T%08x", tid); |
1191 | putpkt (rs->buf); | |
6d820c5c | 1192 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 1193 | return (rs->buf[0] == 'O' && rs->buf[1] == 'K'); |
c906108c SS |
1194 | } |
1195 | ||
1196 | /* About these extended threadlist and threadinfo packets. They are | |
1197 | variable length packets but, the fields within them are often fixed | |
1198 | length. They are redundent enough to send over UDP as is the | |
1199 | remote protocol in general. There is a matching unit test module | |
1200 | in libstub. */ | |
1201 | ||
cce74817 JM |
1202 | #define OPAQUETHREADBYTES 8 |
1203 | ||
1204 | /* a 64 bit opaque identifier */ | |
1205 | typedef unsigned char threadref[OPAQUETHREADBYTES]; | |
1206 | ||
23860348 MS |
1207 | /* WARNING: This threadref data structure comes from the remote O.S., |
1208 | libstub protocol encoding, and remote.c. it is not particularly | |
1209 | changable. */ | |
cce74817 JM |
1210 | |
1211 | /* Right now, the internal structure is int. We want it to be bigger. | |
1212 | Plan to fix this. | |
c5aa993b | 1213 | */ |
cce74817 | 1214 | |
23860348 | 1215 | typedef int gdb_threadref; /* Internal GDB thread reference. */ |
cce74817 | 1216 | |
9d1f7ab2 | 1217 | /* gdb_ext_thread_info is an internal GDB data structure which is |
cfde0993 | 1218 | equivalent to the reply of the remote threadinfo packet. */ |
cce74817 JM |
1219 | |
1220 | struct gdb_ext_thread_info | |
c5aa993b | 1221 | { |
23860348 | 1222 | threadref threadid; /* External form of thread reference. */ |
2bc416ba | 1223 | int active; /* Has state interesting to GDB? |
23860348 | 1224 | regs, stack. */ |
2bc416ba | 1225 | char display[256]; /* Brief state display, name, |
cedea757 | 1226 | blocked/suspended. */ |
23860348 | 1227 | char shortname[32]; /* To be used to name threads. */ |
2bc416ba | 1228 | char more_display[256]; /* Long info, statistics, queue depth, |
23860348 | 1229 | whatever. */ |
c5aa993b | 1230 | }; |
cce74817 JM |
1231 | |
1232 | /* The volume of remote transfers can be limited by submitting | |
1233 | a mask containing bits specifying the desired information. | |
1234 | Use a union of these values as the 'selection' parameter to | |
1235 | get_thread_info. FIXME: Make these TAG names more thread specific. | |
c5aa993b | 1236 | */ |
cce74817 JM |
1237 | |
1238 | #define TAG_THREADID 1 | |
1239 | #define TAG_EXISTS 2 | |
1240 | #define TAG_DISPLAY 4 | |
1241 | #define TAG_THREADNAME 8 | |
c5aa993b | 1242 | #define TAG_MOREDISPLAY 16 |
cce74817 | 1243 | |
23860348 | 1244 | #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2) |
c906108c | 1245 | |
b2dd6311 | 1246 | char *unpack_varlen_hex (char *buff, ULONGEST *result); |
cce74817 | 1247 | |
a14ed312 | 1248 | static char *unpack_nibble (char *buf, int *val); |
cce74817 | 1249 | |
a14ed312 | 1250 | static char *pack_nibble (char *buf, int nibble); |
cce74817 | 1251 | |
23860348 | 1252 | static char *pack_hex_byte (char *pkt, int /* unsigned char */ byte); |
cce74817 | 1253 | |
a14ed312 | 1254 | static char *unpack_byte (char *buf, int *value); |
cce74817 | 1255 | |
a14ed312 | 1256 | static char *pack_int (char *buf, int value); |
cce74817 | 1257 | |
a14ed312 | 1258 | static char *unpack_int (char *buf, int *value); |
cce74817 | 1259 | |
a14ed312 | 1260 | static char *unpack_string (char *src, char *dest, int length); |
cce74817 | 1261 | |
23860348 | 1262 | static char *pack_threadid (char *pkt, threadref *id); |
cce74817 | 1263 | |
23860348 | 1264 | static char *unpack_threadid (char *inbuf, threadref *id); |
cce74817 | 1265 | |
23860348 | 1266 | void int_to_threadref (threadref *id, int value); |
cce74817 | 1267 | |
23860348 | 1268 | static int threadref_to_int (threadref *ref); |
cce74817 | 1269 | |
23860348 | 1270 | static void copy_threadref (threadref *dest, threadref *src); |
cce74817 | 1271 | |
23860348 | 1272 | static int threadmatch (threadref *dest, threadref *src); |
cce74817 | 1273 | |
2bc416ba | 1274 | static char *pack_threadinfo_request (char *pkt, int mode, |
23860348 | 1275 | threadref *id); |
cce74817 | 1276 | |
a14ed312 | 1277 | static int remote_unpack_thread_info_response (char *pkt, |
23860348 | 1278 | threadref *expectedref, |
a14ed312 KB |
1279 | struct gdb_ext_thread_info |
1280 | *info); | |
cce74817 JM |
1281 | |
1282 | ||
2bc416ba | 1283 | static int remote_get_threadinfo (threadref *threadid, |
23860348 | 1284 | int fieldset, /*TAG mask */ |
a14ed312 | 1285 | struct gdb_ext_thread_info *info); |
cce74817 | 1286 | |
a14ed312 KB |
1287 | static char *pack_threadlist_request (char *pkt, int startflag, |
1288 | int threadcount, | |
23860348 | 1289 | threadref *nextthread); |
cce74817 | 1290 | |
a14ed312 KB |
1291 | static int parse_threadlist_response (char *pkt, |
1292 | int result_limit, | |
23860348 | 1293 | threadref *original_echo, |
2bc416ba | 1294 | threadref *resultlist, |
23860348 | 1295 | int *doneflag); |
cce74817 | 1296 | |
a14ed312 | 1297 | static int remote_get_threadlist (int startflag, |
23860348 | 1298 | threadref *nextthread, |
a14ed312 KB |
1299 | int result_limit, |
1300 | int *done, | |
2bc416ba | 1301 | int *result_count, |
23860348 | 1302 | threadref *threadlist); |
cce74817 | 1303 | |
23860348 | 1304 | typedef int (*rmt_thread_action) (threadref *ref, void *context); |
cce74817 | 1305 | |
a14ed312 KB |
1306 | static int remote_threadlist_iterator (rmt_thread_action stepfunction, |
1307 | void *context, int looplimit); | |
cce74817 | 1308 | |
23860348 | 1309 | static int remote_newthread_step (threadref *ref, void *context); |
cce74817 | 1310 | |
23860348 | 1311 | /* Encode 64 bits in 16 chars of hex. */ |
c906108c SS |
1312 | |
1313 | static const char hexchars[] = "0123456789abcdef"; | |
1314 | ||
1315 | static int | |
fba45db2 | 1316 | ishex (int ch, int *val) |
c906108c SS |
1317 | { |
1318 | if ((ch >= 'a') && (ch <= 'f')) | |
1319 | { | |
1320 | *val = ch - 'a' + 10; | |
1321 | return 1; | |
1322 | } | |
1323 | if ((ch >= 'A') && (ch <= 'F')) | |
1324 | { | |
1325 | *val = ch - 'A' + 10; | |
1326 | return 1; | |
1327 | } | |
1328 | if ((ch >= '0') && (ch <= '9')) | |
1329 | { | |
1330 | *val = ch - '0'; | |
1331 | return 1; | |
1332 | } | |
1333 | return 0; | |
1334 | } | |
1335 | ||
1336 | static int | |
fba45db2 | 1337 | stubhex (int ch) |
c906108c SS |
1338 | { |
1339 | if (ch >= 'a' && ch <= 'f') | |
1340 | return ch - 'a' + 10; | |
1341 | if (ch >= '0' && ch <= '9') | |
1342 | return ch - '0'; | |
1343 | if (ch >= 'A' && ch <= 'F') | |
1344 | return ch - 'A' + 10; | |
1345 | return -1; | |
1346 | } | |
1347 | ||
1348 | static int | |
fba45db2 | 1349 | stub_unpack_int (char *buff, int fieldlength) |
c906108c SS |
1350 | { |
1351 | int nibble; | |
1352 | int retval = 0; | |
1353 | ||
1354 | while (fieldlength) | |
1355 | { | |
1356 | nibble = stubhex (*buff++); | |
1357 | retval |= nibble; | |
1358 | fieldlength--; | |
1359 | if (fieldlength) | |
1360 | retval = retval << 4; | |
1361 | } | |
1362 | return retval; | |
1363 | } | |
1364 | ||
1365 | char * | |
fba45db2 | 1366 | unpack_varlen_hex (char *buff, /* packet to parse */ |
b2dd6311 | 1367 | ULONGEST *result) |
c906108c SS |
1368 | { |
1369 | int nibble; | |
d49c44d5 | 1370 | ULONGEST retval = 0; |
c906108c SS |
1371 | |
1372 | while (ishex (*buff, &nibble)) | |
1373 | { | |
1374 | buff++; | |
1375 | retval = retval << 4; | |
1376 | retval |= nibble & 0x0f; | |
1377 | } | |
1378 | *result = retval; | |
1379 | return buff; | |
1380 | } | |
1381 | ||
1382 | static char * | |
fba45db2 | 1383 | unpack_nibble (char *buf, int *val) |
c906108c | 1384 | { |
b7589f7d | 1385 | *val = fromhex (*buf++); |
c906108c SS |
1386 | return buf; |
1387 | } | |
1388 | ||
1389 | static char * | |
fba45db2 | 1390 | pack_nibble (char *buf, int nibble) |
c906108c SS |
1391 | { |
1392 | *buf++ = hexchars[(nibble & 0x0f)]; | |
1393 | return buf; | |
1394 | } | |
1395 | ||
1396 | static char * | |
fba45db2 | 1397 | pack_hex_byte (char *pkt, int byte) |
c906108c SS |
1398 | { |
1399 | *pkt++ = hexchars[(byte >> 4) & 0xf]; | |
1400 | *pkt++ = hexchars[(byte & 0xf)]; | |
1401 | return pkt; | |
1402 | } | |
1403 | ||
1404 | static char * | |
fba45db2 | 1405 | unpack_byte (char *buf, int *value) |
c906108c SS |
1406 | { |
1407 | *value = stub_unpack_int (buf, 2); | |
1408 | return buf + 2; | |
1409 | } | |
1410 | ||
1411 | static char * | |
fba45db2 | 1412 | pack_int (char *buf, int value) |
c906108c SS |
1413 | { |
1414 | buf = pack_hex_byte (buf, (value >> 24) & 0xff); | |
1415 | buf = pack_hex_byte (buf, (value >> 16) & 0xff); | |
1416 | buf = pack_hex_byte (buf, (value >> 8) & 0x0ff); | |
1417 | buf = pack_hex_byte (buf, (value & 0xff)); | |
1418 | return buf; | |
1419 | } | |
1420 | ||
1421 | static char * | |
fba45db2 | 1422 | unpack_int (char *buf, int *value) |
c906108c SS |
1423 | { |
1424 | *value = stub_unpack_int (buf, 8); | |
1425 | return buf + 8; | |
1426 | } | |
1427 | ||
23860348 | 1428 | #if 0 /* Currently unused, uncomment when needed. */ |
a14ed312 | 1429 | static char *pack_string (char *pkt, char *string); |
c906108c SS |
1430 | |
1431 | static char * | |
fba45db2 | 1432 | pack_string (char *pkt, char *string) |
c906108c SS |
1433 | { |
1434 | char ch; | |
1435 | int len; | |
1436 | ||
1437 | len = strlen (string); | |
1438 | if (len > 200) | |
23860348 | 1439 | len = 200; /* Bigger than most GDB packets, junk??? */ |
c906108c SS |
1440 | pkt = pack_hex_byte (pkt, len); |
1441 | while (len-- > 0) | |
1442 | { | |
1443 | ch = *string++; | |
1444 | if ((ch == '\0') || (ch == '#')) | |
23860348 | 1445 | ch = '*'; /* Protect encapsulation. */ |
c906108c SS |
1446 | *pkt++ = ch; |
1447 | } | |
1448 | return pkt; | |
1449 | } | |
1450 | #endif /* 0 (unused) */ | |
1451 | ||
1452 | static char * | |
fba45db2 | 1453 | unpack_string (char *src, char *dest, int length) |
c906108c SS |
1454 | { |
1455 | while (length--) | |
1456 | *dest++ = *src++; | |
1457 | *dest = '\0'; | |
1458 | return src; | |
1459 | } | |
1460 | ||
1461 | static char * | |
fba45db2 | 1462 | pack_threadid (char *pkt, threadref *id) |
c906108c SS |
1463 | { |
1464 | char *limit; | |
1465 | unsigned char *altid; | |
1466 | ||
1467 | altid = (unsigned char *) id; | |
1468 | limit = pkt + BUF_THREAD_ID_SIZE; | |
1469 | while (pkt < limit) | |
1470 | pkt = pack_hex_byte (pkt, *altid++); | |
1471 | return pkt; | |
1472 | } | |
1473 | ||
1474 | ||
1475 | static char * | |
fba45db2 | 1476 | unpack_threadid (char *inbuf, threadref *id) |
c906108c SS |
1477 | { |
1478 | char *altref; | |
1479 | char *limit = inbuf + BUF_THREAD_ID_SIZE; | |
1480 | int x, y; | |
1481 | ||
1482 | altref = (char *) id; | |
1483 | ||
1484 | while (inbuf < limit) | |
1485 | { | |
1486 | x = stubhex (*inbuf++); | |
1487 | y = stubhex (*inbuf++); | |
1488 | *altref++ = (x << 4) | y; | |
1489 | } | |
1490 | return inbuf; | |
1491 | } | |
1492 | ||
1493 | /* Externally, threadrefs are 64 bits but internally, they are still | |
1494 | ints. This is due to a mismatch of specifications. We would like | |
1495 | to use 64bit thread references internally. This is an adapter | |
1496 | function. */ | |
1497 | ||
1498 | void | |
fba45db2 | 1499 | int_to_threadref (threadref *id, int value) |
c906108c SS |
1500 | { |
1501 | unsigned char *scan; | |
1502 | ||
1503 | scan = (unsigned char *) id; | |
1504 | { | |
1505 | int i = 4; | |
1506 | while (i--) | |
1507 | *scan++ = 0; | |
1508 | } | |
1509 | *scan++ = (value >> 24) & 0xff; | |
1510 | *scan++ = (value >> 16) & 0xff; | |
1511 | *scan++ = (value >> 8) & 0xff; | |
1512 | *scan++ = (value & 0xff); | |
1513 | } | |
1514 | ||
1515 | static int | |
fba45db2 | 1516 | threadref_to_int (threadref *ref) |
c906108c SS |
1517 | { |
1518 | int i, value = 0; | |
1519 | unsigned char *scan; | |
1520 | ||
cfd77fa1 | 1521 | scan = *ref; |
c906108c SS |
1522 | scan += 4; |
1523 | i = 4; | |
1524 | while (i-- > 0) | |
1525 | value = (value << 8) | ((*scan++) & 0xff); | |
1526 | return value; | |
1527 | } | |
1528 | ||
1529 | static void | |
fba45db2 | 1530 | copy_threadref (threadref *dest, threadref *src) |
c906108c SS |
1531 | { |
1532 | int i; | |
1533 | unsigned char *csrc, *cdest; | |
1534 | ||
1535 | csrc = (unsigned char *) src; | |
1536 | cdest = (unsigned char *) dest; | |
1537 | i = 8; | |
1538 | while (i--) | |
1539 | *cdest++ = *csrc++; | |
1540 | } | |
1541 | ||
1542 | static int | |
fba45db2 | 1543 | threadmatch (threadref *dest, threadref *src) |
c906108c | 1544 | { |
23860348 | 1545 | /* Things are broken right now, so just assume we got a match. */ |
c906108c SS |
1546 | #if 0 |
1547 | unsigned char *srcp, *destp; | |
1548 | int i, result; | |
1549 | srcp = (char *) src; | |
1550 | destp = (char *) dest; | |
1551 | ||
1552 | result = 1; | |
1553 | while (i-- > 0) | |
1554 | result &= (*srcp++ == *destp++) ? 1 : 0; | |
1555 | return result; | |
1556 | #endif | |
1557 | return 1; | |
1558 | } | |
1559 | ||
1560 | /* | |
c5aa993b JM |
1561 | threadid:1, # always request threadid |
1562 | context_exists:2, | |
1563 | display:4, | |
1564 | unique_name:8, | |
1565 | more_display:16 | |
1566 | */ | |
c906108c SS |
1567 | |
1568 | /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */ | |
1569 | ||
1570 | static char * | |
fba45db2 | 1571 | pack_threadinfo_request (char *pkt, int mode, threadref *id) |
c906108c | 1572 | { |
23860348 MS |
1573 | *pkt++ = 'q'; /* Info Query */ |
1574 | *pkt++ = 'P'; /* process or thread info */ | |
1575 | pkt = pack_int (pkt, mode); /* mode */ | |
c906108c | 1576 | pkt = pack_threadid (pkt, id); /* threadid */ |
23860348 | 1577 | *pkt = '\0'; /* terminate */ |
c906108c SS |
1578 | return pkt; |
1579 | } | |
1580 | ||
23860348 | 1581 | /* These values tag the fields in a thread info response packet. */ |
c906108c | 1582 | /* Tagging the fields allows us to request specific fields and to |
23860348 | 1583 | add more fields as time goes by. */ |
c906108c | 1584 | |
23860348 | 1585 | #define TAG_THREADID 1 /* Echo the thread identifier. */ |
c5aa993b | 1586 | #define TAG_EXISTS 2 /* Is this process defined enough to |
23860348 | 1587 | fetch registers and its stack? */ |
c5aa993b | 1588 | #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */ |
23860348 | 1589 | #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */ |
802188a7 | 1590 | #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about |
23860348 | 1591 | the process. */ |
c906108c SS |
1592 | |
1593 | static int | |
fba45db2 KB |
1594 | remote_unpack_thread_info_response (char *pkt, threadref *expectedref, |
1595 | struct gdb_ext_thread_info *info) | |
c906108c | 1596 | { |
d01949b6 | 1597 | struct remote_state *rs = get_remote_state (); |
c906108c | 1598 | int mask, length; |
cfd77fa1 | 1599 | int tag; |
c906108c | 1600 | threadref ref; |
6d820c5c | 1601 | char *limit = pkt + rs->buf_size; /* Plausible parsing limit. */ |
c906108c SS |
1602 | int retval = 1; |
1603 | ||
23860348 | 1604 | /* info->threadid = 0; FIXME: implement zero_threadref. */ |
c906108c SS |
1605 | info->active = 0; |
1606 | info->display[0] = '\0'; | |
1607 | info->shortname[0] = '\0'; | |
1608 | info->more_display[0] = '\0'; | |
1609 | ||
23860348 MS |
1610 | /* Assume the characters indicating the packet type have been |
1611 | stripped. */ | |
c906108c SS |
1612 | pkt = unpack_int (pkt, &mask); /* arg mask */ |
1613 | pkt = unpack_threadid (pkt, &ref); | |
1614 | ||
1615 | if (mask == 0) | |
8a3fe4f8 | 1616 | warning (_("Incomplete response to threadinfo request.")); |
c906108c | 1617 | if (!threadmatch (&ref, expectedref)) |
23860348 | 1618 | { /* This is an answer to a different request. */ |
8a3fe4f8 | 1619 | warning (_("ERROR RMT Thread info mismatch.")); |
c906108c SS |
1620 | return 0; |
1621 | } | |
1622 | copy_threadref (&info->threadid, &ref); | |
1623 | ||
23860348 | 1624 | /* Loop on tagged fields , try to bail if somthing goes wrong. */ |
c906108c | 1625 | |
23860348 MS |
1626 | /* Packets are terminated with nulls. */ |
1627 | while ((pkt < limit) && mask && *pkt) | |
c906108c SS |
1628 | { |
1629 | pkt = unpack_int (pkt, &tag); /* tag */ | |
23860348 MS |
1630 | pkt = unpack_byte (pkt, &length); /* length */ |
1631 | if (!(tag & mask)) /* Tags out of synch with mask. */ | |
c906108c | 1632 | { |
8a3fe4f8 | 1633 | warning (_("ERROR RMT: threadinfo tag mismatch.")); |
c906108c SS |
1634 | retval = 0; |
1635 | break; | |
1636 | } | |
1637 | if (tag == TAG_THREADID) | |
1638 | { | |
1639 | if (length != 16) | |
1640 | { | |
8a3fe4f8 | 1641 | warning (_("ERROR RMT: length of threadid is not 16.")); |
c906108c SS |
1642 | retval = 0; |
1643 | break; | |
1644 | } | |
1645 | pkt = unpack_threadid (pkt, &ref); | |
1646 | mask = mask & ~TAG_THREADID; | |
1647 | continue; | |
1648 | } | |
1649 | if (tag == TAG_EXISTS) | |
1650 | { | |
1651 | info->active = stub_unpack_int (pkt, length); | |
1652 | pkt += length; | |
1653 | mask = mask & ~(TAG_EXISTS); | |
1654 | if (length > 8) | |
1655 | { | |
8a3fe4f8 | 1656 | warning (_("ERROR RMT: 'exists' length too long.")); |
c906108c SS |
1657 | retval = 0; |
1658 | break; | |
1659 | } | |
1660 | continue; | |
1661 | } | |
1662 | if (tag == TAG_THREADNAME) | |
1663 | { | |
1664 | pkt = unpack_string (pkt, &info->shortname[0], length); | |
1665 | mask = mask & ~TAG_THREADNAME; | |
1666 | continue; | |
1667 | } | |
1668 | if (tag == TAG_DISPLAY) | |
1669 | { | |
1670 | pkt = unpack_string (pkt, &info->display[0], length); | |
1671 | mask = mask & ~TAG_DISPLAY; | |
1672 | continue; | |
1673 | } | |
1674 | if (tag == TAG_MOREDISPLAY) | |
1675 | { | |
1676 | pkt = unpack_string (pkt, &info->more_display[0], length); | |
1677 | mask = mask & ~TAG_MOREDISPLAY; | |
1678 | continue; | |
1679 | } | |
8a3fe4f8 | 1680 | warning (_("ERROR RMT: unknown thread info tag.")); |
23860348 | 1681 | break; /* Not a tag we know about. */ |
c906108c SS |
1682 | } |
1683 | return retval; | |
1684 | } | |
1685 | ||
1686 | static int | |
fba45db2 KB |
1687 | remote_get_threadinfo (threadref *threadid, int fieldset, /* TAG mask */ |
1688 | struct gdb_ext_thread_info *info) | |
c906108c | 1689 | { |
d01949b6 | 1690 | struct remote_state *rs = get_remote_state (); |
c906108c | 1691 | int result; |
c906108c | 1692 | |
2e9f7625 DJ |
1693 | pack_threadinfo_request (rs->buf, fieldset, threadid); |
1694 | putpkt (rs->buf); | |
6d820c5c | 1695 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 1696 | result = remote_unpack_thread_info_response (rs->buf + 2, |
23860348 | 1697 | threadid, info); |
c906108c SS |
1698 | return result; |
1699 | } | |
1700 | ||
c906108c SS |
1701 | /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */ |
1702 | ||
1703 | static char * | |
fba45db2 KB |
1704 | pack_threadlist_request (char *pkt, int startflag, int threadcount, |
1705 | threadref *nextthread) | |
c906108c SS |
1706 | { |
1707 | *pkt++ = 'q'; /* info query packet */ | |
1708 | *pkt++ = 'L'; /* Process LIST or threadLIST request */ | |
23860348 | 1709 | pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */ |
c906108c SS |
1710 | pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */ |
1711 | pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */ | |
1712 | *pkt = '\0'; | |
1713 | return pkt; | |
1714 | } | |
1715 | ||
1716 | /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */ | |
1717 | ||
1718 | static int | |
fba45db2 KB |
1719 | parse_threadlist_response (char *pkt, int result_limit, |
1720 | threadref *original_echo, threadref *resultlist, | |
1721 | int *doneflag) | |
c906108c | 1722 | { |
d01949b6 | 1723 | struct remote_state *rs = get_remote_state (); |
c906108c SS |
1724 | char *limit; |
1725 | int count, resultcount, done; | |
1726 | ||
1727 | resultcount = 0; | |
1728 | /* Assume the 'q' and 'M chars have been stripped. */ | |
6d820c5c | 1729 | limit = pkt + (rs->buf_size - BUF_THREAD_ID_SIZE); |
23860348 | 1730 | /* done parse past here */ |
c906108c SS |
1731 | pkt = unpack_byte (pkt, &count); /* count field */ |
1732 | pkt = unpack_nibble (pkt, &done); | |
1733 | /* The first threadid is the argument threadid. */ | |
1734 | pkt = unpack_threadid (pkt, original_echo); /* should match query packet */ | |
1735 | while ((count-- > 0) && (pkt < limit)) | |
1736 | { | |
1737 | pkt = unpack_threadid (pkt, resultlist++); | |
1738 | if (resultcount++ >= result_limit) | |
1739 | break; | |
1740 | } | |
1741 | if (doneflag) | |
1742 | *doneflag = done; | |
1743 | return resultcount; | |
1744 | } | |
1745 | ||
1746 | static int | |
fba45db2 KB |
1747 | remote_get_threadlist (int startflag, threadref *nextthread, int result_limit, |
1748 | int *done, int *result_count, threadref *threadlist) | |
c906108c | 1749 | { |
d01949b6 | 1750 | struct remote_state *rs = get_remote_state (); |
c906108c | 1751 | static threadref echo_nextthread; |
c906108c SS |
1752 | int result = 1; |
1753 | ||
23860348 | 1754 | /* Trancate result limit to be smaller than the packet size. */ |
ea9c271d DJ |
1755 | if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10) >= get_remote_packet_size ()) |
1756 | result_limit = (get_remote_packet_size () / BUF_THREAD_ID_SIZE) - 2; | |
c906108c | 1757 | |
6d820c5c DJ |
1758 | pack_threadlist_request (rs->buf, startflag, result_limit, nextthread); |
1759 | putpkt (rs->buf); | |
1760 | getpkt (&rs->buf, &rs->buf_size, 0); | |
c906108c | 1761 | |
d8f2712d VP |
1762 | if (*rs->buf == '\0') |
1763 | *result_count = 0; | |
1764 | else | |
1765 | *result_count = | |
1766 | parse_threadlist_response (rs->buf + 2, result_limit, &echo_nextthread, | |
1767 | threadlist, done); | |
c906108c SS |
1768 | |
1769 | if (!threadmatch (&echo_nextthread, nextthread)) | |
1770 | { | |
23860348 MS |
1771 | /* FIXME: This is a good reason to drop the packet. */ |
1772 | /* Possably, there is a duplicate response. */ | |
c906108c SS |
1773 | /* Possabilities : |
1774 | retransmit immediatly - race conditions | |
1775 | retransmit after timeout - yes | |
1776 | exit | |
1777 | wait for packet, then exit | |
1778 | */ | |
8a3fe4f8 | 1779 | warning (_("HMM: threadlist did not echo arg thread, dropping it.")); |
23860348 | 1780 | return 0; /* I choose simply exiting. */ |
c906108c SS |
1781 | } |
1782 | if (*result_count <= 0) | |
1783 | { | |
1784 | if (*done != 1) | |
1785 | { | |
8a3fe4f8 | 1786 | warning (_("RMT ERROR : failed to get remote thread list.")); |
c906108c SS |
1787 | result = 0; |
1788 | } | |
1789 | return result; /* break; */ | |
1790 | } | |
1791 | if (*result_count > result_limit) | |
1792 | { | |
1793 | *result_count = 0; | |
8a3fe4f8 | 1794 | warning (_("RMT ERROR: threadlist response longer than requested.")); |
c906108c SS |
1795 | return 0; |
1796 | } | |
1797 | return result; | |
1798 | } | |
1799 | ||
23860348 MS |
1800 | /* This is the interface between remote and threads, remotes upper |
1801 | interface. */ | |
c906108c SS |
1802 | |
1803 | /* remote_find_new_threads retrieves the thread list and for each | |
1804 | thread in the list, looks up the thread in GDB's internal list, | |
1805 | ading the thread if it does not already exist. This involves | |
1806 | getting partial thread lists from the remote target so, polling the | |
1807 | quit_flag is required. */ | |
1808 | ||
1809 | ||
23860348 | 1810 | /* About this many threadisds fit in a packet. */ |
c906108c SS |
1811 | |
1812 | #define MAXTHREADLISTRESULTS 32 | |
1813 | ||
1814 | static int | |
fba45db2 KB |
1815 | remote_threadlist_iterator (rmt_thread_action stepfunction, void *context, |
1816 | int looplimit) | |
c906108c SS |
1817 | { |
1818 | int done, i, result_count; | |
1819 | int startflag = 1; | |
1820 | int result = 1; | |
1821 | int loopcount = 0; | |
1822 | static threadref nextthread; | |
1823 | static threadref resultthreadlist[MAXTHREADLISTRESULTS]; | |
1824 | ||
1825 | done = 0; | |
1826 | while (!done) | |
1827 | { | |
1828 | if (loopcount++ > looplimit) | |
1829 | { | |
1830 | result = 0; | |
8a3fe4f8 | 1831 | warning (_("Remote fetch threadlist -infinite loop-.")); |
c906108c SS |
1832 | break; |
1833 | } | |
1834 | if (!remote_get_threadlist (startflag, &nextthread, MAXTHREADLISTRESULTS, | |
1835 | &done, &result_count, resultthreadlist)) | |
1836 | { | |
1837 | result = 0; | |
1838 | break; | |
1839 | } | |
23860348 | 1840 | /* Clear for later iterations. */ |
c906108c SS |
1841 | startflag = 0; |
1842 | /* Setup to resume next batch of thread references, set nextthread. */ | |
1843 | if (result_count >= 1) | |
1844 | copy_threadref (&nextthread, &resultthreadlist[result_count - 1]); | |
1845 | i = 0; | |
1846 | while (result_count--) | |
1847 | if (!(result = (*stepfunction) (&resultthreadlist[i++], context))) | |
1848 | break; | |
1849 | } | |
1850 | return result; | |
1851 | } | |
1852 | ||
1853 | static int | |
fba45db2 | 1854 | remote_newthread_step (threadref *ref, void *context) |
c906108c | 1855 | { |
39f77062 | 1856 | ptid_t ptid; |
c906108c | 1857 | |
39f77062 KB |
1858 | ptid = pid_to_ptid (threadref_to_int (ref)); |
1859 | ||
1860 | if (!in_thread_list (ptid)) | |
1861 | add_thread (ptid); | |
c906108c SS |
1862 | return 1; /* continue iterator */ |
1863 | } | |
1864 | ||
1865 | #define CRAZY_MAX_THREADS 1000 | |
1866 | ||
39f77062 KB |
1867 | static ptid_t |
1868 | remote_current_thread (ptid_t oldpid) | |
c906108c | 1869 | { |
d01949b6 | 1870 | struct remote_state *rs = get_remote_state (); |
c906108c SS |
1871 | |
1872 | putpkt ("qC"); | |
6d820c5c | 1873 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 1874 | if (rs->buf[0] == 'Q' && rs->buf[1] == 'C') |
c273b20f JB |
1875 | /* Use strtoul here, so we'll correctly parse values whose highest |
1876 | bit is set. The protocol carries them as a simple series of | |
1877 | hex digits; in the absence of a sign, strtol will see such | |
1878 | values as positive numbers out of range for signed 'long', and | |
1879 | return LONG_MAX to indicate an overflow. */ | |
2e9f7625 | 1880 | return pid_to_ptid (strtoul (&rs->buf[2], NULL, 16)); |
c906108c SS |
1881 | else |
1882 | return oldpid; | |
1883 | } | |
1884 | ||
802188a7 RM |
1885 | /* Find new threads for info threads command. |
1886 | * Original version, using John Metzler's thread protocol. | |
9d1f7ab2 | 1887 | */ |
cce74817 JM |
1888 | |
1889 | static void | |
fba45db2 | 1890 | remote_find_new_threads (void) |
c906108c | 1891 | { |
c5aa993b JM |
1892 | remote_threadlist_iterator (remote_newthread_step, 0, |
1893 | CRAZY_MAX_THREADS); | |
39f77062 KB |
1894 | if (PIDGET (inferior_ptid) == MAGIC_NULL_PID) /* ack ack ack */ |
1895 | inferior_ptid = remote_current_thread (inferior_ptid); | |
c906108c SS |
1896 | } |
1897 | ||
9d1f7ab2 MS |
1898 | /* |
1899 | * Find all threads for info threads command. | |
1900 | * Uses new thread protocol contributed by Cisco. | |
1901 | * Falls back and attempts to use the older method (above) | |
1902 | * if the target doesn't respond to the new method. | |
1903 | */ | |
1904 | ||
0f71a2f6 JM |
1905 | static void |
1906 | remote_threads_info (void) | |
1907 | { | |
d01949b6 | 1908 | struct remote_state *rs = get_remote_state (); |
085dd6e6 | 1909 | char *bufp; |
0f71a2f6 JM |
1910 | int tid; |
1911 | ||
1912 | if (remote_desc == 0) /* paranoia */ | |
8a3fe4f8 | 1913 | error (_("Command can only be used when connected to the remote target.")); |
0f71a2f6 | 1914 | |
9d1f7ab2 MS |
1915 | if (use_threadinfo_query) |
1916 | { | |
1917 | putpkt ("qfThreadInfo"); | |
6d820c5c | 1918 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 1919 | bufp = rs->buf; |
9d1f7ab2 | 1920 | if (bufp[0] != '\0') /* q packet recognized */ |
802188a7 | 1921 | { |
9d1f7ab2 MS |
1922 | while (*bufp++ == 'm') /* reply contains one or more TID */ |
1923 | { | |
1924 | do | |
1925 | { | |
c273b20f JB |
1926 | /* Use strtoul here, so we'll correctly parse values |
1927 | whose highest bit is set. The protocol carries | |
1928 | them as a simple series of hex digits; in the | |
1929 | absence of a sign, strtol will see such values as | |
1930 | positive numbers out of range for signed 'long', | |
1931 | and return LONG_MAX to indicate an overflow. */ | |
1932 | tid = strtoul (bufp, &bufp, 16); | |
39f77062 KB |
1933 | if (tid != 0 && !in_thread_list (pid_to_ptid (tid))) |
1934 | add_thread (pid_to_ptid (tid)); | |
9d1f7ab2 MS |
1935 | } |
1936 | while (*bufp++ == ','); /* comma-separated list */ | |
1937 | putpkt ("qsThreadInfo"); | |
6d820c5c | 1938 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 1939 | bufp = rs->buf; |
9d1f7ab2 MS |
1940 | } |
1941 | return; /* done */ | |
1942 | } | |
1943 | } | |
1944 | ||
23860348 | 1945 | /* Else fall back to old method based on jmetzler protocol. */ |
9d1f7ab2 MS |
1946 | use_threadinfo_query = 0; |
1947 | remote_find_new_threads (); | |
1948 | return; | |
1949 | } | |
1950 | ||
802188a7 | 1951 | /* |
9d1f7ab2 MS |
1952 | * Collect a descriptive string about the given thread. |
1953 | * The target may say anything it wants to about the thread | |
1954 | * (typically info about its blocked / runnable state, name, etc.). | |
1955 | * This string will appear in the info threads display. | |
802188a7 | 1956 | * |
9d1f7ab2 MS |
1957 | * Optional: targets are not required to implement this function. |
1958 | */ | |
1959 | ||
1960 | static char * | |
1961 | remote_threads_extra_info (struct thread_info *tp) | |
1962 | { | |
d01949b6 | 1963 | struct remote_state *rs = get_remote_state (); |
9d1f7ab2 MS |
1964 | int result; |
1965 | int set; | |
1966 | threadref id; | |
1967 | struct gdb_ext_thread_info threadinfo; | |
23860348 | 1968 | static char display_buf[100]; /* arbitrary... */ |
9d1f7ab2 MS |
1969 | int n = 0; /* position in display_buf */ |
1970 | ||
1971 | if (remote_desc == 0) /* paranoia */ | |
8e65ff28 | 1972 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 1973 | _("remote_threads_extra_info")); |
9d1f7ab2 MS |
1974 | |
1975 | if (use_threadextra_query) | |
1976 | { | |
2e9f7625 | 1977 | xsnprintf (rs->buf, get_remote_packet_size (), "qThreadExtraInfo,%x", |
ecbc58df | 1978 | PIDGET (tp->ptid)); |
2e9f7625 | 1979 | putpkt (rs->buf); |
6d820c5c | 1980 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 1981 | if (rs->buf[0] != 0) |
9d1f7ab2 | 1982 | { |
2e9f7625 DJ |
1983 | n = min (strlen (rs->buf) / 2, sizeof (display_buf)); |
1984 | result = hex2bin (rs->buf, (gdb_byte *) display_buf, n); | |
30559e10 | 1985 | display_buf [result] = '\0'; |
9d1f7ab2 MS |
1986 | return display_buf; |
1987 | } | |
0f71a2f6 | 1988 | } |
9d1f7ab2 MS |
1989 | |
1990 | /* If the above query fails, fall back to the old method. */ | |
1991 | use_threadextra_query = 0; | |
1992 | set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME | |
1993 | | TAG_MOREDISPLAY | TAG_DISPLAY; | |
39f77062 | 1994 | int_to_threadref (&id, PIDGET (tp->ptid)); |
9d1f7ab2 MS |
1995 | if (remote_get_threadinfo (&id, set, &threadinfo)) |
1996 | if (threadinfo.active) | |
0f71a2f6 | 1997 | { |
9d1f7ab2 | 1998 | if (*threadinfo.shortname) |
2bc416ba | 1999 | n += xsnprintf (&display_buf[0], sizeof (display_buf) - n, |
ecbc58df | 2000 | " Name: %s,", threadinfo.shortname); |
9d1f7ab2 | 2001 | if (*threadinfo.display) |
2bc416ba | 2002 | n += xsnprintf (&display_buf[n], sizeof (display_buf) - n, |
ecbc58df | 2003 | " State: %s,", threadinfo.display); |
9d1f7ab2 | 2004 | if (*threadinfo.more_display) |
2bc416ba | 2005 | n += xsnprintf (&display_buf[n], sizeof (display_buf) - n, |
ecbc58df | 2006 | " Priority: %s", threadinfo.more_display); |
9d1f7ab2 MS |
2007 | |
2008 | if (n > 0) | |
c5aa993b | 2009 | { |
23860348 | 2010 | /* For purely cosmetic reasons, clear up trailing commas. */ |
9d1f7ab2 MS |
2011 | if (',' == display_buf[n-1]) |
2012 | display_buf[n-1] = ' '; | |
2013 | return display_buf; | |
c5aa993b | 2014 | } |
0f71a2f6 | 2015 | } |
9d1f7ab2 | 2016 | return NULL; |
0f71a2f6 | 2017 | } |
c906108c | 2018 | \f |
c5aa993b | 2019 | |
24b06219 | 2020 | /* Restart the remote side; this is an extended protocol operation. */ |
c906108c SS |
2021 | |
2022 | static void | |
fba45db2 | 2023 | extended_remote_restart (void) |
c906108c | 2024 | { |
d01949b6 | 2025 | struct remote_state *rs = get_remote_state (); |
c906108c SS |
2026 | |
2027 | /* Send the restart command; for reasons I don't understand the | |
2028 | remote side really expects a number after the "R". */ | |
ea9c271d | 2029 | xsnprintf (rs->buf, get_remote_packet_size (), "R%x", 0); |
6d820c5c | 2030 | putpkt (rs->buf); |
c906108c | 2031 | |
ad9a8f3f | 2032 | remote_fileio_reset (); |
c906108c SS |
2033 | } |
2034 | \f | |
2035 | /* Clean up connection to a remote debugger. */ | |
2036 | ||
c906108c | 2037 | static void |
fba45db2 | 2038 | remote_close (int quitting) |
c906108c SS |
2039 | { |
2040 | if (remote_desc) | |
2cd58942 | 2041 | serial_close (remote_desc); |
c906108c SS |
2042 | remote_desc = NULL; |
2043 | } | |
2044 | ||
23860348 | 2045 | /* Query the remote side for the text, data and bss offsets. */ |
c906108c SS |
2046 | |
2047 | static void | |
fba45db2 | 2048 | get_offsets (void) |
c906108c | 2049 | { |
d01949b6 | 2050 | struct remote_state *rs = get_remote_state (); |
2e9f7625 | 2051 | char *buf; |
085dd6e6 | 2052 | char *ptr; |
31d99776 DJ |
2053 | int lose, num_segments = 0, do_sections, do_segments; |
2054 | CORE_ADDR text_addr, data_addr, bss_addr, segments[2]; | |
c906108c | 2055 | struct section_offsets *offs; |
31d99776 DJ |
2056 | struct symfile_segment_data *data; |
2057 | ||
2058 | if (symfile_objfile == NULL) | |
2059 | return; | |
c906108c SS |
2060 | |
2061 | putpkt ("qOffsets"); | |
6d820c5c | 2062 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 2063 | buf = rs->buf; |
c906108c SS |
2064 | |
2065 | if (buf[0] == '\000') | |
2066 | return; /* Return silently. Stub doesn't support | |
23860348 | 2067 | this command. */ |
c906108c SS |
2068 | if (buf[0] == 'E') |
2069 | { | |
8a3fe4f8 | 2070 | warning (_("Remote failure reply: %s"), buf); |
c906108c SS |
2071 | return; |
2072 | } | |
2073 | ||
2074 | /* Pick up each field in turn. This used to be done with scanf, but | |
2075 | scanf will make trouble if CORE_ADDR size doesn't match | |
2076 | conversion directives correctly. The following code will work | |
2077 | with any size of CORE_ADDR. */ | |
2078 | text_addr = data_addr = bss_addr = 0; | |
2079 | ptr = buf; | |
2080 | lose = 0; | |
2081 | ||
2082 | if (strncmp (ptr, "Text=", 5) == 0) | |
2083 | { | |
2084 | ptr += 5; | |
2085 | /* Don't use strtol, could lose on big values. */ | |
2086 | while (*ptr && *ptr != ';') | |
2087 | text_addr = (text_addr << 4) + fromhex (*ptr++); | |
c906108c | 2088 | |
31d99776 DJ |
2089 | if (strncmp (ptr, ";Data=", 6) == 0) |
2090 | { | |
2091 | ptr += 6; | |
2092 | while (*ptr && *ptr != ';') | |
2093 | data_addr = (data_addr << 4) + fromhex (*ptr++); | |
2094 | } | |
2095 | else | |
2096 | lose = 1; | |
2097 | ||
2098 | if (!lose && strncmp (ptr, ";Bss=", 5) == 0) | |
2099 | { | |
2100 | ptr += 5; | |
2101 | while (*ptr && *ptr != ';') | |
2102 | bss_addr = (bss_addr << 4) + fromhex (*ptr++); | |
c906108c | 2103 | |
31d99776 DJ |
2104 | if (bss_addr != data_addr) |
2105 | warning (_("Target reported unsupported offsets: %s"), buf); | |
2106 | } | |
2107 | else | |
2108 | lose = 1; | |
2109 | } | |
2110 | else if (strncmp (ptr, "TextSeg=", 8) == 0) | |
c906108c | 2111 | { |
31d99776 DJ |
2112 | ptr += 8; |
2113 | /* Don't use strtol, could lose on big values. */ | |
c906108c | 2114 | while (*ptr && *ptr != ';') |
31d99776 DJ |
2115 | text_addr = (text_addr << 4) + fromhex (*ptr++); |
2116 | num_segments = 1; | |
2117 | ||
2118 | if (strncmp (ptr, ";DataSeg=", 9) == 0) | |
2119 | { | |
2120 | ptr += 9; | |
2121 | while (*ptr && *ptr != ';') | |
2122 | data_addr = (data_addr << 4) + fromhex (*ptr++); | |
2123 | num_segments++; | |
2124 | } | |
c906108c SS |
2125 | } |
2126 | else | |
2127 | lose = 1; | |
2128 | ||
2129 | if (lose) | |
8a3fe4f8 | 2130 | error (_("Malformed response to offset query, %s"), buf); |
31d99776 DJ |
2131 | else if (*ptr != '\0') |
2132 | warning (_("Target reported unsupported offsets: %s"), buf); | |
c906108c | 2133 | |
802188a7 | 2134 | offs = ((struct section_offsets *) |
a39a16c4 | 2135 | alloca (SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections))); |
802188a7 | 2136 | memcpy (offs, symfile_objfile->section_offsets, |
a39a16c4 | 2137 | SIZEOF_N_SECTION_OFFSETS (symfile_objfile->num_sections)); |
c906108c | 2138 | |
31d99776 DJ |
2139 | data = get_symfile_segment_data (symfile_objfile->obfd); |
2140 | do_segments = (data != NULL); | |
2141 | do_sections = num_segments == 0; | |
c906108c | 2142 | |
28c32713 | 2143 | if (num_segments > 0) |
31d99776 | 2144 | { |
31d99776 DJ |
2145 | segments[0] = text_addr; |
2146 | segments[1] = data_addr; | |
2147 | } | |
28c32713 JB |
2148 | /* If we have two segments, we can still try to relocate everything |
2149 | by assuming that the .text and .data offsets apply to the whole | |
2150 | text and data segments. Convert the offsets given in the packet | |
2151 | to base addresses for symfile_map_offsets_to_segments. */ | |
2152 | else if (data && data->num_segments == 2) | |
2153 | { | |
2154 | segments[0] = data->segment_bases[0] + text_addr; | |
2155 | segments[1] = data->segment_bases[1] + data_addr; | |
2156 | num_segments = 2; | |
2157 | } | |
8d385431 DJ |
2158 | /* If the object file has only one segment, assume that it is text |
2159 | rather than data; main programs with no writable data are rare, | |
2160 | but programs with no code are useless. Of course the code might | |
2161 | have ended up in the data segment... to detect that we would need | |
2162 | the permissions here. */ | |
2163 | else if (data && data->num_segments == 1) | |
2164 | { | |
2165 | segments[0] = data->segment_bases[0] + text_addr; | |
2166 | num_segments = 1; | |
2167 | } | |
28c32713 JB |
2168 | /* There's no way to relocate by segment. */ |
2169 | else | |
2170 | do_segments = 0; | |
31d99776 DJ |
2171 | |
2172 | if (do_segments) | |
2173 | { | |
2174 | int ret = symfile_map_offsets_to_segments (symfile_objfile->obfd, data, | |
2175 | offs, num_segments, segments); | |
2176 | ||
2177 | if (ret == 0 && !do_sections) | |
2178 | error (_("Can not handle qOffsets TextSeg response with this symbol file")); | |
2179 | ||
2180 | if (ret > 0) | |
2181 | do_sections = 0; | |
2182 | } | |
c906108c | 2183 | |
9ef895d6 DJ |
2184 | if (data) |
2185 | free_symfile_segment_data (data); | |
31d99776 DJ |
2186 | |
2187 | if (do_sections) | |
2188 | { | |
2189 | offs->offsets[SECT_OFF_TEXT (symfile_objfile)] = text_addr; | |
2190 | ||
2191 | /* This is a temporary kludge to force data and bss to use the same offsets | |
2192 | because that's what nlmconv does now. The real solution requires changes | |
2193 | to the stub and remote.c that I don't have time to do right now. */ | |
2194 | ||
2195 | offs->offsets[SECT_OFF_DATA (symfile_objfile)] = data_addr; | |
2196 | offs->offsets[SECT_OFF_BSS (symfile_objfile)] = data_addr; | |
2197 | } | |
c906108c SS |
2198 | |
2199 | objfile_relocate (symfile_objfile, offs); | |
2200 | } | |
2201 | ||
8621d6a9 | 2202 | /* Stub for catch_exception. */ |
0f71a2f6 | 2203 | |
2d717e4f DJ |
2204 | struct start_remote_args |
2205 | { | |
2206 | int from_tty; | |
2207 | ||
2208 | /* The current target. */ | |
2209 | struct target_ops *target; | |
2210 | ||
2211 | /* Non-zero if this is an extended-remote target. */ | |
2212 | int extended_p; | |
2213 | }; | |
2214 | ||
9cbc821d | 2215 | static void |
2d717e4f | 2216 | remote_start_remote (struct ui_out *uiout, void *opaque) |
c906108c | 2217 | { |
2d717e4f DJ |
2218 | struct remote_state *rs = get_remote_state (); |
2219 | struct start_remote_args *args = opaque; | |
2220 | char *wait_status = NULL; | |
8621d6a9 | 2221 | |
23860348 | 2222 | immediate_quit++; /* Allow user to interrupt it. */ |
c906108c SS |
2223 | |
2224 | /* Ack any packet which the remote side has already sent. */ | |
2cd58942 | 2225 | serial_write (remote_desc, "+", 1); |
c906108c | 2226 | |
2d717e4f DJ |
2227 | /* Check whether the target is running now. */ |
2228 | putpkt ("?"); | |
2229 | getpkt (&rs->buf, &rs->buf_size, 0); | |
2230 | ||
2231 | if (rs->buf[0] == 'W' || rs->buf[0] == 'X') | |
2232 | { | |
2233 | if (args->extended_p) | |
2234 | { | |
2235 | /* We're connected, but not running. Drop out before we | |
2236 | call start_remote. */ | |
2237 | target_mark_exited (args->target); | |
2238 | return; | |
2239 | } | |
2240 | else | |
2241 | error (_("The target is not running (try extended-remote?)")); | |
2242 | } | |
2243 | else | |
2244 | { | |
2245 | if (args->extended_p) | |
2246 | target_mark_running (args->target); | |
2247 | ||
2248 | /* Save the reply for later. */ | |
2249 | wait_status = alloca (strlen (rs->buf) + 1); | |
2250 | strcpy (wait_status, rs->buf); | |
2251 | } | |
2252 | ||
c906108c SS |
2253 | /* Let the stub know that we want it to return the thread. */ |
2254 | set_thread (-1, 0); | |
2255 | ||
2d717e4f DJ |
2256 | /* Without this, some commands which require an active target |
2257 | (such as kill) won't work. This variable serves (at least) | |
2258 | double duty as both the pid of the target process (if it has | |
2259 | such), and as a flag indicating that a target is active. | |
2260 | These functions should be split out into seperate variables, | |
2261 | especially since GDB will someday have a notion of debugging | |
2262 | several processes. */ | |
2263 | inferior_ptid = pid_to_ptid (MAGIC_NULL_PID); | |
2264 | ||
2265 | /* Now, if we have thread information, update inferior_ptid. */ | |
39f77062 | 2266 | inferior_ptid = remote_current_thread (inferior_ptid); |
c906108c | 2267 | |
23860348 | 2268 | get_offsets (); /* Get text, data & bss offsets. */ |
c906108c | 2269 | |
2d717e4f DJ |
2270 | /* Use the previously fetched status. */ |
2271 | gdb_assert (wait_status != NULL); | |
2272 | strcpy (rs->buf, wait_status); | |
2273 | rs->cached_wait_status = 1; | |
c906108c | 2274 | |
2d717e4f DJ |
2275 | immediate_quit--; |
2276 | start_remote (args->from_tty); /* Initialize gdb process mechanisms. */ | |
c906108c SS |
2277 | } |
2278 | ||
2279 | /* Open a connection to a remote debugger. | |
2280 | NAME is the filename used for communication. */ | |
2281 | ||
2282 | static void | |
fba45db2 | 2283 | remote_open (char *name, int from_tty) |
c906108c | 2284 | { |
75c99385 | 2285 | remote_open_1 (name, from_tty, &remote_ops, 0); |
43ff13b4 JM |
2286 | } |
2287 | ||
c906108c SS |
2288 | /* Open a connection to a remote debugger using the extended |
2289 | remote gdb protocol. NAME is the filename used for communication. */ | |
2290 | ||
2291 | static void | |
fba45db2 | 2292 | extended_remote_open (char *name, int from_tty) |
c906108c | 2293 | { |
75c99385 | 2294 | remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */); |
43ff13b4 JM |
2295 | } |
2296 | ||
c906108c SS |
2297 | /* Generic code for opening a connection to a remote target. */ |
2298 | ||
d471ea57 AC |
2299 | static void |
2300 | init_all_packet_configs (void) | |
2301 | { | |
2302 | int i; | |
444abaca DJ |
2303 | for (i = 0; i < PACKET_MAX; i++) |
2304 | update_packet_config (&remote_protocol_packets[i]); | |
d471ea57 AC |
2305 | } |
2306 | ||
23860348 | 2307 | /* Symbol look-up. */ |
dc8acb97 MS |
2308 | |
2309 | static void | |
2310 | remote_check_symbols (struct objfile *objfile) | |
2311 | { | |
d01949b6 | 2312 | struct remote_state *rs = get_remote_state (); |
dc8acb97 MS |
2313 | char *msg, *reply, *tmp; |
2314 | struct minimal_symbol *sym; | |
2315 | int end; | |
2316 | ||
444abaca | 2317 | if (remote_protocol_packets[PACKET_qSymbol].support == PACKET_DISABLE) |
dc8acb97 MS |
2318 | return; |
2319 | ||
6d820c5c DJ |
2320 | /* Allocate a message buffer. We can't reuse the input buffer in RS, |
2321 | because we need both at the same time. */ | |
ea9c271d | 2322 | msg = alloca (get_remote_packet_size ()); |
6d820c5c | 2323 | |
23860348 | 2324 | /* Invite target to request symbol lookups. */ |
dc8acb97 MS |
2325 | |
2326 | putpkt ("qSymbol::"); | |
6d820c5c DJ |
2327 | getpkt (&rs->buf, &rs->buf_size, 0); |
2328 | packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSymbol]); | |
2e9f7625 | 2329 | reply = rs->buf; |
dc8acb97 MS |
2330 | |
2331 | while (strncmp (reply, "qSymbol:", 8) == 0) | |
2332 | { | |
2333 | tmp = &reply[8]; | |
cfd77fa1 | 2334 | end = hex2bin (tmp, (gdb_byte *) msg, strlen (tmp) / 2); |
dc8acb97 MS |
2335 | msg[end] = '\0'; |
2336 | sym = lookup_minimal_symbol (msg, NULL, NULL); | |
2337 | if (sym == NULL) | |
ea9c271d | 2338 | xsnprintf (msg, get_remote_packet_size (), "qSymbol::%s", &reply[8]); |
dc8acb97 | 2339 | else |
2bbe3cc1 DJ |
2340 | { |
2341 | CORE_ADDR sym_addr = SYMBOL_VALUE_ADDRESS (sym); | |
2342 | ||
2343 | /* If this is a function address, return the start of code | |
2344 | instead of any data function descriptor. */ | |
2345 | sym_addr = gdbarch_convert_from_func_ptr_addr (current_gdbarch, | |
2346 | sym_addr, | |
2347 | ¤t_target); | |
2348 | ||
2349 | xsnprintf (msg, get_remote_packet_size (), "qSymbol:%s:%s", | |
2350 | paddr_nz (sym_addr), &reply[8]); | |
2351 | } | |
2352 | ||
dc8acb97 | 2353 | putpkt (msg); |
6d820c5c | 2354 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 2355 | reply = rs->buf; |
dc8acb97 MS |
2356 | } |
2357 | } | |
2358 | ||
9db8d71f DJ |
2359 | static struct serial * |
2360 | remote_serial_open (char *name) | |
2361 | { | |
2362 | static int udp_warning = 0; | |
2363 | ||
2364 | /* FIXME: Parsing NAME here is a hack. But we want to warn here instead | |
2365 | of in ser-tcp.c, because it is the remote protocol assuming that the | |
2366 | serial connection is reliable and not the serial connection promising | |
2367 | to be. */ | |
2368 | if (!udp_warning && strncmp (name, "udp:", 4) == 0) | |
2369 | { | |
8a3fe4f8 AC |
2370 | warning (_("\ |
2371 | The remote protocol may be unreliable over UDP.\n\ | |
2372 | Some events may be lost, rendering further debugging impossible.")); | |
9db8d71f DJ |
2373 | udp_warning = 1; |
2374 | } | |
2375 | ||
2376 | return serial_open (name); | |
2377 | } | |
2378 | ||
be2a5f71 DJ |
2379 | /* This type describes each known response to the qSupported |
2380 | packet. */ | |
2381 | struct protocol_feature | |
2382 | { | |
2383 | /* The name of this protocol feature. */ | |
2384 | const char *name; | |
2385 | ||
2386 | /* The default for this protocol feature. */ | |
2387 | enum packet_support default_support; | |
2388 | ||
2389 | /* The function to call when this feature is reported, or after | |
2390 | qSupported processing if the feature is not supported. | |
2391 | The first argument points to this structure. The second | |
2392 | argument indicates whether the packet requested support be | |
2393 | enabled, disabled, or probed (or the default, if this function | |
2394 | is being called at the end of processing and this feature was | |
2395 | not reported). The third argument may be NULL; if not NULL, it | |
2396 | is a NUL-terminated string taken from the packet following | |
2397 | this feature's name and an equals sign. */ | |
2398 | void (*func) (const struct protocol_feature *, enum packet_support, | |
2399 | const char *); | |
2400 | ||
2401 | /* The corresponding packet for this feature. Only used if | |
2402 | FUNC is remote_supported_packet. */ | |
2403 | int packet; | |
2404 | }; | |
2405 | ||
be2a5f71 DJ |
2406 | static void |
2407 | remote_supported_packet (const struct protocol_feature *feature, | |
2408 | enum packet_support support, | |
2409 | const char *argument) | |
2410 | { | |
2411 | if (argument) | |
2412 | { | |
2413 | warning (_("Remote qSupported response supplied an unexpected value for" | |
2414 | " \"%s\"."), feature->name); | |
2415 | return; | |
2416 | } | |
2417 | ||
2418 | if (remote_protocol_packets[feature->packet].support | |
2419 | == PACKET_SUPPORT_UNKNOWN) | |
2420 | remote_protocol_packets[feature->packet].support = support; | |
2421 | } | |
be2a5f71 DJ |
2422 | |
2423 | static void | |
2424 | remote_packet_size (const struct protocol_feature *feature, | |
2425 | enum packet_support support, const char *value) | |
2426 | { | |
2427 | struct remote_state *rs = get_remote_state (); | |
2428 | ||
2429 | int packet_size; | |
2430 | char *value_end; | |
2431 | ||
2432 | if (support != PACKET_ENABLE) | |
2433 | return; | |
2434 | ||
2435 | if (value == NULL || *value == '\0') | |
2436 | { | |
2437 | warning (_("Remote target reported \"%s\" without a size."), | |
2438 | feature->name); | |
2439 | return; | |
2440 | } | |
2441 | ||
2442 | errno = 0; | |
2443 | packet_size = strtol (value, &value_end, 16); | |
2444 | if (errno != 0 || *value_end != '\0' || packet_size < 0) | |
2445 | { | |
2446 | warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."), | |
2447 | feature->name, value); | |
2448 | return; | |
2449 | } | |
2450 | ||
2451 | if (packet_size > MAX_REMOTE_PACKET_SIZE) | |
2452 | { | |
2453 | warning (_("limiting remote suggested packet size (%d bytes) to %d"), | |
2454 | packet_size, MAX_REMOTE_PACKET_SIZE); | |
2455 | packet_size = MAX_REMOTE_PACKET_SIZE; | |
2456 | } | |
2457 | ||
2458 | /* Record the new maximum packet size. */ | |
2459 | rs->explicit_packet_size = packet_size; | |
2460 | } | |
2461 | ||
2462 | static struct protocol_feature remote_protocol_features[] = { | |
0876f84a | 2463 | { "PacketSize", PACKET_DISABLE, remote_packet_size, -1 }, |
40e57cf2 | 2464 | { "qXfer:auxv:read", PACKET_DISABLE, remote_supported_packet, |
fd79ecee | 2465 | PACKET_qXfer_auxv }, |
23181151 DJ |
2466 | { "qXfer:features:read", PACKET_DISABLE, remote_supported_packet, |
2467 | PACKET_qXfer_features }, | |
cfa9d6d9 DJ |
2468 | { "qXfer:libraries:read", PACKET_DISABLE, remote_supported_packet, |
2469 | PACKET_qXfer_libraries }, | |
fd79ecee | 2470 | { "qXfer:memory-map:read", PACKET_DISABLE, remote_supported_packet, |
89be2091 | 2471 | PACKET_qXfer_memory_map }, |
4de6483e UW |
2472 | { "qXfer:spu:read", PACKET_DISABLE, remote_supported_packet, |
2473 | PACKET_qXfer_spu_read }, | |
2474 | { "qXfer:spu:write", PACKET_DISABLE, remote_supported_packet, | |
2475 | PACKET_qXfer_spu_write }, | |
89be2091 DJ |
2476 | { "QPassSignals", PACKET_DISABLE, remote_supported_packet, |
2477 | PACKET_QPassSignals }, | |
be2a5f71 DJ |
2478 | }; |
2479 | ||
2480 | static void | |
2481 | remote_query_supported (void) | |
2482 | { | |
2483 | struct remote_state *rs = get_remote_state (); | |
2484 | char *next; | |
2485 | int i; | |
2486 | unsigned char seen [ARRAY_SIZE (remote_protocol_features)]; | |
2487 | ||
2488 | /* The packet support flags are handled differently for this packet | |
2489 | than for most others. We treat an error, a disabled packet, and | |
2490 | an empty response identically: any features which must be reported | |
2491 | to be used will be automatically disabled. An empty buffer | |
2492 | accomplishes this, since that is also the representation for a list | |
2493 | containing no features. */ | |
2494 | ||
2495 | rs->buf[0] = 0; | |
2496 | if (remote_protocol_packets[PACKET_qSupported].support != PACKET_DISABLE) | |
2497 | { | |
2498 | putpkt ("qSupported"); | |
2499 | getpkt (&rs->buf, &rs->buf_size, 0); | |
2500 | ||
2501 | /* If an error occured, warn, but do not return - just reset the | |
2502 | buffer to empty and go on to disable features. */ | |
2503 | if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSupported]) | |
2504 | == PACKET_ERROR) | |
2505 | { | |
2506 | warning (_("Remote failure reply: %s"), rs->buf); | |
2507 | rs->buf[0] = 0; | |
2508 | } | |
2509 | } | |
2510 | ||
2511 | memset (seen, 0, sizeof (seen)); | |
2512 | ||
2513 | next = rs->buf; | |
2514 | while (*next) | |
2515 | { | |
2516 | enum packet_support is_supported; | |
2517 | char *p, *end, *name_end, *value; | |
2518 | ||
2519 | /* First separate out this item from the rest of the packet. If | |
2520 | there's another item after this, we overwrite the separator | |
2521 | (terminated strings are much easier to work with). */ | |
2522 | p = next; | |
2523 | end = strchr (p, ';'); | |
2524 | if (end == NULL) | |
2525 | { | |
2526 | end = p + strlen (p); | |
2527 | next = end; | |
2528 | } | |
2529 | else | |
2530 | { | |
89be2091 DJ |
2531 | *end = '\0'; |
2532 | next = end + 1; | |
2533 | ||
be2a5f71 DJ |
2534 | if (end == p) |
2535 | { | |
2536 | warning (_("empty item in \"qSupported\" response")); | |
2537 | continue; | |
2538 | } | |
be2a5f71 DJ |
2539 | } |
2540 | ||
2541 | name_end = strchr (p, '='); | |
2542 | if (name_end) | |
2543 | { | |
2544 | /* This is a name=value entry. */ | |
2545 | is_supported = PACKET_ENABLE; | |
2546 | value = name_end + 1; | |
2547 | *name_end = '\0'; | |
2548 | } | |
2549 | else | |
2550 | { | |
2551 | value = NULL; | |
2552 | switch (end[-1]) | |
2553 | { | |
2554 | case '+': | |
2555 | is_supported = PACKET_ENABLE; | |
2556 | break; | |
2557 | ||
2558 | case '-': | |
2559 | is_supported = PACKET_DISABLE; | |
2560 | break; | |
2561 | ||
2562 | case '?': | |
2563 | is_supported = PACKET_SUPPORT_UNKNOWN; | |
2564 | break; | |
2565 | ||
2566 | default: | |
2567 | warning (_("unrecognized item \"%s\" in \"qSupported\" response"), p); | |
2568 | continue; | |
2569 | } | |
2570 | end[-1] = '\0'; | |
2571 | } | |
2572 | ||
2573 | for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++) | |
2574 | if (strcmp (remote_protocol_features[i].name, p) == 0) | |
2575 | { | |
2576 | const struct protocol_feature *feature; | |
2577 | ||
2578 | seen[i] = 1; | |
2579 | feature = &remote_protocol_features[i]; | |
2580 | feature->func (feature, is_supported, value); | |
2581 | break; | |
2582 | } | |
2583 | } | |
2584 | ||
2585 | /* If we increased the packet size, make sure to increase the global | |
2586 | buffer size also. We delay this until after parsing the entire | |
2587 | qSupported packet, because this is the same buffer we were | |
2588 | parsing. */ | |
2589 | if (rs->buf_size < rs->explicit_packet_size) | |
2590 | { | |
2591 | rs->buf_size = rs->explicit_packet_size; | |
2592 | rs->buf = xrealloc (rs->buf, rs->buf_size); | |
2593 | } | |
2594 | ||
2595 | /* Handle the defaults for unmentioned features. */ | |
2596 | for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++) | |
2597 | if (!seen[i]) | |
2598 | { | |
2599 | const struct protocol_feature *feature; | |
2600 | ||
2601 | feature = &remote_protocol_features[i]; | |
2602 | feature->func (feature, feature->default_support, NULL); | |
2603 | } | |
2604 | } | |
2605 | ||
2606 | ||
c906108c | 2607 | static void |
75c99385 | 2608 | remote_open_1 (char *name, int from_tty, struct target_ops *target, int extended_p) |
c906108c | 2609 | { |
d01949b6 | 2610 | struct remote_state *rs = get_remote_state (); |
c906108c | 2611 | if (name == 0) |
8a3fe4f8 | 2612 | error (_("To open a remote debug connection, you need to specify what\n" |
22e04375 | 2613 | "serial device is attached to the remote system\n" |
8a3fe4f8 | 2614 | "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).")); |
c906108c | 2615 | |
23860348 | 2616 | /* See FIXME above. */ |
75c99385 | 2617 | if (!remote_async_permitted) |
92d1e331 | 2618 | wait_forever_enabled_p = 1; |
6426a772 | 2619 | |
2d717e4f DJ |
2620 | /* If we're connected to a running target, target_preopen will kill it. |
2621 | But if we're connected to a target system with no running process, | |
2622 | then we will still be connected when it returns. Ask this question | |
2623 | first, before target_preopen has a chance to kill anything. */ | |
2624 | if (remote_desc != NULL && !target_has_execution) | |
2625 | { | |
2626 | if (!from_tty | |
2627 | || query (_("Already connected to a remote target. Disconnect? "))) | |
2628 | pop_target (); | |
2629 | else | |
2630 | error (_("Still connected.")); | |
2631 | } | |
2632 | ||
c906108c SS |
2633 | target_preopen (from_tty); |
2634 | ||
2635 | unpush_target (target); | |
2636 | ||
2d717e4f DJ |
2637 | /* This time without a query. If we were connected to an |
2638 | extended-remote target and target_preopen killed the running | |
2639 | process, we may still be connected. If we are starting "target | |
2640 | remote" now, the extended-remote target will not have been | |
2641 | removed by unpush_target. */ | |
2642 | if (remote_desc != NULL && !target_has_execution) | |
2643 | pop_target (); | |
2644 | ||
89be2091 DJ |
2645 | /* Make sure we send the passed signals list the next time we resume. */ |
2646 | xfree (last_pass_packet); | |
2647 | last_pass_packet = NULL; | |
2648 | ||
ad9a8f3f | 2649 | remote_fileio_reset (); |
1dd41f16 NS |
2650 | reopen_exec_file (); |
2651 | reread_symbols (); | |
2652 | ||
9db8d71f | 2653 | remote_desc = remote_serial_open (name); |
c906108c SS |
2654 | if (!remote_desc) |
2655 | perror_with_name (name); | |
2656 | ||
2657 | if (baud_rate != -1) | |
2658 | { | |
2cd58942 | 2659 | if (serial_setbaudrate (remote_desc, baud_rate)) |
c906108c | 2660 | { |
9b74d5d3 KB |
2661 | /* The requested speed could not be set. Error out to |
2662 | top level after closing remote_desc. Take care to | |
2663 | set remote_desc to NULL to avoid closing remote_desc | |
2664 | more than once. */ | |
2cd58942 | 2665 | serial_close (remote_desc); |
9b74d5d3 | 2666 | remote_desc = NULL; |
c906108c SS |
2667 | perror_with_name (name); |
2668 | } | |
2669 | } | |
2670 | ||
2cd58942 | 2671 | serial_raw (remote_desc); |
c906108c SS |
2672 | |
2673 | /* If there is something sitting in the buffer we might take it as a | |
2674 | response to a command, which would be bad. */ | |
2cd58942 | 2675 | serial_flush_input (remote_desc); |
c906108c SS |
2676 | |
2677 | if (from_tty) | |
2678 | { | |
2679 | puts_filtered ("Remote debugging using "); | |
2680 | puts_filtered (name); | |
2681 | puts_filtered ("\n"); | |
2682 | } | |
23860348 | 2683 | push_target (target); /* Switch to using remote target now. */ |
c906108c | 2684 | |
2d717e4f DJ |
2685 | /* Assume that the target is running, unless we learn otherwise. */ |
2686 | target_mark_running (target); | |
2687 | ||
be2a5f71 DJ |
2688 | /* Reset the target state; these things will be queried either by |
2689 | remote_query_supported or as they are needed. */ | |
d471ea57 | 2690 | init_all_packet_configs (); |
be2a5f71 | 2691 | rs->explicit_packet_size = 0; |
802188a7 | 2692 | |
c5aa993b | 2693 | general_thread = -2; |
cce74817 | 2694 | continue_thread = -2; |
c906108c | 2695 | |
9d1f7ab2 MS |
2696 | /* Probe for ability to use "ThreadInfo" query, as required. */ |
2697 | use_threadinfo_query = 1; | |
2698 | use_threadextra_query = 1; | |
2699 | ||
be2a5f71 DJ |
2700 | /* The first packet we send to the target is the optional "supported |
2701 | packets" request. If the target can answer this, it will tell us | |
2702 | which later probes to skip. */ | |
2703 | remote_query_supported (); | |
2704 | ||
424163ea DJ |
2705 | /* Next, if the target can specify a description, read it. We do |
2706 | this before anything involving memory or registers. */ | |
2707 | target_find_description (); | |
2708 | ||
75c99385 | 2709 | if (remote_async_permitted) |
92d1e331 | 2710 | { |
23860348 | 2711 | /* With this target we start out by owning the terminal. */ |
92d1e331 DJ |
2712 | remote_async_terminal_ours_p = 1; |
2713 | ||
2714 | /* FIXME: cagney/1999-09-23: During the initial connection it is | |
2715 | assumed that the target is already ready and able to respond to | |
2716 | requests. Unfortunately remote_start_remote() eventually calls | |
2717 | wait_for_inferior() with no timeout. wait_forever_enabled_p gets | |
2718 | around this. Eventually a mechanism that allows | |
2719 | wait_for_inferior() to expect/get timeouts will be | |
23860348 | 2720 | implemented. */ |
92d1e331 DJ |
2721 | wait_forever_enabled_p = 0; |
2722 | } | |
2723 | ||
23860348 | 2724 | /* First delete any symbols previously loaded from shared libraries. */ |
f78f6cf1 | 2725 | no_shared_libraries (NULL, 0); |
f78f6cf1 | 2726 | |
36918e70 | 2727 | /* Start the remote connection. If error() or QUIT, discard this |
165b8e33 AC |
2728 | target (we'd otherwise be in an inconsistent state) and then |
2729 | propogate the error on up the exception chain. This ensures that | |
2730 | the caller doesn't stumble along blindly assuming that the | |
2731 | function succeeded. The CLI doesn't have this problem but other | |
2732 | UI's, such as MI do. | |
36918e70 AC |
2733 | |
2734 | FIXME: cagney/2002-05-19: Instead of re-throwing the exception, | |
2735 | this function should return an error indication letting the | |
ce2826aa | 2736 | caller restore the previous state. Unfortunately the command |
36918e70 AC |
2737 | ``target remote'' is directly wired to this function making that |
2738 | impossible. On a positive note, the CLI side of this problem has | |
2739 | been fixed - the function set_cmd_context() makes it possible for | |
2740 | all the ``target ....'' commands to share a common callback | |
2741 | function. See cli-dump.c. */ | |
109c3e39 | 2742 | { |
2d717e4f DJ |
2743 | struct gdb_exception ex; |
2744 | struct start_remote_args args; | |
2745 | ||
2746 | args.from_tty = from_tty; | |
2747 | args.target = target; | |
2748 | args.extended_p = extended_p; | |
2749 | ||
2750 | ex = catch_exception (uiout, remote_start_remote, &args, RETURN_MASK_ALL); | |
109c3e39 AC |
2751 | if (ex.reason < 0) |
2752 | { | |
2753 | pop_target (); | |
75c99385 | 2754 | if (remote_async_permitted) |
109c3e39 AC |
2755 | wait_forever_enabled_p = 1; |
2756 | throw_exception (ex); | |
2757 | } | |
2758 | } | |
c906108c | 2759 | |
75c99385 | 2760 | if (remote_async_permitted) |
92d1e331 | 2761 | wait_forever_enabled_p = 1; |
6426a772 JM |
2762 | |
2763 | if (extended_p) | |
43ff13b4 | 2764 | { |
6240bebf | 2765 | /* Tell the remote that we are using the extended protocol. */ |
6426a772 | 2766 | putpkt ("!"); |
6d820c5c | 2767 | getpkt (&rs->buf, &rs->buf_size, 0); |
43ff13b4 | 2768 | } |
a77053c2 | 2769 | |
2d717e4f DJ |
2770 | /* If we connected to a live target, do some additional setup. */ |
2771 | if (target_has_execution) | |
2772 | { | |
2773 | if (exec_bfd) /* No use without an exec file. */ | |
2774 | remote_check_symbols (symfile_objfile); | |
2775 | } | |
43ff13b4 JM |
2776 | } |
2777 | ||
c906108c SS |
2778 | /* This takes a program previously attached to and detaches it. After |
2779 | this is done, GDB can be used to debug some other program. We | |
2780 | better not have left any breakpoints in the target program or it'll | |
2781 | die when it hits one. */ | |
2782 | ||
2783 | static void | |
2d717e4f | 2784 | remote_detach_1 (char *args, int from_tty, int extended) |
c906108c | 2785 | { |
d01949b6 | 2786 | struct remote_state *rs = get_remote_state (); |
c906108c SS |
2787 | |
2788 | if (args) | |
8a3fe4f8 | 2789 | error (_("Argument given to \"detach\" when remotely debugging.")); |
c906108c | 2790 | |
2d717e4f DJ |
2791 | if (!target_has_execution) |
2792 | error (_("No process to detach from.")); | |
2793 | ||
c906108c | 2794 | /* Tell the remote target to detach. */ |
6d820c5c | 2795 | strcpy (rs->buf, "D"); |
4ddda9b5 PA |
2796 | putpkt (rs->buf); |
2797 | getpkt (&rs->buf, &rs->buf_size, 0); | |
2798 | ||
2799 | if (rs->buf[0] == 'E') | |
2800 | error (_("Can't detach process.")); | |
c906108c | 2801 | |
23860348 | 2802 | /* Unregister the file descriptor from the event loop. */ |
6ad8ae5c DJ |
2803 | if (target_is_async_p ()) |
2804 | serial_async (remote_desc, NULL, 0); | |
2805 | ||
cca728d0 | 2806 | target_mourn_inferior (); |
c906108c | 2807 | if (from_tty) |
2d717e4f DJ |
2808 | { |
2809 | if (extended) | |
2810 | puts_filtered ("Detached from remote process.\n"); | |
2811 | else | |
2812 | puts_filtered ("Ending remote debugging.\n"); | |
2813 | } | |
2814 | } | |
2815 | ||
2816 | static void | |
2817 | remote_detach (char *args, int from_tty) | |
2818 | { | |
2819 | remote_detach_1 (args, from_tty, 0); | |
2820 | } | |
2821 | ||
2822 | static void | |
2823 | extended_remote_detach (char *args, int from_tty) | |
2824 | { | |
2825 | remote_detach_1 (args, from_tty, 1); | |
c906108c SS |
2826 | } |
2827 | ||
6ad8ae5c DJ |
2828 | /* Same as remote_detach, but don't send the "D" packet; just disconnect. */ |
2829 | ||
43ff13b4 | 2830 | static void |
597320e7 | 2831 | remote_disconnect (struct target_ops *target, char *args, int from_tty) |
43ff13b4 | 2832 | { |
43ff13b4 | 2833 | if (args) |
2d717e4f | 2834 | error (_("Argument given to \"disconnect\" when remotely debugging.")); |
43ff13b4 | 2835 | |
23860348 | 2836 | /* Unregister the file descriptor from the event loop. */ |
ed9a39eb | 2837 | if (target_is_async_p ()) |
2cd58942 | 2838 | serial_async (remote_desc, NULL, 0); |
43ff13b4 | 2839 | |
2d717e4f DJ |
2840 | /* Make sure we unpush even the extended remote targets; mourn |
2841 | won't do it. So call remote_mourn_1 directly instead of | |
2842 | target_mourn_inferior. */ | |
2843 | remote_mourn_1 (target); | |
2844 | ||
43ff13b4 JM |
2845 | if (from_tty) |
2846 | puts_filtered ("Ending remote debugging.\n"); | |
2847 | } | |
2848 | ||
2d717e4f DJ |
2849 | /* Attach to the process specified by ARGS. If FROM_TTY is non-zero, |
2850 | be chatty about it. */ | |
2851 | ||
2852 | static void | |
2853 | extended_remote_attach_1 (struct target_ops *target, char *args, int from_tty) | |
2854 | { | |
2855 | struct remote_state *rs = get_remote_state (); | |
be86555c | 2856 | int pid; |
2d717e4f | 2857 | char *dummy; |
96ef3384 | 2858 | char *wait_status = NULL; |
2d717e4f DJ |
2859 | |
2860 | if (!args) | |
2861 | error_no_arg (_("process-id to attach")); | |
2862 | ||
2863 | dummy = args; | |
2864 | pid = strtol (args, &dummy, 0); | |
2865 | /* Some targets don't set errno on errors, grrr! */ | |
2866 | if (pid == 0 && args == dummy) | |
2867 | error (_("Illegal process-id: %s."), args); | |
2868 | ||
2869 | if (remote_protocol_packets[PACKET_vAttach].support == PACKET_DISABLE) | |
2870 | error (_("This target does not support attaching to a process")); | |
2871 | ||
2872 | sprintf (rs->buf, "vAttach;%x", pid); | |
2873 | putpkt (rs->buf); | |
2874 | getpkt (&rs->buf, &rs->buf_size, 0); | |
2875 | ||
2876 | if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vAttach]) == PACKET_OK) | |
2877 | { | |
2878 | if (from_tty) | |
2879 | printf_unfiltered (_("Attached to %s\n"), | |
2880 | target_pid_to_str (pid_to_ptid (pid))); | |
2881 | ||
96ef3384 UW |
2882 | /* Save the reply for later. */ |
2883 | wait_status = alloca (strlen (rs->buf) + 1); | |
2884 | strcpy (wait_status, rs->buf); | |
2d717e4f DJ |
2885 | } |
2886 | else if (remote_protocol_packets[PACKET_vAttach].support == PACKET_DISABLE) | |
2887 | error (_("This target does not support attaching to a process")); | |
2888 | else | |
2889 | error (_("Attaching to %s failed"), | |
2890 | target_pid_to_str (pid_to_ptid (pid))); | |
2891 | ||
2892 | target_mark_running (target); | |
2893 | inferior_ptid = pid_to_ptid (pid); | |
df7df359 | 2894 | attach_flag = 1; |
96ef3384 UW |
2895 | |
2896 | /* Next, if the target can specify a description, read it. We do | |
2897 | this before anything involving memory or registers. */ | |
2898 | target_find_description (); | |
2899 | ||
2900 | /* Use the previously fetched status. */ | |
2901 | gdb_assert (wait_status != NULL); | |
2902 | strcpy (rs->buf, wait_status); | |
2903 | rs->cached_wait_status = 1; | |
2d717e4f DJ |
2904 | } |
2905 | ||
2906 | static void | |
2907 | extended_remote_attach (char *args, int from_tty) | |
2908 | { | |
2909 | extended_remote_attach_1 (&extended_remote_ops, args, from_tty); | |
2910 | } | |
2911 | ||
c906108c SS |
2912 | /* Convert hex digit A to a number. */ |
2913 | ||
30559e10 | 2914 | static int |
fba45db2 | 2915 | fromhex (int a) |
c906108c SS |
2916 | { |
2917 | if (a >= '0' && a <= '9') | |
2918 | return a - '0'; | |
2919 | else if (a >= 'a' && a <= 'f') | |
2920 | return a - 'a' + 10; | |
2921 | else if (a >= 'A' && a <= 'F') | |
2922 | return a - 'A' + 10; | |
c5aa993b | 2923 | else |
8a3fe4f8 | 2924 | error (_("Reply contains invalid hex digit %d"), a); |
c906108c SS |
2925 | } |
2926 | ||
30559e10 | 2927 | static int |
cfd77fa1 | 2928 | hex2bin (const char *hex, gdb_byte *bin, int count) |
30559e10 MS |
2929 | { |
2930 | int i; | |
2931 | ||
30559e10 MS |
2932 | for (i = 0; i < count; i++) |
2933 | { | |
2934 | if (hex[0] == 0 || hex[1] == 0) | |
2935 | { | |
2936 | /* Hex string is short, or of uneven length. | |
23860348 | 2937 | Return the count that has been converted so far. */ |
30559e10 MS |
2938 | return i; |
2939 | } | |
2940 | *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]); | |
2941 | hex += 2; | |
2942 | } | |
2943 | return i; | |
2944 | } | |
2945 | ||
c906108c SS |
2946 | /* Convert number NIB to a hex digit. */ |
2947 | ||
2948 | static int | |
fba45db2 | 2949 | tohex (int nib) |
c906108c SS |
2950 | { |
2951 | if (nib < 10) | |
c5aa993b | 2952 | return '0' + nib; |
c906108c | 2953 | else |
c5aa993b | 2954 | return 'a' + nib - 10; |
c906108c | 2955 | } |
30559e10 MS |
2956 | |
2957 | static int | |
cfd77fa1 | 2958 | bin2hex (const gdb_byte *bin, char *hex, int count) |
30559e10 MS |
2959 | { |
2960 | int i; | |
23860348 | 2961 | /* May use a length, or a nul-terminated string as input. */ |
30559e10 | 2962 | if (count == 0) |
cfd77fa1 | 2963 | count = strlen ((char *) bin); |
30559e10 MS |
2964 | |
2965 | for (i = 0; i < count; i++) | |
2966 | { | |
2967 | *hex++ = tohex ((*bin >> 4) & 0xf); | |
2968 | *hex++ = tohex (*bin++ & 0xf); | |
2969 | } | |
2970 | *hex = 0; | |
2971 | return i; | |
2972 | } | |
c906108c | 2973 | \f |
506fb367 DJ |
2974 | /* Check for the availability of vCont. This function should also check |
2975 | the response. */ | |
c906108c SS |
2976 | |
2977 | static void | |
6d820c5c | 2978 | remote_vcont_probe (struct remote_state *rs) |
c906108c | 2979 | { |
2e9f7625 | 2980 | char *buf; |
6d820c5c | 2981 | |
2e9f7625 DJ |
2982 | strcpy (rs->buf, "vCont?"); |
2983 | putpkt (rs->buf); | |
6d820c5c | 2984 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 2985 | buf = rs->buf; |
c906108c | 2986 | |
506fb367 DJ |
2987 | /* Make sure that the features we assume are supported. */ |
2988 | if (strncmp (buf, "vCont", 5) == 0) | |
2989 | { | |
2990 | char *p = &buf[5]; | |
2991 | int support_s, support_S, support_c, support_C; | |
2992 | ||
2993 | support_s = 0; | |
2994 | support_S = 0; | |
2995 | support_c = 0; | |
2996 | support_C = 0; | |
2997 | while (p && *p == ';') | |
2998 | { | |
2999 | p++; | |
3000 | if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0)) | |
3001 | support_s = 1; | |
3002 | else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0)) | |
3003 | support_S = 1; | |
3004 | else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0)) | |
3005 | support_c = 1; | |
3006 | else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0)) | |
3007 | support_C = 1; | |
3008 | ||
3009 | p = strchr (p, ';'); | |
3010 | } | |
c906108c | 3011 | |
506fb367 DJ |
3012 | /* If s, S, c, and C are not all supported, we can't use vCont. Clearing |
3013 | BUF will make packet_ok disable the packet. */ | |
3014 | if (!support_s || !support_S || !support_c || !support_C) | |
3015 | buf[0] = 0; | |
3016 | } | |
c906108c | 3017 | |
444abaca | 3018 | packet_ok (buf, &remote_protocol_packets[PACKET_vCont]); |
506fb367 | 3019 | } |
c906108c | 3020 | |
506fb367 DJ |
3021 | /* Resume the remote inferior by using a "vCont" packet. The thread |
3022 | to be resumed is PTID; STEP and SIGGNAL indicate whether the | |
3023 | resumed thread should be single-stepped and/or signalled. If PTID's | |
3024 | PID is -1, then all threads are resumed; the thread to be stepped and/or | |
3025 | signalled is given in the global INFERIOR_PTID. This function returns | |
3026 | non-zero iff it resumes the inferior. | |
44eaed12 | 3027 | |
506fb367 DJ |
3028 | This function issues a strict subset of all possible vCont commands at the |
3029 | moment. */ | |
44eaed12 | 3030 | |
506fb367 DJ |
3031 | static int |
3032 | remote_vcont_resume (ptid_t ptid, int step, enum target_signal siggnal) | |
3033 | { | |
3034 | struct remote_state *rs = get_remote_state (); | |
3035 | int pid = PIDGET (ptid); | |
2d717e4f | 3036 | char *outbuf; |
506fb367 | 3037 | struct cleanup *old_cleanup; |
44eaed12 | 3038 | |
444abaca | 3039 | if (remote_protocol_packets[PACKET_vCont].support == PACKET_SUPPORT_UNKNOWN) |
6d820c5c | 3040 | remote_vcont_probe (rs); |
44eaed12 | 3041 | |
444abaca | 3042 | if (remote_protocol_packets[PACKET_vCont].support == PACKET_DISABLE) |
6d820c5c | 3043 | return 0; |
44eaed12 | 3044 | |
506fb367 DJ |
3045 | /* If we could generate a wider range of packets, we'd have to worry |
3046 | about overflowing BUF. Should there be a generic | |
3047 | "multi-part-packet" packet? */ | |
3048 | ||
3049 | if (PIDGET (inferior_ptid) == MAGIC_NULL_PID) | |
c906108c | 3050 | { |
506fb367 DJ |
3051 | /* MAGIC_NULL_PTID means that we don't have any active threads, so we |
3052 | don't have any PID numbers the inferior will understand. Make sure | |
3053 | to only send forms that do not specify a PID. */ | |
3054 | if (step && siggnal != TARGET_SIGNAL_0) | |
2963ee1d | 3055 | outbuf = xstrprintf ("vCont;S%02x", siggnal); |
506fb367 | 3056 | else if (step) |
2963ee1d | 3057 | outbuf = xstrprintf ("vCont;s"); |
506fb367 | 3058 | else if (siggnal != TARGET_SIGNAL_0) |
2963ee1d | 3059 | outbuf = xstrprintf ("vCont;C%02x", siggnal); |
506fb367 | 3060 | else |
2963ee1d | 3061 | outbuf = xstrprintf ("vCont;c"); |
506fb367 DJ |
3062 | } |
3063 | else if (pid == -1) | |
3064 | { | |
3065 | /* Resume all threads, with preference for INFERIOR_PTID. */ | |
3066 | if (step && siggnal != TARGET_SIGNAL_0) | |
2963ee1d DJ |
3067 | outbuf = xstrprintf ("vCont;S%02x:%x;c", siggnal, |
3068 | PIDGET (inferior_ptid)); | |
506fb367 | 3069 | else if (step) |
2963ee1d | 3070 | outbuf = xstrprintf ("vCont;s:%x;c", PIDGET (inferior_ptid)); |
506fb367 | 3071 | else if (siggnal != TARGET_SIGNAL_0) |
2963ee1d DJ |
3072 | outbuf = xstrprintf ("vCont;C%02x:%x;c", siggnal, |
3073 | PIDGET (inferior_ptid)); | |
506fb367 | 3074 | else |
2963ee1d | 3075 | outbuf = xstrprintf ("vCont;c"); |
c906108c SS |
3076 | } |
3077 | else | |
506fb367 DJ |
3078 | { |
3079 | /* Scheduler locking; resume only PTID. */ | |
3080 | if (step && siggnal != TARGET_SIGNAL_0) | |
2963ee1d | 3081 | outbuf = xstrprintf ("vCont;S%02x:%x", siggnal, pid); |
506fb367 | 3082 | else if (step) |
2963ee1d | 3083 | outbuf = xstrprintf ("vCont;s:%x", pid); |
506fb367 | 3084 | else if (siggnal != TARGET_SIGNAL_0) |
2963ee1d | 3085 | outbuf = xstrprintf ("vCont;C%02x:%x", siggnal, pid); |
506fb367 | 3086 | else |
2963ee1d | 3087 | outbuf = xstrprintf ("vCont;c:%x", pid); |
506fb367 | 3088 | } |
c906108c | 3089 | |
ea9c271d | 3090 | gdb_assert (outbuf && strlen (outbuf) < get_remote_packet_size ()); |
6d820c5c | 3091 | old_cleanup = make_cleanup (xfree, outbuf); |
2963ee1d DJ |
3092 | |
3093 | putpkt (outbuf); | |
506fb367 DJ |
3094 | |
3095 | do_cleanups (old_cleanup); | |
3096 | ||
3097 | return 1; | |
c906108c | 3098 | } |
43ff13b4 | 3099 | |
506fb367 DJ |
3100 | /* Tell the remote machine to resume. */ |
3101 | ||
3102 | static enum target_signal last_sent_signal = TARGET_SIGNAL_0; | |
3103 | ||
3104 | static int last_sent_step; | |
3105 | ||
43ff13b4 | 3106 | static void |
506fb367 | 3107 | remote_resume (ptid_t ptid, int step, enum target_signal siggnal) |
43ff13b4 | 3108 | { |
d01949b6 | 3109 | struct remote_state *rs = get_remote_state (); |
2e9f7625 | 3110 | char *buf; |
39f77062 | 3111 | int pid = PIDGET (ptid); |
43ff13b4 | 3112 | |
43ff13b4 JM |
3113 | last_sent_signal = siggnal; |
3114 | last_sent_step = step; | |
3115 | ||
89be2091 DJ |
3116 | /* Update the inferior on signals to silently pass, if they've changed. */ |
3117 | remote_pass_signals (); | |
3118 | ||
506fb367 DJ |
3119 | /* The vCont packet doesn't need to specify threads via Hc. */ |
3120 | if (remote_vcont_resume (ptid, step, siggnal)) | |
75c99385 | 3121 | goto done; |
506fb367 DJ |
3122 | |
3123 | /* All other supported resume packets do use Hc, so call set_thread. */ | |
3124 | if (pid == -1) | |
23860348 | 3125 | set_thread (0, 0); /* Run any thread. */ |
506fb367 | 3126 | else |
23860348 | 3127 | set_thread (pid, 0); /* Run this thread. */ |
506fb367 | 3128 | |
2e9f7625 | 3129 | buf = rs->buf; |
43ff13b4 JM |
3130 | if (siggnal != TARGET_SIGNAL_0) |
3131 | { | |
3132 | buf[0] = step ? 'S' : 'C'; | |
c5aa993b | 3133 | buf[1] = tohex (((int) siggnal >> 4) & 0xf); |
506fb367 | 3134 | buf[2] = tohex (((int) siggnal) & 0xf); |
43ff13b4 JM |
3135 | buf[3] = '\0'; |
3136 | } | |
3137 | else | |
c5aa993b | 3138 | strcpy (buf, step ? "s" : "c"); |
506fb367 | 3139 | |
44eaed12 | 3140 | putpkt (buf); |
43ff13b4 | 3141 | |
75c99385 | 3142 | done: |
2acceee2 JM |
3143 | /* We are about to start executing the inferior, let's register it |
3144 | with the event loop. NOTE: this is the one place where all the | |
3145 | execution commands end up. We could alternatively do this in each | |
23860348 | 3146 | of the execution commands in infcmd.c. */ |
2acceee2 JM |
3147 | /* FIXME: ezannoni 1999-09-28: We may need to move this out of here |
3148 | into infcmd.c in order to allow inferior function calls to work | |
23860348 | 3149 | NOT asynchronously. */ |
362646f5 | 3150 | if (target_can_async_p ()) |
2acceee2 | 3151 | target_async (inferior_event_handler, 0); |
23860348 | 3152 | /* Tell the world that the target is now executing. */ |
2acceee2 JM |
3153 | /* FIXME: cagney/1999-09-23: Is it the targets responsibility to set |
3154 | this? Instead, should the client of target just assume (for | |
3155 | async targets) that the target is going to start executing? Is | |
3156 | this information already found in the continuation block? */ | |
ed9a39eb | 3157 | if (target_is_async_p ()) |
2acceee2 | 3158 | target_executing = 1; |
43ff13b4 | 3159 | } |
c906108c | 3160 | \f |
43ff13b4 JM |
3161 | |
3162 | /* Set up the signal handler for SIGINT, while the target is | |
23860348 | 3163 | executing, ovewriting the 'regular' SIGINT signal handler. */ |
43ff13b4 | 3164 | static void |
fba45db2 | 3165 | initialize_sigint_signal_handler (void) |
43ff13b4 | 3166 | { |
43ff13b4 JM |
3167 | signal (SIGINT, handle_remote_sigint); |
3168 | } | |
3169 | ||
23860348 | 3170 | /* Signal handler for SIGINT, while the target is executing. */ |
43ff13b4 | 3171 | static void |
fba45db2 | 3172 | handle_remote_sigint (int sig) |
43ff13b4 JM |
3173 | { |
3174 | signal (sig, handle_remote_sigint_twice); | |
43ff13b4 JM |
3175 | mark_async_signal_handler_wrapper (sigint_remote_token); |
3176 | } | |
3177 | ||
3178 | /* Signal handler for SIGINT, installed after SIGINT has already been | |
3179 | sent once. It will take effect the second time that the user sends | |
23860348 | 3180 | a ^C. */ |
43ff13b4 | 3181 | static void |
fba45db2 | 3182 | handle_remote_sigint_twice (int sig) |
43ff13b4 | 3183 | { |
b803fb0f | 3184 | signal (sig, handle_remote_sigint); |
43ff13b4 JM |
3185 | mark_async_signal_handler_wrapper (sigint_remote_twice_token); |
3186 | } | |
3187 | ||
6426a772 | 3188 | /* Perform the real interruption of the target execution, in response |
23860348 | 3189 | to a ^C. */ |
c5aa993b | 3190 | static void |
fba45db2 | 3191 | async_remote_interrupt (gdb_client_data arg) |
43ff13b4 JM |
3192 | { |
3193 | if (remote_debug) | |
3194 | fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n"); | |
3195 | ||
3196 | target_stop (); | |
3197 | } | |
3198 | ||
3199 | /* Perform interrupt, if the first attempt did not succeed. Just give | |
23860348 | 3200 | up on the target alltogether. */ |
2df3850c | 3201 | void |
fba45db2 | 3202 | async_remote_interrupt_twice (gdb_client_data arg) |
43ff13b4 | 3203 | { |
2df3850c JM |
3204 | if (remote_debug) |
3205 | fprintf_unfiltered (gdb_stdlog, "remote_interrupt_twice called\n"); | |
b803fb0f DJ |
3206 | |
3207 | interrupt_query (); | |
43ff13b4 JM |
3208 | } |
3209 | ||
3210 | /* Reinstall the usual SIGINT handlers, after the target has | |
23860348 | 3211 | stopped. */ |
6426a772 JM |
3212 | static void |
3213 | cleanup_sigint_signal_handler (void *dummy) | |
43ff13b4 JM |
3214 | { |
3215 | signal (SIGINT, handle_sigint); | |
43ff13b4 JM |
3216 | } |
3217 | ||
c906108c SS |
3218 | /* Send ^C to target to halt it. Target will respond, and send us a |
3219 | packet. */ | |
507f3c78 | 3220 | static void (*ofunc) (int); |
c906108c | 3221 | |
7a292a7a SS |
3222 | /* The command line interface's stop routine. This function is installed |
3223 | as a signal handler for SIGINT. The first time a user requests a | |
3224 | stop, we call remote_stop to send a break or ^C. If there is no | |
3225 | response from the target (it didn't stop when the user requested it), | |
23860348 | 3226 | we ask the user if he'd like to detach from the target. */ |
c906108c | 3227 | static void |
fba45db2 | 3228 | remote_interrupt (int signo) |
c906108c | 3229 | { |
23860348 | 3230 | /* If this doesn't work, try more severe steps. */ |
7a292a7a SS |
3231 | signal (signo, remote_interrupt_twice); |
3232 | ||
b803fb0f | 3233 | gdb_call_async_signal_handler (sigint_remote_token, 1); |
7a292a7a SS |
3234 | } |
3235 | ||
3236 | /* The user typed ^C twice. */ | |
3237 | ||
3238 | static void | |
fba45db2 | 3239 | remote_interrupt_twice (int signo) |
7a292a7a SS |
3240 | { |
3241 | signal (signo, ofunc); | |
b803fb0f | 3242 | gdb_call_async_signal_handler (sigint_remote_twice_token, 1); |
c906108c SS |
3243 | signal (signo, remote_interrupt); |
3244 | } | |
7a292a7a SS |
3245 | |
3246 | /* This is the generic stop called via the target vector. When a target | |
3247 | interrupt is requested, either by the command line or the GUI, we | |
23860348 | 3248 | will eventually end up here. */ |
c906108c | 3249 | static void |
fba45db2 | 3250 | remote_stop (void) |
c906108c | 3251 | { |
7a292a7a SS |
3252 | /* Send a break or a ^C, depending on user preference. */ |
3253 | if (remote_debug) | |
0f71a2f6 | 3254 | fprintf_unfiltered (gdb_stdlog, "remote_stop called\n"); |
c906108c | 3255 | |
7a292a7a | 3256 | if (remote_break) |
2cd58942 | 3257 | serial_send_break (remote_desc); |
c906108c | 3258 | else |
2cd58942 | 3259 | serial_write (remote_desc, "\003", 1); |
c906108c SS |
3260 | } |
3261 | ||
3262 | /* Ask the user what to do when an interrupt is received. */ | |
3263 | ||
3264 | static void | |
fba45db2 | 3265 | interrupt_query (void) |
c906108c SS |
3266 | { |
3267 | target_terminal_ours (); | |
3268 | ||
3269 | if (query ("Interrupted while waiting for the program.\n\ | |
3270 | Give up (and stop debugging it)? ")) | |
3271 | { | |
3272 | target_mourn_inferior (); | |
b803fb0f | 3273 | signal (SIGINT, handle_sigint); |
315a522e | 3274 | deprecated_throw_reason (RETURN_QUIT); |
c906108c SS |
3275 | } |
3276 | ||
3277 | target_terminal_inferior (); | |
3278 | } | |
3279 | ||
6426a772 JM |
3280 | /* Enable/disable target terminal ownership. Most targets can use |
3281 | terminal groups to control terminal ownership. Remote targets are | |
3282 | different in that explicit transfer of ownership to/from GDB/target | |
23860348 | 3283 | is required. */ |
6426a772 JM |
3284 | |
3285 | static void | |
75c99385 | 3286 | remote_terminal_inferior (void) |
6426a772 | 3287 | { |
75c99385 PA |
3288 | if (!remote_async_permitted) |
3289 | /* Nothing to do. */ | |
3290 | return; | |
3291 | ||
6426a772 JM |
3292 | /* FIXME: cagney/1999-09-27: Shouldn't need to test for |
3293 | sync_execution here. This function should only be called when | |
3294 | GDB is resuming the inferior in the forground. A background | |
3295 | resume (``run&'') should leave GDB in control of the terminal and | |
23860348 | 3296 | consequently should not call this code. */ |
6426a772 JM |
3297 | if (!sync_execution) |
3298 | return; | |
3299 | /* FIXME: cagney/1999-09-27: Closely related to the above. Make | |
3300 | calls target_terminal_*() idenpotent. The event-loop GDB talking | |
3301 | to an asynchronous target with a synchronous command calls this | |
3302 | function from both event-top.c and infrun.c/infcmd.c. Once GDB | |
3303 | stops trying to transfer the terminal to the target when it | |
3304 | shouldn't this guard can go away. */ | |
3305 | if (!remote_async_terminal_ours_p) | |
3306 | return; | |
3307 | delete_file_handler (input_fd); | |
3308 | remote_async_terminal_ours_p = 0; | |
3309 | initialize_sigint_signal_handler (); | |
3310 | /* NOTE: At this point we could also register our selves as the | |
3311 | recipient of all input. Any characters typed could then be | |
23860348 | 3312 | passed on down to the target. */ |
6426a772 JM |
3313 | } |
3314 | ||
3315 | static void | |
75c99385 | 3316 | remote_terminal_ours (void) |
6426a772 | 3317 | { |
75c99385 PA |
3318 | if (!remote_async_permitted) |
3319 | /* Nothing to do. */ | |
3320 | return; | |
3321 | ||
3322 | /* See FIXME in remote_terminal_inferior. */ | |
6426a772 JM |
3323 | if (!sync_execution) |
3324 | return; | |
75c99385 | 3325 | /* See FIXME in remote_terminal_inferior. */ |
6426a772 JM |
3326 | if (remote_async_terminal_ours_p) |
3327 | return; | |
3328 | cleanup_sigint_signal_handler (NULL); | |
3329 | add_file_handler (input_fd, stdin_event_handler, 0); | |
3330 | remote_async_terminal_ours_p = 1; | |
3331 | } | |
3332 | ||
c906108c | 3333 | void |
917317f4 | 3334 | remote_console_output (char *msg) |
c906108c SS |
3335 | { |
3336 | char *p; | |
3337 | ||
c5aa993b | 3338 | for (p = msg; p[0] && p[1]; p += 2) |
c906108c SS |
3339 | { |
3340 | char tb[2]; | |
3341 | char c = fromhex (p[0]) * 16 + fromhex (p[1]); | |
3342 | tb[0] = c; | |
3343 | tb[1] = 0; | |
43ff13b4 | 3344 | fputs_unfiltered (tb, gdb_stdtarg); |
c906108c | 3345 | } |
917317f4 | 3346 | gdb_flush (gdb_stdtarg); |
c906108c SS |
3347 | } |
3348 | ||
0f71a2f6 JM |
3349 | /* Wait until the remote machine stops, then return, |
3350 | storing status in STATUS just as `wait' would. | |
802188a7 | 3351 | Returns "pid", which in the case of a multi-threaded |
0f71a2f6 | 3352 | remote OS, is the thread-id. */ |
c906108c | 3353 | |
39f77062 KB |
3354 | static ptid_t |
3355 | remote_wait (ptid_t ptid, struct target_waitstatus *status) | |
c906108c | 3356 | { |
d01949b6 | 3357 | struct remote_state *rs = get_remote_state (); |
ea9c271d | 3358 | struct remote_arch_state *rsa = get_remote_arch_state (); |
b2dd6311 | 3359 | ULONGEST thread_num = -1; |
3c3bea1c | 3360 | ULONGEST addr; |
cfa9d6d9 | 3361 | int solibs_changed = 0; |
c906108c SS |
3362 | |
3363 | status->kind = TARGET_WAITKIND_EXITED; | |
3364 | status->value.integer = 0; | |
3365 | ||
43ff13b4 JM |
3366 | while (1) |
3367 | { | |
2e9f7625 | 3368 | char *buf, *p; |
c5aa993b | 3369 | |
2d717e4f DJ |
3370 | if (rs->cached_wait_status) |
3371 | /* Use the cached wait status, but only once. */ | |
3372 | rs->cached_wait_status = 0; | |
3373 | else | |
9fa2223d | 3374 | { |
2d717e4f | 3375 | if (!target_is_async_p ()) |
9fa2223d | 3376 | { |
2d717e4f DJ |
3377 | ofunc = signal (SIGINT, remote_interrupt); |
3378 | /* If the user hit C-c before this packet, or between packets, | |
3379 | pretend that it was hit right here. */ | |
3380 | if (quit_flag) | |
3381 | { | |
3382 | quit_flag = 0; | |
3383 | remote_interrupt (SIGINT); | |
3384 | } | |
9fa2223d | 3385 | } |
2d717e4f DJ |
3386 | /* FIXME: cagney/1999-09-27: If we're in async mode we should |
3387 | _never_ wait for ever -> test on target_is_async_p(). | |
3388 | However, before we do that we need to ensure that the caller | |
3389 | knows how to take the target into/out of async mode. */ | |
3390 | getpkt (&rs->buf, &rs->buf_size, wait_forever_enabled_p); | |
3391 | if (!target_is_async_p ()) | |
3392 | signal (SIGINT, ofunc); | |
9fa2223d | 3393 | } |
43ff13b4 | 3394 | |
2e9f7625 DJ |
3395 | buf = rs->buf; |
3396 | ||
75c99385 PA |
3397 | remote_stopped_by_watchpoint_p = 0; |
3398 | ||
43ff13b4 JM |
3399 | switch (buf[0]) |
3400 | { | |
23860348 | 3401 | case 'E': /* Error of some sort. */ |
20b4711e DJ |
3402 | /* We're out of sync with the target now. Did it continue or not? |
3403 | Not is more likely, so report a stop. */ | |
8a3fe4f8 | 3404 | warning (_("Remote failure reply: %s"), buf); |
20b4711e DJ |
3405 | status->kind = TARGET_WAITKIND_STOPPED; |
3406 | status->value.sig = TARGET_SIGNAL_0; | |
3407 | goto got_status; | |
23860348 | 3408 | case 'F': /* File-I/O request. */ |
449092f6 CV |
3409 | remote_fileio_request (buf); |
3410 | continue; | |
23860348 | 3411 | case 'T': /* Status with PC, SP, FP, ... */ |
43ff13b4 | 3412 | { |
cfd77fa1 | 3413 | gdb_byte regs[MAX_REGISTER_SIZE]; |
43ff13b4 | 3414 | |
23860348 | 3415 | /* Expedited reply, containing Signal, {regno, reg} repeat. */ |
43ff13b4 | 3416 | /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where |
c5aa993b JM |
3417 | ss = signal number |
3418 | n... = register number | |
3419 | r... = register contents | |
3420 | */ | |
43ff13b4 JM |
3421 | p = &buf[3]; /* after Txx */ |
3422 | ||
3423 | while (*p) | |
3424 | { | |
cfd77fa1 | 3425 | char *p1; |
43ff13b4 | 3426 | char *p_temp; |
6c3f2dbf | 3427 | int fieldsize; |
75c99385 | 3428 | LONGEST pnum = 0; |
43ff13b4 | 3429 | |
23860348 MS |
3430 | /* If the packet contains a register number, save it |
3431 | in pnum and set p1 to point to the character | |
3432 | following it. Otherwise p1 points to p. */ | |
3c3bea1c | 3433 | |
75c99385 PA |
3434 | /* If this packet is an awatch packet, don't parse the |
3435 | 'a' as a register number. */ | |
802188a7 | 3436 | |
cfa9d6d9 | 3437 | if (strncmp (p, "awatch", strlen("awatch")) != 0) |
3c3bea1c | 3438 | { |
75c99385 | 3439 | /* Read the ``P'' register number. */ |
3c3bea1c | 3440 | pnum = strtol (p, &p_temp, 16); |
cfd77fa1 | 3441 | p1 = p_temp; |
3c3bea1c | 3442 | } |
802188a7 | 3443 | else |
3c3bea1c | 3444 | p1 = p; |
43ff13b4 | 3445 | |
23860348 | 3446 | if (p1 == p) /* No register number present here. */ |
43ff13b4 | 3447 | { |
cfd77fa1 | 3448 | p1 = strchr (p, ':'); |
43ff13b4 | 3449 | if (p1 == NULL) |
8a3fe4f8 AC |
3450 | error (_("Malformed packet(a) (missing colon): %s\n\ |
3451 | Packet: '%s'\n"), | |
3fcb8548 | 3452 | p, buf); |
3c3bea1c | 3453 | if (strncmp (p, "thread", p1 - p) == 0) |
43ff13b4 JM |
3454 | { |
3455 | p_temp = unpack_varlen_hex (++p1, &thread_num); | |
3456 | record_currthread (thread_num); | |
cfd77fa1 | 3457 | p = p_temp; |
43ff13b4 | 3458 | } |
3c3bea1c GS |
3459 | else if ((strncmp (p, "watch", p1 - p) == 0) |
3460 | || (strncmp (p, "rwatch", p1 - p) == 0) | |
3461 | || (strncmp (p, "awatch", p1 - p) == 0)) | |
3462 | { | |
3463 | remote_stopped_by_watchpoint_p = 1; | |
3464 | p = unpack_varlen_hex (++p1, &addr); | |
3465 | remote_watch_data_address = (CORE_ADDR)addr; | |
3466 | } | |
cfa9d6d9 DJ |
3467 | else if (strncmp (p, "library", p1 - p) == 0) |
3468 | { | |
3469 | p1++; | |
3470 | p_temp = p1; | |
3471 | while (*p_temp && *p_temp != ';') | |
3472 | p_temp++; | |
3473 | ||
3474 | solibs_changed = 1; | |
3475 | p = p_temp; | |
3476 | } | |
3c3bea1c GS |
3477 | else |
3478 | { | |
3479 | /* Silently skip unknown optional info. */ | |
cfd77fa1 | 3480 | p_temp = strchr (p1 + 1, ';'); |
3c3bea1c GS |
3481 | if (p_temp) |
3482 | p = p_temp; | |
3483 | } | |
43ff13b4 JM |
3484 | } |
3485 | else | |
3486 | { | |
ea9c271d | 3487 | struct packet_reg *reg = packet_reg_from_pnum (rsa, pnum); |
43ff13b4 | 3488 | p = p1; |
75c99385 | 3489 | |
43ff13b4 | 3490 | if (*p++ != ':') |
8a3fe4f8 AC |
3491 | error (_("Malformed packet(b) (missing colon): %s\n\ |
3492 | Packet: '%s'\n"), | |
3fcb8548 | 3493 | p, buf); |
43ff13b4 | 3494 | |
ad10f812 | 3495 | if (reg == NULL) |
75c99385 | 3496 | error (_("Remote sent bad register number %s: %s\n\ |
8a3fe4f8 | 3497 | Packet: '%s'\n"), |
75c99385 | 3498 | phex_nz (pnum, 0), p, buf); |
43ff13b4 | 3499 | |
cfd77fa1 | 3500 | fieldsize = hex2bin (p, regs, |
2bc416ba | 3501 | register_size (current_gdbarch, |
23860348 | 3502 | reg->regnum)); |
6c3f2dbf | 3503 | p += 2 * fieldsize; |
2bc416ba | 3504 | if (fieldsize < register_size (current_gdbarch, |
23860348 | 3505 | reg->regnum)) |
8a3fe4f8 | 3506 | warning (_("Remote reply is too short: %s"), buf); |
594f7785 UW |
3507 | regcache_raw_supply (get_current_regcache (), |
3508 | reg->regnum, regs); | |
43ff13b4 JM |
3509 | } |
3510 | ||
3511 | if (*p++ != ';') | |
8a3fe4f8 | 3512 | error (_("Remote register badly formatted: %s\nhere: %s"), |
0a2cfde4 | 3513 | buf, p); |
43ff13b4 JM |
3514 | } |
3515 | } | |
3516 | /* fall through */ | |
23860348 | 3517 | case 'S': /* Old style status, just signal only. */ |
cfa9d6d9 DJ |
3518 | if (solibs_changed) |
3519 | status->kind = TARGET_WAITKIND_LOADED; | |
3520 | else | |
3521 | { | |
3522 | status->kind = TARGET_WAITKIND_STOPPED; | |
3523 | status->value.sig = (enum target_signal) | |
3524 | (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))); | |
3525 | } | |
43ff13b4 JM |
3526 | |
3527 | if (buf[3] == 'p') | |
3528 | { | |
43ff13b4 JM |
3529 | thread_num = strtol ((const char *) &buf[4], NULL, 16); |
3530 | record_currthread (thread_num); | |
3531 | } | |
43ff13b4 | 3532 | goto got_status; |
23860348 | 3533 | case 'W': /* Target exited. */ |
43ff13b4 JM |
3534 | { |
3535 | /* The remote process exited. */ | |
3536 | status->kind = TARGET_WAITKIND_EXITED; | |
3537 | status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]); | |
3538 | goto got_status; | |
3539 | } | |
3540 | case 'X': | |
3541 | status->kind = TARGET_WAITKIND_SIGNALLED; | |
3542 | status->value.sig = (enum target_signal) | |
3543 | (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))); | |
43ff13b4 JM |
3544 | |
3545 | goto got_status; | |
23860348 | 3546 | case 'O': /* Console output. */ |
43ff13b4 | 3547 | remote_console_output (buf + 1); |
75c99385 PA |
3548 | if (target_can_async_p ()) |
3549 | { | |
3550 | /* Return immediately to the event loop. The event loop | |
3551 | will still be waiting on the inferior afterwards. */ | |
3552 | status->kind = TARGET_WAITKIND_IGNORE; | |
3553 | goto got_status; | |
3554 | } | |
3555 | else | |
3556 | continue; | |
43ff13b4 JM |
3557 | case '\0': |
3558 | if (last_sent_signal != TARGET_SIGNAL_0) | |
3559 | { | |
3560 | /* Zero length reply means that we tried 'S' or 'C' and | |
c5aa993b | 3561 | the remote system doesn't support it. */ |
43ff13b4 JM |
3562 | target_terminal_ours_for_output (); |
3563 | printf_filtered | |
3564 | ("Can't send signals to this remote system. %s not sent.\n", | |
3565 | target_signal_to_name (last_sent_signal)); | |
3566 | last_sent_signal = TARGET_SIGNAL_0; | |
3567 | target_terminal_inferior (); | |
3568 | ||
3569 | strcpy ((char *) buf, last_sent_step ? "s" : "c"); | |
3570 | putpkt ((char *) buf); | |
3571 | continue; | |
3572 | } | |
3573 | /* else fallthrough */ | |
3574 | default: | |
8a3fe4f8 | 3575 | warning (_("Invalid remote reply: %s"), buf); |
43ff13b4 JM |
3576 | continue; |
3577 | } | |
3578 | } | |
c5aa993b | 3579 | got_status: |
43ff13b4 JM |
3580 | if (thread_num != -1) |
3581 | { | |
39f77062 | 3582 | return pid_to_ptid (thread_num); |
43ff13b4 | 3583 | } |
39f77062 | 3584 | return inferior_ptid; |
43ff13b4 JM |
3585 | } |
3586 | ||
74ca34ce | 3587 | /* Fetch a single register using a 'p' packet. */ |
c906108c | 3588 | |
b96ec7ac | 3589 | static int |
56be3814 | 3590 | fetch_register_using_p (struct regcache *regcache, struct packet_reg *reg) |
b96ec7ac AC |
3591 | { |
3592 | struct remote_state *rs = get_remote_state (); | |
2e9f7625 | 3593 | char *buf, *p; |
b96ec7ac AC |
3594 | char regp[MAX_REGISTER_SIZE]; |
3595 | int i; | |
3596 | ||
74ca34ce DJ |
3597 | if (remote_protocol_packets[PACKET_p].support == PACKET_DISABLE) |
3598 | return 0; | |
3599 | ||
3600 | if (reg->pnum == -1) | |
3601 | return 0; | |
3602 | ||
2e9f7625 | 3603 | p = rs->buf; |
fcad0fa4 | 3604 | *p++ = 'p'; |
74ca34ce | 3605 | p += hexnumstr (p, reg->pnum); |
fcad0fa4 | 3606 | *p++ = '\0'; |
6d820c5c | 3607 | remote_send (&rs->buf, &rs->buf_size); |
3f9a994c | 3608 | |
2e9f7625 DJ |
3609 | buf = rs->buf; |
3610 | ||
74ca34ce DJ |
3611 | switch (packet_ok (buf, &remote_protocol_packets[PACKET_p])) |
3612 | { | |
3613 | case PACKET_OK: | |
3614 | break; | |
3615 | case PACKET_UNKNOWN: | |
3616 | return 0; | |
3617 | case PACKET_ERROR: | |
3618 | error (_("Could not fetch register \"%s\""), | |
4a22f64d | 3619 | gdbarch_register_name (get_regcache_arch (regcache), reg->regnum)); |
74ca34ce | 3620 | } |
3f9a994c JB |
3621 | |
3622 | /* If this register is unfetchable, tell the regcache. */ | |
3623 | if (buf[0] == 'x') | |
8480adf2 | 3624 | { |
56be3814 | 3625 | regcache_raw_supply (regcache, reg->regnum, NULL); |
8480adf2 | 3626 | return 1; |
b96ec7ac | 3627 | } |
b96ec7ac | 3628 | |
3f9a994c JB |
3629 | /* Otherwise, parse and supply the value. */ |
3630 | p = buf; | |
3631 | i = 0; | |
3632 | while (p[0] != 0) | |
3633 | { | |
3634 | if (p[1] == 0) | |
74ca34ce | 3635 | error (_("fetch_register_using_p: early buf termination")); |
3f9a994c JB |
3636 | |
3637 | regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]); | |
3638 | p += 2; | |
3639 | } | |
56be3814 | 3640 | regcache_raw_supply (regcache, reg->regnum, regp); |
3f9a994c | 3641 | return 1; |
b96ec7ac AC |
3642 | } |
3643 | ||
74ca34ce DJ |
3644 | /* Fetch the registers included in the target's 'g' packet. */ |
3645 | ||
29709017 DJ |
3646 | static int |
3647 | send_g_packet (void) | |
c906108c | 3648 | { |
d01949b6 | 3649 | struct remote_state *rs = get_remote_state (); |
74ca34ce | 3650 | int i, buf_len; |
c906108c | 3651 | char *p; |
74ca34ce | 3652 | char *regs; |
c906108c | 3653 | |
74ca34ce DJ |
3654 | sprintf (rs->buf, "g"); |
3655 | remote_send (&rs->buf, &rs->buf_size); | |
c906108c | 3656 | |
29709017 DJ |
3657 | /* We can get out of synch in various cases. If the first character |
3658 | in the buffer is not a hex character, assume that has happened | |
3659 | and try to fetch another packet to read. */ | |
3660 | while ((rs->buf[0] < '0' || rs->buf[0] > '9') | |
3661 | && (rs->buf[0] < 'A' || rs->buf[0] > 'F') | |
3662 | && (rs->buf[0] < 'a' || rs->buf[0] > 'f') | |
3663 | && rs->buf[0] != 'x') /* New: unavailable register value. */ | |
3664 | { | |
3665 | if (remote_debug) | |
3666 | fprintf_unfiltered (gdb_stdlog, | |
3667 | "Bad register packet; fetching a new packet\n"); | |
3668 | getpkt (&rs->buf, &rs->buf_size, 0); | |
3669 | } | |
3670 | ||
74ca34ce DJ |
3671 | buf_len = strlen (rs->buf); |
3672 | ||
3673 | /* Sanity check the received packet. */ | |
3674 | if (buf_len % 2 != 0) | |
3675 | error (_("Remote 'g' packet reply is of odd length: %s"), rs->buf); | |
29709017 DJ |
3676 | |
3677 | return buf_len / 2; | |
3678 | } | |
3679 | ||
3680 | static void | |
56be3814 | 3681 | process_g_packet (struct regcache *regcache) |
29709017 | 3682 | { |
4a22f64d | 3683 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
29709017 DJ |
3684 | struct remote_state *rs = get_remote_state (); |
3685 | struct remote_arch_state *rsa = get_remote_arch_state (); | |
3686 | int i, buf_len; | |
3687 | char *p; | |
3688 | char *regs; | |
3689 | ||
3690 | buf_len = strlen (rs->buf); | |
3691 | ||
3692 | /* Further sanity checks, with knowledge of the architecture. */ | |
74ca34ce DJ |
3693 | if (buf_len > 2 * rsa->sizeof_g_packet) |
3694 | error (_("Remote 'g' packet reply is too long: %s"), rs->buf); | |
3695 | ||
3696 | /* Save the size of the packet sent to us by the target. It is used | |
3697 | as a heuristic when determining the max size of packets that the | |
3698 | target can safely receive. */ | |
3699 | if (rsa->actual_register_packet_size == 0) | |
3700 | rsa->actual_register_packet_size = buf_len; | |
3701 | ||
3702 | /* If this is smaller than we guessed the 'g' packet would be, | |
3703 | update our records. A 'g' reply that doesn't include a register's | |
3704 | value implies either that the register is not available, or that | |
3705 | the 'p' packet must be used. */ | |
3706 | if (buf_len < 2 * rsa->sizeof_g_packet) | |
b323314b | 3707 | { |
74ca34ce DJ |
3708 | rsa->sizeof_g_packet = buf_len / 2; |
3709 | ||
4a22f64d | 3710 | for (i = 0; i < gdbarch_num_regs (gdbarch); i++) |
b96ec7ac | 3711 | { |
74ca34ce DJ |
3712 | if (rsa->regs[i].pnum == -1) |
3713 | continue; | |
3714 | ||
3715 | if (rsa->regs[i].offset >= rsa->sizeof_g_packet) | |
3716 | rsa->regs[i].in_g_packet = 0; | |
b96ec7ac | 3717 | else |
74ca34ce | 3718 | rsa->regs[i].in_g_packet = 1; |
b96ec7ac | 3719 | } |
74ca34ce | 3720 | } |
b323314b | 3721 | |
74ca34ce | 3722 | regs = alloca (rsa->sizeof_g_packet); |
c906108c SS |
3723 | |
3724 | /* Unimplemented registers read as all bits zero. */ | |
ea9c271d | 3725 | memset (regs, 0, rsa->sizeof_g_packet); |
c906108c | 3726 | |
c906108c SS |
3727 | /* Reply describes registers byte by byte, each byte encoded as two |
3728 | hex characters. Suck them all up, then supply them to the | |
3729 | register cacheing/storage mechanism. */ | |
3730 | ||
74ca34ce | 3731 | p = rs->buf; |
ea9c271d | 3732 | for (i = 0; i < rsa->sizeof_g_packet; i++) |
c906108c | 3733 | { |
74ca34ce DJ |
3734 | if (p[0] == 0 || p[1] == 0) |
3735 | /* This shouldn't happen - we adjusted sizeof_g_packet above. */ | |
3736 | internal_error (__FILE__, __LINE__, | |
3737 | "unexpected end of 'g' packet reply"); | |
3738 | ||
c906108c | 3739 | if (p[0] == 'x' && p[1] == 'x') |
c5aa993b | 3740 | regs[i] = 0; /* 'x' */ |
c906108c SS |
3741 | else |
3742 | regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); | |
3743 | p += 2; | |
3744 | } | |
3745 | ||
ad10f812 | 3746 | { |
b323314b | 3747 | int i; |
4a22f64d | 3748 | for (i = 0; i < gdbarch_num_regs (gdbarch); i++) |
ad10f812 | 3749 | { |
ea9c271d | 3750 | struct packet_reg *r = &rsa->regs[i]; |
b323314b AC |
3751 | if (r->in_g_packet) |
3752 | { | |
74ca34ce DJ |
3753 | if (r->offset * 2 >= strlen (rs->buf)) |
3754 | /* This shouldn't happen - we adjusted in_g_packet above. */ | |
3755 | internal_error (__FILE__, __LINE__, | |
3756 | "unexpected end of 'g' packet reply"); | |
3757 | else if (rs->buf[r->offset * 2] == 'x') | |
8ccc1287 | 3758 | { |
74ca34ce | 3759 | gdb_assert (r->offset * 2 < strlen (rs->buf)); |
8ccc1287 AC |
3760 | /* The register isn't available, mark it as such (at |
3761 | the same time setting the value to zero). */ | |
56be3814 | 3762 | regcache_raw_supply (regcache, r->regnum, NULL); |
8ccc1287 AC |
3763 | } |
3764 | else | |
56be3814 | 3765 | regcache_raw_supply (regcache, r->regnum, |
8ccc1287 | 3766 | regs + r->offset); |
b323314b | 3767 | } |
ad10f812 AC |
3768 | } |
3769 | } | |
c906108c SS |
3770 | } |
3771 | ||
29709017 | 3772 | static void |
56be3814 | 3773 | fetch_registers_using_g (struct regcache *regcache) |
29709017 DJ |
3774 | { |
3775 | send_g_packet (); | |
56be3814 | 3776 | process_g_packet (regcache); |
29709017 DJ |
3777 | } |
3778 | ||
74ca34ce | 3779 | static void |
56be3814 | 3780 | remote_fetch_registers (struct regcache *regcache, int regnum) |
74ca34ce DJ |
3781 | { |
3782 | struct remote_state *rs = get_remote_state (); | |
3783 | struct remote_arch_state *rsa = get_remote_arch_state (); | |
3784 | int i; | |
3785 | ||
3786 | set_thread (PIDGET (inferior_ptid), 1); | |
3787 | ||
3788 | if (regnum >= 0) | |
3789 | { | |
3790 | struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum); | |
3791 | gdb_assert (reg != NULL); | |
3792 | ||
3793 | /* If this register might be in the 'g' packet, try that first - | |
3794 | we are likely to read more than one register. If this is the | |
3795 | first 'g' packet, we might be overly optimistic about its | |
3796 | contents, so fall back to 'p'. */ | |
3797 | if (reg->in_g_packet) | |
3798 | { | |
56be3814 | 3799 | fetch_registers_using_g (regcache); |
74ca34ce DJ |
3800 | if (reg->in_g_packet) |
3801 | return; | |
3802 | } | |
3803 | ||
56be3814 | 3804 | if (fetch_register_using_p (regcache, reg)) |
74ca34ce DJ |
3805 | return; |
3806 | ||
3807 | /* This register is not available. */ | |
56be3814 | 3808 | regcache_raw_supply (regcache, reg->regnum, NULL); |
74ca34ce DJ |
3809 | |
3810 | return; | |
3811 | } | |
3812 | ||
56be3814 | 3813 | fetch_registers_using_g (regcache); |
74ca34ce | 3814 | |
4a22f64d | 3815 | for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++) |
74ca34ce | 3816 | if (!rsa->regs[i].in_g_packet) |
56be3814 | 3817 | if (!fetch_register_using_p (regcache, &rsa->regs[i])) |
74ca34ce DJ |
3818 | { |
3819 | /* This register is not available. */ | |
56be3814 | 3820 | regcache_raw_supply (regcache, i, NULL); |
74ca34ce DJ |
3821 | } |
3822 | } | |
3823 | ||
c906108c SS |
3824 | /* Prepare to store registers. Since we may send them all (using a |
3825 | 'G' request), we have to read out the ones we don't want to change | |
3826 | first. */ | |
3827 | ||
c5aa993b | 3828 | static void |
316f2060 | 3829 | remote_prepare_to_store (struct regcache *regcache) |
c906108c | 3830 | { |
ea9c271d | 3831 | struct remote_arch_state *rsa = get_remote_arch_state (); |
cf0e1e0d | 3832 | int i; |
cfd77fa1 | 3833 | gdb_byte buf[MAX_REGISTER_SIZE]; |
cf0e1e0d | 3834 | |
c906108c | 3835 | /* Make sure the entire registers array is valid. */ |
444abaca | 3836 | switch (remote_protocol_packets[PACKET_P].support) |
5a2468f5 JM |
3837 | { |
3838 | case PACKET_DISABLE: | |
3839 | case PACKET_SUPPORT_UNKNOWN: | |
cf0e1e0d | 3840 | /* Make sure all the necessary registers are cached. */ |
4a22f64d | 3841 | for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++) |
ea9c271d | 3842 | if (rsa->regs[i].in_g_packet) |
316f2060 | 3843 | regcache_raw_read (regcache, rsa->regs[i].regnum, buf); |
5a2468f5 JM |
3844 | break; |
3845 | case PACKET_ENABLE: | |
3846 | break; | |
3847 | } | |
3848 | } | |
3849 | ||
ad10f812 | 3850 | /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF |
23860348 | 3851 | packet was not recognized. */ |
5a2468f5 JM |
3852 | |
3853 | static int | |
56be3814 | 3854 | store_register_using_P (const struct regcache *regcache, struct packet_reg *reg) |
5a2468f5 | 3855 | { |
4a22f64d | 3856 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
d01949b6 | 3857 | struct remote_state *rs = get_remote_state (); |
ea9c271d | 3858 | struct remote_arch_state *rsa = get_remote_arch_state (); |
5a2468f5 | 3859 | /* Try storing a single register. */ |
6d820c5c | 3860 | char *buf = rs->buf; |
cfd77fa1 | 3861 | gdb_byte regp[MAX_REGISTER_SIZE]; |
5a2468f5 | 3862 | char *p; |
5a2468f5 | 3863 | |
74ca34ce DJ |
3864 | if (remote_protocol_packets[PACKET_P].support == PACKET_DISABLE) |
3865 | return 0; | |
3866 | ||
3867 | if (reg->pnum == -1) | |
3868 | return 0; | |
3869 | ||
ea9c271d | 3870 | xsnprintf (buf, get_remote_packet_size (), "P%s=", phex_nz (reg->pnum, 0)); |
5a2468f5 | 3871 | p = buf + strlen (buf); |
56be3814 | 3872 | regcache_raw_collect (regcache, reg->regnum, regp); |
4a22f64d | 3873 | bin2hex (regp, p, register_size (gdbarch, reg->regnum)); |
6d820c5c | 3874 | remote_send (&rs->buf, &rs->buf_size); |
5a2468f5 | 3875 | |
74ca34ce DJ |
3876 | switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_P])) |
3877 | { | |
3878 | case PACKET_OK: | |
3879 | return 1; | |
3880 | case PACKET_ERROR: | |
3881 | error (_("Could not write register \"%s\""), | |
4a22f64d | 3882 | gdbarch_register_name (gdbarch, reg->regnum)); |
74ca34ce DJ |
3883 | case PACKET_UNKNOWN: |
3884 | return 0; | |
3885 | default: | |
3886 | internal_error (__FILE__, __LINE__, _("Bad result from packet_ok")); | |
3887 | } | |
c906108c SS |
3888 | } |
3889 | ||
23860348 MS |
3890 | /* Store register REGNUM, or all registers if REGNUM == -1, from the |
3891 | contents of the register cache buffer. FIXME: ignores errors. */ | |
c906108c SS |
3892 | |
3893 | static void | |
56be3814 | 3894 | store_registers_using_G (const struct regcache *regcache) |
c906108c | 3895 | { |
d01949b6 | 3896 | struct remote_state *rs = get_remote_state (); |
ea9c271d | 3897 | struct remote_arch_state *rsa = get_remote_arch_state (); |
cfd77fa1 | 3898 | gdb_byte *regs; |
c906108c SS |
3899 | char *p; |
3900 | ||
193cb69f AC |
3901 | /* Extract all the registers in the regcache copying them into a |
3902 | local buffer. */ | |
3903 | { | |
b323314b | 3904 | int i; |
ea9c271d DJ |
3905 | regs = alloca (rsa->sizeof_g_packet); |
3906 | memset (regs, 0, rsa->sizeof_g_packet); | |
4a22f64d | 3907 | for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++) |
193cb69f | 3908 | { |
ea9c271d | 3909 | struct packet_reg *r = &rsa->regs[i]; |
b323314b | 3910 | if (r->in_g_packet) |
56be3814 | 3911 | regcache_raw_collect (regcache, r->regnum, regs + r->offset); |
193cb69f AC |
3912 | } |
3913 | } | |
c906108c SS |
3914 | |
3915 | /* Command describes registers byte by byte, | |
3916 | each byte encoded as two hex characters. */ | |
6d820c5c | 3917 | p = rs->buf; |
193cb69f | 3918 | *p++ = 'G'; |
74ca34ce DJ |
3919 | /* remote_prepare_to_store insures that rsa->sizeof_g_packet gets |
3920 | updated. */ | |
3921 | bin2hex (regs, p, rsa->sizeof_g_packet); | |
6d820c5c | 3922 | remote_send (&rs->buf, &rs->buf_size); |
c906108c | 3923 | } |
74ca34ce DJ |
3924 | |
3925 | /* Store register REGNUM, or all registers if REGNUM == -1, from the contents | |
3926 | of the register cache buffer. FIXME: ignores errors. */ | |
3927 | ||
3928 | static void | |
56be3814 | 3929 | remote_store_registers (struct regcache *regcache, int regnum) |
74ca34ce DJ |
3930 | { |
3931 | struct remote_state *rs = get_remote_state (); | |
3932 | struct remote_arch_state *rsa = get_remote_arch_state (); | |
3933 | int i; | |
3934 | ||
3935 | set_thread (PIDGET (inferior_ptid), 1); | |
3936 | ||
3937 | if (regnum >= 0) | |
3938 | { | |
3939 | struct packet_reg *reg = packet_reg_from_regnum (rsa, regnum); | |
3940 | gdb_assert (reg != NULL); | |
3941 | ||
3942 | /* Always prefer to store registers using the 'P' packet if | |
3943 | possible; we often change only a small number of registers. | |
3944 | Sometimes we change a larger number; we'd need help from a | |
3945 | higher layer to know to use 'G'. */ | |
56be3814 | 3946 | if (store_register_using_P (regcache, reg)) |
74ca34ce DJ |
3947 | return; |
3948 | ||
3949 | /* For now, don't complain if we have no way to write the | |
3950 | register. GDB loses track of unavailable registers too | |
3951 | easily. Some day, this may be an error. We don't have | |
3952 | any way to read the register, either... */ | |
3953 | if (!reg->in_g_packet) | |
3954 | return; | |
3955 | ||
56be3814 | 3956 | store_registers_using_G (regcache); |
74ca34ce DJ |
3957 | return; |
3958 | } | |
3959 | ||
56be3814 | 3960 | store_registers_using_G (regcache); |
74ca34ce | 3961 | |
4a22f64d | 3962 | for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++) |
74ca34ce | 3963 | if (!rsa->regs[i].in_g_packet) |
56be3814 | 3964 | if (!store_register_using_P (regcache, &rsa->regs[i])) |
74ca34ce DJ |
3965 | /* See above for why we do not issue an error here. */ |
3966 | continue; | |
3967 | } | |
c906108c SS |
3968 | \f |
3969 | ||
3970 | /* Return the number of hex digits in num. */ | |
3971 | ||
3972 | static int | |
fba45db2 | 3973 | hexnumlen (ULONGEST num) |
c906108c SS |
3974 | { |
3975 | int i; | |
3976 | ||
3977 | for (i = 0; num != 0; i++) | |
3978 | num >>= 4; | |
3979 | ||
3980 | return max (i, 1); | |
3981 | } | |
3982 | ||
2df3850c | 3983 | /* Set BUF to the minimum number of hex digits representing NUM. */ |
c906108c SS |
3984 | |
3985 | static int | |
fba45db2 | 3986 | hexnumstr (char *buf, ULONGEST num) |
c906108c | 3987 | { |
c906108c | 3988 | int len = hexnumlen (num); |
2df3850c JM |
3989 | return hexnumnstr (buf, num, len); |
3990 | } | |
3991 | ||
c906108c | 3992 | |
2df3850c | 3993 | /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */ |
c906108c | 3994 | |
2df3850c | 3995 | static int |
fba45db2 | 3996 | hexnumnstr (char *buf, ULONGEST num, int width) |
2df3850c JM |
3997 | { |
3998 | int i; | |
3999 | ||
4000 | buf[width] = '\0'; | |
4001 | ||
4002 | for (i = width - 1; i >= 0; i--) | |
c906108c | 4003 | { |
c5aa993b | 4004 | buf[i] = "0123456789abcdef"[(num & 0xf)]; |
c906108c SS |
4005 | num >>= 4; |
4006 | } | |
4007 | ||
2df3850c | 4008 | return width; |
c906108c SS |
4009 | } |
4010 | ||
23860348 | 4011 | /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */ |
c906108c SS |
4012 | |
4013 | static CORE_ADDR | |
fba45db2 | 4014 | remote_address_masked (CORE_ADDR addr) |
c906108c | 4015 | { |
911c95a5 UW |
4016 | int address_size = remote_address_size; |
4017 | /* If "remoteaddresssize" was not set, default to target address size. */ | |
4018 | if (!address_size) | |
4019 | address_size = gdbarch_addr_bit (current_gdbarch); | |
4020 | ||
4021 | if (address_size > 0 | |
4022 | && address_size < (sizeof (ULONGEST) * 8)) | |
c906108c SS |
4023 | { |
4024 | /* Only create a mask when that mask can safely be constructed | |
23860348 | 4025 | in a ULONGEST variable. */ |
c906108c | 4026 | ULONGEST mask = 1; |
911c95a5 | 4027 | mask = (mask << address_size) - 1; |
c906108c SS |
4028 | addr &= mask; |
4029 | } | |
4030 | return addr; | |
4031 | } | |
4032 | ||
a31ea83d DJ |
4033 | /* Convert BUFFER, binary data at least LEN bytes long, into escaped |
4034 | binary data in OUT_BUF. Set *OUT_LEN to the length of the data | |
4035 | encoded in OUT_BUF, and return the number of bytes in OUT_BUF | |
4036 | (which may be more than *OUT_LEN due to escape characters). The | |
4037 | total number of bytes in the output buffer will be at most | |
4038 | OUT_MAXLEN. */ | |
4039 | ||
4040 | static int | |
4041 | remote_escape_output (const gdb_byte *buffer, int len, | |
4042 | gdb_byte *out_buf, int *out_len, | |
4043 | int out_maxlen) | |
4044 | { | |
4045 | int input_index, output_index; | |
4046 | ||
4047 | output_index = 0; | |
4048 | for (input_index = 0; input_index < len; input_index++) | |
4049 | { | |
4050 | gdb_byte b = buffer[input_index]; | |
4051 | ||
4052 | if (b == '$' || b == '#' || b == '}') | |
4053 | { | |
4054 | /* These must be escaped. */ | |
4055 | if (output_index + 2 > out_maxlen) | |
4056 | break; | |
4057 | out_buf[output_index++] = '}'; | |
4058 | out_buf[output_index++] = b ^ 0x20; | |
4059 | } | |
4060 | else | |
4061 | { | |
4062 | if (output_index + 1 > out_maxlen) | |
4063 | break; | |
4064 | out_buf[output_index++] = b; | |
4065 | } | |
4066 | } | |
4067 | ||
4068 | *out_len = input_index; | |
4069 | return output_index; | |
4070 | } | |
4071 | ||
0876f84a DJ |
4072 | /* Convert BUFFER, escaped data LEN bytes long, into binary data |
4073 | in OUT_BUF. Return the number of bytes written to OUT_BUF. | |
4074 | Raise an error if the total number of bytes exceeds OUT_MAXLEN. | |
4075 | ||
4076 | This function reverses remote_escape_output. It allows more | |
4077 | escaped characters than that function does, in particular because | |
4078 | '*' must be escaped to avoid the run-length encoding processing | |
4079 | in reading packets. */ | |
4080 | ||
4081 | static int | |
4082 | remote_unescape_input (const gdb_byte *buffer, int len, | |
4083 | gdb_byte *out_buf, int out_maxlen) | |
4084 | { | |
4085 | int input_index, output_index; | |
4086 | int escaped; | |
4087 | ||
4088 | output_index = 0; | |
4089 | escaped = 0; | |
4090 | for (input_index = 0; input_index < len; input_index++) | |
4091 | { | |
4092 | gdb_byte b = buffer[input_index]; | |
4093 | ||
4094 | if (output_index + 1 > out_maxlen) | |
4095 | { | |
4096 | warning (_("Received too much data from remote target;" | |
4097 | " ignoring overflow.")); | |
4098 | return output_index; | |
4099 | } | |
4100 | ||
4101 | if (escaped) | |
4102 | { | |
4103 | out_buf[output_index++] = b ^ 0x20; | |
4104 | escaped = 0; | |
4105 | } | |
4106 | else if (b == '}') | |
4107 | escaped = 1; | |
4108 | else | |
4109 | out_buf[output_index++] = b; | |
4110 | } | |
4111 | ||
4112 | if (escaped) | |
4113 | error (_("Unmatched escape character in target response.")); | |
4114 | ||
4115 | return output_index; | |
4116 | } | |
4117 | ||
c906108c SS |
4118 | /* Determine whether the remote target supports binary downloading. |
4119 | This is accomplished by sending a no-op memory write of zero length | |
4120 | to the target at the specified address. It does not suffice to send | |
23860348 MS |
4121 | the whole packet, since many stubs strip the eighth bit and |
4122 | subsequently compute a wrong checksum, which causes real havoc with | |
4123 | remote_write_bytes. | |
7a292a7a | 4124 | |
96baa820 JM |
4125 | NOTE: This can still lose if the serial line is not eight-bit |
4126 | clean. In cases like this, the user should clear "remote | |
23860348 | 4127 | X-packet". */ |
96baa820 | 4128 | |
c906108c | 4129 | static void |
fba45db2 | 4130 | check_binary_download (CORE_ADDR addr) |
c906108c | 4131 | { |
d01949b6 | 4132 | struct remote_state *rs = get_remote_state (); |
24b06219 | 4133 | |
444abaca | 4134 | switch (remote_protocol_packets[PACKET_X].support) |
c906108c | 4135 | { |
96baa820 JM |
4136 | case PACKET_DISABLE: |
4137 | break; | |
4138 | case PACKET_ENABLE: | |
4139 | break; | |
4140 | case PACKET_SUPPORT_UNKNOWN: | |
4141 | { | |
96baa820 | 4142 | char *p; |
802188a7 | 4143 | |
2e9f7625 | 4144 | p = rs->buf; |
96baa820 JM |
4145 | *p++ = 'X'; |
4146 | p += hexnumstr (p, (ULONGEST) addr); | |
4147 | *p++ = ','; | |
4148 | p += hexnumstr (p, (ULONGEST) 0); | |
4149 | *p++ = ':'; | |
4150 | *p = '\0'; | |
802188a7 | 4151 | |
2e9f7625 | 4152 | putpkt_binary (rs->buf, (int) (p - rs->buf)); |
6d820c5c | 4153 | getpkt (&rs->buf, &rs->buf_size, 0); |
c906108c | 4154 | |
2e9f7625 | 4155 | if (rs->buf[0] == '\0') |
96baa820 JM |
4156 | { |
4157 | if (remote_debug) | |
4158 | fprintf_unfiltered (gdb_stdlog, | |
4159 | "binary downloading NOT suppported by target\n"); | |
444abaca | 4160 | remote_protocol_packets[PACKET_X].support = PACKET_DISABLE; |
96baa820 JM |
4161 | } |
4162 | else | |
4163 | { | |
4164 | if (remote_debug) | |
4165 | fprintf_unfiltered (gdb_stdlog, | |
4166 | "binary downloading suppported by target\n"); | |
444abaca | 4167 | remote_protocol_packets[PACKET_X].support = PACKET_ENABLE; |
96baa820 JM |
4168 | } |
4169 | break; | |
4170 | } | |
c906108c SS |
4171 | } |
4172 | } | |
4173 | ||
4174 | /* Write memory data directly to the remote machine. | |
4175 | This does not inform the data cache; the data cache uses this. | |
a76d924d | 4176 | HEADER is the starting part of the packet. |
c906108c SS |
4177 | MEMADDR is the address in the remote memory space. |
4178 | MYADDR is the address of the buffer in our space. | |
4179 | LEN is the number of bytes. | |
a76d924d DJ |
4180 | PACKET_FORMAT should be either 'X' or 'M', and indicates if we |
4181 | should send data as binary ('X'), or hex-encoded ('M'). | |
4182 | ||
4183 | The function creates packet of the form | |
4184 | <HEADER><ADDRESS>,<LENGTH>:<DATA> | |
4185 | ||
4186 | where encoding of <DATA> is termined by PACKET_FORMAT. | |
4187 | ||
4188 | If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma | |
4189 | are omitted. | |
4190 | ||
4191 | Returns the number of bytes transferred, or 0 (setting errno) for | |
23860348 | 4192 | error. Only transfer a single packet. */ |
c906108c | 4193 | |
a76d924d DJ |
4194 | static int |
4195 | remote_write_bytes_aux (const char *header, CORE_ADDR memaddr, | |
4196 | const gdb_byte *myaddr, int len, | |
4197 | char packet_format, int use_length) | |
c906108c | 4198 | { |
6d820c5c | 4199 | struct remote_state *rs = get_remote_state (); |
cfd77fa1 | 4200 | char *p; |
a76d924d DJ |
4201 | char *plen = NULL; |
4202 | int plenlen = 0; | |
917317f4 JM |
4203 | int todo; |
4204 | int nr_bytes; | |
a257b5bb | 4205 | int payload_size; |
6765f3e5 | 4206 | int payload_length; |
a76d924d DJ |
4207 | int header_length; |
4208 | ||
4209 | if (packet_format != 'X' && packet_format != 'M') | |
4210 | internal_error (__FILE__, __LINE__, | |
4211 | "remote_write_bytes_aux: bad packet format"); | |
c906108c | 4212 | |
b2182ed2 DJ |
4213 | if (len <= 0) |
4214 | return 0; | |
4215 | ||
3de11b2e | 4216 | payload_size = get_memory_write_packet_size (); |
2bc416ba | 4217 | |
6d820c5c DJ |
4218 | /* The packet buffer will be large enough for the payload; |
4219 | get_memory_packet_size ensures this. */ | |
a76d924d | 4220 | rs->buf[0] = '\0'; |
c906108c | 4221 | |
a257b5bb | 4222 | /* Compute the size of the actual payload by subtracting out the |
3de11b2e NS |
4223 | packet header and footer overhead: "$M<memaddr>,<len>:...#nn". |
4224 | */ | |
a76d924d DJ |
4225 | payload_size -= strlen ("$,:#NN"); |
4226 | if (!use_length) | |
4227 | /* The comma won't be used. */ | |
4228 | payload_size += 1; | |
4229 | header_length = strlen (header); | |
4230 | payload_size -= header_length; | |
3de11b2e | 4231 | payload_size -= hexnumlen (memaddr); |
c906108c | 4232 | |
a76d924d | 4233 | /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */ |
917317f4 | 4234 | |
a76d924d DJ |
4235 | strcat (rs->buf, header); |
4236 | p = rs->buf + strlen (header); | |
4237 | ||
4238 | /* Compute a best guess of the number of bytes actually transfered. */ | |
4239 | if (packet_format == 'X') | |
c906108c | 4240 | { |
23860348 | 4241 | /* Best guess at number of bytes that will fit. */ |
a257b5bb | 4242 | todo = min (len, payload_size); |
a76d924d DJ |
4243 | if (use_length) |
4244 | payload_size -= hexnumlen (todo); | |
3de11b2e | 4245 | todo = min (todo, payload_size); |
a76d924d DJ |
4246 | } |
4247 | else | |
4248 | { | |
23860348 | 4249 | /* Num bytes that will fit. */ |
a257b5bb | 4250 | todo = min (len, payload_size / 2); |
a76d924d DJ |
4251 | if (use_length) |
4252 | payload_size -= hexnumlen (todo); | |
3de11b2e | 4253 | todo = min (todo, payload_size / 2); |
917317f4 | 4254 | } |
a76d924d | 4255 | |
3de11b2e NS |
4256 | if (todo <= 0) |
4257 | internal_error (__FILE__, __LINE__, | |
4258 | _("minumum packet size too small to write data")); | |
802188a7 | 4259 | |
6765f3e5 DJ |
4260 | /* If we already need another packet, then try to align the end |
4261 | of this packet to a useful boundary. */ | |
4262 | if (todo > 2 * REMOTE_ALIGN_WRITES && todo < len) | |
4263 | todo = ((memaddr + todo) & ~(REMOTE_ALIGN_WRITES - 1)) - memaddr; | |
4264 | ||
a257b5bb | 4265 | /* Append "<memaddr>". */ |
917317f4 JM |
4266 | memaddr = remote_address_masked (memaddr); |
4267 | p += hexnumstr (p, (ULONGEST) memaddr); | |
a257b5bb | 4268 | |
a76d924d DJ |
4269 | if (use_length) |
4270 | { | |
4271 | /* Append ",". */ | |
4272 | *p++ = ','; | |
802188a7 | 4273 | |
a76d924d DJ |
4274 | /* Append <len>. Retain the location/size of <len>. It may need to |
4275 | be adjusted once the packet body has been created. */ | |
4276 | plen = p; | |
4277 | plenlen = hexnumstr (p, (ULONGEST) todo); | |
4278 | p += plenlen; | |
4279 | } | |
a257b5bb AC |
4280 | |
4281 | /* Append ":". */ | |
917317f4 JM |
4282 | *p++ = ':'; |
4283 | *p = '\0'; | |
802188a7 | 4284 | |
a257b5bb | 4285 | /* Append the packet body. */ |
a76d924d | 4286 | if (packet_format == 'X') |
917317f4 | 4287 | { |
917317f4 JM |
4288 | /* Binary mode. Send target system values byte by byte, in |
4289 | increasing byte addresses. Only escape certain critical | |
4290 | characters. */ | |
6765f3e5 DJ |
4291 | payload_length = remote_escape_output (myaddr, todo, p, &nr_bytes, |
4292 | payload_size); | |
4293 | ||
4294 | /* If not all TODO bytes fit, then we'll need another packet. Make | |
9b7194bc DJ |
4295 | a second try to keep the end of the packet aligned. Don't do |
4296 | this if the packet is tiny. */ | |
4297 | if (nr_bytes < todo && nr_bytes > 2 * REMOTE_ALIGN_WRITES) | |
6765f3e5 DJ |
4298 | { |
4299 | int new_nr_bytes; | |
4300 | ||
4301 | new_nr_bytes = (((memaddr + nr_bytes) & ~(REMOTE_ALIGN_WRITES - 1)) | |
4302 | - memaddr); | |
4303 | if (new_nr_bytes != nr_bytes) | |
4304 | payload_length = remote_escape_output (myaddr, new_nr_bytes, | |
4305 | p, &nr_bytes, | |
4306 | payload_size); | |
4307 | } | |
4308 | ||
4309 | p += payload_length; | |
a76d924d | 4310 | if (use_length && nr_bytes < todo) |
c906108c | 4311 | { |
802188a7 | 4312 | /* Escape chars have filled up the buffer prematurely, |
917317f4 JM |
4313 | and we have actually sent fewer bytes than planned. |
4314 | Fix-up the length field of the packet. Use the same | |
4315 | number of characters as before. */ | |
917317f4 JM |
4316 | plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen); |
4317 | *plen = ':'; /* overwrite \0 from hexnumnstr() */ | |
c906108c | 4318 | } |
a76d924d DJ |
4319 | } |
4320 | else | |
4321 | { | |
917317f4 JM |
4322 | /* Normal mode: Send target system values byte by byte, in |
4323 | increasing byte addresses. Each byte is encoded as a two hex | |
4324 | value. */ | |
2644f393 | 4325 | nr_bytes = bin2hex (myaddr, p, todo); |
aa6c0017 | 4326 | p += 2 * nr_bytes; |
c906108c | 4327 | } |
802188a7 | 4328 | |
2e9f7625 | 4329 | putpkt_binary (rs->buf, (int) (p - rs->buf)); |
6d820c5c | 4330 | getpkt (&rs->buf, &rs->buf_size, 0); |
802188a7 | 4331 | |
2e9f7625 | 4332 | if (rs->buf[0] == 'E') |
917317f4 JM |
4333 | { |
4334 | /* There is no correspondance between what the remote protocol | |
4335 | uses for errors and errno codes. We would like a cleaner way | |
4336 | of representing errors (big enough to include errno codes, | |
4337 | bfd_error codes, and others). But for now just return EIO. */ | |
4338 | errno = EIO; | |
4339 | return 0; | |
4340 | } | |
802188a7 | 4341 | |
23860348 MS |
4342 | /* Return NR_BYTES, not TODO, in case escape chars caused us to send |
4343 | fewer bytes than we'd planned. */ | |
917317f4 | 4344 | return nr_bytes; |
c906108c SS |
4345 | } |
4346 | ||
a76d924d DJ |
4347 | /* Write memory data directly to the remote machine. |
4348 | This does not inform the data cache; the data cache uses this. | |
4349 | MEMADDR is the address in the remote memory space. | |
4350 | MYADDR is the address of the buffer in our space. | |
4351 | LEN is the number of bytes. | |
4352 | ||
4353 | Returns number of bytes transferred, or 0 (setting errno) for | |
4354 | error. Only transfer a single packet. */ | |
4355 | ||
4356 | int | |
4357 | remote_write_bytes (CORE_ADDR memaddr, const gdb_byte *myaddr, int len) | |
4358 | { | |
4359 | char *packet_format = 0; | |
4360 | ||
4361 | /* Check whether the target supports binary download. */ | |
4362 | check_binary_download (memaddr); | |
4363 | ||
4364 | switch (remote_protocol_packets[PACKET_X].support) | |
4365 | { | |
4366 | case PACKET_ENABLE: | |
4367 | packet_format = "X"; | |
4368 | break; | |
4369 | case PACKET_DISABLE: | |
4370 | packet_format = "M"; | |
4371 | break; | |
4372 | case PACKET_SUPPORT_UNKNOWN: | |
4373 | internal_error (__FILE__, __LINE__, | |
4374 | _("remote_write_bytes: bad internal state")); | |
4375 | default: | |
4376 | internal_error (__FILE__, __LINE__, _("bad switch")); | |
4377 | } | |
4378 | ||
4379 | return remote_write_bytes_aux (packet_format, | |
4380 | memaddr, myaddr, len, packet_format[0], 1); | |
4381 | } | |
4382 | ||
c906108c SS |
4383 | /* Read memory data directly from the remote machine. |
4384 | This does not use the data cache; the data cache uses this. | |
4385 | MEMADDR is the address in the remote memory space. | |
4386 | MYADDR is the address of the buffer in our space. | |
4387 | LEN is the number of bytes. | |
4388 | ||
4389 | Returns number of bytes transferred, or 0 for error. */ | |
4390 | ||
917317f4 JM |
4391 | /* NOTE: cagney/1999-10-18: This function (and its siblings in other |
4392 | remote targets) shouldn't attempt to read the entire buffer. | |
4393 | Instead it should read a single packet worth of data and then | |
4394 | return the byte size of that packet to the caller. The caller (its | |
4395 | caller and its callers caller ;-) already contains code for | |
23860348 | 4396 | handling partial reads. */ |
917317f4 | 4397 | |
449092f6 | 4398 | int |
cfd77fa1 | 4399 | remote_read_bytes (CORE_ADDR memaddr, gdb_byte *myaddr, int len) |
c906108c | 4400 | { |
6d820c5c | 4401 | struct remote_state *rs = get_remote_state (); |
23860348 | 4402 | int max_buf_size; /* Max size of packet output buffer. */ |
c906108c SS |
4403 | int origlen; |
4404 | ||
b2182ed2 DJ |
4405 | if (len <= 0) |
4406 | return 0; | |
4407 | ||
11cf8741 | 4408 | max_buf_size = get_memory_read_packet_size (); |
6d820c5c DJ |
4409 | /* The packet buffer will be large enough for the payload; |
4410 | get_memory_packet_size ensures this. */ | |
c906108c SS |
4411 | |
4412 | origlen = len; | |
4413 | while (len > 0) | |
4414 | { | |
c906108c SS |
4415 | char *p; |
4416 | int todo; | |
4417 | int i; | |
4418 | ||
c5aa993b | 4419 | todo = min (len, max_buf_size / 2); /* num bytes that will fit */ |
c906108c SS |
4420 | |
4421 | /* construct "m"<memaddr>","<len>" */ | |
2e9f7625 | 4422 | /* sprintf (rs->buf, "m%lx,%x", (unsigned long) memaddr, todo); */ |
c906108c | 4423 | memaddr = remote_address_masked (memaddr); |
2e9f7625 | 4424 | p = rs->buf; |
c906108c SS |
4425 | *p++ = 'm'; |
4426 | p += hexnumstr (p, (ULONGEST) memaddr); | |
4427 | *p++ = ','; | |
4428 | p += hexnumstr (p, (ULONGEST) todo); | |
4429 | *p = '\0'; | |
4430 | ||
2e9f7625 | 4431 | putpkt (rs->buf); |
6d820c5c | 4432 | getpkt (&rs->buf, &rs->buf_size, 0); |
c906108c | 4433 | |
2e9f7625 DJ |
4434 | if (rs->buf[0] == 'E' |
4435 | && isxdigit (rs->buf[1]) && isxdigit (rs->buf[2]) | |
4436 | && rs->buf[3] == '\0') | |
c906108c | 4437 | { |
23860348 MS |
4438 | /* There is no correspondance between what the remote |
4439 | protocol uses for errors and errno codes. We would like | |
4440 | a cleaner way of representing errors (big enough to | |
4441 | include errno codes, bfd_error codes, and others). But | |
4442 | for now just return EIO. */ | |
c906108c SS |
4443 | errno = EIO; |
4444 | return 0; | |
4445 | } | |
4446 | ||
c5aa993b JM |
4447 | /* Reply describes memory byte by byte, |
4448 | each byte encoded as two hex characters. */ | |
c906108c | 4449 | |
2e9f7625 | 4450 | p = rs->buf; |
30559e10 | 4451 | if ((i = hex2bin (p, myaddr, todo)) < todo) |
c906108c | 4452 | { |
30559e10 | 4453 | /* Reply is short. This means that we were able to read |
23860348 | 4454 | only part of what we wanted to. */ |
30559e10 | 4455 | return i + (origlen - len); |
c906108c SS |
4456 | } |
4457 | myaddr += todo; | |
4458 | memaddr += todo; | |
4459 | len -= todo; | |
4460 | } | |
4461 | return origlen; | |
4462 | } | |
4463 | \f | |
4464 | /* Read or write LEN bytes from inferior memory at MEMADDR, | |
23860348 MS |
4465 | transferring to or from debugger address BUFFER. Write to inferior |
4466 | if SHOULD_WRITE is nonzero. Returns length of data written or | |
4467 | read; 0 for error. TARGET is unused. */ | |
392a587b | 4468 | |
c906108c | 4469 | static int |
961cb7b5 | 4470 | remote_xfer_memory (CORE_ADDR mem_addr, gdb_byte *buffer, int mem_len, |
0a65a603 | 4471 | int should_write, struct mem_attrib *attrib, |
29e57380 | 4472 | struct target_ops *target) |
c906108c | 4473 | { |
4930751a C |
4474 | int res; |
4475 | ||
4930751a | 4476 | if (should_write) |
b2182ed2 | 4477 | res = remote_write_bytes (mem_addr, buffer, mem_len); |
4930751a | 4478 | else |
b2182ed2 | 4479 | res = remote_read_bytes (mem_addr, buffer, mem_len); |
4930751a C |
4480 | |
4481 | return res; | |
c906108c SS |
4482 | } |
4483 | ||
a76d924d DJ |
4484 | /* Sends a packet with content determined by the printf format string |
4485 | FORMAT and the remaining arguments, then gets the reply. Returns | |
4486 | whether the packet was a success, a failure, or unknown. */ | |
4487 | ||
4488 | enum packet_result | |
4489 | remote_send_printf (const char *format, ...) | |
4490 | { | |
4491 | struct remote_state *rs = get_remote_state (); | |
4492 | int max_size = get_remote_packet_size (); | |
4493 | ||
4494 | va_list ap; | |
4495 | va_start (ap, format); | |
4496 | ||
4497 | rs->buf[0] = '\0'; | |
4498 | if (vsnprintf (rs->buf, max_size, format, ap) >= max_size) | |
4499 | internal_error (__FILE__, __LINE__, "Too long remote packet."); | |
4500 | ||
4501 | if (putpkt (rs->buf) < 0) | |
4502 | error (_("Communication problem with target.")); | |
4503 | ||
4504 | rs->buf[0] = '\0'; | |
4505 | getpkt (&rs->buf, &rs->buf_size, 0); | |
4506 | ||
4507 | return packet_check_result (rs->buf); | |
4508 | } | |
4509 | ||
4510 | static void | |
4511 | restore_remote_timeout (void *p) | |
4512 | { | |
4513 | int value = *(int *)p; | |
4514 | remote_timeout = value; | |
4515 | } | |
4516 | ||
4517 | /* Flash writing can take quite some time. We'll set | |
4518 | effectively infinite timeout for flash operations. | |
4519 | In future, we'll need to decide on a better approach. */ | |
4520 | static const int remote_flash_timeout = 1000; | |
4521 | ||
4522 | static void | |
4523 | remote_flash_erase (struct target_ops *ops, | |
4524 | ULONGEST address, LONGEST length) | |
4525 | { | |
4526 | int saved_remote_timeout = remote_timeout; | |
4527 | enum packet_result ret; | |
4528 | ||
4529 | struct cleanup *back_to = make_cleanup (restore_remote_timeout, | |
4530 | &saved_remote_timeout); | |
4531 | remote_timeout = remote_flash_timeout; | |
4532 | ||
4533 | ret = remote_send_printf ("vFlashErase:%s,%s", | |
4534 | paddr (address), | |
4535 | phex (length, 4)); | |
4536 | switch (ret) | |
4537 | { | |
4538 | case PACKET_UNKNOWN: | |
4539 | error (_("Remote target does not support flash erase")); | |
4540 | case PACKET_ERROR: | |
4541 | error (_("Error erasing flash with vFlashErase packet")); | |
4542 | default: | |
4543 | break; | |
4544 | } | |
4545 | ||
4546 | do_cleanups (back_to); | |
4547 | } | |
4548 | ||
4549 | static LONGEST | |
4550 | remote_flash_write (struct target_ops *ops, | |
4551 | ULONGEST address, LONGEST length, | |
4552 | const gdb_byte *data) | |
4553 | { | |
4554 | int saved_remote_timeout = remote_timeout; | |
4555 | int ret; | |
4556 | struct cleanup *back_to = make_cleanup (restore_remote_timeout, | |
4557 | &saved_remote_timeout); | |
4558 | ||
4559 | remote_timeout = remote_flash_timeout; | |
4560 | ret = remote_write_bytes_aux ("vFlashWrite:", address, data, length, 'X', 0); | |
4561 | do_cleanups (back_to); | |
4562 | ||
4563 | return ret; | |
4564 | } | |
4565 | ||
4566 | static void | |
4567 | remote_flash_done (struct target_ops *ops) | |
4568 | { | |
4569 | int saved_remote_timeout = remote_timeout; | |
4570 | int ret; | |
4571 | struct cleanup *back_to = make_cleanup (restore_remote_timeout, | |
4572 | &saved_remote_timeout); | |
4573 | ||
4574 | remote_timeout = remote_flash_timeout; | |
4575 | ret = remote_send_printf ("vFlashDone"); | |
4576 | do_cleanups (back_to); | |
4577 | ||
4578 | switch (ret) | |
4579 | { | |
4580 | case PACKET_UNKNOWN: | |
4581 | error (_("Remote target does not support vFlashDone")); | |
4582 | case PACKET_ERROR: | |
4583 | error (_("Error finishing flash operation")); | |
4584 | default: | |
4585 | break; | |
4586 | } | |
4587 | } | |
4588 | ||
c906108c | 4589 | static void |
fba45db2 | 4590 | remote_files_info (struct target_ops *ignore) |
c906108c SS |
4591 | { |
4592 | puts_filtered ("Debugging a target over a serial line.\n"); | |
4593 | } | |
4594 | \f | |
4595 | /* Stuff for dealing with the packets which are part of this protocol. | |
4596 | See comment at top of file for details. */ | |
4597 | ||
0876f84a | 4598 | /* Read a single character from the remote end. */ |
c906108c SS |
4599 | |
4600 | static int | |
fba45db2 | 4601 | readchar (int timeout) |
c906108c SS |
4602 | { |
4603 | int ch; | |
4604 | ||
2cd58942 | 4605 | ch = serial_readchar (remote_desc, timeout); |
c906108c | 4606 | |
2acceee2 | 4607 | if (ch >= 0) |
0876f84a | 4608 | return ch; |
2acceee2 JM |
4609 | |
4610 | switch ((enum serial_rc) ch) | |
c906108c SS |
4611 | { |
4612 | case SERIAL_EOF: | |
2acceee2 | 4613 | target_mourn_inferior (); |
8a3fe4f8 | 4614 | error (_("Remote connection closed")); |
2acceee2 | 4615 | /* no return */ |
c906108c | 4616 | case SERIAL_ERROR: |
e2e0b3e5 | 4617 | perror_with_name (_("Remote communication error")); |
2acceee2 | 4618 | /* no return */ |
c906108c | 4619 | case SERIAL_TIMEOUT: |
2acceee2 | 4620 | break; |
c906108c | 4621 | } |
2acceee2 | 4622 | return ch; |
c906108c SS |
4623 | } |
4624 | ||
6d820c5c DJ |
4625 | /* Send the command in *BUF to the remote machine, and read the reply |
4626 | into *BUF. Report an error if we get an error reply. Resize | |
4627 | *BUF using xrealloc if necessary to hold the result, and update | |
4628 | *SIZEOF_BUF. */ | |
c906108c SS |
4629 | |
4630 | static void | |
6d820c5c DJ |
4631 | remote_send (char **buf, |
4632 | long *sizeof_buf) | |
c906108c | 4633 | { |
6d820c5c | 4634 | putpkt (*buf); |
c2d11a7d | 4635 | getpkt (buf, sizeof_buf, 0); |
c906108c | 4636 | |
6d820c5c DJ |
4637 | if ((*buf)[0] == 'E') |
4638 | error (_("Remote failure reply: %s"), *buf); | |
c906108c SS |
4639 | } |
4640 | ||
4641 | /* Display a null-terminated packet on stdout, for debugging, using C | |
4642 | string notation. */ | |
4643 | ||
4644 | static void | |
fba45db2 | 4645 | print_packet (char *buf) |
c906108c SS |
4646 | { |
4647 | puts_filtered ("\""); | |
43e526b9 | 4648 | fputstr_filtered (buf, '"', gdb_stdout); |
c906108c SS |
4649 | puts_filtered ("\""); |
4650 | } | |
4651 | ||
4652 | int | |
fba45db2 | 4653 | putpkt (char *buf) |
c906108c SS |
4654 | { |
4655 | return putpkt_binary (buf, strlen (buf)); | |
4656 | } | |
4657 | ||
4658 | /* Send a packet to the remote machine, with error checking. The data | |
23860348 | 4659 | of the packet is in BUF. The string in BUF can be at most |
ea9c271d | 4660 | get_remote_packet_size () - 5 to account for the $, # and checksum, |
23860348 MS |
4661 | and for a possible /0 if we are debugging (remote_debug) and want |
4662 | to print the sent packet as a string. */ | |
c906108c SS |
4663 | |
4664 | static int | |
fba45db2 | 4665 | putpkt_binary (char *buf, int cnt) |
c906108c | 4666 | { |
2d717e4f | 4667 | struct remote_state *rs = get_remote_state (); |
c906108c SS |
4668 | int i; |
4669 | unsigned char csum = 0; | |
11cf8741 | 4670 | char *buf2 = alloca (cnt + 6); |
085dd6e6 | 4671 | |
c906108c SS |
4672 | int ch; |
4673 | int tcount = 0; | |
4674 | char *p; | |
4675 | ||
2d717e4f DJ |
4676 | /* We're sending out a new packet. Make sure we don't look at a |
4677 | stale cached response. */ | |
4678 | rs->cached_wait_status = 0; | |
4679 | ||
c906108c SS |
4680 | /* Copy the packet into buffer BUF2, encapsulating it |
4681 | and giving it a checksum. */ | |
4682 | ||
c906108c SS |
4683 | p = buf2; |
4684 | *p++ = '$'; | |
4685 | ||
4686 | for (i = 0; i < cnt; i++) | |
4687 | { | |
4688 | csum += buf[i]; | |
4689 | *p++ = buf[i]; | |
4690 | } | |
4691 | *p++ = '#'; | |
4692 | *p++ = tohex ((csum >> 4) & 0xf); | |
4693 | *p++ = tohex (csum & 0xf); | |
4694 | ||
4695 | /* Send it over and over until we get a positive ack. */ | |
4696 | ||
4697 | while (1) | |
4698 | { | |
4699 | int started_error_output = 0; | |
4700 | ||
4701 | if (remote_debug) | |
4702 | { | |
4703 | *p = '\0'; | |
43e526b9 JM |
4704 | fprintf_unfiltered (gdb_stdlog, "Sending packet: "); |
4705 | fputstrn_unfiltered (buf2, p - buf2, 0, gdb_stdlog); | |
d4f3574e | 4706 | fprintf_unfiltered (gdb_stdlog, "..."); |
0f71a2f6 | 4707 | gdb_flush (gdb_stdlog); |
c906108c | 4708 | } |
2cd58942 | 4709 | if (serial_write (remote_desc, buf2, p - buf2)) |
e2e0b3e5 | 4710 | perror_with_name (_("putpkt: write failed")); |
c906108c | 4711 | |
23860348 | 4712 | /* Read until either a timeout occurs (-2) or '+' is read. */ |
c906108c SS |
4713 | while (1) |
4714 | { | |
4715 | ch = readchar (remote_timeout); | |
4716 | ||
c5aa993b | 4717 | if (remote_debug) |
c906108c SS |
4718 | { |
4719 | switch (ch) | |
4720 | { | |
4721 | case '+': | |
1216fa2c | 4722 | case '-': |
c906108c SS |
4723 | case SERIAL_TIMEOUT: |
4724 | case '$': | |
4725 | if (started_error_output) | |
4726 | { | |
4727 | putchar_unfiltered ('\n'); | |
4728 | started_error_output = 0; | |
4729 | } | |
4730 | } | |
4731 | } | |
4732 | ||
4733 | switch (ch) | |
4734 | { | |
4735 | case '+': | |
4736 | if (remote_debug) | |
0f71a2f6 | 4737 | fprintf_unfiltered (gdb_stdlog, "Ack\n"); |
c906108c | 4738 | return 1; |
1216fa2c AC |
4739 | case '-': |
4740 | if (remote_debug) | |
4741 | fprintf_unfiltered (gdb_stdlog, "Nak\n"); | |
c906108c | 4742 | case SERIAL_TIMEOUT: |
c5aa993b | 4743 | tcount++; |
c906108c SS |
4744 | if (tcount > 3) |
4745 | return 0; | |
23860348 | 4746 | break; /* Retransmit buffer. */ |
c906108c SS |
4747 | case '$': |
4748 | { | |
40e3f985 | 4749 | if (remote_debug) |
2bc416ba | 4750 | fprintf_unfiltered (gdb_stdlog, |
23860348 | 4751 | "Packet instead of Ack, ignoring it\n"); |
d6f7abdf AC |
4752 | /* It's probably an old response sent because an ACK |
4753 | was lost. Gobble up the packet and ack it so it | |
4754 | doesn't get retransmitted when we resend this | |
4755 | packet. */ | |
6d820c5c | 4756 | skip_frame (); |
d6f7abdf | 4757 | serial_write (remote_desc, "+", 1); |
23860348 | 4758 | continue; /* Now, go look for +. */ |
c906108c SS |
4759 | } |
4760 | default: | |
4761 | if (remote_debug) | |
4762 | { | |
4763 | if (!started_error_output) | |
4764 | { | |
4765 | started_error_output = 1; | |
0f71a2f6 | 4766 | fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: "); |
c906108c | 4767 | } |
0f71a2f6 | 4768 | fputc_unfiltered (ch & 0177, gdb_stdlog); |
c906108c SS |
4769 | } |
4770 | continue; | |
4771 | } | |
23860348 | 4772 | break; /* Here to retransmit. */ |
c906108c SS |
4773 | } |
4774 | ||
4775 | #if 0 | |
4776 | /* This is wrong. If doing a long backtrace, the user should be | |
c5aa993b JM |
4777 | able to get out next time we call QUIT, without anything as |
4778 | violent as interrupt_query. If we want to provide a way out of | |
4779 | here without getting to the next QUIT, it should be based on | |
4780 | hitting ^C twice as in remote_wait. */ | |
c906108c SS |
4781 | if (quit_flag) |
4782 | { | |
4783 | quit_flag = 0; | |
4784 | interrupt_query (); | |
4785 | } | |
4786 | #endif | |
4787 | } | |
4788 | } | |
4789 | ||
6d820c5c DJ |
4790 | /* Come here after finding the start of a frame when we expected an |
4791 | ack. Do our best to discard the rest of this packet. */ | |
4792 | ||
4793 | static void | |
4794 | skip_frame (void) | |
4795 | { | |
4796 | int c; | |
4797 | ||
4798 | while (1) | |
4799 | { | |
4800 | c = readchar (remote_timeout); | |
4801 | switch (c) | |
4802 | { | |
4803 | case SERIAL_TIMEOUT: | |
4804 | /* Nothing we can do. */ | |
4805 | return; | |
4806 | case '#': | |
4807 | /* Discard the two bytes of checksum and stop. */ | |
4808 | c = readchar (remote_timeout); | |
4809 | if (c >= 0) | |
4810 | c = readchar (remote_timeout); | |
4811 | ||
4812 | return; | |
4813 | case '*': /* Run length encoding. */ | |
4814 | /* Discard the repeat count. */ | |
4815 | c = readchar (remote_timeout); | |
4816 | if (c < 0) | |
4817 | return; | |
4818 | break; | |
4819 | default: | |
4820 | /* A regular character. */ | |
4821 | break; | |
4822 | } | |
4823 | } | |
4824 | } | |
4825 | ||
c906108c | 4826 | /* Come here after finding the start of the frame. Collect the rest |
6d820c5c DJ |
4827 | into *BUF, verifying the checksum, length, and handling run-length |
4828 | compression. NUL terminate the buffer. If there is not enough room, | |
4829 | expand *BUF using xrealloc. | |
c906108c | 4830 | |
c2d11a7d JM |
4831 | Returns -1 on error, number of characters in buffer (ignoring the |
4832 | trailing NULL) on success. (could be extended to return one of the | |
23860348 | 4833 | SERIAL status indications). */ |
c2d11a7d JM |
4834 | |
4835 | static long | |
6d820c5c DJ |
4836 | read_frame (char **buf_p, |
4837 | long *sizeof_buf) | |
c906108c SS |
4838 | { |
4839 | unsigned char csum; | |
c2d11a7d | 4840 | long bc; |
c906108c | 4841 | int c; |
6d820c5c | 4842 | char *buf = *buf_p; |
c906108c SS |
4843 | |
4844 | csum = 0; | |
c2d11a7d | 4845 | bc = 0; |
c906108c SS |
4846 | |
4847 | while (1) | |
4848 | { | |
4849 | c = readchar (remote_timeout); | |
c906108c SS |
4850 | switch (c) |
4851 | { | |
4852 | case SERIAL_TIMEOUT: | |
4853 | if (remote_debug) | |
0f71a2f6 | 4854 | fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog); |
c2d11a7d | 4855 | return -1; |
c906108c SS |
4856 | case '$': |
4857 | if (remote_debug) | |
0f71a2f6 JM |
4858 | fputs_filtered ("Saw new packet start in middle of old one\n", |
4859 | gdb_stdlog); | |
23860348 | 4860 | return -1; /* Start a new packet, count retries. */ |
c906108c SS |
4861 | case '#': |
4862 | { | |
4863 | unsigned char pktcsum; | |
e1b09194 AC |
4864 | int check_0 = 0; |
4865 | int check_1 = 0; | |
c906108c | 4866 | |
c2d11a7d | 4867 | buf[bc] = '\0'; |
c906108c | 4868 | |
e1b09194 AC |
4869 | check_0 = readchar (remote_timeout); |
4870 | if (check_0 >= 0) | |
4871 | check_1 = readchar (remote_timeout); | |
802188a7 | 4872 | |
e1b09194 AC |
4873 | if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT) |
4874 | { | |
4875 | if (remote_debug) | |
2bc416ba | 4876 | fputs_filtered ("Timeout in checksum, retrying\n", |
23860348 | 4877 | gdb_stdlog); |
e1b09194 AC |
4878 | return -1; |
4879 | } | |
4880 | else if (check_0 < 0 || check_1 < 0) | |
40e3f985 FN |
4881 | { |
4882 | if (remote_debug) | |
2bc416ba | 4883 | fputs_filtered ("Communication error in checksum\n", |
23860348 | 4884 | gdb_stdlog); |
40e3f985 FN |
4885 | return -1; |
4886 | } | |
c906108c | 4887 | |
e1b09194 | 4888 | pktcsum = (fromhex (check_0) << 4) | fromhex (check_1); |
c906108c | 4889 | if (csum == pktcsum) |
c2d11a7d | 4890 | return bc; |
c906108c | 4891 | |
c5aa993b | 4892 | if (remote_debug) |
c906108c | 4893 | { |
0f71a2f6 | 4894 | fprintf_filtered (gdb_stdlog, |
c5aa993b | 4895 | "Bad checksum, sentsum=0x%x, csum=0x%x, buf=", |
0f71a2f6 | 4896 | pktcsum, csum); |
0876f84a | 4897 | fputstrn_filtered (buf, bc, 0, gdb_stdlog); |
0f71a2f6 | 4898 | fputs_filtered ("\n", gdb_stdlog); |
c906108c | 4899 | } |
c2d11a7d | 4900 | /* Number of characters in buffer ignoring trailing |
23860348 | 4901 | NULL. */ |
c2d11a7d | 4902 | return -1; |
c906108c | 4903 | } |
23860348 | 4904 | case '*': /* Run length encoding. */ |
c2c6d25f JM |
4905 | { |
4906 | int repeat; | |
4907 | csum += c; | |
c906108c | 4908 | |
b4501125 AC |
4909 | c = readchar (remote_timeout); |
4910 | csum += c; | |
23860348 | 4911 | repeat = c - ' ' + 3; /* Compute repeat count. */ |
c906108c | 4912 | |
23860348 | 4913 | /* The character before ``*'' is repeated. */ |
c2d11a7d | 4914 | |
6d820c5c | 4915 | if (repeat > 0 && repeat <= 255 && bc > 0) |
c2c6d25f | 4916 | { |
6d820c5c DJ |
4917 | if (bc + repeat - 1 >= *sizeof_buf - 1) |
4918 | { | |
4919 | /* Make some more room in the buffer. */ | |
4920 | *sizeof_buf += repeat; | |
4921 | *buf_p = xrealloc (*buf_p, *sizeof_buf); | |
4922 | buf = *buf_p; | |
4923 | } | |
4924 | ||
c2d11a7d JM |
4925 | memset (&buf[bc], buf[bc - 1], repeat); |
4926 | bc += repeat; | |
c2c6d25f JM |
4927 | continue; |
4928 | } | |
4929 | ||
c2d11a7d | 4930 | buf[bc] = '\0'; |
6d820c5c | 4931 | printf_filtered (_("Invalid run length encoding: %s\n"), buf); |
c2d11a7d | 4932 | return -1; |
c2c6d25f | 4933 | } |
c906108c | 4934 | default: |
6d820c5c | 4935 | if (bc >= *sizeof_buf - 1) |
c906108c | 4936 | { |
6d820c5c DJ |
4937 | /* Make some more room in the buffer. */ |
4938 | *sizeof_buf *= 2; | |
4939 | *buf_p = xrealloc (*buf_p, *sizeof_buf); | |
4940 | buf = *buf_p; | |
c906108c SS |
4941 | } |
4942 | ||
6d820c5c DJ |
4943 | buf[bc++] = c; |
4944 | csum += c; | |
4945 | continue; | |
c906108c SS |
4946 | } |
4947 | } | |
4948 | } | |
4949 | ||
4950 | /* Read a packet from the remote machine, with error checking, and | |
6d820c5c DJ |
4951 | store it in *BUF. Resize *BUF using xrealloc if necessary to hold |
4952 | the result, and update *SIZEOF_BUF. If FOREVER, wait forever | |
4953 | rather than timing out; this is used (in synchronous mode) to wait | |
4954 | for a target that is is executing user code to stop. */ | |
d9fcf2fb JM |
4955 | /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we |
4956 | don't have to change all the calls to getpkt to deal with the | |
4957 | return value, because at the moment I don't know what the right | |
23860348 | 4958 | thing to do it for those. */ |
c906108c | 4959 | void |
6d820c5c DJ |
4960 | getpkt (char **buf, |
4961 | long *sizeof_buf, | |
c2d11a7d | 4962 | int forever) |
d9fcf2fb JM |
4963 | { |
4964 | int timed_out; | |
4965 | ||
4966 | timed_out = getpkt_sane (buf, sizeof_buf, forever); | |
4967 | } | |
4968 | ||
4969 | ||
4970 | /* Read a packet from the remote machine, with error checking, and | |
6d820c5c DJ |
4971 | store it in *BUF. Resize *BUF using xrealloc if necessary to hold |
4972 | the result, and update *SIZEOF_BUF. If FOREVER, wait forever | |
4973 | rather than timing out; this is used (in synchronous mode) to wait | |
4974 | for a target that is is executing user code to stop. If FOREVER == | |
4975 | 0, this function is allowed to time out gracefully and return an | |
0876f84a DJ |
4976 | indication of this to the caller. Otherwise return the number |
4977 | of bytes read. */ | |
3172dc30 | 4978 | static int |
6d820c5c | 4979 | getpkt_sane (char **buf, long *sizeof_buf, int forever) |
c906108c | 4980 | { |
2d717e4f | 4981 | struct remote_state *rs = get_remote_state (); |
c906108c SS |
4982 | int c; |
4983 | int tries; | |
4984 | int timeout; | |
4985 | int val; | |
4986 | ||
2d717e4f DJ |
4987 | /* We're reading a new response. Make sure we don't look at a |
4988 | previously cached response. */ | |
4989 | rs->cached_wait_status = 0; | |
4990 | ||
6d820c5c | 4991 | strcpy (*buf, "timeout"); |
c906108c SS |
4992 | |
4993 | if (forever) | |
4994 | { | |
c906108c | 4995 | timeout = watchdog > 0 ? watchdog : -1; |
c906108c SS |
4996 | } |
4997 | ||
4998 | else | |
4999 | timeout = remote_timeout; | |
5000 | ||
5001 | #define MAX_TRIES 3 | |
5002 | ||
5003 | for (tries = 1; tries <= MAX_TRIES; tries++) | |
5004 | { | |
5005 | /* This can loop forever if the remote side sends us characters | |
23860348 MS |
5006 | continuously, but if it pauses, we'll get a zero from |
5007 | readchar because of timeout. Then we'll count that as a | |
5008 | retry. */ | |
c906108c | 5009 | |
23860348 MS |
5010 | /* Note that we will only wait forever prior to the start of a |
5011 | packet. After that, we expect characters to arrive at a | |
5012 | brisk pace. They should show up within remote_timeout | |
5013 | intervals. */ | |
c906108c SS |
5014 | |
5015 | do | |
5016 | { | |
5017 | c = readchar (timeout); | |
5018 | ||
5019 | if (c == SERIAL_TIMEOUT) | |
5020 | { | |
23860348 | 5021 | if (forever) /* Watchdog went off? Kill the target. */ |
c906108c | 5022 | { |
2acceee2 | 5023 | QUIT; |
c906108c | 5024 | target_mourn_inferior (); |
489eaeba | 5025 | error (_("Watchdog timeout has expired. Target detached.")); |
c906108c | 5026 | } |
c906108c | 5027 | if (remote_debug) |
0f71a2f6 | 5028 | fputs_filtered ("Timed out.\n", gdb_stdlog); |
c906108c SS |
5029 | goto retry; |
5030 | } | |
5031 | } | |
5032 | while (c != '$'); | |
5033 | ||
5034 | /* We've found the start of a packet, now collect the data. */ | |
5035 | ||
c2d11a7d | 5036 | val = read_frame (buf, sizeof_buf); |
c906108c | 5037 | |
c2d11a7d | 5038 | if (val >= 0) |
c906108c SS |
5039 | { |
5040 | if (remote_debug) | |
43e526b9 JM |
5041 | { |
5042 | fprintf_unfiltered (gdb_stdlog, "Packet received: "); | |
0876f84a | 5043 | fputstrn_unfiltered (*buf, val, 0, gdb_stdlog); |
43e526b9 JM |
5044 | fprintf_unfiltered (gdb_stdlog, "\n"); |
5045 | } | |
2cd58942 | 5046 | serial_write (remote_desc, "+", 1); |
0876f84a | 5047 | return val; |
c906108c SS |
5048 | } |
5049 | ||
5050 | /* Try the whole thing again. */ | |
5051 | retry: | |
2cd58942 | 5052 | serial_write (remote_desc, "-", 1); |
c906108c SS |
5053 | } |
5054 | ||
2bc416ba | 5055 | /* We have tried hard enough, and just can't receive the packet. |
23860348 | 5056 | Give up. */ |
c906108c | 5057 | |
a3f17187 | 5058 | printf_unfiltered (_("Ignoring packet error, continuing...\n")); |
2cd58942 | 5059 | serial_write (remote_desc, "+", 1); |
0876f84a | 5060 | return -1; |
c906108c SS |
5061 | } |
5062 | \f | |
5063 | static void | |
fba45db2 | 5064 | remote_kill (void) |
43ff13b4 | 5065 | { |
23860348 | 5066 | /* Unregister the file descriptor from the event loop. */ |
ed9a39eb | 5067 | if (target_is_async_p ()) |
2cd58942 | 5068 | serial_async (remote_desc, NULL, 0); |
43ff13b4 | 5069 | |
23860348 MS |
5070 | /* Use catch_errors so the user can quit from gdb even when we |
5071 | aren't on speaking terms with the remote system. */ | |
c5aa993b | 5072 | catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR); |
43ff13b4 JM |
5073 | |
5074 | /* Don't wait for it to die. I'm not really sure it matters whether | |
5075 | we do or not. For the existing stubs, kill is a noop. */ | |
5076 | target_mourn_inferior (); | |
5077 | } | |
5078 | ||
c906108c | 5079 | static void |
fba45db2 | 5080 | remote_mourn (void) |
c906108c SS |
5081 | { |
5082 | remote_mourn_1 (&remote_ops); | |
5083 | } | |
5084 | ||
c906108c SS |
5085 | /* Worker function for remote_mourn. */ |
5086 | static void | |
fba45db2 | 5087 | remote_mourn_1 (struct target_ops *target) |
c906108c SS |
5088 | { |
5089 | unpush_target (target); | |
5090 | generic_mourn_inferior (); | |
5091 | } | |
5092 | ||
2d717e4f DJ |
5093 | static void |
5094 | extended_remote_mourn_1 (struct target_ops *target) | |
5095 | { | |
5096 | struct remote_state *rs = get_remote_state (); | |
c906108c | 5097 | |
2d717e4f DJ |
5098 | /* Unlike "target remote", we do not want to unpush the target; then |
5099 | the next time the user says "run", we won't be connected. */ | |
5100 | ||
5101 | /* Call common code to mark the inferior as not running. */ | |
5102 | generic_mourn_inferior (); | |
5103 | ||
5104 | /* Check whether the target is running now - some remote stubs | |
5105 | automatically restart after kill. */ | |
5106 | putpkt ("?"); | |
5107 | getpkt (&rs->buf, &rs->buf_size, 0); | |
5108 | ||
5109 | if (rs->buf[0] == 'S' || rs->buf[0] == 'T') | |
5110 | { | |
5111 | /* Assume that the target has been restarted. Set inferior_ptid | |
5112 | so that bits of core GDB realizes there's something here, e.g., | |
5113 | so that the user can say "kill" again. */ | |
5114 | inferior_ptid = pid_to_ptid (MAGIC_NULL_PID); | |
5115 | } | |
5116 | else | |
5117 | { | |
5118 | /* Mark this (still pushed) target as not executable until we | |
5119 | restart it. */ | |
5120 | target_mark_exited (target); | |
5121 | } | |
5122 | } | |
c906108c SS |
5123 | |
5124 | static void | |
2d717e4f | 5125 | extended_remote_mourn (void) |
c906108c | 5126 | { |
2d717e4f DJ |
5127 | extended_remote_mourn_1 (&extended_remote_ops); |
5128 | } | |
c906108c | 5129 | |
2d717e4f DJ |
5130 | static int |
5131 | extended_remote_run (char *args) | |
5132 | { | |
5133 | struct remote_state *rs = get_remote_state (); | |
5134 | char *p; | |
5135 | int len; | |
c906108c | 5136 | |
2d717e4f DJ |
5137 | /* If the user has disabled vRun support, or we have detected that |
5138 | support is not available, do not try it. */ | |
5139 | if (remote_protocol_packets[PACKET_vRun].support == PACKET_DISABLE) | |
5140 | return -1; | |
424163ea | 5141 | |
2d717e4f DJ |
5142 | strcpy (rs->buf, "vRun;"); |
5143 | len = strlen (rs->buf); | |
c906108c | 5144 | |
2d717e4f DJ |
5145 | if (strlen (remote_exec_file) * 2 + len >= get_remote_packet_size ()) |
5146 | error (_("Remote file name too long for run packet")); | |
5147 | len += 2 * bin2hex ((gdb_byte *) remote_exec_file, rs->buf + len, 0); | |
5148 | ||
5149 | if (*args) | |
5150 | { | |
5151 | struct cleanup *back_to; | |
5152 | int i; | |
5153 | char **argv; | |
5154 | ||
5155 | argv = buildargv (args); | |
5156 | back_to = make_cleanup ((void (*) (void *)) freeargv, argv); | |
5157 | for (i = 0; argv[i] != NULL; i++) | |
5158 | { | |
5159 | if (strlen (argv[i]) * 2 + 1 + len >= get_remote_packet_size ()) | |
5160 | error (_("Argument list too long for run packet")); | |
5161 | rs->buf[len++] = ';'; | |
5162 | len += 2 * bin2hex ((gdb_byte *) argv[i], rs->buf + len, 0); | |
5163 | } | |
5164 | do_cleanups (back_to); | |
5165 | } | |
5166 | ||
5167 | rs->buf[len++] = '\0'; | |
5168 | ||
5169 | putpkt (rs->buf); | |
5170 | getpkt (&rs->buf, &rs->buf_size, 0); | |
5171 | ||
5172 | if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vRun]) == PACKET_OK) | |
5173 | { | |
5174 | /* We have a wait response; we don't need it, though. All is well. */ | |
5175 | return 0; | |
5176 | } | |
5177 | else if (remote_protocol_packets[PACKET_vRun].support == PACKET_DISABLE) | |
5178 | /* It wasn't disabled before, but it is now. */ | |
5179 | return -1; | |
5180 | else | |
5181 | { | |
5182 | if (remote_exec_file[0] == '\0') | |
5183 | error (_("Running the default executable on the remote target failed; " | |
5184 | "try \"set remote exec-file\"?")); | |
5185 | else | |
5186 | error (_("Running \"%s\" on the remote target failed"), | |
5187 | remote_exec_file); | |
5188 | } | |
c906108c SS |
5189 | } |
5190 | ||
2d717e4f DJ |
5191 | /* In the extended protocol we want to be able to do things like |
5192 | "run" and have them basically work as expected. So we need | |
5193 | a special create_inferior function. We support changing the | |
5194 | executable file and the command line arguments, but not the | |
5195 | environment. */ | |
5196 | ||
43ff13b4 | 5197 | static void |
2d717e4f | 5198 | extended_remote_create_inferior_1 (char *exec_file, char *args, |
75c99385 | 5199 | char **env, int from_tty) |
43ff13b4 | 5200 | { |
43ff13b4 | 5201 | /* If running asynchronously, register the target file descriptor |
23860348 | 5202 | with the event loop. */ |
75c99385 | 5203 | if (target_can_async_p ()) |
2acceee2 | 5204 | target_async (inferior_event_handler, 0); |
43ff13b4 JM |
5205 | |
5206 | /* Now restart the remote server. */ | |
2d717e4f DJ |
5207 | if (extended_remote_run (args) == -1) |
5208 | { | |
5209 | /* vRun was not supported. Fail if we need it to do what the | |
5210 | user requested. */ | |
5211 | if (remote_exec_file[0]) | |
5212 | error (_("Remote target does not support \"set remote exec-file\"")); | |
5213 | if (args[0]) | |
5214 | error (_("Remote target does not support \"set args\" or run <ARGS>")); | |
43ff13b4 | 5215 | |
2d717e4f DJ |
5216 | /* Fall back to "R". */ |
5217 | extended_remote_restart (); | |
5218 | } | |
424163ea | 5219 | |
45280a52 DJ |
5220 | /* Clean up from the last time we ran, before we mark the target |
5221 | running again. This will mark breakpoints uninserted, and | |
5222 | get_offsets may insert breakpoints. */ | |
5223 | init_thread_list (); | |
5224 | init_wait_for_inferior (); | |
5225 | ||
2d717e4f | 5226 | /* Now mark the inferior as running before we do anything else. */ |
df7df359 | 5227 | attach_flag = 0; |
2d717e4f | 5228 | inferior_ptid = pid_to_ptid (MAGIC_NULL_PID); |
75c99385 | 5229 | target_mark_running (&extended_remote_ops); |
2d717e4f DJ |
5230 | |
5231 | /* Get updated offsets, if the stub uses qOffsets. */ | |
5232 | get_offsets (); | |
2d717e4f DJ |
5233 | } |
5234 | ||
5235 | static void | |
5236 | extended_remote_create_inferior (char *exec_file, char *args, | |
5237 | char **env, int from_tty) | |
5238 | { | |
75c99385 | 5239 | extended_remote_create_inferior_1 (exec_file, args, env, from_tty); |
43ff13b4 | 5240 | } |
c906108c | 5241 | \f |
c5aa993b | 5242 | |
8181d85f DJ |
5243 | /* Insert a breakpoint. On targets that have software breakpoint |
5244 | support, we ask the remote target to do the work; on targets | |
5245 | which don't, we insert a traditional memory breakpoint. */ | |
c906108c SS |
5246 | |
5247 | static int | |
8181d85f | 5248 | remote_insert_breakpoint (struct bp_target_info *bp_tgt) |
c906108c | 5249 | { |
d471ea57 AC |
5250 | /* Try the "Z" s/w breakpoint packet if it is not already disabled. |
5251 | If it succeeds, then set the support to PACKET_ENABLE. If it | |
5252 | fails, and the user has explicitly requested the Z support then | |
23860348 | 5253 | report an error, otherwise, mark it disabled and go on. */ |
802188a7 | 5254 | |
444abaca | 5255 | if (remote_protocol_packets[PACKET_Z0].support != PACKET_DISABLE) |
96baa820 | 5256 | { |
4fff2411 JZ |
5257 | CORE_ADDR addr; |
5258 | struct remote_state *rs; | |
5259 | char *p; | |
5260 | ||
5261 | gdbarch_breakpoint_from_pc | |
5262 | (current_gdbarch, &bp_tgt->placed_address, &bp_tgt->placed_size); | |
5263 | ||
5264 | rs = get_remote_state (); | |
5265 | p = rs->buf; | |
802188a7 | 5266 | |
96baa820 JM |
5267 | *(p++) = 'Z'; |
5268 | *(p++) = '0'; | |
5269 | *(p++) = ','; | |
8181d85f DJ |
5270 | addr = (ULONGEST) remote_address_masked (bp_tgt->placed_address); |
5271 | p += hexnumstr (p, addr); | |
5272 | sprintf (p, ",%d", bp_tgt->placed_size); | |
802188a7 | 5273 | |
6d820c5c DJ |
5274 | putpkt (rs->buf); |
5275 | getpkt (&rs->buf, &rs->buf_size, 0); | |
96baa820 | 5276 | |
6d820c5c | 5277 | switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0])) |
96baa820 | 5278 | { |
d471ea57 AC |
5279 | case PACKET_ERROR: |
5280 | return -1; | |
5281 | case PACKET_OK: | |
5282 | return 0; | |
5283 | case PACKET_UNKNOWN: | |
5284 | break; | |
96baa820 JM |
5285 | } |
5286 | } | |
c906108c | 5287 | |
8181d85f | 5288 | return memory_insert_breakpoint (bp_tgt); |
c906108c SS |
5289 | } |
5290 | ||
5291 | static int | |
8181d85f | 5292 | remote_remove_breakpoint (struct bp_target_info *bp_tgt) |
c906108c | 5293 | { |
8181d85f | 5294 | CORE_ADDR addr = bp_tgt->placed_address; |
d01949b6 | 5295 | struct remote_state *rs = get_remote_state (); |
96baa820 JM |
5296 | int bp_size; |
5297 | ||
444abaca | 5298 | if (remote_protocol_packets[PACKET_Z0].support != PACKET_DISABLE) |
96baa820 | 5299 | { |
6d820c5c | 5300 | char *p = rs->buf; |
802188a7 | 5301 | |
96baa820 JM |
5302 | *(p++) = 'z'; |
5303 | *(p++) = '0'; | |
5304 | *(p++) = ','; | |
5305 | ||
8181d85f DJ |
5306 | addr = (ULONGEST) remote_address_masked (bp_tgt->placed_address); |
5307 | p += hexnumstr (p, addr); | |
5308 | sprintf (p, ",%d", bp_tgt->placed_size); | |
802188a7 | 5309 | |
6d820c5c DJ |
5310 | putpkt (rs->buf); |
5311 | getpkt (&rs->buf, &rs->buf_size, 0); | |
96baa820 | 5312 | |
6d820c5c | 5313 | return (rs->buf[0] == 'E'); |
96baa820 JM |
5314 | } |
5315 | ||
8181d85f | 5316 | return memory_remove_breakpoint (bp_tgt); |
c906108c SS |
5317 | } |
5318 | ||
d471ea57 AC |
5319 | static int |
5320 | watchpoint_to_Z_packet (int type) | |
5321 | { | |
5322 | switch (type) | |
5323 | { | |
5324 | case hw_write: | |
bb858e6a | 5325 | return Z_PACKET_WRITE_WP; |
d471ea57 AC |
5326 | break; |
5327 | case hw_read: | |
bb858e6a | 5328 | return Z_PACKET_READ_WP; |
d471ea57 AC |
5329 | break; |
5330 | case hw_access: | |
bb858e6a | 5331 | return Z_PACKET_ACCESS_WP; |
d471ea57 AC |
5332 | break; |
5333 | default: | |
8e65ff28 | 5334 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 5335 | _("hw_bp_to_z: bad watchpoint type %d"), type); |
d471ea57 AC |
5336 | } |
5337 | } | |
5338 | ||
3c3bea1c | 5339 | static int |
fba45db2 | 5340 | remote_insert_watchpoint (CORE_ADDR addr, int len, int type) |
96baa820 | 5341 | { |
d01949b6 | 5342 | struct remote_state *rs = get_remote_state (); |
e514a9d6 | 5343 | char *p; |
d471ea57 | 5344 | enum Z_packet_type packet = watchpoint_to_Z_packet (type); |
96baa820 | 5345 | |
444abaca | 5346 | if (remote_protocol_packets[PACKET_Z0 + packet].support == PACKET_DISABLE) |
5cffb350 | 5347 | return -1; |
802188a7 | 5348 | |
6d820c5c DJ |
5349 | sprintf (rs->buf, "Z%x,", packet); |
5350 | p = strchr (rs->buf, '\0'); | |
96baa820 JM |
5351 | addr = remote_address_masked (addr); |
5352 | p += hexnumstr (p, (ULONGEST) addr); | |
d4f3574e | 5353 | sprintf (p, ",%x", len); |
802188a7 | 5354 | |
6d820c5c DJ |
5355 | putpkt (rs->buf); |
5356 | getpkt (&rs->buf, &rs->buf_size, 0); | |
96baa820 | 5357 | |
6d820c5c | 5358 | switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet])) |
d471ea57 AC |
5359 | { |
5360 | case PACKET_ERROR: | |
5361 | case PACKET_UNKNOWN: | |
5362 | return -1; | |
5363 | case PACKET_OK: | |
5364 | return 0; | |
5365 | } | |
8e65ff28 | 5366 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 5367 | _("remote_insert_watchpoint: reached end of function")); |
96baa820 JM |
5368 | } |
5369 | ||
d471ea57 | 5370 | |
3c3bea1c | 5371 | static int |
fba45db2 | 5372 | remote_remove_watchpoint (CORE_ADDR addr, int len, int type) |
96baa820 | 5373 | { |
d01949b6 | 5374 | struct remote_state *rs = get_remote_state (); |
e514a9d6 | 5375 | char *p; |
d471ea57 AC |
5376 | enum Z_packet_type packet = watchpoint_to_Z_packet (type); |
5377 | ||
444abaca | 5378 | if (remote_protocol_packets[PACKET_Z0 + packet].support == PACKET_DISABLE) |
5cffb350 | 5379 | return -1; |
802188a7 | 5380 | |
6d820c5c DJ |
5381 | sprintf (rs->buf, "z%x,", packet); |
5382 | p = strchr (rs->buf, '\0'); | |
96baa820 JM |
5383 | addr = remote_address_masked (addr); |
5384 | p += hexnumstr (p, (ULONGEST) addr); | |
d4f3574e | 5385 | sprintf (p, ",%x", len); |
6d820c5c DJ |
5386 | putpkt (rs->buf); |
5387 | getpkt (&rs->buf, &rs->buf_size, 0); | |
96baa820 | 5388 | |
6d820c5c | 5389 | switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet])) |
d471ea57 AC |
5390 | { |
5391 | case PACKET_ERROR: | |
5392 | case PACKET_UNKNOWN: | |
5393 | return -1; | |
5394 | case PACKET_OK: | |
5395 | return 0; | |
5396 | } | |
8e65ff28 | 5397 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 5398 | _("remote_remove_watchpoint: reached end of function")); |
96baa820 JM |
5399 | } |
5400 | ||
3c3bea1c | 5401 | |
501eef12 AC |
5402 | int remote_hw_watchpoint_limit = -1; |
5403 | int remote_hw_breakpoint_limit = -1; | |
d471ea57 | 5404 | |
b9362cc7 | 5405 | static int |
3c3bea1c | 5406 | remote_check_watch_resources (int type, int cnt, int ot) |
96baa820 | 5407 | { |
3c3bea1c GS |
5408 | if (type == bp_hardware_breakpoint) |
5409 | { | |
5410 | if (remote_hw_breakpoint_limit == 0) | |
5411 | return 0; | |
501eef12 AC |
5412 | else if (remote_hw_breakpoint_limit < 0) |
5413 | return 1; | |
3c3bea1c GS |
5414 | else if (cnt <= remote_hw_breakpoint_limit) |
5415 | return 1; | |
5416 | } | |
5417 | else | |
5418 | { | |
5419 | if (remote_hw_watchpoint_limit == 0) | |
5420 | return 0; | |
501eef12 AC |
5421 | else if (remote_hw_watchpoint_limit < 0) |
5422 | return 1; | |
3c3bea1c GS |
5423 | else if (ot) |
5424 | return -1; | |
5425 | else if (cnt <= remote_hw_watchpoint_limit) | |
5426 | return 1; | |
5427 | } | |
5428 | return -1; | |
5429 | } | |
5430 | ||
b9362cc7 | 5431 | static int |
3c3bea1c GS |
5432 | remote_stopped_by_watchpoint (void) |
5433 | { | |
5434 | return remote_stopped_by_watchpoint_p; | |
5435 | } | |
5436 | ||
4aa7a7f5 JJ |
5437 | static int |
5438 | remote_stopped_data_address (struct target_ops *target, CORE_ADDR *addr_p) | |
3c3bea1c | 5439 | { |
4aa7a7f5 | 5440 | int rc = 0; |
d983da9c | 5441 | if (remote_stopped_by_watchpoint ()) |
4aa7a7f5 JJ |
5442 | { |
5443 | *addr_p = remote_watch_data_address; | |
5444 | rc = 1; | |
5445 | } | |
5446 | ||
5447 | return rc; | |
3c3bea1c GS |
5448 | } |
5449 | ||
5450 | ||
5451 | static int | |
8181d85f | 5452 | remote_insert_hw_breakpoint (struct bp_target_info *bp_tgt) |
3c3bea1c | 5453 | { |
8181d85f | 5454 | CORE_ADDR addr; |
4fff2411 JZ |
5455 | struct remote_state *rs; |
5456 | char *p; | |
802188a7 | 5457 | |
c8189ed1 | 5458 | /* The length field should be set to the size of a breakpoint |
8181d85f | 5459 | instruction, even though we aren't inserting one ourselves. */ |
c8189ed1 | 5460 | |
3b3b875c UW |
5461 | gdbarch_breakpoint_from_pc |
5462 | (current_gdbarch, &bp_tgt->placed_address, &bp_tgt->placed_size); | |
3c3bea1c | 5463 | |
444abaca | 5464 | if (remote_protocol_packets[PACKET_Z1].support == PACKET_DISABLE) |
5cffb350 | 5465 | return -1; |
2bc416ba | 5466 | |
4fff2411 JZ |
5467 | rs = get_remote_state (); |
5468 | p = rs->buf; | |
5469 | ||
96baa820 JM |
5470 | *(p++) = 'Z'; |
5471 | *(p++) = '1'; | |
5472 | *(p++) = ','; | |
802188a7 | 5473 | |
8181d85f | 5474 | addr = remote_address_masked (bp_tgt->placed_address); |
96baa820 | 5475 | p += hexnumstr (p, (ULONGEST) addr); |
8181d85f | 5476 | sprintf (p, ",%x", bp_tgt->placed_size); |
96baa820 | 5477 | |
6d820c5c DJ |
5478 | putpkt (rs->buf); |
5479 | getpkt (&rs->buf, &rs->buf_size, 0); | |
96baa820 | 5480 | |
6d820c5c | 5481 | switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1])) |
d471ea57 AC |
5482 | { |
5483 | case PACKET_ERROR: | |
5484 | case PACKET_UNKNOWN: | |
5485 | return -1; | |
5486 | case PACKET_OK: | |
5487 | return 0; | |
5488 | } | |
8e65ff28 | 5489 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 5490 | _("remote_insert_hw_breakpoint: reached end of function")); |
96baa820 JM |
5491 | } |
5492 | ||
d471ea57 | 5493 | |
802188a7 | 5494 | static int |
8181d85f | 5495 | remote_remove_hw_breakpoint (struct bp_target_info *bp_tgt) |
96baa820 | 5496 | { |
8181d85f | 5497 | CORE_ADDR addr; |
d01949b6 | 5498 | struct remote_state *rs = get_remote_state (); |
6d820c5c | 5499 | char *p = rs->buf; |
c8189ed1 | 5500 | |
444abaca | 5501 | if (remote_protocol_packets[PACKET_Z1].support == PACKET_DISABLE) |
5cffb350 | 5502 | return -1; |
802188a7 | 5503 | |
96baa820 JM |
5504 | *(p++) = 'z'; |
5505 | *(p++) = '1'; | |
5506 | *(p++) = ','; | |
802188a7 | 5507 | |
8181d85f | 5508 | addr = remote_address_masked (bp_tgt->placed_address); |
96baa820 | 5509 | p += hexnumstr (p, (ULONGEST) addr); |
8181d85f | 5510 | sprintf (p, ",%x", bp_tgt->placed_size); |
96baa820 | 5511 | |
6d820c5c DJ |
5512 | putpkt (rs->buf); |
5513 | getpkt (&rs->buf, &rs->buf_size, 0); | |
802188a7 | 5514 | |
6d820c5c | 5515 | switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1])) |
d471ea57 AC |
5516 | { |
5517 | case PACKET_ERROR: | |
5518 | case PACKET_UNKNOWN: | |
5519 | return -1; | |
5520 | case PACKET_OK: | |
5521 | return 0; | |
5522 | } | |
8e65ff28 | 5523 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 5524 | _("remote_remove_hw_breakpoint: reached end of function")); |
96baa820 | 5525 | } |
96baa820 | 5526 | |
c906108c SS |
5527 | /* Some targets are only capable of doing downloads, and afterwards |
5528 | they switch to the remote serial protocol. This function provides | |
5529 | a clean way to get from the download target to the remote target. | |
5530 | It's basically just a wrapper so that we don't have to expose any | |
5531 | of the internal workings of remote.c. | |
5532 | ||
5533 | Prior to calling this routine, you should shutdown the current | |
5534 | target code, else you will get the "A program is being debugged | |
5535 | already..." message. Usually a call to pop_target() suffices. */ | |
5536 | ||
5537 | void | |
fba45db2 | 5538 | push_remote_target (char *name, int from_tty) |
c906108c | 5539 | { |
a3f17187 | 5540 | printf_filtered (_("Switching to remote protocol\n")); |
c906108c SS |
5541 | remote_open (name, from_tty); |
5542 | } | |
5543 | ||
23860348 | 5544 | /* Table used by the crc32 function to calcuate the checksum. */ |
c906108c | 5545 | |
c5aa993b JM |
5546 | static unsigned long crc32_table[256] = |
5547 | {0, 0}; | |
c906108c SS |
5548 | |
5549 | static unsigned long | |
fba45db2 | 5550 | crc32 (unsigned char *buf, int len, unsigned int crc) |
c906108c | 5551 | { |
c5aa993b | 5552 | if (!crc32_table[1]) |
c906108c | 5553 | { |
23860348 | 5554 | /* Initialize the CRC table and the decoding table. */ |
c906108c SS |
5555 | int i, j; |
5556 | unsigned int c; | |
5557 | ||
5558 | for (i = 0; i < 256; i++) | |
c5aa993b JM |
5559 | { |
5560 | for (c = i << 24, j = 8; j > 0; --j) | |
5561 | c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1); | |
5562 | crc32_table[i] = c; | |
5563 | } | |
c906108c SS |
5564 | } |
5565 | ||
5566 | while (len--) | |
5567 | { | |
5568 | crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buf) & 255]; | |
5569 | buf++; | |
5570 | } | |
5571 | return crc; | |
5572 | } | |
5573 | ||
5574 | /* compare-sections command | |
5575 | ||
5576 | With no arguments, compares each loadable section in the exec bfd | |
5577 | with the same memory range on the target, and reports mismatches. | |
5578 | Useful for verifying the image on the target against the exec file. | |
5579 | Depends on the target understanding the new "qCRC:" request. */ | |
5580 | ||
e514a9d6 JM |
5581 | /* FIXME: cagney/1999-10-26: This command should be broken down into a |
5582 | target method (target verify memory) and generic version of the | |
5583 | actual command. This will allow other high-level code (especially | |
23860348 | 5584 | generic_load()) to make use of this target functionality. */ |
e514a9d6 | 5585 | |
c906108c | 5586 | static void |
fba45db2 | 5587 | compare_sections_command (char *args, int from_tty) |
c906108c | 5588 | { |
d01949b6 | 5589 | struct remote_state *rs = get_remote_state (); |
c906108c SS |
5590 | asection *s; |
5591 | unsigned long host_crc, target_crc; | |
5592 | extern bfd *exec_bfd; | |
5593 | struct cleanup *old_chain; | |
085dd6e6 JM |
5594 | char *tmp; |
5595 | char *sectdata; | |
ce359b09 | 5596 | const char *sectname; |
c906108c SS |
5597 | bfd_size_type size; |
5598 | bfd_vma lma; | |
5599 | int matched = 0; | |
5600 | int mismatched = 0; | |
5601 | ||
5602 | if (!exec_bfd) | |
8a3fe4f8 | 5603 | error (_("command cannot be used without an exec file")); |
c906108c SS |
5604 | if (!current_target.to_shortname || |
5605 | strcmp (current_target.to_shortname, "remote") != 0) | |
8a3fe4f8 | 5606 | error (_("command can only be used with remote target")); |
c906108c | 5607 | |
c5aa993b | 5608 | for (s = exec_bfd->sections; s; s = s->next) |
c906108c SS |
5609 | { |
5610 | if (!(s->flags & SEC_LOAD)) | |
c5aa993b | 5611 | continue; /* skip non-loadable section */ |
c906108c | 5612 | |
2c500098 | 5613 | size = bfd_get_section_size (s); |
c906108c | 5614 | if (size == 0) |
c5aa993b | 5615 | continue; /* skip zero-length section */ |
c906108c | 5616 | |
ce359b09 | 5617 | sectname = bfd_get_section_name (exec_bfd, s); |
c906108c | 5618 | if (args && strcmp (args, sectname) != 0) |
c5aa993b | 5619 | continue; /* not the section selected by user */ |
c906108c | 5620 | |
c5aa993b | 5621 | matched = 1; /* do this section */ |
c906108c | 5622 | lma = s->lma; |
23860348 | 5623 | /* FIXME: assumes lma can fit into long. */ |
ea9c271d | 5624 | xsnprintf (rs->buf, get_remote_packet_size (), "qCRC:%lx,%lx", |
ecbc58df | 5625 | (long) lma, (long) size); |
6d820c5c | 5626 | putpkt (rs->buf); |
c906108c | 5627 | |
23860348 MS |
5628 | /* Be clever; compute the host_crc before waiting for target |
5629 | reply. */ | |
c906108c | 5630 | sectdata = xmalloc (size); |
b8c9b27d | 5631 | old_chain = make_cleanup (xfree, sectdata); |
c906108c SS |
5632 | bfd_get_section_contents (exec_bfd, s, sectdata, 0, size); |
5633 | host_crc = crc32 ((unsigned char *) sectdata, size, 0xffffffff); | |
5634 | ||
6d820c5c DJ |
5635 | getpkt (&rs->buf, &rs->buf_size, 0); |
5636 | if (rs->buf[0] == 'E') | |
8a3fe4f8 | 5637 | error (_("target memory fault, section %s, range 0x%s -- 0x%s"), |
823ca731 | 5638 | sectname, paddr (lma), paddr (lma + size)); |
6d820c5c | 5639 | if (rs->buf[0] != 'C') |
8a3fe4f8 | 5640 | error (_("remote target does not support this operation")); |
c906108c | 5641 | |
6d820c5c | 5642 | for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++) |
c906108c SS |
5643 | target_crc = target_crc * 16 + fromhex (*tmp); |
5644 | ||
d4f3574e SS |
5645 | printf_filtered ("Section %s, range 0x%s -- 0x%s: ", |
5646 | sectname, paddr (lma), paddr (lma + size)); | |
c906108c SS |
5647 | if (host_crc == target_crc) |
5648 | printf_filtered ("matched.\n"); | |
5649 | else | |
c5aa993b JM |
5650 | { |
5651 | printf_filtered ("MIS-MATCHED!\n"); | |
5652 | mismatched++; | |
5653 | } | |
c906108c SS |
5654 | |
5655 | do_cleanups (old_chain); | |
5656 | } | |
5657 | if (mismatched > 0) | |
8a3fe4f8 AC |
5658 | warning (_("One or more sections of the remote executable does not match\n\ |
5659 | the loaded file\n")); | |
c906108c | 5660 | if (args && !matched) |
a3f17187 | 5661 | printf_filtered (_("No loaded section named '%s'.\n"), args); |
c906108c SS |
5662 | } |
5663 | ||
0e7f50da UW |
5664 | /* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET |
5665 | into remote target. The number of bytes written to the remote | |
5666 | target is returned, or -1 for error. */ | |
5667 | ||
5668 | static LONGEST | |
5669 | remote_write_qxfer (struct target_ops *ops, const char *object_name, | |
5670 | const char *annex, const gdb_byte *writebuf, | |
5671 | ULONGEST offset, LONGEST len, | |
5672 | struct packet_config *packet) | |
5673 | { | |
5674 | int i, buf_len; | |
5675 | ULONGEST n; | |
5676 | gdb_byte *wbuf; | |
5677 | struct remote_state *rs = get_remote_state (); | |
5678 | int max_size = get_memory_write_packet_size (); | |
5679 | ||
5680 | if (packet->support == PACKET_DISABLE) | |
5681 | return -1; | |
5682 | ||
5683 | /* Insert header. */ | |
5684 | i = snprintf (rs->buf, max_size, | |
5685 | "qXfer:%s:write:%s:%s:", | |
5686 | object_name, annex ? annex : "", | |
5687 | phex_nz (offset, sizeof offset)); | |
5688 | max_size -= (i + 1); | |
5689 | ||
5690 | /* Escape as much data as fits into rs->buf. */ | |
5691 | buf_len = remote_escape_output | |
5692 | (writebuf, len, (rs->buf + i), &max_size, max_size); | |
5693 | ||
5694 | if (putpkt_binary (rs->buf, i + buf_len) < 0 | |
5695 | || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0 | |
5696 | || packet_ok (rs->buf, packet) != PACKET_OK) | |
5697 | return -1; | |
5698 | ||
5699 | unpack_varlen_hex (rs->buf, &n); | |
5700 | return n; | |
5701 | } | |
5702 | ||
0876f84a DJ |
5703 | /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet. |
5704 | Data at OFFSET, of up to LEN bytes, is read into READBUF; the | |
5705 | number of bytes read is returned, or 0 for EOF, or -1 for error. | |
5706 | The number of bytes read may be less than LEN without indicating an | |
5707 | EOF. PACKET is checked and updated to indicate whether the remote | |
5708 | target supports this object. */ | |
5709 | ||
5710 | static LONGEST | |
5711 | remote_read_qxfer (struct target_ops *ops, const char *object_name, | |
5712 | const char *annex, | |
5713 | gdb_byte *readbuf, ULONGEST offset, LONGEST len, | |
5714 | struct packet_config *packet) | |
5715 | { | |
5716 | static char *finished_object; | |
5717 | static char *finished_annex; | |
5718 | static ULONGEST finished_offset; | |
5719 | ||
5720 | struct remote_state *rs = get_remote_state (); | |
5721 | unsigned int total = 0; | |
5722 | LONGEST i, n, packet_len; | |
5723 | ||
5724 | if (packet->support == PACKET_DISABLE) | |
5725 | return -1; | |
5726 | ||
5727 | /* Check whether we've cached an end-of-object packet that matches | |
5728 | this request. */ | |
5729 | if (finished_object) | |
5730 | { | |
5731 | if (strcmp (object_name, finished_object) == 0 | |
5732 | && strcmp (annex ? annex : "", finished_annex) == 0 | |
5733 | && offset == finished_offset) | |
5734 | return 0; | |
5735 | ||
5736 | /* Otherwise, we're now reading something different. Discard | |
5737 | the cache. */ | |
5738 | xfree (finished_object); | |
5739 | xfree (finished_annex); | |
5740 | finished_object = NULL; | |
5741 | finished_annex = NULL; | |
5742 | } | |
5743 | ||
5744 | /* Request only enough to fit in a single packet. The actual data | |
5745 | may not, since we don't know how much of it will need to be escaped; | |
5746 | the target is free to respond with slightly less data. We subtract | |
5747 | five to account for the response type and the protocol frame. */ | |
5748 | n = min (get_remote_packet_size () - 5, len); | |
5749 | snprintf (rs->buf, get_remote_packet_size () - 4, "qXfer:%s:read:%s:%s,%s", | |
5750 | object_name, annex ? annex : "", | |
5751 | phex_nz (offset, sizeof offset), | |
5752 | phex_nz (n, sizeof n)); | |
5753 | i = putpkt (rs->buf); | |
5754 | if (i < 0) | |
5755 | return -1; | |
5756 | ||
5757 | rs->buf[0] = '\0'; | |
5758 | packet_len = getpkt_sane (&rs->buf, &rs->buf_size, 0); | |
5759 | if (packet_len < 0 || packet_ok (rs->buf, packet) != PACKET_OK) | |
5760 | return -1; | |
5761 | ||
5762 | if (rs->buf[0] != 'l' && rs->buf[0] != 'm') | |
5763 | error (_("Unknown remote qXfer reply: %s"), rs->buf); | |
5764 | ||
5765 | /* 'm' means there is (or at least might be) more data after this | |
5766 | batch. That does not make sense unless there's at least one byte | |
5767 | of data in this reply. */ | |
5768 | if (rs->buf[0] == 'm' && packet_len == 1) | |
5769 | error (_("Remote qXfer reply contained no data.")); | |
5770 | ||
5771 | /* Got some data. */ | |
5772 | i = remote_unescape_input (rs->buf + 1, packet_len - 1, readbuf, n); | |
5773 | ||
5774 | /* 'l' is an EOF marker, possibly including a final block of data, | |
0e7f50da UW |
5775 | or possibly empty. If we have the final block of a non-empty |
5776 | object, record this fact to bypass a subsequent partial read. */ | |
5777 | if (rs->buf[0] == 'l' && offset + i > 0) | |
0876f84a DJ |
5778 | { |
5779 | finished_object = xstrdup (object_name); | |
5780 | finished_annex = xstrdup (annex ? annex : ""); | |
5781 | finished_offset = offset + i; | |
5782 | } | |
5783 | ||
5784 | return i; | |
5785 | } | |
5786 | ||
1e3ff5ad | 5787 | static LONGEST |
4b8a223f | 5788 | remote_xfer_partial (struct target_ops *ops, enum target_object object, |
961cb7b5 MK |
5789 | const char *annex, gdb_byte *readbuf, |
5790 | const gdb_byte *writebuf, ULONGEST offset, LONGEST len) | |
c906108c | 5791 | { |
d01949b6 | 5792 | struct remote_state *rs = get_remote_state (); |
c906108c | 5793 | int i; |
6d820c5c | 5794 | char *p2; |
1e3ff5ad | 5795 | char query_type; |
c906108c | 5796 | |
b2182ed2 | 5797 | /* Handle memory using the standard memory routines. */ |
21e3b9b9 DJ |
5798 | if (object == TARGET_OBJECT_MEMORY) |
5799 | { | |
5800 | int xfered; | |
5801 | errno = 0; | |
5802 | ||
2d717e4f DJ |
5803 | /* If the remote target is connected but not running, we should |
5804 | pass this request down to a lower stratum (e.g. the executable | |
5805 | file). */ | |
5806 | if (!target_has_execution) | |
5807 | return 0; | |
5808 | ||
21e3b9b9 | 5809 | if (writebuf != NULL) |
b2182ed2 | 5810 | xfered = remote_write_bytes (offset, writebuf, len); |
21e3b9b9 | 5811 | else |
b2182ed2 | 5812 | xfered = remote_read_bytes (offset, readbuf, len); |
21e3b9b9 DJ |
5813 | |
5814 | if (xfered > 0) | |
5815 | return xfered; | |
5816 | else if (xfered == 0 && errno == 0) | |
5817 | return 0; | |
5818 | else | |
5819 | return -1; | |
5820 | } | |
5821 | ||
0e7f50da UW |
5822 | /* Handle SPU memory using qxfer packets. */ |
5823 | if (object == TARGET_OBJECT_SPU) | |
5824 | { | |
5825 | if (readbuf) | |
5826 | return remote_read_qxfer (ops, "spu", annex, readbuf, offset, len, | |
5827 | &remote_protocol_packets | |
5828 | [PACKET_qXfer_spu_read]); | |
5829 | else | |
5830 | return remote_write_qxfer (ops, "spu", annex, writebuf, offset, len, | |
5831 | &remote_protocol_packets | |
5832 | [PACKET_qXfer_spu_write]); | |
5833 | } | |
5834 | ||
a76d924d DJ |
5835 | /* Only handle flash writes. */ |
5836 | if (writebuf != NULL) | |
5837 | { | |
5838 | LONGEST xfered; | |
5839 | ||
5840 | switch (object) | |
5841 | { | |
5842 | case TARGET_OBJECT_FLASH: | |
5843 | xfered = remote_flash_write (ops, offset, len, writebuf); | |
5844 | ||
5845 | if (xfered > 0) | |
5846 | return xfered; | |
5847 | else if (xfered == 0 && errno == 0) | |
5848 | return 0; | |
5849 | else | |
5850 | return -1; | |
5851 | ||
5852 | default: | |
5853 | return -1; | |
5854 | } | |
5855 | } | |
4b8a223f | 5856 | |
1e3ff5ad AC |
5857 | /* Map pre-existing objects onto letters. DO NOT do this for new |
5858 | objects!!! Instead specify new query packets. */ | |
5859 | switch (object) | |
c906108c | 5860 | { |
1e3ff5ad AC |
5861 | case TARGET_OBJECT_AVR: |
5862 | query_type = 'R'; | |
5863 | break; | |
802188a7 RM |
5864 | |
5865 | case TARGET_OBJECT_AUXV: | |
0876f84a DJ |
5866 | gdb_assert (annex == NULL); |
5867 | return remote_read_qxfer (ops, "auxv", annex, readbuf, offset, len, | |
5868 | &remote_protocol_packets[PACKET_qXfer_auxv]); | |
802188a7 | 5869 | |
23181151 DJ |
5870 | case TARGET_OBJECT_AVAILABLE_FEATURES: |
5871 | return remote_read_qxfer | |
5872 | (ops, "features", annex, readbuf, offset, len, | |
5873 | &remote_protocol_packets[PACKET_qXfer_features]); | |
5874 | ||
cfa9d6d9 DJ |
5875 | case TARGET_OBJECT_LIBRARIES: |
5876 | return remote_read_qxfer | |
5877 | (ops, "libraries", annex, readbuf, offset, len, | |
5878 | &remote_protocol_packets[PACKET_qXfer_libraries]); | |
5879 | ||
fd79ecee DJ |
5880 | case TARGET_OBJECT_MEMORY_MAP: |
5881 | gdb_assert (annex == NULL); | |
5882 | return remote_read_qxfer (ops, "memory-map", annex, readbuf, offset, len, | |
5883 | &remote_protocol_packets[PACKET_qXfer_memory_map]); | |
5884 | ||
1e3ff5ad | 5885 | default: |
c906108c SS |
5886 | return -1; |
5887 | } | |
5888 | ||
4b8a223f | 5889 | /* Note: a zero OFFSET and LEN can be used to query the minimum |
1e3ff5ad | 5890 | buffer size. */ |
4b8a223f | 5891 | if (offset == 0 && len == 0) |
ea9c271d DJ |
5892 | return (get_remote_packet_size ()); |
5893 | /* Minimum outbuf size is get_remote_packet_size (). If LEN is not | |
24b06219 | 5894 | large enough let the caller deal with it. */ |
ea9c271d | 5895 | if (len < get_remote_packet_size ()) |
1e3ff5ad | 5896 | return -1; |
ea9c271d | 5897 | len = get_remote_packet_size (); |
1e3ff5ad | 5898 | |
23860348 | 5899 | /* Except for querying the minimum buffer size, target must be open. */ |
c5aa993b | 5900 | if (!remote_desc) |
8a3fe4f8 | 5901 | error (_("remote query is only available after target open")); |
c906108c | 5902 | |
1e3ff5ad | 5903 | gdb_assert (annex != NULL); |
4b8a223f | 5904 | gdb_assert (readbuf != NULL); |
c906108c | 5905 | |
6d820c5c | 5906 | p2 = rs->buf; |
c906108c SS |
5907 | *p2++ = 'q'; |
5908 | *p2++ = query_type; | |
5909 | ||
23860348 MS |
5910 | /* We used one buffer char for the remote protocol q command and |
5911 | another for the query type. As the remote protocol encapsulation | |
5912 | uses 4 chars plus one extra in case we are debugging | |
5913 | (remote_debug), we have PBUFZIZ - 7 left to pack the query | |
5914 | string. */ | |
c906108c | 5915 | i = 0; |
ea9c271d | 5916 | while (annex[i] && (i < (get_remote_packet_size () - 8))) |
c906108c | 5917 | { |
1e3ff5ad AC |
5918 | /* Bad caller may have sent forbidden characters. */ |
5919 | gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#'); | |
5920 | *p2++ = annex[i]; | |
c906108c SS |
5921 | i++; |
5922 | } | |
1e3ff5ad AC |
5923 | *p2 = '\0'; |
5924 | gdb_assert (annex[i] == '\0'); | |
c906108c | 5925 | |
6d820c5c | 5926 | i = putpkt (rs->buf); |
c5aa993b JM |
5927 | if (i < 0) |
5928 | return i; | |
c906108c | 5929 | |
6d820c5c DJ |
5930 | getpkt (&rs->buf, &rs->buf_size, 0); |
5931 | strcpy ((char *) readbuf, rs->buf); | |
c906108c | 5932 | |
cfd77fa1 | 5933 | return strlen ((char *) readbuf); |
c906108c SS |
5934 | } |
5935 | ||
08388c79 DE |
5936 | static int |
5937 | remote_search_memory (struct target_ops* ops, | |
5938 | CORE_ADDR start_addr, ULONGEST search_space_len, | |
5939 | const gdb_byte *pattern, ULONGEST pattern_len, | |
5940 | CORE_ADDR *found_addrp) | |
5941 | { | |
5942 | struct remote_state *rs = get_remote_state (); | |
5943 | int max_size = get_memory_write_packet_size (); | |
5944 | struct packet_config *packet = | |
5945 | &remote_protocol_packets[PACKET_qSearch_memory]; | |
5946 | /* number of packet bytes used to encode the pattern, | |
5947 | this could be more than PATTERN_LEN due to escape characters */ | |
5948 | int escaped_pattern_len; | |
5949 | /* amount of pattern that was encodable in the packet */ | |
5950 | int used_pattern_len; | |
5951 | int i; | |
5952 | int found; | |
5953 | ULONGEST found_addr; | |
5954 | ||
5955 | /* Don't go to the target if we don't have to. | |
5956 | This is done before checking packet->support to avoid the possibility that | |
5957 | a success for this edge case means the facility works in general. */ | |
5958 | if (pattern_len > search_space_len) | |
5959 | return 0; | |
5960 | if (pattern_len == 0) | |
5961 | { | |
5962 | *found_addrp = start_addr; | |
5963 | return 1; | |
5964 | } | |
5965 | ||
5966 | /* If we already know the packet isn't supported, fall back to the simple | |
5967 | way of searching memory. */ | |
5968 | ||
5969 | if (packet->support == PACKET_DISABLE) | |
5970 | { | |
5971 | /* Target doesn't provided special support, fall back and use the | |
5972 | standard support (copy memory and do the search here). */ | |
5973 | return simple_search_memory (ops, start_addr, search_space_len, | |
5974 | pattern, pattern_len, found_addrp); | |
5975 | } | |
5976 | ||
5977 | /* Insert header. */ | |
5978 | i = snprintf (rs->buf, max_size, | |
5979 | "qSearch:memory:%s;%s;", | |
5980 | paddr_nz (start_addr), | |
5981 | phex_nz (search_space_len, sizeof (search_space_len))); | |
5982 | max_size -= (i + 1); | |
5983 | ||
5984 | /* Escape as much data as fits into rs->buf. */ | |
5985 | escaped_pattern_len = | |
5986 | remote_escape_output (pattern, pattern_len, (rs->buf + i), | |
5987 | &used_pattern_len, max_size); | |
5988 | ||
5989 | /* Bail if the pattern is too large. */ | |
5990 | if (used_pattern_len != pattern_len) | |
10e0fa18 | 5991 | error ("Pattern is too large to transmit to remote target."); |
08388c79 DE |
5992 | |
5993 | if (putpkt_binary (rs->buf, i + escaped_pattern_len) < 0 | |
5994 | || getpkt_sane (&rs->buf, &rs->buf_size, 0) < 0 | |
5995 | || packet_ok (rs->buf, packet) != PACKET_OK) | |
5996 | { | |
5997 | /* The request may not have worked because the command is not | |
5998 | supported. If so, fall back to the simple way. */ | |
5999 | if (packet->support == PACKET_DISABLE) | |
6000 | { | |
6001 | return simple_search_memory (ops, start_addr, search_space_len, | |
6002 | pattern, pattern_len, found_addrp); | |
6003 | } | |
6004 | return -1; | |
6005 | } | |
6006 | ||
6007 | if (rs->buf[0] == '0') | |
6008 | found = 0; | |
6009 | else if (rs->buf[0] == '1') | |
6010 | { | |
6011 | found = 1; | |
6012 | if (rs->buf[1] != ',') | |
10e0fa18 | 6013 | error (_("Unknown qSearch:memory reply: %s"), rs->buf); |
08388c79 DE |
6014 | unpack_varlen_hex (rs->buf + 2, &found_addr); |
6015 | *found_addrp = found_addr; | |
6016 | } | |
6017 | else | |
10e0fa18 | 6018 | error (_("Unknown qSearch:memory reply: %s"), rs->buf); |
08388c79 DE |
6019 | |
6020 | return found; | |
6021 | } | |
6022 | ||
96baa820 JM |
6023 | static void |
6024 | remote_rcmd (char *command, | |
d9fcf2fb | 6025 | struct ui_file *outbuf) |
96baa820 | 6026 | { |
d01949b6 | 6027 | struct remote_state *rs = get_remote_state (); |
2e9f7625 | 6028 | char *p = rs->buf; |
96baa820 JM |
6029 | |
6030 | if (!remote_desc) | |
8a3fe4f8 | 6031 | error (_("remote rcmd is only available after target open")); |
96baa820 | 6032 | |
23860348 | 6033 | /* Send a NULL command across as an empty command. */ |
7be570e7 JM |
6034 | if (command == NULL) |
6035 | command = ""; | |
6036 | ||
23860348 | 6037 | /* The query prefix. */ |
2e9f7625 DJ |
6038 | strcpy (rs->buf, "qRcmd,"); |
6039 | p = strchr (rs->buf, '\0'); | |
96baa820 | 6040 | |
2e9f7625 | 6041 | if ((strlen (rs->buf) + strlen (command) * 2 + 8/*misc*/) > get_remote_packet_size ()) |
8a3fe4f8 | 6042 | error (_("\"monitor\" command ``%s'' is too long."), command); |
96baa820 | 6043 | |
23860348 | 6044 | /* Encode the actual command. */ |
cfd77fa1 | 6045 | bin2hex ((gdb_byte *) command, p, 0); |
96baa820 | 6046 | |
6d820c5c | 6047 | if (putpkt (rs->buf) < 0) |
8a3fe4f8 | 6048 | error (_("Communication problem with target.")); |
96baa820 JM |
6049 | |
6050 | /* get/display the response */ | |
6051 | while (1) | |
6052 | { | |
2e9f7625 DJ |
6053 | char *buf; |
6054 | ||
23860348 | 6055 | /* XXX - see also tracepoint.c:remote_get_noisy_reply(). */ |
2e9f7625 | 6056 | rs->buf[0] = '\0'; |
6d820c5c | 6057 | getpkt (&rs->buf, &rs->buf_size, 0); |
2e9f7625 | 6058 | buf = rs->buf; |
96baa820 | 6059 | if (buf[0] == '\0') |
8a3fe4f8 | 6060 | error (_("Target does not support this command.")); |
96baa820 JM |
6061 | if (buf[0] == 'O' && buf[1] != 'K') |
6062 | { | |
23860348 | 6063 | remote_console_output (buf + 1); /* 'O' message from stub. */ |
96baa820 JM |
6064 | continue; |
6065 | } | |
6066 | if (strcmp (buf, "OK") == 0) | |
6067 | break; | |
7be570e7 JM |
6068 | if (strlen (buf) == 3 && buf[0] == 'E' |
6069 | && isdigit (buf[1]) && isdigit (buf[2])) | |
6070 | { | |
8a3fe4f8 | 6071 | error (_("Protocol error with Rcmd")); |
7be570e7 | 6072 | } |
96baa820 JM |
6073 | for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2) |
6074 | { | |
6075 | char c = (fromhex (p[0]) << 4) + fromhex (p[1]); | |
6076 | fputc_unfiltered (c, outbuf); | |
6077 | } | |
6078 | break; | |
6079 | } | |
6080 | } | |
6081 | ||
fd79ecee DJ |
6082 | static VEC(mem_region_s) * |
6083 | remote_memory_map (struct target_ops *ops) | |
6084 | { | |
6085 | VEC(mem_region_s) *result = NULL; | |
6086 | char *text = target_read_stralloc (¤t_target, | |
6087 | TARGET_OBJECT_MEMORY_MAP, NULL); | |
6088 | ||
6089 | if (text) | |
6090 | { | |
6091 | struct cleanup *back_to = make_cleanup (xfree, text); | |
6092 | result = parse_memory_map (text); | |
6093 | do_cleanups (back_to); | |
6094 | } | |
6095 | ||
6096 | return result; | |
6097 | } | |
6098 | ||
c906108c | 6099 | static void |
fba45db2 | 6100 | packet_command (char *args, int from_tty) |
c906108c | 6101 | { |
d01949b6 | 6102 | struct remote_state *rs = get_remote_state (); |
c906108c | 6103 | |
c5aa993b | 6104 | if (!remote_desc) |
8a3fe4f8 | 6105 | error (_("command can only be used with remote target")); |
c906108c | 6106 | |
c5aa993b | 6107 | if (!args) |
8a3fe4f8 | 6108 | error (_("remote-packet command requires packet text as argument")); |
c906108c SS |
6109 | |
6110 | puts_filtered ("sending: "); | |
6111 | print_packet (args); | |
6112 | puts_filtered ("\n"); | |
6113 | putpkt (args); | |
6114 | ||
6d820c5c | 6115 | getpkt (&rs->buf, &rs->buf_size, 0); |
c906108c | 6116 | puts_filtered ("received: "); |
6d820c5c | 6117 | print_packet (rs->buf); |
c906108c SS |
6118 | puts_filtered ("\n"); |
6119 | } | |
6120 | ||
6121 | #if 0 | |
23860348 | 6122 | /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */ |
c906108c | 6123 | |
a14ed312 | 6124 | static void display_thread_info (struct gdb_ext_thread_info *info); |
c906108c | 6125 | |
a14ed312 | 6126 | static void threadset_test_cmd (char *cmd, int tty); |
c906108c | 6127 | |
a14ed312 | 6128 | static void threadalive_test (char *cmd, int tty); |
c906108c | 6129 | |
a14ed312 | 6130 | static void threadlist_test_cmd (char *cmd, int tty); |
c906108c | 6131 | |
23860348 | 6132 | int get_and_display_threadinfo (threadref *ref); |
c906108c | 6133 | |
a14ed312 | 6134 | static void threadinfo_test_cmd (char *cmd, int tty); |
c906108c | 6135 | |
23860348 | 6136 | static int thread_display_step (threadref *ref, void *context); |
c906108c | 6137 | |
a14ed312 | 6138 | static void threadlist_update_test_cmd (char *cmd, int tty); |
c906108c | 6139 | |
a14ed312 | 6140 | static void init_remote_threadtests (void); |
c906108c | 6141 | |
23860348 | 6142 | #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */ |
c906108c SS |
6143 | |
6144 | static void | |
fba45db2 | 6145 | threadset_test_cmd (char *cmd, int tty) |
c906108c SS |
6146 | { |
6147 | int sample_thread = SAMPLE_THREAD; | |
6148 | ||
a3f17187 | 6149 | printf_filtered (_("Remote threadset test\n")); |
c906108c SS |
6150 | set_thread (sample_thread, 1); |
6151 | } | |
6152 | ||
6153 | ||
6154 | static void | |
fba45db2 | 6155 | threadalive_test (char *cmd, int tty) |
c906108c SS |
6156 | { |
6157 | int sample_thread = SAMPLE_THREAD; | |
6158 | ||
39f77062 | 6159 | if (remote_thread_alive (pid_to_ptid (sample_thread))) |
c906108c SS |
6160 | printf_filtered ("PASS: Thread alive test\n"); |
6161 | else | |
6162 | printf_filtered ("FAIL: Thread alive test\n"); | |
6163 | } | |
6164 | ||
23860348 | 6165 | void output_threadid (char *title, threadref *ref); |
c906108c SS |
6166 | |
6167 | void | |
fba45db2 | 6168 | output_threadid (char *title, threadref *ref) |
c906108c SS |
6169 | { |
6170 | char hexid[20]; | |
6171 | ||
23860348 | 6172 | pack_threadid (&hexid[0], ref); /* Convert threead id into hex. */ |
c906108c SS |
6173 | hexid[16] = 0; |
6174 | printf_filtered ("%s %s\n", title, (&hexid[0])); | |
6175 | } | |
6176 | ||
6177 | static void | |
fba45db2 | 6178 | threadlist_test_cmd (char *cmd, int tty) |
c906108c SS |
6179 | { |
6180 | int startflag = 1; | |
6181 | threadref nextthread; | |
6182 | int done, result_count; | |
6183 | threadref threadlist[3]; | |
6184 | ||
6185 | printf_filtered ("Remote Threadlist test\n"); | |
6186 | if (!remote_get_threadlist (startflag, &nextthread, 3, &done, | |
6187 | &result_count, &threadlist[0])) | |
6188 | printf_filtered ("FAIL: threadlist test\n"); | |
6189 | else | |
6190 | { | |
6191 | threadref *scan = threadlist; | |
6192 | threadref *limit = scan + result_count; | |
6193 | ||
6194 | while (scan < limit) | |
6195 | output_threadid (" thread ", scan++); | |
6196 | } | |
6197 | } | |
6198 | ||
6199 | void | |
fba45db2 | 6200 | display_thread_info (struct gdb_ext_thread_info *info) |
c906108c SS |
6201 | { |
6202 | output_threadid ("Threadid: ", &info->threadid); | |
6203 | printf_filtered ("Name: %s\n ", info->shortname); | |
6204 | printf_filtered ("State: %s\n", info->display); | |
6205 | printf_filtered ("other: %s\n\n", info->more_display); | |
6206 | } | |
6207 | ||
6208 | int | |
fba45db2 | 6209 | get_and_display_threadinfo (threadref *ref) |
c906108c SS |
6210 | { |
6211 | int result; | |
6212 | int set; | |
6213 | struct gdb_ext_thread_info threadinfo; | |
6214 | ||
6215 | set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME | |
6216 | | TAG_MOREDISPLAY | TAG_DISPLAY; | |
6217 | if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo))) | |
6218 | display_thread_info (&threadinfo); | |
6219 | return result; | |
6220 | } | |
6221 | ||
6222 | static void | |
fba45db2 | 6223 | threadinfo_test_cmd (char *cmd, int tty) |
c906108c SS |
6224 | { |
6225 | int athread = SAMPLE_THREAD; | |
6226 | threadref thread; | |
6227 | int set; | |
6228 | ||
6229 | int_to_threadref (&thread, athread); | |
6230 | printf_filtered ("Remote Threadinfo test\n"); | |
6231 | if (!get_and_display_threadinfo (&thread)) | |
6232 | printf_filtered ("FAIL cannot get thread info\n"); | |
6233 | } | |
6234 | ||
6235 | static int | |
fba45db2 | 6236 | thread_display_step (threadref *ref, void *context) |
c906108c SS |
6237 | { |
6238 | /* output_threadid(" threadstep ",ref); *//* simple test */ | |
6239 | return get_and_display_threadinfo (ref); | |
6240 | } | |
6241 | ||
6242 | static void | |
fba45db2 | 6243 | threadlist_update_test_cmd (char *cmd, int tty) |
c906108c SS |
6244 | { |
6245 | printf_filtered ("Remote Threadlist update test\n"); | |
6246 | remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS); | |
6247 | } | |
6248 | ||
6249 | static void | |
6250 | init_remote_threadtests (void) | |
6251 | { | |
1bedd215 AC |
6252 | add_com ("tlist", class_obscure, threadlist_test_cmd, _("\ |
6253 | Fetch and print the remote list of thread identifiers, one pkt only")); | |
c906108c | 6254 | add_com ("tinfo", class_obscure, threadinfo_test_cmd, |
1bedd215 | 6255 | _("Fetch and display info about one thread")); |
c906108c | 6256 | add_com ("tset", class_obscure, threadset_test_cmd, |
1bedd215 | 6257 | _("Test setting to a different thread")); |
c906108c | 6258 | add_com ("tupd", class_obscure, threadlist_update_test_cmd, |
1bedd215 | 6259 | _("Iterate through updating all remote thread info")); |
c906108c | 6260 | add_com ("talive", class_obscure, threadalive_test, |
1bedd215 | 6261 | _(" Remote thread alive test ")); |
c906108c SS |
6262 | } |
6263 | ||
6264 | #endif /* 0 */ | |
6265 | ||
f3fb8c85 MS |
6266 | /* Convert a thread ID to a string. Returns the string in a static |
6267 | buffer. */ | |
6268 | ||
6269 | static char * | |
39f77062 | 6270 | remote_pid_to_str (ptid_t ptid) |
f3fb8c85 | 6271 | { |
fd0a2a6f | 6272 | static char buf[32]; |
f3fb8c85 | 6273 | |
32a5b2f1 | 6274 | xsnprintf (buf, sizeof buf, "Thread %d", ptid_get_pid (ptid)); |
f3fb8c85 MS |
6275 | return buf; |
6276 | } | |
6277 | ||
38691318 KB |
6278 | /* Get the address of the thread local variable in OBJFILE which is |
6279 | stored at OFFSET within the thread local storage for thread PTID. */ | |
6280 | ||
6281 | static CORE_ADDR | |
6282 | remote_get_thread_local_address (ptid_t ptid, CORE_ADDR lm, CORE_ADDR offset) | |
6283 | { | |
444abaca | 6284 | if (remote_protocol_packets[PACKET_qGetTLSAddr].support != PACKET_DISABLE) |
38691318 KB |
6285 | { |
6286 | struct remote_state *rs = get_remote_state (); | |
6d820c5c | 6287 | char *p = rs->buf; |
571dd617 | 6288 | enum packet_result result; |
38691318 KB |
6289 | |
6290 | strcpy (p, "qGetTLSAddr:"); | |
6291 | p += strlen (p); | |
6292 | p += hexnumstr (p, PIDGET (ptid)); | |
6293 | *p++ = ','; | |
6294 | p += hexnumstr (p, offset); | |
6295 | *p++ = ','; | |
6296 | p += hexnumstr (p, lm); | |
6297 | *p++ = '\0'; | |
6298 | ||
6d820c5c DJ |
6299 | putpkt (rs->buf); |
6300 | getpkt (&rs->buf, &rs->buf_size, 0); | |
6301 | result = packet_ok (rs->buf, &remote_protocol_packets[PACKET_qGetTLSAddr]); | |
571dd617 | 6302 | if (result == PACKET_OK) |
38691318 KB |
6303 | { |
6304 | ULONGEST result; | |
6305 | ||
6d820c5c | 6306 | unpack_varlen_hex (rs->buf, &result); |
38691318 KB |
6307 | return result; |
6308 | } | |
571dd617 | 6309 | else if (result == PACKET_UNKNOWN) |
109c3e39 AC |
6310 | throw_error (TLS_GENERIC_ERROR, |
6311 | _("Remote target doesn't support qGetTLSAddr packet")); | |
38691318 | 6312 | else |
109c3e39 AC |
6313 | throw_error (TLS_GENERIC_ERROR, |
6314 | _("Remote target failed to process qGetTLSAddr request")); | |
38691318 KB |
6315 | } |
6316 | else | |
109c3e39 AC |
6317 | throw_error (TLS_GENERIC_ERROR, |
6318 | _("TLS not supported or disabled on this target")); | |
38691318 KB |
6319 | /* Not reached. */ |
6320 | return 0; | |
6321 | } | |
6322 | ||
29709017 DJ |
6323 | /* Support for inferring a target description based on the current |
6324 | architecture and the size of a 'g' packet. While the 'g' packet | |
6325 | can have any size (since optional registers can be left off the | |
6326 | end), some sizes are easily recognizable given knowledge of the | |
6327 | approximate architecture. */ | |
6328 | ||
6329 | struct remote_g_packet_guess | |
6330 | { | |
6331 | int bytes; | |
6332 | const struct target_desc *tdesc; | |
6333 | }; | |
6334 | typedef struct remote_g_packet_guess remote_g_packet_guess_s; | |
6335 | DEF_VEC_O(remote_g_packet_guess_s); | |
6336 | ||
6337 | struct remote_g_packet_data | |
6338 | { | |
6339 | VEC(remote_g_packet_guess_s) *guesses; | |
6340 | }; | |
6341 | ||
6342 | static struct gdbarch_data *remote_g_packet_data_handle; | |
6343 | ||
6344 | static void * | |
6345 | remote_g_packet_data_init (struct obstack *obstack) | |
6346 | { | |
6347 | return OBSTACK_ZALLOC (obstack, struct remote_g_packet_data); | |
6348 | } | |
6349 | ||
6350 | void | |
6351 | register_remote_g_packet_guess (struct gdbarch *gdbarch, int bytes, | |
6352 | const struct target_desc *tdesc) | |
6353 | { | |
6354 | struct remote_g_packet_data *data | |
6355 | = gdbarch_data (gdbarch, remote_g_packet_data_handle); | |
6356 | struct remote_g_packet_guess new_guess, *guess; | |
6357 | int ix; | |
6358 | ||
6359 | gdb_assert (tdesc != NULL); | |
6360 | ||
6361 | for (ix = 0; | |
6362 | VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess); | |
6363 | ix++) | |
6364 | if (guess->bytes == bytes) | |
6365 | internal_error (__FILE__, __LINE__, | |
6366 | "Duplicate g packet description added for size %d", | |
6367 | bytes); | |
6368 | ||
6369 | new_guess.bytes = bytes; | |
6370 | new_guess.tdesc = tdesc; | |
6371 | VEC_safe_push (remote_g_packet_guess_s, data->guesses, &new_guess); | |
6372 | } | |
6373 | ||
6374 | static const struct target_desc * | |
6375 | remote_read_description (struct target_ops *target) | |
6376 | { | |
6377 | struct remote_g_packet_data *data | |
6378 | = gdbarch_data (current_gdbarch, remote_g_packet_data_handle); | |
6379 | ||
6380 | if (!VEC_empty (remote_g_packet_guess_s, data->guesses)) | |
6381 | { | |
6382 | struct remote_g_packet_guess *guess; | |
6383 | int ix; | |
6384 | int bytes = send_g_packet (); | |
6385 | ||
6386 | for (ix = 0; | |
6387 | VEC_iterate (remote_g_packet_guess_s, data->guesses, ix, guess); | |
6388 | ix++) | |
6389 | if (guess->bytes == bytes) | |
6390 | return guess->tdesc; | |
6391 | ||
6392 | /* We discard the g packet. A minor optimization would be to | |
6393 | hold on to it, and fill the register cache once we have selected | |
6394 | an architecture, but it's too tricky to do safely. */ | |
6395 | } | |
6396 | ||
6397 | return NULL; | |
6398 | } | |
6399 | ||
a6b151f1 DJ |
6400 | /* Remote file transfer support. This is host-initiated I/O, not |
6401 | target-initiated; for target-initiated, see remote-fileio.c. */ | |
6402 | ||
6403 | /* If *LEFT is at least the length of STRING, copy STRING to | |
6404 | *BUFFER, update *BUFFER to point to the new end of the buffer, and | |
6405 | decrease *LEFT. Otherwise raise an error. */ | |
6406 | ||
6407 | static void | |
6408 | remote_buffer_add_string (char **buffer, int *left, char *string) | |
6409 | { | |
6410 | int len = strlen (string); | |
6411 | ||
6412 | if (len > *left) | |
6413 | error (_("Packet too long for target.")); | |
6414 | ||
6415 | memcpy (*buffer, string, len); | |
6416 | *buffer += len; | |
6417 | *left -= len; | |
6418 | ||
6419 | /* NUL-terminate the buffer as a convenience, if there is | |
6420 | room. */ | |
6421 | if (*left) | |
6422 | **buffer = '\0'; | |
6423 | } | |
6424 | ||
6425 | /* If *LEFT is large enough, hex encode LEN bytes from BYTES into | |
6426 | *BUFFER, update *BUFFER to point to the new end of the buffer, and | |
6427 | decrease *LEFT. Otherwise raise an error. */ | |
6428 | ||
6429 | static void | |
6430 | remote_buffer_add_bytes (char **buffer, int *left, const gdb_byte *bytes, | |
6431 | int len) | |
6432 | { | |
6433 | if (2 * len > *left) | |
6434 | error (_("Packet too long for target.")); | |
6435 | ||
6436 | bin2hex (bytes, *buffer, len); | |
6437 | *buffer += 2 * len; | |
6438 | *left -= 2 * len; | |
6439 | ||
6440 | /* NUL-terminate the buffer as a convenience, if there is | |
6441 | room. */ | |
6442 | if (*left) | |
6443 | **buffer = '\0'; | |
6444 | } | |
6445 | ||
6446 | /* If *LEFT is large enough, convert VALUE to hex and add it to | |
6447 | *BUFFER, update *BUFFER to point to the new end of the buffer, and | |
6448 | decrease *LEFT. Otherwise raise an error. */ | |
6449 | ||
6450 | static void | |
6451 | remote_buffer_add_int (char **buffer, int *left, ULONGEST value) | |
6452 | { | |
6453 | int len = hexnumlen (value); | |
6454 | ||
6455 | if (len > *left) | |
6456 | error (_("Packet too long for target.")); | |
6457 | ||
6458 | hexnumstr (*buffer, value); | |
6459 | *buffer += len; | |
6460 | *left -= len; | |
6461 | ||
6462 | /* NUL-terminate the buffer as a convenience, if there is | |
6463 | room. */ | |
6464 | if (*left) | |
6465 | **buffer = '\0'; | |
6466 | } | |
6467 | ||
6468 | /* Parse an I/O result packet from BUFFER. Set RETCODE to the return | |
6469 | value, *REMOTE_ERRNO to the remote error number or zero if none | |
6470 | was included, and *ATTACHMENT to point to the start of the annex | |
6471 | if any. The length of the packet isn't needed here; there may | |
6472 | be NUL bytes in BUFFER, but they will be after *ATTACHMENT. | |
6473 | ||
6474 | Return 0 if the packet could be parsed, -1 if it could not. If | |
6475 | -1 is returned, the other variables may not be initialized. */ | |
6476 | ||
6477 | static int | |
6478 | remote_hostio_parse_result (char *buffer, int *retcode, | |
6479 | int *remote_errno, char **attachment) | |
6480 | { | |
6481 | char *p, *p2; | |
6482 | ||
6483 | *remote_errno = 0; | |
6484 | *attachment = NULL; | |
6485 | ||
6486 | if (buffer[0] != 'F') | |
6487 | return -1; | |
6488 | ||
6489 | errno = 0; | |
6490 | *retcode = strtol (&buffer[1], &p, 16); | |
6491 | if (errno != 0 || p == &buffer[1]) | |
6492 | return -1; | |
6493 | ||
6494 | /* Check for ",errno". */ | |
6495 | if (*p == ',') | |
6496 | { | |
6497 | errno = 0; | |
6498 | *remote_errno = strtol (p + 1, &p2, 16); | |
6499 | if (errno != 0 || p + 1 == p2) | |
6500 | return -1; | |
6501 | p = p2; | |
6502 | } | |
6503 | ||
6504 | /* Check for ";attachment". If there is no attachment, the | |
6505 | packet should end here. */ | |
6506 | if (*p == ';') | |
6507 | { | |
6508 | *attachment = p + 1; | |
6509 | return 0; | |
6510 | } | |
6511 | else if (*p == '\0') | |
6512 | return 0; | |
6513 | else | |
6514 | return -1; | |
6515 | } | |
6516 | ||
6517 | /* Send a prepared I/O packet to the target and read its response. | |
6518 | The prepared packet is in the global RS->BUF before this function | |
6519 | is called, and the answer is there when we return. | |
6520 | ||
6521 | COMMAND_BYTES is the length of the request to send, which may include | |
6522 | binary data. WHICH_PACKET is the packet configuration to check | |
6523 | before attempting a packet. If an error occurs, *REMOTE_ERRNO | |
6524 | is set to the error number and -1 is returned. Otherwise the value | |
6525 | returned by the function is returned. | |
6526 | ||
6527 | ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an | |
6528 | attachment is expected; an error will be reported if there's a | |
6529 | mismatch. If one is found, *ATTACHMENT will be set to point into | |
6530 | the packet buffer and *ATTACHMENT_LEN will be set to the | |
6531 | attachment's length. */ | |
6532 | ||
6533 | static int | |
6534 | remote_hostio_send_command (int command_bytes, int which_packet, | |
6535 | int *remote_errno, char **attachment, | |
6536 | int *attachment_len) | |
6537 | { | |
6538 | struct remote_state *rs = get_remote_state (); | |
6539 | int ret, bytes_read; | |
6540 | char *attachment_tmp; | |
6541 | ||
6542 | if (remote_protocol_packets[which_packet].support == PACKET_DISABLE) | |
6543 | { | |
6544 | *remote_errno = FILEIO_ENOSYS; | |
6545 | return -1; | |
6546 | } | |
6547 | ||
6548 | putpkt_binary (rs->buf, command_bytes); | |
6549 | bytes_read = getpkt_sane (&rs->buf, &rs->buf_size, 0); | |
6550 | ||
6551 | /* If it timed out, something is wrong. Don't try to parse the | |
6552 | buffer. */ | |
6553 | if (bytes_read < 0) | |
6554 | { | |
6555 | *remote_errno = FILEIO_EINVAL; | |
6556 | return -1; | |
6557 | } | |
6558 | ||
6559 | switch (packet_ok (rs->buf, &remote_protocol_packets[which_packet])) | |
6560 | { | |
6561 | case PACKET_ERROR: | |
6562 | *remote_errno = FILEIO_EINVAL; | |
6563 | return -1; | |
6564 | case PACKET_UNKNOWN: | |
6565 | *remote_errno = FILEIO_ENOSYS; | |
6566 | return -1; | |
6567 | case PACKET_OK: | |
6568 | break; | |
6569 | } | |
6570 | ||
6571 | if (remote_hostio_parse_result (rs->buf, &ret, remote_errno, | |
6572 | &attachment_tmp)) | |
6573 | { | |
6574 | *remote_errno = FILEIO_EINVAL; | |
6575 | return -1; | |
6576 | } | |
6577 | ||
6578 | /* Make sure we saw an attachment if and only if we expected one. */ | |
6579 | if ((attachment_tmp == NULL && attachment != NULL) | |
6580 | || (attachment_tmp != NULL && attachment == NULL)) | |
6581 | { | |
6582 | *remote_errno = FILEIO_EINVAL; | |
6583 | return -1; | |
6584 | } | |
6585 | ||
6586 | /* If an attachment was found, it must point into the packet buffer; | |
6587 | work out how many bytes there were. */ | |
6588 | if (attachment_tmp != NULL) | |
6589 | { | |
6590 | *attachment = attachment_tmp; | |
6591 | *attachment_len = bytes_read - (*attachment - rs->buf); | |
6592 | } | |
6593 | ||
6594 | return ret; | |
6595 | } | |
6596 | ||
6597 | /* Open FILENAME on the remote target, using FLAGS and MODE. Return a | |
6598 | remote file descriptor, or -1 if an error occurs (and set | |
6599 | *REMOTE_ERRNO). */ | |
6600 | ||
6601 | static int | |
6602 | remote_hostio_open (const char *filename, int flags, int mode, | |
6603 | int *remote_errno) | |
6604 | { | |
6605 | struct remote_state *rs = get_remote_state (); | |
6606 | char *p = rs->buf; | |
6607 | int left = get_remote_packet_size () - 1; | |
6608 | ||
6609 | remote_buffer_add_string (&p, &left, "vFile:open:"); | |
6610 | ||
6611 | remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename, | |
6612 | strlen (filename)); | |
6613 | remote_buffer_add_string (&p, &left, ","); | |
6614 | ||
6615 | remote_buffer_add_int (&p, &left, flags); | |
6616 | remote_buffer_add_string (&p, &left, ","); | |
6617 | ||
6618 | remote_buffer_add_int (&p, &left, mode); | |
6619 | ||
6620 | return remote_hostio_send_command (p - rs->buf, PACKET_vFile_open, | |
6621 | remote_errno, NULL, NULL); | |
6622 | } | |
6623 | ||
6624 | /* Write up to LEN bytes from WRITE_BUF to FD on the remote target. | |
6625 | Return the number of bytes written, or -1 if an error occurs (and | |
6626 | set *REMOTE_ERRNO). */ | |
6627 | ||
6628 | static int | |
6629 | remote_hostio_pwrite (int fd, const gdb_byte *write_buf, int len, | |
6630 | ULONGEST offset, int *remote_errno) | |
6631 | { | |
6632 | struct remote_state *rs = get_remote_state (); | |
6633 | char *p = rs->buf; | |
6634 | int left = get_remote_packet_size (); | |
6635 | int out_len; | |
6636 | ||
6637 | remote_buffer_add_string (&p, &left, "vFile:pwrite:"); | |
6638 | ||
6639 | remote_buffer_add_int (&p, &left, fd); | |
6640 | remote_buffer_add_string (&p, &left, ","); | |
6641 | ||
6642 | remote_buffer_add_int (&p, &left, offset); | |
6643 | remote_buffer_add_string (&p, &left, ","); | |
6644 | ||
6645 | p += remote_escape_output (write_buf, len, p, &out_len, | |
6646 | get_remote_packet_size () - (p - rs->buf)); | |
6647 | ||
6648 | return remote_hostio_send_command (p - rs->buf, PACKET_vFile_pwrite, | |
6649 | remote_errno, NULL, NULL); | |
6650 | } | |
6651 | ||
6652 | /* Read up to LEN bytes FD on the remote target into READ_BUF | |
6653 | Return the number of bytes read, or -1 if an error occurs (and | |
6654 | set *REMOTE_ERRNO). */ | |
6655 | ||
6656 | static int | |
6657 | remote_hostio_pread (int fd, gdb_byte *read_buf, int len, | |
6658 | ULONGEST offset, int *remote_errno) | |
6659 | { | |
6660 | struct remote_state *rs = get_remote_state (); | |
6661 | char *p = rs->buf; | |
6662 | char *attachment; | |
6663 | int left = get_remote_packet_size (); | |
6664 | int ret, attachment_len; | |
6665 | int read_len; | |
6666 | ||
6667 | remote_buffer_add_string (&p, &left, "vFile:pread:"); | |
6668 | ||
6669 | remote_buffer_add_int (&p, &left, fd); | |
6670 | remote_buffer_add_string (&p, &left, ","); | |
6671 | ||
6672 | remote_buffer_add_int (&p, &left, len); | |
6673 | remote_buffer_add_string (&p, &left, ","); | |
6674 | ||
6675 | remote_buffer_add_int (&p, &left, offset); | |
6676 | ||
6677 | ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_pread, | |
6678 | remote_errno, &attachment, | |
6679 | &attachment_len); | |
6680 | ||
6681 | if (ret < 0) | |
6682 | return ret; | |
6683 | ||
6684 | read_len = remote_unescape_input (attachment, attachment_len, | |
6685 | read_buf, len); | |
6686 | if (read_len != ret) | |
6687 | error (_("Read returned %d, but %d bytes."), ret, (int) read_len); | |
6688 | ||
6689 | return ret; | |
6690 | } | |
6691 | ||
6692 | /* Close FD on the remote target. Return 0, or -1 if an error occurs | |
6693 | (and set *REMOTE_ERRNO). */ | |
6694 | ||
6695 | static int | |
6696 | remote_hostio_close (int fd, int *remote_errno) | |
6697 | { | |
6698 | struct remote_state *rs = get_remote_state (); | |
6699 | char *p = rs->buf; | |
6700 | int left = get_remote_packet_size () - 1; | |
6701 | ||
6702 | remote_buffer_add_string (&p, &left, "vFile:close:"); | |
6703 | ||
6704 | remote_buffer_add_int (&p, &left, fd); | |
6705 | ||
6706 | return remote_hostio_send_command (p - rs->buf, PACKET_vFile_close, | |
6707 | remote_errno, NULL, NULL); | |
6708 | } | |
6709 | ||
6710 | /* Unlink FILENAME on the remote target. Return 0, or -1 if an error | |
6711 | occurs (and set *REMOTE_ERRNO). */ | |
6712 | ||
6713 | static int | |
6714 | remote_hostio_unlink (const char *filename, int *remote_errno) | |
6715 | { | |
6716 | struct remote_state *rs = get_remote_state (); | |
6717 | char *p = rs->buf; | |
6718 | int left = get_remote_packet_size () - 1; | |
6719 | ||
6720 | remote_buffer_add_string (&p, &left, "vFile:unlink:"); | |
6721 | ||
6722 | remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename, | |
6723 | strlen (filename)); | |
6724 | ||
6725 | return remote_hostio_send_command (p - rs->buf, PACKET_vFile_unlink, | |
6726 | remote_errno, NULL, NULL); | |
6727 | } | |
6728 | ||
6729 | static int | |
6730 | remote_fileio_errno_to_host (int errnum) | |
6731 | { | |
6732 | switch (errnum) | |
6733 | { | |
6734 | case FILEIO_EPERM: | |
6735 | return EPERM; | |
6736 | case FILEIO_ENOENT: | |
6737 | return ENOENT; | |
6738 | case FILEIO_EINTR: | |
6739 | return EINTR; | |
6740 | case FILEIO_EIO: | |
6741 | return EIO; | |
6742 | case FILEIO_EBADF: | |
6743 | return EBADF; | |
6744 | case FILEIO_EACCES: | |
6745 | return EACCES; | |
6746 | case FILEIO_EFAULT: | |
6747 | return EFAULT; | |
6748 | case FILEIO_EBUSY: | |
6749 | return EBUSY; | |
6750 | case FILEIO_EEXIST: | |
6751 | return EEXIST; | |
6752 | case FILEIO_ENODEV: | |
6753 | return ENODEV; | |
6754 | case FILEIO_ENOTDIR: | |
6755 | return ENOTDIR; | |
6756 | case FILEIO_EISDIR: | |
6757 | return EISDIR; | |
6758 | case FILEIO_EINVAL: | |
6759 | return EINVAL; | |
6760 | case FILEIO_ENFILE: | |
6761 | return ENFILE; | |
6762 | case FILEIO_EMFILE: | |
6763 | return EMFILE; | |
6764 | case FILEIO_EFBIG: | |
6765 | return EFBIG; | |
6766 | case FILEIO_ENOSPC: | |
6767 | return ENOSPC; | |
6768 | case FILEIO_ESPIPE: | |
6769 | return ESPIPE; | |
6770 | case FILEIO_EROFS: | |
6771 | return EROFS; | |
6772 | case FILEIO_ENOSYS: | |
6773 | return ENOSYS; | |
6774 | case FILEIO_ENAMETOOLONG: | |
6775 | return ENAMETOOLONG; | |
6776 | } | |
6777 | return -1; | |
6778 | } | |
6779 | ||
6780 | static char * | |
6781 | remote_hostio_error (int errnum) | |
6782 | { | |
6783 | int host_error = remote_fileio_errno_to_host (errnum); | |
6784 | ||
6785 | if (host_error == -1) | |
6786 | error (_("Unknown remote I/O error %d"), errnum); | |
6787 | else | |
6788 | error (_("Remote I/O error: %s"), safe_strerror (host_error)); | |
6789 | } | |
6790 | ||
6791 | static void | |
6792 | fclose_cleanup (void *file) | |
6793 | { | |
6794 | fclose (file); | |
6795 | } | |
6796 | ||
6797 | static void | |
6798 | remote_hostio_close_cleanup (void *opaque) | |
6799 | { | |
6800 | int fd = *(int *) opaque; | |
6801 | int remote_errno; | |
6802 | ||
6803 | remote_hostio_close (fd, &remote_errno); | |
6804 | } | |
6805 | ||
6806 | void | |
6807 | remote_file_put (const char *local_file, const char *remote_file, int from_tty) | |
6808 | { | |
6809 | struct cleanup *back_to, *close_cleanup; | |
6810 | int retcode, fd, remote_errno, bytes, io_size; | |
6811 | FILE *file; | |
6812 | gdb_byte *buffer; | |
6813 | int bytes_in_buffer; | |
6814 | int saw_eof; | |
6815 | ULONGEST offset; | |
6816 | ||
6817 | if (!remote_desc) | |
6818 | error (_("command can only be used with remote target")); | |
6819 | ||
6820 | file = fopen (local_file, "rb"); | |
6821 | if (file == NULL) | |
6822 | perror_with_name (local_file); | |
6823 | back_to = make_cleanup (fclose_cleanup, file); | |
6824 | ||
6825 | fd = remote_hostio_open (remote_file, (FILEIO_O_WRONLY | FILEIO_O_CREAT | |
6826 | | FILEIO_O_TRUNC), | |
6827 | 0700, &remote_errno); | |
6828 | if (fd == -1) | |
6829 | remote_hostio_error (remote_errno); | |
6830 | ||
6831 | /* Send up to this many bytes at once. They won't all fit in the | |
6832 | remote packet limit, so we'll transfer slightly fewer. */ | |
6833 | io_size = get_remote_packet_size (); | |
6834 | buffer = xmalloc (io_size); | |
6835 | make_cleanup (xfree, buffer); | |
6836 | ||
6837 | close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd); | |
6838 | ||
6839 | bytes_in_buffer = 0; | |
6840 | saw_eof = 0; | |
6841 | offset = 0; | |
6842 | while (bytes_in_buffer || !saw_eof) | |
6843 | { | |
6844 | if (!saw_eof) | |
6845 | { | |
6846 | bytes = fread (buffer + bytes_in_buffer, 1, io_size - bytes_in_buffer, | |
6847 | file); | |
6848 | if (bytes == 0) | |
6849 | { | |
6850 | if (ferror (file)) | |
6851 | error (_("Error reading %s."), local_file); | |
6852 | else | |
6853 | { | |
6854 | /* EOF. Unless there is something still in the | |
6855 | buffer from the last iteration, we are done. */ | |
6856 | saw_eof = 1; | |
6857 | if (bytes_in_buffer == 0) | |
6858 | break; | |
6859 | } | |
6860 | } | |
6861 | } | |
6862 | else | |
6863 | bytes = 0; | |
6864 | ||
6865 | bytes += bytes_in_buffer; | |
6866 | bytes_in_buffer = 0; | |
6867 | ||
6868 | retcode = remote_hostio_pwrite (fd, buffer, bytes, offset, &remote_errno); | |
6869 | ||
6870 | if (retcode < 0) | |
6871 | remote_hostio_error (remote_errno); | |
6872 | else if (retcode == 0) | |
6873 | error (_("Remote write of %d bytes returned 0!"), bytes); | |
6874 | else if (retcode < bytes) | |
6875 | { | |
6876 | /* Short write. Save the rest of the read data for the next | |
6877 | write. */ | |
6878 | bytes_in_buffer = bytes - retcode; | |
6879 | memmove (buffer, buffer + retcode, bytes_in_buffer); | |
6880 | } | |
6881 | ||
6882 | offset += retcode; | |
6883 | } | |
6884 | ||
6885 | discard_cleanups (close_cleanup); | |
6886 | if (remote_hostio_close (fd, &remote_errno)) | |
6887 | remote_hostio_error (remote_errno); | |
6888 | ||
6889 | if (from_tty) | |
6890 | printf_filtered (_("Successfully sent file \"%s\".\n"), local_file); | |
6891 | do_cleanups (back_to); | |
6892 | } | |
6893 | ||
6894 | void | |
6895 | remote_file_get (const char *remote_file, const char *local_file, int from_tty) | |
6896 | { | |
6897 | struct cleanup *back_to, *close_cleanup; | |
6898 | int retcode, fd, remote_errno, bytes, io_size; | |
6899 | FILE *file; | |
6900 | gdb_byte *buffer; | |
6901 | ULONGEST offset; | |
6902 | ||
6903 | if (!remote_desc) | |
6904 | error (_("command can only be used with remote target")); | |
6905 | ||
6906 | fd = remote_hostio_open (remote_file, FILEIO_O_RDONLY, 0, &remote_errno); | |
6907 | if (fd == -1) | |
6908 | remote_hostio_error (remote_errno); | |
6909 | ||
6910 | file = fopen (local_file, "wb"); | |
6911 | if (file == NULL) | |
6912 | perror_with_name (local_file); | |
6913 | back_to = make_cleanup (fclose_cleanup, file); | |
6914 | ||
6915 | /* Send up to this many bytes at once. They won't all fit in the | |
6916 | remote packet limit, so we'll transfer slightly fewer. */ | |
6917 | io_size = get_remote_packet_size (); | |
6918 | buffer = xmalloc (io_size); | |
6919 | make_cleanup (xfree, buffer); | |
6920 | ||
6921 | close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd); | |
6922 | ||
6923 | offset = 0; | |
6924 | while (1) | |
6925 | { | |
6926 | bytes = remote_hostio_pread (fd, buffer, io_size, offset, &remote_errno); | |
6927 | if (bytes == 0) | |
6928 | /* Success, but no bytes, means end-of-file. */ | |
6929 | break; | |
6930 | if (bytes == -1) | |
6931 | remote_hostio_error (remote_errno); | |
6932 | ||
6933 | offset += bytes; | |
6934 | ||
6935 | bytes = fwrite (buffer, 1, bytes, file); | |
6936 | if (bytes == 0) | |
6937 | perror_with_name (local_file); | |
6938 | } | |
6939 | ||
6940 | discard_cleanups (close_cleanup); | |
6941 | if (remote_hostio_close (fd, &remote_errno)) | |
6942 | remote_hostio_error (remote_errno); | |
6943 | ||
6944 | if (from_tty) | |
6945 | printf_filtered (_("Successfully fetched file \"%s\".\n"), remote_file); | |
6946 | do_cleanups (back_to); | |
6947 | } | |
6948 | ||
6949 | void | |
6950 | remote_file_delete (const char *remote_file, int from_tty) | |
6951 | { | |
6952 | int retcode, remote_errno; | |
6953 | ||
6954 | if (!remote_desc) | |
6955 | error (_("command can only be used with remote target")); | |
6956 | ||
6957 | retcode = remote_hostio_unlink (remote_file, &remote_errno); | |
6958 | if (retcode == -1) | |
6959 | remote_hostio_error (remote_errno); | |
6960 | ||
6961 | if (from_tty) | |
6962 | printf_filtered (_("Successfully deleted file \"%s\".\n"), remote_file); | |
6963 | } | |
6964 | ||
6965 | static void | |
6966 | remote_put_command (char *args, int from_tty) | |
6967 | { | |
6968 | struct cleanup *back_to; | |
6969 | char **argv; | |
6970 | ||
6971 | argv = buildargv (args); | |
6972 | if (argv == NULL) | |
6973 | nomem (0); | |
6974 | back_to = make_cleanup_freeargv (argv); | |
6975 | if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL) | |
6976 | error (_("Invalid parameters to remote put")); | |
6977 | ||
6978 | remote_file_put (argv[0], argv[1], from_tty); | |
6979 | ||
6980 | do_cleanups (back_to); | |
6981 | } | |
6982 | ||
6983 | static void | |
6984 | remote_get_command (char *args, int from_tty) | |
6985 | { | |
6986 | struct cleanup *back_to; | |
6987 | char **argv; | |
6988 | ||
6989 | argv = buildargv (args); | |
6990 | if (argv == NULL) | |
6991 | nomem (0); | |
6992 | back_to = make_cleanup_freeargv (argv); | |
6993 | if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL) | |
6994 | error (_("Invalid parameters to remote get")); | |
6995 | ||
6996 | remote_file_get (argv[0], argv[1], from_tty); | |
6997 | ||
6998 | do_cleanups (back_to); | |
6999 | } | |
7000 | ||
7001 | static void | |
7002 | remote_delete_command (char *args, int from_tty) | |
7003 | { | |
7004 | struct cleanup *back_to; | |
7005 | char **argv; | |
7006 | ||
7007 | argv = buildargv (args); | |
7008 | if (argv == NULL) | |
7009 | nomem (0); | |
7010 | back_to = make_cleanup_freeargv (argv); | |
7011 | if (argv[0] == NULL || argv[1] != NULL) | |
7012 | error (_("Invalid parameters to remote delete")); | |
7013 | ||
7014 | remote_file_delete (argv[0], from_tty); | |
7015 | ||
7016 | do_cleanups (back_to); | |
7017 | } | |
7018 | ||
7019 | static void | |
7020 | remote_command (char *args, int from_tty) | |
7021 | { | |
7022 | help_list (remote_cmdlist, "remote ", -1, gdb_stdout); | |
7023 | } | |
7024 | ||
c906108c | 7025 | static void |
fba45db2 | 7026 | init_remote_ops (void) |
c906108c | 7027 | { |
c5aa993b | 7028 | remote_ops.to_shortname = "remote"; |
c906108c | 7029 | remote_ops.to_longname = "Remote serial target in gdb-specific protocol"; |
c5aa993b | 7030 | remote_ops.to_doc = |
c906108c | 7031 | "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ |
0d06e24b JM |
7032 | Specify the serial device it is connected to\n\ |
7033 | (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."; | |
c5aa993b JM |
7034 | remote_ops.to_open = remote_open; |
7035 | remote_ops.to_close = remote_close; | |
c906108c | 7036 | remote_ops.to_detach = remote_detach; |
6ad8ae5c | 7037 | remote_ops.to_disconnect = remote_disconnect; |
c5aa993b | 7038 | remote_ops.to_resume = remote_resume; |
c906108c SS |
7039 | remote_ops.to_wait = remote_wait; |
7040 | remote_ops.to_fetch_registers = remote_fetch_registers; | |
7041 | remote_ops.to_store_registers = remote_store_registers; | |
7042 | remote_ops.to_prepare_to_store = remote_prepare_to_store; | |
c8e73a31 | 7043 | remote_ops.deprecated_xfer_memory = remote_xfer_memory; |
c5aa993b | 7044 | remote_ops.to_files_info = remote_files_info; |
c906108c SS |
7045 | remote_ops.to_insert_breakpoint = remote_insert_breakpoint; |
7046 | remote_ops.to_remove_breakpoint = remote_remove_breakpoint; | |
3c3bea1c GS |
7047 | remote_ops.to_stopped_by_watchpoint = remote_stopped_by_watchpoint; |
7048 | remote_ops.to_stopped_data_address = remote_stopped_data_address; | |
7049 | remote_ops.to_can_use_hw_breakpoint = remote_check_watch_resources; | |
7050 | remote_ops.to_insert_hw_breakpoint = remote_insert_hw_breakpoint; | |
7051 | remote_ops.to_remove_hw_breakpoint = remote_remove_hw_breakpoint; | |
7052 | remote_ops.to_insert_watchpoint = remote_insert_watchpoint; | |
7053 | remote_ops.to_remove_watchpoint = remote_remove_watchpoint; | |
c5aa993b JM |
7054 | remote_ops.to_kill = remote_kill; |
7055 | remote_ops.to_load = generic_load; | |
c906108c SS |
7056 | remote_ops.to_mourn_inferior = remote_mourn; |
7057 | remote_ops.to_thread_alive = remote_thread_alive; | |
0f71a2f6 | 7058 | remote_ops.to_find_new_threads = remote_threads_info; |
0caabb7e | 7059 | remote_ops.to_pid_to_str = remote_pid_to_str; |
cf759d3b | 7060 | remote_ops.to_extra_thread_info = remote_threads_extra_info; |
c906108c | 7061 | remote_ops.to_stop = remote_stop; |
4b8a223f | 7062 | remote_ops.to_xfer_partial = remote_xfer_partial; |
96baa820 | 7063 | remote_ops.to_rcmd = remote_rcmd; |
49d03eab | 7064 | remote_ops.to_log_command = serial_log_command; |
38691318 | 7065 | remote_ops.to_get_thread_local_address = remote_get_thread_local_address; |
c906108c | 7066 | remote_ops.to_stratum = process_stratum; |
c5aa993b JM |
7067 | remote_ops.to_has_all_memory = 1; |
7068 | remote_ops.to_has_memory = 1; | |
7069 | remote_ops.to_has_stack = 1; | |
7070 | remote_ops.to_has_registers = 1; | |
7071 | remote_ops.to_has_execution = 1; | |
7072 | remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */ | |
7073 | remote_ops.to_magic = OPS_MAGIC; | |
fd79ecee | 7074 | remote_ops.to_memory_map = remote_memory_map; |
a76d924d DJ |
7075 | remote_ops.to_flash_erase = remote_flash_erase; |
7076 | remote_ops.to_flash_done = remote_flash_done; | |
29709017 | 7077 | remote_ops.to_read_description = remote_read_description; |
08388c79 | 7078 | remote_ops.to_search_memory = remote_search_memory; |
75c99385 PA |
7079 | remote_ops.to_can_async_p = remote_can_async_p; |
7080 | remote_ops.to_is_async_p = remote_is_async_p; | |
7081 | remote_ops.to_async = remote_async; | |
7082 | remote_ops.to_async_mask = remote_async_mask; | |
7083 | remote_ops.to_terminal_inferior = remote_terminal_inferior; | |
7084 | remote_ops.to_terminal_ours = remote_terminal_ours; | |
c906108c SS |
7085 | } |
7086 | ||
7087 | /* Set up the extended remote vector by making a copy of the standard | |
7088 | remote vector and adding to it. */ | |
7089 | ||
7090 | static void | |
fba45db2 | 7091 | init_extended_remote_ops (void) |
c906108c SS |
7092 | { |
7093 | extended_remote_ops = remote_ops; | |
7094 | ||
0f71a2f6 | 7095 | extended_remote_ops.to_shortname = "extended-remote"; |
c5aa993b | 7096 | extended_remote_ops.to_longname = |
c906108c | 7097 | "Extended remote serial target in gdb-specific protocol"; |
c5aa993b | 7098 | extended_remote_ops.to_doc = |
c906108c | 7099 | "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ |
39237dd1 PA |
7100 | Specify the serial device it is connected to (e.g. /dev/ttya)."; |
7101 | extended_remote_ops.to_open = extended_remote_open; | |
c906108c SS |
7102 | extended_remote_ops.to_create_inferior = extended_remote_create_inferior; |
7103 | extended_remote_ops.to_mourn_inferior = extended_remote_mourn; | |
2d717e4f DJ |
7104 | extended_remote_ops.to_detach = extended_remote_detach; |
7105 | extended_remote_ops.to_attach = extended_remote_attach; | |
0f71a2f6 JM |
7106 | } |
7107 | ||
6426a772 JM |
7108 | static int |
7109 | remote_can_async_p (void) | |
7110 | { | |
75c99385 PA |
7111 | if (!remote_async_permitted) |
7112 | /* We only enable async when the user specifically asks for it. */ | |
7113 | return 0; | |
7114 | ||
23860348 | 7115 | /* We're async whenever the serial device is. */ |
b84876c2 | 7116 | return remote_async_mask_value && serial_can_async_p (remote_desc); |
6426a772 JM |
7117 | } |
7118 | ||
7119 | static int | |
7120 | remote_is_async_p (void) | |
7121 | { | |
75c99385 PA |
7122 | if (!remote_async_permitted) |
7123 | /* We only enable async when the user specifically asks for it. */ | |
7124 | return 0; | |
7125 | ||
23860348 | 7126 | /* We're async whenever the serial device is. */ |
b84876c2 | 7127 | return remote_async_mask_value && serial_is_async_p (remote_desc); |
6426a772 JM |
7128 | } |
7129 | ||
2acceee2 JM |
7130 | /* Pass the SERIAL event on and up to the client. One day this code |
7131 | will be able to delay notifying the client of an event until the | |
23860348 | 7132 | point where an entire packet has been received. */ |
2acceee2 | 7133 | |
2bc416ba | 7134 | static void (*async_client_callback) (enum inferior_event_type event_type, |
23860348 | 7135 | void *context); |
2acceee2 JM |
7136 | static void *async_client_context; |
7137 | static serial_event_ftype remote_async_serial_handler; | |
7138 | ||
6426a772 | 7139 | static void |
819cc324 | 7140 | remote_async_serial_handler (struct serial *scb, void *context) |
6426a772 | 7141 | { |
2acceee2 JM |
7142 | /* Don't propogate error information up to the client. Instead let |
7143 | the client find out about the error by querying the target. */ | |
7144 | async_client_callback (INF_REG_EVENT, async_client_context); | |
7145 | } | |
7146 | ||
7147 | static void | |
2bc416ba | 7148 | remote_async (void (*callback) (enum inferior_event_type event_type, |
23860348 | 7149 | void *context), void *context) |
2acceee2 | 7150 | { |
b84876c2 | 7151 | if (remote_async_mask_value == 0) |
8e65ff28 | 7152 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 7153 | _("Calling remote_async when async is masked")); |
ed9a39eb | 7154 | |
2acceee2 JM |
7155 | if (callback != NULL) |
7156 | { | |
2cd58942 | 7157 | serial_async (remote_desc, remote_async_serial_handler, NULL); |
2acceee2 JM |
7158 | async_client_callback = callback; |
7159 | async_client_context = context; | |
7160 | } | |
7161 | else | |
2cd58942 | 7162 | serial_async (remote_desc, NULL, NULL); |
6426a772 JM |
7163 | } |
7164 | ||
b84876c2 PA |
7165 | static int |
7166 | remote_async_mask (int new_mask) | |
7167 | { | |
7168 | int curr_mask = remote_async_mask_value; | |
7169 | remote_async_mask_value = new_mask; | |
7170 | return curr_mask; | |
7171 | } | |
7172 | ||
5a2468f5 | 7173 | static void |
c2d11a7d | 7174 | set_remote_cmd (char *args, int from_tty) |
5a2468f5 | 7175 | { |
427c3a89 | 7176 | help_list (remote_set_cmdlist, "set remote ", -1, gdb_stdout); |
5a2468f5 JM |
7177 | } |
7178 | ||
d471ea57 AC |
7179 | static void |
7180 | show_remote_cmd (char *args, int from_tty) | |
7181 | { | |
37a105a1 | 7182 | /* We can't just use cmd_show_list here, because we want to skip |
427c3a89 | 7183 | the redundant "show remote Z-packet" and the legacy aliases. */ |
37a105a1 DJ |
7184 | struct cleanup *showlist_chain; |
7185 | struct cmd_list_element *list = remote_show_cmdlist; | |
7186 | ||
7187 | showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist"); | |
7188 | for (; list != NULL; list = list->next) | |
7189 | if (strcmp (list->name, "Z-packet") == 0) | |
7190 | continue; | |
427c3a89 DJ |
7191 | else if (list->type == not_set_cmd) |
7192 | /* Alias commands are exactly like the original, except they | |
7193 | don't have the normal type. */ | |
7194 | continue; | |
7195 | else | |
37a105a1 DJ |
7196 | { |
7197 | struct cleanup *option_chain | |
7198 | = make_cleanup_ui_out_tuple_begin_end (uiout, "option"); | |
7199 | ui_out_field_string (uiout, "name", list->name); | |
7200 | ui_out_text (uiout, ": "); | |
427c3a89 DJ |
7201 | if (list->type == show_cmd) |
7202 | do_setshow_command ((char *) NULL, from_tty, list); | |
7203 | else | |
7204 | cmd_func (list, NULL, from_tty); | |
37a105a1 DJ |
7205 | /* Close the tuple. */ |
7206 | do_cleanups (option_chain); | |
7207 | } | |
427c3a89 DJ |
7208 | |
7209 | /* Close the tuple. */ | |
7210 | do_cleanups (showlist_chain); | |
d471ea57 | 7211 | } |
5a2468f5 | 7212 | |
0f71a2f6 | 7213 | |
23860348 | 7214 | /* Function to be called whenever a new objfile (shlib) is detected. */ |
dc8acb97 MS |
7215 | static void |
7216 | remote_new_objfile (struct objfile *objfile) | |
7217 | { | |
23860348 | 7218 | if (remote_desc != 0) /* Have a remote connection. */ |
06d3b283 | 7219 | remote_check_symbols (objfile); |
dc8acb97 MS |
7220 | } |
7221 | ||
c906108c | 7222 | void |
fba45db2 | 7223 | _initialize_remote (void) |
c906108c | 7224 | { |
ea9c271d DJ |
7225 | struct remote_state *rs; |
7226 | ||
0f71a2f6 | 7227 | /* architecture specific data */ |
2bc416ba | 7228 | remote_gdbarch_data_handle = |
23860348 | 7229 | gdbarch_data_register_post_init (init_remote_state); |
29709017 DJ |
7230 | remote_g_packet_data_handle = |
7231 | gdbarch_data_register_pre_init (remote_g_packet_data_init); | |
d01949b6 | 7232 | |
ea9c271d DJ |
7233 | /* Initialize the per-target state. At the moment there is only one |
7234 | of these, not one per target. Only one target is active at a | |
7235 | time. The default buffer size is unimportant; it will be expanded | |
7236 | whenever a larger buffer is needed. */ | |
0b83947e | 7237 | rs = get_remote_state_raw (); |
ea9c271d DJ |
7238 | rs->buf_size = 400; |
7239 | rs->buf = xmalloc (rs->buf_size); | |
7240 | ||
c906108c SS |
7241 | init_remote_ops (); |
7242 | add_target (&remote_ops); | |
7243 | ||
7244 | init_extended_remote_ops (); | |
7245 | add_target (&extended_remote_ops); | |
cce74817 | 7246 | |
dc8acb97 | 7247 | /* Hook into new objfile notification. */ |
06d3b283 | 7248 | observer_attach_new_objfile (remote_new_objfile); |
dc8acb97 | 7249 | |
b803fb0f DJ |
7250 | /* Set up signal handlers. */ |
7251 | sigint_remote_token = | |
7252 | create_async_signal_handler (async_remote_interrupt, NULL); | |
7253 | sigint_remote_twice_token = | |
7254 | create_async_signal_handler (inferior_event_handler_wrapper, NULL); | |
7255 | ||
c906108c SS |
7256 | #if 0 |
7257 | init_remote_threadtests (); | |
7258 | #endif | |
7259 | ||
23860348 | 7260 | /* set/show remote ... */ |
d471ea57 | 7261 | |
1bedd215 | 7262 | add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, _("\ |
5a2468f5 JM |
7263 | Remote protocol specific variables\n\ |
7264 | Configure various remote-protocol specific variables such as\n\ | |
1bedd215 | 7265 | the packets being used"), |
cff3e48b | 7266 | &remote_set_cmdlist, "set remote ", |
23860348 | 7267 | 0 /* allow-unknown */, &setlist); |
1bedd215 | 7268 | add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\ |
5a2468f5 JM |
7269 | Remote protocol specific variables\n\ |
7270 | Configure various remote-protocol specific variables such as\n\ | |
1bedd215 | 7271 | the packets being used"), |
cff3e48b | 7272 | &remote_show_cmdlist, "show remote ", |
23860348 | 7273 | 0 /* allow-unknown */, &showlist); |
5a2468f5 | 7274 | |
1a966eab AC |
7275 | add_cmd ("compare-sections", class_obscure, compare_sections_command, _("\ |
7276 | Compare section data on target to the exec file.\n\ | |
7277 | Argument is a single section name (default: all loaded sections)."), | |
c906108c SS |
7278 | &cmdlist); |
7279 | ||
1a966eab AC |
7280 | add_cmd ("packet", class_maintenance, packet_command, _("\ |
7281 | Send an arbitrary packet to a remote target.\n\ | |
c906108c SS |
7282 | maintenance packet TEXT\n\ |
7283 | If GDB is talking to an inferior via the GDB serial protocol, then\n\ | |
7284 | this command sends the string TEXT to the inferior, and displays the\n\ | |
7285 | response packet. GDB supplies the initial `$' character, and the\n\ | |
1a966eab | 7286 | terminating `#' character and checksum."), |
c906108c SS |
7287 | &maintenancelist); |
7288 | ||
7915a72c AC |
7289 | add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, _("\ |
7290 | Set whether to send break if interrupted."), _("\ | |
7291 | Show whether to send break if interrupted."), _("\ | |
7292 | If set, a break, instead of a cntrl-c, is sent to the remote target."), | |
2c5b56ce | 7293 | NULL, NULL, /* FIXME: i18n: Whether to send break if interrupted is %s. */ |
e707bbc2 | 7294 | &setlist, &showlist); |
c906108c | 7295 | |
23860348 | 7296 | /* Install commands for configuring memory read/write packets. */ |
11cf8741 | 7297 | |
1a966eab AC |
7298 | add_cmd ("remotewritesize", no_class, set_memory_write_packet_size, _("\ |
7299 | Set the maximum number of bytes per memory write packet (deprecated)."), | |
11cf8741 | 7300 | &setlist); |
1a966eab AC |
7301 | add_cmd ("remotewritesize", no_class, show_memory_write_packet_size, _("\ |
7302 | Show the maximum number of bytes per memory write packet (deprecated)."), | |
11cf8741 JM |
7303 | &showlist); |
7304 | add_cmd ("memory-write-packet-size", no_class, | |
1a966eab AC |
7305 | set_memory_write_packet_size, _("\ |
7306 | Set the maximum number of bytes per memory-write packet.\n\ | |
7307 | Specify the number of bytes in a packet or 0 (zero) for the\n\ | |
7308 | default packet size. The actual limit is further reduced\n\ | |
7309 | dependent on the target. Specify ``fixed'' to disable the\n\ | |
7310 | further restriction and ``limit'' to enable that restriction."), | |
11cf8741 JM |
7311 | &remote_set_cmdlist); |
7312 | add_cmd ("memory-read-packet-size", no_class, | |
1a966eab AC |
7313 | set_memory_read_packet_size, _("\ |
7314 | Set the maximum number of bytes per memory-read packet.\n\ | |
7315 | Specify the number of bytes in a packet or 0 (zero) for the\n\ | |
7316 | default packet size. The actual limit is further reduced\n\ | |
7317 | dependent on the target. Specify ``fixed'' to disable the\n\ | |
7318 | further restriction and ``limit'' to enable that restriction."), | |
11cf8741 JM |
7319 | &remote_set_cmdlist); |
7320 | add_cmd ("memory-write-packet-size", no_class, | |
7321 | show_memory_write_packet_size, | |
1a966eab | 7322 | _("Show the maximum number of bytes per memory-write packet."), |
11cf8741 JM |
7323 | &remote_show_cmdlist); |
7324 | add_cmd ("memory-read-packet-size", no_class, | |
7325 | show_memory_read_packet_size, | |
1a966eab | 7326 | _("Show the maximum number of bytes per memory-read packet."), |
11cf8741 | 7327 | &remote_show_cmdlist); |
c906108c | 7328 | |
b3f42336 | 7329 | add_setshow_zinteger_cmd ("hardware-watchpoint-limit", no_class, |
7915a72c AC |
7330 | &remote_hw_watchpoint_limit, _("\ |
7331 | Set the maximum number of target hardware watchpoints."), _("\ | |
7332 | Show the maximum number of target hardware watchpoints."), _("\ | |
7333 | Specify a negative limit for unlimited."), | |
2c5b56ce | 7334 | NULL, NULL, /* FIXME: i18n: The maximum number of target hardware watchpoints is %s. */ |
b3f42336 AC |
7335 | &remote_set_cmdlist, &remote_show_cmdlist); |
7336 | add_setshow_zinteger_cmd ("hardware-breakpoint-limit", no_class, | |
7915a72c AC |
7337 | &remote_hw_breakpoint_limit, _("\ |
7338 | Set the maximum number of target hardware breakpoints."), _("\ | |
7339 | Show the maximum number of target hardware breakpoints."), _("\ | |
7340 | Specify a negative limit for unlimited."), | |
2c5b56ce | 7341 | NULL, NULL, /* FIXME: i18n: The maximum number of target hardware breakpoints is %s. */ |
b3f42336 | 7342 | &remote_set_cmdlist, &remote_show_cmdlist); |
501eef12 | 7343 | |
4d28ad1e AC |
7344 | add_setshow_integer_cmd ("remoteaddresssize", class_obscure, |
7345 | &remote_address_size, _("\ | |
7346 | Set the maximum size of the address (in bits) in a memory packet."), _("\ | |
7347 | Show the maximum size of the address (in bits) in a memory packet."), NULL, | |
7348 | NULL, | |
7349 | NULL, /* FIXME: i18n: */ | |
7350 | &setlist, &showlist); | |
c906108c | 7351 | |
444abaca | 7352 | add_packet_config_cmd (&remote_protocol_packets[PACKET_X], |
bb572ddd | 7353 | "X", "binary-download", 1); |
0f71a2f6 | 7354 | |
444abaca | 7355 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vCont], |
bb572ddd | 7356 | "vCont", "verbose-resume", 0); |
506fb367 | 7357 | |
89be2091 DJ |
7358 | add_packet_config_cmd (&remote_protocol_packets[PACKET_QPassSignals], |
7359 | "QPassSignals", "pass-signals", 0); | |
7360 | ||
444abaca | 7361 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol], |
bb572ddd | 7362 | "qSymbol", "symbol-lookup", 0); |
dc8acb97 | 7363 | |
444abaca | 7364 | add_packet_config_cmd (&remote_protocol_packets[PACKET_P], |
bb572ddd | 7365 | "P", "set-register", 1); |
d471ea57 | 7366 | |
444abaca | 7367 | add_packet_config_cmd (&remote_protocol_packets[PACKET_p], |
bb572ddd | 7368 | "p", "fetch-register", 1); |
b96ec7ac | 7369 | |
444abaca | 7370 | add_packet_config_cmd (&remote_protocol_packets[PACKET_Z0], |
bb572ddd | 7371 | "Z0", "software-breakpoint", 0); |
d471ea57 | 7372 | |
444abaca | 7373 | add_packet_config_cmd (&remote_protocol_packets[PACKET_Z1], |
bb572ddd | 7374 | "Z1", "hardware-breakpoint", 0); |
d471ea57 | 7375 | |
444abaca | 7376 | add_packet_config_cmd (&remote_protocol_packets[PACKET_Z2], |
bb572ddd | 7377 | "Z2", "write-watchpoint", 0); |
d471ea57 | 7378 | |
444abaca | 7379 | add_packet_config_cmd (&remote_protocol_packets[PACKET_Z3], |
bb572ddd | 7380 | "Z3", "read-watchpoint", 0); |
d471ea57 | 7381 | |
444abaca | 7382 | add_packet_config_cmd (&remote_protocol_packets[PACKET_Z4], |
bb572ddd | 7383 | "Z4", "access-watchpoint", 0); |
d471ea57 | 7384 | |
0876f84a DJ |
7385 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_auxv], |
7386 | "qXfer:auxv:read", "read-aux-vector", 0); | |
802188a7 | 7387 | |
23181151 DJ |
7388 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_features], |
7389 | "qXfer:features:read", "target-features", 0); | |
7390 | ||
cfa9d6d9 DJ |
7391 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries], |
7392 | "qXfer:libraries:read", "library-info", 0); | |
7393 | ||
fd79ecee DJ |
7394 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_memory_map], |
7395 | "qXfer:memory-map:read", "memory-map", 0); | |
7396 | ||
0e7f50da UW |
7397 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_read], |
7398 | "qXfer:spu:read", "read-spu-object", 0); | |
7399 | ||
7400 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_spu_write], | |
7401 | "qXfer:spu:write", "write-spu-object", 0); | |
7402 | ||
444abaca | 7403 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTLSAddr], |
38691318 | 7404 | "qGetTLSAddr", "get-thread-local-storage-address", |
38691318 KB |
7405 | 0); |
7406 | ||
be2a5f71 DJ |
7407 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qSupported], |
7408 | "qSupported", "supported-packets", 0); | |
7409 | ||
08388c79 DE |
7410 | add_packet_config_cmd (&remote_protocol_packets[PACKET_qSearch_memory], |
7411 | "qSearch:memory", "search-memory", 0); | |
7412 | ||
a6b151f1 DJ |
7413 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_open], |
7414 | "vFile:open", "hostio-open", 0); | |
7415 | ||
7416 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pread], | |
7417 | "vFile:pread", "hostio-pread", 0); | |
7418 | ||
7419 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pwrite], | |
7420 | "vFile:pwrite", "hostio-pwrite", 0); | |
7421 | ||
7422 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_close], | |
7423 | "vFile:close", "hostio-close", 0); | |
7424 | ||
7425 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_unlink], | |
7426 | "vFile:unlink", "hostio-unlink", 0); | |
7427 | ||
2d717e4f DJ |
7428 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach], |
7429 | "vAttach", "attach", 0); | |
7430 | ||
7431 | add_packet_config_cmd (&remote_protocol_packets[PACKET_vRun], | |
7432 | "vRun", "run", 0); | |
7433 | ||
37a105a1 DJ |
7434 | /* Keep the old ``set remote Z-packet ...'' working. Each individual |
7435 | Z sub-packet has its own set and show commands, but users may | |
7436 | have sets to this variable in their .gdbinit files (or in their | |
7437 | documentation). */ | |
e9e68a56 | 7438 | add_setshow_auto_boolean_cmd ("Z-packet", class_obscure, |
7915a72c AC |
7439 | &remote_Z_packet_detect, _("\ |
7440 | Set use of remote protocol `Z' packets"), _("\ | |
7441 | Show use of remote protocol `Z' packets "), _("\ | |
3b64bf98 | 7442 | When set, GDB will attempt to use the remote breakpoint and watchpoint\n\ |
7915a72c | 7443 | packets."), |
e9e68a56 | 7444 | set_remote_protocol_Z_packet_cmd, |
2c5b56ce | 7445 | show_remote_protocol_Z_packet_cmd, /* FIXME: i18n: Use of remote protocol `Z' packets is %s. */ |
e9e68a56 | 7446 | &remote_set_cmdlist, &remote_show_cmdlist); |
449092f6 | 7447 | |
a6b151f1 DJ |
7448 | add_prefix_cmd ("remote", class_files, remote_command, _("\ |
7449 | Manipulate files on the remote system\n\ | |
7450 | Transfer files to and from the remote target system."), | |
7451 | &remote_cmdlist, "remote ", | |
7452 | 0 /* allow-unknown */, &cmdlist); | |
7453 | ||
7454 | add_cmd ("put", class_files, remote_put_command, | |
7455 | _("Copy a local file to the remote system."), | |
7456 | &remote_cmdlist); | |
7457 | ||
7458 | add_cmd ("get", class_files, remote_get_command, | |
7459 | _("Copy a remote file to the local system."), | |
7460 | &remote_cmdlist); | |
7461 | ||
7462 | add_cmd ("delete", class_files, remote_delete_command, | |
7463 | _("Delete a remote file."), | |
7464 | &remote_cmdlist); | |
7465 | ||
2d717e4f DJ |
7466 | remote_exec_file = xstrdup (""); |
7467 | add_setshow_string_noescape_cmd ("exec-file", class_files, | |
7468 | &remote_exec_file, _("\ | |
7469 | Set the remote pathname for \"run\""), _("\ | |
7470 | Show the remote pathname for \"run\""), NULL, NULL, NULL, | |
7471 | &remote_set_cmdlist, &remote_show_cmdlist); | |
7472 | ||
75c99385 PA |
7473 | add_setshow_boolean_cmd ("remote-async", class_maintenance, |
7474 | &remote_async_permitted_set, _("\ | |
7475 | Set whether gdb controls the remote inferior in asynchronous mode."), _("\ | |
7476 | Show whether gdb controls the remote inferior in asynchronous mode."), _("\ | |
7477 | Tells gdb whether to control the remote inferior in asynchronous mode."), | |
7478 | set_maintenance_remote_async_permitted, | |
7479 | show_maintenance_remote_async_permitted, | |
7480 | &maintenance_set_cmdlist, | |
7481 | &maintenance_show_cmdlist); | |
7482 | ||
7483 | ||
449092f6 CV |
7484 | /* Eventually initialize fileio. See fileio.c */ |
7485 | initialize_remote_fileio (remote_set_cmdlist, remote_show_cmdlist); | |
c906108c | 7486 | } |