perf script: Add more filter to find_scripts()
[deliverable/linux.git] / tools / perf / util / util.c
CommitLineData
1aed2671 1#include "../perf.h"
4cf40131 2#include "util.h"
69e3f52d 3#include <sys/mman.h>
4e34d958 4#ifdef BACKTRACE_SUPPORT
dc4552bf 5#include <execinfo.h>
c9f08bee 6#endif
dc4552bf
ACM
7#include <stdio.h>
8#include <stdlib.h>
4cf40131 9
1aed2671
JR
10/*
11 * XXX We need to find a better place for these things...
12 */
0c1fe6b2
ACM
13unsigned int page_size;
14
1aed2671 15bool perf_host = true;
c4a7dca9 16bool perf_guest = false;
1aed2671
JR
17
18void event_attr_init(struct perf_event_attr *attr)
19{
20 if (!perf_host)
21 attr->exclude_host = 1;
22 if (!perf_guest)
23 attr->exclude_guest = 1;
7e1ccd38
SE
24 /* to capture ABI version */
25 attr->size = sizeof(*attr);
1aed2671
JR
26}
27
4cf40131
ACM
28int mkdir_p(char *path, mode_t mode)
29{
30 struct stat st;
31 int err;
32 char *d = path;
33
34 if (*d != '/')
35 return -1;
36
37 if (stat(path, &st) == 0)
38 return 0;
39
40 while (*++d == '/');
41
42 while ((d = strchr(d, '/'))) {
43 *d = '\0';
44 err = stat(path, &st) && mkdir(path, mode);
45 *d++ = '/';
46 if (err)
47 return -1;
48 while (*d == '/')
49 ++d;
50 }
51 return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
52}
53
9e201442
ACM
54static int slow_copyfile(const char *from, const char *to)
55{
56 int err = 0;
57 char *line = NULL;
58 size_t n;
59 FILE *from_fp = fopen(from, "r"), *to_fp;
60
61 if (from_fp == NULL)
62 goto out;
63
64 to_fp = fopen(to, "w");
65 if (to_fp == NULL)
66 goto out_fclose_from;
67
68 while (getline(&line, &n, from_fp) > 0)
69 if (fputs(line, to_fp) == EOF)
70 goto out_fclose_to;
71 err = 0;
72out_fclose_to:
73 fclose(to_fp);
74 free(line);
75out_fclose_from:
76 fclose(from_fp);
77out:
78 return err;
79}
80
4cf40131
ACM
81int copyfile(const char *from, const char *to)
82{
83 int fromfd, tofd;
84 struct stat st;
85 void *addr;
86 int err = -1;
87
88 if (stat(from, &st))
89 goto out;
90
9e201442
ACM
91 if (st.st_size == 0) /* /proc? do it slowly... */
92 return slow_copyfile(from, to);
93
4cf40131
ACM
94 fromfd = open(from, O_RDONLY);
95 if (fromfd < 0)
96 goto out;
97
98 tofd = creat(to, 0755);
99 if (tofd < 0)
100 goto out_close_from;
101
102 addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fromfd, 0);
103 if (addr == MAP_FAILED)
104 goto out_close_to;
105
106 if (write(tofd, addr, st.st_size) == st.st_size)
107 err = 0;
108
109 munmap(addr, st.st_size);
110out_close_to:
111 close(tofd);
112 if (err)
113 unlink(to);
114out_close_from:
115 close(fromfd);
116out:
117 return err;
118}
c82ee828
ACM
119
120unsigned long convert_unit(unsigned long value, char *unit)
121{
122 *unit = ' ';
123
124 if (value > 1000) {
125 value /= 1000;
126 *unit = 'K';
127 }
128
129 if (value > 1000) {
130 value /= 1000;
131 *unit = 'M';
132 }
133
134 if (value > 1000) {
135 value /= 1000;
136 *unit = 'G';
137 }
138
139 return value;
140}
1e7972cc
ACM
141
142int readn(int fd, void *buf, size_t n)
143{
144 void *buf_start = buf;
145
146 while (n) {
147 int ret = read(fd, buf, n);
148
149 if (ret <= 0)
150 return ret;
151
152 n -= ret;
153 buf += ret;
154 }
155
156 return buf - buf_start;
157}
61e04b33
ACM
158
159size_t hex_width(u64 v)
160{
161 size_t n = 1;
162
163 while ((v >>= 4))
164 ++n;
165
166 return n;
167}
dc4552bf 168
b2aff5f6
JO
169static int hex(char ch)
170{
171 if ((ch >= '0') && (ch <= '9'))
172 return ch - '0';
173 if ((ch >= 'a') && (ch <= 'f'))
174 return ch - 'a' + 10;
175 if ((ch >= 'A') && (ch <= 'F'))
176 return ch - 'A' + 10;
177 return -1;
178}
179
180/*
181 * While we find nice hex chars, build a long_val.
182 * Return number of chars processed.
183 */
184int hex2u64(const char *ptr, u64 *long_val)
185{
186 const char *p = ptr;
187 *long_val = 0;
188
189 while (*p) {
190 const int hex_val = hex(*p);
191
192 if (hex_val < 0)
193 break;
194
195 *long_val = (*long_val << 4) | hex_val;
196 p++;
197 }
198
199 return p - ptr;
200}
201
dc4552bf 202/* Obtain a backtrace and print it to stdout. */
4e34d958 203#ifdef BACKTRACE_SUPPORT
dc4552bf
ACM
204void dump_stack(void)
205{
206 void *array[16];
207 size_t size = backtrace(array, ARRAY_SIZE(array));
208 char **strings = backtrace_symbols(array, size);
209 size_t i;
210
211 printf("Obtained %zd stack frames.\n", size);
212
213 for (i = 0; i < size; i++)
214 printf("%s\n", strings[i]);
215
216 free(strings);
217}
c9f08bee
IT
218#else
219void dump_stack(void) {}
220#endif
This page took 0.49466 seconds and 5 git commands to generate.