31#define LLDB_PROPERTIES_processfreebsdkernelcore
32#include "ProcessFreeBSDKernelCoreProperties.inc"
35#define LLDB_PROPERTIES_processfreebsdkernelcore
36#include "ProcessFreeBSDKernelCorePropertiesEnum.inc"
41 static llvm::StringRef GetSettingName() {
45 PluginProperties() : Properties() {
46 m_collection_sp = std::make_shared<OptionValueProperties>(GetSettingName());
47 m_collection_sp->Initialize(g_processfreebsdkernelcore_properties_def);
50 ~PluginProperties()
override =
default;
52 bool GetReadOnly()
const {
53 const uint32_t idx = ePropertyReadOnly;
54 return GetPropertyAtIndexAs<bool>(idx,
true);
61 static PluginProperties g_settings;
71 interpreter,
"process plugin refresh-threads",
72 "Refresh the thread list from the FreeBSD kernel core. The thread "
73 "list and related data structures may be being read from live "
74 "memory (/dev/mem), which may have changed since the last refresh. "
75 "This command clears LLDB's thread list and memory cache then "
76 "re-reads the kernel's allproc/zombie lists to rebuild the thread "
78 "process plugin refresh-threads",
79 eCommandRequiresProcess | eCommandTryTargetAPILock) {}
101 process->m_thread_list_real.Clear();
102 process->m_thread_list.Clear();
105 const uint32_t num_threads =
106 process->GetThreadList().GetSize(
true);
108 "Thread list refreshed, {0} thread{1} found.", num_threads,
109 num_threads == 1 ?
"" :
"s");
131 const FileSpec *crash_file,
bool can_connect) {
132 ModuleSP executable = target_sp->GetExecutableModule();
133 if (crash_file && !can_connect && executable) {
134 char errbuf[_POSIX2_LINE_MAX];
136 kvm_open2(executable->GetFileSpec().GetPath().c_str(),
137 crash_file->
GetPath().c_str(), O_RDONLY, errbuf,
nullptr);
140 return std::make_shared<ProcessFreeBSDKernelCore>(target_sp, listener_sp,
156 debugger, PluginProperties::GetSettingName())) {
157 const bool is_global_setting =
true;
160 "Properties for the freebsd-kernel process plug-in.",
170 bool plugin_specified_by_name) {
178 m_command_sp = std::make_unique<CommandObjectMultiword>(
179 interp,
"process plugin",
180 "Commands for the FreeBSD kernel process plug-in.",
181 "process plugin <subcommand> [<subcommand-options>]");
194 "ProcessFreeBSDKernelCore: no executable module set on target");
196 char errbuf[_POSIX2_LINE_MAX];
197 m_kvm = kvm_open2(executable->GetFileSpec().GetPath().c_str(),
198 GetCoreFile().GetPath().c_str(), O_RDWR, errbuf,
nullptr);
203 "ProcessFreeBSDKernelCore: kvm_open2 failed for core '%s' "
206 executable->GetFileSpec().GetPath().c_str());
237 const void *buf,
size_t size,
241 "Memory writes are currently disabled. You can enable them with "
242 "`settings set plugin.process.freebsd-kernel-core.read-only false`.");
247 rd = kvm_write(
m_kvm, addr, buf, size);
248 if (rd < 0 ||
static_cast<size_t>(rd) != size) {
251 return rd > 0 ? rd : 0;
258 if (old_thread_list.
GetSize(
false) == 0) {
265 switch (
GetTarget().GetArchitecture().GetMachine()) {
266 case llvm::Triple::arm:
267 case llvm::Triple::aarch64:
268 case llvm::Triple::ppc64le:
269 case llvm::Triple::riscv64:
270 case llvm::Triple::x86:
271 case llvm::Triple::x86_64:
287 int32_t offset_p_pid =
329 if (offset_p_list == -1 || offset_p_pid == -1 || offset_p_threads == -1 ||
330 offset_p_comm == -1 || offset_td_tid == -1 || offset_td_plist == -1 ||
331 offset_td_pcb == -1 || offset_td_oncpu == -1 || offset_td_name == -1)
354 uint32_t mp_maxid = 0;
365 uint32_t long_bit = long_size_bytes * 8;
367 if (
auto type_system_or_err =
373 long_size_bytes = *size;
374 long_bit = long_size_bytes * 8;
376 llvm::consumeError(type_system_or_err.takeError());
379 constexpr size_t fbsd_maxcomlen = 19;
389 std::vector<std::pair<lldb::addr_t, int32_t>> process_addrs;
397 process_addrs.emplace_back(proc, pid);
403 std::sort(process_addrs.begin(), process_addrs.end(),
404 [](
const auto &a,
const auto &b) { return a.second < b.second; });
406 for (
auto [proc, pid] : process_addrs) {
408 char comm[fbsd_maxcomlen + 1];
419 error.Success() && td != 0;
438 char thread_name[fbsd_maxcomlen + 1];
440 sizeof(thread_name),
error);
448 std::string thread_desc = llvm::formatv(
"(pid {0}) {1}", pid, comm);
449 if (*thread_name && strcmp(thread_name, comm)) {
451 thread_desc += thread_name;
458 if (tid == dumptid) {
461 thread_desc +=
" (crashed)";
462 }
else if (oncpu != -1) {
465 bool is_stopped =
false;
466 if (oncpu >= 0 &&
static_cast<uint32_t
>(oncpu) <= mp_maxid &&
468 uint32_t
bit = oncpu % long_bit;
469 uint32_t word = oncpu / long_bit;
470 lldb::addr_t mask_addr = stopped_cpus + word * long_size_bytes;
472 mask_addr, long_size_bytes, 0,
error);
474 is_stopped = (mask & (1ULL <<
bit)) != 0;
480 pcb_addr = stoppcbs + oncpu * pcbsize;
484 thread_desc += llvm::formatv(
" (on CPU {0})", oncpu);
491 thread->SetIsCrashedThread(
true);
501 const uint32_t num_threads = old_thread_list.
GetSize(
false);
502 for (uint32_t i = 0; i < num_threads; ++i)
505 return new_thread_list.
GetSize(
false) > 0;
511 rd = kvm_read2(
m_kvm, addr, buf, size);
512 if (rd < 0 ||
static_cast<size_t>(rd) != size) {
515 return rd > 0 ? rd : 0;
527 kssize_t displacement = kvm_kerndisp(
m_kvm);
529 if (displacement == 0)
534 if (!kernel_module_sp)
537 bool changed =
false;
538 kernel_module_sp->SetLoadAddress(target,
544 loaded_module_list.
Append(kernel_module_sp);
570 uint64_t offset_msg_ptr = 0;
571 uint64_t offset_msg_size = 0;
572 uint64_t offset_msg_wseq = 0;
573 uint64_t offset_msg_rseq = 0;
582 for (uint32_t i = 0; i < num_fields; i++) {
583 std::string field_name;
584 uint64_t field_offset = 0;
589 if (field_name ==
"msg_ptr") {
590 offset_msg_ptr = field_offset / 8;
592 }
else if (field_name ==
"msg_size") {
593 offset_msg_size = field_offset / 8;
595 }
else if (field_name ==
"msg_wseq") {
596 offset_msg_wseq = field_offset / 8;
598 }
else if (field_name ==
"msg_rseq") {
599 offset_msg_rseq = field_offset / 8;
604 if (field_found != 4) {
607 "FreeBSD-Kernel-Core: Could not find all required fields for msgbuf");
620 offset_msg_size = ptr_size + 4;
621 offset_msg_wseq = ptr_size + 8;
622 offset_msg_rseq = ptr_size + 12;
632 if (
error.Fail() || size == 0)
647 uint32_t rseq_pos = rseq % size;
648 uint32_t wseq_pos = wseq % size;
650 if (rseq_pos == wseq_pos)
658 stream_sp->PutCString(
"\nUnread portion of the kernel message buffer:\n");
661 if (rseq_pos < wseq_pos) {
663 size_t len = wseq_pos - rseq_pos;
664 std::string buf(len,
'\0');
666 if (
error.Success() && bytes_read > 0) {
667 buf.resize(bytes_read);
672 size_t len1 = size - rseq_pos;
673 std::string buf1(len1,
'\0');
674 size_t bytes_read1 =
ReadMemory(bufp + rseq_pos, &buf1[0], len1,
error);
675 if (
error.Success() && bytes_read1 > 0) {
676 buf1.resize(bytes_read1);
681 std::string buf2(wseq_pos,
'\0');
683 if (
error.Success() && bytes_read2 > 0) {
684 buf2.resize(bytes_read2);
690 stream_sp->PutChar(
'\n');
static llvm::raw_ostream & error(Stream &strm)
static PluginProperties & GetGlobalPluginProperties()
#define LLDB_LOGF(log,...)
#define LLDB_PLUGIN_DEFINE(PluginName)
static PluginProperties & GetGlobalPluginProperties()
void DoExecute(Args &command, CommandReturnObject &result) override
CommandObjectProcessFreeBSDKernelCoreRefreshThreads(CommandInterpreter &interpreter)
~CommandObjectProcessFreeBSDKernelCoreRefreshThreads() override=default
static llvm::StringRef GetPluginNameStatic()
lldb_private::Status DoDestroy() override
static llvm::StringRef GetPluginNameStatic()
void PrintUnreadMessage()
ProcessFreeBSDKernelCore(lldb::TargetSP target_sp, lldb::ListenerSP listener, const lldb_private::FileSpec &core_file)
lldb::addr_t FindSymbol(const char *name)
~ProcessFreeBSDKernelCore()
size_t DoReadMemory(lldb::addr_t addr, void *buf, size_t size, lldb_private::Status &error) override
Actually do the reading of memory from a process.
static lldb::ProcessSP CreateInstance(lldb::TargetSP target_sp, lldb::ListenerSP listener, const lldb_private::FileSpec *crash_file_path, bool can_connect)
void RefreshStateAfterStop() override
Currently called as part of ShouldStop.
static void DebuggerInitialize(lldb_private::Debugger &debugger)
static llvm::StringRef GetPluginDescriptionStatic()
lldb_private::Status DoLoadCore() override
lldb_private::CommandObject * GetPluginCommandObject() override
Return a multi-word command object that can be used to expose plug-in specific commands.
void SetKernelDisplacement()
friend class CommandObjectProcessFreeBSDKernelCoreRefreshThreads
lldb_private::DynamicLoader * GetDynamicLoader() override
Get the dynamic loader plug-in for this process.
size_t DoWriteMemory(lldb::addr_t addr, const void *buf, size_t size, lldb_private::Status &error) override
Actually do the writing of memory to a process.
bool DoUpdateThreadList(lldb_private::ThreadList &old_thread_list, lldb_private::ThreadList &new_thread_list) override
Update the thread list following process plug-in's specific logic.
bool m_printed_unread_message
bool CanDebug(lldb::TargetSP target_sp, bool plugin_specified_by_name) override
Check if a plug-in instance can debug the file in module.
std::unique_ptr< lldb_private::CommandObjectMultiword > m_command_sp
A command line argument class.
CommandObjectParsed(CommandInterpreter &interpreter, const char *name, const char *help=nullptr, const char *syntax=nullptr, uint32_t flags=0)
CommandInterpreter & m_interpreter
void SetStatus(lldb::ReturnStatus status)
void void AppendMessageWithFormatv(const char *format, Args &&...args)
Generic representation of a type in a programming language.
CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) const
Create related types using the current type's AST.
CompilerType GetFieldAtIndex(size_t idx, std::string &name, uint64_t *bit_offset_ptr, uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) const
llvm::Expected< uint64_t > GetByteSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bytes.
uint32_t GetNumFields() const
A uniqued constant string class.
CommandInterpreter & GetCommandInterpreter()
lldb::StreamUP GetAsyncOutputStream()
static DynamicLoader * FindPlugin(Process *process, llvm::StringRef plugin_name)
Find a dynamic loader plugin for a given process.
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
void Clear(bool clear_invalid_ranges=false)
A collection class for Module objects.
void FindTypes(Module *search_first, const TypeQuery &query, lldb_private::TypeResults &results) const
Find types using a type-matching object that contains all search parameters.
void Append(const lldb::ModuleSP &module_sp, bool notify=true)
Append a module to the module list.
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool CreateSettingForProcessPlugin(Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp, llvm::StringRef description, bool is_global_property)
static lldb::OptionValuePropertiesSP GetSettingForProcessPlugin(Debugger &debugger, llvm::StringRef setting_name)
static bool UnregisterPlugin(ABICreateInstance create_callback)
PostMortemProcess(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp, const FileSpec &core_file)
FileSpec GetCoreFile() const override
Provide a way to retrieve the core dump file that is loaded for debugging.
int64_t ReadSignedIntegerFromMemory(lldb::addr_t load_addr, size_t byte_size, int64_t fail_value, Status &error)
size_t ReadCStringFromMemory(lldb::addr_t vm_addr, char *cstr, size_t cstr_max_len, Status &error)
Read a NULL terminated C string from memory.
lldb::DynamicLoaderUP m_dyld_up
virtual size_t ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, Status &error)
Read of memory from a process.
uint64_t ReadUnsignedIntegerFromMemory(lldb::addr_t load_addr, size_t byte_size, uint64_t fail_value, Status &error)
Reads an unsigned integer of the specified byte size from process memory.
lldb::addr_t ReadPointerFromMemory(lldb::addr_t vm_addr, Status &error)
MemoryCache m_memory_cache
uint32_t GetAddressByteSize() const
virtual void Finalize(bool destructing)
This object is about to be destroyed, do any necessary cleanup.
ThreadList m_thread_list
The threads for this process as the user will see them.
friend class DynamicLoader
Target & GetTarget()
Get the target object pointer for this module.
lldb::OptionValuePropertiesSP GetValueProperties() const
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
static Status FromErrorString(const char *str)
lldb::addr_t GetLoadAddress(Target *target) const
void ModulesDidLoad(ModuleList &module_list)
This call may preload module symbols, and may do so in parallel depending on the following target set...
Debugger & GetDebugger() const
lldb::ModuleSP GetExecutableModule()
Gets the module for the main executable.
const ModuleList & GetImages() const
Get accessor for the images for this process.
void AddThread(const lldb::ThreadSP &thread_sp)
uint32_t GetSize(bool can_update=true)
lldb::ThreadSP GetThreadAtIndex(uint32_t idx, bool can_update=true)
lldb::TypeSP GetTypeAtIndex(uint32_t idx)
A class that contains all state required for type lookups.
This class tracks the state and results of a TypeQuery.
#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.
std::shared_ptr< lldb_private::Thread > ThreadSP
std::shared_ptr< lldb_private::CommandObject > CommandObjectSP
@ eLanguageTypeC
Non-standardized C, such as K&R.
std::shared_ptr< lldb_private::Stream > StreamSP
std::shared_ptr< lldb_private::Process > ProcessSP
@ eReturnStatusSuccessFinishResult
std::shared_ptr< lldb_private::Listener > ListenerSP
std::shared_ptr< lldb_private::Target > TargetSP
std::shared_ptr< lldb_private::Module > ModuleSP