有人拥有Linux上fd_set的gdb美化打印机代码吗?

7
现代版的gdb允许将python代码整合到其中,以便“漂亮打印”复杂数据结构。C++的STL类以及一些常见的boost.org类型都有一些很好的漂亮打印机实现。
在网络编程中,人们经常遇到select/poll调用。虽然poll()使用数据结构数组,但select()使用fd_set。
有没有人遇到过fd_set的漂亮打印机实现,最好是可移植的,但是即使特定于平台也可以。理想情况下,它应该是linux/x86,但我会接受任何东西,并希望能够适应。

不幸的是,fd_set 的内容取决于实现,因此一个可移植的解决方案需要访问 C 标头文件,这使得一个可移植的、纯 Python 的解决方案变得不太可能。 - Conspicuous Compiler
我知道fd_sets可能是依赖于实现的。据我所知,有一些Python工具可以让Python代码与C数据结构进行交互(类似于Perl中执行相同操作的工具),但我还没有尝试过这种方法。正如我所指出的,即使现有的非可移植解决方案不适用于我的目标平台,我也很满意,因为它至少是一个起点。 - Chris Cleeland
我正在尝试解决这个问题。问题不在于与数据结构的交互,而在于字节顺序和结构元素的名称未确定。事实上,在大多数Linux系统中,fd_set是匿名结构体的typedef,因此即使确定给定的结构体是fd_set也是启发式的。一旦我有一个可用的解决方案,我会添加答案。 - Conspicuous Compiler
太好了!虽然结构本身不可移植,但类型和相关宏非常一致。也许我应该考虑编写C代码,将其编译为带有Python前端的.so文件...以便可移植地解码fd_set。关于能够确定某个东西是fd_set,对我来说需要进行强制转换是合理的。 - Chris Cleeland
1个回答

4

好的,这里是我写的一些在Linux下似乎对我有用的东西。如果它对你有用,请告诉我:

anonprint.py

import gdb

class fd_set_printer:
  """
  Prints an fd_set, which is normally an opaque
  array of ints, each bit representing one file descriptor
  """

  def __init__(self, val, val_array):
    self.val = val
    self.val_array = val_array

  @staticmethod
  def find_set_bits(bit_array):
    """
    Finds set bits in a long bit list.
    Expects a gdb.Value which contains a C array,
      such as int[10], and treats it as a bitlist
      of int_size * 10 bits long.  Returns an array of
      bit positions, starting with 0, for which the bits
      are on.
    e.g. for int foo[] = [1, 6], it will return [ 0, 33, 34 ]
    The array should be given as a gdb.Value
    """
    set_bits = []
    bits_length = bit_array[0].type.sizeof * 8
    current_bit = 0

    # Can not use 'for current_byte in gdb.Value:' even if
    #  gdb.Value.type.code == gdb.TYPE_CODE_ARRAY
    # So iteration happens this ugly C-style way
    for current_byte_pos in xrange(*bit_array.type.range()):
      current_byte = bit_array[current_byte_pos]
      for bit in xrange(0, bits_length):
        bit_mask = 1 << bit
        if bit_mask & current_byte == bit_mask:
          set_bits.append(current_bit)
        current_bit += 1

    return set_bits

  def to_string(self):
    fd_list = self.find_set_bits(self.val_array)
    if len(fd_list) == 0:
      output = "Empty file descriptor set."
    else:
      output = "File descriptor set: "
      output += ', '.join(map(str, fd_list))
    return output


def anon_struct_lookup_function(val):
  """
  Checks if the given value looks like an fd_set.
  If it does, delegates printing to the printer
  """
  lookup_tag = val.type.tag
  if lookup_tag == None:
    return None
  if lookup_tag != "<anonymous struct>":
    return None

  fields = val.type.fields()
  val_array = None

  if len(fields) == 1 and fields[0].name == 'fds_bits':
    val_array = val['fds_bits']
  elif len(fields) == 1 and fields[0].name == '__fds_bits':
    val_array = val['__fds_bits']

  if not val_array is None:
    return fd_set_printer(val, val_array)

  return None


def add_fd_set_printer(obj = gdb):
  "Adds the fd_set pretty printer to the given object"
  obj.pretty_printers.append(anon_struct_lookup_function)

然后创建你的~/.gdbinit文件:

python
import sys
sys.path.insert(0, '/home/user/anonprint_py_directory_here')
from anonprint import add_fd_set_printer
add_fd_set_printer()
end

这是我第一次尝试通过Python与gdb内部进行交互,因此欢迎评论和建议。


我希望在接下来的几天里尝试一下。谢谢! - Chris Cleeland
没有忘记...只是被其他问题分散了注意力。但它并没有从视线中消失。 - Chris Cleeland

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接