xref: /llvm-project/lldb/test/API/commands/trace/TestTraceEvents.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1import lldb
2from intelpt_testcase import *
3from lldbsuite.test.lldbtest import *
4from lldbsuite.test import lldbutil
5from lldbsuite.test.decorators import *
6
7
8class TestTraceEvents(TraceIntelPTTestCaseBase):
9    @testSBAPIAndCommands
10    def testCPUEvents(self):
11        trace_description_file_path = os.path.join(
12            self.getSourceDir(),
13            "intelpt-multi-core-trace",
14            "trace_missing_threads.json",
15        )
16        self.traceLoad(
17            traceDescriptionFilePath=trace_description_file_path, substrs=["intel-pt"]
18        )
19
20        self.expect(
21            "thread trace dump instructions 3 -e --forward -c 5",
22            substrs=[
23                """thread #3: tid = 3497496
24    0: (event) HW clock tick [40450075477621505]
25    1: (event) CPU core changed [new CPU=51]
26    2: (event) HW clock tick [40450075477657246]
27    3: (event) trace synchronization point [offset = 0x0x1331]
28  m.out`foo() + 65 at multi_thread.cpp:12:21"""
29            ],
30        )
31
32        self.expect(
33            "thread trace dump instructions 3 -e --forward -c 5 -J",
34            substrs=[
35                """{
36    "id": 0,
37    "event": "HW clock tick",
38    "hwClock": 40450075477621505
39  },
40  {
41    "id": 1,
42    "event": "CPU core changed",
43    "cpuId": 51
44  }"""
45            ],
46        )
47
48    @testSBAPIAndCommands
49    def testPauseEvents(self):
50        """
51        Everytime the target stops running on the CPU, a 'disabled' event will
52        be emitted, which is represented by the TraceCursor API as a 'paused'
53        event.
54        """
55        self.expect(
56            "target create "
57            + os.path.join(self.getSourceDir(), "intelpt-trace-multi-file", "a.out")
58        )
59        self.expect("b 12")
60        self.expect("r")
61        self.traceStartThread()
62        self.expect("n")
63        self.expect("n")
64        self.expect("si")
65        self.expect("si")
66        self.expect("si")
67        # We ensure that the paused events are printed correctly forward
68        self.expect(
69            "thread trace dump instructions -e -f",
70            patterns=[
71                f"""thread #1: tid = .*
72    0: \(event\) trace synchronization point \[offset \= 0x0xec0\]
73    1: \(event\) hardware disabled tracing
74  a.out`main \+ 23 at main.cpp:12
75    2: {ADDRESS_REGEX}    movl .*
76    3: \(event\) software disabled tracing
77    4: {ADDRESS_REGEX}    addl .*
78    5: {ADDRESS_REGEX}    movl .*
79    6: \(event\) software disabled tracing
80  a.out`main \+ 34 \[inlined\] inline_function\(\) at main.cpp:4
81    7: {ADDRESS_REGEX}    movl .*
82  a.out`main \+ 41 \[inlined\] inline_function\(\) \+ 7 at main.cpp:5
83    8: {ADDRESS_REGEX}    movl .*
84    9: {ADDRESS_REGEX}    addl .*
85    10: {ADDRESS_REGEX}    movl .*
86  a.out`main \+ 52 \[inlined\] inline_function\(\) \+ 18 at main.cpp:6
87    11: {ADDRESS_REGEX}    movl .*
88  a.out`main \+ 55 at main.cpp:14
89    12: {ADDRESS_REGEX}    movl .*
90    13: {ADDRESS_REGEX}    addl .*
91    14: {ADDRESS_REGEX}    movl .*
92    15: \(event\) software disabled tracing
93  a.out`main \+ 63 at main.cpp:16
94    16: {ADDRESS_REGEX}    callq  .* ; symbol stub for: foo\(\)
95    17: \(event\) software disabled tracing
96  a.out`symbol stub for: foo\(\)
97    18: {ADDRESS_REGEX}    jmpq"""
98            ],
99        )
100
101        # We ensure that the paused events are printed correctly backward
102        self.expect(
103            "thread trace dump instructions -e --id 18",
104            patterns=[
105                f"""thread #1: tid = .*
106  a.out`symbol stub for: foo\(\)
107    18: {ADDRESS_REGEX}    jmpq .*
108    17: \(event\) software disabled tracing
109  a.out`main \+ 63 at main.cpp:16
110    16: {ADDRESS_REGEX}    callq  .* ; symbol stub for: foo\(\)
111    15: \(event\) software disabled tracing
112  a.out`main \+ 60 at main.cpp:14
113    14: {ADDRESS_REGEX}    movl .*
114    13: {ADDRESS_REGEX}    addl .*
115    12: {ADDRESS_REGEX}    movl .*
116  a.out`main \+ 52 \[inlined\] inline_function\(\) \+ 18 at main.cpp:6
117    11: {ADDRESS_REGEX}    movl .*
118  a.out`main \+ 49 \[inlined\] inline_function\(\) \+ 15 at main.cpp:5
119    10: {ADDRESS_REGEX}    movl .*
120    9: {ADDRESS_REGEX}    addl .*
121    8: {ADDRESS_REGEX}    movl .*
122  a.out`main \+ 34 \[inlined\] inline_function\(\) at main.cpp:4
123    7: {ADDRESS_REGEX}    movl .*
124    6: \(event\) software disabled tracing
125  a.out`main \+ 31 at main.cpp:12
126    5: {ADDRESS_REGEX}    movl .*
127    4: {ADDRESS_REGEX}    addl .*
128    3: \(event\) software disabled tracing
129    2: {ADDRESS_REGEX}    movl .*
130    1: \(event\) hardware disabled tracing
131    0: \(event\) trace synchronization point \[offset \= 0x0xec0\]"""
132            ],
133        )
134