|
|
@@ -3,6 +3,7 @@ package containers
|
|
|
import (
|
|
|
"debug/elf"
|
|
|
"fmt"
|
|
|
+ "github.com/coroot/coroot-node-agent/flags"
|
|
|
. "github.com/coroot/coroot-node-agent/utils/modelse"
|
|
|
klog "github.com/sirupsen/logrus"
|
|
|
"os"
|
|
|
@@ -50,15 +51,28 @@ func isJavaAotProcess(pid uint32, rootfs string) bool {
|
|
|
// Get the executable path for the given PID
|
|
|
exePath, err := os.Readlink(fmt.Sprintf("%sproc/%d/exe", "/", pid))
|
|
|
if err != nil {
|
|
|
- fmt.Printf("Error reading executable path for PID %d: %v\n", pid, err)
|
|
|
+ //fmt.Printf("Error reading executable path for PID %d: %v\n", pid, err)
|
|
|
+ klog.WithError(err).Errorf("isJavaAotProcess,failed to reading executable path for PID [%d]", pid)
|
|
|
return false
|
|
|
}
|
|
|
|
|
|
// Read the content of the executable file
|
|
|
- content, err := os.ReadFile(fmt.Sprintf("%s%s", rootfs, exePath))
|
|
|
+ pathPrefix := rootfs
|
|
|
+ if pathPrefix == "" && *flags.RunInContainer {
|
|
|
+ pathPrefix = *flags.HostDirPathPrefix
|
|
|
+ }
|
|
|
+ content, err := os.ReadFile(fmt.Sprintf("%s%s", pathPrefix, exePath))
|
|
|
if err != nil {
|
|
|
- klog.WithError(err).Errorf("Error reading executable file for PID %d\n", pid)
|
|
|
- return false
|
|
|
+ if _, err = os.Stat(exePath); err == nil {
|
|
|
+ content, err = os.ReadFile(exePath)
|
|
|
+ if err != nil {
|
|
|
+ klog.WithError(err).Errorf("isJavaAotProcess,failed to reading executable file local for PID [%d]", pid)
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ klog.WithError(err).Errorf("isJavaAotProcess,failed to reading executable file for PID [%d]", pid)
|
|
|
+ return false
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// Check if the file contains the "graal_attach_thread" string
|
|
|
@@ -73,12 +87,26 @@ func isNetCoreProcess(pid uint32, rootfs string) bool {
|
|
|
path, err := getProcessPath(pid)
|
|
|
if err != nil {
|
|
|
//fmt.Printf("无法获取进程路径:%s\n", err)
|
|
|
+ klog.WithError(err).Errorf("isNetCoreProcess,failed to open as elf binary path for PID [%d]", pid)
|
|
|
return false
|
|
|
}
|
|
|
- ef, err := elf.Open(rootfs + path)
|
|
|
+
|
|
|
+ pathPrefix := rootfs
|
|
|
+ if pathPrefix == "" && *flags.RunInContainer {
|
|
|
+ pathPrefix = *flags.HostDirPathPrefix
|
|
|
+ }
|
|
|
+ ef, err := elf.Open(pathPrefix + path)
|
|
|
if err != nil {
|
|
|
- klog.WithError(err).Error("failed to open as elf binary")
|
|
|
- return false
|
|
|
+ if _, err = os.Stat(path); err == nil {
|
|
|
+ ef, err = elf.Open(path)
|
|
|
+ if err != nil {
|
|
|
+ klog.WithError(err).Errorf("isNetCoreProcess,failed to open as elf binary file local for PID [%d]", pid)
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ klog.WithError(err).Errorf("isNetCoreProcess,failed to open as elf binary file for PID [%d]", pid)
|
|
|
+ return false
|
|
|
+ }
|
|
|
}
|
|
|
defer ef.Close()
|
|
|
|
|
|
@@ -93,13 +121,27 @@ func isNetCoreProcess(pid uint32, rootfs string) bool {
|
|
|
func isGoProcess(pid uint32, rootfs string) bool {
|
|
|
path, err := getProcessPath(pid)
|
|
|
if err != nil {
|
|
|
- klog.WithError(err).Error("failed to open as elf binary")
|
|
|
+ klog.WithError(err).Errorf("isGoProcess,failed to open as elf binary path for PID [%d]", pid)
|
|
|
return false
|
|
|
}
|
|
|
- ef, err := elf.Open(rootfs + path)
|
|
|
+
|
|
|
+ pathPrefix := rootfs
|
|
|
+ if pathPrefix == "" && *flags.RunInContainer {
|
|
|
+ pathPrefix = *flags.HostDirPathPrefix
|
|
|
+ }
|
|
|
+
|
|
|
+ ef, err := elf.Open(pathPrefix + path)
|
|
|
if err != nil {
|
|
|
- fmt.Println("failed to open as elf binary", err)
|
|
|
- return false
|
|
|
+ if _, err = os.Stat(path); err == nil {
|
|
|
+ ef, err = elf.Open(path)
|
|
|
+ if err != nil {
|
|
|
+ klog.WithError(err).Errorf("isGoProcess,failed to open as elf binary file local for PID [%d]", pid)
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ klog.WithError(err).Errorf("isGoProcess,failed to open as elf binary file for PID [%d]", pid)
|
|
|
+ return false
|
|
|
+ }
|
|
|
}
|
|
|
defer ef.Close()
|
|
|
|