码迷,mamicode.com
首页 > 系统相关 > 详细

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

时间:2014-05-26 09:34:23      阅读:392      评论:0      收藏:0      [点我收藏+]

标签:des   style   c   code   tar   ext   

编写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

结果与上面是一样的

Linux崩溃时启动脚本获取进程相关信息,布布扣,bubuko.com

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

标签:des   style   c   code   tar   ext   

原文地址:http://www.cnblogs.com/long123king/p/3747353.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!