1
|
#!/bin/bash
|
2
|
if [ -n "$1" ]; then
|
3
|
isKeepLast=$1
|
4
|
else
|
5
|
isKeepLast=false # 如果未提供参数,则使用默认值
|
6
|
fi
|
7
|
|
8
|
if [ -n "$2" ]; then
|
9
|
dumpMode=$2
|
10
|
else
|
11
|
dumpMode=0 # 如果未提供参数,则使用默认值
|
12
|
fi
|
13
|
|
14
|
cu_dir="/root/yzmm/rel/GNB/cu/cu/nr_hl_cu/build/"
|
15
|
du_dir="/root/yzmm/rel/GNB/du/ran/DU/build/intel/du_bin/bin/"
|
16
|
phy_dir="/root/yzmm/rel/GNB/phy/bin/"
|
17
|
dev_dir="/usr/local/lib/"
|
18
|
baseService_dir="/root/yzmm/baseService/bin/"
|
19
|
#将所有core文件所在的路径存储到数组core_dirs中
|
20
|
core_dirs=($cu_dir $du_dir $phy_dir $dev_dir $baseService_dir)
|
21
|
|
22
|
cu_exe=$cu_dir"gnb_cu"
|
23
|
du_exe=$du_dir"gnb_du"
|
24
|
phy_exe=$phy_dir"l1app"
|
25
|
dev_exe=$dev_dir"dev_om"
|
26
|
baseService_exe=$baseService_dir"baseService"
|
27
|
|
28
|
exe_files=($cu_exe $du_exe $phy_exe $dev_exe $baseService_exe)
|
29
|
module_name_ary=("cu" "du" "l1app" "dev" "baseService")
|
30
|
|
31
|
#core_out_dir存储core调试信息的转储路径
|
32
|
core_out_dir=/root/yzmm/logbak/core/
|
33
|
|
34
|
# 定义要保留的文件数量
|
35
|
KEEP_COUNT=5
|
36
|
|
37
|
#根据core文件的文件夹数组来遍历所有的core文件
|
38
|
for i in ${!core_dirs[@]}
|
39
|
do
|
40
|
# 初始化新的 core_files 列表
|
41
|
new_core_files=""
|
42
|
# 循环处理指定路径下的所有core文件
|
43
|
for corefile in $(ls ${core_dirs[$i]}core.*)
|
44
|
do
|
45
|
# 使用file命令获取产生core文件的进程名,获取./进程名
|
46
|
procname=$(file $corefile | awk -F"'" '{print $2}')
|
47
|
# 获取core文件所属进程
|
48
|
fileOwningProc=$(basename $procname | cut -d '/' -f 2)
|
49
|
# 获取core文件所在文件夹进程
|
50
|
dirOwningProc=$(basename "${exe_files[$i]}" | cut -d '/' -f 2)
|
51
|
# 比较是否是当前exe进程的所属core文件
|
52
|
if [ "$fileOwningProc" != "$dirOwningProc" ]; then
|
53
|
echo "delete file $corefile continue"
|
54
|
rm "$corefile"
|
55
|
continue
|
56
|
fi
|
57
|
# 添加符合条件的文件到 new_core_files 列表
|
58
|
new_core_files="$new_core_files $corefile"
|
59
|
# new_core_files+=("$corefile") #包含特殊字符使用
|
60
|
echo "file $corefile add new"
|
61
|
done
|
62
|
|
63
|
echo "new_core_files=$new_core_files"
|
64
|
# 检查 new_core_files 数组是否为空
|
65
|
if [ "$new_core_files" == "" ]; then
|
66
|
echo "No core files found for the specified executables."
|
67
|
continue
|
68
|
fi
|
69
|
|
70
|
# 对筛选后的 core 文件按修改时间排序(新的在前)
|
71
|
sorted_core_files=$(echo "$new_core_files" | xargs ls -t)
|
72
|
# sorted_core_files=$(printf "%q\n" "${new_core_files[@]}" | xargs -I {} ls -td "{}" | cut -d' ' -f2-) #包含特殊字符使用
|
73
|
# 重新计算筛选后的 core 文件数量
|
74
|
core_count=$(echo "$sorted_core_files" | wc -w)
|
75
|
|
76
|
echo "sorted_core_files=$sorted_core_files"
|
77
|
echo "core_count=$core_count"
|
78
|
|
79
|
# 如果 core 文件数量超过 KEEP_COUNT,删除时间最早的文件,转储KEEP_COUNT数量最新的core文件
|
80
|
count=0
|
81
|
for file in $sorted_core_files; do
|
82
|
if [ $count -lt $KEEP_COUNT ]; then
|
83
|
if [ $count -eq 0 ] && [ $isKeepLast = true ]; then
|
84
|
count=$((count + 1))
|
85
|
continue
|
86
|
fi
|
87
|
|
88
|
if [ $dumpMode -eq 0 ]; then
|
89
|
echo "core delete $file"
|
90
|
else
|
91
|
echo "core dumping $file"
|
92
|
#根据Core文件名称获取进程ID
|
93
|
pid=$(basename $file | cut -d '.' -f 2)
|
94
|
#获取Core文件的最后访问时间,精确到秒
|
95
|
file_access_time=$(stat -c %y $file | cut -d'.' -f1 | tr -d '-' | tr -d ':' | tr ' ' '_')
|
96
|
#获取模块名称
|
97
|
module_name=${module_name_ary[$i]}
|
98
|
#组装转储文件的名称
|
99
|
stackfile=$core_out_dir"core."$module_name"_"$pid"_"$file_access_time".txt"
|
100
|
|
101
|
if [ $dumpMode -eq 1 ]; then
|
102
|
gdb -ex "set pagination 0" -ex "thread apply all bt" --batch -c $file ${exe_files[$i]} > "$stackfile"
|
103
|
elif [ $dumpMode -eq 2 ]; then
|
104
|
#使用gdb来获取堆栈信息和局部变量信息,并将结果输出到以进程ID命名的文件中
|
105
|
gdb -ex "set pagination 0" -ex "thread apply all bt full" --batch -c $file ${exe_files[$i]} > "$stackfile"
|
106
|
else
|
107
|
echo "no use gdb in delete mode."
|
108
|
fi
|
109
|
fi
|
110
|
fi
|
111
|
echo "Deleting $file"
|
112
|
rm "$file"
|
113
|
|
114
|
count=$((count + 1))
|
115
|
done
|
116
|
done
|
117
|
|