LLDB mainline
DynamicLoaderFreeBSDKernel.cpp
Go to the documentation of this file.
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
11#include "lldb/Core/Debugger.h"
12#include "lldb/Core/Module.h"
15#include "lldb/Core/Section.h"
22#include "lldb/Target/Target.h"
23#include "lldb/Target/Thread.h"
28#include "lldb/Utility/Log.h"
29#include "lldb/Utility/State.h"
30
31#include "llvm/Support/Error.h"
32
34
36#include <memory>
37#include <mutex>
38
39using namespace lldb;
40using namespace lldb_private;
41
43
49
53
55 return "The Dynamic Loader Plugin For FreeBSD Kernel";
56}
57
58static bool is_kernel(Module *module) {
59 if (!module)
60 return false;
61
62 ObjectFile *objfile = module->GetObjectFile();
63 if (!objfile)
64 return false;
65 if (objfile->GetType() != ObjectFile::eTypeExecutable)
66 return false;
67 if (objfile->GetStrata() != ObjectFile::eStrataUnknown &&
69 return false;
70
71 return true;
72}
73
74static bool is_kmod(Module *module) {
75 if (!module)
76 return false;
77 if (!module->GetObjectFile())
78 return false;
79 ObjectFile *objfile = module->GetObjectFile();
80 if (objfile->GetType() != ObjectFile::eTypeObjectFile &&
82 return false;
83
84 return true;
85}
86
87static bool is_reloc(Module *module) {
88 if (!module)
89 return false;
90 if (!module->GetObjectFile())
91 return false;
92 ObjectFile *objfile = module->GetObjectFile();
93 if (objfile->GetType() != ObjectFile::eTypeObjectFile)
94 return false;
95
96 return true;
97}
98
99// Instantiate Function of the FreeBSD Kernel Dynamic Loader Plugin called when
100// Register the Plugin
103 bool force) {
104 // Check the environment when the plugin is not force loaded
105 Module *exec = process->GetTarget().GetExecutableModulePointer();
106 if (exec && !is_kernel(exec)) {
107 return nullptr;
108 }
109 if (!force) {
110 // Check if the target is kernel
111 const llvm::Triple &triple_ref =
112 process->GetTarget().GetArchitecture().GetTriple();
113 if (!triple_ref.isOSFreeBSD()) {
114 return nullptr;
115 }
116 }
117
118 // At this point we have checked the target is a FreeBSD kernel and all we
119 // have to do is to find the kernel address
120 const addr_t kernel_address = FindFreeBSDKernel(process);
121
122 if (CheckForKernelImageAtAddress(process, kernel_address).IsValid())
123 return new DynamicLoaderFreeBSDKernel(process, kernel_address);
124
125 return nullptr;
126}
127
128addr_t
130 addr_t kernel_addr = process->GetImageInfoAddress();
131 if (kernel_addr == LLDB_INVALID_ADDRESS)
132 kernel_addr = FindKernelAtLoadAddress(process);
133 return kernel_addr;
134}
135
136// Get the kernel address if the kernel is not loaded with a slide
138 lldb_private::Process *process) {
139 Module *exe_module = process->GetTarget().GetExecutableModulePointer();
140
141 if (!is_kernel(exe_module))
143
144 ObjectFile *exe_objfile = exe_module->GetObjectFile();
145
146 if (!exe_objfile->GetBaseAddress().IsValid())
148
150 process, exe_objfile->GetBaseAddress().GetFileAddress())
151 .IsValid())
152 return exe_objfile->GetBaseAddress().GetFileAddress();
153
155}
156
157// Read ELF header from memry and return
159 lldb::addr_t addr,
160 llvm::ELF::Elf32_Ehdr &header,
161 bool *read_error) {
163 if (read_error)
164 *read_error = false;
165
166 if (process->ReadMemory(addr, &header, sizeof(header), error) !=
167 sizeof(header) ||
168 error.Fail()) {
169 if (read_error)
170 *read_error = true;
171 return false;
172 }
173
174 if (!header.checkMagic())
175 return false;
176
177 return true;
178}
179
180// Check the correctness of Kernel and return UUID
182 Process *process, lldb::addr_t addr, bool *read_error) {
184
185 if (addr == LLDB_INVALID_ADDRESS) {
186 if (read_error)
187 *read_error = true;
188 return UUID();
189 }
190
191 LLDB_LOGF(log,
192 "DynamicLoaderFreeBSDKernel::CheckForKernelImageAtAddress: "
193 "looking for kernel binary at 0x%" PRIx64,
194 addr);
195
196 llvm::ELF::Elf32_Ehdr header;
197 if (!ReadELFHeader(process, addr, header)) {
198 *read_error = true;
199 return UUID();
200 }
201
202 // Check header type
203 if (header.e_type != llvm::ELF::ET_EXEC)
204 return UUID();
205
206 llvm::Expected<ModuleSP> memory_module_sp_or_err =
207 process->ReadModuleFromMemory(FileSpec("temp_freebsd_kernel"), addr);
208 if (auto err = memory_module_sp_or_err.takeError()) {
209 LLDB_LOG_ERROR(log, std::move(err),
210 "DynamicLoaderFreeBSDKernel::CheckForKernelImageAtAddress: "
211 "Failed to read module in memory -- {0}");
212 *read_error = true;
213 return UUID();
214 }
215
216 ModuleSP memory_module_sp = *memory_module_sp_or_err;
217 if (!memory_module_sp.get()) {
218 *read_error = true;
219 return UUID();
220 }
221
222 ObjectFile *exe_objfile = memory_module_sp->GetObjectFile();
223 if (exe_objfile == nullptr) {
224 LLDB_LOGF(log,
225 "DynamicLoaderFreeBSDKernel::CheckForKernelImageAtAddress "
226 "found a binary at 0x%" PRIx64
227 " but could not create an object file from memory",
228 addr);
229 return UUID();
230 }
231
232 // In here, I should check is_kernel for memory_module_sp
233 // However, the ReadModuleFromMemory reads wrong section so that this check
234 // will failed
235 ArchSpec kernel_arch(llvm::ELF::convertEMachineToArchName(header.e_machine));
236
237 if (!process->GetTarget().GetArchitecture().IsCompatibleMatch(kernel_arch))
238 process->GetTarget().SetArchitecture(kernel_arch);
239
240 std::string uuid_str;
241 if (memory_module_sp->GetUUID().IsValid()) {
242 uuid_str = "with UUID ";
243 uuid_str += memory_module_sp->GetUUID().GetAsString();
244 } else {
245 uuid_str = "and no LC_UUID found in load commands ";
246 }
247 LLDB_LOGF(log,
248 "DynamicLoaderFreeBSDKernel::CheckForKernelImageAtAddress: "
249 "kernel binary image found at 0x%" PRIx64 " with arch '%s' %s",
250 addr, kernel_arch.GetTriple().str().c_str(), uuid_str.c_str());
251
252 return memory_module_sp->GetUUID();
253}
254
257
259 addr_t kernel_address)
260 : DynamicLoader(process), m_process(process),
261 m_kernel_load_address(kernel_address), m_mutex() {
262 process->SetCanRunCode(false);
263}
264
266
271
272// Create in memory Module at the load address
274 lldb_private::Process *process) {
277 return true;
279 return false;
280
281 FileSpec file_spec(m_name);
282
283 ModuleSP memory_module_sp;
284
285 llvm::ELF::Elf32_Ehdr elf_eheader;
286 size_t size_to_read = 512;
287
288 if (ReadELFHeader(process, m_load_address, elf_eheader)) {
289 if (elf_eheader.e_ident[llvm::ELF::EI_CLASS] == llvm::ELF::ELFCLASS32) {
290 size_to_read = sizeof(llvm::ELF::Elf32_Ehdr) +
291 elf_eheader.e_phnum * elf_eheader.e_phentsize;
292 } else if (elf_eheader.e_ident[llvm::ELF::EI_CLASS] ==
293 llvm::ELF::ELFCLASS64) {
294 llvm::ELF::Elf64_Ehdr elf_eheader;
296 if (process->ReadMemory(m_load_address, &elf_eheader, sizeof(elf_eheader),
297 error) == sizeof(elf_eheader) &&
298 error.Success())
299 size_to_read = sizeof(llvm::ELF::Elf64_Ehdr) +
300 elf_eheader.e_phnum * elf_eheader.e_phentsize;
301 }
302 }
303
304 llvm::Expected<ModuleSP> memory_module_sp_or_err =
305 process->ReadModuleFromMemory(file_spec, m_load_address, size_to_read);
306 if (auto err = memory_module_sp_or_err.takeError()) {
307 LLDB_LOG_ERROR(log, std::move(err),
308 "KextImageInfo::ReadMemoryModule: Failed to read module "
309 "from memory -- {0}");
310 return false;
311 }
312 memory_module_sp = *memory_module_sp_or_err;
313
314 if (!memory_module_sp)
315 return false;
316
317 bool this_is_kernel = is_kernel(memory_module_sp.get());
318
319 if (!m_uuid.IsValid() && memory_module_sp->GetUUID().IsValid())
320 m_uuid = memory_module_sp->GetUUID();
321
322 m_memory_module_sp = memory_module_sp;
323 m_is_kernel = this_is_kernel;
324
325 // The kernel binary is from memory
326 if (this_is_kernel) {
327 LLDB_LOGF(log, "KextImageInfo::ReadMemoryModule read the kernel binary out "
328 "of memory");
329
330 if (memory_module_sp->GetArchitecture().IsValid())
331 process->GetTarget().SetArchitecture(memory_module_sp->GetArchitecture());
332 }
333
334 return true;
335}
336
338 lldb_private::Process *process) {
340
341 if (IsLoaded())
342 return true;
343
344 Target &target = process->GetTarget();
345
346 if (IsKernel() && m_uuid.IsValid()) {
348 s->Printf("Kernel UUID: %s\n", m_uuid.GetAsString().c_str());
349 s->Printf("Load Address: 0x%" PRIx64 "\n", m_load_address);
350 }
351
352 // Test if the module is loaded into the taget,
353 // maybe the module is loaded manually by user by doing target module add
354 // So that we have to create the module manually
355 if (!m_module_sp) {
356 const ModuleList &target_images = target.GetImages();
357 m_module_sp = target_images.FindModule(m_uuid);
358
359 // Search in the file system
360 if (!m_module_sp) {
361 ModuleSpec module_spec(FileSpec(GetPath()), target.GetArchitecture());
362 if (IsKernel()) {
365 true) &&
366 error.Success()) {
367 if (FileSystem::Instance().Exists(module_spec.GetFileSpec()))
368 m_module_sp = std::make_shared<Module>(module_spec.GetFileSpec(),
369 target.GetArchitecture());
370 }
371 }
372
373 if (!m_module_sp)
374 m_module_sp = target.GetOrCreateModule(module_spec, true);
375 if (IsKernel() && !m_module_sp) {
376 target.GetDebugger().GetAsyncOutputStream()->Printf(
377 "WARNING: Unable to locate kernel binary on the debugger "
378 "system.\n");
379 }
380 }
381
382 if (m_module_sp) {
383 // If the file is not kernel or kmod, the target should be loaded once and
384 // don't reload again
385 if (!IsKernel() && !is_kmod(m_module_sp.get())) {
386 ModuleSP existing_module_sp = target.GetImages().FindModule(m_uuid);
387 if (existing_module_sp &&
388 existing_module_sp->IsLoadedInTarget(&target)) {
389 LLDB_LOGF(log,
390 "'%s' with UUID %s is not a kmod or kernel, and is "
391 "already registered in target, not loading.",
392 m_name.c_str(), m_uuid.GetAsString().c_str());
393 return true;
394 }
395 }
396 m_uuid = m_module_sp->GetUUID();
397
398 // or append to the images
399 target.GetImages().AppendIfNeeded(m_module_sp, false);
400 }
401 }
402
403 // If this file is relocatable kernel module(x86_64), adjust it's
404 // section(PT_LOAD segment) and return Because the kernel module's load
405 // address is the text section. lldb cannot create full memory module upon
406 // relocatable file So what we do is to set the load address only.
407 if (is_kmod(m_module_sp.get()) && is_reloc(m_module_sp.get())) {
408 m_stop_id = process->GetStopID();
409 bool changed = false;
410 m_module_sp->SetLoadAddress(target, m_load_address, true, changed);
411 return true;
412 }
413
414 if (m_module_sp)
415 ReadMemoryModule(process);
416
417 // Calculate the slides of in memory module
419 m_module_sp.reset();
420 return false;
421 }
422
423 ObjectFile *ondisk_object_file = m_module_sp->GetObjectFile();
424 ObjectFile *memory_object_file = m_memory_module_sp->GetObjectFile();
425
426 if (!ondisk_object_file || !memory_object_file)
427 m_module_sp.reset();
428
429 // Find the slide address
430 addr_t fixed_slide = LLDB_INVALID_ADDRESS;
431 if (llvm::dyn_cast<ObjectFileELF>(memory_object_file)) {
432 addr_t load_address = memory_object_file->GetBaseAddress().GetFileAddress();
433
434 if (load_address != LLDB_INVALID_ADDRESS &&
435 m_load_address != load_address) {
436 fixed_slide = m_load_address - load_address;
437 LLDB_LOGF(log,
438 "kmod %s in-memory LOAD vmaddr is not correct, using a "
439 "fixed slide of 0x%" PRIx64,
440 m_name.c_str(), fixed_slide);
441 }
442 }
443
444 SectionList *ondisk_section_list = ondisk_object_file->GetSectionList();
445 SectionList *memory_section_list = memory_object_file->GetSectionList();
446
447 if (memory_section_list && ondisk_object_file) {
448 const uint32_t num_ondisk_sections = ondisk_section_list->GetSize();
449 uint32_t num_load_sections = 0;
450
451 for (uint32_t section_idx = 0; section_idx < num_ondisk_sections;
452 ++section_idx) {
453 SectionSP on_disk_section_sp =
454 ondisk_section_list->GetSectionAtIndex(section_idx);
455
456 if (!on_disk_section_sp)
457 continue;
458 if (fixed_slide != LLDB_INVALID_ADDRESS) {
459 target.SetSectionLoadAddress(on_disk_section_sp,
460 on_disk_section_sp->GetFileAddress() +
461 fixed_slide);
462
463 } else {
464 const Section *memory_section =
465 memory_section_list
466 ->FindSectionByName(on_disk_section_sp->GetName())
467 .get();
468 if (memory_section) {
469 target.SetSectionLoadAddress(on_disk_section_sp,
470 memory_section->GetFileAddress());
471 ++num_load_sections;
472 }
473 }
474 }
475
476 if (num_load_sections)
477 m_stop_id = process->GetStopID();
478 else
479 m_module_sp.reset();
480 } else {
481 m_module_sp.reset();
482 }
483
484 if (IsLoaded() && m_module_sp && IsKernel()) {
486 ObjectFile *kernel_object_file = m_module_sp->GetObjectFile();
487 if (kernel_object_file) {
488 addr_t file_address =
489 kernel_object_file->GetBaseAddress().GetFileAddress();
491 file_address != LLDB_INVALID_ADDRESS) {
492 s->Printf("Kernel slide 0x%" PRIx64 " in memory.\n",
493 m_load_address - file_address);
494 s->Printf("Loaded kernel file %s\n",
495 m_module_sp->GetFileSpec().GetPath().c_str());
496 }
497 }
498 }
499
500 return IsLoaded();
501}
502
503// This function is work for kernel file, others it wil reset load address and
504// return false
506 lldb_private::Process *process) {
507 if (IsLoaded())
508 return true;
509
510 if (m_module_sp) {
511 bool changed = false;
512 if (m_module_sp->SetLoadAddress(process->GetTarget(), 0, true, changed))
513 m_stop_id = process->GetStopID();
514 }
515
516 return false;
517}
518
519// Get the head of found_list
521 std::lock_guard<decltype(m_mutex)> guard(m_mutex);
522
523 if (m_linker_file_list_struct_addr.IsValid()) {
524 // Get tqh_first struct element from linker_files
525 llvm::Expected<lldb::addr_t> address = m_process->ReadPointerFromMemory(
526 m_linker_file_list_struct_addr.GetLoadAddress(&m_process->GetTarget()));
527 if (!address) {
528 llvm::consumeError(address.takeError());
530 return false;
531 }
533
534 if (!m_linker_file_head_addr.IsValid() ||
535 m_linker_file_head_addr.GetFileAddress() == 0) {
537 return false;
538 }
539 }
540 return true;
541}
542
543// Parse Kmod info in found_list
545 std::lock_guard<decltype(m_mutex)> guard(m_mutex);
546 KModImageInfo::collection_type linker_files_list;
548
549 if (!ReadAllKmods(linker_files_head_addr, linker_files_list))
550 return false;
551 LLDB_LOGF(
552 log,
553 "Kmod-changed breakpoint hit, there are %zu kernel modules currently.\n",
554 linker_files_list.size());
555
556 ModuleList &modules = m_process->GetTarget().GetImages();
557 ModuleList remove_modules;
558 ModuleList add_modules;
559
560 for (ModuleSP module : modules.Modules()) {
561 if (is_kernel(module.get()))
562 continue;
563 if (is_kmod(module.get()))
564 remove_modules.AppendIfNeeded(module);
565 }
566
567 m_process->GetTarget().ModulesDidUnload(remove_modules, false);
568
569 for (KModImageInfo &image_info : linker_files_list) {
570 auto it = m_kld_name_to_uuid.find(image_info.GetName());
571 if (it != m_kld_name_to_uuid.end())
572 image_info.SetUUID(it->second);
573 bool failed_to_load = false;
574 if (!image_info.LoadImageUsingMemoryModule(m_process)) {
575 image_info.LoadImageUsingFileAddress(m_process);
576 failed_to_load = true;
577 } else {
578 m_linker_files_list.push_back(image_info);
579 m_kld_name_to_uuid[image_info.GetName()] = image_info.GetUUID();
580 }
581
582 if (!failed_to_load)
583 add_modules.AppendIfNeeded(image_info.GetModule());
584 }
585 m_process->GetTarget().ModulesDidLoad(add_modules);
586 return true;
587}
588
589// Read all kmod from a given arrays of list
591 Address linker_files_head_addr,
592 KModImageInfo::collection_type &kmods_list) {
593
594 // Get offset of next member and load address symbol
595 static ConstString kld_off_address_symbol_name("kld_off_address");
596 static ConstString kld_off_next_symbol_name("kld_off_next");
597 static ConstString kld_off_filename_symbol_name("kld_off_filename");
598 static ConstString kld_off_pathname_symbol_name("kld_off_pathname");
599 const Symbol *kld_off_address_symbol =
600 m_kernel_image_info.GetModule()->FindFirstSymbolWithNameAndType(
601 kld_off_address_symbol_name, eSymbolTypeData);
602 const Symbol *kld_off_next_symbol =
603 m_kernel_image_info.GetModule()->FindFirstSymbolWithNameAndType(
604 kld_off_next_symbol_name, eSymbolTypeData);
605 const Symbol *kld_off_filename_symbol =
606 m_kernel_image_info.GetModule()->FindFirstSymbolWithNameAndType(
607 kld_off_filename_symbol_name, eSymbolTypeData);
608 const Symbol *kld_off_pathname_symbol =
609 m_kernel_image_info.GetModule()->FindFirstSymbolWithNameAndType(
610 kld_off_pathname_symbol_name, eSymbolTypeData);
611
612 if (!kld_off_address_symbol || !kld_off_next_symbol ||
613 !kld_off_filename_symbol || !kld_off_pathname_symbol)
614 return false;
615
617 const int32_t kld_off_address = m_process->ReadSignedIntegerFromMemory(
618 kld_off_address_symbol->GetAddress().GetLoadAddress(
619 &m_process->GetTarget()),
620 4, 0, error);
621 if (error.Fail())
622 return false;
623 const int32_t kld_off_next = m_process->ReadSignedIntegerFromMemory(
624 kld_off_next_symbol->GetAddress().GetLoadAddress(&m_process->GetTarget()),
625 4, 0, error);
626 if (error.Fail())
627 return false;
628 const int32_t kld_off_filename = m_process->ReadSignedIntegerFromMemory(
629 kld_off_filename_symbol->GetAddress().GetLoadAddress(
630 &m_process->GetTarget()),
631 4, 0, error);
632 if (error.Fail())
633 return false;
634
635 const int32_t kld_off_pathname = m_process->ReadSignedIntegerFromMemory(
636 kld_off_pathname_symbol->GetAddress().GetLoadAddress(
637 &m_process->GetTarget()),
638 4, 0, error);
639 if (error.Fail())
640 return false;
641
642 // Parse KMods
643 addr_t kld_load_addr(LLDB_INVALID_ADDRESS);
644 char kld_filename[255];
645 char kld_pathname[255];
646 addr_t current_kld =
647 linker_files_head_addr.GetLoadAddress(&m_process->GetTarget());
648
649 while (current_kld != 0) {
650 llvm::Expected<lldb::addr_t> kld_filename_addr =
651 m_process->ReadPointerFromMemory(current_kld + kld_off_filename);
652 if (!kld_filename_addr) {
653 llvm::consumeError(kld_filename_addr.takeError());
654 return false;
655 }
656 llvm::Expected<lldb::addr_t> kld_pathname_addr =
657 m_process->ReadPointerFromMemory(current_kld + kld_off_pathname);
658 if (!kld_pathname_addr) {
659 llvm::consumeError(kld_pathname_addr.takeError());
660 return false;
661 }
662
663 m_process->ReadCStringFromMemory(*kld_filename_addr, kld_filename,
664 sizeof(kld_filename), error);
665 if (error.Fail())
666 return false;
667 m_process->ReadCStringFromMemory(*kld_pathname_addr, kld_pathname,
668 sizeof(kld_pathname), error);
669 if (error.Fail())
670 return false;
671 llvm::Expected<lldb::addr_t> kld_load_addr_or_err =
672 m_process->ReadPointerFromMemory(current_kld + kld_off_address);
673 if (!kld_load_addr_or_err) {
674 llvm::consumeError(kld_load_addr_or_err.takeError());
675 return false;
676 }
677 kld_load_addr = *kld_load_addr_or_err;
678
679 kmods_list.emplace_back();
680 KModImageInfo &kmod_info = kmods_list.back();
681 kmod_info.SetName(kld_filename);
682 kmod_info.SetLoadAddress(kld_load_addr);
683 kmod_info.SetPath(kld_pathname);
684
685 llvm::Expected<lldb::addr_t> next_kld =
686 m_process->ReadPointerFromMemory(current_kld + kld_off_next);
687
688 if (kmod_info.GetName() == "kernel")
689 kmods_list.pop_back();
690
691 if (!next_kld) {
692 llvm::consumeError(next_kld.takeError());
693 return false;
694 }
695 current_kld = *next_kld;
696 }
697
698 return true;
699}
700
701// Read all kmods
703 std::lock_guard<decltype(m_mutex)> guard(m_mutex);
704
705 if (ReadKmodsListHeader()) {
706 if (m_linker_file_head_addr.IsValid()) {
708 m_linker_files_list.clear();
709 }
710 }
711}
712
713// Load all Kernel Modules
716 LLDB_LOGF(log, "DynamicLoaderFreeBSDKernel::LoadKernelModules "
717 "Start loading Kernel Module");
718
719 // Initialize Kernel Image Information at the first time
720 if (m_kernel_image_info.GetLoadAddress() == LLDB_INVALID_ADDRESS) {
721 ModuleSP module_sp = m_process->GetTarget().GetExecutableModule();
722 if (is_kernel(module_sp.get())) {
723 m_kernel_image_info.SetModule(module_sp);
724 m_kernel_image_info.SetIsKernel(true);
725 }
726
727 // Set name for kernel
728 llvm::StringRef kernel_name("freebsd_kernel");
729 module_sp = m_kernel_image_info.GetModule();
730 if (module_sp.get() && module_sp->GetObjectFile() &&
731 !module_sp->GetObjectFile()->GetFileSpec().GetFilename().empty())
732 kernel_name = module_sp->GetObjectFile()->GetFileSpec().GetFilename();
733 m_kernel_image_info.SetName(kernel_name.data());
734
735 if (m_kernel_image_info.GetLoadAddress() == LLDB_INVALID_ADDRESS) {
737 }
738
739 // Build In memory Module
740 if (m_kernel_image_info.GetLoadAddress() != LLDB_INVALID_ADDRESS) {
741 // If the kernel is not loaded in the memory, use file to load
742 if (!m_kernel_image_info.LoadImageUsingMemoryModule(m_process))
743 m_kernel_image_info.LoadImageUsingFileAddress(m_process);
744 }
745 }
746
748
749 if (!m_kernel_image_info.IsLoaded() || !m_kernel_image_info.GetModule()) {
750 m_kernel_image_info.Clear();
751 return;
752 }
753
754 static ConstString modlist_symbol_name("linker_files");
755
756 const Symbol *symbol =
757 m_kernel_image_info.GetModule()->FindFirstSymbolWithNameAndType(
758 modlist_symbol_name, lldb::eSymbolTypeData);
759
760 if (symbol) {
762 ReadAllKmods();
763 } else {
764 LLDB_LOGF(log, "DynamicLoaderFreeBSDKernel::LoadKernelModules "
765 "cannot file modlist symbol");
766 }
767}
768
769// Update symbol when use kldload by setting callback function on kldload
771
772// Hook called when attach to a process
777
778// Hook called after attach to a process
783
784// Clear all member except kernel address
785void DynamicLoaderFreeBSDKernel::Clear(bool clear_process) {
786 std::lock_guard<decltype(m_mutex)> guard(m_mutex);
787 if (clear_process)
788 m_process = nullptr;
791 m_kernel_image_info.Clear();
792 m_linker_files_list.clear();
793}
794
795// Reinitialize class
797 Clear(true);
798 m_process = process;
799}
800
802 lldb_private::Thread &thread, bool stop_others) {
803 Log *log = GetLog(LLDBLog::Step);
804 LLDB_LOGF(log, "DynamicLoaderFreeBSDKernel::GetStepThroughTrampolinePlan is "
805 "not yet implemented.");
806 return {};
807}
808
810 return Status::FromErrorString("shared object cannot be loaded into kernel");
811}
static llvm::raw_ostream & error(Stream &strm)
static bool is_kernel(Module *module)
static bool is_reloc(Module *module)
static bool is_kernel(Module *module)
static bool is_kmod(Module *module)
#define LLDB_LOGF(log,...)
Definition Log.h:389
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:405
#define LLDB_PLUGIN_DEFINE(PluginName)
bool LoadImageUsingFileAddress(lldb_private::Process *process)
bool ReadMemoryModule(lldb_private::Process *process)
bool LoadImageUsingMemoryModule(lldb_private::Process *process)
void DidAttach() override
Called after attaching a process.
DynamicLoaderFreeBSDKernel(lldb_private::Process *process, lldb::addr_t kernel_addr)
KModImageInfo::collection_type m_linker_files_list
static llvm::StringRef GetPluginNameStatic()
lldb_private::Address m_linker_file_list_struct_addr
void PrivateInitialize(lldb_private::Process *process)
lldb_private::Address m_linker_file_head_addr
lldb::ThreadPlanSP GetStepThroughTrampolinePlan(lldb_private::Thread &thread, bool stop_others) override
Provides a plan to step through the dynamic loader trampoline for the current state of thread.
bool ParseKmods(lldb_private::Address linker_files_head_address)
static void DebuggerInit(lldb_private::Debugger &debugger)
static lldb::addr_t FindFreeBSDKernel(lldb_private::Process *process)
lldb_private::Status CanLoadImage() override
Ask if it is ok to try and load or unload an shared library (image).
std::unordered_map< std::string, lldb_private::UUID > m_kld_name_to_uuid
void DidLaunch() override
Called after launching a process.
static lldb::addr_t FindKernelAtLoadAddress(lldb_private::Process *process)
static llvm::StringRef GetPluginDescriptionStatic()
static lldb_private::UUID CheckForKernelImageAtAddress(lldb_private::Process *process, lldb::addr_t address, bool *read_error=nullptr)
static lldb_private::DynamicLoader * CreateInstance(lldb_private::Process *process, bool force)
static bool ReadELFHeader(lldb_private::Process *process, lldb::addr_t address, llvm::ELF::Elf32_Ehdr &header, bool *read_error=nullptr)
A section + offset based address class.
Definition Address.h:62
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition Address.cpp:303
lldb::addr_t GetFileAddress() const
Get the file address.
Definition Address.cpp:283
bool IsValid() const
Check if the object state is valid.
Definition Address.h:355
An architecture specification class.
Definition ArchSpec.h:32
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition ArchSpec.h:545
bool IsCompatibleMatch(const ArchSpec &rhs) const
Shorthand for IsMatch(rhs, CompatibleMatch).
Definition ArchSpec.h:597
A uniqued constant string class.
Definition ConstString.h:40
A class to manage flag bits.
Definition Debugger.h:100
lldb::StreamUP GetAsyncOutputStream()
A plug-in interface definition class for dynamic loaders.
void LoadOperatingSystemPlugin(bool flush)
DynamicLoader(Process *process)
Construct with a process.
A file utility class.
Definition FileSpec.h:56
static FileSystem & Instance()
A collection class for Module objects.
Definition ModuleList.h:125
bool AppendIfNeeded(const lldb::ModuleSP &new_module, bool notify=true)
Append a module to the module list, if it is not already there.
lldb::ModuleSP FindModule(const Module *module_ptr) const
ModuleIterable Modules() const
Definition ModuleList.h:571
FileSpec & GetFileSpec()
Definition ModuleSpec.h:57
A class that describes an executable image and its associated object and symbol files.
Definition Module.h:91
virtual ObjectFile * GetObjectFile()
Get the object file representation for the current architecture.
Definition Module.cpp:1179
A plug-in interface definition class for object file parsers.
Definition ObjectFile.h:46
@ eTypeExecutable
A normal executable.
Definition ObjectFile.h:55
@ eTypeObjectFile
An intermediate object file.
Definition ObjectFile.h:61
@ eTypeSharedLibrary
A shared library that can be used during execution.
Definition ObjectFile.h:63
virtual SectionList * GetSectionList(bool update_module_section_list=true)
Gets the section list for the currently selected architecture (and object for archives).
virtual lldb_private::Address GetBaseAddress()
Returns base address of this object file.
Definition ObjectFile.h:468
static bool DownloadObjectAndSymbolFile(ModuleSpec &module_spec, Status &error, bool force_lookup=true, bool copy_executable=true)
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
A plug-in interface definition class for debugging a process.
Definition Process.h:367
virtual size_t ReadMemory(const ProcessAddress &process_addr, void *buf, size_t size, Status &error)
Read of memory from a process.
Definition Process.cpp:2081
llvm::Expected< lldb::ModuleSP > ReadModuleFromMemory(const FileSpec &file_spec, lldb::addr_t header_addr, size_t size_to_read=512)
Creates and populates a module using an in-memory object file.
Definition Process.cpp:2840
virtual lldb::addr_t GetImageInfoAddress()
Get the image information address for the current process.
Definition Process.cpp:1504
void SetCanRunCode(bool can_run_code)
Sets whether executing code in this process is possible.
Definition Process.cpp:2806
uint32_t GetStopID() const
Definition Process.h:1513
Target & GetTarget()
Get the target object pointer for this module.
Definition Process.h:1266
size_t GetSize() const
Definition Section.h:77
lldb::SectionSP FindSectionByName(llvm::StringRef section_name) const
Definition Section.cpp:562
lldb::SectionSP GetSectionAtIndex(size_t idx) const
Definition Section.cpp:555
lldb::addr_t GetFileAddress() const
Definition Section.cpp:194
An error handling class.
Definition Status.h:118
static Status FromErrorString(const char *str)
Definition Status.h:141
Address GetAddress() const
Definition Symbol.h:98
Module * GetExecutableModulePointer()
Definition Target.cpp:1641
Debugger & GetDebugger() const
Definition Target.h:1337
lldb::ModuleSP GetOrCreateModule(const ModuleSpec &module_spec, bool notify, Status *error_ptr=nullptr)
Find a binary on the system and return its Module, or return an existing Module that is already in th...
Definition Target.cpp:2450
bool SetArchitecture(const ArchSpec &arch_spec, bool set_platform=false, bool merge=true)
Set the architecture for this target.
Definition Target.cpp:1787
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition Target.h:1254
const ArchSpec & GetArchitecture() const
Definition Target.h:1296
bool SetSectionLoadAddress(const lldb::SectionSP &section, lldb::addr_t load_addr, bool warn_multiple=false)
Definition Target.cpp:3506
Represents UUID's of various sizes.
Definition UUID.h:27
bool IsValid() const
Definition UUID.h:69
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:338
std::shared_ptr< lldb_private::ThreadPlan > ThreadPlanSP
std::shared_ptr< lldb_private::Section > SectionSP
uint64_t addr_t
Definition lldb-types.h:80
std::unique_ptr< lldb_private::Stream > StreamUP
std::shared_ptr< lldb_private::Module > ModuleSP