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

vm: add RemoveBreakPoint support #3674

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
47 changes: 47 additions & 0 deletions cli/vm/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,26 @@
> break 12`,
Action: handleBreak,
},
{
Name: "delete",
Usage: "Remove a breakpoint",
UsageText: `delete <ip>`,
Description: `<ip> is mandatory parameter.

Example:
> delete 12`,
Action: handleRemoveBreak,
},
{
Name: "ib",
Usage: "List breakpoints",
UsageText: `ib`,
Description: `List breakpoints.

Example:
> ib`,
Action: handleListBreak,
},
{
Name: "jump",
Usage: "Jump to the specified instruction (absolute IP value)",
Expand Down Expand Up @@ -597,6 +617,33 @@
return nil
}

func handleRemoveBreak(c *cli.Context) error {
ixje marked this conversation as resolved.
Show resolved Hide resolved
if !checkVMIsReady(c.App) {
return nil
}

Check warning on line 623 in cli/vm/cli.go

View check run for this annotation

Codecov / codecov/patch

cli/vm/cli.go#L622-L623

Added lines #L622 - L623 were not covered by tests
n, err := getInstructionParameter(c)
if err != nil {
return err
}

Check warning on line 627 in cli/vm/cli.go

View check run for this annotation

Codecov / codecov/patch

cli/vm/cli.go#L626-L627

Added lines #L626 - L627 were not covered by tests

v := getVMFromContext(c.App)
v.RemoveBreakPoint(n)
fmt.Fprintf(c.App.Writer, "breakpoint removed at instruction %d\n", n)
return nil
}

func handleListBreak(c *cli.Context) error {
if !checkVMIsReady(c.App) {
return nil
}

Check warning on line 638 in cli/vm/cli.go

View check run for this annotation

Codecov / codecov/patch

cli/vm/cli.go#L637-L638

Added lines #L637 - L638 were not covered by tests

v := getVMFromContext(c.App)
for _, bp := range v.Context().BreakPoints() {
fmt.Fprintf(c.App.Writer, "%d\n", bp)
}
return nil
}

func handleJump(c *cli.Context) error {
if !checkVMIsReady(c.App) {
return nil
Expand Down
25 changes: 25 additions & 0 deletions cli/vm/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,31 @@ func TestRun_WithNewVMContextAndBreakpoints(t *testing.T) {
e.checkNextLine(t, "at breakpoint 10 (ADD)*")
e.checkStack(t, 13)
})
t.Run("contract breakpoints", func(t *testing.T) {
src := `package kek
func Main(a, b int) int {
var c = a + b
return c + 5
}`
tmpDir := t.TempDir()
filename := prepareLoadgoSrc(t, tmpDir, src)

e := newTestVMCLI(t)
e.runProgWithTimeout(t, 10*time.Second,
"loadgo "+filename,
"break 7",
"break 8",
"ib",
"delete 7",
)

e.checkNextLine(t, "READY: loaded \\d* instructions")
e.checkNextLine(t, "breakpoint added at instruction 7")
e.checkNextLine(t, "breakpoint added at instruction 8")
e.checkNextLine(t, "7")
e.checkNextLine(t, "8")
e.checkNextLine(t, "breakpoint removed at instruction 7")
})
}

// prepareLoadgoSrc prepares provided SC source file for loading into VM via `loadgo` command.
Expand Down
7 changes: 7 additions & 0 deletions pkg/vm/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,11 @@ func TestContext_BreakPoints(t *testing.T) {
// New context -> clean breakpoints.
v.loadScriptWithCallingHash(prog, nil, nil, util.Uint160{}, util.Uint160{}, callflag.All, 1, 3, nil)
require.Nil(t, v.Context().BreakPoints())

v.AddBreakPoint(3)
v.AddBreakPoint(3)
v.AddBreakPoint(5)
require.Equal(t, []int{3, 3, 5}, v.Context().BreakPoints())
v.RemoveBreakPoint(3)
require.Equal(t, []int{5}, v.Context().BreakPoints())
}
8 changes: 8 additions & 0 deletions pkg/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,14 @@ func (v *VM) AddBreakPoint(n int) {
ctx.sc.breakPoints = append(ctx.sc.breakPoints, n)
}

// RemoveBreakPoint removes the breakpoint in the current context.
func (v *VM) RemoveBreakPoint(n int) {
AnnaShaleva marked this conversation as resolved.
Show resolved Hide resolved
ctx := v.Context()
ctx.sc.breakPoints = slices.DeleteFunc(ctx.sc.breakPoints, func(i int) bool {
return i == n
})
}

// AddBreakPointRel adds a breakpoint relative to the current
// instruction pointer.
func (v *VM) AddBreakPointRel(n int) {
Expand Down
Loading