debug_adapter_protocol.h 5.7 KB

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