perf probe: Add kernel source path option
authorChase Douglas <chase.douglas@canonical.com>
Thu, 17 Jun 2010 21:08:11 +0000 (17:08 -0400)
committerLeann Ogasawara <leann.ogasawara@canonical.com>
Mon, 28 Mar 2011 13:48:48 +0000 (06:48 -0700)
The probe plugin requires access to the source code for some operations.  The
source code must be in the exact same location as specified by the DWARF tags,
but sometimes the location is an absolute path that cannot be replicated by a
normal user. This change adds the -s|--source option to allow the user to
specify the root of the kernel source tree.

Cc: Ingo Molnar <mingo@elte.hu>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Masami Hiramatsu <mhiramat@redhat.com>
LKML-Reference: <1276543590-10486-1-git-send-email-chase.douglas@canonical.com>
Signed-off-by: Chase Douglas <chase.douglas@canonical.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
(cherry picked from commit 9ed7e1b85cd55dc46cb9410a23086bdaa2ff3eb9 from
git://git.kernel.org/pub/scm/linux/kernel/git/acme/linux-2.6.git)
Signed-off-by: Leann Ogasawara <leann.ogasawara@canonical.com>

tools/perf/util/probe-finder.c

index ab83b6a..ef0f20c 100644 (file)
@@ -57,6 +57,55 @@ static int strtailcmp(const char *s1, const char *s2)
        return 0;
 }
 
+/*
+ * Find a src file from a DWARF tag path. Prepend optional source path prefix
+ * and chop off leading directories that do not exist. Result is passed back as
+ * a newly allocated path on success.
+ * Return 0 if file was found and readable, -errno otherwise.
+ */
+static int get_real_path(const char *raw_path, char **new_path)
+{
+       if (!symbol_conf.source_prefix) {
+               if (access(raw_path, R_OK) == 0) {
+                       *new_path = strdup(raw_path);
+                       return 0;
+               } else
+                       return -errno;
+       }
+
+       *new_path = malloc((strlen(symbol_conf.source_prefix) +
+                           strlen(raw_path) + 2));
+       if (!*new_path)
+               return -ENOMEM;
+
+       for (;;) {
+               sprintf(*new_path, "%s/%s", symbol_conf.source_prefix,
+                       raw_path);
+
+               if (access(*new_path, R_OK) == 0)
+                       return 0;
+
+               switch (errno) {
+               case ENAMETOOLONG:
+               case ENOENT:
+               case EROFS:
+               case EFAULT:
+                       raw_path = strchr(++raw_path, '/');
+                       if (!raw_path) {
+                               free(*new_path);
+                               *new_path = NULL;
+                               return -ENOENT;
+                       }
+                       continue;
+
+               default:
+                       free(*new_path);
+                       *new_path = NULL;
+                       return -errno;
+               }
+       }
+}
+
 /* Line number list operations */
 
 /* Add a line to line number list */
@@ -1636,10 +1685,13 @@ static int line_range_add_line(const char *src, unsigned int lineno,
                               struct line_range *lr)
 {
        /* Copy source path */
+       int ret;
+
+       /* Copy real path */
        if (!lr->path) {
-               lr->path = strdup(src);
-               if (lr->path == NULL)
-                       return -ENOMEM;
+               ret = get_real_path(src, &lr->path);
+               if (ret != 0)
+                       return ret;
        }
        return line_list__add_line(&lr->line_list, lineno);
 }