-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathplugin.h
202 lines (183 loc) · 8.86 KB
/
plugin.h
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
* Copyright (c) 2024-2025 Qualcomm Innovation Center, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* SPDX-License-Identifier: GPL-2.0-only
*/
#ifndef PARSER_DEFS_H_
#define PARSER_DEFS_H_
#include <iostream>
#include <string>
#include <vector>
#include <linux/types.h>
#include <unistd.h>
#include <getopt.h>
#include <unordered_map>
#include <memory>
#include <functional>
#include <algorithm>
#include <bitset>
#include <set>
#include <regex.h>
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <iomanip>
#include <arpa/inet.h>
#include <inttypes.h>
#include "struct_info.h"
#include <sstream>
#include <gelf.h>
#define field_init(type,field_name) type_init(TO_STD_STRING(type),TO_STD_STRING(field_name))
#define field_size(type,field_name) type_size(TO_STD_STRING(type),TO_STD_STRING(field_name))
#define field_offset(type,field_name) type_offset(TO_STD_STRING(type),TO_STD_STRING(field_name))
#define struct_init(type) type_init(TO_STD_STRING(type))
#define struct_size(type) type_size(TO_STD_STRING(type))
#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
#define min(a, b) ((a) < (b) ? (a) : (b))
typedef struct {
long long counter;
} atomic64_t;
typedef struct {
int counter;
} atomic_t;
typedef struct {
int counter;
} atomic_long_t;
#define VM_NONE 0x00000000
#define VM_READ 0x00000001 /* currently active flags */
#define VM_WRITE 0x00000002
#define VM_EXEC 0x00000004
#define VM_SHARED 0x00000008
#define VM_MAYREAD 0x00000010 /* limits for mprotect() etc */
#define VM_MAYWRITE 0x00000020
#define VM_MAYEXEC 0x00000040
#define VM_MAYSHARE 0x00000080
#define VM_GROWSDOWN 0x00000100 /* general info on the segment */
#define VM_UFFD_MISSING 0x00000200 /* missing pages tracking */
#define VM_PFNMAP 0x00000400 /* Page-ranges managed without "struct page", just pure PFN */
#define VM_DENYWRITE 0x00000800 /* ETXTBSY on write attempts.. */
#define VM_UFFD_WP 0x00001000 /* wrprotect pages tracking */
#define VM_LOCKED 0x00002000
#define VM_IO 0x00004000 /* Memory mapped I/O or similar */
#define VM_HUGETLB 0x00400000
#define VM_DONTDUMP 0x04000000
class PaserPlugin {
private:
#if defined(ARM)
ulong* pmd_page_addr(ulong pmd);
#endif
protected:
std::unordered_map<std::string, std::unique_ptr<Typeinfo>> typetable;
static constexpr double KB = 1024.0;
static constexpr double MB = 1024.0 * 1024.0;
static constexpr double GB = 1024.0 * 1024.0 * 1024.0;
public:
PaserPlugin();
const size_t page_size = PAGESIZE();
const size_t page_shift = PAGESHIFT();
const size_t page_mask = ~(page_size - 1);
uint64_t vaddr_mask = 0;
std::string cmd_name;
std::vector<std::string> help_str_list;
char** cmd_help;
virtual void cmd_main(void)=0;
void initialize(void);
std::string csize(size_t size);
std::string csize(size_t size, int unit, int precision);
void print_table();
void type_init(const std::string& type);
void type_init(const std::string& type,const std::string& field);
int type_offset(const std::string& type,const std::string& field);
int type_size(const std::string& type,const std::string& field);
int type_size(const std::string& type);
std::vector<ulong> for_each_rbtree(ulong rb_root,int offset);
std::vector<ulong> for_each_list(ulong list_head,int offset);
std::vector<ulong> for_each_hlist(ulong hlist_head,int offset);
std::vector<ulong> for_each_xarray(ulong xarray_addr);
std::vector<ulong> for_each_mptree(ulong maptree_addr);
std::vector<ulong> for_each_radix(ulong root_rnode);
std::vector<ulong> for_each_process();
std::vector<ulong> for_each_threads();
std::vector<ulong> for_each_vma(ulong& task_addr);
ulonglong read_structure_field(ulong kvaddr,const std::string& type,const std::string& field);
std::string read_cstring(ulong kvaddr,int len, const std::string& note);
void* read_struct(ulong kvaddr,const std::string& type);
bool read_struct(ulong kvaddr,void* buf, int len, const std::string& type);
void* read_memory(ulong kvaddr,int len, const std::string& note);
void* read_phys_memory(ulong paddr, int len, const std::string& note);
ulong read_pointer(ulong kvaddr, const std::string& note);
short read_short(ulong kvaddr,const std::string& note);
ushort read_ushort(ulong kvaddr,const std::string& note);
ulonglong read_ulonglong(ulong kvaddr,const std::string& note);
ulong read_ulong(ulong kvaddr,const std::string& note);
long read_long(ulong kvaddr,const std::string& note);
uint read_uint(ulong kvaddr,const std::string& note);
int read_int(ulong kvaddr,const std::string& note);
bool read_bool(ulong kvaddr,const std::string& note);
unsigned char read_byte(ulong kvaddr, const std::string& note);
int csymbol_exists(const std::string& note);
ulong csymbol_value(const std::string& note);
bool is_kvaddr(ulong addr);
bool is_uvaddr(ulong addr, struct task_context *);
int page_to_nid(ulong page);
ulong virt_to_phy(ulong paddr);
ulong phy_to_virt(ulong vaddr);
ulong page_to_pfn(ulong page);
ulong pfn_to_page(ulong pfn);
ulong phy_to_page(ulong paddr);
physaddr_t page_to_phy(ulong page);
physaddr_t pfn_to_phy(ulong pfn);
ulong phy_to_pfn(ulong paddr);
std::string get_config_val(const std::string& conf_name);
void cfill_pgd(ulonglong pgd, int type, ulong size);
void cfill_pmd(ulonglong pmd, int type, ulong size);
void cfill_ptbl(ulonglong ptbl, int type, ulong size);
void print_backtrace();
bool is_binary_stripped(std::string& filename);
bool add_symbol_file(std::string& filename);
void verify_userspace_symbol(std::string& symbol_name);
bool isNumber(const std::string& str);
std::string extract_string(const char *input);
int is_bigendian(void);
long read_enum_val(const std::string& enum_name);
char get_printable(uint8_t d);
std::string print_line(uint64_t addr, const std::vector<uint8_t>& data);
std::string hexdump(uint64_t addr, const char* buf, size_t length, bool little_endian = true);
#if defined(ARM)
ulong get_arm_pte(ulong task_addr, ulong page_vaddr);
#endif
bool load_symbols(std::string& path, std::string name);
};
#define DEFINE_PLUGIN_INSTANCE(class_name) \
static std::shared_ptr<class_name> instance; \
static void wrapper_func() { \
if (instance) { \
instance->cmd_main(); \
} \
}
#ifndef BUILD_TARGET_TOGETHER
#define DEFINE_PLUGIN_COMMAND(class_name) \
extern "C" void class_name##_init(void); \
extern "C" void class_name##_fini(void); \
std::shared_ptr<class_name> class_name::instance = std::make_shared<class_name>(); \
static struct command_table_entry command_table[] = { \
{ &class_name::instance->cmd_name[0], &class_name::wrapper_func, class_name::instance->cmd_help, 0 }, \
{ NULL } \
}; \
extern "C" void __attribute__((constructor)) class_name##_init(void) { \
register_extension(command_table); \
} \
extern "C" void __attribute__((destructor)) class_name##_fini(void) { \
class_name::instance.reset(); \
}
#endif // BUILD_TARGET_TOGETHER
#endif // PARSER_DEFS_H_