11#include <log.h>
22#include <compiler.h>
33#include <kpmodule.h>
4+ #include <linux/printk.h>
45#include <linux/cred.h>
56#include <taskext.h>
67#include <linux/printk.h>
1213#include "kernel_trace.h"
1314
1415KPM_NAME ("kernel_trace" );
15- KPM_VERSION ("1 .0.0" );
16+ KPM_VERSION ("2 .0.0" );
1617KPM_LICENSE ("GPL v2" );
1718KPM_AUTHOR ("Test" );
1819KPM_DESCRIPTION ("use uprobe trace some fun in kpm" );
@@ -24,7 +25,7 @@ int (*kern_path)(const char *name, unsigned int flags, struct path *path) = 0;
2425struct inode * (* igrab )(struct inode * inode ) = 0 ;
2526void (* path_put )(const struct path * path ) = 0 ;
2627void (* rcu_read_unlock )(void ) = 0 ;
27- unsigned long ( * perf_instruction_pointer )( struct pt_regs * regs ) = 0 ;
28+ int ( * trace_printk )( unsigned long ip , const char * fmt , ... ) = 0 ;
2829
2930
3031char file_name [MAX_PATH_LEN ];
@@ -43,7 +44,7 @@ void before_mincore(hook_fargs3_t *args, void *udata){
4344 }
4445
4546 int trace_info = trace_flag - TRACE_FLAG ;
46- if (trace_info == SET_MODULE_OFFSET ){
47+ if (trace_info == SET_FUN_INFO ){
4748 if (unlikely (hook_num == MAX_HOOK_NUM )){
4849 logke ("+Test-Log+ MAX_HOOK_NUM:%d\n" ,MAX_HOOK_NUM );
4950 goto error_out ;
@@ -126,19 +127,38 @@ void before_mincore(hook_fargs3_t *args, void *udata){
126127}
127128
128129
129- static int trace_handler (struct uprobe_consumer * self , struct pt_regs * regs ){
130+ static int trace_handler (struct uprobe_consumer * self , struct mpt_regs * regs ){
130131 struct task_struct * task = current ;
131132 struct cred * cred = * (struct cred * * )((uintptr_t )task + task_struct_offset .cred_offset );
132133 uid_t uid = * (uid_t * )((uintptr_t )cred + cred_offset .uid_offset );
134+ struct my_key_value * tfun ;
135+ unsigned long fun_offset ;
133136 if (uid == target_uid ){
134- unsigned long fun_offset = perf_instruction_pointer (regs )- module_base ;
135- struct my_key_value * tfun = search_key_value (& fun_info_tree ,fun_offset );
136- if (likely (tfun )){
137- logkd ("+Test-Log+ fun_name:%s,fun_offset:0x%llx calling\n" ,tfun -> value ,fun_offset );
137+ fun_offset = regs -> pc - module_base ;
138+ tfun = search_key_value (& fun_info_tree ,fun_offset );
139+ if (tfun ){
140+ goto target_out ;
141+ }else {
142+ fun_offset = fun_offset - 0x1000 ;
143+ tfun = search_key_value (& fun_info_tree ,fun_offset );
144+ if (likely (tfun )){
145+ goto target_out ;
146+ }
138147 }
139- // logkd("+Test-Log+ fun_offset:%llx\n",perf_instruction_pointer(regs)-module_base);
148+ }else {
149+ goto no_target_out ;
150+ }
151+
152+ target_out :
153+ logkd ("+Test-Log+ fun_name:%s,fun_offset:0x%llx calling\n" ,tfun -> value ,fun_offset );
154+ int trace_printk_ret = trace_printk (_THIS_IP_ ,"+Test-Log+ fun_name:%s,fun_offset:0x%llx calling\n" ,tfun -> value ,fun_offset );
155+ if (unlikely (trace_printk_ret < 0 )){
156+ logke ("+Test-Log+ trace_printk error\n" );
140157 }
141158 return 0 ;
159+
160+ no_target_out :
161+ return 0 ;
142162}
143163
144164
@@ -152,14 +172,15 @@ static long kernel_trace_init(const char *args, const char *event, void *__user
152172 igrab = (typeof (igrab ))kallsyms_lookup_name ("igrab" );
153173 path_put = (typeof (path_put ))kallsyms_lookup_name ("path_put" );
154174 rcu_read_unlock = (typeof (rcu_read_unlock ))kallsyms_lookup_name ("rcu_read_unlock" );
155- perf_instruction_pointer = (typeof (perf_instruction_pointer ))kallsyms_lookup_name ("perf_instruction_pointer" );
156175
157176
158177 rb_erase = (typeof (rb_erase ))kallsyms_lookup_name ("rb_erase" );
159178 rb_insert_color = (typeof (rb_insert_color ))kallsyms_lookup_name ("rb_insert_color" );
160179 rb_first = (typeof (rb_first ))kallsyms_lookup_name ("rb_first" );
161180 kmalloc = (typeof (kmalloc ))kallsyms_lookup_name ("__kmalloc" );
162181 kfree = (typeof (kfree ))kallsyms_lookup_name ("kfree" );
182+
183+ trace_printk = (typeof (trace_printk ))kallsyms_lookup_name ("__trace_printk" );
163184
164185 logkd ("+Test-Log+ mtask_pid_nr_ns:%llx\n" ,mtask_pid_nr_ns );
165186 logkd ("+Test-Log+ uprobe_register:%llx\n" ,uprobe_register );
@@ -174,10 +195,12 @@ static long kernel_trace_init(const char *args, const char *event, void *__user
174195 logkd ("+Test-Log+ rb_first:%llx\n" ,rb_first );
175196 logkd ("+Test-Log+ kmalloc:%llx\n" ,kmalloc );
176197 logkd ("+Test-Log+ kfree:%llx\n" ,kfree );
198+
199+ logkd ("+Test-Log+ trace_printk:%llx\n" ,trace_printk );
177200
178201 if (!(mtask_pid_nr_ns && uprobe_register && uprobe_unregister
179202 && kern_path && igrab && path_put && rcu_read_unlock
180- && rb_erase && rb_insert_color && rb_first )){
203+ && rb_erase && rb_insert_color && rb_first && trace_printk )){
181204 logke ("+Test-Log+ can not find some fun addr\n" );
182205 return -1 ;
183206 }
@@ -186,7 +209,7 @@ static long kernel_trace_init(const char *args, const char *event, void *__user
186209
187210 hook_err_t err = inline_hook_syscalln (__NR_mincore , 3 , before_mincore , 0 , 0 );
188211 if (err ){
189- logke ("+Test-Log+ hook __NR_kexec_file_load error\n" );
212+ logke ("+Test-Log+ hook __NR_mincore error\n" );
190213 return -1 ;
191214 }
192215
0 commit comments