首页 > 代码库 > Linux崩溃时启动脚本获取进程相关信息
Linux崩溃时启动脚本获取进程相关信息
编写test.cpp
#include <stdlib.h>
#include <stdio.h>
#include <exception>
#include <string.h>
#include <unistd.h>
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
[<ffffffff81063c72>] do_wait+0x1e2/0x240
[<ffffffff81064cb4>] SyS_wait4+0x64/0xe0
[<ffffffff816f521d>] system_call_fastpath+0x1a/0x1f
[<ffffffffffffffff>] 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
autoheader
sed -e "/AC_INIT/a\
AM_INIT_AUTOMAKE" configure.scan > configure.ac
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
结果与上面是一样的
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。