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

info: expose more prog jited info #1598

Merged
merged 5 commits into from
Dec 4, 2024
Merged
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
71 changes: 35 additions & 36 deletions btf/ext_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
type ExtInfos struct {
// The slices are sorted by offset in ascending order.
funcInfos map[string]FuncOffsets
lineInfos map[string]LineInfos
lineInfos map[string]LineOffsets
relocationInfos map[string]CORERelocationInfos
}

Expand Down Expand Up @@ -72,7 +72,7 @@ func loadExtInfos(r io.ReaderAt, bo binary.ByteOrder, spec *Spec) (*ExtInfos, er
return nil, fmt.Errorf("parsing BTF line info: %w", err)
}

lineInfos := make(map[string]LineInfos, len(btfLineInfos))
lineInfos := make(map[string]LineOffsets, len(btfLineInfos))
for section, blis := range btfLineInfos {
lineInfos[section], err = newLineInfos(blis, spec.strings)
if err != nil {
Expand Down Expand Up @@ -102,8 +102,10 @@ func loadExtInfos(r io.ReaderAt, bo binary.ByteOrder, spec *Spec) (*ExtInfos, er
return &ExtInfos{funcInfos, lineInfos, coreRelos}, nil
}

type funcInfoMeta struct{}
type coreRelocationMeta struct{}
type (
funcInfoMeta struct{}
coreRelocationMeta struct{}
)

// Assign per-section metadata from BTF to a section's instructions.
func (ei *ExtInfos) Assign(insns asm.Instructions, section string) {
Expand All @@ -118,7 +120,7 @@ func (ei *ExtInfos) Assign(insns asm.Instructions, section string) {
func AssignMetadataToInstructions(
insns asm.Instructions,
funcInfos FuncOffsets,
lineInfos LineInfos,
lineInfos LineOffsets,
reloInfos CORERelocationInfos,
) {
iter := insns.Iterate()
Expand All @@ -128,9 +130,9 @@ func AssignMetadataToInstructions(
funcInfos = funcInfos[1:]
}

if len(lineInfos.infos) > 0 && lineInfos.infos[0].offset == iter.Offset {
*iter.Ins = iter.Ins.WithSource(lineInfos.infos[0].line)
lineInfos.infos = lineInfos.infos[1:]
if len(lineInfos) > 0 && lineInfos[0].Offset == iter.Offset {
*iter.Ins = iter.Ins.WithSource(lineInfos[0].Line)
lineInfos = lineInfos[1:]
}

if len(reloInfos.infos) > 0 && reloInfos.infos[0].offset == iter.Offset {
Expand Down Expand Up @@ -178,9 +180,9 @@ marshal:
}
}

li := &lineInfo{
line: line,
offset: iter.Offset,
li := &LineOffset{
Offset: iter.Offset,
Line: line,
}
if err := li.marshal(&liBuf, b); err != nil {
return nil, nil, fmt.Errorf("write line info: %w", err)
Expand Down Expand Up @@ -516,14 +518,13 @@ func (li *Line) String() string {
return li.line
}

// LineInfos contains a sorted list of line infos.
type LineInfos struct {
infos []lineInfo
}
// LineOffsets contains a sorted list of line infos.
type LineOffsets []LineOffset

type lineInfo struct {
line *Line
offset asm.RawInstructionOffset
// LineOffset represents a line info and its raw instruction offset.
type LineOffset struct {
Offset asm.RawInstructionOffset
Line *Line
}

// Constants for the format of bpfLineInfo.LineCol.
Expand All @@ -542,7 +543,7 @@ type bpfLineInfo struct {
}

// LoadLineInfos parses BTF line info in kernel wire format.
func LoadLineInfos(reader io.Reader, bo binary.ByteOrder, recordNum uint32, spec *Spec) (LineInfos, error) {
func LoadLineInfos(reader io.Reader, bo binary.ByteOrder, recordNum uint32, spec *Spec) (LineOffsets, error) {
lis, err := parseLineInfoRecords(
reader,
bo,
Expand All @@ -551,57 +552,55 @@ func LoadLineInfos(reader io.Reader, bo binary.ByteOrder, recordNum uint32, spec
false,
)
if err != nil {
return LineInfos{}, fmt.Errorf("parsing BTF line info: %w", err)
return LineOffsets{}, fmt.Errorf("parsing BTF line info: %w", err)
}

return newLineInfos(lis, spec.strings)
}

func newLineInfo(li bpfLineInfo, strings *stringTable) (lineInfo, error) {
func newLineInfo(li bpfLineInfo, strings *stringTable) (LineOffset, error) {
line, err := strings.Lookup(li.LineOff)
if err != nil {
return lineInfo{}, fmt.Errorf("lookup of line: %w", err)
return LineOffset{}, fmt.Errorf("lookup of line: %w", err)
}

fileName, err := strings.Lookup(li.FileNameOff)
if err != nil {
return lineInfo{}, fmt.Errorf("lookup of filename: %w", err)
return LineOffset{}, fmt.Errorf("lookup of filename: %w", err)
}

lineNumber := li.LineCol >> bpfLineShift
lineColumn := li.LineCol & bpfColumnMax

return lineInfo{
return LineOffset{
asm.RawInstructionOffset(li.InsnOff),
&Line{
fileName,
line,
lineNumber,
lineColumn,
},
asm.RawInstructionOffset(li.InsnOff),
}, nil
}

func newLineInfos(blis []bpfLineInfo, strings *stringTable) (LineInfos, error) {
lis := LineInfos{
infos: make([]lineInfo, 0, len(blis)),
}
func newLineInfos(blis []bpfLineInfo, strings *stringTable) (LineOffsets, error) {
lis := make([]LineOffset, 0, len(blis))
for _, bli := range blis {
li, err := newLineInfo(bli, strings)
if err != nil {
return LineInfos{}, fmt.Errorf("offset %d: %w", bli.InsnOff, err)
return LineOffsets{}, fmt.Errorf("offset %d: %w", bli.InsnOff, err)
}
lis.infos = append(lis.infos, li)
lis = append(lis, li)
}
sort.Slice(lis.infos, func(i, j int) bool {
return lis.infos[i].offset <= lis.infos[j].offset
sort.Slice(lis, func(i, j int) bool {
return lis[i].Offset <= lis[j].Offset
})
return lis, nil
}

// marshal writes the binary representation of the LineInfo to w.
func (li *lineInfo) marshal(w *bytes.Buffer, b *Builder) error {
line := li.line
func (li *LineOffset) marshal(w *bytes.Buffer, b *Builder) error {
line := li.Line
if line.lineNumber > bpfLineMax {
return fmt.Errorf("line %d exceeds %d", line.lineNumber, bpfLineMax)
}
Expand All @@ -621,7 +620,7 @@ func (li *lineInfo) marshal(w *bytes.Buffer, b *Builder) error {
}

bli := bpfLineInfo{
uint32(li.offset),
uint32(li.Offset),
fileNameOff,
lineOff,
(line.lineNumber << bpfLineShift) | line.lineColumn,
Expand Down
Loading
Loading