anv: null check for buffer before reading size
[mesa.git] / src / intel / tools / intel_dump_gpu.c
index 24dd1dffcaa596b86653769459eced47eaa03482..197aebd5b328d3153d3b8844849ead0efa8c232a 100644 (file)
 #include "intel_aub.h"
 #include "aub_write.h"
 
+#include "dev/gen_debug.h"
 #include "dev/gen_device_info.h"
 #include "util/macros.h"
 
 static int close_init_helper(int fd);
 static int ioctl_init_helper(int fd, unsigned long request, ...);
+static int munmap_init_helper(void *addr, size_t length);
 
 static int (*libc_close)(int fd) = close_init_helper;
 static int (*libc_ioctl)(int fd, unsigned long request, ...) = ioctl_init_helper;
+static int (*libc_munmap)(void *addr, size_t length) = munmap_init_helper;
 
 static int drm_fd = -1;
 static char *output_filename = NULL;
 static FILE *output_file = NULL;
 static int verbose = 0;
-static bool device_override;
+static bool device_override = false;
+static bool capture_only = false;
+static int64_t frame_id = -1;
+static bool capture_finished = false;
 
 #define MAX_FD_COUNT 64
 #define MAX_BO_COUNT 64 * 1024
@@ -65,6 +71,14 @@ struct bo {
    uint32_t size;
    uint64_t offset;
    void *map;
+   /* Whether the buffer has been positionned in the GTT already. */
+   bool gtt_mapped : 1;
+   /* Tracks userspace mmapping of the buffer */
+   bool user_mapped : 1;
+   /* Using the i915-gem mmapping ioctl & execbuffer ioctl, track whether a
+    * buffer has been updated.
+    */
+   bool dirty : 1;
 };
 
 static struct bo *bos;
@@ -218,6 +232,9 @@ dump_execbuffer2(int fd, struct drm_i915_gem_execbuffer2 *execbuffer2)
 
    ensure_device_info(fd);
 
+   if (capture_finished)
+      return;
+
    if (!aub_file.file) {
       aub_file_init(&aub_file, output_file,
                     verbose == 2 ? stdout : NULL,
@@ -234,9 +251,6 @@ dump_execbuffer2(int fd, struct drm_i915_gem_execbuffer2 *execbuffer2)
    else
       offset = aub_gtt_size(&aub_file);
 
-   if (verbose)
-      printf("Dumping execbuffer2:\n");
-
    for (uint32_t i = 0; i < execbuffer2->buffer_count; i++) {
       obj = &exec_objects[i];
       bo = get_bo(fd, obj->handle);
@@ -252,27 +266,71 @@ dump_execbuffer2(int fd, struct drm_i915_gem_execbuffer2 *execbuffer2)
 
       if (obj->flags & EXEC_OBJECT_PINNED) {
          bo->offset = obj->offset;
-         if (verbose)
-            printf("BO #%d (%dB) pinned @ 0x%" PRIx64 "\n",
-                   obj->handle, bo->size, bo->offset);
       } else {
          if (obj->alignment != 0)
             offset = align_u32(offset, obj->alignment);
          bo->offset = offset;
-         if (verbose)
-            printf("BO #%d (%dB) @ 0x%" PRIx64 "\n", obj->handle,
-                   bo->size, bo->offset);
          offset = align_u32(offset + bo->size + 4095, 4096);
       }
 
       if (bo->map == NULL && bo->size > 0)
          bo->map = gem_mmap(fd, obj->handle, 0, bo->size);
       fail_if(bo->map == MAP_FAILED, "bo mmap failed\n");
+   }
+
+   uint64_t current_frame_id = 0;
+   if (frame_id >= 0) {
+      for (uint32_t i = 0; i < execbuffer2->buffer_count; i++) {
+         obj = &exec_objects[i];
+         bo = get_bo(fd, obj->handle);
+
+         /* Check against frame_id requirements. */
+         if (memcmp(bo->map, intel_debug_identifier(),
+                    intel_debug_identifier_size()) == 0) {
+            const struct gen_debug_block_frame *frame_desc =
+               intel_debug_get_identifier_block(bo->map, bo->size,
+                                                GEN_DEBUG_BLOCK_TYPE_FRAME);
+
+            current_frame_id = frame_desc ? frame_desc->frame_id : 0;
+            break;
+         }
+      }
+   }
+
+   if (verbose)
+      printf("Dumping execbuffer2 (frame_id=%"PRIu64", buffers=%u):\n",
+             current_frame_id, execbuffer2->buffer_count);
+
+   /* Check whether we can stop right now. */
+   if (frame_id >= 0) {
+      if (current_frame_id < frame_id)
+         return;
+
+      if (current_frame_id > frame_id) {
+         aub_file_finish(&aub_file);
+         capture_finished = true;
+         return;
+      }
+   }
+
+
+   /* Map buffers into the PPGTT. */
+   for (uint32_t i = 0; i < execbuffer2->buffer_count; i++) {
+      obj = &exec_objects[i];
+      bo = get_bo(fd, obj->handle);
+
+      if (verbose) {
+         printf("BO #%d (%dB) @ 0x%" PRIx64 "\n",
+                obj->handle, bo->size, bo->offset);
+      }
 
-      if (aub_use_execlists(&aub_file))
+      if (aub_use_execlists(&aub_file) && !bo->gtt_mapped) {
          aub_map_ppgtt(&aub_file, bo->offset, bo->size);
+         bo->gtt_mapped = true;
+      }
    }
 
+   /* Write the buffer content into the Aub. */
    batch_index = (execbuffer2->flags & I915_EXEC_BATCH_FIRST) ? 0 :
       execbuffer2->buffer_count - 1;
    batch_bo = get_bo(fd, exec_objects[batch_index].handle);
@@ -285,12 +343,19 @@ dump_execbuffer2(int fd, struct drm_i915_gem_execbuffer2 *execbuffer2)
       else
          data = bo->map;
 
-      if (bo == batch_bo) {
-         aub_write_trace_block(&aub_file, AUB_TRACE_TYPE_BATCH,
-                               GET_PTR(data), bo->size, bo->offset);
-      } else {
-         aub_write_trace_block(&aub_file, AUB_TRACE_TYPE_NOTYPE,
-                               GET_PTR(data), bo->size, bo->offset);
+      bool write = !capture_only || (obj->flags & EXEC_OBJECT_CAPTURE);
+
+      if (write && bo->dirty) {
+         if (bo == batch_bo) {
+            aub_write_trace_block(&aub_file, AUB_TRACE_TYPE_BATCH,
+                                  GET_PTR(data), bo->size, bo->offset);
+         } else {
+            aub_write_trace_block(&aub_file, AUB_TRACE_TYPE_NOTYPE,
+                                  GET_PTR(data), bo->size, bo->offset);
+         }
+
+         if (!bo->user_mapped)
+            bo->dirty = false;
       }
 
       if (data != bo->map)
@@ -331,6 +396,8 @@ add_new_bo(unsigned fd, int handle, uint64_t size, void *map)
 
    bo->size = size;
    bo->map = map;
+   bo->user_mapped = false;
+   bo->gtt_mapped = false;
 }
 
 static void
@@ -340,8 +407,7 @@ remove_bo(int fd, int handle)
 
    if (bo->map && !IS_USERPTR(bo->map))
       munmap(bo->map, bo->size);
-   bo->size = 0;
-   bo->map = NULL;
+   memset(bo, 0, sizeof(*bo));
 }
 
 __attribute__ ((visibility ("default"))) int
@@ -405,6 +471,10 @@ maybe_init(int fd)
          fail_if(output_file == NULL,
                  "failed to open file '%s'\n",
                  output_filename);
+      } else if (!strcmp(key, "capture_only")) {
+         capture_only = atoi(value);
+      } else if (!strcmp(key, "frame")) {
+         frame_id = atol(value);
       } else {
          fprintf(stderr, "unknown option '%s'\n", key);
       }
@@ -636,6 +706,17 @@ ioctl(int fd, unsigned long request, ...)
          return ret;
       }
 
+      case DRM_IOCTL_I915_GEM_MMAP: {
+         ret = libc_ioctl(fd, request, argp);
+         if (ret == 0) {
+            struct drm_i915_gem_mmap *mmap = argp;
+            struct bo *bo = get_bo(fd, mmap->handle);
+            bo->user_mapped = true;
+            bo->dirty = true;
+         }
+         return ret;
+      }
+
       default:
          return libc_ioctl(fd, request, argp);
       }
@@ -649,6 +730,7 @@ init(void)
 {
    libc_close = dlsym(RTLD_NEXT, "close");
    libc_ioctl = dlsym(RTLD_NEXT, "ioctl");
+   libc_munmap = dlsym(RTLD_NEXT, "munmap");
    fail_if(libc_close == NULL || libc_ioctl == NULL,
            "failed to get libc ioctl or close\n");
 }
@@ -674,12 +756,27 @@ ioctl_init_helper(int fd, unsigned long request, ...)
    return libc_ioctl(fd, request, argp);
 }
 
+static int
+munmap_init_helper(void *addr, size_t length)
+{
+   init();
+   for (uint32_t i = 0; i < MAX_FD_COUNT * MAX_BO_COUNT; i++) {
+      struct bo *bo = &bos[i];
+      if (bo->map == addr) {
+         bo->user_mapped = false;
+         break;
+      }
+   }
+   return libc_munmap(addr, length);
+}
+
 static void __attribute__ ((destructor))
 fini(void)
 {
    if (devinfo.gen != 0) {
       free(output_filename);
-      aub_file_finish(&aub_file);
+      if (!capture_finished)
+         aub_file_finish(&aub_file);
       free(bos);
    }
 }