blob: cb4c44c7cb50cd74aed631be1510e6d9c284859a [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
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060025#include "vk_layer_config.h"
Tobin Ehlisa0cb02e2015-07-03 10:15:26 -060026#include "vk_layer_data.h"
27#include "vk_layer_table.h"
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060028#include "vk_loader_platform.h"
29#include "vulkan/vk_layer.h"
Karl Schultzd7f37542016-05-10 11:36:08 -060030#include <cinttypes>
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060031#include <stdarg.h>
32#include <stdbool.h>
33#include <stdio.h>
34#include <unordered_map>
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060035#include <vector>
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060036
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -060037typedef struct _debug_report_data {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060038 VkLayerDbgFunctionNode *debug_callback_list;
39 VkLayerDbgFunctionNode *default_debug_callback_list;
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -060040 VkFlags active_flags;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060041 bool g_DEBUG_REPORT;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -060042} debug_report_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060043
Jon Ashburn5484e0c2016-03-08 17:48:44 -070044template debug_report_data *get_my_data_ptr<debug_report_data>(void *data_key,
45 std::unordered_map<void *, debug_report_data *> &data_map);
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060046
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060047// Forward Declarations
48static inline bool debug_report_log_msg(const debug_report_data *debug_data, VkFlags msgFlags,
49 VkDebugReportObjectTypeEXT objectType, uint64_t srcObject, size_t location, int32_t msgCode,
50 const char *pLayerPrefix, const char *pMsg);
51
52// Add a debug message callback node structure to the specified callback linked list
53static inline void AddDebugMessageCallback(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head,
54 VkLayerDbgFunctionNode *new_node) {
55
56 new_node->pNext = *list_head;
57 *list_head = new_node;
58}
59
60// Remove specified debug message callback node structure from the specified callback linked list
61static inline void RemoveDebugMessageCallback(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head,
62 VkDebugReportCallbackEXT callback) {
63 VkLayerDbgFunctionNode *cur_callback = *list_head;
64 VkLayerDbgFunctionNode *prev_callback = cur_callback;
65 bool matched = false;
66
67 debug_data->active_flags = 0;
68 while (cur_callback) {
69 if (cur_callback->msgCallback == callback) {
70 matched = true;
71 prev_callback->pNext = cur_callback->pNext;
72 if (*list_head == cur_callback) {
73 *list_head = cur_callback->pNext;
74 }
75 debug_report_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
76 reinterpret_cast<uint64_t &>(cur_callback->msgCallback), 0, VK_DEBUG_REPORT_ERROR_CALLBACK_REF_EXT,
77 "DebugReport", "Destroyed callback");
78 } else {
79 matched = false;
80 debug_data->active_flags |= cur_callback->msgFlags;
81 }
82 prev_callback = cur_callback;
83 cur_callback = cur_callback->pNext;
84 if (matched) {
85 free(prev_callback);
86 }
87 }
88}
89
90// Removes all debug callback function nodes from the specified callback linked lists and frees their resources
91static inline void RemoveAllMessageCallbacks(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head) {
92 VkLayerDbgFunctionNode *current_callback = *list_head;
93 VkLayerDbgFunctionNode *prev_callback = current_callback;
94
95 while (current_callback) {
96 prev_callback = current_callback->pNext;
97 debug_report_log_msg(debug_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
98 (uint64_t)current_callback->msgCallback, 0, VK_DEBUG_REPORT_ERROR_CALLBACK_REF_EXT, "DebugReport",
99 "Debug Report callbacks not removed before DestroyInstance");
100 free(current_callback);
101 current_callback = prev_callback;
102 }
103 *list_head = NULL;
104}
105
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600106// Utility function to handle reporting
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600107static inline bool debug_report_log_msg(const debug_report_data *debug_data, VkFlags msgFlags,
108 VkDebugReportObjectTypeEXT objectType, uint64_t srcObject, size_t location, int32_t msgCode,
109 const char *pLayerPrefix, const char *pMsg) {
Dustin Graves8f1eab92016-04-05 09:41:17 -0600110 bool bail = false;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600111 VkLayerDbgFunctionNode *pTrav = NULL;
112
113 if (debug_data->debug_callback_list != NULL) {
114 pTrav = debug_data->debug_callback_list;
115 } else {
116 pTrav = debug_data->default_debug_callback_list;
117 }
118
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600119 while (pTrav) {
120 if (pTrav->msgFlags & msgFlags) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700121 if (pTrav->pfnMsgCallback(msgFlags, objectType, srcObject, location, msgCode, pLayerPrefix, pMsg, pTrav->pUserData)) {
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600122 bail = true;
123 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600124 }
125 pTrav = pTrav->pNext;
126 }
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600127
128 return bail;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600129}
130
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700131static inline debug_report_data *
132debug_report_create_instance(VkLayerInstanceDispatchTable *table, VkInstance inst, uint32_t extension_count,
133 const char *const *ppEnabledExtensions) // layer or extension name to be enabled
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600134{
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700135 debug_report_data *debug_data;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600136 PFN_vkGetInstanceProcAddr gpa = table->GetInstanceProcAddr;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600137
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700138 table->CreateDebugReportCallbackEXT = (PFN_vkCreateDebugReportCallbackEXT)gpa(inst, "vkCreateDebugReportCallbackEXT");
139 table->DestroyDebugReportCallbackEXT = (PFN_vkDestroyDebugReportCallbackEXT)gpa(inst, "vkDestroyDebugReportCallbackEXT");
140 table->DebugReportMessageEXT = (PFN_vkDebugReportMessageEXT)gpa(inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600141
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700142 debug_data = (debug_report_data *)malloc(sizeof(debug_report_data));
143 if (!debug_data)
144 return NULL;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600145
146 memset(debug_data, 0, sizeof(debug_report_data));
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600147 for (uint32_t i = 0; i < extension_count; i++) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600148 // TODO: Check other property fields
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700149 if (strcmp(ppEnabledExtensions[i], VK_EXT_DEBUG_REPORT_EXTENSION_NAME) == 0) {
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600150 debug_data->g_DEBUG_REPORT = true;
151 }
152 }
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600153 return debug_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600154}
155
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700156static inline void layer_debug_report_destroy_instance(debug_report_data *debug_data) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600157 if (debug_data) {
158 RemoveAllMessageCallbacks(debug_data, &debug_data->default_debug_callback_list);
159 RemoveAllMessageCallbacks(debug_data, &debug_data->debug_callback_list);
160 free(debug_data);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600161 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600162}
163
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700164static inline debug_report_data *layer_debug_report_create_device(debug_report_data *instance_debug_data, VkDevice device) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600165 // DEBUG_REPORT shares data between Instance and Device,
166 // so just return instance's data pointer
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600167 return instance_debug_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600168}
169
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600170static inline void layer_debug_report_destroy_device(VkDevice device) {
171 // Nothing to do since we're using instance data record
172}
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600173
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600174static inline void layer_destroy_msg_callback(debug_report_data *debug_data, VkDebugReportCallbackEXT callback,
175 const VkAllocationCallbacks *pAllocator) {
176 RemoveDebugMessageCallback(debug_data, &debug_data->debug_callback_list, callback);
177 RemoveDebugMessageCallback(debug_data, &debug_data->default_debug_callback_list, callback);
178}
179
180static inline VkResult layer_create_msg_callback(debug_report_data *debug_data, bool default_callback,
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700181 const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,
182 const VkAllocationCallbacks *pAllocator, VkDebugReportCallbackEXT *pCallback) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700183 VkLayerDbgFunctionNode *pNewDbgFuncNode = (VkLayerDbgFunctionNode *)malloc(sizeof(VkLayerDbgFunctionNode));
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600184 if (!pNewDbgFuncNode)
185 return VK_ERROR_OUT_OF_HOST_MEMORY;
186
Tobin Ehliseb7715d2015-09-21 09:36:47 -0600187 // Handle of 0 is logging_callback so use allocated Node address as unique handle
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700188 if (!(*pCallback))
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700189 *pCallback = (VkDebugReportCallbackEXT)pNewDbgFuncNode;
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700190 pNewDbgFuncNode->msgCallback = *pCallback;
191 pNewDbgFuncNode->pfnMsgCallback = pCreateInfo->pfnCallback;
192 pNewDbgFuncNode->msgFlags = pCreateInfo->flags;
193 pNewDbgFuncNode->pUserData = pCreateInfo->pUserData;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600194
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600195 if (default_callback) {
196 AddDebugMessageCallback(debug_data, &debug_data->default_debug_callback_list, pNewDbgFuncNode);
197 } else {
198 AddDebugMessageCallback(debug_data, &debug_data->debug_callback_list, pNewDbgFuncNode);
199 }
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700200 debug_data->active_flags |= pCreateInfo->flags;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600201
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700202 debug_report_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
203 (uint64_t)*pCallback, 0, VK_DEBUG_REPORT_ERROR_CALLBACK_REF_EXT, "DebugReport", "Added callback");
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600204 return VK_SUCCESS;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600205}
206
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700207static inline PFN_vkVoidFunction debug_report_get_instance_proc_addr(debug_report_data *debug_data, const char *funcName) {
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600208 if (!debug_data || !debug_data->g_DEBUG_REPORT) {
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600209 return NULL;
210 }
211
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700212 if (!strcmp(funcName, "vkCreateDebugReportCallbackEXT")) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700213 return (PFN_vkVoidFunction)vkCreateDebugReportCallbackEXT;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600214 }
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700215 if (!strcmp(funcName, "vkDestroyDebugReportCallbackEXT")) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700216 return (PFN_vkVoidFunction)vkDestroyDebugReportCallbackEXT;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600217 }
218
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700219 if (!strcmp(funcName, "vkDebugReportMessageEXT")) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700220 return (PFN_vkVoidFunction)vkDebugReportMessageEXT;
Courtney Goeltzenleuchter822e8d72015-11-30 15:28:25 -0700221 }
222
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600223 return NULL;
224}
225
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600226// This utility (called at vkCreateInstance() time), looks at a pNext chain.
227// It counts any VkDebugReportCallbackCreateInfoEXT structs that it finds. It
228// then allocates an array that can hold that many structs, as well as that
229// many VkDebugReportCallbackEXT handles. It then copies each
230// VkDebugReportCallbackCreateInfoEXT, and initializes each handle.
231static VkResult layer_copy_tmp_callbacks(const void *pChain, uint32_t *num_callbacks, VkDebugReportCallbackCreateInfoEXT **infos,
232 VkDebugReportCallbackEXT **callbacks) {
233 uint32_t n = *num_callbacks = 0;
234
235 const void *pNext = pChain;
236 while (pNext) {
237 // 1st, count the number VkDebugReportCallbackCreateInfoEXT:
238 if (((VkDebugReportCallbackCreateInfoEXT *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT) {
239 n++;
240 }
241 pNext = (void *)((VkDebugReportCallbackCreateInfoEXT *)pNext)->pNext;
242 }
243 if (n == 0) {
244 return VK_SUCCESS;
245 }
246
247 // 2nd, allocate memory for each VkDebugReportCallbackCreateInfoEXT:
248 VkDebugReportCallbackCreateInfoEXT *pInfos = *infos =
249 ((VkDebugReportCallbackCreateInfoEXT *)malloc(n * sizeof(VkDebugReportCallbackCreateInfoEXT)));
250 if (!pInfos) {
251 return VK_ERROR_OUT_OF_HOST_MEMORY;
252 }
253 // 3rd, allocate memory for a unique handle for each callback:
254 VkDebugReportCallbackEXT *pCallbacks = *callbacks = ((VkDebugReportCallbackEXT *)malloc(n * sizeof(VkDebugReportCallbackEXT)));
255 if (!pCallbacks) {
256 free(pInfos);
257 return VK_ERROR_OUT_OF_HOST_MEMORY;
258 }
259 // 4th, copy each VkDebugReportCallbackCreateInfoEXT for use by
260 // vkDestroyInstance, and assign a unique handle to each callback (just
261 // use the address of the copied VkDebugReportCallbackCreateInfoEXT):
262 pNext = pChain;
263 while (pNext) {
264 if (((VkInstanceCreateInfo *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT) {
265 memcpy(pInfos, pNext, sizeof(VkDebugReportCallbackCreateInfoEXT));
266 *pCallbacks++ = (VkDebugReportCallbackEXT)pInfos++;
267 }
268 pNext = (void *)((VkInstanceCreateInfo *)pNext)->pNext;
269 }
270
271 *num_callbacks = n;
272 return VK_SUCCESS;
273}
274
275// This utility frees the arrays allocated by layer_copy_tmp_callbacks()
276static void layer_free_tmp_callbacks(VkDebugReportCallbackCreateInfoEXT *infos, VkDebugReportCallbackEXT *callbacks) {
277 free(infos);
278 free(callbacks);
279}
280
281// This utility enables all of the VkDebugReportCallbackCreateInfoEXT structs
282// that were copied by layer_copy_tmp_callbacks()
283static VkResult layer_enable_tmp_callbacks(debug_report_data *debug_data, uint32_t num_callbacks,
284 VkDebugReportCallbackCreateInfoEXT *infos, VkDebugReportCallbackEXT *callbacks) {
285 VkResult rtn = VK_SUCCESS;
286 for (uint32_t i = 0; i < num_callbacks; i++) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600287 rtn = layer_create_msg_callback(debug_data, false, &infos[i], NULL, &callbacks[i]);
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600288 if (rtn != VK_SUCCESS) {
289 for (uint32_t j = 0; j < i; j++) {
290 layer_destroy_msg_callback(debug_data, callbacks[j], NULL);
291 }
292 return rtn;
293 }
294 }
295 return rtn;
296}
297
298// This utility disables all of the VkDebugReportCallbackCreateInfoEXT structs
299// that were copied by layer_copy_tmp_callbacks()
300static void layer_disable_tmp_callbacks(debug_report_data *debug_data, uint32_t num_callbacks,
301 VkDebugReportCallbackEXT *callbacks) {
302 for (uint32_t i = 0; i < num_callbacks; i++) {
303 layer_destroy_msg_callback(debug_data, callbacks[i], NULL);
304 }
305}
306
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600307// Checks if the message will get logged.
308// Allows layer to defer collecting & formating data if the
309// message will be discarded.
Dustin Graves8f1eab92016-04-05 09:41:17 -0600310static inline bool will_log_msg(debug_report_data *debug_data, VkFlags msgFlags) {
Courtney Goeltzenleuchter6a564a12015-09-18 16:30:24 -0600311 if (!debug_data || !(debug_data->active_flags & msgFlags)) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600312 // Message is not wanted
Courtney Goeltzenleuchter6a564a12015-09-18 16:30:24 -0600313 return false;
314 }
315
316 return true;
317}
318
Chris Forbes8a25bce2016-03-24 12:06:35 +1300319#ifdef WIN32
320static inline int vasprintf(char **strp, char const *fmt, va_list ap) {
321 *strp = nullptr;
322 int size = _vscprintf(fmt, ap);
323 if (size >= 0) {
324 *strp = (char *)malloc(size+1);
325 if (!*strp) {
326 return -1;
327 }
328 _vsnprintf(*strp, size+1, fmt, ap);
329 }
330 return size;
331}
332#endif
333
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600334// Output log message via DEBUG_REPORT
335// Takes format and variable arg list so that output string
336// is only computed if a message needs to be logged
Michael Lentine010f4692015-11-03 16:19:46 -0800337#ifndef WIN32
Tobin Ehlis17978d82016-05-17 07:58:01 -0600338static inline bool log_msg(const debug_report_data *debug_data, VkFlags msgFlags, VkDebugReportObjectTypeEXT objectType,
Dustin Graves8f1eab92016-04-05 09:41:17 -0600339 uint64_t srcObject, size_t location, int32_t msgCode, const char *pLayerPrefix, const char *format, ...)
340 __attribute__((format(printf, 8, 9)));
Michael Lentine010f4692015-11-03 16:19:46 -0800341#endif
Tobin Ehlis17978d82016-05-17 07:58:01 -0600342static inline bool log_msg(const debug_report_data *debug_data, VkFlags msgFlags, VkDebugReportObjectTypeEXT objectType,
Dustin Graves8f1eab92016-04-05 09:41:17 -0600343 uint64_t srcObject, size_t location, int32_t msgCode, const char *pLayerPrefix, const char *format,
344 ...) {
Courtney Goeltzenleuchter61cb70a2015-06-26 15:14:50 -0600345 if (!debug_data || !(debug_data->active_flags & msgFlags)) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600346 // Message is not wanted
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600347 return false;
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600348 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600349
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600350 va_list argptr;
351 va_start(argptr, format);
Chris Forbes8a25bce2016-03-24 12:06:35 +1300352 char *str;
Chris Forbesed6a1b32016-04-28 14:27:19 +1200353 if (-1 == vasprintf(&str, format, argptr)) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600354 // On failure, glibc vasprintf leaves str undefined
Chris Forbesed6a1b32016-04-28 14:27:19 +1200355 str = nullptr;
356 }
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600357 va_end(argptr);
Chris Forbesed6a1b32016-04-28 14:27:19 +1200358 bool result = debug_report_log_msg(debug_data, msgFlags, objectType, srcObject, location, msgCode, pLayerPrefix,
359 str ? str : "Allocation failure");
Chris Forbes8a25bce2016-03-24 12:06:35 +1300360 free(str);
361 return result;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600362}
363
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700364static inline VKAPI_ATTR VkBool32 VKAPI_CALL log_callback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType, uint64_t srcObject,
365 size_t location, int32_t msgCode, const char *pLayerPrefix,
366 const char *pMsg, void *pUserData) {
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600367 char msg_flags[30];
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600368
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600369 print_msg_flags(msgFlags, msg_flags);
370
Mark Muelleraab36502016-05-03 13:17:29 -0600371 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 -0700372 srcObject, objType, (unsigned long)location, msgCode, pMsg);
373 fflush((FILE *)pUserData);
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600374
375 return false;
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600376}
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600377
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700378static inline VKAPI_ATTR VkBool32 VKAPI_CALL win32_debug_output_msg(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
379 uint64_t srcObject, size_t location, int32_t msgCode,
380 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600381#ifdef WIN32
382 char msg_flags[30];
383 char buf[2048];
384
385 print_msg_flags(msgFlags, msg_flags);
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700386 _snprintf(buf, sizeof(buf) - 1,
387 "%s (%s): object: 0x%" PRIxPTR " type: %d location: " PRINTF_SIZE_T_SPECIFIER " msgCode: %d: %s\n", pLayerPrefix,
388 msg_flags, (size_t)srcObject, objType, location, msgCode, pMsg);
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600389
390 OutputDebugString(buf);
391#endif
392
393 return false;
394}
395
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600396#endif // LAYER_LOGGING_H