Skip to content

Instantly share code, notes, and snippets.

@kakash1hatake
Last active May 16, 2020 07:46
Show Gist options
  • Save kakash1hatake/75b93be39c432ebf367910a7228cf6d9 to your computer and use it in GitHub Desktop.
Save kakash1hatake/75b93be39c432ebf367910a7228cf6d9 to your computer and use it in GitHub Desktop.
get env variable of the particular process using gdb
#!/bin/sh
#
# get env variable of the particular process. It can show variables that were created at runtime. Requires root privileges
# usage: sudo penv_runtime.sh pid var_name
#
pid=$1
var_name=$2
var_value=`gdb -q -batch -ex "attach $pid" -ex 'call (char*) getenv("'$var_name'")' -ex 'detach' | egrep '^\$1 ='`
if [ "$var_value" == '$1 = 0x0' ]
then
# variable empty or does not exist
echo -n
else
# gdb returns $1 = hex_value "string_value"
var_hex=`echo "$var_value" | awk '{print $3}'`
var_value=`echo "$var_value" | sed -r -e 's/^\$1 = '$var_hex' //;s/^"//;s/"$//'`
echo -n "$var_value"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment