| 1 | /**************************************************************************/ |
| 2 | /* remote_debugger.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 | |
| 31 | #ifndef REMOTE_DEBUGGER_H |
| 32 | #define REMOTE_DEBUGGER_H |
| 33 | |
| 34 | #include "core/debugger/debugger_marshalls.h" |
| 35 | #include "core/debugger/engine_debugger.h" |
| 36 | #include "core/debugger/remote_debugger_peer.h" |
| 37 | #include "core/object/class_db.h" |
| 38 | #include "core/string/string_name.h" |
| 39 | #include "core/string/ustring.h" |
| 40 | #include "core/variant/array.h" |
| 41 | |
| 42 | class RemoteDebugger : public EngineDebugger { |
| 43 | public: |
| 44 | enum MessageType { |
| 45 | MESSAGE_TYPE_LOG, |
| 46 | MESSAGE_TYPE_ERROR, |
| 47 | MESSAGE_TYPE_LOG_RICH, |
| 48 | }; |
| 49 | |
| 50 | private: |
| 51 | typedef DebuggerMarshalls::OutputError ErrorMessage; |
| 52 | |
| 53 | class PerformanceProfiler; |
| 54 | |
| 55 | Ref<PerformanceProfiler> performance_profiler; |
| 56 | |
| 57 | Ref<RemoteDebuggerPeer> peer; |
| 58 | |
| 59 | struct OutputString { |
| 60 | String message; |
| 61 | MessageType type; |
| 62 | }; |
| 63 | List<OutputString> output_strings; |
| 64 | List<ErrorMessage> errors; |
| 65 | |
| 66 | int n_messages_dropped = 0; |
| 67 | int max_errors_per_second = 0; |
| 68 | int max_chars_per_second = 0; |
| 69 | int max_warnings_per_second = 0; |
| 70 | int n_errors_dropped = 0; |
| 71 | int n_warnings_dropped = 0; |
| 72 | int char_count = 0; |
| 73 | int err_count = 0; |
| 74 | int warn_count = 0; |
| 75 | int last_reset = 0; |
| 76 | bool reload_all_scripts = false; |
| 77 | |
| 78 | // Make handlers and send_message thread safe. |
| 79 | Mutex mutex; |
| 80 | bool flushing = false; |
| 81 | Thread::ID flush_thread = 0; |
| 82 | |
| 83 | struct Message { |
| 84 | String message; |
| 85 | Array data; |
| 86 | }; |
| 87 | |
| 88 | HashMap<Thread::ID, List<Message>> messages; |
| 89 | |
| 90 | void _poll_messages(); |
| 91 | bool _has_messages(); |
| 92 | Array _get_message(); |
| 93 | |
| 94 | PrintHandlerList phl; |
| 95 | static void _print_handler(void *p_this, const String &p_string, bool p_error, bool p_rich); |
| 96 | ErrorHandlerList eh; |
| 97 | static void _err_handler(void *p_this, const char *p_func, const char *p_file, int p_line, const char *p_err, const char *p_descr, bool p_editor_notify, ErrorHandlerType p_type); |
| 98 | |
| 99 | ErrorMessage _create_overflow_error(const String &p_what, const String &p_descr); |
| 100 | Error _put_msg(String p_message, Array p_data); |
| 101 | |
| 102 | bool is_peer_connected() { return peer->is_peer_connected(); } |
| 103 | void flush_output(); |
| 104 | |
| 105 | void _send_stack_vars(List<String> &p_names, List<Variant> &p_vals, int p_type); |
| 106 | |
| 107 | Error _profiler_capture(const String &p_cmd, const Array &p_data, bool &r_captured); |
| 108 | Error _core_capture(const String &p_cmd, const Array &p_data, bool &r_captured); |
| 109 | |
| 110 | template <typename T> |
| 111 | void _bind_profiler(const String &p_name, T *p_prof); |
| 112 | Error _try_capture(const String &p_name, const Array &p_data, bool &r_captured); |
| 113 | |
| 114 | public: |
| 115 | // Overrides |
| 116 | void poll_events(bool p_is_idle); |
| 117 | void send_message(const String &p_message, const Array &p_args); |
| 118 | void send_error(const String &p_func, const String &p_file, int p_line, const String &p_err, const String &p_descr, bool p_editor_notify, ErrorHandlerType p_type); |
| 119 | void debug(bool p_can_continue = true, bool p_is_error_breakpoint = false); |
| 120 | |
| 121 | explicit RemoteDebugger(Ref<RemoteDebuggerPeer> p_peer); |
| 122 | ~RemoteDebugger(); |
| 123 | }; |
| 124 | |
| 125 | #endif // REMOTE_DEBUGGER_H |
| 126 | |