Skip to content

Commit 2a0f875

Browse files
authored
Replace Analyzer with NewInfo function (#1941)
* Add NewInfo to process pkg Replace the Analyze method of the Analyzer with the new function. * Rename process/analyze.go to info.go * Remove unneeded process Analyzer
1 parent 2bf72bb commit 2a0f875

File tree

4 files changed

+121
-128
lines changed

4 files changed

+121
-128
lines changed

internal/pkg/instrumentation/manager.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,8 @@ func NewManager(logger *slog.Logger, otelController *opentelemetry.Controller, p
7474
}
7575
}
7676

77-
pa := process.NewAnalyzer(logger, pid)
78-
7977
var err error
80-
m.proc, err = pa.Analyze(funcs)
78+
m.proc, err = process.NewInfo(pid, funcs)
8179
if err != nil {
8280
return nil, err
8381
}

internal/pkg/process/analyze.go

-106
This file was deleted.

internal/pkg/process/discover.go

-19
This file was deleted.

internal/pkg/process/info.go

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package process
5+
6+
import (
7+
"debug/elf"
8+
"errors"
9+
"fmt"
10+
"runtime/debug"
11+
12+
"github.com/Masterminds/semver/v3"
13+
14+
"go.opentelemetry.io/auto/internal/pkg/process/binary"
15+
)
16+
17+
// Info are the details about a target process.
18+
type Info struct {
19+
ID ID
20+
Functions []*binary.Func
21+
GoVersion *semver.Version
22+
Modules map[string]*semver.Version
23+
Allocation *Allocation
24+
}
25+
26+
// NewInfo returns a new Info with information about the process identified by
27+
// id. The functions of the returned Info are filtered by relevantFuncs.
28+
//
29+
// A partial Info and error may be returned for dependencies that cannot be
30+
// parsed.
31+
func NewInfo(id ID, relevantFuncs map[string]interface{}) (*Info, error) {
32+
elfF, err := elf.Open(id.ExePath())
33+
if err != nil {
34+
return nil, err
35+
}
36+
defer elfF.Close()
37+
38+
bi, err := id.BuildInfo()
39+
if err != nil {
40+
return nil, err
41+
}
42+
43+
goVersion, err := semver.NewVersion(bi.GoVersion)
44+
if err != nil {
45+
return nil, err
46+
}
47+
48+
result := &Info{
49+
ID: id,
50+
GoVersion: goVersion,
51+
}
52+
53+
result.Functions, err = findFunctions(elfF, relevantFuncs)
54+
if err != nil {
55+
return nil, err
56+
}
57+
58+
result.Modules, err = findModules(goVersion, bi.Deps)
59+
return result, err
60+
}
61+
62+
func findModules(goVer *semver.Version, deps []*debug.Module) (map[string]*semver.Version, error) {
63+
var err error
64+
out := make(map[string]*semver.Version, len(deps)+1)
65+
for _, dep := range deps {
66+
depVersion, e := semver.NewVersion(dep.Version)
67+
if e != nil {
68+
err = errors.Join(
69+
err,
70+
fmt.Errorf("invalid dependency version %s (%s): %w", dep.Path, dep.Version, e),
71+
)
72+
continue
73+
}
74+
out[dep.Path] = depVersion
75+
}
76+
out["std"] = goVer
77+
return out, err
78+
}
79+
80+
func findFunctions(elfF *elf.File, relevantFuncs map[string]interface{}) ([]*binary.Func, error) {
81+
found, err := binary.FindFunctionsUnStripped(elfF, relevantFuncs)
82+
if err != nil {
83+
if !errors.Is(err, elf.ErrNoSymbols) {
84+
return nil, err
85+
}
86+
found, err = binary.FindFunctionsStripped(elfF, relevantFuncs)
87+
if err != nil {
88+
return nil, err
89+
}
90+
}
91+
92+
if len(found) == 0 {
93+
return nil, errors.New("no functions found")
94+
}
95+
96+
return found, nil
97+
}
98+
99+
// GetFunctionOffset returns the offset for of the function with name.
100+
func (i *Info) GetFunctionOffset(name string) (uint64, error) {
101+
for _, f := range i.Functions {
102+
if f.Name == name {
103+
return f.Offset, nil
104+
}
105+
}
106+
107+
return 0, fmt.Errorf("could not find offset for function %s", name)
108+
}
109+
110+
// GetFunctionReturns returns the return value of the call for the function
111+
// with name.
112+
func (i *Info) GetFunctionReturns(name string) ([]uint64, error) {
113+
for _, f := range i.Functions {
114+
if f.Name == name {
115+
return f.ReturnOffsets, nil
116+
}
117+
}
118+
119+
return nil, fmt.Errorf("could not find returns for function %s", name)
120+
}

0 commit comments

Comments
 (0)