Make TestPrintStackTraces deterministic

This test contained an incredibly complicated inferior, but in reality,
all it was testing was that we can backtrace up to main and see main's
arguments.

However, the way this was implemented (setting a breakpoint on a
separate thread) meant that each time the test would run, it would stop
in a different location on the main thread. Most of the time this
location would be deep in some libc function, which meant that the
success of this test depended on our ability to backtrace out of a
random function of the c library that the user happens to have
installed.

This makes the test unpredictable. Backtracing out of a libc function is
an important functionality, but this is not the way to test it. Often it
is not even our fault that we cannot backtrace out because the C library
contains a lot of assembly routines that may not have correct unwind
info associated with them.

For this reason the test has accumulated numerous @expectedFail/Flaky
decorators. In this patch, I replace the inferior with one that does not
depend on libc functions. Instead I create a couple of stack frames of
user code, and have the test verify that. I also simplify the test by
using lldbutil.run_to_source_breakpoint.

llvm-svn: 358266
This commit is contained in:
Pavel Labath 2019-04-12 08:02:28 +00:00
parent 4b0931bc17
commit 539b7e65b4
2 changed files with 8 additions and 158 deletions

View File

@ -18,47 +18,12 @@ class ThreadsStackTracesTestCase(TestBase):
mydir = TestBase.compute_mydir(__file__)
def setUp(self):
# Call super's setUp().
TestBase.setUp(self)
# Find the line number to break inside main().
self.line = line_number('main.cpp', '// Set break point at this line.')
# We are unable to produce a backtrace of the main thread when the thread
# is blocked in fgets
@expectedFailureAll("llvm.org/pr23043", ["linux"], archs=["i386"])
# The __thread_start function in libc doesn't contain any epilogue and prologue instructions
# hence unwinding fail when we are stopped in __thread_start
@expectedFailureAll(triple='mips*')
@expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24778")
@expectedFlakeyAndroid("llvm.org/26492", archs=["arm"])
@expectedFlakeyLinux("llvm.org/pr27687")
@expectedFailureNetBSD
@add_test_categories(['pyapi'])
def test_stack_traces(self):
"""Test SBprocess and SBThread APIs with printing of the stack traces."""
self.build()
exe = self.getBuildArtifact("a.out")
target = self.dbg.CreateTarget(exe)
self.assertTrue(target, VALID_TARGET)
breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line)
self.assertTrue(breakpoint, VALID_BREAKPOINT)
# Now launch the process, and do not stop at entry point.
process = target.LaunchSimple(
["abc", "xyz"], None, self.get_process_working_directory())
if not process:
self.fail("SBTarget.LaunchProcess() failed")
import lldbsuite.test.lldbutil as lldbutil
if process.GetState() != lldb.eStateStopped:
self.fail("Process should be in the 'stopped' state, "
"instead the actual state is: '%s'" %
lldbutil.state_type_to_str(process.GetState()))
(_, process, _, _) = lldbutil.run_to_source_breakpoint(self,
"// BREAK HERE", lldb.SBFileSpec("main.cpp"))
stacktraces = lldbutil.print_stacktraces(process, string_buffer=True)
self.expect(stacktraces, exe=False,
substrs=['(int)argc=3'])
substrs=['(int)x=4', '(int)y=6', '(int)x=3', '(int)argc=1'])

View File

@ -6,130 +6,15 @@
//
//===----------------------------------------------------------------------===//
// C includes
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
// C++ includes
#include <chrono>
#include <mutex>
#include <random>
#include <thread>
std::thread g_thread_1;
std::thread g_thread_2;
std::thread g_thread_3;
std::mutex g_mask_mutex;
typedef enum {
eGet,
eAssign,
eClearBits
} MaskAction;
uint32_t mask_access (MaskAction action, uint32_t mask = 0);
uint32_t
mask_access (MaskAction action, uint32_t mask)
{
static uint32_t g_mask = 0;
std::lock_guard<std::mutex> lock(g_mask_mutex);
switch (action)
{
case eGet:
break;
case eAssign:
g_mask |= mask;
break;
case eClearBits:
g_mask &= ~mask;
break;
}
return g_mask;
static int foo(int x, int y) {
return x + y; // BREAK HERE
}
void *
thread_func (void *arg)
{
uint32_t thread_index = *((uint32_t *)arg);
uint32_t thread_mask = (1u << (thread_index));
printf ("%s (thread index = %u) startng...\n", __FUNCTION__, thread_index);
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(0, 3000000);
while (mask_access(eGet) & thread_mask)
{
// random micro second sleep from zero to 3 seconds
int usec = distribution(generator);
printf ("%s (thread = %u) doing a usleep (%d)...\n", __FUNCTION__, thread_index, usec);
std::chrono::microseconds duration(usec);
std::this_thread::sleep_for(duration);
printf ("%s (thread = %u) after usleep ...\n", __FUNCTION__, thread_index); // Set break point at this line.
}
printf ("%s (thread index = %u) exiting...\n", __FUNCTION__, thread_index);
return NULL;
static int bar(int x) {
return foo(x + 1, x * 2);
}
int main (int argc, char const *argv[])
{
int err;
void *thread_result = NULL;
uint32_t thread_index_1 = 1;
uint32_t thread_index_2 = 2;
uint32_t thread_index_3 = 3;
uint32_t thread_mask_1 = (1u << thread_index_1);
uint32_t thread_mask_2 = (1u << thread_index_2);
uint32_t thread_mask_3 = (1u << thread_index_3);
// Make a mask that will keep all threads alive
mask_access (eAssign, thread_mask_1 | thread_mask_2 | thread_mask_3); // And that line.
// Create 3 threads
g_thread_1 = std::thread(thread_func, (void*)&thread_index_1);
g_thread_2 = std::thread(thread_func, (void*)&thread_index_2);
g_thread_3 = std::thread(thread_func, (void*)&thread_index_3);
char line[64];
while (mask_access(eGet) != 0)
{
printf ("Enter thread index to kill or ENTER for all:\n");
fflush (stdout);
// Kill threads by index, or ENTER for all threads
if (fgets (line, sizeof(line), stdin))
{
if (line[0] == '\n' || line[0] == '\r' || line[0] == '\0')
{
printf ("Exiting all threads...\n");
break;
}
int32_t index = strtoul (line, NULL, 0);
switch (index)
{
case 1: mask_access (eClearBits, thread_mask_1); break;
case 2: mask_access (eClearBits, thread_mask_2); break;
case 3: mask_access (eClearBits, thread_mask_3); break;
}
continue;
}
break;
}
// Clear all thread bits to they all exit
mask_access (eClearBits, UINT32_MAX);
// Join all of our threads
g_thread_1.join();
g_thread_2.join();
g_thread_3.join();
return 0;
return bar(argc + 2);
}