Bug 29831 - event loop deadlocks when using continue in breakpoint command and using gdb.execute in stop event hook written in Python
Summary: event loop deadlocks when using continue in breakpoint command and using gdb....
Status: NEW
Alias: None
Product: gdb
Classification: Unclassified
Component: gdb (show other bugs)
Version: HEAD
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-11-28 09:00 UTC by Disconnect3d
Modified: 2023-04-12 21:30 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed: 2023-04-12 00:00:00


Attachments
Possible patch for this problem. (405 bytes, patch)
2023-04-12 21:30 UTC, Andrew Burgess
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Disconnect3d 2022-11-28 09:00:11 UTC
Hi,

There is a case when GDB deadlocks or hang indefinetely when a breakpoint is hit and:
1) the breakpoint has attached commands and when one of the commands is `continue`
2) a stop event hook was registered before with a Python script and that hook executes a `gdb.execute` function

This issue is painful for automating GDB and hits many GDB users that use plugins written in Python (see [0]). I would appreciate if it is fixed sooner than later, or, if you can provide any information that may be helpful for fixing or diagnosing this further.

The bug can be reproduced by doing the following steps:

```
$ echo 'void foo(){}; int main() {foo();}' > example.c && gcc example.c

$ cat gdbs.py
import gdb
def stop_handler (event):
    print("Python stop handler started")
    print("Thread is stopped: " + str(gdb.selected_thread().is_stopped()))
    print("Thread is running: " + str(gdb.selected_thread().is_running()))
    (gdb.execute("",to_string=True))
    print("Thread is stopped: " + str(gdb.selected_thread().is_stopped()))
    print("Thread is running: " + str(gdb.selected_thread().is_running()))
    print("Python Stop handler finished\n")
gdb.events.stop.connect (stop_handler)

$ gdb --quiet --nx ./a.out
Reading symbols from ./a.out...
(No debugging symbols found in ./a.out)
(gdb) starti
Starting program: /home/dc/a.out

Program stopped.
0x00007ffff7dd4090 in _start () from /lib64/ld-linux-x86-64.so.2
(gdb) break foo
Breakpoint 1 at 0x5555555545fe
(gdb) commands
Type commands for breakpoint(s) 1, one per line.
End with a line saying just "end".
>continue
>end
(gdb) source gdbs.py
(gdb) c
Continuing.

Breakpoint 1, 0x00005555555545fe in foo ()
Python stop handler started
Thread is stopped: True
Thread is running: False
Thread is stopped: False
Thread is running: True
Python Stop handler finished

```

I was able to reproduce this bug with the following GDB versions:
- 10.2-0ubuntu1~18.04~2
- 12.1 compiled from sources (../configure && make -j)

[0] https://github.com/pwndbg/pwndbg/issues/425
Comment 1 Disconnect3d 2023-04-11 22:03:19 UTC
This can be worked around in the following way. However, this is not a good solution since this means one can't script GDB using both its native CLI commands and Python at the same time.

```
import gdb
def stop_handler (event):
    print("Python stop handler started")
    print("Thread is stopped: " + str(gdb.selected_thread().is_stopped()))
    print("Thread is running: " + str(gdb.selected_thread().is_running()))
    (gdb.execute("",to_string=True))
    print("Thread is stopped: " + str(gdb.selected_thread().is_stopped()))
    print("Thread is running: " + str(gdb.selected_thread().is_running()))
    print("Python Stop handler finished\n")
gdb.events.stop.connect (stop_handler)


class Bp(gdb.Breakpoint):
    def stop(self):
        print("Breakpoint hit!")
        return True

Bp("foo")
```
Comment 2 Andrew Burgess 2023-04-12 21:30:04 UTC
Created attachment 14819 [details]
Possible patch for this problem.

This patch seems to address the problem reported in this bug.  I think there might be other bugs in this area though -- while I was testing this I hit some other strange behaviours that I still need to look into.

Also I need to write some actual tests before this could be posted to the m/l.