-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
346 lines (270 loc) · 7.47 KB
/
main.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include "ogles2_module.h"
#include "warp3dnova_module.h"
#include "logger.h"
#include "gui.h"
#include "filter.h"
#include "timer.h"
#include "version.h"
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/icon.h>
#include <stdio.h>
#include <string.H>
#include <stdlib.h>
struct Params {
LONG ogles2;
LONG nova;
LONG gui;
LONG profiling;
LONG *startTime;
LONG *duration;
char *filter;
};
static const char* const version __attribute__((used)) = "$VER: " VERSION_STRING DATE_STRING "\0";
static const char* const portName = "glSnoop port";
static char* filterFile;
static struct Params params = { 0, 0, 0, 0, NULL, NULL, NULL };
static struct MsgPort* port;
struct Task* mainTask;
BYTE mainSig = -1;
static ULONG startTime;
static ULONG duration;
static BOOL running = TRUE;
static BOOL already_running(void)
{
IExec->Forbid();
struct MsgPort* p = IExec->FindPort(portName);
IExec->Permit();
return p != NULL;
}
static void create_port()
{
port = IExec->AllocSysObjectTags(ASOT_PORT,
ASOPORT_Name, portName,
ASOPORT_Pri, 1,
TAG_DONE);
if (!port) {
puts("Failed to create port");
// Not a big deal, port is only used to guard against multiple glSnoop instances
}
}
static void remove_port()
{
if (port) {
IExec->FreeSysObject(ASOT_PORT, port);
port = NULL;
}
}
static void sanitiseParams(void)
{
if (params.gui) {
// Some of these limitations may be lifted in the future
if (startTime) {
puts("Using STARTTIME with GUI is not possible yet");
startTime = 0;
}
if (duration) {
puts("Using DURATION with GUI is not possible yet");
duration = 0;
}
}
if (!params.profiling) {
if (startTime) {
puts("STARTTIME is meant to be used with PROFILE");
startTime = 0;
}
if (duration) {
puts("DURATION is meant to be used with PROFILE");
duration = 0;
}
}
if (!params.ogles2 && !params.nova) {
// If user gives nothing, assume everything
params.ogles2 = params.nova = TRUE;
}
}
static BOOL parse_args(void)
{
const char* const enabled = "enabled";
const char* const disabled = "disabled";
const char* const pattern = "OGLES2/S,NOVA/S,GUI/S,PROFILE/S,STARTTIME/N,DURATION/N,FILTER/K";
// how-to handle both tooltypes and args?
struct RDArgs *result = IDOS->ReadArgs(pattern, (int32 *)¶ms, NULL);
if (result) {
if (params.filter) {
filterFile = strdup(params.filter);
}
if (params.startTime) {
startTime = (ULONG)*params.startTime;
}
if (params.duration) {
duration = (ULONG)*params.duration;
}
IDOS->FreeArgs(result);
} else {
printf("Error when reading command-line arguments. Known parameters are: %s\n", pattern);
return FALSE;
}
sanitiseParams();
puts("--- Configuration ---");
printf(" OGLES2 module: [%s]\n", params.ogles2 ? enabled : disabled);
printf(" WARP3DNOVA module: [%s]\n", params.nova ? enabled : disabled);
printf(" GUI: [%s]\n", params.gui ? enabled : disabled);
printf(" Tracing mode: [%s]\n", params.profiling ? disabled : enabled);
printf(" Filter file name: [%s]\n", filterFile ? filterFile : disabled);
printf(" Start time: [%lu] seconds %s\n", startTime, !startTime ? "- immediate" : "");
printf(" Duration: [%lu] seconds %s\n", duration, !duration ? "- unlimited" : "");
puts("---------------------");
return TRUE;
}
static void install_patches(void)
{
if (params.ogles2) {
ogles2_install_patches();
}
if (params.nova) {
warp3dnova_install_patches(startTime, duration);
}
}
static void patch_cooldown(void)
{
const ULONG seconds = 1;
logLine("Wait %lu s before quit...", seconds);
// It's possible that some patched application is still running inside glSnoop wrappers.
// Give it time to exit before process memory goes out.
timer_delay(seconds);
logLine("...waiting over");
}
static void remove_patches(void)
{
warp3dnova_remove_patches();
ogles2_remove_patches();
patch_cooldown();
// It may be useful to see the cleanup in serial
resume_log();
warp3dnova_free();
ogles2_free();
}
// When STARTTIME > 0
static void waitForStartTimer()
{
const uint32 timerSig = timer_signal(&triggerTimer);
const ESignalType type = timer_wait_for_signal(timerSig, "Start");
if (type == ESignalType_Timer) {
puts("Timer signal - start profiling");
timer_handle_events(&triggerTimer);
ogles2_start_profiling();
warp3dnova_start_profiling();
} else if (type == ESignalType_Break) {
running = FALSE;
}
}
static void waitForEndTimer()
{
if (running) {
timer_start(&triggerTimer, duration, 0);
const uint32 timerSig = timer_signal(&triggerTimer);
puts("Waiting for timer...");
if (timer_wait_for_signal(timerSig, "Stop") == ESignalType_Timer) {
puts("Timer signal - stop profiling");
timer_handle_events(&triggerTimer);
}
}
}
static void waitForSignal()
{
if (running) {
const uint32 sigMask = 1L << mainSig;
const uint32 wait = IExec->Wait(sigMask | SIGBREAKF_CTRL_C);
if (wait & sigMask) {
puts("Start signal received");
}
if (wait & SIGBREAKF_CTRL_C) {
puts("Break signal received");
running = FALSE;
}
}
}
static void waitForTimers()
{
if (startTime) {
waitForStartTimer();
}
if (duration) {
if (!startTime) {
// NOVA module Signal()s us
puts("Waiting for signal...");
waitForSignal();
}
waitForEndTimer();
} else {
if (startTime) {
waitForSignal();
}
}
}
static void run(void)
{
if (params.gui) {
run_gui(params.profiling);
} else {
if (startTime || duration) {
waitForTimers();
} else {
waitForSignal();
}
}
}
int main(int argc __attribute__((unused)), char* argv[] __attribute__((unused)))
{
if (!timer_init(&timer)) {
goto out;
}
logLine("*** %s started. Built date: %s ***", VERSION_STRING, __DATE__);
if (!parse_args()) {
goto out;
}
if (already_running()) {
puts("glSnoop is already running");
goto out;
}
mainTask = IExec->FindTask(NULL);
if (startTime || duration) {
if (!timer_init(&triggerTimer)) {
goto out;
}
}
mainSig = IExec->AllocSignal(-1);
if (mainSig == -1) {
puts("Failed to allocate signal");
goto out;
}
create_port();
if (!load_filters(filterFile)) {
goto out;
}
install_patches();
if (params.profiling) {
puts("Profiling mode - disabling most serial logging...");
pause_log();
}
puts("System patched. Press Control-C to quit...");
run();
remove_patches();
puts("Patches removed. glSnoop terminating");
out:
if (mainSig != -1) {
IExec->FreeSignal(mainSig);
mainSig = -1;
}
remove_port();
free_filters();
free(filterFile);
if (startTime || duration) {
timer_stop(&triggerTimer);
timer_quit(&triggerTimer);
}
logLine("glSnoop exiting");
timer_quit(&timer);
return 0;
}