JenyaKh
JenyaKh

Reputation: 2508

Gdb: can one have non-stop mode for selected threads?

I managed to use this non-stop mode for the case when one thread is debugged and others run: https://sourceware.org/gdb/onlinedocs/gdb/Non_002dStop-Mode.html#Non_002dStop-Mode.

There it's written:

In non-stop mode, when a thread stops to report a debugging event, only that thread is stopped; GDB does not stop other threads as well, in contrast to the all-stop mode behavior. Additionally, execution commands such as continue and step apply by default only to the current thread in non-stop mode, rather than all threads as in all-stop mode. This allows you to control threads explicitly in ways that are not possible in all-stop mode — for example, stepping one thread while allowing others to run freely, stepping one thread while holding all others stopped, or stepping several threads independently and simultaneously.

You can use GDB’s background execution commands (see Background Execution) to run some threads in the background while you continue to examine or step others from GDB. The MI execution commands (see GDB/MI Program Execution) are always executed asynchronously in non-stop mode.

By the link of the background mode it's written:

Background execution is especially useful in conjunction with non-stop mode for debugging programs with multiple threads; see Non-Stop Mode. However, you can also use these commands in the normal all-stop mode with the restriction that you cannot issue another execution command until the previous one finishes. Examples of commands that are valid in all-stop mode while the program is running include help and info break.

But finally I have not found any way to achieve what is highlighted with bold in the paragraphs above. Is it possible to have the non-stop mode for several threads? E.g., debugging two threads while four others run? Also, how can I run one thread in the background while debugging the rest (this possibility is also present in the bold text above, but I don't understand who I can achieve it)?

Upvotes: 0

Views: 742

Answers (1)

Andrew
Andrew

Reputation: 4781

The non-stop mode will apply to all threads, and needs to be set before an inferior is started.

Once an inferior has been started with non-stop mode on, then when the inferior stops or is interrupted (Ctrl-C) only the current thread stops. Use info threads to see the state of all threads.

If you want to resume a single thread and return to the GDB prompt so you can enter further commands, continue & is what you want. Only the currently selected thread will have continued. You can also continue -a & to continue all threads.

If you are at a prompt and wish to interrupt a thread then Ctrl-C will no work, however you can use the interrupt command to achieve the same result.

You can change the currently selected thread with thread N where N is the ID number of the thread you wish to switch too.

In summary:

  • Use set non-stop on before starting the inferior,
  • Add & to the end of continue, or continue like commands to run the command in the background and return to a prompt,
  • Use interrupt to interrupt a thread when at the prompt,
  • Use info threads to see the state of all threads,
  • Use thread N to switch between threads.

Upvotes: 1

Related Questions