1010#include <linux/string.h>
1111#include <syscall.h>
1212#include <asm/current.h>
13+ #include <hook.h>
1314#include "kernel_trace.h"
1415
1516KPM_NAME ("kernel_trace" );
16- KPM_VERSION ("2.0 .0" );
17+ KPM_VERSION ("2.2 .0" );
1718KPM_LICENSE ("GPL v2" );
1819KPM_AUTHOR ("Test" );
1920KPM_DESCRIPTION ("use uprobe trace some fun in kpm" );
@@ -27,6 +28,8 @@ void (*path_put)(const struct path *path) = 0;
2728void (* rcu_read_unlock )(void ) = 0 ;
2829int (* trace_printk )(unsigned long ip , const char * fmt , ...) = 0 ;
2930
31+ void * show_map_vma_addr ;
32+
3033
3134char file_name [MAX_PATH_LEN ];
3235uid_t target_uid = -1 ;
@@ -37,6 +40,24 @@ static struct inode *inode;
3740unsigned long module_base = 0 ;
3841static struct uprobe_consumer trace_uc ;
3942
43+
44+
45+ void before_show_map_vma (hook_fargs2_t * args , void * udata )
46+ {
47+ struct seq_file * o_seq_file ;
48+ struct vm_area_struct * ovma ;
49+ unsigned long start , end ;
50+
51+ o_seq_file = (struct seq_file * )args -> arg0 ;
52+ ovma = (struct vm_area_struct * )args -> arg1 ;
53+ start = ovma -> vm_start ;
54+ end = ovma -> vm_end ;
55+ if (start == 0x7ffffff000 && end == 0x8000000000 ){
56+ logkd ("+Test-Log+ find uprobe item\n" );
57+ args -> skip_origin = 1 ;
58+ }
59+ }
60+
4061void before_mincore (hook_fargs3_t * args , void * udata ){
4162 int trace_flag = (int )syscall_argn (args , 1 );
4263 if (trace_flag < TRACE_FLAG || trace_flag > TRACE_FLAG + CLEAR_UPROBE ){
@@ -147,15 +168,15 @@ static int trace_handler(struct uprobe_consumer *self, struct mpt_regs *regs){
147168 }else {
148169 goto no_target_out ;
149170 }
150-
171+
151172target_out :
152173// logkd("+Test-Log+ fun_name:%s,fun_offset:0x%llx calling\n",tfun->value,fun_offset);
153174 int trace_printk_ret = trace_printk (_THIS_IP_ ,"+Test-Log+ fun_name:%s,fun_offset:0x%llx calling\n" ,tfun -> value ,fun_offset );
154175 if (unlikely (trace_printk_ret < 0 )){
155176 logke ("+Test-Log+ trace_printk error\n" );
156177 }
157178 return 0 ;
158-
179+
159180no_target_out :
160181 return 0 ;
161182}
@@ -178,9 +199,11 @@ static long kernel_trace_init(const char *args, const char *event, void *__user
178199 rb_first = (typeof (rb_first ))kallsyms_lookup_name ("rb_first" );
179200 kmalloc = (typeof (kmalloc ))kallsyms_lookup_name ("__kmalloc" );
180201 kfree = (typeof (kfree ))kallsyms_lookup_name ("kfree" );
181-
202+
182203 trace_printk = (typeof (trace_printk ))kallsyms_lookup_name ("__trace_printk" );
183204
205+ show_map_vma_addr = (void * )kallsyms_lookup_name ("show_map_vma" );
206+
184207 logkd ("+Test-Log+ mtask_pid_nr_ns:%llx\n" ,mtask_pid_nr_ns );
185208 logkd ("+Test-Log+ uprobe_register:%llx\n" ,uprobe_register );
186209 logkd ("+Test-Log+ uprobe_unregister:%llx\n" ,uprobe_unregister );
@@ -194,12 +217,15 @@ static long kernel_trace_init(const char *args, const char *event, void *__user
194217 logkd ("+Test-Log+ rb_first:%llx\n" ,rb_first );
195218 logkd ("+Test-Log+ kmalloc:%llx\n" ,kmalloc );
196219 logkd ("+Test-Log+ kfree:%llx\n" ,kfree );
197-
220+
198221 logkd ("+Test-Log+ trace_printk:%llx\n" ,trace_printk );
199222
223+ logkd ("+Test-Log+ show_map_vma_addr:%llx\n" ,show_map_vma_addr );
224+
200225 if (!(mtask_pid_nr_ns && uprobe_register && uprobe_unregister
201226 && kern_path && igrab && path_put && rcu_read_unlock
202- && rb_erase && rb_insert_color && rb_first && trace_printk )){
227+ && rb_erase && rb_insert_color && rb_first && trace_printk
228+ && show_map_vma_addr )){
203229 logke ("+Test-Log+ can not find some fun addr\n" );
204230 return -1 ;
205231 }
@@ -212,6 +238,12 @@ static long kernel_trace_init(const char *args, const char *event, void *__user
212238 return -1 ;
213239 }
214240
241+ err = hook_wrap2 (show_map_vma_addr , before_show_map_vma , NULL , 0 );
242+ if (err ){
243+ logke ("+Test-Log+ hook show_map_vma error\n" );
244+ return -1 ;
245+ }
246+
215247
216248 logkd ("+Test-Log+ success init\n" );
217249 return 0 ;
@@ -227,6 +259,7 @@ static long kernel_trace_control0(const char *args, char *__user out_msg, int ou
227259static long kernel_trace_exit (void * __user reserved )
228260{
229261 inline_unhook_syscall (__NR_mincore , before_mincore , 0 );
262+ unhook (show_map_vma_addr );
230263 rcu_read_unlock ();//解锁,不然内核会崩
231264 for (int i = 0 ; i < hook_num ; ++ i ) {
232265 uprobe_unregister (inode ,fun_offsets [i ],& trace_uc );
0 commit comments