1# Tests for the MutexPrinter class.
2#
3# Copyright (C) 2016-2022 Free Software Foundation, Inc.
4# This file is part of the GNU C Library.
5#
6# The GNU C Library is free software; you can redistribute it and/or
7# modify it under the terms of the GNU Lesser General Public
8# License as published by the Free Software Foundation; either
9# version 2.1 of the License, or (at your option) any later version.
10#
11# The GNU C Library is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14# Lesser General Public License for more details.
15#
16# You should have received a copy of the GNU Lesser General Public
17# License along with the GNU C Library; if not, see
18# <https://www.gnu.org/licenses/>.
19
20import sys
21
22from test_printers_common import *
23
24test_source = sys.argv[1]
25test_bin = sys.argv[2]
26printer_files = sys.argv[3:]
27printer_names = ['global glibc-pthread-locks']
28
29try:
30    init_test(test_bin, printer_files, printer_names)
31    go_to_main()
32
33    var = 'mutex'
34    to_string = 'pthread_mutex_t'
35
36    break_at(test_source, 'Test status (destroyed)')
37    continue_cmd() # Go to test_status_destroyed
38    test_printer(var, to_string, {'Status': 'Destroyed'})
39
40    break_at(test_source, 'Test status (non-robust)')
41    continue_cmd() # Go to test_status_no_robust
42    test_printer(var, to_string, {'Status': 'Not acquired'})
43    next_cmd()
44    thread_id = get_current_thread_lwpid()
45    # Owner ID might be reported either as the thread ID or as "Unknown"
46    # (if e.g. lock elision is enabled).
47    test_printer(var, to_string,
48                 {'Status': 'Acquired, possibly with no waiters',
49                  'Owner ID': r'({0}|Unknown)'.format(thread_id)})
50
51    break_at(test_source, 'Test status (robust)')
52    continue_cmd() # Go to test_status_robust
53    test_printer(var, to_string, {'Status': 'Not acquired'})
54
55    # We'll now test the robust mutex locking states.  We'll create a new
56    # thread that will lock a robust mutex and exit without unlocking it.
57    break_at(test_source, 'Create')
58    continue_cmd() # Go to test_locking_state_robust
59    # Set a breakpoint for the new thread to hit.
60    break_at(test_source, 'Thread function')
61    continue_cmd()
62    # By now the new thread is created and has hit its breakpoint.
63    set_scheduler_locking(True)
64    parent = 1
65    child = 2
66    select_thread(child)
67    child_id = get_current_thread_lwpid()
68    # We've got the new thread's ID.
69    select_thread(parent)
70    # Make the new thread finish its function while we wait.
71    continue_cmd(thread=child)
72    # The new thread should be dead by now.
73    break_at(test_source, 'Test locking (robust)')
74    continue_cmd()
75    test_printer(var, to_string, {'Owner ID': r'{0} \(dead\)'.format(child_id)})
76    # Try to lock and unlock the mutex.
77    next_cmd()
78    test_printer(var, to_string, {'Owner ID': thread_id,
79                           'State protected by this mutex': 'Inconsistent'})
80    next_cmd()
81    test_printer(var, to_string, {'Status': 'Not acquired',
82                        'State protected by this mutex': 'Not recoverable'})
83    set_scheduler_locking(False)
84
85    break_at(test_source, 'Test recursive locks')
86    continue_cmd() # Go to test_recursive_locks
87    test_printer(var, to_string, {'Times acquired by the owner': '2'})
88    next_cmd()
89    test_printer(var, to_string, {'Times acquired by the owner': '3'})
90    continue_cmd() # Exit
91
92except (NoLineError, pexpect.TIMEOUT) as exception:
93    print('Error: {0}'.format(exception))
94    result = FAIL
95
96else:
97    print('Test succeeded.')
98    result = PASS
99
100exit(result)
101