Linux崩溃时启动脚本获取进程相关信息

编写test.cpp

#include 
#include 
#include 
#include 
#include 
 
void terminate_handler()
{
   char cmdline[1024] = {0,};
   sprintf(cmdline, "bash term.sh %d %d", getpid(), getppid());
  // printf("executing %s\n", cmdline);
   system(cmdline);
}
 
int main()
{
    std::set_terminate(terminate_handler);
    //abort();
    int a = 10 / 0;
    throw "test";
    return 0;
}

 

Makefile

CC= g++
CC_Flag= -O3
 
test: test.cpp
    $(CC) $(CC_Flag) $< -o $@
 
 

term.sh

src_dir=/proc/$1
term_dir=$PWD/term
dst_dir=$term_dir/$1


#if [ -d $src_dir ]; then
# echo source folder \"$src_dir\" existing
#fi

if ! [ -d $term_dir ]; then
mkdir $term_dir
else
rm -rf $term_dir/*
fi

if ! [ -d $dst_dir ]; then
# echo destination folder \" $dst_dir \" doesn\'t existing
mkdir $dst_dir
#else
# echo destination folder \" $dst_dir \" existing
fi

if [ -f $src_dir/status ]; then
cat $src_dir/status > $dst_dir/status
chmod a+w $dst_dir/status
fi

if [ -d $src_dir/task ]; then
#ls $src_dir/task | tee | sed -e "
s/^/task /"
for file in $(ls $src_dir/task)
do
thread_dir=$src_dir/task/$file
if [ -d $thread_dir ]; then
if [ -O $thread_dir/stack ]; then
mkdir $dst_dir/$file
sudo cat $thread_dir/stack > $dst_dir/$file/stack
chmod a+w $dst_dir/$file/stack
else
echo "
Invalid permissions"
sudo cat $file/stack > $dst_dir/{basename $file}/stack
fi
fi
done
fi





接下来是测试结果

$ ./test
[sudo] password for daniel:
已放弃

 

$ tree
.
├── Makefile
├── term
│ └── 5886
│ ├── 5886
│ │ └── stack
│ └── status
├── term.sh
├── test
└── test.cpp

3 directories, 6 files

 

$ cat term/5886/5886/stack 
[] do_wait+0x1e2/0x240
[] SyS_wait4+0x64/0xe0
[] system_call_fastpath+0x1a/0x1f
[] 0xffffffffffffffff
 
 
$ cat term/5886/status 
Name: test
State: S (sleeping)
Tgid: 5886
Pid: 5886
PPid: 2205
TracerPid: 0
Uid: 1000 1000 1000 1000
Gid: 1000 1000 1000 1000
FDSize: 256
Groups: 4 24 27 30 46 112 118 124 1000
VmPeak: 12652 kB
VmSize: 12652 kB
VmLck: 0 kB
VmPin: 0 kB
VmHWM: 1072 kB
VmRSS: 1072 kB
VmData: 272 kB
VmStk: 136 kB
VmExe: 4 kB
VmLib: 3960 kB
VmPTE: 44 kB
VmSwap: 0 kB
Threads: 1
SigQ: 0/30685
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000010000
SigIgn: 0000000000000006
SigCgt: 0000000000000000
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
CapBnd: 0000001fffffffff
Seccomp: 0
Cpus_allowed: ff
Cpus_allowed_list: 0-7
Mems_allowed: 00000000,00000001
Mems_allowed_list: 0
voluntary_ctxt_switches: 3
nonvoluntary_ctxt_switches: 1

用autotools来替换手动编写Makefile

 

Makefile_Template.am

bin_PROGRAMS = XXX
XXX_SOURCES = YYY

 

autogen.sh

sed -e "{
s/XXX/$1/
s/YYY/$2/
}"
Makefile_Template.am > Makefile.am

autoscan

sed -e "/AC_INIT/a\
AM_INIT_AUTOMAKE"
configure.scan > configure.ac
autoheader

aclocal
touch README AUTHORS NEWS ChangeLog
automake --add-missing --copy
autoconf
./configure
make
【s/YYY/$2是无法支持多个.cpp文件的,需要进一步优化】
 

调用autogen.sh

bash autogen.sh test test.cpp
./test

结果与上面是一样的


src_dir=/proc/$1
timestamp=$(date +%F | tr "-" "_")
timestamp="$timestamp"_"$(date +%T | tr ":" "_")"
term_dir=$PWD/$timestamp
dst_dir=$term_dir/$1

if ! [ -d $term_dir ]; then
mkdir $term_dir
else
rm -rf $term_dir/*
fi

if ! [ -d $dst_dir ]; then
mkdir $dst_dir
fi

if [ -f $src_dir/status ]; then
cat $src_dir/status > $dst_dir/status
chmod a+w $dst_dir/status
fi

if [ -f $src_dir/maps ]; then
cat $src_dir/maps > $dst_dir/maps
chmod a+w $dst_dir/maps
fi

if [ -d $src_dir/task ]; then
for file in $(ls $src_dir/task)
do
thread_dir=$src_dir/task/$file
if [ -d $thread_dir ]; then
if [ -O $thread_dir/stack ]; then
mkdir $dst_dir/$file
sudo cat $thread_dir/stack > $dst_dir/$file/stack
chmod a+w $dst_dir/$file/stack
else
echo "Invalid permissions"
sudo cat $file/stack > $dst_dir/$file/stack
fi
fi
done
fi

max_threads=$(cat /proc/sys/kernel/threads-max)
cur_threads=$(grep -s '^Threads' /proc/[0-9]*/status | awk '{ sum += $2; } END { print sum; }')
max_mem=$(cat /proc/meminfo | grep MemTotal | awk '{print $2,$3}')
free_mem=$(cat /proc/meminfo | grep MemFree | awk '{print $2,$3}')
vm_peak=$(cat $src_dir/status | grep VmPeak | awk '{print $2,$3}')
vm_size=$(cat $src_dir/status | grep VmPeak | awk '{print $2,$3}')

echo "System Summary:" > $term_dir/summary
echo "Threads:"
echo -e "Maximum \e[1;31m" $max_threads "\e[0mthreads allowed!" >> $term_dir/summary
echo -e "Currently \e[1;31m" $cur_threads "\e[0mthreads consumed!" >> $term_dir/summary
echo "Memory:"
echo -e "Total \e[1;31m" $max_mem "\e[0mmemory in system!" >> $term_dir/summary
echo -e "Free \e[1;31m" $free_mem "\e[0mmemory in system!" >> $term_dir/summary
echo "Process " $1 "Memory:"
echo -e "Peak use \e[1;31m" $vm_peak "\e[0mmemory!" >> $term_dir/summary
echo -e "Current use \e[1;31m" $vm_size "\e[0mmemory!" >> $term_dir/summary

cat $term_dir/summary

转载于:https://www.cnblogs.com/long123king/p/3747353.html

你可能感兴趣的:(Linux崩溃时启动脚本获取进程相关信息)