1 //===-- SaveCoreOptions.cpp -------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Symbol/SaveCoreOptions.h"
10 #include "lldb/Core/PluginManager.h"
11 #include "lldb/Target/Process.h"
12 #include "lldb/Target/Thread.h"
15 using namespace lldb_private
;
17 Status
SaveCoreOptions::SetPluginName(const char *name
) {
19 if (!name
|| !name
[0]) {
20 m_plugin_name
= std::nullopt
;
24 if (!PluginManager::IsRegisteredObjectFilePluginName(name
)) {
25 return Status::FromErrorStringWithFormat(
26 "plugin name '%s' is not a valid ObjectFile plugin name", name
);
34 void SaveCoreOptions::SetStyle(lldb::SaveCoreStyle style
) { m_style
= style
; }
36 void SaveCoreOptions::SetOutputFile(FileSpec file
) { m_file
= file
; }
38 std::optional
<std::string
> SaveCoreOptions::GetPluginName() const {
42 lldb::SaveCoreStyle
SaveCoreOptions::GetStyle() const {
43 return m_style
.value_or(lldb::eSaveCoreUnspecified
);
46 const std::optional
<lldb_private::FileSpec
>
47 SaveCoreOptions::GetOutputFile() const {
51 Status
SaveCoreOptions::SetProcess(lldb::ProcessSP process_sp
) {
54 ClearProcessSpecificData();
59 if (!process_sp
->IsValid()) {
60 error
= Status::FromErrorString("Cannot assign an invalid process.");
64 // Don't clear any process specific data if the process is the same.
65 if (m_process_sp
== process_sp
)
68 ClearProcessSpecificData();
69 m_process_sp
= process_sp
;
73 Status
SaveCoreOptions::AddThread(lldb::ThreadSP thread_sp
) {
76 error
= Status::FromErrorString("invalid thread");
81 if (m_process_sp
!= thread_sp
->GetProcess()) {
82 error
= Status::FromErrorString(
83 "Cannot add a thread from a different process.");
87 m_process_sp
= thread_sp
->GetProcess();
90 m_threads_to_save
.insert(thread_sp
->GetID());
94 bool SaveCoreOptions::RemoveThread(lldb::ThreadSP thread_sp
) {
95 return thread_sp
&& m_threads_to_save
.erase(thread_sp
->GetID()) > 0;
98 bool SaveCoreOptions::ShouldThreadBeSaved(lldb::tid_t tid
) const {
99 // If the user specified no threads to save, then we save all threads.
100 if (m_threads_to_save
.empty())
102 return m_threads_to_save
.count(tid
) > 0;
105 bool SaveCoreOptions::HasSpecifiedThreads() const {
106 return !m_threads_to_save
.empty();
109 void SaveCoreOptions::AddMemoryRegionToSave(
110 const lldb_private::MemoryRegionInfo
®ion
) {
111 m_regions_to_save
.Insert(region
.GetRange(), /*combine=*/true);
114 const MemoryRanges
&SaveCoreOptions::GetCoreFileMemoryRanges() const {
115 return m_regions_to_save
;
118 Status
SaveCoreOptions::EnsureValidConfiguration(
119 lldb::ProcessSP process_sp
) const {
121 std::string error_str
;
122 if (!m_threads_to_save
.empty() && GetStyle() == lldb::eSaveCoreFull
)
123 error_str
+= "Cannot save a full core with a subset of threads\n";
125 if (m_process_sp
&& m_process_sp
!= process_sp
)
126 error_str
+= "Cannot save core for process using supplied core options. "
127 "Options were constructed targeting a different process. \n";
129 if (!error_str
.empty())
130 error
= Status(error_str
);
135 void SaveCoreOptions::ClearProcessSpecificData() {
136 // Deliberately not following the formatter style here to indicate that
137 // this method will be expanded in the future.
138 m_threads_to_save
.clear();
141 void SaveCoreOptions::Clear() {
142 m_file
= std::nullopt
;
143 m_plugin_name
= std::nullopt
;
144 m_style
= std::nullopt
;
145 m_threads_to_save
.clear();
146 m_process_sp
.reset();
147 m_regions_to_save
.Clear();