diff mbox series

[21/35] perf tools: Make has_kcore_dir() work also for guest kcore_dir

Message ID 20220711093218.10967-22-adrian.hunter@intel.com (mailing list archive)
State New, archived
Headers show
Series perf intel-pt: Add support for tracing virtual machine user space on the host | expand

Commit Message

Adrian Hunter July 11, 2022, 9:32 a.m. UTC
Copies of /proc/kallsyms, /proc/modules and an extract of /proc/kcore can
be stored in the perf.data output directory under the subdirectory named
kcore_dir. Guest machines will have their files also under subdirectories
beginning kcore_dir__ followed by the machine pid. Make has_kcore_dir()
return true also if there is a guest machine kcore_dir.

Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
---
 tools/perf/util/data.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)

Comments

Ian Rogers July 20, 2022, 12:49 a.m. UTC | #1
On Mon, Jul 11, 2022 at 2:33 AM Adrian Hunter <adrian.hunter@intel.com> wrote:
>
> Copies of /proc/kallsyms, /proc/modules and an extract of /proc/kcore can
> be stored in the perf.data output directory under the subdirectory named
> kcore_dir. Guest machines will have their files also under subdirectories
> beginning kcore_dir__ followed by the machine pid. Make has_kcore_dir()
> return true also if there is a guest machine kcore_dir.
>
> Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>

Acked-by: Ian Rogers <irogers@google.com>

Thanks,
Ian

> ---
>  tools/perf/util/data.c | 24 +++++++++++++++---------
>  1 file changed, 15 insertions(+), 9 deletions(-)
>
> diff --git a/tools/perf/util/data.c b/tools/perf/util/data.c
> index caabeac24c69..9782ccbe595d 100644
> --- a/tools/perf/util/data.c
> +++ b/tools/perf/util/data.c
> @@ -3,6 +3,7 @@
>  #include <linux/kernel.h>
>  #include <linux/string.h>
>  #include <linux/zalloc.h>
> +#include <linux/err.h>
>  #include <sys/types.h>
>  #include <sys/stat.h>
>  #include <errno.h>
> @@ -481,16 +482,21 @@ int perf_data__make_kcore_dir(struct perf_data *data, char *buf, size_t buf_sz)
>
>  bool has_kcore_dir(const char *path)
>  {
> -       char *kcore_dir;
> -       int ret;
> -
> -       if (asprintf(&kcore_dir, "%s/kcore_dir", path) < 0)
> -               return false;
> -
> -       ret = access(kcore_dir, F_OK);
> +       struct dirent *d = ERR_PTR(-EINVAL);
> +       const char *name = "kcore_dir";
> +       DIR *dir = opendir(path);
> +       size_t n = strlen(name);
> +       bool result = false;
> +
> +       if (dir) {
> +               while (d && !result) {
> +                       d = readdir(dir);
> +                       result = d ? strncmp(d->d_name, name, n) : false;
> +               }
> +               closedir(dir);
> +       }
>
> -       free(kcore_dir);
> -       return !ret;
> +       return result;
>  }
>
>  char *perf_data__kallsyms_name(struct perf_data *data)
> --
> 2.25.1
>
diff mbox series

Patch

diff --git a/tools/perf/util/data.c b/tools/perf/util/data.c
index caabeac24c69..9782ccbe595d 100644
--- a/tools/perf/util/data.c
+++ b/tools/perf/util/data.c
@@ -3,6 +3,7 @@ 
 #include <linux/kernel.h>
 #include <linux/string.h>
 #include <linux/zalloc.h>
+#include <linux/err.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <errno.h>
@@ -481,16 +482,21 @@  int perf_data__make_kcore_dir(struct perf_data *data, char *buf, size_t buf_sz)
 
 bool has_kcore_dir(const char *path)
 {
-	char *kcore_dir;
-	int ret;
-
-	if (asprintf(&kcore_dir, "%s/kcore_dir", path) < 0)
-		return false;
-
-	ret = access(kcore_dir, F_OK);
+	struct dirent *d = ERR_PTR(-EINVAL);
+	const char *name = "kcore_dir";
+	DIR *dir = opendir(path);
+	size_t n = strlen(name);
+	bool result = false;
+
+	if (dir) {
+		while (d && !result) {
+			d = readdir(dir);
+			result = d ? strncmp(d->d_name, name, n) : false;
+		}
+		closedir(dir);
+	}
 
-	free(kcore_dir);
-	return !ret;
+	return result;
 }
 
 char *perf_data__kallsyms_name(struct perf_data *data)