Skip to content

instruction ¤

Classes:

Functions:

Attributes:

UNCONDITIONAL_JUMP_INSTRUCTIONS module-attribute ¤

UNCONDITIONAL_JUMP_INSTRUCTIONS: dict[int, set[int]] = {
    CS_ARCH_X86: {X86_INS_JMP},
    CS_ARCH_MIPS: {
        MIPS_INS_J,
        MIPS_INS_JR,
        MIPS_INS_JAL,
        MIPS_INS_JALR,
        MIPS_INS_JALR_HB,
        MIPS_INS_BAL,
        MIPS_INS_ALIAS_BAL,
        MIPS_INS_B,
        MIPS_INS_ALIAS_B,
    },
    CS_ARCH_SPARC: {SPARC_INS_JMP, SPARC_INS_JMPL},
    CS_ARCH_ARM: {ARM_INS_TBB, ARM_INS_TBH},
    CS_ARCH_AARCH64: {AARCH64_INS_BL, AARCH64_INS_BLR, AARCH64_INS_BR},
    CS_ARCH_RISCV: {
        RISCV_INS_JAL,
        RISCV_INS_JALR,
        RISCV_INS_C_JAL,
        RISCV_INS_C_JALR,
        RISCV_INS_C_J,
        RISCV_INS_C_JR,
    },
    CS_ARCH_PPC: {PPC_INS_B, PPC_INS_BA, PPC_INS_BL, PPC_INS_BLA},
    CS_ARCH_SYSTEMZ: {SYSTEMZ_INS_B, SYSTEMZ_INS_BAL, SYSTEMZ_INS_BALR},
    CS_ARCH_LOONGARCH: {
        LOONGARCH_INS_B,
        LOONGARCH_INS_BL,
        LOONGARCH_INS_JIRL,
        LOONGARCH_INS_ALIAS_JR,
    },
}
BRANCH_AND_LINK_INSTRUCTIONS: dict[int, set[int]] = defaultdict(set)

GENERIC_UNCONDITIONAL_JUMP_GROUPS module-attribute ¤

GENERIC_UNCONDITIONAL_JUMP_GROUPS = {CS_GRP_CALL, CS_GRP_RET, CS_GRP_IRET}

GENERIC_JUMP_GROUPS module-attribute ¤

GENERIC_JUMP_GROUPS = {CS_GRP_JUMP, CS_GRP_BRANCH_RELATIVE}

ALL_JUMP_GROUPS module-attribute ¤

FORWARD_JUMP_GROUP module-attribute ¤

FORWARD_JUMP_GROUP = {CS_GRP_CALL} | GENERIC_JUMP_GROUPS

CAPSTONE_ARCH_MAPPING_STRING module-attribute ¤

CAPSTONE_ARCH_MAPPING_STRING = {
    CS_ARCH_ARM: "arm",
    CS_ARCH_AARCH64: "aarch64",
    CS_ARCH_X86: "x86",
    CS_ARCH_PPC: "powerpc",
    CS_ARCH_MIPS: "mips",
    CS_ARCH_SPARC: "sparc",
    CS_ARCH_RISCV: "RISCV",
    CS_ARCH_SYSTEMZ: "s390x",
    CS_ARCH_LOONGARCH: "loongarch",
}

InstructionCondition ¤

Bases: Enum

Attributes:

TRUE class-attribute instance-attribute ¤

TRUE = 1

FALSE class-attribute instance-attribute ¤

FALSE = 2

UNDETERMINED class-attribute instance-attribute ¤

UNDETERMINED = 3

SplitType ¤

Bases: Enum

Attributes:

NO_SPLIT class-attribute instance-attribute ¤

NO_SPLIT = 1

BRANCH_TAKEN class-attribute instance-attribute ¤

BRANCH_TAKEN = 2

BRANCH_NOT_TAKEN class-attribute instance-attribute ¤

BRANCH_NOT_TAKEN = 3

PwndbgInstruction ¤

Bases: Protocol

Methods:

Attributes:

cs_insn instance-attribute ¤

cs_insn: CsInsn

address instance-attribute ¤

address: int

size instance-attribute ¤

size: int

mnemonic instance-attribute ¤

mnemonic: str

op_str instance-attribute ¤

op_str: str

groups instance-attribute ¤

groups: set[int]

id instance-attribute ¤

id: int

operands instance-attribute ¤

operands: list[EnhancedOperand]

asm_string instance-attribute ¤

asm_string: str

next instance-attribute ¤

next: int

target instance-attribute ¤

target: int

target_string instance-attribute ¤

target_string: str | None

target_const instance-attribute ¤

target_const: bool | None

condition instance-attribute ¤

declare_conditional instance-attribute ¤

declare_conditional: bool | None

declare_is_unconditional_jump instance-attribute ¤

declare_is_unconditional_jump: bool

force_unconditional_jump_target instance-attribute ¤

force_unconditional_jump_target: bool

annotation instance-attribute ¤

annotation: str | None

annotation_padding instance-attribute ¤

annotation_padding: int | None

syscall instance-attribute ¤

syscall: int | None

syscall_name instance-attribute ¤

syscall_name: str | None

causes_branch_delay instance-attribute ¤

causes_branch_delay: bool

split instance-attribute ¤

split: SplitType

emulated instance-attribute ¤

emulated: bool

call_like property ¤

call_like: bool

jump_like property ¤

jump_like: bool

has_jump_target property ¤

has_jump_target: bool

is_conditional_jump property ¤

is_conditional_jump: bool

is_unconditional_jump property ¤

is_unconditional_jump: bool

is_conditional_jump_taken property ¤

is_conditional_jump_taken: bool

bytes property ¤

bytes: bytearray

op_find ¤

op_find(op_type: int, position: int) -> EnhancedOperand

op_count ¤

op_count(op_type: int) -> int

PwndbgInstructionImpl ¤

PwndbgInstructionImpl(cs_insn: CsInsn)

Bases: PwndbgInstruction

Methods:

Attributes:

  • cs_insn (CsInsn) –

    The underlying Capstone instruction object.

  • address (int) –
  • size (int) –

    Length of the instruction

  • mnemonic (str) –

    Ex: 'MOV'

  • op_str (str) –

    Ex: 'RAX, RDX'

  • groups (set[int]) –

    Capstone instruction groups that we belong to.

  • id (int) –

    The underlying Capstone ID for the instruction

  • operands (list[EnhancedOperand]) –
  • asm_string (str) –

    The full string representing the instruction - mov rdi, rsp with appropriate padding.

  • next (int) –

    This is the address that the instruction pointer will be set to after using the "nexti" GDB command.

  • target (int) –

    This is target of instructions that change the PC, regardless of if it's conditional or not,

  • target_string (str | None) –

    String representation of the target address.

  • target_const (bool | None) –

    Whether the target is a constant expression

  • condition (InstructionCondition) –

    Does the condition that the instruction checks for pass?

  • declare_conditional (bool | None) –

    This field is used to declare if the instruction is a conditional instruction.

  • declare_is_unconditional_jump (bool) –

    This field is used to declare that this instruction is an unconditional jump.

  • force_unconditional_jump_target (bool) –

    This asserts that the .target attribute is the real target of the instruction.

  • annotation (str | None) –

    The string is set in the "DisassemblyAssistant.enhance" function.

  • annotation_padding (int | None) –

    The left adjustment padding that was used to previously print this.

  • syscall (int | None) –

    The syscall number for this instruction, if it is a syscall. Otherwise None.

  • syscall_name (str | None) –

    The syscall name as a string

  • causes_branch_delay (bool) –

    Whether or not this instruction has a single branch delay slot

  • split (SplitType) –

    The type of split in the disasm display this instruction causes:

  • emulated (bool) –

    If the enhancement successfully used emulation for this instruction

  • call_like (bool) –

    True if this is a call-like instruction, meaning either it's a CALL or a branch and link.

  • jump_like (bool) –

    True if this instruction is "jump-like", such as a JUMP, CALL, or RET.

  • has_jump_target (bool) –

    True if we have determined that this instruction can explicitly change the program counter, and

  • is_conditional_jump (bool) –

    True if this instruction can change the program counter conditionally.

  • is_unconditional_jump (bool) –

    True if we know the instruction can change the program counter, and does so unconditionally.

  • is_conditional_jump_taken (bool) –

    True if this is a conditional jump, and we predicted that we will take the jump

  • bytes (bytearray) –

    Raw machine instruction bytes

cs_insn instance-attribute ¤

cs_insn: CsInsn = cs_insn

The underlying Capstone instruction object. Only the enhancement code should access the 'cs_insn' property

address instance-attribute ¤

address: int = address

size instance-attribute ¤

size: int = size

Length of the instruction

mnemonic instance-attribute ¤

mnemonic: str = mnemonic

Ex: 'MOV'

op_str instance-attribute ¤

op_str: str = op_str

Ex: 'RAX, RDX'

groups instance-attribute ¤

groups: set[int] = set(groups)

Capstone instruction groups that we belong to. Groups that apply to all architectures: CS_GRP_INVALID | CS_GRP_JUMP | CS_GRP_CALL | CS_GRP_RET | CS_GRP_INT | CS_GRP_IRET | CS_GRP_PRIVILEGE | CS_GRP_BRANCH_RELATIVE

id instance-attribute ¤

id: int = alias_id if is_alias else id

The underlying Capstone ID for the instruction If it's an alias, use the id of the alias

Examples: X86_INS_JMP, X86_INS_CALL, RISCV_INS_C_JAL

operands instance-attribute ¤

operands: list[EnhancedOperand] = [EnhancedOperand(op) for op in operands]

asm_string instance-attribute ¤

asm_string: str = f'{mnemonic} {op_str}'

The full string representing the instruction - mov rdi, rsp with appropriate padding.

This is syntax highlighted during enhancement.

This is additionally modified during enhancement for the purposes of replacing immediate values with their corresponding symbols

next instance-attribute ¤

next: int = address + size

This is the address that the instruction pointer will be set to after using the "nexti" GDB command. This means it is the address of the next instruction to be executed in all cases except "call" instructions.

Typically, it is self.address + self.size (the next instruction in memory)

If it is a jump and we know it is taken, then it is the value of the jump target.

Not set to "call" instruction targets, to indicate we will eventually (probably) return to this address

target instance-attribute ¤

target: int = None

This is target of instructions that change the PC, regardless of if it's conditional or not, and whether or not we take the jump. This includes "call" and all other instructions that set the PC

If the instruction is not one that changes the PC, target is set to "next"

target_string instance-attribute ¤

target_string: str | None = None

String representation of the target address.

Colorized symbol if a symbol exists at address, else colorized address

target_const instance-attribute ¤

target_const: bool | None = None

Whether the target is a constant expression

condition instance-attribute ¤

Does the condition that the instruction checks for pass?

For example, "JNE" jumps if Zero Flag is 0, else it does nothing. "CMOVA" conditionally performs a move depending on a flag. See 'condition' function in pwndbg.aglib.disasm.x86 for example on setting this.

UNDETERMINED if we cannot reason about the condition, or if the instruction always executes unconditionally (most instructions).

TRUE if the instruction has a conditional action, and we determine it is taken.

FALSE if the instruction has a conditional action, and we know it is not taken.

declare_conditional instance-attribute ¤

declare_conditional: bool | None = None

This field is used to declare if the instruction is a conditional instruction. In most cases, we can determine this purely based on the instruction ID, and this field is irrelevent. However, in some arches, like Arm, the same instruction can be made conditional by certain instruction attributes. Ex: Arm, bls instruction. This is encoded as a b under the code, with an additional condition code field. In this case, sometimes a b instruction is unconditional (always branches), in other cases it is conditional. We use this field to disambiguate these cases.

True if we manually determine this instruction is a conditional instruction False if it's not a conditional instruction None if we don't have a determination (most cases)

declare_is_unconditional_jump instance-attribute ¤

declare_is_unconditional_jump: bool = False

This field is used to declare that this instruction is an unconditional jump. Most of the time, we depend on Capstone groups to check for jump instructions. However, some instructions become branches depending on the operands, such as Arm add, sub, ldr, pop, where PC is the destination register

In these cases, we want to forcefully state that this instruction mutates the PC, so we set this attribute to True.

This helps in two cases: 1. Disassembly splits 2. Instructions like stepuntilasm work better, as they detect these as branches to stop at.

force_unconditional_jump_target instance-attribute ¤

force_unconditional_jump_target: bool = False

This asserts that the .target attribute is the real target of the instruction. This is only relevent in the edge case that the target is the next instruction in memory (address + size). The normal check for "target" checks that the target is NOT the next address in memory, and here we can assert that even if that is the case, we know that the jump really does just go to where self.target is.

annotation instance-attribute ¤

annotation: str | None = None

The string is set in the "DisassemblyAssistant.enhance" function. It is used in the disasm print view to add context to the instruction, mostly operand value. This string is not used for all cases - if the instruction is a call or a jump, the 'target'. variables is used instead. See 'pwndbg.color.disasm.instruction()' for specific usage.

annotation_padding instance-attribute ¤

annotation_padding: int | None = None

The left adjustment padding that was used to previously print this. We retain it so the output is consistent between prints

syscall instance-attribute ¤

syscall: int | None = None

The syscall number for this instruction, if it is a syscall. Otherwise None.

syscall_name instance-attribute ¤

syscall_name: str | None = None

The syscall name as a string

Ex: "openat", "read"

causes_branch_delay instance-attribute ¤

causes_branch_delay: bool = False

Whether or not this instruction has a single branch delay slot

split instance-attribute ¤

The type of split in the disasm display this instruction causes:

NO_SPLIT            - no extra spacing between this and the next instruction
BRANCH_TAKEN        - a newline with an arrow pointing down
BRANCH_NOT_TAKEN    - an empty newline

emulated instance-attribute ¤

emulated: bool = False

If the enhancement successfully used emulation for this instruction

call_like property ¤

call_like: bool

True if this is a call-like instruction, meaning either it's a CALL or a branch and link.

Checking for the CS_GRP_CALL is insufficient, as there are many "branch and link" instructions that are not labeled as a call

jump_like property ¤

jump_like: bool

True if this instruction is "jump-like", such as a JUMP, CALL, or RET. Basically, the PC is set to some target by means of this instruction.

It may still be a conditional jump - this property does not indicate whether the jump is taken or not.

has_jump_target property ¤

has_jump_target: bool

True if we have determined that this instruction can explicitly change the program counter, and we have determined the jump target.

Edge case - the jump target MAY be the next address in memory - so we check force_unconditional_jump_target

is_conditional_jump property ¤

is_conditional_jump: bool

True if this instruction can change the program counter conditionally.

This is used, in part, to determine if the instruction deserves a "checkmark" in the disasm view.

This does not imply that we have resolved the .target

is_unconditional_jump property ¤

is_unconditional_jump: bool

True if we know the instruction can change the program counter, and does so unconditionally.

This includes things like RET, CALL, and JMP (in x86).

This property is used in enhancement to determine certain codepaths when resolving .next for this instruction.

This does not imply that we have resolved the .target

is_conditional_jump_taken property ¤

is_conditional_jump_taken: bool

True if this is a conditional jump, and we predicted that we will take the jump

bytes property ¤

bytes: bytearray

Raw machine instruction bytes

op_find ¤

op_find(op_type: int, position: int) -> EnhancedOperand

Get the operand at position @position of all operands having the same type @op_type

op_count ¤

op_count(op_type: int) -> int

Return number of operands having same operand Capstone type 'op_type'

__repr__ ¤

__repr__() -> str

EnhancedOperand ¤

EnhancedOperand(cs_op)

Methods:

Attributes:

  • cs_op (Any) –

    Underlying Capstone operand. Takes on a different value depending on the architecture.

  • before_value (int | None) –

    The value of the operand before the instruction executes.

  • after_value (int | None) –

    The value of the operand after the instruction executes.

  • before_value_resolved (int | None) –

    The 'resolved' value of the operand that is actually used in the instruction logic, before the instruction executes.

  • before_value_no_modifiers (int | None) –

    This is a special field used in some architectures that allow operand modifiers, such as shifts and extends in Arm.

  • after_value_resolved (int | None) –

    The 'resolved' value of the operand after the instruction executes.

  • str (str | None) –

    String representing the operand

  • symbol (str | None) –

    Colorized symbol name for this operand, if .before_value is set and symbol exists, else None.

  • type (int) –

    CS_OP_REG | CS_OP_MEM | CS_OP_IMM | CS_OP_INVALID | CS_OP_FP

  • reg (int) –

    The underlying Capstone ID for the register

  • imm (int) –

    The immediate value of the operand (if applicable)

  • mem (Any) –

    Return the underlying Capstone mem object (if applicable)

cs_op instance-attribute ¤

cs_op: Any = cs_op

Underlying Capstone operand. Takes on a different value depending on the architecture.

x86 = capstone.x86.X86Op, arm = capstone.arm.ArmOp, mips = capstone.mips.MipsOp

before_value instance-attribute ¤

before_value: int | None = None

The value of the operand before the instruction executes. This is set only if the operand value can be reasoned about.

after_value instance-attribute ¤

after_value: int | None = None

The value of the operand after the instruction executes. Only set when using emulation.

before_value_resolved instance-attribute ¤

before_value_resolved: int | None = None

The 'resolved' value of the operand that is actually used in the instruction logic, before the instruction executes. This is the same as before_value if it's not a memory operand, in which cases it's the dereferenced value.

Helpful for cases like cmp byte ptr [rip + 0x166669], 0, where first operand could be a register or a memory value to dereference, and we want the actual value used.

before_value_no_modifiers instance-attribute ¤

before_value_no_modifiers: int | None = None

This is a special field used in some architectures that allow operand modifiers, such as shifts and extends in Arm. Capstone bundles the modifier with the operand, and when we are resolving concrete operand values, we apply the modifier. However, in some annotations we need to un-modified raw register value, which is what this field is for.

after_value_resolved instance-attribute ¤

after_value_resolved: int | None = None

The 'resolved' value of the operand after the instruction executes.

str instance-attribute ¤

str: str | None = ''

String representing the operand

Ex: "RAX", or "[0x7fffffffd9e8]". None if value cannot be determined.

symbol instance-attribute ¤

symbol: str | None = None

Colorized symbol name for this operand, if .before_value is set and symbol exists, else None.

type property ¤

type: int

CS_OP_REG | CS_OP_MEM | CS_OP_IMM | CS_OP_INVALID | CS_OP_FP

reg property ¤

reg: int

The underlying Capstone ID for the register

imm property ¤

imm: int

The immediate value of the operand (if applicable)

mem property ¤

mem: Any

Return the underlying Capstone mem object (if applicable)

__repr__ ¤

__repr__() -> str

ManualPwndbgInstruction ¤

ManualPwndbgInstruction(address: int)

Bases: PwndbgInstruction

GDB/LLDB's built-in disassemblers.

Instances of this class do not go through the 'enhancement' process due to lacking important information provided by Capstone. As a result of this, some of the methods raise NotImplementedError, because if they are called it indicates a bug elsewhere in the codebase.

Methods:

Attributes:

cs_insn instance-attribute ¤

cs_insn: CsInsn = None

address instance-attribute ¤

address = address

size instance-attribute ¤

size = ins['length']

mnemonic instance-attribute ¤

mnemonic = strip()

op_str instance-attribute ¤

op_str = strip() if len(asm) > 1 else ''

groups instance-attribute ¤

groups = set()

id instance-attribute ¤

id = -1

operands instance-attribute ¤

operands = []

asm_string instance-attribute ¤

asm_string = f'{mnemonic} {op_str}'

next instance-attribute ¤

next = address + size

target instance-attribute ¤

target = next

target_string instance-attribute ¤

target_string = None

target_const instance-attribute ¤

target_const = None

condition instance-attribute ¤

condition = UNDETERMINED

declare_conditional instance-attribute ¤

declare_conditional = None

declare_is_unconditional_jump instance-attribute ¤

declare_is_unconditional_jump = False

force_unconditional_jump_target instance-attribute ¤

force_unconditional_jump_target = False

annotation instance-attribute ¤

annotation = None

annotation_padding instance-attribute ¤

annotation_padding = None

syscall instance-attribute ¤

syscall = None

syscall_name instance-attribute ¤

syscall_name = None

causes_branch_delay instance-attribute ¤

causes_branch_delay = False

split instance-attribute ¤

split = NO_SPLIT

emulated instance-attribute ¤

emulated = False

bytes property ¤

bytes: bytearray

call_like property ¤

call_like: bool

jump_like property ¤

jump_like: bool

has_jump_target property ¤

has_jump_target: bool

is_conditional_jump property ¤

is_conditional_jump: bool

is_unconditional_jump property ¤

is_unconditional_jump: bool

is_conditional_jump_taken property ¤

is_conditional_jump_taken: bool

op_find ¤

op_find(op_type: int, position: int) -> EnhancedOperand

op_count ¤

op_count(op_type: int) -> int

boolean_to_instruction_condition ¤

boolean_to_instruction_condition(condition: bool) -> InstructionCondition