From 346637cb93ae39223f91f318bf265a1b7ba87abb Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Wed, 7 Sep 2022 16:01:12 +0200 Subject: add "Pause all userspace processes" --- .gitattributes | 2 ++ _posts/2022-09-07-gdb-sleep-all.md | 35 ++++++++++++++++++++++++++ _posts/snippets/gdb_sleep_all/gdb_sleep_all.sh | 17 +++++++++++++ _posts/snippets/gdb_sleep_all/sleep.gdb | 2 ++ 4 files changed, 56 insertions(+) create mode 100644 _posts/2022-09-07-gdb-sleep-all.md create mode 100755 _posts/snippets/gdb_sleep_all/gdb_sleep_all.sh create mode 100644 _posts/snippets/gdb_sleep_all/sleep.gdb diff --git a/.gitattributes b/.gitattributes index 176a458..d76765e 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1,3 @@ * text=auto + +*.sh text eol=lf diff --git a/_posts/2022-09-07-gdb-sleep-all.md b/_posts/2022-09-07-gdb-sleep-all.md new file mode 100644 index 0000000..b4915d3 --- /dev/null +++ b/_posts/2022-09-07-gdb-sleep-all.md @@ -0,0 +1,35 @@ +--- +title: Pause all userspace processes +excerpt: > + How do you pause all userspace processes for a certain amount of time? +snippets_root_directory: snippets/gdb_sleep_all +snippets_language: bash +snippets: + main: + - gdb_sleep_all.sh + gdb: + - sleep.gdb +--- +If you need to debug some kind of monitoring system (or just have some fun), +you might want to pause all userspace processes for a certain number of seconds +(to measure delays, etc.). + +You can easily do this using GDB like this: + +{% include jekyll-theme/snippets/section.html section_id='main' %} + +sleep.gdb is a very simple GDB script; it basically sleeps for a determined +amount of seconds: + +{% include jekyll-theme/snippets/section.html section_id='gdb' %} + +You can simply run + + sudo ./gdb_sleep_all.sh + +and all of your userspace processes should be frozen for 30 seconds. + +On a couple of servers, this worked quite well; not so well on my laptop with +Xfce installed. +Obviously, this would require a bit of work to adapt for containers as well. +Otherwise, pretty neat, huh? diff --git a/_posts/snippets/gdb_sleep_all/gdb_sleep_all.sh b/_posts/snippets/gdb_sleep_all/gdb_sleep_all.sh new file mode 100755 index 0000000..e923740 --- /dev/null +++ b/_posts/snippets/gdb_sleep_all/gdb_sleep_all.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +set -o errexit -o nounset -o pipefail + +# Select all process IDs that are _not_ children of PID 2, [kthreadd]. +pids="$( ps -o pid --no-headers --ppid 2 -p 2 --deselect )" + +for pid in $pids; do + cmdline="$( cat "/proc/$pid/cmdline" | tr '\0' ' ' )" || continue + echo ------------------------------------------------------------------ + echo "PID: $pid" + echo "Command line: $cmdline" + echo ------------------------------------------------------------------ + gdb -p "$pid" -x sleep.gdb -batch & +done + +wait diff --git a/_posts/snippets/gdb_sleep_all/sleep.gdb b/_posts/snippets/gdb_sleep_all/sleep.gdb new file mode 100644 index 0000000..04f17fc --- /dev/null +++ b/_posts/snippets/gdb_sleep_all/sleep.gdb @@ -0,0 +1,2 @@ +shell sleep 30 +quit -- cgit v1.2.3