blob: bcf73c5a225f9aa09bfe1f9568ec775297e5c6f9 [file] [log] [blame]
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07001/*
2 * Copyright (c) 2008 Travis Geiselbrecht
3 *
Maria Yud3200512014-05-28 15:57:51 +08004 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
5 *
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07006 * 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 Geiselbrechteb946052008-09-07 22:32:49 -070029#include <stdarg.h>
30#include <compiler.h>
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070031#include <platform/debug.h>
32#include <printf.h>
33
34#if defined(__cplusplus)
35extern "C" {
36#endif
37
38#if defined(DEBUG)
39#define DEBUGLEVEL DEBUG
40#else
41#define DEBUGLEVEL 2
42#endif
43
Travis Geiselbrechteb946052008-09-07 22:32:49 -070044/* debug levels */
45#define CRITICAL 0
46#define ALWAYS 0
47#define INFO 1
48#define SPEW 2
49
50/* output */
51void _dputc(char c); // XXX for now, platform implements
52int _dputs(const char *str);
53int _dprintf(const char *fmt, ...) __PRINTFLIKE(1, 2);
54int _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 Geiselbrecht28da92a2009-06-28 11:12:40 -070062int dgetc(char *c, bool wait);
Travis Geiselbrechteb946052008-09-07 22:32:49 -070063
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070064/* systemwide halts */
Amol Jadif5317c82012-08-17 17:32:19 -070065void halt(void);
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070066
Amol Jadif5317c82012-08-17 17:32:19 -070067void _panic(void *caller, const char *fmt, ...) __PRINTFLIKE(2, 3);
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070068#define panic(x...) _panic(__GET_CALLER(), x)
69
70#define PANIC_UNIMPLEMENTED panic("%s unimplemented\n", __PRETTY_FUNCTION__)
71
Maria Yud3200512014-05-28 15:57:51 +080072void * __stack_chk_guard;
73
74/*
75* Initialize the stack protector canary value.
76*/
Joonwoo Park3dc18632014-07-18 11:45:42 -070077#define __stack_chk_guard_setup() do { __stack_chk_guard = get_canary(); } while(0)
Maria Yud3200512014-05-28 15:57:51 +080078
79void __attribute__ ((noreturn))
80 __stack_chk_fail (void);
81
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070082/* spin the cpu for a period of (short) time */
83void spin(uint32_t usecs);
84
85/* dump memory */
86void hexdump(const void *ptr, size_t len);
87void hexdump8(const void *ptr, size_t len);
88
89/* trace routines */
90#define TRACE_ENTRY printf("%s: entry\n", __PRETTY_FUNCTION__)
91#define TRACE_EXIT printf("%s: exit\n", __PRETTY_FUNCTION__)
92#define TRACE_ENTRY_OBJ printf("%s: entry obj %p\n", __PRETTY_FUNCTION__, this)
93#define TRACE_EXIT_OBJ printf("%s: exit obj %p\n", __PRETTY_FUNCTION__, this)
94#define TRACE printf("%s:%d\n", __PRETTY_FUNCTION__, __LINE__)
95#define TRACEF(x...) do { printf("%s:%d: ", __PRETTY_FUNCTION__, __LINE__); printf(x); } while (0)
96
97/* trace routines that work if LOCAL_TRACE is set */
98#define LTRACE_ENTRY do { if (LOCAL_TRACE) { TRACE_ENTRY; } } while (0)
99#define LTRACE_EXIT do { if (LOCAL_TRACE) { TRACE_EXIT; } } while (0)
100#define LTRACE do { if (LOCAL_TRACE) { TRACE; } } while (0)
101#define LTRACEF(x...) do { if (LOCAL_TRACE) { TRACEF(x); } } while (0)
102
103#if defined(__cplusplus)
104}
105#endif
106
107#endif