Skip to contentSkip to conversation

qemu_iotests: improve debugging options

PATCH Superseded Emanuele Giuseppe Esposito 4 participants 50 messages May 20, 2021 – Jun 4, 2021

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v4 cover qemu_iotests: improve debugging options

This series adds the option to attach gdbserver and valgrind to the QEMU binary running in qemu_iotests.
It also allows to redirect QEMU binaries output of the python tests to the stdout, instead of a log file.

Patches 1-6 introduce the -gdb option to both python and bash tests, 7-10 extend the already existing -valgrind flag to work also on python tests, and patch 11 introduces -p to enable logging to stdout.

In particular, patches 1,5,10 focus on extending the QMP socket timers when using gdb/valgrind, otherwise the python tests will fail due to delays in the QMP responses.

This series is tested on the previous serie "qemu-iotests: quality of life improvements"
but independent from it, so it can be applied separately.

---
v4:
* Rename environment variable from GDB_QEMU to GDB_OPTIONS
* This time test 297 (pylint) passes [Max]
* Refactor the qmp_timer field in machine.py, and add a new parameter in machine.py and subclasses constructor [John]
* Add additional check in patch 4 to cover the case where GDB_OPTIONS is empty

Emanuele Giuseppe Esposito (15):
python: qemu: add timer parameter for qmp.accept socket
python: qemu: pass the wrapper field from QEMUQtestmachine to
QEMUMachine
docs/devel/testing: add debug section to the QEMU iotests chapter
qemu-iotests: add option to attach gdbserver
qemu-iotests: delay QMP socket timers
qemu_iotests: insert gdbserver command line as wrapper for qemu binary
qemu-iotests: add gdbserver option to script tests too docs/devel/testing: add -gdb option to the debugging section of QEMU
iotests
qemu-iotests: extend the check script to support valgrind for python
tests
qemu-iotests: extent QMP socket timeout when using valgrind
qemu-iotests: allow valgrind to read/delete the generated log file
qemu-iotests: insert valgrind command line as wrapper for qemu binary docs/devel/testing: add -valgrind option to the debug section of QEMU
iotests
qemu-iotests: add option to show qemu binary logs on stdout docs/devel/testing: add -p option to the debug section of QEMU iotests

 docs/devel/testing.rst        | 30 +++++++++++++++++++
 python/qemu/machine.py        |  7 +++--
 python/qemu/qtest.py          |  9 ++++--
 tests/qemu-iotests/check      | 15 +++++++---
 tests/qemu-iotests/common.rc  |  8 ++++-
 tests/qemu-iotests/iotests.py | 56 +++++++++++++++++++++++++++++++----
 tests/qemu-iotests/testenv.py | 25 ++++++++++++++--
 7 files changed, 132 insertions(+), 18 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH v4 1/15 python: qemu: add timer parameter for qmp.accept socket

Alsp add a new _qmp_timer field to the QEMUMachine class.

Let's change the default socket timeout to None, so that if a subclass needs to add a timer, it can be done by modifying this private field.

At the same time, restore the timer to be 15 seconds in iotests.py, to give an upper bound to qemu-iotests execution.

---
 python/qemu/machine.py        | 7 +++++--
 python/qemu/qtest.py          | 5 +++--
 tests/qemu-iotests/iotests.py | 3 ++-
 3 files changed, 10 insertions(+), 5 deletions(-)
python/qemu/machine.py +5 -2
index 6e44bda337..df32de4377 100644
@@ -89,7 +89,8 @@ def __init__(self,
                  socket_scm_helper: Optional[str] = None,
                  sock_dir: Optional[str] = None,
                  drain_console: bool = False,
-                 console_log: Optional[str] = None):
+                 console_log: Optional[str] = None,
+                 qmp_timer: Optional[float] = None):
         '''
         Initialize a QEMUMachine
 
@@ -103,6 +104,7 @@ def __init__(self,
         @param sock_dir: where to create socket (overrides test_dir for sock)
         @param drain_console: (optional) True to drain console socket to buffer
         @param console_log: (optional) path to console log file
+        @param qmp_timer: (optional) default QMP socket timeout
         @note: Qemu process is not started until launch() is used.
         '''
         # Direct user configuration
@@ -110,6 +112,7 @@ def __init__(self,
         self._binary = binary
         self._args = list(args)
         self._wrapper = wrapper
+        self._qmp_timer = qmp_timer
 
         self._name = name or "qemu-%d" % os.getpid()
         self._test_dir = test_dir
@@ -323,7 +326,7 @@ def _pre_launch(self) -> None:
 
     def _post_launch(self) -> None:
         if self._qmp_connection:
-            self._qmp.accept()
+            self._qmp.accept(self._qmp_timer)
 
     def _post_shutdown(self) -> None:
         """
python/qemu/qtest.py +3 -2
index 39a0cf62fe..afea210d9d 100644
@@ -114,14 +114,15 @@ def __init__(self,
                  name: Optional[str] = None,
                  test_dir: str = "/var/tmp",
                  socket_scm_helper: Optional[str] = None,
-                 sock_dir: Optional[str] = None):
+                 sock_dir: Optional[str] = None,
+                 qmp_timer: Optional[float] = None):
         if name is None:
             name = "qemu-%d" % os.getpid()
         if sock_dir is None:
             sock_dir = test_dir
         super().__init__(binary, args, name=name, test_dir=test_dir,
                          socket_scm_helper=socket_scm_helper,
-                         sock_dir=sock_dir)
+                         sock_dir=sock_dir, qmp_timer=qmp_timer)
         self._qtest: Optional[QEMUQtestProtocol] = None
         self._qtest_path = os.path.join(sock_dir, name + "-qtest.sock")
 
tests/qemu-iotests/iotests.py +2 -1
index ec3c69daf1..5d78de0f0b 100644
@@ -571,10 +571,11 @@ class VM(qtest.QEMUQtestMachine):
 
     def __init__(self, path_suffix=''):
         name = "qemu%s-%d" % (path_suffix, os.getpid())
+        timer = 15.0
         super().__init__(qemu_prog, qemu_opts, name=name,
                          test_dir=test_dir,
                          socket_scm_helper=socket_scm_helper,
-                         sock_dir=sock_dir)
+                         sock_dir=sock_dir, qmp_timer=timer)
         self._num_drives = 0
 
     def add_object(self, opts):
Signature · 1 line
2.30.2
Reviewed-by on v4 1/15
#2 lore raw

Re: [PATCH v4 01/15] python: qemu: add timer parameter for qmp.accept socket

Quoted 8 lines · Emanuele Giuseppe Esposito

Alsp add a new _qmp_timer field to the QEMUMachine class.

Let's change the default socket timeout to None, so that if a subclass needs to add a timer, it can be done by modifying this private field.

At the same time, restore the timer to be 15 seconds in iotests.py, to give an upper bound to qemu-iotests execution.

Hmm. Not to qemu-iotests execution, but only to connection to qmp monitor, isn't it?

Quoted 77 lines
---
  python/qemu/machine.py        | 7 +++++--
  python/qemu/qtest.py          | 5 +++--
  tests/qemu-iotests/iotests.py | 3 ++-
  3 files changed, 10 insertions(+), 5 deletions(-)
python/qemu/machine.py +5 -2
index 6e44bda337..df32de4377 100644
@@ -89,7 +89,8 @@ def __init__(self,
                   socket_scm_helper: Optional[str] = None,
                   sock_dir: Optional[str] = None,
                   drain_console: bool = False,
-                 console_log: Optional[str] = None):
+                 console_log: Optional[str] = None,
+                 qmp_timer: Optional[float] = None):
          '''
          Initialize a QEMUMachine
 
@@ -103,6 +104,7 @@ def __init__(self,
          @param sock_dir: where to create socket (overrides test_dir for sock)
          @param drain_console: (optional) True to drain console socket to buffer
          @param console_log: (optional) path to console log file
+        @param qmp_timer: (optional) default QMP socket timeout
          @note: Qemu process is not started until launch() is used.
          '''
          # Direct user configuration
@@ -110,6 +112,7 @@ def __init__(self,
          self._binary = binary
          self._args = list(args)
          self._wrapper = wrapper
+        self._qmp_timer = qmp_timer
 
          self._name = name or "qemu-%d" % os.getpid()
          self._test_dir = test_dir
@@ -323,7 +326,7 @@ def _pre_launch(self) -> None:
 
      def _post_launch(self) -> None:
          if self._qmp_connection:
-            self._qmp.accept()
+            self._qmp.accept(self._qmp_timer)
 
      def _post_shutdown(self) -> None:
          """
python/qemu/qtest.py +3 -2
index 39a0cf62fe..afea210d9d 100644
@@ -114,14 +114,15 @@ def __init__(self,
                   name: Optional[str] = None,
                   test_dir: str = "/var/tmp",
                   socket_scm_helper: Optional[str] = None,
-                 sock_dir: Optional[str] = None):
+                 sock_dir: Optional[str] = None,
+                 qmp_timer: Optional[float] = None):
          if name is None:
              name = "qemu-%d" % os.getpid()
          if sock_dir is None:
              sock_dir = test_dir
          super().__init__(binary, args, name=name, test_dir=test_dir,
                           socket_scm_helper=socket_scm_helper,
-                         sock_dir=sock_dir)
+                         sock_dir=sock_dir, qmp_timer=qmp_timer)
          self._qtest: Optional[QEMUQtestProtocol] = None
          self._qtest_path = os.path.join(sock_dir, name + "-qtest.sock")
 
tests/qemu-iotests/iotests.py +0 -0
index ec3c69daf1..5d78de0f0b 100644
@@ -571,10 +571,11 @@ class VM(qtest.QEMUQtestMachine):
 
      def __init__(self, path_suffix=''):

tests/qemu-iotests/iotests.py class VM(qtest.QEMUQtestMachine): · line 581

Quoted 10 lines
tests/qemu-iotests/iotests.py +2 -1
          name = "qemu%s-%d" % (path_suffix, os.getpid())
+        timer = 15.0
          super().__init__(qemu_prog, qemu_opts, name=name,
                           test_dir=test_dir,
                           socket_scm_helper=socket_scm_helper,
-                         sock_dir=sock_dir)
+                         sock_dir=sock_dir, qmp_timer=timer)
          self._num_drives = 0
 
      def add_object(self, opts):

I'd call it qmp_accept_timeout to be precise. Anyway:

Signature · 2 lines
Best regards,
Vladimir
#3 lore raw

Re: [PATCH v4 01/15] python: qemu: add timer parameter for qmp.accept socket

In reply to Emanuele Giuseppe Esposito
Quoted 10 lines · Emanuele Giuseppe Esposito

Alsp add a new _qmp_timer field to the QEMUMachine class.

Let's change the default socket timeout to None, so that if a subclass needs to add a timer, it can be done by modifying this private field.

At the same time, restore the timer to be 15 seconds in iotests.py, to give an upper bound to qemu-iotests execution.

Hi Emanuele: I'm sorry, but with the recent Python PR this no longer applies to origin/master -- the python files got shuffled around a bit when I added the new CI tests.

May I please ask you to rebase? You don't have to re-spin just yet, just pointing me to the rebase would help me out.

(Also: if you push to gitlab, you can take advantage of the new Python CI tests!)

Apologies,
--js

#4 lore raw

Re: [PATCH v4 01/15] python: qemu: add timer parameter for qmp.accept socket

Quoted 7 lines · John Snow
Quoted 6 lines · Emanuele Giuseppe Esposito

Alsp add a new _qmp_timer field to the QEMUMachine class.

Let's change the default socket timeout to None, so that if a subclass needs to add a timer, it can be done by modifying this private field.

Quoted 8 lines
Quoted 4 lines

At the same time, restore the timer to be 15 seconds in iotests.py, to give an upper bound to qemu-iotests execution.

Hi Emanuele: I'm sorry, but with the recent Python PR this no longer applies to origin/master -- the python files got shuffled around a bit when I added the new CI tests.

May I please ask you to rebase? You don't have to re-spin just yet, just pointing me to the rebase would help me out.

Hi John, no problem. I rebased here:
https://gitlab.com/eesposit/qemu/-/commits/qemu_iotests_io

Let me know if I need to do anything else.
I will re-spin later today.

Thank you,
Emanuele

#5 lore raw

Re: [PATCH v4 01/15] python: qemu: add timer parameter for qmp.accept socket

Quoted 19 lines · Emanuele Giuseppe Esposito
Quoted 7 lines · John Snow
Quoted 6 lines · Emanuele Giuseppe Esposito

Alsp add a new _qmp_timer field to the QEMUMachine class.

Let's change the default socket timeout to None, so that if a subclass needs to add a timer, it can be done by modifying this private field.

Quoted 8 lines
Quoted 4 lines

At the same time, restore the timer to be 15 seconds in iotests.py, to give an upper bound to qemu-iotests execution.

Hi Emanuele: I'm sorry, but with the recent Python PR this no longer applies to origin/master -- the python files got shuffled around a bit when I added the new CI tests.

May I please ask you to rebase? You don't have to re-spin just yet, just pointing me to the rebase would help me out.

Quoted 9 lines

Hi John, no problem. I rebased here:
https://gitlab.com/eesposit/qemu/-/commits/qemu_iotests_io

Let me know if I need to do anything else.
I will re-spin later today.

Thank you,
Emanuele

Hi Emanuele:

https://gitlab.com/jsnow/qemu/-/commits/review

I added in a pylint ignore for you and these patches are clean now.

--js

#6 lore raw

Re: [PATCH v4 01/15] python: qemu: add timer parameter for qmp.accept socket

Quoted 30 lines · John Snow
Quoted 19 lines · Emanuele Giuseppe Esposito
Quoted 7 lines · John Snow
Quoted 6 lines · Emanuele Giuseppe Esposito

Alsp add a new _qmp_timer field to the QEMUMachine class.

Let's change the default socket timeout to None, so that if a subclass needs to add a timer, it can be done by modifying this private field.

Quoted 8 lines
Quoted 4 lines

At the same time, restore the timer to be 15 seconds in iotests.py, to give an upper bound to qemu-iotests execution.

Hi Emanuele: I'm sorry, but with the recent Python PR this no longer applies to origin/master -- the python files got shuffled around a bit when I added the new CI tests.

May I please ask you to rebase? You don't have to re-spin just yet, just pointing me to the rebase would help me out.

Quoted 9 lines

Hi John, no problem. I rebased here:
https://gitlab.com/eesposit/qemu/-/commits/qemu_iotests_io

Let me know if I need to do anything else.
I will re-spin later today.

Thank you,
Emanuele

Quoted 5 lines

Hi Emanuele:

https://gitlab.com/jsnow/qemu/-/commits/review

I added in a pylint ignore for you and these patches are clean now.

Thank you! I will add it to my series.

Emanuele

#7 lore raw

PATCH v4 2/15 python: qemu: pass the wrapper field from QEMUQtestmachine to QEMUMachine

In reply to Emanuele Giuseppe Esposito
---
 python/qemu/qtest.py | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
python/qemu/qtest.py +3 -1
index afea210d9d..e6a8fb5984 100644
@@ -111,6 +111,7 @@ class QEMUQtestMachine(QEMUMachine):
     def __init__(self,
                  binary: str,
                  args: Sequence[str] = (),
+                 wrapper: Sequence[str] = (),
                  name: Optional[str] = None,
                  test_dir: str = "/var/tmp",
                  socket_scm_helper: Optional[str] = None,
@@ -120,7 +121,8 @@ def __init__(self,
             name = "qemu-%d" % os.getpid()
         if sock_dir is None:
             sock_dir = test_dir
-        super().__init__(binary, args, name=name, test_dir=test_dir,
+        super().__init__(binary, args, wrapper=wrapper, name=name,
+                         test_dir=test_dir,
                          socket_scm_helper=socket_scm_helper,
                          sock_dir=sock_dir, qmp_timer=qmp_timer)
         self._qtest: Optional[QEMUQtestProtocol] = None
Signature · 1 line
2.30.2
Reviewed-by on v4 2/15
#8 lore raw

Re: [PATCH v4 02/15] python: qemu: pass the wrapper field from QEMUQtestmachine to QEMUMachine

in subject: s/QEMUQtestmachine/QEMUQtestMachine

Quoted 3 lines · Emanuele Giuseppe Esposito

Usually, r-b lines are placed below s-o-b line.

Quoted 27 lines
---
  python/qemu/qtest.py | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
python/qemu/qtest.py +3 -1
index afea210d9d..e6a8fb5984 100644
@@ -111,6 +111,7 @@ class QEMUQtestMachine(QEMUMachine):
      def __init__(self,
                   binary: str,
                   args: Sequence[str] = (),
+                 wrapper: Sequence[str] = (),
                   name: Optional[str] = None,
                   test_dir: str = "/var/tmp",
                   socket_scm_helper: Optional[str] = None,
@@ -120,7 +121,8 @@ def __init__(self,
              name = "qemu-%d" % os.getpid()
          if sock_dir is None:
              sock_dir = test_dir
-        super().__init__(binary, args, name=name, test_dir=test_dir,
+        super().__init__(binary, args, wrapper=wrapper, name=name,
+                         test_dir=test_dir,
                           socket_scm_helper=socket_scm_helper,
                           sock_dir=sock_dir, qmp_timer=qmp_timer)
          self._qtest: Optional[QEMUQtestProtocol] = None
Signature · 2 lines
Best regards,
Vladimir
#9 lore raw

PATCH v4 3/15 docs/devel/testing: add debug section to the QEMU iotests chapter

In reply to Emanuele Giuseppe Esposito

Introduce the "Debugging a test case" section, in preparation to the additional flags that will be added in the next patches.

---
 docs/devel/testing.rst | 8 ++++++++
 1 file changed, 8 insertions(+)
docs/devel/testing.rst +8 -0
index 1da4c4e4c4..8144e316a4 100644
@@ -224,6 +224,14 @@ another application on the host may have locked the file, possibly leading to a
 test failure.  If using such devices are explicitly desired, consider adding
 ``locking=off`` option to disable image locking.
 
+Debugging a test case
+-----------------------
+The following options to the ``check`` script can be useful when debugging
+a failing test:
+
+* ``-d`` (debug) just increases the logging verbosity, showing
+  for example the QMP commands and answers.
+
 Test case groups
 ----------------
 
Signature · 1 line
2.30.2
Reviewed-by on v4 3/15
#10 lore raw

Re: [PATCH v4 03/15] docs/devel/testing: add debug section to the QEMU iotests chapter

Quoted 4 lines · Emanuele Giuseppe Esposito

Introduce the "Debugging a test case" section, in preparation to the additional flags that will be added in the next patches.

Signature · 2 lines
Best regards,
Vladimir
#11 lore raw

PATCH v4 4/15 qemu-iotests: add option to attach gdbserver

In reply to Emanuele Giuseppe Esposito

Define -gdb flag and GDB_OPTIONS environment variable to python tests to attach a gdbserver to each qemu instance. This patch only adds and parses this flag, it does not yet add the implementation for it.

if -gdb is not provided but $GDB_OPTIONS is set, ignore the environment variable.

---
 tests/qemu-iotests/check      |  6 +++++-
 tests/qemu-iotests/iotests.py |  5 +++++
 tests/qemu-iotests/testenv.py | 19 ++++++++++++++++---
 3 files changed, 26 insertions(+), 4 deletions(-)
tests/qemu-iotests/check +5 -1
index d1c87ceaf1..b9820fdaaf 100755
@@ -33,6 +33,9 @@ def make_argparser() -> argparse.ArgumentParser:
                    help='pretty print output for make check')
 
     p.add_argument('-d', dest='debug', action='store_true', help='debug')
+    p.add_argument('-gdb', action='store_true',
+                   help="start gdbserver with $GDB_OPTIONS options \
+                        ('localhost:12345' if $GDB_OPTIONS is empty)")
     p.add_argument('-misalign', action='store_true',
                    help='misalign memory allocations')
     p.add_argument('--color', choices=['on', 'off', 'auto'],
@@ -112,7 +115,8 @@ if __name__ == '__main__':
     env = TestEnv(imgfmt=args.imgfmt, imgproto=args.imgproto,
                   aiomode=args.aiomode, cachemode=args.cachemode,
                   imgopts=args.imgopts, misalign=args.misalign,
-                  debug=args.debug, valgrind=args.valgrind)
+                  debug=args.debug, valgrind=args.valgrind,
+                  gdb=args.gdb)
 
     testfinder = TestFinder(test_dir=env.source_iotests)
 
tests/qemu-iotests/iotests.py +5 -0
index 5d78de0f0b..d667fde6f8 100644
@@ -75,6 +75,11 @@
 qemu_prog = os.environ.get('QEMU_PROG', 'qemu')
 qemu_opts = os.environ.get('QEMU_OPTIONS', '').strip().split(' ')
 
+gdb_qemu_env = os.environ.get('GDB_OPTIONS')
+qemu_gdb = []
+if gdb_qemu_env:
+    qemu_gdb = ['gdbserver'] + gdb_qemu_env.strip().split(' ')
+
 imgfmt = os.environ.get('IMGFMT', 'raw')
 imgproto = os.environ.get('IMGPROTO', 'file')
 output_dir = os.environ.get('OUTPUT_DIR', '.')
tests/qemu-iotests/testenv.py +16 -3
index 6d27712617..49ddd586ef 100644
@@ -27,6 +27,7 @@
 import glob
 from typing import Dict, Any, Optional, ContextManager
 
+DEF_GDB_OPTIONS = 'localhost:12345'
 
 def isxfile(path: str) -> bool:
     return os.path.isfile(path) and os.access(path, os.X_OK)
@@ -72,7 +73,8 @@ class TestEnv(ContextManager['TestEnv']):
                      'QEMU_NBD_OPTIONS', 'IMGOPTS', 'IMGFMT', 'IMGPROTO',
                      'AIOMODE', 'CACHEMODE', 'VALGRIND_QEMU',
                      'CACHEMODE_IS_DEFAULT', 'IMGFMT_GENERIC', 'IMGOPTSSYNTAX',
-                     'IMGKEYSECRET', 'QEMU_DEFAULT_MACHINE', 'MALLOC_PERTURB_']
+                     'IMGKEYSECRET', 'QEMU_DEFAULT_MACHINE', 'MALLOC_PERTURB_',
+                     'GDB_OPTIONS']
 
     def get_env(self) -> Dict[str, str]:
         env = {}
@@ -163,7 +165,8 @@ def __init__(self, imgfmt: str, imgproto: str, aiomode: str,
                  imgopts: Optional[str] = None,
                  misalign: bool = False,
                  debug: bool = False,
-                 valgrind: bool = False) -> None:
+                 valgrind: bool = False,
+                 gdb: bool = False) -> None:
         self.imgfmt = imgfmt
         self.imgproto = imgproto
         self.aiomode = aiomode
@@ -171,6 +174,14 @@ def __init__(self, imgfmt: str, imgproto: str, aiomode: str,
         self.misalign = misalign
         self.debug = debug
 
+        if gdb:
+            self.gdb_options = os.environ.get('GDB_OPTIONS', DEF_GDB_OPTIONS)
+            if not self.gdb_options:
+                # cover the case 'export GDB_OPTIONS='
+                self.gdb_options = DEF_GDB_OPTIONS
+        elif 'GDB_OPTIONS' in os.environ:
+            del os.environ['GDB_OPTIONS']
+
         if valgrind:
             self.valgrind_qemu = 'y'
 
@@ -269,7 +280,9 @@ def print_env(self) -> None:
 PLATFORM      -- {platform}
 TEST_DIR      -- {TEST_DIR}
 SOCK_DIR      -- {SOCK_DIR}
-SOCKET_SCM_HELPER -- {SOCKET_SCM_HELPER}"""
+SOCKET_SCM_HELPER -- {SOCKET_SCM_HELPER}
+GDB_OPTIONS   -- {GDB_OPTIONS}
+"""
 
         args = collections.defaultdict(str, self.get_env())
 
Signature · 1 line
2.30.2
#12 lore raw

Re: [PATCH v4 04/15] qemu-iotests: add option to attach gdbserver

Quoted 1 line · Emanuele Giuseppe Esposito

Define -gdb flag and GDB_OPTIONS environment variable

Let's use --option notation for new long options

Quoted 19 lines

to python tests to attach a gdbserver to each qemu instance. This patch only adds and parses this flag, it does not yet add the implementation for it.

if -gdb is not provided but $GDB_OPTIONS is set, ignore the environment variable.

---
  tests/qemu-iotests/check      |  6 +++++-
  tests/qemu-iotests/iotests.py |  5 +++++
  tests/qemu-iotests/testenv.py | 19 ++++++++++++++++---
  3 files changed, 26 insertions(+), 4 deletions(-)
tests/qemu-iotests/check +0 -0
index d1c87ceaf1..b9820fdaaf 100755

tests/qemu-iotests/check def make_argparser() -> argparse.ArgumentParser: · line 38

Quoted 6 lines
tests/qemu-iotests/check +3 -0
@@ -33,6 +33,9 @@ def make_argparser() -> argparse.ArgumentParser:
                     help='pretty print output for make check')
 
      p.add_argument('-d', dest='debug', action='store_true', help='debug')
+    p.add_argument('-gdb', action='store_true',
+                   help="start gdbserver with $GDB_OPTIONS options \
+                        ('localhost:12345' if $GDB_OPTIONS is empty)")

Hmm.. Why not just make --gdb a string option, that defaults to GDB_OPTIONS? This way it will more similar with other options.

Quoted 88 lines
tests/qemu-iotests/check +2 -1
      p.add_argument('-misalign', action='store_true',
                     help='misalign memory allocations')
      p.add_argument('--color', choices=['on', 'off', 'auto'],
@@ -112,7 +115,8 @@ if __name__ == '__main__':
      env = TestEnv(imgfmt=args.imgfmt, imgproto=args.imgproto,
                    aiomode=args.aiomode, cachemode=args.cachemode,
                    imgopts=args.imgopts, misalign=args.misalign,
-                  debug=args.debug, valgrind=args.valgrind)
+                  debug=args.debug, valgrind=args.valgrind,
+                  gdb=args.gdb)
 
      testfinder = TestFinder(test_dir=env.source_iotests)
 
tests/qemu-iotests/iotests.py +5 -0
index 5d78de0f0b..d667fde6f8 100644
@@ -75,6 +75,11 @@
  qemu_prog = os.environ.get('QEMU_PROG', 'qemu')
  qemu_opts = os.environ.get('QEMU_OPTIONS', '').strip().split(' ')
 
+gdb_qemu_env = os.environ.get('GDB_OPTIONS')
+qemu_gdb = []
+if gdb_qemu_env:
+    qemu_gdb = ['gdbserver'] + gdb_qemu_env.strip().split(' ')
+
  imgfmt = os.environ.get('IMGFMT', 'raw')
  imgproto = os.environ.get('IMGPROTO', 'file')
  output_dir = os.environ.get('OUTPUT_DIR', '.')
tests/qemu-iotests/testenv.py +16 -3
index 6d27712617..49ddd586ef 100644
@@ -27,6 +27,7 @@
  import glob
  from typing import Dict, Any, Optional, ContextManager
 
+DEF_GDB_OPTIONS = 'localhost:12345'
 
  def isxfile(path: str) -> bool:
      return os.path.isfile(path) and os.access(path, os.X_OK)
@@ -72,7 +73,8 @@ class TestEnv(ContextManager['TestEnv']):
                       'QEMU_NBD_OPTIONS', 'IMGOPTS', 'IMGFMT', 'IMGPROTO',
                       'AIOMODE', 'CACHEMODE', 'VALGRIND_QEMU',
                       'CACHEMODE_IS_DEFAULT', 'IMGFMT_GENERIC', 'IMGOPTSSYNTAX',
-                     'IMGKEYSECRET', 'QEMU_DEFAULT_MACHINE', 'MALLOC_PERTURB_']
+                     'IMGKEYSECRET', 'QEMU_DEFAULT_MACHINE', 'MALLOC_PERTURB_',
+                     'GDB_OPTIONS']
 
      def get_env(self) -> Dict[str, str]:
          env = {}
@@ -163,7 +165,8 @@ def __init__(self, imgfmt: str, imgproto: str, aiomode: str,
                   imgopts: Optional[str] = None,
                   misalign: bool = False,
                   debug: bool = False,
-                 valgrind: bool = False) -> None:
+                 valgrind: bool = False,
+                 gdb: bool = False) -> None:
          self.imgfmt = imgfmt
          self.imgproto = imgproto
          self.aiomode = aiomode
@@ -171,6 +174,14 @@ def __init__(self, imgfmt: str, imgproto: str, aiomode: str,
          self.misalign = misalign
          self.debug = debug
 
+        if gdb:
+            self.gdb_options = os.environ.get('GDB_OPTIONS', DEF_GDB_OPTIONS)
+            if not self.gdb_options:
+                # cover the case 'export GDB_OPTIONS='
+                self.gdb_options = DEF_GDB_OPTIONS
+        elif 'GDB_OPTIONS' in os.environ:
+            del os.environ['GDB_OPTIONS']
+
          if valgrind:
              self.valgrind_qemu = 'y'
 
@@ -269,7 +280,9 @@ def print_env(self) -> None:
  PLATFORM      -- {platform}
  TEST_DIR      -- {TEST_DIR}
  SOCK_DIR      -- {SOCK_DIR}
-SOCKET_SCM_HELPER -- {SOCKET_SCM_HELPER}"""
+SOCKET_SCM_HELPER -- {SOCKET_SCM_HELPER}
+GDB_OPTIONS   -- {GDB_OPTIONS}
+"""
 
          args = collections.defaultdict(str, self.get_env())
 
Signature · 2 lines
Best regards,
Vladimir
#13 lore raw

Re: [PATCH v4 04/15] qemu-iotests: add option to attach gdbserver

Quoted 4 lines · Vladimir Sementsov-Ogievskiy
Quoted 2 lines

Define -gdb flag and GDB_OPTIONS environment variable

Let's use --option notation for new long options

Why make a mix of two styles? -- suggests that single-character options like -d and -v can be combined, is that the case?

Quoted 10 lines
Quoted 10 lines

if -gdb is not provided but $GDB_OPTIONS is set, ignore the environment variable.

---
  tests/qemu-iotests/check      |  6 +++++- tests/qemu-iotests/iotests.py |  5 +++++ tests/qemu-iotests/testenv.py | 19 ++++++++++++++++---

  3 files changed, 26 insertions(+), 4 deletions(-)
Quoted 12 lines

tests/qemu-iotests/check def make_argparser() -> argparse.ArgumentParser: · line 37

Quoted 10 lines
tests/qemu-iotests/check +3 -0
index d1c87ceaf1..b9820fdaaf 100755
@@ -33,6 +33,9 @@ def make_argparser() -> argparse.ArgumentParser:
                      help='pretty print output for make check')
       p.add_argument('-d', dest='debug', action='store_true', help='debug')
+    p.add_argument('-gdb', action='store_true',
+                   help="start gdbserver with $GDB_OPTIONS options \
+                        ('localhost:12345' if $GDB_OPTIONS is empty)")

Hmm.. Why not just make --gdb a string option, that defaults to GDB_OPTIONS? This way it will more similar with other options.

I think then something like "./check -gdb 030" would not work, right?

Paolo

#14 lore raw

Re: [PATCH v4 04/15] qemu-iotests: add option to attach gdbserver

Quoted 7 lines · Paolo Bonzini
Quoted 4 lines · Vladimir Sementsov-Ogievskiy
Quoted 2 lines

Define -gdb flag and GDB_OPTIONS environment variable

Let's use --option notation for new long options

Why make a mix of two styles? -- suggests that single-character options like -d and -v can be combined, is that the case?

Yes.. I think think that --options (with -o short options) is more usual and modern style.

We already have both --options and -options.. So, my idea when I was rewriting ./check was that better to move to --options. I can send patch to change all existing -options of check to be --options for full consistency. It would be some pain for developers..

Quoted 24 lines
Quoted 11 lines
Quoted 11 lines

if -gdb is not provided but $GDB_OPTIONS is set, ignore the environment variable.

---
  tests/qemu-iotests/check      |  6 +++++- tests/qemu-iotests/iotests.py |  5 +++++ tests/qemu-iotests/testenv.py | 19 ++++++++++++++++---

  3 files changed, 26 insertions(+), 4 deletions(-)
Quoted 12 lines

tests/qemu-iotests/check def make_argparser() -> argparse.ArgumentParser: · line 37

Quoted 10 lines
tests/qemu-iotests/check +3 -0
index d1c87ceaf1..b9820fdaaf 100755
@@ -33,6 +33,9 @@ def make_argparser() -> argparse.ArgumentParser:
                      help='pretty print output for make check')
       p.add_argument('-d', dest='debug', action='store_true', help='debug')
+    p.add_argument('-gdb', action='store_true',
+                   help="start gdbserver with $GDB_OPTIONS options \
+                        ('localhost:12345' if $GDB_OPTIONS is empty)")

Hmm.. Why not just make --gdb a string option, that defaults to GDB_OPTIONS? This way it will more similar with other options.

Quoted 2 lines

I think then something like "./check -gdb 030" would not work, right?

Hmm, yes, that's not very convenient. OK then, let's keep bool.

Signature · 2 lines
Best regards,
Vladimir
#15 lore raw

Re: [PATCH v4 04/15] qemu-iotests: add option to attach gdbserver

Quoted 5 lines · Vladimir Sementsov-Ogievskiy
Quoted 4 lines · Paolo Bonzini
Quoted 3 lines · Vladimir Sementsov-Ogievskiy
Quoted 2 lines

Define -gdb flag and GDB_OPTIONS environment variable

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Let's use --option notation for new long options

Why make a mix of two styles? -- suggests that single-character options like -d and -v can be combined, is that the case?

Yes.. I think think that --options (with -o short options) is more usual and modern style.

We already have both --options and -options.. So, my idea when I was rewriting ./check was that better to move to --options. I can send patch to change all existing -options of check to be --options for full consistency. It would be some pain for developers..

I am following the current convention. I put gdb on the same level/category of valgrind, and since the current option is -valgrind, I would like to stick to that. If you want to send a patch changing all -options in --options, feel free to do it in a separate series that changes also -gdb :)

Thank you,
Emanuele

Quoted 31 lines
Quoted 25 lines
Quoted 12 lines
Quoted 12 lines

if -gdb is not provided but $GDB_OPTIONS is set, ignore the environment variable.

---
  tests/qemu-iotests/check      |  6 +++++- tests/qemu-iotests/iotests.py |  5 +++++ tests/qemu-iotests/testenv.py | 19 ++++++++++++++++---

  3 files changed, 26 insertions(+), 4 deletions(-)
Quoted 12 lines

tests/qemu-iotests/check def make_argparser() -> argparse.ArgumentParser: · line 37

Quoted 10 lines
tests/qemu-iotests/check +3 -0
index d1c87ceaf1..b9820fdaaf 100755
@@ -33,6 +33,9 @@ def make_argparser() -> argparse.ArgumentParser:
                      help='pretty print output for make check')
       p.add_argument('-d', dest='debug', action='store_true', help='debug')
+    p.add_argument('-gdb', action='store_true',
+                   help="start gdbserver with $GDB_OPTIONS options \
+                        ('localhost:12345' if $GDB_OPTIONS is empty)")

Hmm.. Why not just make --gdb a string option, that defaults to GDB_OPTIONS? This way it will more similar with other options.

Quoted 2 lines

I think then something like "./check -gdb 030" would not work, right?

Hmm, yes, that's not very convenient. OK then, let's keep bool.

#16 lore raw

Re: [PATCH v4 04/15] qemu-iotests: add option to attach gdbserver

In reply to Emanuele Giuseppe Esposito
Quoted 44 lines · Emanuele Giuseppe Esposito

Define -gdb flag and GDB_OPTIONS environment variable to python tests to attach a gdbserver to each qemu instance. This patch only adds and parses this flag, it does not yet add the implementation for it.

if -gdb is not provided but $GDB_OPTIONS is set, ignore the environment variable.

---
  tests/qemu-iotests/check      |  6 +++++-
  tests/qemu-iotests/iotests.py |  5 +++++
  tests/qemu-iotests/testenv.py | 19 ++++++++++++++++---
  3 files changed, 26 insertions(+), 4 deletions(-)
tests/qemu-iotests/check +5 -1
index d1c87ceaf1..b9820fdaaf 100755
@@ -33,6 +33,9 @@ def make_argparser() -> argparse.ArgumentParser:
                     help='pretty print output for make check')
 
      p.add_argument('-d', dest='debug', action='store_true', help='debug')
+    p.add_argument('-gdb', action='store_true',
+                   help="start gdbserver with $GDB_OPTIONS options \
+                        ('localhost:12345' if $GDB_OPTIONS is empty)")
      p.add_argument('-misalign', action='store_true',
                     help='misalign memory allocations')
      p.add_argument('--color', choices=['on', 'off', 'auto'],
@@ -112,7 +115,8 @@ if __name__ == '__main__':
      env = TestEnv(imgfmt=args.imgfmt, imgproto=args.imgproto,
                    aiomode=args.aiomode, cachemode=args.cachemode,
                    imgopts=args.imgopts, misalign=args.misalign,
-                  debug=args.debug, valgrind=args.valgrind)
+                  debug=args.debug, valgrind=args.valgrind,
+                  gdb=args.gdb)
 
      testfinder = TestFinder(test_dir=env.source_iotests)
 
tests/qemu-iotests/iotests.py +0 -0
index 5d78de0f0b..d667fde6f8 100644

tests/qemu-iotests/iotests.py line 78

Quoted 4 lines
tests/qemu-iotests/iotests.py +1 -0
@@ -75,6 +75,11 @@
  qemu_prog = os.environ.get('QEMU_PROG', 'qemu')
  qemu_opts = os.environ.get('QEMU_OPTIONS', '').strip().split(' ')
 
+gdb_qemu_env = os.environ.get('GDB_OPTIONS')

should we specify a default? otherwise get() should raise an exception when GDB_OPTIONS is not set..

or you need os.getenv, which will return None if variable is absent.

Quoted 40 lines
tests/qemu-iotests/iotests.py +4 -0
+qemu_gdb = []
+if gdb_qemu_env:
+    qemu_gdb = ['gdbserver'] + gdb_qemu_env.strip().split(' ')
+
  imgfmt = os.environ.get('IMGFMT', 'raw')
  imgproto = os.environ.get('IMGPROTO', 'file')
  output_dir = os.environ.get('OUTPUT_DIR', '.')
tests/qemu-iotests/testenv.py +5 -2
index 6d27712617..49ddd586ef 100644
@@ -27,6 +27,7 @@
  import glob
  from typing import Dict, Any, Optional, ContextManager
 
+DEF_GDB_OPTIONS = 'localhost:12345'
 
  def isxfile(path: str) -> bool:
      return os.path.isfile(path) and os.access(path, os.X_OK)
@@ -72,7 +73,8 @@ class TestEnv(ContextManager['TestEnv']):
                       'QEMU_NBD_OPTIONS', 'IMGOPTS', 'IMGFMT', 'IMGPROTO',
                       'AIOMODE', 'CACHEMODE', 'VALGRIND_QEMU',
                       'CACHEMODE_IS_DEFAULT', 'IMGFMT_GENERIC', 'IMGOPTSSYNTAX',
-                     'IMGKEYSECRET', 'QEMU_DEFAULT_MACHINE', 'MALLOC_PERTURB_']
+                     'IMGKEYSECRET', 'QEMU_DEFAULT_MACHINE', 'MALLOC_PERTURB_',
+                     'GDB_OPTIONS']
 
      def get_env(self) -> Dict[str, str]:
          env = {}
@@ -163,7 +165,8 @@ def __init__(self, imgfmt: str, imgproto: str, aiomode: str,
                   imgopts: Optional[str] = None,
                   misalign: bool = False,
                   debug: bool = False,
-                 valgrind: bool = False) -> None:
+                 valgrind: bool = False,
+                 gdb: bool = False) -> None:
          self.imgfmt = imgfmt
          self.imgproto = imgproto
          self.aiomode = aiomode

tests/qemu-iotests/testenv.py def __init__(self, imgfmt: str, imgproto: str, aiomode: str, · line 178

Quoted 5 lines
tests/qemu-iotests/testenv.py +2 -0
@@ -171,6 +174,14 @@ def __init__(self, imgfmt: str, imgproto: str, aiomode: str,
          self.misalign = misalign
          self.debug = debug
 
+        if gdb:
+            self.gdb_options = os.environ.get('GDB_OPTIONS', DEF_GDB_OPTIONS)

everywhere in the file os.getenv is used, let's be consistent on it.

tests/qemu-iotests/testenv.py def __init__(self, imgfmt: str, imgproto: str, aiomode: str, · line 181

Quoted 3 lines
tests/qemu-iotests/testenv.py +3 -0
+            if not self.gdb_options:
+                # cover the case 'export GDB_OPTIONS='
+                self.gdb_options = DEF_GDB_OPTIONS

Hmm, a bit strange to handle 'export X=' only for this new variable, we don't have such logic for other variables.. I'm not against still, if you need it.

tests/qemu-iotests/testenv.py def __init__(self, imgfmt: str, imgproto: str, aiomode: str, · line 183

Quoted 2 lines
tests/qemu-iotests/testenv.py +2 -0
+        elif 'GDB_OPTIONS' in os.environ:
+            del os.environ['GDB_OPTIONS']

Don't need to remove variable from envirton, it has no effect. The task of TestEnv class is to prepare environment in its attributes, listed in env_variables. Then prepared attributes are available through get_env(). So if you don't want to provide GDB_OPTIONS, it's enough to not initialize self.gdb_options. So, just drop "elif:" branch.

tests/qemu-iotests/testenv.py def print_env(self) -> None: · line 284

Quoted 11 lines
tests/qemu-iotests/testenv.py +3 -1
+
          if valgrind:
              self.valgrind_qemu = 'y'
 
@@ -269,7 +280,9 @@ def print_env(self) -> None:
  PLATFORM      -- {platform}
  TEST_DIR      -- {TEST_DIR}
  SOCK_DIR      -- {SOCK_DIR}
-SOCKET_SCM_HELPER -- {SOCKET_SCM_HELPER}"""
+SOCKET_SCM_HELPER -- {SOCKET_SCM_HELPER}
+GDB_OPTIONS   -- {GDB_OPTIONS}

Not sure we need to see this additional line every time.. Maybe, show it only when gdb is set?

Quoted 5 lines
tests/qemu-iotests/testenv.py +1 -0
+"""
 
          args = collections.defaultdict(str, self.get_env())
 
Signature · 2 lines
Best regards,
Vladimir
#17 lore raw

Re: [PATCH v4 04/15] qemu-iotests: add option to attach gdbserver

Quoted 48 lines · Vladimir Sementsov-Ogievskiy
Quoted 41 lines · Emanuele Giuseppe Esposito

Define -gdb flag and GDB_OPTIONS environment variable to python tests to attach a gdbserver to each qemu instance. This patch only adds and parses this flag, it does not yet add the implementation for it.

if -gdb is not provided but $GDB_OPTIONS is set, ignore the environment variable.

---
  tests/qemu-iotests/check      |  6 +++++- tests/qemu-iotests/iotests.py |  5 +++++ tests/qemu-iotests/testenv.py | 19 ++++++++++++++++---

  3 files changed, 26 insertions(+), 4 deletions(-)
tests/qemu-iotests/check +5 -1
index d1c87ceaf1..b9820fdaaf 100755
@@ -33,6 +33,9 @@ def make_argparser() -> argparse.ArgumentParser:
                      help='pretty print output for make check')
       p.add_argument('-d', dest='debug', action='store_true', help='debug')
+    p.add_argument('-gdb', action='store_true',
+                   help="start gdbserver with $GDB_OPTIONS options \
+                        ('localhost:12345' if $GDB_OPTIONS is empty)")
       p.add_argument('-misalign', action='store_true',
                      help='misalign memory allocations')
       p.add_argument('--color', choices=['on', 'off', 'auto'],
@@ -112,7 +115,8 @@ if __name__ == '__main__':
       env = TestEnv(imgfmt=args.imgfmt, imgproto=args.imgproto,
                     aiomode=args.aiomode, cachemode=args.cachemode,
                     imgopts=args.imgopts, misalign=args.misalign,
-                  debug=args.debug, valgrind=args.valgrind)
+                  debug=args.debug, valgrind=args.valgrind,
+                  gdb=args.gdb)
       testfinder = TestFinder(test_dir=env.source_iotests)
tests/qemu-iotests/iotests.py +0 -0
index 5d78de0f0b..d667fde6f8 100644

tests/qemu-iotests/iotests.py line 77

Quoted 3 lines
tests/qemu-iotests/iotests.py +1 -0
@@ -75,6 +75,11 @@
   qemu_prog = os.environ.get('QEMU_PROG', 'qemu')
   qemu_opts = os.environ.get('QEMU_OPTIONS', '').strip().split(' ')
+gdb_qemu_env = os.environ.get('GDB_OPTIONS')

should we specify a default? otherwise get() should raise an exception when GDB_OPTIONS is not set..

Quoted 1 line

or you need os.getenv, which will return None if variable is absent.

No, os.environ.get does not raise any exception. I tested it myself, plus:
https://stackoverflow.com/questions/16924471/difference-between-os-getenv-and-os-environ-get

Quoted 54 lines
Quoted 42 lines
tests/qemu-iotests/iotests.py +4 -0
+qemu_gdb = []
+if gdb_qemu_env:
+    qemu_gdb = ['gdbserver'] + gdb_qemu_env.strip().split(' ')
+
   imgfmt = os.environ.get('IMGFMT', 'raw')
   imgproto = os.environ.get('IMGPROTO', 'file')
   output_dir = os.environ.get('OUTPUT_DIR', '.')
tests/qemu-iotests/testenv.py +0 -0
index 6d27712617..49ddd586ef 100644
@@ -27,6 +27,7 @@

  import glob
  from typing import Dict, Any, Optional, ContextManager +DEF_GDB_OPTIONS = 'localhost:12345'
  def isxfile(path: str) -> bool:
      return os.path.isfile(path) and os.access(path, os.X_OK)

tests/qemu-iotests/testenv.py +0 -1
@@ -72,7 +73,8 @@ class TestEnv(ContextManager['TestEnv']):
                        'QEMU_NBD_OPTIONS', 'IMGOPTS', 'IMGFMT', 'IMGPROTO',
                        'AIOMODE', 'CACHEMODE', 'VALGRIND_QEMU',
                        'CACHEMODE_IS_DEFAULT', 'IMGFMT_GENERIC', 'IMGOPTSSYNTAX',
-                     'IMGKEYSECRET', 'QEMU_DEFAULT_MACHINE', 

'MALLOC_PERTURB_']
+                     'IMGKEYSECRET', 'QEMU_DEFAULT_MACHINE', 'MALLOC_PERTURB_',
+                     'GDB_OPTIONS']
      def get_env(self) -> Dict[str, str]:
          env = {}

tests/qemu-iotests/testenv.py +2 -1
@@ -163,7 +165,8 @@ def __init__(self, imgfmt: str, imgproto: str, 
 aiomode: str,
                    imgopts: Optional[str] = None,
                    misalign: bool = False,
                    debug: bool = False,
-                 valgrind: bool = False) -> None:
+                 valgrind: bool = False,
+                 gdb: bool = False) -> None:

          self.imgfmt = imgfmt
          self.imgproto = imgproto
          self.aiomode = aiomode

tests/qemu-iotests/testenv.py +0 -0
@@ -171,6 +174,14 @@ def __init__(self, imgfmt: str, imgproto: str, 
 aiomode: str,
Quoted 5 lines

          self.misalign = misalign
          self.debug = debug
+        if gdb:
+            self.gdb_options = os.environ.get('GDB_OPTIONS', DEF_GDB_OPTIONS)

everywhere in the file os.getenv is used, let's be consistent on it.

Quoted 3 lines

+            if not self.gdb_options:
+                # cover the case 'export GDB_OPTIONS=' +                self.gdb_options = DEF_GDB_OPTIONS

Quoted 6 lines

Hmm, a bit strange to handle 'export X=' only for this new variable, we don't have such logic for other variables.. I'm not against still, if you need it.

Quoted 2 lines

+        elif 'GDB_OPTIONS' in os.environ:
+            del os.environ['GDB_OPTIONS']

Don't need to remove variable from envirton, it has no effect. The task of TestEnv class is to prepare environment in its attributes, listed in env_variables. Then prepared attributes are available through get_env(). So if you don't want to provide GDB_OPTIONS, it's enough to not initialize self.gdb_options. So, just drop "elif:" branch.

You forget that there are bash tests :)
Think about the following case:

# export GDB_OPTIONS="localhost:1236"
# ./check -qcow2 007 # a script test

The output will contain:
GDB_OPTIONS --
VALGRIND_QEMU --
PRINT_QEMU_OUTPUT --

BUT in common.rc we will have:

	GDB=""
        if [ ! -z ${GDB_OPTIONS} ]; then # <---- still set!
            GDB="gdbserver ${GDB_OPTIONS}"
        fi

so gsdbserver will be set anyways, and the test will block waiting for a gdb connection.

Therefore we need that elif.

Quoted 13 lines

tests/qemu-iotests/testenv.py def print_env(self) -> None: · line 284

Quoted 11 lines

+
          if valgrind:
              self.valgrind_qemu = 'y'

tests/qemu-iotests/testenv.py +2 -1
@@ -269,7 +280,9 @@ def print_env(self) -> None:
   PLATFORM      -- {platform}
   TEST_DIR      -- {TEST_DIR}
   SOCK_DIR      -- {SOCK_DIR}
-SOCKET_SCM_HELPER -- {SOCKET_SCM_HELPER}"""
+SOCKET_SCM_HELPER -- {SOCKET_SCM_HELPER}
+GDB_OPTIONS   -- {GDB_OPTIONS}

Not sure we need to see this additional line every time.. Maybe, show it only when gdb is set?

I think it can be helpful to remind the users what is not set and what is available to them (yes, one can also do ./check --help or read the docs but this is something even the laziest cannot unsee).

Thank you,
Emanuele

Quoted 6 lines
Quoted 4 lines
tests/qemu-iotests/testenv.py +1 -0
+"""
           args = collections.defaultdict(str, self.get_env())
 
#18 lore raw

Re: [PATCH v4 04/15] qemu-iotests: add option to attach gdbserver

Quoted 51 lines · Emanuele Giuseppe Esposito
Quoted 48 lines · Vladimir Sementsov-Ogievskiy
Quoted 41 lines · Emanuele Giuseppe Esposito

Define -gdb flag and GDB_OPTIONS environment variable to python tests to attach a gdbserver to each qemu instance. This patch only adds and parses this flag, it does not yet add the implementation for it.

if -gdb is not provided but $GDB_OPTIONS is set, ignore the environment variable.

---
  tests/qemu-iotests/check      |  6 +++++- tests/qemu-iotests/iotests.py |  5 +++++ tests/qemu-iotests/testenv.py | 19 ++++++++++++++++---

  3 files changed, 26 insertions(+), 4 deletions(-)
tests/qemu-iotests/check +5 -1
index d1c87ceaf1..b9820fdaaf 100755
@@ -33,6 +33,9 @@ def make_argparser() -> argparse.ArgumentParser:
                      help='pretty print output for make check')
       p.add_argument('-d', dest='debug', action='store_true', help='debug')
+    p.add_argument('-gdb', action='store_true',
+                   help="start gdbserver with $GDB_OPTIONS options \
+                        ('localhost:12345' if $GDB_OPTIONS is empty)")
       p.add_argument('-misalign', action='store_true',
                      help='misalign memory allocations')
       p.add_argument('--color', choices=['on', 'off', 'auto'],
@@ -112,7 +115,8 @@ if __name__ == '__main__':
       env = TestEnv(imgfmt=args.imgfmt, imgproto=args.imgproto,
                     aiomode=args.aiomode, cachemode=args.cachemode,
                     imgopts=args.imgopts, misalign=args.misalign,
-                  debug=args.debug, valgrind=args.valgrind)
+                  debug=args.debug, valgrind=args.valgrind,
+                  gdb=args.gdb)
       testfinder = TestFinder(test_dir=env.source_iotests)
tests/qemu-iotests/iotests.py +0 -0
index 5d78de0f0b..d667fde6f8 100644

tests/qemu-iotests/iotests.py line 77

Quoted 3 lines
tests/qemu-iotests/iotests.py +1 -0
@@ -75,6 +75,11 @@
   qemu_prog = os.environ.get('QEMU_PROG', 'qemu')
   qemu_opts = os.environ.get('QEMU_OPTIONS', '').strip().split(' ')
+gdb_qemu_env = os.environ.get('GDB_OPTIONS')

should we specify a default? otherwise get() should raise an exception when GDB_OPTIONS is not set..

Quoted 4 lines
Quoted 1 line

or you need os.getenv, which will return None if variable is absent.

No, os.environ.get does not raise any exception. I tested it myself, plus:
https://stackoverflow.com/questions/16924471/difference-between-os-getenv-and-os-environ-get

Ah, I'm wrong than. OK.

Quoted 50 lines
Quoted 50 lines
Quoted 39 lines
tests/qemu-iotests/iotests.py +4 -0
+qemu_gdb = []
+if gdb_qemu_env:
+    qemu_gdb = ['gdbserver'] + gdb_qemu_env.strip().split(' ')
+
   imgfmt = os.environ.get('IMGFMT', 'raw')
   imgproto = os.environ.get('IMGPROTO', 'file')
   output_dir = os.environ.get('OUTPUT_DIR', '.')
tests/qemu-iotests/testenv.py +0 -0
index 6d27712617..49ddd586ef 100644
@@ -27,6 +27,7 @@

  import glob
  from typing import Dict, Any, Optional, ContextManager +DEF_GDB_OPTIONS = 'localhost:12345'
  def isxfile(path: str) -> bool:
      return os.path.isfile(path) and os.access(path, os.X_OK)

tests/qemu-iotests/testenv.py +2 -1
@@ -72,7 +73,8 @@ class TestEnv(ContextManager['TestEnv']):
                        'QEMU_NBD_OPTIONS', 'IMGOPTS', 'IMGFMT', 'IMGPROTO',
                        'AIOMODE', 'CACHEMODE', 'VALGRIND_QEMU',
                        'CACHEMODE_IS_DEFAULT', 'IMGFMT_GENERIC', 'IMGOPTSSYNTAX',
-                     'IMGKEYSECRET', 'QEMU_DEFAULT_MACHINE', 'MALLOC_PERTURB_']
+                     'IMGKEYSECRET', 'QEMU_DEFAULT_MACHINE', 'MALLOC_PERTURB_',
+                     'GDB_OPTIONS']

      def get_env(self) -> Dict[str, str]:
          env = {}

tests/qemu-iotests/testenv.py +2 -1
@@ -163,7 +165,8 @@ def __init__(self, imgfmt: str, imgproto: str, aiomode: str,
                    imgopts: Optional[str] = None,
                    misalign: bool = False,
                    debug: bool = False,
-                 valgrind: bool = False) -> None:
+                 valgrind: bool = False,
+                 gdb: bool = False) -> None:

          self.imgfmt = imgfmt
          self.imgproto = imgproto
          self.aiomode = aiomode

tests/qemu-iotests/testenv.py +0 -0
@@ -171,6 +174,14 @@ def __init__(self, imgfmt: str, imgproto: str, aiomode: str,
Quoted 4 lines

          self.misalign = misalign
          self.debug = debug
+        if gdb:
+            self.gdb_options = os.environ.get('GDB_OPTIONS', DEF_GDB_OPTIONS)

everywhere in the file os.getenv is used, let's be consistent on it.

Quoted 3 lines

+            if not self.gdb_options:
+                # cover the case 'export GDB_OPTIONS=' +                self.gdb_options = DEF_GDB_OPTIONS

Quoted 8 lines
Quoted 6 lines

Hmm, a bit strange to handle 'export X=' only for this new variable, we don't have such logic for other variables.. I'm not against still, if you need it.

Quoted 2 lines

+        elif 'GDB_OPTIONS' in os.environ:
+            del os.environ['GDB_OPTIONS']

Don't need to remove variable from envirton, it has no effect. The task of TestEnv class is to prepare environment in its attributes, listed in env_variables. Then prepared attributes are available through get_env(). So if you don't want to provide GDB_OPTIONS, it's enough to not initialize self.gdb_options. So, just drop "elif:" branch.

You forget that there are bash tests :)

It shouldn't differ, environment is prepared in the same way for bash and python tests

Quoted 6 lines

Think about the following case:

# export GDB_OPTIONS="localhost:1236"
# ./check -qcow2 007 # a script test
Quoted 8 lines

The output will contain:
GDB_OPTIONS   --
VALGRIND_QEMU --
PRINT_QEMU_OUTPUT --

BUT in common.rc we will have:
    GDB=""
        if [ ! -z ${GDB_OPTIONS} ]; then # <---- still set!

Ah, I thought that we work through testenv.get_env.. But we have testenv.prepare_subprocess, which propagates the original environment too..

the bit I don't like in this all is inconsistency in interfaces of check and tests:

New interface of check:

with -gdb option use GDB_OPTIONS or default value to start gdbserver without -gdb option ignore GDB_OPTIONS

New interface of tests:

with GDB_OPTIONS run gdbserver
without GDB_OPTIONS don't run gdbserver

So, GDB_OPTIONS is different thing for tests and for check script.

I'd go another way:

Add GDB option (boolean false/true, default false) Add GDB_OPTIONS (string, default localhost:1236)

Add -gdb argument to check, which is an alternative way to set GDB variable.

This way environment-variables interface is similar for tests and ./check, and we don't need to drop a variable from the environ, and new variable behave similar to existing variables, doesn't introduce new logic.

But that all don't worth arguing. I'm OK with patch as is.

Quoted 32 lines

            GDB="gdbserver ${GDB_OPTIONS}"
        fi

so gsdbserver will be set anyways, and the test will block waiting for a gdb connection.

Therefore we need that elif.

Quoted 13 lines

tests/qemu-iotests/testenv.py def print_env(self) -> None: · line 284

Quoted 11 lines

+
          if valgrind:
              self.valgrind_qemu = 'y'

tests/qemu-iotests/testenv.py +2 -1
@@ -269,7 +280,9 @@ def print_env(self) -> None:
   PLATFORM      -- {platform}
   TEST_DIR      -- {TEST_DIR}
   SOCK_DIR      -- {SOCK_DIR}
-SOCKET_SCM_HELPER -- {SOCKET_SCM_HELPER}"""
+SOCKET_SCM_HELPER -- {SOCKET_SCM_HELPER}
+GDB_OPTIONS   -- {GDB_OPTIONS}

Not sure we need to see this additional line every time.. Maybe, show it only when gdb is set?

I think it can be helpful to remind the users what is not set and what is available to them (yes, one can also do ./check --help or read the docs but this is something even the laziest cannot unsee).

Thank you,
Emanuele

Quoted 6 lines
Quoted 4 lines
tests/qemu-iotests/testenv.py +1 -0
+"""
           args = collections.defaultdict(str, self.get_env())
 
Signature · 2 lines
Best regards,
Vladimir
Reviewed-by on v4 4/15
#19 lore raw

Re: [PATCH v4 04/15] qemu-iotests: add option to attach gdbserver

In reply to Emanuele Giuseppe Esposito
Quoted 88 lines · Emanuele Giuseppe Esposito

Define -gdb flag and GDB_OPTIONS environment variable to python tests to attach a gdbserver to each qemu instance. This patch only adds and parses this flag, it does not yet add the implementation for it.

if -gdb is not provided but $GDB_OPTIONS is set, ignore the environment variable.

---
  tests/qemu-iotests/check      |  6 +++++-
  tests/qemu-iotests/iotests.py |  5 +++++
  tests/qemu-iotests/testenv.py | 19 ++++++++++++++++---
  3 files changed, 26 insertions(+), 4 deletions(-)
tests/qemu-iotests/check +5 -1
index d1c87ceaf1..b9820fdaaf 100755
@@ -33,6 +33,9 @@ def make_argparser() -> argparse.ArgumentParser:
                     help='pretty print output for make check')
 
      p.add_argument('-d', dest='debug', action='store_true', help='debug')
+    p.add_argument('-gdb', action='store_true',
+                   help="start gdbserver with $GDB_OPTIONS options \
+                        ('localhost:12345' if $GDB_OPTIONS is empty)")
      p.add_argument('-misalign', action='store_true',
                     help='misalign memory allocations')
      p.add_argument('--color', choices=['on', 'off', 'auto'],
@@ -112,7 +115,8 @@ if __name__ == '__main__':
      env = TestEnv(imgfmt=args.imgfmt, imgproto=args.imgproto,
                    aiomode=args.aiomode, cachemode=args.cachemode,
                    imgopts=args.imgopts, misalign=args.misalign,
-                  debug=args.debug, valgrind=args.valgrind)
+                  debug=args.debug, valgrind=args.valgrind,
+                  gdb=args.gdb)
 
      testfinder = TestFinder(test_dir=env.source_iotests)
 
tests/qemu-iotests/iotests.py +5 -0
index 5d78de0f0b..d667fde6f8 100644
@@ -75,6 +75,11 @@
  qemu_prog = os.environ.get('QEMU_PROG', 'qemu')
  qemu_opts = os.environ.get('QEMU_OPTIONS', '').strip().split(' ')
 
+gdb_qemu_env = os.environ.get('GDB_OPTIONS')
+qemu_gdb = []
+if gdb_qemu_env:
+    qemu_gdb = ['gdbserver'] + gdb_qemu_env.strip().split(' ')
+
  imgfmt = os.environ.get('IMGFMT', 'raw')
  imgproto = os.environ.get('IMGPROTO', 'file')
  output_dir = os.environ.get('OUTPUT_DIR', '.')
tests/qemu-iotests/testenv.py +5 -2
index 6d27712617..49ddd586ef 100644
@@ -27,6 +27,7 @@
  import glob
  from typing import Dict, Any, Optional, ContextManager
 
+DEF_GDB_OPTIONS = 'localhost:12345'
 
  def isxfile(path: str) -> bool:
      return os.path.isfile(path) and os.access(path, os.X_OK)
@@ -72,7 +73,8 @@ class TestEnv(ContextManager['TestEnv']):
                       'QEMU_NBD_OPTIONS', 'IMGOPTS', 'IMGFMT', 'IMGPROTO',
                       'AIOMODE', 'CACHEMODE', 'VALGRIND_QEMU',
                       'CACHEMODE_IS_DEFAULT', 'IMGFMT_GENERIC', 'IMGOPTSSYNTAX',
-                     'IMGKEYSECRET', 'QEMU_DEFAULT_MACHINE', 'MALLOC_PERTURB_']
+                     'IMGKEYSECRET', 'QEMU_DEFAULT_MACHINE', 'MALLOC_PERTURB_',
+                     'GDB_OPTIONS']
 
      def get_env(self) -> Dict[str, str]:
          env = {}
@@ -163,7 +165,8 @@ def __init__(self, imgfmt: str, imgproto: str, aiomode: str,
                   imgopts: Optional[str] = None,
                   misalign: bool = False,
                   debug: bool = False,
-                 valgrind: bool = False) -> None:
+                 valgrind: bool = False,
+                 gdb: bool = False) -> None:
          self.imgfmt = imgfmt
          self.imgproto = imgproto
          self.aiomode = aiomode

tests/qemu-iotests/testenv.py def __init__(self, imgfmt: str, imgproto: str, aiomode: str, · line 182

Quoted 9 lines
tests/qemu-iotests/testenv.py +6 -0
@@ -171,6 +174,14 @@ def __init__(self, imgfmt: str, imgproto: str, aiomode: str,
          self.misalign = misalign
          self.debug = debug
 
+        if gdb:
+            self.gdb_options = os.environ.get('GDB_OPTIONS', DEF_GDB_OPTIONS)
+            if not self.gdb_options:
+                # cover the case 'export GDB_OPTIONS='
+                self.gdb_options = DEF_GDB_OPTIONS
+        elif 'GDB_OPTIONS' in os.environ:

please add a comment:

 # to not propagate it in prepare_subprocess()
Quoted 7 lines
tests/qemu-iotests/testenv.py +2 -0
+            del os.environ['GDB_OPTIONS']
+
          if valgrind:
              self.valgrind_qemu = 'y'
 

tests/qemu-iotests/testenv.py def print_env(self) -> None: · line 288

Quoted 10 lines
tests/qemu-iotests/testenv.py +3 -1
@@ -269,7 +280,9 @@ def print_env(self) -> None:
  PLATFORM      -- {platform}
  TEST_DIR      -- {TEST_DIR}
  SOCK_DIR      -- {SOCK_DIR}
-SOCKET_SCM_HELPER -- {SOCKET_SCM_HELPER}"""
+SOCKET_SCM_HELPER -- {SOCKET_SCM_HELPER}
+GDB_OPTIONS   -- {GDB_OPTIONS}
+"""
 
          args = collections.defaultdict(str, self.get_env())
 
Signature · 2 lines
Best regards,
Vladimir
#20 lore raw

PATCH v4 5/15 qemu-iotests: delay QMP socket timers

In reply to Emanuele Giuseppe Esposito

Attaching gdbserver implies that the qmp socket
should wait indefinitely for an answer from QEMU.

---
 tests/qemu-iotests/iotests.py | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
tests/qemu-iotests/iotests.py +6 -4
index d667fde6f8..cf1ca60376 100644
@@ -478,11 +478,13 @@ def __init__(self, seconds, errmsg="Timeout"):
         self.seconds = seconds
         self.errmsg = errmsg
     def __enter__(self):
-        signal.signal(signal.SIGALRM, self.timeout)
-        signal.setitimer(signal.ITIMER_REAL, self.seconds)
+        if not qemu_gdb:
+            signal.signal(signal.SIGALRM, self.timeout)
+            signal.setitimer(signal.ITIMER_REAL, self.seconds)
         return self
     def __exit__(self, exc_type, value, traceback):
-        signal.setitimer(signal.ITIMER_REAL, 0)
+        if not qemu_gdb:
+            signal.setitimer(signal.ITIMER_REAL, 0)
         return False
     def timeout(self, signum, frame):
         raise Exception(self.errmsg)
@@ -576,7 +578,7 @@ class VM(qtest.QEMUQtestMachine):
 
     def __init__(self, path_suffix=''):
         name = "qemu%s-%d" % (path_suffix, os.getpid())
-        timer = 15.0
+        timer = 15.0 if not qemu_gdb else None
         super().__init__(qemu_prog, qemu_opts, name=name,
                          test_dir=test_dir,
                          socket_scm_helper=socket_scm_helper,
Signature · 1 line
2.30.2
Reviewed-by on v4 5/15
#21 lore raw

Re: [PATCH v4 05/15] qemu-iotests: delay QMP socket timers

Quoted 19 lines · Emanuele Giuseppe Esposito

Attaching gdbserver implies that the qmp socket
should wait indefinitely for an answer from QEMU.

---
  tests/qemu-iotests/iotests.py | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
tests/qemu-iotests/iotests.py +1 -2
index d667fde6f8..cf1ca60376 100644
@@ -478,11 +478,13 @@ def __init__(self, seconds, errmsg="Timeout"):
          self.seconds = seconds
          self.errmsg = errmsg
      def __enter__(self):
-        signal.signal(signal.SIGALRM, self.timeout)
-        signal.setitimer(signal.ITIMER_REAL, self.seconds)
+        if not qemu_gdb:

tests/qemu-iotests/iotests.py def __init__(self, seconds, errmsg="Timeout"): · line 490

Quoted 10 lines
tests/qemu-iotests/iotests.py +4 -1
+            signal.signal(signal.SIGALRM, self.timeout)
+            signal.setitimer(signal.ITIMER_REAL, self.seconds)
          return self
      def __exit__(self, exc_type, value, traceback):
-        signal.setitimer(signal.ITIMER_REAL, 0)
+        if not qemu_gdb:
+            signal.setitimer(signal.ITIMER_REAL, 0)
          return False
      def timeout(self, signum, frame):
          raise Exception(self.errmsg)

So, you just make the class do nothing.. I'd prefer something like this:

@contextmanager
def NoTimeout:
yield

if qemu_gdb:
Timeout = NoTimeout

anyway:

tests/qemu-iotests/iotests.py class VM(qtest.QEMUQtestMachine): · line 584

Quoted 10 lines
tests/qemu-iotests/iotests.py +1 -1
@@ -576,7 +578,7 @@ class VM(qtest.QEMUQtestMachine):
 
      def __init__(self, path_suffix=''):
          name = "qemu%s-%d" % (path_suffix, os.getpid())
-        timer = 15.0
+        timer = 15.0 if not qemu_gdb else None
          super().__init__(qemu_prog, qemu_opts, name=name,
                           test_dir=test_dir,
                           socket_scm_helper=socket_scm_helper,

Still, it's not simple to avoid any kind of timeouts. The most annoying would be timeouts in event_wait() / events_wait()

Signature · 2 lines
Best regards,
Vladimir
#22 lore raw

Re: [PATCH v4 05/15] qemu-iotests: delay QMP socket timers

Quoted 10 lines

So, you just make the class do nothing.. I'd prefer something like this:

@contextmanager
def NoTimeout:
   yield

if qemu_gdb:
  Timeout = NoTimeout

I am not sure I understand what you want to do here.
I have a basic understanding of @contextmanager, but can you explain more what you want to do?

Alternatively, I send the patch as-is, and then you can send this change.

Thank you,
Emanuele

Quoted 6 lines

anyway:

#23 lore raw

Re: [PATCH v4 05/15] qemu-iotests: delay QMP socket timers

Quoted 8 lines · Emanuele Giuseppe Esposito
Quoted 8 lines

So, you just make the class do nothing.. I'd prefer something like this:

@contextmanager
def NoTimeout:
    yield

Quoted 5 lines
Quoted 2 lines

if qemu_gdb:
   Timeout = NoTimeout

I am not sure I understand what you want to do here.
I have a basic understanding of @contextmanager, but can you explain more what you want to do?

With qemu_gdb you make the class do absolutely nothing. So, it's just provides an interface of context manager, but does nothing.

Quoted 3 lines
Quoted 3 lines

@contextmanager
def NoTimeout:
yield

is the same thing: it's context manager, so you can do

with NoTimeout():
....

but that context manager does absolutely nothing.

@contextmanager gives you a simple way to create a context manager. You define a function which has yield point. So, everything before yield is __enter__, everything after yield is __exit__. And you can return a value by yield, it will be a return value of __enter__.

So, what I meant: keep Timeout class as is and just add code that I suggested after it.

Quoted 2 lines

Alternatively, I send the patch as-is, and then you can send this change.

OK for me.

Quoted 11 lines

Thank you,
Emanuele

Quoted 6 lines

anyway:

Signature · 2 lines
Best regards,
Vladimir
#24 lore raw

Re: [PATCH v4 05/15] qemu-iotests: delay QMP socket timers

Quoted 26 lines · Vladimir Sementsov-Ogievskiy
Quoted 8 lines · Emanuele Giuseppe Esposito
Quoted 8 lines

So, you just make the class do nothing.. I'd prefer something like this:

@contextmanager
def NoTimeout:
    yield

Quoted 5 lines
Quoted 2 lines

if qemu_gdb:
   Timeout = NoTimeout

I am not sure I understand what you want to do here.
I have a basic understanding of @contextmanager, but can you explain more what you want to do?

With qemu_gdb you make the class do absolutely nothing. So, it's just provides an interface of context manager, but does nothing.

Quoted 3 lines
Quoted 3 lines

@contextmanager
def NoTimeout:
    yield

is the same thing: it's context manager, so you can do

with NoTimeout():
  ....

Quoted 8 lines

but that context manager does absolutely nothing.

@contextmanager gives you a simple way to create a context manager. You
define a function which has yield point. So, everything before yield is __enter__, everything after yield is __exit__. And you can return a value by yield, it will be a return value of __enter__.

So, what I meant: keep Timeout class as is and just add code that I suggested after it.

Oh okay. I didn't know Timeout = NoTimeout was possible (and I didn't know where to put it anyways). Looks cleaner than the normal ifs, will apply this change and send v5.

Thank you,
Emanuele

#25 lore raw

PATCH v4 6/15 qemu_iotests: insert gdbserver command line as wrapper for qemu binary

In reply to Emanuele Giuseppe Esposito
---
 tests/qemu-iotests/iotests.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tests/qemu-iotests/iotests.py +2 -1
index cf1ca60376..c9628e6828 100644
@@ -579,7 +579,8 @@ class VM(qtest.QEMUQtestMachine):
     def __init__(self, path_suffix=''):
         name = "qemu%s-%d" % (path_suffix, os.getpid())
         timer = 15.0 if not qemu_gdb else None
-        super().__init__(qemu_prog, qemu_opts, name=name,
+        super().__init__(qemu_prog, qemu_opts, wrapper=qemu_gdb,
+                         name=name,
                          test_dir=test_dir,
                          socket_scm_helper=socket_scm_helper,
                          sock_dir=sock_dir, qmp_timer=timer)
Signature · 1 line
2.30.2
#27 lore raw

PATCH v4 7/15 qemu-iotests: add gdbserver option to script tests too

In reply to Emanuele Giuseppe Esposito

The only limitation here is that running a script with gdbserver will make the test output mismatch with the expected results, making the test fail.

---
 tests/qemu-iotests/common.rc | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
tests/qemu-iotests/common.rc +7 -1
index 7f49c9716d..f1d5395ff2 100644
@@ -166,8 +166,14 @@ _qemu_wrapper()
         if [ -n "${QEMU_NEED_PID}" ]; then
             echo $BASHPID > "${QEMU_TEST_DIR}/qemu-${_QEMU_HANDLE}.pid"
         fi
+
+        GDB=""
+        if [ ! -z ${GDB_OPTIONS} ]; then
+            GDB="gdbserver ${GDB_OPTIONS}"
+        fi
+
         VALGRIND_QEMU="${VALGRIND_QEMU_VM}" _qemu_proc_exec "${VALGRIND_LOGFILE}" \
-            "$QEMU_PROG" $QEMU_OPTIONS "$@"
+            $GDB "$QEMU_PROG" $QEMU_OPTIONS "$@"
     )
     RETVAL=$?
     _qemu_proc_valgrind_log "${VALGRIND_LOGFILE}" $RETVAL
Signature · 1 line
2.30.2
Reviewed-by on v4 7/15
#28 lore raw

Re: [PATCH v4 07/15] qemu-iotests: add gdbserver option to script tests too

Quoted 5 lines · Emanuele Giuseppe Esposito

The only limitation here is that running a script with gdbserver will make the test output mismatch with the expected results, making the test fail.

Signature · 2 lines
Best regards,
Vladimir
#29 lore raw

PATCH v4 8/15 docs/devel/testing: add -gdb option to the debugging section of QEMU iotests

In reply to Emanuele Giuseppe Esposito
---
 docs/devel/testing.rst | 11 +++++++++++
 1 file changed, 11 insertions(+)
docs/devel/testing.rst +11 -0
index 8144e316a4..a746cab745 100644
@@ -229,6 +229,17 @@ Debugging a test case
 The following options to the ``check`` script can be useful when debugging
 a failing test:
 
+* ``-gdb`` wraps every QEMU invocation in a ``gdbserver``, which waits for a
+  connection from a gdb client.  The options given to ``gdbserver`` (e.g. the
+  address on which to listen for connections) are taken from the ``$GDB_OPTIONS``
+  environment variable.  By default (if ``$GDB_OPTIONS`` is empty), it listens on
+  ``localhost:12345``.
+  It is possible to connect to it for example with
+  ``gdb -iex "target remote $addr"``, where ``$addr`` is the address
+  ``gdbserver`` listens on.
+  If the ``-gdb`` option is not used, ``$GDB_OPTIONS`` is ignored,
+  regardless on whether it is set or not.
+
 * ``-d`` (debug) just increases the logging verbosity, showing
   for example the QMP commands and answers.
 
Signature · 1 line
2.30.2
Reviewed-by on v4 8/15
#30 lore raw

Re: [PATCH v4 08/15] docs/devel/testing: add -gdb option to the debugging section of QEMU iotests

Quoted 1 line · Emanuele Giuseppe Esposito
Quoted 17 lines
---
  docs/devel/testing.rst | 11 +++++++++++
  1 file changed, 11 insertions(+)
docs/devel/testing.rst +4 -0
index 8144e316a4..a746cab745 100644
@@ -229,6 +229,17 @@ Debugging a test case
  The following options to the ``check`` script can be useful when debugging
  a failing test:
 
+* ``-gdb`` wraps every QEMU invocation in a ``gdbserver``, which waits for a
+  connection from a gdb client.  The options given to ``gdbserver`` (e.g. the
+  address on which to listen for connections) are taken from the ``$GDB_OPTIONS``
+  environment variable.  By default (if ``$GDB_OPTIONS`` is empty), it listens on

docs/devel/testing.rst Debugging a test case · line 245

Quoted 10 lines
docs/devel/testing.rst +7 -0
+  ``localhost:12345``.
+  It is possible to connect to it for example with
+  ``gdb -iex "target remote $addr"``, where ``$addr`` is the address
+  ``gdbserver`` listens on.
+  If the ``-gdb`` option is not used, ``$GDB_OPTIONS`` is ignored,
+  regardless on whether it is set or not.
+
  * ``-d`` (debug) just increases the logging verbosity, showing
    for example the QMP commands and answers.
 

Didn't you think of an interface as simple as just wrap qemu binary by "gdb --args" and redirect stdin and stdout directly to the terminal where test is running? Wouldn't it more convenient? So, you just start ./check -gdb <test>, and you are in gdb. Or you need exactly gdb server, to attach from another machine?

Keeping this possibility in mind, it's probably better to call you option "-gdbserver"... But we can rename later if needed, finally, it's only a test framework.

Signature · 2 lines
Best regards,
Vladimir
#31 lore raw

Re: [PATCH v4 08/15] docs/devel/testing: add -gdb option to the debugging section of QEMU iotests

Quoted 37 lines · Vladimir Sementsov-Ogievskiy
Quoted 1 line · Emanuele Giuseppe Esposito
Quoted 20 lines

---
  docs/devel/testing.rst | 11 +++++++++++

  1 file changed, 11 insertions(+)
docs/devel/testing.rst +0 -0
index 8144e316a4..a746cab745 100644
@@ -229,6 +229,17 @@ Debugging a test case

  The following options to the ``check`` script can be useful when debugging a failing test:
+* ``-gdb`` wraps every QEMU invocation in a ``gdbserver``, which waits for a
+  connection from a gdb client.  The options given to ``gdbserver`` (e.g. the
+  address on which to listen for connections) are taken from the ``$GDB_OPTIONS``
+  environment variable.  By default (if ``$GDB_OPTIONS`` is empty), it listens on
+  ``localhost:12345``.

Quoted 9 lines

+  It is possible to connect to it for example with
+  ``gdb -iex "target remote $addr"``, where ``$addr`` is the address +  ``gdbserver`` listens on.
+  If the ``-gdb`` option is not used, ``$GDB_OPTIONS`` is ignored, +  regardless on whether it is set or not.
+
  * ``-d`` (debug) just increases the logging verbosity, showing for example the QMP commands and answers.

Didn't you think of an interface as simple as just wrap qemu binary by "gdb --args" and redirect stdin and stdout directly to the terminal where test is running? Wouldn't it more convenient? So, you just start ./check -gdb <test>, and you are in gdb. Or you need exactly gdb server, to attach from another machine?

Quoted 3 lines

Keeping this possibility in mind, it's probably better to call you option "-gdbserver"... But we can rename later if needed, finally, it's only a test framework.

See https://patchew.org/QEMU/[email protected]/[email protected]/

(penultimate email), there was a similar question:

Quoted 8 lines
Quoted 2 lines

Out of interest: Why gdbserver and not “just” gdb? On Fedora, those are separate packages, so I don’t have gdbserver installed, that’s why I’m asking.

As far as I have tried, using only gdb with ./check is very hard to use, because the stdout is filtered out by the script.
So invoking gdb attaches it to QEMU, but it is not possible to start execution (run command) or interact with it, because of the python script filtering. This leaves the test hanging.

gdbserver is just something that a gdb client can attach to (for example, in another console or even in another host) for example with the command

# gdb -iex "target remote localhost:12345" . This provides a nice and separate gdb monitor to the client.

At this point I should put this somewhere, either in commit message or in the actual documentation...

I don't know about the name. "gdb" should also be short for "gdbserver" in a way.

Thank you,
Emanuele

#32 lore raw

PATCH v4 9/15 qemu-iotests: extend the check script to support valgrind for python tests

In reply to Emanuele Giuseppe Esposito

Currently, the check script only parses the option and sets the VALGRIND_QEMU environmental variable to "y".
Add another local python variable that prepares the command line, identical to the one provided in the test scripts.

Because the python script does not know in advance the valgring PID to assign to the log file name, use the "%p" flag in valgrind log file name that automatically puts the process PID at runtime.

---
 tests/qemu-iotests/check      |  7 ++++---
 tests/qemu-iotests/iotests.py | 11 +++++++++++
 tests/qemu-iotests/testenv.py |  1 +
 3 files changed, 16 insertions(+), 3 deletions(-)
tests/qemu-iotests/check +4 -3
index b9820fdaaf..2101cedfe3 100755
@@ -36,6 +36,10 @@ def make_argparser() -> argparse.ArgumentParser:
     p.add_argument('-gdb', action='store_true',
                    help="start gdbserver with $GDB_OPTIONS options \
                         ('localhost:12345' if $GDB_OPTIONS is empty)")
+    p.add_argument('-valgrind', action='store_true',
+                    help='use valgrind, sets VALGRIND_QEMU environment '
+                    'variable')
+
     p.add_argument('-misalign', action='store_true',
                    help='misalign memory allocations')
     p.add_argument('--color', choices=['on', 'off', 'auto'],
@@ -86,9 +90,6 @@ def make_argparser() -> argparse.ArgumentParser:
     g_bash.add_argument('-o', dest='imgopts',
                         help='options to pass to qemu-img create/convert, '
                         'sets IMGOPTS environment variable')
-    g_bash.add_argument('-valgrind', action='store_true',
-                        help='use valgrind, sets VALGRIND_QEMU environment '
-                        'variable')
 
     g_sel = p.add_argument_group('test selecting options',
                                  'The following options specify test set '
tests/qemu-iotests/iotests.py +11 -0
index c9628e6828..41462a80fc 100644
@@ -97,6 +97,17 @@
     sys.stderr.write('Please run this test via the "check" script\n')
     sys.exit(os.EX_USAGE)
 
+qemu_valgrind = []
+if os.environ.get('VALGRIND_QEMU') == "y" and \
+    os.environ.get('NO_VALGRIND') != "y":
+    valgrind_logfile = "--log-file=" + test_dir.strip()
+    # %p allows to put the valgrind process PID, since
+    # we don't know it a priori (subprocess.Popen is
+    # not yet invoked)
+    valgrind_logfile += "/%p.valgrind"
+
+    qemu_valgrind = ['valgrind', valgrind_logfile, '--error-exitcode=99']
+
 socket_scm_helper = os.environ.get('SOCKET_SCM_HELPER', 'socket_scm_helper')
 
 luks_default_secret_object = 'secret,id=keysec0,data=' + \
tests/qemu-iotests/testenv.py +1 -0
index 49ddd586ef..319d29cb0c 100644
@@ -282,6 +282,7 @@ def print_env(self) -> None:
 SOCK_DIR      -- {SOCK_DIR}
 SOCKET_SCM_HELPER -- {SOCKET_SCM_HELPER}
 GDB_OPTIONS   -- {GDB_OPTIONS}
+VALGRIND_QEMU -- {VALGRIND_QEMU}
 """
 
         args = collections.defaultdict(str, self.get_env())
Signature · 1 line
2.30.2
#33 lore raw

Re: [PATCH v4 09/15] qemu-iotests: extend the check script to support valgrind for python tests

Quoted 47 lines · Emanuele Giuseppe Esposito

Currently, the check script only parses the option and sets the VALGRIND_QEMU environmental variable to "y".
Add another local python variable that prepares the command line, identical to the one provided in the test scripts.

Because the python script does not know in advance the valgring PID to assign to the log file name, use the "%p" flag in valgrind log file name that automatically puts the process PID at runtime.

---
  tests/qemu-iotests/check      |  7 ++++---
  tests/qemu-iotests/iotests.py | 11 +++++++++++
  tests/qemu-iotests/testenv.py |  1 +
  3 files changed, 16 insertions(+), 3 deletions(-)
tests/qemu-iotests/check +4 -3
index b9820fdaaf..2101cedfe3 100755
@@ -36,6 +36,10 @@ def make_argparser() -> argparse.ArgumentParser:
      p.add_argument('-gdb', action='store_true',
                     help="start gdbserver with $GDB_OPTIONS options \
                          ('localhost:12345' if $GDB_OPTIONS is empty)")
+    p.add_argument('-valgrind', action='store_true',
+                    help='use valgrind, sets VALGRIND_QEMU environment '
+                    'variable')
+
      p.add_argument('-misalign', action='store_true',
                     help='misalign memory allocations')
      p.add_argument('--color', choices=['on', 'off', 'auto'],
@@ -86,9 +90,6 @@ def make_argparser() -> argparse.ArgumentParser:
      g_bash.add_argument('-o', dest='imgopts',
                          help='options to pass to qemu-img create/convert, '
                          'sets IMGOPTS environment variable')
-    g_bash.add_argument('-valgrind', action='store_true',
-                        help='use valgrind, sets VALGRIND_QEMU environment '
-                        'variable')
 
      g_sel = p.add_argument_group('test selecting options',
                                   'The following options specify test set '
tests/qemu-iotests/iotests.py +0 -0
index c9628e6828..41462a80fc 100644

tests/qemu-iotests/iotests.py line 103

Quoted 7 lines
tests/qemu-iotests/iotests.py +4 -0
@@ -97,6 +97,17 @@
      sys.stderr.write('Please run this test via the "check" script\n')
      sys.exit(os.EX_USAGE)
 
+qemu_valgrind = []
+if os.environ.get('VALGRIND_QEMU') == "y" and \
+    os.environ.get('NO_VALGRIND') != "y":
+    valgrind_logfile = "--log-file=" + test_dir.strip()

a bit strange that you need to strip test_dir here.. Why?

Quoted 16 lines
tests/qemu-iotests/iotests.py +7 -0
+    # %p allows to put the valgrind process PID, since
+    # we don't know it a priori (subprocess.Popen is
+    # not yet invoked)
+    valgrind_logfile += "/%p.valgrind"
+
+    qemu_valgrind = ['valgrind', valgrind_logfile, '--error-exitcode=99']
+
  socket_scm_helper = os.environ.get('SOCKET_SCM_HELPER', 'socket_scm_helper')
 
  luks_default_secret_object = 'secret,id=keysec0,data=' + \
tests/qemu-iotests/testenv.py +0 -0
index 49ddd586ef..319d29cb0c 100644

tests/qemu-iotests/testenv.py def print_env(self) -> None: · line 288

Quoted 7 lines
tests/qemu-iotests/testenv.py +1 -0
@@ -282,6 +282,7 @@ def print_env(self) -> None:
  SOCK_DIR      -- {SOCK_DIR}
  SOCKET_SCM_HELPER -- {SOCKET_SCM_HELPER}
  GDB_OPTIONS   -- {GDB_OPTIONS}
+VALGRIND_QEMU -- {VALGRIND_QEMU}
  """
 
          args = collections.defaultdict(str, self.get_env())

commit subject sais "support valgrind", but actually we only add a variable. valgrind is not actually used yet.. I'd reflect it in commit subject somehow

Signature · 2 lines
Best regards,
Vladimir
#34 lore raw

Re: [PATCH v4 09/15] qemu-iotests: extend the check script to support valgrind for python tests

Quoted 9 lines

tests/qemu-iotests/iotests.py line 103

Quoted 6 lines

      sys.exit(os.EX_USAGE)

tests/qemu-iotests/iotests.py +4 -0
+qemu_valgrind = []
+if os.environ.get('VALGRIND_QEMU') == "y" and \
+    os.environ.get('NO_VALGRIND') != "y":
+    valgrind_logfile = "--log-file=" + test_dir.strip()

a bit strange that you need to strip test_dir here.. Why?

Yep, it's unnecessary

Quoted 7 lines
Quoted 4 lines

+VALGRIND_QEMU -- {VALGRIND_QEMU}
  """
          args = collections.defaultdict(str, self.get_env())

commit subject sais "support valgrind", but actually we only add a variable. valgrind is not actually used yet.. I'd reflect it in commit subject somehow

will replace with "prepare supporting"

Emanuele

#35 lore raw

PATCH v4 10/15 qemu-iotests: extent QMP socket timeout when using valgrind

In reply to Emanuele Giuseppe Esposito

As with gdbserver, valgrind delays the test execution, so the default QMP socket timeout timeout too soon.

---
 tests/qemu-iotests/iotests.py | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tests/qemu-iotests/iotests.py +3 -3
index 41462a80fc..5d75094ba6 100644
@@ -489,12 +489,12 @@ def __init__(self, seconds, errmsg="Timeout"):
         self.seconds = seconds
         self.errmsg = errmsg
     def __enter__(self):
-        if not qemu_gdb:
+        if not (qemu_gdb or qemu_valgrind):
             signal.signal(signal.SIGALRM, self.timeout)
             signal.setitimer(signal.ITIMER_REAL, self.seconds)
         return self
     def __exit__(self, exc_type, value, traceback):
-        if not qemu_gdb:
+        if not (qemu_gdb or qemu_valgrind):
             signal.setitimer(signal.ITIMER_REAL, 0)
         return False
     def timeout(self, signum, frame):
@@ -589,7 +589,7 @@ class VM(qtest.QEMUQtestMachine):
 
     def __init__(self, path_suffix=''):
         name = "qemu%s-%d" % (path_suffix, os.getpid())
-        timer = 15.0 if not qemu_gdb else None
+        timer = 15.0 if not (qemu_gdb or qemu_valgrind) else None
         super().__init__(qemu_prog, qemu_opts, wrapper=qemu_gdb,
                          name=name,
                          test_dir=test_dir,
Signature · 1 line
2.30.2
Reviewed-by on v4 10/15
#36 lore raw

Re: [PATCH v4 10/15] qemu-iotests: extent QMP socket timeout when using valgrind

Quoted 2 lines · Emanuele Giuseppe Esposito

As with gdbserver, valgrind delays the test execution, so the default QMP socket timeout timeout too soon.

First, "Timeout" class is a generic class for timeouts, not relying to sockets. So, commit message lacks information about that we modify generic context-provider class and why we do it.

Quoted 12 lines
---
  tests/qemu-iotests/iotests.py | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
tests/qemu-iotests/iotests.py +0 -0
index 41462a80fc..5d75094ba6 100644
@@ -489,12 +489,12 @@ def __init__(self, seconds, errmsg="Timeout"):
          self.seconds = seconds

tests/qemu-iotests/iotests.py def __init__(self, seconds, errmsg="Timeout"): · line 497

Quoted 10 lines
tests/qemu-iotests/iotests.py +2 -2
          self.errmsg = errmsg
      def __enter__(self):
-        if not qemu_gdb:
+        if not (qemu_gdb or qemu_valgrind):
              signal.signal(signal.SIGALRM, self.timeout)
              signal.setitimer(signal.ITIMER_REAL, self.seconds)
          return self
      def __exit__(self, exc_type, value, traceback):
-        if not qemu_gdb:
+        if not (qemu_gdb or qemu_valgrind):

If you follow my suggestion on 05, you'll have to modify only one line instead of two.

tests/qemu-iotests/iotests.py class VM(qtest.QEMUQtestMachine): · line 595

Quoted 13 lines
tests/qemu-iotests/iotests.py +1 -1
              signal.setitimer(signal.ITIMER_REAL, 0)
          return False
      def timeout(self, signum, frame):
@@ -589,7 +589,7 @@ class VM(qtest.QEMUQtestMachine):
 
      def __init__(self, path_suffix=''):
          name = "qemu%s-%d" % (path_suffix, os.getpid())
-        timer = 15.0 if not qemu_gdb else None
+        timer = 15.0 if not (qemu_gdb or qemu_valgrind) else None
          super().__init__(qemu_prog, qemu_opts, wrapper=qemu_gdb,
                           name=name,
                           test_dir=test_dir,

still it should work as intended:

Signature · 2 lines
Best regards,
Vladimir
#37 lore raw

PATCH v4 11/15 qemu-iotests: allow valgrind to read/delete the generated log file

In reply to Emanuele Giuseppe Esposito

When using -valgrind on the script tests, it generates a log file in $TEST_DIR that is either read (if valgrind finds problems) or otherwise deleted. Provide the same exact behavior when using -valgrind on the python tests.

---
 tests/qemu-iotests/iotests.py | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
tests/qemu-iotests/iotests.py +16 -0
index 5d75094ba6..a06284acad 100644
@@ -597,6 +597,22 @@ def __init__(self, path_suffix=''):
                          sock_dir=sock_dir, qmp_timer=timer)
         self._num_drives = 0
 
+    def subprocess_check_valgrind(self, valgrind: List[str]) -> None:
+        if not valgrind or not self._popen:
+            return
+
+        valgrind_filename =  f"{test_dir}/{self._popen.pid}.valgrind"
+
+        if self.exitcode() == 99:
+            with open(valgrind_filename) as f:
+                print(f.read())
+        else:
+            os.remove(valgrind_filename)
+
+    def _post_shutdown(self) -> None:
+        super()._post_shutdown()
+        self.subprocess_check_valgrind(qemu_valgrind)
+
     def add_object(self, opts):
         self._args.append('-object')
         self._args.append(opts)
Signature · 1 line
2.30.2
Reviewed-by on v4 11/15
#38 lore raw

Re: [PATCH v4 11/15] qemu-iotests: allow valgrind to read/delete the generated log file

Quoted 15 lines · Emanuele Giuseppe Esposito

When using -valgrind on the script tests, it generates a log file in $TEST_DIR that is either read (if valgrind finds problems) or otherwise deleted. Provide the same exact behavior when using -valgrind on the python tests.

---
  tests/qemu-iotests/iotests.py | 16 ++++++++++++++++
  1 file changed, 16 insertions(+)
tests/qemu-iotests/iotests.py +0 -0
index 5d75094ba6..a06284acad 100644

tests/qemu-iotests/iotests.py def __init__(self, path_suffix=''): · line 600

Quoted 4 lines
tests/qemu-iotests/iotests.py +1 -0
@@ -597,6 +597,22 @@ def __init__(self, path_suffix=''):
                           sock_dir=sock_dir, qmp_timer=timer)
          self._num_drives = 0
 
+    def subprocess_check_valgrind(self, valgrind: List[str]) -> None:

For me just "check_valgrind" would be more intuitive.

I think, you also can use qemu_valgrind global variable directly.. What is the reason for passing it as parameter?

tests/qemu-iotests/iotests.py def __init__(self, path_suffix=''): · line 604

Quoted 4 lines
tests/qemu-iotests/iotests.py +4 -0
+        if not valgrind or not self._popen:
+            return
+
+        valgrind_filename =  f"{test_dir}/{self._popen.pid}.valgrind"

should we use os.path.join instead? I don't know.. And don't care, as I don't use windows anyway. Still os.path.join is used everywhere in the file except for has_working_luks() function.. So, you are going add another exception.

Quoted 5 lines
tests/qemu-iotests/iotests.py +4 -0
+
+        if self.exitcode() == 99:
+            with open(valgrind_filename) as f:
+                print(f.read())

tests/qemu-iotests/iotests.py def __init__(self, path_suffix=''): · line 618

Quoted 10 lines
tests/qemu-iotests/iotests.py +7 -0
+        else:
+            os.remove(valgrind_filename)
+
+    def _post_shutdown(self) -> None:
+        super()._post_shutdown()
+        self.subprocess_check_valgrind(qemu_valgrind)
+
      def add_object(self, opts):
          self._args.append('-object')
          self._args.append(opts)
Signature · 2 lines
Best regards,
Vladimir
#39 lore raw

PATCH v4 12/15 qemu-iotests: insert valgrind command line as wrapper for qemu binary

In reply to Emanuele Giuseppe Esposito

The priority will be given to gdb command line, meaning if the -gdb parameter and -valgrind are given, gdb will be wrapped around the qemu binary.

---
 tests/qemu-iotests/iotests.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tests/qemu-iotests/iotests.py +2 -1
index a06284acad..75f1e1711c 100644
@@ -590,7 +590,8 @@ class VM(qtest.QEMUQtestMachine):
     def __init__(self, path_suffix=''):
         name = "qemu%s-%d" % (path_suffix, os.getpid())
         timer = 15.0 if not (qemu_gdb or qemu_valgrind) else None
-        super().__init__(qemu_prog, qemu_opts, wrapper=qemu_gdb,
+        wrapper = qemu_gdb if qemu_gdb else qemu_valgrind
+        super().__init__(qemu_prog, qemu_opts, wrapper=wrapper,
                          name=name,
                          test_dir=test_dir,
                          socket_scm_helper=socket_scm_helper,
Signature · 1 line
2.30.2
Reviewed-by on v4 12/15
#40 lore raw

Re: [PATCH v4 12/15] qemu-iotests: insert valgrind command line as wrapper for qemu binary

Quoted 3 lines · Emanuele Giuseppe Esposito

The priority will be given to gdb command line, meaning if the -gdb parameter and -valgrind are given, gdb will be wrapped around the qemu binary.

I'd prefer just return an error immediately if user specify both -gdb and -valgrind

Quoted 12 lines
---
  tests/qemu-iotests/iotests.py | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
tests/qemu-iotests/iotests.py +0 -0
index a06284acad..75f1e1711c 100644

tests/qemu-iotests/iotests.py class VM(qtest.QEMUQtestMachine): · line 597

Quoted 9 lines
tests/qemu-iotests/iotests.py +2 -1
@@ -590,7 +590,8 @@ class VM(qtest.QEMUQtestMachine):
      def __init__(self, path_suffix=''):
          name = "qemu%s-%d" % (path_suffix, os.getpid())
          timer = 15.0 if not (qemu_gdb or qemu_valgrind) else None
-        super().__init__(qemu_prog, qemu_opts, wrapper=qemu_gdb,
+        wrapper = qemu_gdb if qemu_gdb else qemu_valgrind
+        super().__init__(qemu_prog, qemu_opts, wrapper=wrapper,
                           name=name,
                           test_dir=test_dir,
                           socket_scm_helper=socket_scm_helper,
Signature · 2 lines
Best regards,
Vladimir
#41 lore raw

PATCH v4 13/15 docs/devel/testing: add -valgrind option to the debug section of QEMU iotests

In reply to Emanuele Giuseppe Esposito
---
 docs/devel/testing.rst | 7 +++++++
 1 file changed, 7 insertions(+)
docs/devel/testing.rst +7 -0
index a746cab745..d743e88746 100644
@@ -240,6 +240,13 @@ a failing test:
   If the ``-gdb`` option is not used, ``$GDB_OPTIONS`` is ignored,
   regardless on whether it is set or not.
 
+* ``-valgrind`` attaches a valgrind instance to QEMU. If it detects
+  warnings, it will print and save the log in
+  ``$TEST_DIR/<valgrind_pid>.valgrind``.
+  The final command line will be ``valgrind --log-file=$TEST_DIR/
+  <valgrind_pid>.valgrind --error-exitcode=99 $QEMU ...``
+  Note: if used together with ``-gdb``, this command will be ignored.
+
 * ``-d`` (debug) just increases the logging verbosity, showing
   for example the QMP commands and answers.
 
Signature · 1 line
2.30.2
#43 lore raw

PATCH v4 14/15 qemu-iotests: add option to show qemu binary logs on stdout

In reply to Emanuele Giuseppe Esposito

Using the flag -p, allow the qemu binary to print to stdout.

---
 tests/qemu-iotests/check      | 4 +++-
 tests/qemu-iotests/iotests.py | 9 +++++++++
 tests/qemu-iotests/testenv.py | 9 +++++++--
 3 files changed, 19 insertions(+), 3 deletions(-)
tests/qemu-iotests/check +3 -1
index 2101cedfe3..51b90681ab 100755
@@ -33,6 +33,8 @@ def make_argparser() -> argparse.ArgumentParser:
                    help='pretty print output for make check')
 
     p.add_argument('-d', dest='debug', action='store_true', help='debug')
+    p.add_argument('-p', dest='print', action='store_true',
+                help='redirects qemu\'s stdout and stderr to the test output')
     p.add_argument('-gdb', action='store_true',
                    help="start gdbserver with $GDB_OPTIONS options \
                         ('localhost:12345' if $GDB_OPTIONS is empty)")
@@ -117,7 +119,7 @@ if __name__ == '__main__':
                   aiomode=args.aiomode, cachemode=args.cachemode,
                   imgopts=args.imgopts, misalign=args.misalign,
                   debug=args.debug, valgrind=args.valgrind,
-                  gdb=args.gdb)
+                  gdb=args.gdb, qprint=args.print)
 
     testfinder = TestFinder(test_dir=env.source_iotests)
 
tests/qemu-iotests/iotests.py +9 -0
index 75f1e1711c..53a3916a91 100644
@@ -80,6 +80,8 @@
 if gdb_qemu_env:
     qemu_gdb = ['gdbserver'] + gdb_qemu_env.strip().split(' ')
 
+qemu_print = os.environ.get('PRINT_QEMU', False)
+
 imgfmt = os.environ.get('IMGFMT', 'raw')
 imgproto = os.environ.get('IMGPROTO', 'file')
 output_dir = os.environ.get('OUTPUT_DIR', '.')
@@ -614,6 +616,13 @@ def _post_shutdown(self) -> None:
         super()._post_shutdown()
         self.subprocess_check_valgrind(qemu_valgrind)
 
+    def _pre_launch(self) -> None:
+        super()._pre_launch()
+        if qemu_print and self._qemu_log_file is not None:
+            # set QEMU binary output to stdout
+            self._qemu_log_file.close()
+            self._qemu_log_file = None
+
     def add_object(self, opts):
         self._args.append('-object')
         self._args.append(opts)
tests/qemu-iotests/testenv.py +7 -2
index 319d29cb0c..b79ce22fe9 100644
@@ -74,7 +74,7 @@ class TestEnv(ContextManager['TestEnv']):
                      'AIOMODE', 'CACHEMODE', 'VALGRIND_QEMU',
                      'CACHEMODE_IS_DEFAULT', 'IMGFMT_GENERIC', 'IMGOPTSSYNTAX',
                      'IMGKEYSECRET', 'QEMU_DEFAULT_MACHINE', 'MALLOC_PERTURB_',
-                     'GDB_OPTIONS']
+                     'GDB_OPTIONS', 'PRINT_QEMU']
 
     def get_env(self) -> Dict[str, str]:
         env = {}
@@ -166,7 +166,8 @@ def __init__(self, imgfmt: str, imgproto: str, aiomode: str,
                  misalign: bool = False,
                  debug: bool = False,
                  valgrind: bool = False,
-                 gdb: bool = False) -> None:
+                 gdb: bool = False,
+                 qprint: bool = False) -> None:
         self.imgfmt = imgfmt
         self.imgproto = imgproto
         self.aiomode = aiomode
@@ -174,6 +175,9 @@ def __init__(self, imgfmt: str, imgproto: str, aiomode: str,
         self.misalign = misalign
         self.debug = debug
 
+        if qprint:
+            self.print_qemu = 'y'
+
         if gdb:
             self.gdb_options = os.environ.get('GDB_OPTIONS', DEF_GDB_OPTIONS)
             if not self.gdb_options:
@@ -283,6 +287,7 @@ def print_env(self) -> None:
 SOCKET_SCM_HELPER -- {SOCKET_SCM_HELPER}
 GDB_OPTIONS   -- {GDB_OPTIONS}
 VALGRIND_QEMU -- {VALGRIND_QEMU}
+PRINT_QEMU_OUTPUT -- {PRINT_QEMU}
 """
 
         args = collections.defaultdict(str, self.get_env())
Signature · 1 line
2.30.2
Reviewed-by on v4 14/15
#44 lore raw

Re: [PATCH v4 14/15] qemu-iotests: add option to show qemu binary logs on stdout

Quoted 46 lines · Emanuele Giuseppe Esposito

Using the flag -p, allow the qemu binary to print to stdout.

---
  tests/qemu-iotests/check      | 4 +++-
  tests/qemu-iotests/iotests.py | 9 +++++++++
  tests/qemu-iotests/testenv.py | 9 +++++++--
  3 files changed, 19 insertions(+), 3 deletions(-)
tests/qemu-iotests/check +3 -1
index 2101cedfe3..51b90681ab 100755
@@ -33,6 +33,8 @@ def make_argparser() -> argparse.ArgumentParser:
                     help='pretty print output for make check')
 
      p.add_argument('-d', dest='debug', action='store_true', help='debug')
+    p.add_argument('-p', dest='print', action='store_true',
+                help='redirects qemu\'s stdout and stderr to the test output')
      p.add_argument('-gdb', action='store_true',
                     help="start gdbserver with $GDB_OPTIONS options \
                          ('localhost:12345' if $GDB_OPTIONS is empty)")
@@ -117,7 +119,7 @@ if __name__ == '__main__':
                    aiomode=args.aiomode, cachemode=args.cachemode,
                    imgopts=args.imgopts, misalign=args.misalign,
                    debug=args.debug, valgrind=args.valgrind,
-                  gdb=args.gdb)
+                  gdb=args.gdb, qprint=args.print)
 
      testfinder = TestFinder(test_dir=env.source_iotests)
 
tests/qemu-iotests/iotests.py +2 -0
index 75f1e1711c..53a3916a91 100644
@@ -80,6 +80,8 @@
  if gdb_qemu_env:
      qemu_gdb = ['gdbserver'] + gdb_qemu_env.strip().split(' ')
 
+qemu_print = os.environ.get('PRINT_QEMU', False)
+
  imgfmt = os.environ.get('IMGFMT', 'raw')
  imgproto = os.environ.get('IMGPROTO', 'file')
  output_dir = os.environ.get('OUTPUT_DIR', '.')

tests/qemu-iotests/iotests.py def _post_shutdown(self) -> None: · line 625

Quoted 10 lines
tests/qemu-iotests/iotests.py +7 -0
@@ -614,6 +616,13 @@ def _post_shutdown(self) -> None:
          super()._post_shutdown()
          self.subprocess_check_valgrind(qemu_valgrind)
 
+    def _pre_launch(self) -> None:
+        super()._pre_launch()
+        if qemu_print and self._qemu_log_file is not None:
+            # set QEMU binary output to stdout
+            self._qemu_log_file.close()
+            self._qemu_log_file = None
+

So, many use of _private members actually show that proper way of doing this is adding an option to __init__ instead

Interesting will pylint complain on using _private members outside of the home class?

Quoted 38 lines
tests/qemu-iotests/iotests.py +0 -0
      def add_object(self, opts):
          self._args.append('-object')
          self._args.append(opts)
tests/qemu-iotests/testenv.py +6 -2
index 319d29cb0c..b79ce22fe9 100644
@@ -74,7 +74,7 @@ class TestEnv(ContextManager['TestEnv']):
                       'AIOMODE', 'CACHEMODE', 'VALGRIND_QEMU',
                       'CACHEMODE_IS_DEFAULT', 'IMGFMT_GENERIC', 'IMGOPTSSYNTAX',
                       'IMGKEYSECRET', 'QEMU_DEFAULT_MACHINE', 'MALLOC_PERTURB_',
-                     'GDB_OPTIONS']
+                     'GDB_OPTIONS', 'PRINT_QEMU']
 
      def get_env(self) -> Dict[str, str]:
          env = {}
@@ -166,7 +166,8 @@ def __init__(self, imgfmt: str, imgproto: str, aiomode: str,
                   misalign: bool = False,
                   debug: bool = False,
                   valgrind: bool = False,
-                 gdb: bool = False) -> None:
+                 gdb: bool = False,
+                 qprint: bool = False) -> None:
          self.imgfmt = imgfmt
          self.imgproto = imgproto
          self.aiomode = aiomode
@@ -174,6 +175,9 @@ def __init__(self, imgfmt: str, imgproto: str, aiomode: str,
          self.misalign = misalign
          self.debug = debug
 
+        if qprint:
+            self.print_qemu = 'y'
+
          if gdb:
              self.gdb_options = os.environ.get('GDB_OPTIONS', DEF_GDB_OPTIONS)
              if not self.gdb_options:

tests/qemu-iotests/testenv.py def print_env(self) -> None: · line 293

Quoted 7 lines
tests/qemu-iotests/testenv.py +1 -0
@@ -283,6 +287,7 @@ def print_env(self) -> None:
  SOCKET_SCM_HELPER -- {SOCKET_SCM_HELPER}
  GDB_OPTIONS   -- {GDB_OPTIONS}
  VALGRIND_QEMU -- {VALGRIND_QEMU}
+PRINT_QEMU_OUTPUT -- {PRINT_QEMU}
  """
 
          args = collections.defaultdict(str, self.get_env())

5 places we need to modify to add a new option. That's not very good :( (not about your patch).

And I still doubt, that we want to add any new variable to print_env. If we do, than it's simpler to print all variables here, than, one place less to modify by hand.

Signature · 2 lines
Best regards,
Vladimir
#45 lore raw

Re: [PATCH v4 14/15] qemu-iotests: add option to show qemu binary logs on stdout

Quoted 17 lines
Quoted 6 lines

+
  imgfmt = os.environ.get('IMGFMT', 'raw')
  imgproto = os.environ.get('IMGPROTO', 'file')
  output_dir = os.environ.get('OUTPUT_DIR', '.')

tests/qemu-iotests/iotests.py def _post_shutdown(self) -> None: · line 624

Quoted 9 lines
tests/qemu-iotests/iotests.py +7 -0
@@ -614,6 +616,13 @@ def _post_shutdown(self) -> None:
           super()._post_shutdown()
           self.subprocess_check_valgrind(qemu_valgrind)
+    def _pre_launch(self) -> None:
+        super()._pre_launch()
+        if qemu_print and self._qemu_log_file is not None:
+            # set QEMU binary output to stdout
+            self._qemu_log_file.close()
+            self._qemu_log_file = None
+

So, many use of _private members actually show that proper way of doing this is adding an option to __init__ instead

And then add yet another bool variable in __init__ just to mark when use the log file or not? At this point, if we really don't want this here we can just create a public function in machine.py and call that... This can also be shared with machine.py's _post_shutdown().

Quoted 2 lines

Interesting will pylint complain on using _private members outside of the home class?

No, test 297 tests it and prints no warning or error.

Thank you,
Emanuele

#46 lore raw

PATCH v4 15/15 docs/devel/testing: add -p option to the debug section of QEMU iotests

In reply to Emanuele Giuseppe Esposito
---
 docs/devel/testing.rst | 4 ++++
 1 file changed, 4 insertions(+)
docs/devel/testing.rst +4 -0
index d743e88746..1192d6489e 100644
@@ -250,6 +250,10 @@ a failing test:
 * ``-d`` (debug) just increases the logging verbosity, showing
   for example the QMP commands and answers.
 
+* ``-p`` (print) redirect QEMU’s stdout and stderr to the test output,
+  instead of saving it into a log file in
+  ``$TEST_DIR/qemu-machine-<random_string>``.
+
 Test case groups
 ----------------
 
Signature · 1 line
2.30.2
Reviewed-by on v4 15/15
#47 lore raw

Re: [PATCH v4 15/15] docs/devel/testing: add -p option to the debug section of QEMU iotests

Quoted 1 line · Emanuele Giuseppe Esposito

I'd merge it to previous patch. anyway:

Quoted 20 lines
---
  docs/devel/testing.rst | 4 ++++
  1 file changed, 4 insertions(+)
docs/devel/testing.rst +4 -0
index d743e88746..1192d6489e 100644
@@ -250,6 +250,10 @@ a failing test:
  * ``-d`` (debug) just increases the logging verbosity, showing
    for example the QMP commands and answers.
 
+* ``-p`` (print) redirect QEMU’s stdout and stderr to the test output,
+  instead of saving it into a log file in
+  ``$TEST_DIR/qemu-machine-<random_string>``.
+
  Test case groups
  ----------------
 
Signature · 2 lines
Best regards,
Vladimir
#48 lore raw
In reply to Emanuele Giuseppe Esposito
Quoted 6 lines · Emanuele Giuseppe Esposito

This series adds the option to attach gdbserver and valgrind to the QEMU binary running in qemu_iotests.
It also allows to redirect QEMU binaries output of the python tests to the stdout, instead of a log file.

Patches 1-6 introduce the -gdb option to both python and bash tests,

Could you describe how to use it?

I often have to debug qemu iotests with gdb, and can say the following:

1. Test runs different qemu binaries (not only qemu, but also qemu-nbd, qemu-img, qemu-io)

2. Test can run qemu binaries several times, or even in parallel.

So, with a new option, how will I choice, which qemu (or qemu-nbd, etc) process I want to be attached to gdb?

Currently, I do the following: I recompile with "sleep(15);" at the place where I want to attach, and start the test. During the sleep, I find the needed process in "ps aux" output and start "gdb -p PID".. Sometimes there may be troubles if the place where I inserted the sleep is triggered on another code path.

So, with new option, can my workflow be improved or not?

Quoted 51 lines

7-10 extend the already existing -valgrind flag to work also on python tests, and patch 11 introduces -p to enable logging to stdout.

In particular, patches 1,5,10 focus on extending the QMP socket timers when using gdb/valgrind, otherwise the python tests will fail due to delays in the QMP responses.

This series is tested on the previous serie "qemu-iotests: quality of life improvements"
but independent from it, so it can be applied separately.

---
v4:
* Rename environment variable from GDB_QEMU to GDB_OPTIONS
* This time test 297 (pylint) passes [Max]
* Refactor the qmp_timer field in machine.py, and add a new parameter in machine.py and subclasses constructor [John]
* Add additional check in patch 4 to cover the case where GDB_OPTIONS is empty

Emanuele Giuseppe Esposito (15):
python: qemu: add timer parameter for qmp.accept socket
python: qemu: pass the wrapper field from QEMUQtestmachine to
QEMUMachine
docs/devel/testing: add debug section to the QEMU iotests chapter
qemu-iotests: add option to attach gdbserver
qemu-iotests: delay QMP socket timers
qemu_iotests: insert gdbserver command line as wrapper for qemu binary
qemu-iotests: add gdbserver option to script tests too docs/devel/testing: add -gdb option to the debugging section of QEMU
iotests
qemu-iotests: extend the check script to support valgrind for python
tests
qemu-iotests: extent QMP socket timeout when using valgrind
qemu-iotests: allow valgrind to read/delete the generated log file
qemu-iotests: insert valgrind command line as wrapper for qemu binary docs/devel/testing: add -valgrind option to the debug section of QEMU
iotests
qemu-iotests: add option to show qemu binary logs on stdout docs/devel/testing: add -p option to the debug section of QEMU iotests

  docs/devel/testing.rst        | 30 +++++++++++++++++++
  python/qemu/machine.py        |  7 +++--
  python/qemu/qtest.py          |  9 ++++--
  tests/qemu-iotests/check      | 15 +++++++---
  tests/qemu-iotests/common.rc  |  8 ++++-
  tests/qemu-iotests/iotests.py | 56 +++++++++++++++++++++++++++++++----
  tests/qemu-iotests/testenv.py | 25 ++++++++++++++--
  7 files changed, 132 insertions(+), 18 deletions(-)
Signature · 2 lines
Best regards,
Vladimir
#49 lore raw
Quoted 14 lines · Vladimir Sementsov-Ogievskiy
Quoted 6 lines · Emanuele Giuseppe Esposito

This series adds the option to attach gdbserver and valgrind to the QEMU binary running in qemu_iotests.
It also allows to redirect QEMU binaries output of the python tests to the stdout, instead of a log file.

Patches 1-6 introduce the -gdb option to both python and bash tests,

Could you describe how to use it?

I often have to debug qemu iotests with gdb, and can say the following:

1. Test runs different qemu binaries (not only qemu, but also qemu-nbd, qemu-img, qemu-io)

Quoted 7 lines

2. Test can run qemu binaries several times, or even in parallel.

So, with a new option, how will I choice, which qemu (or qemu-nbd, etc) process I want to be attached to gdb?

Currently, I do the following: I recompile with  "sleep(15);" at the place where I want to attach, and start the test. During the sleep, I find the needed process in "ps aux" output and start "gdb -p PID".. Sometimes there may be troubles if the place where I inserted the sleep is triggered on another code path.

So, with new option, can my workflow be improved or not?

Unfortunately not, unless you want to debug a qemu_system binary.
The usage should be clear in the doc patches of this series (please check them too if you haven't done already), but the idea is to just wrap gdb around every qemu binary usage.

If you apply these patches together with Paolo's "qemu-iotests: quality of life improvements", you will get something much better, where you can choose which test function to run under gdb.

If I recall correctly, you should be able to do something like:
check -gdb -- ../../../tests/qemu-iotests/055 TestCompressedToQcow2

Still, if the test function has multiple qemu binary invocations or in parallel, you gdb will be applied to all of them. I don't think test/debug granularity can be reduced any further, at least not so easily.

Thank you,
Emanuele

Quoted 54 lines
Quoted 52 lines

7-10 extend the already existing -valgrind flag to work also on python tests, and patch 11 introduces -p to enable logging to stdout.

In particular, patches 1,5,10 focus on extending the QMP socket timers when using gdb/valgrind, otherwise the python tests will fail due to delays in the QMP responses.

This series is tested on the previous serie "qemu-iotests: quality of life improvements"
but independent from it, so it can be applied separately.

---
v4:
* Rename environment variable from GDB_QEMU to GDB_OPTIONS
* This time test 297 (pylint) passes [Max]
* Refactor the qmp_timer field in machine.py, and add a new parameter in machine.py and subclasses constructor [John]
* Add additional check in patch 4 to cover the case where GDB_OPTIONS is empty

Emanuele Giuseppe Esposito (15):
   python: qemu: add timer parameter for qmp.accept socket
   python: qemu: pass the wrapper field from QEMUQtestmachine to QEMUMachine
   docs/devel/testing: add debug section to the QEMU iotests chapter
   qemu-iotests: add option to attach gdbserver
   qemu-iotests: delay QMP socket timers
   qemu_iotests: insert gdbserver command line as wrapper for qemu binary
   qemu-iotests: add gdbserver option to script tests too docs/devel/testing: add -gdb option to the debugging section of QEMU iotests
   qemu-iotests: extend the check script to support valgrind for python tests
   qemu-iotests: extent QMP socket timeout when using valgrind
   qemu-iotests: allow valgrind to read/delete the generated log file
   qemu-iotests: insert valgrind command line as wrapper for qemu binary docs/devel/testing: add -valgrind option to the debug section of QEMU iotests
   qemu-iotests: add option to show qemu binary logs on stdout docs/devel/testing: add -p option to the debug section of QEMU iotests

  docs/devel/testing.rst        | 30 +++++++++++++++++++ python/qemu/machine.py        |  7 +++-- python/qemu/qtest.py          |  9 ++++-- tests/qemu-iotests/check      | 15 +++++++--- tests/qemu-iotests/common.rc  |  8 ++++- tests/qemu-iotests/iotests.py | 56 +++++++++++++++++++++++++++++++---- tests/qemu-iotests/testenv.py | 25 ++++++++++++++--

  7 files changed, 132 insertions(+), 18 deletions(-)