Files
clang-p2996/lldb/test/API/commands/trace/TestTraceStartStop.py
Walter Erquinigo e17cae076c [trace][intel pt] Fix per-psb packet decoding
The per-PSB packet decoding logic was wrong because it was assuming that pt_insn_get_sync_offset was being udpated after every PSB. Silly me, that is not true. It returns the offset of the PSB packet after invoking pt_insn_sync_forward regardless of how many PSBs are visited later. Instead, I'm now following the approach described in https://github.com/intel/libipt/blob/master/doc/howto_libipt.md#parallel-decode for parallel decoding, which is basically what we need.

A nasty error that happened because of this is that when we had two PSBs (A and B), the following was happening

1. PSB A was processed all the way up to the end of the trace, which includes PSB B.
2. PSB B was then processed until the end of the trace.

The instructions emitted by step 2. were also emitted as part of step 1. so our trace had duplicated chunks. This problem becomes worse when you many PSBs.

As part of making sure this diff is correct, I added some other features that are very useful.

- Added a "synchronization point" event to the TraceCursor, so we can inspect when PSBs are emitted.
- Removed the single-thread decoder. Now the per-cpu decoder and single-thread decoder use the same code paths.
- Use the query decoder to fetch PSBs and timestamps. It turns out that the pt_insn_sync_forward of the instruction decoder can move past several PSBs (this means that we could skip some TSCs). On the other hand, the pt_query_sync_forward method doesn't skip PSBs, so we can get more accurate sync events and timing information.
- Turned LibiptDecoder into PSBBlockDecoder, which decodes single PSB blocks. It is the fundamental processing unit for decoding.
- Added many comments, asserts and improved error handling for clarity.
- Improved DecodeSystemWideTraceForThread so that a TSC is emitted always before a cpu change event. This was a bug that was annoying me before.
- SplitTraceInContinuousExecutions and FindLowestTSCInTrace are now using the query decoder, which can identify precisely each PSB along with their TSCs.
- Added an "only-events" option to the trace dumper to inspect only events.

I did extensive testing and I think we should have an in-house testing CI. The LLVM buildbots are not capable of supporting testing post-mortem traces of hundreds of megabytes. I'll leave that for later, but at least for now the current tests were able to catch most of the issues I encountered when doing this task.

A sample output of a program that I was single stepping is the following. You can see that only one PSB is emitted even though stepping happened!

```
thread #1: tid = 3578223
    0: (event) trace synchronization point [offset = 0x0xef0]
  a.out`main + 20 at main.cpp:29:20
    1: 0x0000000000402479    leaq   -0x1210(%rbp), %rax
    2: (event) software disabled tracing
    3: 0x0000000000402480    movq   %rax, %rdi
    4: (event) software disabled tracing
    5: (event) software disabled tracing
    6: 0x0000000000402483    callq  0x403bd4                  ; std::vector<int, std::allocator<int>>::vector at stl_vector.h:391:7
    7: (event) software disabled tracing
  a.out`std::vector<int, std::allocator<int>>::vector() at stl_vector.h:391:7
    8: 0x0000000000403bd4    pushq  %rbp
    9: (event) software disabled tracing
    10: 0x0000000000403bd5    movq   %rsp, %rbp
    11: (event) software disabled tracing
```

This is another trace of a long program with a few PSBs.
```
(lldb) thread trace dump instructions -E -f                                                                                                         thread #1: tid = 3603082
    0: (event) trace synchronization point [offset = 0x0x80]
    47417: (event) software disabled tracing
    129231: (event) trace synchronization point [offset = 0x0x800]
    146747: (event) software disabled tracing
    246076: (event) software disabled tracing
    259068: (event) trace synchronization point [offset = 0x0xf78]
    259276: (event) software disabled tracing
    259278: (event) software disabled tracing
    no more data
```

Differential Revision: https://reviews.llvm.org/D131630
2022-08-12 15:13:48 -07:00

236 lines
9.3 KiB
Python

import lldb
from intelpt_testcase import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil
from lldbsuite.test.decorators import *
class TestTraceStartStop(TraceIntelPTTestCaseBase):
def expectGenericHelpMessageForStartCommand(self):
self.expect("help thread trace start",
substrs=["Syntax: thread trace start [<trace-options>]"])
@testSBAPIAndCommands
def testStartStopSessionFileThreads(self):
# it should fail for processes from json session files
self.expect("trace load -v " + os.path.join(self.getSourceDir(), "intelpt-trace", "trace.json"))
# the help command should be the generic one, as it's not a live process
self.expectGenericHelpMessageForStartCommand()
self.traceStartThread(error=True)
self.traceStopThread(error=True)
@testSBAPIAndCommands
def testStartWithNoProcess(self):
self.traceStartThread(error=True)
@testSBAPIAndCommands
def testStartSessionWithWrongSize(self):
self.expect("file " + os.path.join(self.getSourceDir(), "intelpt-trace", "a.out"))
self.expect("b main")
self.expect("r")
self.traceStartThread(
error=True, iptTraceSize=2000,
substrs=["The intel pt trace size must be a power of 2", "It was 2000"])
self.traceStartThread(
error=True, iptTraceSize=5000,
substrs=["The intel pt trace size must be a power of 2", "It was 5000"])
self.traceStartThread(
error=True, iptTraceSize=0,
substrs=["The intel pt trace size must be a power of 2", "It was 0"])
self.traceStartThread(iptTraceSize=1048576)
@testSBAPIAndCommands
def testStartSessionWithSizeDeclarationInUnits(self):
self.expect("file " + os.path.join(self.getSourceDir(), "intelpt-trace", "a.out"))
self.expect("b main")
self.expect("r")
self.traceStartThread(
error=True, iptTraceSize="abc",
substrs=["invalid bytes expression for 'abc'"])
self.traceStartThread(
error=True, iptTraceSize="123.12",
substrs=["invalid bytes expression for '123.12'"])
self.traceStartThread(
error=True, iptTraceSize="\"\"",
substrs=["invalid bytes expression for ''"])
self.traceStartThread(
error=True, iptTraceSize="2000B",
substrs=["The intel pt trace size must be a power of 2 greater than or equal to 4096 (2^12) bytes. It was 2000"])
self.traceStartThread(
error=True, iptTraceSize="3MB",
substrs=["The intel pt trace size must be a power of 2 greater than or equal to 4096 (2^12) bytes. It was 3145728"])
self.traceStartThread(
error=True, iptTraceSize="3MiB",
substrs=["The intel pt trace size must be a power of 2 greater than or equal to 4096 (2^12) bytes. It was 3145728"])
self.traceStartThread(
error=True, iptTraceSize="3mib",
substrs=["The intel pt trace size must be a power of 2 greater than or equal to 4096 (2^12) bytes. It was 3145728"])
self.traceStartThread(
error=True, iptTraceSize="3M",
substrs=["The intel pt trace size must be a power of 2 greater than or equal to 4096 (2^12) bytes. It was 3145728"])
self.traceStartThread(
error=True, iptTraceSize="3KB",
substrs=["The intel pt trace size must be a power of 2 greater than or equal to 4096 (2^12) bytes. It was 3072"])
self.traceStartThread(
error=True, iptTraceSize="3KiB",
substrs=["The intel pt trace size must be a power of 2 greater than or equal to 4096 (2^12) bytes. It was 3072"])
self.traceStartThread(
error=True, iptTraceSize="3K",
substrs=["The intel pt trace size must be a power of 2 greater than or equal to 4096 (2^12) bytes. It was 3072"])
self.traceStartThread(
error=True, iptTraceSize="3MS",
substrs=["invalid bytes expression for '3MS'"])
self.traceStartThread(iptTraceSize="1048576")
@skipIf(oslist=no_match(['linux']), archs=no_match(['i386', 'x86_64']))
def testSBAPIHelp(self):
self.expect("file " + os.path.join(self.getSourceDir(), "intelpt-trace", "a.out"))
self.expect("b main")
self.expect("r")
help = self.getTraceOrCreate().GetStartConfigurationHelp()
self.assertIn("iptTraceSize", help)
self.assertIn("processBufferSizeLimit", help)
@skipIf(oslist=no_match(['linux']), archs=no_match(['i386', 'x86_64']))
def testStoppingAThread(self):
self.expect("file " + os.path.join(self.getSourceDir(), "intelpt-trace", "a.out"))
self.expect("b main")
self.expect("r")
self.expect("thread trace start")
self.expect("n")
self.expect("thread trace dump instructions", substrs=["""0x0000000000400511 movl $0x0, -0x4(%rbp)
no more data"""])
# process stopping should stop the thread
self.expect("process trace stop")
self.expect("n")
self.expect("thread trace dump instructions", substrs=["not traced"], error=True)
@skipIf(oslist=no_match(['linux']), archs=no_match(['i386', 'x86_64']))
def testStartStopLiveThreads(self):
# The help command should be the generic one if there's no process running
self.expectGenericHelpMessageForStartCommand()
self.expect("thread trace start", error=True,
substrs=["error: Process not available"])
self.expect("file " + os.path.join(self.getSourceDir(), "intelpt-trace", "a.out"))
self.expect("b main")
self.expect("thread trace start", error=True,
substrs=["error: Process not available"])
# The help command should be the generic one if there's still no process running
self.expectGenericHelpMessageForStartCommand()
self.expect("r")
# This fails because "trace start" hasn't been called yet
self.expect("thread trace stop", error=True,
substrs=["error: Process is not being traced"])
# the help command should be the intel-pt one now
self.expect("help thread trace start",
substrs=["Start tracing one or more threads with intel-pt.",
"Syntax: thread trace start [<thread-index> <thread-index> ...] [<intel-pt-options>]"])
# We start tracing with a small buffer size
self.expect("thread trace start 1 --size 4096")
# We fail if we try to trace again
self.expect("thread trace start", error=True,
substrs=["error: Thread ", "already traced"])
# We can reconstruct the single instruction executed in the first line
self.expect("n")
self.expect("thread trace dump instructions -f",
patterns=[f'''thread #1: tid = .*
a.out`main \+ 4 at main.cpp:2
1: {ADDRESS_REGEX} movl'''])
# We can reconstruct the instructions up to the second line
self.expect("n")
self.expect("thread trace dump instructions -f",
patterns=[f'''thread #1: tid = .*
a.out`main \+ 4 at main.cpp:2
1: {ADDRESS_REGEX} movl .*
a.out`main \+ 11 at main.cpp:4
3: {ADDRESS_REGEX} movl .*
5: {ADDRESS_REGEX} jmp .* ; <\+28> at main.cpp:4
7: {ADDRESS_REGEX} cmpl .*
9: {ADDRESS_REGEX} jle .* ; <\+20> at main.cpp:5'''])
self.expect("thread trace dump instructions",
patterns=[f'''thread #1: tid = .*
a.out`main \+ 32 at main.cpp:4
9: {ADDRESS_REGEX} jle .* ; <\+20> at main.cpp:5
7: {ADDRESS_REGEX} cmpl .*
5: {ADDRESS_REGEX} jmp .* ; <\+28> at main.cpp:4
3: {ADDRESS_REGEX} movl .*
a.out`main \+ 4 at main.cpp:2
1: {ADDRESS_REGEX} movl .* '''])
# We stop tracing
self.expect("thread trace stop")
# We can't stop twice
self.expect("thread trace stop", error=True,
substrs=["error: Thread ", "not currently traced"])
# We trace again from scratch, this time letting LLDB to pick the current
# thread
self.expect("thread trace start")
self.expect("n")
self.expect("thread trace dump instructions -f",
patterns=[f'''thread #1: tid = .*
a.out`main \+ 20 at main.cpp:5
1: {ADDRESS_REGEX} xorl'''])
self.expect("thread trace dump instructions",
patterns=[f'''thread #1: tid = .*
a.out`main \+ 20 at main.cpp:5
1: {ADDRESS_REGEX} xorl'''])
self.expect("c")
# Now the process has finished, so the commands should fail
self.expect("thread trace start", error=True,
substrs=["error: Process must be launched"])
self.expect("thread trace stop", error=True,
substrs=["error: Process must be launched"])
# We should be able to trace the program if we relaunch it
# For this, we'll trace starting at a different point in the new
# process.
self.expect("breakpoint disable")
self.expect("b main.cpp:4")
self.expect("r")
self.expect("thread trace start")
# We can reconstruct the single instruction executed in the first line
self.expect("si")
self.expect("thread trace dump instructions -c 1",
patterns=[f'''thread #1: tid = .*
a.out`main \+ 11 at main.cpp:4'''])