Skip to content

Commit 5cb113f

Browse files
mhiramathitachiacmel
authored andcommitted
perf buildid-cache: Consolidate .build-id cache path generators
Consolidate .build-id cache path generating routines to build_id__filename() function. Other functions must use it to get the buildid cache path (link path) from build-id. This can reduce the risk of partial-update. Signed-off-by: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> Acked-by: Namhyung Kim <namhyung@kernel.org> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Borislav Petkov <bp@suse.de> Cc: Hemant Kumar <hemant@linux.vnet.ibm.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Paul Mackerras <paulus@samba.org> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Link: http://lkml.kernel.org/r/20150210091853.19264.58513.stgit@localhost.localdomain Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent e35f736 commit 5cb113f

1 file changed

Lines changed: 41 additions & 17 deletions

File tree

‎tools/perf/util/build-id.c‎

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,35 @@ int build_id__sprintf(const u8 *build_id, int len, char *bf)
9393
return raw - build_id;
9494
}
9595

96+
/* asnprintf consolidates asprintf and snprintf */
97+
static int asnprintf(char **strp, size_t size, const char *fmt, ...)
98+
{
99+
va_list ap;
100+
int ret;
101+
102+
if (!strp)
103+
return -EINVAL;
104+
105+
va_start(ap, fmt);
106+
if (*strp)
107+
ret = vsnprintf(*strp, size, fmt, ap);
108+
else
109+
ret = vasprintf(strp, fmt, ap);
110+
va_end(ap);
111+
112+
return ret;
113+
}
114+
115+
static char *build_id__filename(const char *sbuild_id, char *bf, size_t size)
116+
{
117+
char *tmp = bf;
118+
int ret = asnprintf(&bf, size, "%s/.build-id/%.2s/%s", buildid_dir,
119+
sbuild_id, sbuild_id + 2);
120+
if (ret < 0 || (tmp && size < (unsigned int)ret))
121+
return NULL;
122+
return bf;
123+
}
124+
96125
char *dso__build_id_filename(const struct dso *dso, char *bf, size_t size)
97126
{
98127
char build_id_hex[BUILD_ID_SIZE * 2 + 1];
@@ -101,14 +130,7 @@ char *dso__build_id_filename(const struct dso *dso, char *bf, size_t size)
101130
return NULL;
102131

103132
build_id__sprintf(dso->build_id, sizeof(dso->build_id), build_id_hex);
104-
if (bf == NULL) {
105-
if (asprintf(&bf, "%s/.build-id/%.2s/%s", buildid_dir,
106-
build_id_hex, build_id_hex + 2) < 0)
107-
return NULL;
108-
} else
109-
snprintf(bf, size, "%s/.build-id/%.2s/%s", buildid_dir,
110-
build_id_hex, build_id_hex + 2);
111-
return bf;
133+
return build_id__filename(build_id_hex, bf, size);
112134
}
113135

114136
#define dsos__for_each_with_build_id(pos, head) \
@@ -264,7 +286,7 @@ int build_id_cache__add_s(const char *sbuild_id, const char *name,
264286
{
265287
const size_t size = PATH_MAX;
266288
char *realname, *filename = zalloc(size),
267-
*linkname = zalloc(size), *targetname;
289+
*linkname = zalloc(size), *targetname, *tmp;
268290
int len, err = -1;
269291
bool slash = is_kallsyms || is_vdso;
270292

@@ -297,13 +319,15 @@ int build_id_cache__add_s(const char *sbuild_id, const char *name,
297319
goto out_free;
298320
}
299321

300-
len = scnprintf(linkname, size, "%s/.build-id/%.2s",
301-
buildid_dir, sbuild_id);
322+
if (!build_id__filename(sbuild_id, linkname, size))
323+
goto out_free;
324+
tmp = strrchr(linkname, '/');
325+
*tmp = '\0';
302326

303327
if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
304328
goto out_free;
305329

306-
snprintf(linkname + len, size - len, "/%s", sbuild_id + 2);
330+
*tmp = '/';
307331
targetname = filename + strlen(buildid_dir) - 5;
308332
memcpy(targetname, "../..", 5);
309333

@@ -332,14 +356,14 @@ int build_id_cache__remove_s(const char *sbuild_id)
332356
{
333357
const size_t size = PATH_MAX;
334358
char *filename = zalloc(size),
335-
*linkname = zalloc(size);
359+
*linkname = zalloc(size), *tmp;
336360
int err = -1;
337361

338362
if (filename == NULL || linkname == NULL)
339363
goto out_free;
340364

341-
snprintf(linkname, size, "%s/.build-id/%.2s/%s",
342-
buildid_dir, sbuild_id, sbuild_id + 2);
365+
if (!build_id__filename(sbuild_id, linkname, size))
366+
goto out_free;
343367

344368
if (access(linkname, F_OK))
345369
goto out_free;
@@ -353,8 +377,8 @@ int build_id_cache__remove_s(const char *sbuild_id)
353377
/*
354378
* Since the link is relative, we must make it absolute:
355379
*/
356-
snprintf(linkname, size, "%s/.build-id/%.2s/%s",
357-
buildid_dir, sbuild_id, filename);
380+
tmp = strrchr(linkname, '/') + 1;
381+
snprintf(tmp, size - (tmp - linkname), "%s", filename);
358382

359383
if (unlink(linkname))
360384
goto out_free;

0 commit comments

Comments
 (0)