2 # Copyright (C) 2012 EfficiOS Inc.
4 # SPDX-License-Identifier: GPL-2.0-only
7 # Generates COMPREPLY with the existing session names
8 _lttng_complete_sessions() {
10 sessions=$(lttng --mi xml list | sed '2 s/xmlns/ignore/g' | xmllint --xpath "//command/output/sessions/session/name" - 2>/dev/null | sed -e 's/<name>//g' -e $'s/<\/name>/\\n/g')
11 COMPREPLY=( $(compgen -W "${sessions}" -- $cur) )
16 # Generates COMPREPLY with the available kernel event
17 _lttng_complete_kernel_events() {
19 kernel_event=$(lttng --mi xml list -k |sed '2 s/xmlns/ignore/g' | xmllint --xpath "//command/output/domains/domain[./type = 'KERNEL']/events/event/name" - 2>/dev/null | sed -e "s/<name>//g" -e $"s/<\/name>/\\n/g")
20 COMPREPLY=( $(compgen -W "${kernel_event}" -- $cur) )
24 # Generates COMPREPLY with the available ust event
25 _lttng_complete_ust_events() {
27 ust_event=$(lttng --mi xml list -u | sed '2 s/xmlns/ignore/g' | xmllint --xpath "//command/output/domains/domain[./type = 'UST']/pids/pid/events/event/name" - 2>/dev/null | sed -e "s/<name>//g" -e $"s/<\/name>/\\n/g")
28 COMPREPLY=( $(compgen -W "${ust_event}" -- $cur) )
32 # Generates COMPREPLY with the available jul event
33 _lttng_complete_jul_events() {
35 jul_event=$(lttng --mi xml list -j | sed '2 s/xmlns/ignore/g' | xmllint --xpath "//command/output/domains/domain[./type = 'JUL']/pids/pid/events/event/name" - 2>/dev/null | sed -e "s/<name>//g" -e $"s/<\/name>/\\n/g")
36 COMPREPLY=( $(compgen -W "${jul_event}" -- $cur) )
42 # Generates COMPREPLY with whatever is in the $options variable.
43 _lttng_complete_options() {
44 COMPREPLY=( $(compgen -W "${options}" -- $cur) )
47 # Generates COMPREPLY with whatever is in the $commands variable.
48 _lttng_complete_commands() {
49 COMPREPLY=( $(compgen -W "${commands}" -- $cur) )
52 _lttng_cmd_addcontext() {
53 options=$(lttng add-context --list-options)
57 _lttng_complete_sessions
70 _lttng_complete_options
77 options=$(lttng create --list-options)
88 _lttng_complete_options
94 _lttng_cmd_destroy() {
95 options=$(lttng destroy --list-options)
99 _lttng_complete_options
103 _lttng_complete_sessions
108 _lttng_cmd_disablechannel() {
109 options=$(lttng disable-channel --list-options)
113 _lttng_complete_sessions
120 _lttng_complete_options
125 _lttng_cmd_disableevent() {
126 options=$(lttng disable-event --list-options)
130 _lttng_complete_sessions
140 _lttng_complete_options
146 _lttng_cmd_enablechannel() {
147 options=$(lttng enable-channel --list-options)
151 _lttng_complete_sessions
158 _lttng_complete_options
164 _lttng_cmd_enableevent() {
165 options=$(lttng enable-event --list-options)
169 _lttng_complete_sessions
184 #Check if we want kernel event completion
185 if [[ "$COMP_LINE" == *"-k"* ]]; then
186 _lttng_complete_kernel_events
190 #Check if we want ust event completion
191 if [[ "$COMP_LINE" == *"-u"* ]]; then
192 _lttng_complete_ust_events
196 #Check if we want jul event completion
197 if [[ "$COMP_LINE" == *"-j"* ]]; then
198 _lttng_complete_jul_events
204 _lttng_complete_options
212 options=$(lttng list --list-options)
222 _lttng_complete_options
226 _lttng_complete_sessions
231 _lttng_cmd_setsession() {
232 options=$(lttng set-session --list-options)
236 _lttng_complete_options
240 _lttng_complete_sessions
246 _lttng_cmd_snapshot() {
247 options=$(lttng snapshot --list-options)
248 commands=$(lttng snapshot --list-commands)
250 _lttng_find_command $((command_found_index + 1))
252 if _lttng_cursor_is_after_command; then
255 _lttng_complete_sessions
262 _lttng_complete_options
266 _lttng_complete_commands
271 options=$(lttng start --list-options)
275 _lttng_complete_options
279 _lttng_complete_sessions
286 options=$(lttng stop --list-options)
290 _lttng_complete_options
293 _lttng_complete_sessions
298 _lttng_cmd_version() {
299 options=$(lttng version --list-options)
303 _lttng_complete_options
309 options=$(lttng view --list-options)
313 _lttng_complete_options
320 _lttng_before_command() {
321 # Check if the previous word should alter the behavior
324 COMPREPLY=( $(compgen -g -- $cur) )
335 # If the current word starts with a dash, complete with options
336 _lttng_complete_options
339 # Otherwise complete with commands
340 _lttng_complete_commands
345 _lttng_after_command() {
348 cmd_name=_lttng_cmd_${command_found//-/}
350 type -t $cmd_name | grep -q 'function' && $cmd_name
353 # Check if the word passed as the first parameter corresponds to a
354 # command. $command must be set to the list of possible commands.
355 _lttng_is_command() {
356 for command in $commands; do
357 if [ "$1" == "$command" ]; then
365 # Try to find a command in the current command line. Possible commands
366 # are passed in $commands.
368 # This function takes an optional parameter that indicates the index
369 # where to start the search in COMP_WORDS. If omitted, it defaults to 1.
371 # If a command is found, $command_found is filled with the name of the
372 # command and $command_found_index is set to the index of the command in
373 # $COMP_WORDS. If no command is found, $command_found is an empty string
374 # and $command_found_index is set to -1.
375 _lttng_find_command() {
378 # The text of the found command
381 # The index of the found command in COMP_WORDS
382 command_found_index=-1
384 for (( i = start ; i < ${#COMP_WORDS[@]} ; i++ )); do
385 _lttng_is_command ${COMP_WORDS[$i]}
386 if [ $? -eq 0 ]; then
387 command_found=${COMP_WORDS[$i]}
388 command_found_index=$i
394 _lttng_cursor_is_after_command() {
395 [ -n "$command_found" ] && [ "$COMP_CWORD" -gt "$command_found_index" ]
399 local cur prev commands command_found command_found_index
401 # Get the current and previous word
402 _get_comp_words_by_ref cur prev
404 # Get the valid first-level LTTng commands and options
405 commands=$(lttng --list-commands)
406 options=$(lttng --list-options)
410 # Check if the cursor is before or after the command keyword
411 if _lttng_cursor_is_after_command; then
414 _lttng_before_command
418 complete -F _lttng lttng