Skip to content

Commit cca6637

Browse files
修改提供给用户使用的接口
1 parent 02b279e commit cca6637

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

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

14-
**set_fun_info**函数用于在so文件的指定偏移处设置uprobe挂载点,并可传递指定函数名
14+
**set_fun_info**函数第一个参数是要设置函数的uprobe偏移,计算方法见文末,第二个参数为函数偏移,第三个参数为自定义函数名
1515

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

kernel_trace.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "kernel_trace.h"
1515

1616
KPM_NAME("kernel_trace");
17-
KPM_VERSION("2.2.0");
17+
KPM_VERSION("2.3.0");
1818
KPM_LICENSE("GPL v2");
1919
KPM_AUTHOR("Test");
2020
KPM_DESCRIPTION("use uprobe trace some fun in kpm");
@@ -85,15 +85,21 @@ void before_mincore(hook_fargs3_t *args, void *udata){
8585
logke("+Test-Log+ same fun 0x%llx set uprobe\n",fun_offset);
8686
goto error_out;
8787
}
88-
// logkd("+Test-Log+ fun_name:%s,fun_offset:0x%llx\n",fun_name,fun_offset);
89-
int hret = uprobe_register(inode,fun_offset,&trace_uc);
88+
logkd("+Test-Log+ fun_name:%s,fun_offset:%llx\n",fun_name,fun_offset);
89+
goto success_out;
90+
}
91+
92+
if(trace_info==SET_TARGET_UPROBE){
93+
unsigned long rfun_offset = (unsigned long)syscall_argn(args, 0);
94+
int hret = uprobe_register(inode,rfun_offset,&trace_uc);
9095
if(hret<0){
91-
logke("+Test-Log+ set uprobe error in 0x%llx\n",fun_offset);
96+
logke("+Test-Log+ set uprobe error in 0x%llx\n",rfun_offset);
9297
goto error_out;
9398
}
9499

95-
fun_offsets[hook_num] = fun_offset;
100+
fun_offsets[hook_num] = rfun_offset;
96101
hook_num++;
102+
logkd("+Test-Log+ rfun_offset:%llx\n",rfun_offset);
97103
goto success_out;
98104
}
99105

@@ -157,13 +163,8 @@ static int trace_handler(struct uprobe_consumer *self, struct mpt_regs *regs){
157163
if(uid==target_uid){
158164
fun_offset = regs->pc-module_base;
159165
tfun = search_key_value(&fun_info_tree,fun_offset);
160-
if(tfun){
166+
if(likely(tfun)){
161167
goto target_out;
162-
}else{
163-
tfun = search_key_value(&fun_info_tree,fun_offset- 0x1000);
164-
if(likely(tfun)){
165-
goto target_out;
166-
}
167168
}
168169
}else{
169170
goto no_target_out;

uprobe_trace.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ enum trace_info {
77
SET_TARGET_FILE,
88
SET_MODULE_BASE,
99
SET_FUN_INFO,
10+
SET_TARGET_UPROBE,
1011
SET_TARGET_UID,
1112
CLEAR_UPROBE,
1213
};

user/uprobe_trace_user.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ enum trace_info {
1111
SET_TARGET_FILE,
1212
SET_MODULE_BASE,
1313
SET_FUN_INFO,
14+
SET_TARGET_UPROBE,
1415
SET_TARGET_UID,
1516
CLEAR_UPROBE,
1617
};
@@ -32,9 +33,13 @@ int set_target_file(char* file_name){
3233
return ret;
3334
}
3435

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);
37-
return ret;
36+
int set_fun_info(unsigned long uprobe_offset,unsigned long fun_offset,char *fun_name){
37+
int insert_key_ret = syscall(__NR_mincore,fun_offset,TRACE_FLAG+SET_FUN_INFO,fun_name);
38+
if(insert_key_ret==SET_TRACE_SUCCESS){
39+
int ret = syscall(__NR_mincore,uprobe_offset,TRACE_FLAG+SET_TARGET_UPROBE,"");
40+
return ret;
41+
}
42+
return SET_TRACE_ERROR;
3843
}
3944

4045
int clear_all_uprobes(){

0 commit comments

Comments
 (0)