]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/remote-utils.c
import gdb-1999-07-07 post reformat
[thirdparty/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
55void _initialize_sr_support PARAMS ((void));
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
77static void usage PARAMS ((char *, char *));
78static void sr_com PARAMS ((char *, int));
79
80static void
c5aa993b 81usage (proto, junk)
c906108c
SS
82 char *proto;
83 char *junk;
84{
85 if (junk != NULL)
c5aa993b 86 fprintf_unfiltered (gdb_stderr, "Unrecognized arguments: `%s'.\n", junk);
c906108c
SS
87
88 error ("Usage: target %s [DEVICE [SPEED [DEBUG]]]\n\
89where 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
105void
c5aa993b 106sr_scan_args (proto, args)
c906108c
SS
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. */
c5aa993b 118 for (p = args; isspace (*p); ++p);;
c906108c
SS
119
120 /* find end of device name. */
c5aa993b 121 for (q = p; *q != '\0' && !isspace (*q); ++q);;
c906108c
SS
122
123 /* check for missing or empty device name. */
c5aa993b
JM
124 CHECKDONE (p, q);
125 sr_set_device (savestring (p, q - p));
c906108c
SS
126
127 /* look for baud rate. */
c5aa993b 128 n = strtol (q, &p, 10);
c906108c
SS
129
130 /* check for missing or empty baud rate. */
c5aa993b 131 CHECKDONE (p, q);
c906108c
SS
132 baud_rate = n;
133
134 /* look for debug value. */
c5aa993b 135 n = strtol (p, &q, 10);
c906108c
SS
136
137 /* check for missing or empty debug value. */
c5aa993b
JM
138 CHECKDONE (p, q);
139 sr_set_debug (n);
c906108c
SS
140
141 /* scan off remaining white space. */
c5aa993b 142 for (p = q; isspace (*p); ++p);;
c906108c
SS
143
144 /* if not end of string, then there's unrecognized junk. */
145 if (*p != '\0')
c5aa993b 146 usage (proto, p);
c906108c
SS
147
148 return;
149}
150
151void
c5aa993b 152gr_generic_checkin ()
c906108c 153{
c5aa993b
JM
154 sr_write_cr ("");
155 gr_expect_prompt ();
c906108c
SS
156}
157
158void
c5aa993b 159gr_open (args, from_tty, gr)
c906108c
SS
160 char *args;
161 int from_tty;
162 struct gr_settings *gr;
163{
c5aa993b
JM
164 target_preopen (from_tty);
165 sr_scan_args (gr->ops->to_shortname, args);
166 unpush_target (gr->ops);
c906108c
SS
167
168 gr_settings = gr;
169
c5aa993b 170 gr_set_dcache (dcache_init (gr->readfunc, gr->writefunc));
c906108c 171
c5aa993b 172 if (sr_get_desc () != NULL)
c906108c
SS
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
c5aa993b
JM
182 sr_set_desc (SERIAL_OPEN (sr_get_device ()));
183 if (!sr_get_desc ())
184 perror_with_name ((char *) sr_get_device ());
c906108c
SS
185
186 if (baud_rate != -1)
187 {
c5aa993b 188 if (SERIAL_SETBAUDRATE (sr_get_desc (), baud_rate) != 0)
c906108c 189 {
c5aa993b
JM
190 SERIAL_CLOSE (sr_get_desc ());
191 perror_with_name (sr_get_device ());
c906108c
SS
192 }
193 }
194
c5aa993b 195 SERIAL_RAW (sr_get_desc ());
c906108c
SS
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 */
c5aa993b
JM
202 if (sr_get_retries () == 0)
203 sr_set_retries (1);
c906108c
SS
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
c5aa993b
JM
218 push_target (gr->ops);
219 gr_checkin ();
c906108c
SS
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
227int
228sr_readchar ()
229{
230 int buf;
231
c5aa993b 232 buf = SERIAL_READCHAR (sr_get_desc (), sr_get_timeout ());
c906108c
SS
233
234 if (buf == SERIAL_TIMEOUT)
235 error ("Timeout reading from remote system.");
236
c5aa993b 237 if (sr_get_debug () > 0)
c906108c
SS
238 printf_unfiltered ("%c", buf);
239
240 return buf & 0x7f;
241}
242
243int
c5aa993b 244sr_pollchar ()
c906108c
SS
245{
246 int buf;
247
c5aa993b 248 buf = SERIAL_READCHAR (sr_get_desc (), 0);
c906108c
SS
249 if (buf == SERIAL_TIMEOUT)
250 buf = 0;
c5aa993b 251 if (sr_get_debug () > 0)
c906108c
SS
252 {
253 if (buf)
c5aa993b 254 printf_unfiltered ("%c", buf);
c906108c 255 else
c5aa993b 256 printf_unfiltered ("<empty character poll>");
c906108c
SS
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. */
264void
265sr_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
287void
288sr_write (a, l)
289 char *a;
290 int l;
291{
292 int i;
293
c5aa993b 294 if (SERIAL_WRITE (sr_get_desc (), a, l) != 0)
c906108c
SS
295 perror_with_name ("sr_write: Error writing to remote");
296
c5aa993b 297 if (sr_get_debug () > 0)
c906108c
SS
298 for (i = 0; i < l; i++)
299 printf_unfiltered ("%c", a[i]);
300
301 return;
302}
303
304void
305sr_write_cr (s)
306 char *s;
307{
308 sr_write (s, strlen (s));
309 sr_write ("\r", 1);
310 return;
311}
312
313int
314sr_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
338int
339sr_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. */
363void
364sr_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 */
375long
376sr_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
393static void
394sr_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
411void
c5aa993b 412gr_close (quitting)
c906108c
SS
413 int quitting;
414{
c5aa993b 415 gr_clear_all_breakpoints ();
c906108c 416
c5aa993b 417 if (sr_is_open ())
c906108c 418 {
c5aa993b
JM
419 SERIAL_CLOSE (sr_get_desc ());
420 sr_set_desc (NULL);
c906108c
SS
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
434void
c5aa993b 435gr_detach (args, from_tty)
c906108c
SS
436 char *args;
437 int from_tty;
438{
439 if (args)
440 error ("Argument given to \"detach\" when remotely debugging.");
c5aa993b
JM
441
442 if (sr_is_open ())
c906108c
SS
443 gr_clear_all_breakpoints ();
444
445 pop_target ();
446 if (from_tty)
447 puts_filtered ("Ending remote debugging.\n");
448
449 return;
c5aa993b 450}
c906108c
SS
451
452void
453gr_files_info (ops)
454 struct target_ops *ops;
455{
456#ifdef __GO32__
457 printf_filtered ("\tAttached to DOS asynctsr\n");
458#else
c5aa993b 459 printf_filtered ("\tAttached to %s", sr_get_device ());
c906108c
SS
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
473void
474gr_mourn ()
475{
476 gr_clear_all_breakpoints ();
c5aa993b 477 unpush_target (gr_get_ops ());
c906108c
SS
478 generic_mourn_inferior ();
479}
480
481void
482gr_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. */
489void
490gr_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 ();
c5aa993b 510 gr_checkin ();
c906108c
SS
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
523int
524gr_multi_scan (list, passthrough)
525 char *list[];
526 int passthrough;
527{
c5aa993b
JM
528 char *swallowed = NULL; /* holding area */
529 char *swallowed_p = swallowed; /* Current position in swallowed. */
c906108c
SS
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 {
c5aa993b 544 int length = strlen (list[i]);
c906108c
SS
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)
c5aa993b 552 return (-1);
c906108c
SS
553
554 /* otherwise, we will need a holding area big enough to hold almost two
555 copies of our largest string. */
c5aa993b 556 swallowed_p = swallowed = alloca (max_length << 1);
c906108c
SS
557
558 /* and a list of pointers to current scan points. */
c5aa993b 559 plist = (char **) alloca (string_count * sizeof (*plist));
c906108c
SS
560
561 /* and initialize */
562 for (i = 0; i < string_count; ++i)
563 plist[i] = list[i];
564
c5aa993b 565 for (ch = sr_readchar (); /* loop forever */ ; ch = sr_readchar ())
c906108c 566 {
c5aa993b 567 QUIT; /* Let user quit and leave process running */
c906108c
SS
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')
c5aa993b 576 return (i);
c906108c
SS
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. */
c5aa993b 605 return (-1);
c906108c
SS
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
615void
616gr_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 */
624int
625gr_fetch_word (addr)
626 CORE_ADDR addr;
627{
c5aa993b 628 return dcache_fetch (gr_get_dcache (), addr);
c906108c
SS
629}
630
631/* Write a word WORD into remote address ADDR.
632 This goes through the data cache. */
633
634void
635gr_store_word (addr, word)
636 CORE_ADDR addr;
637 int word;
638{
c5aa993b 639 dcache_poke (gr_get_dcache (), addr, word);
c906108c
SS
640}
641
642void
643_initialize_sr_support ()
644{
645/* FIXME-now: if target is open... */
646 add_show_from_set (add_set_cmd ("remotedevice", no_class,
c5aa993b 647 var_filename, (char *) &sr_settings.device,
c906108c
SS
648 "Set device for remote serial I/O.\n\
649This device is used as the serial port when debugging using remote\n\
650targets.", &setlist),
651 &showlist);
652
653 add_com ("remote <command>", class_obscure, sr_com,
654 "Send a command to the remote monitor.");
655
656}