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