简体   繁体   English

如何在断点上运行 gdb 中的程序函数?

[英]How to run program functions in gdb on a breakpoint?

I have an array that I am constantly modifying.我有一个不断修改的数组。 After my program is finished executing my modifications don't quite do what I want them to do, so my array doesn't turn out the way that I want.在我的程序完成执行后,我的修改并没有完全按照我的意愿去做,所以我的数组并没有按照我想要的方式运行。 I have a function that reads the contents of the array.我有一个读取数组内容的 function。 Is there a way to use gdb and place a breakpoint somewhere, then run my function that reads the content of the array?有没有办法使用 gdb 并在某处放置断点,然后运行我的 function 来读取数组的内容? I want to find out where the problem occurs.我想找出问题发生在哪里。 Gdb does not let me run "p readArray()". Gdb 不允许我运行“p readArray()”。 f I have a breakpoint. f 我有一个断点。

Use "commands" to run a command whenever you hit a particular breakpoint.每当遇到特定断点时,使用“命令”运行命令。 For example, to run the command on the first breakpoint:例如,要在第一个断点上运行命令:

(gdb) commands 1
Type commands for when breakpoint 1 is hit, one per line.
End with a line saying just "end".
> call readArray()
> end

You can use "info break" to determine the number of the breakpoint you are interested in.您可以使用“信息中断”来确定您感兴趣的断点编号。

It sounds like what you want is to set a watch on the array.听起来您想要的是在阵列上设置手表。 The syntax is watch <expression> - refer to this question for more information on using watches with dynamic arrays (it's C++, but should be the same in C).语法是watch <expression> - 有关使用具有动态 arrays 的手表的更多信息,请参阅此问题(它是 C++,但在 C 中应该相同)。

Set the breakboint at address.在地址设置断点。 Get the address of your array at a point where you malloc or statically create array and set the breakpoint at address.在 malloc 或静态创建数组并在地址处设置断点的位置获取数组的地址。

break *addr "set breakpoint at address addr" break *addr "在地址 addr 处设置断点"

A 'dirty' method is to modify the program-counter register to the address of a location in your code where the display function is called. “脏”方法是将程序计数器寄存器修改为代码调用显示 function 的位置的地址。 Be sure to set a break-point after the call so that you can restore the program-counter to its original value if you want the code to continue correctly thereafter.确保在调用后设置断点,以便如果您希望代码在此后正确继续,您可以将程序计数器恢复为其原始值。

Even dirtier, if the function does not take parameters, is to set the program-counter to the address of the first instruction in the function.更脏的是,如果 function 不带参数,就是将程序计数器设置为 function 中第一条指令的地址。 In this case place a break-point at the return statement and restore the program-counter there, otherwise the return will return to the caller of the function of the first break-point which may not be what you would want.在这种情况下,在 return 语句处放置一个断点并在那里恢复程序计数器,否则返回将返回给第一个断点的 function 的调用者,这可能不是你想要的。

That said, the debugger is perfectly capable of displaying array contents via a "watch", so unless the content requires specific interpretation to make sense of it, that would surely be a better method?也就是说,调试器完全能够通过“手表”显示数组内容,所以除非内容需要特定的解释才能理解,否则这肯定是更好的方法吗?

Another non-debugger work-around would be to have the array implemented as a memory mapped file or shared memory, then use a separate process to map and display the same file or memory. Another non-debugger work-around would be to have the array implemented as a memory mapped file or shared memory, then use a separate process to map and display the same file or memory. This technique would be OS specific.这种技术将是特定于操作系统的。

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

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