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