Files
clang-p2996/lldb/packages/Python/lldbsuite/test/tools/intelpt/intelpt_testcase.py
Walter Erquinigo 1f49714d3e [trace][intelpt] Support system-wide tracing [4] - Support per core tracing on lldb-server
This diffs implements per-core tracing on lldb-server. It also includes tests that ensure that tracing can be initiated from the client and that the jLLDBGetState ppacket returns the list of trace buffers per core.

This doesn't include any decoder changes.

Finally, this makes some little changes here and there improving the existing code.

A specific piece of code that can't reliably be tested is when tracing
per core fails due to permissions. In this case we add a
troubleshooting message and this is the manual test:

```
/proc/sys/kernel/perf_event_paranoid set to 1

(lldb) process trace start --per-core-tracing                                         error: perf event syscall failed: Permission denied
 You might need that /proc/sys/kernel/perf_event_paranoid has a value of 0 or -1.
``

Differential Revision: https://reviews.llvm.org/D124858
2022-05-17 12:46:54 -07:00

136 lines
5.3 KiB
Python

from lldbsuite.test.lldbtest import *
import os
import time
import json
ADDRESS_REGEX = '0x[0-9a-fA-F]*'
# Decorator that runs a test with both modes of USE_SB_API.
# It assumes that no tests can be executed in parallel.
def testSBAPIAndCommands(func):
def wrapper(*args, **kwargs):
TraceIntelPTTestCaseBase.USE_SB_API = True
func(*args, **kwargs)
TraceIntelPTTestCaseBase.USE_SB_API = False
func(*args, **kwargs)
return wrapper
# Class that should be used by all python Intel PT tests.
#
# It has a handy check that skips the test if the intel-pt plugin is not enabled.
#
# It also contains many functions that can test both the SB API or the command line version
# of the most important tracing actions.
class TraceIntelPTTestCaseBase(TestBase):
NO_DEBUG_INFO_TESTCASE = True
# If True, the trace test methods will use the SB API, otherwise they'll use raw commands.
USE_SB_API = False
def setUp(self):
TestBase.setUp(self)
if 'intel-pt' not in configuration.enabled_plugins:
self.skipTest("The intel-pt test plugin is not enabled")
def skipIfPerCoreTracingIsNotSupported(self):
def is_supported():
try:
with open("/proc/sys/kernel/perf_event_paranoid", "r") as permissions:
value = int(permissions.readlines()[0])
if value <= 0:
return True
except:
return False
if not is_supported():
self.skipTest("Per core tracing is not supported. You need "
"/proc/sys/kernel/perf_event_paranoid to be 0 or -1.")
def getTraceOrCreate(self):
if not self.target().GetTrace().IsValid():
error = lldb.SBError()
self.target().CreateTrace(error)
return self.target().GetTrace()
def assertSBError(self, sberror, error=False):
if error:
self.assertTrue(sberror.Fail())
else:
self.assertSuccess(sberror)
def createConfiguration(self, traceBufferSize=None,
processBufferSizeLimit=None, enableTsc=False,
psbPeriod=None, perCoreTracing=False):
obj = {}
if processBufferSizeLimit is not None:
obj["processBufferSizeLimit"] = processBufferSizeLimit
if traceBufferSize is not None:
obj["traceBufferSize"] = traceBufferSize
if psbPeriod is not None:
obj["psbPeriod"] = psbPeriod
obj["enableTsc"] = enableTsc
obj["perCoreTracing"] = perCoreTracing
configuration = lldb.SBStructuredData()
configuration.SetFromJSON(json.dumps(obj))
return configuration
def traceStartThread(self, thread=None, error=False, substrs=None,
traceBufferSize=None, enableTsc=False, psbPeriod=None):
if self.USE_SB_API:
trace = self.getTraceOrCreate()
thread = thread if thread is not None else self.thread()
configuration = self.createConfiguration(
traceBufferSize=traceBufferSize, enableTsc=enableTsc,
psbPeriod=psbPeriod)
self.assertSBError(trace.Start(thread, configuration), error)
else:
command = "thread trace start"
if thread is not None:
command += " " + str(thread.GetIndexID())
if traceBufferSize is not None:
command += " -s " + str(traceBufferSize)
if enableTsc:
command += " --tsc"
if psbPeriod is not None:
command += " --psb-period " + str(psbPeriod)
self.expect(command, error=error, substrs=substrs)
def traceStartProcess(self, processBufferSizeLimit=None, error=False,
substrs=None, enableTsc=False, psbPeriod=None,
perCoreTracing=False):
if self.USE_SB_API:
trace = self.getTraceOrCreate()
configuration = self.createConfiguration(
processBufferSizeLimit=processBufferSizeLimit, enableTsc=enableTsc,
psbPeriod=psbPeriod, perCoreTracing=perCoreTracing)
self.assertSBError(trace.Start(configuration), error=error)
else:
command = "process trace start"
if processBufferSizeLimit != None:
command += " -l " + str(processBufferSizeLimit)
if enableTsc:
command += " --tsc"
if psbPeriod is not None:
command += " --psb-period " + str(psbPeriod)
if perCoreTracing:
command += " --per-core-tracing"
self.expect(command, error=error, substrs=substrs)
def traceStopProcess(self):
if self.USE_SB_API:
self.assertSuccess(self.target().GetTrace().Stop())
else:
self.expect("process trace stop")
def traceStopThread(self, thread=None, error=False, substrs=None):
if self.USE_SB_API:
thread = thread if thread is not None else self.thread()
self.assertSBError(self.target().GetTrace().Stop(thread), error)
else:
command = "thread trace stop"
if thread is not None:
command += " " + str(thread.GetIndexID())
self.expect(command, error=error, substrs=substrs)