I did some tests with the conditions you mentioned, haven't been able to hang the IDE after building several projects after using Search in files and running under the GDB debugger.
The consistent hang I can reproduce is after rebuilding the IDE: it managed to succesfully exit once, because the process only really linked, when I cleaned it and rebuilt it, it hanged again, I attach, as you requested, the log.txt file, such as it is: it just stops logging mid word.
"it just stops logging mid word"
And that is in the middle of compiling. Unless there is a big chunk of the log, that was not written to disk.
When it happens again, look at the title of the IDE main window (the window with the menu): It should still say "compiling", and the "red square >stop< button" should still be active.
If that happens, and there is no progress in the "messages" windows, then open the task monitor, and check if there is any fpc.exe and/or ppc___.exe (depending on bitness that is ppci386.exe or ppcx86.exe or ...)
If it does not say "compiling", (and the red button is disabled) then it finished compiling, and it should try to restart.
If it hangs during restart => you can test that by going to Tools > "Configure build lazarus" and un-check the "restart after building IDE" checkbox. (Then you have to restart yourself at some point).
You can build your IDE with -WC (specified in "Configure build lazarus"). Then do not use --debug-log.
The IDE will now open with a console window, and all logging happens there. This often has less caching going on, so more of the log-output may be seen. But I would check the above things first.
Last not least.
The IDE has a feature to check if it is running. Even if switched off, it still does some check...
If the IDE hangs during startup, check in the task manager how many lazarus.exe and how many startlazarus.exe there are.
You can also locate your temp folder e.g. C:\Users\USERNAME\AppData\Local\Temp
and (when the IDE is NOT running) delete all files *_LazarusMain*