llvm-project/lldb/test/API/commands/trace/multiple-threads/TestTraceStartStopMultipleThreads.py
Walter Erquinigo 1f2d49a8e7 [trace][intelpt] Support system-wide tracing [10] - Return warnings and tsc information from lldb-server.
- Add a warnings field in the jLLDBGetState response, for warnings to be delivered to the client for troubleshooting. This removes the need to silently log lldb-server's llvm::Errors and not expose them easily to the user
- Simplify the tscPerfZeroConversion struct and schema. It used to extend a base abstract class, but I'm doubting that we'll ever add other conversion mechanisms because all modern kernels support perf zero. It is also the one who is supposed to work with the timestamps produced by the context switch trace, so expecting it is imperative.
- Force tsc collection for cpu tracing.
- Add a test checking that tscPerfZeroConversion is returned by the GetState request
- Add a pre-check for cpu tracing that makes sure that perf zero values are available.

Differential Revision: https://reviews.llvm.org/D125932
2022-06-15 12:08:00 -07:00

238 lines
8.6 KiB
Python

import lldb
import json
from intelpt_testcase import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil
from lldbsuite.test.decorators import *
class TestTraceStartStopMultipleThreads(TraceIntelPTTestCaseBase):
mydir = TestBase.compute_mydir(__file__)
@skipIf(oslist=no_match(['linux']), archs=no_match(['i386', 'x86_64']))
@testSBAPIAndCommands
def testStartMultipleLiveThreads(self):
self.build()
exe = self.getBuildArtifact("a.out")
self.dbg.CreateTarget(exe)
self.expect("b main")
self.expect("b 6")
self.expect("b 11")
self.expect("r")
self.traceStartProcess()
self.expect("continue")
self.expect("thread trace dump instructions", substrs=['main.cpp:9'])
# We'll see here the second thread
self.expect("continue")
self.expect("thread trace dump instructions", substrs=['main.cpp:4'])
self.traceStopProcess()
@skipIf(oslist=no_match(['linux']), archs=no_match(['i386', 'x86_64']))
@testSBAPIAndCommands
def testStartMultipleLiveThreadsWithStops(self):
self.build()
exe = self.getBuildArtifact("a.out")
self.dbg.CreateTarget(exe)
self.expect("b main")
self.expect("b 6")
self.expect("b 11")
self.expect("r")
self.traceStartProcess()
# We'll see here the first thread
self.expect("continue")
# We are in thread 2
self.expect("thread trace dump instructions", substrs=['main.cpp:9'])
self.expect("thread trace dump instructions 2", substrs=['main.cpp:9'])
# We stop tracing it
self.expect("thread trace stop 2")
# The trace is still in memory
self.expect("thread trace dump instructions 2", substrs=['main.cpp:9'])
# We'll stop at the next breakpoint, thread 2 will be still alive, but not traced. Thread 3 will be traced
self.expect("continue")
self.expect("thread trace dump instructions", substrs=['main.cpp:4'])
self.expect("thread trace dump instructions 3", substrs=['main.cpp:4'])
self.expect("thread trace dump instructions 2", substrs=['not traced'])
self.traceStopProcess()
@skipIf(oslist=no_match(['linux']), archs=no_match(['i386', 'x86_64']))
@testSBAPIAndCommands
def testStartMultipleLiveThreadsWithStops(self):
self.build()
exe = self.getBuildArtifact("a.out")
self.dbg.CreateTarget(exe)
self.expect("b main")
self.expect("b 6")
self.expect("b 11")
self.expect("r")
self.traceStartProcess()
# We'll see here the first thread
self.expect("continue")
# We are in thread 2
self.expect("thread trace dump instructions", substrs=['main.cpp:9'])
self.expect("thread trace dump instructions 2", substrs=['main.cpp:9'])
# We stop tracing all
self.expect("thread trace stop all")
# The trace is still in memory
self.expect("thread trace dump instructions 2", substrs=['main.cpp:9'])
# We'll stop at the next breakpoint in thread 3, thread 2 and 3 will be alive, but only 3 traced.
self.expect("continue")
self.expect("thread trace dump instructions", substrs=['main.cpp:4'])
self.expect("thread trace dump instructions 3", substrs=['main.cpp:4'])
self.expect("thread trace dump instructions 1", substrs=['not traced'])
self.expect("thread trace dump instructions 2", substrs=['not traced'])
self.traceStopProcess()
@skipIf(oslist=no_match(['linux']), archs=no_match(['i386', 'x86_64']))
def testStartMultipleLiveThreadsWithThreadStartAll(self):
self.build()
exe = self.getBuildArtifact("a.out")
target = self.dbg.CreateTarget(exe)
self.expect("b main")
self.expect("b 6")
self.expect("b 11")
self.expect("r")
self.expect("continue")
# We are in thread 2
self.expect("thread trace start all")
# Now we have instructions in thread's 2 trace
self.expect("n")
self.expect("thread trace dump instructions 2", substrs=['main.cpp:11'])
# We stop tracing all
self.runCmd("thread trace stop all")
# The trace is still in memory
self.expect("thread trace dump instructions 2", substrs=['main.cpp:11'])
# We'll stop at the next breakpoint in thread 3, and nothing should be traced
self.expect("continue")
self.expect("thread trace dump instructions 3", substrs=['not traced'])
self.expect("thread trace dump instructions 1", substrs=['not traced'])
self.expect("thread trace dump instructions 2", substrs=['not traced'])
@skipIf(oslist=no_match(['linux']), archs=no_match(['i386', 'x86_64']))
@testSBAPIAndCommands
def testStartMultipleLiveThreadsWithSmallTotalLimit(self):
self.build()
exe = self.getBuildArtifact("a.out")
self.dbg.CreateTarget(exe)
self.expect("b main")
self.expect("r")
# trace the entire process with enough total size for 1 thread trace
self.traceStartProcess(processBufferSizeLimit=5000)
# we get the stop event when trace 2 appears and can't be traced
self.expect("c", substrs=['Thread', "can't be traced"])
# we get the stop event when trace 3 appears and can't be traced
self.expect("c", substrs=['Thread', "can't be traced"])
self.traceStopProcess()
@skipIf(oslist=no_match(['linux']), archs=no_match(['i386', 'x86_64']))
@testSBAPIAndCommands
def testStartPerCoreSession(self):
self.skipIfPerCoreTracingIsNotSupported()
self.build()
exe = self.getBuildArtifact("a.out")
self.dbg.CreateTarget(exe)
self.expect("b main")
self.expect("r")
# We should fail if we hit the total buffer limit. Useful if the number
# of cores is huge.
self.traceStartProcess(error="True", processBufferSizeLimit=100,
perCoreTracing=True,
substrs=["The process can't be traced because the process trace size "
"limit has been reached. Consider retracing with a higher limit."])
self.traceStartProcess(perCoreTracing=True)
self.traceStopProcess()
self.traceStartProcess(perCoreTracing=True)
# We can't support multiple per-core tracing sessions.
self.traceStartProcess(error=True, perCoreTracing=True,
substrs=["Process currently traced. Stop process tracing first"])
# We can't support tracing per thread is per core is enabled.
self.traceStartThread(
error="True",
substrs=["Thread with tid ", "is currently traced"])
# We can't stop individual thread when per core is enabled.
self.traceStopThread(error="True",
substrs=["Can't stop tracing an individual thread when per-core process tracing is enabled"])
# We move forward a little bit to collect some data
self.expect("b 19")
self.expect("c")
# We will assert that the trace state will contain valid context switch and trace buffer entries.
# Besides that, we need to get tsc-to-nanos conversion information.
# We first parse the json response from the custom packet
self.runCmd("""process plugin packet send 'jLLDBTraceGetState:{"type":"intel-pt"}]'""")
response_header = 'response: '
output = None
for line in self.res.GetOutput().splitlines():
if line.find(response_header) != -1:
response = line[line.find(response_header) + len(response_header):].strip()
output = json.loads(response)
self.assertTrue(output is not None)
self.assertIn("cores", output)
self.assertIn("tscPerfZeroConversion", output)
found_non_empty_context_switch = False
for core in output["cores"]:
context_switch_size = None
trace_buffer_size = None
for binary_data in core["binaryData"]:
if binary_data["kind"] == "traceBuffer":
trace_buffer_size = binary_data["size"]
elif binary_data["kind"] == "perfContextSwitchTrace":
context_switch_size = binary_data["size"]
self.assertTrue(context_switch_size is not None)
self.assertTrue(trace_buffer_size is not None)
if context_switch_size > 0:
found_non_empty_context_switch = True
# We must have captured the context switch of when the target resumed
self.assertTrue(found_non_empty_context_switch)
self.traceStopProcess()