Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 1 | #include "../perf.h" |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 2 | #include "util.h" |
Jiri Olsa | 84f5d36 | 2014-07-14 23:46:48 +0200 | [diff] [blame] | 3 | #include "debug.h" |
Borislav Petkov | cd0cfad | 2013-12-09 17:14:24 +0100 | [diff] [blame] | 4 | #include <api/fs/fs.h> |
Frederic Weisbecker | 69e3f52 | 2010-01-16 14:21:15 +0100 | [diff] [blame] | 5 | #include <sys/mman.h> |
Wang Nan | 07bc5c6 | 2015-11-06 13:55:35 +0000 | [diff] [blame] | 6 | #include <sys/utsname.h> |
Ingo Molnar | 89fe808 | 2013-09-30 12:07:11 +0200 | [diff] [blame] | 7 | #ifdef HAVE_BACKTRACE_SUPPORT |
Arnaldo Carvalho de Melo | dc4552b | 2012-08-07 23:32:05 -0300 | [diff] [blame] | 8 | #include <execinfo.h> |
Irina Tirdea | c9f08be | 2012-09-08 03:43:23 +0300 | [diff] [blame] | 9 | #endif |
Arnaldo Carvalho de Melo | fd20e81 | 2017-04-17 15:23:08 -0300 | [diff] [blame^] | 10 | #include <inttypes.h> |
Arnaldo Carvalho de Melo | dc4552b | 2012-08-07 23:32:05 -0300 | [diff] [blame] | 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
Jiri Olsa | cef82c9 | 2013-12-03 14:09:22 +0100 | [diff] [blame] | 13 | #include <string.h> |
| 14 | #include <errno.h> |
Adrian Hunter | 1a47245 | 2013-12-11 14:36:23 +0200 | [diff] [blame] | 15 | #include <limits.h> |
Adrian Hunter | 71db07b | 2013-12-11 14:36:32 +0200 | [diff] [blame] | 16 | #include <byteswap.h> |
Jiri Olsa | 838d145 | 2013-11-28 11:30:15 +0100 | [diff] [blame] | 17 | #include <linux/kernel.h> |
Wang Nan | c339b1a | 2016-02-24 11:20:44 +0000 | [diff] [blame] | 18 | #include <linux/log2.h> |
Arnaldo Carvalho de Melo | bd48c63 | 2016-08-05 15:40:30 -0300 | [diff] [blame] | 19 | #include <linux/time64.h> |
Jiri Olsa | 9398c48 | 2014-08-11 10:50:02 +0200 | [diff] [blame] | 20 | #include <unistd.h> |
Jiri Olsa | 23aadb1 | 2014-10-01 18:00:26 +0200 | [diff] [blame] | 21 | #include "callchain.h" |
Namhyung Kim | 14cbfbe | 2016-01-07 20:41:53 +0900 | [diff] [blame] | 22 | #include "strlist.h" |
Jiri Olsa | 23aadb1 | 2014-10-01 18:00:26 +0200 | [diff] [blame] | 23 | |
Jiri Olsa | 347ca87 | 2016-07-04 14:16:21 +0200 | [diff] [blame] | 24 | #define CALLCHAIN_PARAM_DEFAULT \ |
| 25 | .mode = CHAIN_GRAPH_ABS, \ |
| 26 | .min_percent = 0.5, \ |
| 27 | .order = ORDER_CALLEE, \ |
| 28 | .key = CCKEY_FUNCTION, \ |
| 29 | .value = CCVAL_PERCENT, \ |
| 30 | |
| 31 | struct callchain_param callchain_param = { |
| 32 | CALLCHAIN_PARAM_DEFAULT |
| 33 | }; |
| 34 | |
| 35 | struct callchain_param callchain_param_default = { |
| 36 | CALLCHAIN_PARAM_DEFAULT |
Jiri Olsa | 23aadb1 | 2014-10-01 18:00:26 +0200 | [diff] [blame] | 37 | }; |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 38 | |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 39 | /* |
| 40 | * XXX We need to find a better place for these things... |
| 41 | */ |
Arnaldo Carvalho de Melo | 0c1fe6b | 2012-10-06 14:57:10 -0300 | [diff] [blame] | 42 | unsigned int page_size; |
Don Zickus | 2b1b710 | 2014-05-30 16:10:05 -0400 | [diff] [blame] | 43 | int cacheline_size; |
Arnaldo Carvalho de Melo | 0c1fe6b | 2012-10-06 14:57:10 -0300 | [diff] [blame] | 44 | |
Arnaldo Carvalho de Melo | a29d5c9 | 2016-05-16 21:16:54 -0300 | [diff] [blame] | 45 | int sysctl_perf_event_max_stack = PERF_MAX_STACK_DEPTH; |
| 46 | int sysctl_perf_event_max_contexts_per_stack = PERF_MAX_CONTEXTS_PER_STACK; |
Arnaldo Carvalho de Melo | 4cb9344 | 2016-04-27 10:16:24 -0300 | [diff] [blame] | 47 | |
Arnaldo Carvalho de Melo | 0c6332e | 2012-12-13 16:43:04 -0300 | [diff] [blame] | 48 | bool test_attr__enabled; |
| 49 | |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 50 | bool perf_host = true; |
Joerg Roedel | c4a7dca | 2012-02-10 18:05:05 +0100 | [diff] [blame] | 51 | bool perf_guest = false; |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 52 | |
| 53 | void event_attr_init(struct perf_event_attr *attr) |
| 54 | { |
| 55 | if (!perf_host) |
| 56 | attr->exclude_host = 1; |
| 57 | if (!perf_guest) |
| 58 | attr->exclude_guest = 1; |
Stephane Eranian | 7e1ccd3 | 2012-02-09 16:12:38 +0100 | [diff] [blame] | 59 | /* to capture ABI version */ |
| 60 | attr->size = sizeof(*attr); |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 61 | } |
| 62 | |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 63 | int mkdir_p(char *path, mode_t mode) |
| 64 | { |
| 65 | struct stat st; |
| 66 | int err; |
| 67 | char *d = path; |
| 68 | |
| 69 | if (*d != '/') |
| 70 | return -1; |
| 71 | |
| 72 | if (stat(path, &st) == 0) |
| 73 | return 0; |
| 74 | |
| 75 | while (*++d == '/'); |
| 76 | |
| 77 | while ((d = strchr(d, '/'))) { |
| 78 | *d = '\0'; |
| 79 | err = stat(path, &st) && mkdir(path, mode); |
| 80 | *d++ = '/'; |
| 81 | if (err) |
| 82 | return -1; |
| 83 | while (*d == '/') |
| 84 | ++d; |
| 85 | } |
| 86 | return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0; |
| 87 | } |
| 88 | |
Joe Stringer | 9a9c733 | 2017-01-26 13:19:59 -0800 | [diff] [blame] | 89 | int rm_rf(const char *path) |
Namhyung Kim | 0b1de0be | 2015-05-18 09:30:17 +0900 | [diff] [blame] | 90 | { |
| 91 | DIR *dir; |
| 92 | int ret = 0; |
| 93 | struct dirent *d; |
| 94 | char namebuf[PATH_MAX]; |
| 95 | |
| 96 | dir = opendir(path); |
| 97 | if (dir == NULL) |
| 98 | return 0; |
| 99 | |
| 100 | while ((d = readdir(dir)) != NULL && !ret) { |
| 101 | struct stat statbuf; |
| 102 | |
| 103 | if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) |
| 104 | continue; |
| 105 | |
| 106 | scnprintf(namebuf, sizeof(namebuf), "%s/%s", |
| 107 | path, d->d_name); |
| 108 | |
Masami Hiramatsu | 2a1ef03 | 2016-06-08 18:29:11 +0900 | [diff] [blame] | 109 | /* We have to check symbolic link itself */ |
| 110 | ret = lstat(namebuf, &statbuf); |
Namhyung Kim | 0b1de0be | 2015-05-18 09:30:17 +0900 | [diff] [blame] | 111 | if (ret < 0) { |
| 112 | pr_debug("stat failed: %s\n", namebuf); |
| 113 | break; |
| 114 | } |
| 115 | |
Masami Hiramatsu | 2a1ef03 | 2016-06-08 18:29:11 +0900 | [diff] [blame] | 116 | if (S_ISDIR(statbuf.st_mode)) |
Namhyung Kim | 0b1de0be | 2015-05-18 09:30:17 +0900 | [diff] [blame] | 117 | ret = rm_rf(namebuf); |
Masami Hiramatsu | 2a1ef03 | 2016-06-08 18:29:11 +0900 | [diff] [blame] | 118 | else |
| 119 | ret = unlink(namebuf); |
Namhyung Kim | 0b1de0be | 2015-05-18 09:30:17 +0900 | [diff] [blame] | 120 | } |
| 121 | closedir(dir); |
| 122 | |
| 123 | if (ret < 0) |
| 124 | return ret; |
| 125 | |
| 126 | return rmdir(path); |
| 127 | } |
| 128 | |
Masami Hiramatsu | e1ce726 | 2016-04-26 18:02:42 +0900 | [diff] [blame] | 129 | /* A filter which removes dot files */ |
| 130 | bool lsdir_no_dot_filter(const char *name __maybe_unused, struct dirent *d) |
| 131 | { |
| 132 | return d->d_name[0] != '.'; |
| 133 | } |
| 134 | |
| 135 | /* lsdir reads a directory and store it in strlist */ |
| 136 | struct strlist *lsdir(const char *name, |
| 137 | bool (*filter)(const char *, struct dirent *)) |
| 138 | { |
| 139 | struct strlist *list = NULL; |
| 140 | DIR *dir; |
| 141 | struct dirent *d; |
| 142 | |
| 143 | dir = opendir(name); |
| 144 | if (!dir) |
| 145 | return NULL; |
| 146 | |
| 147 | list = strlist__new(NULL, NULL); |
| 148 | if (!list) { |
Masami Hiramatsu | 357a54f | 2016-05-11 22:51:27 +0900 | [diff] [blame] | 149 | errno = ENOMEM; |
Masami Hiramatsu | e1ce726 | 2016-04-26 18:02:42 +0900 | [diff] [blame] | 150 | goto out; |
| 151 | } |
| 152 | |
| 153 | while ((d = readdir(dir)) != NULL) { |
| 154 | if (!filter || filter(name, d)) |
| 155 | strlist__add(list, d->d_name); |
| 156 | } |
| 157 | |
| 158 | out: |
| 159 | closedir(dir); |
| 160 | return list; |
| 161 | } |
| 162 | |
Milos Vyletel | d7c7260 | 2015-06-08 16:50:16 +0200 | [diff] [blame] | 163 | static int slow_copyfile(const char *from, const char *to) |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 164 | { |
Adrian Hunter | 9a17d72 | 2013-10-14 13:43:41 +0300 | [diff] [blame] | 165 | int err = -1; |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 166 | char *line = NULL; |
| 167 | size_t n; |
| 168 | FILE *from_fp = fopen(from, "r"), *to_fp; |
| 169 | |
| 170 | if (from_fp == NULL) |
| 171 | goto out; |
| 172 | |
| 173 | to_fp = fopen(to, "w"); |
| 174 | if (to_fp == NULL) |
| 175 | goto out_fclose_from; |
| 176 | |
| 177 | while (getline(&line, &n, from_fp) > 0) |
| 178 | if (fputs(line, to_fp) == EOF) |
| 179 | goto out_fclose_to; |
| 180 | err = 0; |
| 181 | out_fclose_to: |
| 182 | fclose(to_fp); |
| 183 | free(line); |
| 184 | out_fclose_from: |
| 185 | fclose(from_fp); |
| 186 | out: |
| 187 | return err; |
| 188 | } |
| 189 | |
Namhyung Kim | 9c9f5a2 | 2015-05-18 09:30:18 +0900 | [diff] [blame] | 190 | int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size) |
| 191 | { |
| 192 | void *ptr; |
| 193 | loff_t pgoff; |
| 194 | |
| 195 | pgoff = off_in & ~(page_size - 1); |
| 196 | off_in -= pgoff; |
| 197 | |
| 198 | ptr = mmap(NULL, off_in + size, PROT_READ, MAP_PRIVATE, ifd, pgoff); |
| 199 | if (ptr == MAP_FAILED) |
| 200 | return -1; |
| 201 | |
| 202 | while (size) { |
| 203 | ssize_t ret = pwrite(ofd, ptr + off_in, size, off_out); |
| 204 | if (ret < 0 && errno == EINTR) |
| 205 | continue; |
| 206 | if (ret <= 0) |
| 207 | break; |
| 208 | |
| 209 | size -= ret; |
| 210 | off_in += ret; |
| 211 | off_out -= ret; |
| 212 | } |
| 213 | munmap(ptr, off_in + size); |
| 214 | |
| 215 | return size ? -1 : 0; |
| 216 | } |
| 217 | |
Adrian Hunter | 9a17d72 | 2013-10-14 13:43:41 +0300 | [diff] [blame] | 218 | int copyfile_mode(const char *from, const char *to, mode_t mode) |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 219 | { |
| 220 | int fromfd, tofd; |
| 221 | struct stat st; |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 222 | int err = -1; |
Milos Vyletel | d7c7260 | 2015-06-08 16:50:16 +0200 | [diff] [blame] | 223 | char *tmp = NULL, *ptr = NULL; |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 224 | |
| 225 | if (stat(from, &st)) |
| 226 | goto out; |
| 227 | |
Milos Vyletel | d7c7260 | 2015-06-08 16:50:16 +0200 | [diff] [blame] | 228 | /* extra 'x' at the end is to reserve space for '.' */ |
| 229 | if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) { |
| 230 | tmp = NULL; |
| 231 | goto out; |
| 232 | } |
| 233 | ptr = strrchr(tmp, '/'); |
| 234 | if (!ptr) |
| 235 | goto out; |
| 236 | ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1); |
| 237 | *ptr = '.'; |
| 238 | |
| 239 | tofd = mkstemp(tmp); |
| 240 | if (tofd < 0) |
| 241 | goto out; |
| 242 | |
| 243 | if (fchmod(tofd, mode)) |
| 244 | goto out_close_to; |
| 245 | |
| 246 | if (st.st_size == 0) { /* /proc? do it slowly... */ |
| 247 | err = slow_copyfile(from, tmp); |
| 248 | goto out_close_to; |
| 249 | } |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 250 | |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 251 | fromfd = open(from, O_RDONLY); |
| 252 | if (fromfd < 0) |
Milos Vyletel | d7c7260 | 2015-06-08 16:50:16 +0200 | [diff] [blame] | 253 | goto out_close_to; |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 254 | |
Namhyung Kim | 9c9f5a2 | 2015-05-18 09:30:18 +0900 | [diff] [blame] | 255 | err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size); |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 256 | |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 257 | close(fromfd); |
Milos Vyletel | d7c7260 | 2015-06-08 16:50:16 +0200 | [diff] [blame] | 258 | out_close_to: |
| 259 | close(tofd); |
| 260 | if (!err) |
| 261 | err = link(tmp, to); |
| 262 | unlink(tmp); |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 263 | out: |
Milos Vyletel | d7c7260 | 2015-06-08 16:50:16 +0200 | [diff] [blame] | 264 | free(tmp); |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 265 | return err; |
| 266 | } |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 267 | |
Adrian Hunter | 9a17d72 | 2013-10-14 13:43:41 +0300 | [diff] [blame] | 268 | int copyfile(const char *from, const char *to) |
| 269 | { |
| 270 | return copyfile_mode(from, to, 0755); |
| 271 | } |
| 272 | |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 273 | unsigned long convert_unit(unsigned long value, char *unit) |
| 274 | { |
| 275 | *unit = ' '; |
| 276 | |
| 277 | if (value > 1000) { |
| 278 | value /= 1000; |
| 279 | *unit = 'K'; |
| 280 | } |
| 281 | |
| 282 | if (value > 1000) { |
| 283 | value /= 1000; |
| 284 | *unit = 'M'; |
| 285 | } |
| 286 | |
| 287 | if (value > 1000) { |
| 288 | value /= 1000; |
| 289 | *unit = 'G'; |
| 290 | } |
| 291 | |
| 292 | return value; |
| 293 | } |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 294 | |
Jiri Olsa | bc3a502 | 2013-11-28 11:30:16 +0100 | [diff] [blame] | 295 | static ssize_t ion(bool is_read, int fd, void *buf, size_t n) |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 296 | { |
| 297 | void *buf_start = buf; |
Jiri Olsa | 838d145 | 2013-11-28 11:30:15 +0100 | [diff] [blame] | 298 | size_t left = n; |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 299 | |
Jiri Olsa | 838d145 | 2013-11-28 11:30:15 +0100 | [diff] [blame] | 300 | while (left) { |
Jiri Olsa | bc3a502 | 2013-11-28 11:30:16 +0100 | [diff] [blame] | 301 | ssize_t ret = is_read ? read(fd, buf, left) : |
| 302 | write(fd, buf, left); |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 303 | |
Namhyung Kim | e148c76 | 2014-04-24 22:27:32 +0900 | [diff] [blame] | 304 | if (ret < 0 && errno == EINTR) |
| 305 | continue; |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 306 | if (ret <= 0) |
| 307 | return ret; |
| 308 | |
Jiri Olsa | 838d145 | 2013-11-28 11:30:15 +0100 | [diff] [blame] | 309 | left -= ret; |
| 310 | buf += ret; |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 311 | } |
| 312 | |
Jiri Olsa | 838d145 | 2013-11-28 11:30:15 +0100 | [diff] [blame] | 313 | BUG_ON((size_t)(buf - buf_start) != n); |
| 314 | return n; |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 315 | } |
Arnaldo Carvalho de Melo | 61e04b3 | 2012-04-19 13:15:24 -0300 | [diff] [blame] | 316 | |
Jiri Olsa | bc3a502 | 2013-11-28 11:30:16 +0100 | [diff] [blame] | 317 | /* |
| 318 | * Read exactly 'n' bytes or return an error. |
| 319 | */ |
| 320 | ssize_t readn(int fd, void *buf, size_t n) |
| 321 | { |
| 322 | return ion(true, fd, buf, n); |
| 323 | } |
| 324 | |
| 325 | /* |
| 326 | * Write exactly 'n' bytes or return an error. |
| 327 | */ |
| 328 | ssize_t writen(int fd, void *buf, size_t n) |
| 329 | { |
| 330 | return ion(false, fd, buf, n); |
| 331 | } |
| 332 | |
Arnaldo Carvalho de Melo | 61e04b3 | 2012-04-19 13:15:24 -0300 | [diff] [blame] | 333 | size_t hex_width(u64 v) |
| 334 | { |
| 335 | size_t n = 1; |
| 336 | |
| 337 | while ((v >>= 4)) |
| 338 | ++n; |
| 339 | |
| 340 | return n; |
| 341 | } |
Arnaldo Carvalho de Melo | dc4552b | 2012-08-07 23:32:05 -0300 | [diff] [blame] | 342 | |
Jiri Olsa | b2aff5f | 2012-10-27 23:18:30 +0200 | [diff] [blame] | 343 | static int hex(char ch) |
| 344 | { |
| 345 | if ((ch >= '0') && (ch <= '9')) |
| 346 | return ch - '0'; |
| 347 | if ((ch >= 'a') && (ch <= 'f')) |
| 348 | return ch - 'a' + 10; |
| 349 | if ((ch >= 'A') && (ch <= 'F')) |
| 350 | return ch - 'A' + 10; |
| 351 | return -1; |
| 352 | } |
| 353 | |
| 354 | /* |
| 355 | * While we find nice hex chars, build a long_val. |
| 356 | * Return number of chars processed. |
| 357 | */ |
| 358 | int hex2u64(const char *ptr, u64 *long_val) |
| 359 | { |
| 360 | const char *p = ptr; |
| 361 | *long_val = 0; |
| 362 | |
| 363 | while (*p) { |
| 364 | const int hex_val = hex(*p); |
| 365 | |
| 366 | if (hex_val < 0) |
| 367 | break; |
| 368 | |
| 369 | *long_val = (*long_val << 4) | hex_val; |
| 370 | p++; |
| 371 | } |
| 372 | |
| 373 | return p - ptr; |
| 374 | } |
| 375 | |
Arnaldo Carvalho de Melo | dc4552b | 2012-08-07 23:32:05 -0300 | [diff] [blame] | 376 | /* Obtain a backtrace and print it to stdout. */ |
Ingo Molnar | 89fe808 | 2013-09-30 12:07:11 +0200 | [diff] [blame] | 377 | #ifdef HAVE_BACKTRACE_SUPPORT |
Arnaldo Carvalho de Melo | dc4552b | 2012-08-07 23:32:05 -0300 | [diff] [blame] | 378 | void dump_stack(void) |
| 379 | { |
| 380 | void *array[16]; |
| 381 | size_t size = backtrace(array, ARRAY_SIZE(array)); |
| 382 | char **strings = backtrace_symbols(array, size); |
| 383 | size_t i; |
| 384 | |
| 385 | printf("Obtained %zd stack frames.\n", size); |
| 386 | |
| 387 | for (i = 0; i < size; i++) |
| 388 | printf("%s\n", strings[i]); |
| 389 | |
| 390 | free(strings); |
| 391 | } |
Irina Tirdea | c9f08be | 2012-09-08 03:43:23 +0300 | [diff] [blame] | 392 | #else |
| 393 | void dump_stack(void) {} |
| 394 | #endif |
David Ahern | 2c803e5 | 2013-01-14 10:48:01 -0700 | [diff] [blame] | 395 | |
Arnaldo Carvalho de Melo | 07c1a0d | 2015-02-24 15:34:23 -0300 | [diff] [blame] | 396 | void sighandler_dump_stack(int sig) |
| 397 | { |
| 398 | psignal(sig, "perf"); |
| 399 | dump_stack(); |
Arnaldo Carvalho de Melo | 9daddf6 | 2015-12-11 16:43:57 -0300 | [diff] [blame] | 400 | signal(sig, SIG_DFL); |
| 401 | raise(sig); |
Arnaldo Carvalho de Melo | 07c1a0d | 2015-02-24 15:34:23 -0300 | [diff] [blame] | 402 | } |
| 403 | |
Namhyung Kim | 99620a5 | 2016-10-24 11:02:45 +0900 | [diff] [blame] | 404 | int timestamp__scnprintf_usec(u64 timestamp, char *buf, size_t sz) |
| 405 | { |
| 406 | u64 sec = timestamp / NSEC_PER_SEC; |
| 407 | u64 usec = (timestamp % NSEC_PER_SEC) / NSEC_PER_USEC; |
| 408 | |
| 409 | return scnprintf(buf, sz, "%"PRIu64".%06"PRIu64, sec, usec); |
| 410 | } |
| 411 | |
Jiri Olsa | 27050f5 | 2013-09-01 12:36:13 +0200 | [diff] [blame] | 412 | unsigned long parse_tag_value(const char *str, struct parse_tag *tags) |
| 413 | { |
| 414 | struct parse_tag *i = tags; |
| 415 | |
| 416 | while (i->tag) { |
| 417 | char *s; |
| 418 | |
| 419 | s = strchr(str, i->tag); |
| 420 | if (s) { |
| 421 | unsigned long int value; |
| 422 | char *endptr; |
| 423 | |
| 424 | value = strtoul(str, &endptr, 10); |
| 425 | if (s != endptr) |
| 426 | break; |
| 427 | |
Adrian Hunter | 56921be | 2013-10-22 10:34:17 +0300 | [diff] [blame] | 428 | if (value > ULONG_MAX / i->mult) |
| 429 | break; |
Jiri Olsa | 27050f5 | 2013-09-01 12:36:13 +0200 | [diff] [blame] | 430 | value *= i->mult; |
| 431 | return value; |
| 432 | } |
| 433 | i++; |
| 434 | } |
| 435 | |
| 436 | return (unsigned long) -1; |
| 437 | } |
Arnaldo Carvalho de Melo | 97a07f1 | 2013-10-17 16:33:43 -0300 | [diff] [blame] | 438 | |
Kan Liang | 076a30c | 2015-08-06 15:44:52 -0400 | [diff] [blame] | 439 | int get_stack_size(const char *str, unsigned long *_size) |
| 440 | { |
| 441 | char *endptr; |
| 442 | unsigned long size; |
| 443 | unsigned long max_size = round_down(USHRT_MAX, sizeof(u64)); |
| 444 | |
| 445 | size = strtoul(str, &endptr, 0); |
| 446 | |
| 447 | do { |
| 448 | if (*endptr) |
| 449 | break; |
| 450 | |
| 451 | size = round_up(size, sizeof(u64)); |
| 452 | if (!size || size > max_size) |
| 453 | break; |
| 454 | |
| 455 | *_size = size; |
| 456 | return 0; |
| 457 | |
| 458 | } while (0); |
| 459 | |
| 460 | pr_err("callchain: Incorrect stack dump size (max %ld): %s\n", |
| 461 | max_size, str); |
| 462 | return -1; |
| 463 | } |
| 464 | |
| 465 | int parse_callchain_record(const char *arg, struct callchain_param *param) |
| 466 | { |
| 467 | char *tok, *name, *saveptr = NULL; |
| 468 | char *buf; |
| 469 | int ret = -1; |
| 470 | |
| 471 | /* We need buffer that we know we can write to. */ |
| 472 | buf = malloc(strlen(arg) + 1); |
| 473 | if (!buf) |
| 474 | return -ENOMEM; |
| 475 | |
| 476 | strcpy(buf, arg); |
| 477 | |
| 478 | tok = strtok_r((char *)buf, ",", &saveptr); |
| 479 | name = tok ? : (char *)buf; |
| 480 | |
| 481 | do { |
| 482 | /* Framepointer style */ |
| 483 | if (!strncmp(name, "fp", sizeof("fp"))) { |
| 484 | if (!strtok_r(NULL, ",", &saveptr)) { |
| 485 | param->record_mode = CALLCHAIN_FP; |
| 486 | ret = 0; |
| 487 | } else |
| 488 | pr_err("callchain: No more arguments " |
| 489 | "needed for --call-graph fp\n"); |
| 490 | break; |
| 491 | |
Kan Liang | 076a30c | 2015-08-06 15:44:52 -0400 | [diff] [blame] | 492 | /* Dwarf style */ |
| 493 | } else if (!strncmp(name, "dwarf", sizeof("dwarf"))) { |
| 494 | const unsigned long default_stack_dump_size = 8192; |
| 495 | |
| 496 | ret = 0; |
| 497 | param->record_mode = CALLCHAIN_DWARF; |
| 498 | param->dump_size = default_stack_dump_size; |
| 499 | |
| 500 | tok = strtok_r(NULL, ",", &saveptr); |
| 501 | if (tok) { |
| 502 | unsigned long size = 0; |
| 503 | |
| 504 | ret = get_stack_size(tok, &size); |
| 505 | param->dump_size = size; |
| 506 | } |
Kan Liang | 076a30c | 2015-08-06 15:44:52 -0400 | [diff] [blame] | 507 | } else if (!strncmp(name, "lbr", sizeof("lbr"))) { |
| 508 | if (!strtok_r(NULL, ",", &saveptr)) { |
| 509 | param->record_mode = CALLCHAIN_LBR; |
| 510 | ret = 0; |
| 511 | } else |
| 512 | pr_err("callchain: No more arguments " |
| 513 | "needed for --call-graph lbr\n"); |
| 514 | break; |
| 515 | } else { |
| 516 | pr_err("callchain: Unknown --call-graph option " |
| 517 | "value: %s\n", arg); |
| 518 | break; |
| 519 | } |
| 520 | |
| 521 | } while (0); |
| 522 | |
| 523 | free(buf); |
| 524 | return ret; |
| 525 | } |
| 526 | |
Dongsheng Yang | e1a2b17 | 2013-12-06 17:25:51 -0500 | [diff] [blame] | 527 | const char *get_filename_for_perf_kvm(void) |
| 528 | { |
| 529 | const char *filename; |
| 530 | |
| 531 | if (perf_host && !perf_guest) |
| 532 | filename = strdup("perf.data.host"); |
| 533 | else if (!perf_host && perf_guest) |
| 534 | filename = strdup("perf.data.guest"); |
| 535 | else |
| 536 | filename = strdup("perf.data.kvm"); |
| 537 | |
| 538 | return filename; |
| 539 | } |
Adrian Hunter | 1a47245 | 2013-12-11 14:36:23 +0200 | [diff] [blame] | 540 | |
| 541 | int perf_event_paranoid(void) |
| 542 | { |
Adrian Hunter | 1a47245 | 2013-12-11 14:36:23 +0200 | [diff] [blame] | 543 | int value; |
| 544 | |
Arnaldo Carvalho de Melo | ce27309 | 2014-12-11 13:37:59 -0300 | [diff] [blame] | 545 | if (sysctl__read_int("kernel/perf_event_paranoid", &value)) |
Adrian Hunter | 1a47245 | 2013-12-11 14:36:23 +0200 | [diff] [blame] | 546 | return INT_MAX; |
| 547 | |
| 548 | return value; |
| 549 | } |
Adrian Hunter | 71db07b | 2013-12-11 14:36:32 +0200 | [diff] [blame] | 550 | |
| 551 | void mem_bswap_32(void *src, int byte_size) |
| 552 | { |
| 553 | u32 *m = src; |
| 554 | while (byte_size > 0) { |
| 555 | *m = bswap_32(*m); |
| 556 | byte_size -= sizeof(u32); |
| 557 | ++m; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | void mem_bswap_64(void *src, int byte_size) |
| 562 | { |
| 563 | u64 *m = src; |
| 564 | |
| 565 | while (byte_size > 0) { |
| 566 | *m = bswap_64(*m); |
| 567 | byte_size -= sizeof(u64); |
| 568 | ++m; |
| 569 | } |
| 570 | } |
Jiri Olsa | 63914ac | 2014-08-01 17:46:54 +0200 | [diff] [blame] | 571 | |
| 572 | bool find_process(const char *name) |
| 573 | { |
| 574 | size_t len = strlen(name); |
| 575 | DIR *dir; |
| 576 | struct dirent *d; |
| 577 | int ret = -1; |
| 578 | |
| 579 | dir = opendir(procfs__mountpoint()); |
| 580 | if (!dir) |
Peter Senna Tschudin | bf64456 | 2015-09-17 12:08:53 +0200 | [diff] [blame] | 581 | return false; |
Jiri Olsa | 63914ac | 2014-08-01 17:46:54 +0200 | [diff] [blame] | 582 | |
| 583 | /* Walk through the directory. */ |
| 584 | while (ret && (d = readdir(dir)) != NULL) { |
| 585 | char path[PATH_MAX]; |
| 586 | char *data; |
| 587 | size_t size; |
| 588 | |
| 589 | if ((d->d_type != DT_DIR) || |
| 590 | !strcmp(".", d->d_name) || |
| 591 | !strcmp("..", d->d_name)) |
| 592 | continue; |
| 593 | |
| 594 | scnprintf(path, sizeof(path), "%s/%s/comm", |
| 595 | procfs__mountpoint(), d->d_name); |
| 596 | |
| 597 | if (filename__read_str(path, &data, &size)) |
| 598 | continue; |
| 599 | |
| 600 | ret = strncmp(name, data, len); |
| 601 | free(data); |
| 602 | } |
| 603 | |
| 604 | closedir(dir); |
| 605 | return ret ? false : true; |
| 606 | } |
Wang Nan | 07bc5c6 | 2015-11-06 13:55:35 +0000 | [diff] [blame] | 607 | |
Wang Nan | d18acd1 | 2016-11-15 04:05:44 +0000 | [diff] [blame] | 608 | static int |
| 609 | fetch_ubuntu_kernel_version(unsigned int *puint) |
| 610 | { |
| 611 | ssize_t len; |
| 612 | size_t line_len = 0; |
| 613 | char *ptr, *line = NULL; |
| 614 | int version, patchlevel, sublevel, err; |
| 615 | FILE *vsig = fopen("/proc/version_signature", "r"); |
| 616 | |
| 617 | if (!vsig) { |
| 618 | pr_debug("Open /proc/version_signature failed: %s\n", |
| 619 | strerror(errno)); |
| 620 | return -1; |
| 621 | } |
| 622 | |
| 623 | len = getline(&line, &line_len, vsig); |
| 624 | fclose(vsig); |
| 625 | err = -1; |
| 626 | if (len <= 0) { |
| 627 | pr_debug("Reading from /proc/version_signature failed: %s\n", |
| 628 | strerror(errno)); |
| 629 | goto errout; |
| 630 | } |
| 631 | |
| 632 | ptr = strrchr(line, ' '); |
| 633 | if (!ptr) { |
| 634 | pr_debug("Parsing /proc/version_signature failed: %s\n", line); |
| 635 | goto errout; |
| 636 | } |
| 637 | |
| 638 | err = sscanf(ptr + 1, "%d.%d.%d", |
| 639 | &version, &patchlevel, &sublevel); |
| 640 | if (err != 3) { |
| 641 | pr_debug("Unable to get kernel version from /proc/version_signature '%s'\n", |
| 642 | line); |
| 643 | goto errout; |
| 644 | } |
| 645 | |
| 646 | if (puint) |
| 647 | *puint = (version << 16) + (patchlevel << 8) + sublevel; |
| 648 | err = 0; |
| 649 | errout: |
| 650 | free(line); |
| 651 | return err; |
| 652 | } |
| 653 | |
Wang Nan | 07bc5c6 | 2015-11-06 13:55:35 +0000 | [diff] [blame] | 654 | int |
| 655 | fetch_kernel_version(unsigned int *puint, char *str, |
| 656 | size_t str_size) |
| 657 | { |
| 658 | struct utsname utsname; |
| 659 | int version, patchlevel, sublevel, err; |
Wang Nan | d18acd1 | 2016-11-15 04:05:44 +0000 | [diff] [blame] | 660 | bool int_ver_ready = false; |
| 661 | |
| 662 | if (access("/proc/version_signature", R_OK) == 0) |
| 663 | if (!fetch_ubuntu_kernel_version(puint)) |
| 664 | int_ver_ready = true; |
Wang Nan | 07bc5c6 | 2015-11-06 13:55:35 +0000 | [diff] [blame] | 665 | |
| 666 | if (uname(&utsname)) |
| 667 | return -1; |
| 668 | |
| 669 | if (str && str_size) { |
| 670 | strncpy(str, utsname.release, str_size); |
| 671 | str[str_size - 1] = '\0'; |
| 672 | } |
| 673 | |
| 674 | err = sscanf(utsname.release, "%d.%d.%d", |
| 675 | &version, &patchlevel, &sublevel); |
| 676 | |
| 677 | if (err != 3) { |
Wang Nan | d18acd1 | 2016-11-15 04:05:44 +0000 | [diff] [blame] | 678 | pr_debug("Unable to get kernel version from uname '%s'\n", |
Wang Nan | 07bc5c6 | 2015-11-06 13:55:35 +0000 | [diff] [blame] | 679 | utsname.release); |
| 680 | return -1; |
| 681 | } |
| 682 | |
Wang Nan | d18acd1 | 2016-11-15 04:05:44 +0000 | [diff] [blame] | 683 | if (puint && !int_ver_ready) |
Wang Nan | 07bc5c6 | 2015-11-06 13:55:35 +0000 | [diff] [blame] | 684 | *puint = (version << 16) + (patchlevel << 8) + sublevel; |
| 685 | return 0; |
| 686 | } |
Namhyung Kim | 14cbfbe | 2016-01-07 20:41:53 +0900 | [diff] [blame] | 687 | |
| 688 | const char *perf_tip(const char *dirpath) |
| 689 | { |
| 690 | struct strlist *tips; |
| 691 | struct str_node *node; |
| 692 | char *tip = NULL; |
| 693 | struct strlist_config conf = { |
Namhyung Kim | 34b7b0f | 2016-01-09 19:16:29 +0900 | [diff] [blame] | 694 | .dirname = dirpath, |
| 695 | .file_only = true, |
Namhyung Kim | 14cbfbe | 2016-01-07 20:41:53 +0900 | [diff] [blame] | 696 | }; |
| 697 | |
| 698 | tips = strlist__new("tips.txt", &conf); |
Namhyung Kim | 34b7b0f | 2016-01-09 19:16:29 +0900 | [diff] [blame] | 699 | if (tips == NULL) |
David Carrillo-Cisneros | 570eda0 | 2017-04-11 23:49:16 -0700 | [diff] [blame] | 700 | return errno == ENOENT ? NULL : |
| 701 | "Tip: check path of tips.txt or get more memory! ;-p"; |
Namhyung Kim | 34b7b0f | 2016-01-09 19:16:29 +0900 | [diff] [blame] | 702 | |
| 703 | if (strlist__nr_entries(tips) == 0) |
Namhyung Kim | 14cbfbe | 2016-01-07 20:41:53 +0900 | [diff] [blame] | 704 | goto out; |
Namhyung Kim | 14cbfbe | 2016-01-07 20:41:53 +0900 | [diff] [blame] | 705 | |
| 706 | node = strlist__entry(tips, random() % strlist__nr_entries(tips)); |
| 707 | if (asprintf(&tip, "Tip: %s", node->s) < 0) |
| 708 | tip = (char *)"Tip: get more memory! ;-)"; |
| 709 | |
| 710 | out: |
| 711 | strlist__delete(tips); |
| 712 | |
| 713 | return tip; |
| 714 | } |
Jiri Olsa | 4035672 | 2016-01-20 12:56:32 +0100 | [diff] [blame] | 715 | |
| 716 | bool is_regular_file(const char *file) |
| 717 | { |
| 718 | struct stat st; |
| 719 | |
| 720 | if (stat(file, &st)) |
| 721 | return false; |
| 722 | |
| 723 | return S_ISREG(st.st_mode); |
| 724 | } |
Wang Nan | 37b2015 | 2016-01-25 09:56:13 +0000 | [diff] [blame] | 725 | |
| 726 | int fetch_current_timestamp(char *buf, size_t sz) |
| 727 | { |
| 728 | struct timeval tv; |
| 729 | struct tm tm; |
| 730 | char dt[32]; |
| 731 | |
| 732 | if (gettimeofday(&tv, NULL) || !localtime_r(&tv.tv_sec, &tm)) |
| 733 | return -1; |
| 734 | |
| 735 | if (!strftime(dt, sizeof(dt), "%Y%m%d%H%M%S", &tm)) |
| 736 | return -1; |
| 737 | |
| 738 | scnprintf(buf, sz, "%s%02u", dt, (unsigned)tv.tv_usec / 10000); |
| 739 | |
| 740 | return 0; |
| 741 | } |
Wang Nan | c339b1a | 2016-02-24 11:20:44 +0000 | [diff] [blame] | 742 | |
| 743 | void print_binary(unsigned char *data, size_t len, |
| 744 | size_t bytes_per_line, print_binary_t printer, |
| 745 | void *extra) |
| 746 | { |
| 747 | size_t i, j, mask; |
| 748 | |
| 749 | if (!printer) |
| 750 | return; |
| 751 | |
| 752 | bytes_per_line = roundup_pow_of_two(bytes_per_line); |
| 753 | mask = bytes_per_line - 1; |
| 754 | |
| 755 | printer(BINARY_PRINT_DATA_BEGIN, 0, extra); |
| 756 | for (i = 0; i < len; i++) { |
| 757 | if ((i & mask) == 0) { |
| 758 | printer(BINARY_PRINT_LINE_BEGIN, -1, extra); |
| 759 | printer(BINARY_PRINT_ADDR, i, extra); |
| 760 | } |
| 761 | |
| 762 | printer(BINARY_PRINT_NUM_DATA, data[i], extra); |
| 763 | |
| 764 | if (((i & mask) == mask) || i == len - 1) { |
| 765 | for (j = 0; j < mask-(i & mask); j++) |
| 766 | printer(BINARY_PRINT_NUM_PAD, -1, extra); |
| 767 | |
| 768 | printer(BINARY_PRINT_SEP, i, extra); |
| 769 | for (j = i & ~mask; j <= i; j++) |
| 770 | printer(BINARY_PRINT_CHAR_DATA, data[j], extra); |
| 771 | for (j = 0; j < mask-(i & mask); j++) |
| 772 | printer(BINARY_PRINT_CHAR_PAD, i, extra); |
| 773 | printer(BINARY_PRINT_LINE_END, -1, extra); |
| 774 | } |
| 775 | } |
| 776 | printer(BINARY_PRINT_DATA_END, -1, extra); |
| 777 | } |
Jiri Olsa | accaed2 | 2016-07-16 18:11:19 +0200 | [diff] [blame] | 778 | |
| 779 | int is_printable_array(char *p, unsigned int len) |
| 780 | { |
| 781 | unsigned int i; |
| 782 | |
| 783 | if (!p || !len || p[len - 1] != 0) |
| 784 | return 0; |
| 785 | |
| 786 | len--; |
| 787 | |
| 788 | for (i = 0; i < len; i++) { |
| 789 | if (!isprint(p[i]) && !isspace(p[i])) |
| 790 | return 0; |
| 791 | } |
| 792 | return 1; |
| 793 | } |
Jiri Olsa | 9808143 | 2017-01-09 10:51:55 +0100 | [diff] [blame] | 794 | |
| 795 | int unit_number__scnprintf(char *buf, size_t size, u64 n) |
| 796 | { |
| 797 | char unit[4] = "BKMG"; |
| 798 | int i = 0; |
| 799 | |
| 800 | while (((n / 1024) > 1) && (i < 3)) { |
| 801 | n /= 1024; |
| 802 | i++; |
| 803 | } |
| 804 | |
| 805 | return scnprintf(buf, size, "%" PRIu64 "%c", n, unit[i]); |
| 806 | } |