Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* Generic support for remote debugging interfaces. |
2 | ||
3 | Copyright 1993, 1994, 1998 Free Software Foundation, Inc. | |
4 | ||
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
c906108c | 11 | |
c5aa993b JM |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
c906108c | 16 | |
c5aa993b JM |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
21 | |
22 | /* This file actually contains two distinct logical "packages". They | |
c5aa993b JM |
23 | are packaged together in this one file because they are typically |
24 | used together. | |
c906108c | 25 | |
c5aa993b JM |
26 | The first package is an addition to the serial package. The |
27 | addition provides reading and writing with debugging output and | |
28 | timeouts based on user settable variables. These routines are | |
29 | intended to support serial port based remote backends. These | |
30 | functions are prefixed with sr_. | |
c906108c | 31 | |
c5aa993b JM |
32 | The second package is a collection of more or less generic |
33 | functions for use by remote backends. They support user settable | |
34 | variables for debugging, retries, and the like. | |
c906108c SS |
35 | |
36 | Todo: | |
37 | ||
38 | * a pass through mode a la kermit or telnet. | |
39 | * autobaud. | |
40 | * ask remote to change his baud rate. | |
c5aa993b | 41 | */ |
c906108c SS |
42 | |
43 | #include <ctype.h> | |
44 | ||
45 | #include "defs.h" | |
46 | #include "gdb_string.h" | |
47 | #include "gdbcmd.h" | |
48 | #include "target.h" | |
49 | #include "serial.h" | |
c5aa993b JM |
50 | #include "gdbcore.h" /* for exec_bfd */ |
51 | #include "inferior.h" /* for generic_mourn_inferior */ | |
c906108c SS |
52 | #include "remote-utils.h" |
53 | ||
54 | ||
a14ed312 | 55 | void _initialize_sr_support (void); |
c906108c | 56 | |
c5aa993b JM |
57 | struct _sr_settings sr_settings = |
58 | { | |
59 | 4, /* timeout: | |
60 | remote-hms.c had 2 | |
61 | remote-bug.c had "with a timeout of 2, we time out waiting for | |
62 | the prompt after an s-record dump." | |
63 | ||
64 | remote.c had (2): This was 5 seconds, which is a long time to | |
65 | sit and wait. Unless this is going though some terminal server | |
66 | or multiplexer or other form of hairy serial connection, I | |
67 | would think 2 seconds would be plenty. | |
68 | */ | |
69 | ||
70 | 10, /* retries */ | |
71 | NULL, /* device */ | |
72 | NULL, /* descriptor */ | |
c906108c SS |
73 | }; |
74 | ||
75 | struct gr_settings *gr_settings = NULL; | |
76 | ||
a14ed312 KB |
77 | static void usage (char *, char *); |
78 | static void sr_com (char *, int); | |
c906108c SS |
79 | |
80 | static void | |
fba45db2 | 81 | usage (char *proto, char *junk) |
c906108c SS |
82 | { |
83 | if (junk != NULL) | |
c5aa993b | 84 | fprintf_unfiltered (gdb_stderr, "Unrecognized arguments: `%s'.\n", junk); |
c906108c SS |
85 | |
86 | error ("Usage: target %s [DEVICE [SPEED [DEBUG]]]\n\ | |
87 | where DEVICE is the name of a device or HOST:PORT", proto, proto); | |
88 | ||
89 | return; | |
90 | } | |
91 | ||
92 | #define CHECKDONE(p, q) \ | |
93 | { \ | |
94 | if (q == p) \ | |
95 | { \ | |
96 | if (*p == '\0') \ | |
97 | return; \ | |
98 | else \ | |
99 | usage(proto, p); \ | |
100 | } \ | |
101 | } | |
102 | ||
103 | void | |
fba45db2 | 104 | sr_scan_args (char *proto, char *args) |
c906108c SS |
105 | { |
106 | int n; | |
107 | char *p, *q; | |
108 | ||
109 | /* if no args, then nothing to do. */ | |
110 | if (args == NULL || *args == '\0') | |
111 | return; | |
112 | ||
113 | /* scan off white space. */ | |
c5aa993b | 114 | for (p = args; isspace (*p); ++p);; |
c906108c SS |
115 | |
116 | /* find end of device name. */ | |
c5aa993b | 117 | for (q = p; *q != '\0' && !isspace (*q); ++q);; |
c906108c SS |
118 | |
119 | /* check for missing or empty device name. */ | |
c5aa993b JM |
120 | CHECKDONE (p, q); |
121 | sr_set_device (savestring (p, q - p)); | |
c906108c SS |
122 | |
123 | /* look for baud rate. */ | |
c5aa993b | 124 | n = strtol (q, &p, 10); |
c906108c SS |
125 | |
126 | /* check for missing or empty baud rate. */ | |
c5aa993b | 127 | CHECKDONE (p, q); |
c906108c SS |
128 | baud_rate = n; |
129 | ||
130 | /* look for debug value. */ | |
c5aa993b | 131 | n = strtol (p, &q, 10); |
c906108c SS |
132 | |
133 | /* check for missing or empty debug value. */ | |
c5aa993b JM |
134 | CHECKDONE (p, q); |
135 | sr_set_debug (n); | |
c906108c SS |
136 | |
137 | /* scan off remaining white space. */ | |
c5aa993b | 138 | for (p = q; isspace (*p); ++p);; |
c906108c SS |
139 | |
140 | /* if not end of string, then there's unrecognized junk. */ | |
141 | if (*p != '\0') | |
c5aa993b | 142 | usage (proto, p); |
c906108c SS |
143 | |
144 | return; | |
145 | } | |
146 | ||
147 | void | |
fba45db2 | 148 | gr_generic_checkin (void) |
c906108c | 149 | { |
c5aa993b JM |
150 | sr_write_cr (""); |
151 | gr_expect_prompt (); | |
c906108c SS |
152 | } |
153 | ||
154 | void | |
fba45db2 | 155 | gr_open (char *args, int from_tty, struct gr_settings *gr) |
c906108c | 156 | { |
f1d7622b C |
157 | DCACHE *dcache; |
158 | ||
c5aa993b JM |
159 | target_preopen (from_tty); |
160 | sr_scan_args (gr->ops->to_shortname, args); | |
161 | unpush_target (gr->ops); | |
c906108c SS |
162 | |
163 | gr_settings = gr; | |
164 | ||
f1d7622b C |
165 | if ((dcache = gr_get_dcache()) == NULL) |
166 | gr_set_dcache (dcache_init (gr->readfunc, gr->writefunc)); | |
167 | else | |
e99586d5 | 168 | dcache_invd (dcache); |
c906108c | 169 | |
c5aa993b | 170 | if (sr_get_desc () != NULL) |
c906108c SS |
171 | gr_close (0); |
172 | ||
173 | /* If no args are specified, then we use the device specified by a | |
174 | previous command or "set remotedevice". But if there is no | |
175 | device, better stop now, not dump core. */ | |
176 | ||
177 | if (sr_get_device () == NULL) | |
178 | usage (gr->ops->to_shortname, NULL); | |
179 | ||
c5aa993b JM |
180 | sr_set_desc (SERIAL_OPEN (sr_get_device ())); |
181 | if (!sr_get_desc ()) | |
182 | perror_with_name ((char *) sr_get_device ()); | |
c906108c SS |
183 | |
184 | if (baud_rate != -1) | |
185 | { | |
c5aa993b | 186 | if (SERIAL_SETBAUDRATE (sr_get_desc (), baud_rate) != 0) |
c906108c | 187 | { |
c5aa993b JM |
188 | SERIAL_CLOSE (sr_get_desc ()); |
189 | perror_with_name (sr_get_device ()); | |
c906108c SS |
190 | } |
191 | } | |
192 | ||
c5aa993b | 193 | SERIAL_RAW (sr_get_desc ()); |
c906108c SS |
194 | |
195 | /* If there is something sitting in the buffer we might take it as a | |
196 | response to a command, which would be bad. */ | |
197 | SERIAL_FLUSH_INPUT (sr_get_desc ()); | |
198 | ||
199 | /* default retries */ | |
c5aa993b JM |
200 | if (sr_get_retries () == 0) |
201 | sr_set_retries (1); | |
c906108c SS |
202 | |
203 | /* default clear breakpoint function */ | |
204 | if (gr_settings->clear_all_breakpoints == NULL) | |
205 | gr_settings->clear_all_breakpoints = remove_breakpoints; | |
206 | ||
207 | if (from_tty) | |
208 | { | |
209 | printf_filtered ("Remote debugging using `%s'", sr_get_device ()); | |
210 | if (baud_rate != -1) | |
211 | printf_filtered (" at baud rate of %d", | |
212 | baud_rate); | |
213 | printf_filtered ("\n"); | |
214 | } | |
215 | ||
c5aa993b JM |
216 | push_target (gr->ops); |
217 | gr_checkin (); | |
c906108c SS |
218 | gr_clear_all_breakpoints (); |
219 | return; | |
220 | } | |
221 | ||
222 | /* Read a character from the remote system masking it down to 7 bits | |
223 | and doing all the fancy timeout stuff. */ | |
224 | ||
225 | int | |
fba45db2 | 226 | sr_readchar (void) |
c906108c SS |
227 | { |
228 | int buf; | |
229 | ||
c5aa993b | 230 | buf = SERIAL_READCHAR (sr_get_desc (), sr_get_timeout ()); |
c906108c SS |
231 | |
232 | if (buf == SERIAL_TIMEOUT) | |
233 | error ("Timeout reading from remote system."); | |
234 | ||
c5aa993b | 235 | if (sr_get_debug () > 0) |
c906108c SS |
236 | printf_unfiltered ("%c", buf); |
237 | ||
238 | return buf & 0x7f; | |
239 | } | |
240 | ||
241 | int | |
fba45db2 | 242 | sr_pollchar (void) |
c906108c SS |
243 | { |
244 | int buf; | |
245 | ||
c5aa993b | 246 | buf = SERIAL_READCHAR (sr_get_desc (), 0); |
c906108c SS |
247 | if (buf == SERIAL_TIMEOUT) |
248 | buf = 0; | |
c5aa993b | 249 | if (sr_get_debug () > 0) |
c906108c SS |
250 | { |
251 | if (buf) | |
c5aa993b | 252 | printf_unfiltered ("%c", buf); |
c906108c | 253 | else |
c5aa993b | 254 | printf_unfiltered ("<empty character poll>"); |
c906108c SS |
255 | } |
256 | ||
257 | return buf & 0x7f; | |
258 | } | |
259 | ||
260 | /* Keep discarding input from the remote system, until STRING is found. | |
261 | Let the user break out immediately. */ | |
262 | void | |
fba45db2 | 263 | sr_expect (char *string) |
c906108c SS |
264 | { |
265 | char *p = string; | |
266 | ||
8edbea78 | 267 | immediate_quit++; |
c906108c SS |
268 | while (1) |
269 | { | |
270 | if (sr_readchar () == *p) | |
271 | { | |
272 | p++; | |
273 | if (*p == '\0') | |
274 | { | |
8edbea78 | 275 | immediate_quit--; |
c906108c SS |
276 | return; |
277 | } | |
278 | } | |
279 | else | |
280 | p = string; | |
281 | } | |
282 | } | |
283 | ||
284 | void | |
fba45db2 | 285 | sr_write (char *a, int l) |
c906108c SS |
286 | { |
287 | int i; | |
288 | ||
c5aa993b | 289 | if (SERIAL_WRITE (sr_get_desc (), a, l) != 0) |
c906108c SS |
290 | perror_with_name ("sr_write: Error writing to remote"); |
291 | ||
c5aa993b | 292 | if (sr_get_debug () > 0) |
c906108c SS |
293 | for (i = 0; i < l; i++) |
294 | printf_unfiltered ("%c", a[i]); | |
295 | ||
296 | return; | |
297 | } | |
298 | ||
299 | void | |
fba45db2 | 300 | sr_write_cr (char *s) |
c906108c SS |
301 | { |
302 | sr_write (s, strlen (s)); | |
303 | sr_write ("\r", 1); | |
304 | return; | |
305 | } | |
306 | ||
307 | int | |
fba45db2 | 308 | sr_timed_read (char *buf, int n) |
c906108c SS |
309 | { |
310 | int i; | |
311 | char c; | |
312 | ||
313 | i = 0; | |
314 | while (i < n) | |
315 | { | |
316 | c = sr_readchar (); | |
317 | ||
318 | if (c == 0) | |
319 | return i; | |
320 | buf[i] = c; | |
321 | i++; | |
322 | ||
323 | } | |
324 | return i; | |
325 | } | |
326 | ||
327 | /* Get a hex digit from the remote system & return its value. If | |
328 | ignore_space is nonzero, ignore spaces (not newline, tab, etc). */ | |
329 | ||
330 | int | |
fba45db2 | 331 | sr_get_hex_digit (int ignore_space) |
c906108c SS |
332 | { |
333 | int ch; | |
334 | ||
335 | while (1) | |
336 | { | |
337 | ch = sr_readchar (); | |
338 | if (ch >= '0' && ch <= '9') | |
339 | return ch - '0'; | |
340 | else if (ch >= 'A' && ch <= 'F') | |
341 | return ch - 'A' + 10; | |
342 | else if (ch >= 'a' && ch <= 'f') | |
343 | return ch - 'a' + 10; | |
344 | else if (ch != ' ' || !ignore_space) | |
345 | { | |
346 | gr_expect_prompt (); | |
347 | error ("Invalid hex digit from remote system."); | |
348 | } | |
349 | } | |
350 | } | |
351 | ||
352 | /* Get a byte from the remote and put it in *BYT. Accept any number | |
353 | leading spaces. */ | |
354 | void | |
fba45db2 | 355 | sr_get_hex_byte (char *byt) |
c906108c SS |
356 | { |
357 | int val; | |
358 | ||
359 | val = sr_get_hex_digit (1) << 4; | |
360 | val |= sr_get_hex_digit (0); | |
361 | *byt = val; | |
362 | } | |
363 | ||
364 | /* Read a 32-bit hex word from the remote, preceded by a space */ | |
365 | long | |
fba45db2 | 366 | sr_get_hex_word (void) |
c906108c SS |
367 | { |
368 | long val; | |
369 | int j; | |
370 | ||
371 | val = 0; | |
372 | for (j = 0; j < 8; j++) | |
373 | val = (val << 4) + sr_get_hex_digit (j == 0); | |
374 | return val; | |
375 | } | |
376 | ||
377 | /* Put a command string, in args, out to the remote. The remote is assumed to | |
378 | be in raw mode, all writing/reading done through desc. | |
379 | Ouput from the remote is placed on the users terminal until the | |
380 | prompt from the remote is seen. | |
381 | FIXME: Can't handle commands that take input. */ | |
382 | ||
383 | static void | |
fba45db2 | 384 | sr_com (char *args, int fromtty) |
c906108c SS |
385 | { |
386 | sr_check_open (); | |
387 | ||
388 | if (!args) | |
389 | return; | |
390 | ||
391 | /* Clear all input so only command relative output is displayed */ | |
392 | ||
393 | sr_write_cr (args); | |
394 | sr_write ("\030", 1); | |
395 | registers_changed (); | |
396 | gr_expect_prompt (); | |
397 | } | |
398 | ||
399 | void | |
fba45db2 | 400 | gr_close (int quitting) |
c906108c | 401 | { |
c5aa993b | 402 | gr_clear_all_breakpoints (); |
c906108c | 403 | |
c5aa993b | 404 | if (sr_is_open ()) |
c906108c | 405 | { |
c5aa993b JM |
406 | SERIAL_CLOSE (sr_get_desc ()); |
407 | sr_set_desc (NULL); | |
c906108c SS |
408 | } |
409 | ||
410 | return; | |
411 | } | |
412 | ||
413 | /* gr_detach() | |
414 | takes a program previously attached to and detaches it. | |
415 | We better not have left any breakpoints | |
416 | in the program or it'll die when it hits one. | |
417 | Close the open connection to the remote debugger. | |
418 | Use this when you want to detach and do something else | |
419 | with your gdb. */ | |
420 | ||
421 | void | |
fba45db2 | 422 | gr_detach (char *args, int from_tty) |
c906108c SS |
423 | { |
424 | if (args) | |
425 | error ("Argument given to \"detach\" when remotely debugging."); | |
c5aa993b JM |
426 | |
427 | if (sr_is_open ()) | |
c906108c SS |
428 | gr_clear_all_breakpoints (); |
429 | ||
430 | pop_target (); | |
431 | if (from_tty) | |
432 | puts_filtered ("Ending remote debugging.\n"); | |
433 | ||
434 | return; | |
c5aa993b | 435 | } |
c906108c SS |
436 | |
437 | void | |
fba45db2 | 438 | gr_files_info (struct target_ops *ops) |
c906108c SS |
439 | { |
440 | #ifdef __GO32__ | |
441 | printf_filtered ("\tAttached to DOS asynctsr\n"); | |
442 | #else | |
c5aa993b | 443 | printf_filtered ("\tAttached to %s", sr_get_device ()); |
c906108c SS |
444 | if (baud_rate != -1) |
445 | printf_filtered ("at %d baud", baud_rate); | |
446 | printf_filtered ("\n"); | |
447 | #endif | |
448 | ||
449 | if (exec_bfd) | |
450 | { | |
451 | printf_filtered ("\tand running program %s\n", | |
452 | bfd_get_filename (exec_bfd)); | |
453 | } | |
454 | printf_filtered ("\tusing the %s protocol.\n", ops->to_shortname); | |
455 | } | |
456 | ||
457 | void | |
fba45db2 | 458 | gr_mourn (void) |
c906108c SS |
459 | { |
460 | gr_clear_all_breakpoints (); | |
c5aa993b | 461 | unpush_target (gr_get_ops ()); |
c906108c SS |
462 | generic_mourn_inferior (); |
463 | } | |
464 | ||
465 | void | |
fba45db2 | 466 | gr_kill (void) |
c906108c SS |
467 | { |
468 | return; | |
469 | } | |
470 | ||
471 | /* This is called not only when we first attach, but also when the | |
472 | user types "run" after having attached. */ | |
473 | void | |
fba45db2 | 474 | gr_create_inferior (char *execfile, char *args, char **env) |
c906108c SS |
475 | { |
476 | int entry_pt; | |
477 | ||
478 | if (args && *args) | |
479 | error ("Can't pass arguments to remote process."); | |
480 | ||
481 | if (execfile == 0 || exec_bfd == 0) | |
482 | error ("No executable file specified"); | |
483 | ||
484 | entry_pt = (int) bfd_get_start_address (exec_bfd); | |
485 | sr_check_open (); | |
486 | ||
487 | gr_kill (); | |
488 | gr_clear_all_breakpoints (); | |
489 | ||
490 | init_wait_for_inferior (); | |
c5aa993b | 491 | gr_checkin (); |
c906108c SS |
492 | |
493 | insert_breakpoints (); /* Needed to get correct instruction in cache */ | |
494 | proceed (entry_pt, -1, 0); | |
495 | } | |
496 | ||
497 | /* Given a null terminated list of strings LIST, read the input until we find one of | |
498 | them. Return the index of the string found or -1 on error. '?' means match | |
499 | any single character. Note that with the algorithm we use, the initial | |
500 | character of the string cannot recur in the string, or we will not find some | |
501 | cases of the string in the input. If PASSTHROUGH is non-zero, then | |
502 | pass non-matching data on. */ | |
503 | ||
504 | int | |
832c69cf | 505 | gr_multi_scan (char *list[], int passthrough) |
c906108c | 506 | { |
c5aa993b JM |
507 | char *swallowed = NULL; /* holding area */ |
508 | char *swallowed_p = swallowed; /* Current position in swallowed. */ | |
c906108c SS |
509 | int ch; |
510 | int ch_handled; | |
511 | int i; | |
512 | int string_count; | |
513 | int max_length; | |
514 | char **plist; | |
515 | ||
516 | /* Look through the strings. Count them. Find the largest one so we can | |
517 | allocate a holding area. */ | |
518 | ||
519 | for (max_length = string_count = i = 0; | |
520 | list[i] != NULL; | |
521 | ++i, ++string_count) | |
522 | { | |
c5aa993b | 523 | int length = strlen (list[i]); |
c906108c SS |
524 | |
525 | if (length > max_length) | |
526 | max_length = length; | |
527 | } | |
528 | ||
529 | /* if we have no strings, then something is wrong. */ | |
530 | if (string_count == 0) | |
c5aa993b | 531 | return (-1); |
c906108c SS |
532 | |
533 | /* otherwise, we will need a holding area big enough to hold almost two | |
534 | copies of our largest string. */ | |
c5aa993b | 535 | swallowed_p = swallowed = alloca (max_length << 1); |
c906108c SS |
536 | |
537 | /* and a list of pointers to current scan points. */ | |
c5aa993b | 538 | plist = (char **) alloca (string_count * sizeof (*plist)); |
c906108c SS |
539 | |
540 | /* and initialize */ | |
541 | for (i = 0; i < string_count; ++i) | |
542 | plist[i] = list[i]; | |
543 | ||
c5aa993b | 544 | for (ch = sr_readchar (); /* loop forever */ ; ch = sr_readchar ()) |
c906108c | 545 | { |
c5aa993b | 546 | QUIT; /* Let user quit and leave process running */ |
c906108c SS |
547 | ch_handled = 0; |
548 | ||
549 | for (i = 0; i < string_count; ++i) | |
550 | { | |
551 | if (ch == *plist[i] || *plist[i] == '?') | |
552 | { | |
553 | ++plist[i]; | |
554 | if (*plist[i] == '\0') | |
c5aa993b | 555 | return (i); |
c906108c SS |
556 | |
557 | if (!ch_handled) | |
558 | *swallowed_p++ = ch; | |
559 | ||
560 | ch_handled = 1; | |
561 | } | |
562 | else | |
563 | plist[i] = list[i]; | |
564 | } | |
565 | ||
566 | if (!ch_handled) | |
567 | { | |
568 | char *p; | |
569 | ||
570 | /* Print out any characters which have been swallowed. */ | |
571 | if (passthrough) | |
572 | { | |
573 | for (p = swallowed; p < swallowed_p; ++p) | |
574 | fputc_unfiltered (*p, gdb_stdout); | |
575 | ||
576 | fputc_unfiltered (ch, gdb_stdout); | |
577 | } | |
578 | ||
579 | swallowed_p = swallowed; | |
580 | } | |
581 | } | |
582 | #if 0 | |
583 | /* Never reached. */ | |
c5aa993b | 584 | return (-1); |
c906108c SS |
585 | #endif |
586 | } | |
587 | ||
588 | /* Get ready to modify the registers array. On machines which store | |
589 | individual registers, this doesn't need to do anything. On machines | |
590 | which store all the registers in one fell swoop, this makes sure | |
591 | that registers contains all the registers from the program being | |
592 | debugged. */ | |
593 | ||
594 | void | |
fba45db2 | 595 | gr_prepare_to_store (void) |
c906108c SS |
596 | { |
597 | /* Do nothing, since we assume we can store individual regs */ | |
598 | } | |
599 | ||
c906108c | 600 | void |
fba45db2 | 601 | _initialize_sr_support (void) |
c906108c SS |
602 | { |
603 | /* FIXME-now: if target is open... */ | |
604 | add_show_from_set (add_set_cmd ("remotedevice", no_class, | |
c5aa993b | 605 | var_filename, (char *) &sr_settings.device, |
c906108c SS |
606 | "Set device for remote serial I/O.\n\ |
607 | This device is used as the serial port when debugging using remote\n\ | |
608 | targets.", &setlist), | |
609 | &showlist); | |
610 | ||
611 | add_com ("remote <command>", class_obscure, sr_com, | |
612 | "Send a command to the remote monitor."); | |
613 | ||
614 | } |