LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
printers_oldgdb.py
Go to the documentation of this file.
1 """
2 Code that works with gdb 7.1's python pretty printing. When gdb >= 7.2 is widely available this
3 file should be deleted (it's only used after importing gdb.printing fails)
4 """
5 import gdb
6 import re
7 
8 class CitizenPrinter(object):
9  "Print a Citizen"
10 
11  def __init__(self, typename, val):
12  self.val = val
13 
14  def to_string(self):
15  return "{0x%x %d %s 0x%x}" % (self.val, self.val["_CitizenId"],
16  self.val["_typeName"], self.val["_sentinel"])
17 
18 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
19 # afw
20 
22  "Print a BaseSourceAttributes"
23 
24  def __init__(self, typename, val):
25  self.val = val
26 
27  def to_string(self):
28  return "Base: {id=%d astrom=(%.3f, %.3f)}" % (self.val["_id"], self.val["_xAstrom"], self.val["_yAstrom"])
29 
30 class SourcePrinter(object):
31  "Print a Source"
32 
33  def __init__(self, typename, val):
34  self.val = val
35 
36  def to_string(self):
37  return "{id=%d astrom=(%.3f, %.3f)}" % (self.val["_id"], self.val["_xAstrom"], self.val["_yAstrom"])
38 
39 class FootprintPrinter(object):
40  "Print a Footprint"
41 
42  def __init__(self, typename, val):
43  self.val = val
44 
45  def to_string(self):
46  return "RHL Footprint (fixme when gdb 7.3 arrives)"
47 
48 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
49 
50 class CoordinateBasePrinter(object):
51  "Print a CoordinateBase"
52 
53  def __init__(self, typename, val):
54  self.val = val
55 
56  def to_string(self):
57  # Make sure &foo works, too.
58  type = self.val.type
59  if type.code == gdb.TYPE_CODE_REF:
60  type = type.target ()
61 
62  return self.val["_vector"]["m_storage"]["m_data"]["array"]
63 
64  def display_hint (self):
65  return "array"
66 
67 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
68 
69 class ImagePrinter(object):
70  "Print an ImageBase or derived class"
71 
72  def dimenStr(self, val=None):
73  if val is None:
74  val = self.val
75 
76  # Make sure &foo works, too.
77  type = val.type
78  if type.code == gdb.TYPE_CODE_REF:
79  type = type.target ()
80 
81  gilView = val["_gilView"]
82  arr = val["_origin"]["_vector"]["m_storage"]["m_data"]["array"]
83 
84  return "%dx%d+%d+%d" % (
85  #val["getWidth"](), val["getHeight"](),
86  gilView["_dimensions"]["x"], gilView["_dimensions"]["y"],
87  arr[0], arr[1])
88 
89  def typeName(self):
90  return self.typename.split(":")[-1]
91 
92  def __init__(self, typename, val):
93  self.typename = typename
94  self.val = val
95 
96  def to_string(self):
97  return "%s(%s)" % (self.typeName(), self.dimenStr())
98 
100  "Print a MaskedImage"
101 
102  def to_string(self):
103  return "%s(%s)" % (self.typeName(), self.dimenStr(self.val["_image"]["px"].dereference()))
104 
106  "Print an Exposure"
107 
108  def to_string(self):
109  return "%s(%s)" % (self.typeName(),
110  self.dimenStr(self.val["_maskedImage"]["_image"]["px"].dereference()))
111 
112 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
113 
114 class PrintImageCommand(gdb.Command):
115  """Print an Image
116 Usage: image x0 y0 [nx [ny] [centerPatch] [obeyXY0]]
117 """
118 
119  def __init__ (self):
120  super (PrintImageCommand, self).__init__ ("show image",
121  gdb.COMMAND_DATA,
122  gdb.COMPLETE_SYMBOL)
123 
124  def get(self, var, x, y):
125  if False:
126  return var["operator()(int, int, bool)"](x, y, True)
127  else:
128  dimensions = var["_gilView"]["_dimensions"]
129  if x < 0 or x >= dimensions["x"] or y < 0 or y >= dimensions["y"]:
130  raise gdb.GdbError("Pixel (%d, %d) is out of range 0:%d, 0:%d" %
131  (x, y, dimensions["x"] - 1, dimensions["y"] - 1))
132 
133  pixels = var["_gilView"]["_pixels"]["_p"]
134  step = pixels["_step_fn"]["_step"]/var.type.template_argument(0).sizeof
135 
136  return pixels["m_iterator"][x + y*step]["_v0"]
137 
138  def invoke (self, args, fromTty):
139  self.dont_repeat()
140 
141  args = gdb.string_to_argv(args)
142  if len(args) < 1:
143  raise gdb.GdbError("Please specify an image")
144  imgName = args.pop(0)
145  var = gdb.parse_and_eval(imgName)
146 
147  if re.search(r"MaskedImage", str(var.type)):
148  print "N.b. %s is a MaskedImage; showing image" % (imgName)
149  var = var["_image"]
150 
151  if re.search(r"shared_ptr<", str(var.type)):
152  var = var["px"].dereference()
153 
154  if var.type.code == gdb.TYPE_CODE_PTR:
155  var = var.dereference() # be nice
156 
157  pixelTypeName = str(var.type.template_argument(0))
158 
159  if len(args) < 2:
160  raise gdb.GdbError("Please specify a pixel's x and y indexes")
161 
162  x0 = gdb.parse_and_eval(args.pop(0))
163  y0 = gdb.parse_and_eval(args.pop(0))
164 
165  if len(args) == 0:
166  print "%g" % self.get(var, x0, y0)
167  return
168 
169  nx = int(args.pop(0))
170  if args:
171  ny = int(args.pop(0))
172  else:
173  ny = 1
174 
175  if args:
176  centerPatch = gdb.parse_and_eval(args.pop(0))
177  if centerPatch:
178  x0 -= nx//2
179  y0 -= ny//2
180 
181  if args:
182  obeyXY0 = gdb.parse_and_eval(args.pop(0))
183 
184  if obeyXY0:
185  arr = var["_origin"]["_vector"]["m_storage"]["m_data"]["array"]
186 
187  x0 -= arr[0]
188  y0 -= arr[1]
189 
190  if args:
191  raise gdb.GdbError('Unexpected trailing arguments: "%s"' % '", "'.join(args))
192  #
193  # OK, finally time to print
194  #
195  if pixelTypeName in ["short", "unsigned short"]:
196  dataFmt = "0x%x"
197  elif pixelTypeName in ["int", "unsigned int"]:
198  dataFmt = "%d"
199  else:
200  dataFmt = "%.2f"
201 
202  print "%-4s" % "",
203  for x in range(x0, x0 + nx):
204  print "%8d" % x,
205  print ""
206 
207  for y in reversed(range(y0, y0 + ny)):
208  print "%-4d" % y,
209  for x in range(x0, x0 + nx):
210  print "%8s" % (dataFmt % self.get(var, x, y)),
211  print ""
212 
213 
215 
216 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
217 #
218 # These two classes (RxPrinter and Printer) come directly from
219 # python/libstdcxx/v6/printers.py and the GPL license therein applies
220 #
221 # A "regular expression" printer which conforms to the
222 # "SubPrettyPrinter" protocol from gdb.printing.
223 class RxPrinter(object):
224  def __init__(self, name, function):
225  super(RxPrinter, self).__init__()
226  self.name = name
227  self.function = function
228  self.enabled = True
229 
230  def invoke(self, value):
231  if not self.enabled:
232  return None
233  return self.function(self.name, value)
234 
235 # A pretty-printer that conforms to the "PrettyPrinter" protocol from
236 # gdb.printing. It can also be used directly as an old-style printer.
237 #
238 class Printer(object):
239  def __init__(self, name):
240  super(Printer, self).__init__()
241  self.name = name
242  self.subprinters = []
243  self.lookup = {}
244  self.enabled = True
245  self.compiled_rx = re.compile('^([a-zA-Z0-9_:]+)<.*>$')
246 
247  def add(self, name, function):
248  # A small sanity check.
249  # FIXME
250  if not self.compiled_rx.match(name + '<>'):
251  raise ValueError, 'libstdc++ programming error: "%s" does not match' % name
252  printer = RxPrinter(name, function)
253  self.subprinters.append(printer)
254  self.lookup[name] = printer
255 
256  @staticmethod
257  def get_basic_type(type):
258  # If it points to a reference, get the reference.
259  if type.code == gdb.TYPE_CODE_REF:
260  type = type.target ()
261 
262  # Get the unqualified type, stripped of typedefs.
263  type = type.unqualified ().strip_typedefs ()
264 
265  return type.tag
266 
267  def __call__(self, val):
268  typename = self.get_basic_type(val.type)
269  if not typename:
270  return None
271 
272  # All the types we match are template types, so we can use a
273  # dictionary.
274  match = self.compiled_rx.match(typename)
275  if not match:
276  return None
277 
278  basename = match.group(1)
279  if basename in self.lookup:
280  return self.lookup[basename].invoke(val)
281 
282  # Cannot find a pretty printer. Return None.
283  return None
284 
285 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
286 
287 printers = []
288 
289 def register(obj):
290  "Register my pretty-printers with objfile Obj."
291 
292  if obj is None:
293  obj = gdb
294 
295  for p in printers:
296  obj.pretty_printers.insert(0, p)
297 
298 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
299 
301  printer = Printer("afw")
302 
303  printer.add('lsst::afw::detection::Footprint', FootprintPrinter)
304  printer.add('lsst::afw::detection::Source', SourcePrinter)
305  printer.add('lsst::afw::detection::BaseSourceAttributes', BaseSourceAttributesPrinter)
306 
307  printer.add('lsst::afw::geom::Point', CoordinateBasePrinter)
308  printer.add('lsst::afw::geom::Extent', CoordinateBasePrinter)
309 
310  printer.add('lsst::afw::image::ImageBase', ImagePrinter)
311  printer.add('lsst::afw::image::Image', ImagePrinter)
312  printer.add('lsst::afw::image::Mask', ImagePrinter)
313  printer.add('lsst::afw::image::MaskedImage', MaskedImagePrinter)
314  printer.add('lsst::afw::image::Exposure', ExposurePrinter)
315 
316  return printer
317 
318 printers.append(build_afw_dictionary())
319 
321  printer = Printer("daf::base")
322 
323  printer.add('lsst::daf::base::Citizen', CitizenPrinter)
324 
325  return printer
326 
327 printers.append(build_daf_base_dictionary())