ktap: A lightweight dynamic tracing tool for Linux
cover ktap: A lightweight dynamic tracing tool for Linux
Hi All,
The following set of patches add ktap tracing tool.
ktap is a new script-based dynamic tracing tool for Linux. It uses a scripting language and lets the user trace system dynamically.
Highlights features:
* a simple but powerful scripting language
* register-based interpreter (heavily optimized) in Linux kernel
* small and lightweight
* not depend on the GCC toolchain for each script run
* easy to use in embedded environments without debugging info
* support for tracepoint, kprobe, uprobe, function trace, timer, and more
* supported in x86, ARM, PowerPC, MIPS
* safety in sandbox
Simple examples:
1). simplest one-liner command to enable all tracepoints
ktap -e "trace *:* { print(argstr) }"2). syscall tracing on target process
ktap -e "trace syscalls:* { print(argstr) }" -- ls
3). simple syscall tracing
ktap -e "trace syscalls:* { print(cpu, pid, execname, argstr) }"
4). ftrace
ktap -e "trace ftrace:function /ip==mutex*/ { print(argstr) }"5). syscall tracing in histogram style
var s = {}
trace syscalls:sys_enter_* {
s[probename] += 1
}
trace_end {
print_hist(s)
}6). kprobe tracing
trace probe:do_sys_open dfd=%di fname=%dx flags=%cx mode=+4($stack) {
print("entry:", execname, argstr)
}
trace probe:do_sys_open%return fd=$retval {
print("exit:", execname, argstr)
}7). uprobe tracing
trace probe:/lib/libc.so.6:malloc {
print("entry:", execname, argstr)
}
trace probe:/lib/libc.so.6:malloc%return {
print("exit:", execname, argstr)
}8). stapsdt tracing (userspace static marker)
trace sdt:/lib64/libc.so.6:lll_futex_wake {
print("lll_futex_wake", execname, argstr)
}
or:
#trace all static mark in libc
trace sdt:/lib64/libc.so.6:* {
print(execname, argstr)
}9). timer
tick-1ms {
printf("time fired on one cpu\n");
}
profile-2s {
printf("time fired on every cpu\n");
}This patchset contains:
1. README and Tutorial
2. ktap kernel module and userspace binary
3. sample scripts
4. test suite
The code can be pull from:
http://github.com/ktap/linux.git upstream
The merit of putting this software in kernel tree is to make it more possible to get feedback from users and thus polish the code.
Thank you.
Jovi Zhangwei (28):
ktap: add README file
ktap: add ktap tutorial
ktap: add sample scripts
ktap: add basic ktap types definition
ktap: add bytecode definition
ktap: add include/ktap_arch.h and error header file
ktap: add runtime/ktap.[c|h]
ktap: add runtime/kp_bcread.[c|h]
ktap: add runtime/kp_vm.[c|h]
ktap: add runtime/kp_str.[c|h] and runtime/kp_mempool.[c|h]
ktap: add runtime/kp_tab.[c|h]
ktap: add runtime/kp_obj.[c|h]
ktap: add runtime/kp_transport.[c|h]
ktap: add runtime/kp_events.[c|h]
ktap: add built-in functions and library (runtime/lib_*.c)
ktap: add runtime/amalg.c
ktap: add userspace/kp_main.c
ktap: add compiler(userspace/kp_lex.[c|h] and
userspace/kp_parse.[c|h]) ktap: add userspace/symbol.[c|h]
ktap: add userspace/kp_parse_events.c
ktap: add userspace/kp_reader.c
ktap: add userspace/kp_bcwrite.c
ktap: add userspace/kp_util.c
ktap: add Makefile
ktap: add Kconfig
ktap: add testsuite
ktap: add vim syntax file
ktap: add COPYRIGHT file
tools/ktap/COPYRIGHT | 63 + tools/ktap/Kconfig | 21 + tools/ktap/Makefile | 189 ++ tools/ktap/README.md | 149 + tools/ktap/doc/tutorial.md | 666 +++++ tools/ktap/include/ktap_arch.h | 33 + tools/ktap/include/ktap_bc.h | 369 +++ tools/ktap/include/ktap_err.h | 11 + tools/ktap/include/ktap_errmsg.h | 135 + tools/ktap/include/ktap_types.h | 463 +++ tools/ktap/runtime/amalg.c | 37 + tools/ktap/runtime/kp_bcread.c | 428 +++ tools/ktap/runtime/kp_bcread.h | 6 + tools/ktap/runtime/kp_events.c | 832 ++++++ tools/ktap/runtime/kp_events.h | 71 + tools/ktap/runtime/kp_mempool.c | 94 + tools/ktap/runtime/kp_mempool.h | 8 + tools/ktap/runtime/kp_obj.c | 281 ++ tools/ktap/runtime/kp_obj.h | 19 + tools/ktap/runtime/kp_str.c | 360 +++ tools/ktap/runtime/kp_str.h | 13 + tools/ktap/runtime/kp_tab.c | 842 ++++++ tools/ktap/runtime/kp_tab.h | 59 + tools/ktap/runtime/kp_transport.c | 649 ++++ tools/ktap/runtime/kp_transport.h | 13 + tools/ktap/runtime/kp_vm.c | 1759 +++++++++++ tools/ktap/runtime/kp_vm.h | 43 + tools/ktap/runtime/ktap.c | 276 ++ tools/ktap/runtime/ktap.h | 181 ++ tools/ktap/runtime/lib_ansi.c | 142 + tools/ktap/runtime/lib_base.c | 409 +++ tools/ktap/runtime/lib_kdebug.c | 198 ++ tools/ktap/runtime/lib_net.c | 107 + tools/ktap/runtime/lib_table.c | 58 + tools/ktap/runtime/lib_timer.c | 210 ++ tools/ktap/samples/ansi/ansi_color_demo.kp | 22 + tools/ktap/samples/basic/backtrace.kp | 6 + tools/ktap/samples/basic/event_trigger.kp | 27 + tools/ktap/samples/basic/event_trigger_ftrace.kp | 27 + tools/ktap/samples/basic/ftrace.kp | 8 + tools/ktap/samples/basic/function_time.kp | 62 + tools/ktap/samples/basic/kretprobe.kp | 6 + tools/ktap/samples/basic/memcpy_memset.kp | 23 + tools/ktap/samples/game/tetris.kp | 297 ++ tools/ktap/samples/helloworld.kp | 3 + tools/ktap/samples/interrupt/hardirq_time.kp | 25 + tools/ktap/samples/interrupt/softirq_time.kp | 24 + tools/ktap/samples/io/kprobes-do-sys-open.kp | 20 + tools/ktap/samples/io/traceio.kp | 61 + tools/ktap/samples/mem/kmalloc-stack.kp | 12 + tools/ktap/samples/mem/kmem_count.kp | 29 + tools/ktap/samples/network/tcp_ipaddr.kp | 20 + tools/ktap/samples/profiling/function_profiler.kp | 41 + .../profiling/kprobe_all_kernel_functions.kp | 13 + tools/ktap/samples/profiling/stack_profile.kp | 27 + tools/ktap/samples/schedule/sched_transition.kp | 5 + tools/ktap/samples/schedule/schedtimes.kp | 131 + tools/ktap/samples/syscalls/errinfo.kp | 145 + tools/ktap/samples/syscalls/execve.kp | 9 + tools/ktap/samples/syscalls/opensnoop.kp | 31 + tools/ktap/samples/syscalls/sctop.kp | 13 + tools/ktap/samples/syscalls/syscalls.kp | 6 + tools/ktap/samples/syscalls/syscalls_count.kp | 54 + .../samples/syscalls/syscalls_count_by_proc.kp | 22 + tools/ktap/samples/syscalls/syslatl.kp | 33 + tools/ktap/samples/syscalls/syslist.kp | 31 + tools/ktap/samples/tracepoints/eventcount.kp | 210 ++ .../ktap/samples/tracepoints/eventcount_by_proc.kp | 57 + tools/ktap/samples/tracepoints/raw_tracepoint.kp | 15 + tools/ktap/samples/tracepoints/tracepoints.kp | 6 + tools/ktap/samples/userspace/gcc_unwind.kp | 9 + tools/ktap/samples/userspace/glibc_func_hist.kp | 44 + tools/ktap/samples/userspace/glibc_sdt.kp | 11 + tools/ktap/samples/userspace/glibc_trace.kp | 11 + tools/ktap/samples/userspace/malloc_free.kp | 20 + tools/ktap/samples/userspace/malloc_size_hist.kp | 22 + tools/ktap/samples/userspace/pthread.kp | 8 + tools/ktap/test/README | 69 + tools/ktap/test/arithmetic.t | 109 + tools/ktap/test/benchmark/cmp_neq.sh | 158 + tools/ktap/test/benchmark/cmp_profile.sh | 54 + tools/ktap/test/benchmark/cmp_table.sh | 112 + tools/ktap/test/benchmark/sembench.c | 556 ++++ tools/ktap/test/benchmark/test.sh | 128 + tools/ktap/test/cli-arg.t | 25 + tools/ktap/test/concat.t | 21 + tools/ktap/test/count.t | 25 + tools/ktap/test/deadloop.t | 37 + tools/ktap/test/fibonacci.t | 42 + tools/ktap/test/function.t | 78 + tools/ktap/test/if.t | 32 + tools/ktap/test/kprobe.t | 82 + tools/ktap/test/kretprobe.t | 35 + tools/ktap/test/len.t | 27 + tools/ktap/test/lib/Test/ktap.pm | 128 + tools/ktap/test/looping.t | 46 + tools/ktap/test/one-liner.t | 48 + tools/ktap/test/pairs.t | 52 + tools/ktap/test/stack_overflow.t | 22 + tools/ktap/test/syntax-err.t | 19 + tools/ktap/test/table.t | 81 + tools/ktap/test/time.t | 59 + tools/ktap/test/timer.t | 65 + tools/ktap/test/tracepoint.t | 53 + tools/ktap/test/util/reindex | 61 + tools/ktap/test/zerodivide.t | 21 + tools/ktap/userspace/kp_bcwrite.c | 375 +++ tools/ktap/userspace/kp_lex.c | 552 ++++ tools/ktap/userspace/kp_lex.h | 94 + tools/ktap/userspace/kp_main.c | 443 +++ tools/ktap/userspace/kp_parse.c | 3139 ++++++++++++++++++++ tools/ktap/userspace/kp_parse.h | 4 + tools/ktap/userspace/kp_parse_events.c | 798 +++++ tools/ktap/userspace/kp_reader.c | 106 + tools/ktap/userspace/kp_symbol.c | 360 +++ tools/ktap/userspace/kp_symbol.h | 50 + tools/ktap/userspace/kp_util.c | 646 ++++ tools/ktap/userspace/kp_util.h | 120 + tools/ktap/vim/ftdetect/ktap.vim | 3 + tools/ktap/vim/syntax/ktap.vim | 106 + 120 files changed, 19881 insertions(+) create mode 100644 tools/ktap/COPYRIGHT create mode 100644 tools/ktap/Kconfig create mode 100644 tools/ktap/Makefile create mode 100644 tools/ktap/README.md create mode 100644 tools/ktap/doc/tutorial.md create mode 100644 tools/ktap/include/ktap_arch.h create mode 100644 tools/ktap/include/ktap_bc.h create mode 100644 tools/ktap/include/ktap_err.h create mode 100644 tools/ktap/include/ktap_errmsg.h create mode 100644 tools/ktap/include/ktap_types.h create mode 100644 tools/ktap/runtime/amalg.c create mode 100644 tools/ktap/runtime/kp_bcread.c create mode 100644 tools/ktap/runtime/kp_bcread.h create mode 100644 tools/ktap/runtime/kp_events.c create mode 100644 tools/ktap/runtime/kp_events.h create mode 100644 tools/ktap/runtime/kp_mempool.c create mode 100644 tools/ktap/runtime/kp_mempool.h create mode 100644 tools/ktap/runtime/kp_obj.c create mode 100644 tools/ktap/runtime/kp_obj.h create mode 100644 tools/ktap/runtime/kp_str.c create mode 100644 tools/ktap/runtime/kp_str.h create mode 100644 tools/ktap/runtime/kp_tab.c create mode 100644 tools/ktap/runtime/kp_tab.h create mode 100644 tools/ktap/runtime/kp_transport.c create mode 100644 tools/ktap/runtime/kp_transport.h create mode 100644 tools/ktap/runtime/kp_vm.c create mode 100644 tools/ktap/runtime/kp_vm.h create mode 100644 tools/ktap/runtime/ktap.c create mode 100644 tools/ktap/runtime/ktap.h create mode 100644 tools/ktap/runtime/lib_ansi.c create mode 100644 tools/ktap/runtime/lib_base.c create mode 100644 tools/ktap/runtime/lib_kdebug.c create mode 100644 tools/ktap/runtime/lib_net.c create mode 100644 tools/ktap/runtime/lib_table.c create mode 100644 tools/ktap/runtime/lib_timer.c create mode 100644 tools/ktap/samples/ansi/ansi_color_demo.kp create mode 100644 tools/ktap/samples/basic/backtrace.kp create mode 100644 tools/ktap/samples/basic/event_trigger.kp create mode 100644 tools/ktap/samples/basic/event_trigger_ftrace.kp create mode 100644 tools/ktap/samples/basic/ftrace.kp create mode 100644 tools/ktap/samples/basic/function_time.kp create mode 100644 tools/ktap/samples/basic/kretprobe.kp create mode 100644 tools/ktap/samples/basic/memcpy_memset.kp create mode 100644 tools/ktap/samples/game/tetris.kp create mode 100644 tools/ktap/samples/helloworld.kp create mode 100644 tools/ktap/samples/interrupt/hardirq_time.kp create mode 100644 tools/ktap/samples/interrupt/softirq_time.kp create mode 100644 tools/ktap/samples/io/kprobes-do-sys-open.kp create mode 100644 tools/ktap/samples/io/traceio.kp create mode 100644 tools/ktap/samples/mem/kmalloc-stack.kp create mode 100644 tools/ktap/samples/mem/kmem_count.kp create mode 100644 tools/ktap/samples/network/tcp_ipaddr.kp create mode 100644 tools/ktap/samples/profiling/function_profiler.kp create mode 100644 tools/ktap/samples/profiling/kprobe_all_kernel_functions.kp create mode 100644 tools/ktap/samples/profiling/stack_profile.kp create mode 100644 tools/ktap/samples/schedule/sched_transition.kp create mode 100644 tools/ktap/samples/schedule/schedtimes.kp create mode 100644 tools/ktap/samples/syscalls/errinfo.kp create mode 100644 tools/ktap/samples/syscalls/execve.kp create mode 100644 tools/ktap/samples/syscalls/opensnoop.kp create mode 100644 tools/ktap/samples/syscalls/sctop.kp create mode 100644 tools/ktap/samples/syscalls/syscalls.kp create mode 100644 tools/ktap/samples/syscalls/syscalls_count.kp create mode 100644 tools/ktap/samples/syscalls/syscalls_count_by_proc.kp create mode 100644 tools/ktap/samples/syscalls/syslatl.kp create mode 100644 tools/ktap/samples/syscalls/syslist.kp create mode 100644 tools/ktap/samples/tracepoints/eventcount.kp create mode 100644 tools/ktap/samples/tracepoints/eventcount_by_proc.kp create mode 100644 tools/ktap/samples/tracepoints/raw_tracepoint.kp create mode 100644 tools/ktap/samples/tracepoints/tracepoints.kp create mode 100644 tools/ktap/samples/userspace/gcc_unwind.kp create mode 100644 tools/ktap/samples/userspace/glibc_func_hist.kp create mode 100644 tools/ktap/samples/userspace/glibc_sdt.kp create mode 100644 tools/ktap/samples/userspace/glibc_trace.kp create mode 100644 tools/ktap/samples/userspace/malloc_free.kp create mode 100644 tools/ktap/samples/userspace/malloc_size_hist.kp create mode 100644 tools/ktap/samples/userspace/pthread.kp create mode 100644 tools/ktap/test/README create mode 100644 tools/ktap/test/arithmetic.t create mode 100644 tools/ktap/test/benchmark/cmp_neq.sh create mode 100644 tools/ktap/test/benchmark/cmp_profile.sh create mode 100644 tools/ktap/test/benchmark/cmp_table.sh create mode 100644 tools/ktap/test/benchmark/sembench.c create mode 100644 tools/ktap/test/benchmark/test.sh create mode 100644 tools/ktap/test/cli-arg.t create mode 100644 tools/ktap/test/concat.t create mode 100644 tools/ktap/test/count.t create mode 100644 tools/ktap/test/deadloop.t create mode 100644 tools/ktap/test/fibonacci.t create mode 100644 tools/ktap/test/function.t create mode 100644 tools/ktap/test/if.t create mode 100644 tools/ktap/test/kprobe.t create mode 100644 tools/ktap/test/kretprobe.t create mode 100644 tools/ktap/test/len.t create mode 100644 tools/ktap/test/lib/Test/ktap.pm create mode 100644 tools/ktap/test/looping.t create mode 100644 tools/ktap/test/one-liner.t create mode 100644 tools/ktap/test/pairs.t create mode 100644 tools/ktap/test/stack_overflow.t create mode 100644 tools/ktap/test/syntax-err.t create mode 100644 tools/ktap/test/table.t create mode 100644 tools/ktap/test/time.t create mode 100644 tools/ktap/test/timer.t create mode 100644 tools/ktap/test/tracepoint.t create mode 100755 tools/ktap/test/util/reindex create mode 100644 tools/ktap/test/zerodivide.t create mode 100644 tools/ktap/userspace/kp_bcwrite.c create mode 100644 tools/ktap/userspace/kp_lex.c create mode 100644 tools/ktap/userspace/kp_lex.h create mode 100644 tools/ktap/userspace/kp_main.c create mode 100644 tools/ktap/userspace/kp_parse.c create mode 100644 tools/ktap/userspace/kp_parse.h create mode 100644 tools/ktap/userspace/kp_parse_events.c create mode 100644 tools/ktap/userspace/kp_reader.c create mode 100644 tools/ktap/userspace/kp_symbol.c create mode 100644 tools/ktap/userspace/kp_symbol.h create mode 100644 tools/ktap/userspace/kp_util.c create mode 100644 tools/ktap/userspace/kp_util.h create mode 100644 tools/ktap/vim/ftdetect/ktap.vim create mode 100644 tools/ktap/vim/syntax/ktap.vim
Signature · 1 line
1.8.3.1
PATCH 1/28 ktap: add README file
--- tools/ktap/README.md | 149 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 tools/ktap/README.md
tools/ktap/README.md new file +149 -0
@@ -0,0 +1,149 @@ +# ktap + +A New Scripting Dynamic Tracing Tool For Linux +[www.ktap.org][homepage] + +ktap is a new scripting dynamic tracing tool for Linux, +it uses a scripting language and lets users trace the Linux kernel dynamically. +ktap is designed to give operational insights with interoperability +that allows users to tune, troubleshoot and extend the kernel and applications. +It's similar to Linux Systemtap and Solaris Dtrace. + +ktap has different design principles from Linux mainstream dynamic tracing +language in that it's based on bytecode, so it doesn't depend upon GCC, +doesn't require compiling kernel module for each script, safe to use in +production environment, fulfilling the embedded ecosystem's tracing needs. + +More information can be found at [ktap homepage][homepage]. + +[homepage]: http://www.ktap.org + +## Highlights + +* a simple but powerful scripting language +* register based interpreter (heavily optimized) in Linux kernel +* small and lightweight +* not depend on the gcc toolchain for each script run +* easy to use in embedded environments without debugging info +* support for tracepoint, kprobe, uprobe, function trace, timer, and more +* supported in x86, arm, ppc, mips +* safety in sandbox + + +## Building & Running + +1. Clone ktap from github + + $ git clone http://github.com/ktap/ktap.git +2. Compiling ktap + + $ cd ktap + $ make #generate ktapvm kernel module and ktap binary +3. Load ktapvm kernel module(make sure debugfs mounted) + + $ make load #need to be root or have sudo access +4. Running ktap + + $ ./ktap samples/helloworld.kp + + +## Examples + +1. simplest one-liner command to enable all tracepoints + + ktap -e "trace *:* { print(argstr) }" +2. syscall tracing on target process + + ktap -e "trace syscalls:* { print(argstr) }" -- ls +3. ftrace(kernel newer than 3.3, and must compiled with CONFIG_FUNCTION_TRACER) + + ktap -e "trace ftrace:function { print(argstr) }" + + ktap -e "trace ftrace:function /ip==mutex*/ { print(argstr) }" +4. simple syscall tracing + + trace syscalls:* { + print(cpu, pid, execname, argstr) + } +5. syscall tracing in histogram style + + var s = {} + + trace syscalls:sys_enter_* { + s[probename] += 1 + } + + trace_end { + print_hist(s) + } +6. kprobe tracing + + trace probe:do_sys_open dfd=%di fname=%dx flags=%cx mode=+4($stack) { + print("entry:", execname, argstr) + } + + trace probe:do_sys_open%return fd=$retval { + print("exit:", execname, argstr) + } +7. uprobe tracing + + trace probe:/lib/libc.so.6:malloc { + print("entry:", execname, argstr) + } + + trace probe:/lib/libc.so.6:malloc%return { + print("exit:", execname, argstr) + } +8. stapsdt tracing (userspace static marker) + + trace sdt:/lib64/libc.so.6:lll_futex_wake { + print("lll_futex_wake", execname, argstr) + } + + or: + + #trace all static mark in libc + trace sdt:/lib64/libc.so.6:* { + print(execname, argstr) + } +9. timer + + tick-1ms { + printf("time fired on one cpu\n"); + } + + profile-2s { + printf("time fired on every cpu\n"); + } + +More examples can be found at [samples][samples_dir] directory. + +[samples_dir]: https://github.com/ktap/ktap/tree/master/samples + +## Mailing list + [email protected] +You can subscribe to ktap mailing list at link (subscribe before posting): +http://www.freelists.org/list/ktap + + +## Copyright and License + +ktap is licensed under GPL v2 + +Copyright (C) 2012-2014, Jovi Zhangwei <[email protected]>. +All rights reserved. + + +## Contribution + +ktap is still under active development, so contributions are welcome. +You are encouraged to report bugs, provide feedback, send feature request, +or hack on it. + + +## See More + +More info can be found at [documentation][tutorial] +[tutorial]: http://www.ktap.org/doc/tutorial.html +
Signature · 1 line
1.8.1.4
PATCH 2/28 ktap: add ktap tutorial
In reply to Jovi ZhangweiThis is detail documentation for ktap users, it contains:
- Basic introduction
- Requirements
- Language Syntax basics
- Built-in functions, libraries and variables
- Simple samples
- Design decision
- References
This tutorial is still keeping update, it would be a good guide for ktap.
--- tools/ktap/doc/tutorial.md | 666 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 666 insertions(+) create mode 100644 tools/ktap/doc/tutorial.md
tools/ktap/doc/tutorial.md new file +666 -0
@@ -0,0 +1,666 @@ +% The ktap Tutorial + +# Introduction + +ktap is a new script-based dynamic tracing tool for Linux +http://www.ktap.org + +ktap is a new script-based dynamic tracing tool for Linux. +It uses a scripting language and lets the user trace the Linux kernel dynamically. +ktap is designed to give operational insights with interoperability +that allows users to tune, troubleshoot and extend kernel and application. +It's similar to Linux SystemTap and Solaris DTrace. + +ktap has different design principles from Linux mainstream dynamic tracing +language in that it's based on bytecode, so it doesn't depend upon GCC, +doesn't require compiling a kernel module for each script, safe to use in +production environment, fulfilling the embedded ecosystem's tracing needs. + +Highlights features: + +* a simple but powerful scripting language +* register-based interpreter (heavily optimized) in Linux kernel +* small and lightweight +* not depend on the GCC toolchain for each script run +* easy to use in embedded environments without debugging info +* support for tracepoint, kprobe, uprobe, function trace, timer, and more +* supported in x86, ARM, PowerPC, MIPS +* safety in sandbox + +# Getting started + +Requirements + +* Linux 3.1 or later (patches are required for earlier versions) +* `CONFIG_EVENT_TRACING` enabled +* `CONFIG_PERF_EVENTS` enabled +* `CONFIG_DEBUG_FS` enabled + + make sure debugfs mounted before `insmod ktapvm` + + mount debugfs: `mount -t debugfs none /sys/kernel/debug/` +* libelf (optional) + Install elfutils-libelf-devel on RHEL-based distros, or libelf-dev on + Debian-based distros. + Use `make NO_LIBELF=1` to build without libelf support. + libelf is required for resolving symbols to addresses in DSO, and for SDT. + +Note that those configurations should always be enabled in Linux distribution, +like RHEL, Fedora, Ubuntu, etc. + +1. Clone ktap from GitHub + + $ git clone http://github.com/ktap/ktap.git +2. Compile ktap + + $ cd ktap + $ make #generate ktapvm kernel module and ktap binary +3. Load ktapvm kernel module(make sure debugfs mounted) + + $ make load #need to be root or have sudo access +4. Run ktap + + $ ./ktap samples/helloworld.kp + + +# Language basics + +## Syntax basics + +ktap's syntax is designed with the C language syntax in mind. This is for lowering the entry barrier for C programmers who are working on the kernel or other systems software. + +* Variable declarations + + The biggest syntax differences with C is that ktap is a dynamically-typed language, so you won't need add any variable type declaration, just use the variable. +* Functions + + All functions in ktap should use keyword "function" declaration +* Comments + + Comments in ktap start with `#`. Long comments are not supported right now. +* Others + + Semicolons (`;`) are not required at the end of statements in ktap. ktap uses a free-syntax style, so you are free to use ';' or not. + +ktap uses `nil` as `NULL`. The result of an arithmetic operation on `nil` is also `nil`. + +ktap does not have array structures, and it does not have any pointer operations. + +## Control structures + +ktap's `if`/`else` statement is the same as the C language's. + +There are three kinds of for-loop in ktap: + +1. a kinda Lua-ish style: + + for (i = init, limit, step) { body } +2. the same form as in C: + + for (i = init; i < limit; i += step) { body } +3. Lua's table iterating style: + + for (k, v in pairs(t)) { body } # looping all elements of table + +Note that ktap does not have the `continue` keyword, but C does. + +## Data structures + +Associative arrays are heavily used in ktap; they are also called "tables". + +Table declarations: + + t = {} + +How to use tables: + + t[1] = 1 + t[1] = "xxx" + t["key"] = 10 + t["key"] = "value" + + for (k, v in pairs(t)) { body } # looping all elements of table + +# Built-in functions and libraries + +## Built-in functions + +**print (...)** + +Receives any number of arguments, and prints their values. print is not intended for formatted output, but only as a quick way to show values, typically for debugging. + +For formatted output, use `printf` instead. + +**printf (fmt, ...)** + +Similar to C's `printf`, for formatted string output. + +**pairs (t)** + +Returns three values: the next function, the table t, and nil, so that the construction + + for (k, v in pairs(t)) { body } + +will iterate through all the key-value pairs in the table `t`. + +**len (t) /len (s)** + +If the argument is a string, returns the length of the string. + +If the argument is a table, returns the number of table pairs. + +**in_interrupt ()** + +Checks if it is in the context of interrupts. + +**exit ()** + +quits ktap programs, similar to the `exit` syscall. + +**arch ()** + +returns machine architecture, like `x86`, `arm`, and etc. + +**kernel_v ()** + +returns Linux kernel version string, like `3.9` and etc. + +**user_string (addr)** + +accepts a userspace address, reads the string data from userspace, and returns the ktap string value. + +**print_hist (t)** + +accepts a table and outputs the table histogram to the user. + + +## Libraries + +### Kdebug Library + +**kdebug.trace_by_id (eventdef_info, eventfun)** + +This function is the underlying interface for the higher level tracing primitives. + +Note that the `eventdef_info` argument is just a C pointer value pointing to a userspace memory block holding the real `eventdef_info` structure. The structure definition is as follows: + + struct ktap_eventdesc { + int nr; /* the number to id */ + int *id_arr; /* id array */ + char *filter; + }; + +Those `id`s are read from `/sys/kernel/debug/tracing/events/$SYS/$EVENT/id`. + +The second argument in above example is a ktap function object: + + function eventfun () { action } + +**kdebug.trace_end (endfunc)** + +This function is used for invoking a function when tracing ends, it will wait until the user presses `CTRL-C` to stop tracing, then ktap will call the argument, the `endfunc` function. The user could output tracing results in that function, or do other things. + +User usually do not need to use the `kdebug` library directly and just use the `trace`/`trace_end` keywords provided by the language. + +### Timer Library + +### Table Library + +**table.new (narr, nrec)** + +pre-allocates a table with `narr` array entries and `nrec` records. + +# Linux tracing basics + +tracepoints, probe, timer, filters, ring buffer + +# Tracing semantics in ktap + +## Tracing block + +**trace EVENTDEF /FILTER/ { ACTION }** + +This is the basic tracing block in ktap. You need to use a specific `EVENTDEF` string, and your own event function. + +There are four types of `EVENTDEF`: tracepoints, kprobes, uprobes, SDT probes. + +- tracepoint: + + EventDef Description + -------------------- ------------------------------- + syscalls:* trace all syscalls events + syscalls:sys_enter_* trace all syscalls entry events + kmem:* trace all kmem related events + sched:* trace all sched related events + sched:sched_switch trace sched_switch tracepoint + \*:\* trace all tracepoints in system + + All tracepoint events are based on + + /sys/kernel/debug/tracing/events/$SYS/$EVENT + +- ftrace (kernel 3.3+, and must be compiled with `CONFIG_FUNCTION_TRACER`) + + EventDef Description + -------------------- ------------------------------- + ftrace:function trace kernel functions based on ftrace + + User need to use filter (/ip==*/) to trace specific functions. + Function must be listed in /sys/kernel/debug/tracing/available_filter_functions + +> ***Note*** of function event +> +> perf support ftrace:function tracepoint since Linux 3.3 (see below commit), +> ktap is based on perf callback, so it means kernel must be newer than 3.3 +> then can use this feature. +> +> commit ced39002f5ea736b716ae233fb68b26d59783912 +> Author: Jiri Olsa <[email protected]> +> Date: Wed Feb 15 15:51:52 2012 +0100 +> +> ftrace, perf: Add support to use function tracepoint in perf +> + +- kprobe: + + EventDef Description + -------------------- ----------------------------------- + probe:schedule trace schedule function + probe:schedule%return trace schedule function return + probe:SyS_write trace SyS_write function + probe:vfs* trace wildcards vfs related function + + kprobe functions must be listed in /proc/kallsyms +- uprobe: + + EventDef Description + ------------------------------------ --------------------------- + probe:/lib64/libc.so.6:malloc trace malloc function + probe:/lib64/libc.so.6:malloc%return trace malloc function return + probe:/lib64/libc.so.6:free trace free function + probe:/lib64/libc.so.6:0x82000 trace function with file offset 0x82000 + probe:/lib64/libc.so.6:* trace all libc function + + symbol resolving need libelf support + +- sdt: + + EventDef Description + ------------------------------------ -------------------------- + sdt:/libc64/libc.so.6:lll_futex_wake trace stapsdt lll_futex_wake + sdt:/libc64/libc.so.6:* trace all static markers in libc + + sdt resolving need libelf support + + +**trace_end { ACTION }** + +## Tracing Built-in variables + +**arg0..9** + +Evaluates to argument 0 to 9 of the event object. If fewer than ten arguments are passed to the current probe, the remaining variables return nil. + +> ***Note*** of arg offset +> +> The arg offset(0..9) is determined by event format shown in debugfs. +> +> #cat /sys/kernel/debug/tracing/events/sched/sched_switch/format +> name: sched_switch +> ID: 268 +> format: +> field:char prev_comm[32]; <- arg0 +> field:pid_t prev_pid; <- arg1 +> field:int prev_prio; <- arg2 +> field:long prev_state; <- arg3 +> field:char next_comm[32]; <- arg4 +> field:pid_t next_pid; <- arg5 +> field:int next_prio; <- arg6 +> +> As shown above, the tracepoint event `sched:sched_switch` takes 7 arguments, from `arg0` to `arg6`. +> +> For syscall event, `arg0` is the syscall number, not the first argument of the syscall function. Use `arg1` as the first argument of the syscall function. +> For example: +> +> SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count) +> <arg1> <arg2> <arg3> +> +> This is similar to kprobe and uprobe, the `arg0` of kprobe/uprobe events +> is always `_probe_ip`, not the first argument given by the user, for example: +> +> # ktap -e 'trace probe:/lib64/libc.so.6:malloc size=%di' +> +> # cat /sys/kernel/debug/tracing/events/ktap_uprobes_3796/malloc/format +> field:unsigned long __probe_ip; <- arg0 +> field:u64 size; <- arg1 + + +**cpu** + +returns the current CPU id. + +**pid** + +returns current process pid. + +**tid** + +returns the current thread id. + +**uid** + +returns the current process's uid. + +**execname** + +returns the current process executable's name in a string. + +**argstr** + +Event string representation. You can print it by `print(argstr)`, turning the +event into a human readable string. The result is mostly the same as each +entry in `/sys/kernel/debug/tracing/trace` + +**probename** + +Event name. Each event has a name associated with it. +(Dtrace also have 'probename' keyword) + +## Timer syntax + +**tick-Ns { ACTION }** + +**tick-Nsec { ACTION }** + +**tick-Nms { ACTION }** + +**tick-Nmsec { ACTION }** + +**tick-Nus { ACTION }** + +**tick-Nusec { ACTION }** + +**profile-Ns { ACTION }** + +**profile-Nsec { ACTION }** + +**profile-Nms { ACTION }** + +**profile-Nmsec { ACTION }** + +**profile-Nus { ACTION }** + +**profile-Nusec { ACTION }** + +architecture overview picture reference(pnp format) + +one-liners + +simple event tracing + +# Advanced tracing pattern + +* Aggregations/histograms +* Thread locals +* Flame graphs + +# Overhead/Performance + +* ktap has a much shorter startup time than SystemTap (try the helloword script). +* ktap has a smaller memory footprint than SystemTap +* Some scripts show that ktap has a little lower overhead than SystemTap +(See more performance comparison between ktap and stap in test/benchmark/. + stap wins on number computation, and ktap wins on table operation; + Normally ktap have little memory consuming than stap) + +# FAQ + +**Q: Why use a bytecode design?** + +A: Using bytecode is a clean and lightweight solution, + you do not need the GCC toolchain to compile every script; all you + need is a ktapvm kernel module and the userspace tool called "ktap". + Since its language uses a virtual machine design, it has a great portability. + Suppose you are working on a multi-arch cluster; if you want to run + a tracing script on each board, you will not need cross-compile your tracing + scripts for all the boards. You can just use the `ktap` tool + to run scripts right away. + + The bytecode-based design also makes execution safer than the native code + generation approach. + + It is already observed that SystemTap is not widely used in embedded Linux systems. This is mainly caused by the problem of SystemTap's design decisions in its architecture design. It is a natural design for Red Hat and IBM, because Red Hat/IBM is focusing on the server area, not embedded area. + +**Q: What's the differences with SystemTap and DTrace?** + +A: For SystemTap, the answer is already mentioned in the above question, + SystemTap chooses the translator design, sacrificing usability for runtime performance. + The dependency on the GCC chain when running scripts is the problem that ktap wants to solve. + + DTrace shares the same design decision of using bytecode, so basically + DTrace and ktap are more alike. There have been some projects aimed at porting + DTrace from Solaris to Linux, but these efforts are still under way and are relatively slow in progress. DTrace + has its root in Solaris, and there are many huge differences between Solaris's + tracing infrastructure and Linux's. + + DTrace is based on D language, a language subset of C. It's a restricted + language, like without for-looping, for safe use in production systems. + It seems that DTrace for Linux only supports x86 architecture, doesn't work on + PowerPC and ARM/MIPS. Obviously it's not suited for embedded Linux currently. + + DTrace uses CTF as input for debuginfo handing, compared to vmlinux for + SystemTap. + + On the license part, DTrace is released as CDDL, which is incompatible with + GPL. (This is why it's impossible to upstream DTrace into mainline.) + +**Q: Why use a dynamically-typed language instead of a statically-typed language?** + +A: It's hard to say which one is better than the other. Dynamically-typed + languages bring efficiency and fast prototype production, but lose type + checking at the compile phase, and it's easy to make mistake in runtime. It also needs many runtime checks. In contrast, statically-typed languages win on programming safety and performance. Statically-typed languages would suit for interoperation with the kernel, as the kernel is written mainly in C. Note that SystemTap and DTrace both use statically-typed languages. + + ktap chooses a dynamically-typed language for its initial implementation. + +**Q: Why do we need ktap for event tracing? There is already a built-in ftrace** + +A: This is also a common question for all dynamic tracing tools, not only ktap. + ktap provides more flexibility than the built-in tracing infrastructure. Suppose you need to print a global variable at a tracepoint hit, or you want to print a backtrace. Furthermore, you want to store some info into an associative array, and display it as a histogram when tracing ends. `ftrace` cannot handle all these requirements. Overall, ktap provides you with great flexibility to script your own trace needs. + +**Q: How about the performance? Is ktap slow?** + +A: ktap is not slow. The bytecode is very high-level, based on Luajit. The language's virtual machine is register-based (compared to the stack-based JVM and CLR), with a small number of instructions. The table data structure is heavily optimized in ktapvm. ktap uses per-cpu allocation in many places, without the global locking scheme. It is very fast when executing tracepoint callbacks. Performance benchmarks show that the overhead of associative array operation is smaller than Systemtap. + + ktap will keep optimizing unfailingly. + +**Q: Why not port a higher-level language, like Python or Java, directly into the kernel?** + +A: I am serious on the size of VM and the memory footprint. The Python VM is too large for embedding into the kernel, and Python has many advanced functionalities which we do not really need. + + There are also some problems when porting those languages into the kernel. Kernel programming is very different from userspace programming, like lack of floating-point numbers, handling sleeping code, deadloop is not allowed in the kernel, multi-thread management, etc. So it is impossible to port large language implementations over to the kernel environment with trivial efforts. + +**Q: What is the status of ktap now?** + +A: Basically it works on x86-32, x86-64, PowerPC, ARM. It also could work for + other hardware architectures, but is not tested yet. (I don't have enough hardware to test.) + If you find any bugs, fix it with your own programming skills, or just report to me. + +**Q: How can I hack on ktap? I want to write some extensions for ktap.** + +A: Patches welcome! Volunteers welcome! + You can write your own libraries to fulfill your specific needs, + or write scripts for fun. + +**Q: What's the plan for ktap? Is there a roadmap?** + +A: The current plan is to deliver stable ktapvm kernel modules, more ktap scripts, and more bugfixes. + +# References + +* [Linux Performance Analysis and Tools][REF1] +* [Dtrace Blog][REF2] +* [Dtrace User Guide][REF3] +* [LWN: ktap -- yet another kernel tracer][REF4] +* [LWN: Ktap almost gets into 3.13][REF5] +* [staging: ktap: add to the kernel tree][REF6] +* [ktap introduction in LinuxCon Japan 2013][REFR7(content is out of date) +* [ktap Examples by Brendan Gregg][REFR8 +* [What Linux can learn from Solaris performance, and vice-versa][REF9] + +[REF1]: http://www.brendangregg.com/Slides/SCaLE_Linux_Performance2013.pdf +[REF2]: http://dtrace.org/blogs/ +[REF3]: http://docs.huihoo.com/opensolaris/dtrace-user-guide/html/index.html +[REF4]: http://lwn.net/Articles/551314/ +[REF5]: http://lwn.net/Articles/572788/ +[REF6]: https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=c63a164271f81220ff4966d41218a9101f3d0ec4 +[REF7]: http://events.linuxfoundation.org/sites/events/files/lcjpcojp13_zhangwei.pdf +[REF8]: http://www.brendangregg.com/ktap.html +[REF9]: http://www.slideshare.net/brendangregg/what-linux-can-learn-from-solaris-performance-and-viceversa + +# History + +* ktap was invented at 2012 +* First RFC sent to LKML at 2012.12.31 +* The code was released in GitHub at 2013.01.18 +* ktap released v0.1 at 2013.05.21 +* ktap released v0.2 at 2013.07.31 +* ktap released v0.3 at 2013.10.29 +* ktap released v0.4 at 2013.12.09 + +For more release info, please look at RELEASES.txt in project root directory. + +# Examples + +1. simplest one-liner command to enable all tracepoints + + ktap -e "trace *:* { print(argstr) }" +2. syscall tracing on target process + + ktap -e "trace syscalls:* { print(argstr) }" -- ls +3. ftrace(kernel newer than 3.3, and must compiled with CONFIG_FUNCTION_TRACER) + + ktap -e "trace ftrace:function { print(argstr) }" + + ktap -e "trace ftrace:function /ip==mutex*/ { print(argstr) }" +4. simple syscall tracing + + trace syscalls:* { + print(cpu, pid, execname, argstr) + } +5. syscall tracing in histogram style + + var s = {} + + trace syscalls:sys_enter_* { + s[probename] += 1 + } + + trace_end { + print_hist(s) + } +6. kprobe tracing + + trace probe:do_sys_open dfd=%di fname=%dx flags=%cx mode=+4($stack) { + print("entry:", execname, argstr) + } + + trace probe:do_sys_open%return fd=$retval { + print("exit:", execname, argstr) + } +7. uprobe tracing + + trace probe:/lib/libc.so.6:malloc { + print("entry:", execname, argstr) + } + + trace probe:/lib/libc.so.6:malloc%return { + print("exit:", execname, argstr) + } +8. stapsdt tracing (userspace static marker) + + trace sdt:/lib64/libc.so.6:lll_futex_wake { + print("lll_futex_wake", execname, argstr) + } + + or: + + #trace all static mark in libc + trace sdt:/lib64/libc.so.6:* { + print(execname, argstr) + } +9. timer + + tick-1ms { + printf("time fired on one cpu\n"); + } + + profile-2s { + printf("time fired on every cpu\n"); + } + +More examples can be found at [samples][samples_dir] directory. + +[samples_dir]: https://github.com/ktap/ktap/tree/master/samples + +# Appendix + +Here is the complete syntax of ktap in extended BNF. +(based on Lua syntax: http://www.lua.org/manual/5.1/manual.html#5.1) + + chunk ::= {stat [';']} [laststat [';'] + + block ::= chunk + + stat ::= varlist '=' explist | + functioncall | + { block } | + while exp { block } | + repeat block until exp | + if exp { block {elseif exp { block }} [else block] } | + for Name '=' exp ',' exp [',' exp] { block } | + for namelist in explist { block } | + function funcname funcbody | + function Name funcbody | + var namelist ['=' explist] + + laststat ::= return [explist] | break + + funcname ::= Name {'.' Name} [':' Name] + + varlist ::= var {',' var} + + var ::= Name | prefixexp '[' exp ']'| prefixexp '.' Name + + namelist ::= Name {',' Name} + + explist ::= {exp ',' exp + + exp ::= nil | false | true | Number | String | '...' | function | + prefixexp | tableconstructor | exp binop exp | unop exp + + prefixexp ::= var | functioncall | '(' exp ')' + + functioncall ::= prefixexp args | prefixexp ':' Name args + + args ::= '(' [explist] ')' | tableconstructor | String + + function ::= function funcbody + + funcbody ::= '(' [parlist] ')' { block } + + parlist ::= namelist [',' '...'] | '...' + + tableconstructor ::= '{' [fieldlist] '}' + + fieldlist ::= field {fieldsep field} [fieldsep] + + field ::= '[' exp ']' '=' exp | Name '=' exp | exp + + fieldsep ::= ',' | ';' + + binop ::= '+' | '-' | '*' | '/' | '^' | '%' | '..' | + '<' | '<=' | '>' | '>=' | '==' | '!=' | + and | or + + unop ::= '-' +
Signature · 1 line
1.8.1.4
PATCH 3/28 ktap: add sample scripts
In reply to Jovi ZhangweiThe samples directory is organized by different subsystems, similar with Dtrace toolkit(http://www.brendangregg.com/dtracetoolkit.html)
It contains:
- helloworld.kp: simple hello world program
- ansi: ansi library for screen display
- basic: some simple examples
- game: tetris game wrote by ktap
- interrupt: collect hardirq and softirq time
- io: open and io related scripts
- mem: kernel memory allocation scripts
- network: networking scripts
- profiling: stack profile and function profile
- schedule: schedule scripts.
schedtimes.kp was inspired by Systemtap schedtimes.stp
- syscalls: tracing syscalls
syslatl.kp/opensnoop.kp/syslist.kp was contributed by Brendan Gregg(from Dtrace toolkit)
- tracepoints: kernel tracepoint count and histogram
- userspace: uprobe(include SDT) scripts
Besides these samples, "ktap Examples" posted by Brendan Gregg
(http://www.brendangregg.com/ktap.html) also have many sample scripts, he mentioned how to can make flame graph by ktap.
--- tools/ktap/samples/ansi/ansi_color_demo.kp | 22 ++ tools/ktap/samples/basic/backtrace.kp | 6 + tools/ktap/samples/basic/event_trigger.kp | 27 ++ tools/ktap/samples/basic/event_trigger_ftrace.kp | 27 ++ tools/ktap/samples/basic/ftrace.kp | 8 + tools/ktap/samples/basic/function_time.kp | 62 +++++ tools/ktap/samples/basic/kretprobe.kp | 6 + tools/ktap/samples/basic/memcpy_memset.kp | 23 ++ tools/ktap/samples/game/tetris.kp | 297 +++++++++++++++++++++ tools/ktap/samples/helloworld.kp | 3 + tools/ktap/samples/interrupt/hardirq_time.kp | 25 ++ tools/ktap/samples/interrupt/softirq_time.kp | 24 ++ tools/ktap/samples/io/kprobes-do-sys-open.kp | 20 ++ tools/ktap/samples/io/traceio.kp | 61 +++++ tools/ktap/samples/mem/kmalloc-stack.kp | 12 + tools/ktap/samples/mem/kmem_count.kp | 29 ++ tools/ktap/samples/network/tcp_ipaddr.kp | 20 ++ tools/ktap/samples/profiling/function_profiler.kp | 41 +++ .../profiling/kprobe_all_kernel_functions.kp | 13 + tools/ktap/samples/profiling/stack_profile.kp | 27 ++ tools/ktap/samples/schedule/sched_transition.kp | 5 + tools/ktap/samples/schedule/schedtimes.kp | 131 +++++++++ tools/ktap/samples/syscalls/errinfo.kp | 145 ++++++++++ tools/ktap/samples/syscalls/execve.kp | 9 + tools/ktap/samples/syscalls/opensnoop.kp | 31 +++ tools/ktap/samples/syscalls/sctop.kp | 13 + tools/ktap/samples/syscalls/syscalls.kp | 6 + tools/ktap/samples/syscalls/syscalls_count.kp | 54 ++++ .../samples/syscalls/syscalls_count_by_proc.kp | 22 ++ tools/ktap/samples/syscalls/syslatl.kp | 33 +++ tools/ktap/samples/syscalls/syslist.kp | 31 +++ tools/ktap/samples/tracepoints/eventcount.kp | 210 +++++++++++++++ .../ktap/samples/tracepoints/eventcount_by_proc.kp | 57 ++++ tools/ktap/samples/tracepoints/raw_tracepoint.kp | 15 ++ tools/ktap/samples/tracepoints/tracepoints.kp | 6 + tools/ktap/samples/userspace/gcc_unwind.kp | 9 + tools/ktap/samples/userspace/glibc_func_hist.kp | 44 +++ tools/ktap/samples/userspace/glibc_sdt.kp | 11 + tools/ktap/samples/userspace/glibc_trace.kp | 11 + tools/ktap/samples/userspace/malloc_free.kp | 20 ++ tools/ktap/samples/userspace/malloc_size_hist.kp | 22 ++ tools/ktap/samples/userspace/pthread.kp | 8 + 42 files changed, 1646 insertions(+) create mode 100644 tools/ktap/samples/ansi/ansi_color_demo.kp create mode 100644 tools/ktap/samples/basic/backtrace.kp create mode 100644 tools/ktap/samples/basic/event_trigger.kp create mode 100644 tools/ktap/samples/basic/event_trigger_ftrace.kp create mode 100644 tools/ktap/samples/basic/ftrace.kp create mode 100644 tools/ktap/samples/basic/function_time.kp create mode 100644 tools/ktap/samples/basic/kretprobe.kp create mode 100644 tools/ktap/samples/basic/memcpy_memset.kp create mode 100644 tools/ktap/samples/game/tetris.kp create mode 100644 tools/ktap/samples/helloworld.kp create mode 100644 tools/ktap/samples/interrupt/hardirq_time.kp create mode 100644 tools/ktap/samples/interrupt/softirq_time.kp create mode 100644 tools/ktap/samples/io/kprobes-do-sys-open.kp create mode 100644 tools/ktap/samples/io/traceio.kp create mode 100644 tools/ktap/samples/mem/kmalloc-stack.kp create mode 100644 tools/ktap/samples/mem/kmem_count.kp create mode 100644 tools/ktap/samples/network/tcp_ipaddr.kp create mode 100644 tools/ktap/samples/profiling/function_profiler.kp create mode 100644 tools/ktap/samples/profiling/kprobe_all_kernel_functions.kp create mode 100644 tools/ktap/samples/profiling/stack_profile.kp create mode 100644 tools/ktap/samples/schedule/sched_transition.kp create mode 100644 tools/ktap/samples/schedule/schedtimes.kp create mode 100644 tools/ktap/samples/syscalls/errinfo.kp create mode 100644 tools/ktap/samples/syscalls/execve.kp create mode 100644 tools/ktap/samples/syscalls/opensnoop.kp create mode 100644 tools/ktap/samples/syscalls/sctop.kp create mode 100644 tools/ktap/samples/syscalls/syscalls.kp create mode 100644 tools/ktap/samples/syscalls/syscalls_count.kp create mode 100644 tools/ktap/samples/syscalls/syscalls_count_by_proc.kp create mode 100644 tools/ktap/samples/syscalls/syslatl.kp create mode 100644 tools/ktap/samples/syscalls/syslist.kp create mode 100644 tools/ktap/samples/tracepoints/eventcount.kp create mode 100644 tools/ktap/samples/tracepoints/eventcount_by_proc.kp create mode 100644 tools/ktap/samples/tracepoints/raw_tracepoint.kp create mode 100644 tools/ktap/samples/tracepoints/tracepoints.kp create mode 100644 tools/ktap/samples/userspace/gcc_unwind.kp create mode 100644 tools/ktap/samples/userspace/glibc_func_hist.kp create mode 100644 tools/ktap/samples/userspace/glibc_sdt.kp create mode 100644 tools/ktap/samples/userspace/glibc_trace.kp create mode 100644 tools/ktap/samples/userspace/malloc_free.kp create mode 100644 tools/ktap/samples/userspace/malloc_size_hist.kp create mode 100644 tools/ktap/samples/userspace/pthread.kp
tools/ktap/samples/ansi/ansi_color_demo.kp new file +22 -0
@@ -0,0 +1,22 @@ +#!/usr/bin/env ktap + +#this script demonstrate how to use ktap to output color text. + +ansi.clear_screen() + +ansi.set_color(32) +printf("this line should be Green color\n") + +ansi.set_color(31) +printf("this line should be Red color\n") + +ansi.set_color2(34, 43) +printf("this line should be Blue color, with Yellow background\n") + +ansi.reset_color() +ansi.set_color3(34, 46, 4) +printf("this line should be Blue color, with Cyan background, underline single attribute\n") + +ansi.reset_color() +ansi.new_line() +
tools/ktap/samples/basic/backtrace.kp new file +6 -0
@@ -0,0 +1,6 @@ +#!/usr/bin/env ktap + +trace sched:sched_switch { + print(stack()) +} +
tools/ktap/samples/basic/event_trigger.kp new file +27 -0
@@ -0,0 +1,27 @@ +#!/usr/bin/env ktap + +#This ktap script will output all tracepoint events between +#sys_enter_open and sys_exit_open, in one cpu. + +var soft_disabled = 1 +var this_cpu = 0 + +trace syscalls:sys_enter_open { + print(argstr) + soft_disabled = 0 + this_cpu = cpu +} + +trace *:* { + if (soft_disabled == 0 && cpu == this_cpu) { + print(argstr) + } +} + +trace syscalls:sys_exit_open { + print(argstr) + if (cpu == this_cpu) { + exit() + } +} +
tools/ktap/samples/basic/event_trigger_ftrace.kp new file +27 -0
@@ -0,0 +1,27 @@ +#!/usr/bin/env ktap + +#This ktap script will output all function calling between +#sys_enter_open and sys_exit_open, in one cpu. + +var soft_disabled = 1 +var this_cpu = 0 + +trace syscalls:sys_enter_open { + print(argstr) + soft_disabled = 0 + this_cpu = cpu +} + +trace ftrace:function { + if (soft_disabled == 0 && cpu == this_cpu) { + print(argstr) + } +} + +trace syscalls:sys_exit_open { + print(argstr) + if (cpu == this_cpu) { + exit() + } +} +
tools/ktap/samples/basic/ftrace.kp new file +8 -0
@@ -0,0 +1,8 @@ +#!/usr/bin/env ktap + +#Description: output all mutex* function event + +trace ftrace:function /ip==mutex*/ { + print(cpu, pid, execname, argstr) +} +
tools/ktap/samples/basic/function_time.kp new file +62 -0
62 lines not on this page · raw message
tools/ktap/samples/basic/kretprobe.kp new file +6 -0
@@ -0,0 +1,6 @@ +#!/usr/bin/env ktap + +trace probe:vfs_read%return fd=$retval { + print(execname, argstr); +} +
tools/ktap/samples/basic/memcpy_memset.kp new file +23 -0
@@ -0,0 +1,23 @@ +#!/usr/bin/env ktap + +# This script collect kernel memcpy/memset size histgoram output. + +var h_memcpy = {} +var h_memset = {} + +trace probe:memcpy size=%dx { + h_memcpy[arg1] += 1 +} + +trace probe:memset size=%dx { + h_memset[arg1] += 1 +} + +trace_end { + print("memcpy size hist:") + print_hist(h_memcpy) + + print() + print("memset size hist:") + print_hist(h_memset) +}
tools/ktap/samples/game/tetris.kp new file +297 -0
297 lines not on this page · raw message
tools/ktap/samples/helloworld.kp new file +3 -0
@@ -0,0 +1,3 @@ +#!/usr/bin/env ktap + +print("Hello World! I am ktap")
tools/ktap/samples/interrupt/hardirq_time.kp new file +25 -0
@@ -0,0 +1,25 @@ +#!/usr/bin/env ktap + +#this script output each average consumimg time of each hardirq + +var s = {} +var map = {} + +trace irq:irq_handler_entry { + map[cpu] = gettimeofday_us() +} + +trace irq:irq_handler_exit { + var entry_time = map[cpu] + if (entry_time == nil) { + return; + } + + s[arg0] += gettimeofday_us() - entry_time + map[cpu] = nil +} + +trace_end { + print_hist(s) +} +
tools/ktap/samples/interrupt/softirq_time.kp new file +24 -0
@@ -0,0 +1,24 @@ +#!/usr/bin/env ktap + +#this script output each average consumimg time of each softirq line +var s = {} +var map = {} + +trace irq:softirq_entry { + map[cpu] = gettimeofday_us() +} + +trace irq:softirq_exit { + var entry_time = map[cpu] + if (entry_time == nil) { + return; + } + + s[arg0] += gettimeofday_us() - entry_time + map[cpu] = nil +} + +trace_end { + print_hist(s) +} +
tools/ktap/samples/io/kprobes-do-sys-open.kp new file +20 -0
@@ -0,0 +1,20 @@ +#!/usr/bin/env ktap + +#Only can run it in x86_64 +# +#Register follow x86_64 call conversion: +# +#x86_64: +# %rcx 4 argument +# %rdx 3 argument +# %rsi 2 argument +# %rdi 1 argument + +trace probe:do_sys_open dfd=%di filename=%si flags=%dx mode=%cx { + printf("[do_sys_open entry]: (%s) open file (%s)\n", + execname, user_string(arg2)) +} + +trace probe:do_sys_open%return fd=$retval { + printf("[do_sys_open exit]: return fd (%d)\n", arg2) +}
tools/ktap/samples/io/traceio.kp new file +61 -0
61 lines not on this page · raw message
tools/ktap/samples/mem/kmalloc-stack.kp new file +12 -0
@@ -0,0 +1,12 @@ +#!/usr/bin/env ktap + +var s = {} + +trace kmem:kmalloc { + s[stack()] += 1 +} + +tick-60s { + print_hist(s) +} +
tools/ktap/samples/mem/kmem_count.kp new file +29 -0
@@ -0,0 +1,29 @@ +#!/usr/bin/env ktap + +var count1 = 0 +trace kmem:kmalloc { + count1 += 1 +} + +var count2 = 0 +trace kmem:kfree { + count2 += 1 +} + +var count3 = 0 +trace kmem:mm_page_alloc { + count3 += 1 +} + +var count4 = 0 +trace kmem:mm_page_free { + count4 += 1 +} + +trace_end { + print("\n") + print("kmem:kmalloc:\t", count1) + print("kmem:kfree:\t", count2) + print("kmem:mm_page_alloc:", count3) + print("kmem:mm_page_free:", count4) +}
tools/ktap/samples/network/tcp_ipaddr.kp new file +20 -0
@@ -0,0 +1,20 @@ +#!/usr/bin/env ktap + +#This script print source and destination IP address of received tcp message +# +#Tested in x86_64 +# +#function tcp_recvmsg prototype: +#int tcp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, +# size_t len, int nonblock, int flags, int *addr_len) +# + +var ip_sock_saddr = net.ip_sock_saddr +var ip_sock_daddr = net.ip_sock_daddr +var format_ip_addr = net.format_ip_addr + +trace probe:tcp_recvmsg sock=%si { + var saddr = format_ip_addr(ip_sock_saddr(arg1)) + var daddr = format_ip_addr(ip_sock_daddr(arg1)) + printf("%s -> %s\n", daddr, saddr) +}
tools/ktap/samples/profiling/function_profiler.kp new file +41 -0
41 lines not on this page · raw message
tools/ktap/samples/profiling/kprobe_all_kernel_functions.kp new file +13 -0
@@ -0,0 +1,13 @@ +#!/usr/bin/env ktap + +#enable kprobe on all available kernel functios in /proc/kallsyms. +# +#This script is very dangerous, it will softlockup your system. +#and make your system extremely slow, but the reason is not +#caused by ktap. +# +#DON'T use this script, at least for now. + +trace probe:* { + print(argstr) +}
tools/ktap/samples/profiling/stack_profile.kp new file +27 -0
@@ -0,0 +1,27 @@ +#!/usr/bin/env ktap + +# This ktap script samples stacktrace of system per 10us, +# you can use generated output to make a flame graph. +# +# Flame Graphs: +# http://dtrace.org/blogs/brendan/2012/03/17/linux-kernel-performance-flame-graphs/ +# +# +# TODO: use aggregation instead of table. + +#pre-allocate 2000 record entries, enlarge it if it's not enough +var s = table.new(0, 2000) + +profile-10us { + #skip 12 stack entries, and dump all remain entries. + s[stack(-1, 12)] += 1 +} + +tick-60s { + exit() +} + +trace_end { + print_hist(s, 100000) +} +
tools/ktap/samples/schedule/sched_transition.kp new file +5 -0
@@ -0,0 +1,5 @@ +#!/usr/bin/env ktap + +trace sched:sched_switch { + printf("%s ... ", arg0) +}
tools/ktap/samples/schedule/schedtimes.kp new file +131 -0
131 lines not on this page · raw message
tools/ktap/samples/syscalls/errinfo.kp new file +145 -0
145 lines not on this page · raw message
tools/ktap/samples/syscalls/execve.kp new file +9 -0
@@ -0,0 +1,9 @@ +#!/usr/bin/env ktap + +#This script trace filename of process execution +#only tested in x86-64 + +trace probe:do_execve filename=%di { + printf("[do_execve entry]: (%s) name=%s\n", execname, + kernel_string(arg1)) +}
tools/ktap/samples/syscalls/opensnoop.kp new file +31 -0
@@ -0,0 +1,31 @@ +#!/usr/local/bin/ktap -q +# +# opensnoop.kp trace open syscalls with pathnames and basic info +# +# 23-Nov-2013 Brendan Gregg Created this + +var path = {} + +printf("%5s %6s %-12s %3s %3s %s\n", "UID", "PID", "COMM", "FD", "ERR", "PATH"); + +trace syscalls:sys_enter_open { + path[tid] = user_string(arg1) +} + +trace syscalls:sys_exit_open { + var fd + var errno + + if (arg1 < 0) { + fd = 0 + errno = -arg1 + } else { + fd = arg1 + errno = 0 + } + + printf("%5d %6d %-12s %3d %3d %s\n", uid, pid, execname, fd, + errno, path[tid]) + + path[tid] = 0 +}
tools/ktap/samples/syscalls/sctop.kp new file +13 -0
@@ -0,0 +1,13 @@ +#! /usr/bin/env ktap + +var s = {} + +trace syscalls:sys_enter_* { + s[probename] += 1 +} + +tick-5s { + ansi.clear_screen() + print_hist(s) + delete(s) +}
tools/ktap/samples/syscalls/syscalls.kp new file +6 -0
@@ -0,0 +1,6 @@ +#!/usr/bin/env ktap + +trace syscalls:* { + print(cpu, pid, execname, argstr) +} +
tools/ktap/samples/syscalls/syscalls_count.kp new file +54 -0
54 lines not on this page · raw message
tools/ktap/samples/syscalls/syscalls_count_by_proc.kp new file +22 -0
@@ -0,0 +1,22 @@ +#!/usr/bin/env ktap + +var s = {} + +trace syscalls:sys_enter_* { + s[execname] += 1 +} + +trace_end { + print_hist(s) +} + +#Result: +# +#[root@jovi ktap]# ./ktap samples/syscalls_count_by_proc.kp +#^C +# value ------------- Distribution ------------- count +# sshd |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 196 +# iscsid |@@@@ 24 +# sendmail |@ 9 + +
tools/ktap/samples/syscalls/syslatl.kp new file +33 -0
@@ -0,0 +1,33 @@ +#!/usr/bin/env ktap +# +# syslatl.kp syscall latency linear aggregation +# +# 10-Nov-2013 Brendan Gregg Created this + +var step = 10 # number of ms per step + +var self = {} +var lats = {} +var max = 0 + +trace syscalls:sys_enter_* { + self[tid] = gettimeofday_us() +} + +trace syscalls:sys_exit_* { + if (self[tid] == nil) { return } + var delta = (gettimeofday_us() - self[tid]) / (step * 1000) + if (delta > max) { max = delta } + lats[delta] += 1 + self[tid] = nil +} + +trace_end { + printf(" %8s %8s\n", "LAT(ms)+", "COUNT"); + for (i = 0, max, 1) { + if (lats[i] == nil) { + lats[i] = 0 + } + printf(" %8d %8d\n", i * step, lats[i]); + } +}
tools/ktap/samples/syscalls/syslist.kp new file +31 -0
@@ -0,0 +1,31 @@ +#!/usr/bin/env ktap +# +# syslist.kp syscall latency as a list with counts +# +# 10-Nov-2013 Brendan Gregg Created this + +var self = {} +var lats = {} +var order = {} # a workaround for key sorting + +trace syscalls:sys_enter_* { + self[tid] = gettimeofday_us() +} + +trace syscalls:sys_exit_* { + if (self[tid] == nil) { return } + var delta = gettimeofday_us() - self[tid] + lats[delta] += 1 + order[delta] = delta + self[tid] = nil +} + +trace_end { + printf(" %8s %8s\n", "LAT(us)", "COUNT"); + #TODO: use a more simple way to sort keys + + #for (lat, dummy in sort_pairs(order, cmp)) { + # printf(" %8d %8d\n", lat, lats[lat]); + #} + print_hist(lats) +}
tools/ktap/samples/tracepoints/eventcount.kp new file +210 -0
210 lines not on this page · raw message
tools/ktap/samples/tracepoints/eventcount_by_proc.kp new file +57 -0
57 lines not on this page · raw message
tools/ktap/samples/tracepoints/raw_tracepoint.kp new file +15 -0
@@ -0,0 +1,15 @@ +#!/usr/bin/env ktap + +#This script use kdebug.tracepoint, not 'trace' keyword which use perf backend. +# +#The overhead of kdebug.tracepoint would be much little than normal perf +#backend tracing. + +kdebug.tracepoint("sys_enter_open", function () { + printf("sys_enter_open: (%s) open file (%s)\n", + execname, user_string(arg1)) +}) + +kdebug.tracepoint("sys_exit_open", function () { + printf("sys_exit_open: return fd: (%d)\n", arg1) +})
tools/ktap/samples/tracepoints/tracepoints.kp new file +6 -0
@@ -0,0 +1,6 @@ +#!/usr/bin/env ktap + +trace *:* { + print(cpu, pid, execname, argstr) +} +
tools/ktap/samples/userspace/gcc_unwind.kp new file +9 -0
@@ -0,0 +1,9 @@ +#!/usr/bin/env ktap + +#only tested in x86-64 system, +#if you run this script in x86_32, change the libc path. + +trace sdt:/lib/x86_64-linux-gnu/libgcc_s.so.1:unwind { + print(execname, argstr) +} +
tools/ktap/samples/userspace/glibc_func_hist.kp new file +44 -0
44 lines not on this page · raw message
tools/ktap/samples/userspace/glibc_sdt.kp new file +11 -0
@@ -0,0 +1,11 @@ +#!/usr/bin/env ktap + +#This ktap script trace all sdt notes in glibc + +#only tested in x86-64 system, +#if you run this script in x86_32, change the libc path. + +trace sdt:/lib64/libc.so.6:* { + print(execname, argstr) +} +
tools/ktap/samples/userspace/glibc_trace.kp new file +11 -0
@@ -0,0 +1,11 @@ +#!/usr/bin/env ktap + +#This ktap script trace all functions in glibc + +#only tested in x86-64 system, +#if you run this script in x86_32, change the libc path. + +trace probe:/lib64/libc.so.6:* { + print(execname, argstr) +} +
tools/ktap/samples/userspace/malloc_free.kp new file +20 -0
@@ -0,0 +1,20 @@ +#!/usr/bin/env ktap + +#only tested in x86-64 system, +#if you run this script in x86_32, change the libc path. + +trace probe:/lib64/libc.so.6:malloc { + print("malloc entry:", execname) +} + +trace probe:/lib64/libc.so.6:malloc%return { + print("malloc exit:", execname) +} + +trace probe:/lib64/libc.so.6:free { + print("free entry:", execname) +} + +trace probe:/lib64/libc.so.6:free%return { + print("free exit:", execname) +}
tools/ktap/samples/userspace/malloc_size_hist.kp new file +22 -0
@@ -0,0 +1,22 @@ +#!/usr/bin/env ktap + +# Aggregate system or process malloc size + +# only tested in x86-64 system, +# if you run this script in x86_32, change the libc path and register name. +# +# Examples: +# +# ktap malloc_size_hist.kp +# ktap malloc_size_hist.kp -- ls + +var s = {} + +trace probe:/lib64/libc.so.6:malloc size=%di { + #arg2 is argument "size" of malloc function + s[arg1] += 1 +} + +trace_end { + print_hist(s) +}
tools/ktap/samples/userspace/pthread.kp new file +8 -0
@@ -0,0 +1,8 @@ +#!/usr/bin/env ktap + +# This script trace pthread_mutex* related call in libpthread +# Tested in x86_64 + +trace probe:/lib64/libpthread-2.17.so:pthread_mutex_* { + print(execname, argstr) +}
Signature · 1 line
1.8.1.4
PATCH 4/28 ktap: add basic ktap types definition
In reply to Jovi ZhangweiFile 'include/ktap_types.h' is the basestone for ktap runtime and userspace tool, it contains key structure definition for ktap.
1. ktap_val_t
ktap_val_t is ktap value representaion in stack, and for object reference. Each value have a type, the type could be:
nil, false, true, number, string, upval, proto, func, table, eventstr, kstrace, kip, uip.
ktap_val_t structure occupy 16 bytes in x86_64.
2. ktap_number
number type in ktap, it is 'long'.
3. ktap_str_t
String type. All string in ktap is interned, it means ktap keeps a single copy for any string. Whenever a new string appears, ktap checks whether it already has a copy of that string and, if so, reuses that copy. Internalization makes operations like string comparison and table indexing very fast, but it slows down string creation.
String is allocated from mempool(runtime/kp_mempool.c), not allocated by kmalloc, because we don't want dynamic allocate string in probe context.
Real string buffer is appended after ktap_str_t.
4. ktap_proto_t
prototype structure for ktap function.
5. ktap_upval_t
The concept of upval is get from lua.
var a = 1
function f() {
print(a) <--- 'a' is upval in 'f'.
}6. ktap_func_t
Instance of ktap_proto_t, also can be called as 'closure', it's created for each function prototype in runtime. ktap_func_t contains upvals array for access variables in upper scope layer.
7. ktap_tab_t
ktap table(associative array) structure, it contains array part and hash part.
Table is pre-allocated by vmalloc, table is not allowed to resize, it will report overflow when entries full.
8. ktap_state_t
ktap context structure.
There have many context in ktap runtime:
1). mainthread conetxt
mainthread context is the context in ktap thread.
2). probe context
ktap runtime pre-allocated percpu probe context.
9. ktap_global_state_t
global state of ktap runtime, it's access by G(ks).
--- tools/ktap/include/ktap_types.h | 463 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 463 insertions(+) create mode 100644 tools/ktap/include/ktap_types.h
tools/ktap/include/ktap_types.h new file +463 -0
@@ -0,0 +1,463 @@ +/* +* ktap types definition. +* +* Copyright (C) 2012-2014 Jovi Zhangwei <[email protected]>. +* Copyright (C) 2005-2014 Mike Pall. +* Copyright (C) 1994-2008 Lua.org, PUC-Rio. +*/ + +#ifndef __KTAP_TYPES_H__ +#define __KTAP_TYPES_H__ + +#ifdef __KERNEL__ +#include <linux/perf_event.h> +#else +typedef char u8 ; +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <stdint.h> +typedef int ptrdiff_t ; +#endif + +#include "../include/ktap_bc.h" + +/* Various VM limits. */ +#define KP_MAX_MEMPOOL_SIZE 10000/* Max. mempool size(Kbytes). */ +#define KP_MAX_STR 512/* Max. string length. */ +#define KP_MAX_STRNUM 9999/* Max. string number. */ + +#define KP_MAX_STRTAB (1<<26)/* Max. string table size. */ +#define KP_MAX_HBITS 26/* Max. hash bits. */ +#define KP_MAX_ABITS 28/* Max. bits of array key. */ +#define KP_MAX_ASIZE ((1<<(KP_MAX_ABITS-1))+1)/* Max. array part size. */ +#define KP_MAX_COLOSIZE 16/* Max. elems for colocated array. */ + +#define KP_MAX_LINE 1000/* Max. source code line number. */ +#define KP_MAX_XLEVEL 200/* Max. syntactic nesting level. */ +#define KP_MAX_BCINS (1<<26)/* Max. # of bytecode instructions. */ +#define KP_MAX_SLOTS 250/* Max. # of slots in a ktap func. */ +#define KP_MAX_LOCVAR 200/* Max. # of local variables. */ +#define KP_MAX_UPVAL 60/* Max. # of upvalues. */ + +#define KP_MAX_CACHED_CFUNCTION 128/* Max. cached global cfunction */ + +#define KP_MAX_STACK_DEPTH 50/* Max. stack depth */ + +/* +* The first argument type of kdebug.trace_by_id() +* The value is a userspace memory pointer. +* Maybe embed it into the trunk file in future. +*/ +typedef struct ktap_eventdesc { +int nr;/* the number of events id */ +int *id_arr;/* events id array */ +char *filter; +}ktap_eventdesc_t ; + + +/* ktap option for each script */ +typedef struct ktap_option { +char *trunk;/* __user */ +int trunk_len; +int argc; +char **argv;/* __user */ +int verbose; +int trace_pid; +int workload; +int trace_cpu; +int print_timestamp; +int quiet; +int dry_run; +}ktap_option_t ; + +/* +* Ioctls that can be done on a ktap fd: +* todo: use _IO macro in include/uapi/asm-generic/ioctl.h +*/ +#define KTAP_CMD_IOC_VERSION ('$' + 0) +#define KTAP_CMD_IOC_RUN ('$' + 1) +#define KTAP_CMD_IOC_EXIT ('$' + 3) + +#define KTAP_VERSION_MAJOR "0" +#define KTAP_VERSION_MINOR "4" + +#define KTAP_VERSION "ktap " KTAP_VERSION_MAJOR "." KTAP_VERSION_MINOR +#define KTAP_AUTHOR "Jovi Zhangwei <[email protected]>" +#define KTAP_COPYRIGHT KTAP_VERSION " Copyright (C) 2012-2014, " KTAP_AUTHOR + +#define MYINT (s) (s[0] - '0') +#define VERSION (MYINT(KTAP_VERSION_MAJOR) * 16 + MYINT(KTAP_VERSION_MINOR)) + +typedef long ktap_number ; +typedef int ktap_instr_t ; +typedef union ktap_obj ktap_obj_t ; + +struct ktap_state ; +typedef int (*ktap_cfunction ) (struct ktap_state *ks); + +/* ktap_val_t is basic value type in ktap stack, for reference all objects */ +typedef struct ktap_val { +union { +ktap_obj_t *gc;/* collectable objects, str/tab/... */ +void *p;/* light userdata */ +ktap_cfunction f;/* light C functions */ +ktap_number n;/* numbers */ +struct { +uint16_t depth;/* stack depth */ +uint16_t skip;/* skip stack entries */ + } stack; + } val; +union { +int type;/* type for above val */ +const unsigned int *pcr;/* Overlaps PC for ktap frames.*/ + }; +}ktap_val_t ; + +typedef ktap_val_t *StkId ; + +#define GCHeader ktap_obj_t *nextgc; u8 gct; + +typedef struct ktap_str { +GCHeader ; +u8 reserved;/* Used by lexer for fast lookup of reserved words. */ +u8 extra; +unsigned int hash; +int len;/* number of characters in string */ +}ktap_str_t ; + +typedef struct ktap_upval { +GCHeader ; +uint8_t closed;/* Set if closed (i.e. uv->v == &uv->u.value). */ +uint8_t immutable;/* Immutable value. */ +union { +ktap_val_t tv;/* If closed: the value itself. */ +struct {/* If open: double linked list, anchored at thread. */ +struct ktap_upval *prev; +struct ktap_upval *next; + }; + }; +ktap_val_t *v;/* Points to stack slot (open) or above (closed). */ +}ktap_upval_t ; + + +typedef struct ktap_func { +GCHeader ; +u8 nupvalues; +BCIns *pc; +struct ktap_proto *p; +struct ktap_upval *upvals[1 ];/* list of upvalues */ +}ktap_func_t ; + +typedef struct ktap_proto { +GCHeader ; +uint8_t numparams;/* Number of parameters. */ +uint8_t framesize;/* Fixed frame size. */ +int sizebc;/* Number of bytecode instructions. */ +ktap_obj_t *gclist; +void *k;/* Split constant array (points to the middle). */ +void *uv;/* Upvalue list. local slot|0x8000 or parent uv idx. */ +int sizekgc;/* Number of collectable constants. */ +int sizekn;/* Number of lua_Number constants. */ +int sizept;/* Total size including colocated arrays. */ +uint8_t sizeuv;/* Number of upvalues. */ +uint8_t flags;/* Miscellaneous flags (see below). */ + +/* --- The following fields are for debugging/tracebacks only --- */ +ktap_str_t *chunkname;/* Chunk name this function was defined in. */ +BCLine firstline;/* First line of the function definition. */ +BCLine numline;/* Number of lines for the function definition. */ +void *lineinfo;/* Compressed map from bytecode ins. to source line. */ +void *uvinfo;/* Upvalue names. */ +void *varinfo;/* Names and compressed extents of local variables. */ +}ktap_proto_t ; + +/* Flags for prototype. */ +#define PROTO_CHILD 0x01/* Has child prototypes. */ +#define PROTO_VARARG 0x02/* Vararg function. */ +#define PROTO_FFI 0x04/* Uses BC_KCDATA for FFI datatypes. */ +#define PROTO_NOJIT 0x08/* JIT disabled for this function. */ +#define PROTO_ILOOP 0x10/* Patched bytecode with ILOOP etc. */ +/* Only used during parsing. */ +#define PROTO_HAS_RETURN 0x20/* Already emitted a return. */ +#define PROTO_FIXUP_RETURN 0x40/* Need to fixup emitted returns. */ +/* Top bits used for counting created closures. */ +#define PROTO_CLCOUNT 0x20/* Base of saturating 3 bit counter. */ +#define PROTO_CLC_BITS 3 +#define PROTO_CLC_POLY (3*PROTO_CLCOUNT)/* Polymorphic threshold. */ + +#define PROTO_UV_LOCAL 0x8000/* Upvalue for local slot. */ +#define PROTO_UV_IMMUTABLE 0x4000/* Immutable upvalue. */ + +#define proto_kgc (pt, idx) (((ktap_obj_t *)(pt)->k)[idx]) +#define proto_bc (pt) ((BCIns *)((char *)(pt) + sizeof(ktap_proto_t))) +#define proto_bcpos (pt, pc) ((BCPos)((pc) - proto_bc(pt))) +#define proto_uv (pt) ((uint16_t *)(pt)->uv) + +#define proto_chunkname (pt) ((pt)->chunkname) +#define proto_lineinfo (pt) ((const void *)(pt)->lineinfo) +#define proto_uvinfo (pt) ((const uint8_t *)(pt)->uvinfo) +#define proto_varinfo (pt) ((const uint8_t *)(pt)->varinfo) + + +typedef struct ktap_node_t { +ktap_val_t val;/* Value object. Must be first field. */ +ktap_val_t key;/* Key object. */ +struct ktap_node_t *next;/* hash chain */ +}ktap_node_t ; + +/* ktap_tab */ +typedef struct ktap_tab { +GCHeader ; +#ifdef __KERNEL__ +arch_spinlock_t lock; +#endif +ktap_val_t *array;/* Array part. */ +ktap_node_t *node;/* Hash part. */ +ktap_node_t *freetop;/* any free position is before this position */ + +uint32_t asize;/* Size of array part (keys [0, asize-1]). */ +uint32_t hmask;/* log2 of size of `node' array */ + +uint32_t hnum;/* number of all nodes */ +}ktap_tab_t ; + +typedef struct ktap_stats { +int mem_allocated; +int nr_mem_allocate; +int events_hits; +int events_missed; +}ktap_stats_t ; + +#define KTAP_STATS (ks) this_cpu_ptr(G(ks)->stats) + + +#define KTAP_RUNNING 0/* normal running state */ +#define KTAP_TRACE_END 1/* running in trace_end function */ +#define KTAP_EXIT 2/* normal exit, set when call exit() */ +#define KTAP_ERROR 3/* error state, called by kp_error */ + +typedef struct ktap_global_state { +void *mempool;/* string memory pool */ +void *mp_freepos;/* free position in memory pool */ +int mp_size;/* memory pool size */ +#ifdef __KERNEL__ +arch_spinlock_t mp_lock;/* mempool lock */ +#endif + +ktap_str_t **strhash;/* String hash table (hash chain anchors). */ +int strmask;/* String hash mask (size of hash table-1). */ +int strnum;/* Number of strings in hash table. */ +#ifdef __KERNEL__ +arch_spinlock_t str_lock;/* string operation lock */ +#endif + +ktap_val_t registry; +ktap_tab_t *gtab;/* global table contains cfunction and args */ +ktap_obj_t *allgc;/* list of all collectable objects */ +ktap_upval_t uvhead;/* head of list of all open upvalues */ + +struct ktap_state *mainthread;/*main state */ +int state;/* status of ktapvm, KTAP_RUNNING, KTAP_TRACE_END, etc */ +#ifdef __KERNEL__ +/* reserved global percpu data */ +void __percpu *percpu_state[PERF_NR_CONTEXTS ]; +void __percpu *percpu_print_buffer[PERF_NR_CONTEXTS ]; +void __percpu *percpu_temp_buffer[PERF_NR_CONTEXTS ]; + +/* for recursion tracing check */ +int __percpu *recursion_context[PERF_NR_CONTEXTS ]; + +ktap_option_t *parm;/* ktap options */ +pid_t trace_pid; +struct task_struct *trace_task; +cpumask_var_t cpumask; +struct ring_buffer *buffer; +struct dentry *trace_pipe_dentry; +struct task_struct *task; +int trace_enabled; +int wait_user;/* flag to indicat waiting user consume content */ + +struct list_head timers;/* timer list */ +struct ktap_stats __percpu *stats;/* memory allocation stats */ +struct list_head events_head;/* probe event list */ + +ktap_func_t *trace_end_closure;/* trace_end closure */ + +/* C function table for fast call */ +int nr_builtin_cfunction; +ktap_cfunction gfunc_tbl[KP_MAX_CACHED_CFUNCTION ]; +#endif +}ktap_global_state_t ; + + +typedef struct ktap_state { +ktap_global_state_t *g;/* global state */ +int stop;/* don't enter tracing handler if stop is 1 */ +StkId top;/* stack top */ +StkId func;/* execute light C function */ +StkId stack_last;/* last stack pointer */ +StkId stack;/* ktap stack, percpu pre-reserved */ +ktap_upval_t *openupval;/* opened upvals list */ + +#ifdef __KERNEL__ +/* current fired event which allocated on stack */ +struct ktap_event_data *current_event; +#endif +}ktap_state_t ; + +#define G (ks) (ks->g) + +/* +* Union of all collectable objects +*/ +union ktap_obj { + struct { GCHeader } gch; +struct ktap_str ts; +struct ktap_func fn; +struct ktap_tab h; +struct ktap_proto pt; +struct ktap_upval uv; +struct ktap_state th;/* thread */ +}; + +#define gch (o) (&(o)->gch) + +/* macros to convert a ktap_obj_t into a specific value */ +#define gco2ts (o) (&((o)->ts)) +#define gco2uv (o) (&((o)->uv)) +#define obj2gco (v) ((ktap_obj_t *)(v)) + +/* predefined values in the registry */ +#define KTAP_RIDX_GLOBALS 1 +#define KTAP_RIDX_LAST KTAP_RIDX_GLOBALS + +/* ktap object types */ +#define KTAP_TNIL (~0u) +#define KTAP_TFALSE (~1u) +#define KTAP_TTRUE (~2u) +#define KTAP_TNUM (~3u) +#define KTAP_TLIGHTUD (~4u) +#define KTAP_TSTR (~5u) +#define KTAP_TUPVAL (~6u) +#define KTAP_TPROTO (~7u) +#define KTAP_TFUNC (~8u) +#define KTAP_TCFUNC (~9u) +#define KTAP_TCDATA (~10u) +#define KTAP_TTAB (~11u) +#define KTAP_TUDATA (~12u) + +/* Specfic types */ +#define KTAP_TEVENTSTR (~13u)/* argstr */ +#define KTAP_TKSTACK (~14u)/* stack(), not intern to string yet */ +#define KTAP_TKIP (~15u)/* kernel function ip addres */ +#define KTAP_TUIP (~16u)/* userspace function ip addres */ + +/* This is just the canonical number type used in some places. */ +#define KTAP_TNUMX (~17u) + + +#define itype (o) ((o)->type) +#define setitype (o, t) ((o)->type = (t)) + +#define val_ (o) ((o)->val) +#define gcvalue (o) (val_(o).gc) + +#define nvalue (o) (val_(o).n) +#define boolvalue (o) (KTAP_TFALSE - (o)->type) +#define hvalue (o) (&val_(o).gc->h) +#define phvalue (o) (&val_(o).gc->ph) +#define clvalue (o) (&val_(o).gc->fn) +#define ptvalue (o) (&val_(o).gc->pt) + +#define getstr (ts) (const char *)((ts) + 1) +#define rawtsvalue (o) (&val_(o).gc->ts) +#define svalue (o) getstr(rawtsvalue(o)) + +#define pvalue (o) (&val_(o).p) +#define fvalue (o) (val_(o).f) + +#define is_nil (o) (itype(o) == KTAP_TNIL) +#define is_false (o) (itype(o) == KTAP_TFALSE) +#define is_true (o) (itype(o) == KTAP_TTRUE) +#define is_bool (o) (is_false(o) || is_true(o)) +#define is_string (o) (itype(o) == KTAP_TSTR) +#define is_number (o) (itype(o) == KTAP_TNUM) +#define is_table (o) (itype(o) == KTAP_TTAB) +#define is_proto (o) (itype(o) == KTAP_TPROTO) +#define is_function (o) (itype(o) == KTAP_TFUNC) +#define is_cfunc (o) (itype(o) == KTAP_TCFUNC) +#define is_eventstr (o) (itype(o) == KTAP_TEVENTSTR) +#define is_kip (o) (itype(o) == KTAP_TKIP) + +#define set_nil (o) ((o)->type = KTAP_TNIL) +#define set_bool (o, x) ((o)->type = KTAP_TFALSE-(uint32_t)(x)) + +static inline void set_number(ktap_val_t *o, ktap_number n) +{ + setitype(o ,KTAP_TNUM ); + o->val.n = n; +} + +static inline void set_string(ktap_val_t *o, const ktap_str_t *str) +{ +setitype (o,KTAP_TSTR ); + o->val.gc = (ktap_obj_t *)str; +} + +static inline void set_table (ktap_val_t *o,ktap_tab_t *tab) +{ +setitype (o,KTAP_TTAB ); + o->val.gc = (ktap_obj_t *)tab; +} + +static inline void set_proto (ktap_val_t *o,ktap_proto_t *pt) +{ +setitype (o,KTAP_TPROTO ); + o->val.gc = (ktap_obj_t *)pt; +} + +static inline void set_kstack (ktap_val_t *o,uint16_t depth,uint16_t skip) +{ +setitype (o,KTAP_TKSTACK ); + o->val.stack.depth = depth; + o->val.stack.skip = skip; +} + +static inline void set_func (ktap_val_t *o,ktap_func_t *fn) +{ +setitype (o,KTAP_TFUNC ); + o->val.gc = (ktap_obj_t *)fn; +} + +static inline void set_cfunc (ktap_val_t *o,ktap_cfunction fn) +{ +setitype (o,KTAP_TCFUNC ); + o->val.f = fn; +} + +static inline void set_eventstr (ktap_val_t *o) +{ +setitype (o,KTAP_TEVENTSTR ); +} + +static inline void set_ip (ktap_val_t *o,unsigned long addr) +{ +setitype (o,KTAP_TKIP ); + o->val.n = addr; +} + + +#define set_obj (o1, o2) { *(o1) = *(o2); } + +#define incr_top (ks) {ks->top++;} + +/* +* KTAP_QL describes how error messages quote program elements. +* CHANGE it if you want a different appearance. +*/ +#define KTAP_QL (x) "'" x "'" +#define KTAP_QS KTAP_QL("%s") + +#endif /* __KTAP_TYPES_H__ */ +
Signature · 1 line
1.8.1.4
PATCH 5/28 ktap: add bytecode definition
In reply to Jovi Zhangweiktap bytecode is based on luajit, a fast JIT for lua language.
There have a good detail introduction about luajit bytecode in
http://wiki.luajit.org/Bytecode-2.0
A single bytecode instruction is 32 bit wide and has an 8 bit opcode field and several operand fields of 8 or 16 bit. Instructions come in one of two formats:
+----+----+----+----+ | B | C | A | OP | Format ABC +----+----+----+----+ | D | A | OP | Format AD +-------------------- MSB LSB
In-memory instructions are always stored in host byte order.
E.g. 0xbbccaa1e is the instruction with opcode 0x1e (ADDVV), with operands A = 0xaa, B = 0xbb and C = 0xcc.
Bytecods:
1). Comparison ops
ISLT, ISGE, ISLE, ISGT, ISEQV, ISNEV, ISEQS, ISNES, ISEQN, ISNEN, ISEQP, ISNEP
2). Unary Test and Copy ops
ISTC, ISFC, IST, ISF
3). Unary ops
MOV, NOT, UNM, LEN
4). Binary ops
ADDVN, SUBVN, MULVN, DIVVN, MODVN, ADDNV, SUBNV, MULNV, DIVNV, MODNV, ADDVV, SUBVV, SUBVV, MULVV, DIVVV, MODVV, POW, CAT
5). Constant ops
KSTR, KCDATA, KSHORT, KNUM, KPRI, KNIL
6). Upvalue and Function ops
UGET, USETV, USETS, USETN, USETP, UCLO, FNEW
7). Table ops
TNEW, TDUP, GGET, GSET, TGETV, TGETS, TGETB, TSETV, TSETS, TSETB, TSETM
8). Calls and Vararg Handling (ktap do not support variable argument now) CALLM, CALL, CALLMT, CALLT, ITERC, ITERN, VARG, ISNEXT
9). Returns (ktap don't support RETM now)
RETM, RET, RET0, RET1
10). Loops and branches (loop bytecode J* is not used in ktap) FORI, JFORI, FORL, IFORL, JFORL, ITERL, IITERL, JITERL, LOOP, ILOOP, JLOOP, JMP
11). Function headers (All is not used in ktap) FUNCF, IFUNCF, JFUNCF, FUNCV, JFUNCV, FUNCC, FUNCCW
12). ktap specific bytecodes
VARGN, VARGSTR, VPROBENAME, VPID, VTID, VUID, VCPU, VEXECNAME, GFUNC
--- tools/ktap/include/ktap_bc.h | 369 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 369 insertions(+) create mode 100644 tools/ktap/include/ktap_bc.h
tools/ktap/include/ktap_bc.h new file +369 -0
@@ -0,0 +1,369 @@ +/* +* Bytecode instruction format. +* +* Copyright (C) 2012-2014 Jovi Zhangwei <[email protected]>. +* Copyright (C) 2005-2014 Mike Pall. +*/ + +#ifndef __KTAP_BC_H__ +#define __KTAP_BC_H__ + +#include "../include/ktap_arch.h" + +/*TODO*/ +#define KP_STATIC_ASSERT (cond) +#define kp_assert (cond) + +/* Types for handling bytecodes. */ +typedef uint32_t BCIns ;/* Bytecode instruction. */ +typedef uint32_t BCPos ;/* Bytecode position. */ +typedef uint32_t BCReg ;/* Bytecode register. */ +typedef int32_t BCLine ;/* Bytecode line number. */ + +/* +* Bytecode instruction format, 32 bit wide, fields of 8 or 16 bit: +* +* +----+----+----+----+ +* | B | C | A | OP | Format ABC +* +----+----+----+----+ +* | D | A | OP | Format AD +* +-------------------- +* MSB LSB +* +* In-memory instructions are always stored in host byte order. +*/ + +/* Operand ranges and related constants. */ +#define BCMAX_A 0xff +#define BCMAX_B 0xff +#define BCMAX_C 0xff +#define BCMAX_D 0xffff +#define BCBIAS_J 0x8000 +#define NO_REG BCMAX_A +#define NO_JMP (~(BCPos)0) + +/* Macros to get instruction fields. */ +#define bc_op (i) ((BCOp)((i)&0xff)) +#define bc_a (i) ((BCReg)(((i)>>8)&0xff)) +#define bc_b (i) ((BCReg)((i)>>24)) +#define bc_c (i) ((BCReg)(((i)>>16)&0xff)) +#define bc_d (i) ((BCReg)((i)>>16)) +#define bc_j (i) ((ptrdiff_t)bc_d(i)-BCBIAS_J) + +/* Macros to set instruction fields. */ +#define setbc_byte (p, x, ofs) \ + ((uint8_t *)(p))[KP_ENDIAN_SELECT(ofs, 3 - ofs)] = (uint8_t)(x) +#define setbc_op (p, x) setbc_byte(p, (x), 0) +#define setbc_a (p, x) setbc_byte(p, (x), 1) +#define setbc_b (p, x) setbc_byte(p, (x), 3) +#define setbc_c (p, x) setbc_byte(p, (x), 2) +#define setbc_d (p, x) \ + ((uint16_t *)(p))[KP_ENDIAN_SELECT(1, 0)] = (uint16_t)(x) +#define setbc_j (p, x) setbc_d(p, (BCPos)((int32_t)(x)+BCBIAS_J)) + +/* Macros to compose instructions. */ +#define BCINS_ABC (o, a, b, c) \ + (((BCIns)(o))|((BCIns)(a)<<8)|((BCIns)(b)<<24)|((BCIns)(c)<<16)) +#define BCINS_AD (o, a, d) \ + (((BCIns)(o))|((BCIns)(a)<<8)|((BCIns)(d)<<16)) +#define BCINS_AJ (o, a, j) BCINS_AD(o, a, (BCPos)((int32_t)(j)+BCBIAS_J)) + +/* +* Bytecode instruction definition. Order matters, see below. +* +* (name, filler, Amode, Bmode, Cmode or Dmode, metamethod) +* +* The opcode name suffixes specify the type for RB/RC or RD: +* V = variable slot +* S = string const +* N = number const +* P = primitive type (~itype) +* B = unsigned byte literal +* M = multiple args/results +*/ +#define BCDEF (_) \ +/* Comparison ops. ORDER OPR. */ \ + _(ISLT, var, ___, var, lt) \ + _(ISGE, var, ___, var, lt) \ + _(ISLE, var, ___, var, le) \ + _(ISGT, var, ___, var, le) \ + \ + _(ISEQV, var, ___, var, eq) \ + _(ISNEV, var, ___, var, eq) \ + _(ISEQS, var, ___, str, eq) \ + _(ISNES, var, ___, str, eq) \ + _(ISEQN, var, ___, num, eq) \ + _(ISNEN, var, ___, num, eq) \ + _(ISEQP, var, ___, pri, eq) \ + _(ISNEP, var, ___, pri, eq) \ + \ +/* Unary test and copy ops. */ \ + _(ISTC, dst, ___, var, ___) \ + _(ISFC, dst, ___, var, ___) \ + _(IST, ___, ___, var, ___) \ + _(ISF, ___, ___, var, ___) \ + _(ISTYPE, var, ___, lit, ___) \ +_ (ISNUM , var, ___, lit, ___) \ + \ + /* Unary ops. */ \ + _(MOV, dst, ___, var, ___) \ + _(NOT, dst, ___, var, ___) \ +_ (UNM , dst, ___, var, unm) \ + \ + /* Binary ops. ORDER OPR. VV last, POW must be next. */ \ + _(ADDVN, dst, var, num, add) \ + _(SUBVN, dst, var, num, sub) \ + _(MULVN, dst, var, num, mul) \ + _(DIVVN, dst, var, num, div) \ + _(MODVN, dst, var, num, mod) \ + \ + _(ADDNV, dst, var, num, add) \ + _(SUBNV, dst, var, num, sub) \ + _(MULNV, dst, var, num, mul) \ + _(DIVNV, dst, var, num, div) \ + _(MODNV, dst, var, num, mod) \ + \ + _(ADDVV, dst, var, var, add) \ + _(SUBVV, dst, var, var, sub) \ + _(MULVV, dst, var, var, mul) \ + _(DIVVV, dst, var, var, div) \ + _(MODVV, dst, var, var, mod) \ + \ + _(POW, dst, var, var, pow) \ +_ (CAT , dst, rbase, rbase, concat) \ + \ + /* Constant ops. */ \ + _(KSTR, dst, ___, str, ___) \ + _(KCDATA, dst, ___, cdata, ___) \ + _(KSHORT, dst, ___, lits, ___) \ + _(KNUM, dst, ___, num, ___) \ + _(KPRI, dst, ___, pri, ___) \ +_ (KNIL , base, ___, base, ___) \ + \ + /* Upvalue and function ops. */ \ + _(UGET, dst, ___, uv, ___) \ + _(USETV, uv, ___, var, ___) \ + _(UINCV, uv, ___, var, ___) \ + _(USETS, uv, ___, str, ___) \ + _(USETN, uv, ___, num, ___) \ + _(UINCN, uv, ___, num, ___) \ + _(USETP, uv, ___, pri, ___) \ + _(UCLO, rbase, ___, jump, ___) \ +_ (FNEW , dst, ___, func, gc) \ + \ + /* Table ops. */ \ + _(TNEW, dst, ___, lit, gc) \ + _(TDUP, dst, ___, tab, gc) \ + _(GGET, dst, ___, str, index) \ + _(GSET, var, ___, str, newindex) \ + _(GINC, var, ___, str, newindex) \ + _(TGETV, dst, var, var, index) \ + _(TGETS, dst, var, str, index) \ + _(TGETB, dst, var, lit, index) \ + _(TGETR, dst, var, var, index) \ + _(TSETV, var, var, var, newindex) \ + _(TINCV, var, var, var, newindex) \ + _(TSETS, var, var, str, newindex) \ + _(TINCS, var, var, str, newindex) \ + _(TSETB, var, var, lit, newindex) \ + _(TINCB, var, var, lit, newindex) \ + _(TSETM, base, ___, num, newindex) \ +_ (TSETR , var, var, var, newindex) \ + \ + /* Calls and vararg handling. T = tail call. */ \ + _(CALLM, base, lit, lit, call) \ + _(CALL, base, lit, lit, call) \ + _(CALLMT, base, ___, lit, call) \ + _(CALLT, base, ___, lit, call) \ + _(ITERC, base, lit, lit, call) \ + _(ITERN, base, lit, lit, call) \ + _(VARG, base, lit, lit, ___) \ +_ (ISNEXT , base, ___, jump, ___) \ + \ + /* Returns. */ \ + _(RETM, base, ___, lit, ___) \ + _(RET, rbase, ___, lit, ___) \ + _(RET0, rbase, ___, lit, ___) \ +_ (RET1 , rbase, ___, lit, ___) \ + \ + /* Loops and branches. I/J = interp/JIT, I/C/L = init/call/loop. */ \ + _(FORI, base, ___, jump, ___) \ + _(JFORI, base, ___, jump, ___) \ + \ + _(FORL, base, ___, jump, ___) \ + _(IFORL, base, ___, jump, ___) \ + _(JFORL, base, ___, lit, ___) \ + \ + _(ITERL, base, ___, jump, ___) \ + _(IITERL, base, ___, jump, ___) \ + _(JITERL, base, ___, lit, ___) \ + \ + _(LOOP, rbase, ___, jump, ___) \ + _(ILOOP, rbase, ___, jump, ___) \ + _(JLOOP, rbase, ___, lit, ___) \ + \ +_ (JMP , rbase, ___, jump, ___) \ + \ + /*Function headers. I/J = interp/JIT, F/V/C = fixarg/vararg/C func.*/ \ + _(FUNCF, rbase, ___, ___, ___) \ + _(IFUNCF, rbase, ___, ___, ___) \ + _(JFUNCF, rbase, ___, lit, ___) \ + _(FUNCV, rbase, ___, ___, ___) \ + _(IFUNCV, rbase, ___, ___, ___) \ + _(JFUNCV, rbase, ___, lit, ___) \ + _(FUNCC, rbase, ___, ___, ___) \ +_ (FUNCCW , rbase, ___, ___, ___) \ + \ + /* specific purpose bc. */ \ + _(VARGN, dst, ___, lit, ___) \ + _(VARGSTR, dst, ___, lit, ___) \ + _(VPROBENAME, dst, ___, lit, ___) \ + _(VPID, dst, ___, lit, ___) \ + _(VTID, dst, ___, lit, ___) \ + _(VUID, dst, ___, lit, ___) \ + _(VCPU, dst, ___, lit, ___) \ + _(VEXECNAME, dst, ___, lit, ___) \ + \ +_ (GFUNC , dst, ___, ___, ___)/*load global C function*/ + +/* Bytecode opcode numbers. */ +typedef enum { +#define BCENUM(name, ma, mb, mc, mt) BC_##name, + BCDEF(BCENUM) +#undef BCENUM +BC__MAX +}BCOp ; + +KP_STATIC_ASSERT ((int )BC_ISEQV +1 == (int )BC_ISNEV ); +KP_STATIC_ASSERT (((int )BC_ISEQV ^1 ) == (int )BC_ISNEV ); +KP_STATIC_ASSERT (((int )BC_ISEQS ^1 ) == (int )BC_ISNES ); +KP_STATIC_ASSERT (((int )BC_ISEQN ^1 ) == (int )BC_ISNEN ); +KP_STATIC_ASSERT (((int )BC_ISEQP ^1 ) == (int )BC_ISNEP ); +KP_STATIC_ASSERT (((int )BC_ISLT ^1 ) == (int )BC_ISGE ); +KP_STATIC_ASSERT (((int )BC_ISLE ^1 ) == (int )BC_ISGT ); +KP_STATIC_ASSERT (((int )BC_ISLT ^3 ) == (int )BC_ISGT ); +KP_STATIC_ASSERT ((int )BC_IST -(int )BC_ISTC == (int )BC_ISF -(int )BC_ISFC ); +KP_STATIC_ASSERT ((int )BC_CALLT -(int )BC_CALL == (int )BC_CALLMT -(int )BC_CALLM ); +KP_STATIC_ASSERT ((int )BC_CALLMT +1 == (int )BC_CALLT ); +KP_STATIC_ASSERT ((int )BC_RETM +1 == (int )BC_RET ); +KP_STATIC_ASSERT ((int )BC_FORL +1 == (int )BC_IFORL ); +KP_STATIC_ASSERT ((int )BC_FORL +2 == (int )BC_JFORL ); +KP_STATIC_ASSERT ((int )BC_ITERL +1 == (int )BC_IITERL ); +KP_STATIC_ASSERT ((int )BC_ITERL +2 == (int )BC_JITERL ); +KP_STATIC_ASSERT ((int )BC_LOOP +1 == (int )BC_ILOOP ); +KP_STATIC_ASSERT ((int )BC_LOOP +2 == (int )BC_JLOOP ); +KP_STATIC_ASSERT ((int )BC_FUNCF +1 == (int )BC_IFUNCF ); +KP_STATIC_ASSERT ((int )BC_FUNCF +2 == (int )BC_JFUNCF ); +KP_STATIC_ASSERT ((int )BC_FUNCV +1 == (int )BC_IFUNCV ); +KP_STATIC_ASSERT ((int )BC_FUNCV +2 == (int )BC_JFUNCV ); + +/* This solves a circular dependency problem, change as needed. */ +#define FF_next_N 4 + +/* Stack slots used by FORI/FORL, relative to operand A. */ +enum { +FORL_IDX ,FORL_STOP ,FORL_STEP ,FORL_EXT +}; + +/* Bytecode operand modes. ORDER BCMode */ +typedef enum { +/* Mode A must be <= 7 */ + BCMnone, BCMdst, BCMbase, BCMvar, BCMrbase, BCMuv, + BCMlit, BCMlits, BCMpri, BCMnum, BCMstr, BCMtab, BCMfunc, + BCMjump, BCMcdata, + BCM_max +}BCMode ; + +#define BCM___ BCMnone + +#define bcmode_a (op) ((BCMode)(bc_mode[op] & 7)) +#define bcmode_b (op) ((BCMode)((bc_mode[op] >> 3) & 15)) +#define bcmode_c (op) ((BCMode)((bc_mode[op] >> 7) & 15)) +#define bcmode_d (op) bcmode_c(op) +#define bcmode_hasd (op) ((bc_mode[op] & (15 << 3)) == (BCMnone << 3)) +#define bcmode_mm (op) ((MMS)(bc_mode[op] >> 11)) + +#define BCMODE (name, ma, mb, mc, mm) \ + (BCM##ma | (BCM##mb << 3) | (BCM##mc << 7)|(MM_##mm << 11)), +#define BCMODE_FF 0 + +static inline int bc_isret (BCOp op) +{ +return (op ==BC_RETM || op ==BC_RET || op ==BC_RET0 || + op ==BC_RET1 ); +} + +/* +* Metamethod definition +* Note ktap don't use any lua methmethod currently. +*/ +typedef enum { + MM_lt, + MM_le, + MM_eq, + MM_unm, + MM_add, + MM_sub, + MM_mul, + MM_div, + MM_mod, + MM_pow, + MM_concat, + MM_gc, + MM_index, + MM_newindex, + MM_call, +MM__MAX , +MM____ =MM__MAX +}MMS ; + + +/* -- Bytecode dump format ------------------------------------------------ */ + +/* +** dump = header proto+ 0U +** header = ESC 'L' 'J' versionB flagsU [namelenU nameB*] +** proto = lengthU pdata +** pdata = phead bcinsW* uvdataH* kgc* knum* [debugB*] +** phead = flagsB numparamsB framesizeB numuvB numkgcU numknU numbcU +** [debuglenU [firstlineU numlineU]] +** kgc = kgctypeU { ktab | (loU hiU) | (rloU rhiU iloU ihiU) | strB* } +** knum = intU0 | (loU1 hiU) +** ktab = narrayU nhashU karray* khash* +** karray = ktabk +** khash = ktabk ktabk +** ktabk = ktabtypeU { intU | (loU hiU) | strB* } +** +** B = 8 bit, H = 16 bit, W = 32 bit, U = ULEB128 of W, U0/U1 = ULEB128 of W+1 +*/ + +/* Bytecode dump header. */ +#define BCDUMP_HEAD1 0x15 +#define BCDUMP_HEAD2 0x22 +#define BCDUMP_HEAD3 0x06 + +/* If you perform *any* kind of private modifications to the bytecode itself +** or to the dump format, you *must* set BCDUMP_VERSION to 0x80 or higher. +*/ +#define BCDUMP_VERSION 1 + +/* Compatibility flags. */ +#define BCDUMP_F_BE 0x01 +#define BCDUMP_F_STRIP 0x02 +#define BCDUMP_F_FFI 0x04 + +#define BCDUMP_F_KNOWN (BCDUMP_F_FFI*2-1) + +/* Type codes for the GC constants of a prototype. Plus length for strings. */ +enum { +BCDUMP_KGC_CHILD ,BCDUMP_KGC_TAB ,BCDUMP_KGC_I64 ,BCDUMP_KGC_U64 , +BCDUMP_KGC_COMPLEX ,BCDUMP_KGC_STR +}; + +/* Type codes for the keys/values of a constant table. */ +enum { +BCDUMP_KTAB_NIL ,BCDUMP_KTAB_FALSE ,BCDUMP_KTAB_TRUE , +BCDUMP_KTAB_INT ,BCDUMP_KTAB_NUM ,BCDUMP_KTAB_STR +}; + +#endif /* __KTAP_BC_H__ */
Signature · 1 line
1.8.1.4
PATCH 6/28 ktap: add include/ktap_arch.h and error header file
In reply to Jovi ZhangweiSimple arch related definition and error msg definition.
--- tools/ktap/include/ktap_arch.h | 33 ++++++++++ tools/ktap/include/ktap_err.h | 11 ++++ tools/ktap/include/ktap_errmsg.h | 135 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 179 insertions(+) create mode 100644 tools/ktap/include/ktap_arch.h create mode 100644 tools/ktap/include/ktap_err.h create mode 100644 tools/ktap/include/ktap_errmsg.h
tools/ktap/include/ktap_arch.h new file +33 -0
@@ -0,0 +1,33 @@ +#ifndef __KTAP_ARCH__ +#define __KTAP_ARCH__ + +#ifdef __KERNEL__ +#include <linux/types.h> +#include <asm/byteorder.h> + +#if defined(__LITTLE_ENDIAN) +#define KP_LE 1 +#define KP_BE 0 +#define KP_ENDIAN_SELECT (le, be) le +#elif defined(__BIG_ENDIAN) +#define KP_LE 0 +#define KP_BE 1 +#define KP_ENDIAN_SELECT (le, be) be +#endif + +#else /* __KERNEL__ */ + +#if __BYTE_ORDER == __LITTLE_ENDIAN +#define KP_LE 1 +#define KP_BE 0 +#define KP_ENDIAN_SELECT (le, be) le +#elif __BYTE_ORDER == __BIG_ENDIAN +#define KP_LE 0 +#define KP_BE 1 +#define KP_ENDIAN_SELECT (le, be) be +#else +#error "could not determine byte order" +#endif + +#endif +#endif
tools/ktap/include/ktap_err.h new file +11 -0
@@ -0,0 +1,11 @@ +#ifndef __KTAP_ERR_H__ +#define __KTAP_ERR_H__ + +typedef enum { +#define ERRDEF(name, msg) \ + KP_ERR_##name, KP_ERR_##name##_ = KP_ERR_##name + sizeof(msg)-1, +#include "ktap_errmsg.h" +KP_ERR__MAX +} ErrMsg; + +#endif
tools/ktap/include/ktap_errmsg.h new file +135 -0
135 lines not on this page · raw message
Signature · 1 line
1.8.1.4
PATCH 7/28 ktap: add runtime/ktap.[c|h]
In reply to Jovi Zhangweiruntime/ktap.c is ktapvm kernel module main entry, it init ktap kernel module, create 'ktap' debugfs directory.
Userspace tool send ioctl command to '/sys/kernel/debug/ktap/ktapvm' file, to control ktap runtime.
It will read bytecode trunk, validate and execute bytecode.
kp_vm_new_state
kp_bcread
kp_vm_validate_code
kp_vm_call_proto
--- tools/ktap/runtime/ktap.c | 276 ++++++++++++++++++++++++++++++++++++++++++++++ tools/ktap/runtime/ktap.h | 181 ++++++++++++++++++++++++++++++ 2 files changed, 457 insertions(+) create mode 100644 tools/ktap/runtime/ktap.c create mode 100644 tools/ktap/runtime/ktap.h
tools/ktap/runtime/ktap.c new file +276 -0
@@ -0,0 +1,276 @@ +/* +* ktap.c - ktapvm kernel module main entry +* +* This file is part of ktap by Jovi Zhangwei. +* +* Copyright (C) 2012-2014 Jovi Zhangwei <[email protected]>. +* +* ktap is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* ktap is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +/* +* this file is the first file to be compile, add CONFIG_ checking in here. +* See Requirements in doc/tutorial.md +*/ + +#include <linux/version.h> +#if LINUX_VERSION_CODE <KERNEL_VERSION (3 ,1 ,0 ) +#error "Currently ktap don't support kernel older than 3.1" +#endif + +#if !CONFIG_EVENT_TRACING +#error "Please enable CONFIG_EVENT_TRACING before compile ktap" +#endif + +#if !CONFIG_PERF_EVENTS +#error "Please enable CONFIG_PERF_EVENTS before compile ktap" +#endif + +#include <linux/module.h> +#include <linux/errno.h> +#include <linux/file.h> +#include <linux/slab.h> +#include <linux/fcntl.h> +#include <linux/sched.h> +#include <linux/poll.h> +#include <linux/anon_inodes.h> +#include <linux/debugfs.h> +#include <linux/vmalloc.h> +#include "../include/ktap_types.h" +#include "ktap.h" +#include "kp_bcread.h" +#include "kp_vm.h" + +/* common helper function */ +long gettimeofday_ns (void ) +{ +struct timespec now; + +getnstimeofday (&now); +return now.tv_sec *NSEC_PER_SEC + now.tv_nsec; +} + +static int load_trunk (ktap_option_t *parm,unsigned long **buff) +{ +int ret; +unsigned long *vmstart; + + vmstart =vmalloc (parm->trunk_len); +if (!vmstart) +return -ENOMEM ; + + ret =copy_from_user (vmstart, (void __user *)parm->trunk, + parm->trunk_len); +if (ret <0 ) { +vfree (vmstart); +return -EFAULT ; + } + + *buff = vmstart; +return 0 ; +} + +static struct dentry *kp_dir_dentry; + +/* Ktap Main Entry */ +static int ktap_main (struct file *file,ktap_option_t *parm) +{ +unsigned long *buff =NULL ; +ktap_state_t *ks; +ktap_proto_t *pt; +long start_time, delta_time; +int ret; + + start_time =gettimeofday_ns (); + + ks =kp_vm_new_state (parm, kp_dir_dentry); +if (unlikely (!ks)) +return -ENOEXEC ; + + file->private_data = ks; + + ret =load_trunk (parm, &buff); +if (ret) { +kp_error (ks,"cannot load file\n" ); + goto out; + } + + pt =kp_bcread (ks, (unsigned char *)buff, parm->trunk_len); + +vfree (buff); + +if (pt) { +/* validate byte code */ +if (kp_vm_validate_code (ks, pt, ks->stack)) + goto out; + + delta_time = (gettimeofday_ns () - start_time) /NSEC_PER_USEC ; +kp_verbose_printf (ks,"booting time: %d (us)\n" , delta_time); + +/* enter vm */ +kp_vm_call_proto (ks, pt); + } + + out: +kp_vm_exit (ks); +return ret; +} + + +static void print_version (void ) +{ +} + +static long ktap_ioctl (struct file *file,unsigned int cmd,unsigned long arg) +{ +ktap_option_t parm; +int ret; + +switch (cmd) { +case KTAP_CMD_IOC_VERSION : +print_version (); +return 0 ; +case KTAP_CMD_IOC_RUN : +/* +* must be root to run ktap script (at least for now) +* +* TODO: check perf_paranoid sysctl and allow non-root user +* to use ktap for tracing process(like uprobe) ? +*/ +if (!capable (CAP_SYS_ADMIN )) +return -EACCES ; + + ret =copy_from_user (&parm, (void __user *)arg, +sizeof (ktap_option_t)); +if (ret <0 ) +return -EFAULT ; + +return ktap_main (file, &parm); +default : +return -EINVAL ; + }; + +return 0 ; +} + +static const struct file_operations ktap_fops = { + .llseek = no_llseek, + .unlocked_ioctl = ktap_ioctl, +}; + +static long ktapvm_ioctl (struct file *file,unsigned int cmd,unsigned long arg) +{ +int new_fd, err; +struct file *new_file; + + new_fd =get_unused_fd (); +if (new_fd <0 ) +return new_fd; + + new_file =anon_inode_getfile ("[ktap]" , &ktap_fops,NULL ,O_RDWR ); +if (IS_ERR (new_file)) { + err =PTR_ERR (new_file); +put_unused_fd (new_fd); +return err; + } + + file->private_data =NULL ; +fd_install (new_fd, new_file); +return new_fd; +} + +static const struct file_operations ktapvm_fops = { + .owner =THIS_MODULE , + .unlocked_ioctl = ktapvm_ioctl, +}; + +int (*kp_ftrace_profile_set_filter)(struct perf_event *event,int event_id, +const char *filter_str); + +struct syscall_metadata **syscalls_metadata; + +/*TODO: kill this function in future */ +static int __init init_dummy_kernel_functions (void ) +{ +unsigned long *addr; + +/* +* ktap need symbol ftrace_profile_set_filter to set event filter, +* export it in future. +*/ +#ifdef CONFIG_PPC64 + kp_ftrace_profile_set_filter = + (void *)kallsyms_lookup_name (".ftrace_profile_set_filter" ); +#else + kp_ftrace_profile_set_filter = + (void *)kallsyms_lookup_name ("ftrace_profile_set_filter" ); +#endif +if (!kp_ftrace_profile_set_filter) { +pr_err ("ktap: cannot lookup ftrace_profile_set_filter " +"in kallsyms\n" ); +return -1 ; + } + +/* use syscalls_metadata for syscall event handling */ + addr = (void *)kallsyms_lookup_name ("syscalls_metadata" ); +if (!addr) { +pr_err ("ktap: cannot lookup syscalls_metadata in kallsyms\n" ); +return -1 ; + } + + syscalls_metadata = (struct syscall_metadata **)*addr; +return 0 ; +} + +static int __init init_ktap (void ) +{ +struct dentry *ktapvm_dentry; + +if (init_dummy_kernel_functions ()) +return -1 ; + + kp_dir_dentry =debugfs_create_dir ("ktap" ,NULL ); +if (!kp_dir_dentry) { +pr_err ("ktap: debugfs_create_dir failed\n" ); +return -1 ; + } + + ktapvm_dentry =debugfs_create_file ("ktapvm" ,0444 , kp_dir_dentry,NULL , + &ktapvm_fops); + +if (!ktapvm_dentry) { +pr_err ("ktapvm: cannot create ktapvm file\n" ); +debugfs_remove_recursive (kp_dir_dentry); +return -1 ; + } + +return 0 ; +} + +static void __exit exit_ktap(void) +{ +debugfs_remove_recursive (kp_dir_dentry); +} + +module_init (init_ktap); +module_exit (exit_ktap); + +MODULE_AUTHOR ("Jovi Zhangwei <[email protected]>" ); +MODULE_DESCRIPTION ("ktap" ); +MODULE_LICENSE ("GPL" ); + +int kp_max_loop_count =100000 ; +module_param_named (max_loop_count, kp_max_loop_count, int,S_IRUGO |S_IWUSR ); +MODULE_PARM_DESC (max_loop_count,"max loop execution count" ); +
tools/ktap/runtime/ktap.h new file +181 -0
181 lines not on this page · raw message
Signature · 1 line
1.8.1.4
Re: [PATCH 07/28] ktap: add runtime/ktap.[c|h]
Jovi Zhangwei <[email protected]> writes:
Quick review of the file.
tools/ktap/runtime/ktap.c line 38
Quoted 12 lines
tools/ktap/runtime/ktap.c new file +12
-0+#include <linux/version.h> +#if LINUX_VERSION_CODE <KERNEL_VERSION (3 ,1 ,0 ) +#error "Currently ktap don't support kernel older than 3.1" +#endif + +#if !CONFIG_EVENT_TRACING +#error "Please enable CONFIG_EVENT_TRACING before compile ktap" +#endif + +#if !CONFIG_PERF_EVENTS +#error "Please enable CONFIG_PERF_EVENTS before compile ktap" +#endif
This should be all in Kconfig
tools/ktap/runtime/ktap.c line 62
Quoted 8 lines
tools/ktap/runtime/ktap.c new file +8
-0+/* common helper function */ +long gettimeofday_ns (void ) +{ +struct timespec now; + +getnstimeofday (&now); +return now.tv_sec *NSEC_PER_SEC + now.tv_nsec; +}
This is ktime_get()
Or more likely you want something like trace_clock(), otherwise you may have problems on systems not using TSC.
tools/ktap/runtime/ktap.c line 74
Quoted 12 lines
tools/ktap/runtime/ktap.c new file +12
-0+ +static int load_trunk (ktap_option_t *parm,unsigned long **buff) +{ +int ret; +unsigned long *vmstart; + + vmstart =vmalloc (parm->trunk_len); +if (!vmstart) +return -ENOMEM ; + + ret =copy_from_user (vmstart, (void __user *)parm->trunk, + parm->trunk_len);
Needs unsigned length check if supplied from user space.
tools/ktap/runtime/ktap.c line 84
Quoted 10 lines
tools/ktap/runtime/ktap.c new file +10
-0+if (ret <0 ) { +vfree (vmstart); +return -EFAULT ; + } + + *buff = vmstart; +return 0 ; +} + +static struct dentry *kp_dir_dentry;
What lock protects that?
tools/ktap/runtime/ktap.c line 142
Quoted 8 lines
tools/ktap/runtime/ktap.c new file +8
-0+static long ktap_ioctl (struct file *file,unsigned int cmd,unsigned long arg) +{ +ktap_option_t parm; +int ret; + +switch (cmd) { +case KTAP_CMD_IOC_VERSION : +print_version ();
This seems odd. Should return the version instead?
tools/ktap/runtime/ktap.c line 149
Quoted 7 lines
tools/ktap/runtime/ktap.c new file +7
-0+return 0 ; +case KTAP_CMD_IOC_RUN : +/* +* must be root to run ktap script (at least for now) +* +* TODO: check perf_paranoid sysctl and allow non-root user +* to use ktap for tracing process(like uprobe) ?
This would need ensuring first that there is no possible way to crash the kernel. Probably very hard.
tools/ktap/runtime/ktap.c line 157
Quoted 8 lines
tools/ktap/runtime/ktap.c new file +8
-0+*/ +if (!capable (CAP_SYS_ADMIN )) +return -EACCES ; + + ret =copy_from_user (&parm, (void __user *)arg, +sizeof (ktap_option_t)); +if (ret <0 ) +return -EFAULT ;
The check is wrong, it should be != 0
tools/ktap/runtime/ktap.c line 210
Quoted 10 lines
tools/ktap/runtime/ktap.c new file +10
-0+struct syscall_metadata **syscalls_metadata; + +/*TODO: kill this function in future */ +static int __init init_dummy_kernel_functions (void ) +{ +unsigned long *addr; + +/* +* ktap need symbol ftrace_profile_set_filter to set event filter, +* export it in future.
Obviously you need to fix that. Just export the symbols.
tools/ktap/runtime/ktap.h line 31
Quoted 4 lines
tools/ktap/runtime/ktap.h new file +4
-0+ +extern struct syscall_metadata **syscalls_metadata; + +/* get from kernel/trace/trace.h */
Use that version instead.
Quoted 6 lines
tools/ktap/runtime/ktap.h new file +6
-0+static __always_inline int trace_get_context_bit(void) +{ +int bit; + +if (in_interrupt ()) { +if (in_nmi ())
tools/ktap/runtime/ktap.h line 47
Quoted 10 lines
tools/ktap/runtime/ktap.h new file +10
-0+ bit =0 ; + else if (in_irq()) + bit =1 ; + else + bit =2 ; + }else + bit =3 ; + +return bit; +}
-Andi
Signature · 1 line
[email protected] -- Speaking for myself only
Re: [PATCH 07/28] ktap: add runtime/ktap.[c|h]
Hi Andi,
Quoted 4 lines · Andi Kleen
Jovi Zhangwei <[email protected]> writes:
Quick review of the file.
Quoted 15 lines
tools/ktap/runtime/ktap.c line 38
Quoted 12 lines
tools/ktap/runtime/ktap.c new file +12
-0+#include <linux/version.h> +#if LINUX_VERSION_CODE <KERNEL_VERSION (3 ,1 ,0 ) +#error "Currently ktap don't support kernel older than 3.1" +#endif + +#if !CONFIG_EVENT_TRACING +#error "Please enable CONFIG_EVENT_TRACING before compile ktap" +#endif + +#if !CONFIG_PERF_EVENTS +#error "Please enable CONFIG_PERF_EVENTS before compile ktap" +#endif This should be all in
Kconfig
Will do.
Quoted 11 lines
tools/ktap/runtime/ktap.c line 62
Quoted 8 lines
tools/ktap/runtime/ktap.c new file +8
-0+/* common helper function */ +long gettimeofday_ns (void ) +{ +struct timespec now; + +getnstimeofday (&now); +return now.tv_sec *NSEC_PER_SEC + now.tv_nsec; +}This is
ktime_get()
Quoted 3 lines
Or more likely you want something like
trace_clock(), otherwise you may have problems on systems not using TSC.
I now find that function would not be safe to called from probe context, because it have seq lock in it, I will add a TODO tag in there.
(It seems Systemtap fix this issue by introduce its own time keeping logic)
Like as you said, we may also need other cheap timestamp for tracing, might trace_clock suit my need.
Quoted 15 lines
tools/ktap/runtime/ktap.c line 74
Quoted 12 lines
tools/ktap/runtime/ktap.c new file +12
-0+ +static int load_trunk (ktap_option_t *parm,unsigned long **buff) +{ +int ret; +unsigned long *vmstart; + + vmstart =vmalloc (parm->trunk_len); +if (!vmstart) +return -ENOMEM ; + + ret =copy_from_user (vmstart, (void __user *)parm->trunk, + parm->trunk_len);Needs unsigned length check if supplied from user space.
Quoted 13 lines
tools/ktap/runtime/ktap.c line 84
Quoted 10 lines
tools/ktap/runtime/ktap.c new file +10
-0+if (ret <0 ) { +vfree (vmstart); +return -EFAULT ; + } + + *buff = vmstart; +return 0 ; +} + +static struct dentry *kp_dir_dentry;What lock protects that?
This dentry would not change after insmod ktapvm.ko, so don't need any lock.
Quoted 11 lines
tools/ktap/runtime/ktap.c line 142
Quoted 8 lines
tools/ktap/runtime/ktap.c new file +8
-0+static long ktap_ioctl (struct file *file,unsigned int cmd,unsigned long arg) +{ +ktap_option_t parm; +int ret; + +switch (cmd) { +case KTAP_CMD_IOC_VERSION : +print_version ();This seems odd. Should return the version instead?
Yes, deleted.
Quoted 9 lines
tools/ktap/runtime/ktap.c line 149
Quoted 8 lines
tools/ktap/runtime/ktap.c new file +7
-0+return 0 ; +case KTAP_CMD_IOC_RUN : +/* +* must be root to run ktap script (at least for now) +* +* TODO: check perf_paranoid sysctl and allow non-root user +* to use ktap for tracing process(like uprobe) ?
Quoted 3 lines
This would need ensuring first that there is no possible way to crash the kernel. Probably very hard.
I agree it's hard to let non-root user to use these dynamic tracing tool, I will remove this comment to avoid confusion after we find a solution.
Quoted 11 lines
tools/ktap/runtime/ktap.c line 157
Quoted 8 lines
tools/ktap/runtime/ktap.c new file +8
-0+*/ +if (!capable (CAP_SYS_ADMIN )) +return -EACCES ; + + ret =copy_from_user (&parm, (void __user *)arg, +sizeof (ktap_option_t)); +if (ret <0 ) +return -EFAULT ;The check is wrong, it should be != 0
Fixed.
Quoted 4 lines
Quoted 4 lines
tools/ktap/runtime/ktap.c new file +4
-0+struct syscall_metadata **syscalls_metadata; + +/*TODO: kill this function in future */ +static int __init init_dummy_kernel_functions (void )
Quoted 9 lines
tools/ktap/runtime/ktap.c line 210
Quoted 6 lines
tools/ktap/runtime/ktap.c new file +6
-0+{ +unsigned long *addr; + +/* +* ktap need symbol ftrace_profile_set_filter to set event filter, +* export it in future. Obviously you need to fix that. Just export the symbols.
Yeah, will do after kernel community decide to merge ktap. :)
Quoted 7 lines
tools/ktap/runtime/ktap.h line 31
Quoted 4 lines
tools/ktap/runtime/ktap.h new file +4
-0+ +extern struct syscall_metadata **syscalls_metadata; + +/* get from kernel/trace/trace.h */ Use that version instead.
Same as above.
Quoted 8 lines
Quoted 8 lines
tools/ktap/runtime/ktap.h new file +8
-0+static __always_inline int trace_get_context_bit(void) +{ +int bit; + +if (in_interrupt ()) { +if (in_nmi ()) + bit =0 ; +else if (in_irq ())
Quoted 10 lines
tools/ktap/runtime/ktap.h line 47
Quoted 8 lines
tools/ktap/runtime/ktap.h new file +8
-0+ bit =1 ; + else + bit =2 ; + }else + bit =3 ; + +return bit; +}
Thanks for your review.
Jovi
Re: [PATCH 07/28] ktap: add runtime/ktap.[c|h]
Quoted 13 lines · Jovi Zhangwei
Quoted 4 lines
Quoted 4 lines
tools/ktap/runtime/ktap.c new file +4
-0+struct syscall_metadata **syscalls_metadata; + +/*TODO: kill this function in future */ +static int __init init_dummy_kernel_functions (void )Quoted 9 lines
tools/ktap/runtime/ktap.c line 210
Quoted 6 lines
tools/ktap/runtime/ktap.c new file +6
-0+{ +unsigned long *addr; + +/* +* ktap need symbol ftrace_profile_set_filter to set event filter, +* export it in future. Obviously you need to fix that. Just export the symbols.
Quoted 1 line
Yeah, will do after kernel community decide to merge ktap. :)
We already said we wanted it merged, so please send patches that do it properly, by exporting the needed symbols as part of the patch series.
thanks,
greg k-h
Re: [PATCH 07/28] ktap: add runtime/ktap.[c|h]
Quoted 14 lines · Greg Kroah-Hartman
Quoted 13 lines · Jovi Zhangwei
Quoted 13 lines
tools/ktap/runtime/ktap.c line 210
Quoted 10 lines
tools/ktap/runtime/ktap.c new file +10
-0+struct syscall_metadata **syscalls_metadata; + +/*TODO: kill this function in future */ +static int __init init_dummy_kernel_functions (void ) +{ +unsigned long *addr; + +/* +* ktap need symbol ftrace_profile_set_filter to set event filter, +* export it in future. Obviously you need to fix that. Just export the symbols.
Quoted 5 lines
Quoted 1 line
Yeah, will do after kernel community decide to merge ktap. :)
We already said we wanted it merged, so please send patches that do it properly, by exporting the needed symbols as part of the patch series.
Sure, I will fix it in next version.
Thanks.
Jovi
PATCH 8/28 ktap: add runtime/kp_bcread.[c|h]
In reply to Jovi ZhangweiFunction kp_bcread read bytecode from buff, and return a prototype of main ktap function.
ktap_proto_t *kp_bcread(ktap_state_t *ks, unsigned char *buff, int len)
--- tools/ktap/runtime/kp_bcread.c | 428 +++++++++++++++++++++++++++++++++++++++++ tools/ktap/runtime/kp_bcread.h | 6 + 2 files changed, 434 insertions(+) create mode 100644 tools/ktap/runtime/kp_bcread.c create mode 100644 tools/ktap/runtime/kp_bcread.h
tools/ktap/runtime/kp_bcread.c new file +428 -0
@@ -0,0 +1,428 @@ +/* +* Bytecode reader. +* +* This file is part of ktap by Jovi Zhangwei. +* +* Copyright (C) 2012-2014 Jovi Zhangwei <[email protected]>. +* +* Adapted from luajit and lua interpreter. +* Copyright (C) 2005-2014 Mike Pall. +* Copyright (C) 1994-2008 Lua.org, PUC-Rio. +* +* ktap is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* ktap is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "../include/ktap_types.h" +#include "../include/ktap_bc.h" +#include "../include/ktap_err.h" +#include "ktap.h" +#include "kp_obj.h" +#include "kp_str.h" +#include "kp_tab.h" + + +/* Context for bytecode reader. */ +typedef struct BCReadCtx { +ktap_state_t *ks; +int flags; +char *start; +char *p; +char *pe; +ktap_str_t *chunkname; +ktap_val_t *savetop; +}BCReadCtx ; + + +#define bcread_flags (ctx) (ctx)->flags +#define bcread_swap (ctx) \ + ((bcread_flags(ctx) & BCDUMP_F_BE) != KP_BE*BCDUMP_F_BE) +#define bcread_oldtop (ctx) (ctx)->savetop +#define bcread_savetop (ctx) (ctx)->savetop = (ctx)->ks->top; + +static inline uint32_t bswap (uint32_t x) +{ +return (uint32_t )__builtin_bswap32 ((int32_t )x); +} + +/* -- Input buffer handling ----------------------------------------------- */ + +/* Throw reader error. */ +static void bcread_error (BCReadCtx *ctx,ErrMsg em) +{ +kp_error (ctx->ks,"%s\n" ,err2msg (em)); +} + +/* Return memory block from buffer. */ +static inline uint8_t *bcread_mem (BCReadCtx *ctx,int len) +{ +uint8_t *p = (uint8_t *)ctx->p; + ctx->p += len; +kp_assert (ctx->p <= ctx->pe); +return p; +} + +/* Copy memory block from buffer. */ +static void bcread_block (BCReadCtx *ctx,void *q,int len) +{ +memcpy (q,bcread_mem (ctx, len), len); +} + +/* Read byte from buffer. */ +static inline uint32_t bcread_byte (BCReadCtx *ctx) +{ +kp_assert (ctx->p < ctx->pe); +return (uint32_t )(uint8_t )*ctx->p++; +} + +/* Read ULEB128 value from buffer. */ +static inline uint32_t bcread_uint32 (BCReadCtx *ctx) +{ +uint32_t v; +bcread_block (ctx, &v,sizeof (uint32_t )); +kp_assert (ctx->p <= ctx->pe); +return v; +} + +/* -- Bytecode reader ----------------------------------------------------- */ + +/* Read debug info of a prototype. */ +static void bcread_dbg (BCReadCtx *ctx,ktap_proto_t *pt,int sizedbg) +{ +void *lineinfo = (void *)proto_lineinfo (pt); + +bcread_block (ctx, lineinfo, sizedbg); +/* Swap lineinfo if the endianess differs. */ +if (bcread_swap (ctx) && pt->numline >=256 ) { +int i, n = pt->sizebc-1 ; +if (pt->numline <65536 ) { +uint16_t *p = (uint16_t *)lineinfo; +for (i =0 ; i < n; i++) + p[i] = (uint16_t )((p[i] >>8 )|(p[i] <<8 )); + }else { +uint32_t *p = (uint32_t *)lineinfo; +for (i =0 ; i < n; i++) + p[i] =bswap (p[i]); + } + } +} + +/* Find pointer to varinfo. */ +static const void *bcread_varinfo (ktap_proto_t *pt) +{ +const uint8_t *p =proto_uvinfo (pt); +int n = pt->sizeuv; +if (n) +while (*p++ || --n) ; +return p; +} + +/* Read a single constant key/value of a template table. */ +static int bcread_ktabk (BCReadCtx *ctx,ktap_val_t *o) +{ +int tp =bcread_uint32 (ctx); +if (tp >=BCDUMP_KTAB_STR ) { +int len = tp -BCDUMP_KTAB_STR ; +const char *p = (const char *)bcread_mem (ctx, len); +ktap_str_t *ts =kp_str_new (ctx->ks, p, len); +if (unlikely (!ts)) +return -ENOMEM ; + +set_string (o, ts); + }else if (tp ==BCDUMP_KTAB_NUM ) { +set_number (o, *(ktap_number *)bcread_mem (ctx, +sizeof (ktap_number))); + }else { +kp_assert (tp <=BCDUMP_KTAB_TRUE ); +setitype (o, ~tp); + } +return 0 ; +} + +/* Read a template table. */ +static ktap_tab_t *bcread_ktab (BCReadCtx *ctx) +{ +int narray =bcread_uint32 (ctx); +int nhash =bcread_uint32 (ctx); + +ktap_tab_t *t =kp_tab_new (ctx->ks, narray,hsize2hbits (nhash)); +if (!t) +return NULL ; + +if (narray) {/* Read array entries. */ +int i; +ktap_val_t *o = t->array; +for (i =0 ; i < narray; i++, o++) +if (bcread_ktabk (ctx, o)) +return NULL ; + } +if (nhash) {/* Read hash entries. */ +int i; +for (i =0 ; i < nhash; i++) { +ktap_val_t key; +ktap_val_t val; +if (bcread_ktabk (ctx, &key)) +return NULL ; +kp_assert (!is_nil (&key)); +if (bcread_ktabk (ctx, &val)) +return NULL ; +kp_tab_set (ctx->ks, t, &key, &val); + } + } +return t; +} + +/* Read GC constants(string, table, child proto) of a prototype. */ +static int bcread_kgc (BCReadCtx *ctx,ktap_proto_t *pt,int sizekgc) +{ +ktap_obj_t **kr = (ktap_obj_t **)pt->k - (ptrdiff_t )sizekgc; +int i; + +for (i =0 ; i < sizekgc; i++, kr++) { +int tp =bcread_uint32 (ctx); +if (tp >=BCDUMP_KGC_STR ) { +int len = tp -BCDUMP_KGC_STR ; +const char *p = (const char *)bcread_mem (ctx, len); + *kr =(ktap_obj_t *)kp_str_new (ctx->ks, p, len); +if (unlikely (!*kr)) +return -1 ; + }else if (tp ==BCDUMP_KGC_TAB ) { + *kr = (ktap_obj_t *)bcread_ktab (ctx); +if (unlikely (!*kr)) +return -1 ; + }else if (tp ==BCDUMP_KGC_CHILD ){ +ktap_state_t *ks = ctx->ks; +if (ks->top <=bcread_oldtop (ctx)) { +bcread_error (ctx,KP_ERR_BCBAD ); +return -1 ; + } + ks->top--; + *kr = (ktap_obj_t *)ptvalue (ks->top); + }else { +bcread_error (ctx,KP_ERR_BCBAD ); +return -1 ; + } + } + +return 0 ; +} + +/* Read number constants of a prototype. */ +static void bcread_knum (BCReadCtx *ctx,ktap_proto_t *pt,int sizekn) +{ +int i; +ktap_val_t *o = pt->k; + +for (i =0 ; i < sizekn; i++, o++) { +set_number (o, *(ktap_number *)bcread_mem (ctx, +sizeof (ktap_number))); + } +} + +/* Read bytecode instructions. */ +static void bcread_bytecode (BCReadCtx *ctx,ktap_proto_t *pt,int sizebc) +{ +BCIns *bc =proto_bc (pt); + bc[0 ] =BCINS_AD ((pt->flags &PROTO_VARARG ) ?BC_FUNCV :BC_FUNCF , + pt->framesize,0 ); +bcread_block (ctx, bc+1 , (sizebc-1 )*(int )sizeof (BCIns)); +/* Swap bytecode instructions if the endianess differs. */ +if (bcread_swap (ctx)) { +int i; +for (i =1 ; i < sizebc; i++) bc[i] =bswap (bc[i]); + } +} + +/* Read upvalue refs. */ +static void bcread_uv (BCReadCtx *ctx,ktap_proto_t *pt,int sizeuv) +{ +if (sizeuv) { +uint16_t *uv =proto_uv (pt); +bcread_block (ctx, uv, sizeuv*2 ); +/* Swap upvalue refs if the endianess differs. */ +if (bcread_swap (ctx)) { +int i; +for (i =0 ; i < sizeuv; i++) + uv[i] = (uint16_t )((uv[i] >>8 )|(uv[i] <<8 )); + } + } +} + +/* Read a prototype. */ +static ktap_proto_t *bcread_proto (BCReadCtx *ctx) +{ +ktap_proto_t *pt; +int framesize, numparams, flags; +int sizeuv, sizekgc, sizekn, sizebc, sizept; +int ofsk, ofsuv, ofsdbg; +int sizedbg =0 ; +BCLine firstline =0 , numline =0 ; + +/* Read prototype header. */ + flags =bcread_byte (ctx); + numparams =bcread_byte (ctx); + framesize =bcread_byte (ctx); + sizeuv =bcread_byte (ctx); + sizekgc =bcread_uint32 (ctx); + sizekn =bcread_uint32 (ctx); + sizebc =bcread_uint32 (ctx) +1 ; +if (!(bcread_flags (ctx) &BCDUMP_F_STRIP )) { + sizedbg =bcread_uint32 (ctx); +if (sizedbg) { + firstline =bcread_uint32 (ctx); + numline =bcread_uint32 (ctx); + } + } + +/* Calculate total size of prototype including all colocated arrays. */ + sizept = (int )sizeof (ktap_proto_t) + sizebc * (int )sizeof (BCIns) + + sizekgc * (int )sizeof (ktap_obj_t *); + sizept = (sizept + (int )sizeof (ktap_val_t)-1 ) & + ~((int )sizeof (ktap_val_t)-1 ); + ofsk = sizept; sizept += sizekn*(int )sizeof (ktap_val_t); + ofsuv = sizept; sizept += ((sizeuv+1 )&~1 )*2 ; + ofsdbg = sizept; sizept += sizedbg; + +/* Allocate prototype object and initialize its fields. */ + pt = (ktap_proto_t *)kp_obj_new (ctx->ks, (int )sizept); + pt->gct = ~KTAP_TPROTO ; + pt->numparams = (uint8_t )numparams; + pt->framesize = (uint8_t )framesize; + pt->sizebc = sizebc; + pt->k = (char *)pt + ofsk; + pt->uv = (char *)pt + ofsuv; + pt->sizekgc =0 ;/* Set to zero until fully initialized. */ + pt->sizekn = sizekn; + pt->sizept = sizept; + pt->sizeuv = (uint8_t )sizeuv; + pt->flags = (uint8_t )flags; + pt->chunkname = ctx->chunkname; + +/* Close potentially uninitialized gap between bc and kgc. */ + *(uint32_t *)((char *)pt + ofsk -sizeof (ktap_obj_t *)*(sizekgc+1 )) + =0 ; + +/* Read bytecode instructions and upvalue refs. */ +bcread_bytecode (ctx, pt, sizebc); +bcread_uv (ctx, pt, sizeuv); + +/* Read constants. */ +if (bcread_kgc (ctx, pt, sizekgc)) +return NULL ; + pt->sizekgc = sizekgc; +bcread_knum (ctx, pt, sizekn); + +/* Read and initialize debug info. */ + pt->firstline = firstline; + pt->numline = numline; +if (sizedbg) { +int sizeli = (sizebc-1 ) << (numline <256 ?0 : + numline <65536 ?1 :2 ); + pt->lineinfo = (char *)pt + ofsdbg; + pt->uvinfo = (char *)pt + ofsdbg + sizeli; +bcread_dbg (ctx, pt, sizedbg); + pt->varinfo = (void *)bcread_varinfo (pt); + }else { + pt->lineinfo =NULL ; + pt->uvinfo =NULL ; + pt->varinfo =NULL ; + } +return pt; +} + +/* Read and check header of bytecode dump. */ +static int bcread_header (BCReadCtx *ctx) +{ +uint32_t flags; + +if (bcread_byte (ctx) !=BCDUMP_HEAD1 || +bcread_byte (ctx) !=BCDUMP_HEAD2 || +bcread_byte (ctx) !=BCDUMP_HEAD3 || +bcread_byte (ctx) !=BCDUMP_VERSION ) +return -1 ; + +bcread_flags (ctx) = flags =bcread_byte (ctx); + +if ((flags & ~(BCDUMP_F_KNOWN )) !=0 ) +return -1 ; + +if ((flags &BCDUMP_F_FFI )) { +return -1 ; + } + +if ((flags &BCDUMP_F_STRIP )) { + ctx->chunkname =kp_str_newz (ctx->ks,"striped" ); + }else { +int len =bcread_uint32 (ctx); + ctx->chunkname =kp_str_new (ctx->ks, + (const char *)bcread_mem (ctx, len), len); + } + +if (unlikely (!ctx->chunkname)) +return -1 ; + +return 0 ; +} + +/* Read a bytecode dump. */ +ktap_proto_t *kp_bcread (ktap_state_t *ks,unsigned char *buff,int len) +{ +BCReadCtx ctx; + + ctx.ks = ks; + ctx.p = buff; + ctx.pe = buff + len; + + ctx.start = buff; + +bcread_savetop (&ctx); +/* Check for a valid bytecode dump header. */ +if (bcread_header (&ctx)) { +bcread_error (&ctx,KP_ERR_BCFMT ); +return NULL ; + } + +for (;;) {/* Process all prototypes in the bytecode dump. */ +ktap_proto_t *pt; +int len; +const char *startp; +/* Read length. */ +if (ctx.p < ctx.pe && ctx.p[0 ] ==0 ) {/* Shortcut EOF. */ + ctx.p++; +break ; + } + len =bcread_uint32 (&ctx); +if (!len) +break ;/* EOF */ + startp = ctx.p; + pt =bcread_proto (&ctx); +if (!pt) +return NULL ; +if (ctx.p != startp + len) { +bcread_error (&ctx,KP_ERR_BCBAD ); +return NULL ; + } +set_proto (ks->top, pt); +incr_top (ks); + } +if ((int32_t )(2 *(uint32_t )(ctx.pe - ctx.p)) >0 || + ks->top-1 !=bcread_oldtop (&ctx)) { +bcread_error (&ctx,KP_ERR_BCBAD ); +return NULL ; + } + +/* Pop off last prototype. */ + ks->top--; +return ptvalue (ks->top); +} +
tools/ktap/runtime/kp_bcread.h new file +6 -0
@@ -0,0 +1,6 @@ +#ifndef __KTAP_BCREAD_H__ +#define __KTAP_BCREAD_H__ + +ktap_proto_t *kp_bcread (ktap_state_t *ks,unsigned char *buff,int len); + +#endif /* __KTAP_BCREAD_H__ */
Signature · 1 line
1.8.1.4
PATCH 9/28 ktap: add runtime/kp_vm.[c|h]
In reply to Jovi Zhangweikp_vm.c is ktap runtime execution engine, it execute all bytecodes and take care core vm stuff.
Exposed functions:
1). kp_vm_new_state: allocate and init main context.
2). kp_vm_exit: ktap main thread exit
3). kp_vm_register_lib: library and built-in functions register interface
4). kp_vm_validate_code:validate bytecode before execute
5). kp_vm_call: dispatch and execute bytecode, called from probe context
6). kp_vm_call_proto: called with a new proto, called when start.
ktap runtime pre-allocate context(ktap_state_t) for each cpu and each probe context(process, irq, sirq, nmi), and pre-allocate ktap stack memory for each ktap_state.
The key function is kp_vm_call, it dispatch and execute bytecodes. Computed goto is used for fast dispatch instead of big switch. All bytecode check its type in runtime, it will return if type mismatch.
Hotloop is checked when execute loop related bytecode, to forbid deadloop, it limit can be set by module parameter.
--- tools/ktap/runtime/kp_vm.c | 1759 ++++++++++++++++++++++++++++++++++++++++++++ tools/ktap/runtime/kp_vm.h | 43 ++ 2 files changed, 1802 insertions(+) create mode 100644 tools/ktap/runtime/kp_vm.c create mode 100644 tools/ktap/runtime/kp_vm.h
tools/ktap/runtime/kp_vm.c new file +1759 -0
@@ -0,0 +1,1759 @@ +/* +* kp_vm.c - ktap script virtual machine in Linux kernel +* +* This file is part of ktap by Jovi Zhangwei. +* +* Copyright (C) 2012-2014 Jovi Zhangwei <[email protected]>. +* +* Adapted from luajit and lua interpreter. +* Copyright (C) 2005-2014 Mike Pall. +* Copyright (C) 1994-2008 Lua.org, PUC-Rio. +* +* ktap is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* ktap is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include <linux/slab.h> +#include <linux/ftrace_event.h> +#include <linux/signal.h> +#include <linux/sched.h> +#include <linux/uaccess.h> +#include "../include/ktap_types.h" +#include "../include/ktap_bc.h" +#include "ktap.h" +#include "kp_obj.h" +#include "kp_str.h" +#include "kp_mempool.h" +#include "kp_tab.h" +#include "kp_transport.h" +#include "kp_vm.h" +#include "kp_events.h" + +#define KTAP_MIN_RESERVED_STACK_SIZE 20 +#define KTAP_STACK_SIZE 120/* enlarge this value for big stack */ +#define KTAP_STACK_SIZE_BYTES (KTAP_STACK_SIZE * sizeof(ktap_val_t)) + +#define KTAP_PERCPU_BUFFER_SIZE (3 * PAGE_SIZE) + +static ktap_cfunction gfunc_get (ktap_state_t *ks,int idx); +static int gfunc_getidx (ktap_global_state_t *g,ktap_cfunction cfunc); + +static ktap_str_t *str_concat (ktap_state_t *ks,StkId top,int start,int end) +{ +int i, len =0 ; +ktap_str_t *ts; +char *ptr, *buffer; + +for (i = start; i <= end; i++) { +if (!is_string (top + i)) { +kp_error (ks,"cannot concat non-string\n" ); +return NULL ; + } + + len +=rawtsvalue (top + i)->len; + } + +if (len >=KTAP_PERCPU_BUFFER_SIZE ) { +kp_error (ks,"Error: too long string concatenation\n" ); +return NULL ; + } + +preempt_disable_notrace (); + + buffer =kp_this_cpu_print_buffer (ks); + ptr = buffer; + +for (i = start; i <= end; i++) { +int len =rawtsvalue (top + i)->len; +strncpy (ptr,svalue (top + i), len); + ptr += len; + } + ts =kp_str_new (ks, buffer, len); + +preempt_enable_notrace (); + +return ts; +} + +static ktap_upval_t *findupval (ktap_state_t *ks,StkId slot) +{ +ktap_global_state_t *g =G (ks); +ktap_upval_t **pp = &ks->openupval; +ktap_upval_t *p; +ktap_upval_t *uv; + +while (*pp !=NULL && (p = *pp)->v >= slot) { +if (p->v == slot) {/* found a corresponding upvalue? */ +return p; + } + pp = (ktap_upval_t **)&p->nextgc; + } + +/* not found: create a new one */ + uv = (ktap_upval_t *)kp_malloc (ks,sizeof (ktap_upval_t)); +if (!uv) +return NULL ; + uv->gct = ~KTAP_TUPVAL ; + uv->closed =0 ;/* still open */ + uv->v = slot;/* current value lives in the stack */ +/* Insert into sorted list of open upvalues. */ + uv->nextgc = (ktap_obj_t *)*pp; + *pp = uv; + uv->prev = &g->uvhead;/* double link it in `uvhead' list */ + uv->next = g->uvhead.next; + uv->next->prev = uv; + g->uvhead.next = uv; +return uv; +} + +static void unlinkupval (ktap_upval_t *uv) +{ + uv->next->prev = uv->prev;/* remove from `uvhead' list */ + uv->prev->next = uv->next; +} + +void kp_freeupval (ktap_state_t *ks,ktap_upval_t *uv) +{ +if (!uv->closed)/* is it open? */ +unlinkupval (uv);/* remove from open list */ +kp_free (ks, uv);/* free upvalue */ +} + +/* close upvals */ +static void func_closeuv (ktap_state_t *ks,StkId level) +{ +ktap_upval_t *uv; +ktap_global_state_t *g =G (ks); +while (ks->openupval !=NULL && + (uv = ks->openupval)->v >= level) { +ktap_obj_t *o =obj2gco (uv); +/* remove from `open' list */ + ks->openupval = (ktap_upval_t *)uv->nextgc; +unlinkupval (uv);/* remove upvalue from 'uvhead' list */ +set_obj (&uv->tv, uv->v);/* move value to upvalue slot */ + uv->v = &uv->tv;/* now current value lives here */ + uv->closed =1 ; +gch (o)->nextgc = g->allgc;/* link upvalue into 'allgc' list */ + g->allgc = o; + } +} + +#define SIZE_KTAP_FUNC (n) (sizeof(ktap_func_t) - sizeof(ktap_obj_t *) + \ + sizeof(ktap_obj_t *) * (n)) +static ktap_func_t *func_new_empty (ktap_state_t *ks,ktap_proto_t *pt) +{ +ktap_func_t *fn; + +/* only mainthread can create new function */ +if (ks !=G (ks)->mainthread) { +kp_error (ks,"only mainthread can create function\n" ); +return NULL ; + } + + fn = (ktap_func_t *)kp_obj_new (ks,SIZE_KTAP_FUNC (pt->sizeuv)); +if (!fn) +return NULL ; + fn->gct = ~KTAP_TFUNC ; + fn->nupvalues =0 ;/* Set to zero until upvalues are initialized. */ + fn->pc =proto_bc (pt); + fn->p = pt; + +return fn; +} + +static ktap_func_t *func_new (ktap_state_t *ks,ktap_proto_t *pt, +ktap_func_t *parent,ktap_val_t *base) +{ +ktap_func_t *fn; +int nuv = pt->sizeuv, i; + + fn =func_new_empty (ks, pt); +if (!fn) +return NULL ; + + fn->nupvalues = nuv; +for (i =0 ; i < nuv; i++) { +uint32_t v =proto_uv (pt)[i]; +ktap_upval_t *uv; + +if (v &PROTO_UV_LOCAL ) { + uv =findupval (ks, base + (v &0xff )); +if (!uv) +return NULL ; + uv->immutable = ((v /PROTO_UV_IMMUTABLE ) &1 ); + }else { + uv = parent->upvals[v]; + } + fn->upvals[i] = uv; + } +return fn; +} + +static inline int checkstack (ktap_state_t *ks,int n) +{ +if (unlikely (ks->stack_last - ks->top <= n)) { +kp_error (ks,"stack overflow, please enlarge stack size\n" ); +return -1 ; + } +return 0 ; +} + +static StkId adjust_varargs (ktap_state_t *ks,ktap_proto_t *p,int actual) +{ +int i; +int nfixargs = p->numparams; +StkId base, fixed; + +/* move fixed parameters to final position */ + fixed = ks->top - actual;/* first fixed argument */ + base = ks->top;/* final position of first argument */ + +for (i=0 ; i < nfixargs; i++) { +set_obj (ks->top++, fixed + i); +set_nil (fixed + i); + } + +return base; +} + +static void poscall (ktap_state_t *ks,StkId func,StkId first_result, +int wanted) +{ +int i; + +for (i = wanted; i !=0 && first_result < ks->top; i--) +set_obj (func++, first_result++); + +while (i-- >0 ) +set_nil (func++); +} + +void kp_vm_call_proto (ktap_state_t *ks,ktap_proto_t *pt) +{ +ktap_func_t *fn; + + fn =func_new_empty (ks, pt); +if (!fn) +return ; +set_func (ks->top++, fn); +kp_vm_call (ks, ks->top -1 ,0 ); +} + +/* +* Hot loop detaction +* +* Check hot loop detaction in three cases: +* 1. jmp -x: this happens in 'while (expr) { ... }' +* 2. FORPREP-FORLOOP +* 3. TFORCALL-TFORLOOP +*/ +static __always_inline int check_hot_loop(ktap_state_t *ks, int loop_count) +{ +if (unlikely (loop_count == kp_max_loop_count)) { +kp_error (ks,"loop execute count exceed max limit(%d)\n" , + kp_max_loop_count); +return -1 ; + } + +return 0 ; +} + +#define dojump (i, e) { pc += (int)bc_d(i) - BCBIAS_J + e; } +#define donextjump { instr = *pc; dojump(instr, 1); } + +#define NUMADD (a, b) ((a) + (b)) +#define NUMSUB (a, b) ((a) - (b)) +#define NUMMUL (a, b) ((a) * (b)) +#define NUMDIV (a, b) ((a) / (b)) +#define NUMUNM (a) (-(a)) +#define NUMEQ (a, b) ((a) == (b)) +#define NUMLT (a, b) ((a) < (b)) +#define NUMLE (a, b) ((a) <= (b)) +#define NUMMOD (a, b) ((a) % (b)) + +#define arith_VV (ks, op) { \ + ktap_val_t *rb = RB; \ + ktap_val_t *rc = RC; \ + if (is_number(rb) && is_number(rc)) { \ + ktap_number nb = nvalue(rb), nc = nvalue(rc); \ + set_number(RA, op(nb, nc)); \ + } else { \ + kp_puts(ks, "Error: Cannot make arith operation\n"); \ + return; \ + } } + +#define arith_VN (ks, op) { \ + ktap_val_t *rb = RB; \ + if (is_number(rb)) { \ + ktap_number nb = nvalue(rb);\ + ktap_number nc = nvalue((ktap_val_t *)kbase + bc_c(instr));\ + set_number(RA, op(nb, nc)); \ + } else { \ + kp_puts(ks, "Error: Cannot make arith operation\n"); \ + return; \ + } } + +#define arith_NV (ks, op) { \ + ktap_val_t *rb = RB; \ + if (is_number(rb)) { \ + ktap_number nb = nvalue(rb);\ + ktap_number nc = nvalue((ktap_val_t *)kbase + bc_c(instr));\ + set_number(RA, op(nc, nb)); \ + } else { \ + kp_puts(ks, "Error: Cannot make arith operation\n"); \ + return; \ + } } + + +static const char *const bc_names[] = { +#define BCNAME(name, ma, mb, mc, mt) #name, +BCDEF (BCNAME ) +#undef BCNAME + NULL +}; + + +/* +* ktap bytecode interpreter routine +* +* +* kp_vm_call only can be used for: +* 1). call ktap function, not light C function +* 2). accept fixed argument function +*/ +void kp_vm_call (ktap_state_t *ks,StkId func,int nresults) +{ +int loop_count =0 ; +ktap_func_t *fn; +ktap_proto_t *pt; +ktap_obj_t **kbase; +unsigned int instr, op; +const unsigned int *pc; +StkId base;/* stack pointer */ +int multres =0 ;/* temp varible */ +ktap_tab_t *gtab =G (ks)->gtab; + +/* use computed goto for opcode dispatch */ + +static void *dispatch_table[] = { +#define BCNAME(name, ma, mb, mc, mt) &&DO_BC_##name, +BCDEF (BCNAME ) +#undef BCNAME + }; + +#define DISPATCH () \ + do { \ + instr = *(pc++); \ + op = bc_op(instr); \ + goto *dispatch_table[op]; \ + } while (0) + +#define RA (base + bc_a(instr)) +#define RB (base + bc_b(instr)) +#define RC (base + bc_c(instr)) +#define RD (base + bc_d(instr)) +#define RKD ((ktap_val_t *)kbase + bc_d(instr)) + +/*TODO: fix argument number mismatch, example: sort cmp closure */ + + fn =clvalue (func); + pt = fn->p; + kbase = fn->p->k; + base = func +1 ; + pc =proto_bc (pt) +1 ; + ks->top = base + pt->framesize; + func->pcr =0 ;/* no previous frame */ + +/* main loop of interpreter */ +DISPATCH (); + +while (1 ) { + DO_BC_ISLT:/* Jump if A < D */ +if (!is_number (RA ) || !is_number (RD )) { +kp_error (ks,"compare with non-number\n" ); +return ; + } + +if (nvalue (RA ) >=nvalue (RD )) + pc++; +else + donextjump; +DISPATCH (); + DO_BC_ISGE:/* Jump if A >= D */ +if (!is_number (RA ) || !is_number (RD )) { +kp_error (ks,"compare with non-number\n" ); +return ; + } + +if (nvalue (RA ) <nvalue (RD )) + pc++; +else + donextjump; +DISPATCH (); + DO_BC_ISLE:/* Jump if A <= D */ +if (!is_number (RA ) || !is_number (RD )) { +kp_error (ks,"compare with non-number\n" ); +return ; + } + +if (nvalue (RA ) >nvalue (RD )) + pc++; +else + donextjump; +DISPATCH (); + DO_BC_ISGT:/* Jump if A > D */ +if (!is_number (RA ) || !is_number (RD )) { +kp_error (ks,"compare with non-number\n" ); +return ; + } + +if (nvalue (RA ) <=nvalue (RD )) + pc++; +else + donextjump; +DISPATCH (); + DO_BC_ISEQV:/* Jump if A = D */ +if (!kp_obj_equal (RA ,RD )) + pc++; +else + donextjump; +DISPATCH (); + DO_BC_ISNEV:/* Jump if A != D */ +if (kp_obj_equal (RA ,RD )) + pc++; +else + donextjump; +DISPATCH (); + DO_BC_ISEQS: {/* Jump if A = D */ +int idx = ~bc_d (instr); + +if (!is_string (RA ) || +rawtsvalue (RA ) != (ktap_str_t *)kbase[idx]) + pc++; +else + donextjump; +DISPATCH (); + } + DO_BC_ISNES: {/* Jump if A != D */ +int idx = ~bc_d (instr); + +if (is_string (RA ) && +rawtsvalue (RA ) == (ktap_str_t *)kbase[idx]) + pc++; +else + donextjump; +DISPATCH (); + } + DO_BC_ISEQN:/* Jump if A = D */ +if (!is_number (RA ) ||nvalue (RA ) !=nvalue (RKD )) + pc++; +else + donextjump; +DISPATCH (); + DO_BC_ISNEN:/* Jump if A != D */ +if (is_number (RA ) &&nvalue (RA ) ==nvalue (RKD )) + pc++; +else + donextjump; +DISPATCH (); + DO_BC_ISEQP:/* Jump if A = D */ +if (itype (RA ) != ~bc_d (instr)) + pc++; +else + donextjump; +DISPATCH (); + DO_BC_ISNEP:/* Jump if A != D */ +if (itype (RA ) == ~bc_d (instr)) + pc++; +else + donextjump; +DISPATCH (); + DO_BC_ISTC:/* Copy D to A and jump, if D is true */ +if (itype (RD ) ==KTAP_TNIL ||itype (RD ) ==KTAP_TFALSE ) + pc++; +else { +set_obj (RA ,RD ); + donextjump; + } +DISPATCH (); + DO_BC_ISFC:/* Copy D to A and jump, if D is false */ +if (itype (RD ) !=KTAP_TNIL &&itype (RD ) !=KTAP_TFALSE ) + pc++; +else { +set_obj (RA ,RD ); + donextjump; + } +DISPATCH (); + DO_BC_IST:/* Jump if D is true */ +if (itype (RD ) ==KTAP_TNIL ||itype (RD ) ==KTAP_TFALSE ) + pc++; +else + donextjump; +DISPATCH (); + DO_BC_ISF:/* Jump if D is false */ +/* only nil and false are considered false, +* all other values are true */ +if (itype (RD ) !=KTAP_TNIL &&itype (RD ) !=KTAP_TFALSE ) + pc++; +else + donextjump; +DISPATCH (); + DO_BC_ISTYPE:/* generated by genlibbc, not compiler; not used now */ + DO_BC_ISNUM: +return ; + DO_BC_MOV:/* Copy D to A */ +set_obj (RA ,RD ); +DISPATCH (); + DO_BC_NOT:/* Set A to boolean not of D */ +if (itype (RD ) ==KTAP_TNIL ||itype (RD ) ==KTAP_TFALSE ) +setitype (RA ,KTAP_TTRUE ); +else +setitype (RA ,KTAP_TFALSE ); + +DISPATCH (); + DO_BC_UNM:/* Set A to -D (unary minus) */ +if (!is_number (RD )) { +kp_error (ks,"use '-' operator on non-number\n" ); +return ; + } + +set_number (RA , -nvalue (RD )); +DISPATCH (); + DO_BC_ADDVN:/* A = B + C */ +arith_VN (ks,NUMADD ); +DISPATCH (); + DO_BC_SUBVN:/* A = B - C */ +arith_VN (ks,NUMSUB ); +DISPATCH (); + DO_BC_MULVN:/* A = B * C */ +arith_VN (ks,NUMMUL ); +DISPATCH (); + DO_BC_DIVVN:/* A = B / C */ +/* divide 0 checking */ +if (!nvalue ((ktap_val_t *)kbase +bc_c (instr))) { +kp_error (ks,"divide 0 arith operation\n" ); +return ; + } +arith_VN (ks,NUMDIV ); +DISPATCH (); + DO_BC_MODVN:/* A = B % C */ +/* divide 0 checking */ +if (!nvalue ((ktap_val_t *)kbase +bc_c (instr))) { +kp_error (ks,"mod 0 arith operation\n" ); +return ; + } +arith_VN (ks,NUMMOD ); +DISPATCH (); + DO_BC_ADDNV:/* A = C + B */ +arith_NV (ks,NUMADD ); +DISPATCH (); + DO_BC_SUBNV:/* A = C - B */ +arith_NV (ks,NUMSUB ); +DISPATCH (); + DO_BC_MULNV:/* A = C * B */ +arith_NV (ks,NUMMUL ); +DISPATCH (); + DO_BC_DIVNV:/* A = C / B */ +/* divide 0 checking */ +if (!nvalue (RB )){ +kp_error (ks,"divide 0 arith operation\n" ); +return ; + } +arith_NV (ks,NUMDIV ); +DISPATCH (); + DO_BC_MODNV:/* A = C % B */ +/* divide 0 checking */ +if (!nvalue (RB )){ +kp_error (ks,"mod 0 arith operation\n" ); +return ; + } +arith_NV (ks,NUMMOD ); +DISPATCH (); + DO_BC_ADDVV:/* A = B + C */ +arith_VV (ks,NUMADD ); +DISPATCH (); + DO_BC_SUBVV:/* A = B - C */ +arith_VV (ks,NUMSUB ); +DISPATCH (); + DO_BC_MULVV:/* A = B * C */ +arith_VV (ks,NUMMUL ); +DISPATCH (); + DO_BC_DIVVV:/* A = B / C */ +arith_VV (ks,NUMDIV ); +DISPATCH (); + DO_BC_MODVV:/* A = B % C */ +arith_VV (ks,NUMMOD ); +DISPATCH (); + DO_BC_POW:/* A = B ^ C, rejected */ +return ; + DO_BC_CAT: {/* A = B .. ~ .. C */ +/* The CAT instruction concatenates all values in +* variable slots B to C inclusive. */ +ktap_str_t *ts =str_concat (ks, base,bc_b (instr), +bc_c (instr)); +if (!ts) +return ; + +set_string (RA , ts); +DISPATCH (); + } + DO_BC_KSTR: {/* Set A to string constant D */ +int idx = ~bc_d (instr); +set_string (RA , (ktap_str_t *)kbase[idx]); +DISPATCH (); + } + DO_BC_KCDATA:/* not used now */ +DISPATCH (); + DO_BC_KSHORT:/* Set A to 16 bit signed integer D */ +set_number (RA ,bc_d (instr)); +DISPATCH (); + DO_BC_KNUM:/* Set A to number constant D */ +set_number (RA ,nvalue (RKD )); +DISPATCH (); + DO_BC_KPRI:/* Set A to primitive D */ +setitype (RA , ~bc_d (instr)); +DISPATCH (); + DO_BC_KNIL: {/* Set slots A to D to nil */ +int i; +for (i =0 ; i <=bc_d (instr) -bc_a (instr); i++) { +set_nil (RA + i); + } +DISPATCH (); + } + DO_BC_UGET:/* Set A to upvalue D */ +set_obj (RA , fn->upvals[bc_d (instr)]->v); +DISPATCH (); + DO_BC_USETV:/* Set upvalue A to D */ +set_obj (fn->upvals[bc_a (instr)]->v,RD ); +DISPATCH (); + DO_BC_UINCV: {/* upvalus[A] += D */ +ktap_val_t *v = fn->upvals[bc_a (instr)]->v; +if (unlikely (!is_number (RD ) || !is_number (v))) { +kp_error (ks,"use '+=' on non-number\n" ); +return ; + } +set_number (v,nvalue (v) +nvalue (RD )); +DISPATCH (); + } + DO_BC_USETS: {/* Set upvalue A to string constant D */ +int idx = ~bc_d (instr); +set_string (fn->upvals[bc_a (instr)]->v, + (ktap_str_t *)kbase[idx]); +DISPATCH (); + } + DO_BC_USETN:/* Set upvalue A to number constant D */ +set_number (fn->upvals[bc_a (instr)]->v,nvalue (RKD )); +DISPATCH (); + DO_BC_UINCN: {/* upvalus[A] += D */ +ktap_val_t *v = fn->upvals[bc_a (instr)]->v; +if (unlikely (!is_number (v))) { +kp_error (ks,"use '+=' on non-number\n" ); +return ; + } +set_number (v,nvalue (v) +nvalue (RKD )); +DISPATCH (); + } + DO_BC_USETP:/* Set upvalue A to primitive D */ +setitype (fn->upvals[bc_a (instr)]->v, ~bc_d (instr)); +DISPATCH (); + DO_BC_UCLO:/* Close upvalues for slots . rbase and jump to target D */ +if (ks->openupval !=NULL ) +func_closeuv (ks,RA ); +dojump (instr,0 ); +DISPATCH (); + DO_BC_FNEW: { +/* Create new closure from prototype D and store it in A */ +int idx = ~bc_d (instr); +ktap_func_t *subfn =func_new (ks, (ktap_proto_t *)kbase[idx], + fn, base); +if (unlikely (!subfn)) +return ; +set_func (RA , subfn); +DISPATCH (); + } + DO_BC_TNEW: {/* Set A to new table with size D */ +/* +* preallocate default narr and nrec, +* op_b and op_c is not used +* This would allocate more memory for some static table. +*/ +ktap_tab_t *t =kp_tab_new_ah (ks,0 ,0 ); +if (unlikely (!t)) +return ; +set_table (RA , t); +DISPATCH (); + } + DO_BC_TDUP: {/* Set A to duplicated template table D */ +int idx = ~bc_d (instr); +ktap_tab_t *t =kp_tab_dup (ks, (ktap_tab_t *)kbase[idx]); +if (!t) +return ; +set_table (RA , t); +DISPATCH (); + } + DO_BC_GGET: {/* A = _G[D] */ +int idx = ~bc_d (instr); +kp_tab_getstr (gtab, (ktap_str_t *)kbase[idx],RA ); +DISPATCH (); + } + DO_BC_GSET:/* _G[D] = A, rejected. */ + DO_BC_GINC:/* _G[D] += A, rejected. */ +return ; + DO_BC_TGETV:/* A = B[C] */ +if (unlikely (!is_table (RB ))) { +kp_error (ks,"get key from non-table\n" ); +return ; + } + +kp_tab_get (ks,hvalue (RB ),RC ,RA ); +DISPATCH (); + DO_BC_TGETS: {/* A = B[C] */ +int idx = ~bc_c (instr); + +if (unlikely (!is_table (RB ))) { +kp_error (ks,"get key from non-table\n" ); +return ; + } +kp_tab_getstr (hvalue (RB ), (ktap_str_t *)kbase[idx],RA ); +DISPATCH (); + } + DO_BC_TGETB: {/* A = B[C] */ +/* 8 bit literal C operand as an unsigned integer +* index (0..255)) */ +uint8_t idx =bc_c (instr); + +if (unlikely (!is_table (RB ))) { +kp_error (ks,"set key to non-table\n" ); +return ; + } +kp_tab_getint (hvalue (RB ), idx,RA ); +DISPATCH (); + } + DO_BC_TGETR:/* generated by genlibbc, not compiler, not used */ +return ; + DO_BC_TSETV:/* B[C] = A */ +if (unlikely (!is_table (RB ))) { +kp_error (ks,"set key to non-table\n" ); +return ; + } +kp_tab_set (ks,hvalue (RB ),RC ,RA ); +DISPATCH (); + DO_BC_TINCV:/* B[C] += A */ +if (unlikely (!is_table (RB ))) { +kp_error (ks,"set key to non-table\n" ); +return ; + } +if (unlikely (!is_number (RA ))) { +kp_error (ks,"use '+=' on non-number\n" ); +return ; + } +kp_tab_incr (ks,hvalue (RB ),RC ,nvalue (RA )); +DISPATCH (); + DO_BC_TSETS: {/* B[C] = A */ +int idx = ~bc_c (instr); + +if (unlikely (!is_table (RB ))) { +kp_error (ks,"set key to non-table\n" ); +return ; + } +kp_tab_setstr (ks,hvalue (RB ), (ktap_str_t *)kbase[idx],RA ); +DISPATCH (); + } + DO_BC_TINCS: {/* B[C] += A */ +int idx = ~bc_c (instr); + +if (unlikely (!is_table (RB ))) { +kp_error (ks,"set key to non-table\n" ); +return ; + } +if (unlikely (!is_number (RA ))) { +kp_error (ks,"use '+=' on non-number\n" ); +return ; + } +kp_tab_incrstr (ks,hvalue (RB ), (ktap_str_t *)kbase[idx], +nvalue (RA )); +DISPATCH (); + } + DO_BC_TSETB: {/* B[C] = A */ +/* 8 bit literal C operand as an unsigned integer +* index (0..255)) */ +uint8_t idx =bc_c (instr); + +if (unlikely (!is_table (RB ))) { +kp_error (ks,"set key to non-table\n" ); +return ; + } +kp_tab_setint (ks,hvalue (RB ), idx,RA ); +DISPATCH (); + } + DO_BC_TINCB: {/* B[C] = A */ +uint8_t idx =bc_c (instr); + +if (unlikely (!is_table (RB ))) { +kp_error (ks,"set key to non-table\n" ); +return ; + } +if (unlikely (!is_number (RA ))) { +kp_error (ks,"use '+=' on non-number\n" ); +return ; + } +kp_tab_incrint (ks,hvalue (RB ), idx,nvalue (RA )); +DISPATCH (); + } + DO_BC_TSETM:/* don't support */ +return ; + DO_BC_TSETR:/* generated by genlibbc, not compiler, not used */ +return ; + DO_BC_CALLM: + DO_BC_CALL: {/* b: return_number + 1; c: argument + 1 */ +int c =bc_c (instr); +int nresults =bc_b (instr) -1 ; +StkId oldtop = ks->top; +StkId newfunc =RA ; + +if (op ==BC_CALL && c !=0 ) + ks->top =RA + c; +else if (op ==BC_CALLM ) + ks->top =RA + c + multres; + +if (itype (newfunc) ==KTAP_TCFUNC ) {/* light C function */ +ktap_cfunction f =fvalue (newfunc); +int n; + +if (unlikely (checkstack (ks, +KTAP_MIN_RESERVED_STACK_SIZE ))) +return ; + + ks->func = newfunc; + n = (*f)(ks); +if (unlikely (n <0 ))/* error occured */ +return ; +poscall (ks, newfunc, ks->top - n, nresults); + + ks->top = oldtop; + multres = n +1 ;/* set to multres */ +DISPATCH (); + }else if (itype (newfunc) ==KTAP_TFUNC ) {/* ktap function */ +int n; + + func = newfunc; + pt =clvalue (func)->p; + +if (unlikely (checkstack (ks, pt->framesize))) +return ; + +/* get number of real arguments */ + n = (int )(ks->top - func) -1 ; + +/* complete missing arguments */ +for (; n < pt->numparams; n++) +set_nil (ks->top++); + + base = (!(pt->flags &PROTO_VARARG )) ? func +1 : +adjust_varargs (ks, pt, n); + + fn =clvalue (func); + pt = fn->p; + kbase = pt->k; + func->pcr = pc -1 ;/* save pc */ + ks->top = base + pt->framesize; + pc =proto_bc (pt) +1 ;/* starting point */ +DISPATCH (); + }else { +kp_error (ks,"attempt to call nil function\n" ); +return ; + } + } + DO_BC_CALLMT:/* don't support */ +return ; + DO_BC_CALLT: {/* Tailcall: return A(A+1, ..., A+D-1) */ +StkId nfunc =RA ; + +if (itype (nfunc) ==KTAP_TCFUNC ) {/* light C function */ +kp_error (ks,"don't support callt for C function" ); +return ; + }else if (itype (nfunc) ==KTAP_TFUNC ) {/* ktap function */ +int aux; + +/* +* tail call: put called frame (n) in place of +* caller one (o) +*/ +StkId ofunc = func;/* caller function */ +/* last stack slot filled by 'precall' */ +StkId lim = nfunc +1 +clvalue (nfunc)->p->numparams; + + fn =clvalue (nfunc); + ofunc->val = nfunc->val; + +/* move new frame into old one */ +for (aux =1 ; nfunc + aux < lim; aux++) +set_obj (ofunc + aux, nfunc + aux); + + pt = fn->p; + kbase = pt->k; + ks->top = base + pt->framesize; + pc =proto_bc (pt) +1 ;/* starting point */ +DISPATCH (); + }else { +kp_error (ks,"attempt to call nil function\n" ); +return ; + } + } + DO_BC_ITERC:/* don't support it now */ +return ; + DO_BC_ITERN:/* Specialized ITERC, if iterator function A-3 is next()*/ +/* detect hot loop */ +if (unlikely (check_hot_loop (ks, loop_count++) <0 )) +return ; + +if (kp_tab_next (ks,hvalue (RA -2 ),RA )) { + donextjump;/* Get jump target from ITERL */ + }else { + pc++;/* jump to ITERL + 1 */ + } +DISPATCH (); + DO_BC_VARG:/* don't support */ +return ; + DO_BC_ISNEXT:/* Verify ITERN specialization and jump */ +if (!is_cfunc (RA -3 ) || !is_table (RA -2 ) || !is_nil (RA -1 ) + ||fvalue (RA -3 ) != (ktap_cfunction )kp_tab_next) { +/* Despecialize bytecode if any of the checks fail. */ +setbc_op (pc -1 ,BC_JMP ); +dojump (instr,0 ); +setbc_op (pc,BC_ITERC ); + }else { +dojump (instr,0 ); +set_nil (RA );/* init control variable */ + } +DISPATCH (); + DO_BC_RETM:/* don't support return multiple values */ + DO_BC_RET: +return ; + DO_BC_RET0: +/* if it's called from external invocation, just return */ +if (!func->pcr) +return ; + + pc = func->pcr;/* restore PC */ + + multres =bc_d (instr); +set_nil (func); + + base = func -bc_a (*pc); + func = base -1 ; + fn =clvalue (func); + kbase = fn->p->k; + ks->top = base + pt->framesize; + pc++; + +DISPATCH (); + DO_BC_RET1: +/* if it's called from external invocation, just return */ +if (!func->pcr) +return ; + + pc = func->pcr;/* restore PC */ + + multres =bc_d (instr); +set_obj (base -1 ,RA );/* move result */ + + base = func -bc_a (*pc); + func = base -1 ; + fn =clvalue (func); + kbase = fn->p->k; + ks->top = base + pt->framesize; + pc++; + +DISPATCH (); + DO_BC_FORI: {/* Numeric 'for' loop init */ +ktap_number idx; +ktap_number limit; +ktap_number step; + +if (unlikely (!is_number (RA ) || !is_number (RA +1 ) || + !is_number (RA +2 ))) { + kp_error(ks, KTAP_QL("for") +" init/limit/step value must be a number\n" ); +return ; + } + + idx =nvalue (RA ); + limit =nvalue (RA +1 ); + step =nvalue (RA +2 ); + +if (NUMLT (0 , step) ?NUMLE (idx, limit) :NUMLE (limit, idx)) { +set_number (RA +3 ,nvalue (RA )); + }else { +dojump (instr,0 ); + } +DISPATCH (); + } + DO_BC_JFORI:/* not used */ +return ; + DO_BC_FORL: {/* Numeric 'for' loop */ +ktap_number step =nvalue (RA +2 ); +/* increment index */ +ktap_number idx =NUMADD (nvalue (RA ), step); +ktap_number limit =nvalue (RA +1 ); +if (NUMLT (0 , step) ?NUMLE (idx, limit) :NUMLE (limit, idx)) { +dojump (instr,0 );/* jump back */ +set_number (RA , idx);/* update internal index... */ +set_number (RA +3 , idx);/* ...and external index */ + } + +if (unlikely (check_hot_loop (ks, loop_count++) <0 )) +return ; + +DISPATCH (); + } + DO_BC_IFORL:/* not used */ + DO_BC_JFORL: + DO_BC_ITERL: + DO_BC_IITERL: + DO_BC_JITERL: +return ; + DO_BC_LOOP:/* Generic loop */ +/* ktap use this bc to detect hot loop */ +if (unlikely (check_hot_loop (ks, loop_count++) <0 )) +return ; +DISPATCH (); + DO_BC_ILOOP:/* not used */ + DO_BC_JLOOP: +return ; + DO_BC_JMP:/* Jump */ +dojump (instr,0 ); +DISPATCH (); + DO_BC_FUNCF:/* function header, not used */ + DO_BC_IFUNCF: + DO_BC_JFUNCF: + DO_BC_FUNCV: + DO_BC_IFUNCV: + DO_BC_JFUNCV: + DO_BC_FUNCC: + DO_BC_FUNCCW: +return ; + DO_BC_VARGN:/* arg0 .. arg9*/ +if (unlikely (!ks->current_event)) { +kp_error (ks,"invalid event context\n" ); +return ; + } + +kp_event_getarg (ks,RA ,bc_d (instr)); +DISPATCH (); + DO_BC_VARGSTR: {/* argstr */ +/* +* If you pass argstr to print/printf function directly, +* then no extra string generated, so don't worry string +* poll size for below case: +* print(argstr) +* +* If you use argstr as table key like below, then it may +* overflow your string pool size, so be care of on it. +* table[argstr] = V +* +* If you assign argstr to upval or table value like below, +* it don't really write string, just write type KTAP_TEVENTSTR, +* the value will be interpreted when value print out in valid +* event context, if context mismatch, error will report. +* table[V] = argstr +* upval = argstr +* +* If you want to save real string of argstr, then use it like +* below, again, be care of string pool size in this case. +* table[V] = stringof(argstr) +* upval = stringof(argstr) +*/ +struct ktap_event_data *e = ks->current_event; + +if (unlikely (!e)) { +kp_error (ks,"invalid event context\n" ); +return ; + } + +if (e->argstr)/* argstr been stringified */ +set_string (RA , e->argstr); +else +set_eventstr (RA ); +DISPATCH (); + } + DO_BC_VPROBENAME: {/* probename */ +struct ktap_event_data *e = ks->current_event; + +if (unlikely (!e)) { +kp_error (ks,"invalid event context\n" ); +return ; + } +set_string (RA , e->event->name); +DISPATCH (); + } + DO_BC_VPID:/* pid */ +set_number (RA , (int )current->pid); +DISPATCH (); + DO_BC_VTID:/* tid */ +set_number (RA , (int )task_pid_vnr (current)); +DISPATCH (); + DO_BC_VUID: {/* uid */ +#if LINUX_VERSION_CODE >=KERNEL_VERSION (3 ,5 ,0 ) +uid_t uid =from_kuid_munged (current_user_ns (),current_uid ()); +#else +uid_t uid =current_uid (); +#endif +set_number (RA , (int )uid); +DISPATCH (); + } + DO_BC_VCPU:/* cpu */ +set_number (RA ,smp_processor_id ()); +DISPATCH (); + DO_BC_VEXECNAME: {/* execname */ +ktap_str_t *ts =kp_str_newz (ks, current->comm); +if (unlikely (!ts)) +return ; +set_string (RA , ts); +DISPATCH (); + } + DO_BC_GFUNC: {/* Call built-in C function, patched by BC_GGET */ +ktap_cfunction cfunc =gfunc_get (ks,bc_d (instr)); +set_cfunc (RA , cfunc); +DISPATCH (); + } + } +} + +/* +* Validate byte code and static analysis. +* +* TODO: more type checking before real running. +*/ +int kp_vm_validate_code (ktap_state_t *ks,ktap_proto_t *pt,ktap_val_t *base) +{ +const unsigned int *pc =proto_bc (pt) +1 ; +unsigned int instr, op; +ktap_obj_t **kbase = pt->k; +ktap_tab_t *gtab =G (ks)->gtab; +int i; + +#define RA (base + bc_a(instr)) +#define RB (base + bc_b(instr)) +#define RC (base + bc_c(instr)) +#define RD (base + bc_d(instr)) + +if (pt->framesize >KP_MAX_SLOTS ) { +kp_error (ks,"exceed max frame size %d\n" , pt->framesize); +return -1 ; + } + +if (base + pt->framesize > ks->stack_last) { +kp_error (ks,"stack overflow\n" ); +return -1 ; + } + +for (i =0 ; i < pt->sizebc -1 ; i++) { + instr = *pc++; + op =bc_op (instr); + + +if (op >=BC__MAX ) { +kp_error (ks,"unknown byte code %d\n" , op); +return -1 ; + } + +switch (op) { +case BC_FNEW : { +int idx = ~bc_d (instr); +ktap_proto_t *newpt = (ktap_proto_t *)kbase[idx]; +if (kp_vm_validate_code (ks, newpt,RA +1 )) +return -1 ; + +break ; + } +case BC_RETM :case BC_RET : +kp_error (ks,"don't support return multiple values\n" ); +return -1 ; +case BC_GSET :case BC_GINC : {/* _G[D] = A, _G[D] += A */ +int idx = ~bc_d (instr); +ktap_str_t *ts = (ktap_str_t *)kbase[idx]; +kp_error (ks,"cannot set global variable '%s'\n" , +getstr (ts)); +return -1 ; + } +case BC_GGET : { +int idx = ~bc_d (instr); +ktap_str_t *ts = (ktap_str_t *)kbase[idx]; +ktap_val_t val; +kp_tab_getstr (gtab, ts, &val); +if (is_nil (&val)) { +kp_error (ks,"undefined global variable" +" '%s'\n" ,getstr (ts)); +return -1 ; + }else if (is_cfunc (&val)) { +int idx =gfunc_getidx (G (ks),fvalue (&val)); +if (idx >=0 ) { +/* patch BC_GGET bytecode to BC_GFUNC */ +setbc_op (pc -1 ,BC_GFUNC ); +setbc_d (pc -1 , idx); + } + } +break ; + } +case BC_ITERC : +kp_error (ks,"ktap only support pairs iteraor\n" ); +return -1 ; +case BC_POW : +kp_error (ks,"ktap don't support pow arith\n" ); +return -1 ; + } + } + +return 0 ; +} + +/* return cfunction by idx */ +static ktap_cfunction gfunc_get (ktap_state_t *ks,int idx) +{ +return G (ks)->gfunc_tbl[idx]; +} + +/* get cfunction index, the index is for fast get cfunction in runtime */ +static int gfunc_getidx (ktap_global_state_t *g,ktap_cfunction cfunc) +{ +int nr = g->nr_builtin_cfunction; +ktap_cfunction *gfunc_tbl = g->gfunc_tbl; +int i; + +for (i =0 ; i < nr; i++) { +if (gfunc_tbl[i] == cfunc) +return i; + } + +return -1 ; +} + +static void gfunc_add (ktap_state_t *ks,ktap_cfunction cfunc) +{ +int nr =G (ks)->nr_builtin_cfunction; + +if (nr ==KP_MAX_CACHED_CFUNCTION ) { +kp_error (ks,"please enlarge KP_MAX_CACHED_CFUNCTION %d\n" , +KP_MAX_CACHED_CFUNCTION ); +return ; + } +G (ks)->gfunc_tbl[nr] = cfunc; +G (ks)->nr_builtin_cfunction++; +} + +/* function for register library */ +int kp_vm_register_lib (ktap_state_t *ks,const char *libname, +const ktap_libfunc_t *funcs) +{ +ktap_tab_t *gtab =G (ks)->gtab; +ktap_tab_t *target_tbl; +int i; + +/* lib is null when register baselib function */ +if (libname ==NULL ) + target_tbl = gtab; +else { +ktap_val_t key, val; +ktap_str_t *ts =kp_str_newz (ks, libname); +if (!ts) +return -ENOMEM ; + +/* calculate the function number contained by this library */ +for (i =0 ; funcs[i].name !=NULL ; i++) { + } + + target_tbl =kp_tab_new_ah (ks,0 , i +1 ); +if (!target_tbl) +return -ENOMEM ; + +set_string (&key, ts); +set_table (&val, target_tbl); +kp_tab_set (ks, gtab, &key, &val); + } + +/* TODO: be care of same function name issue, foo() and tbl.foo() */ +for (i =0 ; funcs[i].name !=NULL ; i++) { +ktap_str_t *func_name =kp_str_newz (ks, funcs[i].name); +ktap_val_t fn; + +if (unlikely (!func_name)) +return -ENOMEM ; + +set_cfunc (&fn, funcs[i].func); +kp_tab_setstr (ks, target_tbl, func_name, &fn); + +gfunc_add (ks, funcs[i].func); + } + +return 0 ; +} + +static int init_registry (ktap_state_t *ks) +{ +ktap_tab_t *registry =kp_tab_new_ah (ks,2 ,0 ); +ktap_val_t gtbl; +ktap_tab_t *t; + +if (!registry) +return -1 ; + +set_table (&G (ks)->registry, registry); + +/* assume there will have max 1024 global variables */ + t =kp_tab_new_ah (ks,0 ,1024 ); +if (!t) +return -1 ; + +set_table (>bl, t); +kp_tab_setint (ks, registry,KTAP_RIDX_GLOBALS , >bl); +G (ks)->gtab = t; + +return 0 ; +} + +static int init_arguments (ktap_state_t *ks,int argc,char __user **user_argv) +{ +ktap_tab_t *gtbl =G (ks)->gtab; +ktap_tab_t *arg_tbl =kp_tab_new_ah (ks, argc,1 ); +ktap_val_t arg_tblval; +ktap_val_t arg_tsval; +ktap_str_t *argts =kp_str_newz (ks,"arg" ); +char **argv; +int i, ret; + +if (!arg_tbl) +return -1 ; + +if (unlikely (!argts)) +return -ENOMEM ; + +set_string (&arg_tsval, argts); +set_table (&arg_tblval, arg_tbl); +kp_tab_set (ks, gtbl, &arg_tsval, &arg_tblval); + +if (!argc) +return 0 ; + +if (argc >1024 ) +return -EINVAL ; + + argv =kzalloc (argc *sizeof (char *),GFP_KERNEL ); +if (!argv) +return -ENOMEM ; + + ret =copy_from_user (argv, user_argv, argc *sizeof (char *)); +if (ret <0 ) { +kfree (argv); +return -EFAULT ; + } + + ret =0 ; +for (i =0 ; i < argc; i++) { +ktap_val_t val; +char __user *ustr = argv[i]; +char *kstr; +int len; +int res; + + len =strlen_user (ustr); +if (len >0x1000 ) { + ret = -EINVAL ; +break ; + } + + kstr =kmalloc (len +1 ,GFP_KERNEL ); +if (!kstr) { + ret = -ENOMEM ; +break ; + } + +if (strncpy_from_user (kstr, ustr, len) <0 ) { +kfree (kstr); + ret = -EFAULT ; +break ; + } + + kstr[len] ='\0' ; + +if (!kstrtoint (kstr,10 , &res)) { +set_number (&val, res); + }else { +ktap_str_t *ts =kp_str_newz (ks, kstr); +if (unlikely (!ts)) { +kfree (kstr); + ret = -ENOMEM ; +break ; + } + +set_string (&val, ts); + } + +kp_tab_setint (ks, arg_tbl, i, &val); + +kfree (kstr); + } + +kfree (argv); +return ret; +} + +static void free_preserved_data (ktap_state_t *ks) +{ +int cpu, i, j; + +/* free stack for each allocated ktap_state */ +for_each_possible_cpu (cpu) { +for (j =0 ; j <PERF_NR_CONTEXTS ; j++) { +void *percpu_state =G (ks)->percpu_state[j]; +ktap_state_t *pks; + +if (!percpu_state) +break ; + pks =per_cpu_ptr (percpu_state, cpu); +if (!ks) +break ; +kfree (pks->stack); + } + } + +/* free percpu ktap_state */ +for (i =0 ; i <PERF_NR_CONTEXTS ; i++) { +if (G (ks)->percpu_state[i]) +free_percpu (G (ks)->percpu_state[i]); + } + +/* free percpu ktap print buffer */ +for (i =0 ; i <PERF_NR_CONTEXTS ; i++) { +if (G (ks)->percpu_print_buffer[i]) +free_percpu (G (ks)->percpu_print_buffer[i]); + } + +/* free percpu ktap temp buffer */ +for (i =0 ; i <PERF_NR_CONTEXTS ; i++) { +if (G (ks)->percpu_temp_buffer[i]) +free_percpu (G (ks)->percpu_temp_buffer[i]); + } + +/* free percpu ktap recursion context flag */ +for (i =0 ; i <PERF_NR_CONTEXTS ; i++) +if (G (ks)->recursion_context[i]) +free_percpu (G (ks)->recursion_context[i]); +} + +#define ALLOC_PERCPU (size) __alloc_percpu(size, __alignof__(char)) +static int init_preserved_data (ktap_state_t *ks) +{ +void __percpu *data; +int cpu, i, j; + +/* init percpu ktap_state */ +for (i =0 ; i <PERF_NR_CONTEXTS ; i++) { + data =ALLOC_PERCPU (sizeof (ktap_state_t)); +if (!data) + goto fail; +G (ks)->percpu_state[i] = data; + } + +/* init stack for each allocated ktap_state */ +for_each_possible_cpu (cpu) { +for (j =0 ; j <PERF_NR_CONTEXTS ; j++) { +void *percpu_state =G (ks)->percpu_state[j]; +ktap_state_t *pks; + +if (!percpu_state) +break ; + pks =per_cpu_ptr (percpu_state, cpu); +if (!ks) +break ; + pks->stack =kzalloc (KTAP_STACK_SIZE_BYTES ,GFP_KERNEL ); +if (!pks->stack) + goto fail; + + pks->stack_last = pks->stack +KTAP_STACK_SIZE ; +G (pks) =G (ks); + } + } + +/* init percpu ktap print buffer */ +for (i =0 ; i <PERF_NR_CONTEXTS ; i++) { + data =ALLOC_PERCPU (KTAP_PERCPU_BUFFER_SIZE ); +if (!data) + goto fail; +G (ks)->percpu_print_buffer[i] = data; + } + +/* init percpu ktap temp buffer */ +for (i =0 ; i <PERF_NR_CONTEXTS ; i++) { + data =ALLOC_PERCPU (KTAP_PERCPU_BUFFER_SIZE ); +if (!data) + goto fail; +G (ks)->percpu_temp_buffer[i] = data; + } + +/* init percpu ktap recursion context flag */ +for (i =0 ; i <PERF_NR_CONTEXTS ; i++) { + data =alloc_percpu (int); +if (!data) + goto fail; +G (ks)->recursion_context[i] = data; + } + +return 0 ; + + fail: +free_preserved_data (ks); +return -ENOMEM ; +} + +/* +* wait ktapio thread read all content in ring buffer. +* +* Here we use stupid approach to sync with ktapio thread, +* note that we cannot use semaphore/completion/other sync method, +* because ktapio thread could be killed by SIG_KILL in anytime, there +* have no safe way to up semaphore or wake waitqueue before thread exit. +* +* we also cannot use waitqueue of current->signal->wait_chldexit to sync +* exit, becasue mainthread and ktapio thread are in same thread group. +* +* Also ktap mainthread must wait ktapio thread exit, otherwise ktapio +* thread will oops when access ktap structure. +*/ +static void wait_user_completion (ktap_state_t *ks) +{ +struct task_struct *tsk =G (ks)->task; +G (ks)->wait_user =1 ; + +while (1 ) { +set_current_state (TASK_INTERRUPTIBLE ); +/* sleep for 100 msecs, and try again. */ +schedule_timeout (HZ /10 ); + +if (get_nr_threads (tsk) ==1 ) +break ; + } +} + +static void sleep_loop (ktap_state_t *ks, +int (*actor)(ktap_state_t *ks,void *arg),void *arg) +{ +while (!ks->stop) { +set_current_state (TASK_INTERRUPTIBLE ); +/* sleep for 100 msecs, and try again. */ +schedule_timeout (HZ /10 ); + +if (actor (ks, arg)) +return ; + } +} + +static int sl_wait_task_pause_actor (ktap_state_t *ks,void *arg) +{ +struct task_struct *task = (struct task_struct *)arg; + +if (task->state) +return 1 ; +else +return 0 ; +} + +static int sl_wait_task_exit_actor (ktap_state_t *ks,void *arg) +{ +struct task_struct *task = (struct task_struct *)arg; + +if (signal_pending (current)) { +flush_signals (current); + +/* newline for handle CTRL+C display as ^C */ +kp_puts (ks,"\n" ); +return 1 ; + } + +/* stop waiting if target pid is exited */ +if (task && task->state ==TASK_DEAD ) +return 1 ; + +return 0 ; +} + +/* wait user interrupt, signal killed */ +static void wait_user_interrupt (ktap_state_t *ks) +{ +struct task_struct *task =G (ks)->trace_task; + +if (G (ks)->state ==KTAP_EXIT ||G (ks)->state ==KTAP_ERROR ) +return ; + +/* let tracing goes now. */ + ks->stop =0 ; + +if (G (ks)->parm->workload) { +/* make sure workload is in pause state +* so it won't miss the signal */ +sleep_loop (ks, sl_wait_task_pause_actor, task); +/* tell workload process to start executing */ +send_sig (SIGINT ,G (ks)->trace_task,0 ); + } + +if (!G (ks)->parm->quiet) +kp_printf (ks,"Tracing... Hit Ctrl-C to end.\n" ); + +sleep_loop (ks, sl_wait_task_exit_actor, task); +} + +/* +* ktap exit, free all resources. +*/ +void kp_vm_exit (ktap_state_t *ks) +{ +if (!list_empty (&G (ks)->events_head) || + !list_empty (&G (ks)->timers)) +wait_user_interrupt (ks); + +kp_exit_timers (ks); +kp_events_exit (ks); + +/* free all resources got by ktap */ +kp_str_freeall (ks); +kp_mempool_destroy (ks); + +func_closeuv (ks,0 );/* close all open upvals, let below call free it */ +kp_obj_freeall (ks); + +kp_vm_exit_thread (ks); +kp_free (ks, ks->stack); + +free_preserved_data (ks); +free_cpumask_var (G (ks)->cpumask); + +wait_user_completion (ks); + +/* should invoke after wait_user_completion */ +if (G (ks)->trace_task) +put_task_struct (G (ks)->trace_task); + +kp_transport_exit (ks); +kp_free (ks, ks);/* free self */ +} + +/* +* ktap mainthread initization +*/ +ktap_state_t *kp_vm_new_state (ktap_option_t *parm,struct dentry *dir) +{ +ktap_state_t *ks; +ktap_global_state_t *g; +pid_t pid; +int cpu; + + ks =kzalloc (sizeof (ktap_state_t) +sizeof (ktap_global_state_t), +GFP_KERNEL ); +if (!ks) +return NULL ; + +G (ks) = (ktap_global_state_t *)(ks +1 ); + g =G (ks); + g->mainthread = ks; + g->task = current; + g->parm = parm; + g->str_lock = (arch_spinlock_t )__ARCH_SPIN_LOCK_UNLOCKED; + g->strmask = ~(int )0 ; + g->uvhead.prev = &g->uvhead; + g->uvhead.next = &g->uvhead; + g->state =KTAP_RUNNING ; +INIT_LIST_HEAD (&(g->timers)); +INIT_LIST_HEAD (&(g->events_head)); + +if (kp_transport_init (ks, dir)) + goto out; + + ks->stack =kp_malloc (ks,KTAP_STACK_SIZE_BYTES ); +if (!ks->stack) + goto out; + + ks->stack_last = ks->stack +KTAP_STACK_SIZE ; + ks->top = ks->stack; + + pid = (pid_t )parm->trace_pid; +if (pid !=-1 ) { +struct task_struct *task; + +rcu_read_lock (); + task =pid_task (find_vpid (pid),PIDTYPE_PID ); +if (!task) { +kp_error (ks,"cannot find pid %d\n" , pid); +rcu_read_unlock (); + goto out; + } + g->trace_task = task; +get_task_struct (task); +rcu_read_unlock (); + } + +if ( !alloc_cpumask_var (&g->cpumask,GFP_KERNEL )) + goto out; + +cpumask_copy (g->cpumask, cpu_online_mask); + + cpu = parm->trace_cpu; +if (cpu !=-1 ) { +if (!cpu_online (cpu)) { +kp_error (ks,"ktap: cpu %d is not online\n" , cpu); + goto out; + } + +cpumask_clear (g->cpumask); +cpumask_set_cpu (cpu, g->cpumask); + } + +if (kp_mempool_init (ks,KP_MAX_MEMPOOL_SIZE )) + goto out; + +if (kp_str_resize (ks,1024 -1 ))/* set string hashtable size */ + goto out; + +if (init_registry (ks)) + goto out; +if (init_arguments (ks, parm->argc, parm->argv)) + goto out; + +/* init librarys */ +if (kp_lib_init_base (ks)) + goto out; +if (kp_lib_init_kdebug (ks)) + goto out; +if (kp_lib_init_timer (ks)) + goto out; +if (kp_lib_init_ansi (ks)) + goto out; +if (kp_lib_init_table (ks)) + goto out; + +if (kp_lib_init_net (ks)) + goto out; + +if (init_preserved_data (ks)) + goto out; + +if (kp_events_init (ks)) + goto out; + +return ks; + + out: + g->state =KTAP_ERROR ; +kp_vm_exit (ks); +return NULL ; +} +
tools/ktap/runtime/kp_vm.h new file +43 -0
43 lines not on this page · raw message
Signature · 1 line
1.8.1.4
PATCH 10/28 ktap: add runtime/kp_str.[c|h] and runtime/kp_mempool.[c|h]
In reply to Jovi ZhangweiExposed functions:
1). kp_str_new:
Return a interned string, failure if out of memory or exceed max string number(default 9999).
It allocate memory from mempool, not kmalloc, because we don't want dynamic allocate string in probe context.
2). kp_str_resize:
Initizate interned hash table g->strhash.
3). kp_str_fmt:
Return a format string, called from 'printf' built-in function.
4). kp_mempool_init/kp_mempool_destroy/kp_mempool_alloc
mempool is only service for string allocation currently.
All string in ktap is interned, it means ktap keeps
a single copy for any string. Whenever a new string appears, ktap checks whether it already has a copy of that string and, if so, reuses that copy. Internalization makes operations like string comparison and table indexing very fast, but it slows down string creation.
--- tools/ktap/runtime/kp_mempool.c | 94 +++++++++++ tools/ktap/runtime/kp_mempool.h | 8 + tools/ktap/runtime/kp_str.c | 360 ++++++++++++++++++++++++++++++++++++++++ tools/ktap/runtime/kp_str.h | 13 ++ 4 files changed, 475 insertions(+) create mode 100644 tools/ktap/runtime/kp_mempool.c create mode 100644 tools/ktap/runtime/kp_mempool.h create mode 100644 tools/ktap/runtime/kp_str.c create mode 100644 tools/ktap/runtime/kp_str.h
tools/ktap/runtime/kp_mempool.c new file +94 -0
@@ -0,0 +1,94 @@ +/* +* kp_mempool.c - ktap memory pool, service for string allocation +* +* This file is part of ktap by Jovi Zhangwei. +* +* Copyright (C) 2012-2014 Jovi Zhangwei <[email protected]>. +* +* ktap is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* ktap is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "../include/ktap_types.h" +#include "kp_obj.h" +#include "kp_str.h" + +#include <linux/ctype.h> +#include <linux/module.h> +#include "ktap.h" + + +/* +* allocate memory from mempool, the allocated memory will be free +* util ktap exit. +* TODO: lock-free allocation +*/ +void *kp_mempool_alloc (ktap_state_t *ks,int size) +{ +ktap_global_state_t *g =G (ks); +void *mempool = g->mempool; +void *freepos = g->mp_freepos; +void *addr; +unsigned long flags; + +local_irq_save (flags); +arch_spin_lock (&g->mp_lock); + +if (unlikely ((unsigned long )((char *)freepos + size)) > + (unsigned long )((char *)mempool + g->mp_size)) { + addr =NULL ; + goto out; + } + + addr = freepos; + g->mp_freepos = (char *)freepos + size; + out: + +arch_spin_unlock (&g->mp_lock); +local_irq_restore (flags); +return addr; +} + +/* +* destroy mempool. +*/ +void kp_mempool_destroy (ktap_state_t *ks) +{ +ktap_global_state_t *g =G (ks); + +if (!g->mempool) +return ; + +vfree (g->mempool); + g->mempool =NULL ; + g->mp_freepos =NULL ; + g->mp_size =0 ; +} + +/* +* pre-allocate size Kbytes memory pool. +*/ +int kp_mempool_init (ktap_state_t *ks,int size) +{ +ktap_global_state_t *g =G (ks); + + g->mempool =vmalloc (size *1024 ); +if (!g->mempool) +return -ENOMEM ; + + g->mp_freepos = g->mempool; + g->mp_size = size *1024 ; + g->mp_lock = (arch_spinlock_t )__ARCH_SPIN_LOCK_UNLOCKED; +return 0 ; +} +
tools/ktap/runtime/kp_mempool.h new file +8 -0
@@ -0,0 +1,8 @@ +#ifndef __KTAP_MEMPOOL_H__ +#define __KTAP_MEMPOOL_H__ + +void *kp_mempool_alloc (ktap_state_t *ks,int size); +void kp_mempool_destroy (ktap_state_t *ks); +int kp_mempool_init (ktap_state_t *ks,int size); + +#endif /* __KTAP_MEMPOOL_H__ */
tools/ktap/runtime/kp_str.c new file +360 -0
360 lines not on this page · raw message
tools/ktap/runtime/kp_str.h new file +13 -0
@@ -0,0 +1,13 @@ +#ifndef __KTAP_STR_H__ +#define __KTAP_STR_H__ + +int kp_str_resize (ktap_state_t *ks,int newmask); +void kp_str_freeall (ktap_state_t *ks); +ktap_str_t *kp_str_new (ktap_state_t *ks,const char *str,size_t len); + +#define kp_str_newz (ks, s) (kp_str_new(ks, s, strlen(s))) + +#include <linux/trace_seq.h> +int kp_str_fmt (ktap_state_t *ks,struct trace_seq *seq); + +#endif /* __KTAP_STR_H__ */
Signature · 1 line
1.8.1.4
PATCH 11/28 ktap: add runtime/kp_tab.[c|h]
In reply to Jovi Zhangweiktap table is associative array, you can put any key value into table and store any type value, for example:
var s = {}
s[1] = 1 <-- store number key and number value s["key"] = "value" <-- store string key and string value
s["key2"] = 1 <--- store string key and number value
Exposed functions:
1). kp_tab_new
return a new allocated table
2). kp_tab_dup
return a new allocated table with template table.
3). kp_tab_free
free a table
4). kp_tab_clear
clear entires of a table, not delete ktap_tab_t structure.
5). kp_tab_getint/kp_tab_setint/kp_tab_incrint
get/set/inc with a int key
6). kp_tab_getstr/kp_tab_setstr/kp_tab_incrstr
get/set/inc with a string key
7). kp_tab_get/kp_tab_set/kp_tab_incr
get/set/inc with a key(variable type)
8). kp_tab_next
table iterator
9). kp_tab_len
return length of a table
10).kp_tab_print_hist
print histogram of a table(value must be number)
All table operation is protect by spinlock.
Table cannot be resize, ktap runtime will report error once table overflow.
ktap table performance is good, benchmark show that ktap table operation performance is better than systemtap, especially for string key(because all string is interned in ktap).
Note table is not aggregation in Systemtap and Dtrace, aggregation use percpu buffer. ktap will introduce aggregation soon with Dtrace aggregation syntax.
--- tools/ktap/runtime/kp_tab.c | 842 ++++++++++++++++++++++++++++++++++++++++++++ tools/ktap/runtime/kp_tab.h | 59 ++++ 2 files changed, 901 insertions(+) create mode 100644 tools/ktap/runtime/kp_tab.c create mode 100644 tools/ktap/runtime/kp_tab.h
tools/ktap/runtime/kp_tab.c new file +842 -0
@@ -0,0 +1,842 @@ +/* +* kp_tab.c - Table handling. +* +* This file is part of ktap by Jovi Zhangwei. +* +* Copyright (C) 2012-2014 Jovi Zhangwei <[email protected]>. +* +* Adapted from luajit and lua interpreter. +* Copyright (C) 2005-2014 Mike Pall. +* Copyright (C) 1994-2008 Lua.org, PUC-Rio. +* +* ktap is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* ktap is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include <linux/spinlock.h> +#include <linux/module.h> +#include <linux/kallsyms.h> +#include <linux/slab.h> +#include <linux/sort.h> +#include "../include/ktap_types.h" +#include "ktap.h" +#include "kp_vm.h" +#include "kp_obj.h" +#include "kp_str.h" +#include "kp_events.h" +#include "kp_tab.h" + +#define tab_lock_init (t) \ + do { \ + (t)->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; \ + } while (0) +#define tab_lock (t) \ + do { \ + local_irq_save(flags); \ + arch_spin_lock(&(t)->lock); \ + } while (0) +#define tab_unlock (t) \ + do { \ + arch_spin_unlock(&(t)->lock); \ + local_irq_restore(flags); \ + } while (0) + + +const ktap_val_t kp_niltv = { {NULL }, {KTAP_TNIL } } ; +#define niltv (&kp_niltv) + +/* -- Object hashing ------------------------------------------------------ */ + +/* Hash values are masked with the table hash mask and used as an index. */ +static __always_inline +ktap_node_t *hashmask(const ktap_tab_t *t, uint32_t hash) +{ +ktap_node_t *n = t->node; +return &n[hash & t->hmask]; +} + +/* String hashes are precomputed when they are interned. */ +#define hashstr (t, s) hashmask(t, (s)->hash) + +#define hashlohi (t, lo, hi) hashmask((t), hashrot((lo), (hi))) +#define hashnum (t, o) hashlohi((t), (o)->val.n & 0xffffffff, 0) +#define hashgcref (t, o) hashlohi((t), \ + ((unsigned long)(o)->val.gc & 0xffffffff), \ + ((unsigned long)(o)->val.gc & 0xffffffff) + HASH_BIAS) + + +/* Hash an arbitrary key and return its anchor position in the hash table. */ +static ktap_node_t *hashkey (const ktap_tab_t *t,const ktap_val_t *key) +{ +kp_assert (!tvisint (key)); +if (is_string (key)) +return hashstr (t,rawtsvalue (key)); +else if (is_number (key)) +return hashnum (t, key); +else if (is_bool (key)) +return hashmask (t,boolvalue (key)); +else +return hashgcref (t, key); +} + +/* -- Table creation and destruction -------------------------------------- */ + +/* Create new hash part for table. */ +static __always_inline +int newhpart(ktap_state_t *ks, ktap_tab_t *t, uint32_t hbits) +{ +uint32_t hsize; +ktap_node_t *node; +kp_assert (hbits !=0 ); + +if (hbits >KP_MAX_HBITS ) { +kp_error (ks,"table overflow\n" ); +return -1 ; + } + hsize =1u << hbits; + node =vmalloc (hsize *sizeof (ktap_node_t)); +if (!node) +return -ENOMEM ; + t->freetop = &node[hsize]; + t->node = node; + t->hmask = hsize-1 ; + +return 0 ; +} + +/* +* Q: Why all of these copies of t->hmask, t->node etc. to local variables? +* A: Because alias analysis for C is _really_ tough. +* Even state-of-the-art C compilers won't produce good code without this. +*/ + +/* Clear hash part of table. */ +static __always_inline void clearhpart(ktap_tab_t *t) +{ +uint32_t i, hmask = t->hmask; +ktap_node_t *node = t->node; +kp_assert (t->hmask !=0 ); + +for (i =0 ; i <= hmask; i++) { +ktap_node_t *n = &node[i]; + n->next =NULL ; +set_nil (&n->key); +set_nil (&n->val); + } + + t->hnum =0 ; +} + +/* Clear array part of table. */ +static __always_inline void clearapart(ktap_tab_t *t) +{ +uint32_t i, asize = t->asize; +ktap_val_t *array = t->array; +for (i =0 ; i < asize; i++) +set_nil (&array[i]); +} + +/* Create a new table. Note: the slots are not initialized (yet). */ +static ktap_tab_t *newtab (ktap_state_t *ks,uint32_t asize,uint32_t hbits) +{ +ktap_tab_t *t; + + t = (ktap_tab_t *)kp_obj_new (ks,sizeof (ktap_tab_t)); + t->gct = ~KTAP_TTAB ; + t->array =NULL ; + t->asize =0 ;/* In case the array allocation fails. */ + t->hmask =0 ; + +tab_lock_init (t); + +if (asize >0 ) { +if (asize >KP_MAX_ASIZE ) { +kp_error (ks,"table overflow\n" ); +return NULL ; + } + + t->array =vmalloc (asize *sizeof (ktap_val_t)); +if (!t->array) +return NULL ; + t->asize = asize; + } +if (hbits) +if (newhpart (ks, t, hbits)) { +vfree (t->array); +return NULL ; + } +return t; +} + +/* Create a new table. +* +* The array size is non-inclusive. E.g. asize=128 creates array slots +* for 0..127, but not for 128. If you need slots 1..128, pass asize=129 +* (slot 0 is wasted in this case). +* +* The hash size is given in hash bits. hbits=0 means no hash part. +* hbits=1 creates 2 hash slots, hbits=2 creates 4 hash slots and so on. +*/ +ktap_tab_t *kp_tab_new (ktap_state_t *ks,uint32_t asize,uint32_t hbits) +{ +ktap_tab_t *t =newtab (ks, asize, hbits); +if (!t) +return NULL ; + +clearapart (t); +if (t->hmask >0 ) +clearhpart (t); +return t; +} + +#define TABLE_NARR_ENTRIES 255/* PAGE_SIZE / sizeof(ktap_value) - 1 */ +#define TABLE_NREC_ENTRIES 2048/* (PAGE_SIZE * 20) / sizeof(ktap_tnode)*/ + +ktap_tab_t *kp_tab_new_ah (ktap_state_t *ks,int32_t a,int32_t h) +{ +if (a ==0 && h ==0 ) { + a =TABLE_NARR_ENTRIES ; + h =TABLE_NREC_ENTRIES ; + } + +return kp_tab_new (ks, (uint32_t )(a >0 ? a+1 :0 ),hsize2hbits (h)); +} + +/* Duplicate a table. */ +ktap_tab_t *kp_tab_dup (ktap_state_t *ks,const ktap_tab_t *kt) +{ +ktap_tab_t *t; +uint32_t asize, hmask; +int i; + +/* allocate default table size */ + t =kp_tab_new_ah (ks,0 ,0 ); +if (!t) +return NULL ; + + asize = kt->asize; +if (asize >0 ) { +ktap_val_t *array = t->array; +ktap_val_t *karray = kt->array; +if (asize <64 ) { +/* An inlined loop beats memcpy for < 512 bytes. */ +uint32_t i; +for (i =0 ; i < asize; i++) +set_obj (&array[i], &karray[i]); + }else { +memcpy (array, karray, asize*sizeof (ktap_val_t)); + } + } + + hmask = kt->hmask; +for (i =0 ; i <= hmask; i++) { +ktap_node_t *knode = &kt->node[i]; +if (is_nil (&knode->key)) +continue ; +kp_tab_set (ks, t, &knode->key, &knode->val); + } +return t; +} + +/* Clear a table. */ +void kp_tab_clear (ktap_tab_t *t) +{ +clearapart (t); +if (t->hmask >0 ) { +ktap_node_t *node = t->node; + t->freetop = &node[t->hmask+1 ]; +clearhpart (t); + } +} + +/* Free a table. */ +void kp_tab_free (ktap_state_t *ks,ktap_tab_t *t) +{ +if (t->hmask >0 ) +vfree (t->node); +if (t->asize >0 ) +vfree (t->array); +kp_free (ks, t); +} + +/* -- Table getters ------------------------------------------------------- */ + +static const ktap_val_t *tab_getinth (ktap_tab_t *t,uint32_t key) +{ +ktap_val_t k; +ktap_node_t *n; + +set_number (&k, (ktap_number )key); + n =hashnum (t, &k); +do { +if (is_number (&n->key) &&nvalue (&n->key) == key) { +return &n->val; + } + }while ((n = n->next)); +return niltv; +} + +static __always_inline +const ktap_val_t *tab_getint(ktap_tab_t *t, uint32_t key) +{ +return ((key < t->asize) ?arrayslot (t, key) : +tab_getinth (t, key)); +} + +void kp_tab_getint (ktap_tab_t *t,uint32_t key,ktap_val_t *val) +{ +unsigned long flags; + +tab_lock (t); +set_obj (val,tab_getint (t, key)); +tab_unlock (t); +} + +static const ktap_val_t *tab_getstr (ktap_tab_t *t,ktap_str_t *key) +{ +ktap_node_t *n =hashstr (t, key); +do { +if (is_string (&n->key) &&rawtsvalue (&n->key) == key) +return &n->val; + }while ((n = n->next)); +return niltv; +} + +void kp_tab_getstr (ktap_tab_t *t,ktap_str_t *key,ktap_val_t *val) +{ +unsigned long flags; + +tab_lock (t); +set_obj (val,tab_getstr (t, key)); +tab_unlock (t); +} + +static const ktap_val_t *tab_get (ktap_state_t *ks,ktap_tab_t *t, +const ktap_val_t *key) +{ +if (is_string (key)) { +return tab_getstr (t,rawtsvalue (key)); + }else if (is_number (key)) { +ktap_number nk =nvalue (key); +uint32_t k = (uint32_t )nk; +if (nk == (ktap_number )k) { +return tab_getint (t, k); + }else { + goto genlookup;/* Else use the generic lookup. */ + } + }else if (is_eventstr (key)) { +const ktap_str_t *ts; + +if (!ks->current_event) { +kp_error (ks, +"cannot stringify event str in invalid context\n" ); +return niltv; + } + + ts =kp_event_stringify (ks); +if (!ts) +return niltv; + +return tab_getstr (t,rawtsvalue (key)); + }else if (!is_nil (key)) { +ktap_node_t *n; + genlookup: + n =hashkey (t, key); +do { +if (kp_obj_equal (&n->key, key)) +return &n->val; + }while ((n = n->next)); + } +return niltv; +} + +void kp_tab_get (ktap_state_t *ks,ktap_tab_t *t,const ktap_val_t *key, +ktap_val_t *val) +{ +unsigned long flags; + +tab_lock (t); +set_obj (val,tab_get (ks, t, key)); +tab_unlock (t); +} + +/* -- Table setters ------------------------------------------------------- */ + +/* Insert new key. Use Brent's variation to optimize the chain length. */ +static ktap_val_t *kp_tab_newkey (ktap_state_t *ks,ktap_tab_t *t, +const ktap_val_t *key) +{ +ktap_node_t *n =hashkey (t, key); + +if (!is_nil (&n->val) || t->hmask ==0 ) { +ktap_node_t *nodebase = t->node; +ktap_node_t *collide, *freenode = t->freetop; + +kp_assert (freenode >= nodebase && + freenode <= nodebase+t->hmask+1 ); +do { +if (freenode == nodebase) {/* No free node found? */ +kp_error (ks,"table overflow\n" ); +return NULL ; + } + }while (!is_nil (&(--freenode)->key)); + + t->freetop = freenode; + collide =hashkey (t, &n->key); +if (collide != n) {/* Colliding node not the main node? */ +while (collide->next != n) +/* Find predecessor. */ + collide = collide->next; + collide->next = freenode;/* Relink chain. */ +/* Copy colliding node into free node and +* free main node. */ + freenode->val = n->val; + freenode->key = n->key; + freenode->next = n->next; + n->next =NULL ; +set_nil (&n->val); +/* Rechain pseudo-resurrected string keys with +* colliding hashes. */ +while (freenode->next) { +ktap_node_t *nn = freenode->next; +if (is_string (&nn->key) && !is_nil (&nn->val) && +hashstr (t,rawtsvalue (&nn->key)) == n) { + freenode->next = nn->next; + nn->next = n->next; + n->next = nn; + }else { + freenode = nn; + } + } + }else {/* Otherwise use free node. */ + freenode->next = n->next;/* Insert into chain. */ + n->next = freenode; + n = freenode; + } + } +set_obj (&n->key, key); + t->hnum++; +return &n->val; +} + +static ktap_val_t *tab_setinth (ktap_state_t *ks,ktap_tab_t *t,uint32_t key) +{ +ktap_val_t k; +ktap_node_t *n; + +set_number (&k, (ktap_number )key); + n =hashnum (t, &k); +do { +if (is_number (&n->key) &&nvalue (&n->key) == key) +return &n->val; + }while ((n = n->next)); +return kp_tab_newkey (ks, t, &k); +} + +static __always_inline +ktap_val_t *tab_setint(ktap_state_t *ks, ktap_tab_t *t, uint32_t key) +{ +return ((key < t->asize) ?arrayslot (t, key) : +tab_setinth (ks, t, key)); +} + +void kp_tab_setint (ktap_state_t *ks,ktap_tab_t *t, +uint32_t key,const ktap_val_t *val) +{ +ktap_val_t *v; +unsigned long flags; + +tab_lock (t); + v =tab_setint (ks, t, key); +if (likely (v)) +set_obj (v, val); +tab_unlock (t); +} + +void kp_tab_incrint (ktap_state_t *ks,ktap_tab_t *t,uint32_t key, +ktap_number n) +{ +ktap_val_t *v; +unsigned long flags; + +tab_lock (t); + v =tab_setint (ks, t, key); +if (unlikely (!v)) + goto out; + +if (likely (is_number (v))) +set_number (v,nvalue (v) + n); +else if (is_nil (v)) +set_number (v, n); +else +kp_error (ks,"use '+=' operator on non-number value\n" ); + + out: +tab_unlock (t); +} + +static ktap_val_t *tab_setstr (ktap_state_t *ks,ktap_tab_t *t, +const ktap_str_t *key) +{ +ktap_val_t k; +ktap_node_t *n =hashstr (t, key); +do { +if (is_string (&n->key) &&rawtsvalue (&n->key) == key) +return &n->val; + }while ((n = n->next)); +set_string (&k, key); +return kp_tab_newkey (ks, t, &k); +} + +void kp_tab_setstr (ktap_state_t *ks,ktap_tab_t *t,const ktap_str_t *key, +const ktap_val_t *val) +{ +ktap_val_t *v; +unsigned long flags; + +tab_lock (t); + v =tab_setstr (ks, t, key); +if (likely (v)) +set_obj (v, val); +tab_unlock (t); +} + +void kp_tab_incrstr (ktap_state_t *ks,ktap_tab_t *t,const ktap_str_t *key, +ktap_number n) +{ +ktap_val_t *v; +unsigned long flags; + +tab_lock (t); + v =tab_setstr (ks, t, key); +if (unlikely (!v)) + goto out; + +if (likely (is_number (v))) +set_number (v,nvalue (v) + n); +else if (is_nil (v)) +set_number (v, n); +else +kp_error (ks,"use '+=' operator on non-number value\n" ); + out: +tab_unlock (t); +} + +static ktap_val_t *tab_set (ktap_state_t *ks,ktap_tab_t *t, +const ktap_val_t *key) +{ +ktap_node_t *n; + +if (is_string (key)) { +return tab_setstr (ks, t,rawtsvalue (key)); + }else if (is_number (key)) { +ktap_number nk =nvalue (key); +uint32_t k = (ktap_number )nk; +if (nk == (ktap_number )k) +return tab_setint (ks, t, k); + }else if (itype (key) ==KTAP_TKSTACK ) { +/* change stack into string */ +ktap_str_t *bt =kp_obj_kstack2str (ks, key->val.stack.depth, + key->val.stack.skip); +if (!bt) +return NULL ; +return tab_setstr (ks, t, bt); + }else if (is_eventstr (key)) { +const ktap_str_t *ts; + +if (!ks->current_event) { +kp_error (ks, +"cannot stringify event str in invalid context\n" ); +return NULL ; + } + + ts =kp_event_stringify (ks); +if (!ts) +return NULL ; + +return tab_setstr (ks, t, ts); +/* Else use the generic lookup. */ + }else if (is_nil (key)) { +kp_error (ks,"table nil index\n" ); +return NULL ; + } + n =hashkey (t, key); +do { +if (kp_obj_equal (&n->key, key)) +return &n->val; + }while ((n = n->next)); +return kp_tab_newkey (ks, t, key); +} + +void kp_tab_set (ktap_state_t *ks,ktap_tab_t *t, +const ktap_val_t *key,const ktap_val_t *val) +{ +ktap_val_t *v; +unsigned long flags; + +tab_lock (t); + v =tab_set (ks, t, key); +if (likely (v)) +set_obj (v, val); +tab_unlock (t); +} + +void kp_tab_incr (ktap_state_t *ks,ktap_tab_t *t,ktap_val_t *key, +ktap_number n) +{ +ktap_val_t *v; +unsigned long flags; + +tab_lock (t); + v =tab_set (ks, t, key); +if (unlikely (!v)) + goto out; + +if (likely (is_number (v))) +set_number (v,nvalue (v) + n); +else if (is_nil (v)) +set_number (v, n); +else +kp_error (ks,"use '+=' operator on non-number value\n" ); + out: +tab_unlock (t); +} + + +/* -- Table traversal ----------------------------------------------------- */ + +/* Get the traversal index of a key. */ +static uint32_t keyindex (ktap_state_t *ks,ktap_tab_t *t, +const ktap_val_t *key) +{ +if (is_number (key)) { +ktap_number nk =nvalue (key); +uint32_t k = (uint32_t )nk; +/* Array key indexes: [0..t->asize-1] */ +if ((uint32_t )k < t->asize && nk == (ktap_number )k) +return (uint32_t )k; + } + +if (!is_nil (key)) { +ktap_node_t *n =hashkey (t, key); +do { +if (kp_obj_equal (&n->key, key)) +return t->asize + (uint32_t )(n - (t->node)); +/* Hash key indexes: [t->asize..t->asize+t->nmask] */ + }while ((n = n->next)); +kp_error (ks,"table next index\n" ); +return 0 ;/* unreachable */ + } +return ~0u ;/* A nil key starts the traversal. */ +} + +/* Advance to the next step in a table traversal. */ +int kp_tab_next (ktap_state_t *ks,ktap_tab_t *t,ktap_val_t *key) +{ +unsigned long flags; +uint32_t i; + +tab_lock (t); + i =keyindex (ks, t, key);/* Find predecessor key index. */ + +/* First traverse the array keys. */ +for (i++; i < t->asize; i++) +if (!is_nil (arrayslot (t, i))) { +set_number (key, i); +set_obj (key +1 ,arrayslot (t, i)); +tab_unlock (t); +return 1 ; + } +/* Then traverse the hash keys. */ +for (i -= t->asize; i <= t->hmask; i++) { +ktap_node_t *n = &t->node[i]; +if (!is_nil (&n->val)) { +set_obj (key, &n->key); +set_obj (key +1 , &n->val); +tab_unlock (t); +return 1 ; + } + } +tab_unlock (t); +return 0 ;/* End of traversal. */ +} + +/* -- Table length calculation -------------------------------------------- */ + +int kp_tab_len (ktap_state_t *ks,ktap_tab_t *t) +{ +unsigned long flags; +int i, len =0 ; + +tab_lock (t); +for (i =0 ; i < t->asize; i++) { +ktap_val_t *v = &t->array[i]; + +if (is_nil (v)) +continue ; + len++; + } + +for (i =0 ; i <= t->hmask; i++) { +ktap_node_t *n = &t->node[i]; + +if (is_nil (&n->key)) +continue ; + + len++; + } +tab_unlock (t); +return len; +} + +static void string_convert (char *output,const char *input) +{ +if (strlen (input) >32 ) { +strncpy (output, input,32 -4 ); +memset (output +32 -4 ,'.' ,3 ); + }else +memcpy (output, input,strlen (input)); +} + +typedef struct ktap_node2 { +ktap_val_t key; +ktap_val_t val; +}ktap_node2_t ; + +static int hist_record_cmp (const void *i,const void *j) +{ +ktap_number n1 =nvalue (&((const ktap_node2_t *)i)->val); +ktap_number n2 =nvalue (&((const ktap_node2_t *)j)->val); + +if (n1 == n2) +return 0 ; +else if (n1 < n2) +return 1 ; +else +return -1 ; +} + +/* todo: make histdump to be faster, just need to sort n entries, not all */ + +/* print_hist: key should be number/string/ip, value must be number */ +static void tab_histdump (ktap_state_t *ks,ktap_tab_t *t,int shownums) +{ +long start_time, delta_time; +uint32_t i, asize = t->asize; +ktap_val_t *array = t->array; +uint32_t hmask = t->hmask; +ktap_node_t *node = t->node; +ktap_node2_t *sort_mem; +char dist_str[39 ]; +int total =0 , sum =0 ; + + start_time =gettimeofday_ns (); + + sort_mem =kmalloc ((t->asize + t->hnum) *sizeof (ktap_node2_t), +GFP_KERNEL ); +if (!sort_mem) +return ; + +/* copy all values in table into sort_mem. */ +for (i =0 ; i < asize; i++) { +ktap_val_t *val = &array[i]; +if (is_nil (val)) +continue ; + +if (!is_number (val)) { +kp_error (ks,"print_hist only can print number\n" ); + goto out; + } + +set_number (&sort_mem[total].key, i); +set_obj (&sort_mem[total].val, val); + sum +=nvalue (val); + total++; + } + +for (i =0 ; i <= hmask; i++) { +ktap_node_t *n = &node[i]; +ktap_val_t *val = &n->val; + +if (is_nil (val)) +continue ; + +if (!is_number (val)) { +kp_error (ks,"print_hist only can print number\n" ); + goto out; + } + +set_obj (&sort_mem[total].key, &n->key); +set_obj (&sort_mem[total].val, val); + sum +=nvalue (val); + total++; + } + +/* sort */ +sort (sort_mem, total,sizeof (ktap_node2_t), hist_record_cmp,NULL ); + + dist_str[sizeof (dist_str) -1 ] ='\0' ; + +for (i =0 ; i < total; i++) { +ktap_val_t *key = &sort_mem[i].key; +ktap_number num =nvalue (&sort_mem[i].val); +int ratio; + +if (!--shownums) +break ; + +memset (dist_str,' ' ,sizeof (dist_str) -1 ); + ratio = (num * (sizeof (dist_str) -1 )) / sum; +memset (dist_str,'@' , ratio); + +if (is_string (key)) { +//char buf[32] = {0}; + +//string_convert(buf, svalue(key)); +if (rawtsvalue (key)->len >32 ) { +kp_puts (ks,svalue (key)); +kp_printf (ks,"%s\n%d\n\n" , dist_str, num); + }else { +kp_printf (ks,"%31s |%s%-7d\n" ,svalue (key), + dist_str, num); + } + }else if (is_number (key)) { +kp_printf (ks,"%31d |%s%-7d\n" ,nvalue (key), + dist_str, num); + }else if (is_kip (key)) { +char str[KSYM_SYMBOL_LEN ]; +char buf[32 ] = {0 }; + +SPRINT_SYMBOL (str,nvalue (key)); +string_convert (buf, str); +kp_printf (ks,"%31s |%s%-7d\n" , buf, dist_str, num); + } + } + +if (!shownums && total) +kp_printf (ks,"%31s |\n" ,"..." ); + + out: +kfree (sort_mem); + + delta_time = (gettimeofday_ns () - start_time) /NSEC_PER_USEC ; +kp_verbose_printf (ks,"tab_histdump time: %d (us)\n" , delta_time); +} + +#define DISTRIBUTION_STR "------------- Distribution -------------" +void kp_tab_print_hist (ktap_state_t *ks,ktap_tab_t *t,int n) +{ +kp_printf (ks,"%31s%s%s\n" ,"value " ,DISTRIBUTION_STR ," count" ); +tab_histdump (ks, t, n); +} +
tools/ktap/runtime/kp_tab.h new file +59 -0
59 lines not on this page · raw message
Signature · 1 line
1.8.1.4
PATCH 12/28 ktap: add runtime/kp_obj.[c|h]
In reply to Jovi Zhangweiruntime/kp_obj.c include some common ktap object operation, like object dump, object length.
It also include generic memory allocate code.
Exposed functions:
1). kp_malloc/kp_zalloc/kp_free
2). kp_obj_dump/kp_obj_show
3). kp_obj_rawequal
4). kp_obj_len
5). kp_obj_new
allocate new object, all object is linked in G(ks)->allgc.
6). kp_obj_kstack2str
convert kernel stack to string.
7). kp_obj_freeall
free all object, called in kp_vm_exit.
--- tools/ktap/runtime/kp_obj.c | 281 ++++++++++++++++++++++++++++++++++++++++++++ tools/ktap/runtime/kp_obj.h | 19 +++ 2 files changed, 300 insertions(+) create mode 100644 tools/ktap/runtime/kp_obj.c create mode 100644 tools/ktap/runtime/kp_obj.h
tools/ktap/runtime/kp_obj.c new file +281 -0
@@ -0,0 +1,281 @@ +/* +* kp_obj.c - ktap object generic operation +* +* This file is part of ktap by Jovi Zhangwei. +* +* Copyright (C) 2012-2014 Jovi Zhangwei <[email protected]>. +* +* Adapted from luajit and lua interpreter. +* Copyright (C) 2005-2014 Mike Pall. +* Copyright (C) 1994-2008 Lua.org, PUC-Rio. +* +* ktap is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* ktap is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include <linux/stacktrace.h> +#include <linux/module.h> +#include <linux/kallsyms.h> +#include <linux/slab.h> +#include "../include/ktap_types.h" +#include "kp_obj.h" +#include "kp_str.h" +#include "kp_tab.h" +#include "ktap.h" +#include "kp_vm.h" +#include "kp_transport.h" + +/* Error message strings. */ +const char *kp_err_allmsg = +#define ERRDEF(name, msg) msg "\0" +#include "../include/ktap_errmsg.h" +; + +/* memory allocation flag */ +#define KTAP_ALLOC_FLAGS ((GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN) \ + & ~__GFP_WAIT) + +void *kp_malloc (ktap_state_t *ks,int size) +{ +void *addr; + + addr =kmalloc (size,KTAP_ALLOC_FLAGS ); +if (unlikely (!addr)) { +kp_error (ks,"kmalloc failed\n" ); + } +return addr; +} + +void *kp_zalloc (ktap_state_t *ks,int size) +{ +void *addr; + + addr =kzalloc (size,KTAP_ALLOC_FLAGS ); +if (unlikely (!addr)) +kp_error (ks,"kzalloc failed\n" ); +return addr; +} + +void kp_free (ktap_state_t *ks,void *addr) +{ +kfree (addr); +} + + +void kp_obj_dump (ktap_state_t *ks,const ktap_val_t *v) +{ +switch (itype (v)) { +case KTAP_TNIL : +kp_puts (ks,"NIL" ); +break ; +case KTAP_TTRUE : +kp_printf (ks,"true" ); +break ; +case KTAP_TFALSE : +kp_printf (ks,"false" ); +break ; +case KTAP_TNUM : +kp_printf (ks,"NUM %ld" ,nvalue (v)); +break ; +case KTAP_TLIGHTUD : +kp_printf (ks,"LIGHTUD 0x%lx" , (unsigned long )pvalue (v)); +break ; +case KTAP_TFUNC : +kp_printf (ks,"FUNCTION 0x%lx" , (unsigned long )fvalue (v)); +break ; +case KTAP_TSTR : +kp_printf (ks,"STR #%s" ,svalue (v)); +break ; +case KTAP_TTAB : +kp_printf (ks,"TABLE 0x%lx" , (unsigned long )hvalue (v)); +break ; +default : +kp_printf (ks,"GCVALUE 0x%lx" , (unsigned long )gcvalue (v)); +break ; + } +} + +void kp_obj_show (ktap_state_t *ks,const ktap_val_t *v) +{ +switch (itype (v)) { +case KTAP_TNIL : +kp_puts (ks,"nil" ); +break ; +case KTAP_TTRUE : +kp_puts (ks,"true" ); +break ; +case KTAP_TFALSE : +kp_puts (ks,"false" ); +break ; +case KTAP_TNUM : +kp_printf (ks,"%ld" ,nvalue (v)); +break ; +case KTAP_TLIGHTUD : +kp_printf (ks,"lightud 0x%lx" , (unsigned long )pvalue (v)); +break ; +case KTAP_TCFUNC : +kp_printf (ks,"cfunction 0x%lx" , (unsigned long )fvalue (v)); +break ; +case KTAP_TFUNC : +kp_printf (ks,"function 0x%lx" , (unsigned long )gcvalue (v)); +break ; +case KTAP_TSTR : +kp_puts (ks,svalue (v)); +break ; +case KTAP_TTAB : +kp_printf (ks,"table 0x%lx" , (unsigned long )hvalue (v)); +break ; +case KTAP_TEVENTSTR : +/* check event context */ +if (!ks->current_event) { +kp_error (ks, +"cannot stringify event str in invalid context\n" ); +return ; + } + +kp_transport_event_write (ks, ks->current_event); +break ; +case KTAP_TKSTACK : +kp_transport_print_kstack (ks, v->val.stack.depth, + v->val.stack.skip); +break ; +default : +kp_error (ks,"print unknown value type: %d\n" ,itype (v)); +break ; + } +} + + +/* +* equality of ktap values. +*/ +int kp_obj_rawequal (const ktap_val_t *t1,const ktap_val_t *t2) +{ +switch (itype (t1)) { +case KTAP_TNIL : +case KTAP_TTRUE : +case KTAP_TFALSE : +return 1 ; +case KTAP_TNUM : +return nvalue (t1) ==nvalue (t2); +case KTAP_TLIGHTUD : +return pvalue (t1) ==pvalue (t2); +case KTAP_TFUNC : +return fvalue (t1) ==fvalue (t2); +case KTAP_TSTR : +return rawtsvalue (t1) ==rawtsvalue (t2); +case KTAP_TTAB : +return hvalue (t1) ==hvalue (t2); +default : +return gcvalue (t1) ==gcvalue (t2); + } + +return 0 ; +} + +/* +* ktap will not use lua's length operator for table, +* also # is not for length operator any more in ktap. +*/ +int kp_obj_len (ktap_state_t *ks,const ktap_val_t *v) +{ +switch (itype (v)) { +case KTAP_TTAB : +return kp_tab_len (ks,hvalue (v)); +case KTAP_TSTR : +return rawtsvalue (v)->len; +default : +kp_printf (ks,"cannot get length of type %d\n" , v->type); +return -1 ; + } +return 0 ; +} + +/* need to protect allgc field? */ +ktap_obj_t *kp_obj_new (ktap_state_t *ks,size_t size) +{ +ktap_obj_t *o, **list; + +if (ks !=G (ks)->mainthread) { +kp_error (ks,"kp_obj_new only can be called in mainthread\n" ); +return NULL ; + } + + o =kp_malloc (ks, size); +if (unlikely (!o)) +return NULL ; + + list = &G (ks)->allgc; +gch (o)->nextgc = *list; + *list = o; + +return o; +} + + +/* this function may be time consuming, move out from table set/get? */ +ktap_str_t *kp_obj_kstack2str (ktap_state_t *ks,uint16_t depth,uint16_t skip) +{ +struct stack_trace trace; +unsigned long *bt; +char *btstr, *p; +int i; + + bt =kp_this_cpu_print_buffer (ks);/* use print percpu buffer */ + trace.nr_entries =0 ; + trace.skip = skip; + trace.max_entries = depth; + trace.entries = (unsigned long *)(bt +1 ); +save_stack_trace (&trace); + +/* convert backtrace to string */ + p = btstr =kp_this_cpu_temp_buffer (ks); +for (i =0 ; i < trace.nr_entries; i++) { +unsigned long addr = trace.entries[i]; + +if (addr ==ULONG_MAX ) +break ; + + p +=sprint_symbol (p, addr); + *p++ ='\n' ; + } + +return kp_str_new (ks, btstr, p - btstr); +} + +static void free_gclist (ktap_state_t *ks,ktap_obj_t *o) +{ +while (o) { +ktap_obj_t *next; + + next =gch (o)->nextgc; +switch (gch (o)->gct) { +case ~KTAP_TTAB : +kp_tab_free (ks, (ktap_tab_t *)o); +break ; +case ~KTAP_TUPVAL : +kp_freeupval (ks, (ktap_upval_t *)o); +break ; +default : +kp_free (ks, o); + } + o = next; + } +} + +void kp_obj_freeall (ktap_state_t *ks) +{ +free_gclist (ks,G (ks)->allgc); +G (ks)->allgc =NULL ; +} +
tools/ktap/runtime/kp_obj.h new file +19 -0
@@ -0,0 +1,19 @@ +#ifndef __KTAP_OBJ_H__ +#define __KTAP_OBJ_H__ + +void *kp_malloc (ktap_state_t *ks,int size); +void *kp_zalloc (ktap_state_t *ks,int size); +void kp_free (ktap_state_t *ks,void *addr); + +void kp_obj_dump (ktap_state_t *ks,const ktap_val_t *v); +void kp_obj_show (ktap_state_t *ks,const ktap_val_t *v); +int kp_obj_len (ktap_state_t *ks,const ktap_val_t *rb); +ktap_obj_t *kp_obj_new (ktap_state_t *ks,size_t size); +int kp_obj_rawequal (const ktap_val_t *t1,const ktap_val_t *t2); +ktap_str_t *kp_obj_kstack2str (ktap_state_t *ks,uint16_t depth,uint16_t skip); +void kp_obj_freeall (ktap_state_t *ks); + +#define kp_obj_equal (o1, o2) \ + (((o1)->type == (o2)->type) && kp_obj_rawequal(o1, o2)) + +#endif /* __KTAP_OBJ_H__ */
Signature · 1 line
1.8.1.4
PATCH 13/28 ktap: add runtime/kp_transport.[c|h]
In reply to Jovi Zhangweiktap transport functionality is based on ftrace ring buffer.
All contents generated by ktap runtime will send to userspace through this ring buffer.
Actually the concept of ktap transport is similar with ftrace, for example, it will write event binary data into ring buffer when use call: 'print(argstr)' or 'print(stack())', and the content will be parse in consumer side, not producer side.
Userspace reader thread will consume content in ring buffer through '/sys/kernel/debug/ktap/trace_pipe_$pid'
A lot of code in this file is duplicated with kernel trace_output.c.
Exposed functions:
1). kp_transport_init
2). kp_transport_exit
3). __kp_bputs/__kp_puts/kp_printf
4). kp_transport_write
5). kp_transport_event_write
write a event content into ring buffer, called by: 'print(argstr)'
6). kp_transport_print_kstack
write a kernel stack into ring buffer, called by: 'print(stack())'
--- tools/ktap/runtime/kp_transport.c | 649 ++++++++++++++++++++++++++++++++++++++ tools/ktap/runtime/kp_transport.h | 13 + 2 files changed, 662 insertions(+) create mode 100644 tools/ktap/runtime/kp_transport.c create mode 100644 tools/ktap/runtime/kp_transport.h
tools/ktap/runtime/kp_transport.c new file +649 -0
@@ -0,0 +1,649 @@ +/* +* kp_transport.c - ktap transport functionality +* +* This file is part of ktap by Jovi Zhangwei. +* +* Copyright (C) 2012-2014 Jovi Zhangwei <[email protected]>. +* +* ktap is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* ktap is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include <linux/debugfs.h> +#include <linux/ftrace_event.h> +#include <linux/stacktrace.h> +#include <linux/clocksource.h> +#include <asm/uaccess.h> +#include <linux/slab.h> +#include <linux/module.h> +#include <linux/kallsyms.h> +#include "../include/ktap_types.h" +#include "ktap.h" +#include "kp_events.h" +#include "kp_transport.h" + +struct ktap_trace_iterator { +struct ring_buffer *buffer; +int print_timestamp; +void *private; + +struct trace_iterator iter; +}; + +enum ktap_trace_type { + __TRACE_FIRST_TYPE =0 , + +TRACE_FN =1 ,/* must be same as ftrace definition in kernel */ +TRACE_PRINT , +TRACE_BPUTS , +TRACE_STACK , +TRACE_USER_STACK , + + __TRACE_LAST_TYPE, +}; + +#define KTAP_TRACE_ITER (iter) \ + container_of(iter, struct ktap_trace_iterator, iter) + +static +ssize_t _trace_seq_to_user (struct trace_seq *s,char __user *ubuf,size_t cnt) +{ +int len; +int ret; + +if (!cnt) +return 0 ; + +if (s->len <= s->readpos) +return -EBUSY ; + + len = s->len - s->readpos; +if (cnt > len) + cnt = len; + ret =copy_to_user (ubuf, s->buffer + s->readpos, cnt); +if (ret == cnt) +return -EFAULT ; + + cnt -= ret; + + s->readpos += cnt; +return cnt; +} + +int _trace_seq_puts (struct trace_seq *s,const char *str) +{ +int len =strlen (str); + +if (s->full) +return 0 ; + +if (len > ((PAGE_SIZE -1 ) - s->len)) { + s->full =1 ; +return 0 ; + } + +memcpy (s->buffer + s->len, str, len); + s->len += len; + +return len; +} + +static int trace_empty (struct trace_iterator *iter) +{ +struct ktap_trace_iterator *ktap_iter =KTAP_TRACE_ITER (iter); +int cpu; + +for_each_online_cpu (cpu) { +if (!ring_buffer_empty_cpu (ktap_iter->buffer, cpu)) +return 0 ; + } + +return 1 ; +} + +static void trace_consume (struct trace_iterator *iter) +{ +struct ktap_trace_iterator *ktap_iter =KTAP_TRACE_ITER (iter); + +ring_buffer_consume (ktap_iter->buffer, iter->cpu, &iter->ts, + &iter->lost_events); +} + +unsigned long long ns2usecs (cycle_t nsec) +{ + nsec +=500 ; +do_div (nsec,1000 ); +return nsec; +} + +static int trace_print_timestamp (struct trace_iterator *iter) +{ +struct trace_seq *s = &iter->seq; +unsigned long long t; +unsigned long secs, usec_rem; + + t =ns2usecs (iter->ts); + usec_rem =do_div (t,USEC_PER_SEC ); + secs = (unsigned long )t; + +return trace_seq_printf (s,"%5lu.%06lu: " , secs, usec_rem); +} + +/* todo: export kernel function ftrace_find_event in future, and make faster */ +static struct trace_event *(*ftrace_find_event)(int type); + +static enum print_line_t print_trace_fmt (struct trace_iterator *iter) +{ +struct ktap_trace_iterator *ktap_iter =KTAP_TRACE_ITER (iter); +struct trace_entry *entry = iter->ent; +struct trace_event *ev; + + ev =ftrace_find_event (entry->type); + +if (ktap_iter->print_timestamp && !trace_print_timestamp (iter)) +return TRACE_TYPE_PARTIAL_LINE ; + +if (ev) { +int ret = ev->funcs->trace (iter,0 , ev); + +/* overwrite '\n' at the ending */ + iter->seq.buffer[iter->seq.len -1 ] ='\0' ; + iter->seq.len--; +return ret; + } + +return TRACE_TYPE_PARTIAL_LINE ; +} + +static enum print_line_t print_trace_stack (struct trace_iterator *iter) +{ +struct trace_entry *entry = iter->ent; +struct stack_trace trace; +char str[KSYM_SYMBOL_LEN ]; +int i; + + trace.entries = (unsigned long *)(entry +1 ); + trace.nr_entries = (iter->ent_size -sizeof (*entry)) / +sizeof (unsigned long ); + +if (!_trace_seq_puts (&iter->seq,"<stack trace>\n" )) +return TRACE_TYPE_PARTIAL_LINE ; + +for (i =0 ; i < trace.nr_entries; i++) { +unsigned long p = trace.entries[i]; + +if (p ==ULONG_MAX ) +break ; + +sprint_symbol (str, p); +if (!trace_seq_printf (&iter->seq," => %s\n" , str)) +return TRACE_TYPE_PARTIAL_LINE ; + } + +return TRACE_TYPE_HANDLED ; +} + +struct ktap_ftrace_entry { +struct trace_entry entry; +unsigned long ip; +unsigned long parent_ip; +}; + +static enum print_line_t print_trace_fn (struct trace_iterator *iter) +{ +struct ktap_trace_iterator *ktap_iter =KTAP_TRACE_ITER (iter); +struct ktap_ftrace_entry *field = (struct ktap_ftrace_entry *)iter->ent; +char str[KSYM_SYMBOL_LEN ]; + +if (ktap_iter->print_timestamp && !trace_print_timestamp (iter)) +return TRACE_TYPE_PARTIAL_LINE ; + +sprint_symbol (str, field->ip); +if (!_trace_seq_puts (&iter->seq, str)) +return TRACE_TYPE_PARTIAL_LINE ; + +if (!_trace_seq_puts (&iter->seq," <- " )) +return TRACE_TYPE_PARTIAL_LINE ; + +sprint_symbol (str, field->parent_ip); +if (!_trace_seq_puts (&iter->seq, str)) +return TRACE_TYPE_PARTIAL_LINE ; + +return TRACE_TYPE_HANDLED ; +} + +static enum print_line_t print_trace_bputs (struct trace_iterator *iter) +{ +if (!_trace_seq_puts (&iter->seq, + (const char *)(*(unsigned long *)(iter->ent +1 )))) +return TRACE_TYPE_PARTIAL_LINE ; + +return TRACE_TYPE_HANDLED ; +} + +static enum print_line_t print_trace_line (struct trace_iterator *iter) +{ +struct trace_entry *entry = iter->ent; +char *str = (char *)(entry +1 ); + +if (entry->type ==TRACE_PRINT ) { +if (!trace_seq_printf (&iter->seq,"%s" , str)) +return TRACE_TYPE_PARTIAL_LINE ; + +return TRACE_TYPE_HANDLED ; + } + +if (entry->type ==TRACE_BPUTS ) +return print_trace_bputs (iter); + +if (entry->type ==TRACE_STACK ) +return print_trace_stack (iter); + +if (entry->type ==TRACE_FN ) +return print_trace_fn (iter); + +return print_trace_fmt (iter); +} + +static struct trace_entry * +peek_next_entry (struct trace_iterator *iter,int cpu,u64 *ts, +unsigned long *lost_events) +{ +struct ktap_trace_iterator *ktap_iter =KTAP_TRACE_ITER (iter); +struct ring_buffer_event *event; + + event =ring_buffer_peek (ktap_iter->buffer, cpu, ts, lost_events); +if (event) { + iter->ent_size =ring_buffer_event_length (event); +return ring_buffer_event_data (event); + } + +return NULL ; +} + +static struct trace_entry * +__find_next_entry (struct trace_iterator *iter,int *ent_cpu, +unsigned long *missing_events,u64 *ent_ts) +{ +struct ktap_trace_iterator *ktap_iter =KTAP_TRACE_ITER (iter); +struct ring_buffer *buffer = ktap_iter->buffer; +struct trace_entry *ent, *next =NULL ; +unsigned long lost_events =0 , next_lost =0 ; +u64 next_ts =0 , ts; +int next_cpu =-1 ; +int next_size =0 ; +int cpu; + +for_each_online_cpu (cpu) { +if (ring_buffer_empty_cpu (buffer, cpu)) +continue ; + + ent =peek_next_entry (iter, cpu, &ts, &lost_events); +/* +* Pick the entry with the smallest timestamp: +*/ +if (ent && (!next || ts < next_ts)) { + next = ent; + next_cpu = cpu; + next_ts = ts; + next_lost = lost_events; + next_size = iter->ent_size; + } + } + + iter->ent_size = next_size; + +if (ent_cpu) + *ent_cpu = next_cpu; + +if (ent_ts) + *ent_ts = next_ts; + +if (missing_events) + *missing_events = next_lost; + +return next; +} + +/* Find the next real entry, and increment the iterator to the next entry */ +static void *trace_find_next_entry_inc (struct trace_iterator *iter) +{ + iter->ent =__find_next_entry (iter, &iter->cpu, + &iter->lost_events, &iter->ts); +if (iter->ent) + iter->idx++; + +return iter->ent ? iter :NULL ; +} + +static void poll_wait_pipe (void ) +{ +set_current_state (TASK_INTERRUPTIBLE ); +/* sleep for 100 msecs, and try again. */ +schedule_timeout (HZ /10 ); +} + +static int tracing_wait_pipe (struct file *filp) +{ +struct trace_iterator *iter = filp->private_data; +struct ktap_trace_iterator *ktap_iter =KTAP_TRACE_ITER (iter); +ktap_state_t *ks = ktap_iter->private; + +while (trace_empty (iter)) { + +if ((filp->f_flags &O_NONBLOCK )) { +return -EAGAIN ; + } + +mutex_unlock (&iter->mutex); + +poll_wait_pipe (); + +mutex_lock (&iter->mutex); + +if (G (ks)->wait_user &&trace_empty (iter)) +return -EINTR ; + } + +return 1 ; +} + +static ssize_t +tracing_read_pipe (struct file *filp,char __user *ubuf,size_t cnt, +loff_t *ppos) +{ +struct trace_iterator *iter = filp->private_data; +ssize_t sret; + +/* return any leftover data */ + sret =_trace_seq_to_user (&iter->seq, ubuf, cnt); +if (sret != -EBUSY ) +return sret; +/* +* Avoid more than one consumer on a single file descriptor +* This is just a matter of traces coherency, the ring buffer itself +* is protected. +*/ +mutex_lock (&iter->mutex); + +waitagain: + sret =tracing_wait_pipe (filp); +if (sret <=0 ) + goto out; + +/* stop when tracing is finished */ +if (trace_empty (iter)) { + sret =0 ; + goto out; + } + +if (cnt >=PAGE_SIZE ) + cnt =PAGE_SIZE -1 ; + +/* reset all but tr, trace, and overruns */ +memset (&iter->seq,0 , +sizeof (struct trace_iterator ) - + offsetof(struct trace_iterator , seq)); + iter->pos =-1 ; + +while (trace_find_next_entry_inc (iter) !=NULL ) { +enum print_line_t ret; +int len = iter->seq.len; + + ret =print_trace_line (iter); +if (ret ==TRACE_TYPE_PARTIAL_LINE ) { +/* don't print partial lines */ + iter->seq.len = len; +break ; + } +if (ret !=TRACE_TYPE_NO_CONSUME ) +trace_consume (iter); + +if (iter->seq.len >= cnt) +break ; + +/* +* Setting the full flag means we reached the trace_seq buffer +* size and we should leave by partial output condition above. +* One of the trace_seq_* functions is not used properly. +*/ +WARN_ONCE (iter->seq.full,"full flag set for trace type %d" , + iter->ent->type); + } + +/* Now copy what we have to the user */ + sret =_trace_seq_to_user (&iter->seq, ubuf, cnt); +if (iter->seq.readpos >= iter->seq.len) +trace_seq_init (&iter->seq); + +/* +* If there was nothing to send to user, in spite of consuming trace +* entries, go back to wait for more entries. +*/ +if (sret == -EBUSY ) + goto waitagain; + +out: +mutex_unlock (&iter->mutex); + +return sret; +} + +static int tracing_open_pipe (struct inode *inode,struct file *filp) +{ +struct ktap_trace_iterator *ktap_iter; +ktap_state_t *ks = inode->i_private; + +/* create a buffer to store the information to pass to userspace */ + ktap_iter =kzalloc (sizeof (*ktap_iter),GFP_KERNEL ); +if (!ktap_iter) +return -ENOMEM ; + + ktap_iter->private = ks; + ktap_iter->buffer =G (ks)->buffer; + ktap_iter->print_timestamp =G (ks)->parm->print_timestamp; +mutex_init (&ktap_iter->iter.mutex); + filp->private_data = &ktap_iter->iter; + +nonseekable_open (inode, filp); + +return 0 ; +} + +static int tracing_release_pipe (struct inode *inode,struct file *file) +{ +struct trace_iterator *iter = file->private_data; +struct ktap_trace_iterator *ktap_iter =KTAP_TRACE_ITER (iter); + +mutex_destroy (&iter->mutex); +kfree (ktap_iter); +return 0 ; +} + +static const struct file_operations tracing_pipe_fops = { + .open = tracing_open_pipe, + .read = tracing_read_pipe, + .splice_read =NULL , + .release = tracing_release_pipe, + .llseek = no_llseek, +}; + +/* +* preempt disabled in ring_buffer_lock_reserve +* +* The implementation is similar with funtion __ftrace_trace_stack. +*/ +void kp_transport_print_kstack (ktap_state_t *ks,uint16_t depth,uint16_t skip) +{ +struct ring_buffer *buffer =G (ks)->buffer; +struct ring_buffer_event *event; +struct trace_entry *entry; +int size; + + size = depth *sizeof (unsigned long ); + event =ring_buffer_lock_reserve (buffer,sizeof (*entry) + size); +if (!event) { +KTAP_STATS (ks)->events_missed +=1 ; +return ; + }else { +struct stack_trace trace; + + entry =ring_buffer_event_data (event); +tracing_generic_entry_update (entry,0 ,0 ); + entry->type =TRACE_STACK ; + + trace.nr_entries =0 ; + trace.skip = skip; + trace.max_entries = depth; + trace.entries = (unsigned long *)(entry +1 ); +save_stack_trace (&trace); + +ring_buffer_unlock_commit (buffer, event); + } +} + +void kp_transport_event_write (ktap_state_t *ks,struct ktap_event_data *e) +{ +struct ring_buffer *buffer =G (ks)->buffer; +struct ring_buffer_event *event; +struct trace_entry *ev_entry = e->data->raw->data; +struct trace_entry *entry; +int entry_size = e->data->raw->size; + + event =ring_buffer_lock_reserve (buffer, entry_size + +sizeof (struct ftrace_event_call *)); +if (!event) { +KTAP_STATS (ks)->events_missed +=1 ; +return ; + }else { + entry =ring_buffer_event_data (event); + +memcpy (entry, ev_entry, entry_size); + +ring_buffer_unlock_commit (buffer, event); + } +} + +void kp_transport_write (ktap_state_t *ks,const void *data,size_t length) +{ +struct ring_buffer *buffer =G (ks)->buffer; +struct ring_buffer_event *event; +struct trace_entry *entry; +int size; + + size =sizeof (struct trace_entry ) + length; + + event =ring_buffer_lock_reserve (buffer, size); +if (!event) { +KTAP_STATS (ks)->events_missed +=1 ; +return ; + }else { + entry =ring_buffer_event_data (event); + +tracing_generic_entry_update (entry,0 ,0 ); + entry->type =TRACE_PRINT ; +memcpy (entry +1 , data, length); + +ring_buffer_unlock_commit (buffer, event); + } +} + +/* general print function */ +void kp_printf (ktap_state_t *ks,const char *fmt, ...) +{ +char buff[1024 ]; +va_list args; +int len; + +va_start (args, fmt); + len =vscnprintf (buff,1024 , fmt, args); +va_end (args); + + buff[len] ='\0' ; +kp_transport_write (ks, buff, len +1 ); +} + +void __kp_puts (ktap_state_t *ks,const char *str) +{ +kp_transport_write (ks, str,strlen (str) +1 ); +} + +void __kp_bputs (ktap_state_t *ks,const char *str) +{ +struct ring_buffer *buffer =G (ks)->buffer; +struct ring_buffer_event *event; +struct trace_entry *entry; +int size; + + size =sizeof (struct trace_entry ) +sizeof (unsigned long *); + + event =ring_buffer_lock_reserve (buffer, size); +if (!event) { +KTAP_STATS (ks)->events_missed +=1 ; +return ; + }else { + entry =ring_buffer_event_data (event); + +tracing_generic_entry_update (entry,0 ,0 ); + entry->type =TRACE_BPUTS ; + *(unsigned long *)(entry +1 ) = (unsigned long )str; + +ring_buffer_unlock_commit (buffer, event); + } +} + +void kp_transport_exit (ktap_state_t *ks) +{ +if (G (ks)->buffer) +ring_buffer_free (G (ks)->buffer); +debugfs_remove (G (ks)->trace_pipe_dentry); +} + +#define TRACE_BUF_SIZE_DEFAULT 1441792UL/* 16384 * 88 (sizeof(entry)) */ + +int kp_transport_init (ktap_state_t *ks,struct dentry *dir) +{ +struct ring_buffer *buffer; +struct dentry *dentry; +char filename[32 ] = {0 }; + +#ifdef CONFIG_PPC64 + ftrace_find_event = (void *)kallsyms_lookup_name (".ftrace_find_event" ); +#else + ftrace_find_event = (void *)kallsyms_lookup_name ("ftrace_find_event" ); +#endif +if (!ftrace_find_event) { +printk ("ktap: cannot lookup ftrace_find_event in kallsyms\n" ); +return -EINVAL ; + } + + buffer =ring_buffer_alloc (TRACE_BUF_SIZE_DEFAULT ,RB_FL_OVERWRITE ); +if (!buffer) +return -ENOMEM ; + +sprintf (filename,"trace_pipe_%d" , (int )task_tgid_vnr (current)); + + dentry =debugfs_create_file (filename,0444 , dir, + ks, &tracing_pipe_fops); +if (!dentry) { +pr_err ("ktapvm: cannot create trace_pipe file in debugfs\n" ); +ring_buffer_free (buffer); +return -1 ; + } + +G (ks)->buffer = buffer; +G (ks)->trace_pipe_dentry = dentry; + +return 0 ; +} +
tools/ktap/runtime/kp_transport.h new file +13 -0
@@ -0,0 +1,13 @@ +#ifndef __KTAP_TRANSPORT_H__ +#define __KTAP_TRANSPORT_H__ + +void kp_transport_write (ktap_state_t *ks,const void *data,size_t length); +void kp_transport_event_write (ktap_state_t *ks,struct ktap_event_data *e); +void kp_transport_print_kstack (ktap_state_t *ks,uint16_t depth,uint16_t skip); +void *kp_transport_reserve (ktap_state_t *ks,size_t length); +void kp_transport_exit (ktap_state_t *ks); +int kp_transport_init (ktap_state_t *ks,struct dentry *dir); + +int _trace_seq_puts (struct trace_seq *s,const char *str); + +#endif /* __KTAP_TRANSPORT_H__ */
Signature · 1 line
1.8.1.4
PATCH 14/28 ktap: add runtime/kp_events.[c|h]
In reply to Jovi Zhangweikp_events.c handle ktap events management(registry, destroy, event callback)
This file is core event management interface between ktap and kernel.
Exposed functions:
1). kp_events_init/kp_events_exit
2). kp_event_create_kprobe
create kprobe event, for example:
kdebug.kprobe("SyS_futex", function () {})
3). kp_event_create_tracepoint
create tracepoint event, for example"
kdebug.tracepoint("sys_futex_enter", function () {})
4). kp_event_create
create perf backend event, for example:
trace sched:sched_switch { print(argstr) }
It call kernel function 'perf_event_create_kernel_counter' to register event(tracepoint/kprobe/uprobe)
5). kp_event_getarg
get argument of event, from arg0 to arg9, only can be called in probe context.
trace sched:sched_switch { print(arg0, arg1) }
6). kp_event_stringify/kp_event_tostr
stringify argstr, sometimes if store argstr as key to table, then it need to stringify firstly, like below:
var s={} trace sched:sched_switch { s[argstr] += 1 }
(This is quite rare usage, but ktap support it)
Note:
Why ktap support 'kdebug.kprobe' and 'kdebug.tracepoint' when it already support perf backend event(trace xxx {})?
Because benchmark shows raw kprobe and tracpoint interface is faster than perf backed tracing, nearly 10+%, it's more fair to compare with Systemtap by raw tracing syntax, not perf backend tracing.
perf backend tracing have a long code path before reach ktap callback, and it need to copy event buffer firstly.
--- tools/ktap/runtime/kp_events.c | 832 +++++++++++++++++++++++++++++++++++++++++ tools/ktap/runtime/kp_events.h | 71 ++++ 2 files changed, 903 insertions(+) create mode 100644 tools/ktap/runtime/kp_events.c create mode 100644 tools/ktap/runtime/kp_events.h
tools/ktap/runtime/kp_events.c new file +832 -0
@@ -0,0 +1,832 @@ +/* +* kp_events.c - ktap events management (registry, destroy, event callback) +* +* This file is part of ktap by Jovi Zhangwei. +* +* Copyright (C) 2012-2014 Jovi Zhangwei <[email protected]>. +* +* ktap is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* ktap is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include <linux/module.h> +#include <linux/ctype.h> +#include <linux/slab.h> +#include <linux/version.h> +#include <asm/syscall.h> +#include "../include/ktap_types.h" +#include "ktap.h" +#include "kp_obj.h" +#include "kp_str.h" +#include "kp_transport.h" +#include "kp_vm.h" +#include "kp_events.h" + +const char *kp_event_tostr (ktap_state_t *ks) +{ +struct ktap_event_data *e = ks->current_event; +struct ftrace_event_call *call; +struct trace_iterator *iter; +struct trace_event *ev; +enum print_line_t ret =TRACE_TYPE_NO_CONSUME ; +static const char *dummy_msg ="argstr_not_available" ; + +/* need to check current context is vaild tracing context */ +if (!ks->current_event) { +kp_error (ks,"cannot stringify event str in invalid context\n" ); +return NULL ; + } + +/*check if stringified before */ +if (ks->current_event->argstr) +return getstr (ks->current_event->argstr); + +/* timer event and raw tracepoint don't have associated argstr */ +if (e->event->type ==KTAP_EVENT_TYPE_PERF && e->event->perf->tp_event) + call = e->event->perf->tp_event; +else +return dummy_msg; + +/* Simulate the iterator */ + +/* +* use temp percpu buffer as trace_iterator +* we cannot use same print_buffer because we may called from printf. +*/ + iter =kp_this_cpu_temp_buffer (ks); + +trace_seq_init (&iter->seq); + iter->ent = e->data->raw->data; + + ev = &(call->event); +if (ev) + ret = ev->funcs->trace (iter,0 , ev); + +if (ret !=TRACE_TYPE_NO_CONSUME ) { +struct trace_seq *s = &iter->seq; +int len = s->len >=PAGE_SIZE ?PAGE_SIZE -1 : s->len; + + s->buffer[len] ='\0' ; +return &s->buffer[0 ]; + } + +return dummy_msg; +} + +/* return string repr of 'argstr' */ +const ktap_str_t *kp_event_stringify (ktap_state_t *ks) +{ +const char *str; +ktap_str_t *ts; + +/*check if stringified before */ +if (ks->current_event->argstr) +return ks->current_event->argstr; + + str =kp_event_tostr (ks); +if (!str) +return NULL ; + + ts =kp_str_newz (ks, str); + ks->current_event->argstr = ts; +return ts; +} + +/* +* This definition should keep update with kernel/trace/trace.h +* TODO: export this struct in kernel +*/ +struct ftrace_event_field { +struct list_head link; +const char *name; +const char *type; +int filter_type; +int offset; +int size; +int is_signed; +}; + +static struct list_head *get_fields (struct ftrace_event_call *event_call) +{ +if (!event_call->class->get_fields) +return &event_call->class->fields; +return event_call->class->get_fields (event_call); +} + +void kp_event_getarg (ktap_state_t *ks,ktap_val_t *ra,int idx) +{ +struct ktap_event_data *e = ks->current_event; +struct ktap_event *event = e->event; +struct ktap_event_field *event_fields = &event->fields[idx]; + +switch (event_fields->type) { +case KTAP_EVENT_FIELD_TYPE_INT : { +struct trace_entry *entry = e->data->raw->data; +void *value = (unsigned char *)entry + event_fields->offset; +int n = *(int *)value; +set_number (ra, n); +return ; + } +case KTAP_EVENT_FIELD_TYPE_LONG : { +struct trace_entry *entry = e->data->raw->data; +void *value = (unsigned char *)entry + event_fields->offset; +long n = *(long *)value; +set_number (ra, n); +return ; + } +case KTAP_EVENT_FIELD_TYPE_STRING : { +struct trace_entry *entry = e->data->raw->data; +ktap_str_t *ts; +void *value = (unsigned char *)entry + event_fields->offset; + ts =kp_str_newz (ks, (char *)value); +if (ts) +set_string (ra, ts); +else +set_nil (ra); +return ; + } +case KTAP_EVENT_FIELD_TYPE_CONST : { +set_number (ra, (ktap_number )event_fields->offset); +return ; + } +case KTAP_EVENT_FIELD_TYPE_REGESTER : { +unsigned long *reg = (unsigned long *)((u8 *)e->regs + + event_fields->offset); +set_number (ra, *reg); +return ; + } +case KTAP_EVENT_FIELD_TYPE_NIL : +set_nil (ra); +return ; +case KTAP_EVENT_FIELD_TYPE_INVALID : +kp_error (ks,"the field type is not supported yet\n" ); +set_nil (ra); +return ; + } +} + +/* init all fields of event, for quick arg1..arg9 access */ +static int init_event_fields (ktap_state_t *ks,struct ktap_event *event) +{ +struct ftrace_event_call *event_call = event->perf->tp_event; +struct ktap_event_field *event_fields = &event->fields[0 ]; +struct ftrace_event_field *field; +struct list_head *head; +int idx =0 , n =0 ; + +/* only init fields for tracepoint, not timer event */ +if (!event_call) +return 0 ; + +/* intern probename */ + event->name =kp_str_newz (ks, event_call->name); +if (unlikely (!event->name)) +return -ENOMEM ; + + head =get_fields (event_call); +list_for_each_entry_reverse (field, head, link) { +if (n++ ==9 ) { +/* +* For some events have fields more than 9, just ignore +* those rest fields at present. +* +* TODO: support access all fields in tracepoint event +* +* Examples: mce:mce_record, ext4:ext4_writepages, ... +*/ +return 0 ; + } + + event_fields[idx].offset = field->offset; + +if (field->size ==4 ) { + event_fields[idx].type =KTAP_EVENT_FIELD_TYPE_INT ; + idx++; +continue ; + }else if (field->size ==8 ) { + event_fields[idx].type =KTAP_EVENT_FIELD_TYPE_LONG ; + idx++; +continue ; + } +if (!strncmp (field->type,"char" ,4 )) { + event_fields[idx].type =KTAP_EVENT_FIELD_TYPE_STRING ; + idx++; +continue ; + } + +/* TODO: add more type check */ + event_fields[idx++].type =KTAP_EVENT_FIELD_TYPE_INVALID ; + } + +/* init all rest fields as NIL */ +while (idx <9 ) + event_fields[idx++].type =KTAP_EVENT_FIELD_TYPE_NIL ; + +return 0 ; +} + +static inline void call_probe_closure (ktap_state_t *mainthread, +ktap_func_t *fn, +struct ktap_event_data *e,int rctx) +{ +ktap_state_t *ks; +ktap_val_t *func; + + ks =kp_vm_new_thread (mainthread, rctx); +set_func (ks->top, fn); + func = ks->top; +incr_top (ks); + + ks->current_event = e; + +kp_vm_call (ks, func,0 ); + + ks->current_event =NULL ; +kp_vm_exit_thread (ks); +} + +/* +* Callback tracing function for perf event subsystem. +* +* make ktap reentrant, don't disable irq in callback function, +* same as perf and ftrace. to make reentrant, we need some +* percpu data to be context isolation(irq/sirq/nmi/process) +* +* The recursion checking in here is mainly purpose for avoiding +* corrupt ktap_state_t with timer closure callback. For tracepoint +* recusion, perf core already handle it. +* +* Note tracepoint handler is calling with rcu_read_lock. +*/ +static void perf_callback (struct perf_event *perf_event, +struct perf_sample_data *data, +struct pt_regs *regs) +{ +struct ktap_event *event; +struct ktap_event_data e; +ktap_state_t *ks; +int rctx; + + event = perf_event->overflow_handler_context; + ks = event->ks; + +if (unlikely (ks->stop)) +return ; + + rctx =get_recursion_context (ks); +if (unlikely (rctx <0 )) +return ; + + e.event = event; + e.data = data; + e.regs = regs; + e.argstr =NULL ; + +call_probe_closure (ks, event->fn, &e, rctx); + +put_recursion_context (ks, rctx); +} + +/* +* Generic ktap event creation function (based on perf callback) +* purpose for tracepoints/kprobe/uprobe/profile-timer/hw_breakpoint/pmu. +*/ +int kp_event_create (ktap_state_t *ks,struct perf_event_attr *attr, +struct task_struct *task,const char *filter, +ktap_func_t *fn) +{ +struct ktap_event *event; +struct perf_event *perf_event; +void *callback = perf_callback; +int cpu, ret; + +if (G (ks)->parm->dry_run) + callback =NULL ; + +/* +* don't tracing until ktap_wait, the reason is: +* 1). some event may hit before apply filter +* 2). more simple to manage tracing thread +* 3). avoid race with mainthread. +* +* Another way to do this is make attr.disabled as 1, then use +* perf_event_enable after filter apply, however, perf_event_enable +* was not exported in kernel older than 3.3, so we drop this method. +*/ + ks->stop =1 ; + +for_each_cpu (cpu,G (ks)->cpumask) { + event =kzalloc (sizeof (struct ktap_event ),GFP_KERNEL ); +if (!event) +return -ENOMEM ; + + event->type =KTAP_EVENT_TYPE_PERF ; + event->ks = ks; + event->fn = fn; + perf_event =perf_event_create_kernel_counter (attr, cpu, task, + callback, event); +if (IS_ERR (perf_event)) { +int err =PTR_ERR (perf_event); +kp_error (ks,"unable register perf event: " +"[cpu: %d; id: %d; err: %d]\n" , + cpu, attr->config, err); +kfree (event); +return err; + } + +if (attr->type ==PERF_TYPE_TRACEPOINT ) { +const char *name = perf_event->tp_event->name; +kp_verbose_printf (ks,"enable perf event: " +"[cpu: %d; id: %d; name: %s; " +"filter: %s; pid: %d]\n" , + cpu, attr->config, name, filter, + task ?task_tgid_vnr (task) :-1 ); + }else if (attr->type ==PERF_TYPE_SOFTWARE && + attr->config ==PERF_COUNT_SW_CPU_CLOCK ) { +kp_verbose_printf (ks,"enable profile event: " +"[cpu: %d; sample_period: %d]\n" , + cpu, attr->sample_period); + }else { +kp_verbose_printf (ks,"unknown perf event type\n" ); + } + + event->perf = perf_event; +INIT_LIST_HEAD (&event->list); +list_add_tail (&event->list, &G (ks)->events_head); + +if (init_event_fields (ks, event)) { +kp_error (ks,"unable init event fields id %d\n" , + attr->config); +perf_event_release_kernel (event->perf); +list_del (&event->list); +kfree (event); +return ret; + } + +if (!filter) +continue ; + + ret =kp_ftrace_profile_set_filter (perf_event, attr->config, + filter); +if (ret) { +kp_error (ks,"unable set event filter: " +"[id: %d; filter: %s; ret: %d]\n" , + attr->config, filter, ret); +perf_event_release_kernel (event->perf); +list_del (&event->list); +kfree (event); +return ret; + } + } + +return 0 ; +} + +/* +* Ignore function proto in here, just use first argument. +*/ +static void probe_callback (void *__data) +{ +struct ktap_event *event = __data; +ktap_state_t *ks = event->ks; +struct ktap_event_data e; +struct pt_regs regs;/* pt_regs maybe is large for stack */ +int rctx; + +if (unlikely (ks->stop)) +return ; + + rctx =get_recursion_context (ks); +if (unlikely (rctx <0 )) +return ; + +perf_fetch_caller_regs (®s); + + e.event = event; + e.regs = ®s; + e.argstr =NULL ; + +call_probe_closure (ks, event->fn, &e, rctx); + +put_recursion_context (ks, rctx); +} + +/* +* syscall events handling +*/ + +static DEFINE_MUTEX (syscall_trace_lock); +static DECLARE_BITMAP(enabled_enter_syscalls, NR_syscalls); +static DECLARE_BITMAP(enabled_exit_syscalls, NR_syscalls); +static int sys_refcount_enter; +static int sys_refcount_exit; + +static int get_syscall_num (const char *name) +{ +int i; + +for (i =0 ; i < NR_syscalls; i++) { +if (syscalls_metadata[i] && + !strcmp (name, syscalls_metadata[i]->name +4 )) +return i; + } +return -1 ; +} + +static void trace_syscall_enter (void *data,struct pt_regs *regs,long id) +{ +struct ktap_event *event = data; +ktap_state_t *ks = event->ks; +struct ktap_event_data e; +int syscall_nr; +int rctx; + +if (unlikely (ks->stop)) +return ; + + syscall_nr =syscall_get_nr (current, regs); +if (unlikely (syscall_nr <0 )) +return ; +if (!test_bit (syscall_nr, enabled_enter_syscalls)) +return ; + + rctx =get_recursion_context (ks); +if (unlikely (rctx <0 )) +return ; + + e.event = event; + e.regs = regs; + e.argstr =NULL ; + +call_probe_closure (ks, event->fn, &e, rctx); + +put_recursion_context (ks, rctx); +} + +static void trace_syscall_exit (void *data,struct pt_regs *regs,long id) +{ +struct ktap_event *event = data; +ktap_state_t *ks = event->ks; +struct ktap_event_data e; +int syscall_nr; +int rctx; + + syscall_nr =syscall_get_nr (current, regs); +if (unlikely (syscall_nr <0 )) +return ; +if (!test_bit (syscall_nr, enabled_exit_syscalls)) +return ; + +if (unlikely (ks->stop)) +return ; + + rctx =get_recursion_context (ks); +if (unlikely (rctx <0 )) +return ; + + e.event = event; + e.regs = regs; + e.argstr =NULL ; + +call_probe_closure (ks, event->fn, &e, rctx); + +put_recursion_context (ks, rctx); +} + +/* called in dry-run mode, purpose for compare overhead with normal vm call */ +static void dry_run_callback (void *data,struct pt_regs *regs,long id) +{ + +} + +static void init_syscall_event_fields (struct ktap_event *event,int is_enter) +{ +struct ftrace_event_call *event_call; +struct ktap_event_field *event_fields = &event->fields[0 ]; +struct syscall_metadata *meta = syscalls_metadata[event->syscall_nr]; +int idx =0 ; + + event_call = is_enter ? meta->enter_event : meta->exit_event; + + event_fields[0 ].type =KTAP_EVENT_FIELD_TYPE_CONST ; + event_fields[0 ].offset = event->syscall_nr; + +if (!is_enter) { +#ifdef CONFIG_X86_64 + event_fields[1 ].type =KTAP_EVENT_FIELD_TYPE_REGESTER ; + event_fields[1 ].offset = offsetof(struct pt_regs , ax); +#endif +return ; + } + +while (idx++ < meta->nb_args) { + event_fields[idx].type =KTAP_EVENT_FIELD_TYPE_REGESTER ; +#ifdef CONFIG_X86_64 +switch (idx) { +case 1 : + event_fields[idx].offset = offsetof(struct pt_regs , di); +break ; +case 2 : + event_fields[idx].offset = offsetof(struct pt_regs , si); +break ; +case 3 : + event_fields[idx].offset = offsetof(struct pt_regs , dx); +break ; +case 4 : + event_fields[idx].offset = + offsetof(struct pt_regs , r10); +break ; +case 5 : + event_fields[idx].offset = offsetof(struct pt_regs , r8); +break ; +case 6 : + event_fields[idx].offset = offsetof(struct pt_regs , r9); +break ; + } +#else +#error "don't support syscall tracepoint event register access in this arch, " + "use 'trace syscalls:* {}' instead" +#endif + } + +/* init all rest fields as NIL */ +while (idx <9 ) + event_fields[idx++].type =KTAP_EVENT_FIELD_TYPE_NIL ; +} + +static int syscall_event_register (ktap_state_t *ks,const char *event_name, +struct ktap_event *event) +{ +int syscall_nr =0 , is_enter =0 ; +void *callback =NULL ; +int ret =0 ; + +if (!strncmp (event_name,"sys_enter_" ,10 )) { + is_enter =1 ; + event->type =KTAP_EVENT_TYPE_SYSCALL_ENTER ; + syscall_nr =get_syscall_num (event_name +10 ); + callback = trace_syscall_enter; + }else if (!strncmp (event_name,"sys_exit_" ,9 )) { + is_enter =0 ; + event->type =KTAP_EVENT_TYPE_SYSCALL_EXIT ; + syscall_nr =get_syscall_num (event_name +9 ); + callback = trace_syscall_exit; + } + +if (G (ks)->parm->dry_run) + callback = dry_run_callback; + +if (syscall_nr <0 ) +return -1 ; + + event->syscall_nr = syscall_nr; + +init_syscall_event_fields (event, is_enter); + +mutex_lock (&syscall_trace_lock); +if (is_enter) { +if (!sys_refcount_enter) + ret =register_trace_sys_enter (callback, event); +if (!ret) { +set_bit (syscall_nr, enabled_enter_syscalls); + sys_refcount_enter++; + } + }else { +if (!sys_refcount_exit) + ret =register_trace_sys_exit (callback, event); +if (!ret) { +set_bit (syscall_nr, enabled_exit_syscalls); + sys_refcount_exit++; + } + } +mutex_unlock (&syscall_trace_lock); + +return ret; +} + +static int syscall_event_unregister (ktap_state_t *ks,struct ktap_event *event) +{ +int ret =0 ; +void *callback; + +if (event->type ==KTAP_EVENT_TYPE_SYSCALL_ENTER ) + callback = trace_syscall_enter; +else + callback = trace_syscall_exit; + +if (G (ks)->parm->dry_run) + callback = dry_run_callback; + +mutex_lock (&syscall_trace_lock); +if (event->type ==KTAP_EVENT_TYPE_SYSCALL_ENTER ) { + sys_refcount_enter--; +clear_bit (event->syscall_nr, enabled_enter_syscalls); +if (!sys_refcount_enter) +unregister_trace_sys_enter (callback, event); + }else { + sys_refcount_exit--; +clear_bit (event->syscall_nr, enabled_exit_syscalls); +if (!sys_refcount_exit) +unregister_trace_sys_exit (callback, event); + } +mutex_unlock (&syscall_trace_lock); + +return ret; +} + +/* +* Register tracepoint event directly, not based on perf callback +* +* This tracing method would be more faster than perf callback, +* because it won't need to write trace data into any temp buffer, +* and code path is much shorter than perf callback. +*/ +int kp_event_create_tracepoint (ktap_state_t *ks,const char *event_name, +ktap_func_t *fn) +{ +struct ktap_event *event; +void *callback = probe_callback; +int is_syscall =0 ; +int ret; + +if (G (ks)->parm->dry_run) + callback =NULL ; + +if (!strncmp (event_name,"sys_enter_" ,10 ) || + !strncmp (event_name,"sys_exit_" ,9 )) + is_syscall =1 ; + + event =kzalloc (sizeof (struct ktap_event ),GFP_KERNEL ); +if (!event) +return -ENOMEM ; + + event->ks = ks; + event->fn = fn; + event->name =kp_str_newz (ks, event_name); +if (unlikely (!event->name)) { +kfree (event); +return -ENOMEM ; + } + +INIT_LIST_HEAD (&event->list); +list_add_tail (&event->list, &G (ks)->events_head); + +if (is_syscall) { + ret =syscall_event_register (ks, event_name, event); + }else { + event->type =KTAP_EVENT_TYPE_TRACEPOINT ; + ret =tracepoint_probe_register (event_name, callback, event); + } + +if (ret) { +kp_error (ks,"register tracepoint %s failed, ret: %d\n" , + event_name, ret); +list_del (&event->list); +kfree (event); +return ret; + } +return 0 ; +} + +/* kprobe handler */ +static int __kprobes pre_handler_kprobe (struct kprobe *p,struct pt_regs *regs) +{ + struct ktap_event *event = container_of(p, struct ktap_event, kp); +ktap_state_t *ks = event->ks; +struct ktap_event_data e; +int rctx; + +if (unlikely (ks->stop)) +return 0 ; + + rctx =get_recursion_context (ks); +if (unlikely (rctx <0 )) +return 0 ; + + e.event = event; + e.regs = regs; + e.argstr =NULL ; + +call_probe_closure (ks, event->fn, &e, rctx); + +put_recursion_context (ks, rctx); +return 0 ; +} + +/* +* Register kprobe event directly, not based on perf callback +* +* This tracing method would be more faster than perf callback, +* because it won't need to write trace data into any temp buffer, +* and code path is much shorter than perf callback. +*/ +int kp_event_create_kprobe (ktap_state_t *ks,const char *event_name, +ktap_func_t *fn) +{ +struct ktap_event *event; +void *callback = pre_handler_kprobe; +int ret; + +if (G (ks)->parm->dry_run) + callback =NULL ; + + event =kzalloc (sizeof (struct ktap_event ),GFP_KERNEL ); +if (!event) +return -ENOMEM ; + + event->ks = ks; + event->fn = fn; + event->name =kp_str_newz (ks, event_name); +if (unlikely (!event->name)) { +kfree (event); +return -ENOMEM ; + } + +INIT_LIST_HEAD (&event->list); +list_add_tail (&event->list, &G (ks)->events_head); + + event->type =KTAP_EVENT_TYPE_KPROBE ; + + event->kp.symbol_name = event_name; + event->kp.pre_handler = callback; + ret =register_kprobe (&event->kp); +if (ret) { +kp_error (ks,"register kprobe event %s failed, ret: %d\n" , + event_name, ret); +list_del (&event->list); +kfree (event); +return ret; + } +return 0 ; +} + + +static void events_destroy (ktap_state_t *ks) +{ +struct ktap_event *event; +struct list_head *tmp, *pos; +struct list_head *head = &G (ks)->events_head; + +list_for_each (pos, head) { + event = container_of(pos, struct ktap_event, + list); +if (event->type ==KTAP_EVENT_TYPE_PERF ) +perf_event_release_kernel (event->perf); +else if (event->type ==KTAP_EVENT_TYPE_TRACEPOINT ) +tracepoint_probe_unregister (getstr (event->name), + probe_callback, event); +else if (event->type ==KTAP_EVENT_TYPE_SYSCALL_ENTER || + event->type ==KTAP_EVENT_TYPE_SYSCALL_EXIT ) +syscall_event_unregister (ks, event); +else if (event->type ==KTAP_EVENT_TYPE_KPROBE ) +unregister_kprobe (&event->kp); + } +/* +* Ensure our callback won't be called anymore. The buffers +* will be freed after that. +*/ +tracepoint_synchronize_unregister (); + +list_for_each_safe (pos, tmp, head) { + event = container_of(pos, struct ktap_event, + list); +list_del (&event->list); +kfree (event); + } +} + +void kp_events_exit (ktap_state_t *ks) +{ +if (!G (ks)->trace_enabled) +return ; + +events_destroy (ks); + +/* call trace_end_closure after all event unregistered */ +if ((G (ks)->state !=KTAP_ERROR ) &&G (ks)->trace_end_closure) { +G (ks)->state =KTAP_TRACE_END ; +set_func (ks->top,G (ks)->trace_end_closure); +incr_top (ks); +kp_vm_call (ks, ks->top -1 ,0 ); +G (ks)->trace_end_closure =NULL ; + } + +G (ks)->trace_enabled =0 ; +} + +int kp_events_init (ktap_state_t *ks) +{ +G (ks)->trace_enabled =1 ; +return 0 ; +} +
tools/ktap/runtime/kp_events.h new file +71 -0
71 lines not on this page · raw message
Signature · 1 line
1.8.1.4
Re: [PATCH 14/28] ktap: add runtime/kp_events.[c|h]
Quoted 33 lines · Jovi Zhangwei
kp_events.chandle ktap eventsmanagement(registry, destroy, event callback)This file is core event management interface between ktap and kernel.
Exposed functions:
1). kp_events_init/kp_events_exit2).
kp_event_create_kprobe
create kprobe event, for example:
kdebug.kprobe("SyS_futex", function () {})3).
kp_event_create_tracepoint
create tracepoint event, for example"
kdebug.tracepoint("sys_futex_enter", function () {})4).
kp_event_create
create perf backend event, for example:
trace sched:sched_switch { print(argstr) }It call kernel function '
perf_event_create_kernel_counter' to register event(tracepoint/kprobe/uprobe)5).
kp_event_getarg
get argument of event, from arg0 to arg9, only can be called in probe context.
trace sched:sched_switch {print(arg0, arg1)}6). kp_event_stringify/kp_event_tostr
stringify argstr, sometimes if store argstr as key to table, then it need to stringify firstly, like below:
var s={} trace sched:sched_switch { s[argstr] += 1 }
(This is quite rare usage, but ktap support it)
Quoted 8 lines
Note:
Why ktap support 'kdebug.kprobe' and 'kdebug.tracepoint' when it already support perf backend event(trace xxx {})?Because benchmark shows raw kprobe and tracpoint interface is faster than perf backed tracing, nearly 10+%, it's more fair to compare with Systemtap by raw tracing syntax, not perf backend tracing.
Do we really need it just for a +10% performance? I doubt that. I think the benefit point of ktap is "dynamic & simple programmable tracer in kernel", not the good performance at least at this point. Thus I think we should start ktap only with perf backend.
Thank you,
Signature · 4 lines
Masami HIRAMATSU IT Management Research Dept. Linux Technology Center Hitachi, Ltd., Yokohama Research Laboratory E-mail: [email protected]
Re: [PATCH 14/28] ktap: add runtime/kp_events.[c|h]
Quoted 43 lines · Masami Hiramatsu
Quoted 33 lines · Jovi Zhangwei
kp_events.chandle ktap eventsmanagement(registry, destroy, event callback)This file is core event management interface between ktap and kernel.
Exposed functions:
1). kp_events_init/kp_events_exit2).
kp_event_create_kprobe
create kprobe event, for example:
kdebug.kprobe("SyS_futex", function () {})3).
kp_event_create_tracepoint
create tracepoint event, for example"
kdebug.tracepoint("sys_futex_enter", function () {})4).
kp_event_create
create perf backend event, for example:
trace sched:sched_switch { print(argstr) }It call kernel function '
perf_event_create_kernel_counter' to register event(tracepoint/kprobe/uprobe)5).
kp_event_getarg
get argument of event, from arg0 to arg9, only can be called in probe context.
trace sched:sched_switch {print(arg0, arg1)}6). kp_event_stringify/kp_event_tostr
stringify argstr, sometimes if store argstr as key to table, then it need to stringify firstly, like below:
var s={} trace sched:sched_switch { s[argstr] += 1 }
(This is quite rare usage, but ktap support it)Quoted 8 lines
Note:
Why ktap support 'kdebug.kprobe' and 'kdebug.tracepoint' when it already support perf backend event(trace xxx {})?Because benchmark shows raw kprobe and tracpoint interface is faster than perf backed tracing, nearly 10+%, it's more fair to compare with Systemtap by raw tracing syntax, not perf backend tracing.
Quoted 5 lines
Do we really need it just for a +10% performance? I doubt that. I think the benefit point of ktap is "dynamic & simple programmable tracer in kernel", not the good performance at least at this point. Thus I think we should start ktap only with perf backend.
Yeah, agreed, most people like the perf-backed tracing syntax, that raw trace interface is just for benchmark when I wanted to look overhead compare with stap, the result is very inspiring, ktap table operation overhead is lower than stap.
On the performance overhead of dynamic tracing tools(ktap/stap/dtrace), it's interesting enough that dtrace was used in production many year, _but_ IMO the runtime of dtrace is slow after I checked dtrace source code :), system workload does big matter than tracing tool overhead.
Thanks.
Jovi
Re: Re: [PATCH 14/28] ktap: add runtime/kp_events.[c|h]
Quoted 55 lines · Jovi Zhangwei
Quoted 43 lines · Masami Hiramatsu
Quoted 33 lines · Jovi Zhangwei
kp_events.chandle ktap eventsmanagement(registry, destroy, event callback)This file is core event management interface between ktap and kernel.
Exposed functions:
1). kp_events_init/kp_events_exit2).
kp_event_create_kprobe
create kprobe event, for example:
kdebug.kprobe("SyS_futex", function () {})3).
kp_event_create_tracepoint
create tracepoint event, for example"
kdebug.tracepoint("sys_futex_enter", function () {})4).
kp_event_create
create perf backend event, for example:
trace sched:sched_switch { print(argstr) }It call kernel function '
perf_event_create_kernel_counter' to register event(tracepoint/kprobe/uprobe)5).
kp_event_getarg
get argument of event, from arg0 to arg9, only can be called in probe context.
trace sched:sched_switch {print(arg0, arg1)}6). kp_event_stringify/kp_event_tostr
stringify argstr, sometimes if store argstr as key to table, then it need to stringify firstly, like below:
var s={} trace sched:sched_switch { s[argstr] += 1 }
(This is quite rare usage, but ktap support it)Quoted 8 lines
Note:
Why ktap support 'kdebug.kprobe' and 'kdebug.tracepoint' when it already support perf backend event(trace xxx {})?Because benchmark shows raw kprobe and tracpoint interface is faster than perf backed tracing, nearly 10+%, it's more fair to compare with Systemtap by raw tracing syntax, not perf backend tracing.
Quoted 5 lines
Do we really need it just for a +10% performance? I doubt that. I think the benefit point of ktap is "dynamic & simple programmable tracer in kernel", not the good performance at least at this point. Thus I think we should start ktap only with perf backend.
Yeah, agreed, most people like the perf-backed tracing syntax, that raw trace interface is just for benchmark when I wanted to look overhead compare with stap, the result is very inspiring, ktap table operation overhead is lower than stap.
Quoted 4 lines
On the performance overhead of dynamic tracing tools(ktap/stap/dtrace), it's interesting enough that dtrace was used in production many year, _but_ IMO the runtime of dtrace is slow after I checked dtrace source code :), system workload does big matter than tracing tool overhead.
Yeah, I see that less overhead is also required especially for enterprise people. I just doubt that it is solved by ktap itself. Should we improve perf(or ftrace) to export more effective interfaces for this kind of tracers?
Thank you,
Signature · 4 lines
Masami HIRAMATSU IT Management Research Dept. Linux Technology Center Hitachi, Ltd., Yokohama Research Laboratory E-mail: [email protected]
Re: Re: [PATCH 14/28] ktap: add runtime/kp_events.[c|h]
Quoted 61 lines · Masami Hiramatsu
Quoted 55 lines · Jovi Zhangwei
Quoted 43 lines · Masami Hiramatsu
Quoted 33 lines · Jovi Zhangwei
kp_events.chandle ktap eventsmanagement(registry, destroy, event callback)This file is core event management interface between ktap and kernel.
Exposed functions:
1). kp_events_init/kp_events_exit2).
kp_event_create_kprobe
create kprobe event, for example:
kdebug.kprobe("SyS_futex", function () {})3).
kp_event_create_tracepoint
create tracepoint event, for example"
kdebug.tracepoint("sys_futex_enter", function () {})4).
kp_event_create
create perf backend event, for example:
trace sched:sched_switch { print(argstr) }It call kernel function '
perf_event_create_kernel_counter' to register event(tracepoint/kprobe/uprobe)5).
kp_event_getarg
get argument of event, from arg0 to arg9, only can be called in probe context.
trace sched:sched_switch {print(arg0, arg1)}6). kp_event_stringify/kp_event_tostr
stringify argstr, sometimes if store argstr as key to table, then it need to stringify firstly, like below:
var s={} trace sched:sched_switch { s[argstr] += 1 }
(This is quite rare usage, but ktap support it)Quoted 8 lines
Note:
Why ktap support 'kdebug.kprobe' and 'kdebug.tracepoint' when it already support perf backend event(trace xxx {})?Because benchmark shows raw kprobe and tracpoint interface is faster than perf backed tracing, nearly 10+%, it's more fair to compare with Systemtap by raw tracing syntax, not perf backend tracing.
Quoted 5 lines
Do we really need it just for a +10% performance? I doubt that. I think the benefit point of ktap is "dynamic & simple programmable tracer in kernel", not the good performance at least at this point. Thus I think we should start ktap only with perf backend.
Yeah, agreed, most people like the perf-backed tracing syntax, that raw trace interface is just for benchmark when I wanted to look overhead compare with stap, the result is very inspiring, ktap table operation overhead is lower than stap.
Quoted 4 lines
On the performance overhead of dynamic tracing tools(ktap/stap/dtrace), it's interesting enough that dtrace was used in production many year, _but_ IMO the runtime of dtrace is slow after I checked dtrace source code :), system workload does big matter than tracing tool overhead.
Quoted 5 lines
Yeah, I see that less overhead is also required especially for enterprise people. I just doubt that it is solved by ktap itself. Should we improve perf(or ftrace) to export more effective interfaces for this kind of tracers?
Yes, I also think it would be better to improve perf/ftrace unified callback overhead, not to let each tracer(stap/ktap/lttng) develop its own raw trace callback for performance reason.
Those raw trace interfaces(only designed for benchmark) will be remove in next version, if we think it's worth to continue.
Thanks.
Jovi
Re: [PATCH 14/28] ktap: add runtime/kp_events.[c|h]
Quoted 29 lines · Jovi Zhangwei
Quoted 24 lines
Quoted 19 lines
Quoted 9 lines
Quoted 8 lines
Note:
Why ktap support 'kdebug.kprobe' and 'kdebug.tracepoint' when it already support perf backend event(trace xxx {})?Because benchmark shows raw kprobe and tracpoint interface is faster than perf backed tracing, nearly 10+%, it's more fair to compare with Systemtap by raw tracing syntax, not perf backend tracing.
Quoted 5 lines
Do we really need it just for a +10% performance? I doubt that. I think the benefit point of ktap is "dynamic & simple programmable tracer in kernel", not the good performance at least at this point. Thus I think we should start ktap only with perf backend.
Yeah, agreed, most people like the perf-backed tracing syntax, that raw trace interface is just for benchmark when I wanted to look overhead compare with stap, the result is very inspiring, ktap table operation overhead is lower than stap.
Quoted 4 lines
On the performance overhead of dynamic tracing tools(ktap/stap/dtrace), it's interesting enough that dtrace was used in production many year, _but_ IMO the runtime of dtrace is slow after I checked dtrace source code :), system workload does big matter than tracing tool overhead.
Quoted 5 lines
Yeah, I see that less overhead is also required especially for enterprise people. I just doubt that it is solved by ktap itself. Should we improve perf(or ftrace) to export more effective interfaces for this kind of tracers?
Quoted 6 lines
Yes, I also think it would be better to improve perf/ftrace unified callback overhead, not to let each tracer(stap/ktap/lttng) develop its own raw trace callback for performance reason.
Those raw trace interfaces(only designed for benchmark) will be remove in next version, if we think it's worth to continue.
Of course, I think ktap scripting flexibility should be merged to upstream :) I just surprised that the size of this series. If you reform this series for incremental build (this means that we can do git-bisect on this series), I think that will be much easier to test and review.
Thank you,
Signature · 4 lines
Masami HIRAMATSU IT Management Research Dept. Linux Technology Center Hitachi, Ltd., Yokohama Research Laboratory E-mail: [email protected]
PATCH 15/28 ktap: add built-in functions and library (runtime/lib_*.c)
In reply to Jovi Zhangweiktap register built-in functions and library into table.
1). Built-in functions(lib_base,c):
print, printf, print_hist, pairs, len, delete, stack, print_trace_clock, num_cpus, arch, kernel_v, kernel_string, user_string, stringof, ipof, gettimeofday_ns, gettimeofday_us, gettimeofday_ms, gettimeofday_s, curr_taskinfo, in_iowait, in_interrupt, exit.
2). Ansi library(lib_ansi.c):
ansi.clear_screen
ansi.set_color
ansi.set_color2
ansi.set_color3
ansi.reset_color
ansi.new_line
3). kdebug library(lib_kdebug.c):
kdebug.trace_by_id
kdebug.trace_end
kdebug.tracepoint
kdebug.kprobe
4). net library(lib_net.c):
net.ip_sock_saddr
net.ip_sock_daddr
net.format_ip_addr
5). table library(lib_table.c):
table.new
6). timer library(lib_timer.c):
timer.profile
timer.tick
--- tools/ktap/runtime/lib_ansi.c | 142 ++++++++++++++ tools/ktap/runtime/lib_base.c | 409 ++++++++++++++++++++++++++++++++++++++++ tools/ktap/runtime/lib_kdebug.c | 198 +++++++++++++++++++ tools/ktap/runtime/lib_net.c | 107 +++++++++++ tools/ktap/runtime/lib_table.c | 58 ++++++ tools/ktap/runtime/lib_timer.c | 210 +++++++++++++++++++++ 6 files changed, 1124 insertions(+) create mode 100644 tools/ktap/runtime/lib_ansi.c create mode 100644 tools/ktap/runtime/lib_base.c create mode 100644 tools/ktap/runtime/lib_kdebug.c create mode 100644 tools/ktap/runtime/lib_net.c create mode 100644 tools/ktap/runtime/lib_table.c create mode 100644 tools/ktap/runtime/lib_timer.c
tools/ktap/runtime/lib_ansi.c new file +142 -0
@@ -0,0 +1,142 @@ +/* +* lib_ansi.c - ANSI escape sequences library +* +* http://en.wikipedia.org/wiki/ANSI_escape_code +* +* This file is part of ktap by Jovi Zhangwei. +* +* Copyright (C) 2012-2014 Jovi Zhangwei <[email protected]>. +* +* ktap is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* ktap is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "../include/ktap_types.h" +#include "ktap.h" +#include "kp_vm.h" + +/** +* function ansi.clear_screen - Move cursor to top left and clear screen. +* +* Description: Sends ansi code for moving cursor to top left and then the +* ansi code for clearing the screen from the cursor position to the end. +*/ + +static int kplib_ansi_clear_screen (ktap_state_t *ks) +{ +kp_printf (ks,"\033[1;1H\033[J" ); +return 0 ; +} + +/** +* function ansi.set_color - Set the ansi Select Graphic Rendition mode. +* @fg: Foreground color to set. +* +* Description: Sends ansi code for Select Graphic Rendition mode for the +* given forground color. Black (30), Blue (34), Green (32), Cyan (36), +* Red (31), Purple (35), Brown (33), Light Gray (37). +*/ + +static int kplib_ansi_set_color (ktap_state_t *ks) +{ +int fg =kp_arg_checknumber (ks,1 ); + +kp_printf (ks,"\033[%dm" , fg); +return 0 ; +} + +/** +* function ansi.set_color2 - Set the ansi Select Graphic Rendition mode. +* @fg: Foreground color to set. +* @bg: Background color to set. +* +* Description: Sends ansi code for Select Graphic Rendition mode for the +* given forground color, Black (30), Blue (34), Green (32), Cyan (36), +* Red (31), Purple (35), Brown (33), Light Gray (37) and the given +* background color, Black (40), Red (41), Green (42), Yellow (43), +* Blue (44), Magenta (45), Cyan (46), White (47). +*/ +static int kplib_ansi_set_color2 (ktap_state_t *ks) +{ +int fg =kp_arg_checknumber (ks,1 ); +int bg =kp_arg_checknumber (ks,2 ); + +kp_printf (ks,"\033[%d;%dm" , fg, bg); +return 0 ; +} + +/** +* function ansi.set_color3 - Set the ansi Select Graphic Rendition mode. +* @fg: Foreground color to set. +* @bg: Background color to set. +* @attr: Color attribute to set. +* +* Description: Sends ansi code for Select Graphic Rendition mode for the +* given forground color, Black (30), Blue (34), Green (32), Cyan (36), +* Red (31), Purple (35), Brown (33), Light Gray (37), the given +* background color, Black (40), Red (41), Green (42), Yellow (43), +* Blue (44), Magenta (45), Cyan (46), White (47) and the color attribute +* All attributes off (0), Intensity Bold (1), Underline Single (4), +* Blink Slow (5), Blink Rapid (6), Image Negative (7). +*/ +static int kplib_ansi_set_color3 (ktap_state_t *ks) +{ +int fg =kp_arg_checknumber (ks,1 ); +int bg =kp_arg_checknumber (ks,2 ); +int attr =kp_arg_checknumber (ks,3 ); + +if (attr) +kp_printf (ks,"\033[%d;%d;%dm" , fg, bg, attr); +else +kp_printf (ks,"\033[%d;%dm" , fg, bg); + +return 0 ; +} + +/** +* function ansi.reset_color - Resets Select Graphic Rendition mode. +* +* Description: Sends ansi code to reset foreground, background and color +* attribute to default values. +*/ +static int kplib_ansi_reset_color (ktap_state_t *ks) +{ +kp_printf (ks,"\033[0;0m" ); +return 0 ; +} + +/** +* function ansi.new_line - Move cursor to new line. +* +* Description: Sends ansi code new line. +*/ +static int kplib_ansi_new_line (ktap_state_t *ks) +{ +kp_printf (ks,"\12" ); +return 0 ; +} + +static const ktap_libfunc_t ansi_lib_funcs[] = { + {"clear_screen" , kplib_ansi_clear_screen}, + {"set_color" , kplib_ansi_set_color}, + {"set_color2" , kplib_ansi_set_color2}, + {"set_color3" , kplib_ansi_set_color3}, + {"reset_color" , kplib_ansi_reset_color}, + {"new_line" , kplib_ansi_new_line}, + {NULL } +}; + +int kp_lib_init_ansi (ktap_state_t *ks) +{ +return kp_vm_register_lib (ks,"ansi" , ansi_lib_funcs); +}
tools/ktap/runtime/lib_base.c new file +409 -0
409 lines not on this page · raw message
tools/ktap/runtime/lib_kdebug.c new file +198 -0
198 lines not on this page · raw message
tools/ktap/runtime/lib_net.c new file +107 -0
107 lines not on this page · raw message
tools/ktap/runtime/lib_table.c new file +58 -0
58 lines not on this page · raw message
tools/ktap/runtime/lib_timer.c new file +210 -0
210 lines not on this page · raw message
Signature · 1 line
1.8.1.4
Re: [PATCH 15/28] ktap: add built-in functions and library (runtime/lib_*.c)
Quoted 8 lines · Jovi Zhangwei
2). Ansi
library(lib_ansi.c):
ansi.clear_screen
ansi.set_color
ansi.set_color2
ansi.set_color3
ansi.reset_color
ansi.new_line
This really can't be done in some kind of ktap library?
-Andi
Signature · 1 line
[email protected] -- Speaking for myself only
Re: [PATCH 15/28] ktap: add built-in functions and library (runtime/lib_*.c)
Quoted 10 lines · Andi Kleen
Quoted 8 lines · Jovi Zhangwei
2). Ansi
library(lib_ansi.c):
ansi.clear_screen
ansi.set_color
ansi.set_color2
ansi.set_color3
ansi.reset_color
ansi.new_line
Quoted 2 lines
This really can't be done in some kind of ktap library?
Maybe in future, after ktap support "include" or "require" to import user defined library in userspace.
Thanks.
Jovi
Re: [PATCH 15/28] ktap: add built-in functions and library (runtime/lib_*.c)
Quoted 2 lines
Maybe in future, after ktap support "include" or "require" to import user defined library in userspace.
Can't you just have some hardcoded standard script for now that is always appeneded and provides these functions?
-Andi
Re: [PATCH 15/28] ktap: add built-in functions and library (runtime/lib_*.c)
Quoted 6 lines · Andi Kleen
Quoted 2 lines
Maybe in future, after ktap support "include" or "require" to import user defined library in userspace.
Can't you just have some hardcoded standard script for now that is always appeneded and provides these functions?
Maybe it's fine to hardcoded just for now.
Since we are agreed on review userspace part in another schedule, so I will remove this ansi library from kernel part in next version.
Thanks for this suggestion.
Jovi
Re: [PATCH 15/28] ktap: add built-in functions and library (runtime/lib_*.c)
Quoted 7 lines · Jovi Zhangwei
Quoted 6 lines · Andi Kleen
Quoted 2 lines
Maybe in future, after ktap support "include" or "require" to import user defined library in userspace.
Can't you just have some hardcoded standard script for now that is always appeneded and provides these functions?
Quoted 6 lines
Maybe it's fine to hardcoded just for now.
Since we are agreed on review userspace part in another schedule, so I will remove this ansi library from kernel part in next version.
Thanks for this suggestion.
Please do the following for the next version:
- Don't repost with all the TODOs regarding changing other kernel parts. Just fix them.
- As others pointed out elsewhere it's too big and full featured right now. Please find ways to define a useful "core ktap" for now with less features. This could be dropping library parts or dropping some of the probe types or some parts of the language. These can then be later phased in over time. For the library it may make sense to add some module interface and keep parts of it external for now?
- Please run as much test content as you have with all the kernel debug options to automatically find bugs. I would at least two runs one with lockdep/lock debugging/preempt debugging/slab/page debugging etc. and another with kmemleak (that is exclusive with some other options) Some of these checks may have false positives, but the messages should be all analyzed at least, and false positives commented.
- Do similar with the static compile time checks: checkpatch, sparse, coccinelle. There will be likely a lot more false positives here, so it may not be feasible to check all, but should at least eyeball the output to see if there are some obvious problems. For sparse it would be also good to annotate the user ioctl parts with __user and carefully look at the warnings there, as that is a common problem area.
-Andi
Re: [PATCH 15/28] ktap: add built-in functions and library (runtime/lib_*.c)
Quoted 13 lines · Andi Kleen
Quoted 12 lines · Jovi Zhangwei
Quoted 6 lines · Andi Kleen
Quoted 2 lines
Maybe in future, after ktap support "include" or "require" to import user defined library in userspace.
Can't you just have some hardcoded standard script for now that is always appeneded and provides these functions?
Maybe it's fine to hardcoded just for now.
Since we are agreed on review userspace part in another schedule, so I will remove this ansi library from kernel part in next version.
Quoted 7 lines
Quoted 1 line
Thanks for this suggestion.
Please do the following for the next version:
- Don't repost with all the TODOs regarding changing other kernel parts. Just fix them.
Sure.
Quoted 7 lines
- As others pointed out elsewhere it's too big and full featured right now. Please find ways to define a useful "core ktap" for now with less features. This could be dropping library parts or dropping some of the probe types or some parts of the language. These can then be later phased in over time. For the library it may make sense to add some module interface and keep parts of it external for now?
I will remove some library and raw tracing interface(designed for benchmark)
Quoted 7 lines
- Please run as much test content as you have with all the kernel debug options to automatically find bugs. I would at least two runs one with lockdep/lock debugging/preempt debugging/slab/page debugging etc. and another with kmemleak (that is exclusive with some other options) Some of these checks may have false positives, but the messages should be all analyzed at least, and false positives commented.
ktap runs fine in lockdep/lock debugging/preempt debugging, but not try to run in slab/page debugging and kmemleak. I will do that.
Quoted 8 lines
- Do similar with the static compile time checks: checkpatch, sparse, coccinelle. There will be likely a lot more false positives here, so it may not be feasible to check all, but should at least eyeball the output to see if there are some obvious problems. For sparse it would be also good to annotate the user ioctl parts with
__userand carefully look at the warnings there, as that is a common problem area.
Sure.
I will send out new version once we make agreement on ktap upstream which discussing in another thread.
Thanks for these comments.
Jovi
PATCH 16/28 ktap: add runtime/amalg.c
In reply to Jovi ZhangweiUse amalgamation build make ktapvm.ko much smaller.
No amalgamation build in x86_64:
ktapvm.ko: 2.4M
amalgamation build in x86_64:
ktapvm.ko: 1.1M
User can set use amalgamation build or not in Makefile.
--- tools/ktap/runtime/amalg.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 tools/ktap/runtime/amalg.c
tools/ktap/runtime/amalg.c new file +37 -0
@@ -0,0 +1,37 @@ +/* +* amalg.c - ktapvm kernel module amalgamation. +* +* This file is part of ktap by Jovi Zhangwei. +* +* Copyright (C) 2012-2014 Jovi Zhangwei <[email protected]>. +* +* ktap is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* ktap is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "ktap.c" +#include "kp_obj.c" +#include "kp_bcread.c" +#include "kp_str.c" +#include "kp_mempool.c" +#include "kp_tab.c" +#include "kp_transport.c" +#include "kp_vm.c" +#include "kp_events.c" +#include "lib_base.c" +#include "lib_ansi.c" +#include "lib_kdebug.c" +#include "lib_timer.c" +#include "lib_table.c" +#include "lib_net.c" +
Signature · 1 line
1.8.1.4
Re: [PATCH 16/28] ktap: add runtime/amalg.c
Quoted 9 lines · Jovi Zhangwei
Use amalgamation build make ktapvm.ko much smaller.
No amalgamation build in
x86_64:
ktapvm.ko: 2.4Mamalgamation build in
x86_64:
ktapvm.ko: 1.1MUser can set use amalgamation build or not in
Makefile.
This seems dubious. Is that due to debug information?
[if yes new enough toolchain can fix that]
If it's due to code something is seriously wrong,
perhaps in your inlines.
-Andi
Signature · 1 line
[email protected] -- Speaking for myself only
Re: [PATCH 16/28] ktap: add runtime/amalg.c
Hi,
Quoted 10 lines · Andi Kleen
Quoted 8 lines · Jovi Zhangwei
Use amalgamation build make ktapvm.ko much smaller.
No amalgamation build in
x86_64:
ktapvm.ko: 2.4Mamalgamation build in
x86_64:
ktapvm.ko: 1.1M
Quoted 9 lines
Quoted 1 line
User can set use amalgamation build or not in
Makefile.This seems dubious. Is that due to debug information?
[if yes new enough toolchain can fix that]
If it's due to code something is seriously wrong,
perhaps in your inlines.
Change: The kernel module size without amalgamation build is: 3.1M
I guess it's not because of debug info.
Amalgamation build is a kind of simple 'LTO', it compiles the ktapvm as one huge C file and allows GCC to generate faster and shorter code.
But I also surprised by the size gap, will spend some time to investigate it.
Thanks.
Jovi
PATCH 17/28 ktap: add userspace/kp_main.c
In reply to Jovi ZhangweiMain entry for userspace ktap tool.
[root@localhost ktap]# ./ktap
Usage: ktap [options] file [script args] -- cmd [args]
or: ktap [options] -e one-liner -- cmd [args]
Options and arguments:
-o file : send script output to file, instead of stderr -p pid : specific tracing pid -C cpu : cpu to monitor in system-wide -T : show timestamp for event -V : show version -v : enable verbose mode -q : suppress start tracing message -d : dry run mode(register NULL callback to perf events) -s : simple event tracing -b : list byte codes -le [glob] : list pre-defined events in system -lf DSO : list available functions from DSO -lm DSO : list available sdt notes from DSO file : program read from script file -- cmd [args] : workload to tracing
--- tools/ktap/userspace/kp_main.c | 443 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 443 insertions(+) create mode 100644 tools/ktap/userspace/kp_main.c
tools/ktap/userspace/kp_main.c new file +443 -0
@@ -0,0 +1,443 @@ +/* +* main.c - ktap compiler and loader entry +* +* This file is part of ktap by Jovi Zhangwei. +* +* Copyright (C) 2012-2014 Jovi Zhangwei <[email protected]>. +* +* ktap is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* ktap is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <sched.h> +#include <string.h> +#include <signal.h> +#include <stdarg.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include <sys/ioctl.h> +#include <sys/types.h> +#include <unistd.h> +#include <fcntl.h> +#include <math.h> +#include <linux/errno.h> + +#include "../include/ktap_types.h" +#include "kp_lex.h" +#include "kp_parse.h" +#include "kp_symbol.h" + +static void usage (const char *msg_fmt, ...) +{ +va_list ap; + +va_start (ap, msg_fmt); +vfprintf (stderr, msg_fmt, ap); +va_end (ap); + +fprintf (stderr, +"Usage: ktap [options] file [script args] -- cmd [args]\n" +" or: ktap [options] -e one-liner -- cmd [args]\n" +"\n" +"Options and arguments:\n" +" -o file : send script output to file, instead of stderr\n" +" -p pid : specific tracing pid\n" +" -C cpu : cpu to monitor in system-wide\n" +" -T : show timestamp for event\n" +" -V : show version\n" +" -v : enable verbose mode\n" +" -q : suppress start tracing message\n" +" -d : dry run mode(register NULL callback to perf events)\n" +" -s : simple event tracing\n" +" -b : list byte codes\n" +" -le [glob] : list pre-defined events in system\n" +#ifndef NO_LIBELF +" -lf DSO : list available functions from DSO\n" +" -lm DSO : list available sdt notes from DSO\n" +#endif +" file : program read from script file\n" +" -- cmd [args] : workload to tracing\n" ); + +exit (EXIT_FAILURE ); +} + +#define handle_error (str) do { perror(str); exit(-1); } while(0) + +ktap_option_t uparm; +static int ktap_trunk_mem_size =1024 ; + +static int kp_writer (const void * p,size_t sz,void * ud) +{ +if (uparm.trunk_len + sz > ktap_trunk_mem_size) { +int new_size = (uparm.trunk_len + sz) *2 ; + uparm.trunk =realloc (uparm.trunk, new_size); + ktap_trunk_mem_size = new_size; + } + +memcpy (uparm.trunk + uparm.trunk_len, p, sz); + uparm.trunk_len += sz; + +return 0 ; +} + + +static int forks; +static char **workload_argv; + +static int fork_workload (int ktap_fd) +{ +int pid; + + pid =fork (); +if (pid <0 ) +handle_error ("failed to fork" ); + +if (pid >0 ) +return pid; + +signal (SIGTERM ,SIG_DFL ); + +execvp ("" , workload_argv); + +/* +* waiting ktapvm prepare all tracing event +* make it more robust in future. +*/ +pause (); + +execvp (workload_argv[0 ], workload_argv); + +perror (workload_argv[0 ]); +exit (-1 ); + +return -1 ; +} + +#define KTAPVM_PATH "/sys/kernel/debug/ktap/ktapvm" + +static char *output_filename; + +static int run_ktapvm () +{ +int ktapvm_fd, ktap_fd; +int ret; + + ktapvm_fd =open (KTAPVM_PATH ,O_RDONLY ); +if (ktapvm_fd <0 ) +handle_error ("open " KTAPVM_PATH " failed" ); + + ktap_fd =ioctl (ktapvm_fd,0 ,NULL ); +if (ktap_fd <0 ) +handle_error ("ioctl ktapvm failed" ); + +kp_create_reader (output_filename); + +if (forks) { + uparm.trace_pid =fork_workload (ktap_fd); + uparm.workload =1 ; + } + + ret =ioctl (ktap_fd,KTAP_CMD_IOC_RUN , &uparm); +switch (ret) { +case -EPERM : +case -EACCES : +fprintf (stderr,"You may not have permission to run ktap\n" ); +break ; + } + +close (ktap_fd); +close (ktapvm_fd); + +return ret; +} + +int verbose; +static int quiet; +static int dry_run; +static int dump_bytecode; +static char oneline_src[1024 ]; +static int trace_pid =-1 ; +static int trace_cpu =-1 ; +static int print_timestamp; + +#define SIMPLE_ONE_LINER_FMT \ + "trace %s { print(cpu(), tid(), execname(), argstr) }" + +static const char *script_file; +static int script_args_start; +static int script_args_end; + +#ifndef NO_LIBELF +struct binary_base +{ +int type; +const char *binary; +}; +static int print_symbol (const char *name,vaddr_t addr,void *arg) +{ +struct binary_base *base = (struct binary_base *)arg; +const char *type = base->type ==FIND_SYMBOL ? +"probe" :"sdt" ; + +printf ("%s:%s:%s\n" , type, base->binary, name); +return 0 ; +} +#endif + +static void parse_option (int argc,char **argv) +{ +char pid[32 ] = {0 }; +char cpu_str[32 ] = {0 }; +char *next_arg; +int i, j; + +for (i =1 ; i < argc; i++) { +if (argv[i][0 ] !='-' ) { + script_file = argv[i]; +if (!script_file) +usage ("" ); + + script_args_start = i +1 ; + script_args_end = argc; + +for (j = i +1 ; j < argc; j++) { +if (argv[j][0 ] =='-' && argv[j][1 ] =='-' ) + goto found_cmd; + } + +return ; + } + +if (argv[i][0 ] =='-' && argv[i][1 ] =='-' ) { + j = i; + goto found_cmd; + } + + next_arg = argv[i +1 ]; + +switch (argv[i][1 ]) { +case 'o' : + output_filename =malloc (strlen (next_arg) +1 ); +if (!output_filename) +return ; + +strncpy (output_filename, next_arg,strlen (next_arg)); + i++; +break ; +case 'e' : +strncpy (oneline_src, next_arg,strlen (next_arg)); + i++; +break ; +case 'p' : +strncpy (pid, next_arg,strlen (next_arg)); + trace_pid =atoi (pid); + i++; +break ; +case 'C' : +strncpy (cpu_str, next_arg,strlen (next_arg)); + trace_cpu =atoi (cpu_str); + i++; +break ; +case 'T' : + print_timestamp =1 ; +break ; +case 'v' : + verbose =1 ; +break ; +case 'q' : + quiet =1 ; +break ; +case 'd' : + dry_run =1 ; +break ; +case 's' : +sprintf (oneline_src,SIMPLE_ONE_LINER_FMT , next_arg); + i++; +break ; +case 'b' : + dump_bytecode =1 ; +break ; +case 'l' :/* list available events */ +switch (argv[i][2 ]) { +case 'e' :/* tracepoints */ +list_available_events (next_arg); +exit (EXIT_SUCCESS ); +#ifndef NO_LIBELF +case 'f' :/* functions in DSO */ +case 'm' :/* static marks in DSO */ { +const char *binary = next_arg; +int type = argv[i][2 ] =='f' ? +FIND_SYMBOL :FIND_STAPSDT_NOTE ; +struct binary_base base = { + .type = type, + .binary = binary, + }; +int ret; + + ret =parse_dso_symbols (binary, type, + print_symbol, + (void *)&base); +if (ret <=0 ) { +fprintf (stderr, +"error: no symbols in binary %s\n" , + binary); +exit (EXIT_FAILURE ); + } +exit (EXIT_SUCCESS ); + } +#endif +default : +exit (EXIT_FAILURE ); + } +break ; +case 'V' : +usage ("%s\n\n" ,KTAP_VERSION ); +break ; +case '?' : +case 'h' : +usage ("" ); +break ; +default : +usage ("wrong argument\n" ); +break ; + } + } + +return ; + + found_cmd: + script_args_end = j; + forks =1 ; + workload_argv = &argv[j +1 ]; +} + +static ktap_proto_t *parse (const char *chunkname,const char *src) +{ +LexState ls; + + ls.chunkarg = chunkname ? chunkname :"?" ; +kp_lex_init (); +kp_buf_init (&ls.sb); +kp_lex_setup (&ls, src); +return kp_parse (&ls); +} + +static void compile (const char *input) +{ +ktap_proto_t *pt; +char *buff; +struct stat sb; +int fdin; + +kp_str_resize (); + +if (oneline_src[0 ] !='\0' ) { + pt =parse (input, oneline_src); + goto dump; + } + + fdin =open (input,O_RDONLY ); +if (fdin <0 ) { +fprintf (stderr,"open file %s failed\n" , input); +exit (-1 ); + } + +if (fstat (fdin, &sb) ==-1 ) +handle_error ("fstat failed" ); + + buff =mmap (NULL , sb.st_size,PROT_READ ,MAP_PRIVATE , fdin,0 ); +if (buff ==MAP_FAILED ) +handle_error ("mmap failed" ); + + pt =parse (input, buff); + +munmap (buff, sb.st_size); +close (fdin); + + dump: +if (dump_bytecode) { +kp_dump_proto (pt); +exit (0 ); + } + +/* bcwrite */ + uparm.trunk =malloc (ktap_trunk_mem_size); +if (!uparm.trunk) +handle_error ("malloc failed" ); + +kp_bcwrite (pt, kp_writer,NULL ,0 ); +} + +int main (int argc,char **argv) +{ +char **ktapvm_argv; +int new_index, i; +int ret; + +if (argc ==1 ) +usage ("" ); + +parse_option (argc, argv); + +if (oneline_src[0 ] !='\0' ) + script_file ="(command line)" ; + +compile (script_file); + + ktapvm_argv = (char **)malloc (sizeof (char *)*(script_args_end - + script_args_start +1 )); +if (!ktapvm_argv) { +fprintf (stderr,"canno allocate ktapvm_argv\n" ); +return -1 ; + } + + ktapvm_argv[0 ] =malloc (strlen (script_file) +1 ); +if (!ktapvm_argv[0 ]) { +fprintf (stderr,"canno allocate memory\n" ); +return -1 ; + } +strcpy (ktapvm_argv[0 ], script_file); + ktapvm_argv[0 ][strlen (script_file)] ='\0' ; + +/* pass rest argv into ktapvm */ + new_index =1 ; +for (i = script_args_start; i < script_args_end; i++) { + ktapvm_argv[new_index] =malloc (strlen (argv[i]) +1 ); +if (!ktapvm_argv[new_index]) { +fprintf (stderr,"canno allocate memory\n" ); +return -1 ; + } +strcpy (ktapvm_argv[new_index], argv[i]); + ktapvm_argv[new_index][strlen (argv[i])] ='\0' ; + new_index++; + } + + uparm.argv = ktapvm_argv; + uparm.argc = new_index; + uparm.verbose = verbose; + uparm.trace_pid = trace_pid; + uparm.trace_cpu = trace_cpu; + uparm.print_timestamp = print_timestamp; + uparm.quiet = quiet; + uparm.dry_run = dry_run; + +/* start running into kernel ktapvm */ + ret =run_ktapvm (); + +cleanup_event_resources (); +return ret; +} + +
Signature · 1 line
1.8.1.4
PATCH 18/28 ktap: add compiler(userspace/kp_lex.[c|h] and userspace/kp_parse.[c|h])
In reply to Jovi Zhangweiktap compiler is based on luajit, origin from lua.
The compiler use one pass compilation, it is very fast, and the generated binary is very small.
[root@localhost ktap]# ll -h ktap
-rwxr-xr-x. 1 root root 83K Mar 27 06:09 ktap
The compiler is easy to hack, the code is very easy to understand.
Note that some bytecode in luajit is not used by ktap, it could be remove in future.
--- tools/ktap/userspace/kp_lex.c | 552 +++++++ tools/ktap/userspace/kp_lex.h | 94 ++ tools/ktap/userspace/kp_parse.c | 3139 +++++++++++++++++++++++++++++++++++++++ tools/ktap/userspace/kp_parse.h | 4 + 4 files changed, 3789 insertions(+) create mode 100644 tools/ktap/userspace/kp_lex.c create mode 100644 tools/ktap/userspace/kp_lex.h create mode 100644 tools/ktap/userspace/kp_parse.c create mode 100644 tools/ktap/userspace/kp_parse.h
tools/ktap/userspace/kp_lex.c new file +552 -0
@@ -0,0 +1,552 @@ +/* +* Lexical analyzer. +* +* This file is part of ktap by Jovi Zhangwei. +* +* Copyright (C) 2012-2014 Jovi Zhangwei <[email protected]>. +* +* Adapted from luajit and lua interpreter. +* Copyright (C) 2005-2014 Mike Pall. +* Copyright (C) 1994-2008 Lua.org, PUC-Rio. +* +* ktap is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* ktap is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "../include/ktap_types.h" +#include "../include/ktap_err.h" +#include "kp_util.h" +#include "kp_lex.h" +#include "kp_parse.h" + +/* lexer token names. */ +static const char *const tokennames[] = { +#define TKSTR1(name) #name, +#define TKSTR2(name, sym) #sym, +TKDEF(TKSTR1, TKSTR2) +#undef TKSTR1 +#undef TKSTR2 +NULL +}; + +/* -- Buffer handling ----------------------------------------------------- */ + +#define LEX_EOF (-1) +#define lex_iseol (ls) (ls->c == '\n' || ls->c == '\r') + +/* Get next character. */ +static inline LexChar lex_next (LexState *ls) +{ +return (ls->c = ls->p < ls->pe ? (LexChar)(uint8_t)*ls->p++ :LEX_EOF ); +} + +/* Save character. */ +static inline void lex_save (LexState *ls,LexChar c) +{ +kp_buf_putb (&ls->sb, c); +} + +/* Save previous character and get next character. */ +static inline LexChar lex_savenext (LexState *ls) +{ +lex_save (ls, ls->c); +return lex_next (ls); +} + +/* Skip line break. Handles "\n", "\r", "\r\n" or "\n\r". */ +static void lex_newline (LexState *ls) +{ +LexChar old = ls->c; + +kp_assert (lex_iseol (ls)); +lex_next (ls);/* Skip "\n" or "\r". */ +if (lex_iseol (ls) && ls->c != old) +lex_next (ls);/* Skip "\n\r" or "\r\n". */ +if (++ls->linenumber >=KP_MAX_LINE ) +kp_lex_error (ls, ls->tok,KP_ERR_XLINES ); +} + +/* -- Scanner for terminals ----------------------------------------------- */ + +static int kp_str2d (const char *s,size_t len,ktap_number *result) +{ +char *endptr; + +if (strpbrk (s,"nN" ))/* reject 'inf' and 'nan' */ +return 0 ; +else + *result = (long )strtoul (s, &endptr,0 ); + +if (endptr == s) +return 0 ;/* nothing recognized */ +while (kp_char_isspace ((unsigned char )(*endptr))) + endptr++; +return (endptr == s + len);/* OK if no trailing characters */ +} + + +/* Parse a number literal. */ +static void lex_number (LexState *ls,ktap_val_t *tv) +{ +LexChar c, xp ='e' ; +ktap_number n =0 ; + +kp_assert (kp_char_isdigit (ls->c)); +if ((c = ls->c) =='0' && (lex_savenext (ls) |0x20 ) =='x' ) + xp ='p' ; +while (kp_char_isident (ls->c) || ls->c =='.' || + ((ls->c =='-' || ls->c =='+' ) && (c |0x20 ) == xp)) { + c = ls->c; +lex_savenext (ls); + } +lex_save (ls,'\0' ); +if (!kp_str2d (sbufB (&ls->sb),sbuflen (&ls->sb) -1 , &n)) +kp_lex_error (ls, ls->tok,KP_ERR_XNUMBER ); +set_number (tv, n); +} + +/* Skip equal signs for "[=...=[" and "]=...=]" and return their count. */ +static int lex_skipeq (LexState *ls) +{ +int count =0 ; +LexChar s = ls->c; + +kp_assert (s =='[' || s ==']' ); +while (lex_savenext (ls) =='=' ) + count++; +return (ls->c == s) ? count : (-count) -1 ; +} + +/* Parse a long string or long comment (tv set to NULL). */ +static void lex_longstring (LexState *ls,ktap_val_t *tv,int sep) +{ +lex_savenext (ls);/* Skip second '['. */ +if (lex_iseol (ls))/* Skip initial newline. */ +lex_newline (ls); +for (;;) { +switch (ls->c) { +case LEX_EOF : +kp_lex_error (ls, TK_eof, + tv ?KP_ERR_XLSTR :KP_ERR_XLCOM ); +break ; +case ']' : +if (lex_skipeq (ls) == sep) { +lex_savenext (ls);/* Skip second ']'. */ + goto endloop; + } +break ; +case '\n' : +case '\r' : +lex_save (ls,'\n' ); +lex_newline (ls); +if (!tv)/* Don't waste space for comments. */ +kp_buf_reset (&ls->sb); +break ; +default : +lex_savenext (ls); +break ; + } + } + endloop: +if (tv) { +ktap_str_t *str =kp_parse_keepstr (ls, +sbufB (&ls->sb) + (2 + (int )sep), +sbuflen (&ls->sb) -2 *(2 + (int )sep)); +set_string (tv, str); + } +} + +/* Parse a string. */ +static void lex_string (LexState *ls,ktap_val_t *tv) +{ +LexChar delim = ls->c;/* Delimiter is '\'' or '"'. */ + +lex_savenext (ls); +while (ls->c != delim) { +switch (ls->c) { +case LEX_EOF : +kp_lex_error (ls, TK_eof,KP_ERR_XSTR ); +continue ; +case '\n' : +case '\r' : +kp_lex_error (ls, TK_string,KP_ERR_XSTR ); +continue ; +case '\\' : { +LexChar c =lex_next (ls);/* Skip the '\\'. */ +switch (c) { +case 'a' : c ='\a' ;break ; +case 'b' : c ='\b' ;break ; +case 'f' : c ='\f' ;break ; +case 'n' : c ='\n' ;break ; +case 'r' : c ='\r' ;break ; +case 't' : c ='\t' ;break ; +case 'v' : c ='\v' ;break ; +case 'x' :/* Hexadecimal escape '\xXX'. */ + c = (lex_next (ls) &15u ) <<4 ; +if (!kp_char_isdigit (ls->c)) { +if (!kp_char_isxdigit (ls->c)) + goto err_xesc; + c +=9 <<4 ; + } + c += (lex_next (ls) &15u ); +if (!kp_char_isdigit (ls->c)) { +if (!kp_char_isxdigit (ls->c)) + goto err_xesc; + c +=9 ; + } +break ; +case 'z' :/* Skip whitespace. */ +lex_next (ls); +while (kp_char_isspace (ls->c)) +if (lex_iseol (ls)) +lex_newline (ls); +else +lex_next (ls); +continue ; +case '\n' :case '\r' : +lex_save (ls,'\n' ); +lex_newline (ls); +continue ; +case '\\' :case '\"' :case '\'' : +break ; +case LEX_EOF : +continue ; +default : +if (!kp_char_isdigit (c)) + goto err_xesc; + c -='0' ;/* Decimal escape '\ddd'. */ +if (kp_char_isdigit (lex_next (ls))) { + c = c*10 + (ls->c -'0' ); +if (kp_char_isdigit (lex_next (ls))) { + c = c*10 + (ls->c -'0' ); +if (c >255 ) { + err_xesc: +kp_lex_error (ls, + TK_string, +KP_ERR_XESC ); + } +lex_next (ls); + } + } +lex_save (ls, c); +continue ; + } +lex_save (ls, c); +lex_next (ls); +continue ; + } +default : +lex_savenext (ls); +break ; + } + } +lex_savenext (ls);/* Skip trailing delimiter. */ +set_string (tv, +kp_parse_keepstr (ls,sbufB (&ls->sb)+1 ,sbuflen (&ls->sb)-2 )); +} + +/* lex helper for parse_trace and parse_timer */ +void kp_lex_read_string_until (LexState *ls,int c) +{ +ktap_str_t *ts; + +kp_buf_reset (&ls->sb); + +while (ls->c ==' ' ) +lex_next (ls); + +do { +lex_savenext (ls); + }while (ls->c != c && ls->c !=LEX_EOF ); + +if (ls->c != c) +kp_lex_error (ls, ls->tok,KP_ERR_XTOKEN , c); + + ts =kp_parse_keepstr (ls,sbufB (&ls->sb),sbuflen (&ls->sb)); + ls->tok = TK_string; +set_string (&ls->tokval, ts); +} + + +/* -- Main lexical scanner ------------------------------------------------ */ + +/* Get next lexical token. */ +static LexToken lex_scan (LexState *ls,ktap_val_t *tv) +{ +kp_buf_reset (&ls->sb); +for (;;) { +if (kp_char_isident (ls->c)) { +ktap_str_t *s; +if (kp_char_isdigit (ls->c)) {/* Numeric literal. */ +lex_number (ls, tv); +return TK_number; + } +/* Identifier or reserved word. */ +do { +lex_savenext (ls); + }while (kp_char_isident (ls->c)); + s =kp_parse_keepstr (ls,sbufB (&ls->sb), +sbuflen (&ls->sb)); +set_string (tv, s); +if (s->reserved >0 )/* Reserved word? */ +return TK_OFS + s->reserved; +return TK_name; + } + +switch (ls->c) { +case '\n' : +case '\r' : +lex_newline (ls); +continue ; +case ' ' : +case '\t' : +case '\v' : +case '\f' : +lex_next (ls); +continue ; + +case '#' : +while (!lex_iseol (ls) && ls->c !=LEX_EOF ) +lex_next (ls); +break ; +case '-' : +lex_next (ls); +if (ls->c !='-' ) +return '-' ; +lex_next (ls); +if (ls->c =='[' ) {/* Long comment "--[=*[...]=*]". */ +int sep =lex_skipeq (ls); +/* `lex_skipeq' may dirty the buffer */ +kp_buf_reset (&ls->sb); +if (sep >=0 ) { +lex_longstring (ls,NULL , sep); +kp_buf_reset (&ls->sb); +continue ; + } + } +/* Short comment "--.*\n". */ +while (!lex_iseol (ls) && ls->c !=LEX_EOF ) +lex_next (ls); +continue ; +case '[' : { +int sep =lex_skipeq (ls); +if (sep >=0 ) { +lex_longstring (ls, tv, sep); +return TK_string; + }else if (sep ==-1 ) { +return '[' ; + }else { +kp_lex_error (ls, TK_string,KP_ERR_XLDELIM ); +continue ; + } + } +case '+' : { +lex_next (ls); +if (ls->c !='=' ) +return '+' ; +else { +lex_next (ls); +return TK_incr; + } + } +case '=' : +lex_next (ls); +if (ls->c !='=' ) +return '=' ; +else { +lex_next (ls); +return TK_eq; + } +case '<' : +lex_next (ls); +if (ls->c !='=' ) +return '<' ; +else { +lex_next (ls); +return TK_le; + } +case '>' : +lex_next (ls); +if (ls->c !='=' ) +return '>' ; +else { +lex_next (ls); +return TK_ge; + } +case '!' : +lex_next (ls); +if (ls->c !='=' ) +return TK_not; +else { +lex_next (ls); +return TK_ne; + } +case ':' : +lex_next (ls); +if (ls->c !=':' ) +return ':' ; +else { +lex_next (ls); +return TK_label; + } +case '"' : +case '\'' : +lex_string (ls, tv); +return TK_string; +case '.' : +if (lex_savenext (ls) =='.' ) { +lex_next (ls); +if (ls->c =='.' ) { +lex_next (ls); +return TK_dots;/* ... */ + } +return TK_concat;/* .. */ + }else if (!kp_char_isdigit (ls->c)) { +return '.' ; + }else { +lex_number (ls, tv); +return TK_number; + } +case LEX_EOF : +return TK_eof; +case '&' : +lex_next (ls); +if (ls->c !='&' ) +return '&' ; +else { +lex_next (ls); +return TK_and; + } +case '|' : +lex_next (ls); +if (ls->c !='|' ) +return '|' ; +else { +lex_next (ls); +return TK_or; + } +default : { +LexChar c = ls->c; +lex_next (ls); +return c;/* Single-char tokens (+ - / ...). */ + } + } + } +} + +/* -- Lexer API ----------------------------------------------------------- */ + +/* Setup lexer state. */ +int kp_lex_setup (LexState *ls,const char *str) +{ + ls->fs =NULL ; + ls->pe = ls->p =NULL ; + ls->p = str; + ls->pe = str +strlen (str); + ls->vstack =NULL ; + ls->sizevstack =0 ; + ls->vtop =0 ; + ls->bcstack =NULL ; + ls->sizebcstack =0 ; + ls->lookahead = TK_eof;/* No look-ahead token. */ + ls->linenumber =1 ; + ls->lastline =1 ; +lex_next (ls);/* Read-ahead first char. */ +if (ls->c ==0xef && ls->p +2 <= ls->pe && + (uint8_t )ls->p[0 ] ==0xbb && + (uint8_t )ls->p[1 ] ==0xbf ) {/* Skip UTF-8 BOM (if buffered). */ + ls->p +=2 ; +lex_next (ls); + } +if (ls->c =='#' ) {/* Skip POSIX #! header line. */ +do { +lex_next (ls); +if (ls->c ==LEX_EOF ) +return 0 ; + }while (!lex_iseol (ls)); +lex_newline (ls); + } +return 0 ; +} + +/* Cleanup lexer state. */ +void kp_lex_cleanup (LexState *ls) +{ +free (ls->bcstack); +free (ls->vstack); +kp_buf_free (&ls->sb); +} + +/* Return next lexical token. */ +void kp_lex_next (LexState *ls) +{ + ls->lastline = ls->linenumber; +if (ls->lookahead == TK_eof) {/* No lookahead token? */ + ls->tok =lex_scan (ls, &ls->tokval);/* Get next token. */ + }else {/* Otherwise return lookahead token. */ + ls->tok = ls->lookahead; + ls->lookahead = TK_eof; + ls->tokval = ls->lookaheadval; + } +} + +/* Look ahead for the next token. */ +LexToken kp_lex_lookahead (LexState *ls) +{ +kp_assert (ls->lookahead == TK_eof); + ls->lookahead =lex_scan (ls, &ls->lookaheadval); +return ls->lookahead; +} + +/* Convert token to string. */ +const char *kp_lex_token2str (LexState *ls,LexToken tok) +{ +if (tok >TK_OFS ) +return tokennames[tok-TK_OFS -1 ]; +else if (!kp_char_iscntrl (tok)) +return kp_sprintf ("%c" , tok); +else +return kp_sprintf ("char(%d)" , tok); +} + +/* Lexer error. */ +void kp_lex_error (LexState *ls,LexToken tok,ErrMsg em, ...) +{ +const char *tokstr; +va_list argp; + +if (tok ==0 ) { + tokstr =NULL ; + }else if (tok == TK_name || tok == TK_string || tok == TK_number) { +lex_save (ls,'\0' ); + tokstr =sbufB (&ls->sb); + }else { + tokstr =kp_lex_token2str (ls, tok); + } + +va_start (argp, em); +kp_err_lex (ls->chunkname, tokstr, ls->linenumber, em, argp); +va_end (argp); +} + +/* Initialize strings for reserved words. */ +void kp_lex_init () +{ +uint32_t i; + +for (i =0 ; i <TK_RESERVED ; i++) { +ktap_str_t *s =kp_str_newz (tokennames[i]); + s->reserved = (uint8_t )(i+1 ); + } +} +
tools/ktap/userspace/kp_lex.h new file +94 -0
94 lines not on this page · raw message
tools/ktap/userspace/kp_parse.c new file +3139 -0
2,354 lines not on this page · raw message
tools/ktap/userspace/kp_parse.h new file +4 -0
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
Signature · 1 line
1.8.1.4
Part of the diff is not shown here. View the raw message.
PATCH 19/28 ktap: add userspace/symbol.[c|h]
In reply to Jovi ZhangweiThis file is use for uprobe(include SDT) symbol lookup, for example:
trace probe:/lib64/libc.so.6:malloc {
print("malloc entry:", execname)
}
trace sdt:/lib64/libc.so.6:* {
print(execname, argstr)
}
It need libelf library support.
--- tools/ktap/userspace/kp_symbol.c | 360 +++++++++++++++++++++++++++++++++++++++ tools/ktap/userspace/kp_symbol.h | 50 ++++++ 2 files changed, 410 insertions(+) create mode 100644 tools/ktap/userspace/kp_symbol.c create mode 100644 tools/ktap/userspace/kp_symbol.h
tools/ktap/userspace/kp_symbol.c new file +360 -0
@@ -0,0 +1,360 @@ +/* +* symbol.c +* +* This file is part of ktap by Jovi Zhangwei. +* +* Copyright (C) 2013 Azat Khuzhin <[email protected]>. +* +* ktap is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* ktap is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include <stdio.h> +#include <stdlib.h> + +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <fcntl.h> +#include <string.h> +#include <linux/limits.h> + +#include <libelf.h> + +#include "../include/ktap_types.h" +#include "kp_symbol.h" + +const char *dbg_link_name =".gnu_debuglink" ; +const char *dbg_bin_dir ="/usr/lib/debug" ; + +static Elf_Scn *elf_section_by_name (Elf *elf,GElf_Ehdr *ep, +GElf_Shdr *shp,const char *name) +{ +Elf_Scn *scn =NULL ; + +/* Elf is corrupted/truncated, avoid calling elf_strptr. */ +if (!elf_rawdata (elf_getscn (elf, ep->e_shstrndx),NULL )) +return NULL ; + +while ((scn =elf_nextscn (elf, scn)) !=NULL ) { +char *str; + +gelf_getshdr (scn, shp); + str =elf_strptr (elf, ep->e_shstrndx, shp->sh_name); +if (!strcmp (name, str)) +break ; + } + +return scn; +} + +/** +* @return v_addr of "LOAD" program header, that have zero offset. +*/ +static int find_load_address (Elf *elf,vaddr_t *load_address) +{ +GElf_Phdr phdr; +size_t i, phdrnum; + +if (elf_getphdrnum (elf, &phdrnum)) +return -1 ; + +for (i =0 ; i < phdrnum; i++) { +if (gelf_getphdr (elf, i, &phdr) ==NULL ) +return -1 ; + +if (phdr.p_type !=PT_LOAD || phdr.p_offset !=0 ) +continue ; + + *load_address = phdr.p_vaddr; +return 0 ; + } + +/* cannot found load address */ +return -1 ; +} + +static size_t elf_symbols (GElf_Shdr shdr) +{ +return shdr.sh_size / shdr.sh_entsize; +} + +static int dso_symbols (Elf *elf,symbol_actor actor,void *arg) +{ +Elf_Data *elf_data =NULL ; +Elf_Scn *scn =NULL ; +GElf_Sym sym; +GElf_Shdr shdr; +int symbols_count =0 ; +vaddr_t load_address; + +if (find_load_address (elf, &load_address)) +return -1 ; + +while ((scn =elf_nextscn (elf, scn))) { +int i; + +gelf_getshdr (scn, &shdr); + +if (shdr.sh_type !=SHT_SYMTAB ) +continue ; + + elf_data =elf_getdata (scn, elf_data); + +for (i =0 ; i <elf_symbols (shdr); i++) { +char *name; +vaddr_t addr; +int ret; + +gelf_getsym (elf_data, i, &sym); + +if (GELF_ST_TYPE (sym.st_info) !=STT_FUNC ) +continue ; + + name =elf_strptr (elf, shdr.sh_link, sym.st_name); + addr = sym.st_value - load_address; + + ret =actor (name, addr, arg); +if (ret) +return ret; + + ++symbols_count; + } + } + +return symbols_count; +} + +#define SDT_NOTE_TYPE 3 +#define SDT_NOTE_COUNT 3 +#define SDT_NOTE_SCN ".note.stapsdt" +#define SDT_NOTE_NAME "stapsdt" + +static vaddr_t sdt_note_addr (Elf *elf,const char *data,size_t len,int type) +{ +vaddr_t vaddr; + +/* +* Three addresses need to be obtained : +* Marker location, address of base section and semaphore location +*/ +union { +Elf64_Addr a64[3 ]; +Elf32_Addr a32[3 ]; + } buf; + +/* +* dst and src are required for translation from file to memory +* representation +*/ +Elf_Data dst = { + .d_buf = &buf, .d_type =ELF_T_ADDR , .d_version =EV_CURRENT , + .d_size =gelf_fsize (elf,ELF_T_ADDR ,SDT_NOTE_COUNT ,EV_CURRENT ), + .d_off =0 , .d_align =0 + }; + +Elf_Data src = { + .d_buf = (void *) data, .d_type =ELF_T_ADDR , + .d_version =EV_CURRENT , .d_size = dst.d_size, .d_off =0 , + .d_align =0 + }; + +/* Check the type of each of the notes */ +if (type !=SDT_NOTE_TYPE ) +return 0 ; + +if (len < dst.d_size +SDT_NOTE_COUNT ) +return 0 ; + +/* Translation from file representation to memory representation */ +if (gelf_xlatetom (elf, &dst, &src, +elf_getident (elf,NULL )[EI_DATA ]) ==NULL ) +return 0 ;/* TODO */ + +memcpy (&vaddr, &buf,sizeof (vaddr)); + +return vaddr; +} + +static const char *sdt_note_name (Elf *elf,GElf_Nhdr *nhdr,const char *data) +{ +const char *provider = data +gelf_fsize (elf, +ELF_T_ADDR ,SDT_NOTE_COUNT ,EV_CURRENT ); +const char *name = (const char *)memchr (provider,'\0' , + data + nhdr->n_descsz - provider); + +if (name++ ==NULL ) +return NULL ; + +return name; +} + +static const char *sdt_note_data (const Elf_Data *data,size_t off) +{ +return ((data->d_buf) + off); +} + +static int dso_sdt_notes (Elf *elf,symbol_actor actor,void *arg) +{ +GElf_Ehdr ehdr; +Elf_Scn *scn =NULL ; +Elf_Data *data; +GElf_Shdr shdr; +size_t shstrndx; +size_t next; +GElf_Nhdr nhdr; +size_t name_off, desc_off, offset; +vaddr_t vaddr =0 ; +int symbols_count =0 ; + +if (gelf_getehdr (elf, &ehdr) ==NULL ) +return 0 ; +if (elf_getshdrstrndx (elf, &shstrndx) !=0 ) +return 0 ; + +/* +* Look for section type = SHT_NOTE, flags = no SHF_ALLOC +* and name = .note.stapsdt +*/ + scn =elf_section_by_name (elf, &ehdr, &shdr,SDT_NOTE_SCN ); +if (!scn) +return 0 ; +if (!(shdr.sh_type ==SHT_NOTE ) || (shdr.sh_flags &SHF_ALLOC )) +return 0 ; + + data =elf_getdata (scn,NULL ); + +for (offset =0 ; + (next =gelf_getnote (data, offset, &nhdr, &name_off, &desc_off)) >0 ; + offset = next) { +const char *name; +int ret; + +if (nhdr.n_namesz !=sizeof (SDT_NOTE_NAME ) || +memcmp (data->d_buf + name_off,SDT_NOTE_NAME , +sizeof (SDT_NOTE_NAME ))) +continue ; + + name =sdt_note_name (elf, &nhdr,sdt_note_data (data, desc_off)); +if (!name) +continue ; + + vaddr =sdt_note_addr (elf,sdt_note_data (data, desc_off), + nhdr.n_descsz, nhdr.n_type); +if (!vaddr) +continue ; + + ret =actor (name, vaddr, arg); +if (ret) +return ret; + + ++symbols_count; + } + +return symbols_count; +} + +int dso_follow_debuglink (Elf *elf, +const char *orig_exec, +int type, +symbol_actor actor, +void *arg) +{ +GElf_Ehdr ehdr; +size_t shstrndx, orig_exec_dir_len; +GElf_Shdr shdr; +Elf_Scn *dbg_link_scn; +Elf_Data *dbg_link_scn_data; +char *dbg_link, *dbg_bin, *last_slash; +int symbols_count; + +/* First try to find the .gnu_debuglink section in the binary. */ +if (gelf_getehdr (elf, &ehdr) ==NULL ) +return 0 ; +if (elf_getshdrstrndx (elf, &shstrndx) !=0 ) +return 0 ; + + dbg_link_scn =elf_section_by_name (elf, &ehdr, &shdr, dbg_link_name); +if (dbg_link_scn ==NULL ) +return 0 ; + +/* Debug link section found, read of the content (only get the first +string, no checksum checking atm). This is debug binary file name. */ + dbg_link_scn_data =elf_getdata (dbg_link_scn,NULL ); +if (dbg_link_scn_data ==NULL || + dbg_link_scn_data->d_size <=0 || + dbg_link_scn_data->d_buf ==NULL ) +return 0 ; + +/* Now compose debug executable name */ + dbg_link = (char *)(dbg_link_scn_data->d_buf); + dbg_bin =malloc (strlen (dbg_bin_dir) +1 + +strlen (orig_exec) +1 + +strlen (dbg_link) +1 ); +if (!dbg_bin) +return 0 ; + + orig_exec_dir_len =PATH_MAX ; + last_slash =strrchr (orig_exec,'/' ); +if (last_slash !=NULL ) + orig_exec_dir_len = last_slash - orig_exec; + +sprintf (dbg_bin,"%s/%.*s/%s" , + dbg_bin_dir, (int )orig_exec_dir_len, orig_exec, dbg_link); + +/* Retry symbol seach with the debug binary */ + symbols_count =parse_dso_symbols (dbg_bin, type, actor, arg); + +free (dbg_bin); + +return symbols_count; +} + +int parse_dso_symbols (const char *exec,int type,symbol_actor actor,void *arg) +{ +int symbols_count =0 ; +Elf *elf; +int fd; + +if (elf_version (EV_CURRENT ) ==EV_NONE ) +return -1 ; + + fd =open (exec,O_RDONLY ); +if (fd <0 ) +return -1 ; + + elf =elf_begin (fd,ELF_C_READ ,NULL ); +if (elf) { +switch (type) { +case FIND_SYMBOL : + symbols_count =dso_symbols (elf, actor, arg); +if (symbols_count !=0 ) +break ; +/* If no symbols found, try in the debuglink binary. */ + symbols_count =dso_follow_debuglink (elf, + exec, + type, + actor, + arg); +break ; +case FIND_STAPSDT_NOTE : + symbols_count =dso_sdt_notes (elf, actor, arg); +break ; + } + +elf_end (elf); + } + +close (fd); +return symbols_count; +}
tools/ktap/userspace/kp_symbol.h new file +50 -0
50 lines not on this page · raw message
Signature · 1 line
1.8.1.4
Re: [PATCH 19/28] ktap: add userspace/symbol.[c|h]
Quoted 7 lines · Jovi Zhangwei
This file is use for uprobe(include SDT) symbol lookup, for example:
trace probe:/lib64/libc.so.6:malloc {
print("malloc entry:", execname)
}
Quoted 5 lines
trace sdt:/lib64/libc.so.6:* {
print(execname, argstr)
}It need libelf library support.
Hmm, I think I should support this in perf-probe too...
Thank you,
Quoted 432 lines
Signed-off-by Jovi Zhangwei <[email protected]>--- tools/ktap/userspace/kp_symbol.c | 360 +++++++++++++++++++++++++++++++++++++++ tools/ktap/userspace/kp_symbol.h | 50 ++++++ 2 files changed, 410 insertions(+) create mode 100644 tools/ktap/userspace/kp_symbol.c create mode 100644 tools/ktap/userspace/kp_symbol.htools/ktap/userspace/kp_symbol.c new file +360
-0@@ -0,0 +1,360 @@ +/* +* symbol.c +* +* This file is part of ktap by Jovi Zhangwei. +* +* Copyright (C) 2013 Azat Khuzhin <[email protected]>. +* +* ktap is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* ktap is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include <stdio.h> +#include <stdlib.h> + +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <fcntl.h> +#include <string.h> +#include <linux/limits.h> + +#include <libelf.h> + +#include "../include/ktap_types.h" +#include "kp_symbol.h" + +const char *dbg_link_name =".gnu_debuglink" ; +const char *dbg_bin_dir ="/usr/lib/debug" ; + +static Elf_Scn *elf_section_by_name (Elf *elf,GElf_Ehdr *ep, +GElf_Shdr *shp,const char *name) +{ +Elf_Scn *scn =NULL ; + +/* Elf is corrupted/truncated, avoid calling elf_strptr. */ +if (!elf_rawdata (elf_getscn (elf, ep->e_shstrndx),NULL )) +return NULL ; + +while ((scn =elf_nextscn (elf, scn)) !=NULL ) { +char *str; + +gelf_getshdr (scn, shp); + str =elf_strptr (elf, ep->e_shstrndx, shp->sh_name); +if (!strcmp (name, str)) +break ; + } + +return scn; +} + +/** +* @return v_addr of "LOAD" program header, that have zero offset. +*/ +static int find_load_address (Elf *elf,vaddr_t *load_address) +{ +GElf_Phdr phdr; +size_t i, phdrnum; + +if (elf_getphdrnum (elf, &phdrnum)) +return -1 ; + +for (i =0 ; i < phdrnum; i++) { +if (gelf_getphdr (elf, i, &phdr) ==NULL ) +return -1 ; + +if (phdr.p_type !=PT_LOAD || phdr.p_offset !=0 ) +continue ; + + *load_address = phdr.p_vaddr; +return 0 ; + } + +/* cannot found load address */ +return -1 ; +} + +static size_t elf_symbols (GElf_Shdr shdr) +{ +return shdr.sh_size / shdr.sh_entsize; +} + +static int dso_symbols (Elf *elf,symbol_actor actor,void *arg) +{ +Elf_Data *elf_data =NULL ; +Elf_Scn *scn =NULL ; +GElf_Sym sym; +GElf_Shdr shdr; +int symbols_count =0 ; +vaddr_t load_address; + +if (find_load_address (elf, &load_address)) +return -1 ; + +while ((scn =elf_nextscn (elf, scn))) { +int i; + +gelf_getshdr (scn, &shdr); + +if (shdr.sh_type !=SHT_SYMTAB ) +continue ; + + elf_data =elf_getdata (scn, elf_data); + +for (i =0 ; i <elf_symbols (shdr); i++) { +char *name; +vaddr_t addr; +int ret; + +gelf_getsym (elf_data, i, &sym); + +if (GELF_ST_TYPE (sym.st_info) !=STT_FUNC ) +continue ; + + name =elf_strptr (elf, shdr.sh_link, sym.st_name); + addr = sym.st_value - load_address; + + ret =actor (name, addr, arg); +if (ret) +return ret; + + ++symbols_count; + } + } + +return symbols_count; +} + +#define SDT_NOTE_TYPE 3 +#define SDT_NOTE_COUNT 3 +#define SDT_NOTE_SCN ".note.stapsdt" +#define SDT_NOTE_NAME "stapsdt" + +static vaddr_t sdt_note_addr (Elf *elf,const char *data,size_t len,int type) +{ +vaddr_t vaddr; + +/* +* Three addresses need to be obtained : +* Marker location, address of base section and semaphore location +*/ +union { +Elf64_Addr a64[3 ]; +Elf32_Addr a32[3 ]; + } buf; + +/* +* dst and src are required for translation from file to memory +* representation +*/ +Elf_Data dst = { + .d_buf = &buf, .d_type =ELF_T_ADDR , .d_version =EV_CURRENT , + .d_size =gelf_fsize (elf,ELF_T_ADDR ,SDT_NOTE_COUNT ,EV_CURRENT ), + .d_off =0 , .d_align =0 + }; + +Elf_Data src = { + .d_buf = (void *) data, .d_type =ELF_T_ADDR , + .d_version =EV_CURRENT , .d_size = dst.d_size, .d_off =0 , + .d_align =0 + }; + +/* Check the type of each of the notes */ +if (type !=SDT_NOTE_TYPE ) +return 0 ; + +if (len < dst.d_size +SDT_NOTE_COUNT ) +return 0 ; + +/* Translation from file representation to memory representation */ +if (gelf_xlatetom (elf, &dst, &src, +elf_getident (elf,NULL )[EI_DATA ]) ==NULL ) +return 0 ;/* TODO */ + +memcpy (&vaddr, &buf,sizeof (vaddr)); + +return vaddr; +} + +static const char *sdt_note_name (Elf *elf,GElf_Nhdr *nhdr,const char *data) +{ +const char *provider = data +gelf_fsize (elf, +ELF_T_ADDR ,SDT_NOTE_COUNT ,EV_CURRENT ); +const char *name = (const char *)memchr (provider,'\0' , + data + nhdr->n_descsz - provider); + +if (name++ ==NULL ) +return NULL ; + +return name; +} + +static const char *sdt_note_data (const Elf_Data *data,size_t off) +{ +return ((data->d_buf) + off); +} + +static int dso_sdt_notes (Elf *elf,symbol_actor actor,void *arg) +{ +GElf_Ehdr ehdr; +Elf_Scn *scn =NULL ; +Elf_Data *data; +GElf_Shdr shdr; +size_t shstrndx; +size_t next; +GElf_Nhdr nhdr; +size_t name_off, desc_off, offset; +vaddr_t vaddr =0 ; +int symbols_count =0 ; + +if (gelf_getehdr (elf, &ehdr) ==NULL ) +return 0 ; +if (elf_getshdrstrndx (elf, &shstrndx) !=0 ) +return 0 ; + +/* +* Look for section type = SHT_NOTE, flags = no SHF_ALLOC +* and name = .note.stapsdt +*/ + scn =elf_section_by_name (elf, &ehdr, &shdr,SDT_NOTE_SCN ); +if (!scn) +return 0 ; +if (!(shdr.sh_type ==SHT_NOTE ) || (shdr.sh_flags &SHF_ALLOC )) +return 0 ; + + data =elf_getdata (scn,NULL ); + +for (offset =0 ; + (next =gelf_getnote (data, offset, &nhdr, &name_off, &desc_off)) >0 ; + offset = next) { +const char *name; +int ret; + +if (nhdr.n_namesz !=sizeof (SDT_NOTE_NAME ) || +memcmp (data->d_buf + name_off,SDT_NOTE_NAME , +sizeof (SDT_NOTE_NAME ))) +continue ; + + name =sdt_note_name (elf, &nhdr,sdt_note_data (data, desc_off)); +if (!name) +continue ; + + vaddr =sdt_note_addr (elf,sdt_note_data (data, desc_off), + nhdr.n_descsz, nhdr.n_type); +if (!vaddr) +continue ; + + ret =actor (name, vaddr, arg); +if (ret) +return ret; + + ++symbols_count; + } + +return symbols_count; +} + +int dso_follow_debuglink (Elf *elf, +const char *orig_exec, +int type, +symbol_actor actor, +void *arg) +{ +GElf_Ehdr ehdr; +size_t shstrndx, orig_exec_dir_len; +GElf_Shdr shdr; +Elf_Scn *dbg_link_scn; +Elf_Data *dbg_link_scn_data; +char *dbg_link, *dbg_bin, *last_slash; +int symbols_count; + +/* First try to find the .gnu_debuglink section in the binary. */ +if (gelf_getehdr (elf, &ehdr) ==NULL ) +return 0 ; +if (elf_getshdrstrndx (elf, &shstrndx) !=0 ) +return 0 ; + + dbg_link_scn =elf_section_by_name (elf, &ehdr, &shdr, dbg_link_name); +if (dbg_link_scn ==NULL ) +return 0 ; + +/* Debug link section found, read of the content (only get the first +string, no checksum checking atm). This is debug binary file name. */ + dbg_link_scn_data =elf_getdata (dbg_link_scn,NULL ); +if (dbg_link_scn_data ==NULL || + dbg_link_scn_data->d_size <=0 || + dbg_link_scn_data->d_buf ==NULL ) +return 0 ; + +/* Now compose debug executable name */ + dbg_link = (char *)(dbg_link_scn_data->d_buf); + dbg_bin =malloc (strlen (dbg_bin_dir) +1 + +strlen (orig_exec) +1 + +strlen (dbg_link) +1 ); +if (!dbg_bin) +return 0 ; + + orig_exec_dir_len =PATH_MAX ; + last_slash =strrchr (orig_exec,'/' ); +if (last_slash !=NULL ) + orig_exec_dir_len = last_slash - orig_exec; + +sprintf (dbg_bin,"%s/%.*s/%s" , + dbg_bin_dir, (int )orig_exec_dir_len, orig_exec, dbg_link); + +/* Retry symbol seach with the debug binary */ + symbols_count =parse_dso_symbols (dbg_bin, type, actor, arg); + +free (dbg_bin); + +return symbols_count; +} + +int parse_dso_symbols (const char *exec,int type,symbol_actor actor,void *arg) +{ +int symbols_count =0 ; +Elf *elf; +int fd; + +if (elf_version (EV_CURRENT ) ==EV_NONE ) +return -1 ; + + fd =open (exec,O_RDONLY ); +if (fd <0 ) +return -1 ; + + elf =elf_begin (fd,ELF_C_READ ,NULL ); +if (elf) { +switch (type) { +case FIND_SYMBOL : + symbols_count =dso_symbols (elf, actor, arg); +if (symbols_count !=0 ) +break ; +/* If no symbols found, try in the debuglink binary. */ + symbols_count =dso_follow_debuglink (elf, + exec, + type, + actor, + arg); +break ; +case FIND_STAPSDT_NOTE : + symbols_count =dso_sdt_notes (elf, actor, arg); +break ; + } + +elf_end (elf); + } + +close (fd); +return symbols_count; +}tools/ktap/userspace/kp_symbol.h new file +50
-0@@ -0,0 +1,50 @@ +/* +* symbol.h - extract symbols from DSO. +* +* This file is part of ktap by Jovi Zhangwei. +* +* Copyright (C) 2013 Azat Khuzhin <[email protected]>. +* +* ktap is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* ktap is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. +*/ + + +#define FIND_SYMBOL 1 +#define FIND_STAPSDT_NOTE 2 + +#ifndef NO_LIBELF + +#include <gelf.h> +#include <sys/queue.h> + +typedef GElf_Addr vaddr_t ; +typedef int (*symbol_actor )(const char *name,vaddr_t addr,void *arg); + +/** +* Parse all DSO symbols/sdt notes and all for every of them +* an actor. +* +* @exec - path to DSO +* @type - see FIND_* +* @symbol_actor - actor to call (callback) +* @arg - argument for @actor +* +* @return +* If there have errors, return negative value; +* No symbols found, return 0; +* Otherwise return number of dso symbols found +*/ +int +parse_dso_symbols (const char *exec,int type,symbol_actor actor,void *arg); +#endif
Signature · 4 lines
Masami HIRAMATSU IT Management Research Dept. Linux Technology Center Hitachi, Ltd., Yokohama Research Laboratory E-mail: [email protected]
PATCH 20/28 ktap: add userspace/kp_parse_events.c
In reply to Jovi ZhangweiFunction 'kp_parse_events' parse event string passed by user, and return events description structure, it covers:
1). tracepoint
Search tracepoint name through '/sys/kernel/debug/tracing/events/', and get event id.
2). kprobe
Search symbol name through '/proc/kallsyms', then write event to '/sys/kernel/debug/tracing/kprobe_events', and read events id.
3). uprobe
Search symbol name through libelf, then write event to '/sys/kernel/debug/tracing/uprobe_events', then read events id.
4). SDT
Same as uprobe.
All events id will assembly ktap_eventdesc_t structure, and finially pass to 'kdebug.trace_by_id' function.
--- tools/ktap/userspace/kp_parse_events.c | 798 +++++++++++++++++++++++++++++++++ 1 file changed, 798 insertions(+) create mode 100644 tools/ktap/userspace/kp_parse_events.c
tools/ktap/userspace/kp_parse_events.c new file +798 -0
@@ -0,0 +1,798 @@ +/* +* parse_events.c - ktap events parser +* +* This file is part of ktap by Jovi Zhangwei. +* +* Copyright (C) 2012-2014 Jovi Zhangwei <[email protected]>. +* +* ktap is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* ktap is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <dirent.h> +#include <fcntl.h> +#include <ctype.h> + +#include "../include/ktap_types.h" +#include "../include/ktap_bc.h" +#include "kp_symbol.h" +#include "kp_util.h" + +#define TRACING_EVENTS_DIR "/sys/kernel/debug/tracing/events" + +static u8 *idmap; +static int idmap_size =1024 ;/* set init size */ +static int id_nr; + +static int idmap_init (void ) +{ + idmap =malloc (idmap_size); +if (!idmap) +return -1 ; + +memset (idmap,0 , idmap_size); +return 0 ; +} + +static void idmap_free (void ) +{ + id_nr =0 ; +free (idmap); +} + +static inline int idmap_is_set (int id) +{ +return idmap[id /8 ] & (1 << (id %8 )); +} + +static void idmap_set (int id) +{ +if (id >= idmap_size *8 ) { +int newsize = id +100 ;/* allocate extra 800 id */ + idmap =realloc (idmap, newsize); +memset (idmap + idmap_size,0 , newsize - idmap_size); + idmap_size = newsize; + } + +if (!idmap_is_set (id)) + id_nr++; + + idmap[id /8 ] = idmap[id /8 ] | (1 << (id %8 )); +} + +static void idmap_clear (int id) +{ +if (!idmap_is_set (id)) +return ; + + id_nr--; + idmap[id /8 ] = idmap[id /8 ] & ~ (1 << (id %8 )); +} + +static int idmap_get_max_id (void ) +{ +return idmap_size *8 ; +} + +static int *get_id_array () +{ +int *id_array; +int i, j =0 ; + + id_array =malloc (sizeof (int ) * id_nr); +if (!id_array) +return NULL ; + +for (i =0 ; i <idmap_get_max_id (); i++) { +if (idmap_is_set (i)) + id_array[j++] = i; + } + +return id_array; +} + +static int add_event (char *evtid_path) +{ +char id_buf[24 ]; +int id, fd; + + fd =open (evtid_path,O_RDONLY ); +if (fd <0 ) { +/* +* some tracepoint doesn't have id file, like ftrace, +* return success in here, and don't print error. +*/ +verbose_printf ("warning: cannot open file %s\n" , evtid_path); +return 0 ; + } + +if (read (fd, id_buf,sizeof (id_buf)) <0 ) { +fprintf (stderr,"read file error %s\n" , evtid_path); +close (fd); +return -1 ; + } + + id =atoll (id_buf); + +idmap_set (id); + +close (fd); +return 0 ; +} + +static int add_tracepoint (const char *sys_name,const char *evt_name) +{ +char evtid_path[PATH_MAX ] = {0 }; + +snprintf (evtid_path,PATH_MAX ,"%s/%s/%s/id" ,TRACING_EVENTS_DIR , + sys_name, evt_name); +return add_event (evtid_path); +} + +static int parse_events_add_tracepoint (char *sys,char *event) +{ +process_available_tracepoints (sys, event, add_tracepoint); +return 0 ; +} + +enum { +KPROBE_EVENT , +UPROBE_EVENT , +}; + +struct probe_list { +struct probe_list *next; +int type; +char event[64 ]; +}; + +static struct probe_list *probe_list_head;/* for cleanup resources */ + +/* +* Some symbol format cannot write to uprobe_events in debugfs, like: +* symbol "check_one_fd.part.0" in glibc. +* For those symbols, we change the format to: +* "check_one_fd.part.0" -> "check_one_fd_part_0" +*/ +static char *format_symbol_name (const char *old_symbol) +{ +char *new_name =strdup (old_symbol); +char *name = new_name; +int changed =0 ; + +if (!isalpha (*name) && *name !='_' ) { + *name ='_' ; + changed =1 ; + } + +while (*++name !='\0' ) { +if (!isalpha (*name) && !isdigit (*name) && *name !='_' ) { + *name ='_' ; + changed =1 ; +continue ; + } + } + +if (changed) +fprintf (stderr, +"Warning: symbol \"%s\" transformed to event \"%s\"\n" , + old_symbol, new_name); + +/* this is a good name */ +return new_name; +} + + +#define KPROBE_EVENTS_PATH "/sys/kernel/debug/tracing/kprobe_events" + +/** +* @return 0 on success, otherwise -1 +*/ +static int +write_kprobe_event (int fd,int ret_probe,const char *symbol, +unsigned long start,char *fetch_args) +{ +char probe_event[128 ] = {0 }; +char event[64 ] = {0 }; +struct probe_list *pl; +char event_id_path[128 ] = {0 }; +char *symbol_name; +int id_fd, ret; + +/* In case some symbols cannot write to uprobe_events debugfs file */ + symbol_name =format_symbol_name (symbol); + +if (!fetch_args) + fetch_args =" " ; + +if (ret_probe) { +snprintf (event,64 ,"ktap_kprobes_%d/ret_%s" , +getpid (), symbol_name); +/* Return probe point must be a symbol */ +snprintf (probe_event,128 ,"r:%s %s %s" , + event, symbol, fetch_args); + }else { +snprintf (event,64 ,"ktap_kprobes_%d/%s" , +getpid (), symbol_name); +snprintf (probe_event,128 ,"p:%s 0x%lx %s" , + event, start, fetch_args); + } + +sprintf (event_id_path,"/sys/kernel/debug/tracing/events/%s/id" , event); +/* if event id already exist, then don't write to kprobes_event again */ + id_fd =open (event_id_path,O_RDONLY ); +if (id_fd >0 ) { +close (id_fd); + +/* remember add event id to ids_array */ + ret =add_event (event_id_path); +if (ret) + goto error; + + goto out; + } + +verbose_printf ("write kprobe event %s\n" , probe_event); + +if (write (fd, probe_event,strlen (probe_event)) <=0 ) { +fprintf (stderr,"Cannot write %s to %s\n" , probe_event, +KPROBE_EVENTS_PATH ); + goto error; + } + +/* add to cleanup list */ + pl =malloc (sizeof (struct probe_list )); +if (!pl) + goto error; + + pl->type =KPROBE_EVENT ; + pl->next = probe_list_head; +memcpy (pl->event, event,64 ); + probe_list_head = pl; + + ret =add_event (event_id_path); +if (ret <0 ) + goto error; + + out: +free (symbol_name); +return 0 ; + + error: +free (symbol_name); +return -1 ; +} + +static unsigned long kprobes_text_start; +static unsigned long kprobes_text_end; + +static void init_kprobe_prohibited_area (void ) +{ +static int once =0 ; + +if (once >0 ) +return ; + + once =1 ; + kprobes_text_start =find_kernel_symbol ("__kprobes_text_start" ); + kprobes_text_end =find_kernel_symbol ("__kprobes_text_end" ); +} + +static int check_kprobe_addr_prohibited (unsigned long addr) +{ +if (addr >= kprobes_text_start && addr <= kprobes_text_end) +return -1 ; + +return 0 ; +} + +struct probe_cb_base { +int fd; +int ret_probe; +const char *event; +char *binary; +char *symbol; +char *fetch_args; +}; + +static int kprobe_symbol_actor (void *arg,const char *name,char type, +unsigned long start) +{ +struct probe_cb_base *base = (struct probe_cb_base *)arg; + +/* only can probe text function */ +if (type !='t' && type !='T' ) +return -1 ; + +if (!strglobmatch (name, base->symbol)) +return -1 ; + +if (check_kprobe_addr_prohibited (start)) +return -1 ; + +/* ignore reture code of write debugfs */ +write_kprobe_event (base->fd, base->ret_probe, name, start, + base->fetch_args); + +return 0 ;/* success */ +} + +static int parse_events_add_kprobe (char *event) +{ +char *symbol, *end; +struct probe_cb_base base; +int fd, ret; + + fd =open (KPROBE_EVENTS_PATH ,O_WRONLY ); +if (fd <0 ) { +fprintf (stderr,"Cannot open %s\n" ,KPROBE_EVENTS_PATH ); +return -1 ; + } + + end =strpbrk (event,"% " ); +if (end) + symbol =strndup (event, end - event); +else + symbol =strdup (event); + + base.fd = fd; + base.ret_probe = !!strstr (event,"%return" ); + base.symbol = symbol; + base.fetch_args =strchr (event,' ' ); + +init_kprobe_prohibited_area (); + + ret =kallsyms_parse (&base, kprobe_symbol_actor); +if (ret <=0 ) { +fprintf (stderr,"cannot parse symbol \"%s\"\n" , symbol); + ret =-1 ; + }else { + ret =0 ; + } + +free (symbol); +close (fd); + +return ret; +} + +#define UPROBE_EVENTS_PATH "/sys/kernel/debug/tracing/uprobe_events" + +/** +* @return 0 on success, otherwise -1 +*/ +static int +write_uprobe_event (int fd,int ret_probe,const char *binary, +const char *symbol,unsigned long addr, +char *fetch_args) +{ +char probe_event[128 ] = {0 }; +char event[64 ] = {0 }; +struct probe_list *pl; +char event_id_path[128 ] = {0 }; +char *symbol_name; +int id_fd, ret; + +/* In case some symbols cannot write to uprobe_events debugfs file */ + symbol_name =format_symbol_name (symbol); + +if (!fetch_args) + fetch_args =" " ; + +if (ret_probe) { +snprintf (event,64 ,"ktap_uprobes_%d/ret_%s" , +getpid (), symbol_name); +snprintf (probe_event,128 ,"r:%s %s:0x%lx %s" , + event, binary, addr, fetch_args); + }else { +snprintf (event,64 ,"ktap_uprobes_%d/%s" , +getpid (), symbol_name); +snprintf (probe_event,128 ,"p:%s %s:0x%lx %s" , + event, binary, addr, fetch_args); + } + +sprintf (event_id_path,"/sys/kernel/debug/tracing/events/%s/id" , event); +/* if event id already exist, then don't write to uprobes_event again */ + id_fd =open (event_id_path,O_RDONLY ); +if (id_fd >0 ) { +close (id_fd); + +/* remember add event id to ids_array */ + ret =add_event (event_id_path); +if (ret) + goto error; + + goto out; + } + +verbose_printf ("write uprobe event %s\n" , probe_event); + +if (write (fd, probe_event,strlen (probe_event)) <=0 ) { +fprintf (stderr,"Cannot write %s to %s\n" , probe_event, +UPROBE_EVENTS_PATH ); + goto error; + } + +/* add to cleanup list */ + pl =malloc (sizeof (struct probe_list )); +if (!pl) + goto error; + + pl->type =UPROBE_EVENT ; + pl->next = probe_list_head; +memcpy (pl->event, event,64 ); + probe_list_head = pl; + + ret =add_event (event_id_path); +if (ret <0 ) + goto error; + + out: +free (symbol_name); +return 0 ; + + error: +free (symbol_name); +return -1 ; +} + +/** +* TODO: avoid copy-paste stuff +* +* @return 1 on success, otherwise 0 +*/ +#ifdef NO_LIBELF +static int parse_events_resolve_symbol (int fd,char *event,int type) +{ +char *colon, *binary, *fetch_args; +unsigned long symbol_address; + + colon =strchr (event,':' ); +if (!colon) +return -1 ; + + symbol_address =strtol (colon +1 /* skip ":" */ ,NULL ,0 ); + + fetch_args =strchr (event,' ' ); + +/** +* We already have address, no need in resolving. +*/ +if (symbol_address) { +int ret; + + binary =strndup (event, colon - event); + ret =write_uprobe_event (fd, !!strstr (event,"%return" ), binary, +"NULL" , symbol_address, fetch_args); +free (binary); +return ret; + } + +fprintf (stderr,"error: cannot resolve event \"%s\" without libelf, " +"please recompile ktap with NO_LIBELF disabled\n" , + event); +exit (EXIT_FAILURE ); +return -1 ; +} + +#else +static int uprobe_symbol_actor (const char *name,vaddr_t addr,void *arg) +{ +struct probe_cb_base *base = (struct probe_cb_base *)arg; +int ret; + +if (!strglobmatch (name, base->symbol)) +return 0 ; + +verbose_printf ("uprobe: binary: \"%s\" symbol \"%s\" " +"resolved to 0x%lx\n" , + base->binary, base->symbol, (unsigned long )addr); + + ret =write_uprobe_event (base->fd, base->ret_probe, base->binary, + name, addr, base->fetch_args); +if (ret) +return ret; + +return 0 ; +} + +static int parse_events_resolve_symbol (int fd,char *event,int type) +{ +char *colon, *end; +vaddr_t symbol_address; +int ret; +struct probe_cb_base base = { + .fd = fd, + .event = event + }; + + colon =strchr (event,':' ); +if (!colon) +return 0 ; + + base.ret_probe = !!strstr (event,"%return" ); + symbol_address =strtol (colon +1 /* skip ":" */ ,NULL ,0 ); + base.binary =strndup (event, colon - event); + + base.fetch_args =strchr (event,' ' ); + +/* +* We already have address, no need in resolving. +*/ +if (symbol_address) { +int ret; + ret =write_uprobe_event (fd, base.ret_probe, base.binary, +"NULL" , symbol_address, + base.fetch_args); +free (base.binary); +return ret; + } + + end =strpbrk (event,"% " ); +if (end) + base.symbol =strndup (colon +1 , end -1 - colon); +else + base.symbol =strdup (colon +1 ); + + ret =parse_dso_symbols (base.binary, type, uprobe_symbol_actor, + (void *)&base); +if (!ret) { +fprintf (stderr,"error: cannot find symbol %s in binary %s\n" , + base.symbol, base.binary); + ret =-1 ; + }else if (ret >0 ) { +/* no error found when parse symbols */ + ret =0 ; + } + +free (base.binary); +free (base.symbol); + +return ret; +} +#endif + +static int parse_events_add_uprobe (char *old_event,int type) +{ +int ret; +int fd; + + fd =open (UPROBE_EVENTS_PATH ,O_WRONLY ); +if (fd <0 ) { +fprintf (stderr,"Cannot open %s\n" ,UPROBE_EVENTS_PATH ); +return -1 ; + } + + ret =parse_events_resolve_symbol (fd, old_event, type); + +close (fd); +return ret; +} + +static int parse_events_add_probe (char *old_event) +{ +char *separator; + + separator =strchr (old_event,':' ); +if (!separator || (separator == old_event)) +return parse_events_add_kprobe (old_event); +else +return parse_events_add_uprobe (old_event,FIND_SYMBOL ); +} + +static int parse_events_add_sdt (char *old_event) +{ +return parse_events_add_uprobe (old_event,FIND_STAPSDT_NOTE ); +} + +static void strim (char *s) +{ +size_t size; +char *end; + + size =strlen (s); +if (!size) +return ; + + end = s + size -1 ; +while (end >= s &&isspace (*end)) + end--; + + *(end +1 ) ='\0' ; +} + +static int get_sys_event_filter_str (char *start, +char **sys,char **event,char **filter) +{ +char *separator, *separator2, *ptr, *end; + +while (*start ==' ' ) + start++; + +/* find sys */ + separator =strchr (start,':' ); +if (!separator || (separator == start)) { +return -1 ; + } + + ptr =malloc (separator - start +1 ); +if (!ptr) +return -1 ; + +strncpy (ptr, start, separator - start); + ptr[separator - start] ='\0' ; + +strim (ptr); + *sys = ptr; + +if (!strcmp (*sys,"probe" ) && (*(separator +1 ) =='/' )) { +/* it's uprobe event */ + separator2 =strchr (separator +1 ,':' ); +if (!separator2) +return -1 ; + }else + separator2 = separator; + +/* find filter */ + end = start +strlen (start); +while (*--end ==' ' ) { + } + +if (*end =='/' ) { +char *filter_start; + + filter_start =strchr (separator2,'/' ); +if (filter_start == end) +return -1 ; + + ptr =malloc (end - filter_start); +if (!ptr) +return -1 ; + +memcpy (ptr, filter_start +1 , end - filter_start -1 ); + ptr[end - filter_start -1 ] ='\0' ; + + *filter = ptr; + + end = filter_start; + }else { + *filter =NULL ; + end++; + } + +/* find event */ + ptr =malloc (end - separator); +if (!ptr) +return -1 ; + +memcpy (ptr, separator +1 , end - separator -1 ); + ptr[end - separator -1 ] ='\0' ; + +strim (ptr); + *event = ptr; + +return 0 ; +} + +static char *get_next_eventdef (char *str) +{ +char *separator; + + separator =strchr (str,',' ); +if (!separator) +return str +strlen (str); + + *separator ='\0' ; +return separator +1 ; +} + +ktap_eventdesc_t *kp_parse_events (const char *eventdef) +{ +char *str =strdup (eventdef); +char *sys, *event, *filter, *next; +ktap_eventdesc_t *evdef_info; +int ret; + +idmap_init (); + + parse_next_eventdef: + next =get_next_eventdef (str); + +if (get_sys_event_filter_str (str, &sys, &event, &filter)) + goto error; + +verbose_printf ("parse_eventdef: sys[%s], event[%s], filter[%s]\n" , + sys, event, filter); + +if (!strcmp (sys,"probe" )) + ret =parse_events_add_probe (event); +else if (!strcmp (sys,"sdt" )) + ret =parse_events_add_sdt (event); +else + ret =parse_events_add_tracepoint (sys, event); + +if (ret) + goto error; + +/* don't trace ftrace:function when all tracepoints enabled */ +if (!strcmp (sys,"*" )) +idmap_clear (1 ); + + +if (filter && *next !='\0' ) { +fprintf (stderr,"Error: eventdef only can append one filter\n" ); + goto error; + } + + str = next; +if (*next !='\0' ) + goto parse_next_eventdef; + + evdef_info =malloc (sizeof (*evdef_info)); +if (!evdef_info) + goto error; + + evdef_info->nr = id_nr; + evdef_info->id_arr =get_id_array (); + evdef_info->filter = filter; + +idmap_free (); +return evdef_info; + error: +idmap_free (); +cleanup_event_resources (); +return NULL ; +} + +void cleanup_event_resources (void ) +{ +struct probe_list *pl; +const char *path; +char probe_event[128 ] = {0 }; +int fd, ret; + +for (pl = probe_list_head; pl; pl = pl->next) { +if (pl->type ==KPROBE_EVENT ) + path =KPROBE_EVENTS_PATH ; +else if (pl->type ==UPROBE_EVENT ) + path =UPROBE_EVENTS_PATH ; +else { +fprintf (stderr,"Cannot cleanup event type %d\n" , + pl->type); +continue ; + } + +snprintf (probe_event,128 ,"-:%s" , pl->event); + + fd =open (path,O_WRONLY ); +if (fd <0 ) { +fprintf (stderr,"Cannot open %s\n" ,UPROBE_EVENTS_PATH ); +continue ; + } + + ret =write (fd, probe_event,strlen (probe_event)); +if (ret <=0 ) { +fprintf (stderr,"Cannot write %s to %s\n" , probe_event, + path); +close (fd); +continue ; + } + +close (fd); + } +} +
Signature · 1 line
1.8.1.4
PATCH 21/28 ktap: add userspace/kp_reader.c
In reply to Jovi ZhangweiThis is ktap ring buffer consumer, a thread poll content from '/sys/kernel/debug/ktap/trace_pipe_%pid' debugfs file.
--- tools/ktap/userspace/kp_reader.c | 106 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 tools/ktap/userspace/kp_reader.c
tools/ktap/userspace/kp_reader.c new file +106 -0
@@ -0,0 +1,106 @@ +/* +* reader.c - ring buffer reader in userspace +* +* This file is part of ktap by Jovi Zhangwei. +* +* Copyright (C) 2012-2014 Jovi Zhangwei <[email protected]>. +* +* ktap is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* ktap is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include <sys/poll.h> +#include <sys/signal.h> +#include <fcntl.h> +#include <pthread.h> + +#define MAX_BUFLEN 131072 +#define PATH_MAX 128 + +#define handle_error (str) do { perror(str); exit(-1); } while(0) + +void sigfunc (int signo) +{ +/* should not not reach here */ +} + +static void block_sigint () +{ +sigset_t mask; + +sigemptyset (&mask); +sigaddset (&mask,SIGINT ); + +pthread_sigmask (SIG_BLOCK , &mask,NULL ); +} + +static void *reader_thread (void *data) +{ +char buf[MAX_BUFLEN ]; +char filename[PATH_MAX ]; +const char *output = data; +int failed =0 , fd, out_fd, len; + +block_sigint (); + +if (output) { + out_fd =open (output,O_CREAT |O_WRONLY |O_TRUNC , +S_IRUSR |S_IWUSR ); +if (out_fd <0 ) { +fprintf (stderr,"Cannot open output file %s\n" , output); +return NULL ; + } + }else + out_fd =1 ; + +sprintf (filename,"/sys/kernel/debug/ktap/trace_pipe_%d" ,getpid ()); + + open_again: + fd =open (filename,O_RDONLY ); +if (fd <0 ) { +usleep (10000 ); + +if (failed++ ==10 ) { +fprintf (stderr,"Cannot open file %s\n" , filename); +return NULL ; + } + goto open_again; + } + +while ((len =read (fd, buf,sizeof (buf))) >0 ) +write (out_fd, buf, len); + +close (fd); +close (out_fd); + +return NULL ; +} + +int kp_create_reader (const char *output) +{ +pthread_t reader; + +signal (SIGINT , sigfunc); + +if (pthread_create (&reader,NULL , reader_thread, (void *)output) <0 ) +handle_error ("pthread_create reader_thread failed\n" ); + +return 0 ; +} +
Signature · 1 line
1.8.1.4
PATCH 22/28 ktap: add userspace/kp_bcwrite.c
In reply to Jovi ZhangweiBytecode writer and listing.
[root@localhost ktap]# ./ktap -b -e 'var s = {} s["key"] = 1'
-- BYTECODE -- (command line):0-1
0001 TNEW 0 0 0002 KSHORT 1 1 0003 TSETS 1 0 0 ; "key" 0004 RET0 0 1
--- tools/ktap/userspace/kp_bcwrite.c | 375 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 375 insertions(+) create mode 100644 tools/ktap/userspace/kp_bcwrite.c
tools/ktap/userspace/kp_bcwrite.c new file +375 -0
@@ -0,0 +1,375 @@ +/* +* Bytecode writer +* +* This file is part of ktap by Jovi Zhangwei. +* +* Copyright (C) 2012-2014 Jovi Zhangwei <[email protected]>. +* +* Copyright (C) 1994-2013 Lua.org, PUC-Rio. +* - The part of code in this file is copied from lua initially. +* - lua's MIT license is compatible with GPL. +* +* ktap is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* ktap is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "../include/ktap_types.h" +#include "kp_util.h" + +/* Context for bytecode writer. */ +typedef struct BCWriteCtx { +SBuf sb;/* Output buffer. */ +ktap_proto_t *pt;/* Root prototype. */ +ktap_writer wfunc;/* Writer callback. */ +void *wdata;/* Writer callback data. */ +int strip;/* Strip debug info. */ +int status;/* Status from writer callback. */ +}BCWriteCtx ; + + +static char *bcwrite_uint32 (char *p,uint32_t v) +{ +memcpy (p, &v,sizeof (uint32_t )); + p +=sizeof (uint32_t ); +return p; +} + +/* -- Bytecode writer ----------------------------------------------------- */ + +/* Write a single constant key/value of a template table. */ +static void bcwrite_ktabk (BCWriteCtx *ctx,const ktap_val_t *o,int narrow) +{ +char *p =kp_buf_more (&ctx->sb,1 +10 ); +if (is_string (o)) { +const ktap_str_t *str =rawtsvalue (o); +int len = str->len; + p =kp_buf_more (&ctx->sb,5 +len); + p =bcwrite_uint32 (p,BCDUMP_KTAB_STR +len); + p =kp_buf_wmem (p,getstr (str), len); + }else if (is_number (o)) { + p =bcwrite_uint32 (p,BCDUMP_KTAB_NUM ); + p =kp_buf_wmem (p, &nvalue (o),sizeof (ktap_number)); + }else { +kp_assert (tvispri (o)); + p =bcwrite_uint32 (p,BCDUMP_KTAB_NIL +~itype (o)); + } +setsbufP (&ctx->sb, p); +} + +/* Write a template table. */ +static void bcwrite_ktab (BCWriteCtx *ctx,char *p,const ktap_tab_t *t) +{ +int narray =0 , nhash =0 ; +if (t->asize >0 ) {/* Determine max. length of array part. */ +ptrdiff_t i; +ktap_val_t *array = t->array; +for (i = (ptrdiff_t )t->asize-1 ; i >=0 ; i--) +if (!is_nil (&array[i])) +break ; + narray = (int )(i+1 ); + } +if (t->hmask >0 ) {/* Count number of used hash slots. */ +int i, hmask = t->hmask; +ktap_node_t *node = t->node; +for (i =0 ; i <= hmask; i++) + nhash += !is_nil (&node[i].val); + } +/* Write number of array slots and hash slots. */ + p =bcwrite_uint32 (p, narray); + p =bcwrite_uint32 (p, nhash); +setsbufP (&ctx->sb, p); +if (narray) {/* Write array entries (may contain nil). */ +int i; +ktap_val_t *o = t->array; +for (i =0 ; i < narray; i++, o++) +bcwrite_ktabk (ctx, o,1 ); + } +if (nhash) {/* Write hash entries. */ +int i = nhash; +ktap_node_t *node = t->node + t->hmask; +for (;; node--) +if (!is_nil (&node->val)) { +bcwrite_ktabk (ctx, &node->key,0 ); +bcwrite_ktabk (ctx, &node->val,1 ); +if (--i ==0 ) +break ; + } + } +} + +/* Write GC constants of a prototype. */ +static void bcwrite_kgc (BCWriteCtx *ctx,ktap_proto_t *pt) +{ +int i, sizekgc = pt->sizekgc; +ktap_obj_t **kr = (ktap_obj_t **)pt->k - (ptrdiff_t )sizekgc; + +for (i =0 ; i < sizekgc; i++, kr++) { +ktap_obj_t *o = *kr; +int tp, need =1 ; +char *p; + +/* Determine constant type and needed size. */ +if (o->gch.gct == ~KTAP_TSTR ) { + tp =BCDUMP_KGC_STR + ((ktap_str_t *)o)->len; + need =5 + ((ktap_str_t *)o)->len; + }else if (o->gch.gct == ~KTAP_TPROTO ) { +kp_assert ((pt->flags &PROTO_CHILD )); + tp =BCDUMP_KGC_CHILD ; + }else { +kp_assert (o->gch.gct == ~KTAP_TTAB ); + tp =BCDUMP_KGC_TAB ; + need =1 +2 *5 ; + } + +/* Write constant type. */ + p =kp_buf_more (&ctx->sb, need); + p =bcwrite_uint32 (p, tp); +/* Write constant data (if any). */ +if (tp >=BCDUMP_KGC_STR ) { + p =kp_buf_wmem (p,getstr ((ktap_str_t *)o), + ((ktap_str_t *)o)->len); + }else if (tp ==BCDUMP_KGC_TAB ) { +bcwrite_ktab (ctx, p, (ktap_tab_t *)o); +continue ; + } +setsbufP (&ctx->sb, p); + } +} + +/* Write number constants of a prototype. */ +static void bcwrite_knum (BCWriteCtx *ctx,ktap_proto_t *pt) +{ +int i, sizekn = pt->sizekn; +const ktap_val_t *o = (ktap_val_t *)pt->k; +char *p =kp_buf_more (&ctx->sb,10 *sizekn); + +for (i =0 ; i < sizekn; i++, o++) { +if (is_number (o)) + p =kp_buf_wmem (p, &nvalue (o),sizeof (ktap_number)); + } +setsbufP (&ctx->sb, p); +} + +/* Write bytecode instructions. */ +static char *bcwrite_bytecode (BCWriteCtx *ctx,char *p,ktap_proto_t *pt) +{ +int nbc = pt->sizebc-1 ;/* Omit the [JI]FUNC* header. */ + + p =kp_buf_wmem (p,proto_bc (pt)+1 , nbc*(int )sizeof (BCIns)); +return p; +} + +/* Write prototype. */ +static void bcwrite_proto (BCWriteCtx *ctx,ktap_proto_t *pt) +{ +int sizedbg =0 ; +char *p; + +/* Recursively write children of prototype. */ +if (pt->flags &PROTO_CHILD ) { +ptrdiff_t i, n = pt->sizekgc; +ktap_obj_t **kr = (ktap_obj_t **)pt->k -1 ; +for (i =0 ; i < n; i++, kr--) { +ktap_obj_t *o = *kr; +if (o->gch.gct == ~KTAP_TPROTO ) +bcwrite_proto (ctx, (ktap_proto_t *)o); + } + } + +/* Start writing the prototype info to a buffer. */ + p =kp_buf_need (&ctx->sb, +5 +4 +6 *5 +(pt->sizebc-1 )*(int )sizeof (BCIns)+pt->sizeuv*2 ); + p +=4 ;/* Leave room for final size. */ + +/* Write prototype header. */ + *p++ = (pt->flags & (PROTO_CHILD |PROTO_VARARG |PROTO_FFI )); + *p++ = pt->numparams; + *p++ = pt->framesize; + *p++ = pt->sizeuv; + p =bcwrite_uint32 (p, pt->sizekgc); + p =bcwrite_uint32 (p, pt->sizekn); + p =bcwrite_uint32 (p, pt->sizebc-1 ); +if (!ctx->strip) { +if (proto_lineinfo (pt)) + sizedbg = pt->sizept - + (int )((char *)proto_lineinfo (pt) - (char *)pt); + p =bcwrite_uint32 (p, sizedbg); +if (sizedbg) { + p =bcwrite_uint32 (p, pt->firstline); + p =bcwrite_uint32 (p, pt->numline); + } + } + +/* Write bytecode instructions and upvalue refs. */ + p =bcwrite_bytecode (ctx, p, pt); + p =kp_buf_wmem (p,proto_uv (pt), pt->sizeuv*2 ); +setsbufP (&ctx->sb, p); + +/* Write constants. */ +bcwrite_kgc (ctx, pt); +bcwrite_knum (ctx, pt); + +/* Write debug info, if not stripped. */ +if (sizedbg) { + p =kp_buf_more (&ctx->sb, sizedbg); + p =kp_buf_wmem (p,proto_lineinfo (pt), sizedbg); +setsbufP (&ctx->sb, p); + } + +/* Pass buffer to writer function. */ +if (ctx->status ==0 ) { +int n =sbuflen (&ctx->sb) -4 ; +char *q =sbufB (&ctx->sb); + p =bcwrite_uint32 (q, n);/* Fill in final size. */ +kp_assert (p ==sbufB (&ctx->sb) +4 ); + ctx->status = ctx->wfunc (q, n +4 , ctx->wdata); + } +} + +/* Write header of bytecode dump. */ +static void bcwrite_header (BCWriteCtx *ctx) +{ +ktap_str_t *chunkname =proto_chunkname (ctx->pt); +const char *name =getstr (chunkname); +int len = chunkname->len; +char *p =kp_buf_need (&ctx->sb,5 +5 +len); + *p++ =BCDUMP_HEAD1 ; + *p++ =BCDUMP_HEAD2 ; + *p++ =BCDUMP_HEAD3 ; + *p++ =BCDUMP_VERSION ; + *p++ = (ctx->strip ?BCDUMP_F_STRIP :0 ) + (KP_BE ?BCDUMP_F_BE :0 ); + +if (!ctx->strip) { + p =bcwrite_uint32 (p, len); + p =kp_buf_wmem (p, name, len); + } + ctx->status = ctx->wfunc (sbufB (&ctx->sb), + (int )(p -sbufB (&ctx->sb)), ctx->wdata); +} + +/* Write footer of bytecode dump. */ +static void bcwrite_footer (BCWriteCtx *ctx) +{ +if (ctx->status ==0 ) { +uint8_t zero =0 ; + ctx->status = ctx->wfunc (&zero,1 , ctx->wdata); + } +} + +/* Write bytecode for a prototype. */ +int kp_bcwrite (ktap_proto_t *pt,ktap_writer writer,void *data,int strip) +{ +BCWriteCtx ctx; + + ctx.pt = pt; + ctx.wfunc = writer; + ctx.wdata = data; + ctx.strip = strip; + ctx.status =0 ; + +kp_buf_init (&ctx.sb); +kp_buf_need (&ctx.sb,1024 );/* Avoids resize for most prototypes. */ +bcwrite_header (&ctx); +bcwrite_proto (&ctx, ctx.pt); +bcwrite_footer (&ctx); + +kp_buf_free (&ctx.sb); +return ctx.status; +} + +/* -- Bytecode dump ----------------------------------------------------- */ + +static const char *const bc_names[] = { +#define BCNAME(name, ma, mb, mc, mt) #name, +BCDEF (BCNAME ) +#undef BCNAME + NULL +}; + +static const uint16_t bc_mode[] = { +BCDEF (BCMODE ) +}; + +static void dump_bytecode (ktap_proto_t *pt) +{ +int nbc = pt->sizebc -1 ;/* Omit the FUNC* header. */ +BCIns *ins =proto_bc (pt) +1 ; +ktap_obj_t **kbase = pt->k; +int i; + +printf ("-- BYTECODE -- %s:%d-%d\n" ,getstr (pt->chunkname), + pt->firstline, pt->firstline + pt->numline); + +for (i =0 ; i < nbc; i++, ins++) { +int op =bc_op (*ins); + +printf ("%04d\t%s" , i +1 , bc_names[op]); + +printf ("\t%d" ,bc_a (*ins)); +if (bcmode_b (op) != BCMnone) +printf ("\t%d" ,bc_b (*ins)); + +if (bcmode_hasd (op)) +printf ("\t%d" ,bc_d (*ins)); +else +printf ("\t%d" ,bc_c (*ins)); + +if (bcmode_b (op) == BCMstr ||bcmode_c (op) == BCMstr) { +printf ("\t ; " ); +if (bcmode_d (op) == BCMstr) { +int idx = ~bc_d (*ins); +printf ("\"%s\"" ,getstr ((ktap_str_t *)kbase[idx])); + } + } +printf ("\n" ); + } +} + +static int function_nr =0 ; + +void kp_dump_proto (ktap_proto_t *pt) +{ +printf ("\n----------------------------------------------------\n" ); +printf ("function proto %d:\n" , function_nr++); +printf ("numparams: %d\n" , pt->numparams); +printf ("framesize: %d\n" , pt->framesize); +printf ("sizebc: %d\n" , pt->sizebc); +printf ("sizekgc: %d\n" , pt->sizekgc); +printf ("sizekn: %d\n" , pt->sizekn); +printf ("sizept: %d\n" , pt->sizept); +printf ("sizeuv: %d\n" , pt->sizeuv); +printf ("firstline: %d\n" , pt->firstline); +printf ("numline: %d\n" , pt->numline); + +printf ("has child proto: %d\n" , pt->flags &PROTO_CHILD ); +printf ("has vararg: %d\n" , pt->flags &PROTO_VARARG ); +printf ("has ILOOP: %d\n" , pt->flags &PROTO_ILOOP ); + +dump_bytecode (pt); + +/* Recursively dump children of prototype. */ +if (pt->flags &PROTO_CHILD ) { +ptrdiff_t i, n = pt->sizekgc; +ktap_obj_t **kr = (ktap_obj_t **)pt->k -1 ; +for (i =0 ; i < n; i++, kr--) { +ktap_obj_t *o = *kr; +if (o->gch.gct == ~KTAP_TPROTO ) +kp_dump_proto ((ktap_proto_t *)o); + } + } +} +
Signature · 1 line
1.8.1.4
PATCH 23/28 ktap: add userspace/kp_util.c
In reply to Jovi Zhangwei--- tools/ktap/userspace/kp_util.c | 646 +++++++++++++++++++++++++++++++++++++++++ tools/ktap/userspace/kp_util.h | 120 ++++++++ 2 files changed, 766 insertions(+) create mode 100644 tools/ktap/userspace/kp_util.c create mode 100644 tools/ktap/userspace/kp_util.h
tools/ktap/userspace/kp_util.c new file +646 -0
@@ -0,0 +1,646 @@ +/* +* util.c +* +* This file is part of ktap by Jovi Zhangwei. +* +* Copyright (C) 2012-2014 Jovi Zhangwei <[email protected]>. +* +* Adapted from luajit and lua interpreter. +* Copyright (C) 2005-2014 Mike Pall. +* Copyright (C) 1994-2008 Lua.org, PUC-Rio. +* +* ktap is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2, as published by the Free Software Foundation. +* +* ktap is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program; if not, write to the Free Software Foundation, Inc., +* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <math.h> +#include <ctype.h> +#include "../include/ktap_types.h" +#include "../include/ktap_bc.h" +#include "kp_util.h" + +/* Error message strings. */ +const char *kp_err_allmsg = +#define ERRDEF(name, msg) msg "\0" +#include "../include/ktap_errmsg.h" +; + +const uint8_t kp_char_bits[257 ] = { +0 , +1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,3 ,3 ,3 ,3 ,3 ,1 ,1 , +1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 , +2 ,4 ,4 ,4 ,4 ,4 ,4 ,4 ,4 ,4 ,4 ,4 ,4 ,4 ,4 ,4 , +152 ,152 ,152 ,152 ,152 ,152 ,152 ,152 ,152 ,152 ,4 ,4 ,4 ,4 ,4 ,4 , +4 ,176 ,176 ,176 ,176 ,176 ,176 ,160 ,160 ,160 ,160 ,160 ,160 ,160 ,160 ,160 , +160 ,160 ,160 ,160 ,160 ,160 ,160 ,160 ,160 ,160 ,160 ,4 ,4 ,4 ,4 ,132 , +4 ,208 ,208 ,208 ,208 ,208 ,208 ,192 ,192 ,192 ,192 ,192 ,192 ,192 ,192 ,192 , +192 ,192 ,192 ,192 ,192 ,192 ,192 ,192 ,192 ,192 ,192 ,4 ,4 ,4 ,4 ,1 , +128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 , +128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 , +128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 , +128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 , +128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 , +128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 , +128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 , +128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 ,128 +}; + +void kp_buf_init (SBuf *sb) +{ + sb->b = (char *)malloc (200 ); + sb->p =NULL ; + sb->e = sb->b +200 ; +} + +void kp_buf_reset (SBuf *sb) +{ + sb->p = sb->b; +} + +void kp_buf_free (SBuf *sb) +{ +free (sbufB (sb)); +} + +char *kp_buf_more (SBuf *sb,int sz) +{ +char *b; +int old_len =sbuflen (sb); + +if (sz >sbufleft (sb)) { + b =realloc (sbufB (sb),sbuflen (sb) *2 ); + sb->b = b; + sb->p = b + old_len; + sb->e = b + old_len *2 ; + } + +return sbufP (sb); +} + +char *kp_buf_need (SBuf *sb,int sz) +{ +char *b; +int old_len =sbuflen (sb); + +if (sz >sbufsz (sb)) { + b =realloc (sbufB (sb), sz); + sb->b = b; + sb->p = b + old_len; + sb->e = b + sz; + } + +return sbufB (sb); +} + +char *kp_buf_wmem (char *p,const void *q,int len) +{ +return (char *)memcpy (p, q, len) + len; +} + +void kp_buf_putb (SBuf *sb,int c) +{ +char *p =kp_buf_more (sb,1 ); + *p++ = (char )c; +setsbufP (sb, p); +} + +ktap_str_t *kp_buf_str (SBuf *sb) +{ +return kp_str_new (sbufB (sb),sbuflen (sb)); +} + +/* Write ULEB128 to buffer. */ +char *strfmt_wuleb128 (char *p,uint32_t v) +{ +for (; v >=0x80 ; v >>=7 ) + *p++ = (char )((v &0x7f ) |0x80 ); + *p++ = (char )v; +return p; +} + +void kp_err_lex (ktap_str_t *src,const char *tok,BCLine line, +ErrMsg em,va_list argp) +{ +const char *msg; + + msg =kp_sprintfv (err2msg (em), argp); + msg =kp_sprintf ("%s:%d: %s" ,getstr (src), line, msg); +if (tok) + msg =kp_sprintf (err2msg (KP_ERR_XNEAR ), msg, tok); +fprintf (stderr,"%s: %s\n" ,err2msg (KP_ERR_XSYNTAX ), msg); +exit (-1 ); +} + +void *kp_reallocv (void *block,size_t osize,size_t nsize) +{ +return realloc (block, nsize); +} + +static const ktap_val_t kp_niltv = { {NULL }, {KTAP_TNIL } } ; +#define niltv (&kp_niltv) + +#define gnode (t,i) (&(t)->node[i]) +#define gkey (n) (&(n)->key) +#define gval (n) (&(n)->val) + +const ktap_val_t *kp_tab_get (ktap_tab_t *t,const ktap_val_t *key) +{ +int i; + +switch (itype (key)) { +case KTAP_TNIL : +return niltv; +case KTAP_TNUM : +for (i =0 ; i <= t->hmask; i++) { +ktap_val_t *v =gkey (gnode (t, i)); +if (is_number (v) &&nvalue (key) ==nvalue (v)) +return gval (gnode (t, i)); + } +break ; +case KTAP_TSTR : +for (i =0 ; i <= t->hmask; i++) { +ktap_val_t *v =gkey (gnode (t, i)); +if (is_string (v) && (rawtsvalue (key) ==rawtsvalue (v))) +return gval (gnode (t, i)); + } +break ; +default : +for (i =0 ; i <= t->hmask; i++) { +if (kp_obj_equal (key,gkey (gnode (t, i)))) +return gval (gnode (t, i)); + } +break ; + } + +return niltv; +} + +const ktap_val_t *kp_tab_getstr (ktap_tab_t *t,const ktap_str_t *ts) +{ +int i; + +for (i =0 ; i <= t->hmask; i++) { +ktap_val_t *v =gkey (gnode (t, i)); +if (is_string (v) && (ts ==rawtsvalue (v))) +return gval (gnode (t, i)); + } + +return niltv; +} + +void kp_tab_setvalue (ktap_tab_t *t,const ktap_val_t *key,ktap_val_t *val) +{ +const ktap_val_t *v =kp_tab_get (t, key); + +if (v != niltv) { +set_obj ((ktap_val_t *)v, val); + }else { +if (t->freetop == t->node) { +int size = (t->hmask +1 ) *sizeof (ktap_node_t); + t->node =realloc (t->node, size *2 ); +memset (t->node + t->hmask +1 ,0 , size); + t->freetop = t->node + (t->hmask +1 ) *2 ; + t->hmask = (t->hmask +1 ) *2 -1 ; + } + +ktap_node_t *n = --t->freetop; +set_obj (gkey (n), key); +set_obj (gval (n), val); + } +} + +ktap_val_t *kp_tab_set (ktap_tab_t *t,const ktap_val_t *key) +{ +const ktap_val_t *v =kp_tab_get (t, key); + +if (v != niltv) { +return (ktap_val_t *)v; + }else { +if (t->freetop == t->node) { +int size = (t->hmask +1 ) *sizeof (ktap_node_t); + t->node =realloc (t->node, size *2 ); +memset (t->node + t->hmask +1 ,0 , size); + t->freetop = t->node + (t->hmask +1 ) *2 ; + t->hmask = (t->hmask +1 ) *2 -1 ; + } + +ktap_node_t *n = --t->freetop; +set_obj (gkey (n), key); +set_nil (gval (n)); +return gval (n); + } +} + + +ktap_tab_t *kp_tab_new (void ) +{ +int hsize, i; + +ktap_tab_t *t =malloc (sizeof (ktap_tab_t)); + t->gct = ~KTAP_TTAB ; + hsize =1024 ; + t->hmask = hsize -1 ; + t->node = (ktap_node_t *)malloc (hsize *sizeof (ktap_node_t)); + t->freetop = &t->node[hsize]; + t->asize =0 ; + +for (i =0 ; i <= t->hmask; i++) { +set_nil (&t->node[i].val); +set_nil (&t->node[i].key); + } +return t; +} + +/* simple interned string array, use hash table in future */ +static ktap_str_t **strtab; +static int strtab_size =1000 ;/* initial size */ +static int strtab_nr; + +void kp_str_resize (void ) +{ +int size = strtab_size *sizeof (ktap_str_t *); + + strtab =malloc (size); +if (!strtab) { +fprintf (stderr,"cannot allocate stringtable\n" ); +exit (-1 ); + } + +memset (strtab,0 , size); + strtab_nr =0 ; +} + +static ktap_str_t *stringtable_search (const char *str,int len) +{ +int i; + +for (i =0 ; i < strtab_nr; i++) { +ktap_str_t *s = strtab[i]; +if ((len == s->len) && !memcmp (str,getstr (s), len)) +return s; + } + +return NULL ; +} + +static void stringtable_insert (ktap_str_t *ts) +{ + strtab[strtab_nr++] = ts; + +if (strtab_nr == strtab_size) { +int size = strtab_size *sizeof (ktap_str_t *); + strtab =realloc (strtab, size *2 ); +memset (strtab + strtab_size,0 , size); + strtab_size *=2 ; + } +} + +static ktap_str_t *createstrobj (const char *str,size_t l) +{ +ktap_str_t *ts; +size_t totalsize;/* total size of TString object */ + + totalsize =sizeof (ktap_str_t) + ((l +1 ) *sizeof (char )); + ts = (ktap_str_t *)malloc (totalsize); + ts->gct = ~KTAP_TSTR ; + ts->len = l; + ts->reserved =0 ; + ts->extra =0 ; +memcpy (ts +1 , str, l *sizeof (char )); + ((char *)(ts +1 ))[l] ='\0' ;/* ending 0 */ +return ts; +} + +ktap_str_t *kp_str_new (const char *str,size_t l) +{ +ktap_str_t *ts =stringtable_search (str, l); + +if (ts) +return ts; + + ts =createstrobj (str, l); +stringtable_insert (ts); +return ts; +} + +ktap_str_t *kp_str_newz (const char *str) +{ +return kp_str_new (str,strlen (str)); +} + +/* +* todo: memory leak here +*/ +char *kp_sprintf (const char *fmt, ...) +{ +char *msg =malloc (128 ); + +va_list argp; +va_start (argp, fmt); +vsprintf (msg, fmt, argp); +va_end (argp); +return msg; +} + +const char *kp_sprintfv (const char *fmt,va_list argp) +{ +char *msg =malloc (128 ); + +vsprintf (msg, fmt, argp); +return msg; +} + +int kp_obj_equal (const ktap_val_t *t1,const ktap_val_t *t2) +{ +switch (itype (t1)) { +case KTAP_TNIL : +return 1 ; +case KTAP_TNUM : +return nvalue (t1) ==nvalue (t2); +case KTAP_TTRUE : +case KTAP_TFALSE : +return itype (t1) ==itype (t2); +case KTAP_TLIGHTUD : +return pvalue (t1) ==pvalue (t2); +case KTAP_TFUNC : +return fvalue (t1) ==fvalue (t2); +case KTAP_TSTR : +return rawtsvalue (t1) ==rawtsvalue (t2); +default : +return gcvalue (t1) ==gcvalue (t2); + } + +return 0 ; +} + +/* +* strglobmatch is copyed from perf(linux/tools/perf/util/string.c) +*/ + +/* Character class matching */ +static bool __match_charclass (const char *pat,char c,const char **npat) +{ +bool complement = false, ret = true; + +if (*pat =='!' ) { + complement = true; + pat++; + } +if (*pat++ == c)/* First character is special */ + goto end; + +while (*pat && *pat !=']' ) {/* Matching */ +if (*pat =='-' && *(pat +1 ) !=']' ) {/* Range */ +if (*(pat -1 ) <= c && c <= *(pat +1 )) + goto end; +if (*(pat -1 ) > *(pat +1 )) + goto error; + pat +=2 ; + }else if (*pat++ == c) + goto end; + } +if (!*pat) + goto error; + ret = false; + +end: +while (*pat && *pat !=']' )/* Searching closing */ + pat++; +if (!*pat) + goto error; + *npat = pat +1 ; +return complement ? !ret : ret; + +error: +return false; +} + +/* Glob/lazy pattern matching */ +static bool __match_glob (const char *str,const char *pat,bool ignore_space) +{ +while (*str && *pat && *pat !='*' ) { +if (ignore_space) { +/* Ignore spaces for lazy matching */ +if (isspace (*str)) { + str++; +continue ; + } +if (isspace (*pat)) { + pat++; +continue ; + } + } +if (*pat =='?' ) {/* Matches any single character */ + str++; + pat++; +continue ; + }else if (*pat =='[' )/* Character classes/Ranges */ +if (__match_charclass (pat +1 , *str, &pat)) { + str++; +continue ; + }else +return false; +else if (*pat =='\\' )/* Escaped char match as normal char */ + pat++; +if (*str++ != *pat++) +return false; + } +/* Check wild card */ +if (*pat =='*' ) { +while (*pat =='*' ) + pat++; +if (!*pat)/* Tail wild card matches all */ +return true; +while (*str) +if (__match_glob (str++, pat, ignore_space)) +return true; + } +return !*str && !*pat; +} + +/** +* strglobmatch - glob expression pattern matching +* @str: the target string to match +* @pat: the pattern string to match +* +* This returns true if the @str matches @pat. @pat can includes wildcards +* ('*','?') and character classes ([CHARS], complementation and ranges are +* also supported). Also, this supports escape character ('\') to use special +* characters as normal character. +* +* Note: if @pat syntax is broken, this always returns false. +*/ +bool strglobmatch (const char *str,const char *pat) +{ +return __match_glob (str, pat, false); +} + +#define handle_error (str) do { perror(str); exit(-1); } while(0) + +#define KALLSYMS_PATH "/proc/kallsyms" +/* +* read kernel symbol from /proc/kallsyms +*/ +int kallsyms_parse (void *arg, +int (*process_symbol)(void *arg,const char *name, +char type,unsigned long start)) +{ +FILE *file; +char *line =NULL ; +int ret =0 ; +int found =0 ; + + file =fopen (KALLSYMS_PATH ,"r" ); +if (file ==NULL ) +handle_error ("open " KALLSYMS_PATH " failed" ); + +while (!feof (file)) { +char *symbol_addr, *symbol_name; +char symbol_type; +unsigned long start; +int line_len; +size_t n; + + line_len =getline (&line, &n, file); +if (line_len <0 || !line) +break ; + + line[--line_len] ='\0' ;/* \n */ + + symbol_addr =strtok (line," \t" ); + start =strtoul (symbol_addr,NULL ,16 ); + + symbol_type = *strtok (NULL ," \t" ); + symbol_name =strtok (NULL ," \t" ); + + ret =process_symbol (arg, symbol_name, symbol_type, start); +if (!ret) + found =1 ; + } + +free (line); +fclose (file); + +return found; +} + +struct ksym_addr_t { +const char *name; +unsigned long addr; +}; + +static int symbol_cmp (void *arg,const char *name,char type, +unsigned long start) +{ +struct ksym_addr_t *base = arg; + +if (strcmp (base->name, name) ==0 ) { + base->addr = start; +return 1 ; + } + +return 0 ; +} + +unsigned long find_kernel_symbol (const char *symbol) +{ +int ret; +struct ksym_addr_t arg = { + .name = symbol, + .addr =0 + }; + + ret =kallsyms_parse (&arg, symbol_cmp); +if (ret <0 || arg.addr ==0 ) { +fprintf (stderr,"cannot read kernel symbol \"%s\" in %s\n" , + symbol,KALLSYMS_PATH ); +exit (EXIT_FAILURE ); + } + +return arg.addr; +} + + +#define AVAILABLE_EVENTS_PATH "/sys/kernel/debug/tracing/available_events" + +void list_available_events (const char *match) +{ +FILE *file; +char *line =NULL ; + + file =fopen (AVAILABLE_EVENTS_PATH ,"r" ); +if (file ==NULL ) +handle_error ("open " AVAILABLE_EVENTS_PATH " failed" ); + +while (!feof (file)) { +int line_len; +size_t n; + + line_len =getline (&line, &n, file); +if (line_len <0 || !line) +break ; + +if (!match ||strglobmatch (line, match)) +printf ("%s" , line); + } + +free (line); +fclose (file); +} + +void process_available_tracepoints (const char *sys,const char *event, +int (*process)(const char *sys, +const char *event)) +{ +char *line =NULL ; +FILE *file; +char str[128 ] = {0 }; + +/* add '\n' into tail */ +snprintf (str,64 ,"%s:%s\n" , sys, event); + + file =fopen (AVAILABLE_EVENTS_PATH ,"r" ); +if (file ==NULL ) +handle_error ("open " AVAILABLE_EVENTS_PATH " failed" ); + +while (!feof (file)) { +int line_len; +size_t n; + + line_len =getline (&line, &n, file); +if (line_len <0 || !line) +break ; + +if (strglobmatch (line, str)) { +char match_sys[64 ] = {0 }; +char match_event[64 ] = {0 }; +char *sep; + + sep =strchr (line,':' ); +memcpy (match_sys, line, sep - line); +memcpy (match_event, sep +1 , + line_len - (sep - line) -2 ); + +if (process (match_sys, match_event)) +break ; + } + } + +free (line); +fclose (file); +} +
tools/ktap/userspace/kp_util.h new file +120 -0
120 lines not on this page · raw message
Signature · 1 line
1.8.1.4
PATCH 24/28 ktap: add Makefile
In reply to Jovi ZhangweiMakefile for ktap kernel module and userspace binary.
--- tools/ktap/Makefile | 189 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 tools/ktap/Makefile
tools/ktap/Makefile new file +189 -0
@@ -0,0 +1,189 @@ + +# +# Define NO_LIBELF if you do not want libelf dependency (e.g. cross-builds) +# (this will also disable resolve resolving symbols in DSO functionality) +# +# Define amalg to enable amalgamation build, This compiles the ktapvm as +# one huge C file and allows GCC to generate faster and shorter code. Alas, +# this requires lots of memory during the build. +# Recommend to use amalgmation build as default. +amalg = 1 + +# Do not instrument the tracer itself: +ifdef CONFIG_FUNCTION_TRACER +ORIG_CFLAGS := $(KBUILD_CFLAGS) +KBUILD_CFLAGS = $(subst -pg,,$(ORIG_CFLAGS)) +endif + +all: mod ktap + +INC = include +RUNTIME = runtime + +KTAP_LIBS = -lpthread + +LIB_OBJS += $(RUNTIME)/lib_base.o $(RUNTIME)/lib_kdebug.o \ + $(RUNTIME)/lib_timer.o $(RUNTIME)/lib_ansi.o \ + $(RUNTIME)/lib_table.o $(RUNTIME)/lib_net.o + +ifndef amalg +RUNTIME_OBJS += $(RUNTIME)/ktap.o $(RUNTIME)/kp_bcread.o $(RUNTIME)/kp_obj.o \ + $(RUNTIME)/kp_str.o $(RUNTIME)/kp_mempool.o \ + $(RUNTIME)/kp_tab.o $(RUNTIME)/kp_vm.o \ + $(RUNTIME)/kp_transport.o $(RUNTIME)/kp_events.o $(LIB_OBJS) +else +RUNTIME_OBJS += $(RUNTIME)/amalg.o +endif + +obj-m += ktapvm.o +ktapvm-y := $(RUNTIME_OBJS) + +KVERSION ?= $(shell uname -r) +KERNEL_SRC ?= /lib/modules/$(KVERSION)/build +PWD := $(shell pwd) +mod: + $(MAKE) -C $(KERNEL_SRC) M=$(PWD) modules + +modules_install: + $(MAKE) -C $(KERNEL_SRC) M=$(PWD) modules_install + +KTAPC_CFLAGS = -Wall -O2 + + +# try-cc +# Usage: option = $(call try-cc, source-to-build, cc-options, msg) +ifneq ($(V),1) +TRY_CC_OUTPUT= > /dev/null 2>&1 +endif +TRY_CC_MSG=echo " CHK $(3)" 1>&2; + +try-cc = $(shell sh -c \ + 'TMP="/tmp/.$$$$"; \ + $(TRY_CC_MSG) \ + echo "$(1)" | \ + $(CC) -x c - $(2) -o "$$TMP" $(TRY_CC_OUTPUT) && echo y; \ + rm -f "$$TMP"') + + +define SOURCE_LIBELF +#include <libelf.h> + +int main(void) +{ + Elf *elf = elf_begin(0, ELF_C_READ, 0); + return (long)elf; +} +endef + +FLAGS_LIBELF = -lelf + +ifdef NO_LIBELF + KTAPC_CFLAGS += -DNO_LIBELF +else +ifneq ($(call try-cc,$(SOURCE_LIBELF),$(FLAGS_LIBELF),libelf),y) + $(warning No libelf found, disables symbol resolving, please install elfutils-libelf-devel/libelf-dev); + NO_LIBELF := 1 + KTAPC_CFLAGS += -DNO_LIBELF +else + KTAP_LIBS += -lelf +endif +endif + +UDIR = userspace + +$(UDIR)/kp_main.o: $(UDIR)/kp_main.c $(INC)/* KTAP-CFLAGS + $(QUIET_CC)$(CC) $(DEBUGINFO_FLAG) $(KTAPC_CFLAGS) -o $@ -c $< +$(UDIR)/kp_lex.o: $(UDIR)/kp_lex.c $(INC)/* KTAP-CFLAGS + $(QUIET_CC)$(CC) $(DEBUGINFO_FLAG) $(KTAPC_CFLAGS) -o $@ -c $< +$(UDIR)/kp_parse.o: $(UDIR)/kp_parse.c $(INC)/* KTAP-CFLAGS + $(QUIET_CC)$(CC) $(DEBUGINFO_FLAG) $(KTAPC_CFLAGS) -o $@ -c $< +$(UDIR)/kp_bcwrite.o: $(UDIR)/kp_bcwrite.c $(INC)/* KTAP-CFLAGS + $(QUIET_CC)$(CC) $(DEBUGINFO_FLAG) $(KTAPC_CFLAGS) -o $@ -c $< +$(UDIR)/kp_reader.o: $(UDIR)/kp_reader.c $(INC)/* KTAP-CFLAGS + $(QUIET_CC)$(CC) $(DEBUGINFO_FLAG) $(KTAPC_CFLAGS) -o $@ -c $< +$(UDIR)/kp_util.o: $(UDIR)/kp_util.c $(INC)/* KTAP-CFLAGS + $(QUIET_CC)$(CC) $(DEBUGINFO_FLAG) $(KTAPC_CFLAGS) -o $@ -c $< +$(UDIR)/kp_parse_events.o: $(UDIR)/kp_parse_events.c $(INC)/* KTAP-CFLAGS + $(QUIET_CC)$(CC) $(DEBUGINFO_FLAG) $(KTAPC_CFLAGS) -o $@ -c $< +ifndef NO_LIBELF +$(UDIR)/kp_symbol.o: $(UDIR)/kp_symbol.c KTAP-CFLAGS + $(QUIET_CC)$(CC) $(DEBUGINFO_FLAG) $(KTAPC_CFLAGS) -o $@ -c $< +endif + +KTAPOBJS = +KTAPOBJS += $(UDIR)/kp_main.o +KTAPOBJS += $(UDIR)/kp_lex.o +KTAPOBJS += $(UDIR)/kp_parse.o +KTAPOBJS += $(UDIR)/kp_bcwrite.o +KTAPOBJS += $(UDIR)/kp_reader.o +KTAPOBJS += $(UDIR)/kp_util.o +KTAPOBJS += $(UDIR)/kp_parse_events.o +ifndef NO_LIBELF +KTAPOBJS += $(UDIR)/kp_symbol.o +endif + +ktap: $(KTAPOBJS) KTAP-CFLAGS + $(QUIET_LINK)$(CC) $(KTAPC_CFLAGS) -o $@ $(KTAPOBJS) $(KTAP_LIBS) + +KMISC := /lib/modules/$(KVERSION)/ktapvm/ + +install: mod ktap + make modules_install ktapvm.ko + install -c ktap /usr/bin/ + mkdir -p ~/.vim/ftdetect + mkdir -p ~/.vim/syntax + cp vim/ftdetect/ktap.vim ~/.vim/ftdetect/ + cp vim/syntax/ktap.vim ~/.vim/syntax/ + +load: + insmod ktapvm.ko + +unload: + rmmod ktapvm + +reload: + make unload; make load + +test: FORCE + #start testing + prove -j4 -r test/ + +clean: + $(MAKE) -C $(KERNEL_SRC) M=$(PWD) clean + $(RM) ktap KTAP-CFLAGS + + +PHONY += FORCE +FORCE: + +TRACK_FLAGS = KTAP +ifdef amalg +TRACK_FLAGS += AMALG +endif +ifdef NO_LIBELF +TRACK_FLAGS += NO_LIBELF +endif + +KTAP-CFLAGS: FORCE + @FLAGS='$(TRACK_FLAGS)'; \ + if test x"$$FLAGS" != x"`cat KTAP-CFLAGS 2>/dev/null`" ; then \ + echo "$$FLAGS" >KTAP-CFLAGS; \ + fi + +#generate tags/etags/cscope index for editor. +define all_sources + (find . -name '*.[ch]' -print) +endef + +.PHONY: tags +tags: + $(all_sources) | xargs ctags + +.PHONY: etags +etags: + $(all_sources) | xargs etags + +.PHONY: cscope +cscope: + $(all_sources) > cscope.files + cscope -k -b
Signature · 1 line
1.8.1.4
PATCH 25/28 ktap: add Kconfig
In reply to Jovi Zhangwei--- tools/ktap/Kconfig | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 tools/ktap/Kconfig
tools/ktap/Kconfig new file +21 -0
@@ -0,0 +1,21 @@ +config KTAP + tristate "a programable dynamic tracing tool for Linux" + depends on PERF_EVENTS && EVENT_TRACING + default n + help + ktap is a new script-based dynamic tracing tool for Linux, + it uses a scripting language and lets users trace the + Linux kernel dynamically. ktap is designed to give + operational insights with interoperability that allow + users to tune, troubleshoot and extend kernel and application. + It's similar with Linux Systemtap and Solaris Dtrace. + + ktap have different design principles from Linux mainstream + dynamic tracing language in that it's based on bytecode, + so it doesn't depend upon GCC, doesn't require compiling + kernel module for each script, safe to use in production + environment, fulfilling the embedded ecosystem's tracing needs. + + See ktap tutorial for more information: + http://www.ktap.org/doc/tutorial.html +
Signature · 1 line
1.8.1.4
PATCH 26/28 ktap: add testsuite
In reply to Jovi Zhangweiktap testsuite is based on perl-prove framwork.
More info can read from test/README.
The test framework is contributed by Yichun Zhang (agentzh)
ktap run the test suite in parallel defaultly:
prove -j4 -r test/
There also have several benchmark script to compare performance between ktap with stap.
The benchmark shows that:
1). ktap number computation and comparsion overhead is bigger than stap,
nearly 10+%.
2). Perf backend tracing overhead is bigger than raw tracepoint/kprobe.
3). ktap table operation overhead is smaller than stap, nearly 10+%.
(This benchmark result only tell the data in my box)
--- tools/ktap/test/README | 69 ++++ tools/ktap/test/arithmetic.t | 109 ++++++ tools/ktap/test/benchmark/cmp_neq.sh | 158 +++++++++ tools/ktap/test/benchmark/cmp_profile.sh | 54 +++ tools/ktap/test/benchmark/cmp_table.sh | 112 +++++++ tools/ktap/test/benchmark/sembench.c | 556 +++++++++++++++++++++++++++++++ tools/ktap/test/benchmark/test.sh | 128 +++++++ tools/ktap/test/cli-arg.t | 25 ++ tools/ktap/test/concat.t | 21 ++ tools/ktap/test/count.t | 25 ++ tools/ktap/test/deadloop.t | 37 ++ tools/ktap/test/fibonacci.t | 42 +++ tools/ktap/test/function.t | 78 +++++ tools/ktap/test/if.t | 32 ++ tools/ktap/test/kprobe.t | 82 +++++ tools/ktap/test/kretprobe.t | 35 ++ tools/ktap/test/len.t | 27 ++ tools/ktap/test/lib/Test/ktap.pm | 128 +++++++ tools/ktap/test/looping.t | 46 +++ tools/ktap/test/one-liner.t | 48 +++ tools/ktap/test/pairs.t | 52 +++ tools/ktap/test/stack_overflow.t | 22 ++ tools/ktap/test/syntax-err.t | 19 ++ tools/ktap/test/table.t | 81 +++++ tools/ktap/test/time.t | 59 ++++ tools/ktap/test/timer.t | 65 ++++ tools/ktap/test/tracepoint.t | 53 +++ tools/ktap/test/util/reindex | 61 ++++ tools/ktap/test/zerodivide.t | 21 ++ 29 files changed, 2245 insertions(+) create mode 100644 tools/ktap/test/README create mode 100644 tools/ktap/test/arithmetic.t create mode 100644 tools/ktap/test/benchmark/cmp_neq.sh create mode 100644 tools/ktap/test/benchmark/cmp_profile.sh create mode 100644 tools/ktap/test/benchmark/cmp_table.sh create mode 100644 tools/ktap/test/benchmark/sembench.c create mode 100644 tools/ktap/test/benchmark/test.sh create mode 100644 tools/ktap/test/cli-arg.t create mode 100644 tools/ktap/test/concat.t create mode 100644 tools/ktap/test/count.t create mode 100644 tools/ktap/test/deadloop.t create mode 100644 tools/ktap/test/fibonacci.t create mode 100644 tools/ktap/test/function.t create mode 100644 tools/ktap/test/if.t create mode 100644 tools/ktap/test/kprobe.t create mode 100644 tools/ktap/test/kretprobe.t create mode 100644 tools/ktap/test/len.t create mode 100644 tools/ktap/test/lib/Test/ktap.pm create mode 100644 tools/ktap/test/looping.t create mode 100644 tools/ktap/test/one-liner.t create mode 100644 tools/ktap/test/pairs.t create mode 100644 tools/ktap/test/stack_overflow.t create mode 100644 tools/ktap/test/syntax-err.t create mode 100644 tools/ktap/test/table.t create mode 100644 tools/ktap/test/time.t create mode 100644 tools/ktap/test/timer.t create mode 100644 tools/ktap/test/tracepoint.t create mode 100755 tools/ktap/test/util/reindex create mode 100644 tools/ktap/test/zerodivide.t
tools/ktap/test/README new file +69 -0
@@ -0,0 +1,69 @@ +This directory contains the test suite for ktap. + +Prerequisites +------------- + +One needs to install perl and CPAN modules Test::Base and IPC::Run +before running the tests. After perl is installed, the "cpan" utility +can be used to install the CPAN modules required: + + cpan Test::Base IPC::Run + +Alternatively you can just install the pre-built binary packages +provided by your Linux distribution vendor. For example, on Fedora, you +just need to run + + yum install perl-Test-Base perl-IPC-Run + +Running tests +------------- + +You are required to run the tests from the root directory of this project. + +You can run the whole test suite like this: + + prove -r test/ + +To utilize multiple CPU cores while running the tests, it is also +supported to spawn multiple processes to run the test files in parallel, +as in + + prove -j4 -r test/ + +Then 4 processes will be spawned to run the tests at the same time. + +To run individual .t test files, just specify their file paths +explicitly: + + prove test/cli-args.t test/one-liner.t + +If you just want to run an individual test case in a particular .t +file, then just add the line + + --- ONLY + +to the end of the test block you want to run and run that .t file +normally with the "prove" utility. + +Similarly, if you want to skip a particular test block, add the line + + --- SKIP + +to that test block. + +Test file formatting +-------------------- + +We do have a "reindex" tool to automatically re-format +the .t test files, so that you do not have to manually get the test +serial numbers exactly right, like "TEST 1: ", "TEST 2: " and etc, +nor manually keep 3 blank lines between adjacent test blocks. For +example, + + ./test/util/reindex test/cli-arg.t + +or re-format all the .t files: + + ./test/util/reindex test/*.t + +Always run this tool before committing your newly editted tests.
tools/ktap/test/arithmetic.t new file +109 -0
109 lines not on this page · raw message
tools/ktap/test/benchmark/cmp_neq.sh new file +158 -0
158 lines not on this page · raw message
tools/ktap/test/benchmark/cmp_profile.sh new file +54 -0
54 lines not on this page · raw message
tools/ktap/test/benchmark/cmp_table.sh new file +112 -0
112 lines not on this page · raw message
tools/ktap/test/benchmark/sembench.c new file +556 -0
556 lines not on this page · raw message
tools/ktap/test/benchmark/test.sh new file +128 -0
128 lines not on this page · raw message
tools/ktap/test/cli-arg.t new file +25 -0
@@ -0,0 +1,25 @@ +# vi: ft= et tw=4 sw=4 + +use lib 'test/lib'; +use Test::ktap 'no_plan'; + +run_tests(); + +__DATA__ + +=== TEST 1: sanity +--- args: 1 testing "2 3 4" +--- src +printf("arg 0: %s\n", arg[0]) +printf("arg 1: %d\n", arg[1]) +printf("arg 2: %s\n", arg[2]) +printf("arg 3: %s\n", arg[2]) + +--- out_like chop +^arg 0: /tmp/\S+\.kp +arg 1: 1 +arg 2: testing +arg 3: testing$ + +--- err +
tools/ktap/test/concat.t new file +21 -0
@@ -0,0 +1,21 @@ +# vi: ft= et tw=4 sw=4 + +use lib 'test/lib'; +use Test::ktap 'no_plan'; + +run_tests(); + +__DATA__ + +=== TEST 1: string concat +--- src +var a = "123" +var b = "456" + +print(a..b) + +--- out +123456 +--- err + +
tools/ktap/test/count.t new file +25 -0
@@ -0,0 +1,25 @@ +# vi: ft= et tw=4 sw=4 + +use lib 'test/lib'; +use Test::ktap 'no_plan'; + +run_tests(); + +__DATA__ + +=== TEST 1: count +--- src +var t = {} + +t["key"] += 1 +print(t["key"]) + +t["key"] += 1 +print(t["key"]) + +--- out +1 +2 +--- err + +
tools/ktap/test/deadloop.t new file +37 -0
@@ -0,0 +1,37 @@ +# vi: ft= et ts=4 + +use lib 'test/lib'; +use Test::ktap 'no_plan'; + +run_tests(); + +__DATA__ + +=== TEST 1: exit dead loop +--- src +tick-1s { + exit() +} + +tick-3s { + print("dead loop not exited") +} + +while (1) {} + +--- out_like +error: loop execute count exceed max limit(.*) +--- err + + + +=== TEST 2: dead loop killed by signal +--- src + +while (1) {} + +--- out_like +error: loop execute count exceed max limit(.*) + +--- err +
tools/ktap/test/fibonacci.t new file +42 -0
42 lines not on this page · raw message
tools/ktap/test/function.t new file +78 -0
78 lines not on this page · raw message
tools/ktap/test/if.t new file +32 -0
@@ -0,0 +1,32 @@ +# vi: ft= et tw=4 sw=4 + +use lib 'test/lib'; +use Test::ktap 'no_plan'; + +run_tests(); + +__DATA__ + +=== TEST 1: test if +--- src + +if (false) { + print("failed") +} + +if (nil) { + print("failed") +} + +# ktap only think false and nil is "real false", number 0 is true +# it's same as lua +# Might change it in future, to make similar with C +if (0) { + print("number 0 is true") +} + +--- out +number 0 is true +--- err + +
tools/ktap/test/kprobe.t new file +82 -0
82 lines not on this page · raw message
tools/ktap/test/kretprobe.t new file +35 -0
@@ -0,0 +1,35 @@ +# vi: ft= et ts=4 + +use lib 'test/lib'; +use Test::ktap 'no_plan'; + +run_tests(); + +__DATA__ + +=== TEST 1: kprobe +--- opts: -q +--- src + +var n = 0 +trace probe:schedule { + n = n + 1 +} + +# share same event id with previous one +trace probe:schedule { +} + +# test event filter +trace probe:do_sys_open dfd=%di filename=%si flags=%dx mode=%cx /dfd==1/ { } + +tick-1s { + print(n==0) + exit() +} + +--- out +false +--- err + +
tools/ktap/test/len.t new file +27 -0
@@ -0,0 +1,27 @@ +# vi: ft= et tw=4 sw=4 + +use lib 'test/lib'; +use Test::ktap 'no_plan'; + +run_tests(); + +__DATA__ + +=== TEST 1: len +--- src +var a = "123456789" + +print(len(a)) + +var b = {} +b[0] = 0 +b[1] = 1 +b["keys"] = "values" + +print(len(b)) + +--- out +9 +3 +--- err +
tools/ktap/test/lib/Test/ktap.pm new file +128 -0
128 lines not on this page · raw message
tools/ktap/test/looping.t new file +46 -0
46 lines not on this page · raw message
tools/ktap/test/one-liner.t new file +48 -0
48 lines not on this page · raw message
tools/ktap/test/pairs.t new file +52 -0
52 lines not on this page · raw message
tools/ktap/test/stack_overflow.t new file +22 -0
@@ -0,0 +1,22 @@ +# vi: ft= et tw=4 sw=4 + +use lib 'test/lib'; +use Test::ktap 'no_plan'; + +run_tests(); + +__DATA__ + +=== TEST 1: stack overflow +--- src +function f(a) { + return 1 + f(a+1) +} + +print(f(0)) + +--- out_like +(.*)stack overflow(.*) +--- err + +
tools/ktap/test/syntax-err.t new file +19 -0
@@ -0,0 +1,19 @@ +# vi: ft= et tw=4 sw=4 + +use lib 'test/lib'; +use Test::ktap 'no_plan'; + +run_tests(); + +__DATA__ + +=== TEST 1: bad assignment (unexpected eof) +--- src +a = + +--- out +--- err_like +unexpected symbol near '<eof>' + +--- ret: 255 +
tools/ktap/test/table.t new file +81 -0
81 lines not on this page · raw message
tools/ktap/test/time.t new file +59 -0
59 lines not on this page · raw message
tools/ktap/test/timer.t new file +65 -0
65 lines not on this page · raw message
tools/ktap/test/tracepoint.t new file +53 -0
53 lines not on this page · raw message
tools/ktap/test/util/reindex new file +61 -0
61 lines not on this page · raw message
tools/ktap/test/zerodivide.t new file +21 -0
@@ -0,0 +1,21 @@ +# vi: ft= et tw=4 sw=4 + +use lib 'test/lib'; +use Test::ktap 'no_plan'; + +run_tests(); + +__DATA__ + +=== TEST 1: zero divide +--- src + +var a = 1/0 +#should not go here +print("failed") + +--- out_like +(.*)divide 0(.*) +--- err + +
Signature · 1 line
1.8.1.4
PATCH 27/28 ktap: add vim syntax file
In reply to Jovi ZhangweiTo make ktap script looks more beautiful.
--- tools/ktap/vim/ftdetect/ktap.vim | 3 ++ tools/ktap/vim/syntax/ktap.vim | 106 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 tools/ktap/vim/ftdetect/ktap.vim create mode 100644 tools/ktap/vim/syntax/ktap.vim
tools/ktap/vim/ftdetect/ktap.vim new file +3 -0
@@ -0,0 +1,3 @@ +augroup filetype + au BufNewFile,BufRead *.kp set filetype=ktap +augroup end
tools/ktap/vim/syntax/ktap.vim new file +106 -0
106 lines not on this page · raw message
Signature · 1 line
1.8.1.4
PATCH 28/28 ktap: add COPYRIGHT file
In reply to Jovi Zhangweiktap is based on laujit and lua, so carry they copyright notices and MIT license in ktap tree.
--- tools/ktap/COPYRIGHT | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 tools/ktap/COPYRIGHT
tools/ktap/COPYRIGHT new file +63 -0
@@ -0,0 +1,63 @@ + +Copyright (C) 2012-2014, Jovi Zhangwei <[email protected]>. +All rights reserved. + +Licensed under the GPL License, Version 2.0 + +=============================================================================== + +* ktap code is based on luajit(compiler & bytecode), so carry luajit + copyright notices in below. + +LuaJIT -- a Just-In-Time Compiler for Lua. http://luajit.org/ + +Copyright (C) 2005-2014 Mike Pall. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +[ MIT license: http://www.opensource.org/licenses/mit-license.php ] + +=============================================================================== + +* Some ktap code is based on lua programming language initially, + so carry lua own copyright notices and license terms: + (lua's MIT license is compatible with GPL. + ktap can redistribute as GPL v2, without violate with lua license, + this was confirmed with official lua team) + +Copyright (C) 1994~2013 Lua.org, PUC-Rio. + +Permissiossion is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +
Signature · 1 line
1.8.1.4
Quoted 3 lines · Jovi Zhangwei
Hi All,
The following set of patches add ktap tracing tool.
Yeah!
Thanks for putting this patch set together, it looks good.
But, why hide the kernel portions of ktap in the tools/ directory? Is that ok with everyone else? I don't expect to see kernel code burried in that directory, so any kernel API changes that might happen, might not get propagated into that location.
thanks,
greg k-h
Quoted 5 lines · Greg Kroah-Hartman
Quoted 3 lines · Jovi Zhangwei
Hi All,
The following set of patches add ktap tracing tool.
Quoted 9 lines
Yeah!
Thanks for putting this patch set together, it looks good.
But, why hide the kernel portions of ktap in the
tools/directory? Is that ok with everyone else? I don't expect to see kernel code burried in that directory, so any kernel API changes that might happen, might not get propagated into that location.
Reasonable, I will try to put kernel module side into kernel/trace/ktap directory in next version.
There have some duplicated code with kernel/trace/*.c, then maybe we can integrate it if ktap location more closer with ftrace.
Thanks for this suggestion.
Jovi
Quoted 7 lines · Jovi Zhangwei
Hi All,
The following set of patches add ktap tracing tool.
ktap is a new script-based dynamic tracing tool for Linux. It uses a scripting language and lets the user trace system dynamically.
Quoted 9 lines
Highlights features:
* a simple but powerful scripting language
* register-based interpreter (heavily optimized) in Linux kernel
* small and lightweight
* not depend on the GCC toolchain for each script run
* easy to use in embedded environments without debugging info
* support for tracepoint, kprobe, uprobe, function trace, timer, and more
* supported in x86, ARM, PowerPC, MIPS
* safety in sandbox
I've asked this fundamental design question before but got no full
answer: how does ktap compare to the ongoing effort of improving the BPF scripting engine?
There's several efforts here that I'm aware of:
1) 64-bit BPF, integration with ftrace scripting, see this lkml thread:
[RFC PATCH v2 tip 0/7] 64-bit BPF insn set and tracing filters
2) better BPF integration with networking:
[PATCH net-next v3 8/9] net: filter: rework/optimize internal BPF interpreter's instruction set
Your patches introduce a separate bytecode interpreter in kernel/trace/ktap/ and that's overlapping with BPF.
Quoted 1 line
From a long term instrumentation code maintenance point of view the
last thing we want is several overlapping scripting engines.
Thanks,
Ingo
Hi Ingo,
Quoted 20 lines · Ingo Molnar
Quoted 7 lines · Jovi Zhangwei
Hi All,
The following set of patches add ktap tracing tool.
ktap is a new script-based dynamic tracing tool for Linux. It uses a scripting language and lets the user trace system dynamically.
Quoted 9 lines
Highlights features:
* a simple but powerful scripting language
* register-based interpreter (heavily optimized) in Linux kernel
* small and lightweight
* not depend on the GCC toolchain for each script run
* easy to use in embedded environments without debugging info
* support for tracepoint, kprobe, uprobe, function trace, timer, and more
* supported in x86, ARM, PowerPC, MIPS
* safety in sandbox
Quoted 6 lines
I've asked this fundamental design question before but got no full
answer: how does ktap compare to the ongoing effort of improving the BPF scripting engine?From long experiences of ktap development, what make me really
love ktap is:
1) Availability
ktap is only available tool to use in small embedded platform, stap and BPF both need GCC now, stap have its own language, so it's much better than BPF.
(IMO there may need several years to complete a skeleton of dynamic tracing script language, see stap and dtrace)
2) Simplicity
ktap is simplest dynamic scripting trace solution now in Linux world, compare with stap/dtrace/BPF.
a). It have simple syntax which make many people like it, it have b). It have simple associate array, make dynamic tracing powerful. c). It have a simple compiler which only have 87K in x86_64. d). It have a simple tracing syntax which constant with perf events.
3) Safety
ktap already delivered its safety to end user, many people use ktap in their dev lab to investigate problem.
But BPF need time to prove its safety, especially proved by end user, and IMO BPF safety check would be more complex if the runtime support more features as time goes.
4). Samples
Many people like those ktap samples, ktap shows the attractive by samples.
Even I so love ktap and would like share ktap values to everyone, but in technical point of view, I still agree with you that there should have unified scripting engines in kernel if that engine can service for many domains(like networking), but that solution should show its availability/ simplicity/safety firstly to user, not just proved by end user.
Dynamic tracing scripting environment should contains:
simple compiler, clean language syntax, fast script engine, associative array, aggregation, kstack, ustack, event management, ring buffer, samples, tapset/library, CTF, etc.
ktap already fixed most of these issues by its simple design, but BPF only have "script engine" part(its associative array still cannot vmalloc), which have long road before could use by end user.
ktap is not just bring a bytecode engine, it bring a complete simple dynamic tracing environment to end user, it bring clean language syntax, samples, flexible table, perf like event management, etc, those is the key part to end user, not bytecode engine, so if we can develop simple BPF compiler with similar ktap syntax in some day, then we can replace kp_lex.c/kp_parse.c/kp_vm.c, and there have zero reason why other parts cannot be shared(associative array, aggregation, kstack, ustack, event management, ring buffer, samples, tapset/library, CTF, etc).
So ktap is deviling a simple designed tracing script environment, but BPF just provide script engine which still not proved by end user in many facts(Availability/Simplicity/Safety).
Sorry for the length of this reply.
Thanks.
Jovi
Quoted 46 lines · Jovi Zhangwei
Hi Ingo,
Quoted 20 lines · Ingo Molnar
Quoted 7 lines · Jovi Zhangwei
Hi All,
The following set of patches add ktap tracing tool.
ktap is a new script-based dynamic tracing tool for Linux. It uses a scripting language and lets the user trace system dynamically.
Quoted 9 lines
Highlights features:
* a simple but powerful scripting language
* register-based interpreter (heavily optimized) in Linux kernel
* small and lightweight
* not depend on the GCC toolchain for each script run
* easy to use in embedded environments without debugging info
* support for tracepoint, kprobe, uprobe, function trace, timer, and more
* supported in x86, ARM, PowerPC, MIPS
* safety in sandboxQuoted 4 lines
I've asked this fundamental design question before but got no full
answer: how does ktap compare to the ongoing effort of improving the BPF scripting engine?From long experiences of ktap development, what make me really love ktap is:
1) Availability
ktap is only available tool to use in small embedded platform, stap and BPF both need GCC now, stap have its own language, so it's much better than BPF.
(IMO there may need several years to complete a skeleton of dynamic tracing script language, see stap and dtrace)2) Simplicity
ktap is simplest dynamic scripting trace solution now in Linux world, compare with stap/dtrace/BPF.
a). It have simple syntax which make many people like it, it have b). It have simple associate array, make dynamic tracing powerful. c). It have a simple compiler which only have 87K inx86_64. d). It have a simple tracing syntax which constant with perf events.
Quoted 6 lines
3) Safety
ktap already delivered its safety to end user, many people use ktap in their dev lab to investigate problem.
But BPF need time to prove its safety, especially proved by end user, and IMO BPF safety check would be more complex if the runtime support more features as time goes.
safety of ktap is arguable.
1.
Quoted 1 line
From the diff it seems that '
loop_count' is a dynamic way of
checking that loops are not infinite, but max_loop_count = 100000 if loop body has many instructions, such large count may trigger hung_task panic.
2.
jumps are not counted, so if userspace makes an error and loads ktap bytecode with wrong jumps, the interpreter will hang.
3.
recursive functions and f1()->f2()->f1() are not detected either. Another possible hang?
4.
bc_[ft]new instruction are allocating memory and garbage collector suppose to free things when ktap module is unloaded, right? since max_loop_cnt is 100k, a script can allocate quite a bit of memory and kernel will be waiting for userspace trigger to free it? Sounds dangerous.
These concerns are just from quick code review.
Quoted 10 lines
4). Samples
Many people like those ktap samples, ktap shows the attractive by samples.Even I so love ktap and would like share ktap values to everyone, but in technical point of view, I still agree with you that there should have unified scripting engines in kernel if that engine can service for many domains(like networking), but that solution should show its availability/ simplicity/safety firstly to user, not just proved by end user.
Quoted 8 lines
Dynamic tracing scripting environment should contains:
simple compiler, clean language syntax, fast script engine, associative array, aggregation, kstack, ustack, event management, ring buffer, samples, tapset/library, CTF, etc.ktap already fixed most of these issues by its simple design, but BPF only have "script engine" part(its associative array still cannot vmalloc), which have long road before could use by end user.
'internal bpf' instruction set is an assembler instruction set. Low level just like x86 instruction set.
It doesn't have vmalloc instruction and shouldn't have. 'internal bpf' program can theoretically make a call to allocate memory, but I don't think it's safe to let loadable programs to arbitrarily allocate memory.
It's a matter of ownership of the memory.
If script can allocate and receive a pointer to memory, the script owns that memory and kernel cannot touch it until script does 'free' or terminates and GC kicks in.
ktap can be invoked through timers, so this dynamically allocated tables may be living for long time affecting the whole system. The tracing tool should be safer than that.
Quoted 8 lines
ktap is not just bring a bytecode engine, it bring a complete simple dynamic tracing environment to end user, it bring clean language syntax, samples, flexible table, perf like event management, etc, those is the key part to end user, not bytecode engine, so if we can develop simple BPF compiler with similar ktap syntax in some day, then we can replace
kp_lex.c/kp_parse.c/kp_vm.c, and there have zero reason why other parts cannot be shared(associative array, aggregation, kstack, ustack, event management, ring buffer, samples, tapset/library, CTF, etc).
I think nothing stops ktap userspace to parse ktap language and generate 'internal bpf' format. gcc is unnecessary here. I personally think that tracing scripts in C are more readable, but that's minor.
But before we go about generating either 'internal bpf' or any other format, we need to discuss safe scripting design principles. We already have systemtap that is relying on userspace for verification. If we want a real alternative to systemtap, kernel should take care of safety.
Note I'm not proposing to expose 'internal bpf' to userspace in uapi headers and I think ktap shouldn't do it either.
Kernel hosted userspace component (like perf) that uses kernel specific headers allows for much cleaner interfaces without creating 'forever maintain' headache.
Regards,
Alexei
Hi Alexei,
Quoted 54 lines · Alexei Starovoitov
Quoted 46 lines · Jovi Zhangwei
Hi Ingo,
Quoted 20 lines · Ingo Molnar
Quoted 7 lines · Jovi Zhangwei
Hi All,
The following set of patches add ktap tracing tool.
ktap is a new script-based dynamic tracing tool for Linux. It uses a scripting language and lets the user trace system dynamically.
Quoted 9 lines
Highlights features:
* a simple but powerful scripting language
* register-based interpreter (heavily optimized) in Linux kernel
* small and lightweight
* not depend on the GCC toolchain for each script run
* easy to use in embedded environments without debugging info
* support for tracepoint, kprobe, uprobe, function trace, timer, and more
* supported in x86, ARM, PowerPC, MIPS
* safety in sandboxQuoted 4 lines
I've asked this fundamental design question before but got no full
answer: how does ktap compare to the ongoing effort of improving the BPF scripting engine?From long experiences of ktap development, what make me really love ktap is:
1) Availability
ktap is only available tool to use in small embedded platform, stap and BPF both need GCC now, stap have its own language, so it's much better than BPF.
(IMO there may need several years to complete a skeleton of dynamic tracing script language, see stap and dtrace)2) Simplicity
ktap is simplest dynamic scripting trace solution now in Linux world, compare with stap/dtrace/BPF.
a). It have simple syntax which make many people like it, it have b). It have simple associate array, make dynamic tracing powerful. c). It have a simple compiler which only have 87K inx86_64. d). It have a simple tracing syntax which constant with perf events.Quoted 6 lines
3) Safety
ktap already delivered its safety to end user, many people use ktap in their dev lab to investigate problem.
But BPF need time to prove its safety, especially proved by end user, and IMO BPF safety check would be more complex if the runtime support more features as time goes.
Quoted 8 lines
safety of ktap is arguable.
1.
From the diff it seems that 'loop_count' is a dynamic way of checking that loops are not infinite, butmax_loop_count= 100000 if loop body has many instructions, such large count may triggerhung_taskpanic.
Actually I'm planing use time-based time to avoid this, minor issue.
Quoted 4 lines
2.
jumps are not counted, so if userspace makes an error and loads ktap bytecode with wrong jumps, the interpreter will hang.
There leave a todo in validation code, as kernel developers don't like many todo in there, so I will also address it.
Quoted 4 lines
3.
recursive functions and f1()->f2()->f1() are not detected either. Another possible hang?
No, it will exit by ktap stack overflow check.
Quoted 7 lines
4.
bc_[ft]new instruction are allocating memory and garbage collector suppose to free things when ktap module is unloaded, right? sincemax_loop_cntis 100k, a script can allocate quite a bit of memory and kernel will be waiting for userspace trigger to free it? Sounds dangerous.
There will have table/function number limitation, so this is not a problem.
Quoted 21 lines
These concerns are just from quick code review.
Quoted 10 lines
4). Samples
Many people like those ktap samples, ktap shows the attractive by samples.Even I so love ktap and would like share ktap values to everyone, but in technical point of view, I still agree with you that there should have unified scripting engines in kernel if that engine can service for many domains(like networking), but that solution should show its availability/ simplicity/safety firstly to user, not just proved by end user.
Quoted 8 lines
Dynamic tracing scripting environment should contains:
simple compiler, clean language syntax, fast script engine, associative array, aggregation, kstack, ustack, event management, ring buffer, samples, tapset/library, CTF, etc.ktap already fixed most of these issues by its simple design, but BPF only have "script engine" part(its associative array still cannot vmalloc), which have long road before could use by end user.
Quoted 10 lines
'internal bpf' instruction set is an assembler instruction set. Low level just like x86 instruction set.
It doesn't have vmalloc instruction and shouldn't have. 'internal bpf' program can theoretically make a call to allocate memory, but I don't think it's safe to let loadable programs to arbitrarily allocate memory.
It's a matter of ownership of the memory.
If script can allocate and receive a pointer to memory, the script owns that memory and kernel cannot touch it until script does 'free' or terminates and GC kicks in.
Wrong, ktap don't have vmalloc instruction, ktap only use vmalloc for table and memory pool pre-allocation.
Quoted 4 lines
ktap can be invoked through timers, so this dynamically allocated tables may be living for long time affecting the whole system. The tracing tool should be safer than that.
Wrong again, ktap table cannot be allocated in timer context.
Quoted 11 lines
Quoted 8 lines
ktap is not just bring a bytecode engine, it bring a complete simple dynamic tracing environment to end user, it bring clean language syntax, samples, flexible table, perf like event management, etc, those is the key part to end user, not bytecode engine, so if we can develop simple BPF compiler with similar ktap syntax in some day, then we can replace
kp_lex.c/kp_parse.c/kp_vm.c, and there have zero reason why other parts cannot be shared(associative array, aggregation, kstack, ustack, event management, ring buffer, samples, tapset/library, CTF, etc).I think nothing stops ktap userspace to parse ktap language and generate 'internal bpf' format. gcc is unnecessary here.
It's a big engineering problem, BPF bytecode is too low level, BPF engine exposed too much low level stuff to end user, see bpf example:
void dropmon(struct bpf_context *ctx) {
void *loc;
uint64_t *drop_cnt;
loc = (void *)ctx->arg2;
drop_cnt = bpf_table_lookup(ctx, 0, &loc);
if (drop_cnt) {
__sync_fetch_and_add(drop_cnt, 1);
} else {
uint64_t init = 0;
bpf_table_update(ctx, 0, &loc, &init);
}
}IMO there have many issues in this simple script.
If user forget add drop_cnt check, what will
happen, it will reference NULL pointer in __sync_fetch_and_add. How to make sure drop_cn pointer is a valid memory address in table, not other kernel memory allocation?
Look bpf_table_update function, if bpf table overflow, there have no way to stop script executing in there, which make completely wrong things, so you have to add exit condition checking after bpf_table_update(and maybe most C function calls).
And obviously you missed add table lock/unlock in there.
In contrast, look ktap script with same functionality:
var s ={}
trace skb:kfree_skb {
s[arg2] += 1
}User don't need to handle error checking and table lock issue at all, both in source level and bytecode level.
Quoted 1 line
From end user point of view, they want clean language syntax like
above ktap example, so if bpf have same dynamic tracing goal, it should follow this way.
BPF is good, but have many engineering problem to solve to became usable dynamic tracing solution.
Quoted 2 lines
I personally think that tracing scripts in C are more readable, but that's minor.
There will have no one end user use your dropmon.c in they dynamic tracing environment.
Quoted 6 lines
But before we go about generating either 'internal bpf' or any other format, we need to discuss safe scripting design principles. We already have systemtap that is relying on userspace for verification. If we want a real alternative to systemtap, kernel should take care of safety.
Quoted 6 lines
Note I'm not proposing to expose 'internal bpf' to userspace in uapi headers and I think ktap shouldn't do it either.
Kernel hosted userspace component (like perf) that uses kernel specific headers allows for much cleaner interfaces without creating 'forever maintain' headache.
Hmm, Looks reasonable.
Thanks.
Jovi
Quoted 80 lines · Jovi Zhangwei
Hi Alexei,
Quoted 54 lines · Alexei Starovoitov
Quoted 46 lines · Jovi Zhangwei
Hi Ingo,
Quoted 20 lines · Ingo Molnar
Quoted 7 lines · Jovi Zhangwei
Hi All,
The following set of patches add ktap tracing tool.
ktap is a new script-based dynamic tracing tool for Linux. It uses a scripting language and lets the user trace system dynamically.
Quoted 9 lines
Highlights features:
* a simple but powerful scripting language
* register-based interpreter (heavily optimized) in Linux kernel
* small and lightweight
* not depend on the GCC toolchain for each script run
* easy to use in embedded environments without debugging info
* support for tracepoint, kprobe, uprobe, function trace, timer, and more
* supported in x86, ARM, PowerPC, MIPS
* safety in sandboxQuoted 4 lines
I've asked this fundamental design question before but got no full
answer: how does ktap compare to the ongoing effort of improving the BPF scripting engine?From long experiences of ktap development, what make me really love ktap is:
1) Availability
ktap is only available tool to use in small embedded platform, stap and BPF both need GCC now, stap have its own language, so it's much better than BPF.
(IMO there may need several years to complete a skeleton of dynamic tracing script language, see stap and dtrace)2) Simplicity
ktap is simplest dynamic scripting trace solution now in Linux world, compare with stap/dtrace/BPF.
a). It have simple syntax which make many people like it, it have b). It have simple associate array, make dynamic tracing powerful. c). It have a simple compiler which only have 87K inx86_64. d). It have a simple tracing syntax which constant with perf events.Quoted 6 lines
3) Safety
ktap already delivered its safety to end user, many people use ktap in their dev lab to investigate problem.
But BPF need time to prove its safety, especially proved by end user, and IMO BPF safety check would be more complex if the runtime support more features as time goes.Quoted 8 lines
safety of ktap is arguable.
1.
From the diff it seems that 'loop_count' is a dynamic way of checking that loops are not infinite, butmax_loop_count= 100000 if loop body has many instructions, such large count may triggerhung_taskpanic.Actually I'm planing use time-based time to avoid this, minor issue.
Quoted 4 lines
2.
jumps are not counted, so if userspace makes an error and loads ktap bytecode with wrong jumps, the interpreter will hang.There leave a todo in validation code, as kernel developers don't like many todo in there, so I will also address it.
Quoted 4 lines
3.
recursive functions and f1()->f2()->f1() are not detected either. Another possible hang?No, it will exit by ktap stack overflow check.
Quoted 10 lines
Quoted 7 lines
4.
bc_[ft]new instruction are allocating memory and garbage collector suppose to free things when ktap module is unloaded, right? sincemax_loop_cntis 100k, a script can allocate quite a bit of memory and kernel will be waiting for userspace trigger to free it? Sounds dangerous.There will have table/function number limitation, so this is not a problem.
Quoted 1 line
These concerns are just from quick code review.
ok. spotted few more things:
5.bc_callt (ktap tailcall) doesn't have loop_count check. so tailcall can loop forever.
Of course you can fix it with elapsed time check at every branch or call instruction.
6.do_bc_kstr doesn't check that 'd' value is valid and goes kbase[~d] Can be fixed of course.
7.
uget/uset and others seem to have similar problems.
It seems that your definition of 'safe ktap' is that user cannot break kernel if he uses ktap scripting syntax.
In that sense ktap is not much different from stap.
Overall it seems you view ktap bytecode as a continuation
of ktap syntax.
ktap language allows to read pid,uid,tid, so they were added as separate instructions to ktap bytecode...
ktap allows dump of a table, so kernel has to do tab_histdump() including sorting of fields and printf formatting.
What if ktap user wants a different table dump?
or new features from the language?
keep extending bytecode for every printf tweak is not a great solution.
I think design approach to ktap needs to change.
What I'm proposing is the following:
- keep ktap syntax as-is, but remove loops
- ktap style of accessing tables is definitely less verbose then C, so keep it, but don't let compiled program to own the memory
- keep table dump as-is, but do it in userspace instead
In other words compiler for ktap scripts can generate kernel program and userspace program at the same time.
the end users won't notice the difference vs what you have now.
we should learn from BPF design mistake:
BPF was
- user space interface
- safe instruction set
- execution engine
all at the same time and it was hard to extend it, since all aspects need to be considered.
We need to break this dependency.
'internal bpf' is an execution engine.
it's a low level assembler language like x86. Think of it as renamed x86 assembler, where registers are called r1, r2 instead of rdi, rsi.
safety comes from verifier which is decoupled from execution. It can allow complex program or very dumb ones. Today classic bpf is still used as kernel-user interface, it goes through bpf checker and converted to 'internal bpf' for faster execution.
In this case bpf checker allows all existing bpf programs, but ibpf execution engine can do a lot more.
ktap can follow similar approach.
Though I think C as a language to express filters is simpler, ktap syntax is fine as well.
ktap compiler can generate ibpf instructions and let kernel verify them.
ibpf verifier that I've posted earlier has enough knobs to be used as very strict or permissive depending on the kernel component, while both being safe from 'not crashing or hanging kernel' point of view.
Like loops are always disallowed, all memory/register accesses must be valid, data and control dependency between instructions are checked.
Best part is that ktap syntax and features can evolve without ever touching execution engine.
Quoted 2 lines
Wrong, ktap don't have vmalloc instruction, ktap only use vmalloc for table and memory pool pre-allocation.
allowing script to own allocated memory is where we diverge on the approach to safety.
if script can loop or allocate memory, you'd need to dynamically track elapsed time, all allocated memory and all read/write accesses from the program, so execution engine slows down and becomes enforcer of safety.
Every new instruction in such engine needs to be considered from safety point of view. The same problem plagued old bpf.
Quoted 2 lines
It's a big engineering problem, BPF bytecode is too low level, BPF engine exposed too much low level stuff to end user, see bpf example:
you're mixing layers here.
'internal bpf' is a low level execution engine.
Nothing prevents userspace to have ktap or C or any other syntax.
Quoted 15 lines
void dropmon(struct bpf_context *ctx) {
void *loc;
uint64_t *drop_cnt;
loc = (void *)ctx->arg2;
drop_cnt = bpf_table_lookup(ctx, 0, &loc);
if (drop_cnt) {
__sync_fetch_and_add(drop_cnt, 1);
} else {
uint64_t init = 0;
bpf_table_update(ctx, 0, &loc, &init);
}
}Quoted 6 lines
IMO there have many issues in this simple script.
If user forget add
drop_cntcheck, what will
happen, it will reference NULL pointer in__sync_fetch_and_add. How to make suredrop_cnpointer is a valid memory address in table, not other kernel memory allocation?
good question :)
The way verifier guarantees correctness is the following. 'bpf_table_lookup' is annotated as 'returns valid memory of size X or NULL', so verifiers follows that value in a register through control flow graph. In if(drop_cnt) branch, the drop_cnt is valid memory. In else branch, drop_cnt is null.
I've explained it in better details in verifier patch and the doc.
Quoted 4 lines
Look
bpf_table_updatefunction, if bpf table overflow, there have no way to stop script executing in there, which make completely wrong things, so you have to add exit condition checking afterbpf_table_update(and maybe most C function calls).
if you think 'table overflow' notification should be hidden from script writer, then go for it.
ktap can generate ibpf that does bpf_table_update and aborts the script if the limit is hit.
All these decisions are up to userspace and language compiler.
Quoted 1 line
And obviously you missed add table lock/unlock in there.
good question :)
It's actually under rcu which is much faster then lock/unlock.
Quoted 10 lines
In contrast, look ktap script with same functionality:
var s ={} trace skb:kfree_skb { s[arg2] += 1 }User don't need to handle error checking and table lock issue at all, both in source level and bytecode level.
agree that ktap syntax is less verbose as C.
bytecode is a different story.
Quoted 3 lines
From end user point of view, they want clean language syntax like above ktap example, so if bpf have same dynamic tracing goal, it should follow this way.
sure. keep ktap syntax. The users should have multiple choices to write their scripts.
Regards,
Alexei
Quoted 104 lines · Alexei Starovoitov
Quoted 80 lines · Jovi Zhangwei
Hi Alexei,
Quoted 54 lines · Alexei Starovoitov
Quoted 46 lines · Jovi Zhangwei
Hi Ingo,
Quoted 20 lines · Ingo Molnar
Quoted 7 lines · Jovi Zhangwei
Hi All,
The following set of patches add ktap tracing tool.
ktap is a new script-based dynamic tracing tool for Linux. It uses a scripting language and lets the user trace system dynamically.
Quoted 9 lines
Highlights features:
* a simple but powerful scripting language
* register-based interpreter (heavily optimized) in Linux kernel
* small and lightweight
* not depend on the GCC toolchain for each script run
* easy to use in embedded environments without debugging info
* support for tracepoint, kprobe, uprobe, function trace, timer, and more
* supported in x86, ARM, PowerPC, MIPS
* safety in sandboxQuoted 4 lines
I've asked this fundamental design question before but got no full
answer: how does ktap compare to the ongoing effort of improving the BPF scripting engine?From long experiences of ktap development, what make me really love ktap is:
1) Availability
ktap is only available tool to use in small embedded platform, stap and BPF both need GCC now, stap have its own language, so it's much better than BPF.
(IMO there may need several years to complete a skeleton of dynamic tracing script language, see stap and dtrace)2) Simplicity
ktap is simplest dynamic scripting trace solution now in Linux world, compare with stap/dtrace/BPF.
a). It have simple syntax which make many people like it, it have b). It have simple associate array, make dynamic tracing powerful. c). It have a simple compiler which only have 87K inx86_64. d). It have a simple tracing syntax which constant with perf events.Quoted 6 lines
3) Safety
ktap already delivered its safety to end user, many people use ktap in their dev lab to investigate problem.
But BPF need time to prove its safety, especially proved by end user, and IMO BPF safety check would be more complex if the runtime support more features as time goes.Quoted 8 lines
safety of ktap is arguable.
1.
From the diff it seems that 'loop_count' is a dynamic way of checking that loops are not infinite, butmax_loop_count= 100000 if loop body has many instructions, such large count may triggerhung_taskpanic.Actually I'm planing use time-based time to avoid this, minor issue.
Quoted 4 lines
2.
jumps are not counted, so if userspace makes an error and loads ktap bytecode with wrong jumps, the interpreter will hang.There leave a todo in validation code, as kernel developers don't like many todo in there, so I will also address it.
Quoted 4 lines
3.
recursive functions and f1()->f2()->f1() are not detected either. Another possible hang?No, it will exit by ktap stack overflow check.
Quoted 10 lines
Quoted 7 lines
4.
bc_[ft]new instruction are allocating memory and garbage collector suppose to free things when ktap module is unloaded, right? sincemax_loop_cntis 100k, a script can allocate quite a bit of memory and kernel will be waiting for userspace trigger to free it? Sounds dangerous.There will have table/function number limitation, so this is not a problem.
Quoted 1 line
These concerns are just from quick code review.
ok. spotted few more things:
5.
bc_callt(ktap tailcall) doesn't haveloop_countcheck. so tailcall can loop forever.
Of course you can fix it with elapsed time check at every branch or call instruction.6.
do_bc_kstrdoesn't check that 'd' value is valid and goes kbase[~d] Can be fixed of course.
Quoted 7 lines
7.
uget/uset and others seem to have similar problems.It seems that your definition of 'safe ktap' is that user cannot break kernel if he uses ktap scripting syntax.
In that sense ktap is not much different from stap.
Definitely not.
Safety don't means bound check which you listed above, ktap have safety on bytecode level in design, not syntax, it means whatever you do on the bytecode, it will never crash kernel, please see more in ktapvm.
Quoted 10 lines
Overall it seems you view ktap bytecode as a continuation
of ktap syntax.
ktap language allows to read pid,uid,tid, so they were added as separate instructions to ktap bytecode...
ktap allows dump of a table, so kernel has to dotab_histdump()including sorting of fields and printf formatting.
What if ktap user wants a different table dump?
or new features from the language?
keep extending bytecode for every printf tweak is not a great solution.
Quoted 7 lines
I think design approach to ktap needs to change.
What I'm proposing is the following:
- keep ktap syntax as-is, but remove loops
- ktap style of accessing tables is definitely less verbose then C, so keep it, but don't let compiled program to own the memory
- keep table dump as-is, but do it in userspace instead
You statements is conflicts in here.
If move table dump into userspace, then your compiler must need to support loop, but you also suggested to kill loop in compiler.
Quoted 102 lines
In other words compiler for ktap scripts can generate kernel program and userspace program at the same time.
the end users won't notice the difference vs what you have now.we should learn from BPF design mistake:
BPF was
- user space interface
- safe instruction set
- execution engine
all at the same time and it was hard to extend it, since all aspects need to be considered.We need to break this dependency.
'internal bpf' is an execution engine.
it's a low level assembler language like x86. Think of it as renamed x86 assembler, where registers are called r1, r2 instead of rdi, rsi.safety comes from verifier which is decoupled from execution. It can allow complex program or very dumb ones. Today classic bpf is still used as kernel-user interface, it goes through bpf checker and converted to 'internal bpf' for faster execution.
In this case bpf checker allows all existing bpf programs, but ibpf execution engine can do a lot more.ktap can follow similar approach.
Though I think C as a language to express filters is simpler, ktap syntax is fine as well.
ktap compiler can generate ibpf instructions and let kernel verify them.
ibpf verifier that I've posted earlier has enough knobs to be used as very strict or permissive depending on the kernel component, while both being safe from 'not crashing or hanging kernel' point of view.
Like loops are always disallowed, all memory/register accesses must be valid, data and control dependency between instructions are checked.Best part is that ktap syntax and features can evolve without ever touching execution engine.
Quoted 2 lines
Wrong, ktap don't have vmalloc instruction, ktap only use vmalloc for table and memory pool pre-allocation.
allowing script to own allocated memory is where we diverge on the approach to safety.
if script can loop or allocate memory, you'd need to dynamically track elapsed time, all allocated memory and all read/write accesses from the program, so execution engine slows down and becomes enforcer of safety.
Every new instruction in such engine needs to be considered from safety point of view. The same problem plagued old bpf.Quoted 2 lines
It's a big engineering problem, BPF bytecode is too low level, BPF engine exposed too much low level stuff to end user, see bpf example:
you're mixing layers here.
'internal bpf' is a low level execution engine.
Nothing prevents userspace to have ktap or C or any other syntax.Quoted 15 lines
void dropmon(struct bpf_context *ctx) { void *loc; uint64_t *drop_cnt; loc = (void *)ctx->arg2; drop_cnt = bpf_table_lookup(ctx, 0, &loc); if (drop_cnt) { __sync_fetch_and_add(drop_cnt, 1); } else { uint64_t init = 0; bpf_table_update(ctx, 0, &loc, &init); } }Quoted 6 lines
IMO there have many issues in this simple script.
If user forget add
drop_cntcheck, what will
happen, it will reference NULL pointer in__sync_fetch_and_add. How to make suredrop_cnpointer is a valid memory address in table, not other kernel memory allocation?good question :)
The way verifier guarantees correctness is the following. 'bpf_table_lookup' is annotated as 'returns valid memory of size X or NULL', so verifiers follows that value in a register through control flow graph. Inif(drop_cnt)branch, thedrop_cntis valid memory. In else branch,drop_cntis null.
I've explained it in better details in verifier patch and the doc.Quoted 4 lines
Look
bpf_table_updatefunction, if bpf table overflow, there have no way to stop script executing in there, which make completely wrong things, so you have to add exit condition checking afterbpf_table_update(and maybe most C function calls).if you think 'table overflow' notification should be hidden from script writer, then go for it.
ktap can generate ibpf that doesbpf_table_updateand aborts the script if the limit is hit.
All these decisions are up to userspace and language compiler.
Quoted 5 lines
Quoted 1 line
And obviously you missed add table lock/unlock in there.
good question :)
It's actually under rcu which is much faster then lock/unlock.
Not sure it's really a good idea to protect table operation by RCU in probe context.
Quoted 14 lines
Quoted 10 lines
In contrast, look ktap script with same functionality:
var s ={} trace skb:kfree_skb { s[arg2] += 1 }User don't need to handle error checking and table lock issue at all, both in source level and bytecode level.
agree that ktap syntax is less verbose as C.
bytecode is a different story.
Quoted 7 lines
Quoted 3 lines
From end user point of view, they want clean language syntax like above ktap example, so if bpf have same dynamic tracing goal, it should follow this way.
sure. keep ktap syntax. The users should have multiple choices to write their scripts.
Ok, we discussed a lot about ktap vs. ebpf, so what comes into my mind is:
1). ktap could generate more low-level bytecode than now.
Basically I agree on this, that's would be faster and make ktapvm more lightweight, that's what I want to see.
So I think ktap bytecode engine maybe can integrate with ebpf in some day, but also maybe need some changes on ebpf part, not only ktap part.
2) keeps ktap simplicity and flexibility
The simplicity and flexibility is the great value of ktap, that's the reason why many people like it.
3) Evolve ktap features independently with bytecode engine The ktap features in here includes:
clean syntax, associative array, aggregation, event management, timer, resource management, kstack, ustack, ring buffer, tapset/library, CTF, samples, etc.
All these tracing features should evolve independently with bytecode engine.
Actually this is what ktap does now, the bytecode engine is very independent, but will be decouples more in future. This is important because we can change bytecode engine smoothly without broken any existing features.
Basically I think the relation between ktap and ebpf is not contention, but complement.
So based on all these input, I suggest:
Put all these community efforts together, figure out the proper design implementation of dynamic tracing tool, ktap can be a good start to build upon it, evolve to a unified kernel script engine with ebpf together, finally service for dynamic tracing and network(if possible).
Our goal is same and very clearly, we really want a "simple & flexible & safe" dynamic scripting tracing tool for Linux, which could compare or even better than Dtrace, this is the motivation of ktap project.
Two solution may be take:
1). upstream ktap into core trace and evolve it step by step, and finally
make a integrated bytecode engine, it's a long process, but I think it's worth.
2). move ktap back into staging, and graduate from staging after the
code make tracing people and ebpf people both happy.
The benefit is the process will be under the eyes of community.
Ingo, steven, Greg, what do you think?
Thanks.
Jovi
Quoted 19 lines · Jovi Zhangwei
So based on all these input, I suggest:
Put all these community efforts together, figure out the proper design implementation of dynamic tracing tool, ktap can be a good start to build upon it, evolve to a unified kernel script engine with ebpf together, finally service for dynamic tracing and network(if possible).
Our goal is same and very clearly, we really want a "simple & flexible & safe" dynamic scripting tracing tool for Linux, which could compare or even better than Dtrace, this is the motivation of ktap project.
Two solution may be take:
1). upstream ktap into core trace and evolve it step by step, and
finally make a integrated bytecode engine, it's a long process, but I think it's worth.
Quoted 6 lines
2). move ktap back into staging, and graduate from staging after the
code make tracing people and ebpf people both happy.The benefit is the process will be under the eyes of community.
Ingo, steven, Greg, what do you think?
For now I'm opting for a third option:
3) Maintain my NAK on the ktap patches until they address the fundamental design concerns outlined by Alexei and others in their review feedback:
The thing is, I've outlined some of the concerns in my previous review. Not much happened on that front, for example ktap did not get any closer in integrating with BPF. Many months have passed since the previous ktap submission, still I see no progress on the 'design' front. That really needs to change.
Please keep me Cc:-ed to any and all future ktap submissions so I can monitor ktap's progress and lift the NAK if the design concerns have been addressed.
Thanks,
Ingo
Quoted 27 lines · Ingo Molnar
Quoted 24 lines · Jovi Zhangwei
So based on all these input, I suggest:
Put all these community efforts together, figure out the proper design implementation of dynamic tracing tool, ktap can be a good start to build upon it, evolve to a unified kernel script engine with ebpf together, finally service for dynamic tracing and network(if possible).
Our goal is same and very clearly, we really want a "simple & flexible & safe" dynamic scripting tracing tool for Linux, which could compare or even better than Dtrace, this is the motivation of ktap project.
Two solution may be take:
1). upstream ktap into core trace and evolve it step by step, and
finally make a integrated bytecode engine, it's a long process, but I think it's worth.2). move ktap back into staging, and graduate from staging after the
code make tracing people and ebpf people both happy.The benefit is the process will be under the eyes of community.
Quoted 8 lines
Quoted 1 line
Ingo, steven, Greg, what do you think?
For now I'm opting for a third option:
3) Maintain my NAK on the ktap patches until they address the fundamental design concerns outlined by Alexei and others in their review feedback:
There is no fundamental design concerns outlined about ktap, that 'loop' in ktap design is not fundamental, others review feedback is focus on patch, not fundamental design, and those "safety" concerns raised by Alexei is just bound checking.
I don't see any others.
Quoted 6 lines
NAKed-by Ingo Molnar <[email protected]>The thing is, I've outlined some of the concerns in my previous review. Not much happened on that front, for example ktap did not get any closer in integrating with BPF.
I don't see any suggestion about integrating BPF before this review cycle.
Quoted 4 lines
Many months have passed since the
previous ktap submission, still I see no progress on the 'design' front. That really needs to change.
I think the change need both in ktap and ebpf, not ktap one side, if you really want integrate ktap and BPF is a way.
There have no proven that current ebpf low level bytecode design is suit for generic dynamic tracing use, I also raised some design concern about ebpf in previous mail but got no clear answer, like exposed too much low level stuff to end user, that would be a problem because script engine need to validate more memory references which should not need to; and also there may be expose table lock into user inf future, that would be more complex IMO.
The efforts need all related people participation, not only ktap, ktap and bpf maybe need to close with each other, but not means bpf stay and let ktap move close.
That's why I think the things would be more faster and effective if in public place(like staging tree), otherwise the change may be hard to happen.
Thanks.
Jovi
Quoted 30 lines · Jovi Zhangwei
Quoted 29 lines · Ingo Molnar
Quoted 25 lines · Jovi Zhangwei
So based on all these input, I suggest:
Put all these community efforts together, figure out the proper design implementation of dynamic tracing tool, ktap can be a good start to build upon it, evolve to a unified kernel script engine with ebpf together, finally service for dynamic tracing and network(if possible).
Our goal is same and very clearly, we really want a "simple & flexible & safe" dynamic scripting tracing tool for Linux, which could compare or even better than Dtrace, this is the motivation of ktap project.
Two solution may be take:
1). upstream ktap into core trace and evolve it step by step, and
finally make a integrated bytecode engine, it's a long process, but I think it's worth.2). move ktap back into staging, and graduate from staging after the
code make tracing people and ebpf people both happy.The benefit is the process will be under the eyes of community.
Ingo, steven, Greg, what do you think?
Quoted 8 lines
Quoted 6 lines
For now I'm opting for a third option:
3) Maintain my NAK on the ktap patches until they address the fundamental design concerns outlined by Alexei and others in their review feedback:
There is no fundamental design concerns outlined about ktap, [...]
Well, the concerns that were outlined do raise to fundamental in my book. But as long as they are addressed it does not matter how we classify them.
Quoted 2 lines
I don't see any suggestion about integrating BPF before this review cycle.
Hm, I mentioned it at the kernel summit to folks who raised the ktap subject, but apparently not over email. I assumed a new ktap submission would come quickly.
Anyway, considering how BPF is integrating with (and hopefully replacing) tracing filters, it makes sense to apply the same concept in the ktap case as well.
Thanks,
Ingo
Quoted 6 lines · Ingo Molnar
Quoted 2 lines · Jovi Zhangwei
I don't see any suggestion about integrating BPF before this review cycle.
Quoted 8 lines
Hm, I mentioned it at the kernel summit to folks who raised the ktap subject, but apparently not over email. I assumed a new ktap submission would come quickly.
Anyway, considering how BPF is integrating with (and hopefully replacing) tracing filters, it makes sense to apply the same concept in the ktap case as well.
It seems that kernel developers want a C based tracing filter, BPF rooted from C(Compiler, performance, and networking target), but ktap designed from a different point of view, which highlights simplicity and flexibility, this simplicity and flexibility is not only presented by simple syntax, but also in its bytecode engine.
trace syscalls:* {
print(cpu, pid, execname, argstr, stack())
}
Each expression have own type in kernel, the type is not judged by userspace compiler, this make print expression much much easy, also make handle associate array(and aggregation) extremely simple. If we force replace ktap core with a much "static" bytecode engine, then it will lose the highlight feature: simplicity and flexibility.
ktap is not designed to be a built-in tracing filter and not purpose for high performance bytecode engine(can use for networking), BPF is suit for that way, but that is not a design flaw in ktap, it's designed to be like that.
Obviously many people love ktap nowadays even though it's not a C-family language, people can use ktap one-liner to do interesting things(http://brendangregg.com/ktap.html), and used in real world. Also especially people in embedded world like ktap very much, they already included ktap into OpenEmebedded as I know.
It seems that more and more tracing related kernel modules will come in future(like https://github.com/draios/sysdig/issues/81),
so maybe these third-part kernel modules based on tracing export symbols should locate in drivers/, not kernel/trace/, today we have well defined tracing kernel interface(tracepoint, kprobe, uprobe, perf callback),
no reason to put all these external modules sit in kernel/trace one place, especially if the module have real end users in world.
Thanks.
Jovi
Quoted 2 lines · Jovi Zhangwei
Obviously many people love ktap nowadays even though it's not a C-family language, [...]
Imagine how much more widespread it would become amongst kernel developers if it had C syntax - see PeterZ's reply for example.
Thanks,
Ingo
Quoted 7 lines · Ingo Molnar
Quoted 2 lines · Jovi Zhangwei
Obviously many people love ktap nowadays even though it's not a C-family language, [...]
Imagine how much more widespread it would become amongst kernel developers if it had C syntax - see PeterZ's reply for example.
+1
I think it would be awesome to reuse the kernel's BPF engine with its C backend for this in the way that Alexei has started in its original set; surely there's a lot of work that still needs to be addressed, but it would allow long-term for just having that engine to maintain, and also to exploit its flexibility and speed (JIT) where we can.
Quoted 16 lines · Alexei Starovoitov
[...]
It seems that your definition of 'safe ktap' is that user cannot break kernel if he uses ktap scripting syntax.
In that sense ktap is not much different from stap.Overall it seems you view ktap bytecode as a continuation
of ktap syntax.
ktap language allows to read pid,uid,tid, so they were added as separate instructions to ktap bytecode...
ktap allows dump of a table, so kernel has to dotab_histdump()including sorting of fields and printf formatting.
What if ktap user wants a different table dump?
or new features from the language?
keep extending bytecode for every printf tweak is not a great solution.
Quoted 6 lines
I think design approach to ktap needs to change.
What I'm proposing is the following:
- keep ktap syntax as-is, but remove loops
- ktap style of accessing tables is definitely less verbose then C, so keep it, but don't let compiled program to own the memory
- keep table dump as-is, but do it in userspace instead
I'd suggest using C syntax instead initially, because that's what the kernel is using.
The overwhelming majority of people probing the kernel are programmers, so there's no point in inventing new syntax, we should reuse existing syntax!
That is one reason why for example the (very simple!) ftrace filter language tries to mimic C syntax.
Especially as C is simpler for an important category, filters:
Quoted 2 lines
Though I think C as a language to express filters is simpler, ktap syntax is fine as well.
Thanks,
Ingo
Quoted 6 lines · Ingo Molnar
I'd suggest using C syntax instead initially, because that's what the kernel is using.
The overwhelming majority of people probing the kernel are programmers, so there's no point in inventing new syntax, we should reuse existing syntax!
Yes please, keep it C, I forever forget all other syntaxes. While I have in the past known other languages, I never use them frequently enough to remember them. And there's nothing more frustrating than having to fight a tool/language when you just want to get work done.
Quoted 4 lines · Peter Zijlstra
Quoted 3 lines · Ingo Molnar
I'd suggest using C syntax instead initially, because that's what the kernel is using.
Quoted 8 lines
Quoted 3 lines
The overwhelming majority of people probing the kernel are programmers, so there's no point in inventing new syntax, we should reuse existing syntax!
Yes please, keep it C, I forever forget all other syntaxes. While I have in the past known other languages, I never use them frequently enough to remember them. And there's nothing more frustrating than having to fight a tool/language when you just want to get work done.
Why wouldn't you write a kernel module in C directly? :)
It seems that all what you need is not a tracing language nor a bytecode engine, but an well organized tracing APIs(library?) for writing a kernel module for tracing...
Thank you,
Signature · 4 lines
Masami HIRAMATSU Software Platform Research Dept. Linux Technology Center Hitachi, Ltd., Yokohama Research Laboratory E-mail: [email protected]
Quoted 14 lines · Masami Hiramatsu
Quoted 4 lines · Peter Zijlstra
Quoted 3 lines · Ingo Molnar
I'd suggest using C syntax instead initially, because that's what the kernel is using.
Quoted 8 lines
Quoted 3 lines
The overwhelming majority of people probing the kernel are programmers, so there's no point in inventing new syntax, we should reuse existing syntax!
Yes please, keep it C, I forever forget all other syntaxes. While I have in the past known other languages, I never use them frequently enough to remember them. And there's nothing more frustrating than having to fight a tool/language when you just want to get work done.
Quoted 4 lines
Why wouldn't you write a kernel module in C directly? :)
It seems that all what you need is not a tracing language nor a bytecode engine, but an well organized tracing APIs(library?) for writing a kernel module for tracing...
Most my kernels are CONFIG_MODULE=n :-) Also, I never can remember how to do modules.
That said; what I currently do it hack the kernel with debug bits and pieces and run that, which is effectively the same. Its just that its impossible to save/share these hacks in any sane fashion.