Skip to content

Commit 782e0ce

Browse files
authored
[lldb] Fix intel trace plugin tests (#133826)
The tests for the [intel-pt](https://github.com/llvm/llvm-project/blob/348374028970c956f2e49ab7553b495d7408ccd9/lldb/docs/use/intel_pt.rst) trace plugin were failing for multiple reasons. On machines where tracing is supported many of the tests were crashing because of a nullptr dereference. It looks like the `core_file` parameter in `ProcessTrace::CreateInstance` was once ignored, but was changed to always being dereferenced. This caused the tests to fail even when tracing was supported. On machines where tracing is not supported we would still run tests that attempt to take a trace. These would obviously fail because the required hardware is not present. Note that some of the tests simply read serialized json as trace files which does not require any special hardware. This PR fixes these two issues by guarding the pointer dereference and then skipping unsupported tests on machines. With these changes the trace tests pass on both types of machines. We also add a new unit test to validate that a process can be created with a nullptr core_file through the generic process trace plugin path.
1 parent 4a73c99 commit 782e0ce

File tree

10 files changed

+83
-2
lines changed

10 files changed

+83
-2
lines changed

lldb/packages/Python/lldbsuite/test/tools/intelpt/intelpt_testcase.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ def wrapper(*args, **kwargs):
1818
return wrapper
1919

2020

21+
def skipIfNoIntelPT(func):
22+
"""Skip tests if the system does not support tracing."""
23+
24+
supported = os.path.exists("/sys/bus/event_source/devices/intel_pt/type")
25+
return unittest.skipIf(not supported, "intel-pt tracing is unsupported")(func)
26+
27+
2128
# Class that should be used by all python Intel PT tests.
2229
#
2330
# It has a handy check that skips the test if the intel-pt plugin is not enabled.

lldb/source/Target/ProcessTrace.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ ProcessSP ProcessTrace::CreateInstance(TargetSP target_sp,
3636
bool can_connect) {
3737
if (can_connect)
3838
return nullptr;
39-
return std::make_shared<ProcessTrace>(target_sp, listener_sp, *crash_file);
39+
return std::make_shared<ProcessTrace>(target_sp, listener_sp,
40+
crash_file ? *crash_file : FileSpec());
4041
}
4142

4243
bool ProcessTrace::CanDebug(TargetSP target_sp, bool plugin_specified_by_name) {

lldb/test/API/commands/trace/TestTraceDumpFunctionCalls.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ def testFunctionCallsWithErrors(self):
133133
],
134134
)
135135

136+
@skipIfNoIntelPT
136137
def testInlineFunctionCalls(self):
137138
self.expect(
138139
"file " + os.path.join(self.getSourceDir(), "inline-function", "a.out")
@@ -194,6 +195,7 @@ def testInlineFunctionCalls(self):
194195
],
195196
)
196197

198+
@skipIfNoIntelPT
197199
def testIncompleteInlineFunctionCalls(self):
198200
self.expect(
199201
"file " + os.path.join(self.getSourceDir(), "inline-function", "a.out")

lldb/test/API/commands/trace/TestTraceEvents.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def testCPUEvents(self):
4545
],
4646
)
4747

48+
@skipIfNoIntelPT
4849
@testSBAPIAndCommands
4950
def testPauseEvents(self):
5051
"""

lldb/test/API/commands/trace/TestTraceSave.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def testErrorMessages(self):
4343
"trace save", substrs=["error: Process is not being traced"], error=True
4444
)
4545

46+
@skipIfNoIntelPT
4647
def testSaveToInvalidDir(self):
4748
self.expect(
4849
"target create "
@@ -165,6 +166,7 @@ def checkSessionBundle(session_file_path):
165166
copied_cpu = find(lambda cor: cor["id"] == cpu["id"], copy["cpus"])
166167
self.assertIsNotNone(copied_cpu)
167168

169+
@skipIfNoIntelPT
168170
def testSaveTrace(self):
169171
self.expect(
170172
"target create "

lldb/test/API/commands/trace/TestTraceStartStop.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from lldbsuite.test.decorators import *
66

77

8+
@skipIfNoIntelPT
89
class TestTraceStartStop(TraceIntelPTTestCaseBase):
910
def expectGenericHelpMessageForStartCommand(self):
1011
self.expect(

lldb/test/API/commands/trace/TestTraceTSC.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from lldbsuite.test.decorators import *
66

77

8+
@skipIfNoIntelPT
89
class TestTraceTimestampCounters(TraceIntelPTTestCaseBase):
910
@testSBAPIAndCommands
1011
@skipIf(oslist=no_match(["linux"]), archs=no_match(["i386", "x86_64"]))

lldb/test/API/commands/trace/multiple-threads/TestTraceStartStopMultipleThreads.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from lldbsuite.test.decorators import *
77

88

9+
@skipIfNoIntelPT
910
class TestTraceStartStopMultipleThreads(TraceIntelPTTestCaseBase):
1011
@skipIf(oslist=no_match(["linux"]), archs=no_match(["i386", "x86_64"]))
1112
@testSBAPIAndCommands

lldb/unittests/Process/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ endif()
77
add_subdirectory(Utility)
88
add_subdirectory(minidump)
99

10-
add_lldb_unittest(ProcessEventDataTests
10+
add_lldb_unittest(ProcessTests
1111
ProcessEventDataTest.cpp
12+
ProcessTraceTest.cpp
1213

1314
LINK_LIBS
1415
lldbCore
@@ -18,5 +19,6 @@ add_lldb_unittest(ProcessEventDataTests
1819
lldbUtility
1920
lldbUtilityHelpers
2021
lldbInterpreter
22+
lldbPluginPlatformLinux
2123
lldbPluginPlatformMacOSX
2224
)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//===-- ProcessEventDataTest.cpp ------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "lldb/Target/ProcessTrace.h"
10+
#include "Plugins/Platform/Linux/PlatformLinux.h"
11+
#include "lldb/Core/Debugger.h"
12+
#include "lldb/Host/HostInfo.h"
13+
#include "gtest/gtest.h"
14+
15+
using namespace lldb_private;
16+
using namespace lldb;
17+
using namespace platform_linux;
18+
19+
// This is needed for the tests that create a trace process.
20+
class ProcessTraceTest : public ::testing::Test {
21+
public:
22+
void SetUp() override {
23+
ProcessTrace::Initialize();
24+
FileSystem::Initialize();
25+
HostInfo::Initialize();
26+
PlatformLinux::Initialize();
27+
}
28+
void TearDown() override {
29+
PlatformLinux::Initialize();
30+
HostInfo::Terminate();
31+
FileSystem::Terminate();
32+
ProcessTrace::Terminate();
33+
}
34+
};
35+
36+
TargetSP CreateTarget(DebuggerSP &debugger_sp, const ArchSpec &arch) {
37+
PlatformSP platform_sp;
38+
TargetSP target_sp;
39+
debugger_sp->GetTargetList().CreateTarget(
40+
*debugger_sp, "", arch, eLoadDependentsNo, platform_sp, target_sp);
41+
return target_sp;
42+
}
43+
44+
// Test that we can create a process trace with a nullptr core file.
45+
TEST_F(ProcessTraceTest, ConstructorWithNullptrCoreFile) {
46+
ArchSpec arch("i386-pc-linux");
47+
48+
Platform::SetHostPlatform(PlatformLinux::CreateInstance(true, &arch));
49+
ASSERT_NE(Platform::GetHostPlatform(), nullptr);
50+
51+
DebuggerSP debugger_sp = Debugger::CreateInstance();
52+
ASSERT_TRUE(debugger_sp);
53+
54+
TargetSP target_sp = CreateTarget(debugger_sp, arch);
55+
ASSERT_TRUE(target_sp);
56+
57+
ProcessSP process_sp = target_sp->CreateProcess(
58+
/*listener*/ nullptr, "trace",
59+
/*crash_file*/ nullptr,
60+
/*can_connect*/ false);
61+
62+
ASSERT_NE(process_sp, nullptr);
63+
}

0 commit comments

Comments
 (0)