Skip to contentSkip to conversation

bpftool: auto-complete BTF IDs for btf dump

PATCH In review Andrii Nakryiko 3 participants 3 messages May 25, 2019 – May 26, 2019

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpftool: auto-complete BTF IDs for btf dump

Auto-complete BTF IDs for btf dump id sub-command. List of possible BTF IDs is scavenged from loaded BPF programs that have associated BTFs, as there is currently no API in libbpf to fetch list of all BTFs in the system.

---
 tools/bpf/bpftool/bash-completion/bpftool | 10 ++++++++++
 1 file changed, 10 insertions(+)
tools/bpf/bpftool/bash-completion/bpftool +10 -0
index 75c01eafd3a1..9fbc33e93689 100644
@@ -71,6 +71,13 @@ _bpftool_get_prog_tags()
         command sed -n 's/.*"tag": "\(.*\)",$/\1/p' )" -- "$cur" ) )
 }
 
+_bpftool_get_btf_ids()
+{
+    COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \
+        command sed -n 's/.*"btf_id": \(.*\),\?$/\1/p' | \
+        command sort -nu )" -- "$cur" ) )
+}
+
 _bpftool_get_obj_map_names()
 {
     local obj
@@ -635,6 +642,9 @@ _bpftool()
                                 map)
                                     _bpftool_get_map_ids
                                     ;;
+                                dump)
+                                    _bpftool_get_btf_ids
+                                    ;;
                             esac
                             return 0
                             ;;
Signature · 1 line
2.17.1
#1 lore raw

2019-05-24 22:38 UTC-0700 ~ Andrii Nakryiko <[email protected]>

Quoted 16 lines

Auto-complete BTF IDs for btf dump id sub-command. List of possible BTF IDs is scavenged from loaded BPF programs that have associated BTFs, as there is currently no API in libbpf to fetch list of all BTFs in the system.

---
 tools/bpf/bpftool/bash-completion/bpftool | 10 ++++++++++
 1 file changed, 10 insertions(+)
tools/bpf/bpftool/bash-completion/bpftool +0 -0
index 75c01eafd3a1..9fbc33e93689 100644

tools/bpf/bpftool/bash-completion/bpftool _bpftool_get_prog_tags() · line 79

Quoted 9 lines
tools/bpf/bpftool/bash-completion/bpftool +6 -0
@@ -71,6 +71,13 @@ _bpftool_get_prog_tags()
         command sed -n 's/.*"tag": "\(.*\)",$/\1/p' )" -- "$cur" ) )
 }
 
+_bpftool_get_btf_ids()
+{
+    COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \
+        command sed -n 's/.*"btf_id": \(.*\),\?$/\1/p' | \
+        command sort -nu )" -- "$cur" ) )
+}

Thanks! It works well. It looks like the "sort -nu" is not required, however? Bash completion on my system seems to run the equivalent of "sort -u" on the results anyway, ignoring the ordering you made just before. As I understand this is what completion always does, unless we pass "-o nosort" to "complete".

E.g. I get the same following output:

	1     1234  191   222   25

When completing with this function:

	_bpftool()
	{
		COMPREPLY+=( $( compgen -W "$( \
			command echo '1 1 1 191 1234 25 222')"))
	}
	complete -F _bpftool bpftool

or with that one:

	_bpftool()
	{
		COMPREPLY+=( $( compgen -W "$( \
			command echo '1 1 1 191 1234 25 222' | \
			command sort -nu )" ) )
	}
	complete -F _bpftool bpftool

Could you double check you have the same thing on your setup, please? If so we can just remove the "sort -nu".

Quentin

#2 lore raw
Quoted 57 lines · Quentin Monnet

2019-05-24 22:38 UTC-0700 ~ Andrii Nakryiko <[email protected]>

Quoted 16 lines

Auto-complete BTF IDs for btf dump id sub-command. List of possible BTF IDs is scavenged from loaded BPF programs that have associated BTFs, as there is currently no API in libbpf to fetch list of all BTFs in the system.

---
 tools/bpf/bpftool/bash-completion/bpftool | 10 ++++++++++
 1 file changed, 10 insertions(+)
tools/bpf/bpftool/bash-completion/bpftool +0 -0
index 75c01eafd3a1..9fbc33e93689 100644

tools/bpf/bpftool/bash-completion/bpftool _bpftool_get_prog_tags() · line 79

Quoted 9 lines
tools/bpf/bpftool/bash-completion/bpftool +6 -0
@@ -71,6 +71,13 @@ _bpftool_get_prog_tags()
         command sed -n 's/.*"tag": "\(.*\)",$/\1/p' )" -- "$cur" ) )
 }
 
+_bpftool_get_btf_ids()
+{
+    COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \
+        command sed -n 's/.*"btf_id": \(.*\),\?$/\1/p' | \
+        command sort -nu )" -- "$cur" ) )
+}

Thanks! It works well. It looks like the "sort -nu" is not required, however? Bash completion on my system seems to run the equivalent of "sort -u" on the results anyway, ignoring the ordering you made just before. As I understand this is what completion always does, unless we pass "-o nosort" to "complete".

E.g. I get the same following output:

        1     1234  191   222   25

When completing with this function:

        _bpftool()
        {
                COMPREPLY+=( $( compgen -W "$( \
                        command echo '1 1 1 191 1234 25 222')"))
        }
        complete -F _bpftool bpftool

or with that one:

        _bpftool()
        {
                COMPREPLY+=( $( compgen -W "$( \
                        command echo '1 1 1 191 1234 25 222' | \
                        command sort -nu )" ) )
        }
        complete -F _bpftool bpftool
Quoted 2 lines

Could you double check you have the same thing on your setup, please? If so we can just remove the "sort -nu".

Yeah, seems like it behaves the same without sort -nu, I dropped it in v2. Thanks!

Quoted 2 lines

Quentin