Coverage Report

Created: 2026-09-05 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vulkan-loader/loader/debug_utils.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2015-2021 The Khronos Group Inc.
3
 * Copyright (c) 2015-2021 Valve Corporation
4
 * Copyright (c) 2015-2021 LunarG, Inc.
5
 * Copyright (C) 2015-2016 Google Inc.
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 *
19
 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
20
 * Author: Jon Ashburn <jon@LunarG.com>
21
 * Author: Mark Young <marky@lunarg.com>
22
 * Author: Charles Giessen <charles@lunarg.com>
23
 *
24
 */
25
26
#include <inttypes.h>
27
#include <stdio.h>
28
#include <stdlib.h>
29
#include <string.h>
30
31
#include "vk_object_types.h"
32
33
#include "allocation.h"
34
#include "debug_utils.h"
35
#include "log.h"
36
#include "loader.h"
37
#include "vk_loader_platform.h"
38
39
// VK_EXT_debug_report related items
40
41
VkResult util_CreateDebugUtilsMessenger(struct loader_instance *inst, const VkDebugUtilsMessengerCreateInfoEXT *pCreateInfo,
42
0
                                        const VkAllocationCallbacks *pAllocator, VkDebugUtilsMessengerEXT messenger) {
43
0
    VkLayerDbgFunctionNode *new_dbg_function_node = NULL;
44
45
0
    new_dbg_function_node = (VkLayerDbgFunctionNode *)loader_calloc_with_instance_fallback(
46
0
        pAllocator, inst, sizeof(VkLayerDbgFunctionNode), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
47
48
0
    if (!new_dbg_function_node) {
49
0
        return VK_ERROR_OUT_OF_HOST_MEMORY;
50
0
    }
51
52
0
    new_dbg_function_node->is_messenger = true;
53
0
    new_dbg_function_node->messenger.messenger = messenger;
54
0
    new_dbg_function_node->messenger.pfnUserCallback = pCreateInfo->pfnUserCallback;
55
0
    new_dbg_function_node->messenger.messageSeverity = pCreateInfo->messageSeverity;
56
0
    new_dbg_function_node->messenger.messageType = pCreateInfo->messageType;
57
0
    new_dbg_function_node->pUserData = pCreateInfo->pUserData;
58
0
    new_dbg_function_node->pNext = inst->instance_only_dbg_function_head;
59
0
    inst->instance_only_dbg_function_head = new_dbg_function_node;
60
0
    inst->current_dbg_function_head = inst->instance_only_dbg_function_head;
61
62
0
    return VK_SUCCESS;
63
0
}
64
65
VKAPI_ATTR VkResult VKAPI_CALL debug_utils_CreateDebugUtilsMessengerEXT(VkInstance instance,
66
                                                                        const VkDebugUtilsMessengerCreateInfoEXT *pCreateInfo,
67
                                                                        const VkAllocationCallbacks *pAllocator,
68
0
                                                                        VkDebugUtilsMessengerEXT *pMessenger) {
69
0
    struct loader_instance *inst = loader_get_instance(instance);
70
0
    loader_platform_thread_lock_mutex(&loader_lock);
71
0
    VkResult result = inst->disp->layer_inst_disp.CreateDebugUtilsMessengerEXT(inst->instance, pCreateInfo, pAllocator, pMessenger);
72
0
    loader_platform_thread_unlock_mutex(&loader_lock);
73
0
    return result;
74
0
}
75
76
VkBool32 util_SubmitDebugUtilsMessageEXT(const struct loader_instance *inst, VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
77
                                         VkDebugUtilsMessageTypeFlagsEXT messageTypes,
78
3.16M
                                         const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData) {
79
3.16M
    VkBool32 bail = false;
80
81
3.16M
    if (NULL != pCallbackData) {
82
3.16M
        VkLayerDbgFunctionNode *pTrav = inst->current_dbg_function_head;
83
3.16M
        VkDebugReportObjectTypeEXT object_type = VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT;
84
3.16M
        VkDebugReportFlagsEXT object_flags = 0;
85
3.16M
        uint64_t object_handle = 0;
86
87
3.16M
        debug_utils_AnnotFlagsToReportFlags(messageSeverity, messageTypes, &object_flags);
88
3.16M
        if (0 < pCallbackData->objectCount) {
89
3.16M
            debug_utils_AnnotObjectToDebugReportObject(pCallbackData->pObjects, &object_type, &object_handle);
90
3.16M
        }
91
3.16M
        while (pTrav) {
92
0
            if (pTrav->is_messenger && (pTrav->messenger.messageSeverity & messageSeverity) &&
93
0
                (pTrav->messenger.messageType & messageTypes)) {
94
0
                if (pTrav->messenger.pfnUserCallback(messageSeverity, messageTypes, pCallbackData, pTrav->pUserData)) {
95
0
                    bail = true;
96
0
                }
97
0
            }
98
0
            if (!pTrav->is_messenger && pTrav->report.msgFlags & object_flags) {
99
0
                if (pTrav->report.pfnMsgCallback(object_flags, object_type, object_handle, 0, pCallbackData->messageIdNumber,
100
0
                                                 pCallbackData->pMessageIdName, pCallbackData->pMessage, pTrav->pUserData)) {
101
0
                    bail = true;
102
0
                }
103
0
            }
104
0
            pTrav = pTrav->pNext;
105
0
        }
106
3.16M
    }
107
108
3.16M
    return bail;
109
3.16M
}
110
111
void util_DestroyDebugUtilsMessenger(struct loader_instance *inst, VkDebugUtilsMessengerEXT messenger,
112
0
                                     const VkAllocationCallbacks *pAllocator) {
113
0
    VkLayerDbgFunctionNode *pTrav = inst->current_dbg_function_head;
114
0
    VkLayerDbgFunctionNode *pPrev = pTrav;
115
116
0
    while (pTrav) {
117
0
        if (pTrav->is_messenger && pTrav->messenger.messenger == messenger) {
118
0
            pPrev->pNext = pTrav->pNext;
119
0
            if (inst->current_dbg_function_head == pTrav) inst->current_dbg_function_head = pTrav->pNext;
120
0
            if (inst->instance_only_dbg_function_head == pTrav) inst->instance_only_dbg_function_head = pTrav->pNext;
121
0
            loader_free_with_instance_fallback(pAllocator, inst, pTrav);
122
0
            break;
123
0
        }
124
0
        pPrev = pTrav;
125
0
        pTrav = pTrav->pNext;
126
0
    }
127
0
}
128
129
VkResult util_CreateDebugUtilsMessengers(struct loader_instance *inst, const void *pChain,
130
8.34k
                                         const VkAllocationCallbacks *pAllocator) {
131
8.34k
    const void *pNext = pChain;
132
8.34k
    while (pNext) {
133
0
        VkBaseInStructure in_structure = {0};
134
0
        memcpy(&in_structure, pNext, sizeof(VkBaseInStructure));
135
0
        if (in_structure.sType == VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT) {
136
            // Assign a unique handle to each messenger (just use the address of the VkDebugUtilsMessengerCreateInfoEXT)
137
            // This is only being used this way due to it being for an 'anonymous' callback during instance creation
138
            // NOLINTNEXTLINE(performance-no-int-to-ptr) - non-dispatchable handle; round-trip via uintptr_t is required
139
0
            VkDebugUtilsMessengerEXT messenger_handle = (VkDebugUtilsMessengerEXT)(uintptr_t)pNext;
140
0
            VkResult ret = util_CreateDebugUtilsMessenger(inst, (const VkDebugUtilsMessengerCreateInfoEXT *)pNext, pAllocator,
141
0
                                                          messenger_handle);
142
0
            if (ret != VK_SUCCESS) {
143
0
                return ret;
144
0
            }
145
0
        }
146
0
        pNext = in_structure.pNext;
147
0
    }
148
8.34k
    return VK_SUCCESS;
149
8.34k
}
150
151
VKAPI_ATTR void VKAPI_CALL debug_utils_SubmitDebugUtilsMessageEXT(VkInstance instance,
152
                                                                  VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
153
                                                                  VkDebugUtilsMessageTypeFlagsEXT messageTypes,
154
0
                                                                  const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData) {
155
0
    struct loader_instance *inst = loader_get_instance(instance);
156
157
0
    inst->disp->layer_inst_disp.SubmitDebugUtilsMessageEXT(inst->instance, messageSeverity, messageTypes, pCallbackData);
158
0
}
159
160
VKAPI_ATTR void VKAPI_CALL debug_utils_DestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerEXT messenger,
161
0
                                                                     const VkAllocationCallbacks *pAllocator) {
162
0
    struct loader_instance *inst = loader_get_instance(instance);
163
0
    loader_platform_thread_lock_mutex(&loader_lock);
164
165
0
    inst->disp->layer_inst_disp.DestroyDebugUtilsMessengerEXT(inst->instance, messenger, pAllocator);
166
167
0
    loader_platform_thread_unlock_mutex(&loader_lock);
168
0
}
169
170
// This is the instance chain terminator function for CreateDebugUtilsMessenger
171
VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateDebugUtilsMessengerEXT(VkInstance instance,
172
                                                                       const VkDebugUtilsMessengerCreateInfoEXT *pCreateInfo,
173
                                                                       const VkAllocationCallbacks *pAllocator,
174
0
                                                                       VkDebugUtilsMessengerEXT *pMessenger) {
175
0
    struct loader_instance *inst = (struct loader_instance *)instance;
176
0
    VkResult res = VK_SUCCESS;
177
0
    VkLayerDbgFunctionNode *new_dbg_func_node = NULL;
178
0
    uint32_t next_index = 0;
179
0
    bool index_reserved = false;
180
181
0
    uint32_t *pNextIndex = loader_instance_heap_alloc(inst, sizeof(uint32_t), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
182
0
    if (NULL == pNextIndex) {
183
0
        res = VK_ERROR_OUT_OF_HOST_MEMORY;
184
0
        goto out;
185
0
    }
186
187
0
    res = loader_get_next_available_entry(inst, &inst->debug_utils_messengers_list, &next_index, pAllocator);
188
0
    if (res != VK_SUCCESS) {
189
0
        goto out;
190
0
    }
191
0
    index_reserved = true;
192
193
0
    for (struct loader_icd_term *icd_term = inst->icd_terms; icd_term != NULL; icd_term = icd_term->next) {
194
0
        if (icd_term->debug_utils_messenger_list.list == NULL) {
195
0
            res = loader_init_generic_list(inst, (struct loader_generic_list *)&icd_term->debug_utils_messenger_list,
196
0
                                           sizeof(VkDebugUtilsMessengerEXT));
197
0
            if (res != VK_SUCCESS) {
198
0
                goto out;
199
0
            }
200
0
        } else if (icd_term->debug_utils_messenger_list.capacity <= next_index * sizeof(VkDebugUtilsMessengerEXT)) {
201
0
            res = loader_resize_generic_list(inst, (struct loader_generic_list *)&icd_term->debug_utils_messenger_list);
202
0
            if (res != VK_SUCCESS) {
203
0
                goto out;
204
0
            }
205
0
        }
206
207
0
        if (icd_term->dispatch.CreateDebugUtilsMessengerEXT) {
208
0
            res = icd_term->dispatch.CreateDebugUtilsMessengerEXT(icd_term->instance, pCreateInfo, pAllocator,
209
0
                                                                  &icd_term->debug_utils_messenger_list.list[next_index]);
210
211
0
            if (res != VK_SUCCESS) {
212
0
                goto out;
213
0
            }
214
0
        }
215
0
    }
216
217
    // Setup the debug report callback in the terminator since a layer may want
218
    // to grab the information itself (RenderDoc) and then return back to the
219
    // user callback a sub-set of the messages.
220
0
    new_dbg_func_node = (VkLayerDbgFunctionNode *)loader_calloc_with_instance_fallback(
221
0
        pAllocator, inst, sizeof(VkLayerDbgFunctionNode), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
222
0
    if (!new_dbg_func_node) {
223
0
        res = VK_ERROR_OUT_OF_HOST_MEMORY;
224
0
        goto out;
225
0
    }
226
227
0
    new_dbg_func_node->is_messenger = true;
228
0
    new_dbg_func_node->messenger.pfnUserCallback = pCreateInfo->pfnUserCallback;
229
0
    new_dbg_func_node->messenger.messageSeverity = pCreateInfo->messageSeverity;
230
0
    new_dbg_func_node->messenger.messageType = pCreateInfo->messageType;
231
0
    new_dbg_func_node->pUserData = pCreateInfo->pUserData;
232
0
    new_dbg_func_node->pNext = inst->current_dbg_function_head;
233
0
    inst->current_dbg_function_head = new_dbg_func_node;
234
0
    *pNextIndex = next_index;
235
    // NOLINTNEXTLINE(performance-no-int-to-ptr) - non-dispatchable handle; round-trip via uintptr_t is required
236
0
    *pMessenger = (VkDebugUtilsMessengerEXT)(uintptr_t)pNextIndex;
237
0
    new_dbg_func_node->messenger.messenger = *pMessenger;
238
239
0
out:
240
241
    // Roll back on errors
242
0
    if (VK_SUCCESS != res) {
243
0
        if (index_reserved) {
244
0
            for (struct loader_icd_term *icd_term = inst->icd_terms; icd_term; icd_term = icd_term->next) {
245
0
                if (icd_term->debug_utils_messenger_list.list &&
246
0
                    icd_term->debug_utils_messenger_list.capacity > next_index * sizeof(VkDebugUtilsMessengerEXT) &&
247
0
                    icd_term->debug_utils_messenger_list.list[next_index] &&
248
0
                    NULL != icd_term->dispatch.DestroyDebugUtilsMessengerEXT) {
249
0
                    icd_term->dispatch.DestroyDebugUtilsMessengerEXT(
250
0
                        icd_term->instance, icd_term->debug_utils_messenger_list.list[next_index], pAllocator);
251
0
                }
252
0
            }
253
0
        }
254
0
        if (index_reserved && inst->debug_utils_messengers_list.list &&
255
0
            inst->debug_utils_messengers_list.capacity > next_index * sizeof(struct loader_used_object_status)) {
256
0
            inst->debug_utils_messengers_list.list[next_index].status = VK_FALSE;
257
0
            if (NULL != pAllocator) {
258
0
                inst->debug_utils_messengers_list.list[next_index].allocation_callbacks = *pAllocator;
259
0
            }
260
0
        }
261
0
        loader_free_with_instance_fallback(pAllocator, inst, new_dbg_func_node);
262
0
        loader_free_with_instance_fallback(pAllocator, inst, pNextIndex);
263
0
    }
264
265
0
    return res;
266
0
}
267
268
// This is the instance chain terminator function for DestroyDebugUtilsMessenger
269
VKAPI_ATTR void VKAPI_CALL terminator_DestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerEXT messenger,
270
0
                                                                    const VkAllocationCallbacks *pAllocator) {
271
0
    struct loader_instance *inst = (struct loader_instance *)instance;
272
    // NOLINTNEXTLINE(performance-no-int-to-ptr) - non-dispatchable handle; round-trip via uintptr_t is required
273
0
    uint32_t *debug_messenger_index = (uint32_t *)(uintptr_t)messenger;
274
    // Make sure that messenger actually points to anything
275
0
    if (NULL == debug_messenger_index) {
276
0
        return;
277
0
    }
278
279
0
    for (struct loader_icd_term *icd_term = inst->icd_terms; icd_term; icd_term = icd_term->next) {
280
0
        if (icd_term->debug_utils_messenger_list.list &&
281
0
            icd_term->debug_utils_messenger_list.capacity > *debug_messenger_index * sizeof(VkDebugUtilsMessengerEXT) &&
282
0
            icd_term->debug_utils_messenger_list.list[*debug_messenger_index] &&
283
0
            NULL != icd_term->dispatch.DestroyDebugUtilsMessengerEXT) {
284
0
            icd_term->dispatch.DestroyDebugUtilsMessengerEXT(
285
0
                icd_term->instance, icd_term->debug_utils_messenger_list.list[*debug_messenger_index], pAllocator);
286
0
        }
287
0
    }
288
289
0
    util_DestroyDebugUtilsMessenger(inst, messenger, pAllocator);
290
0
    if (inst->debug_utils_messengers_list.list &&
291
0
        inst->debug_utils_messengers_list.capacity > (*debug_messenger_index) * sizeof(struct loader_used_object_status)) {
292
0
        inst->debug_utils_messengers_list.list[*debug_messenger_index].status = VK_FALSE;
293
0
        if (NULL != pAllocator) {
294
0
            inst->debug_utils_messengers_list.list[*debug_messenger_index].allocation_callbacks = *pAllocator;
295
0
        }
296
0
    }
297
298
0
    loader_free_with_instance_fallback(pAllocator, inst, debug_messenger_index);
299
0
}
300
301
// This is the instance chain terminator function for SubmitDebugUtilsMessageEXT
302
VKAPI_ATTR void VKAPI_CALL terminator_SubmitDebugUtilsMessageEXT(VkInstance instance,
303
                                                                 VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
304
                                                                 VkDebugUtilsMessageTypeFlagsEXT messageTypes,
305
0
                                                                 const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData) {
306
0
    loader_platform_thread_lock_mutex(&loader_lock);
307
    // NOTE: Just make the callback ourselves because there could be one or more ICDs that support this extension
308
    //       and each one will trigger the callback to the user.  This would result in multiple callback triggers
309
    //       per message.  Instead, if we get a messaged up to here, then just trigger the message ourselves and
310
    //       return.  This would still allow the ICDs to trigger their own messages, but won't get any external ones.
311
0
    struct loader_instance *inst = (struct loader_instance *)instance;
312
0
    util_SubmitDebugUtilsMessageEXT(inst, messageSeverity, messageTypes, pCallbackData);
313
0
    loader_platform_thread_unlock_mutex(&loader_lock);
314
0
}
315
316
// VK_EXT_debug_report related items
317
318
VkResult util_CreateDebugReportCallback(struct loader_instance *inst, const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,
319
0
                                        const VkAllocationCallbacks *pAllocator, VkDebugReportCallbackEXT callback) {
320
0
    VkLayerDbgFunctionNode *new_dbg_func_node = NULL;
321
322
0
    new_dbg_func_node = (VkLayerDbgFunctionNode *)loader_calloc_with_instance_fallback(
323
0
        pAllocator, inst, sizeof(VkLayerDbgFunctionNode), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
324
0
    if (!new_dbg_func_node) {
325
0
        return VK_ERROR_OUT_OF_HOST_MEMORY;
326
0
    }
327
328
0
    new_dbg_func_node->is_messenger = false;
329
0
    new_dbg_func_node->report.msgCallback = callback;
330
0
    new_dbg_func_node->report.pfnMsgCallback = pCreateInfo->pfnCallback;
331
0
    new_dbg_func_node->report.msgFlags = pCreateInfo->flags;
332
0
    new_dbg_func_node->pUserData = pCreateInfo->pUserData;
333
0
    new_dbg_func_node->pNext = inst->instance_only_dbg_function_head;
334
0
    inst->instance_only_dbg_function_head = new_dbg_func_node;
335
0
    inst->current_dbg_function_head = inst->instance_only_dbg_function_head;
336
337
0
    return VK_SUCCESS;
338
0
}
339
340
VKAPI_ATTR VkResult VKAPI_CALL debug_utils_CreateDebugReportCallbackEXT(VkInstance instance,
341
                                                                        const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,
342
                                                                        const VkAllocationCallbacks *pAllocator,
343
0
                                                                        VkDebugReportCallbackEXT *pCallback) {
344
0
    struct loader_instance *inst = loader_get_instance(instance);
345
0
    loader_platform_thread_lock_mutex(&loader_lock);
346
0
    VkResult result = inst->disp->layer_inst_disp.CreateDebugReportCallbackEXT(inst->instance, pCreateInfo, pAllocator, pCallback);
347
0
    loader_platform_thread_unlock_mutex(&loader_lock);
348
0
    return result;
349
0
}
350
351
// Utility function to handle reporting
352
VkBool32 util_DebugReportMessage(const struct loader_instance *inst, VkFlags msgFlags, VkDebugReportObjectTypeEXT objectType,
353
0
                                 uint64_t srcObject, size_t location, int32_t msgCode, const char *pLayerPrefix, const char *pMsg) {
354
0
    VkBool32 bail = false;
355
0
    VkLayerDbgFunctionNode *pTrav = inst->current_dbg_function_head;
356
0
    VkDebugUtilsMessageSeverityFlagBitsEXT severity;
357
0
    VkDebugUtilsMessageTypeFlagsEXT types;
358
0
    VkDebugUtilsMessengerCallbackDataEXT callback_data;
359
0
    VkDebugUtilsObjectNameInfoEXT object_name;
360
361
0
    debug_utils_ReportFlagsToAnnotFlags(msgFlags, false, &severity, &types);
362
0
    debug_utils_ReportObjectToAnnotObject(objectType, srcObject, &object_name);
363
364
0
    callback_data.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CALLBACK_DATA_EXT;
365
0
    callback_data.pNext = NULL;
366
0
    callback_data.flags = 0;
367
0
    callback_data.pMessageIdName = pLayerPrefix;
368
0
    callback_data.messageIdNumber = msgCode;
369
0
    callback_data.pMessage = pMsg;
370
0
    callback_data.cmdBufLabelCount = 0;
371
0
    callback_data.pCmdBufLabels = NULL;
372
0
    callback_data.queueLabelCount = 0;
373
0
    callback_data.pQueueLabels = NULL;
374
0
    callback_data.objectCount = 1;
375
0
    callback_data.pObjects = &object_name;
376
377
0
    while (pTrav) {
378
0
        if (!pTrav->is_messenger && pTrav->report.msgFlags & msgFlags) {
379
0
            if (pTrav->report.pfnMsgCallback(msgFlags, objectType, srcObject, location, msgCode, pLayerPrefix, pMsg,
380
0
                                             pTrav->pUserData)) {
381
0
                bail = true;
382
0
            }
383
0
        }
384
0
        if (pTrav->is_messenger && (pTrav->messenger.messageSeverity & severity) && (pTrav->messenger.messageType & types)) {
385
0
            if (pTrav->messenger.pfnUserCallback(severity, types, &callback_data, pTrav->pUserData)) {
386
0
                bail = true;
387
0
            }
388
0
        }
389
390
0
        pTrav = pTrav->pNext;
391
0
    }
392
393
0
    return bail;
394
0
}
395
396
void util_DestroyDebugReportCallback(struct loader_instance *inst, VkDebugReportCallbackEXT callback,
397
0
                                     const VkAllocationCallbacks *pAllocator) {
398
0
    VkLayerDbgFunctionNode *pTrav = inst->current_dbg_function_head;
399
0
    VkLayerDbgFunctionNode *pPrev = pTrav;
400
401
0
    while (pTrav) {
402
0
        if (!pTrav->is_messenger && pTrav->report.msgCallback == callback) {
403
0
            pPrev->pNext = pTrav->pNext;
404
0
            if (inst->current_dbg_function_head == pTrav) inst->current_dbg_function_head = pTrav->pNext;
405
0
            if (inst->instance_only_dbg_function_head == pTrav) inst->instance_only_dbg_function_head = pTrav->pNext;
406
0
            if (inst->current_dbg_function_head == pTrav) inst->current_dbg_function_head = pTrav->pNext;
407
0
            loader_free_with_instance_fallback(pAllocator, inst, pTrav);
408
0
            break;
409
0
        }
410
0
        pPrev = pTrav;
411
0
        pTrav = pTrav->pNext;
412
0
    }
413
0
}
414
415
VkResult util_CreateDebugReportCallbacks(struct loader_instance *inst, const void *pChain,
416
8.34k
                                         const VkAllocationCallbacks *pAllocator) {
417
8.34k
    const void *pNext = pChain;
418
8.34k
    while (pNext) {
419
0
        VkBaseInStructure in_structure = {0};
420
0
        memcpy(&in_structure, pNext, sizeof(VkBaseInStructure));
421
0
        if (in_structure.sType == VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT) {
422
            // Assign a unique handle to each callback (just use the address of the VkDebugReportCallbackCreateInfoEXT):
423
            // This is only being used this way due to it being for an 'anonymous' callback during instance creation
424
            // NOLINTNEXTLINE(performance-no-int-to-ptr) - non-dispatchable handle; round-trip via uintptr_t is required
425
0
            VkDebugReportCallbackEXT report_handle = (VkDebugReportCallbackEXT)(uintptr_t)pNext;
426
0
            VkResult ret =
427
0
                util_CreateDebugReportCallback(inst, (const VkDebugReportCallbackCreateInfoEXT *)pNext, pAllocator, report_handle);
428
0
            if (ret != VK_SUCCESS) {
429
0
                return ret;
430
0
            }
431
0
        }
432
0
        pNext = in_structure.pNext;
433
0
    }
434
8.34k
    return VK_SUCCESS;
435
8.34k
}
436
437
VKAPI_ATTR void VKAPI_CALL debug_utils_DestroyDebugReportCallbackEXT(VkInstance instance, VkDebugReportCallbackEXT callback,
438
0
                                                                     const VkAllocationCallbacks *pAllocator) {
439
0
    struct loader_instance *inst = loader_get_instance(instance);
440
0
    loader_platform_thread_lock_mutex(&loader_lock);
441
442
0
    inst->disp->layer_inst_disp.DestroyDebugReportCallbackEXT(inst->instance, callback, pAllocator);
443
444
0
    loader_platform_thread_unlock_mutex(&loader_lock);
445
0
}
446
447
VKAPI_ATTR void VKAPI_CALL debug_utils_DebugReportMessageEXT(VkInstance instance, VkDebugReportFlagsEXT flags,
448
                                                             VkDebugReportObjectTypeEXT objType, uint64_t object, size_t location,
449
0
                                                             int32_t msgCode, const char *pLayerPrefix, const char *pMsg) {
450
0
    struct loader_instance *inst = loader_get_instance(instance);
451
452
0
    inst->disp->layer_inst_disp.DebugReportMessageEXT(inst->instance, flags, objType, object, location, msgCode, pLayerPrefix,
453
0
                                                      pMsg);
454
0
}
455
456
// This is the instance chain terminator function
457
// for CreateDebugReportCallback
458
VKAPI_ATTR VkResult VKAPI_CALL terminator_CreateDebugReportCallbackEXT(VkInstance instance,
459
                                                                       const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,
460
                                                                       const VkAllocationCallbacks *pAllocator,
461
0
                                                                       VkDebugReportCallbackEXT *pCallback) {
462
0
    struct loader_instance *inst = (struct loader_instance *)instance;
463
0
    VkResult res = VK_SUCCESS;
464
0
    VkLayerDbgFunctionNode *new_dbg_func_node = NULL;
465
0
    uint32_t next_index = 0;
466
0
    bool index_reserved = false;
467
468
0
    uint32_t *pNextIndex = loader_instance_heap_alloc(inst, sizeof(uint32_t), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
469
0
    if (NULL == pNextIndex) {
470
0
        res = VK_ERROR_OUT_OF_HOST_MEMORY;
471
0
        goto out;
472
0
    }
473
474
0
    res = loader_get_next_available_entry(inst, &inst->debug_report_callbacks_list, &next_index, pAllocator);
475
0
    if (res != VK_SUCCESS) {
476
0
        goto out;
477
0
    }
478
0
    index_reserved = true;
479
480
0
    for (struct loader_icd_term *icd_term = inst->icd_terms; icd_term != NULL; icd_term = icd_term->next) {
481
0
        if (icd_term->debug_report_callback_list.list == NULL) {
482
0
            res = loader_init_generic_list(inst, (struct loader_generic_list *)&icd_term->debug_report_callback_list,
483
0
                                           sizeof(VkDebugUtilsMessengerEXT));
484
0
            if (res != VK_SUCCESS) {
485
0
                goto out;
486
0
            }
487
0
        } else if (icd_term->debug_report_callback_list.capacity <= next_index * sizeof(VkDebugReportCallbackEXT)) {
488
0
            res = loader_resize_generic_list(inst, (struct loader_generic_list *)&icd_term->debug_report_callback_list);
489
0
            if (res != VK_SUCCESS) {
490
0
                goto out;
491
0
            }
492
0
        }
493
494
0
        if (icd_term->dispatch.CreateDebugReportCallbackEXT) {
495
0
            res = icd_term->dispatch.CreateDebugReportCallbackEXT(icd_term->instance, pCreateInfo, pAllocator,
496
0
                                                                  &icd_term->debug_report_callback_list.list[next_index]);
497
498
0
            if (res != VK_SUCCESS) {
499
0
                goto out;
500
0
            }
501
0
        }
502
0
    }
503
504
    // Setup the debug report callback in the terminator since a layer may want
505
    // to grab the information itself (RenderDoc) and then return back to the
506
    // user callback a sub-set of the messages.
507
0
    new_dbg_func_node = (VkLayerDbgFunctionNode *)loader_calloc_with_instance_fallback(
508
0
        pAllocator, inst, sizeof(VkLayerDbgFunctionNode), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
509
510
0
    if (!new_dbg_func_node) {
511
0
        res = VK_ERROR_OUT_OF_HOST_MEMORY;
512
0
        goto out;
513
0
    }
514
515
0
    new_dbg_func_node->is_messenger = false;
516
0
    new_dbg_func_node->report.pfnMsgCallback = pCreateInfo->pfnCallback;
517
0
    new_dbg_func_node->report.msgFlags = pCreateInfo->flags;
518
0
    new_dbg_func_node->pUserData = pCreateInfo->pUserData;
519
0
    new_dbg_func_node->pNext = inst->current_dbg_function_head;
520
0
    inst->current_dbg_function_head = new_dbg_func_node;
521
0
    *pNextIndex = next_index;
522
    // NOLINTNEXTLINE(performance-no-int-to-ptr) - non-dispatchable handle; round-trip via uintptr_t is required
523
0
    *pCallback = (VkDebugReportCallbackEXT)(uintptr_t)pNextIndex;
524
0
    new_dbg_func_node->report.msgCallback = *pCallback;
525
526
0
out:
527
528
    // Roll back on errors
529
0
    if (VK_SUCCESS != res) {
530
0
        if (index_reserved) {
531
0
            for (struct loader_icd_term *icd_term = inst->icd_terms; icd_term; icd_term = icd_term->next) {
532
0
                if (icd_term->debug_report_callback_list.list &&
533
0
                    icd_term->debug_report_callback_list.capacity > next_index * sizeof(VkDebugReportCallbackEXT) &&
534
0
                    icd_term->debug_report_callback_list.list[next_index] &&
535
0
                    NULL != icd_term->dispatch.DestroyDebugReportCallbackEXT) {
536
0
                    icd_term->dispatch.DestroyDebugReportCallbackEXT(
537
0
                        icd_term->instance, icd_term->debug_report_callback_list.list[next_index], pAllocator);
538
0
                }
539
0
            }
540
0
        }
541
0
        if (index_reserved && inst->debug_report_callbacks_list.list &&
542
0
            inst->debug_report_callbacks_list.capacity > next_index * sizeof(struct loader_used_object_status)) {
543
0
            inst->debug_report_callbacks_list.list[next_index].status = VK_FALSE;
544
0
            if (NULL != pAllocator) {
545
0
                inst->debug_report_callbacks_list.list[next_index].allocation_callbacks = *pAllocator;
546
0
            }
547
0
        }
548
0
        loader_free_with_instance_fallback(pAllocator, inst, new_dbg_func_node);
549
0
        loader_free_with_instance_fallback(pAllocator, inst, pNextIndex);
550
0
    }
551
552
0
    return res;
553
0
}
554
555
// This is the instance chain terminator function for DestroyDebugReportCallback
556
VKAPI_ATTR void VKAPI_CALL terminator_DestroyDebugReportCallbackEXT(VkInstance instance, VkDebugReportCallbackEXT callback,
557
0
                                                                    const VkAllocationCallbacks *pAllocator) {
558
0
    struct loader_instance *inst = (struct loader_instance *)instance;
559
    // NOLINTNEXTLINE(performance-no-int-to-ptr) - non-dispatchable handle; round-trip via uintptr_t is required
560
0
    uint32_t *debug_report_index = (uint32_t *)(uintptr_t)callback;
561
    // Make sure that callback actually points to anything
562
0
    if (NULL == debug_report_index) {
563
0
        return;
564
0
    }
565
0
    for (struct loader_icd_term *icd_term = inst->icd_terms; icd_term; icd_term = icd_term->next) {
566
0
        if (icd_term->debug_report_callback_list.list &&
567
0
            icd_term->debug_report_callback_list.capacity > *debug_report_index * sizeof(VkDebugReportCallbackEXT) &&
568
0
            icd_term->debug_report_callback_list.list[*debug_report_index] &&
569
0
            NULL != icd_term->dispatch.DestroyDebugReportCallbackEXT) {
570
0
            icd_term->dispatch.DestroyDebugReportCallbackEXT(
571
0
                icd_term->instance, icd_term->debug_report_callback_list.list[*debug_report_index], pAllocator);
572
0
        }
573
0
    }
574
575
0
    util_DestroyDebugReportCallback(inst, callback, pAllocator);
576
0
    if (inst->debug_report_callbacks_list.list &&
577
0
        inst->debug_report_callbacks_list.capacity > (*debug_report_index) * sizeof(struct loader_used_object_status)) {
578
0
        inst->debug_report_callbacks_list.list[*debug_report_index].status = VK_FALSE;
579
0
        if (NULL != pAllocator) {
580
0
            inst->debug_report_callbacks_list.list[*debug_report_index].allocation_callbacks = *pAllocator;
581
0
        }
582
0
    }
583
0
    loader_free_with_instance_fallback(pAllocator, inst, debug_report_index);
584
0
}
585
586
// This is the instance chain terminator function for DebugReportMessage
587
VKAPI_ATTR void VKAPI_CALL terminator_DebugReportMessageEXT(VkInstance instance, VkDebugReportFlagsEXT flags,
588
                                                            VkDebugReportObjectTypeEXT objType, uint64_t object, size_t location,
589
0
                                                            int32_t msgCode, const char *pLayerPrefix, const char *pMsg) {
590
0
    const struct loader_icd_term *icd_term;
591
592
0
    struct loader_instance *inst = (struct loader_instance *)instance;
593
594
0
    loader_platform_thread_lock_mutex(&loader_lock);
595
0
    for (icd_term = inst->icd_terms; icd_term; icd_term = icd_term->next) {
596
0
        if (icd_term->dispatch.DebugReportMessageEXT != NULL) {
597
0
            icd_term->dispatch.DebugReportMessageEXT(icd_term->instance, flags, objType, object, location, msgCode, pLayerPrefix,
598
0
                                                     pMsg);
599
0
        }
600
0
    }
601
602
    // Now that all ICDs have seen the message, call the necessary callbacks.  Ignoring "bail" return value
603
    // as there is nothing to bail from at this point.
604
605
    // NOLINTNEXTLINE(readability-suspicious-call-argument) - objType/object correctly forward to objectType/srcObject in API order
606
0
    util_DebugReportMessage(inst, flags, objType, object, location, msgCode, pLayerPrefix, pMsg);
607
608
0
    loader_platform_thread_unlock_mutex(&loader_lock);
609
0
}
610
611
// General utilities
612
613
const VkExtensionProperties debug_utils_extension_info[] = {
614
    {VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION},
615
    {VK_EXT_DEBUG_UTILS_EXTENSION_NAME, VK_EXT_DEBUG_UTILS_SPEC_VERSION},
616
};
617
618
8.34k
void destroy_debug_callbacks_chain(struct loader_instance *inst, const VkAllocationCallbacks *pAllocator) {
619
8.34k
    VkLayerDbgFunctionNode *pTrav = inst->current_dbg_function_head;
620
8.34k
    VkLayerDbgFunctionNode *pNext = NULL;
621
8.34k
    while (pTrav) {
622
0
        pNext = pTrav->pNext;
623
0
        loader_free_with_instance_fallback(pAllocator, inst, pTrav);
624
0
        pTrav = pNext;
625
0
    }
626
8.34k
    inst->current_dbg_function_head = NULL;
627
8.34k
}
628
629
0
VkResult add_debug_extensions_to_ext_list(const struct loader_instance *inst, struct loader_extension_list *ext_list) {
630
0
    return loader_add_to_ext_list(inst, ext_list, sizeof(debug_utils_extension_info) / sizeof(VkExtensionProperties),
631
0
                                  debug_utils_extension_info);
632
0
}
633
634
0
bool debug_extensions_InstanceGpa(struct loader_instance *ptr_instance, const char *name, void **addr) {
635
0
    bool ret_type = false;
636
637
0
    *addr = NULL;
638
639
0
    if (!strcmp("vkCreateDebugReportCallbackEXT", name)) {
640
0
        *addr = ptr_instance->enabled_extensions.ext_debug_report == 1 ? (void *)debug_utils_CreateDebugReportCallbackEXT : NULL;
641
0
        ret_type = true;
642
0
    } else if (!strcmp("vkDestroyDebugReportCallbackEXT", name)) {
643
0
        *addr = ptr_instance->enabled_extensions.ext_debug_report == 1 ? (void *)debug_utils_DestroyDebugReportCallbackEXT : NULL;
644
0
        ret_type = true;
645
0
    } else if (!strcmp("vkDebugReportMessageEXT", name)) {
646
0
        *addr = ptr_instance->enabled_extensions.ext_debug_report == 1 ? (void *)debug_utils_DebugReportMessageEXT : NULL;
647
0
        return true;
648
0
    }
649
0
    if (!strcmp("vkCreateDebugUtilsMessengerEXT", name)) {
650
0
        *addr = ptr_instance->enabled_extensions.ext_debug_utils == 1 ? (void *)debug_utils_CreateDebugUtilsMessengerEXT : NULL;
651
0
        ret_type = true;
652
0
    } else if (!strcmp("vkDestroyDebugUtilsMessengerEXT", name)) {
653
0
        *addr = ptr_instance->enabled_extensions.ext_debug_utils == 1 ? (void *)debug_utils_DestroyDebugUtilsMessengerEXT : NULL;
654
0
        ret_type = true;
655
0
    } else if (!strcmp("vkSubmitDebugUtilsMessageEXT", name)) {
656
0
        *addr = ptr_instance->enabled_extensions.ext_debug_utils == 1 ? (void *)debug_utils_SubmitDebugUtilsMessageEXT : NULL;
657
0
        ret_type = true;
658
0
    }
659
660
0
    return ret_type;
661
0
}
662
663
bool debug_utils_ReportFlagsToAnnotFlags(VkDebugReportFlagsEXT dr_flags, bool default_flag_is_spec,
664
                                         VkDebugUtilsMessageSeverityFlagBitsEXT *da_severity,
665
0
                                         VkDebugUtilsMessageTypeFlagsEXT *da_type) {
666
0
    bool type_set = false;
667
0
    if (NULL == da_severity || NULL == da_type) {
668
0
        return false;
669
0
    }
670
0
    *da_type = 0;
671
    // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange) - external Vulkan enum has no zero value we can add
672
0
    *da_severity = 0;
673
674
0
    if ((dr_flags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) != 0) {
675
0
        *da_severity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT;
676
0
        *da_type |= VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT;
677
0
        type_set = true;
678
0
    } else if ((dr_flags & (VK_DEBUG_REPORT_WARNING_BIT_EXT | VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT)) != 0) {
679
0
        *da_severity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT;
680
0
    } else if ((dr_flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) != 0) {
681
0
        *da_severity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
682
0
    } else if ((dr_flags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) != 0) {
683
0
        *da_severity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT;
684
0
        *da_type |= VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT;
685
0
        type_set = true;
686
0
    }
687
688
0
    if ((dr_flags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) != 0) {
689
0
        *da_type |= VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
690
0
    } else if (!type_set) {
691
0
        if (default_flag_is_spec) {
692
0
            *da_type |= VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT;
693
0
        } else {
694
0
            *da_type |= VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT;
695
0
        }
696
0
    }
697
698
0
    return true;
699
0
}
700
701
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters) - mirrors VkDebugUtilsMessengerCallbackDataEXT severity/type fields
702
bool debug_utils_AnnotFlagsToReportFlags(VkDebugUtilsMessageSeverityFlagBitsEXT da_severity,
703
3.16M
                                         VkDebugUtilsMessageTypeFlagsEXT da_type, VkDebugReportFlagsEXT *dr_flags) {
704
3.16M
    if (NULL == dr_flags) {
705
0
        return false;
706
0
    }
707
708
3.16M
    *dr_flags = 0;
709
710
3.16M
    if ((da_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) != 0) {
711
63.3k
        *dr_flags |= VK_DEBUG_REPORT_ERROR_BIT_EXT;
712
3.09M
    } else if ((da_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) != 0) {
713
1.55M
        if ((da_type & VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT) != 0) {
714
0
            *dr_flags |= VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
715
1.55M
        } else {
716
1.55M
            *dr_flags |= VK_DEBUG_REPORT_WARNING_BIT_EXT;
717
1.55M
        }
718
1.55M
    } else if ((da_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) != 0) {
719
808k
        *dr_flags |= VK_DEBUG_REPORT_INFORMATION_BIT_EXT;
720
808k
    } else if ((da_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) != 0) {
721
730k
        *dr_flags |= VK_DEBUG_REPORT_DEBUG_BIT_EXT;
722
730k
    }
723
724
3.16M
    return true;
725
3.16M
}
726
727
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters) - mirrors the VkDebugReportCallbackEXT objectType/object parameters
728
bool debug_utils_ReportObjectToAnnotObject(VkDebugReportObjectTypeEXT dr_object_type, uint64_t object_handle,
729
0
                                           VkDebugUtilsObjectNameInfoEXT *da_object_name_info) {
730
0
    if (NULL == da_object_name_info) {
731
0
        return false;
732
0
    }
733
0
    da_object_name_info->sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
734
0
    da_object_name_info->pNext = NULL;
735
0
    da_object_name_info->objectHandle = (uint64_t)(uintptr_t)object_handle;
736
0
    da_object_name_info->pObjectName = NULL;
737
0
    da_object_name_info->objectType = convertDebugReportObjectToCoreObject(dr_object_type);
738
0
    return true;
739
0
}
740
741
bool debug_utils_AnnotObjectToDebugReportObject(const VkDebugUtilsObjectNameInfoEXT *da_object_name_info,
742
3.16M
                                                VkDebugReportObjectTypeEXT *dr_object_type, uint64_t *dr_object_handle) {
743
3.16M
    if (NULL == da_object_name_info || NULL == dr_object_type || NULL == dr_object_handle) {
744
0
        return false;
745
0
    }
746
3.16M
    *dr_object_type = convertCoreObjectToDebugReportObject(da_object_name_info->objectType);
747
3.16M
    *dr_object_handle = da_object_name_info->objectHandle;
748
    return true;
749
3.16M
}