* gdb/testsuite: Initial creation of gdb/testsuite.
[deliverable/binutils-gdb.git] / gdb / remote-monitor.c
CommitLineData
054240ec
RS
1/* Remote debugging interface for MONITOR boot monitor, for GDB.
2 Copyright 1990, 1991, 1992 Free Software Foundation, Inc.
3 Contributed by Cygnus Support. Written by Rob Savoye for Cygnus.
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 was derived from remote-eb.c, which did a similar job, but for
22 an AMD-29K running EBMON. That file was in turn derived from remote.c
23 as mentioned in the following comment (left in for comic relief):
24
25 "This is like remote.c but is for an esoteric situation--
26 having a 29k board in a PC hooked up to a unix machine with
27 a serial line, and running ctty com1 on the PC, through which
28 the unix machine can run ebmon. Not to mention that the PC
29 has PC/NFS, so it can access the same executables that gdb can,
30 over the net in real time."
31
32 In reality, this module talks to a debug monitor called 'MONITOR', which
33 We communicate with MONITOR via either a direct serial line, or a TCP
34 (or possibly TELNET) stream to a terminal multiplexor,
35 which in turn talks to the target board.
36
37 This is based on remote-st2000.c. I left in the above note here for histerical
38 reasons.
39*/
40
41#include "defs.h"
42#include "gdbcore.h"
43#include "target.h"
44#include "wait.h"
45#include <varargs.h>
46#include <signal.h>
47#include <string.h>
48#include <sys/types.h>
49#include "command.h"
50#include "serial.h"
51#include "monitor.h"
52
53#ifdef HAVE_TERMIO
54# define TERMINAL struct termios
55#else
56# define TERMINAL struct sgttyb
57#endif
58
59struct monitor_ops *current_monitor;
60extern struct target_ops rom68k_ops; /* Forward declaration */
61extern struct target_ops mon68_ops; /* Forward declaration */
62extern struct target_ops bug_ops; /* Forward declaration */
63extern struct monitor_ops rom68k_cmds; /* Forward declaration */
64extern struct monitor_ops mon68_cmds; /* Forward declaration */
65extern struct monitor_ops bug_cmds; /* Forward declaration */
66extern struct cmd_list_element *setlist;
67extern struct cmd_list_element *unsetlist;
68struct cmd_list_element *showlist;
69
70static void monitor_close();
71static void monitor_fetch_register();
72static void monitor_store_register();
73static int kiodebug; /* flag set by "set remotedebug" */
74static int hashmark; /* flag set by "set hash" */
75
76#define LOG_FILE "monitor.log"
77#if defined (LOG_FILE)
78FILE *log_file;
79#endif
80
81static int timeout = 24;
82
83/* Descriptor for I/O to remote machine. Initialize it to -1 so that
84 monitor_open knows that we don't have a file open when the program
85 starts. */
86int monitor_desc = -1;
87
88/* Send data to monitor. Works just like printf. */
89
90static void
91printf_monitor(va_alist)
92 va_dcl
93{
94 va_list args;
95 char *pattern;
96 char buf[200];
97 int i;
98
99 va_start(args);
100
101 pattern = va_arg(args, char *);
102
103 vsprintf(buf, pattern, args);
104
105 if (!serial_write(buf, strlen(buf)))
106 fprintf(stderr, "serial_write failed: %s\n", safe_strerror(errno));
107}
108
109/* Read a character from the remote system, doing all the fancy
110 timeout stuff. */
111static int
112readchar(timeout)
113 int timeout;
114{
115 int c;
116
117 c = serial_readchar(timeout);
118
119 if (kiodebug)
120 putchar(c & 0x7f);
121
122#ifdef LOG_FILE
123 if (isascii (c))
124 putc(c & 0x7f, log_file);
125#endif
126
127 if (c >= 0)
128 return c & 0x7f;
129
130 if (c == -2)
131 {
132 if (timeout == 0)
133 return c; /* Polls shouldn't generate timeout errors */
134
135 error("Timeout reading from remote system.");
136 }
137
138 perror_with_name("remote-monitor");
139}
140
141/* Scan input from the remote system, until STRING is found. If DISCARD is
142 non-zero, then discard non-matching input, else print it out.
143 Let the user break out immediately. */
144static void
145expect(string, discard)
146 char *string;
147 int discard;
148{
149 char *p = string;
150 int c;
151
152 if (kiodebug)
153 printf ("Expecting \"%s\"\n", string);
154
155 immediate_quit = 1;
156 while (1)
157 {
158 c = readchar(timeout);
159 if (!isascii (c))
160 continue;
161 if (c == *p++)
162 {
163 if (*p == '\0')
164 {
165 immediate_quit = 0;
166 if (kiodebug)
167 printf ("\nMatched\n");
168 return;
169 }
170 }
171 else
172 {
173 if (!discard)
174 {
175 fwrite(string, 1, (p - 1) - string, stdout);
176 putchar((char)c);
177 fflush(stdout);
178 }
179 p = string;
180 }
181 }
182}
183
184/* Keep discarding input until we see the MONITOR prompt.
185
186 The convention for dealing with the prompt is that you
187 o give your command
188 o *then* wait for the prompt.
189
190 Thus the last thing that a procedure does with the serial line
191 will be an expect_prompt(). Exception: monitor_resume does not
192 wait for the prompt, because the terminal is being handed over
193 to the inferior. However, the next thing which happens after that
194 is a monitor_wait which does wait for the prompt.
195 Note that this includes abnormal exit, e.g. error(). This is
196 necessary to prevent getting into states from which we can't
197 recover. */
198static void
199expect_prompt(discard)
200 int discard;
201{
202#if defined (LOG_FILE)
203 /* This is a convenient place to do this. The idea is to do it often
204 enough that we never lose much data if we terminate abnormally. */
205 fflush(log_file);
206#endif
207 expect (PROMPT, discard);
208}
209
210/* Get a hex digit from the remote system & return its value.
211 If ignore_space is nonzero, ignore spaces (not newline, tab, etc). */
212static int
213get_hex_digit(ignore_space)
214 int ignore_space;
215{
216 int ch;
217 while (1)
218 {
219 ch = readchar(timeout);
220 if (ch >= '0' && ch <= '9')
221 return ch - '0';
222 else if (ch >= 'A' && ch <= 'F')
223 return ch - 'A' + 10;
224 else if (ch >= 'a' && ch <= 'f')
225 return ch - 'a' + 10;
226 else if (ch == ' ' && ignore_space)
227 ;
228 else
229 {
230 expect_prompt(1);
231 error("Invalid hex digit from remote system.");
232 }
233 }
234}
235
236/* Get a byte from monitor and put it in *BYT. Accept any number
237 leading spaces. */
238static void
239get_hex_byte (byt)
240 char *byt;
241{
242 int val;
243
244 val = get_hex_digit (1) << 4;
245 val |= get_hex_digit (0);
246 *byt = val;
247}
248
249/* Get N 32-bit words from remote, each preceded by a space,
250 and put them in registers starting at REGNO. */
251static void
252get_hex_regs (n, regno)
253 int n;
254 int regno;
255{
256 long val;
257 int i;
258
259 for (i = 0; i < n; i++)
260 {
261 int j;
262
263 val = 0;
264 for (j = 0; j < 8; j++)
265 val = (val << 4) + get_hex_digit (j == 0);
266 supply_register (regno++, (char *) &val);
267 }
268}
269
270/* This is called not only when we first attach, but also when the
271 user types "run" after having attached. */
272static void
273monitor_create_inferior (execfile, args, env)
274 char *execfile;
275 char *args;
276 char **env;
277{
278 int entry_pt;
279
280 if (args && *args)
281 error("Can't pass arguments to remote MONITOR process");
282
283 if (execfile == 0 || exec_bfd == 0)
284 error("No exec file specified");
285
286 entry_pt = (int) bfd_get_start_address (exec_bfd);
287
288#ifdef CREATE_INFERIOR_HOOK
289 CREATE_INFERIOR_HOOK (0); /* No process-ID */
290#endif
291#ifdef LOG_FILE
292 fputs ("\nIn Create_inferior()", log_file);
293#endif
294
295/* The "process" (board) is already stopped awaiting our commands, and
296 the program is already downloaded. We just set its PC and go. */
297
298 clear_proceed_status ();
299
300 /* Tell wait_for_inferior that we've started a new process. */
301 init_wait_for_inferior ();
302
303 /* Set up the "saved terminal modes" of the inferior
304 based on what modes we are starting it with. */
305 target_terminal_init ();
306
307 /* Install inferior's terminal modes. */
308 target_terminal_inferior ();
309
310 /* insert_step_breakpoint (); FIXME, do we need this? */
311 proceed ((CORE_ADDR)entry_pt, -1, 0); /* Let 'er rip... */
312}
313
314/* Open a connection to a remote debugger.
315 NAME is the filename used for communication. */
316
317static int baudrate = 9600;
318static char dev_name[100];
319
320static void
321rom68k_open(args, from_tty)
322 char *args;
323 int from_tty;
324{
325 int n;
326 char junk[100];
327 TERMINAL sg;
328
329 target_preopen(from_tty);
330
331 n = sscanf(args, " %s %d %s", dev_name, &baudrate, junk);
332
333 if (n != 2)
334 error("Bad arguments. Usage: target rom68k <device> <speed>\n\
335or target monitor <host> <port>\n");
336
337 monitor_close(0);
338
339 monitor_desc = serial_open(dev_name);
340
341 serial_setbaudrate(baudrate);
342
343 ioctl (monitor_desc, TIOCGETP, &sg);
344 sg.sg_flags = O_CRDELAY | O_NLDELAY | ~(RAW);
345 ioctl (monitor_desc, TIOCSETN, &sg);
346
347 push_target(&rom68k_ops);
348 push_monitor (&rom68k_cmds);
349
350#if defined (LOG_FILE)
351 log_file = fopen (LOG_FILE, "w");
352 if (log_file == NULL)
353 perror_with_name (LOG_FILE);
354#endif
355
356 /* Hello? Are you there? */
357 printf_monitor("\n"); /* CR wakes up monitor */
358
359 expect_prompt(1);
360
361 if (from_tty)
362 printf("Remote %s connected to %s\n", target_shortname,
363 dev_name);
364}
365
366static void
367mon68_open(args, from_tty)
368 char *args;
369 int from_tty;
370{
371 int n;
372 char junk[100];
373
374 target_preopen(from_tty);
375
376 n = sscanf(args, " %s %d %s", dev_name, &baudrate, junk);
377
378 if (n != 2)
379 error("Bad arguments. Usage: target mon68 <device> <speed>\n\
380or target monitor <host> <port>\n");
381
382 monitor_close(0);
383
384 monitor_desc = serial_open(dev_name);
385
386 serial_setbaudrate(baudrate);
387
388 push_target(&mon68_ops);
389 push_monitor (&mon68_cmds);
390
391#if defined (LOG_FILE)
392 log_file = fopen (LOG_FILE, "w");
393 if (log_file == NULL)
394 perror_with_name (LOG_FILE);
395#endif
396
397 /* Hello? Are you there? */
398 printf_monitor("\n"); /* CR wakes up dbug */
399
400 expect_prompt(1);
401
402 if (from_tty)
403 printf("Remote %s connected to %s\n", target_shortname,
404 dev_name);
405}
406
407static void
408bug_open(args, from_tty)
409 char *args;
410 int from_tty;
411{
412 int n;
413 char junk[100];
414
415 target_preopen(from_tty);
416
417 n = sscanf(args, " %s %d %s", dev_name, &baudrate, junk);
418
419 if (n != 2)
420 error("Bad arguments. Usage: target bug <device> <speed>\n\
421or target monitor <host> <port>\n");
422
423 monitor_close(0);
424
425 monitor_desc = serial_open(dev_name);
426
427 serial_setbaudrate(baudrate);
428
429 push_target(&bug_ops);
430 push_monitor (&bug_cmds);
431
432#if defined (LOG_FILE)
433 log_file = fopen (LOG_FILE, "w");
434 if (log_file == NULL)
435 perror_with_name (LOG_FILE);
436#endif
437
438 /* Hello? Are you there? */
439 printf_monitor("\r"); /* CR wakes up dbug */
440
441 expect_prompt(1);
442
443 if (from_tty)
444 printf("Remote %s connected to %s\n", target_shortname,
445 dev_name);
446}
447
448/*
449 * _close -- Close out all files and local state before this target loses control.
450 */
451static void
452monitor_close (quitting)
453 int quitting;
454{
455 serial_close();
456
457#if defined (LOG_FILE)
458 if (log_file) {
459 if (ferror(log_file))
460 fprintf(stderr, "Error writing log file.\n");
461 if (fclose(log_file) != 0)
462 fprintf(stderr, "Error closing log file.\n");
463 }
464#endif
465}
466
467/* Terminate the open connection to the remote debugger.
468 Use this when you want to detach and do something else
469 with your gdb. */
470static void
471monitor_detach (from_tty)
472 int from_tty;
473{
474 pop_target(); /* calls monitor_close to do the real work */
475 if (from_tty)
476 printf ("Ending remote %s debugging\n", target_shortname);
477}
478
479/*
480 * _resume -- Tell the remote machine to resume.
481 */
482static void
483monitor_resume (step, sig)
484 int step, sig;
485{
486#ifdef LOG_FILE
487 fprintf (log_file, "\nIn Resume (step=%d, sig=%d)\n", step, sig);
488#endif
489
490 if (step)
491 {
492 printf_monitor (STEP_CMD);
493 /* wait for the echo. */
494 expect (STEP_CMD, 1);
495 }
496 else
497 {
498 printf_monitor (GO_CMD);
499 /* swallow the echo. */
500 expect (GO_CMD, 1);
501 }
502}
503
504/*
505 * _wait -- Wait until the remote machine stops, then return,
506 * storing status in status just as `wait' would.
507 */
508
509static int
510monitor_wait (status)
511 WAITTYPE *status;
512{
513 int old_timeout = timeout;
514#ifdef LOG_FILE
515 fputs ("\nIn wait ()", log_file);
516#endif
517
518 WSETEXIT ((*status), 0);
519
520 timeout = 0; /* Don't time out -- user program is running. */
521
522 expect_prompt(0); /* Wait for prompt, outputting extraneous text */
523
524 WSETSTOP ((*status), SIGTRAP);
525
526 timeout = old_timeout;
527
528 return 0;
529}
530
531/* Return the name of register number regno in the form input and output by
532 monitor. Currently, register_names just happens to contain exactly what
533 monitor wants. Lets take advantage of that just as long as possible! */
534
535static char *
536get_reg_name (regno)
537 int regno;
538{
539 static char buf[50];
540 const char *p;
541 char *b;
542
543 b = buf;
544
545 if (regno < 0)
546 return ("");
547 for (p = reg_names[regno]; *p; p++)
548 *b++ = toupper(*p);
549 *b = '\000';
550
551 return buf;
552}
553
554/* read the remote registers into the block regs. */
555
556static void
557monitor_fetch_registers ()
558{
559 int regno;
560
561 /* yeah yeah, i know this is horribly inefficient. but it isn't done
562 very often... i'll clean it up later. */
563
564 for (regno = 0; regno <= PC_REGNUM; regno++)
565 monitor_fetch_register(regno);
566}
567
568/* Fetch register REGNO, or all registers if REGNO is -1.
569 Returns errno value. */
570static void
571monitor_fetch_register (regno)
572 int regno;
573{
574 int val, j;
575
576#ifdef LOG_FILE
577 fprintf (log_file, "\nIn Fetch Register (reg=%s)\n", get_reg_name (regno));
578 fflush (log_file);
579#endif
580
581 if (regno < 0)
582 {
583 monitor_fetch_registers ();
584 }
585 else
586 {
587 char *name = get_reg_name (regno);
588 printf_monitor (GET_REG, name);
589 expect (name, 1);
590 expect (REG_DELIM, 1);
591 if (strcasecmp (name, "SR") == 0)
592 {
593 val = 0;
594 for (j = 0; j < 4; j++)
595 val = (val << 4) + get_hex_digit (j == 0);
596 supply_register (regno, (char *) &val);
597 }
598 else
599 {
600 get_hex_regs (1, regno);
601 }
602 if (CMD_END)
603 {
604 expect (CMD_DELIM);
605 printf_monitor (CMD_END);
606 }
607 expect_prompt (1);
608 }
609 return;
610}
611
612/* Store the remote registers from the contents of the block REGS. */
613
614static void
615monitor_store_registers ()
616{
617 int regno;
618
619 for (regno = 0; regno <= PC_REGNUM; regno++)
620 monitor_store_register(regno);
621
622 registers_changed ();
623}
624
625/* Store register REGNO, or all if REGNO == 0.
626 return errno value. */
627static void
628monitor_store_register (regno)
629 int regno;
630{
631#ifdef LOG_FILE
632 fprintf (log_file, "\nIn Store_register (regno=%d)\n", regno);
633#endif
634 if (regno == -1)
635 monitor_store_registers ();
636 else
637 {
638 if (kiodebug)
639 printf ("Setting register %s to 0x%x\n", get_reg_name (regno), read_register (regno));
640
641 printf_monitor (SET_REG, get_reg_name (regno),
642 read_register (regno));
643
644 expect_prompt (1);
645 }
646}
647
648/* Get ready to modify the registers array. On machines which store
649 individual registers, this doesn't need to do anything. On machines
650 which store all the registers in one fell swoop, this makes sure
651 that registers contains all the registers from the program being
652 debugged. */
653
654static void
655monitor_prepare_to_store ()
656{
657 /* Do nothing, since we can store individual regs */
658}
659
660static void
661monitor_files_info ()
662{
663 printf ("\tAttached to %s at %d baud.\n",
664 dev_name, baudrate);
665}
666
667/* Copy LEN bytes of data from debugger memory at MYADDR
668 to inferior's memory at MEMADDR. Returns length moved. */
669static int
670monitor_write_inferior_memory (memaddr, myaddr, len)
671 CORE_ADDR memaddr;
672 unsigned char *myaddr;
673 int len;
674{
675 int i;
676 char buf[10];
677
678#ifdef LOG_FILE
679 fprintf (log_file, "\nIn Write_inferior_memory (memaddr=%x, len=%d)\n", memaddr, len);
680#endif
681 for (i = 0; i < len; i++)
682 {
683 printf_monitor (MEM_SET_CMD, memaddr + i);
684 expect (sprintf (buf, MEM_PROMPT, memaddr + i), 1);
685 expect (CMD_DELIM);
686 printf_monitor ("%x", myaddr[i]);
687 if (kiodebug)
688 printf ("\nSet 0x%x to 0x%x\n", memaddr + i, myaddr[i]);
689 if (CMD_END)
690 {
691/*** expect (sprintf (buf, MEM_PROMPT, memaddr + i +1), 1);
692 expect (CMD_DELIM); ***/
693 printf_monitor (CMD_END);
694 }
695 expect_prompt (1);
696 }
697 return len;
698}
699
700/* Read LEN bytes from inferior memory at MEMADDR. Put the result
701 at debugger address MYADDR. Returns length moved. */
702static int
703monitor_read_inferior_memory(memaddr, myaddr, len)
704 CORE_ADDR memaddr;
705 char *myaddr;
706 int len;
707{
708 int i, j;
709 char buf[20];
710
711 /* Number of bytes read so far. */
712 int count;
713
714 /* Starting address of this pass. */
715 unsigned long startaddr;
716
717 /* Number of bytes to read in this pass. */
718 int len_this_pass;
719
720#ifdef LOG_FILE
721 fprintf (log_file, "\nIn Read_inferior_memory (memaddr=%x, len=%d)\n", memaddr, len);
722#endif
723
724 /* Note that this code works correctly if startaddr is just less
725 than UINT_MAX (well, really CORE_ADDR_MAX if there was such a
726 thing). That is, something like
727 monitor_read_bytes (CORE_ADDR_MAX - 4, foo, 4)
728 works--it never adds len To memaddr and gets 0. */
729 /* However, something like
730 monitor_read_bytes (CORE_ADDR_MAX - 3, foo, 4)
731 doesn't need to work. Detect it and give up if there's an attempt
732 to do that. */
733 if (((memaddr - 1) + len) < memaddr) {
734 errno = EIO;
735 return 0;
736 }
737
738 startaddr = memaddr;
739 count = 0;
740 while (count < len)
741 {
742 len_this_pass = 16;
743 if ((startaddr % 16) != 0)
744 len_this_pass -= startaddr % 16;
745 if (len_this_pass > (len - count))
746 len_this_pass = (len - count);
747 if (kiodebug)
748 printf ("\nDisplay %d bytes at %x\n", len_this_pass, startaddr);
749
750 for (i = 0; i < len_this_pass; i++)
751 {
752 printf_monitor (MEM_DIS_CMD, startaddr);
753 expect (sprintf(buf, MEM_PROMPT, startaddr), 1);
754 get_hex_byte (&myaddr[count++]);
755 if (kiodebug)
756 printf ("\nRead a 0x%x from 0x%x\n", myaddr[count-1], startaddr);
757 if (CMD_END)
758 {
759 expect (CMD_DELIM);
760 printf_monitor (CMD_END);
761 }
762 expect_prompt (1);
763 startaddr += 1;
764 }
765 }
766 return len;
767}
768
769/* FIXME-someday! merge these two. */
770static int
771monitor_xfer_inferior_memory (memaddr, myaddr, len, write, target)
772 CORE_ADDR memaddr;
773 char *myaddr;
774 int len;
775 int write;
776 struct target_ops *target; /* ignored */
777{
778 if (write)
779 return monitor_write_inferior_memory (memaddr, myaddr, len);
780 else
781 return monitor_read_inferior_memory (memaddr, myaddr, len);
782}
783
784static void
785monitor_kill (args, from_tty)
786 char *args;
787 int from_tty;
788{
789 return; /* ignore attempts to kill target system */
790}
791
792/* Clean up when a program exits.
793 The program actually lives on in the remote processor's RAM, and may be
794 run again without a download. Don't leave it full of breakpoint
795 instructions. */
796
797static void
798monitor_mourn_inferior ()
799{
800 remove_breakpoints ();
801 generic_mourn_inferior (); /* Do all the proper things now */
802}
803
804#define MAX_MONITOR_BREAKPOINTS 16
805
806extern int memory_breakpoint_size;
807static CORE_ADDR breakaddr[MAX_MONITOR_BREAKPOINTS] = {0};
808
809static int
810monitor_insert_breakpoint (addr, shadow)
811 CORE_ADDR addr;
812 char *shadow;
813{
814 int i;
815
816#ifdef LOG_FILE
817 fprintf (log_file, "\nIn Insert_breakpoint (addr=%x)\n", addr);
818#endif
819 for (i = 0; i <= MAX_MONITOR_BREAKPOINTS; i++)
820 if (breakaddr[i] == 0)
821 {
822 breakaddr[i] = addr;
823 if (kiodebug)
824 printf ("Breakpoint at %x\n", addr);
825 monitor_read_inferior_memory(addr, shadow, memory_breakpoint_size);
826 printf_monitor(SET_BREAK_CMD, addr);
827 expect_prompt(1);
828 return 0;
829 }
830
831 fprintf(stderr, "Too many breakpoints (> 16) for monitor\n");
832 return 1;
833}
834
835/*
836 * _remove_breakpoint -- Tell the monitor to remove a breakpoint
837 */
838static int
839monitor_remove_breakpoint (addr, shadow)
840 CORE_ADDR addr;
841 char *shadow;
842{
843 int i;
844
845#ifdef LOG_FILE
846 fprintf (log_file, "\nIn Remove_breakpoint (addr=%x)\n", addr);
847#endif
848 for (i = 0; i < MAX_MONITOR_BREAKPOINTS; i++)
849 if (breakaddr[i] == addr)
850 {
851 breakaddr[i] = 0;
852 /* some monitors remove breakpoints based on the address */
853 if (strcasecmp (target_shortname, "bug") == 0)
854 printf_monitor(CLR_BREAK_CMD, addr);
855 else
856 printf_monitor(CLR_BREAK_CMD, i);
857 expect_prompt(1);
858 return 0;
859 }
860
861 fprintf(stderr, "Can't find breakpoint associated with 0x%x\n", addr);
862 return 1;
863}
864
865/* Load a file. This is usually an srecord, which is ascii. No
866 protocol, just sent line by line. */
867
868#define DOWNLOAD_LINE_SIZE 100
869static void
870monitor_load (arg)
871 char *arg;
872{
873 FILE *download;
874 char buf[DOWNLOAD_LINE_SIZE];
875 int i, bytes_read;
876
877 if (kiodebug)
878 printf ("Loading %s to monitor\n", arg);
879
880 download = fopen (arg, "r");
881 if (download == NULL)
882 {
883 error (sprintf (buf, "%s Does not exist", arg));
884 return;
885 }
886
887 printf_monitor (LOAD_CMD);
888/* expect ("Waiting for S-records from host... ", 1); */
889
890 while (!feof (download))
891 {
892 bytes_read = fread (buf, sizeof (char), DOWNLOAD_LINE_SIZE, download);
893 if (hashmark)
894 {
895 putchar ('.');
896 fflush (stdout);
897 }
898
899 if (!serial_write(buf, bytes_read)) {
900 fprintf(stderr, "serial_write failed: (while downloading) %s\n", safe_strerror(errno));
901 break;
902 }
903 i = 0;
904 while (i++ <=200000) {} ; /* Ugly HACK, probably needs flow control */
905 if (bytes_read < DOWNLOAD_LINE_SIZE)
906 {
907 if (!feof (download))
908 error ("Only read %d bytes\n", bytes_read);
909 break;
910 }
911 }
912
913 if (hashmark)
914 {
915 putchar ('\n');
916 }
917 if (!feof (download))
918 error ("Never got EOF while downloading");
919 fclose (download);
920}
921
922/* Put a command string, in args, out to MONITOR. Output from MONITOR is placed
923 on the users terminal until the prompt is seen. */
924
925static void
926monitor_command (args, fromtty)
927 char *args;
928 int fromtty;
929{
930#ifdef LOG_FILE
931 fprintf (log_file, "\nIn command (args=%s)\n", args);
932#endif
933 if (monitor_desc < 0)
934 error("monitor target not open.");
935
936 if (!args)
937 error("Missing command.");
938
939 printf_monitor("%s\r", args);
940 expect_prompt(0);
941}
942
943/* Connect the user directly to MONITOR. This command acts just like the
944 'cu' or 'tip' command. Use <CR>~. or <CR>~^D to break out. */
945
946static struct ttystate ttystate;
947
948static void
949cleanup_tty()
950{ printf("\r\n[Exiting connect mode]\r\n");
951 serial_restore(0, &ttystate);
952}
953
954static void
955connect_command (args, fromtty)
956 char *args;
957 int fromtty;
958{
959 fd_set readfds;
960 int numfds;
961 int c;
962 char cur_esc = 0;
963
964 dont_repeat();
965
966 if (monitor_desc < 0)
967 error("monitor target not open.");
968
969 if (args)
970 fprintf("This command takes no args. They have been ignored.\n");
971
972 printf("[Entering connect mode. Use ~. or ~^D to escape]\n");
973
974 serial_raw(0, &ttystate);
975
976 make_cleanup(cleanup_tty, 0);
977
978 FD_ZERO(&readfds);
979
980 while (1)
981 {
982 do
983 {
984 FD_SET(0, &readfds);
985 FD_SET(monitor_desc, &readfds);
986 numfds = select(sizeof(readfds)*8, &readfds, 0, 0, 0);
987 }
988 while (numfds == 0);
989
990 if (numfds < 0)
991 perror_with_name("select");
992
993 if (FD_ISSET(0, &readfds))
994 { /* tty input, send to monitor */
995 c = getchar();
996 if (c < 0)
997 perror_with_name("connect");
998
999 printf_monitor("%c", c);
1000 switch (cur_esc)
1001 {
1002 case 0:
1003 if (c == '\r')
1004 cur_esc = c;
1005 break;
1006 case '\r':
1007 if (c == '~')
1008 cur_esc = c;
1009 else
1010 cur_esc = 0;
1011 break;
1012 case '~':
1013 if (c == '.' || c == '\004')
1014 return;
1015 else
1016 cur_esc = 0;
1017 }
1018 }
1019
1020 if (FD_ISSET(monitor_desc, &readfds))
1021 {
1022 while (1)
1023 {
1024 c = readchar(0);
1025 if (c < 0)
1026 break;
1027 putchar(c);
1028 }
1029 fflush(stdout);
1030 }
1031 }
1032}
1033
1034/*
1035 * Define the monitor command strings. Since these are passed directly
1036 * through to a printf style function, we need can include formatting
1037 * strings. We also need a CR or LF on the end.
1038 */
1039struct monitor_ops rom68k_cmds = {
1040 "go \r", /* execute or usually GO command */
1041 "go \r", /* continue command */
1042 "st \r", /* single step */
1043 "db %x\r", /* set a breakpoint */
1044 "cb %x\r", /* clear a breakpoint */
1045 "pm %x\r", /* set memory to a value */
1046 "pm %x\r", /* display memory */
1047 "-%08X ", /* prompt memory commands use */
1048 "pr %s %x\r", /* set a register */
1049 ": ", /* delimiter between registers */
1050 "pr %s\r", /* read a register */
1051 "dc \r", /* download command */
1052 "ROM68K :->", /* monitor command prompt */
1053 "=", /* end-of-command delimitor */
1054 ".\r" /* optional command terminator */
1055};
1056
1057struct monitor_ops bug_cmds = {
1058 "go \r", /* execute or usually GO command */
1059 "go \r", /* continue command */
1060 "gn \r", /* single step */
1061 "br %x\r", /* set a breakpoint */
1062 "nobr %x\r", /* clear a breakpoint */
1063 "mm %x\r", /* set memory to a value */
1064 "mm %x\r", /* display memory */
1065 "%08X", /* prompt memory commands use */
1066 "rs %s %x\r", /* set a register */
1067 "=", /* delimiter between registers */
1068 "rm %s\r", /* read a register */
1069 "lo 0\r", /* download command */
1070 "Bug>", /* monitor command prompt */
1071 "? ", /* end-of-command delimitor */
1072 ".\r" /* optional command terminator */
1073};
1074
1075/* Define the target subroutine names */
1076struct monitor_ops mon68_cmds = {
1077 "", /* execute or usually GO command */
1078 "", /* continue command */
1079 "", /* single step */
1080 "", /* set a breakpoint */
1081 "", /* clear a breakpoint */
1082 "", /* set memory to a value */
1083 "", /* display memory */
1084 "", /* set a register */
1085 "", /* delimiter between registers */
1086 "", /* read a register */
1087 "", /* download command */
1088 ">", /* monitor command prompt */
1089 "", /* end-of-command delimitor */
1090 "" /* optional command terminator */
1091};
1092
1093struct target_ops rom68k_ops = {
1094 "rom68k",
1095 "Integrated System's ROM68K remote debug monitor",
1096 "Use a remote computer running the ROM68K debug monitor, connected by a\n\
1097serial line. Arguments are the name of the device for the serial line,\n\
1098the speed to connect at in bits per second.",
1099 rom68k_open,
1100 monitor_close,
1101 0,
1102 monitor_detach,
1103 monitor_resume,
1104 monitor_wait,
1105 monitor_fetch_register,
1106 monitor_store_register,
1107 monitor_prepare_to_store,
1108 monitor_xfer_inferior_memory,
1109 monitor_files_info,
1110 monitor_insert_breakpoint,
1111 monitor_remove_breakpoint, /* Breakpoints */
1112 0,
1113 0,
1114 0,
1115 0,
1116 0, /* Terminal handling */
1117 monitor_kill,
1118 monitor_load, /* load */
1119 0, /* lookup_symbol */
1120 monitor_create_inferior,
1121 monitor_mourn_inferior,
1122 0, /* can_run */
1123 0, /* notice_signals */
1124 process_stratum,
1125 0, /* next */
1126 1,
1127 1,
1128 1,
1129 1,
1130 1, /* all mem, mem, stack, regs, exec */
1131 0,
1132 0, /* Section pointers */
1133 OPS_MAGIC, /* Always the last thing */
1134};
1135
1136struct target_ops bug_ops = {
1137 "bug",
1138 "Motorola's BUG remote serial debug monitor",
1139 "Use a remote computer running Motorola's BUG debug monitor, connected by a\n\
1140serial line. Arguments are the name of the device for the serial line,\n\
1141the speed to connect at in bits per second.",
1142 bug_open,
1143 monitor_close,
1144 0,
1145 monitor_detach,
1146 monitor_resume,
1147 monitor_wait,
1148 monitor_fetch_register,
1149 monitor_store_register,
1150 monitor_prepare_to_store,
1151 monitor_xfer_inferior_memory,
1152 monitor_files_info,
1153 monitor_insert_breakpoint,
1154 monitor_remove_breakpoint, /* Breakpoints */
1155 0,
1156 0,
1157 0,
1158 0,
1159 0, /* Terminal handling */
1160 monitor_kill,
1161 monitor_load, /* load */
1162 0, /* lookup_symbol */
1163 monitor_create_inferior,
1164 monitor_mourn_inferior,
1165 0, /* can_run */
1166 0, /* notice_signals */
1167 process_stratum,
1168 0, /* next */
1169 1,
1170 1,
1171 1,
1172 1,
1173 1, /* all mem, mem, stack, regs, exec */
1174 0,
1175 0, /* Section pointers */
1176 OPS_MAGIC, /* Always the last thing */
1177};
1178
1179struct target_ops mon68_ops = {
1180 "mon68",
1181 "Intermetric's MON68 remote serial debug monitor",
1182 "Use a remote computer running the MON68 debug monitor, connected by a\n\
1183serial line. Arguments are the name of the device for the serial line,\n\
1184the speed to connect at in bits per second.",
1185 mon68_open,
1186 monitor_close,
1187 0,
1188 monitor_detach,
1189 monitor_resume,
1190 monitor_wait,
1191 monitor_fetch_register,
1192 monitor_store_register,
1193 monitor_prepare_to_store,
1194 monitor_xfer_inferior_memory,
1195 monitor_files_info,
1196 monitor_insert_breakpoint,
1197 monitor_remove_breakpoint, /* Breakpoints */
1198 0,
1199 0,
1200 0,
1201 0,
1202 0, /* Terminal handling */
1203 monitor_kill,
1204 monitor_load, /* load */
1205 0, /* lookup_symbol */
1206 monitor_create_inferior,
1207 monitor_mourn_inferior,
1208 0, /* can_run */
1209 0, /* notice_signals */
1210 process_stratum,
1211 0, /* next */
1212 1,
1213 1,
1214 1,
1215 1,
1216 1, /* all mem, mem, stack, regs, exec */
1217 0,
1218 0, /* Section pointers */
1219 OPS_MAGIC, /* Always the last thing */
1220};
1221
1222void
1223_initialize_remote_monitors ()
1224{
1225 add_show_from_set (
1226 add_set_cmd ("remotedebug", no_class, var_boolean,
1227 (char *)&kiodebug,
1228 "Set debugging of I/O to a serial based Monitor.\n\
1229When enabled, debugging info is displayed.",
1230 &setlist),
1231 &showlist);
1232 add_show_from_set (
1233 add_set_cmd ("hash", no_class, var_boolean,
1234 (char *)&hashmark,
1235 "Set display of activity while downloading a file.\n\
1236When enabled, a period \'.\' is displayed.",
1237 &setlist),
1238 &showlist);
1239
1240 /* generic monitor command */
1241 add_com ("monitor <command>", class_obscure, monitor_command,
1242 "Send a command to the debug monitor.");
1243 add_com ("connect", class_obscure, connect_command,
1244 "Connect the terminal directly up to a serial based command monitor.\n\
1245Use <CR>~. or <CR>~^D to break out.");
1246
1247 add_target (&rom68k_ops);
1248/* add_target (&mon68_ops); */
1249 add_target (&bug_ops);
1250}
This page took 0.06777 seconds and 4 git commands to generate.