forked from abcz316/SKRoot-linuxKernelRoot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaps_helper.h
More file actions
87 lines (70 loc) · 1.98 KB
/
maps_helper.h
File metadata and controls
87 lines (70 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifndef MAPS_HELPER_H_
#define MAPS_HELPER_H_
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
static std::string find_process_libc_so_path(pid_t pid) {
char line[1024] = { 0 };
std::string so_path;
char filename[32];
if (pid < 0) {
/* self process */
snprintf(filename, sizeof(filename), "/proc/self/maps");
} else {
snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
}
FILE* fp = fopen(filename, "r");
if (fp != NULL) {
while (fgets(line, sizeof(line), fp)) {
if (strstr(line, "libc.so")) {
char* start = strstr(line, "/");
if (start) {
start[strlen(start) - 1] = '\0';
so_path = start;
}
break;
}
}
fclose(fp);
}
return so_path;
}
//显然,这里面核心的就是get_module_base函数:
/*
此函数的功能就是通过遍历/proc/pid/maps文件,来找到目的module_name的内存映射起始地址。
由于内存地址的表达方式是startAddrxxxxxxx-endAddrxxxxxxx的,所以会在后面使用strtok(line,"-")来分割字符串
如果pid = -1,表示获取本地进程的某个模块的地址,
否则就是pid进程的某个模块的地址。
*/
static void* get_module_base(pid_t pid, const char* module_name) {
FILE* fp;
long addr = 0;
char* pch;
char filename[32];
char line[1024];
if (pid < 0) {
/* self process */
snprintf(filename, sizeof(filename), "/proc/self/maps");
} else {
snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
}
fp = fopen(filename, "r");
if (fp != NULL) {
while (fgets(line, sizeof(line), fp)) {
if (strstr(line, module_name)) {
//分解字符串为一组字符串。line为要分解的字符串,"-"为分隔符字符串。
pch = strtok(line, "-");
//将参数pch字符串根据参数base(表示进制)来转换成无符号的长整型数
addr = strtoull(pch, NULL, 16);
if (addr == 0x8000)
addr = 0;
break;
}
}
fclose(fp);
}
return (void*)addr;
}
#endif /* MAPS_HELPER_H_ */