Skip to content

Commit

Permalink
feat: support stack args when args count is great than 9
Browse files Browse the repository at this point in the history
  • Loading branch information
jim3ma committed Dec 9, 2024
1 parent afbb698 commit 9f68248
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,12 @@ var regs = [...]string{"ax", "bx", "cx", "di", "si", "r8", "r9", "r10", "r11"}
// is in use
func (t Target) Arg(i int) string {
if t.RegsABI {
if i >= len(regs) {
return fmt.Sprintf(`*(reg("sp") + %d)`, 8*(i-8))
}
// rax, rbx, rcx, rdi, rsi, r8, r9, r10, r11 should do
if i < 0 || i >= len(regs) {
// Refer: https://go.googlesource.com/go/+/refs/heads/dev.regabi/src/cmd/compile/internal-abi.md#amd64-architecture
if i < 0 {
panic("argument out of bounds. roll your own")
}
return fmt.Sprintf("reg(\"%s\")", regs[i])
Expand All @@ -78,16 +82,25 @@ func (t Target) Arg(i int) string {

// RetArg maps return argument indices to bpftrace built-ins taking into account which ABI
// is in use
func (t Target) RetArg(i, offset int) string {
func (t Target) RetArg(i, startOffset int) string {
if t.RegsABI {
// ret args is on stack
if i >= len(regs) {
// calc offset of args
var argsOffset int
if startOffset >= len(regs) {
argsOffset = 8 * (startOffset - 8)
}
return fmt.Sprintf(`*(reg("sp") + %d)`, 8*(i-8)+argsOffset)
}
// rax, rbx, rcx, rdi, rsi, r8, r9, r10, r11 should do
if i < 0 || i >= len(regs) {
panic("argument out of bounds. roll your own")
if i < 0 {
panic("return argument out of bounds. roll your own")
}
return fmt.Sprintf("reg(\"%s\")", regs[i])
}

return fmt.Sprintf(`*(reg("sp") + %d)`, 8*(i+1+offset))
return fmt.Sprintf(`*(reg("sp") + %d)`, 8*(i+1+startOffset))
}

func NewTarget(exe string, arguments func(string) []string) (*Target, error) {
Expand Down

0 comments on commit 9f68248

Please sign in to comment.