Commit 2981ecee authored by Jonas Devlieghere's avatar Jonas Devlieghere
Browse files

[debugserver] Share code between Enable/DisableHardwareWatchpoint (NFC)

This extract the common functionality of enabling and disabling hardware
watchpoints into a single function.

Differential revision: https://reviews.llvm.org/D72971
parent 80146fc1
Loading
Loading
Loading
Loading
+42 −51
Original line number Diff line number Diff line
@@ -502,23 +502,29 @@ bool MachThreadList::DisableHardwareBreakpoint(const DNBBreakpoint *bp) const {
  return false;
}

// DNBWatchpointSet() -> MachProcess::CreateWatchpoint() ->
// MachProcess::EnableWatchpoint()
// -> MachThreadList::EnableHardwareWatchpoint().
uint32_t
MachThreadList::EnableHardwareWatchpoint(const DNBBreakpoint *wp) const {
uint32_t MachThreadList::DoHardwareBreakpointAction(
    const DNBBreakpoint *wp, HardwareBreakpointAction action) const {
  if (wp == NULL)
    return INVALID_NUB_HW_INDEX;

  uint32_t hw_index = INVALID_NUB_HW_INDEX;
  if (wp != NULL) {
  PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
  const size_t num_threads = m_threads.size();
    // On Mac OS X we have to prime the control registers for new threads.  We
    // do this
    // using the control register data for the first thread, for lack of a
  // On Mac OS X we have to prime the control registers for new threads.  We do
  // this using the control register data for the first thread, for lack of a
  // better way of choosing.
  bool also_set_on_task = true;
  for (uint32_t idx = 0; idx < num_threads; ++idx) {
      if ((hw_index = m_threads[idx]->EnableHardwareWatchpoint(
               wp, also_set_on_task)) == INVALID_NUB_HW_INDEX) {
    switch (action) {
    case HardwareBreakpointAction::EnableWatchpoint:
      hw_index = m_threads[idx]->EnableHardwareWatchpoint(wp, also_set_on_task);
      break;
    case HardwareBreakpointAction::DisableWatchpoint:
      hw_index =
          m_threads[idx]->DisableHardwareWatchpoint(wp, also_set_on_task);
      break;
    }
    if (hw_index == INVALID_NUB_HW_INDEX) {
      // We know that idx failed for some reason.  Let's rollback the
      // transaction for [0, idx).
      for (uint32_t i = 0; i < idx; ++i)
@@ -530,37 +536,22 @@ MachThreadList::EnableHardwareWatchpoint(const DNBBreakpoint *wp) const {
  // Notify each thread to commit the pending transaction.
  for (uint32_t idx = 0; idx < num_threads; ++idx)
    m_threads[idx]->FinishTransForHWP();
  }
  return hw_index;
}

bool MachThreadList::DisableHardwareWatchpoint(const DNBBreakpoint *wp) const {
  if (wp != NULL) {
    PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
    const size_t num_threads = m_threads.size();

    // On Mac OS X we have to prime the control registers for new threads.  We
    // do this
    // using the control register data for the first thread, for lack of a
    // better way of choosing.
    bool also_set_on_task = true;
    for (uint32_t idx = 0; idx < num_threads; ++idx) {
      if (!m_threads[idx]->DisableHardwareWatchpoint(wp, also_set_on_task)) {
        // We know that idx failed for some reason.  Let's rollback the
        // transaction for [0, idx).
        for (uint32_t i = 0; i < idx; ++i)
          m_threads[i]->RollbackTransForHWP();
        return false;
      }
      also_set_on_task = false;
// DNBWatchpointSet() -> MachProcess::CreateWatchpoint() ->
// MachProcess::EnableWatchpoint()
// -> MachThreadList::EnableHardwareWatchpoint().
uint32_t
MachThreadList::EnableHardwareWatchpoint(const DNBBreakpoint *wp) const {
  return DoHardwareBreakpointAction(wp,
                                    HardwareBreakpointAction::EnableWatchpoint);
}
    // Notify each thread to commit the pending transaction.
    for (uint32_t idx = 0; idx < num_threads; ++idx)
      m_threads[idx]->FinishTransForHWP();

    return true;
  }
  return false;
bool MachThreadList::DisableHardwareWatchpoint(const DNBBreakpoint *wp) const {
  return DoHardwareBreakpointAction(
             wp, HardwareBreakpointAction::DisableWatchpoint) !=
         INVALID_NUB_HW_INDEX;
}

uint32_t MachThreadList::NumSupportedHardwareWatchpoints() const {
+8 −0
Original line number Diff line number Diff line
@@ -83,6 +83,14 @@ protected:
  typedef collection::iterator iterator;
  typedef collection::const_iterator const_iterator;

  enum class HardwareBreakpointAction {
    EnableWatchpoint,
    DisableWatchpoint,
  };

  uint32_t DoHardwareBreakpointAction(const DNBBreakpoint *wp,
                                      HardwareBreakpointAction action) const;

  uint32_t UpdateThreadList(MachProcess *process, bool update,
                            collection *num_threads = NULL);
  //  const_iterator  FindThreadByID (thread_t tid) const;