Like grep on gdb print

Is there any grep way to print command output in gdb? In my case, I am debugging a kernel dump using gdb, and the object that I am debugging contains many elements. It’s hard for me to find the corresponding ie attribute:

(gdb) print *this | grep <attribute> 

Thanks.

+8
source share
2 answers

(gdb) print * this | grep

The "standard" way to achieve this is to use Meta-X gdb in emacs .

Alternative:

 (gdb) set logging on (gdb) print *this (gdb) set logging off (gdb) shell grep attribute gdb.txt 

The patch mentioned by cnicutar looks attractive compared to the above. I guess why it (or its equivalent) was never introduced, because most GDB-enabled ones use emacs and therefore do not have this problem in the first place.

+6
source

The easiest way is to use Python GDB. One liner:

 gdb Ξ» py ["attribute" in line and print(line) for line in gdb.execute("p *this", to_string=True).splitlines()] 

Assuming you have included command history, you can enter it only once, and then press Ctrl + R b.exec to pull it out of history. Then just change the attribute and *this suit your requirements.


You can also do it this simple:

 gdb Ξ» grep_cmd "p *this" attribute 

To do this, simply add the following to your .gdbinit file:

 py class GrepCmd (gdb.Command): """Execute command, but only show lines matching the pattern Usage: grep_cmd <cmd> <pattern> """ def __init__ (_): super ().__init__ ("grep_cmd", gdb.COMMAND_STATUS) def invoke (_, args_raw, __): args = gdb.string_to_argv(args_raw) if len(args) != 2: print("Wrong parameters number. Usage: grep_cmd <cmd> <pattern>") else: for line in gdb.execute(args[0], to_string=True).splitlines(): if args[1] in line: print(line) GrepCmd() # required to get it registered end 
0
source

All Articles