Added a new decorator only_if_current_arch_in and its doc

This commit is contained in:
hugsy
2021-08-21 14:27:33 -07:00
parent 1ca68c4af9
commit 4554aa0c15
2 changed files with 117 additions and 20 deletions
+101 -20
View File
@@ -88,7 +88,9 @@ $ gdb -q -ex 'pi help(hexdump)' -ex quit
```
### Globals ###
### Reference
#### Global
```python
register_external_command()
@@ -97,6 +99,19 @@ Procedure to add the new GEF command
---
```python
parse_address()
```
Parse an expression into a integer from the current debugging context.
```python
gef ➤ pi hex(parse_address("main+0x4"))
'0x55555555a7d4'
```
---
```python
current_arch
```
@@ -104,11 +119,48 @@ Global variable associated with the architecture of the currently debugged proce
---
```python
current_elf
```
Global variable associated to the currently debugging ELF file.
#### Logging
```python
ok(msg)
info(msg)
warn(msg)
err(msg)
```
#### CPU
```python
get_register(register_name)
```
Returns the value of given register. The function will fail outside a running debugging context.
#### Memory
```python
read_memory(addr, length=0x10)
```
Returns a `length` long byte array with a copy of the process memory read from `addr`.
Ex:
```python
0:000 ➤ pi print(hexdump( read_memory(parse_address("$pc"), length=0x20 )))
0x0000000000000000 f3 0f 1e fa 31 ed 49 89 d1 5e 48 89 e2 48 83 e4 ....1.I..^H..H..
0x0000000000000010 f0 50 54 4c 8d 05 66 0d 01 00 48 8d 0d ef 0c 01 .PTL..f...H.....
```
---
```python
@@ -133,36 +185,37 @@ Return a NULL-terminated array of bytes, from `addr`.
---
```python
get_register(register_name)
```
Returns the value of given register.
---
```python
get_process_maps()
```
Returns an array of Section objects (see below) corresponding to the current memory layout of the process.
Returns an iterable of Section objects (see below) corresponding to the current memory layout of the process.
```python
0:000 ➤ pi print('\n'.join([ f"{x.page_start:#x} -> {x.page_end:#x}" for x in get_process_maps()]))
0x555555554000 -> 0x555555558000
0x555555558000 -> 0x55555556c000
0x55555556c000 -> 0x555555575000
0x555555576000 -> 0x555555577000
0x555555577000 -> 0x555555578000
0x555555578000 -> 0x55555559a000
0x7ffff7cd8000 -> 0x7ffff7cda000
0x7ffff7cda000 -> 0x7ffff7ce1000
0x7ffff7ce1000 -> 0x7ffff7cf2000
0x7ffff7cf2000 -> 0x7ffff7cf7000
[...]
```
#### Code
---
```python
gef_disassemble(addr, nb_insn, from_top=False)
```
Disassemble `nb_insn` instructions after `addr`. If `from_top` is False (default), it will also disassemble the `nb_insn` instructions before `addr`. Return an iterator of Instruction objects (see below).
---
```python
ok(msg)
info(msg)
warn(msg)
err(msg)
```
Logging functions
#### Runtime hooks
---
@@ -200,7 +253,7 @@ gef_on_exit_unhook
Takes a callback function FUNC as parameter: add/remove a call to `FUNC` when GDB exits an inferior.
### Decorators ###
### `do_invoke` decorators ###
```python
@only_if_gdb_running
@@ -225,6 +278,22 @@ Checks if the current GDB session is local i.e. not debugging using GDB `remote`
Checks if the GDB version is higher or equal to the MAJOR and MINOR providedas arguments (both as Integers). This is required since some commands/API ofGDB are only present in the very latest version of GDB.
---
```python
@obsolete_command
```
Decorator to add a warning when a command is obsolete and may be removed without warning in future releases.
---
```python
@experimental_feature
```
Decorator to add a warning when a feature is experimental, and its API/behavior may change in future releases.
---
```python
@@ -274,6 +343,18 @@ args.bleh --> "" # the default value
args.blah --> True # set to True because user input declared the option (would have been False otherwise)
```
---
```python
@only_if_current_arch_in(valid_architectures)
```
Decorator to allow commands for only a subset of the architectured supported by GEF. This decorator is to use lightly, as it goes against the purpose of GEF to support all architectures GDB does. However in some cases, it is necessary.
```python
@only_if_current_arch_in(["X86", "RISCV"])
def do_invoke(self, argv):
[...]
```
### Classes ###
+16
View File
@@ -2632,6 +2632,22 @@ def only_if_gdb_version_higher_than(required_gdb_version):
return wrapper
def only_if_current_arch_in(valid_architectures):
"""Decorator to allow commands for only a subset of the architectured supported by GEF.
This decorator is to use lightly, as it goes against the purpose of GEF to support all
architectures GDB does. However in some cases, it is necessary."""
def wrapper(f):
def inner_f(*args, **kwargs):
if current_arch in valid_architectures:
f(*args, **kwargs)
else:
reason = "This command cannot work for the '{}' architecture".format(current_arch.arch)
raise EnvironmentError(reason)
return inner_f
return wrapper
def FakeExit(*args, **kwargs):
raise RuntimeWarning