debugger gdb evaluate expression

2019-09-03 16:28发布

问题:

is there eval function? I've read "help" and I didnt find

I want to make eval("gdb command")

because I want to create my own function for grepping using this method

How to grep on gdb print.

I want to make eval($arg1)

回答1:

There is an eval command, but it doesn't really do what you want. It provides a limited form of substitution of values into commands.

For a command along the lines of grep, I would suggest writing it in Python. This would be relatively easy to do. The idea would be to use gdb.execute to capture the output of a command into a string, and then use Python to search the string however you like. If done from Python you have complete control of how to parse the command-line, something that's not true if you use the gdb define command.



回答2:

Oddly enough, I wrote a grep python gdb function earlier today for another question. These couple of files make a new command that checks if the call stack contains _malloc. This should be a pretty good start for other string searching and evaluation functions.

Here is a script for gdb

# gdb script: pygdb-logg.gdb
# easier interface for pygdb-logg.py stuff
# from within gdb: (gdb) source -v pygdb-logg.gdb
# from cdmline: gdb -x pygdb-logg.gdb -se test.exe

# first, "include" the python file:
source -v pygdb-logg.py

# define shorthand for inMalloc():
define inMalloc
  python inMalloc()
end

Here is the python file:

#!/usr/bin/python
# gdb will 'recognize' this as python
#  upon 'source pygdb-logg.py'
# however, from gdb functions still have
#  to be called like:
#  (gdb) python print logExecCapture("bt")

import sys
import gdb
import os

def logExecCapture(instr):
  # /dev/shm - save file in RAM
  ltxname="/dev/shm/c.log"

  gdb.execute("set logging file "+ltxname) # lpfname
  gdb.execute("set logging redirect on")
  gdb.execute("set logging overwrite on")
  gdb.execute("set logging on")
  gdb.execute("bt")
  gdb.execute("set logging off")

  replyContents = open(ltxname, 'r').read() # read entire file
  return replyContents

# in malloc?
def inMalloc():
  isInMalloc = -1;
  # as long as we don't find "Breakpoint" in report:
  while isInMalloc == -1:
    REP=logExecCapture("n")
#Look for calls that have '_malloc' in them 
    isInMalloc = REP.find("_malloc")
    if(isInMalloc != -1):
#       print ("Malloc:: ", isInMalloc, "\n", REP)
       gdb.execute("set $inMalloc=1")
       return True
    else:
#       print ("No Malloc:: ", isInMalloc, "\n", REP)
       gdb.execute("set $inMalloc=0")
       return False


标签: gdb