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