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