xref: /llvm-project/lldb/tools/lldb-dap/Watchpoint.cpp (revision b99d4112585302cbd01f9b851a04adc6e4fb5218)
1 //===-- Watchpoint.cpp ------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "Watchpoint.h"
10 #include "DAP.h"
11 #include "JSONUtils.h"
12 #include "lldb/API/SBTarget.h"
13 #include "lldb/lldb-enumerations.h"
14 #include "llvm/ADT/StringExtras.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/JSON.h"
17 #include <cstdint>
18 #include <string>
19 
20 namespace lldb_dap {
21 Watchpoint::Watchpoint(DAP &d, const llvm::json::Object &obj)
22     : BreakpointBase(d, obj) {
23   llvm::StringRef dataId = GetString(obj, "dataId");
24   std::string accessType = GetString(obj, "accessType").str();
25   auto [addr_str, size_str] = dataId.split('/');
26   llvm::to_integer(addr_str, addr, 16);
27   llvm::to_integer(size_str, size);
28   options.SetWatchpointTypeRead(accessType != "write");
29   if (accessType != "read")
30     options.SetWatchpointTypeWrite(lldb::eWatchpointWriteTypeOnModify);
31 }
32 
33 void Watchpoint::SetCondition() { wp.SetCondition(condition.c_str()); }
34 
35 void Watchpoint::SetHitCondition() {
36   uint64_t hitCount = 0;
37   if (llvm::to_integer(hitCondition, hitCount))
38     wp.SetIgnoreCount(hitCount - 1);
39 }
40 
41 void Watchpoint::CreateJsonObject(llvm::json::Object &object) {
42   if (!error.IsValid() || error.Fail()) {
43     object.try_emplace("verified", false);
44     if (error.Fail())
45       EmplaceSafeString(object, "message", error.GetCString());
46   } else {
47     object.try_emplace("verified", true);
48   }
49 }
50 
51 void Watchpoint::SetWatchpoint() {
52   wp = dap.target.WatchpointCreateByAddress(addr, size, options, error);
53   if (!condition.empty())
54     SetCondition();
55   if (!hitCondition.empty())
56     SetHitCondition();
57 }
58 } // namespace lldb_dap
59