Skip to content

Instantly share code, notes, and snippets.

@diamantidis
Created October 14, 2018 06:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diamantidis/d95531fd571c360078fcc795d1967ded to your computer and use it in GitHub Desktop.
Save diamantidis/d95531fd571c360078fcc795d1967ded to your computer and use it in GitHub Desktop.
LLDB command to filter UIButtons by the label text
#!/usr/bin/python
'''
The MIT License (MIT)
Copyright (c) 2018 Ioannis Diamantidis @diamantidis
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
import lldb
import commands
import optparse
import shlex
import re
def create_options():
usage = "usage: filter_button_by_label [options]"
description='''
This command is used to find a UIButton with a label matching the option provided as option
'''
parser = optparse.OptionParser(description=description, prog='filter_button_by_label', usage=usage)
parser.add_option('-n', '--needle', type='string', dest='needle', help='Text to search on UIButton labels.')
return parser
def filter_button_by_label(debugger, command, result, internal_dict):
target = debugger.GetSelectedTarget()
process = target.GetProcess()
mainThread = process.GetThreadAtIndex(0)
currentFrame = mainThread.GetSelectedFrame()
# Parse arguments and options
command_args = shlex.split(command)
parser = create_options()
try:
(options, args) = parser.parse_args(command_args)
# if needle is not provided
if not options.needle:
parser.print_help()
return
except:
return
view_hierarchy_command = '(id)[[[UIApplication sharedApplication] keyWindow] recursiveDescription]'
view_hierarchy = currentFrame.EvaluateExpression(view_hierarchy_command).GetObjectDescription()
for match in re.finditer('.*<UIButton: (0x[0-9a-fA-F]*);.*', view_hierarchy, re.IGNORECASE):
view = match.groups()[-1]
created_command = '(NSString *)[ (id)' + view + ' currentTitle]'
title = currentFrame.EvaluateExpression(created_command).GetObjectDescription()
if title == options.needle:
print >>result, view
else:
print >>result, "Not Found"
def __lldb_init_module(debugger, internal_dict):
debugger.HandleCommand('command script add -f ' + __name__ + '.filter_button_by_label filter_button_by_label')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment