From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753638AbbIKQCr (ORCPT ); Fri, 11 Sep 2015 12:02:47 -0400 Received: from mga14.intel.com ([192.55.52.115]:23244 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753171AbbIKQCq (ORCPT ); Fri, 11 Sep 2015 12:02:46 -0400 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.17,511,1437462000"; d="scan'208";a="802517636" From: Alexander Shishkin To: Peter Zijlstra , Ingo Molnar Cc: linux-kernel@vger.kernel.org, vince@deater.net, eranian@google.com, johannes@sipsolutions.net, Arnaldo Carvalho de Melo , Alexander Shishkin Subject: [PATCH RFC v3 5/6] perf tools: Add userspace counterpart for extended error reporting Date: Fri, 11 Sep 2015 19:00:04 +0300 Message-Id: <1441987205-4021-6-git-send-email-alexander.shishkin@linux.intel.com> X-Mailer: git-send-email 2.5.1 In-Reply-To: <1441987205-4021-1-git-send-email-alexander.shishkin@linux.intel.com> References: <1441987205-4021-1-git-send-email-alexander.shishkin@linux.intel.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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 +#include + +#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