Create new file regcache.h. Update all uses.
[deliverable/binutils-gdb.git] / gdb / remote-utils.c
1 /* Generic support for remote debugging interfaces.
2
3 Copyright 1993, 1994, 1998, 2001 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., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* This file actually contains two distinct logical "packages". They
23 are packaged together in this one file because they are typically
24 used together.
25
26 The first package is an addition to the serial package. The
27 addition provides reading and writing with debugging output and
28 timeouts based on user settable variables. These routines are
29 intended to support serial port based remote backends. These
30 functions are prefixed with sr_.
31
32 The second package is a collection of more or less generic
33 functions for use by remote backends. They support user settable
34 variables for debugging, retries, and the like.
35
36 Todo:
37
38 * a pass through mode a la kermit or telnet.
39 * autobaud.
40 * ask remote to change his baud rate.
41 */
42
43 #include <ctype.h>
44
45 #include "defs.h"
46 #include "gdb_string.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 #include "regcache.h"
54
55
56 void _initialize_sr_support (void);
57
58 struct _sr_settings sr_settings =
59 {
60 4, /* timeout:
61 remote-hms.c had 2
62 remote-bug.c had "with a timeout of 2, we time out waiting for
63 the prompt after an s-record dump."
64
65 remote.c had (2): This was 5 seconds, which is a long time to
66 sit and wait. Unless this is going though some terminal server
67 or multiplexer or other form of hairy serial connection, I
68 would think 2 seconds would be plenty.
69 */
70
71 10, /* retries */
72 NULL, /* device */
73 NULL, /* descriptor */
74 };
75
76 struct gr_settings *gr_settings = NULL;
77
78 static void usage (char *, char *);
79 static void sr_com (char *, int);
80
81 static void
82 usage (char *proto, char *junk)
83 {
84 if (junk != NULL)
85 fprintf_unfiltered (gdb_stderr, "Unrecognized arguments: `%s'.\n", junk);
86
87 error ("Usage: target %s [DEVICE [SPEED [DEBUG]]]\n\
88 where DEVICE is the name of a device or HOST:PORT", proto, proto);
89
90 return;
91 }
92
93 #define CHECKDONE(p, q) \
94 { \
95 if (q == p) \
96 { \
97 if (*p == '\0') \
98 return; \
99 else \
100 usage(proto, p); \
101 } \
102 }
103
104 void
105 sr_scan_args (char *proto, char *args)
106 {
107 int n;
108 char *p, *q;
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 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
148 void
149 gr_generic_checkin (void)
150 {
151 sr_write_cr ("");
152 gr_expect_prompt ();
153 }
154
155 void
156 gr_open (char *args, int from_tty, 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 if (sr_get_desc () != NULL)
165 gr_close (0);
166
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. */
170
171 if (sr_get_device () == NULL)
172 usage (gr->ops->to_shortname, NULL);
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 (baud_rate != -1)
179 {
180 if (SERIAL_SETBAUDRATE (sr_get_desc (), baud_rate) != 0)
181 {
182 SERIAL_CLOSE (sr_get_desc ());
183 perror_with_name (sr_get_device ());
184 }
185 }
186
187 SERIAL_RAW (sr_get_desc ());
188
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 ());
192
193 /* default retries */
194 if (sr_get_retries () == 0)
195 sr_set_retries (1);
196
197 /* default clear breakpoint function */
198 if (gr_settings->clear_all_breakpoints == NULL)
199 gr_settings->clear_all_breakpoints = remove_breakpoints;
200
201 if (from_tty)
202 {
203 printf_filtered ("Remote debugging using `%s'", sr_get_device ());
204 if (baud_rate != -1)
205 printf_filtered (" at baud rate of %d",
206 baud_rate);
207 printf_filtered ("\n");
208 }
209
210 push_target (gr->ops);
211 gr_checkin ();
212 gr_clear_all_breakpoints ();
213 return;
214 }
215
216 /* Read a character from the remote system masking it down to 7 bits
217 and doing all the fancy timeout stuff. */
218
219 int
220 sr_readchar (void)
221 {
222 int buf;
223
224 buf = SERIAL_READCHAR (sr_get_desc (), sr_get_timeout ());
225
226 if (buf == SERIAL_TIMEOUT)
227 error ("Timeout reading from remote system.");
228
229 if (sr_get_debug () > 0)
230 printf_unfiltered ("%c", buf);
231
232 return buf & 0x7f;
233 }
234
235 int
236 sr_pollchar (void)
237 {
238 int buf;
239
240 buf = SERIAL_READCHAR (sr_get_desc (), 0);
241 if (buf == SERIAL_TIMEOUT)
242 buf = 0;
243 if (sr_get_debug () > 0)
244 {
245 if (buf)
246 printf_unfiltered ("%c", buf);
247 else
248 printf_unfiltered ("<empty character poll>");
249 }
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. */
256 void
257 sr_expect (char *string)
258 {
259 char *p = string;
260
261 immediate_quit++;
262 while (1)
263 {
264 if (sr_readchar () == *p)
265 {
266 p++;
267 if (*p == '\0')
268 {
269 immediate_quit--;
270 return;
271 }
272 }
273 else
274 p = string;
275 }
276 }
277
278 void
279 sr_write (char *a, int l)
280 {
281 int i;
282
283 if (SERIAL_WRITE (sr_get_desc (), a, l) != 0)
284 perror_with_name ("sr_write: Error writing to remote");
285
286 if (sr_get_debug () > 0)
287 for (i = 0; i < l; i++)
288 printf_unfiltered ("%c", a[i]);
289
290 return;
291 }
292
293 void
294 sr_write_cr (char *s)
295 {
296 sr_write (s, strlen (s));
297 sr_write ("\r", 1);
298 return;
299 }
300
301 int
302 sr_timed_read (char *buf, int n)
303 {
304 int i;
305 char c;
306
307 i = 0;
308 while (i < n)
309 {
310 c = sr_readchar ();
311
312 if (c == 0)
313 return i;
314 buf[i] = c;
315 i++;
316
317 }
318 return i;
319 }
320
321 /* Get a hex digit from the remote system & return its value. If
322 ignore_space is nonzero, ignore spaces (not newline, tab, etc). */
323
324 int
325 sr_get_hex_digit (int ignore_space)
326 {
327 int ch;
328
329 while (1)
330 {
331 ch = sr_readchar ();
332 if (ch >= '0' && ch <= '9')
333 return ch - '0';
334 else if (ch >= 'A' && ch <= 'F')
335 return ch - 'A' + 10;
336 else if (ch >= 'a' && ch <= 'f')
337 return ch - 'a' + 10;
338 else if (ch != ' ' || !ignore_space)
339 {
340 gr_expect_prompt ();
341 error ("Invalid hex digit from remote system.");
342 }
343 }
344 }
345
346 /* Get a byte from the remote and put it in *BYT. Accept any number
347 leading spaces. */
348 void
349 sr_get_hex_byte (char *byt)
350 {
351 int val;
352
353 val = sr_get_hex_digit (1) << 4;
354 val |= sr_get_hex_digit (0);
355 *byt = val;
356 }
357
358 /* Read a 32-bit hex word from the remote, preceded by a space */
359 long
360 sr_get_hex_word (void)
361 {
362 long val;
363 int j;
364
365 val = 0;
366 for (j = 0; j < 8; j++)
367 val = (val << 4) + sr_get_hex_digit (j == 0);
368 return val;
369 }
370
371 /* Put a command string, in args, out to the remote. The remote is assumed to
372 be in raw mode, all writing/reading done through desc.
373 Ouput from the remote is placed on the users terminal until the
374 prompt from the remote is seen.
375 FIXME: Can't handle commands that take input. */
376
377 static void
378 sr_com (char *args, int fromtty)
379 {
380 sr_check_open ();
381
382 if (!args)
383 return;
384
385 /* Clear all input so only command relative output is displayed */
386
387 sr_write_cr (args);
388 sr_write ("\030", 1);
389 registers_changed ();
390 gr_expect_prompt ();
391 }
392
393 void
394 gr_close (int quitting)
395 {
396 gr_clear_all_breakpoints ();
397
398 if (sr_is_open ())
399 {
400 SERIAL_CLOSE (sr_get_desc ());
401 sr_set_desc (NULL);
402 }
403
404 return;
405 }
406
407 /* gr_detach()
408 takes a program previously attached to and detaches it.
409 We better not have left any breakpoints
410 in the program or it'll die when it hits one.
411 Close the open connection to the remote debugger.
412 Use this when you want to detach and do something else
413 with your gdb. */
414
415 void
416 gr_detach (char *args, 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
431 void
432 gr_files_info (struct target_ops *ops)
433 {
434 #ifdef __GO32__
435 printf_filtered ("\tAttached to DOS asynctsr\n");
436 #else
437 printf_filtered ("\tAttached to %s", sr_get_device ());
438 if (baud_rate != -1)
439 printf_filtered ("at %d baud", baud_rate);
440 printf_filtered ("\n");
441 #endif
442
443 if (exec_bfd)
444 {
445 printf_filtered ("\tand running program %s\n",
446 bfd_get_filename (exec_bfd));
447 }
448 printf_filtered ("\tusing the %s protocol.\n", ops->to_shortname);
449 }
450
451 void
452 gr_mourn (void)
453 {
454 gr_clear_all_breakpoints ();
455 unpush_target (gr_get_ops ());
456 generic_mourn_inferior ();
457 }
458
459 void
460 gr_kill (void)
461 {
462 return;
463 }
464
465 /* This is called not only when we first attach, but also when the
466 user types "run" after having attached. */
467 void
468 gr_create_inferior (char *execfile, char *args, char **env)
469 {
470 int entry_pt;
471
472 if (args && *args)
473 error ("Can't pass arguments to remote process.");
474
475 if (execfile == 0 || exec_bfd == 0)
476 error ("No executable file specified");
477
478 entry_pt = (int) bfd_get_start_address (exec_bfd);
479 sr_check_open ();
480
481 gr_kill ();
482 gr_clear_all_breakpoints ();
483
484 init_wait_for_inferior ();
485 gr_checkin ();
486
487 insert_breakpoints (); /* Needed to get correct instruction in cache */
488 proceed (entry_pt, -1, 0);
489 }
490
491 /* Given a null terminated list of strings LIST, read the input until we find one of
492 them. Return the index of the string found or -1 on error. '?' means match
493 any single character. Note that with the algorithm we use, the initial
494 character of the string cannot recur in the string, or we will not find some
495 cases of the string in the input. If PASSTHROUGH is non-zero, then
496 pass non-matching data on. */
497
498 int
499 gr_multi_scan (char *list[], int passthrough)
500 {
501 char *swallowed = NULL; /* holding area */
502 char *swallowed_p = swallowed; /* Current position in swallowed. */
503 int ch;
504 int ch_handled;
505 int i;
506 int string_count;
507 int max_length;
508 char **plist;
509
510 /* Look through the strings. Count them. Find the largest one so we can
511 allocate a holding area. */
512
513 for (max_length = string_count = i = 0;
514 list[i] != NULL;
515 ++i, ++string_count)
516 {
517 int length = strlen (list[i]);
518
519 if (length > max_length)
520 max_length = length;
521 }
522
523 /* if we have no strings, then something is wrong. */
524 if (string_count == 0)
525 return (-1);
526
527 /* otherwise, we will need a holding area big enough to hold almost two
528 copies of our largest string. */
529 swallowed_p = swallowed = alloca (max_length << 1);
530
531 /* and a list of pointers to current scan points. */
532 plist = (char **) alloca (string_count * sizeof (*plist));
533
534 /* and initialize */
535 for (i = 0; i < string_count; ++i)
536 plist[i] = list[i];
537
538 for (ch = sr_readchar (); /* loop forever */ ; ch = sr_readchar ())
539 {
540 QUIT; /* Let user quit and leave process running */
541 ch_handled = 0;
542
543 for (i = 0; i < string_count; ++i)
544 {
545 if (ch == *plist[i] || *plist[i] == '?')
546 {
547 ++plist[i];
548 if (*plist[i] == '\0')
549 return (i);
550
551 if (!ch_handled)
552 *swallowed_p++ = ch;
553
554 ch_handled = 1;
555 }
556 else
557 plist[i] = list[i];
558 }
559
560 if (!ch_handled)
561 {
562 char *p;
563
564 /* Print out any characters which have been swallowed. */
565 if (passthrough)
566 {
567 for (p = swallowed; p < swallowed_p; ++p)
568 fputc_unfiltered (*p, gdb_stdout);
569
570 fputc_unfiltered (ch, gdb_stdout);
571 }
572
573 swallowed_p = swallowed;
574 }
575 }
576 #if 0
577 /* Never reached. */
578 return (-1);
579 #endif
580 }
581
582 /* Get ready to modify the registers array. On machines which store
583 individual registers, this doesn't need to do anything. On machines
584 which store all the registers in one fell swoop, this makes sure
585 that registers contains all the registers from the program being
586 debugged. */
587
588 void
589 gr_prepare_to_store (void)
590 {
591 /* Do nothing, since we assume we can store individual regs */
592 }
593
594 void
595 _initialize_sr_support (void)
596 {
597 /* FIXME-now: if target is open... */
598 add_show_from_set (add_set_cmd ("remotedevice", no_class,
599 var_filename, (char *) &sr_settings.device,
600 "Set device for remote serial I/O.\n\
601 This device is used as the serial port when debugging using remote\n\
602 targets.", &setlist),
603 &showlist);
604
605 add_com ("remote <command>", class_obscure, sr_com,
606 "Send a command to the remote monitor.");
607
608 }
This page took 0.041833 seconds and 4 git commands to generate.