trace: Specify trace file name
[qemu.git] / tracetool
blob8aeac4870b76c31c78701460f818a77a0d114bfa
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] [-h | -c]
17 Generate tracing code for a file on stdin.
19 Backends:
20 --nop Tracing disabled
21 --simple Simple built-in backend
23 Output formats:
24 -h Generate .h file
25 -c Generate .c file
26 EOF
27 exit 1
30 # Get the name of a trace event
31 get_name()
33 echo ${1%%\(*}
36 # Get the argument list of a trace event, including types and names
37 get_args()
39 local args
40 args=${1#*\(}
41 args=${args%)*}
42 echo "$args"
45 # Get the argument name list of a trace event
46 get_argnames()
48 local nfields field name
49 nfields=0
50 for field in $(get_args "$1"); do
51 nfields=$((nfields + 1))
53 # Drop pointer star
54 field=${field#\*}
56 # Only argument names have commas at the end
57 name=${field%,}
58 test "$field" = "$name" && continue
60 printf "%s" "$name, "
61 done
63 # Last argument name
64 if [ "$nfields" -gt 1 ]
65 then
66 printf "%s" "$name"
70 # Get the number of arguments to a trace event
71 get_argc()
73 local name argc
74 argc=0
75 for name in $(get_argnames "$1"); do
76 argc=$((argc + 1))
77 done
78 echo $argc
81 # Get the format string for a trace event
82 get_fmt()
84 local fmt
85 fmt=${1#*\"}
86 fmt=${fmt%\"*}
87 echo "$fmt"
90 # Get the state of a trace event
91 get_state()
93 local str disable state
94 str=$(get_name "$1")
95 disable=${str##disable }
96 if [ "$disable" = "$str" ] ; then
97 state=1
98 else
99 state=0
101 echo "$state"
104 linetoh_begin_nop()
106 return
109 linetoh_nop()
111 local name args
112 name=$(get_name "$1")
113 args=$(get_args "$1")
115 # Define an empty function for the trace event
116 cat <<EOF
117 static inline void trace_$name($args)
123 linetoh_end_nop()
125 return
128 linetoc_begin_nop()
130 return
133 linetoc_nop()
135 # No need for function definitions in nop backend
136 return
139 linetoc_end_nop()
141 return
144 linetoh_begin_simple()
146 cat <<EOF
147 #include "simpletrace.h"
150 simple_event_num=0
153 cast_args_to_uint64_t()
155 local arg
156 for arg in $(get_argnames "$1"); do
157 printf "%s" "(uint64_t)(uintptr_t)$arg"
158 done
161 linetoh_simple()
163 local name args argc trace_args state
164 name=$(get_name "$1")
165 args=$(get_args "$1")
166 argc=$(get_argc "$1")
167 state=$(get_state "$1")
168 if [ "$state" = "0" ]; then
169 name=${name##disable }
172 trace_args="$simple_event_num"
173 if [ "$argc" -gt 0 ]
174 then
175 trace_args="$trace_args, $(cast_args_to_uint64_t "$1")"
178 cat <<EOF
179 static inline void trace_$name($args)
181 trace$argc($trace_args);
185 simple_event_num=$((simple_event_num + 1))
188 linetoh_end_simple()
190 cat <<EOF
191 #define NR_TRACE_EVENTS $simple_event_num
192 extern TraceEvent trace_list[NR_TRACE_EVENTS];
196 linetoc_begin_simple()
198 cat <<EOF
199 #include "trace.h"
201 TraceEvent trace_list[] = {
203 simple_event_num=0
207 linetoc_simple()
209 local name state
210 name=$(get_name "$1")
211 state=$(get_state "$1")
212 if [ "$state" = "0" ] ; then
213 name=${name##disable }
215 cat <<EOF
216 {.tp_name = "$name", .state=$state},
218 simple_event_num=$((simple_event_num + 1))
221 linetoc_end_simple()
223 cat <<EOF
228 # Process stdin by calling begin, line, and end functions for the backend
229 convert()
231 local begin process_line end str disable
232 begin="lineto$1_begin_$backend"
233 process_line="lineto$1_$backend"
234 end="lineto$1_end_$backend"
236 "$begin"
238 while read -r str; do
239 # Skip comments and empty lines
240 str=${str%%#*}
241 test -z "$str" && continue
243 # Process the line. The nop backend handles disabled lines.
244 disable=${str%%disable *}
245 echo
246 if test -z "$disable"; then
247 # Pass the disabled state as an arg to lineto$1_simple().
248 # For all other cases, call lineto$1_nop()
249 if [ $backend = "simple" ]; then
250 "$process_line" "$str"
251 else
252 "lineto$1_nop" "${str##disable }"
254 else
255 "$process_line" "$str"
257 done
259 echo
260 "$end"
263 tracetoh()
265 cat <<EOF
266 #ifndef TRACE_H
267 #define TRACE_H
269 /* This file is autogenerated by tracetool, do not edit. */
271 #include "qemu-common.h"
273 convert h
274 echo "#endif /* TRACE_H */"
277 tracetoc()
279 echo "/* This file is autogenerated by tracetool, do not edit. */"
280 convert c
283 # Choose backend
284 case "$1" in
285 "--nop" | "--simple") backend="${1#--}" ;;
286 *) usage ;;
287 esac
288 shift
290 case "$1" in
291 "-h") tracetoh ;;
292 "-c") tracetoc ;;
293 "--check-backend") exit 0 ;; # used by ./configure to test for backend
294 *) usage ;;
295 esac
297 exit 0