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

TealDbg: Support for StepOver and refactoring object IDs #3653

Merged
merged 36 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
bb40245
Add stepover function for subroutines
algochoi Feb 15, 2022
e585a7a
Move object ID strings to cdtStateObjects.go
algochoi Feb 17, 2022
d015136
Run make sanity
algochoi Feb 17, 2022
852e71e
Use error return for setBreakpoint
algochoi Feb 17, 2022
d939d21
Revert accidental change in local state
algochoi Feb 18, 2022
3a55617
Add better checks to gloadImpl and add a unit test for runAll on grou…
algochoi Feb 23, 2022
07716aa
Revert gloadImpl check
algochoi Feb 23, 2022
fbfd338
Merge branch 'master' into algochoi/tealdbg-subroutine
algochoi Mar 11, 2022
9c21330
Add program source to tests
algochoi Mar 12, 2022
89ecb51
Add support for call stack on cdt tealdbg
algochoi Mar 16, 2022
c25ba18
Merge branch 'master' into algochoi/tealdbg-subroutine
algochoi Mar 16, 2022
c60ed66
Add parsing test for call stack
algochoi Mar 16, 2022
8cb9870
Revert a mistake in the test
algochoi Mar 16, 2022
eedee40
Merge branch 'master' into algochoi/tealdbg-subroutine
algochoi Mar 23, 2022
e6b39de
Add unit test for testing stepover control
algochoi Mar 23, 2022
766db9a
Refactor and cleanup unnecessary stepover flag
algochoi Mar 23, 2022
e2dcf4c
Finally fix the race conditions and defer notifications until end of …
algochoi Mar 23, 2022
7472837
Fix bad json struct
algochoi Mar 23, 2022
8ddfcc4
Check callstack in the Update handler
algochoi Mar 23, 2022
b6e23a5
Fix callsub tests
algochoi Mar 23, 2022
d39e57d
Change state after taking lock
algochoi Mar 24, 2022
73c857c
Add step out and unit tests
algochoi Mar 24, 2022
6546ac1
Refactor debugConfig to take multiple breakpoints for resume
algochoi Mar 24, 2022
942cb05
Change one of the comments for debugConfig to reflect updates
algochoi Mar 24, 2022
4dd4cd7
More minor refactoring and clean up to delete redundant code
algochoi Mar 25, 2022
61949c5
Consume error in deprecated webdbg :/
algochoi Mar 25, 2022
e46f1e3
Add line check inside the lock
algochoi Mar 25, 2022
f42f312
Minor fixes to testS
algochoi Mar 25, 2022
8717637
Refactor TestCallStackControl into subtests
michaeldiamant Mar 26, 2022
4ce49dc
Merge pull request #2 from michaeldiamant/algochoi/tealdbg-subroutine…
algochoi Mar 28, 2022
8307899
Run gofmt
algochoi Mar 28, 2022
24bdfa5
Fix stepout semantics and pause debugger only when we are outside sub…
algochoi Mar 31, 2022
fa51fff
Fix completed typo
algochoi Mar 31, 2022
f616437
Finish fixing typo
algochoi Mar 31, 2022
7634b63
Merge branch 'master' into algochoi/tealdbg-subroutine
algochoi Apr 12, 2022
1c9ed1a
Add new test for debugger hook
algochoi Apr 12, 2022
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
57 changes: 43 additions & 14 deletions cmd/tealdbg/cdtSession.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ func (s *cdtSession) websocketHandler(w http.ResponseWriter, r *http.Request) {
// set pc and line to 0 to workaround Register ack
state.Update(cdtStateUpdate{
dbgState.Stack, dbgState.Scratch,
0, 0, "",
dbgState.OpcodeBudget, s.debugger.GetStates(nil),
0, 0, "", dbgState.OpcodeBudget, dbgState.CallStack,
s.debugger.GetStates(nil),
})

hash := sha256.Sum256([]byte(state.disassembly)) // some random hash
Expand Down Expand Up @@ -247,7 +247,7 @@ func (s *cdtSession) websocketHandler(w http.ResponseWriter, r *http.Request) {
state.Update(cdtStateUpdate{
dbgState.Stack, dbgState.Scratch,
dbgState.PC, dbgState.Line, dbgState.Error,
dbgState.OpcodeBudget, appState,
dbgState.OpcodeBudget, dbgState.CallStack, appState,
})
dbgStateMu.Unlock()

Expand Down Expand Up @@ -480,7 +480,15 @@ func (s *cdtSession) handleCdtRequest(req *cdt.ChromeRequest, state *cdtState) (
events = append(events, &evDestroyed)
}
response = cdt.ChromeResponse{ID: req.ID, Result: empty}
case "Debugger.stepOver", "Debugger.stepInto":
case "Debugger.stepOver":
state.lastAction.Store("step")
s.debugger.StepOver()
if state.completed.IsSet() {
evDestroyed := s.makeContextDestroyedEvent()
events = append(events, &evDestroyed)
}
response = cdt.ChromeResponse{ID: req.ID, Result: empty}
case "Debugger.stepInto":
state.lastAction.Store("step")
s.debugger.Step()
if state.completed.IsSet() {
Expand Down Expand Up @@ -571,22 +579,43 @@ func (s *cdtSession) makeDebuggerPausedEvent(state *cdtState) cdt.DebuggerPaused
},
}
sc := []cdt.DebuggerScope{scopeLocal, scopeGlobal}
cf := cdt.DebuggerCallFrame{
CallFrameID: "mainframe",
FunctionName: "",
Location: &cdt.DebuggerLocation{
ScriptID: s.scriptID,
LineNumber: state.line.Load(),
ColumnNumber: 0,

cfs := []cdt.DebuggerCallFrame{
{
CallFrameID: "mainframe",
FunctionName: "main",
Location: &cdt.DebuggerLocation{
ScriptID: s.scriptID,
LineNumber: state.line.Load(),
ColumnNumber: 0,
},
URL: s.scriptURL,
ScopeChain: sc,
},
URL: s.scriptURL,
ScopeChain: sc,
}
for i := range state.callStack {
cf := cdt.DebuggerCallFrame{
CallFrameID: "mainframe",
FunctionName: state.callStack[i].LabelName,
Location: &cdt.DebuggerLocation{
ScriptID: s.scriptID,
LineNumber: state.line.Load(),
ColumnNumber: 0,
},
URL: s.scriptURL,
ScopeChain: sc,
}
// Set the previous call frame line number
cfs[0].Location.LineNumber = state.callStack[i].FrameLine
// We have to prepend the newest frame for it to appear first
// in the debugger...
cfs = append([]cdt.DebuggerCallFrame{cf}, cfs...)
}

evPaused := cdt.DebuggerPausedEvent{
Method: "Debugger.paused",
Params: cdt.DebuggerPausedParams{
CallFrames: []cdt.DebuggerCallFrame{cf},
CallFrames: cfs,
Reason: "other",
HitBreakpoints: make([]string, 0),
},
Expand Down
60 changes: 9 additions & 51 deletions cmd/tealdbg/cdtState.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ type cdtState struct {
globals []basics.TealValue

// mutable program state
mu deadlock.Mutex
stack []basics.TealValue
scratch []basics.TealValue
pc atomicInt
line atomicInt
err atomicString
mu deadlock.Mutex
stack []basics.TealValue
scratch []basics.TealValue
pc atomicInt
line atomicInt
err atomicString
callStack []logic.CallFrame
AppState

// debugger states
Expand All @@ -64,6 +65,7 @@ type cdtStateUpdate struct {
line int
err string
opcodeBudget int
callStack []logic.CallFrame

AppState
}
Expand Down Expand Up @@ -110,37 +112,7 @@ func (s *cdtState) Update(state cdtStateUpdate) {
s.AppState = state.AppState
// We need to dynamically override opcodeBudget with the proper value each step.
s.globals[logic.OpcodeBudget].Uint = uint64(state.opcodeBudget)
}

const localScopeObjID = "localScopeObjId"
const globalScopeObjID = "globalScopeObjID"
const globalsObjID = "globalsObjID"
const txnObjID = "txnObjID"
const gtxnObjID = "gtxnObjID"
const stackObjID = "stackObjID"
const scratchObjID = "scratchObjID"
const tealErrorID = "tealErrorID"
const appGlobalObjID = "appGlobalObjID"
const appLocalsObjID = "appLocalsObjID"
const txnArrayFieldObjID = "txnArrayField"
const logsObjID = "logsObjID"
const innerTxnsObjID = "innerTxnsObjID"

type objectDescFn func(s *cdtState, preview bool) []cdt.RuntimePropertyDescriptor

var objectDescMap = map[string]objectDescFn{
globalScopeObjID: makeGlobalScope,
localScopeObjID: makeLocalScope,
globalsObjID: makeGlobals,
txnObjID: makeTxn,
gtxnObjID: makeTxnGroup,
stackObjID: makeStack,
scratchObjID: makeScratch,
tealErrorID: makeTealError,
appGlobalObjID: makeAppGlobalState,
appLocalsObjID: makeAppLocalsState,
logsObjID: makeLogsState,
innerTxnsObjID: makeInnerTxnsState,
s.callStack = state.callStack
}

func (s *cdtState) getObjectDescriptor(objID string, preview bool) (desc []cdt.RuntimePropertyDescriptor, err error) {
Expand Down Expand Up @@ -591,8 +563,6 @@ func makeGlobalsPreview(globals []basics.TealValue) cdt.RuntimeObjectPreview {
return p
}

var gtxnObjIDPrefix = fmt.Sprintf("%s_gid_", gtxnObjID)

func encodeGroupTxnID(groupIndex int) string {
return gtxnObjIDPrefix + strconv.Itoa(groupIndex)
}
Expand All @@ -606,10 +576,6 @@ func decodeGroupTxnID(objID string) (int, bool) {
return 0, false
}

var logObjIDPrefix = fmt.Sprintf("%s_id", logsObjID)
var innerTxnObjIDPrefix = fmt.Sprintf("%s_id", innerTxnsObjID)
var innerNestedTxnObjIDPrefix = fmt.Sprintf("%s_nested", innerTxnsObjID)

func encodeNestedObjID(groupIndexes []int, prefix string) string {
encodedElements := []string{prefix}
for _, i := range groupIndexes {
Expand Down Expand Up @@ -695,8 +661,6 @@ func decodeArraySlice(objID string) (string, int, int, bool) {
return "", 0, 0, false
}

var appGlobalObjIDPrefix = fmt.Sprintf("%s_", appGlobalObjID)

func encodeAppGlobalAppID(key string) string {
return appGlobalObjIDPrefix + key
}
Expand All @@ -710,8 +674,6 @@ func decodeAppGlobalAppID(objID string) (uint64, bool) {
return 0, false
}

var appLocalsObjIDPrefix = fmt.Sprintf("%s_", appLocalsObjID)

func encodeAppLocalsAddr(addr string) string {
return appLocalsObjIDPrefix + addr
}
Expand All @@ -723,8 +685,6 @@ func decodeAppLocalsAddr(objID string) (string, bool) {
return "", false
}

var appLocalAppIDPrefix = fmt.Sprintf("%s__", appLocalsObjID)

func encodeAppLocalsAppID(addr string, appID string) string {
return fmt.Sprintf("%s%s_%s", appLocalAppIDPrefix, addr, appID)
}
Expand All @@ -740,8 +700,6 @@ func decodeAppLocalsAppID(objID string) (string, uint64, bool) {
return "", 0, false
}

var txnArrayFieldPrefix = fmt.Sprintf("%s__", txnArrayFieldObjID)

func encodeTxnArrayField(groupIndex int, field int) string {
return fmt.Sprintf("%s%d_%d", txnArrayFieldPrefix, groupIndex, field)
}
Expand Down
67 changes: 67 additions & 0 deletions cmd/tealdbg/cdtStateObjects.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright (C) 2019-2022 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.

package main

import (
"github.com/algorand/go-algorand/cmd/tealdbg/cdt"
)

// Object IDs
const (
localScopeObjID = "localScopeObjId"
globalScopeObjID = "globalScopeObjID"
globalsObjID = "globalsObjID"
txnObjID = "txnObjID"
gtxnObjID = "gtxnObjID"
stackObjID = "stackObjID"
scratchObjID = "scratchObjID"
tealErrorID = "tealErrorID"
appGlobalObjID = "appGlobalObjID"
appLocalsObjID = "appLocalsObjID"
txnArrayFieldObjID = "txnArrayField"
logsObjID = "logsObjID"
innerTxnsObjID = "innerTxnsObjID"
)

// Object Prefix IDs
const (
gtxnObjIDPrefix = gtxnObjID + "_gid_"
logObjIDPrefix = logsObjID + "_id"
innerTxnObjIDPrefix = innerTxnsObjID + "_id"
innerNestedTxnObjIDPrefix = innerTxnsObjID + "_nested"
appGlobalObjIDPrefix = appGlobalObjID + "_"
appLocalsObjIDPrefix = appLocalsObjID + "_"
appLocalAppIDPrefix = appLocalsObjID + "__"
txnArrayFieldPrefix = txnArrayFieldObjID + "__"
)

type objectDescFn func(s *cdtState, preview bool) []cdt.RuntimePropertyDescriptor

var objectDescMap = map[string]objectDescFn{
globalScopeObjID: makeGlobalScope,
localScopeObjID: makeLocalScope,
globalsObjID: makeGlobals,
txnObjID: makeTxn,
gtxnObjID: makeTxnGroup,
stackObjID: makeStack,
scratchObjID: makeScratch,
tealErrorID: makeTealError,
appGlobalObjID: makeAppGlobalState,
appLocalsObjID: makeAppLocalsState,
logsObjID: makeLogsState,
innerTxnsObjID: makeInnerTxnsState,
}
3 changes: 3 additions & 0 deletions cmd/tealdbg/cdtdbg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ type MockDebugControl struct {
func (c *MockDebugControl) Step() {
}

func (c *MockDebugControl) StepOver() {
}

func (c *MockDebugControl) Resume() {
}

Expand Down
23 changes: 23 additions & 0 deletions cmd/tealdbg/debugger.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type DebugAdapter interface {
// Control interface for execution control
type Control interface {
Step()
StepOver()
Resume()
SetBreakpoint(line int) error
RemoveBreakpoint(line int) error
Expand Down Expand Up @@ -187,6 +188,28 @@ func (s *session) Step() {
s.resume()
}

func (s *session) StepOver() {
currentLine := s.line.Load()
// Get the first TEAL opcode in the line
currentOp := strings.Fields(s.lines[currentLine])[0]

func() {
s.mu.Lock()
defer s.mu.Unlock()
// Step over a function call (callsub op).
if currentOp == "callsub" {
err := s.setBreakpoint(currentLine + 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log the error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the only time it would error would be if callsub was at the end of the program and setBreakpoint() will try to set a breakpoint past the program. In that case, it feels normal to not log something and just continue, but maybe I'm missing a case.

if err != nil {
s.debugConfig = debugConfig{BreakAtLine: stepBreak}
}
} else {
s.debugConfig = debugConfig{BreakAtLine: stepBreak}
}
}()

s.resume()
}

func (s *session) Resume() {
currentLine := s.line.Load()

Expand Down
7 changes: 4 additions & 3 deletions cmd/tealdbg/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,13 +535,14 @@ func (r *LocalRunner) RunAll() error {
start := time.Now()

ep := logic.NewEvalParams(txngroup, &r.proto, &transactions.SpecialAddresses{})
ep.Debugger = r.debugger

var last error
for i := range r.runs {
run := &r.runs[i]
r.debugger.SaveProgram(run.name, run.program, run.source, run.offsetToLine, run.states)

if r.debugger != nil {
r.debugger.SaveProgram(run.name, run.program, run.source, run.offsetToLine, run.states)
ep.Debugger = r.debugger
}
run.result.pass, run.result.err = run.eval(int(run.groupIndex), ep)
if run.result.err != nil {
failed++
Expand Down
Loading