blob: 28363133a8191c67180afc1d42742677cb797495 [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"
Karl Schultzd7f37542016-05-10 11:36:08 -060031#include <cinttypes>
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060032#include <stdarg.h>
33#include <stdbool.h>
34#include <stdio.h>
35#include <unordered_map>
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060036#include <vector>
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060037
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -060038typedef struct _debug_report_data {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060039 VkLayerDbgFunctionNode *debug_callback_list;
40 VkLayerDbgFunctionNode *default_debug_callback_list;
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -060041 VkFlags active_flags;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060042 bool g_DEBUG_REPORT;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -060043} debug_report_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060044
Jon Ashburn5484e0c2016-03-08 17:48:44 -070045template debug_report_data *get_my_data_ptr<debug_report_data>(void *data_key,
46 std::unordered_map<void *, debug_report_data *> &data_map);
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060047
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060048// Forward Declarations
49static inline bool debug_report_log_msg(const debug_report_data *debug_data, VkFlags msgFlags,
50 VkDebugReportObjectTypeEXT objectType, uint64_t srcObject, size_t location, int32_t msgCode,
51 const char *pLayerPrefix, const char *pMsg);
52
53// Add a debug message callback node structure to the specified callback linked list
54static inline void AddDebugMessageCallback(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head,
55 VkLayerDbgFunctionNode *new_node) {
56
57 new_node->pNext = *list_head;
58 *list_head = new_node;
59}
60
61// Remove specified debug message callback node structure from the specified callback linked list
62static inline void RemoveDebugMessageCallback(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head,
63 VkDebugReportCallbackEXT callback) {
64 VkLayerDbgFunctionNode *cur_callback = *list_head;
65 VkLayerDbgFunctionNode *prev_callback = cur_callback;
66 bool matched = false;
67
68 debug_data->active_flags = 0;
69 while (cur_callback) {
70 if (cur_callback->msgCallback == callback) {
71 matched = true;
72 prev_callback->pNext = cur_callback->pNext;
73 if (*list_head == cur_callback) {
74 *list_head = cur_callback->pNext;
75 }
76 debug_report_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
77 reinterpret_cast<uint64_t &>(cur_callback->msgCallback), 0, VK_DEBUG_REPORT_ERROR_CALLBACK_REF_EXT,
78 "DebugReport", "Destroyed callback");
79 } else {
80 matched = false;
81 debug_data->active_flags |= cur_callback->msgFlags;
82 }
83 prev_callback = cur_callback;
84 cur_callback = cur_callback->pNext;
85 if (matched) {
86 free(prev_callback);
87 }
88 }
89}
90
91// Removes all debug callback function nodes from the specified callback linked lists and frees their resources
92static inline void RemoveAllMessageCallbacks(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head) {
93 VkLayerDbgFunctionNode *current_callback = *list_head;
94 VkLayerDbgFunctionNode *prev_callback = current_callback;
95
96 while (current_callback) {
97 prev_callback = current_callback->pNext;
98 debug_report_log_msg(debug_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
99 (uint64_t)current_callback->msgCallback, 0, VK_DEBUG_REPORT_ERROR_CALLBACK_REF_EXT, "DebugReport",
100 "Debug Report callbacks not removed before DestroyInstance");
101 free(current_callback);
102 current_callback = prev_callback;
103 }
104 *list_head = NULL;
105}
106
Mark Lobodzinskifd027d02016-06-08 12:44:54 -0600107static inline bool OutputLogMsgHeader(const debug_report_data *debug_data, VkFlags msgFlags) {
108 bool output = false;
109 VkLayerDbgFunctionNode *pTrav =
110 (debug_data->debug_callback_list != NULL) ? debug_data->debug_callback_list : debug_data->default_debug_callback_list;
111
112 while (pTrav) {
113 const char *message_header = "Legend:\n\n"
114 "*******************************************************************************************\n"
115 "* Vulkan Validation Layer Debug Output *\n"
116 "* *\n"
117 "* Debug Output Type Definitions: *\n"
118 "* ------------------------------ *\n"
119 "* ERROR: Errors are output when a validation layer detects that some application behavior *\n"
120 "* has violated the Vulkan Specification. When an error is encountered it is *\n"
121 "* recommended that the user callback function return 'true' for optimal *\n"
122 "* validation results. Any validation error may result in undefined behavior and *\n"
123 "* errors should be corrected as they are encountered for best results. *\n"
124 "* WARN: Warnings are output in cases where mistakes are commonly made and do NOT *\n"
125 "* necessarily indicate that an app has violated the Vulkan Specification. *\n"
126 "* Warnings basically translate to 'Did you really mean to do this?' *\n"
127 "* PERF: Performance Warnings are output in cases where a possible inefficiency has been *\n"
128 "* detected. These also do NOT imply that the specification was violated. *\n"
129 "* INFO: These log messages are for informational purposes only. For instance, the *\n"
130 "* core_validation layer can print out lists of memory objects and their bindings *\n"
131 "* which may help with debugging or improving application efficiency. *\n"
132 "*******************************************************************************************\n";
133
134 if (pTrav->msgFlags & msgFlags) {
135 pTrav->pfnMsgCallback(VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT, 0, 0,
136 VK_DEBUG_REPORT_ERROR_CALLBACK_REF_EXT, "", message_header, pTrav->pUserData);
137 output |= true;
138 }
139 pTrav = pTrav->pNext;
140 }
141 return output;
142}
143
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600144// Utility function to handle reporting
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600145static inline bool debug_report_log_msg(const debug_report_data *debug_data, VkFlags msgFlags,
146 VkDebugReportObjectTypeEXT objectType, uint64_t srcObject, size_t location, int32_t msgCode,
147 const char *pLayerPrefix, const char *pMsg) {
Dustin Graves8f1eab92016-04-05 09:41:17 -0600148 bool bail = false;
Mark Lobodzinskifd027d02016-06-08 12:44:54 -0600149 VkLayerDbgFunctionNode *pTrav =
150 (debug_data->debug_callback_list != NULL) ? debug_data->debug_callback_list : debug_data->default_debug_callback_list;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600151
Mark Lobodzinskifd027d02016-06-08 12:44:54 -0600152 if ((LogMessageInitialized() == false) && (strcmp(pLayerPrefix, "DebugReport") != 0)) {
153 if (OutputLogMsgHeader(debug_data, msgFlags) == true) {
154 SetLogMessageInitialized();
155 }
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600156 }
157
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600158 while (pTrav) {
159 if (pTrav->msgFlags & msgFlags) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700160 if (pTrav->pfnMsgCallback(msgFlags, objectType, srcObject, location, msgCode, pLayerPrefix, pMsg, pTrav->pUserData)) {
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600161 bail = true;
162 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600163 }
164 pTrav = pTrav->pNext;
165 }
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600166
167 return bail;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600168}
169
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700170static inline debug_report_data *
171debug_report_create_instance(VkLayerInstanceDispatchTable *table, VkInstance inst, uint32_t extension_count,
172 const char *const *ppEnabledExtensions) // layer or extension name to be enabled
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600173{
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700174 debug_report_data *debug_data;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600175 PFN_vkGetInstanceProcAddr gpa = table->GetInstanceProcAddr;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600176
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700177 table->CreateDebugReportCallbackEXT = (PFN_vkCreateDebugReportCallbackEXT)gpa(inst, "vkCreateDebugReportCallbackEXT");
178 table->DestroyDebugReportCallbackEXT = (PFN_vkDestroyDebugReportCallbackEXT)gpa(inst, "vkDestroyDebugReportCallbackEXT");
179 table->DebugReportMessageEXT = (PFN_vkDebugReportMessageEXT)gpa(inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600180
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700181 debug_data = (debug_report_data *)malloc(sizeof(debug_report_data));
182 if (!debug_data)
183 return NULL;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600184
185 memset(debug_data, 0, sizeof(debug_report_data));
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600186 for (uint32_t i = 0; i < extension_count; i++) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600187 // TODO: Check other property fields
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700188 if (strcmp(ppEnabledExtensions[i], VK_EXT_DEBUG_REPORT_EXTENSION_NAME) == 0) {
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600189 debug_data->g_DEBUG_REPORT = true;
190 }
191 }
Mark Lobodzinskifd027d02016-06-08 12:44:54 -0600192
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600193 return debug_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600194}
195
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700196static inline void layer_debug_report_destroy_instance(debug_report_data *debug_data) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600197 if (debug_data) {
198 RemoveAllMessageCallbacks(debug_data, &debug_data->default_debug_callback_list);
199 RemoveAllMessageCallbacks(debug_data, &debug_data->debug_callback_list);
200 free(debug_data);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600201 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600202}
203
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700204static inline debug_report_data *layer_debug_report_create_device(debug_report_data *instance_debug_data, VkDevice device) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600205 // DEBUG_REPORT shares data between Instance and Device,
206 // so just return instance's data pointer
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600207 return instance_debug_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600208}
209
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600210static inline void layer_debug_report_destroy_device(VkDevice device) {
211 // Nothing to do since we're using instance data record
212}
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600213
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600214static inline void layer_destroy_msg_callback(debug_report_data *debug_data, VkDebugReportCallbackEXT callback,
215 const VkAllocationCallbacks *pAllocator) {
216 RemoveDebugMessageCallback(debug_data, &debug_data->debug_callback_list, callback);
217 RemoveDebugMessageCallback(debug_data, &debug_data->default_debug_callback_list, callback);
218}
219
220static inline VkResult layer_create_msg_callback(debug_report_data *debug_data, bool default_callback,
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700221 const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,
222 const VkAllocationCallbacks *pAllocator, VkDebugReportCallbackEXT *pCallback) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700223 VkLayerDbgFunctionNode *pNewDbgFuncNode = (VkLayerDbgFunctionNode *)malloc(sizeof(VkLayerDbgFunctionNode));
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600224 if (!pNewDbgFuncNode)
225 return VK_ERROR_OUT_OF_HOST_MEMORY;
226
Tobin Ehliseb7715d2015-09-21 09:36:47 -0600227 // Handle of 0 is logging_callback so use allocated Node address as unique handle
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700228 if (!(*pCallback))
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700229 *pCallback = (VkDebugReportCallbackEXT)pNewDbgFuncNode;
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700230 pNewDbgFuncNode->msgCallback = *pCallback;
231 pNewDbgFuncNode->pfnMsgCallback = pCreateInfo->pfnCallback;
232 pNewDbgFuncNode->msgFlags = pCreateInfo->flags;
233 pNewDbgFuncNode->pUserData = pCreateInfo->pUserData;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600234
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600235 if (default_callback) {
236 AddDebugMessageCallback(debug_data, &debug_data->default_debug_callback_list, pNewDbgFuncNode);
237 } else {
238 AddDebugMessageCallback(debug_data, &debug_data->debug_callback_list, pNewDbgFuncNode);
239 }
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700240 debug_data->active_flags |= pCreateInfo->flags;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600241
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700242 debug_report_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
243 (uint64_t)*pCallback, 0, VK_DEBUG_REPORT_ERROR_CALLBACK_REF_EXT, "DebugReport", "Added callback");
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600244 return VK_SUCCESS;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600245}
246
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700247static inline PFN_vkVoidFunction debug_report_get_instance_proc_addr(debug_report_data *debug_data, const char *funcName) {
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600248 if (!debug_data || !debug_data->g_DEBUG_REPORT) {
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600249 return NULL;
250 }
251
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700252 if (!strcmp(funcName, "vkCreateDebugReportCallbackEXT")) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700253 return (PFN_vkVoidFunction)vkCreateDebugReportCallbackEXT;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600254 }
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700255 if (!strcmp(funcName, "vkDestroyDebugReportCallbackEXT")) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700256 return (PFN_vkVoidFunction)vkDestroyDebugReportCallbackEXT;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600257 }
258
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700259 if (!strcmp(funcName, "vkDebugReportMessageEXT")) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700260 return (PFN_vkVoidFunction)vkDebugReportMessageEXT;
Courtney Goeltzenleuchter822e8d72015-11-30 15:28:25 -0700261 }
262
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600263 return NULL;
264}
265
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600266// This utility (called at vkCreateInstance() time), looks at a pNext chain.
267// It counts any VkDebugReportCallbackCreateInfoEXT structs that it finds. It
268// then allocates an array that can hold that many structs, as well as that
269// many VkDebugReportCallbackEXT handles. It then copies each
270// VkDebugReportCallbackCreateInfoEXT, and initializes each handle.
271static VkResult layer_copy_tmp_callbacks(const void *pChain, uint32_t *num_callbacks, VkDebugReportCallbackCreateInfoEXT **infos,
272 VkDebugReportCallbackEXT **callbacks) {
273 uint32_t n = *num_callbacks = 0;
274
275 const void *pNext = pChain;
276 while (pNext) {
277 // 1st, count the number VkDebugReportCallbackCreateInfoEXT:
278 if (((VkDebugReportCallbackCreateInfoEXT *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT) {
279 n++;
280 }
281 pNext = (void *)((VkDebugReportCallbackCreateInfoEXT *)pNext)->pNext;
282 }
283 if (n == 0) {
284 return VK_SUCCESS;
285 }
286
287 // 2nd, allocate memory for each VkDebugReportCallbackCreateInfoEXT:
288 VkDebugReportCallbackCreateInfoEXT *pInfos = *infos =
289 ((VkDebugReportCallbackCreateInfoEXT *)malloc(n * sizeof(VkDebugReportCallbackCreateInfoEXT)));
290 if (!pInfos) {
291 return VK_ERROR_OUT_OF_HOST_MEMORY;
292 }
293 // 3rd, allocate memory for a unique handle for each callback:
294 VkDebugReportCallbackEXT *pCallbacks = *callbacks = ((VkDebugReportCallbackEXT *)malloc(n * sizeof(VkDebugReportCallbackEXT)));
295 if (!pCallbacks) {
296 free(pInfos);
297 return VK_ERROR_OUT_OF_HOST_MEMORY;
298 }
299 // 4th, copy each VkDebugReportCallbackCreateInfoEXT for use by
300 // vkDestroyInstance, and assign a unique handle to each callback (just
301 // use the address of the copied VkDebugReportCallbackCreateInfoEXT):
302 pNext = pChain;
303 while (pNext) {
304 if (((VkInstanceCreateInfo *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT) {
305 memcpy(pInfos, pNext, sizeof(VkDebugReportCallbackCreateInfoEXT));
306 *pCallbacks++ = (VkDebugReportCallbackEXT)pInfos++;
307 }
308 pNext = (void *)((VkInstanceCreateInfo *)pNext)->pNext;
309 }
310
311 *num_callbacks = n;
312 return VK_SUCCESS;
313}
314
315// This utility frees the arrays allocated by layer_copy_tmp_callbacks()
316static void layer_free_tmp_callbacks(VkDebugReportCallbackCreateInfoEXT *infos, VkDebugReportCallbackEXT *callbacks) {
317 free(infos);
318 free(callbacks);
319}
320
321// This utility enables all of the VkDebugReportCallbackCreateInfoEXT structs
322// that were copied by layer_copy_tmp_callbacks()
323static VkResult layer_enable_tmp_callbacks(debug_report_data *debug_data, uint32_t num_callbacks,
324 VkDebugReportCallbackCreateInfoEXT *infos, VkDebugReportCallbackEXT *callbacks) {
325 VkResult rtn = VK_SUCCESS;
326 for (uint32_t i = 0; i < num_callbacks; i++) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600327 rtn = layer_create_msg_callback(debug_data, false, &infos[i], NULL, &callbacks[i]);
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600328 if (rtn != VK_SUCCESS) {
329 for (uint32_t j = 0; j < i; j++) {
330 layer_destroy_msg_callback(debug_data, callbacks[j], NULL);
331 }
332 return rtn;
333 }
334 }
335 return rtn;
336}
337
338// This utility disables all of the VkDebugReportCallbackCreateInfoEXT structs
339// that were copied by layer_copy_tmp_callbacks()
340static void layer_disable_tmp_callbacks(debug_report_data *debug_data, uint32_t num_callbacks,
341 VkDebugReportCallbackEXT *callbacks) {
342 for (uint32_t i = 0; i < num_callbacks; i++) {
343 layer_destroy_msg_callback(debug_data, callbacks[i], NULL);
344 }
345}
346
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600347// Checks if the message will get logged.
348// Allows layer to defer collecting & formating data if the
349// message will be discarded.
Dustin Graves8f1eab92016-04-05 09:41:17 -0600350static inline bool will_log_msg(debug_report_data *debug_data, VkFlags msgFlags) {
Courtney Goeltzenleuchter6a564a12015-09-18 16:30:24 -0600351 if (!debug_data || !(debug_data->active_flags & msgFlags)) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600352 // Message is not wanted
Courtney Goeltzenleuchter6a564a12015-09-18 16:30:24 -0600353 return false;
354 }
355
356 return true;
357}
358
Chris Forbes8a25bce2016-03-24 12:06:35 +1300359#ifdef WIN32
360static inline int vasprintf(char **strp, char const *fmt, va_list ap) {
361 *strp = nullptr;
362 int size = _vscprintf(fmt, ap);
363 if (size >= 0) {
364 *strp = (char *)malloc(size+1);
365 if (!*strp) {
366 return -1;
367 }
368 _vsnprintf(*strp, size+1, fmt, ap);
369 }
370 return size;
371}
372#endif
373
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600374// Output log message via DEBUG_REPORT
375// Takes format and variable arg list so that output string
376// is only computed if a message needs to be logged
Michael Lentine010f4692015-11-03 16:19:46 -0800377#ifndef WIN32
Tobin Ehlis17978d82016-05-17 07:58:01 -0600378static inline bool log_msg(const debug_report_data *debug_data, VkFlags msgFlags, VkDebugReportObjectTypeEXT objectType,
Dustin Graves8f1eab92016-04-05 09:41:17 -0600379 uint64_t srcObject, size_t location, int32_t msgCode, const char *pLayerPrefix, const char *format, ...)
380 __attribute__((format(printf, 8, 9)));
Michael Lentine010f4692015-11-03 16:19:46 -0800381#endif
Tobin Ehlis17978d82016-05-17 07:58:01 -0600382static inline bool log_msg(const debug_report_data *debug_data, VkFlags msgFlags, VkDebugReportObjectTypeEXT objectType,
Dustin Graves8f1eab92016-04-05 09:41:17 -0600383 uint64_t srcObject, size_t location, int32_t msgCode, const char *pLayerPrefix, const char *format,
384 ...) {
Courtney Goeltzenleuchter61cb70a2015-06-26 15:14:50 -0600385 if (!debug_data || !(debug_data->active_flags & msgFlags)) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600386 // Message is not wanted
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600387 return false;
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600388 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600389
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600390 va_list argptr;
391 va_start(argptr, format);
Chris Forbes8a25bce2016-03-24 12:06:35 +1300392 char *str;
Chris Forbesed6a1b32016-04-28 14:27:19 +1200393 if (-1 == vasprintf(&str, format, argptr)) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600394 // On failure, glibc vasprintf leaves str undefined
Chris Forbesed6a1b32016-04-28 14:27:19 +1200395 str = nullptr;
396 }
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600397 va_end(argptr);
Chris Forbesed6a1b32016-04-28 14:27:19 +1200398 bool result = debug_report_log_msg(debug_data, msgFlags, objectType, srcObject, location, msgCode, pLayerPrefix,
399 str ? str : "Allocation failure");
Chris Forbes8a25bce2016-03-24 12:06:35 +1300400 free(str);
401 return result;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600402}
403
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700404static inline VKAPI_ATTR VkBool32 VKAPI_CALL log_callback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType, uint64_t srcObject,
405 size_t location, int32_t msgCode, const char *pLayerPrefix,
406 const char *pMsg, void *pUserData) {
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600407 char msg_flags[30];
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600408
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600409 print_msg_flags(msgFlags, msg_flags);
410
Mark Muelleraab36502016-05-03 13:17:29 -0600411 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 -0700412 srcObject, objType, (unsigned long)location, msgCode, pMsg);
413 fflush((FILE *)pUserData);
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600414
415 return false;
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600416}
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600417
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700418static inline VKAPI_ATTR VkBool32 VKAPI_CALL win32_debug_output_msg(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
419 uint64_t srcObject, size_t location, int32_t msgCode,
420 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600421#ifdef WIN32
422 char msg_flags[30];
423 char buf[2048];
424
425 print_msg_flags(msgFlags, msg_flags);
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700426 _snprintf(buf, sizeof(buf) - 1,
427 "%s (%s): object: 0x%" PRIxPTR " type: %d location: " PRINTF_SIZE_T_SPECIFIER " msgCode: %d: %s\n", pLayerPrefix,
428 msg_flags, (size_t)srcObject, objType, location, msgCode, pMsg);
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600429
430 OutputDebugString(buf);
431#endif
432
433 return false;
434}
435
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600436#endif // LAYER_LOGGING_H