The open source OpenXR runtime
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

u/logging: Add hexdump logging method

Add macros for logging hexdumps of memory blocks to help
with driver development. Only macros for trace and debug
level logging are provided, as noone should be logging
hexdumps except for development.

authored by

Jan Schmidt and committed by
Jakob Bornecrantz
e9c97724 4f171b3d

+169
+80
src/xrt/auxiliary/util/u_logging.c
··· 44 44 va_end(copy); \ 45 45 } 46 46 47 + static void 48 + u_log_hexdump_line(char *buf, size_t offset, const uint8_t *data, size_t data_size) 49 + { 50 + char *pos = buf; 51 + 52 + if (data_size > 16) { 53 + data_size = 16; 54 + } 55 + 56 + pos += sprintf(pos, "%08x: ", (uint32_t)offset); 57 + 58 + char *ascii = pos + (16 * 3) + 1; 59 + size_t i; 60 + 61 + for (i = 0; i < data_size; i++) { 62 + pos += sprintf(pos, "%02x ", data[i]); 63 + 64 + if (data[i] >= ' ' && data[i] <= '~') { 65 + *ascii++ = data[i]; 66 + } else { 67 + *ascii++ = '.'; 68 + } 69 + } 70 + 71 + /* Pad short lines with spaces, and null terminate */ 72 + while (i++ < 16) { 73 + pos += sprintf(pos, " "); 74 + } 75 + /* Replace the first NULL terminator with a space */ 76 + *pos++ = ' '; 77 + /* and set it after the ASCII representation */ 78 + *ascii++ = '\0'; 79 + } 80 + 81 + void 82 + u_log_hex(const char *file, 83 + int line, 84 + const char *func, 85 + enum u_logging_level level, 86 + const uint8_t *data, 87 + const size_t data_size) 88 + { 89 + size_t offset = 0; 90 + 91 + while (offset < data_size) { 92 + char tmp[128]; 93 + u_log_hexdump_line(tmp, offset, data + offset, data_size - offset); 94 + u_log(file, line, func, level, "%s", tmp); 95 + 96 + offset += 16; 97 + if (offset > 0xffffffff) { /* Limit the dump length to 4GB(!) */ 98 + break; 99 + } 100 + } 101 + } 102 + 103 + void 104 + u_log_xdev_hex(const char *file, 105 + int line, 106 + const char *func, 107 + enum u_logging_level level, 108 + struct xrt_device *xdev, 109 + const uint8_t *data, 110 + const size_t data_size) 111 + { 112 + size_t offset = 0; 113 + 114 + while (offset < data_size) { 115 + char tmp[128]; 116 + u_log_hexdump_line(tmp, offset, data + offset, data_size - offset); 117 + u_log_xdev(file, line, func, level, xdev, "%s", tmp); 118 + 119 + offset += 16; 120 + if (offset > 0xffffffff) { /* Limit the dump length to 4GB(!) */ 121 + break; 122 + } 123 + } 124 + } 125 + 126 + 47 127 #if defined(XRT_OS_ANDROID) 48 128 49 129 #include <android/log.h>
+89
src/xrt/auxiliary/util/u_logging.h
··· 137 137 } \ 138 138 } while (false) 139 139 140 + /*! 141 + * @brief Log a memory hexdump at @p level only if the level is at least @p cond_level - typically wrapped in a helper 142 + * macro. 143 + * 144 + * Adds file, line, and function context. Like U_LOG_IFL() 145 + * 146 + * @param level A @ref u_logging_level value for this message. 147 + * @param cond_level The minimum @ref u_logging_level that will be actually output. 148 + * @param data The data to print in hexdump format 149 + * @param data_size The size (in bytes) of the data block 150 + */ 151 + #define U_LOG_IFL_HEX(level, cond_level, data, data_size) \ 152 + do { \ 153 + if (cond_level <= level) { \ 154 + u_log_hex(__FILE__, __LINE__, __func__, level, data, data_size); \ 155 + } \ 156 + } while (false) 157 + 158 + /*! 159 + * @brief Log a memory hexdump at @p level for a given @ref xrt_device, only if the level is at least @p cond_level - 160 + * typically wrapped in a helper macro. 161 + * 162 + * Adds file, line, and function context, and forwards device context from provided @p xdev . 163 + * @param level A @ref u_logging_level value for this message. 164 + * @param cond_level The minimum @ref u_logging_level that will be actually output. 165 + * @param xdev The @ref xrt_device pointer associated with this message. 166 + * @param data The data to print in hexdump format 167 + * @param data_size The size (in bytes) of the data block 168 + */ 169 + #define U_LOG_XDEV_IFL_HEX(level, cond_level, xdev, data, data_size) \ 170 + do { \ 171 + if (cond_level <= level) { \ 172 + u_log_xdev_hex(__FILE__, __LINE__, __func__, level, xdev, data, data_size); \ 173 + } \ 174 + } while (false) 140 175 141 176 142 177 /*! ··· 183 218 ...) XRT_PRINTF_FORMAT(6, 7); 184 219 185 220 /*! 221 + * @brief Log implementation for dumping memory buffers as hex: do not call directly, use a macro that wraps it. 222 + * 223 + * This function always logs: level is used for printing or passed to native logging functions. 224 + * 225 + * @param file Source file name associated with a message 226 + * @param line Source file line associated with a message 227 + * @param func Function name associated with a message 228 + * @param level Message level: used for formatting or forwarding to native log functions 229 + * @param data Data buffer to dump 230 + * @param data_size Size of the data buffer in bytes 231 + */ 232 + void 233 + u_log_hex(const char *file, 234 + int line, 235 + const char *func, 236 + enum u_logging_level level, 237 + const uint8_t *data, 238 + const size_t data_size); 239 + 240 + /*! 241 + * @brief Device-related log implementation for dumping memory buffers as hex: do not call directly, use a macro that 242 + * wraps it. 243 + * 244 + * This function always logs: level is used for printing or passed to native logging functions. 245 + * @param file Source file name associated with a message 246 + * @param line Source file line associated with a message 247 + * @param func Function name associated with a message 248 + * @param level Message level: used for formatting or forwarding to native log functions 249 + * @param xdev The associated @ref xrt_device 250 + * @param data Data buffer to dump 251 + * @param data_size Size of the data buffer in bytes 252 + */ 253 + void 254 + u_log_xdev_hex(const char *file, 255 + int line, 256 + const char *func, 257 + enum u_logging_level level, 258 + struct xrt_device *xdev, 259 + const uint8_t *data, 260 + const size_t data_size); 261 + 262 + /*! 186 263 * Sets the logging sink, log is still passed on to the platform defined output 187 264 * as well as the sink. 188 265 * ··· 249 326 #define U_LOG_IFL_W(cond_level, ...) U_LOG_IFL(U_LOGGING_WARN, cond_level, __VA_ARGS__) 250 327 //! Conditionally log a message at U_LOGGING_ERROR level. 251 328 #define U_LOG_IFL_E(cond_level, ...) U_LOG_IFL(U_LOGGING_ERROR, cond_level, __VA_ARGS__) 329 + 330 + //! Conditionally log a memory hexdump at U_LOGGING_TRACE level. 331 + #define U_LOG_IFL_T_HEX(cond_level, data, data_size) U_LOG_IFL_HEX(U_LOGGING_TRACE, cond_level, data, data_size) 332 + //! Conditionally log a memory hexdump at U_LOGGING_DEBUG level. 333 + #define U_LOG_IFL_D_HEX(cond_level, data, data_size) U_LOG_IFL_HEX(U_LOGGING_DEBUG, cond_level, data, data_size) 252 334 /*! 253 335 * @} 254 336 */ ··· 278 360 #define U_LOG_XDEV_IFL_W(xdev, cond_level, ...) U_LOG_XDEV_IFL(U_LOGGING_WARN, cond_level, xdev, __VA_ARGS__) 279 361 //! Conditionally log a device-related message at U_LOGGING_ERROR level. 280 362 #define U_LOG_XDEV_IFL_E(xdev, cond_level, ...) U_LOG_XDEV_IFL(U_LOGGING_ERROR, cond_level, xdev, __VA_ARGS__) 363 + 364 + //! Conditionally log a device-related memory hexdump at U_LOGGING_TRACE level. 365 + #define U_LOG_XDEV_IFL_T_HEX(xdev, cond_level, data, data_size) \ 366 + U_LOG_XDEV_IFL_HEX(U_LOGGING_TRACE, cond_level, xdev, data, data_size) 367 + //! Conditionally log a device-related memory hexdump message at U_LOGGING_DEBUG level. 368 + #define U_LOG_XDEV_IFL_D_HEX(xdev, cond_level, data, data_size) \ 369 + U_LOG_XDEV_IFL_HEX(U_LOGGING_DEBUG, cond_level, xdev, data, data_size) 281 370 /*! 282 371 * @} 283 372 */