debugging 使用GDB打印函数的边界

gwbalxhn  于 2022-11-30  发布在  其他
关注(0)|答案(1)|浏览(152)

我如何使用GDB得到函数的边界?我想知道起始和结束地址,以便我可以转储/恢复机器码。

4xy9mtcn

4xy9mtcn1#

不使用Python:

(gdb) pipe disas main | sed -n '2p;x;$p'
0x0000555555555155 <+0>:     push   %rbp
0x00005555555551b4 <+95>:    ret

(假设ret只占用一个字节)
使用Python:
创建bounds.py

class Bounds(gdb.Command):
  """print lower and upper pc values for function"""

  def __init__(self):
    super(Bounds, self).__init__ ('info bounds', gdb.COMMAND_USER, gdb.COMPLETE_SYMBOL)

  def invoke(self, argstr, from_tty):
    try:
      (funcsym, _) = gdb.lookup_symbol(argstr)
    except gdb.error as gdberr:
      raise gdb.GdbError(f'Got exception "{gdberr}". Start the program and try again.')
    if funcsym == None:
      raise gdb.GdbError(f'{argstr} not found.')
    funcaddr = int(funcsym.value().address)
    block = gdb.block_for_pc(funcaddr)
    print(hex(block.start), hex(block.end-1))

Bounds()

然后在GDB中这样使用它:

(gdb) source bounds.py
(gdb) info bounds main
0x555555555155 0x5555555551b4

相关问题