llvm-project/lldb/examples/python/gdb_disassemble.py
Med Ismail Bennani e3930e77fc [lldb] Update custom commands to always be overrriden
This is a follow-up patch to 6f7835f309b9.

As explained previously, when running from an IDE, it can happen that
the IDE imports some lldb scripts by itself. If the user also tries to
import these commands, lldb will show the following message:

```
error: cannot add command: user command exists and force replace not set
```

This message is confusing to the user, because it suggests that the
command import failed and that the execution should stop. However, in
this case, lldb will continue the execution with the command added
previously by the user.

To prevent that, this patch updates every first-party lldb-packaged
custom commands to override commands that were pre-imported in lldb.

Differential Revision: https://reviews.llvm.org/D140293

Signed-off-by: Med Ismail Bennani <medismail.bennani@gmail.com>
2023-01-12 19:20:51 -08:00

28 lines
1.1 KiB
Python
Executable File

import lldb
def disassemble(debugger, command, result, dict):
if lldb.frame.function:
instructions = lldb.frame.function.instructions
start_addr = lldb.frame.function.addr.load_addr
name = lldb.frame.function.name
elif lldb.frame.symbol:
instructions = lldb.frame.symbol.instructions
start_addr = lldb.frame.symbol.addr.load_addr
name = lldb.frame.symbol.name
for inst in instructions:
inst_addr = inst.addr.load_addr
inst_offset = inst_addr - start_addr
comment = inst.comment
if comment:
print("<%s + %-4u> 0x%x %8s %s ; %s" % (name, inst_offset, inst_addr, inst.mnemonic, inst.operands, comment))
else:
print("<%s + %-4u> 0x%x %8s %s" % (name, inst_offset, inst_addr, inst.mnemonic, inst.operands))
# Install the command when the module gets imported
def __lldb_init_module(debugger, internal_dict):
debugger.HandleCommand(
'command script add -o -f gdb_disassemble.disassemble gdb-disassemble')
print('Installed "gdb-disassemble" command for disassembly')