如何为boost::multiprecision::mpfr_float_50创建可正常输出double值的GDB美化打印机?
Fixing GDB Pretty Printer for boost::multiprecision::mpfr_float_50
It looks like your pretty printer isn't being applied correctly, and there are a couple of small issues in your code preventing it from outputting the double representation you want. Let's walk through the fixes step by step.
Key Issues in Your Original Code
to_stringreturns a GDB Value instead of a string: GDB's pretty printer expects this method to return a human-readable string, not a rawgdb.Valueinstance.- Regex matching is too strict: Your regex only matches the exact type name, but GDB might present the type with modifiers like
constin some cases. - Printer registration doesn't override defaults: Without explicitly enabling replacement, your printer might not take precedence over GDB's default struct printer.
Corrected Pretty Printer Code
Here's the revised version of your gdb_pretty_printers.py script:
import gdb import gdb.printing class BoostMultiprecisionMPFRPrinter(object): "Pretty Printer for boost::multiprecision::mpfr_float_50" def __init__(self, val): self.val = val self.double_type = gdb.lookup_type('double') def to_string(self): # Directly call the convert_to<double>() method on the mpfr_float_50 object # This is safer and cleaner than string-based evaluation double_value = self.val.convert_to(self.double_type) # Convert the GDB Value to a string for proper display return str(double_value) def display_hint(self): # Tell GDB to treat this as a floating-point number for formatting return 'float' def build_pretty_printer(): pp = gdb.printing.RegexpCollectionPrettyPrinter("boost-multiprecision") # Adjust regex to match const-qualified versions of the type pp.add_printer( 'mpfr_float_50', r'^boost::multiprecision::mpfr_float_50(\s+const)?$', BoostMultiprecisionMPFRPrinter ) return pp def register_printers(): # Use replace=True to ensure our printer overrides default struct printing gdb.printing.register_pretty_printer( gdb.current_objfile(), build_pretty_printer(), replace=True )
Key Fixes Explained
- String return type: The
to_stringmethod now converts the resultinggdb.Valueto a string withstr(double_value), which GDB can display directly. - Robust regex matching: The updated regex matches both the base type and
const boost::multiprecision::mpfr_float_50to cover more use cases. - Override default printers: Adding
replace=Truetoregister_pretty_printerensures your custom printer takes priority over GDB's default handling of the struct. - Cleaner method invocation: Using
self.val.convert_to(self.double_type)avoids messy string-based evaluation, making the script more reliable and easier to maintain.
Verify the Setup
- Double-check that the path in your
.gdbinitpoints correctly to your script directory:python import sys sys.path.insert(1, '/full/path/to/your/script/directory') import gdb_pretty_printers gdb_pretty_printers.register_printers() end - Start GDB, load your binary, and set a breakpoint where you have an
mpfr_float_50variable. When you print the variable (e.g.,print my_mpfr_var), you should see the double representation instead of the internal struct details.
内容的提问来源于stack exchange,提问作者Steve Lorimer
相关产品推荐
相关产品推荐

