blob: 58947d501e609bd2c7398b257670af78dd6c26fc [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
Tobin Ehlis8d6acde2017-02-08 07:40:40 -070045template debug_report_data *GetLayerDataPtr<debug_report_data>(void *data_key,
Jon Ashburn5484e0c2016-03-08 17:48:44 -070046 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) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060056 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;
Mark Lobodzinski05b04cd2016-11-10 09:12:57 -070066 VkFlags local_flags = 0;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060067
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060068 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,
Mark Lobodzinski05b04cd2016-11-10 09:12:57 -070077 "DebugReport", "Destroyed callback\n");
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060078 } else {
79 matched = false;
Mark Lobodzinski05b04cd2016-11-10 09:12:57 -070080 local_flags |= cur_callback->msgFlags;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060081 }
82 prev_callback = cur_callback;
83 cur_callback = cur_callback->pNext;
84 if (matched) {
85 free(prev_callback);
86 }
87 }
Mark Lobodzinski05b04cd2016-11-10 09:12:57 -070088 debug_data->active_flags = local_flags;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060089}
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
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600107// Utility function to handle reporting
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600108static inline bool debug_report_log_msg(const debug_report_data *debug_data, VkFlags msgFlags,
109 VkDebugReportObjectTypeEXT objectType, uint64_t srcObject, size_t location, int32_t msgCode,
110 const char *pLayerPrefix, const char *pMsg) {
Dustin Graves8f1eab92016-04-05 09:41:17 -0600111 bool bail = false;
Mark Lobodzinski55a197f2016-06-21 15:54:57 -0600112 VkLayerDbgFunctionNode *pTrav = NULL;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600113
Mark Lobodzinski55a197f2016-06-21 15:54:57 -0600114 if (debug_data->debug_callback_list != NULL) {
115 pTrav = debug_data->debug_callback_list;
116 } else {
117 pTrav = debug_data->default_debug_callback_list;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600118 }
119
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600120 while (pTrav) {
121 if (pTrav->msgFlags & msgFlags) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700122 if (pTrav->pfnMsgCallback(msgFlags, objectType, srcObject, location, msgCode, pLayerPrefix, pMsg, pTrav->pUserData)) {
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600123 bail = true;
124 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600125 }
126 pTrav = pTrav->pNext;
127 }
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600128
129 return bail;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600130}
131
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700132static inline debug_report_data *debug_report_create_instance(
133 VkLayerInstanceDispatchTable *table, VkInstance inst, uint32_t extension_count,
134 const char *const *ppEnabledExtensions) // layer or extension name to be enabled
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600135{
Mark Youngaa1aa3a2016-07-05 16:41:50 -0600136 debug_report_data *debug_data = (debug_report_data *)malloc(sizeof(debug_report_data));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700137 if (!debug_data) return NULL;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600138
139 memset(debug_data, 0, sizeof(debug_report_data));
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600140 for (uint32_t i = 0; i < extension_count; i++) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600141 // TODO: Check other property fields
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700142 if (strcmp(ppEnabledExtensions[i], VK_EXT_DEBUG_REPORT_EXTENSION_NAME) == 0) {
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600143 debug_data->g_DEBUG_REPORT = true;
144 }
145 }
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600146 return debug_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600147}
148
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700149static inline void layer_debug_report_destroy_instance(debug_report_data *debug_data) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600150 if (debug_data) {
151 RemoveAllMessageCallbacks(debug_data, &debug_data->default_debug_callback_list);
152 RemoveAllMessageCallbacks(debug_data, &debug_data->debug_callback_list);
153 free(debug_data);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600154 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600155}
156
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700157static inline debug_report_data *layer_debug_report_create_device(debug_report_data *instance_debug_data, VkDevice device) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600158 // DEBUG_REPORT shares data between Instance and Device,
159 // so just return instance's data pointer
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600160 return instance_debug_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600161}
162
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600163static inline void layer_debug_report_destroy_device(VkDevice device) {
164 // Nothing to do since we're using instance data record
165}
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600166
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600167static inline void layer_destroy_msg_callback(debug_report_data *debug_data, VkDebugReportCallbackEXT callback,
168 const VkAllocationCallbacks *pAllocator) {
169 RemoveDebugMessageCallback(debug_data, &debug_data->debug_callback_list, callback);
170 RemoveDebugMessageCallback(debug_data, &debug_data->default_debug_callback_list, callback);
171}
172
173static inline VkResult layer_create_msg_callback(debug_report_data *debug_data, bool default_callback,
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700174 const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,
175 const VkAllocationCallbacks *pAllocator, VkDebugReportCallbackEXT *pCallback) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700176 VkLayerDbgFunctionNode *pNewDbgFuncNode = (VkLayerDbgFunctionNode *)malloc(sizeof(VkLayerDbgFunctionNode));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700177 if (!pNewDbgFuncNode) return VK_ERROR_OUT_OF_HOST_MEMORY;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600178
Tobin Ehliseb7715d2015-09-21 09:36:47 -0600179 // Handle of 0 is logging_callback so use allocated Node address as unique handle
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700180 if (!(*pCallback)) *pCallback = (VkDebugReportCallbackEXT)pNewDbgFuncNode;
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700181 pNewDbgFuncNode->msgCallback = *pCallback;
182 pNewDbgFuncNode->pfnMsgCallback = pCreateInfo->pfnCallback;
183 pNewDbgFuncNode->msgFlags = pCreateInfo->flags;
184 pNewDbgFuncNode->pUserData = pCreateInfo->pUserData;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600185
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600186 if (default_callback) {
187 AddDebugMessageCallback(debug_data, &debug_data->default_debug_callback_list, pNewDbgFuncNode);
Mark Lobodzinski2134fc32016-11-10 09:10:08 -0700188 debug_data->active_flags |= pCreateInfo->flags;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600189 } else {
190 AddDebugMessageCallback(debug_data, &debug_data->debug_callback_list, pNewDbgFuncNode);
Mark Lobodzinski2134fc32016-11-10 09:10:08 -0700191 debug_data->active_flags = pCreateInfo->flags;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600192 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600193
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700194 debug_report_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
195 (uint64_t)*pCallback, 0, VK_DEBUG_REPORT_ERROR_CALLBACK_REF_EXT, "DebugReport", "Added callback");
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600196 return VK_SUCCESS;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600197}
198
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700199static inline PFN_vkVoidFunction debug_report_get_instance_proc_addr(debug_report_data *debug_data, const char *funcName) {
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600200 if (!debug_data || !debug_data->g_DEBUG_REPORT) {
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600201 return NULL;
202 }
203
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700204 if (!strcmp(funcName, "vkCreateDebugReportCallbackEXT")) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700205 return (PFN_vkVoidFunction)vkCreateDebugReportCallbackEXT;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600206 }
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700207 if (!strcmp(funcName, "vkDestroyDebugReportCallbackEXT")) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700208 return (PFN_vkVoidFunction)vkDestroyDebugReportCallbackEXT;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600209 }
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700210 if (!strcmp(funcName, "vkDebugReportMessageEXT")) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700211 return (PFN_vkVoidFunction)vkDebugReportMessageEXT;
Courtney Goeltzenleuchter822e8d72015-11-30 15:28:25 -0700212 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600213 return NULL;
214}
215
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600216// This utility (called at vkCreateInstance() time), looks at a pNext chain.
217// It counts any VkDebugReportCallbackCreateInfoEXT structs that it finds. It
218// then allocates an array that can hold that many structs, as well as that
219// many VkDebugReportCallbackEXT handles. It then copies each
220// VkDebugReportCallbackCreateInfoEXT, and initializes each handle.
221static VkResult layer_copy_tmp_callbacks(const void *pChain, uint32_t *num_callbacks, VkDebugReportCallbackCreateInfoEXT **infos,
222 VkDebugReportCallbackEXT **callbacks) {
223 uint32_t n = *num_callbacks = 0;
224
225 const void *pNext = pChain;
226 while (pNext) {
227 // 1st, count the number VkDebugReportCallbackCreateInfoEXT:
228 if (((VkDebugReportCallbackCreateInfoEXT *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT) {
229 n++;
230 }
231 pNext = (void *)((VkDebugReportCallbackCreateInfoEXT *)pNext)->pNext;
232 }
233 if (n == 0) {
234 return VK_SUCCESS;
235 }
236
237 // 2nd, allocate memory for each VkDebugReportCallbackCreateInfoEXT:
238 VkDebugReportCallbackCreateInfoEXT *pInfos = *infos =
239 ((VkDebugReportCallbackCreateInfoEXT *)malloc(n * sizeof(VkDebugReportCallbackCreateInfoEXT)));
240 if (!pInfos) {
241 return VK_ERROR_OUT_OF_HOST_MEMORY;
242 }
243 // 3rd, allocate memory for a unique handle for each callback:
244 VkDebugReportCallbackEXT *pCallbacks = *callbacks = ((VkDebugReportCallbackEXT *)malloc(n * sizeof(VkDebugReportCallbackEXT)));
245 if (!pCallbacks) {
246 free(pInfos);
247 return VK_ERROR_OUT_OF_HOST_MEMORY;
248 }
249 // 4th, copy each VkDebugReportCallbackCreateInfoEXT for use by
250 // vkDestroyInstance, and assign a unique handle to each callback (just
251 // use the address of the copied VkDebugReportCallbackCreateInfoEXT):
252 pNext = pChain;
253 while (pNext) {
254 if (((VkInstanceCreateInfo *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT) {
255 memcpy(pInfos, pNext, sizeof(VkDebugReportCallbackCreateInfoEXT));
256 *pCallbacks++ = (VkDebugReportCallbackEXT)pInfos++;
257 }
258 pNext = (void *)((VkInstanceCreateInfo *)pNext)->pNext;
259 }
260
261 *num_callbacks = n;
262 return VK_SUCCESS;
263}
264
265// This utility frees the arrays allocated by layer_copy_tmp_callbacks()
266static void layer_free_tmp_callbacks(VkDebugReportCallbackCreateInfoEXT *infos, VkDebugReportCallbackEXT *callbacks) {
267 free(infos);
268 free(callbacks);
269}
270
271// This utility enables all of the VkDebugReportCallbackCreateInfoEXT structs
272// that were copied by layer_copy_tmp_callbacks()
273static VkResult layer_enable_tmp_callbacks(debug_report_data *debug_data, uint32_t num_callbacks,
274 VkDebugReportCallbackCreateInfoEXT *infos, VkDebugReportCallbackEXT *callbacks) {
275 VkResult rtn = VK_SUCCESS;
276 for (uint32_t i = 0; i < num_callbacks; i++) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600277 rtn = layer_create_msg_callback(debug_data, false, &infos[i], NULL, &callbacks[i]);
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600278 if (rtn != VK_SUCCESS) {
279 for (uint32_t j = 0; j < i; j++) {
280 layer_destroy_msg_callback(debug_data, callbacks[j], NULL);
281 }
282 return rtn;
283 }
284 }
285 return rtn;
286}
287
288// This utility disables all of the VkDebugReportCallbackCreateInfoEXT structs
289// that were copied by layer_copy_tmp_callbacks()
290static void layer_disable_tmp_callbacks(debug_report_data *debug_data, uint32_t num_callbacks,
291 VkDebugReportCallbackEXT *callbacks) {
292 for (uint32_t i = 0; i < num_callbacks; i++) {
293 layer_destroy_msg_callback(debug_data, callbacks[i], NULL);
294 }
295}
296
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600297// Checks if the message will get logged.
298// Allows layer to defer collecting & formating data if the
299// message will be discarded.
Dustin Graves8f1eab92016-04-05 09:41:17 -0600300static inline bool will_log_msg(debug_report_data *debug_data, VkFlags msgFlags) {
Courtney Goeltzenleuchter6a564a12015-09-18 16:30:24 -0600301 if (!debug_data || !(debug_data->active_flags & msgFlags)) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600302 // Message is not wanted
Courtney Goeltzenleuchter6a564a12015-09-18 16:30:24 -0600303 return false;
304 }
305
306 return true;
307}
308
Chris Forbes8a25bce2016-03-24 12:06:35 +1300309#ifdef WIN32
310static inline int vasprintf(char **strp, char const *fmt, va_list ap) {
311 *strp = nullptr;
312 int size = _vscprintf(fmt, ap);
313 if (size >= 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700314 *strp = (char *)malloc(size + 1);
Chris Forbes8a25bce2016-03-24 12:06:35 +1300315 if (!*strp) {
316 return -1;
317 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700318 _vsnprintf(*strp, size + 1, fmt, ap);
Chris Forbes8a25bce2016-03-24 12:06:35 +1300319 }
320 return size;
321}
322#endif
323
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600324// Output log message via DEBUG_REPORT
325// Takes format and variable arg list so that output string
326// is only computed if a message needs to be logged
Michael Lentine010f4692015-11-03 16:19:46 -0800327#ifndef WIN32
Tobin Ehlis17978d82016-05-17 07:58:01 -0600328static inline bool log_msg(const debug_report_data *debug_data, VkFlags msgFlags, VkDebugReportObjectTypeEXT objectType,
Dustin Graves8f1eab92016-04-05 09:41:17 -0600329 uint64_t srcObject, size_t location, int32_t msgCode, const char *pLayerPrefix, const char *format, ...)
330 __attribute__((format(printf, 8, 9)));
Michael Lentine010f4692015-11-03 16:19:46 -0800331#endif
Tobin Ehlis17978d82016-05-17 07:58:01 -0600332static inline bool log_msg(const debug_report_data *debug_data, VkFlags msgFlags, VkDebugReportObjectTypeEXT objectType,
Dustin Graves8f1eab92016-04-05 09:41:17 -0600333 uint64_t srcObject, size_t location, int32_t msgCode, const char *pLayerPrefix, const char *format,
334 ...) {
Courtney Goeltzenleuchter61cb70a2015-06-26 15:14:50 -0600335 if (!debug_data || !(debug_data->active_flags & msgFlags)) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600336 // Message is not wanted
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600337 return false;
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600338 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600339
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600340 va_list argptr;
341 va_start(argptr, format);
Chris Forbes8a25bce2016-03-24 12:06:35 +1300342 char *str;
Chris Forbesed6a1b32016-04-28 14:27:19 +1200343 if (-1 == vasprintf(&str, format, argptr)) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600344 // On failure, glibc vasprintf leaves str undefined
Chris Forbesed6a1b32016-04-28 14:27:19 +1200345 str = nullptr;
346 }
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600347 va_end(argptr);
Chris Forbesed6a1b32016-04-28 14:27:19 +1200348 bool result = debug_report_log_msg(debug_data, msgFlags, objectType, srcObject, location, msgCode, pLayerPrefix,
349 str ? str : "Allocation failure");
Chris Forbes8a25bce2016-03-24 12:06:35 +1300350 free(str);
351 return result;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600352}
353
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700354static inline VKAPI_ATTR VkBool32 VKAPI_CALL log_callback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType, uint64_t srcObject,
355 size_t location, int32_t msgCode, const char *pLayerPrefix,
356 const char *pMsg, void *pUserData) {
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600357 char msg_flags[30];
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600358
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600359 print_msg_flags(msgFlags, msg_flags);
360
Mark Muelleraab36502016-05-03 13:17:29 -0600361 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 -0700362 srcObject, objType, (unsigned long)location, msgCode, pMsg);
363 fflush((FILE *)pUserData);
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600364
365 return false;
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600366}
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600367
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700368static inline VKAPI_ATTR VkBool32 VKAPI_CALL win32_debug_output_msg(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
369 uint64_t srcObject, size_t location, int32_t msgCode,
370 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600371#ifdef WIN32
372 char msg_flags[30];
373 char buf[2048];
374
375 print_msg_flags(msgFlags, msg_flags);
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700376 _snprintf(buf, sizeof(buf) - 1,
377 "%s (%s): object: 0x%" PRIxPTR " type: %d location: " PRINTF_SIZE_T_SPECIFIER " msgCode: %d: %s\n", pLayerPrefix,
378 msg_flags, (size_t)srcObject, objType, location, msgCode, pMsg);
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600379
380 OutputDebugString(buf);
381#endif
382
383 return false;
384}
385
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700386#endif // LAYER_LOGGING_H