83 lines · cpp
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-exception6//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 21namespace orc_rt {22 23#ifndef NDEBUG24 25std::atomic<const char *> DebugTypes;26char DebugTypesAll;27char DebugTypesNone;28 29/// Sets the DebugState and DebugTypes values -- this function may be called30/// concurrently on multiple threads, but will always assign the same values so31/// this should be safe.32const char *initializeDebug() {33 if (const char *DT = getenv("ORC_RT_DEBUG")) {34 // If ORC_RT_DEBUG=1 then log everything.35 if (strcmp(DT, "1") == 0) {36 DebugTypes.store(&DebugTypesAll, std::memory_order_relaxed);37 return &DebugTypesAll;38 }39 40 // If ORC_RT_DEBUG is non-empty then record the string for use in41 // debugTypeEnabled.42 if (strcmp(DT, "") != 0) {43 DebugTypes.store(DT, std::memory_order_relaxed);44 return DT;45 }46 }47 48 // If ORT_RT_DEBUG is undefined or defined as empty then log nothing.49 DebugTypes.store(&DebugTypesNone, std::memory_order_relaxed);50 return &DebugTypesNone;51}52 53bool debugTypeEnabled(const char *Type, const char *Types) {54 assert(Types && Types != &DebugTypesAll && Types != &DebugTypesNone &&55 "Invalid Types value");56 size_t TypeLen = strlen(Type);57 const char *Start = Types;58 const char *End = Start;59 60 do {61 if (*End == '\0' || *End == ',') {62 size_t ItemLen = End - Start;63 if (ItemLen == TypeLen && memcmp(Type, Start, TypeLen) == 0)64 return true;65 if (*End == '\0')66 return false;67 Start = End + 1;68 }69 ++End;70 } while (true);71}72 73void printdbg(const char *format, ...) {74 va_list Args;75 va_start(Args, format);76 vfprintf(stderr, format, Args);77 va_end(Args);78}79 80#endif // !NDEBUG81 82} // namespace orc_rt83