RemoteProcess.cpp 5.3 KB

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