blob: e45ea4c09ad9183ab1412dd597b4419e209d61c6 (
plain)
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
|
# Type utilities.
# Copyright (C) 2010 Free Software Foundation, Inc.
# 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/>.
"""Utilities for working with gdb.Types."""
import gdb
def get_basic_type(type_):
"""Return the "basic" type of a type.
Arguments:
type_: The type to reduce to its basic type.
Returns:
type_ with const/volatile is stripped away,
and typedefs/references converted to the underlying type.
"""
while (type_.code == gdb.TYPE_CODE_REF or
type_.code == gdb.TYPE_CODE_TYPEDEF):
if type_.code == gdb.TYPE_CODE_REF:
type_ = type_.target()
else:
type_ = type_.strip_typedefs()
return type_.unqualified()
def has_field(type_, field):
"""Return True if a type has the specified field.
Arguments:
type_: The type to examine.
It must be one of gdb.TYPE_CODE_STRUCT, gdb.TYPE_CODE_UNION.
field: The name of the field to look up.
Returns:
True if the field is present either in type_ or any baseclass.
Raises:
TypeError: The type is not a struct or union.
"""
type_ = get_basic_type(type_)
if (type_.code != gdb.TYPE_CODE_STRUCT and
type_.code != gdb.TYPE_CODE_UNION):
raise TypeError("not a struct or union")
for f in type_.fields():
if f.is_base_class:
if has_field(f.type, field):
return True
else:
# NOTE: f.name could be None
if f.name == field:
return True
return False
def make_enum_dict(enum_type):
"""Return a dictionary from a program's enum type.
Arguments:
enum_type: The enum to compute the dictionary for.
Returns:
The dictionary of the enum.
Raises:
TypeError: The type is not an enum.
"""
if enum_type.code != gdb.TYPE_CODE_ENUM:
raise TypeError("not an enum type")
enum_dict = {}
for field in enum_type.fields():
# The enum's value is stored in "bitpos".
enum_dict[field.name] = field.bitpos
return enum_dict
|