summaryrefslogtreecommitdiff
path: root/Libraries/LibCore/CEventLoop.cpp
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-08-17 11:35:09 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-08-17 11:39:26 +0200
commit1febd59f830d0c5ee240b73c7fd376c18f76f754 (patch)
tree16ad6282ca0b0a525546b0cc7bd6413b7b60fc15 /Libraries/LibCore/CEventLoop.cpp
parent2fa2d72761d4cbe3b7b305bbd0205e5b05fe4899 (diff)
downloadserenity-1febd59f830d0c5ee240b73c7fd376c18f76f754.zip
LibCore+rpcdump: Publish CObject graph to on-demand RPC socket
All programs that have a CEventLoop now allow local socket connections via /tmp/rpc.PID and will dump a serialized JSON array of all the live CObjects in the program onto connecting sockets. Also added a small /bin/rpcdump tool that connects to an RPC socket and produces a raw dump of the JSON that comes out.
Diffstat (limited to 'Libraries/LibCore/CEventLoop.cpp')
-rw-r--r--Libraries/LibCore/CEventLoop.cpp31
1 files changed, 31 insertions, 0 deletions
diff --git a/Libraries/LibCore/CEventLoop.cpp b/Libraries/LibCore/CEventLoop.cpp
index 2e50891b15..26adf6fd77 100644
--- a/Libraries/LibCore/CEventLoop.cpp
+++ b/Libraries/LibCore/CEventLoop.cpp
@@ -1,6 +1,10 @@
+#include <AK/JsonArray.h>
+#include <AK/JsonObject.h>
+#include <AK/JsonValue.h>
#include <AK/Time.h>
#include <LibCore/CEvent.h>
#include <LibCore/CEventLoop.h>
+#include <LibCore/CLocalSocket.h>
#include <LibCore/CLock.h>
#include <LibCore/CNotifier.h>
#include <LibCore/CObject.h>
@@ -25,6 +29,7 @@ HashMap<int, NonnullOwnPtr<CEventLoop::EventLoopTimer>>* CEventLoop::s_timers;
HashTable<CNotifier*>* CEventLoop::s_notifiers;
int CEventLoop::s_next_timer_id = 1;
int CEventLoop::s_wake_pipe_fds[2];
+CLocalServer CEventLoop::s_rpc_server;
CEventLoop::CEventLoop()
{
@@ -39,6 +44,32 @@ CEventLoop::CEventLoop()
int rc = pipe2(s_wake_pipe_fds, O_CLOEXEC);
ASSERT(rc == 0);
s_event_loop_stack->append(this);
+
+
+ auto rpc_path = String::format("/tmp/rpc.%d", getpid());
+ rc = unlink(rpc_path.characters());
+ if (rc < 0 && errno != ENOENT) {
+ perror("unlink");
+ ASSERT_NOT_REACHED();
+ }
+ bool listening = s_rpc_server.listen(rpc_path);
+ ASSERT(listening);
+
+ s_rpc_server.on_ready_to_accept = [&] {
+ auto* client = s_rpc_server.accept();
+ ASSERT(client);
+ JsonArray objects;
+ for (auto& object : CObject::all_objects()) {
+ JsonObject json_object;
+ json_object.set("class_name", object.class_name());
+ json_object.set("address", String::format("%p", &object));
+ json_object.set("name", object.name());
+ json_object.set("parent", String::format("%p", object.parent()));
+ objects.append(move(json_object));
+ }
+ client->write(objects.to_string());
+ client->delete_later();
+ };
}
#ifdef CEVENTLOOP_DEBUG