Subject: Gotta Looove gdb

From: Shlomi Fish <[email-masked]>
Date: Sat, 14 Mar 2009 21:37:17 +0200

Today I was debugging a development branch of Freecell Solver (I see Muli is 
preparing the graggers) and had to deal with a problem that the scanning dump 
deviated from that in the stable version. Comparing the two dumps yielded that 
some derived states were not followed. But I wanted to know what generates 
them?

I decided to use gdb and its scripting facilities to automate the lookup. The 
end result was this script:

{{{{{{{{{{{{{{{
set args -l gi -s -i -p -t 1941.board
# Breakpoint 1
b main
r
# Breakpoint 2
b scans.c:1127 if instance->num_times == 3150
b scans.c:1139 if instance->num_times == 3150
c
# Now we are before the check.
SET $VERDICT = 0
while ($VERDICT == 0)
    set $START = derived.num_states
    c
    set $END = derived.num_states
    set $I = $START
    while (($VERDICT == 0) && ($I < $END))
        call fc_solve_state_as_string(derived.states[$I], 4, 8, 1, 1, 0, 1)
        SET $STATE_STR = $
        call strstr($STATE_STR , ": 9D 6S 4D AH\n")
        SET $IS_SUBSTRING1 = $
        call strstr($STATE_STR, "Freecells:  4S  AC  KH")
        SET $IS_SUBSTRING2 = $
        if ( $IS_SUBSTRING1 && $IS_SUBSTRING2 )
            SET $VERDICT = 1
        else
            set $I = $I + 1
        end
    end
    if ($VERDICT == 0)
        c
    end
end
}}}}}}}}}}}}}}}

(it operates against revision 1223 of:
http://svn.berlios.de/svnroot/repos/fc-solve/trunk/fc-solve/source/
)

This is the first time I learned about gdb's $NAME variables and assignment to 
them using "set". Ideas for improvements in the next time I'm writing such 
scripts would be welcome.

The end result? This patchlet:

{{{{{{{{{{{{{
Index: scans.c
===================================================================
--- scans.c	(revision 1222)
+++ scans.c	(revision 1223)
@@ -1250,8 +1250,8 @@
                 
                 pack = (fcs_standalone_state_ptrs_t *)malloc(sizeof(*pack));
 
-                pack->key = ptr_state_with_locations_key;
-                pack->val = ptr_state_with_locations_val;
+                pack->key = derived.states[derived_index].key;
+                pack->val = derived.states[derived_index].val;
 
                 fc_solve_PQueuePush(
                     a_star_pqueue,
}}}}}}}}}}}}}

A small copy-and-paste error that was the difference between failure and 
success.

According to http://tromey.com/blog/?p=412 , there is an upcoming Python 
support in gdb, which should allow even more awesome debugger scripting. We're 
so lucky to have gdb which is command-line and scriptable, because many 
debuggers are neither.

Now if gdb switched away from CVS so I can contribute to it with ease. (That 
or git-cvsimport won't get stuck trying to import its CVS repository.).

Regards,

	Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Why I Love Perl - http://xrl.us/bjn88

God gave us two eyes and ten fingers so we will type five times as much as we
read.