1 from __future__
import absolute_import, division, print_function
2 from builtins
import str
3 from builtins
import range
4 from builtins
import object
5 from past.builtins
import long
21 """A subclass of the standard optparse OptionParser for gdb 23 GdbOptionParser raises GdbError rather than exiting when asked for help, or 24 when given an illegal value. E.g. 26 parser = gdb.printing.GdbOptionParser("show image") 27 parser.add_option("-a", "--all", action="store_true", 28 help="Display the whole image") 29 parser.add_option("-w", "--width", type="int", default=8, 30 help="Field width for pixels") 32 opts, args = parser.parse_args(args) 37 Like optparse.OptionParser's API, but with an initial command name argument 40 if not kwargs.get(
"prog"):
42 optparse.OptionParser.__init__(self, *args, **kwargs)
45 """Call OptionParser.parse_args after running gdb.string_to_argv""" 49 args = gdb.string_to_argv(args)
53 help = (
"-h" in args
or "--help" in args)
54 opts, args = optparse.OptionParser.parse_args(self, args, values)
61 def exit(self, status=0, msg=""):
62 """Raise GdbError rather than exiting""" 65 print(msg, file=sys.stderr)
67 raise gdb.GdbError(msg)
81 return "shared_ptr(%s)" % self.
val[
"px"].dereference()
86 "Print a boost::gil pixel" 94 return self.
val[
"_v0"]
97 m_storage = val[
"m_storage"]
99 nx, ny = m_storage[
"m_cols"], m_storage[
"m_rows"]
102 nx, ny = val.type.template_argument(1), \
103 val.type.template_argument(2)
106 size = m_storage[
"m_data"][
"array"].type.sizeof
107 size0 = m_storage[
"m_data"][
"array"].dereference().type.sizeof
109 nx = int(math.sqrt(size/size0))
115 if re.search(
r"Matrix", str(var.type)):
117 return var[
"operator()(int, int)"](x, y)
121 if x < 0
or x >= NX
or y < 0
or y >= NY:
122 raise gdb.GdbError(
"Element (%d, %d) is out of range 0:%d, 0:%d" %
123 (x, y, NX - 1, NY - 1))
125 m_data = var[
"m_storage"][
"m_data"]
130 m_data = m_data.address.cast(m_data.type)
133 val = m_data[x + y*NX]
135 val = m_data[
"array"][x + y*NX]
138 return var[
"operator()(int)"](x)
144 "Element (%d) is out of range 0:%d" % (x, NX - 1))
146 m_data = var[
"m_storage"][
"m_data"]
150 m_data = m_data.address.cast(m_data.type)
155 val = m_data[
"array"][x]
157 if val.type.code == gdb.TYPE_CODE_INT:
159 elif val.type.code == gdb.TYPE_CODE_FLT:
165 "Print an Eigen Matrix" 173 return "%s{%dx%d}" % (self.
val.type, nx, ny)
176 "Print an Eigen Vector" 182 m_storage = self.
val[
"m_storage"]
188 n = m_storage.type.template_argument(1)
192 size = m_storage[
"m_data"][
"array"].type.sizeof
193 size0 = m_storage[
"m_data"][
"array"].dereference(
195 n = math.sqrt(size/size0)
200 """Print an eigen Matrix or Vector 201 Usage: show eigen <matrix> [x0 y0 [nx ny]] 202 show eigen <vector> [x0 [nx]] 206 super(PrintEigenCommand, self).
__init__(
"show eigen",
210 def _mget(self, var, x, y=0):
213 def _vget(self, var, x):
220 parser.add_option(
"-d",
"--dataFmt", default=
"%.2f",
221 help=
"Format for values")
222 parser.add_option(
"-f",
"--formatWidth", type=
"int",
223 default=8, help=
"Field width for values")
224 parser.add_option(
"-o",
"--origin", type=
"str", nargs=
"+",
225 help=
"Origin of the part of the object to print")
228 "eigenObject", help=
"Expression giving Eigen::Matrix/Vector to show")
230 "nx", help=
"Width of patch to print", type=
"int", default=0, nargs=
"?")
232 "ny", help=
"Height of patch to print", type=
"int", default=0, nargs=
"?")
234 opts = parser.parse_args(args)
238 (opts, args) = parser.parse_args(args)
243 raise gdb.GdbError(
"Please specify an object")
244 opts.eigenObject = args.pop(0)
246 opts.nx, opts.ny = 0, 0
248 opts.nx = int(args.pop(0))
250 opts.ny = int(args.pop(0))
254 "Unrecognised trailing arguments: %s" %
" ".join(args))
256 var = gdb.parse_and_eval(opts.eigenObject)
258 if not re.search(
r"(Eigen|LinearTransform)::(Matrix|Vector)", str(var.type)):
260 "Please specify an eigen matrix or vector, not %s" % var.type)
262 if re.search(
r"shared_ptr<", str(var.type)):
263 var = var[
"px"].dereference()
265 if var.type.code == gdb.TYPE_CODE_PTR:
266 var = var.dereference()
268 isMatrix = re.search(
r"Matrix", str(var.type))
273 if len(opts.origin) != 2:
274 raise gdb.GdbError(
"Please specify both x0 and y0")
276 x0 = gdb.parse_and_eval(opts.origin[0])
277 y0 = gdb.parse_and_eval(opts.origin[1])
288 if nx == 1
and ny == 1:
289 print(
"%g" % self.
_vget(var, x0))
292 NX = 0, var[
"m_storage"][
"n"]
295 if len(opts.origin) != 1:
296 raise gdb.GdbError(
"Please only specify x0")
298 x0 = gdb.parse_and_eval(opts.origin[0])
307 print(
"%g" % self.
_vget(var, x0))
313 print(
"%-4s" %
"", end=
' ')
314 for x
in range(x0,
min(NX, x0 + nx)):
315 print(
"%*d" % (opts.formatWidth, x), end=
' ')
318 for y
in range(y0,
min(NY, y0 + ny)):
319 print(
"%-4d" % y, end=
' ')
320 for x
in range(x0,
min(NX, x0 + nx)):
321 print(
"%*s" % (opts.formatWidth, (opts.dataFmt %
322 self.
_mget(var, x, y))), end=
' ')
325 for x
in range(x0,
min(NX, x0 + nx)):
326 print(
"%*s" % (opts.formatWidth, (opts.dataFmt %
327 self.
_vget(var, x))), end=
' ')
339 sentinel = long(self.
val[
"_sentinel"].cast(
340 gdb.lookup_type(
"unsigned int")))
341 return "{%s %d 0x%x}" % (self.
val.address, self.
val[
"_CitizenId"], sentinel)
345 Usage: show citizen <obj> 349 super(PrintCitizenCommand, self).
__init__(
"show citizen",
358 parser.add_option(
"object", help=
"The object in question")
360 opts = parser.parse_args(args)
364 opts, args = parser.parse_args(args)
369 raise gdb.GdbError(
"Please specify an object")
370 opts.object = args.pop(0)
374 "Unrecognised trailing arguments: %s" %
" ".join(args))
376 var = gdb.parse_and_eval(opts.object)
377 if re.search(
r"shared_ptr<", str(var.type)):
380 if var.type.code != gdb.TYPE_CODE_PTR:
383 citizen = var.dynamic_cast(gdb.lookup_type(
384 "lsst::daf::base::Citizen").pointer())
388 "Failed to cast %s to Citizen -- is it a subclass?" % opts.object)
390 citizen = citizen.dereference()
399 "Print a BaseSourceAttributes" 405 return "Base: {id=%d astrom=(%.3f, %.3f)}" % (self.
val[
"_id"],
406 self.
val[
"_xAstrom"],
407 self.
val[
"_yAstrom"])
416 return "Source{id=%d astrom=(%.3f, %.3f)}" % (self.
val[
"_id"],
417 self.
val[
"_xAstrom"],
418 self.
val[
"_yAstrom"])
421 "Print a cameraGeom::Detector" 427 return "Detector{name: %s id: %s type: %s bbox: %s}" % (self.
val[
"_name"], self.
val[
"_id"],
428 self.
val[
"_type"], self.
val[
"_bbox"])
439 nspan = self.
val[
"_spans"][
"size"]()
441 vec_impl = self.
val[
"_spans"][
"_M_impl"]
442 nspan = vec_impl[
"_M_finish"] - vec_impl[
"_M_start"]
444 return "Footprint{id=%d, nspan=%d, area=%d; BBox %s}" % (self.
val[
"_fid"], nspan,
445 self.
val[
"_area"], self.
val[
"_bbox"])
448 "Print a FootprintSet" 454 return "FootprintSet{%s; %s}" % (self.
val[
"_region"], self.
val[
"_footprints"])
463 return "Peak{%d, (%.2f, %.2f)}" % (self.
val[
"_id"], self.
val[
"_fx"], self.
val[
"_fy"])
469 return "%s" % (self.typeName())
480 if type.code == gdb.TYPE_CODE_REF:
488 return "Box2{(%s,%s)--(%s,%s)}" % (llc[0], llc[1],
489 llc[0] + dims[0] - 1, llc[1] + dims[1] - 1)
495 "Print a CoordinateBase" 501 return self.
val[
"_vector"][
"m_storage"][
"m_data"][
"array"]
507 "Print an ellipse::Axes" 513 vec = self.
val[
"_vector"]
517 "Print an ellipse::Quadrupole" 523 mat = self.
val[
"_matrix"]
532 "Print an ImageBase or derived class" 540 if type.code == gdb.TYPE_CODE_REF:
543 gilView = val[
"_gilView"]
544 arr = val[
"_origin"][
"_vector"][
"m_storage"][
"m_data"][
"array"]
546 x0, y0 = arr[0], arr[1]
547 return "%dx%d%s%d%s%d" % (
549 gilView[
"_dimensions"][
"x"], gilView[
"_dimensions"][
"y"],
551 [
"",
"+"][x0 >= 0], x0,
552 [
"",
"+"][y0 >= 0], y0)
565 "Print a MaskedImage" 575 self.
dimenStr(self.
val[
"_maskedImage"][
"_image"][
"px"].dereference()))
581 super(PrintImageCommand, self).
__init__(
"show image",
587 return var[
"operator()(int, int, bool)"](x, y,
True)
589 dimensions = var[
"_gilView"][
"_dimensions"]
590 if x < 0
or x >= dimensions[
"x"]
or y < 0
or y >= dimensions[
"y"]:
591 raise gdb.GdbError(
"Pixel (%d, %d) is out of range 0:%d, 0:%d" %
592 (x, y, dimensions[
"x"] - 1, dimensions[
"y"] - 1))
594 pixels = var[
"_gilView"][
"_pixels"][
"_p"]
595 step = pixels[
"_step_fn"][
"_step"] / \
596 var.type.template_argument(0).sizeof
598 return pixels[
"m_iterator"][x + y*step][
"_v0"]
604 "show image" + (
"" if argparse
else " <image> [<nx> [<ny>]]"))
605 parser.add_option(
"-a",
"--all", action=
"store_true",
606 help=
"Display the whole image/mask")
607 parser.add_option(
"-c",
"--center", type=
"str", nargs=2, default=(
None,
None,),
608 help=
"Center the output at (x, y)")
609 parser.add_option(
"-o",
"--origin", type=
"str", nargs=2, default=(
None,
None,),
610 help=
"Print the region starting at (x, y)")
611 parser.add_option(
"-x",
"--xy0", action=
"store_true",
612 help=
"Obey the image's (x0, y0)")
613 parser.add_option(
"-f",
"--formatWidth", type=
"int",
614 default=8, help=
"Field width for values")
615 parser.add_option(
"-d",
"--dataFmt", default=
"%.2f",
616 help=
"Format for values")
620 "image", help=
"Expression giving image to show")
622 "width", help=
"Width of patch to print", default=1, nargs=
"?")
624 "height", help=
"Height of patch to print", default=1, nargs=
"?")
626 opts = parser.parse_args(args)
630 opts, args = parser.parse_args(args)
635 raise gdb.GdbError(
"Please specify an image")
637 opts.image = args.pop(0)
639 opts.width, opts.height = 1, 1
641 opts.width = int(args.pop(0))
643 opts.height = int(args.pop(0))
647 "Unrecognised trailing arguments: %s" %
" ".join(args))
651 if opts.origin[i]
is None:
652 if opts.center[i]
is not None:
656 if opts.center[i]
is not None:
658 "You may not specify both --center and --origin")
660 val = gdb.parse_and_eval(val)
669 nx, ny = opts.width, opts.height
671 var = gdb.parse_and_eval(opts.image)
673 if re.search(
r"shared_ptr<", str(var.type)):
674 var = var[
"px"].dereference()
676 if not re.search(
r"(lsst::afw::image::)?(Image|Mask|MaskedImage)", str(var.type.unqualified())):
678 "Please specify an image, not %s" % var.type)
680 if re.search(
r"MaskedImage", str(var.type))
and \
681 not re.search(
r"::Image(\s*&)?$", str(var.type)):
682 print(
"N.b. %s is a MaskedImage; showing image" % (opts.image))
685 if re.search(
r"shared_ptr<", str(var.type)):
686 var = var[
"px"].dereference()
688 if var.type.code == gdb.TYPE_CODE_PTR:
689 var = var.dereference()
691 pixelTypeName = str(var.type.template_argument(0))
693 dataFmt = opts.dataFmt
694 elif pixelTypeName
in [
"short",
"unsigned short"]:
696 elif pixelTypeName
in [
"int",
"unsigned int"]:
702 nx = var[
"_gilView"][
"_dimensions"][
"x"]
704 ny = var[
"_gilView"][
"_dimensions"][
"y"]
710 if opts.xy0
and not opts.all:
711 arr = var[
"_origin"][
"_vector"][
"m_storage"][
"m_data"][
"array"]
718 print(
"%-4s" %
"", end=
' ')
719 for x
in range(x0, x0 + nx):
720 print(
"%*d" % (opts.formatWidth, x), end=
' ')
723 for y
in reversed(
list(range(y0, y0 + ny))):
724 print(
"%-4d" % y, end=
' ')
725 for x
in range(x0, x0 + nx):
726 print(
"%*s" % (opts.formatWidth, dataFmt %
727 self.
get(var, x, y)), end=
' ')
740 return "Background(%dx%d) %s %s" % (
741 self.
val[
"_imgWidth"], self.
val[
"_imgHeight"],
745 "Print a BackgroundControl" 752 return "{%s %s %s %s}" % (re.sub(
r"lsst::afw::math::Interpolate::",
"", str(self.
val[
"_style"])),
753 re.sub(
r"lsst::afw::math::",
"",
754 str(self.
val[
"_prop"])),
755 re.sub(
r"lsst::afw::math::",
"", str(
756 self.
val[
"_undersampleStyle"])),
757 self.
val[
"_sctrl"][
"px"].dereference())
767 return "%s(%dx%d)" % (self.
typename,
768 self.
val[
"_width"], self.
val[
"_height"])
771 "Print a StatisticsControl" 778 return "{nSigma=%g nIter=%d ignore=0x%x}" % (self.
val[
"_numSigmaClip"],
779 self.
val[
"_numIter"],
780 self.
val[
"_andMask"])
783 "Print a table::Table" 790 return "{schema = %s, md=%s}" % (self.
val[
"_schema"], self.
val[
"_metadata"])
793 "Print a table::Schema" 800 names = str(self.
val[
"_impl"][
"px"][
"_names"])
801 names = re.sub(
r"^[^{]*{|}|[\[\]\"\"]|\s*=\s*[^,]*",
"", names)
803 return "%s" % (names)
808 "Register my pretty-printers with objfile Obj." 814 gdb.printing.register_pretty_printer(obj, p, replace=
True)
817 """Surely this must be somewhere standard?""" 819 printer = gdb.printing.RegexpCollectionPrettyPrinter(
"rhl-boost")
821 printer.add_printer(
'boost::shared_ptr',
822 '^(boost|tr1|std)::shared_ptr', SharedPtrPrinter)
823 printer.add_printer(
'boost::gil::pixel',
824 'boost::gil::.*pixel_t', GilPixelPrinter)
831 """Surely this must be somewhere standard?""" 833 printer = gdb.printing.RegexpCollectionPrettyPrinter(
"rhl-eigen")
835 printer.add_printer(
'eigen::Matrix',
836 '^Eigen::Matrix', EigenMatrixPrinter)
837 printer.add_printer(
'eigen::Vector',
838 '^Eigen::Vector', EigenVectorPrinter)
845 printer = gdb.printing.RegexpCollectionPrettyPrinter(
"afw")
847 printer.add_printer(
'lsst::afw::cameraGeom::Detector',
848 '^lsst::afw::cameraGeom::(Amp|Ccd|Detector|DetectorMosaic)$', DetectorPrinter)
850 printer.add_printer(
'lsst::afw::detection::Footprint',
851 '^lsst::afw::detection::Footprint$', FootprintPrinter)
852 printer.add_printer(
'lsst::afw::detection::FootprintSet',
853 '^lsst::afw::detection::FootprintSet', FootprintSetPrinter)
854 printer.add_printer(
'lsst::afw::detection::Peak',
855 '^lsst::afw::detection::Peak$', PeakPrinter)
856 printer.add_printer(
'lsst::afw::detection::Psf',
857 '^lsst::afw::detection::Psf$', PsfPrinter)
858 printer.add_printer(
'lsst::afw::detection::Source',
859 '^lsst::afw::detection::Source$', SourcePrinter)
860 printer.add_printer(
'lsst::afw::detection::BaseSourceAttributes',
861 '^lsst::afw::detection::BaseSourceAttributes$', BaseSourceAttributesPrinter)
863 printer.add_printer(
'lsst::afw::geom::Box',
864 '^lsst::afw::geom::Box', Box2Printer)
865 printer.add_printer(
'lsst::afw::geom::Extent',
866 '^lsst::afw::geom::Extent', CoordinateBasePrinter)
867 printer.add_printer(
'lsst::afw::geom::Point',
868 '^lsst::afw::geom::Point', CoordinateBasePrinter)
870 printer.add_printer(
'lsst::afw::geom::ellipses::Axes',
871 '^lsst::afw::geom::ellipses::Axes', AxesPrinter)
872 printer.add_printer(
'lsst::afw::geom::ellipses::Quadrupole',
873 '^lsst::afw::geom::ellipses::Quadrupole', QuadrupolePrinter)
875 printer.add_printer(
'lsst::afw::image::ImageBase',
876 'lsst::afw::image::ImageBase<[^>]+>$', ImagePrinter)
877 printer.add_printer(
'lsst::afw::image::Image',
878 'lsst::afw::image::Image<[^>]+>$', ImagePrinter)
879 printer.add_printer(
'lsst::afw::image::Mask',
880 '^lsst::afw::image::Mask<[^>]+>$', ImagePrinter)
881 printer.add_printer(
'lsst::afw::image::MaskedImage',
882 '^lsst::afw::image::MaskedImage<[^>]+>$', MaskedImagePrinter)
883 printer.add_printer(
'lsst::afw::image::Exposure',
884 '^lsst::afw::image::Exposure', ExposurePrinter)
886 printer.add_printer(
'lsst::afw::math::Background',
887 '^lsst::afw::math::Background$', BackgroundPrinter)
888 printer.add_printer(
'lsst::afw::math::BackgroundControl',
889 '^lsst::afw::math::BackgroundControl$', BackgroundControlPrinter)
890 printer.add_printer(
'lsst::afw::math::Kernel',
891 '^lsst::afw::math::.*Kernel', KernelPrinter)
892 printer.add_printer(
'lsst::afw::math::StatisticsControl',
893 '^lsst::afw::math::StatisticsControl', StatisticsControlPrinter)
895 printer.add_printer(
'lsst::afw::table::Table',
896 '^lsst::afw::table::.*Table$', TablePrinter)
897 printer.add_printer(
'lsst::afw::table::Schema',
898 '^lsst::afw::table::Schema$', TableSchemaPrinter)
905 printer = gdb.printing.RegexpCollectionPrettyPrinter(
"daf::base")
907 printer.add_printer(
'lsst::daf::base::Citizen',
908 'lsst::daf::base::Citizen', CitizenPrinter)
913 except ImportError
as e:
915 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)