<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Feb 8, 2018 at 12:27 AM, Chris Wilson <span dir="ltr"><<a href="mailto:chris@chris-wilson.co.uk" target="_blank">chris@chris-wilson.co.uk</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Quoting Scott D Phillips (2018-02-08 00:19:04)<br>
<span class="">> From: Kevin Rogovin <<a href="mailto:kevin.rogovin@intel.com">kevin.rogovin@intel.com</a>><br>
><br>
> Adds a new debug tool to pad each GEM BO allocated with (weak)<br>
> pseudo-random noise values which are then checked after each<br>
> batchbuffer dispatch to the kernel. This can be quite valuable to<br>
> find diffucult to track down heisenberg style bugs.<br>
><br>
> [<a href="mailto:scott.d.phillips@intel.com">scott.d.phillips@intel.com</a>: split to separate tool]<br>
><br>
> v2: (by Scott D Phillips)<br>
>     - track gem handles per fd (Kevin)<br>
>     - remove handles on GEM_CLOSE (Kevin)<br>
>     - ignore prime handles<br>
>     - meson & shell script<br>
> ---<br>
</span><span class="">> +__attribute__ ((visibility ("default"))) int<br>
> +open(const char *path, int flags, ...)<br>
> +{<br>
> +   va_list args;<br>
> +   mode_t mode;<br>
> +<br>
> +   va_start(args, flags);<br>
> +   mode = va_arg(args, int);<br>
> +   va_end(args);<br>
> +<br>
> +   int fd = libc_open(path, flags, mode);<br>
> +<br>
> +   if (fd >= 0 && strcmp(path, "/dev/dri/renderD128") == 0)<br>
> +      add_drm_fd(fd);<br>
> +<br>
> +   return fd;<br>
> +}<br>
<br>
</span><span class="">> +__attribute__ ((visibility ("default"))) int<br>
> +ioctl(int fd, unsigned long request, ...)<br>
> +{<br>
> +   va_list args;<br>
> +   void *argp;<br>
> +   struct stat buf;<br>
> +<br>
> +   va_start(args, request);<br>
> +   argp = va_arg(args, void *);<br>
> +   va_end(args);<br>
> +<br>
> +   if (_IOC_TYPE(request) == DRM_IOCTL_BASE &&<br>
> +       !is_drm_fd(fd) && fstat(fd, &buf) == 0 &&<br>
> +       (buf.st_mode & S_IFMT) == S_IFCHR && major(buf.st_rdev) == DRM_MAJOR) {<br>
<br>
</span>So you know how to recognise drm from fstat, why not use that for open<br>
(so you don't hardcode the assumption that i915 is card0)?<br>
<br>
What you are missing is recognising i915 devices, for which you want to<br>
query drm_version:<br>
<br>
static int __get_drm_device_name(int fd, char *name, int len)<br>
{<br>
        drm_version_t version = {<br>
                .name = name,<br>
                .name_len = len<br>
        };<br>
        int err = 0;<br>
<br>
        if (ioctl(fd, DRM_IOCTL_VERSION, &version))<br>
                err = -errno;<br>
<br>
        return err;<br>
}<br>
<br>
static bool __is_device(int fd, const char *expect)<br>
{<br>
        char name[5] = "";<br>
<br>
        if (__get_drm_device_name(fd, name, sizeof(name) - 1))<br>
                return false;<br>
<br>
        return strcmp(expect, name) == 0;<br>
}<br>
<br>
static bool is_i915_device(int fd)<br>
{<br>
        return __is_device(fd, "i915");<br>
}<br></blockquote><div><br></div><div> Here's a thought.  Given the number of random little tools we have these days that hook i915, perhaps we'd like to make a little static library (or just a shared C file) which does all this boiler-plate once so we get it right everywhere.<br></div></div></div></div>