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