Skip to content

Commit a9febfc

Browse files
author
aokblast
committed
fix: fix all comment's bug
1 parent a61068e commit a9febfc

File tree

3 files changed

+105
-61
lines changed

3 files changed

+105
-61
lines changed

lldb/source/Plugins/DynamicLoader/FreeBSD-Kernel/DynamicLoaderFreeBSDKernel.cpp

Lines changed: 72 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
//===-- DynamicLoaderFreeBSDKernel.cpp
2+
//------------------------------------------===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
110
#include "lldb/Breakpoint/StoppointCallbackContext.h"
211
#include "lldb/Core/Debugger.h"
312
#include "lldb/Core/Module.h"
@@ -55,7 +64,7 @@ static bool is_kernel(Module *module) {
5564
if (objfile->GetType() != ObjectFile::eTypeExecutable)
5665
return false;
5766
if (objfile->GetStrata() != ObjectFile::eStrataUnknown &&
58-
objfile->GetStrata() != ObjectFile::eStrataUser)
67+
objfile->GetStrata() != ObjectFile::eStrataKernel)
5968
return false;
6069

6170
return true;
@@ -92,16 +101,12 @@ DynamicLoader *
92101
DynamicLoaderFreeBSDKernel::CreateInstance(lldb_private::Process *process,
93102
bool force) {
94103
// Check the environment when the plugin is not force loaded
95-
Log *log = GetLog(LLDBLog::DynamicLoader);
96-
LLDB_LOGF(log, "DynamicLoaderFreeBSDKernel::CreateInstance: "
97-
"Try to create instance");
104+
Module *exec = process->GetTarget().GetExecutableModulePointer();
105+
if (exec && !is_kernel(exec)) {
106+
return nullptr;
107+
}
98108
if (!force) {
99-
Module *exec = process->GetTarget().GetExecutableModulePointer();
100109
// Check if the target is kernel
101-
if (exec && !is_kernel(exec)) {
102-
return nullptr;
103-
}
104-
105110
const llvm::Triple &triple_ref =
106111
process->GetTarget().GetArchitecture().GetTriple();
107112
if (!triple_ref.isOSFreeBSD()) {
@@ -187,17 +192,22 @@ lldb_private::UUID DynamicLoaderFreeBSDKernel::CheckForKernelImageAtAddress(
187192
addr);
188193

189194
llvm::ELF::Elf32_Ehdr header;
190-
if (!ReadELFHeader(process, addr, header))
195+
if (!ReadELFHeader(process, addr, header)) {
196+
*read_error = true;
191197
return UUID();
198+
}
192199

193200
// Check header type
194201
if (header.e_type != llvm::ELF::ET_EXEC)
195202
return UUID();
196203

197204
ModuleSP memory_module_sp =
198205
process->ReadModuleFromMemory(FileSpec("temp_freebsd_kernel"), addr);
199-
if (!memory_module_sp.get())
206+
207+
if (!memory_module_sp.get()) {
208+
*read_error = true;
200209
return UUID();
210+
}
201211

202212
ObjectFile *exe_objfile = memory_module_sp->GetObjectFile();
203213
if (exe_objfile == nullptr) {
@@ -209,31 +219,27 @@ lldb_private::UUID DynamicLoaderFreeBSDKernel::CheckForKernelImageAtAddress(
209219
return UUID();
210220
}
211221

212-
if (is_kernel(memory_module_sp.get())) {
213-
ArchSpec kernel_arch(
214-
llvm::ELF::convertEMachineToArchName(header.e_machine));
222+
// In here, I should check is_kernel for memory_module_sp
223+
// However, the ReadModuleFromMemory reads wrong section so that this check
224+
// will failed
225+
ArchSpec kernel_arch(llvm::ELF::convertEMachineToArchName(header.e_machine));
215226

216-
if (!process->GetTarget().GetArchitecture().IsCompatibleMatch(kernel_arch))
217-
process->GetTarget().SetArchitecture(kernel_arch);
227+
if (!process->GetTarget().GetArchitecture().IsCompatibleMatch(kernel_arch))
228+
process->GetTarget().SetArchitecture(kernel_arch);
218229

219-
if (log) {
220-
std::string uuid_str;
221-
if (memory_module_sp->GetUUID().IsValid()) {
222-
uuid_str = "with UUID ";
223-
uuid_str += memory_module_sp->GetUUID().GetAsString();
224-
} else {
225-
uuid_str = "and no LC_UUID found in load commands ";
226-
}
227-
LLDB_LOGF(log,
228-
"DynamicLoaderFreeBSDKernel::CheckForKernelImageAtAddress: "
229-
"kernel binary image found at 0x%" PRIx64 " with arch '%s' %s",
230-
addr, kernel_arch.GetTriple().str().c_str(), uuid_str.c_str());
231-
}
232-
233-
return memory_module_sp->GetUUID();
230+
std::string uuid_str;
231+
if (memory_module_sp->GetUUID().IsValid()) {
232+
uuid_str = "with UUID ";
233+
uuid_str += memory_module_sp->GetUUID().GetAsString();
234+
} else {
235+
uuid_str = "and no LC_UUID found in load commands ";
234236
}
237+
LLDB_LOGF(log,
238+
"DynamicLoaderFreeBSDKernel::CheckForKernelImageAtAddress: "
239+
"kernel binary image found at 0x%" PRIx64 " with arch '%s' %s",
240+
addr, kernel_arch.GetTriple().str().c_str(), uuid_str.c_str());
235241

236-
return UUID();
242+
return memory_module_sp->GetUUID();
237243
}
238244

239245
void DynamicLoaderFreeBSDKernel::DebuggerInit(
@@ -264,7 +270,7 @@ bool DynamicLoaderFreeBSDKernel::KModImageInfo::ReadMemoryModule(
264270
if (m_load_address == LLDB_INVALID_ADDRESS)
265271
return false;
266272

267-
FileSpec file_spec(m_name.c_str());
273+
FileSpec file_spec(m_name);
268274

269275
ModuleSP memory_module_sp;
270276

@@ -302,10 +308,8 @@ bool DynamicLoaderFreeBSDKernel::KModImageInfo::ReadMemoryModule(
302308

303309
// The kernel binary is from memory
304310
if (this_is_kernel) {
305-
if (log)
306-
LLDB_LOGF(log,
307-
"KextImageInfo::ReadMemoryModule read the kernel binary out "
308-
"of memory");
311+
LLDB_LOGF(log, "KextImageInfo::ReadMemoryModule read the kernel binary out "
312+
"of memory");
309313

310314
if (memory_module_sp->GetArchitecture().IsValid())
311315
process->GetTarget().SetArchitecture(memory_module_sp->GetArchitecture());
@@ -384,7 +388,7 @@ bool DynamicLoaderFreeBSDKernel::KModImageInfo::LoadImageUsingMemoryModule(
384388
// relocatable file So what we do is to set the load address only.
385389
if (is_kmod(m_module_sp.get()) && is_reloc(m_module_sp.get())) {
386390
m_stop_id = process->GetStopID();
387-
bool changed;
391+
bool changed = false;
388392
m_module_sp->SetLoadAddress(target, m_load_address, true, changed);
389393
return true;
390394
}
@@ -594,6 +598,7 @@ bool DynamicLoaderFreeBSDKernel::ReadAllKmods(
594598
if (!kld_off_address_symbol || !kld_off_next_symbol ||
595599
!kld_off_filename_symbol || !kld_off_pathname_symbol)
596600
return false;
601+
597602
Status error;
598603
const int32_t kld_off_address = m_process->ReadSignedIntegerFromMemory(
599604
kld_off_address_symbol->GetAddress().GetLoadAddress(
@@ -628,20 +633,34 @@ bool DynamicLoaderFreeBSDKernel::ReadAllKmods(
628633
linker_files_head_addr.GetLoadAddress(&m_process->GetTarget());
629634

630635
while (current_kld != 0) {
631-
m_process->ReadCStringFromMemory(
632-
m_process->ReadPointerFromMemory(current_kld + kld_off_filename, error),
633-
kld_filename, sizeof(kld_filename), error);
634-
m_process->ReadCStringFromMemory(
635-
m_process->ReadPointerFromMemory(current_kld + kld_off_pathname, error),
636-
kld_pathname, sizeof(kld_pathname), error);
636+
addr_t kld_filename_addr =
637+
m_process->ReadPointerFromMemory(current_kld + kld_off_filename, error);
638+
if (error.Fail())
639+
return false;
640+
addr_t kld_pathname_addr =
641+
m_process->ReadPointerFromMemory(current_kld + kld_off_pathname, error);
642+
if (error.Fail())
643+
return false;
644+
645+
m_process->ReadCStringFromMemory(kld_filename_addr, kld_filename,
646+
sizeof(kld_filename), error);
647+
if (error.Fail())
648+
return false;
649+
m_process->ReadCStringFromMemory(kld_pathname_addr, kld_pathname,
650+
sizeof(kld_pathname), error);
651+
if (error.Fail())
652+
return false;
637653
kld_load_addr =
638654
m_process->ReadPointerFromMemory(current_kld + kld_off_address, error);
655+
if (error.Fail())
656+
return false;
639657

640658
kmods_list.emplace_back();
641659
KModImageInfo &kmod_info = kmods_list.back();
642660
kmod_info.SetName(kld_filename);
643661
kmod_info.SetLoadAddress(kld_load_addr);
644662
kmod_info.SetPath(kld_pathname);
663+
645664
current_kld =
646665
m_process->ReadPointerFromMemory(current_kld + kld_off_next, error);
647666
if (kmod_info.GetName() == "kernel")
@@ -654,19 +673,15 @@ bool DynamicLoaderFreeBSDKernel::ReadAllKmods(
654673
}
655674

656675
// Read all kmods
657-
bool DynamicLoaderFreeBSDKernel::ReadAllKmods() {
676+
void DynamicLoaderFreeBSDKernel::ReadAllKmods() {
658677
std::lock_guard<decltype(m_mutex)> guard(m_mutex);
659678

660679
if (ReadKmodsListHeader()) {
661-
662680
if (m_linker_file_head_addr.IsValid()) {
663681
if (!ParseKmods(m_linker_file_head_addr))
664682
m_linker_files_list.clear();
665-
return true;
666683
}
667684
}
668-
669-
return false;
670685
}
671686

672687
// Load all Kernel Modules
@@ -684,12 +699,15 @@ void DynamicLoaderFreeBSDKernel::LoadKernelModules() {
684699
}
685700

686701
// Set name for kernel
687-
ConstString kernel_name("freebsd_kernel");
702+
llvm::StringRef kernel_name("freebsd_kernel");
688703
module_sp = m_kernel_image_info.GetModule();
689704
if (module_sp.get() && module_sp->GetObjectFile() &&
690705
!module_sp->GetObjectFile()->GetFileSpec().GetFilename().IsEmpty())
691-
kernel_name = module_sp->GetObjectFile()->GetFileSpec().GetFilename();
692-
m_kernel_image_info.SetName(kernel_name.AsCString());
706+
kernel_name = module_sp->GetObjectFile()
707+
->GetFileSpec()
708+
.GetFilename()
709+
.GetStringRef();
710+
m_kernel_image_info.SetName(kernel_name.data());
693711

694712
if (m_kernel_image_info.GetLoadAddress() == LLDB_INVALID_ADDRESS) {
695713
m_kernel_image_info.SetLoadAddress(m_kernel_load_address);
@@ -760,7 +778,8 @@ void DynamicLoaderFreeBSDKernel::PrivateInitialize(Process *process) {
760778
ThreadPlanSP DynamicLoaderFreeBSDKernel::GetStepThroughTrampolinePlan(
761779
lldb_private::Thread &thread, bool stop_others) {
762780
Log *log = GetLog(LLDBLog::Step);
763-
LLDB_LOGF(log, "Could not find symbol for step through.");
781+
LLDB_LOGF(log, "DynamicLoaderFreeBSDKernel::GetStepThroughTrampolinePlan is "
782+
"not yet implemented.");
764783
return {};
765784
}
766785

lldb/source/Plugins/DynamicLoader/FreeBSD-Kernel/DynamicLoaderFreeBSDKernel.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
//===-- DynamicLoaderFreeBSDKernel.h
2+
//------------------------------------------===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
110
#ifndef LLDB_SOURCE_PLUGINS_DYNAMICLOADER_FREEBSD_KERNEL_DYNAMICLOADERFREEBSDKERNEL_H
211
#define LLDB_SOURCE_PLUGINS_DYNAMICLOADER_FREEBSD_KERNEL_DYNAMICLOADERFREEBSDKERNEL_H
312

@@ -24,9 +33,7 @@ class DynamicLoaderFreeBSDKernel : public lldb_private::DynamicLoader {
2433

2534
static void Terminate();
2635

27-
static llvm::StringRef GetPluginNameStatic() {
28-
return "DynamicLoaderFreeBSDKernel";
29-
}
36+
static llvm::StringRef GetPluginNameStatic() { return "freebsd-kernel"; }
3037

3138
static llvm::StringRef GetPluginDescriptionStatic();
3239

@@ -124,7 +131,7 @@ class DynamicLoaderFreeBSDKernel : public lldb_private::DynamicLoader {
124131

125132
void LoadKernelModules();
126133

127-
bool ReadAllKmods();
134+
void ReadAllKmods();
128135

129136
bool ReadAllKmods(lldb_private::Address linker_files_head_address,
130137
KModImageInfo::collection_type &kmods_list);

lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3469,10 +3469,28 @@ ObjectFile::Strata ObjectFileELF::CalculateStrata() {
34693469

34703470
case llvm::ELF::ET_EXEC:
34713471
// 2 - Executable file
3472-
// TODO: is there any way to detect that an executable is a kernel
3473-
// related executable by inspecting the program headers, section headers,
3474-
// symbols, or any other flag bits???
3475-
return eStrataUser;
3472+
{
3473+
SectionList *section_list = GetSectionList();
3474+
if (section_list) {
3475+
static ConstString loader_section_name(".interp");
3476+
SectionSP loader_section =
3477+
section_list->FindSectionByName(loader_section_name);
3478+
if (loader_section) {
3479+
char buffer[256];
3480+
size_t read_size =
3481+
ReadSectionData(loader_section.get(), 0, buffer, sizeof(buffer));
3482+
3483+
// We compare the content of .interp section
3484+
// It will contains \0 when counting read_size, so the size needs to
3485+
// decrease by one
3486+
llvm::StringRef loader_name(buffer, read_size - 1);
3487+
llvm::StringRef freebsd_kernel_loader_name("/red/herring");
3488+
if (loader_name.equals(freebsd_kernel_loader_name))
3489+
return eStrataKernel;
3490+
}
3491+
}
3492+
return eStrataUser;
3493+
}
34763494

34773495
case llvm::ELF::ET_DYN:
34783496
// 3 - Shared object file

0 commit comments

Comments
 (0)