xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/lib/cache.exp (revision 796c32c94f6e154afc9de0f63da35c91bb739b45)
1# Copyright 2012-2016 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
17# The in-memory cache.
18array set gdb_data_cache {}
19
20# A helper for gdb_caching_proc that handles the caching.
21
22proc gdb_do_cache {name} {
23    global gdb_data_cache objdir
24    global GDB_PARALLEL
25
26    # See if some other process wrote the cache file.  Cache value per
27    # "board" to handle runs with multiple options
28    # (e.g. unix/{-m32,-64}) correctly.  We use "file join" here
29    # because we later use this in a real filename.
30    set cache_name [file join [target_info name] $name]
31
32    if {[info exists gdb_data_cache($cache_name)]} {
33	verbose "$name: returning '$gdb_data_cache($cache_name)' from cache" 2
34	return $gdb_data_cache($cache_name)
35    }
36
37    if {[info exists GDB_PARALLEL]} {
38	set cache_filename [make_gdb_parallel_path cache $cache_name]
39	if {[file exists $cache_filename]} {
40	    set fd [open $cache_filename]
41	    set gdb_data_cache($cache_name) [read -nonewline $fd]
42	    close $fd
43	    verbose "$name: returning '$gdb_data_cache($cache_name)' from file cache" 2
44	    return $gdb_data_cache($cache_name)
45	}
46    }
47
48    set real_name gdb_real__$name
49    set gdb_data_cache($cache_name) [uplevel 1 $real_name]
50
51    if {[info exists GDB_PARALLEL]} {
52	verbose "$name: returning '$gdb_data_cache($cache_name)' and writing file" 2
53	file mkdir [file dirname $cache_filename]
54	# Make sure to write the results file atomically.
55	set fd [open $cache_filename.[pid] w]
56	puts $fd $gdb_data_cache($cache_name)
57	close $fd
58	file rename -force -- $cache_filename.[pid] $cache_filename
59    }
60    return $gdb_data_cache($cache_name)
61}
62
63# Define a new proc named NAME that takes no arguments.  BODY is the
64# body of the proc.  The proc will evaluate BODY and cache the
65# results, both in memory and, if GDB_PARALLEL is defined, in the
66# filesystem for use across invocations of dejagnu.
67
68proc gdb_caching_proc {name body} {
69    # Define the underlying proc that we'll call.
70    set real_name gdb_real__$name
71    proc $real_name {} $body
72
73    # Define the advertised proc.
74    proc $name {} [list gdb_do_cache $name]
75}
76