Skip to content

Commit

Permalink
Adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nickschuch committed Nov 24, 2024
1 parent c322c30 commit 548dcce
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 2 deletions.
55 changes: 55 additions & 0 deletions collector/scripts/bpftmpl/elf/notes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package elf

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestReadNotes(t *testing.T) {
input := `Displaying notes found in: .note.gnu.build-id
Owner Data size Description
GNU 0x00000014 NT_GNU_BUILD_ID (unique build ID bitstring)
Build ID: 30d4ef247fb97a94101adda2291644ccdcb8cc77
Displaying notes found in: .note.stapsdt
Owner Data size Description
stapsdt 0x00000045 NT_STAPSDT (SystemTap probe descriptors)
Provider: compass
Name: request_shutdown
Location: 0x000000000000cfa8, Base: 0x0000000000056537, Semaphore: 0x0000000000000000
Arguments: -8@x19 -8@x20 -8@x0
stapsdt 0x0000004f NT_STAPSDT (SystemTap probe descriptors)
Provider: compass
Name: php_function
Location: 0x000000000000e794, Base: 0x0000000000056537, Semaphore: 0x0000000000000000
Arguments: -8@x19 -8@x20 -8@x0 -8@x23 -8@x24`

have, err := ReadNotes(input)
assert.NoError(t, err)

want := []SystemTapNote{
{
Provider: "compass",
Name: "request_shutdown",
Args: []string{
"-8@x19",
"-8@x20",
"-8@x0",
},
},
{
Provider: "compass",
Name: "php_function",
Args: []string{
"-8@x19",
"-8@x20",
"-8@x0",
"-8@x23",
"-8@x24",
},
},
}

assert.Equal(t, want, have)
}
3 changes: 1 addition & 2 deletions collector/scripts/bpftmpl/replace/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,12 @@ func UsingNotes(arch string, notes []elf.SystemTapNote, program string) (string,
func getValueFunc(arch string) (func(string) string, error) {
if arch == "arm64" {
return func(argument string) string {
// @todo, Determine if there are any other prefixes other than "-8@x"
return fmt.Sprintf("regs[%s]", strings.TrimLeft(argument, "-8@x"))
}, nil
}

if arch == "amd64" {
return func(argument string) string {
// @todo, Determine if there are any other prefixes other than "-8@%"
switch argument {
case "-8@%rax":
return "ax"
Expand All @@ -80,6 +78,7 @@ func getValueFunc(arch string) (func(string) string, error) {
case "-8@%rbp":
return "bp"
default:
// Preserve the "r" in the remaining eg. r15.
return strings.TrimLeft(argument, "-8@%")
}
}, nil
Expand Down
90 changes: 90 additions & 0 deletions collector/scripts/bpftmpl/replace/replace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package replace

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"

"github.com/skpr/compass/collector/scripts/bpftmpl/elf"
)

func TestUsingNotesAmd64(t *testing.T) {
notes := []elf.SystemTapNote{
{
Provider: "compass",
Name: "request_shutdown",
Args: []string{
"-8@%rbx",
"-8@%r14",
"-8@%rdi",
},
},
{
Provider: "compass",
Name: "php_function",
Args: []string{
"-8@%rbx",
"-8@%r14",
"-8@%rax",
"-8@%r13",
"-8@%rbp",
},
},
}

replacements := []string{
"PHP_FUNCTION_ARG_FUNCTION_NAME",
"PHP_FUNCTION_ARG_CLASS_NAME",
"PHP_FUNCTION_ARG_FUNCTION_NAME",
"PHP_FUNCTION_ARG_START_TIME",
"PHP_FUNCTION_ARG_END_TIME",
"REQUEST_SHUTDOWN_ARG_REQUEST_ID",
"REQUEST_SHUTDOWN_ARG_URI",
"REQUEST_SHUTDOWN_ARG_URI",
}

program, err := UsingNotes("amd64", notes, strings.Join(replacements, ","))
assert.NoError(t, err)
assert.Equal(t, "ax,r14,ax,r13,bp,bx,r14,r14", program)
}

func TestUsingNotesArm64(t *testing.T) {
notes := []elf.SystemTapNote{
{
Provider: "compass",
Name: "request_shutdown",
Args: []string{
"-8@x19",
"-8@x20",
"-8@x0",
},
},
{
Provider: "compass",
Name: "php_function",
Args: []string{
"-8@x19",
"-8@x20",
"-8@x0",
"-8@x23",
"-8@x24",
},
},
}

replacements := []string{
"PHP_FUNCTION_ARG_FUNCTION_NAME",
"PHP_FUNCTION_ARG_CLASS_NAME",
"PHP_FUNCTION_ARG_FUNCTION_NAME",
"PHP_FUNCTION_ARG_START_TIME",
"PHP_FUNCTION_ARG_END_TIME",
"REQUEST_SHUTDOWN_ARG_REQUEST_ID",
"REQUEST_SHUTDOWN_ARG_URI",
"REQUEST_SHUTDOWN_ARG_URI",
}

program, err := UsingNotes("arm64", notes, strings.Join(replacements, ","))
assert.NoError(t, err)
assert.Equal(t, "regs[0],regs[20],regs[0],regs[23],regs[24],regs[19],regs[20],regs[20]", program)
}

0 comments on commit 548dcce

Please sign in to comment.