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