0

I am currently trying to analyze the behavior of a large complicated function which takes in lots of pointer inputs. Consider the following signature.

int myfunc(typeA *paramA, typeB *paramB);

which is being invoked as

myfunc(argA, argB);

Is it possible to watch with the debugger if the pointer locations of argA and argB were written to? Or is it only possible to watch whether the memory location changed (that is definitely not happening in my case)?

I want to check the difference in these pointer arguments before and after the function call. Is this watch possible?

Note that these classes/structs being passed are huge having other pointers to classes/structs. So, watching each variable one by one would be my last resort

psiyumm
  • 6,437
  • 3
  • 29
  • 50

2 Answers2

0

Since you've tagged your post with CLion, I assume that's the IDE you're using. You may want to read this post: https://blog.jetbrains.com/clion/2015/05/debug-clion/

Specifically the part on Watches:

Capturing every single variable at every single point results in far too much information. Sometimes, you want to focus on a specific variable and the way it changes throughout program execution, including monitoring changes when the variable in question is not local to the code you are inspecting. This is what the Watch area of the Debug tool window is for.

To start watching a variable, simply press the Add button (Alt+Insert (Windows/Linux)/⌘N (OS X)) and type in the name of the variable to watch. Code completion is available here too.

Per your comment: You have options to see when the memory is written to: Can I set a breakpoint on 'memory access' in GDB?

Otherwise if you just want to know if the value is changed for debugging, just copy the value before you call the function:

typeA copyOfA = *argA;
myfunc(&copyOfA, argB);

if (copyOfA != *argA)
{
    // It's changed
}
Community
  • 1
  • 1
dempzorz
  • 1,019
  • 13
  • 28
0

Not sure I'm getting your question exactly, and I don't know whether clion gives you access to the lldb script interpreter, but given this example:

struct Foo
{
  int a;
  int b;
  int c;
};

void ChangeFoo(struct Foo *input)
{
  input->a += 10;
}

int
main()
{
  struct Foo my_foo = {10, 20, 30};
  ChangeFoo(&my_foo);
  return 0;
}

from command-line lldb you can do:

(lldb) br s -l 17
Breakpoint 1: where = tryme`main + 39 at tryme.c:17, address = 0x0000000100000f97
(lldb) br s -l 18
Breakpoint 2: where = tryme`main + 46 at tryme.c:18, address = 0x0000000100000f9e
(lldb) run
Process 16017 launched: '/tmp/tryme' (x86_64)
Process 16017 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x0000000100000f97 tryme`main at tryme.c:17
   14   main()
   15   {
   16     struct Foo my_foo = {10, 20, 30};
-> 17     ChangeFoo(&my_foo);
          ^
   18     return 0;
   19   }
Target 0: (tryme) stopped.
(lldb) script value = lldb.frame.FindVariable("my_foo")
(lldb) script print value
(Foo) my_foo = {
  a = 10
  b = 20
  c = 30
}
(lldb) n
Process 16017 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
    frame #0: 0x0000000100000f9e tryme`main at tryme.c:18
   15   {
   16     struct Foo my_foo = {10, 20, 30};
   17     ChangeFoo(&my_foo);
-> 18     return 0;
          ^
   19   }
Target 0: (tryme) stopped.
(lldb) script
Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
>>> for i in range(0,value.GetNumChildren()):
...     print(i, " ", value.GetChildAtIndex(i).GetValueDidChange())
... 
(0, ' ', True)
(1, ' ', False)
(2, ' ', False)
>>> print value.GetChildAtIndex(0)
(int) a = 20

Note, if my_foo above had been a pointer, we would have only fetched the pointer value which isn't what you want to compare. In that case, when you capture the value do:

(lldb) script value = lldb.frame.FindVariable("my_foo_ptr").Dereference()

where you get the value originally, and then everything after will go as above.

Jim Ingham
  • 25,260
  • 2
  • 55
  • 63