Fix segfault in Python bindings for TRACE_ENABLE=OFF case

This commit is contained in:
Arnim Balzer 2024-03-23 17:43:41 +00:00
parent adbee2f820
commit 93be1d9343
No known key found for this signature in database
GPG Key ID: 785EF903F0917AB7
3 changed files with 52 additions and 56 deletions

View File

@ -9,12 +9,12 @@ namespace py = pybind11;
using OptionalString = std::optional<std::string>; using OptionalString = std::optional<std::string>;
using OptionalInt = std::optional<int>; using OptionalInt = std::optional<int>;
#ifdef TRACY_ENABLE
template <typename Type = uint64_t> template <typename Type = uint64_t>
OptionalNumber MemoryAllocate(const Type &type, std::size_t size, OptionalNumber MemoryAllocate(const Type &type, std::size_t size,
const OptionalString &name = std::nullopt, const OptionalString &name = std::nullopt,
const OptionalNumber &id = std::nullopt, const OptionalNumber &id = std::nullopt,
OptionalInt depth = std::nullopt) { OptionalInt depth = std::nullopt) {
#ifdef TRACY_ENABLE
if (!name && !id) { if (!name && !id) {
if (!depth) if (!depth)
TracyAlloc(reinterpret_cast<void *>(type), size); TracyAlloc(reinterpret_cast<void *>(type), size);
@ -38,27 +38,11 @@ OptionalNumber MemoryAllocate(const Type &type, std::size_t size,
else else
TracyAllocNS(reinterpret_cast<void *>(type), size, *depth, entry.second); TracyAllocNS(reinterpret_cast<void *>(type), size, *depth, entry.second);
return entry.first; return entry.first;
#else
static_cast<void>(type); // unused
static_cast<void>(size); // unused
static_cast<void>(name); // unused
static_cast<void>(id); // unused
static_cast<void>(depth); // unused
#endif
}
template <>
OptionalNumber MemoryAllocate(const py::object &object, std::size_t size,
const OptionalString &name,
const OptionalNumber &id, OptionalInt depth) {
return MemoryAllocate<uint64_t>(reinterpret_cast<uint64_t>(object.ptr()),
size, name, id, depth);
} }
template <typename Type = uint64_t> template <typename Type = uint64_t>
bool MemoryFree(const Type &type, const OptionalNumber &id = std::nullopt, bool MemoryFree(const Type &type, const OptionalNumber &id = std::nullopt,
OptionalInt depth = std::nullopt) { OptionalInt depth = std::nullopt) {
#ifdef TRACY_ENABLE
if (!id) { if (!id) {
if (!depth) if (!depth)
TracyFree(reinterpret_cast<void *>(type)); TracyFree(reinterpret_cast<void *>(type));
@ -75,11 +59,27 @@ bool MemoryFree(const Type &type, const OptionalNumber &id = std::nullopt,
else else
TracyFreeNS(reinterpret_cast<void *>(type), *depth, ptr); TracyFreeNS(reinterpret_cast<void *>(type), *depth, ptr);
return true; return true;
}
#else #else
static_cast<void>(type); // unused
static_cast<void>(id); // unused template <typename Type = uint64_t>
static_cast<void>(depth); // unused OptionalNumber MemoryAllocate(const Type &, std::size_t, const OptionalString &,
const OptionalNumber &, OptionalInt) {
return 0ul;
}
template <typename Type = uint64_t>
bool MemoryFree(const Type &, const OptionalNumber &, OptionalInt) {
return true;
}
#endif #endif
template <>
OptionalNumber MemoryAllocate(const py::object &object, std::size_t size,
const OptionalString &name,
const OptionalNumber &id, OptionalInt depth) {
return MemoryAllocate<uint64_t>(reinterpret_cast<uint64_t>(object.ptr()),
size, name, id, depth);
} }
template <> template <>

View File

@ -703,7 +703,7 @@ PYBIND11_MODULE(TracyClientBindings, m) {
m.def( m.def(
"program_name", "program_name",
[](const std::string &name) { [](const std::string &name) {
if (!tracy::IsEnabled()) return false; if (!tracy::IsEnabled()) return true;
auto entry = NameBuffer::Add(name); auto entry = NameBuffer::Add(name);
if (!entry.first) return false; if (!entry.first) return false;
TracySetProgramName(entry.second); TracySetProgramName(entry.second);
@ -722,7 +722,7 @@ PYBIND11_MODULE(TracyClientBindings, m) {
m.def( m.def(
"app_info", "app_info",
[](const std::string &text) { [](const std::string &text) {
if (!tracy::IsEnabled()) return false; if (!tracy::IsEnabled()) return true;
if (text.size() >= std::numeric_limits<uint16_t>::max()) return false; if (text.size() >= std::numeric_limits<uint16_t>::max()) return false;
TracyAppInfo(text.c_str(), text.size()); TracyAppInfo(text.c_str(), text.size());
return true; return true;
@ -732,7 +732,7 @@ PYBIND11_MODULE(TracyClientBindings, m) {
m.def( m.def(
"message", "message",
[](const std::string &message) { [](const std::string &message) {
if (!tracy::IsEnabled()) return false; if (!tracy::IsEnabled()) return true;
if (message.size() >= std::numeric_limits<uint16_t>::max()) if (message.size() >= std::numeric_limits<uint16_t>::max())
return false; return false;
TracyMessage(message.c_str(), message.size()); TracyMessage(message.c_str(), message.size());
@ -743,7 +743,7 @@ PYBIND11_MODULE(TracyClientBindings, m) {
m.def( m.def(
"message", "message",
[](const std::string &message, uint32_t pColor) { [](const std::string &message, uint32_t pColor) {
if (!tracy::IsEnabled()) return false; if (!tracy::IsEnabled()) return true;
if (message.size() >= std::numeric_limits<uint16_t>::max()) if (message.size() >= std::numeric_limits<uint16_t>::max())
return false; return false;
TracyMessageC(message.c_str(), message.size(), pColor); TracyMessageC(message.c_str(), message.size(), pColor);
@ -756,8 +756,7 @@ PYBIND11_MODULE(TracyClientBindings, m) {
m.def( m.def(
"frame_mark_start", "frame_mark_start",
[](const std::string &name) { [](const std::string &name) {
if (!tracy::IsEnabled()) if (!tracy::IsEnabled()) return static_cast<OptionalNumber>(0ul);
return static_cast<OptionalNumber>(std::nullopt);
auto entry = NameBuffer::Add(name); auto entry = NameBuffer::Add(name);
if (!entry.first) return static_cast<OptionalNumber>(std::nullopt); if (!entry.first) return static_cast<OptionalNumber>(std::nullopt);
FrameMarkStart(entry.second); FrameMarkStart(entry.second);
@ -768,7 +767,7 @@ PYBIND11_MODULE(TracyClientBindings, m) {
m.def( m.def(
"frame_mark_end", "frame_mark_end",
[](std::size_t id) { [](std::size_t id) {
if (!tracy::IsEnabled()) return false; if (!tracy::IsEnabled()) return true;
auto ptr = NameBuffer::Get(id); auto ptr = NameBuffer::Get(id);
if (!ptr) return false; if (!ptr) return false;
FrameMarkEnd(ptr); FrameMarkEnd(ptr);
@ -780,7 +779,7 @@ PYBIND11_MODULE(TracyClientBindings, m) {
"frame_image", "frame_image",
[](const py::bytes &image, uint16_t width, uint16_t height, [](const py::bytes &image, uint16_t width, uint16_t height,
uint8_t offset = 0, bool flip = false) { uint8_t offset = 0, bool flip = false) {
if (!tracy::IsEnabled()) return false; if (!tracy::IsEnabled()) return true;
if (width % 4 != 0 || height % 4 != 0) return false; if (width % 4 != 0 || height % 4 != 0) return false;
TracyCFrameImage(std::string(image).data(), width, height, offset, TracyCFrameImage(std::string(image).data(), width, height, offset,
flip); flip);
@ -823,8 +822,7 @@ PYBIND11_MODULE(TracyClientBindings, m) {
"_plot_config", "_plot_config",
[](const std::string &name, int type, bool step, bool fill, [](const std::string &name, int type, bool step, bool fill,
uint32_t color = 0) { uint32_t color = 0) {
if (!tracy::IsEnabled()) if (!tracy::IsEnabled()) return static_cast<OptionalNumber>(0ul);
return static_cast<OptionalNumber>(std::nullopt);
auto entry = NameBuffer::Add(name); auto entry = NameBuffer::Add(name);
if (!entry.first) return static_cast<OptionalNumber>(std::nullopt); if (!entry.first) return static_cast<OptionalNumber>(std::nullopt);
TracyCPlotConfig(entry.second, type, step, fill, color); TracyCPlotConfig(entry.second, type, step, fill, color);
@ -842,7 +840,7 @@ PYBIND11_MODULE(TracyClientBindings, m) {
m.def( m.def(
"plot", "plot",
[](std::size_t id, double value) { [](std::size_t id, double value) {
if (!tracy::IsEnabled()) return false; if (!tracy::IsEnabled()) return true;
auto ptr = NameBuffer::Get(id); auto ptr = NameBuffer::Get(id);
if (!ptr) return false; if (!ptr) return false;
TracyCPlot(ptr, value); TracyCPlot(ptr, value);
@ -852,7 +850,7 @@ PYBIND11_MODULE(TracyClientBindings, m) {
m.def( m.def(
"plot", "plot",
[](std::size_t id, float value) { [](std::size_t id, float value) {
if (!tracy::IsEnabled()) return false; if (!tracy::IsEnabled()) return true;
auto ptr = NameBuffer::Get(id); auto ptr = NameBuffer::Get(id);
if (!ptr) return false; if (!ptr) return false;
TracyCPlotF(ptr, value); TracyCPlotF(ptr, value);
@ -862,7 +860,7 @@ PYBIND11_MODULE(TracyClientBindings, m) {
m.def( m.def(
"plot", "plot",
[](std::size_t id, int64_t value) { [](std::size_t id, int64_t value) {
if (!tracy::IsEnabled()) return false; if (!tracy::IsEnabled()) return true;
auto ptr = NameBuffer::Get(id); auto ptr = NameBuffer::Get(id);
if (!ptr) return false; if (!ptr) return false;
TracyCPlotI(ptr, value); TracyCPlotI(ptr, value);

View File

@ -23,7 +23,6 @@ bool SetText(const std::string& text, tracy::ScopedZone* zone) {
zone->Text(text.c_str(), text.size()); zone->Text(text.c_str(), text.size());
return true; return true;
} }
#endif
class PyScopedZone { class PyScopedZone {
public: public:
@ -45,47 +44,30 @@ class PyScopedZone {
virtual ~PyScopedZone() { Exit(); }; virtual ~PyScopedZone() { Exit(); };
bool IsActive() const { bool IsActive() const {
#ifdef TRACY_ENABLE
if (!m_zone) return m_active; if (!m_zone) return m_active;
return m_zone->IsActive(); return m_zone->IsActive();
#else
return false;
#endif
} }
template <typename Type> template <typename Type>
bool Text(const Type& text) { bool Text(const Type& text) {
#ifdef TRACY_ENABLE
return SetText(text, m_zone); return SetText(text, m_zone);
#else
static_cast<void>(text); // unused
#endif
} }
bool Name(const std::string& name) { bool Name(const std::string& name) {
#ifdef TRACY_ENABLE
if (name.size() >= std::numeric_limits<uint16_t>::max()) return false; if (name.size() >= std::numeric_limits<uint16_t>::max()) return false;
m_name = name; m_name = name;
if (!m_zone) return true; if (!m_zone) return true;
m_zone->Name(m_name->c_str(), m_name->size()); m_zone->Name(m_name->c_str(), m_name->size());
return true; return true;
#else
static_cast<void>(name); // unused
#endif
} }
void Color(uint32_t color) { void Color(uint32_t color) {
#ifdef TRACY_ENABLE
m_color = color; m_color = color;
if (!m_zone) return; if (!m_zone) return;
m_zone->Color(m_color); m_zone->Color(m_color);
#else
static_cast<void>(color); // unused
#endif
} }
void Enter() { void Enter() {
#ifdef TRACY_ENABLE
if (m_depth) if (m_depth)
m_zone = new tracy::ScopedZone( m_zone = new tracy::ScopedZone(
m_line, m_source.c_str(), m_source.size(), m_function.c_str(), m_line, m_source.c_str(), m_source.size(), m_function.c_str(),
@ -96,14 +78,11 @@ class PyScopedZone {
m_line, m_source.c_str(), m_source.size(), m_function.c_str(), m_line, m_source.c_str(), m_source.size(), m_function.c_str(),
m_function.size(), m_name ? m_name->c_str() : nullptr, m_function.size(), m_name ? m_name->c_str() : nullptr,
m_name ? m_name->size() : 0ul, m_color, m_active); m_name ? m_name->size() : 0ul, m_color, m_active);
#endif
} }
void Exit() { void Exit() {
#ifdef TRACY_ENABLE
if (m_zone) delete m_zone; if (m_zone) delete m_zone;
m_zone = nullptr; m_zone = nullptr;
#endif
} }
private: private:
@ -116,7 +95,26 @@ class PyScopedZone {
std::string m_source; std::string m_source;
uint32_t m_line; uint32_t m_line;
#ifdef TRACY_ENABLE
tracy::ScopedZone* m_zone; tracy::ScopedZone* m_zone;
#endif
}; };
#else
class PyScopedZone {
public:
PyScopedZone(const std::optional<std::string>&, uint32_t, std::optional<int>,
bool, const std::string&, const std::string&, uint32_t line) {}
virtual ~PyScopedZone(){};
bool IsActive() const { return false; }
template <typename Type>
bool Text(const Type&) {
return true;
}
bool Name(const std::string&) { return true; }
void Color(uint32_t) {}
void Enter() {}
void Exit() {}
};
#endif