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][30] = 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
];
454 status
->kind
= TARGET_WAITKIND_EXITED
;
455 status
->value
.integer
= 0;
461 ofunc
= (void (*)()) signal (SIGINT
, arc_interrupt
);
462 arc_xfer_cntlreg (1, &cntl_reg
);
463 signal (SIGINT
, ofunc
);
464 if (cntl_reg
& cntl_reg_halt_bit
[curr_processor
])
466 status
->kind
= TARGET_WAITKIND_STOPPED
;
468 arc_xfer_reg (curr_processor
, 1, 1, 0, &status_reg
);
473 /* Number of bytes of registers this implements. */
474 static int register_bytes_found
;
476 /* Read the remote registers into the block REGS. */
477 /* Currently we just read all the registers, so we don't use regno. */
480 arc_fetch_registers (regno
)
484 char regs
[REGISTER_BYTES
];
486 /* Unimplemented registers read as all bits zero. */
487 memset (regs
, 0, REGISTER_BYTES
);
489 /* get core register */
491 for (i
= 0; i
< AUX_BEG_REGNUM
; i
++)
493 if (curr_processor
== HOST_PROCESSOR
)
494 arc_xfer_datareg (1, 0, 1, ®s
[REGISTER_BYTE(i
)]);
496 arc_xfer_reg (curr_processor
, 1, 0, regno
, ®s
[REGISTER_BYTE(i
)]);
499 /* get aux register */
500 for (i
= AUX_BEG_REGNUM
; i
< AUX_END_REGNUM
; i
++)
502 int auxregnum
= aux_reg_map
[curr_processor
][i
-AUX_BEG_REGNUM
+1];
505 arc_xfer_reg (curr_processor
, 1, 1, auxregnum
, ®s
[REGISTER_BYTE(i
)]);
507 /* copy from status register to pc */
508 for (i
= 1; i
<= 3; i
++)
509 registers
[REGISTER_BYTE (PC_REGNUM
)+i
] =
510 registers
[REGISTER_BYTE (STA_REGNUM
)+i
];
513 if (i != register_bytes_found)
515 register_bytes_found = i;
516 if (!REGISTER_BYTES_OK (i))
517 warning ("Remote reply is too short: %s", buf);
521 for (i
= 0; i
< NUM_REGS
; i
++)
522 supply_register (i
, ®s
[REGISTER_BYTE(i
)]);
525 /* Prepare to store registers. Since we may send them all,
526 we have to read out the ones we don't want to change first. */
529 arc_prepare_to_store ()
531 /* Make sure the entire registers array is valid. */
532 read_register_bytes (0, (char *)NULL
, REGISTER_BYTES
);
535 /* Store register REGNO, or all registers if REGNO == -1, from the contents
536 of REGISTERS. FIXME: ignores errors. */
539 arc_store_registers (regno
)
545 /* Try storing a single register. */
548 int isaux
= (regno
>= AUX_BEG_REGNUM
? 1 : 0);
550 regp
= ®isters
[REGISTER_BYTE (regno
)];
551 arc_xfer_reg (curr_processor
, 0, isaux
, regno
, regp
);
554 /* store core regs */
556 for (i
= 0; i
< AUX_BEG_REGNUM
; i
++)
558 regp
= ®isters
[REGISTER_BYTE (i
)];
559 if (curr_processor
== HOST_PROCESSOR
)
560 arc_xfer_datareg (0, 0, 1, regp
);
562 arc_xfer_reg (curr_processor
, 0, 0, regno
, regp
);
566 /* copy pc back to status register */
567 for (i
= 1; i
<= 3; i
++)
568 registers
[REGISTER_BYTE (STA_REGNUM
)+i
] =
569 registers
[REGISTER_BYTE (PC_REGNUM
)+i
];
570 for (i
= AUX_BEG_REGNUM
; i
<= AUX_END_REGNUM
; i
++)
572 int auxregnum
= aux_reg_map
[curr_processor
][i
-AUX_BEG_REGNUM
+1];
575 regp
= ®isters
[REGISTER_BYTE (i
)];
576 arc_xfer_reg (curr_processor
, 0, 1, auxregnum
, regp
);
582 /* Use of the data cache is disabled because it loses for looking at
583 and changing hardware I/O ports and the like. Accepting `voltile'
584 would perhaps be one way to fix it, but a better way which would
585 win for more cases would be to use the executable file for the text
586 segment, like the `icache' code below but done cleanly (in some
587 target-independent place, perhaps in target_xfer_memory, perhaps
588 based on assigning each target a speed or perhaps by some simpler
591 /* Read a word from remote address ADDR and return it.
592 This goes through the data cache. */
595 arc_fetch_word (addr
)
601 extern CORE_ADDR text_start
, text_end
;
603 if (addr
>= text_start
&& addr
< text_end
)
606 xfer_core_file (addr
, &buffer
, sizeof (int));
611 return dcache_fetch (remote_dcache
, addr
);
614 /* Write a word WORD into remote address ADDR.
615 This goes through the data cache. */
618 arc_store_word (addr
, word
)
622 dcache_poke (remote_dcache
, addr
, word
);
627 /* Write memory data directly to the remote machine.
628 This does not inform the data cache; the data cache uses this.
629 MEMADDR is the address in the remote memory space.
630 MYADDR is the address of the buffer in our space.
631 LEN is the number of bytes.
633 Returns number of bytes transferred, or 0 for error. */
636 arc_write_bytes (memaddr
, myaddr
, len
)
638 unsigned char *myaddr
;
641 char buf1
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
642 char buf2
[6] = {0x21, 0x0, 0x0, 0x0, 0x0, 0x0};
646 /* We send target system values byte by byte, in increasing byte addresses*/
648 buf1
[4] = memaddr
& 0xff;
649 buf1
[3] = (memaddr
>> 8) & 0xff;
650 buf1
[2] = (memaddr
>> 16) & 0xff;
651 buf1
[1] = (memaddr
>> 24) & 0xff;
654 for (i
= 0; i
< len
; )
656 buf2
[1] = myaddr
[i
++];
657 buf2
[2] = myaddr
[i
++];
658 buf2
[3] = myaddr
[i
++];
659 buf2
[4] = myaddr
[i
++];
666 /* Read memory data directly from the remote machine.
667 This does not use the data cache; the data cache uses this.
668 MEMADDR is the address in the remote memory space.
669 MYADDR is the address of the buffer in our space.
670 LEN is the number of bytes.
672 Returns number of bytes transferred, or 0 for error. */
675 arc_read_bytes (memaddr
, myaddr
, len
)
677 unsigned char *myaddr
;
680 unsigned char buf1
[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
681 unsigned char buf2
[3] = {0x31, 0x0, 0x0};
682 unsigned char buf3
[5];
686 /* We send target system values byte by byte, in increasing byte addresses*/
688 buf1
[4] = memaddr
& 0xff;
689 buf1
[3] = (memaddr
>> 8) & 0xff;
690 buf1
[2] = (memaddr
>> 16) & 0xff;
691 buf1
[1] = (memaddr
>> 24) & 0xff;
694 for (i
= 0; i
< len
; )
698 myaddr
[i
++] = buf3
[1];
699 myaddr
[i
++] = buf3
[2];
700 myaddr
[i
++] = buf3
[3];
701 myaddr
[i
++] = buf3
[4];
708 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
709 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
710 nonzero. Returns length of data written or read; 0 for error. */
714 arc_xfer_memory(memaddr
, myaddr
, len
, should_write
, target
)
719 struct target_ops
*target
; /* ignored */
723 int total_xferred
= 0;
727 if (len
> MAXBUFBYTES
)
728 xfersize
= MAXBUFBYTES
;
733 bytes_xferred
= arc_write_bytes (memaddr
,
734 (unsigned char *)myaddr
, xfersize
);
736 bytes_xferred
= arc_read_bytes (memaddr
,
737 (unsigned char *)myaddr
, xfersize
);
739 /* If we get an error, we are done xferring. */
740 if (bytes_xferred
== 0)
743 memaddr
+= bytes_xferred
;
744 myaddr
+= bytes_xferred
;
745 len
-= bytes_xferred
;
746 total_xferred
+= bytes_xferred
;
748 return total_xferred
;
753 arc_files_info (ignore
)
754 struct target_ops
*ignore
;
756 puts_filtered ("Debugging a target over a serial line.\n");
760 /* Read a single character from the remote end, masking it down to 7 bits. */
766 ch
= SERIAL_READCHAR (arc_desc
, 0);
771 error ("Remote connection closed");
773 perror_with_name ("Remote communication error");
781 /* Send a packet to the remote machine, with error checking.
782 The data of the packet is in BUF. */
790 unsigned char csum
= 0;
796 printf_unfiltered ("Sending packet: %s...", buf
);
797 gdb_flush(gdb_stdout
);
799 if (SERIAL_WRITE (arc_desc
, buf
, i
))
800 perror_with_name ("putpkt: write failed");
804 /* This is wrong. If doing a long backtrace, the user should be
805 able to get out next time we call QUIT, without anything as violent
806 as interrupt_query. If we want to provide a way out of here
807 without getting to the next QUIT, it should be based on hitting
808 ^C twice as in arc_wait. */
818 /* Read a packet from the remote machine, with error checking,
819 and store it in BUF. BUF is expected to be of size PBUFSIZ.
820 If FOREVER, wait forever rather than timing out; this is used
821 while the target is executing user code. */
831 for (i
= 0; i
< len
; i
++)
834 if (c
== SERIAL_ERROR
)
837 puts_filtered ("Read error.\n");
843 fprintf_unfiltered (gdb_stderr
, "Packet received: %s\n", buf
);
845 printf_unfiltered ("Ignoring packet error, continuing...\n");
851 /* For some mysterious reason, wait_for_inferior calls kill instead of
852 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
856 target_mourn_inferior ();
859 target_mourn_inferior ();
865 unpush_target (&arc_ops
);
866 generic_mourn_inferior ();
870 #ifdef REMOTE_BREAKPOINT
872 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
873 than other targets. */
874 static unsigned char break_insn
[] = REMOTE_BREAKPOINT
;
876 /* Check that it fits in BREAKPOINT_MAX bytes. */
877 static unsigned char check_break_insn_size
[BREAKPOINT_MAX
] = REMOTE_BREAKPOINT
;
879 #else /* No REMOTE_BREAKPOINT. */
881 static unsigned char break_insn
[] = BREAKPOINT
;
883 #endif /* No REMOTE_BREAKPOINT. */
885 /* Insert a breakpoint on targets that don't have any better breakpoint
886 support. We read the contents of the target location and stash it,
887 then overwrite it with a breakpoint instruction. ADDR is the target
888 location in the target machine. CONTENTS_CACHE is a pointer to
889 memory allocated for saving the target contents. It is guaranteed
890 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
891 is accomplished via BREAKPOINT_MAX). */
894 arc_insert_breakpoint (addr
, contents_cache
)
896 char *contents_cache
;
900 val
= target_read_memory (addr
, contents_cache
, sizeof break_insn
);
902 val
= target_write_memory (addr
, (char *)break_insn
, sizeof break_insn
);
907 arc_remove_breakpoint (addr
, contents_cache
)
909 char *contents_cache
;
911 return target_write_memory (addr
, contents_cache
, sizeof break_insn
);
915 support 'switch' command to switch among the three processors of ARC. */
918 switch_command (args
, fromtty
)
924 struct target_waitstatus status
;
926 if (strncmp (args
, "audio", 3) == 0)
928 else if (strncmp (args
, "graphic", 3) == 0)
930 else if (strncmp (args
, "host", 4) == 0)
933 if (cntl_reg
& cntl_reg_halt_bit
[proc
])
935 curr_processor
= proc
;
939 if ((proc
!= HOST_PROCESSOR
) &&
940 !(cntl_reg
& cntl_reg_halt_bit
[HOST_PROCESSOR
]))
942 cmd
= cntl_reg
| cntl_reg_halt_bit
[HOST_PROCESSOR
];
943 arc_xfer_cntlreg (0, &cmd
);
944 curr_processor
= HOST_PROCESSOR
;
945 arc_wait (inferior_pid
, &status
);
947 if (!(cntl_reg
& cntl_reg_halt_bit
[proc
]))
949 cmd
= cntl_reg
| cntl_reg_halt_bit
[proc
];
950 arc_xfer_cntlreg (0, &cmd
);
951 curr_processor
= proc
;
952 arc_wait (inferior_pid
, &status
);
953 arc_fetch_registers (-1);
958 /* Define the target subroutine names */
960 struct target_ops arc_ops
= {
961 "arc", /* to_shortname */
962 "Remote target in arc-specific protocol", /* to_longname */
963 "Use a remote computer via a parallel line, using a arc-specific protocol.\n\
964 Specify the device it is connected to.", /* to_doc */
965 arc_open
, /* to_open */
966 arc_close
, /* to_close */
967 NULL
, /* to_attach */
968 arc_detach
, /* to_detach */
969 arc_resume
, /* to_resume */
970 arc_wait
, /* to_wait */
971 arc_fetch_registers
, /* to_fetch_registers */
972 arc_store_registers
, /* to_store_registers */
973 arc_prepare_to_store
, /* to_prepare_to_store */
974 arc_xfer_memory
, /* to_xfer_memory */
975 arc_files_info
, /* to_files_info */
977 arc_insert_breakpoint
, /* to_insert_breakpoint */
978 arc_remove_breakpoint
, /* to_remove_breakpoint */
980 NULL
, /* to_terminal_init */
981 NULL
, /* to_terminal_inferior */
982 NULL
, /* to_terminal_ours_for_output */
983 NULL
, /* to_terminal_ours */
984 NULL
, /* to_terminal_info */
985 arc_kill
, /* to_kill */
986 generic_load
, /* to_load */
987 NULL
, /* to_lookup_symbol */
988 NULL
, /* to_create_inferior */
989 arc_mourn
, /* to_mourn_inferior */
991 0, /* to_notice_signals */
993 process_stratum
, /* to_stratum */
995 1, /* to_has_all_memory */
996 1, /* to_has_memory */
997 1, /* to_has_stack */
998 1, /* to_has_registers */
999 1, /* to_has_execution */
1000 NULL
, /* sections */
1001 NULL
, /* sections_end */
1002 OPS_MAGIC
/* to_magic */
1006 _initialize_remote_arc ()
1008 add_target (&arc_ops
);
1009 add_com ("switch <processor>", class_obscure
, switch_command
,
1010 "Switch to debug a different processor, can be one of 'host', \
1011 'graphic' and 'audio'.");