1import lldb
2from intelpt_testcase import *
3from lldbsuite.test.lldbtest import *
4from lldbsuite.test import lldbutil
5from lldbsuite.test.decorators import *
6
7class TestTraceLoad(TraceIntelPTTestCaseBase):
8    NO_DEBUG_INFO_TESTCASE = True
9
10    @testSBAPIAndCommands
11    def testLoadMultiCoreTrace(self):
12        src_dir = self.getSourceDir()
13        trace_description_file_path = os.path.join(src_dir, "intelpt-multi-core-trace", "trace.json")
14        self.traceLoad(traceDescriptionFilePath=trace_description_file_path, substrs=["intel-pt"])
15        self.expect("thread trace dump instructions 2 -t",
16          substrs=["19523: [tsc=40450075478109270] (error) expected tracing enabled event",
17                   "m.out`foo() + 65 at multi_thread.cpp:12:21",
18                   "19521: [tsc=40450075477657246] 0x0000000000400ba7    jg     0x400bb3"])
19        self.expect("thread trace dump instructions 3 -t",
20          substrs=["67912: [tsc=40450075477799536] 0x0000000000400bd7    addl   $0x1, -0x4(%rbp)",
21                   "m.out`bar() + 26 at multi_thread.cpp:20:6"])
22
23        self.expect("thread trace dump info --json",
24          substrs=['''{
25  "traceTechnology": "intel-pt",
26  "threadStats": {
27    "tid": 3497234,
28    "traceItemsCount": 0,
29    "memoryUsage": {
30      "totalInBytes": "0",
31      "avgPerItemInBytes": null
32    },
33    "timingInSeconds": {
34      "Decoding instructions": ''', '''
35    },
36    "events": {
37      "totalCount": 0,
38      "individualCounts": {}
39    },
40    "continuousExecutions": 0,
41    "PSBBlocks": 0,
42    "errorItems": {
43      "total": 0,
44      "individualErrors": {}
45    }
46  },
47  "globalStats": {
48    "timingInSeconds": {
49      "Context switch and Intel PT traces correlation": 0
50    },
51    "totalUnattributedPSBBlocks": 0,
52    "totalCountinuosExecutions": 153,
53    "totalPSBBlocks": 5,
54    "totalContinuousExecutions": 153
55  }
56}'''])
57
58        self.expect("thread trace dump info 2 --json",
59          substrs=['''{
60  "traceTechnology": "intel-pt",
61  "threadStats": {
62    "tid": 3497496,
63    "traceItemsCount": 19524,
64    "memoryUsage": {
65      "totalInBytes": "175760",
66      "avgPerItemInBytes": 9.''', '''},
67    "timingInSeconds": {
68      "Decoding instructions": ''', '''
69    },
70    "events": {
71      "totalCount": 2,
72      "individualCounts": {
73        "software disabled tracing": 1,
74        "CPU core changed": 1
75      }
76    },
77    "continuousExecutions": 1,
78    "PSBBlocks": 1,
79    "errorItems": {
80      "total": 0,
81      "individualErrors": {}
82    }
83  },
84  "globalStats": {
85    "timingInSeconds": {
86      "Context switch and Intel PT traces correlation": 0''', '''},
87    "totalUnattributedPSBBlocks": 0,
88    "totalCountinuosExecutions": 153,
89    "totalPSBBlocks": 5,
90    "totalContinuousExecutions": 153
91  }
92}'''])
93
94    @testSBAPIAndCommands
95    def testLoadCompactMultiCoreTrace(self):
96        src_dir = self.getSourceDir()
97        trace_description_file_path = os.path.join(src_dir, "intelpt-multi-core-trace", "trace.json")
98        self.traceLoad(traceDescriptionFilePath=trace_description_file_path, substrs=["intel-pt"])
99
100        self.expect("thread trace dump info 2", substrs=["Total number of continuous executions found: 153"])
101
102        # we'll save the trace in compact format
103        compact_trace_bundle_dir = os.path.join(self.getBuildDir(), "intelpt-multi-core-trace-compact")
104        self.traceSave(compact_trace_bundle_dir, compact=True)
105
106        # we'll delete the previous target and make sure it's trace object is deleted
107        self.dbg.DeleteTarget(self.dbg.GetTargetAtIndex(0))
108        self.expect("thread trace dump instructions 2 -t", substrs=["error: invalid target"], error=True)
109
110        # we'll load the compact trace and make sure it works
111        self.traceLoad(os.path.join(compact_trace_bundle_dir, "trace.json"), substrs=["intel-pt"])
112        self.expect("thread trace dump instructions 2 -t",
113          substrs=["19523: [tsc=40450075478109270] (error) expected tracing enabled event",
114                   "m.out`foo() + 65 at multi_thread.cpp:12:21",
115                   "19521: [tsc=40450075477657246] 0x0000000000400ba7    jg     0x400bb3"])
116        self.expect("thread trace dump instructions 3 -t",
117          substrs=["67912: [tsc=40450075477799536] 0x0000000000400bd7    addl   $0x1, -0x4(%rbp)",
118                   "m.out`bar() + 26 at multi_thread.cpp:20:6"])
119
120        # This reduced the number of continuous executions to look at
121        self.expect("thread trace dump info 2", substrs=["Total number of continuous executions found: 3"])
122
123        # We clean up for the next run of this test
124        self.dbg.DeleteTarget(self.dbg.GetTargetAtIndex(0))
125
126    @testSBAPIAndCommands
127    def testLoadMultiCoreTraceWithStringNumbers(self):
128        src_dir = self.getSourceDir()
129        trace_description_file_path = os.path.join(src_dir, "intelpt-multi-core-trace", "trace_with_string_numbers.json")
130        self.traceLoad(traceDescriptionFilePath=trace_description_file_path, substrs=["intel-pt"])
131        self.expect("thread trace dump instructions 2 -t",
132          substrs=["19523: [tsc=40450075478109270] (error) expected tracing enabled event",
133                   "m.out`foo() + 65 at multi_thread.cpp:12:21",
134                   "19521: [tsc=40450075477657246] 0x0000000000400ba7    jg     0x400bb3"])
135        self.expect("thread trace dump instructions 3 -t",
136          substrs=["67912: [tsc=40450075477799536] 0x0000000000400bd7    addl   $0x1, -0x4(%rbp)",
137                   "m.out`bar() + 26 at multi_thread.cpp:20:6"])
138
139    @testSBAPIAndCommands
140    def testLoadMultiCoreTraceWithMissingThreads(self):
141        src_dir = self.getSourceDir()
142        trace_description_file_path = os.path.join(src_dir, "intelpt-multi-core-trace", "trace_missing_threads.json")
143        self.traceLoad(traceDescriptionFilePath=trace_description_file_path, substrs=["intel-pt"])
144        self.expect("thread trace dump instructions 3 -t",
145          substrs=["19523: [tsc=40450075478109270] (error) expected tracing enabled event",
146                   "m.out`foo() + 65 at multi_thread.cpp:12:21",
147                   "19521: [tsc=40450075477657246] 0x0000000000400ba7    jg     0x400bb3"])
148        self.expect("thread trace dump instructions 2 -t",
149          substrs=["67912: [tsc=40450075477799536] 0x0000000000400bd7    addl   $0x1, -0x4(%rbp)",
150                   "m.out`bar() + 26 at multi_thread.cpp:20:6"])
151
152    @testSBAPIAndCommands
153    def testLoadTrace(self):
154        src_dir = self.getSourceDir()
155        trace_description_file_path = os.path.join(src_dir, "intelpt-trace", "trace.json")
156        self.traceLoad(traceDescriptionFilePath=trace_description_file_path, substrs=["intel-pt"])
157
158        target = self.dbg.GetSelectedTarget()
159        process = target.GetProcess()
160        self.assertEqual(process.GetProcessID(), 1234)
161
162        self.assertEqual(process.GetNumThreads(), 1)
163        self.assertEqual(process.GetThreadAtIndex(0).GetThreadID(), 3842849)
164
165        self.assertEqual(target.GetNumModules(), 1)
166        module = target.GetModuleAtIndex(0)
167        path = module.GetFileSpec()
168        self.assertEqual(path.fullpath, os.path.join(src_dir, "intelpt-trace", "a.out"))
169        self.assertGreater(module.GetNumSections(), 0)
170        self.assertEqual(module.GetSectionAtIndex(0).GetFileAddress(), 0x400000)
171
172        self.assertEqual("6AA9A4E2-6F28-2F33-377D-59FECE874C71-5B41261A", module.GetUUIDString())
173
174        # check that the Process and Thread objects were created correctly
175        self.expect("thread info", substrs=["tid = 3842849"])
176        self.expect("thread list", substrs=["Process 1234 stopped", "tid = 3842849"])
177        self.expect("thread trace dump info", substrs=['''thread #1: tid = 3842849
178
179  Trace technology: intel-pt
180
181  Total number of trace items: 23
182
183  Memory usage:
184    Raw trace size: 4 KiB
185    Total approximate memory usage (excluding raw trace): 0.20 KiB
186    Average memory usage per item (excluding raw trace): 9.00 bytes
187
188  Timing for this thread:
189    Decoding instructions: ''', '''
190
191  Events:
192    Number of individual events: 2
193      software disabled tracing: 2
194
195  Errors:
196    Number of TSC decoding errors: 0'''])
197
198    @testSBAPIAndCommands
199    def testLoadInvalidTraces(self):
200        src_dir = self.getSourceDir()
201
202        # We test first an invalid type
203        trace_description_file_path = os.path.join(src_dir, "intelpt-trace", "trace_bad.json")
204        expected_substrs = ['''error: expected object at traceBundle.processes[0]
205
206Context:
207{
208  "cpuInfo": { ... },
209  "processes": [
210    /* error: expected object */
211    123
212  ],
213  "type": "intel-pt"
214}
215
216Schema:
217{
218  "type": "intel-pt",
219  "cpuInfo": {
220    // CPU information gotten from, for example, /proc/cpuinfo.
221
222    "vendor": "GenuineIntel" | "unknown",
223    "family": integer,
224    "model": integer,
225    "stepping": integer
226  },''']
227        self.traceLoad(traceDescriptionFilePath=trace_description_file_path, error=True, substrs=expected_substrs)
228
229
230        # Now we test a wrong cpu family field in the global bundle description file
231        trace_description_file_path = os.path.join(src_dir, "intelpt-trace", "trace_bad2.json")
232        expected_substrs = ['error: expected uint64_t at traceBundle.cpuInfo.family', "Context", "Schema"]
233        self.traceLoad(traceDescriptionFilePath=trace_description_file_path, error=True, substrs=expected_substrs)
234
235
236        # Now we test a missing field in the intel-pt settings
237        trace_description_file_path = os.path.join(src_dir, "intelpt-trace", "trace_bad4.json")
238        expected_substrs = ['''error: missing value at traceBundle.cpuInfo.family
239
240Context:
241{
242  "cpuInfo": /* error: missing value */ {
243    "model": 79,
244    "stepping": 1,
245    "vendor": "GenuineIntel"
246  },
247  "processes": [],
248  "type": "intel-pt"
249}''', "Schema"]
250        self.traceLoad(traceDescriptionFilePath=trace_description_file_path, error=True, substrs=expected_substrs)
251
252
253        # Now we test an incorrect load address in the intel-pt settings
254        trace_description_file_path = os.path.join(src_dir, "intelpt-trace", "trace_bad5.json")
255        expected_substrs = ['error: missing value at traceBundle.processes[1].pid', "Schema"]
256        self.traceLoad(traceDescriptionFilePath=trace_description_file_path, error=True, substrs=expected_substrs)
257
258
259        # The following wrong schema will have a valid target and an invalid one. In the case of failure,
260        # no targets should be created.
261        self.assertEqual(self.dbg.GetNumTargets(), 0)
262        trace_description_file_path = os.path.join(src_dir, "intelpt-trace", "trace_bad3.json")
263        expected_substrs = ['error: missing value at traceBundle.processes[1].pid']
264        self.traceLoad(traceDescriptionFilePath=trace_description_file_path, error=True, substrs=expected_substrs)
265        self.assertEqual(self.dbg.GetNumTargets(), 0)
266