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
22namespace __orc_rt {
23
24#ifndef NDEBUG
25
26std::atomic<const char *> DebugTypes;
27char DebugTypesAll;
28char 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.
33const char *initializeDebug() {
34 if (const char *DT = getenv(name: "ORC_RT_DEBUG")) {
35 // If ORC_RT_DEBUG=1 then log everything.
36 if (strcmp(s1: DT, s2: "1") == 0) {
37 DebugTypes.store(p: &DebugTypesAll, m: 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(s1: DT, s2: "") != 0) {
44 DebugTypes.store(p: DT, m: 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(p: &DebugTypesNone, m: std::memory_order_relaxed);
51 return &DebugTypesNone;
52}
53
54bool debugTypeEnabled(const char *Type, const char *Types) {
55 assert(Types && Types != &DebugTypesAll && Types != &DebugTypesNone &&
56 "Invalid Types value");
57 size_t TypeLen = strlen(s: 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(s1: Type, s2: Start, n: TypeLen) == 0)
65 return true;
66 if (*End == '\0')
67 return false;
68 Start = End + 1;
69 }
70 ++End;
71 } while (true);
72}
73
74void printdbg(const char *format, ...) {
75 va_list Args;
76 va_start(Args, format);
77 vfprintf(stderr, format: format, arg: Args);
78 va_end(Args);
79}
80
81#endif // !NDEBUG
82
83} // end namespace __orc_rt
84

source code of compiler-rt/lib/orc/debug.cpp