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