The Vivandiere
The Vivandiere

Reputation: 3191

Print thread id at breakpoint

I am debugging some C++ code. When I am paused at breakpoint, if I do info thread, gdb shows me a list of all the threads in my process, and puts an asterisk next to the thread under execution at breakpoint. Is there a gdb command which makes gdb tell you the thread id when at breakpoint?

I am doing catch throw and catch catch, to debug around the time an exception is thrown on thread 1. But, thread 2 is simultaneously also throwing and catching exceptions. Since, I am only interested in throw and catch on thread1, I plan to ask gdb for threadid, and script the breakpoint to continue if threadid is 2.

(gdb) catch throw
Catchpoint 7 (throw)
(gdb) catch catch
Catchpoint 8 (catch) 
(gdb) command 8
> if threadid == 2
>      c
> end

Can you please show me how to write this line if threadid == 2?

Upvotes: 2

Views: 1425

Answers (1)

Julio Guerra
Julio Guerra

Reputation: 5661

Using the built-in $_thread convenience variable:

The debugger convenience variables $_thread and $_gthread contain, respectively, the per-inferior thread number and the global thread number of the current thread. You may find this useful in writing breakpoint conditional expressions, command scripts, and so forth. See Convenience Variables, for general information on convenience variables.

catch catch if $_thread == 1

Using the Python API:

— Function: gdb.selected_thread () This function returns the thread object for the selected thread. If there is no selected thread, this will return None.

catch catch
command
  python
    if gdb.selected_thread() != 1:
      gdb.execute('continue');
  end
end

Generally speaking, when GDB lacks a feature, it's very unlikely you cannot implement it using the Python API since it allows you to explore your running program and context.

Upvotes: 2

Related Questions