Gert Wollny | 20afd5c | 2018-10-22 16:11:58 +0200 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
| 3 | * Copyright (C) 2018 Collabora Ltd |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 6 | * copy of this software and associated documentation files (the "Software"), |
| 7 | * to deal in the Software without restriction, including without limitation |
| 8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 | * and/or sell copies of the Software, and to permit persons to whom the |
| 10 | * Software is furnished to do so, subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be included |
| 13 | * in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 16 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 19 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 20 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 21 | * OTHER DEALINGS IN THE SOFTWARE. |
| 22 | * |
| 23 | **************************************************************************/ |
| 24 | |
| 25 | #include "vrend_debug.h" |
| 26 | #include "vrend_renderer.h" |
| 27 | #include "util/u_debug.h" |
| 28 | #include <stdlib.h> |
| 29 | #include <stdio.h> |
Gert Wollny | 8900596 | 2018-10-22 16:23:40 +0200 | [diff] [blame] | 30 | |
| 31 | static const char *command_names[VIRGL_MAX_COMMANDS] = { |
| 32 | "NOP", |
| 33 | "CREATE_OBJECT", |
| 34 | "BIND_OBJECT", |
| 35 | "DESTROY_OBJECT", |
| 36 | "SET_VIEWPORT_STATE", |
| 37 | "SET_FRAMEBUFFER_STATE", |
| 38 | "SET_VERTEX_BUFFERS", |
| 39 | "CLEAR", |
| 40 | "DRAW_VBO", |
| 41 | "RESOURCE_INLINE_WRITE", |
| 42 | "SET_SAMPLER_VIEWS", |
| 43 | "SET_INDEX_BUFFER", |
| 44 | "SET_CONSTANT_BUFFER", |
| 45 | "SET_STENCIL_REF", |
| 46 | "SET_BLEND_COLOR", |
| 47 | "SET_SCISSOR_STATE", |
| 48 | "BLIT", |
| 49 | "RESOURCE_COPY_REGION", |
| 50 | "BIND_SAMPLER_STATES", |
| 51 | "BEGIN_QUERY", |
| 52 | "END_QUERY", |
| 53 | "GET_QUERY_RESULT", |
| 54 | "SET_POLYGON_STIPPLE", |
| 55 | "SET_CLIP_STATE", |
| 56 | "SET_SAMPLE_MASK", |
| 57 | "SET_STREAMOUT_TARGETS", |
| 58 | "SET_RENDER_CONDITION", |
| 59 | "SET_UNIFORM_BUFFER", |
| 60 | "SET_SUB_CTX", |
| 61 | "CREATE_SUB_CTX", |
| 62 | "DESTROY_SUB_CTX", |
| 63 | "BIND_SHADER", |
| 64 | "SET_TESS_STATE", |
| 65 | "SET_MIN_SAMPLES", |
| 66 | "SET_SHADER_BUFFERS", |
| 67 | "SET_SHADER_IMAGES", |
| 68 | "MEMORY_BARRIER", |
| 69 | "LAUNCH_GRID", |
| 70 | "SET_FRAMEBUFFER_STATE_NO_ATTACH", |
| 71 | "TEXTURE_BARRIER", |
| 72 | "SET_ATOMIC_BUFFERS", |
Dave Airlie | 3a069fc | 2018-05-21 16:37:44 +1000 | [diff] [blame] | 73 | "SET_DEBUG_FLAGS", |
David Riley | 1fd939c | 2019-04-16 12:49:06 -0700 | [diff] [blame] | 74 | "GET_QBO_RESULT", |
| 75 | "TRANSFER3D", |
| 76 | "END_TRANSFERS", |
Alexandros Frantzis | 1e2f540 | 2019-05-14 17:37:57 +0300 | [diff] [blame] | 77 | "COPY_TRANSFER3D", |
Elie Tournier | 66b5e91 | 2020-03-20 13:47:47 +0000 | [diff] [blame] | 78 | "TWEAK", |
Gert Wollny | a296b90 | 2021-03-04 18:20:52 +0100 | [diff] [blame] | 79 | "CLEAR_TEXTURE", |
Gurchetan Singh | 0c28415 | 2019-08-30 14:52:41 -0700 | [diff] [blame] | 80 | "PIPE_RESOURCE_CREATE", |
Chia-I Wu | f5a45fe | 2020-12-24 18:00:08 -0800 | [diff] [blame] | 81 | "PIPE_RESOURCE_SET_TYPE", |
Gert Wollny | a296b90 | 2021-03-04 18:20:52 +0100 | [diff] [blame] | 82 | "GET_MEMORY_INFO", |
Gert Wollny | fd0769a | 2021-03-04 15:41:49 +0100 | [diff] [blame] | 83 | "SEND_STRING_MARKER", |
Gert Wollny | 8900596 | 2018-10-22 16:23:40 +0200 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | static const char *object_type_names[VIRGL_MAX_OBJECTS] = { |
| 87 | "NULL", |
| 88 | "BLEND", |
| 89 | "RASTERIZER", |
| 90 | "DSA", |
| 91 | "SHADER", |
| 92 | "VERTEX_ELEMENTS", |
| 93 | "SAMPLER_VIEW", |
| 94 | "SAMPLER_STATE", |
| 95 | "SURFACE", |
| 96 | "QUERY", |
Italo Nicola | 4405172 | 2021-04-28 09:21:09 +0000 | [diff] [blame^] | 97 | "STREAMOUT_TARGET", |
| 98 | "MSAA_SURFACE" |
Gert Wollny | 8900596 | 2018-10-22 16:23:40 +0200 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | const char *vrend_get_comand_name(enum virgl_context_cmd cmd) |
| 102 | { |
| 103 | if (cmd < VIRGL_MAX_COMMANDS) |
| 104 | return command_names[cmd]; |
| 105 | return "UNKNOWN"; |
| 106 | } |
| 107 | |
| 108 | extern const char *vrend_get_object_type_name(enum virgl_object_type obj) |
| 109 | { |
| 110 | if (obj < VIRGL_MAX_OBJECTS) |
| 111 | return object_type_names[obj]; |
| 112 | return "UNKNOWN"; |
| 113 | } |
| 114 | |
Gert Wollny | 20afd5c | 2018-10-22 16:11:58 +0200 | [diff] [blame] | 115 | static const struct debug_named_value vrend_debug_options[] = { |
Gert Wollny | 05a1045 | 2018-10-22 16:20:38 +0200 | [diff] [blame] | 116 | {"tgsi", dbg_shader_tgsi, "Print TGSI"}, |
| 117 | {"glsl", dbg_shader_glsl, "Print GLSL shaders created from TGSI"}, |
| 118 | {"shader", dbg_shader, "Print TGSI and created GLSL shaders"}, |
| 119 | {"stream", dbg_shader_streamout, "Print shader streamout"}, |
Gert Wollny | 8900596 | 2018-10-22 16:23:40 +0200 | [diff] [blame] | 120 | {"cmd", dbg_cmd, "Print incoming commands"}, |
| 121 | {"obj", dbg_object, "Print object creation"}, |
Gert Wollny | 97f2b21 | 2018-10-22 16:29:36 +0200 | [diff] [blame] | 122 | {"blit", dbg_blit, "Debug blit code path"}, |
| 123 | {"copyres", dbg_copy_resource, "Debug copy resource code path"}, |
Gert Wollny | 40db85e | 2018-11-06 12:35:00 +0100 | [diff] [blame] | 124 | {"feat", dbg_features, "Log features found"}, |
Gert Wollny | cbb6a48 | 2018-11-06 12:39:43 +0100 | [diff] [blame] | 125 | {"tex", dbg_tex, "Log texture operations"}, |
Gert Wollny | ff18dfe | 2019-02-01 15:45:48 +0100 | [diff] [blame] | 126 | {"caller", dbg_caller, "Log who is creating the context"}, |
Gert Wollny | 87fe820 | 2019-05-27 15:07:36 +0200 | [diff] [blame] | 127 | {"tweak", dbg_tweak, "Log tweaks"}, |
Gert Wollny | 5eefd5a | 2019-05-27 15:34:08 +0200 | [diff] [blame] | 128 | {"query", dbg_query, "Log queries"}, |
Gurchetan Singh | 0a7fe48 | 2019-09-24 18:41:18 -0700 | [diff] [blame] | 129 | {"gles", dbg_gles, "GLES host specific debug"}, |
Gert Wollny | bb02ee0 | 2018-11-06 12:44:06 +0100 | [diff] [blame] | 130 | {"all", dbg_all, "Enable all debugging output"}, |
Gert Wollny | d5efa76 | 2018-11-06 12:42:11 +0100 | [diff] [blame] | 131 | {"guestallow", dbg_allow_guest_override, "Allow the guest to override the debug flags"}, |
Lepton Wu | 4dd3e61 | 2020-01-23 13:59:50 -0800 | [diff] [blame] | 132 | {"khr", dbg_khr, "Enable debug via KHR_debug extension"}, |
Gert Wollny | 20afd5c | 2018-10-22 16:11:58 +0200 | [diff] [blame] | 133 | DEBUG_NAMED_VALUE_END |
| 134 | }; |
| 135 | |
| 136 | static uint64_t vrend_debug_flags = 0; |
| 137 | static int vrend_debug_flags_initalized = 0; |
Gert Wollny | d5efa76 | 2018-11-06 12:42:11 +0100 | [diff] [blame] | 138 | |
| 139 | int vrend_get_debug_flags(const char *flagstring) |
| 140 | { |
| 141 | int retval; |
| 142 | char buf[1024] = ""; |
| 143 | |
| 144 | /* Unfortunately the available function to scan the flags take the string |
| 145 | * from the environment. The alternative to using setenv would be to |
| 146 | * duplicate code or to change the gallium/util intefaces and diverge more |
| 147 | * from mesa. So just stick to the environment variable. */ |
| 148 | snprintf(buf, 1024, "VREND_TEMP_DEBUG_STRING_%d", getpid()); |
| 149 | setenv(buf, flagstring, 1); |
| 150 | |
| 151 | retval = (int)debug_get_flags_option(buf, |
| 152 | vrend_debug_options, 0); |
| 153 | unsetenv(buf); |
| 154 | return retval; |
| 155 | } |
| 156 | |
Gert Wollny | 20afd5c | 2018-10-22 16:11:58 +0200 | [diff] [blame] | 157 | void vrend_init_debug_flags() |
| 158 | { |
| 159 | if (!vrend_debug_flags_initalized) { |
| 160 | vrend_debug_flags_initalized = 1; |
| 161 | vrend_debug_flags = debug_get_flags_option("VREND_DEBUG", |
| 162 | vrend_debug_options, 0); |
| 163 | } |
| 164 | } |
| 165 | |
Chia-I Wu | b2940a2 | 2020-04-23 14:12:10 -0700 | [diff] [blame] | 166 | unsigned vrend_debug(const struct vrend_context *ctx, enum virgl_debug_flags flag) |
Gert Wollny | 20afd5c | 2018-10-22 16:11:58 +0200 | [diff] [blame] | 167 | { |
| 168 | return (vrend_debug_flags & flag) || vrend_context_has_debug_flag(ctx, flag); |
| 169 | } |
Gert Wollny | 40db85e | 2018-11-06 12:35:00 +0100 | [diff] [blame] | 170 | |
| 171 | void vrend_debug_add_flag(enum virgl_debug_flags flag) |
| 172 | { |
| 173 | vrend_debug_flags |= flag; |
| 174 | } |
Gert Wollny | d5efa76 | 2018-11-06 12:42:11 +0100 | [diff] [blame] | 175 | |
| 176 | int vrend_debug_can_override(void) |
| 177 | { |
| 178 | return vrend_debug_flags & dbg_allow_guest_override; |
| 179 | } |
Gert Wollny | 87a2103 | 2019-01-31 12:53:01 +0100 | [diff] [blame] | 180 | |
| 181 | static |
| 182 | void vrend_default_debug_callback(const char *fmt, va_list va) |
| 183 | { |
Lepton Wu | ff43cc7 | 2019-12-18 18:02:17 -0800 | [diff] [blame] | 184 | static FILE* fp = NULL; |
| 185 | if (NULL == fp) { |
| 186 | const char* log = getenv("VIRGL_LOG_FILE"); |
| 187 | if (log) { |
| 188 | char *log_prefix = strdup(log); |
| 189 | char *log_suffix = strstr(log_prefix, "%PID%"); |
| 190 | if (log_suffix) { |
| 191 | *log_suffix = 0; |
| 192 | log_suffix += 5; |
| 193 | int len = strlen(log) + 32; |
| 194 | char *name = malloc(len); |
| 195 | snprintf(name, len, "%s%d%s", log_prefix, getpid(), log_suffix); |
| 196 | fp = fopen(name, "a"); |
| 197 | free(name); |
| 198 | } else { |
| 199 | fp = fopen(log, "a"); |
| 200 | } |
| 201 | free(log_prefix); |
| 202 | if (NULL == fp) { |
| 203 | fprintf(stderr, "Can't open %s\n", log); |
| 204 | fp = stderr; |
| 205 | } |
| 206 | } else { |
| 207 | fp = stderr; |
| 208 | } |
| 209 | } |
| 210 | vfprintf(fp, fmt, va); |
| 211 | fflush(fp); |
Gert Wollny | 87a2103 | 2019-01-31 12:53:01 +0100 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | static virgl_debug_callback_type debug_callback = vrend_default_debug_callback; |
| 215 | |
| 216 | void vrend_printf(const char *fmt, ...) |
| 217 | { |
| 218 | if (debug_callback) { |
| 219 | va_list va; |
| 220 | va_start(va, fmt); |
| 221 | debug_callback(fmt, va); |
| 222 | va_end(va); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | virgl_debug_callback_type vrend_set_debug_callback(virgl_debug_callback_type cb) |
| 227 | { |
| 228 | virgl_debug_callback_type retval = debug_callback; |
| 229 | debug_callback = cb; |
| 230 | return retval; |
| 231 | } |
| 232 | |