blob: bbd24d0fc4b0f0dd08101396794261387605781a [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"
30#include <inttypes.h>
31#include <stdarg.h>
32#include <stdbool.h>
33#include <stdio.h>
34#include <unordered_map>
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060035
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -060036typedef struct _debug_report_data {
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060037 VkLayerDbgFunctionNode *g_pDbgFunctionHead;
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -060038 VkFlags active_flags;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060039 bool g_DEBUG_REPORT;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -060040} debug_report_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060041
Jon Ashburn5484e0c2016-03-08 17:48:44 -070042template debug_report_data *get_my_data_ptr<debug_report_data>(void *data_key,
43 std::unordered_map<void *, debug_report_data *> &data_map);
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060044
45// Utility function to handle reporting
Dustin Graves8f1eab92016-04-05 09:41:17 -060046static inline bool debug_report_log_msg(debug_report_data *debug_data, VkFlags msgFlags, VkDebugReportObjectTypeEXT objectType,
47 uint64_t srcObject, size_t location, int32_t msgCode, const char *pLayerPrefix,
48 const char *pMsg) {
49 bool bail = false;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060050 VkLayerDbgFunctionNode *pTrav = debug_data->g_pDbgFunctionHead;
51 while (pTrav) {
52 if (pTrav->msgFlags & msgFlags) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -070053 if (pTrav->pfnMsgCallback(msgFlags, objectType, srcObject, location, msgCode, pLayerPrefix, pMsg, pTrav->pUserData)) {
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -060054 bail = true;
55 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060056 }
57 pTrav = pTrav->pNext;
58 }
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -060059
60 return bail;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060061}
62
Jon Ashburn5484e0c2016-03-08 17:48:44 -070063static inline debug_report_data *
64debug_report_create_instance(VkLayerInstanceDispatchTable *table, VkInstance inst, uint32_t extension_count,
65 const char *const *ppEnabledExtensions) // layer or extension name to be enabled
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060066{
Jon Ashburn5484e0c2016-03-08 17:48:44 -070067 debug_report_data *debug_data;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -060068 PFN_vkGetInstanceProcAddr gpa = table->GetInstanceProcAddr;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060069
Jon Ashburn5484e0c2016-03-08 17:48:44 -070070 table->CreateDebugReportCallbackEXT = (PFN_vkCreateDebugReportCallbackEXT)gpa(inst, "vkCreateDebugReportCallbackEXT");
71 table->DestroyDebugReportCallbackEXT = (PFN_vkDestroyDebugReportCallbackEXT)gpa(inst, "vkDestroyDebugReportCallbackEXT");
72 table->DebugReportMessageEXT = (PFN_vkDebugReportMessageEXT)gpa(inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -060073
Jon Ashburn5484e0c2016-03-08 17:48:44 -070074 debug_data = (debug_report_data *)malloc(sizeof(debug_report_data));
75 if (!debug_data)
76 return NULL;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -060077
78 memset(debug_data, 0, sizeof(debug_report_data));
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060079 for (uint32_t i = 0; i < extension_count; i++) {
80 /* TODO: Check other property fields */
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -070081 if (strcmp(ppEnabledExtensions[i], VK_EXT_DEBUG_REPORT_EXTENSION_NAME) == 0) {
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060082 debug_data->g_DEBUG_REPORT = true;
83 }
84 }
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -060085 return debug_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060086}
87
Jon Ashburn5484e0c2016-03-08 17:48:44 -070088static inline void layer_debug_report_destroy_instance(debug_report_data *debug_data) {
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -060089 VkLayerDbgFunctionNode *pTrav;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060090 VkLayerDbgFunctionNode *pTravNext;
91
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -060092 if (!debug_data) {
93 return;
94 }
95
96 pTrav = debug_data->g_pDbgFunctionHead;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060097 /* Clear out any leftover callbacks */
98 while (pTrav) {
99 pTravNext = pTrav->pNext;
100
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700101 debug_report_log_msg(debug_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
102 (uint64_t)pTrav->msgCallback, 0, VK_DEBUG_REPORT_ERROR_CALLBACK_REF_EXT, "DebugReport",
103 "Debug Report callbacks not removed before DestroyInstance");
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600104
105 free(pTrav);
106 pTrav = pTravNext;
107 }
108 debug_data->g_pDbgFunctionHead = NULL;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600109
110 free(debug_data);
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600111}
112
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700113static inline debug_report_data *layer_debug_report_create_device(debug_report_data *instance_debug_data, VkDevice device) {
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600114 /* DEBUG_REPORT shares data between Instance and Device,
115 * so just return instance's data pointer */
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600116 return instance_debug_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600117}
118
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700119static inline void layer_debug_report_destroy_device(VkDevice device) { /* Nothing to do since we're using instance data record */ }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600120
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700121static inline VkResult layer_create_msg_callback(debug_report_data *debug_data,
122 const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,
123 const VkAllocationCallbacks *pAllocator, VkDebugReportCallbackEXT *pCallback) {
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700124 /* TODO: Use app allocator */
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700125 VkLayerDbgFunctionNode *pNewDbgFuncNode = (VkLayerDbgFunctionNode *)malloc(sizeof(VkLayerDbgFunctionNode));
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600126 if (!pNewDbgFuncNode)
127 return VK_ERROR_OUT_OF_HOST_MEMORY;
128
Tobin Ehliseb7715d2015-09-21 09:36:47 -0600129 // Handle of 0 is logging_callback so use allocated Node address as unique handle
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700130 if (!(*pCallback))
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700131 *pCallback = (VkDebugReportCallbackEXT)pNewDbgFuncNode;
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700132 pNewDbgFuncNode->msgCallback = *pCallback;
133 pNewDbgFuncNode->pfnMsgCallback = pCreateInfo->pfnCallback;
134 pNewDbgFuncNode->msgFlags = pCreateInfo->flags;
135 pNewDbgFuncNode->pUserData = pCreateInfo->pUserData;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600136 pNewDbgFuncNode->pNext = debug_data->g_pDbgFunctionHead;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600137
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600138 debug_data->g_pDbgFunctionHead = pNewDbgFuncNode;
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700139 debug_data->active_flags |= pCreateInfo->flags;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600140
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700141 debug_report_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
142 (uint64_t)*pCallback, 0, VK_DEBUG_REPORT_ERROR_CALLBACK_REF_EXT, "DebugReport", "Added callback");
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600143 return VK_SUCCESS;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600144}
145
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700146static inline void layer_destroy_msg_callback(debug_report_data *debug_data, VkDebugReportCallbackEXT callback,
147 const VkAllocationCallbacks *pAllocator) {
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600148 VkLayerDbgFunctionNode *pTrav = debug_data->g_pDbgFunctionHead;
149 VkLayerDbgFunctionNode *pPrev = pTrav;
Mike Stroyan14a57e72015-07-31 16:20:39 -0600150 bool matched;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600151
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600152 debug_data->active_flags = 0;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600153 while (pTrav) {
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700154 if (pTrav->msgCallback == callback) {
Mike Stroyan14a57e72015-07-31 16:20:39 -0600155 matched = true;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600156 pPrev->pNext = pTrav->pNext;
157 if (debug_data->g_pDbgFunctionHead == pTrav) {
158 debug_data->g_pDbgFunctionHead = pTrav->pNext;
159 }
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700160 debug_report_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
161 (uint64_t)pTrav->msgCallback, 0, VK_DEBUG_REPORT_ERROR_CALLBACK_REF_EXT, "DebugReport",
162 "Destroyed callback");
Mike Stroyan14a57e72015-07-31 16:20:39 -0600163 } else {
164 matched = false;
Mike Stroyan22ff5022015-08-10 16:14:18 -0600165 debug_data->active_flags |= pTrav->msgFlags;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600166 }
167 pPrev = pTrav;
168 pTrav = pTrav->pNext;
Mike Stroyan14a57e72015-07-31 16:20:39 -0600169 if (matched) {
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700170 /* TODO: Use pAllocator */
Mike Stroyan14a57e72015-07-31 16:20:39 -0600171 free(pPrev);
172 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600173 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600174}
175
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700176static inline PFN_vkVoidFunction debug_report_get_instance_proc_addr(debug_report_data *debug_data, const char *funcName) {
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600177 if (!debug_data || !debug_data->g_DEBUG_REPORT) {
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600178 return NULL;
179 }
180
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700181 if (!strcmp(funcName, "vkCreateDebugReportCallbackEXT")) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700182 return (PFN_vkVoidFunction)vkCreateDebugReportCallbackEXT;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600183 }
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700184 if (!strcmp(funcName, "vkDestroyDebugReportCallbackEXT")) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700185 return (PFN_vkVoidFunction)vkDestroyDebugReportCallbackEXT;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600186 }
187
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700188 if (!strcmp(funcName, "vkDebugReportMessageEXT")) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700189 return (PFN_vkVoidFunction)vkDebugReportMessageEXT;
Courtney Goeltzenleuchter822e8d72015-11-30 15:28:25 -0700190 }
191
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600192 return NULL;
193}
194
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600195// This utility (called at vkCreateInstance() time), looks at a pNext chain.
196// It counts any VkDebugReportCallbackCreateInfoEXT structs that it finds. It
197// then allocates an array that can hold that many structs, as well as that
198// many VkDebugReportCallbackEXT handles. It then copies each
199// VkDebugReportCallbackCreateInfoEXT, and initializes each handle.
200static VkResult layer_copy_tmp_callbacks(const void *pChain, uint32_t *num_callbacks, VkDebugReportCallbackCreateInfoEXT **infos,
201 VkDebugReportCallbackEXT **callbacks) {
202 uint32_t n = *num_callbacks = 0;
203
204 const void *pNext = pChain;
205 while (pNext) {
206 // 1st, count the number VkDebugReportCallbackCreateInfoEXT:
207 if (((VkDebugReportCallbackCreateInfoEXT *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT) {
208 n++;
209 }
210 pNext = (void *)((VkDebugReportCallbackCreateInfoEXT *)pNext)->pNext;
211 }
212 if (n == 0) {
213 return VK_SUCCESS;
214 }
215
216 // 2nd, allocate memory for each VkDebugReportCallbackCreateInfoEXT:
217 VkDebugReportCallbackCreateInfoEXT *pInfos = *infos =
218 ((VkDebugReportCallbackCreateInfoEXT *)malloc(n * sizeof(VkDebugReportCallbackCreateInfoEXT)));
219 if (!pInfos) {
220 return VK_ERROR_OUT_OF_HOST_MEMORY;
221 }
222 // 3rd, allocate memory for a unique handle for each callback:
223 VkDebugReportCallbackEXT *pCallbacks = *callbacks = ((VkDebugReportCallbackEXT *)malloc(n * sizeof(VkDebugReportCallbackEXT)));
224 if (!pCallbacks) {
225 free(pInfos);
226 return VK_ERROR_OUT_OF_HOST_MEMORY;
227 }
228 // 4th, copy each VkDebugReportCallbackCreateInfoEXT for use by
229 // vkDestroyInstance, and assign a unique handle to each callback (just
230 // use the address of the copied VkDebugReportCallbackCreateInfoEXT):
231 pNext = pChain;
232 while (pNext) {
233 if (((VkInstanceCreateInfo *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT) {
234 memcpy(pInfos, pNext, sizeof(VkDebugReportCallbackCreateInfoEXT));
235 *pCallbacks++ = (VkDebugReportCallbackEXT)pInfos++;
236 }
237 pNext = (void *)((VkInstanceCreateInfo *)pNext)->pNext;
238 }
239
240 *num_callbacks = n;
241 return VK_SUCCESS;
242}
243
244// This utility frees the arrays allocated by layer_copy_tmp_callbacks()
245static void layer_free_tmp_callbacks(VkDebugReportCallbackCreateInfoEXT *infos, VkDebugReportCallbackEXT *callbacks) {
246 free(infos);
247 free(callbacks);
248}
249
250// This utility enables all of the VkDebugReportCallbackCreateInfoEXT structs
251// that were copied by layer_copy_tmp_callbacks()
252static VkResult layer_enable_tmp_callbacks(debug_report_data *debug_data, uint32_t num_callbacks,
253 VkDebugReportCallbackCreateInfoEXT *infos, VkDebugReportCallbackEXT *callbacks) {
254 VkResult rtn = VK_SUCCESS;
255 for (uint32_t i = 0; i < num_callbacks; i++) {
256 rtn = layer_create_msg_callback(debug_data, &infos[i], NULL, &callbacks[i]);
257 if (rtn != VK_SUCCESS) {
258 for (uint32_t j = 0; j < i; j++) {
259 layer_destroy_msg_callback(debug_data, callbacks[j], NULL);
260 }
261 return rtn;
262 }
263 }
264 return rtn;
265}
266
267// This utility disables all of the VkDebugReportCallbackCreateInfoEXT structs
268// that were copied by layer_copy_tmp_callbacks()
269static void layer_disable_tmp_callbacks(debug_report_data *debug_data, uint32_t num_callbacks,
270 VkDebugReportCallbackEXT *callbacks) {
271 for (uint32_t i = 0; i < num_callbacks; i++) {
272 layer_destroy_msg_callback(debug_data, callbacks[i], NULL);
273 }
274}
275
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600276/*
Courtney Goeltzenleuchter6a564a12015-09-18 16:30:24 -0600277 * Checks if the message will get logged.
278 * Allows layer to defer collecting & formating data if the
279 * message will be discarded.
280 */
Dustin Graves8f1eab92016-04-05 09:41:17 -0600281static inline bool will_log_msg(debug_report_data *debug_data, VkFlags msgFlags) {
Courtney Goeltzenleuchter6a564a12015-09-18 16:30:24 -0600282 if (!debug_data || !(debug_data->active_flags & msgFlags)) {
283 /* message is not wanted */
284 return false;
285 }
286
287 return true;
288}
289
Chris Forbes8a25bce2016-03-24 12:06:35 +1300290#ifdef WIN32
291static inline int vasprintf(char **strp, char const *fmt, va_list ap) {
292 *strp = nullptr;
293 int size = _vscprintf(fmt, ap);
294 if (size >= 0) {
295 *strp = (char *)malloc(size+1);
296 if (!*strp) {
297 return -1;
298 }
299 _vsnprintf(*strp, size+1, fmt, ap);
300 }
301 return size;
302}
303#endif
304
Courtney Goeltzenleuchter6a564a12015-09-18 16:30:24 -0600305/*
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600306 * Output log message via DEBUG_REPORT
307 * Takes format and variable arg list so that output string
308 * is only computed if a message needs to be logged
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600309 */
Michael Lentine010f4692015-11-03 16:19:46 -0800310#ifndef WIN32
Dustin Graves8f1eab92016-04-05 09:41:17 -0600311static inline bool log_msg(debug_report_data *debug_data, VkFlags msgFlags, VkDebugReportObjectTypeEXT objectType,
312 uint64_t srcObject, size_t location, int32_t msgCode, const char *pLayerPrefix, const char *format, ...)
313 __attribute__((format(printf, 8, 9)));
Michael Lentine010f4692015-11-03 16:19:46 -0800314#endif
Dustin Graves8f1eab92016-04-05 09:41:17 -0600315static inline bool log_msg(debug_report_data *debug_data, VkFlags msgFlags, VkDebugReportObjectTypeEXT objectType,
316 uint64_t srcObject, size_t location, int32_t msgCode, const char *pLayerPrefix, const char *format,
317 ...) {
Courtney Goeltzenleuchter61cb70a2015-06-26 15:14:50 -0600318 if (!debug_data || !(debug_data->active_flags & msgFlags)) {
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600319 /* message is not wanted */
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600320 return false;
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600321 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600322
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600323 va_list argptr;
324 va_start(argptr, format);
Chris Forbes8a25bce2016-03-24 12:06:35 +1300325 char *str;
Chris Forbesed6a1b32016-04-28 14:27:19 +1200326 if (-1 == vasprintf(&str, format, argptr)) {
327 /* on failure, glibc vasprintf leaves str undefined */
328 str = nullptr;
329 }
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600330 va_end(argptr);
Chris Forbesed6a1b32016-04-28 14:27:19 +1200331 bool result = debug_report_log_msg(debug_data, msgFlags, objectType, srcObject, location, msgCode, pLayerPrefix,
332 str ? str : "Allocation failure");
Chris Forbes8a25bce2016-03-24 12:06:35 +1300333 free(str);
334 return result;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600335}
336
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700337static inline VKAPI_ATTR VkBool32 VKAPI_CALL log_callback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType, uint64_t srcObject,
338 size_t location, int32_t msgCode, const char *pLayerPrefix,
339 const char *pMsg, void *pUserData) {
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600340 char msg_flags[30];
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600341
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600342 print_msg_flags(msgFlags, msg_flags);
343
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700344 fprintf((FILE *)pUserData, "%s(%s): object: %#" PRIx64 " type: %d location: %lu msgCode: %d: %s\n", pLayerPrefix, msg_flags,
345 srcObject, objType, (unsigned long)location, msgCode, pMsg);
346 fflush((FILE *)pUserData);
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600347
348 return false;
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600349}
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600350
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700351static inline VKAPI_ATTR VkBool32 VKAPI_CALL win32_debug_output_msg(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
352 uint64_t srcObject, size_t location, int32_t msgCode,
353 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600354#ifdef WIN32
355 char msg_flags[30];
356 char buf[2048];
357
358 print_msg_flags(msgFlags, msg_flags);
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700359 _snprintf(buf, sizeof(buf) - 1,
360 "%s (%s): object: 0x%" PRIxPTR " type: %d location: " PRINTF_SIZE_T_SPECIFIER " msgCode: %d: %s\n", pLayerPrefix,
361 msg_flags, (size_t)srcObject, objType, location, msgCode, pMsg);
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600362
363 OutputDebugString(buf);
364#endif
365
366 return false;
367}
368
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600369#endif // LAYER_LOGGING_H