Skip to content

Commit

Permalink
Add possibility to enable patched based on filter file (ticket #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
capehill committed Apr 22, 2019
1 parent 1cdbf4f commit afc765c
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 41 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ glSnoop supports partial logging of:
- OGLES2: enable ogles2.library tracing
- NOVA: enable Warp3DNova.library tracing
- GUI: launch the graphical user interface
- FILTER filename: define subset of patched functions

## Tips

Expand Down
91 changes: 91 additions & 0 deletions filter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include "filter.h"
#include "common.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_FILTER 100

static char* filters[MAX_FILTER];
static size_t count;

static void add_filter(const char* const name)
{
if (strlen(name) > 0) {
size_t i;
for (i = 0; i < MAX_FILTER; i++) {
if (filters[i] == NULL) {
filters[i] = strdup(name);
logLine("Filter[%u] '%s' added", count, filters[i]);
count++;
break;
}
}
}
}

static void strip(char* const buffer)
{
size_t i;
for (i = 0; i < strlen(buffer); i++) {
switch (buffer[i]) {
case ' ':
case '\n':
case '#':
case ';':
buffer[i] = 0;
break;
}
}
}

BOOL load_filters(const char* const fileName)
{
if (fileName) {
FILE* file = fopen(fileName, "r");

if (file) {
char buffer[64];
while (!feof(file)) {
if (fgets(buffer, sizeof(buffer), file) != NULL) {
strip(buffer);
add_filter(buffer);
}
}

fclose(file);
return TRUE;
}
}

return FALSE;
}

BOOL match(const char* const name)
{
if (count == 0) {
// No filters in use
return TRUE;
}

size_t i;
for (i = 0; i < MAX_FILTER; i++) {
if (filters[i] && strstr(name, filters[i])) {
//logLine("Match '%s' vs '%s'", name, filters[i]);
return TRUE;
}
}

return FALSE;
}

void free_filters(void)
{
size_t i;
for (i = 0; i < MAX_FILTER; i++) {
free(filters[i]);
filters[i] = NULL;
}
}

10 changes: 10 additions & 0 deletions filter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef FILTER_H
#define FILTER_H

#include <exec/types.h>

BOOL load_filters(const char* const fileName);
BOOL match(const char* const name);
void free_filters(void);

#endif
43 changes: 43 additions & 0 deletions filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# glSnoop filters

# OGLES2 functions

# aglSwapBuffers
glCompileShader
glGenBuffers
#glBindBuffer
#glBufferData
#glBufferSubData
glDeleteBuffers
#glEnableVertexAttribArray
#glVertexAttribPointer
#glDrawArrays
#glDrawElements
glShaderSource
#glActiveTexture
#glBindTexture
glGenTextures
glGenerateMipmap
glTexParameterf
glTexParameterfv
glTexParameteri
glTexParameteriv
#glTexSubImage2D
#glTexImage2D
glDeleteTextures

# WARP3D Nova functions

W3DN_Destroy # Keep always active
W3DN_CreateVertexBufferObject
W3DN_DestroyVertexBufferObject
#W3DN_VBOSetArray
#W3DN_VBOGetArray
#W3DN_VBOGetAttr
#W3DN_VBOLock
#W3DN_BufferUnlock
#W3DN_BindVertexAttribArray
#W3DN_DrawArrays
#W3DN_DrawElements


21 changes: 18 additions & 3 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@
#include "warp3dnova_module.h"
#include "common.h"
#include "gui.h"
#include "filter.h"

#include <proto/exec.h>
#include <proto/dos.h>

#include <stdio.h>
#include <string.H>
#include <stdlib.h>

struct Params {
LONG ogles2;
LONG nova;
LONG gui;
char *filter;
};

static const char* portName = "glSnoop port";
static struct Params params = { 0, 0, 0 };
static const char* const portName = "glSnoop port";
static char* filterFile;
static struct Params params = { 0, 0, 0, NULL };

static struct MsgPort* port;

Expand Down Expand Up @@ -51,9 +56,13 @@ static void remove_port()

static void parse_args(void)
{
struct RDArgs *result = IDOS->ReadArgs("OGLES2/S,NOVA/S,GUI/S", (int32 *)&params, NULL);
struct RDArgs *result = IDOS->ReadArgs("OGLES2/S,NOVA/S,GUI/S,FILTER/K", (int32 *)&params, NULL);

if (result) {
if (params.filter) {
filterFile = strdup(params.filter);
}

IDOS->FreeArgs(result);
}

Expand All @@ -65,6 +74,7 @@ static void parse_args(void)
printf("OGLES2 tracing: [%s]\n", params.ogles2 ? "enabled" : "disabled");
printf("WARP3DNOVA tracing: [%s]\n", params.nova ? "enabled" : "disabled");
printf("GUI: [%s]\n", params.gui ? "enabled" : "disabled");
printf("Filter file name: [%s]\n", filterFile ? filterFile : "disabled");
}

static void install_patches(void)
Expand Down Expand Up @@ -112,6 +122,8 @@ int main(int argc __attribute__((unused)), char* argv[] __attribute__((unused)))

create_port();
parse_args();
load_filters(filterFile);

install_patches();

puts("System patched. Press Control-C to quit...");
Expand All @@ -125,6 +137,9 @@ int main(int argc __attribute__((unused)), char* argv[] __attribute__((unused)))
remove_patches();
remove_port();

free_filters();
free(filterFile);

puts("Patches removed. glSnoop terminating");

out:
Expand Down
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ else
DELETE = delete
endif

OBJS = main.o ogles2_module.o warp3dnova_module.o logger.o gui.o common.o
OBJS = main.o ogles2_module.o warp3dnova_module.o logger.o gui.o common.o filter.o
CFLAGS = -Wall -Wextra -O3 -gstabs

%.o : %.c makefile
Expand Down
60 changes: 28 additions & 32 deletions ogles2_module.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "ogles2_module.h"
#include "common.h"
#include "filter.h"

#include <proto/exec.h>
#include <proto/ogles2.h>
Expand Down Expand Up @@ -504,32 +505,32 @@ static void OGLES2_glDeleteTextures(struct OGLES2IFace *Self, GLsizei n, const G
}
}

GENERATE_PATCH(OGLES2IFace, aglSwapBuffers, OGLES2, Ogles2Context)
GENERATE_PATCH(OGLES2IFace, glCompileShader, OGLES2, Ogles2Context)
GENERATE_PATCH(OGLES2IFace, glGenBuffers, OGLES2, Ogles2Context)
GENERATE_PATCH(OGLES2IFace, glBindBuffer, OGLES2, Ogles2Context)
GENERATE_PATCH(OGLES2IFace, glBufferData, OGLES2, Ogles2Context)
GENERATE_PATCH(OGLES2IFace, glBufferSubData, OGLES2, Ogles2Context)
GENERATE_PATCH(OGLES2IFace, glDeleteBuffers, OGLES2, Ogles2Context)
GENERATE_PATCH(OGLES2IFace, glEnableVertexAttribArray, OGLES2, Ogles2Context)
GENERATE_PATCH(OGLES2IFace, glVertexAttribPointer, OGLES2, Ogles2Context)
GENERATE_PATCH(OGLES2IFace, glDrawArrays, OGLES2, Ogles2Context)
GENERATE_PATCH(OGLES2IFace, glDrawElements, OGLES2, Ogles2Context)
GENERATE_PATCH(OGLES2IFace, glShaderSource, OGLES2, Ogles2Context)
GENERATE_PATCH(OGLES2IFace, glActiveTexture, OGLES2, Ogles2Context)
GENERATE_PATCH(OGLES2IFace, glBindTexture, OGLES2, Ogles2Context)
GENERATE_PATCH(OGLES2IFace, glGenTextures, OGLES2, Ogles2Context)
GENERATE_PATCH(OGLES2IFace, glGenerateMipmap, OGLES2, Ogles2Context)
GENERATE_PATCH(OGLES2IFace, glTexParameterf, OGLES2, Ogles2Context)
GENERATE_PATCH(OGLES2IFace, glTexParameterfv, OGLES2, Ogles2Context)
GENERATE_PATCH(OGLES2IFace, glTexParameteri, OGLES2, Ogles2Context)
GENERATE_PATCH(OGLES2IFace, glTexParameteriv, OGLES2, Ogles2Context)
GENERATE_PATCH(OGLES2IFace, glTexSubImage2D, OGLES2, Ogles2Context)
GENERATE_PATCH(OGLES2IFace, glTexImage2D, OGLES2, Ogles2Context)
GENERATE_PATCH(OGLES2IFace, glDeleteTextures, OGLES2, Ogles2Context)
GENERATE_FILTERED_PATCH(OGLES2IFace, aglSwapBuffers, OGLES2, Ogles2Context)
GENERATE_FILTERED_PATCH(OGLES2IFace, glCompileShader, OGLES2, Ogles2Context)
GENERATE_FILTERED_PATCH(OGLES2IFace, glGenBuffers, OGLES2, Ogles2Context)
GENERATE_FILTERED_PATCH(OGLES2IFace, glBindBuffer, OGLES2, Ogles2Context)
GENERATE_FILTERED_PATCH(OGLES2IFace, glBufferData, OGLES2, Ogles2Context)
GENERATE_FILTERED_PATCH(OGLES2IFace, glBufferSubData, OGLES2, Ogles2Context)
GENERATE_FILTERED_PATCH(OGLES2IFace, glDeleteBuffers, OGLES2, Ogles2Context)
GENERATE_FILTERED_PATCH(OGLES2IFace, glEnableVertexAttribArray, OGLES2, Ogles2Context)
GENERATE_FILTERED_PATCH(OGLES2IFace, glVertexAttribPointer, OGLES2, Ogles2Context)
GENERATE_FILTERED_PATCH(OGLES2IFace, glDrawArrays, OGLES2, Ogles2Context)
GENERATE_FILTERED_PATCH(OGLES2IFace, glDrawElements, OGLES2, Ogles2Context)
GENERATE_FILTERED_PATCH(OGLES2IFace, glShaderSource, OGLES2, Ogles2Context)
GENERATE_FILTERED_PATCH(OGLES2IFace, glActiveTexture, OGLES2, Ogles2Context)
GENERATE_FILTERED_PATCH(OGLES2IFace, glBindTexture, OGLES2, Ogles2Context)
GENERATE_FILTERED_PATCH(OGLES2IFace, glGenTextures, OGLES2, Ogles2Context)
GENERATE_FILTERED_PATCH(OGLES2IFace, glGenerateMipmap, OGLES2, Ogles2Context)
GENERATE_FILTERED_PATCH(OGLES2IFace, glTexParameterf, OGLES2, Ogles2Context)
GENERATE_FILTERED_PATCH(OGLES2IFace, glTexParameterfv, OGLES2, Ogles2Context)
GENERATE_FILTERED_PATCH(OGLES2IFace, glTexParameteri, OGLES2, Ogles2Context)
GENERATE_FILTERED_PATCH(OGLES2IFace, glTexParameteriv, OGLES2, Ogles2Context)
GENERATE_FILTERED_PATCH(OGLES2IFace, glTexSubImage2D, OGLES2, Ogles2Context)
GENERATE_FILTERED_PATCH(OGLES2IFace, glTexImage2D, OGLES2, Ogles2Context)
GENERATE_FILTERED_PATCH(OGLES2IFace, glDeleteTextures, OGLES2, Ogles2Context)

static void (*patches[])(BOOL, struct Ogles2Context *) = {
//patch_aglSwapBuffers,
patch_aglSwapBuffers,
patch_glCompileShader,
patch_glGenBuffers,
patch_glBindBuffer,
Expand All @@ -551,8 +552,7 @@ static void (*patches[])(BOOL, struct Ogles2Context *) = {
patch_glTexParameteriv,
patch_glTexSubImage2D,
patch_glTexImage2D,
patch_glDeleteTextures,
NULL
patch_glDeleteTextures
};

void ogles2_install_patches(void)
Expand All @@ -577,9 +577,7 @@ static void patch_ogles2_functions(struct Ogles2Context * ctx)
if (ctx->interface) {
size_t i;
for (i = 0; i < sizeof(patches) / sizeof(patches[0]); i++) {
if (patches[i]) {
patches[i](TRUE, ctx);
}
patches[i](TRUE, ctx);
}
}
}
Expand All @@ -599,9 +597,7 @@ void ogles2_remove_patches(void)
if (contexts[i]) {
size_t p;
for (p = 0; p < sizeof(patches) / sizeof(patches[0]); p++) {
if (patches[p]) {
patches[p](FALSE, contexts[i]);
}
patches[p](FALSE, contexts[i]);
}
}
}
Expand Down
15 changes: 10 additions & 5 deletions warp3dnova_module.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "warp3dnova_module.h"
#include "common.h"
#include "filter.h"

#include <proto/exec.h>
#include <proto/warp3dnova.h>
Expand Down Expand Up @@ -277,12 +278,16 @@ static void W3DN_Destroy(struct W3DN_Context_s *self)
static void patch_##function(BOOL patching, struct NovaContext* nova) \
{ \
if (patching) { \
nova->old_##function = nova->context->function; \
nova->context->function = W3DN_##function; \
logLine("Patched W3DN context function " #function); \
if (match("W3DN_" #function)) { \
nova->old_##function = nova->context->function; \
nova->context->function = W3DN_##function; \
logLine("Patched W3DN context function " #function); \
} \
} else { \
nova->context->function = nova->old_##function; \
nova->old_##function = NULL; \
if (nova->old_##function) { \
nova->context->function = nova->old_##function; \
nova->old_##function = NULL; \
} \
} \
}

Expand Down

0 comments on commit afc765c

Please sign in to comment.