[REFACTOR] divide kprobe tests module
[kernel/swap-modules.git] / tests / kprobe_tests / kp_module.c
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  *
16  * Copyright (C) Samsung Electronics, 2016
17  *
18  * 2016         Vyacheslav Cherkashin <v.cherkashin@samsung.com>
19  *
20  */
21
22
23 #include <linux/module.h>
24 #include <linux/string.h>
25 #include <linux/uaccess.h>
26 #include <linux/kallsyms.h>
27 #include "kp_module.h"
28
29
30 static asmlinkage long (*olog_sys_write)(unsigned int, const char __user *, size_t);
31
32 static long write_to_stdout(const char *buf, size_t len)
33 {
34         long ret;
35
36         mm_segment_t fs = get_fs();
37         set_fs(get_ds());
38         ret = olog_sys_write(1, buf, len);
39         set_fs(fs);
40
41         return ret;
42 }
43
44 static int olog_init(void)
45 {
46         olog_sys_write = (void *)kallsyms_lookup_name("sys_write");
47         if (olog_sys_write == NULL) {
48                 pr_err("ERR: not found 'sys_write' symbol\n");
49                 return -ESRCH;
50         }
51
52         return 0;
53 }
54
55 void olog(const char *fmt, ...)
56 {
57         char buf[256];
58         va_list args;
59
60         va_start(args, fmt);
61         vsnprintf(buf, sizeof(buf), fmt, args);
62         va_end(args);
63
64         printk("%s", buf);
65         write_to_stdout(buf, strlen(buf));
66 }
67
68
69
70
71 static void print_mod_info(void)
72 {
73         struct module *mod = THIS_MODULE;
74
75         printk("### MOD_INFO:\n");
76         printk("    core: %p..%p\n", mod->module_init, mod->module_init + mod->init_text_size);
77         printk("    init: %p..%p\n", mod->module_core, mod->module_core + mod->core_text_size);
78         printk("\n");
79 }
80
81
82 /* TODO: move declare to header */
83 int kp_tests_run(void);
84
85 static int __init tests_init(void)
86 {
87         int ret;
88
89         ret = olog_init();
90         if (ret)
91                 return ret;
92
93         print_mod_info();
94
95         olog("### Begin tests ###\n");
96         kp_tests_run();
97         olog("### End tests ###\n");
98
99         return -1;
100 }
101
102 static void __exit tests_exit(void)
103 {
104 }
105
106 module_init(tests_init);
107 module_exit(tests_exit);
108
109 MODULE_LICENSE("GPL");