Skip to content

Instantly share code, notes, and snippets.

@dominik-hadl
Last active December 20, 2015 06:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dominik-hadl/6083359 to your computer and use it in GitHub Desktop.
Save dominik-hadl/6083359 to your computer and use it in GitHub Desktop.
This script automates the setup of gdb on a stripped applicaiton adds symbols. It uses objc-symbols to get the symbols, then SymTabCreator and finally creates a command that is automatically loaded into gdb on start.
#!/bin/sh
# gdbGetStrippedSymbols.sh
# @author Dominik Hadl (@dominikhadl)
# @description This script automates the setup of gdb on a stripped applicaiton adds symbols. It uses objc-symbols to get the symbols,
# then SymTabCreator and finally creates a command that is automatically loaded into gdb on start.
# @license Licensed under WTFPL license (see http://www.wtfpl.net/txt/copying/ for full license).
# @dependencies
# 1. objc-symbols
# 2. SymTabCreator
# 3. gdb (really non-obvious)
# @usage This script can by used by running this command.
# gdb_symbols_script.sh <application>
############################################################
################################
# Helpers
function usage
{
DDP_FILENAME=`basename ${0##*/}`
echo "usage: ${DDP_FILENAME} <application>"
}
function dependencyIsMissing
{
if [[ $# == 1 ]]; then
echo "Dependency (${1}) is missing. Please install it - preferably into /usr/bin."
fi
exit 1
}
################################
## Main Execution
################################
# Check if there is a correct number of arguments
if [[ $# == 1 ]]; then
# Check if specified file exists
if [ -e "$1" ]; then
# Check if commands/dependencies exists
# 1. gdb (essential)
# 2. class-dump (almost essential)
# 3. objc-symbols (class-dump extension)
# 4. SymTabCreator (for loading symbols into gdb)
command -v gdb > /dev/null || dependencyIsMissing "gdb"
command -v objc-symbols > /dev/null || dependencyIsMissing "objc-symbols"
command -v SymTabCreator > /dev/null || dependencyIsMissing "SymTabCreator"
# Create Variables
# 1. Set temporary directory path with .stabs and gdb commands
# 2. Get the file name without extension
# 3. Create the path for the .stabs file in temporary dir
DDP_TMPDIR="/tmp/ddp_gdbcommand"
DDP_FILENAME=`basename "${1%.*}"`
DDP_SYMBOL_FILE="${DDP_TMPDIR}/${DDP_FILENAME}.stabs"
# Execute commands
# 1. Create the temporary directory
# 2. Create the symbolic table
# 3. Save commands for gdb to file
mkdir "${DDP_TMPDIR}"
objc-symbols "${1}" | SymTabCreator -o "$DDP_SYMBOL_FILE"
echo "symbol-file ${DDP_SYMBOL_FILE}" > "${DDP_TMPDIR}/gdbcommands"
# Start gdb with the symbol file
gdb --silent --command="${DDP_TMPDIR}/gdbcommands" "${1}"
# Cleanup
# Delete the temporary directory including its contents
rm -rf "${DDP_TMPDIR}"
else
echo "File does not exist. Please provide a valid file."
exit 1
fi
elif [[ $# > 1 ]]; then
# Too many arguments specified
echo "Too many arguments."
usage
exit 1
else
# No argument specified
usage
exit 1
fi
@boredzo
Copy link

boredzo commented Jul 25, 2013

Any reason you didn't use DDP_TMPDIR on lines 69 and 72?

@tyilo
Copy link

tyilo commented Jul 25, 2013

Why is class-dump a requirement when it isn't used anywhere?

@dominik-hadl
Copy link
Author

That's what I get when I am working on a script at 3AM. :D
@boredzo Thanks for noticing... maybe I should re-read the script after I'm done with it.
@tyilo I thought that the objc-symbols uses class-dump in someway, it turns out it doesn't.
It should be fixed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment