From fa049504752a2b8ba77939f8eafa9d763abec083 Mon Sep 17 00:00:00 2001 From: Vyacheslav Cherkashin Date: Thu, 9 Jun 2016 15:50:26 +0300 Subject: [PATCH] [REFACTOR] divide kprobe tests module Change-Id: I34dabc4072e385a1a504fe2dbb3a55ceb29a1c37 Signed-off-by: Vyacheslav Cherkashin --- tests/kprobe_tests/Kbuild | 4 +- tests/kprobe_tests/kp_module.c | 109 +++++++++++++++++++++ tests/kprobe_tests/kp_module.h | 30 ++++++ .../kprobe_tests/{kp_tests_module.c => kp_tests.c} | 72 +------------- 4 files changed, 145 insertions(+), 70 deletions(-) create mode 100644 tests/kprobe_tests/kp_module.c create mode 100644 tests/kprobe_tests/kp_module.h rename tests/kprobe_tests/{kp_tests_module.c => kp_tests.c} (85%) diff --git a/tests/kprobe_tests/Kbuild b/tests/kprobe_tests/Kbuild index 7ffd593..f60b2c1 100644 --- a/tests/kprobe_tests/Kbuild +++ b/tests/kprobe_tests/Kbuild @@ -1,4 +1,6 @@ EXTRA_CFLAGS := $(extra_cflags) obj-m := swap_kp_tests.o -swap_kp_tests-y := kp_tests_module.o +swap_kp_tests-y := \ + kp_module.o \ + kp_tests.o diff --git a/tests/kprobe_tests/kp_module.c b/tests/kprobe_tests/kp_module.c new file mode 100644 index 0000000..171bc30 --- /dev/null +++ b/tests/kprobe_tests/kp_module.c @@ -0,0 +1,109 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * Copyright (C) Samsung Electronics, 2016 + * + * 2016 Vyacheslav Cherkashin + * + */ + + +#include +#include +#include +#include +#include "kp_module.h" + + +static asmlinkage long (*olog_sys_write)(unsigned int, const char __user *, size_t); + +static long write_to_stdout(const char *buf, size_t len) +{ + long ret; + + mm_segment_t fs = get_fs(); + set_fs(get_ds()); + ret = olog_sys_write(1, buf, len); + set_fs(fs); + + return ret; +} + +static int olog_init(void) +{ + olog_sys_write = (void *)kallsyms_lookup_name("sys_write"); + if (olog_sys_write == NULL) { + pr_err("ERR: not found 'sys_write' symbol\n"); + return -ESRCH; + } + + return 0; +} + +void olog(const char *fmt, ...) +{ + char buf[256]; + va_list args; + + va_start(args, fmt); + vsnprintf(buf, sizeof(buf), fmt, args); + va_end(args); + + printk("%s", buf); + write_to_stdout(buf, strlen(buf)); +} + + + + +static void print_mod_info(void) +{ + struct module *mod = THIS_MODULE; + + printk("### MOD_INFO:\n"); + printk(" core: %p..%p\n", mod->module_init, mod->module_init + mod->init_text_size); + printk(" init: %p..%p\n", mod->module_core, mod->module_core + mod->core_text_size); + printk("\n"); +} + + +/* TODO: move declare to header */ +int kp_tests_run(void); + +static int __init tests_init(void) +{ + int ret; + + ret = olog_init(); + if (ret) + return ret; + + print_mod_info(); + + olog("### Begin tests ###\n"); + kp_tests_run(); + olog("### End tests ###\n"); + + return -1; +} + +static void __exit tests_exit(void) +{ +} + +module_init(tests_init); +module_exit(tests_exit); + +MODULE_LICENSE("GPL"); diff --git a/tests/kprobe_tests/kp_module.h b/tests/kprobe_tests/kp_module.h new file mode 100644 index 0000000..e3f0b2b --- /dev/null +++ b/tests/kprobe_tests/kp_module.h @@ -0,0 +1,30 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * Copyright (C) Samsung Electronics, 2016 + * + * 2016 Vyacheslav Cherkashin + * + */ + + +#ifndef _KP_MODULE_H +#define _KP_MODULE_H + + +void olog(const char *fmt, ...); + + +#endif /* _KP_MODULE_H */ diff --git a/tests/kprobe_tests/kp_tests_module.c b/tests/kprobe_tests/kp_tests.c similarity index 85% rename from tests/kprobe_tests/kp_tests_module.c rename to tests/kprobe_tests/kp_tests.c index 816d92e..6386f50 100644 --- a/tests/kprobe_tests/kp_tests_module.c +++ b/tests/kprobe_tests/kp_tests.c @@ -21,58 +21,16 @@ #include -#include #include #include #include -#include #include -#include +#include "kp_module.h" static struct task_struct *cur_task; - -/* - ****************************************************************************** - * log * - ****************************************************************************** - */ -static long write_to_stdout(const char *buf, size_t len) -{ - static asmlinkage long (*sys_write)(unsigned int, const char __user *, size_t) = NULL; - long ret = -ESRCH; - - if (sys_write == NULL) - sys_write = (void *)swap_ksyms("sys_write"); - - if (sys_write) { - mm_segment_t fs = get_fs(); - set_fs(get_ds()); - ret = sys_write(1, buf, len); - set_fs(fs); - } - - return ret; -} - -static void olog(const char *fmt, ...) -{ - char buf[256]; - va_list args; - - va_start(args, fmt); - vsnprintf(buf, sizeof(buf), fmt, args); - va_end(args); - - printk("%s", buf); - write_to_stdout(buf, strlen(buf)); -} - - - - static struct kprobe *kp_create(char *name, int (*pre_h)(struct kprobe *, struct pt_regs *)) { @@ -447,24 +405,10 @@ static void test_sync_unreg_and_multiple_handlers2(void) post_test_sync_unreg_and_mh(2, 2); } - -static void print_mod_info(void) -{ - struct module *mod = THIS_MODULE; - - printk("### MOD_INFO:\n"); - printk(" core: %p..%p\n", mod->module_init, mod->module_init + mod->init_text_size); - printk(" init: %p..%p\n", mod->module_core, mod->module_core + mod->core_text_size); - printk("\n"); -} - -static int test_init(void) +int kp_tests_run(void) { cur_task = current; - print_mod_info(); - - olog("### Begin tests ###\n"); test_recursion(); test_recursion_and_multiple_handlers(); test_recursion_and_multiple_handlers2(); @@ -473,16 +417,6 @@ static int test_init(void) test_sync_unreg(); test_sync_unreg_and_multiple_handlers(); test_sync_unreg_and_multiple_handlers2(); - olog("### End tests ###\n"); - - return -1; -} -static void test_exit(void) -{ + return 0; } - - -SWAP_LIGHT_INIT_MODULE(NULL, test_init, test_exit, NULL, NULL); - -MODULE_LICENSE("GPL"); -- 2.7.4