Skip to content

Commit 56d6ff2

Browse files
committed
debug: Add TypeName() function and log statements for Proxy objects being created and destroyed
1 parent 3a3e892 commit 56d6ff2

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

include/mp/proxy-io.h

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

497497
{
498+
MP_LOG(*m_context.loop, Log::Info) << "Creating " << CxxTypeName(*this) << " " << this;
499+
498500
// Handler for the connection getting destroyed before this client object.
499501
auto disconnect_cb = m_context.connection->addSyncCleanup([this]() {
500502
// Release client capability by move-assigning to temporary.
@@ -551,13 +553,16 @@ ProxyClientBase<Interface, Impl>::ProxyClientBase(typename Interface::Client cli
551553
template <typename Interface, typename Impl>
552554
ProxyClientBase<Interface, Impl>::~ProxyClientBase() noexcept
553555
{
556+
MP_LOG(*m_context.loop, Log::Info) << "Cleaning up " << CxxTypeName(*this) << " " << this;
554557
CleanupRun(m_context.cleanup_fns);
558+
MP_LOG(*m_context.loop, Log::Info) << "Destroying " << CxxTypeName(*this) << " " << this;
555559
}
556560

557561
template <typename Interface, typename Impl>
558562
ProxyServerBase<Interface, Impl>::ProxyServerBase(std::shared_ptr<Impl> impl, Connection& connection)
559563
: m_impl(std::move(impl)), m_context(&connection)
560564
{
565+
MP_LOG(*m_context.loop, Log::Info) << "Creating " << CxxTypeName(*this) << " " << this;
561566
assert(m_impl);
562567
}
563568

@@ -576,6 +581,7 @@ ProxyServerBase<Interface, Impl>::ProxyServerBase(std::shared_ptr<Impl> impl, Co
576581
template <typename Interface, typename Impl>
577582
ProxyServerBase<Interface, Impl>::~ProxyServerBase()
578583
{
584+
MP_LOG(*m_context.loop, Log::Info) << "Cleaning up " << CxxTypeName(*this) << " " << this;
579585
if (m_impl) {
580586
// If impl is non-null at this point, it means no client is waiting for
581587
// the m_impl server object to be destroyed synchronously. This can
@@ -602,6 +608,7 @@ ProxyServerBase<Interface, Impl>::~ProxyServerBase()
602608
});
603609
}
604610
assert(m_context.cleanup_fns.empty());
611+
MP_LOG(*m_context.loop, Log::Info) << "Destroying " << CxxTypeName(*this) << " " << this;
605612
}
606613

607614
//! 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
@@ -595,16 +595,16 @@ template <typename Client>
595595
void clientDestroy(Client& client)
596596
{
597597
if (client.m_context.connection) {
598-
MP_LOG(*client.m_context.loop, Log::Info) << "IPC client destroy " << typeid(client).name();
598+
MP_LOG(*client.m_context.loop, Log::Info) << "IPC client destroy " << CxxTypeName(client);
599599
} else {
600-
KJ_LOG(INFO, "IPC interrupted client destroy", typeid(client).name());
600+
KJ_LOG(INFO, "IPC interrupted client destroy", CxxTypeName(client));
601601
}
602602
}
603603

604604
template <typename Server>
605605
void serverDestroy(Server& server)
606606
{
607-
MP_LOG(*server.m_context.loop, Log::Info) << "IPC server destroy " << typeid(server).name();
607+
MP_LOG(*server.m_context.loop, Log::Info) << "IPC server destroy " << CxxTypeName(server);
608608
}
609609

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

include/mp/util.h

Lines changed: 30 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, nullptr, 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)) {}
@@ -337,6 +365,7 @@ void CancelMonitor::promiseDestroyed(CancelProbe& probe)
337365
if (m_on_cancel) m_on_cancel();
338366
m_probe = nullptr;
339367
}
368+
340369
} // namespace mp
341370

342371
#endif // MP_UTIL_H

src/mp/gen.cpp

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

0 commit comments

Comments
 (0)