Skip to content

Commit

Permalink
variable: return *btf.Var from Variable{Spec}.Type()
Browse files Browse the repository at this point in the history
In the initial design, we decided to return the inner type from .Type(), but
I forgot the outer btf.Var was needed to access decl tags set on the variable.

Returning a *Var here is the most convenient way of accessing the tags, otherwise
the caller would need to find the underlying map and parse the datasec manually,
which goes against what this API aims to solve.

Instead of the initial idea of returning a btf.Var as btf.Type, this now returns
btf.Var directly.

Signed-off-by: Timo Beckers <[email protected]>
  • Loading branch information
ti-mo committed Nov 14, 2024
1 parent cfd8515 commit 2ce8a41
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion elf_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,7 @@ func (ec *elfCode) loadDataSections() error {
return fmt.Errorf("data section %s: variable %s size in datasec (%d) doesn't match ELF symbol size (%d)", sec.Name, name, v.Size, ev.size)
}

ev.t = vt.Type
ev.t = vt
}
}
}
Expand Down
22 changes: 15 additions & 7 deletions variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type VariableSpec struct {
size uint64

m *MapSpec
t btf.Type
t *btf.Var
}

// Set sets the value of the VariableSpec to the provided input using the host's
Expand Down Expand Up @@ -81,8 +81,12 @@ func (s *VariableSpec) Constant() bool {
return s.m.readOnly()
}

// Type returns the BTF Type of the variable.
func (s *VariableSpec) Type() btf.Type {
// Type returns the [btf.Var] representing the variable in its data section.
// This is useful for inspecting the variable's decl tags and the type
// information of the inner type.
//
// Returns nil if the original ELF object did not contain BTF information.
func (s *VariableSpec) Type() *btf.Var {
return s.t
}

Expand Down Expand Up @@ -122,12 +126,12 @@ type Variable struct {
name string
offset uint64
size uint64
t btf.Type
t *btf.Var

mm *Memory
}

func newVariable(name string, offset, size uint64, t btf.Type, mm *Memory) (*Variable, error) {
func newVariable(name string, offset, size uint64, t *btf.Var, mm *Memory) (*Variable, error) {
if mm != nil {
if int(offset+size) > mm.Size() {
return nil, fmt.Errorf("offset %d(+%d) is out of bounds", offset, size)
Expand Down Expand Up @@ -159,8 +163,12 @@ func (v *Variable) ReadOnly() bool {
return v.mm.ReadOnly()
}

// Type returns the BTF Type of the variable.
func (v *Variable) Type() btf.Type {
// Type returns the [btf.Var] representing the variable in its data section.
// This is useful for inspecting the variable's decl tags and the type
// information of the inner type.
//
// Returns nil if the original ELF object did not contain BTF information.
func (v *Variable) Type() *btf.Var {
return v.t
}

Expand Down
2 changes: 1 addition & 1 deletion variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func TestVariable(t *testing.T) {

typ := obj.BSS.Type()
qt.Assert(t, qt.IsNotNil(typ))
i, ok := btf.As[*btf.Int](typ)
i, ok := btf.As[*btf.Int](typ.Type)
qt.Assert(t, qt.IsTrue(ok))
qt.Assert(t, qt.Equals(i.Size, 4))

Expand Down

0 comments on commit 2ce8a41

Please sign in to comment.