mirror of
https://sourceware.org/git/binutils-gdb.git
synced 2024-12-27 04:52:05 +08:00
bf80931081
GDB currently has several objects that are put in a singly linked list, by having the object's type have a "next" pointer directly. For example, struct thread_info and struct inferior. Because these are simply-linked lists, and we don't keep track of a "tail" pointer, when we want to append a new element on the list, we need to walk the whole list to find the current tail. It would be nice to get rid of that walk. Removing elements from such lists also requires a walk, to find the "previous" position relative to the element being removed. To eliminate the need for that walk, we could make those lists doubly-linked, by adding a "prev" pointer alongside "next". It would be nice to avoid the boilerplate associated with maintaining such a list manually, though. That is what the new intrusive_list type addresses. With an intrusive list, it's also possible to move items out of the list without destroying them, which is interesting in our case for example for threads, when we exit them, but can't destroy them immediately. We currently keep exited threads on the thread list, but we could change that which would simplify some things. Note that with std::list, element removal is O(N). I.e., with std::list, we need to walk the list to find the iterator pointing to the position to remove. However, we could store a list iterator inside the object as soon as we put the object in the list, to address it, because std::list iterators are not invalidated when other elements are added/removed. However, if you need to put the same object in more than one list, then std::list<object> doesn't work. You need to instead use std::list<object *>, which is less efficient for requiring extra memory allocations. For an example of an object in multiple lists, see the step_over_next/step_over_prev fields in thread_info: /* Step-over chain. A thread is in the step-over queue if these are non-NULL. If only a single thread is in the chain, then these fields point to self. */ struct thread_info *step_over_prev = NULL; struct thread_info *step_over_next = NULL; The new intrusive_list type gives us the advantages of an intrusive linked list, while avoiding the boilerplate associated with manually maintaining it. intrusive_list's API follows the standard container interface, and thus std::list's interface. It is based the API of Boost's intrusive list, here: https://www.boost.org/doc/libs/1_73_0/doc/html/boost/intrusive/list.html Our implementation is relatively simple, while Boost's is complicated and intertwined due to a lot of customization options, which our version doesn't have. The easiest way to use an intrusive_list is to make the list's element type inherit from intrusive_node. This adds a prev/next pointers to the element type. However, to support putting the same object in more than one list, intrusive_list supports putting the "node" info as a field member, so you can have more than one such nodes, one per list. As a first guinea pig, this patch makes the per-inferior thread list use intrusive_list using the base class method. Unlike Boost's implementation, ours is not a circular list. An earlier version of the patch was circular: the intrusive_list type included an intrusive_list_node "head". In this design, a node contained pointers to the previous and next nodes, not the previous and next elements. This wasn't great for when debugging GDB with GDB, as it was difficult to get from a pointer to the node to a pointer to the element. With the design proposed in this patch, nodes contain pointers to the previous and next elements, making it easy to traverse the list by hand and inspect each element. The intrusive_list object contains pointers to the first and last elements of the list. They are nullptr if the list is empty. Each element's node contains a pointer to the previous and next elements. The first element's previous pointer is nullptr and the last element's next pointer is nullptr. Therefore, if there's a single element in the list, both its previous and next pointers are nullptr. To differentiate such an element from an element that is not linked into a list, the previous and next pointers contain a special value (-1) when the node is not linked. This is necessary to be able to reliably tell if a given node is currently linked or not. A begin() iterator points to the first item in the list. An end() iterator contains nullptr. This makes iteration until end naturally work, as advancing past the last element will make the iterator contain nullptr, making it equal to the end iterator. If the list is empty, a begin() iterator will contain nullptr from the start, and therefore be immediately equal to the end. Iterating on an intrusive_list yields references to objects (e.g. `thread_info&`). The rest of GDB currently expects iterators and ranges to yield pointers (e.g. `thread_info*`). To bridge the gap, add the reference_to_pointer_iterator type. It is used to define inf_threads_iterator. Add a Python pretty-printer, to help inspecting intrusive lists when debugging GDB with GDB. Here's an example of the output: (top-gdb) p current_inferior_.m_obj.thread_list $1 = intrusive list of thread_info = {0x61700002c000, 0x617000069080, 0x617000069400, 0x61700006d680, 0x61700006eb80} It's not possible with current master, but with this patch [1] that I hope will be merged eventually, it's possible to index the list and access the pretty-printed value's children: (top-gdb) p current_inferior_.m_obj.thread_list[1] $2 = (thread_info *) 0x617000069080 (top-gdb) p current_inferior_.m_obj.thread_list[1].ptid $3 = { m_pid = 406499, m_lwp = 406503, m_tid = 0 } Even though iterating the list in C++ yields references, the Python pretty-printer yields pointers. The reason for this is that the output of printing the thread list above would be unreadable, IMO, if each thread_info object was printed in-line, since they contain so much information. I think it's more useful to print pointers, and let the user drill down as needed. [1] https://sourceware.org/pipermail/gdb-patches/2021-April/178050.html Co-Authored-By: Simon Marchi <simon.marchi@efficios.com> Change-Id: I3412a14dc77f25876d742dab8f44e0ba7c7586c0
403 lines
15 KiB
Python
403 lines
15 KiB
Python
# Copyright (C) 2009-2021 Free Software Foundation, Inc.
|
|
#
|
|
# This file is part of GDB.
|
|
#
|
|
# This program 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 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
import gdb
|
|
import os.path
|
|
|
|
|
|
class TypeFlag:
|
|
"""A class that allows us to store a flag name, its short name,
|
|
and its value.
|
|
|
|
In the GDB sources, struct type has a component called instance_flags
|
|
in which the value is the addition of various flags. These flags are
|
|
defined by the enumerates type_instance_flag_value. This class helps us
|
|
recreate a list with all these flags that is easy to manipulate and sort.
|
|
Because all flag names start with TYPE_INSTANCE_FLAG_, a short_name
|
|
attribute is provided that strips this prefix.
|
|
|
|
ATTRIBUTES
|
|
name: The enumeration name (eg: "TYPE_INSTANCE_FLAG_CONST").
|
|
value: The associated value.
|
|
short_name: The enumeration name, with the suffix stripped.
|
|
"""
|
|
|
|
def __init__(self, name, value):
|
|
self.name = name
|
|
self.value = value
|
|
self.short_name = name.replace("TYPE_INSTANCE_FLAG_", "")
|
|
|
|
def __lt__(self, other):
|
|
"""Sort by value order."""
|
|
return self.value < other.value
|
|
|
|
|
|
# A list of all existing TYPE_INSTANCE_FLAGS_* enumerations,
|
|
# stored as TypeFlags objects. Lazy-initialized.
|
|
TYPE_FLAGS = None
|
|
|
|
|
|
class TypeFlagsPrinter:
|
|
"""A class that prints a decoded form of an instance_flags value.
|
|
|
|
This class uses a global named TYPE_FLAGS, which is a list of
|
|
all defined TypeFlag values. Using a global allows us to compute
|
|
this list only once.
|
|
|
|
This class relies on a couple of enumeration types being defined.
|
|
If not, then printing of the instance_flag is going to be degraded,
|
|
but it's not a fatal error.
|
|
"""
|
|
|
|
def __init__(self, val):
|
|
self.val = val
|
|
|
|
def __str__(self):
|
|
global TYPE_FLAGS
|
|
if TYPE_FLAGS is None:
|
|
self.init_TYPE_FLAGS()
|
|
if not self.val:
|
|
return "0"
|
|
if TYPE_FLAGS:
|
|
flag_list = [
|
|
flag.short_name for flag in TYPE_FLAGS if self.val & flag.value
|
|
]
|
|
else:
|
|
flag_list = ["???"]
|
|
return "0x%x [%s]" % (self.val, "|".join(flag_list))
|
|
|
|
def init_TYPE_FLAGS(self):
|
|
"""Initialize the TYPE_FLAGS global as a list of TypeFlag objects.
|
|
This operation requires the search of a couple of enumeration types.
|
|
If not found, a warning is printed on stdout, and TYPE_FLAGS is
|
|
set to the empty list.
|
|
|
|
The resulting list is sorted by increasing value, to facilitate
|
|
printing of the list of flags used in an instance_flags value.
|
|
"""
|
|
global TYPE_FLAGS
|
|
TYPE_FLAGS = []
|
|
try:
|
|
iflags = gdb.lookup_type("enum type_instance_flag_value")
|
|
except:
|
|
print("Warning: Cannot find enum type_instance_flag_value type.")
|
|
print(" `struct type' pretty-printer will be degraded")
|
|
return
|
|
TYPE_FLAGS = [TypeFlag(field.name, field.enumval) for field in iflags.fields()]
|
|
TYPE_FLAGS.sort()
|
|
|
|
|
|
class StructTypePrettyPrinter:
|
|
"""Pretty-print an object of type struct type"""
|
|
|
|
def __init__(self, val):
|
|
self.val = val
|
|
|
|
def to_string(self):
|
|
fields = []
|
|
fields.append("pointer_type = %s" % self.val["pointer_type"])
|
|
fields.append("reference_type = %s" % self.val["reference_type"])
|
|
fields.append("chain = %s" % self.val["reference_type"])
|
|
fields.append(
|
|
"instance_flags = %s" % TypeFlagsPrinter(self.val["m_instance_flags"])
|
|
)
|
|
fields.append("length = %d" % self.val["length"])
|
|
fields.append("main_type = %s" % self.val["main_type"])
|
|
return "\n{" + ",\n ".join(fields) + "}"
|
|
|
|
|
|
class StructMainTypePrettyPrinter:
|
|
"""Pretty-print an objet of type main_type"""
|
|
|
|
def __init__(self, val):
|
|
self.val = val
|
|
|
|
def flags_to_string(self):
|
|
"""struct main_type contains a series of components that
|
|
are one-bit ints whose name start with "flag_". For instance:
|
|
flag_unsigned, flag_stub, etc. In essence, these components are
|
|
really boolean flags, and this method prints a short synthetic
|
|
version of the value of all these flags. For instance, if
|
|
flag_unsigned and flag_static are the only components set to 1,
|
|
this function will return "unsigned|static".
|
|
"""
|
|
fields = [
|
|
field.name.replace("flag_", "")
|
|
for field in self.val.type.fields()
|
|
if field.name.startswith("flag_") and self.val[field.name]
|
|
]
|
|
return "|".join(fields)
|
|
|
|
def owner_to_string(self):
|
|
"""Return an image of component "owner"."""
|
|
if self.val["m_flag_objfile_owned"] != 0:
|
|
return "%s (objfile)" % self.val["m_owner"]["objfile"]
|
|
else:
|
|
return "%s (gdbarch)" % self.val["m_owner"]["gdbarch"]
|
|
|
|
def struct_field_location_img(self, field_val):
|
|
"""Return an image of the loc component inside the given field
|
|
gdb.Value.
|
|
"""
|
|
loc_val = field_val["loc"]
|
|
loc_kind = str(field_val["loc_kind"])
|
|
if loc_kind == "FIELD_LOC_KIND_BITPOS":
|
|
return "bitpos = %d" % loc_val["bitpos"]
|
|
elif loc_kind == "FIELD_LOC_KIND_ENUMVAL":
|
|
return "enumval = %d" % loc_val["enumval"]
|
|
elif loc_kind == "FIELD_LOC_KIND_PHYSADDR":
|
|
return "physaddr = 0x%x" % loc_val["physaddr"]
|
|
elif loc_kind == "FIELD_LOC_KIND_PHYSNAME":
|
|
return "physname = %s" % loc_val["physname"]
|
|
elif loc_kind == "FIELD_LOC_KIND_DWARF_BLOCK":
|
|
return "dwarf_block = %s" % loc_val["dwarf_block"]
|
|
else:
|
|
return "loc = ??? (unsupported loc_kind value)"
|
|
|
|
def struct_field_img(self, fieldno):
|
|
"""Return an image of the main_type field number FIELDNO."""
|
|
f = self.val["flds_bnds"]["fields"][fieldno]
|
|
label = "flds_bnds.fields[%d]:" % fieldno
|
|
if f["artificial"]:
|
|
label += " (artificial)"
|
|
fields = []
|
|
fields.append("name = %s" % f["name"])
|
|
fields.append("type = %s" % f["m_type"])
|
|
fields.append("loc_kind = %s" % f["loc_kind"])
|
|
fields.append("bitsize = %d" % f["bitsize"])
|
|
fields.append(self.struct_field_location_img(f))
|
|
return label + "\n" + " {" + ",\n ".join(fields) + "}"
|
|
|
|
def bound_img(self, bound_name):
|
|
"""Return an image of the given main_type's bound."""
|
|
bounds = self.val["flds_bnds"]["bounds"].dereference()
|
|
b = bounds[bound_name]
|
|
bnd_kind = str(b["m_kind"])
|
|
if bnd_kind == "PROP_CONST":
|
|
return str(b["m_data"]["const_val"])
|
|
elif bnd_kind == "PROP_UNDEFINED":
|
|
return "(undefined)"
|
|
else:
|
|
info = [bnd_kind]
|
|
if bound_name == "high" and bounds["flag_upper_bound_is_count"]:
|
|
info.append("upper_bound_is_count")
|
|
return "{} ({})".format(str(b["m_data"]["baton"]), ",".join(info))
|
|
|
|
def bounds_img(self):
|
|
"""Return an image of the main_type bounds."""
|
|
b = self.val["flds_bnds"]["bounds"].dereference()
|
|
low = self.bound_img("low")
|
|
high = self.bound_img("high")
|
|
|
|
img = "flds_bnds.bounds = {%s, %s}" % (low, high)
|
|
if b["flag_bound_evaluated"]:
|
|
img += " [evaluated]"
|
|
return img
|
|
|
|
def type_specific_img(self):
|
|
"""Return a string image of the main_type type_specific union.
|
|
Only the relevant component of that union is printed (based on
|
|
the value of the type_specific_kind field.
|
|
"""
|
|
type_specific_kind = str(self.val["type_specific_field"])
|
|
type_specific = self.val["type_specific"]
|
|
if type_specific_kind == "TYPE_SPECIFIC_NONE":
|
|
img = "type_specific_field = %s" % type_specific_kind
|
|
elif type_specific_kind == "TYPE_SPECIFIC_CPLUS_STUFF":
|
|
img = "cplus_stuff = %s" % type_specific["cplus_stuff"]
|
|
elif type_specific_kind == "TYPE_SPECIFIC_GNAT_STUFF":
|
|
img = (
|
|
"gnat_stuff = {descriptive_type = %s}"
|
|
% type_specific["gnat_stuff"]["descriptive_type"]
|
|
)
|
|
elif type_specific_kind == "TYPE_SPECIFIC_FLOATFORMAT":
|
|
img = "floatformat[0..1] = %s" % type_specific["floatformat"]
|
|
elif type_specific_kind == "TYPE_SPECIFIC_FUNC":
|
|
img = (
|
|
"calling_convention = %d"
|
|
% type_specific["func_stuff"]["calling_convention"]
|
|
)
|
|
# tail_call_list is not printed.
|
|
elif type_specific_kind == "TYPE_SPECIFIC_SELF_TYPE":
|
|
img = "self_type = %s" % type_specific["self_type"]
|
|
elif type_specific_kind == "TYPE_SPECIFIC_FIXED_POINT":
|
|
# The scaling factor is an opaque structure, so we cannot
|
|
# decode its value from Python (not without insider knowledge).
|
|
img = (
|
|
"scaling_factor: <opaque> (call __gmpz_dump with "
|
|
" _mp_num and _mp_den fields if needed)"
|
|
)
|
|
else:
|
|
img = (
|
|
"type_specific = ??? (unknown type_secific_kind: %s)"
|
|
% type_specific_kind
|
|
)
|
|
return img
|
|
|
|
def to_string(self):
|
|
"""Return a pretty-printed image of our main_type."""
|
|
fields = []
|
|
fields.append("name = %s" % self.val["name"])
|
|
fields.append("code = %s" % self.val["code"])
|
|
fields.append("flags = [%s]" % self.flags_to_string())
|
|
fields.append("owner = %s" % self.owner_to_string())
|
|
fields.append("target_type = %s" % self.val["target_type"])
|
|
if self.val["nfields"] > 0:
|
|
for fieldno in range(self.val["nfields"]):
|
|
fields.append(self.struct_field_img(fieldno))
|
|
if self.val["code"] == gdb.TYPE_CODE_RANGE:
|
|
fields.append(self.bounds_img())
|
|
fields.append(self.type_specific_img())
|
|
|
|
return "\n{" + ",\n ".join(fields) + "}"
|
|
|
|
|
|
class CoreAddrPrettyPrinter:
|
|
"""Print CORE_ADDR values as hex."""
|
|
|
|
def __init__(self, val):
|
|
self._val = val
|
|
|
|
def to_string(self):
|
|
return hex(int(self._val))
|
|
|
|
|
|
class IntrusiveListPrinter:
|
|
"""Print a struct intrusive_list."""
|
|
|
|
def __init__(self, val):
|
|
self._val = val
|
|
|
|
# Type of linked items.
|
|
self._item_type = self._val.type.template_argument(0)
|
|
self._node_ptr_type = gdb.lookup_type(
|
|
"intrusive_list_node<{}>".format(self._item_type.tag)
|
|
).pointer()
|
|
|
|
# Type of value -> node converter.
|
|
self._conv_type = self._val.type.template_argument(1)
|
|
|
|
if self._uses_member_node():
|
|
# The second template argument of intrusive_member_node is a member
|
|
# pointer value. Its value is the offset of the node member in the
|
|
# enclosing type.
|
|
member_node_ptr = self._conv_type.template_argument(1)
|
|
member_node_ptr = member_node_ptr.cast(gdb.lookup_type("int"))
|
|
self._member_node_offset = int(member_node_ptr)
|
|
|
|
# This is only needed in _as_node_ptr if using a member node. Look it
|
|
# up here so we only do it once.
|
|
self._char_ptr_type = gdb.lookup_type("char").pointer()
|
|
|
|
def display_hint(self):
|
|
return "array"
|
|
|
|
def _uses_member_node(self):
|
|
"""Return True if the list items use a node as a member, False if
|
|
they use a node as a base class.
|
|
"""
|
|
|
|
if self._conv_type.name.startswith("intrusive_member_node<"):
|
|
return True
|
|
elif self._conv_type.name.startswith("intrusive_base_node<"):
|
|
return False
|
|
else:
|
|
raise RuntimeError(
|
|
"Unexpected intrusive_list value -> node converter type: {}".format(
|
|
self._conv_type.name
|
|
)
|
|
)
|
|
|
|
def to_string(self):
|
|
s = "intrusive list of {}".format(self._item_type)
|
|
|
|
if self._uses_member_node():
|
|
node_member = self._conv_type.template_argument(1)
|
|
s += ", linked through {}".format(node_member)
|
|
|
|
return s
|
|
|
|
def _as_node_ptr(self, elem_ptr):
|
|
"""Given ELEM_PTR, a pointer to a list element, return a pointer to the
|
|
corresponding intrusive_list_node.
|
|
"""
|
|
|
|
assert elem_ptr.type.code == gdb.TYPE_CODE_PTR
|
|
|
|
if self._uses_member_node():
|
|
# Node as a member: add the member node offset from to the element's
|
|
# address to get the member node's address.
|
|
elem_char_ptr = elem_ptr.cast(self._char_ptr_type)
|
|
node_char_ptr = elem_char_ptr + self._member_node_offset
|
|
return node_char_ptr.cast(self._node_ptr_type)
|
|
else:
|
|
# Node as a base: just casting from node pointer to item pointer
|
|
# will adjust the pointer value.
|
|
return elem_ptr.cast(self._node_ptr_type)
|
|
|
|
def _children_generator(self):
|
|
"""Generator that yields one tuple per list item."""
|
|
|
|
elem_ptr = self._val["m_front"]
|
|
idx = 0
|
|
while elem_ptr != 0:
|
|
yield (str(idx), elem_ptr.dereference())
|
|
node_ptr = self._as_node_ptr(elem_ptr)
|
|
elem_ptr = node_ptr["next"]
|
|
idx += 1
|
|
|
|
def children(self):
|
|
return self._children_generator()
|
|
|
|
|
|
def type_lookup_function(val):
|
|
"""A routine that returns the correct pretty printer for VAL
|
|
if appropriate. Returns None otherwise.
|
|
"""
|
|
tag = val.type.tag
|
|
name = val.type.name
|
|
if tag == "type":
|
|
return StructTypePrettyPrinter(val)
|
|
elif tag == "main_type":
|
|
return StructMainTypePrettyPrinter(val)
|
|
elif name == "CORE_ADDR":
|
|
return CoreAddrPrettyPrinter(val)
|
|
elif tag is not None and tag.startswith("intrusive_list<"):
|
|
return IntrusiveListPrinter(val)
|
|
return None
|
|
|
|
|
|
def register_pretty_printer(objfile):
|
|
"""A routine to register a pretty-printer against the given OBJFILE."""
|
|
objfile.pretty_printers.append(type_lookup_function)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if gdb.current_objfile() is not None:
|
|
# This is the case where this script is being "auto-loaded"
|
|
# for a given objfile. Register the pretty-printer for that
|
|
# objfile.
|
|
register_pretty_printer(gdb.current_objfile())
|
|
else:
|
|
# We need to locate the objfile corresponding to the GDB
|
|
# executable, and register the pretty-printer for that objfile.
|
|
# FIXME: The condition used to match the objfile is too simplistic
|
|
# and will not work on Windows.
|
|
for objfile in gdb.objfiles():
|
|
if os.path.basename(objfile.filename) == "gdb":
|
|
objfile.pretty_printers.append(type_lookup_function)
|