-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextensions.py
More file actions
105 lines (78 loc) · 3.55 KB
/
extensions.py
File metadata and controls
105 lines (78 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import lldb
import os.path
import re
import uuid
def __lldb_init_module(debugger, python_context):
module_name = __name__
debugger.HandleCommand(
'command script add -f %s.PrintJSONForDictionary jsonPrint' % module_name)
debugger.HandleCommand(
'command script add -c %s.SaveImageToDesktop saveImage' % module_name)
def PrintJSONForDictionary(debugger, command, execution_context, output, python_context):
# Get language for compile unit (file)
language = execution_context.frame.compile_unit.GetLanguage()
dictionaryExpression = None
# If language is Swift, use expression straightaway
# otherwise get address for object
if language == lldb.eLanguageTypeSwift:
dictionaryExpression = command
else:
inputValueAddress = evaluateInCurrentContext(command).value
dictionaryExpression = "unsafeBitCast(%s, to: NSDictionary.self)" % inputValueAddress
stringFromDict = """
import Foundation
let options = JSONSerialization.WritingOptions.init(rawValue: 1)
let jsonData = JSONSerialization.data(withJSONObject: %s, options: options)
String(data: jsonData, encoding: String.Encoding.utf8)!
""" % (dictionaryExpression)
# Evaluate dictionary result
result = evaluateInSwiftContext(stringFromDict).description
# Remove leading and trailing quote
result = re.findall(r'^\"(.+)\"$', result, re.MULTILINE)[0]
# Unescape quotes
resultUnescaped = result.replace('\\\"', '"').replace('\\n', '\n')
print >>output, resultUnescaped
class SaveImageToDesktop:
def __init__(self, debugger, python_context):
print 'The "%s" from %s is loaded' % (self.__class__.__name__, __file__)
def __call__(self, debugger, command, execution_context, output):
# Get address of the result of evaluating expression
inputValueAddress = evaluateInCurrentContext(command).value
# Store image as file in /tmp folder
saveImageToTmp = """
import Foundation
let image = unsafeBitCast(%s, to: UIImage.self)
let imageData = UIImagePNGRepresentation(image)
let path = NSTemporaryDirectory().appending("img_tmp.png")
try! imageData.write(to: URL(fileURLWithPath: path))
path
""" % (inputValueAddress)
# Set src and destination paths
fromPath = evaluateInSwiftContext(saveImageToTmp).summary
pathToSave = "~/Desktop/lldb_stored_images/lldb_%s.png" % uuid.uuid4()
toPath = os.path.expanduser(pathToSave)
debugger.HandleCommand("platform get-file %s %s" % (fromPath, toPath))
def get_short_help(self):
return "Store UIImage to ~/Desktop/lldb_stored_images/ with random name"
def get_long_help(self):
return "saveImage <UIImageVariableOrAddress> \nCommand loaded from %s" % __file__
def evaluateInCurrentContext(expr):
# expression -- <expr>
return _evaluateInContext(expr)
def evaluateInObjcContext(expr):
# expression -l objc -- <expr>
return _evaluateInContext(expr, lldb.eLanguageTypeObjC)
def evaluateInSwiftContext(expr):
# expression -l swift -- <expr>
return _evaluateInContext(expr, lldb.eLanguageTypeSwift)
def _evaluateInContext(expr, language=None):
options = lldb.SBExpressionOptions()
if language != None:
options.SetLanguage(language)
target = lldb.debugger.GetSelectedTarget()
currentFrame = target.GetProcess().GetSelectedThread().GetSelectedFrame()
value = currentFrame.EvaluateExpression(expr, options) # SBValue
error = value.error
if error.Fail():
print error
return value