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