| 1 | # |
| 2 | # Copyright (C) 2012 EfficiOS Inc. |
| 3 | # |
| 4 | # SPDX-License-Identifier: GPL-2.0-only |
| 5 | # |
| 6 | |
| 7 | # Generates COMPREPLY with the existing session names |
| 8 | _lttng_complete_sessions() { |
| 9 | local 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) ) |
| 12 | return |
| 13 | } |
| 14 | # |
| 15 | |
| 16 | # Generates COMPREPLY with the available kernel event |
| 17 | _lttng_complete_kernel_events() { |
| 18 | local kernel_event |
| 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) ) |
| 21 | return |
| 22 | } |
| 23 | |
| 24 | # Generates COMPREPLY with the available ust event |
| 25 | _lttng_complete_ust_events() { |
| 26 | local ust_event |
| 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) ) |
| 29 | return |
| 30 | } |
| 31 | |
| 32 | # Generates COMPREPLY with the available jul event |
| 33 | _lttng_complete_jul_events() { |
| 34 | local jul_event |
| 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) ) |
| 37 | return |
| 38 | } |
| 39 | |
| 40 | |
| 41 | |
| 42 | # Generates COMPREPLY with whatever is in the $options variable. |
| 43 | _lttng_complete_options() { |
| 44 | COMPREPLY=( $(compgen -W "${options}" -- $cur) ) |
| 45 | } |
| 46 | |
| 47 | # Generates COMPREPLY with whatever is in the $commands variable. |
| 48 | _lttng_complete_commands() { |
| 49 | COMPREPLY=( $(compgen -W "${commands}" -- $cur) ) |
| 50 | } |
| 51 | |
| 52 | _lttng_cmd_addcontext() { |
| 53 | options=$(lttng add-context --list-options) |
| 54 | |
| 55 | case $prev in |
| 56 | --session|-s) |
| 57 | _lttng_complete_sessions |
| 58 | return |
| 59 | ;; |
| 60 | --channel|-c) |
| 61 | return |
| 62 | ;; |
| 63 | --type|-t) |
| 64 | return |
| 65 | ;; |
| 66 | esac |
| 67 | |
| 68 | case $cur in |
| 69 | -*) |
| 70 | _lttng_complete_options |
| 71 | return |
| 72 | ;; |
| 73 | esac |
| 74 | } |
| 75 | |
| 76 | _lttng_cmd_create() { |
| 77 | options=$(lttng create --list-options) |
| 78 | |
| 79 | case $prev in |
| 80 | --output|-o) |
| 81 | _filedir -d |
| 82 | return |
| 83 | ;; |
| 84 | esac |
| 85 | |
| 86 | case $cur in |
| 87 | -*) |
| 88 | _lttng_complete_options |
| 89 | return |
| 90 | ;; |
| 91 | esac |
| 92 | } |
| 93 | |
| 94 | _lttng_cmd_destroy() { |
| 95 | options=$(lttng destroy --list-options) |
| 96 | |
| 97 | case $cur in |
| 98 | -*) |
| 99 | _lttng_complete_options |
| 100 | return |
| 101 | ;; |
| 102 | *) |
| 103 | _lttng_complete_sessions |
| 104 | return |
| 105 | ;; |
| 106 | esac |
| 107 | } |
| 108 | _lttng_cmd_disablechannel() { |
| 109 | options=$(lttng disable-channel --list-options) |
| 110 | |
| 111 | case $prev in |
| 112 | --session|-s) |
| 113 | _lttng_complete_sessions |
| 114 | return |
| 115 | ;; |
| 116 | esac |
| 117 | |
| 118 | case $cur in |
| 119 | -*) |
| 120 | _lttng_complete_options |
| 121 | return |
| 122 | ;; |
| 123 | esac |
| 124 | } |
| 125 | _lttng_cmd_disableevent() { |
| 126 | options=$(lttng disable-event --list-options) |
| 127 | |
| 128 | case $prev in |
| 129 | --session|-s) |
| 130 | _lttng_complete_sessions |
| 131 | return |
| 132 | ;; |
| 133 | --channel|-c) |
| 134 | return |
| 135 | ;; |
| 136 | esac |
| 137 | |
| 138 | case $cur in |
| 139 | -*) |
| 140 | _lttng_complete_options |
| 141 | return |
| 142 | ;; |
| 143 | esac |
| 144 | } |
| 145 | |
| 146 | _lttng_cmd_enablechannel() { |
| 147 | options=$(lttng enable-channel --list-options) |
| 148 | |
| 149 | case $prev in |
| 150 | --session|-s) |
| 151 | _lttng_complete_sessions |
| 152 | return |
| 153 | ;; |
| 154 | esac |
| 155 | |
| 156 | case $cur in |
| 157 | -*) |
| 158 | _lttng_complete_options |
| 159 | return |
| 160 | ;; |
| 161 | esac |
| 162 | } |
| 163 | |
| 164 | _lttng_cmd_enableevent() { |
| 165 | options=$(lttng enable-event --list-options) |
| 166 | |
| 167 | case $prev in |
| 168 | --session|-s) |
| 169 | _lttng_complete_sessions |
| 170 | return |
| 171 | ;; |
| 172 | --channel|-c) |
| 173 | return |
| 174 | ;; |
| 175 | --probe) |
| 176 | return |
| 177 | ;; |
| 178 | --function) |
| 179 | return |
| 180 | ;; |
| 181 | esac |
| 182 | |
| 183 | |
| 184 | #Check if we want kernel event completion |
| 185 | if [[ "$COMP_LINE" == *"-k"* ]]; then |
| 186 | _lttng_complete_kernel_events |
| 187 | return |
| 188 | fi |
| 189 | |
| 190 | #Check if we want ust event completion |
| 191 | if [[ "$COMP_LINE" == *"-u"* ]]; then |
| 192 | _lttng_complete_ust_events |
| 193 | return |
| 194 | fi |
| 195 | |
| 196 | #Check if we want jul event completion |
| 197 | if [[ "$COMP_LINE" == *"-j"* ]]; then |
| 198 | _lttng_complete_jul_events |
| 199 | return |
| 200 | fi |
| 201 | |
| 202 | case $cur in |
| 203 | -*) |
| 204 | _lttng_complete_options |
| 205 | return |
| 206 | ;; |
| 207 | esac |
| 208 | |
| 209 | } |
| 210 | |
| 211 | _lttng_cmd_list() { |
| 212 | options=$(lttng list --list-options) |
| 213 | |
| 214 | case $prev in |
| 215 | --channel|-c) |
| 216 | return |
| 217 | ;; |
| 218 | esac |
| 219 | |
| 220 | case $cur in |
| 221 | -*) |
| 222 | _lttng_complete_options |
| 223 | return |
| 224 | ;; |
| 225 | *) |
| 226 | _lttng_complete_sessions |
| 227 | return |
| 228 | esac |
| 229 | } |
| 230 | |
| 231 | _lttng_cmd_setsession() { |
| 232 | options=$(lttng set-session --list-options) |
| 233 | |
| 234 | case $cur in |
| 235 | -*) |
| 236 | _lttng_complete_options |
| 237 | return |
| 238 | ;; |
| 239 | *) |
| 240 | _lttng_complete_sessions |
| 241 | return |
| 242 | ;; |
| 243 | esac |
| 244 | } |
| 245 | |
| 246 | _lttng_cmd_snapshot() { |
| 247 | options=$(lttng snapshot --list-options) |
| 248 | commands=$(lttng snapshot --list-commands) |
| 249 | |
| 250 | _lttng_find_command $((command_found_index + 1)) |
| 251 | |
| 252 | if _lttng_cursor_is_after_command; then |
| 253 | case $prev in |
| 254 | --session|-s) |
| 255 | _lttng_complete_sessions |
| 256 | return |
| 257 | ;; |
| 258 | esac |
| 259 | |
| 260 | case $cur in |
| 261 | -*) |
| 262 | _lttng_complete_options |
| 263 | ;; |
| 264 | esac |
| 265 | else |
| 266 | _lttng_complete_commands |
| 267 | fi |
| 268 | } |
| 269 | |
| 270 | _lttng_cmd_start() { |
| 271 | options=$(lttng start --list-options) |
| 272 | |
| 273 | case $cur in |
| 274 | -*) |
| 275 | _lttng_complete_options |
| 276 | return |
| 277 | ;; |
| 278 | *) |
| 279 | _lttng_complete_sessions |
| 280 | return |
| 281 | ;; |
| 282 | esac |
| 283 | } |
| 284 | |
| 285 | _lttng_cmd_stop() { |
| 286 | options=$(lttng stop --list-options) |
| 287 | |
| 288 | case $cur in |
| 289 | -*) |
| 290 | _lttng_complete_options |
| 291 | ;; |
| 292 | *) |
| 293 | _lttng_complete_sessions |
| 294 | ;; |
| 295 | esac |
| 296 | } |
| 297 | |
| 298 | _lttng_cmd_version() { |
| 299 | options=$(lttng version --list-options) |
| 300 | |
| 301 | case $cur in |
| 302 | -*) |
| 303 | _lttng_complete_options |
| 304 | ;; |
| 305 | esac |
| 306 | } |
| 307 | |
| 308 | _lttng_cmd_view() { |
| 309 | options=$(lttng view --list-options) |
| 310 | |
| 311 | case $cur in |
| 312 | -*) |
| 313 | _lttng_complete_options |
| 314 | ;; |
| 315 | esac |
| 316 | } |
| 317 | |
| 318 | |
| 319 | |
| 320 | _lttng_before_command() { |
| 321 | # Check if the previous word should alter the behavior |
| 322 | case $prev in |
| 323 | --group|-g) |
| 324 | COMPREPLY=( $(compgen -g -- $cur) ) |
| 325 | return |
| 326 | ;; |
| 327 | --sessiond-path) |
| 328 | _filedir |
| 329 | return |
| 330 | ;; |
| 331 | esac |
| 332 | |
| 333 | case $cur in |
| 334 | -*) |
| 335 | # If the current word starts with a dash, complete with options |
| 336 | _lttng_complete_options |
| 337 | ;; |
| 338 | *) |
| 339 | # Otherwise complete with commands |
| 340 | _lttng_complete_commands |
| 341 | ;; |
| 342 | esac |
| 343 | } |
| 344 | |
| 345 | _lttng_after_command() { |
| 346 | local cmd_name |
| 347 | |
| 348 | cmd_name=_lttng_cmd_${command_found//-/} |
| 349 | |
| 350 | type -t $cmd_name | grep -q 'function' && $cmd_name |
| 351 | } |
| 352 | |
| 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 |
| 358 | return 0 |
| 359 | fi |
| 360 | done |
| 361 | |
| 362 | return 1 |
| 363 | } |
| 364 | |
| 365 | # Try to find a command in the current command line. Possible commands |
| 366 | # are passed in $commands. |
| 367 | # |
| 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. |
| 370 | # |
| 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() { |
| 376 | start=${1:-1} |
| 377 | |
| 378 | # The text of the found command |
| 379 | command_found="" |
| 380 | |
| 381 | # The index of the found command in COMP_WORDS |
| 382 | command_found_index=-1 |
| 383 | |
| 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 |
| 389 | break |
| 390 | fi |
| 391 | done |
| 392 | } |
| 393 | |
| 394 | _lttng_cursor_is_after_command() { |
| 395 | [ -n "$command_found" ] && [ "$COMP_CWORD" -gt "$command_found_index" ] |
| 396 | } |
| 397 | |
| 398 | _lttng() { |
| 399 | local cur prev commands command_found command_found_index |
| 400 | |
| 401 | # Get the current and previous word |
| 402 | _get_comp_words_by_ref cur prev |
| 403 | |
| 404 | # Get the valid first-level LTTng commands and options |
| 405 | commands=$(lttng --list-commands) |
| 406 | options=$(lttng --list-options) |
| 407 | |
| 408 | _lttng_find_command |
| 409 | |
| 410 | # Check if the cursor is before or after the command keyword |
| 411 | if _lttng_cursor_is_after_command; then |
| 412 | _lttng_after_command |
| 413 | else |
| 414 | _lttng_before_command |
| 415 | fi |
| 416 | } |
| 417 | |
| 418 | complete -F _lttng lttng |