aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.arch/mips16-thunks.exp
blob: 417adcbb3d1f25bd588f571f3024699bb5b9af1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# Copyright 2012-2013 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Contributed by Mentor Graphics, written by Maciej W. Rozycki.

# Test MIPS16 thunk support.

# This should work on any targets that support MIPS16 execution, including
# Linux and bare-iron ones, but not all of them do, for example MIPS16
# support has been added to Linux relatively late in the game.  Also besides
# environment support, the target processor has to support the MIPS16 ASE.
# Finally as of this writing MIPS16 support has only been implemented in the
# toolchain for a subset of ABIs, so we need to check that a MIPS16
# executable can be built and run at all before we attempt the actual test.

if { ![istarget "mips*-*-*"] } then {
    verbose "Skipping MIPS16 thunk support tests."
    return
}

# A helper to set caller's SRCFILE and OBJFILE based on FILENAME and SUFFIX.
proc set_src_and_obj { filename { suffix "" } } {
    upvar srcfile srcfile
    upvar objfile objfile
    global srcdir
    global objdir
    global subdir

    if ![string equal "$suffix" ""] then {
	set suffix "-$suffix"
    }
    set srcfile ${srcdir}/${subdir}/${filename}.c
    set objfile ${objdir}/${subdir}/${filename}${suffix}.o
}

# First check if a trivial MIPS16 program can be built and debugged.  This
# verifies environment and processor support, any failure here must be
# classed as the lack of support.
set testname mips16-thunks-main

set_src_and_obj mips16-thunks-inmain
set options [list debug nowarnings additional_flags=-mips16]
set objfiles ${objfile}
gdb_compile ${srcfile} ${objfile} object ${options}

set_src_and_obj mips16-thunks-main
set options [list debug nowarnings additional_flags=-mips16]
lappend objfiles ${objfile}
gdb_compile ${srcfile} ${objfile} object ${options}

set binfile ${objdir}/${subdir}/${testname}
set options [list debug nowarnings]
if { [gdb_compile ${objfiles} ${binfile} executable ${options}] != "" } then {
    unsupported "No MIPS16 support in the toolchain."
    return
}
clean_restart ${testname}
gdb_breakpoint inmain
gdb_run_cmd
gdb_test_multiple "" "check for MIPS16 support in the processor" {
    -re "Breakpoint 1.*inmain .*$gdb_prompt $" {
	gdb_test_multiple "finish" \
	    "check for MIPS16 support in the processor" {
	    -re "Value returned is \\\$\[0-9\]+ = 0\[^0-9\].*$gdb_prompt $" {
		verbose "MIPS16 support check successful."
	    }
	    -re "$gdb_prompt $" {
		unsupported "No MIPS16 support in the processor."
		return
	    }
	    default {
		unsupported "No MIPS16 support in the processor."
		return
	    }
	}
    }
    -re "$gdb_prompt $" {
	unsupported "No MIPS16 support in the processor."
	return
    }
    default {
	unsupported "No MIPS16 support in the processor."
	return
    }
}

# Check if MIPS16 PIC code can be built and debugged.  We want to check
# PIC and MIPS16 thunks are handled correctly together if possible, but
# on targets that do not support PIC code, e.g. bare iron, we still want
# to test the rest of functionality.
set testname mips16-thunks-pic
set picflag ""

set_src_and_obj mips16-thunks-inmain pic
set options [list \
    debug nowarnings additional_flags=-mips16 additional_flags=-fPIC]
set objfiles ${objfile}
gdb_compile ${srcfile} ${objfile} object ${options}

set_src_and_obj mips16-thunks-main pic
set options [list \
    debug nowarnings additional_flags=-mips16 additional_flags=-fPIC]
lappend objfiles ${objfile}
gdb_compile ${srcfile} ${objfile} object ${options}

set binfile ${objdir}/${subdir}/${testname}
set options [list debug nowarnings additional_flags=-fPIC]
if { [gdb_compile ${objfiles} ${binfile} executable ${options}] == "" } then {
    clean_restart ${testname}
    gdb_breakpoint inmain
    gdb_run_cmd
    gdb_test_multiple "" "check for PIC support" {
	-re "Breakpoint 1.*inmain .*$gdb_prompt $" {
	    note "PIC support present, will make additional PIC thunk checks."
	    set picflag additional_flags=-fPIC
	}
	-re "$gdb_prompt $" {
	    note "No PIC support, skipping additional PIC thunk checks."
	}
	default {
	    note "No PIC support, skipping additional PIC thunk checks."
	}
    }
} else {
    note "No PIC support, skipping additional PIC thunk checks."
}

# OK, build the twisted executable.  This program contains the following
# MIPS16 thunks:
# - __call_stub_fp_sin,
# - __call_stub_fp_sinblah,
# - __call_stub_fp_sinfrob,
# - __call_stub_fp_sinhelper,
# - __call_stub_lsinhelper,
# - __fn_stub_lsinmips16,
# - __fn_stub_sinblah16,
# - __fn_stub_sinfrob16,
# - __fn_stub_sinmips16,
# - __mips16_call_stub_df_2,
# - __mips16_ret_df.
# Additionally, if PIC code is supported, it contains the following PIC thunks:
# - .pic.__mips16_call_stub_df_2,
# - .pic.__mips16_ret_df,
# - .pic.sinblah,
# - .pic.sinblah16,
# - .pic.sinfrob,
# - .pic.sinfrob16.
set testname mips16-thunks-sin

set_src_and_obj mips16-thunks-sinmain
set options [list debug nowarnings additional_flags=-mips16]
set objfiles ${objfile}
gdb_compile ${srcfile} ${objfile} object ${options}

set_src_and_obj mips16-thunks-sin
set options [list debug nowarnings additional_flags=-mno-mips16]
lappend objfiles ${objfile}
gdb_compile ${srcfile} ${objfile} object ${options}

set_src_and_obj mips16-thunks-sinmips16
set options [list debug nowarnings additional_flags=-mips16]
lappend objfiles ${objfile}
gdb_compile ${srcfile} ${objfile} object ${options}

set_src_and_obj mips16-thunks-sinfrob
set options [list \
    debug nowarnings additional_flags=-mno-mips16 ${picflag}]
lappend objfiles ${objfile}
gdb_compile ${srcfile} ${objfile} object ${options}

set_src_and_obj mips16-thunks-sinfrob16
set options [list \
    debug nowarnings additional_flags=-mips16 ${picflag}]
lappend objfiles ${objfile}
gdb_compile ${srcfile} ${objfile} object ${options}

set binfile ${objdir}/${subdir}/${testname}
set options [list debug nowarnings]
gdb_compile ${objfiles} ${binfile} executable ${options}
clean_restart ${testname}
if ![runto_main] then {
    fail "running test program, MIPS16 thunk tests aborted"
    return
}

# Build some useful regular expressions out of a list of functions FUNCS
# to be used to match against backtraces.
proc build_frames_re { funcs } {
    upvar anyframe anyframe
    upvar frames frames
    upvar frame frame
    upvar func func

    set fid 0
    set argsandsource " +\\\(.*\\\) +at +\[^\r\n\]+\r\n"
    set addrin "(?:\[^ \]+ +in +)?"
    set anyframe "#${fid} +${addrin}(\[^ \]+)${argsandsource}"
    set frame "#${fid} +${addrin}${func}${argsandsource}"
    set frames "$frame"
    foreach f [lrange $funcs 1 end] {
	incr fid
	append frames "#${fid} +${addrin}${f}${argsandsource}"
    }
}

# Single-step through the function that is at the head of function list
# FUNCS until a different function (frame) is reached.  Before each step
# check the backtrace against FUNCS.  ID is used for reporting, to tell
# apart different calls to this procedure for the same function.  If
# successful, then return the name of the function we have stopped in.
proc step_through { id funcs } {
    global gdb_prompt

    set func [lindex $funcs 0]
    build_frames_re "$funcs"

    set msg "single-stepping through \"${func}\" ($id)"

    # Arbitrarily limit the maximium number of steps made to avoid looping
    # indefinitely in the case something goes wrong, increase as (if)
    # necessary.
    set count 8
    while { $count > 0 } {
	if { [gdb_test_multiple "backtrace" "$msg (backtrace)" {
	    -re "${frames}$gdb_prompt $" {
		if { [gdb_test_multiple "step" "$msg (step)" {
		    -re "$gdb_prompt $" {
			if { [gdb_test_multiple "frame" "$msg (frame)" {
			    -re "${frame}.*$gdb_prompt $" {
			    }
			    -re "${anyframe}.*$gdb_prompt $" {
				pass "$msg"
				return $expect_out(1,string)
			    }
			}] != 0 } then {
			    return ""
			}
		    }
		}] != 0 } then {
		    return ""
		}
	    }
	}] != 0 } then {
	    return ""
	}
	incr count -1
    }
    fail "$msg (too many steps)"
    return ""
}

# Finish the current function that must be one that is at the head of
# function list FUNCS.  Before that check the backtrace against FUNCS.
# ID is used for reporting, to tell apart different calls to this
# procedure for the same function.  If successful, then return the name
# of the function we have stopped in.
proc finish_through { id funcs } {
    global gdb_prompt

    set func [lindex $funcs 0]
    build_frames_re "$funcs"

    set msg "finishing \"${func}\" ($id)"

    gdb_test_multiple "backtrace" "$msg (backtrace)" {
	-re "${frames}$gdb_prompt $" {
	    gdb_test_multiple "finish" "$msg (finish)" {
		-re "Run till exit from ${frame}.*$gdb_prompt $" {
		    gdb_test_multiple "frame" "$msg (frame)" {
			-re "${anyframe}.*$gdb_prompt $" {
			    pass "$msg"
			    return $expect_out(1,string)
			}
		    }
		}
	    }
	}
    }
    return ""
}

# Report PASS if VAL is equal to EXP, otherwise report FAIL, using MSG.
proc pass_if_eq { val exp msg } {
    if [string equal "$val" "$exp"] then {
	pass "$msg"
    } else {
	fail "$msg"
    }
}

# Check if FUNC is equal to WANT.  If not, then assume that we have stepped
# into a library call.  In this case finish it, then step out of the caller.
# ID is used for reporting, to tell apart different calls to this procedure
# for the same function.  If successful, then return the name of the
# function we have stopped in.
proc finish_if_ne { id func want funcs } {
    if ![string equal "$func" "$want"] then {
	set call "$func"
	set want [lindex $funcs 0]
	set func [finish_through "$id" [linsert $funcs 0 "$func"]]
	pass_if_eq "$func" "$want" "\"${call}\" finishing to \"${want}\" ($id)"
	set func [step_through "$id" $funcs]
    }
    return "$func"
}

# Now single-step through the program, making sure all thunks are correctly
# stepped over and omitted from backtraces.

set id 1
set func [step_through $id [list main]]
pass_if_eq "$func" sinfrob16 "stepping from \"main\" into \"sinfrob16\" ($id)"

incr id
set func [step_through $id [list sinfrob16 main]]
set func [finish_if_ne $id "$func" main [list sinfrob16 main]]
pass_if_eq "$func" main "stepping from \"sinfrob16\" back to \"main\" ($id)"

incr id
set func [step_through $id [list main]]
pass_if_eq "$func" sinfrob "stepping from \"main\" into \"sinfrob\" ($id)"

incr id
set func [step_through $id [list sinfrob main]]
set func [finish_if_ne $id "$func" main [list sinfrob main]]
pass_if_eq "$func" main "stepping from \"sinfrob\" back to \"main\" ($id)"

# 5
incr id
set func [step_through $id [list main]]
pass_if_eq "$func" sinhelper "stepping from \"main\" into \"sinhelper\" ($id)"

incr id
set func [step_through $id [list sinhelper main]]
set func [finish_if_ne $id "$func" sinfrob16 [list sinhelper main]]
pass_if_eq "$func" sinfrob16 \
    "stepping from \"sinhelper\" into \"sinfrob16\" ($id)"

incr id
set func [step_through $id [list sinfrob16 sinhelper main]]
set func [finish_if_ne $id "$func" sinhelper [list sinfrob16 sinhelper main]]
pass_if_eq "$func" sinhelper \
    "stepping from \"sinfrob16\" back to \"sinhelper\" ($id)"

incr id
set func [step_through $id [list sinhelper main]]
pass_if_eq "$func" sinfrob "stepping from \"sinhelper\" into \"sinfrob\" ($id)"

incr id
set func [step_through $id [list sinfrob sinhelper main]]
set func [finish_if_ne $id "$func" sinhelper [list sinfrob sinhelper main]]
pass_if_eq "$func" sinhelper \
    "stepping from \"sinfrob\" back to \"sinhelper\" ($id)"

# 10
incr id
set func [step_through $id [list sinhelper main]]
pass_if_eq "$func" sinmips16 \
    "stepping from \"sinhelper\" into \"sinmips16\" ($id)"

incr id
set func [step_through $id [list sinmips16 sinhelper main]]
set func [finish_if_ne $id "$func" sinfrob16 [list sinmips16 sinhelper main]]
pass_if_eq "$func" sinfrob16 \
    "stepping from \"sinmips16\" into \"sinfrob16\" ($id)"

incr id
set func [step_through $id [list sinfrob16 sinmips16 sinhelper main]]
set func [finish_if_ne $id "$func" sinmips16 \
	      [list sinfrob16 sinmips16 sinhelper main]]
pass_if_eq "$func" sinmips16 \
    "stepping from \"sinfrob16\" back to \"sinmips16\" ($id)"

incr id
set func [step_through $id [list sinmips16 sinhelper main]]
pass_if_eq "$func" sinfrob "stepping from \"sinmips16\" into \"sinfrob\" ($id)"

incr id
set func [step_through $id [list sinfrob sinmips16 sinhelper main]]
set func [finish_if_ne $id "$func" sinhelper \
	      [list sinfrob sinmips16 sinhelper main]]
pass_if_eq "$func" sinmips16 \
    "stepping from \"sinfrob\" back to \"sinmips16\" ($id)"

# 15
incr id
set func [step_through $id [list sinmips16 sinhelper main]]
pass_if_eq "$func" sinfrob16 \
    "stepping from \"sinmips16\" into \"sinfrob16\" (indirectly) ($id)"

incr id
set func [step_through $id [list sinfrob16 sinmips16 sinhelper main]]
set func [finish_if_ne $id "$func" sinmips16 \
	      [list sinfrob16 sinmips16 sinhelper main]]
pass_if_eq "$func" sinmips16 \
    "stepping from \"sinfrob16\" back to \"sinmips16\" (indirectly) ($id)"

incr id
set func [step_through $id [list sinmips16 sinhelper main]]
pass_if_eq "$func" sinfrob \
    "stepping from \"sinmips16\" into \"sinfrob\" (indirectly) ($id)"

incr id
set func [step_through $id [list sinfrob sinmips16 sinhelper main]]
set func [finish_if_ne $id "$func" sinhelper \
	      [list sinfrob sinmips16 sinhelper main]]
pass_if_eq "$func" sinmips16 \
    "stepping from \"sinfrob\" back to \"sinmips16\" (indirectly) ($id)"

incr id
set func [step_through $id [list sinmips16 sinhelper main]]
pass_if_eq "$func" sinhelper \
    "stepping from \"sinmips16\" back to \"sinhelper\" ($id)"

# 20
incr id
set func [step_through $id [list sinhelper main]]
pass_if_eq "$func" main "stepping from \"sinhelper\" back to \"main\" ($id)"

incr id
set func [step_through $id [list main]]
pass_if_eq "$func" sinblah "stepping from \"main\" into \"sinblah\" ($id)"

incr id
set func [step_through $id [list sinblah main]]
set func [finish_if_ne $id "$func" main [list sinblah main]]
pass_if_eq "$func" main "stepping from \"sinblah\" back to \"main\" ($id)"

incr id
set func [step_through $id [list main]]
pass_if_eq "$func" sinblah16 "stepping from \"main\" into \"sinblah16\" ($id)"

incr id
set func [step_through $id [list sinblah16 main]]
set func [finish_if_ne $id "$func" main [list sinblah16 main]]
pass_if_eq "$func" main "stepping from \"sinblah16\" back to \"main\" ($id)"

# 25
incr id
set func [step_through $id [list main]]
pass_if_eq "$func" lsinhelper \
    "stepping from \"main\" into \"lsinhelper\" ($id)"

incr id
set func [step_through $id [list lsinhelper main]]
set func [finish_if_ne $id "$func" sinblah [list lsinhelper main]]
pass_if_eq "$func" sinblah \
    "stepping from \"lsinhelper\" into \"sinblah\" ($id)"

incr id
set func [step_through $id [list sinblah lsinhelper main]]
set func [finish_if_ne $id "$func" lsinhelper [list sinblah lsinhelper main]]
pass_if_eq "$func" lsinhelper \
    "stepping from \"sinblah\" back to \"lsinhelper\" ($id)"

incr id
set func [step_through $id [list lsinhelper main]]
pass_if_eq "$func" sinblah16 \
    "stepping from \"lsinhelper\" into \"sinblah16\" ($id)"

incr id
set func [step_through $id [list sinblah16 lsinhelper main]]
set func [finish_if_ne $id "$func" lsinhelper [list sinblah16 lsinhelper main]]
pass_if_eq "$func" lsinhelper \
    "stepping from \"sinblah16\" back to \"lsinhelper\" ($id)"

# 30
incr id
set func [step_through $id [list lsinhelper main]]
pass_if_eq "$func" lsinmips16 \
    "stepping from \"lsinhelper\" into \"lsinmips16\" ($id)"

incr id
set func [step_through $id [list lsinmips16 lsinhelper main]]
set func [finish_if_ne $id "$func" sinblah [list lsinmips16 lsinhelper main]]
pass_if_eq "$func" sinblah \
    "stepping from \"lsinmips16\" into \"sinblah\" ($id)"

incr id
set func [step_through $id [list sinblah lsinmips16 lsinhelper main]]
set func [finish_if_ne $id "$func" lsinmips16 \
	      [list sinblah lsinmips16 lsinhelper main]]
pass_if_eq "$func" lsinmips16 \
    "stepping from \"sinblah\" back to \"lsinmips16\" ($id)"

incr id
set func [step_through $id [list lsinmips16 lsinhelper main]]
pass_if_eq "$func" sinblah16 \
    "stepping from \"lsinmips16\" into \"sinblah16\" ($id)"

incr id
set func [step_through $id [list sinblah16 lsinmips16 lsinhelper main]]
set func [finish_if_ne $id "$func" lsinhelper \
	      [list sinblah16 lsinmips16 lsinhelper main]]
pass_if_eq "$func" lsinmips16 \
    "stepping from \"sinblah16\" back to \"lsinmips16\" ($id)"

# 35
incr id
set func [step_through $id [list lsinmips16 lsinhelper main]]
pass_if_eq "$func" sinblah \
    "stepping from \"lsinmips16\" into \"sinblah\" (indirectly) ($id)"

incr id
set func [step_through $id [list sinblah lsinmips16 lsinhelper main]]
set func [finish_if_ne $id "$func" lsinmips16 \
	      [list sinblah lsinmips16 lsinhelper main]]
pass_if_eq "$func" lsinmips16 \
    "stepping from \"sinblah\" back to \"lsinmips16\" (indirectly) ($id)"

incr id
set func [step_through $id [list lsinmips16 lsinhelper main]]
pass_if_eq "$func" sinblah16 \
    "stepping from \"lsinmips16\" into \"sinblah16\" (indirectly) ($id)"

incr id
set func [step_through $id [list sinblah16 lsinmips16 lsinhelper main]]
set func [finish_if_ne $id "$func" lsinhelper \
	      [list sinblah16 lsinmips16 lsinhelper main]]
pass_if_eq "$func" lsinmips16 \
    "stepping from \"sinblah16\" back to \"lsinmips16\" (indirectly) ($id)"

incr id
set func [step_through $id [list lsinmips16 lsinhelper main]]
pass_if_eq "$func" lsinhelper \
    "stepping from \"lsinmips16\" back to \"lsinhelper\" ($id)"

# 40
incr id
set func [step_through $id [list lsinhelper main]]
pass_if_eq "$func" main "stepping from \"lsinhelper\" back to \"main\" ($id)"