Tests: Add environment variables in tests to attach gdbserver
authorKienan Stewart <kstewart@efficios.com>
Fri, 11 Oct 2024 15:40:34 +0000 (15:40 +0000)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 24 Oct 2024 18:06:21 +0000 (14:06 -0400)
When debugging tests, the current infrastructure in both the python and
bash test harnesses require that the user start the sessiond or relayd
beforehand, and run the tests with environment variables to stop the
spawning of the respective programs.

To facilitate the process, new environment variables are added to allow
gdbserver to be spawned and attach to the relayd or sessiond. The user
may then connect with gdb, for example: `gdb -ex "target
localhost:1001"`.

Change-Id: Id4d1b446c7d6682c011ef27682198fb4a503f5f4
Signed-off-by: kienan Stewart <kstewart@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
tests/utils/lttngtest/environment.py
tests/utils/utils.sh

index ec64c0a533b89956edcc7a4d524af354502dcae4..f97546367aaa3e5516b988e359add8564aad311a 100644 (file)
@@ -822,6 +822,36 @@ class _Environment(logger._Logger):
             self._relayd_output_consumer.daemon = True
             self._relayd_output_consumer.start()
 
+        if os.environ.get("LTTNG_TEST_GDBSERVER_RELAYD") is not None:
+            subprocess.Popen(
+                [
+                    "gdbserver",
+                    "--attach",
+                    "localhost:{}".format(
+                        os.environ.get("LTTNG_TEST_GDBSERVER_RELAYD_PORT", "1025")
+                    ),
+                    str(process.pid),
+                ]
+            )
+
+            if os.environ.get("LTTNG_TEST_GDBSERVER_RELAYD_WAIT", ""):
+                input("Waiting for user input. Press `Enter` to continue")
+            else:
+                subprocess.Popen(
+                    [
+                        "gdb",
+                        "--batch-silent",
+                        "-ex",
+                        "target remote localhost:{}".format(
+                            os.environ.get("LTTNG_TEST_GDBSERVER_RELAYD_PORT", "1025")
+                        ),
+                        "-ex",
+                        "continue",
+                        "-ex",
+                        "disconnect",
+                    ]
+                )
+
         return process
 
     def _launch_lttng_sessiond(self):
@@ -894,6 +924,36 @@ class _Environment(logger._Logger):
             # Wait for SIGUSR1, indicating the sessiond is ready to proceed
             wait_queue.wait_for_signal()
 
+        if os.environ.get("LTTNG_TEST_GDBSERVER_SESSIOND") is not None:
+            subprocess.Popen(
+                [
+                    "gdbserver",
+                    "--attach",
+                    "localhost:{}".format(
+                        os.environ.get("LTTNG_TEST_GDBSERVER_SESSIOND_PORT", "1024")
+                    ),
+                    str(process.pid),
+                ]
+            )
+
+            if os.environ.get("LTTNG_TEST_GDBSERVER_SESSIOND_WAIT", ""):
+                input("Waiting for user input. Press `Enter` to continue")
+            else:
+                subprocess.Popen(
+                    [
+                        "gdb",
+                        "--batch-silent",
+                        "-ex",
+                        "target remote localhost:{}".format(
+                            os.environ.get("LTTNG_TEST_GDBSERVER_SESSIOND_PORT", "1024")
+                        ),
+                        "-ex",
+                        "continue",
+                        "-ex",
+                        "disconnect",
+                    ]
+                )
+
         return process
 
     def _handle_termination_signal(self, signal_number, frame):
index 9bb557ad8cf1eda5f6410d855274460dbae71559..f4b68089a939ba00551814c4ea7d3c0aee35cbd0 100644 (file)
@@ -14,6 +14,12 @@ RELAYD_MATCH=".*lttng-relayd.*"
 LTTNG_BIN="lttng"
 BABELTRACE_BIN="babeltrace2"
 LTTNG_TEST_LOG_DIR="${LTTNG_TEST_LOG_DIR:-}"
+LTTNG_TEST_GDBSERVER_RELAYD="${LTTNG_TEST_GDBSERVER_RELAYD:-}"
+LTTNG_TEST_GDBSERVER_RELAYD_PORT="${LTTNG_TEST_GDBSERVER_RELAYD_PORT:-1025}"
+LTTNG_TEST_GDBSERVER_RELAYD_WAIT="${LTTNG_TEST_GDBSERVER_RELAYD_WAIT:-}"
+LTTNG_TEST_GDBSERVER_SESSIOND="${LTTNG_TEST_GDBSERVER_SESSIOND:-}"
+LTTNG_TEST_GDBSERVER_SESSIOND_PORT="${LTTNG_TEST_GDBSERVER_SESSIOND_PORT:-1024}"
+LTTNG_TEST_GDBSERVER_SESSIOND_WAIT="${LTTNG_TEST_GDBSERVER_SESSIOND_WAIT:-}"
 LTTNG_TEST_VERBOSE_BABELTRACE="${LTTNG_TEST_VERBOSE_BABELTRACE:-}"
 LTTNG_TEST_BABELTRACE_VERBOSITY="${LTTNG_TEST_BABELTRACE_VERBOSITY:-I}"
 LTTNG_TEST_VERBOSE_CLIENT="${LTTNG_TEST_VERBOSE_CLIENT:-}"
@@ -779,9 +785,21 @@ function start_lttng_relayd_opt()
                local ret="${?}"
                if [ $withtap -eq "1" ]; then
                        ok $ret "Start lttng-relayd (process mode: $process_mode opt: ${opts[*]})"
-               else
-                       return $ret
                fi
+
+               if [[ -n "${LTTNG_TEST_GDBSERVER_RELAYD}" ]]; then
+                       # The 'bash' is required since gdbserver doesn't end up running in the
+                       # background with '&'.
+                       bash -c "$(which gdbserver) --attach localhost:${LTTNG_TEST_GDBSERVER_RELAYD_PORT} $(lttng_pgrep "${RELAYD_MATCH}" | head -n 1)" >/dev/null 2>&1 &
+                       if [[ -n "${LTTNG_TEST_GDBSERVER_RELAYD_WAIT}" ]]; then
+                               read -p "Waiting for user input. Press 'Enter' to continue: "
+                       else
+                               # Continue blocks this, but when the next break or signal happens,
+                               # the process will disconnect and terminate.
+                               gdb --batch-silent -ex "target remote localhost:${LTTNG_TEST_GDBSERVER_RELAYD_PORT}" -ex "continue" -ex "disconnect" &
+                       fi
+               fi
+               return $ret
        else
                pass "Start lttng-relayd (opt: ${opts[*]})"
        fi
@@ -963,6 +981,19 @@ function start_lttng_sessiond_opt()
                if [ "$withtap" -eq "1" ]; then
                        ok $status "Start session daemon"
                fi
+
+               if [[ -n "${LTTNG_TEST_GDBSERVER_SESSIOND}" ]]; then
+                       # The 'bash' is required since gdbserver doesn't end up running in the
+                       # background with '&'.
+                       bash -c "$(which gdbserver) --attach localhost:${LTTNG_TEST_GDBSERVER_SESSIOND_PORT} $(lttng_pgrep "${SESSIOND_MATCH}" | head -n 1)" >/dev/null 2>&1 &
+                       if [[ -n "${LTTNG_TEST_GDBSERVER_SESSIOND_WAIT}" ]]; then
+                               read -p "Waiting for user input. Press 'Enter' to continue: "
+                       else
+                               # Continue blocks this, but when the next break or signal happens,
+                               # the process will disconnect and terminate.
+                               gdb --batch-silent -ex "target remote localhost:${LTTNG_TEST_GDBSERVER_SESSIOND_PORT}" -ex "continue" -ex "disconnect" &
+                       fi
+               fi
        fi
 }
 
This page took 0.028322 seconds and 4 git commands to generate.