Path: blob/main/contrib/llvm-project/compiler-rt/lib/orc/debug.cpp
39566 views
//===- debug.cpp ----------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file is a part of the ORC runtime support library.9//10//===----------------------------------------------------------------------===//1112#include "debug.h"1314#include <cassert>15#include <cstdarg>16#include <cstdio>17#include <cstdlib>18#include <cstring>192021namespace __orc_rt {2223#ifndef NDEBUG2425std::atomic<const char *> DebugTypes;26char DebugTypesAll;27char DebugTypesNone;2829/// 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() {33if (const char *DT = getenv("ORC_RT_DEBUG")) {34// If ORC_RT_DEBUG=1 then log everything.35if (strcmp(DT, "1") == 0) {36DebugTypes.store(&DebugTypesAll, std::memory_order_relaxed);37return &DebugTypesAll;38}3940// If ORC_RT_DEBUG is non-empty then record the string for use in41// debugTypeEnabled.42if (strcmp(DT, "") != 0) {43DebugTypes.store(DT, std::memory_order_relaxed);44return DT;45}46}4748// If ORT_RT_DEBUG is undefined or defined as empty then log nothing.49DebugTypes.store(&DebugTypesNone, std::memory_order_relaxed);50return &DebugTypesNone;51}5253bool debugTypeEnabled(const char *Type, const char *Types) {54assert(Types && Types != &DebugTypesAll && Types != &DebugTypesNone &&55"Invalid Types value");56size_t TypeLen = strlen(Type);57const char *Start = Types;58const char *End = Start;5960do {61if (*End == '\0' || *End == ',') {62size_t ItemLen = End - Start;63if (ItemLen == TypeLen && memcmp(Type, Start, TypeLen) == 0)64return true;65if (*End == '\0')66return false;67Start = End + 1;68}69++End;70} while (true);71}7273void printdbg(const char *format, ...) {74va_list Args;75va_start(Args, format);76vfprintf(stderr, format, Args);77va_end(Args);78}7980#endif // !NDEBUG8182} // end namespace __orc_rt838485