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