debugger_marshalls.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*************************************************************************/
  2. /* debugger_marshalls.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "debugger_marshalls.h"
  31. #include "core/io/marshalls.h"
  32. #define CHECK_SIZE(arr, expected, what) ERR_FAIL_COND_V_MSG((uint32_t)arr.size() < (uint32_t)(expected), false, String("Malformed ") + what + " message from script debugger, message too short. Expected size: " + itos(expected) + ", actual size: " + itos(arr.size()))
  33. #define CHECK_END(arr, expected, what) ERR_FAIL_COND_V_MSG((uint32_t)arr.size() > (uint32_t)expected, false, String("Malformed ") + what + " message from script debugger, message too long. Expected size: " + itos(expected) + ", actual size: " + itos(arr.size()))
  34. Array DebuggerMarshalls::ResourceUsage::serialize() {
  35. infos.sort();
  36. Array arr;
  37. arr.push_back(infos.size() * 4);
  38. for (List<ResourceInfo>::Element *E = infos.front(); E; E = E->next()) {
  39. arr.push_back(E->get().path);
  40. arr.push_back(E->get().format);
  41. arr.push_back(E->get().type);
  42. arr.push_back(E->get().vram);
  43. }
  44. return arr;
  45. }
  46. bool DebuggerMarshalls::ResourceUsage::deserialize(const Array &p_arr) {
  47. CHECK_SIZE(p_arr, 1, "ResourceUsage");
  48. uint32_t size = p_arr[0];
  49. CHECK_SIZE(p_arr, size, "ResourceUsage");
  50. int idx = 1;
  51. for (uint32_t i = 0; i < size / 4; i++) {
  52. ResourceInfo info;
  53. info.path = p_arr[idx];
  54. info.format = p_arr[idx + 1];
  55. info.type = p_arr[idx + 2];
  56. info.vram = p_arr[idx + 3];
  57. infos.push_back(info);
  58. }
  59. CHECK_END(p_arr, idx, "ResourceUsage");
  60. return true;
  61. }
  62. Array DebuggerMarshalls::ScriptFunctionSignature::serialize() {
  63. Array arr;
  64. arr.push_back(name);
  65. arr.push_back(id);
  66. return arr;
  67. }
  68. bool DebuggerMarshalls::ScriptFunctionSignature::deserialize(const Array &p_arr) {
  69. CHECK_SIZE(p_arr, 2, "ScriptFunctionSignature");
  70. name = p_arr[0];
  71. id = p_arr[1];
  72. CHECK_END(p_arr, 2, "ScriptFunctionSignature");
  73. return true;
  74. }
  75. Array DebuggerMarshalls::NetworkProfilerFrame::serialize() {
  76. Array arr;
  77. arr.push_back(infos.size() * 6);
  78. for (int i = 0; i < infos.size(); ++i) {
  79. arr.push_back(uint64_t(infos[i].node));
  80. arr.push_back(infos[i].node_path);
  81. arr.push_back(infos[i].incoming_rpc);
  82. arr.push_back(infos[i].incoming_rset);
  83. arr.push_back(infos[i].outgoing_rpc);
  84. arr.push_back(infos[i].outgoing_rset);
  85. }
  86. return arr;
  87. }
  88. bool DebuggerMarshalls::NetworkProfilerFrame::deserialize(const Array &p_arr) {
  89. CHECK_SIZE(p_arr, 1, "NetworkProfilerFrame");
  90. uint32_t size = p_arr[0];
  91. CHECK_SIZE(p_arr, size, "NetworkProfilerFrame");
  92. infos.resize(size);
  93. int idx = 1;
  94. for (uint32_t i = 0; i < size / 6; ++i) {
  95. infos.write[i].node = uint64_t(p_arr[idx]);
  96. infos.write[i].node_path = p_arr[idx + 1];
  97. infos.write[i].incoming_rpc = p_arr[idx + 2];
  98. infos.write[i].incoming_rset = p_arr[idx + 3];
  99. infos.write[i].outgoing_rpc = p_arr[idx + 4];
  100. infos.write[i].outgoing_rset = p_arr[idx + 5];
  101. }
  102. CHECK_END(p_arr, idx, "NetworkProfilerFrame");
  103. return true;
  104. }
  105. Array DebuggerMarshalls::ServersProfilerFrame::serialize() {
  106. Array arr;
  107. arr.push_back(frame_number);
  108. arr.push_back(frame_time);
  109. arr.push_back(idle_time);
  110. arr.push_back(physics_time);
  111. arr.push_back(physics_frame_time);
  112. arr.push_back(script_time);
  113. arr.push_back(servers.size());
  114. for (int i = 0; i < servers.size(); i++) {
  115. ServerInfo &s = servers[i];
  116. arr.push_back(s.name);
  117. arr.push_back(s.functions.size() * 2);
  118. for (int j = 0; j < s.functions.size(); j++) {
  119. ServerFunctionInfo &f = s.functions[j];
  120. arr.push_back(f.name);
  121. arr.push_back(f.time);
  122. }
  123. }
  124. arr.push_back(script_functions.size() * 4);
  125. for (int i = 0; i < script_functions.size(); i++) {
  126. arr.push_back(script_functions[i].sig_id);
  127. arr.push_back(script_functions[i].call_count);
  128. arr.push_back(script_functions[i].self_time);
  129. arr.push_back(script_functions[i].total_time);
  130. }
  131. return arr;
  132. }
  133. bool DebuggerMarshalls::ServersProfilerFrame::deserialize(const Array &p_arr) {
  134. CHECK_SIZE(p_arr, 7, "ServersProfilerFrame");
  135. frame_number = p_arr[0];
  136. frame_time = p_arr[1];
  137. idle_time = p_arr[2];
  138. physics_time = p_arr[3];
  139. physics_frame_time = p_arr[4];
  140. script_time = p_arr[5];
  141. int servers_size = p_arr[6];
  142. int idx = 7;
  143. while (servers_size) {
  144. CHECK_SIZE(p_arr, idx + 2, "ServersProfilerFrame");
  145. servers_size--;
  146. ServerInfo si;
  147. si.name = p_arr[idx];
  148. int sub_data_size = p_arr[idx + 1];
  149. idx += 2;
  150. CHECK_SIZE(p_arr, idx + sub_data_size, "ServersProfilerFrame");
  151. for (int j = 0; j < sub_data_size / 2; j++) {
  152. ServerFunctionInfo sf;
  153. sf.name = p_arr[idx];
  154. sf.time = p_arr[idx + 1];
  155. idx += 2;
  156. si.functions.push_back(sf);
  157. }
  158. servers.push_back(si);
  159. }
  160. CHECK_SIZE(p_arr, idx + 1, "ServersProfilerFrame");
  161. int func_size = p_arr[idx];
  162. idx += 1;
  163. CHECK_SIZE(p_arr, idx + func_size, "ServersProfilerFrame");
  164. for (int i = 0; i < func_size / 4; i++) {
  165. ScriptFunctionInfo fi;
  166. fi.sig_id = p_arr[idx];
  167. fi.call_count = p_arr[idx + 1];
  168. fi.self_time = p_arr[idx + 2];
  169. fi.total_time = p_arr[idx + 3];
  170. script_functions.push_back(fi);
  171. idx += 4;
  172. }
  173. CHECK_END(p_arr, idx, "ServersProfilerFrame");
  174. return true;
  175. }
  176. Array DebuggerMarshalls::ScriptStackDump::serialize() {
  177. Array arr;
  178. arr.push_back(frames.size() * 3);
  179. for (int i = 0; i < frames.size(); i++) {
  180. arr.push_back(frames[i].file);
  181. arr.push_back(frames[i].line);
  182. arr.push_back(frames[i].func);
  183. }
  184. return arr;
  185. }
  186. bool DebuggerMarshalls::ScriptStackDump::deserialize(const Array &p_arr) {
  187. CHECK_SIZE(p_arr, 1, "ScriptStackDump");
  188. uint32_t size = p_arr[0];
  189. CHECK_SIZE(p_arr, size, "ScriptStackDump");
  190. int idx = 1;
  191. for (uint32_t i = 0; i < size / 3; i++) {
  192. ScriptLanguage::StackInfo sf;
  193. sf.file = p_arr[idx];
  194. sf.line = p_arr[idx + 1];
  195. sf.func = p_arr[idx + 2];
  196. frames.push_back(sf);
  197. idx += 3;
  198. }
  199. CHECK_END(p_arr, idx, "ScriptStackDump");
  200. return true;
  201. }
  202. Array DebuggerMarshalls::ScriptStackVariable::serialize(int max_size) {
  203. Array arr;
  204. arr.push_back(name);
  205. arr.push_back(type);
  206. Variant var = value;
  207. if (value.get_type() == Variant::OBJECT && value.get_validated_object() == nullptr) {
  208. var = Variant();
  209. }
  210. int len = 0;
  211. Error err = encode_variant(var, nullptr, len, true);
  212. if (err != OK) {
  213. ERR_PRINT("Failed to encode variant.");
  214. }
  215. if (len > max_size) {
  216. arr.push_back(Variant());
  217. } else {
  218. arr.push_back(var);
  219. }
  220. return arr;
  221. }
  222. bool DebuggerMarshalls::ScriptStackVariable::deserialize(const Array &p_arr) {
  223. CHECK_SIZE(p_arr, 3, "ScriptStackVariable");
  224. name = p_arr[0];
  225. type = p_arr[1];
  226. value = p_arr[2];
  227. CHECK_END(p_arr, 3, "ScriptStackVariable");
  228. return true;
  229. }
  230. Array DebuggerMarshalls::OutputError::serialize() {
  231. Array arr;
  232. arr.push_back(hr);
  233. arr.push_back(min);
  234. arr.push_back(sec);
  235. arr.push_back(msec);
  236. arr.push_back(source_file);
  237. arr.push_back(source_func);
  238. arr.push_back(source_line);
  239. arr.push_back(error);
  240. arr.push_back(error_descr);
  241. arr.push_back(warning);
  242. unsigned int size = callstack.size();
  243. const ScriptLanguage::StackInfo *r = callstack.ptr();
  244. arr.push_back(size * 3);
  245. for (int i = 0; i < callstack.size(); i++) {
  246. arr.push_back(r[i].file);
  247. arr.push_back(r[i].func);
  248. arr.push_back(r[i].line);
  249. }
  250. return arr;
  251. }
  252. bool DebuggerMarshalls::OutputError::deserialize(const Array &p_arr) {
  253. CHECK_SIZE(p_arr, 11, "OutputError");
  254. hr = p_arr[0];
  255. min = p_arr[1];
  256. sec = p_arr[2];
  257. msec = p_arr[3];
  258. source_file = p_arr[4];
  259. source_func = p_arr[5];
  260. source_line = p_arr[6];
  261. error = p_arr[7];
  262. error_descr = p_arr[8];
  263. warning = p_arr[9];
  264. unsigned int stack_size = p_arr[10];
  265. CHECK_SIZE(p_arr, stack_size, "OutputError");
  266. int idx = 11;
  267. callstack.resize(stack_size / 3);
  268. ScriptLanguage::StackInfo *w = callstack.ptrw();
  269. for (unsigned int i = 0; i < stack_size / 3; i++) {
  270. w[i].file = p_arr[idx];
  271. w[i].func = p_arr[idx + 1];
  272. w[i].line = p_arr[idx + 2];
  273. idx += 3;
  274. }
  275. CHECK_END(p_arr, idx, "OutputError");
  276. return true;
  277. }
  278. Array DebuggerMarshalls::VisualProfilerFrame::serialize() {
  279. Array arr;
  280. arr.push_back(frame_number);
  281. arr.push_back(areas.size() * 3);
  282. for (int i = 0; i < areas.size(); i++) {
  283. arr.push_back(areas[i].name);
  284. arr.push_back(areas[i].cpu_msec);
  285. arr.push_back(areas[i].gpu_msec);
  286. }
  287. return arr;
  288. }
  289. bool DebuggerMarshalls::VisualProfilerFrame::deserialize(const Array &p_arr) {
  290. CHECK_SIZE(p_arr, 2, "VisualProfilerFrame");
  291. frame_number = p_arr[0];
  292. int size = p_arr[1];
  293. CHECK_SIZE(p_arr, size, "VisualProfilerFrame");
  294. int idx = 2;
  295. areas.resize(size / 3);
  296. RS::FrameProfileArea *w = areas.ptrw();
  297. for (int i = 0; i < size / 3; i++) {
  298. w[i].name = p_arr[idx];
  299. w[i].cpu_msec = p_arr[idx + 1];
  300. w[i].gpu_msec = p_arr[idx + 2];
  301. idx += 3;
  302. }
  303. CHECK_END(p_arr, idx, "VisualProfilerFrame");
  304. return true;
  305. }