C ++ - thread doesn't stop in async mode in gdb using python user or command sequence

I use gdb 7.4.1 on the built-in powerpc target to do some analysis of my multithreaded C ++ program that uses pthreads. My ultimate goal is a gdb script with python to automate some common analysis functions. The problem is that I find some inconsistency in the behavior when I run the commands individually or in the user gdb command (or by calling the same commands through a python script).

edit: I found this link to a very similar problem on the gdb main mailing list. Although I do not completely agree with Pedro's answer about limiting the asynchronous mode, I think that it implies that in asynchronous mode the relative time of user sequences of commands cannot be trusted. This is what I found empirically.

In both scenarios, I perform the following startup steps, load my program, configure my arguments and enable asynchronous and continuous debugging modes, and then run the program in the background:

(gdb) file myprogram (gdb) set args --interface=eth0 --try-count=0 (gdb) set target-async on (gdb) set pagination off (gdb) set non-stop on (gdb) run & 

At this point, if I manually issue interrupt commands and then info threads , I see a list of all threads except those that were stopped. Then I can continue & and repeat my heart, it works sequentially. When I stop, I can check this framework of the thread stack, and all is well.

However, if instead I put these commands in a custom gdb command:

 (gdb) define foo (gdb) interrupt (gdb) info threads (gdb) continue & (gdb) end (gdb) foo Cannot execute this command while the selected thread is running. 

Then the thread list printed by foo indicates that the threads were not stopped, and therefore the continue & command returns Cannot execute this command while the selected thread is running. . I thought this was a problem inherent to asynchronous gdb commanding, so I inserted an absurdly long wait after the interrupt command and got the same behavior:

 (gdb) define foo (gdb) interrupt (gdb) shell sleep 5 (gdb) info threads (gdb) continue & (gdb) end (gdb) foo Cannot execute this command while the selected thread is running. 

With or without a command, I can always issue command line commands manually, and the threads will stop correctly.

Similarly, I get the same results as searching a python script to read the stream:

 import gdb, time gdb.execute("file myprogram") gdb.execute("set args --interface=eth0 --try-count=0") gdb.execute("set target-async on") gdb.execute("set pagination off") gdb.execute("set non-stop on") gdb.execute("run &") time.sleep(5) gdb.execute("interrupt") # here, I inspect threads via gdb module interface # in practice, they're always all running bc the program neven got interrupted for thread in gdb.selected_inferior().threads(): print thread.is_running(), gdb.execute("continue &") 

I get the same result even if I specify from_tty=True in gdb.execute calls. Also, if I use continue -a , it suppresses the error line, but it doesn’t help otherwise bc the interrupt call still does not work.

So ... this is:

  • cockpit error? Is there something that I'm omitting or doing wrong, given what I'm trying to accomplish? Should this work, or do I need to use GDB / MI to asynchronously "drive" gdb like this?
  • synchronization problem? Perhaps calling shell sleep (or python time.sleep() ) does not do what I am assuming in this context.
  • problem with my use of pthreads? I suggested that since manually using gdb commands always works correctly, it is not.
  • gdb problem?

Thanks.

+6
source share
1 answer

I think this is most likely a gdb problem. I do not understand enough that the material of lower control is more self-confident. I know that the bottom control was not connected to Python at all ...

One thing worth trying is to have a separate Python thread that waits and then sends an interrupt command to the main gdb thread using gdb.post_event.

Then instead of exploring the threads synchronously or doing work after the "interruption", instead use the gdb.events.stop event source to trigger your actions.

Please write error errors in the Python API.

0
source

Source: https://habr.com/ru/post/915834/


All Articles