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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
# Copyright (C) 2022 Free Software Foundation, Inc.
#
# This file is part of GNU Emacs.
#
# GNU Emacs is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# GNU Emacs is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
# Load this module in LLDB with
#
# (lldb) command script import emacs_lldb
#
# Available commands start with 'x' and can be seen with
#
# (lldb) help
import lldb
########################################################################
# Utilities
########################################################################
# Return the name of enumerator ENUM as a string.
def enumerator_name(enum):
enumerators = enum.GetType().GetEnumMembers()
for enum_member in enumerators:
if enum.GetValueAsUnsigned() == enum_member.GetValueAsUnsigned():
return enum_member.GetName()
return None
# A class wrapping an SBValue for a Lisp_Object, providing convenience
# functions.
class Lisp_Object:
# Map pvec_type enumerators to corresponding C types.
pvec2type = {
"PVEC_FRAME": "struct frame",
"PVEC_WINDOW": "struct window",
"PVEC_BIGNUM": "struct Lisp_Bignum",
"PVEC_MARKER": "struct Lisp_Marker",
"PVEC_OVERLAY": "struct Lisp_Overlay",
"PVEC_FINALIZER": "struct Lisp_Finalizer",
"PVEC_SYMBOL_WITH_POS": "struct Lisp_Symbol_With_Pos",
"PVEC_MISC_PTR": "",
"PVEC_USER_PTR": "struct Lisp_User_Ptr",
"PVEC_PROCESS": "struct Lisp_Process",
"PVEC_BOOL_VECTOR": "struct Lisp_Bool_Vector",
"PVEC_BUFFER": "struct buffer",
"PVEC_HASH_TABLE": "struct Lisp_Hash_Table",
"PVEC_TERMINAL": "struct terminal",
"PVEC_WINDOW_CONFIGURATION": "struct save_window_data",
"PVEC_SUBR": "struct Lisp_Subr",
"PVEC_PACKAGE": "struct Lisp_Package",
"PVEC_OTHER": "void",
"PVEC_XWIDGET": "void",
"PVEC_XWIDGET_VIEW": "void",
"PVEC_THREAD": "struct thread_state",
"PVEC_MUTEX": "Lisp_Mutex",
"PVEC_CONDVAR": "Lisp_CondVar",
"PVEC_MODULE_FUNCTION": "struct Lisp_Module_Function",
"PVEC_NATIVE_COMP_UNIT": "struct Lisp_Native_Comp_Unit",
"PVEC_SQLITE": "struct Lisp_Sqlite",
"PVEC_COMPILED": "struct Lisp_Vector",
"PVEC_CHAR_TABLE": "struct Lisp_Vector",
"PVEC_SUB_CHAR_TABLE": "void",
"PVEC_RECORD": "struct Lisp_Vector",
"PVEC_FONT": "struct font",
"PVEC_NORMAL_VECTOR": "struct Lisp_Vector"
}
# Object construction/initialization.
def __init__(self, lisp_obj):
self.lisp_obj = lisp_obj
self.frame = lisp_obj.GetFrame()
self.lisp_type = None
self.pvec_type = None
self.value = None
self.init_unsigned()
self.init_lisp_types()
self.init_values()
def init_unsigned(self):
if self.lisp_obj.GetNumChildren() != 0:
# Lisp_Object is actually a struct.
lisp_word = self.lisp_obj.GetValueForExpressionPath(".i")
self.unsigned = lisp_word.GetValueAsUnsigned()
else:
self.unsigned = self.lisp_obj.GetValueAsUnsigned()
# Initialize self.lisp_type to the C Lisp_Type enumerator of the
# Lisp_Object, as a string. Initialize self.pvec_type likewise to
# the pvec_type enumerator if the object is a vector-like, as a
# string.
def init_lisp_types(self):
t = self.eval(f"(enum Lisp_Type)"
f"((EMACS_INT) {self.unsigned} "
f"& (1 << GCTYPEBITS) - 1)")
self.lisp_type = enumerator_name(t)
if self.lisp_type == "Lisp_Vectorlike":
self.pvec_type = "PVEC_NORMAL_VECTOR"
vector = self.get_lisp_pointer("struct Lisp_Vector")
size = vector.GetValueForExpressionPath("->header.size")
size = size.GetValueAsUnsigned()
pseudo = self.eval(f"{size} & PSEUDOVECTOR_FLAG")
if pseudo.GetValueAsUnsigned() != 0:
typ = self.eval(
f"(enum pvec_type) (({size} "
f"& More_Lisp_Bits::PVEC_TYPE_MASK) "
f">> More_Lisp_Bits::PSEUDOVECTOR_AREA_BITS)")
self.pvec_type = enumerator_name(typ)
# Initialize self.value according to lisp_type and pvec_type.
def init_values(self):
if self.lisp_type == "Lisp_Symbol":
offset = self.get_lisp_pointer("char").GetValueAsUnsigned()
self.value = self.eval(f"(struct Lisp_Symbol *)"
f" ((char *) &lispsym + {offset})")
elif self.lisp_type == "Lisp_String":
self.value = self.get_lisp_pointer("struct Lisp_String")
elif self.lisp_type == "Lisp_Vectorlike":
c_type = Lisp_Object.pvec2type[self.pvec_type]
self.value = self.get_lisp_pointer(c_type)
elif self.lisp_type == "Lisp_Cons":
self.value = self.get_lisp_pointer("struct Lisp_Cons")
elif self.lisp_type == "Lisp_Float":
self.value = self.get_lisp_pointer("struct Lisp_Float")
elif self.lisp_type in ("Lisp_Int0", "Lisp_Int1"):
self.value = self.eval(f"((EMACS_INT) {self.unsigned}) "
f">> (GCTYPEBITS - 1)")
else:
msg = f"Unknown Lisp type {self.lisp_type}"
assert False, msg
# Create an SBValue for EXPR with name NAME.
def create_value(self, name, expr):
return self.lisp_obj.CreateValueFromExpression(name, expr)
# Evaluate EXPR in the context of the current frame.
def eval(self, expr):
return self.frame.EvaluateExpression(expr)
# Return an SBValue for this object denoting a pointer of type
# TYP*.
def get_lisp_pointer(self, typ):
return self.eval(f"({typ}*) (((EMACS_INT) "
f"{self.unsigned}) & VALMASK)")
# If this is a Lisp_String, return an SBValue for its string data.
# Return None otherwise.
def get_string_data(self):
if self.lisp_type == "Lisp_String":
return self.value.GetValueForExpressionPath("->u.s.data")
return None
# if this is a Lisp_Symbol, return an SBBalue for its name.
# Return None otherwise.
def get_symbol_name(self):
if self.lisp_type == "Lisp_Symbol":
name = self.value.GetValueForExpressionPath("->u.s.name")
return Lisp_Object(name).get_string_data()
return None
def is_nil(self):
return self.lisp_type == None
# Get the package of a symbol or None if not a symbol.
def get_symbol_package(self):
if self.lisp_type == "Lisp_Symbol":
value = self.value.GetValueForExpressionPath("->u.s.package")
package = Lisp_Object(value)
if package.pvec_type:
name = Lisp_Object(package.value.GetValueForExpressionPath("->name"))
return name.get_string_data()
return None
# Return a summary string for this object.
def summary(self):
return str(self.value)
def dump(self, result):
if self.lisp_type == "Lisp_Symbol":
result.AppendMessage(f"package: {self.get_symbol_package()}")
result.AppendMessage(f"name: {self.get_symbol_name()}")
elif self.lisp_type == "Lisp_String":
result.AppendMessage(str(self.get_string_data()))
else:
result.AppendMessage(self.summary())
########################################################################
# LLDB Commands
########################################################################
def xbacktrace(debugger, command, ctx, result, internal_dict):
"""Print Emacs Lisp backtrace"""
frame = ctx.GetFrame()
n = frame.EvaluateExpression(
"current_thread->m_specpdl_ptr - current_thread->m_specpdl")
for i in reversed(range(0, n.GetValueAsUnsigned())):
s = frame.EvaluateExpression(f"current_thread->m_specpdl[{i}]")
kind = enumerator_name(s.GetChildMemberWithName("kind"))
if kind == "SPECPDL_BACKTRACE":
function = Lisp_Object(s.GetValueForExpressionPath(".bt.function"))
if function.lisp_type == "Lisp_Symbol":
sym_name = function.get_symbol_name()
result.AppendMessage(str(sym_name))
elif function.lisp_type == "Lisp_Vectorlike":
result.AppendMessage(function.pvec_type)
else:
result.AppendMessage(function.lisp_type)
def xdebug_print(debugger, command, result, internal_dict):
"""Print Lisp_Objects using safe_debug_print()"""
debugger.HandleCommand(f"expr safe_debug_print({command})")
def xprint(debugger, command, ctx, result, internal_dict):
frame = ctx.GetFrame()
lisp_obj = Lisp_Object(frame.EvaluateExpression(command))
lisp_obj.dump(result)
########################################################################
# Formatters
########################################################################
def type_summary_Lisp_Object(obj, internal_dict):
return Lisp_Object(obj).summary()
########################################################################
# Initialization
########################################################################
# Define Python FUNCTION as an LLDB command.
def define_command (debugger, function):
lldb_command = function.__name__
python_function = __name__ + "." + function.__name__
interpreter = debugger.GetCommandInterpreter()
def define(overwrite):
res = lldb.SBCommandReturnObject()
interpreter.HandleCommand(f"command script add "
f"{overwrite} "
f"--function {python_function} "
f"{lldb_command}",
res)
return res.Succeeded()
if not define("--overwrite"):
define("")
# Define Python FUNCTION as an LLDB type summary provider for types
# matching REGEX. Type summaries defined here are defined in the
# category Emacs, and can be seen with 'type summary list -w Emacs',
# and deleted in a similar way.
def define_type_summary(debugger, regex, function):
python_function = __name__ + "." + function.__name__
debugger.HandleCommand(f"type summary add --expand "
f"--cascade true "
f"--category Emacs "
f"--python-function {python_function} "
+ regex)
# Enable a given category of type summary providers.
def enable_type_category(debugger, category):
debugger.HandleCommand(f"type category enable {category}")
# This function is called by LLDB to initialize the module.
def __lldb_init_module(debugger, internal_dict):
define_command(debugger, xbacktrace)
define_command(debugger, xdebug_print)
define_command(debugger, xprint)
define_type_summary(debugger, "Lisp_Object", type_summary_Lisp_Object)
enable_type_category(debugger, "Emacs")
print('Emacs debugging support has been installed.')
# end.
|