Skip to content

Commit 0ce75e3

Browse files
committed
debug: Add TypeName() function and log statements for Proxy objects being created and destroyed
1 parent cc08c23 commit 0ce75e3

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

include/mp/proxy-io.h

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

411411
{
412+
m_context.loop->log() << "Creating " << TypeName(*this) << " " << this;
413+
412414
// Handler for the connection getting destroyed before this client object.
413415
auto disconnect_cb = m_context.connection->addSyncCleanup([this]() {
414416
// Release client capability by move-assigning to temporary.
@@ -465,13 +467,16 @@ ProxyClientBase<Interface, Impl>::ProxyClientBase(typename Interface::Client cli
465467
template <typename Interface, typename Impl>
466468
ProxyClientBase<Interface, Impl>::~ProxyClientBase() noexcept
467469
{
470+
m_context.loop->log() << "Cleaning up " << TypeName(*this) << " " << this;
468471
CleanupRun(m_context.cleanup_fns);
472+
m_context.loop->log() << "Destroying " << TypeName(*this) << " " << this;
469473
}
470474

471475
template <typename Interface, typename Impl>
472476
ProxyServerBase<Interface, Impl>::ProxyServerBase(std::shared_ptr<Impl> impl, Connection& connection)
473477
: m_impl(std::move(impl)), m_context(&connection)
474478
{
479+
m_context.loop->log() << "Creating " << TypeName(*this) << " " << this;
475480
assert(m_impl);
476481
}
477482

@@ -490,6 +495,7 @@ ProxyServerBase<Interface, Impl>::ProxyServerBase(std::shared_ptr<Impl> impl, Co
490495
template <typename Interface, typename Impl>
491496
ProxyServerBase<Interface, Impl>::~ProxyServerBase()
492497
{
498+
m_context.loop->log() << "Cleaning up " << TypeName(*this) << " " << this;
493499
if (m_impl) {
494500
// If impl is non-null at this point, it means no client is waiting for
495501
// the m_impl server object to be destroyed synchronously. This can
@@ -516,6 +522,7 @@ ProxyServerBase<Interface, Impl>::~ProxyServerBase()
516522
});
517523
}
518524
assert(m_context.cleanup_fns.empty());
525+
m_context.loop->log() << "Destroying " << TypeName(*this) << " " << this;
519526
}
520527

521528
//! 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
@@ -568,16 +568,16 @@ template <typename Client>
568568
void clientDestroy(Client& client)
569569
{
570570
if (client.m_context.connection) {
571-
client.m_context.loop->log() << "IPC client destroy " << typeid(client).name();
571+
client.m_context.loop->log() << "IPC client destroy " << TypeName(client);
572572
} else {
573-
KJ_LOG(INFO, "IPC interrupted client destroy", typeid(client).name());
573+
KJ_LOG(INFO, "IPC interrupted client destroy", TypeName(client));
574574
}
575575
}
576576

577577
template <typename Server>
578578
void serverDestroy(Server& server)
579579
{
580-
server.m_context.loop->log() << "IPC server destroy " << typeid(server).name();
580+
server.m_context.loop->log() << "IPC server destroy " << TypeName(server);
581581
}
582582

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

include/mp/util.h

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

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

23+
#if defined(__GNUG__) // GCC & Clang ─ use <cxxabi.h> to demangle
24+
# include <cxxabi.h>
25+
# include <memory>
26+
#endif
27+
2228
namespace mp {
2329

2430
//! Generic utility functions used by capnp code.
@@ -238,6 +244,24 @@ inline char* CharCast(unsigned char* c) { return (char*)c; }
238244
inline const char* CharCast(const char* c) { return c; }
239245
inline const char* CharCast(const unsigned char* c) { return (const char*)c; }
240246

247+
#if defined(__GNUG__) // GCC & Clang ─ use <cxxabi.h> to demangle
248+
inline std::string _demangle(const char* m)
249+
{
250+
int status = 0;
251+
std::unique_ptr<char, void(*)(void*)> p{
252+
abi::__cxa_demangle(m, nullptr, nullptr, &status), std::free};
253+
return (status == 0 && p) ? p.get() : m; // fall back on mangled if needed
254+
}
255+
#else // MSVC or other ─ no demangling available
256+
inline std::string _demangle(const char* m) { return m; }
257+
#endif
258+
259+
template<class T>
260+
std::string TypeName(const T& /*unused*/)
261+
{
262+
return _demangle(typeid(std::decay_t<T>).name());
263+
}
264+
241265
} // namespace mp
242266

243267
#endif // MP_UTIL_H

src/mp/gen.cpp

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

0 commit comments

Comments
 (0)