Skip to content

Commit 548dcce

Browse files
committed
Adds tests
1 parent c322c30 commit 548dcce

File tree

3 files changed

+146
-2
lines changed

3 files changed

+146
-2
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package elf
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestReadNotes(t *testing.T) {
10+
input := `Displaying notes found in: .note.gnu.build-id
11+
Owner Data size Description
12+
GNU 0x00000014 NT_GNU_BUILD_ID (unique build ID bitstring)
13+
Build ID: 30d4ef247fb97a94101adda2291644ccdcb8cc77
14+
15+
Displaying notes found in: .note.stapsdt
16+
Owner Data size Description
17+
stapsdt 0x00000045 NT_STAPSDT (SystemTap probe descriptors)
18+
Provider: compass
19+
Name: request_shutdown
20+
Location: 0x000000000000cfa8, Base: 0x0000000000056537, Semaphore: 0x0000000000000000
21+
Arguments: -8@x19 -8@x20 -8@x0
22+
stapsdt 0x0000004f NT_STAPSDT (SystemTap probe descriptors)
23+
Provider: compass
24+
Name: php_function
25+
Location: 0x000000000000e794, Base: 0x0000000000056537, Semaphore: 0x0000000000000000
26+
Arguments: -8@x19 -8@x20 -8@x0 -8@x23 -8@x24`
27+
28+
have, err := ReadNotes(input)
29+
assert.NoError(t, err)
30+
31+
want := []SystemTapNote{
32+
{
33+
Provider: "compass",
34+
Name: "request_shutdown",
35+
Args: []string{
36+
"-8@x19",
37+
"-8@x20",
38+
"-8@x0",
39+
},
40+
},
41+
{
42+
Provider: "compass",
43+
Name: "php_function",
44+
Args: []string{
45+
"-8@x19",
46+
"-8@x20",
47+
"-8@x0",
48+
"-8@x23",
49+
"-8@x24",
50+
},
51+
},
52+
}
53+
54+
assert.Equal(t, want, have)
55+
}

collector/scripts/bpftmpl/replace/replace.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,12 @@ func UsingNotes(arch string, notes []elf.SystemTapNote, program string) (string,
5858
func getValueFunc(arch string) (func(string) string, error) {
5959
if arch == "arm64" {
6060
return func(argument string) string {
61-
// @todo, Determine if there are any other prefixes other than "-8@x"
6261
return fmt.Sprintf("regs[%s]", strings.TrimLeft(argument, "-8@x"))
6362
}, nil
6463
}
6564

6665
if arch == "amd64" {
6766
return func(argument string) string {
68-
// @todo, Determine if there are any other prefixes other than "-8@%"
6967
switch argument {
7068
case "-8@%rax":
7169
return "ax"
@@ -80,6 +78,7 @@ func getValueFunc(arch string) (func(string) string, error) {
8078
case "-8@%rbp":
8179
return "bp"
8280
default:
81+
// Preserve the "r" in the remaining eg. r15.
8382
return strings.TrimLeft(argument, "-8@%")
8483
}
8584
}, nil
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package replace
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
9+
"github.com/skpr/compass/collector/scripts/bpftmpl/elf"
10+
)
11+
12+
func TestUsingNotesAmd64(t *testing.T) {
13+
notes := []elf.SystemTapNote{
14+
{
15+
Provider: "compass",
16+
Name: "request_shutdown",
17+
Args: []string{
18+
"-8@%rbx",
19+
"-8@%r14",
20+
"-8@%rdi",
21+
},
22+
},
23+
{
24+
Provider: "compass",
25+
Name: "php_function",
26+
Args: []string{
27+
"-8@%rbx",
28+
"-8@%r14",
29+
"-8@%rax",
30+
"-8@%r13",
31+
"-8@%rbp",
32+
},
33+
},
34+
}
35+
36+
replacements := []string{
37+
"PHP_FUNCTION_ARG_FUNCTION_NAME",
38+
"PHP_FUNCTION_ARG_CLASS_NAME",
39+
"PHP_FUNCTION_ARG_FUNCTION_NAME",
40+
"PHP_FUNCTION_ARG_START_TIME",
41+
"PHP_FUNCTION_ARG_END_TIME",
42+
"REQUEST_SHUTDOWN_ARG_REQUEST_ID",
43+
"REQUEST_SHUTDOWN_ARG_URI",
44+
"REQUEST_SHUTDOWN_ARG_URI",
45+
}
46+
47+
program, err := UsingNotes("amd64", notes, strings.Join(replacements, ","))
48+
assert.NoError(t, err)
49+
assert.Equal(t, "ax,r14,ax,r13,bp,bx,r14,r14", program)
50+
}
51+
52+
func TestUsingNotesArm64(t *testing.T) {
53+
notes := []elf.SystemTapNote{
54+
{
55+
Provider: "compass",
56+
Name: "request_shutdown",
57+
Args: []string{
58+
"-8@x19",
59+
"-8@x20",
60+
"-8@x0",
61+
},
62+
},
63+
{
64+
Provider: "compass",
65+
Name: "php_function",
66+
Args: []string{
67+
"-8@x19",
68+
"-8@x20",
69+
"-8@x0",
70+
"-8@x23",
71+
"-8@x24",
72+
},
73+
},
74+
}
75+
76+
replacements := []string{
77+
"PHP_FUNCTION_ARG_FUNCTION_NAME",
78+
"PHP_FUNCTION_ARG_CLASS_NAME",
79+
"PHP_FUNCTION_ARG_FUNCTION_NAME",
80+
"PHP_FUNCTION_ARG_START_TIME",
81+
"PHP_FUNCTION_ARG_END_TIME",
82+
"REQUEST_SHUTDOWN_ARG_REQUEST_ID",
83+
"REQUEST_SHUTDOWN_ARG_URI",
84+
"REQUEST_SHUTDOWN_ARG_URI",
85+
}
86+
87+
program, err := UsingNotes("arm64", notes, strings.Join(replacements, ","))
88+
assert.NoError(t, err)
89+
assert.Equal(t, "regs[0],regs[20],regs[0],regs[23],regs[24],regs[19],regs[20],regs[20]", program)
90+
}

0 commit comments

Comments
 (0)