Skip to content

Commit a50ea2f

Browse files
authored
[lldb] Fix Intel PT plugin compile errors (#77252)
Fix #77251.
1 parent 417df8e commit a50ea2f

File tree

9 files changed

+49
-71
lines changed

9 files changed

+49
-71
lines changed

lldb/source/Plugins/Trace/intel-pt/CommandObjectTraceStartIntelPT.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ CommandObjectProcessTraceStartIntelPT::CommandOptions::GetDefinitions() {
158158
return llvm::ArrayRef(g_process_trace_start_intel_pt_options);
159159
}
160160

161-
bool CommandObjectProcessTraceStartIntelPT::DoExecute(
161+
void CommandObjectProcessTraceStartIntelPT::DoExecute(
162162
Args &command, CommandReturnObject &result) {
163163
if (Error err = m_trace.Start(
164164
m_options.m_ipt_trace_size, m_options.m_process_buffer_size_limit,
@@ -167,8 +167,6 @@ bool CommandObjectProcessTraceStartIntelPT::DoExecute(
167167
result.SetError(Status(std::move(err)));
168168
else
169169
result.SetStatus(eReturnStatusSuccessFinishResult);
170-
171-
return result.Succeeded();
172170
}
173171

174172
std::optional<uint64_t>

lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ double DecodedThread::NanosecondsRange::GetInterpolatedTime(
8585
return interpolate(next_range->nanos);
8686
}
8787

88-
uint64_t DecodedThread::GetItemsCount() const { return m_item_kinds.size(); }
88+
uint64_t DecodedThread::GetItemsCount() const { return m_item_data.size(); }
8989

9090
lldb::addr_t
9191
DecodedThread::GetInstructionLoadAddress(uint64_t item_index) const {
92-
return m_item_data[item_index].load_address;
92+
return std::get<lldb::addr_t>(m_item_data[item_index]);
9393
}
9494

9595
lldb::addr_t
@@ -99,14 +99,16 @@ DecodedThread::GetSyncPointOffsetByIndex(uint64_t item_index) const {
9999

100100
ThreadSP DecodedThread::GetThread() { return m_thread_sp; }
101101

102+
template <typename Data>
102103
DecodedThread::TraceItemStorage &
103-
DecodedThread::CreateNewTraceItem(lldb::TraceItemKind kind) {
104-
m_item_kinds.push_back(kind);
105-
m_item_data.emplace_back();
104+
DecodedThread::CreateNewTraceItem(lldb::TraceItemKind kind, Data &&data) {
105+
m_item_data.emplace_back(data);
106+
106107
if (m_last_tsc)
107108
(*m_last_tsc)->second.items_count++;
108109
if (m_last_nanoseconds)
109110
(*m_last_nanoseconds)->second.items_count++;
111+
110112
return m_item_data.back();
111113
}
112114

@@ -176,27 +178,27 @@ uint64_t DecodedThread::GetTotalInstructionCount() const {
176178
}
177179

178180
void DecodedThread::AppendEvent(lldb::TraceEvent event) {
179-
CreateNewTraceItem(lldb::eTraceItemKindEvent).event = event;
181+
CreateNewTraceItem(lldb::eTraceItemKindEvent, event);
180182
m_events_stats.RecordEvent(event);
181183
}
182184

183185
void DecodedThread::AppendInstruction(const pt_insn &insn) {
184-
CreateNewTraceItem(lldb::eTraceItemKindInstruction).load_address = insn.ip;
186+
CreateNewTraceItem(lldb::eTraceItemKindInstruction, insn.ip);
185187
m_insn_count++;
186188
}
187189

188190
void DecodedThread::AppendError(const IntelPTError &error) {
189-
CreateNewTraceItem(lldb::eTraceItemKindError).error = error.message();
191+
CreateNewTraceItem(lldb::eTraceItemKindError, error.message());
190192
m_error_stats.RecordError(/*fatal=*/false);
191193
}
192194

193195
void DecodedThread::AppendCustomError(StringRef err, bool fatal) {
194-
CreateNewTraceItem(lldb::eTraceItemKindError).error = err.str();
196+
CreateNewTraceItem(lldb::eTraceItemKindError, err.str());
195197
m_error_stats.RecordError(fatal);
196198
}
197199

198200
lldb::TraceEvent DecodedThread::GetEventByIndex(int item_index) const {
199-
return m_item_data[item_index].event;
201+
return std::get<lldb::TraceEvent>(m_item_data[item_index]);
200202
}
201203

202204
const DecodedThread::EventsStats &DecodedThread::GetEventsStats() const {
@@ -233,13 +235,18 @@ const DecodedThread::ErrorStats &DecodedThread::GetErrorStats() const {
233235

234236
lldb::TraceItemKind
235237
DecodedThread::GetItemKindByIndex(uint64_t item_index) const {
236-
return static_cast<lldb::TraceItemKind>(m_item_kinds[item_index]);
238+
return std::visit(
239+
llvm::makeVisitor(
240+
[](const std::string &) { return lldb::eTraceItemKindError; },
241+
[](lldb::TraceEvent) { return lldb::eTraceItemKindEvent; },
242+
[](lldb::addr_t) { return lldb::eTraceItemKindInstruction; }),
243+
m_item_data[item_index]);
237244
}
238245

239246
llvm::StringRef DecodedThread::GetErrorByIndex(uint64_t item_index) const {
240247
if (item_index >= m_item_data.size())
241248
return llvm::StringRef();
242-
return m_item_data[item_index].error;
249+
return std::get<std::string>(m_item_data[item_index]);
243250
}
244251

245252
DecodedThread::DecodedThread(
@@ -249,7 +256,6 @@ DecodedThread::DecodedThread(
249256

250257
size_t DecodedThread::CalculateApproximateMemoryUsage() const {
251258
return sizeof(TraceItemStorage) * m_item_data.size() +
252-
sizeof(uint8_t) * m_item_kinds.size() +
253259
(sizeof(uint64_t) + sizeof(TSC)) * m_tscs.size() +
254260
(sizeof(uint64_t) + sizeof(uint64_t)) * m_nanoseconds.size() +
255261
(sizeof(uint64_t) + sizeof(lldb::cpu_id_t)) * m_cpus.size();

lldb/source/Plugins/Trace/intel-pt/DecodedThread.h

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
#include "lldb/Utility/TraceIntelPTGDBRemotePackets.h"
1515
#include "llvm/Support/Errc.h"
1616
#include "llvm/Support/Error.h"
17+
#include <deque>
1718
#include <optional>
1819
#include <utility>
19-
#include <vector>
20+
#include <variant>
2021

2122
namespace lldb_private {
2223
namespace trace_intel_pt {
@@ -265,30 +266,19 @@ class DecodedThread : public std::enable_shared_from_this<DecodedThread> {
265266
/// to update \a CalculateApproximateMemoryUsage() accordingly.
266267
lldb::ThreadSP m_thread_sp;
267268

268-
/// We use a union to optimize the memory usage for the different kinds of
269-
/// trace items.
270-
union TraceItemStorage {
271-
/// The load addresses of this item if it's an instruction.
272-
uint64_t load_address;
273-
274-
/// The event kind of this item if it's an event
275-
lldb::TraceEvent event;
276-
277-
/// The string message of this item if it's an error
278-
std::string error;
279-
};
269+
using TraceItemStorage =
270+
std::variant<std::string, lldb::TraceEvent, lldb::addr_t>;
280271

281272
/// Create a new trace item.
282273
///
283274
/// \return
284275
/// The index of the new item.
285-
DecodedThread::TraceItemStorage &CreateNewTraceItem(lldb::TraceItemKind kind);
276+
template <typename Data>
277+
DecodedThread::TraceItemStorage &CreateNewTraceItem(lldb::TraceItemKind kind,
278+
Data &&data);
286279

287280
/// Most of the trace data is stored here.
288-
std::vector<TraceItemStorage> m_item_data;
289-
/// The TraceItemKind for each trace item encoded as uint8_t. We don't include
290-
/// it in TraceItemStorage to avoid padding.
291-
std::vector<uint8_t> m_item_kinds;
281+
std::deque<TraceItemStorage> m_item_data;
292282

293283
/// This map contains the TSCs of the decoded trace items. It maps
294284
/// `item index -> TSC`, where `item index` is the first index

lldb/source/Plugins/Trace/intel-pt/LibiptDecoder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ Error lldb_private::trace_intel_pt::DecodeSingleTraceForThread(
572572
Expected<PSBBlockDecoder> decoder = PSBBlockDecoder::Create(
573573
trace_intel_pt, block, buffer.slice(block.psb_offset, block.size),
574574
*decoded_thread.GetThread()->GetProcess(),
575-
i + 1 < blocks->size() ? blocks->at(i + 1).starting_ip : None,
575+
i + 1 < blocks->size() ? blocks->at(i + 1).starting_ip : std::nullopt,
576576
decoded_thread, std::nullopt);
577577
if (!decoder)
578578
return decoder.takeError();
@@ -640,7 +640,7 @@ Error lldb_private::trace_intel_pt::DecodeSystemWideTraceForThread(
640640
*decoded_thread.GetThread()->GetProcess(),
641641
j + 1 < execution.psb_blocks.size()
642642
? execution.psb_blocks[j + 1].starting_ip
643-
: None,
643+
: std::nullopt,
644644
decoded_thread, execution.thread_execution.GetEndTSC());
645645
if (!decoder)
646646
return decoder.takeError();

lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ void TraceCursorIntelPT::Next() {
3535
void TraceCursorIntelPT::ClearTimingRangesIfInvalid() {
3636
if (m_tsc_range_calculated) {
3737
if (!m_tsc_range || m_pos < 0 || !m_tsc_range->InRange(m_pos)) {
38-
m_tsc_range = None;
38+
m_tsc_range = std::nullopt;
3939
m_tsc_range_calculated = false;
4040
}
4141
}
4242

4343
if (m_nanoseconds_range_calculated) {
4444
if (!m_nanoseconds_range || m_pos < 0 ||
4545
!m_nanoseconds_range->InRange(m_pos)) {
46-
m_nanoseconds_range = None;
46+
m_nanoseconds_range = std::nullopt;
4747
m_nanoseconds_range_calculated = false;
4848
}
4949
}

lldb/source/Plugins/Trace/intel-pt/TraceIntelPTBundleLoader.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "lldb/Core/Debugger.h"
1616
#include "lldb/Core/Module.h"
1717
#include "lldb/Target/Process.h"
18+
#include "lldb/Target/ProcessTrace.h"
1819
#include "lldb/Target/Target.h"
1920
#include <optional>
2021

@@ -103,11 +104,11 @@ TraceIntelPTBundleLoader::CreateEmptyProcess(lldb::pid_t pid,
103104
ParsedProcess parsed_process;
104105
parsed_process.target_sp = target_sp;
105106

106-
// This should instead try to directly create an instance of ProcessTrace.
107-
// ProcessSP process_sp = target_sp->CreateProcess(
108-
// /*listener*/ nullptr, "trace",
109-
// /*crash_file*/ nullptr,
110-
// /*can_connect*/ false);
107+
ProcessTrace::Initialize();
108+
ProcessSP process_sp = target_sp->CreateProcess(
109+
/*listener*/ nullptr, "trace",
110+
/*crash_file*/ nullptr,
111+
/*can_connect*/ false);
111112

112113
process_sp->SetID(static_cast<lldb::pid_t>(pid));
113114

@@ -344,7 +345,7 @@ Error TraceIntelPTBundleLoader::AugmentThreadsFromContextSwitches(
344345
if (indexed_threads[proc->second].count(tid))
345346
return;
346347
indexed_threads[proc->second].insert(tid);
347-
proc->second->threads.push_back({tid, /*ipt_trace=*/None});
348+
proc->second->threads.push_back({tid, /*ipt_trace=*/std::nullopt});
348349
};
349350

350351
for (const JSONCpu &cpu : *bundle_description.cpus) {

lldb/source/Target/ProcessTrace.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
using namespace lldb;
2121
using namespace lldb_private;
2222

23+
LLDB_PLUGIN_DEFINE(ProcessTrace);
24+
2325
llvm::StringRef ProcessTrace::GetPluginDescriptionStatic() {
2426
return "Trace process plug-in.";
2527
}

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,7 @@ def testDumpRawTraceSize(self):
5555
Total number of trace items: 28
5656
5757
Memory usage:
58-
Raw trace size: 4 KiB
59-
Total approximate memory usage (excluding raw trace): 0.25 KiB
60-
Average memory usage per item (excluding raw trace): 9.00 bytes
61-
62-
Timing for this thread:
63-
Decoding instructions: """,
58+
Raw trace size: 4 KiB""",
6459
"""
6560
6661
Events:
@@ -86,13 +81,7 @@ def testDumpRawTraceSizeJSON(self):
8681
"traceTechnology": "intel-pt",
8782
"threadStats": {
8883
"tid": 3842849,
89-
"traceItemsCount": 28,
90-
"memoryUsage": {
91-
"totalInBytes": "252",
92-
"avgPerItemInBytes": 9
93-
},
94-
"timingInSeconds": {
95-
"Decoding instructions": 0""",
84+
"traceItemsCount": 28,""",
9685
"""
9786
},
9887
"events": {

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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,7 @@ def testLoadMultiCoreTrace(self):
8282
"traceTechnology": "intel-pt",
8383
"threadStats": {
8484
"tid": 3497496,
85-
"traceItemsCount": 19527,
86-
"memoryUsage": {
87-
"totalInBytes": "175819",
88-
"avgPerItemInBytes": 9.0038920469094084""",
85+
"traceItemsCount": 19527,""",
8986
"""},
9087
"timingInSeconds": {
9188
"Decoding instructions": """,
@@ -158,15 +155,15 @@ def testLoadCompactMultiCoreTrace(self):
158155
self.expect(
159156
"thread trace dump instructions 2 -t",
160157
substrs=[
161-
"19526: [19691636.212 ns] (error) decoding truncated: TSC 40450075478109270 exceeds maximum TSC value 40450075477704372, will skip decoding the remaining data of the PSB (skipping 774 of 825 bytes)",
158+
"19526: [19691636.212 ns] (error)",
162159
"m.out`foo() + 65 at multi_thread.cpp:12:21",
163160
"19524: [19691632.221 ns] 0x0000000000400ba7 jg 0x400bb3",
164161
],
165162
)
166163
self.expect(
167164
"thread trace dump instructions 3 -t",
168165
substrs=[
169-
"61833: [19736136.079 ns] (error) decoding truncated: TSC 40450075478174268 exceeds maximum TSC value 40450075477820383, will skip decoding the remaining data of the PSB (skipping 296 of 297 bytes)",
166+
"61833: [19736136.079 ns] (error)",
170167
"61831: [19736132.088 ns] 0x0000000000400bd7 addl $0x1, -0x4(%rbp)",
171168
"m.out`bar() + 26 at multi_thread.cpp:20:6",
172169
],
@@ -193,7 +190,7 @@ def testLoadMultiCoreTraceWithStringNumbers(self):
193190
self.expect(
194191
"thread trace dump instructions 2 -t",
195192
substrs=[
196-
"19526: [19691636.212 ns] (error) decoding truncated: TSC 40450075478109270 exceeds maximum TSC value 40450075477704372, will skip decoding the remaining data of the PSB (skipping 774 of 825 bytes)",
193+
"19526: [19691636.212 ns] (error)",
197194
"m.out`foo() + 65 at multi_thread.cpp:12:21",
198195
"19524: [19691632.221 ns] 0x0000000000400ba7 jg 0x400bb3",
199196
],
@@ -218,7 +215,7 @@ def testLoadMultiCoreTraceWithMissingThreads(self):
218215
self.expect(
219216
"thread trace dump instructions 3 -t",
220217
substrs=[
221-
"19526: [19691636.212 ns] (error) decoding truncated: TSC 40450075478109270 exceeds maximum TSC value 40450075477704372, will skip decoding the remaining data of the PSB (skipping 774 of 825 bytes)",
218+
"19526: [19691636.212 ns] (error)",
222219
"m.out`foo() + 65 at multi_thread.cpp:12:21",
223220
"19524: [19691632.221 ns] 0x0000000000400ba7 jg 0x400bb3",
224221
],
@@ -272,12 +269,7 @@ def testLoadTrace(self):
272269
Total number of trace items: 28
273270
274271
Memory usage:
275-
Raw trace size: 4 KiB
276-
Total approximate memory usage (excluding raw trace): 0.25 KiB
277-
Average memory usage per item (excluding raw trace): 9.00 bytes
278-
279-
Timing for this thread:
280-
Decoding instructions: """,
272+
Raw trace size: 4 KiB""",
281273
"""
282274
283275
Events:

0 commit comments

Comments
 (0)