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