-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathtrace_tracing.c
272 lines (223 loc) · 5.69 KB
/
trace_tracing.c
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <sys/sysinfo.h>
#include <parse_sym.h>
#include "trace.h"
#include "analysis.h"
/* check whether trampoline is supported by current arch */
static bool tracing_arch_supported()
{
return simple_exec("cat /proc/kallsyms | "
"grep arch_prepare_bpf_trampoline | "
"grep T") == 0;
}
static bool tracing_trace_supported()
{
#ifdef NO_BTF
goto failed;
#endif
/* for now, monitor mode only */
if (trace_ctx.mode != TRACE_MODE_MONITOR)
goto failed;
/* TRACING is not supported, skip this handle */
if (!libbpf_probe_bpf_prog_type(BPF_PROG_TYPE_TRACING, NULL))
goto failed;
if (!tracing_arch_supported()) {
pr_warn("trampoline is not supported, skip TRACING\n");
goto failed;
}
return true;
failed:
pr_verb("TRACING is not supported, trying others\n");
return false;
}
#ifndef NO_BTF
#include "progs/tracing.skel.h"
#include "progs/feat_args_ext.skel.h"
#define MAX_CPU_COUNT 1024
trace_ops_t tracing_ops;
static struct tracing *skel;
static bool tracing_support_feat_args_ext()
{
struct feat_args_ext *tmp;
int err;
tmp = feat_args_ext__open_and_load();
if (tmp == NULL)
return false;
err = feat_args_ext__attach(tmp);
feat_args_ext__destroy(tmp);
return err == 0;
}
static void tracing_adjust_target()
{
struct bpf_program *prog;
trace_t *trace;
trace_for_each(trace) {
if (!(trace->status & TRACE_ATTACH_MANUAL))
continue;
prog = bpf_pbn(trace_ctx.obj, trace->prog);
/* function name contain "." is not supported by BTF */
if (prog && strchr(trace->name, '.')) {
trace_set_invalid_reason(trace, "BTF invalid");
bpf_program__set_autoload(prog, false);
}
#if 0
tracing_trace_attach_manual(trace->prog, trace->name);
if (!trace_is_ret(trace))
continue;
sprintf(kret_name, "ret%s", trace->prog);
tracing_trace_attach_manual(kret_name, trace->name);
#endif
}
}
static int tracing_trace_attach()
{
return tracing__attach(skel);
}
static void tracing_load_rules()
{
rule_t *local_rule;
rules_ret_t *rule;
trace_t *trace;
int i;
trace_for_each(trace) {
if (trace_is_invalid(trace) || !trace_is_enable(trace) ||
!trace_is_ret(trace) || !trace_is_func(trace))
continue;
rule = &skel->bss->rules_all[trace->index];
i = 0;
list_for_each_entry(local_rule, &trace->rules, list) {
if (local_rule->level == RULE_INFO)
continue;
rule->expected[i] = local_rule->expected;
rule->op[i] = local_rule->type;
i++;
}
}
}
static void tracing_check_args()
{
bool support_feat_args_ext, support_btf_modules;
trace_t *trace;
support_feat_args_ext = tracing_support_feat_args_ext();
if (!support_feat_args_ext)
pr_warn("tracing kernel function with 6+ arguments is not"
"supportd by your kernel, following functions "
"are skipped:\n");
trace_for_each(trace) {
if (trace_is_invalid(trace) || !trace_is_enable(trace) ||
!trace_is_func(trace))
continue;
if (!support_feat_args_ext && trace->arg_count > 6) {
pr_warn("\t%s\n", trace->name);
trace_set_invalid(trace);
}
}
support_btf_modules = kernel_has_config("DEBUG_INFO_BTF_MODULES");
if (!support_btf_modules)
pr_warn("CONFIG_DEBUG_INFO_BTF_MODULES is not supported "
"by your kernel, following functions are "
"skipped:\n");
trace_for_each(trace) {
if (trace_is_invalid(trace) || !trace_is_enable(trace) ||
!trace_is_func(trace))
continue;
if (!support_btf_modules && !btf_get_type(trace->name)) {
pr_warn("\t%s\n", trace->name);
trace_set_invalid(trace);
}
}
}
static int tracing_trace_load()
{
DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts,
.btf_custom_path = trace_ctx.args.btf_path,
);
skel = tracing__open_opts(&opts);
if (!skel) {
pr_err("failed to open tracing-based eBPF\n");
goto err;
}
pr_debug("eBPF is opened successfully\n");
/* set the max entries of perf event map to current cpu count */
bpf_map__set_max_entries(skel->maps.m_event, get_nprocs_conf());
bpf_func_init(skel, BPF_PROG_TYPE_TRACING);
trace_ctx.obj = skel->obj;
tracing_load_rules();
tracing_check_args();
if (trace_pre_load()) {
pr_err("failed to prepare load\n");
goto err;
}
tracing_adjust_target();
if (tracing__load(skel)) {
pr_err("failed to load tracing-based eBPF\n");
goto err;
}
pr_debug("eBPF is loaded successfully\n");
bpf_set_config(skel, bss, trace_ctx.bpf_args);
return 0;
err:
return -1;
}
void tracing_trace_close()
{
if (skel)
tracing__destroy(skel);
skel = NULL;
}
static analyzer_result_t
tracing_analy_exit(trace_t *trace, analy_exit_t *e)
{
return RESULT_CONT;
}
static analyzer_result_t
tracing_analy_entry(trace_t *trace, analy_entry_t *e)
{
return RESULT_CONT;
}
static void tracing_trace_ready()
{
bpf_set_config_field(skel, bss, bpf_args_t, ready, true);
}
static void tracing_print_stack(int key)
{
if (key <= 0)
{
pr_info("Call Stack Error! Invalid stack id:%d.\n", key);
return;
}
int map_fd = bpf_map__fd(skel->maps.m_stack);
__u64 ip[PERF_MAX_STACK_DEPTH] = {};
struct sym_result *sym;
int i = 0;
if (bpf_map_lookup_elem(map_fd, &key, ip)) {
pr_info("Call Stack Error!\n");
return;
}
pr_info("Call Stack:\n");
for (; i < PERF_MAX_STACK_DEPTH && ip[i]; i++) {
sym = sym_parse(ip[i]);
if (!sym)
break;
pr_info(" -> [%llx]%s\n", ip[i], sym->desc);
}
pr_info("\n");
}
analyzer_t tracing_analyzer = {
.mode = TRACE_MODE_CTX_MASK,
.analy_entry = tracing_analy_entry,
.analy_exit = tracing_analy_exit,
};
trace_ops_t tracing_ops = {
.trace_attach = tracing_trace_attach,
.trace_load = tracing_trace_load,
.trace_close = tracing_trace_close,
.trace_ready = tracing_trace_ready,
.trace_supported = tracing_trace_supported,
.print_stack = tracing_print_stack,
.analyzer = &tracing_analyzer,
};
#else
trace_ops_t tracing_ops = {
.trace_supported = tracing_trace_supported,
};
#endif