1 //===- debug.cpp ----------------------------------------------------------===//
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 // This file is a part of the ORC runtime support library.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "debug.h"
14
15 #include <cassert>
16 #include <cstdarg>
17 #include <cstdio>
18 #include <cstdlib>
19 #include <cstring>
20
21
22 namespace __orc_rt {
23
24 #ifndef NDEBUG
25
26 std::atomic<const char *> DebugTypes;
27 char DebugTypesAll;
28 char DebugTypesNone;
29
30 /// Sets the DebugState and DebugTypes values -- this function may be called
31 /// concurrently on multiple threads, but will always assign the same values so
32 /// this should be safe.
initializeDebug()33 const char *initializeDebug() {
34 if (const char *DT = getenv("ORC_RT_DEBUG")) {
35 // If ORC_RT_DEBUG=1 then log everything.
36 if (strcmp(DT, "1") == 0) {
37 DebugTypes.store(&DebugTypesAll, std::memory_order_relaxed);
38 return &DebugTypesAll;
39 }
40
41 // If ORC_RT_DEBUG is non-empty then record the string for use in
42 // debugTypeEnabled.
43 if (strcmp(DT, "") != 0) {
44 DebugTypes.store(DT, std::memory_order_relaxed);
45 return DT;
46 }
47 }
48
49 // If ORT_RT_DEBUG is undefined or defined as empty then log nothing.
50 DebugTypes.store(&DebugTypesNone, std::memory_order_relaxed);
51 return &DebugTypesNone;
52 }
53
debugTypeEnabled(const char * Type,const char * Types)54 bool debugTypeEnabled(const char *Type, const char *Types) {
55 assert(Types && Types != &DebugTypesAll && Types != &DebugTypesNone &&
56 "Invalid Types value");
57 size_t TypeLen = strlen(Type);
58 const char *Start = Types;
59 const char *End = Start;
60
61 do {
62 if (*End == '\0' || *End == ',') {
63 size_t ItemLen = End - Start;
64 if (ItemLen == TypeLen && memcmp(Type, Start, TypeLen) == 0)
65 return true;
66 if (*End == '\0')
67 return false;
68 Start = End + 1;
69 }
70 ++End;
71 } while (true);
72 }
73
printdbg(const char * format,...)74 void printdbg(const char *format, ...) {
75 va_list Args;
76 va_start(Args, format);
77 vfprintf(stderr, format, Args);
78 va_end(Args);
79 }
80
81 #endif // !NDEBUG
82
83 } // end namespace __orc_rt
84