Skip to content

Instantly share code, notes, and snippets.

@SamClewlow
Created April 17, 2017 11:00
Show Gist options
  • Save SamClewlow/8b8dd138ef37611f56b654417265ad48 to your computer and use it in GitHub Desktop.
Save SamClewlow/8b8dd138ef37611f56b654417265ad48 to your computer and use it in GitHub Desktop.
A python lldb extension for easier casting of pointers to Swift types when debugging
#!/usr/bin/python
import lldb
import commands
import optparse
import shlex
import string
def create_options_parser():
usage = "usage: %prog [options]"
description = '''This command takes a variable name, memory address and type name to cast to the correct Swift object type'''
parser = optparse.OptionParser(description=description, prog='test', usage=usage)
parser.add_option('-v', '--varname', type='string', dest='var_name', help='name for returned variable')
parser.add_option('-a', '--address', type='string', dest='address', help='memory address of the object')
parser.add_option('-t', '--type', type='string', dest='target', help='Type to case object to')
return parser
def cast_to_type(debugger, command, result, internal_dict):
#debugger.HandleCommand('pvc')
command_args = shlex.split(command)
parser = create_options_parser()
try:
(options, args) = parser.parse_args(command_args)
except:
result.setError("option parsing failed")
var_name = options.var_name
addr = options.address
target = options.target
new_command = string.Template("expr -l Swift -- let $var_name = unsafeBitCast($addr, to: $target.self)").substitute(**locals())
print new_command
debugger.HandleCommand(new_command)
def __lldb_init_module(debugger, internal_dict):
debugger.HandleCommand('command script add -f test.cast_to_type cast_to_type')
print 'The "cast_to_type" python command has been installed and is ready for use.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment