4 if sys
.version_info
[0] >= 3:
7 # This is not quite right, as local vars may override symname
8 def read_global_var (symname
):
9 return gdb
.selected_frame().read_var(symname
)
11 def g_quark_to_string (quark
):
18 val
= read_global_var ("quarks")
19 max_q
= long(read_global_var ("quark_seq_id"))
22 val
= read_global_var ("g_quarks")
23 max_q
= long(read_global_var ("g_quark_seq_id"))
27 return val
[quark
].string()
30 # We override the node printers too, so that node->next is not expanded
31 class GListNodePrinter
:
34 def __init__ (self
, val
):
38 return "{data=%s, next=0x%x, prev=0x%x}" % (str(self
.val
["data"]), long(self
.val
["next"]), long(self
.val
["prev"]))
40 class GSListNodePrinter
:
41 "Prints a GSList node"
43 def __init__ (self
, val
):
47 return "{data=%s, next=0x%x}" % (str(self
.val
["data"]), long(self
.val
["next"]))
53 def __init__(self
, head
, listtype
):
55 self
.listtype
= listtype
64 data
= self
.link
['data']
65 self
.link
= self
.link
['next']
67 self
.count
= self
.count
+ 1
68 return ('[%d]' % count
, data
)
72 def __init__ (self
, val
, listtype
):
74 self
.listtype
= listtype
77 return self
._iterator
(self
.val
, self
.listtype
)
80 return "0x%x" % (long(self
.val
))
82 def display_hint (self
):
89 def __init__(self
, ht
, keys_are_strings
):
92 self
.keys
= ht
["keys"]
93 self
.values
= ht
["values"]
94 self
.hashes
= ht
["hashes"]
95 self
.size
= ht
["size"]
97 self
.keys_are_strings
= keys_are_strings
106 if self
.value
!= None:
110 while long(self
.pos
) < long(self
.size
):
111 self
.pos
= self
.pos
+ 1
112 if long (self
.hashes
[self
.pos
]) >= 2:
113 key
= self
.keys
[self
.pos
]
114 val
= self
.values
[self
.pos
]
116 if self
.keys_are_strings
:
117 key
= key
.cast (gdb
.lookup_type("char").pointer())
119 # Queue value for next result
120 self
.value
= ('[%dv]'% (self
.pos
), val
)
123 return ('[%dk]'% (self
.pos
), key
)
128 def __init__ (self
, val
):
130 self
.keys_are_strings
= False
132 string_hash
= read_global_var ("g_str_hash")
135 if self
.val
!= 0 and string_hash
!= None and self
.val
["hash_func"] == string_hash
:
136 self
.keys_are_strings
= True
139 return self
._iterator
(self
.val
, self
.keys_are_strings
)
141 def to_string (self
):
142 return "0x%x" % (long(self
.val
))
144 def display_hint (self
):
147 def pretty_printer_lookup (val
):
148 # None yet, want things like hash table and list
150 type = val
.type.unqualified()
152 # If it points to a reference, get the reference.
153 if type.code
== gdb
.TYPE_CODE_REF
:
154 type = type.target ()
156 if type.code
== gdb
.TYPE_CODE_PTR
:
157 type = type.target().unqualified()
160 return GListPrinter(val
, "GList")
162 return GListPrinter(val
, "GSList")
163 if t
== "GHashTable":
164 return GHashPrinter(val
)
168 return GListNodePrinter(val
)
170 return GListPrinter(val
, "GSList")
177 obj
.pretty_printers
.append(pretty_printer_lookup
)
179 class ForeachCommand (gdb
.Command
):
180 """Foreach on list"""
183 super (ForeachCommand
, self
).__init
__ ("gforeach",
187 def valid_name (self
, name
):
188 if not name
[0].isalpha():
192 def parse_args (self
, arg
):
195 raise Exception ("No var specified")
197 if not self
.valid_name(var
):
198 raise Exception ("Invalid variable name")
200 while i
< len (arg
) and arg
[i
].isspace():
203 if arg
[i
:i
+2] != "in":
204 raise Exception ("Invalid syntax, missing in")
208 while i
< len (arg
) and arg
[i
].isspace():
211 colon
= arg
.find (":", i
)
213 raise Exception ("Invalid syntax, missing colon")
218 while colon
< len (arg
) and arg
[colon
].isspace():
221 command
= arg
[colon
:]
223 return (var
, val
, command
)
225 def do_iter(self
, arg
, item
, command
):
226 item
= item
.cast (gdb
.lookup_type("void").pointer())
228 to_eval
= "set $%s = (void *)0x%x\n"%(arg
, item
)
232 def slist_iterator (self
, arg
, container
, command
):
233 l
= container
.cast (gdb
.lookup_type("GSList").pointer())
235 self
.do_iter (arg
, l
["data"], command
)
238 def list_iterator (self
, arg
, container
, command
):
239 l
= container
.cast (gdb
.lookup_type("GList").pointer())
241 self
.do_iter (arg
, l
["data"], command
)
244 def pick_iterator (self
, container
):
245 t
= container
.type.unqualified()
246 if t
.code
== gdb
.TYPE_CODE_PTR
:
247 t
= t
.target().unqualified()
250 return self
.slist_iterator
252 return self
.list_iterator
253 raise Exception("Invalid container type %s"%(str(container
.type)))
255 def invoke (self
, arg
, from_tty
):
256 (var
, container
, command
) = self
.parse_args(arg
)
257 container
= gdb
.parse_and_eval (container
)
258 func
= self
.pick_iterator(container
)
259 func(var
, container
, command
)