Skip to content

Instantly share code, notes, and snippets.

@jgmdev
Last active May 21, 2020 18:27
Show Gist options
  • Save jgmdev/d4695e11841793d5f468798d39350f25 to your computer and use it in GitHub Desktop.
Save jgmdev/d4695e11841793d5f468798d39350f25 to your computer and use it in GitHub Desktop.
Small wrapper to troubleshoot libMali issues
// Compile: gcc -ldl wrapper.c -g -shared -o libwrapper.so
// Usage: LD_PRELOAD="./libwrapper.so" {program}
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <string.h>
#include <EGL/egl.h>
#include <X11/Xutil.h>
typedef struct wrapper_s {
char initialized;
void* libMali;
void* libX;
EGLDisplay (*eglGetDisplay)(EGLNativeDisplayType);
XVisualInfo* (*XGetVisualInfo)(Display*, long, XVisualInfo*, int*);
Display* (*XOpenDisplay)(const char*);
} wrapper;
static wrapper wrapper_g;
void wrapper_load()
{
if(wrapper_g.initialized)
return;
printf("WRAPPER: [LOADED]\n");
char *error;
wrapper_g.initialized = 1;
// Load libMali
wrapper_g.libMali = dlopen("libMali.so", RTLD_LAZY);
wrapper_g.libX = dlopen("libX11.so", RTLD_LAZY);
if (!wrapper_g.libMali) {
fprintf (stderr, "%s\n", dlerror());
exit(1);
}
dlerror(); /* Clear any existing error */
// Load symbols
wrapper_g.eglGetDisplay = dlsym(wrapper_g.libMali, "eglGetDisplay");
wrapper_g.XGetVisualInfo = dlsym(wrapper_g.libX, "XGetVisualInfo");
wrapper_g.XOpenDisplay = dlsym(wrapper_g.libX, "XOpenDisplay");
// Check if any error happened
if ((error = dlerror()) != NULL) {
fprintf (stderr, "%s\n", error);
exit(1);
}
}
EGLDisplay eglGetDisplay (EGLNativeDisplayType display_id)
{
wrapper_load();
printf("WRAPPER: eglGetDisplay (param) display_id: %p\n", (void*) display_id);
EGLDisplay out = (*(wrapper_g.eglGetDisplay))(EGL_DEFAULT_DISPLAY);
printf("WRAPPER: eglGetDisplay return: %p\n", (void*) out);
return out;
}
Display* XOpenDisplay(const char* display){
wrapper_load();
printf("WRAPPER: XOpenDisplay (param) display: %p\n", (void*) display);
Display* out = (*(wrapper_g.XOpenDisplay))(display);
printf("WRAPPER: XOpenDisplay return: %p\n", (void*) out);
return out;
}
XVisualInfo* XGetVisualInfo(
Display* display,
long vinfo_mask,
XVisualInfo* vinfo_template,
int* nitems_return
)
{
printf("WRAPPER: XGetVisualInfo (param) display: %p\n", (void*) display);
printf("WRAPPER: XGetVisualInfo (param) vinfo_mask: %ld\n", vinfo_mask);
printf("WRAPPER: XGetVisualInfo (param) vinfo_template.visualid %ld\n", vinfo_template->visualid);
XVisualInfo* out = (*(wrapper_g.XGetVisualInfo))(
display,
vinfo_mask,
vinfo_template,
nitems_return
);
printf("WRAPPER: XGetVisualInfo return: %p\n", (void*) out);
return out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment