RemoteProcess.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "RemoteProcess.h"
  27. #include "RemoteObject.h"
  28. #include "RemoteObjectGraphModel.h"
  29. #include "RemoteObjectPropertyModel.h"
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. namespace Inspector {
  33. RemoteProcess* s_the;
  34. RemoteProcess& RemoteProcess::the()
  35. {
  36. return *s_the;
  37. }
  38. RemoteProcess::RemoteProcess(pid_t pid)
  39. : m_pid(pid)
  40. , m_object_graph_model(RemoteObjectGraphModel::create(*this))
  41. , m_socket(Core::LocalSocket::construct())
  42. {
  43. s_the = this;
  44. m_socket->set_blocking(true);
  45. }
  46. void RemoteProcess::handle_identify_response(const JsonObject& response)
  47. {
  48. int pid = response.get("pid").to_int();
  49. ASSERT(pid == m_pid);
  50. m_process_name = response.get("process_name").as_string_or({});
  51. if (on_update)
  52. on_update();
  53. }
  54. void RemoteProcess::handle_get_all_objects_response(const JsonObject& response)
  55. {
  56. // FIXME: It would be good if we didn't have to make a local copy of the array value here!
  57. auto objects = response.get("objects");
  58. auto& object_array = objects.as_array();
  59. NonnullOwnPtrVector<RemoteObject> remote_objects;
  60. HashMap<FlatPtr, RemoteObject*> objects_by_address;
  61. for (auto& value : object_array.values()) {
  62. ASSERT(value.is_object());
  63. auto& object = value.as_object();
  64. auto remote_object = make<RemoteObject>();
  65. remote_object->address = object.get("address").to_number<FlatPtr>();
  66. remote_object->parent_address = object.get("parent").to_number<FlatPtr>();
  67. remote_object->name = object.get("name").to_string();
  68. remote_object->class_name = object.get("class_name").to_string();
  69. remote_object->json = object;
  70. objects_by_address.set(remote_object->address, remote_object);
  71. remote_objects.append(move(remote_object));
  72. }
  73. for (size_t i = 0; i < remote_objects.size(); ++i) {
  74. auto& remote_object = remote_objects.ptr_at(i);
  75. auto* parent = objects_by_address.get(remote_object->parent_address).value_or(nullptr);
  76. if (!parent) {
  77. m_roots.append(move(remote_object));
  78. } else {
  79. remote_object->parent = parent;
  80. parent->children.append(move(remote_object));
  81. }
  82. }
  83. m_object_graph_model->update();
  84. if (on_update)
  85. on_update();
  86. }
  87. void RemoteProcess::send_request(const JsonObject& request)
  88. {
  89. auto serialized = request.to_string();
  90. i32 length = serialized.length();
  91. m_socket->write((const u8*)&length, sizeof(length));
  92. m_socket->write(serialized);
  93. }
  94. void RemoteProcess::set_inspected_object(FlatPtr address)
  95. {
  96. JsonObject request;
  97. request.set("type", "SetInspectedObject");
  98. request.set("address", address);
  99. send_request(request);
  100. }
  101. void RemoteProcess::set_property(FlatPtr object, const StringView& name, const JsonValue& value)
  102. {
  103. JsonObject request;
  104. request.set("type", "SetProperty");
  105. request.set("address", object);
  106. request.set("name", JsonValue(name));
  107. request.set("value", value);
  108. send_request(request);
  109. }
  110. void RemoteProcess::update()
  111. {
  112. m_socket->on_connected = [this] {
  113. dbgln("Connected to PID {}", m_pid);
  114. {
  115. JsonObject request;
  116. request.set("type", "Identify");
  117. send_request(request);
  118. }
  119. {
  120. JsonObject request;
  121. request.set("type", "GetAllObjects");
  122. send_request(request);
  123. }
  124. };
  125. m_socket->on_ready_to_read = [this] {
  126. if (m_socket->eof()) {
  127. dbgln("Disconnected from PID {}", m_pid);
  128. m_socket->close();
  129. return;
  130. }
  131. u32 length;
  132. int nread = m_socket->read((u8*)&length, sizeof(length));
  133. ASSERT(nread == sizeof(length));
  134. ByteBuffer data;
  135. size_t remaining_bytes = length;
  136. while (remaining_bytes) {
  137. auto packet = m_socket->read(remaining_bytes);
  138. if (packet.size() == 0)
  139. break;
  140. data.append(packet.data(), packet.size());
  141. remaining_bytes -= packet.size();
  142. }
  143. ASSERT(data.size() == length);
  144. dbgln("Got data size {} and read that many bytes", length);
  145. auto json_value = JsonValue::from_string(data);
  146. ASSERT(json_value.has_value());
  147. ASSERT(json_value.value().is_object());
  148. dbgln("Got JSON response {}", json_value.value());
  149. auto& response = json_value.value().as_object();
  150. auto response_type = response.get("type").as_string_or({});
  151. if (response_type.is_null())
  152. return;
  153. if (response_type == "GetAllObjects") {
  154. handle_get_all_objects_response(response);
  155. return;
  156. }
  157. if (response_type == "Identify") {
  158. handle_identify_response(response);
  159. return;
  160. }
  161. };
  162. auto success = m_socket->connect(Core::SocketAddress::local(String::formatted("/tmp/rpc/{}", m_pid)));
  163. if (!success) {
  164. warnln("Couldn't connect to PID {}", m_pid);
  165. exit(1);
  166. }
  167. }
  168. }