|
| 1 | +//===- ErrorReporting.h - Helper to provide nice error messages ----- 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 | +//===----------------------------------------------------------------------===// |
| 10 | + |
| 11 | +#ifndef OFFLOAD_PLUGINS_NEXTGEN_COMMON_ERROR_REPORTING_H |
| 12 | +#define OFFLOAD_PLUGINS_NEXTGEN_COMMON_ERROR_REPORTING_H |
| 13 | + |
| 14 | +#include "PluginInterface.h" |
| 15 | +#include "Shared/EnvironmentVar.h" |
| 16 | + |
| 17 | +#include "llvm/ADT/SmallString.h" |
| 18 | +#include "llvm/ADT/StringRef.h" |
| 19 | +#include "llvm/Support/ErrorHandling.h" |
| 20 | +#include "llvm/Support/WithColor.h" |
| 21 | +#include "llvm/Support/raw_ostream.h" |
| 22 | + |
| 23 | +#include <cstdint> |
| 24 | +#include <cstdio> |
| 25 | +#include <cstdlib> |
| 26 | +#include <functional> |
| 27 | +#include <optional> |
| 28 | +#include <string> |
| 29 | +#include <unistd.h> |
| 30 | + |
| 31 | +namespace llvm { |
| 32 | +namespace omp { |
| 33 | +namespace target { |
| 34 | +namespace plugin { |
| 35 | + |
| 36 | +class ErrorReporter { |
| 37 | + |
| 38 | + enum ColorTy { |
| 39 | + Yellow = int(HighlightColor::Address), |
| 40 | + Green = int(HighlightColor::String), |
| 41 | + DarkBlue = int(HighlightColor::Tag), |
| 42 | + Cyan = int(HighlightColor::Attribute), |
| 43 | + DarkPurple = int(HighlightColor::Enumerator), |
| 44 | + DarkRed = int(HighlightColor::Macro), |
| 45 | + BoldRed = int(HighlightColor::Error), |
| 46 | + BoldLightPurple = int(HighlightColor::Warning), |
| 47 | + BoldDarkGrey = int(HighlightColor::Note), |
| 48 | + BoldLightBlue = int(HighlightColor::Remark), |
| 49 | + }; |
| 50 | + |
| 51 | + /// The banner printed at the beginning of an error report. |
| 52 | + static constexpr auto ErrorBanner = "OFFLOAD ERROR: "; |
| 53 | + |
| 54 | + /// Return the device id as string, or n/a if not available. |
| 55 | + static std::string getDeviceIdStr(GenericDeviceTy *Device) { |
| 56 | + return Device ? std::to_string(Device->getDeviceId()) : "n/a"; |
| 57 | + } |
| 58 | + |
| 59 | + /// Return a nice name for an TargetAllocTy. |
| 60 | + static StringRef getAllocTyName(TargetAllocTy Kind) { |
| 61 | + switch (Kind) { |
| 62 | + case TARGET_ALLOC_DEVICE_NON_BLOCKING: |
| 63 | + case TARGET_ALLOC_DEFAULT: |
| 64 | + case TARGET_ALLOC_DEVICE: |
| 65 | + return "device memory"; |
| 66 | + case TARGET_ALLOC_HOST: |
| 67 | + return "pinned host memory"; |
| 68 | + case TARGET_ALLOC_SHARED: |
| 69 | + return "managed memory"; |
| 70 | + break; |
| 71 | + } |
| 72 | + llvm_unreachable("Unknown target alloc kind"); |
| 73 | + } |
| 74 | + |
| 75 | +#pragma clang diagnostic push |
| 76 | +#pragma clang diagnostic ignored "-Wgcc-compat" |
| 77 | +#pragma clang diagnostic ignored "-Wformat-security" |
| 78 | + /// Print \p Format, instantiated with \p Args to stderr. |
| 79 | + /// TODO: Allow redirection into a file stream. |
| 80 | + template <typename... ArgsTy> |
| 81 | + [[gnu::format(__printf__, 1, 2)]] static void print(const char *Format, |
| 82 | + ArgsTy &&...Args) { |
| 83 | + raw_fd_ostream OS(STDERR_FILENO, false); |
| 84 | + OS << llvm::format(Format, Args...); |
| 85 | + } |
| 86 | + |
| 87 | + /// Print \p Format, instantiated with \p Args to stderr, but colored. |
| 88 | + /// TODO: Allow redirection into a file stream. |
| 89 | + template <typename... ArgsTy> |
| 90 | + [[gnu::format(__printf__, 2, 3)]] static void |
| 91 | + print(ColorTy Color, const char *Format, ArgsTy &&...Args) { |
| 92 | + raw_fd_ostream OS(STDERR_FILENO, false); |
| 93 | + WithColor(OS, HighlightColor(Color)) << llvm::format(Format, Args...); |
| 94 | + } |
| 95 | + |
| 96 | + /// Print \p Format, instantiated with \p Args to stderr, but colored and with |
| 97 | + /// a banner. |
| 98 | + /// TODO: Allow redirection into a file stream. |
| 99 | + template <typename... ArgsTy> |
| 100 | + [[gnu::format(__printf__, 1, 2)]] static void reportError(const char *Format, |
| 101 | + ArgsTy &&...Args) { |
| 102 | + print(BoldRed, "%s", ErrorBanner); |
| 103 | + print(BoldRed, Format, Args...); |
| 104 | + print("\n"); |
| 105 | + } |
| 106 | +#pragma clang diagnostic pop |
| 107 | + |
| 108 | + static void reportError(const char *Str) { reportError("%s", Str); } |
| 109 | + static void print(const char *Str) { print("%s", Str); } |
| 110 | + static void print(StringRef Str) { print("%s", Str.str().c_str()); } |
| 111 | + static void print(ColorTy Color, const char *Str) { print(Color, "%s", Str); } |
| 112 | + static void print(ColorTy Color, StringRef Str) { |
| 113 | + print(Color, "%s", Str.str().c_str()); |
| 114 | + } |
| 115 | + |
| 116 | + /// Pretty print a stack trace. |
| 117 | + static void reportStackTrace(StringRef StackTrace) { |
| 118 | + if (StackTrace.empty()) |
| 119 | + return; |
| 120 | + |
| 121 | + SmallVector<StringRef> Lines, Parts; |
| 122 | + StackTrace.split(Lines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false); |
| 123 | + int Start = Lines.empty() || !Lines[0].contains("PrintStackTrace") ? 0 : 1; |
| 124 | + unsigned NumDigits = |
| 125 | + (int)(floor(log10(Lines.size() - Start - /*0*/ 1)) + 1); |
| 126 | + for (int I = Start, E = Lines.size(); I < E; ++I) { |
| 127 | + auto Line = Lines[I]; |
| 128 | + Parts.clear(); |
| 129 | + Line = Line.drop_while([](char C) { return std::isspace(C); }); |
| 130 | + Line.split(Parts, " ", /*MaxSplit=*/2); |
| 131 | + if (Parts.size() != 3 || Parts[0].size() < 2 || Parts[0][0] != '#') { |
| 132 | + print("%s\n", Line.str().c_str()); |
| 133 | + continue; |
| 134 | + } |
| 135 | + unsigned FrameIdx = std::stoi(Parts[0].drop_front(1).str()); |
| 136 | + if (Start) |
| 137 | + FrameIdx -= 1; |
| 138 | + print(DarkPurple, " %s", Parts[0].take_front().str().c_str()); |
| 139 | + print(Green, "%*u", NumDigits, FrameIdx); |
| 140 | + print(BoldLightBlue, " %s", Parts[1].str().c_str()); |
| 141 | + print(" %s\n", Parts[2].str().c_str()); |
| 142 | + } |
| 143 | + print("\n"); |
| 144 | + } |
| 145 | + |
| 146 | + /// Report information about an allocation associated with \p ATI. |
| 147 | + static void reportAllocationInfo(AllocationTraceInfoTy *ATI) { |
| 148 | + if (!ATI) |
| 149 | + return; |
| 150 | + |
| 151 | + if (!ATI->DeallocationTrace.empty()) { |
| 152 | + print(BoldLightPurple, "Last deallocation:\n"); |
| 153 | + reportStackTrace(ATI->DeallocationTrace); |
| 154 | + } |
| 155 | + |
| 156 | + if (ATI->HostPtr) |
| 157 | + print(BoldLightPurple, |
| 158 | + "Last allocation of size %lu for host pointer %p:\n", ATI->Size, |
| 159 | + ATI->HostPtr); |
| 160 | + else |
| 161 | + print(BoldLightPurple, "Last allocation of size %lu:\n", ATI->Size); |
| 162 | + reportStackTrace(ATI->AllocationTrace); |
| 163 | + if (!ATI->LastAllocationInfo) |
| 164 | + return; |
| 165 | + |
| 166 | + unsigned I = 0; |
| 167 | + print(BoldLightPurple, "Prior allocations with the same base pointer:"); |
| 168 | + while (ATI->LastAllocationInfo) { |
| 169 | + print("\n"); |
| 170 | + ATI = ATI->LastAllocationInfo; |
| 171 | + print(BoldLightPurple, " #%u Prior deallocation of size %lu:\n", I, |
| 172 | + ATI->Size); |
| 173 | + reportStackTrace(ATI->DeallocationTrace); |
| 174 | + if (ATI->HostPtr) |
| 175 | + print(BoldLightPurple, " #%u Prior allocation for host pointer %p:\n", |
| 176 | + I, ATI->HostPtr); |
| 177 | + else |
| 178 | + print(BoldLightPurple, " #%u Prior allocation:\n", I); |
| 179 | + reportStackTrace(ATI->AllocationTrace); |
| 180 | + ++I; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + /// End the execution of the program. |
| 185 | + static void abortExecution() { abort(); } |
| 186 | + |
| 187 | +public: |
| 188 | +#define DEALLOCATION_ERROR(Format, ...) \ |
| 189 | + reportError(Format, __VA_ARGS__); \ |
| 190 | + reportStackTrace(StackTrace); \ |
| 191 | + reportAllocationInfo(ATI); \ |
| 192 | + abortExecution(); |
| 193 | + |
| 194 | + static void reportDeallocationOfNonAllocatedPtr(void *DevicePtr, |
| 195 | + TargetAllocTy Kind, |
| 196 | + AllocationTraceInfoTy *ATI, |
| 197 | + std::string &StackTrace) { |
| 198 | + DEALLOCATION_ERROR("deallocation of non-allocated %s: %p", |
| 199 | + getAllocTyName(Kind).data(), DevicePtr); |
| 200 | + } |
| 201 | + |
| 202 | + static void reportDeallocationOfDeallocatedPtr(void *DevicePtr, |
| 203 | + TargetAllocTy Kind, |
| 204 | + AllocationTraceInfoTy *ATI, |
| 205 | + std::string &StackTrace) { |
| 206 | + DEALLOCATION_ERROR("double-free of %s: %p", getAllocTyName(Kind).data(), |
| 207 | + DevicePtr); |
| 208 | + } |
| 209 | + |
| 210 | + static void reportDeallocationOfWrongPtrKind(void *DevicePtr, |
| 211 | + TargetAllocTy Kind, |
| 212 | + AllocationTraceInfoTy *ATI, |
| 213 | + std::string &StackTrace) { |
| 214 | + DEALLOCATION_ERROR("deallocation requires %s but allocation was %s: %p", |
| 215 | + getAllocTyName(Kind).data(), |
| 216 | + getAllocTyName(ATI->Kind).data(), DevicePtr); |
| 217 | +#undef DEALLOCATION_ERROR |
| 218 | + } |
| 219 | +}; |
| 220 | + |
| 221 | +} // namespace plugin |
| 222 | +} // namespace target |
| 223 | +} // namespace omp |
| 224 | +} // namespace llvm |
| 225 | + |
| 226 | +#endif // OFFLOAD_PLUGINS_NEXTGEN_COMMON_ERROR_REPORTING_H |
0 commit comments