blob: 7a565645227e2c33052482067d252af08204b7d7 [file] [log] [blame]
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -07001/* Copyright (c) 2015-2016 The Khronos Group Inc.
2 * Copyright (c) 2015-2016 Valve Corporation
3 * Copyright (c) 2015-2016 LunarG, Inc.
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -06004 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -06005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -06008 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -06009 * http://www.apache.org/licenses/LICENSE-2.0
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060010 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060016 *
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060017 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
18 * Author: Tobin Ehlis <tobin@lunarg.com>
19 *
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060020 */
21
22#ifndef LAYER_LOGGING_H
23#define LAYER_LOGGING_H
24
Mark Lobodzinskib87f9022016-05-24 16:04:56 -060025#include "vk_loader_layer.h"
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060026#include "vk_layer_config.h"
Tobin Ehlisa0cb02e2015-07-03 10:15:26 -060027#include "vk_layer_data.h"
28#include "vk_layer_table.h"
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060029#include "vk_loader_platform.h"
30#include "vulkan/vk_layer.h"
Mark Lobodzinskic9d81652017-08-10 11:01:17 -060031#include <signal.h>
Karl Schultzd7f37542016-05-10 11:36:08 -060032#include <cinttypes>
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060033#include <stdarg.h>
34#include <stdbool.h>
35#include <stdio.h>
36#include <unordered_map>
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060037#include <vector>
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060038
Mark Lobodzinskic9d81652017-08-10 11:01:17 -060039
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -060040typedef struct _debug_report_data {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060041 VkLayerDbgFunctionNode *debug_callback_list;
42 VkLayerDbgFunctionNode *default_debug_callback_list;
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -060043 VkFlags active_flags;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060044 bool g_DEBUG_REPORT;
Tony Barbour3431dac2017-06-19 16:50:37 -060045 std::unordered_map<uint64_t, std::string> *debugObjectNameMap;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -060046} debug_report_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060047
Tobin Ehlis8d6acde2017-02-08 07:40:40 -070048template debug_report_data *GetLayerDataPtr<debug_report_data>(void *data_key,
Jon Ashburn5484e0c2016-03-08 17:48:44 -070049 std::unordered_map<void *, debug_report_data *> &data_map);
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060050
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060051// Forward Declarations
52static inline bool debug_report_log_msg(const debug_report_data *debug_data, VkFlags msgFlags,
53 VkDebugReportObjectTypeEXT objectType, uint64_t srcObject, size_t location, int32_t msgCode,
54 const char *pLayerPrefix, const char *pMsg);
55
56// Add a debug message callback node structure to the specified callback linked list
57static inline void AddDebugMessageCallback(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head,
58 VkLayerDbgFunctionNode *new_node) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060059 new_node->pNext = *list_head;
60 *list_head = new_node;
61}
62
63// Remove specified debug message callback node structure from the specified callback linked list
64static inline void RemoveDebugMessageCallback(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head,
65 VkDebugReportCallbackEXT callback) {
66 VkLayerDbgFunctionNode *cur_callback = *list_head;
67 VkLayerDbgFunctionNode *prev_callback = cur_callback;
68 bool matched = false;
Mark Lobodzinski05b04cd2016-11-10 09:12:57 -070069 VkFlags local_flags = 0;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060070
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060071 while (cur_callback) {
72 if (cur_callback->msgCallback == callback) {
73 matched = true;
74 prev_callback->pNext = cur_callback->pNext;
75 if (*list_head == cur_callback) {
76 *list_head = cur_callback->pNext;
77 }
78 debug_report_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
Mark Lobodzinski863d5de2017-05-22 10:10:07 -060079 reinterpret_cast<uint64_t &>(cur_callback->msgCallback), 0, 0, "DebugReport",
80 "Destroyed callback\n");
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060081 } else {
82 matched = false;
Mark Lobodzinski05b04cd2016-11-10 09:12:57 -070083 local_flags |= cur_callback->msgFlags;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060084 }
85 prev_callback = cur_callback;
86 cur_callback = cur_callback->pNext;
87 if (matched) {
88 free(prev_callback);
89 }
90 }
Mark Lobodzinski05b04cd2016-11-10 09:12:57 -070091 debug_data->active_flags = local_flags;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060092}
93
94// Removes all debug callback function nodes from the specified callback linked lists and frees their resources
95static inline void RemoveAllMessageCallbacks(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head) {
96 VkLayerDbgFunctionNode *current_callback = *list_head;
97 VkLayerDbgFunctionNode *prev_callback = current_callback;
98
99 while (current_callback) {
100 prev_callback = current_callback->pNext;
101 debug_report_log_msg(debug_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
Mark Lobodzinski863d5de2017-05-22 10:10:07 -0600102 (uint64_t)current_callback->msgCallback, 0, 0, "DebugReport",
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600103 "Debug Report callbacks not removed before DestroyInstance");
104 free(current_callback);
105 current_callback = prev_callback;
106 }
107 *list_head = NULL;
108}
109
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600110// Utility function to handle reporting
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600111static inline bool debug_report_log_msg(const debug_report_data *debug_data, VkFlags msgFlags,
112 VkDebugReportObjectTypeEXT objectType, uint64_t srcObject, size_t location, int32_t msgCode,
113 const char *pLayerPrefix, const char *pMsg) {
Dustin Graves8f1eab92016-04-05 09:41:17 -0600114 bool bail = false;
Mark Lobodzinski55a197f2016-06-21 15:54:57 -0600115 VkLayerDbgFunctionNode *pTrav = NULL;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600116
Mark Lobodzinski55a197f2016-06-21 15:54:57 -0600117 if (debug_data->debug_callback_list != NULL) {
118 pTrav = debug_data->debug_callback_list;
119 } else {
120 pTrav = debug_data->default_debug_callback_list;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600121 }
122
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600123 while (pTrav) {
124 if (pTrav->msgFlags & msgFlags) {
Tony Barbour3431dac2017-06-19 16:50:37 -0600125 auto it = debug_data->debugObjectNameMap->find(srcObject);
126 if (it == debug_data->debugObjectNameMap->end()) {
127 if (pTrav->pfnMsgCallback(msgFlags, objectType, srcObject, location, msgCode, pLayerPrefix, pMsg,
128 pTrav->pUserData)) {
129 bail = true;
130 }
131 } else {
132 std::string newMsg = "SrcObject name = ";
133 newMsg.append(it->second.c_str());
134 newMsg.append(" ");
135 newMsg.append(pMsg);
136 if (pTrav->pfnMsgCallback(msgFlags, objectType, srcObject, location, msgCode, pLayerPrefix, newMsg.c_str(),
137 pTrav->pUserData)) {
138 bail = true;
139 }
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600140 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600141 }
142 pTrav = pTrav->pNext;
143 }
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600144
145 return bail;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600146}
147
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700148static inline debug_report_data *debug_report_create_instance(
149 VkLayerInstanceDispatchTable *table, VkInstance inst, uint32_t extension_count,
150 const char *const *ppEnabledExtensions) // layer or extension name to be enabled
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600151{
Mark Youngaa1aa3a2016-07-05 16:41:50 -0600152 debug_report_data *debug_data = (debug_report_data *)malloc(sizeof(debug_report_data));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700153 if (!debug_data) return NULL;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600154
155 memset(debug_data, 0, sizeof(debug_report_data));
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600156 for (uint32_t i = 0; i < extension_count; i++) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600157 // TODO: Check other property fields
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700158 if (strcmp(ppEnabledExtensions[i], VK_EXT_DEBUG_REPORT_EXTENSION_NAME) == 0) {
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600159 debug_data->g_DEBUG_REPORT = true;
160 }
161 }
Tony Barbour3431dac2017-06-19 16:50:37 -0600162 debug_data->debugObjectNameMap = new std::unordered_map<uint64_t, std::string>;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600163 return debug_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600164}
165
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700166static inline void layer_debug_report_destroy_instance(debug_report_data *debug_data) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600167 if (debug_data) {
168 RemoveAllMessageCallbacks(debug_data, &debug_data->default_debug_callback_list);
169 RemoveAllMessageCallbacks(debug_data, &debug_data->debug_callback_list);
Tony Barbour3431dac2017-06-19 16:50:37 -0600170 delete debug_data->debugObjectNameMap;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600171 free(debug_data);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600172 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600173}
174
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700175static inline debug_report_data *layer_debug_report_create_device(debug_report_data *instance_debug_data, VkDevice device) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600176 // DEBUG_REPORT shares data between Instance and Device,
177 // so just return instance's data pointer
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600178 return instance_debug_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600179}
180
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600181static inline void layer_debug_report_destroy_device(VkDevice device) {
182 // Nothing to do since we're using instance data record
183}
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600184
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600185static inline void layer_destroy_msg_callback(debug_report_data *debug_data, VkDebugReportCallbackEXT callback,
186 const VkAllocationCallbacks *pAllocator) {
187 RemoveDebugMessageCallback(debug_data, &debug_data->debug_callback_list, callback);
188 RemoveDebugMessageCallback(debug_data, &debug_data->default_debug_callback_list, callback);
189}
190
191static inline VkResult layer_create_msg_callback(debug_report_data *debug_data, bool default_callback,
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700192 const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,
193 const VkAllocationCallbacks *pAllocator, VkDebugReportCallbackEXT *pCallback) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700194 VkLayerDbgFunctionNode *pNewDbgFuncNode = (VkLayerDbgFunctionNode *)malloc(sizeof(VkLayerDbgFunctionNode));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700195 if (!pNewDbgFuncNode) return VK_ERROR_OUT_OF_HOST_MEMORY;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600196
Tobin Ehliseb7715d2015-09-21 09:36:47 -0600197 // Handle of 0 is logging_callback so use allocated Node address as unique handle
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700198 if (!(*pCallback)) *pCallback = (VkDebugReportCallbackEXT)pNewDbgFuncNode;
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700199 pNewDbgFuncNode->msgCallback = *pCallback;
200 pNewDbgFuncNode->pfnMsgCallback = pCreateInfo->pfnCallback;
201 pNewDbgFuncNode->msgFlags = pCreateInfo->flags;
202 pNewDbgFuncNode->pUserData = pCreateInfo->pUserData;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600203
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600204 if (default_callback) {
205 AddDebugMessageCallback(debug_data, &debug_data->default_debug_callback_list, pNewDbgFuncNode);
Mark Lobodzinski2134fc32016-11-10 09:10:08 -0700206 debug_data->active_flags |= pCreateInfo->flags;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600207 } else {
208 AddDebugMessageCallback(debug_data, &debug_data->debug_callback_list, pNewDbgFuncNode);
Mark Lobodzinski2134fc32016-11-10 09:10:08 -0700209 debug_data->active_flags = pCreateInfo->flags;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600210 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600211
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700212 debug_report_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
Mark Lobodzinski863d5de2017-05-22 10:10:07 -0600213 (uint64_t)*pCallback, 0, 0, "DebugReport", "Added callback");
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600214 return VK_SUCCESS;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600215}
216
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700217static inline PFN_vkVoidFunction debug_report_get_instance_proc_addr(debug_report_data *debug_data, const char *funcName) {
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600218 if (!debug_data || !debug_data->g_DEBUG_REPORT) {
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600219 return NULL;
220 }
221
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700222 if (!strcmp(funcName, "vkCreateDebugReportCallbackEXT")) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700223 return (PFN_vkVoidFunction)vkCreateDebugReportCallbackEXT;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600224 }
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700225 if (!strcmp(funcName, "vkDestroyDebugReportCallbackEXT")) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700226 return (PFN_vkVoidFunction)vkDestroyDebugReportCallbackEXT;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600227 }
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700228 if (!strcmp(funcName, "vkDebugReportMessageEXT")) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700229 return (PFN_vkVoidFunction)vkDebugReportMessageEXT;
Courtney Goeltzenleuchter822e8d72015-11-30 15:28:25 -0700230 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600231 return NULL;
232}
233
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600234// This utility (called at vkCreateInstance() time), looks at a pNext chain.
235// It counts any VkDebugReportCallbackCreateInfoEXT structs that it finds. It
236// then allocates an array that can hold that many structs, as well as that
237// many VkDebugReportCallbackEXT handles. It then copies each
238// VkDebugReportCallbackCreateInfoEXT, and initializes each handle.
239static VkResult layer_copy_tmp_callbacks(const void *pChain, uint32_t *num_callbacks, VkDebugReportCallbackCreateInfoEXT **infos,
240 VkDebugReportCallbackEXT **callbacks) {
241 uint32_t n = *num_callbacks = 0;
242
243 const void *pNext = pChain;
244 while (pNext) {
245 // 1st, count the number VkDebugReportCallbackCreateInfoEXT:
246 if (((VkDebugReportCallbackCreateInfoEXT *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT) {
247 n++;
248 }
249 pNext = (void *)((VkDebugReportCallbackCreateInfoEXT *)pNext)->pNext;
250 }
251 if (n == 0) {
252 return VK_SUCCESS;
253 }
254
255 // 2nd, allocate memory for each VkDebugReportCallbackCreateInfoEXT:
256 VkDebugReportCallbackCreateInfoEXT *pInfos = *infos =
257 ((VkDebugReportCallbackCreateInfoEXT *)malloc(n * sizeof(VkDebugReportCallbackCreateInfoEXT)));
258 if (!pInfos) {
259 return VK_ERROR_OUT_OF_HOST_MEMORY;
260 }
261 // 3rd, allocate memory for a unique handle for each callback:
262 VkDebugReportCallbackEXT *pCallbacks = *callbacks = ((VkDebugReportCallbackEXT *)malloc(n * sizeof(VkDebugReportCallbackEXT)));
263 if (!pCallbacks) {
264 free(pInfos);
265 return VK_ERROR_OUT_OF_HOST_MEMORY;
266 }
267 // 4th, copy each VkDebugReportCallbackCreateInfoEXT for use by
268 // vkDestroyInstance, and assign a unique handle to each callback (just
269 // use the address of the copied VkDebugReportCallbackCreateInfoEXT):
270 pNext = pChain;
271 while (pNext) {
272 if (((VkInstanceCreateInfo *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT) {
273 memcpy(pInfos, pNext, sizeof(VkDebugReportCallbackCreateInfoEXT));
274 *pCallbacks++ = (VkDebugReportCallbackEXT)pInfos++;
275 }
276 pNext = (void *)((VkInstanceCreateInfo *)pNext)->pNext;
277 }
278
279 *num_callbacks = n;
280 return VK_SUCCESS;
281}
282
283// This utility frees the arrays allocated by layer_copy_tmp_callbacks()
284static void layer_free_tmp_callbacks(VkDebugReportCallbackCreateInfoEXT *infos, VkDebugReportCallbackEXT *callbacks) {
285 free(infos);
286 free(callbacks);
287}
288
289// This utility enables all of the VkDebugReportCallbackCreateInfoEXT structs
290// that were copied by layer_copy_tmp_callbacks()
291static VkResult layer_enable_tmp_callbacks(debug_report_data *debug_data, uint32_t num_callbacks,
292 VkDebugReportCallbackCreateInfoEXT *infos, VkDebugReportCallbackEXT *callbacks) {
293 VkResult rtn = VK_SUCCESS;
294 for (uint32_t i = 0; i < num_callbacks; i++) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600295 rtn = layer_create_msg_callback(debug_data, false, &infos[i], NULL, &callbacks[i]);
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600296 if (rtn != VK_SUCCESS) {
297 for (uint32_t j = 0; j < i; j++) {
298 layer_destroy_msg_callback(debug_data, callbacks[j], NULL);
299 }
300 return rtn;
301 }
302 }
303 return rtn;
304}
305
306// This utility disables all of the VkDebugReportCallbackCreateInfoEXT structs
307// that were copied by layer_copy_tmp_callbacks()
308static void layer_disable_tmp_callbacks(debug_report_data *debug_data, uint32_t num_callbacks,
309 VkDebugReportCallbackEXT *callbacks) {
310 for (uint32_t i = 0; i < num_callbacks; i++) {
311 layer_destroy_msg_callback(debug_data, callbacks[i], NULL);
312 }
313}
314
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600315// Checks if the message will get logged.
316// Allows layer to defer collecting & formating data if the
317// message will be discarded.
Dustin Graves8f1eab92016-04-05 09:41:17 -0600318static inline bool will_log_msg(debug_report_data *debug_data, VkFlags msgFlags) {
Courtney Goeltzenleuchter6a564a12015-09-18 16:30:24 -0600319 if (!debug_data || !(debug_data->active_flags & msgFlags)) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600320 // Message is not wanted
Courtney Goeltzenleuchter6a564a12015-09-18 16:30:24 -0600321 return false;
322 }
323
324 return true;
325}
326
Chris Forbes8a25bce2016-03-24 12:06:35 +1300327#ifdef WIN32
328static inline int vasprintf(char **strp, char const *fmt, va_list ap) {
329 *strp = nullptr;
330 int size = _vscprintf(fmt, ap);
331 if (size >= 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700332 *strp = (char *)malloc(size + 1);
Chris Forbes8a25bce2016-03-24 12:06:35 +1300333 if (!*strp) {
334 return -1;
335 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700336 _vsnprintf(*strp, size + 1, fmt, ap);
Chris Forbes8a25bce2016-03-24 12:06:35 +1300337 }
338 return size;
339}
340#endif
341
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600342// Output log message via DEBUG_REPORT
343// Takes format and variable arg list so that output string
344// is only computed if a message needs to be logged
Michael Lentine010f4692015-11-03 16:19:46 -0800345#ifndef WIN32
Tobin Ehlis17978d82016-05-17 07:58:01 -0600346static inline bool log_msg(const debug_report_data *debug_data, VkFlags msgFlags, VkDebugReportObjectTypeEXT objectType,
Dustin Graves8f1eab92016-04-05 09:41:17 -0600347 uint64_t srcObject, size_t location, int32_t msgCode, const char *pLayerPrefix, const char *format, ...)
348 __attribute__((format(printf, 8, 9)));
Michael Lentine010f4692015-11-03 16:19:46 -0800349#endif
Tobin Ehlis17978d82016-05-17 07:58:01 -0600350static inline bool log_msg(const debug_report_data *debug_data, VkFlags msgFlags, VkDebugReportObjectTypeEXT objectType,
Dustin Graves8f1eab92016-04-05 09:41:17 -0600351 uint64_t srcObject, size_t location, int32_t msgCode, const char *pLayerPrefix, const char *format,
352 ...) {
Courtney Goeltzenleuchter61cb70a2015-06-26 15:14:50 -0600353 if (!debug_data || !(debug_data->active_flags & msgFlags)) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600354 // Message is not wanted
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600355 return false;
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600356 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600357
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600358 va_list argptr;
359 va_start(argptr, format);
Chris Forbes8a25bce2016-03-24 12:06:35 +1300360 char *str;
Chris Forbesed6a1b32016-04-28 14:27:19 +1200361 if (-1 == vasprintf(&str, format, argptr)) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600362 // On failure, glibc vasprintf leaves str undefined
Chris Forbesed6a1b32016-04-28 14:27:19 +1200363 str = nullptr;
364 }
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600365 va_end(argptr);
Chris Forbesed6a1b32016-04-28 14:27:19 +1200366 bool result = debug_report_log_msg(debug_data, msgFlags, objectType, srcObject, location, msgCode, pLayerPrefix,
367 str ? str : "Allocation failure");
Chris Forbes8a25bce2016-03-24 12:06:35 +1300368 free(str);
369 return result;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600370}
371
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700372static inline VKAPI_ATTR VkBool32 VKAPI_CALL log_callback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType, uint64_t srcObject,
373 size_t location, int32_t msgCode, const char *pLayerPrefix,
374 const char *pMsg, void *pUserData) {
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600375 char msg_flags[30];
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600376
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600377 print_msg_flags(msgFlags, msg_flags);
378
Mark Muelleraab36502016-05-03 13:17:29 -0600379 fprintf((FILE *)pUserData, "%s(%s): object: 0x%" PRIx64 " type: %d location: %lu msgCode: %d: %s\n", pLayerPrefix, msg_flags,
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700380 srcObject, objType, (unsigned long)location, msgCode, pMsg);
381 fflush((FILE *)pUserData);
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600382
383 return false;
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600384}
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600385
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700386static inline VKAPI_ATTR VkBool32 VKAPI_CALL win32_debug_output_msg(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
387 uint64_t srcObject, size_t location, int32_t msgCode,
388 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600389#ifdef WIN32
390 char msg_flags[30];
391 char buf[2048];
392
393 print_msg_flags(msgFlags, msg_flags);
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700394 _snprintf(buf, sizeof(buf) - 1,
395 "%s (%s): object: 0x%" PRIxPTR " type: %d location: " PRINTF_SIZE_T_SPECIFIER " msgCode: %d: %s\n", pLayerPrefix,
396 msg_flags, (size_t)srcObject, objType, location, msgCode, pMsg);
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600397
398 OutputDebugString(buf);
399#endif
400
401 return false;
402}
403
Mark Lobodzinskic9d81652017-08-10 11:01:17 -0600404static inline VKAPI_ATTR VkBool32 VKAPI_CALL DebugBreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
405 uint64_t srcObject, size_t location, int32_t msgCode,
406 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
407#ifdef WIN32
408 DebugBreak();
409#else
410 raise(SIGTRAP);
411#endif
412
413 return false;
414}
415
416
Mark Lobodzinski863d5de2017-05-22 10:10:07 -0600417// TODO: Could be autogenerated for the specific handles for extra type safety...
418template <typename HANDLE_T>
419static inline uint64_t HandleToUint64(HANDLE_T *h) {
420 return reinterpret_cast<uint64_t>(h);
Petr Krause9388f62017-05-13 20:53:12 +0200421}
422
Mark Lobodzinski863d5de2017-05-22 10:10:07 -0600423template <typename HANDLE_T>
424uint64_t HandleToUint64(HANDLE_T h);
425
426static inline uint64_t HandleToUint64(uint64_t h) { return h; }
427
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700428#endif // LAYER_LOGGING_H