我有一个多线程应用程序,它使用 netlink 套接字与内核模块通信。用户模式应用程序中的一个线程用作服务器,内核模块用作客户端。大致内核代码如下:
timeout = 3500;
netlink_unicast();
wait:
__set_current_state(TASK_INTERRUPTIBLE);
timeout = schedule_timeout(timeout);
__set_current_state(TASK_RUNNING);
if (!timeout)
{
printk(KERN_ERR "No response received\n");
return -1;
}
if (message_status != UPDATED)
{
printk(KERN_ERR "Somebody woke us up before we got a reply. Time left %d\n", timeout);
__set_current_state(TASK_INTERRUPTIBLE);
goto wait;
}
当用户模式应用程序回复此消息时,message_status 变量在 netlink 回调中更新。所以基本上这个想法是发送一条消息,然后在最大超时时间等待回复。
现在,使用 gdb,如果我在 netlink 服务器线程在用户模式下调用的任何函数中添加一个断点,断点永远不会被击中,内核日志中会充斥着类似
的消息Somebody woke us up before we got a reply. Time left 3499
Somebody woke us up before we got a reply. Time left 3499
Somebody woke us up before we got a reply. Time left 3499
Somebody woke us up before we got a reply. Time left 3499
..
..
Somebody woke us up before we got a reply. Time left 3498
直到我终于得到
No response received
是什么导致内核线程超时唤醒,我应该如何调试用户态代码?
PS:我在 RHEL 6.0 上使用 2.6.32-71.el6.x86_64
最佳答案
使用 gdb。在 gdb 中,您可以使用“信息线程”来查看线程列表。您可以使用命令“线程”跳转到特定线程,其中 n 是所需的线程。您可以使用“break : thread”设置要中断的特定线程。请参阅手册的这一部分以控制与线程停止相关的程序行为。 IE。您可以指定在您只处理一个线程时停止所有线程。 https://sourceware.org/gdb/onlinedocs/gdb/Thread-Stops.html#Thread-Stops
关于linux - 使用 GDB 调试 netlink 通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10821762/