Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 Alyssa Rosenzweig |
| 3 | * Copyright (C) 2017-2018 Lyude Paul |
Alyssa Rosenzweig | d4575c3 | 2019-06-25 13:30:17 -0700 | [diff] [blame] | 4 | * Copyright (C) 2019 Collabora, Ltd. |
Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the "Software"), |
| 8 | * to deal in the Software without restriction, including without limitation |
| 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | * and/or sell copies of the Software, and to permit persons to whom the |
| 11 | * Software is furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice (including the next |
| 14 | * paragraph) shall be included in all copies or substantial portions of the |
| 15 | * Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | * SOFTWARE. |
| 24 | */ |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <assert.h> |
| 29 | #include <stdint.h> |
| 30 | #include <string.h> |
| 31 | |
| 32 | #include "decode.h" |
| 33 | #include "util/macros.h" |
Icecream95 | c195277 | 2020-01-23 10:23:17 +1300 | [diff] [blame] | 34 | #include "util/u_debug.h" |
Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 35 | |
| 36 | /* Memory handling */ |
| 37 | |
| 38 | static struct pandecode_mapped_memory mmaps; |
| 39 | |
| 40 | struct pandecode_mapped_memory * |
Alyssa Rosenzweig | 4f03728 | 2019-08-14 13:21:21 -0700 | [diff] [blame] | 41 | pandecode_find_mapped_gpu_mem_containing(uint64_t addr) |
Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 42 | { |
| 43 | list_for_each_entry(struct pandecode_mapped_memory, pos, &mmaps.node, node) { |
| 44 | if (addr >= pos->gpu_va && addr < pos->gpu_va + pos->length) |
| 45 | return pos; |
| 46 | } |
| 47 | |
| 48 | return NULL; |
| 49 | } |
| 50 | |
Alyssa Rosenzweig | b072d03 | 2019-08-19 10:55:29 -0700 | [diff] [blame] | 51 | static void |
| 52 | pandecode_add_name(struct pandecode_mapped_memory *mem, uint64_t gpu_va, const char *name) |
| 53 | { |
| 54 | if (!name) { |
| 55 | /* If we don't have a name, assign one */ |
| 56 | |
| 57 | snprintf(mem->name, ARRAY_SIZE(mem->name) - 1, |
| 58 | "memory_%" PRIx64, gpu_va); |
| 59 | } else { |
| 60 | assert((strlen(name) + 1) < ARRAY_SIZE(mem->name)); |
| 61 | memcpy(mem->name, name, strlen(name) + 1); |
| 62 | } |
| 63 | } |
| 64 | |
Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 65 | void |
Alyssa Rosenzweig | 4f03728 | 2019-08-14 13:21:21 -0700 | [diff] [blame] | 66 | pandecode_inject_mmap(uint64_t gpu_va, void *cpu, unsigned sz, const char *name) |
Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 67 | { |
Alyssa Rosenzweig | b072d03 | 2019-08-19 10:55:29 -0700 | [diff] [blame] | 68 | /* First, search if we already mapped this and are just updating an address */ |
| 69 | |
| 70 | list_for_each_entry(struct pandecode_mapped_memory, pos, &mmaps.node, node) { |
| 71 | if (pos->gpu_va == gpu_va) { |
| 72 | /* TODO: Resizing weirdness. Only applies to tracing |
| 73 | * the legacy driver, not for native traces */ |
| 74 | |
| 75 | pos->length = sz; |
| 76 | pos->addr = cpu; |
| 77 | pandecode_add_name(pos, gpu_va, name); |
| 78 | |
| 79 | return; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /* Otherwise, add a fresh mapping */ |
Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 84 | struct pandecode_mapped_memory *mapped_mem = NULL; |
| 85 | |
| 86 | mapped_mem = malloc(sizeof(*mapped_mem)); |
| 87 | list_inithead(&mapped_mem->node); |
| 88 | |
| 89 | mapped_mem->gpu_va = gpu_va; |
| 90 | mapped_mem->length = sz; |
| 91 | mapped_mem->addr = cpu; |
Alyssa Rosenzweig | b072d03 | 2019-08-19 10:55:29 -0700 | [diff] [blame] | 92 | pandecode_add_name(mapped_mem, gpu_va, name); |
Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 93 | |
| 94 | list_add(&mapped_mem->node, &mmaps.node); |
| 95 | } |
| 96 | |
| 97 | char * |
Alyssa Rosenzweig | 4f03728 | 2019-08-14 13:21:21 -0700 | [diff] [blame] | 98 | pointer_as_memory_reference(uint64_t ptr) |
Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 99 | { |
| 100 | struct pandecode_mapped_memory *mapped; |
| 101 | char *out = malloc(128); |
| 102 | |
| 103 | /* Try to find the corresponding mapped zone */ |
| 104 | |
| 105 | mapped = pandecode_find_mapped_gpu_mem_containing(ptr); |
| 106 | |
| 107 | if (mapped) { |
| 108 | snprintf(out, 128, "%s + %d", mapped->name, (int) (ptr - mapped->gpu_va)); |
| 109 | return out; |
| 110 | } |
| 111 | |
| 112 | /* Just use the raw address if other options are exhausted */ |
| 113 | |
Alyssa Rosenzweig | 4f03728 | 2019-08-14 13:21:21 -0700 | [diff] [blame] | 114 | snprintf(out, 128, "0x%" PRIx64, ptr); |
Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 115 | return out; |
| 116 | |
| 117 | } |
| 118 | |
Icecream95 | cf2c5a5 | 2020-01-23 10:32:18 +1300 | [diff] [blame^] | 119 | static int pandecode_dump_frame_count = 0; |
| 120 | |
Icecream95 | c195277 | 2020-01-23 10:23:17 +1300 | [diff] [blame] | 121 | static void |
| 122 | pandecode_dump_file_open(void) |
| 123 | { |
| 124 | if (pandecode_dump_stream) |
| 125 | return; |
| 126 | |
Icecream95 | cf2c5a5 | 2020-01-23 10:32:18 +1300 | [diff] [blame^] | 127 | char buffer[1024]; |
Icecream95 | c195277 | 2020-01-23 10:23:17 +1300 | [diff] [blame] | 128 | |
Icecream95 | cf2c5a5 | 2020-01-23 10:32:18 +1300 | [diff] [blame^] | 129 | /* This does a getenv every frame, so it is possible to use |
| 130 | * setenv to change the base at runtime. |
| 131 | */ |
| 132 | const char *dump_file_base = debug_get_option("PANDECODE_DUMP_FILE", "pandecode.dump"); |
| 133 | snprintf(buffer, sizeof(buffer), "%s.%04d", dump_file_base, pandecode_dump_frame_count); |
| 134 | |
| 135 | printf("pandecode: dump command stream to file %s\n", buffer); |
| 136 | pandecode_dump_stream = fopen(buffer, "w"); |
Icecream95 | c195277 | 2020-01-23 10:23:17 +1300 | [diff] [blame] | 137 | |
| 138 | if (!pandecode_dump_stream) |
| 139 | fprintf(stderr,"pandecode: failed to open command stream log file %s\n", |
Icecream95 | cf2c5a5 | 2020-01-23 10:32:18 +1300 | [diff] [blame^] | 140 | buffer); |
Icecream95 | c195277 | 2020-01-23 10:23:17 +1300 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static void |
| 144 | pandecode_dump_file_close(void) |
| 145 | { |
| 146 | if (pandecode_dump_stream) { |
| 147 | fclose(pandecode_dump_stream); |
| 148 | pandecode_dump_stream = NULL; |
| 149 | } |
| 150 | } |
| 151 | |
Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 152 | void |
| 153 | pandecode_initialize(void) |
| 154 | { |
| 155 | list_inithead(&mmaps.node); |
Icecream95 | c195277 | 2020-01-23 10:23:17 +1300 | [diff] [blame] | 156 | pandecode_dump_file_open(); |
| 157 | } |
| 158 | |
| 159 | void |
Icecream95 | cf2c5a5 | 2020-01-23 10:32:18 +1300 | [diff] [blame^] | 160 | pandecode_next_frame(void) |
| 161 | { |
| 162 | pandecode_dump_file_close(); |
| 163 | pandecode_dump_frame_count++; |
| 164 | pandecode_dump_file_open(); |
| 165 | } |
| 166 | |
| 167 | void |
Icecream95 | c195277 | 2020-01-23 10:23:17 +1300 | [diff] [blame] | 168 | pandecode_close(void) |
| 169 | { |
| 170 | pandecode_dump_file_close(); |
Alyssa Rosenzweig | fc7bcee | 2019-06-11 12:25:35 -0700 | [diff] [blame] | 171 | } |