2009-09-18 17:15:32 +02:00
|
|
|
import gdb
|
2016-05-23 16:45:55 +02:00
|
|
|
import glib_gdb
|
2013-12-17 21:14:38 +01:00
|
|
|
import sys
|
|
|
|
|
|
|
|
if sys.version_info[0] >= 3:
|
|
|
|
long = int
|
2014-02-21 20:16:00 +01:00
|
|
|
else:
|
|
|
|
import itertools
|
2020-11-17 16:07:09 +01:00
|
|
|
|
2014-02-21 20:16:00 +01:00
|
|
|
map = itertools.imap
|
2013-12-17 21:14:38 +01:00
|
|
|
|
2014-02-21 20:16:00 +01:00
|
|
|
# FrameDecorator is new in gdb 7.7, so we adapt to its absence.
|
2010-03-23 16:18:12 +01:00
|
|
|
try:
|
2014-02-21 20:16:00 +01:00
|
|
|
import gdb.FrameDecorator
|
2020-11-17 16:07:09 +01:00
|
|
|
|
2014-02-21 20:16:00 +01:00
|
|
|
HAVE_GDB_FRAMEDECORATOR = True
|
|
|
|
FrameDecorator = gdb.FrameDecorator.FrameDecorator
|
2010-03-23 16:18:12 +01:00
|
|
|
except ImportError:
|
2014-02-21 20:16:00 +01:00
|
|
|
HAVE_GDB_FRAMEDECORATOR = False
|
2009-09-18 17:15:32 +02:00
|
|
|
|
2020-11-17 16:32:10 +01:00
|
|
|
|
2009-09-18 17:15:32 +02:00
|
|
|
# This is not quite right, as local vars may override symname
|
2020-11-17 16:07:09 +01:00
|
|
|
def read_global_var(symname):
|
2009-09-18 17:15:32 +02:00
|
|
|
return gdb.selected_frame().read_var(symname)
|
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
|
|
|
|
def g_type_to_typenode(gtype):
|
|
|
|
def lookup_fundamental_type(typenode):
|
2009-09-18 17:15:32 +02:00
|
|
|
if typenode == 0:
|
|
|
|
return None
|
2020-11-17 16:07:09 +01:00
|
|
|
val = read_global_var("static_fundamental_type_nodes")
|
2018-07-12 23:40:50 +02:00
|
|
|
if val is None:
|
2009-09-18 17:15:32 +02:00
|
|
|
return None
|
2018-03-09 09:49:52 +01:00
|
|
|
return val[typenode >> 2].address
|
2009-09-18 17:15:32 +02:00
|
|
|
|
|
|
|
gtype = long(gtype)
|
|
|
|
typenode = gtype - gtype % 4
|
|
|
|
if typenode > (255 << 2):
|
2020-11-17 16:07:09 +01:00
|
|
|
typenode = gdb.Value(typenode).cast(gdb.lookup_type("TypeNode").pointer())
|
2009-09-18 17:15:32 +02:00
|
|
|
else:
|
2020-11-17 16:07:09 +01:00
|
|
|
typenode = lookup_fundamental_type(typenode)
|
2018-03-09 15:24:59 +01:00
|
|
|
return typenode
|
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
|
|
|
|
def g_type_to_name(gtype):
|
2018-03-09 15:24:59 +01:00
|
|
|
typenode = g_type_to_typenode(gtype)
|
2020-11-17 16:32:10 +01:00
|
|
|
if typenode is not None:
|
2020-11-17 16:07:09 +01:00
|
|
|
return glib_gdb.g_quark_to_string(typenode["qname"])
|
2009-09-18 17:15:32 +02:00
|
|
|
return None
|
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
|
|
|
|
def is_g_type_instance(val):
|
|
|
|
def is_g_type_instance_helper(type):
|
2009-09-18 17:15:32 +02:00
|
|
|
if str(type) == "GTypeInstance":
|
|
|
|
return True
|
|
|
|
|
|
|
|
while type.code == gdb.TYPE_CODE_TYPEDEF:
|
|
|
|
type = type.target()
|
|
|
|
|
|
|
|
if type.code != gdb.TYPE_CODE_STRUCT:
|
|
|
|
return False
|
|
|
|
|
|
|
|
fields = type.fields()
|
2020-11-17 16:07:09 +01:00
|
|
|
if len(fields) < 1:
|
2009-09-18 17:15:32 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
first_field = fields[0]
|
|
|
|
return is_g_type_instance_helper(first_field.type)
|
|
|
|
|
|
|
|
type = val.type
|
|
|
|
if type.code != gdb.TYPE_CODE_PTR:
|
|
|
|
return False
|
|
|
|
type = type.target()
|
2020-11-17 16:07:09 +01:00
|
|
|
return is_g_type_instance_helper(type)
|
2009-09-18 17:15:32 +02:00
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
|
|
|
|
def g_type_name_from_instance(instance):
|
2009-09-18 17:15:32 +02:00
|
|
|
if long(instance) != 0:
|
|
|
|
try:
|
2020-11-17 16:07:09 +01:00
|
|
|
inst = instance.cast(gdb.lookup_type("GTypeInstance").pointer())
|
2009-09-18 17:15:32 +02:00
|
|
|
klass = inst["g_class"]
|
|
|
|
gtype = klass["g_type"]
|
2020-11-17 16:07:09 +01:00
|
|
|
name = g_type_to_name(gtype)
|
2009-09-18 17:15:32 +02:00
|
|
|
return name
|
|
|
|
except RuntimeError:
|
|
|
|
pass
|
|
|
|
return None
|
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
|
2009-09-18 17:15:32 +02:00
|
|
|
class GTypePrettyPrinter:
|
|
|
|
"Prints a GType instance pointer"
|
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
def __init__(self, val):
|
2009-09-18 17:15:32 +02:00
|
|
|
self.val = val
|
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
def to_string(self):
|
|
|
|
name = g_type_name_from_instance(self.val)
|
2009-09-18 17:15:32 +02:00
|
|
|
if name:
|
2020-11-17 16:07:09 +01:00
|
|
|
return ("0x%x [%s]") % (long(self.val), name)
|
|
|
|
return ("0x%x") % (long(self.val))
|
2009-09-18 17:15:32 +02:00
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
|
|
|
|
def is_g_type_class_instance(val):
|
2018-03-09 15:24:59 +01:00
|
|
|
type = val.type
|
|
|
|
if type.code != gdb.TYPE_CODE_PTR:
|
|
|
|
return False
|
|
|
|
return str(type.target()) == "GTypeClass"
|
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
|
2018-03-09 15:24:59 +01:00
|
|
|
class GTypeHandlePrettyPrinter:
|
|
|
|
"Prints a GType instance"
|
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
def __init__(self, val, hint=""):
|
2018-03-09 15:24:59 +01:00
|
|
|
self.val = val
|
|
|
|
self.hint = hint
|
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
def to_string(self):
|
2018-03-09 15:24:59 +01:00
|
|
|
typenode = g_type_to_typenode(self.val)
|
2020-11-17 16:32:10 +01:00
|
|
|
if typenode is not None:
|
2020-11-17 16:07:09 +01:00
|
|
|
name = glib_gdb.g_quark_to_string(typenode["qname"])
|
|
|
|
s = ("0x%x [%s%s") % (long(self.val), self.hint, name)
|
|
|
|
for i in range(1, int(typenode["n_supers"])):
|
2018-03-09 15:24:59 +01:00
|
|
|
node = g_type_to_typenode(typenode["supers"][i])
|
|
|
|
if node:
|
|
|
|
name = glib_gdb.g_quark_to_string(node["qname"])
|
|
|
|
else:
|
|
|
|
name = "???"
|
|
|
|
s += "/" + name
|
|
|
|
return s + "]"
|
|
|
|
else:
|
2020-11-17 16:07:09 +01:00
|
|
|
return ("0x%x") % (long(self.val))
|
2018-03-09 15:24:59 +01:00
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
|
|
|
|
def pretty_printer_lookup(val):
|
|
|
|
if is_g_type_instance(val):
|
|
|
|
return GTypePrettyPrinter(val)
|
2018-03-09 15:24:59 +01:00
|
|
|
if str(val.type) == "GType":
|
2020-11-17 16:07:09 +01:00
|
|
|
return GTypeHandlePrettyPrinter(val)
|
|
|
|
if is_g_type_class_instance(val):
|
|
|
|
return GTypeHandlePrettyPrinter(val["g_type"], "g_type: ")
|
2009-09-18 17:15:32 +02:00
|
|
|
|
|
|
|
return None
|
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
|
|
|
|
def get_signal_name(id):
|
2018-07-12 23:40:50 +02:00
|
|
|
if id is None:
|
2013-09-19 17:55:47 +02:00
|
|
|
return None
|
|
|
|
id = long(id)
|
|
|
|
if id == 0:
|
|
|
|
return None
|
2023-12-11 15:07:44 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
val = read_global_var("g_signal_nodes")
|
|
|
|
max_s = read_global_var("g_n_signal_nodes")
|
|
|
|
max_s = long(max_s)
|
|
|
|
if id < max_s:
|
|
|
|
return val[id]["name"].string()
|
|
|
|
except gdb.error:
|
|
|
|
return None
|
|
|
|
|
2009-09-18 17:15:32 +02:00
|
|
|
return None
|
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
|
2014-02-21 20:16:00 +01:00
|
|
|
def frame_name(frame):
|
|
|
|
return str(frame.function())
|
2009-09-18 17:15:32 +02:00
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
|
2014-02-21 20:16:00 +01:00
|
|
|
def frame_var(frame, var):
|
|
|
|
return frame.inferior_frame().read_var(var)
|
2009-09-18 17:15:32 +02:00
|
|
|
|
|
|
|
|
2014-02-21 20:16:00 +01:00
|
|
|
class SignalFrame(FrameDecorator):
|
2020-11-17 16:07:09 +01:00
|
|
|
def __init__(self, frames):
|
2014-02-21 20:16:00 +01:00
|
|
|
FrameDecorator.__init__(self, frames[-1])
|
2009-09-18 17:15:32 +02:00
|
|
|
self.frame = frames[-1]
|
2013-09-19 17:55:47 +02:00
|
|
|
self.frames = frames
|
2009-09-18 17:15:32 +02:00
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
def name(self):
|
2009-09-18 17:15:32 +02:00
|
|
|
return "signal-emission"
|
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
def read_var(self, frame, name, array=None):
|
2009-09-18 17:15:32 +02:00
|
|
|
try:
|
2020-11-17 16:07:09 +01:00
|
|
|
v = frame_var(frame, name)
|
2018-07-12 23:40:50 +02:00
|
|
|
if v is None or v.is_optimized_out:
|
2009-09-18 17:15:32 +02:00
|
|
|
return None
|
2020-11-17 16:32:10 +01:00
|
|
|
if array is not None:
|
2020-11-17 16:07:09 +01:00
|
|
|
array.append(v)
|
2009-09-18 17:15:32 +02:00
|
|
|
return v
|
|
|
|
except ValueError:
|
|
|
|
return None
|
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
def read_object(self, frame, name, array=None):
|
2009-09-18 17:15:32 +02:00
|
|
|
try:
|
2020-11-17 16:07:09 +01:00
|
|
|
v = frame_var(frame, name)
|
2018-07-12 23:40:50 +02:00
|
|
|
if v is None or v.is_optimized_out:
|
2009-09-18 17:15:32 +02:00
|
|
|
return None
|
2020-11-17 16:07:09 +01:00
|
|
|
v = v.cast(gdb.lookup_type("GObject").pointer())
|
2009-09-18 17:15:32 +02:00
|
|
|
# Ensure this is a somewhat correct object pointer
|
2020-11-17 16:32:10 +01:00
|
|
|
if v is not None and g_type_name_from_instance(v):
|
|
|
|
if array is not None:
|
2020-11-17 16:07:09 +01:00
|
|
|
array.append(v)
|
2009-09-18 17:15:32 +02:00
|
|
|
return v
|
|
|
|
return None
|
|
|
|
except ValueError:
|
|
|
|
return None
|
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
def append(self, array, obj):
|
2020-11-17 16:32:10 +01:00
|
|
|
if obj is not None:
|
2020-11-17 16:07:09 +01:00
|
|
|
array.append(obj)
|
2009-09-18 17:15:32 +02:00
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
def or_join_array(self, array):
|
2009-09-18 17:15:32 +02:00
|
|
|
if len(array) == 0:
|
|
|
|
return "???"
|
2013-09-19 21:41:09 +02:00
|
|
|
else:
|
2020-11-17 16:07:09 +01:00
|
|
|
return " or ".join(set(map(str, array)))
|
2009-09-18 17:15:32 +02:00
|
|
|
|
2013-09-19 21:24:16 +02:00
|
|
|
def get_detailed_signal_from_frame(self, frame, signal):
|
2020-11-17 16:07:09 +01:00
|
|
|
detail = self.read_var(frame, "detail")
|
|
|
|
detail = glib_gdb.g_quark_to_string(detail)
|
2013-09-19 21:24:16 +02:00
|
|
|
if detail is not None:
|
|
|
|
return signal + ":" + detail
|
|
|
|
else:
|
2023-05-15 15:42:47 +02:00
|
|
|
return signal
|
2013-09-19 21:24:16 +02:00
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
def function(self):
|
2009-09-18 17:15:32 +02:00
|
|
|
instances = []
|
|
|
|
signals = []
|
|
|
|
|
|
|
|
for frame in self.frames:
|
2014-02-21 20:16:00 +01:00
|
|
|
name = frame_name(frame)
|
2009-09-18 17:15:32 +02:00
|
|
|
if name == "signal_emit_unlocked_R":
|
2020-11-17 16:07:09 +01:00
|
|
|
self.read_object(frame, "instance", instances)
|
|
|
|
node = self.read_var(frame, "node")
|
2009-09-18 17:15:32 +02:00
|
|
|
if node:
|
|
|
|
signal = node["name"].string()
|
2013-09-19 21:24:16 +02:00
|
|
|
signal = self.get_detailed_signal_from_frame(frame, signal)
|
|
|
|
self.append(signals, signal)
|
2009-09-18 17:15:32 +02:00
|
|
|
|
|
|
|
if name == "g_signal_emitv":
|
2020-11-17 16:07:09 +01:00
|
|
|
instance_and_params = self.read_var(frame, "instance_and_params")
|
2009-09-18 17:15:32 +02:00
|
|
|
if instance_and_params:
|
2020-11-17 16:07:09 +01:00
|
|
|
instance = instance_and_params[0]["v_pointer"].cast(
|
|
|
|
gdb.Type("GObject").pointer()
|
|
|
|
)
|
|
|
|
self.append(instances, instance)
|
|
|
|
id = self.read_var(frame, "signal_id")
|
|
|
|
signal = get_signal_name(id)
|
2009-09-18 17:15:32 +02:00
|
|
|
if signal:
|
2013-09-19 21:24:16 +02:00
|
|
|
signal = self.get_detailed_signal_from_frame(frame, signal)
|
2020-11-17 16:07:09 +01:00
|
|
|
self.append(signals, signal)
|
2009-09-18 17:15:32 +02:00
|
|
|
|
|
|
|
if name == "g_signal_emit_valist" or name == "g_signal_emit":
|
2020-11-17 16:07:09 +01:00
|
|
|
self.read_object(frame, "instance", instances)
|
|
|
|
id = self.read_var(frame, "signal_id")
|
|
|
|
signal = get_signal_name(id)
|
2009-09-18 17:15:32 +02:00
|
|
|
if signal:
|
2013-09-19 21:24:16 +02:00
|
|
|
signal = self.get_detailed_signal_from_frame(frame, signal)
|
2020-11-17 16:07:09 +01:00
|
|
|
self.append(signals, signal)
|
2009-09-18 17:15:32 +02:00
|
|
|
|
|
|
|
if name == "g_signal_emit_by_name":
|
2020-11-17 16:07:09 +01:00
|
|
|
self.read_object(frame, "instance", instances)
|
|
|
|
self.read_var(frame, "detailed_signal", signals)
|
2009-09-18 17:15:32 +02:00
|
|
|
break
|
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
instance = self.or_join_array(instances)
|
|
|
|
signal = self.or_join_array(signals)
|
2009-09-18 17:15:32 +02:00
|
|
|
|
2023-05-18 17:04:04 +02:00
|
|
|
return "<emit signal '%s' on instance %s>" % (signal, instance)
|
2014-02-21 20:16:00 +01:00
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
def elided(self):
|
2014-02-21 20:16:00 +01:00
|
|
|
return self.frames[0:-1]
|
2009-09-18 17:15:32 +02:00
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
def describe(self, stream, full):
|
|
|
|
stream.write(" " + self.function() + "\n")
|
|
|
|
|
2009-09-18 17:15:32 +02:00
|
|
|
|
2014-02-21 20:16:00 +01:00
|
|
|
class GFrameDecorator:
|
2020-11-17 16:07:09 +01:00
|
|
|
def __init__(self, iter):
|
2009-09-18 17:15:32 +02:00
|
|
|
self.queue = []
|
|
|
|
self.iter = iter
|
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
def __iter__(self):
|
2009-09-18 17:15:32 +02:00
|
|
|
return self
|
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
def fill(self):
|
2014-02-21 20:16:00 +01:00
|
|
|
while len(self.queue) <= 8:
|
2009-09-18 17:15:32 +02:00
|
|
|
try:
|
2014-02-21 20:16:00 +01:00
|
|
|
f = next(self.iter)
|
2020-11-17 16:07:09 +01:00
|
|
|
self.queue.append(f)
|
2009-09-18 17:15:32 +02:00
|
|
|
except StopIteration:
|
|
|
|
return
|
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
def find_signal_emission(self):
|
|
|
|
for i in range(min(len(self.queue), 3)):
|
2023-05-18 16:45:52 +02:00
|
|
|
name = frame_name(self.queue[i])
|
|
|
|
if name == "signal_emit_unlocked_R" or name == "_g_closure_invoke_va":
|
2009-09-18 17:15:32 +02:00
|
|
|
return i
|
|
|
|
return -1
|
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
def next(self):
|
2009-09-18 17:15:32 +02:00
|
|
|
# Ensure we have enough frames for a full signal emission
|
|
|
|
self.fill()
|
|
|
|
|
|
|
|
# Are we at the end?
|
|
|
|
if len(self.queue) == 0:
|
|
|
|
raise StopIteration
|
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
emission = self.find_signal_emission()
|
2009-09-18 17:15:32 +02:00
|
|
|
if emission > 0:
|
|
|
|
start = emission
|
|
|
|
while True:
|
|
|
|
if start == 0:
|
|
|
|
break
|
2020-11-17 16:07:09 +01:00
|
|
|
prev_name = frame_name(self.queue[start - 1])
|
2023-05-18 16:45:52 +02:00
|
|
|
if prev_name.find("_marshal") >= 0 or prev_name == "g_closure_invoke":
|
2009-09-18 17:15:32 +02:00
|
|
|
start = start - 1
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
end = emission + 1
|
|
|
|
while end < len(self.queue):
|
2020-11-17 16:07:09 +01:00
|
|
|
if frame_name(self.queue[end]) in [
|
|
|
|
"g_signal_emitv",
|
|
|
|
"g_signal_emit_valist",
|
|
|
|
"g_signal_emit",
|
|
|
|
"g_signal_emit_by_name",
|
2023-06-02 15:35:38 +02:00
|
|
|
"signal_emitv_unlocked",
|
|
|
|
"signal_emit_valist_unlocked",
|
2020-11-17 16:07:09 +01:00
|
|
|
]:
|
2009-09-18 17:15:32 +02:00
|
|
|
end = end + 1
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
|
|
|
signal_frames = self.queue[start:end]
|
2014-02-21 20:16:00 +01:00
|
|
|
new_frames = [SignalFrame(signal_frames)]
|
2009-09-18 17:15:32 +02:00
|
|
|
self.queue[start:end] = new_frames
|
|
|
|
|
|
|
|
return self.queue.pop(0)
|
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
def __next__(self):
|
2014-06-16 21:24:37 +02:00
|
|
|
return self.next()
|
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
|
2014-02-21 20:16:00 +01:00
|
|
|
class GFrameFilter(object):
|
2020-11-17 16:07:09 +01:00
|
|
|
name = "glib"
|
2014-02-21 20:16:00 +01:00
|
|
|
enabled = True
|
|
|
|
priority = 100
|
|
|
|
|
|
|
|
def filter(self, iterator):
|
|
|
|
return GFrameDecorator(iterator)
|
2009-09-18 17:15:32 +02:00
|
|
|
|
2020-11-17 16:07:09 +01:00
|
|
|
|
|
|
|
def register(obj):
|
2018-07-12 23:40:50 +02:00
|
|
|
if obj is None:
|
2009-09-18 17:15:32 +02:00
|
|
|
obj = gdb
|
|
|
|
|
2014-02-21 20:16:00 +01:00
|
|
|
if HAVE_GDB_FRAMEDECORATOR:
|
|
|
|
filter = GFrameFilter()
|
|
|
|
obj.frame_filters[filter.name] = filter
|
2009-09-18 17:15:32 +02:00
|
|
|
obj.pretty_printers.append(pretty_printer_lookup)
|