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