diff mbox series

[3/4] tests/guest-debug: Support passing arguments to the GDB test script

Message ID 20240722160709.1677430-4-gustavo.romero@linaro.org
State New
Headers show
Series gdbstub: Add support for MTE in system mode | expand

Commit Message

Gustavo Romero July 22, 2024, 4:07 p.m. UTC
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.romero@linaro.org>
---
 tests/guest-debug/run-test.py | 4 ++++
 1 file changed, 4 insertions(+)

Comments

Alex Bennée July 25, 2024, 8:38 p.m. UTC | #1
Gustavo Romero <gustavo.romero@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.romero@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)
Gustavo Romero Aug. 8, 2024, 5:07 a.m. UTC | #2
Hi Alex,

On 7/25/24 5:38 PM, Alex Bennée wrote:
> Gustavo Romero <gustavo.romero@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.romero@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
diff mbox series

Patch

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")
@@ -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)