[lldb] Delete two overloads of Disassembler::Disassemble

by "inlining" them into their single caller (CommandObjectDisassemble).
The functions mainly consist of long argument lists and defensive
checks. These become unnecessary after inlining, so the end result is
less code. Additionally, this makes the implementation of
CommandObjectDisassemble more uniform (first figure out what you're
going to disassemble, then actually do it), which enables further
cleanups.
This commit is contained in:
Pavel Labath
2020-03-04 14:17:50 +01:00
parent e258ad5129
commit c6a38957a7
3 changed files with 56 additions and 123 deletions

View File

@@ -410,20 +410,6 @@ public:
uint32_t num_mixed_context_lines, uint32_t options,
Stream &strm);
static size_t
Disassemble(Debugger &debugger, const ArchSpec &arch, const char *plugin_name,
const char *flavor, const ExecutionContext &exe_ctx,
SymbolContextList &sc_list, uint32_t num_instructions,
bool mixed_source_and_assembly, uint32_t num_mixed_context_lines,
uint32_t options, Stream &strm);
static bool
Disassemble(Debugger &debugger, const ArchSpec &arch, const char *plugin_name,
const char *flavor, const ExecutionContext &exe_ctx,
ConstString name, Module *module,
uint32_t num_instructions, bool mixed_source_and_assembly,
uint32_t num_mixed_context_lines, uint32_t options, Stream &strm);
static bool
Disassemble(Debugger &debugger, const ArchSpec &arch, const char *plugin_name,
const char *flavor, const ExecutionContext &exe_ctx,

View File

@@ -281,24 +281,35 @@ bool CommandObjectDisassemble::DoExecute(Args &command,
if (m_options.raw)
options |= Disassembler::eOptionRawOuput;
std::vector<AddressRange> ranges;
if (!m_options.func_name.empty()) {
ConstString name(m_options.func_name.c_str());
const bool include_symbols = true;
const bool include_inlines = true;
if (Disassembler::Disassemble(
GetDebugger(), m_options.arch, plugin_name, flavor_string,
m_exe_ctx, name,
nullptr, // Module *
m_options.num_instructions, m_options.show_mixed,
m_options.show_mixed ? m_options.num_lines_context : 0, options,
result.GetOutputStream())) {
result.SetStatus(eReturnStatusSuccessFinishResult);
} else {
// Find functions matching the given name.
SymbolContextList sc_list;
target->GetImages().FindFunctions(
name, eFunctionNameTypeAuto, include_symbols, include_inlines, sc_list);
AddressRange range;
const uint32_t scope =
eSymbolContextBlock | eSymbolContextFunction | eSymbolContextSymbol;
const bool use_inline_block_range = true;
for (SymbolContext sc : sc_list.SymbolContexts()) {
for (uint32_t range_idx = 0;
sc.GetAddressRange(scope, range_idx, use_inline_block_range, range);
++range_idx) {
ranges.push_back(range);
}
}
if (ranges.empty()) {
result.AppendErrorWithFormat("Unable to find symbol with name '%s'.\n",
name.GetCString());
result.SetStatus(eReturnStatusFailed);
return result.Succeeded();
}
} else {
std::vector<AddressRange> ranges;
AddressRange range;
StackFrame *frame = m_exe_ctx.GetFramePtr();
if (m_options.frame_line) {
@@ -444,45 +455,44 @@ bool CommandObjectDisassemble::DoExecute(Args &command,
}
ranges.push_back(range);
}
}
bool print_sc_header = ranges.size() > 1;
for (AddressRange cur_range : ranges) {
bool success;
if (m_options.num_instructions != 0) {
success = Disassembler::Disassemble(
GetDebugger(), m_options.arch, plugin_name, flavor_string,
m_exe_ctx, cur_range.GetBaseAddress(), m_options.num_instructions,
m_options.show_mixed,
m_options.show_mixed ? m_options.num_lines_context : 0, options,
result.GetOutputStream());
} else {
if (cur_range.GetByteSize() == 0)
cur_range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE);
bool print_sc_header = ranges.size() > 1;
for (AddressRange cur_range : ranges) {
bool success;
if (m_options.num_instructions != 0) {
success = Disassembler::Disassemble(
GetDebugger(), m_options.arch, plugin_name, flavor_string, m_exe_ctx,
cur_range.GetBaseAddress(), m_options.num_instructions,
m_options.show_mixed,
m_options.show_mixed ? m_options.num_lines_context : 0, options,
result.GetOutputStream());
} else {
if (cur_range.GetByteSize() == 0)
cur_range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE);
success = Disassembler::Disassemble(
GetDebugger(), m_options.arch, plugin_name, flavor_string,
m_exe_ctx, cur_range, m_options.num_instructions,
m_options.show_mixed,
m_options.show_mixed ? m_options.num_lines_context : 0, options,
result.GetOutputStream());
}
if (success) {
result.SetStatus(eReturnStatusSuccessFinishResult);
} else {
if (m_options.symbol_containing_addr != LLDB_INVALID_ADDRESS) {
result.AppendErrorWithFormat(
"Failed to disassemble memory in function at 0x%8.8" PRIx64 ".\n",
m_options.symbol_containing_addr);
} else {
result.AppendErrorWithFormat(
"Failed to disassemble memory at 0x%8.8" PRIx64 ".\n",
cur_range.GetBaseAddress().GetLoadAddress(target));
}
result.SetStatus(eReturnStatusFailed);
}
if (print_sc_header)
result.AppendMessage("\n");
success = Disassembler::Disassemble(
GetDebugger(), m_options.arch, plugin_name, flavor_string, m_exe_ctx,
cur_range, m_options.num_instructions, m_options.show_mixed,
m_options.show_mixed ? m_options.num_lines_context : 0, options,
result.GetOutputStream());
}
if (success) {
result.SetStatus(eReturnStatusSuccessFinishResult);
} else {
if (m_options.symbol_containing_addr != LLDB_INVALID_ADDRESS) {
result.AppendErrorWithFormat(
"Failed to disassemble memory in function at 0x%8.8" PRIx64 ".\n",
m_options.symbol_containing_addr);
} else {
result.AppendErrorWithFormat(
"Failed to disassemble memory at 0x%8.8" PRIx64 ".\n",
cur_range.GetBaseAddress().GetLoadAddress(target));
}
result.SetStatus(eReturnStatusFailed);
}
if (print_sc_header)
result.AppendMessage("\n");
}
return result.Succeeded();

View File

@@ -126,69 +126,6 @@ static void ResolveAddress(const ExecutionContext &exe_ctx, const Address &addr,
resolved_addr = addr;
}
size_t Disassembler::Disassemble(Debugger &debugger, const ArchSpec &arch,
const char *plugin_name, const char *flavor,
const ExecutionContext &exe_ctx,
SymbolContextList &sc_list,
uint32_t num_instructions,
bool mixed_source_and_assembly,
uint32_t num_mixed_context_lines,
uint32_t options, Stream &strm) {
size_t success_count = 0;
const size_t count = sc_list.GetSize();
SymbolContext sc;
AddressRange range;
const uint32_t scope =
eSymbolContextBlock | eSymbolContextFunction | eSymbolContextSymbol;
const bool use_inline_block_range = true;
for (size_t i = 0; i < count; ++i) {
if (!sc_list.GetContextAtIndex(i, sc))
break;
for (uint32_t range_idx = 0;
sc.GetAddressRange(scope, range_idx, use_inline_block_range, range);
++range_idx) {
if (Disassemble(debugger, arch, plugin_name, flavor, exe_ctx, range,
num_instructions, mixed_source_and_assembly,
num_mixed_context_lines, options, strm)) {
++success_count;
strm.EOL();
}
}
}
return success_count;
}
bool Disassembler::Disassemble(
Debugger &debugger, const ArchSpec &arch, const char *plugin_name,
const char *flavor, const ExecutionContext &exe_ctx, ConstString name,
Module *module, uint32_t num_instructions, bool mixed_source_and_assembly,
uint32_t num_mixed_context_lines, uint32_t options, Stream &strm) {
// If no name is given there's nothing to disassemble.
if (!name)
return false;
const bool include_symbols = true;
const bool include_inlines = true;
// Find functions matching the given name.
SymbolContextList sc_list;
if (module) {
module->FindFunctions(name, CompilerDeclContext(), eFunctionNameTypeAuto,
include_symbols, include_inlines, sc_list);
} else if (exe_ctx.GetTargetPtr()) {
exe_ctx.GetTargetPtr()->GetImages().FindFunctions(
name, eFunctionNameTypeAuto, include_symbols, include_inlines, sc_list);
}
// If no functions were found there's nothing to disassemble.
if (sc_list.IsEmpty())
return false;
return Disassemble(debugger, arch, plugin_name, flavor, exe_ctx, sc_list,
num_instructions, mixed_source_and_assembly,
num_mixed_context_lines, options, strm);
}
lldb::DisassemblerSP Disassembler::DisassembleRange(
const ArchSpec &arch, const char *plugin_name, const char *flavor,
const ExecutionContext &exe_ctx, const AddressRange &range,