summaryrefslogtreecommitdiffstats
path: root/Documentation/libtracefs-kprobes.txt
blob: 593ef9ee11fba030ecb78161c0601c2ff039046c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
libtracefs(3)
=============

NAME
----
tracefs_kprobe_alloc, tracefs_kretprobe_alloc, tracefs_kprobe_raw, tracefs_kretprobe_raw -
Allocate, get, and create kprobes

SYNOPSIS
--------
[verse]
--
*#include <tracefs.h>*

struct tracefs_dynevent pass:[*]
*tracefs_kprobe_alloc*(const char pass:[*]_system_, const char pass:[*]_event_,
		       const char pass:[*]_addr_, const char pass:[*]_format_);
struct tracefs_dynevent pass:[*]
*tracefs_kretprobe_alloc*(const char pass:[*]_system_, const char pass:[*]_event_,
			  const char pass:[*]_addr_, const char pass:[*]_format_, unsigned int _max_);
int *tracefs_kprobe_raw*(const char pass:[*]_system_, const char pass:[*]_event_,
			 const char pass:[*]_addr_, const char pass:[*]_format_);
int *tracefs_kretprobe_raw*(const char pass:[*]_system_, const char pass:[*]_event_,
			    const char pass:[*]_addr_, const char pass:[*]_format_);
--

DESCRIPTION
-----------
*tracefs_kprobe_alloc*() allocates a new kprobe context. The kbrobe is not configured in the system.
The new kprobe will be in the _system_ group (or kprobes if _system_ is NULL) and have the name of
_event_ (or _addr_ if _event_ is NULL). The kprobe will be inserted to _addr_ (function name, with
or without offset, or a address), and the _format_ will define the format of the kprobe. See the
Linux documentation file under: Documentation/trace/kprobetrace.rst

*tracefs_kretprobe_alloc*() is the same as *tracefs_kprobe_alloc*, but allocates context for
kretprobe. It has one additional parameter, which is optional, _max_ - maxactive count.
See description of kretprobes in the Documentation/trace/kprobetrace.rst file.

*tracefs_kprobe_raw*() will create a kprobe event. If _system_ is NULL, then
the default "kprobes" is used for the group (event system). Otherwise if _system_
is specified then the kprobe will be created under the group by that name. The
_event_ is the name of the kprobe event to create. The _addr_ can be a function,
a function and offset, or a kernel address. This is where the location of the
kprobe will be inserted in the kernel. The _format_ is the kprobe format as
specified as FETCHARGS in the Linux kernel source in the Documentation/trace/kprobetrace.rst
document.

*tracefs_kretprobe_raw*() is the same as *tracefs_kprobe_raw()*, except that it
creates a kretprobe instead of a kprobe. The difference is also described
in the Linux kernel source in the Documentation/trace/kprobetrace.rst file.

RETURN VALUE
------------

*tracefs_kprobe_raw*() and *tracefs_kretprobe_raw*() return 0 on success, or -1 on error.
If a parsing error occurs on *tracefs_kprobe_raw*() or *tracefs_kretprobe_raw*() then
*tracefs_error_last*(3) may be used to retrieve the error message explaining the parsing issue.

The *tracefs_kprobe_alloc*() and *tracefs_kretprobe_alloc*() APIs return a pointer to an allocated
tracefs_dynevent structure, describing the probe. This pointer must be freed by
*tracefs_dynevent_free*(3). Note, this only allocates a descriptor representing the kprobe. It does
not modify the running system.

ERRORS
------
The following errors are for all the above calls:

*EPERM* Not run as root user

*ENODEV* Kprobe events are not configured for the running kernel.

*ENOMEM* Memory allocation error.

*tracefs_kprobe_raw*(), *tracefs_kretprobe_raw*(), *tracefs_kprobe_alloc*(),
and *tracefs_kretprobe_alloc*() can fail with the following errors:

*EBADMSG* if _addr_ is NULL.

*EINVAL*  Most likely a parsing error occurred (use *tracefs_error_last*(3) to possibly
          see what that error was).

Other errors may also happen caused by internal system calls.

EXAMPLE
-------
[source,c]
--
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

#include <tracefs.h>

static struct tep_event *open_event;
static struct tep_format_field *file_field;

static struct tep_event *openret_event;
static struct tep_format_field *ret_field;

static int callback(struct tep_event *event, struct tep_record *record,
		    int cpu, void *data)
{
	struct trace_seq seq;

	trace_seq_init(&seq);
	tep_print_event(event->tep, &seq, record, "%d-%s: ", TEP_PRINT_PID, TEP_PRINT_COMM);

	if (event->id == open_event->id) {
		trace_seq_puts(&seq, "open file='");
		tep_print_field(&seq, record->data, file_field);
		trace_seq_puts(&seq, "'\n");
	} else if (event->id == openret_event->id) {
		unsigned long long ret;
		tep_read_number_field(ret_field, record->data, &ret);
		trace_seq_printf(&seq, "open ret=%lld\n", ret);
	} else {
		goto out;
	}

	trace_seq_terminate(&seq);
	trace_seq_do_printf(&seq);
out:
	trace_seq_destroy(&seq);

	return 0;
}

static pid_t run_exec(char **argv, char **env)
{
	pid_t pid;

	pid = fork();
	if (pid)
		return pid;

	execve(argv[0], argv, env);
	perror("exec");
	exit(-1);
}

const char *mykprobe = "my_kprobes";

enum kprobe_type {
	KPROBE,
	KRETPROBE,
};

static void __kprobe_create(enum kprobe_type type, const char *event,
			    const char *addr, const char *fmt)
{
	char *err;
	int r;

	if (type == KPROBE)
		r = tracefs_kprobe_raw(mykprobe, event, addr, fmt);
	else
		r = tracefs_kretprobe_raw(mykprobe, event, addr, fmt);
	if (r < 0) {
		err = tracefs_error_last(NULL);
		perror("Failed to create kprobe:");
		if (err && strlen(err))
			fprintf(stderr, "%s\n", err);
	}
}

static void kprobe_create(const char *event, const char *addr,
			  const char *fmt)
{
	__kprobe_create(KPROBE, event, addr, fmt);
}

static void kretprobe_create(const char *event, const char *addr,
			     const char *fmt)
{
	__kprobe_create(KRETPROBE, event, addr, fmt);
}

int main (int argc, char **argv, char **env)
{
	struct tracefs_instance *instance;
	struct tep_handle *tep;
	const char *sysnames[] = { mykprobe, NULL };
	pid_t pid;

	if (argc < 2) {
		printf("usage: %s command\n", argv[0]);
		exit(-1);
	}

	instance = tracefs_instance_create("exec_open");
	if (!instance) {
		perror("creating instance");
		exit(-1);
	}

	tracefs_dynevent_destroy_all(TRACEFS_DYNEVENT_KPROBE | TRACEFS_DYNEVENT_KRETPROBE, true);

	kprobe_create("open", "do_sys_openat2",
		      "file=+0($arg2):ustring flags=+0($arg3):x64 mode=+8($arg3):x64\n");

	kretprobe_create("openret", "do_sys_openat2", "ret=%ax");

	tep = tracefs_local_events_system(NULL, sysnames);
	if (!tep) {
		perror("reading events");
		exit(-1);
	}
	open_event = tep_find_event_by_name(tep, mykprobe, "open");
	file_field = tep_find_field(open_event, "file");

	openret_event = tep_find_event_by_name(tep, mykprobe, "openret");
	ret_field = tep_find_field(openret_event, "ret");

	tracefs_event_enable(instance, mykprobe, NULL);
	pid = run_exec(&argv[1], env);

	/* Let the child start to run */
	sched_yield();

	do {
		tracefs_load_cmdlines(NULL, tep);
		tracefs_iterate_raw_events(tep, instance, NULL, 0, callback, NULL);
	} while (waitpid(pid, NULL, WNOHANG) != pid);

	/* Will disable the events */
	tracefs_dynevent_destroy_all(TRACEFS_DYNEVENT_KPROBE | TRACEFS_DYNEVENT_KRETPROBE, true);
	tracefs_instance_destroy(instance);
	tep_free(tep);

	return 0;
}
--

FILES
-----
[verse]
--
*tracefs.h*
	Header file to include in order to have access to the library APIs.
*-ltracefs*
	Linker switch to add when building a program that uses the library.
--

SEE ALSO
--------
*libtracefs*(3),
*libtraceevent*(3),
*trace-cmd*(1)

AUTHOR
------
[verse]
--
*Steven Rostedt* <rostedt@goodmis.org>
*Tzvetomir Stoyanov* <tz.stoyanov@gmail.com>
*sameeruddin shaik* <sameeruddin.shaik8@gmail.com>
--
REPORTING BUGS
--------------
Report bugs to  <linux-trace-devel@vger.kernel.org>

LICENSE
-------
libtracefs is Free Software licensed under the GNU LGPL 2.1

RESOURCES
---------
https://git.kernel.org/pub/scm/libs/libtrace/libtracefs.git/

COPYING
-------
Copyright \(C) 2021 VMware, Inc. Free use of this software is granted under
the terms of the GNU Public License (GPL).