* elf32-hppa.c (CURRENT_STUB_OFFSET, hppa_elf_build_arg_reloc_stub,
[deliverable/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.
c6f494e8
RP
40 */
41
42#include <ctype.h>
43
44#include "defs.h"
45#include "gdbcmd.h"
46#include "target.h"
47#include "serial.h"
48#include "gdbcore.h" /* for exec_bfd */
49#include "inferior.h" /* for generic_mourn_inferior */
50#include "remote-utils.h"
51
52struct _sr_settings sr_settings = {
c6f494e8
RP
53 4, /* timeout:
54 remote-hms.c had 2
55 remote-bug.c had "with a timeout of 2, we time out waiting for
56 the prompt after an s-record dump."
57
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.
62*/
63
64 10, /* retries */
65 NULL, /* device */
66 NULL, /* descriptor */
67};
68
69struct gr_settings *gr_settings = NULL;
70
71static void
72usage(proto, junk)
73 char *proto;
74 char *junk;
75{
76 if (junk != NULL)
199b2450 77 fprintf_unfiltered(gdb_stderr, "Unrecognized arguments: `%s'.\n", junk);
c6f494e8 78
9c41f6a6
JK
79 error ("Usage: target %s [DEVICE [SPEED [DEBUG]]]\n\
80where DEVICE is the name of a device or HOST:PORT", proto, proto);
c6f494e8
RP
81
82 return;
83}
84
85#define CHECKDONE(p, q) \
86{ \
87 if (q == p) \
88 { \
89 if (*p == '\0') \
90 return; \
91 else \
92 usage(proto, p); \
93 } \
94}
95
96void
97sr_scan_args(proto, args)
98 char *proto;
99 char *args;
100{
101 int n;
102 char *p, *q;
103
104 extern int strtol();
105
106 /* if no args, then nothing to do. */
107 if (args == NULL || *args == '\0')
108 return;
109
110 /* scan off white space. */
111 for (p = args; isspace(*p); ++p) ;;
112
113 /* find end of device name. */
114 for (q = p; *q != '\0' && !isspace(*q); ++q) ;;
115
116 /* check for missing or empty device name. */
117 CHECKDONE(p, q);
118 sr_set_device(savestring(p, q - p));
119
120 /* look for baud rate. */
121 n = strtol(q, &p, 10);
122
123 /* check for missing or empty baud rate. */
124 CHECKDONE(p, q);
125 sr_set_baud_rate(n);
126
127 /* look for debug value. */
128 n = strtol(p, &q, 10);
129
130 /* check for missing or empty debug value. */
131 CHECKDONE(p, q);
132 sr_set_debug(n);
133
134 /* scan off remaining white space. */
135 for (p = q; isspace(*p); ++p) ;;
136
137 /* if not end of string, then there's unrecognized junk. */
138 if (*p != '\0')
139 usage(proto, p);
140
141 return;
142}
143
144void
145gr_generic_checkin()
146{
147 sr_write_cr("");
148 gr_expect_prompt();
149}
150
151void
152gr_open(args, from_tty, gr)
153 char *args;
154 int from_tty;
155 struct gr_settings *gr;
156{
157 target_preopen(from_tty);
158 sr_scan_args(gr->ops->to_shortname, args);
159 unpush_target(gr->ops);
160
161 gr_settings = gr;
162
163 gr_set_dcache(dcache_init(gr->readfunc, gr->writefunc));
164
165 if (sr_get_desc() != NULL)
166 gr_close (0);
167
9c41f6a6
JK
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
c6f494e8
RP
175 sr_set_desc(SERIAL_OPEN (sr_get_device()));
176 if (!sr_get_desc())
177 perror_with_name((char *) sr_get_device());
178
179 if (SERIAL_SETBAUDRATE(sr_get_desc(), sr_get_baud_rate()) != 0)
180 {
181 SERIAL_CLOSE(sr_get_desc());
182 perror_with_name(sr_get_device());
183 }
184
185 SERIAL_RAW (sr_get_desc());
186
e15f2a54
JK
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 ());
190
c6f494e8
RP
191 /* default retries */
192 if (sr_get_retries() == 0)
193 sr_set_retries(1);
194
195 /* default clear breakpoint function */
196 if (gr_settings->clear_all_breakpoints == NULL)
197 gr_settings->clear_all_breakpoints = remove_breakpoints;
198
199 if (from_tty)
200 printf_filtered ("Remote debugging using `%s' at baud rate of %d\n",
201 sr_get_device(), sr_get_baud_rate());
202
203 push_target(gr->ops);
204 gr_checkin();
205 gr_clear_all_breakpoints ();
206 return;
207}
208
209/* Read a character from the remote system masking it down to 7 bits
210 and doing all the fancy timeout stuff. */
211
212int
213sr_readchar ()
214{
215 int buf;
216
217 buf = SERIAL_READCHAR (sr_get_desc(), sr_get_timeout());
218
219 if (buf == SERIAL_TIMEOUT)
220 error ("Timeout reading from remote system.");
221
222 if (sr_get_debug() > 0)
199b2450 223 printf_unfiltered ("%c", buf);
c6f494e8
RP
224
225 return buf & 0x7f;
226}
227
228int
229sr_pollchar()
230{
231 int buf;
232
233 buf = SERIAL_READCHAR (sr_get_desc(), 0);
234 if (buf == SERIAL_TIMEOUT)
235 buf = 0;
236 if (sr_get_debug() > 0)
237 if (buf)
199b2450 238 printf_unfiltered ("%c", buf);
c6f494e8 239 else
199b2450 240 printf_unfiltered ("<empty character poll>");
c6f494e8
RP
241
242 return buf & 0x7f;
243}
244
245/* Keep discarding input from the remote system, until STRING is found.
246 Let the user break out immediately. */
247void
248sr_expect (string)
249 char *string;
250{
251 char *p = string;
252
253 immediate_quit = 1;
254 while (1)
255 {
256 if (sr_readchar () == *p)
257 {
258 p++;
259 if (*p == '\0')
260 {
261 immediate_quit = 0;
262 return;
263 }
264 }
265 else
266 p = string;
267 }
268}
269
270void
271sr_write (a, l)
272 char *a;
273 int l;
274{
275 int i;
276
277 if (SERIAL_WRITE (sr_get_desc(), a, l) != 0)
278 perror_with_name ("sr_write: Error writing to remote");
279
280 if (sr_get_debug() > 0)
281 for (i = 0; i < l; i++)
199b2450 282 printf_unfiltered ("%c", a[i]);
c6f494e8
RP
283
284 return;
285}
286
287void
288sr_write_cr (s)
289 char *s;
290{
291 sr_write (s, strlen (s));
292 sr_write ("\r", 1);
293 return;
294}
295
296int
297sr_timed_read (buf, n)
298 char *buf;
299 int n;
300{
301 int i;
302 char c;
303
304 i = 0;
305 while (i < n)
306 {
307 c = sr_readchar ();
308
309 if (c == 0)
310 return i;
311 buf[i] = c;
312 i++;
313
314 }
315 return i;
316}
317
318/* Get a hex digit from the remote system & return its value. If
319 ignore_space is nonzero, ignore spaces (not newline, tab, etc). */
320
321int
322sr_get_hex_digit (ignore_space)
323 int ignore_space;
324{
325 int ch;
326
327 while (1)
328 {
329 ch = sr_readchar ();
330 if (ch >= '0' && ch <= '9')
331 return ch - '0';
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)
337 {
338 gr_expect_prompt ();
339 error ("Invalid hex digit from remote system.");
340 }
341 }
342}
343
344/* Get a byte from the remote and put it in *BYT. Accept any number
345 leading spaces. */
346void
347sr_get_hex_byte (byt)
348 char *byt;
349{
350 int val;
351
352 val = sr_get_hex_digit (1) << 4;
353 val |= sr_get_hex_digit (0);
354 *byt = val;
355}
356
357/* Read a 32-bit hex word from the remote, preceded by a space */
358long
359sr_get_hex_word ()
360{
361 long val;
362 int j;
363
364 val = 0;
365 for (j = 0; j < 8; j++)
366 val = (val << 4) + sr_get_hex_digit (j == 0);
367 return val;
368}
369
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. */
375
376void
377sr_com (args, fromtty)
378 char *args;
379 int fromtty;
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 gr_expect_prompt ();
391}
392
393void
394gr_close(quitting)
395 int quitting;
396{
397 gr_clear_all_breakpoints();
398
399 if (sr_is_open())
400 {
401 SERIAL_CLOSE (sr_get_desc());
402 sr_set_desc(NULL);
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
417gr_detach(args, from_tty)
418 char *args;
419 int from_tty;
420{
421 if (args)
422 error ("Argument given to \"detach\" when remotely debugging.");
423
424 if (sr_is_open())
425 gr_clear_all_breakpoints ();
426
427 pop_target ();
428 if (from_tty)
429 puts_filtered ("Ending remote debugging.\n");
430
431 return;
432}
433
434void
435gr_files_info (ops)
436 struct target_ops *ops;
437{
c6f494e8 438#ifdef __GO32__
9c41f6a6 439 printf_filtered ("\tAttached to DOS asynctsr\n");
c6f494e8 440#else
9c41f6a6
JK
441 printf_filtered ("\tAttached to %s at %d baud\n",
442 sr_get_device(), sr_get_baud_rate());
c6f494e8 443#endif
c6f494e8 444
9c41f6a6
JK
445 if (exec_bfd)
446 {
447 printf_filtered ("\tand running program %s\n",
448 bfd_get_filename (exec_bfd));
449 }
c6f494e8
RP
450 printf_filtered ("\tusing the %s protocol.\n", ops->to_shortname);
451}
452
453void
454gr_mourn ()
455{
456 gr_clear_all_breakpoints ();
457 unpush_target (gr_get_ops());
458 generic_mourn_inferior ();
459}
460
461void
462gr_kill ()
463{
464 return;
465}
466
467/* This is called not only when we first attach, but also when the
468 user types "run" after having attached. */
469void
470gr_create_inferior (execfile, args, env)
471 char *execfile;
472 char *args;
473 char **env;
474{
475 int entry_pt;
476
477 if (args && *args)
478 error ("Can't pass arguments to remote process.");
479
480 if (execfile == 0 || exec_bfd == 0)
481 error ("No exec file specified");
482
483 entry_pt = (int) bfd_get_start_address (exec_bfd);
484 sr_check_open ();
485
486 gr_kill ();
487 gr_clear_all_breakpoints ();
488
489 init_wait_for_inferior ();
490 gr_checkin();
491
492 insert_breakpoints (); /* Needed to get correct instruction in cache */
493 proceed (entry_pt, -1, 0);
494}
495
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. */
502
503int
504gr_multi_scan (list, passthrough)
505 char *list[];
506 int passthrough;
507{
508 char *swallowed = NULL; /* holding area */
509 char *swallowed_p = swallowed; /* Current position in swallowed. */
510 int ch;
511 int ch_handled;
512 int i;
513 int string_count;
514 int max_length;
515 char **plist;
516
517 /* Look through the strings. Count them. Find the largest one so we can
518 allocate a holding area. */
519
520 for (max_length = string_count = i = 0;
521 list[i] != NULL;
522 ++i, ++string_count)
523 {
524 int length = strlen(list[i]);
525
526 if (length > max_length)
527 max_length = length;
528 }
529
530 /* if we have no strings, then something is wrong. */
531 if (string_count == 0)
532 return(-1);
533
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);
537
538 /* and a list of pointers to current scan points. */
55fea07b 539 plist = (char **) alloca (string_count * sizeof(*plist));
c6f494e8
RP
540
541 /* and initialize */
542 for (i = 0; i < string_count; ++i)
543 plist[i] = list[i];
544
545 for (ch = sr_readchar(); /* loop forever */ ; ch = sr_readchar())
546 {
547 QUIT; /* Let user quit and leave process running */
548 ch_handled = 0;
549
550 for (i = 0; i < string_count; ++i)
551 {
552 if (ch == *plist[i] || *plist[i] == '?')
553 {
554 ++plist[i];
555 if (*plist[i] == '\0')
556 return(i);
557
558 if (!ch_handled)
559 *swallowed_p++ = ch;
560
561 ch_handled = 1;
562 }
563 else
564 plist[i] = list[i];
565 }
566
567 if (!ch_handled)
568 {
569 char *p;
570
571 /* Print out any characters which have been swallowed. */
572 if (passthrough)
573 {
574 for (p = swallowed; p < swallowed_p; ++p)
ee9feb65 575 fputc_unfiltered (*p, gdb_stdout);
c6f494e8 576
ee9feb65 577 fputc_unfiltered (ch, gdb_stdout);
c6f494e8
RP
578 }
579
580 swallowed_p = swallowed;
581 }
582 }
55fea07b
JK
583#if 0
584 /* Never reached. */
c6f494e8 585 return(-1);
55fea07b 586#endif
c6f494e8
RP
587}
588
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
593 debugged. */
594
595void
596gr_prepare_to_store ()
597{
598 /* Do nothing, since we assume we can store individual regs */
599}
600
601/* Read a word from remote address ADDR and return it.
602 * This goes through the data cache.
603 */
604int
605gr_fetch_word (addr)
606 CORE_ADDR addr;
607{
608 return dcache_fetch (gr_get_dcache(), addr);
609}
610
611/* Write a word WORD into remote address ADDR.
612 This goes through the data cache. */
613
614void
615gr_store_word (addr, word)
616 CORE_ADDR addr;
617 int word;
618{
619 dcache_poke (gr_get_dcache(), addr, word);
620}
621
bf2429ab
SC
622/* general purpose load a file specified on the command line
623 into target memory. */
624
625void
626gr_load_image (args, fromtty)
627 char *args;
628 int fromtty;
629{
630 bfd *abfd;
631
632 asection *s;
633 struct cleanup *old_cleanups;
634 int delta = 4096;
635 char *buffer = xmalloc (delta);
636
637 abfd = bfd_openr (args, (char *) 0);
638
639 if (!abfd)
640 perror_with_name (args);
641
642 old_cleanups = make_cleanup (bfd_close, abfd);
643
644 QUIT;
bf2429ab
SC
645
646 if (!bfd_check_format (abfd, bfd_object))
647 error ("It doesn't seem to be an object file.\n");
648
08c0d7b8 649 for (s = abfd->sections; s && !quit_flag; s = s->next)
bf2429ab
SC
650 {
651 if (bfd_get_section_flags (abfd, s) & SEC_LOAD)
652 {
653 int i;
654 printf_filtered ("%s\t: 0x%4x .. 0x%4x ",
655 s->name, s->vma, s->vma + s->_raw_size);
08c0d7b8
SC
656 fflush (stdout);
657 for (i = 0; i < s->_raw_size && !quit_flag; i += delta)
bf2429ab
SC
658 {
659 int sub_delta = delta;
660 if (sub_delta > s->_raw_size - i)
661 sub_delta = s->_raw_size - i;
08c0d7b8 662 QUIT;
bf2429ab
SC
663 bfd_get_section_contents (abfd, s, buffer, i, sub_delta);
664 target_write_memory (s->vma + i, buffer, sub_delta);
665 printf_filtered ("*");
666 fflush (stdout);
667 }
668 printf_filtered ("\n");
669 }
670 }
08c0d7b8 671
bf2429ab
SC
672 free (buffer);
673 write_pc (bfd_get_start_address (abfd));
674 bfd_close (abfd);
675 discard_cleanups (old_cleanups);
676}
677
678
c6f494e8
RP
679void
680_initialize_sr_support ()
681{
c6f494e8
RP
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\
686This device is used as the serial port when debugging using remote\n\
687targets.", &setlist),
688 &showlist);
689
690 add_com ("remote <command>", class_obscure, sr_com,
691 "Send a command to the remote monitor.");
692
693}
This page took 0.162088 seconds and 4 git commands to generate.