1# Copyright (C) 2010-2020 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# This file is part of the GDB testsuite.
17# It tests the Guile symbol table support.
18
19load_lib gdb-guile.exp
20
21standard_testfile scm-symtab.c scm-symtab-2.c
22
23if {[prepare_for_testing "failed to prepare" $testfile \
24	 [list $srcfile $srcfile2] debug]} {
25    return
26}
27
28# Skip all tests if Guile scripting is not enabled.
29if { [skip_guile_tests] } { continue }
30
31if ![gdb_guile_runto_main] {
32    return
33}
34
35set debug_types [debug_types]
36
37# Setup and get the symbol table.
38set line_no [gdb_get_line_number "Block break here."]
39gdb_breakpoint $line_no
40gdb_continue_to_breakpoint "Block break here."
41gdb_scm_test_silent_cmd "guile (define frame (selected-frame))" \
42    "get frame"
43gdb_scm_test_silent_cmd "guile (define sal (frame-sal frame))" \
44    "get sal"
45gdb_scm_test_silent_cmd "guile (define symtab (sal-symtab sal))" \
46    "get symtab"
47gdb_scm_test_silent_cmd "guile (define global-block (symtab-global-block symtab))" \
48    "get global block"
49gdb_scm_test_silent_cmd "guile (define static-block (symtab-static-block symtab))" \
50    "get static block"
51
52gdb_scm_test_silent_cmd "guile (define global-symbols (map symbol-name (block-symbols global-block)))" \
53    "get global symbol names"
54gdb_scm_test_silent_cmd "guile (define static-symbols (map symbol-name (block-symbols static-block)))" \
55    "get static symbol names"
56gdb_scm_test_silent_cmd "guile (define global-isymbols '()) (define static-isymbols '())" \
57    "set up iterated symbol name lists"
58# TODO: iterated symbols
59gdb_scm_test_silent_cmd "step" "Step to the next line"
60gdb_scm_test_silent_cmd "guile (define new-pc (sal-pc (frame-sal (selected-frame))))" \
61    "get new pc"
62
63# Test sal.
64gdb_test "guile (print (sal-symtab sal))" \
65    ".*gdb.guile/scm-symtab.c.*" "Test sal-symtab"
66gdb_test "guile (print (sal-pc sal))" \
67    "${decimal}" "test sal-pc"
68gdb_test "guile (print (= (sal-last sal) (- new-pc 1)))" \
69    "#t" "test sal-last"
70gdb_test "guile (print (sal-line sal))" \
71    "$line_no" "test sal-line"
72gdb_test "guile (print (sal-valid? sal))" \
73    "#t" "test sal-valid?"
74
75# Test eq? on symtabs.
76gdb_scm_test_silent_cmd "guile (define sal1 (frame-sal frame))" \
77    "get sal1"
78gdb_scm_test_silent_cmd "guile (define sal2 (frame-sal (frame-older frame)))" \
79    "get sal2"
80gdb_test "guile (print (eq? symtab (sal-symtab sal1)))" \
81    "= #t" "test eq? of equal symtabs"
82gdb_test "guile (print (eq? symtab (sal-symtab sal2)))" \
83    "= #t" "test eq? of equal symtabs from different sals"
84gdb_test "guile (print (eq? symtab (symbol-symtab (lookup-global-symbol \"func1\"))))" \
85    "= #f" "test eq? of not-equal symtabs"
86
87# Test symbol table.
88gdb_test "guile (print (symtab-filename symtab))" \
89    ".*gdb.guile/scm-symtab.c.*" "test symtab-filename"
90gdb_test "guile (print (symtab-objfile symtab))" \
91    "#<gdb:objfile .*scm-symtab>" "test symtab-objfile"
92gdb_test "guile (print (symtab-fullname symtab))" \
93    "testsuite/gdb.guile/scm-symtab.c.*" "test symtab-fullname"
94gdb_test "guile (print (symtab-valid? symtab))" \
95    "#t" "test symtab-valid?"
96gdb_test "guile (print (->bool (member \"qq\" global-symbols)))" \
97    "#t" "test qq in global symbols"
98gdb_test "guile (print (->bool (member \"func\" global-symbols)))" \
99    "#t" "test func in global symbols"
100gdb_test "guile (print (->bool (member \"main\" global-symbols)))" \
101    "#t" "test main in global symbols"
102gdb_test "guile (print (->bool (member \"int\" static-symbols)))" \
103    "#t" "test int in static symbols"
104gdb_test "guile (print (->bool (member \"char\" static-symbols)))" \
105    "#t" "test char in static symbols"
106gdb_test_multiple \
107    "guile (print (->bool (member \"simple_struct\" static-symbols)))" \
108    "test simple_struct in static symbols" {
109	-re -wrap "#t" {
110	    pass $gdb_test_name
111	}
112	-re -wrap "#f" {
113	    if { $debug_types } {
114		# Xfail for PR gcc/90232.
115		xfail $gdb_test_name
116	    } else {
117		fail $gdb_test_name
118	    }
119	}
120    }
121
122# Test is_valid when the objfile is unloaded.  This must be the last
123# test as it unloads the object file in GDB.
124gdb_unload
125gdb_test "guile (print (sal-valid? sal))" \
126    "#f" "test sal-valid? after unloading"
127gdb_test "guile (print (symtab-valid? symtab))" \
128    "#f" "test symtab-valid? after unloading"
129
130gdb_test_no_output "guile (set! sal #f)" \
131    "test sal destructor"
132gdb_test_no_output "guile (set! symtab #f)" \
133    "test symtab destructor"
134gdb_test_no_output "guile (gc)" "GC to trigger destructors"
135
136# Start with a fresh gdb.
137clean_restart ${testfile}
138
139# Test find-pc-line.
140# The following tests require execution.
141
142if ![gdb_guile_runto_main] {
143    return
144}
145
146runto [gdb_get_line_number "Break at func2 call site."]
147
148gdb_scm_test_silent_cmd "guile (define line (sal-line (frame-sal (selected-frame))))" \
149    "get line number of func2 call site"
150gdb_test "guile (print (= (sal-line (find-pc-line (frame-pc (selected-frame)))) line))" \
151    "#t" "test find-pc-line at func2 call site"
152
153gdb_scm_test_silent_cmd "step" "step into func2"
154gdb_scm_test_silent_cmd "up" "step out of func2"
155
156gdb_test "guile (print (> (sal-line (find-pc-line (frame-pc (selected-frame)))) line))" \
157    "#t" "test find-pc-line with resume address"
158