intel: tools: dump: only store device id on success
[mesa.git] / src / intel / tools / intel_dump_gpu.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdint.h>
28 #include <stdbool.h>
29 #include <signal.h>
30 #include <stdarg.h>
31 #include <fcntl.h>
32 #include <sys/types.h>
33 #include <sys/sysmacros.h>
34 #include <sys/stat.h>
35 #include <sys/ioctl.h>
36 #include <unistd.h>
37 #include <errno.h>
38 #include <sys/mman.h>
39 #include <dlfcn.h>
40 #include <i915_drm.h>
41 #include <inttypes.h>
42
43 #include "intel_aub.h"
44 #include "aub_write.h"
45
46 #include "dev/gen_device_info.h"
47 #include "util/macros.h"
48
49 static int close_init_helper(int fd);
50 static int ioctl_init_helper(int fd, unsigned long request, ...);
51
52 static int (*libc_close)(int fd) = close_init_helper;
53 static int (*libc_ioctl)(int fd, unsigned long request, ...) = ioctl_init_helper;
54
55 static int drm_fd = -1;
56 static char *output_filename = NULL;
57 static FILE *output_file = NULL;
58 static int verbose = 0;
59 static bool device_override;
60
61 #define MAX_BO_COUNT 64 * 1024
62
63 struct bo {
64 uint32_t size;
65 uint64_t offset;
66 void *map;
67 };
68
69 static struct bo *bos;
70
71 #define DRM_MAJOR 226
72
73 /* We set bit 0 in the map pointer for userptr BOs so we know not to
74 * munmap them on DRM_IOCTL_GEM_CLOSE.
75 */
76 #define USERPTR_FLAG 1
77 #define IS_USERPTR(p) ((uintptr_t) (p) & USERPTR_FLAG)
78 #define GET_PTR(p) ( (void *) ((uintptr_t) p & ~(uintptr_t) 1) )
79
80 static void __attribute__ ((format(__printf__, 2, 3)))
81 fail_if(int cond, const char *format, ...)
82 {
83 va_list args;
84
85 if (!cond)
86 return;
87
88 va_start(args, format);
89 fprintf(stderr, "intel_dump_gpu: ");
90 vfprintf(stderr, format, args);
91 va_end(args);
92
93 raise(SIGTRAP);
94 }
95
96 static struct bo *
97 get_bo(uint32_t handle)
98 {
99 struct bo *bo;
100
101 fail_if(handle >= MAX_BO_COUNT, "bo handle too large\n");
102 bo = &bos[handle];
103
104 return bo;
105 }
106
107 static inline uint32_t
108 align_u32(uint32_t v, uint32_t a)
109 {
110 return (v + a - 1) & ~(a - 1);
111 }
112
113 static struct gen_device_info devinfo = {0};
114 static uint32_t device = 0;
115 static struct aub_file aub_file;
116
117 static void *
118 relocate_bo(struct bo *bo, const struct drm_i915_gem_execbuffer2 *execbuffer2,
119 const struct drm_i915_gem_exec_object2 *obj)
120 {
121 const struct drm_i915_gem_exec_object2 *exec_objects =
122 (struct drm_i915_gem_exec_object2 *) (uintptr_t) execbuffer2->buffers_ptr;
123 const struct drm_i915_gem_relocation_entry *relocs =
124 (const struct drm_i915_gem_relocation_entry *) (uintptr_t) obj->relocs_ptr;
125 void *relocated;
126 int handle;
127
128 relocated = malloc(bo->size);
129 fail_if(relocated == NULL, "out of memory\n");
130 memcpy(relocated, GET_PTR(bo->map), bo->size);
131 for (size_t i = 0; i < obj->relocation_count; i++) {
132 fail_if(relocs[i].offset >= bo->size, "reloc outside bo\n");
133
134 if (execbuffer2->flags & I915_EXEC_HANDLE_LUT)
135 handle = exec_objects[relocs[i].target_handle].handle;
136 else
137 handle = relocs[i].target_handle;
138
139 aub_write_reloc(&devinfo, ((char *)relocated) + relocs[i].offset,
140 get_bo(handle)->offset + relocs[i].delta);
141 }
142
143 return relocated;
144 }
145
146 static int
147 gem_ioctl(int fd, unsigned long request, void *argp)
148 {
149 int ret;
150
151 do {
152 ret = libc_ioctl(fd, request, argp);
153 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
154
155 return ret;
156 }
157
158 static void *
159 gem_mmap(int fd, uint32_t handle, uint64_t offset, uint64_t size)
160 {
161 struct drm_i915_gem_mmap mmap = {
162 .handle = handle,
163 .offset = offset,
164 .size = size
165 };
166
167 if (gem_ioctl(fd, DRM_IOCTL_I915_GEM_MMAP, &mmap) == -1)
168 return MAP_FAILED;
169
170 return (void *)(uintptr_t) mmap.addr_ptr;
171 }
172
173 static int
174 gem_get_param(int fd, uint32_t param)
175 {
176 int value;
177 drm_i915_getparam_t gp = {
178 .param = param,
179 .value = &value
180 };
181
182 if (gem_ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp) == -1)
183 return 0;
184
185 return value;
186 }
187
188 static void
189 dump_execbuffer2(int fd, struct drm_i915_gem_execbuffer2 *execbuffer2)
190 {
191 struct drm_i915_gem_exec_object2 *exec_objects =
192 (struct drm_i915_gem_exec_object2 *) (uintptr_t) execbuffer2->buffers_ptr;
193 uint32_t ring_flag = execbuffer2->flags & I915_EXEC_RING_MASK;
194 uint32_t offset;
195 struct drm_i915_gem_exec_object2 *obj;
196 struct bo *bo, *batch_bo;
197 int batch_index;
198 void *data;
199
200 /* We can't do this at open time as we're not yet authenticated. */
201 if (device == 0) {
202 device = gem_get_param(fd, I915_PARAM_CHIPSET_ID);
203 fail_if(device == 0 || devinfo.gen == 0, "failed to identify chipset\n");
204 }
205 if (devinfo.gen == 0) {
206 fail_if(!gen_get_device_info(device, &devinfo),
207 "failed to identify chipset=0x%x\n", device);
208
209 aub_file_init(&aub_file, output_file, device);
210 if (verbose == 2)
211 aub_file.verbose_log_file = stdout;
212 aub_write_header(&aub_file, program_invocation_short_name);
213
214 if (verbose)
215 printf("[running, output file %s, chipset id 0x%04x, gen %d]\n",
216 output_filename, device, devinfo.gen);
217 }
218
219 if (aub_use_execlists(&aub_file))
220 offset = 0x1000;
221 else
222 offset = aub_gtt_size(&aub_file);
223
224 if (verbose)
225 printf("Dumping execbuffer2:\n");
226
227 for (uint32_t i = 0; i < execbuffer2->buffer_count; i++) {
228 obj = &exec_objects[i];
229 bo = get_bo(obj->handle);
230
231 /* If bo->size == 0, this means they passed us an invalid
232 * buffer. The kernel will reject it and so should we.
233 */
234 if (bo->size == 0) {
235 if (verbose)
236 printf("BO #%d is invalid!\n", obj->handle);
237 return;
238 }
239
240 if (obj->flags & EXEC_OBJECT_PINNED) {
241 bo->offset = obj->offset;
242 if (verbose)
243 printf("BO #%d (%dB) pinned @ 0x%lx\n",
244 obj->handle, bo->size, bo->offset);
245 } else {
246 if (obj->alignment != 0)
247 offset = align_u32(offset, obj->alignment);
248 bo->offset = offset;
249 if (verbose)
250 printf("BO #%d (%dB) @ 0x%lx\n", obj->handle,
251 bo->size, bo->offset);
252 offset = align_u32(offset + bo->size + 4095, 4096);
253 }
254
255 if (bo->map == NULL && bo->size > 0)
256 bo->map = gem_mmap(fd, obj->handle, 0, bo->size);
257 fail_if(bo->map == MAP_FAILED, "bo mmap failed\n");
258
259 if (aub_use_execlists(&aub_file))
260 aub_map_ppgtt(&aub_file, bo->offset, bo->size);
261 }
262
263 batch_index = (execbuffer2->flags & I915_EXEC_BATCH_FIRST) ? 0 :
264 execbuffer2->buffer_count - 1;
265 batch_bo = get_bo(exec_objects[batch_index].handle);
266 for (uint32_t i = 0; i < execbuffer2->buffer_count; i++) {
267 obj = &exec_objects[i];
268 bo = get_bo(obj->handle);
269
270 if (obj->relocation_count > 0)
271 data = relocate_bo(bo, execbuffer2, obj);
272 else
273 data = bo->map;
274
275 if (bo == batch_bo) {
276 aub_write_trace_block(&aub_file, AUB_TRACE_TYPE_BATCH,
277 GET_PTR(data), bo->size, bo->offset);
278 } else {
279 aub_write_trace_block(&aub_file, AUB_TRACE_TYPE_NOTYPE,
280 GET_PTR(data), bo->size, bo->offset);
281 }
282
283 if (data != bo->map)
284 free(data);
285 }
286
287 aub_write_exec(&aub_file,
288 batch_bo->offset + execbuffer2->batch_start_offset,
289 offset, ring_flag);
290
291 if (device_override &&
292 (execbuffer2->flags & I915_EXEC_FENCE_ARRAY) != 0) {
293 struct drm_i915_gem_exec_fence *fences =
294 (void*)(uintptr_t)execbuffer2->cliprects_ptr;
295 for (uint32_t i = 0; i < execbuffer2->num_cliprects; i++) {
296 if ((fences[i].flags & I915_EXEC_FENCE_SIGNAL) != 0) {
297 struct drm_syncobj_array arg = {
298 .handles = (uintptr_t)&fences[i].handle,
299 .count_handles = 1,
300 .pad = 0,
301 };
302 libc_ioctl(fd, DRM_IOCTL_SYNCOBJ_SIGNAL, &arg);
303 }
304 }
305 }
306 }
307
308 static void
309 add_new_bo(int handle, uint64_t size, void *map)
310 {
311 struct bo *bo = &bos[handle];
312
313 fail_if(handle >= MAX_BO_COUNT, "bo handle out of range\n");
314 fail_if(size == 0, "bo size is invalid\n");
315
316 bo->size = size;
317 bo->map = map;
318 }
319
320 static void
321 remove_bo(int handle)
322 {
323 struct bo *bo = get_bo(handle);
324
325 if (bo->map && !IS_USERPTR(bo->map))
326 munmap(bo->map, bo->size);
327 bo->size = 0;
328 bo->map = NULL;
329 }
330
331 __attribute__ ((visibility ("default"))) int
332 close(int fd)
333 {
334 if (fd == drm_fd)
335 drm_fd = -1;
336
337 return libc_close(fd);
338 }
339
340 static void
341 maybe_init(void)
342 {
343 static bool initialized = false;
344 FILE *config;
345 char *key, *value;
346
347 if (initialized)
348 return;
349
350 initialized = true;
351
352 config = fopen(getenv("INTEL_DUMP_GPU_CONFIG"), "r");
353 while (fscanf(config, "%m[^=]=%m[^\n]\n", &key, &value) != EOF) {
354 if (!strcmp(key, "verbose")) {
355 if (!strcmp(value, "1")) {
356 verbose = 1;
357 } else if (!strcmp(value, "2")) {
358 verbose = 2;
359 }
360 } else if (!strcmp(key, "device")) {
361 fail_if(sscanf(value, "%i", &device) != 1,
362 "failed to parse device id '%s'",
363 value);
364 device_override = true;
365 } else if (!strcmp(key, "file")) {
366 output_filename = strdup(value);
367 output_file = fopen(output_filename, "w+");
368 fail_if(output_file == NULL,
369 "failed to open file '%s'\n",
370 output_filename);
371 } else {
372 fprintf(stderr, "unknown option '%s'\n", key);
373 }
374
375 free(key);
376 free(value);
377 }
378 fclose(config);
379
380 bos = calloc(MAX_BO_COUNT, sizeof(bos[0]));
381 fail_if(bos == NULL, "out of memory\n");
382 }
383
384 __attribute__ ((visibility ("default"))) int
385 ioctl(int fd, unsigned long request, ...)
386 {
387 va_list args;
388 void *argp;
389 int ret;
390 struct stat buf;
391
392 va_start(args, request);
393 argp = va_arg(args, void *);
394 va_end(args);
395
396 if (_IOC_TYPE(request) == DRM_IOCTL_BASE &&
397 drm_fd != fd && fstat(fd, &buf) == 0 &&
398 (buf.st_mode & S_IFMT) == S_IFCHR && major(buf.st_rdev) == DRM_MAJOR) {
399 drm_fd = fd;
400 if (verbose)
401 printf("[intercept drm ioctl on fd %d]\n", fd);
402 }
403
404 if (fd == drm_fd) {
405 maybe_init();
406
407 switch (request) {
408 case DRM_IOCTL_I915_GETPARAM: {
409 struct drm_i915_getparam *getparam = argp;
410
411 if (device_override && getparam->param == I915_PARAM_CHIPSET_ID) {
412 *getparam->value = device;
413 return 0;
414 }
415
416 ret = libc_ioctl(fd, request, argp);
417
418 /* If the application looks up chipset_id
419 * (they typically do), we'll piggy-back on
420 * their ioctl and store the id for later
421 * use. */
422 if (ret == 0 && getparam->param == I915_PARAM_CHIPSET_ID)
423 device = *getparam->value;
424
425 return ret;
426 }
427
428 case DRM_IOCTL_I915_GEM_EXECBUFFER: {
429 static bool once;
430 if (!once) {
431 fprintf(stderr,
432 "application uses DRM_IOCTL_I915_GEM_EXECBUFFER, not handled\n");
433 once = true;
434 }
435 return libc_ioctl(fd, request, argp);
436 }
437
438 case DRM_IOCTL_I915_GEM_EXECBUFFER2:
439 case DRM_IOCTL_I915_GEM_EXECBUFFER2_WR: {
440 dump_execbuffer2(fd, argp);
441 if (device_override)
442 return 0;
443
444 return libc_ioctl(fd, request, argp);
445 }
446
447 case DRM_IOCTL_I915_GEM_CREATE: {
448 struct drm_i915_gem_create *create = argp;
449
450 ret = libc_ioctl(fd, request, argp);
451 if (ret == 0)
452 add_new_bo(create->handle, create->size, NULL);
453
454 return ret;
455 }
456
457 case DRM_IOCTL_I915_GEM_USERPTR: {
458 struct drm_i915_gem_userptr *userptr = argp;
459
460 ret = libc_ioctl(fd, request, argp);
461 if (ret == 0)
462 add_new_bo(userptr->handle, userptr->user_size,
463 (void *) (uintptr_t) (userptr->user_ptr | USERPTR_FLAG));
464 return ret;
465 }
466
467 case DRM_IOCTL_GEM_CLOSE: {
468 struct drm_gem_close *close = argp;
469
470 remove_bo(close->handle);
471
472 return libc_ioctl(fd, request, argp);
473 }
474
475 case DRM_IOCTL_GEM_OPEN: {
476 struct drm_gem_open *open = argp;
477
478 ret = libc_ioctl(fd, request, argp);
479 if (ret == 0)
480 add_new_bo(open->handle, open->size, NULL);
481
482 return ret;
483 }
484
485 case DRM_IOCTL_PRIME_FD_TO_HANDLE: {
486 struct drm_prime_handle *prime = argp;
487
488 ret = libc_ioctl(fd, request, argp);
489 if (ret == 0) {
490 off_t size;
491
492 size = lseek(prime->fd, 0, SEEK_END);
493 fail_if(size == -1, "failed to get prime bo size\n");
494 add_new_bo(prime->handle, size, NULL);
495 }
496
497 return ret;
498 }
499
500 default:
501 return libc_ioctl(fd, request, argp);
502 }
503 } else {
504 return libc_ioctl(fd, request, argp);
505 }
506 }
507
508 static void
509 init(void)
510 {
511 libc_close = dlsym(RTLD_NEXT, "close");
512 libc_ioctl = dlsym(RTLD_NEXT, "ioctl");
513 fail_if(libc_close == NULL || libc_ioctl == NULL,
514 "failed to get libc ioctl or close\n");
515 }
516
517 static int
518 close_init_helper(int fd)
519 {
520 init();
521 return libc_close(fd);
522 }
523
524 static int
525 ioctl_init_helper(int fd, unsigned long request, ...)
526 {
527 va_list args;
528 void *argp;
529
530 va_start(args, request);
531 argp = va_arg(args, void *);
532 va_end(args);
533
534 init();
535 return libc_ioctl(fd, request, argp);
536 }
537
538 static void __attribute__ ((destructor))
539 fini(void)
540 {
541 free(output_filename);
542 aub_file_finish(&aub_file);
543 free(bos);
544 }