Add a quick test case for some of the queues debugging support.

It should only run on Darwin systems, and only when a couple of
libraries are available.

llvm-svn: 203754
This commit is contained in:
Jason Molenda 2014-03-13 05:37:51 +00:00
parent 60b1a6d9e4
commit 7f8b9111a5
3 changed files with 253 additions and 0 deletions

View File

@ -0,0 +1,28 @@
CC ?= clang
ifeq "$(ARCH)" ""
ARCH = x86_64
endif
ifeq "$(OS)" ""
OS = $(shell uname -s)
endif
CFLAGS ?= -g -O0
CWD := $(shell pwd)
LIB_PREFIX := lib
ifeq "$(OS)" "Darwin"
CFLAGS += -arch $(ARCH)
endif
all: a.out
a.out: main.o
$(CC) $(CFLAGS) -o a.out main.o
main.o: main.c
$(CC) $(CFLAGS) -c main.c
clean:
rm -rf *.o *~ *.dylib *.so a.out *.dSYM

View File

@ -0,0 +1,127 @@
"""Test queues inspection SB APIs."""
import os, time
import unittest2
import lldb
import lldbutil
from lldbtest import *
class TestQueues(TestBase):
mydir = TestBase.compute_mydir(__file__)
@unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
@python_api_test
@dsym_test
def test_with_dsym_and_python_api(self):
"""Test queues inspection SB APIs."""
self.buildDsym()
self.queues()
@unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
@python_api_test
@dwarf_test
def test_with_dwarf_and_python_api(self):
"""Test queues inspection SB APIs."""
self.buildDwarf()
self.queues()
def setUp(self):
# Call super's setUp().
TestBase.setUp(self)
# Find the line numbers that we will step to in main:
self.main_source = "main.c"
def queues(self):
"""Test queues inspection SB APIs."""
exe = os.path.join(os.getcwd(), "a.out")
if not os.path.isfile('/Applications/Xcode.app/Contents/Developer/usr/lib/libBacktraceRecording.dylib'):
self.skipTest ("Skipped because libBacktraceRecording.dylib was present on the system.")
self.buildDefault()
if not os.path.isfile('/usr/lib/system/introspection/libdispatch.dylib'):
self.skipTest ("Skipped because introspection libdispatch dylib is not present.")
self.buildDefault()
target = self.dbg.CreateTarget(exe)
self.assertTrue(target, VALID_TARGET)
self.main_source_spec = lldb.SBFileSpec (self.main_source)
break1 = target.BreakpointCreateByName ("stopper", 'a.out')
self.assertTrue(break1, VALID_BREAKPOINT)
# Now launch the process, and do not stop at entry point.
process = target.LaunchSimple (None, ['DYLD_INSERT_LIBRARIES=/Applications/Xcode.app/Contents/Developer/usr/lib/libBacktraceRecording.dylib', 'DYLD_LIBRARY_PATH=/usr/lib/system/introspection'], self.get_process_working_directory())
self.assertTrue(process, PROCESS_IS_VALID)
# The stop reason of the thread should be breakpoint.
threads = lldbutil.get_threads_stopped_at_breakpoint (process, break1)
if len(threads) != 1:
self.fail ("Failed to stop at breakpoint 1.")
libbtr_module_filespec = lldb.SBFileSpec()
libbtr_module_filespec.SetFilename ("libBacktraceRecording.dylib")
libbtr_module = target.FindModule (libbtr_module_filespec)
if not libbtr_module.IsValid():
self.skipTest ("Skipped because libBacktraceRecording.dylib was not loaded into the process.")
self.buildDefault()
self.assertTrue(process.GetNumQueues() == 4, "Found the correct number of queues.")
q0 = process.GetQueueAtIndex(0)
q1 = process.GetQueueAtIndex(1)
q2 = process.GetQueueAtIndex(2)
q3 = process.GetQueueAtIndex(3)
self.assertTrue(q0.IsValid(), "queue 0 is valid")
self.assertTrue(q1.IsValid(), "queue 1 is valid")
self.assertTrue(q2.IsValid(), "queue 2 is valid")
self.assertTrue(q3.IsValid(), "queue 3 is valid")
self.assertTrue(q0.GetName() == "com.apple.work_submittor_1", "Get name of first queue")
self.assertTrue(q1.GetName() == "com.apple.work_performer_1", "Get name of second queue")
self.assertTrue(q2.GetName() == "com.apple.work_performer_2", "Get name of third queue")
self.assertTrue(q3.GetName() == "com.apple.work_performer_3", "Get name of fourth queue")
self.assertTrue(q0.GetQueueID() != 0, "Check queue 0 for valid QueueID")
self.assertTrue(q1.GetQueueID() != 0, "Check queue 1 for valid QueueID")
self.assertTrue(q2.GetQueueID() != 0, "Check queue 2 for valid QueueID")
self.assertTrue(q3.GetQueueID() != 0, "Check queue 3 for valid QueueID")
self.assertTrue(q0.GetNumPendingItems() == 0, "queue 0 should have no pending items")
self.assertTrue(q0.GetNumRunningItems() == 1, "queue 0 should have one running item")
self.assertTrue(q1.GetNumPendingItems() == 3, "queue 1 should have 3 pending items")
self.assertTrue(q1.GetNumRunningItems() == 1, "queue 1 should have 1 running item")
self.assertTrue(q2.GetNumPendingItems() == 9999, "queue 2 should have 9999 pending items")
self.assertTrue(q2.GetNumRunningItems() == 1, "queue 2 should have 1 running item")
self.assertTrue(q3.GetNumPendingItems() == 0, "queue 3 should have 0 pending items")
self.assertTrue(q3.GetNumRunningItems() == 4, "queue 3 should have 4 running item")
self.assertTrue(q3.GetNumThreads() == 4, "queue 3 should have 4 threads executing")
self.assertTrue(q1.GetThreadAtIndex(0).GetQueueID() == q1.GetQueueID(), "queue 1's thread should be owned by the same QueueID")
self.assertTrue(q1.GetThreadAtIndex(0).GetQueueName() == q1.GetName(), "queue 1's thread should have the same queue name")
self.assertTrue(q3.GetThreadAtIndex(0).GetQueueID() == q3.GetQueueID(), "queue 3's threads should be owned by the same QueueID")
self.assertTrue(q3.GetThreadAtIndex(0).GetQueueName() == q3.GetName(), "queue 3's threads should have thes ame queue name")
self.assertTrue(q2.GetPendingItemAtIndex(0).IsValid(), "queue 2's pending item #0 is valid")
self.assertTrue(q2.GetPendingItemAtIndex(0).GetAddress().GetSymbol().GetName() == "doing_the_work_2", "queue 2's pending item #0 should be doing_the_work_2")
self.assertTrue(q2.GetNumPendingItems() == 9999, "verify that queue 2 still has 9999 pending items")
self.assertTrue(q2.GetPendingItemAtIndex(9998).IsValid(), "queue 2's pending item #9998 is valid")
self.assertTrue(q2.GetPendingItemAtIndex(9998).GetAddress().GetSymbol().GetName() == "doing_the_work_2", "queue 2's pending item #0 should be doing_the_work_2")
self.assertTrue(q2.GetPendingItemAtIndex(9999).IsValid() == False, "queue 2's pending item #9999 is invalid")
if __name__ == '__main__':
import atexit
lldb.SBDebugger.Initialize()
atexit.register(lambda: lldb.SBDebugger.Terminate())
unittest2.main()

View File

@ -0,0 +1,98 @@
#include <stdio.h>
#include <unistd.h>
#include <dispatch/dispatch.h>
#include <pthread.h>
void
doing_the_work_1(void *in)
{
while (1)
sleep (1);
}
void
submit_work_1a(void *in)
{
dispatch_queue_t *work_performer_1 = (dispatch_queue_t*) in;
dispatch_async_f (*work_performer_1, NULL, doing_the_work_1);
dispatch_async_f (*work_performer_1, NULL, doing_the_work_1);
}
void
submit_work_1b(void *in)
{
dispatch_queue_t *work_performer_1 = (dispatch_queue_t*) in;
dispatch_async_f (*work_performer_1, NULL, doing_the_work_1);
dispatch_async_f (*work_performer_1, NULL, doing_the_work_1);
while (1)
sleep (1);
}
void
doing_the_work_2(void *in)
{
while (1)
sleep (1);
}
void
submit_work_2(void *in)
{
dispatch_queue_t *work_performer_2 = (dispatch_queue_t*) in;
int i = 0;
while (i++ < 5000)
{
dispatch_async_f (*work_performer_2, NULL, doing_the_work_2);
dispatch_async_f (*work_performer_2, NULL, doing_the_work_2);
}
}
void
doing_the_work_3(void *in)
{
while (1)
sleep(1);
}
void
submit_work_3(void *in)
{
dispatch_queue_t *work_performer_3 = (dispatch_queue_t*) in;
dispatch_async_f (*work_performer_3, NULL, doing_the_work_3);
dispatch_async_f (*work_performer_3, NULL, doing_the_work_3);
dispatch_async_f (*work_performer_3, NULL, doing_the_work_3);
dispatch_async_f (*work_performer_3, NULL, doing_the_work_3);
}
void
stopper ()
{
while (1)
sleep (1);
}
int main ()
{
dispatch_queue_t work_submittor_1 = dispatch_queue_create ("com.apple.work_submittor_1", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t work_submittor_2 = dispatch_queue_create ("com.apple.work_submittor_and_quit_2", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t work_submittor_3 = dispatch_queue_create ("com.apple.work_submittor_3", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t work_performer_1 = dispatch_queue_create ("com.apple.work_performer_1", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t work_performer_2 = dispatch_queue_create ("com.apple.work_performer_2", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t work_performer_3 = dispatch_queue_create ("com.apple.work_performer_3", DISPATCH_QUEUE_CONCURRENT);
dispatch_async_f (work_submittor_1, (void*) &work_performer_1, submit_work_1a);
dispatch_async_f (work_submittor_1, (void*) &work_performer_1, submit_work_1b);
dispatch_async_f (work_submittor_2, (void*) &work_performer_2, submit_work_2);
dispatch_async_f (work_submittor_3, (void*) &work_performer_3, submit_work_3);
sleep (1);
stopper ();
}