-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp-valgrind.sh
More file actions
executable file
·143 lines (132 loc) · 3.46 KB
/
cpp-valgrind.sh
File metadata and controls
executable file
·143 lines (132 loc) · 3.46 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/bin/bash
# cpp-valgrind.sh
# Simple script to run C++ exercises with Valgrind in Podman
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# Function to start Podman
start_podman() {
echo -e "${BLUE}Starting Podman machine...${NC}"
if ! podman machine list | grep -q "Currently running"; then
podman machine start
echo -e "${GREEN}Podman started.${NC}"
else
echo -e "${YELLOW}Podman already running.${NC}"
fi
}
# Function to stop Podman
stop_podman() {
echo -e "${BLUE}Stopping Podman machine...${NC}"
podman machine stop
echo -e "${GREEN}Podman stopped.${NC}"
}
# Function to run program normally
run_normal() {
echo -e "${BLUE}Running program normally...${NC}"
podman run --rm -it \
-v "$(pwd):/workspace" \
-w /workspace \
gcc:latest \
bash -c "make re && ./$EXEC_NAME"
}
# Function to run with Valgrind
run_valgrind() {
echo -e "${BLUE}Running with Valgrind...${NC}"
podman run --rm -it \
-v "$(pwd):/workspace" \
-w /workspace \
gcc:latest \
bash -c "apt-get update -qq && apt-get install -y -qq valgrind && make re && valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes ./$EXEC_NAME"
}
# Show usage
show_usage() {
echo -e "${BLUE}Usage:${NC} $0 [COMMAND] [EXECUTABLE_NAME]"
echo
echo "Commands:"
echo " run [name] - Run executable normally (default: uses NAME from Makefile)"
echo " valgrind [name]- Run with Valgrind"
echo " start - Start Podman machine"
echo " stop - Stop Podman machine"
echo " status - Check Podman status"
echo " help - Show this help"
echo
echo "Examples:"
echo " $0 run zombie"
echo " $0 valgrind zombie"
echo " $0 stop"
}
# Get executable name from Makefile or argument
get_exec_name() {
if [ -n "$1" ]; then
echo "$1"
elif [ -f "Makefile" ]; then
grep -E "^NAME\s*=" Makefile | head -1 | cut -d'=' -f2 | tr -d ' '
else
echo "a.out"
fi
}
# Check Podman status
check_status() {
echo -e "${BLUE}Podman machine status:${NC}"
podman machine list
}
# Main
case "$1" in
start)
start_podman
;;
stop)
stop_podman
;;
status)
check_status
;;
run)
EXEC_NAME=$(get_exec_name "$2")
echo -e "${BLUE}Executable: $EXEC_NAME${NC}"
start_podman
run_normal
;;
valgrind)
EXEC_NAME=$(get_exec_name "$2")
echo -e "${BLUE}Executable: $EXEC_NAME${NC}"
start_podman
run_valgrind
;;
help|--help|-h)
show_usage
;;
*)
EXEC_NAME=$(get_exec_name "$1")
echo -e "${BLUE}Executable: $EXEC_NAME${NC}"
echo -e "${BLUE}How would you like to run?${NC}"
echo "1) Normal mode"
echo "2) Valgrind"
echo "3) Just start Podman"
echo "4) Stop Podman"
read -p "Enter choice (1-4): " choice
case $choice in
1)
start_podman
run_normal
;;
2)
start_podman
run_valgrind
;;
3)
start_podman
;;
4)
stop_podman
;;
*)
echo -e "${RED}Invalid choice.${NC}"
show_usage
;;
esac
;;
esac