-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbuild-backtrace.sh
More file actions
executable file
·114 lines (93 loc) · 4.88 KB
/
build-backtrace.sh
File metadata and controls
executable file
·114 lines (93 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env bash
# Build and run the backtrace example on spike emulator.
#
# This example demonstrates panic handling with stack traces.
# The output should show the panic message and (with symbols) a backtrace.
# Note: The example intentionally panics, so both runs will exit non-zero.
set -euo pipefail
export RUSTUP_NO_UPDATE_CHECK=1
PROFILE="dev"
ROOT="$(git rev-parse --show-toplevel 2>/dev/null || (cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd))"
cd "${ROOT}"
OUT_NOSTD="$(mktemp)"
OUT_STD="$(mktemp)"
trap 'rm -f "${OUT_NOSTD}" "${OUT_STD}"' EXIT
# no-std mode
echo "Building backtrace example in no-std mode ..."
TARGET_TRIPLE="riscv64imac-unknown-none-elf"
OUT_DIR="${ROOT}/target/${TARGET_TRIPLE}/$([ "$PROFILE" = "dev" ] && echo debug || echo "$PROFILE")"
BIN="${OUT_DIR}/backtrace"
# Use frame pointer walking for no-std mode (lightweight)
cargo spike build -p backtrace --target "${TARGET_TRIPLE}" --backtrace=frame-pointers -- --quiet --features=with-spike --profile "${PROFILE}"
echo "Running backtrace example (no-std) - expect panic ..."
# The example intentionally panics, so we capture exit code but continue
# Use --symbolize-backtrace to resolve addresses to function names on the host
cargo spike run "${BIN}" --isa RV64IMAC --instructions 10000000 --symbolize-backtrace 2>&1 | tee "${OUT_NOSTD}" || true
# Verify panic message and backtrace appears
grep -q "intentional panic for backtrace demo" "${OUT_NOSTD}"
grep -q "stack backtrace:" "${OUT_NOSTD}"
echo "no-std + frame-pointers test PASSED"
echo ""
# no-std mode with backtrace off
echo "Building backtrace example in no-std mode (off) ..."
cargo spike build -p backtrace --target "${TARGET_TRIPLE}" --backtrace=off -- --quiet --features=with-spike --profile "${PROFILE}"
echo "Running backtrace example (no-std + off) - expect panic without backtrace ..."
OUT_NOSTD_OFF="$(mktemp)"
trap 'rm -f "${OUT_NOSTD}" "${OUT_STD}" "${OUT_NOSTD_OFF}"' EXIT
cargo spike run "${BIN}" --isa RV64IMAC --instructions 10000000 2>&1 | tee "${OUT_NOSTD_OFF}" || true
grep -q "intentional panic for backtrace demo" "${OUT_NOSTD_OFF}"
# Verify NO backtrace
if grep -q "stack backtrace:" "${OUT_NOSTD_OFF}"; then
echo "ERROR: backtrace appeared in off mode!"
exit 1
fi
echo "no-std + off test PASSED (no backtrace as expected)"
echo ""
# std mode
echo ""
echo "Building backtrace example in std mode ..."
TARGET_TRIPLE="riscv64imac-zero-linux-musl"
OUT_DIR="${ROOT}/target/${TARGET_TRIPLE}/$([ "$PROFILE" = "dev" ] && echo debug || echo "$PROFILE")"
BIN="${OUT_DIR}/backtrace"
cargo spike build -p backtrace --target "${TARGET_TRIPLE}" --mode std --backtrace=dwarf -- --quiet --features=std,with-spike --profile "${PROFILE}"
echo "Running backtrace example (std) - expect panic ..."
# The example intentionally panics, so we capture exit code but continue
cargo spike run "${BIN}" --isa RV64IMAC --instructions 100000000 --symbolize-backtrace 2>&1 | tee "${OUT_STD}" || true
# Verify panic message appears
grep -q "intentional panic for backtrace demo" "${OUT_STD}"
echo "std backtrace test PASSED"
echo ""
echo ""
echo "Building backtrace example in std mode (frame-pointers) ..."
cargo spike build -p backtrace --target "${TARGET_TRIPLE}" --mode std --backtrace=frame-pointers -- --quiet --features=std,with-spike --profile "${PROFILE}"
echo "Running backtrace example (std + frame-pointers) - expect panic ..."
OUT_STD_FP="$(mktemp)"
trap 'rm -f "${OUT_NOSTD}" "${OUT_STD}" "${OUT_NOSTD_OFF}" "${OUT_STD_FP}"' EXIT
cargo spike run "${BIN}" --isa RV64IMAC --instructions 100000000 --symbolize-backtrace 2>&1 | tee "${OUT_STD_FP}" || true
grep -q "intentional panic for backtrace demo" "${OUT_STD_FP}"
# Note: std + frame-pointers doesn't print backtrace with default panic hook
# (Rust's std::backtrace requires DWARF; would need custom panic hook for frame-pointers)
# Just verify binary builds and runs
grep -q "stack backtrace:" "${OUT_STD_FP}"
echo "std + frame-pointers backtrace test PASSED"
echo ""
echo "Building backtrace example in std mode (off) ..."
cargo spike build -p backtrace --target "${TARGET_TRIPLE}" --mode std --backtrace=off -- --quiet --features=std,with-spike --profile "${PROFILE}"
echo "Running backtrace example (std + off) - expect panic without backtrace ..."
OUT_STD_OFF="$(mktemp)"
trap 'rm -f "${OUT_NOSTD}" "${OUT_STD}" "${OUT_STD_FP}" "${OUT_STD_OFF}"' EXIT
cargo spike run "${BIN}" --isa RV64IMAC --instructions 100000000 --symbolize-backtrace 2>&1 | tee "${OUT_STD_OFF}" || true
grep -q "intentional panic for backtrace demo" "${OUT_STD_OFF}"
# Verify NO backtrace appears
if grep -q "stack backtrace:" "${OUT_STD_OFF}"; then
echo "ERROR: backtrace appeared in off mode!"
exit 1
fi
echo "std + off mode test PASSED (no backtrace as expected)"
echo ""
echo "All backtrace tests completed successfully."
echo " - no-std + frame-pointers: ✓"
echo " - no-std + off: ✓"
echo " - std + dwarf: ✓"
echo " - std + frame-pointers: ✓"
echo " - std + off: ✓"