1252 lines · 45.4 KB · Text
RawDownload
# bpftool(8) bash completion                               -*- shell-script -*-## SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)# Copyright (C) 2017-2018 Netronome Systems, Inc.## Author: Quentin Monnet <quentin.monnet@netronome.com> # Takes a list of words in argument; each one of them is added to COMPREPLY if# it is not already present on the command line. Returns no value._bpftool_once_attr(){    local w idx found    for w in $*; do        found=0        for (( idx=3; idx < ${#words[@]}-1; idx++ )); do            if [[ $w == ${words[idx]} ]]; then                found=1                break            fi        done        [[ $found -eq 0 ]] && \            COMPREPLY+=( $( compgen -W "$w" -- "$cur" ) )    done} # Takes a list of words as argument; if any of those words is present on the# command line, return 0. Otherwise, return 1._bpftool_search_list(){    local w idx    for w in $*; do        for (( idx=3; idx < ${#words[@]}-1; idx++ )); do            [[ $w == ${words[idx]} ]] && return 0        done    done    return 1} # Takes a list of words in argument; adds them all to COMPREPLY if none of them# is already present on the command line. Returns no value._bpftool_one_of_list(){    _bpftool_search_list $* && return 1    COMPREPLY+=( $( compgen -W "$*" -- "$cur" ) )} _bpftool_get_map_ids(){    COMPREPLY+=( $( compgen -W "$( bpftool -jp map  2>&1 | \        command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )} # Takes map type and adds matching map ids to the list of suggestions._bpftool_get_map_ids_for_type(){    local type="$1"    COMPREPLY+=( $( compgen -W "$( bpftool -jp map  2>&1 | \        command grep -C2 "$type" | \        command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )} _bpftool_get_map_names(){    COMPREPLY+=( $( compgen -W "$( bpftool -jp map  2>&1 | \        command sed -n 's/.*"name": \(.*\),$/\1/p' )" -- "$cur" ) )} # Takes map type and adds matching map names to the list of suggestions._bpftool_get_map_names_for_type(){    local type="$1"    COMPREPLY+=( $( compgen -W "$( bpftool -jp map  2>&1 | \        command grep -C2 "$type" | \        command sed -n 's/.*"name": \(.*\),$/\1/p' )" -- "$cur" ) )} _bpftool_get_prog_ids(){    COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \        command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )} _bpftool_get_prog_tags(){    COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \        command sed -n 's/.*"tag": "\(.*\)",$/\1/p' )" -- "$cur" ) )} _bpftool_get_prog_names(){    COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \        command sed -n 's/.*"name": "\(.*\)",$/\1/p' )" -- "$cur" ) )} _bpftool_get_btf_ids(){    COMPREPLY+=( $( compgen -W "$( bpftool -jp btf 2>&1 | \        command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )} _bpftool_get_link_ids(){    COMPREPLY+=( $( compgen -W "$( bpftool -jp link 2>&1 | \        command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )} _bpftool_get_obj_map_names(){    local obj maps     obj=$1     maps=$(objdump -j .maps -t $obj 2>/dev/null | \        command awk '/g     . .maps/ {print $NF}')     COMPREPLY+=( $( compgen -W "$maps" -- "$cur" ) )} _bpftool_get_obj_map_idxs(){    local obj nmaps     obj=$1     nmaps=$(objdump -j maps -t $obj 2>/dev/null | grep -c 'g     . maps')     COMPREPLY+=( $( compgen -W "$(seq 0 $((nmaps - 1)))" -- "$cur" ) )} _sysfs_get_netdevs(){    COMPREPLY+=( $( compgen -W "$( ls /sys/class/net 2>/dev/null )" -- \        "$cur" ) )} # Retrieve type of the map that we are operating on._bpftool_map_guess_map_type(){    local keyword idx ref=""    for (( idx=3; idx < ${#words[@]}-1; idx++ )); do        case "${words[$((idx-2))]}" in            lookup|update)                keyword=${words[$((idx-1))]}                ref=${words[$((idx))]}                ;;            push)                printf "stack"                return 0                ;;            enqueue)                printf "queue"                return 0                ;;        esac    done    [[ -z $ref ]] && return 0     local type    type=$(bpftool -jp map show $keyword $ref | \        command sed -n 's/.*"type": "\(.*\)",$/\1/p')    [[ -n $type ]] && printf $type} _bpftool_map_update_get_id(){    local command="$1"     # Is it the map to update, or a map to insert into the map to update?    # Search for "value" keyword.    local idx value    for (( idx=7; idx < ${#words[@]}-1; idx++ )); do        if [[ ${words[idx]} == "value" ]]; then            value=1            break        fi    done    if [[ $value -eq 0 ]]; then        case "$command" in            push)                _bpftool_get_map_ids_for_type stack                ;;            enqueue)                _bpftool_get_map_ids_for_type queue                ;;            *)                _bpftool_get_map_ids                ;;        esac        return 0    fi     # Id to complete is for a value. It can be either prog id or map id. This    # depends on the type of the map to update.    local type=$(_bpftool_map_guess_map_type)    case $type in        array_of_maps|hash_of_maps)            _bpftool_get_map_ids            return 0            ;;        prog_array)            _bpftool_get_prog_ids            return 0            ;;        *)            return 0            ;;    esac} _bpftool_map_update_get_name(){    local command="$1"     # Is it the map to update, or a map to insert into the map to update?    # Search for "value" keyword.    local idx value    for (( idx=7; idx < ${#words[@]}-1; idx++ )); do        if [[ ${words[idx]} == "value" ]]; then            value=1            break        fi    done    if [[ $value -eq 0 ]]; then        case "$command" in            push)                _bpftool_get_map_names_for_type stack                ;;            enqueue)                _bpftool_get_map_names_for_type queue                ;;            *)                _bpftool_get_map_names                ;;        esac        return 0    fi     # Name to complete is for a value. It can be either prog name or map name. This    # depends on the type of the map to update.    local type=$(_bpftool_map_guess_map_type)    case $type in        array_of_maps|hash_of_maps)            _bpftool_get_map_names            return 0            ;;        prog_array)            _bpftool_get_prog_names            return 0            ;;        *)            return 0            ;;    esac} _bpftool(){    local cur prev words cword comp_args    local json=0    _init_completion -- "$@" || return     # Deal with options    if [[ ${words[cword]} == -* ]]; then        local c='--version --json --pretty --bpffs --mapcompat --debug \            --use-loader --base-btf --sign -i -k'        COMPREPLY=( $( compgen -W "$c" -- "$cur" ) )        return 0    fi    if _bpftool_search_list -j --json -p --pretty; then        json=1    fi     # Deal with simplest keywords    case $prev in        help|hex)            return 0            ;;        tag)            _bpftool_get_prog_tags            return 0            ;;        dev|offload_dev|xdpmeta_dev)            _sysfs_get_netdevs            return 0            ;;        file|pinned|-B|--base-btf|-i|-k)            _filedir            return 0            ;;        batch)            COMPREPLY=( $( compgen -W 'file' -- "$cur" ) )            return 0            ;;    esac     # Remove all options so completions don't have to deal with them.    local i pprev    for (( i=1; i < ${#words[@]}; )); do        case ${words[i]} in            # Remove option and its argument            -B|--base-btf|-i|-k)                words=( "${words[@]:0:i}" "${words[@]:i+2}" )                [[ $i -le $(($cword + 1)) ]] && cword=$(( cword - 2 ))                ;;            # No argument, remove option only            -*)                words=( "${words[@]:0:i}" "${words[@]:i+1}" )                [[ $i -le $cword ]] && cword=$(( cword - 1 ))                ;;            *)                i=$(( ++i ))                ;;        esac    done    cur=${words[cword]}    prev=${words[cword - 1]}    pprev=${words[cword - 2]}     local object=${words[1]}     if [[ -z $object || $cword -eq 1 ]]; then        case $cur in            *)                COMPREPLY=( $( compgen -W "$( bpftool help 2>&1 | \                    command sed \                    -e '/OBJECT := /!d' \                    -e 's/.*{//' \                    -e 's/}.*//' \                    -e 's/|//g' )" -- "$cur" ) )                COMPREPLY+=( $( compgen -W 'batch help' -- "$cur" ) )                return 0                ;;        esac    fi     local command=${words[2]}    [[ $command == help ]] && return 0     local MAP_TYPE='id pinned name'    local PROG_TYPE='id pinned tag name'     # Completion depends on object and command in use    case $object in        prog)            # Complete id and name, only for subcommands that use prog (but no            # map) ids/names.            case $command in                show|list|dump|pin)                    case $prev in                        id)                            _bpftool_get_prog_ids                            return 0                            ;;                        name)                            _bpftool_get_prog_names                            return 0                            ;;                    esac                    ;;            esac             local METRIC_TYPE='cycles instructions l1d_loads llc_misses \                itlb_misses dtlb_misses'            case $command in                show|list)                    [[ $prev != "$command" ]] && return 0                    COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )                    return 0                    ;;                dump)                    case $prev in                        $command)                            COMPREPLY+=( $( compgen -W "xlated jited" -- \                                "$cur" ) )                            return 0                            ;;                        xlated|jited)                            COMPREPLY=( $( compgen -W "$PROG_TYPE" -- \                                "$cur" ) )                            return 0                            ;;                        *)                            # "file" is not compatible with other keywords here                            if _bpftool_search_list 'file'; then                                return 0                            fi                            if ! _bpftool_search_list 'linum opcodes visual'; then                                _bpftool_once_attr 'file'                            fi                            _bpftool_once_attr 'linum opcodes'                            if _bpftool_search_list 'xlated' && [[ "$json" == 0 ]]; then                                _bpftool_once_attr 'visual'                            fi                            return 0                            ;;                    esac                    ;;                pin)                    if [[ $prev == "$command" ]]; then                        COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )                    else                        _filedir                    fi                    return 0                    ;;                attach|detach)                    case $cword in                        3)                            COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )                            return 0                            ;;                        4)                            case $prev in                                id)                                    _bpftool_get_prog_ids                                    ;;                                name)                                    _bpftool_get_prog_names                                    ;;                                pinned)                                    _filedir                                    ;;                            esac                            return 0                            ;;                        5)                            local BPFTOOL_PROG_ATTACH_TYPES='sk_msg_verdict \                                sk_skb_verdict sk_skb_stream_verdict sk_skb_stream_parser \                                flow_dissector'                            COMPREPLY=( $( compgen -W "$BPFTOOL_PROG_ATTACH_TYPES" -- "$cur" ) )                            return 0                            ;;                        6)                            COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )                            return 0                            ;;                        7)                            case $prev in                                id)                                    _bpftool_get_map_ids                                    ;;                                name)                                    _bpftool_get_map_names                                    ;;                                pinned)                                    _filedir                                    ;;                            esac                            return 0                            ;;                    esac                    ;;                load|loadall)                    local obj                     # Propose "load/loadall" to complete "bpftool prog load",                    # or bash tries to complete "load" as a filename below.                    if [[ ${#words[@]} -eq 3 ]]; then                        COMPREPLY=( $( compgen -W "load loadall" -- "$cur" ) )                        return 0                    fi                     if [[ ${#words[@]} -lt 6 ]]; then                        _filedir                        return 0                    fi                     obj=${words[3]}                     if [[ ${words[-4]} == "map" ]]; then                        COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )                        return 0                    fi                    if [[ ${words[-3]} == "map" ]]; then                        if [[ ${words[-2]} == "idx" ]]; then                            _bpftool_get_obj_map_idxs $obj                        elif [[ ${words[-2]} == "name" ]]; then                            _bpftool_get_obj_map_names $obj                        fi                        return 0                    fi                    if [[ ${words[-2]} == "map" ]]; then                        COMPREPLY=( $( compgen -W "idx name" -- "$cur" ) )                        return 0                    fi                     case $prev in                        type)                            local BPFTOOL_PROG_LOAD_TYPES='socket kprobe \                                kretprobe classifier flow_dissector \                                action tracepoint raw_tracepoint \                                xdp perf_event cgroup/skb cgroup/sock \                                cgroup/dev lwt_in lwt_out lwt_xmit \                                lwt_seg6local sockops sk_skb sk_msg lirc_mode2 \                                cgroup/bind4 cgroup/bind6 \                                cgroup/connect4 cgroup/connect6 cgroup/connect_unix \                                cgroup/getpeername4 cgroup/getpeername6 cgroup/getpeername_unix \                                cgroup/getsockname4 cgroup/getsockname6 cgroup/getsockname_unix \                                cgroup/sendmsg4 cgroup/sendmsg6 cgroup/sendmsg_unix \                                cgroup/recvmsg4 cgroup/recvmsg6 cgroup/recvmsg_unix \

Lines 1–500 of 1252.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t