<commit_before>"""
A stress-test of sorts for LLDB's handling of threads in the inferior.

This test sets a breakpoint in the main thread where test parameters (numbers of
threads) can be adjusted, runs the inferior to that point, and modifies the
locals that control the event thread counts. This test also sets a breakpoint in
breakpoint_func (the function executed by each 'breakpoint' thread) and a
watchpoint on a global modified in watchpoint_func. The inferior is continued
until exit or a crash takes place, and the number of events seen by LLDB is
verified to match the expected number of events.
"""

import os, signal, time
import unittest2
import lldb
from lldbtest import *
import lldbutil

# ==================================================
# Dictionary of signal names
# ==================================================
signal_names = dict((getattr(signal, n), n) \
        for n in dir(signal) if n.startswith('SIG') and '_' not in n )


class ConcurrentEventsTestCase(TestBase):

    mydir = TestBase.compute_mydir(__file__)

    #
    ## Tests for multiple threads that generate a single event.
    #
    @unittest2.skipIf(TestBase.skipLongRunningTest(), "Skip this long running test")
    @dwarf_test
    def test_many_breakpoints_dwarf(self):
        """Test 100 breakpoints from 100 threads."""
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_breakpoint_threads=100)

    @skipIfLinux # llvm.org/pr16714 - LLDB sometimes crashes when setting watchpoints in multithreaded programs
    @unittest2.skipIf(TestBase.skipLongRunningTest(), "Skip this long running test")
    @dwarf_test
    def test_many_watchpoints_dwarf(self):
        """Test 100 watchpoints from 100 threads."""
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_watchpoint_threads=100)

    @unittest2.skipIf(TestBase.skipLongRunningTest(), "Skip this long running test")
    @dwarf_test
    def test_many_signals_dwarf(self):
        """Test 100 signals from 100 threads."""
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_signal_threads=100)

    @unittest2.skipIf(TestBase.skipLongRunningTest(), "Skip this long running test")
    @dwarf_test
    def test_many_crash_dwarf(self):
        """Test 100 threads that cause a segfault."""
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_crash_threads=100)


    #
    ## Tests for concurrent signal and breakpoint
    #
    @expectedFailureFreeBSD("llvm.org/pr17910") # fails to build
    @dwarf_test
    def test_signal_break_dwarf(self):
        """Test signal and a breakpoint in multiple threads."""
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_breakpoint_threads=1, num_signal_threads=1)

    @expectedFailureFreeBSD("llvm.org/pr17910") # fails to build
    @dwarf_test
    def test_delay_signal_break_dwarf(self):
        """Test (1-second delay) signal and a breakpoint in multiple threads."""
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_breakpoint_threads=1, num_delay_signal_threads=1)

    @expectedFailureFreeBSD("llvm.org/pr17910") # fails to build
    @dwarf_test
    def test_signal_delay_break_dwarf(self):
        """Test signal and a (1 second delay) breakpoint in multiple threads."""
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_delay_breakpoint_threads=1, num_signal_threads=1)


    #
    ## Tests for concurrent watchpoint and breakpoint
    #
    @dwarf_test
    @expectedFailureFreeBSD('llvm.org/pr16706') # Watchpoints fail on FreeBSD
    @skipIfRemoteDueToDeadlock
    @skipIfLinux # llvm.org/pr16714 - LLDB sometimes crashes when setting watchpoints in multithreaded programs
    def test_watch_break_dwarf(self):
        """Test watchpoint and a breakpoint in multiple threads."""
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_breakpoint_threads=1, num_watchpoint_threads=1)

    @expectedFailureFreeBSD('llvm.org/pr16706') # Watchpoints fail on FreeBSD
    @dwarf_test
    @skipIfRemoteDueToDeadlock
    @skipIfLinux # llvm.org/pr16714 - LLDB sometimes crashes when setting watchpoints in multithreaded programs
    def test_delay_watch_break_dwarf(self):
        """Test (1-second delay) watchpoint and a breakpoint in multiple threads."""
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_breakpoint_threads=1, num_delay_watchpoint_threads=1)

    @expectedFailureFreeBSD('llvm.org/pr16706') # Watchpoints fail on FreeBSD
    @dwarf_test
    @skipIfRemoteDueToDeadlock
    @skipIfLinux # llvm.org/pr16714 - LLDB sometimes crashes when setting watchpoints in multithreaded programs
    def test_watch_break_dwarf_delay(self):
        """Test watchpoint and a (1 second delay) breakpoint in multiple threads."""
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_delay_breakpoint_threads=1, num_watchpoint_threads=1)

    #
    ## Tests for concurrent signal and watchpoint
    #
    @expectedFailureFreeBSD('llvm.org/pr16706') # Watchpoints fail on FreeBSD
    @dwarf_test
    @skipIfRemoteDueToDeadlock
    @skipIfLinux # llvm.org/pr16714 - LLDB sometimes crashes when setting watchpoints in multithreaded programs
    def test_signal_watch_dwarf(self):
        """Test a watchpoint and a signal in multiple threads."""
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_signal_threads=1, num_watchpoint_threads=1)

    @expectedFailureFreeBSD('llvm.org/pr16706') # Watchpoints fail on FreeBSD
    @dwarf_test
    @skipIfRemoteDueToDeadlock
    @skipIfLinux # llvm.org/pr16714 - LLDB sometimes crashes when setting watchpoints in multithreaded programs
    def test_delay_signal_watch_dwarf(self):
        """Test a watchpoint and a (1 second delay) signal in multiple threads."""
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_delay_signal_threads=1, num_watchpoint_threads=1)

    @expectedFailureFreeBSD('llvm.org/pr16706') # Watchpoints fail on FreeBSD
    @dwarf_test
    @skipIfRemoteDueToDeadlock
    @skipIfLinux # llvm.org/pr16714 - LLDB sometimes crashes when setting watchpoints in multithreaded programs
    def test_signal_delay_watch_dwarf(self):
        """Test a (1 second delay) watchpoint and a signal in multiple threads."""
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_signal_threads=1, num_delay_watchpoint_threads=1)


    #
    ## Tests for multiple breakpoint threads
    #
    @expectedFailureFreeBSD("llvm.org/pr17910") # fails to build
    @dwarf_test
    def test_two_breakpoint_threads_dwarf(self):
        """Test two threads that trigger a breakpoint. """
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_breakpoint_threads=2)

    @expectedFailureFreeBSD("llvm.org/pr17910") # fails to build
    @dwarf_test
    def test_breakpoint_one_delay_breakpoint_threads_dwarf(self):
        """Test threads that trigger a breakpoint where one thread has a 1 second delay. """
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_breakpoint_threads=1,
                               num_delay_breakpoint_threads=1)

    @expectedFailureFreeBSD("llvm.org/pr17910") # fails to build
    @dwarf_test
    def test_two_breakpoints_one_signal_dwarf(self):
        """Test two threads that trigger a breakpoint and one signal thread. """
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_breakpoint_threads=2, num_signal_threads=1)

    @expectedFailureFreeBSD("llvm.org/pr17910") # fails to build
    @dwarf_test
    def test_breakpoint_delay_breakpoint_one_signal_dwarf(self):
        """Test two threads that trigger a breakpoint (one with a 1 second delay) and one signal thread. """
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_breakpoint_threads=1,
                               num_delay_breakpoint_threads=1,
                               num_signal_threads=1)

    @expectedFailureFreeBSD("llvm.org/pr17910") # fails to build
    @dwarf_test
    def test_two_breakpoints_one_delay_signal_dwarf(self):
        """Test two threads that trigger a breakpoint and one (1 second delay) signal thread. """
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_breakpoint_threads=2, num_delay_signal_threads=1)

    @expectedFailureFreeBSD('llvm.org/pr16706') # Watchpoints fail on FreeBSD
    @dwarf_test
    @skipIfRemoteDueToDeadlock
    @skipIfLinux # llvm.org/pr16714 - LLDB sometimes crashes when setting watchpoints in multithreaded programs
    def test_two_breakpoints_one_watchpoint_dwarf(self):
        """Test two threads that trigger a breakpoint and one watchpoint thread. """
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_breakpoint_threads=2, num_watchpoint_threads=1)

    @expectedFailureFreeBSD('llvm.org/pr16706') # Watchpoints fail on FreeBSD
    @dwarf_test
    @skipIfRemoteDueToDeadlock<commit_msg>Remove failure decorators for FreeBSD bug<commit_after>"""
A stress-test of sorts for LLDB's handling of threads in the inferior.

This test sets a breakpoint in the main thread where test parameters (numbers of
threads) can be adjusted, runs the inferior to that point, and modifies the
locals that control the event thread counts. This test also sets a breakpoint in
breakpoint_func (the function executed by each 'breakpoint' thread) and a
watchpoint on a global modified in watchpoint_func. The inferior is continued
until exit or a crash takes place, and the number of events seen by LLDB is
verified to match the expected number of events.
"""

import os, signal, time
import unittest2
import lldb
from lldbtest import *
import lldbutil

# ==================================================
# Dictionary of signal names
# ==================================================
signal_names = dict((getattr(signal, n), n) \
        for n in dir(signal) if n.startswith('SIG') and '_' not in n )


class ConcurrentEventsTestCase(TestBase):

    mydir = TestBase.compute_mydir(__file__)

    #
    ## Tests for multiple threads that generate a single event.
    #
    @unittest2.skipIf(TestBase.skipLongRunningTest(), "Skip this long running test")
    @dwarf_test
    def test_many_breakpoints_dwarf(self):
        """Test 100 breakpoints from 100 threads."""
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_breakpoint_threads=100)

    @skipIfLinux # llvm.org/pr16714 - LLDB sometimes crashes when setting watchpoints in multithreaded programs
    @unittest2.skipIf(TestBase.skipLongRunningTest(), "Skip this long running test")
    @dwarf_test
    def test_many_watchpoints_dwarf(self):
        """Test 100 watchpoints from 100 threads."""
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_watchpoint_threads=100)

    @unittest2.skipIf(TestBase.skipLongRunningTest(), "Skip this long running test")
    @dwarf_test
    def test_many_signals_dwarf(self):
        """Test 100 signals from 100 threads."""
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_signal_threads=100)

    @unittest2.skipIf(TestBase.skipLongRunningTest(), "Skip this long running test")
    @dwarf_test
    def test_many_crash_dwarf(self):
        """Test 100 threads that cause a segfault."""
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_crash_threads=100)


    #
    ## Tests for concurrent signal and breakpoint
    #
    @dwarf_test
    def test_signal_break_dwarf(self):
        """Test signal and a breakpoint in multiple threads."""
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_breakpoint_threads=1, num_signal_threads=1)

    @dwarf_test
    def test_delay_signal_break_dwarf(self):
        """Test (1-second delay) signal and a breakpoint in multiple threads."""
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_breakpoint_threads=1, num_delay_signal_threads=1)

    @dwarf_test
    def test_signal_delay_break_dwarf(self):
        """Test signal and a (1 second delay) breakpoint in multiple threads."""
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_delay_breakpoint_threads=1, num_signal_threads=1)


    #
    ## Tests for concurrent watchpoint and breakpoint
    #
    @dwarf_test
    @expectedFailureFreeBSD('llvm.org/pr16706') # Watchpoints fail on FreeBSD
    @skipIfRemoteDueToDeadlock
    @skipIfLinux # llvm.org/pr16714 - LLDB sometimes crashes when setting watchpoints in multithreaded programs
    def test_watch_break_dwarf(self):
        """Test watchpoint and a breakpoint in multiple threads."""
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_breakpoint_threads=1, num_watchpoint_threads=1)

    @expectedFailureFreeBSD('llvm.org/pr16706') # Watchpoints fail on FreeBSD
    @dwarf_test
    @skipIfRemoteDueToDeadlock
    @skipIfLinux # llvm.org/pr16714 - LLDB sometimes crashes when setting watchpoints in multithreaded programs
    def test_delay_watch_break_dwarf(self):
        """Test (1-second delay) watchpoint and a breakpoint in multiple threads."""
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_breakpoint_threads=1, num_delay_watchpoint_threads=1)

    @expectedFailureFreeBSD('llvm.org/pr16706') # Watchpoints fail on FreeBSD
    @dwarf_test
    @skipIfRemoteDueToDeadlock
    @skipIfLinux # llvm.org/pr16714 - LLDB sometimes crashes when setting watchpoints in multithreaded programs
    def test_watch_break_dwarf_delay(self):
        """Test watchpoint and a (1 second delay) breakpoint in multiple threads."""
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_delay_breakpoint_threads=1, num_watchpoint_threads=1)

    #
    ## Tests for concurrent signal and watchpoint
    #
    @expectedFailureFreeBSD('llvm.org/pr16706') # Watchpoints fail on FreeBSD
    @dwarf_test
    @skipIfRemoteDueToDeadlock
    @skipIfLinux # llvm.org/pr16714 - LLDB sometimes crashes when setting watchpoints in multithreaded programs
    def test_signal_watch_dwarf(self):
        """Test a watchpoint and a signal in multiple threads."""
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_signal_threads=1, num_watchpoint_threads=1)

    @expectedFailureFreeBSD('llvm.org/pr16706') # Watchpoints fail on FreeBSD
    @dwarf_test
    @skipIfRemoteDueToDeadlock
    @skipIfLinux # llvm.org/pr16714 - LLDB sometimes crashes when setting watchpoints in multithreaded programs
    def test_delay_signal_watch_dwarf(self):
        """Test a watchpoint and a (1 second delay) signal in multiple threads."""
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_delay_signal_threads=1, num_watchpoint_threads=1)

    @expectedFailureFreeBSD('llvm.org/pr16706') # Watchpoints fail on FreeBSD
    @dwarf_test
    @skipIfRemoteDueToDeadlock
    @skipIfLinux # llvm.org/pr16714 - LLDB sometimes crashes when setting watchpoints in multithreaded programs
    def test_signal_delay_watch_dwarf(self):
        """Test a (1 second delay) watchpoint and a signal in multiple threads."""
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_signal_threads=1, num_delay_watchpoint_threads=1)


    #
    ## Tests for multiple breakpoint threads
    #
    @expectedFailureFreeBSD("llvm.org/pr17910") # fails to build
    @dwarf_test
    def test_two_breakpoint_threads_dwarf(self):
        """Test two threads that trigger a breakpoint. """
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_breakpoint_threads=2)

    @expectedFailureFreeBSD("llvm.org/pr17910") # fails to build
    @dwarf_test
    def test_breakpoint_one_delay_breakpoint_threads_dwarf(self):
        """Test threads that trigger a breakpoint where one thread has a 1 second delay. """
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_breakpoint_threads=1,
                               num_delay_breakpoint_threads=1)

    @dwarf_test
    def test_two_breakpoints_one_signal_dwarf(self):
        """Test two threads that trigger a breakpoint and one signal thread. """
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_breakpoint_threads=2, num_signal_threads=1)

    @dwarf_test
    def test_breakpoint_delay_breakpoint_one_signal_dwarf(self):
        """Test two threads that trigger a breakpoint (one with a 1 second delay) and one signal thread. """
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_breakpoint_threads=1,
                               num_delay_breakpoint_threads=1,
                               num_signal_threads=1)

    @expectedFailureFreeBSD("llvm.org/pr17910") # fails to build
    @dwarf_test
    def test_two_breakpoints_one_delay_signal_dwarf(self):
        """Test two threads that trigger a breakpoint and one (1 second delay) signal thread. """
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_breakpoint_threads=2, num_delay_signal_threads=1)

    @expectedFailureFreeBSD('llvm.org/pr16706') # Watchpoints fail on FreeBSD
    @dwarf_test
    @skipIfRemoteDueToDeadlock
    @skipIfLinux # llvm.org/pr16714 - LLDB sometimes crashes when setting watchpoints in multithreaded programs
    def test_two_breakpoints_one_watchpoint_dwarf(self):
        """Test two threads that trigger a breakpoint and one watchpoint thread. """
        self.buildDwarf(dictionary=self.getBuildFlags())
        self.do_thread_actions(num_breakpoint_threads=2, num_watchpoint_threads=1)

    @expectedFailureFreeBSD('llvm.org/pr16706') # Watchpoints fail on FreeBSD
    @dwarf_test
    @skipIfRemoteDueToDeadlock
{'fraction_non_alphanumeric': 0.09136890194158917, 'fraction_numerical': 0.020068526676456193, 'mean_word_length': 4.109196999166435, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6890343', 'n_tokens_mistral': 5689, 'n_tokens_neox': 5313, 'n_words': 1302}