llgdb.py: Make sure to clean up the debugger on exit.

<rdar://problem/51807962>

llvm-svn: 363611
GitOrigin-RevId: 2d26cf37d777c9e6c4f89144b506a153b010d495
diff --git a/llgdb.py b/llgdb.py
index 7d4fdd6..5f14497 100644
--- a/llgdb.py
+++ b/llgdb.py
@@ -58,6 +58,12 @@
 debugger = lldb.SBDebugger.Create()
 debugger.SkipLLDBInitFiles(args.n)
 
+# Make sure to clean up the debugger on exit.
+import atexit
+def on_exit():
+    debugger.Terminate()
+atexit.register(on_exit)
+
 # Don't return from lldb function calls until the process stops.
 debugger.SetAsync(False)
 
@@ -150,7 +156,6 @@
             print debugger.HandleCommand(' '.join(cmd))
 
     except SystemExit:
-        lldb.SBDebugger_Terminate()
         raise
     except:
         print 'Could not handle the command "%s"' % ' '.join(cmd)