Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2008 Travis Geiselbrecht |
| 3 | * |
Maria Yu | d320051 | 2014-05-28 15:57:51 +0800 | [diff] [blame] | 4 | * Copyright (c) 2014, The Linux Foundation. All rights reserved. |
| 5 | * |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 6 | * Permission is hereby granted, free of charge, to any person obtaining |
| 7 | * a copy of this software and associated documentation files |
| 8 | * (the "Software"), to deal in the Software without restriction, |
| 9 | * including without limitation the rights to use, copy, modify, merge, |
| 10 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 11 | * and to permit persons to whom the Software is furnished to do so, |
| 12 | * subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be |
| 15 | * included in all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 | */ |
| 25 | #ifndef __DEBUG_H |
| 26 | #define __DEBUG_H |
| 27 | |
| 28 | #include <assert.h> |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 29 | #include <stdarg.h> |
| 30 | #include <compiler.h> |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 31 | #include <platform/debug.h> |
| 32 | #include <printf.h> |
| 33 | |
| 34 | #if defined(__cplusplus) |
| 35 | extern "C" { |
| 36 | #endif |
| 37 | |
| 38 | #if defined(DEBUG) |
| 39 | #define DEBUGLEVEL DEBUG |
| 40 | #else |
| 41 | #define DEBUGLEVEL 2 |
| 42 | #endif |
| 43 | |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 44 | /* debug levels */ |
| 45 | #define CRITICAL 0 |
| 46 | #define ALWAYS 0 |
| 47 | #define INFO 1 |
| 48 | #define SPEW 2 |
| 49 | |
| 50 | /* output */ |
| 51 | void _dputc(char c); // XXX for now, platform implements |
| 52 | int _dputs(const char *str); |
| 53 | int _dprintf(const char *fmt, ...) __PRINTFLIKE(1, 2); |
| 54 | int _dvprintf(const char *fmt, va_list ap); |
| 55 | |
| 56 | #define dputc(level, str) do { if ((level) <= DEBUGLEVEL) { _dputc(str); } } while (0) |
| 57 | #define dputs(level, str) do { if ((level) <= DEBUGLEVEL) { _dputs(str); } } while (0) |
| 58 | #define dprintf(level, x...) do { if ((level) <= DEBUGLEVEL) { _dprintf(x); } } while (0) |
| 59 | #define dvprintf(level, x...) do { if ((level) <= DEBUGLEVEL) { _dvprintf(x); } } while (0) |
| 60 | |
| 61 | /* input */ |
Travis Geiselbrecht | 28da92a | 2009-06-28 11:12:40 -0700 | [diff] [blame] | 62 | int dgetc(char *c, bool wait); |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 63 | |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 64 | /* systemwide halts */ |
Amol Jadi | f5317c8 | 2012-08-17 17:32:19 -0700 | [diff] [blame] | 65 | void halt(void); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 66 | |
Maria Yu | 639951a | 2014-05-29 14:15:54 +0800 | [diff] [blame] | 67 | void dump_frame(void *frame); |
| 68 | |
Amol Jadi | f5317c8 | 2012-08-17 17:32:19 -0700 | [diff] [blame] | 69 | void _panic(void *caller, const char *fmt, ...) __PRINTFLIKE(2, 3); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 70 | #define panic(x...) _panic(__GET_CALLER(), x) |
| 71 | |
| 72 | #define PANIC_UNIMPLEMENTED panic("%s unimplemented\n", __PRETTY_FUNCTION__) |
| 73 | |
Maria Yu | d320051 | 2014-05-28 15:57:51 +0800 | [diff] [blame] | 74 | void * __stack_chk_guard; |
| 75 | |
| 76 | /* |
| 77 | * Initialize the stack protector canary value. |
| 78 | */ |
Joonwoo Park | 3dc1863 | 2014-07-18 11:45:42 -0700 | [diff] [blame] | 79 | #define __stack_chk_guard_setup() do { __stack_chk_guard = get_canary(); } while(0) |
Maria Yu | d320051 | 2014-05-28 15:57:51 +0800 | [diff] [blame] | 80 | |
| 81 | void __attribute__ ((noreturn)) |
| 82 | __stack_chk_fail (void); |
| 83 | |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 84 | /* spin the cpu for a period of (short) time */ |
| 85 | void spin(uint32_t usecs); |
| 86 | |
| 87 | /* dump memory */ |
| 88 | void hexdump(const void *ptr, size_t len); |
| 89 | void hexdump8(const void *ptr, size_t len); |
| 90 | |
| 91 | /* trace routines */ |
| 92 | #define TRACE_ENTRY printf("%s: entry\n", __PRETTY_FUNCTION__) |
| 93 | #define TRACE_EXIT printf("%s: exit\n", __PRETTY_FUNCTION__) |
| 94 | #define TRACE_ENTRY_OBJ printf("%s: entry obj %p\n", __PRETTY_FUNCTION__, this) |
| 95 | #define TRACE_EXIT_OBJ printf("%s: exit obj %p\n", __PRETTY_FUNCTION__, this) |
| 96 | #define TRACE printf("%s:%d\n", __PRETTY_FUNCTION__, __LINE__) |
| 97 | #define TRACEF(x...) do { printf("%s:%d: ", __PRETTY_FUNCTION__, __LINE__); printf(x); } while (0) |
| 98 | |
| 99 | /* trace routines that work if LOCAL_TRACE is set */ |
| 100 | #define LTRACE_ENTRY do { if (LOCAL_TRACE) { TRACE_ENTRY; } } while (0) |
| 101 | #define LTRACE_EXIT do { if (LOCAL_TRACE) { TRACE_EXIT; } } while (0) |
| 102 | #define LTRACE do { if (LOCAL_TRACE) { TRACE; } } while (0) |
| 103 | #define LTRACEF(x...) do { if (LOCAL_TRACE) { TRACEF(x); } } while (0) |
| 104 | |
| 105 | #if defined(__cplusplus) |
| 106 | } |
| 107 | #endif |
| 108 | |
| 109 | #endif |