Re: trace-cmd: question about explicit host endianness setting

Steven Rostedt <[email protected]> Tue, 23 Dec 2025 15:49:00 -0500
Newsgroups org.kernel.vger.linux-trace-devel
Message-ID <[email protected]>
On Tue, 23 Dec 2025 22:42:21 +0800
Wenjun Huo <[email protected]> wrote:

> Dear Tracers,
> 
> While reading the trace-cmd code, I noticed a detail in
> tracecmd_alloc_fd() regarding endianness handling and wanted to
> double-check my understanding.
> 
> In tracecmd_alloc_fd(), after tep_alloc() and plugin loading,
> trace-cmd explicitly sets both file and local endianness:
> 
> tep_set_file_bigendian(handle->pevent, buf[0]);
> tep_set_local_bigendian(handle->pevent, tracecmd_host_bigendian());
> 
> I understand that setting the file endianness here is required,
> since it is read from the trace file header.
> 
> For the local/host endianness, tep_alloc() already initializes
> host_bigendian via tep_is_bigendian(), and in the common case
> tracecmd_host_bigendian() resolves to the same value. So in practice
> this explicit call usually does not change the value.
> 
> My understanding is that this is intentional, and that the call to
> tep_set_local_bigendian() serves as an explicit binding step, making
> it clear that trace-cmd confirms the local execution endianness
> after parsing the trace header and loading plugins, rather than
> relying on tep_alloc() defaults.
> 
> Could you please confirm if this interpretation is correct, and
> whether this explicit setting is mainly for clarity / defensive API
> usage rather than functional necessity?

Nope, it's simply due to legacy and the evolution of software. Sort of why
we still have an appendix ;-)

If you look at trace-cmd commit cd001f7b40f9d ("Add handler to pevent
functions") [ from 2009! ] it has:

+       handle->pevent = pevent_alloc();
+       if (!handle->pevent)
+               goto failed_read;
+
+       handle->pevent->file_bigendian = buf[0];
+       handle->pevent->host_bigendian = bigendian();
 
Where at that time, pevent_alloc() was simply:

struct tep_handle *pevent_alloc(void)
{
        struct tep_handle *pevent = calloc(1, sizeof(*pevent));

        if (pevent)
                pevent->ref_count = 1;

        return pevent;
}

and trace-cmd had to set the file and host endianess.

But since then, libtraceevent renamed pevent_alloc() to tep_alloc() and
initialized the host_bigendian.

trace-cmd was updated to simply keep the original code but modified to have
the new APIs.

> 
> If maintainers think there is a better way to clarify or improve
> this behavior (for example via comments, documentation, or code
> adjustments), I would be very happy to help by preparing and sending
> a patch based on your guidance.

In reality, the line:

	tep_set_local_bigendian(handle->pevent, tracecmd_host_bigendian());

is obsolete, and probably could just be deleted.

If you want to send that patch feel free, or I can simply remove it.

Thanks for your curiosity of looking at this code ;-)

-- Steve