blob: cc0293a25f239c2be5967ebdac13a14e25c74d14 [file] [log] [blame]
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001/*
2 * Vulkan
3 *
4 * Copyright (C) 2015 LunarG, Inc.
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 shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Jon Ashburn <jon@lunarg.com>
26 * Courtney Goeltzenleuchter <courtney@lunarg.com>
27 */
28
Courtney Goeltzenleuchterb620ace2015-07-05 11:28:29 -060029#define _GNU_SOURCE
Courtney Goeltzenleuchterf1eb2492015-06-11 16:01:11 -060030#include <stdio.h>
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060031#include <string.h>
32#include <stdlib.h>
Courtney Goeltzenleuchterf1eb2492015-06-11 16:01:11 -060033#include <inttypes.h>
Tony Barbour69698512015-06-18 16:29:32 -060034#ifndef WIN32
Courtney Goeltzenleuchterf1eb2492015-06-11 16:01:11 -060035#include <alloca.h>
Tony Barbour69698512015-06-18 16:29:32 -060036#endif
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060037#include "debug_report.h"
Tobin Ehlis2d1d9702015-07-03 09:42:57 -060038#include "vk_layer.h"
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060039
Courtney Goeltzenleuchterf1eb2492015-06-11 16:01:11 -060040typedef void (VKAPI *PFN_stringCallback)(char *message);
41
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060042static const struct loader_extension_property debug_report_extension_info = {
43 .info = {
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -060044 .extName = DEBUG_REPORT_EXTENSION_NAME,
Courtney Goeltzenleuchter73a21d32015-07-12 13:20:05 -060045 .specVersion = VK_DEBUG_REPORT_EXTENSION_VERSION,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060046 },
47 .origin = VK_EXTENSION_ORIGIN_LOADER,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060048};
49
50void debug_report_add_instance_extensions(
51 struct loader_extension_list *ext_list)
52{
53 loader_add_to_ext_list(ext_list, 1, &debug_report_extension_info);
54}
55
56void debug_report_create_instance(
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -060057 struct loader_instance *ptr_instance,
58 const VkInstanceCreateInfo *pCreateInfo)
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060059{
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -060060 ptr_instance->debug_report_enabled = false;
61
62 for (uint32_t i = 0; i < pCreateInfo->extensionCount; i++) {
63 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], DEBUG_REPORT_EXTENSION_NAME) == 0) {
64 ptr_instance->debug_report_enabled = true;
65 return;
66 }
67 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060068}
69
70static VkResult debug_report_DbgCreateMsgCallback(
71 VkInstance instance,
72 VkFlags msgFlags,
73 const PFN_vkDbgMsgCallback pfnMsgCallback,
74 void* pUserData,
75 VkDbgMsgCallback* pMsgCallback)
76{
77 VkLayerDbgFunctionNode *pNewDbgFuncNode = (VkLayerDbgFunctionNode *) malloc(sizeof(VkLayerDbgFunctionNode));
78 if (!pNewDbgFuncNode)
79 return VK_ERROR_OUT_OF_HOST_MEMORY;
80
Courtney Goeltzenleuchter8afefb52015-06-08 15:04:02 -060081 struct loader_instance *inst = loader_instance(instance);
Jon Ashburnb40f2562015-05-29 13:15:39 -060082 loader_platform_thread_lock_mutex(&loader_lock);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060083 VkResult result = inst->disp->DbgCreateMsgCallback(instance, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);
84 if (result == VK_SUCCESS) {
85 pNewDbgFuncNode->msgCallback = *pMsgCallback;
86 pNewDbgFuncNode->pfnMsgCallback = pfnMsgCallback;
87 pNewDbgFuncNode->msgFlags = msgFlags;
88 pNewDbgFuncNode->pUserData = pUserData;
89 pNewDbgFuncNode->pNext = inst->DbgFunctionHead;
90 inst->DbgFunctionHead = pNewDbgFuncNode;
91 } else {
92 free(pNewDbgFuncNode);
93 }
Jon Ashburnb40f2562015-05-29 13:15:39 -060094 loader_platform_thread_unlock_mutex(&loader_lock);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060095 return result;
96}
97
98static VkResult debug_report_DbgDestroyMsgCallback(
99 VkInstance instance,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600100 VkDbgMsgCallback msg_callback)
101{
Courtney Goeltzenleuchter8afefb52015-06-08 15:04:02 -0600102 struct loader_instance *inst = loader_instance(instance);
Jon Ashburnb40f2562015-05-29 13:15:39 -0600103 loader_platform_thread_lock_mutex(&loader_lock);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600104 VkLayerDbgFunctionNode *pTrav = inst->DbgFunctionHead;
105 VkLayerDbgFunctionNode *pPrev = pTrav;
106
Courtney Goeltzenleuchter3d8dc1f2015-06-08 15:09:22 -0600107 VkResult result = inst->disp->DbgDestroyMsgCallback(instance, msg_callback);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600108
109 while (pTrav) {
Tony Barbourde4124d2015-07-03 10:33:54 -0600110 if (pTrav->msgCallback.handle == msg_callback.handle) {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600111 pPrev->pNext = pTrav->pNext;
112 if (inst->DbgFunctionHead == pTrav)
113 inst->DbgFunctionHead = pTrav->pNext;
114 free(pTrav);
115 break;
116 }
117 pPrev = pTrav;
118 pTrav = pTrav->pNext;
119 }
120
Jon Ashburnb40f2562015-05-29 13:15:39 -0600121 loader_platform_thread_unlock_mutex(&loader_lock);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600122 return result;
123}
124
125
126/*
127 * This is the instance chain terminator function
128 * for DbgCreateMsgCallback
129 */
Tony Barbourde4124d2015-07-03 10:33:54 -0600130
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600131VkResult loader_DbgCreateMsgCallback(
132 VkInstance instance,
133 VkFlags msgFlags,
134 const PFN_vkDbgMsgCallback pfnMsgCallback,
135 const void* pUserData,
136 VkDbgMsgCallback* pMsgCallback)
137{
138 VkDbgMsgCallback *icd_info;
139 const struct loader_icd *icd;
140 struct loader_instance *inst;
141 VkResult res;
142 uint32_t storage_idx;
143
144 if (instance == VK_NULL_HANDLE)
145 return VK_ERROR_INVALID_HANDLE;
146
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600147 for (inst = loader.instances; inst; inst = inst->next) {
148 if ((VkInstance) inst == instance)
149 break;
150 }
151
152 if (inst == VK_NULL_HANDLE)
153 return VK_ERROR_INVALID_HANDLE;
154
155 icd_info = calloc(sizeof(VkDbgMsgCallback), inst->total_icd_count);
156 if (!icd_info) {
157 return VK_ERROR_OUT_OF_HOST_MEMORY;
158 }
159
160 storage_idx = 0;
161 for (icd = inst->icds; icd; icd = icd->next) {
162 if (!icd->DbgCreateMsgCallback) {
163 continue;
164 }
165
166 res = icd->DbgCreateMsgCallback(
167 icd->instance,
168 msgFlags,
169 pfnMsgCallback,
170 pUserData,
171 &icd_info[storage_idx]);
172
173 if (res != VK_SUCCESS) {
174 break;
175 }
176 storage_idx++;
177 }
178
179 /* roll back on errors */
180 if (icd) {
181 storage_idx = 0;
182 for (icd = inst->icds; icd; icd = icd->next) {
Tony Barbourde4124d2015-07-03 10:33:54 -0600183 if (icd_info[storage_idx].handle) {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600184 icd->DbgDestroyMsgCallback(
185 icd->instance,
186 icd_info[storage_idx]);
187 }
188 storage_idx++;
189 }
190
191 return res;
192 }
193
Tony Barbourde4124d2015-07-03 10:33:54 -0600194 *(VkDbgMsgCallback **)pMsgCallback = icd_info;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600195
196 return VK_SUCCESS;
197}
198
199/*
200 * This is the instance chain terminator function
201 * for DbgDestroyMsgCallback
202 */
203VkResult loader_DbgDestroyMsgCallback(
204 VkInstance instance,
205 VkDbgMsgCallback msgCallback)
206{
207 uint32_t storage_idx;
208 VkDbgMsgCallback *icd_info;
209 const struct loader_icd *icd;
210 VkResult res = VK_SUCCESS;
211 struct loader_instance *inst;
212
213 if (instance == VK_NULL_HANDLE)
214 return VK_ERROR_INVALID_HANDLE;
215
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600216 for (inst = loader.instances; inst; inst = inst->next) {
217 if ((VkInstance) inst == instance)
218 break;
219 }
220
221 if (inst == VK_NULL_HANDLE)
222 return VK_ERROR_INVALID_HANDLE;
223
Tony Barbourde4124d2015-07-03 10:33:54 -0600224 icd_info = *(VkDbgMsgCallback **) &msgCallback;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600225 storage_idx = 0;
226 for (icd = inst->icds; icd; icd = icd->next) {
Tony Barbourde4124d2015-07-03 10:33:54 -0600227 if (icd_info[storage_idx].handle) {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600228 icd->DbgDestroyMsgCallback(
Courtney Goeltzenleuchter3d8dc1f2015-06-08 15:09:22 -0600229 icd->instance,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600230 icd_info[storage_idx]);
231 }
232 storage_idx++;
233 }
234 return res;
235}
236
Courtney Goeltzenleuchterf1eb2492015-06-11 16:01:11 -0600237static void print_msg_flags(VkFlags msgFlags, char *msg_flags)
238{
239 bool separator = false;
240
241 msg_flags[0] = 0;
242 if (msgFlags & VK_DBG_REPORT_DEBUG_BIT) {
243 strcat(msg_flags, "DEBUG");
244 separator = true;
245 }
246 if (msgFlags & VK_DBG_REPORT_INFO_BIT) {
247 if (separator) strcat(msg_flags, ",");
248 strcat(msg_flags, "INFO");
249 separator = true;
250 }
251 if (msgFlags & VK_DBG_REPORT_WARN_BIT) {
252 if (separator) strcat(msg_flags, ",");
253 strcat(msg_flags, "WARN");
254 separator = true;
255 }
256 if (msgFlags & VK_DBG_REPORT_PERF_WARN_BIT) {
257 if (separator) strcat(msg_flags, ",");
258 strcat(msg_flags, "PERF");
259 separator = true;
260 }
261 if (msgFlags & VK_DBG_REPORT_ERROR_BIT) {
262 if (separator) strcat(msg_flags, ",");
263 strcat(msg_flags, "ERROR");
264 }
265}
266
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600267// DebugReport utility callback functions
268static void VKAPI StringCallback(
269 VkFlags msgFlags,
Tony Barbourde4124d2015-07-03 10:33:54 -0600270 VkDbgObjectType objType,
271 uint64_t srcObject,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600272 size_t location,
273 int32_t msgCode,
274 const char* pLayerPrefix,
275 const char* pMsg,
276 void* pUserData)
277{
Jon Ashburncab27b32015-06-30 14:44:13 -0700278 size_t buf_size;
Courtney Goeltzenleuchterf1eb2492015-06-11 16:01:11 -0600279 char *buf;
280 char msg_flags[30];
281 PFN_stringCallback callback = (PFN_stringCallback) pUserData;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600282
Courtney Goeltzenleuchterf1eb2492015-06-11 16:01:11 -0600283 print_msg_flags(msgFlags, msg_flags);
284
285 buf_size = strlen(msg_flags) + /* ReportFlags: i.e. (DEBUG,INFO,WARN,PERF,ERROR) */
286 20 + /* objType */
287 20 + /* srcObject */
288 20 + /* location */
289 20 + /* msgCode */
290 strlen(pLayerPrefix) +
291 strlen(pMsg) +
292 50 /* other / whitespace */;
Tony Barbour69698512015-06-18 16:29:32 -0600293#ifdef WIN32
294 buf = _alloca(buf_size);
295#else
Courtney Goeltzenleuchterf1eb2492015-06-11 16:01:11 -0600296 buf = alloca(buf_size);
Tony Barbour69698512015-06-18 16:29:32 -0600297#endif
Courtney Goeltzenleuchterf1eb2492015-06-11 16:01:11 -0600298 snprintf(buf, buf_size, "%s (%s): object: 0x%" PRIxLEAST64 " type: %d location: %zu msgCode: %d: %s",
299 pLayerPrefix, msg_flags, srcObject, objType, location, msgCode, pMsg);
300 callback(buf);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600301}
302
303static void VKAPI StdioCallback(
304 VkFlags msgFlags,
Tony Barbourde4124d2015-07-03 10:33:54 -0600305 VkDbgObjectType objType,
306 uint64_t srcObject,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600307 size_t location,
308 int32_t msgCode,
309 const char* pLayerPrefix,
310 const char* pMsg,
311 void* pUserData)
312{
Courtney Goeltzenleuchterf1eb2492015-06-11 16:01:11 -0600313 char msg_flags[30];
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600314
Courtney Goeltzenleuchterf1eb2492015-06-11 16:01:11 -0600315 print_msg_flags(msgFlags, msg_flags);
316
317 fprintf((FILE *) pUserData, "%s(%s): object: 0x%" PRIxLEAST64 " type: %d location: %zu msgCode: %d: %s",
318 pLayerPrefix, msg_flags, srcObject, objType, location, msgCode, pMsg);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600319}
320
321static void VKAPI BreakCallback(
322 VkFlags msgFlags,
Tony Barbourde4124d2015-07-03 10:33:54 -0600323 VkDbgObjectType objType,
324 uint64_t srcObject,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600325 size_t location,
326 int32_t msgCode,
327 const char* pLayerPrefix,
328 const char* pMsg,
329 void* pUserData)
330{
331
332}
333
334void *debug_report_instance_gpa(
335 struct loader_instance *ptr_instance,
336 const char* name)
337{
338 if (ptr_instance == VK_NULL_HANDLE || !ptr_instance->debug_report_enabled)
339 return NULL;
340
341 if (!strcmp("vkDbgCreateMsgCallback", name))
342 return (void *) debug_report_DbgCreateMsgCallback;
343 else if (!strcmp("vkDbgDestroyMsgCallback", name))
344 return (void *) debug_report_DbgDestroyMsgCallback;
Jon Ashburnb843f0b2015-05-26 13:56:43 -0600345 else if (!strcmp("vkDbgStringCallback", name))
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600346 return (void *) StringCallback;
Jon Ashburnb843f0b2015-05-26 13:56:43 -0600347 else if (!strcmp("vkDbgStdioCallback", name))
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600348 return (void *) StdioCallback;
Jon Ashburnb843f0b2015-05-26 13:56:43 -0600349 else if (!strcmp("vkDbgBreakCallback", name))
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600350 return (void *) BreakCallback;
351
352 return NULL;
353}