Hi Alex,

On 7/25/24 5:38 PM, Alex Bennée wrote:
Gustavo Romero <gustavo.rom...@linaro.org> writes:

This commit adds a new option to run-test.py, --test-args, which can
be used to pass arguments to the GDB test script specified by the --test
option. The arguments passed are in the key=value form, and multiple
pairs can be passed, separated by a space. For example:

run-test.py [...] --test <GDB_TEST_SCRIPT> --test-args v0="string" v1=10

The 'v0' and 'v1' variables will then be available in the GDB test
script, like this:

print(v0)
print(v1)

Signed-off-by: Gustavo Romero <gustavo.rom...@linaro.org>
---
  tests/guest-debug/run-test.py | 4 ++++
  1 file changed, 4 insertions(+)

diff --git a/tests/guest-debug/run-test.py b/tests/guest-debug/run-test.py
index 368ff8a890..63b55fb8bd 100755
--- a/tests/guest-debug/run-test.py
+++ b/tests/guest-debug/run-test.py
@@ -27,6 +27,7 @@ def get_args():
      parser.add_argument("--binary", help="Binary to debug",
                          required=True)
      parser.add_argument("--test", help="GDB test script")
+    parser.add_argument("--test-args", help="Arguments to GDB test script")
      parser.add_argument("--gdb", help="The gdb binary to use",
                          default=None)
      parser.add_argument("--gdb-args", help="Additional gdb
arguments")

I might be easier to do:

     parser.add_argument('test_args', nargs='*',
                         help="Additional args for test. "
                         "You should precede with -- "
                         "to avoid confusion with flags for runner script")


@@ -91,6 +92,9 @@ def log(output, msg):
      gdb_cmd += " -ex 'target remote %s'" % (socket_name)
      # finally the test script itself
      if args.test:
+        if args.test_args:
+            test_args = args.test_args.replace(" ",";")
+            gdb_cmd += f" -ex 'py {test_args}'"
          gdb_cmd += " -x %s" % (args.test)
As we discussed, -ex 'py [...]' just allow setting variables in Python,
so it won't understand plain argparse'd arguments. For instance, if we
do 'run-test.py [...] -- --mode=system' this will fail because
"--mode-system" not a valid Python syntax. Moreover, there isn't another
way to pass args to the GDB Python scripts afaict.

But since that's a nice idea (to be able to parse args in the GDB Python
scripts) I found a workaround. It's possible to set the sys.argv in the
script env. So -ex "py sys.argv=['--mode=user', '--arg1=1', '--arg2=2', ...]",
for instance, correctly sets the argv and make argparse work normally in
the script. Thus I used that approach to address your suggestion. Please
see v2.


Cheers,
Gustavo

Reply via email to