1 /* Generic support for remote debugging interfaces.
3 Copyright 1993, 1994 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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. */
21 /* This file actually contains two distinct logical "packages". They
22 are packaged together in this one file because they are typically
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_.
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.
37 * a pass through mode a la kermit or telnet.
39 * ask remote to change his baud rate.
49 #include "gdbcore.h" /* for exec_bfd */
50 #include "inferior.h" /* for generic_mourn_inferior */
51 #include "remote-utils.h"
53 struct _sr_settings sr_settings
= {
56 remote-bug.c had "with a timeout of 2, we time out waiting for
57 the prompt after an s-record dump."
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.
67 NULL
, /* descriptor */
70 struct gr_settings
*gr_settings
= NULL
;
78 fprintf_unfiltered(gdb_stderr
, "Unrecognized arguments: `%s'.\n", junk
);
80 error ("Usage: target %s [DEVICE [SPEED [DEBUG]]]\n\
81 where DEVICE is the name of a device or HOST:PORT", proto
, proto
);
86 #define CHECKDONE(p, q) \
98 sr_scan_args(proto
, args
)
105 /* if no args, then nothing to do. */
106 if (args
== NULL
|| *args
== '\0')
109 /* scan off white space. */
110 for (p
= args
; isspace(*p
); ++p
) ;;
112 /* find end of device name. */
113 for (q
= p
; *q
!= '\0' && !isspace(*q
); ++q
) ;;
115 /* check for missing or empty device name. */
117 sr_set_device(savestring(p
, q
- p
));
119 /* look for baud rate. */
120 n
= strtol(q
, &p
, 10);
122 /* check for missing or empty baud rate. */
126 /* look for debug value. */
127 n
= strtol(p
, &q
, 10);
129 /* check for missing or empty debug value. */
133 /* scan off remaining white space. */
134 for (p
= q
; isspace(*p
); ++p
) ;;
136 /* if not end of string, then there's unrecognized junk. */
151 gr_open(args
, from_tty
, gr
)
154 struct gr_settings
*gr
;
156 target_preopen(from_tty
);
157 sr_scan_args(gr
->ops
->to_shortname
, args
);
158 unpush_target(gr
->ops
);
162 gr_set_dcache(dcache_init(gr
->readfunc
, gr
->writefunc
));
164 if (sr_get_desc() != NULL
)
167 /* If no args are specified, then we use the device specified by a
168 previous command or "set remotedevice". But if there is no
169 device, better stop now, not dump core. */
171 if (sr_get_device () == NULL
)
172 usage (gr
->ops
->to_shortname
, NULL
);
174 sr_set_desc(SERIAL_OPEN (sr_get_device()));
176 perror_with_name((char *) sr_get_device());
180 if (SERIAL_SETBAUDRATE(sr_get_desc(), baud_rate
) != 0)
182 SERIAL_CLOSE(sr_get_desc());
183 perror_with_name(sr_get_device());
187 SERIAL_RAW (sr_get_desc());
189 /* If there is something sitting in the buffer we might take it as a
190 response to a command, which would be bad. */
191 SERIAL_FLUSH_INPUT (sr_get_desc ());
193 /* default retries */
194 if (sr_get_retries() == 0)
197 /* default clear breakpoint function */
198 if (gr_settings
->clear_all_breakpoints
== NULL
)
199 gr_settings
->clear_all_breakpoints
= remove_breakpoints
;
203 printf_filtered ("Remote debugging using `%s'", sr_get_device ());
205 printf_filtered (" at baud rate of %d",
207 printf_filtered ("\n");
210 push_target(gr
->ops
);
212 gr_clear_all_breakpoints ();
216 /* Read a character from the remote system masking it down to 7 bits
217 and doing all the fancy timeout stuff. */
224 buf
= SERIAL_READCHAR (sr_get_desc(), sr_get_timeout());
226 if (buf
== SERIAL_TIMEOUT
)
227 error ("Timeout reading from remote system.");
229 if (sr_get_debug() > 0)
230 printf_unfiltered ("%c", buf
);
240 buf
= SERIAL_READCHAR (sr_get_desc(), 0);
241 if (buf
== SERIAL_TIMEOUT
)
243 if (sr_get_debug() > 0)
245 printf_unfiltered ("%c", buf
);
247 printf_unfiltered ("<empty character poll>");
252 /* Keep discarding input from the remote system, until STRING is found.
253 Let the user break out immediately. */
263 if (sr_readchar () == *p
)
284 if (SERIAL_WRITE (sr_get_desc(), a
, l
) != 0)
285 perror_with_name ("sr_write: Error writing to remote");
287 if (sr_get_debug() > 0)
288 for (i
= 0; i
< l
; i
++)
289 printf_unfiltered ("%c", a
[i
]);
298 sr_write (s
, strlen (s
));
304 sr_timed_read (buf
, n
)
325 /* Get a hex digit from the remote system & return its value. If
326 ignore_space is nonzero, ignore spaces (not newline, tab, etc). */
329 sr_get_hex_digit (ignore_space
)
337 if (ch
>= '0' && ch
<= '9')
339 else if (ch
>= 'A' && ch
<= 'F')
340 return ch
- 'A' + 10;
341 else if (ch
>= 'a' && ch
<= 'f')
342 return ch
- 'a' + 10;
343 else if (ch
!= ' ' || !ignore_space
)
346 error ("Invalid hex digit from remote system.");
351 /* Get a byte from the remote and put it in *BYT. Accept any number
354 sr_get_hex_byte (byt
)
359 val
= sr_get_hex_digit (1) << 4;
360 val
|= sr_get_hex_digit (0);
364 /* Read a 32-bit hex word from the remote, preceded by a space */
372 for (j
= 0; j
< 8; j
++)
373 val
= (val
<< 4) + sr_get_hex_digit (j
== 0);
377 /* Put a command string, in args, out to the remote. The remote is assumed to
378 be in raw mode, all writing/reading done through desc.
379 Ouput from the remote is placed on the users terminal until the
380 prompt from the remote is seen.
381 FIXME: Can't handle commands that take input. */
384 sr_com (args
, fromtty
)
393 /* Clear all input so only command relative output is displayed */
396 sr_write ("\030", 1);
397 registers_changed ();
405 gr_clear_all_breakpoints();
409 SERIAL_CLOSE (sr_get_desc());
417 takes a program previously attached to and detaches it.
418 We better not have left any breakpoints
419 in the program or it'll die when it hits one.
420 Close the open connection to the remote debugger.
421 Use this when you want to detach and do something else
425 gr_detach(args
, from_tty
)
430 error ("Argument given to \"detach\" when remotely debugging.");
433 gr_clear_all_breakpoints ();
437 puts_filtered ("Ending remote debugging.\n");
444 struct target_ops
*ops
;
447 printf_filtered ("\tAttached to DOS asynctsr\n");
449 printf_filtered ("\tAttached to %s", sr_get_device());
451 printf_filtered ("at %d baud", baud_rate
);
452 printf_filtered ("\n");
457 printf_filtered ("\tand running program %s\n",
458 bfd_get_filename (exec_bfd
));
460 printf_filtered ("\tusing the %s protocol.\n", ops
->to_shortname
);
466 gr_clear_all_breakpoints ();
467 unpush_target (gr_get_ops());
468 generic_mourn_inferior ();
477 /* This is called not only when we first attach, but also when the
478 user types "run" after having attached. */
480 gr_create_inferior (execfile
, args
, env
)
488 error ("Can't pass arguments to remote process.");
490 if (execfile
== 0 || exec_bfd
== 0)
491 error ("No exec file specified");
493 entry_pt
= (int) bfd_get_start_address (exec_bfd
);
497 gr_clear_all_breakpoints ();
499 init_wait_for_inferior ();
502 insert_breakpoints (); /* Needed to get correct instruction in cache */
503 proceed (entry_pt
, -1, 0);
506 /* Given a null terminated list of strings LIST, read the input until we find one of
507 them. Return the index of the string found or -1 on error. '?' means match
508 any single character. Note that with the algorithm we use, the initial
509 character of the string cannot recur in the string, or we will not find some
510 cases of the string in the input. If PASSTHROUGH is non-zero, then
511 pass non-matching data on. */
514 gr_multi_scan (list
, passthrough
)
518 char *swallowed
= NULL
; /* holding area */
519 char *swallowed_p
= swallowed
; /* Current position in swallowed. */
527 /* Look through the strings. Count them. Find the largest one so we can
528 allocate a holding area. */
530 for (max_length
= string_count
= i
= 0;
534 int length
= strlen(list
[i
]);
536 if (length
> max_length
)
540 /* if we have no strings, then something is wrong. */
541 if (string_count
== 0)
544 /* otherwise, we will need a holding area big enough to hold almost two
545 copies of our largest string. */
546 swallowed_p
= swallowed
= alloca(max_length
<< 1);
548 /* and a list of pointers to current scan points. */
549 plist
= (char **) alloca (string_count
* sizeof(*plist
));
552 for (i
= 0; i
< string_count
; ++i
)
555 for (ch
= sr_readchar(); /* loop forever */ ; ch
= sr_readchar())
557 QUIT
; /* Let user quit and leave process running */
560 for (i
= 0; i
< string_count
; ++i
)
562 if (ch
== *plist
[i
] || *plist
[i
] == '?')
565 if (*plist
[i
] == '\0')
581 /* Print out any characters which have been swallowed. */
584 for (p
= swallowed
; p
< swallowed_p
; ++p
)
585 fputc_unfiltered (*p
, gdb_stdout
);
587 fputc_unfiltered (ch
, gdb_stdout
);
590 swallowed_p
= swallowed
;
599 /* Get ready to modify the registers array. On machines which store
600 individual registers, this doesn't need to do anything. On machines
601 which store all the registers in one fell swoop, this makes sure
602 that registers contains all the registers from the program being
606 gr_prepare_to_store ()
608 /* Do nothing, since we assume we can store individual regs */
611 /* Read a word from remote address ADDR and return it.
612 * This goes through the data cache.
618 return dcache_fetch (gr_get_dcache(), addr
);
621 /* Write a word WORD into remote address ADDR.
622 This goes through the data cache. */
625 gr_store_word (addr
, word
)
629 dcache_poke (gr_get_dcache(), addr
, word
);
632 /* general purpose load a file specified on the command line
633 into target memory. */
636 gr_load_image (args
, fromtty
)
643 struct cleanup
*old_cleanups
;
645 char *buffer
= xmalloc (delta
);
647 abfd
= bfd_openr (args
, (char *) 0);
650 /* FIXME: should be using bfd_errmsg, not assuming it was
651 bfd_error_system_call. */
652 perror_with_name (args
);
654 /* FIXME: should be checking for errors from bfd_close (for one thing,
655 on error it does not free all the storage associated with the
657 old_cleanups
= make_cleanup (bfd_close
, abfd
);
661 if (!bfd_check_format (abfd
, bfd_object
))
662 error ("It doesn't seem to be an object file.\n");
664 for (s
= abfd
->sections
; s
&& !quit_flag
; s
= s
->next
)
666 if (bfd_get_section_flags (abfd
, s
) & SEC_LOAD
)
669 printf_filtered ("%s\t: 0x%4x .. 0x%4x ",
670 s
->name
, s
->vma
, s
->vma
+ s
->_raw_size
);
672 for (i
= 0; i
< s
->_raw_size
&& !quit_flag
; i
+= delta
)
674 int sub_delta
= delta
;
675 if (sub_delta
> s
->_raw_size
- i
)
676 sub_delta
= s
->_raw_size
- i
;
678 bfd_get_section_contents (abfd
, s
, buffer
, i
, sub_delta
);
679 target_write_memory (s
->vma
+ i
, buffer
, sub_delta
);
680 printf_filtered ("*");
683 printf_filtered ("\n");
688 write_pc (bfd_get_start_address (abfd
));
689 if (!bfd_close (abfd
))
690 warning ("cannot close \"%s\": %s",
691 args
, bfd_errmsg (bfd_get_error ()));
692 discard_cleanups (old_cleanups
);
697 _initialize_sr_support ()
699 /* FIXME-now: if target is open... */
700 add_show_from_set (add_set_cmd ("remotedevice", no_class
,
701 var_filename
, (char *)&sr_settings
.device
,
702 "Set device for remote serial I/O.\n\
703 This device is used as the serial port when debugging using remote\n\
704 targets.", &setlist
),
707 add_com ("remote <command>", class_obscure
, sr_com
,
708 "Send a command to the remote monitor.");
This page took 0.058336 seconds and 4 git commands to generate.