Skip to content

Commit

Permalink
move debugger to its own package
Browse files Browse the repository at this point in the history
  • Loading branch information
safchain committed Nov 20, 2024
1 parent adf57ad commit 0cca050
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 71 deletions.
3 changes: 2 additions & 1 deletion examples/ex2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/cilium/ebpf"
"github.com/safchain/baloum/pkg/baloum"
"github.com/safchain/baloum/pkg/baloum/debugger"
"go.uber.org/zap"
)

Expand All @@ -48,7 +49,7 @@ func main() {
},
}

debugger := baloum.NewDebugger(true, nil)
debugger := debugger.NewDebugger(true, nil)
defer debugger.Close()

vm := baloum.NewVM(spec, baloum.Opts{Fncs: fncs, Observer: debugger})
Expand Down
29 changes: 16 additions & 13 deletions pkg/baloum/debugger.go → pkg/baloum/debugger/debugger.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package baloum
package debugger

import (
"fmt"
Expand All @@ -24,6 +24,7 @@ import (

"github.com/cilium/ebpf/asm"
"github.com/peterh/liner"
"github.com/safchain/baloum/pkg/baloum"
)

type DebugCommand string
Expand Down Expand Up @@ -64,8 +65,8 @@ func NewDebugger(enabled bool, variableReaders map[string]VariableReader) *Debug
}
}

func (d *Debugger) dumpRegister(vm *VM) {
for i, v := range vm.regs {
func (d *Debugger) dumpRegister(vm *baloum.VM) {
for i, v := range vm.Regs() {
if i > 0 {
fmt.Printf(", ")
}
Expand All @@ -89,17 +90,17 @@ func (d *Debugger) dumpBytes(bytes []byte) {
fmt.Println()
}

func (d *Debugger) dumpStack(vm *VM) {
d.dumpBytes(vm.stack)
func (d *Debugger) dumpStack(vm *baloum.VM) {
d.dumpBytes(vm.Stack())
}

func (d *Debugger) printMap(vm *VM, args ...string) {
func (d *Debugger) printMap(vm *baloum.VM, args ...string) {
if len(args) != 1 {
fmt.Fprintf(os.Stderr, "syntax error: pm <mapname>\n")
return
}

_map := vm.maps.GetMapByName(args[0])
_map := vm.GetMapByName(args[0])
if _map == nil {
fmt.Fprintf(os.Stderr, "map not found")
return
Expand All @@ -125,7 +126,7 @@ func (d *Debugger) printMap(vm *VM, args ...string) {
}
}

func (d *Debugger) printVariable(vm *VM, args ...string) {
func (d *Debugger) printVariable(vm *baloum.VM, args ...string) {
if len(args) != 2 {
fmt.Fprintf(os.Stderr, "syntax error: pr <varname> <addr|register>\n")
return
Expand All @@ -140,13 +141,15 @@ func (d *Debugger) printVariable(vm *VM, args ...string) {

var ptr uint64

regs := vm.Regs()

if addr[0] == 'R' || addr[0] == 'r' {
reg, err := strconv.Atoi(addr[1:])
if err != nil || reg >= len(vm.regs) {
if err != nil || reg >= len(regs) {
fmt.Fprintf(os.Stderr, "register unknown\n")
return
}
ptr = vm.regs[reg]
ptr = regs[reg]
} else {
value, err := strconv.Atoi(addr)
if err != nil {
Expand All @@ -156,7 +159,7 @@ func (d *Debugger) printVariable(vm *VM, args ...string) {
ptr = uint64(value)
}

bytes, err := vm.getBytes(ptr, reader.Size)
bytes, err := vm.GetBytes(ptr, reader.Size)
if err != nil {
fmt.Fprintf(os.Stderr, "address incorrect\n")
return
Expand All @@ -165,7 +168,7 @@ func (d *Debugger) printVariable(vm *VM, args ...string) {
fmt.Printf(">> %v\n", reader.Read(bytes))
}

func (d *Debugger) printBacktrace(vm *VM) {
func (d *Debugger) printBacktrace(vm *baloum.VM) {
for _, bt := range d.backtrace {
fmt.Printf("%d: %v\n", bt.PC, bt.Inst)
}
Expand All @@ -177,7 +180,7 @@ func (d *Debugger) Close() {
}
}

func (d *Debugger) ObserveInst(vm *VM, pc int, inst *asm.Instruction) {
func (d *Debugger) ObserveInst(vm *baloum.VM, pc int, inst *asm.Instruction) {
d.backtrace = append(d.backtrace, BTInst{PC: pc, Inst: *inst})

if d.state == nil {
Expand Down
38 changes: 19 additions & 19 deletions pkg/baloum/fncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func FnMallocImpl(vm *VM, inst *asm.Instruction) error {
}

func FnCallImpl(vm *VM, inst *asm.Instruction) error {
data, err := vm.getBytes(vm.regs[asm.R1], 0)
data, err := vm.GetBytes(vm.regs[asm.R1], 0)
if err != nil {
return err
}
Expand All @@ -93,7 +93,7 @@ func FnCallImpl(vm *VM, inst *asm.Instruction) error {
return err
}

section, err := vm.getString(vm.regs[asm.R2])
section, err := vm.GetString(vm.regs[asm.R2])
if err != nil {
return err
}
Expand All @@ -113,12 +113,12 @@ func FnStrCmpImpl(vm *VM, inst *asm.Instruction) error {
code := ErrorCode
vm.regs[asm.R0] = uint64(code)

s1, err := vm.getString(vm.regs[asm.R1])
s1, err := vm.GetString(vm.regs[asm.R1])
if err != nil {
return err
}

s2, err := vm.getString(vm.regs[asm.R2])
s2, err := vm.GetString(vm.regs[asm.R2])
if err != nil {
return err
}
Expand All @@ -135,12 +135,12 @@ func FnMemCmpImpl(vm *VM, inst *asm.Instruction) error {

size := vm.regs[asm.R3]

b1, err := vm.getBytes(vm.regs[asm.R1], uint64(size))
b1, err := vm.GetBytes(vm.regs[asm.R1], uint64(size))
if err != nil {
return err
}

b2, err := vm.getBytes(vm.regs[asm.R2], uint64(size))
b2, err := vm.GetBytes(vm.regs[asm.R2], uint64(size))
if err != nil {
return err
}
Expand All @@ -157,20 +157,20 @@ func FnMemCpyImpl(vm *VM, inst *asm.Instruction) error {

size := vm.regs[asm.R3]

srcBytes, err := vm.getBytes(vm.regs[asm.R2], size)
srcBytes, err := vm.GetBytes(vm.regs[asm.R2], size)
if err != nil {
return err
}

return vm.setBytes(vm.regs[asm.R1], srcBytes, size)
return vm.SetBytes(vm.regs[asm.R1], srcBytes, size)
}

var (
reFmt = regexp.MustCompile("(%[^%])")
)

func FnTracePrintkImpl(vm *VM, inst *asm.Instruction) error {
format, err := vm.getString(vm.regs[asm.R1])
format, err := vm.GetString(vm.regs[asm.R1])
if err != nil {
return err
}
Expand Down Expand Up @@ -200,7 +200,7 @@ func FnTracePrintkImpl(vm *VM, inst *asm.Instruction) error {

var value interface{}
if ph == "%s" {
value, err = vm.getString(vm.regs[reg])
value, err = vm.GetString(vm.regs[reg])
if err != nil {
return err
}
Expand All @@ -221,12 +221,12 @@ func FnTracePrintkImpl(vm *VM, inst *asm.Instruction) error {
func FnProbeReadImpl(vm *VM, inst *asm.Instruction) error {
size := vm.regs[asm.R2]

srcBytes, err := vm.getBytes(vm.regs[asm.R3], size)
srcBytes, err := vm.GetBytes(vm.regs[asm.R3], size)
if err != nil {
return err
}

dstBytes, err := vm.getBytes(vm.regs[asm.R1], size)
dstBytes, err := vm.GetBytes(vm.regs[asm.R1], size)
if err != nil {
return err
}
Expand All @@ -239,12 +239,12 @@ func FnProbeReadImpl(vm *VM, inst *asm.Instruction) error {
func FnProbeReadStrImpl(vm *VM, inst *asm.Instruction) error {
size := vm.regs[asm.R2]

src, err := vm.getString(vm.regs[asm.R3])
src, err := vm.GetString(vm.regs[asm.R3])
if err != nil {
return err
}

dstBytes, err := vm.getBytes(vm.regs[asm.R1], size)
dstBytes, err := vm.GetBytes(vm.regs[asm.R1], size)
if err != nil {
return err
}
Expand Down Expand Up @@ -292,7 +292,7 @@ func FnMapLookupElemImpl(vm *VM, inst *asm.Instruction) error {
}

keyAddr := vm.regs[asm.R2]
key, err := vm.getBytes(keyAddr, uint64(_map.KeySize()))
key, err := vm.GetBytes(keyAddr, uint64(_map.KeySize()))
if err != nil {
return err
}
Expand All @@ -313,13 +313,13 @@ func FnMapUpdateElemImpl(vm *VM, inst *asm.Instruction) error {
}

keyAddr := vm.regs[asm.R2]
key, err := vm.getBytes(keyAddr, uint64(_map.KeySize()))
key, err := vm.GetBytes(keyAddr, uint64(_map.KeySize()))
if err != nil {
return err
}

valueAddr := vm.regs[asm.R3]
value, err := vm.getBytes(valueAddr, uint64(_map.ValueSize()))
value, err := vm.GetBytes(valueAddr, uint64(_map.ValueSize()))
if err != nil {
return err
}
Expand All @@ -342,7 +342,7 @@ func FnMapDeleteElemImpl(vm *VM, inst *asm.Instruction) error {
}

keyAddr := vm.regs[asm.R2]
key, err := vm.getBytes(keyAddr, uint64(_map.KeySize()))
key, err := vm.GetBytes(keyAddr, uint64(_map.KeySize()))
if err != nil {
return err
}
Expand All @@ -369,7 +369,7 @@ func FnPerfEventOutputImpl(vm *VM, inst *asm.Instruction) error {
size := vm.regs[asm.R5]

eventAddr := vm.regs[asm.R4]
data, err := vm.getBytes(eventAddr, size)
data, err := vm.GetBytes(eventAddr, size)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 0cca050

Please sign in to comment.