| # Copyright 1997-2026 Free Software Foundation, Inc. |
| |
| # This program is free software; you can redistribute it and/or modify |
| # it under the terms of the GNU General Public License as published by |
| # the Free Software Foundation; either version 3 of the License, or |
| # (at your option) any later version. |
| # |
| # This program is distributed in the hope that it will be useful, |
| # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| # GNU General Public License for more details. |
| # |
| # You should have received a copy of the GNU General Public License |
| # along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| |
| # Test the "local-environment" commands. |
| |
| save_vars { env(GDB_TEST_ENV_VAR) } { |
| setenv GDB_TEST_ENV_VAR abc |
| gdb_start |
| } |
| |
| proc test_set_show_env_var { name value test_name } { |
| gdb_test_no_output "set local-environment $name $value" "$test_name" |
| gdb_test "show local-environment $name" "$name = $value" \ |
| "confirm $test_name" |
| } |
| |
| proc test_set_show_env_var_equal { name value test_name } { |
| gdb_test_no_output "set local-environment $name = $value" "$test_name" |
| gdb_test "show local-environment $name" "$name = $value" \ |
| "confirm $test_name" |
| } |
| |
| # Verify that we can show all currently-set environment variables. |
| # It's hard to do this verification since we can't really compare each |
| # entry with the current environment. So we just check to see if |
| # there is anything that looks like an environment variable being |
| # printed. |
| set saw_env 0 |
| gdb_test_multiple "show environment" "show environment works" -lbl { |
| -re "\r\nGDB_TEST_ENV_VAR=abc" { |
| incr saw_env 1 |
| exp_continue |
| } |
| |
| -re -wrap "" { |
| } |
| } |
| |
| set test "show environment displayed variable" |
| if {[is_remote host]} { |
| unsupported $test |
| } else { |
| gdb_assert {$saw_env == 1} $test |
| } |
| |
| # Verify that we can unset a specific environment variable. |
| gdb_test_no_output "unset local-environment EDITOR" \ |
| "unset environment variable" |
| |
| gdb_test "show local-environment EDITOR" \ |
| "Environment variable \"EDITOR\" not defined\." \ |
| "confirm unset environment variable worked" |
| |
| # Verify that we can unset all environment variables. |
| # Note that on some platforms this is not possible. |
| set can_unset_all 0 |
| # Disable confirmation so we don't have to deal with the question. |
| gdb_test_multiple "with confirm off -- unset local-environment" \ |
| "unset all environment variables" { |
| -re -wrap "Cannot clear the local environment on this host." { |
| # Nothing. |
| pass $gdb_test_name |
| } |
| |
| -re -wrap "" { |
| set can_unset_all 1 |
| pass $gdb_test_name |
| } |
| } |
| |
| if {$can_unset_all} { |
| gdb_test_no_output "show local-environment" \ |
| "all environment variables have been unset" |
| } |
| |
| # Verify that we can set a specific environment variable. |
| test_set_show_env_var "EDITOR" "emacs" "set environment variable" |
| |
| # Verify that GDB responds gracefully to a request to set environment, |
| # with no variable name. |
| gdb_test "set local-environment" \ |
| "Argument required \\\(environment variable and value\\\)\." \ |
| "set environment without arguments" |
| |
| # I'm not sure just what GDB has in mind in explicitly checking |
| # for this variant, but since GDB handles it, test it. |
| gdb_test "set local-environment =" \ |
| "Argument required \\\(environment variable to set\\\)\." \ |
| "set environment without variable name" |
| |
| # Setting an environment variable without a value sets it to a NULL |
| # value. |
| gdb_test "set local-environment EDITOR" \ |
| "Setting environment variable \"EDITOR\" to null value\." \ |
| "set environment variable to null value" |
| gdb_test "show local-environment EDITOR" "EDITOR = " \ |
| "show null environment variable" |
| |
| # Verify that GDB responds gracefully to an attempt to show a |
| # non-existent environment variable. (We hope this variable is |
| # undefined!) |
| gdb_test "show local-environment FOOBARBAZGRUNGESPAZBALL" \ |
| "Environment variable \"FOOBARBAZGRUNGESPAZBALL\" not defined\." \ |
| "show non-existent environment variable" |
| |
| # Verify that GDB can set an environment variable hitherto undefined. |
| test_set_show_env_var "FOOBARBAZGRUNGESPAZBALL" "t" \ |
| "set environment variable previously undefined" |
| |
| # Verify that GDB can also set an environment variable using the "=" |
| # syntax. |
| test_set_show_env_var_equal "FOOBARBAZGRUNGESPAZBALL" "y" \ |
| "set environment variable using = syntax" |
| |
| # Verify that GDB can set an environment variable to a value that has |
| # an embedded (trailing, in this case) equals. |
| test_set_show_env_var_equal "FOOBARBAZGRUNGESPAZBALL" "t=" \ |
| "set environment variable with trailing equals" |
| |
| # Verify that GDB can set an environment variable to a value preceded |
| # by whitespace, and that such whitespace is ignored (not included |
| # in the set value). |
| gdb_test_no_output \ |
| "set local-environment FOOBARBAZGRUNGESPAZBALL = foo" \ |
| "set environment variable with trailing whitespace" |
| gdb_test "show local-environment FOOBARBAZGRUNGESPAZBALL" \ |
| "FOOBARBAZGRUNGESPAZBALL = foo" \ |
| "confirm set environment variable with trailing whitespace" |
| |
| # Verify that the setting affects the "pipe" command. |
| gdb_test "pipe print 23 | printenv | grep FOOBARBAZGRUNGESPAZBALL" \ |
| "FOOBARBAZGRUNGESPAZBALL=foo" \ |
| "set local-environment affects pipe" |
| |
| # Verify that the setting affects "shell". |
| gdb_test "shell printenv | grep FOOBARBAZGRUNGESPAZBALL" \ |
| "FOOBARBAZGRUNGESPAZBALL=foo" \ |
| "set local-environment affects shell" |
| |
| gdb_exit |