2

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.

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Your GDB fu is obviously better than mine. My suggested "dirty" solution works in debuggers that do not have the ability to call arbitrary functions, but I guess that's irrelevant in this case.
0

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"

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.