21acc2c9ec1ecdf4b51fda32b4d70ba2d749c798
1 /* Generic support for remote debugging interfaces.
3 Copyright 1993 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.
48 #include "gdbcore.h" /* for exec_bfd */
49 #include "inferior.h" /* for generic_mourn_inferior */
50 #include "remote-utils.h"
52 struct _sr_settings sr_settings
= {
55 remote-bug.c had "with a timeout of 2, we time out waiting for
56 the prompt after an s-record dump."
58 remote.c had (2): This was 5 seconds, which is a long time to
59 sit and wait. Unless this is going though some terminal server
60 or multiplexer or other form of hairy serial connection, I
61 would think 2 seconds would be plenty.
66 NULL
, /* descriptor */
69 struct gr_settings
*gr_settings
= NULL
;
77 fprintf_unfiltered(gdb_stderr
, "Unrecognized arguments: `%s'.\n", junk
);
79 error ("Usage: target %s [DEVICE [SPEED [DEBUG]]]\n\
80 where DEVICE is the name of a device or HOST:PORT", proto
, proto
);
85 #define CHECKDONE(p, q) \
97 sr_scan_args(proto
, args
)
106 /* if no args, then nothing to do. */
107 if (args
== NULL
|| *args
== '\0')
110 /* scan off white space. */
111 for (p
= args
; isspace(*p
); ++p
) ;;
113 /* find end of device name. */
114 for (q
= p
; *q
!= '\0' && !isspace(*q
); ++q
) ;;
116 /* check for missing or empty device name. */
118 sr_set_device(savestring(p
, q
- p
));
120 /* look for baud rate. */
121 n
= strtol(q
, &p
, 10);
123 /* check for missing or empty baud rate. */
127 /* look for debug value. */
128 n
= strtol(p
, &q
, 10);
130 /* check for missing or empty debug value. */
134 /* scan off remaining white space. */
135 for (p
= q
; isspace(*p
); ++p
) ;;
137 /* if not end of string, then there's unrecognized junk. */
152 gr_open(args
, from_tty
, gr
)
155 struct gr_settings
*gr
;
157 target_preopen(from_tty
);
158 sr_scan_args(gr
->ops
->to_shortname
, args
);
159 unpush_target(gr
->ops
);
163 gr_set_dcache(dcache_init(gr
->readfunc
, gr
->writefunc
));
165 if (sr_get_desc() != NULL
)
168 /* If no args are specified, then we use the device specified by a
169 previous command or "set remotedevice". But if there is no
170 device, better stop now, not dump core. */
172 if (sr_get_device () == NULL
)
173 usage (gr
->ops
->to_shortname
, NULL
);
175 sr_set_desc(SERIAL_OPEN (sr_get_device()));
177 perror_with_name((char *) sr_get_device());
179 if (SERIAL_SETBAUDRATE(sr_get_desc(), sr_get_baud_rate()) != 0)
181 SERIAL_CLOSE(sr_get_desc());
182 perror_with_name(sr_get_device());
185 SERIAL_RAW (sr_get_desc());
187 /* If there is something sitting in the buffer we might take it as a
188 response to a command, which would be bad. */
189 SERIAL_FLUSH_INPUT (sr_get_desc ());
191 /* default retries */
192 if (sr_get_retries() == 0)
195 /* default clear breakpoint function */
196 if (gr_settings
->clear_all_breakpoints
== NULL
)
197 gr_settings
->clear_all_breakpoints
= remove_breakpoints
;
200 printf_filtered ("Remote debugging using `%s' at baud rate of %d\n",
201 sr_get_device(), sr_get_baud_rate());
203 push_target(gr
->ops
);
205 gr_clear_all_breakpoints ();
209 /* Read a character from the remote system masking it down to 7 bits
210 and doing all the fancy timeout stuff. */
217 buf
= SERIAL_READCHAR (sr_get_desc(), sr_get_timeout());
219 if (buf
== SERIAL_TIMEOUT
)
220 error ("Timeout reading from remote system.");
222 if (sr_get_debug() > 0)
223 printf_unfiltered ("%c", buf
);
233 buf
= SERIAL_READCHAR (sr_get_desc(), 0);
234 if (buf
== SERIAL_TIMEOUT
)
236 if (sr_get_debug() > 0)
238 printf_unfiltered ("%c", buf
);
240 printf_unfiltered ("<empty character poll>");
245 /* Keep discarding input from the remote system, until STRING is found.
246 Let the user break out immediately. */
256 if (sr_readchar () == *p
)
277 if (SERIAL_WRITE (sr_get_desc(), a
, l
) != 0)
278 perror_with_name ("sr_write: Error writing to remote");
280 if (sr_get_debug() > 0)
281 for (i
= 0; i
< l
; i
++)
282 printf_unfiltered ("%c", a
[i
]);
291 sr_write (s
, strlen (s
));
297 sr_timed_read (buf
, n
)
318 /* Get a hex digit from the remote system & return its value. If
319 ignore_space is nonzero, ignore spaces (not newline, tab, etc). */
322 sr_get_hex_digit (ignore_space
)
330 if (ch
>= '0' && ch
<= '9')
332 else if (ch
>= 'A' && ch
<= 'F')
333 return ch
- 'A' + 10;
334 else if (ch
>= 'a' && ch
<= 'f')
335 return ch
- 'a' + 10;
336 else if (ch
!= ' ' || !ignore_space
)
339 error ("Invalid hex digit from remote system.");
344 /* Get a byte from the remote and put it in *BYT. Accept any number
347 sr_get_hex_byte (byt
)
352 val
= sr_get_hex_digit (1) << 4;
353 val
|= sr_get_hex_digit (0);
357 /* Read a 32-bit hex word from the remote, preceded by a space */
365 for (j
= 0; j
< 8; j
++)
366 val
= (val
<< 4) + sr_get_hex_digit (j
== 0);
370 /* Put a command string, in args, out to the remote. The remote is assumed to
371 be in raw mode, all writing/reading done through desc.
372 Ouput from the remote is placed on the users terminal until the
373 prompt from the remote is seen.
374 FIXME: Can't handle commands that take input. */
377 sr_com (args
, fromtty
)
386 /* Clear all input so only command relative output is displayed */
389 sr_write ("\030", 1);
397 gr_clear_all_breakpoints();
401 SERIAL_CLOSE (sr_get_desc());
409 takes a program previously attached to and detaches it.
410 We better not have left any breakpoints
411 in the program or it'll die when it hits one.
412 Close the open connection to the remote debugger.
413 Use this when you want to detach and do something else
417 gr_detach(args
, from_tty
)
422 error ("Argument given to \"detach\" when remotely debugging.");
425 gr_clear_all_breakpoints ();
429 puts_filtered ("Ending remote debugging.\n");
436 struct target_ops
*ops
;
439 printf_filtered ("\tAttached to DOS asynctsr\n");
441 printf_filtered ("\tAttached to %s at %d baud\n",
442 sr_get_device(), sr_get_baud_rate());
447 printf_filtered ("\tand running program %s\n",
448 bfd_get_filename (exec_bfd
));
450 printf_filtered ("\tusing the %s protocol.\n", ops
->to_shortname
);
456 gr_clear_all_breakpoints ();
457 unpush_target (gr_get_ops());
458 generic_mourn_inferior ();
467 /* This is called not only when we first attach, but also when the
468 user types "run" after having attached. */
470 gr_create_inferior (execfile
, args
, env
)
478 error ("Can't pass arguments to remote process.");
480 if (execfile
== 0 || exec_bfd
== 0)
481 error ("No exec file specified");
483 entry_pt
= (int) bfd_get_start_address (exec_bfd
);
487 gr_clear_all_breakpoints ();
489 init_wait_for_inferior ();
492 insert_breakpoints (); /* Needed to get correct instruction in cache */
493 proceed (entry_pt
, -1, 0);
496 /* Given a null terminated list of strings LIST, read the input until we find one of
497 them. Return the index of the string found or -1 on error. '?' means match
498 any single character. Note that with the algorithm we use, the initial
499 character of the string cannot recur in the string, or we will not find some
500 cases of the string in the input. If PASSTHROUGH is non-zero, then
501 pass non-matching data on. */
504 gr_multi_scan (list
, passthrough
)
508 char *swallowed
= NULL
; /* holding area */
509 char *swallowed_p
= swallowed
; /* Current position in swallowed. */
517 /* Look through the strings. Count them. Find the largest one so we can
518 allocate a holding area. */
520 for (max_length
= string_count
= i
= 0;
524 int length
= strlen(list
[i
]);
526 if (length
> max_length
)
530 /* if we have no strings, then something is wrong. */
531 if (string_count
== 0)
534 /* otherwise, we will need a holding area big enough to hold almost two
535 copies of our largest string. */
536 swallowed_p
= swallowed
= alloca(max_length
<< 1);
538 /* and a list of pointers to current scan points. */
539 plist
= (char **) alloca (string_count
* sizeof(*plist
));
542 for (i
= 0; i
< string_count
; ++i
)
545 for (ch
= sr_readchar(); /* loop forever */ ; ch
= sr_readchar())
547 QUIT
; /* Let user quit and leave process running */
550 for (i
= 0; i
< string_count
; ++i
)
552 if (ch
== *plist
[i
] || *plist
[i
] == '?')
555 if (*plist
[i
] == '\0')
571 /* Print out any characters which have been swallowed. */
574 for (p
= swallowed
; p
< swallowed_p
; ++p
)
575 fputc_unfiltered (*p
, gdb_stdout
);
577 fputc_unfiltered (ch
, gdb_stdout
);
580 swallowed_p
= swallowed
;
589 /* Get ready to modify the registers array. On machines which store
590 individual registers, this doesn't need to do anything. On machines
591 which store all the registers in one fell swoop, this makes sure
592 that registers contains all the registers from the program being
596 gr_prepare_to_store ()
598 /* Do nothing, since we assume we can store individual regs */
601 /* Read a word from remote address ADDR and return it.
602 * This goes through the data cache.
608 return dcache_fetch (gr_get_dcache(), addr
);
611 /* Write a word WORD into remote address ADDR.
612 This goes through the data cache. */
615 gr_store_word (addr
, word
)
619 dcache_poke (gr_get_dcache(), addr
, word
);
622 /* general purpose load a file specified on the command line
623 into target memory. */
626 gr_load_image (args
, fromtty
)
633 struct cleanup
*old_cleanups
;
635 char *buffer
= xmalloc (delta
);
637 abfd
= bfd_openr (args
, (char *) 0);
640 perror_with_name (args
);
642 old_cleanups
= make_cleanup (bfd_close
, abfd
);
646 if (!bfd_check_format (abfd
, bfd_object
))
647 error ("It doesn't seem to be an object file.\n");
649 for (s
= abfd
->sections
; s
&& !quit_flag
; s
= s
->next
)
651 if (bfd_get_section_flags (abfd
, s
) & SEC_LOAD
)
654 printf_filtered ("%s\t: 0x%4x .. 0x%4x ",
655 s
->name
, s
->vma
, s
->vma
+ s
->_raw_size
);
657 for (i
= 0; i
< s
->_raw_size
&& !quit_flag
; i
+= delta
)
659 int sub_delta
= delta
;
660 if (sub_delta
> s
->_raw_size
- i
)
661 sub_delta
= s
->_raw_size
- i
;
663 bfd_get_section_contents (abfd
, s
, buffer
, i
, sub_delta
);
664 target_write_memory (s
->vma
+ i
, buffer
, sub_delta
);
665 printf_filtered ("*");
668 printf_filtered ("\n");
673 write_pc (bfd_get_start_address (abfd
));
675 discard_cleanups (old_cleanups
);
680 _initialize_sr_support ()
682 /* FIXME-now: if target is open... */
683 add_show_from_set (add_set_cmd ("remotedevice", no_class
,
684 var_filename
, (char *)&sr_settings
.device
,
685 "Set device for remote serial I/O.\n\
686 This device is used as the serial port when debugging using remote\n\
687 targets.", &setlist
),
690 add_com ("remote <command>", class_obscure
, sr_com
,
691 "Send a command to the remote monitor.");
This page took 0.042345 seconds and 4 git commands to generate.