1 /* Remote target communications for serial-line targets in custom GDB protocol
2 Copyright 1988, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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. */
32 #include "gdb-stabs.h"
37 #include <sys/types.h>
43 /* Prototypes for local functions */
46 arc_write_bytes PARAMS ((CORE_ADDR memaddr, unsigned char *myaddr, int len));
49 arc_read_bytes PARAMS ((CORE_ADDR memaddr, unsigned char *myaddr, int len));
52 arc_files_info PARAMS ((struct target_ops *ignore));
55 arc_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len,
56 int should_write, struct target_ops *target));
59 arc_xfer_cntlreg PARAMS ((int rw, unsigned char *data));
62 arc_prepare_to_store PARAMS ((void));
65 arc_fetch_registers PARAMS ((int regno));
68 arc_resume PARAMS ((int pid, int step, enum target_signal siggnal));
71 arc_start_remote PARAMS ((char *dummy));
74 arc_open PARAMS ((char *name, int from_tty));
77 arc_close PARAMS ((int quitting));
80 arc_store_registers PARAMS ((int regno));
83 getpkt PARAMS ((char *buf, int len));
86 putpkt PARAMS ((char *buf, int len));
88 static int arc_wait PARAMS ((int pid, struct target_waitstatus *status));
91 arc_detach PARAMS ((char *args, int from_tty));
94 arc_interrupt PARAMS ((int signo));
97 arc_interrupt_twice PARAMS ((int signo));
100 interrupt_query PARAMS ((void));
102 extern struct target_ops arc_ops; /* Forward decl */
104 static int aux_reg_map[3][31] = AUX_REG_MAP;
106 /* This was 5 seconds, which is a long time to sit and wait.
107 Unless this is going though some terminal server or multiplexer or
108 other form of hairy serial connection, I would think 2 seconds would
110 static int remote_timeout = 2;
116 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
117 arc_open knows that we don't have a file open when the program
119 static serial_t arc_desc = NULL;
121 #define AUDIO_PROCESSOR 0
122 #define GRAPHIC_PROCESSOR 1
123 #define HOST_PROCESSOR 2
124 static unsigned char cntl_reg_halt_bit[3] = { 0x08, 0x10, 0x20 };
125 static unsigned char cntl_reg_step_bit[3] = { 0x01, 0x02, 0x04 };
127 static int curr_processor = HOST_PROCESSOR;
128 static unsigned char cntl_reg = 0;
129 static unsigned int status_reg = 0;
132 #define MAXBUFBYTES 32
135 /* Clean up connection to a remote debugger. */
143 SERIAL_CLOSE (arc_desc);
148 arc_start_remote (dummy)
151 immediate_quit = 1; /* Allow user to interrupt it */
152 arc_xfer_cntlreg (1, &cntl_reg);
155 start_remote (); /* Initialize gdb process mechanisms */
159 /* Open a connection to a remote debugger.
160 NAME is the filename used for communication. */
162 static DCACHE *remote_dcache;
165 arc_open (name, from_tty)
171 "To open a arc debug connection, you need to specify what parallel\n\
172 device is attached to the remote system.");
174 target_preopen (from_tty);
176 unpush_target (&arc_ops);
178 remote_dcache = dcache_init (arc_read_bytes, arc_write_bytes);
180 arc_desc = SERIAL_OPEN (name);
182 perror_with_name (name);
184 /* If there is something sitting in the buffer we might take it as a
185 response to a command, which would be bad. */
186 SERIAL_FLUSH_INPUT (arc_desc);
190 puts_filtered ("Remote debugging using ");
191 puts_filtered (name);
192 puts_filtered ("\n");
194 push_target (&arc_ops); /* Switch to using remote target now */
196 /* Without this, some commands which require an active target (such as kill)
197 won't work. This variable serves (at least) double duty as both the pid
198 of the target process (if it has such), and as a flag indicating that a
199 target is active. These functions should be split out into seperate
200 variables, especially since GDB will someday have a notion of debugging
201 several processes. */
203 inferior_pid = 42000;
205 /* Start the remote connection; if error (0), discard this target.
206 In particular, if the user quits, be sure to discard it
207 (we'd be in an inconsistent state otherwise). */
208 if (!catch_errors (arc_start_remote, (char *)0,
209 "Couldn't establish connection to remote target\n", RETURN_MASK_ALL))
214 takes a program previously attached to and detaches it.
215 We better not have left any breakpoints
216 in the program or it'll die when it hits one.
217 Close the open connection to the remote debugger.
218 Use this when you want to detach and do something else
222 arc_detach (args, from_tty)
227 error ("Argument given to \"detach\" when remotely debugging.");
231 puts_filtered ("Ending remote debugging.\n");
235 set addr reg from debug system. */
238 arc_set_addrreg (addr)
241 unsigned char buf[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
244 buf[4] = addr & 0xff;
245 buf[3] = (addr >> 8) & 0xff;
246 buf[2] = (addr >> 16) & 0xff;
247 buf[1] = (addr >> 24) & 0xff;
253 read or write data reg from debug system. */
256 arc_xfer_datareg (rw, aux, incr, data)
262 unsigned char buf1[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
263 unsigned char buf2[5];
266 /* read or write data */
267 buf1[0] = (aux ? 0x81 : 0x41);
275 *data = *(int *)buf2;
280 buf1[4] = tmp & 0xff;
281 buf1[3] = (tmp >> 8) & 0xff;
282 buf1[2] = (tmp >> 16) & 0xff;
283 buf1[1] = (tmp >> 24) & 0xff;
289 read or write control reg from debug system. */
292 arc_xfer_cntlreg (rw, data)
296 unsigned char buf[3] = {0x0, 0x0, 0x0};
313 read or write a reg to arc processors. */
316 arc_xfer_reg (processor, rw, aux, regnum, data)
325 if (processor == HOST_PROCESSOR)
327 /* write addr (regnum) */
328 arc_set_addrreg (regnum);
329 arc_xfer_datareg (rw, aux, 0, data);
333 /* write addr register (aux r14) */
334 arc_set_addrreg (0xe);
335 tmp = aux ? (regnum | 0x80000000) : regnum;
336 arc_xfer_datareg (0, 1, 0, tmp);
338 /* read/write from data reg (aux reg 15/16) */
339 arc_set_addrreg (processor == AUDIO_PROCESSOR ? 0x10 : 0xf);
340 arc_xfer_datareg (rw, 1, 0, data);
345 /* Tell the remote machine to resume. */
347 static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
351 arc_resume (pid, step, siggnal)
353 enum target_signal siggnal;
357 dcache_flush (remote_dcache);
359 last_sent_signal = siggnal;
360 last_sent_step = step;
362 /* don't know how to handle signal in ARC ***
363 if (siggnal != TARGET_SIGNAL_0)
365 buf[0] = step ? 'S' : 'C';
366 buf[1] = tohex (((int)siggnal >> 4) & 0xf);
367 buf[2] = tohex ((int)siggnal & 0xf);
374 /* write the step bit in control reg of debug system */
375 unsigned char tmp_char = cntl_reg | cntl_reg_step_bit[curr_processor];
376 arc_xfer_cntlreg (0, &tmp_char);
380 /* clear the halt bit in the status register */
381 tmp = status_reg | 0x02000000;
382 arc_xfer_reg (curr_processor, 0, 1, 0, &tmp);
387 static void (*ofunc)();
389 /* Send to target to halt it. */
391 arc_interrupt (signo)
394 unsigned char buf[3] = {0x02, 0x0, 0x0};
395 /* If this doesn't work, try more severe steps. */
396 signal (signo, arc_interrupt_twice);
399 printf_unfiltered ("arc_interrupt called\n");
401 if (cntl_reg & cntl_reg_halt_bit[curr_processor])
404 buf[1] = cntl_reg | cntl_reg_halt_bit[curr_processor];
409 /* The user typed ^C twice. */
411 arc_interrupt_twice (signo)
414 signal (signo, ofunc);
418 signal (signo, arc_interrupt);
421 /* Ask the user what to do when an interrupt is received. */
426 target_terminal_ours ();
428 if (query ("Interrupted while waiting for the program.\n\
429 Give up (and stop debugging it)? "))
431 target_mourn_inferior ();
432 return_to_top_level (RETURN_QUIT);
435 target_terminal_inferior ();
438 /* If nonzero, ignore the next kill. */
441 /* Wait until the remote machine stops, then return,
442 storing status in STATUS just as `wait' would.
443 Returns "pid" (though it's not clear what, if anything, that
444 means in the case of this target). */
447 arc_wait (pid, status)
449 struct target_waitstatus *status;
451 unsigned char buf[PBUFSIZ];
456 status->kind = TARGET_WAITKIND_EXITED;
457 status->value.integer = 0;
463 ofunc = (void (*)()) signal (SIGINT, arc_interrupt);
464 arc_xfer_cntlreg (1, &cntl_reg);
465 signal (SIGINT, ofunc);
466 if (cntl_reg & cntl_reg_halt_bit[curr_processor])
468 status->kind = TARGET_WAITKIND_STOPPED;
471 if ((curr_processor != HOST_PROCESSOR) &&
472 !(cntl_reg & cntl_reg_halt_bit[HOST_PROCESSOR]))
474 cmd = cntl_reg | cntl_reg_halt_bit[HOST_PROCESSOR];
475 arc_xfer_cntlreg (0, &cmd);
480 ofunc = (void (*)()) signal (SIGINT, arc_interrupt);
481 arc_xfer_cntlreg (1, &cntl_reg);
482 signal (SIGINT, ofunc);
483 if (cntl_reg & cntl_reg_halt_bit[HOST_PROCESSOR])
488 for (proc = AUDIO_PROCESSOR ; proc <= GRAPHIC_PROCESSOR; proc++)
490 if ((cntl_reg & cntl_reg_halt_bit[proc]))
492 cmd = cntl_reg | cntl_reg_halt_bit[proc];
493 arc_xfer_cntlreg (0, &cmd);
496 arc_xfer_reg (curr_processor, 1, 1, 0, &status_reg);
500 /* Number of bytes of registers this implements. */
501 static int register_bytes_found;
503 /* Read the remote registers into the block REGS. */
504 /* Currently we just read all the registers, so we don't use regno. */
507 arc_fetch_registers (regno)
511 char regs[REGISTER_BYTES];
513 /* Unimplemented registers read as all bits zero. */
514 memset (regs, 0, REGISTER_BYTES);
516 /* get core register */
518 for (i = 0; i < AUX_BEG_REGNUM; i++)
520 if (curr_processor == HOST_PROCESSOR)
521 arc_xfer_datareg (1, 0, 1, ®s[REGISTER_BYTE(i)]);
523 arc_xfer_reg (curr_processor, 1, 0, regno, ®s[REGISTER_BYTE(i)]);
526 /* get aux register */
527 for (i = AUX_BEG_REGNUM; i < AUX_END_REGNUM; i++)
529 int auxregnum = aux_reg_map[curr_processor][i-AUX_BEG_REGNUM+1];
532 arc_xfer_reg (curr_processor, 1, 1, auxregnum, ®s[REGISTER_BYTE(i)]);
534 /* copy from status register to pc */
535 for (i = 1; i <= 3; i++)
536 registers[REGISTER_BYTE (PC_REGNUM)+i] =
537 registers[REGISTER_BYTE (STA_REGNUM)+i];
540 if (i != register_bytes_found)
542 register_bytes_found = i;
543 if (!REGISTER_BYTES_OK (i))
544 warning ("Remote reply is too short: %s", buf);
548 for (i = 0; i < NUM_REGS; i++)
549 supply_register (i, ®s[REGISTER_BYTE(i)]);
552 /* Prepare to store registers. Since we may send them all,
553 we have to read out the ones we don't want to change first. */
556 arc_prepare_to_store ()
558 /* Make sure the entire registers array is valid. */
559 read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
562 /* Store register REGNO, or all registers if REGNO == -1, from the contents
563 of REGISTERS. FIXME: ignores errors. */
566 arc_store_registers (regno)
572 /* Try storing a single register. */
575 int isaux = (regno >= AUX_BEG_REGNUM ? 1 : 0);
577 regp = ®isters[REGISTER_BYTE (regno)];
578 arc_xfer_reg (curr_processor, 0, isaux, regno, regp);
581 /* store core regs */
583 for (i = 0; i < AUX_BEG_REGNUM; i++)
585 regp = ®isters[REGISTER_BYTE (i)];
586 if (curr_processor == HOST_PROCESSOR)
587 arc_xfer_datareg (0, 0, 1, regp);
589 arc_xfer_reg (curr_processor, 0, 0, regno, regp);
593 /* copy pc back to status register */
594 for (i = 1; i <= 3; i++)
595 registers[REGISTER_BYTE (STA_REGNUM)+i] =
596 registers[REGISTER_BYTE (PC_REGNUM)+i];
597 for (i = AUX_BEG_REGNUM; i <= AUX_END_REGNUM; i++)
599 int auxregnum = aux_reg_map[curr_processor][i-AUX_BEG_REGNUM+1];
602 regp = ®isters[REGISTER_BYTE (i)];
603 arc_xfer_reg (curr_processor, 0, 1, auxregnum, regp);
609 /* Use of the data cache is disabled because it loses for looking at
610 and changing hardware I/O ports and the like. Accepting `voltile'
611 would perhaps be one way to fix it, but a better way which would
612 win for more cases would be to use the executable file for the text
613 segment, like the `icache' code below but done cleanly (in some
614 target-independent place, perhaps in target_xfer_memory, perhaps
615 based on assigning each target a speed or perhaps by some simpler
618 /* Read a word from remote address ADDR and return it.
619 This goes through the data cache. */
622 arc_fetch_word (addr)
628 extern CORE_ADDR text_start, text_end;
630 if (addr >= text_start && addr < text_end)
633 xfer_core_file (addr, &buffer, sizeof (int));
638 return dcache_fetch (remote_dcache, addr);
641 /* Write a word WORD into remote address ADDR.
642 This goes through the data cache. */
645 arc_store_word (addr, word)
649 dcache_poke (remote_dcache, addr, word);
654 /* Write memory data directly to the remote machine.
655 This does not inform the data cache; the data cache uses this.
656 MEMADDR is the address in the remote memory space.
657 MYADDR is the address of the buffer in our space.
658 LEN is the number of bytes.
660 Returns number of bytes transferred, or 0 for error. */
663 arc_write_bytes (memaddr, myaddr, len)
665 unsigned char *myaddr;
668 char buf1[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
669 char buf2[6] = {0x21, 0x0, 0x0, 0x0, 0x0, 0x0};
673 /* We send target system values byte by byte, in increasing byte addresses*/
675 buf1[4] = memaddr & 0xff;
676 buf1[3] = (memaddr >> 8) & 0xff;
677 buf1[2] = (memaddr >> 16) & 0xff;
678 buf1[1] = (memaddr >> 24) & 0xff;
681 for (i = 0; i < len; )
683 buf2[1] = myaddr[i++];
684 buf2[2] = myaddr[i++];
685 buf2[3] = myaddr[i++];
686 buf2[4] = myaddr[i++];
693 /* Read memory data directly from the remote machine.
694 This does not use the data cache; the data cache uses this.
695 MEMADDR is the address in the remote memory space.
696 MYADDR is the address of the buffer in our space.
697 LEN is the number of bytes.
699 Returns number of bytes transferred, or 0 for error. */
702 arc_read_bytes (memaddr, myaddr, len)
704 unsigned char *myaddr;
707 unsigned char buf1[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
708 unsigned char buf2[3] = {0x31, 0x0, 0x0};
709 unsigned char buf3[5];
713 /* We send target system values byte by byte, in increasing byte addresses*/
715 buf1[4] = memaddr & 0xff;
716 buf1[3] = (memaddr >> 8) & 0xff;
717 buf1[2] = (memaddr >> 16) & 0xff;
718 buf1[1] = (memaddr >> 24) & 0xff;
721 for (i = 0; i < len; )
725 myaddr[i++] = buf3[1];
726 myaddr[i++] = buf3[2];
727 myaddr[i++] = buf3[3];
728 myaddr[i++] = buf3[4];
735 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
736 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
737 nonzero. Returns length of data written or read; 0 for error. */
741 arc_xfer_memory(memaddr, myaddr, len, should_write, target)
746 struct target_ops *target; /* ignored */
750 int total_xferred = 0;
754 if (len > MAXBUFBYTES)
755 xfersize = MAXBUFBYTES;
760 bytes_xferred = arc_write_bytes (memaddr,
761 (unsigned char *)myaddr, xfersize);
763 bytes_xferred = arc_read_bytes (memaddr,
764 (unsigned char *)myaddr, xfersize);
766 /* If we get an error, we are done xferring. */
767 if (bytes_xferred == 0)
770 memaddr += bytes_xferred;
771 myaddr += bytes_xferred;
772 len -= bytes_xferred;
773 total_xferred += bytes_xferred;
775 return total_xferred;
780 arc_files_info (ignore)
781 struct target_ops *ignore;
783 puts_filtered ("Debugging a target over a serial line.\n");
787 /* Read a single character from the remote end, masking it down to 7 bits. */
793 ch = SERIAL_READCHAR (arc_desc, 0);
798 error ("Remote connection closed");
800 perror_with_name ("Remote communication error");
808 /* Send a packet to the remote machine, with error checking.
809 The data of the packet is in BUF. */
817 unsigned char csum = 0;
823 printf_unfiltered ("Sending packet: %s...", buf);
824 gdb_flush(gdb_stdout);
826 if (SERIAL_WRITE (arc_desc, buf, i))
827 perror_with_name ("putpkt: write failed");
831 /* This is wrong. If doing a long backtrace, the user should be
832 able to get out next time we call QUIT, without anything as violent
833 as interrupt_query. If we want to provide a way out of here
834 without getting to the next QUIT, it should be based on hitting
835 ^C twice as in arc_wait. */
845 /* Read a packet from the remote machine, with error checking,
846 and store it in BUF. BUF is expected to be of size PBUFSIZ.
847 If FOREVER, wait forever rather than timing out; this is used
848 while the target is executing user code. */
858 for (i = 0; i < len; i++)
861 if (c == SERIAL_ERROR)
864 puts_filtered ("Read error.\n");
870 fprintf_unfiltered (gdb_stderr, "Packet received: %s\n", buf);
872 printf_unfiltered ("Ignoring packet error, continuing...\n");
878 /* For some mysterious reason, wait_for_inferior calls kill instead of
879 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
883 target_mourn_inferior ();
886 target_mourn_inferior ();
892 unpush_target (&arc_ops);
893 generic_mourn_inferior ();
898 static unsigned char big_break_insn[] = BIG_BREAKPOINT;
899 static unsigned char little_break_insn[] = LITTLE_BREAKPOINT;
900 #define BREAKPOINT_LEN (sizeof little_break_insn)
902 /* Insert a breakpoint on targets that don't have any better breakpoint
903 support. We read the contents of the target location and stash it,
904 then overwrite it with a breakpoint instruction. ADDR is the target
905 location in the target machine. CONTENTS_CACHE is a pointer to
906 memory allocated for saving the target contents. It is guaranteed
907 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
908 is accomplished via BREAKPOINT_MAX). */
911 arc_insert_breakpoint (addr, contents_cache)
913 char *contents_cache;
917 val = target_read_memory (addr, contents_cache, BREAKPOINT_LEN);
920 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
921 val = target_write_memory (addr, (char *) big_break_insn,
924 val = target_write_memory (addr, (char *) little_break_insn,
931 arc_remove_breakpoint (addr, contents_cache)
933 char *contents_cache;
935 return target_write_memory (addr, contents_cache, BREAKPOINT_LEN);
939 support 'switch' command to switch among the three processors of ARC. */
942 switch_command (args, fromtty)
946 struct target_waitstatus status;
949 if (strncmp (args, "audio", 3) == 0)
951 else if (strncmp (args, "graphic", 3) == 0)
953 else if (strncmp (args, "host", 4) == 0)
956 curr_processor = proc;
961 tm_print_insn = arc_get_disassembler (bfd_mach_arc_audio,
962 TARGET_BYTE_ORDER == BIG_ENDIAN);
965 tm_print_insn = arc_get_disassembler (bfd_mach_arc_graphics,
966 TARGET_BYTE_ORDER == BIG_ENDIAN);
969 tm_print_insn = arc_get_disassembler (bfd_mach_arc_host,
970 TARGET_BYTE_ORDER == BIG_ENDIAN);
978 /* Define the target subroutine names */
980 struct target_ops arc_ops = {
981 "arc", /* to_shortname */
982 "Remote target in arc-specific protocol", /* to_longname */
983 "Use a remote computer via a parallel line, using a arc-specific protocol.\n\
984 Specify the device it is connected to.", /* to_doc */
985 arc_open, /* to_open */
986 arc_close, /* to_close */
987 NULL, /* to_attach */
988 arc_detach, /* to_detach */
989 arc_resume, /* to_resume */
990 arc_wait, /* to_wait */
991 arc_fetch_registers, /* to_fetch_registers */
992 arc_store_registers, /* to_store_registers */
993 arc_prepare_to_store, /* to_prepare_to_store */
994 arc_xfer_memory, /* to_xfer_memory */
995 arc_files_info, /* to_files_info */
997 arc_insert_breakpoint, /* to_insert_breakpoint */
998 arc_remove_breakpoint, /* to_remove_breakpoint */
1000 NULL, /* to_terminal_init */
1001 NULL, /* to_terminal_inferior */
1002 NULL, /* to_terminal_ours_for_output */
1003 NULL, /* to_terminal_ours */
1004 NULL, /* to_terminal_info */
1005 arc_kill, /* to_kill */
1006 generic_load, /* to_load */
1007 NULL, /* to_lookup_symbol */
1008 NULL, /* to_create_inferior */
1009 arc_mourn, /* to_mourn_inferior */
1011 0, /* to_notice_signals */
1013 process_stratum, /* to_stratum */
1015 1, /* to_has_all_memory */
1016 1, /* to_has_memory */
1017 1, /* to_has_stack */
1018 1, /* to_has_registers */
1019 1, /* to_has_execution */
1020 NULL, /* sections */
1021 NULL, /* sections_end */
1022 OPS_MAGIC /* to_magic */
1026 _initialize_remote_arc ()
1028 add_target (&arc_ops);