使用 gdb 进入 swapcontext()
我正在做一个进行unix系统调用的项目。具体来说,我的项目严重依赖对 getcontext()、makecontext()、swapcontext() 和 setcontext() 的调用。我尝试使用 gdb 调试我的代码。我逐行进入代码并检查了控件,但是一旦调用了 swapcontext(),它就不再进入代码了。相反,调试几乎就此停止,程序的其余部分会自动运行,而不是逐行运行。我猜 gdb 不会进入上下文调用?有什么办法可以解决这个问题吗?有没有可以用于此目的的调试器? 谢谢
I'm doing a project that makes unix system calls. To be specific, my project heavily relies of calls to getcontext(), makecontext(), swapcontext() and setcontext(). I tried to debug my code using gdb. I stepped into the code line by line and examined the control but once a call to swapcontext() is made, it doesn't step into the code anymore. Instead, debugging almost stops there and the rest of the program is run automatically instead of line by line. I guess gdb does not go into context calls? Is there any way to get around this? Is there a debugger that i can use for this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
setcontext 和 swapcontext 调用会更改程序的堆栈,并且 gdb 会感到困惑。我不知道其他调试器是否可以很好地处理它。
setcontext and swapcontext calls change the program's stack, and gdb gets confused. I do not know whether some other debugger can handle it nicely.
gdb 单步执行一个线程并将其称为当前线程。当您执行此操作时,其他线程将运行。如果您设置的断点在当前线程以外的线程中命中,则 gdb 会将当前线程更改为该线程。现在步进是相对于新的当前线程而言的。
gdb steps through one thread and calls this the current thread. Other threads will run as you are doing this. If you set a breakpoint that gets hit in a thread other than the current thread then gdb will change the current thread to that thread. Stepping is now relative to the new current thread.
使用 gdb 单步执行带有“step”或“next”的 swapcontext() 调用不起作用,因为不仅堆栈指针发生变化,而且调用返回到不同的代码行(这是 swapcontext() 所需的效果)。由于 gdb 在下一个代码行中放置了一个断点,直到另一个 swapcontext() 返回到此位置时才会执行该断点,因此执行不会中断。
您需要预见 swapcontext() 将返回的行并在那里设置断点。对于新的(未使用的)上下文,这将是您指定为入口函数的行。对于使用过的上下文,它可能是 swapcontext() 之后的行之一......
Stepping with gdb over calls of swapcontext() with 'step' or 'next' does not work because not only the stackpointer changes but also the call returns to a different code line (which is the desired effect of swapcontext()). As gdb puts a breakpoint in the next code line which will not be executed until another swapcontext() returns to this place the execution will not break.
You need to foresee the line to which swapcontext() will return and set a breakpoint there. For a new (unused) context this will be the line you specified as entry function. For used contexts it will probably one of the lines after a swapcontext() there...
您可以重复使用 GDB 的
stepi
命令首先单步执行swapcontext()
函数,然后单步执行。您必须执行数十次,包括内核系统调用上的几个步骤(我想保存浮点状态?),并且您最终将进入要交换到的用户线程。这有点耗时,但很有效。You can repeatedly use GDB's
stepi
command to first step into and then step through theswapcontext()
function. You must step several dozen times, including a few steps over kernel system calls—I presume to save the floating point state?—and you'll eventually come out in the user thread you're swapping to. It's a tad time-consuming, but it works.尽管您可能不喜欢这个答案,但最好的选择是手动分小块地逐步执行代码。线程程序不能很好地与 GDB 和 Valgrind 等调试器配合使用(至少根据我的经验),并且大多数错误可以通过仔细地逐步手动分析代码来确定。
As much as you may not like this answer, the best bet is to step through your code in small chunks by hand. Threaded programs don't play very well with debuggers like GDB and Valgrind (at least in my experience) and most bugs can be determined by a careful step-by-step manual analysis of the code.