From: Kienan Stewart Date: Fri, 11 Oct 2024 15:40:34 +0000 (+0000) Subject: Tests: Add environment variables in tests to attach gdbserver X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=c1eb72c624235df8eb0118f1ad7f05eabcd4d3de;p=lttng-tools.git Tests: Add environment variables in tests to attach gdbserver 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 Signed-off-by: Jérémie Galarneau --- diff --git a/tests/utils/lttngtest/environment.py b/tests/utils/lttngtest/environment.py index ec64c0a53..f97546367 100644 --- a/tests/utils/lttngtest/environment.py +++ b/tests/utils/lttngtest/environment.py @@ -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): diff --git a/tests/utils/utils.sh b/tests/utils/utils.sh index 9bb557ad8..f4b68089a 100644 --- a/tests/utils/utils.sh +++ b/tests/utils/utils.sh @@ -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 }