<p>I am running an application through gdb and I want to set a breakpoint for any time a specific variable is accessed / changed. Is there a good method for doing this? I would also be interested in other ways to monitor a variable in C/C++ to see if/when it changes.</p>

[{'answer_id': 58892, 'author': 'mweerden', 'author_id': 4285, 'author_profile': 'https://Stackoverflow.com/users/4285', 'pm_score': 3, 'selected': False, 'text': '<p>I just tried the following:</p>\n\n<pre><code> $ cat gdbtest.c\n int abc = 43;\n\n int main()\n {\n   abc = 10;\n }\n $ gcc -g -o gdbtest gdbtest.c\n $ gdb gdbtest\n ...\n (gdb) watch abc\n Hardware watchpoint 1: abc\n (gdb) r\n Starting program: /home/mweerden/gdbtest \n ...\n\n Old value = 43\n New value = 10\n main () at gdbtest.c:6\n 6       }\n (gdb) quit\n</code></pre>\n\n<p>So it seems possible, but you do appear to need some hardware support.</p>\n'}, {'answer_id': 59146, 'author': 'asksol', 'author_id': 5577, 'author_profile': 'https://Stackoverflow.com/users/5577', 'pm_score': 9, 'selected': True, 'text': "<p><em>watch</em> only breaks on write, <em>rwatch</em> let you break on read, and <em>awatch</em> let you break on read/write.</p>\n\n<p>You can set read watchpoints on memory locations:</p>\n\n<pre><code>gdb$ rwatch *0xfeedface\nHardware read watchpoint 2: *0xfeedface\n</code></pre>\n\n<p>but one limitation applies to the rwatch and awatch commands; you can't use gdb variables\nin expressions:</p>\n\n<pre><code>gdb$ rwatch $ebx+0xec1a04f\nExpression cannot be implemented with read/access watchpoint.\n</code></pre>\n\n<p>So you have to expand them yourself: </p>\n\n<pre><code>gdb$ print $ebx \n$13 = 0x135700\ngdb$ rwatch *0x135700+0xec1a04f\nHardware read watchpoint 3: *0x135700 + 0xec1a04f\ngdb$ c\nHardware read watchpoint 3: *0x135700 + 0xec1a04f\n\nValue = 0xec34daf\n0x9527d6e7 in objc_msgSend ()\n</code></pre>\n\n<p><strong>Edit:</strong> Oh, and by the way. You need either hardware <strong>or software support</strong>. Software is obviously much slower. To find out if your OS supports hardware watchpoints you can see the <em>can-use-hw-watchpoints</em> environment setting. </p>\n\n<pre><code>gdb$ show can-use-hw-watchpoints\nDebugger's willingness to use watchpoint hardware is 1.\n</code></pre>\n"}, {'answer_id': 966525, 'author': 'Smirnov', 'author_id': 89207, 'author_profile': 'https://Stackoverflow.com/users/89207', 'pm_score': 5, 'selected': False, 'text': '<p>Assuming the first answer is referring to the C-like syntax <code>(char *)(0x135700 +0xec1a04f)</code> then the answer to do <code>rwatch *0x135700+0xec1a04f</code> is incorrect. The correct syntax is <code>rwatch *(0x135700+0xec1a04f)</code>.</p>\n\n<p>The lack of <code>()</code>s there caused me a great deal of pain trying to use watchpoints myself.</p>\n'}, {'answer_id': 17325119, 'author': 'higgs241', 'author_id': 2438536, 'author_profile': 'https://Stackoverflow.com/users/2438536', 'pm_score': 2, 'selected': False, 'text': "<p>Use watch to see when a variable is written to, rwatch when it is read and awatch when it is read/written from/to, as noted above. However, please note that to use this command, you must break the program, and the variable must be in scope when you've broken the program:</p>\n\n<blockquote>\n  <p>Use the watch command. The argument to the watch command is an\n  expression that is evaluated. This implies that the variabel you want\n  to set a watchpoint on must be in the current scope. So, to set a\n  watchpoint on a non-global variable, you must have set a breakpoint\n  that will stop your program when the variable is in scope. You set the\n  watchpoint after the program breaks.</p>\n</blockquote>\n"}, {'answer_id': 31202563, 'author': 'Paolo M', 'author_id': 2508150, 'author_profile': 'https://Stackoverflow.com/users/2508150', 'pm_score': 5, 'selected': False, 'text': '<p>What you\'re looking for is called a <em>watchpoint</em>.</p>\n\n<p><strong>Usage</strong> </p>\n\n<p><code>(gdb) watch foo</code>: watch the value of <strong>variable</strong> <code>foo</code></p>\n\n<p><code>(gdb) watch *(int*)0x12345678</code>: watch the value pointed by an <strong>address</strong>, casted to whatever type you want</p>\n\n<p><code>(gdb) watch a*b + c/d</code>: watch an arbitrarily <strong>complex expression</strong>, valid in the program\'s native language</p>\n\n<p>Watchpoints are of three kinds:</p>\n\n<ul>\n<li><strong>watch</strong>: gdb will break when a <em>write</em> occurs</li>\n<li><strong>rwatch</strong>: gdb will break wnen a <em>read</em> occurs</li>\n<li><strong>awatch</strong>: gdb will break in <em>both cases</em></li>\n</ul>\n\n<p>You may choose the more appropriate for your needs.</p>\n\n<p>For more information, check <a href="https://sourceware.org/gdb/onlinedocs/gdb/Set-Watchpoints.html" rel="noreferrer">this</a> out.</p>\n'}, {'answer_id': 70610117, 'author': 'Singh', 'author_id': 13032809, 'author_profile': 'https://Stackoverflow.com/users/13032809', 'pm_score': 0, 'selected': False, 'text': '<p>In addition to what has already been answered/commented by <a href="https://stackoverflow.com/users/5577/asksol">asksol</a> and <a href="https://stackoverflow.com/users/2508150/paolo-m">Paolo M</a></p>\n<p>I didn\'t at first read understand, why do we need to cast the results. Though I read this: <a href="https://sourceware.org/gdb/onlinedocs/gdb/Set-Watchpoints.html" rel="nofollow noreferrer">https://sourceware.org/gdb/onlinedocs/gdb/Set-Watchpoints.html</a>, yet it wasn\'t intuitive to me..</p>\n<p>So I did an experiment to make the result clearer:\nCode: (Let\'s say that int main() is at Line 3; int i=0 is at Line 5 and other code.. is from Line 10)</p>\n<pre><code>int main()\n{\nint i = 0;\nint j;\ni = 3840 // binary 1100 0000 0000 to take into account endianness\nother code..\n}\n</code></pre>\n<p>then i started gdb with the executable file\nin my first attempt, i set the breakpoint on the location of variable without casting, following were the results displayed</p>\n<pre><code>Thread 1 &quot;testing2&quot; h\nBreakpoint 2 at 0x10040109b: file testing2.c, line 10.\n(gdb) s\n7           i = 3840;\n(gdb) p i\n$1 = 0\n(gdb) p &amp;i\n$2 = (int *) 0xffffcbfc\n(gdb) watch *0xffffcbfc\nHardware watchpoint 3: *0xffffcbfc\n(gdb) s\n[New Thread 13168.0xa74]\n\nThread 1 &quot;testing2&quot; hit Breakpoint 2, main () at testing2.c:10\n10          b = a;\n(gdb) p i\n$3 = 3840\n(gdb) p *0xffffcbfc\n$4 = 3840\n(gdb) p/t *0xffffcbfc\n$5 = 111100000000\n</code></pre>\n<p>as we could see breakpoint was hit for line 10 which was set by me. gdb didn\'t break because although variable i underwent change yet the location being watched didn\'t change (due to endianness, since it continued to remain all 0\'s)</p>\n<p>in my second attempt, i did the casting on the address of the variable to watch for all the sizeof(int) bytes. this time:</p>\n<pre><code>(gdb) p &amp;i\n$6 = (int *) 0xffffcbfc\n(gdb) p i\n$7 = 0\n(gdb) watch *(int *) 0xffffcbfc\nHardware watchpoint 6: *(int *) 0xffffcbfc\n(gdb) b 10\nBreakpoint 7 at 0x10040109b: file testing2.c, line 10.\n(gdb) i b\nNum     Type           Disp Enb Address            What\n6       hw watchpoint  keep y                      *(int *) 0xffffcbfc\n7       breakpoint     keep y   0x000000010040109b in main at testing2.c:10\n(gdb) n\n[New Thread 21508.0x3c30]\n\nThread 1 &quot;testing2&quot; hit Hardware watchpoint 6: *(int *) 0xffffcbfc\n\nOld value = 0\nNew value = 3840\n\nThread 1 &quot;testing2&quot; hit Breakpoint 7, main () at testing2.c:10\n10          b = a;\n</code></pre>\n<p>gdb break since it detected the value has changed.</p>\n'}]
['https://Stackoverflow.com/questions/58851', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3022/']