Next: , Previous: , Up: Python API   [Contents][Index]


24.3.2.2 Threading in GDB

ROCGDB is not thread-safe. If your Python program uses multiple threads, you must be careful to only call ROCGDB-specific functions in the ROCGDB thread. ROCGDB provides some functions to help with this.

Function: gdb.block_signals ()

As mentioned earlier (see Basic Python), certain signals must be delivered to the ROCGDB main thread. The block_signals function returns a context manager that will block these signals on entry. This can be used when starting a new thread to ensure that the signals are blocked there, like:

with gdb.block_signals():
   start_new_thread()
class: gdb.Thread

This is a subclass of Python’s threading.Thread class. It overrides the start method to call block_signals, making this an easy-to-use drop-in replacement for creating threads that will work well in ROCGDB.

Function: gdb.interrupt ()

This causes ROCGDB to react as if the user had typed a control-C character at the terminal. That is, if the inferior is running, it is interrupted; if a ROCGDB command is executing, it is stopped; and if a Python command is running, KeyboardInterrupt will be raised.

Unlike most Python APIs in ROCGDB, interrupt is thread-safe.

Function: gdb.post_event (event)

Put event, a callable object taking no arguments, into ROCGDB’s internal event queue. This callable will be invoked at some later point, during ROCGDB’s event processing. Events posted using post_event will be run in the order in which they were posted; however, there is no way to know when they will be processed relative to other events inside ROCGDB.

Unlike most Python APIs in ROCGDB, post_event is thread-safe. For example:

(gdb) python
>import threading
>
>class Writer():
> def __init__(self, message):
>        self.message = message;
> def __call__(self):
>        gdb.write(self.message)
>
>class MyThread1 (threading.Thread):
> def run (self):
>        gdb.post_event(Writer("Hello "))
>
>class MyThread2 (threading.Thread):
> def run (self):
>        gdb.post_event(Writer("World\n"))
>
>MyThread1().start()
>MyThread2().start()
>end
(gdb) Hello World

Next: Exception Handling, Previous: Basic Python, Up: Python API   [Contents][Index]