]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/d10v-tdep.c
import gdb-19990422 snapshot
[thirdparty/binutils-gdb.git] / gdb / d10v-tdep.c
CommitLineData
c906108c
SS
1/* Target-dependent code for Mitsubishi D10V, for GDB.
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3
4This file is part of GDB.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20/* Contributed by Martin Hunt, hunt@cygnus.com */
21
22#include "defs.h"
23#include "frame.h"
24#include "obstack.h"
25#include "symtab.h"
26#include "gdbtypes.h"
27#include "gdbcmd.h"
28#include "gdbcore.h"
29#include "gdb_string.h"
30#include "value.h"
31#include "inferior.h"
32#include "dis-asm.h"
33#include "symfile.h"
34#include "objfiles.h"
35
36void d10v_frame_find_saved_regs PARAMS ((struct frame_info *fi,
37 struct frame_saved_regs *fsr));
38
39int
40d10v_frame_chain_valid (chain, frame)
41 CORE_ADDR chain;
42 struct frame_info *frame; /* not used here */
43{
44 return ((chain) != 0 && (frame) != 0 && (frame)->pc > IMEM_START);
45}
46
47
48/* Should we use EXTRACT_STRUCT_VALUE_ADDRESS instead of
49 EXTRACT_RETURN_VALUE? GCC_P is true if compiled with gcc
50 and TYPE is the type (which is known to be struct, union or array).
51
52 The d10v returns anything less than 8 bytes in size in
53 registers. */
54
55int
56d10v_use_struct_convention (gcc_p, type)
57 int gcc_p;
58 struct type *type;
59{
60 return (TYPE_LENGTH (type) > 8);
61}
62
63
64/* Discard from the stack the innermost frame, restoring all saved
65 registers. */
66
67void
68d10v_pop_frame (frame)
69 struct frame_info *frame;
70{
71 CORE_ADDR fp;
72 int regnum;
73 struct frame_saved_regs fsr;
74 char raw_buffer[8];
75
76 fp = FRAME_FP (frame);
77 /* fill out fsr with the address of where each */
78 /* register was stored in the frame */
79 get_frame_saved_regs (frame, &fsr);
80
81 /* now update the current registers with the old values */
82 for (regnum = A0_REGNUM; regnum < A0_REGNUM+2 ; regnum++)
83 {
84 if (fsr.regs[regnum])
85 {
86 read_memory (fsr.regs[regnum], raw_buffer, REGISTER_RAW_SIZE(regnum));
87 write_register_bytes (REGISTER_BYTE (regnum), raw_buffer, REGISTER_RAW_SIZE(regnum));
88 }
89 }
90 for (regnum = 0; regnum < SP_REGNUM; regnum++)
91 {
92 if (fsr.regs[regnum])
93 {
94 write_register (regnum, read_memory_unsigned_integer (fsr.regs[regnum], REGISTER_RAW_SIZE(regnum)));
95 }
96 }
97 if (fsr.regs[PSW_REGNUM])
98 {
99 write_register (PSW_REGNUM, read_memory_unsigned_integer (fsr.regs[PSW_REGNUM], REGISTER_RAW_SIZE(PSW_REGNUM)));
100 }
101
102 write_register (PC_REGNUM, read_register (LR_REGNUM));
103 write_register (SP_REGNUM, fp + frame->size);
104 target_store_registers (-1);
105 flush_cached_frames ();
106}
107
108static int
109check_prologue (op)
110 unsigned short op;
111{
112 /* st rn, @-sp */
113 if ((op & 0x7E1F) == 0x6C1F)
114 return 1;
115
116 /* st2w rn, @-sp */
117 if ((op & 0x7E3F) == 0x6E1F)
118 return 1;
119
120 /* subi sp, n */
121 if ((op & 0x7FE1) == 0x01E1)
122 return 1;
123
124 /* mv r11, sp */
125 if (op == 0x417E)
126 return 1;
127
128 /* nop */
129 if (op == 0x5E00)
130 return 1;
131
132 /* st rn, @sp */
133 if ((op & 0x7E1F) == 0x681E)
134 return 1;
135
136 /* st2w rn, @sp */
137 if ((op & 0x7E3F) == 0x3A1E)
138 return 1;
139
140 return 0;
141}
142
143CORE_ADDR
144d10v_skip_prologue (pc)
145 CORE_ADDR pc;
146{
147 unsigned long op;
148 unsigned short op1, op2;
149 CORE_ADDR func_addr, func_end;
150 struct symtab_and_line sal;
151
152 /* If we have line debugging information, then the end of the */
153 /* prologue should the first assembly instruction of the first source line */
154 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
155 {
156 sal = find_pc_line (func_addr, 0);
157 if ( sal.end && sal.end < func_end)
158 return sal.end;
159 }
160
161 if (target_read_memory (pc, (char *)&op, 4))
162 return pc; /* Can't access it -- assume no prologue. */
163
164 while (1)
165 {
166 op = (unsigned long)read_memory_integer (pc, 4);
167 if ((op & 0xC0000000) == 0xC0000000)
168 {
169 /* long instruction */
170 if ( ((op & 0x3FFF0000) != 0x01FF0000) && /* add3 sp,sp,n */
171 ((op & 0x3F0F0000) != 0x340F0000) && /* st rn, @(offset,sp) */
172 ((op & 0x3F1F0000) != 0x350F0000)) /* st2w rn, @(offset,sp) */
173 break;
174 }
175 else
176 {
177 /* short instructions */
178 if ((op & 0xC0000000) == 0x80000000)
179 {
180 op2 = (op & 0x3FFF8000) >> 15;
181 op1 = op & 0x7FFF;
182 }
183 else
184 {
185 op1 = (op & 0x3FFF8000) >> 15;
186 op2 = op & 0x7FFF;
187 }
188 if (check_prologue(op1))
189 {
190 if (!check_prologue(op2))
191 {
192 /* if the previous opcode was really part of the prologue */
193 /* and not just a NOP, then we want to break after both instructions */
194 if (op1 != 0x5E00)
195 pc += 4;
196 break;
197 }
198 }
199 else
200 break;
201 }
202 pc += 4;
203 }
204 return pc;
205}
206
207/* Given a GDB frame, determine the address of the calling function's frame.
208 This will be used to create a new GDB frame struct, and then
209 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
210*/
211
212CORE_ADDR
213d10v_frame_chain (frame)
214 struct frame_info *frame;
215{
216 struct frame_saved_regs fsr;
217
218 d10v_frame_find_saved_regs (frame, &fsr);
219
220 if (frame->return_pc == IMEM_START || inside_entry_file(frame->return_pc))
221 return (CORE_ADDR)0;
222
223 if (!fsr.regs[FP_REGNUM])
224 {
225 if (!fsr.regs[SP_REGNUM] || fsr.regs[SP_REGNUM] == STACK_START)
226 return (CORE_ADDR)0;
227
228 return fsr.regs[SP_REGNUM];
229 }
230
231 if (!read_memory_unsigned_integer(fsr.regs[FP_REGNUM], REGISTER_RAW_SIZE(FP_REGNUM)))
232 return (CORE_ADDR)0;
233
234 return D10V_MAKE_DADDR (read_memory_unsigned_integer (fsr.regs[FP_REGNUM], REGISTER_RAW_SIZE (FP_REGNUM)));
235}
236
237static int next_addr, uses_frame;
238
239static int
240prologue_find_regs (op, fsr, addr)
241 unsigned short op;
242 struct frame_saved_regs *fsr;
243 CORE_ADDR addr;
244{
245 int n;
246
247 /* st rn, @-sp */
248 if ((op & 0x7E1F) == 0x6C1F)
249 {
250 n = (op & 0x1E0) >> 5;
251 next_addr -= 2;
252 fsr->regs[n] = next_addr;
253 return 1;
254 }
255
256 /* st2w rn, @-sp */
257 else if ((op & 0x7E3F) == 0x6E1F)
258 {
259 n = (op & 0x1E0) >> 5;
260 next_addr -= 4;
261 fsr->regs[n] = next_addr;
262 fsr->regs[n+1] = next_addr+2;
263 return 1;
264 }
265
266 /* subi sp, n */
267 if ((op & 0x7FE1) == 0x01E1)
268 {
269 n = (op & 0x1E) >> 1;
270 if (n == 0)
271 n = 16;
272 next_addr -= n;
273 return 1;
274 }
275
276 /* mv r11, sp */
277 if (op == 0x417E)
278 {
279 uses_frame = 1;
280 return 1;
281 }
282
283 /* nop */
284 if (op == 0x5E00)
285 return 1;
286
287 /* st rn, @sp */
288 if ((op & 0x7E1F) == 0x681E)
289 {
290 n = (op & 0x1E0) >> 5;
291 fsr->regs[n] = next_addr;
292 return 1;
293 }
294
295 /* st2w rn, @sp */
296 if ((op & 0x7E3F) == 0x3A1E)
297 {
298 n = (op & 0x1E0) >> 5;
299 fsr->regs[n] = next_addr;
300 fsr->regs[n+1] = next_addr+2;
301 return 1;
302 }
303
304 return 0;
305}
306
307/* Put here the code to store, into a struct frame_saved_regs, the
308 addresses of the saved registers of frame described by FRAME_INFO.
309 This includes special registers such as pc and fp saved in special
310 ways in the stack frame. sp is even more special: the address we
311 return for it IS the sp for the next frame. */
312void
313d10v_frame_find_saved_regs (fi, fsr)
314 struct frame_info *fi;
315 struct frame_saved_regs *fsr;
316{
317 CORE_ADDR fp, pc;
318 unsigned long op;
319 unsigned short op1, op2;
320 int i;
321
322 fp = fi->frame;
323 memset (fsr, 0, sizeof (*fsr));
324 next_addr = 0;
325
326 pc = get_pc_function_start (fi->pc);
327
328 uses_frame = 0;
329 while (1)
330 {
331 op = (unsigned long)read_memory_integer (pc, 4);
332 if ((op & 0xC0000000) == 0xC0000000)
333 {
334 /* long instruction */
335 if ((op & 0x3FFF0000) == 0x01FF0000)
336 {
337 /* add3 sp,sp,n */
338 short n = op & 0xFFFF;
339 next_addr += n;
340 }
341 else if ((op & 0x3F0F0000) == 0x340F0000)
342 {
343 /* st rn, @(offset,sp) */
344 short offset = op & 0xFFFF;
345 short n = (op >> 20) & 0xF;
346 fsr->regs[n] = next_addr + offset;
347 }
348 else if ((op & 0x3F1F0000) == 0x350F0000)
349 {
350 /* st2w rn, @(offset,sp) */
351 short offset = op & 0xFFFF;
352 short n = (op >> 20) & 0xF;
353 fsr->regs[n] = next_addr + offset;
354 fsr->regs[n+1] = next_addr + offset + 2;
355 }
356 else
357 break;
358 }
359 else
360 {
361 /* short instructions */
362 if ((op & 0xC0000000) == 0x80000000)
363 {
364 op2 = (op & 0x3FFF8000) >> 15;
365 op1 = op & 0x7FFF;
366 }
367 else
368 {
369 op1 = (op & 0x3FFF8000) >> 15;
370 op2 = op & 0x7FFF;
371 }
372 if (!prologue_find_regs(op1,fsr,pc) || !prologue_find_regs(op2,fsr,pc))
373 break;
374 }
375 pc += 4;
376 }
377
378 fi->size = -next_addr;
379
380 if (!(fp & 0xffff))
381 fp = D10V_MAKE_DADDR (read_register(SP_REGNUM));
382
383 for (i=0; i<NUM_REGS-1; i++)
384 if (fsr->regs[i])
385 {
386 fsr->regs[i] = fp - (next_addr - fsr->regs[i]);
387 }
388
389 if (fsr->regs[LR_REGNUM])
390 {
391 CORE_ADDR return_pc = read_memory_unsigned_integer (fsr->regs[LR_REGNUM], REGISTER_RAW_SIZE (LR_REGNUM));
392 fi->return_pc = D10V_MAKE_IADDR (return_pc);
393 }
394 else
395 {
396 fi->return_pc = D10V_MAKE_IADDR (read_register(LR_REGNUM));
397 }
398
399 /* th SP is not normally (ever?) saved, but check anyway */
400 if (!fsr->regs[SP_REGNUM])
401 {
402 /* if the FP was saved, that means the current FP is valid, */
403 /* otherwise, it isn't being used, so we use the SP instead */
404 if (uses_frame)
405 fsr->regs[SP_REGNUM] = read_register(FP_REGNUM) + fi->size;
406 else
407 {
408 fsr->regs[SP_REGNUM] = fp + fi->size;
409 fi->frameless = 1;
410 fsr->regs[FP_REGNUM] = 0;
411 }
412 }
413}
414
415void
416d10v_init_extra_frame_info (fromleaf, fi)
417 int fromleaf;
418 struct frame_info *fi;
419{
420 fi->frameless = 0;
421 fi->size = 0;
422 fi->return_pc = 0;
423
424 /* The call dummy doesn't save any registers on the stack, so we can
425 return now. */
426 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
427 {
428 return;
429 }
430 else
431 {
432 struct frame_saved_regs dummy;
433 d10v_frame_find_saved_regs (fi, &dummy);
434 }
435}
436
437static void
438show_regs (args, from_tty)
439 char *args;
440 int from_tty;
441{
442 int a;
443 printf_filtered ("PC=%04x (0x%x) PSW=%04x RPT_S=%04x RPT_E=%04x RPT_C=%04x\n",
444 read_register (PC_REGNUM), D10V_MAKE_IADDR (read_register (PC_REGNUM)),
445 read_register (PSW_REGNUM),
446 read_register (24),
447 read_register (25),
448 read_register (23));
449 printf_filtered ("R0-R7 %04x %04x %04x %04x %04x %04x %04x %04x\n",
450 read_register (0),
451 read_register (1),
452 read_register (2),
453 read_register (3),
454 read_register (4),
455 read_register (5),
456 read_register (6),
457 read_register (7));
458 printf_filtered ("R8-R15 %04x %04x %04x %04x %04x %04x %04x %04x\n",
459 read_register (8),
460 read_register (9),
461 read_register (10),
462 read_register (11),
463 read_register (12),
464 read_register (13),
465 read_register (14),
466 read_register (15));
467 printf_filtered ("IMAP0 %04x IMAP1 %04x DMAP %04x\n",
468 read_register (IMAP0_REGNUM),
469 read_register (IMAP1_REGNUM),
470 read_register (DMAP_REGNUM));
471 printf_filtered ("A0-A1");
472 for (a = A0_REGNUM; a <= A0_REGNUM + 1; a++)
473 {
474 char num[MAX_REGISTER_RAW_SIZE];
475 int i;
476 printf_filtered (" ");
477 read_register_gen (a, (char *)&num);
478 for (i = 0; i < MAX_REGISTER_RAW_SIZE; i++)
479 {
480 printf_filtered ("%02x", (num[i] & 0xff));
481 }
482 }
483 printf_filtered ("\n");
484}
485
486CORE_ADDR
487d10v_read_pc (pid)
488 int pid;
489{
490 int save_pid;
491 CORE_ADDR pc;
492 CORE_ADDR retval;
493
494 save_pid = inferior_pid;
495 inferior_pid = pid;
496 pc = (int) read_register (PC_REGNUM);
497 inferior_pid = save_pid;
498 retval = D10V_MAKE_IADDR (pc);
499 return retval;
500}
501
502void
503d10v_write_pc (val, pid)
504 CORE_ADDR val;
505 int pid;
506{
507 int save_pid;
508
509 save_pid = inferior_pid;
510 inferior_pid = pid;
511 write_register (PC_REGNUM, D10V_CONVERT_IADDR_TO_RAW (val));
512 inferior_pid = save_pid;
513}
514
515CORE_ADDR
516d10v_read_sp ()
517{
518 return (D10V_MAKE_DADDR (read_register (SP_REGNUM)));
519}
520
521void
522d10v_write_sp (val)
523 CORE_ADDR val;
524{
525 write_register (SP_REGNUM, D10V_CONVERT_DADDR_TO_RAW (val));
526}
527
528void
529d10v_write_fp (val)
530 CORE_ADDR val;
531{
532 write_register (FP_REGNUM, D10V_CONVERT_DADDR_TO_RAW (val));
533}
534
535CORE_ADDR
536d10v_read_fp ()
537{
538 return (D10V_MAKE_DADDR (read_register(FP_REGNUM)));
539}
540
541/* Function: push_return_address (pc)
542 Set up the return address for the inferior function call.
543 Needed for targets where we don't actually execute a JSR/BSR instruction */
544
545CORE_ADDR
546d10v_push_return_address (pc, sp)
547 CORE_ADDR pc;
548 CORE_ADDR sp;
549{
550 write_register (LR_REGNUM, D10V_CONVERT_IADDR_TO_RAW (CALL_DUMMY_ADDRESS ()));
551 return sp;
552}
553
554
7a292a7a
SS
555/* When arguments must be pushed onto the stack, they go on in reverse
556 order. The below implements a FILO (stack) to do this. */
557
558struct stack_item
559{
560 int len;
561 struct stack_item *prev;
562 void *data;
563};
564
565static struct stack_item *push_stack_item PARAMS ((struct stack_item *prev, void *contents, int len));
566static struct stack_item *
567push_stack_item (prev, contents, len)
568 struct stack_item *prev;
569 void *contents;
570 int len;
571{
572 struct stack_item *si;
573 si = xmalloc (sizeof (struct stack_item));
574 si->data = xmalloc (len);
575 si->len = len;
576 si->prev = prev;
577 memcpy (si->data, contents, len);
578 return si;
579}
580
581static struct stack_item *pop_stack_item PARAMS ((struct stack_item *si));
582static struct stack_item *
583pop_stack_item (si)
584 struct stack_item *si;
585{
586 struct stack_item *dead = si;
587 si = si->prev;
588 free (dead->data);
589 free (dead);
590 return si;
591}
592
593
c906108c
SS
594CORE_ADDR
595d10v_push_arguments (nargs, args, sp, struct_return, struct_addr)
596 int nargs;
597 value_ptr *args;
598 CORE_ADDR sp;
599 int struct_return;
600 CORE_ADDR struct_addr;
601{
602 int i;
603 int regnum = ARG1_REGNUM;
7a292a7a 604 struct stack_item *si = NULL;
c906108c
SS
605
606 /* Fill in registers and arg lists */
607 for (i = 0; i < nargs; i++)
608 {
609 value_ptr arg = args[i];
610 struct type *type = check_typedef (VALUE_TYPE (arg));
611 char *contents = VALUE_CONTENTS (arg);
612 int len = TYPE_LENGTH (type);
613 /* printf ("push: type=%d len=%d\n", type->code, len); */
614 if (TYPE_CODE (type) == TYPE_CODE_PTR)
615 {
616 /* pointers require special handling - first convert and
617 then store */
618 long val = extract_signed_integer (contents, len);
619 len = 2;
620 if (TYPE_TARGET_TYPE (type)
621 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC))
622 {
623 /* function pointer */
624 val = D10V_CONVERT_IADDR_TO_RAW (val);
625 }
626 else if (D10V_IADDR_P (val))
627 {
628 /* also function pointer! */
629 val = D10V_CONVERT_DADDR_TO_RAW (val);
630 }
631 else
632 {
633 /* data pointer */
634 val &= 0xFFFF;
635 }
636 if (regnum <= ARGN_REGNUM)
637 write_register (regnum++, val & 0xffff);
638 else
639 {
640 char ptr[2];
7a292a7a 641 /* arg will go onto stack */
c906108c 642 store_address (ptr, val & 0xffff, 2);
7a292a7a 643 si = push_stack_item (si, ptr, 2);
c906108c
SS
644 }
645 }
646 else
647 {
648 int aligned_regnum = (regnum + 1) & ~1;
649 if (len <= 2 && regnum <= ARGN_REGNUM)
650 /* fits in a single register, do not align */
651 {
652 long val = extract_unsigned_integer (contents, len);
653 write_register (regnum++, val);
654 }
655 else if (len <= (ARGN_REGNUM - aligned_regnum + 1) * 2)
656 /* value fits in remaining registers, store keeping left
657 aligned */
658 {
659 int b;
660 regnum = aligned_regnum;
661 for (b = 0; b < (len & ~1); b += 2)
662 {
663 long val = extract_unsigned_integer (&contents[b], 2);
664 write_register (regnum++, val);
665 }
666 if (b < len)
667 {
668 long val = extract_unsigned_integer (&contents[b], 1);
669 write_register (regnum++, (val << 8));
670 }
671 }
672 else
673 {
7a292a7a
SS
674 /* arg will go onto stack */
675 regnum = ARGN_REGNUM + 1;
676 si = push_stack_item (si, contents, len);
c906108c
SS
677 }
678 }
679 }
7a292a7a
SS
680
681 while (si)
682 {
683 sp = (sp - si->len) & ~1;
684 write_memory (sp, si->data, si->len);
685 si = pop_stack_item (si);
686 }
687
c906108c
SS
688 return sp;
689}
690
691
692/* Given a return value in `regbuf' with a type `valtype',
693 extract and copy its value into `valbuf'. */
694
695void
696d10v_extract_return_value (type, regbuf, valbuf)
697 struct type *type;
698 char regbuf[REGISTER_BYTES];
699 char *valbuf;
700{
701 int len;
702 /* printf("RET: TYPE=%d len=%d r%d=0x%x\n",type->code, TYPE_LENGTH (type), RET1_REGNUM - R0_REGNUM, (int) extract_unsigned_integer (regbuf + REGISTER_BYTE(RET1_REGNUM), REGISTER_RAW_SIZE (RET1_REGNUM))); */
703 if (TYPE_CODE (type) == TYPE_CODE_PTR
704 && TYPE_TARGET_TYPE (type)
705 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC))
706 {
707 /* pointer to function */
708 int num;
709 short snum;
710 snum = extract_address (regbuf + REGISTER_BYTE (RET1_REGNUM), REGISTER_RAW_SIZE (RET1_REGNUM));
711 store_address ( valbuf, 4, D10V_MAKE_IADDR(snum));
712 }
713 else if (TYPE_CODE(type) == TYPE_CODE_PTR)
714 {
715 /* pointer to data */
716 int num;
717 short snum;
718 snum = extract_address (regbuf + REGISTER_BYTE (RET1_REGNUM), REGISTER_RAW_SIZE (RET1_REGNUM));
719 store_address ( valbuf, 4, D10V_MAKE_DADDR(snum));
720 }
721 else
722 {
723 len = TYPE_LENGTH (type);
724 if (len == 1)
725 {
726 unsigned short c = extract_unsigned_integer (regbuf + REGISTER_BYTE (RET1_REGNUM), REGISTER_RAW_SIZE (RET1_REGNUM));
727 store_unsigned_integer (valbuf, 1, c);
728 }
729 else if ((len & 1) == 0)
730 memcpy (valbuf, regbuf + REGISTER_BYTE (RET1_REGNUM), len);
731 else
732 {
733 /* For return values of odd size, the first byte is in the
734 least significant part of the first register. The
735 remaining bytes in remaining registers. Interestingly,
736 when such values are passed in, the last byte is in the
737 most significant byte of that same register - wierd. */
738 memcpy (valbuf, regbuf + REGISTER_BYTE (RET1_REGNUM) + 1, len);
739 }
740 }
741}
742
743/* The following code implements access to, and display of, the D10V's
744 instruction trace buffer. The buffer consists of 64K or more
745 4-byte words of data, of which each words includes an 8-bit count,
746 an 8-bit segment number, and a 16-bit instruction address.
747
748 In theory, the trace buffer is continuously capturing instruction
749 data that the CPU presents on its "debug bus", but in practice, the
750 ROMified GDB stub only enables tracing when it continues or steps
751 the program, and stops tracing when the program stops; so it
752 actually works for GDB to read the buffer counter out of memory and
753 then read each trace word. The counter records where the tracing
754 stops, but there is no record of where it started, so we remember
755 the PC when we resumed and then search backwards in the trace
756 buffer for a word that includes that address. This is not perfect,
757 because you will miss trace data if the resumption PC is the target
758 of a branch. (The value of the buffer counter is semi-random, any
759 trace data from a previous program stop is gone.) */
760
761/* The address of the last word recorded in the trace buffer. */
762
763#define DBBC_ADDR (0xd80000)
764
765/* The base of the trace buffer, at least for the "Board_0". */
766
767#define TRACE_BUFFER_BASE (0xf40000)
768
769static void trace_command PARAMS ((char *, int));
770
771static void untrace_command PARAMS ((char *, int));
772
773static void trace_info PARAMS ((char *, int));
774
775static void tdisassemble_command PARAMS ((char *, int));
776
777static void display_trace PARAMS ((int, int));
778
779/* True when instruction traces are being collected. */
780
781static int tracing;
782
783/* Remembered PC. */
784
785static CORE_ADDR last_pc;
786
787/* True when trace output should be displayed whenever program stops. */
788
789static int trace_display;
790
791/* True when trace listing should include source lines. */
792
793static int default_trace_show_source = 1;
794
795struct trace_buffer {
796 int size;
797 short *counts;
798 CORE_ADDR *addrs;
799} trace_data;
800
801static void
802trace_command (args, from_tty)
803 char *args;
804 int from_tty;
805{
806 /* Clear the host-side trace buffer, allocating space if needed. */
807 trace_data.size = 0;
808 if (trace_data.counts == NULL)
809 trace_data.counts = (short *) xmalloc (65536 * sizeof(short));
810 if (trace_data.addrs == NULL)
811 trace_data.addrs = (CORE_ADDR *) xmalloc (65536 * sizeof(CORE_ADDR));
812
813 tracing = 1;
814
815 printf_filtered ("Tracing is now on.\n");
816}
817
818static void
819untrace_command (args, from_tty)
820 char *args;
821 int from_tty;
822{
823 tracing = 0;
824
825 printf_filtered ("Tracing is now off.\n");
826}
827
828static void
829trace_info (args, from_tty)
830 char *args;
831 int from_tty;
832{
833 int i;
834
835 if (trace_data.size)
836 {
837 printf_filtered ("%d entries in trace buffer:\n", trace_data.size);
838
839 for (i = 0; i < trace_data.size; ++i)
840 {
841 printf_filtered ("%d: %d instruction%s at 0x%x\n",
842 i, trace_data.counts[i],
843 (trace_data.counts[i] == 1 ? "" : "s"),
844 trace_data.addrs[i]);
845 }
846 }
847 else
848 printf_filtered ("No entries in trace buffer.\n");
849
850 printf_filtered ("Tracing is currently %s.\n", (tracing ? "on" : "off"));
851}
852
853/* Print the instruction at address MEMADDR in debugged memory,
854 on STREAM. Returns length of the instruction, in bytes. */
855
856static int
857print_insn (memaddr, stream)
858 CORE_ADDR memaddr;
859 GDB_FILE *stream;
860{
861 /* If there's no disassembler, something is very wrong. */
862 if (tm_print_insn == NULL)
863 abort ();
864
865 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
866 tm_print_insn_info.endian = BFD_ENDIAN_BIG;
867 else
868 tm_print_insn_info.endian = BFD_ENDIAN_LITTLE;
869 return (*tm_print_insn) (memaddr, &tm_print_insn_info);
870}
871
872void
873d10v_eva_prepare_to_trace ()
874{
875 if (!tracing)
876 return;
877
878 last_pc = read_register (PC_REGNUM);
879}
880
881/* Collect trace data from the target board and format it into a form
882 more useful for display. */
883
884void
885d10v_eva_get_trace_data ()
886{
887 int count, i, j, oldsize;
888 int trace_addr, trace_seg, trace_cnt, next_cnt;
889 unsigned int last_trace, trace_word, next_word;
890 unsigned int *tmpspace;
891
892 if (!tracing)
893 return;
894
895 tmpspace = xmalloc (65536 * sizeof(unsigned int));
896
897 last_trace = read_memory_unsigned_integer (DBBC_ADDR, 2) << 2;
898
899 /* Collect buffer contents from the target, stopping when we reach
900 the word recorded when execution resumed. */
901
902 count = 0;
903 while (last_trace > 0)
904 {
905 QUIT;
906 trace_word =
907 read_memory_unsigned_integer (TRACE_BUFFER_BASE + last_trace, 4);
908 trace_addr = trace_word & 0xffff;
909 last_trace -= 4;
910 /* Ignore an apparently nonsensical entry. */
911 if (trace_addr == 0xffd5)
912 continue;
913 tmpspace[count++] = trace_word;
914 if (trace_addr == last_pc)
915 break;
916 if (count > 65535)
917 break;
918 }
919
920 /* Move the data to the host-side trace buffer, adjusting counts to
921 include the last instruction executed and transforming the address
922 into something that GDB likes. */
923
924 for (i = 0; i < count; ++i)
925 {
926 trace_word = tmpspace[i];
927 next_word = ((i == 0) ? 0 : tmpspace[i - 1]);
928 trace_addr = trace_word & 0xffff;
929 next_cnt = (next_word >> 24) & 0xff;
930 j = trace_data.size + count - i - 1;
931 trace_data.addrs[j] = (trace_addr << 2) + 0x1000000;
932 trace_data.counts[j] = next_cnt + 1;
933 }
934
935 oldsize = trace_data.size;
936 trace_data.size += count;
937
938 free (tmpspace);
939
940 if (trace_display)
941 display_trace (oldsize, trace_data.size);
942}
943
944static void
945tdisassemble_command (arg, from_tty)
946 char *arg;
947 int from_tty;
948{
949 int i, count;
950 CORE_ADDR low, high;
951 char *space_index;
952
953 if (!arg)
954 {
955 low = 0;
956 high = trace_data.size;
957 }
958 else if (!(space_index = (char *) strchr (arg, ' ')))
959 {
960 low = parse_and_eval_address (arg);
961 high = low + 5;
962 }
963 else
964 {
965 /* Two arguments. */
966 *space_index = '\0';
967 low = parse_and_eval_address (arg);
968 high = parse_and_eval_address (space_index + 1);
969 if (high < low)
970 high = low;
971 }
972
973 printf_filtered ("Dump of trace from %d to %d:\n", low, high);
974
975 display_trace (low, high);
976
977 printf_filtered ("End of trace dump.\n");
978 gdb_flush (gdb_stdout);
979}
980
981static void
982display_trace (low, high)
983 int low, high;
984{
985 int i, count, trace_show_source, first, suppress;
986 CORE_ADDR next_address;
987
988 trace_show_source = default_trace_show_source;
989 if (!have_full_symbols () && !have_partial_symbols())
990 {
991 trace_show_source = 0;
992 printf_filtered ("No symbol table is loaded. Use the \"file\" command.\n");
993 printf_filtered ("Trace will not display any source.\n");
994 }
995
996 first = 1;
997 suppress = 0;
998 for (i = low; i < high; ++i)
999 {
1000 next_address = trace_data.addrs[i];
1001 count = trace_data.counts[i];
1002 while (count-- > 0)
1003 {
1004 QUIT;
1005 if (trace_show_source)
1006 {
1007 struct symtab_and_line sal, sal_prev;
1008
1009 sal_prev = find_pc_line (next_address - 4, 0);
1010 sal = find_pc_line (next_address, 0);
1011
1012 if (sal.symtab)
1013 {
1014 if (first || sal.line != sal_prev.line)
1015 print_source_lines (sal.symtab, sal.line, sal.line + 1, 0);
1016 suppress = 0;
1017 }
1018 else
1019 {
1020 if (!suppress)
1021 /* FIXME-32x64--assumes sal.pc fits in long. */
1022 printf_filtered ("No source file for address %s.\n",
1023 local_hex_string((unsigned long) sal.pc));
1024 suppress = 1;
1025 }
1026 }
1027 first = 0;
1028 print_address (next_address, gdb_stdout);
1029 printf_filtered (":");
1030 printf_filtered ("\t");
1031 wrap_here (" ");
1032 next_address = next_address + print_insn (next_address, gdb_stdout);
1033 printf_filtered ("\n");
1034 gdb_flush (gdb_stdout);
1035 }
1036 }
1037}
1038
1039extern void (*target_resume_hook) PARAMS ((void));
1040extern void (*target_wait_loop_hook) PARAMS ((void));
1041
1042void
1043_initialize_d10v_tdep ()
1044{
1045 tm_print_insn = print_insn_d10v;
1046
1047 target_resume_hook = d10v_eva_prepare_to_trace;
1048 target_wait_loop_hook = d10v_eva_get_trace_data;
1049
1050 add_com ("regs", class_vars, show_regs, "Print all registers");
1051
1052 add_com ("trace", class_support, trace_command,
1053 "Enable tracing of instruction execution.");
1054
1055 add_com ("untrace", class_support, untrace_command,
1056 "Disable tracing of instruction execution.");
1057
1058 add_com ("tdisassemble", class_vars, tdisassemble_command,
1059 "Disassemble the trace buffer.\n\
1060Two optional arguments specify a range of trace buffer entries\n\
1061as reported by info trace (NOT addresses!).");
1062
1063 add_info ("trace", trace_info,
1064 "Display info about the trace data buffer.");
1065
1066 add_show_from_set (add_set_cmd ("tracedisplay", no_class,
1067 var_integer, (char *)&trace_display,
1068 "Set automatic display of trace.\n", &setlist),
1069 &showlist);
1070 add_show_from_set (add_set_cmd ("tracesource", no_class,
1071 var_integer, (char *)&default_trace_show_source,
1072 "Set display of source code with trace.\n", &setlist),
1073 &showlist);
1074
1075}