|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Tests the gdb feature of hyperlight by running a simple program |
| 4 | +# and adding a breakpoint to it. |
| 5 | + |
| 6 | +set -o errexit |
| 7 | +set -o nounset |
| 8 | +set -o pipefail |
| 9 | + |
| 10 | +wait_for_pids() { |
| 11 | + local pids=("$@") |
| 12 | + local timeout=3 |
| 13 | + local elapsed=0 |
| 14 | + local interval=1 |
| 15 | + |
| 16 | + while true; do |
| 17 | + all_done=true |
| 18 | + for pid in "${pids[@]}"; do |
| 19 | + if ps -p "$pid" 2>/dev/null; then |
| 20 | + all_done=false |
| 21 | + fi |
| 22 | + done |
| 23 | + |
| 24 | + if [ "$all_done" = true ]; then |
| 25 | + return 0 |
| 26 | + fi |
| 27 | + |
| 28 | + sleep "$interval" |
| 29 | + elapsed=$((elapsed + interval)) |
| 30 | + |
| 31 | + if [ "$elapsed" -ge "$timeout" ]; then |
| 32 | + return 1 |
| 33 | + fi |
| 34 | + |
| 35 | + done |
| 36 | +} |
| 37 | + |
| 38 | +# Run the example |
| 39 | +cargo run --example guest-debugging --features gdb & |
| 40 | +TARGET_PID=$! |
| 41 | + |
| 42 | +# Wait for the program to start |
| 43 | +sleep 1 |
| 44 | + |
| 45 | +# GDB commands to run |
| 46 | +GDB_COMMANDS=$(cat <<EOF |
| 47 | +file src/tests/rust_guests/bin/debug/simpleguest |
| 48 | +target remote :8080 |
| 49 | +
|
| 50 | +set pagination off |
| 51 | +set logging file gdb.output |
| 52 | +set logging enabled on |
| 53 | +
|
| 54 | +break hyperlight_main |
| 55 | + commands |
| 56 | + echo "Stopped at hyperlight_main breakpoint\n" |
| 57 | + backtrace |
| 58 | + continue |
| 59 | +end |
| 60 | +
|
| 61 | +continue |
| 62 | +
|
| 63 | +set logging enabled off |
| 64 | +quit |
| 65 | +EOF |
| 66 | +) |
| 67 | + |
| 68 | +# Write the gdb commands to a file |
| 69 | +echo "$GDB_COMMANDS" > gdb-commands.txt |
| 70 | + |
| 71 | +# Run gdb headless with the commands |
| 72 | +gdb --nw -x ./gdb-commands.txt & |
| 73 | +GDB_PID=$! |
| 74 | + |
| 75 | +sleep 1 |
| 76 | + |
| 77 | +echo "Waiting for run to finish" |
| 78 | +if wait_for_pids "$TARGET_PID" "$GDB_PID"; then |
| 79 | + echo "Run finished successfully" |
| 80 | +else |
| 81 | + echo "GDB did not finish in time" |
| 82 | + |
| 83 | + for pid in "$TARGET_PID" "$GDB_PID"; do |
| 84 | + if ps -p "$pid" 2>/dev/null; then |
| 85 | + echo "Process $pid did not finish in time, killing it" |
| 86 | + pkill --parent "$pid" |
| 87 | + pkill "$pid" |
| 88 | + fi |
| 89 | + done |
| 90 | + |
| 91 | + exit 1 |
| 92 | +fi |
| 93 | + |
| 94 | +# Check the output |
| 95 | +if ! grep -q "Stopped at hyperlight_main breakpoint" gdb.output; then |
| 96 | + echo "GDB did not stop at the breakpoint" |
| 97 | + exit 1 |
| 98 | +fi |
0 commit comments