فهرست منبع

Inspector: Add a GUI tool for viewing a remote process's CObject graph

Here comes the foundation for a neat remote debugging tool.

Right now, it connects to a remote process's CEventLoop RPC socket and
retreives the remote object graph JSON dump. The remote object graph
is then reconstructed and exposed through a GModel subclass, which is
then displayed in a GTreeView.

It's pretty cool, I think. :^)
Andreas Kling 6 سال پیش
والد
کامیت
05cd178477

+ 23 - 0
DevTools/Inspector/Makefile

@@ -0,0 +1,23 @@
+include ../../Makefile.common
+
+OBJS = \
+    RemoteObjectGraphModel.o \
+    main.o
+
+APP = Inspector
+
+DEFINES += -DUSERLAND
+
+all: $(APP)
+
+$(APP): $(OBJS)
+	$(LD) -o $(APP) $(LDFLAGS) $(OBJS) -lgui -ldraw -lcore -lc
+
+.cpp.o:
+	@echo "CXX $<"; $(CXX) $(CXXFLAGS) -o $@ -c $<
+
+-include $(OBJS:%.o=%.d)
+
+clean:
+	@echo "CLEAN"; rm -f $(APP) $(OBJS) *.d
+

+ 107 - 0
DevTools/Inspector/RemoteObjectGraphModel.cpp

@@ -0,0 +1,107 @@
+#include "RemoteObjectGraphModel.h"
+#include <AK/JsonObject.h>
+#include <AK/JsonValue.h>
+#include <LibDraw/PNGLoader.h>
+#include <LibGUI/GApplication.h>
+#include <stdio.h>
+
+RemoteObjectGraphModel::RemoteObjectGraphModel(pid_t pid)
+    : m_pid(pid)
+{
+    m_object_icon.set_bitmap_for_size(16, load_png("/res/icons/gear16.png"));
+}
+
+RemoteObjectGraphModel::~RemoteObjectGraphModel()
+{
+}
+
+GModelIndex RemoteObjectGraphModel::index(int row, int column, const GModelIndex& parent) const
+{
+    if (!parent.is_valid()) {
+        if (m_remote_roots.is_empty())
+            return {};
+        return create_index(row, column, &m_remote_roots.at(row));
+    }
+    auto& remote_parent = *static_cast<RemoteObject*>(parent.internal_data());
+    return create_index(row, column, remote_parent.children.at(row));
+}
+
+int RemoteObjectGraphModel::row_count(const GModelIndex& index) const
+{
+    if (!index.is_valid())
+        return m_remote_roots.size();
+    auto& remote_object = *static_cast<RemoteObject*>(index.internal_data());
+    return remote_object.children.size();
+}
+
+int RemoteObjectGraphModel::column_count(const GModelIndex&) const
+{
+    return 1;
+}
+
+GVariant RemoteObjectGraphModel::data(const GModelIndex& index, Role role) const
+{
+    auto* remote_object = static_cast<RemoteObject*>(index.internal_data());
+    if (role == Role::Icon) {
+        return m_object_icon;
+    }
+    if (role == Role::Display) {
+        return String::format("%s{%s} (%d)", remote_object->class_name.characters(), remote_object->address.characters(), remote_object->children.size());
+    }
+    return {};
+}
+
+void RemoteObjectGraphModel::update()
+{
+    auto success = m_socket.connect(CSocketAddress::local(String::format("/tmp/rpc.%d", m_pid)));
+    if (!success) {
+        fprintf(stderr, "Couldn't connect to PID %d\n", m_pid);
+        exit(1);
+    }
+
+    m_socket.on_connected = [this] {
+        dbg() << "Connected to PID " << m_pid;
+    };
+
+    m_socket.on_ready_to_read = [this] {
+        if (m_socket.eof()) {
+            dbg() << "Disconnected from PID " << m_pid;
+            m_socket.close();
+            return;
+        }
+
+        auto data = m_socket.read_all();
+        auto json_value = JsonValue::from_string(data);
+        ASSERT(json_value.is_array());
+        m_json = json_value.as_array();
+
+        Vector<NonnullOwnPtr<RemoteObject>> remote_objects;
+        HashMap<String, RemoteObject*> objects_by_address;
+
+        for (auto& value : m_json.values()) {
+            ASSERT(value.is_object());
+            auto& object = value.as_object();
+            auto remote_object = make<RemoteObject>();
+            remote_object->address = object.get("address").to_string();
+            remote_object->parent_address = object.get("parent").to_string();
+            remote_object->name = object.get("name").to_string();
+            remote_object->class_name = object.get("class_name").to_string();
+            objects_by_address.set(remote_object->address, remote_object);
+            remote_objects.append(move(remote_object));
+        }
+
+        for (int i = 0; i < remote_objects.size(); ++i) {
+            auto& remote_object = remote_objects[i];
+            auto* parent = objects_by_address.get(remote_object->parent_address).value_or(nullptr);
+            if (!parent) {
+                m_remote_roots.append(move(remote_object));
+            } else {
+                remote_object->parent = parent;
+                parent->children.append(move(remote_object));
+            }
+        }
+
+        //dbg() << m_json.to_string();
+        did_update();
+    };
+}

+ 41 - 0
DevTools/Inspector/RemoteObjectGraphModel.h

@@ -0,0 +1,41 @@
+#pragma once
+
+#include <AK/JsonArray.h>
+#include <AK/NonnullOwnPtrVector.h>
+#include <LibCore/CLocalSocket.h>
+#include <LibGUI/GModel.h>
+
+class RemoteObjectGraphModel final : public GModel {
+public:
+    static NonnullRefPtr<RemoteObjectGraphModel> create_with_pid(pid_t pid)
+    {
+        return adopt(*new RemoteObjectGraphModel(pid));
+    }
+
+    virtual ~RemoteObjectGraphModel() override;
+
+    virtual int row_count(const GModelIndex& = GModelIndex()) const override;
+    virtual int column_count(const GModelIndex& = GModelIndex()) const override;
+    virtual GVariant data(const GModelIndex&, Role = Role::Display) const override;
+    virtual GModelIndex index(int row, int column, const GModelIndex& parent = GModelIndex()) const override;
+    virtual void update() override;
+
+private:
+    struct RemoteObject {
+        RemoteObject* parent { nullptr };
+        Vector<OwnPtr<RemoteObject>> children;
+
+        String address;
+        String parent_address;
+        String class_name;
+        String name;
+    };
+
+    explicit RemoteObjectGraphModel(pid_t);
+
+    pid_t m_pid { -1 };
+    CLocalSocket m_socket;
+    JsonArray m_json;
+    NonnullOwnPtrVector<RemoteObject> m_remote_roots;
+    GIcon m_object_icon;
+};

+ 41 - 0
DevTools/Inspector/main.cpp

@@ -0,0 +1,41 @@
+#include "RemoteObjectGraphModel.h"
+#include <LibGUI/GApplication.h>
+#include <LibGUI/GBoxLayout.h>
+#include <LibGUI/GTreeView.h>
+#include <LibGUI/GWindow.h>
+#include <stdio.h>
+
+[[noreturn]] static void print_usage_and_exit()
+{
+    printf("usage: Inspector <pid>\n");
+    exit(0);
+}
+
+int main(int argc, char** argv)
+{
+    if (argc != 2)
+        print_usage_and_exit();
+
+    bool ok;
+    pid_t pid = String(argv[1]).to_int(ok);
+    if (!ok)
+        print_usage_and_exit();
+
+    GApplication app(argc, argv);
+
+    auto* window = new GWindow;
+    window->set_title("Inspector");
+    window->set_rect(150, 150, 300, 500);
+
+    auto* widget = new GWidget;
+    window->set_main_widget(widget);
+    widget->set_fill_with_background_color(true);
+    widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
+
+    auto* tree_view = new GTreeView(widget);
+    tree_view->set_model(RemoteObjectGraphModel::create_with_pid(pid));
+    tree_view->model()->update();
+
+    window->show();
+    return app.exec();
+}

+ 2 - 0
Kernel/build-root-filesystem.sh

@@ -91,6 +91,7 @@ cp ../Demos/RetroFetch/RetroFetch mnt/bin/RetroFetch
 cp ../Demos/WidgetGallery/WidgetGallery mnt/bin/WidgetGallery
 cp ../Demos/Fire/Fire mnt/bin/Fire
 cp ../DevTools/VisualBuilder/VisualBuilder mnt/bin/VisualBuilder
+cp ../DevTools/Inspector/Inspector mnt/bin/Inspector
 cp ../Games/Minesweeper/Minesweeper mnt/bin/Minesweeper
 cp ../Games/Snake/Snake mnt/bin/Snake
 cp ../Servers/LookupServer/LookupServer mnt/bin/LookupServer
@@ -121,6 +122,7 @@ ln -s Piano mnt/bin/pi
 ln -s SystemDialog mnt/bin/sd
 ln -s ChanViewer mnt/bin/cv
 ln -s Calculator mnt/bin/calc
+ln -s Inspector mnt/bin/ins
 echo "done"
 
 # Run local sync script, if it exists

+ 1 - 0
Kernel/makeall.sh

@@ -62,6 +62,7 @@ build_targets="$build_targets ../Demos/RetroFetch"
 build_targets="$build_targets ../Demos/WidgetGallery"
 
 build_targets="$build_targets ../DevTools/VisualBuilder"
+build_targets="$build_targets ../DevTools/Inspector"
 
 build_targets="$build_targets ../Games/Minesweeper"
 build_targets="$build_targets ../Games/Snake"