debug_adapter_protocol.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**************************************************************************/
  2. /* debug_adapter_protocol.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #ifndef DEBUG_ADAPTER_PROTOCOL_H
  31. #define DEBUG_ADAPTER_PROTOCOL_H
  32. #include "core/debugger/debugger_marshalls.h"
  33. #include "core/io/stream_peer_tcp.h"
  34. #include "core/io/tcp_server.h"
  35. #include "debug_adapter_parser.h"
  36. #include "debug_adapter_types.h"
  37. #include "scene/debugger/scene_debugger.h"
  38. #define DAP_MAX_BUFFER_SIZE 4194304 // 4MB
  39. #define DAP_MAX_CLIENTS 8
  40. class DebugAdapterParser;
  41. struct DAPeer : RefCounted {
  42. Ref<StreamPeerTCP> connection;
  43. uint8_t req_buf[DAP_MAX_BUFFER_SIZE];
  44. int req_pos = 0;
  45. bool has_header = false;
  46. int content_length = 0;
  47. List<Dictionary> res_queue;
  48. int seq = 0;
  49. uint64_t timestamp = 0;
  50. // Client specific info
  51. bool linesStartAt1 = false;
  52. bool columnsStartAt1 = false;
  53. bool supportsVariableType = false;
  54. bool supportsInvalidatedEvent = false;
  55. bool supportsCustomData = false;
  56. // Internal client info
  57. bool attached = false;
  58. Dictionary pending_launch;
  59. Error handle_data();
  60. Error send_data();
  61. String format_output(const Dictionary &p_params) const;
  62. };
  63. class DebugAdapterProtocol : public Object {
  64. GDCLASS(DebugAdapterProtocol, Object)
  65. friend class DebugAdapterParser;
  66. using DAPVarID = int;
  67. private:
  68. static DebugAdapterProtocol *singleton;
  69. DebugAdapterParser *parser = nullptr;
  70. List<Ref<DAPeer>> clients;
  71. Ref<TCPServer> server;
  72. Error on_client_connected();
  73. void on_client_disconnected(const Ref<DAPeer> &p_peer);
  74. void on_debug_paused();
  75. void on_debug_stopped();
  76. void on_debug_output(const String &p_message, int p_type);
  77. void on_debug_breaked(const bool &p_reallydid, const bool &p_can_debug, const String &p_reason, const bool &p_has_stackdump);
  78. void on_debug_breakpoint_toggled(const String &p_path, const int &p_line, const bool &p_enabled);
  79. void on_debug_stack_dump(const Array &p_stack_dump);
  80. void on_debug_stack_frame_vars(const int &p_size);
  81. void on_debug_stack_frame_var(const Array &p_data);
  82. void on_debug_data(const String &p_msg, const Array &p_data);
  83. void reset_current_info();
  84. void reset_ids();
  85. void reset_stack_info();
  86. int parse_variant(const Variant &p_var);
  87. void parse_object(SceneDebuggerObject &p_obj);
  88. const Variant parse_object_variable(const SceneDebuggerObject::SceneDebuggerProperty &p_property);
  89. void parse_evaluation(DebuggerMarshalls::ScriptStackVariable &p_var);
  90. ObjectID search_object_id(DAPVarID p_var_id);
  91. bool request_remote_object(const ObjectID &p_object_id);
  92. bool request_remote_evaluate(const String &p_eval, int p_stack_frame);
  93. bool _initialized = false;
  94. bool _processing_breakpoint = false;
  95. bool _stepping = false;
  96. bool _processing_stackdump = false;
  97. int _remaining_vars = 0;
  98. int _current_frame = 0;
  99. uint64_t _request_timeout = 5000;
  100. bool _sync_breakpoints = false;
  101. String _current_request;
  102. Ref<DAPeer> _current_peer;
  103. int breakpoint_id = 0;
  104. int stackframe_id = 0;
  105. DAPVarID variable_id = 0;
  106. List<DAP::Breakpoint> breakpoint_list;
  107. HashMap<DAP::StackFrame, List<int>, DAP::StackFrame> stackframe_list;
  108. HashMap<DAPVarID, Array> variable_list;
  109. HashMap<ObjectID, DAPVarID> object_list;
  110. HashSet<ObjectID> object_pending_set;
  111. HashMap<String, DAP::Variable> eval_list;
  112. HashSet<String> eval_pending_list;
  113. public:
  114. friend class DebugAdapterServer;
  115. _FORCE_INLINE_ static DebugAdapterProtocol *get_singleton() { return singleton; }
  116. _FORCE_INLINE_ bool is_active() const { return _initialized && clients.size() > 0; }
  117. bool process_message(const String &p_text);
  118. String get_current_request() const { return _current_request; }
  119. Ref<DAPeer> get_current_peer() const { return _current_peer; }
  120. void notify_initialized();
  121. void notify_process();
  122. void notify_terminated();
  123. void notify_exited(const int &p_exitcode = 0);
  124. void notify_stopped_paused();
  125. void notify_stopped_exception(const String &p_error);
  126. void notify_stopped_breakpoint(const int &p_id);
  127. void notify_stopped_step();
  128. void notify_continued();
  129. void notify_output(const String &p_message, RemoteDebugger::MessageType p_type);
  130. void notify_custom_data(const String &p_msg, const Array &p_data);
  131. void notify_breakpoint(const DAP::Breakpoint &p_breakpoint, const bool &p_enabled);
  132. Array update_breakpoints(const String &p_path, const Array &p_lines);
  133. void poll();
  134. Error start(int p_port, const IPAddress &p_bind_ip);
  135. void stop();
  136. DebugAdapterProtocol();
  137. ~DebugAdapterProtocol();
  138. };
  139. #endif // DEBUG_ADAPTER_PROTOCOL_H