Skip to content

Instantly share code, notes, and snippets.

@gniemann
Created July 18, 2018 14:52
Show Gist options
  • Save gniemann/16d72d7f70bfe5ff24e5d135699f8f63 to your computer and use it in GitHub Desktop.
Save gniemann/16d72d7f70bfe5ff24e5d135699f8f63 to your computer and use it in GitHub Desktop.
LLDB command for identifying views in a broken constraint.
'''
LLDB commands for debugging iOS projecs in XCode
References:
* [LLDB Python reference](https://lldb.llvm.org/python-reference.html)
* [LLDB Python API[(https://lldb.llvm.org/python_reference/index.html)
To install, put this in your ~/.iidbinit:
command script import [path to this file]
'''
import inspect
import sys
from string import Template
# add LLDB library location to the path so it can be imported
sys.path += ':/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Resources/Python/'
import lldb
def border_misconstrained_view(debugger, command, exe_ctx, result, internal_dict):
'''
Install this as a breakpoint for UIViewAlertForUnsatisfiableConstraints
Examines the constraint defined by $arg1, which is being broken due to unsatisfiable constraints.
Draws a thick red border around the first view of the constraint, and a thick blue border around
the second view.
'''
frame = exe_ctx.GetFrame()
if not frame.IsValid():
print "Unable to get frame"
result.SetError('invalid frame')
return
arg1 = frame.FindRegister('x0').GetValue()
if arg1 is None:
# possibly simulator - try rcx
arg1 = frame.FindRegister('rcx').GetValue()
template = '''
import UIKit
let constraint = unsafeBitCast($addr, to: NSLayoutConstraint.self)
if let firstView = constraint.firstItem as? UIView {
firstView.layer.borderColor = UIColor.red.cgColor
firstView.layer.borderWidth = 4
}
if let secondView = constraint.secondItem as? UIView {
secondView.layer.borderColor = UIColor.blue.cgColor
secondView.layer.borderWidth = 4
}
'''
swift_code = Template(template).substitute(addr=arg1)
debugger.HandleCommand('e -l swift -- {}'.format(swift_code))
result.AppendMessage('A red border has been applied to the mis-constrained view')
result.Succeeded()
def __lldb_init_module(debugger, internal_dict):
'''
Runs when this script is imported into LLDB. Installs all functions into LLDB
'''
for (name, func) in inspect.getmembers(sys.modules[__name__], inspect.isfunction):
args = inspect.getargspec(func)[0]
print(args)
if args == ['debugger', 'command', 'exe_ctx', 'result', 'internal_dict']:
debugger.HandleCommand('command script add -f {}.{} {}'.format(__name__, name, name))
print('{} has been installed'.format(name))
@gniemann
Copy link
Author

Also check out Chisel

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