Skip to content

Commit 5580338

Browse files
Jin Yaoacmel
authored andcommitted
perf report: Refactor common code in srcline.c
Introduce dso__name() and filename_split() out of existing code because these codes will be used in several places in next patch. For filename_split(), it may also solve a potential memory leak in existing code. In existing addr2line(), sep = strchr(filename, ':'); if (sep) { *sep++ = '\0'; *file = filename; *line_nr = strtoul(sep, NULL, 0); ret = 1; } out: pclose(fp); return ret; If sep is NULL, filename is not freed or returned via file. Signed-off-by: Yao Jin <[email protected]> Tested-by: Milian Wolff <[email protected]> Cc: Andi Kleen <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Kan Liang <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent b0ad8ea commit 5580338

File tree

1 file changed

+45
-23
lines changed

1 file changed

+45
-23
lines changed

tools/perf/util/srcline.c

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,24 @@
1212

1313
bool srcline_full_filename;
1414

15+
static const char *dso__name(struct dso *dso)
16+
{
17+
const char *dso_name;
18+
19+
if (dso->symsrc_filename)
20+
dso_name = dso->symsrc_filename;
21+
else
22+
dso_name = dso->long_name;
23+
24+
if (dso_name[0] == '[')
25+
return NULL;
26+
27+
if (!strncmp(dso_name, "/tmp/perf-", 10))
28+
return NULL;
29+
30+
return dso_name;
31+
}
32+
1533
#ifdef HAVE_LIBBFD_SUPPORT
1634

1735
/*
@@ -207,6 +225,27 @@ void dso__free_a2l(struct dso *dso)
207225

208226
#else /* HAVE_LIBBFD_SUPPORT */
209227

228+
static int filename_split(char *filename, unsigned int *line_nr)
229+
{
230+
char *sep;
231+
232+
sep = strchr(filename, '\n');
233+
if (sep)
234+
*sep = '\0';
235+
236+
if (!strcmp(filename, "??:0"))
237+
return 0;
238+
239+
sep = strchr(filename, ':');
240+
if (sep) {
241+
*sep++ = '\0';
242+
*line_nr = strtoul(sep, NULL, 0);
243+
return 1;
244+
}
245+
246+
return 0;
247+
}
248+
210249
static int addr2line(const char *dso_name, u64 addr,
211250
char **file, unsigned int *line_nr,
212251
struct dso *dso __maybe_unused,
@@ -216,7 +255,6 @@ static int addr2line(const char *dso_name, u64 addr,
216255
char cmd[PATH_MAX];
217256
char *filename = NULL;
218257
size_t len;
219-
char *sep;
220258
int ret = 0;
221259

222260
scnprintf(cmd, sizeof(cmd), "addr2line -e %s %016"PRIx64,
@@ -233,23 +271,14 @@ static int addr2line(const char *dso_name, u64 addr,
233271
goto out;
234272
}
235273

236-
sep = strchr(filename, '\n');
237-
if (sep)
238-
*sep = '\0';
239-
240-
if (!strcmp(filename, "??:0")) {
241-
pr_debug("no debugging info in %s\n", dso_name);
274+
ret = filename_split(filename, line_nr);
275+
if (ret != 1) {
242276
free(filename);
243277
goto out;
244278
}
245279

246-
sep = strchr(filename, ':');
247-
if (sep) {
248-
*sep++ = '\0';
249-
*file = filename;
250-
*line_nr = strtoul(sep, NULL, 0);
251-
ret = 1;
252-
}
280+
*file = filename;
281+
253282
out:
254283
pclose(fp);
255284
return ret;
@@ -278,15 +307,8 @@ char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
278307
if (!dso->has_srcline)
279308
goto out;
280309

281-
if (dso->symsrc_filename)
282-
dso_name = dso->symsrc_filename;
283-
else
284-
dso_name = dso->long_name;
285-
286-
if (dso_name[0] == '[')
287-
goto out;
288-
289-
if (!strncmp(dso_name, "/tmp/perf-", 10))
310+
dso_name = dso__name(dso);
311+
if (dso_name == NULL)
290312
goto out;
291313

292314
if (!addr2line(dso_name, addr, &file, &line, dso, unwind_inlines))

0 commit comments

Comments
 (0)