Skip to content

Commit

Permalink
库搜索逻辑优化
Browse files Browse the repository at this point in the history
  • Loading branch information
SeeFlowerX committed Sep 6, 2023
1 parent 7d2f980 commit 4f5fc57
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 15 deletions.
40 changes: 31 additions & 9 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,16 @@ func persistentPreRunEFunc(command *cobra.Command, args []string) error {
return fmt.Errorf("can not find process_pid=%d", process_pid)
}
addLibPath(info.Name)
// 解析maps 将maps中的路径加入搜索路径中
search_paths, err := event.FindLibPaths(process_pid)
if err != nil {
return err
}
for _, search_path := range search_paths {
if !slices.Contains(gconfig.LibraryDirs, search_path) {
gconfig.LibraryDirs = append(gconfig.LibraryDirs, search_path)
}
}
}
// 根据 uid 解析进程架构、获取库文件搜索路径
for _, pkg_uid := range mconfig.UidWhitelist {
Expand Down Expand Up @@ -220,10 +230,23 @@ func persistentPreRunEFunc(command *cobra.Command, args []string) error {
if !is_find {
return fmt.Errorf("can not find pkg_name=%s", pkg_name)
}
if info.Uid == 1000 {
pid_list := FindPidByName(pkg_name)
mconfig.PidWhitelist = append(mconfig.PidWhitelist, pid_list...)
} else {
// 根据包名查找进程名 再获取库搜索路径
pid_list := FindPidByName(pkg_name)
for _, pkg_pid := range pid_list {
// 解析maps 将maps中的路径加入搜索路径中
search_paths, err := event.FindLibPaths(pkg_pid)
if err != nil {
return err
}
for _, search_path := range search_paths {
if !slices.Contains(gconfig.LibraryDirs, search_path) {
gconfig.LibraryDirs = append(gconfig.LibraryDirs, search_path)
}
}
}
// 对于system进程不添加uid
mconfig.PidWhitelist = append(mconfig.PidWhitelist, pid_list...)
if info.Uid != 1000 {
mconfig.UidWhitelist = append(mconfig.UidWhitelist, info.Uid)
}
addLibPath(pkg_name)
Expand Down Expand Up @@ -419,9 +442,7 @@ func findBTFAssets() string {
if strings.Contains(lines, "rockchip") {
btf_file = "rock5b-5.10-arm64_min.btf"
}
if gconfig.Debug {
Logger.Printf("findBTFAssets btf_file=%s", btf_file)
}
Logger.Printf("findBTFAssets btf_file=%s", btf_file)
return btf_file
}

Expand All @@ -447,9 +468,10 @@ func findKallsymsSymbol(symbol string) (bool, error) {

func FindPidByName(name string) []uint32 {
var pid_list []uint32
content, err := util.RunCommand("sh", "-c", "ps -ef -o name,pid,ppid | grep `^"+name+"`")
content, err := util.RunCommand("sh", "-c", "ps -ef -o name,pid,ppid | grep ^"+name)
if err != nil {
panic(err)
Logger.Printf("warn, no running process of %s", name)
return pid_list
}
lines := strings.Split(content, "\n")
for _, line := range lines {
Expand Down
20 changes: 20 additions & 0 deletions user/event/event_mmap2.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"stackplz/user/util"
"strings"
"sync"

"golang.org/x/exp/slices"
)

type LibInfo struct {
Expand Down Expand Up @@ -116,6 +118,24 @@ func (this *MapsHelper) FindLib(pid uint32) (ProcMaps, error) {
return pid_maps.Clone(), nil
}

func FindLibPaths(pid uint32) ([]string, error) {
var search_paths []string
pid_maps, err := maps_helper.FindLib(pid)
if err != nil {
return search_paths, err
}
for seg_path, _ := range pid_maps {
if strings.HasPrefix(seg_path, "/") && strings.HasSuffix(seg_path, ".so") {
items := strings.Split(seg_path, "/")
lib_search_path := strings.Join(items[:len(items)-1], "/")
if !slices.Contains(search_paths, lib_search_path) {
search_paths = append(search_paths, lib_search_path)
}
}
}
return search_paths, nil
}

func (this *MapsHelper) UpdateForkEvent(event *ForkEvent) {
// 根据日志实际的记录结果 fork 的 pid ppid 存在相同的情况 why
parent_pid, ok := this.child_parent_map[event.Pid]
Expand Down
8 changes: 3 additions & 5 deletions user/module/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,9 @@ func (this *MStack) update_common_filter() {
this.update_common_list(this.mconf.PidBlacklist, util.PID_BLACKLIST_START)
this.update_common_list(this.mconf.TidWhitelist, util.TID_WHITELIST_START)
this.update_common_list(this.mconf.TidBlacklist, util.TID_BLACKLIST_START)
if this.mconf.Debug {
this.logger.Printf("uid => whitelist:[%s];blacklist:[%s]", this.list2string(this.mconf.UidWhitelist), this.list2string(this.mconf.UidBlacklist))
this.logger.Printf("pid => whitelist:[%s];blacklist:[%s]", this.list2string(this.mconf.PidWhitelist), this.list2string(this.mconf.PidBlacklist))
this.logger.Printf("tid => whitelist:[%s];blacklist:[%s]", this.list2string(this.mconf.TidWhitelist), this.list2string(this.mconf.TidBlacklist))
}
this.logger.Printf("uid => whitelist:[%s];blacklist:[%s]", this.list2string(this.mconf.UidWhitelist), this.list2string(this.mconf.UidBlacklist))
this.logger.Printf("pid => whitelist:[%s];blacklist:[%s]", this.list2string(this.mconf.PidWhitelist), this.list2string(this.mconf.PidBlacklist))
this.logger.Printf("tid => whitelist:[%s];blacklist:[%s]", this.list2string(this.mconf.TidWhitelist), this.list2string(this.mconf.TidBlacklist))
var filter_key uint32 = 0
map_name := "common_filter"
filter_value := this.mconf.GetCommonFilter()
Expand Down
2 changes: 1 addition & 1 deletion user/util/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func FindLib(library string, search_paths []string) (string, error) {
}
if len(full_paths) == 0 {
// 没找到
return library, fmt.Errorf("can not find %s in these paths\n%s", library, strings.Join(search_paths[:], "\n\t"))
return library, fmt.Errorf("can not find %s in these paths\n\t%s", library, strings.Join(search_paths[:], "\n\t"))
}
if len(full_paths) > 1 {
// 在已有的搜索路径下可能存在多个同名的库 提示用户指定全路径
Expand Down

0 comments on commit 4f5fc57

Please sign in to comment.