ed14798ba7b6c86a4b7860989886018f6c50e523
[platform/upstream/binutils.git] / gdb / testsuite / gdb.trace / unavailable.exp
1 # Copyright 1998-2013 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 load_lib "trace-support.exp"
17
18 standard_testfile unavailable.cc
19 set executable $testfile
20
21 if {[prepare_for_testing $testfile.exp $testfile $srcfile \
22          {debug nowarnings c++}]} {
23     return -1
24 }
25
26 set ws "\[\r\n\t \]+"
27 set cr "\[\r\n\]+"
28
29 if [is_amd64_regs_target] {
30     set fpreg "rbp"
31     set spreg "rsp"
32     set pcreg "rip"
33 } elseif [is_x86_like_target] {
34     set fpreg "ebp"
35     set spreg "esp"
36     set pcreg "eip"
37 } else {
38     set fpreg "fp"
39     set spreg "sp"
40     set pcreg "pc"
41 }
42
43 #
44 # Utility procs
45 #
46
47 proc test_register { reg } {
48     global gdb_prompt
49     global hex
50     global cr
51
52     gdb_test_multiple "print /x $reg" "collected $reg" {
53         -re "\\$\[0-9\]+ = \[x0\]+$cr$gdb_prompt $" {
54             fail "collected $reg (zero)"
55         }
56         -re "\\$\[0-9\]+ = $hex$cr$gdb_prompt $" {
57             pass "collected $reg"
58         }
59         -re "\[Ee\]rror.*$gdb_prompt $" {
60             fail "collected $reg (error)"
61         }
62     }
63 }
64
65 proc test_register_unavailable { reg } {
66     gdb_test "print /x $reg" \
67         "<unavailable>" \
68         "correctly report $reg as unavailable"
69 }
70
71 proc prepare_for_trace_test {} {
72     global executable
73
74     clean_restart $executable
75
76     runto_main
77
78     gdb_test "break begin" ".*" ""
79     gdb_test "break end" ".*" ""
80 }
81
82 proc run_trace_experiment { test_func } {
83     global gdb_prompt
84
85     gdb_test "continue" \
86         ".*Breakpoint \[0-9\]+, begin .*" \
87         "advance to begin"
88
89     gdb_test_no_output "tstart" "start trace experiment"
90
91     gdb_test "continue" \
92             "Continuing.*Breakpoint \[0-9\]+, end.*" \
93             "run trace experiment"
94     gdb_test "tstop" \
95             "\[\r\n\]+" \
96             "stop trace experiment"
97     gdb_test "tfind start" \
98             "#0  $test_func .*" \
99             "tfind test frame"
100 }
101
102 # Test that "display VAR" works as expected, assuming VAR is wholly
103 # unavailable.
104
105 proc test_maybe_regvar_display { var } {
106     global gdb_prompt
107
108     # Evaluating VAR's location description may throw an internal
109     # "unavailable" exception, if for example, the value of a register
110     # necessary for computing VAR's location is unavailable.  Such an
111     # exception is caught, and should not cause automatic disablement
112     # of the current display being printed.  (GDB used to disable the
113     # current display whenever any exception was thrown.)
114     set test "display $var"
115     gdb_test_multiple "$test" "$test" {
116         -re "Disabling display ? to avoid infinite recursion.*$gdb_prompt $" {
117             fail "$test"
118         }
119         -re "display ${var}\r\n1: ${var} = <unavailable>\r\n$gdb_prompt $" {
120             pass "$test"
121         }
122     }
123     gdb_test "info display" ".*1:\[ \t\]+y\[ \t\]+${var}" "display ${var} is enabled"
124
125     gdb_test "undisp" \
126         "" \
127         "delete $var display" \
128         ".*Delete all auto-display expressions.*y or n. $" \
129         "y"
130 }
131
132 #
133 # Test procs
134 #
135
136 proc gdb_collect_args_test {} {
137     with_test_prefix "unavailable arguments" {
138         global cr
139         global gdb_prompt
140
141         prepare_for_trace_test
142
143         gdb_test "trace args_test_func" \
144             "Tracepoint \[0-9\]+ at .*" \
145             "set tracepoint"
146
147         # Begin the test.
148         run_trace_experiment args_test_func
149
150         # Test printing the variables, and also their addresses.  We
151         # haven't collected any stack, so there's no way GDB can figure
152         # out the latter.
153
154         gdb_test "print argc" " = <unavailable>"
155         gdb_test "print &argc" \
156             "Can't take address of \"argc\" which isn't an lvalue\."
157
158         gdb_test "print argi" " = <unavailable>"
159         gdb_test "print &argi" \
160             "Can't take address of \"argi\" which isn't an lvalue\."
161
162         gdb_test "print argf" " = <unavailable>"
163         gdb_test "print &argf" \
164             "Can't take address of \"argf\" which isn't an lvalue\."
165
166         gdb_test "print argd" " = <unavailable>"
167         gdb_test "print &argd" \
168             "Can't take address of \"argd\" which isn't an lvalue\."
169
170         # struct arg as one of several args (near end of list)
171
172         gdb_test "print argstruct" \
173             " = \{memberc = <unavailable>, memberi = <unavailable>, memberf = <unavailable>, memberd = <unavailable>\}"
174
175         gdb_test "print argstruct.memberc" " = <unavailable>"
176         gdb_test "print argstruct.memberi" " = <unavailable>"
177         gdb_test "print argstruct.memberf" " = <unavailable>"
178         gdb_test "print argstruct.memberd" " = <unavailable>"
179
180         gdb_test "print argarray" " = \\(int \\*\\) <unavailable>"
181
182         gdb_test "print &argarray" \
183             "Can't take address of \"argarray\" which isn't an lvalue\."
184
185         gdb_test "print argarray\[0\]" "value is not available"
186
187         # Test "info args"
188         set r ""
189         set r "${r}argc = <unavailable>${cr}"
190         set r "${r}argi = <unavailable>${cr}"
191         set r "${r}argf = <unavailable>${cr}"
192         set r "${r}argd = <unavailable>${cr}"
193         set r "${r}argstruct = {memberc = <unavailable>, memberi = <unavailable>, memberf = <unavailable>, memberd = <unavailable>}${cr}"
194         set r "${r}argarray = <unavailable>${cr}"
195         gdb_test "info args" "$r" "info args"
196
197         test_maybe_regvar_display "argc"
198
199         gdb_test "tfind none" \
200             "#0  end .*" \
201             "cease trace debugging"
202     }
203 }
204
205 proc gdb_collect_locals_test { func msg } {
206     with_test_prefix "unavailable locals: $msg" {
207         global cr
208         global gdb_prompt
209
210         prepare_for_trace_test
211
212         set testline [gdb_get_line_number "set $func tracepoint here"]
213
214         gdb_test "trace $testline" \
215             "Tracepoint \[0-9\]+ at .*" \
216             "set tracepoint"
217
218         # Begin the test.
219         run_trace_experiment $func
220
221         gdb_test "print locc" " = <unavailable>"
222         gdb_test "print loci" " = <unavailable>"
223         gdb_test "print locf" " = <unavailable>"
224         gdb_test "print locd" " = <unavailable>"
225
226         gdb_test "print locst.memberc" " = <unavailable>"
227         gdb_test "print locst.memberi" " = <unavailable>"
228         gdb_test "print locst.memberf" " = <unavailable>"
229         gdb_test "print locst.memberd" " = <unavailable>"
230
231         gdb_test "print locar\[0\]" " = <unavailable>"
232         gdb_test "print locar\[1\]" " = <unavailable>"
233         gdb_test "print locar\[2\]" " = <unavailable>"
234         gdb_test "print locar\[3\]" " = <unavailable>"
235
236         # Test "info locals"
237         set r ""
238         set r "${r}locf = <unavailable>${cr}"
239         set r "${r}locd = <unavailable>${cr}"
240         set r "${r}locst = {memberc = <unavailable>, memberi = <unavailable>, memberf = <unavailable>, memberd = <unavailable>}${cr}"
241         set r "${r}locar = {<unavailable>, <unavailable>, <unavailable>, <unavailable>}${cr}"
242         set r "${r}i = <unavailable>${cr}"
243         if { $func == "local_test_func" } {
244             set r "${r}locdefst = {<No data fields>}${cr}"
245         }
246         set r "${r}locc = <unavailable>${cr}"
247         set r "${r}loci = <unavailable>${cr}"
248         gdb_test "info locals" "$r" "info locals"
249
250         test_maybe_regvar_display "loci"
251
252         gdb_test "tfind none" \
253             "#0  end .*" \
254             "cease trace debugging"
255     }
256 }
257
258 proc gdb_unavailable_registers_test { } {
259     with_test_prefix "unavailable registers" {
260         global gdb_prompt
261         global spreg
262         global pcreg
263
264         prepare_for_trace_test
265
266         # We'll simply re-use the globals_test_function for this test
267         gdb_test "trace globals_test_func" \
268             "Tracepoint \[0-9\]+ at .*" \
269             "set tracepoint"
270
271         # Collect nothing.
272
273         # Begin the test.
274         run_trace_experiment globals_test_func
275
276         # On some archs, the $sp/$pc are a real raw registers.  On others,
277         # like x86, they're user registers.  Test both variants.
278         test_register_unavailable "\$$spreg"
279         test_register_unavailable "\$sp"
280
281         # Test reading uncollected pseudo-registers.  The set of which
282         # depends on target.
283         if [is_amd64_regs_target] {
284             # Check the raw register first.
285             test_register_unavailable "\$rax"
286             test_register_unavailable "\$eax"
287             test_register_unavailable "\$ax"
288         } elseif [is_x86_like_target] {
289             # Check the raw register first.
290             test_register_unavailable "\$eax"
291             test_register_unavailable "\$ax"
292         }
293
294         # GDBserver always provides the PC value of regular tracepoint
295         # hits, since it's the same as the tracepoint's address.
296         test_register "\$$pcreg"
297         test_register "\$pc"
298
299         gdb_test "info registers" \
300             "<unavailable>.*<unavailable>" \
301             "info registers, multiple registers not available"
302
303         gdb_test "info registers \$$spreg" \
304             "<unavailable>" \
305             "info registers \$$spreg reports not available"
306
307         gdb_test "tfind none" "#0  end .*" "cease trace debugging"
308     }
309 }
310
311 proc gdb_unavailable_floats { } {
312     global gdb_prompt
313
314     with_test_prefix "unavailable floats" {
315         prepare_for_trace_test
316
317         # We'll simply re-use the globals_test_function for this test
318         gdb_test "trace globals_test_func" \
319             "Tracepoint \[0-9\]+ at .*" \
320             "set tracepoint"
321
322         # Collect nothing.
323
324         # Begin the test.
325         run_trace_experiment globals_test_func
326
327         # Necessarily target specific.
328         if {[istarget "x86_64-*-*"] || [istarget i?86-*]} {
329             send_gdb "info float\n"
330             gdb_expect_list "info float" ".*$gdb_prompt $" {
331                 "Status Word:         <unavailable>"
332                 "Control Word:        <unavailable>"
333                 "Tag Word:            <unavailable>"
334                 "Instruction Pointer: <unavailable>:<unavailable>"
335                 "Operand Pointer:     <unavailable>:<unavailable>"
336                 "Opcode:              <unavailable>"
337             }
338         }
339
340         gdb_test "tfind none" "#0  end .*" "cease trace debugging"
341     }
342 }
343
344 proc gdb_collect_globals_test { } {
345     with_test_prefix "collect globals" {
346         global ws
347         global cr
348         global gdb_prompt
349         global hex
350
351         prepare_for_trace_test
352
353         set testline [gdb_get_line_number "set globals_test_func tracepoint here"]
354
355         gdb_test "trace $testline" \
356             "Tracepoint \[0-9\]+ at .*" \
357             "set tracepoint"
358
359         # We collect the initial sizeof(pointer) bytes of derived_partial
360         # in an attempt of collecting the vptr.  Not portable, but should
361         # work everywhere we need to care.
362         gdb_trace_setactions "define actions" \
363             "" \
364             "collect struct_b.struct_a.array\[2\]" "^$" \
365             "collect struct_b.struct_a.array\[100\]" "^$" \
366             \
367             "collect a" "^$" \
368             "collect c" "^$" \
369             \
370             "collect tarray\[0\].a" "^$" \
371             "collect tarray\[1\].a" "^$" \
372             "collect tarray\[3\].a" "^$" \
373             "collect tarray\[3\].b" "^$" \
374             "collect tarray\[4\].b" "^$" \
375             "collect tarray\[5\].b" "^$" \
376             \
377             "collect g_string_p" "^$" \
378             "collect g_string_partial\[1\]" "^$" \
379             "collect g_string_partial\[2\]" "^$" \
380             \
381             "collect g_structref_p" "^$" \
382             \
383             "collect *((char *)&derived_partial)@sizeof\(void *\)" "^$" \
384             "collect derived_whole" "^$" \
385             \
386             "collect virtual_partial.z" "^$"
387
388         # Begin the test.
389         run_trace_experiment globals_test_func
390
391         gdb_test "print globalc" " = <unavailable>"
392         gdb_test "print globali" " = <unavailable>"
393         gdb_test "print globalf" " = <unavailable>"
394         gdb_test "print globald" " = <unavailable>"
395
396         gdb_test "print globalstruct.memberc" " = <unavailable>"
397         gdb_test "print globalstruct.memberi" " = <unavailable>"
398         gdb_test "print globalstruct.memberf" " = <unavailable>"
399         gdb_test "print globalstruct.memberd" " = <unavailable>"
400
401         gdb_test "print globalstruct" \
402             " = {memberc = <unavailable>, memberi = <unavailable>, memberf = <unavailable>, memberd = <unavailable>}"
403
404         gdb_test "print globalp == &globalstruct" \
405             "value is not available" \
406             "can't compare using non collected global pointer"
407
408         gdb_test "print globalarr\[1\]" " = <unavailable>"
409         gdb_test "print globalarr\[2\]" " = <unavailable>"
410         gdb_test "print globalarr\[3\]" " = <unavailable>"
411
412         gdb_test "print struct_b" \
413             " = {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>}"
414
415         gdb_test "print /x struct_b" \
416             " = {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>}"
417
418         gdb_test "print /x struct_b.struct_a" \
419             " = {a = <unavailable>, b = <unavailable>, array = {<unavailable>, <unavailable>, 0xaaaaaaaa, <unavailable> <repeats 97 times>, 0xaaaaaaaa, <unavailable> <repeats 9899 times>}, ptr = <unavailable>, bitfield = <unavailable>}"
420
421         gdb_test "print /x struct_b.struct_a.array" \
422             " = {<unavailable>, <unavailable>, 0xaaaaaaaa, <unavailable> <repeats 97 times>, 0xaaaaaaaa, <unavailable> <repeats 9899 times>}"
423
424         gdb_test "print /x struct_b.struct_a.array\[0\]" " = <unavailable>"
425
426         gdb_test "print /x struct_b.struct_a.array\[2\]" " = 0xaaaaaaaa"
427
428         # Check the target doesn't overcollect.  GDB used to merge memory
429         # ranges to collect if they were close enough (collecting the hole
430         # as well), but does not do that anymore.  It's plausible that a
431         # target may do this on its end, but as of this writing, no known
432         # target does it.
433         gdb_test "print {a, b, c}" \
434             " = \\{1, <unavailable>, 3\\}" \
435             "No overcollect of almost but not quite adjacent memory ranges"
436
437         # Check <unavailable> isn't confused with 0 in array element repetitions
438
439         gdb_test_no_output "set print repeat 1"
440
441         gdb_test "print /x tarray" \
442             " = \{\{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>\}" \
443             "<unavailable> is not the same as 0 in array element repetitions"
444
445         gdb_test_no_output "set print repeat 10"
446
447         # Check that value repeat handles unavailable-ness.
448         gdb_test "print *tarray@3" " = \\{\\{a = 0, b = <unavailable>\\}, \\{a = 0, b = <unavailable>\\}, \\{a = <unavailable>, b = <unavailable>\\}\\}"
449
450         # Static fields
451
452         gdb_test "print struct_b.static_struct_a" \
453             " = {a = <unavailable>, b = <unavailable>, array = {<unavailable> <repeats 10000 times>}, ptr = <unavailable>, bitfield = <unavailable>}"
454
455         # Bitfields
456
457         gdb_test "print struct_b.struct_a.bitfield" " = <unavailable>"
458
459         # References
460
461         gdb_test "print g_int" " = <unavailable>"
462
463         gdb_test "print g_ref" \
464             "\\(int &\\) @$hex: <unavailable>" \
465             "global reference shows address but not value"
466
467         gdb_test "print *&g_ref" \
468             "\\$\[0-9\]+ = <unavailable>$cr" \
469             "referenced integer was not collected (taking address of reference)"
470
471         gdb_test "print *g_structref_p" \
472             " = {d = <unavailable>, ref = <unavailable>}"
473
474         # Strings
475
476         # Const string is always available, even when not collected.
477         gdb_test "print g_const_string" \
478             " = \"hello world\"$cr" \
479             "non collected const string is still printable"
480
481         gdb_test "print g_string_p" \
482             " = $hex <g_const_string> \"hello world\"" \
483             "printing constant string through collected pointer"
484
485         gdb_test "print g_string_unavail" \
486             " = \{<unavailable> <repeats 12 times>\}" \
487             "printing non collected string"
488
489         # Incomplete strings print as an array.
490         gdb_test "print g_string_partial" \
491             "\\$\[0-9\]+ = \{<unavailable>, 101 'e', 108 'l', <unavailable>, <unavailable>, <unavailable>, <unavailable>, <unavailable>, <unavailable>, <unavailable>, <unavailable>, <unavailable>\}" \
492             "printing partially collected string"
493
494         # It is important for this test that the last examined value is
495         # <unavailable>, to exercise the case of the $__ convenience
496         # variable being set to <unavailable> without error.
497         set msg "examining partially collected object"
498         gdb_test_multiple "x /10x &struct_b" "$msg" {
499             -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 $" {
500                 pass "$msg"
501             }
502             -re "value is not available" {
503                 fail "$msg"
504             }
505         }
506
507         gdb_test "p \$__" " = <unavailable>" "last examined value was <unavailable>"
508
509         # This tests that building the array does not require accessing
510         # g_int's contents.
511         gdb_test "print { 1, g_int, 3 }" \
512             " = \\{1, <unavailable>, 3\\}" \
513             "build array from unavailable value"
514
515         # Note, depends on previous test.
516         gdb_test "print \$\[1\]" \
517             " = <unavailable>" \
518             "subscript a non-memory rvalue array, accessing an unvailable element"
519
520         # Access a field of a non-lazy value, making sure the
521         # unavailable-ness is propagated.  History values are easy
522         # non-lazy values, so use those.  The first test just sets up for
523         # the second.
524         gdb_test "print g_smallstruct" " = \\{member = <unavailable>\\}"
525         gdb_test "print \$.member" " = <unavailable>"
526
527         # Cast to baseclass, checking the unavailable-ness is propagated.
528         gdb_test "print (small_struct) g_smallstruct_b" " = \\{member = <unavailable>\\}"
529
530         # Same cast, but starting from a non-lazy, value.
531         gdb_test "print g_smallstruct_b" " = \\{<small_struct> = \\{member = <unavailable>\\}, <No data fields>\\}"
532         gdb_test "print (small_struct) \$" " = \\{member = <unavailable>\\}"
533
534         gdb_test_no_output "set print object on"
535
536         with_test_prefix "print object on" {
537             # With print object on, printing a pointer may need to fetch
538             # the pointed-to object, to check its run-time type.  Make
539             # sure that fails gracefully and transparently when the
540             # pointer itself is unavailable.
541             gdb_test "print virtualp" " = \\(Virtual \\*\\) <unavailable>"
542
543             # no vtable pointer available
544             gdb_test "print derived_unavail" \
545                 " = {<Middle> = <unavailable>, _vptr.Derived = <unavailable>, z = <unavailable>}"
546
547             # vtable pointer available, but nothing else
548             gdb_test "print derived_partial" \
549                 " = \\(Derived\\) {<Middle> = {<Base> = <unavailable>, _vptr.Middle = <unavailable>, y = <unavailable>}, _vptr.Derived = $hex <vtable for Derived.*>, z = <unavailable>}"
550
551             # whole object available
552             gdb_test "print derived_whole" \
553                 " = \\(Derived\\) {<Middle> = {<Base> = {x = 2}, _vptr.Middle = $hex, y = 3}, _vptr.Derived = $hex <vtable for Derived.*>, z = 4}"
554         }
555
556         gdb_test_no_output "set print object off"
557
558         with_test_prefix "print object off" {
559             gdb_test "print virtualp" " = \\(Virtual \\*\\) <unavailable>"
560
561             # no vtable pointer available
562             gdb_test "print derived_unavail" \
563                 " = {<Middle> = <unavailable>, _vptr.Derived = <unavailable>, z = <unavailable>}"
564
565             # vtable pointer available, but nothing else
566             gdb_test "print derived_partial" \
567                 " = {<Middle> = {<Base> = <unavailable>, _vptr.Middle = <unavailable>, y = <unavailable>}, _vptr.Derived = $hex <vtable for Derived.*>, z = <unavailable>}"
568
569             # whole object available
570             gdb_test "print derived_whole" \
571                 " = {<Middle> = {<Base> = {x = 2}, _vptr.Middle = $hex, y = 3}, _vptr.Derived = $hex <vtable for Derived.*>, z = 4}"
572         }
573
574         # An instance of a virtual class where we collected everything but
575         # the vptr.
576         gdb_test "print virtual_partial" \
577             " = {_vptr.Virtual = <unavailable>, z = 0}"
578
579         gdb_test "tfind none" \
580             "#0  end .*" \
581             "cease trace debugging"
582     }
583 }
584
585 proc gdb_trace_collection_test {} {
586     gdb_collect_globals_test
587     gdb_unavailable_registers_test
588     gdb_unavailable_floats
589
590     gdb_collect_args_test
591     gdb_collect_locals_test local_test_func "auto locals"
592     gdb_collect_locals_test reglocal_test_func "register locals"
593     gdb_collect_locals_test statlocal_test_func "static locals"
594 }
595
596 runto_main
597
598 if { ![gdb_target_supports_trace] } then {
599     unsupported "Current target does not support trace"
600     return 1
601 }
602
603 # Body of test encased in a proc so we can return prematurely.
604 gdb_trace_collection_test
605
606 # Finished!
607 gdb_test "tfind none" ".*" ""