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

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值