1f4d42732SJim Ingham"""
2f4d42732SJim InghamMake sure the stop reason of a thread that did not run
3f4d42732SJim Inghamduring an expression is not changed by running the expression
4f4d42732SJim Ingham"""
5f4d42732SJim Ingham
6f4d42732SJim Ingham
7f4d42732SJim Inghamimport lldb
8f4d42732SJim Inghamimport lldbsuite.test.lldbutil as lldbutil
9f4d42732SJim Inghamfrom lldbsuite.test.lldbtest import *
10a976a7fcSJim Inghamfrom lldbsuite.test.decorators import *
11f4d42732SJim Ingham
12f4d42732SJim Ingham
13*2238dcc3SJonas Devlieghereclass TestStopReasonAfterExpression(TestBase):
14a976a7fcSJim Ingham    @skipIfWindows
15266c90feSMichał Górny    @expectedFailureAll(oslist=["freebsd"], bugnumber="llvm.org/pr48415")
1699562332SMichał Górny    @expectedFlakeyNetBSD
17f4d42732SJim Ingham    def test_thread_state_after_expr(self):
18f4d42732SJim Ingham        self.build()
19f4d42732SJim Ingham        self.main_source_file = lldb.SBFileSpec("main.cpp")
20f4d42732SJim Ingham        self.do_test()
21f4d42732SJim Ingham
22f4d42732SJim Ingham    def do_test(self):
23*2238dcc3SJonas Devlieghere        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
24*2238dcc3SJonas Devlieghere            self, "Set a breakpoint here", self.main_source_file
25*2238dcc3SJonas Devlieghere        )
26f4d42732SJim Ingham
27f4d42732SJim Ingham        self.assertEqual(bkpt.GetNumLocations(), 2, "Got two locations")
28f4d42732SJim Ingham
29f4d42732SJim Ingham        # So now thread holds the main thread.  Continue to hit the
30f4d42732SJim Ingham        # breakpoint again on the spawned thread:
31f4d42732SJim Ingham
32f4d42732SJim Ingham        threads = lldbutil.continue_to_breakpoint(process, bkpt)
33f4d42732SJim Ingham        self.assertEqual(len(threads), 1, "Hit the breakpoint the second time")
34f4d42732SJim Ingham        other_thread = threads[0]
35f4d42732SJim Ingham
36*2238dcc3SJonas Devlieghere        self.assertNotEqual(
37*2238dcc3SJonas Devlieghere            thread.GetThreadID(), other_thread.GetThreadID(), "A different thread"
38*2238dcc3SJonas Devlieghere        )
39f4d42732SJim Ingham        # Run an expression ONLY on other_thread.  Don't let thread run:
40f4d42732SJim Ingham        options = lldb.SBExpressionOptions()
41f4d42732SJim Ingham        options.SetTryAllThreads(False)
42f4d42732SJim Ingham        options.SetStopOthers(True)
43f4d42732SJim Ingham
44*2238dcc3SJonas Devlieghere        result = thread.frames[0].EvaluateExpression(
45*2238dcc3SJonas Devlieghere            '(int) printf("Hello\\n")', options
46*2238dcc3SJonas Devlieghere        )
47779bbbf2SDave Lee        self.assertSuccess(result.GetError(), "Expression failed")
48f4d42732SJim Ingham
49f4d42732SJim Ingham        stop_reason = other_thread.GetStopReason()
50f4d42732SJim Ingham
51*2238dcc3SJonas Devlieghere        self.assertStopReason(
52*2238dcc3SJonas Devlieghere            stop_reason,
53*2238dcc3SJonas Devlieghere            lldb.eStopReasonBreakpoint,
54f4d42732SJim Ingham            "Still records stopped at breakpoint: %s"
55*2238dcc3SJonas Devlieghere            % (lldbutil.stop_reason_to_str(stop_reason)),
56*2238dcc3SJonas Devlieghere        )
57*2238dcc3SJonas Devlieghere        self.assertEqual(
58*2238dcc3SJonas Devlieghere            other_thread.GetStopReasonDataAtIndex(0),
59*2238dcc3SJonas Devlieghere            1,
60*2238dcc3SJonas Devlieghere            "Still records stopped at right breakpoint",
61*2238dcc3SJonas Devlieghere        )
62