I am writing a python(v2.7) script in GDB(v7.5.1-0.7.29). I want to quit the python script when certain condition got false. But i do not want to quit GDB. I tried using sys.exit(), exit() and quit(), but in those case they also quit GDB. Is there any way to just quit the python script but not the gdb. Like ctrl + c command but i want this happend only when a certain condition got false.
2 Answers
You have to raise an exception. Question is what exception to raise; whether you provide code to catch it and whether you want traceback to be printed or not.
If you don't mind traceback to be printed you can raise KeyboardInterrupt. But I presume you want graceful exit without traceback.
If you are writing code for class that integrates into GDB command then you can raise gdb.GdbError. It will be consumed by the code implementing command class and no traceback is printed.
If you are writing a script that is executed by sourcing it then you have to embed your whole script into try / except and catch exception you are raising. In fact, calling exit() also simply raises exception SystemExit, so you may write your script as:
try:
some code
if some_condition:
exit()
some more code
except SystemExit:
pass