Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

btf: Add decl tags for functions #1550

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 19 additions & 1 deletion btf/ext_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func AssignMetadataToInstructions(
//
// If an instruction has an [asm.Comment], it will be synthesized into a mostly
// empty line info.
func MarshalExtInfos(insns asm.Instructions, b *Builder) (funcInfos, lineInfos []byte, _ error) {
func MarshalExtInfos(insns asm.Instructions, b *Builder, types *Spec) (funcInfos, lineInfos []byte, _ error) {
iter := insns.Iterate()
for iter.Next() {
if iter.Ins.Source() != nil || FuncMetadata(iter.Ins) != nil {
Expand All @@ -166,6 +166,24 @@ marshal:
if err := fi.marshal(&fiBuf, b); err != nil {
return nil, nil, fmt.Errorf("write func info: %w", err)
}
if types != nil {
iter := types.Iterate()
for iter.Next() {
if t, ok := iter.Type.(*declTag); ok {
tfn, ok := t.Type.(*Func)
if !ok {
continue
}
if fn.Name != tfn.Name {
continue
}
_, err := b.Add(&declTag{fn, t.Value, t.Index})
if err != nil {
return nil, nil, fmt.Errorf("write decl tag: %w", err)
}
}
}
}
}

if source := iter.Ins.Source(); source != nil {
Expand Down
2 changes: 1 addition & 1 deletion collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ func (cl *collectionLoader) loadProgram(progName string) (*Program, error) {
}
}

prog, err := newProgramWithOptions(progSpec, cl.opts.Programs)
prog, err := newProgramWithOptions(progSpec, cl.opts.Programs, cl.coll.Types)
if err != nil {
return nil, fmt.Errorf("program %s: %w", progName, err)
}
Expand Down
6 changes: 3 additions & 3 deletions prog.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func NewProgramWithOptions(spec *ProgramSpec, opts ProgramOptions) (*Program, er
return nil, errors.New("can't load a program from a nil spec")
}

prog, err := newProgramWithOptions(spec, opts)
prog, err := newProgramWithOptions(spec, opts, nil)
if errors.Is(err, asm.ErrUnsatisfiedMapReference) {
return nil, fmt.Errorf("cannot load program without loading its whole collection: %w", err)
}
Expand All @@ -235,7 +235,7 @@ var (
kfuncBadCall = []byte(fmt.Sprintf("invalid func unknown#%d\n", kfuncCallPoisonBase))
)

func newProgramWithOptions(spec *ProgramSpec, opts ProgramOptions) (*Program, error) {
func newProgramWithOptions(spec *ProgramSpec, opts ProgramOptions, types *btf.Spec) (*Program, error) {
if len(spec.Instructions) == 0 {
return nil, errors.New("instructions cannot be empty")
}
Expand Down Expand Up @@ -307,7 +307,7 @@ func newProgramWithOptions(spec *ProgramSpec, opts ProgramOptions) (*Program, er
if errExtInfos == nil {
// Only add func and line info if the kernel supports it. This allows
// BPF compiled with modern toolchains to work on old kernels.
fib, lib, err := btf.MarshalExtInfos(insns, &b)
fib, lib, err := btf.MarshalExtInfos(insns, &b, types)
if err != nil {
return nil, fmt.Errorf("marshal ext_infos: %w", err)
}
Expand Down
Loading