简体   繁体   English

使用gdb进入swapcontext()

[英]Stepping into swapcontext() with gdb

I'm doing a project that makes unix system calls. 我正在做一个调用unix系统调用的项目。 To be specific, my project heavily relies of calls to getcontext(), makecontext(), swapcontext() and setcontext(). 具体来说,我的项目很大程度上依赖于对getcontext(),makecontext(),swapcontext()和setcontext()的调用。 I tried to debug my code using gdb. 我尝试使用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. 我逐行进入代码并检查控件但是一旦调用swapcontext(),它就不再进入代码了。 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? 我猜gdb不会进入上下文调用? Is there any way to get around this? 有没有办法解决这个问题? Is there a debugger that i can use for this? 有没有我可以使用的调试器? Thanks 谢谢

setcontext and swapcontext calls change the program's stack, and gdb gets confused. setcontext和swapcontext调用改变了程序的堆栈,gdb变得混乱。 I do not know whether some other debugger can handle it nicely. 我不知道其他调试器是否可以很好地处理它。

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()). 使用带有'step'或'next'的swapcontext()调用的gdb步进不起作用,因为不仅stackpointer更改,而且调用返回到不同的代码行(这是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. 由于gdb在下一个代码行中放置一个断点,直到另一个swapcontext()返回到该位置才会执行,执行不会中断。

You need to foresee the line to which swapcontext() will return and set a breakpoint there. 您需要预见swapcontext()将返回的行并在那里设置断点。 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... 对于使用过的上下文,它可能是swapcontext()之后的一行...

You can repeatedly use GDB's stepi command to first step into and then step through the swapcontext() function. 您可以重复使用GDB的stepi命令首先进入,然后逐步执行swapcontext()函数。 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 steps through one thread and calls this the current thread. gdb逐步执行一个线程并将其调用为当前线程。 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. 如果设置的断点在当前线程以外的线程中被命中,则gdb会将当前线程更改为该线程。 Stepping is now relative to the new current thread. 步进现在相对于新的当前线程。

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. 对于像GDB和Valgrind这样的调试器,线程程序不能很好地运行(至少根据我的经验),并且大多数错误可以通过对代码进行仔细的逐步手动分析来确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM