-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiff.py
173 lines (132 loc) · 5.92 KB
/
diff.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
171
172
173
import gdb
import gdb.types
import gdb.printing
import gdb.command
import os
import tempfile
from collections import namedtuple
TrackedValue = namedtuple('TrackedValue',
['gdb_value', 'print_func', 'id',
'values_history', 'file_prev', 'file_cur'])
class DiffCommand(gdb.Command):
'''
Use this command to see the changes in text representation
of specified expression.
List of diff subcommands:
diff add - expression print_function identifier
Will create two files with previous and current text
representation of the specified expression.
print_function - existing function which takes one
argument of the same type as expression
diff remove - identifier
'''
ACTION_ADD = "add"
ACTION_REMOVE = "remove"
def __init__(self):
super(DiffCommand, self).__init__("diff",
gdb.COMMAND_DATA,
gdb.COMPLETE_SYMBOL)
self.dont_repeat()
self._event_handler_registered = False
self._tracked_values = []
def invoke(self, arg, from_tty):
"""
Called by GDB whenever this command is invoked.
"""
args = gdb.string_to_argv(arg)
action_type = str(args[0])
if action_type == self.ACTION_ADD:
if len(args) != 4:
gdb.write(
"Incorrect number of arguments, expected 4", gdb.STDOUT)
var_name = str(args[1])
print_func = str(args[2])
identifier = str(args[3])
try:
gdb_value = gdb.parse_and_eval(var_name)
path_prev = os.path.join(
tempfile.gettempdir(), "gdb_diff_" + identifier + "_PREV.txt")
path_cur = os.path.join(tempfile.gettempdir(
), "gdb_diff_" + identifier + "_CURRENT.txt")
try:
file_prev = open(path_prev, "w")
file_cur = open(path_cur, "w")
except IOError:
gdb.write("Cannot open \"{}\" or \"{}\" for writing".format(
path_prev, path_cur), gdb.STDOUT)
return
tracked_value = TrackedValue(gdb_value=gdb_value,
print_func=print_func,
id=identifier,
values_history=[],
file_prev=file_prev,
file_cur=file_cur)
self._tracked_values.append(tracked_value)
gdb.write("Tracking \"{}\", use your favourite diff application for:\n".format(
var_name), gdb.STDOUT)
gdb.write("\t{}\n".format(path_prev), gdb.STDOUT)
gdb.write("\t{}\n".format(path_cur), gdb.STDOUT)
self.eval_value(tracked_value)
except RuntimeError:
gdb.write("\"{}\" is invalid variable to watch".format(
var_name), gdb.STDOUT)
return
elif action_type == self.ACTION_REMOVE:
if len(args) != 2:
gdb.write(
"Incorrect number of arguments, expected 2", gdb.STDOUT)
identifier = str(args[1])
idx = next((i for i, x in enumerate(
self._tracked_values) if x.id == identifier), None)
if idx is None:
gdb.write("Unknown identifier \"{}\"".format(
identifier), gdb.STDOUT)
else:
self._tracked_values[idx].file_prev.close()
self._tracked_values[idx].file_cur.close()
del self._tracked_values[idx]
gdb.write("Removed \"{}\"".format(identifier), gdb.STDOUT)
else:
gdb.write("Unknown action {}".format(action_type), gdb.STDOUT)
return
if not self._event_handler_registered:
gdb.events.stop.connect(self.stop_handler)
gdb.events.exited.connect(self.exit_handler)
self._event_handler_registered = True
def stop_handler(self, event):
"""
The debugger has stopped (e.g. a breakpoint was hit).
"""
for tracked_value in self._tracked_values:
self.eval_value(tracked_value)
def exit_handler(self, event):
for tracked_value in self._tracked_values:
tracked_value.file_prev.close()
tracked_value.file_cur.close()
self._tracked_values.clear()
def eval_value(self, tracked_value):
eval_expr = "{} (({}) {})".format(tracked_value.print_func,
str(tracked_value.gdb_value.type),
str(int(tracked_value.gdb_value)))
eval_result_str = None
try:
eval_result = gdb.parse_and_eval(eval_expr)
eval_result_str = eval_result.string()
except RuntimeError:
gdb.write("Unable to evaluate \"{}\"".format(
eval_expr), gdb.STDOUT)
return
if eval_result_str is not None:
if len(tracked_value.values_history) == 0 or tracked_value.values_history[-1] != eval_result_str:
tracked_value.values_history.append(eval_result_str)
if len(tracked_value.values_history) > 1:
tracked_value.file_prev.seek(0)
tracked_value.file_prev.write(
tracked_value.values_history[-2])
tracked_value.file_prev.truncate()
tracked_value.file_prev.flush()
tracked_value.file_cur.seek(0)
tracked_value.file_cur.write(tracked_value.values_history[-1])
tracked_value.file_cur.truncate()
tracked_value.file_cur.flush()
DiffCommand()