You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为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

  1. to_string returns a GDB Value instead of a string: GDB's pretty printer expects this method to return a human-readable string, not a raw gdb.Value instance.
  2. Regex matching is too strict: Your regex only matches the exact type name, but GDB might present the type with modifiers like const in some cases.
  3. 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_string method now converts the resulting gdb.Value to a string with str(double_value), which GDB can display directly.
  • Robust regex matching: The updated regex matches both the base type and const boost::multiprecision::mpfr_float_50 to cover more use cases.
  • Override default printers: Adding replace=True to register_pretty_printer ensures 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

  1. Double-check that the path in your .gdbinit points 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
    
  2. Start GDB, load your binary, and set a breakpoint where you have an mpfr_float_50 variable. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 21:02:49