collect some remote things into remote-utils
[binutils-gdb.git] / gdb / remote-utils.c
1 /* Generic support for remote debugging interfaces.
2
3 Copyright 1993 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 * 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
54 struct _sr_settings sr_settings = {
55 0, /* debug */
56 9600, /* baud */
57 4, /* timeout:
58 remote-hms.c had 2
59 remote-bug.c had "with a timeout of 2, we time out waiting for
60 the prompt after an s-record dump."
61
62 remote.c had (2): This was 5 seconds, which is a long time to
63 sit and wait. Unless this is going though some terminal server
64 or multiplexer or other form of hairy serial connection, I
65 would think 2 seconds would be plenty.
66 */
67
68 10, /* retries */
69 NULL, /* device */
70 NULL, /* descriptor */
71 };
72
73 struct gr_settings *gr_settings = NULL;
74
75 static void
76 usage(proto, junk)
77 char *proto;
78 char *junk;
79 {
80 if (junk != NULL)
81 fprintf(stderr, "Unrecognized arguments: `%s'.\n", junk);
82
83 /* FIXME-now: service@host? */
84
85 error("Usage: target %s <device <speed <debug>>>\n\
86 or target %s <host> <port>\n", proto, proto);
87
88 return;
89 }
90
91 #define CHECKDONE(p, q) \
92 { \
93 if (q == p) \
94 { \
95 if (*p == '\0') \
96 return; \
97 else \
98 usage(proto, p); \
99 } \
100 }
101
102 void
103 sr_scan_args(proto, args)
104 char *proto;
105 char *args;
106 {
107 int n;
108 char *p, *q;
109
110 extern int strtol();
111
112 /* if no args, then nothing to do. */
113 if (args == NULL || *args == '\0')
114 return;
115
116 /* scan off white space. */
117 for (p = args; isspace(*p); ++p) ;;
118
119 /* find end of device name. */
120 for (q = p; *q != '\0' && !isspace(*q); ++q) ;;
121
122 /* check for missing or empty device name. */
123 CHECKDONE(p, q);
124 sr_set_device(savestring(p, q - p));
125
126 /* look for baud rate. */
127 n = strtol(q, &p, 10);
128
129 /* check for missing or empty baud rate. */
130 CHECKDONE(p, q);
131 sr_set_baud_rate(n);
132
133 /* look for debug value. */
134 n = strtol(p, &q, 10);
135
136 /* check for missing or empty debug value. */
137 CHECKDONE(p, q);
138 sr_set_debug(n);
139
140 /* scan off remaining white space. */
141 for (p = q; isspace(*p); ++p) ;;
142
143 /* if not end of string, then there's unrecognized junk. */
144 if (*p != '\0')
145 usage(proto, p);
146
147 return;
148 }
149
150 void
151 gr_generic_checkin()
152 {
153 sr_write_cr("");
154 gr_expect_prompt();
155 }
156
157 void
158 gr_open(args, from_tty, gr)
159 char *args;
160 int from_tty;
161 struct gr_settings *gr;
162 {
163 target_preopen(from_tty);
164 sr_scan_args(gr->ops->to_shortname, args);
165 unpush_target(gr->ops);
166
167 gr_settings = gr;
168
169 gr_set_dcache(dcache_init(gr->readfunc, gr->writefunc));
170
171 if (sr_get_desc() != NULL)
172 gr_close (0);
173
174 sr_set_desc(SERIAL_OPEN (sr_get_device()));
175 if (!sr_get_desc())
176 perror_with_name((char *) sr_get_device());
177
178 if (SERIAL_SETBAUDRATE(sr_get_desc(), sr_get_baud_rate()) != 0)
179 {
180 SERIAL_CLOSE(sr_get_desc());
181 perror_with_name(sr_get_device());
182 }
183
184 SERIAL_RAW (sr_get_desc());
185
186 /* default retries */
187 if (sr_get_retries() == 0)
188 sr_set_retries(1);
189
190 /* default clear breakpoint function */
191 if (gr_settings->clear_all_breakpoints == NULL)
192 gr_settings->clear_all_breakpoints = remove_breakpoints;
193
194 if (from_tty)
195 printf_filtered ("Remote debugging using `%s' at baud rate of %d\n",
196 sr_get_device(), sr_get_baud_rate());
197
198 push_target(gr->ops);
199 gr_checkin();
200 gr_clear_all_breakpoints ();
201 return;
202 }
203
204 /* Read a character from the remote system masking it down to 7 bits
205 and doing all the fancy timeout stuff. */
206
207 int
208 sr_readchar ()
209 {
210 int buf;
211
212 buf = SERIAL_READCHAR (sr_get_desc(), sr_get_timeout());
213
214 if (buf == SERIAL_TIMEOUT)
215 error ("Timeout reading from remote system.");
216
217 if (sr_get_debug() > 0)
218 printf ("%c", buf);
219
220 return buf & 0x7f;
221 }
222
223 int
224 sr_pollchar()
225 {
226 int buf;
227
228 buf = SERIAL_READCHAR (sr_get_desc(), 0);
229 if (buf == SERIAL_TIMEOUT)
230 buf = 0;
231 if (sr_get_debug() > 0)
232 if (buf)
233 printf ("%c", buf);
234 else
235 printf ("<empty character poll>");
236
237 return buf & 0x7f;
238 }
239
240 /* Keep discarding input from the remote system, until STRING is found.
241 Let the user break out immediately. */
242 void
243 sr_expect (string)
244 char *string;
245 {
246 char *p = string;
247
248 immediate_quit = 1;
249 while (1)
250 {
251 if (sr_readchar () == *p)
252 {
253 p++;
254 if (*p == '\0')
255 {
256 immediate_quit = 0;
257 return;
258 }
259 }
260 else
261 p = string;
262 }
263 }
264
265 void
266 sr_write (a, l)
267 char *a;
268 int l;
269 {
270 int i;
271
272 if (SERIAL_WRITE (sr_get_desc(), a, l) != 0)
273 perror_with_name ("sr_write: Error writing to remote");
274
275 if (sr_get_debug() > 0)
276 for (i = 0; i < l; i++)
277 printf ("%c", a[i]);
278
279 return;
280 }
281
282 void
283 sr_write_cr (s)
284 char *s;
285 {
286 sr_write (s, strlen (s));
287 sr_write ("\r", 1);
288 return;
289 }
290
291 int
292 sr_timed_read (buf, n)
293 char *buf;
294 int n;
295 {
296 int i;
297 char c;
298
299 i = 0;
300 while (i < n)
301 {
302 c = sr_readchar ();
303
304 if (c == 0)
305 return i;
306 buf[i] = c;
307 i++;
308
309 }
310 return i;
311 }
312
313 /* Get a hex digit from the remote system & return its value. If
314 ignore_space is nonzero, ignore spaces (not newline, tab, etc). */
315
316 int
317 sr_get_hex_digit (ignore_space)
318 int ignore_space;
319 {
320 int ch;
321
322 while (1)
323 {
324 ch = sr_readchar ();
325 if (ch >= '0' && ch <= '9')
326 return ch - '0';
327 else if (ch >= 'A' && ch <= 'F')
328 return ch - 'A' + 10;
329 else if (ch >= 'a' && ch <= 'f')
330 return ch - 'a' + 10;
331 else if (ch != ' ' || !ignore_space)
332 {
333 gr_expect_prompt ();
334 error ("Invalid hex digit from remote system.");
335 }
336 }
337 }
338
339 /* Get a byte from the remote and put it in *BYT. Accept any number
340 leading spaces. */
341 void
342 sr_get_hex_byte (byt)
343 char *byt;
344 {
345 int val;
346
347 val = sr_get_hex_digit (1) << 4;
348 val |= sr_get_hex_digit (0);
349 *byt = val;
350 }
351
352 /* Read a 32-bit hex word from the remote, preceded by a space */
353 long
354 sr_get_hex_word ()
355 {
356 long val;
357 int j;
358
359 val = 0;
360 for (j = 0; j < 8; j++)
361 val = (val << 4) + sr_get_hex_digit (j == 0);
362 return val;
363 }
364
365 /* Put a command string, in args, out to the remote. The remote is assumed to
366 be in raw mode, all writing/reading done through desc.
367 Ouput from the remote is placed on the users terminal until the
368 prompt from the remote is seen.
369 FIXME: Can't handle commands that take input. */
370
371 void
372 sr_com (args, fromtty)
373 char *args;
374 int fromtty;
375 {
376 sr_check_open ();
377
378 if (!args)
379 return;
380
381 /* Clear all input so only command relative output is displayed */
382
383 sr_write_cr (args);
384 sr_write ("\030", 1);
385 gr_expect_prompt ();
386 }
387
388 void
389 gr_close(quitting)
390 int quitting;
391 {
392 gr_clear_all_breakpoints();
393
394 if (sr_is_open())
395 {
396 SERIAL_CLOSE (sr_get_desc());
397 sr_set_desc(NULL);
398 }
399
400 return;
401 }
402
403 /* gr_detach()
404 takes a program previously attached to and detaches it.
405 We better not have left any breakpoints
406 in the program or it'll die when it hits one.
407 Close the open connection to the remote debugger.
408 Use this when you want to detach and do something else
409 with your gdb. */
410
411 void
412 gr_detach(args, from_tty)
413 char *args;
414 int from_tty;
415 {
416 if (args)
417 error ("Argument given to \"detach\" when remotely debugging.");
418
419 if (sr_is_open())
420 gr_clear_all_breakpoints ();
421
422 pop_target ();
423 if (from_tty)
424 puts_filtered ("Ending remote debugging.\n");
425
426 return;
427 }
428
429 void
430 gr_files_info (ops)
431 struct target_ops *ops;
432 {
433 char *file = "nothing";
434
435 if (exec_bfd)
436 file = bfd_get_filename (exec_bfd);
437
438 if (exec_bfd)
439 {
440 #ifdef __GO32__
441 printf_filtered ("\tAttached to DOS asynctsr\n");
442 #else
443 printf_filtered ("\tAttached to %s at %d baud\n",
444 sr_get_device(), sr_get_baud_rate());
445 #endif
446 }
447
448 printf_filtered ("\tand running program %s\n", file);
449 printf_filtered ("\tusing the %s protocol.\n", ops->to_shortname);
450 }
451
452 void
453 gr_mourn ()
454 {
455 gr_clear_all_breakpoints ();
456 unpush_target (gr_get_ops());
457 generic_mourn_inferior ();
458 }
459
460 void
461 gr_kill ()
462 {
463 return;
464 }
465
466 /* This is called not only when we first attach, but also when the
467 user types "run" after having attached. */
468 void
469 gr_create_inferior (execfile, args, env)
470 char *execfile;
471 char *args;
472 char **env;
473 {
474 int entry_pt;
475
476 if (args && *args)
477 error ("Can't pass arguments to remote process.");
478
479 if (execfile == 0 || exec_bfd == 0)
480 error ("No exec file specified");
481
482 entry_pt = (int) bfd_get_start_address (exec_bfd);
483 sr_check_open ();
484
485 gr_kill ();
486 gr_clear_all_breakpoints ();
487
488 init_wait_for_inferior ();
489 gr_checkin();
490
491 insert_breakpoints (); /* Needed to get correct instruction in cache */
492 proceed (entry_pt, -1, 0);
493 }
494
495 /* Given a null terminated list of strings LIST, read the input until we find one of
496 them. Return the index of the string found or -1 on error. '?' means match
497 any single character. Note that with the algorithm we use, the initial
498 character of the string cannot recur in the string, or we will not find some
499 cases of the string in the input. If PASSTHROUGH is non-zero, then
500 pass non-matching data on. */
501
502 int
503 gr_multi_scan (list, passthrough)
504 char *list[];
505 int passthrough;
506 {
507 char *swallowed = NULL; /* holding area */
508 char *swallowed_p = swallowed; /* Current position in swallowed. */
509 int ch;
510 int ch_handled;
511 int i;
512 int string_count;
513 int max_length;
514 char **plist;
515
516 /* Look through the strings. Count them. Find the largest one so we can
517 allocate a holding area. */
518
519 for (max_length = string_count = i = 0;
520 list[i] != NULL;
521 ++i, ++string_count)
522 {
523 int length = strlen(list[i]);
524
525 if (length > max_length)
526 max_length = length;
527 }
528
529 /* if we have no strings, then something is wrong. */
530 if (string_count == 0)
531 return(-1);
532
533 /* otherwise, we will need a holding area big enough to hold almost two
534 copies of our largest string. */
535 swallowed_p = swallowed = alloca(max_length << 1);
536
537 /* and a list of pointers to current scan points. */
538 plist = alloca(string_count * sizeof(*plist));
539
540 /* and initialize */
541 for (i = 0; i < string_count; ++i)
542 plist[i] = list[i];
543
544 for (ch = sr_readchar(); /* loop forever */ ; ch = sr_readchar())
545 {
546 QUIT; /* Let user quit and leave process running */
547 ch_handled = 0;
548
549 for (i = 0; i < string_count; ++i)
550 {
551 if (ch == *plist[i] || *plist[i] == '?')
552 {
553 ++plist[i];
554 if (*plist[i] == '\0')
555 return(i);
556
557 if (!ch_handled)
558 *swallowed_p++ = ch;
559
560 ch_handled = 1;
561 }
562 else
563 plist[i] = list[i];
564 }
565
566 if (!ch_handled)
567 {
568 char *p;
569
570 /* Print out any characters which have been swallowed. */
571 if (passthrough)
572 {
573 for (p = swallowed; p < swallowed_p; ++p)
574 putc (*p, stdout);
575
576 putc (ch, stdout);
577 }
578
579 swallowed_p = swallowed;
580 }
581 }
582
583 return(-1);
584 }
585
586 /* Get ready to modify the registers array. On machines which store
587 individual registers, this doesn't need to do anything. On machines
588 which store all the registers in one fell swoop, this makes sure
589 that registers contains all the registers from the program being
590 debugged. */
591
592 void
593 gr_prepare_to_store ()
594 {
595 /* Do nothing, since we assume we can store individual regs */
596 }
597
598 /* Read a word from remote address ADDR and return it.
599 * This goes through the data cache.
600 */
601 int
602 gr_fetch_word (addr)
603 CORE_ADDR addr;
604 {
605 return dcache_fetch (gr_get_dcache(), addr);
606 }
607
608 /* Write a word WORD into remote address ADDR.
609 This goes through the data cache. */
610
611 void
612 gr_store_word (addr, word)
613 CORE_ADDR addr;
614 int word;
615 {
616 dcache_poke (gr_get_dcache(), addr, word);
617 }
618
619 void
620 _initialize_sr_support ()
621 {
622 add_show_from_set (add_set_cmd ("remotedebug", no_class,
623 var_zinteger, (char *)&sr_settings.debug,
624 "Set debugging of remote serial I/O.\n\
625 When non-zero, each packet sent or received with the remote target\n\
626 is displayed. Higher numbers produce more debugging.", &setlist),
627 &showlist);
628
629 /* FIXME-now: if target is open when baud changes... */
630 add_show_from_set (add_set_cmd ("remotebaud", no_class,
631 var_zinteger, (char *)&sr_settings.baud_rate,
632 "Set baud rate for remote serial I/O.\n\
633 This value is used to set the speed of the serial port when debugging\n\
634 using remote targets.", &setlist),
635 &showlist);
636
637 /* FIXME-now: if target is open... */
638 add_show_from_set (add_set_cmd ("remotedevice", no_class,
639 var_filename, (char *)&sr_settings.device,
640 "Set device for remote serial I/O.\n\
641 This device is used as the serial port when debugging using remote\n\
642 targets.", &setlist),
643 &showlist);
644
645 add_com ("remote <command>", class_obscure, sr_com,
646 "Send a command to the remote monitor.");
647
648 }