Home | History | Annotate | Line # | Download | only in gdb.base
      1 # Copyright 2024 Free Software Foundation, Inc.
      2 
      3 # This program is free software; you can redistribute it and/or modify
      4 # it under the terms of the GNU General Public License as published by
      5 # the Free Software Foundation; either version 3 of the License, or
      6 # (at your option) any later version.
      7 #
      8 # This program is distributed in the hope that it will be useful,
      9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11 # GNU General Public License for more details.
     12 #
     13 # You should have received a copy of the GNU General Public License
     14 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
     15 
     16 # Check GDB can handle reading the full executable name and argument
     17 # list from a core file.
     18 #
     19 # Currently, only Linux supports reading full executable and arguments
     20 # from a core file.
     21 require {is_any_target "*-*-linux*" "*-*-freebsd*"}
     22 
     23 standard_testfile
     24 
     25 if {[build_executable $testfile.exp $testfile $srcfile] == -1} {
     26     untested "failed to compile"
     27     return -1
     28 }
     29 
     30 # Linux core files can encore upto 80 characters for the command and
     31 # arguments in the psinfo.  If BINFILE is less than 80 characters in
     32 # length then lets try to make it longer.
     33 set binfile_len [string length $binfile]
     34 if { $binfile_len <= 80 } {
     35     set extra_len [expr 80 - $binfile_len + 1]
     36     set extra_str [string repeat "x" $extra_len]
     37     set new_binfile $binfile$extra_str
     38     remote_exec build "mv $binfile $new_binfile"
     39     set binfile $new_binfile
     40 }
     41 
     42 # Generate a core file, this time the inferior has no additional
     43 # arguments.
     44 set corefile [core_find $binfile {}]
     45 if {$corefile == ""} {
     46     untested "unable to create corefile"
     47     return 0
     48 }
     49 set corefile_1 "$binfile.1.core"
     50 remote_exec build "mv $corefile $corefile_1"
     51 
     52 # Load the core file and confirm that the full executable name is
     53 # seen.
     54 clean_restart $binfile
     55 set saw_generated_line false
     56 gdb_test_multiple "core-file $corefile_1" "load core file no args" {
     57     -re "^Core was generated by `[string_to_regexp $binfile]'\\.\r\n" {
     58 	set saw_generated_line true
     59 	exp_continue
     60     }
     61 
     62     -re "^$gdb_prompt $" {
     63 	gdb_assert { $saw_generated_line } $gdb_test_name
     64     }
     65 
     66     -re "^\[^\r\n\]*\r\n" {
     67 	exp_continue
     68     }
     69 }
     70 
     71 # Generate a core file, this time pass some arguments to the inferior.
     72 set args "aaaaa bbbbb ccccc ddddd e\\\\ e\\\\ e\\\\ e\\\\ e"
     73 set corefile [core_find $binfile {} $args]
     74 if {$corefile == ""} {
     75     untested "unable to create corefile"
     76     return 0
     77 }
     78 set corefile_2 "$binfile.2.core"
     79 remote_exec build "mv $corefile $corefile_2"
     80 
     81 # Load the core file and confirm that the full executable name and
     82 # argument list are seen.
     83 clean_restart $binfile
     84 set saw_generated_line false
     85 gdb_test_multiple "core-file $corefile_2" "load core file with args" {
     86     -re "^Core was generated by `[string_to_regexp $binfile] $args'\\.\r\n" {
     87 	set saw_generated_line true
     88 	exp_continue
     89     }
     90 
     91     -re "^$gdb_prompt $" {
     92 	gdb_assert { $saw_generated_line } $gdb_test_name
     93     }
     94 
     95     -re "^\[^\r\n\]*\r\n" {
     96 	exp_continue
     97     }
     98 }
     99 
    100 # Also, the argument list should be available through 'show args'.
    101 gdb_test "show args" \
    102     "Argument list to give program being debugged when it is started is \"$args\"\\."
    103 
    104 # Move up to 'main'.  Do it this way because we cannot know how many
    105 # frames up 'main' actually is.
    106 gdb_test_multiple "up" "move up to main" {
    107     -re -wrap "#$decimal\\s+\[^\r\n\]+ in main .*" {
    108 	pass $gdb_test_name
    109     }
    110 
    111     -re -wrap "#$decimal\\s+\[^\r\n\]+ in .*" {
    112 	send_gdb "up\n"
    113 	exp_continue
    114     }
    115 }
    116 
    117 # Check that the inferior was started with the expected arguments.
    118 gdb_test "print argc" " = 6"
    119 gdb_test "print argv\[1\]" " = $hex \"aaaaa\""
    120 gdb_test "print argv\[2\]" " = $hex \"bbbbb\""
    121 gdb_test "print argv\[3\]" " = $hex \"ccccc\""
    122 gdb_test "print argv\[4\]" " = $hex \"ddddd\""
    123 gdb_test "print argv\[5\]" " = $hex \"e e e e e\""
    124 
    125 # Find the name of an environment variable that is not set.
    126 set env_var_base "GDB_TEST_ENV_VAR_"
    127 set env_var_name ""
    128 
    129 for { set i 0 } { $i < 10 } { incr i } {
    130     set tmp_name ${env_var_base}${i}
    131     if { ! [info exists ::env($tmp_name)] } {
    132 	set env_var_name $tmp_name
    133 	break
    134     }
    135 }
    136 
    137 if { $env_var_name eq "" } {
    138     unsupported "couldn't find suitable environment variable name"
    139     return -1
    140 }
    141 
    142 # Generate a core file with this environment variable set.
    143 set env_var_value "TEST VALUE"
    144 save_vars { ::env($env_var_name) } {
    145     setenv $env_var_name $env_var_value
    146 
    147     set corefile [core_find $binfile {} $args]
    148     if {$corefile == ""} {
    149 	untested "unable to create corefile"
    150 	return 0
    151     }
    152 }
    153 set corefile_3 "$binfile.2.core"
    154 remote_exec build "mv $corefile $corefile_3"
    155 
    156 # Restart, load the core file, and check the environment variable
    157 # shows up.
    158 clean_restart $binfile
    159 
    160 # Check for environment variable VAR_NAME in the environment, its
    161 # value should be VAR_VALUE.
    162 proc check_for_env_var { var_name var_value } {
    163     set saw_var false
    164     gdb_test_multiple "show environment" "" {
    165 	-re "^$var_name=$var_value\r\n" {
    166 	    set saw_var true
    167 	    exp_continue
    168 	}
    169 	-re "^\[^\r\n\]*\r\n" {
    170 	    exp_continue
    171 	}
    172 	-re "^$::gdb_prompt $" {
    173 	}
    174     }
    175     return $saw_var
    176 }
    177 
    178 gdb_assert { ![check_for_env_var $env_var_name $env_var_value] } \
    179     "environment variable is not set before core file load"
    180 
    181 gdb_test "core-file $corefile_3" \
    182     "Core was generated by `[string_to_regexp $binfile] $args'\\.\r\n.*" \
    183     "load core file for environment test"
    184 
    185 gdb_assert { [check_for_env_var $env_var_name $env_var_value] } \
    186     "environment variable is set after core file load"
    187