]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/remote-utils.c
* symtab.c, symtab.h, source.c: Change find_line_pc_range to take
[thirdparty/binutils-gdb.git] / gdb / remote-utils.c
CommitLineData
c6f494e8
RP
1/* Generic support for remote debugging interfaces.
2
3 Copyright 1993 Free Software Foundation, Inc.
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.
40 * put generic load here.
41
42 */
43
44#include <ctype.h>
45
46#include "defs.h"
47#include "gdbcmd.h"
48#include "target.h"
49#include "serial.h"
50#include "gdbcore.h" /* for exec_bfd */
51#include "inferior.h" /* for generic_mourn_inferior */
52#include "remote-utils.h"
53
54struct _sr_settings sr_settings = {
c6f494e8
RP
55 4, /* timeout:
56 remote-hms.c had 2
57 remote-bug.c had "with a timeout of 2, we time out waiting for
58 the prompt after an s-record dump."
59
60 remote.c had (2): This was 5 seconds, which is a long time to
61 sit and wait. Unless this is going though some terminal server
62 or multiplexer or other form of hairy serial connection, I
63 would think 2 seconds would be plenty.
64*/
65
66 10, /* retries */
67 NULL, /* device */
68 NULL, /* descriptor */
69};
70
71struct gr_settings *gr_settings = NULL;
72
73static void
74usage(proto, junk)
75 char *proto;
76 char *junk;
77{
78 if (junk != NULL)
199b2450 79 fprintf_unfiltered(gdb_stderr, "Unrecognized arguments: `%s'.\n", junk);
c6f494e8
RP
80
81 /* FIXME-now: service@host? */
82
83 error("Usage: target %s <device <speed <debug>>>\n\
84or target %s <host> <port>\n", proto, proto);
85
86 return;
87}
88
89#define CHECKDONE(p, q) \
90{ \
91 if (q == p) \
92 { \
93 if (*p == '\0') \
94 return; \
95 else \
96 usage(proto, p); \
97 } \
98}
99
100void
101sr_scan_args(proto, args)
102 char *proto;
103 char *args;
104{
105 int n;
106 char *p, *q;
107
108 extern int strtol();
109
110 /* if no args, then nothing to do. */
111 if (args == NULL || *args == '\0')
112 return;
113
114 /* scan off white space. */
115 for (p = args; isspace(*p); ++p) ;;
116
117 /* find end of device name. */
118 for (q = p; *q != '\0' && !isspace(*q); ++q) ;;
119
120 /* check for missing or empty device name. */
121 CHECKDONE(p, q);
122 sr_set_device(savestring(p, q - p));
123
124 /* look for baud rate. */
125 n = strtol(q, &p, 10);
126
127 /* check for missing or empty baud rate. */
128 CHECKDONE(p, q);
129 sr_set_baud_rate(n);
130
131 /* look for debug value. */
132 n = strtol(p, &q, 10);
133
134 /* check for missing or empty debug value. */
135 CHECKDONE(p, q);
136 sr_set_debug(n);
137
138 /* scan off remaining white space. */
139 for (p = q; isspace(*p); ++p) ;;
140
141 /* if not end of string, then there's unrecognized junk. */
142 if (*p != '\0')
143 usage(proto, p);
144
145 return;
146}
147
148void
149gr_generic_checkin()
150{
151 sr_write_cr("");
152 gr_expect_prompt();
153}
154
155void
156gr_open(args, from_tty, gr)
157 char *args;
158 int from_tty;
159 struct gr_settings *gr;
160{
161 target_preopen(from_tty);
162 sr_scan_args(gr->ops->to_shortname, args);
163 unpush_target(gr->ops);
164
165 gr_settings = gr;
166
167 gr_set_dcache(dcache_init(gr->readfunc, gr->writefunc));
168
169 if (sr_get_desc() != NULL)
170 gr_close (0);
171
172 sr_set_desc(SERIAL_OPEN (sr_get_device()));
173 if (!sr_get_desc())
174 perror_with_name((char *) sr_get_device());
175
176 if (SERIAL_SETBAUDRATE(sr_get_desc(), sr_get_baud_rate()) != 0)
177 {
178 SERIAL_CLOSE(sr_get_desc());
179 perror_with_name(sr_get_device());
180 }
181
182 SERIAL_RAW (sr_get_desc());
183
e15f2a54
JK
184 /* If there is something sitting in the buffer we might take it as a
185 response to a command, which would be bad. */
186 SERIAL_FLUSH_INPUT (sr_get_desc ());
187
c6f494e8
RP
188 /* default retries */
189 if (sr_get_retries() == 0)
190 sr_set_retries(1);
191
192 /* default clear breakpoint function */
193 if (gr_settings->clear_all_breakpoints == NULL)
194 gr_settings->clear_all_breakpoints = remove_breakpoints;
195
196 if (from_tty)
197 printf_filtered ("Remote debugging using `%s' at baud rate of %d\n",
198 sr_get_device(), sr_get_baud_rate());
199
200 push_target(gr->ops);
201 gr_checkin();
202 gr_clear_all_breakpoints ();
203 return;
204}
205
206/* Read a character from the remote system masking it down to 7 bits
207 and doing all the fancy timeout stuff. */
208
209int
210sr_readchar ()
211{
212 int buf;
213
214 buf = SERIAL_READCHAR (sr_get_desc(), sr_get_timeout());
215
216 if (buf == SERIAL_TIMEOUT)
217 error ("Timeout reading from remote system.");
218
219 if (sr_get_debug() > 0)
199b2450 220 printf_unfiltered ("%c", buf);
c6f494e8
RP
221
222 return buf & 0x7f;
223}
224
225int
226sr_pollchar()
227{
228 int buf;
229
230 buf = SERIAL_READCHAR (sr_get_desc(), 0);
231 if (buf == SERIAL_TIMEOUT)
232 buf = 0;
233 if (sr_get_debug() > 0)
234 if (buf)
199b2450 235 printf_unfiltered ("%c", buf);
c6f494e8 236 else
199b2450 237 printf_unfiltered ("<empty character poll>");
c6f494e8
RP
238
239 return buf & 0x7f;
240}
241
242/* Keep discarding input from the remote system, until STRING is found.
243 Let the user break out immediately. */
244void
245sr_expect (string)
246 char *string;
247{
248 char *p = string;
249
250 immediate_quit = 1;
251 while (1)
252 {
253 if (sr_readchar () == *p)
254 {
255 p++;
256 if (*p == '\0')
257 {
258 immediate_quit = 0;
259 return;
260 }
261 }
262 else
263 p = string;
264 }
265}
266
267void
268sr_write (a, l)
269 char *a;
270 int l;
271{
272 int i;
273
274 if (SERIAL_WRITE (sr_get_desc(), a, l) != 0)
275 perror_with_name ("sr_write: Error writing to remote");
276
277 if (sr_get_debug() > 0)
278 for (i = 0; i < l; i++)
199b2450 279 printf_unfiltered ("%c", a[i]);
c6f494e8
RP
280
281 return;
282}
283
284void
285sr_write_cr (s)
286 char *s;
287{
288 sr_write (s, strlen (s));
289 sr_write ("\r", 1);
290 return;
291}
292
293int
294sr_timed_read (buf, n)
295 char *buf;
296 int n;
297{
298 int i;
299 char c;
300
301 i = 0;
302 while (i < n)
303 {
304 c = sr_readchar ();
305
306 if (c == 0)
307 return i;
308 buf[i] = c;
309 i++;
310
311 }
312 return i;
313}
314
315/* Get a hex digit from the remote system & return its value. If
316 ignore_space is nonzero, ignore spaces (not newline, tab, etc). */
317
318int
319sr_get_hex_digit (ignore_space)
320 int ignore_space;
321{
322 int ch;
323
324 while (1)
325 {
326 ch = sr_readchar ();
327 if (ch >= '0' && ch <= '9')
328 return ch - '0';
329 else if (ch >= 'A' && ch <= 'F')
330 return ch - 'A' + 10;
331 else if (ch >= 'a' && ch <= 'f')
332 return ch - 'a' + 10;
333 else if (ch != ' ' || !ignore_space)
334 {
335 gr_expect_prompt ();
336 error ("Invalid hex digit from remote system.");
337 }
338 }
339}
340
341/* Get a byte from the remote and put it in *BYT. Accept any number
342 leading spaces. */
343void
344sr_get_hex_byte (byt)
345 char *byt;
346{
347 int val;
348
349 val = sr_get_hex_digit (1) << 4;
350 val |= sr_get_hex_digit (0);
351 *byt = val;
352}
353
354/* Read a 32-bit hex word from the remote, preceded by a space */
355long
356sr_get_hex_word ()
357{
358 long val;
359 int j;
360
361 val = 0;
362 for (j = 0; j < 8; j++)
363 val = (val << 4) + sr_get_hex_digit (j == 0);
364 return val;
365}
366
367/* Put a command string, in args, out to the remote. The remote is assumed to
368 be in raw mode, all writing/reading done through desc.
369 Ouput from the remote is placed on the users terminal until the
370 prompt from the remote is seen.
371 FIXME: Can't handle commands that take input. */
372
373void
374sr_com (args, fromtty)
375 char *args;
376 int fromtty;
377{
378 sr_check_open ();
379
380 if (!args)
381 return;
382
383 /* Clear all input so only command relative output is displayed */
384
385 sr_write_cr (args);
386 sr_write ("\030", 1);
387 gr_expect_prompt ();
388}
389
390void
391gr_close(quitting)
392 int quitting;
393{
394 gr_clear_all_breakpoints();
395
396 if (sr_is_open())
397 {
398 SERIAL_CLOSE (sr_get_desc());
399 sr_set_desc(NULL);
400 }
401
402 return;
403}
404
405/* gr_detach()
406 takes a program previously attached to and detaches it.
407 We better not have left any breakpoints
408 in the program or it'll die when it hits one.
409 Close the open connection to the remote debugger.
410 Use this when you want to detach and do something else
411 with your gdb. */
412
413void
414gr_detach(args, from_tty)
415 char *args;
416 int from_tty;
417{
418 if (args)
419 error ("Argument given to \"detach\" when remotely debugging.");
420
421 if (sr_is_open())
422 gr_clear_all_breakpoints ();
423
424 pop_target ();
425 if (from_tty)
426 puts_filtered ("Ending remote debugging.\n");
427
428 return;
429}
430
431void
432gr_files_info (ops)
433 struct target_ops *ops;
434{
435 char *file = "nothing";
436
437 if (exec_bfd)
438 file = bfd_get_filename (exec_bfd);
439
440 if (exec_bfd)
441 {
442#ifdef __GO32__
443 printf_filtered ("\tAttached to DOS asynctsr\n");
444#else
445 printf_filtered ("\tAttached to %s at %d baud\n",
446 sr_get_device(), sr_get_baud_rate());
447#endif
448 }
449
450 printf_filtered ("\tand running program %s\n", file);
451 printf_filtered ("\tusing the %s protocol.\n", ops->to_shortname);
452}
453
454void
455gr_mourn ()
456{
457 gr_clear_all_breakpoints ();
458 unpush_target (gr_get_ops());
459 generic_mourn_inferior ();
460}
461
462void
463gr_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. */
470void
471gr_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
504int
505gr_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. */
55fea07b 540 plist = (char **) alloca (string_count * sizeof(*plist));
c6f494e8
RP
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)
199b2450 576 putc_unfiltered (*p, gdb_stdout);
c6f494e8 577
199b2450 578 putc_unfiltered (ch, gdb_stdout);
c6f494e8
RP
579 }
580
581 swallowed_p = swallowed;
582 }
583 }
55fea07b
JK
584#if 0
585 /* Never reached. */
c6f494e8 586 return(-1);
55fea07b 587#endif
c6f494e8
RP
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
596void
597gr_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 */
605int
606gr_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
615void
616gr_store_word (addr, word)
617 CORE_ADDR addr;
618 int word;
619{
620 dcache_poke (gr_get_dcache(), addr, word);
621}
622
623void
624_initialize_sr_support ()
625{
c6f494e8
RP
626/* FIXME-now: if target is open... */
627 add_show_from_set (add_set_cmd ("remotedevice", no_class,
628 var_filename, (char *)&sr_settings.device,
629 "Set device for remote serial I/O.\n\
630This device is used as the serial port when debugging using remote\n\
631targets.", &setlist),
632 &showlist);
633
634 add_com ("remote <command>", class_obscure, sr_com,
635 "Send a command to the remote monitor.");
636
637}