Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 1 | // Copyright 2018 The SwiftShader Authors. All Rights Reserved. |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "Debug.hpp" |
| 16 | |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 17 | #include <atomic> |
| 18 | #include <cstdarg> |
| 19 | #include <cstdio> |
| 20 | #include <string> |
| 21 | |
| 22 | #if __ANDROID__ |
| 23 | # include <android/log.h> |
| 24 | #endif |
| 25 | |
| 26 | #if defined(__unix__) |
| 27 | # define PTRACE |
| 28 | # include <sys/ptrace.h> |
| 29 | # include <sys/types.h> |
| 30 | #elif defined(_WIN32) || defined(_WIN64) |
| 31 | # include <windows.h> |
| 32 | #elif defined(__APPLE__) || defined(__MACH__) |
| 33 | # include <sys/sysctl.h> |
| 34 | # include <unistd.h> |
| 35 | #endif |
| 36 | |
| 37 | #ifdef ERROR |
| 38 | # undef ERROR // b/127920555 |
| 39 | #endif |
| 40 | |
Ben Clayton | 4bc07ad | 2020-02-07 20:26:19 +0000 | [diff] [blame] | 41 | #ifndef SWIFTSHADER_LOGGING_LEVEL |
Ben Clayton | 196d817 | 2020-02-10 21:42:18 +0000 | [diff] [blame] | 42 | # define SWIFTSHADER_LOGGING_LEVEL Info |
Ben Clayton | 4bc07ad | 2020-02-07 20:26:19 +0000 | [diff] [blame] | 43 | #endif |
| 44 | |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 45 | namespace { |
| 46 | |
| 47 | bool IsUnderDebugger() |
| 48 | { |
| 49 | #if defined(PTRACE) && !defined(__APPLE__) && !defined(__MACH__) |
| 50 | static bool checked = false; |
| 51 | static bool res = false; |
| 52 | |
| 53 | if(!checked) |
| 54 | { |
| 55 | // If a debugger is attached then we're already being ptraced and ptrace |
| 56 | // will return a non-zero value. |
| 57 | checked = true; |
| 58 | if(ptrace(PTRACE_TRACEME, 0, 1, 0) != 0) |
| 59 | { |
| 60 | res = true; |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | ptrace(PTRACE_DETACH, 0, 1, 0); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return res; |
| 69 | #elif defined(_WIN32) || defined(_WIN64) |
| 70 | return IsDebuggerPresent() != 0; |
| 71 | #elif defined(__APPLE__) || defined(__MACH__) |
| 72 | // Code comes from the Apple Technical Q&A QA1361 |
| 73 | |
| 74 | // Tell sysctl what info we're requestion. Specifically we're asking for |
| 75 | // info about this our PID. |
| 76 | int res = 0; |
| 77 | int request[4] = { |
| 78 | CTL_KERN, |
| 79 | KERN_PROC, |
| 80 | KERN_PROC_PID, |
| 81 | getpid() |
| 82 | }; |
| 83 | struct kinfo_proc info; |
| 84 | size_t size = sizeof(info); |
| 85 | |
| 86 | info.kp_proc.p_flag = 0; |
| 87 | |
| 88 | // Get the info we're requesting, if sysctl fails then info.kp_proc.p_flag will remain 0. |
| 89 | res = sysctl(request, sizeof(request) / sizeof(*request), &info, &size, NULL, 0); |
| 90 | ASSERT_MSG(res == 0, "syscl returned %d", res); |
| 91 | |
| 92 | // We're being debugged if the P_TRACED flag is set |
| 93 | return ((info.kp_proc.p_flag & P_TRACED) != 0); |
| 94 | #else |
| 95 | return false; |
| 96 | #endif |
| 97 | } |
| 98 | |
| 99 | enum class Level |
| 100 | { |
Sean Risser | c1e60dc | 2020-02-19 12:52:24 -0500 | [diff] [blame] | 101 | Verbose, |
Ben Clayton | 196d817 | 2020-02-10 21:42:18 +0000 | [diff] [blame] | 102 | Debug, |
| 103 | Info, |
| 104 | Warn, |
| 105 | Error, |
| 106 | Fatal, |
Sean Risser | c1e60dc | 2020-02-19 12:52:24 -0500 | [diff] [blame] | 107 | Disabled, |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | #ifdef __ANDROID__ |
| 111 | void logv_android(Level level, const char *msg) |
| 112 | { |
| 113 | switch(level) |
| 114 | { |
Ben Clayton | 196d817 | 2020-02-10 21:42:18 +0000 | [diff] [blame] | 115 | case Level::Debug: |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 116 | __android_log_write(ANDROID_LOG_DEBUG, "SwiftShader", msg); |
| 117 | break; |
Ben Clayton | 196d817 | 2020-02-10 21:42:18 +0000 | [diff] [blame] | 118 | case Level::Info: |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 119 | __android_log_write(ANDROID_LOG_INFO, "SwiftShader", msg); |
| 120 | break; |
Ben Clayton | 196d817 | 2020-02-10 21:42:18 +0000 | [diff] [blame] | 121 | case Level::Warn: |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 122 | __android_log_write(ANDROID_LOG_WARN, "SwiftShader", msg); |
| 123 | break; |
Ben Clayton | 196d817 | 2020-02-10 21:42:18 +0000 | [diff] [blame] | 124 | case Level::Error: |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 125 | __android_log_write(ANDROID_LOG_ERROR, "SwiftShader", msg); |
| 126 | break; |
Ben Clayton | 196d817 | 2020-02-10 21:42:18 +0000 | [diff] [blame] | 127 | case Level::Fatal: |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 128 | __android_log_write(ANDROID_LOG_FATAL, "SwiftShader", msg); |
| 129 | break; |
Sean Risser | c1e60dc | 2020-02-19 12:52:24 -0500 | [diff] [blame] | 130 | default: |
| 131 | break; |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | #else |
| 135 | void logv_std(Level level, const char *msg) |
| 136 | { |
| 137 | switch(level) |
| 138 | { |
Ben Clayton | 196d817 | 2020-02-10 21:42:18 +0000 | [diff] [blame] | 139 | case Level::Debug: |
| 140 | case Level::Info: |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 141 | fprintf(stdout, "%s", msg); |
| 142 | break; |
Ben Clayton | 196d817 | 2020-02-10 21:42:18 +0000 | [diff] [blame] | 143 | case Level::Warn: |
| 144 | case Level::Error: |
| 145 | case Level::Fatal: |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 146 | fprintf(stderr, "%s", msg); |
| 147 | break; |
Sean Risser | c1e60dc | 2020-02-19 12:52:24 -0500 | [diff] [blame] | 148 | default: |
| 149 | break; |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | #endif |
| 153 | |
| 154 | void logv(Level level, const char *format, va_list args) |
| 155 | { |
Sean Risser | c1e60dc | 2020-02-19 12:52:24 -0500 | [diff] [blame] | 156 | if(static_cast<int>(level) >= static_cast<int>(Level::SWIFTSHADER_LOGGING_LEVEL)) |
Ben Clayton | 4bc07ad | 2020-02-07 20:26:19 +0000 | [diff] [blame] | 157 | { |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 158 | #ifndef SWIFTSHADER_DISABLE_TRACE |
Sean Risser | c1e60dc | 2020-02-19 12:52:24 -0500 | [diff] [blame] | 159 | char buffer[2048]; |
| 160 | vsnprintf(buffer, sizeof(buffer), format, args); |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 161 | |
| 162 | # if defined(__ANDROID__) |
Sean Risser | c1e60dc | 2020-02-19 12:52:24 -0500 | [diff] [blame] | 163 | logv_android(level, buffer); |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 164 | # elif defined(_WIN32) |
Sean Risser | c1e60dc | 2020-02-19 12:52:24 -0500 | [diff] [blame] | 165 | logv_std(level, buffer); |
| 166 | ::OutputDebugString(buffer); |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 167 | # else |
Sean Risser | c1e60dc | 2020-02-19 12:52:24 -0500 | [diff] [blame] | 168 | logv_std(level, buffer); |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 169 | # endif |
Sean Risser | c1e60dc | 2020-02-19 12:52:24 -0500 | [diff] [blame] | 170 | } |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 171 | |
Sean Risser | c1e60dc | 2020-02-19 12:52:24 -0500 | [diff] [blame] | 172 | const Level traceToFileLevel = Level::Disabled; |
| 173 | if(static_cast<int>(level) >= static_cast<int>(traceToFileLevel)) |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 174 | { |
| 175 | FILE *file = fopen(TRACE_OUTPUT_FILE, "a"); |
| 176 | |
| 177 | if(file) |
| 178 | { |
| 179 | vfprintf(file, format, args); |
| 180 | fclose(file); |
| 181 | } |
| 182 | } |
| 183 | #endif // SWIFTSHADER_DISABLE_TRACE |
| 184 | } |
| 185 | |
| 186 | } // anonymous namespace |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 187 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 188 | namespace sw { |
| 189 | |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 190 | void trace(const char *format, ...) |
| 191 | { |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 192 | va_list vararg; |
| 193 | va_start(vararg, format); |
Ben Clayton | 196d817 | 2020-02-10 21:42:18 +0000 | [diff] [blame] | 194 | logv(Level::Debug, format, vararg); |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 195 | va_end(vararg); |
| 196 | } |
| 197 | |
| 198 | void warn(const char *format, ...) |
| 199 | { |
| 200 | va_list vararg; |
| 201 | va_start(vararg, format); |
Ben Clayton | 196d817 | 2020-02-10 21:42:18 +0000 | [diff] [blame] | 202 | logv(Level::Warn, format, vararg); |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 203 | va_end(vararg); |
| 204 | } |
| 205 | |
| 206 | void abort(const char *format, ...) |
| 207 | { |
| 208 | va_list vararg; |
| 209 | |
| 210 | va_start(vararg, format); |
Ben Clayton | 196d817 | 2020-02-10 21:42:18 +0000 | [diff] [blame] | 211 | logv(Level::Fatal, format, vararg); |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 212 | va_end(vararg); |
| 213 | |
| 214 | ::abort(); |
| 215 | } |
| 216 | |
Sean Risser | c1e60dc | 2020-02-19 12:52:24 -0500 | [diff] [blame] | 217 | void log_trap(const char *format, ...) |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 218 | { |
Sean Risser | c1e60dc | 2020-02-19 12:52:24 -0500 | [diff] [blame] | 219 | // If enabled, log_assert will log all messages, and otherwise ignore them |
| 220 | // unless a debugger is attached. |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 221 | static std::atomic<bool> asserted = { false }; |
| 222 | if(IsUnderDebugger() && !asserted.exchange(true)) |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 223 | { |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 224 | // Abort after tracing and printing to stderr |
| 225 | va_list vararg; |
| 226 | va_start(vararg, format); |
Ben Clayton | 196d817 | 2020-02-10 21:42:18 +0000 | [diff] [blame] | 227 | logv(Level::Fatal, format, vararg); |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 228 | va_end(vararg); |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 229 | |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 230 | ::abort(); |
| 231 | } |
| 232 | else if(!asserted) |
| 233 | { |
| 234 | va_list vararg; |
| 235 | va_start(vararg, format); |
Sean Risser | c1e60dc | 2020-02-19 12:52:24 -0500 | [diff] [blame] | 236 | logv(Level::Verbose, format, vararg); |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 237 | va_end(vararg); |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 238 | } |
| 239 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 240 | |
| 241 | } // namespace sw |