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