As mentioned in
SERVER-28437, when GDB invokes a function, the threads can execute. Perhaps using the command set scheduler-locking on will keep them from executing and modifying the state of the locks. Note, according to the documentation, "On some OSes, you can modify GDB’s default behavior by locking the OS scheduler to allow only a single thread to run. ", so this should be tested thoroughly on all platforms.