Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2008,2009, Steven Rostedt <srostedt@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <dirent.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <mntent.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <stdarg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <sys/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/zalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <internal/lib.h> // page_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include "trace-event.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <api/fs/tracing_path.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include "evsel.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include "debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define VERSION "0.6"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static int output_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) int bigendian(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	unsigned char str[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	unsigned int *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	ptr = (unsigned int *)(void *)str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	return *ptr == 0x01020304;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) /* unfortunately, you can not stat debugfs or proc files for size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static int record_file(const char *file, ssize_t hdr_sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	unsigned long long size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	char buf[BUFSIZ], *sizep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	off_t hdr_pos = lseek(output_fd, 0, SEEK_CUR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	int r, fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	int err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	fd = open(file, O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		pr_debug("Can't read '%s'", file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	/* put in zeros for file size, then fill true size later */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	if (hdr_sz) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		if (write(output_fd, &size, hdr_sz) != hdr_sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		r = read(fd, buf, BUFSIZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		if (r > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 			size += r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 			if (write(output_fd, buf, r) != r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	} while (r > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	/* ugh, handle big-endian hdr_size == 4 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	sizep = (char*)&size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (bigendian())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		sizep += sizeof(u64) - hdr_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	if (hdr_sz && pwrite(output_fd, sizep, hdr_sz, hdr_pos) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		pr_debug("writing file size failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) static int record_header_files(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	char *path = get_events_file("header_page");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	struct stat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	int err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	if (!path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		pr_debug("can't get tracing/events/header_page");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	if (stat(path, &st) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		pr_debug("can't read '%s'", path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	if (write(output_fd, "header_page", 12) != 12) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		pr_debug("can't write header_page\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (record_file(path, 8) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		pr_debug("can't record header_page file\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	put_events_file(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	path = get_events_file("header_event");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (!path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		pr_debug("can't get tracing/events/header_event");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	if (stat(path, &st) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		pr_debug("can't read '%s'", path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	if (write(output_fd, "header_event", 13) != 13) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		pr_debug("can't write header_event\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	if (record_file(path, 8) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		pr_debug("can't record header_event file\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	put_events_file(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static bool name_in_tp_list(char *sys, struct tracepoint_path *tps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	while (tps) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		if (!strcmp(sys, tps->name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		tps = tps->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) #define for_each_event(dir, dent, tps)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	while ((dent = readdir(dir)))				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		if (dent->d_type == DT_DIR &&			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		    (strcmp(dent->d_name, ".")) &&		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		    (strcmp(dent->d_name, "..")))		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static int copy_event_system(const char *sys, struct tracepoint_path *tps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	struct dirent *dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	struct stat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	char *format;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	DIR *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	int count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	dir = opendir(sys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	if (!dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		pr_debug("can't read directory '%s'", sys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	for_each_event(dir, dent, tps) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		if (!name_in_tp_list(dent->d_name, tps))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		if (asprintf(&format, "%s/%s/format", sys, dent->d_name) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 			err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		ret = stat(format, &st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		free(format);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	if (write(output_fd, &count, 4) != 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		pr_debug("can't write count\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	rewinddir(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	for_each_event(dir, dent, tps) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		if (!name_in_tp_list(dent->d_name, tps))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		if (asprintf(&format, "%s/%s/format", sys, dent->d_name) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 			err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		ret = stat(format, &st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		if (ret >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			err = record_file(format, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 			if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 				free(format);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		free(format);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	closedir(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static int record_ftrace_files(struct tracepoint_path *tps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	char *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	path = get_events_file("ftrace");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	if (!path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		pr_debug("can't get tracing/events/ftrace");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	ret = copy_event_system(path, tps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	put_tracing_file(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) static bool system_in_tp_list(char *sys, struct tracepoint_path *tps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	while (tps) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		if (!strcmp(sys, tps->system))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		tps = tps->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) static int record_event_files(struct tracepoint_path *tps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	struct dirent *dent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	struct stat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	char *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	char *sys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	DIR *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	int count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	path = get_tracing_file("events");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	if (!path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		pr_debug("can't get tracing/events");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	dir = opendir(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	if (!dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		err = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		pr_debug("can't read directory '%s'", path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	for_each_event(dir, dent, tps) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		if (strcmp(dent->d_name, "ftrace") == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		    !system_in_tp_list(dent->d_name, tps))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	if (write(output_fd, &count, 4) != 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		pr_debug("can't write count\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	rewinddir(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	for_each_event(dir, dent, tps) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		if (strcmp(dent->d_name, "ftrace") == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		    !system_in_tp_list(dent->d_name, tps))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		if (asprintf(&sys, "%s/%s", path, dent->d_name) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 			err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		ret = stat(sys, &st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		if (ret >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 			ssize_t size = strlen(dent->d_name) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 			if (write(output_fd, dent->d_name, size) != size ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 			    copy_event_system(sys, tps) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 				err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 				free(sys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		free(sys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	closedir(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	put_tracing_file(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) static int record_proc_kallsyms(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	unsigned long long size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	 * Just to keep older perf.data file parsers happy, record a zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	 * sized kallsyms file, i.e. do the same thing that was done when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	 * /proc/kallsyms (or something specified via --kallsyms, in a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	 * different path) couldn't be read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	return write(output_fd, &size, 4) != 4 ? -EIO : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) static int record_ftrace_printk(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	unsigned int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	char *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	struct stat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	int ret, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	path = get_tracing_file("printk_formats");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	if (!path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		pr_debug("can't get tracing/printk_formats");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	ret = stat(path, &st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		/* not found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		if (write(output_fd, &size, 4) != 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 			err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	err = record_file(path, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	put_tracing_file(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) static int record_saved_cmdline(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	unsigned long long size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	char *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	struct stat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	int ret, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	path = get_tracing_file("saved_cmdlines");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	if (!path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		pr_debug("can't get tracing/saved_cmdline");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	ret = stat(path, &st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		/* not found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		if (write(output_fd, &size, 8) != 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 			err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	err = record_file(path, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	put_tracing_file(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) put_tracepoints_path(struct tracepoint_path *tps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	while (tps) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		struct tracepoint_path *t = tps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		tps = tps->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		zfree(&t->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		zfree(&t->system);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		free(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) static struct tracepoint_path *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) get_tracepoints_path(struct list_head *pattrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	struct tracepoint_path path, *ppath = &path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	struct evsel *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	int nr_tracepoints = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	list_for_each_entry(pos, pattrs, core.node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		if (pos->core.attr.type != PERF_TYPE_TRACEPOINT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		++nr_tracepoints;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		if (pos->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 			ppath->next = tracepoint_name_to_path(pos->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 			if (ppath->next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 				goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 			if (strchr(pos->name, ':') == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 				goto try_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) try_id:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 		ppath->next = tracepoint_id_to_path(pos->core.attr.config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 		if (!ppath->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 			pr_debug("No memory to alloc tracepoints list\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 			put_tracepoints_path(path.next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		ppath = ppath->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	return nr_tracepoints > 0 ? path.next : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) bool have_tracepoints(struct list_head *pattrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	struct evsel *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	list_for_each_entry(pos, pattrs, core.node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		if (pos->core.attr.type == PERF_TYPE_TRACEPOINT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) static int tracing_data_header(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	char buf[20];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	ssize_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	/* just guessing this is someone's birthday.. ;) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	buf[0] = 23;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	buf[1] = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	buf[2] = 68;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	memcpy(buf + 3, "tracing", 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	if (write(output_fd, buf, 10) != 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	size = strlen(VERSION) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	if (write(output_fd, VERSION, size) != size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	/* save endian */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	if (bigendian())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		buf[0] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		buf[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	if (write(output_fd, buf, 1) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	/* save size of long */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	buf[0] = sizeof(long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	if (write(output_fd, buf, 1) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	/* save page_size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	if (write(output_fd, &page_size, 4) != 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) struct tracing_data *tracing_data_get(struct list_head *pattrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 				      int fd, bool temp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	struct tracepoint_path *tps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	struct tracing_data *tdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	output_fd = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	tps = get_tracepoints_path(pattrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	if (!tps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	tdata = malloc(sizeof(*tdata));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	if (!tdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	tdata->temp = temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	tdata->size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	if (temp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 		int temp_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		snprintf(tdata->temp_file, sizeof(tdata->temp_file),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 			 "/tmp/perf-XXXXXX");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 		if (!mkstemp(tdata->temp_file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 			pr_debug("Can't make temp file");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 			free(tdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		temp_fd = open(tdata->temp_file, O_RDWR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 		if (temp_fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 			pr_debug("Can't read '%s'", tdata->temp_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 			free(tdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 		 * Set the temp file the default output, so all the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 		 * tracing data are stored into it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 		output_fd = temp_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	err = tracing_data_header();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	err = record_header_files();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	err = record_ftrace_files(tps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	err = record_event_files(tps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	err = record_proc_kallsyms();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	err = record_ftrace_printk();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	err = record_saved_cmdline();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	 * All tracing data are stored by now, we can restore
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	 * the default output file in case we used temp file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	if (temp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 		tdata->size = lseek(output_fd, 0, SEEK_CUR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 		close(output_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 		output_fd = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 		zfree(&tdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	put_tracepoints_path(tps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	return tdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) int tracing_data_put(struct tracing_data *tdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	if (tdata->temp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 		err = record_file(tdata->temp_file, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 		unlink(tdata->temp_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	free(tdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) int read_tracing_data(int fd, struct list_head *pattrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	struct tracing_data *tdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	 * We work over the real file, so we can write data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	 * directly, no temp file is needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	tdata = tracing_data_get(pattrs, fd, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	if (!tdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	err = tracing_data_put(tdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) }