From 91265a7d7cddc10314335ffcfbfae7159c7cecb1 Mon Sep 17 00:00:00 2001 From: Pedro Alves Date: Wed, 8 Feb 2023 16:06:23 +0000 Subject: Add new "$_shell(CMD)" internal function For testing a following patch, I wanted a way to send a SIGINT to GDB from a breakpoint condition. And I didn't want to do it from a Python breakpoint or Python function, as I wanted to exercise non-Python code paths. So I thought I'd add a new $_shell internal function, that runs a command under the shell, and returns the exit code. With this, I could write: (gdb) b foo if $_shell("kill -SIGINT $gdb_pid") != 0 || I think this is generally useful, hence I'm proposing it here. Here's the new function in action: (gdb) p $_shell("true") $1 = 0 (gdb) p $_shell("false") $2 = 1 (gdb) p $_shell("echo hello") hello $3 = 0 (gdb) p $_shell("foobar") bash: line 1: foobar: command not found $4 = 127 (gdb) help function _shell $_shell - execute a shell command and returns the result. Usage: $_shell (command) Returns the command's exit code: zero on success, non-zero otherwise. (gdb) NEWS and manual changes included. Approved-By: Andrew Burgess Approved-By: Tom Tromey Approved-By: Eli Zaretskii Change-Id: I7e36d451ee6b428cbf41fded415ae2d6b4efaa4e --- gdb/testsuite/gdb.base/default.exp | 1 + gdb/testsuite/gdb.base/shell.exp | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) (limited to 'gdb/testsuite') diff --git a/gdb/testsuite/gdb.base/default.exp b/gdb/testsuite/gdb.base/default.exp index d0789a6..7e73db0 100644 --- a/gdb/testsuite/gdb.base/default.exp +++ b/gdb/testsuite/gdb.base/default.exp @@ -606,6 +606,7 @@ set show_conv_list \ {$_cimag = } \ {$_creal = } \ {$_isvoid = } \ + {$_shell = } \ {$_gdb_maint_setting_str = } \ {$_gdb_maint_setting = } \ {$_gdb_setting_str = } \ diff --git a/gdb/testsuite/gdb.base/shell.exp b/gdb/testsuite/gdb.base/shell.exp index 31cdcb4..ba1691e 100644 --- a/gdb/testsuite/gdb.base/shell.exp +++ b/gdb/testsuite/gdb.base/shell.exp @@ -41,6 +41,42 @@ if { ! [ishost *-*-mingw*] } { gdb_test "p \$_shell_exitsignal" " = 2" "shell interrupt exitsignal" } +# Test the $_shell convenience function. + +with_test_prefix "\$_shell convenience function" { + # Simple commands, check the result code. + gdb_test "p \$_shell(\"true\")" " = 0" + gdb_test "p \$_shell(\"false\")" " = 1" + + # Test command with arguments. + gdb_test "p \$_shell(\"echo foo\")" "foo\r\n\\$${decimal} = 0" + + # Check the type of the result. + gdb_test "ptype \$_shell(\"true\")" "type = int" + + # Test passing a non-literal string as command name. + gdb_test "p \$cmd = \"echo bar\"" " = \"echo bar\"" + gdb_test "p \$_shell(\$cmd)" "bar\r\n\\$${decimal} = 0" + + # Test executing a non-existing command. The result is + # shell-dependent, but most (all?) POSIX-like shells return 127 in + # this case. + gdb_test "p \$_shell(\"non-existing-command-foo-bar-qux\")" " = 127" + + gdb_test "p \$_shell" \ + " = " + gdb_test "ptype \$_shell" \ + "type = " + + # Test error scenarios. + gdb_test "p \$_shell()" \ + "You must provide one argument for \\\$_shell\\\." + gdb_test "p \$_shell(\"a\", \"b\")" \ + "You must provide one argument for \\\$_shell\\\." + gdb_test "p \$_shell(1)" \ + "Argument must be a string\\\." +} + # Define the user command "foo", used to test "pipe" command. gdb_test_multiple "define foo" "define foo" { -re "End with" { -- cgit v1.1