Skip to content

Instantly share code, notes, and snippets.

@jlHertel
Last active August 25, 2019 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlHertel/6a594b2d2d37d039ac383c8551c1b7bd to your computer and use it in GitHub Desktop.
Save jlHertel/6a594b2d2d37d039ac383c8551c1b7bd to your computer and use it in GitHub Desktop.
Segmentation Fault with MESA_query_driver example
// Compile with: g++ -o test.bin test.cpp -lgbm -lGL -lEGL -I/usr/include/libdrm -I/usr/include/EGL -ldrm
#include <iostream>
#include <xf86drm.h>
#include <fcntl.h>
#include <gbm.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <drm.h>
typedef EGLDisplay *eglGetPlatformDisplayEXT_t (EGLenum platform, void *native_display, const EGLint *attrib_list);
typedef const char *eglGetDisplayDriverName_t(EGLDisplay disp);
typedef const char *eglGetDisplayDriverconfig_t(EGLDisplay disp);
int main() {
EGLint major = 0, minor = 0;
EGLBoolean binded = eglBindAPI(EGL_OPENGL_API);
if (!binded) {
std::cout << "Failed to bind EGL API" << std::endl;
return 0;
}
auto eglGetPlatformDisplayEXT = (eglGetPlatformDisplayEXT_t *) eglGetProcAddress("eglGetPlatformDisplayEXT");
auto driverName = (eglGetDisplayDriverName_t *) eglGetProcAddress("eglGetDisplayDriverName");
auto driverConfig = (eglGetDisplayDriverconfig_t *) eglGetProcAddress("eglGetDisplayDriverConfig");
drmDevicePtr enumeratedDevices[32];
int deviceCount = drmGetDevices2(0, enumeratedDevices, 32);
std::cout << "Found " << deviceCount << " devices" << std::endl;
for (int i = 0; i < deviceCount; i++) {
std::cout << "Processing device " << i << std::endl;
int fd = open(enumeratedDevices[i]->nodes[DRM_NODE_RENDER], O_RDONLY);
struct gbm_device *gbm = gbm_create_device(fd);
EGLDisplay eglDpy = eglGetPlatformDisplayEXT(EGL_PLATFORM_GBM_MESA, gbm, NULL);
if (eglDpy == EGL_NO_DISPLAY) {
std::cout << "Failed to get display" << std::endl;
continue;
}
EGLBoolean initialized = eglInitialize(eglDpy, &major, &minor);
if (initialized == EGL_FALSE) {
std::cout << "Failed to initialize EGL" << std::endl;
continue;
}
std::cout << "Querying driver name" << std::endl;
const char *name = (* driverName) (eglDpy);
if (name != nullptr) {
std::cout << "Driver " << i << " is named " << name << std::endl;
}
std::cout << "Querying driver config";
const char *configsFound = (* driverConfig) (eglDpy);
if (configsFound != nullptr) {
std::cout << "Driver " << i << " has the configs: ";
std::cout << configsFound << std::endl;
}
eglTerminate(eglDpy);
}
return 0;
}
@jlHertel
Copy link
Author

Assume that I compiled with g++ -o test.bin test.cpp -lgbm -lGL -lEGL -I/usr/include/libdrm -I/usr/include/EGL -ldrm
Running this gives me the output:

./test.bin 
Found 1 devices
Processing device 0
Querying driver name
Driver 0 is named i965
Segmentation Fault

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment