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) {
135 kvm_open2(executable->GetFileSpec().GetPath().c_str(),
136 crash_file->
GetPath().c_str(), O_RDONLY,
nullptr,
nullptr);
139 return std::make_shared<ProcessFreeBSDKernelCore>(target_sp, listener_sp,
154 debugger, PluginProperties::GetSettingName())) {
155 const bool is_global_setting =
true;
158 "Properties for the freebsd-kernel process plug-in.",
168 bool plugin_specified_by_name) {
176 m_command_sp = std::make_unique<CommandObjectMultiword>(
177 interp,
"process plugin",
178 "Commands for the FreeBSD kernel process plug-in.",
179 "process plugin <subcommand> [<subcommand-options>]");
192 "ProcessFreeBSDKernelCore: no executable module set on target");
194 m_kvm = kvm_open2(executable->GetFileSpec().GetPath().c_str(),
195 GetCoreFile().GetPath().c_str(), O_RDWR,
nullptr,
nullptr);
199 "ProcessFreeBSDKernelCore: kvm_open2 failed for core '%s' "
202 executable->GetFileSpec().GetPath().c_str());
232 const void *buf,
size_t size,
236 "Memory writes are currently disabled. You can enable them with "
237 "`settings set plugin.process.freebsd-kernel-core.read-only false`.");
242 rd = kvm_write(
m_kvm, addr, buf, size);
243 if (rd < 0 ||
static_cast<size_t>(rd) != size) {
246 return rd > 0 ? rd : 0;
253 if (old_thread_list.
GetSize(
false) == 0) {
260 switch (
GetTarget().GetArchitecture().GetMachine()) {
261 case llvm::Triple::arm:
262 case llvm::Triple::aarch64:
263 case llvm::Triple::ppc64le:
264 case llvm::Triple::riscv64:
265 case llvm::Triple::x86:
266 case llvm::Triple::x86_64:
278 int32_t offset_p_pid =
297 if (offset_p_list == -1 || offset_p_pid == -1 || offset_p_threads == -1 ||
298 offset_p_comm == -1 || offset_td_tid == -1 || offset_td_plist == -1 ||
299 offset_td_pcb == -1 || offset_td_oncpu == -1 || offset_td_name == -1)
316 uint32_t mp_maxid = 0;
327 uint32_t long_bit = long_size_bytes * 8;
329 if (
auto type_system_or_err =
335 long_size_bytes = *size;
336 long_bit = long_size_bytes * 8;
338 llvm::consumeError(type_system_or_err.takeError());
341 constexpr size_t fbsd_maxcomlen = 19;
347 std::vector<lldb::addr_t> process_addrs;
353 process_addrs.push_back(proc);
358 for (
auto proc_it = process_addrs.rbegin(); proc_it != process_addrs.rend();
364 char comm[fbsd_maxcomlen + 1];
381 char thread_name[fbsd_maxcomlen + 1];
383 sizeof(thread_name),
error);
389 std::string thread_desc = llvm::formatv(
"(pid {0}) {1}", pid, comm);
390 if (*thread_name && strcmp(thread_name, comm)) {
392 thread_desc += thread_name;
399 if (tid == dumptid) {
402 thread_desc +=
" (crashed)";
403 }
else if (oncpu != -1) {
406 bool is_stopped =
false;
407 if (oncpu >= 0 &&
static_cast<uint32_t
>(oncpu) <= mp_maxid &&
409 uint32_t
bit = oncpu % long_bit;
410 uint32_t word = oncpu / long_bit;
411 lldb::addr_t mask_addr = stopped_cpus + word * long_size_bytes;
413 mask_addr, long_size_bytes, 0,
error);
415 is_stopped = (mask & (1ULL <<
bit)) != 0;
421 pcb_addr = stoppcbs + oncpu * pcbsize;
425 thread_desc += llvm::formatv(
" (on CPU {0})", oncpu);
432 thread->SetIsCrashedThread(
true);
438 const uint32_t num_threads = old_thread_list.
GetSize(
false);
439 for (uint32_t i = 0; i < num_threads; ++i)
442 return new_thread_list.
GetSize(
false) > 0;
448 rd = kvm_read2(
m_kvm, addr, buf, size);
449 if (rd < 0 ||
static_cast<size_t>(rd) != size) {
452 return rd > 0 ? rd : 0;
464 kssize_t displacement = kvm_kerndisp(
m_kvm);
466 if (displacement == 0)
471 if (!kernel_module_sp)
474 bool changed =
false;
475 kernel_module_sp->SetLoadAddress(target,
481 loaded_module_list.
Append(kernel_module_sp);
507 uint64_t offset_msg_ptr = 0;
508 uint64_t offset_msg_size = 0;
509 uint64_t offset_msg_wseq = 0;
510 uint64_t offset_msg_rseq = 0;
519 for (uint32_t i = 0; i < num_fields; i++) {
520 std::string field_name;
521 uint64_t field_offset = 0;
526 if (field_name ==
"msg_ptr") {
527 offset_msg_ptr = field_offset / 8;
529 }
else if (field_name ==
"msg_size") {
530 offset_msg_size = field_offset / 8;
532 }
else if (field_name ==
"msg_wseq") {
533 offset_msg_wseq = field_offset / 8;
535 }
else if (field_name ==
"msg_rseq") {
536 offset_msg_rseq = field_offset / 8;
541 if (field_found != 4) {
544 "FreeBSD-Kernel-Core: Could not find all required fields for msgbuf");
557 offset_msg_size = ptr_size + 4;
558 offset_msg_wseq = ptr_size + 8;
559 offset_msg_rseq = ptr_size + 12;
569 if (!
error.Success() || size == 0)
574 if (!
error.Success())
579 if (!
error.Success())
584 uint32_t rseq_pos = rseq % size;
585 uint32_t wseq_pos = wseq % size;
587 if (rseq_pos == wseq_pos)
595 stream_sp->PutCString(
"\nUnread portion of the kernel message buffer:\n");
598 if (rseq_pos < wseq_pos) {
600 size_t len = wseq_pos - rseq_pos;
601 std::string buf(len,
'\0');
603 if (
error.Success() && bytes_read > 0) {
604 buf.resize(bytes_read);
609 size_t len1 = size - rseq_pos;
610 std::string buf1(len1,
'\0');
611 size_t bytes_read1 =
ReadMemory(bufp + rseq_pos, &buf1[0], len1,
error);
612 if (
error.Success() && bytes_read1 > 0) {
613 buf1.resize(bytes_read1);
618 std::string buf2(wseq_pos,
'\0');
620 if (
error.Success() && bytes_read2 > 0) {
621 buf2.resize(bytes_read2);
627 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