Skip to content

Commit 63abc1b

Browse files
committed
Memory dumper to debug memory leak
The entry script is just a wrapper to find the pid The script with the actual implementation does not load lib/lib.sh because it has to be run as root and we want to run as little code as possible as root
1 parent 9cf6b88 commit 63abc1b

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

lib/_dump_memory.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/bash
2+
3+
server_pid="$1"
4+
5+
if [ "$server_pid" = "" ]
6+
then
7+
err "missing argument pid"
8+
exit 1
9+
fi
10+
if ! ps -p "$server_pid" > /dev/null
11+
then
12+
err "Server process not found. Is the server running?"
13+
exit 1
14+
fi
15+
16+
dump_dir=logs/dump_memory
17+
rm -rf "$dump_dir"
18+
mkdir -p "$dump_dir"
19+
20+
# https://serverfault.com/a/408929
21+
grep rw-p /proc/${server_pid}/maps \
22+
| sed -n 's/^\([0-9a-f]*\)-\([0-9a-f]*\) .*$/\1 \2/p' \
23+
| while read start stop; do \
24+
gdb --batch --pid "${server_pid}" -ex \
25+
"dump memory ${dump_dir}/${server_pid}-$start-$stop.dump 0x$start 0x$stop"; \
26+
done
27+
28+
echo "[*] dumped server memory to $dump_dir .. OK"

lib/dump_memory.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
3+
if [ ! -f lib/lib.sh ]
4+
then
5+
echo "Error: lib/lib.sh not found!"
6+
echo "make sure you are in the root of the server repo"
7+
exit 1
8+
fi
9+
10+
source lib/lib.sh
11+
12+
server_pid="$(pgrep -f "$SERVER_UUID")"
13+
14+
if [ "$server_pid" = "" ]
15+
then
16+
err "Server process not found. Is the server running?"
17+
exit 1
18+
fi
19+
20+
log "run the following command as root:"
21+
echo ""
22+
echo " ./lib/_dump_memory.sh $server_pid"
23+
echo ""

0 commit comments

Comments
 (0)