Skip to content

Commit

Permalink
Improve python language detection by linked libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
edeNFed committed Nov 26, 2024
1 parent 398965b commit 6ee5c0b
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions procdiscovery/pkg/inspectors/python/python.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
package python

import (
"github.com/hashicorp/go-version"
"debug/elf"
"fmt"
"os"
"strings"

"github.com/hashicorp/go-version"

"github.com/odigos-io/odigos/common"
"github.com/odigos-io/odigos/procdiscovery/pkg/process"
)

type PythonInspector struct{}

const pythonProcessName = "python"
const (
pythonProcessName = "python"
libPythonStr = "libpython3"
)

func (p *PythonInspector) Inspect(proc *process.Details) (common.ProgrammingLanguage, bool) {
if strings.Contains(proc.ExeName, pythonProcessName) || strings.Contains(proc.CmdLine, pythonProcessName) {
return common.PythonProgrammingLanguage, true
}

if p.isLibPythonLinked(proc) {
return common.PythonProgrammingLanguage, true
}

return "", false
}

Expand All @@ -27,3 +38,36 @@ func (p *PythonInspector) GetRuntimeVersion(proc *process.Details, containerURL

return nil
}

func (p *PythonInspector) isLibPythonLinked(proc *process.Details) bool {
f := fmt.Sprintf("/proc/%d/exe", proc.ProcessID)
file, err := os.Open(f)
if err != nil {
return false
}
defer file.Close()

elfFile, err := elf.NewFile(file)
if err != nil {
return false
}
defer elfFile.Close()

section := elfFile.Section(".dynamic")
if section == nil {
return false
}

dynamicSection, err := elfFile.DynString(elf.DT_NEEDED)
if err != nil {
return false
}

for _, dep := range dynamicSection {
if strings.Contains(dep, libPythonStr) {
return true
}
}

return false
}

0 comments on commit 6ee5c0b

Please sign in to comment.