Skip to content

Commit

Permalink
Fix minor issues
Browse files Browse the repository at this point in the history
Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
  • Loading branch information
kakkoyun committed Mar 2, 2022
1 parent 081486b commit 5f20aeb
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 24 deletions.
3 changes: 2 additions & 1 deletion deploy/dev.jsonnet
Expand Up @@ -25,7 +25,8 @@ function(serverVersion='v0.4.2')
logLevel: 'debug',
insecure: true,
insecureSkipVerify: true,
tempDir: '/tmp'
tempDir: '/tmp',
// podLabelSelector: 'app.kubernetes.io/name=parca',
});

{
Expand Down
7 changes: 2 additions & 5 deletions pkg/maps/mapping.go
Expand Up @@ -56,11 +56,8 @@ func (m *Mapping) AllMappings() ([]*profile.Mapping, []*objectfile.ObjectFile) {
maps := m.pidMappings[pid]
for _, mapping := range maps {
if mapping.BuildID != "" {
objFiles = append(objFiles, &objectfile.ObjectFile{
PID: pid,
File: mapping.File,
BuildID: mapping.BuildID,
})
// TODO(kakkoyun): Use objectfile.FromProcess() and add cache!
objFiles = append(objFiles, objectfile.NewObjectFile(pid, mapping))
}
// TODO(brancz): Do we need to handle potentially duplicate
// vdso/vsyscall mappings?
Expand Down
44 changes: 30 additions & 14 deletions pkg/objectfile/object_file.go
Expand Up @@ -73,15 +73,15 @@ func Open(filePath string, m *profile.Mapping) (*ObjectFile, error) {
return nil, fmt.Errorf("unrecognized binary format: %s", filePath)
}

func open(path string, start, limit, offset uint64, relocationSymbol string) (*ObjectFile, error) {
ef, err := elfOpen(path)
func open(filePath string, start, limit, offset uint64, relocationSymbol string) (*ObjectFile, error) {
ef, err := elfOpen(filePath)
if err != nil {
return nil, fmt.Errorf("error parsing %s: %v", path, err)
return nil, fmt.Errorf("error parsing %s: %v", filePath, err)
}
defer ef.Close()

buildID := ""
if f, err := os.Open(path); err == nil {
if f, err := os.Open(filePath); err == nil {
if id, err := elfexec.GetBuildID(f); err == nil {
buildID = fmt.Sprintf("%x", id)
}
Expand All @@ -91,7 +91,7 @@ func open(path string, start, limit, offset uint64, relocationSymbol string) (*O
kernelOffset *uint64
pageAligned = func(addr uint64) bool { return addr%4096 == 0 }
)
if strings.Contains(path, "vmlinux") || !pageAligned(start) || !pageAligned(limit) || !pageAligned(offset) {
if strings.Contains(filePath, "vmlinux") || !pageAligned(start) || !pageAligned(limit) || !pageAligned(offset) {
// Reading all Symbols is expensive, and we only rarely need it so
// we don't want to do it every time. But if _stext happens to be
// page-aligned but isn't the same as Vaddr, we would symbolize
Expand Down Expand Up @@ -126,22 +126,38 @@ func open(path string, start, limit, offset uint64, relocationSymbol string) (*O
// correctly identify the associated program segment that is needed to compute
// the base.
if _, err := elfexec.GetBase(&ef.FileHeader, elfexec.FindTextProgHeader(ef), kernelOffset, start, limit, offset); err != nil {
return nil, fmt.Errorf("could not identify base for %s: %v", path, err)
}
return &ObjectFile{BuildID: buildID, m: &mapping{
start: start,
limit: limit,
offset: offset,
kernelOffset: kernelOffset,
}}, nil
return nil, fmt.Errorf("could not identify base for %s: %v", filePath, err)
}
return &ObjectFile{
BuildID: buildID,
path: filePath,
m: &mapping{
start: start,
limit: limit,
offset: offset,
kernelOffset: kernelOffset,
},
}, nil
}

func NewObjectFile(pid uint32, m *profile.Mapping) *ObjectFile {
return &ObjectFile{
PID: pid,
File: m.File,
BuildID: m.BuildID,
m: &mapping{
start: m.Start,
limit: m.Limit,
offset: m.Offset,
},
}
}

type ObjectFile struct {
PID uint32
File string
BuildID string

// Only for testing.
path string

// Ensures the base, baseErr and isData are computed once.
Expand Down
14 changes: 10 additions & 4 deletions pkg/profiler/profiler.go
Expand Up @@ -447,9 +447,12 @@ func (p *CgroupProfiler) profileLoop(ctx context.Context, captureTime time.Time)
level.Debug(p.logger).Log("msg", "failed to get mapping", "err", err)
}

objFile, err := p.objCache.ObjectFileForProcess(pid, m)
if err != nil {
level.Debug(p.logger).Log("msg", "failed to open object file", "err", err)
var objFile *objectfile.ObjectFile
if m != nil {
objFile, err = p.objCache.ObjectFileForProcess(pid, m)
if err != nil {
level.Debug(p.logger).Log("msg", "failed to open object file", "err", err)
}
}

// Transform the address by normalizing OS offsets.
Expand All @@ -458,8 +461,11 @@ func (p *CgroupProfiler) profileLoop(ctx context.Context, captureTime time.Time)
nAddr, err := objFile.ObjAddr(addr)
if err != nil {
level.Debug(p.logger).Log("msg", "failed to get normalized address from object file", "err", err)
} else {
// TODO(kakkoyun): Remove!
level.Debug(p.logger).Log("msg", "transforming address", "pid", pid, "addr", addr, "normalizedAddr", normalizedAddr)
normalizedAddr = nAddr
}
normalizedAddr = nAddr
}
l := &profile.Location{
ID: uint64(locationIndex + 1),
Expand Down

0 comments on commit 5f20aeb

Please sign in to comment.