Skip to content
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
11 changes: 11 additions & 0 deletions interpreter/loaderinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"go.opentelemetry.io/ebpf-profiler/host"
"go.opentelemetry.io/ebpf-profiler/libpf"
"go.opentelemetry.io/ebpf-profiler/libpf/pfelf"
"go.opentelemetry.io/ebpf-profiler/process"
"go.opentelemetry.io/ebpf-profiler/util"
)

Expand Down Expand Up @@ -68,3 +69,13 @@ func (i *LoaderInfo) FileName() string {
func (i *LoaderInfo) Gaps() []util.Range {
return i.gaps
}

// ExtractAsFile returns a filename referring to the ELF executable, extracting
// it from a backing archive if needed.
func (i *LoaderInfo) ExtractAsFile() (string, error) {
if pr, ok := i.elfRef.ELFOpener.(process.Process); ok {
return pr.ExtractAsFile(i.FileName())
}
return "", fmt.Errorf("unable to open main executable '%v' due to wrong interface type",
i.FileName())
}
6 changes: 6 additions & 0 deletions process/coredump.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,12 @@ func (cd *CoredumpProcess) OpenELF(path string) (*pfelf.File, error) {
return nil, fmt.Errorf("ELF file `%s` not found", path)
}

// ExtractAsFile implements the Process interface
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// ExtractAsFile implements the Process interface
// ExtractAsFile implements the Process interface.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This follows style of the file. Would it make sense to fixup the whole file in a follow up PR?

func (cd *CoredumpProcess) ExtractAsFile(_ string) (string, error) {
// No filesystem level backing file in coredumps
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// No filesystem level backing file in coredumps
// There is no filesystem level backing file in coredumps.

return "", errors.New("coredump does not support opening backing file")
}

// getFile returns (creating if needed) a matching CoredumpFile for given file name
func (cd *CoredumpProcess) getFile(name string) *CoredumpFile {
if cf, ok := cd.files[name]; ok {
Expand Down
4 changes: 4 additions & 0 deletions process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,7 @@ func (sp *systemProcess) OpenELF(file string) (*pfelf.File, error) {
// Fall back to opening the file using the process specific root
return pfelf.Open(fmt.Sprintf("/proc/%v/root/%s", sp.pid, file))
}

func (sp *systemProcess) ExtractAsFile(file string) (string, error) {
return fmt.Sprintf("/proc/%v/root/%s", sp.pid, file), nil
}
7 changes: 7 additions & 0 deletions process/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ type Process interface {
// CalculateMappingFileID calculates FileID of the backing file
CalculateMappingFileID(*Mapping) (libpf.FileID, error)

// ExtractAsFile returns a filename suitable for opening the given file from
// the target process namespace. This is a last resort method to access the file
// when the ReaderAt interface from OpenMappingFile is not sufficient. The returned
// filename may refer to /proc or be a temporarily file, and it must not be modified
// or deleted.
ExtractAsFile(string) (string, error)

io.Closer

pfelf.ELFOpener
Expand Down
4 changes: 4 additions & 0 deletions processmanager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ func (d *dummyProcess) OpenELF(name string) (*pfelf.File, error) {
return pfelf.Open(name)
}

func (d *dummyProcess) ExtractAsFile(name string) (string, error) {
return name, nil
}

func (d *dummyProcess) Close() error {
return nil
}
Expand Down
10 changes: 10 additions & 0 deletions tools/coredump/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ func (tc *trackedCoredump) OpenELF(fileName string) (*pfelf.File, error) {
return tc.CoredumpProcess.OpenELF(fileName)
}

func (tc *trackedCoredump) ExtractAsFile(fileName string) (string, error) {
prefixedFileName := tc.prefix + fileName
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, we should use path.Join here and at other places, as tc.prefix is a path part.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also then fix the whole in follow up PR if ok?

if _, err := os.Stat(prefixedFileName); err != nil {
tc.warnMissing(fileName)
return "", err
}
tc.seen[fileName] = libpf.Void{}
return prefixedFileName, nil
}

func newNewCmd(store *modulestore.Store) *ffcli.Command {
args := &newCmd{store: store}

Expand Down
38 changes: 34 additions & 4 deletions tools/coredump/storecoredump.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import (
type StoreCoredump struct {
*process.CoredumpProcess

store *modulestore.Store
modules map[string]ModuleInfo
store *modulestore.Store
modules map[string]ModuleInfo
tempFiles map[string]string
}

var _ pfelf.ELFOpener = &StoreCoredump{}
Expand Down Expand Up @@ -60,6 +61,34 @@ func (scd *StoreCoredump) OpenELF(path string) (*pfelf.File, error) {
return scd.CoredumpProcess.OpenELF(path)
}

func (scd *StoreCoredump) ExtractAsFile(file string) (string, error) {
info, ok := scd.modules[file]
if !ok {
return "", os.ErrNotExist
}

f, err := os.CreateTemp("", "ebpf-profiler-coredump.*")
if err != nil {
return "", err
}
tmpFile := f.Name()
f.Close()

if err := scd.store.UnpackModuleToPath(info.Ref, tmpFile); err != nil {
os.Remove(tmpFile)
return "", err
}
scd.tempFiles[file] = tmpFile
return tmpFile, nil
}

func (scd *StoreCoredump) Close() error {
for _, tmpFile := range scd.tempFiles {
os.Remove(tmpFile)
}
return scd.CoredumpProcess.Close()
}

func OpenStoreCoredump(store *modulestore.Store, coreFileRef modulestore.ID, modules []ModuleInfo) (
process.Process, error) {
// Open the coredump from the module store.
Expand All @@ -84,7 +113,8 @@ func OpenStoreCoredump(store *modulestore.Store, coreFileRef modulestore.ID, mod
return &StoreCoredump{
CoredumpProcess: core,

store: store,
modules: moduleMap,
store: store,
modules: moduleMap,
tempFiles: make(map[string]string),
}, nil
}