• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1show_instances()
2{
3   local cur="$1"
4   local bufs=$(trace-cmd list -B)
5   if [ "$bufs" == "No buffer instances defined" ]; then
6	return 0
7   fi
8   COMPREPLY=( $(compgen -W "${bufs}" -- "${cur}") )
9   return 0
10}
11
12show_virt()
13{
14    local cur="$1"
15    if ! which virsh &>/dev/null; then
16	return 1
17    fi
18    local virt=`virsh list | awk '/^ *[0-9]/ { print $2 }'`
19    COMPREPLY=( $(compgen -W "${virt}" -- "${cur}") )
20    return 0
21}
22
23show_options()
24{
25   local cur="$1"
26   local options=$(trace-cmd list -o | sed -e 's/^\(no\)*\(.*\)/\2 no\2/')
27   COMPREPLY=( $(compgen -W "${options}" -- "${cur}") )
28   return 0
29}
30
31__show_files()
32{
33    COMPREPLY=( $(compgen -f -- "$cur") )
34    if [ ${#COMPREPLY[@]} -gt 1 ]; then
35	    return 0;
36    fi
37    # directories get '/' instead of space
38    DIRS=( $(compgen -d -- "$cur"))
39    if [ ${#DIRS[@]} -eq 1 ]; then
40	compopt -o nospace
41	COMPREPLY="$DIRS/"
42	return 0;
43    fi
44    return 0
45}
46
47cmd_options()
48{
49    local type="$1"
50    local cur="$2"
51    local cmds=$(trace-cmd $type -h 2>/dev/null|grep "^ *-" | \
52				 sed -e 's/ *\(-[^ ]*\).*/\1/')
53    COMPREPLY=( $(compgen -W "${cmds}" -- "${cur}") )
54    if [ ${#COMPREPLY[@]} -eq 0 ]; then
55	__show_files "${cur}"
56    fi
57}
58
59plugin_options()
60{
61    local cur="$1"
62
63    local opts=$(trace-cmd list -O | sed -ne 's/option://p')
64    COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
65}
66
67compression_param()
68{
69    local opts=$(trace-cmd list -c | grep -v 'Supported' | cut -d "," -f1)
70    opts+=" any none "
71    COMPREPLY=( $(compgen -W "${opts}") )
72}
73
74__trace_cmd_list_complete()
75{
76    local prev=$1
77    local cur=$2
78    shift 2
79    local words=("$@")
80
81    case "$prev" in
82	list)
83	    local cmds=$(trace-cmd list -h |egrep "^ {10}-" | \
84				 sed -e 's/.*\(-.\).*/\1/')
85	    COMPREPLY=( $(compgen -W "${cmds}" -- "${cur}") )
86	    ;;
87	*)
88	    size=${#words[@]}
89	    if [ $size -gt 3 ]; then
90		if [ "$cur" == "-" ]; then
91		    let size=$size-3
92		else
93		    let size=$size-2
94		fi
95		local w="${words[$size]}"
96		if [ "$w" == "-e" ]; then
97		    local cmds=$(trace-cmd list -h |egrep "^ {12}-" | \
98				 sed -e 's/.*\(-.\).*/\1/')
99		    COMPREPLY=( $(compgen -W "${cmds}" -- "${cur}") )
100		fi
101	    fi
102	    ;;
103    esac
104}
105
106__trace_cmd_show_complete()
107{
108    local prev=$1
109    local cur=$2
110    shift 2
111    local words=("$@")
112
113    case "$prev" in
114	-B)
115	    show_instances "$cur"
116	    ;;
117	*)
118	    cmd_options show "$cur"
119	    ;;
120    esac
121}
122
123__trace_cmd_extract_complete()
124{
125    local prev=$1
126    local cur=$2
127    shift 2
128    local words=("$@")
129
130    case "$prev" in
131	extract)
132	    cmd_options "$prev" "$cur"
133	    ;;
134	-B)
135	    show_instances "$cur"
136	    ;;
137	*)
138	    __show_files
139	    ;;
140    esac
141}
142
143__trace_cmd_record_complete()
144{
145    local prev=$1
146    local cur=$2
147    shift 2
148    local words=("$@")
149
150    case "$prev" in
151        -e)
152	    local list=$(trace-cmd list -e "$cur")
153            local prefix=${cur%%:*}
154	    if [ -z "$cur" -o  "$cur" != "$prefix" ]; then
155		COMPREPLY=( $(compgen -W "all ${list}" -- "${cur}") )
156	    else
157		local events=$(for e in $list; do echo ${e/*:/}; done | sort -u)
158	        local systems=$(for s in $list; do echo ${s/:*/:}; done | sort -u)
159
160		COMPREPLY=( $(compgen -W "all ${events} ${systems}" -- "${cur}") )
161	    fi
162
163            # This is still to handle the "*:*" special case
164            if [[ -n "$prefix" ]]; then
165                local reply_n=${#COMPREPLY[*]}
166                for (( i = 0; i < $reply_n; i++)); do
167                    COMPREPLY[$i]=${COMPREPLY[i]##${prefix}:}
168                done
169            fi
170            ;;
171        -p)
172            local plugins=$(trace-cmd list -p)
173	    COMPREPLY=( $(compgen -W "${plugins}" -- "${cur}" ) )
174            ;;
175        -l|-n|-g)
176            # This is extremely slow still (may take >1sec).
177            local funcs=$(trace-cmd list -f | sed 's/ .*//')
178            COMPREPLY=( $(compgen -W "${funcs}" -- "${cur}") )
179            ;;
180	-B)
181	    show_instances "$cur"
182	    ;;
183	-O)
184	    show_options "$cur"
185	    ;;
186	-A)
187	    if ! show_virt "$cur"; then
188		cmd_options record "$cur"
189	    fi
190	    ;;
191	--compression)
192	    compression_param
193	    ;;
194        *)
195	    # stream start and profile do not show all options
196	    cmd_options record "$cur"
197	    ;;
198    esac
199}
200
201__trace_cmd_report_complete()
202{
203    local prev=$1
204    local cur=$2
205    shift 2
206    local words=("$@")
207
208    case "$prev" in
209	-O)
210	    plugin_options "$cur"
211	    ;;
212        *)
213	    cmd_options report "$cur"
214	    ;;
215    esac
216}
217
218__trace_cmd_dump_complete()
219{
220    local prev=$1
221    local cur=$2
222    shift 2
223    local words=("$@")
224
225    case "$prev" in
226	-i)
227	    __show_files
228	    ;;
229	*)
230	    cmd_options dump "$cur"
231	    ;;
232    esac
233}
234
235__trace_cmd_convert_complete()
236{
237    local prev=$1
238    local cur=$2
239    shift 2
240    local words=("$@")
241
242    case "$prev" in
243	-i)
244	    __show_files
245	    ;;
246	-o)
247	    __show_files
248	    ;;
249	--compression)
250	    compression_param
251	    ;;
252	*)
253	    cmd_options convert "$cur"
254	    ;;
255    esac
256}
257
258__show_command_options()
259{
260    local command="$1"
261    local prev="$2"
262    local cur="$3"
263    local cmds=( $(trace-cmd --help 2>/dev/null | \
264		    grep " - " | sed 's/^ *//; s/ -.*//') )
265
266    for cmd in ${cmds[@]}; do
267	if [ $cmd == "$command" ]; then
268	    local opts=$(trace-cmd $cmd -h 2>/dev/null|grep "^ *-" | \
269				 sed -e 's/ *\(-[^ ]*\).*/\1/')
270	    if [ "$prev" == "-B" ]; then
271		for opt in ${opts[@]}; do
272		    if [ "$opt" == "-B" ]; then
273			show_instances "$cur"
274			return 0
275		    fi
276		done
277	    fi
278	    COMPREPLY=( $(compgen -W "${opts}" -- "$cur"))
279	    break
280	fi
281    done
282    if [ ${#COMPREPLY[@]} -eq 0 ]; then
283	__show_files "${cur}"
284    fi
285}
286
287_trace_cmd_complete()
288{
289    local cur=""
290    local prev=""
291    local words=()
292
293    # Not to use COMP_WORDS to avoid buggy behavior of Bash when
294    # handling with words including ":", like:
295    #
296    # prev="${COMP_WORDS[COMP_CWORD-1]}"
297    # cur="${COMP_WORDS[COMP_CWORD]}"
298    #
299    # Instead, we use _get_comp_words_by_ref() magic.
300    _get_comp_words_by_ref -n : cur prev words
301
302    if [ "$prev" == "trace-cmd" ]; then
303            local cmds=$(trace-cmd --help 2>/dev/null | \
304                                grep " - " | sed 's/^ *//; s/ -.*//')
305            COMPREPLY=( $(compgen -W "${cmds}" -- "${cur}") )
306	    return;
307    fi
308
309    local w="${words[1]}"
310
311    case "$w" in
312	list)
313	    __trace_cmd_list_complete "${prev}" "${cur}" ${words[@]}
314	    return 0
315	    ;;
316	show)
317	    __trace_cmd_show_complete "${prev}" "${cur}" ${words[@]}
318	    return 0
319	    ;;
320	extract)
321	    __trace_cmd_extract_complete "${prev}" "${cur}" ${words[@]}
322	    return 0
323	    ;;
324	record|stream|start|profile)
325	    __trace_cmd_record_complete "${prev}" "${cur}" ${words[@]}
326	    return 0
327	    ;;
328	report)
329	    __trace_cmd_report_complete "${prev}" "${cur}" ${words[@]}
330	    return 0
331	    ;;
332	dump)
333	    __trace_cmd_dump_complete "${prev}" "${cur}" ${words[@]}
334	    return 0
335	    ;;
336	convert)
337	    __trace_cmd_convert_complete "${prev}" "${cur}" ${words[@]}
338	    return 0
339	    ;;
340        *)
341	    __show_command_options "$w" "${prev}" "${cur}"
342            ;;
343    esac
344}
345complete -F _trace_cmd_complete trace-cmd
346