From 80c81ed2b03f5b3de8d5c9c32a21ad6160f18681 Mon Sep 17 00:00:00 2001
From: Maxim Cournoyer <maxim.cournoyer@savoirfairelinux.com>
Date: Thu, 23 Mar 2023 12:29:05 -0400
Subject: [PATCH] build.py: Really run in GDB when using --debug.

The usage (--help) says: "Build with debug support; run in GDB", but
GDB was only used when using the D-Bus daemon.  Run a libwrap built
Jami with GDB as well.

* build.py (run_run): Disable SIGINT when using GDB, and invoke the
Jami client with GDB.  Disable output redirection to logs when using GDB.

Change-Id: Icf3415a3d1fbb87f193d0ede07cb1e1fbb179ce2
---
 build.py | 29 ++++++++++++++++++++++++-----
 1 file changed, 24 insertions(+), 5 deletions(-)

diff --git a/build.py b/build.py
index 43dfcb0f4..2f6dec507 100755
--- a/build.py
+++ b/build.py
@@ -23,6 +23,7 @@ import multiprocessing
 import os
 import platform
 import shlex
+import signal
 import shutil
 import subprocess
 import sys
@@ -458,6 +459,13 @@ def run_clean():
 
 def run_run(args):
     run_env = os.environ
+
+    if args.debug:
+        # Ignore the interruption signal when using GDB, as it's
+        # common to use C-c when debugging and we do not want the
+        # Python script to abort the debugging session.
+        signal.signal(signal.SIGINT, signal.SIG_IGN)
+
     try:
         if args.no_libwrap:
             jamid_log = open("daemon.log", 'a')
@@ -474,10 +482,18 @@ def run_run(args):
         client_log = open('jami.log', 'a')
         client_log.write('=== Starting client (%s) ===' %
                          time.strftime("%d/%m/%Y %H:%M:%S"))
-        client_process = subprocess.Popen(["./install/bin/jami", "-d"],
-                                          stdout=client_log,
-                                          stderr=client_log,
-                                          env=run_env)
+        jami_cmdline = ['install/bin/jami', '-d']
+        if args.debug:
+            jami_cmdline = ['gdb', '-ex', 'run', '--args'] + jami_cmdline
+
+        print('Invoking jami with: {}'.format(str.join(' ', jami_cmdline)))
+        if args.debug:
+            print('Debugging with GDB; NOT redirecting output to log file')
+        client_process = subprocess.Popen(
+            jami_cmdline,
+            stdout=False if args.debug else client_log,
+            stderr=False if args.debug else client_log,
+            env=run_env)
 
         with open('jami.pid', 'w') as f:
             f.write(str(client_process.pid)+'\n')
@@ -494,7 +510,10 @@ def run_run(args):
         print("\nCaught KeyboardInterrupt...")
 
     finally:
-        if args.background == False:
+        if args.debug:
+            # Restore the default signal handler for SIGINT.
+            signal.signal(signal.SIGINT, signal.SIG_DFL)
+        if not args.background:
             try:
                 # Only kill the processes if they are running, as they
                 # could have been closed by the user.
-- 
GitLab