-
Notifications
You must be signed in to change notification settings - Fork 2
/
rt-utils.c
256 lines (219 loc) · 5.54 KB
/
rt-utils.c
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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2020 Red Hat Inc.
*
* This is tailored version of rt-utils.c from rt-tests:
* git://git.kernel.org/pub/scm/utils/rt-tests/rt-tests.git
*
* Copyright (C) 2009 Carsten Emde <carsten.emde@osadl.org>
* Copyright (C) 2010 Clark Williams <williams@redhat.com>
* Copyright (C) 2015 John Kacur <jkacur@redhat.com>
*
* based on functions from cyclictest that has
* (C) 2008-2009 Clark Williams <williams@redhat.com>
* (C) 2005-2007 Thomas Gleixner <tglx@linutronix.de>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sched.h>
#include <stdarg.h>
#include <errno.h>
#include <ctype.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/syscall.h> /* For SYS_gettid definitions */
#include "rt-utils.h"
#include "error.h"
static char debugfileprefix[MAX_PATH];
/*
* Finds the tracing directory in a mounted debugfs
*/
char *get_debugfileprefix(void)
{
char type[100];
FILE *fp;
int size;
int found = 0;
struct stat s;
if (debugfileprefix[0] != '\0')
goto out;
/* look in the "standard" mount point first */
if ((stat("/sys/kernel/debug/tracing", &s) == 0) && S_ISDIR(s.st_mode)) {
strcpy(debugfileprefix, "/sys/kernel/debug/tracing/");
goto out;
}
/* now look in the "other standard" place */
if ((stat("/debug/tracing", &s) == 0) && S_ISDIR(s.st_mode)) {
strcpy(debugfileprefix, "/debug/tracing/");
goto out;
}
/* oh well, parse /proc/mounts and see if it's there */
if ((fp = fopen("/proc/mounts", "r")) == NULL)
goto out;
while (fscanf(fp, "%*s %"
STR(MAX_PATH)
"s %99s %*s %*d %*d\n",
debugfileprefix, type) == 2) {
if (strcmp(type, "debugfs") == 0) {
found = 1;
break;
}
/* stupid check for systemd-style autofs mount */
if ((strcmp(debugfileprefix, "/sys/kernel/debug") == 0) &&
(strcmp(type, "systemd") == 0)) {
found = 1;
break;
}
}
fclose(fp);
if (!found) {
debugfileprefix[0] = '\0';
goto out;
}
size = sizeof(debugfileprefix) - strlen(debugfileprefix);
strncat(debugfileprefix, "/tracing/", size);
out:
return debugfileprefix;
}
int mount_debugfs(char *path)
{
char *mountpoint = path;
char cmd[MAX_PATH];
char *prefix;
int ret;
/* if it's already mounted just return */
prefix = get_debugfileprefix();
if (strlen(prefix) != 0) {
info("debugfs mountpoint: %s\n", prefix);
return 0;
}
if (!mountpoint)
mountpoint = "/sys/kernel/debug";
sprintf(cmd, "mount -t debugfs debugfs %s", mountpoint);
ret = system(cmd);
if (ret != 0) {
fprintf(stderr, "Error mounting debugfs at %s: %s\n",
mountpoint, strerror(errno));
return -1;
}
return 0;
}
static char **tracer_list;
static char *tracer_buffer;
static int num_tracers;
#define CHUNKSZ 1024
/*
* return a list of the tracers configured into the running kernel
*/
int get_tracers(char ***list)
{
int ret;
FILE *fp;
char buffer[CHUNKSZ];
char *prefix = get_debugfileprefix();
char *tmpbuf = NULL;
char *ptr;
int tmpsz = 0;
/* if we've already parse it, return what we have */
if (tracer_list) {
*list = tracer_list;
return num_tracers;
}
/* open the tracing file available_tracers */
sprintf(buffer, "%savailable_tracers", prefix);
if ((fp = fopen(buffer, "r")) == NULL)
fatal("Can't open %s for reading\n", buffer);
/* allocate initial buffer */
ptr = tmpbuf = malloc(CHUNKSZ);
if (ptr == NULL)
fatal("error allocating initial space for tracer list\n");
/* read in the list of available tracers */
while ((ret = fread(buffer, sizeof(char), CHUNKSZ, fp))) {
if ((ptr+ret+1) > (tmpbuf+tmpsz)) {
tmpbuf = realloc(tmpbuf, tmpsz + CHUNKSZ);
if (tmpbuf == NULL)
fatal("error allocating space for list of valid tracers\n");
tmpsz += CHUNKSZ;
}
strncpy(ptr, buffer, ret);
ptr += ret;
}
fclose(fp);
if (tmpsz == 0)
fatal("error reading available tracers\n");
tracer_buffer = tmpbuf;
/* get a buffer for the pointers to tracers */
if (!(tracer_list = malloc(sizeof(char *))))
fatal("error allocatinging tracer list buffer\n");
/* parse the buffer */
ptr = strtok(tmpbuf, " \t\n\r");
do {
tracer_list[num_tracers++] = ptr;
tracer_list = realloc(tracer_list, sizeof(char*)*(num_tracers+1));
tracer_list[num_tracers] = NULL;
} while ((ptr = strtok(NULL, " \t\n\r")) != NULL);
/* return the list and number of tracers */
*list = tracer_list;
return num_tracers;
}
/*
* return zero if tracername is not a valid tracer, non-zero if it is
*/
int valid_tracer(char *tracername)
{
char **list;
int ntracers;
int i;
ntracers = get_tracers(&list);
if (ntracers == 0 || tracername == NULL)
return 0;
for (i = 0; i < ntracers; i++)
if (strncmp(list[i], tracername, strlen(list[i])) == 0)
return 1;
return 0;
}
/*
* enable event tracepoint
*/
int setevent(char *event, char *val)
{
char *prefix = get_debugfileprefix();
char buffer[MAX_PATH];
int fd;
int ret;
sprintf(buffer, "%s%s", prefix, event);
if ((fd = open(buffer, O_WRONLY)) < 0) {
warn("unable to open %s\n", buffer);
return -1;
}
if ((ret = write(fd, val, strlen(val))) < 0) {
warn("unable to write %s to %s\n", val, buffer);
close(fd);
return -1;
}
close(fd);
return 0;
}
int event_enable_all(void)
{
return setevent("events/enable", "1");
}
int event_disable_all(void)
{
return setevent("events/enable", "0");
}
int event_enable(char *event)
{
char path[MAX_PATH];
sprintf(path, "events/%s/enable", event);
return setevent(path, "1");
}
int event_disable(char *event)
{
char path[MAX_PATH];
sprintf(path, "events/%s/enable", event);
return setevent(path, "0");
}