Skip to content

Commit a57ca8b

Browse files
做了一些优化
1 parent 1f0df30 commit a57ca8b

File tree

5 files changed

+55
-31
lines changed

5 files changed

+55
-31
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44

55
# 如何使用
6-
在成功加载本项目的kpm模块后(可通过**dmesg | grep +Test-Log+**命令查看详细日志),再使用项目user目录下的uprobe_trace_user.h文件提供的用户层接口进行编程即可。
6+
在成功加载本项目的kpm模块后可通过 **dmesg | grep +Test-Log+** 命令查看模块日志,再使用项目user目录下的uprobe_trace_user.h文件提供的用户层接口进行编程即可。trace的输出结果在tracefs文件系统下,可通过 **mount | grep tracefs** 命令查看tracefs所在位置,一般都是在/sys/kernel/tracing,通过 **echo "1" >> /sys/kernel/tracing/tracing_on** 开启日志后通过 **cat /sys/kernel/tracing/trace_pipe | grep +Test-Log+** 查看trace的结果
77

88
**set_module_base**函数用于设置要hook so的基址(打印函数偏移需要用到)。
99

1010
**set_target_uid**函数用于设置要hook的app的uid(过滤输出需要)。
1111

1212
**set_target_file**函数用于设置要hook so的路径(必须是完整路径)。
1313

14-
**set_fun_offset**函数用于在so文件的指定偏移处设置uprobe挂载点,并可传递指定函数名。
14+
**set_fun_info**函数用于在so文件的指定偏移处设置uprobe挂载点,并可传递指定函数名。
1515

1616
**clear_all_uprobes**函数用于清除所有的uprobe挂载点。
1717

kernel_trace.c

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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>
@@ -12,7 +13,7 @@
1213
#include "kernel_trace.h"
1314

1415
KPM_NAME("kernel_trace");
15-
KPM_VERSION("1.0.0");
16+
KPM_VERSION("2.0.0");
1617
KPM_LICENSE("GPL v2");
1718
KPM_AUTHOR("Test");
1819
KPM_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;
2425
struct inode *(*igrab)(struct inode *inode) = 0;
2526
void (*path_put)(const struct path *path) = 0;
2627
void (*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

3031
char 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

kernel_trace.h

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "uprobe_trace.h"
44
#include "mrbtree.h"
55

6+
#define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; })
67
#define MAX_PATH_LEN 300
78
#define MAX_FUN_NAME 150
89
#define LOOKUP_FOLLOW 0x0001
@@ -48,23 +49,23 @@ enum uprobe_filter_ctx {
4849
UPROBE_FILTER_MMAP,
4950
};
5051

51-
//struct mpt_regs {
52-
// union {
53-
// struct user_pt_regs user_regs;
54-
// struct {
55-
// u64 regs[31];
56-
// u64 sp;
57-
// u64 pc;
58-
// u64 pstate;
59-
// };
60-
// };
61-
//};
52+
struct mpt_regs {
53+
union {
54+
struct user_pt_regs user_regs;
55+
struct {
56+
u64 regs[31];
57+
u64 sp;
58+
u64 pc;
59+
u64 pstate;
60+
};
61+
};
62+
};
6263

6364
struct uprobe_consumer {
64-
int (*handler)(struct uprobe_consumer *self, struct pt_regs *regs);
65+
int (*handler)(struct uprobe_consumer *self, struct mpt_regs *regs);
6566
int (*ret_handler)(struct uprobe_consumer *self,
6667
unsigned long func,
67-
struct pt_regs *regs);
68+
struct mpt_regs *regs);
6869
bool (*filter)(struct uprobe_consumer *self,
6970
enum uprobe_filter_ctx ctx,
7071
struct mm_struct *mm);

uprobe_trace.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
enum trace_info {
77
SET_TARGET_FILE,
88
SET_MODULE_BASE,
9-
SET_MODULE_OFFSET,
9+
SET_FUN_INFO,
1010
SET_TARGET_UID,
1111
CLEAR_UPROBE,
1212
};

user/uprobe_trace_user.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
enum trace_info {
1111
SET_TARGET_FILE,
1212
SET_MODULE_BASE,
13-
SET_MODULE_OFFSET,
13+
SET_FUN_INFO,
1414
SET_TARGET_UID,
1515
CLEAR_UPROBE,
1616
};
@@ -32,8 +32,8 @@ int set_target_file(char* file_name){
3232
return ret;
3333
}
3434

35-
int set_fun_offset(unsigned long fun_offset,char *fun_name){
36-
int ret = syscall(__NR_mincore,fun_offset,TRACE_FLAG+SET_MODULE_OFFSET,fun_name);
35+
int set_fun_info(unsigned long fun_offset,char *fun_name){
36+
int ret = syscall(__NR_mincore,fun_offset,TRACE_FLAG+SET_FUN_INFO,fun_name);
3737
return ret;
3838
}
3939

0 commit comments

Comments
 (0)