Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 1 | import lldb |
| 2 | from lldbtest import * |
| 3 | import lldbutil |
| 4 | import os |
| 5 | import new |
Sean Callanan | e17428a | 2014-10-28 20:23:20 +0000 | [diff] [blame^] | 6 | import unittest2 |
| 7 | import sys |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 8 | |
| 9 | def source_type(filename): |
| 10 | _, extension = os.path.splitext(filename) |
| 11 | return { |
| 12 | '.c' : 'C_SOURCES', |
| 13 | '.cpp' : 'CXX_SOURCES', |
| 14 | '.cxx' : 'CXX_SOURCES', |
| 15 | '.cc' : 'CXX_SOURCES', |
| 16 | '.m' : 'OBJC_SOURCES', |
| 17 | '.mm' : 'OBJCXX_SOURCES' |
| 18 | }.get(extension, None) |
| 19 | |
| 20 | class CommandParser: |
| 21 | def __init__(self): |
| 22 | self.breakpoints = [] |
| 23 | |
| 24 | def parse_one_command(self, line): |
| 25 | parts = line.split('//%') |
Sean Callanan | d950fa7 | 2014-10-17 00:39:37 +0000 | [diff] [blame] | 26 | |
| 27 | command = None |
| 28 | new_breakpoint = True |
| 29 | |
| 30 | if len(parts) == 2: |
| 31 | command = parts[1].strip() # take off whitespace |
| 32 | new_breakpoint = parts[0].strip() != "" |
| 33 | |
| 34 | return (command, new_breakpoint) |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 35 | |
| 36 | def parse_source_files(self, source_files): |
| 37 | for source_file in source_files: |
| 38 | file_handle = open(source_file) |
| 39 | lines = file_handle.readlines() |
| 40 | line_number = 0 |
Sean Callanan | d950fa7 | 2014-10-17 00:39:37 +0000 | [diff] [blame] | 41 | current_breakpoint = None # non-NULL means we're looking through whitespace to find additional commands |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 42 | for line in lines: |
| 43 | line_number = line_number + 1 # 1-based, so we do this first |
Sean Callanan | d950fa7 | 2014-10-17 00:39:37 +0000 | [diff] [blame] | 44 | (command, new_breakpoint) = self.parse_one_command(line) |
| 45 | |
| 46 | if new_breakpoint: |
| 47 | current_breakpoint = None |
| 48 | |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 49 | if command != None: |
Sean Callanan | d950fa7 | 2014-10-17 00:39:37 +0000 | [diff] [blame] | 50 | if current_breakpoint == None: |
| 51 | current_breakpoint = {} |
| 52 | current_breakpoint['file_name'] = source_file |
| 53 | current_breakpoint['line_number'] = line_number |
| 54 | current_breakpoint['command'] = command |
| 55 | self.breakpoints.append(current_breakpoint) |
| 56 | else: |
| 57 | current_breakpoint['command'] = current_breakpoint['command'] + "\n" + command |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 58 | |
| 59 | def set_breakpoints(self, target): |
| 60 | for breakpoint in self.breakpoints: |
| 61 | breakpoint['breakpoint'] = target.BreakpointCreateByLocation(breakpoint['file_name'], breakpoint['line_number']) |
| 62 | |
| 63 | def handle_breakpoint(self, test, breakpoint_id): |
| 64 | for breakpoint in self.breakpoints: |
| 65 | if breakpoint['breakpoint'].GetID() == breakpoint_id: |
| 66 | test.execute_user_command(breakpoint['command']) |
| 67 | return |
| 68 | |
| 69 | def BuildMakefile(mydir): |
| 70 | categories = {} |
| 71 | |
| 72 | for f in os.listdir(os.getcwd()): |
| 73 | t = source_type(f) |
| 74 | if t: |
| 75 | if t in categories.keys(): |
| 76 | categories[t].append(f) |
| 77 | else: |
| 78 | categories[t] = [f] |
| 79 | |
| 80 | makefile = open("Makefile", 'w+') |
| 81 | |
| 82 | level = os.sep.join([".."] * len(mydir.split(os.sep))) + os.sep + "make" |
| 83 | |
| 84 | makefile.write("LEVEL = " + level + "\n") |
| 85 | |
| 86 | for t in categories.keys(): |
| 87 | line = t + " := " + " ".join(categories[t]) |
| 88 | makefile.write(line + "\n") |
| 89 | |
| 90 | if ('OBJCXX_SOURCES' in categories.keys()) or ('OBJC_SOURCES' in categories.keys()): |
| 91 | makefile.write("LDFLAGS = $(CFLAGS) -lobjc -framework Foundation\n") |
| 92 | |
| 93 | if ('CXX_SOURCES' in categories.keys()): |
Enrico Granata | 4bd54a1 | 2014-10-22 20:33:34 +0000 | [diff] [blame] | 94 | makefile.write("CXXFLAGS += -std=c++11\n") |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 95 | |
| 96 | makefile.write("include $(LEVEL)/Makefile.rules\n") |
| 97 | makefile.flush() |
| 98 | makefile.close() |
| 99 | |
| 100 | def CleanMakefile(): |
| 101 | if (os.path.isfile("Makefile")): |
| 102 | os.unlink("Makefile") |
| 103 | |
| 104 | class InlineTest(TestBase): |
| 105 | # Internal implementation |
| 106 | |
Ed Maste | e1b2536 | 2014-10-23 15:21:45 +0000 | [diff] [blame] | 107 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 108 | def buildDsymWithImplicitMakefile(self): |
| 109 | BuildMakefile(self.mydir) |
| 110 | self.buildDsym() |
| 111 | |
| 112 | def buildDwarfWithImplicitMakefile(self): |
| 113 | BuildMakefile(self.mydir) |
| 114 | self.buildDwarf() |
| 115 | |
Ed Maste | e1b2536 | 2014-10-23 15:21:45 +0000 | [diff] [blame] | 116 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
Sean Callanan | e17428a | 2014-10-28 20:23:20 +0000 | [diff] [blame^] | 117 | def __test_with_dsym(self): |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 118 | self.buildDsymWithImplicitMakefile() |
| 119 | self.do_test() |
| 120 | |
Sean Callanan | e17428a | 2014-10-28 20:23:20 +0000 | [diff] [blame^] | 121 | def __test_with_dwarf(self): |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 122 | self.buildDwarfWithImplicitMakefile() |
| 123 | self.do_test() |
| 124 | |
| 125 | def execute_user_command(self, __command): |
| 126 | exec __command in globals(), locals() |
| 127 | |
| 128 | def do_test(self): |
| 129 | exe_name = "a.out" |
| 130 | exe = os.path.join(os.getcwd(), exe_name) |
| 131 | source_files = [ f for f in os.listdir(os.getcwd()) if source_type(f) ] |
| 132 | target = self.dbg.CreateTarget(exe) |
| 133 | |
| 134 | parser = CommandParser() |
| 135 | parser.parse_source_files(source_files) |
| 136 | parser.set_breakpoints(target) |
| 137 | |
| 138 | process = target.LaunchSimple(None, None, os.getcwd()) |
| 139 | |
| 140 | while lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint): |
| 141 | thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) |
| 142 | breakpoint_id = thread.GetStopReasonDataAtIndex (0) |
| 143 | parser.handle_breakpoint(self, breakpoint_id) |
| 144 | process.Continue() |
| 145 | |
| 146 | @classmethod |
| 147 | def classCleanup(cls): |
| 148 | CleanMakefile() |
| 149 | |
| 150 | # Utilities for testcases |
| 151 | |
| 152 | def check_expression (self, expression, expected_result, use_summary = True): |
| 153 | value = self.frame().EvaluateExpression (expression) |
| 154 | self.assertTrue(value.IsValid(), expression+"returned a valid value") |
| 155 | if self.TraceOn(): |
| 156 | print value.GetSummary() |
| 157 | print value.GetValue() |
| 158 | if use_summary: |
| 159 | answer = value.GetSummary() |
| 160 | else: |
| 161 | answer = value.GetValue() |
| 162 | report_str = "%s expected: %s got: %s"%(expression, expected_result, answer) |
| 163 | self.assertTrue(answer == expected_result, report_str) |
| 164 | |
Sean Callanan | e17428a | 2014-10-28 20:23:20 +0000 | [diff] [blame^] | 165 | def ApplyDecoratorsToFunction(func, decorators): |
| 166 | tmp = func |
| 167 | if type(decorators) == list: |
| 168 | for decorator in decorators: |
| 169 | tmp = decorator(tmp) |
| 170 | elif hasattr(decorators, '__call__'): |
| 171 | tmp = decorators(tmp) |
| 172 | return tmp |
| 173 | |
| 174 | |
| 175 | def MakeInlineTest(__file, __globals, decorators=None): |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 176 | # Derive the test name from the current file name |
| 177 | file_basename = os.path.basename(__file) |
| 178 | InlineTest.mydir = TestBase.compute_mydir(__file) |
| 179 | |
| 180 | test_name, _ = os.path.splitext(file_basename) |
| 181 | # Build the test case |
| 182 | test = new.classobj(test_name, (InlineTest,), {}) |
| 183 | test.name = test_name |
Sean Callanan | e17428a | 2014-10-28 20:23:20 +0000 | [diff] [blame^] | 184 | |
| 185 | test.test_with_dsym = ApplyDecoratorsToFunction(test._InlineTest__test_with_dsym, decorators) |
| 186 | test.test_with_dwarf = ApplyDecoratorsToFunction(test._InlineTest__test_with_dwarf, decorators) |
| 187 | |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 188 | # Add the test case to the globals, and hide InlineTest |
| 189 | __globals.update({test_name : test}) |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 190 | |
| 191 | |