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 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 16 | |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 17 | def source_type(filename): |
| 18 | _, extension = os.path.splitext(filename) |
| 19 | return { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 20 | '.c': 'C_SOURCES', |
| 21 | '.cpp': 'CXX_SOURCES', |
| 22 | '.cxx': 'CXX_SOURCES', |
| 23 | '.cc': 'CXX_SOURCES', |
| 24 | '.m': 'OBJC_SOURCES', |
| 25 | '.mm': 'OBJCXX_SOURCES' |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 26 | }.get(extension, None) |
| 27 | |
Todd Fiala | d9be753 | 2016-01-06 19:16:45 +0000 | [diff] [blame] | 28 | |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 29 | class CommandParser: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 30 | |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 31 | def __init__(self): |
| 32 | self.breakpoints = [] |
| 33 | |
| 34 | def parse_one_command(self, line): |
| 35 | parts = line.split('//%') |
Sean Callanan | d950fa7 | 2014-10-17 00:39:37 +0000 | [diff] [blame] | 36 | |
| 37 | command = None |
| 38 | new_breakpoint = True |
| 39 | |
| 40 | if len(parts) == 2: |
Todd Fiala | d9be753 | 2016-01-06 19:16:45 +0000 | [diff] [blame] | 41 | command = parts[1].strip() # take off whitespace |
Sean Callanan | d950fa7 | 2014-10-17 00:39:37 +0000 | [diff] [blame] | 42 | new_breakpoint = parts[0].strip() != "" |
| 43 | |
| 44 | return (command, new_breakpoint) |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 45 | |
| 46 | def parse_source_files(self, source_files): |
| 47 | for source_file in source_files: |
| 48 | file_handle = open(source_file) |
| 49 | lines = file_handle.readlines() |
| 50 | line_number = 0 |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 51 | # non-NULL means we're looking through whitespace to find |
| 52 | # additional commands |
| 53 | current_breakpoint = None |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 54 | for line in lines: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 55 | line_number = line_number + 1 # 1-based, so we do this first |
Sean Callanan | d950fa7 | 2014-10-17 00:39:37 +0000 | [diff] [blame] | 56 | (command, new_breakpoint) = self.parse_one_command(line) |
| 57 | |
| 58 | if new_breakpoint: |
| 59 | current_breakpoint = None |
| 60 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 61 | if command is not None: |
| 62 | if current_breakpoint is None: |
Sean Callanan | d950fa7 | 2014-10-17 00:39:37 +0000 | [diff] [blame] | 63 | current_breakpoint = {} |
| 64 | current_breakpoint['file_name'] = source_file |
| 65 | current_breakpoint['line_number'] = line_number |
| 66 | current_breakpoint['command'] = command |
| 67 | self.breakpoints.append(current_breakpoint) |
| 68 | else: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 69 | current_breakpoint['command'] = current_breakpoint[ |
| 70 | 'command'] + "\n" + command |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 71 | |
| 72 | def set_breakpoints(self, target): |
| 73 | for breakpoint in self.breakpoints: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | breakpoint['breakpoint'] = target.BreakpointCreateByLocation( |
| 75 | breakpoint['file_name'], breakpoint['line_number']) |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 76 | |
| 77 | def handle_breakpoint(self, test, breakpoint_id): |
| 78 | for breakpoint in self.breakpoints: |
| 79 | if breakpoint['breakpoint'].GetID() == breakpoint_id: |
| 80 | test.execute_user_command(breakpoint['command']) |
| 81 | return |
| 82 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 83 | |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 84 | class InlineTest(TestBase): |
| 85 | # Internal implementation |
| 86 | |
Greg Clayton | 7099558 | 2015-01-07 22:25:50 +0000 | [diff] [blame] | 87 | def BuildMakefile(self): |
Adrian Prantl | 5ec76fe | 2018-01-30 18:29:16 +0000 | [diff] [blame] | 88 | makefilePath = self.getBuildArtifact("Makefile") |
| 89 | if os.path.exists(makefilePath): |
Pavel Labath | f0f62d8 | 2016-06-07 21:29:46 +0000 | [diff] [blame] | 90 | return |
| 91 | |
Greg Clayton | 7099558 | 2015-01-07 22:25:50 +0000 | [diff] [blame] | 92 | categories = {} |
| 93 | |
Adrian Prantl | 5ec76fe | 2018-01-30 18:29:16 +0000 | [diff] [blame] | 94 | for f in os.listdir(self.getSourceDir()): |
Greg Clayton | 7099558 | 2015-01-07 22:25:50 +0000 | [diff] [blame] | 95 | t = source_type(f) |
| 96 | if t: |
Zachary Turner | 606e1e3 | 2015-10-23 17:53:51 +0000 | [diff] [blame] | 97 | if t in list(categories.keys()): |
Greg Clayton | 7099558 | 2015-01-07 22:25:50 +0000 | [diff] [blame] | 98 | categories[t].append(f) |
| 99 | else: |
| 100 | categories[t] = [f] |
| 101 | |
Adrian Prantl | 5ec76fe | 2018-01-30 18:29:16 +0000 | [diff] [blame] | 102 | makefile = open(makefilePath, 'w+') |
Greg Clayton | 7099558 | 2015-01-07 22:25:50 +0000 | [diff] [blame] | 103 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 104 | level = os.sep.join( |
| 105 | [".."] * len(self.mydir.split(os.sep))) + os.sep + "make" |
Greg Clayton | 7099558 | 2015-01-07 22:25:50 +0000 | [diff] [blame] | 106 | |
| 107 | makefile.write("LEVEL = " + level + "\n") |
| 108 | |
Zachary Turner | 606e1e3 | 2015-10-23 17:53:51 +0000 | [diff] [blame] | 109 | for t in list(categories.keys()): |
Greg Clayton | 7099558 | 2015-01-07 22:25:50 +0000 | [diff] [blame] | 110 | line = t + " := " + " ".join(categories[t]) |
| 111 | makefile.write(line + "\n") |
| 112 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 113 | if ('OBJCXX_SOURCES' in list(categories.keys())) or ( |
| 114 | 'OBJC_SOURCES' in list(categories.keys())): |
| 115 | makefile.write( |
| 116 | "LDFLAGS = $(CFLAGS) -lobjc -framework Foundation\n") |
Greg Clayton | 7099558 | 2015-01-07 22:25:50 +0000 | [diff] [blame] | 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 | |
Pavel Labath | 6637738 | 2018-06-05 10:58:44 +0000 | [diff] [blame] | 126 | def _test(self): |
Greg Clayton | 7099558 | 2015-01-07 22:25:50 +0000 | [diff] [blame] | 127 | self.BuildMakefile() |
Adrian Prantl | 5ec76fe | 2018-01-30 18:29:16 +0000 | [diff] [blame] | 128 | self.build() |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 129 | self.do_test() |
Todd Fiala | 94eb010 | 2016-05-26 13:57:03 +0000 | [diff] [blame] | 130 | |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 131 | def execute_user_command(self, __command): |
Zachary Turner | 5821f79 | 2015-11-06 21:37:21 +0000 | [diff] [blame] | 132 | exec(__command, globals(), locals()) |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 133 | |
| 134 | def do_test(self): |
Adrian Prantl | 595048f | 2018-01-19 23:24:35 +0000 | [diff] [blame] | 135 | exe = self.getBuildArtifact("a.out") |
Adrian Prantl | 5ec76fe | 2018-01-30 18:29:16 +0000 | [diff] [blame] | 136 | source_files = [f for f in os.listdir(self.getSourceDir()) |
| 137 | if source_type(f)] |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 138 | target = self.dbg.CreateTarget(exe) |
| 139 | |
| 140 | parser = CommandParser() |
| 141 | parser.parse_source_files(source_files) |
| 142 | parser.set_breakpoints(target) |
| 143 | |
Pavel Labath | 9bdd03f | 2018-02-27 22:45:49 +0000 | [diff] [blame] | 144 | process = target.LaunchSimple(None, None, self.get_process_working_directory()) |
Adrian Prantl | 87a000d | 2018-02-26 22:40:20 +0000 | [diff] [blame] | 145 | hit_breakpoints = 0 |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 146 | |
| 147 | while lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint): |
Adrian Prantl | 87a000d | 2018-02-26 22:40:20 +0000 | [diff] [blame] | 148 | hit_breakpoints += 1 |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 149 | thread = lldbutil.get_stopped_thread( |
| 150 | process, lldb.eStopReasonBreakpoint) |
| 151 | breakpoint_id = thread.GetStopReasonDataAtIndex(0) |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 152 | parser.handle_breakpoint(self, breakpoint_id) |
| 153 | process.Continue() |
| 154 | |
Adrian Prantl | 87a000d | 2018-02-26 22:40:20 +0000 | [diff] [blame] | 155 | self.assertTrue(hit_breakpoints > 0, |
| 156 | "inline test did not hit a single breakpoint") |
| 157 | # Either the process exited or the stepping plan is complete. |
| 158 | self.assertTrue(process.GetState() in [lldb.eStateStopped, |
| 159 | lldb.eStateExited], |
| 160 | PROCESS_EXITED) |
| 161 | |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 162 | # Utilities for testcases |
| 163 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 164 | def check_expression(self, expression, expected_result, use_summary=True): |
| 165 | value = self.frame().EvaluateExpression(expression) |
| 166 | self.assertTrue(value.IsValid(), expression + "returned a valid value") |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 167 | if self.TraceOn(): |
Zachary Turner | ff890da | 2015-10-19 23:45:41 +0000 | [diff] [blame] | 168 | print(value.GetSummary()) |
| 169 | print(value.GetValue()) |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 170 | if use_summary: |
| 171 | answer = value.GetSummary() |
| 172 | else: |
| 173 | answer = value.GetValue() |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 174 | report_str = "%s expected: %s got: %s" % ( |
| 175 | expression, expected_result, answer) |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 176 | self.assertTrue(answer == expected_result, report_str) |
| 177 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 178 | |
Sean Callanan | e17428a | 2014-10-28 20:23:20 +0000 | [diff] [blame] | 179 | def ApplyDecoratorsToFunction(func, decorators): |
| 180 | tmp = func |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 181 | if isinstance(decorators, list): |
Sean Callanan | e17428a | 2014-10-28 20:23:20 +0000 | [diff] [blame] | 182 | for decorator in decorators: |
| 183 | tmp = decorator(tmp) |
| 184 | elif hasattr(decorators, '__call__'): |
| 185 | tmp = decorators(tmp) |
| 186 | return tmp |
Adrian McCarthy | 3f99810 | 2016-05-04 23:32:35 +0000 | [diff] [blame] | 187 | |
Sean Callanan | e17428a | 2014-10-28 20:23:20 +0000 | [diff] [blame] | 188 | |
| 189 | def MakeInlineTest(__file, __globals, decorators=None): |
Todd Fiala | dad52ce | 2016-04-18 20:26:56 +0000 | [diff] [blame] | 190 | # Adjust the filename if it ends in .pyc. We want filenames to |
| 191 | # reflect the source python file, not the compiled variant. |
| 192 | if __file is not None and __file.endswith(".pyc"): |
| 193 | # Strip the trailing "c" |
| 194 | __file = __file[0:-1] |
| 195 | |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 196 | # Derive the test name from the current file name |
| 197 | file_basename = os.path.basename(__file) |
| 198 | InlineTest.mydir = TestBase.compute_mydir(__file) |
| 199 | |
| 200 | test_name, _ = os.path.splitext(file_basename) |
Sean Callanan | e17428a | 2014-10-28 20:23:20 +0000 | [diff] [blame] | 201 | |
Pavel Labath | 6637738 | 2018-06-05 10:58:44 +0000 | [diff] [blame] | 202 | test_func = ApplyDecoratorsToFunction(InlineTest._test, decorators) |
| 203 | # Build the test case |
| 204 | test_class = type(test_name, (InlineTest,), dict(test=test_func, name=test_name)) |
Sean Callanan | e17428a | 2014-10-28 20:23:20 +0000 | [diff] [blame] | 205 | |
Sean Callanan | 816cb3e | 2014-10-16 23:15:22 +0000 | [diff] [blame] | 206 | # Add the test case to the globals, and hide InlineTest |
Pavel Labath | 6637738 | 2018-06-05 10:58:44 +0000 | [diff] [blame] | 207 | __globals.update({test_name: test_class}) |
Todd Fiala | 5bdbef64 | 2015-12-22 17:14:47 +0000 | [diff] [blame] | 208 | |
Todd Fiala | dad52ce | 2016-04-18 20:26:56 +0000 | [diff] [blame] | 209 | # Keep track of the original test filename so we report it |
| 210 | # correctly in test results. |
Pavel Labath | 6637738 | 2018-06-05 10:58:44 +0000 | [diff] [blame] | 211 | test_class.test_filename = __file |
| 212 | return test_class |