llvm-project
83 строки · 2.2 Кб
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() {
34if (const char *DT = getenv("ORC_RT_DEBUG")) {
35// If ORC_RT_DEBUG=1 then log everything.
36if (strcmp(DT, "1") == 0) {
37DebugTypes.store(&DebugTypesAll, std::memory_order_relaxed);
38return &DebugTypesAll;
39}
40
41// If ORC_RT_DEBUG is non-empty then record the string for use in
42// debugTypeEnabled.
43if (strcmp(DT, "") != 0) {
44DebugTypes.store(DT, std::memory_order_relaxed);
45return DT;
46}
47}
48
49// If ORT_RT_DEBUG is undefined or defined as empty then log nothing.
50DebugTypes.store(&DebugTypesNone, std::memory_order_relaxed);
51return &DebugTypesNone;
52}
53
54bool debugTypeEnabled(const char *Type, const char *Types) {
55assert(Types && Types != &DebugTypesAll && Types != &DebugTypesNone &&
56"Invalid Types value");
57size_t TypeLen = strlen(Type);
58const char *Start = Types;
59const char *End = Start;
60
61do {
62if (*End == '\0' || *End == ',') {
63size_t ItemLen = End - Start;
64if (ItemLen == TypeLen && memcmp(Type, Start, TypeLen) == 0)
65return true;
66if (*End == '\0')
67return false;
68Start = End + 1;
69}
70++End;
71} while (true);
72}
73
74void printdbg(const char *format, ...) {
75va_list Args;
76va_start(Args, format);
77vfprintf(stderr, format, Args);
78va_end(Args);
79}
80
81#endif // !NDEBUG
82
83} // end namespace __orc_rt
84