blob: d4bce54577ca85298ba8463464480bd8474d5cda [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
Maria Yu639951a2014-05-29 14:15:54 +080067void dump_frame(void *frame);
68
Amol Jadif5317c82012-08-17 17:32:19 -070069void _panic(void *caller, const char *fmt, ...) __PRINTFLIKE(2, 3);
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070070#define panic(x...) _panic(__GET_CALLER(), x)
71
72#define PANIC_UNIMPLEMENTED panic("%s unimplemented\n", __PRETTY_FUNCTION__)
73
Maria Yud3200512014-05-28 15:57:51 +080074void * __stack_chk_guard;
75
76/*
77* Initialize the stack protector canary value.
78*/
Joonwoo Park3dc18632014-07-18 11:45:42 -070079#define __stack_chk_guard_setup() do { __stack_chk_guard = get_canary(); } while(0)
Maria Yud3200512014-05-28 15:57:51 +080080
81void __attribute__ ((noreturn))
82 __stack_chk_fail (void);
83
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070084/* spin the cpu for a period of (short) time */
85void spin(uint32_t usecs);
86
87/* dump memory */
88void hexdump(const void *ptr, size_t len);
89void 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