1 /* Tracing support for CGEN-based simulators.
2 Copyright (C) 1996-2023 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
5 This file is part of GDB, the GNU debugger.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* This must come before any other includes. */
28 #include "diagnostics.h"
33 #include "sim/callback.h"
35 #ifndef SIZE_INSTRUCTION
36 #define SIZE_INSTRUCTION 16
40 #define SIZE_LOCATION 20
47 #ifndef SIZE_LINE_NUMBER
48 #define SIZE_LINE_NUMBER 4
51 #ifndef SIZE_CYCLE_COUNT
52 #define SIZE_CYCLE_COUNT 2
55 #ifndef SIZE_TOTAL_CYCLE_COUNT
56 #define SIZE_TOTAL_CYCLE_COUNT 9
59 #ifndef SIZE_TRACE_BUF
60 #define SIZE_TRACE_BUF 1024
63 /* Text is queued in TRACE_BUF because we want to output the insn's cycle
64 count first but that isn't known until after the insn has executed.
65 This also handles the queueing of trace results, TRACE_RESULT may be
66 called multiple times for one insn. */
67 static char trace_buf
[SIZE_TRACE_BUF
];
68 /* If NULL, output to stdout directly. */
71 /* Non-zero if this is the first insn in a set of parallel insns. */
72 static int first_insn_p
;
74 /* For communication between cgen_trace_insn and cgen_trace_result. */
75 static int printed_result_p
;
77 /* Insn and its extracted fields.
78 Set by cgen_trace_insn, used by cgen_trace_insn_fini.
79 ??? Move to SIM_CPU to support heterogeneous multi-cpu case. */
80 static const struct cgen_insn
*current_insn
;
81 static const struct argbuf
*current_abuf
;
84 cgen_trace_insn_init (SIM_CPU
*cpu
, int first_p
)
88 first_insn_p
= first_p
;
90 /* Set to NULL so cgen_trace_insn_fini can know if cgen_trace_insn was
97 cgen_trace_insn_fini (SIM_CPU
*cpu
, const struct argbuf
*abuf
, int last_p
)
99 SIM_DESC sd
= CPU_STATE (cpu
);
101 /* Was insn traced? It might not be if trace ranges are in effect. */
102 if (current_insn
== NULL
)
105 /* The first thing printed is current and total cycle counts. */
107 if (PROFILE_MODEL_P (cpu
)
108 && ARGBUF_PROFILE_P (current_abuf
))
110 unsigned long total
= PROFILE_MODEL_TOTAL_CYCLES (CPU_PROFILE_DATA (cpu
));
111 unsigned long this_insn
= PROFILE_MODEL_CUR_INSN_CYCLES (CPU_PROFILE_DATA (cpu
));
115 trace_printf (sd
, cpu
, "%-*ld %-*ld ",
116 SIZE_CYCLE_COUNT
, this_insn
,
117 SIZE_TOTAL_CYCLE_COUNT
, total
);
121 trace_printf (sd
, cpu
, "%-*ld %-*s ",
122 SIZE_CYCLE_COUNT
, this_insn
,
123 SIZE_TOTAL_CYCLE_COUNT
, "---");
127 /* Print the disassembled insn. */
129 trace_printf (sd
, cpu
, "%s", TRACE_PREFIX (CPU_TRACE_DATA (cpu
)));
132 /* Print insn results. */
134 const CGEN_OPINST
*opinst
= CGEN_INSN_OPERANDS (current_insn
);
139 int indices
[MAX_OPERAND_INSTANCES
];
141 /* Fetch the operands used by the insn. */
142 /* FIXME: Add fn ptr to CGEN_CPU_DESC. */
143 CGEN_SYM (get_insn_operands
) (CPU_CPU_DESC (cpu
), current_insn
,
144 0, CGEN_FIELDS_BITSIZE (&insn_fields
),
148 CGEN_OPINST_TYPE (opinst
) != CGEN_OPINST_END
;
151 if (CGEN_OPINST_TYPE (opinst
) == CGEN_OPINST_OUTPUT
)
152 cgen_trace_result (cpu
, current_insn
, opinst
, indices
[i
]);
158 /* Print anything else requested. */
161 trace_printf (sd
, cpu
, " %s\n", trace_buf
);
163 trace_printf (sd
, cpu
, "\n");
167 cgen_trace_insn (SIM_CPU
*cpu
, const struct cgen_insn
*opcode
,
168 const struct argbuf
*abuf
, IADDR pc
)
172 printed_result_p
= 0;
173 current_insn
= opcode
;
176 if (CGEN_INSN_VIRTUAL_P (opcode
))
178 trace_prefix (CPU_STATE (cpu
), cpu
, NULL_CIA
, pc
, 0,
179 NULL
, 0, "%s", CGEN_INSN_NAME (opcode
));
183 CPU_DISASSEMBLER (cpu
) (cpu
, opcode
, abuf
, pc
, disasm_buf
);
184 trace_prefix (CPU_STATE (cpu
), cpu
, NULL_CIA
, pc
, TRACE_LINENUM_P (cpu
),
187 first_insn_p
? " " : "|",
188 SIZE_INSTRUCTION
, disasm_buf
);
192 cgen_trace_extract (SIM_CPU
*cpu
, IADDR pc
, const char *name
, ...)
195 int printed_one_p
= 0;
198 va_start (args
, name
);
200 trace_printf (CPU_STATE (cpu
), cpu
, "Extract: 0x%.*lx: %s ",
201 SIZE_PC
, (unsigned long) pc
, name
);
206 fmt
= va_arg (args
, const char *);
211 trace_printf (CPU_STATE (cpu
), cpu
, ", ");
213 type
= va_arg (args
, int);
217 ival
= va_arg (args
, int);
219 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
220 trace_printf (CPU_STATE (cpu
), cpu
, fmt
, ival
);
230 trace_printf (CPU_STATE (cpu
), cpu
, "\n");
234 cgen_trace_result (SIM_CPU
*cpu
, const char *name
, int type
, ...)
238 va_start (args
, type
);
239 if (printed_result_p
)
240 cgen_trace_printf (cpu
, ", ");
246 cgen_trace_printf (cpu
, "%s <- 0x%x", name
, va_arg (args
, int));
253 /* this is separated from previous line for sunos cc */
254 di
= va_arg (args
, DI
);
255 sim_fpu_64to (&f
, di
);
257 cgen_trace_printf (cpu
, "%s <- ", name
);
258 sim_fpu_printn_fpu (&f
, (sim_fpu_print_func
*) cgen_trace_printf
, 4, cpu
);
264 /* this is separated from previous line for sunos cc */
265 di
= va_arg (args
, DI
);
266 cgen_trace_printf (cpu
, "%s <- 0x%x%08x", name
,
267 GETHIDI(di
), GETLODI (di
));
272 printed_result_p
= 1;
276 /* Print trace output to BUFPTR if active, otherwise print normally.
277 This is only for tracing semantic code. */
280 cgen_trace_printf (SIM_CPU
*cpu
, const char *fmt
, ...)
284 va_start (args
, fmt
);
288 if (TRACE_FILE (CPU_TRACE_DATA (cpu
)) == NULL
)
289 (* STATE_CALLBACK (CPU_STATE (cpu
))->evprintf_filtered
)
290 (STATE_CALLBACK (CPU_STATE (cpu
)), fmt
, args
);
292 vfprintf (TRACE_FILE (CPU_TRACE_DATA (cpu
)), fmt
, args
);
296 vsprintf (bufptr
, fmt
, args
);
297 bufptr
+= strlen (bufptr
);
298 /* ??? Need version of SIM_ASSERT that is always enabled. */
299 if (bufptr
- trace_buf
> SIZE_TRACE_BUF
)
306 /* Disassembly support. */
308 /* sprintf to a "stream" */
311 sim_disasm_sprintf (SFILE
*f
, const char *format
, ...)
316 va_start (args
, format
);
317 vsprintf (f
->current
, format
, args
);
318 f
->current
+= n
= strlen (f
->current
);
323 /* sprintf to a "stream" with styling. */
326 sim_disasm_styled_sprintf (SFILE
*f
, enum disassembler_style style
,
327 const char *format
, ...)
332 va_start (args
, format
);
333 vsprintf (f
->current
, format
, args
);
334 f
->current
+= n
= strlen (f
->current
);
339 /* Memory read support for an opcodes disassembler. */
342 sim_disasm_read_memory (bfd_vma memaddr
, bfd_byte
*myaddr
, unsigned int length
,
343 struct disassemble_info
*info
)
345 SIM_CPU
*cpu
= (SIM_CPU
*) info
->application_data
;
346 SIM_DESC sd
= CPU_STATE (cpu
);
347 unsigned length_read
;
349 length_read
= sim_core_read_buffer (sd
, cpu
, read_map
, myaddr
, memaddr
,
351 if (length_read
!= length
)
356 /* Memory error support for an opcodes disassembler. */
359 sim_disasm_perror_memory (int status
, bfd_vma memaddr
,
360 struct disassemble_info
*info
)
364 info
->fprintf_func (info
->stream
, "Unknown error %d.", status
);
366 /* Actually, address between memaddr and memaddr + len was
368 info
->fprintf_func (info
->stream
,
369 "Address 0x%" PRIx64
" is out of bounds.",
373 /* Disassemble using the CGEN opcode table.
374 ??? While executing an instruction, the insn has been decoded and all its
375 fields have been extracted. It is certainly possible to do the disassembly
376 with that data. This seems simpler, but maybe in the future the already
377 extracted fields will be used. */
380 sim_cgen_disassemble_insn (SIM_CPU
*cpu
, const CGEN_INSN
*insn
,
381 const ARGBUF
*abuf
, IADDR pc
, char *buf
)
384 unsigned int base_length
;
385 unsigned long insn_value
;
386 struct disassemble_info disasm_info
;
389 uint8_t bytes
[CGEN_MAX_INSN_SIZE
];
393 SIM_DESC sd
= CPU_STATE (cpu
);
394 CGEN_CPU_DESC cd
= CPU_CPU_DESC (cpu
);
395 CGEN_EXTRACT_INFO ex_info
;
396 CGEN_FIELDS
*fields
= alloca (CGEN_CPU_SIZEOF_FIELDS (cd
));
397 int insn_bit_length
= CGEN_INSN_BITSIZE (insn
);
398 int insn_length
= insn_bit_length
/ 8;
400 sfile
.buffer
= sfile
.current
= buf
;
401 INIT_DISASSEMBLE_INFO (disasm_info
, (FILE *) &sfile
,
402 (fprintf_ftype
) sim_disasm_sprintf
,
403 (fprintf_styled_ftype
) sim_disasm_styled_sprintf
);
405 (bfd_big_endian (STATE_PROG_BFD (sd
)) ? BFD_ENDIAN_BIG
406 : bfd_little_endian (STATE_PROG_BFD (sd
)) ? BFD_ENDIAN_LITTLE
407 : BFD_ENDIAN_UNKNOWN
);
409 length
= sim_core_read_buffer (sd
, cpu
, read_map
, &insn_buf
, pc
,
412 if (length
!= insn_length
)
414 sim_io_error (sd
, "unable to read address %" PRIxTA
, pc
);
417 /* If the entire insn will fit into an integer, then do it. Otherwise, just
418 use the bits of the base_insn. */
419 if (insn_bit_length
<= 32)
420 base_length
= insn_bit_length
;
422 base_length
= min (cd
->base_insn_bitsize
, insn_bit_length
);
425 case 0 : return; /* fake insn, typically "compile" (aka "invalid") */
426 case 8 : insn_value
= insn_buf
.bytes
[0]; break;
427 case 16 : insn_value
= T2H_2 (insn_buf
.shorts
[0]); break;
428 case 32 : insn_value
= T2H_4 (insn_buf
.words
[0]); break;
432 disasm_info
.buffer_vma
= pc
;
433 disasm_info
.buffer
= insn_buf
.bytes
;
434 disasm_info
.buffer_length
= length
;
436 ex_info
.dis_info
= &disasm_info
;
437 ex_info
.valid
= (1 << length
) - 1;
438 ex_info
.insn_bytes
= insn_buf
.bytes
;
440 length
= (*CGEN_EXTRACT_FN (cd
, insn
)) (cd
, insn
, &ex_info
, insn_value
, fields
, pc
);
441 /* Result of extract fn is in bits. */
442 /* ??? This assumes that each instruction has a fixed length (and thus
443 for insns with multiple versions of variable lengths they would each
444 have their own table entry). */
445 if (length
== insn_bit_length
)
447 (*CGEN_PRINT_FN (cd
, insn
)) (cd
, &disasm_info
, insn
, fields
, pc
, length
);
451 /* This shouldn't happen, but aborting is too drastic. */
452 strcpy (buf
, "***unknown***");