Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 1 | from __future__ import print_function |
Zachary Turner | c1b7cd7 | 2015-11-05 19:22:28 +0000 | [diff] [blame] | 2 | from __future__ import absolute_import |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 3 | |
Zachary Turner | c1b7cd7 | 2015-11-05 19:22:28 +0000 | [diff] [blame] | 4 | # System modules |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 5 | import os |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 6 | |
Zachary Turner | c1b7cd7 | 2015-11-05 19:22:28 +0000 | [diff] [blame] | 7 | # Third-party modules |
| 8 | |
| 9 | # LLDB modules |
| 10 | import lldb |
| 11 | from .lldbtest import * |
Todd Fiala | 94eb010 | 2016-05-26 13:57:03 +0000 | [diff] [blame] | 12 | from . import configuration |
Zachary Turner | c1b7cd7 | 2015-11-05 19:22:28 +0000 | [diff] [blame] | 13 | from . import lldbutil |
Zachary Turner | 9a1a294 | 2016-02-04 23:04:17 +0000 | [diff] [blame] | 14 | from .decorators import * |
Zachary Turner | c1b7cd7 | 2015-11-05 19:22:28 +0000 | [diff] [blame] | 15 | |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 16 | def source_type(filename): |
| 17 | _, extension = os.path.splitext(filename) |
| 18 | return { |
| 19 | '.c' : 'C_SOURCES', |
| 20 | '.cpp' : 'CXX_SOURCES', |
| 21 | '.cxx' : 'CXX_SOURCES', |
| 22 | '.cc' : 'CXX_SOURCES', |
| 23 | '.m' : 'OBJC_SOURCES', |
| 24 | '.mm' : 'OBJCXX_SOURCES' |
| 25 | }.get(extension, None) |
| 26 | |
Todd Fiala | d9be753 | 2016-01-06 19:16:45 +0000 | [diff] [blame] | 27 | |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 28 | class CommandParser: |
| 29 | def __init__(self): |
| 30 | self.breakpoints = [] |
| 31 | |
| 32 | def parse_one_command(self, line): |
| 33 | parts = line.split('//%') |
Sean Callanan | d950fa7 | 2014-10-17 00:39:37 +0000 | [diff] [blame] | 34 | |
| 35 | command = None |
| 36 | new_breakpoint = True |
| 37 | |
| 38 | if len(parts) == 2: |
Todd Fiala | d9be753 | 2016-01-06 19:16:45 +0000 | [diff] [blame] | 39 | command = parts[1].strip() # take off whitespace |
Sean Callanan | d950fa7 | 2014-10-17 00:39:37 +0000 | [diff] [blame] | 40 | new_breakpoint = parts[0].strip() != "" |
| 41 | |
| 42 | return (command, new_breakpoint) |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 43 | |
| 44 | def parse_source_files(self, source_files): |
| 45 | for source_file in source_files: |
| 46 | file_handle = open(source_file) |
| 47 | lines = file_handle.readlines() |
| 48 | line_number = 0 |
Sean Callanan | d950fa7 | 2014-10-17 00:39:37 +0000 | [diff] [blame] | 49 | 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] | 50 | for line in lines: |
| 51 | line_number = line_number + 1 # 1-based, so we do this first |
Sean Callanan | d950fa7 | 2014-10-17 00:39:37 +0000 | [diff] [blame] | 52 | (command, new_breakpoint) = self.parse_one_command(line) |
| 53 | |
| 54 | if new_breakpoint: |
| 55 | current_breakpoint = None |
| 56 | |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 57 | if command != None: |
Sean Callanan | d950fa7 | 2014-10-17 00:39:37 +0000 | [diff] [blame] | 58 | if current_breakpoint == None: |
| 59 | current_breakpoint = {} |
| 60 | current_breakpoint['file_name'] = source_file |
| 61 | current_breakpoint['line_number'] = line_number |
| 62 | current_breakpoint['command'] = command |
| 63 | self.breakpoints.append(current_breakpoint) |
| 64 | else: |
| 65 | current_breakpoint['command'] = current_breakpoint['command'] + "\n" + command |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 66 | |
| 67 | def set_breakpoints(self, target): |
| 68 | for breakpoint in self.breakpoints: |
| 69 | breakpoint['breakpoint'] = target.BreakpointCreateByLocation(breakpoint['file_name'], breakpoint['line_number']) |
| 70 | |
| 71 | def handle_breakpoint(self, test, breakpoint_id): |
| 72 | for breakpoint in self.breakpoints: |
| 73 | if breakpoint['breakpoint'].GetID() == breakpoint_id: |
| 74 | test.execute_user_command(breakpoint['command']) |
| 75 | return |
| 76 | |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 77 | class InlineTest(TestBase): |
| 78 | # Internal implementation |
| 79 | |
Greg Clayton | 7099558 | 2015-01-07 22:25:50 +0000 | [diff] [blame] | 80 | def getRerunArgs(self): |
| 81 | # The -N option says to NOT run a if it matches the option argument, so |
| 82 | # 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] | 83 | if self.using_dsym is None: |
| 84 | # The test was skipped altogether. |
| 85 | return "" |
| 86 | elif self.using_dsym: |
Greg Clayton | 7099558 | 2015-01-07 22:25:50 +0000 | [diff] [blame] | 87 | return "-N dwarf %s" % (self.mydir) |
| 88 | else: |
| 89 | return "-N dsym %s" % (self.mydir) |
Todd Fiala | d9be753 | 2016-01-06 19:16:45 +0000 | [diff] [blame] | 90 | |
Greg Clayton | 7099558 | 2015-01-07 22:25:50 +0000 | [diff] [blame] | 91 | def BuildMakefile(self): |
Pavel Labath | f0f62d8 | 2016-06-07 21:29:46 +0000 | [diff] [blame^] | 92 | if os.path.exists("Makefile"): |
| 93 | return |
| 94 | |
Greg Clayton | 7099558 | 2015-01-07 22:25:50 +0000 | [diff] [blame] | 95 | categories = {} |
| 96 | |
| 97 | for f in os.listdir(os.getcwd()): |
| 98 | t = source_type(f) |
| 99 | if t: |
Zachary Turner | 606e1e3 | 2015-10-23 17:53:51 +0000 | [diff] [blame] | 100 | if t in list(categories.keys()): |
Greg Clayton | 7099558 | 2015-01-07 22:25:50 +0000 | [diff] [blame] | 101 | categories[t].append(f) |
| 102 | else: |
| 103 | categories[t] = [f] |
| 104 | |
Pavel Labath | f0f62d8 | 2016-06-07 21:29:46 +0000 | [diff] [blame^] | 105 | makefile = open("Makefile", 'w+') |
Greg Clayton | 7099558 | 2015-01-07 22:25:50 +0000 | [diff] [blame] | 106 | |
| 107 | level = os.sep.join([".."] * len(self.mydir.split(os.sep))) + os.sep + "make" |
| 108 | |
| 109 | makefile.write("LEVEL = " + level + "\n") |
| 110 | |
Zachary Turner | 606e1e3 | 2015-10-23 17:53:51 +0000 | [diff] [blame] | 111 | for t in list(categories.keys()): |
Greg Clayton | 7099558 | 2015-01-07 22:25:50 +0000 | [diff] [blame] | 112 | line = t + " := " + " ".join(categories[t]) |
| 113 | makefile.write(line + "\n") |
| 114 | |
Zachary Turner | 606e1e3 | 2015-10-23 17:53:51 +0000 | [diff] [blame] | 115 | if ('OBJCXX_SOURCES' in list(categories.keys())) or ('OBJC_SOURCES' in list(categories.keys())): |
Greg Clayton | 7099558 | 2015-01-07 22:25:50 +0000 | [diff] [blame] | 116 | makefile.write("LDFLAGS = $(CFLAGS) -lobjc -framework Foundation\n") |
| 117 | |
Zachary Turner | 606e1e3 | 2015-10-23 17:53:51 +0000 | [diff] [blame] | 118 | if ('CXX_SOURCES' in list(categories.keys())): |
Greg Clayton | 7099558 | 2015-01-07 22:25:50 +0000 | [diff] [blame] | 119 | makefile.write("CXXFLAGS += -std=c++11\n") |
| 120 | |
Pavel Labath | f0f62d8 | 2016-06-07 21:29:46 +0000 | [diff] [blame^] | 121 | makefile.write("include $(LEVEL)/Makefile.rules\n") |
Sean Callanan | bca81c5 | 2016-01-26 01:15:57 +0000 | [diff] [blame] | 122 | makefile.write("\ncleanup:\n\trm -f Makefile *.d\n\n") |
Greg Clayton | 7099558 | 2015-01-07 22:25:50 +0000 | [diff] [blame] | 123 | makefile.flush() |
| 124 | makefile.close() |
| 125 | |
Robert Flack | 13c7ad9 | 2015-03-30 14:12:17 +0000 | [diff] [blame] | 126 | @skipUnlessDarwin |
Sean Callanan | e17428a | 2014-10-28 20:23:20 +0000 | [diff] [blame] | 127 | def __test_with_dsym(self): |
Greg Clayton | 7099558 | 2015-01-07 22:25:50 +0000 | [diff] [blame] | 128 | self.using_dsym = True |
| 129 | self.BuildMakefile() |
| 130 | self.buildDsym() |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 131 | self.do_test() |
| 132 | |
Sean Callanan | e17428a | 2014-10-28 20:23:20 +0000 | [diff] [blame] | 133 | def __test_with_dwarf(self): |
Greg Clayton | 7099558 | 2015-01-07 22:25:50 +0000 | [diff] [blame] | 134 | self.using_dsym = False |
| 135 | self.BuildMakefile() |
| 136 | self.buildDwarf() |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 137 | self.do_test() |
| 138 | |
Enrico Granata | 238de51 | 2015-10-19 20:40:50 +0000 | [diff] [blame] | 139 | def __test_with_dwo(self): |
| 140 | self.using_dsym = False |
| 141 | self.BuildMakefile() |
| 142 | self.buildDwo() |
| 143 | self.do_test() |
| 144 | |
Todd Fiala | 94eb010 | 2016-05-26 13:57:03 +0000 | [diff] [blame] | 145 | def __test_with_gmodules(self): |
| 146 | self.using_dsym = False |
| 147 | self.BuildMakefile() |
| 148 | self.buildGModules() |
| 149 | self.do_test() |
| 150 | |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 151 | def execute_user_command(self, __command): |
Zachary Turner | 5821f79 | 2015-11-06 21:37:21 +0000 | [diff] [blame] | 152 | exec(__command, globals(), locals()) |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 153 | |
| 154 | def do_test(self): |
| 155 | exe_name = "a.out" |
| 156 | exe = os.path.join(os.getcwd(), exe_name) |
| 157 | source_files = [ f for f in os.listdir(os.getcwd()) if source_type(f) ] |
| 158 | target = self.dbg.CreateTarget(exe) |
| 159 | |
| 160 | parser = CommandParser() |
| 161 | parser.parse_source_files(source_files) |
| 162 | parser.set_breakpoints(target) |
| 163 | |
| 164 | process = target.LaunchSimple(None, None, os.getcwd()) |
| 165 | |
| 166 | while lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint): |
| 167 | thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) |
| 168 | breakpoint_id = thread.GetStopReasonDataAtIndex (0) |
| 169 | parser.handle_breakpoint(self, breakpoint_id) |
| 170 | process.Continue() |
| 171 | |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 172 | |
| 173 | # Utilities for testcases |
| 174 | |
| 175 | def check_expression (self, expression, expected_result, use_summary = True): |
| 176 | value = self.frame().EvaluateExpression (expression) |
| 177 | self.assertTrue(value.IsValid(), expression+"returned a valid value") |
| 178 | if self.TraceOn(): |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 179 | print(value.GetSummary()) |
| 180 | print(value.GetValue()) |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 181 | if use_summary: |
| 182 | answer = value.GetSummary() |
| 183 | else: |
| 184 | answer = value.GetValue() |
| 185 | report_str = "%s expected: %s got: %s"%(expression, expected_result, answer) |
| 186 | self.assertTrue(answer == expected_result, report_str) |
| 187 | |
Sean Callanan | e17428a | 2014-10-28 20:23:20 +0000 | [diff] [blame] | 188 | def ApplyDecoratorsToFunction(func, decorators): |
| 189 | tmp = func |
| 190 | if type(decorators) == list: |
| 191 | for decorator in decorators: |
| 192 | tmp = decorator(tmp) |
| 193 | elif hasattr(decorators, '__call__'): |
| 194 | tmp = decorators(tmp) |
| 195 | return tmp |
Adrian McCarthy | 3f99810 | 2016-05-04 23:32:35 +0000 | [diff] [blame] | 196 | |
Sean Callanan | e17428a | 2014-10-28 20:23:20 +0000 | [diff] [blame] | 197 | |
| 198 | def MakeInlineTest(__file, __globals, decorators=None): |
Todd Fiala | dad52ce | 2016-04-18 20:26:56 +0000 | [diff] [blame] | 199 | # Adjust the filename if it ends in .pyc. We want filenames to |
| 200 | # reflect the source python file, not the compiled variant. |
| 201 | if __file is not None and __file.endswith(".pyc"): |
| 202 | # Strip the trailing "c" |
| 203 | __file = __file[0:-1] |
| 204 | |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 205 | # Derive the test name from the current file name |
| 206 | file_basename = os.path.basename(__file) |
| 207 | InlineTest.mydir = TestBase.compute_mydir(__file) |
| 208 | |
| 209 | test_name, _ = os.path.splitext(file_basename) |
Adrian McCarthy | 3f99810 | 2016-05-04 23:32:35 +0000 | [diff] [blame] | 210 | # Build the test case |
Siva Chandra | 311c7db | 2015-01-09 01:54:44 +0000 | [diff] [blame] | 211 | test = type(test_name, (InlineTest,), {'using_dsym': None}) |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 212 | test.name = test_name |
Sean Callanan | e17428a | 2014-10-28 20:23:20 +0000 | [diff] [blame] | 213 | |
Adrian McCarthy | 3f99810 | 2016-05-04 23:32:35 +0000 | [diff] [blame] | 214 | target_platform = lldb.DBG.GetSelectedPlatform().GetTriple().split('-')[2] |
Todd Fiala | 94eb010 | 2016-05-26 13:57:03 +0000 | [diff] [blame] | 215 | if test_categories.is_supported_on_platform("dsym", target_platform, configuration.compilers): |
Adrian McCarthy | 3f99810 | 2016-05-04 23:32:35 +0000 | [diff] [blame] | 216 | test.test_with_dsym = ApplyDecoratorsToFunction(test._InlineTest__test_with_dsym, decorators) |
Todd Fiala | 94eb010 | 2016-05-26 13:57:03 +0000 | [diff] [blame] | 217 | if test_categories.is_supported_on_platform("dwarf", target_platform, configuration.compilers): |
Adrian McCarthy | 3f99810 | 2016-05-04 23:32:35 +0000 | [diff] [blame] | 218 | test.test_with_dwarf = ApplyDecoratorsToFunction(test._InlineTest__test_with_dwarf, decorators) |
Todd Fiala | 94eb010 | 2016-05-26 13:57:03 +0000 | [diff] [blame] | 219 | if test_categories.is_supported_on_platform("dwo", target_platform, configuration.compilers): |
Adrian McCarthy | 3f99810 | 2016-05-04 23:32:35 +0000 | [diff] [blame] | 220 | test.test_with_dwo = ApplyDecoratorsToFunction(test._InlineTest__test_with_dwo, decorators) |
Todd Fiala | 94eb010 | 2016-05-26 13:57:03 +0000 | [diff] [blame] | 221 | if test_categories.is_supported_on_platform("gmodules", target_platform, configuration.compilers): |
| 222 | test.test_with_gmodules = ApplyDecoratorsToFunction(test._InlineTest__test_with_gmodules, decorators) |
Sean Callanan | e17428a | 2014-10-28 20:23:20 +0000 | [diff] [blame] | 223 | |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 224 | # Add the test case to the globals, and hide InlineTest |
| 225 | __globals.update({test_name : test}) |
Todd Fiala | 5bdbef64 | 2015-12-22 17:14:47 +0000 | [diff] [blame] | 226 | |
Todd Fiala | dad52ce | 2016-04-18 20:26:56 +0000 | [diff] [blame] | 227 | # Keep track of the original test filename so we report it |
| 228 | # correctly in test results. |
Todd Fiala | 5bdbef64 | 2015-12-22 17:14:47 +0000 | [diff] [blame] | 229 | test.test_filename = __file |
Enrico Granata | ab0e831 | 2014-11-05 21:31:57 +0000 | [diff] [blame] | 230 | return test |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 231 | |