RemoteProcess.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. RemoteProcess* s_the;
  33. RemoteProcess& RemoteProcess::the()
  34. {
  35. return *s_the;
  36. }
  37. RemoteProcess::RemoteProcess(pid_t pid)
  38. : m_pid(pid)
  39. , m_object_graph_model(RemoteObjectGraphModel::create(*this))
  40. , m_socket(Core::LocalSocket::construct())
  41. {
  42. s_the = this;
  43. }
  44. void RemoteProcess::handle_identify_response(const JsonObject& response)
  45. {
  46. int pid = response.get("pid").to_int();
  47. ASSERT(pid == m_pid);
  48. m_process_name = response.get("process_name").as_string_or({});
  49. if (on_update)
  50. on_update();
  51. }
  52. void RemoteProcess::handle_get_all_objects_response(const JsonObject& response)
  53. {
  54. // FIXME: It would be good if we didn't have to make a local copy of the array value here!
  55. auto objects = response.get("objects");
  56. auto& object_array = objects.as_array();
  57. NonnullOwnPtrVector<RemoteObject> remote_objects;
  58. HashMap<String, RemoteObject*> objects_by_address;
  59. for (auto& value : object_array.values()) {
  60. ASSERT(value.is_object());
  61. auto& object = value.as_object();
  62. auto remote_object = make<RemoteObject>();
  63. remote_object->address = object.get("address").to_string();
  64. remote_object->parent_address = object.get("parent").to_string();
  65. remote_object->name = object.get("name").to_string();
  66. remote_object->class_name = object.get("class_name").to_string();
  67. remote_object->json = object;
  68. objects_by_address.set(remote_object->address, remote_object);
  69. remote_objects.append(move(remote_object));
  70. }
  71. for (size_t i = 0; i < remote_objects.size(); ++i) {
  72. auto& remote_object = remote_objects.ptr_at(i);
  73. auto* parent = objects_by_address.get(remote_object->parent_address).value_or(nullptr);
  74. if (!parent) {
  75. m_roots.append(move(remote_object));
  76. } else {
  77. remote_object->parent = parent;
  78. parent->children.append(move(remote_object));
  79. }
  80. }
  81. m_object_graph_model->update();
  82. if (on_update)
  83. on_update();
  84. }
  85. void RemoteProcess::send_request(const JsonObject& request)
  86. {
  87. auto serialized = request.to_string();
  88. i32 length = serialized.length();
  89. m_socket->write((const u8*)&length, sizeof(length));
  90. m_socket->write(serialized);
  91. }
  92. void RemoteProcess::set_inspected_object(uintptr_t address)
  93. {
  94. JsonObject request;
  95. request.set("type", "SetInspectedObject");
  96. request.set("address", address);
  97. send_request(request);
  98. }
  99. void RemoteProcess::set_property(uintptr_t object, const StringView& name, const JsonValue& value)
  100. {
  101. JsonObject request;
  102. request.set("type", "SetProperty");
  103. request.set("address", object);
  104. request.set("name", JsonValue(name));
  105. request.set("value", value);
  106. send_request(request);
  107. }
  108. void RemoteProcess::update()
  109. {
  110. m_socket->on_connected = [this] {
  111. dbg() << "Connected to PID " << m_pid;
  112. {
  113. JsonObject request;
  114. request.set("type", "Identify");
  115. send_request(request);
  116. }
  117. {
  118. JsonObject request;
  119. request.set("type", "GetAllObjects");
  120. send_request(request);
  121. }
  122. };
  123. m_socket->on_ready_to_read = [this] {
  124. if (m_socket->eof()) {
  125. dbg() << "Disconnected from PID " << m_pid;
  126. m_socket->close();
  127. return;
  128. }
  129. u32 length;
  130. int nread = m_socket->read((u8*)&length, sizeof(length));
  131. ASSERT(nread == sizeof(length));
  132. auto data = m_socket->read(length);
  133. ASSERT(data.size() == length);
  134. dbg() << "Got packet size " << length << " and read that many bytes";
  135. auto json_value = JsonValue::from_string(data);
  136. ASSERT(json_value.is_object());
  137. dbg() << "Got JSON response " << json_value.to_string();
  138. auto& response = json_value.as_object();
  139. auto response_type = response.get("type").as_string_or({});
  140. if (response_type.is_null())
  141. return;
  142. if (response_type == "GetAllObjects") {
  143. handle_get_all_objects_response(response);
  144. return;
  145. }
  146. if (response_type == "Identify") {
  147. handle_identify_response(response);
  148. return;
  149. }
  150. };
  151. auto success = m_socket->connect(Core::SocketAddress::local(String::format("/tmp/rpc.%d", m_pid)));
  152. if (!success) {
  153. fprintf(stderr, "Couldn't connect to PID %d\n", m_pid);
  154. exit(1);
  155. }
  156. }