aboutsummaryrefslogtreecommitdiff
path: root/lldb/tools/lldb-dap/Handler/SetInstructionBreakpointsRequestHandler.cpp
blob: 48b2ba5a25594b71811b8dfacac77a52ed47d984 (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
//===-- SetInstructionBreakpointsRequestHandler.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 "DAP.h"
#include "EventHelper.h"
#include "RequestHandler.h"

namespace lldb_dap {

/// Replaces all existing instruction breakpoints. Typically, instruction
/// breakpoints would be set from a disassembly window. To clear all instruction
/// breakpoints, specify an empty array. When an instruction breakpoint is hit,
/// a stopped event (with reason instruction breakpoint) is generated. Clients
/// should only call this request if the corresponding capability
/// supportsInstructionBreakpoints is true.
llvm::Expected<protocol::SetInstructionBreakpointsResponseBody>
SetInstructionBreakpointsRequestHandler::Run(
    const protocol::SetInstructionBreakpointsArguments &args) const {
  std::vector<protocol::Breakpoint> response_breakpoints;

  // Disable any instruction breakpoints that aren't in this request.
  // There is no call to remove instruction breakpoints other than calling this
  // function with a smaller or empty "breakpoints" list.
  llvm::DenseSet<lldb::addr_t> seen(
      llvm::from_range, llvm::make_first_range(dap.instruction_breakpoints));

  for (const auto &bp : args.breakpoints) {
    // Read instruction breakpoint request.
    InstructionBreakpoint inst_bp(dap, bp);
    const auto [iv, inserted] = dap.instruction_breakpoints.try_emplace(
        inst_bp.GetInstructionAddressReference(), dap, bp);
    if (inserted)
      iv->second.SetBreakpoint();
    else
      iv->second.UpdateBreakpoint(inst_bp);
    response_breakpoints.push_back(iv->second.ToProtocolBreakpoint());
    seen.erase(inst_bp.GetInstructionAddressReference());
  }

  for (const auto &addr : seen) {
    auto inst_bp = dap.instruction_breakpoints.find(addr);
    if (inst_bp == dap.instruction_breakpoints.end())
      continue;
    dap.target.BreakpointDelete(inst_bp->second.GetID());
    dap.instruction_breakpoints.erase(addr);
  }

  return protocol::SetInstructionBreakpointsResponseBody{
      std::move(response_breakpoints)};
}

} // namespace lldb_dap