How to Continue One Thread at a Time When Debugging a Multithreaded Program in Gdb

How to continue one thread at a time when debugging a multithreaded program in GDB?

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.

How to restrict gdb debugging to one thread at a time

It looks like scheduler-locking is only useful when you single-step or next. Once you continue your current thread, they all run, and the next thread to hit a break point will grab the prompts. At least, that's my interpretation of the manual:

http://ftp.gnu.org/old-gnu/Manuals/gdb-5.1.1/html_node/gdb_39.html

So, once you are in thread 3, the other threads are stopped, and as long as you step/next, they will not run. But once you continue, they all run, and the next thread (2 in your example) that hits the break point in the sleep(1) will grab the prompt.

Maybe let all the threads hit the sleep, but then only continue one of them at a time.

gdb how to break in new thread when debugging multi threaded daemon program on linux

I found a workaround that works :-)

While I still can NOT get gdb to break at break-point of my interest (occurs in a short lived thread spawed by main thread), I found a way to switch to that thread explicitly soon enough before that thread ended.

The problem is that gdb needs you to explicitly switch to the thread you are interested in, to stop at break-point.

since the thread that I was trying to debug was rather short lived I can not switch to it soon enough (before it finished) .

In addition to break-point in function of my interest I placed another break-point in a logger function that occurred often in all threads and kept continuing ( annoying for sure) till the thread of my interest was spawned and then switched to that explicitly when new thread was spawned and by listing threads.

(gdb) info threads
(gdb) Thread <thread id>

I am posting this so if you have similar problem you may try this workaround.

Your comments, better answers :-) more responses welcome

gdb: call function in multithreaded program without progressing threads

See @KostasRim's comment -- set scheduler-locking on achieves the desired behavior (https://sourceware.org/gdb/onlinedocs/gdb/All_002dStop-Mode.html). By default, other threads run during stepping, function calling, etc.

GDB Conditional Breakpoint in Multithreaded application

break myfile:1234 if $_streq(managedStr->value(), "testString")

That should work, and the fact that it doesn't work implies a bug in GDB.

That said, this method of debugging is exceedingly slow, because GDB has to stop every thread at line 1234 and then examine the memory values (i.e. evaluate the condition).

Usually it is much faster to insert a little bit of helper code into the program:

if (strcmp(namagedStr->value(), "testString") == 0) {
printf("DEBUG: match\n"); // set breakpoint here!
}

and rebuild it.

Now instead of stopping every thread all the time and evaluating the condition, GDB will set a breakpoint on code that is only reached when the condition is true. This will be 100 to 1000 times faster (depending on number of threads).

This technique could be trivially extended if you need to vary the actual value you are looking for from run to run, or even within a single run, by adding a global variable:

const char *str_to_match = "testString";  // variable can be overwritten in GDB

if (strcmp(managedStr->value(), str_to_match) == 0) {
...
}


Related Topics



Leave a reply



Submit