gdb调试cpu high实例

本文详细介绍如何使用GDB调试器在运行中的程序上进行调试,包括通过指定PID或使用file和attach命令来连接进程,以及如何使用backtrace和step命令进行单步调试。此外,还介绍了如何使用infothread和thread命令进行线程调试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

gdb调试cpu high问题
这种情况是特殊的清楚,是在调试正在运行的程序,而无法直接在调试器中直接运行程序,gdb提供两种方法:
1.在gdb命令上指定运行进程的PID
2.在gdb中使用file和attach PID命令
实例1.

/ # ps ax | grep osi_ll
 1268 root       0:24 osi_ll -l /var/log/osi_ll.log
 1371 root       0:00 grep osi_ll
 / # gdb osi_ll 1268
GNU gdb (GDB) 8.0.1
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "aarch64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from osi_ll...done.
Attaching to program: /bin/osi_ll, process 1268
[New LWP 1274]
[New LWP 1275]
[New LWP 1276]
[New LWP 1277]
[New LWP 1305]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/libthread_db.so.1".

0x0000ffffa6e658f4 in __GI___select (nfds=23, readfds=0xffffc4f2f800, 
    writefds=0x4a5400 <main+816>, exceptfds=0xffffc4f2f810, 
    timeout=0xffffc4f2f9c0) at ../sysdeps/unix/sysv/linux/generic/select.c:45
45      ../sysdeps/unix/sysv/linux/generic/select.c: No such file or directory.
    (gdb) 
    (gdb) detach
Detaching from program: /bin/osi_ll, process 1268
(gdb) quit
/ # 

实例2:
另外一种连接到其它进程的方法是先用file命令加载调试时所需的符号表,然后再通过attach PID命令进行连接:

/ # which osi_ll
/bin/osi_ll
/ # gdb
GNU gdb (GDB) 8.0.1
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "aarch64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb) file /bin/osi_ll
Reading symbols from /bin/osi_ll...done.
(gdb) attach 1268
Attaching to program: /bin/osi_ll, process 1268
[New LWP 1274]
[New LWP 1275]
[New LWP 1276]
[New LWP 1277]
[New LWP 1305]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/libthread_db.so.1".
0x0000ffffa6e658f4 in __GI___select (nfds=23, readfds=0xffffc4f2f800, 
    writefds=0x4a5400 <main+816>, exceptfds=0xffffc4f2f810, 
    timeout=0xffffc4f2f9c0) at ../sysdeps/unix/sysv/linux/generic/select.c:45
45      ../sysdeps/unix/sysv/linux/generic/select.c: No such file or directory.
(gdb) 

如果想知道程序现在运行到了哪里,同样可以使用“backtrace”命令。当然也可以使用“step”命令对程序进行单步调试。
在完成调试之后,不要忘记用detach命令断开连接,让被调试的进程可以继续正常运行。

(gdb) bt
#0  0x0000ffffa6e658f4 in __GI___select (nfds=23, readfds=0xffffc4f2f800, 
    writefds=0x4a5400 <main+816>, exceptfds=0xffffc4f2f810, 
    timeout=0xffffc4f2f9c0) at ../sysdeps/unix/sysv/linux/generic/select.c:45
#1  0x0000ffffa722cae0 in cs_select (n=23, readfds=0xffffc4f2f940, 
    writefds=0xffffc4f2f8c0, exceptfds=0xffffc4f2f840, timeout=0xffffc4f2f9c0)
    at /local/home/jilongl/myprj/stack/applplat_tss15/PLAT_APPL/stack_osi/cs_socket/src/cs_socket_mod.c:867
#2  0x00000000004a5400 in main (argc=3, argv=0xffffc4f2fb18, 
    env=0xffffc4f2fb38)
    at /local/home/jilongl/myprj/stack/applplat_tss15/PLAT_APPL/stack_osi/osi_ll/src/ll_main.c:875
(gdb) s
__libc_disable_asynccancel (oldtype=0) at ../nptl/cancellation.c:69
69      ../nptl/cancellation.c: No such file or directory.
(gdb) detach
Detaching from program: /bin/osi_ll, process 1268
(gdb) 

4.线程调试 info thread和thread ID

(gdb) info thread
  Id   Target Id         Frame 
* 1    Thread 0xffffa7262010 (LWP 1268) "osi_ll" 0x0000ffffa6e658f4 in __GI___select (nfds=23, readfds=0xffffc4f2f800, writefds=0x4a5400 <main+816>, 
    exceptfds=0xffffc4f2f810, timeout=0xffffc4f2f9c0)
    at ../sysdeps/unix/sysv/linux/generic/select.c:45
  2    Thread 0xffffa6da61e0 (LWP 1274) "osi_ll" 0x0000ffffa6e658f4 in __GI___select (nfds=8, readfds=0x4010040100100001, 
    writefds=0x498648 <notify_handler_main+484>, exceptfds=0xffffa6da5800, 
    timeout=0x0) at ../sysdeps/unix/sysv/linux/generic/select.c:45
  3    Thread 0xffffa6d261e0 (LWP 1275) "osi_ll" 0x0000ffffa6e658f4 in __GI___select (nfds=10, readfds=0x4010040140100401, 
    writefds=0x491804 <do_lan_rx_queue+340>, exceptfds=0xffffa6d25900, 
    timeout=0x0) at ../sysdeps/unix/sysv/linux/generic/select.c:45
  4    Thread 0xffffa6ca61e0 (LWP 1276) "osi_ll" 0x0000ffffa6e658f4 in __GI___select (nfds=14, readfds=0x4010040140100401, 
    writefds=0x48cc8c <do_pp_rx_queue+340>, exceptfds=0xffffa6ca5900, 
    timeout=0x0) at ../sysdeps/unix/sysv/linux/generic/select.c:45
  5    Thread 0xffffa6c261e0 (LWP 1277) "osi_ll" 0x0000ffffa6e658f4 in __GI___select (nfds=18, readfds=0x4010040140100401, 
    writefds=0x496294 <main_socket_task+280>, exceptfds=0xffffa6c25900, 
    timeout=0x0) at ../sysdeps/unix/sysv/linux/generic/select.c:45
  6    Thread 0xffffa62f11e0 (LWP 1305) "osi_ll" 0x0000ffffa6e658f4 in __GI___select (nfds=21, readfds=0xffffa62f08d0, writefds=0x407fac <cs_tarp_task+784>, 
---Type <return> to continue, or q <return> to quit---q
eQuit
(gdb) 
    (gdb) thread 2
[Switching to thread 2 (Thread 0xffffa6da61e0 (LWP 1274))]
#0  0x0000ffffa6e658f4 in __GI___select (nfds=8, readfds=0x4010040100100001, 
    writefds=0x498648 <notify_handler_main+484>, exceptfds=0xffffa6da5800, 
    timeout=0x0) at ../sysdeps/unix/sysv/linux/generic/select.c:45
45      in ../sysdeps/unix/sysv/linux/generic/select.c
(gdb) 

5.其他命令
watch div1==div2当变量div1yu div2相等时进入中断。
info thread 查看当前进程的线程。
thread 切换调试的线程为指定ID的线程。
break file.c:100 thread all 在file.c文件第100行处为所有经过这里的线程设置断点。
set scheduler-locking off|on|step,这个是问得最多的。在使用step或者continue命令调试当前被调试线程的时候,其他线程也是同时执行的,怎么只让被调试程序执行呢?通过这个命令就可以实现这个需求。
off 不锁定任何线程,也就是所有线程都执行,这是默认值。
on 只有当前被调试程序会执行。
step 在单步的时候,除了next过一个函数的情况(熟悉情况的人可能知道,这其实是一个设置断点然后continue的行为)以外,只有当前线程会执行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值