linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alexander Shishkin <alexander.shishkin@linux.intel.com>
To: Peter Zijlstra <a.p.zijlstra@chello.nl>, Ingo Molnar <mingo@redhat.com>
Cc: linux-kernel@vger.kernel.org, vince@deater.net,
	eranian@google.com, johannes@sipsolutions.net,
	Arnaldo Carvalho de Melo <acme@infradead.org>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>
Subject: [PATCH RFC v3 5/6] perf tools: Add userspace counterpart for extended error reporting
Date: Fri, 11 Sep 2015 19:00:04 +0300	[thread overview]
Message-ID: <1441987205-4021-6-git-send-email-alexander.shishkin@linux.intel.com> (raw)
In-Reply-To: <1441987205-4021-1-git-send-email-alexander.shishkin@linux.intel.com>

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 <alexander.shishkin@linux.intel.com>
---
 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


  parent reply	other threads:[~2015-09-11 16:02 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-11 15:59 [PATCH RFC v3 0/6] Introduce extended syscall error reporting Alexander Shishkin
2015-09-11 16:00 ` [PATCH RFC v3 1/6] exterr: " Alexander Shishkin
2015-09-14 20:19   ` Jonathan Corbet
2015-09-15 14:15     ` Alexander Shishkin
2015-09-15 14:31       ` Johannes Berg
2015-09-15 15:24         ` Alexander Shishkin
2015-09-15 16:35           ` Jonathan Corbet
2015-09-15 17:02             ` Johannes Berg
2015-09-14 21:59   ` Jonathan Corbet
2016-01-11 10:34   ` Alexander Shishkin
2015-09-11 16:00 ` [PATCH RFC v3 2/6] perf: Use " Alexander Shishkin
2015-09-11 16:00 ` [PATCH RFC v3 3/6] perf/x86: Annotate a BTS error with extended " Alexander Shishkin
2015-09-11 16:00 ` [PATCH RFC v3 4/6] perf tools: Add a simple JSON parser Alexander Shishkin
2015-09-11 16:00 ` Alexander Shishkin [this message]
2015-09-11 16:00 ` [PATCH RFC v3 6/6] perf tools: Use extended syscall error reporting Alexander Shishkin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1441987205-4021-6-git-send-email-alexander.shishkin@linux.intel.com \
    --to=alexander.shishkin@linux.intel.com \
    --cc=a.p.zijlstra@chello.nl \
    --cc=acme@infradead.org \
    --cc=eranian@google.com \
    --cc=johannes@sipsolutions.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=vince@deater.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).