-
Notifications
You must be signed in to change notification settings - Fork 653
/
redex_gdb_hooks.py
170 lines (138 loc) · 5.43 KB
/
redex_gdb_hooks.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
# Defines GDB Pretty printing support for Redex
import gdb
class CallableBase(object):
def __init__(self, val, cmd, reftype):
self.val = val
self.cmd = cmd
self.reftype = reftype
def to_string(self):
if self.reftype and int(self.val) == 0:
return "NULL"
try:
res = gdb.execute(self.cmd, False, True)
# Print instead of returning the string to handle newlines
prncmd = 'call printf ("%s", "{0}\\n")'.format(
res.rstrip().replace('"', "")
)
gdb.execute(prncmd, False, True)
return ""
except BaseException:
return ""
def Show(CallableBase):
def printer(val):
cmd = "call('show({0} const)'({1}))".format(type, val)
return CallableBase(val, cmd, False)
return printer
def ShowDeref(type):
def printer(val):
cmd = "call('show({0} const*)'({1}))".format(type, hex(val))
return CallableBase(val, cmd, True)
return printer
def ShowDerefAsRef(type):
def printer(val):
cmd = "call('show({0} const&)'(*{1}))".format(type, hex(val))
return CallableBase(val, cmd, True)
return printer
def ShowRef(type):
def printer(val):
val = val.address
cmd = "call('show({0} const&)'(*{1}))".format(type, hex(val))
return CallableBase(val, cmd, True)
return printer
def ShowDeobfuscated(type):
def printer(val):
cmd = "call('show_deobfuscated({0} const*)'({1})".format(type, hex(val))
return CallableBase(val, cmd, True)
return printer
pretty_printers_dict = {
# type.unqualified() doesnt seem to remove "const" qualifier.
"cfg::ControlFlowGraph *": ShowDerefAsRef("cfg::ControlFlowGraph"),
"const cfg::ControlFlowGraph *": ShowDerefAsRef("cfg::ControlFlowGraph"),
"cfg::ControlFlowGraph &": ShowRef("cfg::ControlFlowGraph"),
"const cfg::ControlFlowGraph &": ShowRef("cfg::ControlFlowGraph"),
"cfg::Edge *": ShowDeref("cfg::Edge"),
"const cfg::Edge *": ShowDeref("cfg::Edge"),
"cfg::Block *": ShowDeref("cfg::Block"),
"const cfg::Block *": ShowDeref("cfg::Block"),
"DexAnnotation *": ShowDeref("DexAnnotation"),
"const DexAnnotation *": ShowDeref("DexAnnotation"),
"DexAnnotationDirectory *": ShowDeref("DexAnnotationDirectory"),
"const DexAnnotationDirectory *": ShowDeref("DexAnnotationDirectory"),
"DexAnnotationSet *": ShowDeref("DexAnnotationSet"),
"const DexAnnotationSet *": ShowDeref("DexAnnotationSet"),
"DexCode *": ShowDeref("DexCode"),
"const DexCode *": ShowDeref("DexCode"),
"DexFieldRef *": ShowDeref("DexFieldRef"),
"cost DexFieldRef *": ShowDeref("DexFieldRef"),
"DexField *": ShowDeref("DexField"),
"const DexField *": ShowDeref("DexField"),
"DexInstruction *": ShowDeref("DexInstruction"),
"const DexInstruction *": ShowDeref("DexInstruction"),
"DexOpcode": Show("DexOpcode"),
"const DexOpcode": Show("DexOpcode"),
"DexProto *": ShowDeref("DexProto"),
"const DexProto *": ShowDeref("DexProto"),
"DexString *": ShowDeref("DexString"),
"const DexString *": ShowDeref("DexString"),
"DexClass *": ShowDeref("DexClass"),
"const DexClass *": ShowDeref("DexClass"),
"DexMethodRef *": ShowDeref("DexMethodRef"),
"const DexMethodRef *": ShowDeref("DexMethodRef"),
"DexMethod *": ShowDeref("DexMethod"),
"const DexMethod *": ShowDeref("DexMethod"),
"DexType *": ShowDeref("DexType"),
"const DexType *": ShowDeref("DexType"),
"DexTypeList *": ShowDeref("DexTypeList"),
"const DexTypeList *": ShowDeref("DexTypeList"),
"IRInstruction *": ShowDeref("IRInstruction"),
"const IRInstruction *": ShowDeref("IRInstruction"),
# TODO printing IRCode seems to crash gdb
# "IRCode *": ShowDeref("IRCode"),
# "const IRCode *": ShowDeref("IRCode"),
"IRList *": ShowDeref("IRList"),
"const IRList *": ShowDeref("IRList"),
"IROpcode": Show("IROpcode"),
"const IROpcode": Show("IROpcode"),
}
def lookup_function(val):
if val is None or val.type is None:
return None
type = val.type
type_name = str(type.unqualified().strip_typedefs())
if type_name in pretty_printers_dict:
return pretty_printers_dict[type_name](val)
return None
def register_pretty_printer(obj):
if obj is None:
obj = gdb
obj.pretty_printers.insert(0, lookup_function)
def get_gdb_val_for_str(arg):
val = gdb.lookup_symbol(arg)
if not (val[0] is None):
frame = gdb.selected_frame()
return val[0].value(frame)
val = gdb.lookup_global_symbol(arg)
if not (val is None):
return val.value()
val = gdb.lookup_static_symbol(arg)
if not (val is None):
return val.value()
return None
class pp(gdb.Command):
def __init__(self):
gdb.Command.__init__(self, "pp", gdb.COMMAND_DATA, gdb.COMPLETE_SYMBOL, True)
def invoke(self, arg, from_tty):
val = get_gdb_val_for_str(arg)
printer = lookup_function(val)
if printer is None:
print(('No symbol "{0}" in current context'.format(arg)))
return
printer.to_string()
pp()
# register_pretty_printer(gdb.current_objfile())
print("Redex pretty printers added.")
print("Use custom command pp to print Redex symbols")