xref: /llvm-project/lldb/test/API/functionalities/postmortem/elf-core/make-core.sh (revision 99451b4453688a94c6014cac233d371ab4cc342d)
1#! /bin/sh
2
3linux_check_core_pattern()
4{
5    if grep -q '^|' </proc/sys/kernel/core_pattern; then
6        cat <<EOF
7Your system uses a crash report tool ($(cat /proc/sys/kernel/core_pattern)). Core files
8will not be generated.  Please reset /proc/sys/kernel/core_pattern (requires root
9privileges) to enable core generation.
10EOF
11        exit 1
12    fi
13}
14
15OS=$(uname -s)
16case "$OS" in
17FreeBSD)
18    core_pattern=$(sysctl -n kern.corefile)
19    ;;
20Linux)
21    core_pattern=$(cat /proc/sys/kernel/core_pattern)
22    ;;
23*)
24    echo "OS $OS not supported" >&2
25    exit 1
26    ;;
27esac
28
29set -e -x
30
31file=$1
32if [ -z "$file" ]; then
33    cat <<EOF
34Please supply the main source file as the first argument.
35EOF
36    exit 1
37fi
38
39if [ "$OS" = Linux ]; then
40    linux_check_core_pattern
41fi
42
43ulimit -c 1000
44real_limit=$(ulimit -c)
45if [ $real_limit -lt 100 ]; then
46    cat <<EOF
47Unable to increase the core file limit. Core file may be truncated!
48To fix this, increase HARD core file limit (ulimit -H -c 1000). This may require root
49privileges.
50EOF
51fi
52
53${CC:-cc} -nostdlib -static -g $CFLAGS "$file" -o a.out
54
55cat <<EOF
56Executable file is in a.out.
57Core file will be saved according to pattern $core_pattern.
58EOF
59
60ulimit -s 8 # Decrease stack size to 8k => smaller core files.
61exec ./a.out
62