1 /* Copyright (c) 2015-2016 The Khronos Group Inc.
2 * Copyright (c) 2015-2016 Valve Corporation
3 * Copyright (c) 2015-2016 LunarG, Inc.
5 * 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
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * 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.
17 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
18 * Author: Tobin Ehlis <tobin@lunarg.com>
22 #ifndef LAYER_LOGGING_H
23 #define LAYER_LOGGING_H
25 #include "vk_loader_layer.h"
26 #include "vk_layer_config.h"
27 #include "vk_layer_data.h"
28 #include "vk_layer_table.h"
29 #include "vk_loader_platform.h"
30 #include "vulkan/vk_layer.h"
36 #include <unordered_map>
39 typedef struct _debug_report_data {
40 VkLayerDbgFunctionNode *debug_callback_list;
41 VkLayerDbgFunctionNode *default_debug_callback_list;
44 std::unordered_map<uint64_t, std::string> *debugObjectNameMap;
47 template debug_report_data *GetLayerDataPtr<debug_report_data>(void *data_key,
48 std::unordered_map<void *, debug_report_data *> &data_map);
50 // Forward Declarations
51 static inline bool debug_report_log_msg(const debug_report_data *debug_data, VkFlags msgFlags,
52 VkDebugReportObjectTypeEXT objectType, uint64_t srcObject, size_t location, int32_t msgCode,
53 const char *pLayerPrefix, const char *pMsg);
55 // Add a debug message callback node structure to the specified callback linked list
56 static inline void AddDebugMessageCallback(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head,
57 VkLayerDbgFunctionNode *new_node) {
58 new_node->pNext = *list_head;
59 *list_head = new_node;
62 // Remove specified debug message callback node structure from the specified callback linked list
63 static inline void RemoveDebugMessageCallback(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head,
64 VkDebugReportCallbackEXT callback) {
65 VkLayerDbgFunctionNode *cur_callback = *list_head;
66 VkLayerDbgFunctionNode *prev_callback = cur_callback;
68 VkFlags local_flags = 0;
70 while (cur_callback) {
71 if (cur_callback->msgCallback == callback) {
73 prev_callback->pNext = cur_callback->pNext;
74 if (*list_head == cur_callback) {
75 *list_head = cur_callback->pNext;
77 debug_report_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
78 reinterpret_cast<uint64_t &>(cur_callback->msgCallback), 0, 0, "DebugReport",
79 "Destroyed callback\n");
82 local_flags |= cur_callback->msgFlags;
84 prev_callback = cur_callback;
85 cur_callback = cur_callback->pNext;
90 debug_data->active_flags = local_flags;
93 // Removes all debug callback function nodes from the specified callback linked lists and frees their resources
94 static inline void RemoveAllMessageCallbacks(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head) {
95 VkLayerDbgFunctionNode *current_callback = *list_head;
96 VkLayerDbgFunctionNode *prev_callback = current_callback;
98 while (current_callback) {
99 prev_callback = current_callback->pNext;
100 debug_report_log_msg(debug_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
101 (uint64_t)current_callback->msgCallback, 0, 0, "DebugReport",
102 "Debug Report callbacks not removed before DestroyInstance");
103 free(current_callback);
104 current_callback = prev_callback;
109 // Utility function to handle reporting
110 static inline bool debug_report_log_msg(const debug_report_data *debug_data, VkFlags msgFlags,
111 VkDebugReportObjectTypeEXT objectType, uint64_t srcObject, size_t location, int32_t msgCode,
112 const char *pLayerPrefix, const char *pMsg) {
114 VkLayerDbgFunctionNode *pTrav = NULL;
116 if (debug_data->debug_callback_list != NULL) {
117 pTrav = debug_data->debug_callback_list;
119 pTrav = debug_data->default_debug_callback_list;
123 if (pTrav->msgFlags & msgFlags) {
124 auto it = debug_data->debugObjectNameMap->find(srcObject);
125 if (it == debug_data->debugObjectNameMap->end()) {
126 if (pTrav->pfnMsgCallback(msgFlags, objectType, srcObject, location, msgCode, pLayerPrefix, pMsg,
131 std::string newMsg = "SrcObject name = ";
132 newMsg.append(it->second.c_str());
135 if (pTrav->pfnMsgCallback(msgFlags, objectType, srcObject, location, msgCode, pLayerPrefix, newMsg.c_str(),
141 pTrav = pTrav->pNext;
147 static inline debug_report_data *debug_report_create_instance(
148 VkLayerInstanceDispatchTable *table, VkInstance inst, uint32_t extension_count,
149 const char *const *ppEnabledExtensions) // layer or extension name to be enabled
151 debug_report_data *debug_data = (debug_report_data *)malloc(sizeof(debug_report_data));
152 if (!debug_data) return NULL;
154 memset(debug_data, 0, sizeof(debug_report_data));
155 for (uint32_t i = 0; i < extension_count; i++) {
156 // TODO: Check other property fields
157 if (strcmp(ppEnabledExtensions[i], VK_EXT_DEBUG_REPORT_EXTENSION_NAME) == 0) {
158 debug_data->g_DEBUG_REPORT = true;
161 debug_data->debugObjectNameMap = new std::unordered_map<uint64_t, std::string>;
165 static inline void layer_debug_report_destroy_instance(debug_report_data *debug_data) {
167 RemoveAllMessageCallbacks(debug_data, &debug_data->default_debug_callback_list);
168 RemoveAllMessageCallbacks(debug_data, &debug_data->debug_callback_list);
169 delete debug_data->debugObjectNameMap;
174 static inline debug_report_data *layer_debug_report_create_device(debug_report_data *instance_debug_data, VkDevice device) {
175 // DEBUG_REPORT shares data between Instance and Device,
176 // so just return instance's data pointer
177 return instance_debug_data;
180 static inline void layer_debug_report_destroy_device(VkDevice device) {
181 // Nothing to do since we're using instance data record
184 static inline void layer_destroy_msg_callback(debug_report_data *debug_data, VkDebugReportCallbackEXT callback,
185 const VkAllocationCallbacks *pAllocator) {
186 RemoveDebugMessageCallback(debug_data, &debug_data->debug_callback_list, callback);
187 RemoveDebugMessageCallback(debug_data, &debug_data->default_debug_callback_list, callback);
190 static inline VkResult layer_create_msg_callback(debug_report_data *debug_data, bool default_callback,
191 const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,
192 const VkAllocationCallbacks *pAllocator, VkDebugReportCallbackEXT *pCallback) {
193 VkLayerDbgFunctionNode *pNewDbgFuncNode = (VkLayerDbgFunctionNode *)malloc(sizeof(VkLayerDbgFunctionNode));
194 if (!pNewDbgFuncNode) return VK_ERROR_OUT_OF_HOST_MEMORY;
196 // Handle of 0 is logging_callback so use allocated Node address as unique handle
197 if (!(*pCallback)) *pCallback = (VkDebugReportCallbackEXT)pNewDbgFuncNode;
198 pNewDbgFuncNode->msgCallback = *pCallback;
199 pNewDbgFuncNode->pfnMsgCallback = pCreateInfo->pfnCallback;
200 pNewDbgFuncNode->msgFlags = pCreateInfo->flags;
201 pNewDbgFuncNode->pUserData = pCreateInfo->pUserData;
203 if (default_callback) {
204 AddDebugMessageCallback(debug_data, &debug_data->default_debug_callback_list, pNewDbgFuncNode);
205 debug_data->active_flags |= pCreateInfo->flags;
207 AddDebugMessageCallback(debug_data, &debug_data->debug_callback_list, pNewDbgFuncNode);
208 debug_data->active_flags = pCreateInfo->flags;
211 debug_report_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
212 (uint64_t)*pCallback, 0, 0, "DebugReport", "Added callback");
216 static inline PFN_vkVoidFunction debug_report_get_instance_proc_addr(debug_report_data *debug_data, const char *funcName) {
217 if (!debug_data || !debug_data->g_DEBUG_REPORT) {
221 if (!strcmp(funcName, "vkCreateDebugReportCallbackEXT")) {
222 return (PFN_vkVoidFunction)vkCreateDebugReportCallbackEXT;
224 if (!strcmp(funcName, "vkDestroyDebugReportCallbackEXT")) {
225 return (PFN_vkVoidFunction)vkDestroyDebugReportCallbackEXT;
227 if (!strcmp(funcName, "vkDebugReportMessageEXT")) {
228 return (PFN_vkVoidFunction)vkDebugReportMessageEXT;
233 // This utility (called at vkCreateInstance() time), looks at a pNext chain.
234 // It counts any VkDebugReportCallbackCreateInfoEXT structs that it finds. It
235 // then allocates an array that can hold that many structs, as well as that
236 // many VkDebugReportCallbackEXT handles. It then copies each
237 // VkDebugReportCallbackCreateInfoEXT, and initializes each handle.
238 static inline VkResult layer_copy_tmp_callbacks(const void *pChain, uint32_t *num_callbacks,
239 VkDebugReportCallbackCreateInfoEXT **infos, VkDebugReportCallbackEXT **callbacks) {
240 uint32_t n = *num_callbacks = 0;
242 const void *pNext = pChain;
244 // 1st, count the number VkDebugReportCallbackCreateInfoEXT:
245 if (((VkDebugReportCallbackCreateInfoEXT *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT) {
248 pNext = (void *)((VkDebugReportCallbackCreateInfoEXT *)pNext)->pNext;
254 // 2nd, allocate memory for each VkDebugReportCallbackCreateInfoEXT:
255 VkDebugReportCallbackCreateInfoEXT *pInfos = *infos =
256 ((VkDebugReportCallbackCreateInfoEXT *)malloc(n * sizeof(VkDebugReportCallbackCreateInfoEXT)));
258 return VK_ERROR_OUT_OF_HOST_MEMORY;
260 // 3rd, allocate memory for a unique handle for each callback:
261 VkDebugReportCallbackEXT *pCallbacks = *callbacks = ((VkDebugReportCallbackEXT *)malloc(n * sizeof(VkDebugReportCallbackEXT)));
264 return VK_ERROR_OUT_OF_HOST_MEMORY;
266 // 4th, copy each VkDebugReportCallbackCreateInfoEXT for use by
267 // vkDestroyInstance, and assign a unique handle to each callback (just
268 // use the address of the copied VkDebugReportCallbackCreateInfoEXT):
271 if (((VkInstanceCreateInfo *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT) {
272 memcpy(pInfos, pNext, sizeof(VkDebugReportCallbackCreateInfoEXT));
273 *pCallbacks++ = (VkDebugReportCallbackEXT)pInfos++;
275 pNext = (void *)((VkInstanceCreateInfo *)pNext)->pNext;
282 // This utility frees the arrays allocated by layer_copy_tmp_callbacks()
283 static inline void layer_free_tmp_callbacks(VkDebugReportCallbackCreateInfoEXT *infos, VkDebugReportCallbackEXT *callbacks) {
288 // This utility enables all of the VkDebugReportCallbackCreateInfoEXT structs
289 // that were copied by layer_copy_tmp_callbacks()
290 static inline VkResult layer_enable_tmp_callbacks(debug_report_data *debug_data, uint32_t num_callbacks,
291 VkDebugReportCallbackCreateInfoEXT *infos, VkDebugReportCallbackEXT *callbacks) {
292 VkResult rtn = VK_SUCCESS;
293 for (uint32_t i = 0; i < num_callbacks; i++) {
294 rtn = layer_create_msg_callback(debug_data, false, &infos[i], NULL, &callbacks[i]);
295 if (rtn != VK_SUCCESS) {
296 for (uint32_t j = 0; j < i; j++) {
297 layer_destroy_msg_callback(debug_data, callbacks[j], NULL);
305 // This utility disables all of the VkDebugReportCallbackCreateInfoEXT structs
306 // that were copied by layer_copy_tmp_callbacks()
307 static inline void layer_disable_tmp_callbacks(debug_report_data *debug_data, uint32_t num_callbacks,
308 VkDebugReportCallbackEXT *callbacks) {
309 for (uint32_t i = 0; i < num_callbacks; i++) {
310 layer_destroy_msg_callback(debug_data, callbacks[i], NULL);
314 // Checks if the message will get logged.
315 // Allows layer to defer collecting & formating data if the
316 // message will be discarded.
317 static inline bool will_log_msg(debug_report_data *debug_data, VkFlags msgFlags) {
318 if (!debug_data || !(debug_data->active_flags & msgFlags)) {
319 // Message is not wanted
326 static inline int string_sprintf(std::string *output, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
328 static inline int string_sprintf(std::string *output, const char *fmt, ...) {
329 std::string &formatted = *output;
331 va_start(argptr, fmt);
332 int reserve = vsnprintf(nullptr, 0, fmt, argptr);
334 formatted.reserve(reserve + 1);
335 va_start(argptr, fmt);
336 int result = vsnprintf((char *)formatted.data(), formatted.capacity(), fmt, argptr);
338 assert(result == reserve);
343 static inline int vasprintf(char **strp, char const *fmt, va_list ap) {
345 int size = _vscprintf(fmt, ap);
347 *strp = (char *)malloc(size + 1);
351 _vsnprintf(*strp, size + 1, fmt, ap);
357 // Output log message via DEBUG_REPORT
358 // Takes format and variable arg list so that output string
359 // is only computed if a message needs to be logged
361 static inline bool log_msg(const debug_report_data *debug_data, VkFlags msgFlags, VkDebugReportObjectTypeEXT objectType,
362 uint64_t srcObject, size_t location, int32_t msgCode, const char *pLayerPrefix, const char *format, ...)
363 __attribute__((format(printf, 8, 9)));
365 static inline bool log_msg(const debug_report_data *debug_data, VkFlags msgFlags, VkDebugReportObjectTypeEXT objectType,
366 uint64_t srcObject, size_t location, int32_t msgCode, const char *pLayerPrefix, const char *format,
368 if (!debug_data || !(debug_data->active_flags & msgFlags)) {
369 // Message is not wanted
374 va_start(argptr, format);
376 if (-1 == vasprintf(&str, format, argptr)) {
377 // On failure, glibc vasprintf leaves str undefined
381 bool result = debug_report_log_msg(debug_data, msgFlags, objectType, srcObject, location, msgCode, pLayerPrefix,
382 str ? str : "Allocation failure");
387 static inline VKAPI_ATTR VkBool32 VKAPI_CALL log_callback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType, uint64_t srcObject,
388 size_t location, int32_t msgCode, const char *pLayerPrefix,
389 const char *pMsg, void *pUserData) {
392 print_msg_flags(msgFlags, msg_flags);
394 fprintf((FILE *)pUserData, "%s(%s): object: 0x%" PRIx64 " type: %d location: %lu msgCode: %d: %s\n", pLayerPrefix, msg_flags,
395 srcObject, objType, (unsigned long)location, msgCode, pMsg);
396 fflush((FILE *)pUserData);
401 static inline VKAPI_ATTR VkBool32 VKAPI_CALL win32_debug_output_msg(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
402 uint64_t srcObject, size_t location, int32_t msgCode,
403 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
408 print_msg_flags(msgFlags, msg_flags);
409 _snprintf(buf, sizeof(buf) - 1,
410 "%s (%s): object: 0x%" PRIxPTR " type: %d location: " PRINTF_SIZE_T_SPECIFIER " msgCode: %d: %s\n", pLayerPrefix,
411 msg_flags, (size_t)srcObject, objType, location, msgCode, pMsg);
413 OutputDebugString(buf);
419 static inline VKAPI_ATTR VkBool32 VKAPI_CALL DebugBreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
420 uint64_t srcObject, size_t location, int32_t msgCode,
421 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
432 // TODO: Could be autogenerated for the specific handles for extra type safety...
433 template <typename HANDLE_T>
434 static inline uint64_t HandleToUint64(HANDLE_T *h) {
435 return reinterpret_cast<uint64_t>(h);
438 template <typename HANDLE_T>
439 uint64_t HandleToUint64(HANDLE_T h);
441 static inline uint64_t HandleToUint64(uint64_t h) { return h; }
443 #endif // LAYER_LOGGING_H