Extend sim-trace.[hc] to include a generic set of macro's for tracing
[external/binutils.git] / sim / common / sim-trace.h
1 /* Simulator tracing/debugging support.
2    Copyright (C) 1997 Free Software Foundation, Inc.
3    Contributed by Cygnus Support.
4
5 This file is part of GDB, the GNU debugger.
6
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 2, or (at your option)
10 any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 /* This file is meant to be included by sim-basics.h.  */
22
23 #ifndef SIM_TRACE_H
24 #define SIM_TRACE_H
25
26 /* Standard traceable entities.  */
27
28 enum {
29   /* Trace insn execution.  */
30   TRACE_INSN_IDX = 1,
31
32   /* Trace insn decoding.
33      ??? This is more of a simulator debugging operation and might best be
34      moved to --debug-decode.  */
35   TRACE_DECODE_IDX,
36
37   /* Trace insn extraction.
38      ??? This is more of a simulator debugging operation and might best be
39      moved to --debug-extract.  */
40   TRACE_EXTRACT_IDX,
41
42   /* Trace insn execution but include line numbers.  */
43   TRACE_LINENUM_IDX,
44
45   /* Trace memory operations.
46      The difference between this and TRACE_CORE_IDX is (I think) that this
47      is intended to apply to a higher level.  TRACE_CORE_IDX applies to the
48      low level core operations.  */
49   TRACE_MEMORY_IDX,
50
51   /* Include model performance data in tracing output.  */
52   TRACE_MODEL_IDX,
53
54   /* Trace ALU operations.  */
55   TRACE_ALU_IDX,
56
57   /* Trace memory core operations.  */
58   TRACE_CORE_IDX,
59
60   /* Trace events.  */
61   TRACE_EVENTS_IDX,
62
63   /* Trace fpu operations.  */
64   TRACE_FPU_IDX,
65
66   /* Trace branching.  */
67   TRACE_BRANCH_IDX,
68
69   /* Add information useful for debugging the simulator to trace output.  */
70   TRACE_DEBUG_IDX,
71
72   /* Simulator specific trace bits begin here.  */
73   TRACE_NEXT_IDX,
74
75 };
76 /* Maximum number of traceable entities.  */
77 #ifndef MAX_TRACE_VALUES
78 #define MAX_TRACE_VALUES 32
79 #endif
80 \f
81 /* Masks so WITH_TRACE can have symbolic values.
82    The case choice here is on purpose.  The lowercase parts are args to
83    --with-trace.  */
84 #define TRACE_insn     (1 << TRACE_INSN_IDX)
85 #define TRACE_decode   (1 << TRACE_DECODE_IDX)
86 #define TRACE_extract  (1 << TRACE_EXTRACT_IDX)
87 #define TRACE_linenum  (1 << TRACE_LINENUM_IDX)
88 #define TRACE_memory   (1 << TRACE_MEMORY_IDX)
89 #define TRACE_model    (1 << TRACE_MODEL_IDX)
90 #define TRACE_alu      (1 << TRACE_ALU_IDX)
91 #define TRACE_core     (1 << TRACE_CORE_IDX)
92 #define TRACE_events   (1 << TRACE_EVENTS_IDX)
93 #define TRACE_fpu      (1 << TRACE_FPU_IDX)
94 #define TRACE_branch   (1 << TRACE_BRANCH_IDX)
95 #define TRACE_debug    (1 << TRACE_DEBUG_IDX)
96
97 /* Preprocessor macros to simplify tests of WITH_TRACE.  */
98 #define WITH_TRACE_INSN_P       (WITH_TRACE & TRACE_insn)
99 #define WITH_TRACE_DECODE_P     (WITH_TRACE & TRACE_decode)
100 #define WITH_TRACE_EXTRACT_P    (WITH_TRACE & TRACE_extract)
101 #define WITH_TRACE_LINENUM_P    (WITH_TRACE & TRACE_linenum)
102 #define WITH_TRACE_MEMORY_P     (WITH_TRACE & TRACE_memory)
103 #define WITH_TRACE_MODEL_P      (WITH_TRACE & TRACE_model)
104 #define WITH_TRACE_ALU_P        (WITH_TRACE & TRACE_alu)
105 #define WITH_TRACE_CORE_P       (WITH_TRACE & TRACE_core)
106 #define WITH_TRACE_EVENTS_P     (WITH_TRACE & TRACE_events)
107 #define WITH_TRACE_FPU_P        (WITH_TRACE & TRACE_fpu)
108 #define WITH_TRACE_BRANCH_P     (WITH_TRACE & TRACE_branch)
109 #define WITH_TRACE_DEBUG_P      (WITH_TRACE & TRACE_debug)
110
111 /* Tracing install handler.  */
112 MODULE_INSTALL_FN trace_install;
113 \f
114 /* Struct containing all system and cpu trace data.
115
116    System trace data is stored with the associated module.
117    System and cpu tracing must share the same space of bitmasks as they
118    are arguments to --with-trace.  One could have --with-trace and
119    --with-cpu-trace or some such but that's an over-complication at this point
120    in time.  Also, there may be occasions where system and cpu tracing may
121    wish to share a name.  */
122
123 typedef struct _trace_data {
124
125   /* Boolean array of specified tracing flags.  */
126   /* ??? It's not clear that using an array vs a bit mask is faster.
127      Consider the case where one wants to test whether any of several bits
128      are set.  */
129   char trace_flags[MAX_TRACE_VALUES];
130 #define TRACE_FLAGS(t) ((t)->trace_flags)
131
132   /* Tracing output goes to this or stderr if NULL.
133      We can't store `stderr' here as stderr goes through a callback.  */
134   FILE *trace_file;
135 #define TRACE_FILE(t) ((t)->trace_file)
136
137   /* Buffer to store the prefix to be printed before any trace line */
138   char trace_prefix[256];
139 #define TRACE_PREFIX(t) ((t)->trace_prefix)
140
141   /* Buffer to save the inputs for the current instruction.  Use a
142      union to force the buffer into correct alignment */
143   union {
144     unsigned8 i8;
145     unsigned16 i16;
146     unsigned32 i32;
147     unsigned64 i64;
148   } trace_input_data[16];
149   unsigned8 trace_input_fmt[16];
150   unsigned8 trace_input_size[16];
151   int trace_input_idx;
152 #define TRACE_INPUT_DATA(t) ((t)->trace_input_data)
153 #define TRACE_INPUT_FMT(t) ((t)->trace_input_fmt)
154 #define TRACE_INPUT_SIZE(t) ((t)->trace_input_size)
155 #define TRACE_INPUT_IDX(t) ((t)->trace_input_idx)
156   
157   /* Category of trace being performed */
158   int trace_idx;
159 #define TRACE_IDX(t) ((t)->trace_idx)
160   
161 } TRACE_DATA;
162
163 \f
164 /* System tracing support.  */
165
166 #define STATE_TRACE_FLAGS(sd) TRACE_FLAGS (STATE_TRACE_DATA (sd))
167
168 /* Return non-zero if tracing of IDX is enabled for non-cpu specific
169    components.  The "S" in "STRACE" refers to "System".  */
170 #define STRACE_P(sd,idx) \
171 ((WITH_TRACE & (1 << (idx))) != 0 \
172  && STATE_TRACE_FLAGS (sd)[idx] != 0)
173
174 /* Non-zero if --trace-<xxxx> was specified for SD.  */
175 #define STRACE_DEBUG_P(sd)      STRACE_P (sd, TRACE_DEBUG_IDX)
176 \f
177 /* CPU tracing support.  */
178
179 #define CPU_TRACE_FLAGS(cpu) TRACE_FLAGS (CPU_TRACE_DATA (cpu))
180
181 /* Return non-zero if tracing of IDX is enabled for CPU.  */
182 #define TRACE_P(cpu,idx) \
183 ((WITH_TRACE & (1 << (idx))) != 0 \
184  && CPU_TRACE_FLAGS (cpu)[idx] != 0)
185
186 /* Non-zero if --trace-<xxxx> was specified for CPU.  */
187 #define TRACE_INSN_P(cpu)       TRACE_P (cpu, TRACE_INSN_IDX)
188 #define TRACE_DECODE_P(cpu)     TRACE_P (cpu, TRACE_DECODE_IDX)
189 #define TRACE_EXTRACT_P(cpu)    TRACE_P (cpu, TRACE_EXTRACT_IDX)
190 #define TRACE_LINENUM_P(cpu)    TRACE_P (cpu, TRACE_LINENUM_IDX)
191 #define TRACE_MEMORY_P(cpu)     TRACE_P (cpu, TRACE_MEMORY_IDX)
192 #define TRACE_MODEL_P(cpu)      TRACE_P (cpu, TRACE_MODEL_IDX)
193 #define TRACE_ALU_P(cpu)        TRACE_P (cpu, TRACE_ALU_IDX)
194 #define TRACE_CORE_P(cpu)       TRACE_P (cpu, TRACE_CORE_IDX)
195 #define TRACE_EVENTS_P(cpu)     TRACE_P (cpu, TRACE_EVENTS_IDX)
196 #define TRACE_FPU_P(cpu)        TRACE_P (cpu, TRACE_FPU_IDX)
197 #define TRACE_BRANCH_P(cpu)     TRACE_P (cpu, TRACE_BRANCH_IDX)
198 #define TRACE_DEBUG_P(cpu)      TRACE_P (cpu, TRACE_DEBUG_IDX)
199 \f
200 /* Traceing functions.
201
202  */
203
204 /* Prime the trace buffers ready for any trace output.
205    Must be called prior to any other trace operation */
206 extern void trace_prefix PARAMS ((SIM_DESC sd,
207                                   sim_cpu * cpu,
208                                   address_word cia,
209                                   int print_linenum_p,
210                                   const char *file_name,
211                                   int line_nr,
212                                   const char *fmt,
213                                   ...))
214        __attribute__((format (printf, 7, 8)));
215
216 /* Generic trace print, assumes trace_prefix() has been called */
217
218 extern void trace_generic PARAMS ((SIM_DESC sd,
219                                    sim_cpu *cpu,
220                                    int trace_idx,
221                                    char *fmt,
222                                    ...))
223      __attribute__((format (printf, 4, 5)));
224
225 /* Trace a varying number of word sized inputs/outputs.  trace_result*
226    must be called to close the trace operation. */
227
228 extern void trace_input0 PARAMS ((SIM_DESC sd,
229                                   sim_cpu *cpu,
230                                   int trace_idx));
231 extern void trace_input_word1 PARAMS ((SIM_DESC sd,
232                                        sim_cpu *cpu,
233                                        int trace_idx,
234                                        unsigned_word d0));
235 extern void trace_input_word2 PARAMS ((SIM_DESC sd,
236                                        sim_cpu *cpu,
237                                        int trace_idx,
238                                        unsigned_word d0,
239                                        unsigned_word d1));
240 extern void trace_input_word3 PARAMS ((SIM_DESC sd,
241                                        sim_cpu *cpu,
242                                        int trace_idx,
243                                        unsigned_word d0,
244                                        unsigned_word d1,
245                                        unsigned_word d2));
246
247 extern void trace_input_fp1 PARAMS ((SIM_DESC sd,
248                                      sim_cpu *cpu,
249                                      int trace_idx,
250                                      fp_word f0));
251
252 extern void trace_input_fp2 PARAMS ((SIM_DESC sd,
253                                      sim_cpu *cpu,
254                                      int trace_idx,
255                                      fp_word f0,
256                                      fp_word f1));
257
258 extern void trace_input_fp3 PARAMS ((SIM_DESC sd,
259                                      sim_cpu *cpu,
260                                      int trace_idx,
261                                      fp_word f0,
262                                      fp_word f1,
263                                      fp_word f2));
264
265 extern void trace_input_fpu1 PARAMS ((SIM_DESC sd,
266                                      sim_cpu *cpu,
267                                      int trace_idx,
268                                      struct _sim_fpu *f0));
269
270 extern void trace_input_fpu2 PARAMS ((SIM_DESC sd,
271                                      sim_cpu *cpu,
272                                      int trace_idx,
273                                      struct _sim_fpu *f0,
274                                      struct _sim_fpu *f1));
275
276 extern void trace_input_fpu3 PARAMS ((SIM_DESC sd,
277                                      sim_cpu *cpu,
278                                      int trace_idx,
279                                      struct _sim_fpu *f0,
280                                      struct _sim_fpu *f1,
281                                      struct _sim_fpu *f2));
282
283 /* Other trace_input{_<fmt><nr-inputs>} functions can go here */
284
285 extern void trace_result_word1 PARAMS ((SIM_DESC sd,
286                                         sim_cpu *cpu,
287                                         int trace_idx,
288                                         unsigned_word r0));
289
290 extern void trace_result_fp1 PARAMS ((SIM_DESC sd,
291                                       sim_cpu *cpu,
292                                       int trace_idx,
293                                       fp_word f0));
294
295 extern void trace_result_fpu1 PARAMS ((SIM_DESC sd,
296                                        sim_cpu *cpu,
297                                        int trace_idx,
298                                        struct _sim_fpu *f0));
299
300 extern void trace_result_string1 PARAMS ((SIM_DESC sd,
301                                           sim_cpu *cpu,
302                                           int trace_idx,
303                                           char *str0));
304
305 extern void trace_result_word1_string1 PARAMS ((SIM_DESC sd,
306                                                 sim_cpu *cpu,
307                                                 int trace_idx,
308                                                 unsigned_word r0,
309                                                 char *s0));
310
311 /* Other trace_result{_<type><nr-results>} */
312
313
314 /* Macro's for tracing ALU instructions */
315 #define TRACE_ALU_INPUT0() \
316 do { \
317   if (TRACE_ALU_P (CPU)) \
318     trace_input0 (SD, CPU, TRACE_ALU_IDX); \
319 } while (0)
320     
321 #define TRACE_ALU_INPUT1(V0) \
322 do { \
323   if (TRACE_ALU_P (CPU)) \
324     trace_input_word1 (SD, CPU, TRACE_ALU_IDX, (V0)); \
325 } while (0)
326
327 #define TRACE_ALU_INPUT2(V0,V1) \
328 do { \
329   if (TRACE_ALU_P (CPU)) \
330     trace_input_word2 (SD, CPU, TRACE_ALU_IDX, (V0), (V1)); \
331 } while (0)
332
333 #define TRACE_ALU_INPUT3(V0,V1,V2) \
334 do { \
335   if (TRACE_ALU_P (CPU)) \
336     trace_input_word3 (SD, CPU, TRACE_ALU_IDX, (V0), (V1), (V2)); \
337 } while (0)
338
339 #define TRACE_ALU_RESULT(R0) \
340 do { \
341   if (TRACE_ALU_P (CPU)) \
342     trace_result_word1 (SD, CPU, TRACE_ALU_IDX, (R0)); \
343 } while (0)
344
345 \f
346 /* The function trace_one_insn has been replaced by trace_generic */
347 extern void trace_one_insn PARAMS ((SIM_DESC sd,
348                                     sim_cpu * cpu,
349                                     address_word cia,
350                                     int print_linenum_p,
351                                     const char *file_name,
352                                     int line_nr,
353                                     const char *unit,
354                                     const char *fmt,
355                                     ...))
356      __attribute__((format (printf, 8, 9)));
357
358 extern void trace_printf PARAMS ((SIM_DESC, sim_cpu *, const char *, ...))
359      __attribute__((format (printf, 3, 4)));
360
361 extern void trace_vprintf PARAMS ((SIM_DESC, sim_cpu *, const char *, va_list));
362
363 /* Debug support.
364    This is included here because there isn't enough of it to justify
365    a sim-debug.h.  */
366
367 /* Return non-zero if debugging of IDX for CPU is enabled.  */
368 #define DEBUG_P(cpu, idx) \
369 ((WITH_DEBUG & (1 << (idx))) != 0 \
370  && CPU_DEBUG_FLAGS (cpu)[idx] != 0)
371
372 /* Non-zero if "--debug-insn" specified.  */
373 #define DEBUG_INSN_P(cpu) DEBUG_P (cpu, DEBUG_INSN_IDX)
374
375 extern void debug_printf PARAMS ((sim_cpu *, const char *, ...))
376      __attribute__((format (printf, 2, 3)));
377
378 #endif /* SIM_TRACE_H */