Skip to content

Conversation

@disconnect3d
Copy link
Contributor

@disconnect3d disconnect3d commented May 4, 2020

Currently, a print(instruction) displays a not very useful string like:

<capstone.CsInsn object at 0x7f3759d88128>

This PR enhances the display by adding a __repr__ magic method to the capstone.CsInsn class so it displays as follows:

<cs.CsInsn: address=0x5555555545fa, size=1, mnemonic=push, op_str=rbp>

Currently, a `print(instruction)` displays a not very useful string like `<capstone.CsInsn object at 0x7f3759d88128>`.

This PR enhances adds a `__repr__` magic method to the `capstone.CsInsn` class so it displays as follows:
```
<cs.CsInsn: address=0x5555555545fa, size=1, mnemonic=push, op_str=rbp>
```
@disconnect3d
Copy link
Contributor Author

disconnect3d commented May 4, 2020

Let me know if you want any other/better format there. The one I choose here explicitly shows the properties one could use.

Some other ideas:

  • We could change cs.CsInsn to capstone.CsInsn to be more verbose, or, drop the namespace and leave it with CsInsn (since there is already the Cs prefix)
  • We could drop the property names and stay with something very compact like: '<CsInsn %s [%dB]: %s %s>' % (self.address, self.size, self.mnemonic, self.op_str) . Examples:
<CsInsn 0x5555555545fa [1B]: push rbp>
<CsInsn 0x7ffff7b93ae8 [4B]: adc byte ptr [r8 + 9], r8b>

@aquynh
Copy link
Collaborator

aquynh commented May 4, 2020

nice, the second idea looks fine to me, but maybe we can also print all bytes in bytes field?

@disconnect3d
Copy link
Contributor Author

disconnect3d commented May 4, 2020

but maybe we can also print all bytes in bytes field?

Yup, that's a good idea. Below are some example outputs.

Version Alpha

This version shows hex bytes.

    def __repr__(self):
        return '<CsInsn 0x%x [%dB]: %s %s %s>' % (self.address, self.size, self.bytes.hex(), self.mnemonic, self.op_str)

That gives:

<CsInsn 0x5555555545fa [1B]: 55 push rbp>
<CsInsn 0x555555554600 [2B]: 4157 push r15>
<CsInsn 0x7ffff7b93ae8 [4B]: 4d104009 adc byte ptr [r8 + 9], r8b>
<CsInsn 0x5555555544f0 [2B]: 31ed xor ebp, ebp>
<CsInsn 0x5555555545fe [2B]: ebfe jmp 0x5555555545fe>
<CsInsn 0x7ffff7a05b97 [2B]: 89c7 mov edi, eax>

Version Bravo

This version displays the hex bytes in [] brackets.

    def __repr__(self):
        return '<CsInsn 0x%x [%dB] [%s]: %s %s>' % (self.address, self.size, self.bytes.hex(), self.mnemonic, self.op_str)
<CsInsn 0x5555555545fa [1B] [55]: push rbp>
<CsInsn 0x555555554600 [2B] [4157]: push r15>
<CsInsn 0x7ffff7b93ae8 [4B] [4d104009]: adc byte ptr [r8 + 9], r8b>
<CsInsn 0x5555555544f0 [2B] [31ed]: xor ebp, ebp>
<CsInsn 0x5555555545fe [2B] [ebfe]: jmp 0x5555555545fe>
<CsInsn 0x7ffff7a05b97 [2B] [89c7]: mov edi, eax>

Version Charlie

This version skips displaying the size in bytes.

    def __repr__(self):
        return '<CsInsn 0x%x [%s]: %s %s>' % (self.address, self.bytes.hex(), self.mnemonic, self.op_str)
<CsInsn 0x5555555545fa [55]: push rbp>
<CsInsn 0x555555554600 [4157]: push r15>
<CsInsn 0x7ffff7b93ae8 [4d104009]: adc byte ptr [r8 + 9], r8b>
<CsInsn 0x5555555544f0 [31ed]: xor ebp, ebp>
<CsInsn 0x5555555545fe [ebfe]: jmp 0x5555555545fe>
<CsInsn 0x7ffff7a05b97 [89c7]: mov edi, eax>

@disconnect3d
Copy link
Contributor Author

@aquynh see some proposals above :)

@aquynh
Copy link
Collaborator

aquynh commented May 4, 2020 via email

@disconnect3d
Copy link
Contributor Author

disconnect3d commented May 4, 2020

looks nice now, but why do you want to print the size? and with "B", which
seems abundant?

I initially printed the size to give more context but since we go towards displaying hex encoded bytes of the instruction, I don't think it is relevant anymore to do that.

So, shall we go with version charlie? (I updated the PR to contain the charlie version).

@aquynh aquynh merged commit 1b50145 into capstone-engine:master May 4, 2020
@aquynh
Copy link
Collaborator

aquynh commented May 4, 2020

merged, thanks.

aquynh pushed a commit that referenced this pull request May 4, 2020
* Add __repr__ for capstone.CsInsn

Currently, a `print(instruction)` displays a not very useful string like `<capstone.CsInsn object at 0x7f3759d88128>`.

This PR enhances adds a `__repr__` magic method to the `capstone.CsInsn` class so it displays as follows:
```
<cs.CsInsn: address=0x5555555545fa, size=1, mnemonic=push, op_str=rbp>
```

* Update __init__.py
aquynh pushed a commit that referenced this pull request May 4, 2020
* Add __repr__ for capstone.CsInsn

Currently, a `print(instruction)` displays a not very useful string like `<capstone.CsInsn object at 0x7f3759d88128>`.

This PR enhances adds a `__repr__` magic method to the `capstone.CsInsn` class so it displays as follows:
```
<cs.CsInsn: address=0x5555555545fa, size=1, mnemonic=push, op_str=rbp>
```

* Update __init__.py
@disconnect3d disconnect3d deleted the patch-1 branch July 1, 2020 07:29
disconnect3d added a commit to pwndbg/pwndbg that referenced this pull request Oct 9, 2021
We will e.g. have capstone-engine/capstone#1625 which is useful for debugging Pwndbg capstone related features
@riptl riptl mentioned this pull request Jul 22, 2022
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants