* gdb-stabs.h (SECT_OFF_MAX): Increase to 64.
[deliverable/binutils-gdb.git] / gdb / remote-utils.c
CommitLineData
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 55void _initialize_sr_support (void);
c906108c 56
c5aa993b
JM
57struct _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
75struct gr_settings *gr_settings = NULL;
76
a14ed312
KB
77static void usage (char *, char *);
78static void sr_com (char *, int);
c906108c
SS
79
80static void
fba45db2 81usage (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\
87where 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
103void
fba45db2 104sr_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
147void
fba45db2 148gr_generic_checkin (void)
c906108c 149{
c5aa993b
JM
150 sr_write_cr ("");
151 gr_expect_prompt ();
c906108c
SS
152}
153
154void
fba45db2 155gr_open (char *args, int from_tty, struct gr_settings *gr)
c906108c 156{
c5aa993b
JM
157 target_preopen (from_tty);
158 sr_scan_args (gr->ops->to_shortname, args);
159 unpush_target (gr->ops);
c906108c
SS
160
161 gr_settings = gr;
162
c5aa993b 163 if (sr_get_desc () != NULL)
c906108c
SS
164 gr_close (0);
165
166 /* If no args are specified, then we use the device specified by a
167 previous command or "set remotedevice". But if there is no
168 device, better stop now, not dump core. */
169
170 if (sr_get_device () == NULL)
171 usage (gr->ops->to_shortname, NULL);
172
c5aa993b
JM
173 sr_set_desc (SERIAL_OPEN (sr_get_device ()));
174 if (!sr_get_desc ())
175 perror_with_name ((char *) sr_get_device ());
c906108c
SS
176
177 if (baud_rate != -1)
178 {
c5aa993b 179 if (SERIAL_SETBAUDRATE (sr_get_desc (), baud_rate) != 0)
c906108c 180 {
c5aa993b
JM
181 SERIAL_CLOSE (sr_get_desc ());
182 perror_with_name (sr_get_device ());
c906108c
SS
183 }
184 }
185
c5aa993b 186 SERIAL_RAW (sr_get_desc ());
c906108c
SS
187
188 /* If there is something sitting in the buffer we might take it as a
189 response to a command, which would be bad. */
190 SERIAL_FLUSH_INPUT (sr_get_desc ());
191
192 /* default retries */
c5aa993b
JM
193 if (sr_get_retries () == 0)
194 sr_set_retries (1);
c906108c
SS
195
196 /* default clear breakpoint function */
197 if (gr_settings->clear_all_breakpoints == NULL)
198 gr_settings->clear_all_breakpoints = remove_breakpoints;
199
200 if (from_tty)
201 {
202 printf_filtered ("Remote debugging using `%s'", sr_get_device ());
203 if (baud_rate != -1)
204 printf_filtered (" at baud rate of %d",
205 baud_rate);
206 printf_filtered ("\n");
207 }
208
c5aa993b
JM
209 push_target (gr->ops);
210 gr_checkin ();
c906108c
SS
211 gr_clear_all_breakpoints ();
212 return;
213}
214
215/* Read a character from the remote system masking it down to 7 bits
216 and doing all the fancy timeout stuff. */
217
218int
fba45db2 219sr_readchar (void)
c906108c
SS
220{
221 int buf;
222
c5aa993b 223 buf = SERIAL_READCHAR (sr_get_desc (), sr_get_timeout ());
c906108c
SS
224
225 if (buf == SERIAL_TIMEOUT)
226 error ("Timeout reading from remote system.");
227
c5aa993b 228 if (sr_get_debug () > 0)
c906108c
SS
229 printf_unfiltered ("%c", buf);
230
231 return buf & 0x7f;
232}
233
234int
fba45db2 235sr_pollchar (void)
c906108c
SS
236{
237 int buf;
238
c5aa993b 239 buf = SERIAL_READCHAR (sr_get_desc (), 0);
c906108c
SS
240 if (buf == SERIAL_TIMEOUT)
241 buf = 0;
c5aa993b 242 if (sr_get_debug () > 0)
c906108c
SS
243 {
244 if (buf)
c5aa993b 245 printf_unfiltered ("%c", buf);
c906108c 246 else
c5aa993b 247 printf_unfiltered ("<empty character poll>");
c906108c
SS
248 }
249
250 return buf & 0x7f;
251}
252
253/* Keep discarding input from the remote system, until STRING is found.
254 Let the user break out immediately. */
255void
fba45db2 256sr_expect (char *string)
c906108c
SS
257{
258 char *p = string;
259
8edbea78 260 immediate_quit++;
c906108c
SS
261 while (1)
262 {
263 if (sr_readchar () == *p)
264 {
265 p++;
266 if (*p == '\0')
267 {
8edbea78 268 immediate_quit--;
c906108c
SS
269 return;
270 }
271 }
272 else
273 p = string;
274 }
275}
276
277void
fba45db2 278sr_write (char *a, int l)
c906108c
SS
279{
280 int i;
281
c5aa993b 282 if (SERIAL_WRITE (sr_get_desc (), a, l) != 0)
c906108c
SS
283 perror_with_name ("sr_write: Error writing to remote");
284
c5aa993b 285 if (sr_get_debug () > 0)
c906108c
SS
286 for (i = 0; i < l; i++)
287 printf_unfiltered ("%c", a[i]);
288
289 return;
290}
291
292void
fba45db2 293sr_write_cr (char *s)
c906108c
SS
294{
295 sr_write (s, strlen (s));
296 sr_write ("\r", 1);
297 return;
298}
299
300int
fba45db2 301sr_timed_read (char *buf, int n)
c906108c
SS
302{
303 int i;
304 char c;
305
306 i = 0;
307 while (i < n)
308 {
309 c = sr_readchar ();
310
311 if (c == 0)
312 return i;
313 buf[i] = c;
314 i++;
315
316 }
317 return i;
318}
319
320/* Get a hex digit from the remote system & return its value. If
321 ignore_space is nonzero, ignore spaces (not newline, tab, etc). */
322
323int
fba45db2 324sr_get_hex_digit (int ignore_space)
c906108c
SS
325{
326 int ch;
327
328 while (1)
329 {
330 ch = sr_readchar ();
331 if (ch >= '0' && ch <= '9')
332 return ch - '0';
333 else if (ch >= 'A' && ch <= 'F')
334 return ch - 'A' + 10;
335 else if (ch >= 'a' && ch <= 'f')
336 return ch - 'a' + 10;
337 else if (ch != ' ' || !ignore_space)
338 {
339 gr_expect_prompt ();
340 error ("Invalid hex digit from remote system.");
341 }
342 }
343}
344
345/* Get a byte from the remote and put it in *BYT. Accept any number
346 leading spaces. */
347void
fba45db2 348sr_get_hex_byte (char *byt)
c906108c
SS
349{
350 int val;
351
352 val = sr_get_hex_digit (1) << 4;
353 val |= sr_get_hex_digit (0);
354 *byt = val;
355}
356
357/* Read a 32-bit hex word from the remote, preceded by a space */
358long
fba45db2 359sr_get_hex_word (void)
c906108c
SS
360{
361 long val;
362 int j;
363
364 val = 0;
365 for (j = 0; j < 8; j++)
366 val = (val << 4) + sr_get_hex_digit (j == 0);
367 return val;
368}
369
370/* Put a command string, in args, out to the remote. The remote is assumed to
371 be in raw mode, all writing/reading done through desc.
372 Ouput from the remote is placed on the users terminal until the
373 prompt from the remote is seen.
374 FIXME: Can't handle commands that take input. */
375
376static void
fba45db2 377sr_com (char *args, int fromtty)
c906108c
SS
378{
379 sr_check_open ();
380
381 if (!args)
382 return;
383
384 /* Clear all input so only command relative output is displayed */
385
386 sr_write_cr (args);
387 sr_write ("\030", 1);
388 registers_changed ();
389 gr_expect_prompt ();
390}
391
392void
fba45db2 393gr_close (int quitting)
c906108c 394{
c5aa993b 395 gr_clear_all_breakpoints ();
c906108c 396
c5aa993b 397 if (sr_is_open ())
c906108c 398 {
c5aa993b
JM
399 SERIAL_CLOSE (sr_get_desc ());
400 sr_set_desc (NULL);
c906108c
SS
401 }
402
403 return;
404}
405
406/* gr_detach()
407 takes a program previously attached to and detaches it.
408 We better not have left any breakpoints
409 in the program or it'll die when it hits one.
410 Close the open connection to the remote debugger.
411 Use this when you want to detach and do something else
412 with your gdb. */
413
414void
fba45db2 415gr_detach (char *args, int from_tty)
c906108c
SS
416{
417 if (args)
418 error ("Argument given to \"detach\" when remotely debugging.");
c5aa993b
JM
419
420 if (sr_is_open ())
c906108c
SS
421 gr_clear_all_breakpoints ();
422
423 pop_target ();
424 if (from_tty)
425 puts_filtered ("Ending remote debugging.\n");
426
427 return;
c5aa993b 428}
c906108c
SS
429
430void
fba45db2 431gr_files_info (struct target_ops *ops)
c906108c
SS
432{
433#ifdef __GO32__
434 printf_filtered ("\tAttached to DOS asynctsr\n");
435#else
c5aa993b 436 printf_filtered ("\tAttached to %s", sr_get_device ());
c906108c
SS
437 if (baud_rate != -1)
438 printf_filtered ("at %d baud", baud_rate);
439 printf_filtered ("\n");
440#endif
441
442 if (exec_bfd)
443 {
444 printf_filtered ("\tand running program %s\n",
445 bfd_get_filename (exec_bfd));
446 }
447 printf_filtered ("\tusing the %s protocol.\n", ops->to_shortname);
448}
449
450void
fba45db2 451gr_mourn (void)
c906108c
SS
452{
453 gr_clear_all_breakpoints ();
c5aa993b 454 unpush_target (gr_get_ops ());
c906108c
SS
455 generic_mourn_inferior ();
456}
457
458void
fba45db2 459gr_kill (void)
c906108c
SS
460{
461 return;
462}
463
464/* This is called not only when we first attach, but also when the
465 user types "run" after having attached. */
466void
fba45db2 467gr_create_inferior (char *execfile, char *args, char **env)
c906108c
SS
468{
469 int entry_pt;
470
471 if (args && *args)
472 error ("Can't pass arguments to remote process.");
473
474 if (execfile == 0 || exec_bfd == 0)
475 error ("No executable file specified");
476
477 entry_pt = (int) bfd_get_start_address (exec_bfd);
478 sr_check_open ();
479
480 gr_kill ();
481 gr_clear_all_breakpoints ();
482
483 init_wait_for_inferior ();
c5aa993b 484 gr_checkin ();
c906108c
SS
485
486 insert_breakpoints (); /* Needed to get correct instruction in cache */
487 proceed (entry_pt, -1, 0);
488}
489
490/* Given a null terminated list of strings LIST, read the input until we find one of
491 them. Return the index of the string found or -1 on error. '?' means match
492 any single character. Note that with the algorithm we use, the initial
493 character of the string cannot recur in the string, or we will not find some
494 cases of the string in the input. If PASSTHROUGH is non-zero, then
495 pass non-matching data on. */
496
497int
832c69cf 498gr_multi_scan (char *list[], int passthrough)
c906108c 499{
c5aa993b
JM
500 char *swallowed = NULL; /* holding area */
501 char *swallowed_p = swallowed; /* Current position in swallowed. */
c906108c
SS
502 int ch;
503 int ch_handled;
504 int i;
505 int string_count;
506 int max_length;
507 char **plist;
508
509 /* Look through the strings. Count them. Find the largest one so we can
510 allocate a holding area. */
511
512 for (max_length = string_count = i = 0;
513 list[i] != NULL;
514 ++i, ++string_count)
515 {
c5aa993b 516 int length = strlen (list[i]);
c906108c
SS
517
518 if (length > max_length)
519 max_length = length;
520 }
521
522 /* if we have no strings, then something is wrong. */
523 if (string_count == 0)
c5aa993b 524 return (-1);
c906108c
SS
525
526 /* otherwise, we will need a holding area big enough to hold almost two
527 copies of our largest string. */
c5aa993b 528 swallowed_p = swallowed = alloca (max_length << 1);
c906108c
SS
529
530 /* and a list of pointers to current scan points. */
c5aa993b 531 plist = (char **) alloca (string_count * sizeof (*plist));
c906108c
SS
532
533 /* and initialize */
534 for (i = 0; i < string_count; ++i)
535 plist[i] = list[i];
536
c5aa993b 537 for (ch = sr_readchar (); /* loop forever */ ; ch = sr_readchar ())
c906108c 538 {
c5aa993b 539 QUIT; /* Let user quit and leave process running */
c906108c
SS
540 ch_handled = 0;
541
542 for (i = 0; i < string_count; ++i)
543 {
544 if (ch == *plist[i] || *plist[i] == '?')
545 {
546 ++plist[i];
547 if (*plist[i] == '\0')
c5aa993b 548 return (i);
c906108c
SS
549
550 if (!ch_handled)
551 *swallowed_p++ = ch;
552
553 ch_handled = 1;
554 }
555 else
556 plist[i] = list[i];
557 }
558
559 if (!ch_handled)
560 {
561 char *p;
562
563 /* Print out any characters which have been swallowed. */
564 if (passthrough)
565 {
566 for (p = swallowed; p < swallowed_p; ++p)
567 fputc_unfiltered (*p, gdb_stdout);
568
569 fputc_unfiltered (ch, gdb_stdout);
570 }
571
572 swallowed_p = swallowed;
573 }
574 }
575#if 0
576 /* Never reached. */
c5aa993b 577 return (-1);
c906108c
SS
578#endif
579}
580
581/* Get ready to modify the registers array. On machines which store
582 individual registers, this doesn't need to do anything. On machines
583 which store all the registers in one fell swoop, this makes sure
584 that registers contains all the registers from the program being
585 debugged. */
586
587void
fba45db2 588gr_prepare_to_store (void)
c906108c
SS
589{
590 /* Do nothing, since we assume we can store individual regs */
591}
592
c906108c 593void
fba45db2 594_initialize_sr_support (void)
c906108c
SS
595{
596/* FIXME-now: if target is open... */
597 add_show_from_set (add_set_cmd ("remotedevice", no_class,
c5aa993b 598 var_filename, (char *) &sr_settings.device,
c906108c
SS
599 "Set device for remote serial I/O.\n\
600This device is used as the serial port when debugging using remote\n\
601targets.", &setlist),
602 &showlist);
603
604 add_com ("remote <command>", class_obscure, sr_com,
605 "Send a command to the remote monitor.");
606
607}
This page took 0.109398 seconds and 4 git commands to generate.