summaryrefslogtreecommitdiff
path: root/Userland/DevTools/Inspector/RemoteProcess.cpp
blob: c2670d09dccf837499b40e307a88ecba7f48f46d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "RemoteProcess.h"
#include "RemoteObject.h"
#include "RemoteObjectGraphModel.h"
#include "RemoteObjectPropertyModel.h"
#include <stdio.h>
#include <stdlib.h>

namespace Inspector {

RemoteProcess* s_the;

RemoteProcess& RemoteProcess::the()
{
    return *s_the;
}

RemoteProcess::RemoteProcess(pid_t pid)
    : m_pid(pid)
    , m_object_graph_model(RemoteObjectGraphModel::create(*this))
    , m_socket(Core::LocalSocket::construct())
{
    s_the = this;
    m_socket->set_blocking(true);
}

void RemoteProcess::handle_identify_response(const JsonObject& response)
{
    int pid = response.get("pid").to_int();
    VERIFY(pid == m_pid);

    m_process_name = response.get("process_name").as_string_or({});

    if (on_update)
        on_update();
}

void RemoteProcess::handle_get_all_objects_response(const JsonObject& response)
{
    // FIXME: It would be good if we didn't have to make a local copy of the array value here!
    auto objects = response.get("objects");
    auto& object_array = objects.as_array();

    NonnullOwnPtrVector<RemoteObject> remote_objects;
    HashMap<FlatPtr, RemoteObject*> objects_by_address;

    for (auto& value : object_array.values()) {
        VERIFY(value.is_object());
        auto& object = value.as_object();
        auto remote_object = make<RemoteObject>();
        remote_object->address = object.get("address").to_number<FlatPtr>();
        remote_object->parent_address = object.get("parent").to_number<FlatPtr>();
        remote_object->name = object.get("name").to_string();
        remote_object->class_name = object.get("class_name").to_string();
        remote_object->json = object;
        objects_by_address.set(remote_object->address, remote_object);
        remote_objects.append(move(remote_object));
    }

    for (size_t i = 0; i < remote_objects.size(); ++i) {
        auto& remote_object = remote_objects.ptr_at(i);
        auto* parent = objects_by_address.get(remote_object->parent_address).value_or(nullptr);
        if (!parent) {
            m_roots.append(move(remote_object));
        } else {
            remote_object->parent = parent;
            parent->children.append(move(remote_object));
        }
    }

    m_object_graph_model->update();

    if (on_update)
        on_update();
}

void RemoteProcess::send_request(const JsonObject& request)
{
    auto serialized = request.to_string();
    i32 length = serialized.length();
    m_socket->write((const u8*)&length, sizeof(length));
    m_socket->write(serialized);
}

void RemoteProcess::set_inspected_object(FlatPtr address)
{
    JsonObject request;
    request.set("type", "SetInspectedObject");
    request.set("address", address);
    send_request(request);
}

void RemoteProcess::set_property(FlatPtr object, const StringView& name, const JsonValue& value)
{
    JsonObject request;
    request.set("type", "SetProperty");
    request.set("address", object);
    request.set("name", JsonValue(name));
    request.set("value", value);
    send_request(request);
}

void RemoteProcess::update()
{
    m_socket->on_connected = [this] {
        dbgln("Connected to PID {}", m_pid);

        {
            JsonObject request;
            request.set("type", "Identify");
            send_request(request);
        }

        {
            JsonObject request;
            request.set("type", "GetAllObjects");
            send_request(request);
        }
    };

    m_socket->on_ready_to_read = [this] {
        if (m_socket->eof()) {
            dbgln("Disconnected from PID {}", m_pid);
            m_socket->close();
            return;
        }

        u32 length;
        int nread = m_socket->read((u8*)&length, sizeof(length));
        if (nread != sizeof(length)) {
            dbgln("Disconnected from PID {}", m_pid);
            m_socket->close();
            return;
        }

        ByteBuffer data;
        size_t remaining_bytes = length;

        while (remaining_bytes) {
            auto packet = m_socket->read(remaining_bytes);
            if (packet.size() == 0)
                break;
            data.append(packet.data(), packet.size());
            remaining_bytes -= packet.size();
        }

        VERIFY(data.size() == length);
        dbgln("Got data size {} and read that many bytes", length);

        auto json_value = JsonValue::from_string(data);
        VERIFY(json_value.has_value());
        VERIFY(json_value.value().is_object());

        dbgln("Got JSON response {}", json_value.value());

        auto& response = json_value.value().as_object();

        auto response_type = response.get("type").as_string_or({});
        if (response_type.is_null())
            return;

        if (response_type == "GetAllObjects") {
            handle_get_all_objects_response(response);
            return;
        }

        if (response_type == "Identify") {
            handle_identify_response(response);
            return;
        }
    };

    auto success = m_socket->connect(Core::SocketAddress::local(String::formatted("/tmp/rpc/{}", m_pid)));
    if (!success) {
        warnln("Couldn't connect to PID {}", m_pid);
        exit(1);
    }
}

}