trace: Add trace-events file for declaring trace events
[qemu/stefanha.git] / tracetool
blob01de580c445dfd9a7ff045114f2518c9af8193d5
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 [-h | -c]
17 Generate tracing code for a file on stdin.
19 Backends:
20 --nop Tracing disabled
22 Output formats:
23 -h Generate .h file
24 -c Generate .c file
25 EOF
26 exit 1
29 # Get the name of a trace event
30 get_name()
32 echo ${1%%\(*}
35 # Get the argument list of a trace event, including types and names
36 get_args()
38 local args
39 args=${1#*\(}
40 args=${args%)*}
41 echo "$args"
44 # Get the argument name list of a trace event
45 get_argnames()
47 local nfields field name
48 nfields=0
49 for field in $(get_args "$1"); do
50 nfields=$((nfields + 1))
52 # Drop pointer star
53 field=${field#\*}
55 # Only argument names have commas at the end
56 name=${field%,}
57 test "$field" = "$name" && continue
59 printf "%s" "$name, "
60 done
62 # Last argument name
63 if [ "$nfields" -gt 1 ]
64 then
65 printf "%s" "$name"
69 # Get the format string for a trace event
70 get_fmt()
72 local fmt
73 fmt=${1#*\"}
74 fmt=${fmt%\"*}
75 echo "$fmt"
78 linetoh_begin_nop()
80 return
83 linetoh_nop()
85 local name args
86 name=$(get_name "$1")
87 args=$(get_args "$1")
89 # Define an empty function for the trace event
90 cat <<EOF
91 static inline void trace_$name($args)
94 EOF
97 linetoh_end_nop()
99 return
102 linetoc_begin_nop()
104 return
107 linetoc_nop()
109 # No need for function definitions in nop backend
110 return
113 linetoc_end_nop()
115 return
118 # Process stdin by calling begin, line, and end functions for the backend
119 convert()
121 local begin process_line end
122 begin="lineto$1_begin_$backend"
123 process_line="lineto$1_$backend"
124 end="lineto$1_end_$backend"
126 "$begin"
128 while read -r str; do
129 # Skip comments and empty lines
130 str=${str%%#*}
131 test -z "$str" && continue
133 echo
134 "$process_line" "$str"
135 done
137 echo
138 "$end"
141 tracetoh()
143 cat <<EOF
144 #ifndef TRACE_H
145 #define TRACE_H
147 /* This file is autogenerated by tracetool, do not edit. */
149 #include "qemu-common.h"
151 convert h
152 echo "#endif /* TRACE_H */"
155 tracetoc()
157 echo "/* This file is autogenerated by tracetool, do not edit. */"
158 convert c
161 # Choose backend
162 case "$1" in
163 "--nop") backend="${1#--}" ;;
164 *) usage ;;
165 esac
166 shift
168 case "$1" in
169 "-h") tracetoh ;;
170 "-c") tracetoc ;;
171 "--check-backend") exit 0 ;; # used by ./configure to test for backend
172 *) usage ;;
173 esac
175 exit 0