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