Skip to content

Commit

Permalink
Add SIGINT breaking in interactive debugger to break at next instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
maxfierke committed Sep 26, 2024
1 parent 98d5b3e commit 4cde17b
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions debug/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package debug
import (
"errors"
"fmt"
"os"
"os/signal"
"slices"
"strconv"
"strings"
"sync"

"github.com/abiosoft/ishell/v2"
"github.com/maxfierke/gogo-gb/cpu"
Expand All @@ -23,7 +26,8 @@ type breakpoint struct{}

type InteractiveDebugger struct {
breakpoints map[uint16]breakpoint
isStepping bool
stepping bool
steppingMu sync.Mutex
shell *ishell.Shell
}

Expand Down Expand Up @@ -64,7 +68,7 @@ func NewInteractiveDebugger() (*InteractiveDebugger, error) {
Aliases: []string{"c"},
Help: "Continue execution until next breakpoint",
Func: func(c *ishell.Context) {
debugger.isStepping = false
debugger.stopStepping()
c.Stop()
},
})
Expand All @@ -74,7 +78,7 @@ func NewInteractiveDebugger() (*InteractiveDebugger, error) {
Aliases: []string{"s"},
Help: "Execute the next instruction",
Func: func(c *ishell.Context) {
debugger.isStepping = true
debugger.startStepping()
c.Stop()
},
})
Expand Down Expand Up @@ -266,14 +270,24 @@ func NewInteractiveDebugger() (*InteractiveDebugger, error) {
},
})

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
if !debugger.isActive() {
debugger.startStepping()
}
}
}()

debugger.shell = shell

return debugger, nil
}

func (i *InteractiveDebugger) OnDecode(cpu *cpu.CPU, mmu *mem.MMU) {
addr := cpu.PC.Read()
if _, ok := i.breakpoints[addr]; ok || i.isStepping {
if _, ok := i.breakpoints[addr]; ok || i.isStepping() {
i.shell.Printf("reached 0x%02X\n", addr)
i.attachShell(cpu, mmu)
}
Expand Down Expand Up @@ -310,6 +324,28 @@ func (i *InteractiveDebugger) attachShell(cpu *cpu.CPU, mmu *mem.MMU) {
i.shell.Run()
}

func (i *InteractiveDebugger) isActive() bool {
return i.shell.Active()
}

func (i *InteractiveDebugger) startStepping() {
i.steppingMu.Lock()
defer i.steppingMu.Unlock()
i.stepping = true
}

func (i *InteractiveDebugger) stopStepping() {
i.steppingMu.Lock()
defer i.steppingMu.Unlock()
i.stepping = false
}

func (i *InteractiveDebugger) isStepping() bool {
i.steppingMu.Lock()
defer i.steppingMu.Unlock()
return i.stepping
}

func (i *InteractiveDebugger) printState(cpu *cpu.CPU, mmu *mem.MMU) {
i.shell.Printf("Registers:\n")
i.shell.Printf(
Expand Down

0 comments on commit 4cde17b

Please sign in to comment.