1 /* Target-dependent code for Mitsubishi D10V, for GDB.
2 Copyright (C) 1996, 1997 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
29 #include "gdb_string.h"
36 void d10v_frame_find_saved_regs PARAMS ((struct frame_info *fi,
37 struct frame_saved_regs *fsr));
38 static void d10v_pop_dummy_frame PARAMS ((struct frame_info *fi));
40 /* Discard from the stack the innermost frame, restoring all saved
46 struct frame_info *frame = get_current_frame ();
49 struct frame_saved_regs fsr;
52 fp = FRAME_FP (frame);
55 d10v_pop_dummy_frame(frame);
59 /* fill out fsr with the address of where each */
60 /* register was stored in the frame */
61 get_frame_saved_regs (frame, &fsr);
63 /* now update the current registers with the old values */
64 for (regnum = A0_REGNUM; regnum < A0_REGNUM+2 ; regnum++)
68 read_memory (fsr.regs[regnum], raw_buffer, 8);
69 write_register_bytes (REGISTER_BYTE (regnum), raw_buffer, 8);
72 for (regnum = 0; regnum < SP_REGNUM; regnum++)
76 write_register (regnum, read_memory_unsigned_integer (fsr.regs[regnum], 2));
79 if (fsr.regs[PSW_REGNUM])
81 write_register (PSW_REGNUM, read_memory_unsigned_integer (fsr.regs[PSW_REGNUM], 2));
84 write_register (PC_REGNUM, read_register(13));
85 write_register (SP_REGNUM, fp + frame->size);
86 target_store_registers (-1);
87 flush_cached_frames ();
95 if ((op & 0x7E1F) == 0x6C1F)
99 if ((op & 0x7E3F) == 0x6E1F)
103 if ((op & 0x7FE1) == 0x01E1)
115 if ((op & 0x7E1F) == 0x681E)
119 if ((op & 0x7E3F) == 0x3A1E)
126 d10v_skip_prologue (pc)
130 unsigned short op1, op2;
131 CORE_ADDR func_addr, func_end;
132 struct symtab_and_line sal;
134 /* If we have line debugging information, then the end of the */
135 /* prologue should the first assembly instruction of the first source line */
136 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
138 sal = find_pc_line (func_addr, 0);
139 if ( sal.end && sal.end < func_end)
143 if (target_read_memory (pc, (char *)&op, 4))
144 return pc; /* Can't access it -- assume no prologue. */
148 op = (unsigned long)read_memory_integer (pc, 4);
149 if ((op & 0xC0000000) == 0xC0000000)
151 /* long instruction */
152 if ( ((op & 0x3FFF0000) != 0x01FF0000) && /* add3 sp,sp,n */
153 ((op & 0x3F0F0000) != 0x340F0000) && /* st rn, @(offset,sp) */
154 ((op & 0x3F1F0000) != 0x350F0000)) /* st2w rn, @(offset,sp) */
159 /* short instructions */
160 if ((op & 0xC0000000) == 0x80000000)
162 op2 = (op & 0x3FFF8000) >> 15;
167 op1 = (op & 0x3FFF8000) >> 15;
170 if (check_prologue(op1))
172 if (!check_prologue(op2))
174 /* if the previous opcode was really part of the prologue */
175 /* and not just a NOP, then we want to break after both instructions */
189 /* Given a GDB frame, determine the address of the calling function's frame.
190 This will be used to create a new GDB frame struct, and then
191 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
195 d10v_frame_chain (frame)
196 struct frame_info *frame;
198 struct frame_saved_regs fsr;
200 d10v_frame_find_saved_regs (frame, &fsr);
202 if (frame->return_pc == IMEM_START)
205 if (!fsr.regs[FP_REGNUM])
207 if (!fsr.regs[SP_REGNUM] || fsr.regs[SP_REGNUM] == STACK_START)
210 return fsr.regs[SP_REGNUM];
213 if (!read_memory_unsigned_integer(fsr.regs[FP_REGNUM],2))
216 return read_memory_unsigned_integer(fsr.regs[FP_REGNUM],2)| DMEM_START;
219 static int next_addr, uses_frame;
222 prologue_find_regs (op, fsr, addr)
224 struct frame_saved_regs *fsr;
230 if ((op & 0x7E1F) == 0x6C1F)
232 n = (op & 0x1E0) >> 5;
234 fsr->regs[n] = next_addr;
239 else if ((op & 0x7E3F) == 0x6E1F)
241 n = (op & 0x1E0) >> 5;
243 fsr->regs[n] = next_addr;
244 fsr->regs[n+1] = next_addr+2;
249 if ((op & 0x7FE1) == 0x01E1)
251 n = (op & 0x1E) >> 1;
270 if ((op & 0x7E1F) == 0x681E)
272 n = (op & 0x1E0) >> 5;
273 fsr->regs[n] = next_addr;
278 if ((op & 0x7E3F) == 0x3A1E)
280 n = (op & 0x1E0) >> 5;
281 fsr->regs[n] = next_addr;
282 fsr->regs[n+1] = next_addr+2;
289 /* Put here the code to store, into a struct frame_saved_regs, the
290 addresses of the saved registers of frame described by FRAME_INFO.
291 This includes special registers such as pc and fp saved in special
292 ways in the stack frame. sp is even more special: the address we
293 return for it IS the sp for the next frame. */
295 d10v_frame_find_saved_regs (fi, fsr)
296 struct frame_info *fi;
297 struct frame_saved_regs *fsr;
301 unsigned short op1, op2;
305 memset (fsr, 0, sizeof (*fsr));
308 pc = get_pc_function_start (fi->pc);
313 op = (unsigned long)read_memory_integer (pc, 4);
314 if ((op & 0xC0000000) == 0xC0000000)
316 /* long instruction */
317 if ((op & 0x3FFF0000) == 0x01FF0000)
320 short n = op & 0xFFFF;
323 else if ((op & 0x3F0F0000) == 0x340F0000)
325 /* st rn, @(offset,sp) */
326 short offset = op & 0xFFFF;
327 short n = (op >> 20) & 0xF;
328 fsr->regs[n] = next_addr + offset;
330 else if ((op & 0x3F1F0000) == 0x350F0000)
332 /* st2w rn, @(offset,sp) */
333 short offset = op & 0xFFFF;
334 short n = (op >> 20) & 0xF;
335 fsr->regs[n] = next_addr + offset;
336 fsr->regs[n+1] = next_addr + offset + 2;
343 /* short instructions */
344 if ((op & 0xC0000000) == 0x80000000)
346 op2 = (op & 0x3FFF8000) >> 15;
351 op1 = (op & 0x3FFF8000) >> 15;
354 if (!prologue_find_regs(op1,fsr,pc) || !prologue_find_regs(op2,fsr,pc))
360 fi->size = -next_addr;
363 fp = read_register(SP_REGNUM) | DMEM_START;
365 for (i=0; i<NUM_REGS-1; i++)
368 fsr->regs[i] = fp - (next_addr - fsr->regs[i]);
371 if (fsr->regs[LR_REGNUM])
372 fi->return_pc = ((read_memory_unsigned_integer(fsr->regs[LR_REGNUM],2) - 1) << 2) | IMEM_START;
374 fi->return_pc = ((read_register(LR_REGNUM) - 1) << 2) | IMEM_START;
376 /* th SP is not normally (ever?) saved, but check anyway */
377 if (!fsr->regs[SP_REGNUM])
379 /* if the FP was saved, that means the current FP is valid, */
380 /* otherwise, it isn't being used, so we use the SP instead */
382 fsr->regs[SP_REGNUM] = read_register(FP_REGNUM) + fi->size;
385 fsr->regs[SP_REGNUM] = fp + fi->size;
387 fsr->regs[FP_REGNUM] = 0;
393 d10v_init_extra_frame_info (fromleaf, fi)
395 struct frame_info *fi;
397 struct frame_saved_regs dummy;
399 if (fi->next && ((fi->pc & 0xffff) == 0))
400 fi->pc = fi->next->return_pc;
402 d10v_frame_find_saved_regs (fi, &dummy);
406 show_regs (args, from_tty)
410 long long num1, num2;
411 printf_filtered ("PC=%04x (0x%x) PSW=%04x RPT_S=%04x RPT_E=%04x RPT_C=%04x\n",
412 read_register (PC_REGNUM), (read_register (PC_REGNUM) << 2) + IMEM_START,
413 read_register (PSW_REGNUM),
417 printf_filtered ("R0-R7 %04x %04x %04x %04x %04x %04x %04x %04x\n",
426 printf_filtered ("R8-R15 %04x %04x %04x %04x %04x %04x %04x %04x\n",
435 printf_filtered ("IMAP0 %04x IMAP1 %04x DMAP %04x\n",
436 read_register (IMAP0_REGNUM),
437 read_register (IMAP1_REGNUM),
438 read_register (DMAP_REGNUM));
439 read_register_gen (A0_REGNUM, (char *)&num1);
440 read_register_gen (A0_REGNUM+1, (char *)&num2);
441 printf_filtered ("A0-A1 %010llx %010llx\n",num1, num2);
445 d10v_xlate_addr (addr)
451 imap = (int)read_register(IMAP0_REGNUM);
453 imap = (int)read_register(IMAP1_REGNUM);
456 return (CORE_ADDR)(addr + 0x1000000);
457 return (CORE_ADDR)(addr + (imap & 0xff)*0x20000);
465 int save_pid, retval;
467 save_pid = inferior_pid;
469 retval = (int)read_register (PC_REGNUM);
470 inferior_pid = save_pid;
471 return d10v_xlate_addr(retval << 2);
475 d10v_write_pc (val, pid)
481 save_pid = inferior_pid;
483 write_register (PC_REGNUM, (val & 0x3ffff) >> 2);
484 inferior_pid = save_pid;
490 return (read_register(SP_REGNUM) | DMEM_START);
497 write_register (SP_REGNUM, (LONGEST)(val & 0xffff));
501 d10v_fix_call_dummy (dummyname, start_sp, fun, nargs, args, type, gcc_p)
512 char buffer[MAX_REGISTER_RAW_SIZE];
513 struct frame_info *frame = get_current_frame ();
514 frame->dummy = start_sp;
515 start_sp |= DMEM_START;
518 for (regnum = 0; regnum < NUM_REGS; regnum++)
520 sp -= REGISTER_RAW_SIZE(regnum);
521 store_address (buffer, REGISTER_RAW_SIZE(regnum), read_register(regnum));
522 write_memory (sp, buffer, REGISTER_RAW_SIZE(regnum));
524 write_register (SP_REGNUM, (LONGEST)(sp & 0xffff));
525 /* now we need to load LR with the return address */
526 write_register (LR_REGNUM, (LONGEST)(d10v_call_dummy_address() & 0xffff) >> 2);
531 d10v_pop_dummy_frame (fi)
532 struct frame_info *fi;
534 CORE_ADDR sp = fi->dummy;
537 for (regnum = 0; regnum < NUM_REGS; regnum++)
539 sp -= REGISTER_RAW_SIZE(regnum);
540 write_register(regnum, read_memory_unsigned_integer (sp, REGISTER_RAW_SIZE(regnum)));
542 flush_cached_frames (); /* needed? */
547 d10v_push_arguments (nargs, args, sp, struct_return, struct_addr)
552 CORE_ADDR struct_addr;
554 int i, len, index=0, regnum=2;
555 char buffer[4], *contents;
559 /* Pass 1. Put all large args on stack */
560 for (i = 0; i < nargs; i++)
562 value_ptr arg = args[i];
563 struct type *arg_type = check_typedef (VALUE_TYPE (arg));
564 len = TYPE_LENGTH (arg_type);
565 contents = VALUE_CONTENTS(arg);
566 val = extract_signed_integer (contents, len);
569 /* put on stack and pass pointers */
571 write_memory (sp, contents, len);
578 for (i = 0; i < nargs; i++)
580 value_ptr arg = args[i];
581 struct type *arg_type = check_typedef (VALUE_TYPE (arg));
582 len = TYPE_LENGTH (arg_type);
583 contents = VALUE_CONTENTS(arg);
584 val = extract_signed_integer (contents, len);
587 /* use a pointer to previously saved data */
589 write_register (regnum++, ptrs[index++]);
592 /* no more registers available. put it on the stack */
594 store_address (buffer, 2, ptrs[index++]);
595 write_memory (sp, buffer, 2);
603 write_register (regnum++, val>>16);
604 write_register (regnum++, val & 0xffff);
609 store_address (buffer, len, val);
610 write_memory (sp, buffer, len);
618 /* pick an out-of-the-way place to set the return value */
619 /* for an inferior function call. The link register is set to this */
620 /* value and a momentary breakpoint is set there. When the breakpoint */
621 /* is hit, the dummy frame is popped and the previous environment is */
625 d10v_call_dummy_address ()
628 struct minimal_symbol *sym;
630 entry = entry_point_address ();
635 sym = lookup_minimal_symbol ("_start", NULL, symfile_objfile);
637 if (!sym || MSYMBOL_TYPE (sym) != mst_text)
640 return SYMBOL_VALUE_ADDRESS (sym);
643 /* Given a return value in `regbuf' with a type `valtype',
644 extract and copy its value into `valbuf'. */
647 d10v_extract_return_value (valtype, regbuf, valbuf)
648 struct type *valtype;
649 char regbuf[REGISTER_BYTES];
652 memcpy (valbuf, regbuf + REGISTER_BYTE (2), TYPE_LENGTH (valtype));
655 /* The following code implements access to, and display of, the D10V's
656 instruction trace buffer. The buffer consists of 64K or more
657 4-byte words of data, of which each words includes an 8-bit count,
658 an 8-bit segment number, and a 16-bit instruction address.
660 In theory, the trace buffer is continuously capturing instruction
661 data that the CPU presents on its "debug bus", but in practice, the
662 ROMified GDB stub only enables tracing when it continues or steps
663 the program, and stops tracing when the program stops; so it
664 actually works for GDB to read the buffer counter out of memory and
665 then read each trace word. The counter records where the tracing
666 stops, but there is no record of where it started, so we remember
667 the PC when we resumed and then search backwards in the trace
668 buffer for a word that includes that address. This is not perfect,
669 because you will miss trace data if the resumption PC is the target
670 of a branch. (The value of the buffer counter is semi-random, any
671 trace data from a previous program stop is gone.) */
673 /* The address of the last word recorded in the trace buffer. */
675 #define DBBC_ADDR (0xd80000)
677 /* The base of the trace buffer, at least for the "Board_0". */
679 #define TRACE_BUFFER_BASE (0xf40000)
681 static void trace_command PARAMS ((char *, int));
683 static void untrace_command PARAMS ((char *, int));
685 static void trace_info PARAMS ((char *, int));
687 static void tdisassemble_command PARAMS ((char *, int));
689 static void display_trace PARAMS ((int, int));
691 /* True when instruction traces are being collected. */
697 static CORE_ADDR last_pc;
699 /* True when trace output should be displayed whenever program stops. */
701 static int trace_display;
703 /* True when trace listing should include source lines. */
705 static int default_trace_show_source = 1;
707 struct trace_buffer {
714 trace_command (args, from_tty)
718 /* Clear the host-side trace buffer, allocating space if needed. */
720 if (trace_data.counts == NULL)
721 trace_data.counts = (short *) xmalloc (65536 * sizeof(short));
722 if (trace_data.addrs == NULL)
723 trace_data.addrs = (CORE_ADDR *) xmalloc (65536 * sizeof(CORE_ADDR));
727 printf_filtered ("Tracing is now on.\n");
731 untrace_command (args, from_tty)
737 printf_filtered ("Tracing is now off.\n");
741 trace_info (args, from_tty)
749 printf_filtered ("%d entries in trace buffer:\n", trace_data.size);
751 for (i = 0; i < trace_data.size; ++i)
753 printf_filtered ("%d: %d instruction%s at 0x%x\n",
754 i, trace_data.counts[i],
755 (trace_data.counts[i] == 1 ? "" : "s"),
756 trace_data.addrs[i]);
760 printf_filtered ("No entries in trace buffer.\n");
762 printf_filtered ("Tracing is currently %s.\n", (tracing ? "on" : "off"));
765 /* Print the instruction at address MEMADDR in debugged memory,
766 on STREAM. Returns length of the instruction, in bytes. */
769 print_insn (memaddr, stream)
773 /* If there's no disassembler, something is very wrong. */
774 if (tm_print_insn == NULL)
777 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
778 tm_print_insn_info.endian = BFD_ENDIAN_BIG;
780 tm_print_insn_info.endian = BFD_ENDIAN_LITTLE;
781 return (*tm_print_insn) (memaddr, &tm_print_insn_info);
785 d10v_eva_prepare_to_trace ()
790 last_pc = read_register (PC_REGNUM);
793 /* Collect trace data from the target board and format it into a form
794 more useful for display. */
797 d10v_eva_get_trace_data ()
799 int count, i, j, oldsize;
800 int trace_addr, trace_seg, trace_cnt, next_cnt;
801 unsigned int last_trace, trace_word, next_word;
802 unsigned int *tmpspace;
807 tmpspace = xmalloc (65536 * sizeof(unsigned int));
809 last_trace = read_memory_unsigned_integer (DBBC_ADDR, 2) << 2;
811 /* Collect buffer contents from the target, stopping when we reach
812 the word recorded when execution resumed. */
815 while (last_trace > 0)
819 read_memory_unsigned_integer (TRACE_BUFFER_BASE + last_trace, 4);
820 trace_addr = trace_word & 0xffff;
822 /* Ignore an apparently nonsensical entry. */
823 if (trace_addr == 0xffd5)
825 tmpspace[count++] = trace_word;
826 if (trace_addr == last_pc)
832 /* Move the data to the host-side trace buffer, adjusting counts to
833 include the last instruction executed and transforming the address
834 into something that GDB likes. */
836 for (i = 0; i < count; ++i)
838 trace_word = tmpspace[i];
839 next_word = ((i == 0) ? 0 : tmpspace[i - 1]);
840 trace_addr = trace_word & 0xffff;
841 next_cnt = (next_word >> 24) & 0xff;
842 j = trace_data.size + count - i - 1;
843 trace_data.addrs[j] = (trace_addr << 2) + 0x1000000;
844 trace_data.counts[j] = next_cnt + 1;
847 oldsize = trace_data.size;
848 trace_data.size += count;
853 display_trace (oldsize, trace_data.size);
857 tdisassemble_command (arg, from_tty)
868 high = trace_data.size;
870 else if (!(space_index = (char *) strchr (arg, ' ')))
872 low = parse_and_eval_address (arg);
879 low = parse_and_eval_address (arg);
880 high = parse_and_eval_address (space_index + 1);
885 printf_filtered ("Dump of trace from %d to %d:\n", low, high);
887 display_trace (low, high);
889 printf_filtered ("End of trace dump.\n");
890 gdb_flush (gdb_stdout);
894 display_trace (low, high)
897 int i, count, trace_show_source, first, suppress;
898 CORE_ADDR next_address;
900 trace_show_source = default_trace_show_source;
901 if (!have_full_symbols () && !have_partial_symbols())
903 trace_show_source = 0;
904 printf_filtered ("No symbol table is loaded. Use the \"file\" command.\n");
905 printf_filtered ("Trace will not display any source.\n");
910 for (i = low; i < high; ++i)
912 next_address = trace_data.addrs[i];
913 count = trace_data.counts[i];
917 if (trace_show_source)
919 struct symtab_and_line sal, sal_prev;
921 sal_prev = find_pc_line (next_address - 4, 0);
922 sal = find_pc_line (next_address, 0);
926 if (first || sal.line != sal_prev.line)
927 print_source_lines (sal.symtab, sal.line, sal.line + 1, 0);
933 /* FIXME-32x64--assumes sal.pc fits in long. */
934 printf_filtered ("No source file for address %s.\n",
935 local_hex_string((unsigned long) sal.pc));
940 print_address (next_address, gdb_stdout);
941 printf_filtered (":");
942 printf_filtered ("\t");
944 next_address = next_address + print_insn (next_address, gdb_stdout);
945 printf_filtered ("\n");
946 gdb_flush (gdb_stdout);
951 extern void (*target_resume_hook) PARAMS ((void));
952 extern void (*target_wait_loop_hook) PARAMS ((void));
955 _initialize_d10v_tdep ()
957 tm_print_insn = print_insn_d10v;
959 target_resume_hook = d10v_eva_prepare_to_trace;
960 target_wait_loop_hook = d10v_eva_get_trace_data;
962 add_com ("regs", class_vars, show_regs, "Print all registers");
964 add_com ("trace", class_support, trace_command,
965 "Enable tracing of instruction execution.");
967 add_com ("untrace", class_support, untrace_command,
968 "Disable tracing of instruction execution.");
970 add_com ("tdisassemble", class_vars, tdisassemble_command,
971 "Disassemble the trace buffer.\n\
972 Two optional arguments specify a range of trace buffer entries\n\
973 as reported by info trace (NOT addresses!).");
975 add_info ("trace", trace_info,
976 "Display info about the trace data buffer.");
978 add_show_from_set (add_set_cmd ("tracedisplay", no_class,
979 var_integer, (char *)&trace_display,
980 "Set automatic display of trace.\n", &setlist),
982 add_show_from_set (add_set_cmd ("tracesource", no_class,
983 var_integer, (char *)&default_trace_show_source,
984 "Set display of source code with trace.\n", &setlist),