Print entirely unavailable struct/union values as a single <unavailable>.
[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" " = <unavailable>"
173
174         gdb_test "print argstruct.memberc" " = <unavailable>"
175         gdb_test "print argstruct.memberi" " = <unavailable>"
176         gdb_test "print argstruct.memberf" " = <unavailable>"
177         gdb_test "print argstruct.memberd" " = <unavailable>"
178
179         gdb_test "print argarray" " = <unavailable>"
180
181         gdb_test "print &argarray" \
182             "Can't take address of \"argarray\" which isn't an lvalue\."
183
184         gdb_test "print argarray\[0\]" "value is not available"
185
186         # Test "info args"
187         set r ""
188         set r "${r}argc = <unavailable>${cr}"
189         set r "${r}argi = <unavailable>${cr}"
190         set r "${r}argf = <unavailable>${cr}"
191         set r "${r}argd = <unavailable>${cr}"
192         set r "${r}argstruct = <unavailable>${cr}"
193         set r "${r}argarray = <unavailable>${cr}"
194         gdb_test "info args" "$r" "info args"
195
196         test_maybe_regvar_display "argc"
197
198         gdb_test "tfind none" \
199             "#0  end .*" \
200             "cease trace debugging"
201     }
202 }
203
204 proc gdb_collect_locals_test { func msg } {
205     with_test_prefix "unavailable locals: $msg" {
206         global cr
207         global gdb_prompt
208
209         prepare_for_trace_test
210
211         set testline [gdb_get_line_number "set $func tracepoint here"]
212
213         gdb_test "trace $testline" \
214             "Tracepoint \[0-9\]+ at .*" \
215             "set tracepoint"
216
217         # Begin the test.
218         run_trace_experiment $func
219
220         gdb_test "print locc" " = <unavailable>"
221         gdb_test "print loci" " = <unavailable>"
222         gdb_test "print locf" " = <unavailable>"
223         gdb_test "print locd" " = <unavailable>"
224
225         gdb_test "print locst.memberc" " = <unavailable>"
226         gdb_test "print locst.memberi" " = <unavailable>"
227         gdb_test "print locst.memberf" " = <unavailable>"
228         gdb_test "print locst.memberd" " = <unavailable>"
229
230         gdb_test "print locar\[0\]" " = <unavailable>"
231         gdb_test "print locar\[1\]" " = <unavailable>"
232         gdb_test "print locar\[2\]" " = <unavailable>"
233         gdb_test "print locar\[3\]" " = <unavailable>"
234
235         # Test "info locals"
236         set r ""
237         set r "${r}locf = <unavailable>${cr}"
238         set r "${r}locd = <unavailable>${cr}"
239         set r "${r}locst = <unavailable>${cr}"
240         set r "${r}locar = <unavailable>${cr}"
241         set r "${r}i = <unavailable>${cr}"
242         if { $func == "local_test_func" } {
243             set r "${r}locdefst = <unavailable>${cr}"
244         }
245         set r "${r}locc = <unavailable>${cr}"
246         set r "${r}loci = <unavailable>${cr}"
247         gdb_test "info locals" "$r" "info locals"
248
249         test_maybe_regvar_display "loci"
250
251         gdb_test "tfind none" \
252             "#0  end .*" \
253             "cease trace debugging"
254     }
255 }
256
257 proc gdb_unavailable_registers_test { } {
258     with_test_prefix "unavailable registers" {
259         global gdb_prompt
260         global spreg
261         global pcreg
262
263         prepare_for_trace_test
264
265         # We'll simply re-use the globals_test_function for this test
266         gdb_test "trace globals_test_func" \
267             "Tracepoint \[0-9\]+ at .*" \
268             "set tracepoint"
269
270         # Collect nothing.
271
272         # Begin the test.
273         run_trace_experiment globals_test_func
274
275         # On some archs, the $sp/$pc are a real raw registers.  On others,
276         # like x86, they're user registers.  Test both variants.
277         test_register_unavailable "\$$spreg"
278         test_register_unavailable "\$sp"
279
280         # Test reading uncollected pseudo-registers.  The set of which
281         # depends on target.
282         if [is_amd64_regs_target] {
283             # Check the raw register first.
284             test_register_unavailable "\$rax"
285             test_register_unavailable "\$eax"
286             test_register_unavailable "\$ax"
287         } elseif [is_x86_like_target] {
288             # Check the raw register first.
289             test_register_unavailable "\$eax"
290             test_register_unavailable "\$ax"
291         }
292
293         # GDBserver always provides the PC value of regular tracepoint
294         # hits, since it's the same as the tracepoint's address.
295         test_register "\$$pcreg"
296         test_register "\$pc"
297
298         gdb_test "info registers" \
299             "<unavailable>.*<unavailable>" \
300             "info registers, multiple registers not available"
301
302         gdb_test "info registers \$$spreg" \
303             "<unavailable>" \
304             "info registers \$$spreg reports not available"
305
306         gdb_test "tfind none" "#0  end .*" "cease trace debugging"
307     }
308 }
309
310 proc gdb_unavailable_floats { } {
311     global gdb_prompt
312
313     with_test_prefix "unavailable floats" {
314         prepare_for_trace_test
315
316         # We'll simply re-use the globals_test_function for this test
317         gdb_test "trace globals_test_func" \
318             "Tracepoint \[0-9\]+ at .*" \
319             "set tracepoint"
320
321         # Collect nothing.
322
323         # Begin the test.
324         run_trace_experiment globals_test_func
325
326         # Necessarily target specific.
327         if {[istarget "x86_64-*-*"] || [istarget i?86-*]} {
328             send_gdb "info float\n"
329             gdb_expect_list "info float" ".*$gdb_prompt $" {
330                 "Status Word:         <unavailable>"
331                 "Control Word:        <unavailable>"
332                 "Tag Word:            <unavailable>"
333                 "Instruction Pointer: <unavailable>:<unavailable>"
334                 "Operand Pointer:     <unavailable>:<unavailable>"
335                 "Opcode:              <unavailable>"
336             }
337         }
338
339         gdb_test "tfind none" "#0  end .*" "cease trace debugging"
340     }
341 }
342
343 proc gdb_collect_globals_test { } {
344     with_test_prefix "collect globals" {
345         global ws
346         global cr
347         global gdb_prompt
348         global hex
349
350         prepare_for_trace_test
351
352         set testline [gdb_get_line_number "set globals_test_func tracepoint here"]
353
354         gdb_test "trace $testline" \
355             "Tracepoint \[0-9\]+ at .*" \
356             "set tracepoint"
357
358         # We collect the initial sizeof(pointer) bytes of derived_partial
359         # in an attempt of collecting the vptr.  Not portable, but should
360         # work everywhere we need to care.
361         gdb_trace_setactions "define actions" \
362             "" \
363             "collect struct_b.struct_a.array\[2\]" "^$" \
364             "collect struct_b.struct_a.array\[100\]" "^$" \
365             \
366             "collect a" "^$" \
367             "collect c" "^$" \
368             \
369             "collect tarray\[0\].a" "^$" \
370             "collect tarray\[1\].a" "^$" \
371             "collect tarray\[3\].a" "^$" \
372             "collect tarray\[3\].b" "^$" \
373             "collect tarray\[4\].b" "^$" \
374             "collect tarray\[5\].b" "^$" \
375             \
376             "collect g_string_p" "^$" \
377             "collect g_string_partial\[1\]" "^$" \
378             "collect g_string_partial\[2\]" "^$" \
379             \
380             "collect g_structref_p" "^$" \
381             \
382             "collect *((char *)&derived_partial)@sizeof\(void *\)" "^$" \
383             "collect derived_whole" "^$" \
384             \
385             "collect virtual_partial.z" "^$"
386
387         # Begin the test.
388         run_trace_experiment globals_test_func
389
390         gdb_test "print globalc" " = <unavailable>"
391         gdb_test "print globali" " = <unavailable>"
392         gdb_test "print globalf" " = <unavailable>"
393         gdb_test "print globald" " = <unavailable>"
394
395         gdb_test "print globalstruct.memberc" " = <unavailable>"
396         gdb_test "print globalstruct.memberi" " = <unavailable>"
397         gdb_test "print globalstruct.memberf" " = <unavailable>"
398         gdb_test "print globalstruct.memberd" " = <unavailable>"
399
400         gdb_test "print globalstruct" " = <unavailable>"
401
402         gdb_test "print globalp == &globalstruct" \
403             "value is not available" \
404             "can't compare using non collected global pointer"
405
406         gdb_test "print globalarr\[1\]" " = <unavailable>"
407         gdb_test "print globalarr\[2\]" " = <unavailable>"
408         gdb_test "print globalarr\[3\]" " = <unavailable>"
409
410         gdb_test "print struct_b" \
411             " = {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>}"
412
413         gdb_test "print /x struct_b" \
414             " = {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>}"
415
416         gdb_test "print /x struct_b.struct_a" \
417             " = {a = <unavailable>, b = <unavailable>, array = {<unavailable>, <unavailable>, 0xaaaaaaaa, <unavailable> <repeats 97 times>, 0xaaaaaaaa, <unavailable> <repeats 9899 times>}, ptr = <unavailable>, bitfield = <unavailable>}"
418
419         gdb_test "print /x struct_b.struct_a.array" \
420             " = {<unavailable>, <unavailable>, 0xaaaaaaaa, <unavailable> <repeats 97 times>, 0xaaaaaaaa, <unavailable> <repeats 9899 times>}"
421
422         gdb_test "print /x struct_b.struct_a.array\[0\]" " = <unavailable>"
423
424         gdb_test "print /x struct_b.struct_a.array\[2\]" " = 0xaaaaaaaa"
425
426         # Check the target doesn't overcollect.  GDB used to merge memory
427         # ranges to collect if they were close enough (collecting the hole
428         # as well), but does not do that anymore.  It's plausible that a
429         # target may do this on its end, but as of this writing, no known
430         # target does it.
431         gdb_test "print {a, b, c}" \
432             " = \\{1, <unavailable>, 3\\}" \
433             "No overcollect of almost but not quite adjacent memory ranges"
434
435         # Check <unavailable> isn't confused with 0 in array element repetitions
436
437         gdb_test_no_output "set print repeat 1"
438
439         gdb_test "print /x tarray" \
440             " = \{\{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>\}" \
441             "<unavailable> is not the same as 0 in array element repetitions"
442
443         gdb_test_no_output "set print repeat 10"
444
445         # Check that value repeat handles unavailable-ness.
446         gdb_test "print *tarray@3" " = \\{\\{a = 0, b = <unavailable>\\}, \\{a = 0, b = <unavailable>\\}, \\{a = <unavailable>, b = <unavailable>\\}\\}"
447
448         # Static fields
449
450         gdb_test "print struct_b.static_struct_a" " = <unavailable>"
451
452         # Bitfields
453
454         gdb_test "print struct_b.struct_a.bitfield" " = <unavailable>"
455
456         # References
457
458         gdb_test "print g_int" " = <unavailable>"
459
460         gdb_test "print g_ref" \
461             "\\(int &\\) @$hex: <unavailable>" \
462             "global reference shows address but not value"
463
464         gdb_test "print *&g_ref" \
465             "\\$\[0-9\]+ = <unavailable>$cr" \
466             "referenced integer was not collected (taking address of reference)"
467
468         gdb_test "print *g_structref_p" " = <unavailable>"
469
470         # Strings
471
472         # Const string is always available, even when not collected.
473         gdb_test "print g_const_string" \
474             " = \"hello world\"$cr" \
475             "non collected const string is still printable"
476
477         gdb_test "print g_string_p" \
478             " = $hex <g_const_string> \"hello world\"" \
479             "printing constant string through collected pointer"
480
481         gdb_test "print g_string_unavail" \
482             " = <unavailable>" \
483             "printing non collected string"
484
485         # Incomplete strings print as an array.
486         gdb_test "print g_string_partial" \
487             "\\$\[0-9\]+ = \{<unavailable>, 101 'e', 108 'l', <unavailable>, <unavailable>, <unavailable>, <unavailable>, <unavailable>, <unavailable>, <unavailable>, <unavailable>, <unavailable>\}" \
488             "printing partially collected string"
489
490         # It is important for this test that the last examined value is
491         # <unavailable>, to exercise the case of the $__ convenience
492         # variable being set to <unavailable> without error.
493         set msg "examining partially collected object"
494         gdb_test_multiple "x /10x &struct_b" "$msg" {
495             -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 $" {
496                 pass "$msg"
497             }
498             -re "value is not available" {
499                 fail "$msg"
500             }
501         }
502
503         gdb_test "p \$__" " = <unavailable>" "last examined value was <unavailable>"
504
505         # This tests that building the array does not require accessing
506         # g_int's contents.
507         gdb_test "print { 1, g_int, 3 }" \
508             " = \\{1, <unavailable>, 3\\}" \
509             "build array from unavailable value"
510
511         # Note, depends on previous test.
512         gdb_test "print \$\[1\]" \
513             " = <unavailable>" \
514             "subscript a non-memory rvalue array, accessing an unvailable element"
515
516         # Access a field of a non-lazy value, making sure the
517         # unavailable-ness is propagated.  History values are easy
518         # non-lazy values, so use those.  The first test just sets up for
519         # the second.
520         gdb_test "print g_smallstruct" " = <unavailable>"
521         gdb_test "print \$.member" " = <unavailable>"
522
523         # Cast to baseclass, checking the unavailable-ness is propagated.
524         gdb_test "print (small_struct) g_smallstruct_b" " = <unavailable>"
525
526         # Same cast, but starting from a non-lazy, value.
527         gdb_test "print g_smallstruct_b" " = <unavailable>"
528         gdb_test "print (small_struct) \$" " = <unavailable>"
529
530         gdb_test_no_output "set print object on"
531
532         with_test_prefix "print object on" {
533             # With print object on, printing a pointer may need to fetch
534             # the pointed-to object, to check its run-time type.  Make
535             # sure that fails gracefully and transparently when the
536             # pointer itself is unavailable.
537             gdb_test "print virtualp" " = <unavailable>"
538
539             # no vtable pointer available
540             gdb_test "print derived_unavail" " = <unavailable>"
541
542             # vtable pointer available, but nothing else
543             gdb_test "print derived_partial" \
544                 " = \\(Derived\\) {<Middle> = {<Base> = <unavailable>, _vptr.Middle = <unavailable>, y = <unavailable>}, _vptr.Derived = $hex <vtable for Derived.*>, z = <unavailable>}"
545
546             # whole object available
547             gdb_test "print derived_whole" \
548                 " = \\(Derived\\) {<Middle> = {<Base> = {x = 2}, _vptr.Middle = $hex, y = 3}, _vptr.Derived = $hex <vtable for Derived.*>, z = 4}"
549         }
550
551         gdb_test_no_output "set print object off"
552
553         with_test_prefix "print object off" {
554             gdb_test "print virtualp" " = <unavailable>"
555
556             # no vtable pointer available
557             gdb_test "print derived_unavail" \
558                 " = <unavailable>"
559
560             # vtable pointer available, but nothing else
561             gdb_test "print derived_partial" \
562                 " = {<Middle> = {<Base> = <unavailable>, _vptr.Middle = <unavailable>, y = <unavailable>}, _vptr.Derived = $hex <vtable for Derived.*>, z = <unavailable>}"
563
564             # whole object available
565             gdb_test "print derived_whole" \
566                 " = {<Middle> = {<Base> = {x = 2}, _vptr.Middle = $hex, y = 3}, _vptr.Derived = $hex <vtable for Derived.*>, z = 4}"
567         }
568
569         # An instance of a virtual class where we collected everything but
570         # the vptr.
571         gdb_test "print virtual_partial" \
572             " = {_vptr.Virtual = <unavailable>, z = 0}"
573
574         gdb_test "tfind none" \
575             "#0  end .*" \
576             "cease trace debugging"
577     }
578 }
579
580 proc gdb_trace_collection_test {} {
581     gdb_collect_globals_test
582     gdb_unavailable_registers_test
583     gdb_unavailable_floats
584
585     gdb_collect_args_test
586     gdb_collect_locals_test local_test_func "auto locals"
587     gdb_collect_locals_test reglocal_test_func "register locals"
588     gdb_collect_locals_test statlocal_test_func "static locals"
589 }
590
591 runto_main
592
593 if { ![gdb_target_supports_trace] } then {
594     unsupported "Current target does not support trace"
595     return 1
596 }
597
598 # Body of test encased in a proc so we can return prematurely.
599 gdb_trace_collection_test
600
601 # Finished!
602 gdb_test "tfind none" ".*" ""