| 12
 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
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 
 | # Copyright 1998-2016 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/>.
load_lib "trace-support.exp"
standard_testfile unavailable.cc
set executable $testfile
if {[prepare_for_testing $testfile.exp $testfile $srcfile \
	 {debug nowarnings c++}]} {
    return -1
}
set ws "\[\r\n\t \]+"
set cr "\[\r\n\]+"
#
# Utility procs
#
proc test_register { reg } {
    global gdb_prompt
    global hex
    global cr
    gdb_test_multiple "print /x $reg" "collected $reg" {
	-re "\\$\[0-9\]+ = \[x0\]+$cr$gdb_prompt $" {
	    fail "collected $reg (zero)"
	}
	-re "\\$\[0-9\]+ = $hex$cr$gdb_prompt $" {
	    pass "collected $reg"
	}
	-re "\[Ee\]rror.*$gdb_prompt $" {
	    fail "collected $reg (error)"
	}
    }
}
proc test_register_unavailable { reg } {
    gdb_test "print /x $reg" \
	"<unavailable>" \
	"correctly report $reg as unavailable"
}
proc prepare_for_trace_test {} {
    global executable
    clean_restart $executable
    runto_main
    gdb_test "break begin" ".*" ""
    gdb_test "break end" ".*" ""
}
proc run_trace_experiment { test_func } {
    global gdb_prompt
    gdb_test "continue" \
	".*Breakpoint \[0-9\]+, begin .*" \
	"advance to begin"
    gdb_test_no_output "tstart" "start trace experiment"
    gdb_test "continue" \
	    "Continuing.*Breakpoint \[0-9\]+, end.*" \
	    "run trace experiment"
    gdb_test "tstop" \
	    "\[\r\n\]+" \
	    "stop trace experiment"
    gdb_test "tfind start" \
	    "#0  $test_func .*" \
	    "tfind test frame"
}
# Test that "display VAR" works as expected, assuming VAR is wholly
# unavailable.
proc test_maybe_regvar_display { var } {
    global gdb_prompt
    # Evaluating VAR's location description may throw an internal
    # "unavailable" exception, if for example, the value of a register
    # necessary for computing VAR's location is unavailable.  Such an
    # exception is caught, and should not cause automatic disablement
    # of the current display being printed.  (GDB used to disable the
    # current display whenever any exception was thrown.)
    set test "display $var"
    gdb_test_multiple "$test" "$test" {
	-re "Disabling display ? to avoid infinite recursion.*$gdb_prompt $" {
	    fail "$test"
	}
	-re "display ${var}\r\n1: ${var} = <unavailable>\r\n$gdb_prompt $" {
	    pass "$test"
	}
    }
    gdb_test "info display" ".*1:\[ \t\]+y\[ \t\]+${var}" "display ${var} is enabled"
    gdb_test "undisp" \
	"" \
	"delete $var display" \
	".*Delete all auto-display expressions.*y or n. $" \
	"y"
}
#
# Test procs
#
proc gdb_collect_args_test_1 {} {
    global cr
    # Test printing the variables, and also their addresses.  We
    # haven't collected any stack, so there's no way GDB can figure
    # out the latter.
    gdb_test "print argc" " = <unavailable>"
    gdb_test "print &argc" \
	"Can't take address of \"argc\" which isn't an lvalue\."
    gdb_test "print argi" " = <unavailable>"
    gdb_test "print &argi" \
	"Can't take address of \"argi\" which isn't an lvalue\."
    gdb_test "print argf" " = <unavailable>"
    gdb_test "print &argf" \
	"Can't take address of \"argf\" which isn't an lvalue\."
    gdb_test "print argd" " = <unavailable>"
    gdb_test "print &argd" \
	"Can't take address of \"argd\" which isn't an lvalue\."
    # struct arg as one of several args (near end of list)
    gdb_test "print argstruct" " = <unavailable>"
    gdb_test "print argstruct.memberc" " = <unavailable>"
    gdb_test "print argstruct.memberi" " = <unavailable>"
    gdb_test "print argstruct.memberf" " = <unavailable>"
    gdb_test "print argstruct.memberd" " = <unavailable>"
    gdb_test "print argarray" " = <unavailable>"
    gdb_test "print &argarray" \
	"Can't take address of \"argarray\" which isn't an lvalue\."
    gdb_test "print argarray\[0\]" "value is not available"
    # Test "info args"
    set r ""
    set r "${r}argc = <unavailable>${cr}"
    set r "${r}argi = <unavailable>${cr}"
    set r "${r}argf = <unavailable>${cr}"
    set r "${r}argd = <unavailable>${cr}"
    set r "${r}argstruct = <unavailable>${cr}"
    set r "${r}argarray = <unavailable>${cr}"
    gdb_test "info args" "$r" "info args"
    test_maybe_regvar_display "argc"
}
proc gdb_collect_args_test {} {
    with_test_prefix "unavailable arguments" {
	global gdb_prompt
	global testfile srcdir subdir binfile
	global trace_file_targets
	prepare_for_trace_test
	gdb_test "trace args_test_func" \
	    "Tracepoint \[0-9\]+ at .*" \
	    "set tracepoint"
	# Begin the test.
	run_trace_experiment args_test_func
	gdb_collect_args_test_1
	gdb_test "tfind none" \
	    "#0  end .*" \
	    "cease trace debugging"
	set tracefile [standard_output_file ${testfile}]
	gdb_test "tsave ${tracefile}.args.tfile" \
	    "Trace data saved to file '${tracefile}.args.tfile'\.\\r" \
	    "tsave ${testfile}.args.tfile"
	gdb_test "tsave -ctf ${tracefile}.args.ctf" \
	    "Trace data saved to directory '${tracefile}.args.ctf'\.\\r" \
	    "save ctf trace file"
	foreach target_name ${trace_file_targets} {
	    # Restart GDB and read the trace data in ${TARGET_NAME} target.
	    gdb_exit
	    gdb_start
	    gdb_reinitialize_dir $srcdir/$subdir
	    gdb_file_cmd $binfile
	    gdb_test "target ${target_name} ${tracefile}.args.${target_name}" ".*" \
		"change to ${target_name} target"
	    with_test_prefix "${target_name}" {
		gdb_test "tfind start" "#0  args_test_func .*" \
		    "tfind test frame"
		gdb_collect_args_test_1
	    }
	}
    }
}
proc gdb_collect_locals_test_1 { func } {
    global cr
    gdb_test "print locc" " = <unavailable>"
    gdb_test "print loci" " = <unavailable>"
    gdb_test "print locf" " = <unavailable>"
    gdb_test "print locd" " = <unavailable>"
    gdb_test "print locst.memberc" " = <unavailable>"
    gdb_test "print locst.memberi" " = <unavailable>"
    gdb_test "print locst.memberf" " = <unavailable>"
    gdb_test "print locst.memberd" " = <unavailable>"
    gdb_test "print locar\[0\]" " = <unavailable>"
    gdb_test "print locar\[1\]" " = <unavailable>"
    gdb_test "print locar\[2\]" " = <unavailable>"
    gdb_test "print locar\[3\]" " = <unavailable>"
    # Test "info locals"
    set r ""
    set r "${r}locf = <unavailable>${cr}"
    set r "${r}locd = <unavailable>${cr}"
    set r "${r}locst = <unavailable>${cr}"
    set r "${r}locar = <unavailable>${cr}"
    set r "${r}i = <unavailable>${cr}"
    if { $func == "local_test_func" } {
	set r "${r}locdefst = <unavailable>${cr}"
    }
    set r "${r}locc = <unavailable>${cr}"
    set r "${r}loci = <unavailable>${cr}"
    gdb_test "info locals" "$r" "info locals"
    test_maybe_regvar_display "loci"
}
proc gdb_collect_locals_test { func msg } {
    with_test_prefix "unavailable locals: $msg" {
	global gdb_prompt
	global testfile srcdir subdir binfile
	global trace_file_targets
	prepare_for_trace_test
	set testline [gdb_get_line_number "set $func tracepoint here"]
	gdb_test "trace $testline" \
	    "Tracepoint \[0-9\]+ at .*" \
	    "set tracepoint"
	# Begin the test.
	run_trace_experiment $func
	gdb_collect_locals_test_1 $func
	gdb_test "tfind none" \
	    "#0  end .*" \
	    "cease trace debugging"
	set tracefile [standard_output_file ${testfile}]
	gdb_test "tsave ${tracefile}.locals.tfile" \
	    "Trace data saved to file '${tracefile}.locals.tfile'\.\\r" \
	    "tsave ${testfile}.locals.tfile"
	gdb_test "tsave -ctf ${tracefile}.locals.ctf" \
	    "Trace data saved to directory '${tracefile}.locals.ctf'\.\\r" \
	    "save ctf trace file"
	foreach target_name ${trace_file_targets} {
	    # Restart GDB and read the trace data in ${TARGET_NAME} target.
	    gdb_exit
	    gdb_start
	    gdb_reinitialize_dir $srcdir/$subdir
	    gdb_file_cmd $binfile
	    gdb_test "target ${target_name} ${tracefile}.locals.${target_name}" ".*" \
		"change to ${target_name} target"
	    with_test_prefix "${target_name}" {
		gdb_test "tfind start" "#0  $func .*" \
		    "tfind test frame"
		gdb_collect_locals_test_1 $func
	    }
	}
    }
}
proc gdb_unavailable_registers_test_1 { } {
    global spreg
    global pcreg
    # On some archs, the $sp/$pc are a real raw registers.  On others,
    # like x86, they're user registers.  Test both variants.
    test_register_unavailable "\$$spreg"
    test_register_unavailable "\$sp"
    # Test reading uncollected pseudo-registers.  The set of which
    # depends on target.
    if [is_amd64_regs_target] {
	# Check the raw register first.
	test_register_unavailable "\$rax"
	test_register_unavailable "\$eax"
	test_register_unavailable "\$ax"
    } elseif [is_x86_like_target] {
	# Check the raw register first.
	test_register_unavailable "\$eax"
	test_register_unavailable "\$ax"
    }
    # GDBserver always provides the PC value of regular tracepoint
    # hits, since it's the same as the tracepoint's address.
    test_register "\$$pcreg"
    test_register "\$pc"
    gdb_test "info registers" \
	".*<unavailable>.*<unavailable>.*" \
	"info registers, multiple registers not available"
    gdb_test "info registers \$$spreg" \
	"<unavailable>" \
	"info registers \$$spreg reports not available"
}
proc gdb_unavailable_registers_test { } {
    with_test_prefix "unavailable registers" {
	global testfile srcdir subdir binfile
	global trace_file_targets
	prepare_for_trace_test
	# We'll simply re-use the globals_test_function for this test
	gdb_test "trace globals_test_func" \
	    "Tracepoint \[0-9\]+ at .*" \
	    "set tracepoint"
	# Collect nothing.
	# Begin the test.
	run_trace_experiment globals_test_func
	gdb_unavailable_registers_test_1
	gdb_test "tfind none" "#0  end .*" "cease trace debugging"
	set tracefile [standard_output_file ${testfile}]
	gdb_test "tsave ${tracefile}.registers.tfile" \
	    "Trace data saved to file '${tracefile}.registers.tfile'\.\\r" \
	    "tsave ${testfile}.registers.tfile"
	gdb_test "tsave -ctf ${tracefile}.registers.ctf" \
	    "Trace data saved to directory '${tracefile}.registers.ctf'\.\\r" \
	    "save ctf trace file"
	foreach target_name ${trace_file_targets} {
	    # Restart GDB and read the trace data in ${TARGET_NAME} target.
	    gdb_exit
	    gdb_start
	    gdb_reinitialize_dir $srcdir/$subdir
	    gdb_file_cmd $binfile
	    gdb_test "target ${target_name} ${tracefile}.registers.${target_name}" ".*" \
		"change to ${target_name} target"
	    with_test_prefix "${target_name}" {
		gdb_test "tfind start" "#0  globals_test_func .*" \
		    "tfind test frame"
		gdb_unavailable_registers_test_1
	    }
	}
    }
}
proc gdb_unavailable_floats_1 { } {
    global gdb_prompt
    # Necessarily target specific.
    if {[istarget "x86_64-*-*"] || [istarget i?86-*]} {
	send_gdb "info float\n"
	gdb_expect_list "info float" ".*$gdb_prompt $" {
	    "Status Word:         <unavailable>"
	    "Control Word:        <unavailable>"
	    "Tag Word:            <unavailable>"
	    "Instruction Pointer: <unavailable>:<unavailable>"
	    "Operand Pointer:     <unavailable>:<unavailable>"
	    "Opcode:              <unavailable>"
	}
    }
}
proc gdb_unavailable_floats { } {
    with_test_prefix "unavailable floats" {
	global testfile srcdir subdir binfile
	global trace_file_targets
	prepare_for_trace_test
	# We'll simply re-use the globals_test_function for this test
	gdb_test "trace globals_test_func" \
	    "Tracepoint \[0-9\]+ at .*" \
	    "set tracepoint"
	# Collect nothing.
	# Begin the test.
	run_trace_experiment globals_test_func
	gdb_unavailable_floats_1
	gdb_test "tfind none" "#0  end .*" "cease trace debugging"
	set tracefile [standard_output_file ${testfile}]
	gdb_test "tsave ${tracefile}.floats.tfile" \
	    "Trace data saved to file '${tracefile}.floats.tfile'\.\\r" \
	    "tsave ${testfile}.floats.tfile"
	gdb_test "tsave -ctf ${tracefile}.floats.ctf" \
	    "Trace data saved to directory '${tracefile}.floats.ctf'\.\\r" \
	    "save ctf trace file"
	foreach target_name ${trace_file_targets} {
	    # Restart GDB and read the trace data in ${TARGET_NAME} target.
	    gdb_exit
	    gdb_start
	    gdb_reinitialize_dir $srcdir/$subdir
	    gdb_file_cmd $binfile
	    gdb_test "target ${target_name} ${tracefile}.floats.${target_name}" ".*" \
		"change to ${target_name} target"
	    with_test_prefix "${target_name}" {
		gdb_test "tfind start" "#0  globals_test_func .*" \
		    "tfind test frame"
		gdb_unavailable_floats_1
	    }
	}
    }
}
proc gdb_collect_globals_test_1 { } {
    global ws
    global cr
    global gdb_prompt
    global hex
    gdb_test "print globalc" " = <unavailable>"
    gdb_test "print globali" " = <unavailable>"
    gdb_test "print globalf" " = <unavailable>"
    gdb_test "print globald" " = <unavailable>"
    gdb_test "print globalstruct.memberc" " = <unavailable>"
    gdb_test "print globalstruct.memberi" " = <unavailable>"
    gdb_test "print globalstruct.memberf" " = <unavailable>"
    gdb_test "print globalstruct.memberd" " = <unavailable>"
    gdb_test "print globalstruct" " = <unavailable>"
    gdb_test "print globalp == &globalstruct" \
	"value is not available" \
	"can't compare using non collected global pointer"
    gdb_test "print globalarr\[1\]" " = <unavailable>"
    gdb_test "print globalarr\[2\]" " = <unavailable>"
    gdb_test "print globalarr\[3\]" " = <unavailable>"
    gdb_test "print struct_b" \
	" = {d = <unavailable>, ef = <unavailable>, struct_a = {a = <unavailable>, b = <unavailable>, array = {<unavailable>, <unavailable>, -1431655766, <unavailable> <repeats 97 times>, -1431655766, <unavailable> <repeats 9899 times>}, ptr = <unavailable>, bitfield = <unavailable>}, s = <unavailable>, static static_struct_a = {a = <unavailable>, b = <unavailable>, array = {<unavailable> <repeats 10000 times>}, ptr = <unavailable>, bitfield = <unavailable>}, string = <unavailable>}"
    gdb_test "print /x struct_b" \
	" = {d = <unavailable>, ef = <unavailable>, struct_a = {a = <unavailable>, b = <unavailable>, array = {<unavailable>, <unavailable>, 0xaaaaaaaa, <unavailable> <repeats 97 times>, 0xaaaaaaaa, <unavailable> <repeats 9899 times>}, ptr = <unavailable>, bitfield = <unavailable>}, s = <unavailable>, static static_struct_a = {a = <unavailable>, b = <unavailable>, array = {<unavailable> <repeats 10000 times>}, ptr = <unavailable>, bitfield = <unavailable>}, string = <unavailable>}"
    gdb_test "print /x struct_b.struct_a" \
	" = {a = <unavailable>, b = <unavailable>, array = {<unavailable>, <unavailable>, 0xaaaaaaaa, <unavailable> <repeats 97 times>, 0xaaaaaaaa, <unavailable> <repeats 9899 times>}, ptr = <unavailable>, bitfield = <unavailable>}"
    gdb_test "print /x struct_b.struct_a.array" \
	" = {<unavailable>, <unavailable>, 0xaaaaaaaa, <unavailable> <repeats 97 times>, 0xaaaaaaaa, <unavailable> <repeats 9899 times>}"
    gdb_test "print /x struct_b.struct_a.array\[0\]" " = <unavailable>"
    gdb_test "print /x struct_b.struct_a.array\[2\]" " = 0xaaaaaaaa"
    # Check the target doesn't overcollect.  GDB used to merge memory
    # ranges to collect if they were close enough (collecting the hole
    # as well), but does not do that anymore.  It's plausible that a
    # target may do this on its end, but as of this writing, no known
    # target does it.
    gdb_test "print {a, b, c}" \
	" = \\{1, <unavailable>, 3\\}" \
	"No overcollect of almost but not quite adjacent memory ranges"
    # Check <unavailable> isn't confused with 0 in array element repetitions
    gdb_test_no_output "set print repeat 1"
    gdb_test "print /x tarray" \
	" = \{\{a = 0x0, b = <unavailable>\} <repeats 2 times>, \{a = <unavailable>, b = <unavailable>\}, \{a = 0x0, b = 0x0\}, \{a = <unavailable>, b = 0x0\} <repeats 2 times>, \{a = <unavailable>, b = <unavailable>\} <repeats 2 times>\}" \
	"<unavailable> is not the same as 0 in array element repetitions"
    gdb_test_no_output "set print repeat 10"
    # Check that value repeat handles unavailable-ness.
    gdb_test "print *tarray@3" " = \\{\\{a = 0, b = <unavailable>\\}, \\{a = 0, b = <unavailable>\\}, \\{a = <unavailable>, b = <unavailable>\\}\\}"
    # Static fields
    gdb_test "print struct_b.static_struct_a" " = <unavailable>"
    # Bitfields
    gdb_test "print struct_b.struct_a.bitfield" " = <unavailable>"
    # References
    gdb_test "print g_int" " = <unavailable>"
    gdb_test "print g_ref" \
	"\\(int &\\) @$hex: <unavailable>" \
	"global reference shows address but not value"
    gdb_test "print *&g_ref" \
	"\\$\[0-9\]+ = <unavailable>$cr" \
	"referenced integer was not collected (taking address of reference)"
    gdb_test "print *g_structref_p" " = <unavailable>"
    # Strings
    # Const string is always available, even when not collected.
    gdb_test "print g_const_string" \
	" = \"hello world\"$cr" \
	"non collected const string is still printable"
    gdb_test "print g_string_p" \
	" = $hex <g_const_string> \"hello world\"" \
	"printing constant string through collected pointer"
    gdb_test "print g_string_unavail" \
	" = <unavailable>" \
	"printing non collected string"
    # Incomplete strings print as an array.
    gdb_test "print g_string_partial" \
	"\\$\[0-9\]+ = \{<unavailable>, 101 'e', 108 'l', <unavailable>, <unavailable>, <unavailable>, <unavailable>, <unavailable>, <unavailable>, <unavailable>, <unavailable>, <unavailable>\}" \
	"printing partially collected string"
    # It is important for this test that the last examined value is
    # <unavailable>, to exercise the case of the $__ convenience
    # variable being set to <unavailable> without error.
    set msg "examining partially collected object"
    gdb_test_multiple "x /10x &struct_b" "$msg" {
	-re "$hex <struct_b>:${ws}<unavailable>${ws}<unavailable>${ws}<unavailable>${ws}<unavailable>$cr$hex <struct_b\\+16>:${ws}<unavailable>${ws}<unavailable>${ws}0xaaaaaaaa${ws}<unavailable>$cr$hex <struct_b\\+32>:${ws}<unavailable>${ws}<unavailable>$cr$gdb_prompt $" {
	    pass "$msg"
	}
	-re "value is not available" {
	    fail "$msg"
	}
    }
    gdb_test "p \$__" " = <unavailable>" "last examined value was <unavailable>"
    # This tests that building the array does not require accessing
    # g_int's contents.
    gdb_test "print { 1, g_int, 3 }" \
	" = \\{1, <unavailable>, 3\\}" \
	"build array from unavailable value"
    # Note, depends on previous test.
    gdb_test "print \$\[1\]" \
	" = <unavailable>" \
	"subscript a non-memory rvalue array, accessing an unvailable element"
    # Access a field of a non-lazy value, making sure the
    # unavailable-ness is propagated.  History values are easy
    # non-lazy values, so use those.  The first test just sets up for
    # the second.
    gdb_test "print g_smallstruct" " = <unavailable>"
    gdb_test "print \$.member" " = <unavailable>"
    # Cast to baseclass, checking the unavailable-ness is propagated.
    gdb_test "print (small_struct) g_smallstruct_b" " = <unavailable>"
    # Same cast, but starting from a non-lazy, value.
    gdb_test "print g_smallstruct_b" " = <unavailable>"
    gdb_test "print (small_struct) \$" " = <unavailable>"
    gdb_test_no_output "set print object on"
    with_test_prefix "print object on" {
	# With print object on, printing a pointer may need to fetch
	# the pointed-to object, to check its run-time type.  Make
	# sure that fails gracefully and transparently when the
	# pointer itself is unavailable.
	gdb_test "print virtualp" " = <unavailable>"
	# no vtable pointer available
	gdb_test "print derived_unavail" " = <unavailable>"
	# vtable pointer available, but nothing else
	gdb_test "print derived_partial" \
	    " = \\(Derived\\) {<Middle> = {<Base> = <unavailable>, _vptr.Middle = <unavailable>, y = <unavailable>}, _vptr.Derived = $hex <vtable for Derived.*>, z = <unavailable>}"
	# whole object available
	gdb_test "print derived_whole" \
	    " = \\(Derived\\) {<Middle> = {<Base> = {x = 2}, _vptr.Middle = ${hex}( <\[^>]*>)?, y = 3}, _vptr.Derived = $hex <vtable for Derived.*>, z = 4}"
    }
    gdb_test_no_output "set print object off"
    with_test_prefix "print object off" {
	gdb_test "print virtualp" " = <unavailable>"
	# no vtable pointer available
	gdb_test "print derived_unavail" \
	    " = <unavailable>"
	# vtable pointer available, but nothing else
	gdb_test "print derived_partial" \
	    " = {<Middle> = {<Base> = <unavailable>, _vptr.Middle = <unavailable>, y = <unavailable>}, _vptr.Derived = $hex <vtable for Derived.*>, z = <unavailable>}"
	# whole object available
	gdb_test "print derived_whole" \
	    " = {<Middle> = {<Base> = {x = 2}, _vptr.Middle = ${hex}( <\[^>]*>)?, y = 3}, _vptr.Derived = $hex <vtable for Derived.*>, z = 4}"
    }
    # An instance of a virtual class where we collected everything but
    # the vptr.
    gdb_test "print virtual_partial" \
	" = {_vptr.Virtual = <unavailable>, z = 0}"
}
proc gdb_collect_globals_test { } {
    with_test_prefix "collect globals" {
	global testfile binfile srcdir subdir
	global trace_file_targets
	prepare_for_trace_test
	set testline [gdb_get_line_number "set globals_test_func tracepoint here"]
	gdb_test "trace $testline" \
	    "Tracepoint \[0-9\]+ at .*" \
	    "set tracepoint"
	# We collect the initial sizeof(pointer) bytes of derived_partial
	# in an attempt of collecting the vptr.  Not portable, but should
	# work everywhere we need to care.
	gdb_trace_setactions "define actions" \
	    "" \
	    "collect struct_b.struct_a.array\[2\]" "^$" \
	    "collect struct_b.struct_a.array\[100\]" "^$" \
	    \
	    "collect a" "^$" \
	    "collect c" "^$" \
	    \
	    "collect tarray\[0\].a" "^$" \
	    "collect tarray\[1\].a" "^$" \
	    "collect tarray\[3\].a" "^$" \
	    "collect tarray\[3\].b" "^$" \
	    "collect tarray\[4\].b" "^$" \
	    "collect tarray\[5\].b" "^$" \
	    \
	    "collect g_string_p" "^$" \
	    "collect g_string_partial\[1\]" "^$" \
	    "collect g_string_partial\[2\]" "^$" \
	    \
	    "collect g_structref_p" "^$" \
	    \
	    "collect *((char *)&derived_partial)@sizeof\(void *\)" "^$" \
	    "collect derived_whole" "^$" \
	    \
	    "collect virtual_partial.z" "^$"
	# Begin the test.
	run_trace_experiment globals_test_func
	gdb_collect_globals_test_1
	gdb_test "tfind none" \
	    "#0  end .*" \
	    "cease trace debugging"
	set tracefile [standard_output_file ${testfile}]
	gdb_test "tsave ${tracefile}.globals.tfile" \
	    "Trace data saved to file '${tracefile}.globals.tfile'\.\\r" \
	    "tsave ${testfile}.globals.tfile"
	gdb_test "tsave -ctf ${tracefile}.globals.ctf" \
	    "Trace data saved to directory '${tracefile}.globals.ctf'\.\\r" \
	    "save ctf trace file"
	foreach target_name ${trace_file_targets} {
	    # Restart GDB and read the trace data in ${TARGET_NAME} target.
	    gdb_exit
	    gdb_start
	    gdb_reinitialize_dir $srcdir/$subdir
	    gdb_file_cmd $binfile
	    gdb_test "target ${target_name} ${tracefile}.globals.${target_name}" ".*" \
		"change to ${target_name} target"
	    with_test_prefix "${target_name}" {
		gdb_test "tfind start" "#0  globals_test_func .*" \
		    "tfind test frame"
		gdb_collect_globals_test_1
	    }
	}
    }
}
proc gdb_trace_collection_test {} {
    gdb_collect_globals_test
    gdb_unavailable_registers_test
    gdb_unavailable_floats
    gdb_collect_args_test
    gdb_collect_locals_test local_test_func "auto locals"
    gdb_collect_locals_test reglocal_test_func "register locals"
    gdb_collect_locals_test statlocal_test_func "static locals"
}
runto_main
if { ![gdb_target_supports_trace] } then {
    unsupported "Current target does not support trace"
    return 1
}
set trace_file_targets [list "tfile"]
gdb_test_multiple "target ctf" "" {
    -re "Undefined target command: \"ctf\"\.  Try \"help target\"\.\r\n$gdb_prompt $" {
    }
    -re "No CTF directory specified.*\r\n$gdb_prompt $" {
	lappend trace_file_targets "ctf"
    }
}
# Body of test encased in a proc so we can return prematurely.
gdb_trace_collection_test
# Finished!
gdb_test "tfind none" ".*" ""
 |