2 # Copyright (c) - 2012 Simon Marchi <simon.marchi@polymtl.ca>
4 # This program is free software; you can redistribute it and/or modify it under
5 # the terms of the GNU General Public License as published by as published by
6 # the Free Software Foundation; only version 2 of the License.
8 # This program is distributed in the hope that it will be useful, but WITHOUT
9 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 # You should have received a copy of the GNU General Public License along with
14 # this program; if not, write to the Free Software Foundation, Inc., 51
15 # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 # Generates COMPREPLY with the existing session names
19 _lttng_complete_sessions() {
21 sessions=$(lttng --mi xml list | xmllint --xpath "//command/output/sessions/session/name" - 2>/dev/null | sed -e 's/<name>//g' -e $'s/<\/name>/\\n/g')
22 COMPREPLY=( $(compgen -W "${sessions}" -- $cur) )
27 # Generates COMPREPLY with the available kernel event
28 _lttng_complete_kernel_events() {
30 kernel_event=$(lttng --mi xml list -k | xmllint --xpath "//command/output/domains/domain[./type = 'KERNEL']/events/event/name" - 2>/dev/null | sed -e "s/<name>//g" -e $"s/<\/name>/\\n/g")
31 COMPREPLY=( $(compgen -W "${kernel_event}" -- $cur) )
35 # Generates COMPREPLY with the available ust event
36 _lttng_complete_ust_events() {
38 ust_event=$(lttng --mi xml list -u | 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")
39 COMPREPLY=( $(compgen -W "${ust_event}" -- $cur) )
43 # Generates COMPREPLY with the available jul event
44 _lttng_complete_jul_events() {
46 jul_event=$(lttng --mi xml list -j | 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")
47 COMPREPLY=( $(compgen -W "${jul_event}" -- $cur) )
53 # Generates COMPREPLY with whatever is in the $options variable.
54 _lttng_complete_options() {
55 COMPREPLY=( $(compgen -W "${options}" -- $cur) )
58 # Generates COMPREPLY with whatever is in the $commands variable.
59 _lttng_complete_commands() {
60 COMPREPLY=( $(compgen -W "${commands}" -- $cur) )
63 _lttng_cmd_addcontext() {
64 options=$(lttng add-context --list-options)
68 _lttng_complete_sessions
81 _lttng_complete_options
88 options=$(lttng create --list-options)
99 _lttng_complete_options
105 _lttng_cmd_destroy() {
106 options=$(lttng destroy --list-options)
110 _lttng_complete_options
114 _lttng_complete_sessions
119 _lttng_cmd_disablechannel() {
120 options=$(lttng disable-channel --list-options)
124 _lttng_complete_sessions
131 _lttng_complete_options
136 _lttng_cmd_disableevent() {
137 options=$(lttng disable-event --list-options)
141 _lttng_complete_sessions
151 _lttng_complete_options
157 _lttng_cmd_enablechannel() {
158 options=$(lttng enable-channel --list-options)
162 _lttng_complete_sessions
169 _lttng_complete_options
175 _lttng_cmd_enableevent() {
176 options=$(lttng enable-event --list-options)
180 _lttng_complete_sessions
195 #Check if we want kernel event completion
196 if [[ "$COMP_LINE" == *"-k"* ]]; then
197 _lttng_complete_kernel_events
201 #Check if we want ust event completion
202 if [[ "$COMP_LINE" == *"-u"* ]]; then
203 _lttng_complete_ust_events
207 #Check if we want jul event completion
208 if [[ "$COMP_LINE" == *"-j"* ]]; then
209 _lttng_complete_jul_events
215 _lttng_complete_options
223 options=$(lttng list --list-options)
233 _lttng_complete_options
237 _lttng_complete_sessions
242 _lttng_cmd_setsession() {
243 options=$(lttng set-session --list-options)
247 _lttng_complete_options
251 _lttng_complete_sessions
257 _lttng_cmd_snapshot() {
258 options=$(lttng snapshot --list-options)
259 commands=$(lttng snapshot --list-commands)
261 _lttng_find_command $((command_found_index + 1))
263 if _lttng_cursor_is_after_command; then
266 _lttng_complete_sessions
273 _lttng_complete_options
277 _lttng_complete_commands
282 options=$(lttng start --list-options)
286 _lttng_complete_options
290 _lttng_complete_sessions
297 options=$(lttng stop --list-options)
301 _lttng_complete_options
304 _lttng_complete_sessions
309 _lttng_cmd_version() {
310 options=$(lttng version --list-options)
314 _lttng_complete_options
320 options=$(lttng view --list-options)
324 _lttng_complete_options
331 _lttng_before_command() {
332 # Check if the previous word should alter the behavior
335 COMPREPLY=( $(compgen -g -- $cur) )
346 # If the current word starts with a dash, complete with options
347 _lttng_complete_options
350 # Otherwise complete with commands
351 _lttng_complete_commands
356 _lttng_after_command() {
359 cmd_name=_lttng_cmd_${command_found//-/}
361 type -t $cmd_name | grep -q 'function' && $cmd_name
364 # Check if the word passed as the first parameter corresponds to a
365 # command. $command must be set to the list of possible commands.
366 _lttng_is_command() {
367 for command in $commands; do
368 if [ "$1" == "$command" ]; then
376 # Try to find a command in the current command line. Possible commands
377 # are passed in $commands.
379 # This function takes an optional parameter that indicates the index
380 # where to start the search in COMP_WORDS. If omitted, it defaults to 1.
382 # If a command is found, $command_found is filled with the name of the
383 # command and $command_found_index is set to the index of the command in
384 # $COMP_WORDS. If no command is found, $command_found is an empty string
385 # and $command_found_index is set to -1.
386 _lttng_find_command() {
389 # The text of the found command
392 # The index of the found command in COMP_WORDS
393 command_found_index=-1
395 for (( i = start ; i < ${#COMP_WORDS[@]} ; i++ )); do
396 _lttng_is_command ${COMP_WORDS[$i]}
397 if [ $? -eq 0 ]; then
398 command_found=${COMP_WORDS[$i]}
399 command_found_index=$i
405 _lttng_cursor_is_after_command() {
406 [ -n "$command_found" ] && [ "$COMP_CWORD" -gt "$command_found_index" ]
410 local cur prev commands command_found command_found_index
412 # Get the current and previous word
413 _get_comp_words_by_ref cur prev
415 # Get the valid first-level LTTng commands and options
416 commands=$(lttng --list-commands)
417 options=$(lttng --list-options)
421 # Check if the cursor is before or after the command keyword
422 if _lttng_cursor_is_after_command; then
425 _lttng_before_command
429 complete -F _lttng lttng