Skip to content

Commit 80b5c3f

Browse files
Jeffrey Tanclayborg
authored andcommitted
Address feedback
1 parent d167028 commit 80b5c3f

File tree

7 files changed

+129
-131
lines changed

7 files changed

+129
-131
lines changed

lldb/source/Plugins/Process/amdgpu-core/ProcessAmdGpuCore.cpp

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,29 +43,42 @@ void ProcessAmdGpuCore::Initialize() {
4343
llvm::call_once(g_once_flag, []() {
4444
// Register as GPU Process plugin (for merged CPU+GPU cores)
4545
// AMD only supports merged mode, not standalone GPU-only cores
46-
ProcessElfGpuCore::RegisterEmbeddedCorePlugin(
46+
ProcessElfEmbeddedCore::RegisterEmbeddedCorePlugin(
4747
GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance);
4848
});
4949
}
5050

5151
void ProcessAmdGpuCore::Terminate() {
52-
ProcessElfGpuCore::UnregisterEmbeddedCorePlugin(
52+
ProcessElfEmbeddedCore::UnregisterEmbeddedCorePlugin(
5353
ProcessAmdGpuCore::CreateInstance);
5454
}
5555

56-
std::shared_ptr<ProcessElfGpuCore> ProcessAmdGpuCore::CreateInstance(
56+
static std::optional<CoreNote>
57+
GetAmdGpuNote(std::shared_ptr<ProcessElfCore> cpu_core_process) {
58+
const auto &notes = cpu_core_process->GetCoreNotes();
59+
auto it = std::find_if(notes.begin(), notes.end(), [](const CoreNote &note) {
60+
return note.info.n_name == "AMDGPU" &&
61+
note.info.n_type == llvm::ELF::NT_AMDGPU_KFD_CORE_STATE;
62+
});
63+
64+
if (it != notes.end()) {
65+
return *it;
66+
}
67+
return std::nullopt;
68+
}
69+
70+
std::shared_ptr<ProcessElfEmbeddedCore> ProcessAmdGpuCore::CreateInstance(
5771
std::shared_ptr<ProcessElfCore> cpu_core_process,
5872
lldb::ListenerSP listener_sp, const FileSpec *crash_file) {
5973
// Check if this core file has AMD GPU notes (type 33 =
6074
// NT_AMDGPU_KFD_CORE_STATE)
61-
if (!cpu_core_process->GetAmdGpuNote().has_value()) {
75+
if (!GetAmdGpuNote(cpu_core_process).has_value()) {
6276
return nullptr;
6377
}
6478
llvm::Expected<lldb::TargetSP> gpu_target_or_err =
65-
CreateGpuTarget(cpu_core_process->GetTarget().GetDebugger());
79+
CreateEmbeddedCoreTarget(cpu_core_process->GetTarget().GetDebugger());
6680
if (!gpu_target_or_err) {
67-
LLDB_LOGF(GetLog(LLDBLog::Process),
68-
"Failed to create GPU target: %s",
81+
LLDB_LOGF(GetLog(LLDBLog::Process), "Failed to create GPU target: %s",
6982
llvm::toString(gpu_target_or_err.takeError()).c_str());
7083
return nullptr;
7184
}
@@ -83,6 +96,15 @@ std::shared_ptr<ProcessElfGpuCore> ProcessAmdGpuCore::CreateInstance(
8396
// Set up the CPU-GPU connection
8497
cpu_core_process->GetTarget().SetGPUPluginTarget(
8598
gpu_process_sp->GetPluginName(), gpu_target_sp);
99+
100+
// Load the GPU core - report errors to user if loading fails
101+
Status error = gpu_process_sp->LoadCore();
102+
if (error.Fail()) {
103+
cpu_core_process->GetTarget().GetDebugger().ReportError(
104+
llvm::formatv("Failed to load AMD GPU core: {0}", error.AsCString()));
105+
return nullptr;
106+
}
107+
86108
return gpu_process_sp;
87109
}
88110

@@ -120,7 +142,7 @@ static amd_dbgapi_status_t amd_dbgapi_client_process_get_info_callback(
120142
amd_dbgapi_core_state_data_t *core_state_data =
121143
(amd_dbgapi_core_state_data_t *)value;
122144
std::optional<CoreNote> amd_gpu_note_opt =
123-
process->GetCpuProcess()->GetAmdGpuNote();
145+
GetAmdGpuNote(process->GetCpuProcess());
124146

125147
if (!amd_gpu_note_opt.has_value())
126148
return AMD_DBGAPI_STATUS_ERROR_NOT_AVAILABLE;

lldb/source/Plugins/Process/amdgpu-core/ProcessAmdGpuCore.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
#ifndef LLDB_SOURCE_PLUGINS_PROCESS_ELF_CORE_PROCESSAMDGPUCORE_H
1717
#define LLDB_SOURCE_PLUGINS_PROCESS_ELF_CORE_PROCESSAMDGPUCORE_H
1818

19-
#include "../elf-core/ProcessElfGpuCore.h"
19+
#include "../elf-core/ProcessElfEmbeddedCore.h"
2020
#include <amd-dbgapi/amd-dbgapi.h>
2121

22-
class ProcessAmdGpuCore : public ProcessElfGpuCore {
22+
class ProcessAmdGpuCore : public ProcessElfEmbeddedCore {
2323
public:
2424
static void Initialize();
2525

@@ -29,7 +29,7 @@ class ProcessAmdGpuCore : public ProcessElfGpuCore {
2929

3030
static llvm::StringRef GetPluginDescriptionStatic();
3131

32-
static std::shared_ptr<ProcessElfGpuCore>
32+
static std::shared_ptr<ProcessElfEmbeddedCore>
3333
CreateInstance(std::shared_ptr<ProcessElfCore> cpu_core_process,
3434
lldb::ListenerSP listener_sp,
3535
const lldb_private::FileSpec *crash_file);
@@ -38,8 +38,8 @@ class ProcessAmdGpuCore : public ProcessElfGpuCore {
3838
ProcessAmdGpuCore(std::shared_ptr<ProcessElfCore> cpu_core_process,
3939
lldb::TargetSP target_sp, lldb::ListenerSP listener_sp,
4040
const lldb_private::FileSpec &core_file)
41-
: ProcessElfGpuCore(target_sp, /*cpu_core_process=*/cpu_core_process,
42-
listener_sp, core_file) {}
41+
: ProcessElfEmbeddedCore(target_sp, /*cpu_core_process=*/cpu_core_process,
42+
listener_sp, core_file) {}
4343

4444
~ProcessAmdGpuCore() override;
4545

@@ -59,8 +59,6 @@ class ProcessAmdGpuCore : public ProcessElfGpuCore {
5959
// PluginInterface protocol
6060
llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
6161

62-
std::optional<lldb_private::CoreNote> GetAmdGpuNote();
63-
6462
protected:
6563
bool DoUpdateThreadList(lldb_private::ThreadList &old_thread_list,
6664
lldb_private::ThreadList &new_thread_list) override;

lldb/source/Plugins/Process/elf-core/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
add_lldb_library(lldbPluginProcessElfCore PLUGIN
22
ProcessElfCore.cpp
3-
ProcessElfGpuCore.cpp
3+
ProcessElfEmbeddedCore.cpp
44
ThreadElfCore.cpp
55
RegisterContextLinuxCore_x86_64.cpp
66
RegisterContextPOSIXCore_arm.cpp

lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
#include "llvm/BinaryFormat/ELF.h"
2929
#include "llvm/Support/Threading.h"
3030

31-
#include "ProcessElfGpuCore.h"
3231
#include "Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.h"
3332
#include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
3433
#include "Plugins/Process/elf-core/RegisterUtilities.h"
3534
#include "ProcessElfCore.h"
35+
#include "ProcessElfEmbeddedCore.h"
3636
#include "ThreadElfCore.h"
3737

3838
using namespace lldb_private;
@@ -273,12 +273,9 @@ Status ProcessElfCore::DoLoadCore() {
273273
}
274274
}
275275
}
276-
llvm::Expected<std::shared_ptr<ProcessElfGpuCore>> gpu_process =
277-
ProcessElfGpuCore::LoadGpuCore(
278-
std::static_pointer_cast<ProcessElfCore>(shared_from_this()),
279-
GetCoreFile());
280-
if (!gpu_process)
281-
return Status::FromError(gpu_process.takeError());
276+
ProcessElfEmbeddedCore::LoadEmbeddedCoreFiles(
277+
std::static_pointer_cast<ProcessElfCore>(shared_from_this()),
278+
GetCoreFile());
282279

283280
return error;
284281
}
@@ -437,7 +434,7 @@ size_t ProcessElfCore::DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
437434
const lldb::addr_t file_start = address_range->data.GetRangeBase();
438435
const lldb::addr_t file_end = address_range->data.GetRangeEnd();
439436
size_t bytes_to_read = size; // Number of bytes to read from the core file
440-
size_t bytes_copied = 0; // Number of bytes actually read from the core file
437+
size_t bytes_copied = 0; // Number of bytes actually read from the core file
441438
lldb::addr_t bytes_left =
442439
0; // Number of bytes available in the core file from the given address
443440

@@ -520,8 +517,7 @@ lldb::addr_t ProcessElfCore::GetImageInfoAddress() {
520517

521518
// Parse a FreeBSD NT_PRSTATUS note - see FreeBSD sys/procfs.h for details.
522519
static void ParseFreeBSDPrStatus(ThreadData &thread_data,
523-
const DataExtractor &data,
524-
bool lp64) {
520+
const DataExtractor &data, bool lp64) {
525521
lldb::offset_t offset = 0;
526522
int pr_version = data.GetU32(&offset);
527523

@@ -548,8 +544,7 @@ static void ParseFreeBSDPrStatus(ThreadData &thread_data,
548544

549545
// Parse a FreeBSD NT_PRPSINFO note - see FreeBSD sys/procfs.h for details.
550546
static void ParseFreeBSDPrPsInfo(ProcessElfCore &process,
551-
const DataExtractor &data,
552-
bool lp64) {
547+
const DataExtractor &data, bool lp64) {
553548
lldb::offset_t offset = 0;
554549
int pr_version = data.GetU32(&offset);
555550

@@ -568,8 +563,7 @@ static void ParseFreeBSDPrPsInfo(ProcessElfCore &process,
568563
}
569564

570565
static llvm::Error ParseNetBSDProcInfo(const DataExtractor &data,
571-
uint32_t &cpi_nlwps,
572-
uint32_t &cpi_signo,
566+
uint32_t &cpi_nlwps, uint32_t &cpi_signo,
573567
uint32_t &cpi_siglwp,
574568
uint32_t &cpi_pid) {
575569
lldb::offset_t offset = 0;
@@ -737,8 +731,8 @@ llvm::Error ProcessElfCore::parseNetBSDNotes(llvm::ArrayRef<CoreNote> notes) {
737731

738732
if (name == "NetBSD-CORE") {
739733
if (note.info.n_type == NETBSD::NT_PROCINFO) {
740-
llvm::Error error = ParseNetBSDProcInfo(note.data, nlwps, signo,
741-
siglwp, pr_pid);
734+
llvm::Error error =
735+
ParseNetBSDProcInfo(note.data, nlwps, signo, siglwp, pr_pid);
742736
if (error)
743737
return error;
744738
SetID(pr_pid);
@@ -964,7 +958,9 @@ llvm::Error ProcessElfCore::parseLinuxNotes(llvm::ArrayRef<CoreNote> notes) {
964958
Status status = prpsinfo.Parse(note.data, arch);
965959
if (status.Fail())
966960
return status.ToError();
967-
thread_data.name.assign (prpsinfo.pr_fname, strnlen (prpsinfo.pr_fname, sizeof (prpsinfo.pr_fname)));
961+
thread_data.name.assign(
962+
prpsinfo.pr_fname,
963+
strnlen(prpsinfo.pr_fname, sizeof(prpsinfo.pr_fname)));
968964
SetID(prpsinfo.pr_pid);
969965
m_executable_name = thread_data.name;
970966
break;
@@ -1019,7 +1015,7 @@ llvm::Error ProcessElfCore::ParseThreadContextsFromNoteSegment(
10191015
assert(segment_header.p_type == llvm::ELF::PT_NOTE);
10201016

10211017
auto notes_or_error = parseSegment(segment_data);
1022-
if(!notes_or_error)
1018+
if (!notes_or_error)
10231019
return notes_or_error.takeError();
10241020
switch (GetArchitecture().GetTriple().getOS()) {
10251021
case llvm::Triple::FreeBSD:
@@ -1152,14 +1148,15 @@ bool ProcessElfCore::GetProcessInfo(ProcessInstanceInfo &info) {
11521148
return true;
11531149
}
11541150

1155-
std::optional<CoreNote> ProcessElfCore::GetAmdGpuNote() {
1151+
std::vector<CoreNote> ProcessElfCore::GetCoreNotes() {
1152+
std::vector<CoreNote> notes;
11561153
ObjectFileELF *core = (ObjectFileELF *)(m_core_module_sp->GetObjectFile());
11571154
if (core == nullptr)
1158-
return std::nullopt;
1155+
return notes;
11591156
llvm::ArrayRef<elf::ELFProgramHeader> program_headers =
11601157
core->ProgramHeaders();
11611158
if (program_headers.size() == 0)
1162-
return std::nullopt;
1159+
return notes;
11631160

11641161
for (const elf::ELFProgramHeader &H : program_headers) {
11651162
if (H.p_type == llvm::ELF::PT_NOTE) {
@@ -1170,11 +1167,11 @@ std::optional<CoreNote> ProcessElfCore::GetAmdGpuNote() {
11701167
llvm::consumeError(notes_or_error.takeError());
11711168
continue;
11721169
}
1173-
for (const auto &note : *notes_or_error) {
1174-
if (note.info.n_type == llvm::ELF::NT_AMDGPU_KFD_CORE_STATE)
1175-
return note;
1170+
// Append all notes from this segment to our collection
1171+
for (auto &note : *notes_or_error) {
1172+
notes.push_back(std::move(note));
11761173
}
11771174
}
11781175
}
1179-
return std::nullopt;
1176+
return notes;
11801177
}

lldb/source/Plugins/Process/elf-core/ProcessElfCore.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class ProcessElfCore : public lldb_private::PostMortemProcess {
9898

9999
bool GetProcessInfo(lldb_private::ProcessInstanceInfo &info) override;
100100

101-
std::optional<lldb_private::CoreNote> GetAmdGpuNote();
101+
std::vector<lldb_private::CoreNote> GetCoreNotes();
102102

103103
protected:
104104
void Clear();

0 commit comments

Comments
 (0)