Skip to content

Commit 4eae445

Browse files
committed
debug: Add TypeName() function and log statements for Proxy objects being created and destroyed
1 parent f326c5b commit 4eae445

4 files changed

Lines changed: 41 additions & 4 deletions

File tree

include/mp/proxy-io.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ ProxyClientBase<Interface, Impl>::ProxyClientBase(typename Interface::Client cli
511511
: m_client(std::move(client)), m_context(connection)
512512

513513
{
514+
MP_LOG(*m_context.loop, Log::Debug) << "Creating " << CxxTypeName(*this) << " " << this;
514515
// Handler for the connection getting destroyed before this client object.
515516
auto disconnect_cb = m_context.connection->addSyncCleanup([this]() {
516517
// Release client capability by move-assigning to temporary.
@@ -567,13 +568,16 @@ ProxyClientBase<Interface, Impl>::ProxyClientBase(typename Interface::Client cli
567568
template <typename Interface, typename Impl>
568569
ProxyClientBase<Interface, Impl>::~ProxyClientBase() noexcept
569570
{
571+
MP_LOG(*m_context.loop, Log::Debug) << "Cleaning up " << CxxTypeName(*this) << " " << this;
570572
CleanupRun(m_context.cleanup_fns);
573+
MP_LOG(*m_context.loop, Log::Debug) << "Destroying " << CxxTypeName(*this) << " " << this;
571574
}
572575

573576
template <typename Interface, typename Impl>
574577
ProxyServerBase<Interface, Impl>::ProxyServerBase(std::shared_ptr<Impl> impl, Connection& connection)
575578
: m_impl(std::move(impl)), m_context(&connection)
576579
{
580+
MP_LOG(*m_context.loop, Log::Debug) << "Creating " << CxxTypeName(*this) << " " << this;
577581
assert(m_impl);
578582
}
579583

@@ -592,6 +596,7 @@ ProxyServerBase<Interface, Impl>::ProxyServerBase(std::shared_ptr<Impl> impl, Co
592596
template <typename Interface, typename Impl>
593597
ProxyServerBase<Interface, Impl>::~ProxyServerBase()
594598
{
599+
MP_LOG(*m_context.loop, Log::Debug) << "Cleaning up " << CxxTypeName(*this) << " " << this;
595600
if (m_impl) {
596601
// If impl is non-null at this point, it means no client is waiting for
597602
// the m_impl server object to be destroyed synchronously. This can
@@ -618,6 +623,7 @@ ProxyServerBase<Interface, Impl>::~ProxyServerBase()
618623
});
619624
}
620625
assert(m_context.cleanup_fns.empty());
626+
MP_LOG(*m_context.loop, Log::Debug) << "Destroying " << CxxTypeName(*this) << " " << this;
621627
}
622628

623629
//! If the capnp interface defined a special "destroy" method, as described the

include/mp/proxy-types.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -645,16 +645,16 @@ template <typename Client>
645645
void clientDestroy(Client& client)
646646
{
647647
if (client.m_context.connection) {
648-
MP_LOG(*client.m_context.loop, Log::Debug) << "IPC client destroy " << typeid(client).name();
648+
MP_LOG(*client.m_context.loop, Log::Debug) << "IPC client destroy " << CxxTypeName(client);
649649
} else {
650-
KJ_LOG(INFO, "IPC interrupted client destroy", typeid(client).name());
650+
KJ_LOG(INFO, "IPC interrupted client destroy", CxxTypeName(client));
651651
}
652652
}
653653

654654
template <typename Server>
655655
void serverDestroy(Server& server)
656656
{
657-
MP_LOG(*server.m_context.loop, Log::Debug) << "IPC server destroy " << typeid(server).name();
657+
MP_LOG(*server.m_context.loop, Log::Debug) << "IPC server destroy " << CxxTypeName(server);
658658
}
659659

660660
//! Entry point called by generated client code that looks like:

include/mp/util.h

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,25 @@
77

88
#include <capnp/schema.h>
99
#include <cassert>
10-
#include <cstddef>
10+
#include <cstdlib>
1111
#include <cstring>
1212
#include <exception>
1313
#include <functional>
1414
#include <kj/string-tree.h>
1515
#include <mutex>
1616
#include <string>
1717
#include <tuple>
18+
#include <typeinfo>
1819
#include <type_traits>
1920
#include <utility>
2021
#include <variant>
2122
#include <vector>
2223

24+
#if __has_include(<cxxabi.h>)
25+
#include <cxxabi.h>
26+
#include <memory>
27+
#endif
28+
2329
namespace mp {
2430

2531
//! Generic utility functions used by capnp code.
@@ -274,6 +280,28 @@ inline char* CharCast(unsigned char* c) { return (char*)c; }
274280
inline const char* CharCast(const char* c) { return c; }
275281
inline const char* CharCast(const unsigned char* c) { return (const char*)c; }
276282

283+
#if __has_include(<cxxabi.h>) // GCC & Clang ─ use <cxxabi.h> to demangle
284+
inline std::string _demangle(const char* m)
285+
{
286+
int status = 0;
287+
std::unique_ptr<char, void(*)(void*)> p{
288+
abi::__cxa_demangle(m, /*output_buffer=*/nullptr, /*length=*/nullptr, &status), std::free};
289+
return (status == 0 && p) ? p.get() : m; // fall back on mangled if needed
290+
}
291+
#else // MSVC or other ─ no demangling available
292+
inline std::string _demangle(const char* m) { return m; }
293+
#endif
294+
295+
template<class T>
296+
std::string CxxTypeName(const T& /*unused*/)
297+
{
298+
#ifdef __cpp_rtti
299+
return _demangle(typeid(std::decay_t<T>).name());
300+
#else
301+
return "<type information unavailable without rtti>";
302+
#endif
303+
}
304+
277305
//! Exception thrown from code executing an IPC call that is interrupted.
278306
struct InterruptException final : std::exception {
279307
explicit InterruptException(std::string message) : m_message(std::move(message)) {}

src/mp/gen.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,11 @@ static void Generate(kj::StringPtr src_prefix,
240240
cpp_types << "// Generated by " PROXY_BIN " from " << src_file << "\n\n";
241241
cpp_types << "// IWYU pragma: no_include \"mp/proxy.h\"\n";
242242
cpp_types << "// IWYU pragma: no_include \"mp/proxy-io.h\"\n";
243+
cpp_types << "#include <" << include_path << ".h> // IWYU pragma: keep\n";
243244
cpp_types << "#include <" << include_path << ".proxy.h>\n";
244245
cpp_types << "#include <" << include_path << ".proxy-types.h> // IWYU pragma: keep\n";
246+
cpp_types << "#include <kj/common.h>\n";
247+
cpp_types << "#include <mp/util.h>\n";
245248
cpp_types << "#include <" << PROXY_TYPES << ">\n\n";
246249
cpp_types << "namespace mp {\n";
247250

0 commit comments

Comments
 (0)