blob: c8209f72e3ae3f8559db8762c471d12ee4e8f3d2 [file] [log] [blame]
Sean Callanan816cb3e2014-10-16 23:15:22 +00001import lldb
2from lldbtest import *
3import lldbutil
4import os
5import new
Sean Callanane17428a2014-10-28 20:23:20 +00006import unittest2
7import sys
Sean Callanan816cb3e2014-10-16 23:15:22 +00008
9def 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
20class CommandParser:
21 def __init__(self):
22 self.breakpoints = []
23
24 def parse_one_command(self, line):
25 parts = line.split('//%')
Sean Callanand950fa72014-10-17 00:39:37 +000026
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 Callanan816cb3e2014-10-16 23:15:22 +000035
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 Callanand950fa72014-10-17 00:39:37 +000041 current_breakpoint = None # non-NULL means we're looking through whitespace to find additional commands
Sean Callanan816cb3e2014-10-16 23:15:22 +000042 for line in lines:
43 line_number = line_number + 1 # 1-based, so we do this first
Sean Callanand950fa72014-10-17 00:39:37 +000044 (command, new_breakpoint) = self.parse_one_command(line)
45
46 if new_breakpoint:
47 current_breakpoint = None
48
Sean Callanan816cb3e2014-10-16 23:15:22 +000049 if command != None:
Sean Callanand950fa72014-10-17 00:39:37 +000050 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 Callanan816cb3e2014-10-16 23:15:22 +000058
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
69def 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 Granata4bd54a12014-10-22 20:33:34 +000094 makefile.write("CXXFLAGS += -std=c++11\n")
Sean Callanan816cb3e2014-10-16 23:15:22 +000095
96 makefile.write("include $(LEVEL)/Makefile.rules\n")
97 makefile.flush()
98 makefile.close()
99
100def CleanMakefile():
101 if (os.path.isfile("Makefile")):
102 os.unlink("Makefile")
103
104class InlineTest(TestBase):
105 # Internal implementation
106
Ed Mastee1b25362014-10-23 15:21:45 +0000107 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
Sean Callanan816cb3e2014-10-16 23:15:22 +0000108 def buildDsymWithImplicitMakefile(self):
109 BuildMakefile(self.mydir)
110 self.buildDsym()
111
112 def buildDwarfWithImplicitMakefile(self):
113 BuildMakefile(self.mydir)
114 self.buildDwarf()
115
Ed Mastee1b25362014-10-23 15:21:45 +0000116 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
Sean Callanane17428a2014-10-28 20:23:20 +0000117 def __test_with_dsym(self):
Sean Callanan816cb3e2014-10-16 23:15:22 +0000118 self.buildDsymWithImplicitMakefile()
119 self.do_test()
120
Sean Callanane17428a2014-10-28 20:23:20 +0000121 def __test_with_dwarf(self):
Sean Callanan816cb3e2014-10-16 23:15:22 +0000122 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 Callanane17428a2014-10-28 20:23:20 +0000165def 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
175def MakeInlineTest(__file, __globals, decorators=None):
Sean Callanan816cb3e2014-10-16 23:15:22 +0000176 # 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 Callanane17428a2014-10-28 20:23:20 +0000184
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 Callanan816cb3e2014-10-16 23:15:22 +0000188 # Add the test case to the globals, and hide InlineTest
189 __globals.update({test_name : test})
Sean Callanan816cb3e2014-10-16 23:15:22 +0000190
191