1 /* Generic support for remote debugging interfaces.
3 Copyright 1993, 1994, 1995, 1996, 1998, 2000, 2001
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* This file actually contains two distinct logical "packages". They
24 are packaged together in this one file because they are typically
27 The first package is an addition to the serial package. The
28 addition provides reading and writing with debugging output and
29 timeouts based on user settable variables. These routines are
30 intended to support serial port based remote backends. These
31 functions are prefixed with sr_.
33 The second package is a collection of more or less generic
34 functions for use by remote backends. They support user settable
35 variables for debugging, retries, and the like.
39 * a pass through mode a la kermit or telnet.
41 * ask remote to change his baud rate.
47 #include "gdb_string.h"
51 #include "gdbcore.h" /* for exec_bfd */
52 #include "inferior.h" /* for generic_mourn_inferior */
53 #include "remote-utils.h"
57 void _initialize_sr_support (void);
59 struct _sr_settings sr_settings
=
63 remote-bug.c had "with a timeout of 2, we time out waiting for
64 the prompt after an s-record dump."
66 remote.c had (2): This was 5 seconds, which is a long time to
67 sit and wait. Unless this is going though some terminal server
68 or multiplexer or other form of hairy serial connection, I
69 would think 2 seconds would be plenty.
74 NULL
, /* descriptor */
77 struct gr_settings
*gr_settings
= NULL
;
79 static void usage (char *, char *);
80 static void sr_com (char *, int);
83 usage (char *proto
, char *junk
)
86 fprintf_unfiltered (gdb_stderr
, "Unrecognized arguments: `%s'.\n", junk
);
88 error ("Usage: target %s [DEVICE [SPEED [DEBUG]]]\n\
89 where DEVICE is the name of a device or HOST:PORT", proto
);
94 #define CHECKDONE(p, q) \
106 sr_scan_args (char *proto
, char *args
)
111 /* if no args, then nothing to do. */
112 if (args
== NULL
|| *args
== '\0')
115 /* scan off white space. */
116 for (p
= args
; isspace (*p
); ++p
);;
118 /* find end of device name. */
119 for (q
= p
; *q
!= '\0' && !isspace (*q
); ++q
);;
121 /* check for missing or empty device name. */
123 sr_set_device (savestring (p
, q
- p
));
125 /* look for baud rate. */
126 n
= strtol (q
, &p
, 10);
128 /* check for missing or empty baud rate. */
132 /* look for debug value. */
133 n
= strtol (p
, &q
, 10);
135 /* check for missing or empty debug value. */
139 /* scan off remaining white space. */
140 for (p
= q
; isspace (*p
); ++p
);;
142 /* if not end of string, then there's unrecognized junk. */
150 gr_generic_checkin (void)
157 gr_open (char *args
, int from_tty
, struct gr_settings
*gr
)
159 target_preopen (from_tty
);
160 sr_scan_args (gr
->ops
->to_shortname
, args
);
161 unpush_target (gr
->ops
);
165 if (sr_get_desc () != NULL
)
168 /* If no args are specified, then we use the device specified by a
169 previous command or "set remotedevice". But if there is no
170 device, better stop now, not dump core. */
172 if (sr_get_device () == NULL
)
173 usage (gr
->ops
->to_shortname
, NULL
);
175 sr_set_desc (serial_open (sr_get_device ()));
177 perror_with_name ((char *) sr_get_device ());
181 if (serial_setbaudrate (sr_get_desc (), baud_rate
) != 0)
183 serial_close (sr_get_desc ());
184 perror_with_name (sr_get_device ());
188 serial_raw (sr_get_desc ());
190 /* If there is something sitting in the buffer we might take it as a
191 response to a command, which would be bad. */
192 serial_flush_input (sr_get_desc ());
194 /* default retries */
195 if (sr_get_retries () == 0)
198 /* default clear breakpoint function */
199 if (gr_settings
->clear_all_breakpoints
== NULL
)
200 gr_settings
->clear_all_breakpoints
= remove_breakpoints
;
204 printf_filtered ("Remote debugging using `%s'", sr_get_device ());
206 printf_filtered (" at baud rate of %d",
208 printf_filtered ("\n");
211 push_target (gr
->ops
);
213 gr_clear_all_breakpoints ();
217 /* Read a character from the remote system masking it down to 7 bits
218 and doing all the fancy timeout stuff. */
225 buf
= serial_readchar (sr_get_desc (), sr_get_timeout ());
227 if (buf
== SERIAL_TIMEOUT
)
228 error ("Timeout reading from remote system.");
230 if (sr_get_debug () > 0)
231 printf_unfiltered ("%c", buf
);
241 buf
= serial_readchar (sr_get_desc (), 0);
242 if (buf
== SERIAL_TIMEOUT
)
244 if (sr_get_debug () > 0)
247 printf_unfiltered ("%c", buf
);
249 printf_unfiltered ("<empty character poll>");
255 /* Keep discarding input from the remote system, until STRING is found.
256 Let the user break out immediately. */
258 sr_expect (char *string
)
265 if (sr_readchar () == *p
)
280 sr_write (char *a
, int l
)
284 if (serial_write (sr_get_desc (), a
, l
) != 0)
285 perror_with_name ("sr_write: Error writing to remote");
287 if (sr_get_debug () > 0)
288 for (i
= 0; i
< l
; i
++)
289 printf_unfiltered ("%c", a
[i
]);
295 sr_write_cr (char *s
)
297 sr_write (s
, strlen (s
));
303 sr_timed_read (char *buf
, int n
)
322 /* Get a hex digit from the remote system & return its value. If
323 ignore_space is nonzero, ignore spaces (not newline, tab, etc). */
326 sr_get_hex_digit (int ignore_space
)
333 if (ch
>= '0' && ch
<= '9')
335 else if (ch
>= 'A' && ch
<= 'F')
336 return ch
- 'A' + 10;
337 else if (ch
>= 'a' && ch
<= 'f')
338 return ch
- 'a' + 10;
339 else if (ch
!= ' ' || !ignore_space
)
342 error ("Invalid hex digit from remote system.");
347 /* Get a byte from the remote and put it in *BYT. Accept any number
350 sr_get_hex_byte (char *byt
)
354 val
= sr_get_hex_digit (1) << 4;
355 val
|= sr_get_hex_digit (0);
359 /* Read a 32-bit hex word from the remote, preceded by a space */
361 sr_get_hex_word (void)
367 for (j
= 0; j
< 8; j
++)
368 val
= (val
<< 4) + sr_get_hex_digit (j
== 0);
372 /* Put a command string, in args, out to the remote. The remote is assumed to
373 be in raw mode, all writing/reading done through desc.
374 Ouput from the remote is placed on the users terminal until the
375 prompt from the remote is seen.
376 FIXME: Can't handle commands that take input. */
379 sr_com (char *args
, int fromtty
)
386 /* Clear all input so only command relative output is displayed */
389 sr_write ("\030", 1);
390 registers_changed ();
395 gr_close (int quitting
)
397 gr_clear_all_breakpoints ();
401 serial_close (sr_get_desc ());
409 takes a program previously attached to and detaches it.
410 We better not have left any breakpoints
411 in the program or it'll die when it hits one.
412 Close the open connection to the remote debugger.
413 Use this when you want to detach and do something else
417 gr_detach (char *args
, int from_tty
)
420 error ("Argument given to \"detach\" when remotely debugging.");
423 gr_clear_all_breakpoints ();
427 puts_filtered ("Ending remote debugging.\n");
433 gr_files_info (struct target_ops
*ops
)
436 printf_filtered ("\tAttached to DOS asynctsr\n");
438 printf_filtered ("\tAttached to %s", sr_get_device ());
440 printf_filtered ("at %d baud", baud_rate
);
441 printf_filtered ("\n");
446 printf_filtered ("\tand running program %s\n",
447 bfd_get_filename (exec_bfd
));
449 printf_filtered ("\tusing the %s protocol.\n", ops
->to_shortname
);
455 gr_clear_all_breakpoints ();
456 unpush_target (gr_get_ops ());
457 generic_mourn_inferior ();
466 /* This is called not only when we first attach, but also when the
467 user types "run" after having attached. */
469 gr_create_inferior (char *execfile
, char *args
, char **env
)
474 error ("Can't pass arguments to remote process.");
476 if (execfile
== 0 || exec_bfd
== 0)
477 error ("No executable file specified");
479 entry_pt
= (int) bfd_get_start_address (exec_bfd
);
483 gr_clear_all_breakpoints ();
485 init_wait_for_inferior ();
488 insert_breakpoints (); /* Needed to get correct instruction in cache */
489 proceed (entry_pt
, -1, 0);
492 /* Given a null terminated list of strings LIST, read the input until we find one of
493 them. Return the index of the string found or -1 on error. '?' means match
494 any single character. Note that with the algorithm we use, the initial
495 character of the string cannot recur in the string, or we will not find some
496 cases of the string in the input. If PASSTHROUGH is non-zero, then
497 pass non-matching data on. */
500 gr_multi_scan (char *list
[], int passthrough
)
502 char *swallowed
= NULL
; /* holding area */
503 char *swallowed_p
= swallowed
; /* Current position in swallowed. */
511 /* Look through the strings. Count them. Find the largest one so we can
512 allocate a holding area. */
514 for (max_length
= string_count
= i
= 0;
518 int length
= strlen (list
[i
]);
520 if (length
> max_length
)
524 /* if we have no strings, then something is wrong. */
525 if (string_count
== 0)
528 /* otherwise, we will need a holding area big enough to hold almost two
529 copies of our largest string. */
530 swallowed_p
= swallowed
= alloca (max_length
<< 1);
532 /* and a list of pointers to current scan points. */
533 plist
= (char **) alloca (string_count
* sizeof (*plist
));
536 for (i
= 0; i
< string_count
; ++i
)
539 for (ch
= sr_readchar (); /* loop forever */ ; ch
= sr_readchar ())
541 QUIT
; /* Let user quit and leave process running */
544 for (i
= 0; i
< string_count
; ++i
)
546 if (ch
== *plist
[i
] || *plist
[i
] == '?')
549 if (*plist
[i
] == '\0')
565 /* Print out any characters which have been swallowed. */
568 for (p
= swallowed
; p
< swallowed_p
; ++p
)
569 fputc_unfiltered (*p
, gdb_stdout
);
571 fputc_unfiltered (ch
, gdb_stdout
);
574 swallowed_p
= swallowed
;
583 /* Get ready to modify the registers array. On machines which store
584 individual registers, this doesn't need to do anything. On machines
585 which store all the registers in one fell swoop, this makes sure
586 that registers contains all the registers from the program being
590 gr_prepare_to_store (void)
592 /* Do nothing, since we assume we can store individual regs */
596 _initialize_sr_support (void)
598 /* FIXME-now: if target is open... */
599 add_show_from_set (add_set_cmd ("remotedevice", no_class
,
600 var_filename
, (char *) &sr_settings
.device
,
601 "Set device for remote serial I/O.\n\
602 This device is used as the serial port when debugging using remote\n\
603 targets.", &setlist
),
606 add_com ("remote <command>", class_obscure
, sr_com
,
607 "Send a command to the remote monitor.");