[Lldb-commits] [lldb] r186446 - Add tests for 'thread step out' for multithreaded programs

Daniel Malea daniel.malea at intel.com
Tue Jul 16 12:41:37 PDT 2013


Author: dmalea
Date: Tue Jul 16 14:41:37 2013
New Revision: 186446

URL: http://llvm.org/viewvc/llvm-project?rev=186446&view=rev
Log:
Add tests for 'thread step out' for multithreaded programs
- test with python API
- test with command interpreter
- test stepping a single (selected) thread
- test stepping all threads in the program



Added:
    lldb/trunk/test/functionalities/thread/step_out/
    lldb/trunk/test/functionalities/thread/step_out/Makefile
    lldb/trunk/test/functionalities/thread/step_out/TestThreadStepOut.py
    lldb/trunk/test/functionalities/thread/step_out/main.cpp

Added: lldb/trunk/test/functionalities/thread/step_out/Makefile
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/functionalities/thread/step_out/Makefile?rev=186446&view=auto
==============================================================================
--- lldb/trunk/test/functionalities/thread/step_out/Makefile (added)
+++ lldb/trunk/test/functionalities/thread/step_out/Makefile Tue Jul 16 14:41:37 2013
@@ -0,0 +1,7 @@
+LEVEL = ../../../make
+CXX_SOURCES := main.cpp
+LD_EXTRAS := -lpthread
+
+CFLAGS_EXTRAS += -std=c++11
+
+include $(LEVEL)/Makefile.rules

Added: lldb/trunk/test/functionalities/thread/step_out/TestThreadStepOut.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/functionalities/thread/step_out/TestThreadStepOut.py?rev=186446&view=auto
==============================================================================
--- lldb/trunk/test/functionalities/thread/step_out/TestThreadStepOut.py (added)
+++ lldb/trunk/test/functionalities/thread/step_out/TestThreadStepOut.py Tue Jul 16 14:41:37 2013
@@ -0,0 +1,143 @@
+"""
+Test stepping out from a function in a multi-threaded program.
+"""
+
+import os, time
+import unittest2
+import lldb
+from lldbtest import *
+import lldbutil
+
+class ThreadStepOutTestCase(TestBase):
+
+    mydir = os.path.join("functionalities", "thread", "step_out")
+
+    @dsym_test
+    def test_step_single_thread_with_dsym(self):
+        """Test thread step out on one thread via command interpreter. """
+        self.buildDsym(dictionary=self.getBuildFlags())
+        self.step_out_test(self.step_out_single_thread_with_cmd)
+
+    @dwarf_test
+    def test_step_single_thread_with_dwarf(self):
+        """Test thread step out on one thread via command interpreter. """
+        self.buildDwarf(dictionary=self.getBuildFlags())
+        self.step_out_test(self.step_out_single_thread_with_cmd)
+
+    @dsym_test
+    def test_step_all_threads_with_dsym(self):
+        """Test thread step out on all threads via command interpreter. """
+        self.buildDsym(dictionary=self.getBuildFlags())
+        self.step_out_test(self.step_out_all_threads_with_cmd)
+
+    @dwarf_test
+    def test_step_all_threads_with_dwarf(self):
+        """Test thread step out on all threads via command interpreter. """
+        self.buildDwarf(dictionary=self.getBuildFlags())
+        self.step_out_test(self.step_out_all_threads_with_cmd)
+
+    @dsym_test
+    def test_python_with_dsym(self):
+        """Test thread step out on one threads via Python API (dsym)."""
+        self.buildDsym(dictionary=self.getBuildFlags())
+        self.step_out_test(self.step_out_with_python)
+
+    @dwarf_test
+    def test_python_with_dwarf(self):
+        """Test thread step out on one thread via Python API (dwarf)."""
+        self.buildDwarf(dictionary=self.getBuildFlags())
+        self.step_out_test(self.step_out_with_python)
+
+    def setUp(self):
+        # Call super's setUp().
+        TestBase.setUp(self)
+        # Find the line number for our breakpoint.
+        self.breakpoint = line_number('main.cpp', '// Set breakpoint here')
+        if "gcc" in self.getCompiler():
+            self.step_out_destination = line_number('main.cpp', '// Expect to stop here after step-out (gcc)')
+        else:
+            self.step_out_destination = line_number('main.cpp', '// Expect to stop here after step-out (clang)')
+
+    def step_out_single_thread_with_cmd(self):
+        self.step_out_with_cmd("this-thread")
+        self.expect("thread backtrace all", "Thread location after step out is correct",
+            substrs = ["main.cpp:%d" % self.step_out_destination,
+                       "main.cpp:%d" % self.breakpoint])
+
+    def step_out_all_threads_with_cmd(self):
+        self.step_out_with_cmd("all-threads")
+        self.expect("thread backtrace all", "Thread location after step out is correct",
+            substrs = ["main.cpp:%d" % self.step_out_destination])
+
+    def step_out_with_cmd(self, run_mode):
+        self.runCmd("thread select %d" % self.step_out_thread.GetIndexID())
+        self.runCmd("thread step-out -m %s" % run_mode)
+        self.expect("process status", "Expected stop reason to be step-out",
+            substrs = ["stop reason = step out"])
+
+        self.expect("thread list", "Selected thread did not change during step-out",
+            substrs = ["* thread #%d" % self.step_out_thread.GetIndexID()])
+
+    def step_out_with_python(self):
+        self.step_out_thread.StepOut()
+
+        reason = self.step_out_thread.GetStopReason()
+        self.assertEqual(lldb.eStopReasonPlanComplete, reason,
+            "Expected thread stop reason 'plancomplete', but got '%s'" % lldbutil.stop_reason_to_str(reason))
+
+        # Verify location after stepping out
+        frame = self.step_out_thread.GetFrameAtIndex(0)
+        desc = lldbutil.get_description(frame.GetLineEntry())
+        expect = "main.cpp:%d" % self.step_out_destination
+        self.assertTrue(expect in desc, "Expected %s but thread stopped at %s" % (expect, desc))
+
+    def step_out_test(self, step_out_func):
+        """Test single thread step out of a function."""
+        exe = os.path.join(os.getcwd(), "a.out")
+        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
+
+        # This should create a breakpoint in the main thread.
+        lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.breakpoint, num_expected_locations=1)
+
+        # The breakpoint list should show 1 location.
+        self.expect("breakpoint list -f", "Breakpoint location shown correctly",
+            substrs = ["1: file = 'main.cpp', line = %d, locations = 1" % self.breakpoint])
+
+        # Run the program.
+        self.runCmd("run", RUN_SUCCEEDED)
+
+        # Get the target process
+        self.inferior_target = self.dbg.GetSelectedTarget()
+        self.inferior_process = self.inferior_target.GetProcess()
+
+        # Get the number of threads, ensure we see all three.
+        num_threads = self.inferior_process.GetNumThreads()
+        self.assertEqual(num_threads, 3, 'Number of expected threads and actual threads do not match.')
+
+        (breakpoint_threads, other_threads) = ([], [])
+        lldbutil.sort_stopped_threads(self.inferior_process,
+                                      breakpoint_threads=breakpoint_threads,
+                                      other_threads=other_threads)
+
+        while len(breakpoint_threads) < 2:
+            self.runCmd("thread continue %s" % " ".join([str(x.GetIndexID()) for x in other_threads]))
+            lldbutil.sort_stopped_threads(self.inferior_process,
+                                          breakpoint_threads=breakpoint_threads,
+                                          other_threads=other_threads)
+
+        self.step_out_thread = breakpoint_threads[0]
+
+        # Step out of thread stopped at breakpoint
+        step_out_func()
+
+        # Run to completion
+        self.runCmd("continue")
+
+        # At this point, the inferior process should have exited.
+        self.assertTrue(self.inferior_process.GetState() == lldb.eStateExited, PROCESS_EXITED)
+
+if __name__ == '__main__':
+    import atexit
+    lldb.SBDebugger.Initialize()
+    atexit.register(lambda: lldb.SBDebugger.Terminate())
+    unittest2.main()

Added: lldb/trunk/test/functionalities/thread/step_out/main.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/functionalities/thread/step_out/main.cpp?rev=186446&view=auto
==============================================================================
--- lldb/trunk/test/functionalities/thread/step_out/main.cpp (added)
+++ lldb/trunk/test/functionalities/thread/step_out/main.cpp Tue Jul 16 14:41:37 2013
@@ -0,0 +1,66 @@
+//===-- main.cpp ------------------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// This test is intended to create a situation in which two threads are stopped
+// at a breakpoint and the debugger issues a step-out command.
+
+#include <pthread.h>
+#include <atomic>
+
+// Note that although hogging the CPU while waiting for a variable to change
+// would be terrible in production code, it's great for testing since it
+// avoids a lot of messy context switching to get multiple threads synchronized.
+#define do_nothing()
+
+#define pseudo_barrier_wait(bar) \
+    --bar;                       \
+    while (bar > 0)              \
+        do_nothing();
+
+#define pseudo_barrier_init(bar, count) (bar = count)
+
+std::atomic_int g_barrier;
+
+volatile int g_test = 0;
+
+void step_out_of_here() {
+  g_test += 5; // Set breakpoint here
+}
+
+void *
+thread_func (void *input)
+{
+    // Wait until both threads are running
+    pseudo_barrier_wait(g_barrier);
+
+    // Do something
+    step_out_of_here(); // Expect to stop here after step-out (clang)
+
+    // Return
+    return NULL;  // Expect to stop here after step-out (gcc)
+}
+
+int main ()
+{
+    pthread_t thread_1;
+    pthread_t thread_2;
+
+    // Don't let either thread do anything until they're both ready.
+    pseudo_barrier_init(g_barrier, 2);
+
+    // Create two threads
+    pthread_create (&thread_1, NULL, thread_func, NULL);
+    pthread_create (&thread_2, NULL, thread_func, NULL);
+
+    // Wait for the threads to finish
+    pthread_join(thread_1, NULL);
+    pthread_join(thread_2, NULL);
+
+    return 0;
+}





More information about the lldb-commits mailing list