Skip to content

Commit fc462ac

Browse files
liu-song-6acmel
authored andcommitted
perf bpf: Extract logic to create program names from perf_event__synthesize_one_bpf_prog()
Extract logic to create program names to synthesize_bpf_prog_name(), so that it can be reused in header.c:print_bpf_prog_info(). This commit doesn't change the behavior. Signed-off-by: Song Liu <[email protected]> Cc: Alexei Starovoitov <[email protected]> Cc: Daniel Borkmann <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Stanislav Fomichev <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent d56354d commit fc462ac

File tree

1 file changed

+35
-27
lines changed

1 file changed

+35
-27
lines changed

tools/perf/util/bpf-event.c

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,38 @@ static int perf_env__fetch_btf(struct perf_env *env,
111111
return 0;
112112
}
113113

114+
static int synthesize_bpf_prog_name(char *buf, int size,
115+
struct bpf_prog_info *info,
116+
struct btf *btf,
117+
u32 sub_id)
118+
{
119+
u8 (*prog_tags)[BPF_TAG_SIZE] = (void *)(uintptr_t)(info->prog_tags);
120+
void *func_infos = (void *)(uintptr_t)(info->func_info);
121+
u32 sub_prog_cnt = info->nr_jited_ksyms;
122+
const struct bpf_func_info *finfo;
123+
const char *short_name = NULL;
124+
const struct btf_type *t;
125+
int name_len;
126+
127+
name_len = snprintf(buf, size, "bpf_prog_");
128+
name_len += snprintf_hex(buf + name_len, size - name_len,
129+
prog_tags[sub_id], BPF_TAG_SIZE);
130+
if (btf) {
131+
finfo = func_infos + sub_id * info->func_info_rec_size;
132+
t = btf__type_by_id(btf, finfo->type_id);
133+
short_name = btf__name_by_offset(btf, t->name_off);
134+
} else if (sub_id == 0 && sub_prog_cnt == 1) {
135+
/* no subprog */
136+
if (info->name[0])
137+
short_name = info->name;
138+
} else
139+
short_name = "F";
140+
if (short_name)
141+
name_len += snprintf(buf + name_len, size - name_len,
142+
"_%s", short_name);
143+
return name_len;
144+
}
145+
114146
/*
115147
* Synthesize PERF_RECORD_KSYMBOL and PERF_RECORD_BPF_EVENT for one bpf
116148
* program. One PERF_RECORD_BPF_EVENT is generated for the program. And
@@ -135,7 +167,6 @@ static int perf_event__synthesize_one_bpf_prog(struct perf_session *session,
135167
struct bpf_prog_info_node *info_node;
136168
struct bpf_prog_info *info;
137169
struct btf *btf = NULL;
138-
bool has_btf = false;
139170
struct perf_env *env;
140171
u32 sub_prog_cnt, i;
141172
int err = 0;
@@ -189,19 +220,13 @@ static int perf_event__synthesize_one_bpf_prog(struct perf_session *session,
189220
btf = NULL;
190221
goto out;
191222
}
192-
has_btf = true;
193223
perf_env__fetch_btf(env, info->btf_id, btf);
194224
}
195225

196226
/* Synthesize PERF_RECORD_KSYMBOL */
197227
for (i = 0; i < sub_prog_cnt; i++) {
198-
u8 (*prog_tags)[BPF_TAG_SIZE] = (void *)(uintptr_t)(info->prog_tags);
199-
__u32 *prog_lens = (__u32 *)(uintptr_t)(info->jited_func_lens);
228+
__u32 *prog_lens = (__u32 *)(uintptr_t)(info->jited_func_lens);
200229
__u64 *prog_addrs = (__u64 *)(uintptr_t)(info->jited_ksyms);
201-
void *func_infos = (void *)(uintptr_t)(info->func_info);
202-
const struct bpf_func_info *finfo;
203-
const char *short_name = NULL;
204-
const struct btf_type *t;
205230
int name_len;
206231

207232
*ksymbol_event = (struct ksymbol_event){
@@ -214,26 +239,9 @@ static int perf_event__synthesize_one_bpf_prog(struct perf_session *session,
214239
.ksym_type = PERF_RECORD_KSYMBOL_TYPE_BPF,
215240
.flags = 0,
216241
};
217-
name_len = snprintf(ksymbol_event->name, KSYM_NAME_LEN,
218-
"bpf_prog_");
219-
name_len += snprintf_hex(ksymbol_event->name + name_len,
220-
KSYM_NAME_LEN - name_len,
221-
prog_tags[i], BPF_TAG_SIZE);
222-
if (has_btf) {
223-
finfo = func_infos + i * info->func_info_rec_size;
224-
t = btf__type_by_id(btf, finfo->type_id);
225-
short_name = btf__name_by_offset(btf, t->name_off);
226-
} else if (i == 0 && sub_prog_cnt == 1) {
227-
/* no subprog */
228-
if (info->name[0])
229-
short_name = info->name;
230-
} else
231-
short_name = "F";
232-
if (short_name)
233-
name_len += snprintf(ksymbol_event->name + name_len,
234-
KSYM_NAME_LEN - name_len,
235-
"_%s", short_name);
236242

243+
name_len = synthesize_bpf_prog_name(ksymbol_event->name,
244+
KSYM_NAME_LEN, info, btf, i);
237245
ksymbol_event->header.size += PERF_ALIGN(name_len + 1,
238246
sizeof(u64));
239247

0 commit comments

Comments
 (0)