aboutsummaryrefslogtreecommitdiff
path: root/lldb/source/Breakpoint/WatchpointResource.cpp
blob: c2f510f03638f71690be377ffda24faad7869a92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//===-- WatchpointResource.cpp --------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include <assert.h>

#include "lldb/Breakpoint/WatchpointResource.h"
#include "lldb/Utility/Stream.h"

#include <algorithm>

using namespace lldb;
using namespace lldb_private;

WatchpointResource::WatchpointResource(lldb::addr_t addr, size_t size,
                                       bool read, bool write)
    : m_id(GetNextID()), m_addr(addr), m_size(size),
      m_watch_read(read), m_watch_write(write) {}

WatchpointResource::~WatchpointResource() {
  std::lock_guard<std::mutex> guard(m_constituents_mutex);
  m_constituents.clear();
}

addr_t WatchpointResource::GetLoadAddress() const { return m_addr; }

size_t WatchpointResource::GetByteSize() const { return m_size; }

bool WatchpointResource::WatchpointResourceRead() const { return m_watch_read; }

bool WatchpointResource::WatchpointResourceWrite() const {
  return m_watch_write;
}

void WatchpointResource::SetType(bool read, bool write) {
  m_watch_read = read;
  m_watch_write = write;
}

wp_resource_id_t WatchpointResource::GetID() const { return m_id; }

bool WatchpointResource::Contains(addr_t addr) {
  if (addr >= m_addr && addr < m_addr + m_size)
    return true;
  return false;
}

void WatchpointResource::AddConstituent(const WatchpointSP &wp_sp) {
  std::lock_guard<std::mutex> guard(m_constituents_mutex);
  m_constituents.push_back(wp_sp);
}

void WatchpointResource::RemoveConstituent(WatchpointSP &wp_sp) {
  std::lock_guard<std::mutex> guard(m_constituents_mutex);
  auto it = llvm::find(m_constituents, wp_sp);
  if (it != m_constituents.end())
    m_constituents.erase(it);
}

size_t WatchpointResource::GetNumberOfConstituents() {
  std::lock_guard<std::mutex> guard(m_constituents_mutex);
  return m_constituents.size();
}

bool WatchpointResource::ConstituentsContains(const WatchpointSP &wp_sp) {
  return ConstituentsContains(wp_sp.get());
}

bool WatchpointResource::ConstituentsContains(const Watchpoint *wp) {
  std::lock_guard<std::mutex> guard(m_constituents_mutex);
  return llvm::any_of(m_constituents,
                      [&wp](const WatchpointSP &x) { return x.get() == wp; });
}

WatchpointSP WatchpointResource::GetConstituentAtIndex(size_t idx) {
  std::lock_guard<std::mutex> guard(m_constituents_mutex);
  assert(idx < m_constituents.size());
  if (idx >= m_constituents.size())
    return {};

  return m_constituents[idx];
}

WatchpointResource::WatchpointCollection
WatchpointResource::CopyConstituentsList() {
  std::lock_guard<std::mutex> guard(m_constituents_mutex);
  return m_constituents;
}

bool WatchpointResource::ShouldStop(StoppointCallbackContext *context) {
  // LWP_TODO: Need to poll all Watchpoint constituents and see if
  // we should stop, like BreakpointSites do.
#if 0
  m_hit_counter.Increment();
  // ShouldStop can do a lot of work, and might even come back and hit
  // this breakpoint site again.  So don't hold the m_constituents_mutex the
  // whole while.  Instead make a local copy of the collection and call
  // ShouldStop on the copy.
  WatchpointResourceCollection constituents_copy;
  {
    std::lock_guard<std::recursive_mutex> guard(m_constituents_mutex);
    constituents_copy = m_constituents;
  }
  return constituents_copy.ShouldStop(context);
#endif
  return true;
}

void WatchpointResource::Dump(Stream *s) const {
  s->Printf("addr = 0x%8.8" PRIx64 " size = %zu", m_addr, m_size);
}

wp_resource_id_t WatchpointResource::GetNextID() {
  static wp_resource_id_t g_next_id = 0;
  return ++g_next_id;
}