rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2008-2012 Travis Geiselbrecht |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | #ifndef __DEBUG_H |
| 24 | #define __DEBUG_H |
| 25 | |
| 26 | #include <stdarg.h> |
| 27 | #include <stddef.h> |
| 28 | #include <compiler.h> |
| 29 | #include <platform/debug.h> |
| 30 | #include <list.h> |
| 31 | #include <stdio.h> |
| 32 | |
| 33 | #if !defined(LK_DEBUGLEVEL) |
| 34 | #define LK_DEBUGLEVEL 0 |
| 35 | #endif |
| 36 | |
| 37 | /* debug levels */ |
| 38 | #define CRITICAL 0 |
| 39 | #define ALWAYS 0 |
| 40 | #define INFO 1 |
| 41 | #define SPEW 2 |
| 42 | |
| 43 | __BEGIN_CDECLS |
| 44 | |
| 45 | typedef struct __print_callback print_callback_t; |
| 46 | struct __print_callback { |
| 47 | struct list_node entry; |
| 48 | void (*print)(print_callback_t *cb, const char *str, size_t len); |
| 49 | }; |
| 50 | |
| 51 | #if !DISABLE_DEBUG_OUTPUT |
| 52 | |
| 53 | /* input/output */ |
| 54 | int _dprintf(const char *fmt, ...) __PRINTFLIKE(1, 2); |
| 55 | |
| 56 | // Obtain the panic file descriptor. |
| 57 | FILE get_panic_fd(void); |
| 58 | |
| 59 | /* dump memory */ |
| 60 | void hexdump(const void *ptr, size_t len); |
| 61 | void hexdump8_ex(const void *ptr, size_t len, uint64_t disp_addr_start); |
| 62 | |
| 63 | #else |
| 64 | |
| 65 | /* input/output */ |
| 66 | static inline int __PRINTFLIKE(1, 2) _dprintf(const char *fmt, ...) { return 0; } |
| 67 | |
| 68 | /* dump memory */ |
| 69 | static inline void hexdump(const void *ptr, size_t len) { } |
| 70 | static inline void hexdump8_ex(const void *ptr, size_t len, uint64_t disp_addr_start) { } |
| 71 | |
| 72 | #endif /* DISABLE_DEBUG_OUTPUT */ |
| 73 | |
| 74 | static inline void hexdump8(const void *ptr, size_t len) |
| 75 | { |
| 76 | hexdump8_ex(ptr, len, (uint64_t)((addr_t)ptr)); |
| 77 | } |
| 78 | |
| 79 | /* register callback to receive debug prints */ |
| 80 | void register_print_callback(print_callback_t *cb); |
| 81 | void unregister_print_callback(print_callback_t *cb); |
| 82 | |
| 83 | #define dprintf(level, x...) do { if ((level) <= LK_DEBUGLEVEL) { _dprintf(x); } } while (0) |
| 84 | |
| 85 | /* systemwide halts */ |
| 86 | void _panic(void *caller, const char *fmt, ...) __PRINTFLIKE(2, 3) __NO_RETURN; |
| 87 | #define panic(x...) _panic(__GET_CALLER(), x) |
| 88 | |
| 89 | #define PANIC_UNIMPLEMENTED panic("%s unimplemented\n", __PRETTY_FUNCTION__) |
| 90 | |
| 91 | /* spin the cpu for a period of (short) time */ |
| 92 | void spin(uint32_t usecs); |
| 93 | |
| 94 | /* spin the cpu for a certain number of cpu cycles */ |
| 95 | void spin_cycles(uint32_t usecs); |
| 96 | |
| 97 | __END_CDECLS |
| 98 | |
| 99 | #endif |