xref: /aosp_15_r20/external/libtracefs/Documentation/libtracefs-eprobes.txt (revision 287e80b3a36113050663245e7f2c00d274188f18)
1libtracefs(3)
2=============
3
4NAME
5----
6tracefs_eprobe_alloc - Allocate new event probe (eprobe)
7
8SYNOPSIS
9--------
10[verse]
11--
12*#include <tracefs.h>*
13
14struct tracefs_dynevent pass:[*]
15*tracefs_eprobe_alloc*(const char pass:[*]_system_, const char pass:[*]_event_,
16		       const char pass:[*]_target_system_, const char pass:[*]_target_event_,
17		       const char pass:[*]_fetchargs_);
18--
19
20DESCRIPTION
21-----------
22*tracefs_eprobe_alloc*() allocates a new eprobe context. The ebrobe is not configured in the system.
23The new eprobe will be in the _system_ group (or eprobes if _system_ is NULL) and have the name of
24_event_. The eprobe will be attached to _target_event_, located in _target_system_. The list of
25arguments, described in _fetchargs_, will be fetched from _target_event_. The returned pointer to
26the event probe must be freed with *tracefs_dynevent_free*().
27
28
29RETURN VALUE
30------------
31The *tracefs_eprobe_alloc*() API returns a pointer to an allocated tracefs_dynevent structure,
32describing the event probe. This pointer must be freed by *tracefs_dynevent_free*(3). Note, this
33only allocates a descriptor representing the eprobe. It does not modify the running system. On error
34NULL is returned.
35
36EXAMPLE
37-------
38[source,c]
39--
40#include <stdlib.h>
41#include <unistd.h>
42#include <sys/wait.h>
43
44#include <tracefs.h>
45
46static struct tep_event *open_event;
47static struct tep_format_field *file_field;
48
49static int callback(struct tep_event *event, struct tep_record *record,
50		    int cpu, void *data)
51{
52	struct trace_seq seq;
53
54	trace_seq_init(&seq);
55	tep_print_event(event->tep, &seq, record, "%d-%s: ", TEP_PRINT_PID, TEP_PRINT_COMM);
56
57	if (event->id == open_event->id) {
58		trace_seq_puts(&seq, "open file='");
59		tep_print_field(&seq, record->data, file_field);
60		trace_seq_puts(&seq, "'\n");
61	}
62
63	trace_seq_terminate(&seq);
64	trace_seq_do_printf(&seq);
65	trace_seq_destroy(&seq);
66
67	return 0;
68}
69
70static pid_t run_exec(char **argv, char **env)
71{
72	pid_t pid;
73
74	pid = fork();
75	if (pid)
76		return pid;
77
78	execve(argv[0], argv, env);
79	perror("exec");
80	exit(-1);
81}
82
83const char *myprobe = "my_eprobes";
84
85int main (int argc, char **argv, char **env)
86{
87	struct tracefs_dynevent *eprobe;
88	struct tracefs_instance *instance;
89	struct tep_handle *tep;
90	const char *sysnames[] = { myprobe, NULL };
91	pid_t pid;
92
93	if (argc < 2) {
94		printf("usage: %s command\n", argv[0]);
95		exit(-1);
96	}
97
98	instance = tracefs_instance_create("exec_open");
99	if (!instance) {
100		perror("creating instance");
101		exit(-1);
102	}
103
104	tracefs_dynevent_destroy_all(TRACEFS_DYNEVENT_EPROBE, true);
105
106	eprobe = tracefs_eprobe_alloc(myprobe, "sopen", "syscalls", "sys_enter_openat2",
107				      "file=+0($filename):ustring");
108	if (!eprobe) {
109		perror("allocating event probe");
110		exit(-1);
111	}
112
113	if (tracefs_dynevent_create(eprobe)) {
114		perror("creating event probe");
115		exit(-1);
116	}
117
118	tep = tracefs_local_events_system(NULL, sysnames);
119	if (!tep) {
120		perror("reading events");
121		exit(-1);
122	}
123
124	open_event = tep_find_event_by_name(tep, myprobe, "sopen");
125	file_field = tep_find_field(open_event, "file");
126
127	tracefs_event_enable(instance, myprobe, "sopen");
128	pid = run_exec(&argv[1], env);
129
130	/* Let the child start to run */
131	sched_yield();
132
133	do {
134		tracefs_load_cmdlines(NULL, tep);
135		tracefs_iterate_raw_events(tep, instance, NULL, 0, callback, NULL);
136	} while (waitpid(pid, NULL, WNOHANG) != pid);
137
138	/* Will disable the events */
139	tracefs_dynevent_destroy(eprobe, true);
140	tracefs_dynevent_free(eprobe);
141	tracefs_instance_destroy(instance);
142	tep_free(tep);
143
144	return 0;
145}
146--
147
148FILES
149-----
150[verse]
151--
152*tracefs.h*
153	Header file to include in order to have access to the library APIs.
154*-ltracefs*
155	Linker switch to add when building a program that uses the library.
156--
157
158SEE ALSO
159--------
160*libtracefs*(3),
161*libtraceevent*(3),
162*trace-cmd*(1)
163
164AUTHOR
165------
166[verse]
167--
168*Steven Rostedt* <[email protected]>
169*Tzvetomir Stoyanov* <[email protected]>
170--
171
172REPORTING BUGS
173--------------
174Report bugs to  <[email protected]>
175
176LICENSE
177-------
178libtracefs is Free Software licensed under the GNU LGPL 2.1
179
180RESOURCES
181---------
182https://git.kernel.org/pub/scm/libs/libtrace/libtracefs.git/
183
184COPYING
185-------
186Copyright \(C) 2021 VMware, Inc. Free use of this software is granted under
187the terms of the GNU Public License (GPL).
188