# This GDB script file is used when running gdbcustom.sh/gdbgame.sh etc,
# or 'Test Game under GDB' in Custom.

set lang c++

define dumpstate
	echo Dumping state. FB error string:\n
	p ((char**)&__fb_ctx)[5]
	echo GFXBACKENDINFO:
	p GFXBACKENDINFO
	echo MUSICBACKENDINFO:
	p MUSICBACKENDINFO
	echo Threads and backtrace:\n
	info threads
	bt
end

echo (Note: please ignore any 'Function "XYZ" not defined' messages due to Unix/Windows differences)\n

# Only happens when running under Test Game
break HOOK_AFTER_ATTACH_TO_CUSTOM
break HOOK_AFTER_ATTACH_TO_CUSTOM@4

# Break when exiting with __fb_ctx.errmsg set (only when compiled with -exx)
# (This is equal to __fb_errmsg on an error, and NULL otherwise)
# (On Windows this is called fb_End@4. Exactly 1 of the following 'break's
# should succeed, so that breakpoint numbers stay in-sync.)
break fb_End if ((char**)&__fb_ctx)[5] != 0
break fb_End@4 if ((char**)&__fb_ctx)[5] != 0
# For some reason, a conditional break on exit gives a 'not defined' error with old gdb (on OSX)
#break exit if ((char**)&__fb_ctx)[5] != 0
# Not sure why can't hook abort
#break abort
commands 2
#commands 2 3
	dumpstate
end

# Exit gdb when the program exits
define bye
	# Need to use a define block to continue and quit, because
	# a 'commands' block is cancelled when the program exits
	cont
	quit
end
break exit
commands 3
	bye
end
# Hook engine shutting itself down abnormally
break FATAL_ERROR_SHUTDOWN
break FATAL_ERROR_SHUTDOWN@0

handle SIGPIPE nostop noprint pass
source misc/gdbscripts
echo                Running...\n
run

# Doesn't really work, since the program has already exited in either case...
init-if-undefined $_exitcode = 0
init-if-undefined $_exitsignal = 0
if $_exitcode || $_exitsignal
	echo Non-zero exit code or killed by signal!\n
        dumpstate
else
	# If we pressed Ctrl+C or otherwise interrupted the program, a signal would be set
	if $_isvoid($_siginfo)
		echo Program exited normally\n
		quit
	end
end


