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 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=["19522: [tsc=40450075478109270] (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=["67911: [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=["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 2 -t",
46          substrs=["67911: [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 trace items: 23
78
79  Memory usage:
80    Raw trace size: 4 KiB
81    Total approximate memory usage (excluding raw trace): 0.20 KiB
82    Average memory usage per item (excluding raw trace): 9.00 bytes
83
84  Timing for this thread:
85    Decoding instructions: ''', '''
86
87  Events:
88    Number of individual events: 2
89      software disabled tracing: 2
90
91  Errors:
92    Number of TSC decoding errors: 0'''])
93
94    @testSBAPIAndCommands
95    def testLoadInvalidTraces(self):
96        src_dir = self.getSourceDir()
97
98        # We test first an invalid type
99        trace_description_file_path = os.path.join(src_dir, "intelpt-trace", "trace_bad.json")
100        expected_substrs = ['''error: expected object at traceBundle.processes[0]
101
102Context:
103{
104  "cpuInfo": { ... },
105  "processes": [
106    /* error: expected object */
107    123
108  ],
109  "type": "intel-pt"
110}
111
112Schema:
113{
114  "type": "intel-pt",
115  "cpuInfo": {
116    // CPU information gotten from, for example, /proc/cpuinfo.
117
118    "vendor": "GenuineIntel" | "unknown",
119    "family": integer,
120    "model": integer,
121    "stepping": integer
122  },''']
123        self.traceLoad(traceDescriptionFilePath=trace_description_file_path, error=True, substrs=expected_substrs)
124
125
126        # Now we test a wrong cpu family field in the global bundle description file
127        trace_description_file_path = os.path.join(src_dir, "intelpt-trace", "trace_bad2.json")
128        expected_substrs = ['error: expected uint64_t at traceBundle.cpuInfo.family', "Context", "Schema"]
129        self.traceLoad(traceDescriptionFilePath=trace_description_file_path, error=True, substrs=expected_substrs)
130
131
132        # Now we test a missing field in the intel-pt settings
133        trace_description_file_path = os.path.join(src_dir, "intelpt-trace", "trace_bad4.json")
134        expected_substrs = ['''error: missing value at traceBundle.cpuInfo.family
135
136Context:
137{
138  "cpuInfo": /* error: missing value */ {
139    "model": 79,
140    "stepping": 1,
141    "vendor": "GenuineIntel"
142  },
143  "processes": [],
144  "type": "intel-pt"
145}''', "Schema"]
146        self.traceLoad(traceDescriptionFilePath=trace_description_file_path, error=True, substrs=expected_substrs)
147
148
149        # Now we test an incorrect load address in the intel-pt settings
150        trace_description_file_path = os.path.join(src_dir, "intelpt-trace", "trace_bad5.json")
151        expected_substrs = ['error: missing value at traceBundle.processes[1].pid', "Schema"]
152        self.traceLoad(traceDescriptionFilePath=trace_description_file_path, error=True, substrs=expected_substrs)
153
154
155        # The following wrong schema will have a valid target and an invalid one. In the case of failure,
156        # no targets should be created.
157        self.assertEqual(self.dbg.GetNumTargets(), 0)
158        trace_description_file_path = os.path.join(src_dir, "intelpt-trace", "trace_bad3.json")
159        expected_substrs = ['error: missing value at traceBundle.processes[1].pid']
160        self.traceLoad(traceDescriptionFilePath=trace_description_file_path, error=True, substrs=expected_substrs)
161        self.assertEqual(self.dbg.GetNumTargets(), 0)
162