The art of debugging with GDB (5) -- Useful tips

荣曾笑
2023-12-01

GDB Threads Command Summary

Here is a summary of the usage of GDB’s thread-related commands:

  • info threads         (Gives information on all current threads)

  • thread 3         (Changes to thread 3)

  • break 88 thread 3         (Stops execution when thread 3 reaches source line 88)

  • break 88 thread 3 if x==y         (Stops execution when thread 3 reaches source line 88 and the variables x and y are equal)

How to Separate the GDB I/O from the Program’s I/O?

GDB’s tty command can instruct GDB to have the program (which is being debugged by GDB) execute in a different terminal window than the one that GDB is running in.

(gdb) tty /dev/pts/X         # Set the I/O of the program being debugged to terminal /dev/pts/X

Run the Unix command tty to get the terminal ID you are using, For example:

[mg@vm201226 ~]$ tty
/dev/pts/0
[mg@vm201226 ~]$

Open another terminal window, and get its tty ID

[mg@vm201226 ~]$ tty
/dev/pts/1
[mg@vm201226 ~]$

Now, I want to run gdb in terminal /dev/pts/0 (we call this the GDB window), and want the Program’s I/O to be in the terminal /dev/pts/1 (we call this the execution window). What should I do ?

# GDB window (the window where you run gdb)
#
[mg@vm201226 test]$ tty
/dev/pts/0
[mg@vm201226 test]$
[mg@vm201226 test]$ gdb a.out
Reading symbols from a.out...done.
(gdb) b main
Breakpoint 1 at 0x40059e: file 2.c, line 6.
(gdb) tty /dev/pts/1 
(gdb)

One last thing before we start: We must type something like sleep 10000 in the execution window (The program’s I/O terminal, here is /dev/pts/1), so that our keyboard input to that window will go to the program, rather than to the shell.

# the execution window (the window where the program's I/O performs) 
#
[mg@vm201226 test]$ tty
/dev/pts/1
[mg@vm201226 test]$ sleep 10000

Then when you run the program in GDB in /dev/pts/0, the program’s I/O will be performed in /dev/pts/1.

Notes:

There are other ways we could handle the problem of separating GDB output from the program’s output. For instance, we could start the program’s execution first, then fire up GDB in another window, attaching it to the running program.

 类似资料: