6 #ifdef HAVE_BACKTRACE_SUPPORT
15 #include <linux/kernel.h>
18 * XXX We need to find a better place for these things...
20 unsigned int page_size
;
23 bool test_attr__enabled
;
25 bool perf_host
= true;
26 bool perf_guest
= false;
28 char tracing_events_path
[PATH_MAX
+ 1] = "/sys/kernel/debug/tracing/events";
30 void event_attr_init(struct perf_event_attr
*attr
)
33 attr
->exclude_host
= 1;
35 attr
->exclude_guest
= 1;
36 /* to capture ABI version */
37 attr
->size
= sizeof(*attr
);
40 int mkdir_p(char *path
, mode_t mode
)
49 if (stat(path
, &st
) == 0)
54 while ((d
= strchr(d
, '/'))) {
56 err
= stat(path
, &st
) && mkdir(path
, mode
);
63 return (stat(path
, &st
) && mkdir(path
, mode
)) ? -1 : 0;
66 static int slow_copyfile(const char *from
, const char *to
, mode_t mode
)
71 FILE *from_fp
= fopen(from
, "r"), *to_fp
;
77 old_umask
= umask(mode
^ 0777);
78 to_fp
= fopen(to
, "w");
83 while (getline(&line
, &n
, from_fp
) > 0)
84 if (fputs(line
, to_fp
) == EOF
)
96 int copyfile_mode(const char *from
, const char *to
, mode_t mode
)
106 if (st
.st_size
== 0) /* /proc? do it slowly... */
107 return slow_copyfile(from
, to
, mode
);
109 fromfd
= open(from
, O_RDONLY
);
113 tofd
= creat(to
, mode
);
117 addr
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fromfd
, 0);
118 if (addr
== MAP_FAILED
)
121 if (write(tofd
, addr
, st
.st_size
) == st
.st_size
)
124 munmap(addr
, st
.st_size
);
135 int copyfile(const char *from
, const char *to
)
137 return copyfile_mode(from
, to
, 0755);
140 unsigned long convert_unit(unsigned long value
, char *unit
)
162 static ssize_t
ion(bool is_read
, int fd
, void *buf
, size_t n
)
164 void *buf_start
= buf
;
168 ssize_t ret
= is_read
? read(fd
, buf
, left
) :
169 write(fd
, buf
, left
);
171 if (ret
< 0 && errno
== EINTR
)
180 BUG_ON((size_t)(buf
- buf_start
) != n
);
185 * Read exactly 'n' bytes or return an error.
187 ssize_t
readn(int fd
, void *buf
, size_t n
)
189 return ion(true, fd
, buf
, n
);
193 * Write exactly 'n' bytes or return an error.
195 ssize_t
writen(int fd
, void *buf
, size_t n
)
197 return ion(false, fd
, buf
, n
);
200 size_t hex_width(u64 v
)
210 static int hex(char ch
)
212 if ((ch
>= '0') && (ch
<= '9'))
214 if ((ch
>= 'a') && (ch
<= 'f'))
215 return ch
- 'a' + 10;
216 if ((ch
>= 'A') && (ch
<= 'F'))
217 return ch
- 'A' + 10;
222 * While we find nice hex chars, build a long_val.
223 * Return number of chars processed.
225 int hex2u64(const char *ptr
, u64
*long_val
)
231 const int hex_val
= hex(*p
);
236 *long_val
= (*long_val
<< 4) | hex_val
;
243 /* Obtain a backtrace and print it to stdout. */
244 #ifdef HAVE_BACKTRACE_SUPPORT
245 void dump_stack(void)
248 size_t size
= backtrace(array
, ARRAY_SIZE(array
));
249 char **strings
= backtrace_symbols(array
, size
);
252 printf("Obtained %zd stack frames.\n", size
);
254 for (i
= 0; i
< size
; i
++)
255 printf("%s\n", strings
[i
]);
260 void dump_stack(void) {}
263 void get_term_dimensions(struct winsize
*ws
)
265 char *s
= getenv("LINES");
268 ws
->ws_row
= atoi(s
);
269 s
= getenv("COLUMNS");
271 ws
->ws_col
= atoi(s
);
272 if (ws
->ws_row
&& ws
->ws_col
)
277 if (ioctl(1, TIOCGWINSZ
, ws
) == 0 &&
278 ws
->ws_row
&& ws
->ws_col
)
285 static void set_tracing_events_path(const char *mountpoint
)
287 snprintf(tracing_events_path
, sizeof(tracing_events_path
), "%s/%s",
288 mountpoint
, "tracing/events");
291 const char *perf_debugfs_mount(const char *mountpoint
)
295 mnt
= debugfs_mount(mountpoint
);
299 set_tracing_events_path(mnt
);
304 void perf_debugfs_set_path(const char *mntpt
)
306 snprintf(debugfs_mountpoint
, strlen(debugfs_mountpoint
), "%s", mntpt
);
307 set_tracing_events_path(mntpt
);
310 static const char *find_debugfs(void)
312 const char *path
= perf_debugfs_mount(NULL
);
315 fprintf(stderr
, "Your kernel does not support the debugfs filesystem");
321 * Finds the path to the debugfs/tracing
322 * Allocates the string and stores it.
324 const char *find_tracing_dir(void)
326 static char *tracing
;
327 static int tracing_found
;
333 debugfs
= find_debugfs();
337 if (asprintf(&tracing
, "%s/tracing", debugfs
) < 0)
344 char *get_tracing_file(const char *name
)
349 tracing
= find_tracing_dir();
353 if (asprintf(&file
, "%s/%s", tracing
, name
) < 0)
359 void put_tracing_file(char *file
)
364 int parse_nsec_time(const char *str
, u64
*ptime
)
366 u64 time_sec
, time_nsec
;
369 time_sec
= strtoul(str
, &end
, 10);
370 if (*end
!= '.' && *end
!= '\0')
377 if (strlen(++end
) > 9)
380 strncpy(nsec_buf
, end
, 9);
383 /* make it nsec precision */
384 for (i
= strlen(nsec_buf
); i
< 9; i
++)
387 time_nsec
= strtoul(nsec_buf
, &end
, 10);
393 *ptime
= time_sec
* NSEC_PER_SEC
+ time_nsec
;
397 unsigned long parse_tag_value(const char *str
, struct parse_tag
*tags
)
399 struct parse_tag
*i
= tags
;
404 s
= strchr(str
, i
->tag
);
406 unsigned long int value
;
409 value
= strtoul(str
, &endptr
, 10);
413 if (value
> ULONG_MAX
/ i
->mult
)
421 return (unsigned long) -1;
424 int filename__read_int(const char *filename
, int *value
)
427 int fd
= open(filename
, O_RDONLY
), err
= -1;
432 if (read(fd
, line
, sizeof(line
)) > 0) {
441 int filename__read_str(const char *filename
, char **buf
, size_t *sizep
)
443 size_t size
= 0, alloc_size
= 0;
444 void *bf
= NULL
, *nbf
;
447 fd
= open(filename
, O_RDONLY
);
452 if (size
== alloc_size
) {
453 alloc_size
+= BUFSIZ
;
454 nbf
= realloc(bf
, alloc_size
);
463 n
= read(fd
, bf
+ size
, alloc_size
- size
);
466 pr_warning("read failed %d: %s\n",
467 errno
, strerror(errno
));
488 const char *get_filename_for_perf_kvm(void)
490 const char *filename
;
492 if (perf_host
&& !perf_guest
)
493 filename
= strdup("perf.data.host");
494 else if (!perf_host
&& perf_guest
)
495 filename
= strdup("perf.data.guest");
497 filename
= strdup("perf.data.kvm");
502 int perf_event_paranoid(void)
505 const char *procfs
= procfs__mountpoint();
511 scnprintf(path
, PATH_MAX
, "%s/sys/kernel/perf_event_paranoid", procfs
);
513 if (filename__read_int(path
, &value
))
519 void mem_bswap_32(void *src
, int byte_size
)
522 while (byte_size
> 0) {
524 byte_size
-= sizeof(u32
);
529 void mem_bswap_64(void *src
, int byte_size
)
533 while (byte_size
> 0) {
535 byte_size
-= sizeof(u64
);