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" |
Frederic Weisbecker | 69e3f52 | 2010-01-16 14:21:15 +0100 | [diff] [blame] | 3 | #include <sys/mman.h> |
Irina Tirdea | c9f08be | 2012-09-08 03:43:23 +0300 | [diff] [blame] | 4 | #ifndef NO_BACKTRACE |
Arnaldo Carvalho de Melo | dc4552b | 2012-08-07 23:32:05 -0300 | [diff] [blame] | 5 | #include <execinfo.h> |
Irina Tirdea | c9f08be | 2012-09-08 03:43:23 +0300 | [diff] [blame] | 6 | #endif |
Arnaldo Carvalho de Melo | dc4552b | 2012-08-07 23:32:05 -0300 | [diff] [blame] | 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 9 | |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 10 | /* |
| 11 | * XXX We need to find a better place for these things... |
| 12 | */ |
| 13 | bool perf_host = true; |
Joerg Roedel | c4a7dca | 2012-02-10 18:05:05 +0100 | [diff] [blame] | 14 | bool perf_guest = false; |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 15 | |
| 16 | void event_attr_init(struct perf_event_attr *attr) |
| 17 | { |
| 18 | if (!perf_host) |
| 19 | attr->exclude_host = 1; |
| 20 | if (!perf_guest) |
| 21 | attr->exclude_guest = 1; |
Stephane Eranian | 7e1ccd3 | 2012-02-09 16:12:38 +0100 | [diff] [blame] | 22 | /* to capture ABI version */ |
| 23 | attr->size = sizeof(*attr); |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 24 | } |
| 25 | |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 26 | int mkdir_p(char *path, mode_t mode) |
| 27 | { |
| 28 | struct stat st; |
| 29 | int err; |
| 30 | char *d = path; |
| 31 | |
| 32 | if (*d != '/') |
| 33 | return -1; |
| 34 | |
| 35 | if (stat(path, &st) == 0) |
| 36 | return 0; |
| 37 | |
| 38 | while (*++d == '/'); |
| 39 | |
| 40 | while ((d = strchr(d, '/'))) { |
| 41 | *d = '\0'; |
| 42 | err = stat(path, &st) && mkdir(path, mode); |
| 43 | *d++ = '/'; |
| 44 | if (err) |
| 45 | return -1; |
| 46 | while (*d == '/') |
| 47 | ++d; |
| 48 | } |
| 49 | return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0; |
| 50 | } |
| 51 | |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 52 | static int slow_copyfile(const char *from, const char *to) |
| 53 | { |
| 54 | int err = 0; |
| 55 | char *line = NULL; |
| 56 | size_t n; |
| 57 | FILE *from_fp = fopen(from, "r"), *to_fp; |
| 58 | |
| 59 | if (from_fp == NULL) |
| 60 | goto out; |
| 61 | |
| 62 | to_fp = fopen(to, "w"); |
| 63 | if (to_fp == NULL) |
| 64 | goto out_fclose_from; |
| 65 | |
| 66 | while (getline(&line, &n, from_fp) > 0) |
| 67 | if (fputs(line, to_fp) == EOF) |
| 68 | goto out_fclose_to; |
| 69 | err = 0; |
| 70 | out_fclose_to: |
| 71 | fclose(to_fp); |
| 72 | free(line); |
| 73 | out_fclose_from: |
| 74 | fclose(from_fp); |
| 75 | out: |
| 76 | return err; |
| 77 | } |
| 78 | |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 79 | int copyfile(const char *from, const char *to) |
| 80 | { |
| 81 | int fromfd, tofd; |
| 82 | struct stat st; |
| 83 | void *addr; |
| 84 | int err = -1; |
| 85 | |
| 86 | if (stat(from, &st)) |
| 87 | goto out; |
| 88 | |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 89 | if (st.st_size == 0) /* /proc? do it slowly... */ |
| 90 | return slow_copyfile(from, to); |
| 91 | |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 92 | fromfd = open(from, O_RDONLY); |
| 93 | if (fromfd < 0) |
| 94 | goto out; |
| 95 | |
| 96 | tofd = creat(to, 0755); |
| 97 | if (tofd < 0) |
| 98 | goto out_close_from; |
| 99 | |
| 100 | addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fromfd, 0); |
| 101 | if (addr == MAP_FAILED) |
| 102 | goto out_close_to; |
| 103 | |
| 104 | if (write(tofd, addr, st.st_size) == st.st_size) |
| 105 | err = 0; |
| 106 | |
| 107 | munmap(addr, st.st_size); |
| 108 | out_close_to: |
| 109 | close(tofd); |
| 110 | if (err) |
| 111 | unlink(to); |
| 112 | out_close_from: |
| 113 | close(fromfd); |
| 114 | out: |
| 115 | return err; |
| 116 | } |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 117 | |
| 118 | unsigned long convert_unit(unsigned long value, char *unit) |
| 119 | { |
| 120 | *unit = ' '; |
| 121 | |
| 122 | if (value > 1000) { |
| 123 | value /= 1000; |
| 124 | *unit = 'K'; |
| 125 | } |
| 126 | |
| 127 | if (value > 1000) { |
| 128 | value /= 1000; |
| 129 | *unit = 'M'; |
| 130 | } |
| 131 | |
| 132 | if (value > 1000) { |
| 133 | value /= 1000; |
| 134 | *unit = 'G'; |
| 135 | } |
| 136 | |
| 137 | return value; |
| 138 | } |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 139 | |
| 140 | int readn(int fd, void *buf, size_t n) |
| 141 | { |
| 142 | void *buf_start = buf; |
| 143 | |
| 144 | while (n) { |
| 145 | int ret = read(fd, buf, n); |
| 146 | |
| 147 | if (ret <= 0) |
| 148 | return ret; |
| 149 | |
| 150 | n -= ret; |
| 151 | buf += ret; |
| 152 | } |
| 153 | |
| 154 | return buf - buf_start; |
| 155 | } |
Arnaldo Carvalho de Melo | 61e04b3 | 2012-04-19 13:15:24 -0300 | [diff] [blame] | 156 | |
| 157 | size_t hex_width(u64 v) |
| 158 | { |
| 159 | size_t n = 1; |
| 160 | |
| 161 | while ((v >>= 4)) |
| 162 | ++n; |
| 163 | |
| 164 | return n; |
| 165 | } |
Arnaldo Carvalho de Melo | dc4552b | 2012-08-07 23:32:05 -0300 | [diff] [blame] | 166 | |
| 167 | /* Obtain a backtrace and print it to stdout. */ |
Irina Tirdea | c9f08be | 2012-09-08 03:43:23 +0300 | [diff] [blame] | 168 | #ifndef NO_BACKTRACE |
Arnaldo Carvalho de Melo | dc4552b | 2012-08-07 23:32:05 -0300 | [diff] [blame] | 169 | void dump_stack(void) |
| 170 | { |
| 171 | void *array[16]; |
| 172 | size_t size = backtrace(array, ARRAY_SIZE(array)); |
| 173 | char **strings = backtrace_symbols(array, size); |
| 174 | size_t i; |
| 175 | |
| 176 | printf("Obtained %zd stack frames.\n", size); |
| 177 | |
| 178 | for (i = 0; i < size; i++) |
| 179 | printf("%s\n", strings[i]); |
| 180 | |
| 181 | free(strings); |
| 182 | } |
Irina Tirdea | c9f08be | 2012-09-08 03:43:23 +0300 | [diff] [blame] | 183 | #else |
| 184 | void dump_stack(void) {} |
| 185 | #endif |