GDB strcmp in a core dump

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[0] == 0x0 && $arg1[0] != 0x0)
                set $result = 0 
        end 
        if ($arg0[0] != 0x0 && $arg1[0] == 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

This entry was posted in C/C++. Bookmark the permalink.

3 Responses to GDB strcmp in a core dump

  1. Sandeep says:

    Thanks!
    We need this code after while loop to rule out the comparison failure for strings like “abcd” and “abcdef”

    if ($arg0[$_i] != 0x0 || $arg1[$_i] != 0x0)
    set $strcmp_res = 0
    end

  2. moy says:

    Sorry Subramanya, I have not.

  3. Subramanya says:

    Hi have you written any macro which does the work of strlcpy.
    If there let me know

    Thanks in advance

Leave a Reply

Your email address will not be published. Required fields are marked *

*