<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>technochakra.com &#187; gdb</title>
	<atom:link href="http://www.technochakra.com/tag/gdb/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.technochakra.com</link>
	<description>Wheels Of Technology</description>
	<lastBuildDate>Thu, 04 Feb 2010 11:48:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Debugging &#8211; Modifying Code At Runtime</title>
		<link>http://www.technochakra.com/debugging-modifying-code-at-runtime/</link>
		<comments>http://www.technochakra.com/debugging-modifying-code-at-runtime/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 12:15:44 +0000</pubDate>
		<dc:creator>tc</dc:creator>
				<category><![CDATA[debugging]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[gdb]]></category>
		<category><![CDATA[visualstudio]]></category>

		<guid isPermaLink="false">http://www.technochakra.com/?p=343</guid>
		<description><![CDATA[Introduction The build and debug cycle can be tedious especially when you are unsure whether the change you have in mind solves your problem or not. Sometimes it is good to be able to tweak the logic of your code during a debugging session. This article explains how to make minor adjustments to code without [...]]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>The build and debug cycle can be tedious especially when you are unsure whether the change you have in mind solves your problem or not.  Sometimes it is good to be able to tweak the logic of your code during a debugging session.  This article explains how to make minor adjustments to code without having to rebuild your application.</p>
<p>The article requires tweaking the assembly  and dealing with opcodes.  If you are not very familar with debugging in assembly, you may want to read <a href="http://www.technochakra.com/assembly-and-the-art-of-debugging/" target="_blank">this article</a> first.  The tips shared here are also useful while debugging libraries where you either do not have the source code or the build environment is unavailable.</p>
<p>Debuggers work on the common principle of launching the executable as a child program and requesting special permissions from the operating system to access and alter the state of the program.  This allows it to show and/or modify the memory, registers, code, etc in a debugging session.  If a debugger attaches itself to another program already loaded in memory, it requests special permission from the operating system to take control of the program for the purpose of debugging.  It will now seem very obvious why the same program in memory cannot be attached by two debuggers at the same time.  The operating system caters to the request on a first come first basis and refuses permissions to the second debugger.</p>
<p>The debugger&#8217;s access to a program&#8217;s memory and registers not only allows users to view the state of the program but also set software breakpoints.  Software breakpoints are set by altering and restoring the instructions in memory and has already been covered indepth in an <a href="http://www.technochakra.com/software-breakpoints/" target="_blank">earlier article</a>.</p>
<h3>How to modify code at runtime</h3>
<p>Altering instructions without compilation of code during debugging works on a similar logic.  If one uses the debugger to alter instructions of the program which it controls, then one can essentially change the logic compiled into the executable that is currently loaded in memory.  It is worth noting that altering a program in memory is temporary.  When the program is restarted and reloaded in memory, all changes are lost and no real harm has been done.  As this requires directly tweaking the opcodes in memory, one needs to know how to deal with assembly and this cannot be accomplished with knowledge of a high level language alone.<br />
Independent of the operating system and debugger, the steps needed to change the logic from within the debugger are as follows -</p>
<ol>
<li>Launch the program in a debugger.</li>
<li>Set a breakpoint in the code at the location which you want to alter execution.</li>
<li>Execute the program and drive the program so that your breakpoint is hit.</li>
<li>Request the debugger to display the disassembly of the code.</li>
<li>Get comfortable with the source-assembly mapping.</li>
<li>Identify the address of the assembly line you would like to alter.  Altering usually means changing an if statement to if NOT or &#8220;jump if zero&#8221; to &#8220;jump if not zero&#8221;.</li>
<li>Take the address and dump the memory contents at that address.  You will see opcodes corresponding to the assembly you just saw.</li>
<li>Modify the memory location with new opcodes (more on this later).</li>
<li>Cross your fingers so that your change won&#8217;t cause a crash <img src='http://www.technochakra.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </li>
<li>Disable the breakpoint so that execution doesn&#8217;t stop too many times at this breakpoint as it makes debugging distracting.</li>
<li>Continue execution and now the program will respond to the new logic in the program.</li>
<li>To undo the effect of change, restart the program.</li>
</ol>
<h3>Example</h3>
<p>Let us look at the following simple snippet of code below.</p>
<p><code> bool flag = true;<br />
if( !flag )<br />
cout &lt;&lt; "flag is false" ;<br />
else<br />
cout &lt;&lt; "flag is true" ; </code></p>
<p>The code when compiled and run normally would print &#8220;flag is true&#8221; and this is obvious from the code above.  Say we at runtime would like to alter the logic by replacing if(!flag) with if(flag) so that the statement &#8220;flag is false&#8221; is printed instead.<br />
Let us see how to do this with the two most commonly available debuggers.</p>
<h4>GDB</h4>
<p>The steps below are in the same order as the generic steps explained earlier.</p>
<ul>
<li>gdb &lt;myprogram&gt;</li>
<li>break &lt;line_number_of_if_statement&gt;</li>
<li>run   Code will stop at the breakpoint set above.</li>
<li>disassemble.  The dump on my machine is as follows -</li>
</ul>
<blockquote><p>0&#215;00401170 &lt;main+32&gt;:   call   0x40f260 &lt;_alloca&gt;<br />
0&#215;00401175 &lt;main+37&gt;:   call   0&#215;410410 &lt;__main&gt;<br />
0x0040117a &lt;main+42&gt;:   movb   $0&#215;1,-0&#215;1(%ebp)<br />
0x0040117e &lt;main+46&gt;:   cmpb   $0&#215;0,-0&#215;1(%ebp)<br />
0&#215;00401182 &lt;main+50&gt;:   jne    0x40119a &lt;main+74&gt;<br />
0&#215;00401184 &lt;main+52&gt;:   movl   $0&#215;443000,0&#215;4(%esp)<br />
0x0040118c &lt;main+60&gt;:   movl   $0x4463a0,(%esp)<br />
0&#215;00401193 &lt;main+67&gt;:   call   0x43e720 &lt;_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc&gt;<br />
0&#215;00401198 &lt;main+72&gt;:   jmp    0x4011ae &lt;main+94&gt;<br />
0x0040119a &lt;main+74&gt;:   movl   $0x44300e,0&#215;4(%esp)<br />
0x004011a2 &lt;main+82&gt;:   movl   $0x4463a0,(%esp)<br />
0x004011a9 &lt;main+89&gt;:   call   0x43e720 &lt;_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc&gt;<br />
0x004011ae &lt;main+94&gt;:   mov    $0&#215;0,%eax</p></blockquote>
<ul>
<li>Note that main+67 and main+89 are calls to cout.  We have now identified the two important statements and the if check should be just above it.  It usually helps to corelate the assembly with the source code in this manner.</li>
<li>The if check is on lines main+42, main+46 and main+50 (if !(flag==0) ).  If we change jne to je, we can negate the if condition and thus alter the logic of the code.  The address of the opcode jne above is 0&#215;00401182.</li>
<li>x/6x 0&#215;00401182  will dump the opcodes and operands in hexadecimal.  In the dump below, 0&#215;75 is the opcode for jne and the operand is 0&#215;16.  For details, refer to the Intel instruction set.</li>
</ul>
<blockquote><p>0&#215;401182 &lt;main+50&gt;:     0&#215;75    0&#215;16    0xc7    0&#215;44    0&#215;24    0&#215;04</p></blockquote>
<ul>
<li>set *(char*)0&#215;00401182=0&#215;74 where 0&#215;74 is the opcode for je. Effectively &#8220;jump if not equal&#8221; has been changed to &#8220;jump if equal&#8221; thereby inverting the logic of the if statement.</li>
<li>cross your fingers.</li>
<li>disable  1.  Disable breakpoint set earlier.</li>
<li>continue</li>
</ul>
<p>You will observe that as we have modified the runtime code using gdb, the output of the program after the above steps would be &#8220;flag is false&#8221;.</p>
<h4>Visual Studio</h4>
<p><a href="http://www.technochakra.com/wp-content/uploads/assembly_vs.jpg"><img class="alignnone size-full wp-image-345" title="assembly_vs" src="http://www.technochakra.com/wp-content/uploads/assembly_vs.jpg" alt="Assembly Screen Shot in Visual Studio" width="566" height="111" /></a><br />
The above screen shot shows the debugger displaying the assembly after hitting the breakpoint at the if statement.  Do make sure that when you right click in the assembly, &#8220;Show Code Bytes&#8221; and &#8220;Show Address&#8221; are checked.<br />
From the screen shot above, the address of jne instruction is 0x003314B8 and its opcode is 0&#215;75.  If we modify the opcode at location 0x003314B8 and change it to 0&#215;74 (opcode for je), we effectively negate the if statement at runtime.</p>
<p>To do this, open the memory window (Debug-&gt;Windows-&gt;Memory-&gt;Memory 1) and enter 0x003314B8 in the Address field.  The first byte shows as 0&#215;75.  Put the cursor on 0&#215;75 and type 74 to change the opcode.  You can confirm this by seeing the effect in the disassembly window.</p>
<p>This modifies the code in memory and now when the program execution is continued, the output would be &#8220;flag is false&#8221;.</p>
<h4>Conclusion</h4>
<p>Another useful tip to keep in mind is that a lot of code can be disabled at runtime if all corresponding opcodes and operands are replaced with NOPs which has the opcode 0&#215;90 on an Intel machine.  Similarly useful results can also be achived by altering the operands instead of the opcodes.</p>
<p>The ability to change code at runtime is an effective tool for quick debugging though it requires a good understanding of the code at the assembly level.   This technique is meant for small tweaks because it is faster to recompile in case complex changes in assembly are needed.   Moreover, when the program finishes execution, the modification needs to be redone as this technique does not modify the image on the disk.</p>
<p style="text-align: center;"><script type="text/javascript"><!--
google_ad_client = "pub-2898169159040774";
/* 468x60, created 7/1/09 */
google_ad_slot = "5882807646";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>]]></content:encoded>
			<wfw:commentRss>http://www.technochakra.com/debugging-modifying-code-at-runtime/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Cross-platform Debugging Cheat Sheet</title>
		<link>http://www.technochakra.com/cross-platform-debugging-cheat-sheet/</link>
		<comments>http://www.technochakra.com/cross-platform-debugging-cheat-sheet/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 06:06:36 +0000</pubDate>
		<dc:creator>tc</dc:creator>
				<category><![CDATA[debugging]]></category>
		<category><![CDATA[gdb]]></category>
		<category><![CDATA[visualstudio]]></category>
		<category><![CDATA[windbg]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://www.technochakra.com/?p=231</guid>
		<description><![CDATA[If you work on multiple platforms and use different debuggers, you are expected to know the debugger&#8217;s user interfaces well enough.  At times this gets confusing especially if you have one primary  platform and you work on other platforms rather infrequently.   I have compiled a list of my favorite features in a debugger and [...]]]></description>
			<content:encoded><![CDATA[<p>If you work on multiple platforms and use different debuggers, you are expected to know the debugger&#8217;s user interfaces well enough.  At times this gets confusing especially if you have one primary  platform and you work on other platforms rather infrequently.  </p>
<p>I have compiled a list of my favorite features in a debugger and how to invoke them on different debuggers (Visual Studio, XCode, gdb and Windbg). </p>
<p>This is not a substitute for the debugger&#8217;s documentation but helpful for quickly switching to an unfamiliar debugging environment.  Click the image below for viewing the table or download the PDF version as a ready reference.</p>
<p><a rel="attachment wp-att-249" href="http://www.technochakra.com/cross-platform-debugging-cheat-sheet/debuggersv01/"><img class="alignnone size-medium wp-image-249" title="Debugger Cheatsheet" src="http://www.technochakra.com/wp-content/uploads/debuggersV01-300x212.jpg" alt="Debugger Cheatsheet" width="300" height="212" /></a></p>
<p><a href="http://www.technochakra.com/wp-content/uploads/debuggersV01.pdf" target="_blank">Debugger Cheat Sheet (Download PDF)</a></p>
<p>Please note :</p>
<ul>
<li>The commands (especially for gdb) are not necessarily complete and the debugger&#8217;s help should be consulted for detailed usage.</li>
<li>The list is not comprehensive and I have only put in my favorite commands that I use while debugging.  </li>
<li>A square bracket [ ] denotes a keyboard shortcut.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.technochakra.com/cross-platform-debugging-cheat-sheet/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Debugging &#8211; Types Of Data Breakpoints In GDB</title>
		<link>http://www.technochakra.com/debugging-types-of-data-breakpoints-in-gdb/</link>
		<comments>http://www.technochakra.com/debugging-types-of-data-breakpoints-in-gdb/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 17:33:46 +0000</pubDate>
		<dc:creator>tc</dc:creator>
				<category><![CDATA[debugging]]></category>
		<category><![CDATA[breakpoints]]></category>
		<category><![CDATA[gdb]]></category>

		<guid isPermaLink="false">http://www.technochakra.com/?p=216</guid>
		<description><![CDATA[Data breakpoints are now becoming a part of common breakpoint vocabulary. They help in detecting heap corruption, inadvertent data overwrites and writing past buffer boundaries. Most programmer&#8217;s restrict the definition of data breakpoints to breakpoints that help halting the execution of code in the debugger when memory is written to. This is the kind of [...]]]></description>
			<content:encoded><![CDATA[<p>Data breakpoints are now becoming a part of common breakpoint vocabulary.  They help in detecting heap corruption, inadvertent data overwrites and writing past buffer boundaries.</p>
<p>Most programmer&#8217;s restrict the definition of data breakpoints to breakpoints that help halting the execution of code in the debugger when memory is written to.  This is the kind of breakpoint that helps in catching most of the corruption bugs.</p>
<p>GDB provides data breakpoints (at least on Intel platforms) that do slightly more than that.  As data breakpoints are implemented through hardware assistance, it is the hardware platform that provides the different kinds of data breakpoints and the debuggers provide the required interface.</p>
<h3>watch</h3>
<p>watch is gdb&#8217;s way of setting data breakpoints which will halt the execution of a program if memory changes at the specified location.</p>
<p>watch breakpoints can either be set on the variable name or any address location.</p>
<blockquote><p>watch my_variable<br />
watch *0&#215;12345678<br />
where 0&#215;12345678 is a valid address.</p></blockquote>
<p>Usually a crash because of heap corruption or invalid outcome due to buffer overruns shows up in the debugger when it is too late to figure out what went wrong.  The watch or write data breakpoints can be used to find when a memory location has changed.  The debugger at that very instant shows the reason for the inadvertent change.</p>
<p>The cause usually is double deletion of memory, writing to deleted memory, writing past the buffer boundary, etc.  In order to fix such issues, it is more important to know when the corruption happens than to know what happens when the corruption has taken place.</p>
<p>Another interesting use of write data breakpoints is to find out the cause of memory leaks in reference counted objects by monitoring the increase and decrease in the reference count of the objects.</p>
<h3>rwatch</h3>
<p>rwatch (read-watch) breakpoints break the execution of code when the program tries to read from a variable or memory location.</p>
<blockquote><p>rwatch iWasAccessed<br />
rwatch *0&#215;12345678<br />
where 0&#215;12345678 is a valid address.</p></blockquote>
<p>For example, say you are new to your project and would like to figure out where exactly are your encryption routines in code.  As a start, you can first search your codebase but you may hit a few false positives.  If you know the memory location of your password and you set your rwatch breakpoints correctly, it would not be long before the debugger breaks execution right in your encryption algorithms which have to read the password in order to perform their function.  Getting evil ideas already?</p>
<p>Another use of read data breakpoints is finding out the code that is reading from memory that has already been deleted and is using this corrupt information later.</p>
<h3>awatch</h3>
<p>awatch or access watches break execution of the program if a variable or memory location is written to or read from.  In summary, awatches are watches and rwatches all in one.  It is a handy way of creating one breakpoint than two separate ones.</p>
<blockquote><p>awatch *0&#215;12345678<br />
where 0&#215;12345678 is a valid address.</p></blockquote>
<p>The problem with data breakpoints like any other breakpoint is that they can be triggered far too many times and the programmer may lose track of the problem being debugged.  To ensure the breakpoints are hit the least number of times, the <a href="http://www.technochakra.com/debugging-divide-and-conquer-the-input-data">data being worked upon should be minimal</a> and the breakpoint should be set as late as possible.<br />
<DIV ALIGN=CENTER><br />
<iframe src="http://rcm.amazon.com/e/cm?t=technochakra-20&#038;o=1&#038;p=6&#038;l=st1&#038;mode=books&#038;search=gdb%20debugging%20&#038;fc1=000000&#038;lt1=&#038;lc1=3366FF&#038;bg1=FFFFFF&#038;f=ifr" marginwidth="0" marginheight="0" width="120" height="150" border="0" frameborder="0" style="border:none;" scrolling="no"></iframe><br />
</DIV></p>
<p>
]]></content:encoded>
			<wfw:commentRss>http://www.technochakra.com/debugging-types-of-data-breakpoints-in-gdb/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Software Breakpoints</title>
		<link>http://www.technochakra.com/software-breakpoints/</link>
		<comments>http://www.technochakra.com/software-breakpoints/#comments</comments>
		<pubDate>Wed, 13 May 2009 19:05:53 +0000</pubDate>
		<dc:creator>tc</dc:creator>
				<category><![CDATA[debugging]]></category>
		<category><![CDATA[breakpoints]]></category>
		<category><![CDATA[gdb]]></category>
		<category><![CDATA[visualstudio]]></category>

		<guid isPermaLink="false">http://www.technochakra.com/?p=192</guid>
		<description><![CDATA[It is very useful to be able to break execution of code at a line number of your choice.  Breakpoints are provided in debuggers to do exactly that.  It is fun getting to the root of the problem by setting breakpoints in a debug session.  It is even more fun to know how do breakpoints work in the first place. ]]></description>
			<content:encoded><![CDATA[<p><em>Line breakpoints can be implemented either in hardware or software.  This article discusses the latter in detail. </em></p>
<p>It is very useful to be able to break execution of code at a line number of your choice.  Breakpoints are provided in debuggers to do exactly that.  It is fun getting to the root of the problem by setting breakpoints in a debug session.  It is even more fun to know how do breakpoints work in the first place.</p>
<p>Software breakpoints work by  inserting a special instruction in the program being debugged.  This special instruction on the Intel platform is &#8220;int 3&#8243;.  When executed it calls the debugger&#8217;s exception handler.</p>
<h3>Example</h3>
<p>Let us look at a very simple example that inserts a breakpoint in a program at compile time and not through a debugger.  The code uses the Intel instruction &#8220;int 3&#8243; and you may need to figure out the equivalent instruction for a non-Intel platform.<br />
<code><br />
// The code below works well with Visual Studio.<br />
int main()<br />
{<br />
__asm int 3;<br />
printf("Hello World\n");<br />
return 0;<br />
}</code><br />
<code><br />
// The code below works well with gcc + gdb<br />
int main()<br />
{<br />
asm("int $3");<br />
printf("Hello World\n");<br />
return 0;<br />
}</code></p>
<p>If you run this program in Visual Studio, you get a dialog saying &#8220;<em><strong>helloworld</strong><strong>.exe has triggered a breakpoint</strong></em>&#8220;.</p>
<p>In gdb you get the message &#8220;<strong><em>Program received signal SIGTRAP, Trace/breakpoint trap.</em></strong>&#8221;</p>
<p>In the example above, a call to &#8220;int 3&#8243; invokes the debugger&#8217;s exception handler.</p>
<p>It is also interesting to note the assembly instructions generated for the program above.</p>
<p>In Visual Studio, right click on the code and click on &#8220;Show Disassembly&#8221;. Also ensure that &#8220;Show Code Bytes&#8221; is on in the same context menu.</p>
<div class="mceTemp">
<dl id="attachment_198" class="wp-caption alignnone" style="width: 455px;">
<dt class="wp-caption-dt"><img class="size-full wp-image-198" title="vs2008_disassembly" src="http://www.technochakra.com/wp-content/uploads/vs2008_disassembly.jpg" alt="Visual Studio 2008 Disassembly" width="445" height="49" /></dt>
</dl>
</div>
<p>In gdb type disassemble at the gdb command.</p>
<blockquote><p>0x0040107a &lt;main+42&gt;:   int3</p></blockquote>
<p>Now obtain the opcode of the int3 instruction using the x (examine memory) command</p>
<blockquote><p>(gdb) x/x main+42</p>
<p>0x40107a &lt;main+42&gt;:     0xcc</p></blockquote>
<p>As seen above, the breakpoint opcode we inserted during compilation is 0xCC .</p>
<h3>How Do Debuggers Insert Breakpoints?</h3>
<p>For practical reasons, it is unwise to ask for a recompilation whenever a breakpoint is added or deleted.  Debuggers change the loaded image of the executable in memory and insert the &#8220;int 3&#8243; instruction at runtime.  The common steps a debugger performs to provide the functionality of a line breakpoint to a user are as follows -</p>
<ol>
<li> When a user inserts a breakpoint in a line of code, the debugger saves the opcode at that given location and replaces it with 0xCC (int 3).</li>
<li>When the program is run and it executes the &#8220;int 3&#8243; instruction, control is passed to the debugger&#8217;s exception handler.</li>
<li>The debugger notifies the user that a breakpoint has been hit.  Say that the user instructs the debugger to resume execution of the program.</li>
<li>The debugger replaces the opcode 0xCC with the one it had saved earlier.  This is done to restore the instructions to their original state.</li>
<li>The debugger then single steps the program.</li>
<li>It then resaves the original instruction and re-inserts the opcode 0xCC.  If this step were not done, the breakpoint would have been lost.  <a href="http://www.technochakra.com/temporary-breakpoint-now-you-see-it-now-you-dont/">Temporary breakpoints</a> on the other hand skip this step.</li>
<li>The debugger then resumes execution of the program.</li>
</ol>
<p>Hardware breakpoints are limited in number but debuggers are able to provide unlimited breakpoints by implementing them through software.</p>
<p>Knowing what goes behind the scenes makes debugging a bit easier.  A debugger may defer setting a breakpoint if the module is not loaded in memory yet.  It needs to replace some opcode with 0xCC and that can happen only when the module is in memory.  Likewise, a mismatch between a binary, its sources and its debug symbols (or the lack of it) may cause breakpoints to be hit at unexpected locations because the debugger is not able to correctly map the source line to the opcode that it needs to replace with 0xCC.  At times debuggers complain about the mismatch and refuse to set the breakpoints.</p>
<p>Many of the setup issues with breakpoints become obvious once we know how they work internally.  And when all else fails and release build breakpoints  adamantly refuse to work, you always have the option of compiling an &#8220;int 3&#8243; breakpoint right into your code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.technochakra.com/software-breakpoints/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Temporary Breakpoint &#8211; Now You See It, Now You Don&#8217;t</title>
		<link>http://www.technochakra.com/temporary-breakpoint-now-you-see-it-now-you-dont/</link>
		<comments>http://www.technochakra.com/temporary-breakpoint-now-you-see-it-now-you-dont/#comments</comments>
		<pubDate>Tue, 19 Aug 2008 18:00:02 +0000</pubDate>
		<dc:creator>tc</dc:creator>
				<category><![CDATA[debugging]]></category>
		<category><![CDATA[breakpoints]]></category>
		<category><![CDATA[gdb]]></category>
		<category><![CDATA[windbg]]></category>

		<guid isPermaLink="false">http://www.technochakra.com/?p=126</guid>
		<description><![CDATA[Have you faced the problem of breakpoint clutter where breakpoints keep piling up only to hinder the debugging session?  It is then that one realizes that there are some breakpoints that can be deleted and others disabled.  A useful feature in a debugger is a temporary breakpoint that automagically gets deleted when hit thereby reducing the clutter of unnecessary breakpoints.  These [...]]]></description>
			<content:encoded><![CDATA[<p>Have you faced the problem of breakpoint clutter where breakpoints keep piling up only to hinder the debugging session?  It is then that one realizes that there are some breakpoints that can be deleted and others disabled. </p>
<p>A useful feature in a debugger is a temporary breakpoint that automagically gets deleted when hit thereby reducing the clutter of unnecessary breakpoints.  These breakpoints are useful when you wish to stop at a code location only once and do not require the execution to stop at that location ever again. </p>
<p>For example, say you are trying to determine whether a particular test scenario invokes a specific line of code or not, in that case a temporary breakpoint can be used as the breakpoint is not useful once it has been hit alteast once.</p>
<p>Below are steps on how to set temporary breakpoints in various debuggers.</p>
<h2>gdb</h2>
<p>Use the<em> <strong>tb</strong></em> command to set a temporary breakpoint in gdb.  It is similar to the <em><strong>break </strong></em>command but the breakpoint will automatically be deleted when hit.</p>
<blockquote><p><strong><span style="text-decoration: underline;">(gdb)</span></strong>help tb<br />
Set a temporary breakpoint.<br />
Like &#8220;break&#8221; except the breakpoint is only temporary,<br />
so it will be deleted when hit.  Equivalent to &#8220;break&#8221; followed<br />
by using &#8220;enable delete&#8221; on the breakpoint number.<br />
    </p></blockquote>
<h2>Windbg</h2>
<p>In Windbg, breakpoints set in the Command window using the <strong><em>bl /1</em></strong> command can be used to create temporary breakpoints.  The <strong><em>/1 </em></strong>tells Windbg that the breakpoint should be deleted when hit.</p>
<p>In Windbg temporary breakpoints are also known as &#8220;one shot breakpoints&#8221;.</p>
<h2>Visual Studio</h2>
<p>I found it a bit painful to create temporary breakpoints in Visual Studio.  The only way I could create one was by setting a breakpoint and then setting the hit count for the breakpoint to be equal to 1.   The  article <a href="http://www.technochakra.com/debugging-using-breakpoint-hit-count-for-fun-and-profit#vs_hitcount">here</a> explains how to set a hit count in Visual Studio.</p>
<p>The amount of work involved to do this sometimes doesn&#8217;t make temporary breakpoints worthwhile to set.  Moreover the breakpoint lingers on and doesn&#8217;t actually get deleted when hit.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.technochakra.com/temporary-breakpoint-now-you-see-it-now-you-dont/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>GDB &#8211; Debugging In Assembly</title>
		<link>http://www.technochakra.com/gdb-debugging-in-assembly/</link>
		<comments>http://www.technochakra.com/gdb-debugging-in-assembly/#comments</comments>
		<pubDate>Tue, 05 Aug 2008 17:55:13 +0000</pubDate>
		<dc:creator>tc</dc:creator>
				<category><![CDATA[debugging]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[gdb]]></category>

		<guid isPermaLink="false">http://www.technochakra.com/?p=112</guid>
		<description><![CDATA[If you haven&#8217;t debugged in assembly yet, you haven&#8217;t debugged enough   Debugging assembly in gdb can be tricky at times. A normal debugging session involving assembly language consists of the following steps - Launch the program in gdb and set the required breakpoint(s). When the breakpoint is hit, use the disassemble command to view [...]]]></description>
			<content:encoded><![CDATA[<p>If you haven&#8217;t debugged in assembly yet, you haven&#8217;t debugged enough <img src='http://www.technochakra.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />   Debugging assembly in gdb can be tricky at times.</p>
<p>A normal debugging session involving assembly language consists of the following steps -</p>
<ol>
<li>Launch the program in gdb and set the required breakpoint(s).</li>
<li>When the breakpoint is hit, use the <em>disassemble</em> command to view the assembly language of the current frame.</li>
<li>Use the <em>frame</em> command and figure out the program counter and thus your location in the assembly that gdb produced in the last step.</li>
<li>Use the <em>si</em> and <em>ni</em> commands to step in and step over the instructions in assembly language.</li>
</ol>
<p>The problem is that this may require a bit of scrolling to do if the disassembled function is very long.  The display command can be used to solve this problem.</p>
<p>The display command in gdb allows a user to configure the variables that should show up each time the program is suspended.</p>
<blockquote><p><span style="text-decoration: underline;">(gdb)</span> <strong>help display</strong><br />
Print value of expression EXP each time the program stops.<br />
/FMT may be used before EXP as in the &#8220;print&#8221; command.<br />
/FMT &#8220;i&#8221; or &#8220;s&#8221; or including a size-letter is allowed,<br />
as in the &#8220;x&#8221; command, and then EXP is used to get the address to examine<br />
and examining is done as in the &#8220;x&#8221; command.</p>
<p>With no argument, display all currently requested auto-display expressions.<br />
Use &#8220;undisplay&#8221; to cancel display requests previously made.</p></blockquote>
<p>One can use the display command to view the next instruction that is about to be executed.  The pointer to the instruction yet to be executed is stored in the computer&#8217;s program counter (also known as the instruction pointer).   The following command will show the next 3 instructions that are pointed to by the program counter.</p>
<blockquote><p>display /3i $pc</p></blockquote>
<p>The /3i above causes three instructions to be printed and $pc references the program counter register.  A sample output is shown below.</p>
<blockquote><p>1: x/3i $pc<br />
0&#215;401175 &lt;main+37&gt;:     call   0x4103f0 &lt;__main&gt;<br />
0x40117a &lt;main+42&gt;:     lea    0xffffffe8(%ebp),%eax<br />
0x40117d &lt;main+45&gt;:     mov    %eax,(%esp)<br />
<span style="text-decoration: underline;">(gdb)</span></p></blockquote>
<p>
<DIV ALIGN=CENTER><br />
<iframe src="http://rcm.amazon.com/e/cm?t=technochakra-20&#038;o=1&#038;p=6&#038;l=st1&#038;mode=books&#038;search=debugging%20%20assembly&#038;fc1=000000&#038;lt1=&#038;lc1=3366FF&#038;bg1=FFFFFF&#038;f=ifr" marginwidth="0" marginheight="0" width="120" height="150" border="0" frameborder="0" style="border:none;" scrolling="no"></iframe><br />
</DIV></p>
<p>
]]></content:encoded>
			<wfw:commentRss>http://www.technochakra.com/gdb-debugging-in-assembly/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
