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: 5.38 KiB'''])
42
43    def testLoadInvalidTraces(self):
44        src_dir = self.getSourceDir()
45        # We test first an invalid type
46        self.expect("trace load -v " + os.path.join(src_dir, "intelpt-trace", "trace_bad.json"), error=True,
47          substrs=['''error: expected object at traceSession.processes[0]
48
49Context:
50{
51  "processes": [
52    /* error: expected object */
53    123
54  ],
55  "trace": { ... }
56}
57
58Schema:
59{
60  "trace": {
61    "type": "intel-pt",
62    "cpuInfo": {
63      "vendor": "intel" | "unknown",
64      "family": integer,
65      "model": integer,
66      "stepping": integer
67    }
68  },'''])
69
70        # Now we test a missing field in the global session file
71        self.expect("trace load -v " + os.path.join(src_dir, "intelpt-trace", "trace_bad2.json"), error=True,
72            substrs=['error: missing value at traceSession.processes[1].triple', "Context", "Schema"])
73
74        # Now we test a missing field in the intel-pt settings
75        self.expect("trace load -v " + os.path.join(src_dir, "intelpt-trace", "trace_bad4.json"), error=True,
76            substrs=['''error: missing value at traceSession.trace.cpuInfo.family
77
78Context:
79{
80  "processes": [],
81  "trace": {
82    "cpuInfo": /* error: missing value */ {
83      "model": 79,
84      "stepping": 1,
85      "vendor": "intel"
86    },
87    "type": "intel-pt"
88  }
89}''', "Schema"])
90
91        # Now we test an incorrect load address in the intel-pt settings
92        self.expect("trace load -v " + os.path.join(src_dir, "intelpt-trace", "trace_bad5.json"), error=True,
93            substrs=['error: expected numeric string at traceSession.processes[0].modules[0].loadAddress',
94                     '"loadAddress": /* error: expected numeric string */ 400000,', "Schema"])
95
96        # The following wrong schema will have a valid target and an invalid one. In the case of failure,
97        # no targets should be created.
98        self.assertEqual(self.dbg.GetNumTargets(), 0)
99        self.expect("trace load -v " + os.path.join(src_dir, "intelpt-trace", "trace_bad3.json"), error=True,
100            substrs=['error: missing value at traceSession.processes[1].pid'])
101        self.assertEqual(self.dbg.GetNumTargets(), 0)
102