i8254: Factor out pit_get_channel_info
[qemu/opensuse.git] / scripts / tracetool
blob4c9951d0aaf9e1ec2d2506467466a592b340f26b
1 #!/bin/sh
3 # Code generator for trace events
5 # Copyright IBM, Corp. 2010
7 # This work is licensed under the terms of the GNU GPL, version 2. See
8 # the COPYING file in the top-level directory.
10 # Disable pathname expansion, makes processing text with '*' characters simpler
11 set -f
13 usage()
15 cat >&2 <<EOF
16 usage: $0 [--nop | --simple | --stderr | --ust | --dtrace] [-h | -c]
17 Generate tracing code for a file on stdin.
19 Backends:
20 --nop Tracing disabled
21 --simple Simple built-in backend
22 --stderr Stderr built-in backend
23 --ust LTTng User Space Tracing backend
24 --dtrace DTrace/SystemTAP backend
26 Output formats:
27 -h Generate .h file
28 -c Generate .c file
29 -d Generate .d file (DTrace only)
30 --stap Generate .stp file (DTrace with SystemTAP only)
32 Options:
33 --binary [path] Full path to QEMU binary
34 --target-arch [arch] QEMU emulator target arch
35 --target-type [type] QEMU emulator target type ('system' or 'user')
36 --probe-prefix [prefix] Prefix for dtrace probe names
37 (default: qemu-\$targettype-\$targetarch)
39 EOF
40 exit 1
43 # Print a line without interpreting backslash escapes
45 # The built-in echo command may interpret backslash escapes without an option
46 # to disable this behavior.
47 puts()
49 printf "%s\n" "$1"
52 # Get the name of a trace event
53 get_name()
55 local name
56 name=${1%%\(*}
57 echo "${name##* }"
60 # Get the given property of a trace event
61 # 1: trace-events line
62 # 2: property name
63 # -> return 0 if property is present, or 1 otherwise
64 has_property()
66 local props prop
67 props=${1%%\(*}
68 props=${props% *}
69 for prop in $props; do
70 if [ "$prop" = "$2" ]; then
71 return 0
73 done
74 return 1
77 # Get the argument list of a trace event, including types and names
78 get_args()
80 local args
81 args=${1#*\(}
82 args=${args%%\)*}
83 echo "$args"
86 # Get the argument name list of a trace event
87 get_argnames()
89 local nfields field name sep
90 nfields=0
91 sep="$2"
92 for field in $(get_args "$1"); do
93 nfields=$((nfields + 1))
95 # Drop pointer star
96 field=${field#\*}
98 # Only argument names have commas at the end
99 name=${field%,}
100 test "$field" = "$name" && continue
102 printf "%s%s " $name $sep
103 done
105 # Last argument name
106 if [ "$nfields" -gt 1 ]
107 then
108 printf "%s" "$name"
112 # Get the number of arguments to a trace event
113 get_argc()
115 local name argc
116 argc=0
117 for name in $(get_argnames "$1", ","); do
118 argc=$((argc + 1))
119 done
120 echo $argc
123 # Get the format string including double quotes for a trace event
124 get_fmt()
126 puts "${1#*)}"
129 linetoh_begin_nop()
131 return
134 linetoh_nop()
136 local name args
137 name=$(get_name "$1")
138 args=$(get_args "$1")
140 # Define an empty function for the trace event
141 cat <<EOF
142 static inline void trace_$name($args)
148 linetoh_end_nop()
150 return
153 linetoc_begin_nop()
155 return
158 linetoc_nop()
160 # No need for function definitions in nop backend
161 return
164 linetoc_end_nop()
166 return
169 linetoh_begin_simple()
171 cat <<EOF
172 #include "trace/simple.h"
175 simple_event_num=0
178 cast_args_to_uint64_t()
180 local arg
181 for arg in $(get_argnames "$1", ","); do
182 printf "%s" "(uint64_t)(uintptr_t)$arg"
183 done
186 linetoh_simple()
188 local name args argc trace_args
189 name=$(get_name "$1")
190 args=$(get_args "$1")
191 argc=$(get_argc "$1")
193 trace_args="$simple_event_num"
194 if [ "$argc" -gt 0 ]
195 then
196 trace_args="$trace_args, $(cast_args_to_uint64_t "$1")"
199 cat <<EOF
200 static inline void trace_$name($args)
202 trace$argc($trace_args);
206 simple_event_num=$((simple_event_num + 1))
209 linetoh_end_simple()
211 cat <<EOF
212 #define NR_TRACE_EVENTS $simple_event_num
213 extern TraceEvent trace_list[NR_TRACE_EVENTS];
217 linetoc_begin_simple()
219 cat <<EOF
220 #include "trace.h"
222 TraceEvent trace_list[] = {
224 simple_event_num=0
228 linetoc_simple()
230 local name
231 name=$(get_name "$1")
232 cat <<EOF
233 {.tp_name = "$name", .state=0},
235 simple_event_num=$((simple_event_num + 1))
238 linetoc_end_simple()
240 cat <<EOF
245 #STDERR
246 linetoh_begin_stderr()
248 cat <<EOF
249 #include <stdio.h>
250 #include "trace/stderr.h"
252 extern TraceEvent trace_list[];
255 stderr_event_num=0
258 linetoh_stderr()
260 local name args argnames argc fmt
261 name=$(get_name "$1")
262 args=$(get_args "$1")
263 argnames=$(get_argnames "$1" ",")
264 argc=$(get_argc "$1")
265 fmt=$(get_fmt "$1")
267 if [ "$argc" -gt 0 ]; then
268 argnames=", $argnames"
271 cat <<EOF
272 static inline void trace_$name($args)
274 if (trace_list[$stderr_event_num].state != 0) {
275 fprintf(stderr, "$name " $fmt "\n" $argnames);
279 stderr_event_num=$((stderr_event_num + 1))
283 linetoh_end_stderr()
285 cat <<EOF
286 #define NR_TRACE_EVENTS $stderr_event_num
290 linetoc_begin_stderr()
292 cat <<EOF
293 #include "trace.h"
295 TraceEvent trace_list[] = {
297 stderr_event_num=0
300 linetoc_stderr()
302 local name
303 name=$(get_name "$1")
304 cat <<EOF
305 {.tp_name = "$name", .state=0},
307 stderr_event_num=$(($stderr_event_num + 1))
310 linetoc_end_stderr()
312 cat <<EOF
316 #END OF STDERR
318 # Clean up after UST headers which pollute the namespace
319 ust_clean_namespace() {
320 cat <<EOF
321 #undef mutex_lock
322 #undef mutex_unlock
323 #undef inline
324 #undef wmb
328 linetoh_begin_ust()
330 echo "#include <ust/tracepoint.h>"
331 ust_clean_namespace
334 linetoh_ust()
336 local name args argnames
337 name=$(get_name "$1")
338 args=$(get_args "$1")
339 argnames=$(get_argnames "$1", ",")
341 cat <<EOF
342 DECLARE_TRACE(ust_$name, TP_PROTO($args), TP_ARGS($argnames));
343 #define trace_$name trace_ust_$name
347 linetoh_end_ust()
349 return
352 linetoc_begin_ust()
354 cat <<EOF
355 #include <ust/marker.h>
356 $(ust_clean_namespace)
357 #include "trace.h"
361 linetoc_ust()
363 local name args argnames fmt
364 name=$(get_name "$1")
365 args=$(get_args "$1")
366 argnames=$(get_argnames "$1", ",")
367 [ -z "$argnames" ] || argnames=", $argnames"
368 fmt=$(get_fmt "$1")
370 cat <<EOF
371 DEFINE_TRACE(ust_$name);
373 static void ust_${name}_probe($args)
375 trace_mark(ust, $name, $fmt$argnames);
379 # Collect names for later
380 names="$names $name"
383 linetoc_end_ust()
385 cat <<EOF
386 static void __attribute__((constructor)) trace_init(void)
390 for name in $names; do
391 cat <<EOF
392 register_trace_ust_$name(ust_${name}_probe);
394 done
396 echo "}"
399 linetoh_begin_dtrace()
401 cat <<EOF
402 #include "trace-dtrace.h"
406 linetoh_dtrace()
408 local name args argnames nameupper
409 name=$(get_name "$1")
410 args=$(get_args "$1")
411 argnames=$(get_argnames "$1", ",")
413 nameupper=`echo $name | tr '[:lower:]' '[:upper:]'`
415 # Define an empty function for the trace event
416 cat <<EOF
417 static inline void trace_$name($args) {
418 if (QEMU_${nameupper}_ENABLED()) {
419 QEMU_${nameupper}($argnames);
425 linetoh_end_dtrace()
427 return
430 linetoc_begin_dtrace()
432 return
435 linetoc_dtrace()
437 # No need for function definitions in dtrace backend
438 return
441 linetoc_end_dtrace()
443 return
446 linetod_begin_dtrace()
448 cat <<EOF
449 provider qemu {
453 linetod_dtrace()
455 local name args
456 name=$(get_name "$1")
457 args=$(get_args "$1")
459 # DTrace provider syntax expects foo() for empty
460 # params, not foo(void)
461 if [ "$args" = "void" ]; then
462 args=""
465 # Define prototype for probe arguments
466 cat <<EOF
467 probe $name($args);
471 linetod_end_dtrace()
473 cat <<EOF
478 linetostap_begin_dtrace()
480 return
483 linetostap_dtrace()
485 local i arg name args arglist
486 name=$(get_name "$1")
487 args=$(get_args "$1")
488 arglist=$(get_argnames "$1", "")
490 # Define prototype for probe arguments
491 cat <<EOF
492 probe $probeprefix.$name = process("$binary").mark("$name")
497 for arg in $arglist
499 # 'limit' is a reserved keyword
500 if [ "$arg" = "limit" ]; then
501 arg="_limit"
503 cat <<EOF
504 $arg = \$arg$i;
506 i="$((i+1))"
507 done
509 cat <<EOF
514 linetostap_end_dtrace()
516 return
519 # Process stdin by calling begin, line, and end functions for the backend
520 convert()
522 local begin process_line end str disable
523 begin="lineto$1_begin_$backend"
524 process_line="lineto$1_$backend"
525 end="lineto$1_end_$backend"
527 "$begin"
529 while read -r str; do
530 # Skip comments and empty lines
531 test -z "${str%%#*}" && continue
533 echo
534 # Process the line. The nop backend handles disabled lines.
535 if has_property "$str" "disable"; then
536 "lineto$1_nop" "$str"
537 else
538 "$process_line" "$str"
540 done
542 echo
543 "$end"
546 tracetoh()
548 cat <<EOF
549 #ifndef TRACE_H
550 #define TRACE_H
552 /* This file is autogenerated by tracetool, do not edit. */
554 #include "qemu-common.h"
556 convert h
557 echo "#endif /* TRACE_H */"
560 tracetoc()
562 echo "/* This file is autogenerated by tracetool, do not edit. */"
563 convert c
566 tracetod()
568 if [ $backend != "dtrace" ]; then
569 echo "DTrace probe generator not applicable to $backend backend"
570 exit 1
572 echo "/* This file is autogenerated by tracetool, do not edit. */"
573 convert d
576 tracetostap()
578 if [ $backend != "dtrace" ]; then
579 echo "SystemTAP tapset generator not applicable to $backend backend"
580 exit 1
582 if [ -z "$binary" ]; then
583 echo "--binary is required for SystemTAP tapset generator"
584 exit 1
586 if [ -z "$probeprefix" -a -z "$targettype" ]; then
587 echo "--target-type is required for SystemTAP tapset generator"
588 exit 1
590 if [ -z "$probeprefix" -a -z "$targetarch" ]; then
591 echo "--target-arch is required for SystemTAP tapset generator"
592 exit 1
594 if [ -z "$probeprefix" ]; then
595 probeprefix="qemu.$targettype.$targetarch";
597 echo "/* This file is autogenerated by tracetool, do not edit. */"
598 convert stap
602 backend=
603 output=
604 binary=
605 targettype=
606 targetarch=
607 probeprefix=
610 until [ -z "$1" ]
612 case "$1" in
613 "--nop" | "--simple" | "--stderr" | "--ust" | "--dtrace") backend="${1#--}" ;;
615 "--binary") shift ; binary="$1" ;;
616 "--target-arch") shift ; targetarch="$1" ;;
617 "--target-type") shift ; targettype="$1" ;;
618 "--probe-prefix") shift ; probeprefix="$1" ;;
620 "-h" | "-c" | "-d") output="${1#-}" ;;
621 "--stap") output="${1#--}" ;;
623 "--check-backend") exit 0 ;; # used by ./configure to test for backend
625 "--list-backends") # used by ./configure to list available backends
626 echo "nop simple stderr ust dtrace"
627 exit 0
631 usage;;
632 esac
633 shift
634 done
636 if [ "$backend" = "" -o "$output" = "" ]; then
637 usage
640 gen="traceto$output"
641 "$gen"
643 exit 0