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