blob: 80192b0e5d5cee0fb97237dbc99f76930aa0d451 [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"
Mark Lobodzinskic9d81652017-08-10 11:01:17 -060031#include <signal.h>
Karl Schultzd7f37542016-05-10 11:36:08 -060032#include <cinttypes>
Tobin Ehlis2d9deec2016-04-21 14:19:26 -060033#include <stdarg.h>
34#include <stdbool.h>
35#include <stdio.h>
36#include <unordered_map>
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060037#include <vector>
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060038
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -060039typedef struct _debug_report_data {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060040 VkLayerDbgFunctionNode *debug_callback_list;
41 VkLayerDbgFunctionNode *default_debug_callback_list;
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -060042 VkFlags active_flags;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060043 bool g_DEBUG_REPORT;
Tony Barbour3431dac2017-06-19 16:50:37 -060044 std::unordered_map<uint64_t, std::string> *debugObjectNameMap;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -060045} debug_report_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060046
Tobin Ehlis8d6acde2017-02-08 07:40:40 -070047template debug_report_data *GetLayerDataPtr<debug_report_data>(void *data_key,
Jon Ashburn5484e0c2016-03-08 17:48:44 -070048 std::unordered_map<void *, debug_report_data *> &data_map);
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -060049
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060050// Forward Declarations
51static inline bool debug_report_log_msg(const debug_report_data *debug_data, VkFlags msgFlags,
52 VkDebugReportObjectTypeEXT objectType, uint64_t srcObject, size_t location, int32_t msgCode,
53 const char *pLayerPrefix, const char *pMsg);
54
55// Add a debug message callback node structure to the specified callback linked list
56static inline void AddDebugMessageCallback(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head,
57 VkLayerDbgFunctionNode *new_node) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060058 new_node->pNext = *list_head;
59 *list_head = new_node;
60}
61
62// Remove specified debug message callback node structure from the specified callback linked list
63static inline void RemoveDebugMessageCallback(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head,
64 VkDebugReportCallbackEXT callback) {
65 VkLayerDbgFunctionNode *cur_callback = *list_head;
66 VkLayerDbgFunctionNode *prev_callback = cur_callback;
67 bool matched = false;
Mark Lobodzinski05b04cd2016-11-10 09:12:57 -070068 VkFlags local_flags = 0;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060069
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060070 while (cur_callback) {
71 if (cur_callback->msgCallback == callback) {
72 matched = true;
73 prev_callback->pNext = cur_callback->pNext;
74 if (*list_head == cur_callback) {
75 *list_head = cur_callback->pNext;
76 }
77 debug_report_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
Mark Lobodzinski863d5de2017-05-22 10:10:07 -060078 reinterpret_cast<uint64_t &>(cur_callback->msgCallback), 0, 0, "DebugReport",
79 "Destroyed callback\n");
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060080 } else {
81 matched = false;
Mark Lobodzinski05b04cd2016-11-10 09:12:57 -070082 local_flags |= cur_callback->msgFlags;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060083 }
84 prev_callback = cur_callback;
85 cur_callback = cur_callback->pNext;
86 if (matched) {
87 free(prev_callback);
88 }
89 }
Mark Lobodzinski05b04cd2016-11-10 09:12:57 -070090 debug_data->active_flags = local_flags;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -060091}
92
93// Removes all debug callback function nodes from the specified callback linked lists and frees their resources
94static inline void RemoveAllMessageCallbacks(debug_report_data *debug_data, VkLayerDbgFunctionNode **list_head) {
95 VkLayerDbgFunctionNode *current_callback = *list_head;
96 VkLayerDbgFunctionNode *prev_callback = current_callback;
97
98 while (current_callback) {
99 prev_callback = current_callback->pNext;
100 debug_report_log_msg(debug_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
Mark Lobodzinski863d5de2017-05-22 10:10:07 -0600101 (uint64_t)current_callback->msgCallback, 0, 0, "DebugReport",
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600102 "Debug Report callbacks not removed before DestroyInstance");
103 free(current_callback);
104 current_callback = prev_callback;
105 }
106 *list_head = NULL;
107}
108
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600109// Utility function to handle reporting
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600110static inline bool debug_report_log_msg(const debug_report_data *debug_data, VkFlags msgFlags,
111 VkDebugReportObjectTypeEXT objectType, uint64_t srcObject, size_t location, int32_t msgCode,
112 const char *pLayerPrefix, const char *pMsg) {
Dustin Graves8f1eab92016-04-05 09:41:17 -0600113 bool bail = false;
Mark Lobodzinski55a197f2016-06-21 15:54:57 -0600114 VkLayerDbgFunctionNode *pTrav = NULL;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600115
Mark Lobodzinski55a197f2016-06-21 15:54:57 -0600116 if (debug_data->debug_callback_list != NULL) {
117 pTrav = debug_data->debug_callback_list;
118 } else {
119 pTrav = debug_data->default_debug_callback_list;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600120 }
121
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600122 while (pTrav) {
123 if (pTrav->msgFlags & msgFlags) {
Tony Barbour3431dac2017-06-19 16:50:37 -0600124 auto it = debug_data->debugObjectNameMap->find(srcObject);
125 if (it == debug_data->debugObjectNameMap->end()) {
126 if (pTrav->pfnMsgCallback(msgFlags, objectType, srcObject, location, msgCode, pLayerPrefix, pMsg,
127 pTrav->pUserData)) {
128 bail = true;
129 }
130 } else {
131 std::string newMsg = "SrcObject name = ";
132 newMsg.append(it->second.c_str());
133 newMsg.append(" ");
134 newMsg.append(pMsg);
135 if (pTrav->pfnMsgCallback(msgFlags, objectType, srcObject, location, msgCode, pLayerPrefix, newMsg.c_str(),
136 pTrav->pUserData)) {
137 bail = true;
138 }
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600139 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600140 }
141 pTrav = pTrav->pNext;
142 }
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600143
144 return bail;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600145}
146
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700147static inline debug_report_data *debug_report_create_instance(
148 VkLayerInstanceDispatchTable *table, VkInstance inst, uint32_t extension_count,
149 const char *const *ppEnabledExtensions) // layer or extension name to be enabled
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600150{
Mark Youngaa1aa3a2016-07-05 16:41:50 -0600151 debug_report_data *debug_data = (debug_report_data *)malloc(sizeof(debug_report_data));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700152 if (!debug_data) return NULL;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600153
154 memset(debug_data, 0, sizeof(debug_report_data));
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600155 for (uint32_t i = 0; i < extension_count; i++) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600156 // TODO: Check other property fields
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700157 if (strcmp(ppEnabledExtensions[i], VK_EXT_DEBUG_REPORT_EXTENSION_NAME) == 0) {
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600158 debug_data->g_DEBUG_REPORT = true;
159 }
160 }
Tony Barbour3431dac2017-06-19 16:50:37 -0600161 debug_data->debugObjectNameMap = new std::unordered_map<uint64_t, std::string>;
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600162 return debug_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600163}
164
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700165static inline void layer_debug_report_destroy_instance(debug_report_data *debug_data) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600166 if (debug_data) {
167 RemoveAllMessageCallbacks(debug_data, &debug_data->default_debug_callback_list);
168 RemoveAllMessageCallbacks(debug_data, &debug_data->debug_callback_list);
Tony Barbour3431dac2017-06-19 16:50:37 -0600169 delete debug_data->debugObjectNameMap;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600170 free(debug_data);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600171 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600172}
173
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700174static inline debug_report_data *layer_debug_report_create_device(debug_report_data *instance_debug_data, VkDevice device) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600175 // DEBUG_REPORT shares data between Instance and Device,
176 // so just return instance's data pointer
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600177 return instance_debug_data;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600178}
179
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600180static inline void layer_debug_report_destroy_device(VkDevice device) {
181 // Nothing to do since we're using instance data record
182}
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600183
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600184static inline void layer_destroy_msg_callback(debug_report_data *debug_data, VkDebugReportCallbackEXT callback,
185 const VkAllocationCallbacks *pAllocator) {
186 RemoveDebugMessageCallback(debug_data, &debug_data->debug_callback_list, callback);
187 RemoveDebugMessageCallback(debug_data, &debug_data->default_debug_callback_list, callback);
188}
189
190static inline VkResult layer_create_msg_callback(debug_report_data *debug_data, bool default_callback,
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700191 const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,
192 const VkAllocationCallbacks *pAllocator, VkDebugReportCallbackEXT *pCallback) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700193 VkLayerDbgFunctionNode *pNewDbgFuncNode = (VkLayerDbgFunctionNode *)malloc(sizeof(VkLayerDbgFunctionNode));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700194 if (!pNewDbgFuncNode) return VK_ERROR_OUT_OF_HOST_MEMORY;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600195
Tobin Ehliseb7715d2015-09-21 09:36:47 -0600196 // Handle of 0 is logging_callback so use allocated Node address as unique handle
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700197 if (!(*pCallback)) *pCallback = (VkDebugReportCallbackEXT)pNewDbgFuncNode;
Courtney Goeltzenleuchter05854bf2015-11-30 12:13:14 -0700198 pNewDbgFuncNode->msgCallback = *pCallback;
199 pNewDbgFuncNode->pfnMsgCallback = pCreateInfo->pfnCallback;
200 pNewDbgFuncNode->msgFlags = pCreateInfo->flags;
201 pNewDbgFuncNode->pUserData = pCreateInfo->pUserData;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600202
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600203 if (default_callback) {
204 AddDebugMessageCallback(debug_data, &debug_data->default_debug_callback_list, pNewDbgFuncNode);
Mark Lobodzinski2134fc32016-11-10 09:10:08 -0700205 debug_data->active_flags |= pCreateInfo->flags;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600206 } else {
207 AddDebugMessageCallback(debug_data, &debug_data->debug_callback_list, pNewDbgFuncNode);
Mark Lobodzinski2134fc32016-11-10 09:10:08 -0700208 debug_data->active_flags = pCreateInfo->flags;
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600209 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600210
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700211 debug_report_log_msg(debug_data, VK_DEBUG_REPORT_DEBUG_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT,
Mark Lobodzinski863d5de2017-05-22 10:10:07 -0600212 (uint64_t)*pCallback, 0, 0, "DebugReport", "Added callback");
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600213 return VK_SUCCESS;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600214}
215
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700216static inline PFN_vkVoidFunction debug_report_get_instance_proc_addr(debug_report_data *debug_data, const char *funcName) {
Courtney Goeltzenleuchtere45acec2015-06-14 12:03:26 -0600217 if (!debug_data || !debug_data->g_DEBUG_REPORT) {
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600218 return NULL;
219 }
220
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700221 if (!strcmp(funcName, "vkCreateDebugReportCallbackEXT")) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700222 return (PFN_vkVoidFunction)vkCreateDebugReportCallbackEXT;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600223 }
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700224 if (!strcmp(funcName, "vkDestroyDebugReportCallbackEXT")) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700225 return (PFN_vkVoidFunction)vkDestroyDebugReportCallbackEXT;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600226 }
Courtney Goeltzenleuchter7415d5a2015-12-09 15:48:16 -0700227 if (!strcmp(funcName, "vkDebugReportMessageEXT")) {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700228 return (PFN_vkVoidFunction)vkDebugReportMessageEXT;
Courtney Goeltzenleuchter822e8d72015-11-30 15:28:25 -0700229 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600230 return NULL;
231}
232
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600233// This utility (called at vkCreateInstance() time), looks at a pNext chain.
234// It counts any VkDebugReportCallbackCreateInfoEXT structs that it finds. It
235// then allocates an array that can hold that many structs, as well as that
236// many VkDebugReportCallbackEXT handles. It then copies each
237// VkDebugReportCallbackCreateInfoEXT, and initializes each handle.
Mark Lobodzinski1bcbdf92018-02-07 10:00:22 -0700238static inline VkResult layer_copy_tmp_callbacks(const void *pChain, uint32_t *num_callbacks,
239 VkDebugReportCallbackCreateInfoEXT **infos, VkDebugReportCallbackEXT **callbacks) {
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600240 uint32_t n = *num_callbacks = 0;
241
242 const void *pNext = pChain;
243 while (pNext) {
244 // 1st, count the number VkDebugReportCallbackCreateInfoEXT:
245 if (((VkDebugReportCallbackCreateInfoEXT *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT) {
246 n++;
247 }
248 pNext = (void *)((VkDebugReportCallbackCreateInfoEXT *)pNext)->pNext;
249 }
250 if (n == 0) {
251 return VK_SUCCESS;
252 }
253
254 // 2nd, allocate memory for each VkDebugReportCallbackCreateInfoEXT:
255 VkDebugReportCallbackCreateInfoEXT *pInfos = *infos =
256 ((VkDebugReportCallbackCreateInfoEXT *)malloc(n * sizeof(VkDebugReportCallbackCreateInfoEXT)));
257 if (!pInfos) {
258 return VK_ERROR_OUT_OF_HOST_MEMORY;
259 }
260 // 3rd, allocate memory for a unique handle for each callback:
261 VkDebugReportCallbackEXT *pCallbacks = *callbacks = ((VkDebugReportCallbackEXT *)malloc(n * sizeof(VkDebugReportCallbackEXT)));
262 if (!pCallbacks) {
263 free(pInfos);
264 return VK_ERROR_OUT_OF_HOST_MEMORY;
265 }
266 // 4th, copy each VkDebugReportCallbackCreateInfoEXT for use by
267 // vkDestroyInstance, and assign a unique handle to each callback (just
268 // use the address of the copied VkDebugReportCallbackCreateInfoEXT):
269 pNext = pChain;
270 while (pNext) {
271 if (((VkInstanceCreateInfo *)pNext)->sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT) {
272 memcpy(pInfos, pNext, sizeof(VkDebugReportCallbackCreateInfoEXT));
273 *pCallbacks++ = (VkDebugReportCallbackEXT)pInfos++;
274 }
275 pNext = (void *)((VkInstanceCreateInfo *)pNext)->pNext;
276 }
277
278 *num_callbacks = n;
279 return VK_SUCCESS;
280}
281
282// This utility frees the arrays allocated by layer_copy_tmp_callbacks()
Mark Lobodzinski1bcbdf92018-02-07 10:00:22 -0700283static inline void layer_free_tmp_callbacks(VkDebugReportCallbackCreateInfoEXT *infos, VkDebugReportCallbackEXT *callbacks) {
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600284 free(infos);
285 free(callbacks);
286}
287
288// This utility enables all of the VkDebugReportCallbackCreateInfoEXT structs
289// that were copied by layer_copy_tmp_callbacks()
Mark Lobodzinski1bcbdf92018-02-07 10:00:22 -0700290static inline VkResult layer_enable_tmp_callbacks(debug_report_data *debug_data, uint32_t num_callbacks,
291 VkDebugReportCallbackCreateInfoEXT *infos, VkDebugReportCallbackEXT *callbacks) {
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600292 VkResult rtn = VK_SUCCESS;
293 for (uint32_t i = 0; i < num_callbacks; i++) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600294 rtn = layer_create_msg_callback(debug_data, false, &infos[i], NULL, &callbacks[i]);
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600295 if (rtn != VK_SUCCESS) {
296 for (uint32_t j = 0; j < i; j++) {
297 layer_destroy_msg_callback(debug_data, callbacks[j], NULL);
298 }
299 return rtn;
300 }
301 }
302 return rtn;
303}
304
305// This utility disables all of the VkDebugReportCallbackCreateInfoEXT structs
306// that were copied by layer_copy_tmp_callbacks()
Mark Lobodzinski1bcbdf92018-02-07 10:00:22 -0700307static inline void layer_disable_tmp_callbacks(debug_report_data *debug_data, uint32_t num_callbacks,
308 VkDebugReportCallbackEXT *callbacks) {
Ian Elliotted6b5ac2016-04-28 09:08:13 -0600309 for (uint32_t i = 0; i < num_callbacks; i++) {
310 layer_destroy_msg_callback(debug_data, callbacks[i], NULL);
311 }
312}
313
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600314// Checks if the message will get logged.
315// Allows layer to defer collecting & formating data if the
316// message will be discarded.
Dustin Graves8f1eab92016-04-05 09:41:17 -0600317static inline bool will_log_msg(debug_report_data *debug_data, VkFlags msgFlags) {
Courtney Goeltzenleuchter6a564a12015-09-18 16:30:24 -0600318 if (!debug_data || !(debug_data->active_flags & msgFlags)) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600319 // Message is not wanted
Courtney Goeltzenleuchter6a564a12015-09-18 16:30:24 -0600320 return false;
321 }
322
323 return true;
324}
John Zulauf6664e272018-01-17 11:00:22 -0700325#ifndef WIN32
Mark Lobodzinski1bcbdf92018-02-07 10:00:22 -0700326static inline int string_sprintf(std::string *output, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
John Zulauf6664e272018-01-17 11:00:22 -0700327#endif
Mark Lobodzinski1bcbdf92018-02-07 10:00:22 -0700328static inline int string_sprintf(std::string *output, const char *fmt, ...) {
John Zulauf6664e272018-01-17 11:00:22 -0700329 std::string &formatted = *output;
330 va_list argptr;
331 va_start(argptr, fmt);
332 int reserve = vsnprintf(nullptr, 0, fmt, argptr);
333 va_end(argptr);
334 formatted.reserve(reserve + 1);
335 va_start(argptr, fmt);
336 int result = vsnprintf((char *)formatted.data(), formatted.capacity(), fmt, argptr);
337 va_end(argptr);
338 assert(result == reserve);
339 return result;
340}
Courtney Goeltzenleuchter6a564a12015-09-18 16:30:24 -0600341
Chris Forbes8a25bce2016-03-24 12:06:35 +1300342#ifdef WIN32
343static inline int vasprintf(char **strp, char const *fmt, va_list ap) {
344 *strp = nullptr;
345 int size = _vscprintf(fmt, ap);
346 if (size >= 0) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700347 *strp = (char *)malloc(size + 1);
Chris Forbes8a25bce2016-03-24 12:06:35 +1300348 if (!*strp) {
349 return -1;
350 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700351 _vsnprintf(*strp, size + 1, fmt, ap);
Chris Forbes8a25bce2016-03-24 12:06:35 +1300352 }
353 return size;
354}
355#endif
356
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600357// Output log message via DEBUG_REPORT
358// Takes format and variable arg list so that output string
359// is only computed if a message needs to be logged
Michael Lentine010f4692015-11-03 16:19:46 -0800360#ifndef WIN32
Tobin Ehlis17978d82016-05-17 07:58:01 -0600361static inline bool log_msg(const debug_report_data *debug_data, VkFlags msgFlags, VkDebugReportObjectTypeEXT objectType,
Dustin Graves8f1eab92016-04-05 09:41:17 -0600362 uint64_t srcObject, size_t location, int32_t msgCode, const char *pLayerPrefix, const char *format, ...)
363 __attribute__((format(printf, 8, 9)));
Michael Lentine010f4692015-11-03 16:19:46 -0800364#endif
Tobin Ehlis17978d82016-05-17 07:58:01 -0600365static inline bool log_msg(const debug_report_data *debug_data, VkFlags msgFlags, VkDebugReportObjectTypeEXT objectType,
Dustin Graves8f1eab92016-04-05 09:41:17 -0600366 uint64_t srcObject, size_t location, int32_t msgCode, const char *pLayerPrefix, const char *format,
367 ...) {
Courtney Goeltzenleuchter61cb70a2015-06-26 15:14:50 -0600368 if (!debug_data || !(debug_data->active_flags & msgFlags)) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600369 // Message is not wanted
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600370 return false;
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600371 }
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600372
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600373 va_list argptr;
374 va_start(argptr, format);
Chris Forbes8a25bce2016-03-24 12:06:35 +1300375 char *str;
Chris Forbesed6a1b32016-04-28 14:27:19 +1200376 if (-1 == vasprintf(&str, format, argptr)) {
Mark Lobodzinski97c4d512016-05-19 15:27:18 -0600377 // On failure, glibc vasprintf leaves str undefined
Chris Forbesed6a1b32016-04-28 14:27:19 +1200378 str = nullptr;
379 }
Courtney Goeltzenleuchterb9f0bf32015-06-14 09:50:18 -0600380 va_end(argptr);
Chris Forbesed6a1b32016-04-28 14:27:19 +1200381 bool result = debug_report_log_msg(debug_data, msgFlags, objectType, srcObject, location, msgCode, pLayerPrefix,
382 str ? str : "Allocation failure");
Chris Forbes8a25bce2016-03-24 12:06:35 +1300383 free(str);
384 return result;
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600385}
386
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700387static inline VKAPI_ATTR VkBool32 VKAPI_CALL log_callback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType, uint64_t srcObject,
388 size_t location, int32_t msgCode, const char *pLayerPrefix,
389 const char *pMsg, void *pUserData) {
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600390 char msg_flags[30];
Courtney Goeltzenleuchter7d8503b2015-06-11 15:58:51 -0600391
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600392 print_msg_flags(msgFlags, msg_flags);
393
Mark Muelleraab36502016-05-03 13:17:29 -0600394 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 -0700395 srcObject, objType, (unsigned long)location, msgCode, pMsg);
396 fflush((FILE *)pUserData);
Courtney Goeltzenleuchter06640832015-09-04 13:52:24 -0600397
398 return false;
Courtney Goeltzenleuchter2f25bb42015-06-14 11:29:24 -0600399}
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600400
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700401static inline VKAPI_ATTR VkBool32 VKAPI_CALL win32_debug_output_msg(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
402 uint64_t srcObject, size_t location, int32_t msgCode,
403 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600404#ifdef WIN32
405 char msg_flags[30];
406 char buf[2048];
407
408 print_msg_flags(msgFlags, msg_flags);
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700409 _snprintf(buf, sizeof(buf) - 1,
410 "%s (%s): object: 0x%" PRIxPTR " type: %d location: " PRINTF_SIZE_T_SPECIFIER " msgCode: %d: %s\n", pLayerPrefix,
411 msg_flags, (size_t)srcObject, objType, location, msgCode, pMsg);
Courtney Goeltzenleuchter5907ac42015-10-05 14:41:34 -0600412
413 OutputDebugString(buf);
414#endif
415
416 return false;
417}
418
Mark Lobodzinskic9d81652017-08-10 11:01:17 -0600419static inline VKAPI_ATTR VkBool32 VKAPI_CALL DebugBreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
420 uint64_t srcObject, size_t location, int32_t msgCode,
421 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
422#ifdef WIN32
423 DebugBreak();
424#else
425 raise(SIGTRAP);
426#endif
427
428 return false;
429}
430
431
Mark Lobodzinski863d5de2017-05-22 10:10:07 -0600432// TODO: Could be autogenerated for the specific handles for extra type safety...
433template <typename HANDLE_T>
434static inline uint64_t HandleToUint64(HANDLE_T *h) {
435 return reinterpret_cast<uint64_t>(h);
Petr Krause9388f62017-05-13 20:53:12 +0200436}
437
Mark Lobodzinski863d5de2017-05-22 10:10:07 -0600438template <typename HANDLE_T>
439uint64_t HandleToUint64(HANDLE_T h);
440
441static inline uint64_t HandleToUint64(uint64_t h) { return h; }
442
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700443#endif // LAYER_LOGGING_H