2014-10-16 23:15:22 +00:00
|
|
|
import lldb
|
|
|
|
from lldbtest import *
|
|
|
|
import lldbutil
|
|
|
|
import os
|
2014-10-28 20:23:20 +00:00
|
|
|
import unittest2
|
|
|
|
import sys
|
2014-10-16 23:15:22 +00:00
|
|
|
|
|
|
|
def source_type(filename):
|
|
|
|
_, extension = os.path.splitext(filename)
|
|
|
|
return {
|
|
|
|
'.c' : 'C_SOURCES',
|
|
|
|
'.cpp' : 'CXX_SOURCES',
|
|
|
|
'.cxx' : 'CXX_SOURCES',
|
|
|
|
'.cc' : 'CXX_SOURCES',
|
|
|
|
'.m' : 'OBJC_SOURCES',
|
|
|
|
'.mm' : 'OBJCXX_SOURCES'
|
|
|
|
}.get(extension, None)
|
|
|
|
|
|
|
|
class CommandParser:
|
|
|
|
def __init__(self):
|
|
|
|
self.breakpoints = []
|
|
|
|
|
|
|
|
def parse_one_command(self, line):
|
|
|
|
parts = line.split('//%')
|
2014-10-17 00:39:37 +00:00
|
|
|
|
|
|
|
command = None
|
|
|
|
new_breakpoint = True
|
|
|
|
|
|
|
|
if len(parts) == 2:
|
|
|
|
command = parts[1].strip() # take off whitespace
|
|
|
|
new_breakpoint = parts[0].strip() != ""
|
|
|
|
|
|
|
|
return (command, new_breakpoint)
|
2014-10-16 23:15:22 +00:00
|
|
|
|
|
|
|
def parse_source_files(self, source_files):
|
|
|
|
for source_file in source_files:
|
|
|
|
file_handle = open(source_file)
|
|
|
|
lines = file_handle.readlines()
|
|
|
|
line_number = 0
|
2014-10-17 00:39:37 +00:00
|
|
|
current_breakpoint = None # non-NULL means we're looking through whitespace to find additional commands
|
2014-10-16 23:15:22 +00:00
|
|
|
for line in lines:
|
|
|
|
line_number = line_number + 1 # 1-based, so we do this first
|
2014-10-17 00:39:37 +00:00
|
|
|
(command, new_breakpoint) = self.parse_one_command(line)
|
|
|
|
|
|
|
|
if new_breakpoint:
|
|
|
|
current_breakpoint = None
|
|
|
|
|
2014-10-16 23:15:22 +00:00
|
|
|
if command != None:
|
2014-10-17 00:39:37 +00:00
|
|
|
if current_breakpoint == None:
|
|
|
|
current_breakpoint = {}
|
|
|
|
current_breakpoint['file_name'] = source_file
|
|
|
|
current_breakpoint['line_number'] = line_number
|
|
|
|
current_breakpoint['command'] = command
|
|
|
|
self.breakpoints.append(current_breakpoint)
|
|
|
|
else:
|
|
|
|
current_breakpoint['command'] = current_breakpoint['command'] + "\n" + command
|
2014-10-16 23:15:22 +00:00
|
|
|
|
|
|
|
def set_breakpoints(self, target):
|
|
|
|
for breakpoint in self.breakpoints:
|
|
|
|
breakpoint['breakpoint'] = target.BreakpointCreateByLocation(breakpoint['file_name'], breakpoint['line_number'])
|
|
|
|
|
|
|
|
def handle_breakpoint(self, test, breakpoint_id):
|
|
|
|
for breakpoint in self.breakpoints:
|
|
|
|
if breakpoint['breakpoint'].GetID() == breakpoint_id:
|
|
|
|
test.execute_user_command(breakpoint['command'])
|
|
|
|
return
|
|
|
|
|
2015-01-07 22:25:50 +00:00
|
|
|
class InlineTest(TestBase):
|
|
|
|
# Internal implementation
|
2014-12-19 18:26:33 +00:00
|
|
|
|
2015-01-07 22:25:50 +00:00
|
|
|
def getRerunArgs(self):
|
|
|
|
# The -N option says to NOT run a if it matches the option argument, so
|
|
|
|
# if we are using dSYM we say to NOT run dwarf (-N dwarf) and vice versa.
|
2015-01-09 01:54:44 +00:00
|
|
|
if self.using_dsym is None:
|
|
|
|
# The test was skipped altogether.
|
|
|
|
return ""
|
|
|
|
elif self.using_dsym:
|
2015-01-07 22:25:50 +00:00
|
|
|
return "-N dwarf %s" % (self.mydir)
|
|
|
|
else:
|
|
|
|
return "-N dsym %s" % (self.mydir)
|
|
|
|
|
|
|
|
def BuildMakefile(self):
|
|
|
|
if os.path.exists("Makefile"):
|
|
|
|
return
|
2014-10-16 23:15:22 +00:00
|
|
|
|
2015-01-07 22:25:50 +00:00
|
|
|
categories = {}
|
2014-10-16 23:15:22 +00:00
|
|
|
|
2015-01-07 22:25:50 +00:00
|
|
|
for f in os.listdir(os.getcwd()):
|
|
|
|
t = source_type(f)
|
|
|
|
if t:
|
|
|
|
if t in categories.keys():
|
|
|
|
categories[t].append(f)
|
|
|
|
else:
|
|
|
|
categories[t] = [f]
|
2014-10-16 23:15:22 +00:00
|
|
|
|
2015-01-07 22:25:50 +00:00
|
|
|
makefile = open("Makefile", 'w+')
|
2014-10-16 23:15:22 +00:00
|
|
|
|
2015-01-07 22:25:50 +00:00
|
|
|
level = os.sep.join([".."] * len(self.mydir.split(os.sep))) + os.sep + "make"
|
2014-10-16 23:15:22 +00:00
|
|
|
|
2015-01-07 22:25:50 +00:00
|
|
|
makefile.write("LEVEL = " + level + "\n")
|
2014-10-16 23:15:22 +00:00
|
|
|
|
2015-01-07 22:25:50 +00:00
|
|
|
for t in categories.keys():
|
|
|
|
line = t + " := " + " ".join(categories[t])
|
|
|
|
makefile.write(line + "\n")
|
2014-10-16 23:15:22 +00:00
|
|
|
|
2015-01-07 22:25:50 +00:00
|
|
|
if ('OBJCXX_SOURCES' in categories.keys()) or ('OBJC_SOURCES' in categories.keys()):
|
|
|
|
makefile.write("LDFLAGS = $(CFLAGS) -lobjc -framework Foundation\n")
|
2014-10-16 23:15:22 +00:00
|
|
|
|
2015-01-07 22:25:50 +00:00
|
|
|
if ('CXX_SOURCES' in categories.keys()):
|
|
|
|
makefile.write("CXXFLAGS += -std=c++11\n")
|
2014-10-16 23:15:22 +00:00
|
|
|
|
2015-01-07 22:25:50 +00:00
|
|
|
makefile.write("include $(LEVEL)/Makefile.rules\n")
|
|
|
|
makefile.flush()
|
|
|
|
makefile.close()
|
2014-10-16 23:15:22 +00:00
|
|
|
|
|
|
|
|
2015-03-30 14:12:17 +00:00
|
|
|
@skipUnlessDarwin
|
2014-10-28 20:23:20 +00:00
|
|
|
def __test_with_dsym(self):
|
2015-01-07 22:25:50 +00:00
|
|
|
self.using_dsym = True
|
|
|
|
self.BuildMakefile()
|
|
|
|
self.buildDsym()
|
2014-10-16 23:15:22 +00:00
|
|
|
self.do_test()
|
|
|
|
|
2014-10-28 20:23:20 +00:00
|
|
|
def __test_with_dwarf(self):
|
2015-01-07 22:25:50 +00:00
|
|
|
self.using_dsym = False
|
|
|
|
self.BuildMakefile()
|
|
|
|
self.buildDwarf()
|
2014-10-16 23:15:22 +00:00
|
|
|
self.do_test()
|
|
|
|
|
|
|
|
def execute_user_command(self, __command):
|
|
|
|
exec __command in globals(), locals()
|
|
|
|
|
|
|
|
def do_test(self):
|
|
|
|
exe_name = "a.out"
|
|
|
|
exe = os.path.join(os.getcwd(), exe_name)
|
|
|
|
source_files = [ f for f in os.listdir(os.getcwd()) if source_type(f) ]
|
|
|
|
target = self.dbg.CreateTarget(exe)
|
|
|
|
|
|
|
|
parser = CommandParser()
|
|
|
|
parser.parse_source_files(source_files)
|
|
|
|
parser.set_breakpoints(target)
|
|
|
|
|
|
|
|
process = target.LaunchSimple(None, None, os.getcwd())
|
|
|
|
|
|
|
|
while lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint):
|
|
|
|
thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
|
|
|
|
breakpoint_id = thread.GetStopReasonDataAtIndex (0)
|
|
|
|
parser.handle_breakpoint(self, breakpoint_id)
|
|
|
|
process.Continue()
|
|
|
|
|
|
|
|
|
|
|
|
# Utilities for testcases
|
|
|
|
|
|
|
|
def check_expression (self, expression, expected_result, use_summary = True):
|
|
|
|
value = self.frame().EvaluateExpression (expression)
|
|
|
|
self.assertTrue(value.IsValid(), expression+"returned a valid value")
|
|
|
|
if self.TraceOn():
|
|
|
|
print value.GetSummary()
|
|
|
|
print value.GetValue()
|
|
|
|
if use_summary:
|
|
|
|
answer = value.GetSummary()
|
|
|
|
else:
|
|
|
|
answer = value.GetValue()
|
|
|
|
report_str = "%s expected: %s got: %s"%(expression, expected_result, answer)
|
|
|
|
self.assertTrue(answer == expected_result, report_str)
|
|
|
|
|
2014-10-28 20:23:20 +00:00
|
|
|
def ApplyDecoratorsToFunction(func, decorators):
|
|
|
|
tmp = func
|
|
|
|
if type(decorators) == list:
|
|
|
|
for decorator in decorators:
|
|
|
|
tmp = decorator(tmp)
|
|
|
|
elif hasattr(decorators, '__call__'):
|
|
|
|
tmp = decorators(tmp)
|
|
|
|
return tmp
|
|
|
|
|
|
|
|
|
|
|
|
def MakeInlineTest(__file, __globals, decorators=None):
|
2014-10-16 23:15:22 +00:00
|
|
|
# Derive the test name from the current file name
|
|
|
|
file_basename = os.path.basename(__file)
|
|
|
|
InlineTest.mydir = TestBase.compute_mydir(__file)
|
|
|
|
|
|
|
|
test_name, _ = os.path.splitext(file_basename)
|
|
|
|
# Build the test case
|
2015-01-09 01:54:44 +00:00
|
|
|
test = type(test_name, (InlineTest,), {'using_dsym': None})
|
2014-10-16 23:15:22 +00:00
|
|
|
test.name = test_name
|
2014-10-28 20:23:20 +00:00
|
|
|
|
|
|
|
test.test_with_dsym = ApplyDecoratorsToFunction(test._InlineTest__test_with_dsym, decorators)
|
|
|
|
test.test_with_dwarf = ApplyDecoratorsToFunction(test._InlineTest__test_with_dwarf, decorators)
|
|
|
|
|
2014-10-16 23:15:22 +00:00
|
|
|
# Add the test case to the globals, and hide InlineTest
|
|
|
|
__globals.update({test_name : test})
|
2014-11-05 21:31:57 +00:00
|
|
|
|
|
|
|
return test
|
2014-10-16 23:15:22 +00:00
|
|
|
|