aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.base/corefile-buildid.exp
blob: 88d989d84cc5f27c0c288d8268fd1eb91304af16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# Copyright 2019-2025 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/>.

# Based on break.exp, written by Rob Savoye. (rob@cygnus.com)
# Modified to test gdb's handling of separate debug info files.
# Modified to test gdb's handling of a debug-id retrieval.

# Build-id-related tests for core files.

standard_testfile .c -shlib-shr.c -shlib.c

# Create a corefile from PROGNAME.  Return the name of the generated
# corefile, or the empty string if anything goes wrong.
#
# The generated corefile must contain a buildid for PROGNAME.  If it
# doesn't then an empty string will be returned.
proc create_core_file { progname } {
    # Generate a corefile.
    set corefile [core_find $progname]
    if {$corefile == ""} {
	untested "could not generate core file"
	return ""
    }
    verbose -log "corefile is $corefile"

    # Check the corefile has a build-id for the executable.
    if { [catch "exec [gdb_find_eu-unstrip] -n --core $corefile" output] == 0 } {
	set line [lindex [split $output "\n"] 0]
	set binfile_re (?:[string_to_regexp $progname]|\\\[(?:exe|pie)\\\])
	if { ![regexp "^${::hex}\\+${::hex} \[a-f0-9\]+@${::hex}.*$binfile_re$" $line] } {
	    unsupported "no build-id for executable in corefile"
	    return ""
	}
    } else {
	unsupported "eu-unstrip tool failed"
	return ""
    }

    return $corefile
}


# Build a non-shared executable.

proc build_corefile_buildid_exec { progname } {
    return [expr {[build_executable "build non-shared exec" $progname $::srcfile] != -1}]
}

# Build a shared executable.

proc build_corefile_buildid_shared { progname } {
    # Compile DSO.
    set objdso [standard_output_file $::testfile-shlib-shr.so]
    if {[build_executable "build dso" $objdso $::srcfile2 {debug shlib}] == -1} {
	return false
    }


    # Compile shared library.
    set srclib $::srcfile3
    set libname lib$::testfile.so
    set objlib [standard_output_file $libname]
    set dlopen_lib [shlib_target_file $objdso]
    set opts [list debug shlib_load shlib \
		  additional_flags=-DSHLIB_NAME=\"$dlopen_lib\"]
    if {[build_executable "build solib" $objlib $::srcfile3 $opts] == -1} {
	return false
    }

    # Compile main program.
    set opts [list debug shlib=$objlib additional_flags=-DTEST_SHARED]
    if {[build_executable "build shared exec" $progname $::srcfile $opts] == -1} {
	return false
    }

    return true
}

# Append DEBUGDIR to the debug-file-directory path.

proc append_debug_dir {debugdir} {
    global gdb_prompt

    set orig_debugdir {}
    gdb_test_multiple "show debug-file-directory" \
	"get debug-file-directory" {
	    -re "The directory where separate debug symbols are searched for is \"(.*)\"\.\[\r\n\]+$gdb_prompt $" {
		set orig_debugdir $expect_out(1,string)
		pass "get debug-file-directory"
	    }
	}
    gdb_test_no_output "set debug-file-directory $debugdir:$orig_debugdir" \
	"append debug directory"
}

# A convenience procedure to check if "info files" mentions the exec file
# FILE.

proc check_exec_file {file} {
    global gdb_prompt
    send_log "expecting exec file \"$file\"\n"

    # Get line with "Local exec file:".
    set ok 0
    gdb_test_multiple "info files" "" -lbl {
	-re "\r\nLocal exec file:" {
	    set test_name $gdb_test_name
	    set ok 1
	}
    }

    if { $ok == 0 } {
	return
    }

    # Get subsequent line with $file.
    set ok 0
    gdb_test_multiple "" $test_name -lbl {
	-re "\r\n\[\t\ \]+`[string_to_regexp $file]'\[^\r\n\]*" {
	    set ok 1
	}
    }

    if { $ok == 0 } {
	return
    }

    # Skip till prompt.
    gdb_test_multiple "" $test_name -lbl {
	-re "\r\n$gdb_prompt $" {
	    pass $gdb_test_name
	}
    }
}

# Test whether gdb can find an exec file from a core file's build-id.
# The executable (and separate debuginfo if SEPDEBUG is true) is
# copied to the .build-id directory.
#
# SUFFIX is appended to the .builid-id parent directory name to
# keep all tests separate.
# SYMLINK specifies whether build-id files should be copied or symlinked.
# SHARED is a boolean indicating whether we are testing the shared
# library core dump test case.

proc locate_exec_from_core_build_id {corefile buildid \
					 dirname progname \
					 sepdebug symlink shared} {
    clean_restart

    # Set up the build-id directory and symlink the binary there.
    set d "debugdir"
    if {$shared} {
	set d "${d}_shared"
    } else {
	set d "${d}_not-shared"
    }
    if {$symlink} {
	set d "${d}_symlink"
    } else {
	set d "${d}_copy"
    }
    if {$sepdebug} {
	set d "${d}_stripped"
    } else {
	set d "${d}_not-stripped"
    }

    set debugdir [standard_output_file $d]
    remote_exec build \
	"mkdir -p [file join $debugdir [file dirname $buildid]]"

    set files_list {}
    lappend files_list [file join $dirname [file tail $progname]] \
	$buildid
    if {$sepdebug} {
	lappend files_list [file join $dirname [file tail $progname]].debug \
	    "$buildid.debug"
    }

    foreach {target name} $files_list {
	set t [file join $dirname [file tail $target]]
	if {$symlink} {
	    remote_exec build "ln -s $t [file join $debugdir $name]"
	} else {
	    remote_exec build "cp $t [file join $debugdir $name]"
	}
    }

    # Append the debugdir to the separate debug directory search path.
    append_debug_dir $debugdir

    gdb_test "core-file $corefile" "Program terminated with .*" \
	"load core file"
    if {$symlink} {
	set expected_file [file join $dirname [file tail $progname]]
    } else {
	set expected_file $buildid
    }
    check_exec_file [file join $debugdir $expected_file]
}

foreach_with_prefix mode { exec shared } {
    # Build the executable.
    set progname ${binfile}-$mode
    set build_proc build_corefile_buildid_${mode}
    if { ![$build_proc $progname] } {
	return -1
    }

    # Generate a corefile.
    set corefile [create_core_file $progname]
    if { $corefile eq "" } {
	return -1
    }

    # Get the build-id filename without ".debug" on the end.  This
    # will have the format: '.build-id/xx/xxxxx'
    set buildid [build_id_debug_filename_get $progname ""]
    if {$buildid == ""} {
	untested "binary has no build-id"
	return
    }
    verbose -log "build-id is $buildid"

    # Create a directory for the non-stripped test.
    set combined_dirname [standard_output_file ${mode}_non-stripped]
    remote_exec build "mkdir -p $combined_dirname"
    remote_exec build "cp $progname $combined_dirname"

    # Create a directory for the stripped test.
    if {[gdb_gnu_strip_debug [standard_output_file $progname] no-debuglink] != 0} {
	untested "could not strip executable  for [join $suffix \ ]"
	return
    }
    set sepdebug_dirname [standard_output_file ${mode}_stripped]
    remote_exec build "mkdir -p $sepdebug_dirname"
    remote_exec build "mv $progname $sepdebug_dirname"
    remote_exec build "mv ${progname}.debug $sepdebug_dirname"

    # Now do the actual testing part.  Fill out a debug directory with
    # build-id related files (copies or symlinks) and then load the
    # corefile.  Check GDB finds the executable and debug information
    # via the build-id related debug directory contents.
    foreach_with_prefix sepdebug { false true } {
	if { $sepdebug } {
	    set dirname $sepdebug_dirname
	} else {
	    set dirname $combined_dirname
	}

	foreach_with_prefix symlink { false true } {
	    locate_exec_from_core_build_id $corefile $buildid \
		$dirname $progname \
		$sepdebug $symlink [expr {$mode eq "shared"}]
	}
    }
}