Next: Events In Python, Previous: Type Printing API, Up: Python API

##### 23.2.2.9 Inferiors In Python

Programs which are being run under gdb are called inferiors (see Inferiors and Programs). Python scripts can access information about and manipulate inferiors controlled by gdb via objects of the `gdb.Inferior` class.

The following inferior-related functions are available in the `gdb` module:

— Function: gdb.inferiors ()

> Return a tuple containing all inferior objects.

— Function: gdb.selected_inferior ()

> Return an object representing the current inferior.

A `gdb.Inferior` object has the following attributes:

— Variable: Inferior.num

> ID of inferior, as assigned by GDB.

— Variable: Inferior.pid

> Process ID of the inferior, as assigned by the underlying operating system.

— Variable: Inferior.was_attached

> Boolean signaling whether the inferior was created using `attach', or started by gdb itself.

A `gdb.Inferior` object has the following methods:

— Function: Inferior.is_valid ()

> Returns `True` if the `gdb.Inferior` object is valid, `False` if not. A `gdb.Inferior` object will become invalid if the inferior no longer exists within gdb. All other `gdb.Inferior` methods will throw an exception if it is invalid at the time the method is called.

— Function: Inferior.threads ()

> This method returns a tuple holding all the threads which are valid when it is called. If there are no valid threads, the method will return an empty tuple.

— Function: Inferior.read_memory (address, length)

> Read length bytes of memory from the inferior, starting at address. Returns a buffer object, which behaves much like an array or a string. It can be modified and given to the `Inferior.write_memory` function. In `Python` 3, the return value is a `memoryview` object.

— Function: Inferior.write_memory (address, buffer [, length])

> Write the contents of buffer to the inferior, starting at address. The buffer parameter must be a Python object which supports the buffer protocol, i.e., a string, an array or the object returned from `Inferior.read_memory`. If given, length determines the number of bytes from buffer to be written.

— Function: Inferior.search_memory (address, length, pattern)

> Search a region of the inferior memory starting at address with the given length using the search pattern supplied in pattern. The pattern parameter must be a Python object which supports the buffer protocol, i.e., a string, an array or the object returned from `gdb.read_memory`. Returns a Python `Long` containing the address where the pattern was found, or `None` if the pattern could not be found.