From 061da546b983eb767bad15e67af1174fb0bcf31c Mon Sep 17 00:00:00 2001 From: patrick Date: Mon, 3 Aug 2020 14:33:06 +0000 Subject: Import LLVM 10.0.0 release including clang, lld and lldb. ok hackroom tested by plenty --- .../lldbsuite/test/python_api/event/Makefile | 3 + .../lldbsuite/test/python_api/event/TestEvents.py | 316 +++++++++++++++++++++ .../Python/lldbsuite/test/python_api/event/main.c | 48 ++++ 3 files changed, 367 insertions(+) create mode 100644 gnu/llvm/lldb/packages/Python/lldbsuite/test/python_api/event/Makefile create mode 100644 gnu/llvm/lldb/packages/Python/lldbsuite/test/python_api/event/TestEvents.py create mode 100644 gnu/llvm/lldb/packages/Python/lldbsuite/test/python_api/event/main.c (limited to 'gnu/llvm/lldb/packages/Python/lldbsuite/test/python_api/event') diff --git a/gnu/llvm/lldb/packages/Python/lldbsuite/test/python_api/event/Makefile b/gnu/llvm/lldb/packages/Python/lldbsuite/test/python_api/event/Makefile new file mode 100644 index 00000000000..10495940055 --- /dev/null +++ b/gnu/llvm/lldb/packages/Python/lldbsuite/test/python_api/event/Makefile @@ -0,0 +1,3 @@ +C_SOURCES := main.c + +include Makefile.rules diff --git a/gnu/llvm/lldb/packages/Python/lldbsuite/test/python_api/event/TestEvents.py b/gnu/llvm/lldb/packages/Python/lldbsuite/test/python_api/event/TestEvents.py new file mode 100644 index 00000000000..97ebe8ffc03 --- /dev/null +++ b/gnu/llvm/lldb/packages/Python/lldbsuite/test/python_api/event/TestEvents.py @@ -0,0 +1,316 @@ +""" +Test lldb Python event APIs. +""" + +from __future__ import print_function + + +import re +import lldb +from lldbsuite.test.decorators import * +from lldbsuite.test.lldbtest import * +from lldbsuite.test import lldbutil + + +@skipIfLinux # llvm.org/pr25924, sometimes generating SIGSEGV +@skipIfDarwin +class EventAPITestCase(TestBase): + + mydir = TestBase.compute_mydir(__file__) + NO_DEBUG_INFO_TESTCASE = True + + def setUp(self): + # Call super's setUp(). + TestBase.setUp(self) + # Find the line number to of function 'c'. + self.line = line_number( + 'main.c', '// Find the line number of function "c" here.') + + @add_test_categories(['pyapi']) + @expectedFailureAll( + oslist=["linux"], + bugnumber="llvm.org/pr23730 Flaky, fails ~1/10 cases") + @skipIfWindows # This is flakey on Windows AND when it fails, it hangs: llvm.org/pr38373 + @skipIfNetBSD + def test_listen_for_and_print_event(self): + """Exercise SBEvent API.""" + self.build() + exe = self.getBuildArtifact("a.out") + + self.dbg.SetAsync(True) + + # Create a target by the debugger. + target = self.dbg.CreateTarget(exe) + self.assertTrue(target, VALID_TARGET) + + # Now create a breakpoint on main.c by name 'c'. + breakpoint = target.BreakpointCreateByName('c', 'a.out') + + listener = lldb.SBListener("my listener") + + # Now launch the process, and do not stop at the entry point. + error = lldb.SBError() + process = target.Launch(listener, + None, # argv + None, # envp + None, # stdin_path + None, # stdout_path + None, # stderr_path + None, # working directory + 0, # launch flags + False, # Stop at entry + error) # error + + self.assertTrue( + process.GetState() == lldb.eStateStopped, + PROCESS_STOPPED) + + # Create an empty event object. + event = lldb.SBEvent() + + traceOn = self.TraceOn() + if traceOn: + lldbutil.print_stacktraces(process) + + # Create MyListeningThread class to wait for any kind of event. + import threading + + class MyListeningThread(threading.Thread): + + def run(self): + count = 0 + # Let's only try at most 4 times to retrieve any kind of event. + # After that, the thread exits. + while not count > 3: + if traceOn: + print("Try wait for event...") + if listener.WaitForEvent(5, event): + if traceOn: + desc = lldbutil.get_description(event) + print("Event description:", desc) + print("Event data flavor:", event.GetDataFlavor()) + print( + "Process state:", + lldbutil.state_type_to_str( + process.GetState())) + print() + else: + if traceOn: + print("timeout occurred waiting for event...") + count = count + 1 + listener.Clear() + return + + # Let's start the listening thread to retrieve the events. + my_thread = MyListeningThread() + my_thread.start() + + # Use Python API to continue the process. The listening thread should be + # able to receive the state changed events. + process.Continue() + + # Use Python API to kill the process. The listening thread should be + # able to receive the state changed event, too. + process.Kill() + + # Wait until the 'MyListeningThread' terminates. + my_thread.join() + + # Shouldn't we be testing against some kind of expectation here? + + @add_test_categories(['pyapi']) + @expectedFlakeyLinux("llvm.org/pr23730") # Flaky, fails ~1/100 cases + @skipIfWindows # This is flakey on Windows AND when it fails, it hangs: llvm.org/pr38373 + @skipIfNetBSD + def test_wait_for_event(self): + """Exercise SBListener.WaitForEvent() API.""" + self.build() + exe = self.getBuildArtifact("a.out") + + self.dbg.SetAsync(True) + + # Create a target by the debugger. + target = self.dbg.CreateTarget(exe) + self.assertTrue(target, VALID_TARGET) + + # Now create a breakpoint on main.c by name 'c'. + breakpoint = target.BreakpointCreateByName('c', 'a.out') + #print("breakpoint:", breakpoint) + self.assertTrue(breakpoint and + breakpoint.GetNumLocations() == 1, + VALID_BREAKPOINT) + + # Get the debugger listener. + listener = self.dbg.GetListener() + + # Now launch the process, and do not stop at entry point. + error = lldb.SBError() + process = target.Launch(listener, + None, # argv + None, # envp + None, # stdin_path + None, # stdout_path + None, # stderr_path + None, # working directory + 0, # launch flags + False, # Stop at entry + error) # error + self.assertTrue(error.Success() and process, PROCESS_IS_VALID) + + # Create an empty event object. + event = lldb.SBEvent() + self.assertFalse(event, "Event should not be valid initially") + + # Create MyListeningThread to wait for any kind of event. + import threading + + class MyListeningThread(threading.Thread): + + def run(self): + count = 0 + # Let's only try at most 3 times to retrieve any kind of event. + while not count > 3: + if listener.WaitForEvent(5, event): + #print("Got a valid event:", event) + #print("Event data flavor:", event.GetDataFlavor()) + #print("Event type:", lldbutil.state_type_to_str(event.GetType())) + listener.Clear() + return + count = count + 1 + print("Timeout: listener.WaitForEvent") + listener.Clear() + return + + # Use Python API to kill the process. The listening thread should be + # able to receive a state changed event. + process.Kill() + + # Let's start the listening thread to retrieve the event. + my_thread = MyListeningThread() + my_thread.start() + + # Wait until the 'MyListeningThread' terminates. + my_thread.join() + + self.assertTrue(event, + "My listening thread successfully received an event") + + @skipIfFreeBSD # llvm.org/pr21325 + @add_test_categories(['pyapi']) + @expectedFailureAll( + oslist=["linux"], + bugnumber="llvm.org/pr23617 Flaky, fails ~1/10 cases") + @skipIfWindows # This is flakey on Windows AND when it fails, it hangs: llvm.org/pr38373 + @expectedFlakeyNetBSD + def test_add_listener_to_broadcaster(self): + """Exercise some SBBroadcaster APIs.""" + self.build() + exe = self.getBuildArtifact("a.out") + + self.dbg.SetAsync(True) + + # Create a target by the debugger. + target = self.dbg.CreateTarget(exe) + self.assertTrue(target, VALID_TARGET) + + # Now create a breakpoint on main.c by name 'c'. + breakpoint = target.BreakpointCreateByName('c', 'a.out') + #print("breakpoint:", breakpoint) + self.assertTrue(breakpoint and + breakpoint.GetNumLocations() == 1, + VALID_BREAKPOINT) + + listener = lldb.SBListener("my listener") + + # Now launch the process, and do not stop at the entry point. + error = lldb.SBError() + process = target.Launch(listener, + None, # argv + None, # envp + None, # stdin_path + None, # stdout_path + None, # stderr_path + None, # working directory + 0, # launch flags + False, # Stop at entry + error) # error + + # Create an empty event object. + event = lldb.SBEvent() + self.assertFalse(event, "Event should not be valid initially") + + # The finite state machine for our custom listening thread, with an + # initial state of None, which means no event has been received. + # It changes to 'connected' after 'connected' event is received (for remote platforms) + # It changes to 'running' after 'running' event is received (should happen only if the + # currentstate is either 'None' or 'connected') + # It changes to 'stopped' if a 'stopped' event is received (should happen only if the + # current state is 'running'.) + self.state = None + + # Create MyListeningThread to wait for state changed events. + # By design, a "running" event is expected following by a "stopped" + # event. + import threading + + class MyListeningThread(threading.Thread): + + def run(self): + #print("Running MyListeningThread:", self) + + # Regular expression pattern for the event description. + pattern = re.compile("data = {.*, state = (.*)}$") + + # Let's only try at most 6 times to retrieve our events. + count = 0 + while True: + if listener.WaitForEvent(5, event): + desc = lldbutil.get_description(event) + #print("Event description:", desc) + match = pattern.search(desc) + if not match: + break + if match.group(1) == 'connected': + # When debugging remote targets with lldb-server, we + # first get the 'connected' event. + self.context.assertTrue(self.context.state is None) + self.context.state = 'connected' + continue + elif match.group(1) == 'running': + self.context.assertTrue( + self.context.state is None or self.context.state == 'connected') + self.context.state = 'running' + continue + elif match.group(1) == 'stopped': + self.context.assertTrue( + self.context.state == 'running') + # Whoopee, both events have been received! + self.context.state = 'stopped' + break + else: + break + print("Timeout: listener.WaitForEvent") + count = count + 1 + if count > 6: + break + listener.Clear() + return + + # Use Python API to continue the process. The listening thread should be + # able to receive the state changed events. + process.Continue() + + # Start the listening thread to receive the "running" followed by the + # "stopped" events. + my_thread = MyListeningThread() + # Supply the enclosing context so that our listening thread can access + # the 'state' variable. + my_thread.context = self + my_thread.start() + + # Wait until the 'MyListeningThread' terminates. + my_thread.join() + + # The final judgement. :-) + self.assertTrue(self.state == 'stopped', + "Both expected state changed events received") diff --git a/gnu/llvm/lldb/packages/Python/lldbsuite/test/python_api/event/main.c b/gnu/llvm/lldb/packages/Python/lldbsuite/test/python_api/event/main.c new file mode 100644 index 00000000000..57369520dd1 --- /dev/null +++ b/gnu/llvm/lldb/packages/Python/lldbsuite/test/python_api/event/main.c @@ -0,0 +1,48 @@ +//===-- main.c --------------------------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +#include + +// This simple program is to test the lldb Python API related to events. + +int a(int); +int b(int); +int c(int); + +int a(int val) +{ + if (val <= 1) + return b(val); + else if (val >= 3) + return c(val); + + return val; +} + +int b(int val) +{ + return c(val); +} + +int c(int val) +{ + return val + 3; // Find the line number of function "c" here. +} + +int main (int argc, char const *argv[]) +{ + int A1 = a(1); // a(1) -> b(1) -> c(1) + printf("a(1) returns %d\n", A1); + + int B2 = b(2); // b(2) -> c(2) + printf("b(2) returns %d\n", B2); + + int A3 = a(3); // a(3) -> c(3) + printf("a(3) returns %d\n", A3); + + return 0; +} -- cgit v1.2.3-59-g8ed1b