Add a wrapper for fetching, parsing and pretty-printing kernel's extended
syscall error reports in a manner that can be useful for communicating
errors to the user.

Signed-off-by: Alexander Shishkin <[email protected]>
---
 tools/perf/util/Build    |  1 +
 tools/perf/util/exterr.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/exterr.h | 21 +++++++++++++
 3 files changed, 101 insertions(+)
 create mode 100644 tools/perf/util/exterr.c
 create mode 100644 tools/perf/util/exterr.h

diff --git a/tools/perf/util/Build b/tools/perf/util/Build
index af5acb9a02..2ccfb3e0e3 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -75,6 +75,7 @@ libperf-y += stat-shadow.o
 libperf-y += record.o
 libperf-y += srcline.o
 libperf-y += data.o
+libperf-y += exterr.o
 libperf-$(CONFIG_X86) += tsc.o
 libperf-$(CONFIG_AUXTRACE) += tsc.o
 libperf-y += cloexec.o
diff --git a/tools/perf/util/exterr.c b/tools/perf/util/exterr.c
new file mode 100644
index 0000000000..3091009688
--- /dev/null
+++ b/tools/perf/util/exterr.c
@@ -0,0 +1,79 @@
+/*
+ * exterr.c: Extended syscall error reporting support
+ * Copyright (c) 2015, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ */
+
+#include <sys/prctl.h>
+#include <stdio.h>
+
+#include "tools/json.h"
+#include "util/util.h"
+#include "util/exterr.h"
+
+static char message[BUFSIZ], attr_field[BUFSIZ], line[8], code[8];
+
+#define JSON_FIELD(name)                                               \
+       { .key = __stringify(name), .value = (name), .size = sizeof(name), }
+
+static struct json_member exterr_schema[] = {
+       JSON_FIELD(line),
+       JSON_FIELD(code),
+       JSON_FIELD(message),
+       JSON_FIELD(attr_field),
+       { .key = NULL },
+};
+
+ssize_t exterr__strerror(char *msg, size_t size)
+{
+       char sbuf[BUFSIZ];
+       size_t len;
+       int ret;
+
+       ret = prctl(PR_GET_ERR_DESC, sbuf, sizeof(sbuf), 0, 0);
+       if (ret > 0) {
+               struct json_parser p = {
+                       .buffer = sbuf,
+                       .end    = sbuf + strlen(sbuf),
+                       .schema = exterr_schema,
+                       .schema_strict = 0,
+               };
+
+               ret = parse_json(&p);
+               if (!ret) {
+                       int orig_err;
+
+                       orig_err = atoi(code);
+                       ret = scnprintf(msg, size, "Syscall returned %d, 
becasue %s.\n",
+                                       orig_err, message);
+                       len   = ret;
+                       msg  += ret;
+                       size -= ret;
+
+                       if (attr_field[0]) {
+                               /*
+                                * there can also be a lookup table with more
+                                * helpful messages based on this field
+                                */
+                               ret = scnprintf(msg, size, "Offending attribute 
field: \"%s\"\n",
+                                               attr_field);
+                               len  += ret;
+                               msg  += ret;
+                               size -= ret;
+                       }
+
+                       ret = len;
+               }
+       }
+
+       return ret;
+}
diff --git a/tools/perf/util/exterr.h b/tools/perf/util/exterr.h
new file mode 100644
index 0000000000..fce70e4f31
--- /dev/null
+++ b/tools/perf/util/exterr.h
@@ -0,0 +1,21 @@
+/*
+ * exterr.h: Extended syscall error reporting support
+ * Copyright (c) 2015, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ */
+
+#ifndef __PERF_EXTERR_H
+#define __PERF_EXTERR_H 1
+
+ssize_t exterr__strerror(char *msg, size_t size);
+
+#endif /* __PERF_EXTERR_H */
-- 
2.5.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Reply via email to