1from __future__ import print_function
2
3import os
4import lldb
5import time
6
7from lldbsuite.test.decorators import *
8from lldbsuite.test.lldbtest import *
9from lldbsuite.test import lldbutil
10from lldbsuite.test import configuration
11
12
13class TestIntelPTSimpleBinary(TestBase):
14
15    mydir = TestBase.compute_mydir(__file__)
16    NO_DEBUG_INFO_TESTCASE = True
17
18    def setUp(self):
19        TestBase.setUp(self)
20
21        if 'intel-pt' not in configuration.enabled_plugins:
22            self.skipTest("The intel-pt test plugin is not enabled")
23
24        plugin_path = os.path.join(os.environ["LLDB_IMPLIB_DIR"], "liblldbIntelFeatures.so")
25        self.runCmd("plugin load " + plugin_path)
26
27    @skipIf(oslist=no_match(['linux']))
28    @skipIf(archs=no_match(['i386', 'x86_64']))
29    @skipIfRemote
30    def test_basic_flow(self):
31        """Test collection, decoding, and dumping instructions"""
32
33        self.build()
34        exe = self.getBuildArtifact("a.out")
35        lldbutil.run_to_name_breakpoint(self, "main", exe_name=exe)
36        # We start tracing from main
37        self.runCmd("processor-trace start all")
38
39        # We check the trace after the for loop
40        self.runCmd("b " + str(line_number('main.cpp', '// Break 1')))
41        self.runCmd("c")
42
43        # We wait a little bit to ensure the processor has send the PT packets to
44        # the memory
45        time.sleep(.1)
46
47        # We find the start address of the 'fun' function for a later check
48        target = self.dbg.GetSelectedTarget()
49        fun_start_adddress = target.FindFunctions("fun")[0].GetSymbol() \
50            .GetStartAddress().GetLoadAddress(target)
51
52        # We print the last instructions
53        self.expect("processor-trace show-instr-log -c 100",
54            patterns=[
55                # We expect to have seen the first instruction of 'fun'
56                hex(fun_start_adddress),
57                # We expect to see the exit condition of the for loop
58                "at main.cpp:" + str(line_number('main.cpp', '// Break for loop'))
59            ])
60
61        self.runCmd("processor-trace stop")
62