16 """A subclass of the standard optparse OptionParser for gdb 18 GdbOptionParser raises GdbError rather than exiting when asked for help, or 19 when given an illegal value. E.g. 21 parser = gdb.printing.GdbOptionParser("show image") 22 parser.add_option("-a", "--all", action="store_true", 23 help="Display the whole image") 24 parser.add_option("-w", "--width", type="int", default=8, 25 help="Field width for pixels") 27 opts, args = parser.parse_args(args) 32 Like optparse.OptionParser's API, but with an initial command name argument 35 if not kwargs.get(
"prog"):
37 optparse.OptionParser.__init__(self, *args, **kwargs)
40 """Call OptionParser.parse_args after running gdb.string_to_argv""" 44 args = gdb.string_to_argv(args)
48 help = (
"-h" in args
or "--help" in args)
49 opts, args = optparse.OptionParser.parse_args(self, args, values)
56 def exit(self, status=0, msg=""):
57 """Raise GdbError rather than exiting""" 60 print(msg, file=sys.stderr)
62 raise gdb.GdbError(msg)
76 return "shared_ptr(%s)" % self.
val[
"px"].dereference()
81 "Print a boost::gil pixel" 89 return self.
val[
"_v0"]
92 m_storage = val[
"m_storage"]
94 nx, ny = m_storage[
"m_cols"], m_storage[
"m_rows"]
97 nx, ny = val.type.template_argument(1), \
98 val.type.template_argument(2)
101 size = m_storage[
"m_data"][
"array"].type.sizeof
102 size0 = m_storage[
"m_data"][
"array"].dereference().type.sizeof
104 nx =
int(math.sqrt(size/size0))
110 if re.search(
r"Matrix",
str(var.type)):
112 return var[
"operator()(int, int)"](x, y)
116 if x < 0
or x >= NX
or y < 0
or y >= NY:
117 raise gdb.GdbError(
"Element (%d, %d) is out of range 0:%d, 0:%d" %
118 (x, y, NX - 1, NY - 1))
120 m_data = var[
"m_storage"][
"m_data"]
125 m_data = m_data.address.cast(m_data.type)
128 val = m_data[x + y*NX]
130 val = m_data[
"array"][x + y*NX]
133 return var[
"operator()(int)"](x)
139 "Element (%d) is out of range 0:%d" % (x, NX - 1))
141 m_data = var[
"m_storage"][
"m_data"]
145 m_data = m_data.address.cast(m_data.type)
150 val = m_data[
"array"][x]
152 if val.type.code == gdb.TYPE_CODE_INT:
154 elif val.type.code == gdb.TYPE_CODE_FLT:
160 "Print an Eigen Matrix" 168 return "%s{%dx%d}" % (self.
val.type, nx, ny)
171 "Print an Eigen Vector" 177 m_storage = self.
val[
"m_storage"]
183 n = m_storage.type.template_argument(1)
187 size = m_storage[
"m_data"][
"array"].type.sizeof
188 size0 = m_storage[
"m_data"][
"array"].dereference(
190 n = math.sqrt(size/size0)
195 """Print an eigen Matrix or Vector 196 Usage: show eigen <matrix> [x0 y0 [nx ny]] 197 show eigen <vector> [x0 [nx]] 201 super(PrintEigenCommand, self).
__init__(
"show eigen",
205 def _mget(self, var, x, y=0):
208 def _vget(self, var, x):
215 parser.add_option(
"-d",
"--dataFmt", default=
"%.2f",
216 help=
"Format for values")
217 parser.add_option(
"-f",
"--formatWidth", type=
"int",
218 default=8, help=
"Field width for values")
219 parser.add_option(
"-o",
"--origin", type=
"str", nargs=
"+",
220 help=
"Origin of the part of the object to print")
223 "eigenObject", help=
"Expression giving Eigen::Matrix/Vector to show")
225 "nx", help=
"Width of patch to print", type=
"int", default=0, nargs=
"?")
227 "ny", help=
"Height of patch to print", type=
"int", default=0, nargs=
"?")
229 opts = parser.parse_args(args)
233 (opts, args) = parser.parse_args(args)
238 raise gdb.GdbError(
"Please specify an object")
239 opts.eigenObject = args.pop(0)
241 opts.nx, opts.ny = 0, 0
243 opts.nx =
int(args.pop(0))
245 opts.ny =
int(args.pop(0))
249 "Unrecognised trailing arguments: %s" %
" ".join(args))
251 var = gdb.parse_and_eval(opts.eigenObject)
253 if not re.search(
r"(Eigen|LinearTransform)::(Matrix|Vector)",
str(var.type)):
255 "Please specify an eigen matrix or vector, not %s" % var.type)
257 if re.search(
r"shared_ptr<",
str(var.type)):
258 var = var[
"px"].dereference()
260 if var.type.code == gdb.TYPE_CODE_PTR:
261 var = var.dereference()
263 isMatrix = re.search(
r"Matrix",
str(var.type))
268 if len(opts.origin) != 2:
269 raise gdb.GdbError(
"Please specify both x0 and y0")
271 x0 = gdb.parse_and_eval(opts.origin[0])
272 y0 = gdb.parse_and_eval(opts.origin[1])
283 if nx == 1
and ny == 1:
284 print(
"%g" % self.
_vget(var, x0))
287 NX = 0, var[
"m_storage"][
"n"]
290 if len(opts.origin) != 1:
291 raise gdb.GdbError(
"Please only specify x0")
293 x0 = gdb.parse_and_eval(opts.origin[0])
302 print(
"%g" % self.
_vget(var, x0))
308 print(
"%-4s" %
"", end=
' ')
309 for x
in range(x0,
min(NX, x0 + nx)):
310 print(
"%*d" % (opts.formatWidth, x), end=
' ')
313 for y
in range(y0,
min(NY, y0 + ny)):
314 print(
"%-4d" % y, end=
' ')
315 for x
in range(x0,
min(NX, x0 + nx)):
316 print(
"%*s" % (opts.formatWidth, (opts.dataFmt %
317 self.
_mget(var, x, y))), end=
' ')
320 for x
in range(x0,
min(NX, x0 + nx)):
321 print(
"%*s" % (opts.formatWidth, (opts.dataFmt %
322 self.
_vget(var, x))), end=
' ')
334 sentinel =
int(self.
val[
"_sentinel"].cast(
335 gdb.lookup_type(
"unsigned int")))
336 return "{%s %d 0x%x}" % (self.
val.address, self.
val[
"_CitizenId"], sentinel)
340 Usage: show citizen <obj> 344 super(PrintCitizenCommand, self).
__init__(
"show citizen",
353 parser.add_option(
"object", help=
"The object in question")
355 opts = parser.parse_args(args)
359 opts, args = parser.parse_args(args)
364 raise gdb.GdbError(
"Please specify an object")
365 opts.object = args.pop(0)
369 "Unrecognised trailing arguments: %s" %
" ".join(args))
371 var = gdb.parse_and_eval(opts.object)
372 if re.search(
r"shared_ptr<",
str(var.type)):
375 if var.type.code != gdb.TYPE_CODE_PTR:
378 citizen = var.dynamic_cast(gdb.lookup_type(
379 "lsst::daf::base::Citizen").pointer())
383 "Failed to cast %s to Citizen -- is it a subclass?" % opts.object)
385 citizen = citizen.dereference()
394 "Print a BaseSourceAttributes" 400 return "Base: {id=%d astrom=(%.3f, %.3f)}" % (self.
val[
"_id"],
401 self.
val[
"_xAstrom"],
402 self.
val[
"_yAstrom"])
411 return "Source{id=%d astrom=(%.3f, %.3f)}" % (self.
val[
"_id"],
412 self.
val[
"_xAstrom"],
413 self.
val[
"_yAstrom"])
416 "Print a cameraGeom::Detector" 422 return "Detector{name: %s id: %s type: %s bbox: %s}" % (self.
val[
"_name"], self.
val[
"_id"],
423 self.
val[
"_type"], self.
val[
"_bbox"])
434 nspan = self.
val[
"_spans"][
"size"]()
436 vec_impl = self.
val[
"_spans"][
"_M_impl"]
437 nspan = vec_impl[
"_M_finish"] - vec_impl[
"_M_start"]
439 return "Footprint{id=%d, nspan=%d, area=%d; BBox %s}" % (self.
val[
"_fid"], nspan,
440 self.
val[
"_area"], self.
val[
"_bbox"])
443 "Print a FootprintSet" 449 return "FootprintSet{%s; %s}" % (self.
val[
"_region"], self.
val[
"_footprints"])
458 return "Peak{%d, (%.2f, %.2f)}" % (self.
val[
"_id"], self.
val[
"_fx"], self.
val[
"_fy"])
464 return "%s" % (self.typeName())
475 if type.code == gdb.TYPE_CODE_REF:
483 return "Box2{(%s,%s)--(%s,%s)}" % (llc[0], llc[1],
484 llc[0] + dims[0] - 1, llc[1] + dims[1] - 1)
490 "Print a CoordinateBase" 496 return self.
val[
"_vector"][
"m_storage"][
"m_data"][
"array"]
502 "Print an ellipse::Axes" 508 vec = self.
val[
"_vector"]
512 "Print an ellipse::Quadrupole" 518 mat = self.
val[
"_matrix"]
527 "Print an ImageBase or derived class" 535 if type.code == gdb.TYPE_CODE_REF:
538 gilView = val[
"_gilView"]
539 arr = val[
"_origin"][
"_vector"][
"m_storage"][
"m_data"][
"array"]
541 x0, y0 = arr[0], arr[1]
542 return "%dx%d%s%d%s%d" % (
544 gilView[
"_dimensions"][
"x"], gilView[
"_dimensions"][
"y"],
546 [
"",
"+"][x0 >= 0], x0,
547 [
"",
"+"][y0 >= 0], y0)
560 "Print a MaskedImage" 570 self.
dimenStr(self.
val[
"_maskedImage"][
"_image"][
"px"].dereference()))
576 super(PrintImageCommand, self).
__init__(
"show image",
582 return var[
"operator()(int, int, bool)"](x, y,
True)
584 dimensions = var[
"_gilView"][
"_dimensions"]
585 if x < 0
or x >= dimensions[
"x"]
or y < 0
or y >= dimensions[
"y"]:
586 raise gdb.GdbError(
"Pixel (%d, %d) is out of range 0:%d, 0:%d" %
587 (x, y, dimensions[
"x"] - 1, dimensions[
"y"] - 1))
589 pixels = var[
"_gilView"][
"_pixels"][
"_p"]
590 step = pixels[
"_step_fn"][
"_step"] / \
591 var.type.template_argument(0).sizeof
593 return pixels[
"m_iterator"][x + y*step][
"_v0"]
599 "show image" + (
"" if argparse
else " <image> [<nx> [<ny>]]"))
600 parser.add_option(
"-a",
"--all", action=
"store_true",
601 help=
"Display the whole image/mask")
602 parser.add_option(
"-c",
"--center", type=
"str", nargs=2, default=(
None,
None,),
603 help=
"Center the output at (x, y)")
604 parser.add_option(
"-o",
"--origin", type=
"str", nargs=2, default=(
None,
None,),
605 help=
"Print the region starting at (x, y)")
606 parser.add_option(
"-x",
"--xy0", action=
"store_true",
607 help=
"Obey the image's (x0, y0)")
608 parser.add_option(
"-f",
"--formatWidth", type=
"int",
609 default=8, help=
"Field width for values")
610 parser.add_option(
"-d",
"--dataFmt", default=
"%.2f",
611 help=
"Format for values")
615 "image", help=
"Expression giving image to show")
617 "width", help=
"Width of patch to print", default=1, nargs=
"?")
619 "height", help=
"Height of patch to print", default=1, nargs=
"?")
621 opts = parser.parse_args(args)
625 opts, args = parser.parse_args(args)
630 raise gdb.GdbError(
"Please specify an image")
632 opts.image = args.pop(0)
634 opts.width, opts.height = 1, 1
636 opts.width =
int(args.pop(0))
638 opts.height =
int(args.pop(0))
642 "Unrecognised trailing arguments: %s" %
" ".join(args))
646 if opts.origin[i]
is None:
647 if opts.center[i]
is not None:
651 if opts.center[i]
is not None:
653 "You may not specify both --center and --origin")
655 val = gdb.parse_and_eval(val)
664 nx, ny = opts.width, opts.height
666 var = gdb.parse_and_eval(opts.image)
668 if re.search(
r"shared_ptr<",
str(var.type)):
669 var = var[
"px"].dereference()
671 if not re.search(
r"(lsst::afw::image::)?(Image|Mask|MaskedImage)",
str(var.type.unqualified())):
673 "Please specify an image, not %s" % var.type)
675 if re.search(
r"MaskedImage",
str(var.type))
and \
676 not re.search(
r"::Image(\s*&)?$",
str(var.type)):
677 print(
"N.b. %s is a MaskedImage; showing image" % (opts.image))
680 if re.search(
r"shared_ptr<",
str(var.type)):
681 var = var[
"px"].dereference()
683 if var.type.code == gdb.TYPE_CODE_PTR:
684 var = var.dereference()
686 pixelTypeName =
str(var.type.template_argument(0))
688 dataFmt = opts.dataFmt
689 elif pixelTypeName
in [
"short",
"unsigned short"]:
691 elif pixelTypeName
in [
"int",
"unsigned int"]:
697 nx = var[
"_gilView"][
"_dimensions"][
"x"]
699 ny = var[
"_gilView"][
"_dimensions"][
"y"]
705 if opts.xy0
and not opts.all:
706 arr = var[
"_origin"][
"_vector"][
"m_storage"][
"m_data"][
"array"]
713 print(
"%-4s" %
"", end=
' ')
714 for x
in range(x0, x0 + nx):
715 print(
"%*d" % (opts.formatWidth, x), end=
' ')
718 for y
in reversed(
list(range(y0, y0 + ny))):
719 print(
"%-4d" % y, end=
' ')
720 for x
in range(x0, x0 + nx):
721 print(
"%*s" % (opts.formatWidth, dataFmt %
722 self.
get(var, x, y)), end=
' ')
735 return "Background(%dx%d) %s %s" % (
736 self.
val[
"_imgWidth"], self.
val[
"_imgHeight"],
740 "Print a BackgroundControl" 747 return "{%s %s %s %s}" % (re.sub(
r"lsst::afw::math::Interpolate::",
"",
str(self.
val[
"_style"])),
748 re.sub(
r"lsst::afw::math::",
"",
750 re.sub(
r"lsst::afw::math::",
"",
str(
751 self.
val[
"_undersampleStyle"])),
752 self.
val[
"_sctrl"][
"px"].dereference())
762 return "%s(%dx%d)" % (self.
typename,
763 self.
val[
"_width"], self.
val[
"_height"])
766 "Print a StatisticsControl" 773 return "{nSigma=%g nIter=%d ignore=0x%x}" % (self.
val[
"_numSigmaClip"],
774 self.
val[
"_numIter"],
775 self.
val[
"_andMask"])
778 "Print a table::Table" 785 return "{schema = %s, md=%s}" % (self.
val[
"_schema"], self.
val[
"_metadata"])
788 "Print a table::Schema" 795 names =
str(self.
val[
"_impl"][
"px"][
"_names"])
796 names = re.sub(
r"^[^{]*{|}|[\[\]\"\"]|\s*=\s*[^,]*",
"", names)
798 return "%s" % (names)
803 "Register my pretty-printers with objfile Obj." 809 gdb.printing.register_pretty_printer(obj, p, replace=
True)
812 """Surely this must be somewhere standard?""" 814 printer = gdb.printing.RegexpCollectionPrettyPrinter(
"rhl-boost")
816 printer.add_printer(
'boost::shared_ptr',
817 '^(boost|tr1|std)::shared_ptr', SharedPtrPrinter)
818 printer.add_printer(
'boost::gil::pixel',
819 'boost::gil::.*pixel_t', GilPixelPrinter)
826 """Surely this must be somewhere standard?""" 828 printer = gdb.printing.RegexpCollectionPrettyPrinter(
"rhl-eigen")
830 printer.add_printer(
'eigen::Matrix',
831 '^Eigen::Matrix', EigenMatrixPrinter)
832 printer.add_printer(
'eigen::Vector',
833 '^Eigen::Vector', EigenVectorPrinter)
840 printer = gdb.printing.RegexpCollectionPrettyPrinter(
"afw")
842 printer.add_printer(
'lsst::afw::cameraGeom::Detector',
843 '^lsst::afw::cameraGeom::(Amp|Ccd|Detector|DetectorMosaic)$', DetectorPrinter)
845 printer.add_printer(
'lsst::afw::detection::Footprint',
846 '^lsst::afw::detection::Footprint$', FootprintPrinter)
847 printer.add_printer(
'lsst::afw::detection::FootprintSet',
848 '^lsst::afw::detection::FootprintSet', FootprintSetPrinter)
849 printer.add_printer(
'lsst::afw::detection::Peak',
850 '^lsst::afw::detection::Peak$', PeakPrinter)
851 printer.add_printer(
'lsst::afw::detection::Psf',
852 '^lsst::afw::detection::Psf$', PsfPrinter)
853 printer.add_printer(
'lsst::afw::detection::Source',
854 '^lsst::afw::detection::Source$', SourcePrinter)
855 printer.add_printer(
'lsst::afw::detection::BaseSourceAttributes',
856 '^lsst::afw::detection::BaseSourceAttributes$', BaseSourceAttributesPrinter)
858 printer.add_printer(
'lsst::afw::geom::Box',
859 '^lsst::afw::geom::Box', Box2Printer)
860 printer.add_printer(
'lsst::afw::geom::Extent',
861 '^lsst::afw::geom::Extent', CoordinateBasePrinter)
862 printer.add_printer(
'lsst::afw::geom::Point',
863 '^lsst::afw::geom::Point', CoordinateBasePrinter)
865 printer.add_printer(
'lsst::afw::geom::ellipses::Axes',
866 '^lsst::afw::geom::ellipses::Axes', AxesPrinter)
867 printer.add_printer(
'lsst::afw::geom::ellipses::Quadrupole',
868 '^lsst::afw::geom::ellipses::Quadrupole', QuadrupolePrinter)
870 printer.add_printer(
'lsst::afw::image::ImageBase',
871 'lsst::afw::image::ImageBase<[^>]+>$', ImagePrinter)
872 printer.add_printer(
'lsst::afw::image::Image',
873 'lsst::afw::image::Image<[^>]+>$', ImagePrinter)
874 printer.add_printer(
'lsst::afw::image::Mask',
875 '^lsst::afw::image::Mask<[^>]+>$', ImagePrinter)
876 printer.add_printer(
'lsst::afw::image::MaskedImage',
877 '^lsst::afw::image::MaskedImage<[^>]+>$', MaskedImagePrinter)
878 printer.add_printer(
'lsst::afw::image::Exposure',
879 '^lsst::afw::image::Exposure', ExposurePrinter)
881 printer.add_printer(
'lsst::afw::math::Background',
882 '^lsst::afw::math::Background$', BackgroundPrinter)
883 printer.add_printer(
'lsst::afw::math::BackgroundControl',
884 '^lsst::afw::math::BackgroundControl$', BackgroundControlPrinter)
885 printer.add_printer(
'lsst::afw::math::Kernel',
886 '^lsst::afw::math::.*Kernel', KernelPrinter)
887 printer.add_printer(
'lsst::afw::math::StatisticsControl',
888 '^lsst::afw::math::StatisticsControl', StatisticsControlPrinter)
890 printer.add_printer(
'lsst::afw::table::Table',
891 '^lsst::afw::table::.*Table$', TablePrinter)
892 printer.add_printer(
'lsst::afw::table::Schema',
893 '^lsst::afw::table::Schema$', TableSchemaPrinter)
900 printer = gdb.printing.RegexpCollectionPrettyPrinter(
"daf::base")
902 printer.add_printer(
'lsst::daf::base::Citizen',
903 'lsst::daf::base::Citizen', CitizenPrinter)
908 except ImportError
as e:
910 from .printers_oldgdb
import *
def build_daf_base_dictionary()
def __init__(self, prog, args, kwargs)
def build_boost_dictionary()
def build_afw_dictionary()
def getEigenValue(var, x, y=0)
def dimenStr(self, val=None)
def invoke(self, args, fromTty)
def build_eigen_dictionary()
def parse_args(self, args, values=None)
def getEigenMatrixDimensions(val)
def exit(self, status=0, msg="")
def invoke(self, args, fromTty)
daf::base::PropertyList * list
def invoke(self, args, fromTty)
def _mget(self, var, x, y=0)