]> Git Repo - binutils.git/blame - gdb/remote-utils.c
* a29k-tdep.c (pop_frame): Fix a variable name.
[binutils.git] / gdb / remote-utils.c
CommitLineData
c6f494e8
RP
1/* Generic support for remote debugging interfaces.
2
ba47c66a 3 Copyright 1993, 1994 Free Software Foundation, Inc.
c6f494e8
RP
4
5This file is part of GDB.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, 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.
c6f494e8
RP
40 */
41
42#include <ctype.h>
43
44#include "defs.h"
ba47c66a 45#include <string.h>
c6f494e8
RP
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
53struct _sr_settings sr_settings = {
c6f494e8
RP
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
70struct gr_settings *gr_settings = NULL;
71
72static void
73usage(proto, junk)
74 char *proto;
75 char *junk;
76{
77 if (junk != NULL)
199b2450 78 fprintf_unfiltered(gdb_stderr, "Unrecognized arguments: `%s'.\n", junk);
c6f494e8 79
9c41f6a6
JK
80 error ("Usage: target %s [DEVICE [SPEED [DEBUG]]]\n\
81where DEVICE is the name of a device or HOST:PORT", proto, proto);
c6f494e8
RP
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
97void
98sr_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);
c20c1bdf 126 baud_rate = n;
c6f494e8
RP
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
145void
146gr_generic_checkin()
147{
148 sr_write_cr("");
149 gr_expect_prompt();
150}
151
152void
153gr_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
9c41f6a6
JK
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
c6f494e8
RP
176 sr_set_desc(SERIAL_OPEN (sr_get_device()));
177 if (!sr_get_desc())
178 perror_with_name((char *) sr_get_device());
179
c20c1bdf 180 if (baud_rate != -1)
c6f494e8 181 {
c20c1bdf
JK
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 }
c6f494e8
RP
187 }
188
189 SERIAL_RAW (sr_get_desc());
190
e15f2a54
JK
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
c6f494e8
RP
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)
c20c1bdf
JK
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);
a2961423 209 printf_filtered ("\n");
c20c1bdf 210 }
c6f494e8
RP
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
221int
222sr_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)
199b2450 232 printf_unfiltered ("%c", buf);
c6f494e8
RP
233
234 return buf & 0x7f;
235}
236
237int
238sr_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)
199b2450 247 printf_unfiltered ("%c", buf);
c6f494e8 248 else
199b2450 249 printf_unfiltered ("<empty character poll>");
c6f494e8
RP
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. */
256void
257sr_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
279void
280sr_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++)
199b2450 291 printf_unfiltered ("%c", a[i]);
c6f494e8
RP
292
293 return;
294}
295
296void
297sr_write_cr (s)
298 char *s;
299{
300 sr_write (s, strlen (s));
301 sr_write ("\r", 1);
302 return;
303}
304
305int
306sr_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
330int
331sr_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. */
355void
356sr_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 */
367long
368sr_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
385void
386sr_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);
93584146 399 registers_changed ();
c6f494e8
RP
400 gr_expect_prompt ();
401}
402
403void
404gr_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
426void
427gr_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
444void
445gr_files_info (ops)
446 struct target_ops *ops;
447{
c6f494e8 448#ifdef __GO32__
9c41f6a6 449 printf_filtered ("\tAttached to DOS asynctsr\n");
c6f494e8 450#else
c20c1bdf
JK
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");
c6f494e8 455#endif
c6f494e8 456
9c41f6a6
JK
457 if (exec_bfd)
458 {
459 printf_filtered ("\tand running program %s\n",
460 bfd_get_filename (exec_bfd));
461 }
c6f494e8
RP
462 printf_filtered ("\tusing the %s protocol.\n", ops->to_shortname);
463}
464
465void
466gr_mourn ()
467{
468 gr_clear_all_breakpoints ();
469 unpush_target (gr_get_ops());
470 generic_mourn_inferior ();
471}
472
473void
474gr_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. */
481void
482gr_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
515int
516gr_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. */
55fea07b 551 plist = (char **) alloca (string_count * sizeof(*plist));
c6f494e8
RP
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)
ee9feb65 587 fputc_unfiltered (*p, gdb_stdout);
c6f494e8 588
ee9feb65 589 fputc_unfiltered (ch, gdb_stdout);
c6f494e8
RP
590 }
591
592 swallowed_p = swallowed;
593 }
594 }
55fea07b
JK
595#if 0
596 /* Never reached. */
c6f494e8 597 return(-1);
55fea07b 598#endif
c6f494e8
RP
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
607void
608gr_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 */
616int
617gr_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
626void
627gr_store_word (addr, word)
628 CORE_ADDR addr;
629 int word;
630{
631 dcache_poke (gr_get_dcache(), addr, word);
632}
633
bf2429ab
SC
634/* general purpose load a file specified on the command line
635 into target memory. */
636
637void
638gr_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)
9de0904c
JK
652 /* FIXME: should be using bfd_errmsg, not assuming it was
653 bfd_error_system_call. */
bf2429ab
SC
654 perror_with_name (args);
655
9de0904c
JK
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). */
bf2429ab
SC
659 old_cleanups = make_cleanup (bfd_close, abfd);
660
661 QUIT;
bf2429ab
SC
662
663 if (!bfd_check_format (abfd, bfd_object))
664 error ("It doesn't seem to be an object file.\n");
665
08c0d7b8 666 for (s = abfd->sections; s && !quit_flag; s = s->next)
bf2429ab
SC
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);
08c0d7b8
SC
673 fflush (stdout);
674 for (i = 0; i < s->_raw_size && !quit_flag; i += delta)
bf2429ab
SC
675 {
676 int sub_delta = delta;
677 if (sub_delta > s->_raw_size - i)
678 sub_delta = s->_raw_size - i;
08c0d7b8 679 QUIT;
bf2429ab
SC
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 }
08c0d7b8 688
bf2429ab
SC
689 free (buffer);
690 write_pc (bfd_get_start_address (abfd));
9de0904c
JK
691 if (!bfd_close (abfd))
692 warning ("cannot close \"%s\": %s",
693 args, bfd_errmsg (bfd_get_error ()));
bf2429ab
SC
694 discard_cleanups (old_cleanups);
695}
696
697
c6f494e8
RP
698void
699_initialize_sr_support ()
700{
c6f494e8
RP
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\
705This device is used as the serial port when debugging using remote\n\
706targets.", &setlist),
707 &showlist);
708
709 add_com ("remote <command>", class_obscure, sr_com,
710 "Send a command to the remote monitor.");
711
712}
This page took 0.265139 seconds and 4 git commands to generate.