xref: /llvm-project/lldb/packages/Python/lldbsuite/test/tools/intelpt/intelpt_testcase.py (revision bf9f21a28be171dc500cc68b4cb1fcd3fc33f229)
1from lldbsuite.test.lldbtest import *
2import os
3import time
4import json
5
6ADDRESS_REGEX = '0x[0-9a-fA-F]*'
7
8# Decorator that runs a test with both modes of USE_SB_API.
9# It assumes that no tests can be executed in parallel.
10def testSBAPIAndCommands(func):
11    def wrapper(*args, **kwargs):
12        TraceIntelPTTestCaseBase.USE_SB_API = True
13        func(*args, **kwargs)
14        TraceIntelPTTestCaseBase.USE_SB_API = False
15        func(*args, **kwargs)
16    return wrapper
17
18# Class that should be used by all python Intel PT tests.
19#
20# It has a handy check that skips the test if the intel-pt plugin is not enabled.
21#
22# It also contains many functions that can test both the SB API or the command line version
23# of the most important tracing actions.
24class TraceIntelPTTestCaseBase(TestBase):
25
26    NO_DEBUG_INFO_TESTCASE = True
27
28    # If True, the trace test methods will use the SB API, otherwise they'll use raw commands.
29    USE_SB_API = False
30
31    def setUp(self):
32        TestBase.setUp(self)
33        if 'intel-pt' not in configuration.enabled_plugins:
34            self.skipTest("The intel-pt test plugin is not enabled")
35
36    def getTraceOrCreate(self):
37        if not self.target().GetTrace().IsValid():
38            error = lldb.SBError()
39            self.target().CreateTrace(error)
40        return self.target().GetTrace()
41
42    def assertSBError(self, sberror, error=False):
43        if error:
44            self.assertTrue(sberror.Fail())
45        else:
46            self.assertSuccess(sberror)
47
48    def createConfiguration(self, threadBufferSize=None, processBufferSizeLimit=None):
49        obj = {}
50        if processBufferSizeLimit is not None:
51            obj["processBufferSizeLimit"] = processBufferSizeLimit
52        if threadBufferSize is not None:
53            obj["threadBufferSize"]  = threadBufferSize
54
55        configuration = lldb.SBStructuredData()
56        configuration.SetFromJSON(json.dumps(obj))
57        return configuration
58
59    def traceStartThread(self, thread=None, error=False, substrs=None, threadBufferSize=None):
60        if self.USE_SB_API:
61            trace = self.getTraceOrCreate()
62            thread = thread if thread is not None else self.thread()
63            configuration = self.createConfiguration(threadBufferSize=threadBufferSize)
64            self.assertSBError(trace.Start(thread, configuration), error)
65        else:
66            command = "thread trace start"
67            if thread is not None:
68                command += " " + str(thread.GetIndexID())
69            if threadBufferSize is not None:
70                command += " -s " + str(threadBufferSize)
71            self.expect(command, error=error, substrs=substrs)
72
73    def traceStartProcess(self, processBufferSizeLimit=None, error=False, substrs=None):
74        if self.USE_SB_API:
75            trace = self.getTraceOrCreate()
76            configuration = self.createConfiguration(processBufferSizeLimit=processBufferSizeLimit)
77            self.assertSBError(trace.Start(configuration), error=error)
78        else:
79            command = "process trace start"
80            if processBufferSizeLimit != None:
81                command += " -l " + str(processBufferSizeLimit)
82            self.expect(command, error=error, substrs=substrs)
83
84    def traceStopProcess(self):
85        if self.USE_SB_API:
86            self.assertSuccess(self.target().GetTrace().Stop())
87        else:
88            self.expect("process trace stop")
89
90    def traceStopThread(self, thread=None, error=False):
91        if self.USE_SB_API:
92            thread = thread if thread is not None else self.thread()
93            self.assertSBError(self.target().GetTrace().Stop(thread), error)
94
95        else:
96            command = "thread trace stop"
97            if thread is not None:
98                command += " " + str(thread.GetIndexID())
99            self.expect(command, error=error)
100