aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.guile/scm-parameter.exp
blob: e2f82a1e7387aa096be5f24a43c134e5975fbaf4 (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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# Copyright (C) 2010-2023 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/>.

# This file is part of the GDB testsuite.
# It tests GDB parameter support in Guile.

load_lib gdb-guile.exp

require allow_guile_tests

clean_restart

gdb_install_guile_utils
gdb_install_guile_module

proc scm_param_test_maybe_no_output { command pattern args } {
    if [string length $pattern] {
	gdb_test $command $pattern $args
    } else {
	gdb_test_no_output $command $args
    }
}

# We use "." here instead of ":" so that this works on win32 too.
if { [is_remote host] } {
    # Proc gdb_reinitialize_dir has no effect for remote host.
    gdb_test "guile (print (parameter-value \"directories\"))" \
	"\\\$cdir.\\\$cwd"
} else {
    set escaped_directory [string_to_regexp "$srcdir/$subdir"]
    gdb_test "guile (print (parameter-value \"directories\"))" \
	"$escaped_directory.\\\$cdir.\\\$cwd"
}

# Test a simple boolean parameter, and parameter? while we're at it.

gdb_test_multiline "Simple gdb boolean parameter" \
    "guile" "" \
    "(define test-param" "" \
    "  (make-parameter \"print test-param\"" "" \
    "   #:command-class COMMAND_DATA" "" \
    "   #:parameter-type PARAM_BOOLEAN" "" \
    "   #:doc \"When enabled, test param does something useful. When disabled, does nothing.\"" "" \
    "   #:set-doc \"Set the state of the boolean test-param.\"" "" \
    "   #:show-doc \"Show the state of the boolean test-param.\"" "" \
    "   #:show-func (lambda (self value)" ""\
    "      (format #f \"The state of the Test Parameter is ~a.\" value))" "" \
    "   #:initial-value #t))" "" \
    "(register-parameter! test-param)" "" \
    "end"

with_test_prefix "test-param" {
    gdb_test "guile (print (parameter-value test-param))" "= #t" "parameter value (true)"
    gdb_test "show print test-param" "The state of the Test Parameter is on." "show parameter on"
    gdb_test_no_output "set print test-param off"
    gdb_test "show print test-param" "The state of the Test Parameter is off." "show parameter off"
    gdb_test "guile (print (parameter-value test-param))" "= #f" "parameter value (false)"
    gdb_test "help show print test-param" "Show the state of the boolean test-param.*" "show help"
    gdb_test "help set print test-param" "Set the state of the boolean test-param.*" "set help"
    gdb_test "help set print" "set print test-param -- Set the state of the boolean test-param.*" "general help"

    gdb_test "guile (print (parameter? test-param))" "= #t"
    gdb_test "guile (print (parameter? 42))" "= #f"
}

# Test an enum parameter.

gdb_test_multiline "enum gdb parameter" \
    "guile" "" \
    "(define test-enum-param" "" \
    "  (make-parameter \"print test-enum-param\"" "" \
    "   #:command-class COMMAND_DATA" "" \
    "   #:parameter-type PARAM_ENUM" "" \
    "   #:enum-list '(\"one\" \"two\")" "" \
    "   #:doc \"When set, test param does something useful. When disabled, does nothing.\"" "" \
    "   #:show-doc \"Show the state of the enum.\"" "" \
    "   #:set-doc \"Set the state of the enum.\"" "" \
    "   #:show-func (lambda (self value)" "" \
    "      (format #f \"The state of the enum is ~a.\" value))" "" \
    "   #:initial-value \"one\"))" "" \
    "(register-parameter! test-enum-param)" "" \
    "end"

with_test_prefix "test-enum-param" {
    gdb_test "guile (print (parameter-value test-enum-param))" "one" "enum parameter value (one)"
    gdb_test "show print test-enum-param" "The state of the enum is one." "show initial value"
    gdb_test_no_output "set print test-enum-param two"
    gdb_test "show print test-enum-param" "The state of the enum is two." "show new value"
    gdb_test "guile (print (parameter-value test-enum-param))" "two" "enum parameter value (two)"
    gdb_test "set print test-enum-param three" "Undefined item: \"three\".*" "set invalid enum parameter" 
}

# Test integer parameters.

foreach_with_prefix param {
    "listsize"
    "print elements"
    "max-completions"
    "print characters"
} {
    set param_range_error "integer -1 out of range"
    set param_type_error \
	"#<gdb:exception out-of-range\
	 \\(\"gdbscm_parameter_value\"\
	    \"Out of range: program error: unhandled type in position 1: ~S\"\
	    \\(3\\) \\(3\\)\\)>"
    switch -- $param {
	"listsize" {
	    set param_get_zero "#:unlimited"
	    set param_get_minus_one -1
	    set param_set_minus_one ""
	}
	"print elements" -
	"print characters" {
	    set param_get_zero "#:unlimited"
	    set param_get_minus_one "#:unlimited"
	    set param_set_minus_one $param_range_error
	}
	"max-completions" {
	    set param_get_zero 0
	    set param_get_minus_one "#:unlimited"
	    set param_set_minus_one ""
	}
	default {
	    error "invalid param: $param"
	}
    }

    gdb_test_no_output "set $param 1" "test set to 1"

    gdb_test "guile (print (parameter-value \"$param\"))" \
	1 "test value of 1"

    gdb_test_no_output "set $param 0" "test set to 0"

    gdb_test "guile (print (parameter-value \"$param\"))" \
	$param_get_zero "test value of 0"

    scm_param_test_maybe_no_output "set $param -1" \
	$param_set_minus_one "test set to -1"

    gdb_test "guile (print (parameter-value \"$param\"))" \
	$param_get_minus_one "test value of -1"

    gdb_test_no_output "set $param unlimited" "test set to 'unlimited'"

    gdb_test "guile (print (parameter-value \"$param\"))" \
	"#:unlimited" "test value of 'unlimited'"

    if {$param == "print characters"} {
	gdb_test_no_output "set $param elements" "test set to 'elements'"

	gdb_test "guile (print (parameter-value \"$param\"))" \
	    "#:elements" "test value of 'elements'"
    }
}

foreach_with_prefix kind {
    PARAM_UINTEGER
    PARAM_ZINTEGER
    PARAM_ZUINTEGER
    PARAM_ZUINTEGER_UNLIMITED
} {
    gdb_test_multiline "create gdb parameter" \
	"guile" "" \
	"(define test-$kind-param" "" \
	"  (make-parameter \"print test-$kind-param\"" "" \
	"   #:command-class COMMAND_DATA" "" \
	"   #:parameter-type $kind" "" \
	"   #:doc \"Set to a number or 'unlimited' to yield an effect.\"" "" \
	"   #:show-doc \"Show the state of $kind.\"" "" \
	"   #:set-doc \"Set the state of $kind.\"" "" \
	"   #:show-func (lambda (self value)" "" \
	"      (format #f \"The state of $kind is ~a.\" value))" "" \
	"   #:initial-value 3))" "" \
	"(register-parameter! test-$kind-param)" "" \
	"end"

    set param_integer_error \
	[multi_line \
	    "ERROR: In procedure set-parameter-value!:" \
	    "(ERROR: )?In procedure gdbscm_set_parameter_value_x:\
	     Wrong type argument in position 2 \\(expecting integer\\):\
	     #:unlimited" \
	    "Error while executing Scheme code\\."]
    set param_minus_one_error "integer -1 out of range"
    set param_minus_two_error "integer -2 out of range"
    switch -- $kind {
	PARAM_UINTEGER {
	    set param_get_zero "#:unlimited"
	    set param_get_minus_one "#:unlimited"
	    set param_get_minus_two "#:unlimited"
	    set param_str_unlimited unlimited
	    set param_set_unlimited ""
	    set param_set_minus_one $param_minus_one_error
	    set param_set_minus_two $param_minus_two_error
	}
	PARAM_ZINTEGER {
	    set param_get_zero 0
	    set param_get_minus_one -1
	    set param_get_minus_two -2
	    set param_str_unlimited 2
	    set param_set_unlimited $param_integer_error
	    set param_set_minus_one ""
	    set param_set_minus_two ""
	}
	PARAM_ZUINTEGER {
	    set param_get_zero 0
	    set param_get_minus_one 0
	    set param_get_minus_two 0
	    set param_str_unlimited 2
	    set param_set_unlimited $param_integer_error
	    set param_set_minus_one $param_minus_one_error
	    set param_set_minus_two $param_minus_two_error
	}
	PARAM_ZUINTEGER_UNLIMITED {
	    set param_get_zero 0
	    set param_get_minus_one "#:unlimited"
	    set param_get_minus_two "#:unlimited"
	    set param_str_unlimited unlimited
	    set param_set_unlimited ""
	    set param_set_minus_one ""
	    set param_set_minus_two $param_minus_two_error
	}
	default {
	    error "invalid kind: $kind"
	}
    }

    with_test_prefix "test-$kind-param" {
	gdb_test "guile (print (parameter-value test-$kind-param))" \
	    3 "$kind parameter value (3)"
	gdb_test "show print test-$kind-param" \
	    "The state of $kind is 3." "show initial value"
	gdb_test_no_output "set print test-$kind-param 2"
	gdb_test "show print test-$kind-param" \
	    "The state of $kind is 2." "show new value"
	gdb_test "guile (print (parameter-value test-$kind-param))" \
	    2 "$kind parameter value (2)"
	scm_param_test_maybe_no_output \
	    "guile (set-parameter-value! test-$kind-param #:unlimited)" \
	    $param_set_unlimited
	gdb_test "show print test-$kind-param" \
	    "The state of $kind is $param_str_unlimited." \
	    "show unlimited value"
	gdb_test_no_output "guile (set-parameter-value! test-$kind-param 1)"
	gdb_test "guile (print (parameter-value test-$kind-param))" \
	    1 "$kind parameter value (1)"
	gdb_test_no_output "guile (set-parameter-value! test-$kind-param 0)"
	gdb_test "guile (print (parameter-value test-$kind-param))" \
	    $param_get_zero "$kind parameter value (0)"
	scm_param_test_maybe_no_output "set print test-$kind-param -1" \
	    $param_set_minus_one
	gdb_test "guile (print (parameter-value test-$kind-param))" \
	    $param_get_minus_one "$kind parameter value (-1)"
	scm_param_test_maybe_no_output "set print test-$kind-param -2" \
	    $param_set_minus_two
	gdb_test "guile (print (parameter-value test-$kind-param))" \
	    $param_get_minus_two "$kind parameter value (-2)"
    }
}

# Test a file parameter.

gdb_test_multiline "file gdb parameter" \
    "guile" "" \
    "(define test-file-param" "" \
    "  (make-parameter \"test-file-param\"" "" \
    "   #:command-class COMMAND_FILES" "" \
    "   #:parameter-type PARAM_FILENAME" "" \
    "   #:doc \"When set, test param does something useful. When disabled, does nothing.\"" "" \
    "   #:show-doc \"Show the name of the file.\"" "" \
    "   #:set-doc \"Set the name of the file.\"" "" \
    "   #:show-func (lambda (self value)" "" \
    "      (format #f \"The name of the file is ~a.\" value))" "" \
    "   #:initial-value \"foo.txt\"))" "" \
    "(register-parameter! test-file-param)" "" \
    "end"

with_test_prefix "test-file-param" {
    gdb_test "guile (print (parameter-value test-file-param))" "foo.txt" "initial parameter value"
    gdb_test "show test-file-param" "The name of the file is foo.txt." "show initial value"
    gdb_test_no_output "set test-file-param bar.txt"
    gdb_test "show test-file-param" "The name of the file is bar.txt." "show new value"
    gdb_test "guile (print (parameter-value test-file-param))" "bar.txt" " new parameter value"
    gdb_test "set test-file-param" "Argument required.*" 
}

# Test a parameter that is not documented.

gdb_test_multiline "undocumented gdb parameter" \
    "guile" "" \
    "(register-parameter! (make-parameter \"print test-undoc-param\"" "" \
    "   #:command-class COMMAND_DATA" "" \
    "   #:parameter-type PARAM_BOOLEAN" "" \
    "   #:show-func (lambda (self value)" "" \
    "      (format #f \"The state of the Test Parameter is ~a.\" value))" "" \
    "   #:initial-value #t))" "" \
    "end"

with_test_prefix "test-undocumented-param" {
    gdb_test "show print test-undoc-param" "The state of the Test Parameter is on." "show parameter on"
    gdb_test_no_output "set print test-undoc-param off"
    gdb_test "show print test-undoc-param" "The state of the Test Parameter is off." "show parameter off"
    gdb_test "help show print test-undoc-param" "This command is not documented." "show help"
    gdb_test "help set print test-undoc-param" "This command is not documented." "set help"
    gdb_test "help set print" "set print test-undoc-param -- This command is not documented.*" "general help"
}

# Test a parameter with a restricted range, where we need to notify the user
# and restore the previous value.

gdb_test_multiline "restricted gdb parameter" \
    "guile" "" \
    "(register-parameter! (make-parameter \"test-restricted-param\"" "" \
    "   #:command-class COMMAND_DATA" "" \
    "   #:parameter-type PARAM_ZINTEGER" "" \
    "   #:set-func (lambda (self)" "" \
    "      (let ((value (parameter-value self)))" "" \
    "        (if (and (>= value 0) (<= value 10))" "" \
    "            \"\"" "" \
    "            (begin" "" \
    "              (set-parameter-value! self (object-property self 'value))" "" \
    "              \"Error: Range of parameter is 0-10.\"))))" "" \
    "   #:show-func (lambda (self value)" "" \
    "      (format #f \"The value of the restricted parameter is ~a.\" value))" "" \
    "   #:initial-value (lambda (self)" "" \
    "      (set-object-property! self 'value 2)" "" \
    "      2)))" "" \
    "end"

with_test_prefix "test-restricted-param" {
    gdb_test "show test-restricted-param" "The value of the restricted parameter is 2." \
	"test-restricted-param is initially 2"
    gdb_test_no_output "set test-restricted-param 10"
    gdb_test "show test-restricted-param" "The value of the restricted parameter is 10." \
	"test-restricted-param is now 10"
    gdb_test "set test-restricted-param 42" "Error: Range of parameter is 0-10."
    gdb_test "show test-restricted-param" "The value of the restricted parameter is 2." \
	"test-restricted-param is back to 2 again"
}

# Test registering a parameter that already exists.

gdb_test "guile (register-parameter! (make-parameter \"height\"))" \
    "ERROR.*is already defined.*" "error registering existing parameter"

# Test printing and setting the value of an unregistered parameter.
gdb_test "guile (print (parameter-value (make-parameter \"foo\")))" \
    "= #f"
gdb_test "guile (define myparam (make-parameter \"foo\"))"
gdb_test_no_output "guile (set-parameter-value! myparam #t)"
gdb_test "guile (print (parameter-value myparam))" \
    "= #t"

# Test registering a parameter named with what was an ambiguous spelling
# of existing parameters.

gdb_test_multiline "previously ambiguously named boolean parameter" \
    "guile" "" \
    "(define prev-ambig" "" \
    "  (make-parameter \"print s\"" "" \
    "   #:parameter-type PARAM_BOOLEAN))" "" \
    "end"

gdb_test_no_output "guile (register-parameter! prev-ambig)"

with_test_prefix "previously-ambiguous" {
    gdb_test "guile (print (parameter-value prev-ambig))" "= #f" "parameter value (false)"
    gdb_test "show print s" "Command is not documented is off." "show parameter off"
    gdb_test_no_output "set print s on"
    gdb_test "show print s" "Command is not documented is on." "show parameter on"
    gdb_test "guile (print (parameter-value prev-ambig))" "= #t" "parameter value (true)"
    gdb_test "help show print s" "This command is not documented." "show help"
    gdb_test "help set print s" "This command is not documented." "set help"
    gdb_test "help set print" "set print s -- This command is not documented.*" "general help"
}

rename scm_param_test_maybe_no_output ""