perf buildid-cache: Support binary objects from other namespaces
[linux-2.6-block.git] / tools / perf / util / util.c
CommitLineData
1aed2671 1#include "../perf.h"
4cf40131 2#include "util.h"
84f5d36f 3#include "debug.h"
cd0cfad7 4#include <api/fs/fs.h>
69e3f52d 5#include <sys/mman.h>
7a8ef4c4 6#include <sys/stat.h>
07bc5c69 7#include <sys/utsname.h>
76b31a29 8#include <dirent.h>
fd20e811 9#include <inttypes.h>
9607ad3a 10#include <signal.h>
dc4552bf
ACM
11#include <stdio.h>
12#include <stdlib.h>
cef82c9f
JO
13#include <string.h>
14#include <errno.h>
1a47245d 15#include <limits.h>
838d1452 16#include <linux/kernel.h>
c339b1a9 17#include <linux/log2.h>
bd48c63e 18#include <linux/time64.h>
9398c484 19#include <unistd.h>
14cbfbeb 20#include "strlist.h"
23aadb1f 21
1aed2671
JR
22/*
23 * XXX We need to find a better place for these things...
24 */
0c1fe6b2 25unsigned int page_size;
2b1b7100 26int cacheline_size;
0c1fe6b2 27
a29d5c9b
ACM
28int sysctl_perf_event_max_stack = PERF_MAX_STACK_DEPTH;
29int sysctl_perf_event_max_contexts_per_stack = PERF_MAX_CONTEXTS_PER_STACK;
4cb93446 30
0c6332e9
ACM
31bool test_attr__enabled;
32
1aed2671 33bool perf_host = true;
c4a7dca9 34bool perf_guest = false;
1aed2671
JR
35
36void event_attr_init(struct perf_event_attr *attr)
37{
38 if (!perf_host)
39 attr->exclude_host = 1;
40 if (!perf_guest)
41 attr->exclude_guest = 1;
7e1ccd38
SE
42 /* to capture ABI version */
43 attr->size = sizeof(*attr);
1aed2671
JR
44}
45
4cf40131
ACM
46int mkdir_p(char *path, mode_t mode)
47{
48 struct stat st;
49 int err;
50 char *d = path;
51
52 if (*d != '/')
53 return -1;
54
55 if (stat(path, &st) == 0)
56 return 0;
57
58 while (*++d == '/');
59
60 while ((d = strchr(d, '/'))) {
61 *d = '\0';
62 err = stat(path, &st) && mkdir(path, mode);
63 *d++ = '/';
64 if (err)
65 return -1;
66 while (*d == '/')
67 ++d;
68 }
69 return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
70}
71
9a9c733d 72int rm_rf(const char *path)
0b1de0be
NK
73{
74 DIR *dir;
75 int ret = 0;
76 struct dirent *d;
77 char namebuf[PATH_MAX];
78
79 dir = opendir(path);
80 if (dir == NULL)
81 return 0;
82
83 while ((d = readdir(dir)) != NULL && !ret) {
84 struct stat statbuf;
85
86 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
87 continue;
88
89 scnprintf(namebuf, sizeof(namebuf), "%s/%s",
90 path, d->d_name);
91
2a1ef032
MH
92 /* We have to check symbolic link itself */
93 ret = lstat(namebuf, &statbuf);
0b1de0be
NK
94 if (ret < 0) {
95 pr_debug("stat failed: %s\n", namebuf);
96 break;
97 }
98
2a1ef032 99 if (S_ISDIR(statbuf.st_mode))
0b1de0be 100 ret = rm_rf(namebuf);
2a1ef032
MH
101 else
102 ret = unlink(namebuf);
0b1de0be
NK
103 }
104 closedir(dir);
105
106 if (ret < 0)
107 return ret;
108
109 return rmdir(path);
110}
111
e1ce726e
MH
112/* A filter which removes dot files */
113bool lsdir_no_dot_filter(const char *name __maybe_unused, struct dirent *d)
114{
115 return d->d_name[0] != '.';
116}
117
118/* lsdir reads a directory and store it in strlist */
119struct strlist *lsdir(const char *name,
120 bool (*filter)(const char *, struct dirent *))
121{
122 struct strlist *list = NULL;
123 DIR *dir;
124 struct dirent *d;
125
126 dir = opendir(name);
127 if (!dir)
128 return NULL;
129
130 list = strlist__new(NULL, NULL);
131 if (!list) {
357a54f3 132 errno = ENOMEM;
e1ce726e
MH
133 goto out;
134 }
135
136 while ((d = readdir(dir)) != NULL) {
137 if (!filter || filter(name, d))
138 strlist__add(list, d->d_name);
139 }
140
141out:
142 closedir(dir);
143 return list;
144}
145
f045b8c4 146static int slow_copyfile(const char *from, const char *to, struct nsinfo *nsi)
9e201442 147{
9a17d726 148 int err = -1;
9e201442
ACM
149 char *line = NULL;
150 size_t n;
f045b8c4
KJ
151 FILE *from_fp, *to_fp;
152 struct nscookie nsc;
9e201442 153
f045b8c4
KJ
154 nsinfo__mountns_enter(nsi, &nsc);
155 from_fp = fopen(from, "r");
156 nsinfo__mountns_exit(&nsc);
9e201442
ACM
157 if (from_fp == NULL)
158 goto out;
159
160 to_fp = fopen(to, "w");
161 if (to_fp == NULL)
162 goto out_fclose_from;
163
164 while (getline(&line, &n, from_fp) > 0)
165 if (fputs(line, to_fp) == EOF)
166 goto out_fclose_to;
167 err = 0;
168out_fclose_to:
169 fclose(to_fp);
170 free(line);
171out_fclose_from:
172 fclose(from_fp);
173out:
174 return err;
175}
176
9c9f5a2f
NK
177int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size)
178{
179 void *ptr;
180 loff_t pgoff;
181
182 pgoff = off_in & ~(page_size - 1);
183 off_in -= pgoff;
184
185 ptr = mmap(NULL, off_in + size, PROT_READ, MAP_PRIVATE, ifd, pgoff);
186 if (ptr == MAP_FAILED)
187 return -1;
188
189 while (size) {
190 ssize_t ret = pwrite(ofd, ptr + off_in, size, off_out);
191 if (ret < 0 && errno == EINTR)
192 continue;
193 if (ret <= 0)
194 break;
195
196 size -= ret;
197 off_in += ret;
198 off_out -= ret;
199 }
200 munmap(ptr, off_in + size);
201
202 return size ? -1 : 0;
203}
204
f045b8c4
KJ
205static int copyfile_mode_ns(const char *from, const char *to, mode_t mode,
206 struct nsinfo *nsi)
4cf40131
ACM
207{
208 int fromfd, tofd;
209 struct stat st;
f045b8c4 210 int err;
d7c72606 211 char *tmp = NULL, *ptr = NULL;
f045b8c4 212 struct nscookie nsc;
4cf40131 213
f045b8c4
KJ
214 nsinfo__mountns_enter(nsi, &nsc);
215 err = stat(from, &st);
216 nsinfo__mountns_exit(&nsc);
217 if (err)
4cf40131 218 goto out;
f045b8c4 219 err = -1;
4cf40131 220
d7c72606
MV
221 /* extra 'x' at the end is to reserve space for '.' */
222 if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) {
223 tmp = NULL;
4cf40131 224 goto out;
d7c72606
MV
225 }
226 ptr = strrchr(tmp, '/');
227 if (!ptr)
228 goto out;
229 ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1);
230 *ptr = '.';
4cf40131 231
d7c72606 232 tofd = mkstemp(tmp);
4cf40131 233 if (tofd < 0)
d7c72606
MV
234 goto out;
235
236 if (fchmod(tofd, mode))
237 goto out_close_to;
238
239 if (st.st_size == 0) { /* /proc? do it slowly... */
f045b8c4 240 err = slow_copyfile(from, tmp, nsi);
d7c72606
MV
241 goto out_close_to;
242 }
243
f045b8c4 244 nsinfo__mountns_enter(nsi, &nsc);
d7c72606 245 fromfd = open(from, O_RDONLY);
f045b8c4 246 nsinfo__mountns_exit(&nsc);
d7c72606
MV
247 if (fromfd < 0)
248 goto out_close_to;
4cf40131 249
9c9f5a2f 250 err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size);
4cf40131 251
4cf40131 252 close(fromfd);
d7c72606
MV
253out_close_to:
254 close(tofd);
255 if (!err)
256 err = link(tmp, to);
257 unlink(tmp);
4cf40131 258out:
d7c72606 259 free(tmp);
4cf40131
ACM
260 return err;
261}
c82ee828 262
f045b8c4
KJ
263int copyfile_ns(const char *from, const char *to, struct nsinfo *nsi)
264{
265 return copyfile_mode_ns(from, to, 0755, nsi);
266}
267
268int copyfile_mode(const char *from, const char *to, mode_t mode)
269{
270 return copyfile_mode_ns(from, to, mode, NULL);
271}
272
9a17d726
AH
273int copyfile(const char *from, const char *to)
274{
275 return copyfile_mode(from, to, 0755);
276}
277
bc3a502b 278static ssize_t ion(bool is_read, int fd, void *buf, size_t n)
1e7972cc
ACM
279{
280 void *buf_start = buf;
838d1452 281 size_t left = n;
1e7972cc 282
838d1452 283 while (left) {
bc3a502b
JO
284 ssize_t ret = is_read ? read(fd, buf, left) :
285 write(fd, buf, left);
1e7972cc 286
e148c760
NK
287 if (ret < 0 && errno == EINTR)
288 continue;
1e7972cc
ACM
289 if (ret <= 0)
290 return ret;
291
838d1452
JO
292 left -= ret;
293 buf += ret;
1e7972cc
ACM
294 }
295
838d1452
JO
296 BUG_ON((size_t)(buf - buf_start) != n);
297 return n;
1e7972cc 298}
61e04b33 299
bc3a502b
JO
300/*
301 * Read exactly 'n' bytes or return an error.
302 */
303ssize_t readn(int fd, void *buf, size_t n)
304{
305 return ion(true, fd, buf, n);
306}
307
308/*
309 * Write exactly 'n' bytes or return an error.
310 */
311ssize_t writen(int fd, void *buf, size_t n)
312{
313 return ion(false, fd, buf, n);
314}
315
61e04b33
ACM
316size_t hex_width(u64 v)
317{
318 size_t n = 1;
319
320 while ((v >>= 4))
321 ++n;
322
323 return n;
324}
dc4552bf 325
b2aff5f6
JO
326static int hex(char ch)
327{
328 if ((ch >= '0') && (ch <= '9'))
329 return ch - '0';
330 if ((ch >= 'a') && (ch <= 'f'))
331 return ch - 'a' + 10;
332 if ((ch >= 'A') && (ch <= 'F'))
333 return ch - 'A' + 10;
334 return -1;
335}
336
337/*
338 * While we find nice hex chars, build a long_val.
339 * Return number of chars processed.
340 */
341int hex2u64(const char *ptr, u64 *long_val)
342{
343 const char *p = ptr;
344 *long_val = 0;
345
346 while (*p) {
347 const int hex_val = hex(*p);
348
349 if (hex_val < 0)
350 break;
351
352 *long_val = (*long_val << 4) | hex_val;
353 p++;
354 }
355
356 return p - ptr;
357}
358
1a47245d
AH
359int perf_event_paranoid(void)
360{
1a47245d
AH
361 int value;
362
ce27309f 363 if (sysctl__read_int("kernel/perf_event_paranoid", &value))
1a47245d
AH
364 return INT_MAX;
365
366 return value;
367}
d18acd15
WN
368static int
369fetch_ubuntu_kernel_version(unsigned int *puint)
370{
371 ssize_t len;
372 size_t line_len = 0;
373 char *ptr, *line = NULL;
374 int version, patchlevel, sublevel, err;
44b58e06 375 FILE *vsig;
d18acd15 376
44b58e06
ACM
377 if (!puint)
378 return 0;
379
380 vsig = fopen("/proc/version_signature", "r");
d18acd15
WN
381 if (!vsig) {
382 pr_debug("Open /proc/version_signature failed: %s\n",
383 strerror(errno));
384 return -1;
385 }
386
387 len = getline(&line, &line_len, vsig);
388 fclose(vsig);
389 err = -1;
390 if (len <= 0) {
391 pr_debug("Reading from /proc/version_signature failed: %s\n",
392 strerror(errno));
393 goto errout;
394 }
395
396 ptr = strrchr(line, ' ');
397 if (!ptr) {
398 pr_debug("Parsing /proc/version_signature failed: %s\n", line);
399 goto errout;
400 }
401
402 err = sscanf(ptr + 1, "%d.%d.%d",
403 &version, &patchlevel, &sublevel);
404 if (err != 3) {
405 pr_debug("Unable to get kernel version from /proc/version_signature '%s'\n",
406 line);
407 goto errout;
408 }
409
44b58e06 410 *puint = (version << 16) + (patchlevel << 8) + sublevel;
d18acd15
WN
411 err = 0;
412errout:
413 free(line);
414 return err;
415}
416
07bc5c69
WN
417int
418fetch_kernel_version(unsigned int *puint, char *str,
419 size_t str_size)
420{
421 struct utsname utsname;
422 int version, patchlevel, sublevel, err;
d18acd15
WN
423 bool int_ver_ready = false;
424
425 if (access("/proc/version_signature", R_OK) == 0)
426 if (!fetch_ubuntu_kernel_version(puint))
427 int_ver_ready = true;
07bc5c69
WN
428
429 if (uname(&utsname))
430 return -1;
431
432 if (str && str_size) {
433 strncpy(str, utsname.release, str_size);
434 str[str_size - 1] = '\0';
435 }
436
44b58e06
ACM
437 if (!puint || int_ver_ready)
438 return 0;
439
07bc5c69
WN
440 err = sscanf(utsname.release, "%d.%d.%d",
441 &version, &patchlevel, &sublevel);
442
443 if (err != 3) {
d18acd15 444 pr_debug("Unable to get kernel version from uname '%s'\n",
07bc5c69
WN
445 utsname.release);
446 return -1;
447 }
448
44b58e06 449 *puint = (version << 16) + (patchlevel << 8) + sublevel;
07bc5c69
WN
450 return 0;
451}
14cbfbeb
NK
452
453const char *perf_tip(const char *dirpath)
454{
455 struct strlist *tips;
456 struct str_node *node;
457 char *tip = NULL;
458 struct strlist_config conf = {
34b7b0f9
NK
459 .dirname = dirpath,
460 .file_only = true,
14cbfbeb
NK
461 };
462
463 tips = strlist__new("tips.txt", &conf);
34b7b0f9 464 if (tips == NULL)
570eda03
DCC
465 return errno == ENOENT ? NULL :
466 "Tip: check path of tips.txt or get more memory! ;-p";
34b7b0f9
NK
467
468 if (strlist__nr_entries(tips) == 0)
14cbfbeb 469 goto out;
14cbfbeb
NK
470
471 node = strlist__entry(tips, random() % strlist__nr_entries(tips));
472 if (asprintf(&tip, "Tip: %s", node->s) < 0)
473 tip = (char *)"Tip: get more memory! ;-)";
474
475out:
476 strlist__delete(tips);
477
478 return tip;
479}