]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.base/printcmds.exp
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / printcmds.exp
1 # This testcase is part of GDB, the GNU debugger.
2
3 # Copyright 1992-2023 Free Software Foundation, Inc.
4
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 # Please email any bugs, comments, and/or additions to this file to:
19 # bug-gdb@gnu.org
20
21 # This file was written by Fred Fish. (fnf@cygnus.com)
22
23 standard_testfile .c
24
25 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
26 untested "failed to compile"
27 return -1
28 }
29
30 proc test_integer_literals_accepted {} {
31 global gdb_prompt
32
33 # Test various decimal values.
34
35 gdb_test "p 123" " = 123"
36 gdb_test "p -123" " = -123"
37 gdb_test "p/d 123" " = 123"
38
39 # Test various octal values.
40
41 gdb_test "p 0123" " = 83"
42 gdb_test "p 00123" " = 83"
43 gdb_test "p -0123" " = -83"
44 gdb_test "p/o 0123" " = 0123"
45
46 # Test various hexadecimal values.
47
48 gdb_test "p 0x123" " = 291"
49 gdb_test "p -0x123" " = -291"
50 gdb_test "p 0x0123" " = 291"
51 gdb_test "p -0x0123" " = -291"
52 gdb_test "p 0xABCDEF" " = 11259375"
53 gdb_test "p 0xabcdef" " = 11259375"
54 gdb_test "p 0xAbCdEf" " = 11259375"
55 gdb_test "p/x 0x123" " = 0x123"
56
57 # Test various binary values.
58
59 gdb_test "p 0b0" " = 0"
60 gdb_test "p 0b1111" " = 15"
61 gdb_test "p 0B1111" " = 15"
62 gdb_test "p -0b1111" " = -15"
63 }
64
65 proc test_character_literals_accepted {} {
66 global gdb_prompt
67
68 gdb_test "p 'a'" " = 97 'a'"
69 gdb_test "p/c 'a'" " = 97 'a'"
70 gdb_test "p/x 'a'" " = 0x61"
71 gdb_test "p/d 'a'" " = 97"
72 gdb_test "p/t 'a'" " = 1100001"
73 gdb_test "p '\\141'" " = 97 'a'"
74 gdb_test "p/x '\\377'" " = 0xff"
75 # Note "p '\''" => "= 39 '\''"
76 gdb_test "p '\\''" " = 39 '\\\\''"
77 # Note "p '\\'" => "= 92 '\\'"
78 gdb_test "p '\\\\'" " = 92 '\\\\\\\\'"
79 }
80
81 proc test_integer_literals_rejected {} {
82 global gdb_prompt
83
84 test_print_reject "p 0x"
85 test_print_reject "p 0b"
86 gdb_test "p ''" "(Empty character constant\\.|A character constant must contain at least one character\\.)"
87 gdb_test "p '''" "(Empty character constant\\.|A character constant must contain at least one character\\.)"
88 test_print_reject "p '\\'"
89
90 # Note that this turns into "p '\\\'" at gdb's input.
91 test_print_reject "p '\\\\\\'"
92
93 # Test various decimal values.
94
95 test_print_reject "p DEADBEEF"
96
97 # Test various octal values.
98
99 test_print_reject "p 09"
100 test_print_reject "p 079"
101
102 # Test various hexadecimal values.
103
104 test_print_reject "p 0xG"
105 test_print_reject "p 0xAG"
106
107 # Test various binary values.
108
109 test_print_reject "p 0b2"
110 test_print_reject "p 0b12"
111 }
112
113 proc test_float_accepted {} {
114 global gdb_prompt
115
116 # This test is useful to catch successful parsing of the first fp value.
117 gdb_test "p 123.4+56.7" " = 180.(099\[0-9]*|100\[0-9\]*)" "check for floating addition"
118
119 # Test all the suffixes (including no suffix).
120 gdb_test "p 1." " = 1"
121 gdb_test "p 1.5" " = 1.5"
122 gdb_test "p 1.f" " = 1"
123 gdb_test "p 1.5f" " = 1.5"
124 gdb_test "p 1.l" " = 1"
125 gdb_test "p 1.5l" " = 1.5"
126
127 # Test hexadecimal floating point.
128 foreach {num result} {
129 0x1.1 1.0625
130 0x1.8480000000000p+6 97.125
131 0x1.8480000000000p6 97.125
132 0x00.1p0 0.0625
133 0x00.1p1 0.125
134 0x00.1p-1 0.03125
135 } {
136 gdb_test "p $num" " = [string_to_regexp $result]"
137 }
138 }
139
140 proc test_float_rejected {} {
141 # Gdb use to fail this test for all configurations. The C
142 # lexer thought that 123DEADBEEF was a floating point number, but
143 # then failed to notice that atof() only eats the 123 part.
144 # Fixed, 4/25/97, by Bob Manson.
145 test_print_reject "p 123DEADBEEF"
146
147 test_print_reject "p 123foobar.bazfoo3"
148 test_print_reject "p 123EEEEEEEEEEEEEEEEE33333k333"
149
150 # Test bad suffixes.
151 test_print_reject "p 1.1x"
152 test_print_reject "p 1.1ff"
153 test_print_reject "p 1.1ll"
154 }
155
156 # Regression test for PR gdb/21675
157 proc test_radices {} {
158 gdb_test "print/o 16777211" " = 077777773"
159
160 # This is written in a somewhat funny way to avoid assuming a
161 # particular float format.
162 set s_int [get_sizeof int -1]
163 set s_float [get_sizeof float -1]
164 if {$s_int == $s_float && $s_int != -1} {
165 foreach fmt {d u x t o} {
166 set ival [get_valueof "/$fmt" "*(int *) &f_var" "FAIL" \
167 "get_valueof int/$fmt"]
168 set fval [get_valueof "/$fmt" f_var "FAIL" \
169 "get_valueof float/$fmt"]
170 # See PR gdb/16242 for this.
171 if {[string compare $ival $fval] == 0 && $ival != "FAIL"} {
172 pass "print/$fmt f_var"
173 } else {
174 fail "print/$fmt f_var"
175 }
176 }
177 }
178
179 gdb_test "print/c f_var" " = 65 'A'"
180
181 gdb_test "print/u (char) -1" " = 255"
182 gdb_test "print/d (unsigned char) -1" " = -1"
183 }
184
185 proc test_print_all_chars {} {
186 global gdb_prompt
187
188 # Set the target-charset to ASCII, because the output varies from
189 # different charset.
190 with_target_charset "ASCII" {
191
192 gdb_test "p ctable1\[0\]" " = 0 '\\\\000'"
193 gdb_test "p ctable1\[1\]" " = 1 '\\\\001'"
194 gdb_test "p ctable1\[2\]" " = 2 '\\\\002'"
195 gdb_test "p ctable1\[3\]" " = 3 '\\\\003'"
196 gdb_test "p ctable1\[4\]" " = 4 '\\\\004'"
197 gdb_test "p ctable1\[5\]" " = 5 '\\\\005'"
198 gdb_test "p ctable1\[6\]" " = 6 '\\\\006'"
199 gdb_test "p ctable1\[7\]" " = 7 '\\\\a'"
200 gdb_test "p ctable1\[8\]" " = 8 '\\\\b'"
201 gdb_test "p ctable1\[9\]" " = 9 '\\\\t'"
202 gdb_test "p ctable1\[10\]" " = 10 '\\\\n'"
203 gdb_test "p ctable1\[11\]" " = 11 '\\\\v'"
204 gdb_test "p ctable1\[12\]" " = 12 '\\\\f'"
205 gdb_test "p ctable1\[13\]" " = 13 '\\\\r'"
206 gdb_test "p ctable1\[14\]" " = 14 '\\\\016'"
207 gdb_test "p ctable1\[15\]" " = 15 '\\\\017'"
208 gdb_test "p ctable1\[16\]" " = 16 '\\\\020'"
209 gdb_test "p ctable1\[17\]" " = 17 '\\\\021'"
210 gdb_test "p ctable1\[18\]" " = 18 '\\\\022'"
211 gdb_test "p ctable1\[19\]" " = 19 '\\\\023'"
212 gdb_test "p ctable1\[20\]" " = 20 '\\\\024'"
213 gdb_test "p ctable1\[21\]" " = 21 '\\\\025'"
214 gdb_test "p ctable1\[22\]" " = 22 '\\\\026'"
215 gdb_test "p ctable1\[23\]" " = 23 '\\\\027'"
216 gdb_test "p ctable1\[24\]" " = 24 '\\\\030'"
217 gdb_test "p ctable1\[25\]" " = 25 '\\\\031'"
218 gdb_test "p ctable1\[26\]" " = 26 '\\\\032'"
219 gdb_test "p ctable1\[27\]" " = 27 '\\\\033'"
220 gdb_test "p ctable1\[28\]" " = 28 '\\\\034'"
221 gdb_test "p ctable1\[29\]" " = 29 '\\\\035'"
222 gdb_test "p ctable1\[30\]" " = 30 '\\\\036'"
223 gdb_test "p ctable1\[31\]" " = 31 '\\\\037'"
224 gdb_test "p ctable1\[32\]" " = 32 ' '"
225 gdb_test "p ctable1\[33\]" " = 33 '!'"
226 gdb_test "p ctable1\[34\]" " = 34 '\"'"
227 gdb_test "p ctable1\[35\]" " = 35 '#'"
228 gdb_test "p ctable1\[36\]" " = 36 '\\\$'"
229 gdb_test "p ctable1\[37\]" " = 37 '%'"
230 gdb_test "p ctable1\[38\]" " = 38 '&'"
231 gdb_test "p ctable1\[39\]" " = 39 '\\\\''"
232 gdb_test "p ctable1\[40\]" " = 40 '\\('"
233 gdb_test "p ctable1\[41\]" " = 41 '\\)'"
234 gdb_test "p ctable1\[42\]" " = 42 '\\*'"
235 gdb_test "p ctable1\[43\]" " = 43 '\\+'"
236 gdb_test "p ctable1\[44\]" " = 44 ','"
237 gdb_test "p ctable1\[45\]" " = 45 '-'"
238 gdb_test "p ctable1\[46\]" " = 46 '.'"
239 gdb_test "p ctable1\[47\]" " = 47 '/'"
240 gdb_test "p ctable1\[48\]" " = 48 '0'"
241 gdb_test "p ctable1\[49\]" " = 49 '1'"
242 gdb_test "p ctable1\[50\]" " = 50 '2'"
243 gdb_test "p ctable1\[51\]" " = 51 '3'"
244 gdb_test "p ctable1\[52\]" " = 52 '4'"
245 gdb_test "p ctable1\[53\]" " = 53 '5'"
246 gdb_test "p ctable1\[54\]" " = 54 '6'"
247 gdb_test "p ctable1\[55\]" " = 55 '7'"
248 gdb_test "p ctable1\[56\]" " = 56 '8'"
249 gdb_test "p ctable1\[57\]" " = 57 '9'"
250 gdb_test "p ctable1\[58\]" " = 58 ':'"
251 gdb_test "p ctable1\[59\]" " = 59 ';'"
252 gdb_test "p ctable1\[60\]" " = 60 '<'"
253 gdb_test "p ctable1\[61\]" " = 61 '='"
254 gdb_test "p ctable1\[62\]" " = 62 '>'"
255 gdb_test "p ctable1\[63\]" " = 63 '\\?'"
256 gdb_test "p ctable1\[64\]" " = 64 '@'"
257 gdb_test "p ctable1\[65\]" " = 65 'A'"
258 gdb_test "p ctable1\[66\]" " = 66 'B'"
259 gdb_test "p ctable1\[67\]" " = 67 'C'"
260 gdb_test "p ctable1\[68\]" " = 68 'D'"
261 gdb_test "p ctable1\[69\]" " = 69 'E'"
262 gdb_test "p ctable1\[70\]" " = 70 'F'"
263 gdb_test "p ctable1\[71\]" " = 71 'G'"
264 gdb_test "p ctable1\[72\]" " = 72 'H'"
265 gdb_test "p ctable1\[73\]" " = 73 'I'"
266 gdb_test "p ctable1\[74\]" " = 74 'J'"
267 gdb_test "p ctable1\[75\]" " = 75 'K'"
268 gdb_test "p ctable1\[76\]" " = 76 'L'"
269 gdb_test "p ctable1\[77\]" " = 77 'M'"
270 gdb_test "p ctable1\[78\]" " = 78 'N'"
271 gdb_test "p ctable1\[79\]" " = 79 'O'"
272 gdb_test "p ctable1\[80\]" " = 80 'P'"
273 gdb_test "p ctable1\[81\]" " = 81 'Q'"
274 gdb_test "p ctable1\[82\]" " = 82 'R'"
275 gdb_test "p ctable1\[83\]" " = 83 'S'"
276 gdb_test "p ctable1\[84\]" " = 84 'T'"
277 gdb_test "p ctable1\[85\]" " = 85 'U'"
278 gdb_test "p ctable1\[86\]" " = 86 'V'"
279 gdb_test "p ctable1\[87\]" " = 87 'W'"
280 gdb_test "p ctable1\[88\]" " = 88 'X'"
281 gdb_test "p ctable1\[89\]" " = 89 'Y'"
282 gdb_test "p ctable1\[90\]" " = 90 'Z'"
283 gdb_test "p ctable1\[91\]" " = 91 '\\\['"
284 gdb_test "p ctable1\[92\]" " = 92 '\\\\\\\\'"
285 gdb_test "p ctable1\[93\]" " = 93 '\\\]'"
286 gdb_test "p ctable1\[94\]" " = 94 '\\^'"
287 gdb_test "p ctable1\[95\]" " = 95 '_'"
288 gdb_test "p ctable1\[96\]" " = 96 '`'"
289 gdb_test "p ctable1\[97\]" " = 97 'a'"
290 gdb_test "p ctable1\[98\]" " = 98 'b'"
291 gdb_test "p ctable1\[99\]" " = 99 'c'"
292 gdb_test "p ctable1\[100\]" " = 100 'd'"
293 gdb_test "p ctable1\[101\]" " = 101 'e'"
294 gdb_test "p ctable1\[102\]" " = 102 'f'"
295 gdb_test "p ctable1\[103\]" " = 103 'g'"
296 gdb_test "p ctable1\[104\]" " = 104 'h'"
297 gdb_test "p ctable1\[105\]" " = 105 'i'"
298 gdb_test "p ctable1\[106\]" " = 106 'j'"
299 gdb_test "p ctable1\[107\]" " = 107 'k'"
300 gdb_test "p ctable1\[108\]" " = 108 'l'"
301 gdb_test "p ctable1\[109\]" " = 109 'm'"
302 gdb_test "p ctable1\[110\]" " = 110 'n'"
303 gdb_test "p ctable1\[111\]" " = 111 'o'"
304 gdb_test "p ctable1\[112\]" " = 112 'p'"
305 gdb_test "p ctable1\[113\]" " = 113 'q'"
306 gdb_test "p ctable1\[114\]" " = 114 'r'"
307 gdb_test "p ctable1\[115\]" " = 115 's'"
308 gdb_test "p ctable1\[116\]" " = 116 't'"
309 gdb_test "p ctable1\[117\]" " = 117 'u'"
310 gdb_test "p ctable1\[118\]" " = 118 'v'"
311 gdb_test "p ctable1\[119\]" " = 119 'w'"
312 gdb_test "p ctable1\[120\]" " = 120 'x'"
313 gdb_test "p ctable1\[121\]" " = 121 'y'"
314 gdb_test "p ctable1\[122\]" " = 122 'z'"
315 gdb_test "p ctable1\[123\]" " = 123 '\[{\]+'"
316 gdb_test "p ctable1\[124\]" " = 124 '\[|\]+'"
317 gdb_test "p ctable1\[125\]" " = 125 '\[}\]+'"
318 gdb_test "p ctable1\[126\]" " = 126 '\[~\]'"
319 gdb_test "p ctable1\[127\]" " = 127 '\\\\177'"
320 gdb_test "p ctable1\[128\]" " = 128 '\\\\200'"
321 gdb_test "p ctable1\[129\]" " = 129 '\\\\201'"
322 gdb_test "p ctable1\[130\]" " = 130 '\\\\202'"
323 gdb_test "p ctable1\[131\]" " = 131 '\\\\203'"
324 gdb_test "p ctable1\[132\]" " = 132 '\\\\204'"
325 gdb_test "p ctable1\[133\]" " = 133 '\\\\205'"
326 gdb_test "p ctable1\[134\]" " = 134 '\\\\206'"
327 gdb_test "p ctable1\[135\]" " = 135 '\\\\207'"
328 gdb_test "p ctable1\[136\]" " = 136 '\\\\210'"
329 gdb_test "p ctable1\[137\]" " = 137 '\\\\211'"
330 gdb_test "p ctable1\[138\]" " = 138 '\\\\212'"
331 gdb_test "p ctable1\[139\]" " = 139 '\\\\213'"
332 gdb_test "p ctable1\[140\]" " = 140 '\\\\214'"
333 gdb_test "p ctable1\[141\]" " = 141 '\\\\215'"
334 gdb_test "p ctable1\[142\]" " = 142 '\\\\216'"
335 gdb_test "p ctable1\[143\]" " = 143 '\\\\217'"
336 gdb_test "p ctable1\[144\]" " = 144 '\\\\220'"
337 gdb_test "p ctable1\[145\]" " = 145 '\\\\221'"
338 gdb_test "p ctable1\[146\]" " = 146 '\\\\222'"
339 gdb_test "p ctable1\[147\]" " = 147 '\\\\223'"
340 gdb_test "p ctable1\[148\]" " = 148 '\\\\224'"
341 gdb_test "p ctable1\[149\]" " = 149 '\\\\225'"
342 gdb_test "p ctable1\[150\]" " = 150 '\\\\226'"
343 gdb_test "p ctable1\[151\]" " = 151 '\\\\227'"
344 gdb_test "p ctable1\[152\]" " = 152 '\\\\230'"
345 gdb_test "p ctable1\[153\]" " = 153 '\\\\231'"
346 gdb_test "p ctable1\[154\]" " = 154 '\\\\232'"
347 gdb_test "p ctable1\[155\]" " = 155 '\\\\233'"
348 gdb_test "p ctable1\[156\]" " = 156 '\\\\234'"
349 gdb_test "p ctable1\[157\]" " = 157 '\\\\235'"
350 gdb_test "p ctable1\[158\]" " = 158 '\\\\236'"
351 gdb_test "p ctable1\[159\]" " = 159 '\\\\237'"
352 gdb_test "p ctable1\[160\]" " = 160 '\\\\240'"
353 gdb_test "p ctable1\[161\]" " = 161 '\\\\241'"
354 gdb_test "p ctable1\[162\]" " = 162 '\\\\242'"
355 gdb_test "p ctable1\[163\]" " = 163 '\\\\243'"
356 gdb_test "p ctable1\[164\]" " = 164 '\\\\244'"
357 gdb_test "p ctable1\[165\]" " = 165 '\\\\245'"
358 gdb_test "p ctable1\[166\]" " = 166 '\\\\246'"
359 gdb_test "p ctable1\[167\]" " = 167 '\\\\247'"
360 gdb_test "p ctable1\[168\]" " = 168 '\\\\250'"
361 gdb_test "p ctable1\[169\]" " = 169 '\\\\251'"
362 gdb_test "p ctable1\[170\]" " = 170 '\\\\252'"
363 gdb_test "p ctable1\[171\]" " = 171 '\\\\253'"
364 gdb_test "p ctable1\[172\]" " = 172 '\\\\254'"
365 gdb_test "p ctable1\[173\]" " = 173 '\\\\255'"
366 gdb_test "p ctable1\[174\]" " = 174 '\\\\256'"
367 gdb_test "p ctable1\[175\]" " = 175 '\\\\257'"
368 gdb_test "p ctable1\[176\]" " = 176 '\\\\260'"
369 gdb_test "p ctable1\[177\]" " = 177 '\\\\261'"
370 gdb_test "p ctable1\[178\]" " = 178 '\\\\262'"
371 gdb_test "p ctable1\[179\]" " = 179 '\\\\263'"
372 gdb_test "p ctable1\[180\]" " = 180 '\\\\264'"
373 gdb_test "p ctable1\[181\]" " = 181 '\\\\265'"
374 gdb_test "p ctable1\[182\]" " = 182 '\\\\266'"
375 gdb_test "p ctable1\[183\]" " = 183 '\\\\267'"
376 gdb_test "p ctable1\[184\]" " = 184 '\\\\270'"
377 gdb_test "p ctable1\[185\]" " = 185 '\\\\271'"
378 gdb_test "p ctable1\[186\]" " = 186 '\\\\272'"
379 gdb_test "p ctable1\[187\]" " = 187 '\\\\273'"
380 gdb_test "p ctable1\[188\]" " = 188 '\\\\274'"
381 gdb_test "p ctable1\[189\]" " = 189 '\\\\275'"
382 gdb_test "p ctable1\[190\]" " = 190 '\\\\276'"
383 gdb_test "p ctable1\[191\]" " = 191 '\\\\277'"
384 gdb_test "p ctable1\[192\]" " = 192 '\\\\300'"
385 gdb_test "p ctable1\[193\]" " = 193 '\\\\301'"
386 gdb_test "p ctable1\[194\]" " = 194 '\\\\302'"
387 gdb_test "p ctable1\[195\]" " = 195 '\\\\303'"
388 gdb_test "p ctable1\[196\]" " = 196 '\\\\304'"
389 gdb_test "p ctable1\[197\]" " = 197 '\\\\305'"
390 gdb_test "p ctable1\[198\]" " = 198 '\\\\306'"
391 gdb_test "p ctable1\[199\]" " = 199 '\\\\307'"
392 gdb_test "p ctable1\[200\]" " = 200 '\\\\310'"
393 gdb_test "p ctable1\[201\]" " = 201 '\\\\311'"
394 gdb_test "p ctable1\[202\]" " = 202 '\\\\312'"
395 gdb_test "p ctable1\[203\]" " = 203 '\\\\313'"
396 gdb_test "p ctable1\[204\]" " = 204 '\\\\314'"
397 gdb_test "p ctable1\[205\]" " = 205 '\\\\315'"
398 gdb_test "p ctable1\[206\]" " = 206 '\\\\316'"
399 gdb_test "p ctable1\[207\]" " = 207 '\\\\317'"
400 gdb_test "p ctable1\[208\]" " = 208 '\\\\320'"
401 gdb_test "p ctable1\[209\]" " = 209 '\\\\321'"
402 gdb_test "p ctable1\[210\]" " = 210 '\\\\322'"
403 gdb_test "p ctable1\[211\]" " = 211 '\\\\323'"
404 gdb_test "p ctable1\[212\]" " = 212 '\\\\324'"
405 gdb_test "p ctable1\[213\]" " = 213 '\\\\325'"
406 gdb_test "p ctable1\[214\]" " = 214 '\\\\326'"
407 gdb_test "p ctable1\[215\]" " = 215 '\\\\327'"
408 gdb_test "p ctable1\[216\]" " = 216 '\\\\330'"
409 gdb_test "p ctable1\[217\]" " = 217 '\\\\331'"
410 gdb_test "p ctable1\[218\]" " = 218 '\\\\332'"
411 gdb_test "p ctable1\[219\]" " = 219 '\\\\333'"
412 gdb_test "p ctable1\[220\]" " = 220 '\\\\334'"
413 gdb_test "p ctable1\[221\]" " = 221 '\\\\335'"
414 gdb_test "p ctable1\[222\]" " = 222 '\\\\336'"
415 gdb_test "p ctable1\[223\]" " = 223 '\\\\337'"
416 gdb_test "p ctable1\[224\]" " = 224 '\\\\340'"
417 gdb_test "p ctable1\[225\]" " = 225 '\\\\341'"
418 gdb_test "p ctable1\[226\]" " = 226 '\\\\342'"
419 gdb_test "p ctable1\[227\]" " = 227 '\\\\343'"
420 gdb_test "p ctable1\[228\]" " = 228 '\\\\344'"
421 gdb_test "p ctable1\[229\]" " = 229 '\\\\345'"
422 gdb_test "p ctable1\[230\]" " = 230 '\\\\346'"
423 gdb_test "p ctable1\[231\]" " = 231 '\\\\347'"
424 gdb_test "p ctable1\[232\]" " = 232 '\\\\350'"
425 gdb_test "p ctable1\[233\]" " = 233 '\\\\351'"
426 gdb_test "p ctable1\[234\]" " = 234 '\\\\352'"
427 gdb_test "p ctable1\[235\]" " = 235 '\\\\353'"
428 gdb_test "p ctable1\[236\]" " = 236 '\\\\354'"
429 gdb_test "p ctable1\[237\]" " = 237 '\\\\355'"
430 gdb_test "p ctable1\[238\]" " = 238 '\\\\356'"
431 gdb_test "p ctable1\[239\]" " = 239 '\\\\357'"
432 gdb_test "p ctable1\[240\]" " = 240 '\\\\360'"
433 gdb_test "p ctable1\[241\]" " = 241 '\\\\361'"
434 gdb_test "p ctable1\[242\]" " = 242 '\\\\362'"
435 gdb_test "p ctable1\[243\]" " = 243 '\\\\363'"
436 gdb_test "p ctable1\[244\]" " = 244 '\\\\364'"
437 gdb_test "p ctable1\[245\]" " = 245 '\\\\365'"
438 gdb_test "p ctable1\[246\]" " = 246 '\\\\366'"
439 gdb_test "p ctable1\[247\]" " = 247 '\\\\367'"
440 gdb_test "p ctable1\[248\]" " = 248 '\\\\370'"
441 gdb_test "p ctable1\[249\]" " = 249 '\\\\371'"
442 gdb_test "p ctable1\[250\]" " = 250 '\\\\372'"
443 gdb_test "p ctable1\[251\]" " = 251 '\\\\373'"
444 gdb_test "p ctable1\[252\]" " = 252 '\\\\374'"
445 gdb_test "p ctable1\[253\]" " = 253 '\\\\375'"
446 gdb_test "p ctable1\[254\]" " = 254 '\\\\376'"
447 gdb_test "p ctable1\[255\]" " = 255 '\\\\377'"
448 }
449 }
450
451 # Test interaction of the number of print elements to print and the
452 # repeat count, set to the default of 10.
453
454 proc test_print_repeats_10 {} {
455 global gdb_prompt decimal
456
457 for { set x 1 } { $x <= 16 } { incr x } {
458 gdb_test_no_output "set print elements $x" "elements $x repeats"
459 for { set e 1 } { $e <= 16 } {incr e } {
460 set v [expr $e - 1]
461 set command "p &ctable2\[${v}*16\]"
462 if { $x < $e } {
463 set aval $x
464 } else {
465 set aval $e
466 }
467 set xval [expr $x - $e]
468 if { $xval < 0 } {
469 set xval 0
470 }
471 if { $aval > 10 } {
472 set a "'a' <repeats $aval times>"
473 if { $xval > 0 } {
474 set a "${a}, \\\""
475 }
476 } else {
477 set a "\\\"[string range "aaaaaaaaaaaaaaaa" 1 $aval]"
478 if { $xval > 10 } {
479 set a "$a\\\", "
480 }
481 }
482 set xstr ""
483 if { $xval > 10 } {
484 set xstr "'X' <repeats $xval times>"
485 } else {
486 if { $xval > 0 } {
487 set xstr "[string range "XXXXXXXXXXXXXXXX" 1 $xval]\\\""
488 } else {
489 if { $aval <= 10 } {
490 set xstr "\\\""
491 }
492 }
493 }
494 if { $aval < 16 } {
495 set xstr "${xstr}\[.\]\[.\]\[.\]"
496 }
497 set string " = \[(\]unsigned char \[*\]\[)\] <ctable2(\\+$decimal)?> ${a}${xstr}"
498 gdb_test "$command" "$string" "$command with print elements set to $x"
499 }
500 }
501 }
502
503 # This tests whether GDB uses the correct element content offsets
504 # (relative to the complete `some_struct' value) when counting value
505 # repetitions.
506
507 proc test_print_repeats_embedded_array {} {
508 global gdb_prompt
509
510 gdb_test_escape_braces "p/x some_struct" \
511 "= {a = 0x12345678, b = 0x87654321, array = {0xaa <repeats 20 times>}}" \
512 "correct element repeats in array embedded at offset > 0"
513 }
514
515 proc test_print_strings {} {
516 global gdb_prompt decimal
517
518 # We accept "(unsigned char *) " before the string. char vs. unsigned char
519 # is already tested elsewhere.
520
521 # Test that setting print elements unlimited doesn't completely suppress
522 # printing; this was a bug in older gdb's.
523 gdb_test_no_output "set print elements 0"
524 gdb_test "p teststring" \
525 " = (.unsigned char .. )?\"teststring contents\"" "p teststring with elements set to 0"
526 gdb_test_no_output "set print elements 1"
527 gdb_test "p teststring" \
528 " = (.unsigned char .. )?\"t\"\\.\\.\\." "p teststring with elements set to 1"
529 gdb_test_no_output "set print elements 5"
530 gdb_test "p teststring" \
531 " = (.unsigned char .. )?\"tests\"\\.\\.\\." "p teststring with elements set to 5"
532 gdb_test_no_output "set print elements 19"
533 gdb_test "p teststring" \
534 " = (.unsigned char .. )?\"teststring contents\"" "p teststring with elements set to 19"
535 gdb_test_no_output "set print elements 20"
536 gdb_test "p teststring" \
537 " = (.unsigned char .. )?\"teststring contents\"" "p teststring with elements set to 20"
538
539 gdb_test "p -elements 1 -- teststring" \
540 " = (.unsigned char .. )?\"t\"\\.\\.\\."
541 gdb_test "p -elements 5 -- teststring" \
542 " = (.unsigned char .. )?\"tests\"\\.\\.\\."
543 gdb_test "p -elements 19 -- teststring" \
544 " = (.unsigned char .. )?\"teststring contents\""
545 gdb_test "p -elements 20 -- teststring" \
546 " = (.unsigned char .. )?\"teststring contents\""
547
548 gdb_test "print teststring2" \
549 " = \\(charptr\\) \"more contents\""
550
551 gdb_test_no_output "set print elements 8"
552
553 # Set the target-charset to ASCII, because the output varies from
554 # different charset.
555 with_target_charset "ASCII" {
556 gdb_test "p &ctable1\[0\]" \
557 " = \\(unsigned char \\*\\) <ctable1> \"\""
558 gdb_test "p &ctable1\[1\]" \
559 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"\\\\001\\\\002\\\\003\\\\004\\\\005\\\\006\\\\a\\\\b\"..."
560 gdb_test "p &ctable1\[1*8\]" \
561 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"\\\\b\\\\t\\\\n\\\\v\\\\f\\\\r\\\\016\\\\017\"..."
562 gdb_test "p &ctable1\[2*8\]" \
563 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"\\\\020\\\\021\\\\022\\\\023\\\\024\\\\025\\\\026\\\\027\"..."
564 gdb_test "p &ctable1\[3*8\]" \
565 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"\\\\030\\\\031\\\\032\\\\033\\\\034\\\\035\\\\036\\\\037\"..."
566 gdb_test "p &ctable1\[4*8\]" \
567 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \" !\\\\\"#\\\$%&'\"..."
568 gdb_test "p &ctable1\[5*8\]" \
569 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"\\(\\)\\*\\+,-./\"..."
570 gdb_test "p &ctable1\[6*8\]" \
571 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"01234567\"..."
572 gdb_test "p &ctable1\[7*8\]" \
573 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"89:;<=>\\?\"..."
574 gdb_test "p &ctable1\[8*8\]" \
575 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"@ABCDEFG\"..."
576 gdb_test "p &ctable1\[9*8\]" \
577 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"HIJKLMNO\"..."
578 gdb_test "p &ctable1\[10*8\]" \
579 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"PQRSTUVW\"..."
580 gdb_test "p &ctable1\[11*8\]" \
581 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"XYZ\\\[\\\\\\\\\\\]\\^_\"..."
582 gdb_test "p &ctable1\[12*8\]" \
583 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"`abcdefg\"..."
584 gdb_test "p &ctable1\[13*8\]" \
585 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"hijklmno\"..."
586 gdb_test "p &ctable1\[14*8\]" \
587 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"pqrstuvw\"..."
588 gdb_test "p &ctable1\[15*8\]" \
589 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"xyz\[{|}\]+\\~\\\\177\"..."
590 gdb_test "p &ctable1\[16*8\]" \
591 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"\\\\200\\\\201\\\\202\\\\203\\\\204\\\\205\\\\206\\\\207\"..."
592 gdb_test "p &ctable1\[17*8\]" \
593 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"\\\\210\\\\211\\\\212\\\\213\\\\214\\\\215\\\\216\\\\217\"..."
594 gdb_test "p &ctable1\[18*8\]" \
595 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"\\\\220\\\\221\\\\222\\\\223\\\\224\\\\225\\\\226\\\\227\"..."
596 gdb_test "p &ctable1\[19*8\]" \
597 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"\\\\230\\\\231\\\\232\\\\233\\\\234\\\\235\\\\236\\\\237\"..."
598 gdb_test "p &ctable1\[20*8\]" \
599 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"\\\\240\\\\241\\\\242\\\\243\\\\244\\\\245\\\\246\\\\247\"..."
600 gdb_test "p &ctable1\[21*8\]" \
601 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"\\\\250\\\\251\\\\252\\\\253\\\\254\\\\255\\\\256\\\\257\"..."
602 gdb_test "p &ctable1\[22*8\]" \
603 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"\\\\260\\\\261\\\\262\\\\263\\\\264\\\\265\\\\266\\\\267\"..."
604 gdb_test "p &ctable1\[23*8\]" \
605 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"\\\\270\\\\271\\\\272\\\\273\\\\274\\\\275\\\\276\\\\277\"..."
606 gdb_test "p &ctable1\[24*8\]" \
607 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"\\\\300\\\\301\\\\302\\\\303\\\\304\\\\305\\\\306\\\\307\"..."
608 gdb_test "p &ctable1\[25*8\]" \
609 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"\\\\310\\\\311\\\\312\\\\313\\\\314\\\\315\\\\316\\\\317\"..."
610 gdb_test "p &ctable1\[26*8\]" \
611 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"\\\\320\\\\321\\\\322\\\\323\\\\324\\\\325\\\\326\\\\327\"..."
612 gdb_test "p &ctable1\[27*8\]" \
613 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"\\\\330\\\\331\\\\332\\\\333\\\\334\\\\335\\\\336\\\\337\"..."
614 gdb_test "p &ctable1\[28*8\]" \
615 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"\\\\340\\\\341\\\\342\\\\343\\\\344\\\\345\\\\346\\\\347\"..."
616 gdb_test "p &ctable1\[29*8\]" \
617 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"\\\\350\\\\351\\\\352\\\\353\\\\354\\\\355\\\\356\\\\357\"..."
618 gdb_test "p &ctable1\[30*8\]" \
619 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"\\\\360\\\\361\\\\362\\\\363\\\\364\\\\365\\\\366\\\\367\"..."
620 gdb_test "p &ctable1\[31*8\]" \
621 " = \\(unsigned char \\*\\) <ctable1\\+$decimal> \"\\\\370\\\\371\\\\372\\\\373\\\\374\\\\375\\\\376\\\\377\"..."
622 }
623 }
624
625 proc test_print_int_arrays {} {
626 global gdb_prompt
627
628 gdb_test_no_output "set print elements 24" "elements 24 int arrays"
629
630 gdb_test_escape_braces "p int1dim" \
631 " = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}"
632 gdb_test_escape_braces "p int2dim" \
633 " = {{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}}"
634 gdb_test_escape_braces "p int3dim" \
635 " = {{{0, 1}, {2, 3}, {4, 5}}, {{6, 7}, {8, 9}, {10, 11}}}"
636 gdb_test_escape_braces "p int4dim" \
637 " = {{{{0, 1}, {2, 3}, {4, 5}}, {{6, 7}, {8, 9}, {10, 11}}}}"
638
639 # Some checks for various output formats.
640 gdb_test_escape_braces "p/x int4dim" \
641 " = {{{{0x0, 0x1}, {0x2, 0x3}, {0x4, 0x5}}, {{0x6, 0x7}, {0x8, 0x9}, {0xa, 0xb}}}}"
642 gdb_test_escape_braces "p/z int4dim" \
643 " = {{{{0x0+0, 0x0+1}, {0x0+2, 0x0+3}, {0x0+4, 0x0+5}}, {{0x0+6, 0x0+7}, {0x0+8, 0x0+9}, {0x0+a, 0x0+b}}}}"
644 gdb_test_escape_braces "p/o int4dim" \
645 " = {{{{0, 01}, {02, 03}, {04, 05}}, {{06, 07}, {010, 011}, {012, 013}}}}"
646 gdb_test_escape_braces "p/t int4dim" \
647 " = {{{{0, 1}, {10, 11}, {100, 101}}, {{110, 111}, {1000, 1001}, {1010, 1011}}}}"
648 }
649
650 proc test_print_typedef_arrays {} {
651 global gdb_prompt
652
653 gdb_test_no_output "set print elements 24" "elements 24 typedef_arrays"
654
655 gdb_test_escape_braces "p a1" \
656 " = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}"
657 gdb_test "p a1\[0\]" " = 2"
658 gdb_test "p a1\[9\]" " = 20"
659
660 gdb_test "p a2" \
661 " = \"abcd\""
662 gdb_test "p a2\[0\]" " = 97 'a'"
663 gdb_test "p a2\[3\]" " = 100 'd'"
664
665 # Regression test of null-stop; PR 11049.
666 gdb_test_no_output "set print null-stop on"
667 gdb_test "p a2" " = \"abcd\"" "print a2 with null-stop on"
668 gdb_test_no_output "set print null-stop off"
669 }
670
671 proc test_artificial_arrays {} {
672 # Send \026@ instead of just @ in case the kill character is @.
673 # \026 (ctrl-v) is to escape the next character (@), but it is
674 # not only unnecessary to do so on MingW hosts, but also harmful
675 # for the test because that character isn't recognized as an
676 # escape character.
677 set ctrlv "\026"
678 if [ishost *-*-mingw*] {
679 set ctrlv ""
680 }
681 gdb_test_escape_braces "p int1dim\[0\]${ctrlv}@2" " = {0, 1}" {p int1dim[0]@2}
682 gdb_test_escape_braces "p int1dim\[0\]${ctrlv}@2${ctrlv}@3" \
683 "({{0, 1}, {2, 3}, {4, 5}}|\[Cc\]annot.*)" \
684 {p int1dim[0]@2@3}
685 gdb_test_escape_braces "p int1dim\[0\]${ctrlv}@FE_TWO" " = {0, 1}" \
686 {p int1dim[0]@TWO}
687 gdb_test_escape_braces "p int1dim\[0\]${ctrlv}@FE_TWO${ctrlv}@three" \
688 "({{0, 1}, {2, 3}, {4, 5}}|\[Cc\]annot.*)" \
689 {p int1dim[0]@TWO@three}
690 gdb_test_escape_braces {p/x (short [])0x12345678} \
691 " = ({0x1234, 0x5678}|{0x5678, 0x1234})"
692 }
693
694 proc test_print_char_arrays {} {
695 global gdb_prompt
696 global hex decimal
697
698 gdb_test_no_output "set print elements 24" "elements 24 char_arrays"
699 gdb_test_no_output "set print address on"
700
701 gdb_test "p arrays" \
702 " = {array1 = \"abc\", array2 = \"d\", array3 = \"e\", array4 = \"fg\", array5 = \"hij\"}"
703
704 gdb_test "p parrays" " = \\(struct some_arrays \\*\\) $hex <arrays>"
705 gdb_test "p parrays->array1" " = \"abc\""
706 gdb_test "p &parrays->array1" " = \\(unsigned char \\(\\*\\)\\\[4\\\]\\) $hex <arrays>"
707 gdb_test "p parrays->array2" " = \"d\""
708 gdb_test "p &parrays->array2" " = \\(unsigned char \\(\\*\\)\\\[1\\\]\\) $hex <arrays\\+$decimal>"
709 gdb_test "p parrays->array3" " = \"e\""
710 gdb_test "p &parrays->array3" " = \\(unsigned char \\(\\*\\)\\\[1\\\]\\) $hex <arrays\\+$decimal>"
711 gdb_test "p parrays->array4" " = \"fg\""
712 gdb_test "p &parrays->array4" " = \\(unsigned char \\(\\*\\)\\\[2\\\]\\) $hex <arrays\\+$decimal>"
713 gdb_test "p parrays->array5" " = \"hij\""
714 gdb_test "p &parrays->array5" " = \\(unsigned char \\(\\*\\)\\\[4\\\]\\) $hex <arrays\\+$decimal>"
715
716 gdb_test_no_output "set print address off" "address off char arrays"
717 }
718
719 proc test_print_nibbles {} {
720 gdb_test_no_output "set print nibbles on"
721 foreach lang_line {
722 {"ada" "0" "0" "0011 0000" "1111 1111" "1111" "0001" "1111 0000 1111"}
723 {"asm" "0" "0" "0011 0000" "1111 1111" "1111" "0001" "1111 0000 1111"}
724 {"c" "0" "0" "0011 0000" "1111 1111" "1111" "0001" "1111 0000 1111"}
725 {"c++" "0" "0" "0011'0000" "1111'1111" "1111" "0001" "1111'0000'1111"}
726 {"d" "0" "0" "0011 0000" "1111 1111" "1111" "0001" "1111 0000 1111"}
727 {"fortran" "0" "0" "0011 0000" "1111 1111" "1111" "0001" "1111 0000 1111"}
728 {"go" "0" "0" "0011 0000" "1111 1111" "1111" "0001" "1111 0000 1111"}
729 {"minimal" "0" "0" "0011 0000" "1111 1111" "1111" "0001" "1111 0000 1111"}
730 {"objective-c" "0" "0" "0011 0000" "1111 1111" "1111" "0001" "1111 0000 1111"}
731 {"opencl" "0" "0" "0011 0000" "1111 1111" "1111" "0001" "1111 0000 1111"}
732 {"pascal" "0" "0" "0011 0000" "1111 1111" "1111" "0001" "1111 0000 1111"}
733 {"rust" "0" "0" "0011_0000" "1111_1111" "1111" "0001" "1111_0000_1111"}
734 } {
735 set lang [lindex $lang_line 0]
736 set val1 [lindex $lang_line 1]
737 set val2 [lindex $lang_line 2]
738 set val3 [lindex $lang_line 3]
739 set val4 [lindex $lang_line 4]
740 set val5 [lindex $lang_line 5]
741 set val6 [lindex $lang_line 6]
742 set val7 [lindex $lang_line 7]
743 set val8 [lindex $lang_line 8]
744
745 with_test_prefix "$lang" {
746 gdb_test_no_output "set language $lang"
747 gdb_test "p/t 0" $val1
748 gdb_test "p/t 0x0" $val2
749 gdb_test "p/t 0x30" $val3
750 gdb_test "p/t 0xff" $val4
751 gdb_test "p/t 0x0f" $val5
752 gdb_test "p/t 0x01" $val6
753 gdb_test "p/t 0xf0f" $val7
754 gdb_test "p/t 0x70f" $val8
755 gdb_test_no_output "set language auto"
756 }
757 }
758 gdb_test_no_output "set print nibbles off"
759 }
760
761 proc test_print_string_constants {} {
762 global gdb_prompt
763
764 gdb_test_no_output "set print elements 50"
765
766 if [target_info exists gdb,cannot_call_functions] {
767 unsupported "this target can not call functions"
768 return
769 }
770
771 # We need to up this because this can be really slow on some boards.
772 # (Test may involve inferior malloc() calls).
773 set timeout 60
774
775 gdb_test "p \"a string\"" " = \"a string\""
776 gdb_test "p \"embedded \\000 null\"" " = \"embedded \\\\000 null\""
777 gdb_test "p \"abcd\"\[2\]" " = 99 'c'"
778 gdb_test "p sizeof (\"abcdef\")" " = 7"
779 gdb_test "ptype \"foo\"" " = char \\\[4\\\]"
780 gdb_test "p *\"foo\"" " = 102 'f'"
781 gdb_test "ptype *\"foo\"" " = char"
782 gdb_test "p &*\"foo\"" " = \"foo\""
783 gdb_test "ptype &*\"foo\"" "type = char \\*"
784 gdb_test "p (char *)\"foo\"" " = \"foo\""
785 }
786
787 proc test_print_array_constants {} {
788 global hex
789
790 if [target_info exists gdb,cannot_call_functions] {
791 unsupported "this target can not call functions"
792 return
793 }
794
795 # We need to up this because this can be really slow on some boards.
796 # (Test may involve inferior malloc() calls).
797 set timeout 60
798
799 gdb_test "print {'a','b','c'}" " = \"abc\""
800 gdb_test_escape_braces "print {0,1,2}" " = {0, 1, 2}"
801 gdb_test_escape_braces "print {(long)0,(long)1,(long)2}" " = {0, 1, 2}"
802 gdb_test_escape_braces "print {{0,1,2},{3,4,5}}" " = {{0, 1, 2}, {3, 4, 5}}"
803 gdb_test "print {4,5,6}\[2\]" " = 6"
804 gdb_test "print *&{4,5,6}\[1\]" "Attempt to take address of value not located in memory."
805
806 # This used to cause a crash.
807 set val [string_to_regexp {"\377\377\377\377"}]
808 gdb_test "print {unsigned char\[\]}{0xffffffff}" " = $val"
809 }
810
811 proc test_print_enums {} {
812 # Regression test for PR11827.
813 gdb_test "print some_volatile_enum" "enumvolval1"
814
815 # Print a flag enum.
816 gdb_test "print three" [string_to_regexp " = (FE_ONE | FE_TWO)"]
817
818 # Print a flag enum with value 0, where an enumerator has value 0.
819 gdb_test "print (enum flag_enum) 0x0" [string_to_regexp " = FE_NONE"]
820
821 # Print a flag enum with value 0, where no enumerator has value 0.
822 gdb_test "print flag_enum_without_zero" [string_to_regexp " = 0"]
823
824 # Print a flag enum with unknown bits set.
825 gdb_test "print (enum flag_enum) 0xf1" [string_to_regexp " = (FE_ONE | unknown: 0xf0)"]
826
827 # Test printing an enum not considered a "flag enum" (because one of its
828 # enumerators has multiple bits set).
829 gdb_test "print three_not_flag" [string_to_regexp " = 3"]
830 }
831
832 proc test_printf {} {
833 gdb_test "printf \"x=%d,y=%d,z=%d\\n\", 5, 6, 7" "x=5,y=6,z=7"
834 gdb_test "printf \"string=%.4sxx\\n\", teststring" "string=testxx"
835 gdb_test "printf \"string=%sxx\\n\", teststring" \
836 "string=teststring contentsxx"
837
838 gdb_test "printf \"%f is fun\\n\", 1.0" "1\.0+ is fun"
839
840 # Test mixing args of different sizes.
841 gdb_test "printf \"x=%d,y=%f,z=%d\\n\", 5, 6.0, 7" "x=5,y=6\.0+,z=7"
842 gdb_test "printf \"%x %f, %c %x, %x, %f\\n\", 0xbad, -99.541, 'z',\
843 0xfeedface, 0xdeadbeef, 5.0" "bad -99.54\[0-9\]+, z feedface, deadbeef, 5.0+"
844
845 # Regression test for C lexer bug.
846 gdb_test "printf \"%c\\n\", \"x\"\[1,0\]" "x"
847
848 # Regression test for "%% at end of format string.
849 # See http://sourceware.org/bugzilla/show_bug.cgi?id=11345
850 gdb_test "printf \"%%%d%%\\n\", 5" "%5%"
851
852 # Some tests for missing format specifier after '%'.
853 gdb_test "printf \"%\", 0" "Incomplete format specifier at end of format string"
854 gdb_test "printf \"%.234\", 0" "Incomplete format specifier at end of format string"
855 gdb_test "printf \"%-\", 0" "Incomplete format specifier at end of format string"
856 gdb_test "printf \"%-23\", 0" "Incomplete format specifier at end of format string"
857
858 # Test for invalid printf flags on pointer types.
859 gdb_test "printf \"%#p\", 0" "Inappropriate modifiers to format specifier 'p' in printf"
860 gdb_test "printf \"% p\", 0" "Inappropriate modifiers to format specifier 'p' in printf"
861 gdb_test "printf \"%0p\", 0" "Inappropriate modifiers to format specifier 'p' in printf"
862 gdb_test "printf \"%+p\", 0" "Inappropriate modifiers to format specifier 'p' in printf"
863
864
865 gdb_test "define hibob\nprintf \"hi bob \"\nshell echo zzz\nprintf \"y\\n\"\nend" \
866 "" \
867 "create hibob command"
868 gdb_test "hibob" "hi bob zzz.*y" "run hibob command"
869
870 # PR cli/19918.
871 gdb_test "printf \"%-16dq\\n\", 0" "0 q"
872 gdb_test "printf \"%-16pq\\n\", 0" "\\(nil\\) q"
873
874 # PR cli/14977.
875 gdb_test "printf \"%s\\n\", 0" "\\(null\\)"
876 }
877
878 #Test printing DFP values with printf
879 proc test_printf_with_dfp {} {
880
881 # Test various dfp values, covering 32-bit, 64-bit and 128-bit ones
882
883 # _Decimal32 constants, which can support up to 7 digits
884 gdb_test "printf \"%Hf\\n\",1.2df" "1.2"
885 gdb_test "printf \"%Hf\\n\",-1.2df" "-1.2"
886 gdb_test "printf \"%Hf\\n\",1.234567df" "1.234567"
887 gdb_test "printf \"%Hf\\n\",-1.234567df" "-1.234567"
888 gdb_test "printf \"%Hf\\n\",1234567.df" "1234567"
889 gdb_test "printf \"%Hf\\n\",-1234567.df" "-1234567"
890
891 gdb_test "printf \"%Hf\\n\",1.2E1df" "12"
892 gdb_test "printf \"%Hf\\n\",1.2E10df" "1.2E\\+10"
893 gdb_test "printf \"%Hf\\n\",1.2E-10df" "1.2E-10"
894
895 # The largest exponent for 32-bit dfp value is 96.
896 gdb_test "printf \"%Hf\\n\",1.2E96df" "1.200000E\\+96"
897
898 # _Decimal64 constants, which can support up to 16 digits
899 gdb_test "printf \"%Df\\n\",1.2dd" "1.2"
900 gdb_test "printf \"%Df\\n\",-1.2dd" "-1.2"
901 gdb_test "printf \"%Df\\n\",1.234567890123456dd" "1.234567890123456"
902 gdb_test "printf \"%Df\\n\",-1.234567890123456dd" "-1.234567890123456"
903 gdb_test "printf \"%Df\\n\",1234567890123456.dd" "1234567890123456"
904 gdb_test "printf \"%Df\\n\",-1234567890123456.dd" "-1234567890123456"
905
906 gdb_test "printf \"%Df\\n\",1.2E1dd" "12"
907 gdb_test "printf \"%Df\\n\",1.2E10dd" "1.2E\\+10"
908 gdb_test "printf \"%Df\\n\",1.2E-10dd" "1.2E-10"
909
910 # The largest exponent for 64-bit dfp value is 384.
911 gdb_test "printf \"%Df\\n\",1.2E384dd" "1.200000000000000E\\+384"
912
913 # _Decimal128 constants, which can support up to 34 digits
914 gdb_test "printf \"%DDf\\n\",1.2dl" "1.2"
915 gdb_test "printf \"%DDf\\n\",-1.2dl" "-1.2"
916 gdb_test "printf \"%DDf\\n\",1.234567890123456789012345678901234dl" "1.234567890123456789012345678901234"
917 gdb_test "printf \"%DDf\\n\",-1.234567890123456789012345678901234dl" "-1.234567890123456789012345678901234"
918 gdb_test "printf \"%DDf\\n\",1234567890123456789012345678901234.dl" "1234567890123456789012345678901234"
919 gdb_test "printf \"%DDf\\n\",-1234567890123456789012345678901234.dl" "-1234567890123456789012345678901234"
920
921 gdb_test "printf \"%DDf\\n\",1.2E1dl" "12"
922 gdb_test "printf \"%DDf\\n\",1.2E10dl" "1.2E\\+10"
923 gdb_test "printf \"%DDf\\n\",1.2E-10dl" "1.2E-10"
924
925 # The largest exponent for 128-bit dfp value is 6144.
926 gdb_test "printf \"%DDf\\n\",1.2E6144dl" "1.200000000000000000000000000000000E\\+6144"
927
928 # GDB used to get this wrong.
929 gdb_test "printf \"%Hf %Hf\\n\",1.2df,1.3df" "1.2 1.3"
930 }
931
932 proc test_print_symbol {} {
933 gdb_test_no_output "set print symbol on"
934
935 gdb_test "print &three" " = .* <three>"
936 gdb_test "print parrays" " = .* <arrays>"
937
938 # In case somebody adds tests after this.
939 gdb_test_no_output "set print symbol off"
940 }
941
942 # Escape a left curly brace to prevent it from being interpreted as
943 # the beginning of a bound
944 proc gdb_test_escape_braces { args } {
945
946 set pattern [lindex $args 1]
947 regsub -all {\{[0-9]} $pattern {\\&} esc_pattern
948 gdb_test [lindex $args 0] $esc_pattern [lindex $args 2]
949 }
950
951 proc test_repeat_bytes {} {
952 set start(E) {}
953 set start(S) {a}
954 set start(L) {abaabbaaabbb}
955 set start(R) {'a' <repeats 20 times>}
956 set end(E) {}
957 set end(S) {c}
958 set end(L) {cdccddcccddd}
959 set end(R) {'c' <repeats 20 times>}
960 set invalid(S) {\\240}
961 set invalid(L) {\\240\\240\\240\\240}
962 set invalid(R) {'\\240' <repeats 20 times>}
963
964 set fmt(SSS) "\"%s%s%s\""
965 set fmt(SSR) "\"%s%s\", %s"
966 set fmt(SRS) "\"%s\", %s, \"%s\""
967 set fmt(RSS) "%s, \"%s%s\""
968 set fmt(RSR) "%s, \"%s\", %s"
969 set fmt(SRR) "\"%s\", %s, %s"
970 set fmt(RRS) "%s, %s, \"%s\""
971 set fmt(RRR) "%s, %s, %s"
972
973 set fmt(RS) "%s, \"%s\""
974 set fmt(RR) "%s, %s"
975 set fmt(SR) "\"%s\", %s"
976 set fmt(SS) "\"%s%s\""
977
978 # Set the target-charset to ASCII, because the output varies from
979 # different charset.
980 with_target_charset "ASCII" {
981 # Test the various permutations of invalid characters
982 foreach i [array names invalid] {
983 set I $i
984
985 if {$i == "L"} {
986 set i "S"
987 }
988
989 foreach s [array names start] {
990 set S $s
991
992 if {$s == "L"} {
993 set s "S"
994 }
995
996
997 foreach e [array names end] {
998 set E $e
999
1000 if {$e == "L"} {
1001 set e "S"
1002 }
1003
1004 # Skip E*E.
1005 if {$s == "E" && $e == "E"} { continue }
1006
1007 # Special cases...
1008 if {$s == "E"} {
1009 set result [format $fmt($i$e) $invalid($I) $end($E)]
1010 } elseif {$e == "E"} {
1011 set result [format $fmt($s$i) $start($S) $invalid($I)]
1012 } else {
1013 set result [format $fmt($s$i$e) \
1014 $start($S) $invalid($I) $end($E)]
1015 }
1016
1017 send_log "expecting: = $result\n"
1018 gdb_test "print invalid_$S$I$E" "= $result"
1019 }
1020 }
1021 }
1022 }
1023 }
1024
1025 # Test printf of convenience variables.
1026 # These tests can be done with or without a running inferior.
1027 # PREFIX ensures uniqueness of test names.
1028
1029 proc test_printf_convenience_var {prefix} {
1030
1031 with_test_prefix "conv var: $prefix" {
1032 gdb_test_no_output "set var \$cstr = \"abcde\"" "set \$cstr"
1033 gdb_test "printf \"cstr val = %s\\n\", \$cstr" "cstr val = abcde" \
1034 "printf \$cstr"
1035 gdb_test_no_output "set var \$abcde = \"ABCDE\"" "set \$abcde"
1036 gdb_test "eval \"print \$%s\\n\", \$cstr" "= \"ABCDE\"" \
1037 "indirect print abcde"
1038 # Without a target, the below produces no output
1039 # but with a target, it gives a warning.
1040 # So, use gdb_test expecting ".*" instead of gdb_test_no_output.
1041 gdb_test "set language ada" ".*"
1042 gdb_test_no_output "set var \$astr := \"fghij\"" "set \$astr"
1043 gdb_test "printf \"astr val = %s\\n\", \$astr" "astr val = fghij" \
1044 "printf \$astr"
1045 gdb_test_no_output "set language auto" "set language auto"
1046 gdb_test "printf \"astr val = %s\\n\", \$astr" "astr val = fghij" \
1047 "printf \$astr, auto language"
1048 # Wide strings can only be created when wchar_t type is known.
1049 # Switch to c++ for the wide strings tests, as wchar_t is predefined
1050 # when current language is c++.
1051 # See above "set language ada" about why we use gdb_test.
1052 gdb_test "set language c++" ".*"
1053 gdb_test_no_output "set var \$wstr = L\"facile\"" \
1054 "set \$wstr"
1055 gdb_test "printf \"wstr val = %ls\\n\", \$wstr" \
1056 "wstr val = facile" "printf \$wstr"
1057 gdb_test_no_output "set language auto" "set language auto, wstring"
1058 }
1059 }
1060
1061
1062 # Start with a fresh gdb.
1063
1064 gdb_exit
1065 gdb_start
1066 gdb_reinitialize_dir $srcdir/$subdir
1067
1068 gdb_test "print \$pc" "No registers\\."
1069
1070 # Some simple operations on strings should work even without a target
1071 # (and therefore without calling malloc).
1072 gdb_test "print \"abc\"" " = \"abc\""
1073 gdb_test "print sizeof (\"abc\")" " = 4"
1074 gdb_test "ptype \"abc\"" " = char \\\[4\\\]"
1075 gdb_test "print \$cvar = \"abc\"" " = \"abc\""
1076 gdb_test "print sizeof (\$cvar)" " = 4"
1077
1078 # Similarly, printf of a string convenience var should work without a target.
1079 test_printf_convenience_var "no target"
1080
1081 # Test void results.
1082 gdb_test "p (void)10" " = void"
1083 gdb_test "p/x (void)10" " = void"
1084 gdb_test "p \$abcd" " = void"
1085
1086 # GDB used to complete the explicit location options even when
1087 # printing expressions.
1088 gdb_test_no_output "complete p -function"
1089 gdb_test_no_output "complete p -line"
1090 gdb_test_no_output "complete p -source"
1091
1092 gdb_file_cmd ${binfile}
1093
1094 gdb_test "print \$pc" "No registers\\." "print \$pc (with file)"
1095
1096 gdb_test_no_output "set print sevenbit-strings"
1097 gdb_test_no_output "set print address off"
1098 gdb_test_no_output "set width 0"
1099
1100 if { [test_compiler_info "armcc-*"] } {
1101 # ARM RealView compresses large arrays in the data segment.
1102 # Before the program starts, we can not read them. There is
1103 # nothing in the file to indicate that data is compressed.
1104 setup_xfail "arm*-*-eabi"
1105 }
1106 gdb_test "p ctable1\[120\]" "120 'x'" "p ctable1\[120\] #1"
1107
1108 gdb_load ${binfile}
1109
1110 if {![runto_main]} {
1111 return 0
1112 }
1113
1114 # With a running target, printf convenience vars should of course work.
1115 test_printf_convenience_var "with target"
1116
1117 # It should also work when inferior function calls are forbidden.
1118 gdb_test_no_output "set may-call-functions off"
1119 test_printf_convenience_var "with target, may-call-functions off"
1120 gdb_test_no_output "set may-call-functions on"
1121
1122 # Test printf of a variable that holds the address to a substring in
1123 # the inferior. This test will not work without a target.
1124 gdb_test_no_output "set var \$test_substr = \(char \*\) \(&teststring\[0\] + 4\)" \
1125 "set \$test_substr var"
1126 gdb_test "printf \"test_substr val = %s\\n\", \$test_substr" \
1127 "test_substr val = string contents" \
1128 "print \$test_substr"
1129
1130 test_integer_literals_accepted
1131 test_integer_literals_rejected
1132 test_float_accepted
1133 test_float_rejected
1134 test_character_literals_accepted
1135 test_print_all_chars
1136 test_print_repeats_10
1137 test_print_repeats_embedded_array
1138 test_print_strings
1139 test_print_int_arrays
1140 test_print_typedef_arrays
1141 test_artificial_arrays
1142 test_print_char_arrays
1143 test_print_nibbles
1144 # We used to do the runto main here.
1145 test_print_string_constants
1146 test_print_array_constants
1147 test_print_enums
1148 test_printf
1149 test_printf_with_dfp
1150 test_print_symbol
1151 test_repeat_bytes
1152 test_radices