* remote.c, remote-mon.c, remote-utils.c, remote-utils.h,
[deliverable/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 print_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 gr_expect_prompt ();
400 }
401
402 void
403 gr_close(quitting)
404 int quitting;
405 {
406 gr_clear_all_breakpoints();
407
408 if (sr_is_open())
409 {
410 SERIAL_CLOSE (sr_get_desc());
411 sr_set_desc(NULL);
412 }
413
414 return;
415 }
416
417 /* gr_detach()
418 takes a program previously attached to and detaches it.
419 We better not have left any breakpoints
420 in the program or it'll die when it hits one.
421 Close the open connection to the remote debugger.
422 Use this when you want to detach and do something else
423 with your gdb. */
424
425 void
426 gr_detach(args, from_tty)
427 char *args;
428 int from_tty;
429 {
430 if (args)
431 error ("Argument given to \"detach\" when remotely debugging.");
432
433 if (sr_is_open())
434 gr_clear_all_breakpoints ();
435
436 pop_target ();
437 if (from_tty)
438 puts_filtered ("Ending remote debugging.\n");
439
440 return;
441 }
442
443 void
444 gr_files_info (ops)
445 struct target_ops *ops;
446 {
447 #ifdef __GO32__
448 printf_filtered ("\tAttached to DOS asynctsr\n");
449 #else
450 printf_filtered ("\tAttached to %s", sr_get_device());
451 if (baud_rate != -1)
452 printf_filtered ("at %d baud", baud_rate);
453 printf_filtered ("\n");
454 #endif
455
456 if (exec_bfd)
457 {
458 printf_filtered ("\tand running program %s\n",
459 bfd_get_filename (exec_bfd));
460 }
461 printf_filtered ("\tusing the %s protocol.\n", ops->to_shortname);
462 }
463
464 void
465 gr_mourn ()
466 {
467 gr_clear_all_breakpoints ();
468 unpush_target (gr_get_ops());
469 generic_mourn_inferior ();
470 }
471
472 void
473 gr_kill ()
474 {
475 return;
476 }
477
478 /* This is called not only when we first attach, but also when the
479 user types "run" after having attached. */
480 void
481 gr_create_inferior (execfile, args, env)
482 char *execfile;
483 char *args;
484 char **env;
485 {
486 int entry_pt;
487
488 if (args && *args)
489 error ("Can't pass arguments to remote process.");
490
491 if (execfile == 0 || exec_bfd == 0)
492 error ("No exec file specified");
493
494 entry_pt = (int) bfd_get_start_address (exec_bfd);
495 sr_check_open ();
496
497 gr_kill ();
498 gr_clear_all_breakpoints ();
499
500 init_wait_for_inferior ();
501 gr_checkin();
502
503 insert_breakpoints (); /* Needed to get correct instruction in cache */
504 proceed (entry_pt, -1, 0);
505 }
506
507 /* Given a null terminated list of strings LIST, read the input until we find one of
508 them. Return the index of the string found or -1 on error. '?' means match
509 any single character. Note that with the algorithm we use, the initial
510 character of the string cannot recur in the string, or we will not find some
511 cases of the string in the input. If PASSTHROUGH is non-zero, then
512 pass non-matching data on. */
513
514 int
515 gr_multi_scan (list, passthrough)
516 char *list[];
517 int passthrough;
518 {
519 char *swallowed = NULL; /* holding area */
520 char *swallowed_p = swallowed; /* Current position in swallowed. */
521 int ch;
522 int ch_handled;
523 int i;
524 int string_count;
525 int max_length;
526 char **plist;
527
528 /* Look through the strings. Count them. Find the largest one so we can
529 allocate a holding area. */
530
531 for (max_length = string_count = i = 0;
532 list[i] != NULL;
533 ++i, ++string_count)
534 {
535 int length = strlen(list[i]);
536
537 if (length > max_length)
538 max_length = length;
539 }
540
541 /* if we have no strings, then something is wrong. */
542 if (string_count == 0)
543 return(-1);
544
545 /* otherwise, we will need a holding area big enough to hold almost two
546 copies of our largest string. */
547 swallowed_p = swallowed = alloca(max_length << 1);
548
549 /* and a list of pointers to current scan points. */
550 plist = (char **) alloca (string_count * sizeof(*plist));
551
552 /* and initialize */
553 for (i = 0; i < string_count; ++i)
554 plist[i] = list[i];
555
556 for (ch = sr_readchar(); /* loop forever */ ; ch = sr_readchar())
557 {
558 QUIT; /* Let user quit and leave process running */
559 ch_handled = 0;
560
561 for (i = 0; i < string_count; ++i)
562 {
563 if (ch == *plist[i] || *plist[i] == '?')
564 {
565 ++plist[i];
566 if (*plist[i] == '\0')
567 return(i);
568
569 if (!ch_handled)
570 *swallowed_p++ = ch;
571
572 ch_handled = 1;
573 }
574 else
575 plist[i] = list[i];
576 }
577
578 if (!ch_handled)
579 {
580 char *p;
581
582 /* Print out any characters which have been swallowed. */
583 if (passthrough)
584 {
585 for (p = swallowed; p < swallowed_p; ++p)
586 fputc_unfiltered (*p, gdb_stdout);
587
588 fputc_unfiltered (ch, gdb_stdout);
589 }
590
591 swallowed_p = swallowed;
592 }
593 }
594 #if 0
595 /* Never reached. */
596 return(-1);
597 #endif
598 }
599
600 /* Get ready to modify the registers array. On machines which store
601 individual registers, this doesn't need to do anything. On machines
602 which store all the registers in one fell swoop, this makes sure
603 that registers contains all the registers from the program being
604 debugged. */
605
606 void
607 gr_prepare_to_store ()
608 {
609 /* Do nothing, since we assume we can store individual regs */
610 }
611
612 /* Read a word from remote address ADDR and return it.
613 * This goes through the data cache.
614 */
615 int
616 gr_fetch_word (addr)
617 CORE_ADDR addr;
618 {
619 return dcache_fetch (gr_get_dcache(), addr);
620 }
621
622 /* Write a word WORD into remote address ADDR.
623 This goes through the data cache. */
624
625 void
626 gr_store_word (addr, word)
627 CORE_ADDR addr;
628 int word;
629 {
630 dcache_poke (gr_get_dcache(), addr, word);
631 }
632
633 /* general purpose load a file specified on the command line
634 into target memory. */
635
636 void
637 gr_load_image (args, fromtty)
638 char *args;
639 int fromtty;
640 {
641 bfd *abfd;
642
643 asection *s;
644 struct cleanup *old_cleanups;
645 int delta = 4096;
646 char *buffer = xmalloc (delta);
647
648 abfd = bfd_openr (args, (char *) 0);
649
650 if (!abfd)
651 perror_with_name (args);
652
653 old_cleanups = make_cleanup (bfd_close, abfd);
654
655 QUIT;
656
657 if (!bfd_check_format (abfd, bfd_object))
658 error ("It doesn't seem to be an object file.\n");
659
660 for (s = abfd->sections; s && !quit_flag; s = s->next)
661 {
662 if (bfd_get_section_flags (abfd, s) & SEC_LOAD)
663 {
664 int i;
665 printf_filtered ("%s\t: 0x%4x .. 0x%4x ",
666 s->name, s->vma, s->vma + s->_raw_size);
667 fflush (stdout);
668 for (i = 0; i < s->_raw_size && !quit_flag; i += delta)
669 {
670 int sub_delta = delta;
671 if (sub_delta > s->_raw_size - i)
672 sub_delta = s->_raw_size - i;
673 QUIT;
674 bfd_get_section_contents (abfd, s, buffer, i, sub_delta);
675 target_write_memory (s->vma + i, buffer, sub_delta);
676 printf_filtered ("*");
677 fflush (stdout);
678 }
679 printf_filtered ("\n");
680 }
681 }
682
683 free (buffer);
684 write_pc (bfd_get_start_address (abfd));
685 bfd_close (abfd);
686 discard_cleanups (old_cleanups);
687 }
688
689
690 void
691 _initialize_sr_support ()
692 {
693 /* FIXME-now: if target is open... */
694 add_show_from_set (add_set_cmd ("remotedevice", no_class,
695 var_filename, (char *)&sr_settings.device,
696 "Set device for remote serial I/O.\n\
697 This device is used as the serial port when debugging using remote\n\
698 targets.", &setlist),
699 &showlist);
700
701 add_com ("remote <command>", class_obscure, sr_com,
702 "Send a command to the remote monitor.");
703
704 }
This page took 0.045777 seconds and 4 git commands to generate.