New util library

- Add string_printf/string_vprintf.
- Add warning and error with backtrace support.

Change-Id: I3dd73b4caed0d767bd9e39ffef15ff8484d0b0bf
Bu işleme şunda yer alıyor:
Laurent Morichetti
2022-08-17 14:25:41 -07:00
ebeveyn 993dcf9503
işleme 80d363a4bc
5 değiştirilmiş dosya ile 265 ekleme ve 4 silme
+117
Dosyayı Görüntüle
@@ -0,0 +1,117 @@
/* Copyright (c) 2022 Advanced Micro Devices, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. */
#include "debug.h"
#include "util.h"
#include <cstdarg>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#if defined(ENABLE_BACKTRACE)
#include <cxxabi.h>
#include <backtrace.h>
namespace {
struct BackTraceInfo {
struct ::backtrace_state* state = nullptr;
std::stringstream sstream{};
int depth = 0;
int error = 0;
};
void errorCallback(void* data, const char* message, int errnum) {
BackTraceInfo* info = static_cast<BackTraceInfo*>(data);
info->sstream << "Error: " << message << '(' << errnum << ')';
info->error = 1;
}
void syminfoCallback(void* data, uintptr_t /* pc */, const char* symname, uintptr_t /* symval */,
uintptr_t /* symsize */) {
BackTraceInfo* info = static_cast<BackTraceInfo*>(data);
if (symname == nullptr) return;
int status;
char* demangled = abi::__cxa_demangle(symname, nullptr, nullptr, &status);
info->sstream << ' ' << (status == 0 ? demangled : symname);
free(demangled);
}
int fullCallback(void* data, uintptr_t pc, const char* filename, int lineno, const char* function) {
BackTraceInfo* info = static_cast<BackTraceInfo*>(data);
info->sstream << std::endl
<< " #" << std::dec << info->depth++ << ' ' << std::showbase << std::hex
<< std::setfill('0') << std::setw(sizeof(pc) * 2) << pc;
if (function == nullptr)
backtrace_syminfo(info->state, pc, syminfoCallback, errorCallback, data);
else {
int status;
char* demangled = abi::__cxa_demangle(function, nullptr, nullptr, &status);
info->sstream << ' ' << (status == 0 ? demangled : function);
free(demangled);
if (filename != nullptr) {
info->sstream << " in " << filename;
if (lineno) info->sstream << ':' << std::dec << lineno;
}
}
return info->error;
}
} // namespace
#endif // defined (ENABLE_BACKTRACE)
namespace roctracer {
void warning(const char* format, ...) {
va_list va;
va_start(va, format);
std::cerr << "Warning: " << string_vprintf(format, va) << std::endl;
va_end(va);
}
void fatal [[noreturn]] (const char* format, ...) {
va_list va;
va_start(va, format);
std::string message = string_vprintf(format, va);
va_end(va);
#if defined(ENABLE_BACKTRACE)
BackTraceInfo info;
info.sstream << std::endl << "Backtrace:";
info.state = ::backtrace_create_state("/proc/self/exe", 0, errorCallback, &info);
::backtrace_full(info.state, 1, fullCallback, errorCallback, &info);
message += info.sstream.str();
#endif /* defined (ENABLE_BACKTRACE) */
std::cerr << "Error: " << message << std::endl;
abort();
}
} // namespace roctracer
+37
Dosyayı Görüntüle
@@ -0,0 +1,37 @@
/* Copyright (c) 2022 Advanced Micro Devices, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. */
#pragma once
namespace roctracer {
extern void warning(const char* format, ...)
#if defined(__GNUC__)
__attribute__((format(printf, 1, 2)))
#endif // defined (__GNUC__)
;
extern void fatal [[noreturn]] (const char* format, ...)
#if defined(__GNUC__)
__attribute__((format(printf, 1, 2)))
#endif // defined (__GNUC__)
;
} // namespace roctracer
+51
Dosyayı Görüntüle
@@ -0,0 +1,51 @@
/* Copyright (c) 2022 Advanced Micro Devices, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. */
#include "util.h"
#include <cstdio>
#include <cstdarg>
#include <string>
namespace roctracer {
std::string string_vprintf(const char* format, va_list va) {
va_list copy;
va_copy(copy, va);
size_t size = vsnprintf(NULL, 0, format, copy);
va_end(copy);
std::string str(size, '\0');
vsprintf(&str[0], format, va);
return str;
}
std::string string_printf(const char* format, ...) {
va_list va;
va_start(va, format);
std::string str(string_vprintf(format, va));
va_end(va);
return str;
}
} // namespace roctracer
+36
Dosyayı Görüntüle
@@ -0,0 +1,36 @@
/* Copyright (c) 2022 Advanced Micro Devices, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. */
#pragma once
#include <cstdarg>
#include <string>
namespace roctracer {
extern std::string string_vprintf(const char* format, va_list va);
extern std::string string_printf(const char* format, ...)
#if defined(__GNUC__)
__attribute__((format(printf, 1, 2)))
#endif // defined (__GNUC__)
;
} // namespace roctracer