1import lldb
2from lldbsuite.test.lldbtest import *
3from lldbsuite.test import lldbutil
4from lldbsuite.test.decorators import *
5
6class TestTraceLoad(TestBase):
7
8    mydir = TestBase.compute_mydir(__file__)
9    NO_DEBUG_INFO_TESTCASE = True
10
11    def setUp(self):
12        TestBase.setUp(self)
13        if 'intel-pt' not in configuration.enabled_plugins:
14            self.skipTest("The intel-pt test plugin is not enabled")
15
16
17    def testLoadTrace(self):
18        src_dir = self.getSourceDir()
19        trace_definition_file = os.path.join(src_dir, "intelpt-trace", "trace.json")
20        self.expect("trace load -v " + trace_definition_file, substrs=["intel-pt"])
21
22        target = self.dbg.GetSelectedTarget()
23        process = target.GetProcess()
24        self.assertEqual(process.GetProcessID(), 1234)
25
26        self.assertEqual(process.GetNumThreads(), 1)
27        self.assertEqual(process.GetThreadAtIndex(0).GetThreadID(), 3842849)
28
29        self.assertEqual(target.GetNumModules(), 1)
30        module = target.GetModuleAtIndex(0)
31        path = module.GetFileSpec()
32        self.assertEqual(path.fullpath, os.path.join(src_dir, "intelpt-trace", "a.out"))
33        self.assertGreater(module.GetNumSections(), 0)
34        self.assertEqual(module.GetSectionAtIndex(0).GetFileAddress(), 0x400000)
35
36        self.assertEqual("6AA9A4E2-6F28-2F33-377D-59FECE874C71-5B41261A", module.GetUUIDString())
37
38        # check that the Process and Thread objects were created correctly
39        self.expect("thread info", substrs=["tid = 3842849"])
40        self.expect("thread list", substrs=["Process 1234 stopped", "tid = 3842849"])
41
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