remarkable-linux/tools/perf
Yunlong Song 1312c8a8fb perf tools: Avoid confusion with preloaded bash function for perf bash completion
Since some functions (e.g. '_get_comp_words_by_ref()') in perf bash
completion script are originally taken from git bash completion script,
these functions may be preloaded before perf bash completion script
runs.

In order to avoid repeating loading the same function twice, some test
constraints are used before these function definitions in the perf bash
completion script (e.g. 'type _get_comp_words_by_ref &>/dev/null ||').

The problem is that, if these functions in perf bash completion script
are changed for some reason, perf will still use the preloaded bash
functions rather than the customized functions of its own.

As a result, the perf bash completion will behave incorrectly. To get
rid of this problem, a flag can be defined to determine the proper
situation.

And to avoid overwriting the preloaded functions, the names of these
functions in perf bash completion script should be renamed to the
perf-customized ones.

Example:

Before this patch:

 $ type _get_comp_words_by_ref
 _get_comp_words_by_ref is a function
 _get_comp_words_by_ref ()
 {
     local exclude flag i OPTIND=1;
     local cur cword words=();
     local upargs=() upvars=() vcur vcword vprev vwords;
     while getopts "c:i:n:p:w:" flag "$@"; do
         case $flag in
             c)
                 vcur=$OPTARG
             ;;
             i)
                 vcword=$OPTARG
             ;;
             n)
                 exclude=$OPTARG
             ;;
             p)
                 vprev=$OPTARG
             ;;
             w)
                 vwords=$OPTARG
             ;;
         esac;
     done;
     while [[ $# -ge $OPTIND ]]; do
         case ${!OPTIND} in
             cur)
                 vcur=cur
             ;;
             prev)
                 vprev=prev
             ;;
             cword)
                 vcword=cword
             ;;
             words)
                 vwords=words
             ;;
             *)
                 echo "bash: $FUNCNAME(): \`${!OPTIND}': unknown argument" 1>&2;
                 return 1
             ;;
         esac;
         let "OPTIND += 1";
     done;
     __get_cword_at_cursor_by_ref "$exclude" words cword cur;
     [[ -n $vcur ]] && {
         upvars+=("$vcur");
         upargs+=(-v $vcur "$cur")
     };
     [[ -n $vcword ]] && {
         upvars+=("$vcword");
         upargs+=(-v $vcword "$cword")
     };
     [[ -n $vprev && $cword -ge 1 ]] && {
         upvars+=("$vprev");
         upargs+=(-v $vprev "${words[cword - 1]}")
     };
     [[ -n $vwords ]] && {
         upvars+=("$vwords");
         upargs+=(-a${#words[@]} $vwords "${words[@]}")
     };
     (( ${#upvars[@]} )) && local "${upvars[@]}" && _upvars "${upargs[@]}"
 }

As shown above, the _get_comp_words_by_ref is the preloaded function in
fact, rather than the function defined in perf-completion.sh. So if we
happen to change the function for some reason, the result will behave in
a wrong state.

After this patch:

We can set preload_get_comp_words_by_ref="false" to not use the preloaded
function. Instead, it will use the function defined in perf-completion.sh,
which is renamed as __perf_get_comp_words_by_ref to avoid overwriting
the preloaded function _get_comp_words_by_ref.

 $ type __perf_get_comp_words_by_ref
 __perf_get_comp_words_by_ref is a function
 __perf_get_comp_words_by_ref ()
 {
     local exclude cur_ words_ cword_;
     if [ "$1" = "-n" ]; then
         exclude=$2;
         shift 2;
     fi;
     __my_reassemble_comp_words_by_ref "$exclude";
     cur_=${words_[cword_]};
     while [ $# -gt 0 ]; do
         case "$1" in
             cur)
                 cur=$cur_
             ;;
             prev)
                 prev=${words_[$cword_-1]}
             ;;
             words)
                 words=("${words_[@]}")
             ;;
             cword)
                 cword=$cword_
             ;;
         esac;
         shift;
     done
 }

As shown above, the function __perf_get_comp_words_by_ref is loaded and
can work this time.

Note that we do not change the original behavior when those functions are
not preloaded before perf bash completion script runs. In this case,
although the flag is set to "true", the code will still change it to
"false" to use the function defined in perf-completion.sh.

Signed-off-by: Yunlong Song <yunlong.song@huawei.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/r/1426685758-25488-14-git-send-email-yunlong.song@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-03-19 13:53:27 -03:00
..
arch perf build: Add arch sparc objects building 2015-02-12 13:22:01 -03:00
bench Merge 'tip/perf/urgent' into perf/core to pick fixes 2015-03-02 11:45:49 -03:00
config perf tools: Fix perf-read-vdsox32 not building and lib64 install dir 2015-03-19 13:38:05 -03:00
Documentation perf kmem: Allow -v option 2015-03-13 07:47:48 -03:00
python
scripts perf build: Add scripts objects building 2015-02-12 11:49:53 -03:00
tests perf tools: Add the bash completion for listing subsubcommands of perf test 2015-03-19 13:52:54 -03:00
ui perf hists browser: Allow annotating entries in callchains 2015-03-17 18:27:28 -03:00
util perf hists browser: Allow annotating entries in callchains 2015-03-17 18:27:28 -03:00
.gitignore perf tools: Add PERF-FEATURES to the .gitignore file 2015-03-02 12:06:20 -03:00
Build perf tools: Add new 'perf data' command 2015-02-25 12:42:25 -03:00
builtin-annotate.c perf ordered_events: Shorten function signatures 2015-03-11 10:17:09 -03:00
builtin-bench.c
builtin-buildid-cache.c perf buildid-cache: Show usage with incorrect params 2015-02-27 15:52:40 -03:00
builtin-buildid-list.c perf ordered_events: Shorten function signatures 2015-03-11 10:17:09 -03:00
builtin-data.c perf tools: Add the bash completion for listing subsubcommands of perf data 2015-03-19 13:49:38 -03:00
builtin-diff.c perf ordered_events: Shorten function signatures 2015-03-11 10:17:09 -03:00
builtin-evlist.c perf tools: Modify error code for when perf_session__new() fails 2014-09-26 12:32:58 -03:00
builtin-help.c perf tools: Add the bash completion for listing subsubcommands of perf help 2015-03-19 13:49:39 -03:00
builtin-inject.c perf tools: tool->finished_round() doesn't need perf_session 2015-03-12 12:39:49 -03:00
builtin-kmem.c perf kmem: Fix alignment of slab result table 2015-03-13 07:47:48 -03:00
builtin-kvm.c perf tools: tool->finished_round() doesn't need perf_session 2015-03-12 12:39:49 -03:00
builtin-list.c perf list: Extend raw-dump to certain kind of events 2015-02-27 15:52:24 -03:00
builtin-lock.c perf ordered_events: Shorten function signatures 2015-03-11 10:17:09 -03:00
builtin-mem.c perf ordered_events: Shorten function signatures 2015-03-11 10:17:09 -03:00
builtin-probe.c perf probe: Add --quiet option to suppress output result message 2014-10-29 10:32:49 -02:00
builtin-record.c perf ordered_events: Shorten function signatures 2015-03-11 10:17:09 -03:00
builtin-report.c perf ordered_events: Shorten function signatures 2015-03-11 10:17:09 -03:00
builtin-sched.c perf ordered_events: Shorten function signatures 2015-03-11 10:17:09 -03:00
builtin-script.c perf tools: Add the bash completion for listing subsubcommands of perf script 2015-03-19 13:49:39 -03:00
builtin-stat.c perf stat: Always correctly indent ratio column 2015-03-13 07:47:44 -03:00
builtin-timechart.c perf tools: Add the bash completion for listing subsubcommands of perf timechart 2015-03-19 13:53:25 -03:00
builtin-top.c perf evlist: Adopt events_stats from perf_session 2015-02-22 22:22:57 -03:00
builtin-trace.c perf tools: Add the bash completion for listing subsubcommands of perf trace 2015-03-19 13:53:26 -03:00
builtin.h perf tools: Add new 'perf data' command 2015-02-25 12:42:25 -03:00
command-list.txt perf tools: Add new 'perf data' command 2015-02-25 12:42:25 -03:00
CREDITS
design.txt
Makefile perf tools: Compare JOBS to 0 after grep 2015-03-02 11:51:00 -03:00
Makefile.perf perf build: Rename PERF-FEATURES into FEATURE-DUMP 2015-03-18 15:10:44 -03:00
MANIFEST tools build: Add new build support 2015-02-11 18:30:03 -03:00
perf-archive.sh
perf-completion.sh perf tools: Avoid confusion with preloaded bash function for perf bash completion 2015-03-19 13:53:27 -03:00
perf-read-vdso.c perf tools: Build programs to copy 32-bit compatibility 2014-10-29 10:32:48 -02:00
perf-sys.h perf tools: Avoid build splat for syscall numbers with uclibc 2015-01-16 17:49:29 -03:00
perf-with-kcore.sh perf tools: Add perf-with-kcore script 2014-09-17 17:08:08 -03:00
perf.c perf tools: Fix the bash completion problem of 'perf --*' 2015-02-27 15:52:28 -03:00
perf.h perf record: Support recording running/enabled time 2015-02-25 12:42:23 -03:00