1 //===------- Debug.h - Target independent OpenMP target RTL -- C++ --------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Routines used to provide debug messages and information from libomptarget
10 // and plugin RTLs to the user.
11 //
12 // Each plugin RTL and libomptarget define TARGET_NAME and DEBUG_PREFIX for use
13 // when sending messages to the user. These indicate which RTL sent the message
14 //
15 // Debug and information messages are controlled by the environment variables
16 // LIBOMPTARGET_DEBUG and LIBOMPTARGET_INFO which is set upon initialization
17 // of libomptarget or the plugin RTL.
18 //
19 // To printf a pointer in hex with a fixed width of 16 digits and a leading 0x,
20 // use printf("ptr=" DPxMOD "...\n", DPxPTR(ptr));
21 //
22 // DPxMOD expands to:
23 // "0x%0*" PRIxPTR
24 // where PRIxPTR expands to an appropriate modifier for the type uintptr_t on a
25 // specific platform, e.g. "lu" if uintptr_t is typedef'd as unsigned long:
26 // "0x%0*lu"
27 //
28 // Ultimately, the whole statement expands to:
29 // printf("ptr=0x%0*lu...\n", // the 0* modifier expects an extra argument
30 // // specifying the width of the output
31 // (int)(2*sizeof(uintptr_t)), // the extra argument specifying the width
32 // // 8 digits for 32bit systems
33 // // 16 digits for 64bit
34 // (uintptr_t) ptr);
35 //
36 //===----------------------------------------------------------------------===//
37 #ifndef _OMPTARGET_DEBUG_H
38 #define _OMPTARGET_DEBUG_H
39
getInfoLevel()40 static inline int getInfoLevel() {
41 static int InfoLevel = -1;
42 if (InfoLevel >= 0)
43 return InfoLevel;
44
45 if (char *EnvStr = getenv("LIBOMPTARGET_INFO"))
46 InfoLevel = std::stoi(EnvStr);
47
48 return InfoLevel;
49 }
50
getDebugLevel()51 static inline int getDebugLevel() {
52 static int DebugLevel = -1;
53 if (DebugLevel >= 0)
54 return DebugLevel;
55
56 if (char *EnvStr = getenv("LIBOMPTARGET_DEBUG"))
57 DebugLevel = std::stoi(EnvStr);
58
59 return DebugLevel;
60 }
61
62 #ifndef __STDC_FORMAT_MACROS
63 #define __STDC_FORMAT_MACROS
64 #endif
65 #include <inttypes.h>
66 #undef __STDC_FORMAT_MACROS
67
68 #define DPxMOD "0x%0*" PRIxPTR
69 #define DPxPTR(ptr) ((int)(2 * sizeof(uintptr_t))), ((uintptr_t)(ptr))
70 #define GETNAME2(name) #name
71 #define GETNAME(name) GETNAME2(name)
72
73 /// Print a generic message string from libomptarget or a plugin RTL
74 #define MESSAGE0(_str) \
75 do { \
76 fprintf(stderr, GETNAME(TARGET_NAME) " message: %s\n", _str); \
77 } while (0)
78
79 /// Print a printf formatting string message from libomptarget or a plugin RTL
80 #define MESSAGE(_str, ...) \
81 do { \
82 fprintf(stderr, GETNAME(TARGET_NAME) " message: " _str "\n", __VA_ARGS__); \
83 } while (0)
84
85 /// Print fatal error message with an error string and error identifier
86 #define FATAL_MESSAGE0(_num, _str) \
87 do { \
88 fprintf(stderr, GETNAME(TARGET_NAME) " fatal error %d: %s\n", _num, _str); \
89 abort(); \
90 } while (0)
91
92 /// Print fatal error message with a printf string and error identifier
93 #define FATAL_MESSAGE(_num, _str, ...) \
94 do { \
95 fprintf(stderr, GETNAME(TARGET_NAME) " fatal error %d:" _str "\n", _num, \
96 __VA_ARGS__); \
97 abort(); \
98 } while (0)
99
100 /// Print a generic error string from libomptarget or a plugin RTL
101 #define FAILURE_MESSAGE(...) \
102 do { \
103 fprintf(stderr, GETNAME(TARGET_NAME) " error: "); \
104 fprintf(stderr, __VA_ARGS__); \
105 } while (0)
106
107 /// Print a generic information string used if LIBOMPTARGET_INFO=1
108 #define INFO_MESSAGE(_num, ...) \
109 do { \
110 fprintf(stderr, GETNAME(TARGET_NAME) " device %d info: ", _num); \
111 fprintf(stderr, __VA_ARGS__); \
112 } while (0)
113
114 // Debugging messages
115 #ifdef OMPTARGET_DEBUG
116 #include <stdio.h>
117
118 #define DEBUGP(prefix, ...) \
119 { \
120 fprintf(stderr, "%s --> ", prefix); \
121 fprintf(stderr, __VA_ARGS__); \
122 }
123
124 /// Emit a message for debugging
125 #define DP(...) \
126 do { \
127 if (getDebugLevel() > 0) { \
128 DEBUGP(DEBUG_PREFIX, __VA_ARGS__); \
129 } \
130 } while (false)
131
132 /// Emit a message for debugging or failure if debugging is disabled
133 #define REPORT(...) \
134 do { \
135 if (getDebugLevel() > 0) { \
136 DP(__VA_ARGS__); \
137 } else { \
138 FAILURE_MESSAGE(__VA_ARGS__); \
139 } \
140 } while (false)
141 #else
142 #define DEBUGP(prefix, ...) \
143 {}
144 #define DP(...) \
145 {}
146 #define REPORT(...) FAILURE_MESSAGE(__VA_ARGS__);
147 #endif // OMPTARGET_DEBUG
148
149 /// Emit a message giving the user extra information about the runtime if
150 #define INFO(_id, ...) \
151 do { \
152 if (getDebugLevel() > 0) { \
153 DEBUGP(DEBUG_PREFIX, __VA_ARGS__); \
154 } else if (getInfoLevel() > 0) { \
155 INFO_MESSAGE(_id, __VA_ARGS__); \
156 } \
157 } while (false)
158
159 #endif // _OMPTARGET_DEBUG_H
160