Coverage Report

Created: 2025-11-24 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vulkan-loader/loader/log.c
Line
Count
Source
1
/*
2
 *
3
 * Copyright (c) 2014-2022 The Khronos Group Inc.
4
 * Copyright (c) 2014-2022 Valve Corporation
5
 * Copyright (c) 2014-2022 LunarG, 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: Jon Ashburn <jon@lunarg.com>
20
 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
21
 * Author: Chia-I Wu <olvaffe@gmail.com>
22
 * Author: Chia-I Wu <olv@lunarg.com>
23
 * Author: Mark Lobodzinski <mark@LunarG.com>
24
 * Author: Lenny Komow <lenny@lunarg.com>
25
 * Author: Charles Giessen <charles@lunarg.com>
26
 *
27
 */
28
29
#include "log.h"
30
31
#include <stdio.h>
32
#include <stdarg.h>
33
34
#include "debug_utils.h"
35
#include "loader_common.h"
36
#include "loader_environment.h"
37
#include "settings.h"
38
#include "vk_loader_platform.h"
39
40
uint32_t g_loader_debug = 0;
41
42
2
void loader_init_global_debug_level(void) {
43
2
    char *env, *orig;
44
45
2
    if (g_loader_debug > 0) return;
46
47
2
    g_loader_debug = 0;
48
49
    // Parse comma-separated debug options
50
2
    orig = env = loader_getenv("VK_LOADER_DEBUG", NULL);
51
2
    while (env) {
52
0
        char *p = strchr(env, ',');
53
0
        size_t len;
54
55
0
        if (p) {
56
0
            len = p - env;
57
0
        } else {
58
0
            len = strlen(env);
59
0
        }
60
61
0
        if (len > 0) {
62
0
            if (strncmp(env, "all", len) == 0) {
63
0
                g_loader_debug = ~0u;
64
0
            } else if (strncmp(env, "warn", len) == 0) {
65
0
                g_loader_debug |= VULKAN_LOADER_WARN_BIT;
66
0
            } else if (strncmp(env, "info", len) == 0) {
67
0
                g_loader_debug |= VULKAN_LOADER_INFO_BIT;
68
0
            } else if (strncmp(env, "perf", len) == 0) {
69
0
                g_loader_debug |= VULKAN_LOADER_PERF_BIT;
70
0
            } else if (strncmp(env, "error", len) == 0) {
71
0
                g_loader_debug |= VULKAN_LOADER_ERROR_BIT;
72
0
            } else if (strncmp(env, "debug", len) == 0) {
73
0
                g_loader_debug |= VULKAN_LOADER_DEBUG_BIT;
74
0
            } else if (strncmp(env, "layer", len) == 0) {
75
0
                g_loader_debug |= VULKAN_LOADER_LAYER_BIT;
76
0
            } else if (strncmp(env, "driver", len) == 0 || strncmp(env, "implem", len) == 0 || strncmp(env, "icd", len) == 0) {
77
0
                g_loader_debug |= VULKAN_LOADER_DRIVER_BIT;
78
0
            }
79
0
        }
80
81
0
        if (!p) break;
82
83
0
        env = p + 1;
84
0
    }
85
86
2
    loader_free_getenv(orig, NULL);
87
2
}
88
89
0
void loader_set_global_debug_level(uint32_t new_loader_debug) { g_loader_debug = new_loader_debug; }
90
91
1.60k
void generate_debug_flag_str(VkFlags msg_type, size_t cmd_line_size, char *cmd_line_msg) {
92
1.60k
    cmd_line_msg[0] = '\0';
93
94
1.60k
    if ((msg_type & VULKAN_LOADER_ERROR_BIT) != 0) {
95
64
        loader_strncat(cmd_line_msg, cmd_line_size, "ERROR", sizeof("ERROR"));
96
64
    }
97
1.60k
    if ((msg_type & VULKAN_LOADER_WARN_BIT) != 0) {
98
81
        if (strlen(cmd_line_msg) > 0) {
99
54
            loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
100
54
        }
101
81
        loader_strncat(cmd_line_msg, cmd_line_size, "WARNING", sizeof("WARNING"));
102
81
    }
103
1.60k
    if ((msg_type & VULKAN_LOADER_INFO_BIT) != 0) {
104
101
        if (strlen(cmd_line_msg) > 0) {
105
62
            loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
106
62
        }
107
101
        loader_strncat(cmd_line_msg, cmd_line_size, "INFO", sizeof("INFO"));
108
101
    }
109
1.60k
    if ((msg_type & VULKAN_LOADER_DEBUG_BIT) != 0) {
110
85
        if (strlen(cmd_line_msg) > 0) {
111
77
            loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
112
77
        }
113
85
        loader_strncat(cmd_line_msg, cmd_line_size, "DEBUG", sizeof("DEBUG"));
114
85
    }
115
1.60k
    if ((msg_type & VULKAN_LOADER_PERF_BIT) != 0) {
116
86
        if (strlen(cmd_line_msg) > 0) {
117
70
            loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
118
70
        }
119
86
        loader_strncat(cmd_line_msg, cmd_line_size, "PERF", sizeof("PERF"));
120
86
    }
121
1.60k
    if ((msg_type & VULKAN_LOADER_DRIVER_BIT) != 0) {
122
86
        if (strlen(cmd_line_msg) > 0) {
123
79
            loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
124
79
        }
125
86
        loader_strncat(cmd_line_msg, cmd_line_size, "DRIVER", sizeof("DRIVER"));
126
86
    }
127
1.60k
    if ((msg_type & VULKAN_LOADER_LAYER_BIT) != 0) {
128
83
        if (strlen(cmd_line_msg) > 0) {
129
71
            loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
130
71
        }
131
83
        loader_strncat(cmd_line_msg, cmd_line_size, "LAYER", sizeof("LAYER"));
132
83
    }
133
134
1.60k
#undef STRNCAT_TO_BUFFER
135
1.60k
}
136
137
void DECORATE_PRINTF(4, 5)
138
471k
    loader_log(const struct loader_instance *inst, VkFlags msg_type, int32_t msg_code, const char *format, ...) {
139
471k
    (void)msg_code;
140
471k
    char msg[512] = {0};
141
142
471k
    va_list ap;
143
471k
    va_start(ap, format);
144
471k
    int ret = vsnprintf(msg, sizeof(msg), format, ap);
145
471k
    if ((ret >= (int)sizeof(msg)) || ret < 0) {
146
175
        msg[sizeof(msg) - 1] = '\0';
147
175
    }
148
471k
    va_end(ap);
149
150
471k
    if (inst) {
151
471k
        VkDebugUtilsMessageSeverityFlagBitsEXT severity = 0;
152
471k
        VkDebugUtilsMessageTypeFlagsEXT type = 0;
153
471k
        VkDebugUtilsMessengerCallbackDataEXT callback_data = {0};
154
471k
        VkDebugUtilsObjectNameInfoEXT object_name = {0};
155
156
471k
        if ((msg_type & VULKAN_LOADER_INFO_BIT) != 0) {
157
48.4k
            severity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT;
158
423k
        } else if ((msg_type & VULKAN_LOADER_WARN_BIT) != 0) {
159
234k
            severity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT;
160
234k
        } else if ((msg_type & VULKAN_LOADER_ERROR_BIT) != 0) {
161
14.9k
            severity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
162
174k
        } else if ((msg_type & VULKAN_LOADER_DEBUG_BIT) != 0) {
163
62.1k
            severity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT;
164
112k
        } else if ((msg_type & VULKAN_LOADER_LAYER_BIT) != 0 || (msg_type & VULKAN_LOADER_DRIVER_BIT) != 0) {
165
            // Just driver or just layer bit should be treated as an info message in debug utils.
166
112k
            severity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT;
167
112k
        }
168
169
471k
        if ((msg_type & VULKAN_LOADER_PERF_BIT) != 0) {
170
0
            type = VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
171
471k
        } else if ((msg_type & VULKAN_LOADER_VALIDATION_BIT) != 0) {
172
            // For loader logging, if it's a validation message, we still want to also keep the general flag as well
173
            // so messages of type validation can still be triggered for general message callbacks.
174
0
            type = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT;
175
471k
        } else {
176
471k
            type = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT;
177
471k
        }
178
179
471k
        callback_data.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CALLBACK_DATA_EXT;
180
471k
        callback_data.pMessageIdName = "Loader Message";
181
471k
        callback_data.pMessage = msg;
182
471k
        callback_data.objectCount = 1;
183
471k
        callback_data.pObjects = &object_name;
184
471k
        object_name.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
185
471k
        object_name.objectType = VK_OBJECT_TYPE_INSTANCE;
186
471k
        object_name.objectHandle = (uint64_t)(uintptr_t)inst;
187
188
471k
        util_SubmitDebugUtilsMessageEXT(inst, severity, type, &callback_data);
189
471k
    }
190
191
    // Always log to stderr if this is a fatal error
192
471k
    if (0 == (msg_type & VULKAN_LOADER_FATAL_ERROR_BIT)) {
193
471k
        if (inst && inst->settings.settings_active && inst->settings.debug_level > 0) {
194
            // Exit early if the current instance settings have some debugging options but do match the current msg_type
195
11.8k
            if (0 == (msg_type & inst->settings.debug_level)) {
196
5.79k
                return;
197
5.79k
            }
198
            // Check the global settings and if that doesn't say to skip, check the environment variable
199
460k
        } else if (0 == (msg_type & g_loader_debug)) {
200
460k
            return;
201
460k
        }
202
471k
    }
203
204
#if defined(DEBUG)
205
    int debug_flag_mask =
206
        msg_type & (VULKAN_LOADER_ERROR_BIT | VULKAN_LOADER_WARN_BIT | VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_DEBUG_BIT);
207
    assert((debug_flag_mask == 0 || debug_flag_mask == VULKAN_LOADER_ERROR_BIT || debug_flag_mask == VULKAN_LOADER_WARN_BIT ||
208
            debug_flag_mask == VULKAN_LOADER_INFO_BIT || debug_flag_mask == VULKAN_LOADER_DEBUG_BIT) &&
209
           "This log has more than one exclusive debug flags (error, warn, info, debug) set");
210
#endif
211
212
    // Only need enough space to create the filter description header for log messages
213
    // Also use the same header for all output
214
6.06k
    char cmd_line_msg[64] = {0};
215
6.06k
    size_t cmd_line_size = sizeof(cmd_line_msg);
216
217
6.06k
    loader_strncat(cmd_line_msg, cmd_line_size, "[Vulkan Loader] ", sizeof("[Vulkan Loader] "));
218
219
6.06k
    bool need_separator = false;
220
6.06k
    if ((msg_type & VULKAN_LOADER_ERROR_BIT) != 0) {
221
252
        loader_strncat(cmd_line_msg, cmd_line_size, "ERROR", sizeof("ERROR"));
222
252
        need_separator = true;
223
5.81k
    } else if ((msg_type & VULKAN_LOADER_WARN_BIT) != 0) {
224
1.85k
        loader_strncat(cmd_line_msg, cmd_line_size, "WARNING", sizeof("WARNING"));
225
1.85k
        need_separator = true;
226
3.95k
    } else if ((msg_type & VULKAN_LOADER_INFO_BIT) != 0) {
227
1.37k
        loader_strncat(cmd_line_msg, cmd_line_size, "INFO", sizeof("INFO"));
228
1.37k
        need_separator = true;
229
2.58k
    } else if ((msg_type & VULKAN_LOADER_DEBUG_BIT) != 0) {
230
2.55k
        loader_strncat(cmd_line_msg, cmd_line_size, "DEBUG", sizeof("DEBUG"));
231
2.55k
        need_separator = true;
232
2.55k
    }
233
234
6.06k
    if ((msg_type & VULKAN_LOADER_PERF_BIT) != 0) {
235
0
        if (need_separator) {
236
0
            loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
237
0
        }
238
0
        loader_strncat(cmd_line_msg, cmd_line_size, "PERF", sizeof("PERF"));
239
6.06k
    } else if ((msg_type & VULKAN_LOADER_DRIVER_BIT) != 0) {
240
11
        if (need_separator) {
241
1
            loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
242
1
        }
243
11
        loader_strncat(cmd_line_msg, cmd_line_size, "DRIVER", sizeof("DRIVER"));
244
6.05k
    } else if ((msg_type & VULKAN_LOADER_LAYER_BIT) != 0) {
245
1.28k
        if (need_separator) {
246
1.26k
            loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
247
1.26k
        }
248
1.28k
        loader_strncat(cmd_line_msg, cmd_line_size, "LAYER", sizeof("LAYER"));
249
1.28k
    }
250
251
6.06k
    loader_strncat(cmd_line_msg, cmd_line_size, ": ", sizeof(": "));
252
6.06k
    size_t num_used = strlen(cmd_line_msg);
253
254
    // Justifies the output to at least 29 spaces
255
6.06k
    if (num_used < 32) {
256
5.87k
        const char space_buffer[] = "                                ";
257
        // Only write (32 - num_used) spaces
258
5.87k
        loader_strncat(cmd_line_msg, cmd_line_size, space_buffer, sizeof(space_buffer) - 1 - num_used);
259
5.87k
    }
260
    // Assert that we didn't write more than what is available in cmd_line_msg
261
6.06k
    assert(cmd_line_size > num_used);
262
263
    //fputs(cmd_line_msg, stderr);
264
    //fputs(msg, stderr);
265
    //fputc('\n', stderr);
266
#if defined(WIN32)
267
    OutputDebugString(cmd_line_msg);
268
    OutputDebugString(msg);
269
    OutputDebugString("\n");
270
#endif
271
6.06k
}
272
273
void loader_log_asm_function_not_supported(const struct loader_instance *inst, VkFlags msg_type, int32_t msg_code,
274
0
                                           const char *func_name) {
275
0
    loader_log(inst, msg_type, msg_code, "Function %s not supported for this physical device", func_name);
276
0
}
277
278
0
void loader_log_generate_uuid_string(const uint8_t uuid[16], char output[UUID_STR_LEN]) {
279
0
    assert(uuid && output);
280
0
    snprintf(output, UUID_STR_LEN, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", uuid[0], uuid[1],
281
0
             uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7], uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13],
282
0
             uuid[14], uuid[15]);
283
0
}