Debian Codegraph @

extras/zsh-completion/_lttng

raw
1 #compdef lttng
2 #
3 # SPDX-FileCopyrightText: 2015-2025 Philippe Proulx <[email protected]>
4 # SPDX-License-Identifier: MIT
5 #
6 # This is a Zsh completion function for the lttng(1) command (see
7 # <https://lttng.org/>), for versions 2.5 to 2.15.
8 #
9 # If you want, at your own risk, the function to work with versions
10 # above 2.15, set `LTTNG_ZSH_COMP_IGNORE_VERSION_LIMIT=1`.
11 #
12 # Each subcommand changes the command part of the current context so
13 # that you can target a specific subcommand with `zstyle`, for
14 # example:
15 #
16 # $ zstyle ':completion:*:*:lttng-start:*:sessions' verbose yes
17 # $ zstyle ':completion:*:*:lttng-add-context:type-option:*' \
18 # group-order perf-context-types static-context-types
19 #
20 # The relevant style arguments and tags are:
21 #
22 # `actions` tag (with the `lttng-snapshot` command):
23 # Snapshot action names.
24 #
25 # `channel` argument and `channels` tag:
26 # Channel names.
27 #
28 # The `verbose` style controls whether or not this function adds
29 # completions as a list (with channel information for each item) or
30 # simple channel names.
31 #
32 # If the verbose style is enabled, then the information part of each
33 # item starts with either ` [enabled` or ` [disabled` (note the
34 # two leading spaces), depending on the status of the channel.
35 #
36 # `commands` tag:
37 # lttng(1) subcommand names.
38 #
39 # `log-levels` tag:
40 # Log level names.
41 #
42 # `session` argument and `sessions` tag:
43 # Recording session names.
44 #
45 # The `verbose` style controls whether or not this function adds
46 # completions as a list (with recording session information for each
47 # item) or simple recording session names.
48 #
49 # If the verbose style is enabled, then the information part of each
50 # item starts with either ` [active` or ` [inactive` (note the two
51 # leading spaces), depending on the status of the recording session.
52 #
53 # `type-option` argument:
54 # `static-context-types` tag:
55 # Statically-known context field types.
56 #
57 # `perf-context-types` tag:
58 # perf counter context field types.
59 #
60 # `triggers` tag:
61 # Trigger names.
62
63 # Sets the `minor_version` parameter to the minor version of
64 # LTTng-tools, or to `0` if not found.
65 __lttng_set_minor_version() {
66 minor_version=0
67
68 local -a match
69
70 if [[ $($prog_name --version) =~ '[[:blank:]]+2\.([[:digit:]]+)' ]]; then
71 minor_version=$match[1]
72 fi
73 }
74
75 # Usage:
76 #
77 # __lttng_run [ARG]...
78 #
79 # Runs the `lttng` command with the given ARG arguments
80 # using `tracing_group`.
81 __lttng_run() {
82 $prog_name --no-sessiond --group=$tracing_group "$@" 2>/dev/null
83 }
84
85 # Usage:
86 #
87 # __lttng_mi_run MSG [ARG]...
88 #
89 # Runs the `__lttng_run --mi=xml` command with the given ARG values, and
90 # sets `REPLY` to the XML output.
91 #
92 # On failure, show MSG.
93 #
94 # This function returns 1 with LTTng-tools < 2.6 and if `xmllint` isn't
95 # available. In this case, this function adds completion through
96 # _message(). Because of this, do NOT call this function in a subshell.
97 __lttng_mi_run() {
98 local -r msg=$1
99
100 shift
101
102 if ((minor_version < 6)); then
103 # MI output requires LTTng-tools ≥ 2.6
104 _message -e descriptions $msg
105 return 1
106 fi
107
108 if ((! $+commands[xmllint])); then
109 # XML output parsing requires xmllint
110 _message -e descriptions $msg
111 return 1
112 fi
113
114 if ! REPLY=$(__lttng_run --mi=xml "$@"); then
115 # Command failed
116 _message -e descriptions $msg
117 return 1
118 fi
119 }
120
121 # Usage:
122 #
123 # __lttng_style_is_verbose ARG TAG
124 #
125 # Returns whether or not the verbose style is enabled for the given
126 # argument ARG and tag TAG, using `curcontext` for the other parts of
127 # the full completion context.
128 __lttng_style_is_verbose() {
129 local -r arg=$1 tag=$2
130 local -r ccparts=("${(@s.:.)curcontext}")
131
132 zstyle -t :completion:${ccparts[1]}:${ccparts[2]}:${ccparts[3]}:$arg:$tag verbose
133 }
134
135 # Returns 0 if a Linux kernel tracing domain option is set in
136 # `opt_args`, or 1 otherwise.
137 __lttng_kernel_domain_opt_is_set() {
138 (($+opt_args[-k] || $+opt_args[--kernel] ||
139 $+opt_args[domain--k] || $+opt_args[domain---kernel]))
140 }
141
142 # Returns 0 if a user space tracing domain option is set in
143 # `opt_args`, or 1 otherwise.
144 __lttng_user_domain_opt_is_set() {
145 (($+opt_args[-u] || $+opt_args[--userspace] ||
146 $+opt_args[domain--u] || $+opt_args[domain---userspace]))
147 }
148
149 # Returns 0 if a `java.util.logging` tracing domain option is set in
150 # `opt_args`, or 1 otherwise.
151 __lttng_jul_domain_opt_is_set() {
152 (($+opt_args[-j] || $+opt_args[--jul] ||
153 $+opt_args[domain--j] || $+opt_args[domain---jul]))
154 }
155
156 # Returns 0 if an Apache log4j 1.x tracing domain option is set in
157 # `opt_args`, or 1 otherwise.
158 __lttng_log4j1_domain_opt_is_set() {
159 (($+opt_args[-l] || $+opt_args[--log4j] ||
160 $+opt_args[domain--l] || $+opt_args[domain---log4j]))
161 }
162
163 # Returns 0 if an Apache Log4j 2 tracing domain option is set in
164 # `opt_args`, or 1 otherwise.
165 __lttng_log4j2_domain_opt_is_set() {
166 (($+opt_args[--log4j2] || $+opt_args[domain---log4j2]))
167 }
168
169 # Returns 0 if a Python tracing domain option is set in `opt_args`,
170 # or 1 otherwise.
171 __lttng_python_domain_opt_is_set() {
172 (($+opt_args[-p] || $+opt_args[--python] ||
173 $+opt_args[domain--p] || $+opt_args[domain---python]))
174 }
175
176 # Usage:
177 #
178 # __lttng_exclude_opts OPT...
179 #
180 # Excludes one or more options OPT from the possible options.
181 #
182 # Each OPT must match `--*` (long) or `-*` (short).
183 #
184 # This function removes description lines from the `long_opts` and
185 # `short_opts` arrays.
186 __lttng_exclude_opts() {
187 local opt
188
189 for opt in "$@"; do
190 if [[ $opt == --* ]]; then
191 long_opts=("${(@)long_opts:#$opt:*}")
192 elif [[ $opt == -* ]]; then
193 short_opts=("${(@)short_opts:#$opt:*}")
194 fi
195 done
196 }
197
198 # Usage:
199 #
200 # __lttng_add_opts LONGOPT [SHORTOPT] DESCR
201 #
202 # Adds an option LONGOPT to `long_opts` and optionally short option
203 # SHORTOPT to `short_opts` with the description DESCR.
204 __lttng_add_opts() {
205 local -r long_opt=$1
206 local short_opt descr
207
208 if [[ $2 == -* ]]; then
209 short_opt=$2
210 descr=$3
211 else
212 descr=$2
213 fi
214
215 long_opts+=("$long_opt:$descr")
216
217 if [[ -n $short_opt ]]; then
218 short_opts+=("$short_opt:$descr")
219 fi
220 }
221
222 # Adds completions for the name of an `lttng` command.
223 __lttng_complete_cmd_name() {
224 # LTTng-tools 2.5+
225 local cmd_names=(
226 'add-context:add context fields to be recorded'
227 'create:create a recording session'
228 'destroy:destroy a recording session'
229 'disable-channel:disable channels'
230 'disable-event:disable recording event rules'
231 'enable-channel:create or enable a channel'
232 'enable-event:create or enable recording event rules'
233 'help:show the help of a command'
234 'list:list recording sessions and instrumentation points'
235 'set-session:set the current recording session'
236 'snapshot:take a recording session snapshot'
237 'start:start a recording session'
238 'stop:stop a recording session'
239 'version:show LTTng-tools version information'
240 'view:launch a trace reader'
241 )
242
243 # LTTng-tools 2.6+
244 if ((minor_version >= 6)); then
245 cmd_names+=(
246 'load:load recording session configurations'
247 'save:save recording session configurations'
248 )
249 fi
250
251 # LTTng-tools 2.7+
252 if ((minor_version >= 7)); then
253 cmd_names+=(
254 'track:allow specific processes to record events'
255 'untrack:disallow specific processes to record events'
256 )
257 fi
258
259 # LTTng-tools 2.8+
260 if ((minor_version >= 8)); then
261 cmd_names+=(
262 'status:show the status of the current recording session'
263 )
264 fi
265
266 # LTTng-tools 2.9+
267 if ((minor_version >= 9)); then
268 cmd_names+=(
269 'regenerate:regenerate specific recording session data'
270 )
271 fi
272
273 # LTTng-tools 2.11+
274 if ((minor_version >= 11)); then
275 cmd_names+=(
276 'rotate:archive the current trace chunk of a recording session'
277 'enable-rotation:set a recording session rotation schedule'
278 'disable-rotation:unset a recording session rotation schedule'
279 )
280 fi
281
282 # LTTng-tools 2.12+
283 if ((minor_version >= 12)); then
284 cmd_names+=(
285 'clear:clear a recording session'
286 )
287 fi
288
289 # LTTng-tools 2.13+
290 if ((minor_version >= 13)); then
291 cmd_names+=(
292 'add-trigger:add a trigger'
293 'list-triggers:list triggers'
294 'remove-trigger:remove a trigger'
295 )
296 fi
297
298 # LTTng-tools 2.15+
299 if ((minor_version >= 15)); then
300 cmd_names+=(
301 'reclaim-memory:reclaim channel memory immediately'
302 )
303 fi
304
305 # Add completions
306 _describe -t commands command cmd_names
307 }
308
309 # Adds completions for the name of an `lttng snapshot` action.
310 __lttng_complete_snapshot_action_name() {
311 local -r action_names=(
312 'add-output:add a snapshot output'
313 'del-output:remove a snapshot output'
314 'list-output:show the snapshot output'
315 'record:take a snapshot'
316 )
317
318 # Add completions
319 _describe -t actions 'snapshot action' action_names
320 }
321
322 # Adds user ID completions.
323 #
324 # The added completions are displayed as user names, but the actual
325 # completions are corresponding user IDs.
326 __lttng_complete_uid() {
327 if [[ ! -f /etc/passwd ]]; then
328 # Weird flex but ok
329 _message -e descriptions 'user ID'
330 return
331 fi
332
333 # Read user names and IDs from `/etc/passwd`
334 local line
335 local -a uids names fields
336
337 while read -r line; do
338 if [[ -z ${line// } ]]; then
339 # Skip empty line
340 continue
341 fi
342
343 # Extract user ID and name
344 fields=("${(@s/:/)line}")
345 uids+=($fields[3])
346 names+=("$fields[1]")
347 done < /etc/passwd
348
349 # Add completions
350 expl=
351 _wanted users expl 'user ID' compadd -d names -a uids
352 }
353
354 # Adds completions for a context field type (for the `--type` option of
355 # the `add-context` command).
356 #
357 # This function replaces the argument field of the current context with
358 # `type-option`.
359 #
360 # This function relies on the tracing domain options of `opt_args` to
361 # restrict the offered completions. Without a tracing domain option,
362 # this function adds all the possible context field type completions.
363 #
364 # This function runs `lttng add-context --list` to get the list of
365 # available context field types.
366 #
367 # This function adds completions with both the `static-context-types`
368 # and `perf-context-types` tags so that the Zsh user can hide a group or
369 # select them with the `group-order` and `tag-order` style.
370 __lttng_complete_context_type() {
371 # Statically-known context field types
372 local kernel_types=(
373 pid procname prio nice vpid tid vtid ppid vppid hostname
374 interruptible preemptible need_reschedule migratable
375 callstack-kernel callstack-user cgroup_ns ipc_ns mnt_ns net_ns
376 pid_ns time_ns user_ns uts_ns uid euid suid gid egid sgid vuid veuid
377 vsuid vgid vegid vsgid
378 )
379 local user_types=(
380 vpid pthread_id procname ip cgroup_ns ipc_ns mnt_ns net_ns pid_ns
381 time_ns user_ns uts_ns vuid veuid vsuid vgid vegid vsgid
382 )
383
384 # `cpu_id` user context type requires LTTng-tools ≥ 2.14
385 if ((minor_version >= 14)); then
386 user_types+=(cpu_id)
387 fi
388
389 # Set `types` and `domain` depending on the selected tracing domain
390 local -a types
391 local domain descr_suffix
392
393 if __lttng_kernel_domain_opt_is_set; then
394 # Kernel context field types
395 types=($kernel_types)
396 domain=k
397 elif __lttng_user_domain_opt_is_set; then
398 # User space context field types
399 types=($user_types)
400 domain=u
401 else
402 # No supported tracing domain: offer all context field types
403 types=($user_types $kernel_types)
404 descr_suffix=' (all tracing domains)'
405 fi
406
407 # Get XML list of available context field types
408 local -r msg='context type'
409
410 if ! __lttng_mi_run $msg add-context --list; then
411 return 0
412 fi
413
414 local list_xml=$REPLY
415
416 # Convert to one context field type per line
417 local lines
418
419 if ! lines=$(__lttng_xmllint_xpath $list_xml '//*[local-name()="symbol"]/text()'); then
420 _guard '^-*' $msg
421 return
422 fi
423
424 # Filter context field types depending on the selected tracing domain
425 local -a static_items perf_items
426 local line
427
428 while read -r line; do
429 if (($types[(Ie)$line])); then
430 # Statically-known context field type
431 static_items+=$line
432 elif [[ $line == perf:cpu:* && $domain != u ]]; then
433 # Per-CPU perf counter
434 perf_items+=$line
435 elif [[ $line == perf:thread:* && $domain != k ]]; then
436 # Per-thread perf counter
437 perf_items+=$line
438 elif [[ $line == perf:* && $domain != u ]]; then
439 # Other perf counter (Linux kernel tracing domain or none)
440 perf_items+=$line
441 fi
442 done <<< $lines
443
444 # Add completions
445 _alternative -C type-option \
446 "static-context-types:statically-known context type${descr_suffix}:compadd -a static_items" \
447 "perf-context-types:perf counter context type${descr_suffix}:compadd -a perf_items"
448 }
449
450 # Adds completions for a trigger name.
451 #
452 # Relies on `opt_args[--owner-uid]` to restrict the offered
453 # completions.
454 __lttng_complete_trigger_name() {
455 # Get XML list of available triggers
456 local msg='trigger name'
457
458 if ! __lttng_mi_run $msg list-triggers; then
459 return 0
460 fi
461
462 local list_xml=$REPLY
463
464 # Create the owner UID predicate XPath part
465 local owner_uid_xpath
466
467 if (($+opt_args[--owner-uid])); then
468 owner_uid_xpath="[./*[local-name()='owner_uid'] = ${opt_args[--owner-uid]}]"
469 fi
470
471 # Convert to one trigger name per line
472 local lines
473
474 if ! lines=$(__lttng_xmllint_xpath $list_xml "//*[local-name()='trigger']$owner_uid_xpath/*[local-name()='name']/text()"); then
475 _guard '^-*' $msg
476 return
477 fi
478
479 local line
480 local -a trigger_names
481
482 while read -r line; do
483 trigger_names+=$line
484 done <<< $lines
485
486 # Add completions
487 expl=
488 _wanted triggers expl $msg compadd -a trigger_names
489 }
490
491 # Usage:
492 #
493 # __lttng_xmllint_xpath XMLDOC XPATH
494 #
495 # Runs `xmllint` with XML document XMLDOC and XPath XPATH
496 # to extract data.
497 __lttng_xmllint_xpath() {
498 local -r xml=$1 xpath=$2
499
500 xmllint --xpath $xpath - <<< $xml 2>/dev/null
501 }
502
503 # Usage:
504 #
505 # __lttng_session_status_from_enabled_prop PROP
506 #
507 # Sets `REPLY` to the equivalent recording session status adjective
508 # (`active` or `inactive`) for an `enabled` XML property PROP, or
509 # `unknown` on error.
510 __lttng_session_status_from_enabled_prop() {
511 local -r prop=$1
512
513 case $prop in
514 true) REPLY=active;;
515 false) REPLY=inactive;;
516 *) REPLY=unknown;;
517 esac
518 }
519
520 # Usage:
521 #
522 # __lttng_immediate_prop_from_xml NODEXML NODENAME PROPNAME
523 #
524 # Prints the value of the immediate XML property named PROPNAME of the
525 # child node named NODENAME within the parent node NODEXML.
526 __lttng_immediate_prop_from_xml() {
527 local -r node_xml=$1 node_name=$2 prop_name=$3
528
529 __lttng_xmllint_xpath $node_xml "/*[local-name()='$node_name']/*[local-name()='$prop_name']/text()"
530 }
531
532 # Usage:
533 #
534 # __lttng_name_prop_from_xml NODEXML NODENAME
535 #
536 # Prints the value of the immediate XML `name` property of the child
537 # node named NODENAME within the parent node NODEXML.
538 __lttng_name_prop_from_xml() {
539 local -r node_xml=$1 node_name=$2
540
541 __lttng_immediate_prop_from_xml $node_xml $node_name name
542 }
543
544 # Usage:
545 #
546 # __lttng_enabled_prop_from_xml NODEXML NODENAME
547 #
548 # Prints the value of the immediate XML `enabled` property of the child
549 # node named NODENAME within the parent node NODEXML.
550 __lttng_enabled_prop_from_xml() {
551 local -r node_xml=$1 node_name=$2
552
553 __lttng_immediate_prop_from_xml $node_xml $node_name enabled
554 }
555
556 # Usage:
557 #
558 # __lttng_complete_session_name REQSTATUS REQMODE
559 #
560 # Adds completions for a recording session name.
561 #
562 # REQSTATUS is one of:
563 #
564 # `all`:
565 # Add completions for active and inactive recording sessions.
566 #
567 # `active`:
568 # Only add completions for active recording sessions.
569 #
570 # `inactive`:
571 # Only add completions for inactive recording sessions.
572 #
573 # REQMODE is one of:
574 #
575 # `all`:
576 # Add completions for recording sessions regardless of their mode.
577 #
578 # `snapshot`:
579 # Only add completions for snapshot recording sessions.
580 #
581 # `live`:
582 # Only add completions for LTTng live recording sessions.
583 __lttng_complete_session_name() {
584 local -r req_status=$1 req_mode=$2
585
586 # Get XML document (summary of recording sessions)
587 if ! __lttng_mi_run 'recording session name' list; then
588 return 0
589 fi
590
591 local session_summaries_xml=$REPLY
592
593 # Count recording sessions
594 local -i session_count
595
596 session_count=$(__lttng_xmllint_xpath $session_summaries_xml 'count(//*[local-name()="session"])')
597
598 if (($? != 0 || session_count == 0)); then
599 _guard '^-*' 'recording session name'
600 return
601 fi
602
603 # Append the name and info of one recording session at a time
604 local -a session_names session_infos
605 local -i index iprop
606 local session_name session_summary_xml session_mode session_status session_info
607
608 # For each recording session summary
609 for index in {1..$session_count}; do
610 # Get recording session summary XML node
611 if ! session_summary_xml=$(__lttng_xmllint_xpath $session_summaries_xml "//*[local-name()='session'][$index]"); then
612 continue
613 fi
614
615 # Get recording session name
616 if ! session_name=$(__lttng_name_prop_from_xml $session_summary_xml session); then
617 continue
618 fi
619
620 # Get recording session status
621 if ! session_status=$(__lttng_enabled_prop_from_xml $session_summary_xml session); then
622 continue
623 fi
624
625 __lttng_session_status_from_enabled_prop $session_status
626 session_status=$REPLY
627
628 if [[ $req_status != all && $req_status != $session_status ]]; then
629 # Skip recording session with unexpected status
630 continue
631 fi
632
633 # Get recording session mode
634 session_mode=
635 iprop=$(__lttng_immediate_prop_from_xml $session_summary_xml session snapshot_mode)
636
637 if (($? == 0 && iprop)); then
638 session_mode=snapshot
639 else
640 iprop=$(__lttng_immediate_prop_from_xml $session_summary_xml session live_timer_interval)
641
642 if (($? == 0 && iprop)); then
643 session_mode=live
644 fi
645 fi
646
647 if [[ $req_mode != all && $req_mode != $session_mode ]]; then
648 # Skip recording session with unexpected mode
649 continue
650 fi
651
652 session_info=$session_status
653
654 if [[ -n $session_mode ]]; then
655 session_info+=", $session_mode mode"
656 fi
657
658 session_names+=("$session_name")
659 session_infos+=($session_info)
660 done
661
662 # No recording sessions: show message
663 if (($#session_names == 0)); then
664 _guard '^-*' 'recording session name'
665 return
666 fi
667
668 # Compute maximum session info length
669 local -i max_info_len=0 len
670
671 for session_info in $session_infos; do
672 len=$#session_info
673
674 if ((len > max_info_len)); then
675 max_info_len=$len
676 fi
677 done
678
679 # Compute maximum session name length
680 local -i max_name_len=0
681
682 for session_name in $session_names; do
683 len=$#session_name
684
685 if ((len > max_name_len)); then
686 max_name_len=$len
687 fi
688 done
689
690 # Some room for the longest info string, two spaces, and two brackets
691 local -ir max_possible_name_len=$((COLUMNS - max_info_len - 5))
692
693 if ((max_name_len > max_possible_name_len)); then
694 # Clamp
695 max_name_len=$max_possible_name_len
696 fi
697
698 # Create the dislay strings (name, status, mode)
699 local -a disps
700
701 for index in {1..${#session_names}}; do
702 disps+=("${(r:$max_name_len:: :)session_names[$index]} [$session_infos[$index]]")
703 done
704
705 # Add completions
706 expl=
707
708 if __lttng_style_is_verbose session sessions; then
709 # Verbose mode (list with infos)
710 _wanted -C session sessions expl 'recording session name' \
711 compadd -d disps -l -a session_names
712 else
713 # Just the recording session names
714 _wanted -C session sessions expl 'recording session name' \
715 compadd -a session_names
716 fi
717 }
718
719 # Sets `REPLY` to the name of the Unix user's current recording session,
720 # if any.
721 __lttng_cur_session_name() {
722 local -r lttngrc_path=${LTTNG_HOME:-$HOME}/.lttngrc
723
724 if [[ ! -f $lttngrc_path ]]; then
725 return 1
726 fi
727
728 local line cur_session_name
729 local -a match
730
731 while read -r line || [[ -n $line ]]; do
732 if [[ $line =~ 'session=([^[:blank:]]+)' ]]; then
733 cur_session_name=$match[1]
734 break
735 fi
736 done < $lttngrc_path
737
738 if [[ -z $cur_session_name ]]; then
739 return 1
740 fi
741
742 REPLY=$cur_session_name
743 }
744
745 # Usage:
746 #
747 # __lttng_status_from_enabled_prop PROP
748 #
749 # Sets `REPLY` to the equivalent status adjective (`enabled` or
750 # `disabled`) for an `enabled` XML property PROP, or `unknown` on error.
751 __lttng_status_from_enabled_prop() {
752 local -r prop=$1
753
754 case $prop in
755 true) REPLY=enabled;;
756 false) REPLY=disabled;;
757 *) REPLY=unknown;;
758 esac
759 }
760
761 # Usage:
762 #
763 # __lttng_session_xml MSG SESSIONNAME
764 #
765 # Sets `REPLY` to the XML document of `lttng list SESSIONNAME`
766 # (specifics of a single recording session).
767 #
768 # On error, show MSG.
769 #
770 # This function calls __lttng_mi_run(); see notes about
771 # __lttng_mi_run().
772 __lttng_session_xml() {
773 local -r msg=$1 session_name=$2
774
775 __lttng_mi_run $msg list $session_name
776 }
777
778 # Usage:
779 #
780 # __lttng_complete_channel_name REQSTATUS SRC
781 #
782 # Adds completions for a channel name.
783 #
784 # This function relies on the tracing domain options of `opt_args`,
785 # `opt_args[-s]`, `opt_args[--session]`, and the first non-option
786 # argument to restrict the offered completions.
787 #
788 # REQSTATUS is one of:
789 #
790 # `all`:
791 # Add completions for enabled and disabled channels.
792 #
793 # `enabled`:
794 # Only add completions for enabled channels.
795 #
796 # `disabled`:
797 # Only add completions for disabled channels.
798 #
799 # SRC is one of:
800 #
801 # `opt`:
802 # Use `opt_args[-s]` and `opt_args[--session]` to find the name of
803 # the selected recording session.
804 #
805 # `arg`:
806 # Use `line[1]` to find the name of the selected recording session.
807 #
808 # Anything else:
809 # Use the current recording session name.
810 __lttng_complete_channel_name() {
811 local -r req_status=$1 session_name_src=$2
812
813 shift 2
814
815 # Find the recording session name to use
816 local session_name
817
818 if [[ $session_name_src == opt ]]; then
819 if (($+opt_args[-s])); then
820 session_name=$opt_args[-s]
821 elif (($+opt_args[--session])); then
822 session_name=$opt_args[--session]
823 fi
824 elif [[ $session_name_src == arg ]]; then
825 session_name=$line[1]
826 fi
827
828 if [[ -z $session_name ]]; then
829 # Fall back to current recording session
830 if ! __lttng_cur_session_name; then
831 _guard '^-*' 'channel name'
832 return
833 fi
834
835 session_name=$REPLY
836 fi
837
838 # Get XML document (detailed recording session)
839 if ! __lttng_session_xml 'channel name' $session_name; then
840 return 0
841 fi
842
843 local session_xml=$REPLY
844
845 # Count tracing domains
846 local -i domain_count
847
848 domain_count=$(__lttng_xmllint_xpath $session_xml 'count(//*[local-name()="domain"])')
849
850 if (($? != 0 || domain_count == 0)); then
851 _guard '^-*' 'channel name'
852 return
853 fi
854
855 # Append the name and info of one channel at a time
856 local domain_xml domain prop
857 local channel_xml channel_name channel_status channel_erl_mode
858 local -a channel_names channel_infos
859 local -i channel_count domain_index channel_index
860
861 # For each tracing domain
862 for domain_index in {1..$domain_count}; do
863 # Get tracing domain XML node
864 if ! domain_xml=$(__lttng_xmllint_xpath $session_xml "//*[local-name()='domain'][$domain_index]"); then
865 continue
866 fi
867
868 # Get tracing domain type
869 if ! domain=$(__lttng_xmllint_xpath $domain_xml '/*/*[local-name()="type"]/text()'); then
870 continue
871 fi
872
873 # Skip unexpected tracing domains
874 if [[ $domain == KERNEL ]]; then
875 if __lttng_user_domain_opt_is_set; then
876 # Skip unexpected Linux kernel tracing domain
877 continue
878 fi
879
880 domain='Linux kernel'
881 fi
882
883 if [[ $domain == UST ]]; then
884 if __lttng_kernel_domain_opt_is_set; then
885 # Skip unexpected user space tracing domain
886 continue
887 fi
888
889 domain='user space'
890 fi
891
892 # Count channels within tracing domain
893 channel_count=$(__lttng_xmllint_xpath $domain_xml 'count(//*[local-name()="channel"])')
894
895 if (($? != 0 || channel_count == 0)); then
896 continue
897 fi
898
899 # For each channel
900 for channel_index in {1..$channel_count}; do
901 # Get channel XML node
902 if ! channel_xml=$(__lttng_xmllint_xpath $domain_xml "//*[local-name()='channel'][$channel_index]"); then
903 continue
904 fi
905
906 # Get channel name
907 if ! channel_name=$(__lttng_name_prop_from_xml $channel_xml channel); then
908 continue
909 fi
910
911 # Get channel status
912 if ! channel_status=$(__lttng_enabled_prop_from_xml $channel_xml channel); then
913 continue
914 fi
915
916 __lttng_status_from_enabled_prop $channel_status
917 channel_status=$REPLY
918
919 if [[ $req_status != all && $req_status != $channel_status ]]; then
920 # Skip channel with unexpected status
921 continue
922 fi
923
924 # Get channel event record loss mode
925 if ! channel_erl_mode=$(__lttng_xmllint_xpath $channel_xml '//*[local-name()="attributes"]/*[local-name()="overwrite_mode"]/text()'); then
926 continue
927 fi
928
929 if [[ $channel_erl_mode == DISCARD ]]; then
930 channel_erl_mode=discard
931 elif [[ $channel_erl_mode == OVERWRITE ]]; then
932 channel_erl_mode=overwrite
933 fi
934
935 channel_names+=("$channel_name")
936 channel_infos+=("$channel_status, $domain, $channel_erl_mode mode")
937 done
938 done
939
940 # No channels: show message
941 if (($#channel_names == 0)); then
942 _guard '^-*' 'channel name'
943 return
944 fi
945
946 # Compute maximum channel info length
947 local channel_info
948 local -i max_info_len=0 len
949
950 for channel_info in $channel_infos; do
951 len=$#channel_info
952
953 if ((len > max_info_len)); then
954 max_info_len=$len
955 fi
956 done
957
958 # Compute maximum channel name length
959 local -i max_name_len=0
960
961 for channel_name in $channel_names; do
962 len=$#channel_name
963
964 if ((len > max_name_len)); then
965 max_name_len=$len
966 fi
967 done
968
969 # Some room for the longest info string, two spaces, and two brackets
970 local -ir max_possible_name_len=$((COLUMNS - max_info_len - 5))
971
972 if ((max_name_len > max_possible_name_len)); then
973 # Clamp
974 max_name_len=$max_possible_name_len
975 fi
976
977 # Create the dislay strings (name, status, tracing domain, mode)
978 local -a disps
979
980 for channel_index in {1..${#channel_names}}; do
981 disps+=("${(r:$max_name_len:: :)channel_names[$channel_index]} [$channel_infos[$channel_index]]")
982 done
983
984 # Add completions
985 expl=
986
987 if __lttng_style_is_verbose channel channels; then
988 # Verbose mode (list with infos).
989 #
990 # Using `-2` as Linux kernel and user space channels may have the
991 # same name, but we want to show the different infos.
992 _wanted -C channel channels expl 'channel name' \
993 compadd "$@" -2 -d disps -l -a channel_names
994 else
995 # Just the channel names, no duplicates
996 _wanted -C channel channels expl 'channel name' \
997 compadd "$@" -a channel_names
998 fi
999 }
1000
1001 # Adds completions for instrumentation point names.
1002 #
1003 # This function relies on the tracing domain options of `opt_args` and
1004 # `opt_args[ip---syscall]` to restrict the offered completions.
1005 __lttng_complete_ip_name() {
1006 local msg
1007 local -a list_opts
1008
1009 if __lttng_kernel_domain_opt_is_set; then
1010 list_opts=(-k)
1011
1012 if (($+opt_args[ip---syscall])); then
1013 msg='system call name (no `sys_` prefix)'
1014 list_opts+=(--syscall)
1015 else
1016 msg='Linux kernel tracepoint name'
1017 fi
1018 elif __lttng_user_domain_opt_is_set; then
1019 msg='user space tracepoint name'
1020 list_opts=(-u)
1021 elif __lttng_jul_domain_opt_is_set; then
1022 msg='`java.util.logging` logger name'
1023 list_opts=(-j)
1024 elif __lttng_log4j1_domain_opt_is_set; then
1025 msg='Apache log4j 1.x logger name'
1026 list_opts=(-l)
1027 elif __lttng_log4j2_domain_opt_is_set; then
1028 msg='Apache Log4j 2 logger name'
1029 list_opts=(--log4j2)
1030 elif __lttng_python_domain_opt_is_set; then
1031 msg='Python logger name'
1032 list_opts=(-p)
1033 else
1034 # No tracing domain option
1035 _guard '^-*' 'instrumentation point or recording event rule name'
1036 return
1037 fi
1038
1039 # Get XML list of available instrumentation point names
1040 if ! __lttng_mi_run $msg list $list_opts; then
1041 return 0
1042 fi
1043
1044 local list_xml=$REPLY
1045
1046 # Convert to one instrumentation point name per line
1047 local ip_names
1048
1049 ip_names=$(__lttng_xmllint_xpath $list_xml '//*[local-name()="event"]//*[local-name()="name"]/text()')
1050
1051 if (($? != 0)) || [[ -z $ip_names ]]; then
1052 # `xmllint` error or no instrumentation points
1053 _guard '^-*' $msg
1054 return
1055 fi
1056
1057 local ip_name
1058 local -a items
1059
1060 while read -r ip_name; do
1061 if [[ -z $ip_name ]]; then
1062 continue
1063 fi
1064
1065 items+=($ip_name)
1066 done <<< $ip_names
1067
1068 # Add completions
1069 expl=
1070 _wanted instrumentation-points expl $msg compadd "$@" -a items
1071 }
1072
1073 # Adds completions for the arguments of the `add-context` command.
1074 __lttng_complete_add_context_cmd() {
1075 specs=(
1076 $help_opt_specs
1077 '(- : *)--list[list the available context field types]'
1078 '(--list -s --session)'{-s+,--session=}'[select a specific recording session]: : __lttng_complete_session_name inactive all'
1079 '(--list -c --channel)'{-c+,--channel=}'[select a specific channel]: : __lttng_complete_channel_name enabled opt'
1080 '(--list)*'{-t+,--type=}'[add a context field to be recorded]: : __lttng_complete_context_type'
1081 + '(domain)'
1082 '(--list)'{-k,--kernel}'[select the Linux kernel tracing domain]'
1083 '(--list)'{-u,--userspace}'[select the user space tracing domain]'
1084 )
1085
1086 # The `java.util.logging` and Apache log4j 1.x tracing domain options
1087 # require LTTng-tools ≥ 2.8 (for application-specific context field
1088 # types).
1089 if ((minor_version >= 8)); then
1090 specs+=(
1091 '(--list)'{-j,--jul}'[select the `java.util.logging` tracing domain]'
1092 '(--list)'{-l,--log4j}'[select the Apache log4j 1.x tracing domain]'
1093 )
1094 fi
1095
1096 # The Apache Log4j 2 tracing domain option requires LTTng-tools ≥ 2.14
1097 # (for application-specific context field types).
1098 if ((minor_version >= 14)); then
1099 specs+=(
1100 '(--list)--log4j2[select the Apache Log4j 2 tracing domain]'
1101 )
1102 fi
1103
1104 # Add completions
1105 _arguments -s -W : $specs
1106 }
1107
1108 # Usage:
1109 #
1110 # __lttng_complete_log_level TITLE
1111 #
1112 # Adds completions for log level names/rule.
1113 #
1114 # This function relies on the tracing domain options of `opt_args` to
1115 # restrict the offered completions.
1116 #
1117 # TITLE is the title of the message to show.
1118 __lttng_complete_log_level() {
1119 local -r title=$1
1120 local -a log_levels
1121
1122 # Fill the `log_levels` array depending on the tracing domain option
1123 if __lttng_user_domain_opt_is_set; then
1124 log_levels=(
1125 EMERG
1126 ALERT
1127 CRIT
1128 ERR
1129 WARNING
1130 NOTICE
1131 INFO
1132 DEBUG_SYSTEM
1133 DEBUG_PROGRAM
1134 DEBUG_PROCESS
1135 DEBUG_MODULE
1136 DEBUG_UNIT
1137 DEBUG_FUNCTION
1138 DEBUG_LINE
1139 DEBUG
1140 )
1141 elif __lttng_jul_domain_opt_is_set; then
1142 log_levels=(
1143 OFF
1144 SEVERE
1145 WARNING
1146 INFO
1147 CONFIG
1148 FINE
1149 FINER
1150 FINEST
1151 ALL
1152 )
1153 elif __lttng_log4j1_domain_opt_is_set || __lttng_log4j2_domain_opt_is_set; then
1154 log_levels=(
1155 OFF
1156 FATAL
1157 ERROR
1158 WARN
1159 INFO
1160 DEBUG
1161 TRACE
1162 ALL
1163 )
1164 elif __lttng_python_domain_opt_is_set; then
1165 log_levels=(
1166 CRITICAL
1167 ERROR
1168 WARNING
1169 INFO
1170 DEBUG
1171 NOTSET
1172 )
1173 else
1174 # No tracing domain option
1175 _guard '^-*' "$title (no tracing domain option found)"
1176 return
1177 fi
1178
1179 # Add completions
1180 expl=
1181 _wanted log-levels expl $title compadd -a log_levels -o nosort
1182 }
1183
1184 # Forwards to __lttng_complete_log_level() when completing the
1185 # log level options of the `enable-event` command.
1186 __lttng_complete_log_level_name() {
1187 __lttng_complete_log_level "log level name"
1188 }
1189
1190 # Adds completions for a trigger rate policy.
1191 __lttng_complete_trigger_rate_policy() {
1192 local -r types=(
1193 'once-after:execute the action once after N execution requests'
1194 'every:execute the action every N execution requests'
1195 )
1196
1197 # Add completions
1198 _describe 'trigger rate policy type' types -S ':'
1199 }
1200
1201 # Tries to complete common trigger action options (`--rate-policy`
1202 # and `--action`).
1203 #
1204 # Uses the `in_opt_val` parameter.
1205 #
1206 # Sets `opt_completed` to `1` if an option was recognized and completion
1207 # attempted, or `0` otherwise.
1208 #
1209 # Returns the status of the completion function when `opt_completed`
1210 # is `1`.
1211 __lttng_try_complete_action_opt() {
1212 opt_completed=1
1213
1214 if [[ $in_opt_val == rate-policy ]] || compset -P '--rate-policy=*:'; then
1215 _message -e descriptions 'execution request count'
1216 return
1217 elif [[ $in_opt_val == rate-policy ]] || compset -P '--rate-policy='; then
1218 __lttng_complete_trigger_rate_policy
1219 return
1220 elif [[ $in_opt_val == action ]] || compset -P '--action='; then
1221 __lttng_complete_trigger_action_type
1222 return
1223 fi
1224
1225 opt_completed=0
1226 }
1227
1228 # Adds completions for a `notify` trigger action.
1229 __lttng_complete_trigger_action_notify() {
1230 # Check all the next words in order up to (and excluding) the
1231 # current one.
1232 for word_index in {$word_index..$#words}; do
1233 if ((word_index == CURRENT)); then
1234 break
1235 fi
1236
1237 word=$words[word_index]
1238
1239 case $word in
1240 --rate-policy=*)
1241 __lttng_exclude_opts --rate-policy
1242 ;;
1243 --rate-policy)
1244 # Next: value of `--rate-policy` option
1245 in_opt_val=rate-policy
1246 ;;
1247 --action=*)
1248 # Defer to action handling dispatch. Action type is what
1249 # follows `--action=` here.
1250 ((word_index++))
1251 __lttng_complete_trigger_action ${word#--action=}
1252 return
1253 ;;
1254 --action)
1255 # Next: value of `--action` option
1256 in_opt_val=action
1257 ;;
1258 *)
1259 # Anything else: check for option value, or ignore otherwise
1260 case $in_opt_val in
1261 rate-policy)
1262 __lttng_exclude_opts --rate-policy
1263 ;;
1264 action)
1265 # Defer to action handling dispatch. Action type is
1266 # the whole current word.
1267 ((word_index++))
1268 __lttng_complete_trigger_action $word
1269 return
1270 ;;
1271 esac
1272
1273 # Next: not the value of any known option
1274 in_opt_val=
1275 ;;
1276 esac
1277 done
1278
1279 # Try to complete common action options
1280 __lttng_try_complete_action_opt
1281
1282 local -ir ret=$?
1283
1284 if ((opt_completed)); then
1285 return $ret
1286 fi
1287
1288 # Offer all possible options
1289 _describe -O option long_opts -qS=
1290 }
1291
1292 # Adds completions for a `start-session`, `stop-session`, or
1293 # `rotate-session` trigger action.
1294 __lttng_complete_trigger_action_session() {
1295 local -i has_session_name=0
1296
1297 # Check all the next words in order up to (and excluding) the
1298 # current one.
1299 for word_index in {$word_index..$#words}; do
1300 if ((word_index == CURRENT)); then
1301 break
1302 fi
1303
1304 word=$words[word_index]
1305
1306 case $word in
1307 --rate-policy=*)
1308 __lttng_exclude_opts --rate-policy
1309 ;;
1310 --rate-policy)
1311 # Next: value of `--rate-policy` option
1312 in_opt_val=rate-policy
1313 ;;
1314 --action=*)
1315 # Defer to action handling dispatch. Action type is what
1316 # follows `--action=` here.
1317 ((word_index++))
1318 __lttng_complete_trigger_action ${word#--action=}
1319 return
1320 ;;
1321 --action)
1322 # Next: value of `--action` option
1323 in_opt_val=action
1324 ;;
1325 *)
1326 # Anything else: check for option value, or ignore otherwise
1327 case $in_opt_val in
1328 rate-policy)
1329 __lttng_exclude_opts --rate-policy
1330 ;;
1331 action)
1332 # Defer to action handling dispatch. Action type is
1333 # the whole current word.
1334 ((word_index++))
1335 __lttng_complete_trigger_action $word
1336 return
1337 ;;
1338 *)
1339 # Positional argument: session name
1340 has_session_name=1
1341 ;;
1342 esac
1343
1344 # Next: not the value of any known option
1345 in_opt_val=
1346 ;;
1347 esac
1348 done
1349
1350 # Try to complete common action options
1351 __lttng_try_complete_action_opt
1352
1353 local -ir ret=$?
1354
1355 if ((opt_completed)); then
1356 return $ret
1357 fi
1358
1359 if [[ $PREFIX != -* ]] && ((! has_session_name)); then
1360 # Recording session name is the first positional argument
1361 __lttng_complete_session_name all all
1362 return
1363 fi
1364
1365 # Offer all possible options
1366 _describe -O option long_opts -qS=
1367 }
1368
1369 # Adds completions for a `snapshot-session` trigger action.
1370 __lttng_complete_trigger_action_snapshot_session() {
1371 local session_name
1372
1373 # Append specific options to `long_opts` and `short_opts`
1374 __lttng_add_opts --name -n 'set the snapshot output name'
1375 __lttng_add_opts --max-size -m 'set the maximum total size of all snapshot files'
1376 __lttng_add_opts --path 'set the local output destination directory'
1377 __lttng_add_opts --url 'set the output URL'
1378 __lttng_add_opts --ctrl-url 'set the control URL'
1379 __lttng_add_opts --data-url 'set the trace data output URL'
1380
1381 # Check all the next words in order up to (and excluding) the
1382 # current one.
1383 for word_index in {$word_index..$#words}; do
1384 if ((word_index == CURRENT)); then
1385 break
1386 fi
1387
1388 word=$words[word_index]
1389
1390 case $word in
1391 --name=*)
1392 __lttng_exclude_opts --name -n
1393 ;;
1394 --name|-n)
1395 # Next: value of `--name`/`-n` option
1396 in_opt_val=name
1397 ;;
1398 --max-size=*)
1399 __lttng_exclude_opts --max-size -m
1400 ;;
1401 --max-size|-m)
1402 # Next: value of `--max-size`/`-m` option
1403 in_opt_val=max-size
1404 ;;
1405 --path=*)
1406 __lttng_exclude_opts --path --url --ctrl-url --data-url
1407 ;;
1408 --path)
1409 # Next: value of `--path` option
1410 in_opt_val=path
1411 ;;
1412 --url=*)
1413 __lttng_exclude_opts --path --url --ctrl-url --data-url
1414 ;;
1415 --url)
1416 # Next: value of `--url` option
1417 in_opt_val=url
1418 ;;
1419 --ctrl-url=*)
1420 __lttng_exclude_opts --path --url --ctrl-url
1421 ;;
1422 --ctrl-url)
1423 # Next: value of `--ctrl-url` option
1424 in_opt_val=ctrl-url
1425 ;;
1426 --data-url=*)
1427 __lttng_exclude_opts --path --url --data-url
1428 ;;
1429 --data-url)
1430 # Next: value of `--data-url` option
1431 in_opt_val=data-url
1432 ;;
1433 --rate-policy=*)
1434 __lttng_exclude_opts --rate-policy
1435 ;;
1436 --rate-policy)
1437 # Next: value of `--rate-policy` option
1438 in_opt_val=rate-policy
1439 ;;
1440 --action=*)
1441 # Defer to action handling dispatch. Action type is what
1442 # follows `--action=` here.
1443 ((word_index++))
1444 __lttng_complete_trigger_action ${word#--action=}
1445 return
1446 ;;
1447 --action)
1448 # Next: value of `--action` option
1449 in_opt_val=action
1450 ;;
1451 *)
1452 # Anything else: check for option value, or ignore otherwise
1453 case $in_opt_val in
1454 name)
1455 __lttng_exclude_opts --name -n
1456 ;;
1457 max-size)
1458 __lttng_exclude_opts --max-size -m
1459 ;;
1460 path)
1461 __lttng_exclude_opts --path --url --ctrl-url --data-url
1462 ;;
1463 url)
1464 __lttng_exclude_opts --path --url --ctrl-url --data-url
1465 ;;
1466 ctrl-url)
1467 __lttng_exclude_opts --path --url --ctrl-url
1468 ;;
1469 data-url)
1470 __lttng_exclude_opts --path --url --data-url
1471 ;;
1472 rate-policy)
1473 __lttng_exclude_opts --rate-policy
1474 ;;
1475 action)
1476 # Defer to action handling dispatch. Action type is
1477 # the whole current word.
1478 ((word_index++))
1479 __lttng_complete_trigger_action $word
1480 return
1481 ;;
1482 *)
1483 # Set recording session name from positional argument
1484 session_name=$word
1485 esac
1486
1487 # Next: not the value of any known option
1488 in_opt_val=
1489 ;;
1490 esac
1491 done
1492
1493 # Try to complete common action options
1494 __lttng_try_complete_action_opt
1495
1496 local -ir ret=$?
1497
1498 if ((opt_completed)); then
1499 return $ret
1500 fi
1501
1502 # Try to offer option-specific completions
1503 if [[ $in_opt_val == name ]] || compset -P '--name='; then
1504 _message -e descriptions 'snapshot output name'
1505 return
1506 elif [[ $in_opt_val == max-size ]] || compset -P '--max-size='; then
1507 _message -e descriptions 'maximum size (bytes; `k`/`M`/`G` suffixes supported)'
1508 return
1509 elif [[ $in_opt_val == path ]] || compset -P '--path='; then
1510 _files -/
1511 return
1512 elif [[ $in_opt_val == url ]] || compset -P '--url='; then
1513 _message -e descriptions 'output URL'
1514 return
1515 elif [[ $in_opt_val == ctrl-url ]] || compset -P '--ctrl-url='; then
1516 _message -e descriptions 'control URL'
1517 return
1518 elif [[ $in_opt_val == data-url ]] || compset -P '--data-url='; then
1519 _message -e descriptions 'trace data output URL'
1520 return
1521 elif [[ $PREFIX != -* && -z $session_name ]]; then
1522 # Recording session name is the first positional argument
1523 __lttng_complete_session_name all snapshot
1524 return
1525 fi
1526
1527 # Offer all possible options
1528 _describe -O option long_opts -qS= -- short_opts
1529 }
1530
1531 # Usage:
1532 #
1533 # __lttng_complete_trigger_action ACTIONTYPE
1534 #
1535 # Adds completions for a trigger action of type ACTIONTYPE.
1536 __lttng_complete_trigger_action() {
1537 local -r action_type=$1
1538 local -i opt_completed
1539
1540 # Common initial state for all action completion functions
1541 in_opt_val=
1542 long_opts=()
1543 short_opts=()
1544 __lttng_add_opts --rate-policy 'set the rate policy'
1545 __lttng_add_opts --action 'set the type of the next trigger action'
1546
1547 case $action_type in
1548 notify)
1549 __lttng_complete_trigger_action_notify
1550 return
1551 ;;
1552 (start|stop|rotate)-session)
1553 __lttng_complete_trigger_action_session
1554 return
1555 ;;
1556 snapshot-session)
1557 __lttng_complete_trigger_action_snapshot_session
1558 return
1559 ;;
1560 *)
1561 _message "unknown action type \`$action_type\`"
1562 return
1563 ;;
1564 esac
1565 }
1566
1567 # Adds completions for a trigger action type.
1568 __lttng_complete_trigger_action_type() {
1569 local -r types=(
1570 'notify:send a notification through the notification mechanism'
1571 'start-session:start a recording session'
1572 'stop-session:stop a recording session'
1573 'rotate-session:archive the current trace chunk of a recording session'
1574 'snapshot-session:take a recording session snapshot'
1575 )
1576
1577 # Add completions
1578 _describe 'trigger action type' types
1579 }
1580
1581 # Adds completions for a new-style tracing domain type.
1582 __lttng_complete_new_domain_type() {
1583 local -r types=(
1584 'kernel:Linux kernel'
1585 'user:user space tracing'
1586 'jul:`java.util.logging`'
1587 'log4j:Apache log4j 1.x'
1588 'log4j2:Apache Log4j 2'
1589 'python:Python'
1590 )
1591
1592 # Add completions
1593 _describe 'tracing domain type' types
1594 }
1595
1596 # Usage:
1597 #
1598 # __lttng_set_opt_args_from_new_domain_type DOMAINTYPE
1599 #
1600 # Sets an entry of the `opt_args` associative array from the new-style
1601 # tracing domain type name DOMAINTYPE.
1602 __lttng_set_opt_args_from_new_domain_type() {
1603 local -r domain_type=$1
1604
1605 case $domain_type in
1606 kernel*|*probe*|*syscall*) opt_args[-k]=1;;
1607 user*) opt_args[-u]=1;;
1608 jul*) opt_args[-j]=1;;
1609 log4j*) opt_args[-l]=1;;
1610 log4j2*) opt_args[--log4j2]=1;;
1611 python*) opt_args[-p]=1;;
1612 esac
1613 }
1614
1615 # Usage:
1616 #
1617 # __lttng_complete_inst_point_type EXCLKERNEL EXCLPROBE ONLYPROBE
1618 #
1619 # Adds completions for a new-style instrumentation point type.
1620 #
1621 # EXCLKERNEL:
1622 # `1` to exclude Linux kernel types.
1623 #
1624 # EXCLPROBE:
1625 # `1` to exclude probe types.
1626 #
1627 # ONLYPROBE:
1628 # `1` to _only_ offer probe types.
1629 __lttng_complete_inst_point_type() {
1630 local -ir exclude_kernel_types=$1
1631 local -ir exclude_probe_types=$2
1632 local -ir only_probe_types=$3
1633 local -a types
1634 local -r probe_types=(
1635 {'kernel\:',}'kprobe:Linux kprobe'
1636 {'kernel\:',}'uprobe:Linux user space probe'
1637 )
1638
1639 if ((only_probe_types)); then
1640 # Only offer `kernel:kprobe` and `kernel:uprobe`
1641 types=($probe_types)
1642 else
1643 # Start with non-kernel types (always available)
1644 if ((! exclude_kernel_types)); then
1645 types+=(
1646 kernel{,'\:tracepoint'}':LTTng kernel tracepoint'
1647 {kernel,}'syscall\:entry:Linux system call entry'
1648 {kernel,}'syscall\:exit:Linux system call exit'
1649 {'kernel\:',}syscall{'\:entry+exit',}':Linux system call entry and exit'
1650 )
1651
1652 if ((! exclude_probe_types)); then
1653 types+=($probe_types)
1654 fi
1655 fi
1656
1657 types+=(
1658 user{,'\:tracepoint'}':LTTng user space tracepoint'
1659 jul{,'\:logging'}':`java.util.logging` logging statement'
1660 log4j{,'\:logging'}':Apache log4j 1.x logging statement'
1661 log4j2{,'\:logging'}':Apache Log4j 2 logging statement'
1662 python{,'\:logging'}':Python logging statement'
1663 )
1664 fi
1665
1666 # Add completions
1667 _describe 'instrumentation point type' types -o nosort
1668 }
1669
1670 # Adds completions for an `event-rule-matches` trigger condition.
1671 __lttng_complete_trigger_cond_event_rule_matches() {
1672 local ip_type
1673 local -i has_exclude_name_or_log_level_opt=0
1674 local -i has_filter_or_name_opt=0
1675 local -i has_location_or_event_name_opt=0
1676
1677 __lttng_add_opts --type -t 'set the instrumentation point type'
1678 __lttng_add_opts --name -n 'set the instrumentation point name pattern'
1679 __lttng_add_opts --exclude-name -x 'add an instrumentation point name exclusion pattern'
1680 __lttng_add_opts --log-level -l 'set the log level rule'
1681 __lttng_add_opts --filter -f 'set the filter expression'
1682 __lttng_add_opts --location -L 'set the probe location'
1683 __lttng_add_opts --event-name -E 'set the event name'
1684 __lttng_add_opts --capture 'add a capture descriptor'
1685
1686 # Check all the next words in order up to (and excluding) the
1687 # current one.
1688 for word_index in {$word_index..$#words}; do
1689 if ((word_index == CURRENT)); then
1690 break
1691 fi
1692
1693 word=$words[word_index]
1694
1695 case $word in
1696 --type=*)
1697 __lttng_exclude_opts --type -t
1698
1699 # Save the event rule type
1700 ip_type=${word#--type=}
1701 ;;
1702 --type|-t)
1703 # Next: value of `--type`/`-t` option
1704 in_opt_val=type
1705 ;;
1706 --name=*)
1707 has_filter_or_name_opt=1
1708 __lttng_exclude_opts --name -n
1709 ;;
1710 --name|-n)
1711 # Next: value of `--name`/`-n` option
1712 in_opt_val=name
1713 ;;
1714 --exclude-name=*)
1715 has_exclude_name_or_log_level_opt=1
1716 ;;
1717 --exclude-name|-x)
1718 # Next: value of `--exclude-name`/`-x` option
1719 in_opt_val=exclude-name
1720 ;;
1721 --log-level=*)
1722 has_exclude_name_or_log_level_opt=1
1723 __lttng_exclude_opts --log-level -l
1724 ;;
1725 --log-level|-l)
1726 # Next: value of `--log-level`/`-l` option
1727 in_opt_val=log-level
1728 ;;
1729 --filter=*)
1730 has_filter_or_name_opt=1
1731 __lttng_exclude_opts --filter -f
1732 ;;
1733 --filter|-f)
1734 # Next: value of `--filter`/`-f` option
1735 in_opt_val=filter
1736 ;;
1737 --location=*)
1738 has_location_or_event_name_opt=1
1739 __lttng_exclude_opts --location -L
1740 ;;
1741 --location|-L)
1742 # Next: value of `--location`/`-L` option
1743 in_opt_val=location
1744 ;;
1745 --event-name=*)
1746 has_location_or_event_name_opt=1
1747 __lttng_exclude_opts --event-name -E
1748 ;;
1749 --event-name|-E)
1750 # Next: value of `--event-name`/`-E` option
1751 in_opt_val=event-name
1752 ;;
1753 --capture)
1754 # Next: value of `--capture` option
1755 in_opt_val=capture
1756 ;;
1757 --action=*)
1758 # Defer to action handling dispatch. Action type is what
1759 # follows `--action=` here.
1760 ((word_index++))
1761 __lttng_complete_trigger_action ${word#--action=}
1762 return
1763 ;;
1764 --action)
1765 # Next: value of `--action` option
1766 in_opt_val=action
1767 ;;
1768 *)
1769 # Anything else: check for option value, or ignore otherwise
1770 case $in_opt_val in
1771 type)
1772 __lttng_exclude_opts --type -t
1773
1774 # Save the event rule type
1775 ip_type=$word
1776 ;;
1777 name)
1778 has_filter_or_name_opt=1
1779 __lttng_exclude_opts --name -n
1780 ;;
1781 exclude-name)
1782 has_exclude_name_or_log_level_opt=1;;
1783 log-level)
1784 has_exclude_name_or_log_level_opt=1
1785 __lttng_exclude_opts --log-level -l
1786 ;;
1787 filter)
1788 has_filter_or_name_opt=1
1789 __lttng_exclude_opts --filter -f
1790 ;;
1791 location)
1792 has_location_or_event_name_opt=1
1793 __lttng_exclude_opts --location -L
1794 ;;
1795 event-name)
1796 has_location_or_event_name_opt=1
1797 __lttng_exclude_opts --event-name -E
1798 ;;
1799 action)
1800 # Defer to action handling dispatch. Action type is
1801 # the whole current word.
1802 ((word_index++))
1803 __lttng_complete_trigger_action $word
1804 return
1805 ;;
1806 esac
1807
1808 # Next: not the value of any known option
1809 in_opt_val=0
1810 ;;
1811 esac
1812 done
1813
1814 # Exclude groups of options
1815 if ((has_filter_or_name_opt || has_exclude_name_or_log_level_opt)); then
1816 # Remove probe options from possible options
1817 __lttng_exclude_opts --event-name -E --location -L
1818 fi
1819
1820 if ((has_location_or_event_name_opt)); then
1821 # Remove non-probe options from possible options
1822 __lttng_exclude_opts --filter -f --log-level -l --name -n --exclude-name -x
1823 fi
1824
1825 # Filter options based on `--type`/`-t`
1826 case $ip_type in
1827 kernel(|:tracepoint|:syscall*))
1828 __lttng_exclude_opts --exclude-name -x --log-level -l --location -L --event-name -E
1829 ;;
1830 *probe*)
1831 __lttng_exclude_opts --name -n --exclude-name -x --log-level -l --filter -f
1832 ;;
1833 user*)
1834 __lttng_exclude_opts --location -L --event-name -E
1835 ;;
1836 *jul*|*log4j*|*python*|*logging*)
1837 __lttng_exclude_opts --exclude-name -x --location -L --event-name -E
1838 ;;
1839 esac
1840
1841 # Try to offer option-specific completions
1842 if [[ $in_opt_val == type ]] || compset -P '--type='; then
1843 __lttng_complete_inst_point_type \
1844 $has_exclude_name_or_log_level_opt \
1845 $has_filter_or_name_opt \
1846 $has_location_or_event_name_opt
1847 return
1848 elif [[ $in_opt_val == name ]] || compset -P '--name='; then
1849 _message -e descriptions 'instrumentation point name pattern'
1850 return
1851 elif [[ $in_opt_val == exclude-name ]] || compset -P '--exclude-name='; then
1852 _message -e descriptions 'instrumentation point name exclusion pattern'
1853 return
1854 elif [[ $in_opt_val == log-level ]] || compset -P '--log-level='; then
1855 # __lttng_complete_log_level() relies on `opt_args` for the
1856 # tracing domain.
1857 __lttng_set_opt_args_from_new_domain_type $ip_type
1858 __lttng_complete_log_level 'log level rule'
1859 return
1860 elif [[ $in_opt_val == filter ]] || compset -P '--filter='; then
1861 _message -e descriptions 'filter expression'
1862 return
1863 elif [[ $in_opt_val == location ]] || compset -P '--location='; then
1864 case $ip_type in
1865 *kprobe*)
1866 _message -e descriptions 'Linux kprobe location'
1867 return
1868 ;;
1869 *uprobe*)
1870 _message -e descriptions 'Linux user space probe location'
1871 return
1872 ;;
1873 *)
1874 _message -e descriptions 'probe location'
1875 return
1876 ;;
1877 esac
1878 elif [[ $in_opt_val == event-name ]] || compset -P '--event-name='; then
1879 _message -e descriptions 'event name'
1880 return
1881 elif [[ $in_opt_val == capture ]] || compset -P '--capture='; then
1882 _message -e descriptions 'capture descriptor'
1883 return
1884 elif [[ $in_opt_val == action ]] || compset -P '--action='; then
1885 __lttng_complete_trigger_action_type
1886 return
1887 else
1888 # Offer all possible options
1889 _describe -O option long_opts -qS= -- short_opts
1890 return
1891 fi
1892
1893 return 1
1894 }
1895
1896 # Usage:
1897 #
1898 # __lttng_complete_trigger_cond_simple WITHCHANDOMAIN WITHRATIO WITHSIZE
1899 #
1900 # Adds completions for a "simple" trigger condition (anything except
1901 # `event-rule-matches`).
1902 #
1903 # WITHCHANDOMAIN:
1904 # `1` to handle channel and tracing domain options.
1905 #
1906 # WITHRATIO:
1907 # `1` to handle a `--threshold-ratio` option.
1908 #
1909 # WITHSIZE:
1910 # `1` to handle `--threshold-size`/`-t` options.
1911 __lttng_complete_trigger_cond_simple() {
1912 local -ir with_channel_domain_opts=$1
1913 local -ir with_threshold_ratio_opt=$2
1914 local -ir with_threshold_size_opts=$3
1915 local session_name
1916 local domain_type
1917
1918 __lttng_add_opts --session -s 'set the target recording session name'
1919
1920 if ((with_channel_domain_opts)); then
1921 __lttng_add_opts --channel -c 'set the target channel name'
1922 __lttng_add_opts --domain -d 'set the target tracing domain'
1923 fi
1924
1925 if ((with_threshold_ratio_opt)); then
1926 __lttng_add_opts --threshold-ratio 'set the threshold as a ratio of the total buffer size'
1927 fi
1928
1929 if ((with_threshold_size_opts)); then
1930 __lttng_add_opts --threshold-size -t 'set the threshold size'
1931 fi
1932
1933 # Check all the next words in order up to (and excluding) the
1934 # current one.
1935 for word_index in {$word_index..$#words}; do
1936 if ((word_index == CURRENT)); then
1937 break
1938 fi
1939
1940 word=$words[word_index]
1941
1942 case $word in
1943 --session=*)
1944 __lttng_exclude_opts --session -s
1945
1946 # Save the recording session name
1947 session_name=${word#--session=}
1948 ;;
1949 --session|-s)
1950 # Next: value of `--session`/`-s` option
1951 in_opt_val=session;;
1952 --channel=*)
1953 __lttng_exclude_opts --channel -c
1954 ;;
1955 --channel|-c)
1956 # Next: value of `--channel`/`-c` option
1957 in_opt_val=channel;;
1958 --domain=*)
1959 __lttng_exclude_opts --domain -d
1960
1961 # Save the tracing domain type name
1962 domain_type=${word#--domain=}
1963 ;;
1964 --domain|-d)
1965 # Next: value of `--domain`/`-d` option
1966 in_opt_val=domain;;
1967 --threshold-(size|ratio)=*)
1968 __lttng_exclude_opts --threshold-size --threshold-ratio -t
1969 ;;
1970 --threshold-size|-t)
1971 # Next: value of `--threshold-size`/`-t` option
1972 in_opt_val=threshold-size;;
1973 --threshold-ratio)
1974 # Next: value of `--threshold-ratio` option
1975 in_opt_val=threshold-ratio;;
1976 --action=*)
1977 # Defer to action handling dispatch. Action type is what
1978 # follows `--action=` here.
1979 ((word_index++))
1980 __lttng_complete_trigger_action ${word#--action=}
1981 return
1982 ;;
1983 --action)
1984 # Next: value of `--action` option
1985 in_opt_val=action;;
1986 *)
1987 # Anything else: check for option value, or ignore otherwise
1988 case $in_opt_val in
1989 session)
1990 __lttng_exclude_opts --session -s
1991
1992 # Save the recording session name
1993 session_name=$word
1994 ;;
1995 channel)
1996 __lttng_exclude_opts --channel -c
1997 ;;
1998 domain)
1999 __lttng_exclude_opts --domain -d
2000
2001 # Save the tracing domain type
2002 domain_type=$word
2003 ;;
2004 threshold-*)
2005 __lttng_exclude_opts --threshold-size --threshold-ratio -t
2006 ;;
2007 action)
2008 # Defer to action handling dispatch. Action type is
2009 # the whole current word.
2010 ((word_index++))
2011 __lttng_complete_trigger_action $word
2012 return
2013 ;;
2014 esac
2015
2016 # Next: not the value of any known option
2017 in_opt_val=
2018 ;;
2019 esac
2020 done
2021
2022 # Try to offer option-specific completions
2023 if [[ $in_opt_val == session ]] || compset -P '--session='; then
2024 __lttng_complete_session_name all all
2025 return
2026 elif [[ $in_opt_val == channel ]] || compset -P '--channel='; then
2027 if ((with_channel_domain_opts)); then
2028 # __lttng_complete_channel_name() relies on `line[1]` for the
2029 # recording session name and on `opt_args` for the
2030 # tracing domain.
2031 line=($session_name)
2032 opt_args=()
2033 __lttng_set_opt_args_from_new_domain_type $domain_type
2034 __lttng_complete_channel_name all arg
2035 return
2036 fi
2037 elif [[ $in_opt_val == domain ]] || compset -P '--domain='; then
2038 if ((with_channel_domain_opts)); then
2039 __lttng_complete_new_domain_type
2040 return
2041 fi
2042 elif [[ $in_opt_val == threshold-size ]] || compset -P '--threshold-size='; then
2043 if ((with_threshold_size_opts)); then
2044 _message -e descriptions 'threshold size (bytes; `k`/`M`/`G` suffixes supported)'
2045 return
2046 fi
2047 elif [[ $in_opt_val == threshold-ratio ]] || compset -P '--threshold-ratio='; then
2048 if ((with_threshold_ratio_opt)); then
2049 _message -e descriptions 'threshold ratio (0 to 1)'
2050 return
2051 fi
2052 elif [[ $in_opt_val == action ]] || compset -P '--action='; then
2053 __lttng_complete_trigger_action_type
2054 return
2055 else
2056 # Offer all possible options
2057 _describe -O option long_opts -qS= -- short_opts
2058 return
2059 fi
2060
2061 return 1
2062 }
2063
2064 # Usage:
2065 #
2066 # __lttng_complete_trigger_cond CONDTYPE
2067 #
2068 # Adds completions for a trigger condition of type CONDTYPE.
2069 __lttng_complete_trigger_cond() {
2070 local -r cond_type=$1
2071
2072 # Common initial state for all condition completion functions
2073 long_opts=()
2074 short_opts=()
2075 in_opt_val=
2076 __lttng_add_opts --action 'set the type of the next trigger action'
2077
2078 case $cond_type in
2079 event-rule-matches)
2080 __lttng_complete_trigger_cond_event_rule_matches
2081 return
2082 ;;
2083 channel-buffer-usage-[lg]e)
2084 __lttng_complete_trigger_cond_simple 1 1 1
2085 return
2086 ;;
2087 session-consumed-size-ge)
2088 __lttng_complete_trigger_cond_simple 0 0 1
2089 return
2090 ;;
2091 session-rotation-(starts|finishes))
2092 __lttng_complete_trigger_cond_simple 0 0 0
2093 return
2094 ;;
2095 *)
2096 _message "unknown condition type \`$cond_type\`"
2097 return
2098 ;;
2099 esac
2100 }
2101
2102 # Adds completions for a trigger condition type.
2103 __lttng_complete_trigger_cond_type() {
2104 local types=(
2105 'event-rule-matches:an event rule matches an event'
2106 )
2107
2108 if ((minor_version >= 15)); then
2109 types+=(
2110 'channel-buffer-usage-ge:channel buffer size becomes ≥ some threshold'
2111 'channel-buffer-usage-le:channel buffer size becomes ≤ some threshold'
2112 'session-consumed-size-ge:recording session consumed size becomes ≥ some threshold'
2113 'session-rotation-finishes:recording session rotation operation finishes'
2114 'session-rotation-starts:recording session rotation operation starts'
2115 )
2116 fi
2117
2118 # Add completions
2119 _describe 'trigger condition type' types
2120 }
2121
2122 # Adds completions for the arguments of the `add-trigger` command.
2123 __lttng_complete_add_trigger_cmd() {
2124 # Shared parameters used by called functions
2125 local in_opt_val
2126 local -a long_opts short_opts
2127 local -i word_index
2128 local word
2129
2130 __lttng_add_opts --name 'set the trigger name'
2131 __lttng_add_opts --owner-uid 'add the trigger to another Unix user'
2132 __lttng_add_opts --condition 'set the type of the trigger condition'
2133
2134 # Check all words in order up to (and excluding) the current one
2135 for word_index in {1..$#words}; do
2136 if ((word_index == CURRENT)); then
2137 break
2138 fi
2139
2140 word=$words[word_index]
2141
2142 case $word in
2143 -h|--help)
2144 # Exit now: nothing to complete if help is wanted
2145 return 1;;
2146 --name=*)
2147 __lttng_exclude_opts --name;;
2148 --name)
2149 # Next: value of `--name` option
2150 in_opt_val=name;;
2151 --owner-uid=*)
2152 __lttng_exclude_opts --owner-uid;;
2153 --owner-uid)
2154 # Next: value of `--owner-uid` option
2155 in_opt_val=owner-uid;;
2156 --condition=*)
2157 # Defer to condition handling dispatch. Condition type is what
2158 # follows `--condition=` here.
2159 ((word_index++))
2160 __lttng_complete_trigger_cond ${word#--condition=}
2161 return
2162 ;;
2163 --condition)
2164 # Next: value of `--condition` option
2165 in_opt_val=condition;;
2166 *)
2167 # Anything else: check for option value, or ignore otherwise
2168 case $in_opt_val in
2169 name)
2170 __lttng_exclude_opts --name;;
2171 owner-uid)
2172 __lttng_exclude_opts --owner-uid;;
2173 condition)
2174 # Defer to condition handling dispatch. Condition type is
2175 # the whole current word.
2176 ((word_index++))
2177 __lttng_complete_trigger_cond $word
2178 return
2179 ;;
2180 esac
2181
2182 # Next: not the value of any known option
2183 in_opt_val=
2184 ;;
2185 esac
2186 done
2187
2188 # Try to offer option-specific completions
2189 if [[ $in_opt_val == name ]] || compset -P '--name='; then
2190 _message -e descriptions 'trigger name'
2191 return
2192 elif [[ $in_opt_val == owner-uid ]] || compset -P '--owner-uid='; then
2193 __lttng_complete_uid
2194 return
2195 elif [[ $in_opt_val == condition ]] || compset -P '--condition='; then
2196 __lttng_complete_trigger_cond_type
2197 return
2198 else
2199 # Offer all possible options
2200 local -r help_opts=({-h,--help}':show help')
2201
2202 _describe -O option long_opts -qS= -- help_opts
2203 return
2204 fi
2205 }
2206
2207 # Adds completions for the arguments of the `clear` command.
2208 __lttng_complete_clear_cmd() {
2209 specs=(
2210 $help_opt_specs
2211 '(-a --all):: : __lttng_complete_session_name all all'
2212 '(1 -a --all)'{-a,--all}'[clear all recording sessions]'
2213 )
2214
2215 # Add completions
2216 _arguments -s -W : $specs
2217 }
2218
2219 # Adds completions for the arguments of the `create` command.
2220 __lttng_complete_create_cmd() {
2221 specs=(
2222 $help_opt_specs
2223 ':: :_guard "^-*" "new recording session name"'
2224 '(-C --ctrl-url output)'{-C+,--ctrl-url=}'[set the control URL]:control URL:'
2225 '(-D --data-url output)'{-D+,--data-url=}'[set the trace data output URL]:trace data output URL:'
2226 )
2227
2228 # The `--shm-path` option requires LTTng-tools ≥ 2.7
2229 if ((minor_version >= 7)); then
2230 specs+=(
2231 '--shm-path=[write shared memory files to a specific directory]:shared memory file directory:_files -/'
2232 )
2233 fi
2234
2235 # The `--trace-format` option requires LTTng-tools ≥ 2.15
2236 if ((minor_version >= 15)); then
2237 specs+=(
2238 '(-F --trace-format)'{-F+,--trace-format=}'[set the trace format]:trace format:(ctf-1.8 ctf-2 default)'
2239 )
2240 fi
2241
2242 # Add the remaining option groups
2243 specs+=(
2244 + '(output)'
2245 '(-D --data-url -C --ctrl-url --live)--no-output[disable trace data output]'
2246 '(-D --data-url -C --ctrl-url --live)'{-o+,--output=}'[set the local trace data output directory]:trace data output directory:_files -/'
2247 '(-D --data-url -C --ctrl-url)'{-U+,--set-url=}'[set the trace data output and control URL]:trace data output and control URL:'
2248 + '(mode)'
2249 '(-o --output --no-output)--live=[create an LTTng live recording session]:live timer period (µs):'
2250 '--snapshot[create a snapshot recording session]'
2251 )
2252
2253 # Add completions
2254 _arguments -s -W : $specs
2255 }
2256
2257 # Adds completions for the arguments of the `destroy` command.
2258 __lttng_complete_destroy_cmd() {
2259 specs=(
2260 $help_opt_specs
2261 )
2262
2263 # The `--no-wait` option requires LTTng-tools ≥ 2.8
2264 if ((minor_version >= 8)); then
2265 specs+=(
2266 '(-n --no-wait)'{-n,--no-wait}'[exit immediately]'
2267 )
2268 fi
2269
2270 specs+=(
2271 + '(session)'
2272 ':: : __lttng_complete_session_name all all'
2273 {-a,--all}'[destroy all recording sessions]'
2274 )
2275
2276 # The `--glob` option requires LTTng-tools ≥ 2.14
2277 if ((minor_version >= 14)); then
2278 specs+=(
2279 {-g+,--glob=}'[destroy recording sessions matched by a pattern]:globbing pattern:'
2280 )
2281 fi
2282
2283 # Add completions
2284 _arguments -s -W : $specs
2285 }
2286
2287 # Adds completions for the arguments of the `disable-channel` command.
2288 __lttng_complete_disable_channel_cmd() {
2289 specs=(
2290 $help_opt_specs
2291 '(-s --session)'{-s+,--session=}'[select a specific recording session]: : __lttng_complete_session_name all all'
2292 ': : _sequence __lttng_complete_channel_name enabled opt'
2293 + '(domain)'
2294 {-k,--kernel}'[select the Linux kernel tracing domain]'
2295 {-u,--userspace}'[select the user space tracing domain]'
2296 )
2297
2298 # Add completions
2299 _arguments -s -W : $specs
2300 }
2301
2302 # Sets `REPLY` to the XML tracing domain name value depending on the
2303 # tracing domain option (in `opt_args`), or prints nothing and
2304 # returns 1 when not available.
2305 __lttng_xml_domain_name_from_opt() {
2306 if __lttng_kernel_domain_opt_is_set; then
2307 REPLY=KERNEL
2308 elif __lttng_user_domain_opt_is_set; then
2309 REPLY=UST
2310 elif __lttng_jul_domain_opt_is_set; then
2311 REPLY=JUL
2312 elif __lttng_log4j1_domain_opt_is_set; then
2313 REPLY=LOG4J
2314 elif __lttng_log4j2_domain_opt_is_set; then
2315 REPLY=LOG4J2
2316 elif __lttng_python_domain_opt_is_set; then
2317 REPLY=PYTHON
2318 else
2319 return 1
2320 fi
2321 }
2322
2323 # Sets `REPLY` to the XML instrumentation point type value depending on
2324 # the instrumentation point option (in `opt_args`).
2325 __lttng_xml_ip_type_from_opt() {
2326 if (($+opt_args[--syscall] || $+opt_args[ip---syscall])); then
2327 REPLY=SYSCALL
2328 elif (($+opt_args[--probe] || $+opt_args[ip---probe])); then
2329 REPLY=PROBE
2330 elif (($+opt_args[--function] || $+opt_args[ip---function])); then
2331 REPLY=FUNCTION
2332 else
2333 REPLY=TRACEPOINT
2334 fi
2335 }
2336
2337 # Adds completions for an old, enabled recording event rule name
2338 # condition (for the `disable-event` command).
2339 __lttng_complete_old_enabled_er_name_cond() {
2340 local -r msg='recording event rule name condition'
2341
2342 # Find the recording session name to use
2343 local session_name
2344
2345 if (($+opt_args[-s])); then
2346 session_name=$opt_args[-s]
2347 elif (($+opt_args[--session])); then
2348 session_name=$opt_args[--session]
2349 else
2350 # Fall back to current recording session
2351 if ! __lttng_cur_session_name; then
2352 _guard '^-*' $msg
2353 fi
2354
2355 session_name=$REPLY
2356 fi
2357
2358 # Find the channel name to use (`channel0` is the default name)
2359 local channel_name=channel0
2360
2361 if (($+opt_args[-c])); then
2362 channel_name=$opt_args[-c]
2363 elif (($+opt_args[--channel])); then
2364 channel_name=$opt_args[--channel]
2365 fi
2366
2367 # Create tracing domain XPath part
2368 local domain_xpath
2369
2370 if __lttng_xml_domain_name_from_opt; then
2371 domain_xpath="*[local-name() = 'domain'][*[local-name() = 'type'] = '$REPLY']//"
2372 fi
2373
2374 # Create channel XPath part
2375 local -r channel_xpath="*[local-name() = 'channel'][*[local-name() = 'name'] = '$channel_name']//"
2376
2377 # Create instrumentation point type XPath part
2378 __lttng_xml_ip_type_from_opt
2379
2380 local -r ip_type_xpath="[*[local-name() = 'type'] = '$REPLY']"
2381
2382 # Get XML document (detailed recording session)
2383 if ! __lttng_session_xml $msg $session_name; then
2384 return 0
2385 fi
2386
2387 local session_xml=$REPLY
2388
2389 # Convert to one recording event rule name per line
2390 local lines
2391
2392 if ! lines=$(__lttng_xmllint_xpath $session_xml "//$domain_xpath${channel_xpath}*[local-name() = 'event']${ip_type_xpath}[*[local-name() = 'enabled'] = 'true']/*[local-name() = 'name']/text()"); then
2393 _guard '^-*' $msg
2394 return
2395 fi
2396
2397 local line
2398 local -a er_names
2399
2400 while read -r line; do
2401 if [[ -z ${line// } ]]; then
2402 # Skip empty line
2403 continue
2404 fi
2405
2406 er_names+=$line
2407 done <<< $lines
2408
2409 # Add completions
2410 expl=
2411 _wanted -C event events expl $msg compadd "$@" -a er_names
2412 }
2413
2414 # Adds completions for the arguments of the `disable-event` command.
2415 __lttng_complete_disable_event_cmd() {
2416 local -r kernel_ip_opt_excl=(--syscall --probe --function)
2417
2418 specs=(
2419 $help_opt_specs
2420 '(-s --session)'{-s+,--session=}'[select a specific recording session]: : __lttng_complete_session_name all all'
2421 '(-c --channel)'{-c+,--channel=}'[select a specific channel]: : __lttng_complete_channel_name all opt'
2422 + '(names)'
2423 {-a,--all-events}'[disable all recording event rules]'
2424 ': :_sequence __lttng_complete_old_enabled_er_name_cond'
2425 + '(domain)'
2426 {-k,--kernel}'[select the Linux kernel tracing domain]'
2427 "($kernel_ip_opt_excl)"{-u,--userspace}'[select the user space tracing domain]'
2428 "($kernel_ip_opt_excl)"{-j,--jul}'[select the `java.util.logging` tracing domain]'
2429 )
2430
2431 # Add tracing domain option specifications based on the minor version
2432 # of LTTng-tools.
2433 if ((minor_version >= 6)); then
2434 specs+=(
2435 "($kernel_ip_opt_excl)"{-l,--log4j}'[select the Apache log4j 1.x tracing domain]'
2436 )
2437 fi
2438
2439 if ((minor_version >= 14)); then
2440 specs+=(
2441 "($kernel_ip_opt_excl)--log4j2[select the Apache Log4j 2 tracing domain]"
2442 )
2443 fi
2444
2445 if ((minor_version >= 7)); then
2446 specs+=(
2447 "($kernel_ip_opt_excl)"{-p,--python}'[select the Python tracing domain]'
2448 )
2449 fi
2450
2451 # Add instrumentation point type option specifications based on the
2452 # minor version of LTTng-tools.
2453 if ((minor_version >= 6)); then
2454 specs+=(
2455 + '(ip)'
2456 "($non_kernel_domain_opt_excl)--syscall[disable recording ER matching Linux system call events]"
2457 )
2458 fi
2459
2460 if ((minor_version >= 7)); then
2461 specs+=(
2462 '--tracepoint[disable recording ER matching tracepoint events]'
2463 "($non_kernel_domain_opt_excl)--probe[disable recording ER matching kprobe events]"
2464 "($non_kernel_domain_opt_excl)--function[disable recording ER matching kretprobe events]"
2465 )
2466 fi
2467
2468 # Add completions
2469 _arguments -s -W : $specs
2470 }
2471
2472 # Adds completions for the arguments of the `disable-rotation` command.
2473 __lttng_complete_disable_rotation_cmd() {
2474 specs=(
2475 $help_opt_specs
2476 '(-s --session)'{-s+,--session=}'[select a specific recording session]: : __lttng_complete_session_name all all'
2477 '--timer[disable the periodic rotation schedule]'
2478 '--size[disable the size-based rotation schedule]'
2479 )
2480
2481 _arguments -s -W : $specs
2482 }
2483
2484 # Adds completions for a channel output type (`mmap` and, possibly,
2485 # `splice`) (for the `--output` option of the `enable-channel` command).
2486 #
2487 # This function replaces the argument field of the current context with
2488 # `output-opt`.
2489 #
2490 # This function relies on the user space tracing domain options of
2491 # `opt_args` to restrict the offered completions. Without the user
2492 # space tracing domain option, this function adds the `splice`
2493 # completion too.
2494 __lttng_complete_channel_output_type() {
2495 local output_types=(mmap)
2496
2497 if ! __lttng_user_domain_opt_is_set; then
2498 # Linux kernel tracing domain or none
2499 output_types+=(splice)
2500 fi
2501
2502 expl=
2503 _wanted -C 'output-opt' values expl 'output type' compadd -a output_types
2504 }
2505
2506 # Adds completions for the non-option argument of the `enable-channel`
2507 # command.
2508 #
2509 # This function either, depending on the keys of `opt_args`:
2510 #
2511 # At least one creation option:
2512 # Shows a message to enter the new channel name.
2513 #
2514 # Otherwise:
2515 # Adds completions for a comma-separated list of known channel names
2516 # using __lttng_complete_channel_name().
2517 __lttng_complete_enable_channel_cmd_names() {
2518 local key
2519 local -r enable_opts=(
2520 -s --session
2521 domain---kernel domain--k
2522 domain---userspace domain--u
2523 )
2524
2525 # For each key of `opt_args`
2526 for key in "${(@k)opt_args}"; do
2527 if (($enable_opts[(Ie)$key])); then
2528 # Enabling option exists: skip
2529 continue
2530 fi
2531
2532 # Creation option exists: single name
2533 _guard '^-*' 'new channel name'
2534 return
2535 done
2536
2537 # Comma-separated list of existing channel names
2538 _sequence __lttng_complete_channel_name disabled opt
2539 }
2540
2541 # Adds completions for a buffer ownership.
2542 __lttng_complete_buffer_ownership() {
2543 local -r kernel_choices=(
2544 'system:use global ring buffers'
2545 )
2546 local -r user_choices=(
2547 'user:use per-user ring buffers'
2548 'process:use per-process ring buffers'
2549 )
2550 local -a choices
2551
2552 if __lttng_kernel_domain_opt_is_set; then
2553 choices=($kernel_choices)
2554 elif __lttng_user_domain_opt_is_set; then
2555 choices=($user_choices)
2556 else
2557 choices=($kernel_choices $user_choices)
2558 fi
2559
2560 # Add completions
2561 _describe 'buffer ownership' choices
2562 }
2563
2564 # Adds completions for a buffer allocation policy.
2565 __lttng_complete_buffer_alloc() {
2566 local choices=(
2567 'per-cpu:allocate sub-buffers for each CPU'
2568 )
2569
2570 if __lttng_user_domain_opt_is_set || ! __lttng_kernel_domain_opt_is_set; then
2571 choices+=('per-channel:allocate sub-buffers for each channel')
2572 fi
2573
2574 # Add completions
2575 _describe 'buffer allocation policy' choices
2576 }
2577
2578 # Adds completions for an automatic memory reclaim strategy.
2579 __lttng_complete_auto_reclaim_memory() {
2580 if compset -P older-than:; then
2581 _message -e descriptions 'age (µs; `ms`/`s`/`m`/`h` suffixes supported)'
2582 return
2583 fi
2584
2585 local -r strategies=(
2586 'off:no automatic memory reclamation'
2587 'consumed:reclaim memory as soon as sub-buffers are consumed'
2588 )
2589 local -r strategies_with_arg=(
2590 'older-than:reclaim memory of sub-buffers older than some age'
2591 )
2592
2593 # Add completions
2594 _describe 'automatic memory reclaim strategy' strategies -- strategies_with_arg -qS:
2595 }
2596
2597 # Adds completions for the arguments of the `enable-channel` command.
2598 __lttng_complete_enable_channel_cmd() {
2599 specs=(
2600 $help_opt_specs
2601 '--switch-timer=[set the switch timer period]:switch timer period (µs):'
2602 '--read-timer=[set the read timer period]:read timer period (µs):'
2603 '--subbuf-size=[set the size of each sub-buffer]:sub-buffer size (bytes; `k`/`M`/`G` suffixes supported):'
2604 '--num-subbuf=[set the number of sub-buffers per ring buffer]:sub-buffer count:'
2605 '--tracefile-size=[set the maximum size of each trace file]:trace file size (bytes; `k`/`M`/`G` suffixes supported):'
2606 '--tracefile-count=[set the maximum number of trace files]:maximum trace file count:'
2607 '--output=[set the output type]: : __lttng_complete_channel_output_type'
2608 '(-s --session)'{-s+,--session=}'[select a specific recording session]: : __lttng_complete_session_name all all'
2609 ': : __lttng_complete_enable_channel_cmd_names'
2610 )
2611
2612 # The `--blocking-timeout` and `--monitor-timer` options require
2613 # LTTng-tools ≥ 2.10.
2614 if ((minor_version >= 10)); then
2615 specs+=(
2616 '(--kernel --overwrite --buffers-global)--blocking-timeout=[set the blocking timeout]:blocking timeout (µs):'
2617 '--monitor-timer=[set the monitor timer period]:monitor timer period (µs):'
2618 )
2619 fi
2620
2621 # The `buffer-allocation` option require LTTng-tools ≥ 2.14
2622 if ((minor_version >= 14)); then
2623 specs+=(
2624 '--buffer-allocation=[set the allocation policy of ring buffers]: : __lttng_complete_buffer_alloc'
2625 )
2626 fi
2627
2628 # Add the remaining option groups
2629 specs+=(
2630 + '(domain)'
2631 '(--blocking-timeout --buffers-uid --buffers-pid --auto-reclaim-memory --buffer-preallocation --watchdog-timer)'{-k,--kernel}'[select the Linux kernel tracing domain]'
2632 '(--buffers-global)'{-u,--userspace}'[select the user space tracing domain]'
2633 + '(loss-mode)'
2634 '--discard[discard event records with no available sub-buffer]'
2635 '(--blocking-timeout --auto-reclaim-memory)--overwrite[overwrite oldest sub-buffer with no available sub-buffer]'
2636 + '(buffer-ownership)'
2637 '(-k --kernel)--buffers-uid[use per-user ring buffers]'
2638 '(-k --kernel)--buffers-pid[use per-process ring buffers]'
2639 '(-u --userspace --blocking-timeout)--buffers-global[use global ring buffers]'
2640 )
2641
2642 # The `--buffer-ownership` option requires LTTng-tools ≥ 2.14
2643 if ((minor_version >= 14)); then
2644 specs+=(
2645 '--buffer-ownership=[set the ownership of ring buffers]: : __lttng_complete_buffer_ownership'
2646 )
2647 fi
2648
2649 # The `--auto-reclaim-memory`, `--buffer-preallocation`,
2650 # and `--watchdog-timer` options require LTTng-tools ≥ 2.15
2651 if ((minor_version >= 15)); then
2652 specs+=(
2653 '(-k --kernel --overwrite)--auto-reclaim-memory=[set automatic memory reclamation strategy]: : __lttng_complete_auto_reclaim_memory'
2654 '(-k --kernel)--buffer-preallocation=[set the sub-buffer preallocation policy]:policy:(on-demand preallocate)'
2655 '(-k --kernel)--watchdog-timer=[set the watchdog timer period]:watchdog timer period (µs):'
2656 )
2657 fi
2658
2659 # Add completions
2660 _arguments -s -W : $specs
2661 }
2662
2663 # Adds completions for the arguments of the `enable-event` command.
2664 __lttng_complete_enable_event_cmd() {
2665 local -r kernel_opts_excl=(
2666 -k --kernel
2667 --syscall
2668 --probe
2669 --userspace-probe
2670 --function
2671 )
2672 local -r non_kernel_opts_excl=(
2673 $non_kernel_domain_opt_excl
2674 -x --exclude
2675 log-level
2676 )
2677 local -r exclude_opt_excl=(
2678 $kernel_opts_excl
2679 -j --jul
2680 -l --log4j
2681 --log4j2
2682 -p --python
2683 -x --exclude
2684 )
2685 local ip_specs=(
2686 '--tracepoint[only match LTTng tracepoint events]'
2687 "($non_kernel_opts_excl)--syscall[only match Linux system call events]"
2688 "($non_kernel_opts_excl)--probe=[only match kprobe events]:kprobe location:"
2689 "($non_kernel_opts_excl)--function=[only match kretprobe events]:kretprobe location:"
2690 )
2691 local domain_specs=(
2692 "($non_kernel_opts_excl)"{-k,--kernel}'[select the Linux kernel tracing domain]'
2693 "($kernel_opts_excl)"{-u,--userspace}'[select the user space tracing domain]'
2694 "($kernel_opts_excl -x --exclude)"{-j,--jul}'[select the `java.util.logging` tracing domain]'
2695 )
2696
2697 # The Apache log4j 1.x tracing domain options require LTTng-tools ≥ 2.6
2698 if ((minor_version >= 6)); then
2699 domain_specs+=(
2700 "($kernel_opts_excl -x --exclude)"{-l,--log4j}'[select the Apache log4j 1.x tracing domain]'
2701 )
2702 fi
2703
2704 # The Python tracing domain options require LTTng-tools ≥ 2.7
2705 if ((minor_version >= 7)); then
2706 domain_specs+=(
2707 "($kernel_opts_excl -x --exclude)"{-p,--python}'[select the Python tracing domain]'
2708 )
2709 fi
2710
2711 # The Linux user space probe instrumentation options require LTTng-tools ≥ 2.11
2712 if ((minor_version >= 11)); then
2713 ip_specs+=(
2714 "($non_kernel_opts_excl)--userspace-probe=[only match Linux user space probe events]:user space probe location:"
2715 )
2716 fi
2717
2718 # The Apache Log4j 2 tracing domain options require LTTng-tools ≥ 2.14
2719 if ((minor_version >= 14)); then
2720 domain_specs+=(
2721 "($kernel_opts_excl -x --exclude)--log4j2[select the Apache Log4j 2 tracing domain]"
2722 )
2723 fi
2724
2725 # Add completions.
2726 #
2727 # There's no way, based on the command-line arguments, to distinguish
2728 # between creating a new recording event rule and enabling an
2729 # existing, disabled recording event rule here.
2730 #
2731 # For example, given this:
2732 #
2733 # $ lttng enable-event --kernel --syscall <TAB>
2734 #
2735 # At this point, the user might want the list of existing, disabled
2736 # kernel system call recording event rule names (current recording
2737 # session, default channel name), or the full list of available system
2738 # call instrumentation point names.
2739 #
2740 # This function makes the arbitrary choice to provide the available
2741 # instrumentation point names (__lttng_complete_ip_name()) because,
2742 # interactively, it seems to be more common/useful than disabling
2743 # existing recording event rules.
2744 specs=(
2745 $help_opt_specs
2746 '(--probe --userspace-probe --function -f --filter)'{-f+,--filter=}'[only match events which satisfy an expression]:filter expression:'
2747 "($exclude_opt_excl)"{-x+,--exclude=}'[exclude event name patterns]:comma-separated list of patterns:'
2748 '(-s --session)'{-s+,--session=}'[select a specific recording session]: : __lttng_complete_session_name all all'
2749 '(-c --channel)'{-c+,--channel=}'[select a specific channel]: : __lttng_complete_channel_name all opt'
2750 + '(log-level)'
2751 "($kernel_opts_excl)--loglevel=[only match events with specific log levels]: : __lttng_complete_log_level_name"
2752 "($kernel_opts_excl)--loglevel-only=[only match events with an exact log level]: : __lttng_complete_log_level_name"
2753 + '(names)'
2754 {-a,--all}'[match events regardless of their name]'
2755 ': :_sequence __lttng_complete_ip_name'
2756 + '(ip)' $ip_specs
2757 + '(domain)' $domain_specs
2758 )
2759
2760 _arguments -s -W : $specs
2761 }
2762
2763 # Adds completions for the arguments of the `enable-rotation` command.
2764 __lttng_complete_enable_rotation_cmd() {
2765 specs=(
2766 $help_opt_specs
2767 '(-s --session)'{-s+,--session=}'[select a specific recording session]: : __lttng_complete_session_name all all'
2768 '--timer=[rotate periodically]:period (µs; `ms`/`s`/`m`/`h` suffixes supported):'
2769 '--size=[rotate based on flushed size of current trace chunk]:size (bytes; `k`/`M`/`G` suffixes supported):'
2770 )
2771
2772 # Add completions
2773 _arguments -s -W : $specs
2774 }
2775
2776 # Adds completions for the arguments of the `help` command.
2777 __lttng_complete_help_cmd() {
2778 specs=(
2779 $help_opt_specs
2780 ': : __lttng_complete_cmd_name'
2781 )
2782
2783 # Add completions
2784 _arguments -s -W : $specs
2785 }
2786
2787 # Adds completions for the arguments of the `list` command.
2788 __lttng_complete_list_cmd() {
2789 local -r domain_opt_excl=(
2790 -d --domain
2791 -f --fields
2792 -c --channel
2793 -k --kernel
2794 -u --userspace
2795 -j --jul
2796 -l --log4j
2797 --log4j2
2798 -p --python
2799 --syscall
2800 )
2801
2802 specs=(
2803 $help_opt_specs
2804 '(-d --domain -f --fields -c --channel --mem-usage 1)'{-f,--fields}'[show user space tracepoint fields]'
2805 "($domain_opt_excl)"{-d,--domain}'[show tracing domains]'
2806 '(-d --domain -f --fields --syscall -c --channel)'{-c+,--channel=}'[list the objects of specific channel(s)]: : __lttng_complete_channel_name all arg'
2807 '(-d --domain -f --fields --syscall):recording session name: __lttng_complete_session_name all all'
2808 '(-d --domain -k --kernel)'{-k,--kernel}'[list Linux kernel tracing domain objects]'
2809 '(-d --domain -u --userspace)'{-u,--userspace}'[list user space tracing domain objects]'
2810 '(-d --domain -j --jul)'{-j,--jul}'[list `java.util.logging` tracing domain objects]'
2811 )
2812
2813 # The Apache log4j 1.x tracing domain and `--syscall` options require
2814 # LTTng-tools ≥ 2.6.
2815 if ((minor_version >= 6)); then
2816 specs+=(
2817 '(-d --domain -l --log4j)'{-l,--log4j}'[list Apache log4j 1.x tracing domain objects]'
2818 '(-d --domain -f --fields -c --channel --mem-usage 1)--syscall[list Linux kernel system calls]'
2819 )
2820 fi
2821
2822 # The Python tracing domain options require LTTng-tools 2.7
2823 if ((minor_version >= 7)); then
2824 specs+=(
2825 '(-d --domain -p --python)'{-p,--python}'[list Python tracing domain objects]'
2826 )
2827 fi
2828
2829 # The Apache Log4j 2 tracing domain requires LTTng-tools ≥ 2.14.
2830 if ((minor_version >= 14)); then
2831 specs+=(
2832 '(-d --domain --log4j2)--log4j2[list Apache Log4j 2 tracing domain objects]'
2833 )
2834 fi
2835
2836 # The modern output options require LTTng-tools ≥ 2.15.
2837 if ((minor_version >= 15)); then
2838 specs+=(
2839 '(-f --fields --syscall)--mem-usage=[set channel memory usage display mode]:memory usage mode:(compact full)'
2840 '--style=[set command output style]:output style:(compact)'
2841 '--no-truncate[do not truncate long output lines]'
2842 )
2843 fi
2844
2845 # Add completions
2846 _arguments -s -W : $specs
2847 }
2848
2849 # Adds completions for the arguments of the `list-triggers` command.
2850 __lttng_complete_list_triggers_cmd() {
2851 _arguments -s -W : $help_opt_specs
2852 }
2853
2854 # Adds completions for the arguments of the `load` command.
2855 __lttng_complete_load_cmd() {
2856 specs=(
2857 $help_opt_specs
2858 '(-f --force)'{-f,--force}'[overwrite existing recording sessions]'
2859 '(-i --input-path)'{-i+,--input-path=}'[load recording session configurations from a specific path]:recording session configuration path:_files'
2860 '(-a --all --override-name 1)'{-a,--all}'[load all recording session configurations]'
2861 '(-a --all):recording session configuration name:_guard "^-*" "recording session name"'
2862 )
2863
2864 if ((minor_version >= 9)); then
2865 specs+=(
2866 '--override-url=[override the loaded recording session output URL]:output URL:'
2867 '(-a --all)--override-name=[override the loaded recording session name]:recording session name:'
2868 )
2869 fi
2870
2871 # Add completions
2872 _arguments -s -W : $specs
2873 }
2874
2875 # Adds completions for the arguments of the `metadata` command
2876 # (deprecated).
2877 __lttng_complete_metadata_cmd() {
2878 specs=(
2879 $help_opt_specs
2880 ':action:(regenerate)'
2881 '(-s --session)'{-s+,--session=}'[select a specific recording session]: : __lttng_complete_session_name all all'
2882 )
2883
2884 # Add completions
2885 _arguments -s -W : $specs
2886 }
2887
2888 # Adds completions for the arguments of the `regenerate` command.
2889 __lttng_complete_regenerate_cmd() {
2890 specs=(
2891 $help_opt_specs
2892 ':trace data type:(metadata statedump)'
2893 '(-s --session)'{-s+,--session=}'[select a specific recording session]: : __lttng_complete_session_name all all'
2894 )
2895
2896 # Add completions
2897 _arguments -s -W : $specs
2898 }
2899
2900 # Adds completions for the arguments of the `reclaim-memory` command.
2901 __lttng_complete_reclaim_memory_cmd() {
2902 specs=(
2903 $help_opt_specs
2904 '(-s --session)'{-s+,--session=}'[select a specific recording session]: : __lttng_complete_session_name all all'
2905 '(-n --no-wait)'{-n,--no-wait}'[exit immediately]'
2906 '--older-than=[only reclaim sub-buffers older than a threshold]:age (µs; `ms`/`s`/`m`/`h` suffixes supported):'
2907 '(-u --userspace)'{-u,--userspace}'[select user space tracing domain]'
2908 + '(channels)'
2909 {-a,--all}'[reclaim memory for all user space channels]'
2910 '*: : __lttng_complete_channel_name all opt'
2911 )
2912
2913 # Add completions
2914 _arguments -s -W : $specs
2915 }
2916
2917 # Adds completions for the arguments of the `remove-trigger` command.
2918 __lttng_complete_remove_trigger_cmd() {
2919 specs=(
2920 $help_opt_specs
2921 '--owner-uid=[remove the trigger as another Unix user]: : __lttng_complete_uid'
2922 ': : __lttng_complete_trigger_name'
2923 )
2924
2925 # Add completions
2926 _arguments -s -W : $specs
2927 }
2928
2929 # Adds completions for the arguments of the `rotate` command.
2930 __lttng_complete_rotate_cmd() {
2931 specs=(
2932 $help_opt_specs
2933 '(-n --no-wait)'{-n,--no-wait}'[exit immediately]'
2934 ': : __lttng_complete_session_name all all'
2935 )
2936
2937 # Add completions
2938 _arguments -s -W : $specs
2939 }
2940
2941 # Adds completions for the arguments of the `save` command.
2942 __lttng_complete_save_cmd() {
2943 specs=(
2944 $help_opt_specs
2945 '(-f --force)'{-f,--force}'[overwrite existing recording session configuration files]'
2946 '(-o --output-path)'{-o+,--output-path=}'[save recording session configuration files to a specific directory]:recording session configuration directory:_files -/'
2947 '(-a --all 1)'{-a,--all}'[save all recording session configurations]'
2948 '(-a --all): : __lttng_complete_session_name all all'
2949 )
2950
2951 # Add completions
2952 _arguments -s -W : $specs
2953 }
2954
2955 # Adds completions for the arguments of the `set-session` command.
2956 __lttng_complete_set_session_cmd() {
2957 specs=(
2958 $help_opt_specs
2959 ': : __lttng_complete_session_name all all'
2960 )
2961
2962 # Add completions
2963 _arguments -s -W : $specs
2964 }
2965
2966 # Adds completions for the arguments of the `snapshot` command.
2967 __lttng_complete_snapshot_cmd() {
2968 specs=(
2969 $help_opt_specs
2970 '(-): : __lttng_complete_snapshot_action_name' \
2971 '(-)*:: :->action-args' \
2972 )
2973
2974 # Add action name completions
2975 local curcontext=$curcontext state state_descr line
2976 local -A opt_args
2977
2978 if _arguments -C -s -W : $specs; then
2979 # Completions added: we're done
2980 return
2981 fi
2982
2983 # Add action-specific completions
2984 local -r common_session_specs=(
2985 '(-s --session)'{-s+,--session=}'[select a specific recording session]: : __lttng_complete_session_name all snapshot'
2986 )
2987 local -r common_output_specs=(
2988 '(-m --max-size)'{-m+,--max-size=}'[set the maximum total size of all snapshot files]:maximum size (bytes; `k`/`M`/`G` suffixes supported):'
2989 '(-n --name)'{-n+,--name=}'[set the snapshot output name]:snapshot output name:'
2990 '(1 -C --ctrl-url output)'{-C+,--ctrl-url=}'[set the control URL]:control URL:'
2991 '(1 -D --data-url output)'{-D+,--data-url=}'[set the trace data output URL]:trace data output URL:'
2992 '(-C --ctrl-url -D --data-url): :_guard "^-*" "snapshot output URL"'
2993 )
2994
2995 if [[ $state[1] == action-args ]]; then
2996 # Add completions for the arguments of the specific snapshot action
2997 curcontext=${curcontext%:*:*}:lttng-snapshot-$line[1]:
2998
2999 case $line[1] in
3000 add-output | record)
3001 specs=($common_session_specs $common_output_specs);;
3002 del-output)
3003 specs=($common_session_specs ':snapshot output index:(1)');;
3004 list-output)
3005 specs=($common_session_specs);;
3006 *)
3007 _message "unknown snapshot action \`$line[1]\`"
3008 return
3009 ;;
3010 esac
3011
3012 # Add completions
3013 _arguments -s -W : $specs
3014 return
3015 fi
3016
3017 return 1
3018 }
3019
3020 # Adds completions for the arguments of the `start` command.
3021 __lttng_complete_start_cmd() {
3022 specs=(
3023 $help_opt_specs
3024 + '(session)'
3025 ': : __lttng_complete_session_name inactive all'
3026 )
3027
3028 # The `--glob` and `--all` options require LTTng-tools ≥ 2.14
3029 if ((minor_version >= 14)); then
3030 specs+=(
3031 {-a,--all}'[start all recording sessions]'
3032 {-g+,--glob=}'[start recording sessions matched by a pattern]:globbing pattern:'
3033 )
3034 fi
3035
3036 # Add completions
3037 _arguments -s -W : $specs
3038 }
3039
3040 # Adds completions for the arguments of the `stop` command.
3041 __lttng_complete_stop_cmd() {
3042 specs=(
3043 $help_opt_specs
3044 '(-n --no-wait)'{-n,--no-wait}'[exit immediately]'
3045 + '(session)'
3046 ': : __lttng_complete_session_name active all'
3047 )
3048
3049 # The `--glob` and `--all` options require LTTng-tools ≥ 2.14
3050 if ((minor_version >= 14)); then
3051 specs+=(
3052 {-a,--all}'[stop all recording sessions]'
3053 {-g+,--glob=}'[stop recording sessions matched by a pattern]:globbing pattern:'
3054 )
3055 fi
3056
3057 # Add completions
3058 _arguments -s -W : $specs
3059 }
3060
3061 # Adds completions for the arguments of the `status` command.
3062 __lttng_complete_status_cmd() {
3063 specs=(
3064 $help_opt_specs
3065 )
3066
3067 # The forwarded `list` options require LTTng-tools ≥ 2.15
3068 if ((minor_version >= 15)); then
3069 specs+=(
3070 '(-c --channel)'{-c+,--channel=}'[select specific channel(s)]: : __lttng_complete_channel_name all cur'
3071 '--mem-usage=[set channel memory usage display mode]:memory usage mode:(compact full)'
3072 '--style=[set command output style]:output style:(compact)'
3073 '--no-truncate[do not truncate long output lines]'
3074 '(-k --kernel)'{-k,--kernel}'[select Linux kernel tracing domain]'
3075 '(-u --userspace)'{-u,--userspace}'[select user space tracing domain]'
3076 '(-j --jul)'{-j,--jul}'[select `java.util.logging` tracing domain]'
3077 '(-l --log4j)'{-l,--log4j}'[select Apache log4j 1.x tracing domain]'
3078 '--log4j2[select Apache Log4j 2 tracing domain]'
3079 '(-p --python)'{-p,--python}'[select Python tracing domain]'
3080 )
3081 fi
3082
3083 _arguments -s -W : $specs
3084 }
3085
3086 # Adds completions for the arguments of the `track` command.
3087 __lttng_complete_track_cmd() {
3088 specs=(
3089 $help_opt_specs
3090 '(-s --session)'{-s+,--session=}'[select a specific recording session]: : __lttng_complete_session_name all all'
3091 '(-a --all)'{-a,--all}'[add all possible values to the selected inclusion sets]'
3092 '(-p --pid)'{-p+,--pid=}'[add values to the process ID inclusion set]:process ID(s):_sequence _pids'
3093 )
3094
3095 # Virtual PID and user/group inclusion sets require LTTng-tools ≥ 2.12
3096 if ((minor_version >= 12)); then
3097 specs+=(
3098 '--vpid=[add values to the virtual process ID inclusion set]:virtual process ID(s):_sequence _pids'
3099 '(-u --userspace)--uid=[add values to the user ID inclusion set]:user(s):_sequence _users'
3100 '--vuid=[add values to the virtual user ID inclusion set]:virtual user(s):_sequence _users'
3101 '(-u --userspace)--gid=[add values to the group ID inclusion set]:group(s):_sequence _groups'
3102 '--vgid=[add values to the virtual group ID inclusion set]:virtual group(s):_sequence _groups'
3103 )
3104 fi
3105
3106 # Append tracing domain specifications
3107 specs+=(
3108 + '(domain)'
3109 {-k,--kernel}'[select the Linux kernel tracing domain]'
3110 "(--uid --gid)"{-u,--userspace}'[select the user space tracing domain]'
3111 )
3112
3113 # Add completions
3114 _arguments -s -W : $specs
3115 }
3116
3117 # Adds completions for the arguments of the `untrack` command.
3118 __lttng_complete_untrack_cmd() {
3119 # As of LTTng-tools 2.13, the `track` and `untrack` commands expect
3120 # the same arguments.
3121 __lttng_complete_track_cmd
3122 }
3123
3124 # Adds completions for the arguments of the `version` command.
3125 __lttng_complete_version_cmd() {
3126 _arguments -s -W : $help_opt_specs
3127 }
3128
3129 # Adds completions for the arguments of the `view` command.
3130 __lttng_complete_view_cmd() {
3131 specs=(
3132 $help_opt_specs
3133 '(-e --viewer)'{-e+,--viewer=}'[set the trace reader path]:trace reader path:_files'
3134 '(-t --trace-path): : __lttng_complete_session_name all all'
3135 '(1 -t --trace-path)'{-t+,--trace-path=}'[set the trace directory to pass to the reader]:trace directory:_files -/'
3136 )
3137
3138 # Add completions
3139 _arguments -s -W : $specs
3140 }
3141
3142 # Adds completions for the specific `lttng` command named `line[1]`.
3143 __lttng_complete_cmd() {
3144 # An lttng(1) command: replace `lttng` with `lttng-$line[1]` (for
3145 # example, `lttng-add-trigger`).
3146 curcontext=${curcontext%:*:*}:lttng-$line[1]:
3147
3148 # Shared parameters used by called functions
3149 local expl
3150 local -a specs
3151
3152 # Keep the tracing group: we need to execute `lttng` for some
3153 # completions and use the required tracing group to connect to the
3154 # same session daemon.
3155 #
3156 # The default tracing group is `tracing`.
3157 local tracing_group=tracing
3158
3159 if (($+opt_args[-g])); then
3160 tracing_group=$opt_args[-g]
3161 elif (($+opt_args[--group])); then
3162 tracing_group=$opt_args[--group]
3163 fi
3164
3165 # Add command completions: dispatch to a dedicated function
3166 local -r func_name=__lttng_complete_${line[1]//-/_}_cmd
3167
3168 if ! typeset -f $func_name >/dev/null; then
3169 _message "unknown command \`$line[1]\`"
3170 return
3171 fi
3172
3173 local -A opt_args
3174
3175 $func_name
3176 }
3177
3178 # Ensure predictable Zsh behavior and prevent option leakage
3179 emulate -L zsh
3180 setopt extended_glob
3181
3182 # Make `REPLY` local to this function
3183 local REPLY
3184
3185 # Save program name
3186 local -r prog_name=$words[1]
3187
3188 # First, set the `minor_version` parameter to the minor version of
3189 # LTTng-tools. Some features depend on a specific version and this
3190 # completion function supports many versions from LTTng-tools 2.5.
3191 local -i minor_version
3192
3193 __lttng_set_minor_version
3194
3195 # Exit now with LTTng-tools < 2.5 or LTTng-tools > 2.15
3196 local -r ignore_version_limit=${LTTNG_ZSH_COMP_IGNORE_VERSION_LIMIT:-0}
3197
3198 if ((minor_version < 5 || (minor_version > 15 && !ignore_version_limit))); then
3199 _message "completion not available for LTTng-tools 2.$minor_version; please update the completion files or set \`LTTNG_ZSH_COMP_IGNORE_VERSION_LIMIT=1\`"
3200 return
3201 fi
3202
3203 # Common help option specifications
3204 local -r help_opt_specs=(
3205 '(- : *)'{-h,--help}'[show help]'
3206 )
3207
3208 # Common non Linux kernel tracing domain option exclusions
3209 local -r non_kernel_domain_opt_excl=(-u --userspace -j --jul -l --log4j --log4j2 -p --python)
3210
3211 # General option specifications
3212 local gen_opt_specs=(
3213 $help_opt_specs
3214 '(- : *)--list-commands[list the available commands and quit]'
3215 '--relayd-path=[set the relay daemon path]:relay daemon path:_files -g \*lttng-relayd'
3216 '--group=[set the tracing group]:tracing group:_groups'
3217 '(-q --quiet)*'{-v,--verbose}'[increase verbosity]'
3218 '(-q --quiet -v --verbose)'{-q,--quiet}'[suppress all messages, including warnings and errors]'
3219 '(- : *)'{-V,--version}'[show version and quit]'
3220 )
3221
3222 # MI output requires LTTng-tools ≥ 2.6
3223 if ((minor_version >= 6)); then
3224 gen_opt_specs+=(
3225 '(-m --mi)'{-m+,--mi=}'[use the machine interface output]:machine interface type:(xml)'
3226 )
3227 fi
3228
3229 # Append session daemon option specifications
3230 gen_opt_specs+=(
3231 + '(sessiond)'
3232 {-n,--no-sessiond}'[do not spawn a session daemon]'
3233 '--sessiond-path=[set the session daemon path]:session daemon path:_files -g \*lttng-sessiond'
3234 )
3235
3236 # Add general option and command name completions
3237 local curcontext=$curcontext state state_descr line
3238 local -A opt_args
3239
3240 _arguments -C -s -W : \
3241 '(-): : __lttng_complete_cmd_name' \
3242 '(-)*:: :->cmd-args' \
3243 $gen_opt_specs
3244
3245 local -ir main_ret=$?
3246
3247 if ((main_ret == 0)); then
3248 # Completions added: we're done
3249 return
3250 fi
3251
3252 if [[ $state[1] == cmd-args ]]; then
3253 # Add completions for the arguments of the specific command
3254 __lttng_complete_cmd
3255 return
3256 fi
3257
3258 return $main_ret