Skip to content

Commit eda3913

Browse files
dsahernIngo Molnar
authored andcommitted
perf tools: Fix endian conversion reading event attr from file header
The perf_event_attr struct has two __u32's at the top and they need to be swapped individually. With this change I was able to analyze a perf.data collected in a 32-bit PPC VM on an x86 system. I tested both 32-bit and 64-bit binaries for the Intel analysis side; both read the PPC perf.data file correctly. -v2: - changed the existing perf_event__attr_swap() to swap only elements of perf_event_attr and exported it for use in swapping the attributes in the file header - updated swap_ops used for processing events Signed-off-by: David Ahern <dsahern@gmail.com> Acked-by: Frederic Weisbecker <fweisbec@gmail.com> Cc: acme@ghostprotocols.net Cc: peterz@infradead.org Cc: paulus@samba.org Cc: <stable@kernel.org> Link: http://lkml.kernel.org/r/1310754849-12474-1-git-send-email-dsahern@gmail.com Signed-off-by: Ingo Molnar <mingo@elte.hu>
1 parent 0111919 commit eda3913

3 files changed

Lines changed: 23 additions & 13 deletions

File tree

‎tools/perf/util/header.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,9 +877,12 @@ int perf_session__read_header(struct perf_session *session, int fd)
877877
struct perf_evsel *evsel;
878878
off_t tmp;
879879

880-
if (perf_header__getbuffer64(header, fd, &f_attr, sizeof(f_attr)))
880+
if (readn(fd, &f_attr, sizeof(f_attr)) <= 0)
881881
goto out_errno;
882882

883+
if (header->needs_swap)
884+
perf_event__attr_swap(&f_attr.attr);
885+
883886
tmp = lseek(fd, 0, SEEK_CUR);
884887
evsel = perf_evsel__new(&f_attr.attr, i);
885888

‎tools/perf/util/session.c‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -413,20 +413,26 @@ static void perf_event__read_swap(union perf_event *event)
413413
event->read.id = bswap_64(event->read.id);
414414
}
415415

416-
static void perf_event__attr_swap(union perf_event *event)
416+
/* exported for swapping attributes in file header */
417+
void perf_event__attr_swap(struct perf_event_attr *attr)
418+
{
419+
attr->type = bswap_32(attr->type);
420+
attr->size = bswap_32(attr->size);
421+
attr->config = bswap_64(attr->config);
422+
attr->sample_period = bswap_64(attr->sample_period);
423+
attr->sample_type = bswap_64(attr->sample_type);
424+
attr->read_format = bswap_64(attr->read_format);
425+
attr->wakeup_events = bswap_32(attr->wakeup_events);
426+
attr->bp_type = bswap_32(attr->bp_type);
427+
attr->bp_addr = bswap_64(attr->bp_addr);
428+
attr->bp_len = bswap_64(attr->bp_len);
429+
}
430+
431+
static void perf_event__hdr_attr_swap(union perf_event *event)
417432
{
418433
size_t size;
419434

420-
event->attr.attr.type = bswap_32(event->attr.attr.type);
421-
event->attr.attr.size = bswap_32(event->attr.attr.size);
422-
event->attr.attr.config = bswap_64(event->attr.attr.config);
423-
event->attr.attr.sample_period = bswap_64(event->attr.attr.sample_period);
424-
event->attr.attr.sample_type = bswap_64(event->attr.attr.sample_type);
425-
event->attr.attr.read_format = bswap_64(event->attr.attr.read_format);
426-
event->attr.attr.wakeup_events = bswap_32(event->attr.attr.wakeup_events);
427-
event->attr.attr.bp_type = bswap_32(event->attr.attr.bp_type);
428-
event->attr.attr.bp_addr = bswap_64(event->attr.attr.bp_addr);
429-
event->attr.attr.bp_len = bswap_64(event->attr.attr.bp_len);
435+
perf_event__attr_swap(&event->attr.attr);
430436

431437
size = event->header.size;
432438
size -= (void *)&event->attr.id - (void *)event;
@@ -454,7 +460,7 @@ static perf_event__swap_op perf_event__swap_ops[] = {
454460
[PERF_RECORD_LOST] = perf_event__all64_swap,
455461
[PERF_RECORD_READ] = perf_event__read_swap,
456462
[PERF_RECORD_SAMPLE] = perf_event__all64_swap,
457-
[PERF_RECORD_HEADER_ATTR] = perf_event__attr_swap,
463+
[PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap,
458464
[PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
459465
[PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
460466
[PERF_RECORD_HEADER_BUILD_ID] = NULL,

‎tools/perf/util/session.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ int perf_session__set_kallsyms_ref_reloc_sym(struct map **maps,
112112
u64 addr);
113113

114114
void mem_bswap_64(void *src, int byte_size);
115+
void perf_event__attr_swap(struct perf_event_attr *attr);
115116

116117
int perf_session__create_kernel_maps(struct perf_session *self);
117118

0 commit comments

Comments
 (0)