Skip to content

Commit 545cda6

Browse files
authored
[lldb] Add flag to "settings show" to include default values (#153233)
Adds a `--defaults`/`-d` flag to `settings show`. This mode will _optionally_ show a setting's default value. In other words, this does not always print a default value for every setting. A default value is not shown when the current value _is_ the default. Note: some setting types do not print empty or invalid values. For these setting types, if the default value is empty or invalid, the same elision logic is applied to printing the default value.
1 parent ba5d487 commit 545cda6

23 files changed

+338
-50
lines changed

lldb/include/lldb/Interpreter/OptionValue.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "lldb/Utility/FileSpec.h"
1818
#include "lldb/Utility/FileSpecList.h"
1919
#include "lldb/Utility/Status.h"
20+
#include "lldb/Utility/Stream.h"
2021
#include "lldb/Utility/StringList.h"
2122
#include "lldb/Utility/UUID.h"
2223
#include "lldb/lldb-defines.h"
@@ -61,6 +62,7 @@ class OptionValue {
6162
eDumpOptionDescription = (1u << 3),
6263
eDumpOptionRaw = (1u << 4),
6364
eDumpOptionCommand = (1u << 5),
65+
eDumpOptionDefaultValue = (1u << 6),
6466
eDumpGroupValue = (eDumpOptionName | eDumpOptionType | eDumpOptionValue),
6567
eDumpGroupHelp =
6668
(eDumpOptionName | eDumpOptionType | eDumpOptionDescription),
@@ -338,6 +340,20 @@ class OptionValue {
338340
// DeepCopy to it. Inherit from Cloneable to avoid doing this manually.
339341
virtual lldb::OptionValueSP Clone() const = 0;
340342

343+
class DefaultValueFormat {
344+
public:
345+
DefaultValueFormat(Stream &stream) : stream(stream) {
346+
stream.PutCString(" (default: ");
347+
}
348+
~DefaultValueFormat() { stream.PutChar(')'); }
349+
350+
DefaultValueFormat(const DefaultValueFormat &) = delete;
351+
DefaultValueFormat &operator=(const DefaultValueFormat &) = delete;
352+
353+
private:
354+
Stream &stream;
355+
};
356+
341357
lldb::OptionValueWP m_parent_wp;
342358
std::function<void()> m_callback;
343359
bool m_value_was_set = false; // This can be used to see if a value has been

lldb/include/lldb/Interpreter/OptionValueEnumeration.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class OptionValueEnumeration
7272

7373
protected:
7474
void SetEnumerations(const OptionEnumValues &enumerators);
75+
void DumpEnum(Stream &strm, enum_type value);
7576

7677
enum_type m_current_value;
7778
enum_type m_default_value;

lldb/include/lldb/Utility/ArchSpec.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ class ArchSpec {
564564
/// \return true if \a lhs is less than \a rhs
565565
bool operator<(const ArchSpec &lhs, const ArchSpec &rhs);
566566
bool operator==(const ArchSpec &lhs, const ArchSpec &rhs);
567+
bool operator!=(const ArchSpec &lhs, const ArchSpec &rhs);
567568

568569
bool ParseMachCPUDashSubtypeTriple(llvm::StringRef triple_str, ArchSpec &arch);
569570

lldb/source/Commands/CommandObjectSettings.cpp

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,28 +237,62 @@ insert-before or insert-after.");
237237
};
238238

239239
// CommandObjectSettingsShow -- Show current values
240+
#define LLDB_OPTIONS_settings_show
241+
#include "CommandOptions.inc"
240242

241243
class CommandObjectSettingsShow : public CommandObjectParsed {
242244
public:
243245
CommandObjectSettingsShow(CommandInterpreter &interpreter)
244246
: CommandObjectParsed(interpreter, "settings show",
245247
"Show matching debugger settings and their current "
246-
"values. Defaults to showing all settings.",
247-
nullptr) {
248+
"values. Defaults to showing all settings.") {
248249
AddSimpleArgumentList(eArgTypeSettingVariableName, eArgRepeatOptional);
249250
}
250251

251252
~CommandObjectSettingsShow() override = default;
252253

254+
Options *GetOptions() override { return &m_options; }
255+
256+
class CommandOptions : public Options {
257+
public:
258+
~CommandOptions() override = default;
259+
260+
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
261+
ExecutionContext *execution_context) override {
262+
const int short_option = m_getopt_table[option_idx].val;
263+
switch (short_option) {
264+
case 'd':
265+
m_include_defaults = true;
266+
break;
267+
default:
268+
llvm_unreachable("Unimplemented option");
269+
}
270+
return {};
271+
}
272+
273+
void OptionParsingStarting(ExecutionContext *execution_context) override {
274+
m_include_defaults = false;
275+
}
276+
277+
llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
278+
return g_settings_show_options;
279+
}
280+
281+
bool m_include_defaults = false;
282+
};
283+
253284
protected:
254285
void DoExecute(Args &args, CommandReturnObject &result) override {
255286
result.SetStatus(eReturnStatusSuccessFinishResult);
256287

288+
uint32_t dump_mask = OptionValue::eDumpGroupValue;
289+
if (m_options.m_include_defaults)
290+
dump_mask |= OptionValue::eDumpOptionDefaultValue;
291+
257292
if (!args.empty()) {
258293
for (const auto &arg : args) {
259294
Status error(GetDebugger().DumpPropertyValue(
260-
&m_exe_ctx, result.GetOutputStream(), arg.ref(),
261-
OptionValue::eDumpGroupValue));
295+
&m_exe_ctx, result.GetOutputStream(), arg.ref(), dump_mask));
262296
if (error.Success()) {
263297
result.GetOutputStream().EOL();
264298
} else {
@@ -267,9 +301,12 @@ class CommandObjectSettingsShow : public CommandObjectParsed {
267301
}
268302
} else {
269303
GetDebugger().DumpAllPropertyValues(&m_exe_ctx, result.GetOutputStream(),
270-
OptionValue::eDumpGroupValue);
304+
dump_mask);
271305
}
272306
}
307+
308+
private:
309+
CommandOptions m_options;
273310
};
274311

275312
// CommandObjectSettingsWrite -- Write settings to file

lldb/source/Commands/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ let Command = "settings clear" in {
5656
Desc<"Clear all settings.">;
5757
}
5858

59+
let Command = "settings show" in {
60+
def setshow_defaults : Option<"defaults", "d">,
61+
Desc<"Include default values if defined.">;
62+
}
63+
5964
let Command = "breakpoint list" in {
6065
// FIXME: We need to add an "internal" command, and then add this sort of
6166
// thing to it. But I need to see it for now, and don't want to wait.

lldb/source/Interpreter/OptionValueArch.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "lldb/DataFormatters/FormatManager.h"
1212
#include "lldb/Interpreter/CommandCompletions.h"
1313
#include "lldb/Interpreter/CommandInterpreter.h"
14+
#include "lldb/Interpreter/OptionValue.h"
1415
#include "lldb/Utility/Args.h"
1516
#include "lldb/Utility/State.h"
1617

@@ -30,6 +31,12 @@ void OptionValueArch::DumpValue(const ExecutionContext *exe_ctx, Stream &strm,
3031
if (arch_name)
3132
strm.PutCString(arch_name);
3233
}
34+
35+
if (dump_mask & eDumpOptionDefaultValue &&
36+
m_current_value != m_default_value && m_default_value.IsValid()) {
37+
DefaultValueFormat label(strm);
38+
strm.PutCString(m_default_value.GetArchitectureName());
39+
}
3340
}
3441
}
3542

lldb/source/Interpreter/OptionValueArray.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "lldb/Interpreter/OptionValueArray.h"
1010

11+
#include "lldb/Interpreter/OptionValue.h"
1112
#include "lldb/Utility/Args.h"
1213
#include "lldb/Utility/Stream.h"
1314

@@ -27,8 +28,15 @@ void OptionValueArray::DumpValue(const ExecutionContext *exe_ctx, Stream &strm,
2728
if (dump_mask & eDumpOptionValue) {
2829
const bool one_line = dump_mask & eDumpOptionCommand;
2930
const uint32_t size = m_values.size();
30-
if (dump_mask & eDumpOptionType)
31-
strm.Printf(" =%s", (m_values.size() > 0 && !one_line) ? "\n" : "");
31+
if (dump_mask & (eDumpOptionType | eDumpOptionDefaultValue)) {
32+
strm.PutCString(" =");
33+
if (dump_mask & eDumpOptionDefaultValue && !m_values.empty()) {
34+
DefaultValueFormat label(strm);
35+
strm.PutCString("empty");
36+
}
37+
if (!m_values.empty() && !one_line)
38+
strm.PutCString("\n");
39+
}
3240
if (!one_line)
3341
strm.IndentMore();
3442
for (uint32_t i = 0; i < size; ++i) {

lldb/source/Interpreter/OptionValueBoolean.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "lldb/Host/PosixApi.h"
1212
#include "lldb/Interpreter/OptionArgParser.h"
13+
#include "lldb/Interpreter/OptionValue.h"
1314
#include "lldb/Utility/Stream.h"
1415
#include "lldb/Utility/StringList.h"
1516
#include "llvm/ADT/STLExtras.h"
@@ -27,6 +28,11 @@ void OptionValueBoolean::DumpValue(const ExecutionContext *exe_ctx,
2728
if (dump_mask & eDumpOptionType)
2829
strm.PutCString(" = ");
2930
strm.PutCString(m_current_value ? "true" : "false");
31+
if (dump_mask & eDumpOptionDefaultValue &&
32+
m_current_value != m_default_value) {
33+
DefaultValueFormat label(strm);
34+
strm.PutCString(m_default_value ? "true" : "false");
35+
}
3036
}
3137
}
3238

lldb/source/Interpreter/OptionValueChar.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,21 @@
99
#include "lldb/Interpreter/OptionValueChar.h"
1010

1111
#include "lldb/Interpreter/OptionArgParser.h"
12+
#include "lldb/Interpreter/OptionValue.h"
1213
#include "lldb/Utility/Stream.h"
1314
#include "lldb/Utility/StringList.h"
1415
#include "llvm/ADT/STLExtras.h"
1516

1617
using namespace lldb;
1718
using namespace lldb_private;
1819

20+
static void DumpChar(Stream &strm, char value) {
21+
if (value != '\0')
22+
strm.PutChar(value);
23+
else
24+
strm.PutCString("(null)");
25+
}
26+
1927
void OptionValueChar::DumpValue(const ExecutionContext *exe_ctx, Stream &strm,
2028
uint32_t dump_mask) {
2129
if (dump_mask & eDumpOptionType)
@@ -24,10 +32,12 @@ void OptionValueChar::DumpValue(const ExecutionContext *exe_ctx, Stream &strm,
2432
if (dump_mask & eDumpOptionValue) {
2533
if (dump_mask & eDumpOptionType)
2634
strm.PutCString(" = ");
27-
if (m_current_value != '\0')
28-
strm.PutChar(m_current_value);
29-
else
30-
strm.PutCString("(null)");
35+
DumpChar(strm, m_current_value);
36+
if (dump_mask & eDumpOptionDefaultValue &&
37+
m_current_value != m_default_value) {
38+
DefaultValueFormat label(strm);
39+
DumpChar(strm, m_default_value);
40+
}
3141
}
3242
}
3343

lldb/source/Interpreter/OptionValueDictionary.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "lldb/Interpreter/OptionValueDictionary.h"
1010

1111
#include "lldb/DataFormatters/FormatManager.h"
12+
#include "lldb/Interpreter/OptionValue.h"
1213
#include "lldb/Interpreter/OptionValueEnumeration.h"
1314
#include "lldb/Interpreter/OptionValueString.h"
1415
#include "lldb/Utility/Args.h"
@@ -30,8 +31,13 @@ void OptionValueDictionary::DumpValue(const ExecutionContext *exe_ctx,
3031
}
3132
if (dump_mask & eDumpOptionValue) {
3233
const bool one_line = dump_mask & eDumpOptionCommand;
33-
if (dump_mask & eDumpOptionType)
34+
if (dump_mask & (eDumpOptionType | eDumpOptionDefaultValue)) {
3435
strm.PutCString(" =");
36+
if (dump_mask & eDumpOptionDefaultValue && !m_values.empty()) {
37+
DefaultValueFormat label(strm);
38+
strm.PutCString("empty");
39+
}
40+
}
3541

3642
if (!one_line)
3743
strm.IndentMore();

0 commit comments

Comments
 (0)