1 /* Simulator tracing/debugging support.
2    Copyright (C) 1997-2013 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 #include "sim-main.h"
21 #include "sim-io.h"
22 #include "sim-options.h"
23 #include "sim-fpu.h"
24 
25 #include "bfd.h"
26 #include "libiberty.h"
27 
28 #include "sim-assert.h"
29 
30 #ifdef HAVE_STRING_H
31 #include <string.h>
32 #else
33 #ifdef HAVE_STRINGS_H
34 #include <strings.h>
35 #endif
36 #endif
37 #ifdef HAVE_STDLIB_H
38 #include <stdlib.h>
39 #endif
40 
41 #ifndef SIZE_PHASE
42 #define SIZE_PHASE 8
43 #endif
44 
45 #ifndef SIZE_LOCATION
46 #define SIZE_LOCATION 20
47 #endif
48 
49 #ifndef SIZE_PC
50 #define SIZE_PC 6
51 #endif
52 
53 #ifndef SIZE_LINE_NUMBER
54 #define SIZE_LINE_NUMBER 4
55 #endif
56 
57 static MODULE_INIT_FN trace_init;
58 static MODULE_UNINSTALL_FN trace_uninstall;
59 
60 static DECLARE_OPTION_HANDLER (trace_option_handler);
61 
62 enum {
63   OPTION_TRACE_INSN	= OPTION_START,
64   OPTION_TRACE_DECODE,
65   OPTION_TRACE_EXTRACT,
66   OPTION_TRACE_LINENUM,
67   OPTION_TRACE_MEMORY,
68   OPTION_TRACE_MODEL,
69   OPTION_TRACE_ALU,
70   OPTION_TRACE_CORE,
71   OPTION_TRACE_EVENTS,
72   OPTION_TRACE_FPU,
73   OPTION_TRACE_BRANCH,
74   OPTION_TRACE_SEMANTICS,
75   OPTION_TRACE_RANGE,
76   OPTION_TRACE_FUNCTION,
77   OPTION_TRACE_DEBUG,
78   OPTION_TRACE_FILE,
79   OPTION_TRACE_VPU,
80   OPTION_TRACE_SYSCALL
81 };
82 
83 static const OPTION trace_options[] =
84 {
85   /* This table is organized to group related instructions together.  */
86   { {"trace", optional_argument, NULL, 't'},
87       't', "on|off", "Trace useful things",
88       trace_option_handler, NULL },
89   { {"trace-insn", optional_argument, NULL, OPTION_TRACE_INSN},
90       '\0', "on|off", "Perform instruction tracing",
91       trace_option_handler, NULL },
92   { {"trace-decode", optional_argument, NULL, OPTION_TRACE_DECODE},
93       '\0', "on|off", "Trace instruction decoding",
94       trace_option_handler, NULL },
95   { {"trace-extract", optional_argument, NULL, OPTION_TRACE_EXTRACT},
96       '\0', "on|off", "Trace instruction extraction",
97       trace_option_handler, NULL },
98   { {"trace-linenum", optional_argument, NULL, OPTION_TRACE_LINENUM},
99       '\0', "on|off", "Perform line number tracing (implies --trace-insn)",
100       trace_option_handler, NULL },
101   { {"trace-memory", optional_argument, NULL, OPTION_TRACE_MEMORY},
102       '\0', "on|off", "Trace memory operations",
103       trace_option_handler, NULL },
104   { {"trace-alu", optional_argument, NULL, OPTION_TRACE_ALU},
105       '\0', "on|off", "Trace ALU operations",
106       trace_option_handler, NULL },
107   { {"trace-fpu", optional_argument, NULL, OPTION_TRACE_FPU},
108       '\0', "on|off", "Trace FPU operations",
109       trace_option_handler, NULL },
110   { {"trace-vpu", optional_argument, NULL, OPTION_TRACE_VPU},
111       '\0', "on|off", "Trace VPU operations",
112       trace_option_handler, NULL },
113   { {"trace-branch", optional_argument, NULL, OPTION_TRACE_BRANCH},
114       '\0', "on|off", "Trace branching",
115       trace_option_handler, NULL },
116   { {"trace-semantics", optional_argument, NULL, OPTION_TRACE_SEMANTICS},
117       '\0', "on|off", "Perform ALU, FPU, MEMORY, and BRANCH tracing",
118       trace_option_handler, NULL },
119   { {"trace-model", optional_argument, NULL, OPTION_TRACE_MODEL},
120       '\0', "on|off", "Include model performance data",
121       trace_option_handler, NULL },
122   { {"trace-core", optional_argument, NULL, OPTION_TRACE_CORE},
123       '\0', "on|off", "Trace core operations",
124       trace_option_handler, NULL },
125   { {"trace-events", optional_argument, NULL, OPTION_TRACE_EVENTS},
126       '\0', "on|off", "Trace events",
127       trace_option_handler, NULL },
128   { {"trace-syscall", optional_argument, NULL, OPTION_TRACE_SYSCALL},
129       '\0', "on|off", "Trace system calls",
130       trace_option_handler, NULL },
131 #ifdef SIM_HAVE_ADDR_RANGE
132   { {"trace-range", required_argument, NULL, OPTION_TRACE_RANGE},
133       '\0', "START,END", "Specify range of addresses for instruction tracing",
134       trace_option_handler, NULL },
135 #if 0 /*wip*/
136   { {"trace-function", required_argument, NULL, OPTION_TRACE_FUNCTION},
137       '\0', "FUNCTION", "Specify function to trace",
138       trace_option_handler, NULL },
139 #endif
140 #endif
141   { {"trace-debug", optional_argument, NULL, OPTION_TRACE_DEBUG},
142       '\0', "on|off", "Add information useful for debugging the simulator to the tracing output",
143       trace_option_handler, NULL },
144   { {"trace-file", required_argument, NULL, OPTION_TRACE_FILE},
145       '\0', "FILE NAME", "Specify tracing output file",
146       trace_option_handler, NULL },
147   { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL, NULL }
148 };
149 
150 /* Set/reset the trace options indicated in MASK.  */
151 
152 static SIM_RC
set_trace_option_mask(SIM_DESC sd,const char * name,int mask,const char * arg)153 set_trace_option_mask (SIM_DESC sd, const char *name, int mask, const char *arg)
154 {
155   int trace_nr;
156   int cpu_nr;
157   int trace_val = 1;
158 
159   if (arg != NULL)
160     {
161       if (strcmp (arg, "yes") == 0
162 	  || strcmp (arg, "on") == 0
163 	  || strcmp (arg, "1") == 0)
164 	trace_val = 1;
165       else if (strcmp (arg, "no") == 0
166 	       || strcmp (arg, "off") == 0
167 	       || strcmp (arg, "0") == 0)
168 	trace_val = 0;
169       else
170 	{
171 	  sim_io_eprintf (sd, "Argument `%s' for `--trace%s' invalid, one of `on', `off', `yes', `no' expected\n", arg, name);
172 	  return SIM_RC_FAIL;
173 	}
174     }
175 
176   /* update applicable trace bits */
177   for (trace_nr = 0; trace_nr < MAX_TRACE_VALUES; ++trace_nr)
178     {
179       if ((mask & (1 << trace_nr)) == 0)
180 	continue;
181 
182       /* Set non-cpu specific values.  */
183       switch (trace_nr)
184 	{
185 	case TRACE_EVENTS_IDX:
186 	  STATE_EVENTS (sd)->trace = trace_val;
187 	  break;
188 	case TRACE_DEBUG_IDX:
189 	  STATE_TRACE_FLAGS (sd)[trace_nr] = trace_val;
190 	  break;
191 	}
192 
193       /* Set cpu values.  */
194       for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; cpu_nr++)
195 	{
196 	  CPU_TRACE_FLAGS (STATE_CPU (sd, cpu_nr))[trace_nr] = trace_val;
197 	}
198     }
199 
200   /* Re-compute the cpu trace summary.  */
201   if (trace_val)
202     {
203       for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; cpu_nr++)
204 	CPU_TRACE_DATA (STATE_CPU (sd, cpu_nr))->trace_any_p = 1;
205     }
206   else
207     {
208       for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; cpu_nr++)
209 	{
210 	  CPU_TRACE_DATA (STATE_CPU (sd, cpu_nr))->trace_any_p = 0;
211 	  for (trace_nr = 0; trace_nr < MAX_TRACE_VALUES; ++trace_nr)
212 	    {
213 	      if (CPU_TRACE_FLAGS (STATE_CPU (sd, cpu_nr))[trace_nr])
214 		{
215 		  CPU_TRACE_DATA (STATE_CPU (sd, cpu_nr))->trace_any_p = 1;
216 		  break;
217 		}
218 	    }
219 	}
220     }
221 
222   return SIM_RC_OK;
223 }
224 
225 /* Set one trace option based on its IDX value.  */
226 
227 static SIM_RC
set_trace_option(SIM_DESC sd,const char * name,int idx,const char * arg)228 set_trace_option (SIM_DESC sd, const char *name, int idx, const char *arg)
229 {
230   return set_trace_option_mask (sd, name, 1 << idx, arg);
231 }
232 
233 
234 static SIM_RC
trace_option_handler(SIM_DESC sd,sim_cpu * cpu,int opt,char * arg,int is_command)235 trace_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
236 		      char *arg, int is_command)
237 {
238   int n;
239 
240   switch (opt)
241     {
242     case 't' :
243       if (! WITH_TRACE)
244 	sim_io_eprintf (sd, "Tracing not compiled in, `-t' ignored\n");
245       else
246 	return set_trace_option_mask (sd, "trace", TRACE_USEFUL_MASK, arg);
247       break;
248 
249     case OPTION_TRACE_INSN :
250       if (WITH_TRACE_INSN_P)
251 	return set_trace_option (sd, "-insn", TRACE_INSN_IDX, arg);
252       else
253 	sim_io_eprintf (sd, "Instruction tracing not compiled in, `--trace-insn' ignored\n");
254       break;
255 
256     case OPTION_TRACE_DECODE :
257       if (WITH_TRACE_DECODE_P)
258 	return set_trace_option (sd, "-decode", TRACE_DECODE_IDX, arg);
259       else
260 	sim_io_eprintf (sd, "Decode tracing not compiled in, `--trace-decode' ignored\n");
261       break;
262 
263     case OPTION_TRACE_EXTRACT :
264       if (WITH_TRACE_EXTRACT_P)
265 	return set_trace_option (sd, "-extract", TRACE_EXTRACT_IDX, arg);
266       else
267 	sim_io_eprintf (sd, "Extract tracing not compiled in, `--trace-extract' ignored\n");
268       break;
269 
270     case OPTION_TRACE_LINENUM :
271       if (WITH_TRACE_LINENUM_P && WITH_TRACE_INSN_P)
272 	{
273 	  if (set_trace_option (sd, "-linenum", TRACE_LINENUM_IDX, arg) != SIM_RC_OK
274 	      || set_trace_option (sd, "-linenum", TRACE_INSN_IDX, arg) != SIM_RC_OK)
275 	    return SIM_RC_FAIL;
276 	}
277       else
278 	sim_io_eprintf (sd, "Line number or instruction tracing not compiled in, `--trace-linenum' ignored\n");
279       break;
280 
281     case OPTION_TRACE_MEMORY :
282       if (WITH_TRACE_MEMORY_P)
283 	return set_trace_option (sd, "-memory", TRACE_MEMORY_IDX, arg);
284       else
285 	sim_io_eprintf (sd, "Memory tracing not compiled in, `--trace-memory' ignored\n");
286       break;
287 
288     case OPTION_TRACE_MODEL :
289       if (WITH_TRACE_MODEL_P)
290 	return set_trace_option (sd, "-model", TRACE_MODEL_IDX, arg);
291       else
292 	sim_io_eprintf (sd, "Model tracing not compiled in, `--trace-model' ignored\n");
293       break;
294 
295     case OPTION_TRACE_ALU :
296       if (WITH_TRACE_ALU_P)
297 	return set_trace_option (sd, "-alu", TRACE_ALU_IDX, arg);
298       else
299 	sim_io_eprintf (sd, "ALU tracing not compiled in, `--trace-alu' ignored\n");
300       break;
301 
302     case OPTION_TRACE_CORE :
303       if (WITH_TRACE_CORE_P)
304 	return set_trace_option (sd, "-core", TRACE_CORE_IDX, arg);
305       else
306 	sim_io_eprintf (sd, "CORE tracing not compiled in, `--trace-core' ignored\n");
307       break;
308 
309     case OPTION_TRACE_EVENTS :
310       if (WITH_TRACE_EVENTS_P)
311 	return set_trace_option (sd, "-events", TRACE_EVENTS_IDX, arg);
312       else
313 	sim_io_eprintf (sd, "EVENTS tracing not compiled in, `--trace-events' ignored\n");
314       break;
315 
316     case OPTION_TRACE_FPU :
317       if (WITH_TRACE_FPU_P)
318 	return set_trace_option (sd, "-fpu", TRACE_FPU_IDX, arg);
319       else
320 	sim_io_eprintf (sd, "FPU tracing not compiled in, `--trace-fpu' ignored\n");
321       break;
322 
323     case OPTION_TRACE_VPU :
324       if (WITH_TRACE_VPU_P)
325 	return set_trace_option (sd, "-vpu", TRACE_VPU_IDX, arg);
326       else
327 	sim_io_eprintf (sd, "VPU tracing not compiled in, `--trace-vpu' ignored\n");
328       break;
329 
330     case OPTION_TRACE_BRANCH :
331       if (WITH_TRACE_BRANCH_P)
332 	return set_trace_option (sd, "-branch", TRACE_BRANCH_IDX, arg);
333       else
334 	sim_io_eprintf (sd, "Branch tracing not compiled in, `--trace-branch' ignored\n");
335       break;
336 
337     case OPTION_TRACE_SYSCALL :
338       if (WITH_TRACE_SYSCALL_P)
339 	return set_trace_option (sd, "-syscall", TRACE_SYSCALL_IDX, arg);
340       else
341 	sim_io_eprintf (sd, "System call tracing not compiled in, `--trace-syscall' ignored\n");
342       break;
343 
344     case OPTION_TRACE_SEMANTICS :
345       if (WITH_TRACE_ALU_P
346 	  && WITH_TRACE_FPU_P
347 	  && WITH_TRACE_MEMORY_P
348 	  && WITH_TRACE_BRANCH_P)
349 	{
350 	  if (set_trace_option (sd, "-semantics", TRACE_ALU_IDX, arg) != SIM_RC_OK
351 	      || set_trace_option (sd, "-semantics", TRACE_FPU_IDX, arg) != SIM_RC_OK
352 	      || set_trace_option (sd, "-semantics", TRACE_VPU_IDX, arg) != SIM_RC_OK
353 	      || set_trace_option (sd, "-semantics", TRACE_MEMORY_IDX, arg) != SIM_RC_OK
354 	      || set_trace_option (sd, "-semantics", TRACE_BRANCH_IDX, arg) != SIM_RC_OK)
355 	    return SIM_RC_FAIL;
356 	}
357       else
358 	sim_io_eprintf (sd, "Alu, fpu, memory, and/or branch tracing not compiled in, `--trace-semantics' ignored\n");
359       break;
360 
361 #ifdef SIM_HAVE_ADDR_RANGE
362     case OPTION_TRACE_RANGE :
363       if (WITH_TRACE)
364 	{
365 	  int cpu_nr;
366 	  char *chp = arg;
367 	  unsigned long start,end;
368 	  start = strtoul (chp, &chp, 0);
369 	  if (*chp != ',')
370 	    {
371 	      sim_io_eprintf (sd, "--trace-range missing END argument\n");
372 	      return SIM_RC_FAIL;
373 	    }
374 	  end = strtoul (chp + 1, NULL, 0);
375 	  /* FIXME: Argument validation.  */
376 	  if (cpu != NULL)
377 	    sim_addr_range_add (TRACE_RANGE (CPU_PROFILE_DATA (cpu)),
378 				start, end);
379 	  else
380 	    for (cpu_nr = 0; cpu_nr < MAX_NR_PROCESSORS; ++cpu_nr)
381 	      sim_addr_range_add (TRACE_RANGE (CPU_TRACE_DATA (STATE_CPU (sd, cpu_nr))),
382 				  start, end);
383 	}
384       else
385 	sim_io_eprintf (sd, "Tracing not compiled in, `--trace-range' ignored\n");
386       break;
387 
388     case OPTION_TRACE_FUNCTION :
389       if (WITH_TRACE)
390 	{
391 	  /*wip: need to compute function range given name*/
392 	}
393       else
394 	sim_io_eprintf (sd, "Tracing not compiled in, `--trace-function' ignored\n");
395       break;
396 #endif /* SIM_HAVE_ADDR_RANGE */
397 
398     case OPTION_TRACE_DEBUG :
399       if (WITH_TRACE_DEBUG_P)
400 	return set_trace_option (sd, "-debug", TRACE_DEBUG_IDX, arg);
401       else
402 	sim_io_eprintf (sd, "Tracing debug support not compiled in, `--trace-debug' ignored\n");
403       break;
404 
405     case OPTION_TRACE_FILE :
406       if (! WITH_TRACE)
407 	sim_io_eprintf (sd, "Tracing not compiled in, `--trace-file' ignored\n");
408       else
409 	{
410 	  FILE *f = fopen (arg, "w");
411 
412 	  if (f == NULL)
413 	    {
414 	      sim_io_eprintf (sd, "Unable to open trace output file `%s'\n", arg);
415 	      return SIM_RC_FAIL;
416 	    }
417 	  for (n = 0; n < MAX_NR_PROCESSORS; ++n)
418 	    TRACE_FILE (CPU_TRACE_DATA (STATE_CPU (sd, n))) = f;
419 	  TRACE_FILE (STATE_TRACE_DATA (sd)) = f;
420 	}
421       break;
422     }
423 
424   return SIM_RC_OK;
425 }
426 
427 /* Install tracing support.  */
428 
429 SIM_RC
trace_install(SIM_DESC sd)430 trace_install (SIM_DESC sd)
431 {
432   int i;
433 
434   SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
435 
436   sim_add_option_table (sd, NULL, trace_options);
437   memset (STATE_TRACE_DATA (sd), 0, sizeof (* STATE_TRACE_DATA (sd)));
438   for (i = 0; i < MAX_NR_PROCESSORS; ++i)
439     memset (CPU_TRACE_DATA (STATE_CPU (sd, i)), 0,
440 	    sizeof (* CPU_TRACE_DATA (STATE_CPU (sd, i))));
441   sim_module_add_init_fn (sd, trace_init);
442   sim_module_add_uninstall_fn (sd, trace_uninstall);
443   return SIM_RC_OK;
444 }
445 
446 static SIM_RC
trace_init(SIM_DESC sd)447 trace_init (SIM_DESC sd)
448 {
449 #ifdef SIM_HAVE_ADDR_RANGE
450   /* Check if a range has been specified without specifying what to
451      collect.  */
452   {
453     int i;
454 
455     for (i = 0; i < MAX_NR_PROCESSORS; ++i)
456       {
457 	sim_cpu *cpu = STATE_CPU (sd, i);
458 
459 	if (ADDR_RANGE_RANGES (TRACE_RANGE (CPU_TRACE_DATA (cpu)))
460 	    && ! TRACE_INSN_P (cpu))
461 	  {
462 	    sim_io_eprintf_cpu (cpu, "Tracing address range specified without --trace-insn.\n");
463 	    sim_io_eprintf_cpu (cpu, "Address range ignored.\n");
464 	    sim_addr_range_delete (TRACE_RANGE (CPU_TRACE_DATA (cpu)),
465 				   0, ~ (address_word) 0);
466 	  }
467       }
468   }
469 #endif
470 
471   return SIM_RC_OK;
472 }
473 
474 static void
trace_uninstall(SIM_DESC sd)475 trace_uninstall (SIM_DESC sd)
476 {
477   int i,j;
478   FILE *sfile = TRACE_FILE (STATE_TRACE_DATA (sd));
479 
480   if (sfile != NULL)
481     fclose (sfile);
482 
483   for (i = 0; i < MAX_NR_PROCESSORS; ++i)
484     {
485       FILE *cfile = TRACE_FILE (CPU_TRACE_DATA (STATE_CPU (sd, i)));
486       if (cfile != NULL && cfile != sfile)
487 	{
488 	  /* If output from different cpus is going to the same file,
489 	     avoid closing the file twice.  */
490 	  for (j = 0; j < i; ++j)
491 	    if (TRACE_FILE (CPU_TRACE_DATA (STATE_CPU (sd, j))) == cfile)
492 	      break;
493 	  if (i == j)
494 	    fclose (cfile);
495 	}
496     }
497 }
498 
499 /* compute the nr of trace data units consumed by data */
500 static int
save_data_size(TRACE_DATA * data,long size)501 save_data_size (TRACE_DATA *data,
502 		long size)
503 {
504   return ((size + sizeof (TRACE_INPUT_DATA (data) [0]) - 1)
505 	  / sizeof (TRACE_INPUT_DATA (data) [0]));
506 }
507 
508 
509 /* Archive DATA into the trace buffer */
510 void
save_data(SIM_DESC sd,TRACE_DATA * data,data_fmt fmt,long size,const void * buf)511 save_data (SIM_DESC sd,
512 	   TRACE_DATA *data,
513 	   data_fmt fmt,
514 	   long size,
515 	   const void *buf)
516 {
517   int i = TRACE_INPUT_IDX (data);
518   if (i == sizeof (TRACE_INPUT_FMT (data)))
519     sim_io_error (sd, "trace buffer overflow");
520   TRACE_INPUT_FMT (data) [i] = fmt;
521   TRACE_INPUT_SIZE (data) [i] = size;
522   memcpy (&TRACE_INPUT_DATA (data) [i], buf, size);
523   i += save_data_size (data, size);
524   TRACE_INPUT_IDX (data) = i;
525 }
526 
527 static void
print_data(SIM_DESC sd,sim_cpu * cpu,data_fmt fmt,long size,void * data)528 print_data (SIM_DESC sd,
529 	    sim_cpu *cpu,
530 	    data_fmt fmt,
531 	    long size,
532 	    void *data)
533 {
534   switch (fmt)
535     {
536     case trace_fmt_instruction_incomplete:
537       trace_printf (sd, cpu, " (instruction incomplete)");
538       break;
539     case trace_fmt_word:
540     case trace_fmt_addr:
541       {
542 	switch (size)
543 	  {
544 	  case sizeof (unsigned32):
545 	    trace_printf (sd, cpu, " 0x%08lx", (long) * (unsigned32*) data);
546 	    break;
547 	  case sizeof (unsigned64):
548 	    trace_printf (sd, cpu, " 0x%08lx%08lx",
549 			  (long) ((* (unsigned64*) data) >> 32),
550 			  (long) * (unsigned64*) data);
551 	    break;
552 	  default:
553 	    abort ();
554 	  }
555 	break;
556       }
557     case trace_fmt_bool:
558       {
559 	SIM_ASSERT (size == sizeof (int));
560 	trace_printf (sd, cpu, " %-8s",
561 		      (* (int*) data) ? "true" : "false");
562 	break;
563       }
564     case trace_fmt_fp:
565       {
566 	sim_fpu fp;
567 	switch (size)
568 	  {
569 	    /* FIXME: Assumes sizeof float == 4; sizeof double == 8 */
570 	  case 4:
571 	    sim_fpu_32to (&fp, *(unsigned32*)data);
572 	    break;
573 	  case 8:
574 	    sim_fpu_64to (&fp, *(unsigned64*)data);
575 	    break;
576 	  default:
577 	    abort ();
578 	  }
579 	trace_printf (sd, cpu, " %8g", sim_fpu_2d (&fp));
580 	switch (size)
581 	  {
582 	  case 4:
583 	    trace_printf (sd, cpu, " (0x%08lx)",
584 			  (long) *(unsigned32*)data);
585 	    break;
586 	  case 8:
587 	    trace_printf (sd, cpu, " (0x%08lx%08lx)",
588 			  (long) (*(unsigned64*)data >> 32),
589 			  (long) (*(unsigned64*)data));
590 	    break;
591 	  default:
592 	    abort ();
593 	  }
594 	break;
595       }
596     case trace_fmt_fpu:
597       /* FIXME: At present sim_fpu data is stored as a double */
598       trace_printf (sd, cpu, " %8g", * (double*) data);
599       break;
600     case trace_fmt_string:
601       trace_printf (sd, cpu, " %-8s", (char*) data);
602       break;
603     default:
604       abort ();
605     }
606 }
607 
608 static const char *
trace_idx_to_str(int trace_idx)609 trace_idx_to_str (int trace_idx)
610 {
611   static char num[8];
612   switch (trace_idx)
613     {
614     case TRACE_ALU_IDX:     return "alu:     ";
615     case TRACE_INSN_IDX:    return "insn:    ";
616     case TRACE_DECODE_IDX:  return "decode:  ";
617     case TRACE_EXTRACT_IDX: return "extract: ";
618     case TRACE_MEMORY_IDX:  return "memory:  ";
619     case TRACE_CORE_IDX:    return "core:    ";
620     case TRACE_EVENTS_IDX:  return "events:  ";
621     case TRACE_FPU_IDX:     return "fpu:     ";
622     case TRACE_BRANCH_IDX:  return "branch:  ";
623     case TRACE_SYSCALL_IDX: return "syscall: ";
624     case TRACE_VPU_IDX:     return "vpu:     ";
625     default:
626       sprintf (num, "?%d?", trace_idx);
627       return num;
628     }
629 }
630 
631 static void
trace_results(SIM_DESC sd,sim_cpu * cpu,int trace_idx,int last_input)632 trace_results (SIM_DESC sd,
633 	       sim_cpu *cpu,
634 	       int trace_idx,
635 	       int last_input)
636 {
637   TRACE_DATA *data = CPU_TRACE_DATA (cpu);
638   int nr_out;
639   int i;
640 
641   /* cross check trace_idx against TRACE_IDX (data)? */
642 
643   /* prefix */
644   trace_printf (sd, cpu, "%s %s",
645 		trace_idx_to_str (TRACE_IDX (data)),
646 		TRACE_PREFIX (data));
647   TRACE_IDX (data) = 0;
648 
649   for (i = 0, nr_out = 0;
650        i < TRACE_INPUT_IDX (data);
651        i += save_data_size (data, TRACE_INPUT_SIZE (data) [i]), nr_out++)
652     {
653       if (i == last_input)
654 	{
655 	  int pad = (strlen (" 0x") + sizeof (unsigned_word) * 2);
656 	  int padding = pad * (3 - nr_out);
657 	  if (padding < 0)
658 	    padding = 0;
659 	  padding += strlen (" ::");
660 	  trace_printf (sd, cpu, "%*s", padding, " ::");
661 	}
662       print_data (sd, cpu,
663 		  TRACE_INPUT_FMT (data) [i],
664 		  TRACE_INPUT_SIZE (data) [i],
665 		  &TRACE_INPUT_DATA (data) [i]);
666     }
667   trace_printf (sd, cpu, "\n");
668 }
669 
670 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,...)671 trace_prefix (SIM_DESC sd,
672 	      sim_cpu *cpu,
673 	      sim_cia cia,
674 	      address_word pc,
675 	      int line_p,
676 	      const char *filename,
677 	      int linenum,
678 	      const char *fmt,
679 	      ...)
680 {
681   TRACE_DATA *data = CPU_TRACE_DATA (cpu);
682   va_list ap;
683   char *prefix = TRACE_PREFIX (data);
684   char *chp;
685  /* FIXME: The TRACE_PREFIX_WIDTH should be determined at build time using
686     known information about the disassembled instructions. */
687 #ifndef TRACE_PREFIX_WIDTH
688 #define TRACE_PREFIX_WIDTH 48
689 #endif
690   int width = TRACE_PREFIX_WIDTH;
691 
692   /* if the previous trace data wasn't flushed, flush it now with a
693      note indicating that the trace was incomplete. */
694   if (TRACE_IDX (data) != 0)
695     {
696       int last_input = TRACE_INPUT_IDX (data);
697       save_data (sd, data, trace_fmt_instruction_incomplete, 1, "");
698       trace_results (sd, cpu, TRACE_IDX (data), last_input);
699     }
700   TRACE_IDX (data) = 0;
701   TRACE_INPUT_IDX (data) = 0;
702 
703   /* Create the text prefix for this new instruction: */
704   if (!line_p)
705     {
706       if (filename)
707 	{
708 	  sprintf (prefix, "%s:%-*d 0x%.*lx ",
709 		   filename,
710 		   SIZE_LINE_NUMBER, linenum,
711 		   SIZE_PC, (long) pc);
712 	}
713       else
714 	{
715 	  sprintf (prefix, "0x%.*lx ",
716 		   SIZE_PC, (long) pc);
717 	  /* Shrink the width by the amount that we didn't print.  */
718 	  width -= SIZE_LINE_NUMBER + SIZE_PC + 8;
719 	}
720       chp = strchr (prefix, '\0');
721       va_start (ap, fmt);
722       vsprintf (chp, fmt, ap);
723       va_end (ap);
724     }
725   else
726     {
727       char buf[256];
728       buf[0] = 0;
729       if (STATE_TEXT_SECTION (CPU_STATE (cpu))
730 	  && pc >= STATE_TEXT_START (CPU_STATE (cpu))
731 	  && pc < STATE_TEXT_END (CPU_STATE (cpu)))
732 	{
733 	  const char *pc_filename = (const char *)0;
734 	  const char *pc_function = (const char *)0;
735 	  unsigned int pc_linenum = 0;
736 	  bfd *abfd;
737 	  asymbol **asymbols;
738 
739 	  abfd = STATE_PROG_BFD (CPU_STATE (cpu));
740 	  asymbols = STATE_PROG_SYMS (CPU_STATE (cpu));
741 	  if (asymbols == NULL)
742 	    {
743 	      long symsize;
744 	      long symbol_count;
745 
746 	      symsize = bfd_get_symtab_upper_bound (abfd);
747 	      if (symsize < 0)
748 		{
749 		  sim_engine_abort (sd, cpu, cia, "could not read symbols");
750 		}
751 	      asymbols = (asymbol **) xmalloc (symsize);
752 	      symbol_count = bfd_canonicalize_symtab (abfd, asymbols);
753 	      if (symbol_count < 0)
754 		{
755 		  sim_engine_abort (sd, cpu, cia, "could not canonicalize symbols");
756 		}
757 	      STATE_PROG_SYMS (CPU_STATE (cpu)) = asymbols;
758 	    }
759 
760 	  if (bfd_find_nearest_line (abfd,
761 				     STATE_TEXT_SECTION (CPU_STATE (cpu)),
762 				     asymbols,
763 				     pc - STATE_TEXT_START (CPU_STATE (cpu)),
764 				     &pc_filename, &pc_function, &pc_linenum))
765 	    {
766 	      char *p = buf;
767 	      if (pc_linenum)
768 		{
769 		  sprintf (p, "#%-*d ", SIZE_LINE_NUMBER, pc_linenum);
770 		  p += strlen (p);
771 		}
772 	      else
773 		{
774 		  sprintf (p, "%-*s ", SIZE_LINE_NUMBER+1, "---");
775 		  p += SIZE_LINE_NUMBER+2;
776 		}
777 
778 	      if (pc_function)
779 		{
780 		  sprintf (p, "%s ", pc_function);
781 		  p += strlen (p);
782 		}
783 	      else if (pc_filename)
784 		{
785 		  char *q = (char *) strrchr (pc_filename, '/');
786 		  sprintf (p, "%s ", (q) ? q+1 : pc_filename);
787 		  p += strlen (p);
788 		}
789 
790 	      if (*p == ' ')
791 		*p = '\0';
792 	    }
793 	}
794 
795       sprintf (prefix, "0x%.*x %-*.*s ",
796 	       SIZE_PC, (unsigned) pc,
797 	       SIZE_LOCATION, SIZE_LOCATION, buf);
798       chp = strchr (prefix, '\0');
799       va_start (ap, fmt);
800       vsprintf (chp, fmt, ap);
801       va_end (ap);
802     }
803 
804   /* Pad it out to TRACE_PREFIX_WIDTH.  */
805   chp = strchr (prefix, '\0');
806   if (chp - prefix < width)
807     {
808       memset (chp, ' ', width - (chp - prefix));
809       chp = &prefix [width];
810       *chp = '\0';
811     }
812   strcpy (chp, " -");
813 
814   /* check that we've not over flowed the prefix buffer */
815   if (strlen (prefix) >= sizeof (TRACE_PREFIX (data)))
816     abort ();
817 }
818 
819 void
trace_generic(SIM_DESC sd,sim_cpu * cpu,int trace_idx,const char * fmt,...)820 trace_generic (SIM_DESC sd,
821 	       sim_cpu *cpu,
822 	       int trace_idx,
823 	       const char *fmt,
824 	       ...)
825 {
826   va_list ap;
827   trace_printf (sd, cpu, "%s %s",
828 		trace_idx_to_str (trace_idx),
829 		TRACE_PREFIX (CPU_TRACE_DATA (cpu)));
830   va_start (ap, fmt);
831   trace_vprintf (sd, cpu, fmt, ap);
832   va_end (ap);
833   trace_printf (sd, cpu, "\n");
834 }
835 
836 void
trace_input0(SIM_DESC sd,sim_cpu * cpu,int trace_idx)837 trace_input0 (SIM_DESC sd,
838 	      sim_cpu *cpu,
839 	      int trace_idx)
840 {
841   TRACE_DATA *data = CPU_TRACE_DATA (cpu);
842   TRACE_IDX (data) = trace_idx;
843 }
844 
845 void
trace_input_word1(SIM_DESC sd,sim_cpu * cpu,int trace_idx,unsigned_word d0)846 trace_input_word1 (SIM_DESC sd,
847 		   sim_cpu *cpu,
848 		   int trace_idx,
849 		   unsigned_word d0)
850 {
851   TRACE_DATA *data = CPU_TRACE_DATA (cpu);
852   TRACE_IDX (data) = trace_idx;
853   save_data (sd, data, trace_fmt_word, sizeof (unsigned_word), &d0);
854 }
855 
856 void
trace_input_word2(SIM_DESC sd,sim_cpu * cpu,int trace_idx,unsigned_word d0,unsigned_word d1)857 trace_input_word2 (SIM_DESC sd,
858 		   sim_cpu *cpu,
859 		   int trace_idx,
860 		   unsigned_word d0,
861 		   unsigned_word d1)
862 {
863   TRACE_DATA *data = CPU_TRACE_DATA (cpu);
864   TRACE_IDX (data) = trace_idx;
865   save_data (sd, data, trace_fmt_word, sizeof (unsigned_word), &d0);
866   save_data (sd, data, trace_fmt_word, sizeof (unsigned_word), &d1);
867 }
868 
869 void
trace_input_word3(SIM_DESC sd,sim_cpu * cpu,int trace_idx,unsigned_word d0,unsigned_word d1,unsigned_word d2)870 trace_input_word3 (SIM_DESC sd,
871 		   sim_cpu *cpu,
872 		   int trace_idx,
873 		   unsigned_word d0,
874 		   unsigned_word d1,
875 		   unsigned_word d2)
876 {
877   TRACE_DATA *data = CPU_TRACE_DATA (cpu);
878   TRACE_IDX (data) = trace_idx;
879   save_data (sd, data, trace_fmt_word, sizeof (unsigned_word), &d0);
880   save_data (sd, data, trace_fmt_word, sizeof (unsigned_word), &d1);
881   save_data (sd, data, trace_fmt_word, sizeof (unsigned_word), &d2);
882 }
883 
884 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)885 trace_input_word4 (SIM_DESC sd,
886 		   sim_cpu *cpu,
887 		   int trace_idx,
888 		   unsigned_word d0,
889 		   unsigned_word d1,
890 		   unsigned_word d2,
891 		   unsigned_word d3)
892 {
893   TRACE_DATA *data = CPU_TRACE_DATA (cpu);
894   TRACE_IDX (data) = trace_idx;
895   save_data (sd, data, trace_fmt_word, sizeof (d0), &d0);
896   save_data (sd, data, trace_fmt_word, sizeof (d1), &d1);
897   save_data (sd, data, trace_fmt_word, sizeof (d2), &d2);
898   save_data (sd, data, trace_fmt_word, sizeof (d3), &d3);
899 }
900 
901 void
trace_input_bool1(SIM_DESC sd,sim_cpu * cpu,int trace_idx,int d0)902 trace_input_bool1 (SIM_DESC sd,
903 		   sim_cpu *cpu,
904 		   int trace_idx,
905 		   int d0)
906 {
907   TRACE_DATA *data = CPU_TRACE_DATA (cpu);
908   TRACE_IDX (data) = trace_idx;
909   save_data (sd, data, trace_fmt_bool, sizeof (d0), &d0);
910 }
911 
912 void
trace_input_addr1(SIM_DESC sd,sim_cpu * cpu,int trace_idx,address_word d0)913 trace_input_addr1 (SIM_DESC sd,
914 		   sim_cpu *cpu,
915 		   int trace_idx,
916 		   address_word d0)
917 {
918   TRACE_DATA *data = CPU_TRACE_DATA (cpu);
919   TRACE_IDX (data) = trace_idx;
920   save_data (sd, data, trace_fmt_addr, sizeof (d0), &d0);
921 }
922 
923 void
trace_input_fp1(SIM_DESC sd,sim_cpu * cpu,int trace_idx,fp_word f0)924 trace_input_fp1 (SIM_DESC sd,
925 		 sim_cpu *cpu,
926 		 int trace_idx,
927 		 fp_word f0)
928 {
929   TRACE_DATA *data = CPU_TRACE_DATA (cpu);
930   TRACE_IDX (data) = trace_idx;
931   save_data (sd, data, trace_fmt_fp, sizeof (fp_word), &f0);
932 }
933 
934 void
trace_input_fp2(SIM_DESC sd,sim_cpu * cpu,int trace_idx,fp_word f0,fp_word f1)935 trace_input_fp2 (SIM_DESC sd,
936 		 sim_cpu *cpu,
937 		 int trace_idx,
938 		 fp_word f0,
939 		 fp_word f1)
940 {
941   TRACE_DATA *data = CPU_TRACE_DATA (cpu);
942   TRACE_IDX (data) = trace_idx;
943   save_data (sd, data, trace_fmt_fp, sizeof (fp_word), &f0);
944   save_data (sd, data, trace_fmt_fp, sizeof (fp_word), &f1);
945 }
946 
947 void
trace_input_fp3(SIM_DESC sd,sim_cpu * cpu,int trace_idx,fp_word f0,fp_word f1,fp_word f2)948 trace_input_fp3 (SIM_DESC sd,
949 		 sim_cpu *cpu,
950 		 int trace_idx,
951 		 fp_word f0,
952 		 fp_word f1,
953 		 fp_word f2)
954 {
955   TRACE_DATA *data = CPU_TRACE_DATA (cpu);
956   TRACE_IDX (data) = trace_idx;
957   save_data (sd, data, trace_fmt_fp, sizeof (fp_word), &f0);
958   save_data (sd, data, trace_fmt_fp, sizeof (fp_word), &f1);
959   save_data (sd, data, trace_fmt_fp, sizeof (fp_word), &f2);
960 }
961 
962 void
trace_input_fpu1(SIM_DESC sd,sim_cpu * cpu,int trace_idx,sim_fpu * f0)963 trace_input_fpu1 (SIM_DESC sd,
964 		  sim_cpu *cpu,
965 		  int trace_idx,
966 		  sim_fpu *f0)
967 {
968   double d;
969   TRACE_DATA *data = CPU_TRACE_DATA (cpu);
970   TRACE_IDX (data) = trace_idx;
971   d = sim_fpu_2d (f0);
972   save_data (sd, data, trace_fmt_fp, sizeof (double), &d);
973 }
974 
975 void
trace_input_fpu2(SIM_DESC sd,sim_cpu * cpu,int trace_idx,sim_fpu * f0,sim_fpu * f1)976 trace_input_fpu2 (SIM_DESC sd,
977 		  sim_cpu *cpu,
978 		  int trace_idx,
979 		  sim_fpu *f0,
980 		  sim_fpu *f1)
981 {
982   double d;
983   TRACE_DATA *data = CPU_TRACE_DATA (cpu);
984   TRACE_IDX (data) = trace_idx;
985   d = sim_fpu_2d (f0);
986   save_data (sd, data, trace_fmt_fp, sizeof (double), &d);
987   d = sim_fpu_2d (f1);
988   save_data (sd, data, trace_fmt_fp, sizeof (double), &d);
989 }
990 
991 void
trace_input_fpu3(SIM_DESC sd,sim_cpu * cpu,int trace_idx,sim_fpu * f0,sim_fpu * f1,sim_fpu * f2)992 trace_input_fpu3 (SIM_DESC sd,
993 		  sim_cpu *cpu,
994 		  int trace_idx,
995 		  sim_fpu *f0,
996 		  sim_fpu *f1,
997 		  sim_fpu *f2)
998 {
999   double d;
1000   TRACE_DATA *data = CPU_TRACE_DATA (cpu);
1001   TRACE_IDX (data) = trace_idx;
1002   d = sim_fpu_2d (f0);
1003   save_data (sd, data, trace_fmt_fp, sizeof (double), &d);
1004   d = sim_fpu_2d (f1);
1005   save_data (sd, data, trace_fmt_fp, sizeof (double), &d);
1006   d = sim_fpu_2d (f2);
1007   save_data (sd, data, trace_fmt_fp, sizeof (double), &d);
1008 }
1009 
1010 void
trace_result_word1(SIM_DESC sd,sim_cpu * cpu,int trace_idx,unsigned_word r0)1011 trace_result_word1 (SIM_DESC sd,
1012 		    sim_cpu *cpu,
1013 		    int trace_idx,
1014 		    unsigned_word r0)
1015 {
1016   TRACE_DATA *data = CPU_TRACE_DATA (cpu);
1017   int last_input;
1018 
1019   /* Append any results to the end of the inputs */
1020   last_input = TRACE_INPUT_IDX (data);
1021   save_data (sd, data, trace_fmt_word, sizeof (unsigned_word), &r0);
1022 
1023   trace_results (sd, cpu, trace_idx, last_input);
1024 }
1025 
1026 void
trace_result0(SIM_DESC sd,sim_cpu * cpu,int trace_idx)1027 trace_result0 (SIM_DESC sd,
1028 	       sim_cpu *cpu,
1029 	       int trace_idx)
1030 {
1031   TRACE_DATA *data = CPU_TRACE_DATA (cpu);
1032   int last_input;
1033 
1034   /* Append any results to the end of the inputs */
1035   last_input = TRACE_INPUT_IDX (data);
1036 
1037   trace_results (sd, cpu, trace_idx, last_input);
1038 }
1039 
1040 void
trace_result_word2(SIM_DESC sd,sim_cpu * cpu,int trace_idx,unsigned_word r0,unsigned_word r1)1041 trace_result_word2 (SIM_DESC sd,
1042 		    sim_cpu *cpu,
1043 		    int trace_idx,
1044 		    unsigned_word r0,
1045 		    unsigned_word r1)
1046 {
1047   TRACE_DATA *data = CPU_TRACE_DATA (cpu);
1048   int last_input;
1049 
1050   /* Append any results to the end of the inputs */
1051   last_input = TRACE_INPUT_IDX (data);
1052   save_data (sd, data, trace_fmt_word, sizeof (r0), &r0);
1053   save_data (sd, data, trace_fmt_word, sizeof (r1), &r1);
1054 
1055   trace_results (sd, cpu, trace_idx, last_input);
1056 }
1057 
1058 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)1059 trace_result_word4 (SIM_DESC sd,
1060 		    sim_cpu *cpu,
1061 		    int trace_idx,
1062 		    unsigned_word r0,
1063 		    unsigned_word r1,
1064 		    unsigned_word r2,
1065 		    unsigned_word r3)
1066 {
1067   TRACE_DATA *data = CPU_TRACE_DATA (cpu);
1068   int last_input;
1069 
1070   /* Append any results to the end of the inputs */
1071   last_input = TRACE_INPUT_IDX (data);
1072   save_data (sd, data, trace_fmt_word, sizeof (r0), &r0);
1073   save_data (sd, data, trace_fmt_word, sizeof (r1), &r1);
1074   save_data (sd, data, trace_fmt_word, sizeof (r2), &r2);
1075   save_data (sd, data, trace_fmt_word, sizeof (r3), &r3);
1076 
1077   trace_results (sd, cpu, trace_idx, last_input);
1078 }
1079 
1080 void
trace_result_bool1(SIM_DESC sd,sim_cpu * cpu,int trace_idx,int r0)1081 trace_result_bool1 (SIM_DESC sd,
1082 		    sim_cpu *cpu,
1083 		    int trace_idx,
1084 		    int r0)
1085 {
1086   TRACE_DATA *data = CPU_TRACE_DATA (cpu);
1087   int last_input;
1088 
1089   /* Append any results to the end of the inputs */
1090   last_input = TRACE_INPUT_IDX (data);
1091   save_data (sd, data, trace_fmt_bool, sizeof (r0), &r0);
1092 
1093   trace_results (sd, cpu, trace_idx, last_input);
1094 }
1095 
1096 void
trace_result_addr1(SIM_DESC sd,sim_cpu * cpu,int trace_idx,address_word r0)1097 trace_result_addr1 (SIM_DESC sd,
1098 		    sim_cpu *cpu,
1099 		    int trace_idx,
1100 		    address_word r0)
1101 {
1102   TRACE_DATA *data = CPU_TRACE_DATA (cpu);
1103   int last_input;
1104 
1105   /* Append any results to the end of the inputs */
1106   last_input = TRACE_INPUT_IDX (data);
1107   save_data (sd, data, trace_fmt_addr, sizeof (r0), &r0);
1108 
1109   trace_results (sd, cpu, trace_idx, last_input);
1110 }
1111 
1112 void
trace_result_fp1(SIM_DESC sd,sim_cpu * cpu,int trace_idx,fp_word f0)1113 trace_result_fp1 (SIM_DESC sd,
1114 		  sim_cpu *cpu,
1115 		  int trace_idx,
1116 		  fp_word f0)
1117 {
1118   TRACE_DATA *data = CPU_TRACE_DATA (cpu);
1119   int last_input;
1120 
1121   /* Append any results to the end of the inputs */
1122   last_input = TRACE_INPUT_IDX (data);
1123   save_data (sd, data, trace_fmt_fp, sizeof (fp_word), &f0);
1124 
1125   trace_results (sd, cpu, trace_idx, last_input);
1126 }
1127 
1128 void
trace_result_fp2(SIM_DESC sd,sim_cpu * cpu,int trace_idx,fp_word f0,fp_word f1)1129 trace_result_fp2 (SIM_DESC sd,
1130 		  sim_cpu *cpu,
1131 		  int trace_idx,
1132 		  fp_word f0,
1133 		  fp_word f1)
1134 {
1135   TRACE_DATA *data = CPU_TRACE_DATA (cpu);
1136   int last_input;
1137 
1138   /* Append any results to the end of the inputs */
1139   last_input = TRACE_INPUT_IDX (data);
1140   save_data (sd, data, trace_fmt_fp, sizeof (f0), &f0);
1141   save_data (sd, data, trace_fmt_fp, sizeof (f1), &f1);
1142 
1143   trace_results (sd, cpu, trace_idx, last_input);
1144 }
1145 
1146 void
trace_result_fpu1(SIM_DESC sd,sim_cpu * cpu,int trace_idx,sim_fpu * f0)1147 trace_result_fpu1 (SIM_DESC sd,
1148 		   sim_cpu *cpu,
1149 		   int trace_idx,
1150 		   sim_fpu *f0)
1151 {
1152   double d;
1153   TRACE_DATA *data = CPU_TRACE_DATA (cpu);
1154   int last_input;
1155 
1156   /* Append any results to the end of the inputs */
1157   last_input = TRACE_INPUT_IDX (data);
1158   d = sim_fpu_2d (f0);
1159   save_data (sd, data, trace_fmt_fp, sizeof (double), &d);
1160 
1161   trace_results (sd, cpu, trace_idx, last_input);
1162 }
1163 
1164 void
trace_result_string1(SIM_DESC sd,sim_cpu * cpu,int trace_idx,char * s0)1165 trace_result_string1 (SIM_DESC sd,
1166 		      sim_cpu *cpu,
1167 		      int trace_idx,
1168 		      char *s0)
1169 {
1170   TRACE_DATA *data = CPU_TRACE_DATA (cpu);
1171   int last_input;
1172 
1173   /* Append any results to the end of the inputs */
1174   last_input = TRACE_INPUT_IDX (data);
1175   save_data (sd, data, trace_fmt_string, strlen (s0) + 1, s0);
1176 
1177   trace_results (sd, cpu, trace_idx, last_input);
1178 }
1179 
1180 void
trace_result_word1_string1(SIM_DESC sd,sim_cpu * cpu,int trace_idx,unsigned_word r0,char * s0)1181 trace_result_word1_string1 (SIM_DESC sd,
1182 			    sim_cpu *cpu,
1183 			    int trace_idx,
1184 			    unsigned_word r0,
1185 			    char *s0)
1186 {
1187   TRACE_DATA *data = CPU_TRACE_DATA (cpu);
1188   int last_input;
1189 
1190   /* Append any results to the end of the inputs */
1191   last_input = TRACE_INPUT_IDX (data);
1192   save_data (sd, data, trace_fmt_word, sizeof (unsigned_word), &r0);
1193   save_data (sd, data, trace_fmt_string, strlen (s0) + 1, s0);
1194 
1195   trace_results (sd, cpu, trace_idx, last_input);
1196 }
1197 
1198 void
trace_vprintf(SIM_DESC sd,sim_cpu * cpu,const char * fmt,va_list ap)1199 trace_vprintf (SIM_DESC sd, sim_cpu *cpu, const char *fmt, va_list ap)
1200 {
1201   if (cpu != NULL)
1202     {
1203       if (TRACE_FILE (CPU_TRACE_DATA (cpu)) != NULL)
1204 	vfprintf (TRACE_FILE (CPU_TRACE_DATA (cpu)), fmt, ap);
1205       else
1206 	sim_io_evprintf (sd, fmt, ap);
1207     }
1208   else
1209     {
1210       if (TRACE_FILE (STATE_TRACE_DATA (sd)) != NULL)
1211 	vfprintf (TRACE_FILE (STATE_TRACE_DATA (sd)), fmt, ap);
1212       else
1213 	sim_io_evprintf (sd, fmt, ap);
1214     }
1215 }
1216 
1217 /* The function trace_one_insn has been replaced by the function pair
1218    trace_prefix() + trace_generic().  It is still used. */
1219 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,...)1220 trace_one_insn (SIM_DESC sd, sim_cpu *cpu, address_word pc,
1221 		int line_p, const char *filename, int linenum,
1222 		const char *phase_wo_colon, const char *fmt,
1223 		...)
1224 {
1225   va_list ap;
1226   char phase[SIZE_PHASE+2];
1227 
1228   strncpy (phase, phase_wo_colon, SIZE_PHASE);
1229   strcat (phase, ":");
1230 
1231   if (!line_p)
1232     {
1233       trace_printf (sd, cpu, "%-*s %s:%-*d 0x%.*lx ",
1234 		    SIZE_PHASE+1, phase,
1235 		    filename,
1236 		    SIZE_LINE_NUMBER, linenum,
1237 		    SIZE_PC, (long)pc);
1238       va_start (ap, fmt);
1239       trace_vprintf (sd, cpu, fmt, ap);
1240       va_end (ap);
1241       trace_printf (sd, cpu, "\n");
1242     }
1243   else
1244     {
1245       char buf[256];
1246 
1247       buf[0] = 0;
1248       if (STATE_TEXT_SECTION (CPU_STATE (cpu))
1249 	  && pc >= STATE_TEXT_START (CPU_STATE (cpu))
1250 	  && pc < STATE_TEXT_END (CPU_STATE (cpu)))
1251 	{
1252 	  const char *pc_filename = (const char *)0;
1253 	  const char *pc_function = (const char *)0;
1254 	  unsigned int pc_linenum = 0;
1255 
1256 	  if (bfd_find_nearest_line (STATE_PROG_BFD (CPU_STATE (cpu)),
1257 				     STATE_TEXT_SECTION (CPU_STATE (cpu)),
1258 				     (struct bfd_symbol **) 0,
1259 				     pc - STATE_TEXT_START (CPU_STATE (cpu)),
1260 				     &pc_filename, &pc_function, &pc_linenum))
1261 	    {
1262 	      char *p = buf;
1263 	      if (pc_linenum)
1264 		{
1265 		  sprintf (p, "#%-*d ", SIZE_LINE_NUMBER, pc_linenum);
1266 		  p += strlen (p);
1267 		}
1268 	      else
1269 		{
1270 		  sprintf (p, "%-*s ", SIZE_LINE_NUMBER+1, "---");
1271 		  p += SIZE_LINE_NUMBER+2;
1272 		}
1273 
1274 	      if (pc_function)
1275 		{
1276 		  sprintf (p, "%s ", pc_function);
1277 		  p += strlen (p);
1278 		}
1279 	      else if (pc_filename)
1280 		{
1281 		  char *q = (char *) strrchr (pc_filename, '/');
1282 		  sprintf (p, "%s ", (q) ? q+1 : pc_filename);
1283 		  p += strlen (p);
1284 		}
1285 
1286 	      if (*p == ' ')
1287 		*p = '\0';
1288 	    }
1289 	}
1290 
1291       trace_printf (sd, cpu, "%-*s 0x%.*x %-*.*s ",
1292 		    SIZE_PHASE+1, phase,
1293 		    SIZE_PC, (unsigned) pc,
1294 		    SIZE_LOCATION, SIZE_LOCATION, buf);
1295       va_start (ap, fmt);
1296       trace_vprintf (sd, cpu, fmt, ap);
1297       va_end (ap);
1298       trace_printf (sd, cpu, "\n");
1299     }
1300 }
1301 
1302 void
trace_printf(SIM_DESC sd,sim_cpu * cpu,const char * fmt,...)1303 trace_printf VPARAMS ((SIM_DESC sd, sim_cpu *cpu, const char *fmt, ...))
1304 {
1305 #if !defined __STDC__ && !defined ALMOST_STDC
1306   SIM_DESC sd;
1307   sim_cpu *cpu;
1308   const char *fmt;
1309 #endif
1310   va_list ap;
1311 
1312   VA_START (ap, fmt);
1313 #if !defined __STDC__ && !defined ALMOST_STDC
1314   sd = va_arg (ap, SIM_DESC);
1315   cpu = va_arg (ap, sim_cpu *);
1316   fmt = va_arg (ap, const char *);
1317 #endif
1318 
1319   trace_vprintf (sd, cpu, fmt, ap);
1320 
1321   va_end (ap);
1322 }
1323 
1324 void
debug_printf(sim_cpu * cpu,const char * fmt,...)1325 debug_printf VPARAMS ((sim_cpu *cpu, const char *fmt, ...))
1326 {
1327 #if !defined __STDC__ && !defined ALMOST_STDC
1328   sim_cpu *cpu;
1329   const char *fmt;
1330 #endif
1331   va_list ap;
1332 
1333   VA_START (ap, fmt);
1334 #if !defined __STDC__ && !defined ALMOST_STDC
1335   cpu = va_arg (ap, sim_cpu *);
1336   fmt = va_arg (ap, const char *);
1337 #endif
1338 
1339   if (CPU_DEBUG_FILE (cpu) == NULL)
1340     (* STATE_CALLBACK (CPU_STATE (cpu))->evprintf_filtered)
1341       (STATE_CALLBACK (CPU_STATE (cpu)), fmt, ap);
1342   else
1343     vfprintf (CPU_DEBUG_FILE (cpu), fmt, ap);
1344 
1345   va_end (ap);
1346 }
1347