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=["19522: [tsc=40450075478109270] (error) expected tracing enabled event",
17                   "m.out`foo() + 65 at multi_thread.cpp:12:21",
18                   "19520: [tsc=40450075477657246] 0x0000000000400ba7    jg     0x400bb3"])
19        self.expect("thread trace dump instructions 3 -t",
20          substrs=["67911: [tsc=40450075477799536] 0x0000000000400bd7    addl   $0x1, -0x4(%rbp)",
21                   "m.out`bar() + 26 at multi_thread.cpp:20:6"])
22
23    @testSBAPIAndCommands
24    def testLoadCompactMultiCoreTrace(self):
25        src_dir = self.getSourceDir()
26        trace_description_file_path = os.path.join(src_dir, "intelpt-multi-core-trace", "trace.json")
27        self.traceLoad(traceDescriptionFilePath=trace_description_file_path, substrs=["intel-pt"])
28
29        self.expect("thread trace dump info 2", substrs=["Total number of continuous executions found: 153"])
30
31        # we'll save the trace in compact format
32        compact_trace_bundle_dir = os.path.join(self.getBuildDir(), "intelpt-multi-core-trace-compact")
33        self.traceSave(compact_trace_bundle_dir, compact=True)
34
35        # we'll delete the previous target and make sure it's trace object is deleted
36        self.dbg.DeleteTarget(self.dbg.GetTargetAtIndex(0))
37        self.expect("thread trace dump instructions 2 -t", substrs=["error: invalid target"], error=True)
38
39        # we'll load the compact trace and make sure it works
40        self.traceLoad(os.path.join(compact_trace_bundle_dir, "trace.json"), substrs=["intel-pt"])
41        self.expect("thread trace dump instructions 2 -t",
42          substrs=["19522: [tsc=40450075478109270] (error) expected tracing enabled event",
43                   "m.out`foo() + 65 at multi_thread.cpp:12:21",
44                   "19520: [tsc=40450075477657246] 0x0000000000400ba7    jg     0x400bb3"])
45        self.expect("thread trace dump instructions 3 -t",
46          substrs=["67911: [tsc=40450075477799536] 0x0000000000400bd7    addl   $0x1, -0x4(%rbp)",
47                   "m.out`bar() + 26 at multi_thread.cpp:20:6"])
48
49        # This reduced the number of continuous executions to look at
50        self.expect("thread trace dump info 2", substrs=["Total number of continuous executions found: 3"])
51
52        # We clean up for the next run of this test
53        self.dbg.DeleteTarget(self.dbg.GetTargetAtIndex(0))
54
55
56    @testSBAPIAndCommands
57    def testLoadMultiCoreTraceWithStringNumbers(self):
58        src_dir = self.getSourceDir()
59        trace_description_file_path = os.path.join(src_dir, "intelpt-multi-core-trace", "trace_with_string_numbers.json")
60        self.traceLoad(traceDescriptionFilePath=trace_description_file_path, substrs=["intel-pt"])
61        self.expect("thread trace dump instructions 2 -t",
62          substrs=["19522: [tsc=40450075478109270] (error) expected tracing enabled event",
63                   "m.out`foo() + 65 at multi_thread.cpp:12:21",
64                   "19520: [tsc=40450075477657246] 0x0000000000400ba7    jg     0x400bb3"])
65        self.expect("thread trace dump instructions 3 -t",
66          substrs=["67911: [tsc=40450075477799536] 0x0000000000400bd7    addl   $0x1, -0x4(%rbp)",
67                   "m.out`bar() + 26 at multi_thread.cpp:20:6"])
68
69    @testSBAPIAndCommands
70    def testLoadMultiCoreTraceWithMissingThreads(self):
71        src_dir = self.getSourceDir()
72        trace_description_file_path = os.path.join(src_dir, "intelpt-multi-core-trace", "trace_missing_threads.json")
73        self.traceLoad(traceDescriptionFilePath=trace_description_file_path, substrs=["intel-pt"])
74        self.expect("thread trace dump instructions 3 -t",
75          substrs=["19522: [tsc=40450075478109270] (error) expected tracing enabled event",
76                   "m.out`foo() + 65 at multi_thread.cpp:12:21",
77                   "19520: [tsc=40450075477657246] 0x0000000000400ba7    jg     0x400bb3"])
78        self.expect("thread trace dump instructions 2 -t",
79          substrs=["67911: [tsc=40450075477799536] 0x0000000000400bd7    addl   $0x1, -0x4(%rbp)",
80                   "m.out`bar() + 26 at multi_thread.cpp:20:6"])
81
82    @testSBAPIAndCommands
83    def testLoadTrace(self):
84        src_dir = self.getSourceDir()
85        trace_description_file_path = os.path.join(src_dir, "intelpt-trace", "trace.json")
86        self.traceLoad(traceDescriptionFilePath=trace_description_file_path, substrs=["intel-pt"])
87
88        target = self.dbg.GetSelectedTarget()
89        process = target.GetProcess()
90        self.assertEqual(process.GetProcessID(), 1234)
91
92        self.assertEqual(process.GetNumThreads(), 1)
93        self.assertEqual(process.GetThreadAtIndex(0).GetThreadID(), 3842849)
94
95        self.assertEqual(target.GetNumModules(), 1)
96        module = target.GetModuleAtIndex(0)
97        path = module.GetFileSpec()
98        self.assertEqual(path.fullpath, os.path.join(src_dir, "intelpt-trace", "a.out"))
99        self.assertGreater(module.GetNumSections(), 0)
100        self.assertEqual(module.GetSectionAtIndex(0).GetFileAddress(), 0x400000)
101
102        self.assertEqual("6AA9A4E2-6F28-2F33-377D-59FECE874C71-5B41261A", module.GetUUIDString())
103
104        # check that the Process and Thread objects were created correctly
105        self.expect("thread info", substrs=["tid = 3842849"])
106        self.expect("thread list", substrs=["Process 1234 stopped", "tid = 3842849"])
107        self.expect("thread trace dump info", substrs=['''Trace technology: intel-pt
108
109thread #1: tid = 3842849
110  Total number of trace items: 23
111
112  Memory usage:
113    Raw trace size: 4 KiB
114    Total approximate memory usage (excluding raw trace): 0.20 KiB
115    Average memory usage per item (excluding raw trace): 9.00 bytes
116
117  Timing for this thread:
118    Decoding instructions: ''', '''
119
120  Events:
121    Number of individual events: 2
122      software disabled tracing: 2
123
124  Errors:
125    Number of TSC decoding errors: 0'''])
126
127    @testSBAPIAndCommands
128    def testLoadInvalidTraces(self):
129        src_dir = self.getSourceDir()
130
131        # We test first an invalid type
132        trace_description_file_path = os.path.join(src_dir, "intelpt-trace", "trace_bad.json")
133        expected_substrs = ['''error: expected object at traceBundle.processes[0]
134
135Context:
136{
137  "cpuInfo": { ... },
138  "processes": [
139    /* error: expected object */
140    123
141  ],
142  "type": "intel-pt"
143}
144
145Schema:
146{
147  "type": "intel-pt",
148  "cpuInfo": {
149    // CPU information gotten from, for example, /proc/cpuinfo.
150
151    "vendor": "GenuineIntel" | "unknown",
152    "family": integer,
153    "model": integer,
154    "stepping": integer
155  },''']
156        self.traceLoad(traceDescriptionFilePath=trace_description_file_path, error=True, substrs=expected_substrs)
157
158
159        # Now we test a wrong cpu family field in the global bundle description file
160        trace_description_file_path = os.path.join(src_dir, "intelpt-trace", "trace_bad2.json")
161        expected_substrs = ['error: expected uint64_t at traceBundle.cpuInfo.family', "Context", "Schema"]
162        self.traceLoad(traceDescriptionFilePath=trace_description_file_path, error=True, substrs=expected_substrs)
163
164
165        # Now we test a missing field in the intel-pt settings
166        trace_description_file_path = os.path.join(src_dir, "intelpt-trace", "trace_bad4.json")
167        expected_substrs = ['''error: missing value at traceBundle.cpuInfo.family
168
169Context:
170{
171  "cpuInfo": /* error: missing value */ {
172    "model": 79,
173    "stepping": 1,
174    "vendor": "GenuineIntel"
175  },
176  "processes": [],
177  "type": "intel-pt"
178}''', "Schema"]
179        self.traceLoad(traceDescriptionFilePath=trace_description_file_path, error=True, substrs=expected_substrs)
180
181
182        # Now we test an incorrect load address in the intel-pt settings
183        trace_description_file_path = os.path.join(src_dir, "intelpt-trace", "trace_bad5.json")
184        expected_substrs = ['error: missing value at traceBundle.processes[1].pid', "Schema"]
185        self.traceLoad(traceDescriptionFilePath=trace_description_file_path, error=True, substrs=expected_substrs)
186
187
188        # The following wrong schema will have a valid target and an invalid one. In the case of failure,
189        # no targets should be created.
190        self.assertEqual(self.dbg.GetNumTargets(), 0)
191        trace_description_file_path = os.path.join(src_dir, "intelpt-trace", "trace_bad3.json")
192        expected_substrs = ['error: missing value at traceBundle.processes[1].pid']
193        self.traceLoad(traceDescriptionFilePath=trace_description_file_path, error=True, substrs=expected_substrs)
194        self.assertEqual(self.dbg.GetNumTargets(), 0)
195