]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/remote-utils.c
Copyright updates for 2007.
[thirdparty/binutils-gdb.git] / gdb / remote-utils.c
CommitLineData
c906108c
SS
1/* Generic support for remote debugging interfaces.
2
6aba47ca 3 Copyright (C) 1993, 1994, 1995, 1996, 1998, 2000, 2001, 2007
b6ba6518 4 Free Software Foundation, Inc.
c906108c 5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
c906108c 12
c5aa993b
JM
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
c906108c 17
c5aa993b
JM
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
197e01b6
EZ
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
c906108c
SS
22
23/* This file actually contains two distinct logical "packages". They
c5aa993b
JM
24 are packaged together in this one file because they are typically
25 used together.
c906108c 26
c5aa993b
JM
27 The first package is an addition to the serial package. The
28 addition provides reading and writing with debugging output and
29 timeouts based on user settable variables. These routines are
30 intended to support serial port based remote backends. These
31 functions are prefixed with sr_.
c906108c 32
c5aa993b
JM
33 The second package is a collection of more or less generic
34 functions for use by remote backends. They support user settable
35 variables for debugging, retries, and the like.
c906108c
SS
36
37 Todo:
38
39 * a pass through mode a la kermit or telnet.
40 * autobaud.
41 * ask remote to change his baud rate.
c5aa993b 42 */
c906108c
SS
43
44#include <ctype.h>
45
46#include "defs.h"
47#include "gdb_string.h"
48#include "gdbcmd.h"
49#include "target.h"
50#include "serial.h"
c5aa993b
JM
51#include "gdbcore.h" /* for exec_bfd */
52#include "inferior.h" /* for generic_mourn_inferior */
c906108c 53#include "remote-utils.h"
4e052eda 54#include "regcache.h"
c906108c
SS
55
56
a14ed312 57void _initialize_sr_support (void);
c906108c 58
c5aa993b
JM
59struct _sr_settings sr_settings =
60{
61 4, /* timeout:
62 remote-hms.c had 2
63 remote-bug.c had "with a timeout of 2, we time out waiting for
64 the prompt after an s-record dump."
65
66 remote.c had (2): This was 5 seconds, which is a long time to
67 sit and wait. Unless this is going though some terminal server
68 or multiplexer or other form of hairy serial connection, I
69 would think 2 seconds would be plenty.
70 */
71
72 10, /* retries */
73 NULL, /* device */
74 NULL, /* descriptor */
c906108c
SS
75};
76
77struct gr_settings *gr_settings = NULL;
78
a14ed312
KB
79static void usage (char *, char *);
80static void sr_com (char *, int);
c906108c
SS
81
82static void
fba45db2 83usage (char *proto, char *junk)
c906108c
SS
84{
85 if (junk != NULL)
c5aa993b 86 fprintf_unfiltered (gdb_stderr, "Unrecognized arguments: `%s'.\n", junk);
c906108c 87
8a3fe4f8
AC
88 error (_("Usage: target %s [DEVICE [SPEED [DEBUG]]]\n\
89where DEVICE is the name of a device or HOST:PORT"), proto);
c906108c
SS
90
91 return;
92}
93
94#define CHECKDONE(p, q) \
95{ \
96 if (q == p) \
97 { \
98 if (*p == '\0') \
99 return; \
100 else \
101 usage(proto, p); \
102 } \
103}
104
105void
fba45db2 106sr_scan_args (char *proto, char *args)
c906108c
SS
107{
108 int n;
109 char *p, *q;
110
111 /* if no args, then nothing to do. */
112 if (args == NULL || *args == '\0')
113 return;
114
115 /* scan off white space. */
c5aa993b 116 for (p = args; isspace (*p); ++p);;
c906108c
SS
117
118 /* find end of device name. */
c5aa993b 119 for (q = p; *q != '\0' && !isspace (*q); ++q);;
c906108c
SS
120
121 /* check for missing or empty device name. */
c5aa993b
JM
122 CHECKDONE (p, q);
123 sr_set_device (savestring (p, q - p));
c906108c
SS
124
125 /* look for baud rate. */
c5aa993b 126 n = strtol (q, &p, 10);
c906108c
SS
127
128 /* check for missing or empty baud rate. */
c5aa993b 129 CHECKDONE (p, q);
c906108c
SS
130 baud_rate = n;
131
132 /* look for debug value. */
c5aa993b 133 n = strtol (p, &q, 10);
c906108c
SS
134
135 /* check for missing or empty debug value. */
c5aa993b
JM
136 CHECKDONE (p, q);
137 sr_set_debug (n);
c906108c
SS
138
139 /* scan off remaining white space. */
c5aa993b 140 for (p = q; isspace (*p); ++p);;
c906108c
SS
141
142 /* if not end of string, then there's unrecognized junk. */
143 if (*p != '\0')
c5aa993b 144 usage (proto, p);
c906108c
SS
145
146 return;
147}
148
149void
fba45db2 150gr_generic_checkin (void)
c906108c 151{
c5aa993b
JM
152 sr_write_cr ("");
153 gr_expect_prompt ();
c906108c
SS
154}
155
156void
fba45db2 157gr_open (char *args, int from_tty, struct gr_settings *gr)
c906108c 158{
c5aa993b
JM
159 target_preopen (from_tty);
160 sr_scan_args (gr->ops->to_shortname, args);
161 unpush_target (gr->ops);
c906108c
SS
162
163 gr_settings = gr;
164
c5aa993b 165 if (sr_get_desc () != NULL)
c906108c
SS
166 gr_close (0);
167
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. */
171
172 if (sr_get_device () == NULL)
173 usage (gr->ops->to_shortname, NULL);
174
2cd58942 175 sr_set_desc (serial_open (sr_get_device ()));
c5aa993b
JM
176 if (!sr_get_desc ())
177 perror_with_name ((char *) sr_get_device ());
c906108c
SS
178
179 if (baud_rate != -1)
180 {
2cd58942 181 if (serial_setbaudrate (sr_get_desc (), baud_rate) != 0)
c906108c 182 {
2cd58942 183 serial_close (sr_get_desc ());
c5aa993b 184 perror_with_name (sr_get_device ());
c906108c
SS
185 }
186 }
187
2cd58942 188 serial_raw (sr_get_desc ());
c906108c
SS
189
190 /* If there is something sitting in the buffer we might take it as a
191 response to a command, which would be bad. */
2cd58942 192 serial_flush_input (sr_get_desc ());
c906108c
SS
193
194 /* default retries */
c5aa993b
JM
195 if (sr_get_retries () == 0)
196 sr_set_retries (1);
c906108c
SS
197
198 /* default clear breakpoint function */
199 if (gr_settings->clear_all_breakpoints == NULL)
200 gr_settings->clear_all_breakpoints = remove_breakpoints;
201
202 if (from_tty)
203 {
204 printf_filtered ("Remote debugging using `%s'", sr_get_device ());
205 if (baud_rate != -1)
206 printf_filtered (" at baud rate of %d",
207 baud_rate);
208 printf_filtered ("\n");
209 }
210
c5aa993b
JM
211 push_target (gr->ops);
212 gr_checkin ();
c906108c
SS
213 gr_clear_all_breakpoints ();
214 return;
215}
216
217/* Read a character from the remote system masking it down to 7 bits
218 and doing all the fancy timeout stuff. */
219
220int
fba45db2 221sr_readchar (void)
c906108c
SS
222{
223 int buf;
224
2cd58942 225 buf = serial_readchar (sr_get_desc (), sr_get_timeout ());
c906108c
SS
226
227 if (buf == SERIAL_TIMEOUT)
8a3fe4f8 228 error (_("Timeout reading from remote system."));
c906108c 229
c5aa993b 230 if (sr_get_debug () > 0)
c906108c
SS
231 printf_unfiltered ("%c", buf);
232
233 return buf & 0x7f;
234}
235
236int
fba45db2 237sr_pollchar (void)
c906108c
SS
238{
239 int buf;
240
2cd58942 241 buf = serial_readchar (sr_get_desc (), 0);
c906108c
SS
242 if (buf == SERIAL_TIMEOUT)
243 buf = 0;
c5aa993b 244 if (sr_get_debug () > 0)
c906108c
SS
245 {
246 if (buf)
c5aa993b 247 printf_unfiltered ("%c", buf);
c906108c 248 else
c5aa993b 249 printf_unfiltered ("<empty character poll>");
c906108c
SS
250 }
251
252 return buf & 0x7f;
253}
254
255/* Keep discarding input from the remote system, until STRING is found.
256 Let the user break out immediately. */
257void
fba45db2 258sr_expect (char *string)
c906108c
SS
259{
260 char *p = string;
261
8edbea78 262 immediate_quit++;
c906108c
SS
263 while (1)
264 {
265 if (sr_readchar () == *p)
266 {
267 p++;
268 if (*p == '\0')
269 {
8edbea78 270 immediate_quit--;
c906108c
SS
271 return;
272 }
273 }
274 else
275 p = string;
276 }
277}
278
279void
fba45db2 280sr_write (char *a, int l)
c906108c
SS
281{
282 int i;
283
2cd58942 284 if (serial_write (sr_get_desc (), a, l) != 0)
e2e0b3e5 285 perror_with_name (_("sr_write: Error writing to remote"));
c906108c 286
c5aa993b 287 if (sr_get_debug () > 0)
c906108c
SS
288 for (i = 0; i < l; i++)
289 printf_unfiltered ("%c", a[i]);
290
291 return;
292}
293
294void
fba45db2 295sr_write_cr (char *s)
c906108c
SS
296{
297 sr_write (s, strlen (s));
298 sr_write ("\r", 1);
299 return;
300}
301
302int
fba45db2 303sr_timed_read (char *buf, int n)
c906108c
SS
304{
305 int i;
306 char c;
307
308 i = 0;
309 while (i < n)
310 {
311 c = sr_readchar ();
312
313 if (c == 0)
314 return i;
315 buf[i] = c;
316 i++;
317
318 }
319 return i;
320}
321
322/* Get a hex digit from the remote system & return its value. If
323 ignore_space is nonzero, ignore spaces (not newline, tab, etc). */
324
325int
fba45db2 326sr_get_hex_digit (int ignore_space)
c906108c
SS
327{
328 int ch;
329
330 while (1)
331 {
332 ch = sr_readchar ();
333 if (ch >= '0' && ch <= '9')
334 return ch - '0';
335 else if (ch >= 'A' && ch <= 'F')
336 return ch - 'A' + 10;
337 else if (ch >= 'a' && ch <= 'f')
338 return ch - 'a' + 10;
339 else if (ch != ' ' || !ignore_space)
340 {
341 gr_expect_prompt ();
8a3fe4f8 342 error (_("Invalid hex digit from remote system."));
c906108c
SS
343 }
344 }
345}
346
347/* Get a byte from the remote and put it in *BYT. Accept any number
348 leading spaces. */
349void
fba45db2 350sr_get_hex_byte (char *byt)
c906108c
SS
351{
352 int val;
353
354 val = sr_get_hex_digit (1) << 4;
355 val |= sr_get_hex_digit (0);
356 *byt = val;
357}
358
359/* Read a 32-bit hex word from the remote, preceded by a space */
360long
fba45db2 361sr_get_hex_word (void)
c906108c
SS
362{
363 long val;
364 int j;
365
366 val = 0;
367 for (j = 0; j < 8; j++)
368 val = (val << 4) + sr_get_hex_digit (j == 0);
369 return val;
370}
371
372/* Put a command string, in args, out to the remote. The remote is assumed to
373 be in raw mode, all writing/reading done through desc.
374 Ouput from the remote is placed on the users terminal until the
375 prompt from the remote is seen.
376 FIXME: Can't handle commands that take input. */
377
378static void
fba45db2 379sr_com (char *args, int fromtty)
c906108c
SS
380{
381 sr_check_open ();
382
383 if (!args)
384 return;
385
386 /* Clear all input so only command relative output is displayed */
387
388 sr_write_cr (args);
389 sr_write ("\030", 1);
390 registers_changed ();
391 gr_expect_prompt ();
392}
393
394void
fba45db2 395gr_close (int quitting)
c906108c 396{
c5aa993b 397 gr_clear_all_breakpoints ();
c906108c 398
c5aa993b 399 if (sr_is_open ())
c906108c 400 {
2cd58942 401 serial_close (sr_get_desc ());
c5aa993b 402 sr_set_desc (NULL);
c906108c
SS
403 }
404
405 return;
406}
407
408/* gr_detach()
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
414 with your gdb. */
415
416void
fba45db2 417gr_detach (char *args, int from_tty)
c906108c
SS
418{
419 if (args)
8a3fe4f8 420 error (_("Argument given to \"detach\" when remotely debugging."));
c5aa993b
JM
421
422 if (sr_is_open ())
c906108c
SS
423 gr_clear_all_breakpoints ();
424
425 pop_target ();
426 if (from_tty)
427 puts_filtered ("Ending remote debugging.\n");
428
429 return;
c5aa993b 430}
c906108c
SS
431
432void
fba45db2 433gr_files_info (struct target_ops *ops)
c906108c
SS
434{
435#ifdef __GO32__
436 printf_filtered ("\tAttached to DOS asynctsr\n");
437#else
c5aa993b 438 printf_filtered ("\tAttached to %s", sr_get_device ());
c906108c
SS
439 if (baud_rate != -1)
440 printf_filtered ("at %d baud", baud_rate);
441 printf_filtered ("\n");
442#endif
443
444 if (exec_bfd)
445 {
446 printf_filtered ("\tand running program %s\n",
447 bfd_get_filename (exec_bfd));
448 }
449 printf_filtered ("\tusing the %s protocol.\n", ops->to_shortname);
450}
451
452void
fba45db2 453gr_mourn (void)
c906108c
SS
454{
455 gr_clear_all_breakpoints ();
c5aa993b 456 unpush_target (gr_get_ops ());
c906108c
SS
457 generic_mourn_inferior ();
458}
459
460void
fba45db2 461gr_kill (void)
c906108c
SS
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. */
468void
fba45db2 469gr_create_inferior (char *execfile, char *args, char **env)
c906108c
SS
470{
471 int entry_pt;
472
473 if (args && *args)
8a3fe4f8 474 error (_("Can't pass arguments to remote process."));
c906108c
SS
475
476 if (execfile == 0 || exec_bfd == 0)
8a3fe4f8 477 error (_("No executable file specified"));
c906108c
SS
478
479 entry_pt = (int) bfd_get_start_address (exec_bfd);
480 sr_check_open ();
481
482 gr_kill ();
483 gr_clear_all_breakpoints ();
484
485 init_wait_for_inferior ();
c5aa993b 486 gr_checkin ();
c906108c
SS
487
488 insert_breakpoints (); /* Needed to get correct instruction in cache */
489 proceed (entry_pt, -1, 0);
490}
491
492/* Given a null terminated list of strings LIST, read the input until we find one of
493 them. Return the index of the string found or -1 on error. '?' means match
494 any single character. Note that with the algorithm we use, the initial
495 character of the string cannot recur in the string, or we will not find some
496 cases of the string in the input. If PASSTHROUGH is non-zero, then
497 pass non-matching data on. */
498
499int
832c69cf 500gr_multi_scan (char *list[], int passthrough)
c906108c 501{
c5aa993b
JM
502 char *swallowed = NULL; /* holding area */
503 char *swallowed_p = swallowed; /* Current position in swallowed. */
c906108c
SS
504 int ch;
505 int ch_handled;
506 int i;
507 int string_count;
508 int max_length;
509 char **plist;
510
511 /* Look through the strings. Count them. Find the largest one so we can
512 allocate a holding area. */
513
514 for (max_length = string_count = i = 0;
515 list[i] != NULL;
516 ++i, ++string_count)
517 {
c5aa993b 518 int length = strlen (list[i]);
c906108c
SS
519
520 if (length > max_length)
521 max_length = length;
522 }
523
524 /* if we have no strings, then something is wrong. */
525 if (string_count == 0)
c5aa993b 526 return (-1);
c906108c
SS
527
528 /* otherwise, we will need a holding area big enough to hold almost two
529 copies of our largest string. */
c5aa993b 530 swallowed_p = swallowed = alloca (max_length << 1);
c906108c
SS
531
532 /* and a list of pointers to current scan points. */
c5aa993b 533 plist = (char **) alloca (string_count * sizeof (*plist));
c906108c
SS
534
535 /* and initialize */
536 for (i = 0; i < string_count; ++i)
537 plist[i] = list[i];
538
c5aa993b 539 for (ch = sr_readchar (); /* loop forever */ ; ch = sr_readchar ())
c906108c 540 {
c5aa993b 541 QUIT; /* Let user quit and leave process running */
c906108c
SS
542 ch_handled = 0;
543
544 for (i = 0; i < string_count; ++i)
545 {
546 if (ch == *plist[i] || *plist[i] == '?')
547 {
548 ++plist[i];
549 if (*plist[i] == '\0')
c5aa993b 550 return (i);
c906108c
SS
551
552 if (!ch_handled)
553 *swallowed_p++ = ch;
554
555 ch_handled = 1;
556 }
557 else
558 plist[i] = list[i];
559 }
560
561 if (!ch_handled)
562 {
563 char *p;
564
565 /* Print out any characters which have been swallowed. */
566 if (passthrough)
567 {
568 for (p = swallowed; p < swallowed_p; ++p)
569 fputc_unfiltered (*p, gdb_stdout);
570
571 fputc_unfiltered (ch, gdb_stdout);
572 }
573
574 swallowed_p = swallowed;
575 }
576 }
577#if 0
578 /* Never reached. */
c5aa993b 579 return (-1);
c906108c
SS
580#endif
581}
582
583/* Get ready to modify the registers array. On machines which store
584 individual registers, this doesn't need to do anything. On machines
585 which store all the registers in one fell swoop, this makes sure
586 that registers contains all the registers from the program being
587 debugged. */
588
589void
fba45db2 590gr_prepare_to_store (void)
c906108c
SS
591{
592 /* Do nothing, since we assume we can store individual regs */
593}
594
c906108c 595void
fba45db2 596_initialize_sr_support (void)
c906108c 597{
f397e303
AC
598 /* FIXME-now: if target is open... */
599 add_setshow_filename_cmd ("remotedevice", no_class, &sr_settings.device, _("\
600Set device for remote serial I/O."), _("\
601Show device for remote serial I/O."), _("\
602This device is used as the serial port when debugging using remote targets."),
603 NULL,
604 NULL, /* FIXME: i18n: */
605 &setlist, &showlist);
c906108c 606
24ec834b 607 add_com ("remote", class_obscure, sr_com,
1bedd215 608 _("Send a command to the remote monitor."));
c906108c
SS
609
610}