Skip to content

Instantly share code, notes, and snippets.

@lilyball
Created January 3, 2017 19:49
Show Gist options
  • Save lilyball/4ae544382622dc84bfc690e068beb66d to your computer and use it in GitHub Desktop.
Save lilyball/4ae544382622dc84bfc690e068beb66d to your computer and use it in GitHub Desktop.
# ...
command script import ~/path/to/viewControllers.py
# ...
#!/usr/bin/python
import lldb
import shlex
#import argparse
#def create_argument_parser():
#description = '''With no arguments, print the view controller hierarchy.'''
#parser = argparse.ArgumentParser(description=descriptoin, prog='viewControllers')
#parser.add_argument('-l', '--limit-to-view', dest='view', metavar='address', help='limit controllers to the responder chain for this view')
#return parser
def viewControllers(debugger, command, result, internal_dict):
#parser = create_argument_parser()
#try:
#args = parser.parse_args(shlex.split(command))
#except SystemExit:
## bad arguments cause ArgumentParser to throw SystemExit
#result.SetError('argument parsing failed')
#return
args = shlex.split(command)
if len(args) != 1:
result.SetError('expected one argument')
return
target = debugger.GetSelectedTarget()
code = '''
import UIKit
var results: [UIViewController] = []
for resp in sequence(first: unsafeBitCast('''+args[0]+''', to: UIResponder.self), next: { $0.next }) {
if let vc = resp as? UIViewController {
results.append(vc)
}
}
results'''
opts = lldb.SBExpressionOptions()
opts.SetLanguage(lldb.eLanguageTypeSwift)
opts.SetUnwindOnError(True)
opts.SetTimeoutInMicroSeconds(5000000) # 5 seconds
val = target.EvaluateExpression(code, opts)
if val.error.fail:
result.SetError(val.error, "unknown error")
return
if val.num_children:
for child in val:
print >>result, "{} {}".format(child.name, child.description.strip())
else:
print >>result, "no results"
def __lldb_init_module(debugger, internal_dict):
#parser = create_argument_parser()
#viewControllers.__doc__ = parser.format_help()
viewControllers.__doc__ = '''Usage: viewControllers <address>
Prints the view controllers in the responder chain for the view identified by <address>.'''
debugger.HandleCommand('command script add -f viewControllers.viewControllers viewControllers')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment