-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[lldb] Support disassembling RISC-V proprietary instructions #145793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
1a7ee42
eee2048
2fe2115
1ec2424
2966de6
afedb0f
9cf0fa3
63f417c
c58c4da
1a4c94a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| """ | ||
| Defines a command, fdis, that does filtered disassembly. The command does the | ||
| lldb disassemble command with -b and any other arguments passed in, and | ||
| pipes that through a provided filter program. | ||
| The intention is to support disassembly of RISC-V proprietary instructions. | ||
| This is handled with llvm-objdump by piping the output of llvm-objdump through | ||
| a filter program. This script is intended to mimic that workflow. | ||
| """ | ||
|
|
||
| import lldb | ||
| import subprocess | ||
|
|
||
| filter_program = "crustfilt" | ||
|
|
||
| def __lldb_init_module(debugger, dict): | ||
| debugger.HandleCommand( | ||
| 'command script add -f filter_disasm.fdis fdis') | ||
| print("Disassembly filter command (fdis) loaded") | ||
| print("Filter program set to %s" % filter_program) | ||
|
|
||
|
|
||
| def fdis(debugger, args, result, dict): | ||
| """ | ||
| Call the built in disassembler, then pass its output to a filter program | ||
| to add in disassembly for hidden opcodes. | ||
| Except for get and set, use the fdis command like the disassemble command. | ||
| By default, the filter program is crustfilt, from | ||
| https://github.com/quic/crustfilt . This can be changed by changing | ||
| the global variable filter_program. | ||
| Usage: | ||
| fdis [[get] [set <program>] [<disassembly options>]] | ||
| Choose one of the following: | ||
| get | ||
| Gets the current filter program | ||
| set <program> | ||
| Sets the current filter program. This can be an executable, which | ||
| will be found on PATH, or an absolute path. | ||
| <disassembly options> | ||
| If the first argument is not get or set, the args will be passed | ||
| to the disassemble command as is. | ||
| """ | ||
|
|
||
| global filter_program | ||
| args_list = args.split(' ') | ||
| result.Clear() | ||
|
|
||
| if len(args_list) == 1 and args_list[0] == 'get': | ||
| result.PutCString(filter_program) | ||
| result.SetStatus(lldb.eReturnStatusSuccessFinishResult) | ||
| return | ||
|
|
||
| if len(args_list) == 2 and args_list[0] == 'set': | ||
| filter_program = args_list[1] | ||
| result.PutCString("Filter program set to %s" % filter_program) | ||
| result.SetStatus(lldb.eReturnStatusSuccessFinishResult) | ||
| return | ||
|
|
||
| res = lldb.SBCommandReturnObject() | ||
| debugger.GetCommandInterpreter().HandleCommand('disassemble -b ' + args, res) | ||
| if (len(res.GetError()) > 0): | ||
| result.SetError(res.GetError()) | ||
| result.SetStatus(lldb.eReturnStatusFailed) | ||
| return | ||
| output = res.GetOutput() | ||
|
|
||
| try: | ||
| proc = subprocess.run([filter_program], capture_output=True, text=True, input=output) | ||
| except (subprocess.SubprocessError, OSError) as e: | ||
| result.PutCString("Error occurred. Original disassembly:\n\n" + output) | ||
| result.SetError(str(e)) | ||
| result.SetStatus(lldb.eReturnStatusFailed) | ||
| return | ||
|
|
||
| print(proc.stderr) | ||
| if proc.stderr: | ||
| pass | ||
| #result.SetError(proc.stderr) | ||
| #result.SetStatus(lldb.eReturnStatusFailed) | ||
|
||
| else: | ||
| result.PutCString(proc.stdout) | ||
| result.SetStatus(lldb.eReturnStatusSuccessFinishResult) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -658,8 +658,13 @@ void Instruction::Dump(lldb_private::Stream *s, uint32_t max_opcode_byte_size, | |
| // the byte dump to be able to always show 15 bytes (3 chars each) plus a | ||
| // space | ||
| if (max_opcode_byte_size > 0) | ||
| m_opcode.Dump(&ss, max_opcode_byte_size * 3 + 1); | ||
| else | ||
| // make RISC-V opcode dump look like llvm-objdump | ||
DavidSpickett marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (exe_ctx && | ||
| exe_ctx->GetTargetSP()->GetArchitecture().GetTriple().isRISCV()) | ||
| m_opcode.DumpRISCV(&ss, max_opcode_byte_size * 3 + 1); | ||
|
||
| else | ||
| m_opcode.Dump(&ss, max_opcode_byte_size * 3 + 1); | ||
| else | ||
| m_opcode.Dump(&ss, 15 * 3 + 1); | ||
| } else { | ||
| // Else, we have ARM or MIPS which can show up to a uint32_t 0x00000000 | ||
|
|
@@ -685,10 +690,13 @@ void Instruction::Dump(lldb_private::Stream *s, uint32_t max_opcode_byte_size, | |
| } | ||
| } | ||
| const size_t opcode_pos = ss.GetSizeOfLastLine(); | ||
| const std::string &opcode_name = | ||
| std::string &opcode_name = | ||
| show_color ? m_markup_opcode_name : m_opcode_name; | ||
| const std::string &mnemonics = show_color ? m_markup_mnemonics : m_mnemonics; | ||
|
|
||
| if (opcode_name.empty()) | ||
| opcode_name = "<unknown>"; | ||
|
|
||
| // The default opcode size of 7 characters is plenty for most architectures | ||
| // but some like arm can pull out the occasional vqrshrun.s16. We won't get | ||
| // consistent column spacing in these cases, unfortunately. Also note that we | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,6 +78,44 @@ lldb::ByteOrder Opcode::GetDataByteOrder() const { | |
| return eByteOrderInvalid; | ||
| } | ||
|
|
||
| // make RISC-V byte dumps look like llvm-objdump, instead of just dumping bytes | ||
| int Opcode::DumpRISCV(Stream *s, uint32_t min_byte_width) { | ||
| const uint32_t previous_bytes = s->GetWrittenBytes(); | ||
| // if m_type is not bytes, call Dump | ||
| if (m_type != Opcode::eTypeBytes) | ||
| return Dump(s, min_byte_width); | ||
|
|
||
| // from RISCVPrettyPrinter in llvm-objdump.cpp | ||
| // if size % 4 == 0, print as 1 or 2 32 bit values (32 or 64 bit inst) | ||
| // else if size % 2 == 0, print as 1 or 3 16 bit values (16 or 48 bit inst) | ||
| // else fall back and print bytes | ||
|
||
| for (uint32_t i = 0; i < m_data.inst.length;) { | ||
| if (i > 0) | ||
| s->PutChar(' '); | ||
| if (!(m_data.inst.length % 4)) { | ||
| s->Printf("%2.2x%2.2x%2.2x%2.2x", m_data.inst.bytes[i + 3], | ||
| m_data.inst.bytes[i + 2], | ||
| m_data.inst.bytes[i + 1], | ||
| m_data.inst.bytes[i + 0]); | ||
| i += 4; | ||
| } else if (!(m_data.inst.length % 2)) { | ||
| s->Printf("%2.2x%2.2x", m_data.inst.bytes[i + 1], | ||
| m_data.inst.bytes[i + 0]); | ||
| i += 2; | ||
| } else { | ||
| s->Printf("%2.2x", m_data.inst.bytes[i]); | ||
| ++i; | ||
| } | ||
| } | ||
|
|
||
| uint32_t bytes_written_so_far = s->GetWrittenBytes() - previous_bytes; | ||
| // Add spaces to make sure bytes display comes out even in case opcodes aren't | ||
| // all the same size. | ||
| if (bytes_written_so_far < min_byte_width) | ||
| s->Printf("%*s", min_byte_width - bytes_written_so_far, ""); | ||
| return s->GetWrittenBytes() - previous_bytes; | ||
| } | ||
|
|
||
| uint32_t Opcode::GetData(DataExtractor &data) const { | ||
| uint32_t byte_size = GetByteSize(); | ||
| uint8_t swap_buf[8]; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.