Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1 | /* |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 2 | * |
Courtney Goeltzenleuchter | fcbe16f | 2015-10-29 13:50:34 -0600 | [diff] [blame] | 3 | * Copyright (C) 2015 Valve Corporation |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 6 | * copy of this software and associated documentation files (the "Software"), |
| 7 | * to deal in the Software without restriction, including without limitation |
| 8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 | * and/or sell copies of the Software, and to permit persons to whom the |
| 10 | * Software is furnished to do so, subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be included |
| 13 | * in all copies or substantial portions of the 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 |
| 21 | * DEALINGS IN THE SOFTWARE. |
| 22 | * |
Courtney Goeltzenleuchter | 0555952 | 2015-10-30 11:14:30 -0600 | [diff] [blame] | 23 | * Author: Courtney Goeltzenleuchter <courtney@LunarG.com> |
| 24 | * |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 25 | */ |
| 26 | |
Courtney Goeltzenleuchter | 7f5aafc | 2015-07-05 11:28:29 -0600 | [diff] [blame] | 27 | #define _GNU_SOURCE |
Courtney Goeltzenleuchter | 7dcc6a7 | 2015-06-11 16:01:11 -0600 | [diff] [blame] | 28 | #include <stdio.h> |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 29 | #include <string.h> |
| 30 | #include <stdlib.h> |
Courtney Goeltzenleuchter | 7dcc6a7 | 2015-06-11 16:01:11 -0600 | [diff] [blame] | 31 | #include <inttypes.h> |
Tony Barbour | 1d825c7 | 2015-06-18 16:29:32 -0600 | [diff] [blame] | 32 | #ifndef WIN32 |
Courtney Goeltzenleuchter | 03663d0 | 2015-07-22 11:01:53 -0600 | [diff] [blame] | 33 | #include <signal.h> |
| 34 | #else |
Tony Barbour | 1d825c7 | 2015-06-18 16:29:32 -0600 | [diff] [blame] | 35 | #endif |
Jon Ashburn | 480a50a | 2015-08-28 14:58:46 -0700 | [diff] [blame] | 36 | #include "vk_loader_platform.h" |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 37 | #include "debug_report.h" |
David Pinedo | 9316d3b | 2015-11-06 12:54:48 -0700 | [diff] [blame] | 38 | #include "vulkan/vk_layer.h" |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 39 | |
Chia-I Wu | 9ab6150 | 2015-11-06 06:42:02 +0800 | [diff] [blame] | 40 | typedef void (VKAPI_PTR *PFN_stringCallback)(char *message); |
Courtney Goeltzenleuchter | 7dcc6a7 | 2015-06-11 16:01:11 -0600 | [diff] [blame] | 41 | |
Jon Ashburn | 5c042ea | 2015-08-04 11:14:18 -0600 | [diff] [blame] | 42 | static const VkExtensionProperties debug_report_extension_info = { |
Courtney Goeltzenleuchter | 09197ae | 2015-11-25 13:40:37 -0700 | [diff] [blame] | 43 | .extensionName = VK_EXT_LUNARG_DEBUG_REPORT_EXTENSION_NAME, |
| 44 | .specVersion = VK_EXT_LUNARG_DEBUG_REPORT_EXTENSION_REVISION, |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | void debug_report_add_instance_extensions( |
Jon Ashburn | e39a4f8 | 2015-08-28 13:38:21 -0600 | [diff] [blame] | 48 | const struct loader_instance *inst, |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 49 | struct loader_extension_list *ext_list) |
| 50 | { |
Jon Ashburn | e39a4f8 | 2015-08-28 13:38:21 -0600 | [diff] [blame] | 51 | loader_add_to_ext_list(inst, ext_list, 1, &debug_report_extension_info); |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void debug_report_create_instance( |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 55 | struct loader_instance *ptr_instance, |
| 56 | const VkInstanceCreateInfo *pCreateInfo) |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 57 | { |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 58 | ptr_instance->debug_report_enabled = false; |
| 59 | |
Chia-I Wu | d50a7d7 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 60 | for (uint32_t i = 0; i < pCreateInfo->enabledExtensionNameCount; i++) { |
Courtney Goeltzenleuchter | 09197ae | 2015-11-25 13:40:37 -0700 | [diff] [blame] | 61 | if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_EXT_LUNARG_DEBUG_REPORT_EXTENSION_NAME) == 0) { |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 62 | ptr_instance->debug_report_enabled = true; |
| 63 | return; |
| 64 | } |
| 65 | } |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 66 | } |
| 67 | |
Jon Ashburn | 4f23258 | 2015-11-06 15:31:44 -0700 | [diff] [blame] | 68 | static VKAPI_ATTR VkResult VKAPI_CALL debug_report_DbgCreateMsgCallback( |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 69 | VkInstance instance, |
| 70 | VkFlags msgFlags, |
| 71 | const PFN_vkDbgMsgCallback pfnMsgCallback, |
| 72 | void* pUserData, |
| 73 | VkDbgMsgCallback* pMsgCallback) |
| 74 | { |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 75 | VkLayerDbgFunctionNode *pNewDbgFuncNode = (VkLayerDbgFunctionNode *) loader_heap_alloc((struct loader_instance *)instance, sizeof(VkLayerDbgFunctionNode), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 76 | if (!pNewDbgFuncNode) |
| 77 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 78 | |
Jon Ashburn | e0e6457 | 2015-09-30 12:56:42 -0600 | [diff] [blame] | 79 | struct loader_instance *inst = loader_get_instance(instance); |
Jon Ashburn | 6301a0f | 2015-05-29 13:15:39 -0600 | [diff] [blame] | 80 | loader_platform_thread_lock_mutex(&loader_lock); |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 81 | VkResult result = inst->disp->DbgCreateMsgCallback(instance, msgFlags, pfnMsgCallback, pUserData, pMsgCallback); |
| 82 | if (result == VK_SUCCESS) { |
| 83 | pNewDbgFuncNode->msgCallback = *pMsgCallback; |
| 84 | pNewDbgFuncNode->pfnMsgCallback = pfnMsgCallback; |
| 85 | pNewDbgFuncNode->msgFlags = msgFlags; |
| 86 | pNewDbgFuncNode->pUserData = pUserData; |
| 87 | pNewDbgFuncNode->pNext = inst->DbgFunctionHead; |
| 88 | inst->DbgFunctionHead = pNewDbgFuncNode; |
| 89 | } else { |
Jon Ashburn | e39a4f8 | 2015-08-28 13:38:21 -0600 | [diff] [blame] | 90 | loader_heap_free((struct loader_instance *) instance, pNewDbgFuncNode); |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 91 | } |
Jon Ashburn | 6301a0f | 2015-05-29 13:15:39 -0600 | [diff] [blame] | 92 | loader_platform_thread_unlock_mutex(&loader_lock); |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 93 | return result; |
| 94 | } |
| 95 | |
Jon Ashburn | 4f23258 | 2015-11-06 15:31:44 -0700 | [diff] [blame] | 96 | static VKAPI_ATTR VkResult VKAPI_CALL debug_report_DbgDestroyMsgCallback( |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 97 | VkInstance instance, |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 98 | VkDbgMsgCallback msg_callback) |
| 99 | { |
Jon Ashburn | e0e6457 | 2015-09-30 12:56:42 -0600 | [diff] [blame] | 100 | struct loader_instance *inst = loader_get_instance(instance); |
Jon Ashburn | 6301a0f | 2015-05-29 13:15:39 -0600 | [diff] [blame] | 101 | loader_platform_thread_lock_mutex(&loader_lock); |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 102 | VkLayerDbgFunctionNode *pTrav = inst->DbgFunctionHead; |
| 103 | VkLayerDbgFunctionNode *pPrev = pTrav; |
| 104 | |
Courtney Goeltzenleuchter | 7d0023c | 2015-06-08 15:09:22 -0600 | [diff] [blame] | 105 | VkResult result = inst->disp->DbgDestroyMsgCallback(instance, msg_callback); |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 106 | |
| 107 | while (pTrav) { |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 108 | if (pTrav->msgCallback == msg_callback) { |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 109 | pPrev->pNext = pTrav->pNext; |
| 110 | if (inst->DbgFunctionHead == pTrav) |
| 111 | inst->DbgFunctionHead = pTrav->pNext; |
Jon Ashburn | e39a4f8 | 2015-08-28 13:38:21 -0600 | [diff] [blame] | 112 | loader_heap_free((struct loader_instance *) instance, pTrav); |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 113 | break; |
| 114 | } |
| 115 | pPrev = pTrav; |
| 116 | pTrav = pTrav->pNext; |
| 117 | } |
| 118 | |
Jon Ashburn | 6301a0f | 2015-05-29 13:15:39 -0600 | [diff] [blame] | 119 | loader_platform_thread_unlock_mutex(&loader_lock); |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 120 | return result; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | /* |
| 125 | * This is the instance chain terminator function |
| 126 | * for DbgCreateMsgCallback |
| 127 | */ |
Tony Barbour | 1d2cd3f | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 128 | |
Chia-I Wu | 9ab6150 | 2015-11-06 06:42:02 +0800 | [diff] [blame] | 129 | VKAPI_ATTR VkResult VKAPI_CALL loader_DbgCreateMsgCallback( |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 130 | VkInstance instance, |
| 131 | VkFlags msgFlags, |
| 132 | const PFN_vkDbgMsgCallback pfnMsgCallback, |
Jon Ashburn | e67741a | 2015-08-06 13:56:43 -0600 | [diff] [blame] | 133 | void* pUserData, |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 134 | VkDbgMsgCallback* pMsgCallback) |
| 135 | { |
| 136 | VkDbgMsgCallback *icd_info; |
| 137 | const struct loader_icd *icd; |
| 138 | struct loader_instance *inst; |
| 139 | VkResult res; |
| 140 | uint32_t storage_idx; |
| 141 | |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 142 | for (inst = loader.instances; inst; inst = inst->next) { |
| 143 | if ((VkInstance) inst == instance) |
| 144 | break; |
| 145 | } |
| 146 | |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 147 | icd_info = calloc(sizeof(VkDbgMsgCallback), inst->total_icd_count); |
| 148 | if (!icd_info) { |
| 149 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 150 | } |
| 151 | |
| 152 | storage_idx = 0; |
| 153 | for (icd = inst->icds; icd; icd = icd->next) { |
| 154 | if (!icd->DbgCreateMsgCallback) { |
| 155 | continue; |
| 156 | } |
| 157 | |
| 158 | res = icd->DbgCreateMsgCallback( |
| 159 | icd->instance, |
| 160 | msgFlags, |
| 161 | pfnMsgCallback, |
| 162 | pUserData, |
| 163 | &icd_info[storage_idx]); |
| 164 | |
| 165 | if (res != VK_SUCCESS) { |
| 166 | break; |
| 167 | } |
| 168 | storage_idx++; |
| 169 | } |
| 170 | |
| 171 | /* roll back on errors */ |
| 172 | if (icd) { |
| 173 | storage_idx = 0; |
| 174 | for (icd = inst->icds; icd; icd = icd->next) { |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 175 | if (icd_info[storage_idx]) { |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 176 | icd->DbgDestroyMsgCallback( |
| 177 | icd->instance, |
| 178 | icd_info[storage_idx]); |
| 179 | } |
| 180 | storage_idx++; |
| 181 | } |
| 182 | |
| 183 | return res; |
| 184 | } |
| 185 | |
Tony Barbour | 1d2cd3f | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 186 | *(VkDbgMsgCallback **)pMsgCallback = icd_info; |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 187 | |
| 188 | return VK_SUCCESS; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * This is the instance chain terminator function |
| 193 | * for DbgDestroyMsgCallback |
| 194 | */ |
Chia-I Wu | 9ab6150 | 2015-11-06 06:42:02 +0800 | [diff] [blame] | 195 | VKAPI_ATTR VkResult VKAPI_CALL loader_DbgDestroyMsgCallback( |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 196 | VkInstance instance, |
| 197 | VkDbgMsgCallback msgCallback) |
| 198 | { |
| 199 | uint32_t storage_idx; |
| 200 | VkDbgMsgCallback *icd_info; |
| 201 | const struct loader_icd *icd; |
| 202 | VkResult res = VK_SUCCESS; |
| 203 | struct loader_instance *inst; |
| 204 | |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 205 | for (inst = loader.instances; inst; inst = inst->next) { |
| 206 | if ((VkInstance) inst == instance) |
| 207 | break; |
| 208 | } |
| 209 | |
Tony Barbour | 1d2cd3f | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 210 | icd_info = *(VkDbgMsgCallback **) &msgCallback; |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 211 | storage_idx = 0; |
| 212 | for (icd = inst->icds; icd; icd = icd->next) { |
Chia-I Wu | e2fc552 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 213 | if (icd_info[storage_idx]) { |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 214 | icd->DbgDestroyMsgCallback( |
Courtney Goeltzenleuchter | 7d0023c | 2015-06-08 15:09:22 -0600 | [diff] [blame] | 215 | icd->instance, |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 216 | icd_info[storage_idx]); |
| 217 | } |
| 218 | storage_idx++; |
| 219 | } |
| 220 | return res; |
| 221 | } |
| 222 | |
Jon Ashburn | f7a48db | 2015-10-01 12:03:17 -0600 | [diff] [blame] | 223 | bool debug_report_instance_gpa( |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 224 | struct loader_instance *ptr_instance, |
Jon Ashburn | f7a48db | 2015-10-01 12:03:17 -0600 | [diff] [blame] | 225 | const char* name, |
| 226 | void **addr) |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 227 | { |
Jon Ashburn | 8a39efc | 2015-11-06 11:02:40 -0700 | [diff] [blame] | 228 | // debug_report is currently advertised to be supported by the loader, |
| 229 | // so always return the entry points if name matches and it's enabled |
Jon Ashburn | f7a48db | 2015-10-01 12:03:17 -0600 | [diff] [blame] | 230 | *addr = NULL; |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 231 | |
Jon Ashburn | f7a48db | 2015-10-01 12:03:17 -0600 | [diff] [blame] | 232 | if (!strcmp("vkDbgCreateMsgCallback", name)) { |
| 233 | *addr = ptr_instance->debug_report_enabled ? (void *) debug_report_DbgCreateMsgCallback : NULL; |
| 234 | return true; |
| 235 | } |
| 236 | if (!strcmp("vkDbgDestroyMsgCallback", name)) { |
| 237 | *addr = ptr_instance->debug_report_enabled ? (void *) debug_report_DbgDestroyMsgCallback : NULL; |
| 238 | return true; |
| 239 | } |
Jon Ashburn | f7a48db | 2015-10-01 12:03:17 -0600 | [diff] [blame] | 240 | return false; |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 241 | } |