3 # This is not quite right, as local vars may override symname
4 def read_global_var (symname
):
5 return gdb
.selected_frame().read_var(symname
)
7 def g_quark_to_string (quark
):
13 val
= read_global_var ("g_quarks")
14 max_q
= long(read_global_var ("g_quark_seq_id"))
16 return val
[quark
].string()
19 # We override the node printers too, so that node->next is not expanded
20 class GListNodePrinter
:
23 def __init__ (self
, val
):
27 return "{data=%s, next=0x%x, prev=0x%x}" % (str(self
.val
["data"]), long(self
.val
["next"]), long(self
.val
["prev"]))
29 class GSListNodePrinter
:
30 "Prints a GSList node"
32 def __init__ (self
, val
):
36 return "{data=%s, next=0x%x}" % (str(self
.val
["data"]), long(self
.val
["next"]))
42 def __init__(self
, head
, listtype
):
44 self
.listtype
= listtype
53 data
= self
.link
['data']
54 self
.link
= self
.link
['next']
56 self
.count
= self
.count
+ 1
57 return ('[%d]' % count
, data
)
59 def __init__ (self
, val
, listtype
):
61 self
.listtype
= listtype
64 return self
._iterator
(self
.val
, self
.listtype
)
67 return "0x%x" % (long(self
.val
))
69 def display_hint (self
):
76 def __init__(self
, ht
, keys_are_strings
):
79 self
.array
= ht
["nodes"]
80 self
.size
= ht
["size"]
82 self
.keys_are_strings
= keys_are_strings
91 if self
.value
!= None:
95 while long(self
.pos
) < long(self
.size
):
96 node
= self
.array
[self
.pos
]
97 self
.pos
= self
.pos
+ 1
98 if long (node
["key_hash"]) >= 2:
102 if self
.keys_are_strings
:
103 key
= key
.cast (gdb
.lookup_type("char").pointer())
105 # Queue value for next result
106 self
.value
= ('[%dv]'% (self
.pos
), val
)
109 return ('[%dk]'% (self
.pos
), key
)
112 def __init__ (self
, val
):
114 self
.keys_are_strings
= False
116 string_hash
= read_global_var ("g_str_hash")
119 string_hash
= read_global_var ("IA__g_str_hash")
122 if self
.val
!= 0 and string_hash
!= None and self
.val
["hash_func"] == string_hash
:
123 self
.keys_are_strings
= True
126 return self
._iterator
(self
.val
, self
.keys_are_strings
)
128 def to_string (self
):
129 return "0x%x" % (long(self
.val
))
131 def display_hint (self
):
134 def pretty_printer_lookup (val
):
135 if is_g_type_instance (val
):
136 return GTypePrettyPrinter (val
)
138 def pretty_printer_lookup (val
):
139 # None yet, want things like hash table and list
141 type = val
.type.unqualified()
143 # If it points to a reference, get the reference.
144 if type.code
== gdb
.TYPE_CODE_REF
:
145 type = type.target ()
147 if type.code
== gdb
.TYPE_CODE_PTR
:
148 type = type.target().unqualified()
151 return GListPrinter(val
, "GList")
153 return GListPrinter(val
, "GSList")
154 if t
== "GHashTable":
155 return GHashPrinter(val
)
159 return GListNodePrinter(val
)
161 return GListPrinter(val
, "GSList")
168 obj
.pretty_printers
.append(pretty_printer_lookup
)
170 class ForeachCommand (gdb
.Command
):
171 """Foreach on list"""
174 super (ForeachCommand
, self
).__init
__ ("gforeach",
178 def valid_name (self
, name
):
179 if not name
[0].isalpha():
183 def parse_args (self
, arg
):
186 raise Exception ("No var specified")
188 if not self
.valid_name(var
):
189 raise Exception ("Invalid variable name")
191 while i
< len (arg
) and arg
[i
].isspace():
194 if arg
[i
:i
+2] != "in":
195 raise Exception ("Invalid syntax, missing in")
199 while i
< len (arg
) and arg
[i
].isspace():
202 colon
= arg
.find (":", i
)
204 raise Exception ("Invalid syntax, missing colon")
209 while colon
< len (arg
) and arg
[colon
].isspace():
212 command
= arg
[colon
:]
214 return (var
, val
, command
)
216 def do_iter(self
, arg
, item
, command
):
217 item
= item
.cast (gdb
.lookup_type("void").pointer())
219 to_eval
= "set $%s = (void *)0x%x\n"%(arg
, item
)
223 def slist_iterator (self
, arg
, container
, command
):
224 l
= container
.cast (gdb
.lookup_type("GSList").pointer())
226 self
.do_iter (arg
, l
["data"], command
)
229 def list_iterator (self
, arg
, container
, command
):
230 l
= container
.cast (gdb
.lookup_type("GList").pointer())
232 self
.do_iter (arg
, l
["data"], command
)
235 def pick_iterator (self
, container
):
236 t
= container
.type.unqualified()
237 if t
.code
== gdb
.TYPE_CODE_PTR
:
238 t
= t
.target().unqualified()
241 return self
.slist_iterator
243 return self
.list_iterator
244 raise Exception("Invalid container type %s"%(str(container
.type)))
246 def invoke (self
, arg
, from_tty
):
247 (var
, container
, command
) = self
.parse_args(arg
)
248 container
= gdb
.parse_and_eval (container
)
249 func
= self
.pick_iterator(container
)
250 func(var
, container
, command
)