Debugger.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include "BreakpointCallback.h"
  28. #include <AK/Function.h>
  29. #include <AK/Vector.h>
  30. #include <LibDebug/DebugSession.h>
  31. #include <LibThread/Lock.h>
  32. #include <LibThread/Thread.h>
  33. namespace HackStudio {
  34. class Debugger {
  35. public:
  36. static Debugger& the();
  37. enum class HasControlPassedToUser {
  38. No,
  39. Yes,
  40. };
  41. static void initialize(
  42. Function<HasControlPassedToUser(const PtraceRegisters&)> on_stop_callback,
  43. Function<void()> on_continue_callback,
  44. Function<void()> on_exit_callback);
  45. static bool is_initialized();
  46. static void on_breakpoint_change(const String& file, size_t line, BreakpointChange change_type);
  47. void set_executable_path(const String& path) { m_executable_path = path; }
  48. Debug::DebugSession* session() { return m_debug_session.ptr(); }
  49. // Thread entry point
  50. static int start_static();
  51. pthread_mutex_t* continue_mutex() { return &m_continue_mutex; }
  52. pthread_cond_t* continue_cond() { return &m_continue_cond; }
  53. enum class ContinueType {
  54. Continue,
  55. SourceSingleStep,
  56. SourceStepOut,
  57. SourceStepOver,
  58. };
  59. void set_continue_type(ContinueType type) { m_continue_type = type; }
  60. void reset_breakpoints() { m_breakpoints.clear(); }
  61. private:
  62. class DebuggingState {
  63. public:
  64. enum State {
  65. Normal, // Continue normally until we hit a breakpoint / program terminates
  66. SingleStepping,
  67. SteppingOut,
  68. SteppingOver,
  69. };
  70. State get() const { return m_state; }
  71. void set_normal();
  72. void set_single_stepping(Debug::DebugInfo::SourcePosition original_source_position);
  73. void set_stepping_out() { m_state = State::SteppingOut; }
  74. void set_stepping_over() { m_state = State::SteppingOver; }
  75. bool should_stop_single_stepping(const Debug::DebugInfo::SourcePosition& current_source_position) const;
  76. void clear_temporary_breakpoints();
  77. void add_temporary_breakpoint(u32 address);
  78. const Vector<u32>& temporary_breakpoints() const { return m_addresses_of_temporary_breakpoints; }
  79. private:
  80. State m_state { Normal };
  81. Optional<Debug::DebugInfo::SourcePosition> m_original_source_position; // The source position at which we started the current single step
  82. Vector<u32> m_addresses_of_temporary_breakpoints;
  83. };
  84. explicit Debugger(
  85. Function<HasControlPassedToUser(const PtraceRegisters&)> on_stop_callback,
  86. Function<void()> on_continue_callback,
  87. Function<void()> on_exit_callback);
  88. static Debug::DebugInfo::SourcePosition create_source_position(const String& file, size_t line);
  89. void start();
  90. int debugger_loop();
  91. void remove_temporary_breakpoints();
  92. void do_step_out(const PtraceRegisters&);
  93. void do_step_over(const PtraceRegisters&);
  94. void insert_temporary_breakpoint(FlatPtr address);
  95. void insert_temporary_breakpoint_at_return_address(const PtraceRegisters&);
  96. OwnPtr<Debug::DebugSession> m_debug_session;
  97. DebuggingState m_state;
  98. pthread_mutex_t m_continue_mutex {};
  99. pthread_cond_t m_continue_cond {};
  100. Vector<Debug::DebugInfo::SourcePosition> m_breakpoints;
  101. String m_executable_path;
  102. Function<HasControlPassedToUser(const PtraceRegisters&)> m_on_stopped_callback;
  103. Function<void()> m_on_continue_callback;
  104. Function<void()> m_on_exit_callback;
  105. ContinueType m_continue_type { ContinueType::Continue };
  106. };
  107. }