1 //===--- Debug.cpp -------- Debug utilities ----------------------- C++ -*-===//
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 contains debug utilities
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "Debug.h"
14 #include "Configuration.h"
15 #include "Mapping.h"
16 #include "Types.h"
17 
18 using namespace _OMP;
19 
20 #pragma omp declare target
21 
22 extern "C" {
23 void __assert_assume(bool cond, const char *exp, const char *file, int line) {
24   if (!cond && config::isDebugMode(config::DebugKind::Assertion)) {
25     PRINTF("ASSERTION failed: %s at %s, line %d\n", exp, file, line);
26     __builtin_trap();
27   }
28 
29   __builtin_assume(cond);
30 }
31 
32 void __assert_fail(const char *assertion, const char *file, unsigned line,
33                    const char *function) {
34   PRINTF("%s:%u: %s: Assertion `%s' failed.\n", file, line, function,
35          assertion);
36   __builtin_trap();
37 }
38 }
39 
40 /// Current indentation level for the function trace. Only accessed by thread 0.
41 static uint32_t Level = 0;
42 #pragma omp allocate(Level) allocator(omp_pteam_mem_alloc)
43 
44 DebugEntryRAII::DebugEntryRAII(const unsigned Line, const char *Function) {
45   if (config::isDebugMode(config::DebugKind::FunctionTracing) &&
46       mapping::getThreadIdInBlock() == 0) {
47 
48     for (int I = 0; I < Level; ++I)
49       PRINTF("%s", "  ");
50 
51     PRINTF("Line %u: Thread %u Entering %s:%u\n", Line,
52            mapping::getThreadIdInBlock(), Function);
53     Level++;
54   }
55 }
56 
57 DebugEntryRAII::~DebugEntryRAII() {
58   if (config::isDebugMode(config::DebugKind::FunctionTracing) &&
59       mapping::getThreadIdInBlock() == 0)
60     Level--;
61 }
62 
63 #pragma omp end declare target
64