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
87 _lttng_cmd_calibrate() {
88 options=$(lttng calibrate --list-options)
92 _lttng_complete_options
99 options=$(lttng create --list-options)
110 _lttng_complete_options
116 _lttng_cmd_destroy() {
117 options=$(lttng destroy --list-options)
121 _lttng_complete_options
125 _lttng_complete_sessions
130 _lttng_cmd_disablechannel() {
131 options=$(lttng disable-channel --list-options)
135 _lttng_complete_sessions
142 _lttng_complete_options
147 _lttng_cmd_disableevent() {
148 options=$(lttng disable-event --list-options)
152 _lttng_complete_sessions
162 _lttng_complete_options
168 _lttng_cmd_enablechannel() {
169 options=$(lttng enable-channel --list-options)
173 _lttng_complete_sessions
180 _lttng_complete_options
186 _lttng_cmd_enableevent() {
187 options=$(lttng enable-event --list-options)
191 _lttng_complete_sessions
206 #Check if we want kernel event completion
207 if [[ "$COMP_LINE" == *"-k"* ]]; then
208 _lttng_complete_kernel_events
212 #Check if we want ust event completion
213 if [[ "$COMP_LINE" == *"-u"* ]]; then
214 _lttng_complete_ust_events
218 #Check if we want jul event completion
219 if [[ "$COMP_LINE" == *"-j"* ]]; then
220 _lttng_complete_jul_events
226 _lttng_complete_options
234 options=$(lttng list --list-options)
244 _lttng_complete_options
248 _lttng_complete_sessions
253 _lttng_cmd_setsession() {
254 options=$(lttng set-session --list-options)
258 _lttng_complete_options
262 _lttng_complete_sessions
268 _lttng_cmd_snapshot() {
269 options=$(lttng snapshot --list-options)
270 commands=$(lttng snapshot --list-commands)
272 _lttng_find_command $((command_found_index + 1))
274 if _lttng_cursor_is_after_command; then
277 _lttng_complete_sessions
284 _lttng_complete_options
288 _lttng_complete_commands
293 options=$(lttng start --list-options)
297 _lttng_complete_options
301 _lttng_complete_sessions
308 options=$(lttng stop --list-options)
312 _lttng_complete_options
315 _lttng_complete_sessions
320 _lttng_cmd_version() {
321 options=$(lttng version --list-options)
325 _lttng_complete_options
331 options=$(lttng view --list-options)
335 _lttng_complete_options
342 _lttng_before_command() {
343 # Check if the previous word should alter the behavior
346 COMPREPLY=( $(compgen -g -- $cur) )
357 # If the current word starts with a dash, complete with options
358 _lttng_complete_options
361 # Otherwise complete with commands
362 _lttng_complete_commands
367 _lttng_after_command() {
370 cmd_name=_lttng_cmd_${command_found//-/}
372 type -t $cmd_name | grep -q 'function' && $cmd_name
375 # Check if the word passed as the first parameter corresponds to a
376 # command. $command must be set to the list of possible commands.
377 _lttng_is_command() {
378 for command in $commands; do
379 if [ "$1" == "$command" ]; then
387 # Try to find a command in the current command line. Possible commands
388 # are passed in $commands.
390 # This function takes an optional parameter that indicates the index
391 # where to start the search in COMP_WORDS. If omitted, it defaults to 1.
393 # If a command is found, $command_found is filled with the name of the
394 # command and $command_found_index is set to the index of the command in
395 # $COMP_WORDS. If no command is found, $command_found is an empty string
396 # and $command_found_index is set to -1.
397 _lttng_find_command() {
400 # The text of the found command
403 # The index of the found command in COMP_WORDS
404 command_found_index=-1
406 for (( i = start ; i < ${#COMP_WORDS[@]} ; i++ )); do
407 _lttng_is_command ${COMP_WORDS[$i]}
408 if [ $? -eq 0 ]; then
409 command_found=${COMP_WORDS[$i]}
410 command_found_index=$i
416 _lttng_cursor_is_after_command() {
417 [ -n "$command_found" ] && [ "$COMP_CWORD" -gt "$command_found_index" ]
421 local cur prev commands command_found command_found_index
423 # Get the current and previous word
424 _get_comp_words_by_ref cur prev
426 # Get the valid first-level LTTng commands and options
427 commands=$(lttng --list-commands)
428 options=$(lttng --list-options)
432 # Check if the cursor is before or after the command keyword
433 if _lttng_cursor_is_after_command; then
436 _lttng_before_command
440 complete -F _lttng lttng