blob: 82a3b91dfd021c575b4ca9da5d1c8962fd38b611 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
|
make_small() {
local w=$1
echo $w | tr A-Z a-z
}
show_instances()
{
local cur="$1"
local bufs=$(trace-cmd list -B)
if [ "$bufs" == "No buffer instances defined" ]; then
return 0
fi
COMPREPLY=( $(compgen -W "${bufs}" -- "${cur}") )
return 0
}
show_virt()
{
local cur="$1"
if ! which virsh &>/dev/null; then
return 1
fi
local virt=`virsh list | awk '/^ *[0-9]/ { print $2 }'`
COMPREPLY=( $(compgen -W "${virt}" -- "${cur}") )
return 0
}
show_options()
{
local cur="$1"
local options=$(trace-cmd list -o | sed -e 's/^\(no\)*\(.*\)/\2 no\2/')
COMPREPLY=( $(compgen -W "${options}" -- "${cur}") )
return 0
}
__show_files()
{
COMPREPLY=( $(compgen -f -- "$cur") )
if [ ${#COMPREPLY[@]} -gt 1 ]; then
return 0;
fi
# directories get '/' instead of space
DIRS=( $(compgen -d -- "$cur"))
if [ ${#DIRS[@]} -eq 1 ]; then
compopt -o nospace
COMPREPLY="$DIRS/"
return 0;
fi
return 0
}
cmd_options()
{
local type="$1"
local cur="$2"
local extra="$3"
local cmds=$(trace-cmd $type -h 2>/dev/null|grep "^ *-" | \
sed -e 's/ *\(-[^ ]*\).*/\1/')
COMPREPLY=( $(compgen -W "${cmds} ${extra}" -- "${cur}") )
}
cmd_options_files()
{
cmd_options "$1" "$2" "$3"
if [ ${#COMPREPLY[@]} -eq 0 ]; then
__show_files "${cur}"
fi
}
plugin_options()
{
local cur="$1"
local opts=$(trace-cmd list -O | sed -ne 's/option://p')
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
}
compression_param()
{
local opts=$(trace-cmd list -c | grep -v 'Supported' | cut -d "," -f1)
opts+=" any none "
COMPREPLY=( $(compgen -W "${opts}") )
}
list_events() {
local cur=$1
local list=$(trace-cmd list -e "$cur")
local prefix=${cur%%:*}
if [ -z "$cur" -o "$cur" != "$prefix" ]; then
echo "${list}"
else
local events=$(for e in $list; do echo ${e/*:/}; done | sort -u)
local systems=$(for s in $list; do echo ${s/:*/:}; done | sort -u)
echo "${events} ${systems}"
fi
}
__trace_cmd_list_complete()
{
local prev=$1
local cur=$2
shift 2
local words=("$@")
case "$prev" in
list)
local cmds=$(trace-cmd list -h |egrep "^ {10}-" | \
sed -e 's/.*\(-.\).*/\1/')
COMPREPLY=( $(compgen -W "${cmds}" -- "${cur}") )
;;
-e)
local list=`list_events "$cur"`
COMPREPLY=( $(compgen -W "all $list" -- "${cur}") )
;;
*)
size=${#words[@]}
if [ $size -gt 3 ]; then
if [ "$cur" == "-" ]; then
let size=$size-3
else
let size=$size-2
fi
local w="${words[$size]}"
if [ "$w" == "-e" ]; then
local cmds=$(trace-cmd list -h |egrep "^ {12}-" | \
sed -e 's/.*\(-.\).*/\1/')
COMPREPLY=( $(compgen -W "${cmds}" -- "${cur}") )
fi
fi
;;
esac
}
__trace_cmd_show_complete()
{
local prev=$1
local cur=$2
shift 2
local words=("$@")
case "$prev" in
-B)
show_instances "$cur"
;;
--hist|--trigger)
local list=`list_events $cur`
COMPREPLY=( $(compgen -W "all ${list}" -- "${cur}") )
;;
*)
cmd_options show "$cur"
;;
esac
}
__trace_cmd_extract_complete()
{
local prev=$1
local cur=$2
shift 2
local words=("$@")
case "$prev" in
extract)
cmd_options "$prev" "$cur"
;;
-B)
show_instances "$cur"
;;
*)
__show_files
;;
esac
}
__trace_cmd_record_complete()
{
local prev=$1
local cur=$2
shift 2
local words=("$@")
case "$prev" in
-e)
local list=`list_events $cur`
COMPREPLY=( $(compgen -W "all ${list}" -- "${cur}") )
# This is still to handle the "*:*" special case
if [[ -n "$prefix" ]]; then
local reply_n=${#COMPREPLY[*]}
for (( i = 0; i < $reply_n; i++)); do
COMPREPLY[$i]=${COMPREPLY[i]##${prefix}:}
done
fi
;;
-p)
local plugins=$(trace-cmd list -p)
COMPREPLY=( $(compgen -W "${plugins}" -- "${cur}" ) )
;;
-l|-n|-g)
# This is extremely slow still (may take >1sec).
local funcs=$(trace-cmd list -f | sed 's/ .*//')
COMPREPLY=( $(compgen -W "${funcs}" -- "${cur}") )
;;
-B)
show_instances "$cur"
;;
-O)
show_options "$cur"
;;
-A)
if ! show_virt "$cur"; then
cmd_options_files record "$cur"
fi
;;
--compression)
compression_param
;;
*)
# stream start and profile do not show all options
cmd_options_files record "$cur"
;;
esac
}
__trace_cmd_report_complete()
{
local prev=$1
local cur=$2
shift 2
local words=("$@")
case "$prev" in
-O)
plugin_options "$cur"
;;
*)
cmd_options report "$cur"
;;
esac
}
dynevent_options()
{
local cur="$1"
local opts=("kprobe" "kretprobe" "uprobe" "uretprobe" "eprobe" "synth" "all")
COMPREPLY=( $(compgen -W "${opts[*]}" -- "${cur}") )
}
__trace_cmd_reset_complete()
{
local prev=$1
local cur=$2
shift 2
local words=("$@")
case "$prev" in
-B)
show_instances "$cur"
;;
-k)
dynevent_options "$cur"
;;
*)
cmd_options reset "$cur"
;;
esac
}
__trace_cmd_dump_complete()
{
local prev=$1
local cur=$2
shift 2
local words=("$@")
case "$prev" in
-i)
__show_files
;;
*)
cmd_options dump "$cur"
;;
esac
}
__trace_cmd_convert_complete()
{
local prev=$1
local cur=$2
shift 2
local words=("$@")
case "$prev" in
-i)
__show_files
;;
-o)
__show_files
;;
--compression)
compression_param
;;
*)
cmd_options convert "$cur"
;;
esac
}
##### SQLHIST COMMANDS #####
prev_keyword() {
local i=$1
shift
local words=("$@")
while [ $i -gt 0 ]; do
let i=$i-1
local w=`make_small ${words[$i]}`
case $w in
select)
echo "select"
return
;;
from)
echo "from"
return
;;
as)
echo "as"
return
;;
on)
echo "on"
return
;;
join)
echo "join"
return
;;
where)
echo "where"
return
;;
*)
if [ "$w" != "${w%%,}" ]; then
echo ","
return
fi
if [ "$w" != "${w%%=}" ]; then
echo "="
return
fi
;;
esac
done
echo ""
}
prev_command() {
local i=$1
shift
local words=("$@")
while [ $i -gt 0 ]; do
let i=$i-1
local w=`make_small ${words[$i]}`
case $w in
select)
echo "select"
return
;;
from)
echo "from"
return
;;
on)
echo "on"
return
;;
join)
echo "join"
return
;;
where)
echo "where"
return
;;
esac
done
echo ""
}
add_vars() {
local words=("$@")
local i=$COMP_CWORD
let found_from=0
while [ $i -gt 0 ]; do
let i=$i-1
local w=`make_small ${words[$i]}`
case $w in
"from")
let found_from=1
;;
*)
if [ $found_from ]; then
start=`echo $w | sed -e 's/\.[^\.]*$//'`
if [ "$start" != "$w" -a "$start" == "${start%%\.*}" ]; then
echo -n "$start "
fi
fi
;;
esac
done
}
add_options() {
local cur="$1"
local list="$2"
COMPREPLY=( $(compgen -W "${list}" -- "${cur}") )
}
print_fields() {
local event=$1
local var=$2
local extra=$3
local list=`trace-cmd list -e "^${event/\./:}\$" -F | cut -d';' -f1 | sed -ne 's/\t.*:.* \(.*\)/\1/p' |sed -e 's/\[.*\]//'`
for field in $list $extra; do
echo "$event.$field"
if [ ! -z "$var" ]; then
echo "$var.$field"
fi
done
}
select_options() {
local cur=$1
local extra=$2
local list=`list_events "${cur/\./:}" | sed -e 's/:/./g'`
local select_list=" TIMESTAMP_DELTA TIMESTAMP_DELTA_USECS $extra"
local select_fields=" TIMESTAMP TIMESTAMP_USECS STACKTRACE"
add_options "$cur" "$list $select_list"
local cnt=${#COMPREPLY[@]}
if [ $cnt -eq 1 ]; then
local comp=${COMPREPLY[0]}
local w=$(compgen -W "$select_list" -- "$comp" )
if [ -z "$w" ]; then
COMPREPLY=("$comp.")
compopt -o nospace
fi
elif [ $cnt -eq 0 ]; then
local w=`echo $cur | sed -e 's/\.[^\.]*$//'`
list=`print_fields $w "" "$select_fields"`
COMPREPLY=( $(compgen -W "${list}" -- "${cur}") )
fi
}
check_as() {
local words=("$@")
last_key=`prev_keyword $COMP_CWORD ${words[@]}`
if [ "$last_key" != "as" ]; then
echo -n "AS"
fi
}
on_list() {
local type=$1
shift
local words=("$@")
local i=$COMP_CWORD
local var=""
while [ $i -gt 0 ]; do
let i=$i-1
local w=`make_small ${words[$i]}`
case $w in
"from"|"join")
if [ $w == $type ]; then
print_fields ${words[$i+1]} "$var"
return
fi
var=""
;;
as)
var=${words[$i+1]}
;;
esac
done
}
update_completion() {
local cur=$1
shift
local words=("$@")
if [ ${#COMPREPLY[@]} -gt 0 ]; then
return
fi
for w in ${words[@]}; do
if [ "$w" != "${w##$cur}" ]; then
COMPREPLY=("$w")
return
fi
done
}
__trace_cmd_sqlhist_complete()
{
local prev=$1
local cur=$2
shift 2
local words=("$@")
if [ "$cur" != "${cur%%,}" ]; then
COMPREPLY=("$cur")
return
fi
local p=`make_small $prev`
if [ "$p" != "${p%%,}" ]; then
p=`prev_command $COMP_CWORD ${words[@]}`
fi
case "$p" in
"sqlhist")
cmd_options sqlhist "$cur" "SELECT"
update_completion "$cur" select
;;
"select")
select_options "$cur"
;;
"on")
list=`on_list "from" ${words[@]}`
add_options "$cur" "$list"
;;
"as")
local last_cmd=`prev_command $COMP_CWORD ${words[@]}`
case $last_cmd in
"from"|"join")
list=`add_vars ${words[@]}`
if [ ! -z "$list" ]; then
add_options "$cur" "$list"
fi
;;
esac
;;
"from"|"join")
local list=$(trace-cmd list -e "${cur/\./:}" | tr : .)
local prefix=${cur/\./}
if [ -z "$cur" -o "$cur" != "$prefix" ]; then
COMPREPLY=( $(compgen -W "${list}" -- "${cur}") )
else
local events=$(for e in $list; do echo ${e/*\./}; done | sort -u)
local systems=$(for s in $list; do echo ${s/\.*/.}; done | sort -u)
COMPREPLY=( $(compgen -W "all ${events} ${systems}" -- "${cur}") )
fi
;;
*)
local last_cmd=`prev_command $COMP_CWORD ${words[@]}`
local list=`check_as ${words[@]}`
local alist=""
if [ ! -z "$list" ]; then
alist="as"
fi
case $last_cmd in
"select")
if [ "$cur" != "${cur%%,}" ]; then
select_options "$cur" "$list"
else
add_options "$cur" "FROM , $list"
update_completion "$cur" from $alist
fi
;;
"from")
add_options "$cur" "JOIN $list"
update_completion "$cur" join $alist
;;
"join")
add_options "$cur" "ON $list"
update_completion "$cur" on $alist
;;
"on")
if [ "$cur" != "${cur%%=}" ]; then
COMPREPLY=("")
else
last_key=`prev_keyword $COMP_CWORD ${words[@]}`
if [ "$last_key" == "=" ]; then
if [ $prev == "=" ]; then
list=`on_list "join" ${words[@]}`
add_options "$cur" "$list"
else
add_options "$cur" "WHERE"
update_completion "$cur" where
fi
else
add_options "$cur" "="
fi
fi
;;
"where")
;;
*)
cmd_options sqlhist "$cur" "SELECT"
update_completion "$cur" select
;;
esac
;;
esac
}
##### SQLHIST COMMANDS END #####
__show_command_options()
{
local command="$1"
local prev="$2"
local cur="$3"
local cmds=( $(trace-cmd --help 2>/dev/null | \
grep " - " | sed 's/^ *//; s/ -.*//') )
for cmd in ${cmds[@]}; do
if [ $cmd == "$command" ]; then
local opts=$(trace-cmd $cmd -h 2>/dev/null|grep "^ *-" | \
sed -e 's/ *\(-[^ ]*\).*/\1/')
if [ "$prev" == "-B" ]; then
for opt in ${opts[@]}; do
if [ "$opt" == "-B" ]; then
show_instances "$cur"
return 0
fi
done
fi
COMPREPLY=( $(compgen -W "${opts}" -- "$cur"))
break
fi
done
if [ ${#COMPREPLY[@]} -eq 0 ]; then
__show_files "${cur}"
fi
}
_trace_cmd_complete()
{
local cur=""
local prev=""
local words=()
# Not to use COMP_WORDS to avoid buggy behavior of Bash when
# handling with words including ":", like:
#
# prev="${COMP_WORDS[COMP_CWORD-1]}"
# cur="${COMP_WORDS[COMP_CWORD]}"
#
# Instead, we use _get_comp_words_by_ref() magic.
_get_comp_words_by_ref -n : cur prev words
if [ "$prev" == "trace-cmd" ]; then
local cmds=$(trace-cmd --help 2>/dev/null | \
grep " - " | sed 's/^ *//; s/ -.*//')
COMPREPLY=( $(compgen -W "${cmds}" -- "${cur}") )
return;
fi
local w="${words[1]}"
case "$w" in
list)
__trace_cmd_list_complete "${prev}" "${cur}" ${words[@]}
return 0
;;
show)
__trace_cmd_show_complete "${prev}" "${cur}" ${words[@]}
return 0
;;
extract)
__trace_cmd_extract_complete "${prev}" "${cur}" ${words[@]}
return 0
;;
record|stream|start|set|profile)
__trace_cmd_record_complete "${prev}" "${cur}" ${words[@]}
return 0
;;
report)
__trace_cmd_report_complete "${prev}" "${cur}" ${words[@]}
return 0
;;
reset)
__trace_cmd_reset_complete "${prev}" "${cur}" "${words[@]}"
return 0
;;
dump)
__trace_cmd_dump_complete "${prev}" "${cur}" ${words[@]}
return 0
;;
convert)
__trace_cmd_convert_complete "${prev}" "${cur}" ${words[@]}
return 0
;;
sqlhist)
__trace_cmd_sqlhist_complete "${prev}" "${cur}" ${words[@]}
return 0
;;
*)
__show_command_options "$w" "${prev}" "${cur}"
;;
esac
}
complete -F _trace_cmd_complete trace-cmd
|