Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions cmd/bpf2go/gen/output.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,34 @@ import (
{{ end }}
{{- end }}

{{- if or .Maps (or .Variables .Programs) }}
// Constant names for all maps, variables, and programs as defined in the ELF file.
//
// They can be passed to ebpf.CollectionSpec and ebpf.Collection map fields.
const (
{{- if .Maps }}
// {{ .Name }} map names.
{{- range $name, $id := .Maps }}
{{ $.Name }}MapName{{ $id }} = "{{ $name }}"
{{- end }}
{{- end }}

{{- if .Variables }}
// {{ .Name }} variable names.
{{- range $name, $id := .Variables }}
{{ $.Name }}VariableName{{ $id }} = "{{ $name }}"
{{- end }}
{{- end }}

{{- if .Programs }}
// {{ .Name }} program names.
{{- range $name, $id := .Programs }}
{{ $.Name }}ProgramName{{ $id }} = "{{ $name }}"
{{- end }}
{{- end }}
)
{{- end }}

// {{ .Name.Load }} returns the embedded CollectionSpec for {{ .Name }}.
func {{ .Name.Load }}() (*ebpf.CollectionSpec, error) {
reader := bytes.NewReader({{ .Name.Bytes }})
Expand Down
6 changes: 5 additions & 1 deletion cmd/bpf2go/gen/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestCustomIdentifier(t *testing.T) {
qt.Assert(t, qt.StringContains(buf.String(), "DO_THING"))
}

func TestObjects(t *testing.T) {
func TestObjectsAndConstants(t *testing.T) {
var buf bytes.Buffer
args := GenerateArgs{
Package: "foo",
Expand All @@ -117,6 +117,10 @@ func TestObjects(t *testing.T) {
qt.Assert(t, qt.StringContains(str, "Map1 *ebpf.Map `ebpf:\"map1\"`"))
qt.Assert(t, qt.StringContains(str, "Var1 *ebpf.Variable `ebpf:\"var_1\"`"))
qt.Assert(t, qt.StringContains(str, "ProgFoo1 *ebpf.Program `ebpf:\"prog_foo_1\"`"))

qt.Assert(t, qt.StringContains(str, "barMapNameMap1 = \"map1\""))
qt.Assert(t, qt.StringContains(str, "barVariableNameVar1 = \"var_1\""))
qt.Assert(t, qt.StringContains(str, "barProgramNameProgFoo1 = \"prog_foo_1\""))
}

func TestGenerateStructTypes(t *testing.T) {
Expand Down