Thread (31 messages) 31 messages, 2 authors, 2024-07-11

Re: [PATCH v3 01/28] perf auxtrace: Remove dummy tools

From: Adrian Hunter <adrian.hunter@intel.com>
Date: 2024-07-11 06:46:47
Also in: linux-perf-users, lkml

On 11/07/24 09:43, Adrian Hunter wrote:
On 29/06/24 07:53, Ian Rogers wrote:
quoted
diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index 0ec92d47373c..44db5b49128f 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -1764,6 +1764,36 @@ int perf_session__deliver_synth_event(struct perf_session *session,
 	return machines__deliver_event(&session->machines, evlist, event, sample, tool, 0, NULL);
 }
 
+int perf_session__deliver_synth_attr_event(struct perf_session *session,
+					   const struct perf_event_attr *attr,
+					   u64 id)
+{
+	union {
+		struct {
+			struct perf_record_header_attr attr;
+			u64 ids[1];
+		} attr_id;
+		union perf_event ev;
+	} ev = {
+		.attr_id = {
+			.attr = {
+				.header = {
+					.type = PERF_RECORD_HEADER_ATTR,
+					.size = PERF_ALIGN(sizeof(struct perf_event_attr),
+							   sizeof(u64))
+					    + sizeof(struct perf_event_header)
+					    + sizeof(u64),
+				},
+			},
+			.ids = { id, }
+		},
Nested definitions are not needed.  It can just be:

		.attr_id.attr.header.type = PERF_RECORD_HEADER_ATTR,
		.attr_id.attr.header.size = sizeof(ev.attr_id),
		.attr_id.ids[0] = id,
quoted
+	};
+
+	memcpy(&ev.attr_id.attr.attr, attr, min((u32)sizeof(struct perf_event_attr), attr->size));
It is not unreasonable to validate attr->size, but neither slicing
nor padding will work.  Better to just return an error:

	if (attr->size != sizeof(ev.attr_id.attr))
Or rather

 	if (attr->size != sizeof(ev.attr_id.attr.attr))
		return -EINVAL;

Then:

	ev.attr_id.attr.attr = *attr;

quoted
+	ev.attr_id.attr.attr.size = sizeof(struct perf_event_attr);
+	return perf_session__deliver_synth_event(session, &ev.ev, NULL);
+}
+
  
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help