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