When I attach to a process, or when a breakpoint is hit, all the threads are paused. I can't find out how to pause one thread, and let other threads continue running.
I can't find out how to pause one thread, and let other threads continue running.
Besides the non-stop mode mentioned in this answer, and scheduler locking mentioned here, you could do this:
# arrange for GDB to stop in the thread you want to suspend.
(gdb) call sleep(1000) # current thread will sleep, all others will continue running
Related
I'm debugging a multi-threaded C++ application in Eclipse Oxygen with gdb 7.4
The default behaviour is that when a breakpoint is reached all threads are halted, however, I'd like only the thread that reached the breakpoint to halt and all others would continue to run.
How is possible?
How is possible?
(gdb) set non-stop on
By default non-stop mode is off. You want it to be on, see gdb builtin help:
(gdb) help set non-stop
Set whether gdb controls the inferior in non-stop mode.
When debugging a multi-threaded program and this setting is
off (the default, also called all-stop mode), when one thread stops
(for a breakpoint, watchpoint, exception, or similar events), GDB stops
all other threads in the program while you interact with the thread of
interest. When you continue or step a thread, you can allow the other
threads to run, or have them remain stopped, but while you inspect any
thread's state, all threads stop.
In non-stop mode, when one thread stops, other threads can continue
to run freely. You'll be able to step each thread independently,
leave it stopped or free to run as needed.
(gdb)
I have an embedded application, running as a single process on Linux.
I use sigaction() to catch problems, such as segmentation fault, etc.
The process has a few threads, all of which, like the app, should run forever.
My question is whether (and how) I should detect if one of the threads dies.
Would a seg fault in a thread be caught by the application’s sigaction() handler?
I was thinking of using pthread_cleanup_push/pop, but this page says “If any thread within a process calls exit, _Exit, or _exit, then the entire process terminates”, so I wonder if a thread dying would be caught at the process level …
It is not a must that you need to check whether the child thread is completed.
If you have a need of doing something after the child thread completes its processing you can call thread_join() from the main thread, so that it will wait till the child threads completes execution and you can do the rest after this. If you are using thread_exit in the main thread it will get terminated once it is done, leaving the spawned threads to continue execution. The process will get killed only after all the threads completes execution.
If you want to check the status of the spawned threads you can use a flag to detect whether it is running or not. Check this link for more details
How do you query a pthread to see if it is still running?
I created a thread from main and detach it from main thread.
I want to kill this thread when it is in progress state.
I tried std::terminate() But it kill whole process . This thing i didn't want.
I want to kill that progress thread and start another thread in C++.
Please help me to solve this for std::thread.
In case of detached threads, if the main thread finishes executing before the detached thread, this would terminate the process killing all the threads. pthread_join() doesn't work in for detached threads.
So in what scenarios are detached threads used because I should be sure that detached thread has finished execution before terminating the process?
Why do you care whether the thread has finished execution? What you care about is whether any work that you need done has been done. If you use some other way to track what work is done, you don't need to wait for the thread to finish execution.
I have a program which uses two threads. I have put the break point in both the threads. While running the program under gdb I want to switch between the threads and make them run.
(thread t1 is active and running and thread t2; when paused on the breakpoint. I want to stop T1 running and run the T2).
Is there any way that I can schedule the threads in gdb?
By default, GDB stops all threads when any breakpoint is hit, and resumes all threads when you issue any command (such as continue, next, step, finish, etc.) which requires that the inferior process (the one you are debugging) start to execute.
However, you can tell GDB not to do that:
(gdb) help set scheduler-locking
Set mode for locking scheduler during execution.
off == no locking (threads may preempt at any time)
on == full locking (no thread except the current thread may run)
step == scheduler locked during every single-step operation.
In this mode, no other thread may run during a step command.
Other threads may run while stepping over a function call ('next').
So you'll want to set breakpoints, then set scheduler-locking on, then continue or finish in thread 1 (thread 2 is still stopped), then Ctrl-C to regain control of GDB, switch to thread 2, continue (thread 1 is still stopped), etc.
Beware: by setting scheduler-locking on it is very easy to cause the inferior process to self-deadlock.
If you're using GDB 7 or later, try "non-stop mode".
http://sourceware.org/gdb/current/onlinedocs/gdb/Non_002dStop-Mode.html
The "scheduler-locking on" command previously mentioned allows you step one thread with the others stopped. Non-stop mode allows you to step one thread with the others active.
use break conditions
(gdb) break frik.c:13 thread 28 if bartab > lim
see Debugging with GDB
Edit:
(gdb) break <thread_function_entry_point> thread 2
(gdb) break <thread_function_entry_point> thread 1
(gdb) thread 1
(gdb) continue
(gdb) ... thread 1 finishes
(gdb) thread 2
(gdb) continue
You can put these commands inside a .gdbrc file.