简体   繁体   中英

Print bounds of function using GDB

How can I get the bounds of a function using GDB? I would like to know the starting and ending addresses so that I can dump/restore the machine code.

Without Python:

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

(assuming ret only takes up one byte)

With Python:

Create 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.')
    if not funcsym.is_function:
      raise gdb.GdbError(f'{argstr} is not a function.')
    funcaddr = int(funcsym.value().address)
    block = gdb.block_for_pc(funcaddr)
    print(hex(block.start), hex(block.end-1))

Bounds()

Then use it in GDB like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM