Commit | Line | Data |
---|---|---|
b543979c | 1 | /* Remote target communications for serial-line targets in custom GDB protocol |
0a325463 | 2 | Copyright 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc. |
bd5635a1 RP |
3 | |
4 | This file is part of GDB. | |
5 | ||
b543979c | 6 | This program is free software; you can redistribute it and/or modify |
bd5635a1 | 7 | it under the terms of the GNU General Public License as published by |
b543979c JG |
8 | the Free Software Foundation; either version 2 of the License, or |
9 | (at your option) any later version. | |
bd5635a1 | 10 | |
b543979c | 11 | This program is distributed in the hope that it will be useful, |
bd5635a1 RP |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
b543979c | 17 | along with this program; if not, write to the Free Software |
998cfe7d | 18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
bd5635a1 RP |
19 | |
20 | /* Remote communication protocol. | |
e50ebec8 JK |
21 | |
22 | A debug packet whose contents are <data> | |
23 | is encapsulated for transmission in the form: | |
24 | ||
25 | $ <data> # CSUM1 CSUM2 | |
26 | ||
27 | <data> must be ASCII alphanumeric and cannot include characters | |
0c993550 JK |
28 | '$' or '#'. If <data> starts with two characters followed by |
29 | ':', then the existing stubs interpret this as a sequence number. | |
e50ebec8 JK |
30 | |
31 | CSUM1 and CSUM2 are ascii hex representation of an 8-bit | |
32 | checksum of <data>, the most significant nibble is sent first. | |
33 | the hex digits 0-9,a-f are used. | |
34 | ||
35 | Receiver responds with: | |
36 | ||
37 | + - if CSUM is correct and ready for next packet | |
38 | - - if CSUM is incorrect | |
39 | ||
40 | <data> is as follows: | |
4cc1b3f7 JK |
41 | Most values are encoded in ascii hex digits. Signal numbers are according |
42 | to the numbering in target.h. | |
bd5635a1 RP |
43 | |
44 | Request Packet | |
45 | ||
4cc1b3f7 JK |
46 | set thread Hct... Set thread for subsequent operations. |
47 | c = 'c' for thread used in step and | |
48 | continue; t... can be -1 for all | |
49 | threads. | |
50 | c = 'g' for thread used in other | |
51 | operations. If zero, pick a thread, | |
52 | any thread. | |
53 | reply OK for success | |
54 | ENN for an error. | |
55 | ||
bd5635a1 RP |
56 | read registers g |
57 | reply XX....X Each byte of register data | |
58 | is described by two hex digits. | |
59 | Registers are in the internal order | |
60 | for GDB, and the bytes in a register | |
61 | are in the same order the machine uses. | |
62 | or ENN for an error. | |
63 | ||
64 | write regs GXX..XX Each byte of register data | |
65 | is described by two hex digits. | |
66 | reply OK for success | |
67 | ENN for an error | |
68 | ||
0c993550 | 69 | write reg Pn...=r... Write register n... with value r..., |
4aa6fe10 JK |
70 | which contains two hex digits for each |
71 | byte in the register (target byte | |
72 | order). | |
73 | reply OK for success | |
74 | ENN for an error | |
75 | (not supported by all stubs). | |
76 | ||
bd5635a1 RP |
77 | read mem mAA..AA,LLLL AA..AA is address, LLLL is length. |
78 | reply XX..XX XX..XX is mem contents | |
d538b510 RP |
79 | Can be fewer bytes than requested |
80 | if able to read only part of the data. | |
bd5635a1 RP |
81 | or ENN NN is errno |
82 | ||
83 | write mem MAA..AA,LLLL:XX..XX | |
84 | AA..AA is address, | |
85 | LLLL is number of bytes, | |
86 | XX..XX is data | |
87 | reply OK for success | |
d538b510 RP |
88 | ENN for an error (this includes the case |
89 | where only part of the data was | |
90 | written). | |
bd5635a1 | 91 | |
4cc1b3f7 | 92 | continue cAA..AA AA..AA is address to resume |
bd5635a1 RP |
93 | If AA..AA is omitted, |
94 | resume at same address. | |
95 | ||
96 | step sAA..AA AA..AA is address to resume | |
97 | If AA..AA is omitted, | |
98 | resume at same address. | |
99 | ||
0a325463 SG |
100 | continue with Csig;AA..AA Continue with signal sig (hex signal |
101 | signal number). If ;AA..AA is omitted, resume | |
102 | at same address. | |
4cc1b3f7 | 103 | |
0a325463 | 104 | step with Ssig;AA..AA Like 'C' but step not continue. |
4cc1b3f7 JK |
105 | signal |
106 | ||
bd5635a1 RP |
107 | last signal ? Reply the current reason for stopping. |
108 | This is the same reply as is generated | |
109 | for step or cont : SAA where AA is the | |
110 | signal number. | |
111 | ||
b52cac6b FF |
112 | detach D Reply OK. |
113 | ||
bd5635a1 RP |
114 | There is no immediate reply to step or cont. |
115 | The reply comes when the machine stops. | |
4cc1b3f7 | 116 | It is SAA AA is the signal number. |
bd5635a1 | 117 | |
4cc1b3f7 | 118 | or... TAAn...:r...;n...:r...;n...:r...; |
e50ebec8 | 119 | AA = signal number |
4cc1b3f7 JK |
120 | n... = register number (hex) |
121 | r... = register contents | |
122 | n... = `thread' | |
123 | r... = thread process ID. This is | |
124 | a hex integer. | |
125 | n... = other string not starting | |
126 | with valid hex digit. | |
127 | gdb should ignore this n,r pair | |
128 | and go on to the next. This way | |
129 | we can extend the protocol. | |
72bba93b | 130 | or... WAA The process exited, and AA is |
758aeb93 ILT |
131 | the exit status. This is only |
132 | applicable for certains sorts of | |
133 | targets. | |
4cc1b3f7 JK |
134 | or... XAA The process terminated with signal |
135 | AA. | |
998cfe7d SC |
136 | or... OXX..XX XX..XX is hex encoding of ASCII data. This |
137 | can happen at any time while the program is | |
4cc1b3f7 JK |
138 | running and the debugger should |
139 | continue to wait for 'W', 'T', etc. | |
140 | ||
2b576293 C |
141 | thread alive TXX Find out if the thread XX is alive. |
142 | reply OK thread is still alive | |
143 | ENN thread is dead | |
144 | ||
145 | remote restart RXX Restart the remote server | |
146 | ||
147 | extended ops ! Use the extended remote protocol. | |
148 | Sticky -- only needs to be set once. | |
149 | ||
d538b510 RP |
150 | kill request k |
151 | ||
152 | toggle debug d toggle debug flag (see 386 & 68k stubs) | |
153 | reset r reset -- see sparc stub. | |
154 | reserved <other> On other requests, the stub should | |
155 | ignore the request and send an empty | |
156 | response ($#<checksum>). This way | |
157 | we can extend the protocol and GDB | |
158 | can tell whether the stub it is | |
159 | talking to uses the old or the new. | |
72bba93b | 160 | search tAA:PP,MM Search backwards starting at address |
94d4b713 JK |
161 | AA for a match with pattern PP and |
162 | mask MM. PP and MM are 4 bytes. | |
163 | Not supported by all stubs. | |
164 | ||
72bba93b SG |
165 | general query qXXXX Request info about XXXX. |
166 | general set QXXXX=yyyy Set value of XXXX to yyyy. | |
167 | query sect offs qOffsets Get section offsets. Reply is | |
168 | Text=xxx;Data=yyy;Bss=zzz | |
72bba93b | 169 | |
94d4b713 | 170 | Responses can be run-length encoded to save space. A '*' means that |
284f4ee9 | 171 | the next character is an ASCII encoding giving a repeat count which |
94d4b713 | 172 | stands for that many repititions of the character preceding the '*'. |
284f4ee9 SC |
173 | The encoding is n+29, yielding a printable character where n >=3 |
174 | (which is where rle starts to win). Don't use an n > 126. | |
175 | ||
176 | So | |
177 | "0* " means the same as "0000". */ | |
bd5635a1 | 178 | |
d747e0af | 179 | #include "defs.h" |
2b576293 | 180 | #include "gdb_string.h" |
bd5635a1 | 181 | #include <fcntl.h> |
bd5635a1 RP |
182 | #include "frame.h" |
183 | #include "inferior.h" | |
e50ebec8 | 184 | #include "bfd.h" |
6b27ebe8 | 185 | #include "symfile.h" |
bd5635a1 RP |
186 | #include "target.h" |
187 | #include "wait.h" | |
0d14c7df | 188 | /*#include "terminal.h"*/ |
8f86a4e4 | 189 | #include "gdbcmd.h" |
758aeb93 ILT |
190 | #include "objfiles.h" |
191 | #include "gdb-stabs.h" | |
cb1709ae | 192 | #include "gdbthread.h" |
bd5635a1 | 193 | |
d538b510 RP |
194 | #include "dcache.h" |
195 | ||
bd5635a1 RP |
196 | #ifdef USG |
197 | #include <sys/types.h> | |
198 | #endif | |
199 | ||
200 | #include <signal.h> | |
ebdb9ade | 201 | #include "serial.h" |
bd5635a1 | 202 | |
b543979c JG |
203 | /* Prototypes for local functions */ |
204 | ||
45993f61 | 205 | static int remote_write_bytes PARAMS ((CORE_ADDR memaddr, |
43fc25c8 | 206 | char *myaddr, int len)); |
b543979c | 207 | |
45993f61 | 208 | static int remote_read_bytes PARAMS ((CORE_ADDR memaddr, |
43fc25c8 | 209 | char *myaddr, int len)); |
b543979c | 210 | |
45993f61 | 211 | static void remote_files_info PARAMS ((struct target_ops *ignore)); |
b543979c | 212 | |
45993f61 SC |
213 | static int remote_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, |
214 | int len, int should_write, | |
215 | struct target_ops *target)); | |
b543979c | 216 | |
45993f61 | 217 | static void remote_prepare_to_store PARAMS ((void)); |
b543979c | 218 | |
45993f61 | 219 | static void remote_fetch_registers PARAMS ((int regno)); |
b543979c | 220 | |
45993f61 SC |
221 | static void remote_resume PARAMS ((int pid, int step, |
222 | enum target_signal siggnal)); | |
b543979c | 223 | |
45993f61 | 224 | static int remote_start_remote PARAMS ((char *dummy)); |
7c622b41 | 225 | |
45993f61 | 226 | static void remote_open PARAMS ((char *name, int from_tty)); |
b543979c | 227 | |
2b576293 C |
228 | static void extended_remote_open PARAMS ((char *name, int from_tty)); |
229 | ||
230 | static void remote_open_1 PARAMS ((char *, int, struct target_ops *)); | |
231 | ||
45993f61 | 232 | static void remote_close PARAMS ((int quitting)); |
b543979c | 233 | |
45993f61 | 234 | static void remote_store_registers PARAMS ((int regno)); |
b543979c | 235 | |
2b576293 C |
236 | static void remote_mourn PARAMS ((void)); |
237 | ||
238 | static void extended_remote_restart PARAMS ((void)); | |
239 | ||
240 | static void extended_remote_mourn PARAMS ((void)); | |
241 | ||
242 | static void extended_remote_create_inferior PARAMS ((char *, char *, char **)); | |
243 | ||
244 | static void remote_mourn_1 PARAMS ((struct target_ops *)); | |
245 | ||
45993f61 | 246 | static void getpkt PARAMS ((char *buf, int forever)); |
b543979c | 247 | |
45993f61 | 248 | static int putpkt PARAMS ((char *buf)); |
b543979c | 249 | |
45993f61 | 250 | static void remote_send PARAMS ((char *buf)); |
b543979c | 251 | |
45993f61 | 252 | static int readchar PARAMS ((int timeout)); |
b543979c | 253 | |
94d4b713 | 254 | static int remote_wait PARAMS ((int pid, struct target_waitstatus *status)); |
b543979c | 255 | |
45993f61 | 256 | static void remote_kill PARAMS ((void)); |
b543979c | 257 | |
45993f61 | 258 | static int tohex PARAMS ((int nib)); |
b543979c | 259 | |
45993f61 | 260 | static int fromhex PARAMS ((int a)); |
5af4f5f6 | 261 | |
45993f61 | 262 | static void remote_detach PARAMS ((char *args, int from_tty)); |
5af4f5f6 | 263 | |
45993f61 | 264 | static void remote_interrupt PARAMS ((int signo)); |
b543979c | 265 | |
45993f61 SC |
266 | static void remote_interrupt_twice PARAMS ((int signo)); |
267 | ||
268 | static void interrupt_query PARAMS ((void)); | |
981a3309 | 269 | |
b607efe7 FF |
270 | static void set_thread PARAMS ((int, int)); |
271 | ||
272 | static int remote_thread_alive PARAMS ((int)); | |
273 | ||
274 | static void get_offsets PARAMS ((void)); | |
275 | ||
276 | static int read_frame PARAMS ((char *)); | |
277 | ||
278 | static int remote_insert_breakpoint PARAMS ((CORE_ADDR, char *)); | |
279 | ||
280 | static int remote_remove_breakpoint PARAMS ((CORE_ADDR, char *)); | |
281 | ||
0a325463 SG |
282 | static struct target_ops remote_ops; /* Forward decl */ |
283 | static struct target_ops extended_remote_ops; /* Forward decl */ | |
bd5635a1 | 284 | |
ebdb9ade JK |
285 | /* This was 5 seconds, which is a long time to sit and wait. |
286 | Unless this is going though some terminal server or multiplexer or | |
287 | other form of hairy serial connection, I would think 2 seconds would | |
288 | be plenty. */ | |
bd5635a1 | 289 | |
cb1709ae DP |
290 | /* Changed to allow option to set timeout value. |
291 | was static int remote_timeout = 2; */ | |
292 | extern int remote_timeout; | |
bd5635a1 | 293 | |
b52cac6b FF |
294 | /* This variable chooses whether to send a ^C or a break when the user |
295 | requests program interruption. Although ^C is usually what remote | |
296 | systems expect, and that is the default here, sometimes a break is | |
297 | preferable instead. */ | |
298 | ||
299 | static int remote_break; | |
300 | ||
16e1d1d3 | 301 | /* Descriptor for I/O to remote machine. Initialize it to NULL so that |
bd5635a1 RP |
302 | remote_open knows that we don't have a file open when the program |
303 | starts. */ | |
0a325463 | 304 | static serial_t remote_desc = NULL; |
bd5635a1 | 305 | |
4d57c599 JK |
306 | /* Having this larger than 400 causes us to be incompatible with m68k-stub.c |
307 | and i386-stub.c. Normally, no one would notice because it only matters | |
308 | for writing large chunks of memory (e.g. in downloads). Also, this needs | |
309 | to be more than 400 if required to hold the registers (see below, where | |
310 | we round it up based on REGISTER_BYTES). */ | |
311 | #define PBUFSIZ 400 | |
bd5635a1 RP |
312 | |
313 | /* Maximum number of bytes to read/write at once. The value here | |
314 | is chosen to fill up a packet (the headers account for the 32). */ | |
315 | #define MAXBUFBYTES ((PBUFSIZ-32)/2) | |
316 | ||
b543979c | 317 | /* Round up PBUFSIZ to hold all the registers, at least. */ |
2ddeed27 JK |
318 | /* The blank line after the #if seems to be required to work around a |
319 | bug in HP's PA compiler. */ | |
b543979c | 320 | #if REGISTER_BYTES > MAXBUFBYTES |
2ddeed27 JK |
321 | |
322 | #undef PBUFSIZ | |
b543979c | 323 | #define PBUFSIZ (REGISTER_BYTES * 2 + 32) |
bd5635a1 | 324 | #endif |
4aa6fe10 | 325 | |
fea17b55 SS |
326 | /* This variable sets the number of bytes to be written to the target |
327 | in a single packet. Normally PBUFSIZ is satisfactory, but some | |
328 | targets need smaller values (perhaps because the receiving end | |
329 | is slow). */ | |
330 | ||
331 | static int remote_write_size = PBUFSIZ; | |
332 | ||
0a325463 SG |
333 | /* This is the size (in chars) of the first response to the `g' command. This |
334 | is used to limit the size of the memory read and write commands to prevent | |
335 | stub buffers from overflowing. */ | |
336 | ||
337 | static int remote_register_buf_size = 0; | |
338 | ||
4aa6fe10 JK |
339 | /* Should we try the 'P' request? If this is set to one when the stub |
340 | doesn't support 'P', the only consequence is some unnecessary traffic. */ | |
341 | static int stub_supports_P = 1; | |
342 | ||
0a325463 SG |
343 | /* These are pointers to hook functions that may be set in order to |
344 | modify resume/wait behavior for a particular architecture. */ | |
345 | ||
346 | void (*target_resume_hook) PARAMS ((void)); | |
347 | void (*target_wait_loop_hook) PARAMS ((void)); | |
348 | ||
4cc1b3f7 JK |
349 | \f |
350 | /* These are the threads which we last sent to the remote system. -1 for all | |
351 | or -2 for not sent yet. */ | |
352 | int general_thread; | |
353 | int cont_thread; | |
354 | ||
355 | static void | |
356 | set_thread (th, gen) | |
357 | int th; | |
358 | int gen; | |
359 | { | |
360 | char buf[PBUFSIZ]; | |
361 | int state = gen ? general_thread : cont_thread; | |
362 | if (state == th) | |
363 | return; | |
364 | buf[0] = 'H'; | |
365 | buf[1] = gen ? 'g' : 'c'; | |
366 | if (th == 42000) | |
367 | { | |
368 | buf[2] = '0'; | |
369 | buf[3] = '\0'; | |
370 | } | |
371 | else if (th < 0) | |
372 | sprintf (&buf[2], "-%x", -th); | |
373 | else | |
374 | sprintf (&buf[2], "%x", th); | |
375 | putpkt (buf); | |
376 | getpkt (buf, 0); | |
377 | if (gen) | |
378 | general_thread = th; | |
379 | else | |
380 | cont_thread = th; | |
381 | } | |
bd5635a1 | 382 | \f |
2b576293 | 383 | /* Return nonzero if the thread TH is still alive on the remote system. */ |
43fc25c8 JL |
384 | |
385 | static int | |
386 | remote_thread_alive (th) | |
387 | int th; | |
388 | { | |
389 | char buf[PBUFSIZ]; | |
390 | ||
391 | buf[0] = 'T'; | |
392 | if (th < 0) | |
393 | sprintf (&buf[1], "-%x", -th); | |
394 | else | |
395 | sprintf (&buf[1], "%x", th); | |
396 | putpkt (buf); | |
397 | getpkt (buf, 0); | |
398 | return (buf[0] == 'O' && buf[1] == 'K'); | |
399 | } | |
2b576293 C |
400 | |
401 | /* Restart the remote side; this is an extended protocol operation. */ | |
402 | ||
403 | static void | |
404 | extended_remote_restart () | |
405 | { | |
406 | char buf[PBUFSIZ]; | |
407 | ||
408 | /* Send the restart command; for reasons I don't understand the | |
409 | remote side really expects a number after the "R". */ | |
410 | buf[0] = 'R'; | |
411 | sprintf (&buf[1], "%x", 0); | |
412 | putpkt (buf); | |
413 | ||
414 | /* Now query for status so this looks just like we restarted | |
415 | gdbserver from scratch. */ | |
416 | putpkt ("?"); | |
417 | getpkt (buf, 0); | |
418 | } | |
43fc25c8 | 419 | \f |
bd5635a1 RP |
420 | /* Clean up connection to a remote debugger. */ |
421 | ||
e1ce8aa5 | 422 | /* ARGSUSED */ |
b543979c | 423 | static void |
bd5635a1 RP |
424 | remote_close (quitting) |
425 | int quitting; | |
426 | { | |
ebdb9ade JK |
427 | if (remote_desc) |
428 | SERIAL_CLOSE (remote_desc); | |
429 | remote_desc = NULL; | |
b543979c JG |
430 | } |
431 | ||
72bba93b SG |
432 | /* Query the remote side for the text, data and bss offsets. */ |
433 | ||
434 | static void | |
435 | get_offsets () | |
436 | { | |
6c27841f | 437 | char buf[PBUFSIZ]; |
72bba93b SG |
438 | int nvals; |
439 | CORE_ADDR text_addr, data_addr, bss_addr; | |
440 | struct section_offsets *offs; | |
441 | ||
442 | putpkt ("qOffsets"); | |
443 | ||
1c95d7ab | 444 | getpkt (buf, 0); |
72bba93b | 445 | |
1c95d7ab JK |
446 | if (buf[0] == '\000') |
447 | return; /* Return silently. Stub doesn't support this | |
448 | command. */ | |
72bba93b SG |
449 | if (buf[0] == 'E') |
450 | { | |
451 | warning ("Remote failure reply: %s", buf); | |
452 | return; | |
453 | } | |
454 | ||
455 | nvals = sscanf (buf, "Text=%lx;Data=%lx;Bss=%lx", &text_addr, &data_addr, | |
456 | &bss_addr); | |
457 | if (nvals != 3) | |
458 | error ("Malformed response to offset query, %s", buf); | |
459 | ||
460 | if (symfile_objfile == NULL) | |
461 | return; | |
462 | ||
463 | offs = (struct section_offsets *) alloca (sizeof (struct section_offsets) | |
464 | + symfile_objfile->num_sections | |
465 | * sizeof (offs->offsets)); | |
466 | memcpy (offs, symfile_objfile->section_offsets, | |
467 | sizeof (struct section_offsets) | |
468 | + symfile_objfile->num_sections | |
469 | * sizeof (offs->offsets)); | |
470 | ||
471 | ANOFFSET (offs, SECT_OFF_TEXT) = text_addr; | |
1624c38f SG |
472 | |
473 | /* This is a temporary kludge to force data and bss to use the same offsets | |
474 | because that's what nlmconv does now. The real solution requires changes | |
475 | to the stub and remote.c that I don't have time to do right now. */ | |
476 | ||
72bba93b | 477 | ANOFFSET (offs, SECT_OFF_DATA) = data_addr; |
1624c38f | 478 | ANOFFSET (offs, SECT_OFF_BSS) = data_addr; |
72bba93b SG |
479 | |
480 | objfile_relocate (symfile_objfile, offs); | |
481 | } | |
482 | ||
7c622b41 JG |
483 | /* Stub for catch_errors. */ |
484 | ||
485 | static int | |
486 | remote_start_remote (dummy) | |
487 | char *dummy; | |
488 | { | |
ac7a377f JK |
489 | immediate_quit = 1; /* Allow user to interrupt it */ |
490 | ||
7c622b41 | 491 | /* Ack any packet which the remote side has already sent. */ |
72bba93b SG |
492 | SERIAL_WRITE (remote_desc, "+", 1); |
493 | ||
4cc1b3f7 JK |
494 | /* Let the stub know that we want it to return the thread. */ |
495 | set_thread (-1, 0); | |
496 | ||
72bba93b SG |
497 | get_offsets (); /* Get text, data & bss offsets */ |
498 | ||
7c622b41 | 499 | putpkt ("?"); /* initiate a query from remote machine */ |
ac7a377f | 500 | immediate_quit = 0; |
7c622b41 JG |
501 | |
502 | start_remote (); /* Initialize gdb process mechanisms */ | |
503 | return 1; | |
504 | } | |
505 | ||
bd5635a1 RP |
506 | /* Open a connection to a remote debugger. |
507 | NAME is the filename used for communication. */ | |
508 | ||
2b576293 C |
509 | static void |
510 | remote_open (name, from_tty) | |
511 | char *name; | |
512 | int from_tty; | |
513 | { | |
514 | remote_open_1 (name, from_tty, &remote_ops); | |
515 | } | |
516 | ||
517 | /* Open a connection to a remote debugger using the extended | |
935e77f5 | 518 | remote gdb protocol. NAME is the filename used for communication. */ |
2b576293 C |
519 | |
520 | static void | |
521 | extended_remote_open (name, from_tty) | |
522 | char *name; | |
523 | int from_tty; | |
524 | { | |
525 | char buf[PBUFSIZ]; | |
526 | ||
527 | /* Do the basic remote open stuff. */ | |
528 | remote_open_1 (name, from_tty, &extended_remote_ops); | |
529 | ||
530 | /* Now tell the remote that we're using the extended protocol. */ | |
531 | putpkt ("!"); | |
532 | getpkt (buf, 0); | |
533 | ||
534 | } | |
535 | ||
536 | /* Generic code for opening a connection to a remote target. */ | |
d538b510 RP |
537 | static DCACHE *remote_dcache; |
538 | ||
b543979c | 539 | static void |
2b576293 | 540 | remote_open_1 (name, from_tty, target) |
bd5635a1 RP |
541 | char *name; |
542 | int from_tty; | |
2b576293 | 543 | struct target_ops *target; |
bd5635a1 | 544 | { |
bd5635a1 | 545 | if (name == 0) |
45993f61 | 546 | error ("To open a remote debug connection, you need to specify what serial\n\ |
bd5635a1 RP |
547 | device is attached to the remote system (e.g. /dev/ttya)."); |
548 | ||
f2fc6e7a JK |
549 | target_preopen (from_tty); |
550 | ||
2b576293 | 551 | unpush_target (target); |
bd5635a1 | 552 | |
d538b510 | 553 | remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes); |
bd5635a1 | 554 | |
ebdb9ade JK |
555 | remote_desc = SERIAL_OPEN (name); |
556 | if (!remote_desc) | |
bd5635a1 RP |
557 | perror_with_name (name); |
558 | ||
94d4b713 | 559 | if (baud_rate != -1) |
b543979c | 560 | { |
94d4b713 JK |
561 | if (SERIAL_SETBAUDRATE (remote_desc, baud_rate)) |
562 | { | |
563 | SERIAL_CLOSE (remote_desc); | |
564 | perror_with_name (name); | |
565 | } | |
b543979c | 566 | } |
ebdb9ade | 567 | |
45993f61 | 568 | |
ebdb9ade | 569 | SERIAL_RAW (remote_desc); |
bd5635a1 | 570 | |
e15f2a54 JK |
571 | /* If there is something sitting in the buffer we might take it as a |
572 | response to a command, which would be bad. */ | |
573 | SERIAL_FLUSH_INPUT (remote_desc); | |
574 | ||
bd5635a1 | 575 | if (from_tty) |
7c622b41 JG |
576 | { |
577 | puts_filtered ("Remote debugging using "); | |
578 | puts_filtered (name); | |
579 | puts_filtered ("\n"); | |
580 | } | |
2b576293 | 581 | push_target (target); /* Switch to using remote target now */ |
bd5635a1 | 582 | |
4aa6fe10 JK |
583 | /* Start out by trying the 'P' request to set registers. We set this each |
584 | time that we open a new target so that if the user switches from one | |
585 | stub to another, we can (if the target is closed and reopened) cope. */ | |
586 | stub_supports_P = 1; | |
587 | ||
4cc1b3f7 JK |
588 | general_thread = -2; |
589 | cont_thread = -2; | |
590 | ||
a1e0ba7a SG |
591 | /* Without this, some commands which require an active target (such as kill) |
592 | won't work. This variable serves (at least) double duty as both the pid | |
593 | of the target process (if it has such), and as a flag indicating that a | |
594 | target is active. These functions should be split out into seperate | |
595 | variables, especially since GDB will someday have a notion of debugging | |
596 | several processes. */ | |
597 | ||
4fb7359d | 598 | inferior_pid = 42000; |
4fb7359d SG |
599 | /* Start the remote connection; if error (0), discard this target. |
600 | In particular, if the user quits, be sure to discard it | |
601 | (we'd be in an inconsistent state otherwise). */ | |
602 | if (!catch_errors (remote_start_remote, (char *)0, | |
45993f61 | 603 | "Couldn't establish connection to remote target\n", RETURN_MASK_ALL)) |
4fb7359d | 604 | pop_target(); |
bd5635a1 RP |
605 | } |
606 | ||
b52cac6b FF |
607 | /* This takes a program previously attached to and detaches it. After |
608 | this is done, GDB can be used to debug some other program. We | |
609 | better not have left any breakpoints in the target program or it'll | |
610 | die when it hits one. */ | |
bd5635a1 RP |
611 | |
612 | static void | |
613 | remote_detach (args, from_tty) | |
614 | char *args; | |
615 | int from_tty; | |
616 | { | |
b52cac6b FF |
617 | char buf[PBUFSIZ]; |
618 | ||
bd5635a1 RP |
619 | if (args) |
620 | error ("Argument given to \"detach\" when remotely debugging."); | |
b52cac6b FF |
621 | |
622 | /* Tell the remote target to detach. */ | |
623 | strcpy (buf, "D"); | |
624 | remote_send (buf); | |
625 | ||
bd5635a1 RP |
626 | pop_target (); |
627 | if (from_tty) | |
7c622b41 | 628 | puts_filtered ("Ending remote debugging.\n"); |
bd5635a1 RP |
629 | } |
630 | ||
631 | /* Convert hex digit A to a number. */ | |
632 | ||
633 | static int | |
634 | fromhex (a) | |
635 | int a; | |
636 | { | |
637 | if (a >= '0' && a <= '9') | |
638 | return a - '0'; | |
639 | else if (a >= 'a' && a <= 'f') | |
640 | return a - 'a' + 10; | |
ec10503a | 641 | else |
6c27841f | 642 | error ("Reply contains invalid hex digit %d", a); |
bd5635a1 RP |
643 | } |
644 | ||
645 | /* Convert number NIB to a hex digit. */ | |
646 | ||
647 | static int | |
648 | tohex (nib) | |
649 | int nib; | |
650 | { | |
651 | if (nib < 10) | |
652 | return '0'+nib; | |
653 | else | |
654 | return 'a'+nib-10; | |
655 | } | |
656 | \f | |
657 | /* Tell the remote machine to resume. */ | |
658 | ||
4cc1b3f7 JK |
659 | static enum target_signal last_sent_signal = TARGET_SIGNAL_0; |
660 | int last_sent_step; | |
661 | ||
b543979c | 662 | static void |
d538b510 | 663 | remote_resume (pid, step, siggnal) |
94d4b713 JK |
664 | int pid, step; |
665 | enum target_signal siggnal; | |
bd5635a1 RP |
666 | { |
667 | char buf[PBUFSIZ]; | |
668 | ||
4cc1b3f7 JK |
669 | if (pid == -1) |
670 | set_thread (inferior_pid, 0); | |
671 | else | |
672 | set_thread (pid, 0); | |
bd5635a1 | 673 | |
d538b510 | 674 | dcache_flush (remote_dcache); |
bd5635a1 | 675 | |
4cc1b3f7 JK |
676 | last_sent_signal = siggnal; |
677 | last_sent_step = step; | |
678 | ||
0a325463 SG |
679 | /* A hook for when we need to do something at the last moment before |
680 | resumption. */ | |
681 | if (target_resume_hook) | |
682 | (*target_resume_hook) (); | |
683 | ||
4cc1b3f7 JK |
684 | if (siggnal != TARGET_SIGNAL_0) |
685 | { | |
686 | buf[0] = step ? 'S' : 'C'; | |
687 | buf[1] = tohex (((int)siggnal >> 4) & 0xf); | |
688 | buf[2] = tohex ((int)siggnal & 0xf); | |
689 | buf[3] = '\0'; | |
690 | } | |
691 | else | |
692 | strcpy (buf, step ? "s": "c"); | |
bd5635a1 RP |
693 | |
694 | putpkt (buf); | |
695 | } | |
ebdb9ade | 696 | \f |
b543979c JG |
697 | /* Send ^C to target to halt it. Target will respond, and send us a |
698 | packet. */ | |
699 | ||
5af4f5f6 JK |
700 | static void |
701 | remote_interrupt (signo) | |
e676a15f | 702 | int signo; |
b543979c | 703 | { |
ebdb9ade JK |
704 | /* If this doesn't work, try more severe steps. */ |
705 | signal (signo, remote_interrupt_twice); | |
8f86a4e4 | 706 | |
d0d8484a | 707 | if (remote_debug) |
199b2450 | 708 | printf_unfiltered ("remote_interrupt called\n"); |
8f86a4e4 | 709 | |
b52cac6b FF |
710 | /* Send a break or a ^C, depending on user preference. */ |
711 | if (remote_break) | |
712 | SERIAL_SEND_BREAK (remote_desc); | |
713 | else | |
714 | SERIAL_WRITE (remote_desc, "\003", 1); | |
b543979c JG |
715 | } |
716 | ||
5af4f5f6 JK |
717 | static void (*ofunc)(); |
718 | ||
ebdb9ade JK |
719 | /* The user typed ^C twice. */ |
720 | static void | |
721 | remote_interrupt_twice (signo) | |
722 | int signo; | |
723 | { | |
724 | signal (signo, ofunc); | |
725 | ||
981a3309 SG |
726 | interrupt_query (); |
727 | ||
728 | signal (signo, remote_interrupt); | |
729 | } | |
730 | ||
731 | /* Ask the user what to do when an interrupt is received. */ | |
732 | ||
733 | static void | |
734 | interrupt_query () | |
735 | { | |
ebdb9ade | 736 | target_terminal_ours (); |
981a3309 | 737 | |
6b27ebe8 | 738 | if (query ("Interrupted while waiting for the program.\n\ |
ebdb9ade JK |
739 | Give up (and stop debugging it)? ")) |
740 | { | |
741 | target_mourn_inferior (); | |
e50ebec8 | 742 | return_to_top_level (RETURN_QUIT); |
ebdb9ade | 743 | } |
981a3309 SG |
744 | |
745 | target_terminal_inferior (); | |
ebdb9ade | 746 | } |
b543979c | 747 | |
4cc1b3f7 JK |
748 | /* If nonzero, ignore the next kill. */ |
749 | int kill_kludge; | |
750 | ||
bd5635a1 | 751 | /* Wait until the remote machine stops, then return, |
e1ce8aa5 JK |
752 | storing status in STATUS just as `wait' would. |
753 | Returns "pid" (though it's not clear what, if anything, that | |
754 | means in the case of this target). */ | |
bd5635a1 | 755 | |
b543979c | 756 | static int |
d0d8484a SG |
757 | remote_wait (pid, status) |
758 | int pid; | |
94d4b713 | 759 | struct target_waitstatus *status; |
bd5635a1 RP |
760 | { |
761 | unsigned char buf[PBUFSIZ]; | |
4cc1b3f7 | 762 | int thread_num = -1; |
8f86a4e4 | 763 | |
94d4b713 JK |
764 | status->kind = TARGET_WAITKIND_EXITED; |
765 | status->value.integer = 0; | |
b543979c | 766 | |
4f8a48e5 | 767 | while (1) |
8f86a4e4 | 768 | { |
4f8a48e5 | 769 | unsigned char *p; |
a03d4f8e | 770 | |
4f8a48e5 ILT |
771 | ofunc = (void (*)()) signal (SIGINT, remote_interrupt); |
772 | getpkt ((char *) buf, 1); | |
773 | signal (SIGINT, ofunc); | |
4ecee2f9 | 774 | |
0a325463 SG |
775 | /* This is a hook for when we need to do something (perhaps the |
776 | collection of trace data) every time the target stops. */ | |
777 | if (target_wait_loop_hook) | |
778 | (*target_wait_loop_hook) (); | |
779 | ||
754e5da2 | 780 | switch (buf[0]) |
8f86a4e4 | 781 | { |
754e5da2 SG |
782 | case 'E': /* Error of some sort */ |
783 | warning ("Remote failure reply: %s", buf); | |
784 | continue; | |
785 | case 'T': /* Status with PC, SP, FP, ... */ | |
786 | { | |
787 | int i; | |
788 | long regno; | |
789 | char regs[MAX_REGISTER_RAW_SIZE]; | |
a03d4f8e | 790 | |
754e5da2 SG |
791 | /* Expedited reply, containing Signal, {regno, reg} repeat */ |
792 | /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where | |
793 | ss = signal number | |
794 | n... = register number | |
795 | r... = register contents | |
796 | */ | |
5af4f5f6 | 797 | |
754e5da2 | 798 | p = &buf[3]; /* after Txx */ |
5af4f5f6 | 799 | |
754e5da2 SG |
800 | while (*p) |
801 | { | |
802 | unsigned char *p1; | |
45993f61 | 803 | char *p_temp; |
5af4f5f6 | 804 | |
2b576293 | 805 | regno = strtol ((const char *) p, &p_temp, 16); /* Read the register number */ |
45993f61 | 806 | p1 = (unsigned char *)p_temp; |
5af4f5f6 | 807 | |
754e5da2 | 808 | if (p1 == p) |
754e5da2 | 809 | { |
2b576293 | 810 | p1 = (unsigned char *) strchr ((const char *) p, ':'); |
4cc1b3f7 JK |
811 | if (p1 == NULL) |
812 | warning ("Malformed packet (missing colon): %s\n\ | |
813 | Packet: '%s'\n", | |
814 | p, buf); | |
2b576293 | 815 | if (strncmp ((const char *) p, "thread", p1 - p) == 0) |
4cc1b3f7 | 816 | { |
2b576293 | 817 | thread_num = strtol ((const char *) ++p1, &p_temp, 16); |
45993f61 | 818 | p = (unsigned char *)p_temp; |
4cc1b3f7 JK |
819 | } |
820 | } | |
821 | else | |
822 | { | |
823 | p = p1; | |
824 | ||
825 | if (*p++ != ':') | |
826 | warning ("Malformed packet (missing colon): %s\n\ | |
827 | Packet: '%s'\n", | |
828 | p, buf); | |
829 | ||
830 | if (regno >= NUM_REGS) | |
2b576293 | 831 | warning ("Remote sent bad register number %ld: %s\n\ |
4cc1b3f7 JK |
832 | Packet: '%s'\n", |
833 | regno, p, buf); | |
834 | ||
835 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i++) | |
836 | { | |
837 | if (p[0] == 0 || p[1] == 0) | |
838 | warning ("Remote reply is too short: %s", buf); | |
0d14c7df | 839 | regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); |
4cc1b3f7 JK |
840 | p += 2; |
841 | } | |
842 | supply_register (regno, regs); | |
754e5da2 | 843 | } |
4f8a48e5 | 844 | |
754e5da2 SG |
845 | if (*p++ != ';') |
846 | warning ("Remote register badly formatted: %s", buf); | |
754e5da2 SG |
847 | } |
848 | } | |
849 | /* fall through */ | |
850 | case 'S': /* Old style status, just signal only */ | |
851 | status->kind = TARGET_WAITKIND_STOPPED; | |
852 | status->value.sig = (enum target_signal) | |
853 | (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))); | |
4f8a48e5 | 854 | |
4cc1b3f7 | 855 | goto got_status; |
754e5da2 SG |
856 | case 'W': /* Target exited */ |
857 | { | |
858 | /* The remote process exited. */ | |
859 | status->kind = TARGET_WAITKIND_EXITED; | |
860 | status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]); | |
4cc1b3f7 | 861 | goto got_status; |
754e5da2 | 862 | } |
4cc1b3f7 JK |
863 | case 'X': |
864 | status->kind = TARGET_WAITKIND_SIGNALLED; | |
865 | status->value.sig = (enum target_signal) | |
866 | (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))); | |
867 | kill_kludge = 1; | |
868 | ||
869 | goto got_status; | |
754e5da2 | 870 | case 'O': /* Console output */ |
998cfe7d SC |
871 | for (p = buf + 1; *p; p +=2) |
872 | { | |
873 | char tb[2]; | |
874 | char c = fromhex (p[0]) * 16 + fromhex (p[1]); | |
875 | tb[0] = c; | |
876 | tb[1] = 0; | |
877 | if (target_output_hook) | |
878 | target_output_hook (tb); | |
879 | else | |
880 | fputs_filtered (tb, gdb_stdout); | |
881 | } | |
754e5da2 | 882 | continue; |
4cc1b3f7 JK |
883 | case '\0': |
884 | if (last_sent_signal != TARGET_SIGNAL_0) | |
885 | { | |
886 | /* Zero length reply means that we tried 'S' or 'C' and | |
887 | the remote system doesn't support it. */ | |
888 | target_terminal_ours_for_output (); | |
889 | printf_filtered | |
890 | ("Can't send signals to this remote system. %s not sent.\n", | |
891 | target_signal_to_name (last_sent_signal)); | |
892 | last_sent_signal = TARGET_SIGNAL_0; | |
893 | target_terminal_inferior (); | |
894 | ||
2b576293 C |
895 | strcpy ((char *) buf, last_sent_step ? "s" : "c"); |
896 | putpkt ((char *) buf); | |
4cc1b3f7 JK |
897 | continue; |
898 | } | |
899 | /* else fallthrough */ | |
754e5da2 SG |
900 | default: |
901 | warning ("Invalid remote reply: %s", buf); | |
902 | continue; | |
4f8a48e5 | 903 | } |
758aeb93 | 904 | } |
4cc1b3f7 JK |
905 | got_status: |
906 | if (thread_num != -1) | |
907 | { | |
908 | /* Initial thread value can only be acquired via wait, so deal with | |
909 | this marker which is used before the first thread value is | |
910 | acquired. */ | |
911 | if (inferior_pid == 42000) | |
912 | { | |
913 | inferior_pid = thread_num; | |
914 | add_thread (inferior_pid); | |
915 | } | |
916 | return thread_num; | |
917 | } | |
918 | return inferior_pid; | |
bd5635a1 RP |
919 | } |
920 | ||
55fea07b JK |
921 | /* Number of bytes of registers this stub implements. */ |
922 | static int register_bytes_found; | |
923 | ||
bd5635a1 | 924 | /* Read the remote registers into the block REGS. */ |
e1ce8aa5 JK |
925 | /* Currently we just read all the registers, so we don't use regno. */ |
926 | /* ARGSUSED */ | |
b543979c | 927 | static void |
bd5635a1 RP |
928 | remote_fetch_registers (regno) |
929 | int regno; | |
930 | { | |
931 | char buf[PBUFSIZ]; | |
932 | int i; | |
933 | char *p; | |
934 | char regs[REGISTER_BYTES]; | |
935 | ||
4cc1b3f7 JK |
936 | set_thread (inferior_pid, 1); |
937 | ||
bd5635a1 RP |
938 | sprintf (buf, "g"); |
939 | remote_send (buf); | |
940 | ||
0a325463 SG |
941 | if (remote_register_buf_size == 0) |
942 | remote_register_buf_size = strlen (buf); | |
943 | ||
55fea07b JK |
944 | /* Unimplemented registers read as all bits zero. */ |
945 | memset (regs, 0, REGISTER_BYTES); | |
946 | ||
981a3309 SG |
947 | /* We can get out of synch in various cases. If the first character |
948 | in the buffer is not a hex character, assume that has happened | |
949 | and try to fetch another packet to read. */ | |
950 | while ((buf[0] < '0' || buf[0] > '9') | |
951 | && (buf[0] < 'a' || buf[0] > 'f')) | |
952 | { | |
d0d8484a | 953 | if (remote_debug) |
199b2450 | 954 | printf_unfiltered ("Bad register packet; fetching a new packet\n"); |
981a3309 SG |
955 | getpkt (buf, 0); |
956 | } | |
957 | ||
bd5635a1 RP |
958 | /* Reply describes registers byte by byte, each byte encoded as two |
959 | hex characters. Suck them all up, then supply them to the | |
960 | register cacheing/storage mechanism. */ | |
961 | ||
962 | p = buf; | |
963 | for (i = 0; i < REGISTER_BYTES; i++) | |
964 | { | |
55fea07b JK |
965 | if (p[0] == 0) |
966 | break; | |
967 | if (p[1] == 0) | |
968 | { | |
969 | warning ("Remote reply is of odd length: %s", buf); | |
970 | /* Don't change register_bytes_found in this case, and don't | |
971 | print a second warning. */ | |
972 | goto supply_them; | |
973 | } | |
bd5635a1 RP |
974 | regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); |
975 | p += 2; | |
976 | } | |
55fea07b JK |
977 | |
978 | if (i != register_bytes_found) | |
979 | { | |
980 | register_bytes_found = i; | |
981 | #ifdef REGISTER_BYTES_OK | |
982 | if (!REGISTER_BYTES_OK (i)) | |
983 | warning ("Remote reply is too short: %s", buf); | |
984 | #endif | |
985 | } | |
986 | ||
987 | supply_them: | |
bd5635a1 RP |
988 | for (i = 0; i < NUM_REGS; i++) |
989 | supply_register (i, ®s[REGISTER_BYTE(i)]); | |
bd5635a1 RP |
990 | } |
991 | ||
4aa6fe10 JK |
992 | /* Prepare to store registers. Since we may send them all (using a |
993 | 'G' request), we have to read out the ones we don't want to change | |
994 | first. */ | |
bd5635a1 | 995 | |
b543979c | 996 | static void |
bd5635a1 RP |
997 | remote_prepare_to_store () |
998 | { | |
34517ebc JG |
999 | /* Make sure the entire registers array is valid. */ |
1000 | read_register_bytes (0, (char *)NULL, REGISTER_BYTES); | |
bd5635a1 RP |
1001 | } |
1002 | ||
4aa6fe10 JK |
1003 | /* Store register REGNO, or all registers if REGNO == -1, from the contents |
1004 | of REGISTERS. FIXME: ignores errors. */ | |
bd5635a1 | 1005 | |
b543979c | 1006 | static void |
bd5635a1 RP |
1007 | remote_store_registers (regno) |
1008 | int regno; | |
1009 | { | |
1010 | char buf[PBUFSIZ]; | |
1011 | int i; | |
1012 | char *p; | |
1013 | ||
4cc1b3f7 JK |
1014 | set_thread (inferior_pid, 1); |
1015 | ||
4aa6fe10 JK |
1016 | if (regno >= 0 && stub_supports_P) |
1017 | { | |
1018 | /* Try storing a single register. */ | |
1019 | char *regp; | |
1020 | ||
0c993550 | 1021 | sprintf (buf, "P%x=", regno); |
4aa6fe10 JK |
1022 | p = buf + strlen (buf); |
1023 | regp = ®isters[REGISTER_BYTE (regno)]; | |
1024 | for (i = 0; i < REGISTER_RAW_SIZE (regno); ++i) | |
1025 | { | |
1026 | *p++ = tohex ((regp[i] >> 4) & 0xf); | |
1027 | *p++ = tohex (regp[i] & 0xf); | |
1028 | } | |
1029 | *p = '\0'; | |
1030 | remote_send (buf); | |
1031 | if (buf[0] != '\0') | |
1032 | { | |
1033 | /* The stub understands the 'P' request. We are done. */ | |
1034 | return; | |
1035 | } | |
1036 | ||
1037 | /* The stub does not support the 'P' request. Use 'G' instead, | |
1038 | and don't try using 'P' in the future (it will just waste our | |
1039 | time). */ | |
1040 | stub_supports_P = 0; | |
1041 | } | |
1042 | ||
bd5635a1 | 1043 | buf[0] = 'G'; |
4aa6fe10 | 1044 | |
bd5635a1 RP |
1045 | /* Command describes registers byte by byte, |
1046 | each byte encoded as two hex characters. */ | |
1047 | ||
1048 | p = buf + 1; | |
55fea07b JK |
1049 | /* remote_prepare_to_store insures that register_bytes_found gets set. */ |
1050 | for (i = 0; i < register_bytes_found; i++) | |
bd5635a1 RP |
1051 | { |
1052 | *p++ = tohex ((registers[i] >> 4) & 0xf); | |
1053 | *p++ = tohex (registers[i] & 0xf); | |
1054 | } | |
1055 | *p = '\0'; | |
1056 | ||
1057 | remote_send (buf); | |
bd5635a1 RP |
1058 | } |
1059 | ||
45993f61 SC |
1060 | /* |
1061 | Use of the data cache *used* to be disabled because it loses for looking at | |
b43e0347 | 1062 | and changing hardware I/O ports and the like. Accepting `volatile' |
45993f61 SC |
1063 | would perhaps be one way to fix it. Another idea would be to use the |
1064 | executable file for the text segment (for all SEC_CODE sections? | |
1065 | For all SEC_READONLY sections?). This has problems if you want to | |
1066 | actually see what the memory contains (e.g. self-modifying code, | |
1067 | clobbered memory, user downloaded the wrong thing). | |
1068 | ||
1069 | Because it speeds so much up, it's now enabled, if you're playing | |
1070 | with registers you turn it of (set remotecache 0) | |
1071 | */ | |
b43e0347 | 1072 | |
bd5635a1 RP |
1073 | /* Read a word from remote address ADDR and return it. |
1074 | This goes through the data cache. */ | |
1075 | ||
2b576293 | 1076 | #if 0 /* unused? */ |
b43e0347 | 1077 | static int |
bd5635a1 RP |
1078 | remote_fetch_word (addr) |
1079 | CORE_ADDR addr; | |
1080 | { | |
d538b510 | 1081 | return dcache_fetch (remote_dcache, addr); |
bd5635a1 RP |
1082 | } |
1083 | ||
1084 | /* Write a word WORD into remote address ADDR. | |
1085 | This goes through the data cache. */ | |
1086 | ||
b43e0347 | 1087 | static void |
bd5635a1 RP |
1088 | remote_store_word (addr, word) |
1089 | CORE_ADDR addr; | |
1090 | int word; | |
1091 | { | |
d538b510 | 1092 | dcache_poke (remote_dcache, addr, word); |
bd5635a1 | 1093 | } |
2b576293 | 1094 | #endif /* 0 (unused?) */ |
45993f61 | 1095 | |
bd5635a1 | 1096 | \f |
0a325463 SG |
1097 | |
1098 | /* Return the number of hex digits in num. */ | |
1099 | ||
1100 | static int | |
1101 | hexnumlen (num) | |
1102 | ULONGEST num; | |
1103 | { | |
1104 | int i; | |
1105 | ||
1106 | for (i = 0; num != 0; i++) | |
1107 | num >>= 4; | |
1108 | ||
1109 | return min (i, 1); | |
1110 | } | |
1111 | ||
bd5635a1 RP |
1112 | /* Write memory data directly to the remote machine. |
1113 | This does not inform the data cache; the data cache uses this. | |
1114 | MEMADDR is the address in the remote memory space. | |
1115 | MYADDR is the address of the buffer in our space. | |
d538b510 | 1116 | LEN is the number of bytes. |
bd5635a1 | 1117 | |
d538b510 RP |
1118 | Returns number of bytes transferred, or 0 for error. */ |
1119 | ||
1120 | static int | |
bd5635a1 RP |
1121 | remote_write_bytes (memaddr, myaddr, len) |
1122 | CORE_ADDR memaddr; | |
43fc25c8 | 1123 | char *myaddr; |
bd5635a1 RP |
1124 | int len; |
1125 | { | |
0a325463 SG |
1126 | int max_buf_size; /* Max size of packet output buffer */ |
1127 | int origlen; | |
1128 | ||
ec10503a | 1129 | /* Chop the transfer down if necessary */ |
bd5635a1 | 1130 | |
0a325463 SG |
1131 | max_buf_size = min (remote_write_size, PBUFSIZ); |
1132 | max_buf_size = min (max_buf_size, remote_register_buf_size); | |
1133 | ||
1134 | #define PACKET_OVERHEAD (1 + 1 + 1 + 2) /* $x#xx - Overhead for all types of packets */ | |
1135 | ||
1136 | /* packet overhead + <memaddr>,<len>: */ | |
1137 | max_buf_size -= PACKET_OVERHEAD + hexnumlen (memaddr + len - 1) + 1 + hexnumlen (len) + 1; | |
1138 | ||
1139 | origlen = len; | |
1140 | while (len > 0) | |
ec10503a | 1141 | { |
0a325463 SG |
1142 | char buf[PBUFSIZ]; |
1143 | char *p; | |
1144 | int todo; | |
1145 | int i; | |
fea17b55 | 1146 | |
0a325463 | 1147 | todo = min (len, max_buf_size / 2); /* num bytes that will fit */ |
bd5635a1 | 1148 | |
ec10503a SC |
1149 | /* FIXME-32x64: Need a version of print_address_numeric which puts the |
1150 | result in a buffer like sprintf. */ | |
0a325463 | 1151 | sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, todo); |
bd5635a1 | 1152 | |
ec10503a SC |
1153 | /* We send target system values byte by byte, in increasing byte addresses, |
1154 | each byte encoded as two hex characters. */ | |
bd5635a1 | 1155 | |
ec10503a SC |
1156 | p = buf + strlen (buf); |
1157 | for (i = 0; i < todo; i++) | |
1158 | { | |
0a325463 SG |
1159 | *p++ = tohex ((myaddr[i] >> 4) & 0xf); |
1160 | *p++ = tohex (myaddr[i] & 0xf); | |
ec10503a SC |
1161 | } |
1162 | *p = '\0'; | |
d538b510 | 1163 | |
ec10503a SC |
1164 | putpkt (buf); |
1165 | getpkt (buf, 0); | |
1166 | ||
1167 | if (buf[0] == 'E') | |
1168 | { | |
1169 | /* There is no correspondance between what the remote protocol uses | |
1170 | for errors and errno codes. We would like a cleaner way of | |
1171 | representing errors (big enough to include errno codes, bfd_error | |
1172 | codes, and others). But for now just return EIO. */ | |
1173 | errno = EIO; | |
1174 | return 0; | |
1175 | } | |
0a325463 SG |
1176 | myaddr += todo; |
1177 | memaddr += todo; | |
1178 | len -= todo; | |
d538b510 | 1179 | } |
0a325463 | 1180 | return origlen; |
bd5635a1 RP |
1181 | } |
1182 | ||
1183 | /* Read memory data directly from the remote machine. | |
1184 | This does not use the data cache; the data cache uses this. | |
1185 | MEMADDR is the address in the remote memory space. | |
1186 | MYADDR is the address of the buffer in our space. | |
d538b510 | 1187 | LEN is the number of bytes. |
bd5635a1 | 1188 | |
d538b510 RP |
1189 | Returns number of bytes transferred, or 0 for error. */ |
1190 | ||
1191 | static int | |
bd5635a1 RP |
1192 | remote_read_bytes (memaddr, myaddr, len) |
1193 | CORE_ADDR memaddr; | |
43fc25c8 | 1194 | char *myaddr; |
bd5635a1 RP |
1195 | int len; |
1196 | { | |
0a325463 SG |
1197 | int max_buf_size; /* Max size of packet output buffer */ |
1198 | int origlen; | |
bd5635a1 | 1199 | |
0a325463 SG |
1200 | /* Chop the transfer down if necessary */ |
1201 | ||
1202 | max_buf_size = min (remote_write_size, PBUFSIZ); | |
1203 | max_buf_size = min (max_buf_size, remote_register_buf_size); | |
1204 | ||
1205 | /* packet overhead */ | |
1206 | max_buf_size -= PACKET_OVERHEAD; | |
1207 | ||
1208 | origlen = len; | |
1209 | while (len > 0) | |
0d14c7df | 1210 | { |
0a325463 SG |
1211 | char buf[PBUFSIZ]; |
1212 | char *p; | |
1213 | int todo; | |
1214 | int i; | |
1215 | ||
1216 | todo = min (len, max_buf_size / 2); /* num bytes that will fit */ | |
bd5635a1 | 1217 | |
0d14c7df FF |
1218 | /* FIXME-32x64: Need a version of print_address_numeric which puts the |
1219 | result in a buffer like sprintf. */ | |
0a325463 | 1220 | sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo); |
0d14c7df FF |
1221 | putpkt (buf); |
1222 | getpkt (buf, 0); | |
d538b510 | 1223 | |
0d14c7df FF |
1224 | if (buf[0] == 'E') |
1225 | { | |
1226 | /* There is no correspondance between what the remote protocol uses | |
1227 | for errors and errno codes. We would like a cleaner way of | |
1228 | representing errors (big enough to include errno codes, bfd_error | |
1229 | codes, and others). But for now just return EIO. */ | |
1230 | errno = EIO; | |
1231 | return 0; | |
1232 | } | |
bd5635a1 | 1233 | |
b543979c | 1234 | /* Reply describes memory byte by byte, |
bd5635a1 RP |
1235 | each byte encoded as two hex characters. */ |
1236 | ||
0d14c7df FF |
1237 | p = buf; |
1238 | for (i = 0; i < todo; i++) | |
1239 | { | |
1240 | if (p[0] == 0 || p[1] == 0) | |
1241 | /* Reply is short. This means that we were able to read only part | |
1242 | of what we wanted to. */ | |
0a325463 SG |
1243 | return i + (origlen - len); |
1244 | myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]); | |
0d14c7df FF |
1245 | p += 2; |
1246 | } | |
0a325463 SG |
1247 | myaddr += todo; |
1248 | memaddr += todo; | |
1249 | len -= todo; | |
bd5635a1 | 1250 | } |
0a325463 | 1251 | return origlen; |
bd5635a1 RP |
1252 | } |
1253 | \f | |
1254 | /* Read or write LEN bytes from inferior memory at MEMADDR, transferring | |
e1ce8aa5 | 1255 | to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is |
bd5635a1 RP |
1256 | nonzero. Returns length of data written or read; 0 for error. */ |
1257 | ||
b543979c JG |
1258 | /* ARGSUSED */ |
1259 | static int | |
1260 | remote_xfer_memory(memaddr, myaddr, len, should_write, target) | |
bd5635a1 RP |
1261 | CORE_ADDR memaddr; |
1262 | char *myaddr; | |
1263 | int len; | |
e1ce8aa5 | 1264 | int should_write; |
b543979c | 1265 | struct target_ops *target; /* ignored */ |
bd5635a1 | 1266 | { |
45993f61 | 1267 | return dcache_xfer_memory (remote_dcache, memaddr, myaddr, len, should_write); |
bd5635a1 RP |
1268 | } |
1269 | ||
45993f61 | 1270 | |
94d4b713 JK |
1271 | #if 0 |
1272 | /* Enable after 4.12. */ | |
1273 | ||
1274 | void | |
1275 | remote_search (len, data, mask, startaddr, increment, lorange, hirange | |
1276 | addr_found, data_found) | |
1277 | int len; | |
1278 | char *data; | |
1279 | char *mask; | |
1280 | CORE_ADDR startaddr; | |
1281 | int increment; | |
1282 | CORE_ADDR lorange; | |
1283 | CORE_ADDR hirange; | |
1284 | CORE_ADDR *addr_found; | |
1285 | char *data_found; | |
1286 | { | |
1287 | if (increment == -4 && len == 4) | |
1288 | { | |
1289 | long mask_long, data_long; | |
1290 | long data_found_long; | |
1291 | CORE_ADDR addr_we_found; | |
1292 | char buf[PBUFSIZ]; | |
1293 | long returned_long[2]; | |
1294 | char *p; | |
1295 | ||
1296 | mask_long = extract_unsigned_integer (mask, len); | |
1297 | data_long = extract_unsigned_integer (data, len); | |
1298 | sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long); | |
1299 | putpkt (buf); | |
1300 | getpkt (buf, 0); | |
1301 | if (buf[0] == '\0') | |
1302 | { | |
1303 | /* The stub doesn't support the 't' request. We might want to | |
1304 | remember this fact, but on the other hand the stub could be | |
1305 | switched on us. Maybe we should remember it only until | |
1306 | the next "target remote". */ | |
1307 | generic_search (len, data, mask, startaddr, increment, lorange, | |
1308 | hirange, addr_found, data_found); | |
1309 | return; | |
1310 | } | |
1311 | ||
1312 | if (buf[0] == 'E') | |
1313 | /* There is no correspondance between what the remote protocol uses | |
1314 | for errors and errno codes. We would like a cleaner way of | |
1315 | representing errors (big enough to include errno codes, bfd_error | |
1316 | codes, and others). But for now just use EIO. */ | |
1317 | memory_error (EIO, startaddr); | |
1318 | p = buf; | |
1319 | addr_we_found = 0; | |
1320 | while (*p != '\0' && *p != ',') | |
1321 | addr_we_found = (addr_we_found << 4) + fromhex (*p++); | |
1322 | if (*p == '\0') | |
1323 | error ("Protocol error: short return for search"); | |
1324 | ||
1325 | data_found_long = 0; | |
1326 | while (*p != '\0' && *p != ',') | |
1327 | data_found_long = (data_found_long << 4) + fromhex (*p++); | |
1328 | /* Ignore anything after this comma, for future extensions. */ | |
1329 | ||
1330 | if (addr_we_found < lorange || addr_we_found >= hirange) | |
1331 | { | |
1332 | *addr_found = 0; | |
1333 | return; | |
1334 | } | |
1335 | ||
1336 | *addr_found = addr_we_found; | |
1337 | *data_found = store_unsigned_integer (data_we_found, len); | |
1338 | return; | |
1339 | } | |
1340 | generic_search (len, data, mask, startaddr, increment, lorange, | |
1341 | hirange, addr_found, data_found); | |
1342 | } | |
1343 | #endif /* 0 */ | |
1344 | \f | |
b543979c | 1345 | static void |
8f86a4e4 | 1346 | remote_files_info (ignore) |
5af4f5f6 | 1347 | struct target_ops *ignore; |
bd5635a1 | 1348 | { |
7c622b41 | 1349 | puts_filtered ("Debugging a target over a serial line.\n"); |
bd5635a1 RP |
1350 | } |
1351 | \f | |
e50ebec8 JK |
1352 | /* Stuff for dealing with the packets which are part of this protocol. |
1353 | See comment at top of file for details. */ | |
bd5635a1 | 1354 | |
ebdb9ade | 1355 | /* Read a single character from the remote end, masking it down to 7 bits. */ |
b543979c | 1356 | |
bd5635a1 | 1357 | static int |
754e5da2 SG |
1358 | readchar (timeout) |
1359 | int timeout; | |
bd5635a1 | 1360 | { |
ebdb9ade | 1361 | int ch; |
bd5635a1 | 1362 | |
ebdb9ade | 1363 | ch = SERIAL_READCHAR (remote_desc, timeout); |
fce7f2d9 | 1364 | |
754e5da2 SG |
1365 | switch (ch) |
1366 | { | |
1367 | case SERIAL_EOF: | |
1368 | error ("Remote connection closed"); | |
1369 | case SERIAL_ERROR: | |
1370 | perror_with_name ("Remote communication error"); | |
1371 | case SERIAL_TIMEOUT: | |
1372 | return ch; | |
1373 | default: | |
1374 | return ch & 0x7f; | |
1375 | } | |
bd5635a1 RP |
1376 | } |
1377 | ||
1378 | /* Send the command in BUF to the remote machine, | |
1379 | and read the reply into BUF. | |
1380 | Report an error if we get an error reply. */ | |
1381 | ||
1382 | static void | |
1383 | remote_send (buf) | |
1384 | char *buf; | |
1385 | { | |
bd5635a1 | 1386 | putpkt (buf); |
7c622b41 | 1387 | getpkt (buf, 0); |
bd5635a1 RP |
1388 | |
1389 | if (buf[0] == 'E') | |
1390 | error ("Remote failure reply: %s", buf); | |
1391 | } | |
1392 | ||
1393 | /* Send a packet to the remote machine, with error checking. | |
1394 | The data of the packet is in BUF. */ | |
1395 | ||
4cc1b3f7 | 1396 | static int |
bd5635a1 RP |
1397 | putpkt (buf) |
1398 | char *buf; | |
1399 | { | |
1400 | int i; | |
1401 | unsigned char csum = 0; | |
b543979c | 1402 | char buf2[PBUFSIZ]; |
bd5635a1 | 1403 | int cnt = strlen (buf); |
ebdb9ade | 1404 | int ch; |
45993f61 | 1405 | int tcount = 0; |
bd5635a1 RP |
1406 | char *p; |
1407 | ||
1408 | /* Copy the packet into buffer BUF2, encapsulating it | |
1409 | and giving it a checksum. */ | |
1410 | ||
b52cac6b | 1411 | if (cnt > (int) sizeof (buf2) - 5) /* Prosanity check */ |
b543979c JG |
1412 | abort(); |
1413 | ||
bd5635a1 RP |
1414 | p = buf2; |
1415 | *p++ = '$'; | |
1416 | ||
1417 | for (i = 0; i < cnt; i++) | |
1418 | { | |
1419 | csum += buf[i]; | |
1420 | *p++ = buf[i]; | |
1421 | } | |
1422 | *p++ = '#'; | |
1423 | *p++ = tohex ((csum >> 4) & 0xf); | |
1424 | *p++ = tohex (csum & 0xf); | |
1425 | ||
1426 | /* Send it over and over until we get a positive ack. */ | |
1427 | ||
6b27ebe8 JK |
1428 | while (1) |
1429 | { | |
1624c38f SG |
1430 | int started_error_output = 0; |
1431 | ||
d0d8484a | 1432 | if (remote_debug) |
6b27ebe8 JK |
1433 | { |
1434 | *p = '\0'; | |
1624c38f SG |
1435 | printf_unfiltered ("Sending packet: %s...", buf2); |
1436 | gdb_flush(gdb_stdout); | |
6b27ebe8 JK |
1437 | } |
1438 | if (SERIAL_WRITE (remote_desc, buf2, p - buf2)) | |
1439 | perror_with_name ("putpkt: write failed"); | |
1440 | ||
1441 | /* read until either a timeout occurs (-2) or '+' is read */ | |
1442 | while (1) | |
1443 | { | |
754e5da2 | 1444 | ch = readchar (remote_timeout); |
6b27ebe8 | 1445 | |
45993f61 | 1446 | if (remote_debug) |
1624c38f SG |
1447 | { |
1448 | switch (ch) | |
1449 | { | |
1450 | case '+': | |
1451 | case SERIAL_TIMEOUT: | |
1624c38f SG |
1452 | case '$': |
1453 | if (started_error_output) | |
1454 | { | |
45993f61 | 1455 | putchar_unfiltered ('\n'); |
1624c38f SG |
1456 | started_error_output = 0; |
1457 | } | |
1458 | } | |
1459 | } | |
1460 | ||
6b27ebe8 JK |
1461 | switch (ch) |
1462 | { | |
1463 | case '+': | |
d0d8484a | 1464 | if (remote_debug) |
199b2450 | 1465 | printf_unfiltered("Ack\n"); |
4cc1b3f7 | 1466 | return 1; |
6b27ebe8 | 1467 | case SERIAL_TIMEOUT: |
45993f61 SC |
1468 | tcount ++; |
1469 | if (tcount > 3) | |
1470 | return 0; | |
6b27ebe8 | 1471 | break; /* Retransmit buffer */ |
1624c38f SG |
1472 | case '$': |
1473 | { | |
6c27841f | 1474 | char junkbuf[PBUFSIZ]; |
1624c38f SG |
1475 | |
1476 | /* It's probably an old response, and we're out of sync. Just | |
1477 | gobble up the packet and ignore it. */ | |
1478 | getpkt (junkbuf, 0); | |
1479 | continue; /* Now, go look for + */ | |
1480 | } | |
6b27ebe8 | 1481 | default: |
d0d8484a | 1482 | if (remote_debug) |
1624c38f SG |
1483 | { |
1484 | if (!started_error_output) | |
1485 | { | |
1486 | started_error_output = 1; | |
1487 | printf_unfiltered ("putpkt: Junk: "); | |
1488 | } | |
45993f61 | 1489 | putchar_unfiltered (ch & 0177); |
1624c38f | 1490 | } |
6b27ebe8 JK |
1491 | continue; |
1492 | } | |
1493 | break; /* Here to retransmit */ | |
1494 | } | |
981a3309 | 1495 | |
94d4b713 JK |
1496 | #if 0 |
1497 | /* This is wrong. If doing a long backtrace, the user should be | |
1498 | able to get out next time we call QUIT, without anything as violent | |
1499 | as interrupt_query. If we want to provide a way out of here | |
1500 | without getting to the next QUIT, it should be based on hitting | |
1501 | ^C twice as in remote_wait. */ | |
981a3309 SG |
1502 | if (quit_flag) |
1503 | { | |
1504 | quit_flag = 0; | |
1505 | interrupt_query (); | |
1506 | } | |
94d4b713 | 1507 | #endif |
6b27ebe8 | 1508 | } |
bd5635a1 RP |
1509 | } |
1510 | ||
754e5da2 SG |
1511 | /* Come here after finding the start of the frame. Collect the rest into BUF, |
1512 | verifying the checksum, length, and handling run-length compression. | |
1513 | Returns 0 on any error, 1 on success. */ | |
1514 | ||
1515 | static int | |
1516 | read_frame (buf) | |
1517 | char *buf; | |
1518 | { | |
1519 | unsigned char csum; | |
1520 | char *bp; | |
1521 | int c; | |
1522 | ||
1523 | csum = 0; | |
1524 | bp = buf; | |
1525 | ||
1526 | while (1) | |
1527 | { | |
1528 | c = readchar (remote_timeout); | |
1529 | ||
1530 | switch (c) | |
1531 | { | |
1532 | case SERIAL_TIMEOUT: | |
1533 | if (remote_debug) | |
1534 | puts_filtered ("Timeout in mid-packet, retrying\n"); | |
1535 | return 0; | |
1536 | case '$': | |
1537 | if (remote_debug) | |
1538 | puts_filtered ("Saw new packet start in middle of old one\n"); | |
1539 | return 0; /* Start a new packet, count retries */ | |
1540 | case '#': | |
1541 | { | |
1542 | unsigned char pktcsum; | |
1543 | ||
1544 | *bp = '\000'; | |
1545 | ||
205fc02b SC |
1546 | pktcsum = fromhex (readchar (remote_timeout)) << 4; |
1547 | pktcsum |= fromhex (readchar (remote_timeout)); | |
754e5da2 SG |
1548 | |
1549 | if (csum == pktcsum) | |
1550 | return 1; | |
1551 | ||
6c27841f SG |
1552 | if (remote_debug) |
1553 | { | |
1554 | printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=", | |
1555 | pktcsum, csum); | |
1556 | puts_filtered (buf); | |
1557 | puts_filtered ("\n"); | |
1558 | } | |
754e5da2 SG |
1559 | return 0; |
1560 | } | |
1561 | case '*': /* Run length encoding */ | |
284f4ee9 | 1562 | csum += c; |
754e5da2 SG |
1563 | c = readchar (remote_timeout); |
1564 | csum += c; | |
1565 | c = c - ' ' + 3; /* Compute repeat count */ | |
1566 | ||
6c27841f SG |
1567 | |
1568 | if (c > 0 && c < 255 && bp + c - 1 < buf + PBUFSIZ - 1) | |
754e5da2 SG |
1569 | { |
1570 | memset (bp, *(bp - 1), c); | |
1571 | bp += c; | |
1572 | continue; | |
1573 | } | |
1574 | ||
1575 | *bp = '\0'; | |
1576 | printf_filtered ("Repeat count %d too large for buffer: ", c); | |
1577 | puts_filtered (buf); | |
1578 | puts_filtered ("\n"); | |
754e5da2 | 1579 | return 0; |
284f4ee9 | 1580 | |
754e5da2 SG |
1581 | default: |
1582 | if (bp < buf + PBUFSIZ - 1) | |
1583 | { | |
1584 | *bp++ = c; | |
1585 | csum += c; | |
1586 | continue; | |
1587 | } | |
1588 | ||
1589 | *bp = '\0'; | |
1590 | puts_filtered ("Remote packet too long: "); | |
1591 | puts_filtered (buf); | |
1592 | puts_filtered ("\n"); | |
1593 | ||
1594 | return 0; | |
1595 | } | |
1596 | } | |
1597 | } | |
1598 | ||
bd5635a1 | 1599 | /* Read a packet from the remote machine, with error checking, |
7c622b41 JG |
1600 | and store it in BUF. BUF is expected to be of size PBUFSIZ. |
1601 | If FOREVER, wait forever rather than timing out; this is used | |
1602 | while the target is executing user code. */ | |
bd5635a1 RP |
1603 | |
1604 | static void | |
754e5da2 SG |
1605 | getpkt (buf, forever) |
1606 | char *buf; | |
ebdb9ade | 1607 | int forever; |
bd5635a1 | 1608 | { |
754e5da2 SG |
1609 | int c; |
1610 | int tries; | |
1611 | int timeout; | |
1612 | int val; | |
94d4b713 | 1613 | |
45993f61 SC |
1614 | strcpy (buf,"timeout"); |
1615 | ||
754e5da2 | 1616 | if (forever) |
45993f61 SC |
1617 | { |
1618 | #ifdef MAINTENANCE_CMDS | |
1619 | timeout = watchdog > 0 ? watchdog : -1; | |
1620 | #else | |
1621 | timeout = -1; | |
1622 | #endif | |
1623 | } | |
1624 | ||
754e5da2 SG |
1625 | else |
1626 | timeout = remote_timeout; | |
bd5635a1 | 1627 | |
45993f61 | 1628 | #define MAX_TRIES 3 |
981a3309 | 1629 | |
754e5da2 SG |
1630 | for (tries = 1; tries <= MAX_TRIES; tries++) |
1631 | { | |
7c622b41 JG |
1632 | /* This can loop forever if the remote side sends us characters |
1633 | continuously, but if it pauses, we'll get a zero from readchar | |
1634 | because of timeout. Then we'll count that as a retry. */ | |
6b27ebe8 | 1635 | |
754e5da2 SG |
1636 | /* Note that we will only wait forever prior to the start of a packet. |
1637 | After that, we expect characters to arrive at a brisk pace. They | |
1638 | should show up within remote_timeout intervals. */ | |
6b27ebe8 | 1639 | |
754e5da2 | 1640 | do |
6b27ebe8 | 1641 | { |
754e5da2 | 1642 | c = readchar (timeout); |
7c622b41 | 1643 | |
ebdb9ade | 1644 | if (c == SERIAL_TIMEOUT) |
7c622b41 | 1645 | { |
45993f61 SC |
1646 | #ifdef MAINTENANCE_CMDS |
1647 | if (forever) /* Watchdog went off. Kill the target. */ | |
1648 | { | |
1649 | target_mourn_inferior (); | |
1650 | error ("Watchdog has expired. Target detached.\n"); | |
1651 | } | |
1652 | #endif | |
d0d8484a | 1653 | if (remote_debug) |
754e5da2 SG |
1654 | puts_filtered ("Timed out.\n"); |
1655 | goto retry; | |
7c622b41 | 1656 | } |
bd5635a1 | 1657 | } |
754e5da2 | 1658 | while (c != '$'); |
bd5635a1 | 1659 | |
754e5da2 | 1660 | /* We've found the start of a packet, now collect the data. */ |
38094c60 | 1661 | |
754e5da2 SG |
1662 | val = read_frame (buf); |
1663 | ||
1664 | if (val == 1) | |
38094c60 | 1665 | { |
754e5da2 SG |
1666 | if (remote_debug) |
1667 | fprintf_unfiltered (gdb_stderr, "Packet received: %s\n", buf); | |
1668 | SERIAL_WRITE (remote_desc, "+", 1); | |
1669 | return; | |
38094c60 | 1670 | } |
754e5da2 SG |
1671 | |
1672 | /* Try the whole thing again. */ | |
45993f61 | 1673 | retry: |
754e5da2 | 1674 | SERIAL_WRITE (remote_desc, "-", 1); |
bd5635a1 RP |
1675 | } |
1676 | ||
754e5da2 | 1677 | /* We have tried hard enough, and just can't receive the packet. Give up. */ |
7c622b41 | 1678 | |
754e5da2 | 1679 | printf_unfiltered ("Ignoring packet error, continuing...\n"); |
ebdb9ade | 1680 | SERIAL_WRITE (remote_desc, "+", 1); |
bd5635a1 RP |
1681 | } |
1682 | \f | |
ebdb9ade JK |
1683 | static void |
1684 | remote_kill () | |
1685 | { | |
4cc1b3f7 JK |
1686 | /* For some mysterious reason, wait_for_inferior calls kill instead of |
1687 | mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */ | |
1688 | if (kill_kludge) | |
1689 | { | |
1690 | kill_kludge = 0; | |
1691 | target_mourn_inferior (); | |
1692 | return; | |
1693 | } | |
1694 | ||
1695 | /* Use catch_errors so the user can quit from gdb even when we aren't on | |
1696 | speaking terms with the remote system. */ | |
1697 | catch_errors (putpkt, "k", "", RETURN_MASK_ERROR); | |
1698 | ||
ebdb9ade JK |
1699 | /* Don't wait for it to die. I'm not really sure it matters whether |
1700 | we do or not. For the existing stubs, kill is a noop. */ | |
1701 | target_mourn_inferior (); | |
1702 | } | |
bd5635a1 | 1703 | |
ebdb9ade JK |
1704 | static void |
1705 | remote_mourn () | |
1706 | { | |
2b576293 C |
1707 | remote_mourn_1 (&remote_ops); |
1708 | } | |
1709 | ||
1710 | static void | |
1711 | extended_remote_mourn () | |
1712 | { | |
1713 | /* We do _not_ want to mourn the target like this; this will | |
1714 | remove the extended remote target from the target stack, | |
1715 | and the next time the user says "run" it'll fail. | |
1716 | ||
1717 | FIXME: What is the right thing to do here? */ | |
1718 | #if 0 | |
1719 | remote_mourn_1 (&extended_remote_ops); | |
1720 | #endif | |
1721 | } | |
1722 | ||
1723 | /* Worker function for remote_mourn. */ | |
1724 | static void | |
1725 | remote_mourn_1 (target) | |
1726 | struct target_ops *target; | |
1727 | { | |
1728 | unpush_target (target); | |
ebdb9ade JK |
1729 | generic_mourn_inferior (); |
1730 | } | |
2b576293 C |
1731 | |
1732 | /* In the extended protocol we want to be able to do things like | |
1733 | "run" and have them basically work as expected. So we need | |
1734 | a special create_inferior function. | |
1735 | ||
1736 | FIXME: One day add support for changing the exec file | |
1737 | we're debugging, arguments and an environment. */ | |
1738 | ||
1739 | static void | |
1740 | extended_remote_create_inferior (exec_file, args, env) | |
1741 | char *exec_file; | |
1742 | char *args; | |
1743 | char **env; | |
1744 | { | |
1745 | /* Rip out the breakpoints; we'll reinsert them after restarting | |
1746 | the remote server. */ | |
1747 | remove_breakpoints (); | |
1748 | ||
1749 | /* Now restart the remote server. */ | |
1750 | extended_remote_restart (); | |
1751 | ||
1752 | /* Now put the breakpoints back in. This way we're safe if the | |
1753 | restart function works via a unix fork on the remote side. */ | |
1754 | insert_breakpoints (); | |
1755 | ||
1756 | /* Clean up from the last time we were running. */ | |
1757 | clear_proceed_status (); | |
1758 | ||
1759 | /* Let the remote process run. */ | |
1760 | proceed (-1, TARGET_SIGNAL_0, 0); | |
1761 | } | |
1762 | ||
ebdb9ade | 1763 | \f |
5af4f5f6 | 1764 | /* On some machines, e.g. 68k, we may use a different breakpoint instruction |
fea17b55 SS |
1765 | than other targets; in those use REMOTE_BREAKPOINT instead of just |
1766 | BREAKPOINT. Also, bi-endian targets may define LITTLE_REMOTE_BREAKPOINT | |
1767 | and BIG_REMOTE_BREAKPOINT. If none of these are defined, we just call | |
1768 | the standard routines that are in mem-break.c. */ | |
1769 | ||
1770 | /* FIXME, these ought to be done in a more dynamic fashion. For instance, | |
1771 | the choice of breakpoint instruction affects target program design and | |
1772 | vice versa, and by making it user-tweakable, the special code here | |
1773 | goes away and we need fewer special GDB configurations. */ | |
1774 | ||
1775 | #if defined (LITTLE_REMOTE_BREAKPOINT) && defined (BIG_REMOTE_BREAKPOINT) && !defined(REMOTE_BREAKPOINT) | |
1776 | #define REMOTE_BREAKPOINT | |
1777 | #endif | |
1778 | ||
1779 | #ifdef REMOTE_BREAKPOINT | |
5af4f5f6 | 1780 | |
fea17b55 SS |
1781 | /* If the target isn't bi-endian, just pretend it is. */ |
1782 | #if !defined (LITTLE_REMOTE_BREAKPOINT) && !defined (BIG_REMOTE_BREAKPOINT) | |
1783 | #define LITTLE_REMOTE_BREAKPOINT REMOTE_BREAKPOINT | |
1784 | #define BIG_REMOTE_BREAKPOINT REMOTE_BREAKPOINT | |
1785 | #endif | |
5af4f5f6 | 1786 | |
fea17b55 SS |
1787 | static unsigned char big_break_insn[] = BIG_REMOTE_BREAKPOINT; |
1788 | static unsigned char little_break_insn[] = LITTLE_REMOTE_BREAKPOINT; | |
5af4f5f6 | 1789 | |
fea17b55 | 1790 | #endif /* REMOTE_BREAKPOINT */ |
5af4f5f6 JK |
1791 | |
1792 | /* Insert a breakpoint on targets that don't have any better breakpoint | |
1793 | support. We read the contents of the target location and stash it, | |
1794 | then overwrite it with a breakpoint instruction. ADDR is the target | |
1795 | location in the target machine. CONTENTS_CACHE is a pointer to | |
1796 | memory allocated for saving the target contents. It is guaranteed | |
1797 | by the caller to be long enough to save sizeof BREAKPOINT bytes (this | |
1798 | is accomplished via BREAKPOINT_MAX). */ | |
1799 | ||
d538b510 | 1800 | static int |
5af4f5f6 JK |
1801 | remote_insert_breakpoint (addr, contents_cache) |
1802 | CORE_ADDR addr; | |
1803 | char *contents_cache; | |
1804 | { | |
fea17b55 | 1805 | #ifdef REMOTE_BREAKPOINT |
5af4f5f6 JK |
1806 | int val; |
1807 | ||
fea17b55 | 1808 | val = target_read_memory (addr, contents_cache, sizeof big_break_insn); |
5af4f5f6 JK |
1809 | |
1810 | if (val == 0) | |
fea17b55 SS |
1811 | { |
1812 | if (TARGET_BYTE_ORDER == BIG_ENDIAN) | |
1813 | val = target_write_memory (addr, (char *) big_break_insn, | |
1814 | sizeof big_break_insn); | |
1815 | else | |
1816 | val = target_write_memory (addr, (char *) little_break_insn, | |
1817 | sizeof little_break_insn); | |
1818 | } | |
5af4f5f6 JK |
1819 | |
1820 | return val; | |
fea17b55 SS |
1821 | #else |
1822 | return memory_insert_breakpoint (addr, contents_cache); | |
1823 | #endif /* REMOTE_BREAKPOINT */ | |
5af4f5f6 JK |
1824 | } |
1825 | ||
d538b510 | 1826 | static int |
5af4f5f6 JK |
1827 | remote_remove_breakpoint (addr, contents_cache) |
1828 | CORE_ADDR addr; | |
1829 | char *contents_cache; | |
1830 | { | |
fea17b55 SS |
1831 | #ifdef REMOTE_BREAKPOINT |
1832 | return target_write_memory (addr, contents_cache, sizeof big_break_insn); | |
1833 | #else | |
1834 | return memory_remove_breakpoint (addr, contents_cache); | |
1835 | #endif /* REMOTE_BREAKPOINT */ | |
5af4f5f6 JK |
1836 | } |
1837 | \f | |
bd5635a1 RP |
1838 | /* Define the target subroutine names */ |
1839 | ||
0a325463 SG |
1840 | static struct target_ops remote_ops = |
1841 | { | |
b543979c JG |
1842 | "remote", /* to_shortname */ |
1843 | "Remote serial target in gdb-specific protocol", /* to_longname */ | |
1844 | "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ | |
1845 | Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */ | |
1846 | remote_open, /* to_open */ | |
1847 | remote_close, /* to_close */ | |
1848 | NULL, /* to_attach */ | |
1849 | remote_detach, /* to_detach */ | |
1850 | remote_resume, /* to_resume */ | |
1851 | remote_wait, /* to_wait */ | |
1852 | remote_fetch_registers, /* to_fetch_registers */ | |
1853 | remote_store_registers, /* to_store_registers */ | |
1854 | remote_prepare_to_store, /* to_prepare_to_store */ | |
b543979c JG |
1855 | remote_xfer_memory, /* to_xfer_memory */ |
1856 | remote_files_info, /* to_files_info */ | |
5af4f5f6 JK |
1857 | remote_insert_breakpoint, /* to_insert_breakpoint */ |
1858 | remote_remove_breakpoint, /* to_remove_breakpoint */ | |
b543979c JG |
1859 | NULL, /* to_terminal_init */ |
1860 | NULL, /* to_terminal_inferior */ | |
1861 | NULL, /* to_terminal_ours_for_output */ | |
1862 | NULL, /* to_terminal_ours */ | |
1863 | NULL, /* to_terminal_info */ | |
ebdb9ade | 1864 | remote_kill, /* to_kill */ |
6b27ebe8 | 1865 | generic_load, /* to_load */ |
b543979c JG |
1866 | NULL, /* to_lookup_symbol */ |
1867 | NULL, /* to_create_inferior */ | |
ebdb9ade | 1868 | remote_mourn, /* to_mourn_inferior */ |
34517ebc | 1869 | 0, /* to_can_run */ |
7c622b41 | 1870 | 0, /* to_notice_signals */ |
43fc25c8 | 1871 | remote_thread_alive, /* to_thread_alive */ |
6c27841f | 1872 | 0, /* to_stop */ |
b543979c JG |
1873 | process_stratum, /* to_stratum */ |
1874 | NULL, /* to_next */ | |
1875 | 1, /* to_has_all_memory */ | |
1876 | 1, /* to_has_memory */ | |
1877 | 1, /* to_has_stack */ | |
1878 | 1, /* to_has_registers */ | |
1879 | 1, /* to_has_execution */ | |
1880 | NULL, /* sections */ | |
1881 | NULL, /* sections_end */ | |
1882 | OPS_MAGIC /* to_magic */ | |
bd5635a1 RP |
1883 | }; |
1884 | ||
0a325463 SG |
1885 | static struct target_ops extended_remote_ops = |
1886 | { | |
2b576293 C |
1887 | "extended-remote", /* to_shortname */ |
1888 | "Extended remote serial target in gdb-specific protocol",/* to_longname */ | |
1889 | "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ | |
1890 | Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */ | |
1891 | extended_remote_open, /* to_open */ | |
1892 | remote_close, /* to_close */ | |
1893 | NULL, /* to_attach */ | |
1894 | remote_detach, /* to_detach */ | |
1895 | remote_resume, /* to_resume */ | |
1896 | remote_wait, /* to_wait */ | |
1897 | remote_fetch_registers, /* to_fetch_registers */ | |
1898 | remote_store_registers, /* to_store_registers */ | |
1899 | remote_prepare_to_store, /* to_prepare_to_store */ | |
1900 | remote_xfer_memory, /* to_xfer_memory */ | |
1901 | remote_files_info, /* to_files_info */ | |
1902 | ||
1903 | remote_insert_breakpoint, /* to_insert_breakpoint */ | |
1904 | remote_remove_breakpoint, /* to_remove_breakpoint */ | |
1905 | ||
1906 | NULL, /* to_terminal_init */ | |
1907 | NULL, /* to_terminal_inferior */ | |
1908 | NULL, /* to_terminal_ours_for_output */ | |
1909 | NULL, /* to_terminal_ours */ | |
1910 | NULL, /* to_terminal_info */ | |
1911 | remote_kill, /* to_kill */ | |
1912 | generic_load, /* to_load */ | |
1913 | NULL, /* to_lookup_symbol */ | |
1914 | extended_remote_create_inferior,/* to_create_inferior */ | |
1915 | extended_remote_mourn, /* to_mourn_inferior */ | |
1916 | 0, /* to_can_run */ | |
1917 | 0, /* to_notice_signals */ | |
1918 | remote_thread_alive, /* to_thread_alive */ | |
1919 | 0, /* to_stop */ | |
1920 | process_stratum, /* to_stratum */ | |
1921 | NULL, /* to_next */ | |
1922 | 1, /* to_has_all_memory */ | |
1923 | 1, /* to_has_memory */ | |
1924 | 1, /* to_has_stack */ | |
1925 | 1, /* to_has_registers */ | |
1926 | 1, /* to_has_execution */ | |
1927 | NULL, /* sections */ | |
1928 | NULL, /* sections_end */ | |
1929 | OPS_MAGIC /* to_magic */ | |
1930 | }; | |
1931 | ||
0a325463 SG |
1932 | /* Some targets are only capable of doing downloads, and afterwards they switch |
1933 | to the remote serial protocol. This function provides a clean way to get | |
1934 | from the download target to the remote target. It's basically just a | |
1935 | wrapper so that we don't have to expose any of the internal workings of | |
1936 | remote.c. | |
1937 | ||
1938 | Prior to calling this routine, you should shutdown the current target code, | |
1939 | else you will get the "A program is being debugged already..." message. | |
1940 | Usually a call to pop_target() suffices. | |
1941 | */ | |
1942 | ||
1943 | void | |
1944 | push_remote_target (name, from_tty) | |
1945 | char *name; | |
1946 | int from_tty; | |
1947 | { | |
1948 | printf_filtered ("Switching to remote protocol\n"); | |
1949 | remote_open (name, from_tty); | |
1950 | } | |
1951 | ||
bd5635a1 RP |
1952 | void |
1953 | _initialize_remote () | |
1954 | { | |
1955 | add_target (&remote_ops); | |
2b576293 | 1956 | add_target (&extended_remote_ops); |
2c441e7f SS |
1957 | |
1958 | add_show_from_set (add_set_cmd ("remotetimeout", no_class, | |
1959 | var_integer, (char *)&remote_timeout, | |
1960 | "Set timeout value for remote read.\n", &setlist), | |
1961 | &showlist); | |
b52cac6b FF |
1962 | |
1963 | add_show_from_set (add_set_cmd ("remotebreak", no_class, | |
1964 | var_integer, (char *)&remote_break, | |
1965 | "Set whether to send break if interrupted.\n", &setlist), | |
1966 | &showlist); | |
fea17b55 SS |
1967 | |
1968 | add_show_from_set (add_set_cmd ("remotewritesize", no_class, | |
1969 | var_integer, (char *)&remote_write_size, | |
1970 | "Set the maximum number of bytes in each memory write packet.\n", &setlist), | |
1971 | &showlist); | |
976bb0be | 1972 | } |