debug.h
Go to the documentation of this file.
1 /*
2  * Bareflank Hypervisor
3  *
4  * Copyright (C) 2015 Assured Information Security, Inc.
5  * Author: Rian Quinn <quinnr@ainfosec.com>
6  * Author: Brendan Kerrigan <kerriganb@ainfosec.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #ifndef BFDEBUG_H
24 #define BFDEBUG_H
25 
26 /* -------------------------------------------------------------------------- */
27 /* C++ Debugging */
28 /* -------------------------------------------------------------------------- */
29 
30 #ifdef __cplusplus
31 
32 #include <iomanip>
33 #include <iostream>
34 #include <sstream>
35 
36 #include <view_as_pointer.h>
37 
38 #define bfcolor_green "\033[1;32m"
39 #define bfcolor_red "\033[1;31m"
40 
41 #define bfcolor_end "\033[0m"
42 #define bfcolor_debug "\033[1;32m"
43 #define bfcolor_warning "\033[1;33m"
44 #define bfcolor_error "\033[1;31m"
45 #define bfcolor_func "\033[1;36m"
46 #define bfcolor_line "\033[1;35m"
47 
48 /*
49  * Current Function Macro
50  *
51  * Clang Tidy does not like the built in macros that return character pointers
52  * as they claim it breaks the Core Guidelines which is obnoxious, so this
53  * macro redefines how this is done.
54  */
55 #define __FUNC__ static_cast<const char *>(__PRETTY_FUNCTION__)
56 
57 /*
58  * Output To Core
59  *
60  * All std::cout and std::cerr are sent to a specific debug_ring
61  * based on the vcpuid that you provide, instead of being
62  * sent to vcpuid=0 and serial.
63  *
64  * @param vcpuid the vcpu to send the output to
65  * @param func a lambda function containing the output to redirect
66  */
67 template<class V, class T>
68 void output_to_vcpu(V vcpuid, T func)
69 {
70  std::cout << "$vcpuid=" << std::setw(18) << view_as_pointer(vcpuid);
71  func();
72 }
73 
74 /*
75  * Newline macro
76  */
77 #ifndef bfendl
78 #define bfendl '\n'
79 #endif
80 
81 /*
82  * This macro is a shortcut for std::cout that adds some text and color.
83  * Use it like std::cout
84  *
85  * @code
86  * bfinfo << "hello world" << bfend;
87  * @endcode
88  */
89 #ifndef bfinfo
90 #define bfinfo \
91  std::cout
92 #endif
93 
94 /*
95  * This macro is a shortcut for std::cout that adds some text and color.
96  * Use it like std::cout
97  *
98  * @code
99  * bfdebug << "hello world" << bfend;
100  * @endcode
101  */
102 #ifndef bfdebug
103 #define bfdebug \
104  std::cout << bfcolor_debug << "DEBUG" << bfcolor_end << ": "
105 #endif
106 
107 /*
108  * This macro is a shortcut for std::cout that adds some text and color.
109  * Use it like std::cout
110  *
111  * @code
112  * bfwarning << "hello world" << bfend;
113  * @endcode
114  */
115 #ifndef bfwarning
116 #define bfwarning \
117  std::cerr << bfcolor_warning << "WARNING" << bfcolor_end << ": "
118 #endif
119 
120 /*
121  * This macro is a shortcut for std::cout that adds some text and color.
122  * Use it like std::cout
123  *
124  * @code
125  * bferror << "hello world" << bfend;
126  * @endcode
127  */
128 #ifndef bferror
129 #define bferror \
130  std::cerr << bfcolor_error << "ERROR" << bfcolor_end << ": "
131 #endif
132 
133 /*
134  * This macro is a shortcut for std::cout that adds some text and color.
135  * Use it like std::cout
136  *
137  * @code
138  * bffatal << "hello world" << bfend;
139  * @endcode
140  */
141 #ifndef bffatal
142 #define bffatal \
143  std::cerr << bfcolor_error << "FATAL ERROR" << bfcolor_end << ": "
144 #endif
145 
146 #endif
147 
148 /* -------------------------------------------------------------------------- */
149 /* C Debugging */
150 /* -------------------------------------------------------------------------- */
151 
152 #ifndef KERNEL
153 #include <stdio.h>
154 #define INFO(...) printf(__VA_ARGS__)
155 #define DEBUG(...) printf("[BAREFLANK DEBUG]: " __VA_ARGS__)
156 #define ALERT(...) printf("[BAREFLANK ERROR]: " __VA_ARGS__)
157 #endif
158 
159 /* -------------------------------------------------------------------------- */
160 /* Linux Debugging */
161 /* -------------------------------------------------------------------------- */
162 
163 #ifdef KERNEL
164 #if defined(__linux__)
165 #include <linux/module.h>
166 #define INFO(...) printk(KERN_INFO __VA_ARGS__)
167 #define DEBUG(...) printk(KERN_INFO "[BAREFLANK DEBUG]: " __VA_ARGS__)
168 #define ALERT(...) printk(KERN_ALERT "[BAREFLANK ERROR]: " __VA_ARGS__)
169 #endif
170 #endif
171 
172 /* -------------------------------------------------------------------------- */
173 /* Windows Debugging */
174 /* -------------------------------------------------------------------------- */
175 
176 #ifdef KERNEL
177 #ifdef _WIN32
178 #include <wdm.h>
179 #define INFO(...) DbgPrintEx(DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL, __VA_ARGS__)
180 #define DEBUG(...) DbgPrintEx(DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL, "[BAREFLANK DEBUG]: " __VA_ARGS__)
181 #define ALERT(...) DbgPrintEx(DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, "[BAREFLANK ERROR]: " __VA_ARGS__)
182 #endif
183 #endif
184 
185 /* -------------------------------------------------------------------------- */
186 /* OSX Debugging */
187 /* -------------------------------------------------------------------------- */
188 
189 #ifdef KERNEL
190 #ifdef __APPLE__
191 #include <IOKit/IOLib.h>
192 #define INFO(...) IOLog(__VA_ARGS__)
193 #define DEBUG(...) IOLog("[BAREFLANK DEBUG]: " __VA_ARGS__)
194 #define ALERT(...) IOLog("[BAREFLANK ERROR]: " __VA_ARGS__)
195 #endif
196 #endif
197 
198 #endif
Definition: vcpuid.h:29
const void * view_as_pointer(const T val)