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