Switch the license of all files explicitly copyright the FSF
[external/binutils.git] / sim / common / sim-trace.h
1 /* Simulator tracing/debugging support.
2    Copyright (C) 1997, 1998, 2001, 2007 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 3 of the License, or
10 (at your option) 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
18 along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 /* This file is meant to be included by sim-basics.h.  */
21
22 #ifndef SIM_TRACE_H
23 #define SIM_TRACE_H
24
25 /* Standard traceable entities.  */
26
27 enum {
28   /* Trace insn execution.  */
29   TRACE_INSN_IDX = 1,
30
31   /* Trace insn decoding.
32      ??? This is more of a simulator debugging operation and might best be
33      moved to --debug-decode.  */
34   TRACE_DECODE_IDX,
35
36   /* Trace insn extraction.
37      ??? This is more of a simulator debugging operation and might best be
38      moved to --debug-extract.  */
39   TRACE_EXTRACT_IDX,
40
41   /* Trace insn execution but include line numbers.  */
42   TRACE_LINENUM_IDX,
43
44   /* Trace memory operations.
45      The difference between this and TRACE_CORE_IDX is (I think) that this
46      is intended to apply to a higher level.  TRACE_CORE_IDX applies to the
47      low level core operations.  */
48   TRACE_MEMORY_IDX,
49
50   /* Include model performance data in tracing output.  */
51   TRACE_MODEL_IDX,
52
53   /* Trace ALU operations.  */
54   TRACE_ALU_IDX,
55
56   /* Trace memory core operations.  */
57   TRACE_CORE_IDX,
58
59   /* Trace events.  */
60   TRACE_EVENTS_IDX,
61
62   /* Trace fpu operations.  */
63   TRACE_FPU_IDX,
64
65   /* Trace vpu operations.  */
66   TRACE_VPU_IDX,
67
68   /* Trace branching.  */
69   TRACE_BRANCH_IDX,
70
71   /* Add information useful for debugging the simulator to trace output.  */
72   TRACE_DEBUG_IDX,
73
74   /* Simulator specific trace bits begin here.  */
75   TRACE_NEXT_IDX,
76
77 };
78 /* Maximum number of traceable entities.  */
79 #ifndef MAX_TRACE_VALUES
80 #define MAX_TRACE_VALUES 32
81 #endif
82
83 /* The -t option only prints useful values.  It's easy to type and shouldn't
84    splat on the screen everything under the sun making nothing easy to
85    find.  */
86 #define TRACE_USEFUL_MASK \
87 ((1 << TRACE_INSN_IDX) \
88  | (1 << TRACE_LINENUM_IDX) \
89  | (1 << TRACE_MEMORY_IDX) \
90  | (1 << TRACE_MODEL_IDX))
91 \f
92 /* Masks so WITH_TRACE can have symbolic values.
93    The case choice here is on purpose.  The lowercase parts are args to
94    --with-trace.  */
95 #define TRACE_insn     (1 << TRACE_INSN_IDX)
96 #define TRACE_decode   (1 << TRACE_DECODE_IDX)
97 #define TRACE_extract  (1 << TRACE_EXTRACT_IDX)
98 #define TRACE_linenum  (1 << TRACE_LINENUM_IDX)
99 #define TRACE_memory   (1 << TRACE_MEMORY_IDX)
100 #define TRACE_model    (1 << TRACE_MODEL_IDX)
101 #define TRACE_alu      (1 << TRACE_ALU_IDX)
102 #define TRACE_core     (1 << TRACE_CORE_IDX)
103 #define TRACE_events   (1 << TRACE_EVENTS_IDX)
104 #define TRACE_fpu      (1 << TRACE_FPU_IDX)
105 #define TRACE_vpu      (1 << TRACE_VPU_IDX)
106 #define TRACE_branch   (1 << TRACE_BRANCH_IDX)
107 #define TRACE_debug    (1 << TRACE_DEBUG_IDX)
108
109 /* Preprocessor macros to simplify tests of WITH_TRACE.  */
110 #define WITH_TRACE_INSN_P       (WITH_TRACE & TRACE_insn)
111 #define WITH_TRACE_DECODE_P     (WITH_TRACE & TRACE_decode)
112 #define WITH_TRACE_EXTRACT_P    (WITH_TRACE & TRACE_extract)
113 #define WITH_TRACE_LINENUM_P    (WITH_TRACE & TRACE_linenum)
114 #define WITH_TRACE_MEMORY_P     (WITH_TRACE & TRACE_memory)
115 #define WITH_TRACE_MODEL_P      (WITH_TRACE & TRACE_model)
116 #define WITH_TRACE_ALU_P        (WITH_TRACE & TRACE_alu)
117 #define WITH_TRACE_CORE_P       (WITH_TRACE & TRACE_core)
118 #define WITH_TRACE_EVENTS_P     (WITH_TRACE & TRACE_events)
119 #define WITH_TRACE_FPU_P        (WITH_TRACE & TRACE_fpu)
120 #define WITH_TRACE_VPU_P        (WITH_TRACE & TRACE_vpu)
121 #define WITH_TRACE_BRANCH_P     (WITH_TRACE & TRACE_branch)
122 #define WITH_TRACE_DEBUG_P      (WITH_TRACE & TRACE_debug)
123
124 /* Tracing install handler.  */
125 MODULE_INSTALL_FN trace_install;
126 \f
127 /* Struct containing all system and cpu trace data.
128
129    System trace data is stored with the associated module.
130    System and cpu tracing must share the same space of bitmasks as they
131    are arguments to --with-trace.  One could have --with-trace and
132    --with-cpu-trace or some such but that's an over-complication at this point
133    in time.  Also, there may be occasions where system and cpu tracing may
134    wish to share a name.  */
135
136 typedef struct _trace_data {
137
138   /* Global summary of all the current trace options */
139   char trace_any_p;
140
141   /* Boolean array of specified tracing flags.  */
142   /* ??? It's not clear that using an array vs a bit mask is faster.
143      Consider the case where one wants to test whether any of several bits
144      are set.  */
145   char trace_flags[MAX_TRACE_VALUES];
146 #define TRACE_FLAGS(t) ((t)->trace_flags)
147
148   /* Tracing output goes to this or stderr if NULL.
149      We can't store `stderr' here as stderr goes through a callback.  */
150   FILE *trace_file;
151 #define TRACE_FILE(t) ((t)->trace_file)
152
153   /* Buffer to store the prefix to be printed before any trace line.  */
154   char trace_prefix[256];
155 #define TRACE_PREFIX(t) ((t)->trace_prefix)
156
157   /* Buffer to save the inputs for the current instruction.  Use a
158      union to force the buffer into correct alignment */
159   union {
160     unsigned8 i8;
161     unsigned16 i16;
162     unsigned32 i32;
163     unsigned64 i64;
164   } trace_input_data[16];
165   unsigned8 trace_input_fmt[16];
166   unsigned8 trace_input_size[16];
167   int trace_input_idx;
168 #define TRACE_INPUT_DATA(t) ((t)->trace_input_data)
169 #define TRACE_INPUT_FMT(t) ((t)->trace_input_fmt)
170 #define TRACE_INPUT_SIZE(t) ((t)->trace_input_size)
171 #define TRACE_INPUT_IDX(t) ((t)->trace_input_idx)
172
173   /* Category of trace being performed */
174   int trace_idx;
175 #define TRACE_IDX(t) ((t)->trace_idx)
176
177   /* Trace range.
178      ??? Not all cpu's support this.  */
179   ADDR_RANGE range;
180 #define TRACE_RANGE(t) (& (t)->range)
181 } TRACE_DATA;
182 \f
183 /* System tracing support.  */
184
185 #define STATE_TRACE_FLAGS(sd) TRACE_FLAGS (STATE_TRACE_DATA (sd))
186
187 /* Return non-zero if tracing of IDX is enabled for non-cpu specific
188    components.  The "S" in "STRACE" refers to "System".  */
189 #define STRACE_P(sd,idx) \
190 ((WITH_TRACE & (1 << (idx))) != 0 \
191  && STATE_TRACE_FLAGS (sd)[idx] != 0)
192
193 /* Non-zero if --trace-<xxxx> was specified for SD.  */
194 #define STRACE_DEBUG_P(sd)      STRACE_P (sd, TRACE_DEBUG_IDX)
195 \f
196 /* CPU tracing support.  */
197
198 #define CPU_TRACE_FLAGS(cpu) TRACE_FLAGS (CPU_TRACE_DATA (cpu))
199
200 /* Return non-zero if tracing of IDX is enabled for CPU.  */
201 #define TRACE_P(cpu,idx) \
202 ((WITH_TRACE & (1 << (idx))) != 0 \
203  && CPU_TRACE_FLAGS (cpu)[idx] != 0)
204
205 /* Non-zero if --trace-<xxxx> was specified for CPU.  */
206 #define TRACE_ANY_P(cpu)        ((WITH_TRACE) && (CPU_TRACE_DATA (cpu)->trace_any_p))
207 #define TRACE_INSN_P(cpu)       TRACE_P (cpu, TRACE_INSN_IDX)
208 #define TRACE_DECODE_P(cpu)     TRACE_P (cpu, TRACE_DECODE_IDX)
209 #define TRACE_EXTRACT_P(cpu)    TRACE_P (cpu, TRACE_EXTRACT_IDX)
210 #define TRACE_LINENUM_P(cpu)    TRACE_P (cpu, TRACE_LINENUM_IDX)
211 #define TRACE_MEMORY_P(cpu)     TRACE_P (cpu, TRACE_MEMORY_IDX)
212 #define TRACE_MODEL_P(cpu)      TRACE_P (cpu, TRACE_MODEL_IDX)
213 #define TRACE_ALU_P(cpu)        TRACE_P (cpu, TRACE_ALU_IDX)
214 #define TRACE_CORE_P(cpu)       TRACE_P (cpu, TRACE_CORE_IDX)
215 #define TRACE_EVENTS_P(cpu)     TRACE_P (cpu, TRACE_EVENTS_IDX)
216 #define TRACE_FPU_P(cpu)        TRACE_P (cpu, TRACE_FPU_IDX)
217 #define TRACE_VPU_P(cpu)        TRACE_P (cpu, TRACE_VPU_IDX)
218 #define TRACE_BRANCH_P(cpu)     TRACE_P (cpu, TRACE_BRANCH_IDX)
219 #define TRACE_DEBUG_P(cpu)      TRACE_P (cpu, TRACE_DEBUG_IDX)
220 \f
221 /* Tracing functions.  */
222
223 /* Prime the trace buffers ready for any trace output.
224    Must be called prior to any other trace operation */
225 extern void trace_prefix PARAMS ((SIM_DESC sd,
226                                   sim_cpu *cpu,
227                                   sim_cia cia,
228                                   address_word pc,
229                                   int print_linenum_p,
230                                   const char *file_name,
231                                   int line_nr,
232                                   const char *fmt,
233                                   ...))
234        __attribute__((format (printf, 8, 9)));
235
236 /* Generic trace print, assumes trace_prefix() has been called */
237
238 extern void trace_generic PARAMS ((SIM_DESC sd,
239                                    sim_cpu *cpu,
240                                    int trace_idx,
241                                    const char *fmt,
242                                    ...))
243      __attribute__((format (printf, 4, 5)));
244
245 /* Trace a varying number of word sized inputs/outputs.  trace_result*
246    must be called to close the trace operation. */
247
248 extern void trace_input0 PARAMS ((SIM_DESC sd,
249                                   sim_cpu *cpu,
250                                   int trace_idx));
251
252 extern void trace_input_word1 PARAMS ((SIM_DESC sd,
253                                        sim_cpu *cpu,
254                                        int trace_idx,
255                                        unsigned_word d0));
256
257 extern void trace_input_word2 PARAMS ((SIM_DESC sd,
258                                        sim_cpu *cpu,
259                                        int trace_idx,
260                                        unsigned_word d0,
261                                        unsigned_word d1));
262
263 extern void trace_input_word3 PARAMS ((SIM_DESC sd,
264                                        sim_cpu *cpu,
265                                        int trace_idx,
266                                        unsigned_word d0,
267                                        unsigned_word d1,
268                                        unsigned_word d2));
269
270 extern void trace_input_word4 PARAMS ((SIM_DESC sd,
271                                        sim_cpu *cpu,
272                                        int trace_idx,
273                                        unsigned_word d0,
274                                        unsigned_word d1,
275                                        unsigned_word d2,
276                                        unsigned_word d3));
277
278 extern void trace_input_addr1 PARAMS ((SIM_DESC sd,
279                                        sim_cpu *cpu,
280                                        int trace_idx,
281                                        address_word d0));
282
283 extern void trace_input_bool1 PARAMS ((SIM_DESC sd,
284                                        sim_cpu *cpu,
285                                        int trace_idx,
286                                        int d0));
287
288 extern void trace_input_fp1 PARAMS ((SIM_DESC sd,
289                                      sim_cpu *cpu,
290                                      int trace_idx,
291                                      fp_word f0));
292
293 extern void trace_input_fp2 PARAMS ((SIM_DESC sd,
294                                      sim_cpu *cpu,
295                                      int trace_idx,
296                                      fp_word f0,
297                                      fp_word f1));
298
299 extern void trace_input_fp3 PARAMS ((SIM_DESC sd,
300                                      sim_cpu *cpu,
301                                      int trace_idx,
302                                      fp_word f0,
303                                      fp_word f1,
304                                      fp_word f2));
305
306 extern void trace_input_fpu1 PARAMS ((SIM_DESC sd,
307                                      sim_cpu *cpu,
308                                      int trace_idx,
309                                      struct _sim_fpu *f0));
310
311 extern void trace_input_fpu2 PARAMS ((SIM_DESC sd,
312                                      sim_cpu *cpu,
313                                      int trace_idx,
314                                      struct _sim_fpu *f0,
315                                      struct _sim_fpu *f1));
316
317 extern void trace_input_fpu3 PARAMS ((SIM_DESC sd,
318                                      sim_cpu *cpu,
319                                      int trace_idx,
320                                      struct _sim_fpu *f0,
321                                      struct _sim_fpu *f1,
322                                      struct _sim_fpu *f2));
323
324 /* Other trace_input{_<fmt><nr-inputs>} functions can go here */
325
326 extern void trace_result0 PARAMS ((SIM_DESC sd,
327                                    sim_cpu *cpu,
328                                    int trace_idx));
329
330 extern void trace_result_word1 PARAMS ((SIM_DESC sd,
331                                         sim_cpu *cpu,
332                                         int trace_idx,
333                                         unsigned_word r0));
334
335 extern void trace_result_word2 PARAMS ((SIM_DESC sd,
336                                         sim_cpu *cpu,
337                                         int trace_idx,
338                                         unsigned_word r0,
339                                         unsigned_word r1));
340
341 extern void trace_result_word4 PARAMS ((SIM_DESC sd,
342                                         sim_cpu *cpu,
343                                         int trace_idx,
344                                         unsigned_word r0,
345                                         unsigned_word r1,
346                                         unsigned_word r2,
347                                         unsigned_word r3));
348
349 extern void trace_result_bool1 PARAMS ((SIM_DESC sd,
350                                         sim_cpu *cpu,
351                                         int trace_idx,
352                                         int r0));
353
354 extern void trace_result_addr1 PARAMS ((SIM_DESC sd,
355                                         sim_cpu *cpu,
356                                         int trace_idx,
357                                         address_word r0));
358
359 extern void trace_result_fp1 PARAMS ((SIM_DESC sd,
360                                       sim_cpu *cpu,
361                                       int trace_idx,
362                                       fp_word f0));
363
364 extern void trace_result_fp2 PARAMS ((SIM_DESC sd,
365                                       sim_cpu *cpu,
366                                       int trace_idx,
367                                       fp_word f0,
368                                       fp_word f1));
369
370 extern void trace_result_fpu1 PARAMS ((SIM_DESC sd,
371                                        sim_cpu *cpu,
372                                        int trace_idx,
373                                        struct _sim_fpu *f0));
374
375 extern void trace_result_string1 PARAMS ((SIM_DESC sd,
376                                           sim_cpu *cpu,
377                                           int trace_idx,
378                                           char *str0));
379
380 extern void trace_result_word1_string1 PARAMS ((SIM_DESC sd,
381                                                 sim_cpu *cpu,
382                                                 int trace_idx,
383                                                 unsigned_word r0,
384                                                 char *s0));
385
386 /* Other trace_result{_<type><nr-results>} */
387
388
389 /* Macros for tracing ALU instructions */
390
391 #define TRACE_ALU_INPUT0() \
392 do { \
393   if (TRACE_ALU_P (CPU)) \
394     trace_input0 (SD, CPU, TRACE_ALU_IDX); \
395 } while (0)
396     
397 #define TRACE_ALU_INPUT1(V0) \
398 do { \
399   if (TRACE_ALU_P (CPU)) \
400     trace_input_word1 (SD, CPU, TRACE_ALU_IDX, (V0)); \
401 } while (0)
402
403 #define TRACE_ALU_INPUT2(V0,V1) \
404 do { \
405   if (TRACE_ALU_P (CPU)) \
406     trace_input_word2 (SD, CPU, TRACE_ALU_IDX, (V0), (V1)); \
407 } while (0)
408
409 #define TRACE_ALU_INPUT3(V0,V1,V2) \
410 do { \
411   if (TRACE_ALU_P (CPU)) \
412     trace_input_word3 (SD, CPU, TRACE_ALU_IDX, (V0), (V1), (V2)); \
413 } while (0)
414
415 #define TRACE_ALU_INPUT4(V0,V1,V2,V3) \
416 do { \
417   if (TRACE_ALU_P (CPU)) \
418     trace_input_word4 (SD, CPU, TRACE_ALU_IDX, (V0), (V1), (V2), (V3)); \
419 } while (0)
420
421 #define TRACE_ALU_RESULT(R0) TRACE_ALU_RESULT1(R0)
422
423 #define TRACE_ALU_RESULT0() \
424 do { \
425   if (TRACE_ALU_P (CPU)) \
426     trace_result0 (SD, CPU, TRACE_ALU_IDX); \
427 } while (0)
428
429 #define TRACE_ALU_RESULT1(R0) \
430 do { \
431   if (TRACE_ALU_P (CPU)) \
432     trace_result_word1 (SD, CPU, TRACE_ALU_IDX, (R0)); \
433 } while (0)
434
435 #define TRACE_ALU_RESULT2(R0,R1) \
436 do { \
437   if (TRACE_ALU_P (CPU)) \
438     trace_result_word2 (SD, CPU, TRACE_ALU_IDX, (R0), (R1)); \
439 } while (0)
440
441 #define TRACE_ALU_RESULT4(R0,R1,R2,R3) \
442 do { \
443   if (TRACE_ALU_P (CPU)) \
444     trace_result_word4 (SD, CPU, TRACE_ALU_IDX, (R0), (R1), (R2), (R3)); \
445 } while (0)
446
447 /* Macros for tracing inputs to comparative branch instructions. */
448
449 #define TRACE_BRANCH_INPUT1(V0) \
450 do { \
451   if (TRACE_BRANCH_P (CPU)) \
452     trace_input_word1 (SD, CPU, TRACE_BRANCH_IDX, (V0)); \
453 } while (0)
454
455 #define TRACE_BRANCH_INPUT2(V0,V1) \
456 do { \
457   if (TRACE_BRANCH_P (CPU)) \
458     trace_input_word2 (SD, CPU, TRACE_BRANCH_IDX, (V0), (V1)); \
459 } while (0)
460
461 /* Macros for tracing FPU instructions */
462
463 #define TRACE_FP_INPUT0() \
464 do { \
465   if (TRACE_FPU_P (CPU)) \
466     trace_input0 (SD, CPU, TRACE_FPU_IDX); \
467 } while (0)
468     
469 #define TRACE_FP_INPUT1(V0) \
470 do { \
471   if (TRACE_FPU_P (CPU)) \
472     trace_input_fp1 (SD, CPU, TRACE_FPU_IDX, (V0)); \
473 } while (0)
474
475 #define TRACE_FP_INPUT2(V0,V1) \
476 do { \
477   if (TRACE_FPU_P (CPU)) \
478     trace_input_fp2 (SD, CPU, TRACE_FPU_IDX, (V0), (V1)); \
479 } while (0)
480
481 #define TRACE_FP_INPUT3(V0,V1,V2) \
482 do { \
483   if (TRACE_FPU_P (CPU)) \
484     trace_input_fp3 (SD, CPU, TRACE_FPU_IDX, (V0), (V1), (V2)); \
485 } while (0)
486
487 #define TRACE_FP_INPUT_WORD1(V0) \
488 do { \
489   if (TRACE_FPU_P (CPU)) \
490     trace_input_word1 (SD, CPU, TRACE_FPU_IDX, (V0)); \
491 } while (0)
492     
493 #define TRACE_FP_RESULT(R0) \
494 do { \
495   if (TRACE_FPU_P (CPU)) \
496     trace_result_fp1 (SD, CPU, TRACE_FPU_IDX, (R0)); \
497 } while (0)
498
499 #define TRACE_FP_RESULT2(R0,R1) \
500 do { \
501   if (TRACE_FPU_P (CPU)) \
502     trace_result_fp2 (SD, CPU, TRACE_FPU_IDX, (R0), (R1)); \
503 } while (0)
504
505 #define TRACE_FP_RESULT_BOOL(R0) \
506 do { \
507   if (TRACE_FPU_P (CPU)) \
508     trace_result_bool1 (SD, CPU, TRACE_FPU_IDX, (R0)); \
509 } while (0)
510
511 #define TRACE_FP_RESULT_WORD(R0) \
512 do { \
513   if (TRACE_FPU_P (CPU)) \
514     trace_result_word1 (SD, CPU, TRACE_FPU_IDX, (R0)); \
515 } while (0)
516
517
518 /* Macros for tracing branches */
519
520 #define TRACE_BRANCH_INPUT(COND) \
521 do { \
522   if (TRACE_BRANCH_P (CPU)) \
523     trace_input_bool1 (SD, CPU, TRACE_BRANCH_IDX, (COND)); \
524 } while (0)
525
526 #define TRACE_BRANCH_RESULT(DEST) \
527 do { \
528   if (TRACE_BRANCH_P (CPU)) \
529     trace_result_addr1 (SD, CPU, TRACE_BRANCH_IDX, (DEST)); \
530 } while (0)
531
532 \f
533 /* The function trace_one_insn has been replaced by the function pair
534    trace_prefix() + trace_generic() */
535 extern void trace_one_insn PARAMS ((SIM_DESC sd,
536                                     sim_cpu * cpu,
537                                     address_word cia,
538                                     int print_linenum_p,
539                                     const char *file_name,
540                                     int line_nr,
541                                     const char *unit,
542                                     const char *fmt,
543                                     ...))
544      __attribute__((format (printf, 8, 9)));
545
546 extern void trace_printf PARAMS ((SIM_DESC, sim_cpu *, const char *, ...))
547      __attribute__((format (printf, 3, 4)));
548
549 extern void trace_vprintf PARAMS ((SIM_DESC, sim_cpu *, const char *, va_list));
550
551 /* Debug support.
552    This is included here because there isn't enough of it to justify
553    a sim-debug.h.  */
554
555 /* Return non-zero if debugging of IDX for CPU is enabled.  */
556 #define DEBUG_P(cpu, idx) \
557 ((WITH_DEBUG & (1 << (idx))) != 0 \
558  && CPU_DEBUG_FLAGS (cpu)[idx] != 0)
559
560 /* Non-zero if "--debug-insn" specified.  */
561 #define DEBUG_INSN_P(cpu) DEBUG_P (cpu, DEBUG_INSN_IDX)
562
563 extern void debug_printf PARAMS ((sim_cpu *, const char *, ...))
564      __attribute__((format (printf, 2, 3)));
565
566 #endif /* SIM_TRACE_H */