Coverage Report

Created: 2025-10-10 06:38

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.77k
void generate_debug_flag_str(VkFlags msg_type, size_t cmd_line_size, char *cmd_line_msg) {
92
1.77k
    cmd_line_msg[0] = '\0';
93
94
1.77k
    if ((msg_type & VULKAN_LOADER_ERROR_BIT) != 0) {
95
95
        loader_strncat(cmd_line_msg, cmd_line_size, "ERROR", sizeof("ERROR"));
96
95
    }
97
1.77k
    if ((msg_type & VULKAN_LOADER_WARN_BIT) != 0) {
98
118
        if (strlen(cmd_line_msg) > 0) {
99
85
            loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
100
85
        }
101
118
        loader_strncat(cmd_line_msg, cmd_line_size, "WARNING", sizeof("WARNING"));
102
118
    }
103
1.77k
    if ((msg_type & VULKAN_LOADER_INFO_BIT) != 0) {
104
154
        if (strlen(cmd_line_msg) > 0) {
105
93
            loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
106
93
        }
107
154
        loader_strncat(cmd_line_msg, cmd_line_size, "INFO", sizeof("INFO"));
108
154
    }
109
1.77k
    if ((msg_type & VULKAN_LOADER_DEBUG_BIT) != 0) {
110
129
        if (strlen(cmd_line_msg) > 0) {
111
125
            loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
112
125
        }
113
129
        loader_strncat(cmd_line_msg, cmd_line_size, "DEBUG", sizeof("DEBUG"));
114
129
    }
115
1.77k
    if ((msg_type & VULKAN_LOADER_PERF_BIT) != 0) {
116
132
        if (strlen(cmd_line_msg) > 0) {
117
111
            loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
118
111
        }
119
132
        loader_strncat(cmd_line_msg, cmd_line_size, "PERF", sizeof("PERF"));
120
132
    }
121
1.77k
    if ((msg_type & VULKAN_LOADER_DRIVER_BIT) != 0) {
122
120
        if (strlen(cmd_line_msg) > 0) {
123
115
            loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
124
115
        }
125
120
        loader_strncat(cmd_line_msg, cmd_line_size, "DRIVER", sizeof("DRIVER"));
126
120
    }
127
1.77k
    if ((msg_type & VULKAN_LOADER_LAYER_BIT) != 0) {
128
116
        if (strlen(cmd_line_msg) > 0) {
129
112
            loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
130
112
        }
131
116
        loader_strncat(cmd_line_msg, cmd_line_size, "LAYER", sizeof("LAYER"));
132
116
    }
133
134
1.77k
#undef STRNCAT_TO_BUFFER
135
1.77k
}
136
137
void DECORATE_PRINTF(4, 5)
138
553k
    loader_log(const struct loader_instance *inst, VkFlags msg_type, int32_t msg_code, const char *format, ...) {
139
553k
    (void)msg_code;
140
553k
    char msg[512] = {0};
141
142
553k
    va_list ap;
143
553k
    va_start(ap, format);
144
553k
    int ret = vsnprintf(msg, sizeof(msg), format, ap);
145
553k
    if ((ret >= (int)sizeof(msg)) || ret < 0) {
146
286
        msg[sizeof(msg) - 1] = '\0';
147
286
    }
148
553k
    va_end(ap);
149
150
553k
    if (inst) {
151
553k
        VkDebugUtilsMessageSeverityFlagBitsEXT severity = 0;
152
553k
        VkDebugUtilsMessageTypeFlagsEXT type = 0;
153
553k
        VkDebugUtilsMessengerCallbackDataEXT callback_data = {0};
154
553k
        VkDebugUtilsObjectNameInfoEXT object_name = {0};
155
156
553k
        if ((msg_type & VULKAN_LOADER_INFO_BIT) != 0) {
157
29.6k
            severity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT;
158
523k
        } else if ((msg_type & VULKAN_LOADER_WARN_BIT) != 0) {
159
349k
            severity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT;
160
349k
        } else if ((msg_type & VULKAN_LOADER_ERROR_BIT) != 0) {
161
13.3k
            severity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
162
161k
        } else if ((msg_type & VULKAN_LOADER_DEBUG_BIT) != 0) {
163
60.7k
            severity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT;
164
100k
        } 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
100k
            severity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT;
167
100k
        }
168
169
553k
        if ((msg_type & VULKAN_LOADER_PERF_BIT) != 0) {
170
0
            type = VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
171
553k
        } 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
553k
        } else {
176
553k
            type = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT;
177
553k
        }
178
179
553k
        callback_data.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CALLBACK_DATA_EXT;
180
553k
        callback_data.pMessageIdName = "Loader Message";
181
553k
        callback_data.pMessage = msg;
182
553k
        callback_data.objectCount = 1;
183
553k
        callback_data.pObjects = &object_name;
184
553k
        object_name.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
185
553k
        object_name.objectType = VK_OBJECT_TYPE_INSTANCE;
186
553k
        object_name.objectHandle = (uint64_t)(uintptr_t)inst;
187
188
553k
        util_SubmitDebugUtilsMessageEXT(inst, severity, type, &callback_data);
189
553k
    }
190
191
    // Always log to stderr if this is a fatal error
192
553k
    if (0 == (msg_type & VULKAN_LOADER_FATAL_ERROR_BIT)) {
193
553k
        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
21.0k
            if (0 == (msg_type & inst->settings.debug_level)) {
196
12.2k
                return;
197
12.2k
            }
198
            // Check the global settings and if that doesn't say to skip, check the environment variable
199
532k
        } else if (0 == (msg_type & g_loader_debug)) {
200
532k
            return;
201
532k
        }
202
553k
    }
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
8.72k
    char cmd_line_msg[64] = {0};
215
8.72k
    size_t cmd_line_size = sizeof(cmd_line_msg);
216
217
8.72k
    loader_strncat(cmd_line_msg, cmd_line_size, "[Vulkan Loader] ", sizeof("[Vulkan Loader] "));
218
219
8.72k
    bool need_separator = false;
220
8.72k
    if ((msg_type & VULKAN_LOADER_ERROR_BIT) != 0) {
221
513
        loader_strncat(cmd_line_msg, cmd_line_size, "ERROR", sizeof("ERROR"));
222
513
        need_separator = true;
223
8.21k
    } else if ((msg_type & VULKAN_LOADER_WARN_BIT) != 0) {
224
1.67k
        loader_strncat(cmd_line_msg, cmd_line_size, "WARNING", sizeof("WARNING"));
225
1.67k
        need_separator = true;
226
6.53k
    } else if ((msg_type & VULKAN_LOADER_INFO_BIT) != 0) {
227
2.01k
        loader_strncat(cmd_line_msg, cmd_line_size, "INFO", sizeof("INFO"));
228
2.01k
        need_separator = true;
229
4.52k
    } else if ((msg_type & VULKAN_LOADER_DEBUG_BIT) != 0) {
230
4.48k
        loader_strncat(cmd_line_msg, cmd_line_size, "DEBUG", sizeof("DEBUG"));
231
4.48k
        need_separator = true;
232
4.48k
    }
233
234
8.72k
    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
8.72k
    } else if ((msg_type & VULKAN_LOADER_DRIVER_BIT) != 0) {
240
0
        if (need_separator) {
241
0
            loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
242
0
        }
243
0
        loader_strncat(cmd_line_msg, cmd_line_size, "DRIVER", sizeof("DRIVER"));
244
8.72k
    } else if ((msg_type & VULKAN_LOADER_LAYER_BIT) != 0) {
245
1.97k
        if (need_separator) {
246
1.93k
            loader_strncat(cmd_line_msg, cmd_line_size, " | ", sizeof(" | "));
247
1.93k
        }
248
1.97k
        loader_strncat(cmd_line_msg, cmd_line_size, "LAYER", sizeof("LAYER"));
249
1.97k
    }
250
251
8.72k
    loader_strncat(cmd_line_msg, cmd_line_size, ": ", sizeof(": "));
252
8.72k
    size_t num_used = strlen(cmd_line_msg);
253
254
    // Justifies the output to at least 29 spaces
255
8.72k
    if (num_used < 32) {
256
7.87k
        const char space_buffer[] = "                                ";
257
        // Only write (32 - num_used) spaces
258
7.87k
        loader_strncat(cmd_line_msg, cmd_line_size, space_buffer, sizeof(space_buffer) - 1 - num_used);
259
7.87k
    }
260
    // Assert that we didn't write more than what is available in cmd_line_msg
261
8.72k
    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
8.72k
}
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
}