-
Notifications
You must be signed in to change notification settings - Fork 0
/
mininit.c
287 lines (243 loc) · 5.74 KB
/
mininit.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sched.h>
#if 0
/* When things stabilize and we can move it to the sysroot */
#include <linux/owl.h>
#else
#define __user /* This is stripped from uapi headers by linux */
#include "owl.h"
#endif
#include "owl-user.h"
#define max(x,y) (x > y ? x : y)
int _close(int fd);
int _open(char *path, int flags);
//int ioctl(int fd, unsigned long request, void *ptr);
enum command {
CMD_START,
CMD_STOP,
CMD_DUMP,
CMD_INVALID,
CMD_NR_COMMANDS = CMD_INVALID,
};
struct options {
enum command cmd;
char *path;
bool ignore_illegal_insn;
};
#define INIT_OPTIONS { \
.cmd = CMD_NR_COMMANDS, \
.path = "/dev/tracectrl0", \
.ignore_illegal_insn = true } /* TODO: This should be configurable */
struct state {
int fd;
char *path;
};
#define INIT_STATE { .fd = -1, }
const char *str_to_cmd[CMD_NR_COMMANDS + 1] = {
[CMD_START] = "start",
[CMD_STOP] = "stop",
[CMD_DUMP] = "dump",
[CMD_NR_COMMANDS] = NULL,
};
#if 0
/* Return 0 on success */
static int
parse_cmdline(int argc, char *argv[], struct options *options)
{
enum command i;
if (argc < 3)
return 1;
if (strcmp("trace", argv[1]))
return 1;
for (i = 0; str_to_cmd[i]; i++) {
if (!strcmp(str_to_cmd[i], argv[2]))
break;
}
options->cmd = i;
return options->cmd >= CMD_NR_COMMANDS;
}
#endif
static int
do_config(struct options *options, struct state *state)
{
struct owl_config config = { 0 };
config.ignore_illegal_insn = options->ignore_illegal_insn;
return ioctl(state->fd, OWL_IOCTL_CONFIG, &config);
}
static int
do_start(struct options *options, struct state *state)
{
int err;
err = do_config(options, state);
if (err)
return err;
return ioctl(state->fd, OWL_IOCTL_ENABLE);
}
static int
do_stop(struct options *options, struct state *state)
{
(void)options;
return ioctl(state->fd, OWL_IOCTL_DISABLE);
}
static int
do_dump(struct options *options, struct state *state)
{
(void)options;
(void)state;
#if 0
int ret = 0;
uint64_t offs;
struct owl_status status = { 0 };
struct owl_trace_header header = { 0 };
struct owl_trace_file_header file_header = { 0 };
ret = ioctl(state->fd, OWL_IOCTL_STATUS, &status);
if (ret) {
perror("status");
return ret;
}
header.max_tracebuf_size = status.tracebuf_size;
header.max_sched_info_size = status.sched_info_size;
header.max_map_info_size = status.map_info_size;
header.max_stream_info_size = status.stream_info_size;
header.streaminfobuf = calloc(1, header.max_stream_info_size);
if (!header.streaminfobuf) {
perror("calloc");
ret = 1;
goto out;
}
header.tracebuf = calloc(1, header.max_tracebuf_size);
if (!header.tracebuf) {
perror("calloc");
ret = 2;
goto free_streaminfobuf;
}
header.schedinfobuf = calloc(1, header.max_sched_info_size);
if (!header.schedinfobuf) {
perror("calloc");
ret = 3;
goto free_tracebuf;
}
header.mapinfobuf = calloc(1, header.max_map_info_size);
if (!header.mapinfobuf) {
perror("calloc");
ret = 4;
goto free_schedinfobuf;
}
ret = ioctl(state->fd, OWL_IOCTL_DUMP, &header);
if (ret) {
perror("dump");
goto free_mapinfobuf;
}
file_header.magic = OWL_TRACE_FILE_HEADER_MAGIC;
file_header.trace_format = header.trace_format;
file_header.num_cpus = max(1,
header.stream_info_size /
sizeof(struct owl_stream_info));
file_header.stream_info_size = header.stream_info_size;
file_header.tracebuf_size = header.tracebuf_size;
file_header.sched_info_size = header.sched_info_size;
file_header.map_info_size = header.map_info_size;
offs = 0;
file_header.stream_info_offs = offs;
offs += header.stream_info_size;
file_header.tracebuf_offs = offs;
offs += header.tracebuf_size;
file_header.sched_info_offs = offs;
offs += header.sched_info_size;
file_header.map_info_offs = offs;
file_header.sentinel = OWL_TRACE_FILE_HEADER_SENTINEL;
fwrite(&file_header, sizeof(file_header), 1, stdout);
fwrite(header.streaminfobuf, header.stream_info_size, 1, stdout);
fwrite(header.tracebuf, header.tracebuf_size, 1, stdout);
fwrite(header.schedinfobuf, header.sched_info_size, 1, stdout);
fwrite(header.mapinfobuf, header.map_info_size, 1, stdout);
free_mapinfobuf:
free(header.mapinfobuf);
free_schedinfobuf:
free(header.schedinfobuf);
free_tracebuf:
free(header.tracebuf);
free_streaminfobuf:
free(header.streaminfobuf);
out:
return ret;
#endif
return 0;
}
static int
(* const dispatch[CMD_NR_COMMANDS]) (struct options *, struct state *) = {
[CMD_START] = do_start,
[CMD_STOP] = do_stop,
[CMD_DUMP] = do_dump,
};
static int
init(struct options *options, struct state *state)
{
int ret;
ret = open(options->path, O_RDWR);
if (ret < 0) {
printf("open error %d\n", ret);
//perror(options->path);
} else {
state->fd = ret;
ret = 0;
}
return ret;
}
static int
fini(struct options *options, struct state *state)
{
(void)options;
if (state->fd > -1) {
close(state->fd);
state->fd = -1;
}
return 0;
}
void usage(const char *argv0)
{
(void)argv0;
//fprintf(stderr, "usage: %s trace [start|stop|dump]\n", argv0);
}
int main(int argc, char *argv[])
{
(void)argc;
(void)argv;
int ret;
struct options options = INIT_OPTIONS;
struct state state = INIT_STATE;
options.cmd = CMD_START;
printf("Enable tracing\n");
#if 0
if (parse_cmdline(argc, argv, &options)) {
usage(argv[0]);
exit(EXIT_FAILURE);
}
#endif
ret = init(&options, &state);
if (ret) {
printf("Fail init\n");
return ret;
}
ret = dispatch[options.cmd](&options, &state);
if (ret) {
printf("Fail ioctl? %d\n", ret);
usage(argv[0]);
}
printf("Tracing enabled entering mainloop\n");
while (true) {
;
}
fini(&options, &state);
return ret;
}