I tried finding an answer on google about how to do a strcmp operation in a core dump and could not find any solution. The answers available focused on using “call” to call the libc strcmp() function on a live process. Most interesting debugging for me happens on core dumps, so I decided to write my own gdb user defined command (sort of like a macro):
define gdb_strcmp dont-repeat set $result = 1 set $_i = 0 if ($arg0 == 0x0 && $arg1 != 0x0) set $result = 0 end if ($arg0 != 0x0 && $arg1 == 0x0) set $result = 0 end while ($result == 1 && $arg0[$_i] != 0x0 && $arg1[$_i] != 0x0) if ($arg0[$_i] != $arg1[$_i]) set $result = 0 end set $_i = $_i + 1 end end document gdb_strcmp Determines if two C strings match end
Note that gdb user commands are annoying because you don’t really have return values (they are not really functions/macros), so you have to set a global variable (yuck!) to hold the result. This macro sets $result to 0 if the strings are not equal and $1 if they are. I contemplated using the same return value than the C counterpart, but since I was interested in just a ‘yes or no’ answer I sticked to use 1 for equal and 0 for non equal.
You can then go ahead and use this macro in other macros to do useful things, such as scan a linked list and verify if a given member has certain string value.
PD. I know it’d be cleaner to start using Python for these things but I have not really looked yet into that