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 testLoadTrace(self):
13        src_dir = self.getSourceDir()
14        trace_definition_file = os.path.join(src_dir, "intelpt-trace", "trace.json")
15        self.expect("trace load -v " + trace_definition_file, substrs=["intel-pt"])
16
17        target = self.dbg.GetSelectedTarget()
18        process = target.GetProcess()
19        self.assertEqual(process.GetProcessID(), 1234)
20
21        self.assertEqual(process.GetNumThreads(), 1)
22        self.assertEqual(process.GetThreadAtIndex(0).GetThreadID(), 3842849)
23
24        self.assertEqual(target.GetNumModules(), 1)
25        module = target.GetModuleAtIndex(0)
26        path = module.GetFileSpec()
27        self.assertEqual(path.fullpath, os.path.join(src_dir, "intelpt-trace", "a.out"))
28        self.assertGreater(module.GetNumSections(), 0)
29        self.assertEqual(module.GetSectionAtIndex(0).GetFileAddress(), 0x400000)
30
31        self.assertEqual("6AA9A4E2-6F28-2F33-377D-59FECE874C71-5B41261A", module.GetUUIDString())
32
33        # check that the Process and Thread objects were created correctly
34        self.expect("thread info", substrs=["tid = 3842849"])
35        self.expect("thread list", substrs=["Process 1234 stopped", "tid = 3842849"])
36        self.expect("thread trace dump info", substrs=['''Trace technology: intel-pt
37
38thread #1: tid = 3842849
39  Raw trace size: 4 KiB
40  Total number of instructions: 21
41  Total approximate memory usage: 0.98 KiB
42  Average memory usage per instruction: 48.00 bytes'''])
43
44    def testLoadInvalidTraces(self):
45        src_dir = self.getSourceDir()
46        # We test first an invalid type
47        self.expect("trace load -v " + os.path.join(src_dir, "intelpt-trace", "trace_bad.json"), error=True,
48          substrs=['''error: expected object at traceSession.processes[0]
49
50Context:
51{
52  "processes": [
53    /* error: expected object */
54    123
55  ],
56  "trace": { ... }
57}
58
59Schema:
60{
61  "trace": {
62    "type": "intel-pt",
63    "cpuInfo": {
64      "vendor": "intel" | "unknown",
65      "family": integer,
66      "model": integer,
67      "stepping": integer
68    }
69  },'''])
70
71        # Now we test a missing field in the global session file
72        self.expect("trace load -v " + os.path.join(src_dir, "intelpt-trace", "trace_bad2.json"), error=True,
73            substrs=['error: missing value at traceSession.processes[1].triple', "Context", "Schema"])
74
75        # Now we test a missing field in the intel-pt settings
76        self.expect("trace load -v " + os.path.join(src_dir, "intelpt-trace", "trace_bad4.json"), error=True,
77            substrs=['''error: missing value at traceSession.trace.cpuInfo.family
78
79Context:
80{
81  "processes": [],
82  "trace": {
83    "cpuInfo": /* error: missing value */ {
84      "model": 79,
85      "stepping": 1,
86      "vendor": "intel"
87    },
88    "type": "intel-pt"
89  }
90}''', "Schema"])
91
92        # Now we test an incorrect load address in the intel-pt settings
93        self.expect("trace load -v " + os.path.join(src_dir, "intelpt-trace", "trace_bad5.json"), error=True,
94            substrs=['error: expected numeric string at traceSession.processes[0].modules[0].loadAddress',
95                     '"loadAddress": /* error: expected numeric string */ 400000,', "Schema"])
96
97        # The following wrong schema will have a valid target and an invalid one. In the case of failure,
98        # no targets should be created.
99        self.assertEqual(self.dbg.GetNumTargets(), 0)
100        self.expect("trace load -v " + os.path.join(src_dir, "intelpt-trace", "trace_bad3.json"), error=True,
101            substrs=['error: missing value at traceSession.processes[1].pid'])
102        self.assertEqual(self.dbg.GetNumTargets(), 0)
103