* README: Remove note about gcc warnings on alpha, these should be
[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 sr_set_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 (SERIAL_SETBAUDRATE(sr_get_desc(), sr_get_baud_rate()) != 0)
181 {
182 SERIAL_CLOSE(sr_get_desc());
183 perror_with_name(sr_get_device());
184 }
185
186 SERIAL_RAW (sr_get_desc());
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 */
193 if (sr_get_retries() == 0)
194 sr_set_retries(1);
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 printf_filtered ("Remote debugging using `%s' at baud rate of %d\n",
202 sr_get_device(), sr_get_baud_rate());
203
204 push_target(gr->ops);
205 gr_checkin();
206 gr_clear_all_breakpoints ();
207 return;
208 }
209
210 /* Read a character from the remote system masking it down to 7 bits
211 and doing all the fancy timeout stuff. */
212
213 int
214 sr_readchar ()
215 {
216 int buf;
217
218 buf = SERIAL_READCHAR (sr_get_desc(), sr_get_timeout());
219
220 if (buf == SERIAL_TIMEOUT)
221 error ("Timeout reading from remote system.");
222
223 if (sr_get_debug() > 0)
224 printf_unfiltered ("%c", buf);
225
226 return buf & 0x7f;
227 }
228
229 int
230 sr_pollchar()
231 {
232 int buf;
233
234 buf = SERIAL_READCHAR (sr_get_desc(), 0);
235 if (buf == SERIAL_TIMEOUT)
236 buf = 0;
237 if (sr_get_debug() > 0)
238 if (buf)
239 printf_unfiltered ("%c", buf);
240 else
241 printf_unfiltered ("<empty character poll>");
242
243 return buf & 0x7f;
244 }
245
246 /* Keep discarding input from the remote system, until STRING is found.
247 Let the user break out immediately. */
248 void
249 sr_expect (string)
250 char *string;
251 {
252 char *p = string;
253
254 immediate_quit = 1;
255 while (1)
256 {
257 if (sr_readchar () == *p)
258 {
259 p++;
260 if (*p == '\0')
261 {
262 immediate_quit = 0;
263 return;
264 }
265 }
266 else
267 p = string;
268 }
269 }
270
271 void
272 sr_write (a, l)
273 char *a;
274 int l;
275 {
276 int i;
277
278 if (SERIAL_WRITE (sr_get_desc(), a, l) != 0)
279 perror_with_name ("sr_write: Error writing to remote");
280
281 if (sr_get_debug() > 0)
282 for (i = 0; i < l; i++)
283 printf_unfiltered ("%c", a[i]);
284
285 return;
286 }
287
288 void
289 sr_write_cr (s)
290 char *s;
291 {
292 sr_write (s, strlen (s));
293 sr_write ("\r", 1);
294 return;
295 }
296
297 int
298 sr_timed_read (buf, n)
299 char *buf;
300 int n;
301 {
302 int i;
303 char c;
304
305 i = 0;
306 while (i < n)
307 {
308 c = sr_readchar ();
309
310 if (c == 0)
311 return i;
312 buf[i] = c;
313 i++;
314
315 }
316 return i;
317 }
318
319 /* Get a hex digit from the remote system & return its value. If
320 ignore_space is nonzero, ignore spaces (not newline, tab, etc). */
321
322 int
323 sr_get_hex_digit (ignore_space)
324 int ignore_space;
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. */
347 void
348 sr_get_hex_byte (byt)
349 char *byt;
350 {
351 int val;
352
353 val = sr_get_hex_digit (1) << 4;
354 val |= sr_get_hex_digit (0);
355 *byt = val;
356 }
357
358 /* Read a 32-bit hex word from the remote, preceded by a space */
359 long
360 sr_get_hex_word ()
361 {
362 long val;
363 int j;
364
365 val = 0;
366 for (j = 0; j < 8; j++)
367 val = (val << 4) + sr_get_hex_digit (j == 0);
368 return val;
369 }
370
371 /* Put a command string, in args, out to the remote. The remote is assumed to
372 be in raw mode, all writing/reading done through desc.
373 Ouput from the remote is placed on the users terminal until the
374 prompt from the remote is seen.
375 FIXME: Can't handle commands that take input. */
376
377 void
378 sr_com (args, fromtty)
379 char *args;
380 int fromtty;
381 {
382 sr_check_open ();
383
384 if (!args)
385 return;
386
387 /* Clear all input so only command relative output is displayed */
388
389 sr_write_cr (args);
390 sr_write ("\030", 1);
391 gr_expect_prompt ();
392 }
393
394 void
395 gr_close(quitting)
396 int quitting;
397 {
398 gr_clear_all_breakpoints();
399
400 if (sr_is_open())
401 {
402 SERIAL_CLOSE (sr_get_desc());
403 sr_set_desc(NULL);
404 }
405
406 return;
407 }
408
409 /* gr_detach()
410 takes a program previously attached to and detaches it.
411 We better not have left any breakpoints
412 in the program or it'll die when it hits one.
413 Close the open connection to the remote debugger.
414 Use this when you want to detach and do something else
415 with your gdb. */
416
417 void
418 gr_detach(args, from_tty)
419 char *args;
420 int from_tty;
421 {
422 if (args)
423 error ("Argument given to \"detach\" when remotely debugging.");
424
425 if (sr_is_open())
426 gr_clear_all_breakpoints ();
427
428 pop_target ();
429 if (from_tty)
430 puts_filtered ("Ending remote debugging.\n");
431
432 return;
433 }
434
435 void
436 gr_files_info (ops)
437 struct target_ops *ops;
438 {
439 #ifdef __GO32__
440 printf_filtered ("\tAttached to DOS asynctsr\n");
441 #else
442 printf_filtered ("\tAttached to %s at %d baud\n",
443 sr_get_device(), sr_get_baud_rate());
444 #endif
445
446 if (exec_bfd)
447 {
448 printf_filtered ("\tand running program %s\n",
449 bfd_get_filename (exec_bfd));
450 }
451 printf_filtered ("\tusing the %s protocol.\n", ops->to_shortname);
452 }
453
454 void
455 gr_mourn ()
456 {
457 gr_clear_all_breakpoints ();
458 unpush_target (gr_get_ops());
459 generic_mourn_inferior ();
460 }
461
462 void
463 gr_kill ()
464 {
465 return;
466 }
467
468 /* This is called not only when we first attach, but also when the
469 user types "run" after having attached. */
470 void
471 gr_create_inferior (execfile, args, env)
472 char *execfile;
473 char *args;
474 char **env;
475 {
476 int entry_pt;
477
478 if (args && *args)
479 error ("Can't pass arguments to remote process.");
480
481 if (execfile == 0 || exec_bfd == 0)
482 error ("No exec file specified");
483
484 entry_pt = (int) bfd_get_start_address (exec_bfd);
485 sr_check_open ();
486
487 gr_kill ();
488 gr_clear_all_breakpoints ();
489
490 init_wait_for_inferior ();
491 gr_checkin();
492
493 insert_breakpoints (); /* Needed to get correct instruction in cache */
494 proceed (entry_pt, -1, 0);
495 }
496
497 /* Given a null terminated list of strings LIST, read the input until we find one of
498 them. Return the index of the string found or -1 on error. '?' means match
499 any single character. Note that with the algorithm we use, the initial
500 character of the string cannot recur in the string, or we will not find some
501 cases of the string in the input. If PASSTHROUGH is non-zero, then
502 pass non-matching data on. */
503
504 int
505 gr_multi_scan (list, passthrough)
506 char *list[];
507 int passthrough;
508 {
509 char *swallowed = NULL; /* holding area */
510 char *swallowed_p = swallowed; /* Current position in swallowed. */
511 int ch;
512 int ch_handled;
513 int i;
514 int string_count;
515 int max_length;
516 char **plist;
517
518 /* Look through the strings. Count them. Find the largest one so we can
519 allocate a holding area. */
520
521 for (max_length = string_count = i = 0;
522 list[i] != NULL;
523 ++i, ++string_count)
524 {
525 int length = strlen(list[i]);
526
527 if (length > max_length)
528 max_length = length;
529 }
530
531 /* if we have no strings, then something is wrong. */
532 if (string_count == 0)
533 return(-1);
534
535 /* otherwise, we will need a holding area big enough to hold almost two
536 copies of our largest string. */
537 swallowed_p = swallowed = alloca(max_length << 1);
538
539 /* and a list of pointers to current scan points. */
540 plist = (char **) alloca (string_count * sizeof(*plist));
541
542 /* and initialize */
543 for (i = 0; i < string_count; ++i)
544 plist[i] = list[i];
545
546 for (ch = sr_readchar(); /* loop forever */ ; ch = sr_readchar())
547 {
548 QUIT; /* Let user quit and leave process running */
549 ch_handled = 0;
550
551 for (i = 0; i < string_count; ++i)
552 {
553 if (ch == *plist[i] || *plist[i] == '?')
554 {
555 ++plist[i];
556 if (*plist[i] == '\0')
557 return(i);
558
559 if (!ch_handled)
560 *swallowed_p++ = ch;
561
562 ch_handled = 1;
563 }
564 else
565 plist[i] = list[i];
566 }
567
568 if (!ch_handled)
569 {
570 char *p;
571
572 /* Print out any characters which have been swallowed. */
573 if (passthrough)
574 {
575 for (p = swallowed; p < swallowed_p; ++p)
576 fputc_unfiltered (*p, gdb_stdout);
577
578 fputc_unfiltered (ch, gdb_stdout);
579 }
580
581 swallowed_p = swallowed;
582 }
583 }
584 #if 0
585 /* Never reached. */
586 return(-1);
587 #endif
588 }
589
590 /* Get ready to modify the registers array. On machines which store
591 individual registers, this doesn't need to do anything. On machines
592 which store all the registers in one fell swoop, this makes sure
593 that registers contains all the registers from the program being
594 debugged. */
595
596 void
597 gr_prepare_to_store ()
598 {
599 /* Do nothing, since we assume we can store individual regs */
600 }
601
602 /* Read a word from remote address ADDR and return it.
603 * This goes through the data cache.
604 */
605 int
606 gr_fetch_word (addr)
607 CORE_ADDR addr;
608 {
609 return dcache_fetch (gr_get_dcache(), addr);
610 }
611
612 /* Write a word WORD into remote address ADDR.
613 This goes through the data cache. */
614
615 void
616 gr_store_word (addr, word)
617 CORE_ADDR addr;
618 int word;
619 {
620 dcache_poke (gr_get_dcache(), addr, word);
621 }
622
623 /* general purpose load a file specified on the command line
624 into target memory. */
625
626 void
627 gr_load_image (args, fromtty)
628 char *args;
629 int fromtty;
630 {
631 bfd *abfd;
632
633 asection *s;
634 struct cleanup *old_cleanups;
635 int delta = 4096;
636 char *buffer = xmalloc (delta);
637
638 abfd = bfd_openr (args, (char *) 0);
639
640 if (!abfd)
641 perror_with_name (args);
642
643 old_cleanups = make_cleanup (bfd_close, abfd);
644
645 QUIT;
646
647 if (!bfd_check_format (abfd, bfd_object))
648 error ("It doesn't seem to be an object file.\n");
649
650 for (s = abfd->sections; s && !quit_flag; s = s->next)
651 {
652 if (bfd_get_section_flags (abfd, s) & SEC_LOAD)
653 {
654 int i;
655 printf_filtered ("%s\t: 0x%4x .. 0x%4x ",
656 s->name, s->vma, s->vma + s->_raw_size);
657 fflush (stdout);
658 for (i = 0; i < s->_raw_size && !quit_flag; i += delta)
659 {
660 int sub_delta = delta;
661 if (sub_delta > s->_raw_size - i)
662 sub_delta = s->_raw_size - i;
663 QUIT;
664 bfd_get_section_contents (abfd, s, buffer, i, sub_delta);
665 target_write_memory (s->vma + i, buffer, sub_delta);
666 printf_filtered ("*");
667 fflush (stdout);
668 }
669 printf_filtered ("\n");
670 }
671 }
672
673 free (buffer);
674 write_pc (bfd_get_start_address (abfd));
675 bfd_close (abfd);
676 discard_cleanups (old_cleanups);
677 }
678
679
680 void
681 _initialize_sr_support ()
682 {
683 /* FIXME-now: if target is open... */
684 add_show_from_set (add_set_cmd ("remotedevice", no_class,
685 var_filename, (char *)&sr_settings.device,
686 "Set device for remote serial I/O.\n\
687 This device is used as the serial port when debugging using remote\n\
688 targets.", &setlist),
689 &showlist);
690
691 add_com ("remote <command>", class_obscure, sr_com,
692 "Send a command to the remote monitor.");
693
694 }