xref: /linux/tools/perf/perf-completion.sh (revision 4b132aacb0768ac1e652cf517097ea6f237214b9)
1# perf bash and zsh completion
2# SPDX-License-Identifier: GPL-2.0
3
4# Taken from git.git's completion script.
5__my_reassemble_comp_words_by_ref()
6{
7	local exclude i j first
8	# Which word separators to exclude?
9	exclude="${1//[^$COMP_WORDBREAKS]}"
10	cword_=$COMP_CWORD
11	if [ -z "$exclude" ]; then
12		words_=("${COMP_WORDS[@]}")
13		return
14	fi
15	# List of word completion separators has shrunk;
16	# re-assemble words to complete.
17	for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
18		# Append each nonempty word consisting of just
19		# word separator characters to the current word.
20		first=t
21		while
22			[ $i -gt 0 ] &&
23			[ -n "${COMP_WORDS[$i]}" ] &&
24			# word consists of excluded word separators
25			[ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
26		do
27			# Attach to the previous token,
28			# unless the previous token is the command name.
29			if [ $j -ge 2 ] && [ -n "$first" ]; then
30				((j--))
31			fi
32			first=
33			words_[$j]=${words_[j]}${COMP_WORDS[i]}
34			if [ $i = $COMP_CWORD ]; then
35				cword_=$j
36			fi
37			if (($i < ${#COMP_WORDS[@]} - 1)); then
38				((i++))
39			else
40				# Done.
41				return
42			fi
43		done
44		words_[$j]=${words_[j]}${COMP_WORDS[i]}
45		if [ $i = $COMP_CWORD ]; then
46			cword_=$j
47		fi
48	done
49}
50
51# Define preload_get_comp_words_by_ref="false", if the function
52# __perf_get_comp_words_by_ref() is required instead.
53preload_get_comp_words_by_ref="true"
54
55if [ $preload_get_comp_words_by_ref = "true" ]; then
56	type _get_comp_words_by_ref &>/dev/null ||
57	preload_get_comp_words_by_ref="false"
58fi
59[ $preload_get_comp_words_by_ref = "true" ] ||
60__perf_get_comp_words_by_ref()
61{
62	local exclude cur_ words_ cword_
63	if [ "$1" = "-n" ]; then
64		exclude=$2
65		shift 2
66	fi
67	__my_reassemble_comp_words_by_ref "$exclude"
68	cur_=${words_[cword_]}
69	while [ $# -gt 0 ]; do
70		case "$1" in
71		cur)
72			cur=$cur_
73			;;
74		prev)
75			prev=${words_[$cword_-1]}
76			;;
77		words)
78			words=("${words_[@]}")
79			;;
80		cword)
81			cword=$cword_
82			;;
83		esac
84		shift
85	done
86}
87
88# Define preload__ltrim_colon_completions="false", if the function
89# __perf__ltrim_colon_completions() is required instead.
90preload__ltrim_colon_completions="true"
91
92if [ $preload__ltrim_colon_completions = "true" ]; then
93	type __ltrim_colon_completions &>/dev/null ||
94	preload__ltrim_colon_completions="false"
95fi
96[ $preload__ltrim_colon_completions = "true" ] ||
97__perf__ltrim_colon_completions()
98{
99	if [[ "$1" == *:* && "$COMP_WORDBREAKS" == *:* ]]; then
100		# Remove colon-word prefix from COMPREPLY items
101		local colon_word=${1%"${1##*:}"}
102		local i=${#COMPREPLY[*]}
103		while [[ $((--i)) -ge 0 ]]; do
104			COMPREPLY[$i]=${COMPREPLY[$i]#"$colon_word"}
105		done
106	fi
107}
108
109__perfcomp ()
110{
111	# Expansion of spaces to array is deliberate.
112	# shellcheck disable=SC2207
113	COMPREPLY=( $( compgen -W "$1" -- "$2" ) )
114}
115
116__perfcomp_colon ()
117{
118	__perfcomp "$1" "$2"
119	if [ $preload__ltrim_colon_completions = "true" ]; then
120		__ltrim_colon_completions $cur
121	else
122		__perf__ltrim_colon_completions $cur
123	fi
124}
125
126__perf_prev_skip_opts ()
127{
128	local i cmd_ cmds_
129
130	let i=cword-1
131	cmds_=$($cmd $1 --list-cmds)
132	prev_skip_opts=""
133	while [ $i -ge 0 ]; do
134		if [[ ${words[i]} == "$1" ]]; then
135			return
136		fi
137		for cmd_ in $cmds_; do
138			if [[ ${words[i]} == "$cmd_" ]]; then
139				prev_skip_opts=${words[i]}
140				return
141			fi
142		done
143		((i--))
144	done
145}
146
147__perf_main ()
148{
149	local cmd
150
151	cmd=${words[0]}
152	COMPREPLY=()
153
154	# Skip options backward and find the last perf command
155	__perf_prev_skip_opts
156	# List perf subcommands or long options
157	if [ -z $prev_skip_opts ]; then
158		if [[ $cur == --* ]]; then
159			cmds=$($cmd --list-opts)
160		else
161			cmds=$($cmd --list-cmds)
162		fi
163		__perfcomp "$cmds" "$cur"
164	# List possible events for -e option
165	elif [[ $prev == @("-e"|"--event") &&
166		$prev_skip_opts == @(record|stat|top) ]]; then
167
168		local cur1=${COMP_WORDS[COMP_CWORD]}
169		local raw_evts
170		local arr s tmp result cpu_evts
171
172		raw_evts=$($cmd list --raw-dump hw sw cache tracepoint pmu sdt)
173		# aarch64 doesn't have /sys/bus/event_source/devices/cpu/events
174		if [[ `uname -m` != aarch64 ]]; then
175			cpu_evts=$(ls /sys/bus/event_source/devices/cpu/events)
176		fi
177
178		if [[ "$cur1" == */* && ${cur1#*/} =~ ^[A-Z] ]]; then
179			OLD_IFS="$IFS"
180			IFS=" "
181			# Expansion of spaces to array is deliberate.
182			# shellcheck disable=SC2206
183			arr=($raw_evts)
184			IFS="$OLD_IFS"
185
186			for s in "${arr[@]}"
187			do
188				if [[ "$s" == *cpu/* ]]; then
189					tmp=${s#*cpu/}
190					result=$result" ""cpu/"${tmp^^}
191				else
192					result=$result" "$s
193				fi
194			done
195
196			evts=${result}" "${cpu_evts}
197		else
198			evts=${raw_evts}" "${cpu_evts}
199		fi
200
201		if [[ "$cur1" == , ]]; then
202			__perfcomp_colon "$evts" ""
203		else
204			__perfcomp_colon "$evts" "$cur1"
205		fi
206	elif [[ $prev == @("--pfm-events") &&
207		$prev_skip_opts == @(record|stat|top) ]]; then
208		local evts
209		evts=$($cmd list --raw-dump pfm)
210		__perfcomp "$evts" "$cur"
211	elif [[ $prev == @("-M"|"--metrics") &&
212		$prev_skip_opts == @(stat) ]]; then
213		local metrics
214		metrics=$($cmd list --raw-dump metric metricgroup)
215		__perfcomp "$metrics" "$cur"
216	else
217		# List subcommands for perf commands
218		if [[ $prev_skip_opts == @(kvm|kmem|mem|lock|sched|
219			|data|help|script|test|timechart|trace) ]]; then
220			subcmds=$($cmd $prev_skip_opts --list-cmds)
221			__perfcomp_colon "$subcmds" "$cur"
222		fi
223		# List long option names
224		if [[ $cur == --* ]];  then
225			subcmd=$prev_skip_opts
226			__perf_prev_skip_opts $subcmd
227			subcmd=$subcmd" "$prev_skip_opts
228			opts=$($cmd $subcmd --list-opts)
229			__perfcomp "$opts" "$cur"
230		fi
231	fi
232}
233
234if [[ -n ${ZSH_VERSION-} ]]; then
235	autoload -U +X compinit && compinit
236
237	__perfcomp ()
238	{
239		emulate -L zsh
240
241		local c IFS=$' \t\n'
242		local -a array
243
244		for c in ${=1}; do
245			case $c in
246			--*=*|*.) ;;
247			*) c="$c " ;;
248			esac
249			array[${#array[@]}+1]="$c"
250		done
251
252		compset -P '*[=:]'
253		compadd -Q -S '' -a -- array && _ret=0
254	}
255
256	__perfcomp_colon ()
257	{
258		emulate -L zsh
259
260		local cur_="${2-$cur}"
261		local c IFS=$' \t\n'
262		local -a array
263
264		if [[ "$cur_" == *:* ]]; then
265			local colon_word=${cur_%"${cur_##*:}"}
266		fi
267
268		for c in ${=1}; do
269			case $c in
270			--*=*|*.) ;;
271			*) c="$c " ;;
272			esac
273			array[$#array+1]=${c#"$colon_word"}
274		done
275
276		compset -P '*[=:]'
277		compadd -Q -S '' -a -- array && _ret=0
278	}
279
280	_perf ()
281	{
282		local _ret=1 cur cword prev
283		cur=${words[CURRENT]}
284		prev=${words[CURRENT-1]}
285		let cword=CURRENT-1
286		emulate ksh -c __perf_main
287		let _ret && _default && _ret=0
288		# _ret is only assigned 0 or 1, disable inaccurate analysis.
289		# shellcheck disable=SC2152
290		return _ret
291	}
292
293	compdef _perf perf
294	return
295fi
296
297type perf &>/dev/null &&
298_perf()
299{
300	if [[ "$COMP_WORDBREAKS" != *,* ]]; then
301		COMP_WORDBREAKS="${COMP_WORDBREAKS},"
302		export COMP_WORDBREAKS
303	fi
304
305	if [[ "$COMP_WORDBREAKS" == *:* ]]; then
306		COMP_WORDBREAKS="${COMP_WORDBREAKS/:/}"
307		export COMP_WORDBREAKS
308	fi
309
310	local cur words cword prev
311	if [ $preload_get_comp_words_by_ref = "true" ]; then
312		_get_comp_words_by_ref -n =:, cur words cword prev
313	else
314		__perf_get_comp_words_by_ref -n =:, cur words cword prev
315	fi
316	__perf_main
317} &&
318
319complete -o bashdefault -o default -o nospace -F _perf perf 2>/dev/null \
320	|| complete -o default -o nospace -F _perf perf
321