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
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
调用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
Linux崩溃时启动脚本获取进程相关信息的更多相关文章
- 014-交互式Shell和shell脚本获取进程 pid
Linux 的交互式 Shell 与 Shell 脚本存在一定的差异,主要是由于后者存在一个独立的运行进程 1.交互式 Bash Shell 获取进程 pid 在已知进程名(name)的前提下,交互式 ...
- Linux sysinfo获取系统相关信息
Linux中,可以用sysinfo来获取系统相关信息. #include <stdio.h> #include <stdlib.h> #include <errno.h& ...
- Python基础:获取平台相关信息
Windows 10家庭中文版,Python 3.6.4, 本文介绍了使用os.platform.sys三个模块获取Python程序的运行平台相关的信息. os模块:提供 各种各样的操作系统接口 os ...
- Linux下使用fstatfs/statfs查询系统相关信息
Linux下使用fstatfs/statfs查询系统相关信息 1. 功能 #include < sys/statfs.h > int statfs(const char *path, ...
- 获取系统相关信息 (CPU使用率 内存使用率 系统磁盘大小)
引言 在软件开个过程中,对于软件的稳定性和使用率也是我们需要关注的 . 使用sigar来监控,简单方便! 使用说明:下载sigar jar及配合sigar的dll文件来用,需要将dll文件放到JD ...
- 把数据保存到数据库附加表 `dede_addonarticle` 时出错,请把相关信息提交给DedeCms官方。Duplicate entry
把数据保存到数据库附加表 `dede_addonarticle` 时出错,请把相关信息提交给DedeCms官方.Duplicate entry ’3′ for key ‘PRIMARY’ 你的主键是不 ...
- PHP获取手机相关信息
该PHP操作类实现获取手机号手机头信息,取UA,取得手机类型,判断是否是opera,判断是否是m3gate,取得HA,取得手机IP 代码如下: <?php /** * @desc 手机操作类 获 ...
- 通过runtime获取对象相关信息
通过runtime获取对象相关信息 在这里,本人给大家提供一个runtime关于NSObject的扩展,用来显示各种NSObject中的信息,这有助于你来分析类的组成:) 先准备以下类供测试: Mod ...
- 获取IP相关信息和文件上传
获取IP相关信息 要获取用户访问者的IP地址相关信息,可以利用依赖注入,获取IHttpConnectionFeature的实例,从该实例上可以获取IP地址的相关信息,实例如下: var connect ...
随机推荐
- eclipse新建maven项目出错 pom.xml报错
问题: 1.新建项目后会提示一个这样的错 maven-compiler-plugin:3.1:compile(1 errors) maven-compiler-plugin:3.1:testCompi ...
- oracle存储过程调试-plsql
1.搜索找到存储过程的包,并打开 选中右击--view spec&body 2.找到要测试的存储过程,设置断点,进行测试
- spring-第七篇之深入理解容器中的bean
1.抽象bean与子bean 用于指定配置模板. 2.容器中的工厂bean 这种工厂bean必须实现FactoryBean接口,通过spring容器getBean()方法获取它时,容器返回的不是Fac ...
- [暑假集训Day1T2]北极通讯网络
这题主要考察对“卫星电话”的理解,k个卫星电话相当于可以让k个联通块保持联通,因此我们只需要让原图连成k个联通块,然后给每个联通块的任意一个节点发一部卫星电话即可.因此我们需要连n-k条边,特别地,当 ...
- P3773 [CTSC2017]吉夫特
传送门 看到组合数在模 $2$ 意义下的乘积,考虑用 $lucas$ 定理把组合数拆开 $lucas$ 告诉我们,$C(n,m)$ 在模 $k$ 意义下的值,相当于 $n,m$ 在 $k$ 进制下每一 ...
- vue-devtools工具的安装
vue-devtools是一款基于chrome游览器的插件,用于调试vue应用,这可以极大地提高我们的调试效率.接下来我们就介绍一下vue-devtools的安装 1.登录github,获取到vue- ...
- 更新252板子代码(前端+cgi中间件)
1.前端代码 前端的html.css.js代码,利用打包工具生成dist文件夹,放入lighttpd的指定目录. 2.cgi中间件 1)编译 1.下载代码工程V100R100C00 2.将工程代码以共 ...
- ES6——字符串
1.多了两个方法 1)startsWith 2)endsWith 2.模板字符串(`..`)—— 方便字符串连接 `反单引号 1)可以直接把表达式塞进去 &a ...
- 纯手动拉WebPanel页面保存出现错误提示 "error:字符文本中的字符太多"
环境为.Net 没有使用WorkWithPlus 纯手拉WebFrom页面 问题出现情况如下:在拉页面的过程中拖了3个Value类型的变量到Table中 页面如下 但是在我保存的时候 发现提示如下错 ...
- python关键字global和nonlocal总结
函数中使用全局变量 a = 100 b = 200 def func(): def sub(): return b return a + b + sub() 执行fun()后返回值为:500 a, b ...