LIBTRACECMD(3)
π LIBTRACECMD(3) β the mapping-host-and-guest-files one β¨
okay so this page lives in the libtracefs manual, section 3, and it's all about LIBTRACECMD(3) β the little family of functions that lets you glue your host trace files to your guest trace files so you can actually analyze them together. bestie, it's giving "detective work but make it C" ππ
πΈ NAME πΈ
tracecmd_map_vcpus, tracecmd_get_cpu_map, tracecmd_map_find_by_host_pid, tracecmd_map_get_host_pid, tracecmd_map_get_guest, tracecmd_map_set_private, tracecmd_map_get_private β mapping host and guest data. literally the whole friend group π«Ά
β¨ SYNOPSIS β¨
#include <trace-cmd.h>
int tracecmd_map_vcpus(struct tracecmd_input **handles, int nr_handles);
struct tracecmd_cpu_map *tracecmd_get_cpu_map(struct tracecmd_input *handle, int cpu);
struct tracecmd_cpu_map *tracecmd_map_find_by_host_pid(struct tracecmd_input *handle,
int host_pid);
int tracecmd_map_get_host_pid(struct tracecmd_cpu_map *map);
struct tracecmd_input *tracecmd_map_get_guest(struct tracecmd_cpu_map *map);
void tracecmd_map_set_private(struct tracecmd_cpu_map *map, void *priv);
void *tracecmd_map_get_private(struct tracecmd_cpu_map *map);
π¦ DESCRIPTION π¦
so this whole API set is here to map host and guest trace files together so you can do more tracing analysis afterwards. it's the matchmaker of trace files, no cap π
tracecmd_map_vcpus() takes an array of handles β each item in that array was made by one of the tracecmd_open(3) functions β plus the number of handles as nr_handles. the first handle in that array is expected to be the descriptor for the host tracing file, and the rest are guest trace files that run on the host, created through the trace-cmd record(1) and trace-cmd agent(1) interactions. it returns the number of guests found in handles that got associated with the host, or negative on error. π
tracecmd_get_cpu_map() returns a descriptor for a given CPU for a handle. if the handle was a guest defined from tracecmd_map_vcpus(), then you get the mapping created by that function that's associated with this particular vCPU (denoted by cpu) from handle. this descriptor can be used by the tracecmd_map_get_guest(), tracecmd_map_set_private() and tracecmd_map_get_private() functions. iconic lil handoff π
tracecmd_map_find_by_host_pid() returns a mapping for a guest virtual CPU that is handled by the given host_pid. note β bestie, this part matters β the handle you pass in can be either the host handle or one of the guest's handles for that host that was mapped by tracecmd_map_vcpus(), even if the guest handle doesn't have the vCPU that the host_pid represents. yeah. it's flexible like that π
tracecmd_map_get_host_pid() returns the host_pid for a given map that was retrieved by one of the above functions.
tracecmd_map_get_guest() returns the guest_handle for a given map that was retrieved by one of the above functions.
tracecmd_map_set_private() lets the app assign private data for a given guest vCPU β host thread mapping defined by map. your lil stash spot π
tracecmd_map_get_private() retrieves the priv data from map that was set by tracecmd_map_set_private().
π₯ RETURN VALUE π₯
tracecmd_map_vcpus()β returns the number of guests in the handles array that were mapped to the host handle (which is the first entry in handles). returns-1on error.tracecmd_get_cpu_map()β returns a map created bytracecmd_map_vcpus()for a given cpu for a given handle, orNULLif it's not found.tracecmd_map_find_by_host_pid()β returns a map associated with the host task that has host_pid as its process ID. handle can be either the host handle, or one of the guest handles that were mapped to the host viatracecmd_map_vcpus()β even if the guest handle is a different guest than the one the mapping is for. returnsNULLif not found.tracecmd_map_get_host_pid()β returns the host process ID for an associated mapping defined by map.tracecmd_map_get_guest()β returns the guest handle for an associated mapping defined by map.tracecmd_map_get_private()β returns the private data of a mapping defined by map that was set bytracecmd_map_set_private().
π EXAMPLE π
here's the whole vibe in one program, copy-paste ready π
#include <stdlib.h>
#include <errno.h>
#include <trace-cmd.h>
int main(int argc, char **argv)
{
struct tracecmd_input **handles = NULL;
int nr_handles;
int i;
if (argc < 2) {
printf("usage: host_trace.dat guest1_trace.dat [guest2_trace.dat ...]\n");
exit(-1);
}
for (i = 1; i < argc; i++) {
handles = realloc(handles, sizeof(*handles) * (nr_handles + 1));
if (!handles)
exit(-1);
handles[nr_handles] = tracecmd_open(argv[i], 0);
if (!handles[nr_handles]) {
perror(argv[1]);
exit(-1);
}
tracecmd_set_private(handles[nr_handles], argv[i]);
nr_handles++;
}
tracecmd_map_vcpus(handles, nr_handles);
for (i = 1; i < nr_handles; i++) {
struct tracecmd_cpu_map *map;
struct tep_handle *tep;
const char *file = tracecmd_get_private(handles[i]);
int cpus, cpu;
printf("Mappings for guest %s:\n", file);
tep = tracecmd_get_tep(handles[i]);
cpus = tep_get_cpus(tep);
for (cpu = 0; cpu < cpus; cpu++) {
printf(" [%03d] ", cpu);
map = tracecmd_get_cpu_map(handles[i], cpu);
if (!map) {
printf("Has no mapping!\n");
continue;
}
printf("host_pid: %d\n", tracecmd_map_get_host_pid(map));
}
}
for (i = 0; i < nr_handles; i++)
tracecmd_close(handles[i]);
free(handles);
exit(0);
}
π FILES π
trace-cmd.hβ header file to include so you have access to the library APIs.-ltracecmdβ linker switch to add when building a program that uses the library.
π SEE ALSO π
libtracefs(3), libtraceevent(3), trace-cmd(1) trace-cmd.dat(5)
π REPORTING BUGS π
report bugs to <linux-trace-devel@vger.kernel.org[1]>
πΈ LICENSE πΈ
libtracecmd is Free Software licensed under the GNU LGPL 2.1
β RESOURCES β
https://git.kernel.org/pub/scm/utils/trace-cmd/trace-cmd.git/
π COPYING π
Copyright (C) 2020 VMware, Inc. Free use of this software is granted under the terms of the GNU Public License (GPL).
π NOTES π
linux-trace-devel@vger.kernel.orgmailto:linux-trace-devel@vger.kernel.org
ok that's the whole page, bestie β go map some vCPUs, you're gonna slay π β¨
mwah π β this one shipped from the libtracefs manual, dated 03/09/2026, and it answers to the name LIBTRACECMD(3). bye bye, ily π«Ά
π¦ where this page comes from
- arch rolling: trace-cmd 3.4-1 Β· GPL-2.0-only AND LGPL-2.1-only
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms