blob: 89340e6b6e75fbee10140896fc11faa0c5ac09e6 [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 Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060045 .version = VK_DEBUG_REPORT_EXTENSION_VERSION,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -060046 .specVersion = VK_API_VERSION,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060047 },
48 .origin = VK_EXTENSION_ORIGIN_LOADER,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060049};
50
51void debug_report_add_instance_extensions(
52 struct loader_extension_list *ext_list)
53{
54 loader_add_to_ext_list(ext_list, 1, &debug_report_extension_info);
55}
56
57void debug_report_create_instance(
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -060058 struct loader_instance *ptr_instance,
59 const VkInstanceCreateInfo *pCreateInfo)
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060060{
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -060061 ptr_instance->debug_report_enabled = false;
62
63 for (uint32_t i = 0; i < pCreateInfo->extensionCount; i++) {
64 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], DEBUG_REPORT_EXTENSION_NAME) == 0) {
65 ptr_instance->debug_report_enabled = true;
66 return;
67 }
68 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060069}
70
71static VkResult debug_report_DbgCreateMsgCallback(
72 VkInstance instance,
73 VkFlags msgFlags,
74 const PFN_vkDbgMsgCallback pfnMsgCallback,
75 void* pUserData,
76 VkDbgMsgCallback* pMsgCallback)
77{
78 VkLayerDbgFunctionNode *pNewDbgFuncNode = (VkLayerDbgFunctionNode *) malloc(sizeof(VkLayerDbgFunctionNode));
79 if (!pNewDbgFuncNode)
80 return VK_ERROR_OUT_OF_HOST_MEMORY;
81
Courtney Goeltzenleuchter8afefb52015-06-08 15:04:02 -060082 struct loader_instance *inst = loader_instance(instance);
Jon Ashburnb40f2562015-05-29 13:15:39 -060083 loader_platform_thread_lock_mutex(&loader_lock);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060084 VkResult result = inst->disp->DbgCreateMsgCallback(instance, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);
85 if (result == VK_SUCCESS) {
86 pNewDbgFuncNode->msgCallback = *pMsgCallback;
87 pNewDbgFuncNode->pfnMsgCallback = pfnMsgCallback;
88 pNewDbgFuncNode->msgFlags = msgFlags;
89 pNewDbgFuncNode->pUserData = pUserData;
90 pNewDbgFuncNode->pNext = inst->DbgFunctionHead;
91 inst->DbgFunctionHead = pNewDbgFuncNode;
92 } else {
93 free(pNewDbgFuncNode);
94 }
Jon Ashburnb40f2562015-05-29 13:15:39 -060095 loader_platform_thread_unlock_mutex(&loader_lock);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060096 return result;
97}
98
99static VkResult debug_report_DbgDestroyMsgCallback(
100 VkInstance instance,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600101 VkDbgMsgCallback msg_callback)
102{
Courtney Goeltzenleuchter8afefb52015-06-08 15:04:02 -0600103 struct loader_instance *inst = loader_instance(instance);
Jon Ashburnb40f2562015-05-29 13:15:39 -0600104 loader_platform_thread_lock_mutex(&loader_lock);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600105 VkLayerDbgFunctionNode *pTrav = inst->DbgFunctionHead;
106 VkLayerDbgFunctionNode *pPrev = pTrav;
107
Courtney Goeltzenleuchter3d8dc1f2015-06-08 15:09:22 -0600108 VkResult result = inst->disp->DbgDestroyMsgCallback(instance, msg_callback);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600109
110 while (pTrav) {
111 if (pTrav->msgCallback == msg_callback) {
112 pPrev->pNext = pTrav->pNext;
113 if (inst->DbgFunctionHead == pTrav)
114 inst->DbgFunctionHead = pTrav->pNext;
115 free(pTrav);
116 break;
117 }
118 pPrev = pTrav;
119 pTrav = pTrav->pNext;
120 }
121
Jon Ashburnb40f2562015-05-29 13:15:39 -0600122 loader_platform_thread_unlock_mutex(&loader_lock);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600123 return result;
124}
125
126
127/*
128 * This is the instance chain terminator function
129 * for DbgCreateMsgCallback
130 */
131VkResult 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) {
183 if (icd_info[storage_idx]) {
184 icd->DbgDestroyMsgCallback(
185 icd->instance,
186 icd_info[storage_idx]);
187 }
188 storage_idx++;
189 }
190
191 return res;
192 }
193
194 *pMsgCallback = (VkDbgMsgCallback) icd_info;
195
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
224 icd_info = (VkDbgMsgCallback *) msgCallback;
225 storage_idx = 0;
226 for (icd = inst->icds; icd; icd = icd->next) {
227 if (icd_info[storage_idx]) {
228 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,
270 VkObjectType objType,
271 VkObject srcObject,
272 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,
305 VkObjectType objType,
306 VkObject srcObject,
307 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,
323 VkObjectType objType,
324 VkObject srcObject,
325 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}