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