Skip to content

Commit 0d74bd6

Browse files
committed
feat: read full program name from function info, if available
Analogous to bpftool's get_prog_full_name function, query a program's full name (if possible) from the BTF info. Signed-off-by: Max Altgelt <[email protected]>
1 parent 0a9a910 commit 0d74bd6

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

info.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,44 @@ func newProgramInfoFromFd(fd *sys.FD) (*ProgramInfo, error) {
497497
}
498498
}
499499

500+
if len(pi.Name) == len(info.Name)-1 { // Possibly truncated, check BTF info for full name
501+
name, err := readNameFromFunc(&pi)
502+
if err == nil {
503+
pi.Name = name
504+
} // If an error occurs, keep the truncated name, which is better than none
505+
}
506+
500507
return &pi, nil
501508
}
502509

510+
func readNameFromFunc(pi *ProgramInfo) (string, error) {
511+
if pi.numFuncInfos == 0 {
512+
return "", errors.New("no function info")
513+
}
514+
515+
spec, err := pi.btfSpec()
516+
if err != nil {
517+
return "", err
518+
}
519+
520+
funcInfos, err := btf.LoadFuncInfos(
521+
bytes.NewReader(pi.funcInfos),
522+
internal.NativeEndian,
523+
pi.numFuncInfos,
524+
spec,
525+
)
526+
if err != nil {
527+
return "", err
528+
}
529+
530+
for _, funcInfo := range funcInfos {
531+
if funcInfo.Offset == 0 { // Information about the whole program
532+
return funcInfo.Func.Name, nil
533+
}
534+
}
535+
return "", errors.New("no function info about program")
536+
}
537+
503538
func readProgramInfoFromProc(fd *sys.FD, pi *ProgramInfo) error {
504539
var progType uint32
505540
err := scanFdInfo(fd, map[string]interface{}{

0 commit comments

Comments
 (0)