开发者

How to run program functions in gdb on a breakpoint?

开发者 https://www.devze.com 2023-03-10 03:23 出处:网络
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 hav

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. Is there a way to use gdb and place a breakpoint somewhere, then run my function that reads 开发者_如何学编程the content of the array? I want to find out where the problem occurs. Gdb does not let me run "p readArray()". f I have a breakpoint.


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.


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.

break *addr "set breakpoint at address 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. 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. 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.

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. This technique would be OS specific.

0

精彩评论

暂无评论...
验证码 换一张
取 消