Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[lldb-dap] Enabling instruction breakpoint support to lldb-dap. #105278

Merged
merged 75 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 68 commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
960351c
[lldb][test] Add the ability to extract the variable value out of the…
Nov 17, 2023
94c7680
Merge branch 'llvm:main' into main
santhoshe447 Nov 17, 2023
3235090
Merge branch 'llvm:main' into main
santhoshe447 Dec 12, 2023
ff9077f
Merge branch 'llvm:main' into main
santhoshe447 Dec 12, 2023
fa762d2
Merge branch 'llvm:main' into main
santhoshe447 Dec 12, 2023
09ff158
Merge branch 'llvm:main' into main
santhoshe447 May 3, 2024
ab44a69
[lldb-dap] Added "port" property to vscode "attach" command.
May 3, 2024
ef4f016
Merge branch 'llvm:main' into main
santhoshe447 May 3, 2024
35ba7f7
Merge branch 'llvm:main' into main
santhoshe447 May 6, 2024
bd38f88
Merge branch 'llvm:main' into main
santhoshe447 May 7, 2024
f65a580
Merge branch 'llvm:main' into main
santhoshe447 May 7, 2024
5763225
Merge branch 'llvm:main' into main
santhoshe447 May 7, 2024
bb18d6d
Merge branch 'llvm:main' into main
santhoshe447 May 7, 2024
1b1ae2b
Merge branch 'llvm:main' into main
santhoshe447 May 7, 2024
a2e4580
Merge branch 'llvm:main' into main
santhoshe447 May 8, 2024
3795dcd
Merge branch 'llvm:main' into main
santhoshe447 May 8, 2024
628fa40
Merge branch 'llvm:main' into main
santhoshe447 May 8, 2024
4e2e524
[lldb-dap] Added "port" property to vscode "attach" command.
May 8, 2024
4774f58
Merge branch 'llvm:main' into main
santhoshe447 May 9, 2024
175f1d3
[lldb-dap] Added "port" property to vscode "attach" command.
May 9, 2024
a6e9f66
Merge branch 'llvm:main' into main
santhoshe447 May 9, 2024
e5c0aed
Resolved code format issue.
May 9, 2024
c502203
Resolved code format errors.
May 9, 2024
89800a8
Merge branch 'llvm:main' into main
santhoshe447 May 15, 2024
c1bf2bd
[lldb-dap] Added "port" property to vscode "attach" command - Address…
May 15, 2024
5590afc
[lldb-dap] Added "port" property to vscode "attach" command - fix cod…
May 15, 2024
afcce33
Merge branch 'llvm:main' into main
santhoshe447 May 20, 2024
281c281
Merge branch 'llvm:main' into main
santhoshe447 May 24, 2024
624b789
Merge branch 'llvm:main' into main
santhoshe447 May 28, 2024
023b51e
[lldb-dap] Added "port" property to vscode "attach" command - resolve…
May 28, 2024
d19eb88
[lldb-dap] Added "port" property to vscode "attach" command -resolved…
May 28, 2024
16c37cd
Merge branch 'llvm:main' into main
santhoshe447 May 30, 2024
599a5ca
Merge branch 'llvm:main' into main
santhoshe447 May 30, 2024
fe9a4b6
[lldb-dap] Added "port" property to vscode "attach" command. #91570
May 30, 2024
c91b02d
[lldb-dap] Added "port" property to vscode "attach" command.
May 30, 2024
35ad541
Merge branch 'llvm:main' into main
santhoshe447 Jun 4, 2024
8e3f3a5
Merge branch 'llvm:main' into main
santhoshe447 Jun 4, 2024
de29728
[lldb-dap] Added "port" property to vscode "attach" command - address…
Jun 4, 2024
ac84b53
[lldb-dap] Added "port" property to vscode "attach" command - resolve…
Jun 4, 2024
7d1319c
Merge branch 'llvm:main' into main
santhoshe447 Jun 7, 2024
2a0eb9d
Merge branch 'llvm:main' into main
santhoshe447 Jun 10, 2024
08f40aa
[lldb-dap] Added "port" property to vscode "attach" command - fixed …
Jun 10, 2024
2a0bc63
[lldb-dap] Added "port" property to vscode "attach" command - resolve…
Jun 10, 2024
656c34a
[lldb-dap] Added "port" property to vscode "attach" command.
Jun 10, 2024
673ae34
Merge branch 'llvm:main' into main
santhoshe447 Jun 28, 2024
c6e552a
Merge branch 'llvm:main' into main
santhoshe447 Jul 22, 2024
043895d
Merge branch 'llvm:main' into main
santhoshe447 Jul 22, 2024
fe36e25
[lldb-dap] Updated README.md for newly added attach properties.
Jul 22, 2024
2011cb5
Merge branch 'llvm:main' into main
santhoshe447 Jul 23, 2024
03e4c20
Merge branch 'llvm:main' into main
santhoshe447 Jul 24, 2024
10d16b8
[lldb-dap] Updated README.md for newly added attach properties. #99926
Jul 24, 2024
a6c4895
Merge branch 'llvm:main' into main
santhoshe447 Jul 25, 2024
58affe7
[lldb-dap] Bump the version to 0.2.3
Jul 25, 2024
033c6a1
Merge branch 'llvm:main' into main
santhoshe447 Jul 25, 2024
7c96402
Merge branch 'llvm:main' into main
santhoshe447 Jul 26, 2024
17a99a7
Merge branch 'llvm:main' into main
santhoshe447 Aug 5, 2024
1e7d5f0
Merge branch 'llvm:main' into main
santhoshe447 Aug 13, 2024
e6fa6a8
Merge branch 'llvm:main' into main
santhoshe447 Aug 19, 2024
5069297
Merge branch 'llvm:main' into main
santhoshe447 Aug 20, 2024
7e22182
[lldb-dap] Enabling instruction breakpoint support to lldb-dap.
santhoshe447 Aug 20, 2024
ffe8dbc
[lldb-dap] Enabling instruction breakpoint support to lldb-dap.
santhoshe447 Aug 20, 2024
4d87a93
[lldb-dap] Enabling instruction breakpoint support to lldb-dap.
santhoshe447 Aug 20, 2024
18bcb03
[lldb-dap] Enabling instruction breakpoint support to lldb-dap.
santhoshe447 Aug 20, 2024
9893985
[lldb-dap] Enabling instruction breakpoint support to lldb-dap.
santhoshe447 Aug 21, 2024
e0101c1
[lldb-dap] Enabling instruction breakpoint support to lldb-dap.
santhoshe447 Aug 22, 2024
17f8c6d
[lldb-dap] Enabling instruction breakpoint support to lldb-dap.
santhoshe447 Aug 22, 2024
8c19498
[lldb-dap] Enabling instruction breakpoint support to lldb-dap.
santhoshe447 Aug 22, 2024
5031df9
Merge branch 'main' into main
santhoshe447 Aug 22, 2024
27d668f
Merge branch 'llvm:main' into main
santhoshe447 Aug 26, 2024
dbad4c5
[lldb-dap] Enabling instruction breakpoint support to lldb-dap.
santhoshe447 Aug 26, 2024
bac086e
[lldb-dap] Enabling instruction breakpoint support to lldb-dap.
santhoshe447 Aug 26, 2024
5401fbe
[lldb-dap] Enabling instruction breakpoint support to lldb-dap.
santhoshe447 Aug 26, 2024
3523cef
[lldb-dap] Enabling instruction breakpoint support to lldb-dap.
santhoshe447 Aug 26, 2024
ab68b22
[lldb-dap] Enabling instruction breakpoint support to lldb-dap.
santhoshe447 Aug 26, 2024
fc30dd1
Merge branch 'llvm:main' into main
santhoshe447 Aug 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,20 @@ def terminate(self):
self.send.close()
# self.recv.close()

def request_setInstructionBreakpoints(self, memory_reference=[]):
breakpoints = []
for i in memory_reference:
args_dict = {
"instructionReference": i,
}
breakpoints.append(args_dict)
args_dict = {"breakpoints": breakpoints}
command_dict = {
"command": "setInstructionBreakpoints",
"type": "request",
"arguments": args_dict,
}
return self.send_recv(command_dict)

class DebugAdaptorServer(DebugCommunication):
def __init__(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CXX_SOURCES := main-copy.cpp
CXXFLAGS_EXTRAS := -O0 -g
include Makefile.rules

main-copy.cpp: main.cpp
cp -f $< $@
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import dap_server
import shutil
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil
import lldbdap_testcase
import os
import lldb


class TestDAP_InstructionBreakpointTestCase(lldbdap_testcase.DAPTestCaseBase):
NO_DEBUG_INFO_TESTCASE = True

def setUp(self):
lldbdap_testcase.DAPTestCaseBase.setUp(self)

self.main_basename = "main-copy.cpp"
self.main_path = os.path.realpath(self.getBuildArtifact(self.main_basename))

def test_instruction_breakpoint(self):
self.build()
self.instruction_breakpoint_test()

def instruction_breakpoint_test(self):
"""Sample test to ensure SBFrame::Disassemble produces SOME output"""
# Create a target by the debugger.
target = self.createTestTarget()

main_line = line_number("main.cpp", "breakpoint 1")

program = self.getBuildArtifact("a.out")
self.build_and_launch(program)

# Set source breakpoint 1
response = self.dap_server.request_setBreakpoints(self.main_path, [main_line])
breakpoints = response["body"]["breakpoints"]
self.assertEquals(len(breakpoints), 1)
breakpoint = breakpoints[0]
self.assertEqual(
breakpoint["line"], main_line, "incorrect breakpoint source line"
)
self.assertTrue(breakpoint["verified"], "breakpoint is not verified")
self.assertEqual(
self.main_basename, breakpoint["source"]["name"], "incorrect source name"
)
self.assertEqual(
self.main_path, breakpoint["source"]["path"], "incorrect source file path"
)
other_breakpoint_id = breakpoint["id"]

# Continue and then verifiy the breakpoint
self.dap_server.request_continue()
self.verify_breakpoint_hit([other_breakpoint_id])

# now we check the stack trace making sure that we got mapped source paths
frames = self.dap_server.request_stackTrace()["body"]["stackFrames"]
intstructionPointerReference = []
setIntstructionBreakpoints = []
intstructionPointerReference.append(frames[0]["instructionPointerReference"])
self.assertEqual(
frames[0]["source"]["name"], self.main_basename, "incorrect source name"
)
self.assertEqual(
frames[0]["source"]["path"], self.main_path, "incorrect source file path"
)

# Check disassembly view
instruction = self.disassemble(frameIndex=0)
self.assertEqual(
instruction["address"],
intstructionPointerReference[0],
"current breakpoint reference is not in the disaasembly view",
)

# Get next instruction address to set instruction breakpoint
disassembled_instruction_list = self.dap_server.disassembled_instructions
instruction_addr_list = list(disassembled_instruction_list.keys())
index = instruction_addr_list.index(intstructionPointerReference[0])
if len(instruction_addr_list) >= (index + 1):
next_inst_addr = int(instruction_addr_list[index + 1], 16)
if next_inst_addr is not 0:
setIntstructionBreakpoints.append(hex(next_inst_addr))
instruction_breakpoint_response = (
self.dap_server.request_setInstructionBreakpoints(
setIntstructionBreakpoints
)
)
inst_breakpoints = instruction_breakpoint_response["body"][
"breakpoints"
]
self.assertEqual(
inst_breakpoints[0]["instructionReference"],
hex(next_inst_addr),
"Instruction breakpoint has not been resolved or failed to relocate the instruction breakpoint",
)
self.dap_server.request_continue()
self.verify_breakpoint_hit([inst_breakpoints[0]["id"]])
18 changes: 18 additions & 0 deletions lldb/test/API/tools/lldb-dap/instruction-breakpoint/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <stdio.h>
#include <unistd.h>

int function(int x) {

if (x == 0) // breakpoint 1
return x;

if ((x % 2) != 0)
return x;
else
return function(x - 1) + x;
}

int main(int argc, char const *argv[]) {
int n = function(2);
return n;
}
1 change: 1 addition & 0 deletions lldb/tools/lldb-dap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ add_lldb_tool(lldb-dap
SourceBreakpoint.cpp
DAP.cpp
Watchpoint.cpp
InstructionBreakpoint.cpp

LINK_LIBS
liblldb
Expand Down
31 changes: 30 additions & 1 deletion lldb/tools/lldb-dap/DAP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ static std::string capitalize(llvm::StringRef str) {

void DAP::PopulateExceptionBreakpoints() {
llvm::call_once(init_exception_breakpoints_flag, [this]() {
exception_breakpoints = std::vector<ExceptionBreakpoint> {};
exception_breakpoints = std::vector<ExceptionBreakpoint>{};

if (lldb::SBDebugger::SupportsLanguage(lldb::eLanguageTypeC_plus_plus)) {
exception_breakpoints->emplace_back("cpp_catch", "C++ Catch",
Expand Down Expand Up @@ -996,4 +996,33 @@ void DAP::SetThreadFormat(llvm::StringRef format) {
}
}

InstructionBreakpoint *
DAP::GetInstructionBreakpoint(const lldb::break_id_t bp_id) {

santhoshe447 marked this conversation as resolved.
Show resolved Hide resolved
for (auto &bp : instruction_breakpoints) {
if (bp.second.id == bp_id)
return &bp.second;
}
return nullptr;
}

InstructionBreakpoint *
DAP::GetInstructionBPFromStopReason(lldb::SBThread &thread) {
const auto num = thread.GetStopReasonDataCount();
InstructionBreakpoint *inst_bp = nullptr;
for (size_t i = 0; i < num; i += 2) {
// thread.GetStopReasonDataAtIndex(i) will return the bp ID and
// thread.GetStopReasonDataAtIndex(i+1) will return the location
// within that breakpoint. We only care about the bp ID so we can
// see if this is an instruction breakpoint that is getting hit.
lldb::break_id_t bp_id = thread.GetStopReasonDataAtIndex(i);
inst_bp = GetInstructionBreakpoint(bp_id);
// If any breakpoint is not an instruction breakpoint, then stop and
// report this as a normal breakpoint
if (inst_bp == nullptr)
return nullptr;
}
return inst_bp;
}

} // namespace lldb_dap
8 changes: 8 additions & 0 deletions lldb/tools/lldb-dap/DAP.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include "ExceptionBreakpoint.h"
#include "FunctionBreakpoint.h"
#include "IOStream.h"
#include "InstructionBreakpoint.h"
#include "ProgressEvent.h"
#include "RunInTerminal.h"
#include "SourceBreakpoint.h"
Expand All @@ -68,6 +69,8 @@ namespace lldb_dap {

typedef llvm::DenseMap<uint32_t, SourceBreakpoint> SourceBreakpointMap;
typedef llvm::StringMap<FunctionBreakpoint> FunctionBreakpointMap;
typedef llvm::DenseMap<lldb::addr_t, InstructionBreakpoint>
InstructionBreakpointMap;

enum class OutputType { Console, Stdout, Stderr, Telemetry };

Expand Down Expand Up @@ -160,6 +163,7 @@ struct DAP {
std::unique_ptr<std::ofstream> log;
llvm::StringMap<SourceBreakpointMap> source_breakpoints;
FunctionBreakpointMap function_breakpoints;
InstructionBreakpointMap instruction_breakpoints;
std::optional<std::vector<ExceptionBreakpoint>> exception_breakpoints;
llvm::once_flag init_exception_breakpoints_flag;
std::vector<std::string> pre_init_commands;
Expand Down Expand Up @@ -334,6 +338,10 @@ struct DAP {

void SetThreadFormat(llvm::StringRef format);

InstructionBreakpoint *GetInstructionBreakpoint(const lldb::break_id_t bp_id);

InstructionBreakpoint *GetInstructionBPFromStopReason(lldb::SBThread &thread);

private:
// Send the JSON in "json_str" to the "out" stream. Correctly send the
// "Content-Length:" field followed by the length, followed by the raw
Expand Down
1 change: 1 addition & 0 deletions lldb/tools/lldb-dap/DAPForward.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct ExceptionBreakpoint;
struct FunctionBreakpoint;
struct SourceBreakpoint;
struct Watchpoint;
struct InstructionBreakpoint;
} // namespace lldb_dap

namespace lldb {
Expand Down
27 changes: 27 additions & 0 deletions lldb/tools/lldb-dap/InstructionBreakpoint.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===-- InstructionBreakpoint.cpp ------------------------------------*- C++
//-*-===//
//
// 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 "InstructionBreakpoint.h"
#include "DAP.h"

namespace lldb_dap {

// Instruction Breakpoint
InstructionBreakpoint::InstructionBreakpoint(const llvm::json::Object &obj)
: Breakpoint(obj), instructionReference(LLDB_INVALID_ADDRESS), id(0),
offset(GetSigned(obj, "offset", 0)) {
GetString(obj, "instructionReference").getAsInteger(0, instructionReference);
instructionReference += offset;
}

void InstructionBreakpoint::SetInstructionBreakpoint() {
bp = g_dap.target.BreakpointCreateByAddress(instructionReference);
id = bp.GetID();
}
} // namespace lldb_dap
36 changes: 36 additions & 0 deletions lldb/tools/lldb-dap/InstructionBreakpoint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//===-- InstructionBreakpoint.h --------------------------------------*- C++
//-*-===//
//
// 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
//
//===----------------------------------------------------------------------===//

#ifndef LLDB_TOOLS_LLDB_DAP_INSTRUCTIONBREAKPOINT_H
#define LLDB_TOOLS_LLDB_DAP_INSTRUCTIONBREAKPOINT_H

#include "Breakpoint.h"
#include "llvm/ADT/StringRef.h"

namespace lldb_dap {

// Instruction Breakpoint
struct InstructionBreakpoint : public Breakpoint {

lldb::addr_t instructionReference;
santhoshe447 marked this conversation as resolved.
Show resolved Hide resolved
int32_t id;
int32_t offset;

InstructionBreakpoint()
: Breakpoint(), instructionReference(LLDB_INVALID_ADDRESS), id(0),
offset(0) {}
InstructionBreakpoint(const llvm::json::Object &obj);

// Set instruction breakpoint in LLDB as a new breakpoint
void SetInstructionBreakpoint();
};

} // namespace lldb_dap

#endif
Loading