linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jinli Xiao <jinli.xiao@nyu.edu>
To: unlisted-recipients:; (no To-header on input)
Cc: Jinli Xiao <jinli.xiao@nyu.edu>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@kernel.org>, Namhyung Kim <namhyung@kernel.org>,
	Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	Leo Yan <leo.yan@linaro.org>,
	Suzuki Poulouse <suzuki.poulose@arm.com>,
	James Clark <james.clark@arm.com>,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] perf python: Set error messages on call failure
Date: Tue,  2 May 2023 16:31:34 -0400	[thread overview]
Message-ID: <20230502203135.24794-1-jinli.xiao@nyu.edu> (raw)

Updates the perf python binding to provide more informative error
messages to the user when there is a call failure. The changes include
setting error messages on several different scenarios when the package
needs to return -1 or NULL.

Signed-off-by: Jinli Xiao <jinli.xiao@nyu.edu>
---
 tools/perf/util/python.c | 61 ++++++++++++++++++++++++++++++----------
 1 file changed, 46 insertions(+), 15 deletions(-)

diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index 42e8b813d010..7e6b12e87744 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -635,12 +635,16 @@ static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus,
 	char *cpustr = NULL;
 
 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s",
-					 kwlist, &cpustr))
+					 kwlist, &cpustr)) {
+		PyErr_BadArgument();
 		return -1;
+	}
 
 	pcpus->cpus = perf_cpu_map__new(cpustr);
-	if (pcpus->cpus == NULL)
+	if (pcpus->cpus == NULL) {
+		PyErr_SetFromErrno(PyExc_OSError);
 		return -1;
+	}
 	return 0;
 }
 
@@ -704,12 +708,16 @@ static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads,
 	int pid = -1, tid = -1, uid = UINT_MAX;
 
 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iii",
-					 kwlist, &pid, &tid, &uid))
+					 kwlist, &pid, &tid, &uid)) {
+		PyErr_BadArgument();
 		return -1;
+	}
 
 	pthreads->threads = thread_map__new(pid, tid, uid);
-	if (pthreads->threads == NULL)
+	if (pthreads->threads == NULL) {
+		PyErr_SetFromErrno(PyExc_OSError);
 		return -1;
+	}
 	return 0;
 }
 
@@ -839,13 +847,18 @@ static int pyrf_evsel__init(struct pyrf_evsel *pevsel,
 					 &enable_on_exec, &task, &watermark,
 					 &precise_ip, &mmap_data, &sample_id_all,
 					 &attr.wakeup_events, &attr.bp_type,
-					 &attr.bp_addr, &attr.bp_len, &idx))
+					 &attr.bp_addr, &attr.bp_len, &idx)) {
+		PyErr_BadArgument();
 		return -1;
+	}
 
 	/* union... */
 	if (sample_period != 0) {
-		if (attr.sample_freq != 0)
-			return -1; /* FIXME: throw right exception */
+		if (attr.sample_freq != 0) {
+			PyErr_SetString(PyExc_ValueError,
+					"perf: sample_freq and sample_period are mutually exclusive");
+			return -1;
+		}
 		attr.sample_period = sample_period;
 	}
 
@@ -892,8 +905,10 @@ static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel,
 	static char *kwlist[] = { "cpus", "threads", "group", "inherit", NULL };
 
 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist,
-					 &pcpus, &pthreads, &group, &inherit))
+					 &pcpus, &pthreads, &group, &inherit)) {
+		PyErr_BadArgument();
 		return NULL;
+	}
 
 	if (pthreads != NULL)
 		threads = ((struct pyrf_thread_map *)pthreads)->threads;
@@ -957,8 +972,10 @@ static int pyrf_evlist__init(struct pyrf_evlist *pevlist,
 	struct perf_cpu_map *cpus;
 	struct perf_thread_map *threads;
 
-	if (!PyArg_ParseTuple(args, "OO", &pcpus, &pthreads))
+	if (!PyArg_ParseTuple(args, "OO", &pcpus, &pthreads)) {
+		PyErr_BadArgument();
 		return -1;
+	}
 
 	threads = ((struct pyrf_thread_map *)pthreads)->threads;
 	cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
@@ -980,8 +997,10 @@ static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist,
 	int pages = 128, overwrite = false;
 
 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist,
-					 &pages, &overwrite))
+					 &pages, &overwrite)) {
+		PyErr_BadArgument();
 		return NULL;
+	}
 
 	if (evlist__mmap(evlist, pages) < 0) {
 		PyErr_SetFromErrno(PyExc_OSError);
@@ -999,8 +1018,10 @@ static PyObject *pyrf_evlist__poll(struct pyrf_evlist *pevlist,
 	static char *kwlist[] = { "timeout", NULL };
 	int timeout = -1, n;
 
-	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &timeout))
+	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &timeout)) {
+		PyErr_BadArgument();
 		return NULL;
+	}
 
 	n = evlist__poll(evlist, timeout);
 	if (n < 0) {
@@ -1057,8 +1078,10 @@ static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist,
 	PyObject *pevsel;
 	struct evsel *evsel;
 
-	if (!PyArg_ParseTuple(args, "O", &pevsel))
+	if (!PyArg_ParseTuple(args, "O", &pevsel)) {
+		PyErr_BadArgument();
 		return NULL;
+	}
 
 	Py_INCREF(pevsel);
 	evsel = &((struct pyrf_evsel *)pevsel)->evsel;
@@ -1093,12 +1116,16 @@ static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,
 	int err;
 
 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist,
-					 &cpu, &sample_id_all))
+					 &cpu, &sample_id_all)) {
+		PyErr_BadArgument();
 		return NULL;
+	}
 
 	md = get_md(evlist, cpu);
-	if (!md)
+	if (!md) {
+		PyErr_SetFromErrno(PyExc_OSError);
 		return NULL;
+	}
 
 	if (perf_mmap__read_init(&md->core) < 0)
 		goto end;
@@ -1324,6 +1351,8 @@ static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel,
 				  PyObject *args, PyObject *kwargs)
 {
 #ifndef HAVE_LIBTRACEEVENT
+	PyErr_SetString(PyExc_OSError,
+			"perf: tracepoint support not compiled in");
 	return NULL;
 #else
 	struct tep_event *tp_format;
@@ -1332,8 +1361,10 @@ static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel,
 	char *name = NULL;
 
 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss", kwlist,
-					 &sys, &name))
+					 &sys, &name)) {
+		PyErr_BadArgument();
 		return NULL;
+	}
 
 	tp_format = trace_event__tp_format(sys, name);
 	if (IS_ERR(tp_format))
-- 
2.39.2


             reply	other threads:[~2023-05-02 21:34 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-02 20:31 Jinli Xiao [this message]
2023-05-02 20:37 ` [PATCH] perf python: Set error messages on call failure Ian Rogers
2023-05-03  2:27   ` Jinli Xiao
2023-05-03 17:16     ` Ian Rogers

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=20230502203135.24794-1-jinli.xiao@nyu.edu \
    --to=jinli.xiao@nyu.edu \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=irogers@google.com \
    --cc=james.clark@arm.com \
    --cc=jolsa@kernel.org \
    --cc=leo.yan@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=suzuki.poulose@arm.com \
    /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).