]> asedeno.scripts.mit.edu Git - linux.git/blobdiff - tools/perf/util/probe-file.c
perf probe: Add supported for type casting by the running kernel
[linux.git] / tools / perf / util / probe-file.c
index 9aed9c332da656c69d91693e531df87fb03007b1..697ef66bff91c7c1cdc9f947dd173c98ce67f266 100644 (file)
@@ -133,7 +133,7 @@ int probe_file__open_both(int *kfd, int *ufd, int flag)
 /* Get raw string list of current kprobe_events  or uprobe_events */
 struct strlist *probe_file__get_rawlist(int fd)
 {
-       int ret, idx;
+       int ret, idx, fddup;
        FILE *fp;
        char buf[MAX_CMDLEN];
        char *p;
@@ -143,8 +143,17 @@ struct strlist *probe_file__get_rawlist(int fd)
                return NULL;
 
        sl = strlist__new(NULL, NULL);
+       if (sl == NULL)
+               return NULL;
+
+       fddup = dup(fd);
+       if (fddup < 0)
+               goto out_free_sl;
+
+       fp = fdopen(fddup, "r");
+       if (!fp)
+               goto out_close_fddup;
 
-       fp = fdopen(dup(fd), "r");
        while (!feof(fp)) {
                p = fgets(buf, MAX_CMDLEN, fp);
                if (!p)
@@ -156,13 +165,21 @@ struct strlist *probe_file__get_rawlist(int fd)
                ret = strlist__add(sl, buf);
                if (ret < 0) {
                        pr_debug("strlist__add failed (%d)\n", ret);
-                       strlist__delete(sl);
-                       return NULL;
+                       goto out_close_fp;
                }
        }
        fclose(fp);
 
        return sl;
+
+out_close_fp:
+       fclose(fp);
+       goto out_free_sl;
+out_close_fddup:
+       close(fddup);
+out_free_sl:
+       strlist__delete(sl);
+       return NULL;
 }
 
 static struct strlist *__probe_file__get_namelist(int fd, bool include_group)
@@ -447,12 +464,17 @@ static int probe_cache__load(struct probe_cache *pcache)
 {
        struct probe_cache_entry *entry = NULL;
        char buf[MAX_CMDLEN], *p;
-       int ret = 0;
+       int ret = 0, fddup;
        FILE *fp;
 
-       fp = fdopen(dup(pcache->fd), "r");
-       if (!fp)
+       fddup = dup(pcache->fd);
+       if (fddup < 0)
+               return -errno;
+       fp = fdopen(fddup, "r");
+       if (!fp) {
+               close(fddup);
                return -EINVAL;
+       }
 
        while (!feof(fp)) {
                if (!fgets(buf, MAX_CMDLEN, fp))
@@ -855,3 +877,60 @@ int probe_cache__show_all_caches(struct strfilter *filter)
 
        return 0;
 }
+
+static struct {
+       const char *pattern;
+       bool    avail;
+       bool    checked;
+} probe_type_table[] = {
+#define DEFINE_TYPE(idx, pat, def_avail)       \
+       [idx] = {.pattern = pat, .avail = (def_avail)}
+       DEFINE_TYPE(PROBE_TYPE_U, "* u8/16/32/64,*", true),
+       DEFINE_TYPE(PROBE_TYPE_S, "* s8/16/32/64,*", true),
+       DEFINE_TYPE(PROBE_TYPE_X, "* x8/16/32/64,*", false),
+       DEFINE_TYPE(PROBE_TYPE_STRING, "* string,*", true),
+       DEFINE_TYPE(PROBE_TYPE_BITFIELD,
+                   "* b<bit-width>@<bit-offset>/<container-size>", true),
+};
+
+bool probe_type_is_available(enum probe_type type)
+{
+       FILE *fp;
+       char *buf = NULL;
+       size_t len = 0;
+       bool target_line = false;
+       bool ret = probe_type_table[type].avail;
+
+       if (type >= PROBE_TYPE_END)
+               return false;
+       /* We don't have to check the type which supported by default */
+       if (ret || probe_type_table[type].checked)
+               return ret;
+
+       if (asprintf(&buf, "%s/README", tracing_path) < 0)
+               return ret;
+
+       fp = fopen(buf, "r");
+       if (!fp)
+               goto end;
+
+       zfree(&buf);
+       while (getline(&buf, &len, fp) > 0 && !ret) {
+               if (!target_line) {
+                       target_line = !!strstr(buf, " type: ");
+                       if (!target_line)
+                               continue;
+               } else if (strstr(buf, "\t          ") != buf)
+                       break;
+               ret = strglobmatch(buf, probe_type_table[type].pattern);
+       }
+       /* Cache the result */
+       probe_type_table[type].checked = true;
+       probe_type_table[type].avail = ret;
+
+       fclose(fp);
+end:
+       free(buf);
+
+       return ret;
+}