|
|
@@ -5,19 +5,17 @@ import (
|
|
|
"fmt"
|
|
|
. "github.com/coroot/coroot-node-agent/utils/modelse"
|
|
|
klog "github.com/sirupsen/logrus"
|
|
|
- "io/ioutil"
|
|
|
"os"
|
|
|
"os/exec"
|
|
|
"regexp"
|
|
|
"runtime"
|
|
|
"strings"
|
|
|
-
|
|
|
)
|
|
|
|
|
|
var libjvmRegex = regexp.MustCompile(`.*/libjvm\.so`)
|
|
|
|
|
|
-func GetExeType(pid uint32) CodeType {
|
|
|
- mapsFilePath := fmt.Sprintf("/proc/%d/maps", pid)
|
|
|
+func GetExeType(pid uint32, rootfs string) CodeType {
|
|
|
+ mapsFilePath := fmt.Sprintf("%sproc/%d/maps", "/", pid)
|
|
|
|
|
|
data, err := os.ReadFile(mapsFilePath)
|
|
|
if err != nil {
|
|
|
@@ -29,18 +27,18 @@ func GetExeType(pid uint32) CodeType {
|
|
|
|
|
|
if libjvmRegex.MatchString(content) {
|
|
|
//fmt.Println("is java process")
|
|
|
- if isJavaAotProcess(pid) {
|
|
|
+ if isJavaAotProcess(pid, rootfs) {
|
|
|
//fmt.Println("is javaAot process")
|
|
|
return CodeTypeJavaAot
|
|
|
}
|
|
|
return CodeTypeJava
|
|
|
- } else if isJavaAotProcess(pid) {
|
|
|
+ } else if isJavaAotProcess(pid, rootfs) {
|
|
|
//fmt.Println("is javaAot process")
|
|
|
return CodeTypeJavaAot
|
|
|
- } else if isGoProcess(pid) {
|
|
|
+ } else if isGoProcess(pid, rootfs) {
|
|
|
//fmt.Println("is go process")
|
|
|
return CodeTypeGo
|
|
|
- } else if isNetCoreProcess(pid) {
|
|
|
+ } else if isNetCoreProcess(pid, rootfs) {
|
|
|
// fmt.Println("is netcore process")
|
|
|
return CodeTypeNetCoreAot
|
|
|
}
|
|
|
@@ -48,18 +46,18 @@ func GetExeType(pid uint32) CodeType {
|
|
|
}
|
|
|
|
|
|
// isJavaAotProcess checks if the process with the given PID is a GraalVM native image
|
|
|
-func isJavaAotProcess(pid uint32) bool {
|
|
|
+func isJavaAotProcess(pid uint32, rootfs string) bool {
|
|
|
// Get the executable path for the given PID
|
|
|
- exePath, err := os.Readlink(fmt.Sprintf("/proc/%d/exe", 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)
|
|
|
return false
|
|
|
}
|
|
|
|
|
|
// Read the content of the executable file
|
|
|
- content, err := ioutil.ReadFile(exePath)
|
|
|
+ content, err := os.ReadFile(fmt.Sprintf("%s%s", rootfs, exePath))
|
|
|
if err != nil {
|
|
|
- fmt.Printf("Error reading executable file for PID %d: %v\n", pid, err)
|
|
|
+ klog.WithError(err).Errorf("Error reading executable file for PID %d\n", pid)
|
|
|
return false
|
|
|
}
|
|
|
|
|
|
@@ -71,15 +69,15 @@ func isJavaAotProcess(pid uint32) bool {
|
|
|
return false
|
|
|
}
|
|
|
|
|
|
-func isNetCoreProcess(pid uint32) bool {
|
|
|
+func isNetCoreProcess(pid uint32, rootfs string) bool {
|
|
|
path, err := getProcessPath(pid)
|
|
|
if err != nil {
|
|
|
- fmt.Printf("无法获取进程路径:%s\n", err)
|
|
|
+ //fmt.Printf("无法获取进程路径:%s\n", err)
|
|
|
return false
|
|
|
}
|
|
|
- ef, err := elf.Open(path)
|
|
|
+ ef, err := elf.Open(rootfs + path)
|
|
|
if err != nil {
|
|
|
- fmt.Println("failed to open as elf binary", err)
|
|
|
+ klog.WithError(err).Error("failed to open as elf binary")
|
|
|
return false
|
|
|
}
|
|
|
defer ef.Close()
|
|
|
@@ -92,13 +90,13 @@ func isNetCoreProcess(pid uint32) bool {
|
|
|
return false
|
|
|
}
|
|
|
|
|
|
-func isGoProcess(pid uint32) bool {
|
|
|
+func isGoProcess(pid uint32, rootfs string) bool {
|
|
|
path, err := getProcessPath(pid)
|
|
|
if err != nil {
|
|
|
- fmt.Printf("无法获取进程路径:%s\n", err)
|
|
|
+ klog.WithError(err).Error("failed to open as elf binary")
|
|
|
return false
|
|
|
}
|
|
|
- ef, err := elf.Open(path)
|
|
|
+ ef, err := elf.Open(rootfs + path)
|
|
|
if err != nil {
|
|
|
fmt.Println("failed to open as elf binary", err)
|
|
|
return false
|