Skip to content

Commit

Permalink
Added a new decorator only_if_current_arch_in and its doc
Browse files Browse the repository at this point in the history
  • Loading branch information
hugsy committed Aug 21, 2021
1 parent 1ca68c4 commit 4554aa0
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 18 deletions.
117 changes: 99 additions & 18 deletions docs/api.md
Expand Up @@ -88,7 +88,9 @@ $ gdb -q -ex 'pi help(hexdump)' -ex quit
```


### Globals ###
### Reference

#### Global

```python
register_external_command()
Expand All @@ -97,18 +99,68 @@ 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
```
Global variable associated with the architecture of the currently debugged process. The variable is an instance of the `Architecture` class (see below).

---

```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
Expand All @@ -134,35 +186,36 @@ Return a NULL-terminated array of bytes, from `addr`.
---

```python
get_register(register_name)
get_process_maps()
```

Returns the value of given register.

---
Returns an iterable of Section objects (see below) corresponding to the current memory layout of the process.

```python
get_process_maps()
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
[...]
```
Returns an array of Section objects (see below) corresponding to the current memory layout of the process.

---
#### 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

---

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 ###
Expand Down
16 changes: 16 additions & 0 deletions gef.py
Expand Up @@ -2632,6 +2632,22 @@ def inner_f(*args, **kwargs):
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

Expand Down

0 comments on commit 4554aa0

Please sign in to comment.