aboutsummaryrefslogtreecommitdiff
path: root/sim/common/sim-trace.c
blob: 3290ec36d344d8d7c0d34a520680ba421dbeb572 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
/* Simulator tracing/debugging support.
   Copyright (C) 1997, 1998, 2001, 2007, 2008 Free Software Foundation, Inc.
   Contributed by Cygnus Support.

This file is part of GDB, the GNU debugger.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include "sim-main.h"
#include "sim-io.h"
#include "sim-options.h"
#include "sim-fpu.h"

#include "bfd.h"
#include "libiberty.h"

#include "sim-assert.h"

#ifdef HAVE_STRING_H
#include <string.h>
#else
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

#ifndef SIZE_PHASE
#define SIZE_PHASE 8
#endif

#ifndef SIZE_LOCATION
#define SIZE_LOCATION 20
#endif

#ifndef SIZE_PC
#define SIZE_PC 6
#endif

#ifndef SIZE_LINE_NUMBER
#define SIZE_LINE_NUMBER 4
#endif

static MODULE_INIT_FN trace_init;
static MODULE_UNINSTALL_FN trace_uninstall;

static DECLARE_OPTION_HANDLER (trace_option_handler);

enum {
  OPTION_TRACE_INSN	= OPTION_START,
  OPTION_TRACE_DECODE,
  OPTION_TRACE_EXTRACT,
  OPTION_TRACE_LINENUM,
  OPTION_TRACE_MEMORY,
  OPTION_TRACE_MODEL,
  OPTION_TRACE_ALU,
  OPTION_TRACE_CORE,
  OPTION_TRACE_EVENTS,
  OPTION_TRACE_FPU,
  OPTION_TRACE_BRANCH,
  OPTION_TRACE_SEMANTICS,
  OPTION_TRACE_RANGE,
  OPTION_TRACE_FUNCTION,
  OPTION_TRACE_DEBUG,
  OPTION_TRACE_FILE,
  OPTION_TRACE_VPU
};

static const OPTION trace_options[] =
{
  /* This table is organized to group related instructions together.  */
  { {"trace", optional_argument, NULL, 't'},
      't', "on|off", "Trace useful things",
      trace_option_handler },
  { {"trace-insn", optional_argument, NULL, OPTION_TRACE_INSN},
      '\0', "on|off", "Perform instruction tracing",
      trace_option_handler },
  { {"trace-decode", optional_argument, NULL, OPTION_TRACE_DECODE},
      '\0', "on|off", "Trace instruction decoding",
      trace_option_handler },
  { {"trace-extract", optional_argument, NULL, OPTION_TRACE_EXTRACT},
      '\0', "on|off", "Trace instruction extraction",
      trace_option_handler },
  { {"trace-linenum", optional_argument, NULL, OPTION_TRACE_LINENUM},
      '\0', "on|off", "Perform line number tracing (implies --trace-insn)",
      trace_option_handler },
  { {"trace-memory", optional_argument, NULL, OPTION_TRACE_MEMORY},
      '\0', "on|off", "Trace memory operations",
      trace_option_handler },
  { {"trace-alu", optional_argument, NULL, OPTION_TRACE_ALU},
      '\0', "on|off", "Trace ALU operations",
      trace_option_handler },
  { {"trace-fpu", optional_argument, NULL, OPTION_TRACE_FPU},
      '\0', "on|off", "Trace FPU operations",
      trace_option_handler },
  { {"trace-vpu", optional_argument, NULL, OPTION_TRACE_VPU},
      '\0', "on|off", "Trace VPU operations",
      trace_option_handler },
  { {"trace-branch", optional_argument, NULL, OPTION_TRACE_BRANCH},
      '\0', "on|off", "Trace branching",
      trace_option_handler },
  { {"trace-semantics", optional_argument, NULL, OPTION_TRACE_SEMANTICS},
      '\0', "on|off", "Perform ALU, FPU, MEMORY, and BRANCH tracing",
      trace_option_handler },
  { {"trace-model", optional_argument, NULL, OPTION_TRACE_MODEL},
      '\0', "on|off", "Include model performance data",
      trace_option_handler },
  { {"trace-core", optional_argument, NULL, OPTION_TRACE_CORE},
      '\0', "on|off", "Trace core operations",
      trace_option_handler },
  { {"trace-events", optional_argument, NULL, OPTION_TRACE_EVENTS},
      '\0', "on|off", "Trace events",
      trace_option_handler },
#ifdef SIM_HAVE_ADDR_RANGE
  { {"trace-range", required_argument, NULL, OPTION_TRACE_RANGE},
      '\0', "START,END", "Specify range of addresses for instruction tracing",
      trace_option_handler },
#if 0 /*wip*/
  { {"trace-function", required_argument, NULL, OPTION_TRACE_FUNCTION},
      '\0', "FUNCTION", "Specify function to trace",
      trace_option_handler },
#endif
#endif
  { {"trace-debug", optional_argument, NULL, OPTION_TRACE_DEBUG},
      '\0', "on|off", "Add information useful for debugging the simulator to the tracing output",
      trace_option_handler },
  { {"trace-file", required_argument, NULL, OPTION_TRACE_FILE},
      '\0', "FILE NAME", "Specify tracing output file",
      trace_option_handler },
  { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
};

/* Set/reset the trace options indicated in MASK.  */

static SIM_RC
set_trace_option_mask (sd, name, mask, arg)
     SIM_DESC sd;
     const char *name;
     int mask;
     const char *arg;
{
  int trace_nr;
  int cpu_nr;
  int trace_val = 1;

  if (arg != NULL)
    {
      if (strcmp (arg, "yes") == 0
	  || strcmp (arg, "on") == 0
	  || strcmp (arg, "1") == 0)
	trace_val = 1;
      else if (strcmp (arg, "no") == 0
	       || strcmp (arg, "off") == 0
	       || strcmp (arg, "0") == 0)
	trace_val = 0;
      else
	{
	  sim_io_eprintf (sd, "Argument `%s' for `--trace%s' invalid, one of `on', `off', `yes', `no' expected\n", arg, name);
	  return SIM_RC_FAIL;
	}
    }

  /* update applicable trace bits */
  for (trace_nr = 0; trace_nr < MAX_TRACE_VALUES; ++trace_nr)
    {
      if ((mask & (1 << trace_nr)) == 0)
	continue;

      /* Set non-cpu specific values.  */
      switch (trace_nr)
	{
	case TRACE_EVENTS_IDX:
	  STATE_EVENTS (sd)->trace = trace_val;
	  break;
	case TRACE_DEBUG_IDX:
	  STATE_TRACE_FLAGS (sd)[trace_nr] = trace_val;
	  break;
	}

      /* Set cpu values.  */
      for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; cpu_nr++)
	{
	  CPU_TRACE_FLAGS (STATE_CPU (sd, cpu_nr))[trace_nr] = trace_val;
	}
    }

  /* Re-compute the cpu trace summary.  */
  if (trace_val)
    {
      for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; cpu_nr++)
	CPU_TRACE_DATA (STATE_CPU (sd, cpu_nr))->trace_any_p = 1;
    }
  else
    {
      for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; cpu_nr++)
	{
	  CPU_TRACE_DATA (STATE_CPU (sd, cpu_nr))->trace_any_p = 0;
	  for (trace_nr = 0; trace_nr < MAX_TRACE_VALUES; ++trace_nr)
	    {
	      if (CPU_TRACE_FLAGS (STATE_CPU (sd, cpu_nr))[trace_nr])
		{
		  CPU_TRACE_DATA (STATE_CPU (sd, cpu_nr))->trace_any_p = 1;
		  break;
		}
	    }
	}
    }  

  return SIM_RC_OK;
}

/* Set one trace option based on its IDX value.  */

static SIM_RC
set_trace_option (sd, name, idx, arg)
     SIM_DESC sd;
     const char *name;
     int idx;
     const char *arg;
{
  return set_trace_option_mask (sd, name, 1 << idx, arg);
}


static SIM_RC
trace_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
		      char *arg, int is_command)
{
  int n;
  int cpu_nr;

  switch (opt)
    {
    case 't' :
      if (! WITH_TRACE)
	sim_io_eprintf (sd, "Tracing not compiled in, `-t' ignored\n");
      else
	return set_trace_option_mask (sd, "trace", TRACE_USEFUL_MASK, arg);
      break;

    case OPTION_TRACE_INSN :
      if (WITH_TRACE_INSN_P)
	return set_trace_option (sd, "-insn", TRACE_INSN_IDX, arg);
      else
	sim_io_eprintf (sd, "Instruction tracing not compiled in, `--trace-insn' ignored\n");
      break;

    case OPTION_TRACE_DECODE :
      if (WITH_TRACE_DECODE_P)
	return set_trace_option (sd, "-decode", TRACE_DECODE_IDX, arg);
      else
	sim_io_eprintf (sd, "Decode tracing not compiled in, `--trace-decode' ignored\n");
      break;

    case OPTION_TRACE_EXTRACT :
      if (WITH_TRACE_EXTRACT_P)
	return set_trace_option (sd, "-extract", TRACE_EXTRACT_IDX, arg);
      else
	sim_io_eprintf (sd, "Extract tracing not compiled in, `--trace-extract' ignored\n");
      break;

    case OPTION_TRACE_LINENUM :
      if (WITH_TRACE_LINENUM_P && WITH_TRACE_INSN_P)
	{
	  if (set_trace_option (sd, "-linenum", TRACE_LINENUM_IDX, arg) != SIM_RC_OK
	      || set_trace_option (sd, "-linenum", TRACE_INSN_IDX, arg) != SIM_RC_OK)
	    return SIM_RC_FAIL;
	}
      else
	sim_io_eprintf (sd, "Line number or instruction tracing not compiled in, `--trace-linenum' ignored\n");
      break;

    case OPTION_TRACE_MEMORY :
      if (WITH_TRACE_MEMORY_P)
	return set_trace_option (sd, "-memory", TRACE_MEMORY_IDX, arg);
      else
	sim_io_eprintf (sd, "Memory tracing not compiled in, `--trace-memory' ignored\n");
      break;

    case OPTION_TRACE_MODEL :
      if (WITH_TRACE_MODEL_P)
	return set_trace_option (sd, "-model", TRACE_MODEL_IDX, arg);
      else
	sim_io_eprintf (sd, "Model tracing not compiled in, `--trace-model' ignored\n");
      break;

    case OPTION_TRACE_ALU :
      if (WITH_TRACE_ALU_P)
	return set_trace_option (sd, "-alu", TRACE_ALU_IDX, arg);
      else
	sim_io_eprintf (sd, "ALU tracing not compiled in, `--trace-alu' ignored\n");
      break;

    case OPTION_TRACE_CORE :
      if (WITH_TRACE_CORE_P)
	return set_trace_option (sd, "-core", TRACE_CORE_IDX, arg);
      else
	sim_io_eprintf (sd, "CORE tracing not compiled in, `--trace-core' ignored\n");
      break;

    case OPTION_TRACE_EVENTS :
      if (WITH_TRACE_EVENTS_P)
	return set_trace_option (sd, "-events", TRACE_EVENTS_IDX, arg);
      else
	sim_io_eprintf (sd, "EVENTS tracing not compiled in, `--trace-events' ignored\n");
      break;

    case OPTION_TRACE_FPU :
      if (WITH_TRACE_FPU_P)
	return set_trace_option (sd, "-fpu", TRACE_FPU_IDX, arg);
      else
	sim_io_eprintf (sd, "FPU tracing not compiled in, `--trace-fpu' ignored\n");
      break;

    case OPTION_TRACE_VPU :
      if (WITH_TRACE_VPU_P)
	return set_trace_option (sd, "-vpu", TRACE_VPU_IDX, arg);
      else
	sim_io_eprintf (sd, "VPU tracing not compiled in, `--trace-vpu' ignored\n");
      break;

    case OPTION_TRACE_BRANCH :
      if (WITH_TRACE_BRANCH_P)
	return set_trace_option (sd, "-branch", TRACE_BRANCH_IDX, arg);
      else
	sim_io_eprintf (sd, "Branch tracing not compiled in, `--trace-branch' ignored\n");
      break;

    case OPTION_TRACE_SEMANTICS :
      if (WITH_TRACE_ALU_P
	  && WITH_TRACE_FPU_P
	  && WITH_TRACE_MEMORY_P
	  && WITH_TRACE_BRANCH_P)
	{
	  if (set_trace_option (sd, "-semantics", TRACE_ALU_IDX, arg) != SIM_RC_OK
	      || set_trace_option (sd, "-semantics", TRACE_FPU_IDX, arg) != SIM_RC_OK
	      || set_trace_option (sd, "-semantics", TRACE_VPU_IDX, arg) != SIM_RC_OK
	      || set_trace_option (sd, "-semantics", TRACE_MEMORY_IDX, arg) != SIM_RC_OK
	      || set_trace_option (sd, "-semantics", TRACE_BRANCH_IDX, arg) != SIM_RC_OK)
	    return SIM_RC_FAIL;
	}
      else
	sim_io_eprintf (sd, "Alu, fpu, memory, and/or branch tracing not compiled in, `--trace-semantics' ignored\n");
      break;

#ifdef SIM_HAVE_ADDR_RANGE
    case OPTION_TRACE_RANGE :
      if (WITH_TRACE)
	{
	  char *chp = arg;
	  unsigned long start,end;
	  start = strtoul (chp, &chp, 0);
	  if (*chp != ',')
	    {
	      sim_io_eprintf (sd, "--trace-range missing END argument\n");
	      return SIM_RC_FAIL;
	    }
	  end = strtoul (chp + 1, NULL, 0);
	  /* FIXME: Argument validation.  */
	  if (cpu != NULL)
	    sim_addr_range_add (TRACE_RANGE (CPU_PROFILE_DATA (cpu)),
				start, end);
	  else
	    for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
	      sim_addr_range_add (TRACE_RANGE (CPU_TRACE_DATA (STATE_CPU (sd, cpu_nr))),
				  start, end);
	}
      else
	sim_io_eprintf (sd, "Tracing not compiled in, `--trace-range' ignored\n");
      break;

    case OPTION_TRACE_FUNCTION :
      if (WITH_TRACE)
	{
	  /*wip: need to compute function range given name*/
	}
      else
	sim_io_eprintf (sd, "Tracing not compiled in, `--trace-function' ignored\n");
      break;
#endif /* SIM_HAVE_ADDR_RANGE */

    case OPTION_TRACE_DEBUG :
      if (WITH_TRACE_DEBUG_P)
	return set_trace_option (sd, "-debug", TRACE_DEBUG_IDX, arg);
      else
	sim_io_eprintf (sd, "Tracing debug support not compiled in, `--trace-debug' ignored\n");
      break;

    case OPTION_TRACE_FILE :
      if (! WITH_TRACE)
	sim_io_eprintf (sd, "Tracing not compiled in, `--trace-file' ignored\n");
      else
	{
	  FILE *f = fopen (arg, "w");

	  if (f == NULL)
	    {
	      sim_io_eprintf (sd, "Unable to open trace output file `%s'\n", arg);
	      return SIM_RC_FAIL;
	    }
	  for (n = 0; n < MAX_NR_PROCESSORS; ++n)
	    TRACE_FILE (CPU_TRACE_DATA (STATE_CPU (sd, n))) = f;
	  TRACE_FILE (STATE_TRACE_DATA (sd)) = f;
	}
      break;
    }

  return SIM_RC_OK;
}

/* Install tracing support.  */

SIM_RC
trace_install (SIM_DESC sd)
{
  int i;

  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);

  sim_add_option_table (sd, NULL, trace_options);
  memset (STATE_TRACE_DATA (sd), 0, sizeof (* STATE_TRACE_DATA (sd)));
  for (i = 0; i < MAX_NR_PROCESSORS; ++i)
    memset (CPU_TRACE_DATA (STATE_CPU (sd, i)), 0,
	    sizeof (* CPU_TRACE_DATA (STATE_CPU (sd, i))));
  sim_module_add_init_fn (sd, trace_init);
  sim_module_add_uninstall_fn (sd, trace_uninstall);
  return SIM_RC_OK;
}

static SIM_RC
trace_init (SIM_DESC sd)
{
#ifdef SIM_HAVE_ADDR_RANGE
  /* Check if a range has been specified without specifying what to
     collect.  */
  {
    int i;

    for (i = 0; i < MAX_NR_PROCESSORS; ++i)
      {
	sim_cpu *cpu = STATE_CPU (sd, i);

	if (ADDR_RANGE_RANGES (TRACE_RANGE (CPU_TRACE_DATA (cpu)))
	    && ! TRACE_INSN_P (cpu))
	  {
	    sim_io_eprintf_cpu (cpu, "Tracing address range specified without --trace-insn.\n");
	    sim_io_eprintf_cpu (cpu, "Address range ignored.\n");
	    sim_addr_range_delete (TRACE_RANGE (CPU_TRACE_DATA (cpu)),
				   0, ~ (address_word) 0);
	  }
      }
  }
#endif

  return SIM_RC_OK;
}

static void
trace_uninstall (SIM_DESC sd)
{
  int i,j;
  FILE *sfile = TRACE_FILE (STATE_TRACE_DATA (sd));

  if (sfile != NULL)
    fclose (sfile);

  for (i = 0; i < MAX_NR_PROCESSORS; ++i)
    {
      FILE *cfile = TRACE_FILE (CPU_TRACE_DATA (STATE_CPU (sd, i)));
      if (cfile != NULL && cfile != sfile)
	{
	  /* If output from different cpus is going to the same file,
	     avoid closing the file twice.  */
	  for (j = 0; j < i; ++j)
	    if (TRACE_FILE (CPU_TRACE_DATA (STATE_CPU (sd, j))) == cfile)
	      break;
	  if (i == j)
	    fclose (cfile);
	}
    }
}

typedef enum {
  trace_fmt_invalid,
  trace_fmt_word,
  trace_fmt_fp,
  trace_fmt_fpu,
  trace_fmt_string,
  trace_fmt_bool,
  trace_fmt_addr,
  trace_fmt_instruction_incomplete,
} data_fmt;

/* compute the nr of trace data units consumed by data */
static int
save_data_size (TRACE_DATA *data,
		long size)
{
  return ((size + sizeof (TRACE_INPUT_DATA (data) [0]) - 1)
	  / sizeof (TRACE_INPUT_DATA (data) [0]));
}


/* Archive DATA into the trace buffer */
static void
save_data (SIM_DESC sd,
	   TRACE_DATA *data,
	   data_fmt fmt,
	   long size,
	   void *buf)
{
  int i = TRACE_INPUT_IDX (data);
  if (i == sizeof (TRACE_INPUT_FMT (data)))
    sim_io_error (sd, "trace buffer overflow");
  TRACE_INPUT_FMT (data) [i] = fmt;
  TRACE_INPUT_SIZE (data) [i] = size;
  memcpy (&TRACE_INPUT_DATA (data) [i], buf, size);
  i += save_data_size (data, size);
  TRACE_INPUT_IDX (data) = i;
}

static void
print_data (SIM_DESC sd,
	    sim_cpu *cpu,
	    data_fmt fmt,
	    long size,
	    void *data)
{
  switch (fmt)
    {
    case trace_fmt_instruction_incomplete:
      trace_printf (sd, cpu, " (instruction incomplete)");
      break;
    case trace_fmt_word:
    case trace_fmt_addr:
      {
	switch (size)
	  {
	  case sizeof (unsigned32):
	    trace_printf (sd, cpu, " 0x%08lx", (long) * (unsigned32*) data);
	    break;
	  case sizeof (unsigned64):
	    trace_printf (sd, cpu, " 0x%08lx%08lx",
			  (long) ((* (unsigned64*) data) >> 32),
			  (long) * (unsigned64*) data);
	    break;
	  default:
	    abort ();
	  }
	break;
      }
    case trace_fmt_bool:
      {
	SIM_ASSERT (size == sizeof (int));
	trace_printf (sd, cpu, " %-8s",
		      (* (int*) data) ? "true" : "false");
	break;
      }
    case trace_fmt_fp:
      {
	sim_fpu fp;
	switch (size)
	  {
	    /* FIXME: Assumes sizeof float == 4; sizeof double == 8 */
	  case 4:
	    sim_fpu_32to (&fp, *(unsigned32*)data);
	    break;
	  case 8:
	    sim_fpu_64to (&fp, *(unsigned64*)data);
	    break;
	  default:
	    abort ();
	  }
	trace_printf (sd, cpu, " %8g", sim_fpu_2d (&fp));
	switch (size)
	  {
	  case 4:
	    trace_printf (sd, cpu, " (0x%08lx)",
			  (long) *(unsigned32*)data);
	    break;
	  case 8:
	    trace_printf (sd, cpu, " (0x%08lx%08lx)",
			  (long) (*(unsigned64*)data >> 32),
			  (long) (*(unsigned64*)data));
	    break;
	  default:
	    abort ();
	  }
	break;
      }
    case trace_fmt_fpu:
      /* FIXME: At present sim_fpu data is stored as a double */
      trace_printf (sd, cpu, " %8g", * (double*) data);
      break;
    case trace_fmt_string:
      trace_printf (sd, cpu, " %-8s", (char*) data);
      break;
    default:
      abort ();
    }
}
		  
static const char *
trace_idx_to_str (int trace_idx)
{
  static char num[8];
  switch (trace_idx)
    {
    case TRACE_ALU_IDX:     return "alu:     ";
    case TRACE_INSN_IDX:    return "insn:    ";
    case TRACE_DECODE_IDX:  return "decode:  ";
    case TRACE_EXTRACT_IDX: return "extract: ";
    case TRACE_MEMORY_IDX:  return "memory:  ";
    case TRACE_CORE_IDX:    return "core:    ";
    case TRACE_EVENTS_IDX:  return "events:  ";
    case TRACE_FPU_IDX:     return "fpu:     ";
    case TRACE_BRANCH_IDX:  return "branch:  ";
    case TRACE_VPU_IDX:     return "vpu:     ";
    default:
      sprintf (num, "?%d?", trace_idx);
      return num;
    }
}

static void
trace_results (SIM_DESC sd,
	       sim_cpu *cpu,
	       int trace_idx,
	       int last_input)
{
  TRACE_DATA *data = CPU_TRACE_DATA (cpu);
  int nr_out;
  int i;

  /* cross check trace_idx against TRACE_IDX (data)? */

  /* prefix */
  trace_printf (sd, cpu, "%s %s",
		trace_idx_to_str (TRACE_IDX (data)),
		TRACE_PREFIX (data));
  TRACE_IDX (data) = 0;

  for (i = 0, nr_out = 0;
       i < TRACE_INPUT_IDX (data);
       i += save_data_size (data, TRACE_INPUT_SIZE (data) [i]), nr_out++)
    {
      if (i == last_input)
	{
	  int pad = (strlen (" 0x") + sizeof (unsigned_word) * 2);
	  int padding = pad * (3 - nr_out);
	  if (padding < 0)
	    padding = 0;
	  padding += strlen (" ::");
	  trace_printf (sd, cpu, "%*s", padding, " ::");
	}
      print_data (sd, cpu,
		  TRACE_INPUT_FMT (data) [i],
		  TRACE_INPUT_SIZE (data) [i],
		  &TRACE_INPUT_DATA (data) [i]);
    }
  trace_printf (sd, cpu, "\n");
}

void
trace_prefix (SIM_DESC sd,
	      sim_cpu *cpu,
	      sim_cia cia,
	      address_word pc,
	      int line_p,
	      const char *filename,
	      int linenum,
	      const char *fmt,
	      ...)
{
  TRACE_DATA *data = CPU_TRACE_DATA (cpu);
  va_list ap;
  char *prefix = TRACE_PREFIX (data);
  char *chp;
 /* FIXME: The TRACE_PREFIX_WIDTH should be determined at build time using
    known information about the disassembled instructions. */
#ifndef TRACE_PREFIX_WIDTH
#define TRACE_PREFIX_WIDTH 48
#endif
  int width = TRACE_PREFIX_WIDTH;

  /* if the previous trace data wasn't flushed, flush it now with a
     note indicating that the trace was incomplete. */
  if (TRACE_IDX (data) != 0)
    {
      int last_input = TRACE_INPUT_IDX (data);
      save_data (sd, data, trace_fmt_instruction_incomplete, 1, "");
      trace_results (sd, cpu, TRACE_IDX (data), last_input);
    }
  TRACE_IDX (data) = 0;
  TRACE_INPUT_IDX (data) = 0;

  /* Create the text prefix for this new instruction: */
  if (!line_p)
    {
      if (filename)
	{
	  sprintf (prefix, "%s:%-*d 0x%.*lx ",
		   filename,
		   SIZE_LINE_NUMBER, linenum,
		   SIZE_PC, (long) pc);
	}
      else
	{
	  sprintf (prefix, "0x%.*lx ",
		   SIZE_PC, (long) pc);
	  /* Shrink the width by the amount that we didn't print.  */
	  width -= SIZE_LINE_NUMBER + SIZE_PC + 8;
	}
      chp = strchr (prefix, '\0');
      va_start (ap, fmt);
      vsprintf (chp, fmt, ap);
      va_end (ap);
    }
  else
    {
      char buf[256];
      buf[0] = 0;
      if (STATE_TEXT_SECTION (CPU_STATE (cpu))
	  && pc >= STATE_TEXT_START (CPU_STATE (cpu))
	  && pc < STATE_TEXT_END (CPU_STATE (cpu)))
	{
	  const char *pc_filename = (const char *)0;
	  const char *pc_function = (const char *)0;
	  unsigned int pc_linenum = 0;
	  bfd *abfd;
	  asymbol **asymbols;

	  abfd = STATE_PROG_BFD (CPU_STATE (cpu));
	  asymbols = STATE_PROG_SYMS (CPU_STATE (cpu));
	  if (asymbols == NULL)
	    {
	      long symsize;
	      long symbol_count;

	      symsize = bfd_get_symtab_upper_bound (abfd);
	      if (symsize < 0)
		{
		  sim_engine_abort (sd, cpu, cia, "could not read symbols");
		}
	      asymbols = (asymbol **) xmalloc (symsize);
	      symbol_count = bfd_canonicalize_symtab (abfd, asymbols);
	      if (symbol_count < 0)
		{
		  sim_engine_abort (sd, cpu, cia, "could not canonicalize symbols");
		}
	      STATE_PROG_SYMS (CPU_STATE (cpu)) = asymbols;
	    }

	  if (bfd_find_nearest_line (abfd,
				     STATE_TEXT_SECTION (CPU_STATE (cpu)),
				     asymbols,
				     pc - STATE_TEXT_START (CPU_STATE (cpu)),
				     &pc_filename, &pc_function, &pc_linenum))
	    {
	      char *p = buf;
	      if (pc_linenum)
		{
		  sprintf (p, "#%-*d ", SIZE_LINE_NUMBER, pc_linenum);
		  p += strlen (p);
		}
	      else
		{
		  sprintf (p, "%-*s ", SIZE_LINE_NUMBER+1, "---");
		  p += SIZE_LINE_NUMBER+2;
		}

	      if (pc_function)
		{
		  sprintf (p, "%s ", pc_function);
		  p += strlen (p);
		}
	      else if (pc_filename)
		{
		  char *q = (char *) strrchr (pc_filename, '/');
		  sprintf (p, "%s ", (q) ? q+1 : pc_filename);
		  p += strlen (p);
		}

	      if (*p == ' ')
		*p = '\0';
	    }
	}

      sprintf (prefix, "0x%.*x %-*.*s ",
	       SIZE_PC, (unsigned) pc,
	       SIZE_LOCATION, SIZE_LOCATION, buf);
      chp = strchr (prefix, '\0');
      va_start (ap, fmt);
      vsprintf (chp, fmt, ap);
      va_end (ap);
    }

  /* Pad it out to TRACE_PREFIX_WIDTH.  */
  chp = strchr (prefix, '\0');
  if (chp - prefix < width)
    {
      memset (chp, ' ', width - (chp - prefix));
      chp = &prefix [width];
      *chp = '\0';
    }
  strcpy (chp, " -");

  /* check that we've not over flowed the prefix buffer */
  if (strlen (prefix) >= sizeof (TRACE_PREFIX (data)))
    abort ();
}

void
trace_generic (SIM_DESC sd,
	       sim_cpu *cpu,
	       int trace_idx,
	       const char *fmt,
	       ...)
{
  va_list ap;
  trace_printf (sd, cpu, "%s %s",
		trace_idx_to_str (trace_idx),
		TRACE_PREFIX (CPU_TRACE_DATA (cpu)));
  va_start (ap, fmt);
  trace_vprintf (sd, cpu, fmt, ap);
  va_end (ap);
  trace_printf (sd, cpu, "\n");
}

void
trace_input0 (SIM_DESC sd,
	      sim_cpu *cpu,
	      int trace_idx)
{
  TRACE_DATA *data = CPU_TRACE_DATA (cpu);
  TRACE_IDX (data) = trace_idx;
}

void
trace_input_word1 (SIM_DESC sd,
		   sim_cpu *cpu,
		   int trace_idx,
		   unsigned_word d0)
{
  TRACE_DATA *data = CPU_TRACE_DATA (cpu);
  TRACE_IDX (data) = trace_idx;
  save_data (sd, data, trace_fmt_word, sizeof (unsigned_word), &d0);
}

void
trace_input_word2 (SIM_DESC sd,
		   sim_cpu *cpu,
		   int trace_idx,
		   unsigned_word d0,
		   unsigned_word d1)
{
  TRACE_DATA *data = CPU_TRACE_DATA (cpu);
  TRACE_IDX (data) = trace_idx;
  save_data (sd, data, trace_fmt_word, sizeof (unsigned_word), &d0);
  save_data (sd, data, trace_fmt_word, sizeof (unsigned_word), &d1);
}

void
trace_input_word3 (SIM_DESC sd,
		   sim_cpu *cpu,
		   int trace_idx,
		   unsigned_word d0,
		   unsigned_word d1,
		   unsigned_word d2)
{
  TRACE_DATA *data = CPU_TRACE_DATA (cpu);
  TRACE_IDX (data) = trace_idx;
  save_data (sd, data, trace_fmt_word, sizeof (unsigned_word), &d0);
  save_data (sd, data, trace_fmt_word, sizeof (unsigned_word), &d1);
  save_data (sd, data, trace_fmt_word, sizeof (unsigned_word), &d2);
}

void
trace_input_word4 (SIM_DESC sd,
		   sim_cpu *cpu,
		   int trace_idx,
		   unsigned_word d0,
		   unsigned_word d1,
		   unsigned_word d2,
		   unsigned_word d3)
{
  TRACE_DATA *data = CPU_TRACE_DATA (cpu);
  TRACE_IDX (data) = trace_idx;
  save_data (sd, data, trace_fmt_word, sizeof (d0), &d0);
  save_data (sd, data, trace_fmt_word, sizeof (d1), &d1);
  save_data (sd, data, trace_fmt_word, sizeof (d2), &d2);
  save_data (sd, data, trace_fmt_word, sizeof (d3), &d3);
}

void
trace_input_bool1 (SIM_DESC sd,
		   sim_cpu *cpu,
		   int trace_idx,
		   int d0)
{
  TRACE_DATA *data = CPU_TRACE_DATA (cpu);
  TRACE_IDX (data) = trace_idx;
  save_data (sd, data, trace_fmt_bool, sizeof (d0), &d0);
}

void
trace_input_addr1 (SIM_DESC sd,
		   sim_cpu *cpu,
		   int trace_idx,
		   address_word d0)
{
  TRACE_DATA *data = CPU_TRACE_DATA (cpu);
  TRACE_IDX (data) = trace_idx;
  save_data (sd, data, trace_fmt_addr, sizeof (d0), &d0);
}

void
trace_input_fp1 (SIM_DESC sd,
		 sim_cpu *cpu,
		 int trace_idx,
		 fp_word f0)
{
  TRACE_DATA *data = CPU_TRACE_DATA (cpu);
  TRACE_IDX (data) = trace_idx;
  save_data (sd, data, trace_fmt_fp, sizeof (fp_word), &f0);
}

void
trace_input_fp2 (SIM_DESC sd,
		 sim_cpu *cpu,
		 int trace_idx,
		 fp_word f0,
		 fp_word f1)
{
  TRACE_DATA *data = CPU_TRACE_DATA (cpu);
  TRACE_IDX (data) = trace_idx;
  save_data (sd, data, trace_fmt_fp, sizeof (fp_word), &f0);
  save_data (sd, data, trace_fmt_fp, sizeof (fp_word), &f1);
}

void
trace_input_fp3 (SIM_DESC sd,
		 sim_cpu *cpu,
		 int trace_idx,
		 fp_word f0,
		 fp_word f1,
		 fp_word f2)
{
  TRACE_DATA *data = CPU_TRACE_DATA (cpu);
  TRACE_IDX (data) = trace_idx;
  save_data (sd, data, trace_fmt_fp, sizeof (fp_word), &f0);
  save_data (sd, data, trace_fmt_fp, sizeof (fp_word), &f1);
  save_data (sd, data, trace_fmt_fp, sizeof (fp_word), &f2);
}

void
trace_input_fpu1 (SIM_DESC sd,
		  sim_cpu *cpu,
		  int trace_idx,
		  sim_fpu *f0)
{
  double d;
  TRACE_DATA *data = CPU_TRACE_DATA (cpu);
  TRACE_IDX (data) = trace_idx;
  d = sim_fpu_2d (f0);
  save_data (sd, data, trace_fmt_fp, sizeof (double), &d);
}

void
trace_input_fpu2 (SIM_DESC sd,
		  sim_cpu *cpu,
		  int trace_idx,
		  sim_fpu *f0,
		  sim_fpu *f1)
{
  double d;
  TRACE_DATA *data = CPU_TRACE_DATA (cpu);
  TRACE_IDX (data) = trace_idx;
  d = sim_fpu_2d (f0);
  save_data (sd, data, trace_fmt_fp, sizeof (double), &d);
  d = sim_fpu_2d (f1);
  save_data (sd, data, trace_fmt_fp, sizeof (double), &d);
}

void
trace_input_fpu3 (SIM_DESC sd,
		  sim_cpu *cpu,
		  int trace_idx,
		  sim_fpu *f0,
		  sim_fpu *f1,
		  sim_fpu *f2)
{
  double d;
  TRACE_DATA *data = CPU_TRACE_DATA (cpu);
  TRACE_IDX (data) = trace_idx;
  d = sim_fpu_2d (f0);
  save_data (sd, data, trace_fmt_fp, sizeof (double), &d);
  d = sim_fpu_2d (f1);
  save_data (sd, data, trace_fmt_fp, sizeof (double), &d);
  d = sim_fpu_2d (f2);
  save_data (sd, data, trace_fmt_fp, sizeof (double), &d);
}

void
trace_result_word1 (SIM_DESC sd,
		    sim_cpu *cpu,
		    int trace_idx,
		    unsigned_word r0)
{
  TRACE_DATA *data = CPU_TRACE_DATA (cpu);
  int last_input;

  /* Append any results to the end of the inputs */
  last_input = TRACE_INPUT_IDX (data);
  save_data (sd, data, trace_fmt_word, sizeof (unsigned_word), &r0);

  trace_results (sd, cpu, trace_idx, last_input);
}	      

void
trace_result0 (SIM_DESC sd,
	       sim_cpu *cpu,
	       int trace_idx)
{
  TRACE_DATA *data = CPU_TRACE_DATA (cpu);
  int last_input;

  /* Append any results to the end of the inputs */
  last_input = TRACE_INPUT_IDX (data);

  trace_results (sd, cpu, trace_idx, last_input);
}	      

void
trace_result_word2 (SIM_DESC sd,
		    sim_cpu *cpu,
		    int trace_idx,
		    unsigned_word r0,
		    unsigned_word r1)
{
  TRACE_DATA *data = CPU_TRACE_DATA (cpu);
  int last_input;

  /* Append any results to the end of the inputs */
  last_input = TRACE_INPUT_IDX (data);
  save_data (sd, data, trace_fmt_word, sizeof (r0), &r0);
  save_data (sd, data, trace_fmt_word, sizeof (r1), &r1);

  trace_results (sd, cpu, trace_idx, last_input);
}	      

void
trace_result_word4 (SIM_DESC sd,
		    sim_cpu *cpu,
		    int trace_idx,
		    unsigned_word r0,
		    unsigned_word r1,
		    unsigned_word r2,
		    unsigned_word r3)
{
  TRACE_DATA *data = CPU_TRACE_DATA (cpu);
  int last_input;

  /* Append any results to the end of the inputs */
  last_input = TRACE_INPUT_IDX (data);
  save_data (sd, data, trace_fmt_word, sizeof (r0), &r0);
  save_data (sd, data, trace_fmt_word, sizeof (r1), &r1);
  save_data (sd, data, trace_fmt_word, sizeof (r2), &r2);
  save_data (sd, data, trace_fmt_word, sizeof (r3), &r3);

  trace_results (sd, cpu, trace_idx, last_input);
}	      

void
trace_result_bool1 (SIM_DESC sd,
		    sim_cpu *cpu,
		    int trace_idx,
		    int r0)
{
  TRACE_DATA *data = CPU_TRACE_DATA (cpu);
  int last_input;

  /* Append any results to the end of the inputs */
  last_input = TRACE_INPUT_IDX (data);
  save_data (sd, data, trace_fmt_bool, sizeof (r0), &r0);

  trace_results (sd, cpu, trace_idx, last_input);
}	      

void
trace_result_addr1 (SIM_DESC sd,
		    sim_cpu *cpu,
		    int trace_idx,
		    address_word r0)
{
  TRACE_DATA *data = CPU_TRACE_DATA (cpu);
  int last_input;

  /* Append any results to the end of the inputs */
  last_input = TRACE_INPUT_IDX (data);
  save_data (sd, data, trace_fmt_addr, sizeof (r0), &r0);

  trace_results (sd, cpu, trace_idx, last_input);
}	      

void
trace_result_fp1 (SIM_DESC sd,
		  sim_cpu *cpu,
		  int trace_idx,
		  fp_word f0)
{
  TRACE_DATA *data = CPU_TRACE_DATA (cpu);
  int last_input;

  /* Append any results to the end of the inputs */
  last_input = TRACE_INPUT_IDX (data);
  save_data (sd, data, trace_fmt_fp, sizeof (fp_word), &f0);

  trace_results (sd, cpu, trace_idx, last_input);
}	      

void
trace_result_fp2 (SIM_DESC sd,
		  sim_cpu *cpu,
		  int trace_idx,
		  fp_word f0,
		  fp_word f1)
{
  TRACE_DATA *data = CPU_TRACE_DATA (cpu);
  int last_input;

  /* Append any results to the end of the inputs */
  last_input = TRACE_INPUT_IDX (data);
  save_data (sd, data, trace_fmt_fp, sizeof (f0), &f0);
  save_data (sd, data, trace_fmt_fp, sizeof (f1), &f1);

  trace_results (sd, cpu, trace_idx, last_input);
}	      

void
trace_result_fpu1 (SIM_DESC sd,
		   sim_cpu *cpu,
		   int trace_idx,
		   sim_fpu *f0)
{
  double d;
  TRACE_DATA *data = CPU_TRACE_DATA (cpu);
  int last_input;

  /* Append any results to the end of the inputs */
  last_input = TRACE_INPUT_IDX (data);
  d = sim_fpu_2d (f0);
  save_data (sd, data, trace_fmt_fp, sizeof (double), &d);

  trace_results (sd, cpu, trace_idx, last_input);
}	      

void
trace_result_string1 (SIM_DESC sd,
		      sim_cpu *cpu,
		      int trace_idx,
		      char *s0)
{
  TRACE_DATA *data = CPU_TRACE_DATA (cpu);
  int last_input;

  /* Append any results to the end of the inputs */
  last_input = TRACE_INPUT_IDX (data);
  save_data (sd, data, trace_fmt_string, strlen (s0) + 1, s0);

  trace_results (sd, cpu, trace_idx, last_input);
}	      

void
trace_result_word1_string1 (SIM_DESC sd,
			    sim_cpu *cpu,
			    int trace_idx,
			    unsigned_word r0,
			    char *s0)
{
  TRACE_DATA *data = CPU_TRACE_DATA (cpu);
  int last_input;

  /* Append any results to the end of the inputs */
  last_input = TRACE_INPUT_IDX (data);
  save_data (sd, data, trace_fmt_word, sizeof (unsigned_word), &r0);
  save_data (sd, data, trace_fmt_string, strlen (s0) + 1, s0);

  trace_results (sd, cpu, trace_idx, last_input);
}	      

void
trace_vprintf (SIM_DESC sd, sim_cpu *cpu, const char *fmt, va_list ap)
{
  if (cpu != NULL)
    {
      if (TRACE_FILE (CPU_TRACE_DATA (cpu)) != NULL)
	vfprintf (TRACE_FILE (CPU_TRACE_DATA (cpu)), fmt, ap);
      else
	sim_io_evprintf (sd, fmt, ap);
    }
  else
    {
      if (TRACE_FILE (STATE_TRACE_DATA (sd)) != NULL)
	vfprintf (TRACE_FILE (STATE_TRACE_DATA (sd)), fmt, ap);
      else
	sim_io_evprintf (sd, fmt, ap);
    }
}

/* The function trace_one_insn has been replaced by the function pair
   trace_prefix() + trace_generic().  It is still used. */
void
trace_one_insn (SIM_DESC sd, sim_cpu *cpu, address_word pc,
		int line_p, const char *filename, int linenum,
		const char *phase_wo_colon, const char *fmt,
		...)
{
  va_list ap;
  char phase[SIZE_PHASE+2];

  strncpy (phase, phase_wo_colon, SIZE_PHASE);
  strcat (phase, ":");

  if (!line_p)
    {
      trace_printf (sd, cpu, "%-*s %s:%-*d 0x%.*lx ",
		    SIZE_PHASE+1, phase,
		    filename,
		    SIZE_LINE_NUMBER, linenum,
		    SIZE_PC, (long)pc);
      va_start (ap, fmt);
      trace_vprintf (sd, cpu, fmt, ap);
      va_end (ap);
      trace_printf (sd, cpu, "\n");
    }
  else
    {
      char buf[256];

      buf[0] = 0;
      if (STATE_TEXT_SECTION (CPU_STATE (cpu))
	  && pc >= STATE_TEXT_START (CPU_STATE (cpu))
	  && pc < STATE_TEXT_END (CPU_STATE (cpu)))
	{
	  const char *pc_filename = (const char *)0;
	  const char *pc_function = (const char *)0;
	  unsigned int pc_linenum = 0;

	  if (bfd_find_nearest_line (STATE_PROG_BFD (CPU_STATE (cpu)),
				     STATE_TEXT_SECTION (CPU_STATE (cpu)),
				     (struct bfd_symbol **) 0,
				     pc - STATE_TEXT_START (CPU_STATE (cpu)),
				     &pc_filename, &pc_function, &pc_linenum))
	    {
	      char *p = buf;
	      if (pc_linenum)
		{
		  sprintf (p, "#%-*d ", SIZE_LINE_NUMBER, pc_linenum);
		  p += strlen (p);
		}
	      else
		{
		  sprintf (p, "%-*s ", SIZE_LINE_NUMBER+1, "---");
		  p += SIZE_LINE_NUMBER+2;
		}

	      if (pc_function)
		{
		  sprintf (p, "%s ", pc_function);
		  p += strlen (p);
		}
	      else if (pc_filename)
		{
		  char *q = (char *) strrchr (pc_filename, '/');
		  sprintf (p, "%s ", (q) ? q+1 : pc_filename);
		  p += strlen (p);
		}

	      if (*p == ' ')
		*p = '\0';
	    }
	}

      trace_printf (sd, cpu, "%-*s 0x%.*x %-*.*s ",
		    SIZE_PHASE+1, phase,
		    SIZE_PC, (unsigned) pc,
		    SIZE_LOCATION, SIZE_LOCATION, buf);
      va_start (ap, fmt);
      trace_vprintf (sd, cpu, fmt, ap);
      va_end (ap);
      trace_printf (sd, cpu, "\n");
    }
}

void
trace_printf VPARAMS ((SIM_DESC sd, sim_cpu *cpu, const char *fmt, ...))
{
#if !defined __STDC__ && !defined ALMOST_STDC
  SIM_DESC sd;
  sim_cpu *cpu;
  const char *fmt;
#endif
  va_list ap;

  VA_START (ap, fmt);
#if !defined __STDC__ && !defined ALMOST_STDC
  sd = va_arg (ap, SIM_DESC);
  cpu = va_arg (ap, sim_cpu *);
  fmt = va_arg (ap, const char *);
#endif

  trace_vprintf (sd, cpu, fmt, ap);

  va_end (ap);
}

void
debug_printf VPARAMS ((sim_cpu *cpu, const char *fmt, ...))
{
#if !defined __STDC__ && !defined ALMOST_STDC
  sim_cpu *cpu;
  const char *fmt;
#endif
  va_list ap;

  VA_START (ap, fmt);
#if !defined __STDC__ && !defined ALMOST_STDC
  cpu = va_arg (ap, sim_cpu *);
  fmt = va_arg (ap, const char *);
#endif

  if (CPU_DEBUG_FILE (cpu) == NULL)
    (* STATE_CALLBACK (CPU_STATE (cpu))->evprintf_filtered)
      (STATE_CALLBACK (CPU_STATE (cpu)), fmt, ap);
  else
    vfprintf (CPU_DEBUG_FILE (cpu), fmt, ap);

  va_end (ap);
}