81 lines
1.6 KiB
Go
81 lines
1.6 KiB
Go
package utils
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/shirou/gopsutil/v3/process"
|
|
)
|
|
|
|
func IsDebug() bool {
|
|
return isGolandDebug() || isDebugByPath()
|
|
|
|
}
|
|
|
|
func isDebugByPath() bool {
|
|
exePath := strings.ToLower(os.Args[0])
|
|
// 获取绝对路径
|
|
absPath, err := filepath.Abs(exePath)
|
|
if err != nil {
|
|
absPath = exePath
|
|
}
|
|
|
|
// 检查路径中是否包含常见的临时目录或"go-build"字样
|
|
tmpDir := strings.ToLower(getTempDir())
|
|
isTemp := strings.Contains(absPath, tmpDir) || strings.Contains(absPath, "go-build") || strings.Contains(absPath, "go_build_")
|
|
return isTemp
|
|
}
|
|
func getTempDir() string {
|
|
tempDir := os.TempDir()
|
|
|
|
// 尝试转换为长路径
|
|
if longPath, err := filepath.EvalSymlinks(tempDir); err == nil {
|
|
if absPath, err := filepath.Abs(longPath); err == nil {
|
|
return absPath
|
|
}
|
|
return longPath
|
|
}
|
|
|
|
return filepath.Dir(tempDir)
|
|
}
|
|
|
|
func isGolandDebug() bool {
|
|
// 获取当前进程ID
|
|
pid := os.Getpid()
|
|
|
|
// 获取当前进程信息
|
|
currentProcess, err := process.NewProcess(int32(pid))
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
// 获取父进程
|
|
parent, err := currentProcess.Parent()
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
// 获取父进程名称
|
|
name, err := parent.Name()
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
// 检查父进程是否是dlv(Go调试器)或GoLand相关进程
|
|
name = strings.ToLower(name)
|
|
debugProcessNames := []string{
|
|
"dlv.exe", "dlv",
|
|
"goland.exe", "goland64.exe",
|
|
"goland", "idea.exe",
|
|
}
|
|
|
|
for _, debugName := range debugProcessNames {
|
|
if strings.Contains(name, debugName) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|