WSL下无法使用core dump的解决方案

我正在做一个小项目,使用的WSL,但是在学习gdb的时候遇到了一个问题,就是我的程序没有core dumped,对于以下c程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int actual_calc(int a, int b){
int c;
c=a/b;
return 0;
}

int calc(){
int a;
int b;
a=13;
b=0;
actual_calc(a, b);
return 0;
}

int main(){
calc();
return 0;
}

使用

1
gcc -ggdb test.c -o test.out

然后设定

1
2
3
4
5
if ! grep -qi 'kernel.core_pattern' /etc/sysctl.conf; then
sudo sh -c 'echo "kernel.core_pattern=core.%p.%u.%s.%e.%t" >> /etc/sysctl.conf'
sudo sysctl -p
fi
ulimit -c unlimited

然后添加以下进/etc/security/limits.conf

1
2
* soft core unlimited
* hard core unlimited

之后运行test.out,但是输出

1
Floating point exception

理论上应该有

1
Floating point exception(core dumped)

后面在WSL的issue里面找到了https://github.com/microsoft/WSL/issues/1262,通过设置

1
sudo sysctl -w kernel.core_pattern=/tmp/core

可以生成core文件(不然就是空的),现在可以看到core文件正常了

1
2
file /tmp/core.890
/tmp/core.890: ELF 64-bit LSB core file, x86-64, version 1 (SYSV), SVR4-style, from './test.out', real uid: 1002, effective uid: 1002, real gid: 1002, effective gid: 1002, execfn: './test.out', platform: 'x86_64'
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2015-2024 0375w