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 #pragma once
24 
25 #include <stddef.h>
26 #include <stdio.h>
27 #include <compiler.h>
28 #include <panic.h>
29 #include <platform/debug.h>
30 
31 #if !defined(LK_DEBUGLEVEL)
32 #define LK_DEBUGLEVEL 0
33 #endif
34 
35 /* debug levels */
36 #define LK_DEBUGLEVEL_CRITICAL 0
37 #define LK_DEBUGLEVEL_ALWAYS 0
38 #define LK_DEBUGLEVEL_INFO 1
39 #define LK_DEBUGLEVEL_SPEW 2
40 
41 /* Deprecated macros for debug levels. LK_DEBUGLEVEL_NO_ALIASES may optionally
42  * be set to avoid name collisions with macros defined in other headers */
43 #if !LK_DEBUGLEVEL_NO_ALIASES
44 #define CRITICAL LK_DEBUGLEVEL_CRITICAL
45 #define ALWAYS LK_DEBUGLEVEL_ALWAYS
46 #define INFO LK_DEBUGLEVEL_INFO
47 #define SPEW LK_DEBUGLEVEL_SPEW
48 #endif
49 
50 __BEGIN_CDECLS
51 
52 #if !DISABLE_DEBUG_OUTPUT
53 
54 #if ENABLE_PANIC_SHELL
55 /* Obtain the panic file descriptor. */
56 FILE *get_panic_fd(void);
57 #endif
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 /* Obtain the panic file descriptor. */
66 static inline FILE *get_panic_fd(void) { return NULL; }
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 
hexdump8(const void * ptr,size_t len)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 #define _dprintf_internal(level, x...) do { if ((level) <= LK_LOGLEVEL) { printf(x); } } while (0)
80 #define vdprintf(level, fmt, args) do { if ((level) <= LK_LOGLEVEL) { vprintf(fmt, args); } } while (0)
81 #if LK_DEBUGLEVEL_NO_ALIASES
82 #define dprintf(level, x...) _dprintf_internal(LK_DEBUGLEVEL_##level, ##x)
83 #else
84 #define dprintf(level, x...) _dprintf_internal(level, ##x)
85 #endif
86 
87 /* spin the cpu for a period of (short) time */
88 void spin(uint32_t usecs);
89 
90 /* spin the cpu for a certain number of cpu cycles */
91 void spin_cycles(uint32_t usecs);
92 
93 __END_CDECLS
94