Skip to content

Commit 07b7770

Browse files
authored
Merge pull request #10941 from jasonmolenda/cp/r7438787-support-additional-registers-in-corefile-metadata
Cp/r7438787 support additional registers in corefile metadata
2 parents a401bac + 288dc9f commit 07b7770

File tree

11 files changed

+1056
-75
lines changed

11 files changed

+1056
-75
lines changed

lldb/include/lldb/Symbol/ObjectFile.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "lldb/Utility/Endian.h"
1919
#include "lldb/Utility/FileSpec.h"
2020
#include "lldb/Utility/FileSpecList.h"
21+
#include "lldb/Utility/StructuredData.h"
2122
#include "lldb/Utility/UUID.h"
2223
#include "lldb/lldb-private.h"
2324
#include "llvm/Support/Threading.h"
@@ -544,9 +545,9 @@ class ObjectFile : public std::enable_shared_from_this<ObjectFile>,
544545
return false;
545546
}
546547

547-
/// Get metadata about threads from the corefile.
548+
/// Get metadata about thread ids from the corefile.
548549
///
549-
/// The corefile may have metadata (e.g. a Mach-O "thread extrainfo"
550+
/// The corefile may have metadata (e.g. a Mach-O "process metadata"
550551
/// LC_NOTE) which for the threads in the process; this method tries
551552
/// to retrieve them.
552553
///
@@ -568,6 +569,18 @@ class ObjectFile : public std::enable_shared_from_this<ObjectFile>,
568569
return false;
569570
}
570571

572+
/// Get process metadata from the corefile in a StructuredData dictionary.
573+
///
574+
/// The corefile may have notes (e.g. a Mach-O "process metadata" LC_NOTE)
575+
/// which provide metadata about the process and threads in a JSON or
576+
/// similar format.
577+
///
578+
/// \return
579+
/// A StructuredData object with the metadata in the note, if there is
580+
/// one. An empty shared pointer is returned if not metadata is found,
581+
/// or a problem parsing it.
582+
virtual StructuredData::ObjectSP GetCorefileProcessMetadata() { return {}; }
583+
571584
virtual lldb::RegisterContextSP
572585
GetThreadContextAtIndex(uint32_t idx, lldb_private::Thread &thread) {
573586
return lldb::RegisterContextSP();

lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp

Lines changed: 70 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -187,46 +187,32 @@ class RegisterContextDarwin_x86_64_Mach : public RegisterContextDarwin_x86_64 {
187187
SetError(GPRRegSet, Read, -1);
188188
SetError(FPURegSet, Read, -1);
189189
SetError(EXCRegSet, Read, -1);
190-
bool done = false;
191190

192-
while (!done) {
191+
while (offset < data.GetByteSize()) {
193192
int flavor = data.GetU32(&offset);
194193
if (flavor == 0)
195-
done = true;
196-
else {
197-
uint32_t i;
198-
uint32_t count = data.GetU32(&offset);
199-
switch (flavor) {
200-
case GPRRegSet:
201-
for (i = 0; i < count; ++i)
202-
(&gpr.rax)[i] = data.GetU64(&offset);
203-
SetError(GPRRegSet, Read, 0);
204-
done = true;
205-
206-
break;
207-
case FPURegSet:
208-
// TODO: fill in FPU regs....
209-
// SetError (FPURegSet, Read, -1);
210-
done = true;
211-
212-
break;
213-
case EXCRegSet:
214-
exc.trapno = data.GetU32(&offset);
215-
exc.err = data.GetU32(&offset);
216-
exc.faultvaddr = data.GetU64(&offset);
217-
SetError(EXCRegSet, Read, 0);
218-
done = true;
219-
break;
220-
case 7:
221-
case 8:
222-
case 9:
223-
// fancy flavors that encapsulate of the above flavors...
224-
break;
225-
226-
default:
227-
done = true;
228-
break;
229-
}
194+
break;
195+
uint32_t count = data.GetU32(&offset);
196+
switch (flavor) {
197+
case GPRRegSet: {
198+
uint32_t *gpr_data = reinterpret_cast<uint32_t *>(&gpr.rax);
199+
for (uint32_t i = 0; i < count && offset < data.GetByteSize(); ++i)
200+
gpr_data[i] = data.GetU32(&offset);
201+
SetError(GPRRegSet, Read, 0);
202+
} break;
203+
case FPURegSet:
204+
// TODO: fill in FPU regs....
205+
SetError(FPURegSet, Read, -1);
206+
break;
207+
case EXCRegSet:
208+
exc.trapno = data.GetU32(&offset);
209+
exc.err = data.GetU32(&offset);
210+
exc.faultvaddr = data.GetU64(&offset);
211+
SetError(EXCRegSet, Read, 0);
212+
break;
213+
default:
214+
offset += count * 4;
215+
break;
230216
}
231217
}
232218
}
@@ -356,11 +342,11 @@ class RegisterContextDarwin_x86_64_Mach : public RegisterContextDarwin_x86_64 {
356342
}
357343

358344
protected:
359-
int DoReadGPR(lldb::tid_t tid, int flavor, GPR &gpr) override { return 0; }
345+
int DoReadGPR(lldb::tid_t tid, int flavor, GPR &gpr) override { return -1; }
360346

361-
int DoReadFPU(lldb::tid_t tid, int flavor, FPU &fpu) override { return 0; }
347+
int DoReadFPU(lldb::tid_t tid, int flavor, FPU &fpu) override { return -1; }
362348

363-
int DoReadEXC(lldb::tid_t tid, int flavor, EXC &exc) override { return 0; }
349+
int DoReadEXC(lldb::tid_t tid, int flavor, EXC &exc) override { return -1; }
364350

365351
int DoWriteGPR(lldb::tid_t tid, int flavor, const GPR &gpr) override {
366352
return 0;
@@ -5893,27 +5879,8 @@ bool ObjectFileMachO::GetCorefileThreadExtraInfos(
58935879
std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
58945880

58955881
Log *log(GetLog(LLDBLog::Object | LLDBLog::Process | LLDBLog::Thread));
5896-
auto lc_notes = FindLC_NOTEByName("process metadata");
5897-
for (auto lc_note : lc_notes) {
5898-
offset_t payload_offset = std::get<0>(lc_note);
5899-
offset_t strsize = std::get<1>(lc_note);
5900-
std::string buf(strsize, '\0');
5901-
if (m_data.CopyData(payload_offset, strsize, buf.data()) != strsize) {
5902-
LLDB_LOGF(log,
5903-
"Unable to read %" PRIu64
5904-
" bytes of 'process metadata' LC_NOTE JSON contents",
5905-
strsize);
5906-
return false;
5907-
}
5908-
while (buf.back() == '\0')
5909-
buf.resize(buf.size() - 1);
5910-
StructuredData::ObjectSP object_sp = StructuredData::ParseJSON(buf);
5882+
if (StructuredData::ObjectSP object_sp = GetCorefileProcessMetadata()) {
59115883
StructuredData::Dictionary *dict = object_sp->GetAsDictionary();
5912-
if (!dict) {
5913-
LLDB_LOGF(log, "Unable to read 'process metadata' LC_NOTE, did not "
5914-
"get a dictionary.");
5915-
return false;
5916-
}
59175884
StructuredData::Array *threads;
59185885
if (!dict->GetValueForKeyAsArray("threads", threads) || !threads) {
59195886
LLDB_LOGF(log,
@@ -5956,6 +5923,49 @@ bool ObjectFileMachO::GetCorefileThreadExtraInfos(
59565923
return false;
59575924
}
59585925

5926+
StructuredData::ObjectSP ObjectFileMachO::GetCorefileProcessMetadata() {
5927+
ModuleSP module_sp(GetModule());
5928+
if (!module_sp)
5929+
return {};
5930+
5931+
Log *log(GetLog(LLDBLog::Object | LLDBLog::Process | LLDBLog::Thread));
5932+
std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
5933+
auto lc_notes = FindLC_NOTEByName("process metadata");
5934+
if (lc_notes.size() == 0)
5935+
return {};
5936+
5937+
if (lc_notes.size() > 1)
5938+
LLDB_LOGF(
5939+
log,
5940+
"Multiple 'process metadata' LC_NOTEs found, only using the first.");
5941+
5942+
auto [payload_offset, strsize] = lc_notes[0];
5943+
std::string buf(strsize, '\0');
5944+
if (m_data.CopyData(payload_offset, strsize, buf.data()) != strsize) {
5945+
LLDB_LOGF(log,
5946+
"Unable to read %" PRIu64
5947+
" bytes of 'process metadata' LC_NOTE JSON contents",
5948+
strsize);
5949+
return {};
5950+
}
5951+
while (buf.back() == '\0')
5952+
buf.resize(buf.size() - 1);
5953+
StructuredData::ObjectSP object_sp = StructuredData::ParseJSON(buf);
5954+
if (!object_sp) {
5955+
LLDB_LOGF(log, "Unable to read 'process metadata' LC_NOTE, did not "
5956+
"parse as valid JSON.");
5957+
return {};
5958+
}
5959+
StructuredData::Dictionary *dict = object_sp->GetAsDictionary();
5960+
if (!dict) {
5961+
LLDB_LOGF(log, "Unable to read 'process metadata' LC_NOTE, did not "
5962+
"get a dictionary.");
5963+
return {};
5964+
}
5965+
5966+
return object_sp;
5967+
}
5968+
59595969
lldb::RegisterContextSP
59605970
ObjectFileMachO::GetThreadContextAtIndex(uint32_t idx,
59615971
lldb_private::Thread &thread) {

lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ class ObjectFileMachO : public lldb_private::ObjectFile {
132132

133133
bool GetCorefileThreadExtraInfos(std::vector<lldb::tid_t> &tids) override;
134134

135+
lldb_private::StructuredData::ObjectSP GetCorefileProcessMetadata() override;
136+
135137
bool LoadCoreFileImages(lldb_private::Process &process) override;
136138

137139
lldb::RegisterContextSP

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
add_lldb_library(lldbPluginProcessMachCore PLUGIN
22
ProcessMachCore.cpp
33
ThreadMachCore.cpp
4+
RegisterContextUnifiedCore.cpp
45

56
LINK_LIBS
67
lldbBreakpoint

0 commit comments

Comments
 (0)