summaryrefslogtreecommitdiffstats
path: root/src/tracefs-kprobes.c
blob: a8c016376118d51b3e46658e00cd0b8fa52ba8bc (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
// SPDX-License-Identifier: LGPL-2.1
/*
 * Copyright (C) 2021 VMware Inc, Steven Rostedt <rostedt@goodmis.org>
 *
 * Updates:
 * Copyright (C) 2021, VMware, Tzvetomir Stoyanov <tz.stoyanov@gmail.com>
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>

#include "tracefs.h"
#include "tracefs-local.h"

#define KPROBE_EVENTS "kprobe_events"
#define KPROBE_DEFAULT_GROUP "kprobes"

static struct tracefs_dynevent *
kprobe_alloc(enum tracefs_dynevent_type type, const char *system, const char *event,
	     const char *addr, const char *format)
{
	struct tracefs_dynevent *kp;
	const char *sys = system;
	const char *ename = event;
	char *tmp;

	if (!addr) {
		errno = EBADMSG;
		return NULL;
	}
	if (!sys)
		sys = KPROBE_DEFAULT_GROUP;

	if (!event) {
		ename = strdup(addr);
		if (!ename)
			return NULL;
		tmp = strchr(ename, ':');
		if (tmp)
			*tmp = '\0';
	}

	kp = dynevent_alloc(type, sys, ename, addr, format);
	if (!event)
		free((char *)ename);

	return kp;
}

/**
 * tracefs_kprobe_alloc - Allocate new kprobe
 * @system: The system name (NULL for the default kprobes)
 * @event: The event to create (NULL to use @addr for the event)
 * @addr: The function and offset (or address) to insert the probe
 * @format: The format string to define the probe.
 *
 * Allocate a kprobe context that will be in the @system group (or kprobes if
 * @system is NULL). Have the name of @event (or @addr if @event is NULL). 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
 *
 * The kprobe is not created in the system.
 *
 * Return a pointer to a kprobe context on success, or NULL on error.
 * The returned pointer must be freed with tracefs_dynevent_free()
 *
 * errno will be set to EBADMSG if addr is NULL.
 */
struct tracefs_dynevent *
tracefs_kprobe_alloc(const char *system, const char *event, const char *addr, const char *format)

{
	return kprobe_alloc(TRACEFS_DYNEVENT_KPROBE, system, event, addr, format);
}

/**
 * tracefs_kretprobe_alloc - Allocate new kretprobe
 * @system: The system name (NULL for the default kprobes)
 * @event: The event to create (NULL to use @addr for the event)
 * @addr: The function and offset (or address) to insert the retprobe
 * @format: The format string to define the retprobe.
 * @max: Maximum number of instances of the specified function that
 *	 can be probed simultaneously, or 0 for the default value.
 *
 * Allocate a kretprobe that will be in the @system group (or kprobes if
 * @system is NULL). Have the name of @event (or @addr if @event is
 * NULL). Will be inserted to @addr (function name, with or without
 * offset, or a address). And the @format will define the raw format
 * of the kprobe. See the Linux documentation file under:
 * Documentation/trace/kprobetrace.rst
 * The kretprobe is not created in the system.
 *
 * Return a pointer to a kprobe context on success, or NULL on error.
 * The returned pointer must be freed with tracefs_dynevent_free()
 *
 * errno will be set to EBADMSG if addr is NULL.
 */
struct tracefs_dynevent *
tracefs_kretprobe_alloc(const char *system, const char *event,
			const char *addr, const char *format, unsigned int max)
{
	struct tracefs_dynevent *kp;
	int ret;

	kp = kprobe_alloc(TRACEFS_DYNEVENT_KRETPROBE, system, event, addr, format);
	if (!kp)
		return NULL;

	if (!max)
		return kp;

	free(kp->prefix);
	kp->prefix = NULL;
	ret = asprintf(&kp->prefix, "r%d:", max);
	if (ret < 0)
		goto error;

	return kp;
error:
	tracefs_dynevent_free(kp);
	return NULL;
}

static int kprobe_raw(enum tracefs_dynevent_type type, const char *system,
		      const char *event, const char *addr, const char *format)
{
	static struct tracefs_dynevent *kp;
	int ret;

	kp = kprobe_alloc(type, system, event, addr, format);
	if (!kp)
		return -1;

	ret = tracefs_dynevent_create(kp);
	tracefs_dynevent_free(kp);

	return ret;
}

/**
 * tracefs_kprobe_raw - Create a kprobe using raw format
 * @system: The system name (NULL for the default kprobes)
 * @event: The event to create (NULL to use @addr for the event)
 * @addr: The function and offset (or address) to insert the probe
 * @format: The raw format string to define the probe.
 *
 * Create a kprobe that will be in the @system group (or kprobes if
 * @system is NULL). Have the name of @event (or @addr if @event is
 * NULL). Will be inserted to @addr (function name, with or without
 * offset, or a address). And the @format will define the raw format
 * of the kprobe. See the Linux documentation file under:
 * Documentation/trace/kprobetrace.rst
 *
 * Return 0 on success, or -1 on error.
 *   If the syntex of @format was incorrect, running
 *   tracefs_error_last(NULL) may show what went wrong.
 *
 * errno will be set to EBADMSG if addr or format is NULL.
 */
int tracefs_kprobe_raw(const char *system, const char *event,
		       const char *addr, const char *format)
{
	return kprobe_raw(TRACEFS_DYNEVENT_KPROBE, system, event, addr, format);
}

/**
 * tracefs_kretprobe_raw - Create a kretprobe using raw format
 * @system: The system name (NULL for the default kprobes)
 * @event: The event to create (NULL to use @addr for the event)
 * @addr: The function and offset (or address) to insert the retprobe
 * @format: The raw format string to define the retprobe.
 *
 * Create a kretprobe that will be in the @system group (or kprobes if
 * @system is NULL). Have the name of @event (or @addr if @event is
 * NULL). Will be inserted to @addr (function name, with or without
 * offset, or a address). And the @format will define the raw format
 * of the kprobe. See the Linux documentation file under:
 * Documentation/trace/kprobetrace.rst
 *
 * Return 0 on success, or -1 on error.
 *   If the syntex of @format was incorrect, running
 *   tracefs_error_last(NULL) may show what went wrong.
 *
 * errno will be set to EBADMSG if addr or format is NULL.
 */
int tracefs_kretprobe_raw(const char *system, const char *event,
			  const char *addr, const char *format)
{
	return kprobe_raw(TRACEFS_DYNEVENT_KRETPROBE, system, event, addr, format);
}