Debugger.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include "BreakpointCallback.h"
  8. #include <AK/Function.h>
  9. #include <AK/LexicalPath.h>
  10. #include <AK/Vector.h>
  11. #include <LibDebug/DebugSession.h>
  12. #include <LibThreading/Mutex.h>
  13. #include <LibThreading/Thread.h>
  14. namespace HackStudio {
  15. class Debugger {
  16. public:
  17. static Debugger& the();
  18. enum class HasControlPassedToUser {
  19. No,
  20. Yes,
  21. };
  22. static void initialize(
  23. DeprecatedString source_root,
  24. Function<HasControlPassedToUser(PtraceRegisters const&)> on_stop_callback,
  25. Function<void()> on_continue_callback,
  26. Function<void()> on_exit_callback);
  27. static bool is_initialized();
  28. void on_breakpoint_change(DeprecatedString const& file, size_t line, BreakpointChange change_type);
  29. bool set_execution_position(DeprecatedString const& file, size_t line);
  30. void set_executable_path(DeprecatedString const& path) { m_executable_path = path; }
  31. void set_source_root(DeprecatedString const& source_root) { m_source_root = source_root; }
  32. void set_pid_to_attach(pid_t pid) { m_pid_to_attach = pid; }
  33. Debug::DebugSession* session() { return m_debug_session.ptr(); }
  34. void stop();
  35. // Thread entry point
  36. static intptr_t start_static();
  37. pthread_mutex_t* continue_mutex() { return &m_ui_action_mutex; }
  38. pthread_cond_t* continue_cond() { return &m_ui_action_cond; }
  39. enum class DebuggerAction {
  40. Continue,
  41. SourceSingleStep,
  42. SourceStepOut,
  43. SourceStepOver,
  44. Exit,
  45. };
  46. void set_requested_debugger_action(DebuggerAction);
  47. void reset_breakpoints() { m_breakpoints.clear(); }
  48. void set_child_setup_callback(Function<ErrorOr<void>()> callback) { m_child_setup_callback = move(callback); }
  49. private:
  50. class DebuggingState {
  51. public:
  52. enum State {
  53. Normal, // Continue normally until we hit a breakpoint / program terminates
  54. SingleStepping,
  55. SteppingOut,
  56. SteppingOver,
  57. };
  58. State get() const { return m_state; }
  59. void set_normal();
  60. void set_single_stepping(Debug::DebugInfo::SourcePosition original_source_position);
  61. void set_stepping_out() { m_state = State::SteppingOut; }
  62. void set_stepping_over() { m_state = State::SteppingOver; }
  63. bool should_stop_single_stepping(Debug::DebugInfo::SourcePosition const& current_source_position) const;
  64. void clear_temporary_breakpoints();
  65. void add_temporary_breakpoint(FlatPtr address);
  66. Vector<FlatPtr> const& temporary_breakpoints() const { return m_addresses_of_temporary_breakpoints; }
  67. private:
  68. State m_state { Normal };
  69. Optional<Debug::DebugInfo::SourcePosition> m_original_source_position; // The source position at which we started the current single step
  70. Vector<FlatPtr> m_addresses_of_temporary_breakpoints;
  71. };
  72. explicit Debugger(
  73. DeprecatedString source_root,
  74. Function<HasControlPassedToUser(PtraceRegisters const&)> on_stop_callback,
  75. Function<void()> on_continue_callback,
  76. Function<void()> on_exit_callback);
  77. Debug::DebugInfo::SourcePosition create_source_position(DeprecatedString const& file, size_t line);
  78. void start();
  79. int debugger_loop(Debug::DebugSession::DesiredInitialDebugeeState);
  80. void remove_temporary_breakpoints();
  81. void do_step_out(PtraceRegisters const&);
  82. void do_step_over(PtraceRegisters const&);
  83. void insert_temporary_breakpoint(FlatPtr address);
  84. void insert_temporary_breakpoint_at_return_address(PtraceRegisters const&);
  85. struct CreateDebugSessionResult {
  86. NonnullOwnPtr<Debug::DebugSession> session;
  87. Debug::DebugSession::DesiredInitialDebugeeState initial_state { Debug::DebugSession::Stopped };
  88. };
  89. CreateDebugSessionResult create_debug_session();
  90. OwnPtr<Debug::DebugSession> m_debug_session;
  91. DeprecatedString m_source_root;
  92. DebuggingState m_state;
  93. pthread_mutex_t m_ui_action_mutex {};
  94. pthread_cond_t m_ui_action_cond {};
  95. DebuggerAction m_requested_debugger_action { DebuggerAction::Continue };
  96. Vector<Debug::DebugInfo::SourcePosition> m_breakpoints;
  97. DeprecatedString m_executable_path;
  98. Optional<pid_t> m_pid_to_attach;
  99. Function<HasControlPassedToUser(PtraceRegisters const&)> m_on_stopped_callback;
  100. Function<void()> m_on_continue_callback;
  101. Function<void()> m_on_exit_callback;
  102. Function<ErrorOr<void>()> m_child_setup_callback;
  103. };
  104. }